Class: Ccrypto::Java::Keystore::PEMKeystore

Inherits:
Object
  • Object
show all
Includes:
DataConversion, TR::CondUtils
Defined in:
lib/ccrypto/java/keystore/pem_keystore.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

.from_pem(str) ⇒ Object

def to_pem

Raises:

  • (KeystoreException)


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
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ccrypto/java/keystore/pem_keystore.rb', line 48

def self.from_pem(str)
 
  raise KeystoreException, "block is required" if not block

  case check_keytype(str)
  when :ecc
    cont = str.lines[1..-2].join
    Ccrypto::Java::ECCPrivateKey.to_key(from_b64(cont))
  when :rsa
  when :ed25519
  when :x25519
  when :crystal_dilithium
  when :crystal_kyber
  else
    raise KeystoreException, "Unable to derive keytype from input : '#{str}'"
  end

  case key
  when java.security.interfaces.ECPrivateKey
    param = java.security.AlgorithmParameters.getInstance("EC")
    param.init(key.params)
    oid = param.getParameterSpec(java.security.spec.ECGenParameterSpec.java_class).name
    curve = org.bouncycastle.asn1.x9.ECNamedCurveTable.getName(org.bouncycastle.asn1.ASN1ObjectIdentifier.new(oid)) 
    logger.debug "Recover curve info : #{curve}"
    conf = Ccrypto::Java::ECCEngine.find_curve(curve) 
    logger.debug "Found config : #{conf}"
    [Ccrypto::Java::ECCKeyBundle.new(kp, conf), userCert, chain]

  when org.bouncycastle.jcajce.provider.asymmetric.ec::BCECPrivateKey
    curve = key.params.name
    logger.debug "Recover curve info : #{curve}"
    conf = Ccrypto::Java::ECCEngine.find_curve(curve) 
    logger.debug "Found config : #{conf}"
    [Ccrypto::Java::ECCKeyBundle.new(kp, conf), userCert, chain]
  when java.security.interfaces.RSAPrivateKey
    [Ccrypto::Java::RSAKeyBundle.new(kp), userCert, chain]
  else
    raise KeystoreException, "Unknown key type #{key}"
  end
  
 
end

.to_pem(&block) ⇒ Object

Raises:

  • (KeystoreException)


11
12
13
14
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
46
# File 'lib/ccrypto/java/keystore/pem_keystore.rb', line 11

def self.to_pem(&block)

  raise KeystoreException, "Block is required" if not block

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

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

  output = block.call(:output)
  if not_empty?(output)
    ext = File.extname(output)
    bname = File.basename(output, ext)
    privPath = File.join("#{bname}_priv#{ext}")
    pubPath = File.join("#{bname}_pub#{ext}")

    privHead, privFoot, pubHead, pubFoot = marker(keypair)
    File.open(privPath,"wb") do |f|
      f.write privHead
      f.write to_b64(keypair.private.to_bin)
      f.write privFoot
    end

    File.open(pubPath,"wb") do |f|
      f.write pubHead
      f.write to_b64(keypair.public.to_bin)
      f.write pubFoot
    end

  else
    [to_b64(keypair.private.to_bin),to_b64(keypair.public.to_bin)]

  end
  
end