author | xuelei |
Mon, 25 Jun 2018 13:41:39 -0700 | |
changeset 50768 | 68fa3d4026ea |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
50768 | 2 |
* Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
import java.io.*; |
|
25 |
import java.net.*; |
|
26 |
||
27 |
import java.security.cert.Certificate; |
|
28 |
||
29 |
import javax.net.ssl.*; |
|
30 |
||
31 |
class JSSEClient extends CipherTest.Client { |
|
32 |
||
33 |
private final SSLContext sslContext; |
|
34 |
private final MyX509KeyManager keyManager; |
|
35 |
||
36 |
JSSEClient(CipherTest cipherTest) throws Exception { |
|
37 |
super(cipherTest); |
|
38 |
this.keyManager = new MyX509KeyManager(CipherTest.keyManager); |
|
39 |
sslContext = SSLContext.getInstance("TLS"); |
|
40 |
} |
|
41 |
||
42 |
void runTest(CipherTest.TestParameters params) throws Exception { |
|
43 |
SSLSocket socket = null; |
|
44 |
try { |
|
45 |
keyManager.setAuthType(params.clientAuth); |
|
43106
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
46 |
sslContext.init( |
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
47 |
new KeyManager[] { keyManager }, |
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
48 |
new TrustManager[] { CipherTest.trustManager }, |
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
49 |
CipherTest.secureRandom); |
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
50 |
SSLSocketFactory factory |
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
51 |
= (SSLSocketFactory) sslContext.getSocketFactory(); |
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
52 |
|
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
53 |
socket = (SSLSocket) factory.createSocket(); |
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
54 |
try { |
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
55 |
socket.connect(new InetSocketAddress("127.0.0.1", |
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
56 |
CipherTest.serverPort), 15000); |
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
57 |
} catch (IOException ioe) { |
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
58 |
// The server side may be impacted by naughty test cases or |
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
59 |
// third party routines, and cannot accept connections. |
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
60 |
// |
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
61 |
// Just ignore the test if the connection cannot be |
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
62 |
// established. |
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
63 |
System.out.println( |
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
64 |
"Cannot make a connection in 15 seconds. " + |
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
65 |
"Ignore in client side."); |
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
66 |
return; |
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
67 |
} |
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
68 |
|
4a7670d316ee
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents:
5506
diff
changeset
|
69 |
socket.setSoTimeout(CipherTest.TIMEOUT); |
50768 | 70 |
socket.setEnabledCipherSuites(new String[] {params.cipherSuite.name()}); |
71 |
socket.setEnabledProtocols(new String[] {params.protocol.name}); |
|
2 | 72 |
InputStream in = socket.getInputStream(); |
73 |
OutputStream out = socket.getOutputStream(); |
|
74 |
sendRequest(in, out); |
|
75 |
socket.close(); |
|
76 |
SSLSession session = socket.getSession(); |
|
77 |
session.invalidate(); |
|
78 |
String cipherSuite = session.getCipherSuite(); |
|
50768 | 79 |
if (!params.cipherSuite.name().equals(cipherSuite)) { |
2 | 80 |
throw new Exception("Negotiated ciphersuite mismatch: " + cipherSuite + " != " + params.cipherSuite); |
81 |
} |
|
82 |
String protocol = session.getProtocol(); |
|
50768 | 83 |
if (!params.protocol.name.equals(protocol)) { |
2 | 84 |
throw new Exception("Negotiated protocol mismatch: " + protocol + " != " + params.protocol); |
85 |
} |
|
86 |
if (cipherSuite.indexOf("DH_anon") == -1) { |
|
87 |
session.getPeerCertificates(); |
|
88 |
} |
|
89 |
Certificate[] certificates = session.getLocalCertificates(); |
|
90 |
if (params.clientAuth == null) { |
|
91 |
if (certificates != null) { |
|
92 |
throw new Exception("Local certificates should be null"); |
|
93 |
} |
|
94 |
} else { |
|
95 |
if ((certificates == null) || (certificates.length == 0)) { |
|
96 |
throw new Exception("Certificates missing"); |
|
97 |
} |
|
98 |
String keyAlg = certificates[0].getPublicKey().getAlgorithm(); |
|
99 |
if (keyAlg.equals("EC")) { |
|
100 |
keyAlg = "ECDSA"; |
|
101 |
} |
|
102 |
if (params.clientAuth != keyAlg) { |
|
103 |
throw new Exception("Certificate type mismatch: " + keyAlg + " != " + params.clientAuth); |
|
104 |
} |
|
105 |
} |
|
106 |
} finally { |
|
107 |
if (socket != null) { |
|
108 |
socket.close(); |
|
109 |
} |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
} |