From d76e47914b5251a4a80d4fe692372cfdb595af7c Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Wed, 27 Mar 2013 17:54:38 +0100 Subject: [PATCH] Don't try to guess at overflowing time values on 32-bit systems Since CKA_START_DATE and CKA_END_DATE are the only places where we want to parse out times, and these are optional, just leave blank if the time overflows what libc can handle on a 32-bit system. https://bugs.freedesktop.org/show_bug.cgi?id=62825 --- build/certs/Makefile.am | 3 ++ build/certs/distant-end-date.der | Bin 0 -> 366 bytes common/asn1.c | 6 ++-- trust/builder.c | 5 +-- trust/tests/test-builder.c | 71 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 build/certs/distant-end-date.der diff --git a/build/certs/Makefile.am b/build/certs/Makefile.am index 0a46b56..4231591 100644 --- a/build/certs/Makefile.am +++ b/build/certs/Makefile.am @@ -45,3 +45,6 @@ build-self-signed: openssl req -new -x509 -outform DER -out self-signed-with-ku.der \ -newkey rsa -keyout /dev/null -nodes -subj /CN=self-signed-with-ku.example.com \ -config with-ku.conf -set_serial 888 -extensions v3_ca + openssl req -new -x509 -outform DER -out distant-end-date.der \ + -newkey rsa:512 -keyout /dev/null -nodes -subj /CN=far-in-the-future.example.com \ + -config with-ku.conf -set_serial 999 -extensions v3_ca -days 20000 diff --git a/build/certs/distant-end-date.der b/build/certs/distant-end-date.der new file mode 100644 index 0000000000000000000000000000000000000000..1b3fd471c63dab331058bbeeac984e54cf03bb93 GIT binary patch literal 366 zcmXqLV$3pVViZ}x%*4pV#KipEfR~L^tIebBJ1-+6D=UM6hM}5)3LA4M3$w6nT4IrI zW}a?IMyhUFX-R2Os$ObEVs1fBs$Oz_u7QF$uc5Jlv5~o&?u%GDpVqkgC>6?N49&y6n4srXLoYHj{~&k%4hB&_@PJKtIW8i>zcld6=(Ce#&ChH2nP)09oj3pa1{> literal 0 HcmV?d00001 diff --git a/common/asn1.c b/common/asn1.c index 44f96eb..c98d959 100644 --- a/common/asn1.c +++ b/common/asn1.c @@ -469,9 +469,9 @@ when_and_offset_to_time_t (struct tm *when, { time_t timet; - /* In order to work with 32 bit time_t. */ - if (sizeof (time_t) <= 4 && when->tm_year >= 2038) { - timet = (time_t)2145914603; /* 2037-12-31 23:23:23 */ + /* A 32-bit time, cannot represent this time */ + if (sizeof (time_t) <= 4 && when->tm_year >= 138) { + return -1; /* Convert to seconds since epoch */ } else { diff --git a/trust/builder.c b/trust/builder.c index 53201ed..b23d018 100644 --- a/trust/builder.c +++ b/trust/builder.c @@ -257,14 +257,12 @@ calc_date (node_asn *node, ret = asn1_read_value (node, sub, buf, &len); return_val_if_fail (ret == ASN1_SUCCESS, false); timet = p11_asn1_parse_general (buf, &when); - return_val_if_fail (timet >= 0, false); } else if (strcmp (buf, "utcTime") == 0) { len = sizeof (buf) - 1; ret = asn1_read_value (node, sub, buf, &len); return_val_if_fail (ret == ASN1_SUCCESS, false); timet = p11_asn1_parse_utc (buf, &when); - return_val_if_fail (timet >= 0, false); } else { return_val_if_reached (false); @@ -272,6 +270,9 @@ calc_date (node_asn *node, free (sub); + if (timet < 0) + return false; + assert (sizeof (date->year) == 4); snprintf ((char *)buf, 5, "%04d", 1900 + when.tm_year); memcpy (date->year, buf, 4); diff --git a/trust/tests/test-builder.c b/trust/tests/test-builder.c index 3212bac..723a251 100644 --- a/trust/tests/test-builder.c +++ b/trust/tests/test-builder.c @@ -514,6 +514,76 @@ test_build_extension (CuTest *cu) teardown (cu); } +/* This certificate has and end date in 2067 */ +static const unsigned char cert_distant_end_date[] = { + 0x30, 0x82, 0x01, 0x6a, 0x30, 0x82, 0x01, 0x14, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x02, 0x03, + 0xe7, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, + 0x30, 0x28, 0x31, 0x26, 0x30, 0x24, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x1d, 0x66, 0x61, 0x72, + 0x2d, 0x69, 0x6e, 0x2d, 0x74, 0x68, 0x65, 0x2d, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2e, 0x65, + 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x20, 0x17, 0x0d, 0x31, 0x33, + 0x30, 0x33, 0x32, 0x37, 0x31, 0x36, 0x34, 0x39, 0x33, 0x33, 0x5a, 0x18, 0x0f, 0x32, 0x30, 0x36, + 0x37, 0x31, 0x32, 0x32, 0x39, 0x31, 0x36, 0x34, 0x39, 0x33, 0x33, 0x5a, 0x30, 0x28, 0x31, 0x26, + 0x30, 0x24, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x1d, 0x66, 0x61, 0x72, 0x2d, 0x69, 0x6e, 0x2d, + 0x74, 0x68, 0x65, 0x2d, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, + 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x5c, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, + 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x4b, 0x00, 0x30, 0x48, 0x02, 0x41, 0x00, 0xe2, + 0x2d, 0x35, 0x70, 0x75, 0xc0, 0x07, 0x56, 0x40, 0x7d, 0x63, 0xbc, 0xd2, 0x60, 0xb3, 0xcf, 0xb8, + 0x3d, 0x27, 0x6e, 0x10, 0xcd, 0x42, 0x50, 0x51, 0x9d, 0x79, 0x30, 0x79, 0x5a, 0xe3, 0xc3, 0x51, + 0x38, 0x85, 0x4c, 0xb4, 0x91, 0xd9, 0xe6, 0x8d, 0x69, 0x6a, 0xd4, 0x9c, 0x1c, 0x49, 0xc2, 0x25, + 0x2a, 0xc9, 0x2b, 0xf2, 0xf4, 0x8e, 0x8a, 0x3f, 0x8b, 0x4c, 0x97, 0xc3, 0x16, 0x96, 0x99, 0x02, + 0x03, 0x01, 0x00, 0x01, 0xa3, 0x26, 0x30, 0x24, 0x30, 0x22, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, + 0x1b, 0x30, 0x19, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x06, 0x08, 0x2b, + 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x04, 0x06, 0x03, 0x2a, 0x03, 0x04, 0x30, 0x0d, 0x06, 0x09, + 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x41, 0x00, 0xc2, 0x83, + 0x27, 0x32, 0x80, 0x74, 0x73, 0xe2, 0xa3, 0x92, 0xaa, 0x7c, 0xd8, 0x50, 0xf4, 0x61, 0x50, 0xb1, + 0x63, 0x9e, 0x29, 0xef, 0x38, 0x1d, 0xc0, 0x55, 0x20, 0x0f, 0x7e, 0xe9, 0x1f, 0xa1, 0x54, 0x1a, + 0x5f, 0x8c, 0x26, 0x1b, 0x66, 0x96, 0x0e, 0x64, 0x52, 0x1c, 0x00, 0x96, 0xfb, 0x81, 0x77, 0xa2, + 0x3a, 0x1d, 0x49, 0x0c, 0x03, 0xd5, 0x19, 0xf2, 0x6a, 0x01, 0x29, 0x31, 0xfb, 0xf5, +}; + +static void +test_build_distant_end_date (CuTest *cu) +{ + CK_ATTRIBUTE input[] = { + { CKA_CLASS, &certificate, sizeof (certificate) }, + { CKA_CERTIFICATE_TYPE, &x509, sizeof (x509) }, + { CKA_VALUE, (void *)cert_distant_end_date, sizeof (cert_distant_end_date) }, + { CKA_INVALID }, + }; + + CK_ATTRIBUTE expected[] = { + { CKA_END_DATE, }, + { CKA_START_DATE, "20130327", 8 }, + { CKA_INVALID }, + }; + + CK_ATTRIBUTE *attrs; + CK_RV rv; + + setup (cu); + + /* + * On a 32-bit system, the end date will be too big to compute with + * libc. So it'll be empty, since this is an optional field. + */ + if (sizeof (time_t) <= 4) { + expected[0].pValue = ""; + expected[0].ulValueLen = 0; + } else { + expected[0].pValue = "20671229"; + expected[0].ulValueLen = 8; + } + + attrs = NULL; + rv = p11_builder_build (test.builder, test.index, &attrs, p11_attrs_dup (input)); + CuAssertIntEquals (cu, CKR_OK, rv); + + test_check_attrs (cu, expected, attrs); + p11_attrs_free (attrs); + + teardown (cu); +} + static void test_create_not_settable (CuTest *cu) { @@ -1666,6 +1736,7 @@ main (void) SUITE_ADD_TEST (suite, test_build_certificate_no_type); SUITE_ADD_TEST (suite, test_build_certificate_bad_type); SUITE_ADD_TEST (suite, test_build_extension); + SUITE_ADD_TEST (suite, test_build_distant_end_date); SUITE_ADD_TEST (suite, test_create_not_settable); SUITE_ADD_TEST (suite, test_create_but_loadable); SUITE_ADD_TEST (suite, test_create_unsupported); -- 1.8.1.4