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: add tsa cert chain revocation check after timestamping #246

Merged
merged 9 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
57 changes: 57 additions & 0 deletions internal/timestamp/timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ package timestamp

import (
"crypto/x509"
"fmt"

"github.com/notaryproject/notation-core-go/revocation"
"github.com/notaryproject/notation-core-go/revocation/result"
"github.com/notaryproject/notation-core-go/signature"
nx509 "github.com/notaryproject/notation-core-go/x509"
"github.com/notaryproject/tspclient-go"
Expand Down Expand Up @@ -52,5 +55,59 @@ func Timestamp(req *signature.SignRequest, opts tspclient.RequestOptions) ([]byt
if err := nx509.ValidateTimestampingCertChain(tsaCertChain); err != nil {
return nil, err
}
// certificate chain revocation check after timestamping
if req.RevocationTimestampingValidator != nil {
certResults, err := req.RevocationTimestampingValidator.ValidateContext(ctx, revocation.ValidateContextOptions{
CertChain: tsaCertChain,
})
if err != nil {
return nil, fmt.Errorf("after timestamping: failed to check timestamping certificate chain revocation with error: %w", err)
Two-Hearts marked this conversation as resolved.
Show resolved Hide resolved
}
if err := revocationFinalResult(certResults, tsaCertChain); err != nil {
return nil, fmt.Errorf("after timestamping: %w", err)
Two-Hearts marked this conversation as resolved.
Show resolved Hide resolved
}
}
return resp.TimestampToken.FullBytes, nil
}

// revocationFinalResult returns an error if the final revocation result is
// not ResultOK
func revocationFinalResult(certResults []*result.CertRevocationResult, certChain []*x509.Certificate) error {
Two-Hearts marked this conversation as resolved.
Show resolved Hide resolved
finalResult := result.ResultUnknown
numOKResults := 0
var problematicCertSubject string
revokedFound := false
var revokedCertSubject string
for i := len(certResults) - 1; i >= 0; i-- {
cert := certChain[i]
certResult := certResults[i]
if certResult.Result == result.ResultOK || certResult.Result == result.ResultNonRevokable {
numOKResults++
} else {
finalResult = certResult.Result
problematicCertSubject = cert.Subject.String()
if certResult.Result == result.ResultRevoked {
revokedFound = true
revokedCertSubject = problematicCertSubject
}
}
Two-Hearts marked this conversation as resolved.
Show resolved Hide resolved
}
if revokedFound {
problematicCertSubject = revokedCertSubject
finalResult = result.ResultRevoked
}
if numOKResults == len(certResults) {
finalResult = result.ResultOK
}

// process final result
switch finalResult {
case result.ResultOK:
return nil
case result.ResultRevoked:
return fmt.Errorf("timestamping certificate with subject %q is revoked", problematicCertSubject)
default:
// revocationresult.ResultUnknown
return fmt.Errorf("timestamping certificate with subject %q revocation status is unknown", problematicCertSubject)
Two-Hearts marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading
Loading