diff options
author | beck <> | 2021-11-04 23:52:34 +0000 |
---|---|---|
committer | beck <> | 2021-11-04 23:52:34 +0000 |
commit | 244374d8dda906a87c40f39a8ed949cf07a1c8f3 (patch) | |
tree | 3ca9bd91a3930e5f3e28873aa362dffdb8cf6227 /src/lib/libcrypto/x509/x509_vfy.c | |
parent | b866948734d2d995d78efdc04fb93574782722fa (diff) | |
download | openbsd-244374d8dda906a87c40f39a8ed949cf07a1c8f3.tar.gz openbsd-244374d8dda906a87c40f39a8ed949cf07a1c8f3.tar.bz2 openbsd-244374d8dda906a87c40f39a8ed949cf07a1c8f3.zip |
Cache sha512 hash and parsed not_before and not_after with X509 cert.
Replace sha1 hash use with sha512 for certificate comparisons internal
to the library. use the cached sha512 for the validator's verification
cache.
Reduces our recomputation of hashes, and heavy use of time1 time
conversion functions noticed bu claudio@ in rpki client.
ok jsing@ tb@
Diffstat (limited to '')
-rw-r--r-- | src/lib/libcrypto/x509/x509_vfy.c | 56 |
1 files changed, 35 insertions, 21 deletions
diff --git a/src/lib/libcrypto/x509/x509_vfy.c b/src/lib/libcrypto/x509/x509_vfy.c index 664474139c..3b0d6dfa35 100644 --- a/src/lib/libcrypto/x509/x509_vfy.c +++ b/src/lib/libcrypto/x509/x509_vfy.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: x509_vfy.c,v 1.93 2021/11/01 20:53:08 tb Exp $ */ | 1 | /* $OpenBSD: x509_vfy.c,v 1.94 2021/11/04 23:52:34 beck Exp $ */ |
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | * All rights reserved. | 3 | * All rights reserved. |
4 | * | 4 | * |
@@ -1843,6 +1843,18 @@ verify_cb_cert(X509_STORE_CTX *ctx, X509 *x, int depth, int err) | |||
1843 | return ctx->verify_cb(0, ctx); | 1843 | return ctx->verify_cb(0, ctx); |
1844 | } | 1844 | } |
1845 | 1845 | ||
1846 | |||
1847 | /* Mimic OpenSSL '0 for failure' ick */ | ||
1848 | static int | ||
1849 | time_t_bogocmp(time_t a, time_t b) | ||
1850 | { | ||
1851 | if (a == -1 || b == -1) | ||
1852 | return 0; | ||
1853 | if (a <= b) | ||
1854 | return -1; | ||
1855 | return 1; | ||
1856 | } | ||
1857 | |||
1846 | /* | 1858 | /* |
1847 | * Check certificate validity times. | 1859 | * Check certificate validity times. |
1848 | * | 1860 | * |
@@ -1854,17 +1866,21 @@ verify_cb_cert(X509_STORE_CTX *ctx, X509 *x, int depth, int err) | |||
1854 | int | 1866 | int |
1855 | x509_check_cert_time(X509_STORE_CTX *ctx, X509 *x, int depth) | 1867 | x509_check_cert_time(X509_STORE_CTX *ctx, X509 *x, int depth) |
1856 | { | 1868 | { |
1857 | time_t *ptime; | 1869 | time_t ptime; |
1858 | int i; | 1870 | int i; |
1859 | 1871 | ||
1860 | if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) | 1872 | if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) |
1861 | ptime = &ctx->param->check_time; | 1873 | ptime = ctx->param->check_time; |
1862 | else if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME) | 1874 | else if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME) |
1863 | return 1; | 1875 | return 1; |
1864 | else | 1876 | else |
1865 | ptime = NULL; | 1877 | ptime = time(NULL); |
1878 | |||
1879 | if (x->ex_flags & EXFLAG_SET) | ||
1880 | i = time_t_bogocmp(x->not_before, ptime); | ||
1881 | else | ||
1882 | i = X509_cmp_time(X509_get_notBefore(x), &ptime); | ||
1866 | 1883 | ||
1867 | i = X509_cmp_time(X509_get_notBefore(x), ptime); | ||
1868 | if (i >= 0 && depth < 0) | 1884 | if (i >= 0 && depth < 0) |
1869 | return 0; | 1885 | return 0; |
1870 | if (i == 0 && !verify_cb_cert(ctx, x, depth, | 1886 | if (i == 0 && !verify_cb_cert(ctx, x, depth, |
@@ -1874,7 +1890,11 @@ x509_check_cert_time(X509_STORE_CTX *ctx, X509 *x, int depth) | |||
1874 | X509_V_ERR_CERT_NOT_YET_VALID)) | 1890 | X509_V_ERR_CERT_NOT_YET_VALID)) |
1875 | return 0; | 1891 | return 0; |
1876 | 1892 | ||
1877 | i = X509_cmp_time_internal(X509_get_notAfter(x), ptime, 1); | 1893 | if (x->ex_flags & EXFLAG_SET) |
1894 | i = time_t_bogocmp(x->not_after, ptime); | ||
1895 | else | ||
1896 | i = X509_cmp_time_internal(X509_get_notAfter(x), &ptime, 1); | ||
1897 | |||
1878 | if (i <= 0 && depth < 0) | 1898 | if (i <= 0 && depth < 0) |
1879 | return 0; | 1899 | return 0; |
1880 | if (i == 0 && !verify_cb_cert(ctx, x, depth, | 1900 | if (i == 0 && !verify_cb_cert(ctx, x, depth, |
@@ -1883,6 +1903,7 @@ x509_check_cert_time(X509_STORE_CTX *ctx, X509 *x, int depth) | |||
1883 | if (i < 0 && !verify_cb_cert(ctx, x, depth, | 1903 | if (i < 0 && !verify_cb_cert(ctx, x, depth, |
1884 | X509_V_ERR_CERT_HAS_EXPIRED)) | 1904 | X509_V_ERR_CERT_HAS_EXPIRED)) |
1885 | return 0; | 1905 | return 0; |
1906 | |||
1886 | return 1; | 1907 | return 1; |
1887 | } | 1908 | } |
1888 | 1909 | ||
@@ -1994,30 +2015,23 @@ X509_cmp_current_time(const ASN1_TIME *ctm) | |||
1994 | * 0 on error. | 2015 | * 0 on error. |
1995 | */ | 2016 | */ |
1996 | static int | 2017 | static int |
1997 | X509_cmp_time_internal(const ASN1_TIME *ctm, time_t *cmp_time, int clamp_notafter) | 2018 | X509_cmp_time_internal(const ASN1_TIME *ctm, time_t *cmp_time, int is_notafter) |
1998 | { | 2019 | { |
1999 | time_t compare; | 2020 | time_t compare, cert_time; |
2000 | struct tm tm1, tm2; | ||
2001 | int ret = 0; | ||
2002 | 2021 | ||
2003 | if (cmp_time == NULL) | 2022 | if (cmp_time == NULL) |
2004 | compare = time(NULL); | 2023 | compare = time(NULL); |
2005 | else | 2024 | else |
2006 | compare = *cmp_time; | 2025 | compare = *cmp_time; |
2007 | 2026 | ||
2008 | memset(&tm1, 0, sizeof(tm1)); | 2027 | if ((cert_time = x509_verify_asn1_time_to_time_t(ctm, is_notafter)) == |
2028 | -1) | ||
2029 | return 0; /* invalid time */ | ||
2009 | 2030 | ||
2010 | if (!x509_verify_asn1_time_to_tm(ctm, &tm1, clamp_notafter)) | 2031 | if (cert_time <= compare) |
2011 | goto out; /* invalid time */ | 2032 | return -1; /* 0 is used for error, so map same to less than */ |
2012 | 2033 | ||
2013 | if (gmtime_r(&compare, &tm2) == NULL) | 2034 | return 1; |
2014 | goto out; | ||
2015 | |||
2016 | ret = ASN1_time_tm_cmp(&tm1, &tm2); | ||
2017 | if (ret == 0) | ||
2018 | ret = -1; /* 0 is used for error, so map same to less than */ | ||
2019 | out: | ||
2020 | return (ret); | ||
2021 | } | 2035 | } |
2022 | 2036 | ||
2023 | int | 2037 | int |