Skip to content

Commit

Permalink
Extract all subjects from SANs for x509 verifier
Browse files Browse the repository at this point in the history
Validation is also handled during certificate creation, so the contents
of the x509 cert should follow rfc5280.

Signed-off-by: Hayden Blauzvern <[email protected]>
  • Loading branch information
haydentherapper committed Aug 11, 2023
1 parent 6357794 commit ea3a02e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
20 changes: 3 additions & 17 deletions pkg/pki/x509/x509.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,31 +195,17 @@ func (k PublicKey) EmailAddresses() []string {

// Subjects implements the pki.PublicKey interface
func (k PublicKey) Subjects() []string {
var names []string
var subjects []string
var cert *x509.Certificate
if k.cert != nil {
cert = k.cert.c
} else if len(k.certs) > 0 {
cert = k.certs[0]
}
if cert != nil {
validate := validator.New()
for _, name := range cert.EmailAddresses {
if errs := validate.Var(name, "required,email"); errs == nil {
names = append(names, strings.ToLower(name))
}
}
for _, name := range cert.URIs {
if errs := validate.Var(name.String(), "required,uri"); errs == nil {
names = append(names, strings.ToLower(name.String()))
}
}
otherName, _ := cryptoutils.UnmarshalOtherNameSAN(cert.Extensions)
if len(otherName) > 0 {
names = append(names, otherName)
}
subjects = cryptoutils.GetSubjectAlternateNames(cert)
}
return names
return subjects
}

// Identities implements the pki.PublicKey interface
Expand Down
11 changes: 8 additions & 3 deletions pkg/pki/x509/x509_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"crypto/ed25519"
"crypto/rsa"
"crypto/x509"
"net"
"net/url"
"reflect"
"strings"
Expand Down Expand Up @@ -249,8 +250,9 @@ func TestSignature_VerifyFail(t *testing.T) {
func TestPublicKeyWithCertChain(t *testing.T) {
rootCert, rootKey, _ := testutils.GenerateRootCa()
subCert, subKey, _ := testutils.GenerateSubordinateCa(rootCert, rootKey)
url, _ := url.Parse("https://github.com/slsa-framework/slsa-github-generator/.github/workflows/builder_go_slsa3.yml@refs/tags/v1.1.1")
leafCert, leafKey, _ := testutils.GenerateLeafCert("[email protected]", "oidc-issuer", url, subCert, subKey)
subjectUrl, _ := url.Parse("https://github.com/slsa-framework/slsa-github-generator/.github/workflows/builder_go_slsa3.yml@refs/tags/v1.1.1")

Check warning on line 253 in pkg/pki/x509/x509_test.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var subjectUrl should be subjectURL (revive)
leafCert, leafKey, _ := testutils.GenerateLeafCertWithSubjectAlternateNames(
[]string{"example.com"}, []string{"[email protected]"}, []net.IP{{1, 1, 1, 1}}, []*url.URL{subjectUrl}, "oidc-issuer", subCert, subKey)
leafCertPEM, _ := cryptoutils.MarshalCertificateToPEM(leafCert)

pemCertChain, err := cryptoutils.MarshalCertificatesToPEM([]*x509.Certificate{leafCert, subCert, rootCert})
Expand All @@ -274,7 +276,10 @@ func TestPublicKeyWithCertChain(t *testing.T) {
t.Fatalf("expected matching subjects, expected %v, got %v", leafCert.EmailAddresses, pub.EmailAddresses())
}

expectedSubjects := leafCert.EmailAddresses
var expectedSubjects []string
expectedSubjects = append(expectedSubjects, leafCert.DNSNames...)
expectedSubjects = append(expectedSubjects, leafCert.EmailAddresses...)
expectedSubjects = append(expectedSubjects, leafCert.IPAddresses[0].String())
expectedSubjects = append(expectedSubjects, leafCert.URIs[0].String())
if !reflect.DeepEqual(pub.Subjects(), expectedSubjects) {
t.Fatalf("expected matching subjects, expected %v, got %v", expectedSubjects, pub.Subjects())
Expand Down

0 comments on commit ea3a02e

Please sign in to comment.