-
Notifications
You must be signed in to change notification settings - Fork 4
/
scep_controller.rb
267 lines (232 loc) · 8.35 KB
/
scep_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
class ScepController < ApplicationController
include OpenSSL::ASN1
MessageType = "2.16.840.1.113733.1.9.2"
PkiStatus = "2.16.840.1.113733.1.9.3"
FailInfo = "2.16.840.1.113733.1.9.4"
SenderNonce = "2.16.840.1.113733.1.9.5"
RecipientNonce = "2.16.840.1.113733.1.9.6"
TransId = "2.16.840.1.113733.1.9.7"
ExtensionReq = "2.16.840.1.113733.1.9.8"
MessageTypes = {'PKCSReq' => 19, 'CertRep' => 3, 'GetCertInitial' => 20, 'GetCert' => 21, 'GetCRL' => 22}
PkiStatuses = {'SUCCESS' => 0, 'FAILURE' => 2, 'PENDING' => 3}
FailInfos = {'badAlg' => 0, 'badMessageCheck' => 1, 'badRequest' => 2, 'badTime' => 3, 'badCertId' => 4}
skip_before_filter :verify_authenticity_token
def get_ca_caps
send_data "POSTPKIOperation\nSHA-1", :content_type => 'text/plain'
end
def get_ca_cert
send_data SCEPCert.to_der, :content_type => 'application/x-x509-ca-cert'
end
def pki_operation
raw_message = request.raw_post
# Ruby's PKCS7 doesn't have the ability to grab signed attributes
# so I'm parsing the ASN1 structure manually
asn1 = decode raw_message
raw_signed_attributes = asn1.value[1].value.first.value[4].first.value[3].value
signed_attributes = raw_signed_attributes.inject({}) do |hash, raw_signed_attribute|
hash.merge({raw_signed_attribute.value.first.value => raw_signed_attribute.value.last.value.first.value})
end
# Grab the relevant signed attributes to complete the transaction
message_type_id = signed_attributes[MessageType]
transaction_id = signed_attributes[TransId]
sender_nonce = signed_attributes[SenderNonce].unpack('H*')[0]
# Skip the verification, this bit is self signed anyway
# Security is provided by encrypting the inner envelope with the CACaps and
# verification before giving out the challengePassword in devices_controller
message = OpenSSL::PKCS7.new raw_message
message.verify nil, SCEPStore, nil, OpenSSL::PKCS7::NOVERIFY
sender_serial = message.certificates.first.serial
recipient_key = message.certificates.first.public_key
message_envelope = OpenSSL::PKCS7.new(message.data)
# Decrypt the inner envelope, parse it as a CSR, and pull the challenge out
x509_request = OpenSSL::X509::Request.new message_envelope.decrypt(SCEPKey, SCEPCert, nil)
device_id, challenge_password = x509_request.attributes.select{|a| a.oid == 'challengePassword' }.first.value.value.first.value.split(':')
device = Device.find(device_id)
# Errors, if I cared more I'd actually use SCEP failures rather than raise a 500
raise ArgumentError, 'No matching device' unless device
raise ArgumentError, 'Device secret doesnt match' unless device.secret == challenge_password
raise ArgumentError, 'X509 verification failed' unless x509_request.verify(x509_request.public_key)
new_serial = Random.rand(2**(159))
# Sign the CSR
new_cert = OpenSSL::X509::Certificate.new
new_cert.serial = new_serial
new_cert.version = 2
new_cert.not_before = Time.now
new_cert.not_after = 2.years.from_now
new_cert.subject = x509_request.subject
new_cert.public_key = x509_request.public_key
new_cert.issuer = SCEPCert.subject
extension_factory = OpenSSL::X509::ExtensionFactory.new
extension_factory.subject_certificate = new_cert
extension_factory.subject_request = x509_request
extension_factory.issuer_certificate = SCEPCert
new_cert.add_extension extension_factory.create_extension('keyUsage', 'digitalSignature,keyEncipherment')
new_cert.sign SCEPKey, OpenSSL::Digest::SHA1.new
# Save the serial to the database
device.secret_digest = nil
device.certificate_serial = new_serial.to_s(16)
device.save!
# Form the PKCS7 degenerate, needed to transmit the certificate
degenerate = Sequence.new([
ObjectId.new('1.2.840.113549.1.7.2'),
ASN1Data.new([
Sequence.new([
Integer.new(1),
Set.new([
]),
Sequence.new([
ObjectId.new('1.2.840.113549.1.7.1')
]),
ASN1Data.new([
decode(new_cert.to_der)
], 0, :CONTEXT_SPECIFIC),
ASN1Data.new([
], 1, :CONTEXT_SPECIFIC),
Set.new([
])
])
], 0, :CONTEXT_SPECIFIC)
])
sha1 = OpenSSL::Digest::SHA1.new
des = OpenSSL::Cipher::Cipher.new("des-ede3-cbc")
des.encrypt
content_encryption_key = des.random_key
content_encryption_iv = des.random_iv
des.key = content_encryption_key
des.iv = content_encryption_iv
encrypted_payload = des.update(degenerate.to_der) + des.final
recipient_information = Sequence.new([
decode(message.certificates.first.subject.to_der),
Integer.new(sender_serial.to_i)
])
# Wrap the degenerate in an envelope
envelope = Sequence.new([
ObjectId.new('1.2.840.113549.1.7.3'),
ASN1Data.new([
Sequence.new([
Integer.new(0),
Set.new([
Sequence.new([
Integer.new(0),
recipient_information,
Sequence.new([
ObjectId.new('1.2.840.113549.1.1.1'),
Null.new(nil)
]),
OctetString.new( recipient_key.public_encrypt content_encryption_key )
])
]),
Sequence.new([
ObjectId.new('1.2.840.113549.1.7.1'),
Sequence.new([
ObjectId.new('1.2.840.113549.3.7'),
OctetString.new( content_encryption_iv )
]),
ASN1Data.new( encrypted_payload, 0, :CONTEXT_SPECIFIC)
])
])
], 0, :CONTEXT_SPECIFIC)
])
text = envelope.to_der
message_digest = sha1.digest text
now = Time.now
# Form the signed-attributes for SCEP
signed_attributes = ASN1Data.new([
Sequence.new([
ObjectId.new('1.2.840.113549.1.9.3'),
Set.new([
ObjectId.new('1.2.840.113549.1.7.1')
])
]),
Sequence.new([
ObjectId.new('1.2.840.113549.1.9.5'),
Set.new([
UTCTime.new(now)
])
]),
Sequence.new([
ObjectId.new('1.2.840.113549.1.9.4'),
Set.new([
OctetString.new( message_digest )
])
]),
Sequence.new([
ObjectId.new( MessageType ),
Set.new([
PrintableString.new( MessageTypes['CertRep'].to_s )
])
]),
Sequence.new([
ObjectId.new( PkiStatus ),
Set.new([
PrintableString.new( PkiStatuses['SUCCESS'].to_s )
])
]),
Sequence.new([
ObjectId.new(RecipientNonce),
Set.new([
OctetString.new( [sender_nonce].pack('H*') )
])
]),
Sequence.new([
ObjectId.new(SenderNonce),
Set.new([
OctetString.new( [SecureRandom.hex].pack('H*') )
])
]),
Sequence.new([
ObjectId.new( TransId ),
Set.new([
PrintableString.new( transaction_id )
])
])
], 0, :CONTEXT_SPECIFIC)
signed_attributes_digest = SCEPKey.private_encrypt Sequence.new([
Sequence.new([
ObjectId.new('1.3.14.3.2.26'),
Null.new(nil)
]),
OctetString.new( sha1.digest Set.new(signed_attributes.value[0..-1]).to_der )
]).to_der
# Wrap in a PKI message
pki_message = Sequence.new([
ObjectId.new('1.2.840.113549.1.7.2'),
ASN1Data.new([Sequence.new([
Integer.new(1),
Set.new([
Sequence.new([
ObjectId.new('1.3.14.3.2.26'),
Null.new(nil)
])
]),
Sequence.new([
ObjectId.new('1.2.840.113549.1.7.1'),
ASN1Data.new([
OctetString.new( text )
], 0, :CONTEXT_SPECIFIC)
]),
Set.new([
Sequence.new([
Integer.new(1),
Sequence.new([
decode(SCEPCert.subject.to_der),
Integer.new( SCEPCert.serial )
]),
Sequence.new([
ObjectId.new('1.3.14.3.2.26'),
Null.new(nil)
]),
signed_attributes,
Sequence.new([
ObjectId.new('1.2.840.113549.1.1.1'),
Null.new(nil)
]),
OctetString.new( signed_attributes_digest )
]),
])
])
], 0, :CONTEXT_SPECIFIC)
])
send_data pki_message.to_der, :content_type => 'application/x-pki-message'
end
end