Standard library / crypto/tls
tls.QUICEncryptionLevelHandshake
Protection level for handshake CRYPTO messages after the initial exchange. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
const QUICEncryptionLevelHandshake QUICEncryptionLevelWorking Example
Example Snippetgo
package main
import ( "context" "crypto/ed25519" "crypto/rand" "crypto/tls" "crypto/x509" "crypto/x509/pkix" "fmt" "math/big" "time")
func main() { sc, cc := fixture() c, s, a, b := quicPair(sc, cc, false, false) defer c.Close() defer s.Close() _ = a _ = b fmt.Println(len(a.read[tls.QUICEncryptionLevelHandshake]) > 0)}
// Test fixture: synthetic CA and short-lived certificates, fixed test clock.func configs() (*tls.Config, *tls.Config) { now := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC) pub, key, e := ed25519.GenerateKey(rand.Reader) check(e) ca := &x509.Certificate{SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: "Test CA"}, NotBefore: now.Add(-time.Hour), NotAfter: now.Add(time.Hour), BasicConstraintsValid: true, IsCA: true, KeyUsage: x509.KeyUsageCertSign} root := must(x509.ParseCertificate(must(x509.CreateCertificate(rand.Reader, ca, ca, pub, key)))) lp, lk, e := ed25519.GenerateKey(rand.Reader) check(e) leaf := &x509.Certificate{SerialNumber: big.NewInt(2), DNSNames: []string{"example.invalid"}, NotBefore: ca.NotBefore, NotAfter: ca.NotAfter, KeyUsage: x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}} der := must(x509.CreateCertificate(rand.Reader, leaf, root, lp, key)) cert := tls.Certificate{Certificate: [][]byte{der}, PrivateKey: lk, Leaf: must(x509.ParseCertificate(der))} pool := x509.NewCertPool() pool.AddCert(root) server := &tls.Config{Certificates: []tls.Certificate{cert}, MinVersion: tls.VersionTLS13, Time: func() time.Time { return now }, NextProtos: []string{"demo"}} client := &tls.Config{RootCAs: pool, ServerName: "example.invalid", MinVersion: tls.VersionTLS13, Time: server.Time, NextProtos: []string{"demo"}} return server, client}
// In-memory TLS handshake only: not a QUIC packet implementation.// Events are drained serially, data is copied before advancing, secrets never printed.type trace struct { seen map[tls.QUICEventKind]bool state *tls.SessionState read, write map[tls.QUICEncryptionLevel][]byte params []byte}
func quicStart(q *tls.QUICConn) { q.SetTransportParameters([]byte{1, 2}) check(q.Start(context.Background()))}func quicPair(sc, cc *tls.Config, early, decline bool) (*tls.QUICConn, *tls.QUICConn, *trace, *trace) { c := tls.QUICClient(&tls.QUICConfig{TLSConfig: cc, EnableSessionEvents: true}) s := tls.QUICServer(&tls.QUICConfig{TLSConfig: sc, EnableSessionEvents: true}) quicStart(c) s.SetTransportParameters([]byte{3, 4}) check(s.Start(context.Background())) a, b := &trace{seen: map[tls.QUICEventKind]bool{}, read: map[tls.QUICEncryptionLevel][]byte{}, write: map[tls.QUICEncryptionLevel][]byte{}}, &trace{seen: map[tls.QUICEventKind]bool{}, read: map[tls.QUICEncryptionLevel][]byte{}, write: map[tls.QUICEncryptionLevel][]byte{}} for rounds := 0; rounds < 100; rounds++ { progress := false for i, q := range []*tls.QUICConn{c, s} { peer, t := s, a if i == 1 { peer, t = c, b } for count := 0; count < 100; count++ { e := q.NextEvent() if e.Kind == tls.QUICNoEvent { break } progress = true t.seen[e.Kind] = true switch e.Kind { case tls.QUICWriteData: check(peer.HandleData(e.Level, append([]byte(nil), e.Data...))) case tls.QUICSetReadSecret: t.read[e.Level] = append([]byte(nil), e.Data...) case tls.QUICSetWriteSecret: t.write[e.Level] = append([]byte(nil), e.Data...) case tls.QUICTransportParameters: t.params = append([]byte(nil), e.Data...) case tls.QUICTransportParametersRequired: panic("unexpected parameter request") case tls.QUICHandshakeDone: if i == 1 { check(s.SendSessionTicket(tls.QUICSessionTicketOptions{EarlyData: early, Extra: [][]byte{[]byte("demo:v1")}})) } case tls.QUICResumeSession: if decline && i == 1 { e.SessionState.EarlyData = false } case tls.QUICStoreSession: t.state = e.SessionState check(q.StoreSession(e.SessionState)) } } } if !progress { if !c.ConnectionState().HandshakeComplete || !s.ConnectionState().HandshakeComplete { panic("handshake incomplete") } return c, s, a, b } } panic("event limit exceeded")}func fixture() (*tls.Config, *tls.Config) { sc, cc := configs() cc.ClientSessionCache = tls.NewLRUClientSessionCache(4) return sc, cc}func session() (*tls.ClientSessionState, *tls.SessionState, []byte) { sc, cc := fixture() c, s, a, _ := quicPair(sc, cc, false, false) defer c.Close() defer s.Close() cs, ok := cc.ClientSessionCache.Get(cc.ServerName) if !ok || cs == nil || a.state == nil { panic("no session") } ticket, state, e := cs.ResumptionState() check(e) return cs, state, ticket}func ticketFixture() (*tls.Config, tls.ConnectionState, *tls.SessionState) { sc, cc := fixture() var state *tls.SessionState var conn tls.ConnectionState sc.WrapSession = func(cs tls.ConnectionState, ss *tls.SessionState) ([]byte, error) { state = ss conn = cs return sc.EncryptTicket(cs, ss) } c, s, _, _ := quicPair(sc, cc, false, false) defer c.Close() defer s.Close() if state == nil { panic("no server state") } return sc, conn, state}func resumed(early, decline bool) (*tls.QUICConn, *tls.QUICConn, *trace, *trace) { sc, cc := fixture() c, s, _, _ := quicPair(sc, cc, early, false) check(c.Close()) check(s.Close()) return quicPair(sc, cc, early, decline)}
func must[T any](v T, e error) T { if e != nil { panic(e) } return v}func check(e error) { if e != nil { panic(e) }}