Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ASN1

Implements parsing of DER-encoded ASN.1 data structures, as defined in ITU-T Rec X.690.

See also A Layman's Guide to a Subset of ASN.1, BER, and DER,'' http://luca.ntop.org/Teaching/Appunti/asn1.html.

ASN.1 is a syntax for specifying abstract objects and BER, DER, PER, XER etc are different encoding formats for those objects. Here, we'll be dealing with DER, the Distinguished Encoding Rules. DER is used in X.509 because it's fast to parse and, unlike BER, has a unique encoding for every object. When calculating hashes over objects, it's important that the resulting bytes be the same at both ends and DER removes this margin of error. ASN.1 is very complex and this package doesn't attempt to implement everything by any means.

DER Encoding of ASN.1 Types: https://msdn.microsoft.com/en-us/library/windows/desktop/bb540792(v=vs.85).aspx

Hierarchy

  • ASN1

Index

Constructors

constructor

  • new ASN1(tagClass: Class, tag: Tag, data: Buffer, isCompound?: boolean): ASN1
  • Parameters

    • tagClass: Class
    • tag: Tag
    • data: Buffer
    • Default value isCompound: boolean = false

    Returns ASN1

Properties

Private _der

_der: Buffer | null

Private _value

_value: any

bytes

bytes: Buffer

class

class: Class

isCompound

isCompound: boolean

tag

tag: Tag

Accessors

DER

  • get DER(): Buffer
  • the DER format Buffer of this ASN.1 object.

    Returns Buffer

value

  • get value(): any
  • the well parsed value of this ASN.1 object. It will be boolean, number, string, BitString, Date, array of ASN.1 objects and so on.

    Returns any

Methods

Protected __computed

  • __computed(_depth: any, options: any): string
  • Parameters

    • _depth: any
    • options: any

    Returns string

equals

  • equals(obj: ASN1): boolean
  • Returns true if two ASN.1 objects equally.

    Parameters

    • obj: ASN1

      another ASN.1 object.

    Returns boolean

mustCompound

  • mustCompound(msg?: string): ASN1[]
  • Expecting it is compound ASN.1 object and returns an array of sub ASN.1 objects.

    Parameters

    • Default value msg: string = "asn1 object value is not compound"

      error message to throw when it is not compound ASN.1 object.

    Returns ASN1[]

toDER

  • toDER(): Buffer
  • Converts this ASN.1 object to a buffer of bytes in DER format.

    Returns Buffer

toJSON

  • toJSON(): any
  • Return a friendly JSON object for debuging.

    Returns any

validate

  • Validates that the given ASN.1 object is at least a super set of the given ASN.1 structure. Only tag classes and types are checked. An optional map may also be provided to capture ASN.1 values while the structure is checked.

    To capture an ASN.1 object, set an object in the validator's 'capture' parameter to the key to use in the capture map.

    Objects in the validator may set a field 'optional' to true to indicate that it isn't necessary to pass validation.

    Parameters

    • tpl: Template

      Template object to validate.

    • Default value captures: Captures = {}

      Captures object to capture ASN.1 object.

    Returns Error | null

valueOf

  • valueOf(): any
  • Parse the value of this ASN.1 object when it is Class.UNIVERSAL. The value will be boolean, number, string, BitString, Date, array of ASN.1 objects and so on.

    Returns any

Static BitString

  • Creates a Tag.BITSTRING ASN.1 object.

    Parameters

    • val: BitString | Buffer

      BitString object or buffer.

    Returns ASN1

Static Bool

  • Bool(val: boolean): ASN1
  • Creates a Tag.BOOLEAN ASN.1 object.

    Parameters

    • val: boolean

      boolean value.

    Returns ASN1

Static GeneralString

  • GeneralString(val: string): ASN1
  • Creates an Tag.GENERALSTRING (specified in ISO-2022/ECMA-35) ASN.1 object.

    Parameters

    • val: string

      general string.

    Returns ASN1

