1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-20 12:14:15 -04:00
v2fly/transport/internet/tls/config_other.go

54 lines
902 B
Go
Raw Normal View History

2018-04-13 04:01:10 -04:00
// +build !windows
2019-02-01 14:08:21 -05:00
// +build !confonly
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()
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
}