-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
supplier_selfsigned.go
70 lines (58 loc) · 1.78 KB
/
supplier_selfsigned.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
package certificate
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"time"
"github.com/go-acme/lego/v4/certcrypto"
)
type SelfSignedSupplier struct {
}
func NewSelfSignedSupplier() *SelfSignedSupplier {
return &SelfSignedSupplier{}
}
func (s *SelfSignedSupplier) GetCertificate(subject string, altNames []string) (*Details, error) {
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, err
}
template := x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{
Organization: []string{"Centauri"},
CommonName: subject,
},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(time.Hour * 24 * 30),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
DNSNames: append([]string{subject}, altNames...),
BasicConstraintsValid: true,
}
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, key.Public(), key)
if err != nil {
return nil, err
}
return &Details{
PrivateKey: string(certcrypto.PEMEncode(key)),
Certificate: string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes})),
Subject: subject,
AltNames: altNames,
NotAfter: template.NotAfter,
NextOcspUpdate: time.Now().Add(time.Hour * 24 * 30), // As the cert expires
}, nil
}
func (s *SelfSignedSupplier) UpdateStaple(_ *Details) error {
// Shouldn't be called - self-signed certs aren't stapled
return nil
}
func (s *SelfSignedSupplier) MinCertificateValidity() time.Duration {
return time.Hour * 24 * 7
}
func (s *SelfSignedSupplier) MinStapleValidity() time.Duration {
return time.Second
}