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

Fix NPE with length-1 cert chains #61

Merged
merged 1 commit into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (cred X509Credential) Verify(trusted []*x509.Certificate) error {
// If no previous certificate has been signed under a trusted certificate,
// then the last certificate in the chain must be signed by a trusted
// certificate
last := next
last := cred.Chain[len(cred.Chain)-1]
parent, ok := pool.parent(last)
if !ok {
return fmt.Errorf("No candidate trust anchor found")
Expand Down
16 changes: 11 additions & 5 deletions credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ func makeCertChain(t *testing.T, rootPriv crypto.Signer, depth int, addSKI bool)
return sigPriv, rootCert, chain
}

func makeX509Credential(t *testing.T, addSKI bool) (*Credential, *x509.Certificate) {
func makeX509Credential(t *testing.T, depth int, addSKI bool) (*Credential, *x509.Certificate) {
rootPriv := newEd25519(t)
leafPriv, rootCert, chain := makeCertChain(t, rootPriv, 3, addSKI)
leafPriv, rootCert, chain := makeCertChain(t, rootPriv, depth, addSKI)

cred, err := NewX509Credential(chain, leafPriv)
require.Nil(t, err)
Expand Down Expand Up @@ -125,7 +125,7 @@ func TestBasicCredential(t *testing.T) {
}

func TestX509Credential(t *testing.T) {
cred, _ := makeX509Credential(t, true)
cred, _ := makeX509Credential(t, 3, true)

require.NotNil(t, cred)
require.True(t, cred.Equals(*cred))
Expand All @@ -141,14 +141,20 @@ func TestX509Credential(t *testing.T) {
require.Nil(t, err)
}

func TestX509CredentialOne(t *testing.T) {
cred, root := makeX509Credential(t, 1, false)
trusted := []*x509.Certificate{root}
require.Nil(t, cred.X509.Verify(trusted))
}

func TestX509CredentialVerifyByName(t *testing.T) {
cred, root := makeX509Credential(t, false)
cred, root := makeX509Credential(t, 3, false)
trusted := []*x509.Certificate{root}
require.Nil(t, cred.X509.Verify(trusted))
}

func TestX509CredentialVerifyBySKI(t *testing.T) {
cred, root := makeX509Credential(t, true)
cred, root := makeX509Credential(t, 3, true)
trusted := []*x509.Certificate{root}
require.Nil(t, cred.X509.Verify(trusted))
}
Expand Down