Skip to content

Commit a25d0d8

Browse files
committed
crypto/x509: cache the result of SystemCertPool
Fixes #24540 Change-Id: I65e9f2f99403e22d25ea64cc26701bf62a31d070 Reviewed-on: https://go-review.googlesource.com/102699 Run-TryBot: Filippo Valsorda <filippo@golang.org> Reviewed-by: Adam Langley <agl@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
1 parent ad0ebc3 commit a25d0d8

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

src/crypto/x509/cert_pool.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,43 @@ func NewCertPool() *CertPool {
2525
}
2626
}
2727

28+
func (s *CertPool) copy() *CertPool {
29+
p := &CertPool{
30+
bySubjectKeyId: make(map[string][]int, len(s.bySubjectKeyId)),
31+
byName: make(map[string][]int, len(s.byName)),
32+
certs: make([]*Certificate, len(s.certs)),
33+
}
34+
for k, v := range s.bySubjectKeyId {
35+
indexes := make([]int, len(v))
36+
copy(indexes, v)
37+
p.bySubjectKeyId[k] = indexes
38+
}
39+
for k, v := range s.byName {
40+
indexes := make([]int, len(v))
41+
copy(indexes, v)
42+
p.byName[k] = indexes
43+
}
44+
copy(p.certs, s.certs)
45+
return p
46+
}
47+
2848
// SystemCertPool returns a copy of the system cert pool.
2949
//
3050
// Any mutations to the returned pool are not written to disk and do
3151
// not affect any other pool.
52+
//
53+
// New changes in the the system cert pool might not be reflected
54+
// in subsequent calls.
3255
func SystemCertPool() (*CertPool, error) {
3356
if runtime.GOOS == "windows" {
3457
// Issue 16736, 18609:
3558
return nil, errors.New("crypto/x509: system root pool is not available on Windows")
3659
}
3760

61+
if sysRoots := systemRootsPool(); sysRoots != nil {
62+
return sysRoots.copy(), nil
63+
}
64+
3865
return loadSystemRoots()
3966
}
4067

src/crypto/x509/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ func systemRootsPool() *CertPool {
1919

2020
func initSystemRoots() {
2121
systemRoots, systemRootsErr = loadSystemRoots()
22+
if systemRootsErr != nil {
23+
systemRoots = nil
24+
}
2225
}

src/crypto/x509/x509_test.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1656,10 +1656,46 @@ func TestSystemCertPool(t *testing.T) {
16561656
if runtime.GOOS == "windows" {
16571657
t.Skip("not implemented on Windows; Issue 16736, 18609")
16581658
}
1659-
_, err := SystemCertPool()
1659+
if runtime.GOOS == "nacl" {
1660+
t.Skip("not implemented on NaCl; Issue 24561")
1661+
}
1662+
a, err := SystemCertPool()
16601663
if err != nil {
16611664
t.Fatal(err)
16621665
}
1666+
b, err := SystemCertPool()
1667+
if err != nil {
1668+
t.Fatal(err)
1669+
}
1670+
if !reflect.DeepEqual(a, b) {
1671+
t.Fatal("two calls to SystemCertPool had different results")
1672+
}
1673+
if ok := b.AppendCertsFromPEM([]byte(`
1674+
-----BEGIN CERTIFICATE-----
1675+
MIIDBjCCAe6gAwIBAgIRANXM5I3gjuqDfTp/PYrs+u8wDQYJKoZIhvcNAQELBQAw
1676+
EjEQMA4GA1UEChMHQWNtZSBDbzAeFw0xODAzMjcxOTU2MjFaFw0xOTAzMjcxOTU2
1677+
MjFaMBIxEDAOBgNVBAoTB0FjbWUgQ28wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAw
1678+
ggEKAoIBAQDK+9m3rjsO2Djes6bIYQZ3eV29JF09ZrjOrEHLtaKrD6/acsoSoTsf
1679+
cQr+rzzztdB5ijWXCS64zo/0OiqBeZUNZ67jVdToa9qW5UYe2H0Y+ZNdfA5GYMFD
1680+
yk/l3/uBu3suTZPfXiW2TjEi27Q8ruNUIZ54DpTcs6y2rBRFzadPWwn/VQMlvRXM
1681+
jrzl8Y08dgnYmaAHprxVzwMXcQ/Brol+v9GvjaH1DooHqkn8O178wsPQNhdtvN01
1682+
IXL46cYdcUwWrE/GX5u+9DaSi+0KWxAPQ+NVD5qUI0CKl4714yGGh7feXMjJdHgl
1683+
VG4QJZlJvC4FsURgCHJT6uHGIelnSwhbAgMBAAGjVzBVMA4GA1UdDwEB/wQEAwIF
1684+
oDATBgNVHSUEDDAKBggrBgEFBQcDATAMBgNVHRMBAf8EAjAAMCAGA1UdEQQZMBeC
1685+
FVRlc3RTeXN0ZW1DZXJ0UG9vbC5nbzANBgkqhkiG9w0BAQsFAAOCAQEAwuSRx/VR
1686+
BKh2ICxZjL6jBwk/7UlU1XKbhQD96RqkidDNGEc6eLZ90Z5XXTurEsXqdm5jQYPs
1687+
1cdcSW+fOSMl7MfW9e5tM66FaIPZl9rKZ1r7GkOfgn93xdLAWe8XHd19xRfDreub
1688+
YC8DVqgLASOEYFupVSl76ktPfxkU5KCvmUf3P2PrRybk1qLGFytGxfyice2gHSNI
1689+
gify3K/+H/7wCkyFW4xYvzl7WW4mXxoqPRPjQt1J423DhnnQ4G1P8V/vhUpXNXOq
1690+
N9IEPnWuihC09cyx/WMQIUlWnaQLHdfpPS04Iez3yy2PdfXJzwfPrja7rNE+skK6
1691+
pa/O1nF0AfWOpw==
1692+
-----END CERTIFICATE-----
1693+
`)); !ok {
1694+
t.Fatal("AppendCertsFromPEM failed")
1695+
}
1696+
if reflect.DeepEqual(a, b) {
1697+
t.Fatal("changing one pool modified the other")
1698+
}
16631699
}
16641700

16651701
const emptyNameConstraintsPEM = `

0 commit comments

Comments
 (0)