Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/fips/JSSEClient.java
38855 views
/*1* Copyright (c) 2002, 2005, 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*/2223import java.io.*;24import java.net.*;25import java.util.*;2627import java.security.*;28import java.security.cert.*;29import java.security.cert.Certificate;3031import javax.net.ssl.*;3233class JSSEClient extends CipherTest.Client {3435private final SSLContext sslContext;36private final MyX509KeyManager keyManager;3738JSSEClient(CipherTest cipherTest) throws Exception {39super(cipherTest);40this.keyManager = new MyX509KeyManager(CipherTest.keyManager);41sslContext = SSLContext.getInstance("TLS");42}4344void runTest(CipherTest.TestParameters params) throws Exception {45SSLSocket socket = null;46try {47keyManager.setAuthType(params.clientAuth);48sslContext.init(new KeyManager[] {CipherTest.keyManager}, new TrustManager[] {cipherTest.trustManager}, cipherTest.secureRandom);49SSLSocketFactory factory = (SSLSocketFactory)sslContext.getSocketFactory();50socket = (SSLSocket)factory.createSocket("127.0.0.1", cipherTest.serverPort);51socket.setSoTimeout(cipherTest.TIMEOUT);52socket.setEnabledCipherSuites(new String[] {params.cipherSuite});53socket.setEnabledProtocols(new String[] {params.protocol});54InputStream in = socket.getInputStream();55OutputStream out = socket.getOutputStream();56sendRequest(in, out);57socket.close();58SSLSession session = socket.getSession();59session.invalidate();60String cipherSuite = session.getCipherSuite();61if (params.cipherSuite.equals(cipherSuite) == false) {62throw new Exception("Negotiated ciphersuite mismatch: " + cipherSuite + " != " + params.cipherSuite);63}64String protocol = session.getProtocol();65if (params.protocol.equals(protocol) == false) {66throw new Exception("Negotiated protocol mismatch: " + protocol + " != " + params.protocol);67}68if (cipherSuite.indexOf("DH_anon") == -1) {69session.getPeerCertificates();70}71Certificate[] certificates = session.getLocalCertificates();72if (params.clientAuth == null) {73if (certificates != null) {74throw new Exception("Local certificates should be null");75}76} else {77if ((certificates == null) || (certificates.length == 0)) {78throw new Exception("Certificates missing");79}80String keyAlg = certificates[0].getPublicKey().getAlgorithm();81if (params.clientAuth != keyAlg) {82throw new Exception("Certificate type mismatch: " + keyAlg + " != " + params.clientAuth);83}84}85} finally {86if (socket != null) {87socket.close();88}89}90}9192}939495