Class: SecurityObject

Inherits:
Object
  • Object
show all
Defined in:
lib/action_mailer_x509/security_object.rb

Constant Summary collapse

ATTRS =
{     'C' => :country,
'ST' => :state,
'L' => :location,
'O' => :organization,
'OU' => :organizational_unit,
'CN' => :common_name,
'emailAddress' => :email}
IATTRS =
ATTRS.invert

Class Method Summary collapse

Class Method Details

.p12_certificate(params = {}) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/action_mailer_x509/security_object.rb', line 129

def p12_certificate(params = {})
  params.symbolize_keys!

  key, cert = signed_certificate(params)

  #root_key = OpenSSL::PKey::RSA.new 4096 # the CA's public/private key
  #root_ca = OpenSSL::X509::Certificate.new
  #root_ca.version = 2 # cf. RFC 5280 - to make it a "v3" certificate
  #root_ca.serial = 1
  #root_ca.subject = OpenSSL::X509::Name.parse "/DC=org/DC=ruby-lang/CN=Ruby CA"
  #root_ca.issuer = root_ca.subject # root CA's are "self-signed"
  #root_ca.public_key = root_key.public_key
  #root_ca.not_before = Time.now
  #root_ca.not_after = root_ca.not_before + 2 * 365 * 24 * 60 * 60 # 2 years validity
  #
  #ef = OpenSSL::X509::ExtensionFactory.new
  #ef.subject_certificate = root_ca
  #ef.issuer_certificate = root_ca
  #root_ca.add_extension(ef.create_extension('basicConstraints', 'CA:TRUE', true))
  #root_ca.add_extension(ef.create_extension('keyUsage', 'keyCertSign, cRLSign', true))
  #root_ca.add_extension(ef.create_extension('subjectKeyIdentifier', 'hash', false))
  #root_ca.add_extension(ef.create_extension('authorityKeyIdentifier', 'keyid:always', false))
  #root_ca.sign(root_key, OpenSSL::Digest::SHA256.new)
  #
  ## The next step is to create the end-entity certificate using the root CA
  ## certificate.
  ##
  #key = OpenSSL::PKey::RSA.new 4096
  #cert = OpenSSL::X509::Certificate.new
  #cert.version = 2
  #cert.serial = 2
  #cert.subject = OpenSSL::X509::Name.parse "/DC=org/DC=ruby-lang/CN=Ruby certificate"
  #cert.issuer = root_ca.subject # root CA is the issuer
  #cert.public_key = key.public_key
  #cert.not_before = Time.now
  #cert.not_after = cert.not_before + 1 * 365 * 24 * 60 * 60 # 1 years validity
  #
  #ef = OpenSSL::X509::ExtensionFactory.new
  #ef.subject_certificate = cert
  #ef.issuer_certificate = root_ca
  #cert.add_extension(ef.create_extension('keyUsage', 'digitalSignature', true))
  #cert.add_extension(ef.create_extension('subjectKeyIdentifier', 'hash', false))
  #cert.sign(root_key, OpenSSL::Digest::SHA256.new)

  p12 = OpenSSL::PKCS12.create(params[:password], params[:description] || 'My Name', key, cert)
  bytes = p12.to_der
  to_file(bytes, path) if params[:file]
  bytes
end

.self_signed_certificate(params = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/action_mailer_x509/security_object.rb', line 66

def self_signed_certificate(params = {})
  params.symbolize_keys!

  root_key, root_ca = certificate(params)
  root_ca.serial = 1

  ef = OpenSSL::X509::ExtensionFactory.new
  ef.subject_certificate = root_ca
  ef.issuer_certificate = root_ca
  root_ca.add_extension(ef.create_extension('basicConstraints', 'CA:TRUE', true))
  root_ca.add_extension(ef.create_extension('keyUsage', 'keyCertSign, cRLSign', true))
  root_ca.add_extension(ef.create_extension('subjectKeyIdentifier', 'hash', false))
  root_ca.add_extension(ef.create_extension('authorityKeyIdentifier', 'keyid:always', false))
  root_ca.sign(root_key, OpenSSL::Digest::SHA256.new)


  [ root_key, root_ca ]


#      key = OpenSSL::PKey::RSA.new(1024)
#      public_key = key.public_key
#
#      subject = "/C=BE/O=Test/OU=Test/CN=Test"
#
#      cert = OpenSSL::X509::Certificate.new
#      cert.subject = cert.issuer = OpenSSL::X509::Name.parse(subject)
#      cert.not_before = Time.now
#      cert.not_after = Time.now + 365 * 24 * 60 * 60
#      cert.public_key = public_key
#      cert.serial = 0x0
#      cert.version = 2
#
#      ef = OpenSSL::X509::ExtensionFactory.new
#      ef.subject_certificate = cert
#      ef.issuer_certificate = cert
#      cert.extensions = [
#          ef.create_extension("basicConstraints","CA:TRUE", true),
#          ef.create_extension("subjectKeyIdentifier", "hash"),
## ef.create_extension("keyUsage", "cRLSign,keyCertSign", true),
#      ]
#      cert.add_extension ef.create_extension("authorityKeyIdentifier",
#                                             "keyid:always,issuer:always")
#
#      cert.sign key, OpenSSL::Digest::SHA1.new
end

.signed_certificate(params = {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/action_mailer_x509/security_object.rb', line 112

def signed_certificate(params = {})
  params.symbolize_keys!
  root_key, root_ca = self_signed_certificate(params)
  key, cert = certificate(params, root_ca)
  cert.serial = 2

  ef = OpenSSL::X509::ExtensionFactory.new
  ef.subject_certificate = cert
  ef.issuer_certificate = root_ca
  cert.add_extension(ef.create_extension('keyUsage', 'digitalSignature', true))
  cert.add_extension(ef.create_extension('subjectKeyIdentifier', 'hash', false))
  cert.sign(root_key, OpenSSL::Digest::SHA256.new)

  [ key, cert ]
end