forked from Shyp/generate-tls-cert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.go
245 lines (213 loc) · 6.44 KB
/
generate.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
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
// generate-tls-cert generates root, leaf, and client TLS certificates.
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"flag"
"fmt"
"log"
"math/big"
"net"
"os"
"os/exec"
"strings"
"time"
)
var (
host = flag.String("host", "", "Comma-separated hostnames and IPs to generate a certificate for")
validFrom = flag.String("start-date", "", "Creation date formatted as Jan 1 15:04:05 2011 (default now)")
validFor = flag.Duration("duration", 365*24*time.Hour, "Duration that certificate is valid for")
version = flag.Bool("version", false, "Print the version string")
)
const Version = "0.1"
func main() {
flag.Parse()
if *version {
fmt.Fprintf(os.Stderr, "generate-tls-cert version %s\n", Version)
os.Exit(2)
}
if len(*host) == 0 {
log.Fatalf("Missing required --host parameter")
}
var err error
var notBefore time.Time
if len(*validFrom) == 0 {
notBefore = time.Now()
} else {
notBefore, err = time.Parse("Jan 2 15:04:05 2006", *validFrom)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse creation date: %s\n", err)
os.Exit(1)
}
}
notAfter := notBefore.Add(*validFor)
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
log.Fatalf("failed to generate serial number: %s", err)
}
rootKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
panic(err)
}
keyToFile("root.key", rootKey)
rootTemplate := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{"Acme Co"},
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 {
panic(err)
}
debugCertToFile("root.debug.crt", derBytes)
certToFile("root.pem", derBytes)
leafKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
panic(err)
}
keyToFile("leaf.key", leafKey)
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{"Acme Co"},
CommonName: "test_cert_1",
},
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IsCA: false,
}
hosts := strings.Split(*host, ",")
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 {
panic(err)
}
debugCertToFile("leaf.debug.crt", derBytes)
certToFile("leaf.pem", derBytes)
clientKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
panic(err)
}
keyToFile("client.key", clientKey)
clientTemplate := x509.Certificate{
SerialNumber: new(big.Int).SetInt64(4),
Subject: pkix.Name{
Organization: []string{"Acme Co"},
CommonName: "client_auth_test_cert",
},
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
BasicConstraintsValid: true,
IsCA: false,
}
derBytes, err = x509.CreateCertificate(rand.Reader, &clientTemplate, &rootTemplate, &clientKey.PublicKey, rootKey)
if err != nil {
panic(err)
}
debugCertToFile("client.debug.crt", derBytes)
certToFile("client.pem", derBytes)
fmt.Fprintf(os.Stdout, `Successfully generated certificates! Here's what you generated.
# Root CA
root.key
The private key for the root Certificate Authority. Keep this private.
root.pem
The public key for the root Certificate Authority. Clients should load the
certificate in this file to connect to the server.
root.debug.crt
Debug information about the generated certificate.
# Leaf Certificate - Use these to serve TLS traffic.
leaf.key
Private key (PEM-encoded) for terminating TLS traffic on the server.
leaf.pem
Public key for terminating TLS traffic on the server.
leaf.debug.crt
Debug information about the generated certificate
# Client Certificate - You probably don't need these.
client.key: Secret key for TLS client authentication
client.pem: Public key for TLS client authentication
See https://github.com/Shyp/generate-tls-cert for examples of how to use in code.
`)
}
// keyToFile writes a PEM serialization of |key| to a new file called
// |filename|.
func keyToFile(filename string, key *ecdsa.PrivateKey) {
file, err := os.Create(filename)
if err != nil {
panic(err)
}
defer file.Close()
b, err := x509.MarshalECPrivateKey(key)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to marshal ECDSA private key: %v", err)
os.Exit(2)
}
if err := pem.Encode(file, &pem.Block{Type: "EC PRIVATE KEY", Bytes: b}); err != nil {
panic(err)
}
}
func certToFile(filename string, derBytes []byte) {
certOut, err := os.Create(filename)
if err != nil {
log.Fatalf("failed to open cert.pem for writing: %s", err)
}
if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
log.Fatalf("failed to write data to cert.pem: %s", err)
}
if err := certOut.Close(); err != nil {
log.Fatalf("error closing cert.pem: %s", err)
}
}
// debugCertToFile writes a PEM serialization and OpenSSL debugging dump of
// |derBytes| to a new file called |filename|.
func debugCertToFile(filename string, derBytes []byte) {
cmd := exec.Command("openssl", "x509", "-text", "-inform", "DER")
file, err := os.Create(filename)
if err != nil {
panic(err)
}
defer file.Close()
cmd.Stdout = file
cmd.Stderr = os.Stderr
stdin, err := cmd.StdinPipe()
if err != nil {
panic(err)
}
if err := cmd.Start(); err != nil {
panic(err)
}
if _, err := stdin.Write(derBytes); err != nil {
panic(err)
}
stdin.Close()
if err := cmd.Wait(); err != nil {
panic(err)
}
}