config_other.go 882 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // +build !windows
  2. package tls
  3. import (
  4. "crypto/x509"
  5. "sync"
  6. )
  7. type rootCertsCache struct {
  8. sync.Mutex
  9. pool *x509.CertPool
  10. }
  11. func (c *rootCertsCache) load() (*x509.CertPool, error) {
  12. c.Lock()
  13. defer c.Unlock()
  14. if c.pool != nil {
  15. return c.pool, nil
  16. }
  17. pool, err := x509.SystemCertPool()
  18. if err != nil {
  19. return nil, err
  20. }
  21. c.pool = pool
  22. return pool, nil
  23. }
  24. var rootCerts rootCertsCache
  25. func (c *Config) getCertPool() (*x509.CertPool, error) {
  26. if c.DisableSystemRoot {
  27. return c.loadSelfCertPool()
  28. }
  29. if len(c.Certificate) == 0 {
  30. return rootCerts.load()
  31. }
  32. pool, err := x509.SystemCertPool()
  33. if err != nil {
  34. return nil, newError("system root").AtWarning().Base(err)
  35. }
  36. for _, cert := range c.Certificate {
  37. if !pool.AppendCertsFromPEM(cert.Certificate) {
  38. return nil, newError("append cert to root").AtWarning().Base(err)
  39. }
  40. }
  41. return pool, err
  42. }