PeculiarVentures / ASN1.js

ASN1js is a pure JavaScript library implementing a full ASN.1 BER decoder and encoder.
https://asn1js.org
Other
267 stars 57 forks source link

Decoding ObjectIdentifiers with IMPLICT tags #78

Closed Caracasa closed 2 years ago

Caracasa commented 2 years ago

With an ASN1 definition using IMPLICIT tagging I need a push in the right direction how to decode an ObjectIdentifier.

Definition DEFINITIONS AUTOMATIC TAGS ::=
BEGIN

Seq ::= SEQUENCE {
   oid [1] OBJECT IDENTIFIER,
   ...
}

END

How would I construct the sequence object for decoding (of the oid) in that case? I read the example in #45 but do not understand how to adapt it to my usecase.

To be honest at the moment I manually iterate over the sequence and match ids to use primitive objects with the valueBlocks. For asn1js.ObjectIdentifier I failed to instantiate the object from the raw arraybuffer.

microshine commented 2 years ago

Have you seen our another package @peculiar/asn1-schema? It's based on ASN1.js and allows using TypeScript decorators for schema declaration.

https://github.com/PeculiarVentures/asn1-schema/blob/988241d5c3c8fa4ef6f581b55b7354330ead0156/packages/ocsp/src/cert_status.ts#L20

And schema declaration could see like this

/**
 * ```
 * Definition DEFINITIONS AUTOMATIC TAGS ::=
 * BEGIN
 * 
 * Seq ::= SEQUENCE {
 *    oid [1] OBJECT IDENTIFIER,
 *    ...
 * }
 * 
 * END
 * ```
 */
@AsnType()
export class MysSeq {

  @AsnProp({ type: AsnPropTypes.ObjecIdentifier, context: 1, implicit: true })
  public oid: string;

}
Caracasa commented 2 years ago

I have now. A very sleek and easy to use approach to the topic "asn1-schema" after setting up the support for the experimental decorators. We will make good use of it.

@AsnType({type: AsnTypeTypes.Sequence})
export class TrapSequence {

@AsnProp({ type: AsnPropTypes.ObjecIdentifier, context: 1, implicit: true })
  public oid: string;  
}

[...]
}
microshine commented 2 years ago

@Caracasa If you are declaring an RFC ASN.1 schema, you could make a PR of your schema to the https://github.com/PeculiarVentures/asn1-schema mono repo and allow NodeJS community to use it.

Caracasa commented 2 years ago

It is an application-specific schema for storing structured sensor/log data.