Path: blob/master/test/jdk/javax/net/ssl/sanity/interop/JSSEClient.java
41154 views
/*1* Copyright (c) 2002, 2018, 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.InputStream;24import java.io.OutputStream;25import java.security.cert.Certificate;2627import javax.net.ssl.KeyManager;28import javax.net.ssl.SSLContext;29import javax.net.ssl.SSLSession;30import javax.net.ssl.SSLSocket;31import javax.net.ssl.SSLSocketFactory;32import javax.net.ssl.TrustManager;3334class JSSEClient extends CipherTest.Client {3536private final SSLContext sslContext;37private final MyX509KeyManager keyManager;3839JSSEClient(CipherTest cipherTest) throws Exception {40super(cipherTest);41this.keyManager = new MyX509KeyManager(CipherTest.keyManager);42sslContext = SSLContext.getInstance("TLS");43}4445void runTest(CipherTest.TestParameters params) throws Exception {46SSLSocket socket = null;47try {48keyManager.setAuthType(params.clientAuth);49sslContext.init(50new KeyManager[] { keyManager },51new TrustManager[] { CipherTest.trustManager },52CipherTest.secureRandom);53SSLSocketFactory factory = (SSLSocketFactory)sslContext.getSocketFactory();54socket = (SSLSocket)factory.createSocket("127.0.0.1", CipherTest.serverPort);55socket.setSoTimeout(CipherTest.TIMEOUT);56socket.setEnabledCipherSuites(new String[] { params.cipherSuite.name() });57socket.setEnabledProtocols(new String[] { params.protocol.name });58InputStream in = socket.getInputStream();59OutputStream out = socket.getOutputStream();60sendRequest(in, out);61socket.close();62SSLSession session = socket.getSession();63session.invalidate();64String cipherSuite = session.getCipherSuite();65if (!params.cipherSuite.name().equals(cipherSuite)) {66throw new Exception("Negotiated ciphersuite mismatch: "67+ cipherSuite + " != " + params.cipherSuite);68}69String protocol = session.getProtocol();70if (!params.protocol.name.equals(protocol)) {71throw new Exception("Negotiated protocol mismatch: " + protocol72+ " != " + params.protocol);73}74if (cipherSuite.indexOf("DH_anon") == -1) {75session.getPeerCertificates();76}77Certificate[] certificates = session.getLocalCertificates();78if (params.clientAuth == null) {79if (certificates != null) {80throw new Exception("Local certificates should be null");81}82} else {83if ((certificates == null) || (certificates.length == 0)) {84throw new Exception("Certificates missing");85}86String keyAlg = certificates[0].getPublicKey().getAlgorithm();87if (params.clientAuth != keyAlg) {88throw new Exception("Certificate type mismatch: " + keyAlg + " != " + params.clientAuth);89}90}91} finally {92if (socket != null) {93socket.close();94}95}96}9798}99100101