@@ -2,6 +2,8 @@ package config
2
2
3
3
import (
4
4
"crypto/tls"
5
+ "crypto/x509"
6
+ "errors"
5
7
"log/slog"
6
8
"testing"
7
9
@@ -24,11 +26,188 @@ func TestGetServerTLSConfig(t *testing.T) {
24
26
ClientCAs : bundle .CACert .Name (),
25
27
ClientCRL : bundle .ClientCRL .Name (),
26
28
},
27
- }, tlsserver . WithTLSServerNextProtos ([] string { "h2" }) )
29
+ })
28
30
require .NoError (t , err )
29
31
require .NotNil (t , tlsConfig .ClientCAs )
30
32
require .Equal (t , tlsConfig .ClientAuth , tls .RequireAndVerifyClientCert )
31
33
require .NotEmpty (t , tlsConfig .Certificates )
34
+ // clientCRL verification
32
35
require .NotNil (t , tlsConfig .VerifyPeerCertificate )
36
+ require .Nil (t , tlsConfig .NextProtos )
37
+ require .Nil (t , tlsConfig .CipherSuites )
38
+ require .Nil (t , tlsConfig .CurvePreferences )
39
+ }
40
+
41
+ func TestGetServerTLSOptionsConfig (t * testing.T ) {
42
+ bundle := testutil .NewCertsBundle ()
43
+ defer bundle .Close ()
44
+
45
+ tlsConfig , err := GetServerTLSConfig (slog .Default (), & config.TLSServerConfig {
46
+ Enable : true ,
47
+ Refresh : 0 ,
48
+ File : config.TLSServerFiles {
49
+ Key : bundle .ServerKey .Name (),
50
+ Cert : bundle .ServerCert .Name (),
51
+ },
52
+ }, tlsserver .WithTLSServerNextProtos ([]string {"h2" }),
53
+ tlsserver .WithTLSServerCipherSuites ([]uint16 {tls .TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 }),
54
+ tlsserver .WithTLSServerCurvePreferences ([]tls.CurveID {tls .CurveP256 , tls .CurveP384 }),
55
+ )
56
+ require .NoError (t , err )
57
+ require .Nil (t , tlsConfig .ClientCAs )
58
+ require .Equal (t , tlsConfig .ClientAuth , tls .NoClientCert )
59
+ require .NotEmpty (t , tlsConfig .Certificates )
60
+ require .Nil (t , tlsConfig .VerifyPeerCertificate )
33
61
require .Equal (t , tlsConfig .NextProtos , []string {"h2" })
62
+ require .Equal (t , tlsConfig .CipherSuites , []uint16 {tls .TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 })
63
+ require .Equal (t , tlsConfig .CurvePreferences , []tls.CurveID {tls .CurveP256 , tls .CurveP384 })
64
+ }
65
+
66
+ func TestGetServerTLSVerifyPeerCertificateConfig (t * testing.T ) {
67
+ bundle := testutil .NewCertsBundle ()
68
+ defer bundle .Close ()
69
+
70
+ tests := []struct {
71
+ name string
72
+ clientCAs string
73
+ verifyFuncs []tlsserver.VerifyPeerCertificateFunc
74
+ verifyError error
75
+ }{
76
+ {
77
+ name : "no peer verification" ,
78
+ },
79
+ {
80
+ name : "default client CA/CLR verification" ,
81
+ clientCAs : bundle .CACert .Name (),
82
+ verifyError : nil , // CRLs are not set, verification is successful
83
+ },
84
+ {
85
+ name : "client CA/CLR verify success, second verify success" ,
86
+ clientCAs : bundle .CACert .Name (),
87
+ verifyFuncs : []tlsserver.VerifyPeerCertificateFunc {
88
+ func (rawCerts [][]byte , verifiedChains [][]* x509.Certificate ) error {
89
+ return nil
90
+ },
91
+ },
92
+ },
93
+ {
94
+ name : "client CA/CLR verify success, third verify success" ,
95
+ clientCAs : bundle .CACert .Name (),
96
+ verifyFuncs : []tlsserver.VerifyPeerCertificateFunc {
97
+ func (rawCerts [][]byte , verifiedChains [][]* x509.Certificate ) error {
98
+ return nil
99
+ },
100
+ func (rawCerts [][]byte , verifiedChains [][]* x509.Certificate ) error {
101
+ return nil
102
+ },
103
+ },
104
+ },
105
+ {
106
+ name : "client CA/CLR verify success, third verify failure" ,
107
+ clientCAs : bundle .CACert .Name (),
108
+ verifyFuncs : []tlsserver.VerifyPeerCertificateFunc {
109
+ func (rawCerts [][]byte , verifiedChains [][]* x509.Certificate ) error {
110
+ return nil
111
+ },
112
+ func (rawCerts [][]byte , verifiedChains [][]* x509.Certificate ) error {
113
+ return errors .New ("3 function failed" )
114
+ },
115
+ },
116
+ verifyError : errors .New ("3 function failed" ),
117
+ },
118
+ {
119
+ name : "client CA/CLR verify success, second verify failure" ,
120
+ clientCAs : bundle .CACert .Name (),
121
+ verifyFuncs : []tlsserver.VerifyPeerCertificateFunc {
122
+ func (rawCerts [][]byte , verifiedChains [][]* x509.Certificate ) error {
123
+ return errors .New ("2 function failed" )
124
+ },
125
+ func (rawCerts [][]byte , verifiedChains [][]* x509.Certificate ) error {
126
+ return errors .New ("3 function would also fail" )
127
+ },
128
+ },
129
+ verifyError : errors .New ("2 function failed" ),
130
+ },
131
+ {
132
+ name : "first verify success" ,
133
+ verifyFuncs : []tlsserver.VerifyPeerCertificateFunc {
134
+ func (rawCerts [][]byte , verifiedChains [][]* x509.Certificate ) error {
135
+ return nil
136
+ },
137
+ },
138
+ },
139
+ {
140
+ name : "second verify success" ,
141
+ verifyFuncs : []tlsserver.VerifyPeerCertificateFunc {
142
+ func (rawCerts [][]byte , verifiedChains [][]* x509.Certificate ) error {
143
+ return nil
144
+ },
145
+ func (rawCerts [][]byte , verifiedChains [][]* x509.Certificate ) error {
146
+ return nil
147
+ },
148
+ },
149
+ },
150
+ {
151
+ name : "second verify failure" ,
152
+ verifyFuncs : []tlsserver.VerifyPeerCertificateFunc {
153
+ func (rawCerts [][]byte , verifiedChains [][]* x509.Certificate ) error {
154
+ return nil
155
+ },
156
+ func (rawCerts [][]byte , verifiedChains [][]* x509.Certificate ) error {
157
+ return errors .New ("2 function failed" )
158
+ },
159
+ },
160
+ verifyError : errors .New ("2 function failed" ),
161
+ },
162
+ {
163
+ name : "first verify failure" ,
164
+ verifyFuncs : []tlsserver.VerifyPeerCertificateFunc {
165
+ func (rawCerts [][]byte , verifiedChains [][]* x509.Certificate ) error {
166
+ return errors .New ("1 function failed" )
167
+ },
168
+ func (rawCerts [][]byte , verifiedChains [][]* x509.Certificate ) error {
169
+ return errors .New ("2 function would also fail" )
170
+ },
171
+ },
172
+ verifyError : errors .New ("1 function failed" ),
173
+ },
174
+ {
175
+ name : "unset verify function" ,
176
+ verifyFuncs : []tlsserver.VerifyPeerCertificateFunc {
177
+ func (rawCerts [][]byte , verifiedChains [][]* x509.Certificate ) error {
178
+ return errors .New ("1 function failed" )
179
+ },
180
+ nil , // unset chain of verify functions
181
+ func (rawCerts [][]byte , verifiedChains [][]* x509.Certificate ) error {
182
+ return nil
183
+ },
184
+ },
185
+ },
186
+ }
187
+ for _ , tc := range tests {
188
+ t .Run (tc .name , func (t * testing.T ) {
189
+
190
+ opts := make ([]tlsserver.TLSServerConfigOption , 0 , len (tc .verifyFuncs ))
191
+ for _ , f := range tc .verifyFuncs {
192
+ opts = append (opts , tlsserver .WithTLSServerVerifyPeerCertificate (f ))
193
+ }
194
+ tlsConfig , err := GetServerTLSConfig (slog .Default (), & config.TLSServerConfig {
195
+ Enable : true ,
196
+ Refresh : 0 ,
197
+ File : config.TLSServerFiles {
198
+ Key : bundle .ServerKey .Name (),
199
+ Cert : bundle .ServerCert .Name (),
200
+ ClientCAs : tc .clientCAs ,
201
+ },
202
+ }, opts ... )
203
+ require .NoError (t , err )
204
+ if tc .clientCAs == "" && len (tc .verifyFuncs ) == 0 {
205
+ require .Nil (t , tlsConfig .VerifyPeerCertificate )
206
+ } else {
207
+ require .NotNil (t , tlsConfig .VerifyPeerCertificate )
208
+ err = tlsConfig .VerifyPeerCertificate (nil , nil )
209
+ require .Equal (t , tc .verifyError , err )
210
+ }
211
+ })
212
+ }
34
213
}
0 commit comments