package credentials
import (
credinternal
)
type TLSInfo struct {
State tls.ConnectionState
CommonAuthInfo
SPIFFEID *url.URL
}
func ( TLSInfo) () string {
return "tls"
}
func ( TLSInfo) () ChannelzSecurityValue {
:= &TLSChannelzSecurityValue{
StandardName: cipherSuiteLookup[.State.CipherSuite],
}
if len(.State.PeerCertificates) > 0 {
.RemoteCertificate = .State.PeerCertificates[0].Raw
}
return
}
type tlsCreds struct {
config *tls.Config
}
func ( tlsCreds) () ProtocolInfo {
return ProtocolInfo{
SecurityProtocol: "tls",
SecurityVersion: "1.2",
ServerName: .config.ServerName,
}
}
func ( *tlsCreds) ( context.Context, string, net.Conn) ( net.Conn, AuthInfo, error) {
:= credinternal.CloneTLSConfig(.config)
if .ServerName == "" {
, , := net.SplitHostPort()
if != nil {
=
}
.ServerName =
}
:= tls.Client(, )
:= make(chan error, 1)
go func() {
<- .Handshake()
close()
}()
select {
case := <-:
if != nil {
.Close()
return nil, nil,
}
case <-.Done():
.Close()
return nil, nil, .Err()
}
:= TLSInfo{
State: .ConnectionState(),
CommonAuthInfo: CommonAuthInfo{
SecurityLevel: PrivacyAndIntegrity,
},
}
:= credinternal.SPIFFEIDFromState(.ConnectionState())
if != nil {
.SPIFFEID =
}
return credinternal.WrapSyscallConn(, ), , nil
}
func ( *tlsCreds) ( net.Conn) (net.Conn, AuthInfo, error) {
:= tls.Server(, .config)
if := .Handshake(); != nil {
.Close()
return nil, nil,
}
:= TLSInfo{
State: .ConnectionState(),
CommonAuthInfo: CommonAuthInfo{
SecurityLevel: PrivacyAndIntegrity,
},
}
:= credinternal.SPIFFEIDFromState(.ConnectionState())
if != nil {
.SPIFFEID =
}
return credinternal.WrapSyscallConn(, ), , nil
}
func ( *tlsCreds) () TransportCredentials {
return NewTLS(.config)
}
func ( *tlsCreds) ( string) error {
.config.ServerName =
return nil
}
func ( *tls.Config) TransportCredentials {
:= &tlsCreds{credinternal.CloneTLSConfig()}
.config.NextProtos = credinternal.AppendH2ToNextProtos(.config.NextProtos)
return
}
func ( *x509.CertPool, string) TransportCredentials {
return NewTLS(&tls.Config{ServerName: , RootCAs: })
}
func (, string) (TransportCredentials, error) {
, := os.ReadFile()
if != nil {
return nil,
}
:= x509.NewCertPool()
if !.AppendCertsFromPEM() {
return nil, fmt.Errorf("credentials: failed to append certificates")
}
return NewTLS(&tls.Config{ServerName: , RootCAs: }), nil
}
func ( *tls.Certificate) TransportCredentials {
return NewTLS(&tls.Config{Certificates: []tls.Certificate{*}})
}
func (, string) (TransportCredentials, error) {
, := tls.LoadX509KeyPair(, )
if != nil {
return nil,
}
return NewTLS(&tls.Config{Certificates: []tls.Certificate{}}), nil
}
type TLSChannelzSecurityValue struct {
ChannelzSecurityValue
StandardName string
LocalCertificate []byte
RemoteCertificate []byte
}
var cipherSuiteLookup = map[uint16]string{
tls.TLS_RSA_WITH_RC4_128_SHA: "TLS_RSA_WITH_RC4_128_SHA",
tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA: "TLS_RSA_WITH_3DES_EDE_CBC_SHA",
tls.TLS_RSA_WITH_AES_128_CBC_SHA: "TLS_RSA_WITH_AES_128_CBC_SHA",
tls.TLS_RSA_WITH_AES_256_CBC_SHA: "TLS_RSA_WITH_AES_256_CBC_SHA",
tls.TLS_RSA_WITH_AES_128_GCM_SHA256: "TLS_RSA_WITH_AES_128_GCM_SHA256",
tls.TLS_RSA_WITH_AES_256_GCM_SHA384: "TLS_RSA_WITH_AES_256_GCM_SHA384",
tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA: "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA",
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA: "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA: "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA: "TLS_ECDHE_RSA_WITH_RC4_128_SHA",
tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA: "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA",
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA: "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA: "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256: "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384: "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384: "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
tls.TLS_FALLBACK_SCSV: "TLS_FALLBACK_SCSV",
tls.TLS_RSA_WITH_AES_128_CBC_SHA256: "TLS_RSA_WITH_AES_128_CBC_SHA256",
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256: "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256: "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305: "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305",
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305: "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305",
tls.TLS_AES_128_GCM_SHA256: "TLS_AES_128_GCM_SHA256",
tls.TLS_AES_256_GCM_SHA384: "TLS_AES_256_GCM_SHA384",
tls.TLS_CHACHA20_POLY1305_SHA256: "TLS_CHACHA20_POLY1305_SHA256",
}