|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 8309667 |
| 27 | + * @library /test/lib |
| 28 | + * @modules java.base/sun.security.tools.keytool |
| 29 | + * java.base/sun.security.x509 |
| 30 | + * @summary ensures attributes reading is correct |
| 31 | + */ |
| 32 | +import jdk.test.lib.Asserts; |
| 33 | +import sun.security.tools.keytool.CertAndKeyGen; |
| 34 | +import sun.security.x509.X500Name; |
| 35 | + |
| 36 | +import javax.crypto.EncryptedPrivateKeyInfo; |
| 37 | +import javax.crypto.spec.SecretKeySpec; |
| 38 | +import java.io.ByteArrayInputStream; |
| 39 | +import java.io.ByteArrayOutputStream; |
| 40 | +import java.security.KeyStore; |
| 41 | +import java.security.PKCS12Attribute; |
| 42 | +import java.security.cert.Certificate; |
| 43 | +import java.security.cert.X509Certificate; |
| 44 | +import java.util.Set; |
| 45 | + |
| 46 | +public class AttributesCorrectness { |
| 47 | + |
| 48 | + static final char[] PASSWORD = "changeit".toCharArray(); |
| 49 | + static final String LOCAL_KEY_ID = "1.2.840.113549.1.9.21"; |
| 50 | + static final String TRUSTED_KEY_USAGE = "2.16.840.1.113894.746875.1.1"; |
| 51 | + static final String FRIENDLY_NAME = "1.2.840.113549.1.9.20"; |
| 52 | + |
| 53 | + static CertAndKeyGen cag; |
| 54 | + static KeyStore ks; |
| 55 | + |
| 56 | + public static void main(String[] args) throws Exception { |
| 57 | + |
| 58 | + ks = KeyStore.getInstance("pkcs12"); |
| 59 | + ks.load(null, null); |
| 60 | + cag = new CertAndKeyGen("Ed25519", "Ed25519"); |
| 61 | + |
| 62 | + cag.generate(-1); |
| 63 | + ks.setCertificateEntry("c", ss("c")); |
| 64 | + |
| 65 | + cag.generate(-1); |
| 66 | + ks.setKeyEntry("d", cag.getPrivateKey(), PASSWORD, chain("d")); |
| 67 | + |
| 68 | + cag.generate(-1); |
| 69 | + ks.setKeyEntry("e", new EncryptedPrivateKeyInfo( |
| 70 | + "PBEWithMD5AndDES", new byte[100]).getEncoded(), chain("e")); |
| 71 | + |
| 72 | + var f = new KeyStore.SecretKeyEntry(new SecretKeySpec(new byte[16], "AES"), |
| 73 | + Set.of(new PKCS12Attribute("1.2.3", "456"))); |
| 74 | + ks.setEntry("f", f, new KeyStore.PasswordProtection(PASSWORD)); |
| 75 | + |
| 76 | + cag.generate(-1); |
| 77 | + var g = new KeyStore.TrustedCertificateEntry(ss("g"), |
| 78 | + Set.of(new PKCS12Attribute("1.2.4", "456"))); |
| 79 | + ks.setEntry("g", g, null); |
| 80 | + |
| 81 | + cag.generate(-1); |
| 82 | + var h = new KeyStore.PrivateKeyEntry(cag.getPrivateKey(), chain("h"), |
| 83 | + Set.of(new PKCS12Attribute("1.2.5", "456"))); |
| 84 | + ks.setEntry("h", h, new KeyStore.PasswordProtection(PASSWORD)); |
| 85 | + |
| 86 | + var i = new KeyStore.SecretKeyEntry(new SecretKeySpec(new byte[16], "AES")); |
| 87 | + ks.setEntry("i", i, new KeyStore.PasswordProtection(PASSWORD)); |
| 88 | + |
| 89 | + cag.generate(-1); |
| 90 | + var j = new KeyStore.TrustedCertificateEntry(ss("g")); |
| 91 | + ks.setEntry("j", j, null); |
| 92 | + |
| 93 | + cag.generate(-1); |
| 94 | + var k = new KeyStore.PrivateKeyEntry(cag.getPrivateKey(), chain("h")); |
| 95 | + ks.setEntry("k", k, new KeyStore.PasswordProtection(PASSWORD)); |
| 96 | + check(); |
| 97 | + |
| 98 | + var bout = new ByteArrayOutputStream(); |
| 99 | + ks.store(bout, PASSWORD); |
| 100 | + ks.load(new ByteArrayInputStream(bout.toByteArray()), PASSWORD); |
| 101 | + check(); |
| 102 | + } |
| 103 | + |
| 104 | + static X509Certificate ss(String alias) throws Exception { |
| 105 | + return cag.getSelfCertificate(new X500Name("CN=" + alias), 100); |
| 106 | + } |
| 107 | + |
| 108 | + static Certificate[] chain(String alias) throws Exception { |
| 109 | + return new Certificate[] { ss(alias) }; |
| 110 | + } |
| 111 | + |
| 112 | + static Void check() { |
| 113 | + checkAttributes("c", TRUSTED_KEY_USAGE); |
| 114 | + checkAttributes("d", LOCAL_KEY_ID); |
| 115 | + checkAttributes("e", LOCAL_KEY_ID); |
| 116 | + checkAttributes("f", LOCAL_KEY_ID, "1.2.3"); |
| 117 | + checkAttributes("g", TRUSTED_KEY_USAGE, "1.2.4"); |
| 118 | + checkAttributes("h", LOCAL_KEY_ID, "1.2.5"); |
| 119 | + checkAttributes("i", LOCAL_KEY_ID); |
| 120 | + checkAttributes("j", TRUSTED_KEY_USAGE); |
| 121 | + checkAttributes("k", LOCAL_KEY_ID); |
| 122 | + return null; |
| 123 | + } |
| 124 | + |
| 125 | + static void checkAttributes(String alias, String... keys) { |
| 126 | + try { |
| 127 | + var attrs = keys[0].equals(LOCAL_KEY_ID) |
| 128 | + ? ks.getAttributes(alias) |
| 129 | + : ks.getEntry(alias, null).getAttributes(); |
| 130 | + Asserts.assertEQ(attrs.size(), keys.length + 1); |
| 131 | + Asserts.assertTrue( |
| 132 | + attrs.contains(new PKCS12Attribute(FRIENDLY_NAME, alias))); |
| 133 | + for (var attr : attrs) { |
| 134 | + var name = attr.getName(); |
| 135 | + if (name.equals(FRIENDLY_NAME)) continue; |
| 136 | + var found = false; |
| 137 | + for (var key : keys) { |
| 138 | + if (key.equals(name)) { |
| 139 | + found = true; |
| 140 | + break; |
| 141 | + } |
| 142 | + } |
| 143 | + Asserts.assertTrue(found, name); |
| 144 | + } |
| 145 | + } catch (Exception e) { |
| 146 | + throw new RuntimeException(e); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments