1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-10 06:16:53 -05:00
v2fly/transport/internet/tls/config_other.go

54 lines
930 B
Go
Raw Normal View History

2021-10-28 06:34:19 -04:00
//go:build !windows
// +build !windows
2018-04-13 04:01:10 -04:00
package tls
2018-08-09 07:30:29 -04:00
import (
"crypto/x509"
"sync"
)
type rootCertsCache struct {
2018-08-09 07:30:29 -04:00
sync.Mutex
pool *x509.CertPool
2018-08-09 07:30:29 -04: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
}
pool, err := x509.SystemCertPool()
if err != nil {
return nil, err
}
c.pool = pool
return pool, nil
2018-08-09 07:30:29 -04:00
}
var rootCerts rootCertsCache
2018-08-09 07:30:29 -04:00
func (c *Config) getCertPool() (*x509.CertPool, error) {
if c.DisableSystemRoot {
return c.loadSelfCertPool(Certificate_AUTHORITY_VERIFY)
2018-04-13 04:01:10 -04:00
}
2018-08-09 07:30:29 -04:00
if len(c.Certificate) == 0 {
return rootCerts.load()
2018-08-09 07:30:29 -04: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
}
}
return pool, err
2018-04-13 04:01:10 -04:00
}