2021-08-21 01:20:40 -04:00
|
|
|
//go:build !windows && !confonly
|
|
|
|
// +build !windows,!confonly
|
2018-04-13 04:01:10 -04:00
|
|
|
|
|
|
|
package tls
|
|
|
|
|
2018-08-09 07:30:29 -04:00
|
|
|
import (
|
|
|
|
"crypto/x509"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2019-02-26 15:58:54 -05:00
|
|
|
type rootCertsCache struct {
|
2018-08-09 07:30:29 -04:00
|
|
|
sync.Mutex
|
2019-02-26 15:58:54 -05:00
|
|
|
pool *x509.CertPool
|
2018-08-09 07:30:29 -04:00
|
|
|
}
|
|
|
|
|
2019-02-26 15:58:54 -05:00
|
|
|
func (c *rootCertsCache) load() (*x509.CertPool, error) {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
if c.pool != nil {
|
|
|
|
return c.pool, nil
|
2018-08-09 07:30:29 -04:00
|
|
|
}
|
2019-02-26 15:58:54 -05:00
|
|
|
|
|
|
|
pool, err := x509.SystemCertPool()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c.pool = pool
|
|
|
|
return pool, nil
|
2018-08-09 07:30:29 -04:00
|
|
|
}
|
|
|
|
|
2019-02-26 15:58:54 -05:00
|
|
|
var rootCerts rootCertsCache
|
2018-08-09 07:30:29 -04:00
|
|
|
|
2019-02-26 15:58:54 -05:00
|
|
|
func (c *Config) getCertPool() (*x509.CertPool, error) {
|
|
|
|
if c.DisableSystemRoot {
|
|
|
|
return c.loadSelfCertPool()
|
2018-04-13 04:01:10 -04:00
|
|
|
}
|
2018-08-09 07:30:29 -04:00
|
|
|
|
2019-02-26 15:58:54 -05:00
|
|
|
if len(c.Certificate) == 0 {
|
|
|
|
return rootCerts.load()
|
2018-08-09 07:30:29 -04:00
|
|
|
}
|
|
|
|
|
2019-02-26 15:58:54 -05:00
|
|
|
pool, err := x509.SystemCertPool()
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("system root").AtWarning().Base(err)
|
|
|
|
}
|
|
|
|
for _, cert := range c.Certificate {
|
2021-09-01 17:34:13 -04:00
|
|
|
/* Do not treat client certificate authority as a peer certificate authority.
|
|
|
|
This is designed to prevent a client certificate with a permissive key usage from being used to attacker server.
|
|
|
|
In next release, the certificate usage will be enforced strictly.
|
|
|
|
Only a certificate with AUTHORITY_VERIFY usage will be accepted.
|
|
|
|
*/
|
|
|
|
if cert.Usage == Certificate_AUTHORITY_VERIFY_CLIENT {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-02-26 15:58:54 -05:00
|
|
|
if !pool.AppendCertsFromPEM(cert.Certificate) {
|
|
|
|
return nil, newError("append cert to root").AtWarning().Base(err)
|
2018-04-13 04:01:10 -04:00
|
|
|
}
|
|
|
|
}
|
2019-02-26 15:58:54 -05:00
|
|
|
return pool, err
|
2018-04-13 04:01:10 -04:00
|
|
|
}
|