Skip to content

Commit 9da872a

Browse files
Merge pull request #152 from hyperledger/fix_client_mtls
Provide the client certificate without relying on golang matching it
2 parents 7717b7b + f2d135c commit 9da872a

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

pkg/fftls/fftls.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,34 @@ func NewTLSConfig(ctx context.Context, config *Config, tlsType TLSType) (*tls.Co
8989

9090
tlsConfig.RootCAs = rootCAs
9191

92+
var configuredCert *tls.Certificate
9293
// For mTLS we need both the cert and key
9394
if config.CertFile != "" && config.KeyFile != "" {
9495
// Read the key pair to create certificate
9596
cert, err := tls.LoadX509KeyPair(config.CertFile, config.KeyFile)
9697
if err != nil {
9798
return nil, i18n.WrapError(ctx, err, i18n.MsgInvalidKeyPairFiles)
9899
}
99-
tlsConfig.Certificates = []tls.Certificate{cert}
100+
configuredCert = &cert
100101
} else if config.Cert != "" && config.Key != "" {
101102
cert, err := tls.X509KeyPair([]byte(config.Cert), []byte(config.Key))
102103
if err != nil {
103104
return nil, i18n.WrapError(ctx, err, i18n.MsgInvalidKeyPairFiles)
104105
}
105-
tlsConfig.Certificates = []tls.Certificate{cert}
106+
configuredCert = &cert
107+
}
108+
109+
if configuredCert != nil {
110+
// Rather than letting Golang pick a certificate it thinks matches from the list of one,
111+
// we directly supply it the one we have in all cases.
112+
tlsConfig.GetClientCertificate = func(_ *tls.CertificateRequestInfo) (*tls.Certificate, error) {
113+
log.L(ctx).Debugf("Supplying client certificate")
114+
return configuredCert, nil
115+
}
116+
tlsConfig.GetCertificate = func(_ *tls.ClientHelloInfo) (*tls.Certificate, error) {
117+
log.L(ctx).Debugf("Supplying server certificate")
118+
return configuredCert, nil
119+
}
106120
}
107121

108122
if tlsType == ServerType {

0 commit comments

Comments
 (0)