Class: Ccrypto::Ruby::ASN1Engine

Inherits:
Object
  • Object
show all
Includes:
TR::CondUtils
Defined in:
lib/ccrypto/ruby/engines/asn1_engine.rb

Class Method Summary collapse

Class Method Details

.asn1_length(*args, &block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ccrypto/ruby/engines/asn1_engine.rb', line 57

def self.asn1_length(*args, &block)
  
  val = args.first
  if not_empty?(val)
    
    v = val
    if v.is_a?(ASN1Object)
      v = v.native_asn1
    end

    totalLen = 0
    begin
      OpenSSL::ASN1.traverse(v) do |depth,offset,headerLen,length,constructed,tagClass,tag|
        totalLen = headerLen+length
        break
      end
    rescue StandardError => ex
    rescue OpenSSL::ASN1::ASN1Error => ex
      raise ASN1EngineException, ex
    end

    totalLen

  else
    0
  end


end

.build(*args, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ccrypto/ruby/engines/asn1_engine.rb', line 10

def self.build(*args, &block)
  type = args.first
  val = args[1]
  case type
  when :oid
    ASN1Object.new(type, OpenSSL::ASN1::ObjectId.new(val))
  when :seq
    ASN1Object.new(type, OpenSSL::ASN1::Sequence.new(val))
  when :str, :utf8_str
    ASN1Object.new(type, OpenSSL::ASN1::UTF8String.new(val))
  when :octet_str
    ASN1Object.new(type, OpenSSL::ASN1::OctetString.new(val))
  when :int
    ASN1Object.new(type, OpenSSL::ASN1::Integer.new(val))
  when :bin
    ASN1Object.new(type, OpenSSL::ASN1::BitString.new(val))
  when :date, :time, :generalize_time
    ASN1Object.new(type, OpenSSL::ASN1::GeneralizedTime.new(val))
  else
    raise ASN1EngineException, "Unknown ASN1 object type '#{type.class}'"
  end
end

.openssl_to_asn1object(oasn1) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ccrypto/ruby/engines/asn1_engine.rb', line 87

def self.openssl_to_asn1object(oasn1)
  case oasn1
  when OpenSSL::ASN1::ObjectId
    type = :oid
  when OpenSSL::ASN1::Sequence
    type = :seq
  when OpenSSL::ASN1::UTF8String
    type = :str
  when OpenSSL::ASN1::OctetString
    type = :octet_str
  when OpenSSL::ASN1::Integer
    type = :int
  when OpenSSL::ASN1::BitString
    type = :bin
  when OpenSSL::ASN1::GeneralizedTime
    type = :time
  end

  ASN1Object.new(:oid, oasn1)
end

.to_value(*args, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ccrypto/ruby/engines/asn1_engine.rb', line 33

def self.to_value(*args, &block)
  val = args.first
  expectedType = args[1]
  v = OpenSSL::ASN1.decode(val).value
  if not_empty?(expectedType)
    case expectedType
    when :int
      if v.is_a?(OpenSSL::BN)
        v.to_i
      else
        v
      end
    else
      v
    end
  else
    if v.is_a?(OpenSSL::BN)
      v.to_i
    else
      v
    end
  end
end