Merge pull request #8407 from embhorn/zd19346

Fix compat layer ASN1_TIME_diff to accept NULL output params
pull/8402/head
Sean Parkinson 2025-02-04 08:43:50 +10:00 committed by GitHub
commit 7898cce43c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 20 deletions

View File

@ -3823,7 +3823,6 @@ static int wolfssl_asn1_time_to_secs(const WOLFSSL_ASN1_TIME* t,
* @param [in] from ASN.1 TIME object as start time. * @param [in] from ASN.1 TIME object as start time.
* @param [in] to ASN.1 TIME object as end time. * @param [in] to ASN.1 TIME object as end time.
* @return 1 on success. * @return 1 on success.
* @return 0 when days or secs is NULL.
* @return 0 when conversion of time fails. * @return 0 when conversion of time fails.
*/ */
int wolfSSL_ASN1_TIME_diff(int *days, int *secs, const WOLFSSL_ASN1_TIME *from, int wolfSSL_ASN1_TIME_diff(int *days, int *secs, const WOLFSSL_ASN1_TIME *from,
@ -3833,21 +3832,15 @@ int wolfSSL_ASN1_TIME_diff(int *days, int *secs, const WOLFSSL_ASN1_TIME *from,
WOLFSSL_ENTER("wolfSSL_ASN1_TIME_diff"); WOLFSSL_ENTER("wolfSSL_ASN1_TIME_diff");
/* Validate parameters. */ if ((from == NULL) && (to == NULL)) {
if (days == NULL) { if (days != NULL) {
WOLFSSL_MSG("days is NULL"); *days = 0;
ret = 0; }
if (secs != NULL) {
*secs = 0;
}
} }
if ((ret == 1) && (secs == NULL)) { else {
WOLFSSL_MSG("secs is NULL");
ret = 0;
}
if ((ret == 1) && ((from == NULL) && (to == NULL))) {
*days = 0;
*secs = 0;
}
else if (ret == 1) {
const long long SECS_PER_DAY = 24 * 60 * 60; const long long SECS_PER_DAY = 24 * 60 * 60;
long long fromSecs; long long fromSecs;
long long toSecs = 0; long long toSecs = 0;
@ -3858,8 +3851,13 @@ int wolfSSL_ASN1_TIME_diff(int *days, int *secs, const WOLFSSL_ASN1_TIME *from,
} }
if (ret == 1) { if (ret == 1) {
long long diffSecs = toSecs - fromSecs; long long diffSecs = toSecs - fromSecs;
*days = (int) (diffSecs / SECS_PER_DAY); if (days != NULL) {
*secs = (int) (diffSecs - ((long long)*days * SECS_PER_DAY)); *days = (int) (diffSecs / SECS_PER_DAY);
}
if (secs != NULL) {
*secs = (int) (diffSecs -
((long long)(diffSecs / SECS_PER_DAY) * SECS_PER_DAY));
}
} }
} }

View File

@ -54544,9 +54544,10 @@ static int test_wolfSSL_ASN1_TIME_diff_compare(void)
ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1); ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
/* Error conditions. */ /* Test when secsDiff or daysDiff is NULL. */
ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0); ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 1);
ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0); ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 1);
ExpectIntEQ(ASN1_TIME_diff(NULL, NULL, fromTime, toTime), 1);
/* If both times are NULL, difference is 0. */ /* If both times are NULL, difference is 0. */
ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1); ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);