@@ -205,6 +205,17 @@ type basicConstraints struct {
205
205
MaxPathLen int `asn1:"optional,default:-1"`
206
206
}
207
207
208
+ type testEnforcer struct {
209
+ enforcer func (* x509.Certificate ) error
210
+ }
211
+
212
+ func (e * testEnforcer ) Enforce (cert * x509.Certificate ) error {
213
+ if e .enforcer != nil {
214
+ return e .enforcer (cert )
215
+ }
216
+ return nil
217
+ }
218
+
208
219
func TestAuthority_Sign (t * testing.T ) {
209
220
pub , priv , err := keyutil .GenerateDefaultKeyPair ()
210
221
assert .FatalError (t , err )
@@ -238,14 +249,15 @@ func TestAuthority_Sign(t *testing.T) {
238
249
assert .FatalError (t , err )
239
250
240
251
type signTest struct {
241
- auth * Authority
242
- csr * x509.CertificateRequest
243
- signOpts provisioner.SignOptions
244
- extraOpts []provisioner.SignOption
245
- notBefore time.Time
246
- notAfter time.Time
247
- err error
248
- code int
252
+ auth * Authority
253
+ csr * x509.CertificateRequest
254
+ signOpts provisioner.SignOptions
255
+ extraOpts []provisioner.SignOption
256
+ notBefore time.Time
257
+ notAfter time.Time
258
+ extensionsCount int
259
+ err error
260
+ code int
249
261
}
250
262
tests := map [string ]func (* testing.T ) * signTest {
251
263
"fail invalid signature" : func (t * testing.T ) * signTest {
@@ -454,22 +466,66 @@ ZYtQ9Ot36qc=
454
466
code : http .StatusInternalServerError ,
455
467
}
456
468
},
457
- "ok " : func (t * testing.T ) * signTest {
469
+ "fail with provisioner enforcer " : func (t * testing.T ) * signTest {
458
470
csr := getCSR (t , priv )
459
- _a := testAuthority (t )
460
- _a .db = & db.MockAuthDB {
471
+ aa := testAuthority (t )
472
+ aa .db = & db.MockAuthDB {
461
473
MStoreCertificate : func (crt * x509.Certificate ) error {
462
474
assert .Equals (t , crt .Subject .CommonName , "smallstep test" )
463
475
return nil
464
476
},
465
477
}
478
+
466
479
return & signTest {
467
- auth : a ,
480
+ auth : aa ,
481
+ csr : csr ,
482
+ extraOpts : append (extraOpts , & testEnforcer {
483
+ enforcer : func (crt * x509.Certificate ) error { return fmt .Errorf ("an error" ) },
484
+ }),
485
+ signOpts : signOpts ,
486
+ err : errors .New ("error creating certificate" ),
487
+ code : http .StatusForbidden ,
488
+ }
489
+ },
490
+ "fail with custom enforcer" : func (t * testing.T ) * signTest {
491
+ csr := getCSR (t , priv )
492
+ aa := testAuthority (t , WithX509Enforcers (& testEnforcer {
493
+ enforcer : func (cert * x509.Certificate ) error {
494
+ return fmt .Errorf ("an error" )
495
+ },
496
+ }))
497
+ aa .db = & db.MockAuthDB {
498
+ MStoreCertificate : func (crt * x509.Certificate ) error {
499
+ assert .Equals (t , crt .Subject .CommonName , "smallstep test" )
500
+ return nil
501
+ },
502
+ }
503
+ return & signTest {
504
+ auth : aa ,
468
505
csr : csr ,
469
506
extraOpts : extraOpts ,
470
507
signOpts : signOpts ,
471
- notBefore : signOpts .NotBefore .Time ().Truncate (time .Second ),
472
- notAfter : signOpts .NotAfter .Time ().Truncate (time .Second ),
508
+ err : errors .New ("error creating certificate" ),
509
+ code : http .StatusForbidden ,
510
+ }
511
+ },
512
+ "ok" : func (t * testing.T ) * signTest {
513
+ csr := getCSR (t , priv )
514
+ _a := testAuthority (t )
515
+ _a .db = & db.MockAuthDB {
516
+ MStoreCertificate : func (crt * x509.Certificate ) error {
517
+ assert .Equals (t , crt .Subject .CommonName , "smallstep test" )
518
+ return nil
519
+ },
520
+ }
521
+ return & signTest {
522
+ auth : a ,
523
+ csr : csr ,
524
+ extraOpts : extraOpts ,
525
+ signOpts : signOpts ,
526
+ notBefore : signOpts .NotBefore .Time ().Truncate (time .Second ),
527
+ notAfter : signOpts .NotAfter .Time ().Truncate (time .Second ),
528
+ extensionsCount : 6 ,
473
529
}
474
530
},
475
531
"ok with enforced modifier" : func (t * testing.T ) * signTest {
@@ -497,12 +553,13 @@ ZYtQ9Ot36qc=
497
553
},
498
554
}
499
555
return & signTest {
500
- auth : a ,
501
- csr : csr ,
502
- extraOpts : enforcedExtraOptions ,
503
- signOpts : signOpts ,
504
- notBefore : now .Truncate (time .Second ),
505
- notAfter : now .Add (365 * 24 * time .Hour ).Truncate (time .Second ),
556
+ auth : a ,
557
+ csr : csr ,
558
+ extraOpts : enforcedExtraOptions ,
559
+ signOpts : signOpts ,
560
+ notBefore : now .Truncate (time .Second ),
561
+ notAfter : now .Add (365 * 24 * time .Hour ).Truncate (time .Second ),
562
+ extensionsCount : 6 ,
506
563
}
507
564
},
508
565
"ok with custom template" : func (t * testing.T ) * signTest {
@@ -530,12 +587,13 @@ ZYtQ9Ot36qc=
530
587
},
531
588
}
532
589
return & signTest {
533
- auth : testAuthority ,
534
- csr : csr ,
535
- extraOpts : testExtraOpts ,
536
- signOpts : signOpts ,
537
- notBefore : signOpts .NotBefore .Time ().Truncate (time .Second ),
538
- notAfter : signOpts .NotAfter .Time ().Truncate (time .Second ),
590
+ auth : testAuthority ,
591
+ csr : csr ,
592
+ extraOpts : testExtraOpts ,
593
+ signOpts : signOpts ,
594
+ notBefore : signOpts .NotBefore .Time ().Truncate (time .Second ),
595
+ notAfter : signOpts .NotAfter .Time ().Truncate (time .Second ),
596
+ extensionsCount : 6 ,
539
597
}
540
598
},
541
599
"ok/csr with no template critical SAN extension" : func (t * testing.T ) * signTest {
@@ -558,12 +616,39 @@ ZYtQ9Ot36qc=
558
616
},
559
617
}
560
618
return & signTest {
561
- auth : _a ,
562
- csr : csr ,
563
- extraOpts : enforcedExtraOptions ,
564
- signOpts : provisioner.SignOptions {},
565
- notBefore : now .Truncate (time .Second ),
566
- notAfter : now .Add (365 * 24 * time .Hour ).Truncate (time .Second ),
619
+ auth : _a ,
620
+ csr : csr ,
621
+ extraOpts : enforcedExtraOptions ,
622
+ signOpts : provisioner.SignOptions {},
623
+ notBefore : now .Truncate (time .Second ),
624
+ notAfter : now .Add (365 * 24 * time .Hour ).Truncate (time .Second ),
625
+ extensionsCount : 5 ,
626
+ }
627
+ },
628
+ "ok with custom enforcer" : func (t * testing.T ) * signTest {
629
+ csr := getCSR (t , priv )
630
+ aa := testAuthority (t , WithX509Enforcers (& testEnforcer {
631
+ enforcer : func (cert * x509.Certificate ) error {
632
+ cert .CRLDistributionPoints = []string {"http://ca.example.org/leaf.crl" }
633
+ return nil
634
+ },
635
+ }))
636
+ aa .config .AuthorityConfig .Template = a .config .AuthorityConfig .Template
637
+ aa .db = & db.MockAuthDB {
638
+ MStoreCertificate : func (crt * x509.Certificate ) error {
639
+ assert .Equals (t , crt .Subject .CommonName , "smallstep test" )
640
+ assert .Equals (t , crt .CRLDistributionPoints , []string {"http://ca.example.org/leaf.crl" })
641
+ return nil
642
+ },
643
+ }
644
+ return & signTest {
645
+ auth : aa ,
646
+ csr : csr ,
647
+ extraOpts : extraOpts ,
648
+ signOpts : signOpts ,
649
+ notBefore : signOpts .NotBefore .Time ().Truncate (time .Second ),
650
+ notAfter : signOpts .NotAfter .Time ().Truncate (time .Second ),
651
+ extensionsCount : 7 ,
567
652
}
568
653
},
569
654
}
@@ -645,16 +730,14 @@ ZYtQ9Ot36qc=
645
730
// Empty CSR subject test does not use any provisioner extensions.
646
731
// So provisioner ID ext will be missing.
647
732
found = 1
648
- assert .Len (t , 5 , leaf .Extensions )
649
- } else {
650
- assert .Len (t , 6 , leaf .Extensions )
651
733
}
652
734
}
653
735
}
654
736
assert .Equals (t , found , 1 )
655
737
realIntermediate , err := x509 .ParseCertificate (issuer .Raw )
656
738
assert .FatalError (t , err )
657
739
assert .Equals (t , intermediate , realIntermediate )
740
+ assert .Len (t , tc .extensionsCount , leaf .Extensions )
658
741
}
659
742
}
660
743
})
0 commit comments