Skip to content

Commit 3a83921

Browse files
committed
move verifyAddChain
1 parent 565af26 commit 3a83921

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

internal/scti/chain_validation.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,36 @@ func validateChain(rawChain [][]byte, validationOpts ChainValidationOpts) ([]*x5
263263
return nil, errors.New("no RFC compliant path to root found when trying to validate chain")
264264
}
265265

266+
// verifyAddChain is used by add-chain and add-pre-chain. It does the checks that the supplied
267+
// cert is of the correct type and chains to a trusted root.
268+
// TODO(phbnf): add tests
269+
func verifyAddChain(log *log, req rfc6962.AddChainRequest, expectingPrecert bool) ([]*x509.Certificate, error) {
270+
// We already checked that the chain is not empty so can move on to verification
271+
validPath, err := validateChain(req.Chain, log.chainValidationOpts)
272+
if err != nil {
273+
// We rejected it because the cert failed checks or we could not find a path to a root etc.
274+
// Lots of possible causes for errors
275+
return nil, fmt.Errorf("chain failed to verify: %s", err)
276+
}
277+
278+
isPrecert, err := isPrecertificate(validPath[0])
279+
if err != nil {
280+
return nil, fmt.Errorf("precert test failed: %s", err)
281+
}
282+
283+
// The type of the leaf must match the one the handler expects
284+
if isPrecert != expectingPrecert {
285+
if expectingPrecert {
286+
klog.Warningf("%s: Cert (or precert with invalid CT ext) submitted as precert chain: %q", log.origin, req.Chain)
287+
} else {
288+
klog.Warningf("%s: Precert (or cert with invalid CT ext) submitted as cert chain: %q", log.origin, req.Chain)
289+
}
290+
return nil, fmt.Errorf("cert / precert mismatch: %T", expectingPrecert)
291+
}
292+
293+
return validPath, nil
294+
}
295+
266296
func chainsEquivalent(inChain []*x509.Certificate, verifiedChain []*x509.Certificate) bool {
267297
// The verified chain includes a root, but the input chain may or may not include a
268298
// root (RFC 6962 s4.1/ s4.2 "the last [certificate] is either the root certificate

internal/scti/handlers.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package scti
1616

1717
import (
1818
"context"
19-
"crypto/x509"
2019
"encoding/base64"
2120
"encoding/json"
2221
"errors"
@@ -388,37 +387,6 @@ func deadlineTime(opts *HandlerOptions) time.Time {
388387
return opts.TimeSource.Now().Add(opts.Deadline)
389388
}
390389

391-
// verifyAddChain is used by add-chain and add-pre-chain. It does the checks that the supplied
392-
// cert is of the correct type and chains to a trusted root.
393-
// TODO(phbnf): add tests
394-
// TODO(phbnf): move to chain_validation.go
395-
func verifyAddChain(log *log, req rfc6962.AddChainRequest, expectingPrecert bool) ([]*x509.Certificate, error) {
396-
// We already checked that the chain is not empty so can move on to verification
397-
validPath, err := validateChain(req.Chain, log.chainValidationOpts)
398-
if err != nil {
399-
// We rejected it because the cert failed checks or we could not find a path to a root etc.
400-
// Lots of possible causes for errors
401-
return nil, fmt.Errorf("chain failed to verify: %s", err)
402-
}
403-
404-
isPrecert, err := isPrecertificate(validPath[0])
405-
if err != nil {
406-
return nil, fmt.Errorf("precert test failed: %s", err)
407-
}
408-
409-
// The type of the leaf must match the one the handler expects
410-
if isPrecert != expectingPrecert {
411-
if expectingPrecert {
412-
klog.Warningf("%s: Cert (or precert with invalid CT ext) submitted as precert chain: %q", log.origin, req.Chain)
413-
} else {
414-
klog.Warningf("%s: Precert (or cert with invalid CT ext) submitted as cert chain: %q", log.origin, req.Chain)
415-
}
416-
return nil, fmt.Errorf("cert / precert mismatch: %T", expectingPrecert)
417-
}
418-
419-
return validPath, nil
420-
}
421-
422390
// marshalAndWriteAddChainResponse is used by add-chain and add-pre-chain to create and write
423391
// the JSON response to the client
424392
func marshalAndWriteAddChainResponse(sct *rfc6962.SignedCertificateTimestamp, w http.ResponseWriter) error {

0 commit comments

Comments
 (0)