Skip to content

Deprecate Asn1StringRef::as_utf8 in favor of a NUL-safe to_string#2652

Merged
alex merged 1 commit into
rust-openssl:masterfrom
alex:claude/confident-darwin-9QCvv
Jun 8, 2026
Merged

Deprecate Asn1StringRef::as_utf8 in favor of a NUL-safe to_string#2652
alex merged 1 commit into
rust-openssl:masterfrom
alex:claude/confident-darwin-9QCvv

Conversation

@alex

@alex alex commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

as_utf8 calls ASN1_STRING_to_UTF8, which returns an explicit length, but discards it: the result is read back through CStr::from_ptr (in OpensslStringRef's Deref), dropping everything after the first NUL byte. X.509 name entries can legally contain interior NULs, and seeing them matters for detecting the classic NUL-byte-CN spoofing trick (e.g. "example.com\0evil.com"). The truncation can't be fixed in place because OpensslString's representation is inherently NUL-terminated.

Add Asn1StringRef::to_string, which preserves the full contents using the length reported for the conversion. For string types whose UTF-8 conversion is the identity (UTF8String already containing valid UTF-8, and the Latin-1 family when ASCII-only), it copies directly out of the underlying buffer with a single allocation; otherwise it converts through ASN1_STRING_to_UTF8 and copies the result (replacing any invalid UTF-8 with U+FFFD rather than assuming validity unchecked). The Debug impl now uses to_string, so interior NULs show up there too.

https://claude.ai/code/session_01JS5sEpf4jRcbgsqsfNnMPs

as_utf8 calls ASN1_STRING_to_UTF8, which returns an explicit length, but
discards it: the result is read back through CStr::from_ptr (in
OpensslStringRef's Deref), dropping everything after the first NUL byte.
X.509 name entries can legally contain interior NULs, and seeing them
matters for detecting the classic NUL-byte-CN spoofing trick (e.g.
"example.com\0evil.com"). The truncation can't be fixed in place
because OpensslString's representation is inherently NUL-terminated.

Add Asn1StringRef::to_string, which preserves the full contents using
the length reported for the conversion. For string types whose UTF-8
conversion is the identity (UTF8String already containing valid UTF-8,
and the Latin-1 family when ASCII-only), it copies directly out of the
underlying buffer with a single allocation; otherwise it converts
through ASN1_STRING_to_UTF8 and copies the result (replacing any
invalid UTF-8 with U+FFFD rather than assuming validity unchecked).
The Debug impl now uses to_string, so interior NULs show up there too.

https://claude.ai/code/session_01JS5sEpf4jRcbgsqsfNnMPs
@alex alex merged commit 8b1519e into rust-openssl:master Jun 8, 2026
87 checks passed
@alex alex deleted the claude/confident-darwin-9QCvv branch June 8, 2026 02:42
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.

3 participants