Module: Ccrypto::Ruby::PKCS12Store::ClassMethods

Defined in:
lib/ccrypto/ruby/keybundle_store/pkcs12.rb

Instance Method Summary collapse

Instance Method Details

#from_pkcs12(input, &block) ⇒ Object



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
# File 'lib/ccrypto/ruby/keybundle_store/pkcs12.rb', line 15

def from_pkcs12(input, &block)
  raise PKCS12StoreException, "Input cannot be empty" if is_empty?(input) 

  raise PKCS12StoreException, "Block is required" if not block

  inForm = block.call(:in_format)
  case inForm
  when :b64
    inp = from_b64(input)
  when :hex
    inp = from_hex(input)
  else
    inp = input
  end

  pass = block.call(:store_pass)
  raise PKCS12StoreException, "Password cannot be empty" if is_empty?(pass)

  begin
    p12 = OpenSSL::PKCS12.new(inp, pass)
    case p12.key
    when OpenSSL::PKey::EC
      [Ccrypto::Ruby::ECCKeyBundle.new(p12.key), Ccrypto::X509Cert.new(p12.certificate), p12.ca_certs.collect{ |c| Ccrypto::X509Cert.new(c) }]
    else
      [Ccrypto::Ruby::RSAKeyBundle.new(p12.key), Ccrypto::X509Cert.new(p12.certificate), p12.ca_certs.collect{ |c| Ccrypto::X509Cert.new(c) }]
    end
  rescue Exception => ex
    raise PKCS12StoreException, ex
  end

end