Module: RASN2

Defined in:
lib/rasn2.rb,
lib/rasn2/model.rb,
lib/rasn2/types.rb,
lib/rasn2/errors.rb,
lib/rasn2/tracer.rb,
lib/rasn2/version.rb,
lib/rasn2/wrapper.rb,
lib/rasn2/types/any.rb,
lib/rasn2/types/set.rb,
lib/rasn2/types/tag.rb,
lib/rasn2/types/base.rb,
lib/rasn2/types/null.rb,
lib/rasn2/types/choice.rb,
lib/rasn2/types/set_of.rb,
lib/rasn2/schema_parser.rb,
lib/rasn2/types/boolean.rb,
lib/rasn2/types/integer.rb,
lib/rasn2/types/sequence.rb,
lib/rasn2/types/utc_time.rb,
lib/rasn2/value_notation.rb,
lib/rasn2/types/object_id.rb,
lib/rasn2/types/primitive.rb,
lib/rasn2/helpers/colorize.rb,
lib/rasn2/types/bit_string.rb,
lib/rasn2/types/bmp_string.rb,
lib/rasn2/types/enumerated.rb,
lib/rasn2/types/ia5_string.rb,
lib/rasn2/types/constrained.rb,
lib/rasn2/types/constructed.rb,
lib/rasn2/types/sequence_of.rb,
lib/rasn2/types/utf8_string.rb,
lib/rasn2/types/octet_string.rb,
lib/rasn2/types/numeric_string.rb,
lib/rasn2/types/visible_string.rb,
lib/rasn2/types/generalized_time.rb,
lib/rasn2/types/printable_string.rb,
lib/rasn2/types/universal_string.rb

Overview

Rasn1 is a pure ruby library to parse, decode and encode ASN.1 data.

Author:

  • Sylvain Daubert

Defined Under Namespace

Modules: Helpers, SchemaParser, Types, ValueNotation Classes: ASN1Error, ChoiceError, ClassError, ConstraintError, EnumeratedError, Error, Model, ModelValidationError, Tracer, Wrapper

Constant Summary collapse

CONTAINER_CLASSES =
[Types::Sequence, Types::Set].freeze
VERSION =

RASN2 version number

'1.0.1'

Class Method Summary collapse

Class Method Details

.parse(der, ber: false) ⇒ Types::Base, Array[Types::Base]

Note:

If you want to check ASN.1 grammary, you should define a Model and use RASN2::Model::Accel#parse.

Note:

This method will never decode SEQUENCE OF or SET OF objects, as these ones use the same encoding as SEQUENCE and SET, respectively.

Note:

Real type of tagged value cannot be guessed. So, such tag will generate RASN2::Types::Base objects.

Parse a DER/BER string without checking a model

Parameters:

  • der (String)

    binary string to parse

  • ber (Boolean) (defaults to: false)

    if true, decode a BER string, else a DER one

Returns:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rasn2.rb', line 32

def self.parse(der, ber: false) # rubocop:disable Metrics/AbcSize
  result = []
  until der.nil? || der.empty?
    type = Types.id2type(der)
    size = type.parse!(der, ber: ber)

    if CONTAINER_CLASSES.include?(type.class) || type.constructed?
      RASN2.tracer.tracing_level += 1 unless RASN2.tracer.nil?
      content = self.parse(type.value)
      type.value = content.is_a?(Array) ? content : [content]
      RASN2.tracer.tracing_level -= 1 unless RASN2.tracer.nil?
    end

    result << type
    der = der[size..]
  end

  result.size == 1 ? result[0] : result
end

.trace(io = $stdout, color: false) ⇒ void

This method returns an undefined value.

Trace RASN2 parsing to io. All parsing methods called in block are traced to io. Each ASN.1 element is traced in a line showing element's id, its length and its data.

Examples:

RASN2.trace do
  RASN2.parse("\x02\x01\x01")  # puts "INTEGER id: 2 (0x02), len: 1 (0x01), data: 0x01"
end
RASN2.parse("\x01\x01\xff")    # puts nothing onto STDOUT

with color

RASN2.trace(color: true) do
  RASN2.parse("\x02\x01\x01")  # same output but with ANSI colors
end

Parameters:

  • io (IO) (defaults to: $stdout)
  • color (Boolean) (defaults to: false)

    enable colorized output using pastel gem (requires pastel to be installed)



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rasn2/tracer.rb', line 68

def self.trace(io=$stdout, color: false)
  self.tracer = Tracer.new(io, color: color)
  Tracer::TRACED_CLASSES.each(&:start_tracing)

  begin
    yield self.tracer
  ensure
    Tracer::TRACED_CLASSES.reverse.each(&:stop_tracing)
    self.tracer.io.flush
    self.tracer = nil
  end
end

.tracerObject



82
83
84
# File 'lib/rasn2/tracer.rb', line 82

def self.tracer
  @tracer
end

.tracer=(tracer) ⇒ Object



86
87
88
# File 'lib/rasn2/tracer.rb', line 86

def self.tracer=(tracer)
  @tracer = tracer
end