Class: Ccrypto::Ruby::X509Engine
- Inherits:
-
Object
- Object
- Ccrypto::Ruby::X509Engine
- Includes:
- TR::CondUtils, TeLogger::TeLogHelper
- Defined in:
- lib/ccrypto/ruby/engines/x509_engine.rb
Instance Method Summary collapse
- #generate(issuerKey, &block) ⇒ Object
- #generate_from_cert_profile(cp, issuerKey, &block) ⇒ Object
- #generate_from_csr(cp, issuerKey, &block) ⇒ Object
-
#initialize(cert_profile) ⇒ X509Engine
constructor
A new instance of X509Engine.
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 |
# File 'lib/ccrypto/ruby/engines/x509_engine.rb', line 15 def generate(issuerKey, &block) cp = @certProfile if not_empty?(cp.csr) teLogger.debug "Given cert profile with CSR" generate_from_csr(cp, issuerKey, &block) else teLogger.debug "Given cert profile with user values" generate_from_cert_profile(cp, issuerKey, &block) end end |
#generate_from_cert_profile(cp, issuerKey, &block) ⇒ Object
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 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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/ccrypto/ruby/engines/x509_engine.rb', line 29 def generate_from_cert_profile(cp, issuerKey, &block) cert = OpenSSL::X509::Certificate.new cert.version = 2 if is_empty?(cert.serial) raise X509EngineException, "Certificate serial no and block is both not given. " if not block serial = block.call(:cert_serial) cert.serial = OpenSSL::BN.new(serial, 16) else cert.serial = OpenSSL::BN.new(cp.serial, 16) end cert.subject = to_cert_subject(cp) 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 elsif pubKey.is_a?(String) # Changes for OpenSSL v3/Ruby v3 # native_pubKey is no longer object, will be a binary string instead pub = OpenSSL::PKey::EC.new(pubKey) cert.public_key = pub else cert.public_key = pubKey end if cp.gen_issuer_cert? spec = [] spec << "CA:TRUE" spec << "pathlen:#{cp.issuer_path_len}" if not_empty?(cp.issuer_path_len) cert.add_extension(ext.create_extension("basicConstraints",spec.join(","),true)) end 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) cp.custom_extension.each do |k,v| cert.add_extension(OpenSSL::X509::Extension.new(k, v[:value], v[:critical])) end # 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) if not_empty?(cp.custom_extension) and cp.custom_extension.is_a?(Hash) teLogger.debug "custom extension" cp.custom_extension.each do |k,v| case v[:type] when :string cert.add_extension OpenSSL::X509::Extension.new(k, OpenSSL::ASN1::OctetString.new(v[:value]), v[:critical]) else raise X509CSREngineException, "Unsupported custom extension type #{v[:type]}" end end end 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 |
#generate_from_csr(cp, issuerKey, &block) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/ccrypto/ruby/engines/x509_engine.rb', line 201 def generate_from_csr(cp, issuerKey, &block) csrObj = Ccrypto::X509CSR.new(cp.csr) csrCp = csrObj.csr_info cp.public_key = csrCp.public_key cert = OpenSSL::X509::Certificate.new cert.version = 2 if is_empty?(cert.serial) serial = block.call(:cert_serial) if block raise X509EngineException, "No serial number is given for the certificate" if is_empty?(serial) cert.serial = OpenSSL::BN.new(serial, 16) else cert.serial = OpenSSL::BN.new(cp.serial, 16) end # allow external to add or edit parsed info before convert into actual certificate csrCp = block.call(:verify_csr_info, csrCp) if block cert.subject = to_cert_subject(csrCp) 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 csrCp.public_key when Ccrypto::PublicKey pubKey = csrCp.public_key.native_pubKey when OpenSSL::PKey::EC, OpenSSL::PKey::RSA pubKey = csrCp.public_key else raise X509EngineException, "Public key type '#{csrCp.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 if cp.gen_issuer_cert? spec = [] spec << "CA:TRUE" spec << "pathlen:#{cp.issuer_path_len}" if not_empty?(cp.issuer_path_len) cert.add_extension(ext.create_extension("basicConstraints",spec.join(","),true)) end #cert.add_extension(ext.create_extension("basicConstraints","CA:TRUE,pathlen:0",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:#{csrCp.email.uniq.join(",email:")}",false)) if not_empty?(csrCp.email) cert.add_extension(ext.create_extension("subjectAltName","DNS:#{csrCp.dns_name.uniq.join(",DNS:")}",false)) if not_empty?(csrCp.dns_name) cert.add_extension(ext.create_extension("subjectAltName","IP:#{csrCp.ip_addr.uniq.join(",IP:")}",false)) if not_empty?(csrCp.ip_addr) cert.add_extension(ext.create_extension("subjectAltName","URI:#{csrCp.uri.uniq.join(",URI:")}",false)) if not_empty?(csrCp.uri) csrCp.custom_extension.each do |k,v| cert.add_extension(OpenSSL::X509::Extension.new(k, v[:value], v[:critical])) end 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 |