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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
|
# coding: UTF-8
require 'rubygems/test_case'
unless defined?(OpenSSL::SSL) then
warn 'Skipping Gem::Security::Policy tests. openssl not found.'
end
class TestGemSecurityPolicy < Gem::TestCase
ALTERNATE_KEY = load_key 'alternate'
INVALID_KEY = load_key 'invalid'
CHILD_KEY = load_key 'child'
GRANDCHILD_KEY = load_key 'grandchild'
INVALIDCHILD_KEY = load_key 'invalidchild'
ALTERNATE_CERT = load_cert 'alternate'
CA_CERT = load_cert 'ca'
CHILD_CERT = load_cert 'child'
EXPIRED_CERT = load_cert 'expired'
FUTURE_CERT = load_cert 'future'
GRANDCHILD_CERT = load_cert 'grandchild'
INVALIDCHILD_CERT = load_cert 'invalidchild'
INVALID_ISSUER_CERT = load_cert 'invalid_issuer'
INVALID_SIGNER_CERT = load_cert 'invalid_signer'
WRONG_KEY_CERT = load_cert 'wrong_key'
def setup
super
@spec = quick_gem 'a' do |s|
s.description = 'π'
s.files = %w[lib/code.rb]
end
@sha1 = OpenSSL::Digest::SHA1
@trust_dir = Gem::Security.trust_dir.dir # HACK use the object
@no = Gem::Security::NoSecurity
@almost_no = Gem::Security::AlmostNoSecurity
@low = Gem::Security::LowSecurity
@medium = Gem::Security::MediumSecurity
@high = Gem::Security::HighSecurity
@chain = Gem::Security::Policy.new(
'Chain',
:verify_data => true,
:verify_signer => true,
:verify_chain => true,
:verify_root => false,
:only_trusted => false,
:only_signed => false
)
@root = Gem::Security::Policy.new(
'Root',
:verify_data => true,
:verify_signer => true,
:verify_chain => true,
:verify_root => true,
:only_trusted => false,
:only_signed => false
)
end
def test_check_data
data = digest 'hello'
signature = sign data
assert @almost_no.check_data(PUBLIC_KEY, @sha1, signature, data)
end
def test_check_data_invalid
data = digest 'hello'
signature = sign data
invalid = digest 'hello!'
e = assert_raises Gem::Security::Exception do
@almost_no.check_data PUBLIC_KEY, @sha1, signature, invalid
end
assert_equal 'invalid signature', e.message
end
def test_check_chain
chain = [PUBLIC_CERT, CHILD_CERT, GRANDCHILD_CERT]
assert @chain.check_chain chain, Time.now
end
def test_check_chain_empty_chain
e = assert_raises Gem::Security::Exception do
@chain.check_chain [], Time.now
end
assert_equal 'empty signing chain', e.message
end
def test_check_chain_invalid
chain = [PUBLIC_CERT, CHILD_CERT, INVALIDCHILD_CERT]
e = assert_raises Gem::Security::Exception do
@chain.check_chain chain, Time.now
end
assert_equal "invalid signing chain: " +
"certificate #{INVALIDCHILD_CERT.subject} " +
"was not issued by #{CHILD_CERT.subject}", e.message
end
def test_check_chain_no_chain
e = assert_raises Gem::Security::Exception do
@chain.check_chain nil, Time.now
end
assert_equal 'missing signing chain', e.message
end
def test_check_cert
assert @low.check_cert(PUBLIC_CERT, nil, Time.now)
end
def test_check_cert_expired
e = assert_raises Gem::Security::Exception do
@low.check_cert EXPIRED_CERT, nil, Time.now
end
assert_equal "certificate #{EXPIRED_CERT.subject} " +
"not valid after #{EXPIRED_CERT.not_after}",
e.message
end
def test_check_cert_future
e = assert_raises Gem::Security::Exception do
@low.check_cert FUTURE_CERT, nil, Time.now
end
assert_equal "certificate #{FUTURE_CERT.subject} " +
"not valid before #{FUTURE_CERT.not_before}",
e.message
end
def test_check_cert_invalid_issuer
e = assert_raises Gem::Security::Exception do
@low.check_cert INVALID_ISSUER_CERT, PUBLIC_CERT, Time.now
end
assert_equal "certificate #{INVALID_ISSUER_CERT.subject} " +
"was not issued by #{PUBLIC_CERT.subject}",
e.message
end
def test_check_cert_issuer
assert @low.check_cert(CHILD_CERT, PUBLIC_CERT, Time.now)
end
def test_check_cert_no_signer
e = assert_raises Gem::Security::Exception do
@high.check_cert(nil, nil, Time.now)
end
assert_equal 'missing signing certificate', e.message
end
def test_check_key
assert @almost_no.check_key(PUBLIC_CERT, PRIVATE_KEY)
end
def test_check_key_no_signer
assert @almost_no.check_key(nil, nil)
e = assert_raises Gem::Security::Exception do
@high.check_key(nil, nil)
end
assert_equal 'missing key or signature', e.message
end
def test_check_key_wrong_key
e = assert_raises Gem::Security::Exception do
@almost_no.check_key(PUBLIC_CERT, ALTERNATE_KEY)
end
assert_equal "certificate #{PUBLIC_CERT.subject} " +
"does not match the signing key", e.message
end
def test_check_root
chain = [PUBLIC_CERT, CHILD_CERT, INVALIDCHILD_CERT]
assert @chain.check_root chain, Time.now
end
def test_check_root_empty_chain
e = assert_raises Gem::Security::Exception do
@chain.check_root [], Time.now
end
assert_equal 'missing root certificate', e.message
end
def test_check_root_invalid_signer
chain = [INVALID_SIGNER_CERT]
e = assert_raises Gem::Security::Exception do
@chain.check_root chain, Time.now
end
assert_equal "certificate #{INVALID_SIGNER_CERT.subject} " +
"was not issued by #{INVALID_SIGNER_CERT.issuer}",
e.message
end
def test_check_root_not_self_signed
chain = [INVALID_ISSUER_CERT]
e = assert_raises Gem::Security::Exception do
@chain.check_root chain, Time.now
end
assert_equal "root certificate #{INVALID_ISSUER_CERT.subject} " +
"is not self-signed (issuer #{INVALID_ISSUER_CERT.issuer})",
e.message
end
def test_check_root_no_chain
e = assert_raises Gem::Security::Exception do
@chain.check_root nil, Time.now
end
assert_equal 'missing signing chain', e.message
end
def test_check_trust
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
assert @high.check_trust [PUBLIC_CERT], @sha1, @trust_dir
end
def test_check_trust_child
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
assert @high.check_trust [PUBLIC_CERT, CHILD_CERT], @sha1, @trust_dir
end
def test_check_trust_empty_chain
e = assert_raises Gem::Security::Exception do
@chain.check_trust [], @sha1, @trust_dir
end
assert_equal 'missing root certificate', e.message
end
def test_check_trust_mismatch
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
e = assert_raises Gem::Security::Exception do
@high.check_trust [WRONG_KEY_CERT], @sha1, @trust_dir
end
assert_equal "trusted root certificate #{PUBLIC_CERT.subject} checksum " +
"does not match signing root certificate checksum", e.message
end
def test_check_trust_no_chain
e = assert_raises Gem::Security::Exception do
@chain.check_trust nil, @sha1, @trust_dir
end
assert_equal 'missing signing chain', e.message
end
def test_check_trust_no_trust
e = assert_raises Gem::Security::Exception do
@high.check_trust [PUBLIC_CERT], @sha1, @trust_dir
end
assert_equal "root cert #{PUBLIC_CERT.subject} is not trusted", e.message
end
def test_check_trust_no_trust_child
e = assert_raises Gem::Security::Exception do
@high.check_trust [PUBLIC_CERT, CHILD_CERT], @sha1, @trust_dir
end
assert_equal "root cert #{PUBLIC_CERT.subject} is not trusted " +
"(root of signing cert #{CHILD_CERT.subject})", e.message
end
def test_subject
assert_equal 'email:nobody@example', @no.subject(PUBLIC_CERT)
assert_equal '/C=JP/O=JIN.GR.JP/OU=RRR/CN=CA', @no.subject(CA_CERT)
end
def test_verify
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
assert @almost_no.verify [PUBLIC_CERT], nil, *dummy_signatures
end
def test_verify_chain_signatures
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
assert @high.verify [PUBLIC_CERT], nil, *dummy_signatures
end
def test_verify_chain_key
@almost_no.verify [PUBLIC_CERT], PRIVATE_KEY, *dummy_signatures
end
def test_verify_no_digests
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
_, signatures = dummy_signatures
e = assert_raises Gem::Security::Exception do
@almost_no.verify [PUBLIC_CERT], nil, {}, signatures
end
assert_equal 'no digests provided (probable bug)', e.message
end
def test_verify_no_digests_no_security
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
_, signatures = dummy_signatures
e = assert_raises Gem::Security::Exception do
@no.verify [PUBLIC_CERT], nil, {}, signatures
end
assert_equal 'missing digest for 0', e.message
end
def test_verify_no_signatures
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
digests, = dummy_signatures
use_ui @ui do
@no.verify [PUBLIC_CERT], nil, digests, {}, 'some_gem'
end
assert_match "WARNING: some_gem is not signed\n", @ui.error
assert_raises Gem::Security::Exception do
@high.verify [PUBLIC_CERT], nil, digests, {}
end
end
def test_verify_no_signatures_no_digests
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
use_ui @ui do
@no.verify [PUBLIC_CERT], nil, {}, {}, 'some_gem'
end
assert_empty @ui.output
assert_empty @ui.error
end
def test_verify_not_enough_signatures
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
digests, signatures = dummy_signatures
data = digest 'goodbye'
signatures[1] = PRIVATE_KEY.sign @sha1.new, data.digest
e = assert_raises Gem::Security::Exception do
@almost_no.verify [PUBLIC_CERT], nil, digests, signatures
end
assert_equal 'missing digest for 1', e.message
end
def test_verify_no_trust
digests, signatures = dummy_signatures
use_ui @ui do
@low.verify [PUBLIC_CERT], nil, digests, signatures, 'some_gem'
end
assert_equal "WARNING: email:nobody@example is not trusted for some_gem\n",
@ui.error
assert_raises Gem::Security::Exception do
@medium.verify [PUBLIC_CERT], nil, digests, signatures
end
end
def test_verify_wrong_digest_type
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
sha512 = OpenSSL::Digest::SHA512
data = sha512.new
data << 'hello'
digests = { 'SHA512' => { 0 => data } }
signature = PRIVATE_KEY.sign sha512.new, data.digest
signatures = { 0 => signature }
e = assert_raises Gem::Security::Exception do
@almost_no.verify [PUBLIC_CERT], nil, digests, signatures
end
assert_equal 'no digests provided (probable bug)', e.message
end
def test_verify_signatures_chain
@spec.cert_chain = [PUBLIC_CERT, CHILD_CERT]
assert @chain.verify_signatures @spec, *dummy_signatures(CHILD_KEY)
end
def test_verify_signatures_data
@spec.cert_chain = [PUBLIC_CERT]
@almost_no.verify_signatures @spec, *dummy_signatures
end
def test_verify_signatures_root
@spec.cert_chain = [PUBLIC_CERT, CHILD_CERT]
assert @root.verify_signatures @spec, *dummy_signatures(CHILD_KEY)
end
def test_verify_signatures_signer
@spec.cert_chain = [PUBLIC_CERT]
assert @low.verify_signatures @spec, *dummy_signatures
end
def test_verify_signatures_trust
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
@spec.cert_chain = [PUBLIC_CERT]
assert @high.verify_signatures @spec, *dummy_signatures
end
def test_verify_signatures
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
@spec.cert_chain = [PUBLIC_CERT.to_s]
metadata_gz = Gem.gzip @spec.to_yaml
package = Gem::Package.new 'nonexistent.gem'
package.checksums['SHA1'] = {}
s = StringIO.new metadata_gz
def s.full_name() 'metadata.gz' end
digests = package.digest s
metadata_gz_digest = digests['SHA1']['metadata.gz']
signatures = {}
signatures['metadata.gz'] =
PRIVATE_KEY.sign @sha1.new, metadata_gz_digest.digest
assert @high.verify_signatures @spec, digests, signatures
end
def test_verify_signatures_missing
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
@spec.cert_chain = [PUBLIC_CERT.to_s]
metadata_gz = Gem.gzip @spec.to_yaml
package = Gem::Package.new 'nonexistent.gem'
package.checksums['SHA1'] = {}
s = StringIO.new metadata_gz
def s.full_name() 'metadata.gz' end
digests = package.digest s
digests['SHA1']['data.tar.gz'] = OpenSSL::Digest.new 'SHA1', 'hello'
metadata_gz_digest = digests['SHA1']['metadata.gz']
signatures = {}
signatures['metadata.gz'] =
PRIVATE_KEY.sign @sha1.new, metadata_gz_digest.digest
e = assert_raises Gem::Security::Exception do
@high.verify_signatures @spec, digests, signatures
end
assert_equal 'missing signature for data.tar.gz', e.message
end
def test_verify_signatures_none
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
@spec.cert_chain = [PUBLIC_CERT.to_s]
metadata_gz = Gem.gzip @spec.to_yaml
package = Gem::Package.new 'nonexistent.gem'
package.checksums['SHA1'] = {}
s = StringIO.new metadata_gz
def s.full_name() 'metadata.gz' end
digests = package.digest s
digests['SHA1']['data.tar.gz'] = OpenSSL::Digest.new 'SHA1', 'hello'
assert_raises Gem::Security::Exception do
@high.verify_signatures @spec, digests, {}
end
end
def digest data
digester = @sha1.new
digester << data
digester
end
def sign data, key = PRIVATE_KEY
key.sign @sha1.new, data.digest
end
def dummy_signatures key = PRIVATE_KEY
data = digest 'hello'
digests = { 'SHA1' => { 0 => data } }
signatures = { 0 => sign(data, key) }
return digests, signatures
end
end if defined?(OpenSSL::SSL)
|