Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/net/ssl/TLSv12/SignatureAlgorithms.java
41152 views
1
/*
2
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
//
27
// SunJSSE does not support dynamic system properties, no way to re-use
28
// system properties in samevm/agentvm mode.
29
//
30
31
/*
32
* @test
33
* @bug 8049321
34
* @summary Support SHA256WithDSA in JSSE
35
* @run main/othervm SignatureAlgorithms PKIX "SHA-224,SHA-256"
36
* TLS_DHE_DSS_WITH_AES_128_CBC_SHA
37
* @run main/othervm SignatureAlgorithms PKIX "SHA-1,SHA-224"
38
* TLS_DHE_DSS_WITH_AES_128_CBC_SHA
39
* @run main/othervm SignatureAlgorithms PKIX "SHA-1,SHA-256"
40
* TLS_DHE_DSS_WITH_AES_128_CBC_SHA
41
* @run main/othervm SignatureAlgorithms PKIX "SHA-224,SHA-256"
42
* TLS_DHE_DSS_WITH_AES_128_CBC_SHA256
43
* @run main/othervm SignatureAlgorithms PKIX "SHA-1,SHA-224"
44
* TLS_DHE_DSS_WITH_AES_128_CBC_SHA256
45
* @run main/othervm SignatureAlgorithms PKIX "SHA-1,SHA-256"
46
* TLS_DHE_DSS_WITH_AES_128_CBC_SHA256
47
*/
48
49
import java.net.*;
50
import java.util.*;
51
import java.io.*;
52
import javax.net.ssl.*;
53
import java.security.Security;
54
import java.security.KeyStore;
55
import java.security.KeyFactory;
56
import java.security.cert.Certificate;
57
import java.security.cert.X509Certificate;
58
import java.security.cert.CertificateFactory;
59
import java.security.spec.*;
60
import java.security.interfaces.*;
61
62
public class SignatureAlgorithms {
63
64
/*
65
* =============================================================
66
* Set the various variables needed for the tests, then
67
* specify what tests to run on each side.
68
*/
69
70
/*
71
* Should we run the client or server in a separate thread?
72
* Both sides can throw exceptions, but do you have a preference
73
* as to which side should be the main thread.
74
*/
75
static boolean separateServerThread = true;
76
77
/*
78
* Where do we find the keystores?
79
*/
80
// Certificates and key (DSA) used in the test.
81
static String trustedCertStr =
82
"-----BEGIN CERTIFICATE-----\n" +
83
"MIIDYTCCAyGgAwIBAgIJAK8/gw6zg/DPMAkGByqGSM44BAMwOzELMAkGA1UEBhMC\n" +
84
"VVMxDTALBgNVBAoTBEphdmExHTAbBgNVBAsTFFN1bkpTU0UgVGVzdCBTZXJpdmNl\n" +
85
"MB4XDTE1MTIwMzEzNTIyNVoXDTM2MTExMjEzNTIyNVowOzELMAkGA1UEBhMCVVMx\n" +
86
"DTALBgNVBAoTBEphdmExHTAbBgNVBAsTFFN1bkpTU0UgVGVzdCBTZXJpdmNlMIIB\n" +
87
"uDCCASwGByqGSM44BAEwggEfAoGBAPH+b+GSMX6KS7jXDRevzc464DFG4X+uxu5V\n" +
88
"b3U4yhsU8A8cuH4gwin6L/IDkmZQ7N0zC0jRsiGVSMsFETTq10F39pH2eBfUv/hJ\n" +
89
"cLfBnIjBEtVqV/dExK88Hul2sZ4mQihQ4issPl7hsroS9EWYicnX0oNAqAB9PO5Y\n" +
90
"zKbfpL7TAhUA13WW48rln2UP/LaAgtnzKhqcNtMCgYEA3Rv0GirTbAaor8iURd82\n" +
91
"b5FlDTevOCTuq7ZIpfZVV30neS7cBYNet6m/3/4cfUlbbrqhbqIJ2I+I81drnN0Y\n" +
92
"lyN4KkuxEcB6OTwfWkIUj6rvPaCQrBH8Q213bDq3HHtYNaP8OoeQUyVXW+SEGADC\n" +
93
"J1+z8uqP3lIB6ltdgOiV/GQDgYUAAoGBAOXRppuJSGdt6AiZkb81P1DCUgIUlZFI\n" +
94
"J9GxWrjbbHDmGllMwPNhK6dU7LJKJJuYVPW+95rUGlSJEjRqSlHuyHkNb6e3e7qx\n" +
95
"tmx1/oIyq+oLult50hBS7uBvLLR0JbIKjBzzkudL8Rjze4G/Wq7KDM2T1JOP49tW\n" +
96
"eocCvaC8h8uQo4GtMIGqMB0GA1UdDgQWBBT17HcqLllsqnZzP+kElcGcBGmubjBr\n" +
97
"BgNVHSMEZDBigBT17HcqLllsqnZzP+kElcGcBGmubqE/pD0wOzELMAkGA1UEBhMC\n" +
98
"VVMxDTALBgNVBAoTBEphdmExHTAbBgNVBAsTFFN1bkpTU0UgVGVzdCBTZXJpdmNl\n" +
99
"ggkArz+DDrOD8M8wDwYDVR0TAQH/BAUwAwEB/zALBgNVHQ8EBAMCAQYwCQYHKoZI\n" +
100
"zjgEAwMvADAsAhQ6Y1I6LtIEBMqNo8o6GIe4LLEJuwIUbVQUKi8tvtWyRoxm8AFV\n" +
101
"0axJYUU=\n" +
102
"-----END CERTIFICATE-----";
103
104
static String[] targetCertStr = {
105
// DSA-SHA1
106
"-----BEGIN CERTIFICATE-----\n" +
107
"MIIDKTCCAumgAwIBAgIJAOy5c0b+8stFMAkGByqGSM44BAMwOzELMAkGA1UEBhMC\n" +
108
"VVMxDTALBgNVBAoTBEphdmExHTAbBgNVBAsTFFN1bkpTU0UgVGVzdCBTZXJpdmNl\n" +
109
"MB4XDTE1MTIwMzEzNTIyNVoXDTM1MDgyMDEzNTIyNVowTzELMAkGA1UEBhMCVVMx\n" +
110
"DTALBgNVBAoMBEphdmExHTAbBgNVBAsMFFN1bkpTU0UgVGVzdCBTZXJpdmNlMRIw\n" +
111
"EAYDVQQDDAlsb2NhbGhvc3QwggG3MIIBLAYHKoZIzjgEATCCAR8CgYEA8f5v4ZIx\n" +
112
"fopLuNcNF6/NzjrgMUbhf67G7lVvdTjKGxTwDxy4fiDCKfov8gOSZlDs3TMLSNGy\n" +
113
"IZVIywURNOrXQXf2kfZ4F9S/+Elwt8GciMES1WpX90TErzwe6XaxniZCKFDiKyw+\n" +
114
"XuGyuhL0RZiJydfSg0CoAH087ljMpt+kvtMCFQDXdZbjyuWfZQ/8toCC2fMqGpw2\n" +
115
"0wKBgQDdG/QaKtNsBqivyJRF3zZvkWUNN684JO6rtkil9lVXfSd5LtwFg163qb/f\n" +
116
"/hx9SVtuuqFuognYj4jzV2uc3RiXI3gqS7ERwHo5PB9aQhSPqu89oJCsEfxDbXds\n" +
117
"Orcce1g1o/w6h5BTJVdb5IQYAMInX7Py6o/eUgHqW12A6JX8ZAOBhAACgYB+zYqn\n" +
118
"jJwG4GZpBIN/6qhzbp0flChsV+Trlu0SL0agAQzb6XdI/4JnO87Pgbxaxh3VNAj3\n" +
119
"3+Ghr1NLBuBfTKzJ4j9msWT3EpLupkMyNtXvBYM0iyMrll67lSjMdv++wLEw35Af\n" +
120
"/bzVcjGyA5Q0i0cuEzDmHTVfi0OydynbwSLxtKNjMGEwCwYDVR0PBAQDAgPoMB0G\n" +
121
"A1UdDgQWBBQXJI8AxM0qsYCbbkIMuI5zJ+nMEDAfBgNVHSMEGDAWgBT17HcqLlls\n" +
122
"qnZzP+kElcGcBGmubjASBgNVHREBAf8ECDAGhwR/AAABMAkGByqGSM44BAMDLwAw\n" +
123
"LAIUXgyJ0xll4FrZAKXi8bj7Kiz+SA4CFH9WCSZIBYA9lmJkiTgRS7iM/6IC\n" +
124
"-----END CERTIFICATE-----",
125
126
// DSA-SHA224
127
"-----BEGIN CERTIFICATE-----\n" +
128
"MIIDLzCCAuugAwIBAgIJAOy5c0b+8stGMAsGCWCGSAFlAwQDATA7MQswCQYDVQQG\n" +
129
"EwJVUzENMAsGA1UEChMESmF2YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2\n" +
130
"Y2UwHhcNMTUxMjAzMTU0NDM5WhcNMzUwODIwMTU0NDM5WjBPMQswCQYDVQQGEwJV\n" +
131
"UzENMAsGA1UECgwESmF2YTEdMBsGA1UECwwUU3VuSlNTRSBUZXN0IFNlcml2Y2Ux\n" +
132
"EjAQBgNVBAMMCWxvY2FsaG9zdDCCAbcwggEsBgcqhkjOOAQBMIIBHwKBgQDx/m/h\n" +
133
"kjF+iku41w0Xr83OOuAxRuF/rsbuVW91OMobFPAPHLh+IMIp+i/yA5JmUOzdMwtI\n" +
134
"0bIhlUjLBRE06tdBd/aR9ngX1L/4SXC3wZyIwRLValf3RMSvPB7pdrGeJkIoUOIr\n" +
135
"LD5e4bK6EvRFmInJ19KDQKgAfTzuWMym36S+0wIVANd1luPK5Z9lD/y2gILZ8yoa\n" +
136
"nDbTAoGBAN0b9Boq02wGqK/IlEXfNm+RZQ03rzgk7qu2SKX2VVd9J3ku3AWDXrep\n" +
137
"v9/+HH1JW266oW6iCdiPiPNXa5zdGJcjeCpLsRHAejk8H1pCFI+q7z2gkKwR/ENt\n" +
138
"d2w6txx7WDWj/DqHkFMlV1vkhBgAwidfs/Lqj95SAepbXYDolfxkA4GEAAKBgA81\n" +
139
"CJKEv+pwiqYgxtw/9rkQ9748WP3mKrEC06kjUG+94/Z9dQloNFFfj6LiO1bymc5l\n" +
140
"6QIR8XCi4Po3N80K3+WxhBGFhY+RkVWTh43JV8epb41aH2qiWErarBwBGEh8LyGT\n" +
141
"i30db+Nkz2gfvyz9H/9T0jmYgfLEOlMCusali1qHo2MwYTALBgNVHQ8EBAMCA+gw\n" +
142
"HQYDVR0OBBYEFBqSP0S4+X+zOCTEnlp2hbAjV/W5MB8GA1UdIwQYMBaAFPXsdyou\n" +
143
"WWyqdnM/6QSVwZwEaa5uMBIGA1UdEQEB/wQIMAaHBH8AAAEwCwYJYIZIAWUDBAMB\n" +
144
"AzEAMC4CFQChiRaOnAnsCSJFwdpK22jSxU/mhQIVALgLbj/G39+1Ej8UuSWnEQyU\n" +
145
"4DA+\n" +
146
"-----END CERTIFICATE-----",
147
148
// DSA-SHA256
149
"-----BEGIN CERTIFICATE-----\n" +
150
"MIIDLTCCAuugAwIBAgIJAOy5c0b+8stHMAsGCWCGSAFlAwQDAjA7MQswCQYDVQQG\n" +
151
"EwJVUzENMAsGA1UEChMESmF2YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2\n" +
152
"Y2UwHhcNMTUxMjAzMTU0NjUxWhcNMzUwODIwMTU0NjUxWjBPMQswCQYDVQQGEwJV\n" +
153
"UzENMAsGA1UECgwESmF2YTEdMBsGA1UECwwUU3VuSlNTRSBUZXN0IFNlcml2Y2Ux\n" +
154
"EjAQBgNVBAMMCWxvY2FsaG9zdDCCAbcwggEsBgcqhkjOOAQBMIIBHwKBgQDx/m/h\n" +
155
"kjF+iku41w0Xr83OOuAxRuF/rsbuVW91OMobFPAPHLh+IMIp+i/yA5JmUOzdMwtI\n" +
156
"0bIhlUjLBRE06tdBd/aR9ngX1L/4SXC3wZyIwRLValf3RMSvPB7pdrGeJkIoUOIr\n" +
157
"LD5e4bK6EvRFmInJ19KDQKgAfTzuWMym36S+0wIVANd1luPK5Z9lD/y2gILZ8yoa\n" +
158
"nDbTAoGBAN0b9Boq02wGqK/IlEXfNm+RZQ03rzgk7qu2SKX2VVd9J3ku3AWDXrep\n" +
159
"v9/+HH1JW266oW6iCdiPiPNXa5zdGJcjeCpLsRHAejk8H1pCFI+q7z2gkKwR/ENt\n" +
160
"d2w6txx7WDWj/DqHkFMlV1vkhBgAwidfs/Lqj95SAepbXYDolfxkA4GEAAKBgEF7\n" +
161
"2qiYxGrjX4KCOy0k5nK/RYlgLy4gYDChihQpiaa+fbA5JOBOxPWsh7rdtmJuDrEJ\n" +
162
"keacU223+DIhOKC49fa+EvhLNqo6U1oPn8n/yvBsvvnWkcynw5KfNzaLlaPmzugh\n" +
163
"v9xl/GhyZNAXc1QUcW3C+ceHVNrKnkfbTKZz5eRSo2MwYTALBgNVHQ8EBAMCA+gw\n" +
164
"HQYDVR0OBBYEFNMkPrt40oO9Dpy+bcbQdEvOlNlyMB8GA1UdIwQYMBaAFPXsdyou\n" +
165
"WWyqdnM/6QSVwZwEaa5uMBIGA1UdEQEB/wQIMAaHBH8AAAEwCwYJYIZIAWUDBAMC\n" +
166
"Ay8AMCwCFCvA2QiKSe/n+6GqSYQwgQ/zL5M9AhQfSiuWdMJKWpgPJKakvzhBUbMb\n" +
167
"vA==\n" +
168
"-----END CERTIFICATE-----"};
169
170
// Private key in the format of PKCS#8, key size is 1024 bits.
171
static String[] targetPrivateKey = {
172
// For cert DSA-SHA1
173
"MIIBSwIBADCCASwGByqGSM44BAEwggEfAoGBAPH+b+GSMX6KS7jXDRevzc464DFG\n" +
174
"4X+uxu5Vb3U4yhsU8A8cuH4gwin6L/IDkmZQ7N0zC0jRsiGVSMsFETTq10F39pH2\n" +
175
"eBfUv/hJcLfBnIjBEtVqV/dExK88Hul2sZ4mQihQ4issPl7hsroS9EWYicnX0oNA\n" +
176
"qAB9PO5YzKbfpL7TAhUA13WW48rln2UP/LaAgtnzKhqcNtMCgYEA3Rv0GirTbAao\n" +
177
"r8iURd82b5FlDTevOCTuq7ZIpfZVV30neS7cBYNet6m/3/4cfUlbbrqhbqIJ2I+I\n" +
178
"81drnN0YlyN4KkuxEcB6OTwfWkIUj6rvPaCQrBH8Q213bDq3HHtYNaP8OoeQUyVX\n" +
179
"W+SEGADCJ1+z8uqP3lIB6ltdgOiV/GQEFgIUOiB7J/lrFrNduQ8nDNTe8VspoAI=",
180
181
// For cert DSA-SHA224
182
"MIIBSwIBADCCASwGByqGSM44BAEwggEfAoGBAPH+b+GSMX6KS7jXDRevzc464DFG\n" +
183
"4X+uxu5Vb3U4yhsU8A8cuH4gwin6L/IDkmZQ7N0zC0jRsiGVSMsFETTq10F39pH2\n" +
184
"eBfUv/hJcLfBnIjBEtVqV/dExK88Hul2sZ4mQihQ4issPl7hsroS9EWYicnX0oNA\n" +
185
"qAB9PO5YzKbfpL7TAhUA13WW48rln2UP/LaAgtnzKhqcNtMCgYEA3Rv0GirTbAao\n" +
186
"r8iURd82b5FlDTevOCTuq7ZIpfZVV30neS7cBYNet6m/3/4cfUlbbrqhbqIJ2I+I\n" +
187
"81drnN0YlyN4KkuxEcB6OTwfWkIUj6rvPaCQrBH8Q213bDq3HHtYNaP8OoeQUyVX\n" +
188
"W+SEGADCJ1+z8uqP3lIB6ltdgOiV/GQEFgIUOj9F5mxWd9W1tiLSdsOAt8BUBzE=",
189
190
// For cert DSA-SHA256
191
"MIIBSwIBADCCASwGByqGSM44BAEwggEfAoGBAPH+b+GSMX6KS7jXDRevzc464DFG\n" +
192
"4X+uxu5Vb3U4yhsU8A8cuH4gwin6L/IDkmZQ7N0zC0jRsiGVSMsFETTq10F39pH2\n" +
193
"eBfUv/hJcLfBnIjBEtVqV/dExK88Hul2sZ4mQihQ4issPl7hsroS9EWYicnX0oNA\n" +
194
"qAB9PO5YzKbfpL7TAhUA13WW48rln2UP/LaAgtnzKhqcNtMCgYEA3Rv0GirTbAao\n" +
195
"r8iURd82b5FlDTevOCTuq7ZIpfZVV30neS7cBYNet6m/3/4cfUlbbrqhbqIJ2I+I\n" +
196
"81drnN0YlyN4KkuxEcB6OTwfWkIUj6rvPaCQrBH8Q213bDq3HHtYNaP8OoeQUyVX\n" +
197
"W+SEGADCJ1+z8uqP3lIB6ltdgOiV/GQEFgIUQ2WGgg+OO39Aujj0e4lM4pP4/9g="};
198
199
200
static char passphrase[] = "passphrase".toCharArray();
201
202
/*
203
* Turn on SSL debugging?
204
*/
205
static boolean debug = false;
206
207
/*
208
* Is the server ready to serve?
209
*/
210
volatile boolean serverReady = false;
211
212
/*
213
* Define the server side of the test.
214
*
215
* If the server prematurely exits, serverReady will be set to true
216
* to avoid infinite hangs.
217
*/
218
void doServerSide() throws Exception {
219
220
SSLContext context = generateSSLContext(
221
null, targetCertStr, targetPrivateKey);
222
SSLServerSocketFactory sslssf = context.getServerSocketFactory();
223
try (SSLServerSocket sslServerSocket =
224
(SSLServerSocket)sslssf.createServerSocket(serverPort)) {
225
226
serverPort = sslServerSocket.getLocalPort();
227
228
/*
229
* Signal Client, we're ready for his connect.
230
*/
231
serverReady = true;
232
233
try (SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept()) {
234
sslSocket.setEnabledCipherSuites(
235
sslSocket.getSupportedCipherSuites());
236
InputStream sslIS = sslSocket.getInputStream();
237
OutputStream sslOS = sslSocket.getOutputStream();
238
239
sslIS.read();
240
sslOS.write('A');
241
sslOS.flush();
242
243
dumpSignatureAlgorithms(sslSocket);
244
}
245
}
246
}
247
248
/*
249
* Define the client side of the test.
250
*
251
* If the server prematurely exits, serverReady will be set to true
252
* to avoid infinite hangs.
253
*/
254
void doClientSide() throws Exception {
255
256
/*
257
* Wait for server to get started.
258
*/
259
while (!serverReady) {
260
Thread.sleep(50);
261
}
262
263
SSLContext context = generateSSLContext(trustedCertStr, null, null);
264
SSLSocketFactory sslsf = context.getSocketFactory();
265
266
try (SSLSocket sslSocket =
267
(SSLSocket)sslsf.createSocket("localhost", serverPort)) {
268
269
// enable TLSv1.2 only
270
sslSocket.setEnabledProtocols(new String[] {"TLSv1.2"});
271
272
// enable a block cipher
273
sslSocket.setEnabledCipherSuites(new String[] {cipherSuite});
274
275
InputStream sslIS = sslSocket.getInputStream();
276
OutputStream sslOS = sslSocket.getOutputStream();
277
278
sslOS.write('B');
279
sslOS.flush();
280
sslIS.read();
281
282
dumpSignatureAlgorithms(sslSocket);
283
}
284
}
285
286
static void dumpSignatureAlgorithms(SSLSocket sslSocket) throws Exception {
287
288
boolean isClient = sslSocket.getUseClientMode();
289
String mode = "[" + (isClient ? "Client" : "Server") + "]";
290
ExtendedSSLSession session =
291
(ExtendedSSLSession)sslSocket.getSession();
292
String[] signAlgs = session.getLocalSupportedSignatureAlgorithms();
293
System.out.println(
294
mode + " local supported signature algorithms: " +
295
Arrays.asList(signAlgs));
296
297
if (!isClient) {
298
signAlgs = session.getPeerSupportedSignatureAlgorithms();
299
System.out.println(
300
mode + " peer supported signature algorithms: " +
301
Arrays.asList(signAlgs));
302
} else {
303
Certificate[] serverCerts = session.getPeerCertificates();
304
305
// server should always send the authentication cert.
306
String sigAlg = ((X509Certificate)serverCerts[0]).getSigAlgName();
307
System.out.println(
308
mode + " the signature algorithm of server certificate: " +
309
sigAlg);
310
if (sigAlg.contains("SHA1")) {
311
if (disabledAlgorithms.contains("SHA-1")) {
312
throw new Exception(
313
"Not the expected server certificate. " +
314
"SHA-1 should be disabled");
315
}
316
} else if (sigAlg.contains("SHA224")) {
317
if (disabledAlgorithms.contains("SHA-224")) {
318
throw new Exception(
319
"Not the expected server certificate. " +
320
"SHA-224 should be disabled");
321
}
322
} else { // SHA-256
323
if (disabledAlgorithms.contains("SHA-256")) {
324
throw new Exception(
325
"Not the expected server certificate. " +
326
"SHA-256 should be disabled");
327
}
328
}
329
}
330
}
331
332
/*
333
* =============================================================
334
* The remainder is just support stuff
335
*/
336
private static String tmAlgorithm; // trust manager
337
private static String disabledAlgorithms; // disabled algorithms
338
private static String cipherSuite; // cipher suite
339
340
private static void parseArguments(String[] args) {
341
tmAlgorithm = args[0];
342
disabledAlgorithms = args[1];
343
cipherSuite = args[2];
344
}
345
346
private static SSLContext generateSSLContext(String trustedCertStr,
347
String[] keyCertStrs, String[] keySpecStrs) throws Exception {
348
349
// generate certificate from cert string
350
CertificateFactory cf = CertificateFactory.getInstance("X.509");
351
352
// create a key store
353
KeyStore ks = KeyStore.getInstance("JKS");
354
ks.load(null, null);
355
356
// import the trused cert
357
Certificate trusedCert = null;
358
ByteArrayInputStream is = null;
359
if (trustedCertStr != null) {
360
is = new ByteArrayInputStream(trustedCertStr.getBytes());
361
trusedCert = cf.generateCertificate(is);
362
is.close();
363
364
ks.setCertificateEntry("DSA Signer", trusedCert);
365
}
366
367
if (keyCertStrs != null && keyCertStrs.length != 0) {
368
for (int i = 0; i < keyCertStrs.length; i++) {
369
String keyCertStr = keyCertStrs[i];
370
String keySpecStr = keySpecStrs[i];
371
372
// generate the private key.
373
PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(
374
Base64.getMimeDecoder().decode(keySpecStr));
375
KeyFactory kf = KeyFactory.getInstance("DSA");
376
DSAPrivateKey priKey =
377
(DSAPrivateKey)kf.generatePrivate(priKeySpec);
378
379
// generate certificate chain
380
is = new ByteArrayInputStream(keyCertStr.getBytes());
381
Certificate keyCert = cf.generateCertificate(is);
382
is.close();
383
384
Certificate[] chain = null;
385
if (trusedCert != null) {
386
chain = new Certificate[2];
387
chain[0] = keyCert;
388
chain[1] = trusedCert;
389
} else {
390
chain = new Certificate[1];
391
chain[0] = keyCert;
392
}
393
394
// import the key entry.
395
ks.setKeyEntry("DSA Entry " + i, priKey, passphrase, chain);
396
}
397
}
398
399
// create SSL context
400
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmAlgorithm);
401
tmf.init(ks);
402
403
SSLContext ctx = SSLContext.getInstance("TLS");
404
if (keyCertStrs != null && keyCertStrs.length != 0) {
405
KeyManagerFactory kmf = KeyManagerFactory.getInstance("NewSunX509");
406
kmf.init(ks, passphrase);
407
408
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
409
ks = null;
410
} else {
411
ctx.init(null, tmf.getTrustManagers(), null);
412
}
413
414
return ctx;
415
}
416
417
418
// use any free port by default
419
volatile int serverPort = 0;
420
421
volatile Exception serverException = null;
422
volatile Exception clientException = null;
423
424
public static void main(String[] args) throws Exception {
425
/*
426
* debug option
427
*/
428
if (debug) {
429
System.setProperty("javax.net.debug", "all");
430
}
431
432
/*
433
* Get the customized arguments.
434
*/
435
parseArguments(args);
436
437
438
/*
439
* Ignore testing on Windows if only SHA-224 is available.
440
*/
441
if ((Security.getProvider("SunMSCAPI") != null) &&
442
(disabledAlgorithms.contains("SHA-1")) &&
443
(disabledAlgorithms.contains("SHA-256"))) {
444
445
System.out.println(
446
"Windows system does not support SHA-224 algorithms yet. " +
447
"Ignore the testing");
448
449
return;
450
}
451
452
/*
453
* Expose the target algorithms by diabling unexpected algorithms.
454
*/
455
Security.setProperty(
456
"jdk.certpath.disabledAlgorithms", disabledAlgorithms);
457
458
/*
459
* Reset the security property to make sure that the algorithms
460
* and keys used in this test are not disabled by default.
461
*/
462
Security.setProperty( "jdk.tls.disabledAlgorithms", "");
463
464
/*
465
* Start the tests.
466
*/
467
new SignatureAlgorithms();
468
}
469
470
Thread clientThread = null;
471
Thread serverThread = null;
472
473
/*
474
* Primary constructor, used to drive remainder of the test.
475
*
476
* Fork off the other side, then do your work.
477
*/
478
SignatureAlgorithms() throws Exception {
479
try {
480
if (separateServerThread) {
481
startServer(true);
482
startClient(false);
483
} else {
484
startClient(true);
485
startServer(false);
486
}
487
} catch (Exception e) {
488
// swallow for now. Show later
489
}
490
491
/*
492
* Wait for other side to close down.
493
*/
494
if (separateServerThread) {
495
serverThread.join();
496
} else {
497
clientThread.join();
498
}
499
500
/*
501
* When we get here, the test is pretty much over.
502
* Which side threw the error?
503
*/
504
Exception local;
505
Exception remote;
506
String whichRemote;
507
508
if (separateServerThread) {
509
remote = serverException;
510
local = clientException;
511
whichRemote = "server";
512
} else {
513
remote = clientException;
514
local = serverException;
515
whichRemote = "client";
516
}
517
518
/*
519
* If both failed, return the curthread's exception, but also
520
* print the remote side Exception
521
*/
522
if ((local != null) && (remote != null)) {
523
System.out.println(whichRemote + " also threw:");
524
remote.printStackTrace();
525
System.out.println();
526
throw local;
527
}
528
529
if (remote != null) {
530
throw remote;
531
}
532
533
if (local != null) {
534
throw local;
535
}
536
}
537
538
void startServer(boolean newThread) throws Exception {
539
if (newThread) {
540
serverThread = new Thread() {
541
public void run() {
542
try {
543
doServerSide();
544
} catch (Exception e) {
545
/*
546
* Our server thread just died.
547
*
548
* Release the client, if not active already...
549
*/
550
System.err.println("Server died..." + e);
551
serverReady = true;
552
serverException = e;
553
}
554
}
555
};
556
serverThread.start();
557
} else {
558
try {
559
doServerSide();
560
} catch (Exception e) {
561
serverException = e;
562
} finally {
563
serverReady = true;
564
}
565
}
566
}
567
568
void startClient(boolean newThread) throws Exception {
569
if (newThread) {
570
clientThread = new Thread() {
571
public void run() {
572
try {
573
doClientSide();
574
} catch (Exception e) {
575
/*
576
* Our client thread just died.
577
*/
578
System.err.println("Client died..." + e);
579
clientException = e;
580
}
581
}
582
};
583
clientThread.start();
584
} else {
585
try {
586
doClientSide();
587
} catch (Exception e) {
588
clientException = e;
589
}
590
}
591
}
592
}
593
594