Add SNI-based certificate selection to TLS 1.3 server handshake#28
Closed
pomelio wants to merge 1 commit into
Closed
Add SNI-based certificate selection to TLS 1.3 server handshake#28pomelio wants to merge 1 commit into
pomelio wants to merge 1 commit into
Conversation
Add SNI-based certificate selection to TLS 1.3 server handshake
**PR Description**
This change adds Server Name Indication (SNI) support to the TLS 1.3
server handshake.
The server now parses the `server_name` extension from `ClientHello`,
stores the requested hostname in handshake state, and can select a
certificate dynamically through a new `Options.sni_auth` callback. If
the callback does not return a certificate, the existing `auth` field is
still used as the default fallback.
- Added `Options.sni_auth` to support dynamic certificate selection
based on SNI.
- Added `Handshake.server_name` state to retain the hostname parsed from
`ClientHello`.
- Implemented parsing for the `server_name` TLS extension.
- Changed server certificate selection to use the resolved SNI-specific
certificate instead of always using `opt.auth`.
- Deferred signature scheme validation until after certificate
selection, so validation is performed against the actual certificate
chosen for the handshake.
- Added tests covering:
- existing `ClientHello` parsing behavior
- SNI parsing and SNI-driven certificate selection
Before this change, the server always used a single fixed certificate
from `opt.auth`, regardless of the hostname requested by the client.
That made virtual hosting impossible.
This patch enables:
- serving multiple hostnames from the same listener
- choosing the correct certificate during handshake
- preserving backward compatibility for callers that only use `auth`
New server option:
```zig
sni_auth: ?SniAuth = null
```
Where `SniAuth` is:
```zig
pub const SniAuth = struct {
ctx: *anyopaque,
selectFn: *const fn (ctx: *anyopaque, server_name: ?[]const u8)
?*CertKeyPair,
};
```
Behavior:
- `selectFn` is called after parsing SNI from `ClientHello`
- if it returns a certificate, that certificate is used
- if it returns `null`, the handshake falls back to `auth`
This change is backward compatible:
- existing users of `auth` continue to work unchanged
- `sni_auth` is optional
I could not use full `zig test` output as the validation signal because
the current repository does not compile cleanly against the local Zig
standard library version for unrelated reasons. The modified file passes
formatting checks with:
```sh
zig fmt --check src/handshake_server.zig
```
Owner
Use Zig version from build.zig.zon or the latest one 0.16.0-dev.3061+9b1eaad13. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add SNI-based certificate selection to TLS 1.3 server handshake
PR Description
This change adds Server Name Indication (SNI) support to the TLS 1.3 server handshake.
The server now parses the
server_nameextension fromClientHello, stores the requested hostname in handshake state, and can select a certificate dynamically through a newOptions.sni_authcallback. If the callback does not return a certificate, the existingauthfield is still used as the default fallback.Options.sni_authto support dynamic certificate selection based on SNI.Handshake.server_namestate to retain the hostname parsed fromClientHello.server_nameTLS extension.opt.auth.ClientHelloparsing behaviorBefore this change, the server always used a single fixed certificate from
opt.auth, regardless of the hostname requested by the client. That made virtual hosting impossible.This patch enables:
authNew server option:
Where
SniAuthis:Behavior:
selectFnis called after parsing SNI fromClientHellonull, the handshake falls back toauthThis change is backward compatible:
authcontinue to work unchangedsni_authis optionalI could not use full
zig testoutput as the validation signal because the current repository does not compile cleanly against the local Zig standard library version for unrelated reasons. The modified file passes formatting checks with: