Skip to content

Commit 255e968

Browse files
committed
* modules/ssl/ssl_engine_vars.c (ssl_var_lookup_ssl_cert): Use
ASN1_TIME_diff() if available to avoid parsing the ASN1_TIME.
1 parent 76659b2 commit 255e968

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

modules/ssl/ssl_engine_vars.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -827,23 +827,33 @@ static const char *ssl_var_lookup_ssl_cert_valid(apr_pool_t *p, ASN1_TIME *tm)
827827
return modssl_bio_free_read(p, bio);
828828
}
829829

830-
#define DIGIT2NUM(x) (((x)[0] - '0') * 10 + (x)[1] - '0')
830+
/* Evaluates to true if asn1 isn't a valid ASN.1 TIME; RFC3280
831+
* mandates that the seconds digits are present even though ASN.1
832+
* doesn't. */
833+
#define INVALID_ASN1_TIME(asn1) ( \
834+
((asn1)->type == V_ASN1_UTCTIME && (asn1)->length < 11) \
835+
|| ((asn1)->type == V_ASN1_GENERALIZEDTIME && (asn1)->length < 13) \
836+
|| ASN1_TIME_check(asn1) != 1)
831837

832838
/* Return a string giving the number of days remaining until 'tm', or
833839
* "0" if this can't be determined. */
834840
static const char *ssl_var_lookup_ssl_cert_remain(apr_pool_t *p, ASN1_TIME *tm)
835841
{
842+
#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
843+
int diff;
844+
845+
if (INVALID_ASN1_TIME(tm) || ASN1_TIME_diff(&diff, NULL, NULL, tm) != 1) {
846+
return "0";
847+
}
848+
#else
836849
apr_time_t then, now = apr_time_now();
837850
apr_time_exp_t exp = {0};
838851
long diff;
839852
unsigned char *dp;
840853

841-
/* Fail if the time isn't a valid ASN.1 TIME; RFC3280 mandates
842-
* that the seconds digits are present even though ASN.1
843-
* doesn't. */
844-
if ((tm->type == V_ASN1_UTCTIME && tm->length < 11) ||
845-
(tm->type == V_ASN1_GENERALIZEDTIME && tm->length < 13) ||
846-
!ASN1_TIME_check(tm)) {
854+
#define DIGIT2NUM(x) (((x)[0] - '0') * 10 + (x)[1] - '0')
855+
856+
if (INVALID_ASN1_TIME(tm)) {
847857
return "0";
848858
}
849859

@@ -857,7 +867,7 @@ static const char *ssl_var_lookup_ssl_cert_remain(apr_pool_t *p, ASN1_TIME *tm)
857867
}
858868

859869
exp.tm_mon = DIGIT2NUM(dp) - 1;
860-
exp.tm_mday = DIGIT2NUM(dp + 2);
870+
exp.tm_mday = DIGIT2NUM(dp + 2) + 1;
861871
exp.tm_hour = DIGIT2NUM(dp + 4);
862872
exp.tm_min = DIGIT2NUM(dp + 6);
863873
exp.tm_sec = DIGIT2NUM(dp + 8);
@@ -867,6 +877,7 @@ static const char *ssl_var_lookup_ssl_cert_remain(apr_pool_t *p, ASN1_TIME *tm)
867877
}
868878

869879
diff = (long)((apr_time_sec(then) - apr_time_sec(now)) / (60*60*24));
880+
#endif
870881

871882
return diff > 0 ? apr_ltoa(p, diff) : "0";
872883
}

0 commit comments

Comments
 (0)