| 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 standardstekscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/ |
| 16 | |
| 17 | import ( |
| 18 | "logscip-go gomod github.com/golang/go/src go1.25.1 log/" |
| 19 | "runtime/debugscip-go gomod github.com/golang/go/src go1.25.1 `runtime/debug`/" |
| 20 | "syncscip-go gomod github.com/golang/go/src go1.25.1 sync/" |
| 21 | "timescip-go gomod github.com/golang/go/src go1.25.1 time/" |
| 22 | |
| 23 | "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/" |
| 24 | "github.com/caddyserver/caddy/v2/modules/caddytlsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/" |
| 25 | ) |
| 26 | |
| 27 | func initscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/init().() { |
| 28 | 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().(standardSTEKProviderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#{}) |
| 29 | } |
| 30 | |
| 31 | type standardSTEKProviderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider# struct { |
| 32 | stekConfigscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#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# |
| 33 | timerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#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# |
| 34 | } |
| 35 | |
| 36 | // CaddyModule returns the Caddy module information. |
| 37 | func (standardSTEKProviderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#) CaddyModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#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# { |
| 38 | 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#{ |
| 39 | IDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#ID.: "tls.stek.standard", |
| 40 | 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(standardSTEKProviderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#) }, |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // Initialize sets the configuration for s and returns the starting keys. |
| 45 | func (s *standardSTEKProviderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#) Initializescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#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) { |
| 46 | // keep a reference to the config; we'll need it when rotating keys |
| 47 | s.stekConfigscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#stekConfig. = config |
| 48 | |
| 49 | itvl := 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/standardstek`/standardSTEKProvider#stekConfig..RotationIntervalscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/SessionTicketService#RotationInterval.) |
| 50 | |
| 51 | mutexscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/mutex..Lockscip-go gomod github.com/golang/go/src go1.25.1 sync/RWMutex#Lock().() |
| 52 | defer mutexscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/mutex..Unlockscip-go gomod github.com/golang/go/src go1.25.1 sync/RWMutex#Unlock().() |
| 53 | |
| 54 | // if this is our first rotation or we are overdue |
| 55 | // for one, perform a rotation immediately; otherwise, |
| 56 | // we assume that the keys are non-empty and fresh |
| 57 | since := timescip-go gomod github.com/golang/go/src go1.25.1 time/.Sincescip-go gomod github.com/golang/go/src go1.25.1 time/Since().(lastRotationscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/lastRotation.) |
| 58 | if lastRotationscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/lastRotation..IsZeroscip-go gomod github.com/golang/go/src go1.25.1 time/Time#IsZero().() || since > itvl { |
| 59 | var err error |
| 60 | keysscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/keys., err = s.stekConfigscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#stekConfig..RotateSTEKsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/SessionTicketService#RotateSTEKs().(keysscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/keys.) |
| 61 | if err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | since = 0 // since this is overdue or is the first rotation, use full interval |
| 65 | lastRotationscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/lastRotation. = 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().() |
| 66 | } |
| 67 | |
| 68 | // create timer for the remaining time on the interval; |
| 69 | // this timer is cleaned up only when Next() returns |
| 70 | s.timerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#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().(itvl - since) |
| 71 | |
| 72 | return keysscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/keys., nil |
| 73 | } |
| 74 | |
| 75 | // Next returns a channel which transmits the latest session ticket keys. |
| 76 | func (s *standardSTEKProviderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#) Nextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#Next().(doneChan <-chan struct{}) <-chan [][32]byte { |
| 77 | keysChan := make(chan [][32]byte) |
| 78 | go s.rotatescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#rotate().(doneChan, keysChan) |
| 79 | return keysChan |
| 80 | } |
| 81 | |
| 82 | // rotate rotates keys on a regular basis, sending each updated set of |
| 83 | // keys down keysChan, until doneChan is closed. |
| 84 | func (s *standardSTEKProviderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#) rotatescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#rotate().(doneChan <-chan struct{}, keysChan chan<- [][32]byte) { |
| 85 | defer func() { |
| 86 | if err := recover(); err != nil { |
| 87 | 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] standard 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().()) |
| 88 | } |
| 89 | }() |
| 90 | for { |
| 91 | select { |
| 92 | case now := <-s.timerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#timer..Cscip-go gomod github.com/golang/go/src go1.25.1 time/Timer#C.: |
| 93 | // copy the slice header to avoid races |
| 94 | mutexscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/mutex..RLockscip-go gomod github.com/golang/go/src go1.25.1 sync/RWMutex#RLock().() |
| 95 | keysCopy := keysscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/keys. |
| 96 | mutexscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/mutex..RUnlockscip-go gomod github.com/golang/go/src go1.25.1 sync/RWMutex#RUnlock().() |
| 97 | |
| 98 | // generate a new key, rotating old ones |
| 99 | var err error |
| 100 | keysCopy, err = s.stekConfigscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#stekConfig..RotateSTEKsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/SessionTicketService#RotateSTEKs().(keysCopy) |
| 101 | if err != nil { |
| 102 | // TODO: improve this handling |
| 103 | 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] Generating STEK: %v", err) |
| 104 | continue |
| 105 | } |
| 106 | |
| 107 | // replace keys slice with updated value and |
| 108 | // record the timestamp of rotation |
| 109 | mutexscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/mutex..Lockscip-go gomod github.com/golang/go/src go1.25.1 sync/RWMutex#Lock().() |
| 110 | keysscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/keys. = keysCopy |
| 111 | lastRotationscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/lastRotation. = now |
| 112 | mutexscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/mutex..Unlockscip-go gomod github.com/golang/go/src go1.25.1 sync/RWMutex#Unlock().() |
| 113 | |
| 114 | // send the updated keys to the service |
| 115 | keysChan <- keysCopy |
| 116 | |
| 117 | // timer channel is already drained, so reset directly (see godoc) |
| 118 | s.timerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#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/.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/standardstek`/standardSTEKProvider#stekConfig..RotationIntervalscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/SessionTicketService#RotationInterval.)) |
| 119 | |
| 120 | case <-doneChan: |
| 121 | // again, see godocs for why timer is stopped this way |
| 122 | if !s.timerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#timer..Stopscip-go gomod github.com/golang/go/src go1.25.1 time/Timer#Stop().() { |
| 123 | <-s.timerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#timer..Cscip-go gomod github.com/golang/go/src go1.25.1 time/Timer#C. |
| 124 | } |
| 125 | return |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | var ( |
| 131 | lastRotationscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/lastRotation. 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# |
| 132 | keysscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/keys. [][32]byte |
| 133 | mutexscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/mutex. syncscip-go gomod github.com/golang/go/src go1.25.1 sync/.RWMutexscip-go gomod github.com/golang/go/src go1.25.1 sync/RWMutex# // protects keys and lastRotation |
| 134 | ) |
| 135 | |
| 136 | // Interface guard |
| 137 | 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# = (*standardSTEKProviderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls/standardstek`/standardSTEKProvider#)(nil) |