module Net::BER

Constants

ENCODING_TYPE

BER encoding type is kept in bit 6 of the tag type octet.

<table> <tr><th>Bitmask</th><th>Definition</th></tr> <tr><th>0b__0_____</th><td>Primitive</td></tr> <tr><th>0b__1_____</th><td>Constructed</td></tr> </table>

MAX_FIXNUM_SIZE

Used for BER-encoding the length and content bytes of a Fixnum integer values.

Null

The default BerIdentifiedNull object.

TAG_CLASS

BER tag classes are kept in bits seven and eight of the tag type octet.

<table> <tr><th>Bitmask</th><th>Definition</th></tr> <tr><th>0b00______</th><td>Universal (ASN.1 Native) Types</td></tr> <tr><th>0b01______</th><td>Application Types</td></tr> <tr><th>0b10______</th><td>Context-Specific Types</td></tr> <tr><th>0b11______</th><td>Private Types</td></tr> </table>

VERSION

Public Class Methods

compile_syntax(syntax) click to toggle source

Accepts a hash of hashes describing a BER syntax and converts it into a byte-keyed object for fast BER conversion lookup. The resulting “compiled” syntax is used by Net::BER::BERParser.

This method should be called only by client classes of Net::BER (e.g., Net::LDAP and Net::SNMP) and not by clients of those classes.

The hash-based syntax uses TAG_CLASS keys that contain hashes of ENCODING_TYPE keys that contain tag numbers with object type markers.

:<TAG_CLASS> => {
  :<ENCODING_TYPE> => {
    <number> => <object-type>
  },
},

Permitted Object Types

:string

A string value, represented as BerIdentifiedString.

:integer

An integer value, represented with Fixnum.

:oid

An Object Identifier value; see X.690 section 8.19. Currently represented with a standard array, but may be better represented as a BerIdentifiedOID object.

:array

A sequence, represented as BerIdentifiedArray.

:boolean

A boolean value, represented as true or false.

:null

A null value, represented as BerIdentifiedNull.

Example

Net::LDAP defines its ASN.1 BER syntax something like this:

class Net::LDAP
  AsnSyntax = Net::BER.compile_syntax({
    :application => {
      :primitive => {
        2 => :null,
      },
      :constructed => {
        0 => :array,
        # ...
      },
    },
    :context_specific => {
      :primitive => {
        0 => :string,
        # ...
      },
      :constructed => {
        0 => :array,
        # ...
      },
    }
    })
end
NOTE

For readability and formatting purposes, Net::LDAP and its siblings actually construct their syntaxes more deliberately, as shown below. Since a hash is passed in the end in any case, the format does not matter.

primitive = { 2 => :null }
constructed = {
  0 => :array,
  # ...
}
application = {
  :primitive => primitive,
  :constructed => constructed
}

primitive = {
  0 => :string,
  # ...
}
constructed = {
  0 => :array,
  # ...
}
context_specific = {
  :primitive => primitive,
  :constructed => constructed
}
AsnSyntax = Net::BER.compile_syntax(:application => application,
                                    :context_specific => context_specific)
# File lib/net/ber.rb, line 233
def self.compile_syntax(syntax)
  # TODO 20100327 AZ: Should we be allocating an array of 256 values
  # that will either be +nil+ or an object type symbol, or should we
  # allocate an empty Hash since unknown values return +nil+ anyway?
  out = [nil] * 256
  syntax.each do |tag_class_id, encodings|
    tag_class = TAG_CLASS[tag_class_id]
    encodings.each do |encoding_id, classes|
      encoding = ENCODING_TYPE[encoding_id]
      object_class = tag_class + encoding
      classes.each do |number, object_type|
        out[object_class + number] = object_type
      end
    end
  end
  out
end