|
7 | 7 | */
|
8 | 8 | package com.joyent.http.signature;
|
9 | 9 |
|
10 |
| -import com.joyent.http.signature.crypto.NssBridgeKeyConverter; |
11 | 10 | import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
12 | 11 | import org.bouncycastle.openssl.PEMDecryptorProvider;
|
13 | 12 | import org.bouncycastle.openssl.PEMEncryptedKeyPair;
|
14 | 13 | import org.bouncycastle.openssl.PEMKeyPair;
|
15 | 14 | import org.bouncycastle.openssl.PEMParser;
|
| 15 | +import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; |
16 | 16 | import org.bouncycastle.openssl.jcajce.JcePEMDecryptorProviderBuilder;
|
17 | 17 |
|
18 | 18 | import java.io.BufferedReader;
|
|
26 | 26 | import java.nio.file.Files;
|
27 | 27 | import java.nio.file.Path;
|
28 | 28 | import java.security.KeyPair;
|
| 29 | +import java.security.Provider; |
29 | 30 | import java.security.Security;
|
30 | 31 |
|
31 | 32 |
|
|
35 | 36 | */
|
36 | 37 | public final class KeyPairLoader {
|
37 | 38 |
|
| 39 | + /** |
| 40 | + * Provider name for libnss. |
| 41 | + */ |
| 42 | + public static final String PROVIDER_PKCS11_NSS = "SunPKCS11-NSS"; |
38 | 43 |
|
39 |
| - @SuppressWarnings("checkstyle:javadocmethod") |
40 |
| - private KeyPairLoader() { |
41 |
| - } |
| 44 | + /** |
| 45 | + * Provider name for Bouncy Castle. |
| 46 | + */ |
| 47 | + public static final String PROVIDER_BOUNCY_CASTLE = "BC"; |
| 48 | + |
| 49 | + /** |
| 50 | + * The key format converter to use when reading key pairs and libnss is enabled (or specifically requested). |
| 51 | + */ |
| 52 | + private static final JcaPEMKeyConverter CONVERTER_PKCS11_NSS; |
| 53 | + |
| 54 | + /** |
| 55 | + * The key format converter to use when libnss is disabled (or BC is specifically requested). |
| 56 | + */ |
| 57 | + private static final JcaPEMKeyConverter CONVERTER_BOUNCY_CASTLE; |
42 | 58 |
|
43 | 59 | /**
|
44 |
| - * The key format converter to use when reading key pairs. |
| 60 | + * Set of security providers users can request. |
45 | 61 | */
|
46 |
| - private static final NssBridgeKeyConverter CONVERTER = |
47 |
| - new NssBridgeKeyConverter(); |
48 |
| - { |
49 |
| - CONVERTER.setProvider("BC"); |
| 62 | + @SuppressWarnings({"checkstyle:JavaDocVariable", "checkstyle:JavadocMethod"}) |
| 63 | + public enum DesiredSecurityProvider { |
| 64 | + BC(PROVIDER_BOUNCY_CASTLE), |
| 65 | + NSS(PROVIDER_PKCS11_NSS); |
| 66 | + |
| 67 | + private final String providerCode; |
| 68 | + |
| 69 | + DesiredSecurityProvider(final String providerCode) { |
| 70 | + this.providerCode = providerCode; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public String toString() { |
| 75 | + return providerCode; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + static { |
| 80 | + final Provider providerPkcs11NSS = Security.getProvider(PROVIDER_PKCS11_NSS); |
| 81 | + |
| 82 | + if (providerPkcs11NSS != null) { |
| 83 | + CONVERTER_PKCS11_NSS = new JcaPEMKeyConverter().setProvider(PROVIDER_PKCS11_NSS); |
| 84 | + } else { |
| 85 | + CONVERTER_PKCS11_NSS = null; |
| 86 | + } |
| 87 | + |
| 88 | + final Provider providerBouncyCastle = Security.getProvider(PROVIDER_BOUNCY_CASTLE); |
| 89 | + |
| 90 | + if (providerBouncyCastle == null) { |
| 91 | + Security.addProvider(new BouncyCastleProvider()); |
| 92 | + } |
| 93 | + |
| 94 | + CONVERTER_BOUNCY_CASTLE = new JcaPEMKeyConverter().setProvider(PROVIDER_BOUNCY_CASTLE); |
| 95 | + } |
| 96 | + |
| 97 | + @SuppressWarnings("checkstyle:javadocmethod") |
| 98 | + private KeyPairLoader() { |
50 | 99 | }
|
51 | 100 |
|
52 | 101 | /**
|
@@ -150,36 +199,71 @@ public static KeyPair getKeyPair(final byte[] pKeyBytes, final char[] password)
|
150 | 199 | /**
|
151 | 200 | * Read KeyPair from an input stream, optionally using password.
|
152 | 201 | *
|
153 |
| - * @param is private key content as a stream |
| 202 | + * @param is private key content as a stream |
| 203 | + * @param password password associated with key |
| 204 | + * @return public/private keypair object |
| 205 | + * @throws IOException If unable to read the private key from the string |
| 206 | + */ |
| 207 | + public static KeyPair getKeyPair(final InputStream is, |
| 208 | + final char[] password) throws IOException { |
| 209 | + return getKeyPair(is, password, null); |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * Read KeyPair from an input stream, optionally using password and desired Security Provider. Most implementations |
| 214 | + * should continue calling the one and two-argument methods |
| 215 | + * |
| 216 | + * @param is private key content as a stream |
154 | 217 | * @param password password associated with key
|
| 218 | + * @param provider security provider to use when loading the key |
155 | 219 | * @return public/private keypair object
|
156 | 220 | * @throws IOException If unable to read the private key from the string
|
157 | 221 | */
|
158 | 222 | public static KeyPair getKeyPair(final InputStream is,
|
159 |
| - final char[] password) throws IOException { |
| 223 | + final char[] password, |
| 224 | + final DesiredSecurityProvider provider) throws IOException { |
| 225 | + final Object pemObject; |
160 | 226 | try (InputStreamReader isr = new InputStreamReader(is, StandardCharsets.US_ASCII);
|
161 | 227 | BufferedReader br = new BufferedReader(isr);
|
162 | 228 | PEMParser pemParser = new PEMParser(br)) {
|
163 | 229 |
|
| 230 | + pemObject = pemParser.readObject(); |
| 231 | + } |
| 232 | + |
| 233 | + final PEMKeyPair pemKeyPair; |
| 234 | + |
| 235 | + if (pemObject instanceof PEMEncryptedKeyPair) { |
164 | 236 | if (password == null) {
|
165 |
| - Security.addProvider(new BouncyCastleProvider()); |
166 |
| - final Object object = pemParser.readObject(); |
167 |
| - return CONVERTER.getKeyPair((PEMKeyPair) object); |
168 |
| - } else { |
169 |
| - PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().build(password); |
170 |
| - |
171 |
| - Object object = pemParser.readObject(); |
172 |
| - |
173 |
| - final KeyPair kp; |
174 |
| - if (object instanceof PEMEncryptedKeyPair) { |
175 |
| - kp = CONVERTER.getKeyPair(((PEMEncryptedKeyPair) object).decryptKeyPair(decProv)); |
176 |
| - } else { |
177 |
| - kp = CONVERTER.getKeyPair((PEMKeyPair) object); |
178 |
| - } |
179 |
| - |
180 |
| - return kp; |
| 237 | + throw new KeyLoadException("Loaded key is encrypted but no password was supplied."); |
181 | 238 | }
|
| 239 | + |
| 240 | + final PEMDecryptorProvider decryptorProvider = new JcePEMDecryptorProviderBuilder().build(password); |
| 241 | + final PEMEncryptedKeyPair encryptedPemObject = ((PEMEncryptedKeyPair) pemObject); |
| 242 | + pemKeyPair = encryptedPemObject.decryptKeyPair(decryptorProvider); |
| 243 | + } else if (pemObject instanceof PEMKeyPair) { |
| 244 | + if (password != null) { |
| 245 | + throw new KeyLoadException("Loaded key is not encrypted but a password was supplied."); |
| 246 | + } |
| 247 | + |
| 248 | + pemKeyPair = (PEMKeyPair) pemObject; |
| 249 | + } else { |
| 250 | + throw new KeyLoadException("Unexpected PEM object loaded: " + pemObject.getClass().getCanonicalName()); |
182 | 251 | }
|
| 252 | + |
| 253 | + // throw if the user has specifically requested NSS and it is unavailable |
| 254 | + if (provider != null && provider.equals(DesiredSecurityProvider.NSS) && CONVERTER_PKCS11_NSS == null) { |
| 255 | + throw new KeyLoadException(PROVIDER_PKCS11_NSS + " provider requested but unavailable. " |
| 256 | + + "Is java.security configured correctly?"); |
| 257 | + } |
| 258 | + |
| 259 | + // Attempt to load with NSS if it is available and requested (or no provider was specified) |
| 260 | + final boolean attemptPKCS11NSS = provider == null || provider.equals(DesiredSecurityProvider.NSS); |
| 261 | + |
| 262 | + if (CONVERTER_PKCS11_NSS != null && attemptPKCS11NSS) { |
| 263 | + return CONVERTER_PKCS11_NSS.getKeyPair(pemKeyPair); |
| 264 | + } |
| 265 | + |
| 266 | + return CONVERTER_BOUNCY_CASTLE.getKeyPair(pemKeyPair); |
183 | 267 | }
|
184 | 268 |
|
185 | 269 | }
|
0 commit comments