Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/ssl/SignatureScheme/SigAlgosExtTestWithTLS12.java
41152 views
1
/*
2
* Copyright (C) 2021 THL A29 Limited, a Tencent company. 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.
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
*
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.
22
*/
23
24
/*
25
* @test 8263188
26
* @summary If TLS the server and client has no common signature algorithms,
27
* the connection should fail fast with "No supported signature algorithm".
28
* This test only covers TLS 1.2.
29
*
30
* @library /test/lib
31
* /javax/net/ssl/templates
32
*
33
* @run main/othervm
34
* -Djdk.tls.server.SignatureSchemes=ecdsa_secp384r1_sha384
35
* -Djdk.tls.client.SignatureSchemes=ecdsa_secp256r1_sha256,ecdsa_secp384r1_sha384
36
* -Dtest.clientAuth=false
37
* -Dtest.expectFail=false
38
* SigAlgosExtTestWithTLS12
39
* @run main/othervm
40
* -Djdk.tls.server.SignatureSchemes=ecdsa_secp384r1_sha384
41
* -Djdk.tls.client.SignatureSchemes=ecdsa_secp256r1_sha256
42
* -Dtest.clientAuth=false
43
* -Dtest.expectFail=true
44
* SigAlgosExtTestWithTLS12
45
* @run main/othervm
46
* -Djdk.tls.server.SignatureSchemes=ecdsa_secp256r1_sha256
47
* -Djdk.tls.client.SignatureSchemes=ecdsa_secp256r1_sha256
48
* -Dtest.clientAuth=true
49
* -Dtest.expectFail=true
50
* SigAlgosExtTestWithTLS12
51
*/
52
53
import javax.net.ssl.*;
54
import java.nio.ByteBuffer;
55
import java.util.*;
56
57
public class SigAlgosExtTestWithTLS12 extends SSLEngineTemplate {
58
59
private static final boolean CLIENT_AUTH
60
= Boolean.getBoolean("test.clientAuth");
61
private static final boolean EXPECT_FAIL
62
= Boolean.getBoolean("test.expectFail");
63
64
private static final String[] CA_CERTS = new String[] {
65
// SHA256withECDSA, curve secp256r1
66
// Validity
67
// Not Before: May 22 07:18:16 2018 GMT
68
// Not After : May 17 07:18:16 2038 GMT
69
// Subject Key Identifier:
70
// 60:CF:BD:73:FF:FA:1A:30:D2:A4:EC:D3:49:71:46:EF:1A:35:A0:86
71
"-----BEGIN CERTIFICATE-----\n" +
72
"MIIBvjCCAWOgAwIBAgIJAIvFG6GbTroCMAoGCCqGSM49BAMCMDsxCzAJBgNVBAYT\n" +
73
"AlVTMQ0wCwYDVQQKDARKYXZhMR0wGwYDVQQLDBRTdW5KU1NFIFRlc3QgU2VyaXZj\n" +
74
"ZTAeFw0xODA1MjIwNzE4MTZaFw0zODA1MTcwNzE4MTZaMDsxCzAJBgNVBAYTAlVT\n" +
75
"MQ0wCwYDVQQKDARKYXZhMR0wGwYDVQQLDBRTdW5KU1NFIFRlc3QgU2VyaXZjZTBZ\n" +
76
"MBMGByqGSM49AgEGCCqGSM49AwEHA0IABBz1WeVb6gM2mh85z3QlvaB/l11b5h0v\n" +
77
"LIzmkC3DKlVukZT+ltH2Eq1oEkpXuf7QmbM0ibrUgtjsWH3mULfmcWmjUDBOMB0G\n" +
78
"A1UdDgQWBBRgz71z//oaMNKk7NNJcUbvGjWghjAfBgNVHSMEGDAWgBRgz71z//oa\n" +
79
"MNKk7NNJcUbvGjWghjAMBgNVHRMEBTADAQH/MAoGCCqGSM49BAMCA0kAMEYCIQCG\n" +
80
"6wluh1r2/T6L31mZXRKf9JxeSf9pIzoLj+8xQeUChQIhAJ09wAi1kV8yePLh2FD9\n" +
81
"2YEHlSQUAbwwqCDEVB5KxaqP\n" +
82
"-----END CERTIFICATE-----",
83
84
// SHA384withECDSA, curve secp384r1
85
// Validity
86
// Not Before: Jun 24 08:15:06 2019 GMT
87
// Not After : Jun 19 08:15:06 2039 GMT
88
// Subject Key Identifier:
89
// 0a:93:a9:a0:bf:e7:d5:48:9d:4f:89:15:c6:51:98:80:05:51:4e:4e
90
"-----BEGIN CERTIFICATE-----\n" +
91
"MIICCDCCAY6gAwIBAgIUCpOpoL/n1UidT4kVxlGYgAVRTk4wCgYIKoZIzj0EAwMw\n" +
92
"OzELMAkGA1UEBhMCVVMxDTALBgNVBAoMBEphdmExHTAbBgNVBAsMFFN1bkpTU0Ug\n" +
93
"VGVzdCBTZXJpdmNlMB4XDTE5MDYyNDA4MTUwNloXDTM5MDYxOTA4MTUwNlowOzEL\n" +
94
"MAkGA1UEBhMCVVMxDTALBgNVBAoMBEphdmExHTAbBgNVBAsMFFN1bkpTU0UgVGVz\n" +
95
"dCBTZXJpdmNlMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAENVQN1wXWFdgC6u/dDdiC\n" +
96
"y+WtMTF66oL/0BSm+1ZqsogamzCryawOcHgiuXgWzx5CQ3LuOC+tDFyXpGfHuCvb\n" +
97
"dkzxPrP5n9NrR8/uRPe5l1KOUbchviU8z9cTP+LZxnZDo1MwUTAdBgNVHQ4EFgQU\n" +
98
"SktSFArR1p/5mXV0kyo0RxIVa/UwHwYDVR0jBBgwFoAUSktSFArR1p/5mXV0kyo0\n" +
99
"RxIVa/UwDwYDVR0TAQH/BAUwAwEB/zAKBggqhkjOPQQDAwNoADBlAjBZvoNmq3/v\n" +
100
"RD2gBTyvxjS9h0rsMRLHDnvul/KWngytwGPTOBo0Y8ixQXSjdKoc3rkCMQDkiNgx\n" +
101
"IDxuHedmrLQKIPnVcthTmwv7//jHiqGoKofwChMo2a1P+DQdhszmeHD/ARQ=\n" +
102
"-----END CERTIFICATE-----"
103
};
104
105
private static final String[] EE_CERTS = new String[] {
106
// SHA256withECDSA, curve secp256r1
107
// Validity
108
// Not Before: May 22 07:18:16 2018 GMT
109
// Not After : May 17 07:18:16 2038 GMT
110
// Authority Key Identifier:
111
// 60:CF:BD:73:FF:FA:1A:30:D2:A4:EC:D3:49:71:46:EF:1A:35:A0:86
112
"-----BEGIN CERTIFICATE-----\n" +
113
"MIIBqjCCAVCgAwIBAgIJAPLY8qZjgNRAMAoGCCqGSM49BAMCMDsxCzAJBgNVBAYT\n" +
114
"AlVTMQ0wCwYDVQQKDARKYXZhMR0wGwYDVQQLDBRTdW5KU1NFIFRlc3QgU2VyaXZj\n" +
115
"ZTAeFw0xODA1MjIwNzE4MTZaFw0zODA1MTcwNzE4MTZaMFUxCzAJBgNVBAYTAlVT\n" +
116
"MQ0wCwYDVQQKDARKYXZhMR0wGwYDVQQLDBRTdW5KU1NFIFRlc3QgU2VyaXZjZTEY\n" +
117
"MBYGA1UEAwwPUmVncmVzc2lvbiBUZXN0MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcD\n" +
118
"QgAEb+9n05qfXnfHUb0xtQJNS4JeSi6IjOfW5NqchvKnfJey9VkJzR7QHLuOESdf\n" +
119
"xlR7q8YIWgih3iWLGfB+wxHiOqMjMCEwHwYDVR0jBBgwFoAUYM+9c//6GjDSpOzT\n" +
120
"SXFG7xo1oIYwCgYIKoZIzj0EAwIDSAAwRQIgWpRegWXMheiD3qFdd8kMdrkLxRbq\n" +
121
"1zj8nQMEwFTUjjQCIQDRIrAjZX+YXHN9b0SoWWLPUq0HmiFIi8RwMnO//wJIGQ==\n" +
122
"-----END CERTIFICATE-----",
123
124
// SHA384withECDSA, curve secp384r1
125
// Validity
126
// Not Before: Jun 24 08:15:06 2019 GMT
127
// Not After : Jun 19 08:15:06 2039 GMT
128
// Authority Key Identifier:
129
// 40:2D:AA:EE:66:AA:33:27:AD:9B:5D:52:9B:60:67:6A:2B:AD:52:D2
130
"-----BEGIN CERTIFICATE-----\n" +
131
"MIICEjCCAZegAwIBAgIUS3F0AqAXWRg07CnbknJzxofyBQMwCgYIKoZIzj0EAwMw\n" +
132
"OzELMAkGA1UEBhMCVVMxDTALBgNVBAoMBEphdmExHTAbBgNVBAsMFFN1bkpTU0Ug\n" +
133
"VGVzdCBTZXJpdmNlMB4XDTE5MDYyNDA4MTUwNloXDTM5MDYxOTA4MTUwNlowVTEL\n" +
134
"MAkGA1UEBhMCVVMxDTALBgNVBAoMBEphdmExHTAbBgNVBAsMFFN1bkpTU0UgVGVz\n" +
135
"dCBTZXJpdmNlMRgwFgYDVQQDDA9SZWdyZXNzaW9uIFRlc3QwdjAQBgcqhkjOPQIB\n" +
136
"BgUrgQQAIgNiAARqElz8b6T07eyKomIinhztV3/3XBk9bKGtJ0W+JOltjuhMmP/w\n" +
137
"G8ASSevpgqgpi6EzpBZaaJxE3zNfkNnxXOZmQi2Ypd1uK0zRdbEOKg0XOcTTZwEj\n" +
138
"iLjYmt3O0pwpklijQjBAMB0GA1UdDgQWBBRALaruZqozJ62bXVKbYGdqK61S0jAf\n" +
139
"BgNVHSMEGDAWgBRKS1IUCtHWn/mZdXSTKjRHEhVr9TAKBggqhkjOPQQDAwNpADBm\n" +
140
"AjEArVDFKf48xijN6huVUJzKCOP0zlWB5Js+DItIkZmLQuhciPLhLIB/rChf3Y4C\n" +
141
"xuP4AjEAmfLhQRI0O3pifpYzYSVh2G7/jHNG4eO+2dvgAcU+Lh2IIj/cpLaPFSvL\n" +
142
"J8FXY9Nj\n" +
143
"-----END CERTIFICATE-----"
144
};
145
146
private static final String[] EE_KEYS = new String[] {
147
"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgn5K03bpTLjEtFQRa\n" +
148
"JUtx22gtmGEvvSUSQdimhGthdtihRANCAARv72fTmp9ed8dRvTG1Ak1Lgl5KLoiM\n" +
149
"59bk2pyG8qd8l7L1WQnNHtAcu44RJ1/GVHurxghaCKHeJYsZ8H7DEeI6",
150
"MIG2AgEAMBAGByqGSM49AgEGBSuBBAAiBIGeMIGbAgEBBDASuI9EtK29APXPipkc\n" +
151
"qDA+qwlewMjv/OcjUJ77kP1Vz62oVF9iY9SRIyFIUju8wt+hZANiAARqElz8b6T0\n" +
152
"7eyKomIinhztV3/3XBk9bKGtJ0W+JOltjuhMmP/wG8ASSevpgqgpi6EzpBZaaJxE\n" +
153
"3zNfkNnxXOZmQi2Ypd1uK0zRdbEOKg0XOcTTZwEjiLjYmt3O0pwpklg="
154
};
155
156
private static final String[] EE_ALGS = new String[] {
157
"EC",
158
"EC"
159
};
160
161
private static final String[] EE_ALIASES = new String[] {
162
"EC-SHA256",
163
"EC-SHA384"
164
};
165
166
private static final Map<Integer, String> SIG_SCHEMES_MAP = Map.of(
167
0x0403, "ecdsa_secp256r1_sha256",
168
0x0503, "ecdsa_secp384r1_sha384");
169
170
private static final int TLS_HS_CLI_HELLO = 1;
171
private static final int TLS_HS_CERT_REQ = 13;
172
private static final int HELLO_EXT_SIG_ALGS = 13;
173
174
public SigAlgosExtTestWithTLS12() throws Exception {
175
super();
176
}
177
178
/*
179
* Create an instance of KeyManager for client use.
180
*/
181
public KeyManager createClientKeyManager() throws Exception {
182
return SSLContextTemplate.createKeyManager(
183
EE_CERTS,
184
EE_KEYS,
185
EE_ALGS,
186
EE_ALIASES,
187
getServerContextParameters());
188
}
189
190
@Override
191
public TrustManager createClientTrustManager() throws Exception {
192
return SSLContextTemplate.createTrustManager(
193
CA_CERTS,
194
getServerContextParameters());
195
}
196
197
@Override
198
public KeyManager createServerKeyManager() throws Exception {
199
return SSLContextTemplate.createKeyManager(
200
EE_CERTS,
201
EE_KEYS,
202
EE_ALGS,
203
EE_ALIASES,
204
getServerContextParameters());
205
}
206
207
@Override
208
public TrustManager createServerTrustManager() throws Exception {
209
return SSLContextTemplate.createTrustManager(
210
CA_CERTS,
211
getServerContextParameters());
212
}
213
214
@Override
215
protected SSLEngine configureServerEngine(SSLEngine serverEngine) {
216
serverEngine.setUseClientMode(false);
217
serverEngine.setNeedClientAuth(CLIENT_AUTH);
218
return serverEngine;
219
}
220
221
@Override
222
protected SSLEngine configureClientEngine(SSLEngine clientEngine) {
223
clientEngine.setUseClientMode(true);
224
clientEngine.setEnabledProtocols(new String[] { "TLSv1.2" });
225
return clientEngine;
226
}
227
228
public static void main(String[] args) throws Exception {
229
System.setProperty("javax.net.debug", "ssl:handshake");
230
231
try {
232
new SigAlgosExtTestWithTLS12().run();
233
if (EXPECT_FAIL) {
234
throw new RuntimeException(
235
"Expected SSLHandshakeException wasn't thrown");
236
}
237
} catch (SSLHandshakeException e) {
238
if (EXPECT_FAIL && e.getMessage().equals(
239
"No supported signature algorithm")) {
240
System.out.println("Expected SSLHandshakeException");
241
} else {
242
throw e;
243
}
244
}
245
}
246
247
private void run() throws Exception {
248
boolean dataDone = false;
249
while (isOpen(clientEngine) || isOpen(serverEngine)) {
250
clientEngine.wrap(clientOut, cTOs);
251
cTOs.flip();
252
253
// Consume the ClientHello and get the server flight of handshake
254
// messages. We expect that it will be one TLS record containing
255
// multiple handshake messages, one of which is a CertificateRequest
256
// when the client authentication is required.
257
serverEngine.unwrap(cTOs, serverIn);
258
runDelegatedTasks(serverEngine);
259
260
// Wrap the server flight
261
serverEngine.wrap(serverOut, sTOc);
262
sTOc.flip();
263
264
if (CLIENT_AUTH && EXPECT_FAIL) {
265
twistCertReqMsg(sTOc);
266
}
267
268
clientEngine.unwrap(sTOc, clientIn);
269
runDelegatedTasks(clientEngine);
270
271
serverEngine.unwrap(cTOs, serverIn);
272
runDelegatedTasks(serverEngine);
273
274
cTOs.compact();
275
sTOc.compact();
276
277
if (!dataDone && (clientOut.limit() == serverIn.position()) &&
278
(serverOut.limit() == clientIn.position())) {
279
checkTransfer(serverOut, clientIn);
280
checkTransfer(clientOut, serverIn);
281
282
clientEngine.closeOutbound();
283
dataDone = true;
284
serverEngine.closeOutbound();
285
}
286
}
287
}
288
289
/**
290
* Twists signature schemes in CertificateRequest message for negative
291
* client authentication cases.
292
*
293
* @param tlsRecord a ByteBuffer containing a TLS record. It is assumed
294
* that the position of the ByteBuffer is on the first byte of the TLS
295
* record header.
296
*
297
* @throws SSLException if the incoming ByteBuffer does not contain a
298
* well-formed TLS message.
299
*/
300
private static void twistCertReqMsg(
301
ByteBuffer tlsRecord) throws SSLException {
302
Objects.requireNonNull(tlsRecord);
303
tlsRecord.mark();
304
305
// Process the TLS record header
306
int type = Byte.toUnsignedInt(tlsRecord.get());
307
int ver_major = Byte.toUnsignedInt(tlsRecord.get());
308
int ver_minor = Byte.toUnsignedInt(tlsRecord.get());
309
int recLen = Short.toUnsignedInt(tlsRecord.getShort());
310
311
// Simple sanity checks
312
if (type != 22) {
313
throw new SSLException("Not a handshake: Type = " + type);
314
} else if (recLen > tlsRecord.remaining()) {
315
throw new SSLException("Incomplete record in buffer: " +
316
"Record length = " + recLen + ", Remaining = " +
317
tlsRecord.remaining());
318
}
319
320
while (tlsRecord.hasRemaining()) {
321
// Grab the handshake message header.
322
int msgHdr = tlsRecord.getInt();
323
int msgType = (msgHdr >> 24) & 0x000000FF;
324
int msgLen = msgHdr & 0x00FFFFFF;
325
326
if (msgType == TLS_HS_CERT_REQ) {
327
// Slice the buffer such that it contains the entire
328
// handshake message (less the handshake header).
329
int bufPos = tlsRecord.position();
330
ByteBuffer buf = tlsRecord.slice(bufPos, msgLen);
331
332
// Replace the signature scheme with an unknown value
333
twistSigSchemesCertReq(buf, (short) 0x0000);
334
byte[] bufBytes = new byte[buf.limit()];
335
buf.get(bufBytes);
336
tlsRecord.put(bufPos, bufBytes);
337
338
break;
339
} else {
340
// Skip to the next handshake message, if there is one
341
tlsRecord.position(tlsRecord.position() + msgLen);
342
}
343
}
344
345
tlsRecord.reset();
346
}
347
348
/**
349
* Replace the signature schemes in CertificateRequest message with an
350
* alternative value. It is assumed that the provided ByteBuffer has its
351
* position set at the first byte of the CertificateRequest message body
352
* (AFTER the handshake header) and contains the entire CR message. Upon
353
* successful completion of this method the ByteBuffer will have its
354
* position reset to the initial offset in the buffer.
355
* If an exception is thrown the position at the time of the exception
356
* will be preserved.
357
*
358
* @param data the ByteBuffer containing the CertificateRequest bytes
359
* @param altSigScheme an alternative signature scheme
360
*/
361
private static void twistSigSchemesCertReq(ByteBuffer data,
362
Short altSigScheme) {
363
Objects.requireNonNull(data);
364
data.mark();
365
366
// Jump past the certificate types
367
int certTypeLen = Byte.toUnsignedInt(data.get());
368
if (certTypeLen != 0) {
369
data.position(data.position() + certTypeLen);
370
}
371
372
int sigSchemeLen = Short.toUnsignedInt(data.getShort());
373
for (int ssOff = 0; ssOff < sigSchemeLen; ssOff += 2) {
374
System.err.println(
375
"Use alternative signature scheme: " + altSigScheme);
376
data.putShort(data.position(), altSigScheme);
377
}
378
379
data.reset();
380
}
381
}
382
383