Skip to content

Commit 2b3a641

Browse files
committed
8245134: test/lib/jdk/test/lib/security/KeyStoreUtils.java should allow to specify aliases
Backport-of: 8457999
1 parent 8f49220 commit 2b3a641

File tree

1 file changed

+66
-49
lines changed

1 file changed

+66
-49
lines changed

test/lib/jdk/test/lib/security/KeyStoreUtils.java

Lines changed: 66 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* 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)
118118
}
119119

120120
/**
121-
* Create trust store with given certificates.
121+
* Create trust store with given certificates and corresponding aliases.
122122
*
123123
* @param type the key store type
124124
* @param certStrs the certificates added to the trust store
125+
* @param aliases the aliases corresponding to the trust entries respectively
125126
* @return the trust store
126127
* @throws Exception on errors
127128
*/
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+
130136
KeyStore trustStore = initKeyStore(type);
131137

132138
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,
134141
CertUtils.getCertFromString(certStrs[i]));
135142
}
136143

@@ -140,25 +147,56 @@ public static KeyStore createTrustStore(String type, String[] certStrs)
140147
/**
141148
* Create trust store with given certificates.
142149
*
150+
* @param type the key store type
143151
* @param certStrs the certificates added to the trust store
144152
* @return the trust store
145153
* @throws Exception on errors
146154
*/
147-
public static KeyStore createTrustStore(String[] certStrs)
155+
public static KeyStore createTrustStore(String type, String[] certStrs)
148156
throws Exception {
149-
return createTrustStore(DEFAULT_TYPE, certStrs);
157+
return createTrustStore(type, certStrs, null);
150158
}
151159

152160
/**
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.
154186
*
155187
* @param type the key store type
156188
* @param entries the key entries added to the key store
189+
* @param aliases the aliases corresponding to the key entries respectively
157190
* @return the key store
158191
* @throws Exception on errors
159192
*/
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+
162200
KeyStore keyStore = initKeyStore(type);
163201

164202
for (int i = 0; i < entries.length; i++) {
@@ -173,7 +211,8 @@ public static KeyStore createKeyStore(String type, KeyEntry[] entries)
173211
chain[j] = CertUtils.getCertFromString(entry.certStrs[j]);
174212
}
175213

176-
keyStore.setKeyEntry("cert-" + i, key, password, chain);
214+
String alias = aliases == null ? "cert-" + i : aliases[i];
215+
keyStore.setKeyEntry(alias, key, password, chain);
177216
}
178217

179218
return keyStore;
@@ -182,64 +221,42 @@ public static KeyStore createKeyStore(String type, KeyEntry[] entries)
182221
/**
183222
* Create key store with given entries.
184223
*
224+
* @param type the key store type
185225
* @param entries the key entries added to the key store
186226
* @return the key store
187227
* @throws Exception on errors
188228
*/
189-
public static KeyStore createKeyStore(KeyEntry[] entries)
229+
public static KeyStore createKeyStore(String type, KeyEntry[] entries)
190230
throws Exception {
191-
return createKeyStore(DEFAULT_TYPE, entries);
231+
return createKeyStore(type, entries, null);
192232
}
193233

194234
/**
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.
200236
*
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
207240
* @throws Exception on errors
208241
*/
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)
211243
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);
221245
}
222246

223247
/**
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.
229249
*
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
235252
* @throws Exception on errors
236253
*/
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);
241256
}
242257

258+
// Initialize key store with given store type.
259+
// Note that it always has no password.
243260
private static KeyStore initKeyStore(String type) throws Exception {
244261
KeyStore keyStore = KeyStore.getInstance(type);
245262
keyStore.load(null, null);

0 commit comments

Comments
 (0)