Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

httpcaddyfile: Add client certificate config to tls #3335

Merged
merged 11 commits into from
Jun 5, 2020
70 changes: 70 additions & 0 deletions caddyconfig/httpcaddyfile/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
package httpcaddyfile

import (
"encoding/base64"
"encoding/pem"
"fmt"
"html"
"io/ioutil"
"net/http"
"reflect"
"strings"
Expand Down Expand Up @@ -59,6 +62,13 @@ func parseBind(h Helper) ([]ConfigValue, error) {
// protocols <min> [<max>]
// ciphers <cipher_suites...>
// curves <curves...>
// clients {
// mode [request|require|verify_if_given|require_and_verify]
// trusted_ca_certs <trusted_ca_certs...>
// trusted_ca_certs_file <filename...>
// trusted_leaf_certs <trusted_leaf_certs...>
// trusted_leaf_certs_file <filename...>
// }
// alpn <values...>
// load <paths...>
// ca <acme_ca_endpoint>
Expand Down Expand Up @@ -181,6 +191,66 @@ func parseTLS(h Helper) ([]ConfigValue, error) {
cp.Curves = append(cp.Curves, h.Val())
}

case "clients":
mholt marked this conversation as resolved.
Show resolved Hide resolved
ch := h.NewFromNextSegment() // new helper for client auth config
cp.ClientAuthentication = &caddytls.ClientAuthentication{}
for ch.Next() {
for ch.NextBlock(0) {
switch ch.Val() {
case "mode":
for ch.NextArg() {
cp.ClientAuthentication.Mode = ch.Val()
}
case "trusted_ca_certs":
mholt marked this conversation as resolved.
Show resolved Hide resolved
for ch.NextArg() {
if _, err := caddytls.DecodeBase64DERCert(ch.Val()); err != nil {
return nil, h.Errf("cannot decode trusted CA certificate '%s'", ch.Val())
mholt marked this conversation as resolved.
Show resolved Hide resolved
}
cp.ClientAuthentication.TrustedCACerts = append(cp.ClientAuthentication.TrustedCACerts, ch.Val())
}
case "trusted_ca_certs_file":
mholt marked this conversation as resolved.
Show resolved Hide resolved
for ch.NextArg() {
filename := ch.Val()
// read PEM file
certDataPEM, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
// safe repack PEM file
block, _ := pem.Decode(certDataPEM)
if block == nil || block.Type != "CERTIFICATE" {
return nil, h.Errf("cannot decode trusted CA certificate file'%s'", ch.Val())
}
cp.ClientAuthentication.TrustedCACerts = append(cp.ClientAuthentication.TrustedCACerts, base64.StdEncoding.EncodeToString(block.Bytes))
mholt marked this conversation as resolved.
Show resolved Hide resolved
}
case "trusted_leaf_certs":
mholt marked this conversation as resolved.
Show resolved Hide resolved
for ch.NextArg() {
if _, err := caddytls.DecodeBase64DERCert(ch.Val()); err != nil {
return nil, h.Errf("cannot decode trusted leaf certificate '%s'", ch.Val())
}
mholt marked this conversation as resolved.
Show resolved Hide resolved
cp.ClientAuthentication.TrustedLeafCerts = append(cp.ClientAuthentication.TrustedLeafCerts, ch.Val())
}
case "trusted_leaf_certs_file":
mholt marked this conversation as resolved.
Show resolved Hide resolved
for ch.NextArg() {
filename := ch.Val()
// read PEM file
certDataPEM, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
// safe repack PEM file
block, _ := pem.Decode(certDataPEM)
if block == nil || block.Type != "CERTIFICATE" {
return nil, h.Errf("cannot decode trusted leaf certificate file'%s'", ch.Val())
}
cp.ClientAuthentication.TrustedLeafCerts = append(cp.ClientAuthentication.TrustedLeafCerts, base64.StdEncoding.EncodeToString(block.Bytes))
mholt marked this conversation as resolved.
Show resolved Hide resolved
}
default:
return nil, h.Errf("Unknown param for Client Certificate Check: '%s'", ch.Val())
}
}
}

case "alpn":
args := h.RemainingArgs()
if len(args) == 0 {
Expand Down
20 changes: 20 additions & 0 deletions caddytest/caddy.ca.cer
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-----BEGIN CERTIFICATE-----
MIIDSzCCAjOgAwIBAgIUfIRObjWNUA4jxQ/0x8BOCvE2Vw4wDQYJKoZIhvcNAQEL
BQAwFjEUMBIGA1UEAwwLRWFzeS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CERTIFICATE-----
141 changes: 140 additions & 1 deletion caddytest/integration/caddyfile_adapt_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package integration

import (
"io/ioutil"
"regexp"
Expand Down Expand Up @@ -45,3 +44,143 @@ func TestCaddyfileAdaptToJSON(t *testing.T) {
}
}
}


func TestEnableClientCertificates(t *testing.T) {
caddytest.AssertAdapt(t, `
localhost {
respond "hello from localhost"
tls {
clients {
mode request
trusted_ca_certs MIIDSzCCAjOgAwIBAgIUfIRObjWNUA4jxQ/0x8BOCvE2Vw4wDQYJKoZIhvcNAQELBQAwFjEUMBIGA1UEAwwLRWFzeS1SU0EgQ0EwHhcNMTkwODI4MTYyNTU5WhcNMjkwODI1MTYyNTU5WjAWMRQwEgYDVQQDDAtFYXN5LVJTQSBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK5m5elxhQfMp/3aVJ4JnpN9PUSz6LlP6LePAPFU7gqohVVFVtDkChJAG3FNkNQNlieVTja/bgH9IcC6oKbROwdY1h0MvNV8AHHigvl03WuJD8g2ReVFXXwsnrPmKXCFzQyMI6TYk3m2gYrXsZOU1GLnfMRC3KAMRgE2F45twOs9hqG169YJ6mM2eQjzjCHWI6S2/iUYvYxRkCOlYUbLsMD/AhgAf1plzg6LPqNxtdlwxZnA0ytgkmhK67HtzJu0+ovUCsMv0RwcMhsEo9T8nyFAGt9XLZ63X5WpBCTUApaAUhnG0XnerjmUWb6eUWw4zev54sEfY5F3x002iQaW6cECAwEAAaOBkDCBjTAdBgNVHQ4EFgQU4CBUbZsS2GaNIkGRz/cBsD5ivjswUQYDVR0jBEowSIAU4CBUbZsS2GaNIkGRz/cBsD5ivjuhGqQYMBYxFDASBgNVBAMMC0Vhc3ktUlNBIENBghR8hE5uNY1QDiPFD/THwE4K8TZXDjAMBgNVHRMEBTADAQH/MAsGA1UdDwQEAwIBBjANBgkqhkiG9w0BAQsFAAOCAQEAKB3V4HIzoiO/Ch6WMj9bLJ2FGbpkMrcb/Eq01hT5zcfKD66lVS1MlK+cRL446Z2b2KDP1oFyVs+qmrmtdwrWgD+nfe2sBmmIHo9m9KygMkEOfG3MghGTEcS+0cTKEcoHYWYyOqQh6jnedXY8Cdm4GM1hAc9MiL3/sqV8YCVSLNnkoNysmr06/rZ0MCUZPGUtRmfd0heWhrfzAKw2HLgX+RAmpOE2MZqWcjvqKGyaRiaZks4nJkP6521aC2Lgp0HhCz1j8/uQ5ldoDszCnu/iro0NAsNtudTMD+YoLQxLqdleIh6CW+illc2VdXwj7mn6J04yns9jfE2jRjW/yTLFuQ==
}
}
}
`, "caddyfile", `{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":443"
],
"routes": [
{
"match": [
{
"host": [
"localhost"
]
}
],
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"body": "hello from localhost",
"handler": "static_response"
}
]
}
]
}
],
"terminal": true
}
],
"tls_connection_policies": [
{
"match": {
"sni": [
"localhost"
]
},
"client_authentication": {
"trusted_ca_certs": [
"MIIDSzCCAjOgAwIBAgIUfIRObjWNUA4jxQ/0x8BOCvE2Vw4wDQYJKoZIhvcNAQELBQAwFjEUMBIGA1UEAwwLRWFzeS1SU0EgQ0EwHhcNMTkwODI4MTYyNTU5WhcNMjkwODI1MTYyNTU5WjAWMRQwEgYDVQQDDAtFYXN5LVJTQSBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK5m5elxhQfMp/3aVJ4JnpN9PUSz6LlP6LePAPFU7gqohVVFVtDkChJAG3FNkNQNlieVTja/bgH9IcC6oKbROwdY1h0MvNV8AHHigvl03WuJD8g2ReVFXXwsnrPmKXCFzQyMI6TYk3m2gYrXsZOU1GLnfMRC3KAMRgE2F45twOs9hqG169YJ6mM2eQjzjCHWI6S2/iUYvYxRkCOlYUbLsMD/AhgAf1plzg6LPqNxtdlwxZnA0ytgkmhK67HtzJu0+ovUCsMv0RwcMhsEo9T8nyFAGt9XLZ63X5WpBCTUApaAUhnG0XnerjmUWb6eUWw4zev54sEfY5F3x002iQaW6cECAwEAAaOBkDCBjTAdBgNVHQ4EFgQU4CBUbZsS2GaNIkGRz/cBsD5ivjswUQYDVR0jBEowSIAU4CBUbZsS2GaNIkGRz/cBsD5ivjuhGqQYMBYxFDASBgNVBAMMC0Vhc3ktUlNBIENBghR8hE5uNY1QDiPFD/THwE4K8TZXDjAMBgNVHRMEBTADAQH/MAsGA1UdDwQEAwIBBjANBgkqhkiG9w0BAQsFAAOCAQEAKB3V4HIzoiO/Ch6WMj9bLJ2FGbpkMrcb/Eq01hT5zcfKD66lVS1MlK+cRL446Z2b2KDP1oFyVs+qmrmtdwrWgD+nfe2sBmmIHo9m9KygMkEOfG3MghGTEcS+0cTKEcoHYWYyOqQh6jnedXY8Cdm4GM1hAc9MiL3/sqV8YCVSLNnkoNysmr06/rZ0MCUZPGUtRmfd0heWhrfzAKw2HLgX+RAmpOE2MZqWcjvqKGyaRiaZks4nJkP6521aC2Lgp0HhCz1j8/uQ5ldoDszCnu/iro0NAsNtudTMD+YoLQxLqdleIh6CW+illc2VdXwj7mn6J04yns9jfE2jRjW/yTLFuQ=="
],
"mode": "request"
}
},
{}
]
}
}
}
}
}`)
}

func TestEnableClientCertificatesWithFile(t *testing.T) {

caddytest.AssertAdapt(t, `
localhost {
respond "hello from localhost"
tls {
clients {
mode request
trusted_ca_certs_file ../caddy.ca.cer
}
}
}
`, "caddyfile", `{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":443"
],
"routes": [
{
"match": [
{
"host": [
"localhost"
]
}
],
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"body": "hello from localhost",
"handler": "static_response"
}
]
}
]
}
],
"terminal": true
}
],
"tls_connection_policies": [
{
"match": {
"sni": [
"localhost"
]
},
"client_authentication": {
"trusted_ca_certs": [
"MIIDSzCCAjOgAwIBAgIUfIRObjWNUA4jxQ/0x8BOCvE2Vw4wDQYJKoZIhvcNAQELBQAwFjEUMBIGA1UEAw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]
}
}
}
}
}`)
}
8 changes: 4 additions & 4 deletions modules/caddytls/connpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func (clientauth *ClientAuthentication) ConfigureTLSConfig(cfg *tls.Config) erro
if len(clientauth.TrustedCACerts) > 0 {
caPool := x509.NewCertPool()
for _, clientCAString := range clientauth.TrustedCACerts {
clientCA, err := decodeBase64DERCert(clientCAString)
clientCA, err := DecodeBase64DERCert(clientCAString)
if err != nil {
return fmt.Errorf("parsing certificate: %v", err)
}
Expand All @@ -363,7 +363,7 @@ func (clientauth *ClientAuthentication) ConfigureTLSConfig(cfg *tls.Config) erro
clientauth.trustedLeafCerts = []*x509.Certificate{}

for _, clientCertString := range clientauth.TrustedLeafCerts {
clientCert, err := decodeBase64DERCert(clientCertString)
clientCert, err := DecodeBase64DERCert(clientCertString)
if err != nil {
return fmt.Errorf("parsing certificate: %v", err)
}
Expand Down Expand Up @@ -409,8 +409,8 @@ func (clientauth ClientAuthentication) verifyPeerCertificate(rawCerts [][]byte,
return fmt.Errorf("client leaf certificate failed validation")
}

// decodeBase64DERCert base64-decodes, then DER-decodes, certStr.
func decodeBase64DERCert(certStr string) (*x509.Certificate, error) {
// DecodeBase64DERCert base64-decodes, then DER-decodes, certStr.
func DecodeBase64DERCert(certStr string) (*x509.Certificate, error) {
mholt marked this conversation as resolved.
Show resolved Hide resolved
// decode base64
derBytes, err := base64.StdEncoding.DecodeString(certStr)
if err != nil {
Expand Down