Class: Ccrypto::X509Cert

Inherits:
Object
  • Object
show all
Includes:
Java::DataConversion, TR::CondUtils
Defined in:
lib/ccrypto/java/ext/x509_cert.rb

Overview

X509Cert object

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Java::DataConversion

#from_b64, #from_b64_mime, #from_hex, included, #logger, #to_b64, #to_b64_mime, #to_bin, #to_hex, #to_java_bytes, #to_str

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mtd, *args, &block) ⇒ Object



389
390
391
392
393
394
395
396
397
# File 'lib/ccrypto/java/ext/x509_cert.rb', line 389

def method_missing(mtd, *args, &block)
  if cert_info.respond_to?(mtd)
    cert_info.send(mtd, *args, &block)
  elsif @nativeX509.respond_to?(mtd)
    @nativeX509.send(mtd, *args, &block)
  else
    super
  end
end

Class Method Details

.from_pem(str) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/ccrypto/java/ext/x509_cert.rb', line 340

def self.from_pem(str)
  case str
  when String
    sstr = str.lines
    if sstr[0] =~ /BEGIN CERTIFICATE/
      certBin = from_b64_mime(sstr[1..-2].join)
      baos = java.io.ByteArrayOutputStream.new
      baos.write(certBin)
      to_java_cert(baos.toByteArray) 
    else
      raise Error, "Not a certificate PEM"
    end
  else 
    if str.to_java.is_a?(Java::byte[])
    else
      raise Error, "Unsupported input '#{str.class}' to read PEM format"
    end
  end
end

.from_storage(input, opts = { format: :b64 }) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/ccrypto/java/ext/x509_cert.rb', line 368

def self.from_storage(input, opts = { format: :b64 })
  defOpts = {
    jce_provider: Java::JCEProvider::DEFProv
  }

  defOpts.merge!(opts)

  case defOpts[:format]
  when :b64, :base64
    bin = from_b64(input)
  when :hex
    # hex
    bin = from_hex(input)
  else
    # binary
    bin = input
  end

  to_java_cert(bin, defOpts[:jce_provider])
end

.to_cert_from_file(path) ⇒ Object



332
333
334
335
336
337
338
# File 'lib/ccrypto/java/ext/x509_cert.rb', line 332

def self.to_cert_from_file(path)
  if File.exist?(path)
    to_java_cert(java.io.FileInputStream.new(path))
  else
    raise Error, "Given file to load '#{path}' does not exist"
  end
end

.to_java_cert(cert, prov = Java::JCEProvider::DEFProv) ⇒ Object

Raises:

  • (X509CertException)


421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/ccrypto/java/ext/x509_cert.rb', line 421

def self.to_java_cert(cert, prov = Java::JCEProvider::DEFProv)
  raise X509CertException, "Given certificate to convert to Java certificate object is empty" if is_empty?(cert) 

  case cert
  when org.bouncycastle.jcajce.provider.asymmetric.x509.X509CertificateObject
    #Ccrypto.logger(:x509_cert).debug "Given X509CertificateObject to convert"
    cert.to_java(java.security.cert.Certificate)

  when java.security.cert.Certificate
    #Ccrypto.logger(:x509_cert).debug "Given java certificate object to convert"
    cert
  when org.bouncycastle.cert.X509CertificateHolder
    #Ccrypto.logger(:x509_cert).debug "Given BC certificate holder to convert"
    org.bouncycastle.cert.jcajce.JcaX509CertificateConverter.new.get_certificate(cert)
    #cert.to_java_cert
  when Ccrypto::X509Cert
    #Ccrypto.logger(:x509_cert).debug "Given Ccrypto::X509Cert to convert"
    to_java_cert(cert.nativeX509)

  when String
    #Ccrypto.logger(:x509_cert).debug "Given String to convert"
    cf = java.security.cert.CertificateFactory.getInstance("X.509", prov)
    c = cf.generateCertificate(java.io.ByteArrayInputStream.new(cert))
    Ccrypto::X509Cert.new(c)

  else

    if cert.to_java.is_a?(::Java::byte[])
      #Ccrypto.logger(:x509_cert).debug "Given java byte array to convert"
      cf = java.security.cert.CertificateFactory.getInstance("X.509", prov)
      c = cf.generateCertificate(java.io.ByteArrayInputStream.new(cert)).to_java(java.security.cert.X509Certificate)
      Ccrypto::X509Cert.new(c)
    else
      raise X509CertException, "Unknown certificate type #{cert.class} for conversion"
    end

  end

end

Instance Method Details

#cert_infoObject

Raises:

  • (X509CertException)


461
462
463
464
465
466
467
468
# File 'lib/ccrypto/java/ext/x509_cert.rb', line 461

def cert_info
  raise X509CertException, "Certificate not given to extract cert info" if @nativeX509.nil?

  if @_cert_info.nil?
    @_cert_info = X509CertInfo.new(@nativeX509)
  end
  @_cert_info
end

#equal?(cert) ⇒ Boolean Also known as: equals?

Returns:

  • (Boolean)


399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/ccrypto/java/ext/x509_cert.rb', line 399

def equal?(cert)
  if cert.nil?
    if @nativeX509.nil?
      true
    else
      false
    end
  else

    tcert = self.class.to_java_cert(cert)
    lcert = self.class.to_java_cert(@nativeX509)

    tcert.encoded == @nativeX509.encoded
  end
end

#ownerObject



417
418
419
# File 'lib/ccrypto/java/ext/x509_cert.rb', line 417

def owner
  cert_info.owner 
end

#to_derObject



328
329
330
# File 'lib/ccrypto/java/ext/x509_cert.rb', line 328

def to_der
  @nativeX509.encoded
end

#to_pemObject



360
361
362
363
364
365
366
# File 'lib/ccrypto/java/ext/x509_cert.rb', line 360

def to_pem
  out = []
  out << "-----BEGIN CERTIFICATE-----"
  out << to_b64_mime(@nativeX509.encoded) 
  out << "-----END CERTIFICATE-----"
  out.join("\n")
end