2010-12-15 14:50:00 -05:00
|
|
|
require_relative 'utils'
|
2011-05-13 21:33:58 -04:00
|
|
|
require 'base64'
|
2004-12-05 11:36:26 -05:00
|
|
|
|
|
|
|
if defined?(OpenSSL)
|
|
|
|
|
|
|
|
class OpenSSL::TestPKeyRSA < Test::Unit::TestCase
|
|
|
|
def test_padding
|
|
|
|
key = OpenSSL::PKey::RSA.new(512, 3)
|
|
|
|
|
|
|
|
# Need right size for raw mode
|
|
|
|
plain0 = "x" * (512/8)
|
|
|
|
cipher = key.private_encrypt(plain0, OpenSSL::PKey::RSA::NO_PADDING)
|
|
|
|
plain1 = key.public_decrypt(cipher, OpenSSL::PKey::RSA::NO_PADDING)
|
|
|
|
assert_equal(plain0, plain1)
|
|
|
|
|
|
|
|
# Need smaller size for pkcs1 mode
|
|
|
|
plain0 = "x" * (512/8 - 11)
|
|
|
|
cipher1 = key.private_encrypt(plain0, OpenSSL::PKey::RSA::PKCS1_PADDING)
|
|
|
|
plain1 = key.public_decrypt(cipher1, OpenSSL::PKey::RSA::PKCS1_PADDING)
|
|
|
|
assert_equal(plain0, plain1)
|
|
|
|
|
|
|
|
cipherdef = key.private_encrypt(plain0) # PKCS1_PADDING is default
|
|
|
|
plain1 = key.public_decrypt(cipherdef)
|
|
|
|
assert_equal(plain0, plain1)
|
|
|
|
assert_equal(cipher1, cipherdef)
|
|
|
|
|
|
|
|
# Failure cases
|
|
|
|
assert_raise(ArgumentError){ key.private_encrypt() }
|
|
|
|
assert_raise(ArgumentError){ key.private_encrypt("hi", 1, nil) }
|
|
|
|
assert_raise(OpenSSL::PKey::RSAError){ key.private_encrypt(plain0, 666) }
|
|
|
|
end
|
2005-09-18 18:56:11 -04:00
|
|
|
|
|
|
|
def test_private
|
|
|
|
key = OpenSSL::PKey::RSA.new(512, 3)
|
|
|
|
assert(key.private?)
|
|
|
|
key2 = OpenSSL::PKey::RSA.new(key.to_der)
|
|
|
|
assert(key2.private?)
|
|
|
|
key3 = key.public_key
|
|
|
|
assert(!key3.private?)
|
|
|
|
key4 = OpenSSL::PKey::RSA.new(key3.to_der)
|
|
|
|
assert(!key4.private?)
|
|
|
|
end
|
2011-04-06 02:14:05 -04:00
|
|
|
|
|
|
|
def test_new
|
|
|
|
key = OpenSSL::PKey::RSA.new 512
|
|
|
|
pem = key.public_key.to_pem
|
|
|
|
OpenSSL::PKey::RSA.new pem
|
|
|
|
assert_equal([], OpenSSL.errors)
|
|
|
|
end
|
2011-05-13 21:33:58 -04:00
|
|
|
|
2011-11-04 01:12:31 -04:00
|
|
|
def test_new_exponent_default
|
|
|
|
assert_equal(65537, OpenSSL::PKey::RSA.new(512).e)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_new_with_exponent
|
|
|
|
1.upto(30) do |idx|
|
|
|
|
e = (2 ** idx) + 1
|
|
|
|
key = OpenSSL::PKey::RSA.new(512, e)
|
|
|
|
assert_equal(e, key.e)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-01 03:42:29 -04:00
|
|
|
def test_new_break
|
|
|
|
assert_nil(OpenSSL::PKey::RSA.new(1024) { break })
|
|
|
|
assert_raise(RuntimeError) do
|
|
|
|
OpenSSL::PKey::RSA.new(1024) { raise }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-18 18:50:35 -04:00
|
|
|
def test_sign_verify
|
2011-06-12 16:39:38 -04:00
|
|
|
key = OpenSSL::TestUtils::TEST_KEY_RSA1024
|
2011-05-18 18:50:35 -04:00
|
|
|
digest = OpenSSL::Digest::SHA1.new
|
|
|
|
data = 'Sign me!'
|
|
|
|
sig = key.sign(digest, data)
|
|
|
|
assert(key.verify(digest, sig, data))
|
|
|
|
end
|
|
|
|
|
2014-04-15 20:51:18 -04:00
|
|
|
def test_sign_verify_memory_leak
|
|
|
|
bug9743 = '[ruby-core:62038] [Bug #9743]'
|
|
|
|
assert_no_memory_leak(%w[-ropenssl], <<-PREP, <<-CODE, bug9743, rss: true)
|
|
|
|
data = 'Sign me!'
|
|
|
|
digest = OpenSSL::Digest::SHA512.new
|
|
|
|
pkey = OpenSSL::PKey::RSA.new(2048)
|
|
|
|
signature = pkey.sign(digest, data)
|
|
|
|
pub_key = pkey.public_key
|
|
|
|
PREP
|
|
|
|
20_000.times {
|
|
|
|
pub_key.verify(digest, signature, data)
|
|
|
|
}
|
|
|
|
CODE
|
|
|
|
|
|
|
|
assert_no_memory_leak(%w[-ropenssl], <<-PREP, <<-CODE, bug9743, rss: true)
|
|
|
|
data = 'Sign me!'
|
|
|
|
digest = OpenSSL::Digest::SHA512.new
|
|
|
|
pkey = OpenSSL::PKey::RSA.new(2048)
|
|
|
|
signature = pkey.sign(digest, data)
|
|
|
|
pub_key = pkey.public_key
|
|
|
|
PREP
|
|
|
|
20_000.times {
|
|
|
|
begin
|
|
|
|
pub_key.verify(digest, signature, 1)
|
|
|
|
rescue TypeError
|
|
|
|
end
|
|
|
|
}
|
|
|
|
CODE
|
|
|
|
end
|
|
|
|
|
2011-05-18 18:50:35 -04:00
|
|
|
def test_digest_state_irrelevant_sign
|
2011-06-12 16:39:38 -04:00
|
|
|
key = OpenSSL::TestUtils::TEST_KEY_RSA1024
|
2011-05-18 18:50:35 -04:00
|
|
|
digest1 = OpenSSL::Digest::SHA1.new
|
|
|
|
digest2 = OpenSSL::Digest::SHA1.new
|
|
|
|
data = 'Sign me!'
|
|
|
|
digest1 << 'Change state of digest1'
|
|
|
|
sig1 = key.sign(digest1, data)
|
|
|
|
sig2 = key.sign(digest2, data)
|
|
|
|
assert_equal(sig1, sig2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_digest_state_irrelevant_verify
|
2011-06-12 16:39:38 -04:00
|
|
|
key = OpenSSL::TestUtils::TEST_KEY_RSA1024
|
2011-05-18 18:50:35 -04:00
|
|
|
digest1 = OpenSSL::Digest::SHA1.new
|
|
|
|
digest2 = OpenSSL::Digest::SHA1.new
|
|
|
|
data = 'Sign me!'
|
|
|
|
sig = key.sign(digest1, data)
|
|
|
|
digest1.reset
|
|
|
|
digest1 << 'Change state of digest1'
|
|
|
|
assert(key.verify(digest1, sig, data))
|
|
|
|
assert(key.verify(digest2, sig, data))
|
|
|
|
end
|
|
|
|
|
2011-05-13 21:33:58 -04:00
|
|
|
def test_read_RSAPublicKey
|
|
|
|
modulus = 10664264882656732240315063514678024569492171560814833397008094754351396057398262071307709191731289492697968568138092052265293364132872019762410446076526351
|
|
|
|
exponent = 65537
|
|
|
|
seq = OpenSSL::ASN1::Sequence.new([OpenSSL::ASN1::Integer.new(modulus), OpenSSL::ASN1::Integer.new(exponent)])
|
|
|
|
key = OpenSSL::PKey::RSA.new(seq.to_der)
|
|
|
|
assert(key.public?)
|
|
|
|
assert(!key.private?)
|
|
|
|
assert_equal(modulus, key.n)
|
|
|
|
assert_equal(exponent, key.e)
|
|
|
|
assert_equal(nil, key.d)
|
|
|
|
assert_equal(nil, key.p)
|
|
|
|
assert_equal(nil, key.q)
|
2011-06-22 04:41:08 -04:00
|
|
|
assert_equal([], OpenSSL.errors)
|
2011-05-13 21:33:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_read_RSA_PUBKEY
|
|
|
|
modulus = 10664264882656732240315063514678024569492171560814833397008094754351396057398262071307709191731289492697968568138092052265293364132872019762410446076526351
|
|
|
|
exponent = 65537
|
2011-05-13 23:14:33 -04:00
|
|
|
algo = OpenSSL::ASN1::ObjectId.new('rsaEncryption')
|
2011-05-13 21:33:58 -04:00
|
|
|
null_params = OpenSSL::ASN1::Null.new(nil)
|
2011-05-13 23:14:33 -04:00
|
|
|
algo_id = OpenSSL::ASN1::Sequence.new ([algo, null_params])
|
2011-05-13 21:33:58 -04:00
|
|
|
pub_key = OpenSSL::ASN1::Sequence.new([OpenSSL::ASN1::Integer.new(modulus), OpenSSL::ASN1::Integer.new(exponent)])
|
|
|
|
seq = OpenSSL::ASN1::Sequence.new([algo_id, OpenSSL::ASN1::BitString.new(pub_key.to_der)])
|
|
|
|
key = OpenSSL::PKey::RSA.new(seq.to_der)
|
|
|
|
assert(key.public?)
|
|
|
|
assert(!key.private?)
|
|
|
|
assert_equal(modulus, key.n)
|
|
|
|
assert_equal(exponent, key.e)
|
|
|
|
assert_equal(nil, key.d)
|
|
|
|
assert_equal(nil, key.p)
|
|
|
|
assert_equal(nil, key.q)
|
2011-06-22 04:41:08 -04:00
|
|
|
assert_equal([], OpenSSL.errors)
|
2011-05-13 21:33:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_read_RSAPublicKey_pem
|
|
|
|
modulus = 9416340886363418692990906464787534854462163316648195510702927337693641649864839352187127240942127674615733815606532506566068276485089353644309497938966061
|
|
|
|
exponent = 65537
|
|
|
|
pem = <<-EOF
|
|
|
|
-----BEGIN RSA PUBLIC KEY-----
|
|
|
|
MEgCQQCzyh2RIZK62E2PbTWqUljD+K23XR9AGBKNtXjal6WD2yRGcLqzPJLNCa60
|
|
|
|
AudJR1JobbIbDJrQu6AXnWh5k/YtAgMBAAE=
|
|
|
|
-----END RSA PUBLIC KEY-----
|
|
|
|
EOF
|
|
|
|
key = OpenSSL::PKey::RSA.new(pem)
|
|
|
|
assert(key.public?)
|
|
|
|
assert(!key.private?)
|
|
|
|
assert_equal(modulus, key.n)
|
|
|
|
assert_equal(exponent, key.e)
|
|
|
|
assert_equal(nil, key.d)
|
|
|
|
assert_equal(nil, key.p)
|
|
|
|
assert_equal(nil, key.q)
|
2011-06-22 04:41:08 -04:00
|
|
|
assert_equal([], OpenSSL.errors)
|
2011-05-13 21:33:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_read_RSA_PUBKEY_pem
|
|
|
|
modulus = 9416340886363418692990906464787534854462163316648195510702927337693641649864839352187127240942127674615733815606532506566068276485089353644309497938966061
|
|
|
|
exponent = 65537
|
|
|
|
pem = <<-EOF
|
|
|
|
-----BEGIN PUBLIC KEY-----
|
|
|
|
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALPKHZEhkrrYTY9tNapSWMP4rbdd
|
|
|
|
H0AYEo21eNqXpYPbJEZwurM8ks0JrrQC50lHUmhtshsMmtC7oBedaHmT9i0C
|
|
|
|
AwEAAQ==
|
|
|
|
-----END PUBLIC KEY-----
|
|
|
|
EOF
|
|
|
|
key = OpenSSL::PKey::RSA.new(pem)
|
|
|
|
assert(key.public?)
|
|
|
|
assert(!key.private?)
|
|
|
|
assert_equal(modulus, key.n)
|
|
|
|
assert_equal(exponent, key.e)
|
|
|
|
assert_equal(nil, key.d)
|
|
|
|
assert_equal(nil, key.p)
|
|
|
|
assert_equal(nil, key.q)
|
2011-06-22 04:41:08 -04:00
|
|
|
assert_equal([], OpenSSL.errors)
|
2011-05-13 21:33:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_export_format_is_RSA_PUBKEY
|
|
|
|
key = OpenSSL::PKey::RSA.new(512)
|
|
|
|
asn1 = OpenSSL::ASN1.decode(key.public_key.to_der)
|
|
|
|
check_PUBKEY(asn1, key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_export_format_is_RSA_PUBKEY_pem
|
|
|
|
key = OpenSSL::PKey::RSA.new(512)
|
|
|
|
pem = key.public_key.to_pem
|
2011-05-13 23:14:33 -04:00
|
|
|
pem.gsub!(/^-+(\w|\s)+-+$/, "") # eliminate --------BEGIN...-------
|
2011-05-13 21:33:58 -04:00
|
|
|
asn1 = OpenSSL::ASN1.decode(Base64.decode64(pem))
|
|
|
|
check_PUBKEY(asn1, key)
|
|
|
|
end
|
|
|
|
|
2011-06-12 16:39:38 -04:00
|
|
|
def test_read_private_key_der
|
|
|
|
der = OpenSSL::TestUtils::TEST_KEY_RSA1024.to_der
|
|
|
|
key = OpenSSL::PKey.read(der)
|
|
|
|
assert(key.private?)
|
|
|
|
assert_equal(der, key.to_der)
|
2011-06-22 04:41:08 -04:00
|
|
|
assert_equal([], OpenSSL.errors)
|
2011-06-12 16:39:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_read_private_key_pem
|
|
|
|
pem = OpenSSL::TestUtils::TEST_KEY_RSA1024.to_pem
|
|
|
|
key = OpenSSL::PKey.read(pem)
|
|
|
|
assert(key.private?)
|
|
|
|
assert_equal(pem, key.to_pem)
|
2011-06-22 04:41:08 -04:00
|
|
|
assert_equal([], OpenSSL.errors)
|
2011-06-12 16:39:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_read_public_key_der
|
|
|
|
der = OpenSSL::TestUtils::TEST_KEY_RSA1024.public_key.to_der
|
|
|
|
key = OpenSSL::PKey.read(der)
|
|
|
|
assert(!key.private?)
|
|
|
|
assert_equal(der, key.to_der)
|
2011-06-22 04:41:08 -04:00
|
|
|
assert_equal([], OpenSSL.errors)
|
2011-06-12 16:39:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_read_public_key_pem
|
|
|
|
pem = OpenSSL::TestUtils::TEST_KEY_RSA1024.public_key.to_pem
|
|
|
|
key = OpenSSL::PKey.read(pem)
|
|
|
|
assert(!key.private?)
|
|
|
|
assert_equal(pem, key.to_pem)
|
2011-06-22 04:41:08 -04:00
|
|
|
assert_equal([], OpenSSL.errors)
|
2011-06-12 16:39:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_read_private_key_pem_pw
|
|
|
|
pem = OpenSSL::TestUtils::TEST_KEY_RSA1024.to_pem(OpenSSL::Cipher.new('AES-128-CBC'), 'secret')
|
|
|
|
#callback form for password
|
|
|
|
key = OpenSSL::PKey.read(pem) do
|
|
|
|
'secret'
|
|
|
|
end
|
|
|
|
assert(key.private?)
|
|
|
|
# pass password directly
|
|
|
|
key = OpenSSL::PKey.read(pem, 'secret')
|
|
|
|
assert(key.private?)
|
|
|
|
#omit pem equality check, will be different due to cipher iv
|
2011-06-22 04:41:08 -04:00
|
|
|
assert_equal([], OpenSSL.errors)
|
2011-12-20 01:06:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_read_private_key_pem_pw_exception
|
|
|
|
pem = OpenSSL::TestUtils::TEST_KEY_RSA1024.to_pem(OpenSSL::Cipher.new('AES-128-CBC'), 'secret')
|
|
|
|
# it raises an ArgumentError from PEM reading. The exception raised inside are ignored for now.
|
|
|
|
assert_raise(ArgumentError) do
|
|
|
|
OpenSSL::PKey.read(pem) do
|
|
|
|
raise RuntimeError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal([], OpenSSL.errors)
|
2011-06-12 16:39:38 -04:00
|
|
|
end
|
|
|
|
|
2012-06-09 21:23:21 -04:00
|
|
|
def test_export_password_length
|
|
|
|
key = OpenSSL::TestUtils::TEST_KEY_RSA1024
|
|
|
|
assert_raise(OpenSSL::OpenSSLError) do
|
|
|
|
key.export(OpenSSL::Cipher.new('AES-128-CBC'), 'sec')
|
|
|
|
end
|
|
|
|
pem = key.export(OpenSSL::Cipher.new('AES-128-CBC'), 'secr')
|
|
|
|
assert(pem)
|
|
|
|
end
|
|
|
|
|
2011-05-13 21:33:58 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def check_PUBKEY(asn1, key)
|
|
|
|
assert_equal(OpenSSL::ASN1::SEQUENCE, asn1.tag)
|
|
|
|
assert_equal(2, asn1.value.size)
|
|
|
|
seq = asn1.value
|
|
|
|
assert_equal(OpenSSL::ASN1::SEQUENCE, seq[0].tag)
|
|
|
|
assert_equal(2, seq[0].value.size)
|
|
|
|
algo_id = seq[0].value
|
|
|
|
assert_equal(OpenSSL::ASN1::OBJECT, algo_id[0].tag)
|
|
|
|
assert_equal('rsaEncryption', algo_id[0].value)
|
|
|
|
assert_equal(OpenSSL::ASN1::NULL, algo_id[1].tag)
|
|
|
|
assert_equal(nil, algo_id[1].value)
|
|
|
|
assert_equal(OpenSSL::ASN1::BIT_STRING, seq[1].tag)
|
|
|
|
assert_equal(0, seq[1].unused_bits)
|
|
|
|
pub_key = OpenSSL::ASN1.decode(seq[1].value)
|
|
|
|
assert_equal(OpenSSL::ASN1::SEQUENCE, pub_key.tag)
|
|
|
|
assert_equal(2, pub_key.value.size)
|
|
|
|
assert_equal(OpenSSL::ASN1::INTEGER, pub_key.value[0].tag)
|
|
|
|
assert_equal(key.n, pub_key.value[0].value)
|
|
|
|
assert_equal(OpenSSL::ASN1::INTEGER, pub_key.value[1].tag)
|
|
|
|
assert_equal(key.e, pub_key.value[1].value)
|
2011-06-22 04:41:08 -04:00
|
|
|
assert_equal([], OpenSSL.errors)
|
2011-05-13 21:33:58 -04:00
|
|
|
end
|
|
|
|
|
2004-12-05 11:36:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|