Skip to content

Commit ab355ca

Browse files
committed
[fix] raise PKeyError from PKey.read when no key (#285)
1 parent 0b322fd commit ab355ca

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,13 @@ public static IRubyObject read(final ThreadContext context, IRubyObject recv, IR
141141
PublicKey pubKey = null;
142142
try {
143143
pubKey = PEMInputOutput.readRSAPublicKey(new StringReader(str.toString()), null);
144-
return new PKeyRSA(runtime, (RSAPublicKey) pubKey);
144+
if (pubKey != null) return new PKeyRSA(runtime, (RSAPublicKey) pubKey);
145145
} catch (IOException e) {
146146
debugStackTrace(runtime, "PKey readRSAPublicKey", e); /* ignore */
147147
}
148148
try {
149149
pubKey = PEMInputOutput.readDSAPublicKey(new StringReader(str.toString()), null);
150-
return new PKeyDSA(runtime, (DSAPublicKey) pubKey);
150+
if (pubKey != null) return new PKeyDSA(runtime, (DSAPublicKey) pubKey);
151151
} catch (IOException e) {
152152
debugStackTrace(runtime, "PKey readDSAPublicKey", e); /* ignore */
153153
}
@@ -163,7 +163,9 @@ public static IRubyObject read(final ThreadContext context, IRubyObject recv, IR
163163
if (pubKey == null) {
164164
try {
165165
pubKey = PEMInputOutput.readPubKey(new StringReader(str.toString()));
166-
} catch (IOException e) { /* ignore */ }
166+
} catch (IOException e) {
167+
debugStackTrace(runtime, "PKey readPubKey", e); /* ignore */
168+
}
167169
}
168170

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

181-
throw runtime.newArgumentError("Could not parse PKey");
183+
throw newPKeyError(runtime, "Could not parse PKey: unsupported");
182184
}
183185

184186
private static String getAlgorithm(final KeyPair key) {

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

+3-12
Original file line numberDiff line numberDiff line change
@@ -132,24 +132,15 @@ public static KeyPair readPrivateKey(final Type type, final PrivateKeyInfo keyIn
132132
}
133133

134134
// d2i_PUBKEY_bio
135-
public static PublicKey readPublicKey(byte[] input) throws IOException,
136-
NoSuchAlgorithmException, InvalidKeySpecException {
135+
public static PublicKey readPublicKey(byte[] input) throws IOException, NoSuchAlgorithmException {
137136
PublicKey key = null;
138137
try {
139138
key = readRSAPublicKey(input);
140-
}
141-
catch (NoSuchAlgorithmException e) { throw e; /* should not happen */ }
142-
catch (InvalidKeySpecException e) {
143-
// ignore
144-
}
139+
} catch (InvalidKeySpecException e) { /* ignore */ }
145140
if (key == null) {
146141
try {
147142
key = readDSAPublicKey(input);
148-
}
149-
catch (NoSuchAlgorithmException e) { throw e; /* should not happen */ }
150-
catch (InvalidKeySpecException e) {
151-
// ignore
152-
}
143+
} catch (InvalidKeySpecException e) { /* ignore */ }
153144
}
154145
return key;
155146
}

src/test/ruby/test_pkey.rb

+16
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ def test_pkey_read_pkcs8_and_check_with_cert
2828
assert_true cert.check_private_key(pkey)
2929
end
3030

31+
def test_pkey_pem_file_error
32+
begin
33+
ret = OpenSSL::PKey.read('not a PEM file')
34+
fail "expected OpenSSL::PKey.read to raise (got: #{ret.inspect})"
35+
rescue OpenSSL::PKey::PKeyError => e
36+
assert_equal 'Could not parse PKey: unsupported', e.message
37+
end
38+
39+
begin
40+
ret = OpenSSL::PKey::RSA.new('not a PEM file')
41+
fail "expected OpenSSL::PKey::RSA.new to raise (got: #{ret.inspect})"
42+
rescue OpenSSL::PKey::RSAError
43+
assert true
44+
end
45+
end
46+
3147
def test_to_java
3248
pkey = OpenSSL::PKey.read(KEY)
3349
assert_kind_of java.security.PublicKey, pkey.to_java

0 commit comments

Comments
 (0)