From ba44656b97b7103d96718452e300df8a6bd59c87 Mon Sep 17 00:00:00 2001 From: Alice Pellegrini Date: Thu, 25 Sep 2025 08:44:14 +0000 Subject: [PATCH] 8366454: TLS1.3 server fails with bad_record_mac when receiving encrypted records with empty body MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel JeliƄski Reviewed-by: djelinski --- .../share/classes/sun/security/ssl/Alert.java | 10 ++++++++++ .../share/classes/sun/security/ssl/SSLCipher.java | 13 ++++++------- .../ssl/SSLEngineImpl/SSLEngineEmptyFragments.java | 6 +++--- .../ssl/SSLSocketImpl/SSLSocketEmptyFragments.java | 8 ++++---- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/java.base/share/classes/sun/security/ssl/Alert.java b/src/java.base/share/classes/sun/security/ssl/Alert.java index 960b3f3b37d..ed5e079bf44 100644 --- a/src/java.base/share/classes/sun/security/ssl/Alert.java +++ b/src/java.base/share/classes/sun/security/ssl/Alert.java @@ -181,6 +181,16 @@ public enum Alert { AlertMessage(TransportContext context, ByteBuffer m) throws IOException { + // From RFC 8446 "Implementations + // MUST NOT send Handshake and Alert records that have a zero-length + // TLSInnerPlaintext.content; if such a message is received, the + // receiving implementation MUST terminate the connection with an + // "unexpected_message" alert." + if (m.remaining() == 0) { + throw context.fatal(Alert.UNEXPECTED_MESSAGE, + "Alert fragments must not be zero length."); + } + // struct { // AlertLevel level; // AlertDescription description; diff --git a/src/java.base/share/classes/sun/security/ssl/SSLCipher.java b/src/java.base/share/classes/sun/security/ssl/SSLCipher.java index d11ffc96b47..4a52a2ea583 100644 --- a/src/java.base/share/classes/sun/security/ssl/SSLCipher.java +++ b/src/java.base/share/classes/sun/security/ssl/SSLCipher.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -1923,9 +1923,9 @@ enum SSLCipher { // remove inner plaintext padding int i = pt.limit() - 1; - for (; i > 0 && pt.get(i) == 0; i--); + for (; i >= pos && pt.get(i) == 0; i--); - if (i < (pos + 1)) { + if (i < pos) { throw new BadPaddingException( "Incorrect inner plaintext: no content type"); } @@ -2441,10 +2441,9 @@ enum SSLCipher { // remove inner plaintext padding int i = pt.limit() - 1; - for (; i > 0 && pt.get(i) == 0; i--) { - // blank - } - if (i < (pos + 1)) { + for (; i >= pos && pt.get(i) == 0; i--); + + if (i < pos) { throw new BadPaddingException( "Incorrect inner plaintext: no content type"); } diff --git a/test/jdk/sun/security/ssl/SSLEngineImpl/SSLEngineEmptyFragments.java b/test/jdk/sun/security/ssl/SSLEngineImpl/SSLEngineEmptyFragments.java index f2aa8041b9a..837e607c009 100644 --- a/test/jdk/sun/security/ssl/SSLEngineImpl/SSLEngineEmptyFragments.java +++ b/test/jdk/sun/security/ssl/SSLEngineImpl/SSLEngineEmptyFragments.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -103,7 +103,7 @@ public class SSLEngineEmptyFragments extends SSLContextTemplate { try { unwrap(serverEngine, alert, serverIn); throw new RuntimeException("Expected exception was not thrown."); - } catch (SSLHandshakeException exc) { + } catch (SSLProtocolException exc) { log("Got the exception I wanted."); } } @@ -133,7 +133,7 @@ public class SSLEngineEmptyFragments extends SSLContextTemplate { unwrap(serverEngine, alert, serverIn); log("Server unwrap was successful when it should have failed."); throw new RuntimeException("Expected exception was not thrown."); - } catch (SSLHandshakeException exc) { + } catch (SSLProtocolException exc) { log("Got the exception I wanted."); } } diff --git a/test/jdk/sun/security/ssl/SSLSocketImpl/SSLSocketEmptyFragments.java b/test/jdk/sun/security/ssl/SSLSocketImpl/SSLSocketEmptyFragments.java index dec93c1c199..de0ef43924d 100644 --- a/test/jdk/sun/security/ssl/SSLSocketImpl/SSLSocketEmptyFragments.java +++ b/test/jdk/sun/security/ssl/SSLSocketImpl/SSLSocketEmptyFragments.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -351,9 +351,9 @@ public class SSLSocketEmptyFragments extends SSLContextTemplate { tests.executeTest( tests::testEmptyHandshakeRecord, SSLProtocolException.class); tests.executeTest( - tests::testEmptyAlertNotHandshaking, SSLHandshakeException.class); + tests::testEmptyAlertNotHandshaking, SSLProtocolException.class); tests.executeTest( - tests::testEmptyAlertDuringHandshake, SSLHandshakeException.class); + tests::testEmptyAlertDuringHandshake, SSLProtocolException.class); tests.executeTest( tests::testEmptyChangeCipherSpecMessage, SSLProtocolException.class); @@ -361,6 +361,6 @@ public class SSLSocketEmptyFragments extends SSLContextTemplate { tests.executeTest( tests::testEmptyHandshakeRecord, SSLProtocolException.class); tests.executeTest( - tests::testEmptyAlertNotHandshaking, SSLHandshakeException.class); + tests::testEmptyAlertNotHandshaking, SSLProtocolException.class); } }