Skip to content

Commit 4e5688d

Browse files
committed
Add proper tests for TLS client certificates
Add tests cases when the client actually sends a certificate, and check how the server responds to it. The client certificates were generated with this command: openssl req -x509 -newkey ec:<(openssl ecparam -name secp384r1) -keyout client2_selfsigned.key -out client2_selfsigned.pem -nodes -subj '/CN=test2' -days 36500 -addext "extendedKeyUsage = clientAuth" Signed-off-by: WGH <[email protected]>
1 parent f62acde commit 4e5688d

File tree

6 files changed

+87
-3
lines changed

6 files changed

+87
-3
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MIG2AgEAMBAGByqGSM49AgEGBSuBBAAiBIGeMIGbAgEBBDC8CYtAwKp1uLWXLXFE
3+
Ue2Bz6PijwHZcL7jAxtlk2dbW0GlRQ+rcalHCcnExIIKAAehZANiAATlPRxDnbJb
4+
Zq9u+jh7DyEJumQZFqjIDFdFxfHtI6hwyMtlL6FIwpqn3z4uXs2wx6/NsD4XOChy
5+
j/tXXKCHS/22+51TivjGA53c9bLgc4dK/uJJNSivp0kymbtA5vgKzJE=
6+
-----END PRIVATE KEY-----
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIByjCCAU+gAwIBAgIUYcG9p4RzCRdvUGa9BWvc6rB/wMYwCgYIKoZIzj0EAwIw
3+
EDEOMAwGA1UEAwwFdGVzdDIwIBcNMjEwODIwMTUzMjE4WhgPMjEyMTA3MjcxNTMy
4+
MThaMBAxDjAMBgNVBAMMBXRlc3QyMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE5T0c
5+
Q52yW2avbvo4ew8hCbpkGRaoyAxXRcXx7SOocMjLZS+hSMKap98+Ll7NsMevzbA+
6+
Fzgoco/7V1ygh0v9tvudU4r4xgOd3PWy4HOHSv7iSTUor6dJMpm7QOb4CsyRo2gw
7+
ZjAdBgNVHQ4EFgQUWpsZ2aWo6WEI2LiNQXoWKYr0rlkwHwYDVR0jBBgwFoAUWpsZ
8+
2aWo6WEI2LiNQXoWKYr0rlkwDwYDVR0TAQH/BAUwAwEB/zATBgNVHSUEDDAKBggr
9+
BgEFBQcDAjAKBggqhkjOPQQDAgNpADBmAjEA/Mv4OjCqVw8PzxQW4FJmZNyJB4ps
10+
xkAUBRpDy75n64ICsWKX/Mille0bo+C8d63JAjEA3IH/y1O4oyCaawNpibfcwSZK
11+
7ND9Z+WTJi50EumXUWKirmb/V59ToH5nc10x7NDX
12+
-----END CERTIFICATE-----

web/testdata/client_selfsigned.key

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MIG2AgEAMBAGByqGSM49AgEGBSuBBAAiBIGeMIGbAgEBBDDmoTxYcBfRrqYb/TJy
3+
oHlBKo4/fNk2LBUZxpC3HeKasAQzS9AB1evw3k4M3Pe8c4+hZANiAASxUS40AV1Y
4+
h1ABCLCoJcG9B8Twv/gg2tU0zqdW9FhK2Fu13MeZkTRJLFVgFzlmCj3o9dIX8iUi
5+
RP9jYkQG6wHD44kb9NQ4A7fjs8DOANGWKgY/96liSh/ynPKCoWONW8w=
6+
-----END PRIVATE KEY-----

web/testdata/client_selfsigned.pem

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIBxzCCAU2gAwIBAgIUGCNnsX0qd0HD7UaQsx67ze0UaNowCgYIKoZIzj0EAwIw
3+
DzENMAsGA1UEAwwEdGVzdDAgFw0yMTA4MjAxNDQ5MTRaGA8yMTIxMDcyNzE0NDkx
4+
NFowDzENMAsGA1UEAwwEdGVzdDB2MBAGByqGSM49AgEGBSuBBAAiA2IABLFRLjQB
5+
XViHUAEIsKglwb0HxPC/+CDa1TTOp1b0WErYW7Xcx5mRNEksVWAXOWYKPej10hfy
6+
JSJE/2NiRAbrAcPjiRv01DgDt+OzwM4A0ZYqBj/3qWJKH/Kc8oKhY41bzKNoMGYw
7+
HQYDVR0OBBYEFPRbKtRBgw+AZ0b6T8oWw/+QoyjaMB8GA1UdIwQYMBaAFPRbKtRB
8+
gw+AZ0b6T8oWw/+QoyjaMA8GA1UdEwEB/wQFMAMBAf8wEwYDVR0lBAwwCgYIKwYB
9+
BQUHAwIwCgYIKoZIzj0EAwIDaAAwZQIwZqwXMJiTycZdmLN+Pwk/8Sb7wQazbocb
10+
16Zw5mZXqFJ4K+74OQMZ33i82hYohtE/AjEAn0a8q8QupgiXpr0I/PvGTRKqLQRM
11+
0mptBvpn/DcB2p3Hi80GJhtchz9Z0OqbMX4S
12+
-----END CERTIFICATE-----
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
tls_server_config:
2+
cert_file: "server.crt"
3+
key_file: "server.key"
4+
client_auth_type: "RequireAndVerifyClientCert"
5+
client_ca_file: "client_selfsigned.pem"

