Files
openssl/test/x509_time_test.c
Bob Beck 4036f4b0e3
Some checks failed
GitHub CI / check_update (push) Has been cancelled
GitHub CI / check_docs (push) Has been cancelled
GitHub CI / check-c99 (push) Has been cancelled
GitHub CI / basic_gcc (push) Has been cancelled
GitHub CI / basic_clang (push) Has been cancelled
GitHub CI / linux-arm64 (push) Has been cancelled
GitHub CI / linux-x86 (push) Has been cancelled
GitHub CI / freebsd-x86_64 (push) Has been cancelled
GitHub CI / minimal (push) Has been cancelled
GitHub CI / no-deprecated (push) Has been cancelled
GitHub CI / no-shared-ubuntu (push) Has been cancelled
GitHub CI / no-shared-macos (push) Has been cancelled
GitHub CI / non-caching (push) Has been cancelled
GitHub CI / address_ub_sanitizer (push) Has been cancelled
GitHub CI / fuzz_tests (push) Has been cancelled
GitHub CI / memory_sanitizer (push) Has been cancelled
GitHub CI / threads_sanitizer (push) Has been cancelled
GitHub CI / enable_non-default_options (push) Has been cancelled
GitHub CI / full_featured (push) Has been cancelled
GitHub CI / no-legacy (push) Has been cancelled
GitHub CI / legacy (push) Has been cancelled
GitHub CI / out-of-readonly-source-and-install-ubuntu (push) Has been cancelled
GitHub CI / out-of-readonly-source-and-install-macos (push) Has been cancelled
GitHub CI / external-tests-misc (push) Has been cancelled
GitHub CI / external-tests-oqs-provider (push) Has been cancelled
GitHub CI / external-tests-pkcs11-provider (push) Has been cancelled
GitHub CI / external-tests-pyca (3.9) (push) Has been cancelled
Compiler Zoo CI / gcc (gcc-10) (push) Has been cancelled
Compiler Zoo CI / gcc (gcc-11) (push) Has been cancelled
Compiler Zoo CI / gcc (gcc-12) (push) Has been cancelled
Compiler Zoo CI / gcc (gcc-13) (push) Has been cancelled
Compiler Zoo CI / gcc (gcc-14) (push) Has been cancelled
Compiler Zoo CI / gcc (gcc-9) (push) Has been cancelled
Compiler Zoo CI / clang (clang-11) (push) Has been cancelled
Compiler Zoo CI / clang (clang-12) (push) Has been cancelled
Compiler Zoo CI / clang (clang-13) (push) Has been cancelled
Compiler Zoo CI / clang (clang-14) (push) Has been cancelled
Compiler Zoo CI / clang (clang-15) (push) Has been cancelled
Compiler Zoo CI / clang (clang-16) (push) Has been cancelled
Compiler Zoo CI / clang (clang-17) (push) Has been cancelled
Compiler Zoo CI / clang (clang-18) (push) Has been cancelled
Compiler Zoo CI / clang (clang-19) (push) Has been cancelled
Compiler Zoo CI / clang (clang-20) (push) Has been cancelled
Cross Compile / cross-compilation (map[arch:aarch64-linux-gnu fips:no libs:libc6-dev-arm64-cross target:linux-aarch64]) (push) Has been cancelled
Cross Compile / cross-compilation (map[arch:alpha-linux-gnu fips:no libs:libc6.1-dev-alpha-cross target:linux-alpha-gcc]) (push) Has been cancelled
Cross Compile / cross-compilation (map[arch:arm-linux-gnueabi fips:no libs:libc6-dev-armel-cross target:linux-armv4 tests:-test_includes -test_store -test_x509_store]) (push) Has been cancelled
Cross Compile / cross-compilation (map[arch:arm-linux-gnueabihf fips:no libs:libc6-dev-armhf-cross target:linux-armv4 tests:-test_includes -test_store -test_x509_store]) (push) Has been cancelled
Cross Compile / cross-compilation (map[arch:hppa-linux-gnu fips:no libs:libc6-dev-hppa-cross target:-static -O1 linux-generic32 tests:-test_includes -test_store -test_x509_store]) (push) Has been cancelled
Cross Compile / cross-compilation (map[arch:hppa-linux-gnu libs:libc6-dev-hppa-cross target:linux-generic32 tests:none]) (push) Has been cancelled
Cross Compile / cross-compilation (map[arch:i386-pc-msdosdjgpp libs:libc-djgpp-dev libwatt-djgpp-dev djgpp-utils ppa:jwt27/djgpp-toolchain target:no-threads 386 DJGPP tests:none]) (push) Has been cancelled
Cross Compile / cross-compilation (map[arch:m68k-linux-gnu fips:no libs:libc6-dev-m68k-cross target:-static -m68040 linux-latomic -Wno-stringop-overflow tests:-test_includes -test_store -test_x509_store]) (push) Has been cancelled
Cross Compile / cross-compilation (map[arch:m68k-linux-gnu libs:libc6-dev-m68k-cross target:-mcfv4e -mxgot linux-latomic -Wno-stringop-overflow no-quic tests:none]) (push) Has been cancelled
Cross Compile / cross-compilation (map[arch:mips-linux-gnu fips:no libs:libc6-dev-mips-cross target:-static linux-mips32 tests:-test_includes -test_store -test_x509_store]) (push) Has been cancelled
Cross Compile / cross-compilation (map[arch:mips-linux-gnu libs:libc6-dev-mips-cross target:linux-mips32 tests:none]) (push) Has been cancelled
Cross Compile / cross-compilation (map[arch:mips64-linux-gnuabi64 fips:no libs:libc6-dev-mips64-cross target:-static linux64-mips64]) (push) Has been cancelled
Cross Compile / cross-compilation (map[arch:mips64-linux-gnuabi64 libs:libc6-dev-mips64-cross target:linux64-mips64 tests:none]) (push) Has been cancelled
Cross Compile / cross-compilation (map[arch:mipsel-linux-gnu fips:no libs:libc6-dev-mipsel-cross target:linux-mips32 tests:-test_includes -test_store -test_x509_store]) (push) Has been cancelled
Cross Compile / cross-compilation (map[arch:powerpc64le-linux-gnu fips:no libs:libc6-dev-ppc64el-cross target:linux-ppc64le]) (push) Has been cancelled
Cross Compile / cross-compilation (map[arch:riscv64-linux-gnu fips:no libs:libc6-dev-riscv64-cross target:linux64-riscv64]) (push) Has been cancelled
Cross Compile / cross-compilation (map[arch:s390x-linux-gnu fips:no libs:libc6-dev-s390x-cross target:linux64-s390x]) (push) Has been cancelled
Cross Compile / cross-compilation (map[arch:sh4-linux-gnu fips:no libs:libc6-dev-sh4-cross target:no-async linux-latomic tests:-test_includes -test_store -test_x509_store]) (push) Has been cancelled
Cross Compile / cross-compilation (map[arch:sparc64-linux-gnu libs:libc6-dev-sparc64-cross target:linux64-sparcv9 tests:none]) (push) Has been cancelled
Trigger docs.openssl.org deployment / trigger (push) Has been cancelled
Fuzz-checker CI / fuzz-checker (map[cc:afl-clang-fast config:enable-fuzz-afl no-module install:afl++ name:AFL]) (push) Has been cancelled
Fuzz-checker CI / fuzz-checker (map[cc:clang-18 config:enable-fuzz-libfuzzer enable-asan enable-ubsan -fno-sanitize=function -fsanitize-coverage=trace-cmp -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION extra:enable-fips enable-lms enable-ec_nistp_64_gcc_128 -fno-sanitize=al… (push) Has been cancelled
Fuzz-checker CI / fuzz-checker (map[cc:clang-18 config:enable-fuzz-libfuzzer enable-asan enable-ubsan -fno-sanitize=function install:libfuzzer-18-dev libs:--with-fuzzer-lib=/usr/lib/llvm-18/lib/libFuzzer.a --with-fuzzer-include=/usr/include/clang/18/include/fuzzer linke… (push) Has been cancelled
Perl-minimal-checker CI / perl-minimal-checker (push) Has been cancelled
Cross Compile for RISC-V Extensions / cross-compilation-riscv (map[arch:riscv64-linux-gnu fips:no libs:libc6-dev-riscv64-cross opensslcaps:rv64gc_inlineasm opensslcapsname:riscvcap qemucpu:rv64,zbb=true,zbkb=true,zknh=true,zksh=true target:-march=rv64gc_zbb_zbkb_zknh_zksh linux64-riscv64]) (push) Has been cancelled
Cross Compile for RISC-V Extensions / cross-compilation-riscv (map[arch:riscv64-linux-gnu fips:no libs:libc6-dev-riscv64-cross opensslcaps:rv64gc_v_zbb opensslcapsname:riscvcap qemucpu:rv64,v=true,vlen=128,zbb=true,zvbb=false,zvkb=false target:linux64-riscv64]) (push) Has been cancelled
Cross Compile for RISC-V Extensions / cross-compilation-riscv (map[arch:riscv64-linux-gnu fips:no libs:libc6-dev-riscv64-cross opensslcaps:rv64gc_v_zvkb_zvbc opensslcapsname:riscvcap qemucpu:rv64,v=true,vlen=128,zvbb=true,zvbc=true,zvkg=false target:linux64-riscv64]) (push) Has been cancelled
Cross Compile for RISC-V Extensions / cross-compilation-riscv (map[arch:riscv64-linux-gnu fips:no libs:libc6-dev-riscv64-cross opensslcaps:rv64gc_v_zvkg opensslcapsname:riscvcap qemucpu:rv64,v=true,vlen=128,zvkg=true,zvbb=false target:linux64-riscv64]) (push) Has been cancelled
Cross Compile for RISC-V Extensions / cross-compilation-riscv (map[arch:riscv64-linux-gnu fips:no libs:libc6-dev-riscv64-cross opensslcaps:rv64gc_v_zvkned opensslcapsname:riscvcap qemucpu:rv64,v=true,vlen=128,zvkned=true,zvbb=false,zvkb=false,zvkg=false target:linux64-riscv64]) (push) Has been cancelled
Cross Compile for RISC-V Extensions / cross-compilation-riscv (map[arch:riscv64-linux-gnu fips:no libs:libc6-dev-riscv64-cross opensslcaps:rv64gc_zba_zbb_zbc_zbs_zbkb_zbkc_zbkx_zknd_zkne_zknh_zksed_zksh_zkr_zkt_v_zvbb_zvbc_zvkb_zvkg_zvkned_zvknha_zvknhb_zvksed_zvksh opensslcapsname:riscvca… (push) Has been cancelled
Cross Compile for RISC-V Extensions / cross-compilation-riscv (map[arch:riscv64-linux-gnu fips:no libs:libc6-dev-riscv64-cross opensslcaps:rv64gc_zbb_zbc_zbkb_zknd_zkne opensslcapsname:riscvcap qemucpu:rv64,zbb=true,zbc=true,zbkb=true,zknd=true,zkne=true target:linux64-riscv64]) (push) Has been cancelled
Cross Compile for RISC-V Extensions / cross-compilation-riscv (map[arch:riscv64-linux-gnu fips:no libs:libc6-dev-riscv64-cross opensslcaps:rv64gc_zbc opensslcapsname:riscvcap qemucpu:rv64,zbc=true,zbb=false,zbkb=false target:linux64-riscv64]) (push) Has been cancelled
Cross Compile for RISC-V Extensions / cross-compilation-riscv (map[arch:riscv64-linux-gnu fips:no libs:libc6-dev-riscv64-cross opensslcaps:rv64gc_zbc_zbb opensslcapsname:riscvcap qemucpu:rv64,zbc=true,zbb=true,zbkb=false target:linux64-riscv64]) (push) Has been cancelled
Run-checker CI / run-checker (enable-trace enable-fips) (push) Has been cancelled
Run-checker CI / run-checker (no-cmp) (push) Has been cancelled
Run-checker CI / run-checker (no-cms) (push) Has been cancelled
Run-checker CI / run-checker (no-default-thread-pool) (push) Has been cancelled
Run-checker CI / run-checker (no-dgram) (push) Has been cancelled
Run-checker CI / run-checker (no-dh) (push) Has been cancelled
Run-checker CI / run-checker (no-dtls) (push) Has been cancelled
Run-checker CI / run-checker (no-ec) (push) Has been cancelled
Run-checker CI / run-checker (no-ecx) (push) Has been cancelled
Run-checker CI / run-checker (no-http) (push) Has been cancelled
Run-checker CI / run-checker (no-legacy) (push) Has been cancelled
Run-checker CI / run-checker (no-ml-dsa) (push) Has been cancelled
Run-checker CI / run-checker (no-ml-kem) (push) Has been cancelled
Run-checker CI / run-checker (no-quic) (push) Has been cancelled
Run-checker CI / run-checker (no-sm2) (push) Has been cancelled
Run-checker CI / run-checker (no-sock) (push) Has been cancelled
Run-checker CI / run-checker (no-stdio) (push) Has been cancelled
Run-checker CI / run-checker (no-thread-pool) (push) Has been cancelled
Run-checker CI / run-checker (no-threads) (push) Has been cancelled
Run-checker CI / run-checker (no-tls) (push) Has been cancelled
Run-checker CI / run-checker (no-tls1_2) (push) Has been cancelled
Run-checker CI / run-checker (no-tls1_3) (push) Has been cancelled
Run-checker CI / run-checker (no-ui) (push) Has been cancelled
Run-checker merge / run-checker (enable-asan enable-ubsan no-shared no-asm -DOPENSSL_SMALL_FOOTPRINT -fno-sanitize=function) (push) Has been cancelled
Run-checker merge / run-checker (enable-pie) (push) Has been cancelled
Run-checker merge / run-checker (enable-ubsan no-asm -DOPENSSL_SMALL_FOOTPRINT -fno-sanitize=function) (push) Has been cancelled
Run-checker merge / run-checker (enable-weak-ssl-ciphers) (push) Has been cancelled
Run-checker merge / run-checker (enable-zlib) (push) Has been cancelled
Run-checker merge / run-checker (no-dso) (push) Has been cancelled
Run-checker merge / run-checker (no-ec2m enable-fips) (push) Has been cancelled
Run-checker merge / run-checker (no-err) (push) Has been cancelled
Run-checker merge / run-checker (no-filenames) (push) Has been cancelled
Run-checker merge / run-checker (no-integrity-only-ciphers) (push) Has been cancelled
Run-checker merge / run-checker (no-module) (push) Has been cancelled
Run-checker merge / run-checker (no-ocsp) (push) Has been cancelled
Run-checker merge / run-checker (no-pinshared) (push) Has been cancelled
Run-checker merge / run-checker (no-shared) (push) Has been cancelled
Run-checker merge / run-checker (no-srp) (push) Has been cancelled
Run-checker merge / run-checker (no-srtp) (push) Has been cancelled
Run-checker merge / run-checker (no-ts) (push) Has been cancelled
Run-checker merge / jitter (push) Has been cancelled
Run-checker merge / threads_sanitizer_atomic_fallback (push) Has been cancelled
Windows GitHub CI / shared (map[arch:amd64 config:enable-lms enable-fips no-thread-pool no-quic os:windows-2025 vcvars:C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat]) (push) Has been cancelled
Windows GitHub CI / shared (map[arch:amd64 config:enable-lms enable-fips os:windows-2022 vcvars:C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat]) (push) Has been cancelled
Windows GitHub CI / shared (map[arch:x86 config:no-fips enable-lms os:windows-2022 vcvars:C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars32.bat]) (push) Has been cancelled
Windows GitHub CI / plain (push) Has been cancelled
Windows GitHub CI / minimal (push) Has been cancelled
Windows GitHub CI / cygwin (windows-2022, map[arch:win64 config:-DCMAKE_C_COMPILER=gcc --strict-warnings enable-demos no-fips]) (push) Has been cancelled
Windows Compression GitHub CI / zstd (push) Has been cancelled
Windows Compression GitHub CI / brotli (push) Has been cancelled
Add new public API for checking certificate times.
Fixes: #1631

This changes the previously internal ossl_x509_check_certificate_times()
to be the public X509_check_certificate_times(). It adds documentation
for the new function and marks X509_cmp_time, X509_cmp_timeframe,
and X509_cmp_current_time as deprecated in 4.0, as discussed in #1631.

Since the function is now public, we can replace the remaining
uses of deprecated stuff with this function.

Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Saša Nedvědický <sashan@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/29152)
2025-12-18 13:34:58 -05:00

771 lines
19 KiB
C

/*
* Copyright 2017-2025 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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 "internal/nelem.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;
typedef struct {
const char *data;
/* 0 for check-only mode, 1 for set-string mode */
int set_string;
/* 0 for error, 1 if succeed */
int expected;
/*
* The following 2 fields are ignored if set_string field is set to '0'
* (in check only mode).
*
* But they can still be ignored explicitly in set-string mode by:
* setting -1 to expected_type and setting NULL to expected_string.
*
* It's useful in a case of set-string mode but the expected result
* is a 'parsing error'.
*/
int expected_type;
const char *expected_string;
} TESTDATA_FORMAT;
/*
* Actually, the "loose" mode has been tested in
* those time-compare-cases, so we may not test it again.
*/
static TESTDATA_FORMAT x509_format_tests[] = {
/* GeneralizedTime */
{
/* good format, check only */
"20170217180105Z",
0,
1,
-1,
NULL,
},
{
/* not leap year, check only */
"20170229180105Z",
0,
0,
-1,
NULL,
},
{
/* leap year, check only */
"20160229180105Z",
0,
1,
-1,
NULL,
},
{
/* SS is missing, check only */
"201702171801Z",
0,
0,
-1,
NULL,
},
{
/* fractional seconds, check only */
"20170217180105.001Z",
0,
0,
-1,
NULL,
},
{
/* timezone, check only */
"20170217180105+0800",
0,
0,
-1,
NULL,
},
{
/* SS is missing, set string */
"201702171801Z",
1,
0,
-1,
NULL,
},
{
/* fractional seconds, set string */
"20170217180105.001Z",
1,
0,
-1,
NULL,
},
{
/* timezone, set string */
"20170217180105+0800",
1,
0,
-1,
NULL,
},
{
/* good format, check returned 'turned' string */
"20170217180154Z",
1,
1,
V_ASN1_UTCTIME,
"170217180154Z",
},
{
/* good format, check returned string */
"20510217180154Z",
1,
1,
V_ASN1_GENERALIZEDTIME,
"20510217180154Z",
},
{
/* good format but out of UTC range, check returned string */
"19230419180154Z",
1,
1,
V_ASN1_GENERALIZEDTIME,
"19230419180154Z",
},
/* UTC */
{
/* SS is missing, check only */
"1702171801Z",
0,
0,
-1,
NULL,
},
{
/* not leap year, check only */
"050229180101Z",
0,
0,
-1,
NULL,
},
{
/* leap year, check only */
"040229180101Z",
0,
1,
-1,
NULL,
},
{
/* timezone, check only */
"170217180154+0800",
0,
0,
-1,
NULL,
},
{
/* SS is missing, set string */
"1702171801Z",
1,
0,
-1,
NULL,
},
{
/* timezone, set string */
"170217180154+0800",
1,
0,
-1,
NULL,
},
{
/* 2017, good format, check returned string */
"170217180154Z",
1,
1,
V_ASN1_UTCTIME,
"170217180154Z",
},
{
/* 1998, good format, check returned string */
"981223180154Z",
1,
1,
V_ASN1_UTCTIME,
"981223180154Z",
},
};
#if !defined(OPENSSL_NO_DEPRECATED_4_0)
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 = (int)strlen(x509_cmp_tests[idx].data);
t.flags = 0;
OSSL_BEGIN_ALLOW_DEPRECATED
result = X509_cmp_time(&t, &x509_cmp_tests[idx].cmp_time);
OSSL_END_ALLOW_DEPRECATED
if (!TEST_int_eq(result, x509_cmp_tests[idx].expected)) {
TEST_info("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(void)
{
time_t now = time(NULL);
/* Pick a day earlier and later, relative to any system clock. */
ASN1_TIME *asn1_before = NULL, *asn1_after = NULL, *asn1_now = NULL;
int cmp_result, failed = 0;
asn1_before = ASN1_TIME_adj(NULL, now, -1, 0);
asn1_after = ASN1_TIME_adj(NULL, now, 1, 0);
asn1_now = ASN1_TIME_adj(NULL, now, 0, 0);
/* X509_cmp_time is expected to return -1 for equal */
OSSL_BEGIN_ALLOW_DEPRECATED
cmp_result = X509_cmp_time(asn1_now, &now);
if (!TEST_int_eq(cmp_result, -1))
failed = 1;
cmp_result = X509_cmp_time(asn1_before, &now);
if (!TEST_int_eq(cmp_result, -1))
failed = 1;
cmp_result = X509_cmp_time(asn1_after, &now);
if (!TEST_int_eq(cmp_result, 1))
failed = 1;
OSSL_END_ALLOW_DEPRECATED
ASN1_TIME_free(asn1_before);
ASN1_TIME_free(asn1_after);
ASN1_TIME_free(asn1_now);
return failed == 0;
}
static int test_X509_cmp_timeframe_vpm(const X509_VERIFY_PARAM *vpm,
ASN1_TIME *asn1_before,
ASN1_TIME *asn1_mid,
ASN1_TIME *asn1_after)
{
int always_0 = vpm != NULL
&& (X509_VERIFY_PARAM_get_flags(vpm) & X509_V_FLAG_USE_CHECK_TIME) == 0
&& (X509_VERIFY_PARAM_get_flags(vpm) & X509_V_FLAG_NO_CHECK_TIME) != 0;
OSSL_BEGIN_ALLOW_DEPRECATED
return asn1_before != NULL && asn1_mid != NULL && asn1_after != NULL
&& TEST_int_eq(X509_cmp_timeframe(vpm, asn1_before, asn1_after), 0)
&& TEST_int_eq(X509_cmp_timeframe(vpm, asn1_before, NULL), 0)
&& TEST_int_eq(X509_cmp_timeframe(vpm, NULL, asn1_after), 0)
&& TEST_int_eq(X509_cmp_timeframe(vpm, NULL, NULL), 0)
&& TEST_int_eq(X509_cmp_timeframe(vpm, asn1_after, asn1_after),
always_0 ? 0 : -1)
&& TEST_int_eq(X509_cmp_timeframe(vpm, asn1_before, asn1_before),
always_0 ? 0 : 1)
&& TEST_int_eq(X509_cmp_timeframe(vpm, asn1_after, asn1_before),
always_0 ? 0 : 1);
OSSL_END_ALLOW_DEPRECATED
}
static int test_X509_cmp_timeframe(void)
{
time_t now = time(NULL);
ASN1_TIME *asn1_mid = ASN1_TIME_adj(NULL, now, 0, 0);
/* Pick a day earlier and later, relative to any system clock. */
ASN1_TIME *asn1_before = ASN1_TIME_adj(NULL, now, -1, 0);
ASN1_TIME *asn1_after = ASN1_TIME_adj(NULL, now, 1, 0);
X509_VERIFY_PARAM *vpm = X509_VERIFY_PARAM_new();
int res = 0;
if (vpm == NULL)
goto finish;
res = test_X509_cmp_timeframe_vpm(NULL, asn1_before, asn1_mid, asn1_after)
&& test_X509_cmp_timeframe_vpm(vpm, asn1_before, asn1_mid, asn1_after);
X509_VERIFY_PARAM_set_time(vpm, now);
res = res
&& test_X509_cmp_timeframe_vpm(vpm, asn1_before, asn1_mid, asn1_after)
&& X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_CHECK_TIME)
&& test_X509_cmp_timeframe_vpm(vpm, asn1_before, asn1_mid, asn1_after);
X509_VERIFY_PARAM_free(vpm);
finish:
ASN1_TIME_free(asn1_mid);
ASN1_TIME_free(asn1_before);
ASN1_TIME_free(asn1_after);
return res;
}
#endif /* !defined(OPENSSL_NO_DEPRECATED_4_0) */
static int test_x509_time(int idx)
{
ASN1_TIME *t = NULL;
int result, rv = 0;
if (x509_format_tests[idx].set_string) {
/* set-string mode */
t = ASN1_TIME_new();
if (t == NULL) {
TEST_info("test_x509_time(%d) failed: internal error\n", idx);
return 0;
}
}
result = ASN1_TIME_set_string_X509(t, x509_format_tests[idx].data);
/* time string parsing result is always checked against what's expected */
if (!TEST_int_eq(result, x509_format_tests[idx].expected)) {
TEST_info("test_x509_time(%d) failed: expected %d, got %d\n",
idx, x509_format_tests[idx].expected, result);
goto out;
}
/* if t is not NULL but expected_type is ignored(-1), it is an 'OK' case */
if (t != NULL && x509_format_tests[idx].expected_type != -1) {
if (!TEST_int_eq(t->type, x509_format_tests[idx].expected_type)) {
TEST_info("test_x509_time(%d) failed: expected_type %d, got %d\n",
idx, x509_format_tests[idx].expected_type, t->type);
goto out;
}
}
/* if t is not NULL but expected_string is NULL, it is an 'OK' case too */
if (t != NULL && x509_format_tests[idx].expected_string) {
if (!TEST_mem_eq((const char *)t->data, t->length,
x509_format_tests[idx].expected_string,
strlen(x509_format_tests[idx].expected_string))) {
TEST_info("test_x509_time(%d) failed: expected_string %s, got %.*s\n",
idx, x509_format_tests[idx].expected_string, t->length,
t->data);
goto out;
}
}
rv = 1;
out:
if (t != NULL)
ASN1_TIME_free(t);
return rv;
}
static const struct {
int y, m, d;
int yd, wd;
} day_of_week_tests[] = {
/*YYYY MM DD DoY DoW */
{ 1900, 1, 1, 0, 1 },
{ 1900, 2, 28, 58, 3 },
{ 1900, 3, 1, 59, 4 },
{ 1900, 12, 31, 364, 1 },
{ 1901, 1, 1, 0, 2 },
{ 1970, 1, 1, 0, 4 },
{ 1999, 1, 10, 9, 0 },
{ 1999, 12, 31, 364, 5 },
{ 2000, 1, 1, 0, 6 },
{ 2000, 2, 28, 58, 1 },
{ 2000, 2, 29, 59, 2 },
{ 2000, 3, 1, 60, 3 },
{ 2000, 12, 31, 365, 0 },
{ 2001, 1, 1, 0, 1 },
{ 2008, 1, 1, 0, 2 },
{ 2008, 2, 28, 58, 4 },
{ 2008, 2, 29, 59, 5 },
{ 2008, 3, 1, 60, 6 },
{ 2008, 12, 31, 365, 3 },
{ 2009, 1, 1, 0, 4 },
{ 2011, 1, 1, 0, 6 },
{ 2011, 2, 28, 58, 1 },
{ 2011, 3, 1, 59, 2 },
{ 2011, 12, 31, 364, 6 },
{ 2012, 1, 1, 0, 0 },
{ 2019, 1, 2, 1, 3 },
{ 2019, 2, 2, 32, 6 },
{ 2019, 3, 2, 60, 6 },
{ 2019, 4, 2, 91, 2 },
{ 2019, 5, 2, 121, 4 },
{ 2019, 6, 2, 152, 0 },
{ 2019, 7, 2, 182, 2 },
{ 2019, 8, 2, 213, 5 },
{ 2019, 9, 2, 244, 1 },
{ 2019, 10, 2, 274, 3 },
{ 2019, 11, 2, 305, 6 },
{ 2019, 12, 2, 335, 1 },
{ 2020, 1, 2, 1, 4 },
{ 2020, 2, 2, 32, 0 },
{ 2020, 3, 2, 61, 1 },
{ 2020, 4, 2, 92, 4 },
{ 2020, 5, 2, 122, 6 },
{ 2020, 6, 2, 153, 2 },
{ 2020, 7, 2, 183, 4 },
{ 2020, 8, 2, 214, 0 },
{ 2020, 9, 2, 245, 3 },
{ 2020, 10, 2, 275, 5 },
{ 2020, 11, 2, 306, 1 },
{ 2020, 12, 2, 336, 3 }
};
static int test_days(int n)
{
char d[16];
ASN1_TIME *a = NULL;
struct tm t;
int r;
BIO_snprintf(d, sizeof(d), "%04d%02d%02d050505Z",
day_of_week_tests[n].y, day_of_week_tests[n].m,
day_of_week_tests[n].d);
if (!TEST_ptr(a = ASN1_TIME_new()))
return 0;
r = TEST_true(ASN1_TIME_set_string(a, d))
&& TEST_true(ASN1_TIME_to_tm(a, &t))
&& TEST_int_eq(t.tm_yday, day_of_week_tests[n].yd)
&& TEST_int_eq(t.tm_wday, day_of_week_tests[n].wd);
ASN1_TIME_free(a);
return r;
}
#define construct_asn1_time(s, t, e) \
{ { sizeof(s) - 1, t, (unsigned char *)s, 0 }, e }
static const struct {
ASN1_TIME asn1;
const char *readable;
} x509_print_tests_rfc_822[] = {
/* Generalized Time */
construct_asn1_time("20170731222050Z", V_ASN1_GENERALIZEDTIME,
"Jul 31 22:20:50 2017 GMT"),
/* Generalized Time, no seconds */
construct_asn1_time("201707312220Z", V_ASN1_GENERALIZEDTIME,
"Bad time value"),
/* Generalized Time, fractional seconds (3 digits) */
construct_asn1_time("20170731222050.123Z", V_ASN1_GENERALIZEDTIME,
"Jul 31 22:20:50.123 2017 GMT"),
/* Generalized Time, fractional seconds (1 digit) */
construct_asn1_time("20170731222050.1Z", V_ASN1_GENERALIZEDTIME,
"Jul 31 22:20:50.1 2017 GMT"),
/* Generalized Time, fractional seconds (0 digit) */
construct_asn1_time("20170731222050.Z", V_ASN1_GENERALIZEDTIME,
"Bad time value"),
/* UTC Time */
construct_asn1_time("170731222050Z", V_ASN1_UTCTIME,
"Jul 31 22:20:50 2017 GMT"),
/* UTC Time, no seconds */
construct_asn1_time("1707312220Z", V_ASN1_UTCTIME,
"Bad time value"),
};
static const struct {
ASN1_TIME asn1;
const char *readable;
} x509_print_tests_iso_8601[] = {
/* Generalized Time */
construct_asn1_time("20170731222050Z", V_ASN1_GENERALIZEDTIME,
"2017-07-31 22:20:50Z"),
/* Generalized Time, no seconds */
construct_asn1_time("201707312220Z", V_ASN1_GENERALIZEDTIME,
"Bad time value"),
/* Generalized Time, fractional seconds (3 digits) */
construct_asn1_time("20170731222050.123Z", V_ASN1_GENERALIZEDTIME,
"2017-07-31 22:20:50.123Z"),
/* Generalized Time, fractional seconds (1 digit) */
construct_asn1_time("20170731222050.1Z", V_ASN1_GENERALIZEDTIME,
"2017-07-31 22:20:50.1Z"),
/* Generalized Time, fractional seconds (0 digit) */
construct_asn1_time("20170731222050.Z", V_ASN1_GENERALIZEDTIME,
"Bad time value"),
/* UTC Time */
construct_asn1_time("170731222050Z", V_ASN1_UTCTIME,
"2017-07-31 22:20:50Z"),
/* UTC Time, no seconds */
construct_asn1_time("1707312220Z", V_ASN1_UTCTIME,
"Bad time value"),
};
static int test_x509_time_print_rfc_822(int idx)
{
BIO *m;
int ret = 0, rv;
char *pp;
const char *readable;
if (!TEST_ptr(m = BIO_new(BIO_s_mem())))
goto err;
rv = ASN1_TIME_print_ex(m, &x509_print_tests_rfc_822[idx].asn1, ASN1_DTFLGS_RFC822);
readable = x509_print_tests_rfc_822[idx].readable;
if (rv == 0 && !TEST_str_eq(readable, "Bad time value")) {
/* only if the test case intends to fail... */
goto err;
}
if (!TEST_int_ne(rv = BIO_get_mem_data(m, &pp), 0)
|| !TEST_int_eq(rv, (int)strlen(readable))
|| !TEST_strn_eq(pp, readable, rv))
goto err;
ret = 1;
err:
BIO_free(m);
return ret;
}
static int test_x509_time_print_iso_8601(int idx)
{
BIO *m;
int ret = 0, rv;
char *pp;
const char *readable;
if (!TEST_ptr(m = BIO_new(BIO_s_mem())))
goto err;
rv = ASN1_TIME_print_ex(m, &x509_print_tests_iso_8601[idx].asn1, ASN1_DTFLGS_ISO8601);
readable = x509_print_tests_iso_8601[idx].readable;
if (rv == 0 && !TEST_str_eq(readable, "Bad time value")) {
/* only if the test case intends to fail... */
goto err;
}
if (!TEST_int_ne(rv = BIO_get_mem_data(m, &pp), 0)
|| !TEST_int_eq(rv, (int)strlen(readable))
|| !TEST_strn_eq(pp, readable, rv))
goto err;
ret = 1;
err:
BIO_free(m);
return ret;
}
int setup_tests(void)
{
#if !defined(OPENSSL_NO_DEPRECATED_4_0)
ADD_TEST(test_x509_cmp_time_current);
ADD_TEST(test_X509_cmp_timeframe);
ADD_ALL_TESTS(test_x509_cmp_time, OSSL_NELEM(x509_cmp_tests));
#endif /* !defined(OPENSSL_NO_DEPRECATED_4_0) */
ADD_ALL_TESTS(test_x509_time, OSSL_NELEM(x509_format_tests));
ADD_ALL_TESTS(test_days, OSSL_NELEM(day_of_week_tests));
ADD_ALL_TESTS(test_x509_time_print_rfc_822, OSSL_NELEM(x509_print_tests_rfc_822));
ADD_ALL_TESTS(test_x509_time_print_iso_8601, OSSL_NELEM(x509_print_tests_iso_8601));
return 1;
}