asn1.js: ASN.1 (Abstract Syntax Notation One) JavaScript Library

repository·master·Indexed 43 days ago

https://github.com/peculiarventures/asn1.js

A pure JavaScript library for encoding and decoding ASN.1 data, specifically implementing BER (Basic Encoding Rules). Version 3.0.10 provides utilities for building ASN.1 structures, validating them against schemas using verifySchema(), and safely parsing untrusted input with resource limits (maxDepth, maxNodes, maxContentLength) via fromBER(). It serves as the foundation for the PKIjs library and is designed for use in browsers.

Tokens
8.7K
Snippets
38
Records
44
Agent score
55%

What's inside asn1.js

  1. Overview of ASN.1.js

    master
    ASN.1.js is a JavaScript module used for encoding and decoding data according to the Abstract Syntax Notation One (ASN.1) standard. It is designed to handle complex data structures and facilitate interoperability with protocols such as LDAP, PKCS, and X.509. The library is suitable for projects involving binary data representation, secure communication protocols, and cryptographic structures.
  2. Understand the CompareSchemaResult return type

    master

    The compareSchema and verifySchema functions return a discriminated union type CompareSchemaResult to handle validation outcomes.

    Success State

    When verified is true, the result property contains the root object, which may have been augmented with properties from the input data based on the schema's name fields.

    interface CompareSchemaSuccess {
      verified: true;
      result: AsnType & Record<string, any>;
    }

    Failure State

    When verified is false, the result property contains either the root object (often with an error string attached) or a standalone error object.

    interface CompareSchemaFail {
      verified: false;
      name?: string;
      result: AsnType | { error: string };
    }
  3. Supported AsnSchemaType components

    master

    An AsnSchemaType is a union of several types used to define the structure of ASN.1 data for validation:

    • AsnType: A standard ASN.1 type.
    • Any: A schema element that accepts any valid ASN.1 data. If the Any schema element has a name property, the validated data will be attached to the root object under that name.
    • Choice: A schema element representing a choice between multiple possible types. The validator will iterate through the value array of the Choice and succeed if the input matches any one of the elements.
    • Repeated: A schema element used within constructed types to indicate that a component can appear multiple times.
  4. Use internal schemas for primitively encoded data types

    master

    When working with basic ASN.1 data types (primitives) that do not require a custom complex structure, you can use the library's built-in 'internal schemas'. This allows you to encode or decode standard types without manually defining a full ASN.1 structure. This is useful for handling raw primitive data types directly.

    // Note: The actual implementation details are contained in the linked example file.
    // This pattern involves using the library's internal schema definitions to handle
    // primitive ASN.1 types directly.
  5. Create new ASN.1 structures

    master

    You can create new ASN.1 structures by defining a schema and then using the library to encode data according to that schema. This process involves defining the structure's components (like integers, sequences, or strings) and then calling the appropriate encoding methods to transform a JavaScript object into an ASN.1 encoded buffer.

    // Note: The actual implementation details are contained in the linked example file.
    // Typically involves defining a schema and using encoding methods.
  6. Create ASN.1 structures using constructor parameters

    master

    A more concise way to build structures is to pass a configuration object directly to the constructor. This object can include a value array containing other ASN.1 types, allowing for nested structures.

    var sequence2 = new asn1js.Sequence({
      value: [
        new asn1js.Integer({ value: 1 }),
        new asn1js.Integer({
          isHexOnly: true,
          valueHex: integer_data
        })
      ]
    });
  7. Validate ASN.1 against a pre-defined schema

    master

    To ensure a decoded ASN.1 structure matches a specific format, define a schema using ASN.1 types and use asn1js.verifySchema().

    In the schema, you can:

    • Assign a name to blocks for easy access in the result.
    • Mark blocks as optional: true so validation passes even if they are missing from the encoded data.

    asn1js.verifySchema(encoded_data, schema) returns an object containing verified (boolean) and result (the decoded data with block names applied).

    var asn1_schema = new asn1js.Sequence({
      name: "block1",
      value: [
        new asn1js.Null({
          name: "block2"
        }),
        new asn1js.Integer({
          name: "block3",
          optional: true
        })
      ]
    });
    
    var variant1 = asn1js.verifySchema(encoded_sequence, asn1_schema);
    var variant1_verified = variant1.verified;
    var variant1_result = variant1.result; // Verified decoded data with all block names inside
  8. Create new ASN.1 structures by calling constructors with parameters

    master

    You can create new ASN.1 structures by directly invoking constructors with specific parameters. This approach allows for programmatic construction of complex ASN.1 data types by defining their components and constraints at the time of instantiation.

    // Note: The actual implementation details are contained in the associated .example file.
    // This pattern involves calling ASN.1 constructors with specific parameters to define structure.