Class: RASN2::Types::Any

Inherits:
Base
  • Object
show all
Defined in:
lib/rasn2/types/any.rb

Overview

ASN.1 ANY: accepts any types

If any#value is nil and Any object is not Base#optional?, any will be encoded as a Null object.

Author:

  • Sylvain Daubert

Constant Summary

Constants inherited from Base

Base::CLASSES, Base::CLASS_MASK, Base::ID, Base::INDEFINITE_LENGTH, Base::MULTI_OCTETS_ID

Instance Attribute Summary

Attributes inherited from Base

#asn1_class, #default, #model, #name, #options

Instance Method Summary collapse

Methods inherited from Base

#==, #asn1_class_to_s, #attributes, #bin2hex, #build, #check_id, #colorize, #common_inspect, constrained?, #constructed?, #der2name, #der_to_value, #do_parse_explicit, #do_parse_explicit_with_tracing, #do_parse_with_tracing, #encode_identifier_octets, #encode_size, encoded_type, #explicit?, #explicit_type, #find_type, #get_data, #get_length, #id, #id2octets, #id_value, #implicit?, #indent, #initialize, #initialize_copy, #inspect, #inspect_value, #msg_type, #optional?, parse, #pc_bit, #primitive?, #private?, #raise_id_error, #raise_on_indefinite_length, #self2name, #set_class, #set_default, #set_optional, #set_tag, #set_value, #specific_initializer, start_tracing, stop_tracing, #tag_id_to_integer, #tagged?, #trace_data, #trace_real, #tracer, type, #type, #universal?, #unpack, #unsigned_to_chained_octets, #value, #value=, #value?, #value_size, #value_to_der, #void_value

Methods included from Helpers::Colorize

#begin_colorizer, #brace_surround, #colorize, #colorize_attribute, #colorize_bool, #colorize_class, #colorize_default, #colorize_enum, #colorize_id, #colorize_name, #colorize_nil, #colorize_value, #colorizer, #colorizer=, #end_colorizer, #int_with_hex, #length_specifier, #parens_hex

Constructor Details

This class inherits a constructor from RASN2::Types::Base

Instance Method Details

#can_build?Boolean

Returns:



24
25
26
# File 'lib/rasn2/types/any.rb', line 24

def can_build?
  value? || !optional?
end

#do_parse(der, ber: false) ⇒ Object

See Also:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rasn2/types/any.rb', line 52

def do_parse(der, ber: false)
  asn1_class, _pc, id, id_size = Types.decode_identifier_octets(der)
  @id = id
  @asn1_class = asn1_class

  if der.empty?
    return [0, ''] if optional?

    raise ASN1Error, 'Expected ANY but get nothing'
  end

  total_length, = get_data(der[id_size..], ber)
  total_length += id_size

  @no_value = false
  real_value = der[0, total_length].to_s
  @value = if constructed?
             RASN2.parse(real_value, ber: ber)
           else
             real_value
           end

  [total_length, @value]
end

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

Parse a DER string. This method updates object: Base#value will be a DER string.

Parameters:

  • der (String)

    DER string

  • ber (Boolean) (defaults to: false)

    if true, accept BER encoding

Returns:

  • (Integer)

    total number of parsed bytes



33
34
35
36
37
# File 'lib/rasn2/types/any.rb', line 33

def parse!(der, ber: false)
  total_length, data = do_parse(der, ber: ber)
  @value = data
  total_length
end

#to_derString

Returns DER-formated string.

Returns:

  • (String)

    DER-formated string



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rasn2/types/any.rb', line 11

def to_der
  if value?
    case @value
    when Base, Model
      @value.to_der
    else
      @value.to_s
    end
  else
    optional? ? '' : Null.new.to_der
  end
end

#traceString

Returns:

  • (String)


41
42
43
44
45
46
47
48
# File 'lib/rasn2/types/any.rb', line 41

def trace
  return trace_any if value?

  parts = []
  parts << msg_type(no_id: true)
  parts << colorize_nil
  parts.reject(&:empty?).join(' ')
end

#trace_anyObject



77
78
79
# File 'lib/rasn2/types/any.rb', line 77

def trace_any
  "#{msg_type(no_id: false)}#{trace_data(value)}"
end