Newer
Older
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
package acmeserver
import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"fmt"
"io"
"log"
"path/filepath"
"time"
"git.autistici.org/ai3/go-common/clientutil"
"golang.org/x/crypto/acme"
)
type certInfo struct {
domains []string
retryDeadline time.Time
}
type certStorage interface {
GetCert(string) ([][]byte, crypto.Signer, error)
PutCert(string, [][]byte, crypto.Signer) error
}
type CertGenerator interface {
GetCertificate(context.Context, crypto.Signer, []string) ([][]byte, *x509.Certificate, error)
}
// Manager periodically renews certificates before they expire, and
// responds to http-01 validation requests.
type Manager struct {
//email string
//accountKeyPath string
configDir string
useRSA bool
storage certStorage
certs []*certInfo
certGen CertGenerator
configCh chan [][]string
stopCh chan bool
doneCh chan bool
}
// NewManager creates a new Manager with the given configuration.
func NewManager(config *Config, certGen CertGenerator) (*Manager, error) {
// Validate the configuration.
if config.Dir == "" {
return nil, errors.New("configuration parameter 'cert_dir' is unset")
}
m := &Manager{
useRSA: config.UseRSA,
configDir: filepath.Join(config.Dir, "config"),
stopCh: make(chan bool),
doneCh: make(chan bool),
configCh: make(chan [][]string, 1),
certGen: certGen,
}
ds := &dirStorage{root: filepath.Join(config.Dir, "certs")}
if config.ReplDS == nil {
m.storage = ds
} else {
be, err := clientutil.NewBackend(config.ReplDS)
if err != nil {
return nil, err
}
m.storage = &replStorage{
dirStorage: ds,
replClient: be,
}
}
return m, nil
}
// Start the renewal processes. Canceling the provided context will
// cause background processing to stop.
func (m *Manager) Start(ctx context.Context) error {
domains, err := readCertConfigsFromDir(m.configDir)
if err != nil {
return err
}
m.configCh <- domains
go func() {
m.loop(ctx)
close(m.doneCh)
}()
return nil
}
// Stop any pending operation and release all resources.
func (m *Manager) Stop() {
close(m.stopCh)
<-m.doneCh
}
// Reload configuration.
func (m *Manager) Reload() {
domains, err := readCertConfigsFromDir(m.configDir)
if err != nil {
log.Printf("error reading config: %v", err)
return
}
m.configCh <- domains
log.Printf("configuration reloaded")
}
var (
renewalTimeout = 10 * time.Minute
errorRetryTimeout = 10 * time.Minute
)
func (m *Manager) updateAllCerts(ctx context.Context) {
for _, certInfo := range m.certs {
if certInfo.retryDeadline.Before(time.Now()) {
continue
}
uctx, cancel := context.WithTimeout(ctx, renewalTimeout)
err := m.updateCert(uctx, certInfo)
cancel()
if err != nil {
log.Printf("error updating %s: %v", certInfo.domains[0], err)
certInfo.retryDeadline = time.Now().Add(errorRetryTimeout)
}
}
}
func (m *Manager) updateCert(ctx context.Context, certInfo *certInfo) error {
// Create a new private key.
var (
err error
key crypto.Signer
)
if m.useRSA {
key, err = rsa.GenerateKey(rand.Reader, 2048)
} else {
key, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
}
if err != nil {
return err
}
der, leaf, err := m.certGen.GetCertificate(ctx, key, certInfo.domains)
if err != nil {
return err
}
if err := m.storage.PutCert(certInfo.domains[0], der, key); err != nil {
return err
}
certInfo.retryDeadline = renewalDeadline(leaf)
return nil
}
// Replace the current configuration.
func (m *Manager) loadConfig(certDomains [][]string) {
var certs []*certInfo
for _, domains := range certDomains {
cn := domains[0]
certInfo := &certInfo{
domains: domains,
}
pub, priv, err := m.storage.GetCert(cn)
if err != nil {
log.Printf("cert for %s is missing", cn)
} else {
// By calling validCert we catch things like subjectAltName changes.
leaf, err := validCert(domains, pub, priv)
if err == nil {
log.Printf("cert for %s loaded from storage", cn)
certInfo.retryDeadline = renewalDeadline(leaf)
} else {
log.Printf("cert for %s loaded from storage but parameters have changed", cn)
}
}
certs = append(certs, certInfo)
}
m.certs = certs
}
func (m *Manager) loop(ctx context.Context) {
tick := time.NewTicker(5 * time.Minute)
for {
select {
case <-tick.C:
m.updateAllCerts(ctx)
case certDomains := <-m.configCh:
m.loadConfig(certDomains)
case <-m.stopCh:
return
case <-ctx.Done():
return
}
}
}
var renewalDays = 15
func renewalDeadline(cert *x509.Certificate) time.Time {
return cert.NotAfter.AddDate(0, 0, -renewalDays)
}
func concatDER(der [][]byte) []byte {
// Append DERs to a single []byte buffer and parse the results.
var n int
for _, b := range der {
n += len(b)
}
out := make([]byte, n)
n = 0
for _, b := range der {
n += copy(out[n:], b)
}
return out
}
func validCert(domains []string, der [][]byte, key crypto.Signer) (leaf *x509.Certificate, err error) {
x509Cert, err := x509.ParseCertificates(concatDER(der))
if err != nil {
return nil, err
}
if len(x509Cert) == 0 {
return nil, errors.New("no public key found")
}
leaf = x509Cert[0]
// verify the leaf is not expired and matches the given domains
now := time.Now()
if now.Before(leaf.NotBefore) {
return nil, errors.New("certificate isn't valid yet")
}
if now.After(leaf.NotAfter) {
return nil, errors.New("certificate expired")
}
for _, domain := range domains {
if err := leaf.VerifyHostname(domain); err != nil {
return nil, fmt.Errorf("certificate does not match domain %q", domain)
}
}
// ensure the leaf corresponds to the private key
switch pub := leaf.PublicKey.(type) {
case *rsa.PublicKey:
prv, ok := key.(*rsa.PrivateKey)
if !ok {
return nil, errors.New("private key type does not match public key type")
}
if pub.N.Cmp(prv.N) != 0 {
return nil, errors.New("private key does not match public key")
}
case *ecdsa.PublicKey:
prv, ok := key.(*ecdsa.PrivateKey)
if !ok {
return nil, errors.New("private key type does not match public key type")
}
if pub.X.Cmp(prv.X) != 0 || pub.Y.Cmp(prv.Y) != 0 {
return nil, errors.New("private key does not match public key")
}
default:
return nil, errors.New("unsupported public key algorithm")
}
return leaf, nil
}
func certRequest(key crypto.Signer, domains []string) ([]byte, error) {
req := &x509.CertificateRequest{
Subject: pkix.Name{CommonName: domains[0]},
}
if len(domains) > 1 {
req.DNSNames = domains[1:]
}
return x509.CreateCertificateRequest(rand.Reader, req, key)
}
func parsePrivateKey(der []byte) (crypto.Signer, error) {
if key, err := x509.ParsePKCS1PrivateKey(der); err == nil {
return key, nil
}
if key, err := x509.ParsePKCS8PrivateKey(der); err == nil {
switch key := key.(type) {
case *rsa.PrivateKey:
return key, nil
case *ecdsa.PrivateKey:
return key, nil
default:
return nil, errors.New("unknown private key type in PKCS#8 wrapping")
}
}
if key, err := x509.ParseECPrivateKey(der); err == nil {
return key, nil
}
return nil, errors.New("failed to parse private key")
}
func encodeECDSAKey(w io.Writer, key *ecdsa.PrivateKey) error {
b, err := x509.MarshalECPrivateKey(key)
if err != nil {
return err
}
pb := &pem.Block{Type: "EC PRIVATE KEY", Bytes: b}
return pem.Encode(w, pb)
}
func pickChallenge(typ string, chal []*acme.Challenge) *acme.Challenge {
for _, c := range chal {
if c.Type == typ {
return c
}
}
return nil
}