Class: RASN1::Wrapper

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/rasn1/wrapper.rb

Overview

This class is used to wrap a Types::Base or Model instance to force its options.

Usage

This class may be used to wrap another RASN1 object by 3 ways:

  • wrap an object to modify its options,

  • implicitly wrap an object (i.e. change its tag),

  • explicitly wrap an object (i.e wrap the object in another explicit ASN.1 tag)

Examples:

# object to wrap
int = RASN1::Types::Integer.new(implicit: 1)    # its tag is 0x81
# simple wrapper, change an option
wrapper = RASN1::Wrapper.new(int, default: 1)
# implicit wrapper
wrapper = RASN1::Wrapper.new(int, implicit: 3)  # wrapped int tag is now 0x83
# explicit wrapper
wrapper = RASN1::Wrapper.new(int, explicit: 4)  # int tag is always 0x81, but it is wrapped in a 0x84 tag

Since:

  • 0.12.0

Defined Under Namespace

Classes: ExplicitWrapper

Instance Method Summary collapse

Constructor Details

#initialize(element, options = {}) ⇒ Wrapper

Returns a new instance of Wrapper.

Parameters:

  • element (Types::Base, Model)

    element to wrap

  • options (Hash) (defaults to: {})

Raises:

Since:

  • 0.12.0



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rasn1/wrapper.rb', line 56

def initialize(element, options={})
  opts = explicit_implicit(options)

  if explicit?
    generate_explicit_wrapper(opts)
    element.options = element.options.merge(generate_explicit_wrapper_options(opts))
    @options = opts
  else
    opts[:value] = element.value
    element.options = element.options.merge(opts)
    @options = {}
  end
  raise RASN1::Error, 'Cannot be implicit and explicit' if explicit? && implicit?

  super(element)
end

Instance Method Details

#asn1_classSymbol

Returns:

  • (Symbol)

See Also:

Since:

  • 0.12.0



149
150
151
152
153
# File 'lib/rasn1/wrapper.rb', line 149

def asn1_class
  return element.asn1_class unless @options.key?(:class)

  @options[:class]
end

#constructed?Boolean

Returns:

  • (Boolean)

See Also:

  • Types::Base#constructed

Since:

  • 0.12.0



157
158
159
160
161
# File 'lib/rasn1/wrapper.rb', line 157

def constructed?
  return element.constructed? unless @options.key?(:constructed)

  @options[:constructed]
end

#elementTypes::Base, Model

Return Wrapped element

Returns:

Since:

  • 0.12.0



131
132
133
# File 'lib/rasn1/wrapper.rb', line 131

def element
  __getobj__
end

#explicit?Boolean

Say if wrapper is an explicit one (i.e. add tag and length to its element)

Returns:

  • (Boolean)

Since:

  • 0.12.0



75
76
77
# File 'lib/rasn1/wrapper.rb', line 75

def explicit?
  !!@explicit
end

#id::Integer

Returns:

  • (::Integer)

See Also:

Since:

  • 0.12.0



137
138
139
140
141
142
143
144
145
# File 'lib/rasn1/wrapper.rb', line 137

def id
  if implicit?
    @implicit
  elsif explicit?
    @explicit
  else
    element.id
  end
end

#implicit?Boolean

Say if wrapper is an implicit one (i.e. change tag of its element)

Returns:

  • (Boolean)

Since:

  • 0.12.0



81
82
83
# File 'lib/rasn1/wrapper.rb', line 81

def implicit?
  !!@implicit
end

#inspect(level = 0) ⇒ String

Parameters:

  • level (::Integer) (defaults to: 0)

Returns:

  • (String)

Since:

  • 0.12.0



171
172
173
174
175
# File 'lib/rasn1/wrapper.rb', line 171

def inspect(level=0)
  return super(level) unless explicit?

  @explicit_wrapper.inspect(level) << ' ' << super(level)
end

#parse!(der, ber: false) ⇒ Integer

Parse a DER string. This method updates object.

Parameters:

  • der (String)

    DER string

  • ber (Boolean) (defaults to: false)

    if true, accept BER encoding

Returns:

  • (Integer)

    total number of parsed bytes

Raises:

Since:

  • 0.12.0



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rasn1/wrapper.rb', line 104

def parse!(der, ber: false)
  if implicit?
    el = generate_implicit_element
    parsed = el.parse!(der, ber: ber)
    element.value = el.value
    parsed
  elsif explicit?
    parsed = @explicit_wrapper.parse!(der, ber: ber)
    element.parse!(@explicit_wrapper.value, ber: ber) if parsed.positive?
    parsed
  else
    element.parse!(der, ber: ber)
  end
end

#primitive?Boolean

Returns:

  • (Boolean)

See Also:

  • Types::Base#primitive

Since:

  • 0.12.0



165
166
167
# File 'lib/rasn1/wrapper.rb', line 165

def primitive?
  !constructed?
end

#to_derString

Convert wrapper and its element to a DER string

Returns:

  • (String)

Since:

  • 0.12.0



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rasn1/wrapper.rb', line 87

def to_der
  if implicit?
    el = generate_implicit_element
    el.to_der
  elsif explicit?
    @explicit_wrapper.value = element
    @explicit_wrapper.to_der
  else
    element.to_der
  end
end

#value?Boolean

Returns:

  • (Boolean)

See Also:

Since:

  • 0.12.0



121
122
123
124
125
126
127
# File 'lib/rasn1/wrapper.rb', line 121

def value?
  if explicit?
    @explicit_wrapper.value?
  else
    __getobj__.value?
  end
end