5 cryptorand "crypto/rand"
9 "github.com/mjl-/mox/mlog"
12// StartTLSSessionTicketKeyRefresher sets session keys on the TLS config, and
13// rotates them periodically.
15// Useful for TLS configs that are being cloned for each connection. The
16// automatically managed keys would happen in the cloned config, and not make
17// it back to the base config.
18func StartTLSSessionTicketKeyRefresher(ctx context.Context, log mlog.Log, c *tls.Config) {
20 first := make(chan struct{})
22 // Similar to crypto/tls, we rotate keys once a day. Previous keys stay valid for 7
23 // days. We currently only store ticket keys in memory, so a restart invalidates
24 // previous session tickets. We could store them in the future.
28 if _, err := cryptorand.Read(nk[:]); err != nil {
34 keys = append([][32]byte{nk}, keys...)
35 c.SetSessionTicketKeys(keys)
42 ctxDone := Sleep(ctx, 24*time.Hour)
46 log.Info("rotating tls session keys")