Class: RASN1::Types::Base Abstract
- Inherits:
-
Object
- Object
- RASN1::Types::Base
- Defined in:
- lib/rasn1/types/base.rb
Overview
Define an optional value
An optional value may be defined using :optional
key from #initialize:
Integer.new(:int, optional: true)
An optional value implies:
-
while parsing, if decoded tag is not optional expected tag, no ASN1Error is raised, and parser tries net tag,
-
while encoding, if #value is
nil
, this value is not encoded.
Define a default value
A default value may be defined using :default
key from #initialize:
Integer.new(:int, default: 0)
A default value implies:
-
while parsing, if decoded tag is not expected tag, no ASN1Error is raised and parser sets default value to this tag. Then parser tries nex tag,
-
while encoding, if #value is equal to default value, this value is not encoded.
Define a tagged value
ASN.1 permits to define tagged values. By example:
-- context specific tag
CType ::= [0] EXPLICIT INTEGER
-- application specific tag
AType ::= [APPLICATION 1] EXPLICIT INTEGER
-- private tag
PType ::= [PRIVATE 2] EXPLICIT INTEGER
These types may be defined as:
ctype = RASN1::Types::Integer.new(explicit: 0) # with explicit, default #asn1_class is :context
atype = RASN1::Types::Integer.new(explicit: 1, class: :application)
ptype = RASN1::Types::Integer.new(explicit: 2, class: :private)
Sometimes, an EXPLICIT type should be CONSTRUCTED. To do that, use :constructed
option:
ptype = RASN1::Types::Integer.new(explicit: 2, class: :private, constructed: true)
Implicit tagged values may also be defined:
ctype_implicit = RASN1::Types::Integer.new(implicit: 0)
Direct Known Subclasses
Constant Summary collapse
- CLASSES =
Allowed ASN.1 tag classes
{ universal: 0x00, application: 0x40, context: 0x80, private: 0xc0 }
- MAX_TAG =
Maximum ASN.1 tag number
0x1e
- INDEFINITE_LENGTH =
Length value for indefinite length
0x80
Instance Attribute Summary collapse
- #asn1_class ⇒ Symbol readonly
-
#default ⇒ Object?
readonly
Default value, if defined.
- #name ⇒ String? readonly
-
#value ⇒ Object
Get value or default value.
Class Method Summary collapse
-
.encode_type ⇒ String
Get ASN.1 type used to encode this one.
-
.parse(der_or_ber, options = {}) ⇒ Object
Parse a DER or BER string.
-
.type ⇒ String
Get ASN.1 type.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Objects are equal if they have same class AND same DER.
-
#constructed? ⇒ ::Boolean
true
if this is a constructed type. -
#explicit? ⇒ ::Boolean?
Say if a tagged type is explicit.
-
#implicit? ⇒ ::Boolean?
Say if a tagged type is implicit.
-
#initialize(value_or_options = {}, options = {}) ⇒ Base
constructor
A new instance of Base.
-
#initialize_copy(other) ⇒ Object
Used by
#dup
and#clone
. - #inspect(level = 0) ⇒ String
- #optional? ⇒ ::Boolean
-
#parse!(der, ber: false) ⇒ Integer
abstract
Parse a DER string.
-
#primitive? ⇒ ::Boolean
true
if this is a primitive type. -
#tag ⇒ Integer
Get tag value.
-
#tagged? ⇒ ::Boolean
Say if this type is tagged or not.
-
#to_der ⇒ String
abstract
DER-formated string.
-
#type ⇒ String
Get ASN.1 type.
-
#value_size ⇒ Integer
Give size in octets of encoded value.
Constructor Details
Instance Attribute Details
#asn1_class ⇒ Symbol (readonly)
64 65 66 |
# File 'lib/rasn1/types/base.rb', line 64 def asn1_class @asn1_class end |
#default ⇒ Object? (readonly)
Returns default value, if defined.
66 67 68 |
# File 'lib/rasn1/types/base.rb', line 66 def default @default end |
#name ⇒ String? (readonly)
62 63 64 |
# File 'lib/rasn1/types/base.rb', line 62 def name @name end |
#value ⇒ Object
Get value or default value
148 149 150 151 152 153 154 |
# File 'lib/rasn1/types/base.rb', line 148 def value if @value.nil? @default else @value end end |
Class Method Details
.encode_type ⇒ String
Get ASN.1 type used to encode this one
79 80 81 |
# File 'lib/rasn1/types/base.rb', line 79 def self.encode_type type end |
.parse(der_or_ber, options = {}) ⇒ Object
More options are supported. See #initialize.
Parse a DER or BER string
88 89 90 91 92 |
# File 'lib/rasn1/types/base.rb', line 88 def self.parse(der_or_ber, ={}) obj = self.new() obj.parse!(der_or_ber, ber: [:ber]) obj end |
.type ⇒ String
Get ASN.1 type
72 73 74 75 |
# File 'lib/rasn1/types/base.rb', line 72 def self.type return @type if @type @type = self.to_s.gsub(/.*::/, '').gsub(/([a-z0-9])([A-Z])/, '\1 \2').upcase end |
Instance Method Details
#==(other) ⇒ Boolean
Objects are equal if they have same class AND same DER
262 263 264 |
# File 'lib/rasn1/types/base.rb', line 262 def ==(other) (other.class == self.class) && (other.to_der == self.to_der) end |
#constructed? ⇒ ::Boolean
Returns true
if this is a constructed type.
194 195 196 |
# File 'lib/rasn1/types/base.rb', line 194 def constructed? !!((self.class < Constructed) || @constructed) end |
#explicit? ⇒ ::Boolean?
Say if a tagged type is explicit
170 171 172 |
# File 'lib/rasn1/types/base.rb', line 170 def explicit? @tag.nil? ? @tag : @tag == :explicit end |
#implicit? ⇒ ::Boolean?
Say if a tagged type is implicit
177 178 179 |
# File 'lib/rasn1/types/base.rb', line 177 def implicit? @tag.nil? ? @tag : @tag == :implicit end |
#initialize_copy(other) ⇒ Object
Used by #dup
and #clone
. Deep copy @value.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/rasn1/types/base.rb', line 132 def initialize_copy(other) @value = case when NilClass, TrueClass, FalseClass, Integer @value else @value.dup end @default = case when NilClass, TrueClass, FalseClass, Integer @default else @default.dup end end |
#inspect(level = 0) ⇒ String
249 250 251 252 253 254 255 256 257 |
# File 'lib/rasn1/types/base.rb', line 249 def inspect(level=0) str = '' str << ' ' * level if level > 0 str << "#{@name} " unless @name.nil? str << "#{type}: #{value.inspect}" str << " OPTIONAL" if optional? str << " DEFAULT #{@default}" unless @default.nil? str end |
#optional? ⇒ ::Boolean
157 158 159 |
# File 'lib/rasn1/types/base.rb', line 157 def optional? @optional end |
#parse!(der, ber: false) ⇒ Integer
This method SHOULD be partly implemented by subclasses to parse data. Subclasses SHOULD respond to #der_to_value
.
Parse a DER string. This method updates object.
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/rasn1/types/base.rb', line 224 def parse!(der, ber: false) return 0 unless check_tag(der) total_length, data = get_data(der, ber) if explicit? # Delegate to #explicit type to generate sub-tag type = explicit_type type.value = @value type.parse!(data) @value = type.value else der_to_value(data, ber: ber) end total_length end |
#primitive? ⇒ ::Boolean
Returns true
if this is a primitive type.
189 190 191 |
# File 'lib/rasn1/types/base.rb', line 189 def primitive? (self.class < Primitive) && !@constructed end |
#tag ⇒ Integer
Get tag value
206 207 208 209 210 211 212 213 214 215 |
# File 'lib/rasn1/types/base.rb', line 206 def tag pc = if @constructed.nil? self.class::ASN1_PC elsif @constructed # true Constructed::ASN1_PC else # false 0 end (@tag_value || self.class::TAG) | CLASSES[@asn1_class] | pc end |
#tagged? ⇒ ::Boolean
Say if this type is tagged or not
163 164 165 |
# File 'lib/rasn1/types/base.rb', line 163 def tagged? !@tag.nil? end |
#to_der ⇒ String
This method SHOULD be partly implemented by subclasses, which SHOULD respond to #value_to_der
.
Returns DER-formated string.
184 185 186 |
# File 'lib/rasn1/types/base.rb', line 184 def to_der build_tag end |
#type ⇒ String
Get ASN.1 type
200 201 202 |
# File 'lib/rasn1/types/base.rb', line 200 def type self.class.type end |
#value_size ⇒ Integer
Give size in octets of encoded value
243 244 245 |
# File 'lib/rasn1/types/base.rb', line 243 def value_size value_to_der.size end |