web/tls_config_test.go

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ type TestInputs struct {
8585
CurvePreferences []tls.CurveID
8686
Username string
8787
Password string
88+
ClientCertificate string
8889
}
8990

9091
func TestYAMLFiles(t *testing.T) {
@@ -287,6 +288,33 @@ func TestServerBehaviour(t *testing.T) {
287288
UseTLSClient: true,
288289
ExpectedError: ErrorMap["Bad certificate"],
289290
},
291+
{
292+
Name: `valid tls config yml and tls client with RequireAnyClientCert (present certificate)`,
293+
YAMLConfigPath: "testdata/tls_config_noAuth.requireanyclientcert.good.yml",
294+
UseTLSClient: true,
295+
ClientCertificate: "client_selfsigned",
296+
ExpectedError: nil,
297+
},
298+
{
299+
Name: `valid tls config yml and tls client with RequireAndVerifyClientCert`,
300+
YAMLConfigPath: "testdata/tls_config_noAuth.requireandverifyclientcert.good.yml",
301+
UseTLSClient: true,
302+
ExpectedError: ErrorMap["Bad certificate"],
303+
},
304+
{
305+
Name: `valid tls config yml and tls client with RequireAndVerifyClientCert (present certificate)`,
306+
YAMLConfigPath: "testdata/tls_config_noAuth.requireandverifyclientcert.good.yml",
307+
UseTLSClient: true,
308+
ClientCertificate: "client_selfsigned",
309+
ExpectedError: nil,
310+
},
311+
{
312+
Name: `valid tls config yml and tls client with RequireAndVerifyClientCert (present wrong certificate)`,
313+
YAMLConfigPath: "testdata/tls_config_noAuth.requireandverifyclientcert.good.yml",
314+
UseTLSClient: true,
315+
ClientCertificate: "client2_selfsigned",
316+
ExpectedError: ErrorMap["Bad certificate"],
317+
}
290318
}
291319
for _, testInputs := range testTables {
292320
t.Run(testInputs.Name, testInputs.Test)
@@ -330,7 +358,7 @@ func TestConfigReloading(t *testing.T) {
330358
recordConnectionError(err)
331359
}()
332360

333-
client := getTLSClient()
361+
client := getTLSClient("")
334362

335363
TestClientConnection := func() error {
336364
time.Sleep(250 * time.Millisecond)
@@ -404,7 +432,7 @@ func (test *TestInputs) Test(t *testing.T) {
404432
var client *http.Client
405433
var proto string
406434
if test.UseTLSClient {
407-
client = getTLSClient()
435+
client = getTLSClient(test.ClientCertificate)
408436
t := client.Transport.(*http.Transport)
409437
t.TLSClientConfig.MaxVersion = test.ClientMaxTLSVersion
410438
if len(test.CipherSuites) > 0 {
@@ -496,11 +524,23 @@ func (test *TestInputs) isCorrectError(returnedError error) bool {
496524
return true
497525
}
498526

499-
func getTLSClient() *http.Client {
527+
func getTLSClient(clientCertName string) *http.Client {
500528
cert, err := ioutil.ReadFile("testdata/tls-ca-chain.pem")
501529
if err != nil {
502530
panic("Unable to start TLS client. Check cert path")
503531
}
532+
533+
var clientCertficate tls.Certificate
534+
if clientCertName != "" {
535+
clientCertficate, err = tls.LoadX509KeyPair(
536+
"testdata/"+clientCertName+".pem",
537+
"testdata/"+clientCertName+".key",
538+
)
539+
if err != nil {
540+
panic(fmt.Sprintf("failed to load client certificate: %v", err))
541+
}
542+
}
543+
504544
client := &http.Client{
505545
Transport: &http.Transport{
506546
TLSClientConfig: &tls.Config{
@@ -509,6 +549,9 @@ func getTLSClient() *http.Client {
509549
caCertPool.AppendCertsFromPEM(cert)
510550
return caCertPool
511551
}(),
552+
GetClientCertificate: func(req *tls.CertificateRequestInfo) (*tls.Certificate, error) {
553+
return &clientCertficate, nil
554+
},
512555
},
513556
},
514557
}

0 commit comments

Comments
 (0)