summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorEmilia Kasper <emilia@openssl.org>2017-02-17 19:00:15 +0100
committerDr. Matthias St. Pierre <Matthias.St.Pierre@ncp-e.com>2018-05-05 21:30:39 +0200
commit414d19d0341407b211c64729df37889e2c572e12 (patch)
tree3a052531d8a685bc30371027602d26756ad45c3b /test
parent29627a364be80f8c30fe7824bc3642d43d7e2c0a (diff)
X509 time: tighten validation per RFC 5280
- Reject fractional seconds - Reject offsets - Check that the date/time digits are in valid range. - Add documentation for X509_cmp_time GH issue 2620 Backported from 80770da39e Reviewed-by: Rich Salz <rsalz@openssl.org> Reviewed-by: Matthias St. Pierre <Matthias.St.Pierre@ncp-e.com> (Merged from https://github.com/openssl/openssl/pull/6181)
Diffstat (limited to 'test')
-rw-r--r--test/build.info6
-rw-r--r--test/recipes/60-test_x509_time.t12
-rw-r--r--test/x509_time_test.c212
3 files changed, 229 insertions, 1 deletions
diff --git a/test/build.info b/test/build.info
index 1c11ae1350..dcb398517c 100644
--- a/test/build.info
+++ b/test/build.info
@@ -18,7 +18,7 @@ IF[{- !$disabled{tests} -}]
dtlsv1listentest ct_test threadstest afalgtest d2i_test \
ssl_test_ctx_test ssl_test x509aux cipherlist_test asynciotest \
bioprinttest sslapitest dtlstest sslcorrupttest bio_enc_test \
- ocspapitest fatalerrtest
+ ocspapitest fatalerrtest x509_time_test
SOURCE[versions]=versions.c
INCLUDE[versions]=../include
@@ -297,6 +297,10 @@ IF[{- !$disabled{tests} -}]
INCLUDE[bio_enc_test]=../include
DEPEND[bio_enc_test]=../libcrypto
+ SOURCE[x509_time_test]=x509_time_test.c testutil.c
+ INCLUDE[x509_time_test]=.. ../include
+ DEPEND[x509_time_test]=../libcrypto
+
IF[{- !$disabled{shared} -}]
PROGRAMS_NO_INST=shlibloadtest
SOURCE[shlibloadtest]=shlibloadtest.c
diff --git a/test/recipes/60-test_x509_time.t b/test/recipes/60-test_x509_time.t
new file mode 100644
index 0000000000..8b311ad314
--- /dev/null
+++ b/test/recipes/60-test_x509_time.t
@@ -0,0 +1,12 @@
+#! /usr/bin/env perl
+# Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
+#
+# Licensed under the OpenSSL license (the "License"). You may not use
+# this file except in compliance with the License. You can obtain a copy
+# in the file LICENSE in the source distribution or at
+# https://www.openssl.org/source/license.html
+
+
+use OpenSSL::Test::Simple;
+
+simple_test("test_x509_time", "x509_time_test");
diff --git a/test/x509_time_test.c b/test/x509_time_test.c
new file mode 100644
index 0000000000..bc7e4c2b9c
--- /dev/null
+++ b/test/x509_time_test.c
@@ -0,0 +1,212 @@
+/*
+ * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the OpenSSL license (the "License"). You may not use
+ * this file except in compliance with the License. You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+/* Tests for X509 time functions */
+
+#include <string.h>
+#include <time.h>
+
+#include <openssl/asn1.h>
+#include <openssl/x509.h>
+#include "testutil.h"
+#include "e_os.h"
+
+typedef struct {
+ const char *data;
+ int type;
+ time_t cmp_time;
+ /* -1 if asn1_time <= cmp_time, 1 if asn1_time > cmp_time, 0 if error. */
+ int expected;
+} TESTDATA;
+
+static TESTDATA x509_cmp_tests[] = {
+ {
+ "20170217180154Z", V_ASN1_GENERALIZEDTIME,
+ /* The same in seconds since epoch. */
+ 1487354514, -1,
+ },
+ {
+ "20170217180154Z", V_ASN1_GENERALIZEDTIME,
+ /* One second more. */
+ 1487354515, -1,
+ },
+ {
+ "20170217180154Z", V_ASN1_GENERALIZEDTIME,
+ /* One second less. */
+ 1487354513, 1,
+ },
+ /* Same as UTC time. */
+ {
+ "170217180154Z", V_ASN1_UTCTIME,
+ /* The same in seconds since epoch. */
+ 1487354514, -1,
+ },
+ {
+ "170217180154Z", V_ASN1_UTCTIME,
+ /* One second more. */
+ 1487354515, -1,
+ },
+ {
+ "170217180154Z", V_ASN1_UTCTIME,
+ /* One second less. */
+ 1487354513, 1,
+ },
+ /* UTCTime from the 20th century. */
+ {
+ "990217180154Z", V_ASN1_UTCTIME,
+ /* The same in seconds since epoch. */
+ 919274514, -1,
+ },
+ {
+ "990217180154Z", V_ASN1_UTCTIME,
+ /* One second more. */
+ 919274515, -1,
+ },
+ {
+ "990217180154Z", V_ASN1_UTCTIME,
+ /* One second less. */
+ 919274513, 1,
+ },
+ /* Various invalid formats. */
+ {
+ /* No trailing Z. */
+ "20170217180154", V_ASN1_GENERALIZEDTIME, 0, 0,
+ },
+ {
+ /* No trailing Z, UTCTime. */
+ "170217180154", V_ASN1_UTCTIME, 0, 0,
+ },
+ {
+ /* No seconds. */
+ "201702171801Z", V_ASN1_GENERALIZEDTIME, 0, 0,
+ },
+ {
+ /* No seconds, UTCTime. */
+ "1702171801Z", V_ASN1_UTCTIME, 0, 0,
+ },
+ {
+ /* Fractional seconds. */
+ "20170217180154.001Z", V_ASN1_GENERALIZEDTIME, 0, 0,
+ },
+ {
+ /* Fractional seconds, UTCTime. */
+ "170217180154.001Z", V_ASN1_UTCTIME, 0, 0,
+ },
+ {
+ /* Timezone offset. */
+ "20170217180154+0100", V_ASN1_GENERALIZEDTIME, 0, 0,
+ },
+ {
+ /* Timezone offset, UTCTime. */
+ "170217180154+0100", V_ASN1_UTCTIME, 0, 0,
+ },
+ {
+ /* Extra digits. */
+ "2017021718015400Z", V_ASN1_GENERALIZEDTIME, 0, 0,
+ },
+ {
+ /* Extra digits, UTCTime. */
+ "17021718015400Z", V_ASN1_UTCTIME, 0, 0,
+ },
+ {
+ /* Non-digits. */
+ "2017021718015aZ", V_ASN1_GENERALIZEDTIME, 0, 0,
+ },
+ {
+ /* Non-digits, UTCTime. */
+ "17021718015aZ", V_ASN1_UTCTIME, 0, 0,
+ },
+ {
+ /* Trailing garbage. */
+ "20170217180154Zlongtrailinggarbage", V_ASN1_GENERALIZEDTIME, 0, 0,
+ },
+ {
+ /* Trailing garbage, UTCTime. */
+ "170217180154Zlongtrailinggarbage", V_ASN1_UTCTIME, 0, 0,
+ },
+ {
+ /* Swapped type. */
+ "20170217180154Z", V_ASN1_UTCTIME, 0, 0,
+ },
+ {
+ /* Swapped type. */
+ "170217180154Z", V_ASN1_GENERALIZEDTIME, 0, 0,
+ },
+ {
+ /* Bad type. */
+ "20170217180154Z", V_ASN1_OCTET_STRING, 0, 0,
+ },
+};
+
+static int test_x509_cmp_time(int idx)
+{
+ ASN1_TIME t;
+ int result;
+
+ memset(&t, 0, sizeof(t));
+ t.type = x509_cmp_tests[idx].type;
+ t.data = (unsigned char*)(x509_cmp_tests[idx].data);
+ t.length = strlen(x509_cmp_tests[idx].data);
+
+ result = X509_cmp_time(&t, &x509_cmp_tests[idx].cmp_time);
+ if (result != x509_cmp_tests[idx].expected) {
+ fprintf(stderr, "test_x509_cmp_time(%d) failed: expected %d, got %d\n",
+ idx, x509_cmp_tests[idx].expected, result);
+ return 0;
+ }
+ return 1;
+}
+
+static int test_x509_cmp_time_current()
+{
+ time_t now = time(NULL);
+ /* Pick a day earlier and later, relative to any system clock. */
+ ASN1_TIME *asn1_before = NULL, *asn1_after = NULL;
+ int cmp_result, failed = 0;
+
+ asn1_before = ASN1_TIME_adj(NULL, now, -1, 0);
+ asn1_after = ASN1_TIME_adj(NULL, now, 1, 0);
+
+ cmp_result = X509_cmp_time(asn1_before, NULL);
+ if (cmp_result != -1) {
+ fprintf(stderr, "test_x509_cmp_time_current failed: expected -1, got %d\n",
+ cmp_result);
+ failed = 1;
+ }
+
+ cmp_result = X509_cmp_time(asn1_after, NULL);
+ if (cmp_result != 1) {
+ fprintf(stderr, "test_x509_cmp_time_current failed: expected 1, got %d\n",
+ cmp_result);
+ failed = 1;
+ }
+
+ ASN1_TIME_free(asn1_before);
+ ASN1_TIME_free(asn1_after);
+
+ return failed == 0;
+}
+
+int main(int argc, char **argv)
+{
+ int ret = 0;
+ unsigned int idx;
+
+ if (!test_x509_cmp_time_current())
+ ret = 1;
+
+ for (idx=0 ; idx < OSSL_NELEM(x509_cmp_tests) ; ++idx) {
+ if (!test_x509_cmp_time(idx))
+ ret = 1;
+ }
+
+ if (ret == 0)
+ printf("PASS\n");
+ return ret;
+}