github.com/Hnampk/my-fabric@v0.0.0-20201028083322-75069da399c0/gossip/comm/crypto.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package comm
     8  
     9  import (
    10  	"context"
    11  
    12  	"github.com/hyperledger/fabric/common/util"
    13  	"google.golang.org/grpc/credentials"
    14  	"google.golang.org/grpc/peer"
    15  )
    16  
    17  func certHashFromRawCert(rawCert []byte) []byte {
    18  	if len(rawCert) == 0 {
    19  		return nil
    20  	}
    21  	return util.ComputeSHA256(rawCert)
    22  }
    23  
    24  // ExtractCertificateHash extracts the hash of the certificate from the stream
    25  func extractCertificateHashFromContext(ctx context.Context) []byte {
    26  	pr, extracted := peer.FromContext(ctx)
    27  	if !extracted {
    28  		return nil
    29  	}
    30  
    31  	authInfo := pr.AuthInfo
    32  	if authInfo == nil {
    33  		return nil
    34  	}
    35  
    36  	tlsInfo, isTLSConn := authInfo.(credentials.TLSInfo)
    37  	if !isTLSConn {
    38  		return nil
    39  	}
    40  	certs := tlsInfo.State.PeerCertificates
    41  	if len(certs) == 0 {
    42  		return nil
    43  	}
    44  	raw := certs[0].Raw
    45  	return certHashFromRawCert(raw)
    46  }