Skip to content

Commit a729a70

Browse files
committed
8225181: KeyStore should have a getAttributes method
Reviewed-by: mullan
1 parent 38f525e commit a729a70

File tree

6 files changed

+169
-2
lines changed

6 files changed

+169
-2
lines changed

src/java.base/share/classes/java/security/KeyStore.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,34 @@ public final String getType()
10201020
return this.type;
10211021
}
10221022

1023+
/**
1024+
* Retrieves the attributes associated with the given alias.
1025+
*
1026+
* @param alias the alias name
1027+
* @return an unmodifiable {@code Set} of attributes. This set is
1028+
* empty if the {@code KeyStoreSpi} implementation has not overridden
1029+
* {@link KeyStoreSpi#engineGetAttributes(String)}, or the given
1030+
* alias does not exist, or there are no attributes associated
1031+
* with the alias. This set may also be empty for
1032+
* {@code PrivateKeyEntry} or {@code SecretKeyEntry}
1033+
* entries that contain protected attributes and are only available
1034+
* through the {@link Entry#getAttributes} method after the entry
1035+
* is extracted.
1036+
*
1037+
* @throws KeyStoreException if the keystore has not been initialized
1038+
* (loaded).
1039+
* @throws NullPointerException if {@code alias} is {@code null}
1040+
*
1041+
* @since 18
1042+
*/
1043+
public final Set<Entry.Attribute> getAttributes(String alias)
1044+
throws KeyStoreException {
1045+
if (!initialized) {
1046+
throw new KeyStoreException("Uninitialized keystore");
1047+
}
1048+
return keyStoreSpi.engineGetAttributes(Objects.requireNonNull(alias));
1049+
}
1050+
10231051
/**
10241052
* Returns the key associated with the given alias, using the given
10251053
* password to recover it. The key must have been associated with

src/java.base/share/classes/java/security/KeyStoreSpi.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2021, 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
@@ -447,6 +447,33 @@ void engineLoad(InputStream stream, KeyStore.LoadStoreParameter param)
447447
return;
448448
}
449449

450+
/**
451+
* Retrieves the attributes associated with the given alias.
452+
*
453+
* @implSpec
454+
* The default implementation returns an empty {@code Set}.
455+
* {@code KeyStoreSpi} implementations that support attributes
456+
* should override this method.
457+
*
458+
* @param alias the alias name
459+
* @return an unmodifiable {@code Set} of attributes. This set is
460+
* empty if the given alias does not exist or there are no
461+
* attributes associated with the alias. This set may also be
462+
* empty for {@code PrivateKeyEntry} or {@code SecretKeyEntry}
463+
* entries that contain protected attributes. These protected
464+
* attributes should be populated into the result returned by
465+
* {@link #engineGetEntry} and can be retrieved by calling
466+
* the {@link Entry#getAttributes} method.
467+
*
468+
* @throws KeyStoreException if the keystore has not been initialized
469+
* (loaded).
470+
*
471+
* @since 18
472+
*/
473+
public Set<Entry.Attribute> engineGetAttributes(String alias) {
474+
return Collections.emptySet();
475+
}
476+
450477
/**
451478
* Gets a {@code KeyStore.Entry} for the specified alias
452479
* with the specified protection parameter.

src/java.base/share/classes/sun/security/pkcs12/PKCS12KeyStore.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,15 @@ public synchronized void engineStore(OutputStream stream, char[] password)
13071307
stream.flush();
13081308
}
13091309

1310+
@Override
1311+
public Set<KeyStore.Entry.Attribute> engineGetAttributes(String alias) {
1312+
if (!engineContainsAlias(alias)) {
1313+
return super.engineGetAttributes(alias);
1314+
}
1315+
Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
1316+
return getAttributes(entry);
1317+
}
1318+
13101319
/**
13111320
* Gets a <code>KeyStore.Entry</code> for the specified alias
13121321
* with the specified protection parameter.

src/java.base/share/classes/sun/security/provider/DomainKeyStore.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2021, 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
@@ -236,6 +236,28 @@ public Date engineGetCreationDate(String alias) {
236236
return date;
237237
}
238238

239+
@Override
240+
public Set<KeyStore.Entry.Attribute> engineGetAttributes(String alias) {
241+
242+
AbstractMap.SimpleEntry<String, Collection<KeyStore>> pair =
243+
getKeystoresForReading(alias);
244+
Set<KeyStore.Entry.Attribute> result = Collections.emptySet();
245+
246+
try {
247+
String entryAlias = pair.getKey();
248+
for (KeyStore keystore : pair.getValue()) {
249+
result = keystore.getAttributes(entryAlias);
250+
if (result != null) {
251+
break;
252+
}
253+
}
254+
} catch (KeyStoreException e) {
255+
throw new IllegalStateException(e);
256+
}
257+
258+
return result;
259+
}
260+
239261
/**
240262
* Assigns the given private key to the given alias, protecting
241263
* it with the given password as defined in PKCS8.

src/java.base/share/classes/sun/security/util/KeyStoreDelegator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ public void engineDeleteEntry(String alias) throws KeyStoreException {
129129
keystore.engineDeleteEntry(alias);
130130
}
131131

132+
@Override
133+
public Set<KeyStore.Entry.Attribute> engineGetAttributes(String alias) {
134+
return keystore.engineGetAttributes(alias);
135+
}
136+
132137
@Override
133138
public Enumeration<String> engineAliases() {
134139
return keystore.engineAliases();
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8225181
27+
* @summary KeyStore should have a getAttributes method
28+
* @library /test/lib
29+
* @modules java.base/sun.security.tools.keytool
30+
* java.base/sun.security.x509
31+
*/
32+
33+
import jdk.test.lib.Asserts;
34+
import sun.security.tools.keytool.CertAndKeyGen;
35+
import sun.security.x509.X500Name;
36+
37+
import java.io.ByteArrayInputStream;
38+
import java.io.ByteArrayOutputStream;
39+
import java.security.KeyStore;
40+
import java.security.cert.Certificate;
41+
42+
public class GetAttributes {
43+
44+
static char[] pass = "changeit".toCharArray();
45+
46+
public static void main(String[] args) throws Exception {
47+
48+
// Create a keystore with one private key entry and one cert entry
49+
CertAndKeyGen cag = new CertAndKeyGen("EC", "SHA256withECDSA");
50+
KeyStore ks = KeyStore.getInstance("pkcs12");
51+
ks.load(null, null);
52+
cag.generate("secp256r1");
53+
ks.setKeyEntry("a", cag.getPrivateKey(), pass, new Certificate[] {
54+
cag.getSelfCertificate(new X500Name("CN=a"), 1000)} );
55+
cag.generate("secp256r1");
56+
ks.setCertificateEntry("b",
57+
cag.getSelfCertificate(new X500Name("CN=b"), 1000));
58+
59+
// Test
60+
check(ks);
61+
62+
// Test newly loaded
63+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
64+
ks.store(bos, pass);
65+
KeyStore ks2 = KeyStore.getInstance("pkcs12");
66+
ks2.load(new ByteArrayInputStream(bos.toByteArray()), pass);
67+
check(ks2);
68+
}
69+
70+
static void check(KeyStore ks) throws Exception {
71+
var entry = ks.getEntry("a", new KeyStore.PasswordProtection(pass));
72+
Asserts.assertEQ(ks.getAttributes("a"), entry.getAttributes());
73+
entry = ks.getEntry("b", null);
74+
Asserts.assertEQ(ks.getAttributes("b"), entry.getAttributes());
75+
}
76+
}

0 commit comments

Comments
 (0)