2020-12-04 09:32:55 -05:00
|
|
|
// +build !windows
|
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 {
|
|
|
|
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
|
|
|
}
|