| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // Package distributedstek provides TLS session ticket ephemeral |
| 16 | // keys (STEKs) in a distributed fashion by utilizing configured |
| 17 | // storage for locking and key sharing. This allows a cluster of |
| 18 | // machines to optimally resume TLS sessions in a load-balanced |
| 19 | // environment without any hassle. This is similar to what |
| 20 | // Twitter does, but without needing to rely on SSH, as it is |
| 21 | // built into the web server this way: |
| 22 | // https://blog.twitter.com/engineering/en_us/a/2013/forward-secrecy-at-twitter.html |
| 23 | package distributedstekscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/ |
| 24 | |
| 25 | import ( |
| 26 | "bytesscip-go gomod github.com/golang/go/src go1.25.1 bytes/" |
| 27 | "encoding/gobscip-go gomod github.com/golang/go/src go1.25.1 `encoding/gob`/" |
| 28 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/" |
| 29 | "errorsscip-go gomod github.com/golang/go/src go1.25.1 errors/" |
| 30 | "fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/" |
| 31 | "io/fsscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/" |
| 32 | "logscip-go gomod github.com/golang/go/src go1.25.1 log/" |
| 33 | "runtime/debugscip-go gomod github.com/golang/go/src go1.25.1 `runtime/debug`/" |
| 34 | "timescip-go gomod github.com/golang/go/src go1.25.1 time/" |
| 35 | |
| 36 | "github.com/caddyserver/certmagicscip-go gomod github.com/caddyserver/certmagic v0.25.3 `github.com/caddyserver/certmagic`/" |
| 37 | |
| 38 | "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/" |
| 39 | "github.com/caddyserver/caddy/v2/modules/caddytlsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/" |
| 40 | ) |
| 41 | |
| 42 | func initscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/init().() { |
| 43 | caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.RegisterModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/RegisterModule().(Providerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#{}) |
| 44 | } |
| 45 | |
| 46 | // Provider implements a distributed STEK provider. This |
| 47 | // module will obtain STEKs from a storage module instead |
| 48 | // of generating STEKs internally. This allows STEKs to be |
| 49 | // coordinated, improving TLS session resumption in a cluster. |
| 50 | type Providerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider# struct { |
| 51 | // The storage module wherein to store and obtain session |
| 52 | // ticket keys. If unset, Caddy's default/global-configured |
| 53 | // storage module will be used. |
| 54 | Storagescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#Storage. jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/.RawMessagescip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/RawMessage# `json:"storage,omitempty" caddy:"namespace=caddy.storage inline_key=module"` |
| 55 | |
| 56 | storagescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#storage. certmagicscip-go gomod github.com/caddyserver/certmagic v0.25.3 `github.com/caddyserver/certmagic`/.Storagescip-go gomod github.com/caddyserver/certmagic v0.25.3 `github.com/caddyserver/certmagic`/Storage# |
| 57 | stekConfigscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#stekConfig. *caddytlsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/.SessionTicketServicescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/SessionTicketService# |
| 58 | timerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#timer. *timescip-go gomod github.com/golang/go/src go1.25.1 time/.Timerscip-go gomod github.com/golang/go/src go1.25.1 time/Timer# |
| 59 | ctxscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#ctx. caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context# |
| 60 | } |
| 61 | |
| 62 | // CaddyModule returns the Caddy module information. |
| 63 | func (Providerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#) CaddyModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#CaddyModule().() caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleInfoscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo# { |
| 64 | return caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleInfoscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#{ |
| 65 | IDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#ID.: "tls.stek.distributed", |
| 66 | Newscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#New.: func() caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Modulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Module# { return new(Providerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#) }, |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Provision provisions s. |
| 71 | func (s *Providerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#) Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#Provision().(ctx caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#) error { |
| 72 | s.ctxscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#ctx. = ctx |
| 73 | |
| 74 | // unpack the storage module to use, if different from the default |
| 75 | if s.Storagescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#Storage. != nil { |
| 76 | val, err := ctx.LoadModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#LoadModule().(s, "Storage") |
| 77 | if err != nil { |
| 78 | return fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("loading TLS storage module: %s", err) |
| 79 | } |
| 80 | cmStorage, err := val.(caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.StorageConverterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/StorageConverter#).CertMagicStoragescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/StorageConverter#CertMagicStorage.() |
| 81 | if err != nil { |
| 82 | return fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("creating TLS storage configuration: %v", err) |
| 83 | } |
| 84 | s.storagescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#storage. = cmStorage |
| 85 | } |
| 86 | |
| 87 | // otherwise, use default storage |
| 88 | if s.storagescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#storage. == nil { |
| 89 | s.storagescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#storage. = ctx.Storagescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#Storage().() |
| 90 | } |
| 91 | |
| 92 | return nil |
| 93 | } |
| 94 | |
| 95 | // Initialize sets the configuration for s and returns the starting keys. |
| 96 | func (s *Providerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#) Initializescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#Initialize().(config *caddytlsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/.SessionTicketServicescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/SessionTicketService#) ([][32]byte, error) { |
| 97 | // keep a reference to the config; we'll need it when rotating keys |
| 98 | s.stekConfigscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#stekConfig. = config |
| 99 | |
| 100 | dstek, err := s.getSTEKscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#getSTEK().() |
| 101 | if err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | |
| 105 | // create timer for the remaining time on the interval; |
| 106 | // this timer is cleaned up only when rotate() returns |
| 107 | s.timerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#timer. = timescip-go gomod github.com/golang/go/src go1.25.1 time/.NewTimerscip-go gomod github.com/golang/go/src go1.25.1 time/NewTimer().(timescip-go gomod github.com/golang/go/src go1.25.1 time/.Untilscip-go gomod github.com/golang/go/src go1.25.1 time/Until().(dstek.NextRotationscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/distributedSTEK#NextRotation.)) |
| 108 | |
| 109 | return dstek.Keysscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/distributedSTEK#Keys., nil |
| 110 | } |
| 111 | |
| 112 | // Next returns a channel which transmits the latest session ticket keys. |
| 113 | func (s *Providerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#) Nextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#Next().(doneChan <-chan struct{}) <-chan [][32]byte { |
| 114 | keysChan := make(chan [][32]byte) |
| 115 | go s.rotatescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#rotate().(doneChan, keysChan) |
| 116 | return keysChan |
| 117 | } |
| 118 | |
| 119 | func (s *Providerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#) loadSTEKscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#loadSTEK().() (distributedSTEKscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/distributedSTEK#, error) { |
| 120 | var sg distributedSTEKscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/distributedSTEK# |
| 121 | gobBytes, err := s.storagescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#storage..Loadscip-go gomod github.com/caddyserver/certmagic v0.25.3 `github.com/caddyserver/certmagic`/Storage#Load().(s.ctxscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#ctx., stekFileNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/stekFileName.) |
| 122 | if err != nil { |
| 123 | return sg, err // don't wrap, in case error is certmagic.ErrNotExist |
| 124 | } |
| 125 | dec := gobscip-go gomod github.com/golang/go/src go1.25.1 `encoding/gob`/.NewDecoderscip-go gomod github.com/golang/go/src go1.25.1 `encoding/gob`/NewDecoder().(bytesscip-go gomod github.com/golang/go/src go1.25.1 bytes/.NewReaderscip-go gomod github.com/golang/go/src go1.25.1 bytes/NewReader().(gobBytes)) |
| 126 | err = dec.Decodescip-go gomod github.com/golang/go/src go1.25.1 `encoding/gob`/Decoder#Decode().(&sg) |
| 127 | if err != nil { |
| 128 | return sg, fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("STEK gob corrupted: %v", err) |
| 129 | } |
| 130 | return sg, nil |
| 131 | } |
| 132 | |
| 133 | func (s *Providerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#) storeSTEKscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#storeSTEK().(dstek distributedSTEKscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/distributedSTEK#) error { |
| 134 | var buf bytesscip-go gomod github.com/golang/go/src go1.25.1 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.1 bytes/Buffer# |
| 135 | err := gobscip-go gomod github.com/golang/go/src go1.25.1 `encoding/gob`/.NewEncoderscip-go gomod github.com/golang/go/src go1.25.1 `encoding/gob`/NewEncoder().(&buf).Encodescip-go gomod github.com/golang/go/src go1.25.1 `encoding/gob`/Encoder#Encode().(dstek) |
| 136 | if err != nil { |
| 137 | return fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("encoding STEK gob: %v", err) |
| 138 | } |
| 139 | err = s.storagescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#storage..Storescip-go gomod github.com/caddyserver/certmagic v0.25.3 `github.com/caddyserver/certmagic`/Storage#Store().(s.ctxscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#ctx., stekFileNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/stekFileName., buf.Bytesscip-go gomod github.com/golang/go/src go1.25.1 bytes/Buffer#Bytes().()) |
| 140 | if err != nil { |
| 141 | return fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("storing STEK gob: %v", err) |
| 142 | } |
| 143 | return nil |
| 144 | } |
| 145 | |
| 146 | // getSTEK locks and loads the current STEK from storage. If none |
| 147 | // currently exists, a new STEK is created and persisted. If the |
| 148 | // current STEK is outdated (NextRotation time is in the past), |
| 149 | // then it is rotated and persisted. The resulting STEK is returned. |
| 150 | func (s *Providerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#) getSTEKscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#getSTEK().() (distributedSTEKscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/distributedSTEK#, error) { |
| 151 | err := s.storagescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#storage..Lockscip-go gomod github.com/caddyserver/certmagic v0.25.3 `github.com/caddyserver/certmagic`/Locker#Lock().(s.ctxscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#ctx., stekLockNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/stekLockName.) |
| 152 | if err != nil { |
| 153 | return distributedSTEKscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/distributedSTEK#{}, fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("failed to acquire storage lock: %v", err) |
| 154 | } |
| 155 | |
| 156 | //nolint:errcheck |
| 157 | defer s.storagescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#storage..Unlockscip-go gomod github.com/caddyserver/certmagic v0.25.3 `github.com/caddyserver/certmagic`/Locker#Unlock().(s.ctxscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#ctx., stekLockNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/stekLockName.) |
| 158 | |
| 159 | // load the current STEKs from storage |
| 160 | dstek, err := s.loadSTEKscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#loadSTEK().() |
| 161 | if errorsscip-go gomod github.com/golang/go/src go1.25.1 errors/.Isscip-go gomod github.com/golang/go/src go1.25.1 errors/Is().(err, fsscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/.ErrNotExistscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/ErrNotExist.) { |
| 162 | // if there is none, then make some right away |
| 163 | dstek, err = s.rotateKeysscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#rotateKeys().(dstek) |
| 164 | if err != nil { |
| 165 | return dstek, fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("creating new STEK: %v", err) |
| 166 | } |
| 167 | } else if err != nil { |
| 168 | // some other error, that's a problem |
| 169 | return dstek, fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("loading STEK: %v", err) |
| 170 | } else if timescip-go gomod github.com/golang/go/src go1.25.1 time/.Nowscip-go gomod github.com/golang/go/src go1.25.1 time/Now().().Afterscip-go gomod github.com/golang/go/src go1.25.1 time/Time#After().(dstek.NextRotationscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/distributedSTEK#NextRotation.) { |
| 171 | // if current STEKs are outdated, rotate them |
| 172 | dstek, err = s.rotateKeysscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#rotateKeys().(dstek) |
| 173 | if err != nil { |
| 174 | return dstek, fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("rotating keys: %v", err) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return dstek, nil |
| 179 | } |
| 180 | |
| 181 | // rotateKeys rotates the keys of oldSTEK and returns the new distributedSTEK |
| 182 | // with updated keys and timestamps. It stores the returned STEK in storage, |
| 183 | // so this function must only be called in a storage-provided lock. |
| 184 | func (s *Providerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#) rotateKeysscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#rotateKeys().(oldSTEK distributedSTEKscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/distributedSTEK#) (distributedSTEKscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/distributedSTEK#, error) { |
| 185 | var newSTEK distributedSTEKscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/distributedSTEK# |
| 186 | var err error |
| 187 | |
| 188 | newSTEK.Keysscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/distributedSTEK#Keys., err = s.stekConfigscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#stekConfig..RotateSTEKsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/SessionTicketService#RotateSTEKs().(oldSTEK.Keysscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/distributedSTEK#Keys.) |
| 189 | if err != nil { |
| 190 | return newSTEK, err |
| 191 | } |
| 192 | |
| 193 | now := timescip-go gomod github.com/golang/go/src go1.25.1 time/.Nowscip-go gomod github.com/golang/go/src go1.25.1 time/Now().() |
| 194 | newSTEK.LastRotationscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/distributedSTEK#LastRotation. = now |
| 195 | newSTEK.NextRotationscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/distributedSTEK#NextRotation. = now.Addscip-go gomod github.com/golang/go/src go1.25.1 time/Time#Add().(timescip-go gomod github.com/golang/go/src go1.25.1 time/.Durationscip-go gomod github.com/golang/go/src go1.25.1 time/Duration#(s.stekConfigscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#stekConfig..RotationIntervalscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/SessionTicketService#RotationInterval.)) |
| 196 | |
| 197 | err = s.storeSTEKscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#storeSTEK().(newSTEK) |
| 198 | if err != nil { |
| 199 | return newSTEK, err |
| 200 | } |
| 201 | |
| 202 | return newSTEK, nil |
| 203 | } |
| 204 | |
| 205 | // rotate rotates keys on a regular basis, sending each updated set of |
| 206 | // keys down keysChan, until doneChan is closed. |
| 207 | func (s *Providerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#) rotatescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#rotate().(doneChan <-chan struct{}, keysChan chan<- [][32]byte) { |
| 208 | defer func() { |
| 209 | if err := recover(); err != nil { |
| 210 | logscip-go gomod github.com/golang/go/src go1.25.1 log/.Printfscip-go gomod github.com/golang/go/src go1.25.1 log/Printf().("[PANIC] distributed STEK rotation: %v\n%s", err, debugscip-go gomod github.com/golang/go/src go1.25.1 `runtime/debug`/.Stackscip-go gomod github.com/golang/go/src go1.25.1 `runtime/debug`/Stack().()) |
| 211 | } |
| 212 | }() |
| 213 | for { |
| 214 | select { |
| 215 | case <-s.timerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#timer..Cscip-go gomod github.com/golang/go/src go1.25.1 time/Timer#C.: |
| 216 | dstek, err := s.getSTEKscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#getSTEK().() |
| 217 | if err != nil { |
| 218 | // TODO: improve this handling |
| 219 | logscip-go gomod github.com/golang/go/src go1.25.1 log/.Printfscip-go gomod github.com/golang/go/src go1.25.1 log/Printf().("[ERROR] Loading STEK: %v", err) |
| 220 | continue |
| 221 | } |
| 222 | |
| 223 | // send the updated keys to the service |
| 224 | keysChan <- dstek.Keysscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/distributedSTEK#Keys. |
| 225 | |
| 226 | // timer channel is already drained, so reset directly (see godoc) |
| 227 | s.timerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#timer..Resetscip-go gomod github.com/golang/go/src go1.25.1 time/Timer#Reset().(timescip-go gomod github.com/golang/go/src go1.25.1 time/.Untilscip-go gomod github.com/golang/go/src go1.25.1 time/Until().(dstek.NextRotationscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/distributedSTEK#NextRotation.)) |
| 228 | |
| 229 | case <-doneChan: |
| 230 | // again, see godocs for why timer is stopped this way |
| 231 | if !s.timerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#timer..Stopscip-go gomod github.com/golang/go/src go1.25.1 time/Timer#Stop().() { |
| 232 | <-s.timerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#timer..Cscip-go gomod github.com/golang/go/src go1.25.1 time/Timer#C. |
| 233 | } |
| 234 | return |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | type distributedSTEKscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/distributedSTEK# struct { |
| 240 | Keysscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/distributedSTEK#Keys. [][32]byte |
| 241 | LastRotationscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/distributedSTEK#LastRotation., NextRotationscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/distributedSTEK#NextRotation. timescip-go gomod github.com/golang/go/src go1.25.1 time/.Timescip-go gomod github.com/golang/go/src go1.25.1 time/Time# |
| 242 | } |
| 243 | |
| 244 | const ( |
| 245 | stekLockNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/stekLockName. = "stek_check" |
| 246 | stekFileNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/stekFileName. = "stek/stek.bin" |
| 247 | ) |
| 248 | |
| 249 | // Interface guard |
| 250 | var _ caddytlsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/.STEKProviderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/STEKProvider# = (*Providerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/distributedstek`/Provider#)(nil) |