From 5d1947474e5b32c0d73a4790cc8866e85b726cf6 Mon Sep 17 00:00:00 2001 From: Andy Green Date: Mon, 24 Feb 2025 11:16:20 +0000 Subject: [PATCH] lws_tls_openssl_asn1time_to_unix: fix 13 char asn1 epoch Also align to struct tm's year epoch of 1900 https://github.com/warmcat/libwebsockets/issues/3341 --- lib/tls/openssl/openssl-x509.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/tls/openssl/openssl-x509.c b/lib/tls/openssl/openssl-x509.c index dac4aa391..d8e8dcc82 100644 --- a/lib/tls/openssl/openssl-x509.c +++ b/lib/tls/openssl/openssl-x509.c @@ -47,11 +47,13 @@ lws_tls_openssl_asn1time_to_unix(ASN1_TIME *as) memset(&t, 0, sizeof(t)); if (strlen(p) == 13) { - t.tm_year = (dec(p[0]) * 10) + dec(p[1]) + 100; + t.tm_year = (dec(p[0]) * 10) + dec(p[1]); + if (t.tm_year < 50) /* RFC5280: 13 char dates will break after 2049 */ + t.tm_year += 100; /* struct tm year is -1900, this gives 2000..2049 */ p += 2; } else { - t.tm_year = (dec(p[0]) * 1000) + (dec(p[1]) * 100) + - (dec(p[2]) * 10) + dec(p[3]); + t.tm_year = ((dec(p[0]) * 1000) + (dec(p[1]) * 100) + + (dec(p[2]) * 10) + dec(p[3])) - 1900; /* struct tm year is -1900 */ p += 4; } t.tm_mon = (dec(p[0]) * 10) + dec(p[1]) - 1;