-
Notifications
You must be signed in to change notification settings - Fork 0
/
cert.go
140 lines (127 loc) · 2.94 KB
/
cert.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
package main
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"os"
"time"
)
type certGenerator struct {
caCert *x509.Certificate
caKey crypto.PrivateKey
}
func newCertGenerator(publicKeyFile, privateKeyFile string) (*certGenerator, error) {
tlsCert, err := tls.LoadX509KeyPair(publicKeyFile, privateKeyFile)
if err != nil {
return nil, err
}
caCert, err := x509.ParseCertificate(tlsCert.Certificate[0])
if err != nil {
return nil, err
}
return &certGenerator{
caCert: caCert,
caKey: tlsCert.PrivateKey,
}, nil
}
func (cg *certGenerator) Get(hostname string) (*tls.Config, error) {
serial, err := getRandomSerialNumber()
if err != nil {
return nil, err
}
hostCert := &x509.Certificate{
SerialNumber: serial,
ExtKeyUsage: []x509.ExtKeyUsage{
x509.ExtKeyUsageServerAuth,
},
BasicConstraintsValid: true,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
DNSNames: []string{hostname},
NotBefore: time.Now().Add(-time.Second * 300),
NotAfter: time.Now().Add(time.Hour * 24 * 30),
Subject: pkix.Name{
CommonName: hostname,
},
}
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, err
}
certDER, err := x509.CreateCertificate(rand.Reader, hostCert, cg.caCert, key.Public(), cg.caKey)
if err != nil {
return nil, err
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{
{
Certificate: [][]byte{
certDER,
},
PrivateKey: key,
},
},
NextProtos: []string{
"http/1.1",
"h2",
},
}
return tlsConfig, nil
}
func createCA(caCertFile, caKeyFile string) error {
serial, err := getRandomSerialNumber()
if err != nil {
return err
}
caCert := &x509.Certificate{
SerialNumber: serial,
BasicConstraintsValid: true,
IsCA: true,
KeyUsage: x509.KeyUsageKeyEncipherment |
x509.KeyUsageDigitalSignature |
x509.KeyUsageCertSign |
x509.KeyUsageCRLSign,
NotBefore: time.Now(),
NotAfter: time.Now().Add(time.Hour * 24 * 365 * 10),
Subject: pkix.Name{
CommonName: "Proxy CA",
},
}
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return err
}
caCertDER, err := x509.CreateCertificate(rand.Reader, caCert, caCert, key.Public(), key)
if err != nil {
return err
}
caCertPEM := &pem.Block{
Type: "CERTIFICATE",
Bytes: caCertDER,
}
err = os.WriteFile(caCertFile, pem.EncodeToMemory(caCertPEM), 0640)
if err != nil {
return err
}
caKeyDER, err := x509.MarshalPKCS8PrivateKey(key)
if err != nil {
return err
}
caKeyPEM := &pem.Block{
Type: "PRIVATE KEY",
Bytes: caKeyDER,
}
err = os.WriteFile(caKeyFile, pem.EncodeToMemory(caKeyPEM), 0600)
if err != nil {
return err
}
return nil
}
func getRandomSerialNumber() (*big.Int, error) {
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
return rand.Int(rand.Reader, serialNumberLimit)
}