Path: blob/master/test/jdk/sun/security/pkcs11/sslecc/JSSEClient.java
41152 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.*;24import java.net.*;2526import java.security.cert.Certificate;2728import javax.net.ssl.*;2930class JSSEClient extends CipherTest.Client {3132private final SSLContext sslContext;33private final MyX509KeyManager keyManager;3435JSSEClient(CipherTest cipherTest) throws Exception {36super(cipherTest);37this.keyManager = new MyX509KeyManager(CipherTest.keyManager);38sslContext = SSLContext.getInstance("TLS");39}4041void runTest(CipherTest.TestParameters params) throws Exception {42SSLSocket socket = null;43try {44keyManager.setAuthType(params.clientAuth);45sslContext.init(46new KeyManager[] { keyManager },47new TrustManager[] { CipherTest.trustManager },48CipherTest.secureRandom);49SSLSocketFactory factory50= (SSLSocketFactory) sslContext.getSocketFactory();5152socket = (SSLSocket) factory.createSocket();53try {54socket.connect(new InetSocketAddress("127.0.0.1",55CipherTest.serverPort), 15000);56} catch (IOException ioe) {57// The server side may be impacted by naughty test cases or58// third party routines, and cannot accept connections.59//60// Just ignore the test if the connection cannot be61// established.62System.out.println(63"Cannot make a connection in 15 seconds. " +64"Ignore in client side.");65return;66}6768socket.setSoTimeout(CipherTest.TIMEOUT);69socket.setEnabledCipherSuites(new String[] {params.cipherSuite.name()});70socket.setEnabledProtocols(new String[] {params.protocol.name});71InputStream in = socket.getInputStream();72OutputStream out = socket.getOutputStream();73sendRequest(in, out);74socket.close();75SSLSession session = socket.getSession();76session.invalidate();77String cipherSuite = session.getCipherSuite();78if (!params.cipherSuite.name().equals(cipherSuite)) {79throw new Exception("Negotiated ciphersuite mismatch: " + cipherSuite + " != " + params.cipherSuite);80}81String protocol = session.getProtocol();82if (!params.protocol.name.equals(protocol)) {83throw new Exception("Negotiated protocol mismatch: " + protocol + " != " + params.protocol);84}85if (cipherSuite.indexOf("DH_anon") == -1) {86session.getPeerCertificates();87}88Certificate[] certificates = session.getLocalCertificates();89if (params.clientAuth == null) {90if (certificates != null) {91throw new Exception("Local certificates should be null");92}93} else {94if ((certificates == null) || (certificates.length == 0)) {95throw new Exception("Certificates missing");96}97String keyAlg = certificates[0].getPublicKey().getAlgorithm();98if (keyAlg.equals("EC")) {99keyAlg = "ECDSA";100}101if (params.clientAuth != keyAlg) {102throw new Exception("Certificate type mismatch: " + keyAlg + " != " + params.clientAuth);103}104}105} finally {106if (socket != null) {107socket.close();108}109}110}111112}113114115