Skip to content

Commit

Permalink
Merge pull request #152 from hyperledger/fix_client_mtls
Browse files Browse the repository at this point in the history
Provide the client certificate without relying on golang matching it
  • Loading branch information
peterbroadhurst authored Oct 14, 2024
2 parents 7717b7b + f2d135c commit 9da872a
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions pkg/fftls/fftls.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,34 @@ func NewTLSConfig(ctx context.Context, config *Config, tlsType TLSType) (*tls.Co

tlsConfig.RootCAs = rootCAs

var configuredCert *tls.Certificate
// For mTLS we need both the cert and key
if config.CertFile != "" && config.KeyFile != "" {
// Read the key pair to create certificate
cert, err := tls.LoadX509KeyPair(config.CertFile, config.KeyFile)
if err != nil {
return nil, i18n.WrapError(ctx, err, i18n.MsgInvalidKeyPairFiles)
}
tlsConfig.Certificates = []tls.Certificate{cert}
configuredCert = &cert
} else if config.Cert != "" && config.Key != "" {
cert, err := tls.X509KeyPair([]byte(config.Cert), []byte(config.Key))
if err != nil {
return nil, i18n.WrapError(ctx, err, i18n.MsgInvalidKeyPairFiles)
}
tlsConfig.Certificates = []tls.Certificate{cert}
configuredCert = &cert
}

if configuredCert != nil {
// Rather than letting Golang pick a certificate it thinks matches from the list of one,
// we directly supply it the one we have in all cases.
tlsConfig.GetClientCertificate = func(_ *tls.CertificateRequestInfo) (*tls.Certificate, error) {
log.L(ctx).Debugf("Supplying client certificate")
return configuredCert, nil
}
tlsConfig.GetCertificate = func(_ *tls.ClientHelloInfo) (*tls.Certificate, error) {
log.L(ctx).Debugf("Supplying server certificate")
return configuredCert, nil
}
}

if tlsType == ServerType {
Expand Down

0 comments on commit 9da872a

Please sign in to comment.