Class: Ccrypto::Ruby::X509Engine

Inherits:
Object
  • Object
show all
Includes:
TR::CondUtils, TeLogger::TeLogHelper
Defined in:
lib/ccrypto/ruby/engines/x509_engine.rb

Instance Method Summary collapse

Constructor Details

#initialize(cert_profile) ⇒ X509Engine

Returns a new instance of X509Engine.



11
12
13
# File 'lib/ccrypto/ruby/engines/x509_engine.rb', line 11

def initialize(cert_profile)
  @certProfile = cert_profile
end

Instance Method Details

#generate(issuerKey, &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
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
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
# File 'lib/ccrypto/ruby/engines/x509_engine.rb', line 15

def generate(issuerKey, &block)

  cp = @certProfile
  cert = OpenSSL::X509::Certificate.new
  cert.version = 2
  cert.serial = OpenSSL::BN.new(cp.serial, 16)
  cert.subject = to_cert_subject

  ext = OpenSSL::X509::ExtensionFactory.new
  ext.subject_certificate = cert

  iss = cp.issuer_cert

  iss = iss.nativeX509 if iss.is_a?(Ccrypto::X509Cert)

  if not_empty?(iss) 
    raise X509EngineException, "Issuer certificate must be X509 Certificate object" if not iss.is_a?(OpenSSL::X509::Certificate)
    cert.issuer = iss.subject
    ext.issuer_certificate = iss

    cp.match_issuer_not_before(iss.not_before)
    cp.match_issuer_not_after(iss.not_after)

  else
    cert.issuer = cert.subject
    ext.issuer_certificate = cert
  end

  cert.not_before = cp.not_before 
  cert.not_after = cp.not_after

  case cp.public_key
  when Ccrypto::PublicKey
    pubKey = cp.public_key.native_pubKey
  else
    raise X509EngineException, "Public key type '#{cp.public_key.class}' is not supported"
  end

  if pubKey.is_a?(OpenSSL::PKey::EC::Point)
    # ECC patch
    pub = OpenSSL::PKey::EC.new(pubKey.group)
    pub.public_key = pubKey
    cert.public_key = pub
  else
    cert.public_key = pubKey
  end

  cert.add_extension(ext.create_extension("basicConstraints","CA:TRUE",true)) if cp.gen_issuer_cert?
  cert.add_extension(ext.create_extension("subjectKeyIdentifier","hash")) if cp.gen_subj_key_id?
  cert.add_extension(ext.create_extension("authorityKeyIdentifier","keyid:always,issuer:always")) if cp.gen_auth_key_id?

  #cert.add_extension(ext.create_extension("keyUsage",to_keyusage,true))
  cp.key_usage.selected.each do |ku,critical|
    teLogger.debug "Setting KeyUsage : #{ku} (#{critical})"
    case ku
    when :crlSign
      cert.add_extension(ext.create_extension("keyUsage","cRLSign",critical))
    else
      cert.add_extension(ext.create_extension("keyUsage",ku.to_s,critical))
    end
  end

 
  #extKeyUsage = to_extkeyusage
  extKeyUsage = []
  cp.ext_key_usage.selected.each do |ku,critical|
    case ku
    when :allPurpose
      #kur << :anyExtendedKeyUsage
      cert.add_extension(ext.create_extension("extendedKeyUsage","anyExtendedKeyUsage",critical)) 
    when :timestamping
      #kur << :timeStamping
      cert.add_extension(ext.create_extension("extendedKeyUsage","timeStamping",critical)) 
    when :ocspSigning
      #kur << :oCSPSigning
      cert.add_extension(ext.create_extension("extendedKeyUsage","oCSPSigning",critical)) 
    when :ipSecIKE
      #kur << :ipsecIKE
      cert.add_extension(ext.create_extension("extendedKeyUsage","ipsecIKE",critical)) 
    when :msCtlsign
      #kur << :msCTLSign
      cert.add_extension(ext.create_extension("extendedKeyUsage","msCTLSign",critical)) 
    when :msEFS
      #kur << :msEfs
      cert.add_extension(ext.create_extension("extendedKeyUsage","msEfs",critical)) 
    else
      #kur << ku
      cert.add_extension(ext.create_extension("extendedKeyUsage",ku.to_s,critical)) 
    end
  end

  cp.domain_key_usage.each do |dku, critical|
    cert.add_extension(ext.create_extension("extendedKeyUsage",dku.to_s,critical)) 
  end

  cert.add_extension(ext.create_extension("subjectAltName","email:#{cp.email.join(",email:")}",false)) if not_empty?(cp.email)
  cert.add_extension(ext.create_extension("subjectAltName","DNS:#{cp.dns_name.join(",DNS:")}",false)) if not_empty?(cp.dns_name)
  cert.add_extension(ext.create_extension("subjectAltName","IP:#{cp.ip_addr.join(",IP:")}",false)) if not_empty?(cp.ip_addr)
  cert.add_extension(ext.create_extension("subjectAltName","URI:#{cp.uri.join(",URI:")}",false)) if not_empty?(cp.uri)

  # try to sync the structure with Java BC output
  # whereby single name = multiple URI however failed
  # If single format is required need more R&D
  #
  #crlDistPoint = []
  #if not_empty?(cp.crl_dist_point)
  #  cnt = 1
  #  cp.crl_dist_point.each do |cdp|
  #    crlDistPoint << "URI.#{cnt}:#{cdp}"
  #    cnt += 1
  #  end
  #end
  #p crlDistPoint.join(",")
  #cert.add_extension(ext.create_extension("crlDistributionPoints","URI:#{crlDistPoint.join(",")}",false)) if not_empty?(crlDistPoint)
  #
  cert.add_extension(ext.create_extension("crlDistributionPoints","URI:#{cp.crl_dist_point.join(",URI:")}",false)) if not_empty?(cp.crl_dist_point)

  aia = []
  aia << "OCSP;URI:#{cp.ocsp_url.join(",OCSP;URI:")}" if not_empty?(cp.ocsp_url)
  aia << "caIssuers;URI:#{cp.issuer_url.join(",caIssuers;URI:")}" if not_empty?(cp.issuer_url)
  cert.add_extension(ext.create_extension("authorityInfoAccess",aia.join(","),false)) if not_empty?(aia)


  case issuerKey
  when Ccrypto::KeyBundle
    privKey = issuerKey.private_key.native_privKey
  when Ccrypto::PrivateKey
    privKey = issuerKey.native_privKey
  else
    raise X509EngineException, "Unsupported issuer key #{issuerKey}"
  end

  res = cert.sign(privKey, DigestEngine.instance(cp.hashAlgo).native_instance)

  Ccrypto::X509Cert.new(res)

end