Skip to content

Commit beb6e4f

Browse files
committed
wire TLS ticket auth data into request context
1 parent 9b292bb commit beb6e4f

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,9 @@ func run() int {
893893
BaseContext: func(_ net.Listener) context.Context {
894894
return stopContext
895895
},
896+
ConnContext: func(ctx context.Context, conn net.Conn) context.Context {
897+
return tlsutil.NonDefaultKeyUsedToContext(ctx, conn)
898+
},
896899
}
897900
if args.disableHTTP2 {
898901
server.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))

tlsutil/preserve.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package tlsutil
22

33
import (
4+
"context"
45
"crypto/tls"
56
"errors"
67
"net"
78
)
89

910
type preservedKeyKey struct{}
10-
1111
type nonDefaultKeyUsedKey struct{}
12+
type connKey struct{}
1213

1314
func saveConnKey(conn ConnTagger, key [32]byte) {
1415
conn.SetTag(preservedKeyKey{}, key)
@@ -48,6 +49,19 @@ func WasNonDefaultKeyUsed(conn net.Conn) bool {
4849
return val
4950
}
5051

52+
func NonDefaultKeyUsedToContext(ctx context.Context, conn net.Conn) context.Context {
53+
return context.WithValue(ctx, connKey{}, conn)
54+
}
55+
56+
func NonDefaultKeyUsedFromContext(ctx context.Context) bool {
57+
val := ctx.Value(connKey{})
58+
conn, ok := val.(net.Conn)
59+
if !ok {
60+
return false
61+
}
62+
return WasNonDefaultKeyUsed(conn)
63+
}
64+
5165
func PreserveSessionKeys(cfg *tls.Config, keys [][32]byte) *tls.Config {
5266
if len(keys) < 2 {
5367
// there's just one key defined, nothing to do
@@ -88,13 +102,14 @@ func PreserveSessionKeys(cfg *tls.Config, keys [][32]byte) *tls.Config {
88102
return nil, nil
89103
}
90104
cfg.WrapSession = func(cs tls.ConnectionState, ss *tls.SessionState) ([]byte, error) {
105+
skCfg := cfg.Clone()
106+
skCfg.SessionTicketKey = [32]byte{}
107+
key := keys[0]
91108
// is there previous key? if so, use it
92-
if key, ok := getConnKey(conn); ok {
93-
skCfg := cfg.Clone()
94-
skCfg.SessionTicketKey = [32]byte{}
95-
skCfg.SetSessionTicketKeys([][32]byte{key})
96-
return skCfg.EncryptTicket(cs, ss)
109+
if k, ok := getConnKey(conn); ok {
110+
key = k
97111
}
112+
skCfg.SetSessionTicketKeys([][32]byte{key})
98113
return cfg.EncryptTicket(cs, ss)
99114
}
100115
return cfg, nil

0 commit comments

Comments
 (0)