Path: blob/master/test/jdk/sun/security/ssl/X509TrustManagerImpl/CheckNullEntity.java
41152 views
/*1* Copyright (c) 2005, 2019, 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* @test25* @bug 505381526* @summary unspecified exceptions in X509TrustManager.checkClient[Server]Truste27d28* @author Xuelei Fan29*/3031import java.io.*;32import java.net.*;33import javax.net.ssl.*;34import java.security.cert.X509Certificate;35import java.security.*;36import java.util.Enumeration;3738public class CheckNullEntity {3940/*41* =============================================================42* Set the various variables needed for the tests, then43* specify what tests to run on each side.44*/4546/*47* Should we run the client or server in a separate thread?48* Both sides can throw exceptions, but do you have a preference49* as to which side should be the main thread.50*/51static boolean separateServerThread = true;5253/*54* Where do we find the keystores?55*/56static String pathToStores = "../../../../javax/net/ssl/etc";57static String keyStoreFile = "keystore";58static String trustStoreFile = "truststore";59static String passwd = "passphrase";6061private void initialize() throws Exception {62String trustFilename =63System.getProperty("test.src", "./") + "/" + pathToStores +64"/" + trustStoreFile;65char[] passphrase = "passphrase".toCharArray();6667KeyStore ks = KeyStore.getInstance("JKS");68ks.load(new FileInputStream(trustFilename), passphrase);6970for (Enumeration e = ks.aliases() ; e.hasMoreElements() ;) {71String alias = (String)e.nextElement();72if (ks.isCertificateEntry(alias)) {73certChain[0] = (X509Certificate)ks.getCertificate(alias);74break;75}76}7778TrustManagerFactory tmf =79TrustManagerFactory.getInstance("SunX509");80tmf.init(ks);8182trustManager = (X509TrustManager)(tmf.getTrustManagers())[0];83}8485/*86* =============================================================87* The remainder is just support stuff88*/89public static void main(String[] args) throws Exception {90/*91* Start the tests.92*/93new CheckNullEntity();94}9596X509Certificate[] certChain = {null, null};97X509TrustManager trustManager = null;9899/*100* Primary constructor, used to drive remainder of the test.101*102* Fork off the other side, then do your work.103*/104CheckNullEntity() throws Exception {105String authType = "RSA";106int failed = 0x3F; // indicate six tests for normal TM107int extFailed = 0x3F; // indicate six tests for extended TM108109initialize();110try {111try {112trustManager.checkClientTrusted(certChain, (String)null);113} catch (IllegalArgumentException iae) {114// get the right exception115failed >>= 1;116}117118try {119trustManager.checkServerTrusted(certChain, (String)null);120} catch (IllegalArgumentException iae) {121// get the right exception122failed >>= 1;123}124125try {126trustManager.checkClientTrusted(certChain, "");127} catch (IllegalArgumentException iae) {128// get the right exception129failed >>= 1;130}131132try {133trustManager.checkServerTrusted(certChain, "");134} catch (IllegalArgumentException iae) {135// get the right exception136failed >>= 1;137}138139try {140trustManager.checkClientTrusted(null, authType);141} catch (IllegalArgumentException iae) {142// get the right exception143failed >>= 1;144}145146try {147trustManager.checkServerTrusted(null, authType);148} catch (IllegalArgumentException iae) {149// get the right exception150failed >>= 1;151}152153if (trustManager instanceof X509ExtendedTrustManager) {154try {155((X509ExtendedTrustManager)trustManager).checkClientTrusted(156certChain, (String)null, (Socket)null);157} catch (IllegalArgumentException iae) {158// get the right exception159extFailed >>= 1;160}161162try {163((X509ExtendedTrustManager)trustManager).checkServerTrusted(164certChain, (String)null, (Socket)null);165} catch (IllegalArgumentException iae) {166// get the right exception167extFailed >>= 1;168}169170try {171((X509ExtendedTrustManager)trustManager).checkClientTrusted(172certChain, "", (Socket)null);173} catch (IllegalArgumentException iae) {174// get the right exception175extFailed >>= 1;176}177178try {179((X509ExtendedTrustManager)trustManager).checkServerTrusted(180certChain, "", (Socket)null);181} catch (IllegalArgumentException iae) {182// get the right exception183extFailed >>= 1;184}185186try {187((X509ExtendedTrustManager)trustManager).checkClientTrusted(188null, authType, (Socket)null);189} catch (IllegalArgumentException iae) {190// get the right exception191extFailed >>= 1;192}193194try {195((X509ExtendedTrustManager)trustManager).checkServerTrusted(196null, authType, (Socket)null);197} catch (IllegalArgumentException iae) {198// get the right exception199extFailed >>= 1;200}201} else {202extFailed = 0;203}204} catch (NullPointerException npe) {205// IllegalArgumentException should be thrown206failed = 1;207} catch (Exception e) {208// ignore209System.out.println("Got another exception e" + e);210}211212if (failed != 0 || extFailed != 0) {213throw new Exception("Should throw IllegalArgumentException");214}215}216}217218219