-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathselfsigned.go
More file actions
289 lines (233 loc) · 7.45 KB
/
Copy pathselfsigned.go
File metadata and controls
289 lines (233 loc) · 7.45 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package selfsigned
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"net"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
)
type Cert struct {
// Path to the SSL certificates.
CACertPath, CertPath string
// Path to the private keys for the SSL certificates.
CAKeyPath, KeyPath string
}
type CertOptions struct {
// DNSNames for cert
DNSNames []string
// IP Addresses for cert
IPAddrs []net.IP
// SkipDefaultIPAddrs indicates if the default IPAddrs should be skipped if IPAddrs is not set.
SkipDefaultIPAddrs bool
// NotBefore for certificate
NotBefore time.Time
// NotAfter for certificate
NotAfter time.Time
// CombinedFile indicates if the certificate and key should be combined into a single file
CombinedFile bool
// CAOrganization sets the CA certificate's Subject.Organization field
CAOrganization string
// CACommonName sets the CA certificate's Subject.CommonName field
CACommonName string
// ExtKeyUsage replaces the leaf certificate's extended key usages. It is
// only consulted when ExtKeyUsageSet is true, so that an empty value can
// omit the extension entirely rather than mean "unset".
ExtKeyUsage []x509.ExtKeyUsage
// ExtKeyUsageSet indicates ExtKeyUsage was configured with WithExtKeyUsage.
ExtKeyUsageSet bool
}
type CertOpt func(*CertOptions)
func WithDNSName(dnsName string) CertOpt {
return func(o *CertOptions) {
o.DNSNames = append(o.DNSNames, dnsName)
}
}
func WithIPAddr(ipAddr net.IP) CertOpt {
return func(o *CertOptions) {
o.IPAddrs = append(o.IPAddrs, ipAddr)
}
}
func WithNoDefaultIPAddrs() CertOpt {
return func(o *CertOptions) {
o.SkipDefaultIPAddrs = true
}
}
func WithNotBefore(notBefore time.Time) CertOpt {
return func(o *CertOptions) {
o.NotBefore = notBefore
}
}
func WithNotAfter(notAfter time.Time) CertOpt {
return func(o *CertOptions) {
o.NotAfter = notAfter
}
}
func WithCombinedFile() CertOpt {
return func(o *CertOptions) {
o.CombinedFile = true
}
}
// WithExtKeyUsage replaces the leaf certificate's extended key usages, which
// default to server and client authentication. Passing no usages omits the
// extension entirely, leaving the certificate unrestricted.
func WithExtKeyUsage(eku ...x509.ExtKeyUsage) CertOpt {
return func(o *CertOptions) {
o.ExtKeyUsage = eku
o.ExtKeyUsageSet = true
}
}
func WithCASubject(organization, commonName string) CertOpt {
return func(o *CertOptions) {
o.CAOrganization = organization
o.CACommonName = commonName
}
}
func NewSelfSignedCert(t *testing.T, opts ...CertOpt) *Cert {
t.Helper()
tmpdir := t.TempDir()
// Get options for self-signed certificate and set defaults if needed.
options := CertOptions{}
for _, o := range opts {
o(&options)
}
if len(options.DNSNames) == 0 {
options.DNSNames = []string{"localhost"}
}
if len(options.IPAddrs) == 0 && !options.SkipDefaultIPAddrs {
options.IPAddrs = []net.IP{net.IPv4(127, 0, 0, 1)}
}
if options.NotBefore.IsZero() {
options.NotBefore = time.Now()
}
if options.NotAfter.IsZero() {
options.NotAfter = time.Now().Add(7 * 24 * time.Hour)
}
if options.CAOrganization == "" {
options.CAOrganization = "my_test_ca"
}
if options.CACommonName == "" {
options.CACommonName = "My Test CA"
}
// Sanity check options.
require.NotEmpty(t, options.DNSNames)
// Now generate the certificate.
capk, err := rsa.GenerateKey(rand.Reader, 1024)
require.NoError(t, err)
caSerial, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
require.NoError(t, err)
caTemplate := x509.Certificate{
SerialNumber: caSerial,
NotBefore: options.NotBefore,
NotAfter: options.NotAfter,
KeyUsage: x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
BasicConstraintsValid: true,
Subject: pkix.Name{
Organization: []string{options.CAOrganization},
CommonName: options.CACommonName,
},
IsCA: true,
}
caCert, err := x509.CreateCertificate(rand.Reader, &caTemplate, &caTemplate, capk.Public(), capk)
require.NoError(t, err)
caCertPEM := &pem.Block{Type: "CERTIFICATE", Bytes: caCert}
caCertFile, err := os.CreateTemp(tmpdir, "cacert-*.pem")
require.NoError(t, err)
require.NoError(t, pem.Encode(caCertFile, caCertPEM))
require.NoError(t, caCertFile.Close())
caKeyFile, err := os.CreateTemp(tmpdir, "cakey-*.pem")
require.NoError(t, err)
require.NoError(t, pem.Encode(caKeyFile, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(capk)}))
require.NoError(t, caKeyFile.Close())
serial, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
require.NoError(t, err)
extKeyUsage := []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}
if options.ExtKeyUsageSet {
extKeyUsage = options.ExtKeyUsage
}
// Basically the same as the CA template, but its own serial, and with ip addresses and dns names.
template := x509.Certificate{
SerialNumber: serial,
NotBefore: options.NotBefore,
NotAfter: options.NotAfter,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: extKeyUsage,
BasicConstraintsValid: true,
Subject: pkix.Name{
CommonName: options.DNSNames[0],
},
IPAddresses: options.IPAddrs,
DNSNames: options.DNSNames,
}
pk, err := rsa.GenerateKey(rand.Reader, 1024)
require.NoError(t, err)
cert, err := x509.CreateCertificate(rand.Reader, &template, &caTemplate, pk.Public(), capk)
require.NoError(t, err)
certPEM := &pem.Block{Type: "CERTIFICATE", Bytes: cert}
keyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(pk)}
var certPath, keyPath string
if options.CombinedFile {
// Create a single file containing both certificate and key
combinedFile, err := os.CreateTemp(tmpdir, "combined-*.pem")
require.NoError(t, err)
require.NoError(t, pem.Encode(combinedFile, certPEM))
require.NoError(t, pem.Encode(combinedFile, keyPEM))
require.NoError(t, combinedFile.Close())
certPath = combinedFile.Name()
keyPath = combinedFile.Name()
} else {
// Create separate certificate and key files
certFile, err := os.CreateTemp(tmpdir, "sslcert-*.pem")
require.NoError(t, err)
require.NoError(t, pem.Encode(certFile, certPEM))
require.NoError(t, certFile.Close())
keyFile, err := os.CreateTemp(tmpdir, "key-*.pem")
require.NoError(t, err)
require.NoError(t, pem.Encode(keyFile, keyPEM))
require.NoError(t, keyFile.Close())
certPath = certFile.Name()
keyPath = keyFile.Name()
}
return &Cert{
CACertPath: caCertFile.Name(),
CAKeyPath: caKeyFile.Name(),
CertPath: certPath,
KeyPath: keyPath,
}
}
func (c *Cert) ClientTLSConfig(t *testing.T, insecure, addCert bool) *tls.Config {
t.Helper()
cert, err := os.ReadFile(c.CACertPath)
require.NoError(t, err)
// Return a TLS config that trusts our self-generated CA.
var pool *x509.CertPool
if !insecure {
pool = x509.NewCertPool()
require.True(t, pool.AppendCertsFromPEM(cert), "failed to append certificate")
}
conf := &tls.Config{
RootCAs: pool,
InsecureSkipVerify: insecure,
}
if addCert {
clientPair, err := tls.LoadX509KeyPair(c.CertPath, c.KeyPath)
require.NoError(t, err)
conf.Certificates = []tls.Certificate{clientPair}
}
return conf
}
func (c *Cert) ServerTLSConfig(t *testing.T) *tls.Config {
cert, err := tls.LoadX509KeyPair(c.CertPath, c.KeyPath)
require.NoError(t, err)
return &tls.Config{
ServerName: "localhost",
Certificates: []tls.Certificate{cert},
}
}