* * Copyright 2014 gRPC 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. *

package credentials

import (
	
	
	
	
	
	
	

	credinternal 
)
TLSInfo contains the auth information for a TLS authenticated connection. It implements the AuthInfo interface.
This API is experimental.
AuthType returns the type of TLSInfo as a string.
func ( TLSInfo) () string {
	return "tls"
}
GetSecurityValue returns security info requested by channelz.
Currently there's no way to get LocalCertificate info from tls package.
tlsCreds is the credentials required for authenticating a connection using TLS.
TLS configuration
use local cfg to avoid clobbering ServerName if using multiple endpoints
	 := credinternal.CloneTLSConfig(.config)
	if .ServerName == "" {
		, ,  := net.SplitHostPort()
If the authority had no host port or if the authority cannot be parsed, use it as-is.
			 = 
		}
		.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
}
NewTLS uses c to construct a TransportCredentials based on TLS.
NewClientTLSFromCert constructs TLS credentials from the provided root certificate authority certificate(s) to validate server connections. If certificates to establish the identity of the client need to be included in the credentials (eg: for mTLS), use NewTLS instead, where a complete tls.Config can be specified. serverNameOverride is for testing only. If set to a non empty string, it will override the virtual host name of authority (e.g. :authority header field) in requests.
func ( *x509.CertPool,  string) TransportCredentials {
	return NewTLS(&tls.Config{ServerName: , RootCAs: })
}
NewClientTLSFromFile constructs TLS credentials from the provided root certificate authority certificate file(s) to validate server connections. If certificates to establish the identity of the client need to be included in the credentials (eg: for mTLS), use NewTLS instead, where a complete tls.Config can be specified. serverNameOverride is for testing only. If set to a non empty string, it will override the virtual host name of authority (e.g. :authority header field) in requests.
func (,  string) (TransportCredentials, error) {
	,  := ioutil.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
}
NewServerTLSFromCert constructs TLS credentials from the input certificate for server.
NewServerTLSFromFile constructs TLS credentials from the input certificate file and key file for server.
func (,  string) (TransportCredentials, error) {
	,  := tls.LoadX509KeyPair(, )
	if  != nil {
		return nil, 
	}
	return NewTLS(&tls.Config{Certificates: []tls.Certificate{}}), nil
}
TLSChannelzSecurityValue defines the struct that TLS protocol should return from GetSecurityValue(), containing security info like cipher and certificate used. This API is EXPERIMENTAL.
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",