2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
require 'rubygems/test_case'
|
|
|
|
|
2020-12-08 02:33:39 -05:00
|
|
|
unless Gem::HAVE_OPENSSL
|
2013-09-14 04:59:02 -04:00
|
|
|
warn 'Skipping Gem::Security::Policy tests. openssl not found.'
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
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'
|
2013-09-14 04:59:02 -04:00
|
|
|
CA_CERT = load_cert 'ca'
|
2012-11-29 01:52:18 -05:00
|
|
|
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
|
|
|
|
|
2020-06-28 14:39:26 -04:00
|
|
|
@digest = OpenSSL::Digest.new Gem::Security::DIGEST_NAME
|
2012-11-29 01:52:18 -05:00
|
|
|
@trust_dir = Gem::Security.trust_dir.dir # HACK use the object
|
|
|
|
|
2013-02-07 00:56:53 -05:00
|
|
|
@no = Gem::Security::NoSecurity
|
2012-11-29 01:52:18 -05:00
|
|
|
@almost_no = Gem::Security::AlmostNoSecurity
|
|
|
|
@low = Gem::Security::LowSecurity
|
2013-02-07 21:58:19 -05:00
|
|
|
@medium = Gem::Security::MediumSecurity
|
2012-11-29 01:52:18 -05:00
|
|
|
@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
|
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
assert @almost_no.check_data(PUBLIC_KEY, @digest, signature, data)
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_check_data_invalid
|
|
|
|
data = digest 'hello'
|
|
|
|
|
|
|
|
signature = sign data
|
|
|
|
|
|
|
|
invalid = digest 'hello!'
|
|
|
|
|
|
|
|
e = assert_raises Gem::Security::Exception do
|
2017-10-07 21:32:18 -04:00
|
|
|
@almost_no.check_data PUBLIC_KEY, @digest, signature, invalid
|
2012-11-29 01:52:18 -05:00
|
|
|
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
|
|
|
|
|
2013-02-07 21:58:19 -05:00
|
|
|
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
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
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
|
|
|
|
|
2012-12-06 00:26:46 -05:00
|
|
|
assert_equal "invalid signing chain: " +
|
|
|
|
"certificate #{INVALIDCHILD_CERT.subject} " +
|
2012-11-29 01:52:18 -05:00
|
|
|
"was not issued by #{CHILD_CERT.subject}", e.message
|
|
|
|
end
|
|
|
|
|
2013-02-07 21:58:19 -05:00
|
|
|
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
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
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
|
|
|
|
|
2012-12-06 00:26:46 -05:00
|
|
|
assert_equal "certificate #{EXPIRED_CERT.subject} " +
|
2012-11-29 01:52:18 -05:00
|
|
|
"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
|
|
|
|
|
2012-12-06 00:26:46 -05:00
|
|
|
assert_equal "certificate #{FUTURE_CERT.subject} " +
|
2012-11-29 01:52:18 -05:00
|
|
|
"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
|
|
|
|
|
2012-12-06 00:26:46 -05:00
|
|
|
assert_equal "certificate #{INVALID_ISSUER_CERT.subject} " +
|
2012-11-29 01:52:18 -05:00
|
|
|
"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
|
|
|
|
|
2013-02-07 21:58:19 -05:00
|
|
|
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
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def test_check_key
|
|
|
|
assert @almost_no.check_key(PUBLIC_CERT, PRIVATE_KEY)
|
|
|
|
end
|
|
|
|
|
2013-02-07 21:58:19 -05:00
|
|
|
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
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def test_check_key_wrong_key
|
|
|
|
e = assert_raises Gem::Security::Exception do
|
|
|
|
@almost_no.check_key(PUBLIC_CERT, ALTERNATE_KEY)
|
|
|
|
end
|
|
|
|
|
2012-12-06 00:26:46 -05:00
|
|
|
assert_equal "certificate #{PUBLIC_CERT.subject} " +
|
2012-11-29 01:52:18 -05:00
|
|
|
"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
|
|
|
|
|
2013-02-07 21:58:19 -05:00
|
|
|
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
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def test_check_root_invalid_signer
|
|
|
|
chain = [INVALID_SIGNER_CERT]
|
|
|
|
|
|
|
|
e = assert_raises Gem::Security::Exception do
|
|
|
|
@chain.check_root chain, Time.now
|
|
|
|
end
|
|
|
|
|
2012-12-06 00:26:46 -05:00
|
|
|
assert_equal "certificate #{INVALID_SIGNER_CERT.subject} " +
|
2012-11-29 01:52:18 -05:00
|
|
|
"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
|
|
|
|
|
2012-12-06 00:26:46 -05:00
|
|
|
assert_equal "root certificate #{INVALID_ISSUER_CERT.subject} " +
|
2012-11-29 01:52:18 -05:00
|
|
|
"is not self-signed (issuer #{INVALID_ISSUER_CERT.issuer})",
|
|
|
|
e.message
|
|
|
|
end
|
|
|
|
|
2013-02-07 21:58:19 -05:00
|
|
|
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
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def test_check_trust
|
|
|
|
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
|
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
assert @high.check_trust [PUBLIC_CERT], @digest, @trust_dir
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_check_trust_child
|
|
|
|
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
|
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
assert @high.check_trust [PUBLIC_CERT, CHILD_CERT], @digest, @trust_dir
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
2013-02-07 21:58:19 -05:00
|
|
|
def test_check_trust_empty_chain
|
|
|
|
e = assert_raises Gem::Security::Exception do
|
2017-10-07 21:32:18 -04:00
|
|
|
@chain.check_trust [], @digest, @trust_dir
|
2013-02-07 21:58:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 'missing root certificate', e.message
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def test_check_trust_mismatch
|
|
|
|
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
|
|
|
|
|
|
|
|
e = assert_raises Gem::Security::Exception do
|
2017-10-07 21:32:18 -04:00
|
|
|
@high.check_trust [WRONG_KEY_CERT], @digest, @trust_dir
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
2012-12-06 00:26:46 -05:00
|
|
|
assert_equal "trusted root certificate #{PUBLIC_CERT.subject} checksum " +
|
2012-11-29 01:52:18 -05:00
|
|
|
"does not match signing root certificate checksum", e.message
|
|
|
|
end
|
|
|
|
|
2013-02-07 21:58:19 -05:00
|
|
|
def test_check_trust_no_chain
|
|
|
|
e = assert_raises Gem::Security::Exception do
|
2017-10-07 21:32:18 -04:00
|
|
|
@chain.check_trust nil, @digest, @trust_dir
|
2013-02-07 21:58:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 'missing signing chain', e.message
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def test_check_trust_no_trust
|
|
|
|
e = assert_raises Gem::Security::Exception do
|
2017-10-07 21:32:18 -04:00
|
|
|
@high.check_trust [PUBLIC_CERT], @digest, @trust_dir
|
2012-11-29 01:52:18 -05:00
|
|
|
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
|
2017-10-07 21:32:18 -04:00
|
|
|
@high.check_trust [PUBLIC_CERT, CHILD_CERT], @digest, @trust_dir
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
2012-12-06 00:26:46 -05:00
|
|
|
assert_equal "root cert #{PUBLIC_CERT.subject} is not trusted " +
|
2012-11-29 01:52:18 -05:00
|
|
|
"(root of signing cert #{CHILD_CERT.subject})", e.message
|
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def test_subject
|
|
|
|
assert_equal 'email:nobody@example', @no.subject(PUBLIC_CERT)
|
2019-05-27 10:44:15 -04:00
|
|
|
assert_equal '/C=JP/ST=Tokyo/O=RubyGemsTest/CN=CA', @no.subject(CA_CERT)
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def test_verify
|
|
|
|
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
|
|
|
|
|
2013-02-07 00:56:53 -05:00
|
|
|
assert @almost_no.verify [PUBLIC_CERT], nil, *dummy_signatures
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_verify_chain_signatures
|
|
|
|
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
|
|
|
|
|
2013-02-07 00:56:53 -05:00
|
|
|
assert @high.verify [PUBLIC_CERT], nil, *dummy_signatures
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_verify_chain_key
|
2013-02-07 00:56:53 -05:00
|
|
|
@almost_no.verify [PUBLIC_CERT], PRIVATE_KEY, *dummy_signatures
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
2013-02-07 00:56:53 -05:00
|
|
|
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
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
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
|
2014-09-13 23:30:02 -04:00
|
|
|
@high.verify [PUBLIC_CERT], nil, digests, {}
|
2013-09-14 04:59:02 -04:00
|
|
|
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
|
|
|
|
|
2013-02-07 00:56:53 -05:00
|
|
|
def test_verify_not_enough_signatures
|
|
|
|
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
|
|
|
|
|
|
|
|
digests, signatures = dummy_signatures
|
|
|
|
|
|
|
|
data = digest 'goodbye'
|
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
signatures[1] = PRIVATE_KEY.sign @digest.new, data.digest
|
2013-02-07 00:56:53 -05:00
|
|
|
|
|
|
|
e = assert_raises Gem::Security::Exception do
|
|
|
|
@almost_no.verify [PUBLIC_CERT], nil, digests, signatures
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-02-07 00:56:53 -05:00
|
|
|
assert_equal 'missing digest for 1', e.message
|
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
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
|
|
|
|
|
2013-02-07 00:56:53 -05:00
|
|
|
def test_verify_wrong_digest_type
|
|
|
|
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
|
|
|
|
|
2020-06-28 14:39:26 -04:00
|
|
|
data = OpenSSL::Digest.new('SHA512')
|
2013-02-07 00:56:53 -05:00
|
|
|
data << 'hello'
|
|
|
|
|
|
|
|
digests = { 'SHA512' => { 0 => data } }
|
2020-06-28 14:39:26 -04:00
|
|
|
signature = PRIVATE_KEY.sign 'sha512', data.digest
|
2013-02-07 00:56:53 -05:00
|
|
|
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
|
2012-11-29 01:52:18 -05:00
|
|
|
@spec.cert_chain = [PUBLIC_CERT, CHILD_CERT]
|
|
|
|
|
2013-02-07 00:56:53 -05:00
|
|
|
assert @chain.verify_signatures @spec, *dummy_signatures(CHILD_KEY)
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_verify_signatures_data
|
|
|
|
@spec.cert_chain = [PUBLIC_CERT]
|
|
|
|
|
2013-02-07 00:56:53 -05:00
|
|
|
@almost_no.verify_signatures @spec, *dummy_signatures
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_verify_signatures_root
|
|
|
|
@spec.cert_chain = [PUBLIC_CERT, CHILD_CERT]
|
|
|
|
|
2013-02-07 00:56:53 -05:00
|
|
|
assert @root.verify_signatures @spec, *dummy_signatures(CHILD_KEY)
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_verify_signatures_signer
|
|
|
|
@spec.cert_chain = [PUBLIC_CERT]
|
|
|
|
|
2013-02-07 00:56:53 -05:00
|
|
|
assert @low.verify_signatures @spec, *dummy_signatures
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_verify_signatures_trust
|
|
|
|
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
|
|
|
|
|
|
|
|
@spec.cert_chain = [PUBLIC_CERT]
|
|
|
|
|
2013-02-07 00:56:53 -05:00
|
|
|
assert @high.verify_signatures @spec, *dummy_signatures
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_verify_signatures
|
|
|
|
Gem::Security.trust_dir.trust_cert PUBLIC_CERT
|
|
|
|
|
|
|
|
@spec.cert_chain = [PUBLIC_CERT.to_s]
|
|
|
|
|
2018-05-17 21:39:13 -04:00
|
|
|
metadata_gz = Gem::Util.gzip @spec.to_yaml
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
package = Gem::Package.new 'nonexistent.gem'
|
2017-10-07 21:32:18 -04:00
|
|
|
package.checksums[Gem::Security::DIGEST_NAME] = {}
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
s = StringIO.new metadata_gz
|
|
|
|
def s.full_name() 'metadata.gz' end
|
|
|
|
|
|
|
|
digests = package.digest s
|
2017-10-07 21:32:18 -04:00
|
|
|
metadata_gz_digest = digests[Gem::Security::DIGEST_NAME]['metadata.gz']
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
signatures = {}
|
|
|
|
signatures['metadata.gz'] =
|
2017-10-07 21:32:18 -04:00
|
|
|
PRIVATE_KEY.sign @digest.new, metadata_gz_digest.digest
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
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]
|
|
|
|
|
2018-05-17 21:39:13 -04:00
|
|
|
metadata_gz = Gem::Util.gzip @spec.to_yaml
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
package = Gem::Package.new 'nonexistent.gem'
|
2017-10-07 21:32:18 -04:00
|
|
|
package.checksums[Gem::Security::DIGEST_NAME] = {}
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
s = StringIO.new metadata_gz
|
|
|
|
def s.full_name() 'metadata.gz' end
|
|
|
|
|
|
|
|
digests = package.digest s
|
2020-06-28 14:39:26 -04:00
|
|
|
digests[Gem::Security::DIGEST_NAME]['data.tar.gz'] = @digest.hexdigest 'hello'
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
metadata_gz_digest = digests[Gem::Security::DIGEST_NAME]['metadata.gz']
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
signatures = {}
|
|
|
|
signatures['metadata.gz'] =
|
2017-10-07 21:32:18 -04:00
|
|
|
PRIVATE_KEY.sign @digest.new, metadata_gz_digest.digest
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
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]
|
|
|
|
|
2018-05-17 21:39:13 -04:00
|
|
|
metadata_gz = Gem::Util.gzip @spec.to_yaml
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
package = Gem::Package.new 'nonexistent.gem'
|
2017-10-07 21:32:18 -04:00
|
|
|
package.checksums[Gem::Security::DIGEST_NAME] = {}
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
s = StringIO.new metadata_gz
|
|
|
|
def s.full_name() 'metadata.gz' end
|
|
|
|
|
|
|
|
digests = package.digest s
|
2020-06-28 14:39:26 -04:00
|
|
|
digests[Gem::Security::DIGEST_NAME]['data.tar.gz'] = @digest.hexdigest 'hello'
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
assert_raises Gem::Security::Exception do
|
2014-09-13 23:30:02 -04:00
|
|
|
@high.verify_signatures @spec, digests, {}
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def digest(data)
|
2017-10-07 21:32:18 -04:00
|
|
|
digester = @digest.new
|
2012-11-29 01:52:18 -05:00
|
|
|
digester << data
|
|
|
|
digester
|
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def sign(data, key = PRIVATE_KEY)
|
2017-10-07 21:32:18 -04:00
|
|
|
key.sign @digest.new, data.digest
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def dummy_signatures(key = PRIVATE_KEY)
|
2013-02-07 00:56:53 -05:00
|
|
|
data = digest 'hello'
|
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
digests = { Gem::Security::DIGEST_NAME => { 0 => data } }
|
2013-02-07 00:56:53 -05:00
|
|
|
signatures = { 0 => sign(data, key) }
|
|
|
|
|
|
|
|
return digests, signatures
|
|
|
|
end
|
2020-12-08 02:33:39 -05:00
|
|
|
end if Gem::HAVE_OPENSSL
|