|
1 | 1 | package ca
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "crypto/tls" |
| 5 | + "crypto/x509" |
| 6 | + "encoding/pem" |
| 7 | + "io/ioutil" |
4 | 8 | "time"
|
5 | 9 |
|
6 | 10 | pb "github.com/hyperledger/fabric/membersrvc/protos"
|
7 |
| - "google.golang.org/grpc" |
8 |
| - |
9 | 11 | "github.com/spf13/viper"
|
| 12 | + "google.golang.org/grpc" |
| 13 | + "google.golang.org/grpc/credentials" |
10 | 14 | )
|
11 | 15 |
|
| 16 | +/** Performs Certificate type validation **/ |
| 17 | +/* |
| 18 | +* Checks for valid Cert format type |
| 19 | +* Cert expiration |
| 20 | +* |
| 21 | + */ |
| 22 | +func isValidCertFormatted(certLocation string) bool { |
| 23 | + |
| 24 | + var isvalidCert = false |
| 25 | + certificate, err := ioutil.ReadFile(certLocation) |
| 26 | + if err != nil { |
| 27 | + return false |
| 28 | + } |
| 29 | + block, _ := pem.Decode(certificate) |
| 30 | + if block == nil { |
| 31 | + certificates, err := x509.ParseCertificates(certificate) |
| 32 | + if err != nil { |
| 33 | + caLogger.Error("Not a valid Certificate") |
| 34 | + } else { |
| 35 | + validCert := validateCert(certificates[0]) |
| 36 | + if !validCert { |
| 37 | + caLogger.Error("Certificate has expired") |
| 38 | + } |
| 39 | + return validCert |
| 40 | + } |
| 41 | + } else { |
| 42 | + certificates, err := x509.ParseCertificates(block.Bytes) |
| 43 | + if err != nil { |
| 44 | + caLogger.Error("Not a valid Certificate") |
| 45 | + } else { |
| 46 | + validCert := validateCert(certificates[0]) |
| 47 | + if !validCert { |
| 48 | + caLogger.Error("Certificate has expired") |
| 49 | + } |
| 50 | + return validCert |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return isvalidCert |
| 55 | + |
| 56 | +} |
| 57 | + |
| 58 | +/** Given the cert , it checks for expiry |
| 59 | +* Does not check for revocation |
| 60 | + */ |
| 61 | +func validateCert(cert *x509.Certificate) bool { |
| 62 | + |
| 63 | + notBefore := cert.NotBefore |
| 64 | + notAfter := cert.NotAfter |
| 65 | + |
| 66 | + currentTime := time.Now() |
| 67 | + diffFromExpiry := notAfter.Sub(currentTime) |
| 68 | + diffFromStart := currentTime.Sub(notBefore) |
| 69 | + |
| 70 | + return ((diffFromExpiry > 0) && (diffFromStart > 0)) |
| 71 | + |
| 72 | +} |
| 73 | + |
| 74 | +// NewClientTLSFromFile creates Client TLS connection credentials |
| 75 | +// @certFile : TLS Server Certificate in PEM format |
| 76 | +// @serverNameOverride : Common Name (CN) of the TLS Server Certificate |
| 77 | +// returns Secure Transport Credentials |
| 78 | +// |
| 79 | +func NewClientTLSFromFile(certFile, serverNameOverride string) (credentials.TransportCredentials, error) { |
| 80 | + caLogger.Debug("upgrading to TLS1.2") |
| 81 | + b, err := ioutil.ReadFile(certFile) |
| 82 | + |
| 83 | + if err != nil { |
| 84 | + caLogger.Errorf("Certificate could not be found in the [%s] path", certFile) |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + if !isValidCertFormatted(certFile) { |
| 89 | + return nil, nil |
| 90 | + } |
| 91 | + |
| 92 | + cp := x509.NewCertPool() |
| 93 | + |
| 94 | + ok := cp.AppendCertsFromPEM(b) |
| 95 | + if !ok { |
| 96 | + caLogger.Error("credentials: failed to append certificates: ") |
| 97 | + return nil, nil |
| 98 | + } |
| 99 | + return credentials.NewTLS(&tls.Config{ServerName: serverNameOverride, RootCAs: cp, MinVersion: 0, MaxVersion: 0}), nil |
| 100 | +} |
| 101 | + |
12 | 102 | //GetClientConn returns a connection to the server located on *address*.
|
13 | 103 | func GetClientConn(address string, serverName string) (*grpc.ClientConn, error) {
|
| 104 | + |
| 105 | + caLogger.Debug("GetACAClient: using the given gRPC client connection to return a new ACA client") |
14 | 106 | var opts []grpc.DialOption
|
15 |
| - opts = append(opts, grpc.WithInsecure()) |
| 107 | + |
| 108 | + if viper.GetBool("security.tls_enabled") { |
| 109 | + caLogger.Debug("TLS was enabled [security.tls_enabled == true]") |
| 110 | + |
| 111 | + creds, err := NewClientTLSFromFile(viper.GetString("security.client.cert.file"), viper.GetString("security.serverhostoverride")) |
| 112 | + |
| 113 | + if err != nil { |
| 114 | + caLogger.Error("Could not establish TLS client connection in GetClientConn while getting creds:") |
| 115 | + caLogger.Error(err) |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + opts = append(opts, grpc.WithTransportCredentials(creds)) |
| 119 | + } else { |
| 120 | + caLogger.Debug("TLS was not enabled [security.tls_enabled == false]") |
| 121 | + opts = append(opts, grpc.WithInsecure()) |
| 122 | + } |
16 | 123 | opts = append(opts, grpc.WithTimeout(time.Second*3))
|
17 | 124 | return grpc.Dial(address, opts...)
|
18 | 125 | }
|
19 | 126 |
|
20 | 127 | //GetACAClient returns a client to Attribute Certificate Authority.
|
21 | 128 | func GetACAClient() (*grpc.ClientConn, pb.ACAPClient, error) {
|
| 129 | + caLogger.Debug("GetACAClient: Trying to create a new ACA Client from the connection provided") |
22 | 130 | conn, err := GetClientConn(viper.GetString("aca.address"), viper.GetString("aca.server-name"))
|
23 | 131 | if err != nil {
|
24 | 132 | return nil, nil, err
|
|
0 commit comments