openssl icon indicating copy to clipboard operation
openssl copied to clipboard

TLSPlaintext.legacy_record_version inconsistency

Open LiD0209 opened this issue 7 months ago • 2 comments

TLSPlaintext.legacy_record_version inconsistency

1. Conclusion

Looking at the TLSPlaintext.legacy_record_version requirement alone, OpenSSL is closer to Not Satisfied than to "Fully Satisfied".

The reason is:

  • RFC 8446 requires that TLSPlaintext.legacy_record_version MUST be ignored.
  • OpenSSL does have an "ignore this field" implementation note on the path where TLS 1.3 plaintext has already been confirmed.
  • But on the earlier TLS_ANY_VERSION path, OpenSSL still reads and checks whether the high byte of rec->rec_version belongs to the 0x03xx family, so the field is not ignored in all scenarios.

Therefore this sub-issue is more accurately described as: the handling of TLSPlaintext.legacy_record_version is not fully consistent with RFC 8446.

2. RFC 8446 English Source Text

2.1 TLSPlaintext structure definition requirement

From RFC 8446 (TLS 1.3), Section 5.1:

legacy_record_version:  MUST be set to 0x0303 for all records
   generated by a TLS 1.3 implementation other than an initial
   ClientHello (i.e., one not generated after a HelloRetryRequest),
   where it MAY also be 0x0301 for compatibility purposes.  This
   field is deprecated and MUST be ignored for all purposes.
   Previous versions of TLS would use other values in this field
   under some circumstances.

The key sentence is:

This field is deprecated and MUST be ignored for all purposes.

2.2 Appendix D summary of this field

From RFC 8446, Appendix D:

(TLSPlaintext.legacy_record_version and
TLSCiphertext.legacy_record_version) for various purposes.  As of
TLS 1.3, this field is deprecated.  The value of
TLSPlaintext.legacy_record_version MUST be ignored by all
implementations.  The value of TLSCiphertext.legacy_record_version is
included in the additional data for deprotection but MAY otherwise be
ignored or MAY be validated to match the fixed constant value.
Version negotiation is performed using only the handshake versions

The most critical normative sentence here is:

TLSPlaintext.legacy_record_version MUST be ignored by all implementations.

And the spec also states:

Version negotiation is performed using only the handshake versions

That is, version negotiation must rely on handshake-layer fields, not on TLSPlaintext.legacy_record_version.

2.3 Receiver compatibility note

From RFC 8446, Appendix D:

Note that earlier versions of TLS did not clearly specify the record
layer version number value in all cases
(TLSPlaintext.legacy_record_version).  Servers will receive various
TLS 1.x versions in this field, but its value MUST always be ignored.

This again emphasizes:

its value MUST always be ignored.

3. OpenSSL Code Description

3.1 OpenSSL behavior when the TLS version is not yet determined

From ssl/record/methods/tlsany_meth.c:

