Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cert_pool): add AppendCertsFromFile func #71233

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/crypto/x509/cert_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"crypto/sha256"
"encoding/pem"
"os"
"sync"
)

Expand Down Expand Up @@ -210,6 +211,19 @@ func (s *CertPool) addCertFunc(rawSum224 sum224, rawSubject string, getCert func
s.byName[rawSubject] = append(s.byName[rawSubject], len(s.lazyCerts)-1)
}

// AppendCertsFromFile attempts to parse a series of File certificates.
// It appends any certificates found to s and reports whether any certificates
// were successfully parsed.
// If an error occurs while reading the file, an error will be returned.
func (s *CertPool) AppendCertsFromFile(path string) (bool, error) {
file, err := os.ReadFile(path)
if err != nil {
return false, err
}

return s.AppendCertsFromPEM(file), nil
}

// AppendCertsFromPEM attempts to parse a series of PEM encoded certificates.
// It appends any certificates found to s and reports whether any certificates
// were successfully parsed.
Expand Down
3 changes: 1 addition & 2 deletions src/crypto/x509/root_plan9.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ func loadSystemRoots() (*CertPool, error) {
roots := NewCertPool()
var bestErr error
for _, file := range certFiles {
data, err := os.ReadFile(file)
_, err := roots.AppendCertsFromFile(file)
if err == nil {
roots.AppendCertsFromPEM(data)
return roots, nil
}
if bestErr == nil || (os.IsNotExist(bestErr) && !os.IsNotExist(err)) {
Expand Down
8 changes: 2 additions & 6 deletions src/crypto/x509/root_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ func loadSystemRoots() (*CertPool, error) {

var firstErr error
for _, file := range files {
data, err := os.ReadFile(file)
_, err := roots.AppendCertsFromFile(file)
if err == nil {
roots.AppendCertsFromPEM(data)
break
}
if firstErr == nil && !os.IsNotExist(err) {
Expand All @@ -67,10 +66,7 @@ func loadSystemRoots() (*CertPool, error) {
continue
}
for _, fi := range fis {
data, err := os.ReadFile(directory + "/" + fi.Name())
if err == nil {
roots.AppendCertsFromPEM(data)
}
_, err := roots.AppendCertsFromFile(directory + "/" + fi.Name())
}
}

Expand Down