or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cli-usage.mdcore-parsing.mddom-visualization.mdformat-support.mdindex.mdstream-processing.md
tile.json

tessl/npm-lapo--asn1js

Generic ASN.1 parser/decoder that can decode any valid ASN.1 DER or BER structures.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@lapo/asn1js@2.1.x

To install, run

npx @tessl/cli install tessl/npm-lapo--asn1js@2.1.0

index.mddocs/

ASN.1 JavaScript Parser

@lapo/asn1js is a generic ASN.1 parser/decoder that can decode any valid ASN.1 DER (Distinguished Encoding Rules) or BER (Basic Encoding Rules) structures. It provides comprehensive tools for parsing, analyzing, and visualizing ASN.1 data in both Node.js and browser environments.

Package Information

  • Package Name: @lapo/asn1js
  • Package Type: npm
  • Language: JavaScript (ES6 modules)
  • Installation: npm install @lapo/asn1js

Core Imports

import { ASN1 } from '@lapo/asn1js';
import { Stream } from '@lapo/asn1js';
import { Hex } from '@lapo/asn1js/hex.js';
import { Base64 } from '@lapo/asn1js/base64.js';

For CommonJS (Node.js 22+ with --experimental-require-module):

const { ASN1 } = require('@lapo/asn1js');
const { Hex } = require('@lapo/asn1js/hex.js');

For older Node.js versions (dynamic import):

const { ASN1 } = await import('@lapo/asn1js');
const { Hex } = await import('@lapo/asn1js/hex.js');

Basic Usage

import { ASN1 } from '@lapo/asn1js';
import { Hex } from '@lapo/asn1js/hex.js';
import { Base64 } from '@lapo/asn1js/base64.js';

// Parse from hex string
const hexData = '06032B6570';
const binaryData = Hex.decode(hexData);
const asn1 = ASN1.decode(binaryData);
console.log(asn1.content()); // OID content

// Parse from Base64/PEM
const base64Data = 'MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA...';
const derData = Base64.unarmor(base64Data);
const parsed = ASN1.decode(derData);
console.log(parsed.toPrettyString());

Architecture

The library is built around several core components:

  • ASN.1 Parser: Core ASN1 class for parsing and representing ASN.1 structures
  • Stream Processing: Stream class for efficient byte-level processing with zero-copy approach
  • Encoding Support: Hex and Base64 classes for handling various input formats
  • Tag System: ASN1Tag class for proper ASN.1 tag representation and validation
  • DOM Integration: ASN1DOM class for browser-based visualization
  • Utility Classes: Int10 for big integer support, OID database, and RFC definitions

Capabilities

Core ASN.1 Parsing

Parse any valid ASN.1 DER or BER structure with complete type support and detailed content extraction.

/**
 * Main ASN.1 decoder function
 * @param stream - Stream or binary data to decode
 * @param offset - Starting position (optional)
 * @returns ASN1 instance representing the parsed structure
 */
function ASN1.decode(stream: Stream | ArrayLike<number> | string, offset?: number): ASN1;

class ASN1 {
  /** Get human-readable type name for this ASN.1 element */
  typeName(): string;
  /** Get human-readable content preview */
  content(maxLength?: number): string | null;
  /** Pretty formatted string representation with indentation */
  toPrettyString(indent?: string): string;
  /** Get starting position in stream */
  posStart(): number;
  /** Get content starting position */
  posContent(): number;
  /** Get ending position */
  posEnd(): number;
  /** Convert to hexadecimal string */
  toHexString(type?: 'raw' | 'byte' | 'dump'): string;
  /** Convert to Base64url string */
  toB64String(type?: 'url' | 'std'): string;
}

Core Parsing

Stream Processing

Efficient byte-level stream processing with zero-copy approach for optimal performance.

class Stream {
  constructor(enc: Stream | ArrayLike<number> | string, pos?: number);
  /** Get byte at current position (and increment) or specified position */
  get(pos?: number): number;
  /** Hexadecimal dump of stream region */
  hexDump(start: number, end: number, type?: 'raw' | 'byte' | 'dump'): string;
  /** Base64 dump of stream region */
  b64Dump(start: number, end: number, type?: 'url' | 'std'): string;
}

Stream Processing

Format Support

Comprehensive support for hexadecimal and Base64 input formats, including PEM armoring.

class Hex {
  /** Decode hexadecimal string/array to binary data */
  static decode(a: string | ArrayLike<number>): Uint8Array | number[];
}

class Base64 {
  /** Decode base64 string/array to binary data */
  static decode(a: string | ArrayLike<number>): Uint8Array | number[];
  /** Format base64 string with proper padding and line breaks */
  static pretty(str: string): string;
  /** Extract and decode base64 from PEM or other armored formats */
  static unarmor(a: string): Uint8Array | number[];
}

Format Support

DOM Visualization

Browser-specific functionality for creating interactive ASN.1 structure visualizations.

class ASN1DOM extends ASN1 {
  /** Generate DOM element representation of ASN.1 structure */
  toDOM(spaces?: string): HTMLElement;
  /** Generate hexadecimal DOM display */
  toHexDOM(start?: number, trimmedHex?: boolean): HTMLElement;
}

DOM Visualization

Command Line Interface

Command-line tool for dumping and analyzing ASN.1 structures from files or data URIs.

# Dump ASN.1 structure from file
npx @lapo/asn1js certificate.der

# Dump from data URI
npx @lapo/asn1js data:base64,MIICIjANBgkqhkiG9w0BAQEFAAOCAg8A...

CLI Usage

Types

// ASN1Tag interface (internal class, not exported - accessible via asn1.tag property)
interface ASN1Tag {
  tagClass: number;
  tagConstructed: boolean;
  tagNumber: number;
  isUniversal(): boolean;
  isEOC(): boolean;
}

class Int10 {
  constructor(value?: number);
  mulAdd(m: number, c: number): void;
  sub(c: number): void;
  toString(base?: number): string;
  valueOf(): number;
  simplify(): number | Int10;
}

// OID database - maps OID strings to descriptive information
interface OIDInfo {
  d?: string; // description
  c?: string; // comment  
  w?: string; // warning
}

const oids: Record<string, OIDInfo>;