mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
crypto/x509: add SystemCertPool, refactor system cert pool loading
This exports the system cert pool. The system cert loading was refactored to let it be run multiple times (so callers get a copy, and can't mutate global state), and also to not discard errors. SystemCertPool returns an error on Windows. Maybe it's fixable later, but so far we haven't used it, since the system verifies TLS. Fixes #13335 Change-Id: I3dfb4656a373f241bae8529076d24c5f532f113c Reviewed-on: https://go-review.googlesource.com/21293 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Andrew Gerrand <adg@golang.org>
This commit is contained in:
parent
71ab3c1ccf
commit
a62ae9f62f
10 changed files with 80 additions and 41 deletions
|
|
@ -6,7 +6,10 @@
|
|||
|
||||
package x509
|
||||
|
||||
import "io/ioutil"
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Possible directories with certificate files; stop after successfully
|
||||
// reading at least one file from a directory.
|
||||
|
|
@ -19,20 +22,26 @@ func (c *Certificate) systemVerify(opts *VerifyOptions) (chains [][]*Certificate
|
|||
return nil, nil
|
||||
}
|
||||
|
||||
func initSystemRoots() {
|
||||
func loadSystemRoots() (*CertPool, error) {
|
||||
roots := NewCertPool()
|
||||
var firstErr error
|
||||
for _, file := range certFiles {
|
||||
data, err := ioutil.ReadFile(file)
|
||||
if err == nil {
|
||||
roots.AppendCertsFromPEM(data)
|
||||
systemRoots = roots
|
||||
return
|
||||
return roots, nil
|
||||
}
|
||||
if firstErr == nil && !os.IsNotExist(err) {
|
||||
firstErr = err
|
||||
}
|
||||
}
|
||||
|
||||
for _, directory := range certDirectories {
|
||||
fis, err := ioutil.ReadDir(directory)
|
||||
if err != nil {
|
||||
if firstErr == nil && !os.IsNotExist(err) {
|
||||
firstErr = err
|
||||
}
|
||||
continue
|
||||
}
|
||||
rootsAdded := false
|
||||
|
|
@ -43,11 +52,9 @@ func initSystemRoots() {
|
|||
}
|
||||
}
|
||||
if rootsAdded {
|
||||
systemRoots = roots
|
||||
return
|
||||
return roots, nil
|
||||
}
|
||||
}
|
||||
|
||||
// All of the files failed to load. systemRoots will be nil which will
|
||||
// trigger a specific error at verification time.
|
||||
return nil, firstErr
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue