@@ -2,27 +2,18 @@ package tlsconfig
2
2
3
3
import (
4
4
"bytes"
5
- "crypto"
6
- "crypto/ecdsa"
7
- "crypto/elliptic"
8
- "crypto/rand"
9
- "crypto/rsa"
10
5
"crypto/tls"
11
6
"crypto/x509"
12
- "crypto/x509/pkix"
13
7
"encoding/pem"
14
- "io"
15
8
"io/ioutil"
16
- "math/big"
17
9
"os"
18
- "path/filepath"
19
10
"reflect"
20
11
"testing"
21
- "time"
22
12
)
23
13
24
14
// This is the currently active LetsEncrypt IdenTrust cross-signed CA cert. It expires Mar 17, 2021.
25
- const systemRootTrustedCert = `
15
+ const (
16
+ systemRootTrustedCert = `
26
17
-----BEGIN CERTIFICATE-----
27
18
MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/
28
19
MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT
@@ -51,108 +42,27 @@ PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6
51
42
KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==
52
43
-----END CERTIFICATE-----
53
44
`
45
+ rsaPrivateKeyFile = "fixtures/key.pem"
46
+ certificateFile = "fixtures/cert.pem"
47
+ multiCertificateFile = "fixtures/multi.pem"
48
+ )
54
49
55
- var certTemplate = x509.Certificate {
56
- SerialNumber : big .NewInt (199999 ),
57
- Subject : pkix.Name {
58
- CommonName : "test" ,
59
- },
60
- NotBefore : time .Now ().AddDate (- 1 , 1 , 1 ),
61
- NotAfter : time .Now ().AddDate (1 , 1 , 1 ),
62
-
63
- KeyUsage : x509 .KeyUsageKeyEncipherment | x509 .KeyUsageDigitalSignature ,
64
- ExtKeyUsage : []x509.ExtKeyUsage {x509 .ExtKeyUsageCodeSigning , x509 .ExtKeyUsageAny },
65
-
66
- BasicConstraintsValid : true ,
67
- }
68
-
69
- func generateCertificate (t * testing.T , signer crypto.Signer , out io.Writer , isCA bool ) {
70
- template := certTemplate
71
- template .IsCA = isCA
72
- if isCA {
73
- template .KeyUsage = template .KeyUsage | x509 .KeyUsageCertSign
74
- template .MaxPathLen = 1
75
- }
76
- derBytes , err := x509 .CreateCertificate (rand .Reader , & template , & certTemplate , signer .Public (), signer )
77
- if err != nil {
78
- t .Fatal ("Unable to generate a certificate" , err .Error ())
79
- }
80
-
81
- if err = pem .Encode (out , & pem.Block {Type : "CERTIFICATE" , Bytes : derBytes }); err != nil {
82
- t .Fatal ("Unable to write cert to file" , err .Error ())
83
- }
84
- }
85
-
86
- // generates a multiple-certificate CA file with both RSA and ECDSA certs and
87
- // returns the filename so that cleanup can be deferred.
88
- func generateMultiCert (t * testing.T , tempDir string ) string {
89
- certOut , err := os .Create (filepath .Join (tempDir , "multi" ))
90
- if err != nil {
91
- t .Fatal ("Unable to create file to write multi-cert to" , err .Error ())
92
- }
93
- defer certOut .Close ()
94
-
95
- rsaKey , err := rsa .GenerateKey (rand .Reader , 2048 )
96
- if err != nil {
97
- t .Fatal ("Unable to generate RSA key for multi-cert" , err .Error ())
98
- }
99
- ecKey , err := ecdsa .GenerateKey (elliptic .P256 (), rand .Reader )
100
- if err != nil {
101
- t .Fatal ("Unable to generate ECDSA key for multi-cert" , err .Error ())
102
- }
103
-
104
- for _ , signer := range []crypto.Signer {rsaKey , ecKey } {
105
- generateCertificate (t , signer , certOut , true )
106
- }
107
-
108
- return certOut .Name ()
109
- }
110
-
111
- func generateCertAndKey (t * testing.T , tempDir string ) (string , string ) {
112
- rsaKey , err := rsa .GenerateKey (rand .Reader , 2048 )
113
- if err != nil {
114
- t .Fatal ("Unable to generate RSA key" , err .Error ())
115
-
116
- }
117
- keyBytes := x509 .MarshalPKCS1PrivateKey (rsaKey )
118
-
119
- keyOut , err := os .Create (filepath .Join (tempDir , "key" ))
120
- if err != nil {
121
- t .Fatal ("Unable to create file to write key to" , err .Error ())
122
-
123
- }
124
- defer keyOut .Close ()
125
-
126
- if err = pem .Encode (keyOut , & pem.Block {Type : "RSA PRIVATE KEY" , Bytes : keyBytes }); err != nil {
127
- t .Fatal ("Unable to write key to file" , err .Error ())
128
- }
129
-
130
- certOut , err := os .Create (filepath .Join (tempDir , "cert" ))
131
- if err != nil {
132
- t .Fatal ("Unable to create file to write cert to" , err .Error ())
133
- }
134
- defer certOut .Close ()
135
-
136
- generateCertificate (t , rsaKey , certOut , false )
137
-
138
- return keyOut .Name (), certOut .Name ()
50
+ // returns the name of a pre-generated, multiple-certificate CA file
51
+ // with both RSA and ECDSA certs.
52
+ func getMultiCert () string {
53
+ return multiCertificateFile
139
54
}
140
55
141
- func makeTempDir (t * testing.T ) string {
142
- tempDir , err := ioutil .TempDir ("" , "tlsconfig-test" )
143
- if err != nil {
144
- t .Fatal ("Could not make a temporary directory" , err .Error ())
145
- }
146
- return tempDir
56
+ // returns the names of pre-generated key and certificate files.
57
+ func getCertAndKey () (string , string ) {
58
+ return rsaPrivateKeyFile , certificateFile
147
59
}
148
60
149
61
// If the cert files and directory are provided but are invalid, an error is
150
62
// returned.
151
63
func TestConfigServerTLSFailsIfUnableToLoadCerts (t * testing.T ) {
152
- tempDir := makeTempDir (t )
153
- defer os .RemoveAll (tempDir )
154
- key , cert := generateCertAndKey (t , tempDir )
155
- ca := generateMultiCert (t , tempDir )
64
+ key , cert := getCertAndKey ()
65
+ ca := getMultiCert ()
156
66
157
67
tempFile , err := ioutil .TempFile ("" , "cert-test" )
158
68
if err != nil {
@@ -182,9 +92,7 @@ func TestConfigServerTLSFailsIfUnableToLoadCerts(t *testing.T) {
182
92
// If server cert and key are provided and client auth and client CA are not
183
93
// set, a tls config with only the server certs will be returned.
184
94
func TestConfigServerTLSServerCertsOnly (t * testing.T ) {
185
- tempDir := makeTempDir (t )
186
- defer os .RemoveAll (tempDir )
187
- key , cert := generateCertAndKey (t , tempDir )
95
+ key , cert := getCertAndKey ()
188
96
189
97
keypair , err := tls .LoadX509KeyPair (cert , key )
190
98
if err != nil {
@@ -225,10 +133,8 @@ func TestConfigServerTLSServerCertsOnly(t *testing.T) {
225
133
// If client CA is provided, it will only be used if the client auth is >=
226
134
// VerifyClientCertIfGiven
227
135
func TestConfigServerTLSClientCANotSetIfClientAuthTooLow (t * testing.T ) {
228
- tempDir := makeTempDir (t )
229
- defer os .RemoveAll (tempDir )
230
- key , cert := generateCertAndKey (t , tempDir )
231
- ca := generateMultiCert (t , tempDir )
136
+ key , cert := getCertAndKey ()
137
+ ca := getMultiCert ()
232
138
233
139
tlsConfig , err := Server (Options {
234
140
CertFile : cert ,
@@ -255,10 +161,8 @@ func TestConfigServerTLSClientCANotSetIfClientAuthTooLow(t *testing.T) {
255
161
// If client CA is provided, it will only be used if the client auth is >=
256
162
// VerifyClientCertIfGiven
257
163
func TestConfigServerTLSClientCASet (t * testing.T ) {
258
- tempDir := makeTempDir (t )
259
- defer os .RemoveAll (tempDir )
260
- key , cert := generateCertAndKey (t , tempDir )
261
- ca := generateMultiCert (t , tempDir )
164
+ key , cert := getCertAndKey ()
165
+ ca := getMultiCert ()
262
166
263
167
tlsConfig , err := Server (Options {
264
168
CertFile : cert ,
@@ -290,10 +194,8 @@ func TestConfigServerTLSClientCASet(t *testing.T) {
290
194
// Exclusive root pools determines whether the CA pool will be a union of the system
291
195
// certificate pool and custom certs, or an exclusive or of the custom certs and system pool
292
196
func TestConfigServerExclusiveRootPools (t * testing.T ) {
293
- tempDir := makeTempDir (t )
294
- defer os .RemoveAll (tempDir )
295
- key , cert := generateCertAndKey (t , tempDir )
296
- ca := generateMultiCert (t , tempDir )
197
+ key , cert := getCertAndKey ()
198
+ ca := getMultiCert ()
297
199
298
200
caBytes , err := ioutil .ReadFile (ca )
299
201
if err != nil {
@@ -384,9 +286,7 @@ func TestConfigServerTLSMinVersionIsSetBasedOnOptions(t *testing.T) {
384
286
tls .VersionTLS11 ,
385
287
tls .VersionTLS12 ,
386
288
}
387
- tempDir := makeTempDir (t )
388
- defer os .RemoveAll (tempDir )
389
- key , cert := generateCertAndKey (t , tempDir )
289
+ key , cert := getCertAndKey ()
390
290
391
291
for _ , v := range versions {
392
292
tlsConfig , err := Server (Options {
@@ -408,9 +308,7 @@ func TestConfigServerTLSMinVersionIsSetBasedOnOptions(t *testing.T) {
408
308
// An error should be returned if the specified minimum version for the server
409
309
// is too low, i.e. less than VersionTLS10
410
310
func TestConfigServerTLSMinVersionNotSetIfMinVersionIsTooLow (t * testing.T ) {
411
- tempDir := makeTempDir (t )
412
- defer os .RemoveAll (tempDir )
413
- key , cert := generateCertAndKey (t , tempDir )
311
+ key , cert := getCertAndKey ()
414
312
415
313
_ , err := Server (Options {
416
314
MinVersion : tls .VersionSSL30 ,
@@ -426,9 +324,7 @@ func TestConfigServerTLSMinVersionNotSetIfMinVersionIsTooLow(t *testing.T) {
426
324
// An error should be returned if an invalid minimum version for the server is
427
325
// in the options struct
428
326
func TestConfigServerTLSMinVersionNotSetIfMinVersionIsInvalid (t * testing.T ) {
429
- tempDir := makeTempDir (t )
430
- defer os .RemoveAll (tempDir )
431
- key , cert := generateCertAndKey (t , tempDir )
327
+ key , cert := getCertAndKey ()
432
328
433
329
_ , err := Server (Options {
434
330
MinVersion : 1 ,
@@ -444,9 +340,7 @@ func TestConfigServerTLSMinVersionNotSetIfMinVersionIsInvalid(t *testing.T) {
444
340
// The root CA is never set if InsecureSkipBoolean is set to true, but the
445
341
// default client options are set
446
342
func TestConfigClientTLSNoVerify (t * testing.T ) {
447
- tempDir := makeTempDir (t )
448
- defer os .RemoveAll (tempDir )
449
- ca := generateMultiCert (t , tempDir )
343
+ ca := getMultiCert ()
450
344
451
345
tlsConfig , err := Client (Options {CAFile : ca , InsecureSkipVerify : true })
452
346
@@ -497,9 +391,7 @@ func TestConfigClientTLSNoRoot(t *testing.T) {
497
391
498
392
// The RootCA is set if the file is provided and InsecureSkipVerify is false
499
393
func TestConfigClientTLSRootCAFileWithOneCert (t * testing.T ) {
500
- tempDir := makeTempDir (t )
501
- defer os .RemoveAll (tempDir )
502
- ca := generateMultiCert (t , tempDir )
394
+ ca := getMultiCert ()
503
395
504
396
tlsConfig , err := Client (Options {CAFile : ca })
505
397
@@ -531,9 +423,7 @@ func TestConfigClientTLSNonexistentRootCAFile(t *testing.T) {
531
423
// An error is returned if either the client cert or the key are provided
532
424
// but invalid or blank.
533
425
func TestConfigClientTLSClientCertOrKeyInvalid (t * testing.T ) {
534
- tempDir := makeTempDir (t )
535
- defer os .RemoveAll (tempDir )
536
- key , cert := generateCertAndKey (t , tempDir )
426
+ key , cert := getCertAndKey ()
537
427
538
428
tempFile , err := ioutil .TempFile ("" , "cert-test" )
539
429
if err != nil {
@@ -558,9 +448,7 @@ func TestConfigClientTLSClientCertOrKeyInvalid(t *testing.T) {
558
448
// The certificate is set if the client cert and client key are provided and
559
449
// valid.
560
450
func TestConfigClientTLSValidClientCertAndKey (t * testing.T ) {
561
- tempDir := makeTempDir (t )
562
- defer os .RemoveAll (tempDir )
563
- key , cert := generateCertAndKey (t , tempDir )
451
+ key , cert := getCertAndKey ()
564
452
565
453
keypair , err := tls .LoadX509KeyPair (cert , key )
566
454
if err != nil {
@@ -593,9 +481,7 @@ func TestConfigClientTLSValidClientCertAndKey(t *testing.T) {
593
481
// Exclusive root pools determines whether the CA pool will be a union of the system
594
482
// certificate pool and custom certs, or an exclusive or of the custom certs and system pool
595
483
func TestConfigClientExclusiveRootPools (t * testing.T ) {
596
- tempDir := makeTempDir (t )
597
- defer os .RemoveAll (tempDir )
598
- ca := generateMultiCert (t , tempDir )
484
+ ca := getMultiCert ()
599
485
600
486
caBytes , err := ioutil .ReadFile (ca )
601
487
if err != nil {
@@ -671,9 +557,7 @@ func TestConfigClientExclusiveRootPools(t *testing.T) {
671
557
// If a valid MinVersion is specified in the options, the client's
672
558
// minimum version should be set accordingly
673
559
func TestConfigClientTLSMinVersionIsSetBasedOnOptions (t * testing.T ) {
674
- tempDir := makeTempDir (t )
675
- defer os .RemoveAll (tempDir )
676
- key , cert := generateCertAndKey (t , tempDir )
560
+ key , cert := getCertAndKey ()
677
561
678
562
tlsConfig , err := Client (Options {
679
563
MinVersion : tls .VersionTLS12 ,
@@ -693,9 +577,7 @@ func TestConfigClientTLSMinVersionIsSetBasedOnOptions(t *testing.T) {
693
577
// An error should be returned if the specified minimum version for the client
694
578
// is too low, i.e. less than VersionTLS12
695
579
func TestConfigClientTLSMinVersionNotSetIfMinVersionIsTooLow (t * testing.T ) {
696
- tempDir := makeTempDir (t )
697
- defer os .RemoveAll (tempDir )
698
- key , cert := generateCertAndKey (t , tempDir )
580
+ key , cert := getCertAndKey ()
699
581
700
582
_ , err := Client (Options {
701
583
MinVersion : tls .VersionTLS11 ,
@@ -711,9 +593,7 @@ func TestConfigClientTLSMinVersionNotSetIfMinVersionIsTooLow(t *testing.T) {
711
593
// An error should be returned if an invalid minimum version for the client is
712
594
// in the options struct
713
595
func TestConfigClientTLSMinVersionNotSetIfMinVersionIsInvalid (t * testing.T ) {
714
- tempDir := makeTempDir (t )
715
- defer os .RemoveAll (tempDir )
716
- key , cert := generateCertAndKey (t , tempDir )
596
+ key , cert := getCertAndKey ()
717
597
718
598
_ , err := Client (Options {
719
599
MinVersion : 1 ,
0 commit comments