Class: CSR

Inherits:
Object
  • Object
show all
Defined in:
lib/csr.rb,
lib/csr/version.rb

Constant Summary collapse

VERSION =
'0.1.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#bitsObject (readonly)

Returns the value of attribute bits.



6
7
8
# File 'lib/csr.rb', line 6

def bits
  @bits
end

#cipherObject (readonly)

Returns the value of attribute cipher.



6
7
8
# File 'lib/csr.rb', line 6

def cipher
  @cipher
end

#cityObject (readonly)

Returns the value of attribute city.



6
7
8
# File 'lib/csr.rb', line 6

def city
  @city
end

#common_nameObject (readonly)

Returns the value of attribute common_name.



6
7
8
# File 'lib/csr.rb', line 6

def common_name
  @common_name
end

#countryObject (readonly)

Returns the value of attribute country.



6
7
8
# File 'lib/csr.rb', line 6

def country
  @country
end

#departmentObject (readonly)

Returns the value of attribute department.



6
7
8
# File 'lib/csr.rb', line 6

def department
  @department
end

#digestObject (readonly)

Returns the value of attribute digest.



6
7
8
# File 'lib/csr.rb', line 6

def digest
  @digest
end

#emailObject (readonly)

Returns the value of attribute email.



6
7
8
# File 'lib/csr.rb', line 6

def email
  @email
end

#organizationObject (readonly)

Returns the value of attribute organization.



6
7
8
# File 'lib/csr.rb', line 6

def organization
  @organization
end

#passphraseObject (readonly)

Returns the value of attribute passphrase.



6
7
8
# File 'lib/csr.rb', line 6

def passphrase
  @passphrase
end

#stateObject (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

Returns:

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

#pemObject



77
78
79
# File 'lib/csr.rb', line 77

def pem
  request.to_pem
end

#private_keyObject



36
37
38
# File 'lib/csr.rb', line 36

def private_key
  @private_key ||= OpenSSL::PKey::RSA.new(bits)
end

#private_key_pemObject



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

#requestObject



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