Skip to content

Commit 073bcee

Browse files
committed
[fix] PKey.read not parsing RSA pub-key
resolves #176
1 parent e3530c5 commit 073bcee

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed

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

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -109,59 +109,53 @@ public static IRubyObject read(final ThreadContext context, IRubyObject recv, IR
109109
pass = args[1].isNil() ? null : args[1].toString().toCharArray();
110110
}
111111

112-
final byte[] input = StringHelper.readX509PEM(context, data);
113-
KeyPair key = null;
112+
final RubyString str = readInitArg(context, data);
113+
Object key = null;
114114
// d2i_PrivateKey_bio
115115
try {
116-
key =org.jruby.ext.openssl.impl.PKey.readPrivateKey(input);
117-
} catch (IOException ioe) {
118-
// ignore
119-
} catch (GeneralSecurityException gse) {
120-
// ignore
121-
}
116+
key = readPrivateKey(str, pass);
117+
} catch (IOException e) { /* ignore */ }
122118
// PEM_read_bio_PrivateKey
123-
if (key == null) {
124-
try {
125-
key = PEMInputOutput.readPrivateKey(new InputStreamReader(new ByteArrayInputStream(input)), pass);
126-
} catch (IOException ioe) {
127-
// ignore
128-
}
129-
}
130119
if (key != null) {
131-
final String alg = getAlgorithm(key);
120+
final KeyPair keyPair = (KeyPair) key;
121+
final String alg = getAlgorithm(keyPair);
132122
if ( "RSA".equals(alg) ) {
133123
return new PKeyRSA(runtime, _PKey(runtime).getClass("RSA"),
134-
(RSAPrivateCrtKey) key.getPrivate(), (RSAPublicKey) key.getPublic()
124+
(RSAPrivateCrtKey) keyPair.getPrivate(), (RSAPublicKey) keyPair.getPublic()
135125
);
136126
}
137127
if ( "DSA".equals(alg) ) {
138128
return new PKeyDSA(runtime, _PKey(runtime).getClass("DSA"),
139-
(DSAPrivateKey) key.getPrivate(), (DSAPublicKey) key.getPublic()
129+
(DSAPrivateKey) keyPair.getPrivate(), (DSAPublicKey) keyPair.getPublic()
140130
);
141131
}
142132
if ( "ECDSA".equals(alg) ) {
143133
return new PKeyEC(runtime, _PKey(runtime).getClass("EC"),
144-
(PrivateKey) key.getPrivate(), (PublicKey) key.getPublic()
134+
(PrivateKey) keyPair.getPrivate(), (PublicKey) keyPair.getPublic()
145135
);
146136
}
147137
}
148138

149139
PublicKey pubKey = null;
140+
try {
141+
pubKey = PEMInputOutput.readRSAPublicKey(new StringReader(str.toString()), null);
142+
return new PKeyRSA(runtime, (RSAPublicKey) pubKey);
143+
} catch (IOException e) { /* ignore */ }
144+
try {
145+
pubKey = PEMInputOutput.readDSAPublicKey(new StringReader(str.toString()), null);
146+
return new PKeyDSA(runtime, (DSAPublicKey) pubKey);
147+
} catch (IOException e) { /* ignore */ }
148+
149+
final byte[] input = StringHelper.readX509PEM(context, str);
150150
// d2i_PUBKEY_bio
151151
try {
152152
pubKey = org.jruby.ext.openssl.impl.PKey.readPublicKey(input);
153-
} catch (IOException ioe) {
154-
// ignore
155-
} catch (GeneralSecurityException gse) {
156-
// ignore
157-
}
153+
} catch (IOException|GeneralSecurityException e) { /* ignore */ }
158154
// PEM_read_bio_PUBKEY
159155
if (pubKey == null) {
160156
try {
161-
pubKey = PEMInputOutput.readPubKey(new InputStreamReader(new ByteArrayInputStream(input)));
162-
} catch (IOException ioe) {
163-
// ignore
164-
}
157+
pubKey = PEMInputOutput.readPubKey(new StringReader(str.toString()));
158+
} catch (IOException e) { /* ignore */ }
165159
}
166160

167161
if (pubKey != null) {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,6 @@ public static PublicKey readDSAPublicKey(final KeyFactory dsaFactory, final byte
263263
BigInteger p = ((ASN1Integer) seq.getObjectAt(0)).getValue();
264264
BigInteger q = ((ASN1Integer) seq.getObjectAt(1)).getValue();
265265
BigInteger g = ((ASN1Integer) seq.getObjectAt(2)).getValue();
266-
//System.out.println(y);
267-
//System.out.println(p);
268266
return dsaFactory.generatePublic(new DSAPublicKeySpec(y.getPositiveValue(), p, q, g));
269267
}
270268
}

src/test/ruby/test_pkey.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# coding: US-ASCII
2+
require File.expand_path('test_helper', File.dirname(__FILE__))
3+
4+
class TestPKey < TestCase
5+
6+
def test_pkey_read
7+
key = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArTlm5TxJp3WHMNmWIfo/\nWvkyhJCXc1S78Y9B8lSXxXnkRqX8Twxu5EkdUP0TwgD5gp0TGy7UPm/SgWlQOcqX\nqtdOWq/Hk29Ve9z6k6wTmst7NTefmm/7OqkeYmBhfhoECLCKBADM8ctjoqD63R0e\n3bUW2knq6vCS5YMmD76/5UoU647BzB9CjgDzjuTKEbXL5AvcO5wWDgHSp7CA+2t4\nIFQvQMrPso5mvm2hNvD19vI0VjiY21rKgkJQAXSrLgkJg/fTL2wQiz10d2GnYsmx\nDeJCiBMwC+cmRW2eWePqaCPaWJwr92KsIiry+LgyGb3y01SUVV8kQgQXazutHqfu\ncQIDAQAB\n-----END PUBLIC KEY-----\n"
8+
9+
# assert OpenSSL::PKey::RSA.new(key).public?
10+
11+
pkey = OpenSSL::PKey.read(key)
12+
assert_same OpenSSL::PKey::RSA, pkey.class
13+
assert pkey.public?
14+
assert_equal OpenSSL::PKey::RSA.new(key).n, pkey.n
15+
assert_equal OpenSSL::PKey::RSA.new(key).e, pkey.e
16+
end
17+
18+
end

0 commit comments

Comments
 (0)