Use custom X.509 verification logic for proxy connections (#2008)
This reduces the chance that we'll hit a failure due to a mismatched worker address, on the certificate, especially if a worker has multiple addresses. We aren't relying on IP/DNS for security anyways, so let's not have it be a hindrance.
This commit is contained in:
1 parent
631de192d3
commit
bf9e7874b1
6 files changed
+376
-67
No files matched your search
@@ -0,0 +1,69 @@
|
||||
package connect
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"fmt"
|
||||
|
||||
targetspb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/targets"
|
||||
)
|
||||
|
||||
// ClientTlsConfig creates a TLS configuration from the session authorization
|
||||
// data and host
|
||||
func ClientTlsConfig(sessionAuthzData *targetspb.SessionAuthorizationData, host string) (*tls.Config, error) {
|
||||
const op = "connect.ClientTlsConfig"
|
||||
if sessionAuthzData == nil {
|
||||
return nil, fmt.Errorf("%s: nil session authorization data", op)
|
||||
}
|
||||
parsedCert, err := x509.ParseCertificate(sessionAuthzData.Certificate)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to decode mTLS certificate: %w", err)
|
||||
}
|
||||
|
||||
certPool := x509.NewCertPool()
|
||||
certPool.AddCert(parsedCert)
|
||||
|
||||
tlsConf := &tls.Config{
|
||||
Certificates: []tls.Certificate{
|
||||
{
|
||||
Certificate: [][]byte{sessionAuthzData.Certificate},
|
||||
PrivateKey: ed25519.PrivateKey(sessionAuthzData.PrivateKey),
|
||||
Leaf: parsedCert,
|
||||
},
|
||||
},
|
||||
ServerName: host,
|
||||
MinVersion: tls.VersionTLS13,
|
||||
NextProtos: []string{"http/1.1", sessionAuthzData.SessionId},
|
||||
|
||||
// This is set this way so we can make use of VerifyConnection, which we
|
||||
// set on this TLS config below. We are not skipping verification!
|
||||
InsecureSkipVerify: true,
|
||||
}
|
||||
if host == "" {
|
||||
tlsConf.ServerName = parsedCert.DNSNames[0]
|
||||
}
|
||||
|
||||
// We disable normal DNS SAN behavior as we don't rely on DNS or IP
|
||||
// addresses for security and want to avoid issues with including localhost
|
||||
// etc.
|
||||
verifyOpts := x509.VerifyOptions{
|
||||
DNSName: sessionAuthzData.SessionId,
|
||||
Roots: certPool,
|
||||
KeyUsages: []x509.ExtKeyUsage{
|
||||
x509.ExtKeyUsageClientAuth,
|
||||
x509.ExtKeyUsageServerAuth,
|
||||
},
|
||||
}
|
||||
tlsConf.VerifyConnection = func(cs tls.ConnectionState) error {
|
||||
// Go will not run this without at least one peer certificate, but
|
||||
// doesn't hurt to check
|
||||
if len(cs.PeerCertificates) == 0 {
|
||||
return fmt.Errorf("%s: no peer certificates provided", op)
|
||||
}
|
||||
_, err := cs.PeerCertificates[0].Verify(verifyOpts)
|
||||
return err
|
||||
}
|
||||
|
||||
return tlsConf, nil
|
||||
}
|
||||
@@ -2,9 +2,7 @@ package connect
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/ed25519"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -437,13 +435,12 @@ func (c *Command) Run(args []string) (retCode int) {
|
||||
}
|
||||
}
|
||||
|
||||
parsedCert, err := x509.ParseCertificate(c.sessionAuthzData.Certificate)
|
||||
tlsConf, err := ClientTlsConfig(c.sessionAuthzData, workerHost)
|
||||
if err != nil {
|
||||
c.PrintCliError(fmt.Errorf("Unable to decode mTLS certificate: %w", err))
|
||||
return base.CommandUserError
|
||||
c.PrintCliError(fmt.Errorf("Error creating TLS configuration: %w", err))
|
||||
return base.CommandCliError
|
||||
}
|
||||
|
||||
c.expiration = parsedCert.NotAfter
|
||||
c.expiration = tlsConf.Certificates[0].Leaf.NotAfter
|
||||
|
||||
// We don't _rely_ on client-side timeout verification but this prevents us
|
||||
// seeming to be ready for a connection that will immediately fail when we
|
||||
@@ -451,23 +448,6 @@ func (c *Command) Run(args []string) (retCode int) {
|
||||
c.proxyCtx, c.proxyCancel = context.WithDeadline(c.Context, c.expiration)
|
||||
defer c.proxyCancel()
|
||||
|
||||
certPool := x509.NewCertPool()
|
||||
certPool.AddCert(parsedCert)
|
||||
|
||||
tlsConf := &tls.Config{
|
||||
Certificates: []tls.Certificate{
|
||||
{
|
||||
Certificate: [][]byte{c.sessionAuthzData.Certificate},
|
||||
PrivateKey: ed25519.PrivateKey(c.sessionAuthzData.PrivateKey),
|
||||
Leaf: parsedCert,
|
||||
},
|
||||
},
|
||||
RootCAs: certPool,
|
||||
ServerName: workerHost,
|
||||
MinVersion: tls.VersionTLS13,
|
||||
NextProtos: []string{"http/1.1", c.sessionAuthzData.SessionId},
|
||||
}
|
||||
|
||||
transport := cleanhttp.DefaultTransport()
|
||||
transport.DisableKeepAlives = false
|
||||
// This isn't/shouldn't used anyways really because the connection is
|
||||
|
||||
Reference in new issue
Block a user