JonathanWilbur / asn1-ts

ASN.1 TypeScript library, including codecs for Basic Encoding Rules (BER) and Distinguished Encoding Rules (DER).
MIT License
32 stars 6 forks source link

Question ASN1 DER Encoding/Decoding Class=APPLICATION TAGNUMBER=31 #16

Closed Axel-EH closed 2 years ago

Axel-EH commented 2 years ago

Dear @JonathanWilbur , @hamano

Thanks for this library I'm starting to use in javascript and ASN1 DER.

I get the following exception while decoding a DER element from:

File: der.js, Function: fromBytes(bytes), Line 454:

[source,javascript] ....

        if (this.tagNumber <= 31) {
            throw new errors.ASN1Error("ASN.1 tag number could have been encoded in short form.", this);
        }

....

Before that, it was encoded using the following code snippet:

[source,javascript] .... /* ASN1 Description
application_tag_31 ::= [APPLICATION 31] IMPLICIT UTF8String OPTIONAL

Used Library: [Jonathan M. Wilbur](https://github.com/JonathanWilbur/asn1-ts)
const ASN1 = require('asn1-ts');

*/

module.exports.set_tag31 = function(tag=31) { var e = new ASN1.DERElement(); e.tagClass = ASN1.ASN1TagClass.application; e.construction = ASN1.ASN1Construction.primitive; e.tagNumber = tag; e.name = "application_tag_31"; e.utf8String = "Hi"; LOG_ASN1_ELEMENT(e); return e; } ....

Encoding with: der_encoded = der_element.toBytes() gives the following console output:

.... Name: application_tag_31-ASN1-DER Encoded, Number bytes: 5 Dezimal: 95,31,2,72,105 Hex: 0x5F,0x1F,0x02,0x48,0x69 ....

Decoding same buffer with der_element.fromBytes(der_encoded) gives the following exeption as console output and fails therefore:

.... Exception Thrown: Error: ASN.1 tag number could have been encoded in short form. ....

To me the identifier octet (class application,primitive,for tags with a number greater than or equal to 31 (bit5 to bit0 0b11111)):0x5F and Tag (last Octet = bit8 zero, tag number 31) 0x1F encoding looks correct, but the decode function should check if (this.tagNumber < 31) instead of if (this.tagNumber <= 31).

What do you think?

Best regards,

Axel

JonathanWilbur commented 2 years ago

Hey Axel, sorry I somehow didn't see this issue until now. You're absolutely right. That's a bug on my part. I will fix this now and publish a new version.

JonathanWilbur commented 2 years ago

Fixed with published version 7.0.7. Thanks again for reporting this!

Axel-EH commented 2 years ago

Many thanks! Axel