|
27 | 27 | ***** END LICENSE BLOCK *****/ |
28 | 28 | package org.jruby.ext.openssl.impl; |
29 | 29 |
|
| 30 | +import java.io.ByteArrayInputStream; |
30 | 31 | import java.io.IOException; |
| 32 | +import java.io.InputStreamReader; |
| 33 | +import java.io.Reader; |
31 | 34 | import java.math.BigInteger; |
32 | 35 |
|
33 | 36 | import java.security.KeyFactory; |
|
51 | 54 | import java.security.spec.X509EncodedKeySpec; |
52 | 55 | import javax.crypto.spec.DHParameterSpec; |
53 | 56 |
|
| 57 | +import org.bouncycastle.asn1.ASN1Encodable; |
54 | 58 | import org.bouncycastle.asn1.ASN1EncodableVector; |
55 | 59 | import org.bouncycastle.asn1.ASN1Encoding; |
56 | 60 | import org.bouncycastle.asn1.ASN1InputStream; |
|
69 | 73 | import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; |
70 | 74 | import org.bouncycastle.asn1.x9.X9ObjectIdentifiers; |
71 | 75 | import org.bouncycastle.jcajce.provider.asymmetric.util.KeyUtil; |
| 76 | +import org.bouncycastle.openssl.PEMParser; |
| 77 | +import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; |
72 | 78 |
|
73 | 79 | import org.jruby.ext.openssl.SecurityHelper; |
74 | 80 |
|
@@ -132,17 +138,12 @@ public static KeyPair readPrivateKey(final Type type, final PrivateKeyInfo keyIn |
132 | 138 | } |
133 | 139 |
|
134 | 140 | // d2i_PUBKEY_bio |
135 | | - public static PublicKey readPublicKey(byte[] input) throws IOException, NoSuchAlgorithmException { |
136 | | - PublicKey key = null; |
137 | | - try { |
138 | | - key = readRSAPublicKey(input); |
139 | | - } catch (InvalidKeySpecException e) { /* ignore */ } |
140 | | - if (key == null) { |
141 | | - try { |
142 | | - key = readDSAPublicKey(input); |
143 | | - } catch (InvalidKeySpecException e) { /* ignore */ } |
| 141 | + public static PublicKey readPublicKey(byte[] input) throws IOException { |
| 142 | + try (Reader in = new InputStreamReader(new ByteArrayInputStream(input))) { |
| 143 | + Object pemObject = new PEMParser(in).readObject(); |
| 144 | + SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(pemObject); |
| 145 | + return new JcaPEMKeyConverter().getPublicKey(publicKeyInfo); |
144 | 146 | } |
145 | | - return key; |
146 | 147 | } |
147 | 148 |
|
148 | 149 | // d2i_RSAPrivateKey_bio |
|
0 commit comments