-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcerty.go
More file actions
627 lines (527 loc) · 17.6 KB
/
Copy pathcerty.go
File metadata and controls
627 lines (527 loc) · 17.6 KB
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
package certy
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
"golang.org/x/crypto/acme"
)
const (
letsencryptStagingURL = "https://acme-staging-v02.api.letsencrypt.org/directory"
letsencryptProdURL = "https://acme-v02.api.letsencrypt.org/directory"
// File permissions
dirPerm = 0755
certPerm = 0644
keyPerm = 0600
// Certificate renewal threshold (days before expiry)
renewalThreshold = 30
)
// rsaPrivateKeyJSON is a JSON-serializable wrapper for rsa.PrivateKey.
// It stores the key as PEM-encoded PKCS#8 data to ensure proper round-trip serialization.
type rsaPrivateKeyJSON struct {
*rsa.PrivateKey
}
func (k rsaPrivateKeyJSON) MarshalJSON() ([]byte, error) {
if k.PrivateKey == nil {
return []byte("null"), nil
}
der, err := x509.MarshalPKCS8PrivateKey(k.PrivateKey)
if err != nil {
return nil, fmt.Errorf("failed to marshal RSA private key: %w", err)
}
block := &pem.Block{Type: "PRIVATE KEY", Bytes: der}
pemData := pem.EncodeToMemory(block)
return json.Marshal(string(pemData))
}
func (k *rsaPrivateKeyJSON) UnmarshalJSON(data []byte) error {
var pemStr string
if err := json.Unmarshal(data, &pemStr); err != nil {
return err
}
if pemStr == "" {
k.PrivateKey = nil
return nil
}
block, _ := pem.Decode([]byte(pemStr))
if block == nil {
return errors.New("failed to decode PEM block for account key")
}
key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return fmt.Errorf("failed to parse account key: %w", err)
}
rsaKey, ok := key.(*rsa.PrivateKey)
if !ok {
return errors.New("account key is not RSA")
}
k.PrivateKey = rsaKey
return nil
}
// DomainAcme is a struct for domain acme data
type DomainAcme struct {
Sans []string `json:"sans"`
IssuerData IssuerData `json:"issuer_data"`
AccountKey rsaPrivateKeyJSON `json:"account_key"`
CertFile string `json:"cert_file"`
KeyFile string `json:"key_file"`
ExpireDate time.Time `json:"expire_date"`
IssueDate time.Time `json:"issue_date"`
CustomCert bool `json:"custom_cert"`
}
// RenewRequired checks if certificate needs renewal
func (d *DomainAcme) RenewRequired() bool {
if d.ExpireDate.IsZero() {
return true
}
// Renew 30 days before expiry
return time.Now().After(d.ExpireDate.AddDate(0, 0, -renewalThreshold))
}
func (d *DomainAcme) IsNull() bool {
return d.IssuerData.Ca == "" && d.IssuerData.URL == "" &&
d.IssuerData.ChallengeToken == "" && d.AccountKey.PrivateKey == nil &&
d.IssueDate.IsZero()
}
// Expired checks if certificate is expired
func (d *DomainAcme) Expired() bool {
if d.ExpireDate.IsZero() {
return true
}
return time.Now().After(d.ExpireDate)
}
// IssuerData is a struct for issuer data
type IssuerData struct {
URL string `json:"url"`
Ca string `json:"ca"`
ChallengeToken string `json:"challenge_token"`
}
// Logger is an optional interface for logging. If nil, no logs are emitted.
type Logger interface {
Printf(format string, v ...interface{})
}
// Manager is a struct for managing certificates
type Manager struct {
Email string
Location string
Staging bool
Logger Logger
mu sync.RWMutex
issuings map[string]bool
certCache sync.Map // domain -> *tls.Certificate
}
// NewManager creates a new certificate manager
func NewManager(email, location string, staging bool) *Manager {
return &Manager{
Email: email,
Location: location,
Staging: staging,
issuings: make(map[string]bool),
}
}
// logf logs a message if a Logger is configured.
func (m *Manager) logf(format string, v ...interface{}) {
if m.Logger != nil {
m.Logger.Printf(format, v...)
}
}
// directoryURL returns the ACME directory URL based on the Staging flag.
func (m *Manager) directoryURL() string {
if m.Staging {
return letsencryptStagingURL
}
return letsencryptProdURL
}
// validateDomain checks that the domain name is safe to use as a path component.
func validateDomain(domain string) error {
if domain == "" {
return errors.New("empty domain name")
}
if strings.Contains(domain, "..") || strings.ContainsAny(domain, "/\\\x00") {
return fmt.Errorf("invalid domain name: %q", domain)
}
if len(domain) > 253 {
return fmt.Errorf("domain name too long: %d characters", len(domain))
}
return nil
}
// IssueCert issues a Let's Encrypt certificate for the domain
func (m *Manager) IssueCert(ctx context.Context, domain string) error {
if err := validateDomain(domain); err != nil {
return err
}
return m.issueLetsEncryptCert(ctx, m.Email, domain, m.Location)
}
// GetChallengeToken gets the challenge token for a domain
func (m *Manager) GetChallengeToken(domain string) (string, error) {
if err := validateDomain(domain); err != nil {
return "", err
}
location := filepath.Join(m.Location, domain, fmt.Sprintf("%s-acme.json", domain))
file, err := os.ReadFile(location)
if err != nil {
return "", fmt.Errorf("failed to read domain acme file: %w", err)
}
var domainAcme DomainAcme
if err := json.Unmarshal(file, &domainAcme); err != nil {
return "", fmt.Errorf("failed to unmarshal domain acme data: %w", err)
}
return domainAcme.IssuerData.ChallengeToken, nil
}
// GetCert gets TLS certificate for a domain. Certificates are cached in memory
// and only reloaded from disk on cache miss.
func (m *Manager) GetCert(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
domain := hello.ServerName
if err := validateDomain(domain); err != nil {
return nil, err
}
// Check in-memory cache first
if cached, ok := m.certCache.Load(domain); ok {
return cached.(*tls.Certificate), nil
}
location := filepath.Join(m.Location, domain, fmt.Sprintf("%s-acme.json", domain))
certFile := filepath.Join(m.Location, domain, fmt.Sprintf("%s-cert.crt", domain))
keyFile := filepath.Join(m.Location, domain, fmt.Sprintf("%s-key.pem", domain))
if _, err := os.Stat(location); os.IsNotExist(err) {
return nil, fmt.Errorf("domain acme data not found for %s", domain)
}
certFileData, err := os.ReadFile(certFile)
if err != nil {
return nil, fmt.Errorf("failed to read certificate file: %w", err)
}
keyFileData, err := os.ReadFile(keyFile)
if err != nil {
return nil, fmt.Errorf("failed to read key file: %w", err)
}
cert, err := tls.X509KeyPair(certFileData, keyFileData)
if err != nil {
return nil, fmt.Errorf("failed to get x509 key pair: %w", err)
}
// Store in cache
m.certCache.Store(domain, &cert)
return &cert, nil
}
// InvalidateCertCache removes a domain's certificate from the in-memory cache,
// forcing the next GetCert call to reload from disk.
func (m *Manager) InvalidateCertCache(domain string) {
m.certCache.Delete(domain)
}
// GetAcmeFileData gets acme file data for a domain
func (m *Manager) GetAcmeFileData(domain string) (*DomainAcme, error) {
if err := validateDomain(domain); err != nil {
return nil, err
}
location := filepath.Join(m.Location, domain, fmt.Sprintf("%s-acme.json", domain))
file, err := os.ReadFile(location)
if err != nil {
return nil, fmt.Errorf("failed to read acme file: %w", err)
}
var domainAcme DomainAcme
if err := json.Unmarshal(file, &domainAcme); err != nil {
return nil, fmt.Errorf("failed to unmarshal domain acme data: %w", err)
}
return &domainAcme, nil
}
// HTTPHandler is a http handler for serving acme challenge
func (m *Manager) HTTPHandler(fallback http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check if request path has acme challenge
if !strings.HasPrefix(r.URL.Path, "/.well-known/acme-challenge/") {
fallback.ServeHTTP(w, r)
return
}
token := r.URL.Path[len("/.well-known/acme-challenge/"):]
if token == "" {
http.Error(w, "Empty challenge token", http.StatusBadRequest)
return
}
acmeData, err := m.GetAcmeFileData(r.Host)
if err != nil {
m.logf("Failed to get acme file data for %s: %v", r.Host, err)
http.Error(w, "Domain not found", http.StatusNotFound)
return
}
if acmeData.AccountKey.PrivateKey == nil {
http.Error(w, "Invalid account key", http.StatusInternalServerError)
return
}
client := &acme.Client{
DirectoryURL: m.directoryURL(),
Key: acmeData.AccountKey.PrivateKey,
}
// HTTP-01 challenge response
http01, err := client.HTTP01ChallengeResponse(token)
if err != nil {
m.logf("HTTP-01 challenge response failed for %s: %v", r.Host, err)
http.Error(w, "Challenge failed", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(http01))
})
}
// isIssuing checks if certificate issuance is already in progress
func (m *Manager) isIssuing(domain string) bool {
m.mu.RLock()
defer m.mu.RUnlock()
return m.issuings[domain]
}
// setIssuing sets the issuing status for a domain
func (m *Manager) setIssuing(domain string, status bool) {
m.mu.Lock()
defer m.mu.Unlock()
m.issuings[domain] = status
}
// issueLetsEncryptCert issues a Let's Encrypt certificate
func (m *Manager) issueLetsEncryptCert(ctx context.Context, email, domain, location string) error {
if m == nil {
return errors.New("manager is nil")
}
// Check if already issuing
if m.isIssuing(domain) {
return fmt.Errorf("issuing already in progress for domain: %s", domain)
}
m.setIssuing(domain, true)
defer m.setIssuing(domain, false)
// Create base location directory
if err := os.MkdirAll(location, dirPerm); err != nil {
return fmt.Errorf("failed to create base directory: %w", err)
}
// Create domain directory
domainLocation := filepath.Join(location, domain)
if err := os.MkdirAll(domainLocation, dirPerm); err != nil {
return fmt.Errorf("failed to create domain directory: %w", err)
}
// Check existing certificate
domainAcmeFile := filepath.Join(domainLocation, fmt.Sprintf("%s-acme.json", domain))
var domainAcme DomainAcme
if _, err := os.Stat(domainAcmeFile); err == nil {
// File exists, read it
acmefile, err := os.ReadFile(domainAcmeFile)
if err != nil {
return fmt.Errorf("failed to read domain acme file: %w", err)
}
if len(acmefile) > 0 {
if err := json.Unmarshal(acmefile, &domainAcme); err != nil {
return fmt.Errorf("failed to unmarshal domain acme data: %w", err)
}
}
}
// Check if renewal is needed
if !domainAcme.RenewRequired() && !domainAcme.Expired() {
return nil // Certificate is still valid
}
// Generate a new account key
accountKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return fmt.Errorf("account key generation failed: %w", err)
}
client := &acme.Client{
DirectoryURL: m.directoryURL(),
Key: accountKey,
}
// Register a new ACME account
acct := &acme.Account{Contact: []string{"mailto:" + email}}
acct, err = client.Register(ctx, acct, acme.AcceptTOS)
if err != nil {
return fmt.Errorf("account registration failed: %w", err)
}
if domainAcme.IsNull() {
domainAcme = DomainAcme{
Sans: []string{domain},
IssuerData: IssuerData{
URL: acct.URI,
Ca: client.DirectoryURL,
},
AccountKey: rsaPrivateKeyJSON{accountKey},
}
} else {
domainAcme.AccountKey = rsaPrivateKeyJSON{accountKey}
}
// Save domainAcme struct
if err := m.saveDomainAcme(domainAcmeFile, domainAcme); err != nil {
return fmt.Errorf("failed to save domain acme data: %w", err)
}
// Create a new order for the domain
order, err := client.AuthorizeOrder(ctx, acme.DomainIDs(domain))
if err != nil {
return fmt.Errorf("order authorization failed: %w", err)
}
// Find HTTP-01 challenge
chal, err := m.findHTTP01Challenge(ctx, client, order)
if err != nil {
return fmt.Errorf("failed to find HTTP-01 challenge: %w", err)
}
// Update challenge token
domainAcme.IssuerData.ChallengeToken = chal.Token
if err := m.saveDomainAcme(domainAcmeFile, domainAcme); err != nil {
return fmt.Errorf("failed to save challenge token: %w", err)
}
// Accept the challenge
_, err = client.Accept(ctx, chal)
if err != nil {
return fmt.Errorf("challenge acceptance failed: %w", err)
}
// Wait for authorization to be valid
_, err = client.WaitAuthorization(ctx, chal.URI)
if err != nil {
return fmt.Errorf("challenge validation failed: %w", err)
}
// Generate ECDSA key pair
ecdsaPrivateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return fmt.Errorf("ECDSA private key generation failed: %w", err)
}
// Create CSR
csr, err := x509.CreateCertificateRequest(rand.Reader, &x509.CertificateRequest{
Subject: pkix.Name{CommonName: domain},
}, ecdsaPrivateKey)
if err != nil {
return fmt.Errorf("certificate request creation failed: %w", err)
}
// Finalize the order and get the certificate
der, _, err := client.CreateOrderCert(ctx, order.FinalizeURL, csr, true)
if err != nil {
return fmt.Errorf("certificate issuance failed: %w", err)
}
// Save certificate and key files
certFile := filepath.Join(domainLocation, fmt.Sprintf("%s-cert.crt", domain))
keyFile := filepath.Join(domainLocation, fmt.Sprintf("%s-key.pem", domain))
if err := m.saveCertificateChain(certFile, der); err != nil {
return fmt.Errorf("failed to save certificate: %w", err)
}
if err := m.savePrivateKey(keyFile, ecdsaPrivateKey); err != nil {
return fmt.Errorf("failed to save private key: %w", err)
}
// Parse real expiry from the leaf certificate
leafCert, err := x509.ParseCertificate(der[0])
if err != nil {
return fmt.Errorf("failed to parse issued certificate: %w", err)
}
// Update domainAcme with new certificate info
domainAcme.CertFile = certFile
domainAcme.KeyFile = keyFile
domainAcme.ExpireDate = leafCert.NotAfter
domainAcme.IssueDate = leafCert.NotBefore
// Save final domainAcme struct
if err := m.saveDomainAcme(domainAcmeFile, domainAcme); err != nil {
return fmt.Errorf("failed to save final domain acme data: %w", err)
}
// Invalidate cached cert so the new one is loaded on next TLS handshake
m.InvalidateCertCache(domain)
m.logf("Certificate and key saved to %s", domainLocation)
return nil
}
// findHTTP01Challenge finds HTTP-01 challenge in the order
func (m *Manager) findHTTP01Challenge(ctx context.Context, client *acme.Client, order *acme.Order) (*acme.Challenge, error) {
for _, authzURL := range order.AuthzURLs {
authz, err := client.GetAuthorization(ctx, authzURL)
if err != nil {
continue
}
for _, c := range authz.Challenges {
if c.Type == "http-01" {
return c, nil
}
}
}
return nil, errors.New("no HTTP-01 challenge found")
}
// saveDomainAcme saves domain acme data to file
func (m *Manager) saveDomainAcme(filename string, data DomainAcme) error {
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal domain acme data: %w", err)
}
if err := os.WriteFile(filename, jsonData, certPerm); err != nil {
return fmt.Errorf("failed to write domain acme data: %w", err)
}
return nil
}
// saveCertificateChain saves certificate chain to file
func (m *Manager) saveCertificateChain(filename string, der [][]byte) error {
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, certPerm)
if err != nil {
return fmt.Errorf("failed to create certificate file: %w", err)
}
defer file.Close()
for _, cert := range der {
block := &pem.Block{Type: "CERTIFICATE", Bytes: cert}
if err := pem.Encode(file, block); err != nil {
return fmt.Errorf("failed to write certificate: %w", err)
}
}
return nil
}
// savePrivateKey saves private key to file
func (m *Manager) savePrivateKey(filename string, key *ecdsa.PrivateKey) error {
keyBytes, err := x509.MarshalECPrivateKey(key)
if err != nil {
return fmt.Errorf("failed to marshal ECDSA private key: %w", err)
}
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyBytes})
if err := os.WriteFile(filename, keyPEM, keyPerm); err != nil {
return fmt.Errorf("failed to write key: %w", err)
}
return nil
}
// AddCustomCert adds a custom certificate for a domain
func (m *Manager) AddCustomCert(domain, certPEM, keyPEM string) error {
if err := validateDomain(domain); err != nil {
return err
}
domainLocation := filepath.Join(m.Location, domain)
// Create domain directory
if err := os.MkdirAll(domainLocation, dirPerm); err != nil {
return fmt.Errorf("failed to create domain directory: %w", err)
}
// Parse the certificate to extract the real expiry date
block, _ := pem.Decode([]byte(certPEM))
if block == nil {
return errors.New("failed to decode certificate PEM")
}
parsedCert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return fmt.Errorf("failed to parse certificate: %w", err)
}
// Save certificate and key files
certFile := filepath.Join(domainLocation, fmt.Sprintf("%s-cert.crt", domain))
keyFile := filepath.Join(domainLocation, fmt.Sprintf("%s-key.pem", domain))
if err := os.WriteFile(certFile, []byte(certPEM), certPerm); err != nil {
return fmt.Errorf("failed to write certificate file: %w", err)
}
if err := os.WriteFile(keyFile, []byte(keyPEM), keyPerm); err != nil {
return fmt.Errorf("failed to write key file: %w", err)
}
// Create acme file location
acmeLocation := filepath.Join(domainLocation, fmt.Sprintf("%s-acme.json", domain))
// Create domainAcme struct — store file paths, not PEM content
domainAcme := DomainAcme{
Sans: []string{domain},
IssuerData: IssuerData{},
CertFile: certFile,
KeyFile: keyFile,
CustomCert: true,
ExpireDate: parsedCert.NotAfter,
IssueDate: parsedCert.NotBefore,
}
// Save acme data
if err := m.saveDomainAcme(acmeLocation, domainAcme); err != nil {
return fmt.Errorf("failed to save domain acme data: %w", err)
}
// Invalidate cached cert so the new one is loaded on next TLS handshake
m.InvalidateCertCache(domain)
return nil
}