Skip to content

Commit

Permalink
Fix compat layer ASN1_TIME_diff to accept NULL output params
Browse files Browse the repository at this point in the history
  • Loading branch information
embhorn committed Jan 31, 2025
1 parent 275beca commit b488af1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
32 changes: 15 additions & 17 deletions src/ssl_asn1.c
Original file line number Diff line number Diff line change
Expand Up @@ -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] to ASN.1 TIME object as end time.
* @return 1 on success.
* @return 0 when days or secs is NULL.
* @return 0 when conversion of time fails.
*/
int wolfSSL_ASN1_TIME_diff(int *days, int *secs, const WOLFSSL_ASN1_TIME *from,
Expand All @@ -3833,21 +3832,15 @@ int wolfSSL_ASN1_TIME_diff(int *days, int *secs, const WOLFSSL_ASN1_TIME *from,

WOLFSSL_ENTER("wolfSSL_ASN1_TIME_diff");

/* Validate parameters. */
if (days == NULL) {
WOLFSSL_MSG("days is NULL");
ret = 0;
}
if ((ret == 1) && (secs == NULL)) {
WOLFSSL_MSG("secs is NULL");
ret = 0;
}

if ((ret == 1) && ((from == NULL) && (to == NULL))) {
*days = 0;
*secs = 0;
if ((from == NULL) && (to == NULL)) {
if (days != NULL) {
*days = 0;
}
if (secs != NULL) {
*secs = 0;
}
}
else if (ret == 1) {
else {
const long long SECS_PER_DAY = 24 * 60 * 60;
long long fromSecs;
long long toSecs = 0;
Expand All @@ -3858,8 +3851,13 @@ int wolfSSL_ASN1_TIME_diff(int *days, int *secs, const WOLFSSL_ASN1_TIME *from,
}
if (ret == 1) {
long long diffSecs = toSecs - fromSecs;
*days = (int) (diffSecs / SECS_PER_DAY);
*secs = (int) (diffSecs - ((long long)*days * SECS_PER_DAY));
if (days != NULL) {
*days = (int) (diffSecs / SECS_PER_DAY);
}
if (secs != NULL) {
*secs = (int) (diffSecs -
((long long)(diffSecs / SECS_PER_DAY) * SECS_PER_DAY));
}
}
}

Expand Down
7 changes: 4 additions & 3 deletions tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -54544,9 +54544,10 @@ static int test_wolfSSL_ASN1_TIME_diff_compare(void)

ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);

/* Error conditions. */
ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
/* Test when secsDiff or daysDiff is NULL. */
ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 1);
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. */
ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
Expand Down

0 comments on commit b488af1

Please sign in to comment.