Static GeneralizedTime

  • GeneralizedTime(date: Date): ASN1
  • Creates an Tag.GENERALIZEDTIME ASN.1 object.

    Parameters

    • date: Date

      date value.

    Returns ASN1

Static IA5String

  • IA5String(val: string): ASN1
  • Creates an Tag.IA5STRING (ASCII string) ASN.1 object.

    Parameters

    • val: string

      ASCII string.

    Returns ASN1

Static Integer

  • Integer(val: number | Buffer): ASN1
  • Creates a Tag.INTEGER ASN.1 object.

    Parameters

    • val: number | Buffer

      integer value or buffer.

    Returns ASN1

Static Null

Static NumericString

  • NumericString(val: string): ASN1
  • Creates an Tag.NUMERICSTRING ASN.1 object.

    Parameters

    • val: string

      numeric string.

    Returns ASN1

Static OID

  • OID(val: string): ASN1
  • Creates an Tag.OID (dot-separated numeric string) ASN.1 object.

    Parameters

    • val: string

      dot-separated numeric string.

    Returns ASN1

Static PrintableString

  • PrintableString(val: string): ASN1
  • Creates an Tag.NUMERICSTRING ASN.1 object.

    Parameters

    • val: string

      printable string.

    Returns ASN1

Static Seq

  • Creates an Tag.SEQUENCE ASN.1 object.

    Parameters

    • objs: ASN1[]

      an array of ASN.1 objects.

    Returns ASN1

Static Set

  • Creates an Tag.SET ASN.1 object.

    Parameters

    • objs: ASN1[]

      an array of ASN.1 objects.

    Returns ASN1

Static Spec

  • Creates an Class.CONTEXT_SPECIFIC ASN.1 object.

    Note: the tag means nothing with Class.CONTEXT_SPECIFIC

    Parameters

    • tag: Tag

      number.

    • objs: ASN1 | ASN1[]

      an array of ASN.1 objects or a ASN.1 object.

    • Default value isCompound: boolean = true

      when objs is a array, the isCompound will be set to true.

    Returns ASN1

Static T61String

  • T61String(val: string): ASN1
  • Creates an Tag.T61STRING (8-bit clean string) ASN.1 object.

    Parameters

    • val: string

      8-bit clean string.

    Returns ASN1

Static UTCTime

  • UTCTime(date: Date): ASN1
  • Creates an Tag.UTCTIME ASN.1 object.

    Note: GeneralizedTime has 4 digits for the year and is used for X.509. dates past 2049. Converting to a GeneralizedTime hasn't been implemented yet.

    Parameters

    • date: Date

      date value.

    Returns ASN1

Static UTF8

  • UTF8(val: string): ASN1
  • Creates an Tag.UTF8 ASN.1 object.

    Parameters

    • val: string

      utf8 string.

    Returns ASN1

Static Private _fromDER

Static Private _parseCompound

  • _parseCompound(buf: Buffer, deepParse: boolean): ASN1[]
  • Parameters

    • buf: Buffer
    • deepParse: boolean

    Returns ASN1[]

Static fromDER

  • fromDER(buf: Buffer, deepParse?: boolean): ASN1
  • Parse a ASN.1 object from a buffer in DER format.

    Parameters

    • buf: Buffer

      the buffer to parse.

    • Default value deepParse: boolean = false

      deeply parse or not.

    Returns ASN1

Static parseBitString

  • Parse a Tag.BITSTRING value from ASN.1 object' value.

    Parameters

    • buf: Buffer

      the buffer to parse.

    Returns BitString

Static parseBool

  • parseBool(buf: Buffer): boolean
  • Parse a Tag.BOOLEAN value from ASN.1 object' value.

    Parameters

    • buf: Buffer

      the buffer to parse.

    Returns boolean

Static parseDER

  • Parse a ASN.1 object from a buffer in DER format with given class and tag. If class or tag is not match, it will throw a error.

    Parameters

    • buf: Buffer

      the buffer to parse.

    • tagClass: Class

      expect class to parse.

    • tag: Tag

      expect type to parse.

    Returns ASN1

Static parseDERWithTemplate

  • Parse a ASN.1 object from a buffer in DER format with given Template object. If template is not match, it will throw a error.

    Parameters

    • buf: Buffer

      the buffer to parse.

    • tpl: Template

      expect template to parse.

    Returns Captures

    a Captures object with captured ASN.1 objects

Static parseGeneralString

  • parseGeneralString(buf: Buffer): string
  • Parse a Tag.GENERALSTRING string from ASN.1 object' value.

    Parameters

    • buf: Buffer

      the buffer to parse.

    Returns string

Static parseGeneralizedTime

  • parseGeneralizedTime(buf: Buffer): Date
  • Parse a Tag.GENERALIZEDTIME date from ASN.1 object' value.

    Parameters

    • buf: Buffer

      the buffer to parse.

    Returns Date

Static parseIA5String

  • parseIA5String(buf: Buffer): string
  • Parse a Tag.IA5STRING string from ASN.1 object' value.

    Parameters

    • buf: Buffer

      the buffer to parse.

    Returns string

Static parseInteger

  • parseInteger(buf: Buffer): number | string
  • Parse a Tag.INTEGER value from ASN.1 object' value.

    Parameters

    • buf: Buffer

      the buffer to parse.

    Returns number | string

Static parseIntegerNum

  • parseIntegerNum(buf: Buffer): number
  • Parse a Tag.INTEGER value as a number from ASN.1 object' value.

    Parameters

    • buf: Buffer

      the buffer to parse.

    Returns number

Static parseIntegerStr

  • parseIntegerStr(buf: Buffer): string
  • Parse a Tag.INTEGER value as a hex string(for BigInt) from ASN.1 object' value.

    Parameters

    • buf: Buffer

      the buffer to parse.

    Returns string

Static parseNull

  • parseNull(buf: Buffer): null
  • Parse a Tag.NULL value from ASN.1 object' value.

    Parameters

    • buf: Buffer

      the buffer to parse.

    Returns null

Static parseNumericString

  • parseNumericString(buf: Buffer): string
  • Parse a Tag.UTF8 string from ASN.1 object' value.

    Parameters

    • buf: Buffer

      the buffer to parse.

    Returns string

Static parseOID

  • parseOID(buf: Buffer): string
  • Parse a Tag.OID value from ASN.1 object' value.

    Parameters

    • buf: Buffer

      the buffer to parse.

    Returns string

Static parsePrintableString

  • parsePrintableString(buf: Buffer): string
  • Parse a Tag.PRINTABLESTRING string from ASN.1 object' value.

    Parameters

    • buf: Buffer

      the buffer to parse.

    Returns string

Static parseT61String

  • parseT61String(buf: Buffer): string
  • Parse a Tag.T61STRING string from ASN.1 object' value.

    Parameters

    • buf: Buffer

      the buffer to parse.

    Returns string

Static parseTime

  • parseTime(tag: Tag, buf: Buffer): Date
  • Parse a Tag.UTCTIME date of Tag.GENERALIZEDTIME date from ASN.1 object' value.

    Parameters

    • tag: Tag

      the type.

    • buf: Buffer

      the buffer to parse.

    Returns Date

Static parseUTCTime

  • parseUTCTime(buf: Buffer): Date
  • Parse a Tag.UTCTIME date from ASN.1 object' value.

    Parameters

    • buf: Buffer

      the buffer to parse.

    Returns Date

Static parseUTF8

  • parseUTF8(buf: Buffer): string
  • Parse a Tag.UTF8 string from ASN.1 object' value.

    Parameters

    • buf: Buffer

      the buffer to parse.

    Returns string

Generated using TypeDoc