|
5 | 5 | import java.io.IOException; |
6 | 6 | import java.io.Reader; |
7 | 7 | import java.io.StringReader; |
8 | | -import java.security.KeyFactory; |
9 | 8 | import java.security.KeyStore; |
10 | 9 | import java.security.KeyStoreException; |
11 | 10 | import java.security.NoSuchAlgorithmException; |
12 | 11 | import java.security.PrivateKey; |
13 | 12 | import java.security.cert.Certificate; |
14 | 13 | import java.security.cert.CertificateException; |
15 | 14 | import java.security.spec.InvalidKeySpecException; |
16 | | -import java.security.spec.PKCS8EncodedKeySpec; |
17 | 15 | import java.util.ArrayList; |
18 | 16 | import java.util.List; |
19 | 17 |
|
|
25 | 23 | import org.bouncycastle.jce.provider.BouncyCastleProvider; |
26 | 24 | import org.bouncycastle.openssl.PEMKeyPair; |
27 | 25 | import org.bouncycastle.openssl.PEMParser; |
| 26 | +import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; |
28 | 27 | import org.slf4j.Logger; |
29 | 28 | import org.slf4j.LoggerFactory; |
30 | 29 |
|
@@ -111,47 +110,36 @@ public static PrivateKey loadPrivateKey(final Reader reader) throws IOException, |
111 | 110 | try (PEMParser pemParser = new PEMParser(reader)) { |
112 | 111 | Object readObject = pemParser.readObject(); |
113 | 112 | while (readObject != null) { |
114 | | - if (readObject instanceof PEMKeyPair) { |
115 | | - PEMKeyPair pemKeyPair = (PEMKeyPair) readObject; |
116 | | - PrivateKey privateKey = guessKey(pemKeyPair.getPrivateKeyInfo().getEncoded()); |
117 | | - if (privateKey != null) { |
118 | | - return privateKey; |
119 | | - } |
120 | | - } else if (readObject instanceof PrivateKeyInfo) { |
121 | | - PrivateKeyInfo privateKeyInfo = (PrivateKeyInfo) readObject; |
122 | | - PrivateKey privateKey = guessKey(privateKeyInfo.getEncoded()); |
123 | | - if (privateKey != null) { |
124 | | - return privateKey; |
125 | | - } |
126 | | - } else if (readObject instanceof ASN1ObjectIdentifier) { |
127 | | - // no idea how it can be used |
128 | | - final ASN1ObjectIdentifier asn1ObjectIdentifier = (ASN1ObjectIdentifier) readObject; |
129 | | - LOG.trace("Ignoring asn1ObjectIdentifier {}", asn1ObjectIdentifier); |
130 | | - } else { |
131 | | - LOG.warn("Unknown object '{}' from PEMParser", readObject); |
| 113 | + PrivateKeyInfo privateKeyInfo = getPrivateKeyInfoOrNull(readObject); |
| 114 | + if (privateKeyInfo != null) { |
| 115 | + return new JcaPEMKeyConverter().getPrivateKey(privateKeyInfo); |
132 | 116 | } |
133 | | - |
134 | 117 | readObject = pemParser.readObject(); |
135 | 118 | } |
136 | 119 | } |
137 | 120 |
|
138 | 121 | return null; |
139 | 122 | } |
140 | 123 |
|
| 124 | + /** |
| 125 | + * Find a PrivateKeyInfo in the PEM object details. Returns null if the PEM object type is unknown. |
| 126 | + */ |
141 | 127 | @CheckForNull |
142 | | - public static PrivateKey guessKey(byte[] encodedKey) throws NoSuchAlgorithmException { |
143 | | - //no way to know, so iterate |
144 | | - for (String guessFactory : new String[]{"RSA", "ECDSA"}) { |
145 | | - try { |
146 | | - KeyFactory factory = KeyFactory.getInstance(guessFactory); |
147 | | - |
148 | | - PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedKey); |
149 | | - return factory.generatePrivate(privateKeySpec); |
150 | | - } catch (InvalidKeySpecException ignore) { |
151 | | - } |
| 128 | + private static PrivateKeyInfo getPrivateKeyInfoOrNull(Object pemObject) throws NoSuchAlgorithmException { |
| 129 | + PrivateKeyInfo privateKeyInfo = null; |
| 130 | + if (pemObject instanceof PEMKeyPair) { |
| 131 | + PEMKeyPair pemKeyPair = (PEMKeyPair) pemObject; |
| 132 | + privateKeyInfo = pemKeyPair.getPrivateKeyInfo(); |
| 133 | + } else if (pemObject instanceof PrivateKeyInfo) { |
| 134 | + privateKeyInfo = (PrivateKeyInfo) pemObject; |
| 135 | + } else if (pemObject instanceof ASN1ObjectIdentifier) { |
| 136 | + // no idea how it can be used |
| 137 | + final ASN1ObjectIdentifier asn1ObjectIdentifier = (ASN1ObjectIdentifier) pemObject; |
| 138 | + LOG.trace("Ignoring asn1ObjectIdentifier {}", asn1ObjectIdentifier); |
| 139 | + } else { |
| 140 | + LOG.warn("Unknown object '{}' from PEMParser", pemObject); |
152 | 141 | } |
153 | | - |
154 | | - return null; |
| 142 | + return privateKeyInfo; |
155 | 143 | } |
156 | 144 |
|
157 | 145 | /** |
|
0 commit comments