7
7
8
8
package oracle .nosql .driver .iam ;
9
9
10
- import static oracle .nosql .driver .util .CheckNull .requireNonNullIAE ;
11
-
12
10
import java .io .IOException ;
13
11
import java .io .InputStream ;
14
- import java .io .InputStreamReader ;
15
- import java .nio .charset .StandardCharsets ;
16
- import java .security .Provider ;
17
- import java .security .Security ;
12
+ import java .nio .channels .Channels ;
13
+ import java .nio .channels .ReadableByteChannel ;
14
+ import java .security .PrivateKey ;
18
15
import java .security .interfaces .RSAPrivateKey ;
19
16
import java .util .Arrays ;
20
17
21
- import org .bouncycastle .asn1 .pkcs .PrivateKeyInfo ;
22
- import org .bouncycastle .asn1 .x509 .SubjectPublicKeyInfo ;
23
- import org .bouncycastle .openssl .EncryptionException ;
24
- import org .bouncycastle .openssl .PEMDecryptorProvider ;
25
- import org .bouncycastle .openssl .PEMEncryptedKeyPair ;
26
- import org .bouncycastle .openssl .PEMException ;
27
- import org .bouncycastle .openssl .PEMKeyPair ;
28
- import org .bouncycastle .openssl .PEMParser ;
29
- import org .bouncycastle .openssl .jcajce .JcaPEMKeyConverter ;
30
- import org .bouncycastle .openssl .jcajce .JcePEMDecryptorProviderBuilder ;
18
+ import oracle .nosql .driver .iam .pki .Pem ;
19
+ import oracle .nosql .driver .iam .pki .PemEncryptionException ;
20
+ import oracle .nosql .driver .iam .pki .PemException ;
31
21
32
22
/**
33
23
* @hidden
34
24
* Internal use only
35
25
* <p>
36
26
* The RSA private key provider that loads and caches private key from input
37
- * stream using bouncy castle PEM key utilities.
27
+ * stream using PEM key utilities in oci-java-sdk.
28
+ *
29
+ * See com.oracle.bmc.http.signing.internal.PEMStreamRSAPrivateKeySupplier
30
+ * for reference.
38
31
*/
39
32
class PrivateKeyProvider {
40
- private final JcaPEMKeyConverter converter = new JcaPEMKeyConverter ();
41
33
private RSAPrivateKey key = null ;
42
34
43
35
/**
@@ -66,79 +58,25 @@ void reload(InputStream keyInputStream, char[] passphrase) {
66
58
}
67
59
68
60
void getKeyInternal (InputStream keyInputStream , char [] passphrase ) {
69
- PEMParser keyReader = null ;
70
- try {
71
- keyReader = new PEMParser (
72
- new InputStreamReader (keyInputStream , StandardCharsets .UTF_8 ));
73
-
74
- Object object = null ;
75
- try {
76
- object = keyReader .readObject ();
77
- } catch (IOException ioe ) {
78
- throw new IllegalArgumentException (
79
- "Error reading private key" , ioe );
80
- }
81
- PrivateKeyInfo keyInfo ;
82
-
83
- if (object instanceof PEMEncryptedKeyPair ) {
84
- requireNonNullIAE (
85
- passphrase ,
86
- "The provided private key requires a passphrase" );
87
-
88
- JcePEMDecryptorProviderBuilder decryptBuilder =
89
- new JcePEMDecryptorProviderBuilder ();
90
-
91
- if (!isProviderInstalled ()) {
92
- /*
93
- * If BouncyCastle is not installed, must add the provider
94
- * explicitly to enable the PEMDecrptorProvider.
95
- * https://github.com/bcgit/bc-java/issues/156
96
- */
97
- decryptBuilder .setProvider (getBouncyCastleProvider ());
98
- }
99
-
100
- PEMDecryptorProvider decProv = decryptBuilder .build (passphrase );
101
- try {
102
- keyInfo = ((PEMEncryptedKeyPair ) object )
103
- .decryptKeyPair (decProv )
104
- .getPrivateKeyInfo ();
105
- } catch (EncryptionException ee ) {
106
- throw new IllegalArgumentException (
107
- "The provided passphrase is incorrect." , ee );
108
- } catch (IOException ioe ) {
109
- throw new IllegalArgumentException (
110
- "Error decrypting private key." , ioe );
111
- }
112
- } else if (object instanceof PrivateKeyInfo ) {
113
- keyInfo = (PrivateKeyInfo ) object ;
114
- } else if (object instanceof PEMKeyPair ) {
115
- keyInfo = ((PEMKeyPair ) object ).getPrivateKeyInfo ();
116
- } else if (object instanceof SubjectPublicKeyInfo ) {
117
- throw new IllegalArgumentException (
118
- "Public key provided instead of private key" );
119
- } else if (object != null ) {
120
- throw new IllegalArgumentException (
121
- "Private key must be in PEM format," +
122
- "was: " + object .getClass ());
61
+ try (ReadableByteChannel channel = Channels .newChannel (keyInputStream );
62
+ Pem .Passphrase pemPassphrase = Pem .Passphrase .of (passphrase )){
63
+ PrivateKey privateKey =
64
+ Pem .decoder ().with (pemPassphrase ).decodePrivateKey (channel );
65
+ if (privateKey instanceof RSAPrivateKey ) {
66
+ key = (RSAPrivateKey ) privateKey ;
123
67
} else {
124
68
throw new IllegalArgumentException (
125
- "Private key must be in PEM format" );
126
- }
127
-
128
- try {
129
- this .key = (RSAPrivateKey ) converter .getPrivateKey (keyInfo );
130
- } catch (PEMException e ) {
131
- throw new IllegalArgumentException (
132
- "Error converting private key" );
69
+ "Must be RSA private key, but " + privateKey .toString ());
133
70
}
71
+ } catch (PemEncryptionException e ) {
72
+ throw new IllegalArgumentException (
73
+ "The provided passphrase is incorrect." , e );
74
+ } catch (PemException e ) {
75
+ throw new IllegalArgumentException (
76
+ "Private key must be in PEM format" , e );
77
+ } catch (IOException e ) {
78
+ throw new IllegalArgumentException ("Error reading private key" , e );
134
79
} finally {
135
- if (keyReader != null ) {
136
- try {
137
- keyReader .close ();
138
- } catch (IOException e ) {
139
- /* ignore */
140
- }
141
- }
142
80
if (keyInputStream != null ) {
143
81
try {
144
82
keyInputStream .close ();
@@ -151,25 +89,4 @@ void getKeyInternal(InputStream keyInputStream, char[] passphrase) {
151
89
}
152
90
}
153
91
}
154
-
155
- private static boolean isProviderInstalled () {
156
- return (Security .getProvider ("BC" ) != null );
157
- }
158
-
159
- private static Provider getBouncyCastleProvider () {
160
- Provider provider = null ;
161
- try {
162
- Class <?> providerClass = Class .forName (
163
- "org.bouncycastle.jce.provider.BouncyCastleProvider" );
164
- provider = (Provider )providerClass
165
- .getDeclaredConstructor ().newInstance ();
166
- } catch (ClassNotFoundException e ) {
167
- throw new IllegalArgumentException (
168
- "Unable to find bouncy castle provider" );
169
- } catch (Exception e ) {
170
- throw new IllegalArgumentException (
171
- "Error creating bouncy castle provider" );
172
- }
173
- return provider ;
174
- }
175
92
}
0 commit comments