Class: AppInfo::Certificate
- Inherits:
-
Object
- Object
- AppInfo::Certificate
- Defined in:
- lib/app_info/certificate.rb
Overview
Certificate wrapper for OpenSSL::X509::Certifiate.
Class Method Summary collapse
-
.parse(data) ⇒ AppInfo::Certificate
Parse Raw data into X509 cerificate wrapper.
Instance Method Summary collapse
-
#algorithm ⇒ Object
return algorithm name of public key.
- #created_at ⇒ Time
-
#digest ⇒ Object
return algorithm digest.
- #expired? ⇒ Boolean
- #expired_at ⇒ Time
-
#fingerprint(name = :sha256, transform: :lower, delimiter: nil) ⇒ String
return fingerprint of certificate.
-
#format ⇒ String
Format always be :x509.
-
#initialize(cert) ⇒ Certificate
constructor
A new instance of Certificate.
-
#issuer(format: :raw) ⇒ Array, ...
return issuer from DN, similar to #subject.
-
#length ⇒ Integer
(also: #size)
return length of public key.
-
#raw ⇒ OpenSSL::X509::Certificate
Orginal OpenSSL X509 certificate.
-
#serial(base = 10, transform: :lower, prefix: nil) ⇒ String
return serial of certificate.
-
#subject(format: :raw) ⇒ Array, ...
return subject from DN, similar to #issuer.
-
#version(prefix: 'v', base: 1) ⇒ String
return version of certificate.
Constructor Details
#initialize(cert) ⇒ Certificate
Returns a new instance of Certificate.
16 17 18 |
# File 'lib/app_info/certificate.rb', line 16 def initialize(cert) @cert = cert end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (private)
168 169 170 |
# File 'lib/app_info/certificate.rb', line 168 def method_missing(method, *args, &block) @cert.send(method.to_sym, *args, &block) || super end |
Class Method Details
.parse(data) ⇒ AppInfo::Certificate
Parse Raw data into X509 cerificate wrapper
10 11 12 13 |
# File 'lib/app_info/certificate.rb', line 10 def self.parse(data) cert = OpenSSL::X509::Certificate.new(data) new(cert) end |
Instance Method Details
#algorithm ⇒ Object
return algorithm name of public key
112 113 114 115 116 117 118 119 |
# File 'lib/app_info/certificate.rb', line 112 def algorithm case public_key when OpenSSL::PKey::RSA then :rsa when OpenSSL::PKey::DSA then :dsa when OpenSSL::PKey::DH then :dh when OpenSSL::PKey::EC then :ec end end |
#created_at ⇒ Time
59 60 61 |
# File 'lib/app_info/certificate.rb', line 59 def created_at raw.not_before end |
#digest ⇒ Object
return algorithm digest
OpenSSL supported digests:
-blake2b512 -blake2s256 -md4 -md5 -md5-sha1 -mdc2 -ripemd -ripemd160 -rmd160 -sha1 -sha224 -sha256 -sha3-224 -sha3-256 -sha3-384 -sha3-512 -sha384 -sha512 -sha512-224 -sha512-256 -shake128 -shake256 -sm3 -ssl3-md5 -ssl3-sha1 -whirlpool
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/app_info/certificate.rb', line 91 def digest signature_algorithm = raw.signature_algorithm case signature_algorithm when /md5/ :md5 when /sha1/ :sha1 when /sha224/ :sha224 when /sha256/ :sha256 when /sha512/ :sha512 else # Android signature no need the others signature_algorithm.to_sym end end |
#expired? ⇒ Boolean
69 70 71 |
# File 'lib/app_info/certificate.rb', line 69 def expired? expired_at < Time.now.utc end |
#expired_at ⇒ Time
64 65 66 |
# File 'lib/app_info/certificate.rb', line 64 def expired_at raw.not_after end |
#fingerprint(name = :sha256, transform: :lower, delimiter: nil) ⇒ String
return fingerprint of certificate
138 139 140 141 142 143 144 145 146 |
# File 'lib/app_info/certificate.rb', line 138 def fingerprint(name = :sha256, transform: :lower, delimiter: nil) digest = OpenSSL::Digest.new(name.to_s.upcase) digest.update(raw.to_der) fingerprint = digest.to_s fingerprint = fingerprint.upcase if transform.to_sym == :upper return fingerprint unless delimiter fingerprint.scan(/../).join(delimiter) end |
#format ⇒ String
Returns format always be :x509.
74 75 76 |
# File 'lib/app_info/certificate.rb', line 74 def format :x509 end |
#issuer(format: :raw) ⇒ Array, ...
return issuer from DN, similar to #subject.
Example:
47 48 49 |
# File 'lib/app_info/certificate.rb', line 47 def issuer(format: :raw) convert_cert_name(raw.issuer, format: format) end |
#length ⇒ Integer Also known as: size
return length of public key
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/app_info/certificate.rb', line 124 def length case public_key when OpenSSL::PKey::RSA public_key.n.num_bits when OpenSSL::PKey::DSA, OpenSSL::PKey::DH public_key.p.num_bits when OpenSSL::PKey::EC raise NotImplementedError, "key length for #{public_key.inspect} not implemented" end end |
#raw ⇒ OpenSSL::X509::Certificate
Orginal OpenSSL X509 certificate
150 151 152 |
# File 'lib/app_info/certificate.rb', line 150 def raw @cert end |
#serial(base = 10, transform: :lower, prefix: nil) ⇒ String
return serial of certificate
33 34 35 36 37 38 39 |
# File 'lib/app_info/certificate.rb', line 33 def serial(base = 10, transform: :lower, prefix: nil) serial = raw.serial.to_s(base) serial = transform == :lower ? serial.downcase : serial.upcase return serial unless prefix "#{prefix}#{serial}" end |
#subject(format: :raw) ⇒ Array, ...
return subject from DN, similar to #issuer.
54 55 56 |
# File 'lib/app_info/certificate.rb', line 54 def subject(format: :raw) convert_cert_name(raw.subject, format: format) end |
#version(prefix: 'v', base: 1) ⇒ String
return version of certificate
24 25 26 |
# File 'lib/app_info/certificate.rb', line 24 def version(prefix: 'v', base: 1) "#{prefix}#{raw.version + base}" end |