forked from dmcgowan/quicktls
/
main.go
222 lines (194 loc) · 6.1 KB
/
main.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
package main
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"flag"
"fmt"
"log"
"math/big"
"net"
"os"
"path/filepath"
"time"
)
var (
clientN int
directory string
org string
duration time.Duration
rsaBits int
ec string
)
// Usage: quicktls host1 host2 host3
// Output: ca.pem host1.cert host1.key host2.cert host2.key host3.cert host3.key
// NOTE: CA key is NEVER saved to disk, this ensures safety of the ca
func main() {
flag.IntVar(&clientN, "clients", 0, "Number of client certificates to generate")
flag.StringVar(&directory, "o", "", "Output directory")
flag.StringVar(&org, "org", "QuickTLS", "Organization in the certificate")
flag.DurationVar(&duration, "exp", 1080*24*time.Hour, "Time until Certificate expiration")
flag.IntVar(&rsaBits, "rsa", 2048, "Number of RSA bits")
flag.StringVar(&ec, "ec", "", "Which elliptic curve key to use 224, 384, 521 (default to use RSA)")
flag.Parse()
hosts := flag.Args()
caFile := filepath.Join(directory, "ca.pem")
ca, caKey, err := generateCA(caFile)
if err != nil {
log.Fatal(err)
}
for _, host := range hosts {
hostCert := filepath.Join(directory, fmt.Sprintf("%s.cert", host))
hostKey := filepath.Join(directory, fmt.Sprintf("%s.key", host))
if err := generateCert([]string{host}, hostCert, hostKey, ca, caKey); err != nil {
log.Fatal(err)
}
}
for i := 0; i < clientN; i++ {
clientCert := filepath.Join(directory, fmt.Sprintf("client-%d.cert", i))
clientKey := filepath.Join(directory, fmt.Sprintf("client-%d.key", i))
if err := generateClient(clientCert, clientKey, ca, caKey); err != nil {
log.Fatal(err)
}
}
}
// newCertificate creates a new template
func newCertificate() *x509.Certificate {
notBefore := time.Now()
notAfter := notBefore.Add(duration)
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)
}
return &x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{org},
},
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
BasicConstraintsValid: true,
}
}
// newPrivateKey creates a new private key depending
// on the input flags
func newPrivateKey() (crypto.PrivateKey, error) {
if ec != "" {
var curve elliptic.Curve
switch ec {
case "224":
curve = elliptic.P224()
case "384":
curve = elliptic.P384()
case "521":
curve = elliptic.P521()
default:
return nil, fmt.Errorf("Unknown elliptic curve: %q", ec)
}
return ecdsa.GenerateKey(curve, rand.Reader)
}
return rsa.GenerateKey(rand.Reader, rsaBits)
}
// generateCA creates a new CA certificate, saves the certificate
// and returns the x509 certificate and crypto private key. This
// private key should never be saved to disk, but rather used to
// immediately generate further certificates.
func generateCA(caFile string) (*x509.Certificate, crypto.PrivateKey, error) {
template := newCertificate()
template.IsCA = true
template.KeyUsage |= x509.KeyUsageCertSign
template.Subject.CommonName = org
priv, err := newPrivateKey()
if err != nil {
return nil, nil, err
}
derBytes, err := x509.CreateCertificate(rand.Reader, template, template, priv.(crypto.Signer).Public(), priv)
if err != nil {
return nil, nil, err
}
ca, err := x509.ParseCertificate(derBytes)
if err != nil {
return nil, nil, err
}
certOut, err := os.Create(caFile)
if err != nil {
return nil, nil, err
}
defer certOut.Close()
if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
return nil, nil, err
}
return ca, priv, nil
}
// generateCert generates a new certificate for the given hosts using the
// provided certificate authority. The cert and key files are stored in the
// the provided files.
func generateCert(hosts []string, certFile, keyFile string, ca *x509.Certificate, caKey crypto.PrivateKey) error {
template := newCertificate()
for _, h := range hosts {
if ip := net.ParseIP(h); ip != nil {
template.IPAddresses = append(template.IPAddresses, ip)
} else {
template.DNSNames = append(template.DNSNames, h)
if template.Subject.CommonName == "" {
template.Subject.CommonName = h
}
}
}
priv, err := newPrivateKey()
if err != nil {
return err
}
return generateFromTemplate(certFile, keyFile, template, ca, priv, caKey)
}
// generateClient gnerates a new client certificate. The cert and key files are
// stored in the provided files.
func generateClient(certFile, keyFile string, ca *x509.Certificate, caKey crypto.PrivateKey) error {
template := newCertificate()
template.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}
priv, err := newPrivateKey()
if err != nil {
return err
}
return generateFromTemplate(certFile, keyFile, template, ca, priv, caKey)
}
// generateFromTemplate generates a certificate from the given template and signed by
// the given parent, storing the results in a certificate and key file.
func generateFromTemplate(certFile, keyFile string, template, parent *x509.Certificate, key crypto.PrivateKey, parentKey crypto.PrivateKey) error {
derBytes, err := x509.CreateCertificate(rand.Reader, template, parent, key.(crypto.Signer).Public(), parentKey)
if err != nil {
return err
}
certOut, err := os.Create(certFile)
if err != nil {
return err
}
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
certOut.Close()
keyOut, err := os.OpenFile(keyFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer keyOut.Close()
switch v := key.(type) {
case *rsa.PrivateKey:
keyBytes := x509.MarshalPKCS1PrivateKey(v)
pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: keyBytes})
case *ecdsa.PrivateKey:
keyBytes, err := x509.MarshalECPrivateKey(v)
if err != nil {
return err
}
pem.Encode(keyOut, &pem.Block{Type: "EC PRIVATE KEY", Bytes: keyBytes})
default:
return fmt.Errorf("Unsupport private key type: %#v", key)
}
return nil
}