Class: CSR
- Inherits:
-
Object
- Object
- CSR
- Defined in:
- lib/csr.rb,
lib/csr/version.rb
Constant Summary collapse
- VERSION =
'0.1.0'
Instance Attribute Summary collapse
-
#bits ⇒ Object
readonly
Returns the value of attribute bits.
-
#cipher ⇒ Object
readonly
Returns the value of attribute cipher.
-
#city ⇒ Object
readonly
Returns the value of attribute city.
-
#common_name ⇒ Object
readonly
Returns the value of attribute common_name.
-
#country ⇒ Object
readonly
Returns the value of attribute country.
-
#department ⇒ Object
readonly
Returns the value of attribute department.
-
#digest ⇒ Object
readonly
Returns the value of attribute digest.
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#organization ⇒ Object
readonly
Returns the value of attribute organization.
-
#passphrase ⇒ Object
readonly
Returns the value of attribute passphrase.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(country:, state:, city:, department:, organization:, common_name:, email:, bits: 4096, private_key: nil, passphrase: nil, cipher: nil, digest: nil) ⇒ CSR
constructor
A new instance of CSR.
- #pem ⇒ Object
- #private_key ⇒ Object
- #private_key_pem ⇒ Object
- #request ⇒ Object
- #save_to(directory, name) ⇒ Object
Constructor Details
#initialize(country:, state:, city:, department:, organization:, common_name:, email:, bits: 4096, private_key: nil, passphrase: nil, cipher: nil, digest: nil) ⇒ CSR
Returns a new instance of CSR.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/csr.rb', line 16 def initialize(country:, state:, city:, department:, organization:, common_name:, email:, bits: 4096, private_key: nil, passphrase: nil, cipher: nil, digest: nil) cipher ||= OpenSSL::Cipher::Cipher.new('des-ede3-cbc') digest ||= OpenSSL::Digest::SHA256.new @country = country @state = state @city = city @department = department @organization = organization @common_name = common_name @email = email @bits = bits @passphrase = passphrase @private_key = OpenSSL::PKey::RSA.new(private_key) if private_key @cipher = cipher @digest = digest end |
Instance Attribute Details
#bits ⇒ Object (readonly)
Returns the value of attribute bits.
6 7 8 |
# File 'lib/csr.rb', line 6 def bits @bits end |
#cipher ⇒ Object (readonly)
Returns the value of attribute cipher.
6 7 8 |
# File 'lib/csr.rb', line 6 def cipher @cipher end |
#city ⇒ Object (readonly)
Returns the value of attribute city.
6 7 8 |
# File 'lib/csr.rb', line 6 def city @city end |
#common_name ⇒ Object (readonly)
Returns the value of attribute common_name.
6 7 8 |
# File 'lib/csr.rb', line 6 def common_name @common_name end |
#country ⇒ Object (readonly)
Returns the value of attribute country.
6 7 8 |
# File 'lib/csr.rb', line 6 def country @country end |
#department ⇒ Object (readonly)
Returns the value of attribute department.
6 7 8 |
# File 'lib/csr.rb', line 6 def department @department end |
#digest ⇒ Object (readonly)
Returns the value of attribute digest.
6 7 8 |
# File 'lib/csr.rb', line 6 def digest @digest end |
#email ⇒ Object (readonly)
Returns the value of attribute email.
6 7 8 |
# File 'lib/csr.rb', line 6 def email @email end |
#organization ⇒ Object (readonly)
Returns the value of attribute organization.
6 7 8 |
# File 'lib/csr.rb', line 6 def organization @organization end |
#passphrase ⇒ Object (readonly)
Returns the value of attribute passphrase.
6 7 8 |
# File 'lib/csr.rb', line 6 def passphrase @passphrase end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
6 7 8 |
# File 'lib/csr.rb', line 6 def state @state end |
Class Method Details
.verify?(request_key, private_key, passphrase = nil) ⇒ Boolean
9 10 11 12 13 14 |
# File 'lib/csr.rb', line 9 def self.verify?(request_key, private_key, passphrase = nil) private_key = OpenSSL::PKey::RSA.new(private_key, passphrase) csr = OpenSSL::X509::Request.new(request_key) csr.public_key = private_key.public_key csr.verify(csr.public_key) end |
Instance Method Details
#pem ⇒ Object
77 78 79 |
# File 'lib/csr.rb', line 77 def pem request.to_pem end |
#private_key ⇒ Object
36 37 38 |
# File 'lib/csr.rb', line 36 def private_key @private_key ||= OpenSSL::PKey::RSA.new(bits) end |
#private_key_pem ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/csr.rb', line 66 def private_key_pem args = [] if passphrase args << cipher args << passphrase end private_key.to_pem(*args) end |
#request ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/csr.rb', line 40 def request @request ||= OpenSSL::X509::Request.new.tap do |request| request.version = 0 request.subject = OpenSSL::X509::Name.new([ ['C', country, OpenSSL::ASN1::PRINTABLESTRING], ['ST', state, OpenSSL::ASN1::PRINTABLESTRING], ['L', city, OpenSSL::ASN1::PRINTABLESTRING], ['O', organization, OpenSSL::ASN1::UTF8STRING], ['OU', department, OpenSSL::ASN1::UTF8STRING], ['CN', common_name, OpenSSL::ASN1::UTF8STRING], ['emailAddress', email, OpenSSL::ASN1::UTF8STRING] ]) request.public_key = private_key.public_key request.sign(private_key, digest) end end |
#save_to(directory, name) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/csr.rb', line 58 def save_to(directory, name) FileUtils.mkdir_p(directory) base_path = File.join(directory, name) save_private_key_to("#{base_path}.key") save_csr_to("#{base_path}.csr") true end |