2021-09-20 13:00:55 +00:00
|
|
|
//go:build !windows
|
2020-11-25 11:01:53 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package tls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/x509"
|
|
|
|
"sync"
|
2024-06-29 18:32:57 +00:00
|
|
|
|
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2020-11-25 11:01:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type rootCertsCache struct {
|
|
|
|
sync.Mutex
|
|
|
|
pool *x509.CertPool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rootCertsCache) load() (*x509.CertPool, error) {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
if c.pool != nil {
|
|
|
|
return c.pool, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pool, err := x509.SystemCertPool()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c.pool = pool
|
|
|
|
return pool, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var rootCerts rootCertsCache
|
|
|
|
|
|
|
|
func (c *Config) getCertPool() (*x509.CertPool, error) {
|
|
|
|
if c.DisableSystemRoot {
|
|
|
|
return c.loadSelfCertPool()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.Certificate) == 0 {
|
|
|
|
return rootCerts.load()
|
|
|
|
}
|
|
|
|
|
|
|
|
pool, err := x509.SystemCertPool()
|
|
|
|
if err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
return nil, errors.New("system root").AtWarning().Base(err)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
for _, cert := range c.Certificate {
|
|
|
|
if !pool.AppendCertsFromPEM(cert.Certificate) {
|
2024-06-29 18:32:57 +00:00
|
|
|
return nil, errors.New("append cert to root").AtWarning().Base(err)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return pool, err
|
|
|
|
}
|