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

Broken toString and toJSON function on 7.0.6 #22

Closed dicksonhk closed 1 year ago

dicksonhk commented 1 year ago

Calling toString and toJSON on a SET_OF ASN1Element that is not uniquely tagged throws ASN1ConstructionError, after the introduction of uniqueness checking for SET introduced in v7.0.6.

Calling setOf, instead of set, could be one way of solving the issue, as both SET and SET_OF have the same signature.

source/codecs/ber.ts, source/codecs/cer.ts, source/codecs/der.ts:

get set (): SET<ASN1Element> {
    const ret = this.sequence;
    if (!isUniquelyTagged(ret)) {
        throw new errors.ASN1ConstructionError("Duplicate tag in SET.", this);
    }
    return ret;
}

source/asn1.ts

// toString()
switch (this.tagNumber) {
    case (ASN1UniversalType.set): return ("{ " + this.set // <-- this could be a SET_OF, with might not be uniquely tagged
        .map((el) => (el.name.length ? `${el.name} ${el.toString()}` : el.toString()))
        .join(" , ") + " }");
}

source/asn1.ts

// toJSON()
switch (this.tagNumber) {
    case (ASN1UniversalType.set): { // <-- this could be a SET_OF, with might not be uniquely tagged
        if (!recurse) {
            return null;
        }
        return this.set.map((el) => el.toJSON());
    }
}
JonathanWilbur commented 1 year ago

I fixed this just now. I released version 7.0.18, which fixes both this bug and #20 . Thank you so much for your very thoughtful issue reporting and PR!