Class: Tcrypto::TagProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/tcrypto_java.rb

Overview

injecting TagProvider class

Class Method Summary collapse

Class Method Details

.decode(bin) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/tcrypto_java.rb', line 182

def self.decode(bin)
  ais = org.bouncycastle.asn1.ASN1InputStream.new(bin)
  res = ais.readObject
  if res.is_a?(org.bouncycastle.asn1.DLSequence)
    res = res.to_a
  else
    res
  end

  res
end

.encode(type, val, opts = { }, &block) ⇒ Object



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
78
79
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/tcrypto_java.rb', line 51

def self.encode(type, val, opts = { }, &block)
  case type
  when :oid
    org.bouncycastle.asn1.ASN1ObjectIdentifier.new(val)
  when :octet_str, :bin
    if not val.is_a?(Java::byte[])
      val = val.encoded
    end
    org.bouncycastle.asn1.DEROctetString.new(val)
  when :str, :utf8_str
    org.bouncycastle.asn1.DERUTF8String.new(val)
  when :pstr, :printable_str
    org.bouncycastle.asn1.DERPrintableString.new(val)
  when :vstr, :visible_str
    org.bouncycastle.asn1.DERVisibleString.new(val)
  when :nstr, :numeric_str
    org.bouncycastle.asn1.DERNumericString.new(val)
  when :int
    org.bouncycastle.asn1.DERInteger.new(val)
  when :seq
    v = org.bouncycastle.asn1.ASN1EncodableVector.new
    val.each do |vv|
      v.add(vv)
    end
    org.bouncycastle.asn1.DERSequence.new(v)
  when :date, :datetime, :time
    org.bouncycastle.asn1.DERUTCTime.new(val)
  when :long_content_write
    # special case...
    # derived from actual code of writing long asn1 code to a file
    res = { }
    raise TcryptoJava::Error, "Source to write long content is not available" if val[:src].nil?

    srcHash = val[:src]
    if not srcHash[:file_is].nil?
      TcryptoJava::GConf.instance.glog.debug "Encrypted source is file"
      src = srcHash[:file_is]
      srcLen = srcHash[:file].length if not srcHash[:file].nil?
    elsif not srcHash[:bin_is].nil?
      TcryptoJava::GConf.instance.glog.debug "Encrypted source is memory buffer"
      src = srcHash[:bin_is]
      srcLen = srcHash[:bin].length if not srcHash[:bin].nil?
    else
      raise TcryptoJava::Error, "Source is unknown for long content write"
    end

    if val[:dest].nil?
      TcryptoJava::GConf.instance.glog.debug "Staging output not given. Internal generation of output."
      if val[:temp_dir].nil? or val[:temp_dir].empty?
        tmpPayloadFile = java.io.File.createTempFile('payload_',nil)
      else
        Tcrypto::GConf.instance.glog.debug "temp_dir is defined: #{val[:temp_dir]}"
        tmpPayloadFile = java.io.File.new(val[:temp_dir],"payload_#{SecureRandom.uuid}.tmp")
      end
      
      Tcrypto::GConf.instance.glog.debug "Payload staging temp file created : #{tmpPayloadFile.absolute_path}"
      tmpPayloadFile.deleteOnExit if not val[:auto_remove_staging].nil? and val[:auto_remove_staging]
      os = java.io.FileOutputStream.new(tmpPayloadFile) 
      res[:payload_path] = tmpPayloadFile.absolute_path
    else
      if val[:dest].is_a?(java.io.OutputStream)
        TcryptoJava::GConf.instance.glog.debug "Given staging output is an OutputStream. Directly use the object."
        os = val[:dest]
      elsif val[:dest].is_a?(String)
        TcryptoJava::GConf.instance.glog.debug "Staging output gave by application to be at #{val[:dest]}"
        # assume is file path?
        f = java.io.File.new(val[:dest])
        raise TcryptoJava::Error, "Cannot write destination to a directory [#{f.absolute_path}]" if f.is_directory?
        TcryptoJava::GConf.instance.glog.debug "Opening given path #{f.absolute_path} for payload staging"
        os = java.io.FileOutputStream.new(f)
        res[:payload_path] = f.absolute_path
      else
        raise TcryptoJava::Error, "Unsupported staging output #{val[:dest]}"
      end
    end
    
    gos = org.bouncycastle.asn1.DERSequenceGenerator.new(os)
    val[:header].each do |h|
      gos.addObject(h)
    end
    gos.getRawOutputStream.flush
    
    longOut = org.bouncycastle.asn1.BEROctetStringGenerator.new(gos.getRawOutputStream)
   
    srcLen = -1 if srcLen.nil?
    
    bufSize = val[:bufSize] || 102400
    outOs = longOut.getOctetOutputStream(Java::byte[bufSize].new)
    buf = Java::byte[bufSize].new
    total = 0
    while((read = src.read(buf,0,buf.length)) != -1)
      if block
        block.call(:before_write, { buf: buf, from: 0, len: read, total: srcLen })
      end
      outOs.write(buf,0,read)
      total += read
      #Tcrypto::GConf.instance.glog.debug "Copied #{read} bytes to final output (Total : #{total})"
    end

    outOs.flush
    # write the octet string ending tag
    outOs.close
    
    # write the sequence ending tag
    gos.close

    os.close

    #org.bouncycastle.asn1.BEROctetStringGenerator.new(os)
    #org.bouncycastle.asn1.DERSequenceGenerator.new(os)
    #org.bouncycastle.asn1.DEROutputStream.new(os)
    #org.bouncycastle.asn1.ASN1OutputStream.new(os)
    
    res

  when :long_content_read
    is = val
    raise TcryptoJava::Error, "Long content (read) requires upstream input stream is given." if val.nil?
    raise TcryptoJava::Error, "Given val is not input stream." if not val.is_a?(java.io.InputStream)

    org.bouncycastle.asn1.ASN1InputStream.new(is)

  else
    raise TcryptoJava::Error, "Unknown type '#{type}' to encode"
  end
end

.encode_idObject



47
48
49
# File 'lib/tcrypto_java.rb', line 47

def self.encode_id
  encode(:oid,"#{Tcrypto::ENCODER_ID}.1")
end

.encode_timestamp(v = java.util.Calendar.getInstance.time) ⇒ Object



178
179
180
# File 'lib/tcrypto_java.rb', line 178

def self.encode_timestamp(v = java.util.Calendar.getInstance.time)
  self.encode(:datetime, v)
end

.envp_type(bin) ⇒ Object

Raises:

  • (GcryptTag::Error)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tcrypto_java.rb', line 27

def self.envp_type(bin)
  dec = decode(bin)
  dec.each_slice(2) do |e,f|
    if e.is_a?(org.bouncycastle.asn1.DERSequence)
      dat = e.to_a
      if dat[0].to_s == Tcrypto::Tag.value(:envp_header)
        return Tcrypto::Tag.key(dat[1].to_s)
      end
    elsif e.is_a?(org.bouncycastle.asn1.ASN1ObjectIdentifier)
      if e.to_s == Tcrypto::Tag.value(:envp_header)
        return Tcrypto::Tag.key(f.to_s)
      end
    else
      raise TcryptoJava::Error, "Not a recognized tag? #{e.class}"
    end
  end

  raise GcryptTag::Error, "Header not found"
end

.to_bin(out, opts = { }) ⇒ Object



234
235
236
# File 'lib/tcrypto_java.rb', line 234

def self.to_bin(out, opts = { })
  self.to_format(out, opts)
end

.to_format(out, opts = { }) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/tcrypto_java.rb', line 238

def self.to_format(out, opts = { })
  if not (out.nil?)
    
    case out
    when org.bouncycastle.asn1.ASN1Encodable

      if not (opts.nil? or opts.empty?)

        case opts[:to_format]
        when :der
          out.encoded
        when :pem
        when :b64
        else
          out.encoded
        end
      else
        out.encoded
      end
      
    when String
      out.to_java.getBytes
    else
      out
    end

  else
    out
  end

end

.to_string(buf, opts = { }) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/tcrypto_java.rb', line 219

def self.to_string(buf, opts = { })
  if not buf.nil?
    case buf
    when Java::byte[]
      String.from_java_bytes(buf)
    when String
      buf
    else
      raise TcryptoJava::Error, "Unknown type to conver to string #{buf}"
    end
  else
    ""
  end
end

.value(tag, opts = { }) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/tcrypto_java.rb', line 194

def self.value(tag, opts = { })
  if not tag.nil?
    case tag
    when org.bouncycastle.asn1.ASN1ObjectIdentifier
      tag.id
    when org.bouncycastle.asn1.DEROctetString
      tag.octets
    when org.bouncycastle.asn1.BEROctetString
      tag.octet_stream
    when org.bouncycastle.asn1.ASN1Integer
      tag.value
    when org.bouncycastle.asn1.DLSequence
      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
      tag.adjusted_date
    else
      raise TcryptoJava::Error, "Unknown type '#{tag.class}'"
    end
  end
end