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"
17 changes: 15 additions & 2 deletions web/tls_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,17 +272,26 @@ func ConfigToTLSConfig(c *TLSConfig) (*tls.Config, error) {
cfg.CurvePreferences = cp
}

// clientCAsErr 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.
var clientCAsErr error
if c.ClientCAs != "" {
clientCAPool := x509.NewCertPool()
clientCAFile, err := os.ReadFile(c.ClientCAs)
if err != nil {
return nil, err
}
clientCAPool.AppendCertsFromPEM(clientCAFile)
if !clientCAPool.AppendCertsFromPEM(clientCAFile) {
clientCAsErr = fmt.Errorf("no client CA certificates found in client_ca_file (%s)", c.ClientCAs)
}
cfg.ClientCAs = clientCAPool
} else if c.ClientCAsText != "" {
clientCAPool := x509.NewCertPool()
clientCAPool.AppendCertsFromPEM([]byte(c.ClientCAsText))
if !clientCAPool.AppendCertsFromPEM([]byte(c.ClientCAsText)) {
clientCAsErr = errors.New("no client CA certificates found in client_ca")
}
cfg.ClientCAs = clientCAPool
}

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

if clientCAsErr != nil {
return nil, clientCAsErr
}

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 @@ -48,6 +48,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 @@ -167,6 +168,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
Loading