Skip to content

tls: kTLS reads each traffic secret where BoringSSL wrote its key-log line, and the JVM's HMAC leaves no copy of it (#45) - #56

Merged
MDA2AV merged 1 commit into
mainfrom
fix/keylog-in-place
Nov 11, 2026
Merged

MDA2AV merged 1 commit into
mainfrom
fix/keylog-in-place

Conversation

@MDA2AV

@MDA2AV MDA2AV commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Closes #45.

The issue lists three ways a traffic secret outlives the zeroing in Ktls.kt. Two are real, and this fixes them.

Strings of the key-log line. onKeylog made a String of BoringSSL's line, then a substring of its hex and a trimmed copy of that, all immutable and left on the heap until collected and overwritten. It now compares the two labels and decodes the hex from the line's own bytes, as MDA2AV/ioxide#561 does, so the secret's ByteArray is all it allocates. BoringSsl.cString leaves the shared API with its only common caller; each target keeps it private, for error strings and cipher names.

The JVM's HMAC. hmacSha256 went through Mac and SecretKeySpec. It now calls BoringSSL's HMAC, as the native target does, on the arrays themselves through a critical downcall, as SSL_write from a heap array already does. So nothing copies the key. I checked both with a heap dump taken after the caller zeroed a random key, 3 runs each:

  • Mac + SecretKeySpec: the key once (SecretKeySpec's copy), plus key^ipad and key^opad, the HMAC pads, which give the key back.
  • BoringSSL's HMAC: none of the three.

Zeroing before free: not real, left as is. In a release build from this branch, Ktls.program still zeroes the key, the nonce and the secret (three memset calls) and the crypto info (an out-of-line zeroMemory call) before each free.

Tests.

  • KtlsSecretTest (common): a key-log line decodes to its secret in either case, with or without a newline, and a last word that isn't whole hex gives none. hmacSha256 gives RFC 4231 test cases 1 and 2, so the JVM's new path is checked too.
  • KeylogAllocationTest (JVM): decoding allocates nothing but the secret's array. With the String parse back, it fails at 528 bytes.
  • Native: all 321 passed. JVM: all 97 passed.

… line, and the JVM's HMAC leaves no copy of it (#45)

The keylog callback made Strings of the line, its hex and that trimmed, which stay on the heap until collected and overwritten, after the ByteArray Ktls zeroes. It now compares the labels and decodes the hex from the line's own bytes, allocating nothing but the secret's array. BoringSsl.cString leaves the shared API with its only common caller; each target keeps it for error strings and cipher names.

On the JVM, hmacSha256 went through Mac and SecretKeySpec, which keep the key and both HMAC pads on the heap. It now calls BoringSSL's HMAC as the native target does, on the arrays themselves through a critical downcall, as SSL_write from a heap array already does.
@MDA2AV
MDA2AV merged commit debf305 into main Nov 11, 2026
3 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.

kTLS: traffic secrets pass through strings that can't be zeroed, despite "no copy stays behind"

1 participant