Skip to content

Commit

Permalink
fix: the trustcerts not add to globalCerts after ca.ResetCertificate (#…
Browse files Browse the repository at this point in the history
…1801)

support PEM format for custom-certificates too
  • Loading branch information
wwqgtxx committed Jan 20, 2025
1 parent fc23318 commit 9c73b5b
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions component/ca/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
C "github.com/metacubex/mihomo/constant"
)

var trustCerts []*x509.Certificate
var globalCertPool *x509.CertPool
var mutex sync.RWMutex
var errNotMatch = errors.New("certificate fingerprints do not match")
Expand All @@ -30,11 +29,19 @@ var DisableSystemCa, _ = strconv.ParseBool(os.Getenv("DISABLE_SYSTEM_CA"))
func AddCertificate(certificate string) error {
mutex.Lock()
defer mutex.Unlock()

if certificate == "" {
return fmt.Errorf("certificate is empty")
}
if cert, err := x509.ParseCertificate([]byte(certificate)); err == nil {
trustCerts = append(trustCerts, cert)

if globalCertPool == nil {
initializeCertPool()
}

if globalCertPool.AppendCertsFromPEM([]byte(certificate)) {
return nil
} else if cert, err := x509.ParseCertificate([]byte(certificate)); err == nil {
globalCertPool.AddCert(cert)
return nil
} else {
return fmt.Errorf("add certificate failed")
Expand All @@ -51,9 +58,6 @@ func initializeCertPool() {
globalCertPool = x509.NewCertPool()
}
}
for _, cert := range trustCerts {
globalCertPool.AddCert(cert)
}
if !DisableEmbedCa {
globalCertPool.AppendCertsFromPEM(_CaCertificates)
}
Expand All @@ -62,7 +66,6 @@ func initializeCertPool() {
func ResetCertificate() {
mutex.Lock()
defer mutex.Unlock()
trustCerts = nil
initializeCertPool()
}

Expand Down

0 comments on commit 9c73b5b

Please sign in to comment.