ASN.1 Parser Utility

5.1.9 · maintenance · verified Sun Apr 19

parse-asn1 is a utility library designed for parsing Abstract Syntax Notation One (ASN.1) encoded data. It is primarily used within the `crypto-browserify` ecosystem, serving as a dependency for libraries like `browserify-sign` to handle cryptographic artifacts such as X.509 certificates and private keys. The current stable version is 5.1.9, with the last update approximately 7 months ago, indicating a maintenance-focused release cadence. The library provides a JavaScript implementation for interpreting BER/DER encoded binary data into a structured JavaScript object. A key differentiator is its role in providing browser compatibility for Node.js's crypto functionalities, though ASN.1 parsing itself is inherently complex and a common source of security vulnerabilities if not handled robustly.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `parse-asn1` to decode a simple DER-encoded ASN.1 sequence and an Object Identifier (OID) from a Buffer and print the resulting JavaScript object structure.

import parseASN1 from 'parse-asn1';
import { Buffer } from 'buffer';

// Example: A simple DER-encoded ASN.1 sequence
// This represents a SEQUENCE containing an INTEGER (value 1) and a BOOLEAN (value true)
// 30 07         -- Sequence, length 7
//   02 01 01    -- Integer, length 1, value 1
//   01 01 ff    -- Boolean, length 1, value true
const asn1DerBuffer = Buffer.from([
  0x30, 0x07,
  0x02, 0x01, 0x01,
  0x01, 0x01, 0xFF
]);

try {
  const parsed = parseASN1(asn1DerBuffer);
  console.log('Successfully parsed ASN.1 data:');
  console.log(JSON.stringify(parsed, null, 2));

  // Accessing elements (example output structure might vary slightly)
  if (parsed && Array.isArray(parsed.value)) {
    const integerNode = parsed.value[0];
    const booleanNode = parsed.value[1];
    console.log(`First element (Integer): Type ${integerNode.type}, Value ${integerNode.value.toString('hex')}`);
    console.log(`Second element (Boolean): Type ${booleanNode.type}, Value ${booleanNode.value.toString('hex')}`);
  }

} catch (error) {
  console.error('Error parsing ASN.1 data:', error.message);
}

// Another example: a simple OID
// 06 03 2A 86 48 (1.2.840)
const oidBuffer = Buffer.from([0x06, 0x03, 0x2A, 0x86, 0x48]);
try {
  const parsedOid = parseASN1(oidBuffer);
  console.log('\nSuccessfully parsed OID:');
  console.log(JSON.stringify(parsedOid, null, 2));
} catch (error) {
  console.error('Error parsing OID:', error.message);
}

view raw JSON →