Class: Ccrypto::Java::ASN1Engine

Inherits:
Object
  • Object
show all
Extended by:
DataConversion
Includes:
TR::CondUtils, TeLogger::TeLogHelper
Defined in:
lib/ccrypto/java/engines/asn1_engine.rb

Class Method Summary collapse

Methods included from DataConversion

from_b64, from_b64_mime, from_hex, included, logger, to_b64, to_b64_mime, to_bin, to_hex, to_java_bytes, to_str

Class Method Details

.asn1_length(*args, &block) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ccrypto/java/engines/asn1_engine.rb', line 134

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

    totalLen = 0

    ais = org.bouncycastle.asn1.ASN1InputStream.new(v)
    res = ais.readObject
    totalLen = res.encoded.length

    totalLen

  else
    0
  end
end

.build(*args, &block) ⇒ Object



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
77
# File 'lib/ccrypto/java/engines/asn1_engine.rb', line 19

def self.build(*args,&block)
  type = args.first
  val = args[1]
  case type
  when :oid
    ASN1Object.new(type,org.bouncycastle.asn1.ASN1ObjectIdentifier.new(val))

  when :seq
    v = org.bouncycastle.asn1.ASN1EncodableVector.new
    val.each do |vv|
      if vv.is_a?(::Java::byte[])
        v.add(org.bouncycastle.asn1.DERBitString.new(vv))
      else
        v.add(vv)
      end
    end
    ASN1Object.new(type,org.bouncycastle.asn1.DERSequence.new(v))

  when :str, :utf8_str
    ASN1Object.new(type,org.bouncycastle.asn1.DERUTF8String.new(val.to_s))

  when :octet_str
    if val.is_a?(Array)
      val = val.pack("c*").to_java_bytes
    elsif val.respond_to?(:to_bin)
      val = val.to_bin
    end

    ASN1Object.new(type, val)

  when :int
    #ASN1Object.new(type,org.bouncycastle.asn1.DERInteger.new(val))
    ASN1Object.new(type,org.bouncycastle.asn1.ASN1Integer.new(val))
    #Java::OrgBouncycastleAsn1::DERInteger.new(val)

  when :bin
    ASN1Object.new(type,org.bouncycastle.asn1.DERBitString.new(to_java_bytes(val)))
    #baos = java.io.ByteArrayOutputStream.new
    #aos = org.bouncycastle.asn1.ASN1OutputStream.new(baos)
    #aos.writeObject(org.bouncycastle.asn1.DERBitString.new(to_java_bytes(val)))
    #aos.flush
    #aos.close

    #baos

  when :date, :time, :generalize_time
    if val.is_a?(Time)
      val = val.to_java
    elsif not val.is_a?(java.util.Date)
      raise ASN1EngineException, "Unknown datetime objec to convert ['#{val.class}']"
    end
    
    #ASN1Object.new(type,org.bouncycastle.asn1.DERUTCTime.new(val))
    ASN1Object.new(type,org.bouncycastle.asn1.DERGeneralizedTime.new(val))

  else
    raise ASN1EngineException, "Unknown ASN1 Object type '#{type.class}'"
  end
end

.to_value(*args, &block) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ccrypto/java/engines/asn1_engine.rb', line 80

def self.to_value(*args, &block)

  val = args.first
  #teLogger.debug "Received #{val}"

  expectedType = args[1]

  if val.is_a?(::Java::byte[])
    ais = org.bouncycastle.asn1.ASN1InputStream.new(val)
    tag = ais.readObject
  elsif val.is_a?(String)
    ais = org.bouncycastle.asn1.ASN1InputStream.new(val.to_java_bytes)
    tag = ais.readObject
  else
    tag = val
  end
  #raise ASN1EngineException, "Byte array is expected" if not val.is_a?(::Java::byte[])

  #teLogger.debug "Tag : #{tag} / #{tag.class}"

  case tag
  when org.bouncycastle.asn1.ASN1ObjectIdentifier
    tag.id

  when org.bouncycastle.asn1.DEROctetString
    tag.octets

  when org.bouncycastle.asn1.ASN1Integer
    #tag.int_value_exact
    tag.value

  when org.bouncycastle.asn1.DLSequence, org.bouncycastle.asn1.DERSequence
    tag.to_a

  when org.bouncycastle.asn1.DERUTF8String, org.bouncycastle.asn1.DERPrintableString, org.bouncycastle.asn1.DERVisibleString
    tag.to_s

  when org.bouncycastle.asn1.DERNumericString
    "#{tag.to_i}"

  when org.bouncycastle.asn1.DERUTCTime, org.bouncycastle.asn1.ASN1UTCTime, org.bouncycastle.asn1.ASN1GeneralizedTime
    tag.date

  when org.bouncycastle.asn1.DERBitString
    tag.bytes

  else
    raise ASN1EngineException, "Unknown type '#{tag}'"
  end



end