aboutsummaryrefslogtreecommitdiffstats
path: root/lib/asn1kit/types/object_identifier.rb
blob: 1c8f69c9ac0ce12cc8e973eea5d3a3734867e94e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# coding: ASCII-8BIT

class ASN1Kit::ObjectIdentifier < ASN1Kit::Type
  asn1_tag :IMPLICIT, :UNIVERSAL, 6
  asn1_alias "OBJECT IDENTIFIER"

  attr_reader :components

  def initialize(components)
    @components = components
  end

  def self.from_dot_notation(string)
    new(string.split(".").map { |s| Integer(s) })
  end

  def dot_notation
    @components.join(".")
  end

  def to_der
    first, second, rest = @components
    content = [first * 40 + second, *rest].pack("w*")
    der_header(content.bytesize) << content
  end

  private def inspect_inner
    "{ #{@components.join(" ")} }"
  end

  def cast_to(type)
    return type.new(@components) if type <= ASN1Kit::ObjectIdentifier
    super
  end

  def ==(other)
    other.is_a?(ASN1Kit::ObjectIdentifier) && @components == other.components
  end
end

module ASN1Kit::Internal::CompileObjectIdentifier
  refine ASN1Kit::ObjectIdentifier.singleton_class do
    def _new_internal(base, arys)
      obj = allocate
      obj.instance_variable_set(:@_compile_base, base)
      obj.instance_variable_set(:@_compile_arys, arys)
      obj
    end
  end

  refine ASN1Kit::ObjectIdentifier do
    def _compile_fixup(state)
      base_value = remove_instance_variable(:@_compile_base)
      arys = remove_instance_variable(:@_compile_arys)

      if base_value
        base = base_value.unwrap(state)
        components = base.components.dup
      else
        components = []
      end

      arys.each do |ary|
        case ary
        when ASN1Kit::Internal::UnresolvedValue
          rel_oid = ary.unwrap(state)
          components.concat(rel_oid.components)
        else
          components.concat(ary.map { |v| v.unwrap_as_number(state) })
        end
      end

      @components = components
    end
  end
end