diff options
author | Bob Beck <bbe@google.com> | 2023-02-08 11:32:19 -0700 |
---|---|---|
committer | Boringssl LUCI CQ <boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2023-02-08 19:04:21 +0000 |
commit | 6e20b77e6b79069e2468686bdc69169d3fa2252e (patch) | |
tree | f721b90bea0194732329cdc5b9f6ee53b83f75b2 /crypto/asn1 | |
parent | 19721cd7789e4e4a69c0786951b452262eaba251 (diff) | |
download | boringssl-6e20b77e6b79069e2468686bdc69169d3fa2252e.zip boringssl-6e20b77e6b79069e2468686bdc69169d3fa2252e.tar.gz boringssl-6e20b77e6b79069e2468686bdc69169d3fa2252e.tar.bz2 |
Get rid of time_t usage internally, change to int64_t
We still keep time_t stuff around for calling time() and
for external interfaces that are meant to give you time_t
values, but we stop using time_t internally. For publicly
exposed and used inputs that rely on time_t, _posix versions are
added to support providing times as an int64_t, and internal
use is changed to use the _posix version.
Several legacy functions which are extensivly used and
and use pointers to time_t are retained for compatibility,
along with posix time versions of them which we use exclusively.
This fixes the tests which were disabled on 32 bit platorms
to always run.
Update-Note: This is a potentially breaking change for things
that bind to the ASN1_[UTC|GENERALIZED]TIME_set and ASN1_TIME_adj
family of functions (and can not type convert a time_t to an
int64).
Bug: 416
Change-Id: Ic4daba5a299d8f35191853742640750a1ecc53d6
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/54765
Commit-Queue: Bob Beck <bbe@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
Diffstat (limited to 'crypto/asn1')
-rw-r--r-- | crypto/asn1/a_gentm.c | 9 | ||||
-rw-r--r-- | crypto/asn1/a_time.c | 28 | ||||
-rw-r--r-- | crypto/asn1/a_utctm.c | 11 | ||||
-rw-r--r-- | crypto/asn1/asn1_test.cc | 66 |
4 files changed, 57 insertions, 57 deletions
diff --git a/crypto/asn1/a_gentm.c b/crypto/asn1/a_gentm.c index 096e912..ff1f97c 100644 --- a/crypto/asn1/a_gentm.c +++ b/crypto/asn1/a_gentm.c @@ -58,6 +58,7 @@ #include <openssl/bytestring.h> #include <openssl/err.h> #include <openssl/mem.h> +#include <openssl/time.h> #include <string.h> #include <time.h> @@ -98,15 +99,15 @@ int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str) { } ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s, - time_t t) { - return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0); + int64_t posix_time) { + return ASN1_GENERALIZEDTIME_adj(s, posix_time, 0, 0); } ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s, - time_t t, int offset_day, + int64_t posix_time, int offset_day, long offset_sec) { struct tm data; - if (!OPENSSL_gmtime(&t, &data)) { + if (!OPENSSL_posix_to_tm(posix_time, &data)) { return NULL; } diff --git a/crypto/asn1/a_time.c b/crypto/asn1/a_time.c index 643584b..87bf092 100644 --- a/crypto/asn1/a_time.c +++ b/crypto/asn1/a_time.c @@ -74,29 +74,31 @@ IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME) IMPLEMENT_ASN1_FUNCTIONS_const(ASN1_TIME) -ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t) { - return ASN1_TIME_adj(s, t, 0, 0); +ASN1_TIME *ASN1_TIME_set_posix(ASN1_TIME *s, int64_t posix_time) { + return ASN1_TIME_adj(s, posix_time, 0, 0); } -ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t, int offset_day, +ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t time) { + return ASN1_TIME_adj(s, time, 0, 0); +} + +ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, int64_t posix_time, int offset_day, long offset_sec) { - struct tm *ts; - struct tm data; + struct tm tm; - ts = OPENSSL_gmtime(&t, &data); - if (ts == NULL) { + if (!OPENSSL_posix_to_tm(posix_time, &tm)) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_ERROR_GETTING_TIME); return NULL; } if (offset_day || offset_sec) { - if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec)) { + if (!OPENSSL_gmtime_adj(&tm, offset_day, offset_sec)) { return NULL; } } - if ((ts->tm_year >= 50) && (ts->tm_year < 150)) { - return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec); + if ((tm.tm_year >= 50) && (tm.tm_year < 150)) { + return ASN1_UTCTIME_adj(s, posix_time, offset_day, offset_sec); } - return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec); + return ASN1_GENERALIZEDTIME_adj(s, posix_time, offset_day, offset_sec); } int ASN1_TIME_check(const ASN1_TIME *t) { @@ -172,9 +174,7 @@ int ASN1_TIME_set_string(ASN1_TIME *s, const char *str) { static int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *t, int allow_timezone_offset) { if (t == NULL) { - time_t now_t; - time(&now_t); - if (OPENSSL_gmtime(&now_t, tm)) { + if (OPENSSL_posix_to_tm(time(NULL), tm)) { return 1; } return 0; diff --git a/crypto/asn1/a_utctm.c b/crypto/asn1/a_utctm.c index d8d26ff..45c4081 100644 --- a/crypto/asn1/a_utctm.c +++ b/crypto/asn1/a_utctm.c @@ -58,6 +58,7 @@ #include <openssl/bytestring.h> #include <openssl/err.h> #include <openssl/mem.h> +#include <openssl/time.h> #include <string.h> #include <time.h> @@ -98,14 +99,14 @@ int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str) { return 1; } -ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t) { - return ASN1_UTCTIME_adj(s, t, 0, 0); +ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, int64_t posix_time) { + return ASN1_UTCTIME_adj(s, posix_time, 0, 0); } -ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t, int offset_day, +ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, int64_t posix_time, int offset_day, long offset_sec) { struct tm data; - if (!OPENSSL_gmtime(&t, &data)) { + if (!OPENSSL_posix_to_tm(posix_time, &data)) { return NULL; } @@ -151,7 +152,7 @@ int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t) { return -2; } - if (!OPENSSL_gmtime(&t, &ttm)) { + if (!OPENSSL_posix_to_tm(t, &ttm)) { return -2; } diff --git a/crypto/asn1/asn1_test.cc b/crypto/asn1/asn1_test.cc index 3bb7b34..47d2356 100644 --- a/crypto/asn1/asn1_test.cc +++ b/crypto/asn1/asn1_test.cc @@ -922,7 +922,7 @@ static std::string ASN1StringToStdString(const ASN1_STRING *str) { ASN1_STRING_get0_data(str) + ASN1_STRING_length(str)); } -static bool ASN1Time_check_time_t(const ASN1_TIME *s, time_t t) { +static bool ASN1Time_check_posix(const ASN1_TIME *s, int64_t t) { struct tm stm, ttm; int day, sec; @@ -940,7 +940,7 @@ static bool ASN1Time_check_time_t(const ASN1_TIME *s, time_t t) { default: return false; } - if (!OPENSSL_gmtime(&t, &ttm) || + if (!OPENSSL_posix_to_tm(t, &ttm) || !OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm)) { return false; } @@ -964,37 +964,35 @@ static std::string PrintStringToBIO(const ASN1_STRING *str, TEST(ASN1Test, SetTime) { static const struct { - time_t time; + int64_t time; const char *generalized; const char *utc; const char *printed; } kTests[] = { - {-631152001, "19491231235959Z", nullptr, "Dec 31 23:59:59 1949 GMT"}, - {-631152000, "19500101000000Z", "500101000000Z", - "Jan 1 00:00:00 1950 GMT"}, - {0, "19700101000000Z", "700101000000Z", "Jan 1 00:00:00 1970 GMT"}, - {981173106, "20010203040506Z", "010203040506Z", "Feb 3 04:05:06 2001 GMT"}, - {951804000, "20000229060000Z", "000229060000Z", "Feb 29 06:00:00 2000 GMT"}, - // NASA says this is the correct time for posterity. - {-16751025, "19690621025615Z", "690621025615Z", "Jun 21 02:56:15 1969 GMT"}, - // -1 is sometimes used as an error value. Ensure we correctly handle it. - {-1, "19691231235959Z", "691231235959Z", "Dec 31 23:59:59 1969 GMT"}, -#if defined(OPENSSL_64_BIT) - // TODO(https://crbug.com/boringssl/416): These cases overflow 32-bit - // |time_t| and do not consistently work on 32-bit platforms. For now, - // disable the tests on 32-bit. Re-enable them once the bug is fixed. - {2524607999, "20491231235959Z", "491231235959Z", - "Dec 31 23:59:59 2049 GMT"}, - {2524608000, "20500101000000Z", nullptr, "Jan 1 00:00:00 2050 GMT"}, - // Test boundary conditions. - {-62167219200, "00000101000000Z", nullptr, "Jan 1 00:00:00 0 GMT"}, - {-62167219201, nullptr, nullptr, nullptr}, - {253402300799, "99991231235959Z", nullptr, "Dec 31 23:59:59 9999 GMT"}, - {253402300800, nullptr, nullptr, nullptr}, -#endif + {-631152001, "19491231235959Z", nullptr, "Dec 31 23:59:59 1949 GMT"}, + {-631152000, "19500101000000Z", "500101000000Z", + "Jan 1 00:00:00 1950 GMT"}, + {0, "19700101000000Z", "700101000000Z", "Jan 1 00:00:00 1970 GMT"}, + {981173106, "20010203040506Z", "010203040506Z", + "Feb 3 04:05:06 2001 GMT"}, + {951804000, "20000229060000Z", "000229060000Z", + "Feb 29 06:00:00 2000 GMT"}, + // NASA says this is the correct time for posterity. + {-16751025, "19690621025615Z", "690621025615Z", + "Jun 21 02:56:15 1969 GMT"}, + // -1 is sometimes used as an error value. Ensure we correctly handle it. + {-1, "19691231235959Z", "691231235959Z", "Dec 31 23:59:59 1969 GMT"}, + {2524607999, "20491231235959Z", "491231235959Z", + "Dec 31 23:59:59 2049 GMT"}, + {2524608000, "20500101000000Z", nullptr, "Jan 1 00:00:00 2050 GMT"}, + // Test boundary conditions. + {-62167219200, "00000101000000Z", nullptr, "Jan 1 00:00:00 0 GMT"}, + {-62167219201, nullptr, nullptr, nullptr}, + {253402300799, "99991231235959Z", nullptr, "Dec 31 23:59:59 9999 GMT"}, + {253402300800, nullptr, nullptr, nullptr}, }; for (const auto &t : kTests) { - time_t tt; + int64_t tt; SCOPED_TRACE(t.time); bssl::UniquePtr<ASN1_UTCTIME> utc(ASN1_UTCTIME_set(nullptr, t.time)); @@ -1002,8 +1000,8 @@ TEST(ASN1Test, SetTime) { ASSERT_TRUE(utc); EXPECT_EQ(V_ASN1_UTCTIME, ASN1_STRING_type(utc.get())); EXPECT_EQ(t.utc, ASN1StringToStdString(utc.get())); - EXPECT_TRUE(ASN1Time_check_time_t(utc.get(), t.time)); - EXPECT_EQ(ASN1_TIME_to_time_t(utc.get(), &tt), 1); + EXPECT_TRUE(ASN1Time_check_posix(utc.get(), t.time)); + EXPECT_EQ(ASN1_TIME_to_posix(utc.get(), &tt), 1); EXPECT_EQ(tt, t.time); EXPECT_EQ(PrintStringToBIO(utc.get(), &ASN1_UTCTIME_print), t.printed); EXPECT_EQ(PrintStringToBIO(utc.get(), &ASN1_TIME_print), t.printed); @@ -1017,8 +1015,8 @@ TEST(ASN1Test, SetTime) { ASSERT_TRUE(generalized); EXPECT_EQ(V_ASN1_GENERALIZEDTIME, ASN1_STRING_type(generalized.get())); EXPECT_EQ(t.generalized, ASN1StringToStdString(generalized.get())); - EXPECT_TRUE(ASN1Time_check_time_t(generalized.get(), t.time)); - EXPECT_EQ(ASN1_TIME_to_time_t(generalized.get(), &tt), 1); + EXPECT_TRUE(ASN1Time_check_posix(generalized.get(), t.time)); + EXPECT_EQ(ASN1_TIME_to_posix(generalized.get(), &tt), 1); EXPECT_EQ(tt, t.time); EXPECT_EQ( PrintStringToBIO(generalized.get(), &ASN1_GENERALIZEDTIME_print), @@ -1029,7 +1027,7 @@ TEST(ASN1Test, SetTime) { EXPECT_FALSE(generalized); } - bssl::UniquePtr<ASN1_TIME> choice(ASN1_TIME_set(nullptr, t.time)); + bssl::UniquePtr<ASN1_TIME> choice(ASN1_TIME_set_posix(nullptr, t.time)); if (t.generalized) { ASSERT_TRUE(choice); if (t.utc) { @@ -1039,8 +1037,8 @@ TEST(ASN1Test, SetTime) { EXPECT_EQ(V_ASN1_GENERALIZEDTIME, ASN1_STRING_type(choice.get())); EXPECT_EQ(t.generalized, ASN1StringToStdString(choice.get())); } - EXPECT_TRUE(ASN1Time_check_time_t(choice.get(), t.time)); - EXPECT_EQ(ASN1_TIME_to_time_t(choice.get(), &tt), 1); + EXPECT_TRUE(ASN1Time_check_posix(choice.get(), t.time)); + EXPECT_EQ(ASN1_TIME_to_posix(choice.get(), &tt), 1); EXPECT_EQ(tt, t.time); } else { EXPECT_FALSE(choice); |