24
24
import java .security .KeyStoreException ;
25
25
import java .security .NoSuchAlgorithmException ;
26
26
import java .security .PrivateKey ;
27
+ import java .security .Provider ;
27
28
import java .security .Security ;
28
29
import java .security .UnrecoverableKeyException ;
29
30
import java .security .cert .Certificate ;
32
33
import java .security .cert .X509Certificate ;
33
34
import java .security .spec .InvalidKeySpecException ;
34
35
import java .util .Collection ;
36
+ import java .util .ServiceLoader ;
35
37
import javax .net .ssl .KeyManager ;
36
38
import javax .net .ssl .KeyManagerFactory ;
37
39
import org .bouncycastle .asn1 .pkcs .PrivateKeyInfo ;
40
42
import org .bouncycastle .openssl .jcajce .JcaMiscPEMGenerator ;
41
43
import org .bouncycastle .openssl .jcajce .JcaPEMKeyConverter ;
42
44
import org .bouncycastle .util .io .pem .PemWriter ;
45
+ import org .slf4j .Logger ;
46
+ import org .slf4j .LoggerFactory ;
43
47
44
48
public class SSLUtils {
49
+ private static final Logger log = LoggerFactory .getLogger (SSLUtils .class );
50
+
45
51
static {
46
- Security .addProvider (new org .bouncycastle .jce .provider .BouncyCastleProvider ());
52
+ ServiceLoader <Provider > services = ServiceLoader .load (java .security .Provider .class );
53
+ for (Provider service : services ) {
54
+ log .debug ("Found security provider: " + service .getName ());
55
+ Security .addProvider (service );
56
+ }
47
57
}
48
58
49
59
public static boolean isNotNullOrEmpty (String val ) {
50
60
return val != null && val .length () > 0 ;
51
61
}
52
62
53
63
public static KeyManager [] keyManagers (
54
- byte [] certData ,
55
- byte [] keyData ,
56
- String algo ,
57
- String passphrase ,
58
- String keyStoreFile ,
59
- String keyStorePassphrase )
60
- throws NoSuchAlgorithmException , UnrecoverableKeyException , KeyStoreException ,
64
+ byte [] certData ,
65
+ byte [] keyData ,
66
+ String algo ,
67
+ String passphrase ,
68
+ String keyStoreFile ,
69
+ String keyStorePassphrase )
70
+ throws NoSuchAlgorithmException , UnrecoverableKeyException , KeyStoreException ,
61
71
CertificateException , InvalidKeySpecException , IOException {
62
72
KeyManager [] keyManagers = null ;
63
73
if (certData != null && keyData != null ) {
64
74
KeyStore keyStore =
65
- createKeyStore (certData , keyData , algo , passphrase , keyStoreFile , keyStorePassphrase );
75
+ createKeyStore (certData , keyData , algo , passphrase , keyStoreFile , keyStorePassphrase );
66
76
KeyManagerFactory kmf =
67
- KeyManagerFactory .getInstance (KeyManagerFactory .getDefaultAlgorithm ());
77
+ KeyManagerFactory .getInstance (KeyManagerFactory .getDefaultAlgorithm ());
68
78
kmf .init (keyStore , passphrase .toCharArray ());
69
79
keyManagers = kmf .getKeyManagers ();
70
80
}
71
81
return keyManagers ;
72
82
}
73
83
74
84
public static KeyStore createKeyStore (
75
- byte [] clientCertData ,
76
- byte [] clientKeyData ,
77
- String clientKeyAlgo ,
78
- String clientKeyPassphrase ,
79
- String keyStoreFile ,
80
- String keyStorePassphrase )
81
- throws IOException , CertificateException , NoSuchAlgorithmException , InvalidKeySpecException ,
85
+ byte [] clientCertData ,
86
+ byte [] clientKeyData ,
87
+ String clientKeyAlgo ,
88
+ String clientKeyPassphrase ,
89
+ String keyStoreFile ,
90
+ String keyStorePassphrase )
91
+ throws IOException , CertificateException , NoSuchAlgorithmException , InvalidKeySpecException ,
82
92
KeyStoreException {
83
93
try (InputStream certInputStream = new ByteArrayInputStream (clientCertData );
84
- InputStream keyInputStream = new ByteArrayInputStream (clientKeyData )) {
94
+ InputStream keyInputStream = new ByteArrayInputStream (clientKeyData )) {
85
95
return createKeyStore (
86
- certInputStream ,
87
- keyInputStream ,
88
- clientKeyAlgo ,
89
- clientKeyPassphrase != null ? clientKeyPassphrase .toCharArray () : null ,
90
- keyStoreFile ,
91
- getKeyStorePassphrase (keyStorePassphrase ));
96
+ certInputStream ,
97
+ keyInputStream ,
98
+ clientKeyAlgo ,
99
+ clientKeyPassphrase != null ? clientKeyPassphrase .toCharArray () : null ,
100
+ keyStoreFile ,
101
+ getKeyStorePassphrase (keyStorePassphrase ));
92
102
}
93
103
}
94
104
@@ -113,24 +123,24 @@ public static String recognizePrivateKeyAlgo(byte[] privateKeyBytes) {
113
123
}
114
124
115
125
public static PrivateKey loadKey (byte [] privateKeyBytes )
116
- throws IOException , InvalidKeySpecException {
126
+ throws IOException , InvalidKeySpecException {
117
127
return loadKey (
118
- new ByteArrayInputStream (privateKeyBytes ), recognizePrivateKeyAlgo (privateKeyBytes ));
128
+ new ByteArrayInputStream (privateKeyBytes ), recognizePrivateKeyAlgo (privateKeyBytes ));
119
129
}
120
130
121
131
public static PrivateKey loadKey (byte [] pemPrivateKeyBytes , String algo )
122
- throws IOException , InvalidKeySpecException {
132
+ throws IOException , InvalidKeySpecException {
123
133
return loadKey (new ByteArrayInputStream (pemPrivateKeyBytes ), algo );
124
134
}
125
135
126
136
public static PrivateKey loadKey (InputStream keyInputStream , String clientKeyAlgo )
127
- throws IOException , InvalidKeySpecException {
137
+ throws IOException , InvalidKeySpecException {
128
138
final PrivateKey privateKey ;
129
139
try (final PEMParser pemParser = new PEMParser (new InputStreamReader (keyInputStream ))) {
130
140
final Object pemObject = pemParser .readObject ();
131
141
if (pemObject == null ) {
132
142
final String message =
133
- String .format ("PEM Private Key Algorithm [%s] not parsed" , clientKeyAlgo );
143
+ String .format ("PEM Private Key Algorithm [%s] not parsed" , clientKeyAlgo );
134
144
throw new InvalidKeySpecException (message );
135
145
}
136
146
final JcaPEMKeyConverter converter = new JcaPEMKeyConverter ();
@@ -144,23 +154,23 @@ public static PrivateKey loadKey(InputStream keyInputStream, String clientKeyAlg
144
154
} else {
145
155
final String pemObjectType = pemObject .getClass ().getSimpleName ();
146
156
final String message =
147
- String .format (
148
- "PEM Private Key Algorithm [%s] Type [%s] not supported" ,
149
- clientKeyAlgo , pemObjectType );
157
+ String .format (
158
+ "PEM Private Key Algorithm [%s] Type [%s] not supported" ,
159
+ clientKeyAlgo , pemObjectType );
150
160
throw new InvalidKeySpecException (message );
151
161
}
152
162
}
153
163
return privateKey ;
154
164
}
155
165
156
166
public static KeyStore createKeyStore (
157
- InputStream certInputStream ,
158
- InputStream keyInputStream ,
159
- String clientKeyAlgo ,
160
- char [] clientKeyPassphrase ,
161
- String keyStoreFile ,
162
- char [] keyStorePassphrase )
163
- throws IOException , CertificateException , NoSuchAlgorithmException , InvalidKeySpecException ,
167
+ InputStream certInputStream ,
168
+ InputStream keyInputStream ,
169
+ String clientKeyAlgo ,
170
+ char [] clientKeyPassphrase ,
171
+ String keyStoreFile ,
172
+ char [] keyStorePassphrase )
173
+ throws IOException , CertificateException , NoSuchAlgorithmException , InvalidKeySpecException ,
164
174
KeyStoreException {
165
175
CertificateFactory certFactory = CertificateFactory .getInstance ("X509" );
166
176
Collection <? extends Certificate > certs = certFactory .generateCertificates (certInputStream );
@@ -184,15 +194,15 @@ public static KeyStore createKeyStore(
184
194
}
185
195
186
196
String alias =
187
- ((X509Certificate ) certs .stream ().findFirst ().get ()).getSubjectX500Principal ().getName ();
197
+ ((X509Certificate ) certs .stream ().findFirst ().get ()).getSubjectX500Principal ().getName ();
188
198
keyStore .setKeyEntry (
189
- alias , privateKey , clientKeyPassphrase , certs .toArray (new X509Certificate [certs .size ()]));
199
+ alias , privateKey , clientKeyPassphrase , certs .toArray (new X509Certificate [certs .size ()]));
190
200
191
201
return keyStore ;
192
202
}
193
203
194
204
private static void loadDefaultKeyStoreFile (KeyStore keyStore , char [] keyStorePassphrase )
195
- throws CertificateException , NoSuchAlgorithmException , IOException {
205
+ throws CertificateException , NoSuchAlgorithmException , IOException {
196
206
197
207
String keyStorePath = System .getProperty ("javax.net.ssl.keyStore" );
198
208
if (keyStorePath != null && keyStorePath .length () > 0 ) {
@@ -206,7 +216,7 @@ private static void loadDefaultKeyStoreFile(KeyStore keyStore, char[] keyStorePa
206
216
}
207
217
208
218
private static boolean loadDefaultStoreFile (KeyStore keyStore , File fileToLoad , char [] passphrase )
209
- throws CertificateException , NoSuchAlgorithmException , IOException {
219
+ throws CertificateException , NoSuchAlgorithmException , IOException {
210
220
if (fileToLoad .exists () && fileToLoad .isFile () && fileToLoad .length () > 0 ) {
211
221
try (FileInputStream inputStream = new FileInputStream (fileToLoad )) {
212
222
keyStore .load (inputStream , passphrase );
0 commit comments