-
Notifications
You must be signed in to change notification settings - Fork 17.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
crypto/x509: implement SetFallbackRoots
Adds a method which allows users to set a fallback certificate pool for usage during verification if the system certificate pool is empty. Updates #43958 Change-Id: I279dd2f753743bce19790f2ae29f063c89c9359d Reviewed-on: https://go-review.googlesource.com/c/go/+/449235 Run-TryBot: Roland Shoemaker <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Roland Shoemaker <[email protected]> Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]>
- Loading branch information
1 parent
c824448
commit 04d6aa6
Showing
4 changed files
with
161 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pkg crypto/x509, func SetFallbackRoots(*CertPool) #43958 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright 2022 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package x509 | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestFallbackPanic(t *testing.T) { | ||
defer func() { | ||
if recover() == nil { | ||
t.Fatal("Multiple calls to SetFallbackRoots should panic") | ||
} | ||
}() | ||
SetFallbackRoots(nil) | ||
SetFallbackRoots(nil) | ||
} | ||
|
||
func TestFallback(t *testing.T) { | ||
// call systemRootsPool so that the sync.Once is triggered, and we can | ||
// manipulate systemRoots without worrying about our working being overwritten | ||
systemRootsPool() | ||
if systemRoots != nil { | ||
originalSystemRoots := *systemRoots | ||
defer func() { systemRoots = &originalSystemRoots }() | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
systemRoots *CertPool | ||
systemPool bool | ||
poolContent []*Certificate | ||
forceFallback bool | ||
returnsFallback bool | ||
}{ | ||
{ | ||
name: "nil systemRoots", | ||
returnsFallback: true, | ||
}, | ||
{ | ||
name: "empty systemRoots", | ||
systemRoots: NewCertPool(), | ||
returnsFallback: true, | ||
}, | ||
{ | ||
name: "empty systemRoots system pool", | ||
systemRoots: NewCertPool(), | ||
systemPool: true, | ||
}, | ||
{ | ||
name: "filled systemRoots system pool", | ||
systemRoots: NewCertPool(), | ||
poolContent: []*Certificate{{}}, | ||
systemPool: true, | ||
}, | ||
{ | ||
name: "filled systemRoots", | ||
systemRoots: NewCertPool(), | ||
poolContent: []*Certificate{{}}, | ||
}, | ||
{ | ||
name: "filled systemRoots, force fallback", | ||
systemRoots: NewCertPool(), | ||
poolContent: []*Certificate{{}}, | ||
forceFallback: true, | ||
returnsFallback: true, | ||
}, | ||
{ | ||
name: "filled systemRoot system pool, force fallback", | ||
systemRoots: NewCertPool(), | ||
poolContent: []*Certificate{{}}, | ||
systemPool: true, | ||
forceFallback: true, | ||
returnsFallback: true, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
fallbacksSet = false | ||
systemRoots = tc.systemRoots | ||
if systemRoots != nil { | ||
systemRoots.systemPool = tc.systemPool | ||
} | ||
for _, c := range tc.poolContent { | ||
systemRoots.AddCert(c) | ||
} | ||
if tc.forceFallback { | ||
t.Setenv("GODEBUG", "x509usefallbackroots=1") | ||
} else { | ||
t.Setenv("GODEBUG", "x509usefallbackroots=0") | ||
} | ||
|
||
fallbackPool := NewCertPool() | ||
SetFallbackRoots(fallbackPool) | ||
|
||
systemPoolIsFallback := systemRoots == fallbackPool | ||
|
||
if tc.returnsFallback && !systemPoolIsFallback { | ||
t.Error("systemRoots was not set to fallback pool") | ||
} else if !tc.returnsFallback && systemPoolIsFallback { | ||
t.Error("systemRoots was set to fallback pool when it shouldn't have been") | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters