1
1
/*
2
- * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
@@ -118,19 +118,26 @@ public static KeyStore loadKeyStore(String path, String password)
118
118
}
119
119
120
120
/**
121
- * Create trust store with given certificates.
121
+ * Create trust store with given certificates and corresponding aliases .
122
122
*
123
123
* @param type the key store type
124
124
* @param certStrs the certificates added to the trust store
125
+ * @param aliases the aliases corresponding to the trust entries respectively
125
126
* @return the trust store
126
127
* @throws Exception on errors
127
128
*/
128
- public static KeyStore createTrustStore (String type , String [] certStrs )
129
- throws Exception {
129
+ public static KeyStore createTrustStore (String type , String [] certStrs ,
130
+ String [] aliases ) throws Exception {
131
+ if (aliases != null && aliases .length != certStrs .length ) {
132
+ throw new IllegalArgumentException (
133
+ "The counts of certs and aliases are not matching." );
134
+ }
135
+
130
136
KeyStore trustStore = initKeyStore (type );
131
137
132
138
for (int i = 0 ; i < certStrs .length ; i ++) {
133
- trustStore .setCertificateEntry ("trust-" + i ,
139
+ String alias = aliases == null ? "trust-" + i : aliases [i ];
140
+ trustStore .setCertificateEntry (alias ,
134
141
CertUtils .getCertFromString (certStrs [i ]));
135
142
}
136
143
@@ -140,25 +147,56 @@ public static KeyStore createTrustStore(String type, String[] certStrs)
140
147
/**
141
148
* Create trust store with given certificates.
142
149
*
150
+ * @param type the key store type
143
151
* @param certStrs the certificates added to the trust store
144
152
* @return the trust store
145
153
* @throws Exception on errors
146
154
*/
147
- public static KeyStore createTrustStore (String [] certStrs )
155
+ public static KeyStore createTrustStore (String type , String [] certStrs )
148
156
throws Exception {
149
- return createTrustStore (DEFAULT_TYPE , certStrs );
157
+ return createTrustStore (type , certStrs , null );
150
158
}
151
159
152
160
/**
153
- * Create key store with given entries.
161
+ * Create trust store with given certificates and corresponding aliases.
162
+ *
163
+ * @param certStrs the certificates added to the trust store
164
+ * @param aliases the aliases corresponding to the trust entries respectively
165
+ * @return the trust store
166
+ * @throws Exception on errors
167
+ */
168
+ public static KeyStore createTrustStore (String [] certStrs , String [] aliases )
169
+ throws Exception {
170
+ return createTrustStore (DEFAULT_TYPE , certStrs , aliases );
171
+ }
172
+
173
+ /**
174
+ * Create trust store with given certificates.
175
+ *
176
+ * @param certStrs the certificates added to the trust store
177
+ * @return the trust store
178
+ * @throws Exception on errors
179
+ */
180
+ public static KeyStore createTrustStore (String [] certStrs ) throws Exception {
181
+ return createTrustStore (DEFAULT_TYPE , certStrs , null );
182
+ }
183
+
184
+ /**
185
+ * Create key store with given entries and corresponding aliases.
154
186
*
155
187
* @param type the key store type
156
188
* @param entries the key entries added to the key store
189
+ * @param aliases the aliases corresponding to the key entries respectively
157
190
* @return the key store
158
191
* @throws Exception on errors
159
192
*/
160
- public static KeyStore createKeyStore (String type , KeyEntry [] entries )
161
- throws Exception {
193
+ public static KeyStore createKeyStore (String type , KeyEntry [] entries ,
194
+ String [] aliases ) throws Exception {
195
+ if (aliases != null && aliases .length != entries .length ) {
196
+ throw new IllegalArgumentException (
197
+ "The counts of entries and aliases are not matching." );
198
+ }
199
+
162
200
KeyStore keyStore = initKeyStore (type );
163
201
164
202
for (int i = 0 ; i < entries .length ; i ++) {
@@ -173,7 +211,8 @@ public static KeyStore createKeyStore(String type, KeyEntry[] entries)
173
211
chain [j ] = CertUtils .getCertFromString (entry .certStrs [j ]);
174
212
}
175
213
176
- keyStore .setKeyEntry ("cert-" + i , key , password , chain );
214
+ String alias = aliases == null ? "cert-" + i : aliases [i ];
215
+ keyStore .setKeyEntry (alias , key , password , chain );
177
216
}
178
217
179
218
return keyStore ;
@@ -182,64 +221,42 @@ public static KeyStore createKeyStore(String type, KeyEntry[] entries)
182
221
/**
183
222
* Create key store with given entries.
184
223
*
224
+ * @param type the key store type
185
225
* @param entries the key entries added to the key store
186
226
* @return the key store
187
227
* @throws Exception on errors
188
228
*/
189
- public static KeyStore createKeyStore (KeyEntry [] entries )
229
+ public static KeyStore createKeyStore (String type , KeyEntry [] entries )
190
230
throws Exception {
191
- return createKeyStore (DEFAULT_TYPE , entries );
231
+ return createKeyStore (type , entries , null );
192
232
}
193
233
194
234
/**
195
- * Create key store with given private keys and associated certificate chains.
196
- * Note that here one chain contains only one certificate. If a chain needs
197
- * to contain multiple certificates, please use the following methods:
198
- * createKeyStore(String type, KeyEntry[] entries);
199
- * createKeyStore(KeyEntry[] entries)
235
+ * Create key store with given entries and corresponding aliases.
200
236
*
201
- * @param type the key store type
202
- * @param keyAlgos the key algorithm array
203
- * @param keyStrs the PEM-encoded PKCS8 key string array
204
- * @param passwords the key-associated password array
205
- * @param certStrs the key-associated certificate array
206
- * @return the key store
237
+ * @param entries the key entries added to the key store
238
+ * @param aliases the aliases corresponding to the key entries respectively
239
+ * @return the key store
207
240
* @throws Exception on errors
208
241
*/
209
- public static KeyStore createKeyStore (String type , String [] keyAlgos ,
210
- String [] keyStrs , String [] passwords , String [] certStrs )
242
+ public static KeyStore createKeyStore (KeyEntry [] entries , String [] aliases )
211
243
throws Exception {
212
- KeyEntry [] entries = new KeyEntry [keyStrs .length ];
213
- for (int i = 0 ; i < entries .length ; i ++) {
214
- entries [i ] = new KeyEntry (
215
- keyAlgos [i ],
216
- keyStrs [i ],
217
- passwords == null ? null : passwords [i ],
218
- new String [] { certStrs [i ] });
219
- }
220
- return createKeyStore (type , entries );
244
+ return createKeyStore (DEFAULT_TYPE , entries , aliases );
221
245
}
222
246
223
247
/**
224
- * Create key store with given private keys and associated certificate chains.
225
- * Note that here one chain contains only one certificate. If a chain needs
226
- * to contain multiple certificates, please use the following methods:
227
- * createKeyStore(String type, KeyEntry[] entries);
228
- * createKeyStore(KeyEntry[] entries)
248
+ * Create key store with given entries.
229
249
*
230
- * @param keyAlgos the key algorithm array
231
- * @param keyStrs the PEM-encoded PKCS8 key string array
232
- * @param passwords the key-associated password array
233
- * @param certStrs the key-associated certificate array
234
- * @return the key store
250
+ * @param entries the key entries added to the key store
251
+ * @return the key store
235
252
* @throws Exception on errors
236
253
*/
237
- public static KeyStore createKeyStore (String [] keyAlgos , String [] keyStrs ,
238
- String [] passwords , String [] certStrs ) throws Exception {
239
- return createKeyStore (DEFAULT_TYPE , keyAlgos , keyStrs , passwords ,
240
- certStrs );
254
+ public static KeyStore createKeyStore (KeyEntry [] entries ) throws Exception {
255
+ return createKeyStore (DEFAULT_TYPE , entries , null );
241
256
}
242
257
258
+ // Initialize key store with given store type.
259
+ // Note that it always has no password.
243
260
private static KeyStore initKeyStore (String type ) throws Exception {
244
261
KeyStore keyStore = KeyStore .getInstance (type );
245
262
keyStore .load (null , null );
0 commit comments