asn1.js: ASN.1 (Abstract Syntax Notation One) JavaScript Library
repository·master·Indexed 43 days ago
https://github.com/peculiarventures/asn1.jsA 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.
What's inside asn1.js
- 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.
Install asn1js via npm
masterTo add
asn1jsto your project, run the following command using npm:npm install asn1jsBuild the website for production
masterTo generate static content for deployment, run
npm run build. The resulting static files will be located in thebuilddirectory and can be hosted on any static content hosting service.$ npm run buildInstall the website dependencies
masterTo install the necessary dependencies for the website project, run
npm installin the root directory of the website package.$ npm installRun the website in local development mode
masterTo start a local development server, use
npm run dev. This command will open a browser window and support live reloading, meaning most changes will be reflected immediately without restarting the server.$ npm run devUnderstand the CompareSchemaResult return type
masterThe
compareSchemaandverifySchemafunctions return a discriminated union typeCompareSchemaResultto handle validation outcomes.Success State
When
verifiedistrue, theresultproperty contains therootobject, which may have been augmented with properties from the input data based on the schema'snamefields.interface CompareSchemaSuccess { verified: true; result: AsnType & Record<string, any>; }Failure State
When
verifiedisfalse, theresultproperty contains either therootobject (often with anerrorstring attached) or a standalone error object.interface CompareSchemaFail { verified: false; name?: string; result: AsnType | { error: string }; }Supported AsnSchemaType components
masterAn
AsnSchemaTypeis 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 theAnyschema element has anameproperty, the validated data will be attached to therootobject under that name.Choice: A schema element representing a choice between multiple possible types. The validator will iterate through thevaluearray of theChoiceand 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.
Use internal schemas for primitively encoded data types
masterWhen 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.Create new ASN.1 structures
masterYou 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.Create ASN.1 structures using constructor parameters
masterA more concise way to build structures is to pass a configuration object directly to the constructor. This object can include a
valuearray 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 }) ] });Validate ASN.1 against a pre-defined schema
masterTo 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
nameto blocks for easy access in the result. - Mark blocks as
optional: trueso validation passes even if they are missing from the encoded data.
asn1js.verifySchema(encoded_data, schema)returns an object containingverified(boolean) andresult(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- Assign a
Create new ASN.1 structures by calling constructors with parameters
masterYou 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.