-
Notifications
You must be signed in to change notification settings - Fork 11
/
tls.go
355 lines (313 loc) · 8.97 KB
/
tls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package j8a
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/tls"
"crypto/x509"
"encoding/hex"
"errors"
"fmt"
"github.com/hako/durafmt"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"math"
"math/big"
"strings"
"time"
)
type PDuration time.Duration
const Days30 = time.Duration(time.Hour * 24 * 30)
const Days398 = PDuration(time.Hour * 24 * 398)
func (p PDuration) AsString() string {
return durafmt.Parse(time.Duration(p)).LimitFirstN(2).String()
}
func (p PDuration) AsDuration() time.Duration {
return time.Duration(p)
}
func (p PDuration) AsDays() int {
return int(p.AsDuration().Hours() / 24)
}
type TlsLink struct {
cert *x509.Certificate
issued time.Time
remainingValidity PDuration
totalValidity PDuration
browserValidity PDuration
earliestExpiry bool
isCA bool
}
func (t TlsLink) expiresTooCloseForComfort() bool {
gracePeriodDays := Days30
if Runner.Connection.Downstream.Tls.Acme.GracePeriodDays > 0 {
gracePeriodDays = time.Hour * 24 * time.Duration(Runner.Connection.Downstream.Tls.Acme.GracePeriodDays)
}
return time.Duration(t.remainingValidity) <= gracePeriodDays
}
func (t TlsLink) expiryLongerThanLegalBrowserMaximum() bool {
return t.browserValidity < t.remainingValidity
}
func (t TlsLink) legalBrowserValidityPeriodPassed() bool {
return t.browserValidity < 0
}
func (t TlsLink) printRemainingValidity() string {
rv := t.remainingValidity.AsString()
if t.earliestExpiry {
rv = rv + ", which is the earliest in your chain"
}
return rv
}
func (r *Runtime) tlsHealthCheck(daemon bool) {
defer func() {
if r := recover(); r != nil {
log.Trace().Msgf("TLS cert not analysed, cause: %s", r)
}
}()
//safety first
if r.ReloadableCert.Cert != nil {
Daemon:
for {
//Andeka is checking our certificate chains forever.
andeka, _ := checkFullCertChain(r.ReloadableCert.Cert)
logCertStats(andeka)
if andeka[0].expiresTooCloseForComfort() {
r.renewAcmeCertAndKey()
}
if daemon {
time.Sleep(time.Hour * 24)
} else {
break Daemon
}
}
}
}
const acmeRetry24h = "unable to renew ACME certificate from provider %s, cause: %s, will retry in 24h"
func (r *Runtime) renewAcmeCertAndKey() error {
p := r.Connection.Downstream.Tls.Acme.Provider
log.Info().Msgf("triggering renewal of ACME certificate from provider %s ", p)
e1 := r.fetchAcmeCertAndKey(acmeProviders[p].endpoint)
if e1 == nil {
c := []byte(r.Connection.Downstream.Tls.Cert)
k := []byte(r.Connection.Downstream.Tls.Key)
if newCerts, e2 := checkFullCertChainFromBytes(c, k); e2 != nil {
log.Warn().Msgf(acmeRetry24h, p, e2)
return e2
} else {
//if no issues, cache the cert and key. we don't assert whether this works it only matters when loading.
r.cacheAcmeCertAndKey(acmeProviders[p].endpoint)
//now trigger a re-init of TLS cert for the cert we just downloaded.
e3 := r.ReloadableCert.triggerInit()
if e3 == nil {
logCertStats(newCerts)
log.Info().Msgf("successful renewal of ACME certificate from provider %s complete", p)
} else {
log.Warn().Msgf(acmeRetry24h, p, e3)
return e3
}
}
}
return nil
}
func checkFullCertChainFromBytes(cert []byte, key []byte) ([]TlsLink, error) {
var chain tls.Certificate
var e1 error
chain, e1 = tls.X509KeyPair(cert, key)
if e1 != nil {
return nil, e1
}
return checkFullCertChain(&chain)
}
func checkFullCertChain(chain *tls.Certificate) ([]TlsLink, error) {
if len(chain.Certificate) == 0 {
return nil, errors.New("no certificate data found")
}
var e2 error
chain.Leaf, e2 = x509.ParseCertificate(chain.Certificate[0])
if e2 != nil {
return nil, e2
}
if chain.Leaf.DNSNames == nil || len(chain.Leaf.DNSNames) == 0 {
return nil, errors.New("no DNS name specified")
}
inter, root, e3 := splitCertPools(chain)
if e3 != nil {
return nil, e3
}
verified, e4 := chain.Leaf.Verify(verifyOptions(inter, root))
if e4 != nil {
return nil, e4
}
return parseTlsLinks(verified[0]), nil
}
func verifyOptions(inter *x509.CertPool, root *x509.CertPool) x509.VerifyOptions {
opts := x509.VerifyOptions{}
if inter != nil && len(inter.Subjects()) > 0 {
opts.Intermediates = inter
}
if root != nil && len(root.Subjects()) > 0 {
opts.Roots = root
}
return opts
}
func formatSerial(serial *big.Int) string {
serial = serial.Abs(serial)
hex := fmt.Sprintf("%X", serial)
if len(hex)%2 != 0 {
hex = "0" + hex
}
if len(hex) > 2 {
frm := strings.Builder{}
for i := 0; i < len(hex); i += 2 {
var j = 0
if i+2 <= len(hex) {
j = i + 2
} else {
j = len(hex)
}
w := hex[i:j]
frm.WriteString(w)
if i < len(hex)-2 {
frm.WriteString(":")
}
}
hex = frm.String()
}
return hex
}
func sha1Fingerprint(cert *x509.Certificate) string {
sha1 := sha1.Sum(cert.Raw)
return "#" + JoinHashString(sha1[:])
}
func sha256Fingerprint(cert *x509.Certificate) string {
sha256 := sha256.Sum256(cert.Raw)
return "#" + JoinHashString(sha256[:])
}
func md5Fingerprint(cert *x509.Certificate) string {
md5 := md5.Sum(cert.Raw)
return "#" + JoinHashString(md5[:])
}
func JoinHashString(hash []byte) string {
return strings.Join(ChunkString(strings.ToUpper(hex.EncodeToString(hash[:])), 2), ":")
}
func ChunkString(s string, chunkSize int) []string {
var chunks []string
runes := []rune(s)
if len(runes) == 0 {
return []string{s}
}
for i := 0; i < len(runes); i += chunkSize {
nn := i + chunkSize
if nn > len(runes) {
nn = len(runes)
}
chunks = append(chunks, string(runes[i:nn]))
}
return chunks
}
func logCertStats(tlsLinks []TlsLink) {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("Snapshot of your cert chain size %d explained. ", len(tlsLinks)))
for i, link := range tlsLinks {
//we only want to know the below for certs, not CAs
if !link.isCA {
sb.WriteString(fmt.Sprintf("[%d/%d] TLS cert serial #%s, sha1 fingerprint %s for DNS names %s, valid from %s, signed by [%s], expires in %s. ",
i+1,
len(tlsLinks),
formatSerial(link.cert.SerialNumber),
sha1Fingerprint(link.cert),
link.cert.DNSNames,
link.issued.Format("2006-01-02"),
link.cert.Issuer.CommonName,
link.printRemainingValidity(),
))
//is this cert valid longer than 398d? tell the admin
if link.expiryLongerThanLegalBrowserMaximum() {
sb.WriteString(fmt.Sprintf("Total validity period of %d days is above legal browser period of %d days. ",
int(link.totalValidity.AsDays()),
int(Days398.AsDays())))
}
//has browser validity passed?
if link.legalBrowserValidityPeriodPassed() {
sb.WriteString(fmt.Sprintf("Legal browser period already expired %s ago, update this certificate now. ",
link.browserValidity.AsString()))
//if it hasn't warn the user if it's a long-lived cert.
} else if link.expiryLongerThanLegalBrowserMaximum() {
sb.WriteString(fmt.Sprintf("Despite valid certificate, You may experience disruption in %s. ",
link.browserValidity.AsString()))
}
} else {
caType := "Intermediate"
if isRoot(link.cert) {
caType = "Root"
}
sb.WriteString(fmt.Sprintf("[%d/%d] %s CA #%s Common name [%s], signed by [%s], expires in %s. ",
i+1,
len(tlsLinks),
caType,
formatSerial(link.cert.SerialNumber),
link.cert.Subject.CommonName,
link.cert.Issuer.CommonName,
link.remainingValidity.AsString(),
))
}
}
for _, t := range tlsLinks {
if t.earliestExpiry {
var ev *zerolog.Event
//if the certificate shows signs of problems but is valid, log.warn instead
if t.expiresTooCloseForComfort() || t.legalBrowserValidityPeriodPassed() {
ev = log.Warn()
} else {
ev = log.Info()
}
ev.Msg(sb.String())
}
}
}
func parseTlsLinks(chain []*x509.Certificate) []TlsLink {
earliestExpiry := PDuration(math.MaxInt64)
var tlsLinks []TlsLink
si := 0
for i, cert := range chain {
link := TlsLink{
cert: cert,
issued: cert.NotBefore,
remainingValidity: PDuration(time.Until(cert.NotAfter)),
totalValidity: PDuration(cert.NotAfter.Sub(cert.NotBefore)),
browserValidity: PDuration(time.Until(cert.NotBefore.Add(Days398.AsDuration()))),
earliestExpiry: false,
isCA: cert.IsCA,
}
tlsLinks = append(tlsLinks, link)
if link.remainingValidity < earliestExpiry {
si = i
earliestExpiry = link.remainingValidity
}
}
tlsLinks[si].earliestExpiry = true
return tlsLinks
}
func splitCertPools(chain *tls.Certificate) (*x509.CertPool, *x509.CertPool, error) {
var err error
root := x509.NewCertPool()
inter := x509.NewCertPool()
for _, c := range chain.Certificate {
c1, caerr := x509.ParseCertificate(c)
if caerr != nil {
err = caerr
}
//for CA's we treat you as intermediate unless you signed yourself
if c1.IsCA {
//as above, you're intermediate in the last position unless you signed yourself, that makes you a root cert.
if isRoot(c1) {
root.AddCert(c1)
} else {
inter.AddCert(c1)
}
}
}
return inter, root, err
}
func isRoot(c *x509.Certificate) bool {
return c.IsCA && c.Issuer.CommonName == c.Subject.CommonName
}