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: self-signed leaf certificate is not self-issued #236

Merged
merged 15 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion revocation/ocsp/ocsp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func TestCheckStatusForNonSelfSignedSingleCert(t *testing.T) {
}

certResults, err := CheckStatus(opts)
expectedErr := result.InvalidChainError{Err: errors.New("invalid self-signed certificate. subject: \"CN=Notation Test RSA Leaf Cert,O=Notary,L=Seattle,ST=WA,C=US\". Error: crypto/rsa: verification error")}
expectedErr := result.InvalidChainError{Err: errors.New("invalid self-signed leaf certificate. subject: \"CN=Notation Test RSA Leaf Cert,O=Notary,L=Seattle,ST=WA,C=US\". Error: crypto/rsa: verification error")}
if err == nil || err.Error() != expectedErr.Error() {
t.Errorf("Expected CheckStatus to fail with %v, but got: %v", expectedErr, err)
}
Expand Down
13 changes: 12 additions & 1 deletion revocation/revocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,9 @@ func TestCRL(t *testing.T) {
}

revocationClient, err := NewWithOptions(Options{
OCSPHTTPClient: &http.Client{},
OCSPHTTPClient: &http.Client{
JeyJeyGao marked this conversation as resolved.
Show resolved Hide resolved
Transport: &serverErrorTransport{},
},
CRLFetcher: fetcher,
CertChainPurpose: purpose.CodeSigning,
})
Expand Down Expand Up @@ -1352,6 +1354,15 @@ func (t panicTransport) RoundTrip(req *http.Request) (*http.Response, error) {
panic("panic")
}

type serverErrorTransport struct{}

func (t serverErrorTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusInternalServerError,
Body: io.NopCloser(bytes.NewReader([]byte{})),
}, nil
}

func TestValidateContext(t *testing.T) {
r, err := NewWithOptions(Options{
OCSPHTTPClient: &http.Client{},
Expand Down
6 changes: 3 additions & 3 deletions x509/codesigning_cert_validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
// For self-signed signing certificate (not a CA)
if len(certChain) == 1 {
cert := certChain[0]
if err := validateSelfSignedLeaf(cert); err != nil {
return err
}

Check warning on line 39 in x509/codesigning_cert_validations.go

View check run for this annotation

Codecov / codecov/patch

x509/codesigning_cert_validations.go#L38-L39

Added lines #L38 - L39 were not covered by tests
if signedTimeError := validateSigningTime(cert, signingTime); signedTimeError != nil {
return signedTimeError
}
if err := cert.CheckSignature(cert.SignatureAlgorithm, cert.RawTBSCertificate, cert.Signature); err != nil {
return fmt.Errorf("invalid self-signed certificate. subject: %q. Error: %w", cert.Subject, err)
}
if err := validateCodeSigningLeafCertificate(cert); err != nil {
return fmt.Errorf("invalid self-signed certificate. Error: %w", err)
}
Expand Down
13 changes: 13 additions & 0 deletions x509/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@
"github.com/notaryproject/notation-core-go/signature"
)

// validateSelfSignedLeaf validates a self-signed leaf certificate.
//
// A self-signed leaf certificate must have the same subject and issuer.
func validateSelfSignedLeaf(cert *x509.Certificate) error {
Two-Hearts marked this conversation as resolved.
Show resolved Hide resolved
if err := cert.CheckSignature(cert.SignatureAlgorithm, cert.RawTBSCertificate, cert.Signature); err != nil {
return fmt.Errorf("invalid self-signed leaf certificate. subject: %q. Error: %w", cert.Subject, err)
}
if !bytes.Equal(cert.RawSubject, cert.RawIssuer) {
return fmt.Errorf("invalid self-signed leaf certificate. subject: %q. Error: issuer and subject are not the same", cert.Subject)
}

Check warning on line 35 in x509/helper.go

View check run for this annotation

Codecov / codecov/patch

x509/helper.go#L34-L35

Added lines #L34 - L35 were not covered by tests
return nil
}

func isSelfSigned(cert *x509.Certificate) (bool, error) {
return isIssuedBy(cert, cert)
}
Expand Down
4 changes: 2 additions & 2 deletions x509/timestamp_cert_validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func ValidateTimestampingCertChain(certChain []*x509.Certificate) error {
// For self-signed signing certificate (not a CA)
if len(certChain) == 1 {
cert := certChain[0]
if err := cert.CheckSignature(cert.SignatureAlgorithm, cert.RawTBSCertificate, cert.Signature); err != nil {
return fmt.Errorf("invalid self-signed certificate. subject: %q. Error: %w", cert.Subject, err)
if err := validateSelfSignedLeaf(cert); err != nil {
return err
}
if err := validateTimestampingLeafCertificate(cert); err != nil {
return fmt.Errorf("invalid self-signed certificate. Error: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion x509/timestamp_cert_validations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestInvalidTimestampingChain(t *testing.T) {
assertErrorEqual(expectedErr, err, t)

certChain = []*x509.Certificate{timestamp_leaf}
expectedErr = "invalid self-signed certificate. subject: \"CN=DigiCert Timestamp 2023,O=DigiCert\\\\, Inc.,C=US\". Error: crypto/rsa: verification error"
expectedErr = "invalid self-signed leaf certificate. subject: \"CN=DigiCert Timestamp 2023,O=DigiCert\\\\, Inc.,C=US\". Error: crypto/rsa: verification error"
err = ValidateTimestampingCertChain(certChain)
assertErrorEqual(expectedErr, err, t)

Expand Down
Loading