@@ -15,6 +15,7 @@ import (
1515 "strconv"
1616 "strings"
1717 "testing"
18+ "time"
1819
1920 "github.com/hashicorp/go-secure-stdlib/parseutil"
2021 vaulthttp "github.com/hashicorp/vault/http"
@@ -467,6 +468,18 @@ func TestOcsp_HigherLevel(t *testing.T) {
467468 require .Equal (t , certToRevoke .SerialNumber , ocspResp .SerialNumber )
468469}
469470
471+ // TestOcsp_NextUpdate make sure that we are setting the appropriate values
472+ // for the NextUpdate field within our responses.
473+ func TestOcsp_NextUpdate (t * testing.T ) {
474+ // Within the runOcspRequestTest, with a ocspExpiry of 0,
475+ // we will validate that NextUpdate was not set in the response
476+ runOcspRequestTest (t , "POST" , "ec" , 0 , 0 , crypto .SHA256 , 0 )
477+
478+ // Within the runOcspRequestTest, with a ocspExpiry of 24 hours, we will validate
479+ // that NextUpdate is set and has a time 24 hours larger than ThisUpdate
480+ runOcspRequestTest (t , "POST" , "ec" , 0 , 0 , crypto .SHA256 , 24 * time .Hour )
481+ }
482+
470483func TestOcsp_ValidRequests (t * testing.T ) {
471484 type caKeyConf struct {
472485 keyType string
@@ -506,13 +519,15 @@ func TestOcsp_ValidRequests(t *testing.T) {
506519 localTT .reqHash )
507520 t .Run (testName , func (t * testing.T ) {
508521 runOcspRequestTest (t , localTT .reqType , localTT .keyConf .keyType , localTT .keyConf .keyBits ,
509- localTT .keyConf .sigBits , localTT .reqHash )
522+ localTT .keyConf .sigBits , localTT .reqHash , 12 * time . Hour )
510523 })
511524 }
512525}
513526
514- func runOcspRequestTest (t * testing.T , requestType string , caKeyType string , caKeyBits int , caKeySigBits int , requestHash crypto.Hash ) {
515- b , s , testEnv := setupOcspEnvWithCaKeyConfig (t , caKeyType , caKeyBits , caKeySigBits )
527+ func runOcspRequestTest (t * testing.T , requestType string , caKeyType string ,
528+ caKeyBits int , caKeySigBits int , requestHash crypto.Hash , ocspExpiry time.Duration ,
529+ ) {
530+ b , s , testEnv := setupOcspEnvWithCaKeyConfig (t , caKeyType , caKeyBits , caKeySigBits , ocspExpiry )
516531
517532 // Non-revoked cert
518533 resp , err := SendOcspRequest (t , b , s , requestType , testEnv .leafCertIssuer1 , testEnv .issuer1 , requestHash )
@@ -574,17 +589,28 @@ func runOcspRequestTest(t *testing.T, requestType string, caKeyType string, caKe
574589 require .Equal (t , testEnv .leafCertIssuer2 .SerialNumber , ocspResp .SerialNumber )
575590
576591 // Verify that our thisUpdate and nextUpdate fields are updated as expected
577- thisUpdate := ocspResp .ThisUpdate
578- nextUpdate := ocspResp .NextUpdate
579- require .True (t , thisUpdate .Before (nextUpdate ),
580- fmt .Sprintf ("thisUpdate %s, should have been before nextUpdate: %s" , thisUpdate , nextUpdate ))
581- nextUpdateDiff := nextUpdate .Sub (thisUpdate )
582- expectedDiff , err := parseutil .ParseDurationSecond (defaultCrlConfig .OcspExpiry )
592+ resp , err = CBRead (b , s , "config/crl" )
593+ requireSuccessNonNilResponse (t , resp , err , "failed reading from config/crl" )
594+ requireFieldsSetInResp (t , resp , "ocsp_expiry" )
595+ ocspExpiryRaw := resp .Data ["ocsp_expiry" ].(string )
596+ expectedDiff , err := parseutil .ParseDurationSecond (ocspExpiryRaw )
583597 require .NoError (t , err , "failed to parse default ocsp expiry value" )
584- require .Equal (t , expectedDiff , nextUpdateDiff ,
585- fmt .Sprintf ("the delta between thisUpdate %s and nextUpdate: %s should have been around: %s but was %s" ,
586- thisUpdate , nextUpdate , defaultCrlConfig .OcspExpiry , nextUpdateDiff ))
587598
599+ thisUpdate := ocspResp .ThisUpdate
600+ require .Less (t , time .Since (thisUpdate ), 10 * time .Second , "expected ThisUpdate field to be within the last 10 seconds" )
601+ if expectedDiff != 0 {
602+ nextUpdate := ocspResp .NextUpdate
603+ require .False (t , nextUpdate .IsZero (), "nextUpdate field value should have been a non-zero time" )
604+ require .True (t , thisUpdate .Before (nextUpdate ),
605+ fmt .Sprintf ("thisUpdate %s, should have been before nextUpdate: %s" , thisUpdate , nextUpdate ))
606+ nextUpdateDiff := nextUpdate .Sub (thisUpdate )
607+ require .Equal (t , expectedDiff , nextUpdateDiff ,
608+ fmt .Sprintf ("the delta between thisUpdate %s and nextUpdate: %s should have been around: %s but was %s" ,
609+ thisUpdate , nextUpdate , defaultCrlConfig .OcspExpiry , nextUpdateDiff ))
610+ } else {
611+ // With the config value set to 0, we shouldn't have a NextUpdate field set
612+ require .True (t , ocspResp .NextUpdate .IsZero (), "nextUpdate value was not zero as expected was: %v" , ocspResp .NextUpdate )
613+ }
588614 requireOcspSignatureAlgoForKey (t , testEnv .issuer2 .SignatureAlgorithm , ocspResp .SignatureAlgorithm )
589615 requireOcspResponseSignedBy (t , ocspResp , testEnv .issuer2 )
590616}
@@ -610,16 +636,22 @@ type ocspTestEnv struct {
610636}
611637
612638func setupOcspEnv (t * testing.T , keyType string ) (* backend , logical.Storage , * ocspTestEnv ) {
613- return setupOcspEnvWithCaKeyConfig (t , keyType , 0 , 0 )
639+ return setupOcspEnvWithCaKeyConfig (t , keyType , 0 , 0 , 12 * time . Hour )
614640}
615641
616- func setupOcspEnvWithCaKeyConfig (t * testing.T , keyType string , caKeyBits int , caKeySigBits int ) (* backend , logical.Storage , * ocspTestEnv ) {
642+ func setupOcspEnvWithCaKeyConfig (t * testing.T , keyType string , caKeyBits int , caKeySigBits int , ocspExpiry time. Duration ) (* backend , logical.Storage , * ocspTestEnv ) {
617643 b , s := CreateBackendWithStorage (t )
618644 var issuerCerts []* x509.Certificate
619645 var leafCerts []* x509.Certificate
620646 var issuerIds []issuerID
621647 var keyIds []keyID
622648
649+ resp , err := CBWrite (b , s , "config/crl" , map [string ]interface {}{
650+ "ocsp_enable" : true ,
651+ "ocsp_expiry" : fmt .Sprintf ("%ds" , int (ocspExpiry .Seconds ())),
652+ })
653+ requireSuccessNonNilResponse (t , resp , err , "config/crl failed" )
654+
623655 for i := 0 ; i < 2 ; i ++ {
624656 resp , err := CBWrite (b , s , "root/generate/internal" , map [string ]interface {}{
625657 "key_type" : keyType ,
0 commit comments