2019-07-01 01:07:58 +03:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-04-25 22:54:48 +03:00
|
|
|
package caddytls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2019-05-24 22:18:45 +03:00
|
|
|
"crypto/x509"
|
2019-09-03 18:35:36 +03:00
|
|
|
"encoding/base64"
|
2019-04-25 22:54:48 +03:00
|
|
|
"fmt"
|
2019-05-24 22:18:45 +03:00
|
|
|
"strings"
|
2019-04-25 22:54:48 +03:00
|
|
|
|
2019-07-02 21:37:06 +03:00
|
|
|
"github.com/caddyserver/caddy/v2"
|
2020-07-31 00:18:14 +03:00
|
|
|
"github.com/mholt/acmez"
|
2019-04-25 22:54:48 +03:00
|
|
|
)
|
|
|
|
|
2020-07-31 00:18:14 +03:00
|
|
|
// ConnectionPolicies govern the establishment of TLS connections. It is
|
|
|
|
// an ordered group of connection policies; the first matching policy will
|
|
|
|
// be used to configure TLS connections at handshake-time.
|
2019-04-25 22:54:48 +03:00
|
|
|
type ConnectionPolicies []*ConnectionPolicy
|
|
|
|
|
2020-03-07 09:15:25 +03:00
|
|
|
// Provision sets up each connection policy. It should be called
|
|
|
|
// during the Validate() phase, after the TLS app (if any) is
|
|
|
|
// already set up.
|
|
|
|
func (cp ConnectionPolicies) Provision(ctx caddy.Context) error {
|
2019-04-25 22:54:48 +03:00
|
|
|
for i, pol := range cp {
|
2019-05-27 20:31:47 +03:00
|
|
|
// matchers
|
2019-12-10 23:36:46 +03:00
|
|
|
mods, err := ctx.LoadModule(pol, "MatchersRaw")
|
|
|
|
if err != nil {
|
2020-03-07 09:15:25 +03:00
|
|
|
return fmt.Errorf("loading handshake matchers: %v", err)
|
2019-12-10 23:36:46 +03:00
|
|
|
}
|
|
|
|
for _, modIface := range mods.(map[string]interface{}) {
|
|
|
|
cp[i].matchers = append(cp[i].matchers, modIface.(ConnectionMatcher))
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
2019-05-27 20:31:47 +03:00
|
|
|
|
2020-03-14 04:14:49 +03:00
|
|
|
// enable HTTP/2 by default
|
|
|
|
if len(pol.ALPN) == 0 {
|
|
|
|
pol.ALPN = append(pol.ALPN, defaultALPN...)
|
|
|
|
}
|
|
|
|
|
2020-03-07 09:15:25 +03:00
|
|
|
// pre-build standard TLS config so we don't have to at handshake-time
|
|
|
|
err = pol.buildStandardTLSConfig(ctx)
|
2019-04-25 22:54:48 +03:00
|
|
|
if err != nil {
|
2020-03-07 09:15:25 +03:00
|
|
|
return fmt.Errorf("connection policy %d: building standard TLS config: %s", i, err)
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-07 09:15:25 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TLSConfig returns a standard-lib-compatible TLS configuration which
|
|
|
|
// selects the first matching policy based on the ClientHello.
|
|
|
|
func (cp ConnectionPolicies) TLSConfig(ctx caddy.Context) *tls.Config {
|
2019-05-24 22:18:45 +03:00
|
|
|
// using ServerName to match policies is extremely common, especially in configs
|
|
|
|
// with lots and lots of different policies; we can fast-track those by indexing
|
|
|
|
// them by SNI, so we don't have to iterate potentially thousands of policies
|
2020-03-21 00:51:37 +03:00
|
|
|
// (TODO: this map does not account for wildcards, see if this is a problem in practice?)
|
2019-05-24 22:18:45 +03:00
|
|
|
indexedBySNI := make(map[string]ConnectionPolicies)
|
|
|
|
if len(cp) > 30 {
|
|
|
|
for _, p := range cp {
|
|
|
|
for _, m := range p.matchers {
|
|
|
|
if sni, ok := m.(MatchServerName); ok {
|
|
|
|
for _, sniName := range sni {
|
|
|
|
indexedBySNI[sniName] = append(indexedBySNI[sniName], p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:54:48 +03:00
|
|
|
return &tls.Config{
|
2020-11-23 00:50:29 +03:00
|
|
|
MinVersion: tls.VersionTLS12,
|
2019-04-25 22:54:48 +03:00
|
|
|
GetConfigForClient: func(hello *tls.ClientHelloInfo) (*tls.Config, error) {
|
2019-05-24 22:18:45 +03:00
|
|
|
// filter policies by SNI first, if possible, to speed things up
|
|
|
|
// when there may be lots of policies
|
|
|
|
possiblePolicies := cp
|
|
|
|
if indexedPolicies, ok := indexedBySNI[hello.ServerName]; ok {
|
|
|
|
possiblePolicies = indexedPolicies
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:54:48 +03:00
|
|
|
policyLoop:
|
2019-05-24 22:18:45 +03:00
|
|
|
for _, pol := range possiblePolicies {
|
|
|
|
for _, matcher := range pol.matchers {
|
2019-04-25 22:54:48 +03:00
|
|
|
if !matcher.Match(hello) {
|
|
|
|
continue policyLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pol.stdTLSConfig, nil
|
|
|
|
}
|
2019-06-21 05:36:29 +03:00
|
|
|
|
2019-04-25 22:54:48 +03:00
|
|
|
return nil, fmt.Errorf("no server TLS configuration available for ClientHello: %+v", hello)
|
|
|
|
},
|
2020-03-07 09:15:25 +03:00
|
|
|
}
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectionPolicy specifies the logic for handling a TLS handshake.
|
2019-12-29 09:56:08 +03:00
|
|
|
// An empty policy is valid; safe and sensible defaults will be used.
|
2019-04-25 22:54:48 +03:00
|
|
|
type ConnectionPolicy struct {
|
2019-12-10 23:36:46 +03:00
|
|
|
// How to match this policy with a TLS ClientHello. If
|
|
|
|
// this policy is the first to match, it will be used.
|
|
|
|
MatchersRaw caddy.ModuleMap `json:"match,omitempty" caddy:"namespace=tls.handshake_match"`
|
|
|
|
|
|
|
|
// How to choose a certificate if more than one matched
|
|
|
|
// the given ServerName (SNI) value.
|
caddytls: Refactor certificate selection policies (close #1575)
Certificate selection used to be a module, but this seems unnecessary,
especially since the built-in CustomSelectionPolicy allows quite complex
selection logic on a number of fields in certs. If we need to extend
that logic, we can, but I don't think there are SO many possibilities
that we need modules.
This update also allows certificate selection to choose between multiple
matching certs based on client compatibility and makes a number of other
improvements in the default cert selection logic, both here and in the
latest CertMagic.
The hardest part of this was the conn policy consolidation logic
(Caddyfile only, of course). We have to merge connection policies that
we can easily combine, because if two certs are manually loaded in a
Caddyfile site block, that produces two connection policies, and each
cert is tagged with a different tag, meaning only the first would ever
be selected. So given the same matchers, we can merge the two, but this
required improving the Tag selection logic to support multiple tags to
choose from, hence "tags" changed to "any_tag" or "all_tags" (but we
use any_tag in our Caddyfile logic).
Combining conn policies with conflicting settings is impossible, so
that should return an error if two policies with the exact same matchers
have non-empty settings that are not the same (the one exception being
any_tag which we can merge because the logic for them is to OR them).
It was a bit complicated. It seems to work in numerous tests I've
conducted, but we'll see how it pans out in the release candidates.
2020-04-02 05:49:35 +03:00
|
|
|
CertSelection *CustomCertSelectionPolicy `json:"certificate_selection,omitempty"`
|
2019-12-10 23:36:46 +03:00
|
|
|
|
|
|
|
// The list of cipher suites to support. Caddy's
|
|
|
|
// defaults are modern and secure.
|
|
|
|
CipherSuites []string `json:"cipher_suites,omitempty"`
|
|
|
|
|
|
|
|
// The list of elliptic curves to support. Caddy's
|
|
|
|
// defaults are modern and secure.
|
|
|
|
Curves []string `json:"curves,omitempty"`
|
|
|
|
|
|
|
|
// Protocols to use for Application-Layer Protocol
|
|
|
|
// Negotiation (ALPN) during the handshake.
|
|
|
|
ALPN []string `json:"alpn,omitempty"`
|
|
|
|
|
|
|
|
// Minimum TLS protocol version to allow. Default: `tls1.2`
|
|
|
|
ProtocolMin string `json:"protocol_min,omitempty"`
|
|
|
|
|
|
|
|
// Maximum TLS protocol version to allow. Default: `tls1.3`
|
|
|
|
ProtocolMax string `json:"protocol_max,omitempty"`
|
|
|
|
|
|
|
|
// Enables and configures TLS client authentication.
|
2019-09-03 18:35:36 +03:00
|
|
|
ClientAuthentication *ClientAuthentication `json:"client_authentication,omitempty"`
|
2019-04-25 22:54:48 +03:00
|
|
|
|
2020-03-07 09:15:25 +03:00
|
|
|
// DefaultSNI becomes the ServerName in a ClientHello if there
|
|
|
|
// is no policy configured for the empty SNI value.
|
|
|
|
DefaultSNI string `json:"default_sni,omitempty"`
|
|
|
|
|
2019-05-24 22:18:45 +03:00
|
|
|
matchers []ConnectionMatcher
|
2019-04-25 22:54:48 +03:00
|
|
|
stdTLSConfig *tls.Config
|
|
|
|
}
|
|
|
|
|
2019-06-14 20:58:28 +03:00
|
|
|
func (p *ConnectionPolicy) buildStandardTLSConfig(ctx caddy.Context) error {
|
2019-05-17 01:05:38 +03:00
|
|
|
tlsAppIface, err := ctx.App("tls")
|
2019-04-29 18:22:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting tls app: %v", err)
|
|
|
|
}
|
|
|
|
tlsApp := tlsAppIface.(*TLS)
|
2019-04-25 22:54:48 +03:00
|
|
|
|
2019-07-01 20:47:46 +03:00
|
|
|
// fill in some "easy" default values, but for other values
|
|
|
|
// (such as slices), we should ensure that they start empty
|
|
|
|
// so the user-provided config can fill them in; then we will
|
|
|
|
// fill in a default config at the end if they are still unset
|
2019-04-25 22:54:48 +03:00
|
|
|
cfg := &tls.Config{
|
2019-05-24 22:18:45 +03:00
|
|
|
NextProtos: p.ALPN,
|
2019-04-25 22:54:48 +03:00
|
|
|
PreferServerCipherSuites: true,
|
|
|
|
GetCertificate: func(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
2020-03-07 09:15:25 +03:00
|
|
|
// TODO: I don't love how this works: we pre-build certmagic configs
|
|
|
|
// so that handshakes are faster. Unfortunately, certmagic configs are
|
|
|
|
// comprised of settings from both a TLS connection policy and a TLS
|
2020-03-18 06:00:45 +03:00
|
|
|
// automation policy. The only two fields (as of March 2020; v2 beta 17)
|
2020-03-07 09:15:25 +03:00
|
|
|
// of a certmagic config that come from the TLS connection policy are
|
|
|
|
// CertSelection and DefaultServerName, so an automation policy is what
|
|
|
|
// builds the base certmagic config. Since the pre-built config is
|
|
|
|
// shared, I don't think we can change any of its fields per-handshake,
|
|
|
|
// hence the awkward shallow copy (dereference) here and the subsequent
|
|
|
|
// changing of some of its fields. I'm worried this dereference allocates
|
|
|
|
// more at handshake-time, but I don't know how to practically pre-build
|
|
|
|
// a certmagic config for each combination of conn policy + automation policy...
|
|
|
|
cfg := *tlsApp.getConfigForName(hello.ServerName)
|
2020-04-02 06:07:38 +03:00
|
|
|
if p.CertSelection != nil {
|
|
|
|
// you would think we could just set this whether or not
|
|
|
|
// p.CertSelection is nil, but that leads to panics if
|
|
|
|
// it is, because cfg.CertSelection is an interface,
|
|
|
|
// so it will have a non-nil value even if the actual
|
|
|
|
// value underlying it is nil (sigh)
|
|
|
|
cfg.CertSelection = p.CertSelection
|
|
|
|
}
|
2020-03-07 09:15:25 +03:00
|
|
|
cfg.DefaultServerName = p.DefaultSNI
|
|
|
|
return cfg.GetCertificate(hello)
|
2019-04-25 22:54:48 +03:00
|
|
|
},
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
MaxVersion: tls.VersionTLS13,
|
|
|
|
}
|
|
|
|
|
2019-05-30 08:11:46 +03:00
|
|
|
// session tickets support
|
2019-09-30 18:07:43 +03:00
|
|
|
if tlsApp.SessionTickets != nil {
|
|
|
|
cfg.SessionTicketsDisabled = tlsApp.SessionTickets.Disabled
|
|
|
|
|
|
|
|
// session ticket key rotation
|
|
|
|
tlsApp.SessionTickets.register(cfg)
|
|
|
|
ctx.OnCancel(func() {
|
2020-01-03 21:33:22 +03:00
|
|
|
// do cleanup when the context is canceled because,
|
2019-09-30 18:07:43 +03:00
|
|
|
// though unlikely, it is possible that a context
|
|
|
|
// needing a TLS server config could exist for less
|
|
|
|
// than the lifetime of the whole app
|
|
|
|
tlsApp.SessionTickets.unregister(cfg)
|
|
|
|
})
|
|
|
|
}
|
2019-05-30 08:11:46 +03:00
|
|
|
|
2019-09-03 18:35:36 +03:00
|
|
|
// TODO: Clean up session ticket active locks in storage if app (or process) is being closed!
|
2019-06-21 05:36:29 +03:00
|
|
|
|
2019-04-25 22:54:48 +03:00
|
|
|
// add all the cipher suites in order, without duplicates
|
|
|
|
cipherSuitesAdded := make(map[uint16]struct{})
|
2019-05-24 22:18:45 +03:00
|
|
|
for _, csName := range p.CipherSuites {
|
2020-04-01 23:09:29 +03:00
|
|
|
csID := CipherSuiteID(csName)
|
|
|
|
if csID == 0 {
|
|
|
|
return fmt.Errorf("unsupported cipher suite: %s", csName)
|
|
|
|
}
|
2019-04-25 22:54:48 +03:00
|
|
|
if _, ok := cipherSuitesAdded[csID]; !ok {
|
|
|
|
cipherSuitesAdded[csID] = struct{}{}
|
|
|
|
cfg.CipherSuites = append(cfg.CipherSuites, csID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add all the curve preferences in order, without duplicates
|
|
|
|
curvesAdded := make(map[tls.CurveID]struct{})
|
2019-05-24 22:18:45 +03:00
|
|
|
for _, curveName := range p.Curves {
|
2019-08-09 21:05:47 +03:00
|
|
|
curveID := SupportedCurves[curveName]
|
2019-04-25 22:54:48 +03:00
|
|
|
if _, ok := curvesAdded[curveID]; !ok {
|
|
|
|
curvesAdded[curveID] = struct{}{}
|
|
|
|
cfg.CurvePreferences = append(cfg.CurvePreferences, curveID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure ALPN includes the ACME TLS-ALPN protocol
|
|
|
|
var alpnFound bool
|
2019-05-24 22:18:45 +03:00
|
|
|
for _, a := range p.ALPN {
|
2020-07-31 00:18:14 +03:00
|
|
|
if a == acmez.ACMETLS1Protocol {
|
2019-04-25 22:54:48 +03:00
|
|
|
alpnFound = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !alpnFound {
|
2020-07-31 00:18:14 +03:00
|
|
|
cfg.NextProtos = append(cfg.NextProtos, acmez.ACMETLS1Protocol)
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// min and max protocol versions
|
2020-02-03 19:25:32 +03:00
|
|
|
if (p.ProtocolMin != "" && p.ProtocolMax != "") && p.ProtocolMin > p.ProtocolMax {
|
|
|
|
return fmt.Errorf("protocol min (%x) cannot be greater than protocol max (%x)", p.ProtocolMin, p.ProtocolMax)
|
|
|
|
}
|
2019-05-24 22:18:45 +03:00
|
|
|
if p.ProtocolMin != "" {
|
2019-08-09 21:05:47 +03:00
|
|
|
cfg.MinVersion = SupportedProtocols[p.ProtocolMin]
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
2019-05-24 22:18:45 +03:00
|
|
|
if p.ProtocolMax != "" {
|
2019-08-09 21:05:47 +03:00
|
|
|
cfg.MaxVersion = SupportedProtocols[p.ProtocolMax]
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
2019-09-03 18:35:36 +03:00
|
|
|
// client authentication
|
|
|
|
if p.ClientAuthentication != nil {
|
|
|
|
err := p.ClientAuthentication.ConfigureTLSConfig(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("configuring TLS client authentication: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-01 20:47:46 +03:00
|
|
|
setDefaultTLSParams(cfg)
|
|
|
|
|
2019-05-24 22:18:45 +03:00
|
|
|
p.stdTLSConfig = cfg
|
2019-04-25 22:54:48 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:37:52 +03:00
|
|
|
// SettingsEmpty returns true if p's settings (fields
|
|
|
|
// except the matchers) are all empty/unset.
|
|
|
|
func (p ConnectionPolicy) SettingsEmpty() bool {
|
|
|
|
return p.CertSelection == nil &&
|
|
|
|
p.CipherSuites == nil &&
|
|
|
|
p.Curves == nil &&
|
|
|
|
p.ALPN == nil &&
|
|
|
|
p.ProtocolMin == "" &&
|
|
|
|
p.ProtocolMax == "" &&
|
|
|
|
p.ClientAuthentication == nil &&
|
|
|
|
p.DefaultSNI == ""
|
|
|
|
}
|
|
|
|
|
2019-09-03 18:35:36 +03:00
|
|
|
// ClientAuthentication configures TLS client auth.
|
|
|
|
type ClientAuthentication struct {
|
|
|
|
// A list of base64 DER-encoded CA certificates
|
|
|
|
// against which to validate client certificates.
|
|
|
|
// Client certs which are not signed by any of
|
|
|
|
// these CAs will be rejected.
|
|
|
|
TrustedCACerts []string `json:"trusted_ca_certs,omitempty"`
|
|
|
|
|
|
|
|
// A list of base64 DER-encoded client leaf certs
|
|
|
|
// to accept. If this list is not empty, client certs
|
|
|
|
// which are not in this list will be rejected.
|
|
|
|
TrustedLeafCerts []string `json:"trusted_leaf_certs,omitempty"`
|
|
|
|
|
2020-03-08 18:48:25 +03:00
|
|
|
// The mode for authenticating the client. Allowed values are:
|
|
|
|
//
|
|
|
|
// Mode | Description
|
|
|
|
// -----|---------------
|
|
|
|
// `request` | Ask clients for a certificate, but allow even if there isn't one; do not verify it
|
|
|
|
// `require` | Require clients to present a certificate, but do not verify it
|
|
|
|
// `verify_if_given` | Ask clients for a certificate; allow even if there isn't one, but verify it if there is
|
|
|
|
// `require_and_verify` | Require clients to present a valid certificate that is verified
|
|
|
|
//
|
|
|
|
// The default mode is `require_and_verify` if any
|
|
|
|
// TrustedCACerts or TrustedLeafCerts are provided;
|
|
|
|
// otherwise, the default mode is `require`.
|
|
|
|
Mode string `json:"mode,omitempty"`
|
|
|
|
|
2019-09-03 18:35:36 +03:00
|
|
|
// state established with the last call to ConfigureTLSConfig
|
|
|
|
trustedLeafCerts []*x509.Certificate
|
|
|
|
existingVerifyPeerCert func([][]byte, [][]*x509.Certificate) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Active returns true if clientauth has an actionable configuration.
|
|
|
|
func (clientauth ClientAuthentication) Active() bool {
|
2020-03-08 18:48:25 +03:00
|
|
|
return len(clientauth.TrustedCACerts) > 0 || len(clientauth.TrustedLeafCerts) > 0 || len(clientauth.Mode) > 0
|
2019-09-03 18:35:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigureTLSConfig sets up cfg to enforce clientauth's configuration.
|
|
|
|
func (clientauth *ClientAuthentication) ConfigureTLSConfig(cfg *tls.Config) error {
|
|
|
|
// if there's no actionable client auth, simply disable it
|
|
|
|
if !clientauth.Active() {
|
|
|
|
cfg.ClientAuth = tls.NoClientCert
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-08 18:48:25 +03:00
|
|
|
// enforce desired mode of client authentication
|
|
|
|
if len(clientauth.Mode) > 0 {
|
|
|
|
switch clientauth.Mode {
|
|
|
|
case "request":
|
|
|
|
cfg.ClientAuth = tls.RequestClientCert
|
|
|
|
case "require":
|
|
|
|
cfg.ClientAuth = tls.RequireAnyClientCert
|
|
|
|
case "verify_if_given":
|
|
|
|
cfg.ClientAuth = tls.VerifyClientCertIfGiven
|
|
|
|
case "require_and_verify":
|
|
|
|
cfg.ClientAuth = tls.RequireAndVerifyClientCert
|
|
|
|
default:
|
2020-06-05 21:19:36 +03:00
|
|
|
return fmt.Errorf("client auth mode not recognized: %s", clientauth.Mode)
|
2020-03-08 18:48:25 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// otherwise, set a safe default mode
|
|
|
|
if len(clientauth.TrustedCACerts) > 0 || len(clientauth.TrustedLeafCerts) > 0 {
|
|
|
|
cfg.ClientAuth = tls.RequireAndVerifyClientCert
|
|
|
|
} else {
|
|
|
|
cfg.ClientAuth = tls.RequireAnyClientCert
|
|
|
|
}
|
|
|
|
}
|
2019-09-03 18:35:36 +03:00
|
|
|
|
|
|
|
// enforce CA verification by adding CA certs to the ClientCAs pool
|
|
|
|
if len(clientauth.TrustedCACerts) > 0 {
|
|
|
|
caPool := x509.NewCertPool()
|
|
|
|
for _, clientCAString := range clientauth.TrustedCACerts {
|
|
|
|
clientCA, err := decodeBase64DERCert(clientCAString)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing certificate: %v", err)
|
|
|
|
}
|
|
|
|
caPool.AddCert(clientCA)
|
|
|
|
}
|
|
|
|
cfg.ClientCAs = caPool
|
|
|
|
}
|
|
|
|
|
|
|
|
// enforce leaf verification by writing our own verify function
|
|
|
|
if len(clientauth.TrustedLeafCerts) > 0 {
|
|
|
|
clientauth.trustedLeafCerts = []*x509.Certificate{}
|
|
|
|
for _, clientCertString := range clientauth.TrustedLeafCerts {
|
|
|
|
clientCert, err := decodeBase64DERCert(clientCertString)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing certificate: %v", err)
|
|
|
|
}
|
|
|
|
clientauth.trustedLeafCerts = append(clientauth.trustedLeafCerts, clientCert)
|
|
|
|
}
|
|
|
|
// if a custom verification function already exists, wrap it
|
|
|
|
clientauth.existingVerifyPeerCert = cfg.VerifyPeerCertificate
|
|
|
|
cfg.VerifyPeerCertificate = clientauth.verifyPeerCertificate
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// verifyPeerCertificate is for use as a tls.Config.VerifyPeerCertificate
|
|
|
|
// callback to do custom client certificate verification. It is intended
|
|
|
|
// for installation only by clientauth.ConfigureTLSConfig().
|
|
|
|
func (clientauth ClientAuthentication) verifyPeerCertificate(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
|
|
|
|
// first use any pre-existing custom verification function
|
|
|
|
if clientauth.existingVerifyPeerCert != nil {
|
|
|
|
err := clientauth.existingVerifyPeerCert(rawCerts, verifiedChains)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rawCerts) == 0 {
|
|
|
|
return fmt.Errorf("no client certificate provided")
|
|
|
|
}
|
|
|
|
|
2020-05-06 19:07:13 +03:00
|
|
|
remoteLeafCert, err := x509.ParseCertificate(rawCerts[0])
|
2019-09-03 18:35:36 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't parse the given certificate: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, trustedLeafCert := range clientauth.trustedLeafCerts {
|
|
|
|
if remoteLeafCert.Equal(trustedLeafCert) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("client leaf certificate failed validation")
|
|
|
|
}
|
|
|
|
|
|
|
|
// decodeBase64DERCert base64-decodes, then DER-decodes, certStr.
|
|
|
|
func decodeBase64DERCert(certStr string) (*x509.Certificate, error) {
|
|
|
|
derBytes, err := base64.StdEncoding.DecodeString(certStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return x509.ParseCertificate(derBytes)
|
|
|
|
}
|
|
|
|
|
2019-07-01 20:47:46 +03:00
|
|
|
// setDefaultTLSParams sets the default TLS cipher suites, protocol versions,
|
|
|
|
// and server preferences of cfg if they are not already set; it does not
|
|
|
|
// overwrite values, only fills in missing values.
|
|
|
|
func setDefaultTLSParams(cfg *tls.Config) {
|
|
|
|
if len(cfg.CipherSuites) == 0 {
|
|
|
|
cfg.CipherSuites = getOptimalDefaultCipherSuites()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not a cipher suite, but still important for mitigating protocol downgrade attacks
|
|
|
|
// (prepend since having it at end breaks http2 due to non-h2-approved suites before it)
|
|
|
|
cfg.CipherSuites = append([]uint16{tls.TLS_FALLBACK_SCSV}, cfg.CipherSuites...)
|
|
|
|
|
|
|
|
if len(cfg.CurvePreferences) == 0 {
|
|
|
|
cfg.CurvePreferences = defaultCurves
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.MinVersion == 0 {
|
|
|
|
cfg.MinVersion = tls.VersionTLS12
|
|
|
|
}
|
|
|
|
if cfg.MaxVersion == 0 {
|
|
|
|
cfg.MaxVersion = tls.VersionTLS13
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.PreferServerCipherSuites = true
|
|
|
|
}
|
|
|
|
|
2019-05-27 20:31:47 +03:00
|
|
|
// PublicKeyAlgorithm is a JSON-unmarshalable wrapper type.
|
|
|
|
type PublicKeyAlgorithm x509.PublicKeyAlgorithm
|
2019-05-24 22:18:45 +03:00
|
|
|
|
|
|
|
// UnmarshalJSON satisfies json.Unmarshaler.
|
2019-05-27 20:31:47 +03:00
|
|
|
func (a *PublicKeyAlgorithm) UnmarshalJSON(b []byte) error {
|
2019-05-24 22:18:45 +03:00
|
|
|
algoStr := strings.ToLower(strings.Trim(string(b), `"`))
|
|
|
|
algo, ok := publicKeyAlgorithms[algoStr]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unrecognized public key algorithm: %s (expected one of %v)",
|
|
|
|
algoStr, publicKeyAlgorithms)
|
|
|
|
}
|
2019-05-27 20:31:47 +03:00
|
|
|
*a = PublicKeyAlgorithm(algo)
|
2019-05-24 22:18:45 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:54:48 +03:00
|
|
|
// ConnectionMatcher is a type which matches TLS handshakes.
|
|
|
|
type ConnectionMatcher interface {
|
|
|
|
Match(*tls.ClientHelloInfo) bool
|
|
|
|
}
|
2020-03-14 04:14:49 +03:00
|
|
|
|
|
|
|
var defaultALPN = []string{"h2", "http/1.1"}
|