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

This file is part of RASN1 See codeberg.org/lemontree55/rasn1 for more informations Copyright © 2016-2024 Sylvain Daubert <[email protected]> Copyright © 2024 LemonTree55 <[email protected]> This program is published under MIT license.

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 =

RASN1 version number

'0.16.2'

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rasn1.rb', line 36

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)
      RASN1.tracer.tracing_level += 1 unless RASN1.tracer.nil?
      content = self.parse(type.value)
      type.value = content.is_a?(Array) ? content : [content]
      RASN1.tracer.tracing_level -= 1 unless RASN1.tracer.nil?
    end

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

  result.size == 1 ? result[0] : result
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)


51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rasn1/tracer.rb', line 51

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



65
66
67
# File 'lib/rasn1/tracer.rb', line 65

def self.tracer
  @tracer
end