Skip to content

Commit af09859

Browse files
committed
8311644: Server should not send bad_certificate alert when the client does not send any certificates
Backport-of: f62b5789add23adda2634a1cfb80f48b4387be74
1 parent d90297a commit af09859

File tree

8 files changed

+246
-79
lines changed

8 files changed

+246
-79
lines changed

src/java.base/share/classes/sun/security/ssl/Alert.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -123,13 +123,13 @@ SSLException createSSLException(String reason, Throwable cause) {
123123
}
124124

125125
if (cause instanceof IOException) {
126-
return new SSLException(reason, cause);
126+
return new SSLException("(" + description + ") " + reason, cause);
127127
} else if ((this == UNEXPECTED_MESSAGE)) {
128-
return new SSLProtocolException(reason, cause);
128+
return new SSLProtocolException("(" + description + ") " + reason, cause);
129129
} else if (handshakeOnly) {
130-
return new SSLHandshakeException(reason, cause);
130+
return new SSLHandshakeException("(" + description + ") " + reason, cause);
131131
} else {
132-
return new SSLException(reason, cause);
132+
return new SSLException("(" + description + ") " + reason, cause);
133133
}
134134
}
135135

src/java.base/share/classes/sun/security/ssl/CertificateMessage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ private void onCertificate(ServerHandshakeContext shc,
381381
if (shc.sslConfig.clientAuthType !=
382382
ClientAuthType.CLIENT_AUTH_REQUESTED) {
383383
// unexpected or require client authentication
384-
throw shc.conContext.fatal(Alert.BAD_CERTIFICATE,
384+
throw shc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
385385
"Empty client certificate chain");
386386
} else {
387387
return;
@@ -1163,7 +1163,7 @@ private void onConsumeCertificate(ServerHandshakeContext shc,
11631163
shc.handshakeConsumers.remove(
11641164
SSLHandshake.CERTIFICATE_VERIFY.id);
11651165
if (shc.sslConfig.clientAuthType == CLIENT_AUTH_REQUIRED) {
1166-
throw shc.conContext.fatal(Alert.BAD_CERTIFICATE,
1166+
throw shc.conContext.fatal(Alert.CERTIFICATE_REQUIRED,
11671167
"Empty client certificate chain");
11681168
} else {
11691169
// optional client authentication
@@ -1187,7 +1187,7 @@ private void onConsumeCertificate(ClientHandshakeContext chc,
11871187
T13CertificateMessage certificateMessage )throws IOException {
11881188
if (certificateMessage.certEntries == null ||
11891189
certificateMessage.certEntries.isEmpty()) {
1190-
throw chc.conContext.fatal(Alert.BAD_CERTIFICATE,
1190+
throw chc.conContext.fatal(Alert.DECODE_ERROR,
11911191
"Empty server certificate chain");
11921192
}
11931193

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2024, 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.
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
26+
* @library /javax/net/ssl/templates
27+
* @bug 8311644
28+
* @summary Verify CertificateMessage alerts are correct to the TLS specs
29+
* @run main/othervm -Djdk.tls.client.protocols=TLSv1.2 CertMsgCheck handshake_failure
30+
* @run main/othervm -Djdk.tls.client.protocols=TLSv1.3 CertMsgCheck certificate_required
31+
*
32+
*/
33+
34+
public class CertMsgCheck {
35+
36+
public static void main(String[] args) throws Exception {
37+
// Start server
38+
TLSBase.Server server = new TLSBase.ServerBuilder().setClientAuth(true).
39+
build();
40+
41+
// Initial client session
42+
TLSBase.Client client1 = new TLSBase.Client(true, false);
43+
if (server.getSession(client1).getSessionContext() == null) {
44+
for (Exception e : server.getExceptionList()) {
45+
System.out.println("Looking at " + e.getClass() + " " +
46+
e.getMessage());
47+
if (e.getMessage().contains(args[0])) {
48+
System.out.println("Found correct exception: " + args[0] +
49+
" in " + e.getMessage());
50+
return;
51+
} else {
52+
System.out.println("No \"" + args[0] + "\" found.");
53+
}
54+
}
55+
56+
throw new Exception("Failed to find expected alert: " + args[0]);
57+
}
58+
}
59+
}

test/jdk/javax/net/ssl/SSLSession/CheckSessionContext.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -36,36 +36,30 @@
3636
*/
3737

3838
import javax.net.ssl.SSLSession;
39+
import java.util.HexFormat;
3940

4041
public class CheckSessionContext {
4142

42-
static void toHex(byte[] id) {
43-
for (byte b : id) {
44-
System.out.printf("%02X ", b);
45-
}
46-
System.out.println();
47-
}
43+
static HexFormat hex = HexFormat.of();
4844

4945
public static void main(String[] args) throws Exception {
5046
TLSBase.Server server = new TLSBase.Server();
5147

5248
// Initial client session
5349
TLSBase.Client client1 = new TLSBase.Client();
5450
if (server.getSession(client1).getSessionContext() == null) {
55-
throw new Exception("Context was null");
51+
throw new Exception("Context was null. Handshake failure.");
5652
} else {
5753
System.out.println("Context was found");
5854
}
5955
SSLSession ss = server.getSession(client1);
6056
System.out.println(ss);
6157
byte[] id = ss.getId();
62-
System.out.print("id = ");
63-
toHex(id);
58+
System.out.println("id = " + hex.formatHex(id));
6459
System.out.println("ss.getSessionContext().getSession(id) = " + ss.getSessionContext().getSession(id));
6560
if (ss.getSessionContext().getSession(id) != null) {
6661
id = ss.getSessionContext().getSession(id).getId();
67-
System.out.print("id = ");
68-
toHex(id);
62+
System.out.println("id = " + hex.formatHex(id));
6963
}
7064
server.close(client1);
7165
client1.close();

0 commit comments

Comments
 (0)