Skip to content

Commit 3ef1522

Browse files
committed
convert functions to methods
1 parent 3a83921 commit 3ef1522

File tree

3 files changed

+32
-32
lines changed

3 files changed

+32
-32
lines changed

internal/scti/chain_validation.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func isPrecertificate(cert *x509.Certificate) (bool, error) {
149149
// supplied in the chain. Then applies the RFC requirement that the path must involve all
150150
// the submitted chain in the order of submission.
151151
// TODO(phboneff): make this a method func([][]byte) ([]*x509.Certificate, error)
152-
func validateChain(rawChain [][]byte, validationOpts ChainValidationOpts) ([]*x509.Certificate, error) {
152+
func (opts ChainValidationOpts) validateChain(rawChain [][]byte) ([]*x509.Certificate, error) {
153153
if len(rawChain) == 0 {
154154
return nil, errors.New("empty certificate chain")
155155
}
@@ -172,8 +172,8 @@ func validateChain(rawChain [][]byte, validationOpts ChainValidationOpts) ([]*x5
172172
}
173173
}
174174

175-
naStart := validationOpts.notAfterStart
176-
naLimit := validationOpts.notAfterLimit
175+
naStart := opts.notAfterStart
176+
naLimit := opts.notAfterLimit
177177
cert := chain[0]
178178

179179
// Check whether the expiry date of the cert is within the acceptable range.
@@ -184,24 +184,24 @@ func validateChain(rawChain [][]byte, validationOpts ChainValidationOpts) ([]*x5
184184
return nil, fmt.Errorf("certificate NotAfter (%v) >= %v", cert.NotAfter, *naLimit)
185185
}
186186

187-
now := validationOpts.currentTime
187+
now := opts.currentTime
188188
if now.IsZero() {
189189
now = time.Now()
190190
}
191191
expired := now.After(cert.NotAfter)
192-
if validationOpts.rejectExpired && expired {
192+
if opts.rejectExpired && expired {
193193
return nil, errors.New("rejecting expired certificate")
194194
}
195-
if validationOpts.rejectUnexpired && !expired {
195+
if opts.rejectUnexpired && !expired {
196196
return nil, errors.New("rejecting unexpired certificate")
197197
}
198198

199199
// Check for unwanted extension types, if required.
200200
// TODO(al): Refactor CertValidationOpts c'tor to a builder pattern and
201201
// pre-calc this in there
202-
if len(validationOpts.rejectExtIds) != 0 {
202+
if len(opts.rejectExtIds) != 0 {
203203
badIDs := make(map[string]bool)
204-
for _, id := range validationOpts.rejectExtIds {
204+
for _, id := range opts.rejectExtIds {
205205
badIDs[id.String()] = true
206206
}
207207
for idx, ext := range cert.Extensions {
@@ -214,9 +214,9 @@ func validateChain(rawChain [][]byte, validationOpts ChainValidationOpts) ([]*x5
214214

215215
// TODO(al): Refactor CertValidationOpts c'tor to a builder pattern and
216216
// pre-calc this in there too.
217-
if len(validationOpts.extKeyUsages) > 0 {
217+
if len(opts.extKeyUsages) > 0 {
218218
acceptEKUs := make(map[x509.ExtKeyUsage]bool)
219-
for _, eku := range validationOpts.extKeyUsages {
219+
for _, eku := range opts.extKeyUsages {
220220
acceptEKUs[eku] = true
221221
}
222222
good := false
@@ -227,7 +227,7 @@ func validateChain(rawChain [][]byte, validationOpts ChainValidationOpts) ([]*x5
227227
}
228228
}
229229
if !good {
230-
return nil, fmt.Errorf("rejecting certificate without EKU in %v", validationOpts.extKeyUsages)
230+
return nil, fmt.Errorf("rejecting certificate without EKU in %v", opts.extKeyUsages)
231231
}
232232
}
233233

@@ -237,9 +237,9 @@ func validateChain(rawChain [][]byte, validationOpts ChainValidationOpts) ([]*x5
237237
// - allow certificate without policing them since this is not CT's responsibility
238238
// See /internal/lax509/README.md for further information.
239239
verifyOpts := lax509.VerifyOptions{
240-
Roots: validationOpts.trustedRoots.CertPool(),
240+
Roots: opts.trustedRoots.CertPool(),
241241
Intermediates: intermediatePool.CertPool(),
242-
KeyUsages: validationOpts.extKeyUsages,
242+
KeyUsages: opts.extKeyUsages,
243243
}
244244

245245
verifiedChains, err := lax509.Verify(cert, verifyOpts)
@@ -266,9 +266,9 @@ func validateChain(rawChain [][]byte, validationOpts ChainValidationOpts) ([]*x5
266266
// verifyAddChain is used by add-chain and add-pre-chain. It does the checks that the supplied
267267
// cert is of the correct type and chains to a trusted root.
268268
// TODO(phbnf): add tests
269-
func verifyAddChain(log *log, req rfc6962.AddChainRequest, expectingPrecert bool) ([]*x509.Certificate, error) {
269+
func (opts ChainValidationOpts) verifyAddChain(req rfc6962.AddChainRequest, expectingPrecert bool) ([]*x509.Certificate, error) {
270270
// We already checked that the chain is not empty so can move on to verification
271-
validPath, err := validateChain(req.Chain, log.chainValidationOpts)
271+
validPath, err := opts.validateChain(req.Chain)
272272
if err != nil {
273273
// We rejected it because the cert failed checks or we could not find a path to a root etc.
274274
// Lots of possible causes for errors
@@ -283,9 +283,9 @@ func verifyAddChain(log *log, req rfc6962.AddChainRequest, expectingPrecert bool
283283
// The type of the leaf must match the one the handler expects
284284
if isPrecert != expectingPrecert {
285285
if expectingPrecert {
286-
klog.Warningf("%s: Cert (or precert with invalid CT ext) submitted as precert chain: %q", log.origin, req.Chain)
286+
klog.Warningf("Cert (or precert with invalid CT ext) submitted as precert chain: %q", req.Chain)
287287
} else {
288-
klog.Warningf("%s: Precert (or cert with invalid CT ext) submitted as cert chain: %q", log.origin, req.Chain)
288+
klog.Warningf("Precert (or cert with invalid CT ext) submitted as cert chain: %q", req.Chain)
289289
}
290290
return nil, fmt.Errorf("cert / precert mismatch: %T", expectingPrecert)
291291
}

internal/scti/chain_validation_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ func TestValidateChain(t *testing.T) {
254254
if !fakeCARoots.AppendCertsFromPEM([]byte(testdata.RealPrecertIntermediatePEM)) {
255255
t.Fatal("failed to load real intermediate")
256256
}
257-
validateOpts := ChainValidationOpts{
257+
opts := ChainValidationOpts{
258258
trustedRoots: fakeCARoots,
259259
}
260260

@@ -403,11 +403,11 @@ func TestValidateChain(t *testing.T) {
403403
}
404404
for _, test := range tests {
405405
t.Run(test.desc, func(t *testing.T) {
406-
validateOpts := validateOpts
406+
opts := opts
407407
if test.modifyOpts != nil {
408-
test.modifyOpts(&validateOpts)
408+
test.modifyOpts(&opts)
409409
}
410-
gotPath, err := validateChain(test.chain, validateOpts)
410+
gotPath, err := opts.validateChain(test.chain)
411411
if err != nil {
412412
if !test.wantErr {
413413
t.Errorf("ValidateChain()=%v,%v; want _,nil", gotPath, err)
@@ -433,7 +433,7 @@ func TestNotAfterRange(t *testing.T) {
433433
if !fakeCARoots.AppendCertsFromPEM([]byte(testdata.FakeCACertPEM)) {
434434
t.Fatal("failed to load fake root")
435435
}
436-
validateOpts := ChainValidationOpts{
436+
opts := ChainValidationOpts{
437437
trustedRoots: fakeCARoots,
438438
rejectExpired: false,
439439
}
@@ -473,12 +473,12 @@ func TestNotAfterRange(t *testing.T) {
473473
for _, test := range tests {
474474
t.Run(test.desc, func(t *testing.T) {
475475
if !test.notAfterStart.IsZero() {
476-
validateOpts.notAfterStart = &test.notAfterStart
476+
opts.notAfterStart = &test.notAfterStart
477477
}
478478
if !test.notAfterLimit.IsZero() {
479-
validateOpts.notAfterLimit = &test.notAfterLimit
479+
opts.notAfterLimit = &test.notAfterLimit
480480
}
481-
gotPath, err := validateChain(test.chain, validateOpts)
481+
gotPath, err := opts.validateChain(test.chain)
482482
if err != nil {
483483
if !test.wantErr {
484484
t.Errorf("ValidateChain()=%v,%v; want _,nil", gotPath, err)
@@ -500,7 +500,7 @@ func TestRejectExpiredUnexpired(t *testing.T) {
500500
}
501501
// Validity period: May 13, 2016 - Jul 12, 2019.
502502
chain := pemsToDERChain(t, []string{testdata.LeafSignedByFakeIntermediateCertPEM, testdata.FakeIntermediateCertPEM})
503-
validateOpts := ChainValidationOpts{
503+
opts := ChainValidationOpts{
504504
trustedRoots: fakeCARoots,
505505
extKeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
506506
}
@@ -587,10 +587,10 @@ func TestRejectExpiredUnexpired(t *testing.T) {
587587
},
588588
} {
589589
t.Run(tc.desc, func(t *testing.T) {
590-
validateOpts.currentTime = tc.now
591-
validateOpts.rejectExpired = tc.rejectExpired
592-
validateOpts.rejectUnexpired = tc.rejectUnexpired
593-
_, err := validateChain(chain, validateOpts)
590+
opts.currentTime = tc.now
591+
opts.rejectExpired = tc.rejectExpired
592+
opts.rejectUnexpired = tc.rejectUnexpired
593+
_, err := opts.validateChain(chain)
594594
if err != nil {
595595
if len(tc.wantErr) == 0 {
596596
t.Errorf("ValidateChain()=_,%v; want _,nil", err)
@@ -692,7 +692,7 @@ func TestPreIssuedCert(t *testing.T) {
692692
trustedRoots: roots,
693693
extKeyUsages: tc.eku,
694694
}
695-
chain, err := validateChain(rawChain, opts)
695+
chain, err := opts.validateChain(rawChain)
696696
if err != nil {
697697
t.Fatalf("failed to ValidateChain: %v", err)
698698
}

internal/scti/handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func addChainInternal(ctx context.Context, opts *HandlerOptions, log *log, w htt
265265
for _, der := range addChainReq.Chain {
266266
opts.RequestLog.addDERToChain(ctx, der)
267267
}
268-
chain, err := verifyAddChain(log, addChainReq, isPrecert)
268+
chain, err := log.chainValidationOpts.verifyAddChain(addChainReq, isPrecert)
269269
if err != nil {
270270
return http.StatusBadRequest, fmt.Errorf("failed to verify add-chain contents: %s", err)
271271
}

0 commit comments

Comments
 (0)