Skip to content

Commit

Permalink
[fix] raise PKeyError from PKey.read when no key (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
kares committed Apr 8, 2024
1 parent 0b322fd commit ab355ca
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
10 changes: 6 additions & 4 deletions src/main/java/org/jruby/ext/openssl/PKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ public static IRubyObject read(final ThreadContext context, IRubyObject recv, IR
PublicKey pubKey = null;
try {
pubKey = PEMInputOutput.readRSAPublicKey(new StringReader(str.toString()), null);
return new PKeyRSA(runtime, (RSAPublicKey) pubKey);
if (pubKey != null) return new PKeyRSA(runtime, (RSAPublicKey) pubKey);
} catch (IOException e) {
debugStackTrace(runtime, "PKey readRSAPublicKey", e); /* ignore */
}
try {
pubKey = PEMInputOutput.readDSAPublicKey(new StringReader(str.toString()), null);
return new PKeyDSA(runtime, (DSAPublicKey) pubKey);
if (pubKey != null) return new PKeyDSA(runtime, (DSAPublicKey) pubKey);
} catch (IOException e) {
debugStackTrace(runtime, "PKey readDSAPublicKey", e); /* ignore */
}
Expand All @@ -163,7 +163,9 @@ public static IRubyObject read(final ThreadContext context, IRubyObject recv, IR
if (pubKey == null) {
try {
pubKey = PEMInputOutput.readPubKey(new StringReader(str.toString()));
} catch (IOException e) { /* ignore */ }
} catch (IOException e) {
debugStackTrace(runtime, "PKey readPubKey", e); /* ignore */
}
}

if (pubKey != null) {
Expand All @@ -178,7 +180,7 @@ public static IRubyObject read(final ThreadContext context, IRubyObject recv, IR
}
}

throw runtime.newArgumentError("Could not parse PKey");
throw newPKeyError(runtime, "Could not parse PKey: unsupported");
}

private static String getAlgorithm(final KeyPair key) {
Expand Down
15 changes: 3 additions & 12 deletions src/main/java/org/jruby/ext/openssl/impl/PKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,15 @@ public static KeyPair readPrivateKey(final Type type, final PrivateKeyInfo keyIn
}

// d2i_PUBKEY_bio
public static PublicKey readPublicKey(byte[] input) throws IOException,
NoSuchAlgorithmException, InvalidKeySpecException {
public static PublicKey readPublicKey(byte[] input) throws IOException, NoSuchAlgorithmException {
PublicKey key = null;
try {
key = readRSAPublicKey(input);
}
catch (NoSuchAlgorithmException e) { throw e; /* should not happen */ }
catch (InvalidKeySpecException e) {
// ignore
}
} catch (InvalidKeySpecException e) { /* ignore */ }
if (key == null) {
try {
key = readDSAPublicKey(input);
}
catch (NoSuchAlgorithmException e) { throw e; /* should not happen */ }
catch (InvalidKeySpecException e) {
// ignore
}
} catch (InvalidKeySpecException e) { /* ignore */ }
}
return key;
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/ruby/test_pkey.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ def test_pkey_read_pkcs8_and_check_with_cert
assert_true cert.check_private_key(pkey)
end

def test_pkey_pem_file_error
begin
ret = OpenSSL::PKey.read('not a PEM file')
fail "expected OpenSSL::PKey.read to raise (got: #{ret.inspect})"
rescue OpenSSL::PKey::PKeyError => e
assert_equal 'Could not parse PKey: unsupported', e.message
end

begin
ret = OpenSSL::PKey::RSA.new('not a PEM file')
fail "expected OpenSSL::PKey::RSA.new to raise (got: #{ret.inspect})"
rescue OpenSSL::PKey::RSAError
assert true
end
end

def test_to_java
pkey = OpenSSL::PKey.read(KEY)
assert_kind_of java.security.PublicKey, pkey.to_java
Expand Down

0 comments on commit ab355ca

Please sign in to comment.