static int tls_validate_record_header(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *rec)
{
    if (rl->version == TLS_ANY_VERSION) {
        if ((rec->rec_version >> 8) != SSL3_VERSION_MAJOR) {
            if (rl->is_first_record) {
                unsigned char *p;

                /*
                 * Go back to start of packet, look at the five bytes that
                 * we have.
                 */
                p = rl->packet;
                if (HAS_PREFIX((char *)p, "GET ") || HAS_PREFIX((char *)p, "POST ") || HAS_PREFIX((char *)p, "HEAD ") || HAS_PREFIX((char *)p, "PATCH") || HAS_PREFIX((char *)p, "OPTIO") || HAS_PREFIX((char *)p, "DELET") || HAS_PREFIX((char *)p, "TRACE") || HAS_PREFIX((char *)p, "PUT ")) {
                    RLAYERfatal(rl, SSL_AD_NO_ALERT, SSL_R_HTTP_REQUEST);
                    return 0;
                } else if (HAS_PREFIX((char *)p, "CONNE")) {
                    RLAYERfatal(rl, SSL_AD_NO_ALERT,
                        SSL_R_HTTPS_PROXY_REQUEST);
                    return 0;
                }

                /* Doesn't look like TLS - don't send an alert */
                RLAYERfatal(rl, SSL_AD_NO_ALERT,
                    SSL_R_WRONG_VERSION_NUMBER);
                return 0;
            } else {
                RLAYERfatal(rl, SSL_AD_PROTOCOL_VERSION,
                    SSL_R_WRONG_VERSION_NUMBER);
                return 0;
            }
        }

This code shows:

  • When rl->version == TLS_ANY_VERSION, OpenSSL has not yet determined the final protocol version.
  • But it already inspects rec->rec_version.
  • The specific check is:
if ((rec->rec_version >> 8) != SSL3_VERSION_MAJOR)

This requires the high byte of the record header version to look like 0x03xx.

So on this early path, TLSPlaintext.legacy_record_version is not fully ignored — it is used for initial filtering.

3.2 OpenSSL behavior when TLS 1.3 plaintext path is already known

    } else if (rl->version == TLS1_3_VERSION) {
        /*
         * In this case we know we are going to negotiate TLSv1.3, but we've
         * had an HRR, so we haven't actually done so yet. In TLSv1.3 we
         * must ignore the legacy record version in plaintext records.
         */

This comment is direct: the OpenSSL developers know the RFC requirement and do follow "ignore the legacy record version in plaintext records" on this branch.

3.3 Version comparison in the non-TLS-1.3 branch

    } else if (rec->rec_version != rl->version) {
        if ((rl->version & 0xFF00) == (rec->rec_version & 0xFF00)) {
            if (rec->type == SSL3_RT_ALERT) {
                RLAYERfatal(rl, SSL_AD_NO_ALERT,
                    SSL_R_WRONG_VERSION_NUMBER);
                return 0;
            }
            /* Send back error using their minor version number */
            rl->version = (unsigned short)rec->rec_version;
        }
        RLAYERfatal(rl, SSL_AD_PROTOCOL_VERSION,
            SSL_R_WRONG_VERSION_NUMBER);
        return 0;
    }

This further shows that OpenSSL's record layer treats rec->rec_version as a field that participates in decisions in many cases, rather than unconditionally ignoring it.

4. Where the Inconsistency Lies

The core difference between the standard and the implementation is the scope of "ignore".

The RFC requirement is unconditional and global:

TLSPlaintext.legacy_record_version MUST be ignored by all implementations.

And also:

its value MUST always be ignored.

This means:

  • The field must not be used to filter input even when the version has not yet been negotiated.
  • It must not serve as an early indicator of "looks like TLS".
  • Version negotiation must rely only on handshake-layer version fields.

But OpenSSL's implementation is conditional:

  • When rl->version == TLS1_3_VERSION: the field is ignored.
  • When rl->version == TLS_ANY_VERSION: the high byte of rec->rec_version is checked against 0x03.

So what OpenSSL actually does is:

  • "Ignore this field after the TLS 1.3 plaintext context has been confirmed."

While RFC requires:

  • "Always ignore TLSPlaintext.legacy_record_version itself."

These two are not the same semantic strength.

5. Why This Inconsistency Exists

This inconsistency looks more like a gap between "implementation-layer early filtering strategy" and "RFC ideal semantics".

Likely implementation motivations include:

  • Quickly distinguishing "TLS-like input" from "clearly non-TLS input" at the earliest record header read.
  • Early interception of HTTP requests, proxy requests, or obviously malformed first packets.
  • Reusing existing record-layer version filtering logic rather than pushing the TLS 1.3 "ignore legacy_record_version" semantics all the way back to the very first path.

Regardless of implementation motivation, from the literal text of RFC 8446, this approach is stricter than the spec requires and uses TLSPlaintext.legacy_record_version earlier than the spec allows, which creates the inconsistency.

6. Final Assessment

Evaluating only the TLSPlaintext.legacy_record_version sub-issue:

  • Standard requirement: MUST be ignored
  • OpenSSL implementation: ignored only after the TLS 1.3 plaintext path is confirmed; on the earlier path the field is still read and used for filtering
  • Conclusion: not fully consistent

In the broader context of this item, the reason it was previously described as "partially satisfied" is:

  • The TLSPlaintext half leans toward "not satisfied"
  • The TLSCiphertext half permits implementations to validate the fixed constant value 0x0303

LiD0209 avatar Apr 28 '26 03:04 LiD0209

@LiD0209 I agree with the code observation but it doesn't show a TLS 1.3 conformance issue.

The TLS_ANY_VERSION check happens before OpenSSL has determined that the peer is speaking TLS 1.3, TLS 1.2 or even TLS at all. The check is limited to the record version major byte being 0x03, so it accepts the TLS/SSL3 record version family and doesn't use the field to negotiate the protocol version.

Once OpenSSL is actually in the TLS 1.3 plaintext path, it already ignores legacy_record_version. That behavior was fixed in #19058 (commit 2093428) and there is a regression test for the post-HRR plaintext ServerHello case with an intentionally wrong record version.

So for me the current TLS_ANY_VERSION logic can be classified a pre-negotiation record format sanity check, not as TLS 1.3 version negotiation and not as a violation of RFC 8446 §5.1. I don't think we should change this unless there is a concrete interoperability failure with a conforming TLS 1.3 peer. That said, a clarifying comment in the code may be useful.

idrassi avatar Apr 30 '26 09:04 idrassi

Thanks for the clarification. I understand the distinction now: the TLS_ANY_VERSION check is a pre-negotiation record format sanity check, not TLS 1.3 version negotiation, and the TLS 1.3 plaintext path already ignores legacy_record_version as required.

I agree that this does not appear to be a TLS 1.3 conformance issue without a concrete interoperability failure. A clarifying comment in the code may be useful, but I’m fine with closing this from my side. Thanks for the detailed explanation.

LiD0209 avatar Apr 30 '26 10:04 LiD0209