Files
teleport/api/utils/tlsutils/tlsutils.go
T
cf2c7059a3 Add full IP pinning enforcement (#24743)
* Add full IP pinning enforcement

We're adding IP pinning check to `authorizer.Authorize` which is used for every call,
so now all communications with teleport should enforce IP pinning.
Also making sure we always provide login IP for user certificate creation
and correct client IP propagation everywhere.

* Add integration test for App IP pinning.

* Fix wording

Co-authored-by: Tiago Silva <tiago.silva@goteleport.com>

* Wrap error

Co-authored-by: Tiago Silva <tiago.silva@goteleport.com>

* Add godocs

* Clone TLS config

* Improve proxyHeaderSigner usage

* Wider use proxyHeaderDialer and remove adhoc writing of singed header

* Add helper function TLSDial

* Use proxyHeader dialer in authConnect

* Simplify tlsConfig manipulation

Co-authored-by: Przemko Robakowski <przemko.robakowski@goteleport.com>

* Remove redundant channels processing in TLSDial

* Reduce nesting

* Update generated protobufs

* Remove ignoring of bad IP on signed PROXY header generation

* Provide logger to CheckIPPinning function

---------

Co-authored-by: Tiago Silva <tiago.silva@goteleport.com>
Co-authored-by: Przemko Robakowski <przemko.robakowski@goteleport.com>
2023-04-23 17:09:49 +00:00

95 lines
2.5 KiB
Go

/*
Copyright 2021 Gravitational, Inc.
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.
*/
// Package tlsutils contains utilities for TLS configuration and formats.
package tlsutils
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"net"
"strings"
"github.com/gravitational/trace"
)
// ParseCertificatePEM parses PEM-encoded x509 certificate.
func ParseCertificatePEM(bytes []byte) (*x509.Certificate, error) {
block, _ := pem.Decode(bytes)
if block == nil {
return nil, trace.BadParameter("expected PEM-encoded block")
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, trace.BadParameter(err.Error())
}
return cert, nil
}
// ContextDialer represents network dialer interface that uses context
type ContextDialer interface {
// DialContext is a function that dials the specified address
DialContext(ctx context.Context, network, addr string) (net.Conn, error)
}
// TLSDial dials and establishes TLS connection using custom dialer
// is similar to tls.DialWithDialer
// Note: function taken from lib/utils/tlsdial.go
func TLSDial(ctx context.Context, dialer ContextDialer, network, addr string, tlsConfig *tls.Config) (*tls.Conn, error) {
if tlsConfig == nil {
return nil, trace.BadParameter("tls config must be specified")
}
plainConn, err := dialer.DialContext(ctx, network, addr)
if err != nil {
return nil, trace.Wrap(err)
}
colonPos := strings.LastIndex(addr, ":")
if colonPos == -1 {
colonPos = len(addr)
}
hostname := addr[:colonPos]
// If no ServerName is set, infer the ServerName
// from the hostname we're connecting to.
if tlsConfig.ServerName == "" {
// Make a copy to avoid polluting argument or default.
tlsConfig = tlsConfig.Clone()
tlsConfig.ServerName = hostname
}
conn := tls.Client(plainConn, tlsConfig)
err = conn.HandshakeContext(ctx)
if err != nil {
plainConn.Close()
return nil, trace.Wrap(err)
}
if tlsConfig.InsecureSkipVerify {
return conn, nil
}
if err := conn.VerifyHostname(tlsConfig.ServerName); err != nil {
plainConn.Close()
return nil, trace.Wrap(err)
}
return conn, nil
}