Class: AppInfo::Certificate

Inherits:
Object
  • Object
show all
Defined in:
lib/app_info/certificate.rb

Overview

Certificate wrapper for OpenSSL::X509::Certifiate.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cert) ⇒ Certificate

Returns a new instance of Certificate.

Parameters:

  • certificate (OpenSSL::X509::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

Parameters:

  • certificate (String)

    raw data

Returns:



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

#algorithmObject

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_atTime

Returns:

  • (Time)


59
60
61
# File 'lib/app_info/certificate.rb', line 59

def created_at
  raw.not_before
end

#digestObject

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

Returns:

  • (Boolean)


69
70
71
# File 'lib/app_info/certificate.rb', line 69

def expired?
  expired_at < Time.now.utc
end

#expired_atTime

Returns:

  • (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

Returns:

  • (String)


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

#formatString

Returns format always be :x509.

Returns:

  • (String)

    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:

Parameters:

  • format (Symbol) (defaults to: :raw)

    avaiables in ‘:to_a`, `:to_s` and `:raw`

Returns:

  • (Array, String, OpenSSL::X509::Name)

    the object converted into the expected format.



47
48
49
# File 'lib/app_info/certificate.rb', line 47

def issuer(format: :raw)
  convert_cert_name(raw.issuer, format: format)
end

#lengthInteger Also known as: size

return length of public key

Returns:

  • (Integer)

Raises:

  • NotImplementedError



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

#rawOpenSSL::X509::Certificate

Orginal OpenSSL X509 certificate

Returns:

  • (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

Parameters:

  • base (Integer) (defaults to: 10)
  • transform (Symbol) (defaults to: :lower)

    avaiables in :lower, :upper

  • prefix (String) (defaults to: nil)

Returns:

  • (String)

    serial



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.

Parameters:

  • format (Symbol) (defaults to: :raw)

    avaiables in ‘:to_a`, `:to_s` and `:raw`

Returns:

  • (Array, String, OpenSSL::X509::Name)

    the object converted into the expected format.



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

Parameters:

  • prefix (String) (defaults to: 'v')
  • base (Integer) (defaults to: 1)

Returns:

  • (String)

    version



24
25
26
# File 'lib/app_info/certificate.rb', line 24

def version(prefix: 'v', base: 1)
  "#{prefix}#{raw.version + base}"
end