1 /* 2 * Copyright (c) 2005, 2015, 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 // 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 /* 31 * @test 32 * @bug 6302644 33 * @summary X509KeyManager implementation for NewSunX509 doesn't return most 34 * preferable key 35 * @run main/othervm PreferredKey 36 */ 37 import java.io.*; 38 import java.net.*; 39 import java.security.*; 40 import javax.net.ssl.*; 41 42 public class PreferredKey { 43 44 /* 45 * ============================================================= 46 * Set the various variables needed for the tests, then 47 * specify what tests to run on each side. 48 */ 49 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"; 56 57 main(String[] args)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"); 64 65 KeyStore ks; 66 KeyManagerFactory kmf; 67 X509KeyManager km; 68 69 String keyFilename = 70 System.getProperty("test.src", ".") + "/" + pathToStores + 71 "/" + keyStoreFile; 72 char [] password = passwd.toCharArray(); 73 74 ks = KeyStore.getInstance("JKS"); 75 ks.load(new FileInputStream(keyFilename), password); 76 kmf = KeyManagerFactory.getInstance("NewSunX509"); 77 kmf.init(ks, password); 78 km = (X509KeyManager) kmf.getKeyManagers()[0]; 79 80 /* 81 * There should be both an rsa and a dsa entry in the 82 * keystore, otherwise the test will no work. 83 */ 84 String[] aliases = km.getClientAliases("RSA", null); 85 String alias = km.chooseClientAlias(new String[] {"RSA", "DSA"}, 86 null, null); 87 88 // there're should both be null or nonnull 89 if (aliases != null || alias != null) { 90 String algorithm = km.getPrivateKey(alias).getAlgorithm(); 91 if (!algorithm.equals("RSA") || !algorithm.equals( 92 km.getPrivateKey(aliases[0]).getAlgorithm())) { 93 throw new Exception("Failed to get the preferable key aliases"); 94 } 95 } 96 97 aliases = km.getClientAliases("DSA", null); 98 alias = km.chooseClientAlias(new String[] {"DSA", "RSA"}, 99 null, null); 100 101 // there're should both be null or nonnull 102 if (aliases != null || alias != null) { 103 String algorithm = km.getPrivateKey(alias).getAlgorithm(); 104 if (!algorithm.equals("DSA") || !algorithm.equals( 105 km.getPrivateKey(aliases[0]).getAlgorithm())) { 106 throw new Exception("Failed to get the preferable key aliases"); 107 } 108 } 109 } 110 } 111