Module: Ccrypto::Ruby::PKCS12Store

Includes:
DataConversion, TR::CondUtils
Included in:
ECCKeyBundle, KSP12Store, RSAKeyBundle
Defined in:
lib/ccrypto/ruby/keybundle_store/pkcs12.rb

Defined Under Namespace

Modules: ClassMethods Classes: PKCS12StoreException

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DataConversion

#from_b64, #from_hex, #to_b64, #to_hex, #to_int_array

Class Method Details

.included(klass) ⇒ Object



47
48
49
# File 'lib/ccrypto/ruby/keybundle_store/pkcs12.rb', line 47

def self.included(klass)
  klass.extend(ClassMethods)
end

Instance Method Details

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

def to_pkcs12(&block)

  raise PKCS12StoreException, "Block is required" if not block

  ucert = block.call(:cert)
  raise PKCS12StoreException, "Certificate is required" if is_empty?(ucert)

  case ucert
  when String
     begin
       cert = OpenSSL::X509::Certificate.new(ucert)
     rescue Exception => ex
       raise PKCS12StoreException, ex
     end
  when OpenSSL::X509::Certificate
    cert = ucert
  when Ccrypto::X509Cert
    cert = ucert.nativeX509
  else
    raise PKCS12StoreException, "Unknown given certificate to store in P12 : #{cert}"
  end

  ca = block.call(:certchain) 
  ca = [cert] if is_empty?(ca)
  ca = ca.collect do |c|
    case c
    when Ccrypto::X509Cert
      c.nativeX509
    else
      c
    end
  end

  pass = block.call(:store_pass) 
  raise PKCS12StoreException, "Password is required" if is_empty?(pass)

  name = block.call(:key_name)
  name = "Ccrypto KeyBundle" if is_empty?(name)

  keypair = block.call(:keypair)
  raise PKCS12StoreException, "Keypair is required" if is_empty?(keypair)

  res = OpenSSL::PKCS12.create(pass, name, keypair, cert, ca)

  outFormat = block.call(:out_format)
  outFormat = :bin if is_empty?(outFormat)

  case outFormat
  when :b64
    to_b64(res.to_der)
  when :to_hex
    to_hex(res.to_der)
  else
    res.to_der
  end

end