Skip to content

Commit 52ea2b0

Browse files
committed
Fix TLS Client Certificate Verify Not Applied
1 parent c93ad2f commit 52ea2b0

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

transport/internet/tls/config.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ func ParseCertificate(c *cert.Certificate) *Certificate {
3030
return nil
3131
}
3232

33-
func (c *Config) loadSelfCertPool() (*x509.CertPool, error) {
33+
func (c *Config) loadSelfCertPool(usage Certificate_Usage) (*x509.CertPool, error) {
3434
root := x509.NewCertPool()
3535
for _, cert := range c.Certificate {
36-
if !root.AppendCertsFromPEM(cert.Certificate) {
37-
return nil, newError("failed to append cert").AtWarning()
36+
if cert.Usage == usage {
37+
if !root.AppendCertsFromPEM(cert.Certificate) {
38+
return nil, newError("failed to append cert").AtWarning()
39+
}
3840
}
3941
}
4042
return root, nil
@@ -209,13 +211,19 @@ func (c *Config) GetTLSConfig(opts ...Option) *tls.Config {
209211
}
210212
}
211213

214+
clientRoot, err := c.loadSelfCertPool(Certificate_AUTHORITY_VERIFY_CLIENT)
215+
if err != nil {
216+
newError("failed to load client root certificate").AtError().Base(err).WriteToLog()
217+
}
218+
212219
config := &tls.Config{
213220
ClientSessionCache: globalSessionCache,
214221
RootCAs: root,
215222
InsecureSkipVerify: c.AllowInsecure,
216223
NextProtos: c.NextProtocol,
217224
SessionTicketsDisabled: !c.EnableSessionResumption,
218225
VerifyPeerCertificate: c.verifyPeerCert,
226+
ClientCAs: clientRoot,
219227
}
220228

221229
for _, opt := range opts {
@@ -238,6 +246,9 @@ func (c *Config) GetTLSConfig(opts ...Option) *tls.Config {
238246
config.NextProtos = []string{"h2", "http/1.1"}
239247
}
240248

249+
if c.VerifyClientCertificate {
250+
config.ClientAuth = tls.RequireAndVerifyClientCert
251+
}
241252
return config
242253
}
243254

transport/internet/tls/config_other.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var rootCerts rootCertsCache
3333

3434
func (c *Config) getCertPool() (*x509.CertPool, error) {
3535
if c.DisableSystemRoot {
36-
return c.loadSelfCertPool()
36+
return c.loadSelfCertPool(Certificate_AUTHORITY_VERIFY)
3737
}
3838

3939
if len(c.Certificate) == 0 {

transport/internet/tls/config_windows.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import "crypto/x509"
77

88
func (c *Config) getCertPool() (*x509.CertPool, error) {
99
if c.DisableSystemRoot {
10-
return c.loadSelfCertPool()
10+
return c.loadSelfCertPool(Certificate_AUTHORITY_VERIFY)
1111
}
1212

1313
return nil, nil

0 commit comments

Comments
 (0)