Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions web/testdata/tls-ca-chain-invalid.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This file is intentionally not a PEM encoded certificate. It stands in for a
truncated, DER encoded or otherwise unusable CA bundle.
5 changes: 5 additions & 0 deletions web/testdata/web_config_auth_clientCAsText_nocerts.bad.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tls_server_config:
cert_file: "server.crt"
key_file: "server.key"
client_auth_type: "RequireAndVerifyClientCert"
client_ca: "this is not a PEM encoded certificate"
5 changes: 5 additions & 0 deletions web/testdata/web_config_auth_clientCAs_nocerts.bad.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tls_server_config:
cert_file: "server.crt"
key_file: "server.key"
client_auth_type: "RequireAndVerifyClientCert"
client_ca_file: "tls-ca-chain-invalid.pem"
16 changes: 14 additions & 2 deletions web/tls_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,22 @@ func ConfigToTLSConfig(c *TLSConfig) (*tls.Config, error) {
cfg.CurvePreferences = cp
}

// clientCAsEmpty records that a client CA was configured but held no
// usable certificate. It is reported after the client auth policy has been
// validated, so that a configuration carrying both faults keeps reporting
// the policy one.
clientCAsEmpty := false
if c.ClientCAs != "" {
clientCAPool := x509.NewCertPool()
clientCAFile, err := os.ReadFile(c.ClientCAs)
if err != nil {
return nil, err
}
clientCAPool.AppendCertsFromPEM(clientCAFile)
clientCAsEmpty = !clientCAPool.AppendCertsFromPEM(clientCAFile)
cfg.ClientCAs = clientCAPool
} else if c.ClientCAsText != "" {
clientCAPool := x509.NewCertPool()
clientCAPool.AppendCertsFromPEM([]byte(c.ClientCAsText))
clientCAsEmpty = !clientCAPool.AppendCertsFromPEM([]byte(c.ClientCAsText))
cfg.ClientCAs = clientCAPool
}

Expand Down Expand Up @@ -296,6 +301,13 @@ func ConfigToTLSConfig(c *TLSConfig) (*tls.Config, error) {
return nil, errors.New("client CA's have been configured without a Client Auth Policy")
}

if clientCAsEmpty {
if c.ClientCAs != "" {
return nil, fmt.Errorf("no client CA certificates found in client_ca_file (%s)", c.ClientCAs)
}
return nil, errors.New("no client CA certificates found in client_ca")
}

return cfg, nil
}

Expand Down
11 changes: 11 additions & 0 deletions web/tls_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var (
"Invalid Cert or CertPath": regexp.MustCompile(`missing one of cert or cert_file`),
"Invalid Key or KeyPath": regexp.MustCompile(`missing one of key or key_file`),
"ClientCA set without policy": regexp.MustCompile(`client CA's have been configured without a Client Auth Policy`),
"No client CA certificates": regexp.MustCompile(`no client CA certificates found in client_ca`),
"Bad password": regexp.MustCompile(`hashedSecret too short to be a bcrypted password`),
"Unauthorized": regexp.MustCompile(`Unauthorized`),
"Forbidden": regexp.MustCompile(`Forbidden`),
Expand Down Expand Up @@ -171,6 +172,16 @@ func TestYAMLFiles(t *testing.T) {
YAMLConfigPath: "testdata/web_config_auth_clientCAs_invalid.bad.yml",
ExpectedError: ErrorMap["No such file"],
},
{
Name: `invalid config yml (client CA file without certificates)`,
YAMLConfigPath: "testdata/web_config_auth_clientCAs_nocerts.bad.yml",
ExpectedError: ErrorMap["No client CA certificates"],
},
{
Name: `invalid config yml (inline client CA without certificates)`,
YAMLConfigPath: "testdata/web_config_auth_clientCAsText_nocerts.bad.yml",
ExpectedError: ErrorMap["No client CA certificates"],
},
{
Name: `invalid config yml (invalid user list)`,
YAMLConfigPath: "testdata/web_config_auth_user_list_invalid.bad.yml",
Expand Down