Module: RASN1

Defined in:
lib/rasn1.rb,
lib/rasn1/model.rb,
lib/rasn1/types.rb,
lib/rasn1/errors.rb,
lib/rasn1/tracer.rb,
lib/rasn1/version.rb,
lib/rasn1/wrapper.rb,
lib/rasn1/types/any.rb,
lib/rasn1/types/set.rb,
lib/rasn1/types/base.rb,
lib/rasn1/types/null.rb,
lib/rasn1/types/choice.rb,
lib/rasn1/types/set_of.rb,
lib/rasn1/types/boolean.rb,
lib/rasn1/types/integer.rb,
lib/rasn1/types/sequence.rb,
lib/rasn1/types/utc_time.rb,
lib/rasn1/types/ia5string.rb,
lib/rasn1/types/object_id.rb,
lib/rasn1/types/primitive.rb,
lib/rasn1/types/bit_string.rb,
lib/rasn1/types/bmp_string.rb,
lib/rasn1/types/enumerated.rb,
lib/rasn1/types/constrained.rb,
lib/rasn1/types/constructed.rb,
lib/rasn1/types/sequence_of.rb,
lib/rasn1/types/utf8_string.rb,
lib/rasn1/types/octet_string.rb,
lib/rasn1/types/numeric_string.rb,
lib/rasn1/types/visible_string.rb,
lib/rasn1/types/generalized_time.rb,
lib/rasn1/types/printable_string.rb,
lib/rasn1/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: Types Classes: ASN1Error, ChoiceError, ClassError, ConstraintError, EnumeratedError, Error, Model, ModelValidationError, Tracer, Wrapper

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

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

Note:

If you want to check ASN.1 grammary, you should define a Model and use RASN1::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 RASN1::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:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rasn1.rb', line 26

def self.parse(der, ber: false) # rubocop:disable Metrics:AbcSize
  type = Types.id2type(der)
  type.parse!(der, ber: ber)

  if CONTAINER_CLASSES.include?(type.class)
    subder = type.value
    ary = []
    RASN1.tracer.tracing_level += 1 unless RASN1.tracer.nil?
    until subder.empty?
      ary << self.parse(subder)
      subder = subder[ary.last.to_der.size..-1]
    end
    RASN1.tracer.tracing_level -= 1 unless RASN1.tracer.nil?
    type.value = ary
  end
  type
end

.trace(io = $stdout) ⇒ void

This method returns an undefined value.

Trace RASN1 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:

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

Parameters:

  • io (IO) (defaults to: $stdout)


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rasn1/tracer.rb', line 45

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

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

.tracerObject



59
60
61
# File 'lib/rasn1/tracer.rb', line 59

def self.tracer
  @tracer
end