@@ -5619,6 +5619,125 @@ int test_wc_MakeCert_generalizedTimeValidity(void)
56195619 return EXPECT_RESULT ();
56205620}
56215621
5622+ #if defined(TEST_SIGN_CERT_BOUNDS_RSA ) && !defined(NO_ASN_TIME )
5623+ static time_t test_asn_validityNow ;
5624+ static time_t test_asn_validity_time_cb (time_t * t )
5625+ {
5626+ if (t != NULL )
5627+ * t = test_asn_validityNow ;
5628+ return test_asn_validityNow ;
5629+ }
5630+
5631+ /* Make a cert valid for daysValid at the fixed time and check both dates. */
5632+ static int test_asn_validity_days (Cert * cert , RsaKey * key , WC_RNG * rng ,
5633+ byte * der , time_t now , int daysValid , const char * expBefore ,
5634+ const char * expAfter )
5635+ {
5636+ EXPECT_DECLS ;
5637+ DecodedCert dc ;
5638+ int derSz = 0 ;
5639+ word32 len ;
5640+
5641+ test_asn_validityNow = now ;
5642+ cert -> daysValid = daysValid ;
5643+ ExpectIntEQ (wc_SetTimeCb (test_asn_validity_time_cb ), 0 );
5644+ derSz = wc_MakeCert (cert , der , SIGN_CERT_SCRATCH_SZ , key , NULL , rng );
5645+ wc_SetTimeCb (NULL );
5646+ if (expAfter == NULL ) {
5647+ ExpectIntEQ (derSz , WC_NO_ERR_TRACE (DATE_E ));
5648+ return EXPECT_RESULT ();
5649+ }
5650+ ExpectIntGT (derSz , 0 );
5651+ if (EXPECT_SUCCESS ()) {
5652+ ExpectIntGT (derSz = wc_SignCert (cert -> bodySz , cert -> sigType , der ,
5653+ SIGN_CERT_SCRATCH_SZ , key , NULL , rng ), 0 );
5654+ }
5655+ if (EXPECT_SUCCESS ()) {
5656+ wc_InitDecodedCert (& dc , der , (word32 )derSz , NULL );
5657+ ExpectIntEQ (wc_ParseCert (& dc , CERT_TYPE , NO_VERIFY , NULL ), 0 );
5658+ len = (word32 )XSTRLEN (expBefore );
5659+ ExpectIntEQ (dc .beforeDate [0 ], (len == 15 ) ? ASN_GENERALIZED_TIME :
5660+ ASN_UTC_TIME );
5661+ ExpectIntEQ (dc .beforeDate [1 ], len );
5662+ ExpectBufEQ (dc .beforeDate + 2 , expBefore , len );
5663+ len = (word32 )XSTRLEN (expAfter );
5664+ ExpectIntEQ (dc .afterDate [0 ], (len == 15 ) ? ASN_GENERALIZED_TIME :
5665+ ASN_UTC_TIME );
5666+ ExpectIntEQ (dc .afterDate [1 ], len );
5667+ ExpectBufEQ (dc .afterDate + 2 , expAfter , len );
5668+ wc_FreeDecodedCert (& dc );
5669+ }
5670+ return EXPECT_RESULT ();
5671+ }
5672+ #endif
5673+
5674+ int test_wc_MakeCert_validityDays (void )
5675+ {
5676+ EXPECT_DECLS ;
5677+ #if defined(TEST_SIGN_CERT_BOUNDS_RSA ) && !defined(NO_ASN_TIME )
5678+ WC_RNG rng ;
5679+ Cert cert ;
5680+ RsaKey key ;
5681+ byte * der = NULL ;
5682+ word32 idx = 0 ;
5683+ int rngInit = 0 ;
5684+ int keyInit = 0 ;
5685+ /* 2026-11-19T12:00:00Z and 2028-03-01T00:00:00Z */
5686+ const time_t now = (time_t )1795089600L ;
5687+ const time_t leapNow = (time_t )1835481600L ;
5688+
5689+ XMEMSET (& rng , 0 , sizeof (rng ));
5690+ XMEMSET (& cert , 0 , sizeof (cert ));
5691+ XMEMSET (& key , 0 , sizeof (key ));
5692+
5693+ ExpectIntEQ (wc_InitRng (& rng ), 0 );
5694+ if (EXPECT_SUCCESS ()) rngInit = 1 ;
5695+ ExpectIntEQ (wc_InitRsaKey_ex (& key , HEAP_HINT , testDevId ), 0 );
5696+ if (EXPECT_SUCCESS ()) keyInit = 1 ;
5697+ ExpectIntEQ (wc_RsaPrivateKeyDecode (server_key_der_2048 , & idx , & key ,
5698+ sizeof_server_key_der_2048 ), 0 );
5699+ ExpectNotNull (der = (byte * )XMALLOC (SIGN_CERT_SCRATCH_SZ , HEAP_HINT ,
5700+ DYNAMIC_TYPE_TMP_BUFFER ));
5701+ ExpectIntEQ (wc_InitCert (& cert ), 0 );
5702+ if (EXPECT_SUCCESS ()) {
5703+ cert .sigType = CTC_SHA256wRSA ;
5704+ cert .serial [0 ] = 0x01 ;
5705+ cert .serialSz = 1 ;
5706+ XSTRNCPY (cert .subject .commonName , "validity-days" , CTC_NAME_SIZE );
5707+ }
5708+
5709+ ExpectIntEQ (test_asn_validity_days (& cert , & key , & rng , der , now , 9000 ,
5710+ "261118120000Z" , "20510711120000Z" ), TEST_SUCCESS );
5711+ ExpectIntEQ (test_asn_validity_days (& cert , & key , & rng , der , now , 2912120 ,
5712+ "261118120000Z" , "99991231120000Z" ), TEST_SUCCESS );
5713+ ExpectIntEQ (test_asn_validity_days (& cert , & key , & rng , der , now , 2912121 ,
5714+ NULL , NULL ), TEST_SUCCESS );
5715+ ExpectIntEQ (test_asn_validity_days (& cert , & key , & rng , der , leapNow , 365 ,
5716+ "280229000000Z" , "290301000000Z" ), TEST_SUCCESS );
5717+ ExpectIntEQ (test_asn_validity_days (& cert , & key , & rng , der , now , -739938 ,
5718+ "261118120000Z" , "00010101120000Z" ), TEST_SUCCESS );
5719+ ExpectIntEQ (test_asn_validity_days (& cert , & key , & rng , der , now , -739939 ,
5720+ NULL , NULL ), TEST_SUCCESS );
5721+ /* An XTIME error, then a clock before 1970 as a signed 32-bit time_t
5722+ * after 2038 gives. */
5723+ ExpectIntEQ (test_asn_validity_days (& cert , & key , & rng , der , (time_t )- 1 ,
5724+ 365 , NULL , NULL ), TEST_SUCCESS );
5725+ #ifndef WOLFSSL_GMTIME
5726+ if ((time_t )- 1 < (time_t )1 ) {
5727+ ExpectIntEQ (test_asn_validity_days (& cert , & key , & rng , der ,
5728+ (time_t )- 86400 , 365 , NULL , NULL ), TEST_SUCCESS );
5729+ }
5730+ #endif
5731+
5732+ XFREE (der , HEAP_HINT , DYNAMIC_TYPE_TMP_BUFFER );
5733+ if (keyInit )
5734+ wc_FreeRsaKey (& key );
5735+ if (rngInit )
5736+ wc_FreeRng (& rng );
5737+ #endif
5738+ return EXPECT_RESULT ();
5739+ }
5740+
56225741#if defined(TEST_SIGN_CERT_BOUNDS_RSA ) || defined(TEST_SIGN_CERT_BOUNDS_ECC )
56235742#define TEST_MAKECERT_SERIAL
56245743#endif
0 commit comments