1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-02 17:27:50 -04:00
v2fly/transport/internet/tls/config_other.go

63 lines
1.3 KiB
Go
Raw Normal View History

//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"
)
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 {
/* 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
}
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
}