1 /* 2 * Copyright (c) 2001, 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 4387949 4302197 27 * @summary Need to add Sockets and key arrays to the 28 * X509KeyManager.choose*Alias() methods & There's no mechanism 29 * to select one key out of many in a keystore 30 * 31 * chooseServerAlias method is reverted back to accept a single 32 * keytype as a parameter, please see RFE: 4501014 33 * The part of the test on the server-side is changed to test 34 * passing in a single keytype parameter to chooseServerAlias method. 35 * 36 * @author Brad Wetmore 37 */ 38 39 import java.io.*; 40 import java.net.*; 41 import java.security.*; 42 import javax.net.ssl.*; 43 44 public class SelectOneKeyOutOfMany { 45 46 /* 47 * ============================================================= 48 * Set the various variables needed for the tests, then 49 * specify what tests to run on each side. 50 */ 51 52 /* 53 * Where do we find the keystores? 54 */ 55 static String pathToStores = "../../../../javax/net/ssl/etc"; 56 static String keyStoreFile = "keystore"; 57 static String passwd = "passphrase"; 58 main(String[] args)59 public static void main(String[] args) throws Exception { 60 KeyStore ks; 61 KeyManagerFactory kmf; 62 X509KeyManager km; 63 64 char[] passphrase = passwd.toCharArray(); 65 66 String keyFilename = 67 System.getProperty("test.src", ".") + "/" + pathToStores + 68 "/" + keyStoreFile; 69 /* 70 * Setup the tests. 71 */ 72 kmf = KeyManagerFactory.getInstance("SunX509"); 73 ks = KeyStore.getInstance("JKS"); 74 ks.load(new FileInputStream(keyFilename), passphrase); 75 kmf.init(ks, passphrase); 76 km = (X509KeyManager) kmf.getKeyManagers()[0]; 77 78 /* 79 * There should be one of each key type here. 80 */ 81 String [] nothing = new String [] { "nothing" }; 82 String [] rsa = new String [] { "RSA" }; 83 String [] dsa = new String [] { "DSA" }; 84 String [] rsaDsa = new String [] { "RSA", "DSA" }; 85 String [] dsaRsa = new String [] { "DSA", "RSA" }; 86 87 String resultsRsaDsa, resultsDsaRsa; 88 String resultsRsa, resultsDsa; 89 String resultsNone; 90 91 String [] resultArrayRSA; 92 String [] resultArrayDSA; 93 94 /* 95 * Check get*Aliases for null returns 96 */ 97 if (km.getClientAliases("nothing", null) != null) 98 throw new Exception("km.getClientAliases(nothing) != null"); 99 System.out.println("km.getClientAlias(nothing) returning nulls"); 100 101 if (km.getServerAliases("nothing", null) != null) 102 throw new Exception("km.getServerAliases(nothing) != null"); 103 System.out.println("km.getServerAlias(nothing) returning nulls"); 104 System.out.println("====="); 105 106 System.out.println("Dumping Certs..."); 107 if ((resultArrayRSA = km.getServerAliases("RSA", null)) == null) 108 throw new Exception("km.getServerAliases(RSA) == null"); 109 for (int i = 0; i < resultArrayRSA.length; i++) { 110 System.out.println(" resultArrayRSA#" + i + ": " + 111 resultArrayRSA[i]); 112 } 113 114 if ((resultArrayDSA = km.getServerAliases("DSA", null)) == null) 115 throw new Exception("km.getServerAliases(DSA) == null"); 116 for (int i = 0; i < resultArrayDSA.length; i++) { 117 System.out.println(" resultArrayDSA#" + i + ": " + 118 resultArrayDSA[i]); 119 } 120 System.out.println("====="); 121 122 /* 123 * Check chooseClientAliases for null returns 124 */ 125 resultsNone = km.chooseClientAlias(nothing, null, null); 126 if (resultsNone != null) { 127 throw new Exception("km.chooseClientAlias(nothing) != null"); 128 } 129 System.out.println("km.ChooseClientAlias(nothing) passed"); 130 131 /* 132 * Check chooseClientAlias for RSA keys. 133 */ 134 resultsRsa = km.chooseClientAlias(rsa, null, null); 135 if (resultsRsa == null) { 136 throw new Exception( 137 "km.chooseClientAlias(rsa, null, null) != null"); 138 } 139 System.out.println("km.chooseClientAlias(rsa) passed"); 140 141 /* 142 * Check chooseClientAlias for DSA keys. 143 */ 144 resultsDsa = km.chooseClientAlias(dsa, null, null); 145 if (resultsDsa == null) { 146 throw new Exception( 147 "km.chooseClientAlias(dsa, null, null) != null"); 148 } 149 System.out.println("km.chooseClientAlias(dsa) passed"); 150 151 /* 152 * There should be both an rsa and a dsa entry in the 153 * keystore. 154 * 155 * Check chooseClientAlias for RSA/DSA keys and be sure 156 * the ordering is correct. 157 */ 158 resultsRsaDsa = km.chooseClientAlias(rsaDsa, null, null); 159 if ((resultsRsaDsa == null) || (resultsRsaDsa != resultsRsa)) { 160 throw new Exception("km.chooseClientAlias(rsaDsa) failed"); 161 } 162 System.out.println("km.chooseClientAlias(rsaDsa) passed"); 163 164 resultsDsaRsa = km.chooseClientAlias(dsaRsa, null, null); 165 if ((resultsDsaRsa == null) || (resultsDsaRsa != resultsDsa)) { 166 throw new Exception("km.chooseClientAlias(DsaRsa) failed"); 167 } 168 System.out.println("km.chooseClientAlias(DsaRsa) passed"); 169 170 System.out.println("====="); 171 172 /* 173 * Check chooseServerAliases for null returns 174 */ 175 resultsNone = km.chooseServerAlias("nothing", null, null); 176 if (resultsNone != null) { 177 throw new Exception("km.chooseServerAlias(\"nothing\") != null"); 178 } 179 System.out.println("km.ChooseServerAlias(\"nothing\") passed"); 180 181 /* 182 * Check chooseServerAlias for RSA keys. 183 */ 184 resultsRsa = km.chooseServerAlias("RSA", null, null); 185 if (resultsRsa == null) { 186 throw new Exception( 187 "km.chooseServerAlias(\"RSA\", null, null) != null"); 188 } 189 System.out.println("km.chooseServerAlias(\"RSA\") passed"); 190 191 /* 192 * Check chooseServerAlias for DSA keys. 193 */ 194 resultsDsa = km.chooseServerAlias("DSA", null, null); 195 if (resultsDsa == null) { 196 throw new Exception( 197 "km.chooseServerAlias(\"DSA\", null, null) != null"); 198 } 199 System.out.println("km.chooseServerAlias(\"DSA\") passed"); 200 201 } 202 } 203