Skip to content

Commit

Permalink
enables identification of root certificate if subject and issuer iden…
Browse files Browse the repository at this point in the history
…tical in spite of defined authority key id. (#30)
  • Loading branch information
jonhadfield committed May 20, 2023
1 parent fe0279e commit b85f701
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
20 changes: 20 additions & 0 deletions pkg/cert/cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ func Test_expiryFormat(t *testing.T) {
})
}

func Test_rootIdentification(t *testing.T) {
t.Run("given certificate issuer is identical to subject but authority key id is set then identify as root", func(t *testing.T) {
certificate, err := FromBytes(loadTestFile(t, "root_with_authority_key_id.pem"))
require.NoError(t, err)
require.Len(t, certificate, 1)
require.Equal(t, certificate[0].x509Certificate.RawSubject, certificate[0].x509Certificate.RawIssuer)
require.NotEmpty(t, certificate[0].x509Certificate.AuthorityKeyId)
require.Equal(t, "root", CertificateType(certificate[0].x509Certificate))
})

t.Run("given certificate authority key id is unset then identify as root", func(t *testing.T) {
certificate, err := FromBytes(loadTestFile(t, "cert.pem"))
require.NoError(t, err)
require.Len(t, certificate, 1)
assert.Len(t, certificate[0].x509Certificate.AuthorityKeyId, 0)
assert.True(t, certificate[0].x509Certificate.IsCA)
require.Equal(t, "root", CertificateType(certificate[0].x509Certificate))
})
}

// --- helper functions ---

func loadTestCertificates(t *testing.T, file string) Certificates {
Expand Down
23 changes: 23 additions & 0 deletions pkg/cert/testdata/root_with_authority_key_id.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-----BEGIN CERTIFICATE-----
MIIDxTCCAq2gAwIBAgIQAqxcJmoLQJuPC3nyrkYldzANBgkqhkiG9w0BAQUFADBs
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBIaWdoIEFzc3VyYW5j
ZSBFViBSb290IENBMB4XDTA2MTExMDAwMDAwMFo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IJ9VNep
+OkuE6N36B9K
-----END CERTIFICATE-----
4 changes: 3 additions & 1 deletion pkg/cert/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cert

import (
"bytes"
"crypto/x509"
"time"
)
Expand Down Expand Up @@ -45,9 +46,10 @@ func ValidityFormat(t time.Time) string {

func CertificateType(cert *x509.Certificate) string {

if cert.AuthorityKeyId == nil {
if bytes.Equal(cert.RawSubject, cert.RawIssuer) || cert.AuthorityKeyId == nil {
return "root"
}

if cert.IsCA {
return "intermediate"
}
Expand Down

0 comments on commit b85f701

Please sign in to comment.