Path: blob/master/test/jdk/sun/security/ssl/X509KeyManager/PreferredKey.java
41152 views
/*1* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223//24// Security properties, once set, cannot revert to unset. To avoid25// conflicts with tests running in the same VM isolate this test by26// running it in otherVM mode.27//2829/*30* @test31* @bug 630264432* @summary X509KeyManager implementation for NewSunX509 doesn't return most33* preferable key34* @run main/othervm PreferredKey35*/36import java.io.*;37import java.net.*;38import java.security.*;39import javax.net.ssl.*;4041public class PreferredKey {4243/*44* =============================================================45* Set the various variables needed for the tests, then46* specify what tests to run on each side.47*/4849/*50* Where do we find the keystores?51*/52static String pathToStores = "../../../../javax/net/ssl/etc";53static String keyStoreFile = "keystore";54static String passwd = "passphrase";555657public static void main(String[] args) throws Exception {58// MD5 is used in this test case, don't disable MD5 algorithm.59Security.setProperty("jdk.certpath.disabledAlgorithms",60"MD2, RSA keySize < 1024");61Security.setProperty("jdk.tls.disabledAlgorithms",62"SSLv3, RC4, DH keySize < 768");6364KeyStore ks;65KeyManagerFactory kmf;66X509KeyManager km;6768String keyFilename =69System.getProperty("test.src", ".") + "/" + pathToStores +70"/" + keyStoreFile;71char [] password = passwd.toCharArray();7273ks = KeyStore.getInstance("JKS");74ks.load(new FileInputStream(keyFilename), password);75kmf = KeyManagerFactory.getInstance("NewSunX509");76kmf.init(ks, password);77km = (X509KeyManager) kmf.getKeyManagers()[0];7879/*80* There should be both an rsa and a dsa entry in the81* keystore, otherwise the test will no work.82*/83String[] aliases = km.getClientAliases("RSA", null);84String alias = km.chooseClientAlias(new String[] {"RSA", "DSA"},85null, null);8687// there're should both be null or nonnull88if (aliases != null || alias != null) {89String algorithm = km.getPrivateKey(alias).getAlgorithm();90if (!algorithm.equals("RSA") || !algorithm.equals(91km.getPrivateKey(aliases[0]).getAlgorithm())) {92throw new Exception("Failed to get the preferable key aliases");93}94}9596aliases = km.getClientAliases("DSA", null);97alias = km.chooseClientAlias(new String[] {"DSA", "RSA"},98null, null);99100// there're should both be null or nonnull101if (aliases != null || alias != null) {102String algorithm = km.getPrivateKey(alias).getAlgorithm();103if (!algorithm.equals("DSA") || !algorithm.equals(104km.getPrivateKey(aliases[0]).getAlgorithm())) {105throw new Exception("Failed to get the preferable key aliases");106}107}108}109}110111112