-
Notifications
You must be signed in to change notification settings - Fork 46
/
tls.go
119 lines (103 loc) · 3.26 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
package cmd
// Derived from https://github.com/Shyp/generate-tls-cert/
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
log "github.com/sirupsen/logrus"
"math/big"
"net"
"strings"
"time"
)
func generateTLSCertificate() {
orgName := params.TLSOrgName
hosts := strings.Split(params.TLSName, ",")
notBefore := time.Now()
notAfter := notBefore.Add(time.Hour * 24 * 365)
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
log.Fatalf("failed to generate tls serial number: %s", err)
}
rootKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
log.Fatalf("failed to generate P256 key: %s", err)
}
rootTemplate := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{orgName},
CommonName: "Root CA",
},
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IsCA: true,
}
derBytes, err := x509.CreateCertificate(rand.Reader, &rootTemplate, &rootTemplate, &rootKey.PublicKey, rootKey)
if err != nil {
log.Fatalf("failed to generate certificate: %s", err)
}
rootCert := derBytes
leafKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
log.Fatalf("failed to generate leaf P256 key: %s", err)
}
serialNumber, err = rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
log.Fatalf("failed to generate serial number: %s", err)
}
leafTemplate := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{orgName},
CommonName: hosts[0],
},
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IsCA: false,
}
for _, h := range hosts {
if ip := net.ParseIP(h); ip != nil {
leafTemplate.IPAddresses = append(leafTemplate.IPAddresses, ip)
} else {
leafTemplate.DNSNames = append(leafTemplate.DNSNames, h)
}
}
derBytes, err = x509.CreateCertificate(rand.Reader, &leafTemplate, &rootTemplate, &leafKey.PublicKey, rootKey)
if err != nil {
log.Fatalf("failed to generate leaf certificate: %s", err)
}
leafCert := derBytes
_ = rootCert
params.TLSCertData = certToPEM(leafCert)
params.TLSKeyData = keyToPEM(leafKey)
}
func keyToPEM(key *ecdsa.PrivateKey) string {
var pemBytes bytes.Buffer
b, err := x509.MarshalECPrivateKey(key)
if err != nil {
log.Fatalf("failed to marshal ECDSA private key: %v", err)
}
if err := pem.Encode(&pemBytes, &pem.Block{Type: "EC PRIVATE KEY", Bytes: b}); err != nil {
log.Fatalf("failed to PEM encode ECDSA private key: %s", err)
}
return string(pemBytes.Bytes())
}
func certToPEM(derBytes []byte) string {
var pemBytes bytes.Buffer
if err := pem.Encode(&pemBytes, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
log.Fatalf("failed to PEM encode certificate: %s", err)
}
return string(pemBytes.Bytes())
}