зеркало из https://github.com/github/ruby.git
[ruby/openssl] test/openssl/utils: remove dup_public helper method
It uses deprecated PKey::{RSA,DSA,DH}#set_* methods, which will not work with OpenSSL 3.0. The same can easily be achieved using PKey#public_to_der regardless of the key kind. https://github.com/ruby/openssl/commit/7b66eaa2db
This commit is contained in:
Родитель
e353bcd111
Коммит
37632a0ac6
|
@ -40,12 +40,14 @@ class OpenSSL::TestPKeyDH < OpenSSL::PKeyTestCase
|
||||||
|
|
||||||
def test_DHparams
|
def test_DHparams
|
||||||
dh1024 = Fixtures.pkey("dh1024")
|
dh1024 = Fixtures.pkey("dh1024")
|
||||||
|
dh1024params = dh1024.public_key
|
||||||
|
|
||||||
asn1 = OpenSSL::ASN1::Sequence([
|
asn1 = OpenSSL::ASN1::Sequence([
|
||||||
OpenSSL::ASN1::Integer(dh1024.p),
|
OpenSSL::ASN1::Integer(dh1024.p),
|
||||||
OpenSSL::ASN1::Integer(dh1024.g)
|
OpenSSL::ASN1::Integer(dh1024.g)
|
||||||
])
|
])
|
||||||
key = OpenSSL::PKey::DH.new(asn1.to_der)
|
key = OpenSSL::PKey::DH.new(asn1.to_der)
|
||||||
assert_same_dh dup_public(dh1024), key
|
assert_same_dh dh1024params, key
|
||||||
|
|
||||||
pem = <<~EOF
|
pem = <<~EOF
|
||||||
-----BEGIN DH PARAMETERS-----
|
-----BEGIN DH PARAMETERS-----
|
||||||
|
@ -55,9 +57,9 @@ class OpenSSL::TestPKeyDH < OpenSSL::PKeyTestCase
|
||||||
-----END DH PARAMETERS-----
|
-----END DH PARAMETERS-----
|
||||||
EOF
|
EOF
|
||||||
key = OpenSSL::PKey::DH.new(pem)
|
key = OpenSSL::PKey::DH.new(pem)
|
||||||
assert_same_dh dup_public(dh1024), key
|
assert_same_dh dh1024params, key
|
||||||
key = OpenSSL::PKey.read(pem)
|
key = OpenSSL::PKey.read(pem)
|
||||||
assert_same_dh dup_public(dh1024), key
|
assert_same_dh dh1024params, key
|
||||||
|
|
||||||
assert_equal asn1.to_der, dh1024.to_der
|
assert_equal asn1.to_der, dh1024.to_der
|
||||||
assert_equal pem, dh1024.export
|
assert_equal pem, dh1024.export
|
||||||
|
|
|
@ -138,6 +138,8 @@ class OpenSSL::TestPKeyDSA < OpenSSL::PKeyTestCase
|
||||||
|
|
||||||
def test_PUBKEY
|
def test_PUBKEY
|
||||||
dsa512 = Fixtures.pkey("dsa512")
|
dsa512 = Fixtures.pkey("dsa512")
|
||||||
|
dsa512pub = OpenSSL::PKey::DSA.new(dsa512.public_to_der)
|
||||||
|
|
||||||
asn1 = OpenSSL::ASN1::Sequence([
|
asn1 = OpenSSL::ASN1::Sequence([
|
||||||
OpenSSL::ASN1::Sequence([
|
OpenSSL::ASN1::Sequence([
|
||||||
OpenSSL::ASN1::ObjectId("DSA"),
|
OpenSSL::ASN1::ObjectId("DSA"),
|
||||||
|
@ -153,7 +155,7 @@ class OpenSSL::TestPKeyDSA < OpenSSL::PKeyTestCase
|
||||||
])
|
])
|
||||||
key = OpenSSL::PKey::DSA.new(asn1.to_der)
|
key = OpenSSL::PKey::DSA.new(asn1.to_der)
|
||||||
assert_not_predicate key, :private?
|
assert_not_predicate key, :private?
|
||||||
assert_same_dsa dup_public(dsa512), key
|
assert_same_dsa dsa512pub, key
|
||||||
|
|
||||||
pem = <<~EOF
|
pem = <<~EOF
|
||||||
-----BEGIN PUBLIC KEY-----
|
-----BEGIN PUBLIC KEY-----
|
||||||
|
@ -166,10 +168,15 @@ class OpenSSL::TestPKeyDSA < OpenSSL::PKeyTestCase
|
||||||
-----END PUBLIC KEY-----
|
-----END PUBLIC KEY-----
|
||||||
EOF
|
EOF
|
||||||
key = OpenSSL::PKey::DSA.new(pem)
|
key = OpenSSL::PKey::DSA.new(pem)
|
||||||
assert_same_dsa dup_public(dsa512), key
|
assert_same_dsa dsa512pub, key
|
||||||
|
|
||||||
assert_equal asn1.to_der, dup_public(dsa512).to_der
|
assert_equal asn1.to_der, key.to_der
|
||||||
assert_equal pem, dup_public(dsa512).export
|
assert_equal pem, key.export
|
||||||
|
|
||||||
|
assert_equal asn1.to_der, dsa512.public_to_der
|
||||||
|
assert_equal asn1.to_der, key.public_to_der
|
||||||
|
assert_equal pem, dsa512.public_to_pem
|
||||||
|
assert_equal pem, key.public_to_pem
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_read_DSAPublicKey_pem
|
def test_read_DSAPublicKey_pem
|
||||||
|
|
|
@ -210,6 +210,8 @@ class OpenSSL::TestEC < OpenSSL::PKeyTestCase
|
||||||
|
|
||||||
def test_PUBKEY
|
def test_PUBKEY
|
||||||
p256 = Fixtures.pkey("p256")
|
p256 = Fixtures.pkey("p256")
|
||||||
|
p256pub = OpenSSL::PKey::EC.new(p256.public_to_der)
|
||||||
|
|
||||||
asn1 = OpenSSL::ASN1::Sequence([
|
asn1 = OpenSSL::ASN1::Sequence([
|
||||||
OpenSSL::ASN1::Sequence([
|
OpenSSL::ASN1::Sequence([
|
||||||
OpenSSL::ASN1::ObjectId("id-ecPublicKey"),
|
OpenSSL::ASN1::ObjectId("id-ecPublicKey"),
|
||||||
|
@ -221,7 +223,7 @@ class OpenSSL::TestEC < OpenSSL::PKeyTestCase
|
||||||
])
|
])
|
||||||
key = OpenSSL::PKey::EC.new(asn1.to_der)
|
key = OpenSSL::PKey::EC.new(asn1.to_der)
|
||||||
assert_not_predicate key, :private?
|
assert_not_predicate key, :private?
|
||||||
assert_same_ec dup_public(p256), key
|
assert_same_ec p256pub, key
|
||||||
|
|
||||||
pem = <<~EOF
|
pem = <<~EOF
|
||||||
-----BEGIN PUBLIC KEY-----
|
-----BEGIN PUBLIC KEY-----
|
||||||
|
@ -230,10 +232,15 @@ class OpenSSL::TestEC < OpenSSL::PKeyTestCase
|
||||||
-----END PUBLIC KEY-----
|
-----END PUBLIC KEY-----
|
||||||
EOF
|
EOF
|
||||||
key = OpenSSL::PKey::EC.new(pem)
|
key = OpenSSL::PKey::EC.new(pem)
|
||||||
assert_same_ec dup_public(p256), key
|
assert_same_ec p256pub, key
|
||||||
|
|
||||||
assert_equal asn1.to_der, dup_public(p256).to_der
|
assert_equal asn1.to_der, key.to_der
|
||||||
assert_equal pem, dup_public(p256).export
|
assert_equal pem, key.export
|
||||||
|
|
||||||
|
assert_equal asn1.to_der, p256.public_to_der
|
||||||
|
assert_equal asn1.to_der, key.public_to_der
|
||||||
|
assert_equal pem, p256.public_to_pem
|
||||||
|
assert_equal pem, key.public_to_pem
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_ec_group
|
def test_ec_group
|
||||||
|
|
|
@ -201,7 +201,7 @@ class OpenSSL::TestPKeyRSA < OpenSSL::PKeyTestCase
|
||||||
|
|
||||||
def test_encrypt_decrypt
|
def test_encrypt_decrypt
|
||||||
rsapriv = Fixtures.pkey("rsa-1")
|
rsapriv = Fixtures.pkey("rsa-1")
|
||||||
rsapub = dup_public(rsapriv)
|
rsapub = OpenSSL::PKey.read(rsapriv.public_to_der)
|
||||||
|
|
||||||
# Defaults to PKCS #1 v1.5
|
# Defaults to PKCS #1 v1.5
|
||||||
raw = "data"
|
raw = "data"
|
||||||
|
@ -216,7 +216,7 @@ class OpenSSL::TestPKeyRSA < OpenSSL::PKeyTestCase
|
||||||
|
|
||||||
def test_encrypt_decrypt_legacy
|
def test_encrypt_decrypt_legacy
|
||||||
rsapriv = Fixtures.pkey("rsa-1")
|
rsapriv = Fixtures.pkey("rsa-1")
|
||||||
rsapub = dup_public(rsapriv)
|
rsapub = OpenSSL::PKey.read(rsapriv.public_to_der)
|
||||||
|
|
||||||
# Defaults to PKCS #1 v1.5
|
# Defaults to PKCS #1 v1.5
|
||||||
raw = "data"
|
raw = "data"
|
||||||
|
@ -346,13 +346,15 @@ class OpenSSL::TestPKeyRSA < OpenSSL::PKeyTestCase
|
||||||
|
|
||||||
def test_RSAPublicKey
|
def test_RSAPublicKey
|
||||||
rsa1024 = Fixtures.pkey("rsa1024")
|
rsa1024 = Fixtures.pkey("rsa1024")
|
||||||
|
rsa1024pub = OpenSSL::PKey::RSA.new(rsa1024.public_to_der)
|
||||||
|
|
||||||
asn1 = OpenSSL::ASN1::Sequence([
|
asn1 = OpenSSL::ASN1::Sequence([
|
||||||
OpenSSL::ASN1::Integer(rsa1024.n),
|
OpenSSL::ASN1::Integer(rsa1024.n),
|
||||||
OpenSSL::ASN1::Integer(rsa1024.e)
|
OpenSSL::ASN1::Integer(rsa1024.e)
|
||||||
])
|
])
|
||||||
key = OpenSSL::PKey::RSA.new(asn1.to_der)
|
key = OpenSSL::PKey::RSA.new(asn1.to_der)
|
||||||
assert_not_predicate key, :private?
|
assert_not_predicate key, :private?
|
||||||
assert_same_rsa dup_public(rsa1024), key
|
assert_same_rsa rsa1024pub, key
|
||||||
|
|
||||||
pem = <<~EOF
|
pem = <<~EOF
|
||||||
-----BEGIN RSA PUBLIC KEY-----
|
-----BEGIN RSA PUBLIC KEY-----
|
||||||
|
@ -362,11 +364,13 @@ class OpenSSL::TestPKeyRSA < OpenSSL::PKeyTestCase
|
||||||
-----END RSA PUBLIC KEY-----
|
-----END RSA PUBLIC KEY-----
|
||||||
EOF
|
EOF
|
||||||
key = OpenSSL::PKey::RSA.new(pem)
|
key = OpenSSL::PKey::RSA.new(pem)
|
||||||
assert_same_rsa dup_public(rsa1024), key
|
assert_same_rsa rsa1024pub, key
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_PUBKEY
|
def test_PUBKEY
|
||||||
rsa1024 = Fixtures.pkey("rsa1024")
|
rsa1024 = Fixtures.pkey("rsa1024")
|
||||||
|
rsa1024pub = OpenSSL::PKey::RSA.new(rsa1024.public_to_der)
|
||||||
|
|
||||||
asn1 = OpenSSL::ASN1::Sequence([
|
asn1 = OpenSSL::ASN1::Sequence([
|
||||||
OpenSSL::ASN1::Sequence([
|
OpenSSL::ASN1::Sequence([
|
||||||
OpenSSL::ASN1::ObjectId("rsaEncryption"),
|
OpenSSL::ASN1::ObjectId("rsaEncryption"),
|
||||||
|
@ -381,7 +385,7 @@ class OpenSSL::TestPKeyRSA < OpenSSL::PKeyTestCase
|
||||||
])
|
])
|
||||||
key = OpenSSL::PKey::RSA.new(asn1.to_der)
|
key = OpenSSL::PKey::RSA.new(asn1.to_der)
|
||||||
assert_not_predicate key, :private?
|
assert_not_predicate key, :private?
|
||||||
assert_same_rsa dup_public(rsa1024), key
|
assert_same_rsa rsa1024pub, key
|
||||||
|
|
||||||
pem = <<~EOF
|
pem = <<~EOF
|
||||||
-----BEGIN PUBLIC KEY-----
|
-----BEGIN PUBLIC KEY-----
|
||||||
|
@ -392,10 +396,15 @@ class OpenSSL::TestPKeyRSA < OpenSSL::PKeyTestCase
|
||||||
-----END PUBLIC KEY-----
|
-----END PUBLIC KEY-----
|
||||||
EOF
|
EOF
|
||||||
key = OpenSSL::PKey::RSA.new(pem)
|
key = OpenSSL::PKey::RSA.new(pem)
|
||||||
assert_same_rsa dup_public(rsa1024), key
|
assert_same_rsa rsa1024pub, key
|
||||||
|
|
||||||
assert_equal asn1.to_der, dup_public(rsa1024).to_der
|
assert_equal asn1.to_der, key.to_der
|
||||||
assert_equal pem, dup_public(rsa1024).export
|
assert_equal pem, key.export
|
||||||
|
|
||||||
|
assert_equal asn1.to_der, rsa1024.public_to_der
|
||||||
|
assert_equal asn1.to_der, key.public_to_der
|
||||||
|
assert_equal pem, rsa1024.public_to_pem
|
||||||
|
assert_equal pem, key.public_to_pem
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_pem_passwd
|
def test_pem_passwd
|
||||||
|
@ -482,12 +491,6 @@ class OpenSSL::TestPKeyRSA < OpenSSL::PKeyTestCase
|
||||||
assert_same_rsa rsa1024, OpenSSL::PKey.read(pem, "abcdef")
|
assert_same_rsa rsa1024, OpenSSL::PKey.read(pem, "abcdef")
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_public_encoding
|
|
||||||
rsa1024 = Fixtures.pkey("rsa1024")
|
|
||||||
assert_equal dup_public(rsa1024).to_der, rsa1024.public_to_der
|
|
||||||
assert_equal dup_public(rsa1024).to_pem, rsa1024.public_to_pem
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_dup
|
def test_dup
|
||||||
key = Fixtures.pkey("rsa1024")
|
key = Fixtures.pkey("rsa1024")
|
||||||
key2 = key.dup
|
key2 = key.dup
|
||||||
|
|
|
@ -306,32 +306,6 @@ class OpenSSL::PKeyTestCase < OpenSSL::TestCase
|
||||||
assert_equal base.send(comp), test.send(comp)
|
assert_equal base.send(comp), test.send(comp)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def dup_public(key)
|
|
||||||
case key
|
|
||||||
when OpenSSL::PKey::RSA
|
|
||||||
rsa = OpenSSL::PKey::RSA.new
|
|
||||||
rsa.set_key(key.n, key.e, nil)
|
|
||||||
rsa
|
|
||||||
when OpenSSL::PKey::DSA
|
|
||||||
dsa = OpenSSL::PKey::DSA.new
|
|
||||||
dsa.set_pqg(key.p, key.q, key.g)
|
|
||||||
dsa.set_key(key.pub_key, nil)
|
|
||||||
dsa
|
|
||||||
when OpenSSL::PKey::DH
|
|
||||||
dh = OpenSSL::PKey::DH.new
|
|
||||||
dh.set_pqg(key.p, nil, key.g)
|
|
||||||
dh
|
|
||||||
else
|
|
||||||
if defined?(OpenSSL::PKey::EC) && OpenSSL::PKey::EC === key
|
|
||||||
ec = OpenSSL::PKey::EC.new(key.group)
|
|
||||||
ec.public_key = key.public_key
|
|
||||||
ec
|
|
||||||
else
|
|
||||||
raise "unknown key type"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
module OpenSSL::Certs
|
module OpenSSL::Certs
|
||||||
|
|
Загрузка…
Ссылка в новой задаче