Module: RASN1::Types

Defined in:
lib/rasn1/types.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/enumerated.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

Overview

This modules is a namesapce for all ASN.1 type classes.

Author:

  • Sylvain Daubert

Defined Under Namespace

Classes: Any, Base, BitString, Boolean, Choice, Constructed, Enumerated, GeneralizedTime, IA5String, Integer, Null, NumericString, ObjectId, OctetString, Primitive, PrintableString, Sequence, SequenceOf, Set, SetOf, UtcTime, Utf8String, VisibleString

Class Method Summary collapse

Class Method Details

.constructedArray<Types::Constructed>

Give all constructed types

Returns:



15
16
17
18
# File 'lib/rasn1/types.rb', line 15

def self.constructed
  @constructed ||= self.constants.map { |c| Types.const_get(c) }.
                        select { |klass| klass < Constructed }
end

.primitivesArray<Types::Primitive>

Give all primitive types

Returns:



8
9
10
11
# File 'lib/rasn1/types.rb', line 8

def self.primitives
  @primitives ||= self.constants.map { |c| Types.const_get(c) }.
                       select { |klass| klass < Primitive }
end

.tag2type(tag) ⇒ Types::Base

Give ASN.1 type from an integer. If tag is unknown, return a Base object.

Parameters:

Returns:

Raises:



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

def self.tag2type(tag)
  raise ASN1Error, "tag is out of range" if tag > 0xff

  if !defined? @tag2types
    constructed = self.constructed - [Types::SequenceOf, Types::SetOf]
    primitives = self.primitives - [Types::Enumerated]
    ary = [primitives, constructed].flatten.map do |type|
      next unless type.const_defined? :TAG
      [type::TAG, type]
    end
    @tag2types = Hash[ary]
    @tag2types.default = Types::Base
  end

  klass = @tag2types[tag & 0xdf] # Remove CONSTRUCTED bit
  is_constructed = (tag & 0x20) == 0x20
  asn1class = Types::Base::CLASSES.key(tag & 0xc0)
  options = { class: asn1class, constructed: is_constructed }
  options[:tag_value] = (tag & 0x1f) if klass == Types::Base
  klass.new(options)
end