Skip to content

Commit 0792e51

Browse files
committed
[fix] raise PKeyError for unsupported public key algorithm
1 parent 76ab17c commit 0792e51

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/main/java/org/jruby/ext/openssl/impl/PKey.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ private static PublicKey readPublicKey(final SubjectPublicKeyInfo publicKeyInfo)
193193
return keyFactory.generatePublic(new X509EncodedKeySpec(publicKeyInfo.getEncoded()));
194194
}
195195

196-
private static Type matchPublicKeyType(final AlgorithmIdentifier algId) throws IllegalArgumentException {
196+
private static Type matchPublicKeyType(final AlgorithmIdentifier algId) throws IOException {
197197
final ASN1ObjectIdentifier algIdentifier = algId.getAlgorithm();
198198

199199
if (X9ObjectIdentifiers.id_ecPublicKey.equals(algIdentifier)) return Type.EC;
@@ -202,7 +202,7 @@ private static Type matchPublicKeyType(final AlgorithmIdentifier algId) throws I
202202
if (EdECObjectIdentifiers.id_Ed25519.equals(algIdentifier)) return Type.EdDSA;
203203
if (EdECObjectIdentifiers.id_Ed448.equals(algIdentifier)) return Type.EdDSA;
204204

205-
return Type.valueOf(algIdentifier.getId());
205+
throw new IOException("unsupported public key algorithm: " + algIdentifier);
206206
}
207207

208208
// d2i_RSAPrivateKey_bio

test/test_pkey.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ def test_pkey_read_pkcs8_and_check_with_cert
6767
assert_true cert.check_private_key(pkey)
6868
end
6969

70+
# SubjectPublicKeyInfo with an unsupported algorithm (used to escape as a Java exception)
71+
def test_pkey_read_public_key_unknown_algorithm
72+
spki = OpenSSL::ASN1::Sequence([
73+
OpenSSL::ASN1::Sequence([ OpenSSL::ASN1::ObjectId('1.2.3.4.5.6.7.8') ]),
74+
OpenSSL::ASN1::BitString("\x00" * 32)
75+
])
76+
pem = "-----BEGIN PUBLIC KEY-----\n" + [spki.to_der].pack('m') + "-----END PUBLIC KEY-----\n"
77+
78+
assert_raise(OpenSSL::PKey::PKeyError) { OpenSSL::PKey.read(pem) }
79+
assert_raise(OpenSSL::PKey::PKeyError) { OpenSSL::PKey.read(spki.to_der) }
80+
end
81+
7082
def test_pkey_pem_file_error
7183
begin
7284
ret = OpenSSL::PKey.read('not a PEM file')

0 commit comments

Comments
 (0)