@@ -41,6 +41,8 @@ import (
4141 credinternal "google.golang.org/grpc/internal/credentials"
4242)
4343
44+ type CertificateChains [][]* x509.Certificate
45+
4446// HandshakeVerificationInfo contains information about a handshake needed for
4547// verification for use when implementing the `PostHandshakeVerificationFunc`
4648// The fields in this struct are read-only.
@@ -53,7 +55,7 @@ type HandshakeVerificationInfo struct {
5355 RawCerts [][]byte
5456 // The verification chain obtained by checking peer RawCerts against the
5557 // trust certificate bundle(s), if applicable.
56- VerifiedChains [][] * x509. Certificate
58+ VerifiedChains CertificateChains
5759 // The leaf certificate sent from peer, if choosing to verify the peer
5860 // certificate(s) and that verification passed. This field would be nil if
5961 // either user chose not to verify or the verification failed.
@@ -552,7 +554,8 @@ func (c *advancedTLSCreds) ClientHandshake(ctx context.Context, authority string
552554 if cfg .ServerName == "" {
553555 cfg .ServerName = authority
554556 }
555- cfg .VerifyPeerCertificate = buildVerifyFunc (c , cfg .ServerName , rawConn )
557+ peerVerifiedChains := CertificateChains {}
558+ cfg .VerifyPeerCertificate = buildVerifyFunc (c , cfg .ServerName , rawConn , & peerVerifiedChains )
556559 conn := tls .Client (rawConn , cfg )
557560 errChannel := make (chan error , 1 )
558561 go func () {
@@ -576,12 +579,14 @@ func (c *advancedTLSCreds) ClientHandshake(ctx context.Context, authority string
576579 },
577580 }
578581 info .SPIFFEID = credinternal .SPIFFEIDFromState (conn .ConnectionState ())
582+ info .State .VerifiedChains = peerVerifiedChains
579583 return credinternal .WrapSyscallConn (rawConn , conn ), info , nil
580584}
581585
582586func (c * advancedTLSCreds ) ServerHandshake (rawConn net.Conn ) (net.Conn , credentials.AuthInfo , error ) {
583587 cfg := credinternal .CloneTLSConfig (c .config )
584- cfg .VerifyPeerCertificate = buildVerifyFunc (c , "" , rawConn )
588+ peerVerifiedChains := CertificateChains {}
589+ cfg .VerifyPeerCertificate = buildVerifyFunc (c , "" , rawConn , & peerVerifiedChains )
585590 conn := tls .Server (rawConn , cfg )
586591 if err := conn .Handshake (); err != nil {
587592 conn .Close ()
@@ -594,6 +599,7 @@ func (c *advancedTLSCreds) ServerHandshake(rawConn net.Conn) (net.Conn, credenti
594599 },
595600 }
596601 info .SPIFFEID = credinternal .SPIFFEIDFromState (conn .ConnectionState ())
602+ info .State .VerifiedChains = peerVerifiedChains
597603 return credinternal .WrapSyscallConn (rawConn , conn ), info , nil
598604}
599605
@@ -618,9 +624,15 @@ func (c *advancedTLSCreds) OverrideServerName(serverNameOverride string) error {
618624// 1. does not have a good support on root cert reloading.
619625// 2. will ignore basic certificate check when setting InsecureSkipVerify
620626// to true.
627+ //
628+ // peerVerifiedChains(output param): verified chain of certs from leaf to the
629+ // trust cert that the peer trusts.
630+ // 1. For server it is, client certs + Root ca that the server trusts
631+ // 2. For client it is, server certs + Root ca that the client trusts
621632func buildVerifyFunc (c * advancedTLSCreds ,
622633 serverName string ,
623- rawConn net.Conn ) func (rawCerts [][]byte , verifiedChains [][]* x509.Certificate ) error {
634+ rawConn net.Conn ,
635+ peerVerifiedChains * CertificateChains ) func (rawCerts [][]byte , verifiedChains [][]* x509.Certificate ) error {
624636 return func (rawCerts [][]byte , verifiedChains [][]* x509.Certificate ) error {
625637 chains := verifiedChains
626638 var leafCert * x509.Certificate
@@ -684,7 +696,7 @@ func buildVerifyFunc(c *advancedTLSCreds,
684696 if c .revocationOptions != nil {
685697 verifiedChains := chains
686698 if verifiedChains == nil {
687- verifiedChains = [][] * x509. Certificate {rawCertList }
699+ verifiedChains = CertificateChains {rawCertList }
688700 }
689701 if err := checkChainRevocation (verifiedChains , * c .revocationOptions ); err != nil {
690702 return err
@@ -698,8 +710,11 @@ func buildVerifyFunc(c *advancedTLSCreds,
698710 VerifiedChains : chains ,
699711 Leaf : leafCert ,
700712 })
701- return err
713+ if err != nil {
714+ return err
715+ }
702716 }
717+ * peerVerifiedChains = chains
703718 return nil
704719 }
705720}
0 commit comments