The HTTP/2 server uses m2TLS server-side extensions:
-
Server context:
TLS.ContextCreateServer()creates a context withTLS_server_method()andSSL_VERIFY_NONE(servers don't verify clients by default). -
Certificate loading:
TLS.SetServerCert(ctx, certPath, keyPath)loads PEM certificate chain and private key. -
ALPN negotiation:
TLS.SetALPNServer(ctx, protos, protosLen)installs a callback that matches client-offered protocols against the server's list. The server advertisesh2only.
\x02h2 (* length-prefixed: 2 bytes "h2" *)
The server rejects connections that don't negotiate h2.
Each accepted connection gets its own TLS session via
TLS.SessionCreateServer(). This uses SSL_set_accept_state()
instead of SSL_set_connect_state().
The handshake is driven by the event loop:
- Watcher on client fd for read events
TLS.Handshake()returns WantRead/WantWrite during negotiation- On completion,
TLS.GetALPN()verifies "h2" was negotiated
- PEM format (both cert and key)
- Key must match certificate
- For development: generate self-signed cert with openssl
openssl req -x509 -newkey rsa:2048 -keyout server.key \
-out server.crt -days 365 -nodes -subj "/CN=localhost"