Library asn1

ASN.1 functions.

Large chunks of this code have been ripped right out from snmp.lua.

Author:

  • Patrik Karlsson

Copyright © Same as Nmap--See https://nmap.org/book/man-legal.html

Source: https://svn.nmap.org/nmap/nselib/asn1 .lua

Functions

asn1.decoder (self, encStr, elen, pos)

Template for an ASN1 decoder function.

asn1.encoder (self, val)

Template for an ASN1 encoder function.

ASN1Decoder.decode (self, encStr, pos)

Decodes the ASN.1's built-in simple types

ASN1Decoder.decodeInt (encStr, len, pos)

Decodes an Integer according to ASN.1 basic encoding rules.

ASN1Decoder.decodeLength (encStr, pos)

Decodes length part of encoded value according to ASN.1 basic encoding rules.

ASN1Decoder.decodeOID (self, encStr, len, pos)

Decodes an OID from a sequence of bytes.

ASN1Decoder.decodeSeq (self, encStr, len, pos)

Decodes a sequence according to ASN.1 basic encoding rules.

ASN1Decoder.registerBaseDecoders (self)

Registers the base simple type decoders

ASN1Decoder.registerTagDecoders (self, tagDecoders)

Allows for registration of additional tag decoders

ASN1Decoder.registerTagEncoders (self, tagEncoders)

Allows for registration of additional tag encoders

ASN1Decoder.setStopOnError (self, val)

Tells the decoder to stop if it detects an error while decoding.

ASN1Encoder.encode (self, val)

Encodes a given value according to ASN.1 basic encoding rules for SNMP packet creation.

ASN1Encoder.encodeInt (val)

Encodes an Integer according to ASN.1 basic encoding rules.

ASN1Encoder.encodeLength (len)

Encodes the length part of a ASN.1 encoding triplet using the "primitive, definite-length" method.

ASN1Encoder.encodeSeq (self, seqData)

Encodes an ASN1 sequence

ASN1Encoder.registerBaseEncoders (self)

Registers the base ASN.1 Simple types encoders

BERtoInt (class, constructed, number)

Converts a BER encoded type to a numeric value

intToBER (i)

Converts an integer to a BER encoded type table

Tables

tagDecoders

Table for registering additional tag decoders.

tagEncoders

Table for registering additional tag encoders.

Functions

asn1.decoder (self, encStr, elen, pos)

Template for an ASN1 decoder function.

Parameters

self
The ASN1Decoder object
encStr
Encoded string
elen
Length of the object in bytes
pos
Current position in the string

Return values:

  1. The decoded object
  2. The position after decoding
asn1.encoder (self, val)

Template for an ASN1 encoder function.

Parameters

self
The ASN1Encoder object
val
The value to encode

Return value:

The encoded object
ASN1Decoder.decode (self, encStr, pos)

Decodes the ASN.1's built-in simple types

Parameters

self
 
encStr
Encoded string.
pos
Current position in the string.

Return values:

  1. The decoded value(s).
  2. The position after decoding
ASN1Decoder.decodeInt (encStr, len, pos)

Decodes an Integer according to ASN.1 basic encoding rules.

Parameters

encStr
Encoded string.
len
Length of integer in bytes.
pos
Current position in the string.

Return values:

  1. The decoded integer.
  2. The position after decoding.
ASN1Decoder.decodeLength (encStr, pos)

Decodes length part of encoded value according to ASN.1 basic encoding rules.

Parameters

encStr
Encoded string.
pos
Current position in the string.

Return values:

  1. The length of the following value.
  2. The position after decoding.
ASN1Decoder.decodeOID (self, encStr, len, pos)

Decodes an OID from a sequence of bytes.

Parameters

self
 
encStr
Encoded string.
len
Length of sequence in bytes.
pos
Current position in the string.

Return values:

  1. The OID as an array.
  2. The position after decoding.
ASN1Decoder.decodeSeq (self, encStr, len, pos)

Decodes a sequence according to ASN.1 basic encoding rules.

Parameters

self
 
encStr
Encoded string.
len
Length of sequence in bytes.
pos
Current position in the string.

Return values:

  1. The decoded sequence as a table.
  2. The position after decoding.
ASN1Decoder.registerBaseDecoders (self)

Registers the base simple type decoders

Parameters

self
 
ASN1Decoder.registerTagDecoders (self, tagDecoders)

Allows for registration of additional tag decoders

Parameters

self
 
tagDecoders
table containing decoding functions

See also:

ASN1Decoder.registerTagEncoders (self, tagEncoders)

Allows for registration of additional tag encoders

Parameters

self
 
tagEncoders
table containing encoding functions

See also:

ASN1Decoder.setStopOnError (self, val)

Tells the decoder to stop if it detects an error while decoding.

This should probably be the default, but some scripts depend on being able to decode stuff while lacking proper ASN1 decoding functions.

Parameters

self
 
val
boolean, true if decoding should stop on error, otherwise false (default)
ASN1Encoder.encode (self, val)

Encodes a given value according to ASN.1 basic encoding rules for SNMP packet creation.

Parameters

self
 
val
Value to be encoded.

Return value:

Encoded value.
ASN1Encoder.encodeInt (val)

Encodes an Integer according to ASN.1 basic encoding rules.

Parameters

val
Value to be encoded.

Return value:

Encoded integer.
ASN1Encoder.encodeLength (len)

Encodes the length part of a ASN.1 encoding triplet using the "primitive, definite-length" method.

Parameters

len
Length to be encoded.

Return value:

Encoded length value.
ASN1Encoder.encodeSeq (self, seqData)

Encodes an ASN1 sequence

Parameters

self
 
seqData
A string of sequence data

Return value:

ASN.1 BER-encoded sequence
ASN1Encoder.registerBaseEncoders (self)

Registers the base ASN.1 Simple types encoders

  • boolean
  • integer (Lua number)
  • string
  • null (Lua nil)

Parameters

self
 
BERtoInt (class, constructed, number)

Converts a BER encoded type to a numeric value

This allows it to be used in the encoding function

Parameters

class
number - see <code>BERCLASS<code>
constructed
boolean (true if constructed, false if primitive)
number
numeric

Return value:

number to be used with encode
intToBER (i)

Converts an integer to a BER encoded type table

Parameters

i
number containing the value to decode

Return value:

table with the following entries:
  • class
  • constructed
  • primitive
  • number

Tables

tagDecoders

Table for registering additional tag decoders.

Each index is a tag number as a hex string. Values are ASN1 decoder functions.

tagEncoders

Table for registering additional tag encoders.

Each index is a lua type as a string. Values are ASN1 encoder functions.