Skip to content

Answer a HelloRetryRequest as uTLS does: one key share, BoringSSL padding per flight - #1

Merged
aimalygin merged 1 commit into
xray/rustls-0.23.40from
claude/hrr-retry-shaping
Nov 10, 2026
Merged

aimalygin merged 1 commit into
xray/rustls-0.23.40from
claude/hrr-retry-shaping

Conversation

@aimalygin

@aimalygin aimalygin commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

This PR makes a ClientHello shaped by a ClientHelloPlan answer a HelloRetryRequest as uTLS answers it.

xray-rust's uTLS shaping needs it; the companion PR is aimalygin/xray-rust#258. Merge with a merge commit or a fast-forward, not a squash, so the rev xray-rust pins stays in this branch's history.

What broke

A plan is built once per connection, and emit_client_hello_for_retry applies it to the second ClientHello as well. Against OpenSSL 3.5's s_server, a uTLS-shaped Chrome hello on 3a131be broke in three ways:

  • A retry that names a group (-groups P-256). The second ClientHello carried the plan's GREASE key share next to the P-256 one. RFC 8446 §4.1.2 asks for a single share from the indicated group. OpenSSL answers illegal_parameter, and so does Go's crypto/tls server ("client didn't send one key share in second ClientHello").
  • A retry carrying a cookie (-stateless). The planned extension order had no cookie, so set_custom_order refused and the second ClientHello was never built.
  • Padding. PadToHandshakeSize padded the second ClientHello to the first one's target. BoringSSL, and uTLS's BoringPaddingStyle, decide the padding again from the second ClientHello's own length.

Changes

  • Retry key shares. On a retry whose HelloRetryRequest names a group, the plan's raw key shares and its GREASE key-share position are skipped, so the second ClientHello carries rustls's single retry_key_share. A retry that carries only a cookie keeps them, because RFC 8446 wants the first ClientHello's key share list repeated, and the plan's entries are byte-identical across flights.

  • ClientHelloPaddingPlan::boringssl(offset). BoringSSL's padding rule (ssl_add_clienthello_tlsext) is applied to each ClientHello as it is encoded:

    • a handshake message of 256 to 511 bytes before padding is padded up to 512 bytes;
    • when the extension header alone would reach 512, it is padded by one byte;
    • any other ClientHello carries no padding extension. The custom order may still name it, since encode_one writes only present extensions.

    offset is added to the measured length and to the padding. It serves a ClientHello that many bytes shorter than the one it imitates; xray-rust passes the bytes of the cipher suites this provider cannot negotiate. It is bounded to ±511.

  • The cookie needs no change here. A plan's extension order may name Cookie, and PlannedExtensionOrder already drops it from a ClientHello that carries none, translating GREASE positions past it. xray-rust plans it where uTLS inserts it.

Tests

In client/test.rs, a connection is driven through a scripted HelloRetryRequest:

  • a_retry_naming_a_group_sends_its_key_share_alone: GREASE as a raw share, and through the GREASE plan.
  • a_retry_with_a_cookie_repeats_the_key_shares_and_takes_its_planned_place
  • boringssl_padding_follows_each_client_hellos_own_length: both flights, inside the window, at the one-byte edge, the first flight inside and the second past it, and below the window, with BoringSSL's rule restated in the test.
  • boringssl_padding_len_is_boringssls_rule: the boundaries and the offset bound.

All 311 rustls lib tests pass. Mutations on the change: 11 of 11 caught (the retry skip off, on for a cookie-only retry, raw or GREASE shares kept, the window taking 512, the edge padding no byte or off by one, the offset left out of the rule or of the padding, an empty extension outside the window, the offset bound).

xray-rust's live runs on this rev pass:

  • OpenSSL s_server -groups P-256. -stateless closes the connection after its HelloRetryRequest even for OpenSSL's own s_client, so it judges nothing; xray-rust's scripted server checks the cookie path;
  • Xray-core v26.11.9's TLS inbound with curvePreferences: ["CurveP256"], so the HelloRetryRequest comes from Go itself.

🤖 Generated with Claude Code

A ClientHelloPlan is built once per connection and applied to the second
ClientHello a HelloRetryRequest asks for as well. Two of its parts did not
fit that flight:

- A retry that names a group must carry a single key share for it (RFC
  8446 section 4.1.2), the one rustls makes in retry_key_share. The plan's
  raw key shares and its GREASE key share were added beside it, and OpenSSL
  and Go's crypto/tls refuse such a ClientHello with illegal_parameter. They
  now belong to the first ClientHello alone; a retry that carries only a
  cookie repeats them, as it must repeat the key share list.
- PadToHandshakeSize pads every flight to the first one's target.
  ClientHelloPaddingPlan::boringssl(offset) applies BoringSSL's padding rule
  (ssl_add_clienthello_tlsext, uTLS's BoringPaddingStyle) to each
  ClientHello as it is encoded: a handshake message of 256 to 511 bytes
  before padding is padded up to 512, or by one byte when the extension
  header alone would reach 512, and any other carries no padding extension.
  offset is added to the measured length and to the padding, for a
  ClientHello that many bytes shorter than the one it stands for.

A HelloRetryRequest's cookie needs no change here: a planned extension
order may name the cookie, and PlannedExtensionOrder drops it from a
ClientHello that carries none.

Tests drive a connection through a scripted HelloRetryRequest naming a
group (GREASE as a raw share and through the GREASE plan), one carrying a
cookie, and BoringSSL padding across both flights.
@aimalygin
aimalygin merged commit eae43d3 into xray/rustls-0.23.40 Nov 10, 2026
14 of 31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant