|
1 | 1 | /* |
2 | | - * Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2005, 2026, 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 |
|
21 | 21 | * questions. |
22 | 22 | */ |
23 | 23 |
|
24 | | -// |
25 | | -// Security properties, once set, cannot revert to unset. To avoid |
26 | | -// conflicts with tests running in the same VM isolate this test by |
27 | | -// running it in otherVM mode. |
28 | | -// |
29 | | - |
30 | 24 | /* |
31 | 25 | * @test |
32 | 26 | * @bug 6302644 |
33 | 27 | * @summary X509KeyManager implementation for NewSunX509 doesn't return most |
34 | 28 | * preferable key |
35 | | - * @run main/othervm PreferredKey |
| 29 | + * @modules java.base/sun.security.x509 |
| 30 | + * java.base/sun.security.util |
| 31 | + * @library /test/lib |
36 | 32 | */ |
37 | | -import java.io.*; |
38 | | -import java.net.*; |
39 | | -import java.security.*; |
40 | | -import javax.net.ssl.*; |
| 33 | +import jdk.test.lib.Asserts; |
| 34 | +import jdk.test.lib.security.CertificateBuilder; |
| 35 | + |
| 36 | +import javax.net.ssl.KeyManagerFactory; |
| 37 | +import javax.net.ssl.X509KeyManager; |
| 38 | +import java.io.IOException; |
| 39 | +import java.math.BigInteger; |
| 40 | +import java.security.KeyPair; |
| 41 | +import java.security.KeyPairGenerator; |
| 42 | +import java.security.KeyStore; |
| 43 | +import java.security.SecureRandom; |
| 44 | +import java.security.cert.Certificate; |
| 45 | +import java.security.cert.CertificateException; |
| 46 | +import java.security.cert.X509Certificate; |
41 | 47 |
|
42 | 48 | public class PreferredKey { |
43 | 49 |
|
44 | | - /* |
45 | | - * ============================================================= |
46 | | - * Set the various variables needed for the tests, then |
47 | | - * specify what tests to run on each side. |
48 | | - */ |
| 50 | + public static void main(String[] args) throws Exception { |
| 51 | + X509KeyManager km = getKeyManager(); |
| 52 | + |
| 53 | + testPreferredKey(km, "RSA", new String[] {"RSA", "DSA"}); |
| 54 | + testPreferredKey(km, "DSA", new String[] {"DSA", "RSA"}); |
| 55 | + } |
49 | 56 |
|
50 | | - /* |
51 | | - * Where do we find the keystores? |
52 | | - */ |
53 | | - static String pathToStores = "../../../../javax/net/ssl/etc"; |
54 | | - static String keyStoreFile = "keystore"; |
55 | | - static String passwd = "passphrase"; |
| 57 | + private static void testPreferredKey(X509KeyManager km, |
| 58 | + String keyType, |
| 59 | + String[] multiKeyTypes) { |
| 60 | + String[] aliases = km.getClientAliases(keyType, null); |
| 61 | + String alias = km.chooseClientAlias(multiKeyTypes, null, null); |
56 | 62 |
|
| 63 | + Asserts.assertTrue(aliases != null && alias != null, |
| 64 | + "Should return preferred alias"); |
57 | 65 |
|
58 | | - public static void main(String[] args) throws Exception { |
59 | | - // MD5 is used in this test case, don't disable MD5 algorithm. |
60 | | - Security.setProperty("jdk.certpath.disabledAlgorithms", |
61 | | - "MD2, RSA keySize < 1024"); |
62 | | - Security.setProperty("jdk.tls.disabledAlgorithms", |
63 | | - "SSLv3, RC4, DH keySize < 768"); |
| 66 | + String algorithm = km.getPrivateKey(alias).getAlgorithm(); |
| 67 | + Asserts.assertTrue(algorithm.equals(keyType) && algorithm.equals( |
| 68 | + km.getPrivateKey(aliases[0]).getAlgorithm()), |
| 69 | + "Failed to get the preferable key aliases"); |
| 70 | + } |
64 | 71 |
|
65 | | - KeyStore ks; |
66 | | - KeyManagerFactory kmf; |
67 | | - X509KeyManager km; |
| 72 | + private static X509KeyManager getKeyManager() throws Exception { |
| 73 | + char[] passphrase = "passphrase".toCharArray(); |
68 | 74 |
|
69 | | - String keyFilename = |
70 | | - System.getProperty("test.src", ".") + "/" + pathToStores + |
71 | | - "/" + keyStoreFile; |
72 | | - char [] password = passwd.toCharArray(); |
| 75 | + KeyPair rsaKey = KeyPairGenerator.getInstance("RSA").generateKeyPair(); |
| 76 | + KeyPair dsaKey = KeyPairGenerator.getInstance("DSA").generateKeyPair(); |
73 | 77 |
|
74 | | - ks = KeyStore.getInstance(new File(keyFilename), password); |
75 | | - kmf = KeyManagerFactory.getInstance("NewSunX509"); |
76 | | - kmf.init(ks, password); |
77 | | - km = (X509KeyManager) kmf.getKeyManagers()[0]; |
| 78 | + // create a key store |
| 79 | + KeyStore ks = KeyStore.getInstance("PKCS12"); |
| 80 | + ks.load(null, passphrase); |
78 | 81 |
|
79 | | - /* |
80 | | - * There should be both an rsa and a dsa entry in the |
81 | | - * keystore, otherwise the test will no work. |
82 | | - */ |
83 | | - String[] aliases = km.getClientAliases("RSA", null); |
84 | | - String alias = km.chooseClientAlias(new String[] {"RSA", "DSA"}, |
85 | | - null, null); |
| 82 | + ks.setKeyEntry("dummyrsa", |
| 83 | + rsaKey.getPrivate(), |
| 84 | + passphrase, |
| 85 | + new Certificate[]{createSelfSignedCert(rsaKey, |
| 86 | + "SHA256withRSA")}); |
| 87 | + ks.setKeyEntry("dummydsa", |
| 88 | + dsaKey.getPrivate(), |
| 89 | + passphrase, |
| 90 | + new Certificate[]{createSelfSignedCert(dsaKey, |
| 91 | + "SHA256withDSA")}); |
86 | 92 |
|
87 | | - // there're should both be null or nonnull |
88 | | - if (aliases != null || alias != null) { |
89 | | - String algorithm = km.getPrivateKey(alias).getAlgorithm(); |
90 | | - if (!algorithm.equals("RSA") || !algorithm.equals( |
91 | | - km.getPrivateKey(aliases[0]).getAlgorithm())) { |
92 | | - throw new Exception("Failed to get the preferable key aliases"); |
93 | | - } |
94 | | - } |
| 93 | + KeyManagerFactory kmf = KeyManagerFactory.getInstance("NewSunX509"); |
| 94 | + kmf.init(ks, passphrase); |
95 | 95 |
|
96 | | - aliases = km.getClientAliases("DSA", null); |
97 | | - alias = km.chooseClientAlias(new String[] {"DSA", "RSA"}, |
98 | | - null, null); |
| 96 | + return (X509KeyManager) kmf.getKeyManagers()[0]; |
| 97 | + } |
99 | 98 |
|
100 | | - // there're should both be null or nonnull |
101 | | - if (aliases != null || alias != null) { |
102 | | - String algorithm = km.getPrivateKey(alias).getAlgorithm(); |
103 | | - if (!algorithm.equals("DSA") || !algorithm.equals( |
104 | | - km.getPrivateKey(aliases[0]).getAlgorithm())) { |
105 | | - throw new Exception("Failed to get the preferable key aliases"); |
106 | | - } |
107 | | - } |
| 99 | + private static X509Certificate createSelfSignedCert(KeyPair caKeys, |
| 100 | + String keyAlg) |
| 101 | + throws CertificateException, IOException { |
| 102 | + return (new CertificateBuilder() |
| 103 | + .setSubjectName("CN=dummy.example.com, OU=Dummy, " + |
| 104 | + "O=Dummy, L=Cupertino, ST=CA, C=US") |
| 105 | + .setPublicKey(caKeys.getPublic()) |
| 106 | + .setOneHourValidity() |
| 107 | + .setSerialNumber(BigInteger.valueOf( |
| 108 | + new SecureRandom().nextLong(1000000) + 1)) |
| 109 | + ).build(null, caKeys.getPrivate(), keyAlg); |
108 | 110 | } |
109 | 111 | } |
0 commit comments