Skip to content

Commit f976c84

Browse files
httpcaddyfile: Fix cert file decoding to load multiple PEM in one file (#5997)
1 parent 1bf72db commit f976c84

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

caddyconfig/httpcaddyfile/builtins.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -246,16 +246,26 @@ func parseTLS(h Helper) ([]ConfigValue, error) {
246246
if err != nil {
247247
return nil, err
248248
}
249-
block, _ := pem.Decode(certDataPEM)
250-
if block == nil || block.Type != "CERTIFICATE" {
251-
return nil, h.Errf("no CERTIFICATE pem block found in %s", h.Val())
249+
// while block is not nil, we have more certificates in the file
250+
for block, rest := pem.Decode(certDataPEM); block != nil; block, rest = pem.Decode(rest) {
251+
if block.Type != "CERTIFICATE" {
252+
return nil, h.Errf("no CERTIFICATE pem block found in %s", filename)
253+
}
254+
if subdir == "trusted_ca_cert_file" {
255+
cp.ClientAuthentication.TrustedCACerts = append(
256+
cp.ClientAuthentication.TrustedCACerts,
257+
base64.StdEncoding.EncodeToString(block.Bytes),
258+
)
259+
} else {
260+
cp.ClientAuthentication.TrustedLeafCerts = append(
261+
cp.ClientAuthentication.TrustedLeafCerts,
262+
base64.StdEncoding.EncodeToString(block.Bytes),
263+
)
264+
}
252265
}
253-
if subdir == "trusted_ca_cert_file" {
254-
cp.ClientAuthentication.TrustedCACerts = append(cp.ClientAuthentication.TrustedCACerts,
255-
base64.StdEncoding.EncodeToString(block.Bytes))
256-
} else {
257-
cp.ClientAuthentication.TrustedLeafCerts = append(cp.ClientAuthentication.TrustedLeafCerts,
258-
base64.StdEncoding.EncodeToString(block.Bytes))
266+
// if we decoded nothing, return an error
267+
if len(cp.ClientAuthentication.TrustedCACerts) == 0 && len(cp.ClientAuthentication.TrustedLeafCerts) == 0 {
268+
return nil, h.Errf("no CERTIFICATE pem block found in %s", filename)
259269
}
260270

261271
default:

0 commit comments

Comments
 (0)