From 5fdaa38febb7463dbd44f849b2930edcbd5f6bae Mon Sep 17 00:00:00 2001 From: Pauli Date: Mon, 11 May 2020 09:37:45 +1000 Subject: [PATCH] params: add OSSL_PARAM helpers for time_t. POSIX mandates that time_t is a signed integer but it doesn't specify the lenght. Having wrappers lets uses ignore this. Reviewed-by: Matthias St. Pierre (Merged from https://github.com/openssl/openssl/pull/11682) --- crypto/param_build.c | 7 +++++++ crypto/params.c | 27 +++++++++++++++++++++++++++ doc/man3/OSSL_PARAM_int.pod | 16 ++++++++++------ include/openssl/param_build.h | 2 ++ include/openssl/params.h | 5 +++++ test/param_build_test.c | 9 +++++++++ test/params_api_test.c | 28 ++++++++++++++++++++++++++++ util/libcrypto.num | 4 ++++ 8 files changed, 92 insertions(+), 6 deletions(-) diff --git a/crypto/param_build.c b/crypto/param_build.c index 76522cd377..de829d354f 100644 --- a/crypto/param_build.c +++ b/crypto/param_build.c @@ -190,6 +190,13 @@ int OSSL_PARAM_BLD_push_size_t(OSSL_PARAM_BLD *bld, const char *key, OSSL_PARAM_UNSIGNED_INTEGER); } +int OSSL_PARAM_BLD_push_time_t(OSSL_PARAM_BLD *bld, const char *key, + time_t num) +{ + return param_push_num(bld, key, &num, sizeof(num), + OSSL_PARAM_INTEGER); +} + int OSSL_PARAM_BLD_push_double(OSSL_PARAM_BLD *bld, const char *key, double num) { diff --git a/crypto/params.c b/crypto/params.c index 9bccc51760..67ca4f0c83 100644 --- a/crypto/params.c +++ b/crypto/params.c @@ -617,6 +617,33 @@ OSSL_PARAM OSSL_PARAM_construct_size_t(const char *key, size_t *buf) sizeof(size_t)); } +int OSSL_PARAM_get_time_t(const OSSL_PARAM *p, time_t *val) +{ + switch (sizeof(time_t)) { + case sizeof(int32_t): + return OSSL_PARAM_get_int32(p, (int32_t *)val); + case sizeof(int64_t): + return OSSL_PARAM_get_int64(p, (int64_t *)val); + } + return 0; +} + +int OSSL_PARAM_set_time_t(OSSL_PARAM *p, time_t val) +{ + switch (sizeof(time_t)) { + case sizeof(int32_t): + return OSSL_PARAM_set_int32(p, (int32_t)val); + case sizeof(int64_t): + return OSSL_PARAM_set_int64(p, (int64_t)val); + } + return 0; +} + +OSSL_PARAM OSSL_PARAM_construct_time_t(const char *key, time_t *buf) +{ + return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(time_t)); +} + int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val) { BIGNUM *b; diff --git a/doc/man3/OSSL_PARAM_int.pod b/doc/man3/OSSL_PARAM_int.pod index 739c2a8c2d..ca4e9d066e 100644 --- a/doc/man3/OSSL_PARAM_int.pod +++ b/doc/man3/OSSL_PARAM_int.pod @@ -3,13 +3,13 @@ =head1 NAME OSSL_PARAM_double, OSSL_PARAM_int, OSSL_PARAM_int32, OSSL_PARAM_int64, -OSSL_PARAM_long, OSSL_PARAM_size_t, OSSL_PARAM_uint, OSSL_PARAM_uint32, +OSSL_PARAM_long, OSSL_PARAM_size_t, OSSL_PARAM_time_t, OSSL_PARAM_uint, OSSL_PARAM_uint32, OSSL_PARAM_uint64, OSSL_PARAM_ulong, OSSL_PARAM_BN, OSSL_PARAM_utf8_string, OSSL_PARAM_octet_string, OSSL_PARAM_utf8_ptr, OSSL_PARAM_octet_ptr, OSSL_PARAM_END, OSSL_PARAM_construct_double, OSSL_PARAM_construct_int, OSSL_PARAM_construct_int32, OSSL_PARAM_construct_int64, -OSSL_PARAM_construct_long, OSSL_PARAM_construct_size_t, +OSSL_PARAM_construct_long, OSSL_PARAM_construct_size_t, OSSL_PARAM_construct_time_t, OSSL_PARAM_construct_uint, OSSL_PARAM_construct_uint32, OSSL_PARAM_construct_uint64, OSSL_PARAM_construct_ulong, OSSL_PARAM_construct_BN, OSSL_PARAM_construct_utf8_string, @@ -17,13 +17,13 @@ OSSL_PARAM_construct_utf8_ptr, OSSL_PARAM_construct_octet_string, OSSL_PARAM_construct_octet_ptr, OSSL_PARAM_construct_end, OSSL_PARAM_locate, OSSL_PARAM_locate_const, OSSL_PARAM_get_double, OSSL_PARAM_get_int, OSSL_PARAM_get_int32, -OSSL_PARAM_get_int64, OSSL_PARAM_get_long, OSSL_PARAM_get_size_t, +OSSL_PARAM_get_int64, OSSL_PARAM_get_long, OSSL_PARAM_get_size_t, OSSL_PARAM_get_time_t, OSSL_PARAM_get_uint, OSSL_PARAM_get_uint32, OSSL_PARAM_get_uint64, OSSL_PARAM_get_ulong, OSSL_PARAM_get_BN, OSSL_PARAM_get_utf8_string, OSSL_PARAM_get_octet_string, OSSL_PARAM_get_utf8_ptr, OSSL_PARAM_get_octet_ptr, OSSL_PARAM_set_double, OSSL_PARAM_set_int, OSSL_PARAM_set_int32, -OSSL_PARAM_set_int64, OSSL_PARAM_set_long, OSSL_PARAM_set_size_t, +OSSL_PARAM_set_int64, OSSL_PARAM_set_long, OSSL_PARAM_set_size_t, OSSL_PARAM_set_time_t, OSSL_PARAM_set_uint, OSSL_PARAM_set_uint32, OSSL_PARAM_set_uint64, OSSL_PARAM_set_ulong, OSSL_PARAM_set_BN, OSSL_PARAM_set_utf8_string, OSSL_PARAM_set_octet_string, OSSL_PARAM_set_utf8_ptr, @@ -39,9 +39,9 @@ OSSL_PARAM_UNMODIFIED, OSSL_PARAM_modified, OSSL_PARAM_set_all_unmodified /* * TYPE in function names is one of: - * double, int, int32, int64, long, size_t, uint, uint32, uint64, ulong + * double, int, int32, int64, long, size_t, time_t, uint, uint32, uint64, ulong * Corresponding TYPE in function arguments is one of: - * double, int, int32_t, int64_t, long, size_t, unsigned int, uint32_t, + * double, int, int32_t, int64_t, long, size_t, time_t, unsigned int, uint32_t, * uint64_t, unsigned long */ @@ -126,6 +126,10 @@ long int (long) =item * +time_t + +=item * + size_t =item * diff --git a/include/openssl/param_build.h b/include/openssl/param_build.h index 45ed1bf13f..58ad9be732 100644 --- a/include/openssl/param_build.h +++ b/include/openssl/param_build.h @@ -33,6 +33,8 @@ int OSSL_PARAM_BLD_push_uint64(OSSL_PARAM_BLD *bld, const char *key, uint64_t val); int OSSL_PARAM_BLD_push_size_t(OSSL_PARAM_BLD *bld, const char *key, size_t val); +int OSSL_PARAM_BLD_push_time_t(OSSL_PARAM_BLD *bld, const char *key, + time_t val); int OSSL_PARAM_BLD_push_double(OSSL_PARAM_BLD *bld, const char *key, double val); int OSSL_PARAM_BLD_push_BN(OSSL_PARAM_BLD *bld, const char *key, diff --git a/include/openssl/params.h b/include/openssl/params.h index 993ee8188e..44fc1a6a38 100644 --- a/include/openssl/params.h +++ b/include/openssl/params.h @@ -49,6 +49,8 @@ extern "C" { sizeof(uint64_t)) # define OSSL_PARAM_size_t(key, addr) \ OSSL_PARAM_DEFN((key), OSSL_PARAM_UNSIGNED_INTEGER, (addr), sizeof(size_t)) +# define OSSL_PARAM_time_t(key, addr) \ + OSSL_PARAM_DEFN((key), OSSL_PARAM_INTEGER, (addr), sizeof(time_t)) # define OSSL_PARAM_double(key, addr) \ OSSL_PARAM_DEFN((key), OSSL_PARAM_REAL, (addr), sizeof(double)) @@ -78,6 +80,7 @@ OSSL_PARAM OSSL_PARAM_construct_uint32(const char *key, uint32_t *buf); OSSL_PARAM OSSL_PARAM_construct_int64(const char *key, int64_t *buf); OSSL_PARAM OSSL_PARAM_construct_uint64(const char *key, uint64_t *buf); OSSL_PARAM OSSL_PARAM_construct_size_t(const char *key, size_t *buf); +OSSL_PARAM OSSL_PARAM_construct_time_t(const char *key, time_t *buf); OSSL_PARAM OSSL_PARAM_construct_BN(const char *key, unsigned char *buf, size_t bsize); OSSL_PARAM OSSL_PARAM_construct_double(const char *key, double *buf); @@ -105,6 +108,7 @@ int OSSL_PARAM_get_uint32(const OSSL_PARAM *p, uint32_t *val); int OSSL_PARAM_get_int64(const OSSL_PARAM *p, int64_t *val); int OSSL_PARAM_get_uint64(const OSSL_PARAM *p, uint64_t *val); int OSSL_PARAM_get_size_t(const OSSL_PARAM *p, size_t *val); +int OSSL_PARAM_get_time_t(const OSSL_PARAM *p, time_t *val); int OSSL_PARAM_set_int(OSSL_PARAM *p, int val); int OSSL_PARAM_set_uint(OSSL_PARAM *p, unsigned int val); @@ -115,6 +119,7 @@ int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val); int OSSL_PARAM_set_int64(OSSL_PARAM *p, int64_t val); int OSSL_PARAM_set_uint64(OSSL_PARAM *p, uint64_t val); int OSSL_PARAM_set_size_t(OSSL_PARAM *p, size_t val); +int OSSL_PARAM_set_time_t(OSSL_PARAM *p, time_t val); int OSSL_PARAM_get_double(const OSSL_PARAM *p, double *val); int OSSL_PARAM_set_double(OSSL_PARAM *p, double val); diff --git a/test/param_build_test.c b/test/param_build_test.c index d2cf78dc97..7a3bfa54b5 100644 --- a/test/param_build_test.c +++ b/test/param_build_test.c @@ -24,6 +24,7 @@ static int template_public_test(void) int32_t i32; int64_t i64; double d; + time_t t; char *utf = NULL; const char *cutf; int res = 0; @@ -33,6 +34,7 @@ static int template_public_test(void) || !TEST_true(OSSL_PARAM_BLD_push_long(bld, "l", 42)) || !TEST_true(OSSL_PARAM_BLD_push_int32(bld, "i32", 1532)) || !TEST_true(OSSL_PARAM_BLD_push_int64(bld, "i64", -9999999)) + || !TEST_true(OSSL_PARAM_BLD_push_time_t(bld, "t", 11224)) || !TEST_true(OSSL_PARAM_BLD_push_double(bld, "d", 1.61803398875)) || !TEST_ptr(bn = BN_new()) || !TEST_true(BN_set_word(bn, 1729)) @@ -70,6 +72,13 @@ static int template_public_test(void) || !TEST_size_t_eq(p->data_size, sizeof(long int)) || !TEST_true(OSSL_PARAM_get_long(p, &l)) || !TEST_long_eq(l, 42) + /* Check time_t */ + || !TEST_ptr(p = OSSL_PARAM_locate(params, "t")) + || !TEST_str_eq(p->key, "t") + || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) + || !TEST_size_t_eq(p->data_size, sizeof(time_t)) + || !TEST_true(OSSL_PARAM_get_time_t(p, &t)) + || !TEST_time_t_eq(t, 11224) /* Check double */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "d")) || !TEST_true(OSSL_PARAM_get_double(p, &d)) diff --git a/test/params_api_test.c b/test/params_api_test.c index 110820c8d1..8ba05120c3 100644 --- a/test/params_api_test.c +++ b/test/params_api_test.c @@ -379,6 +379,33 @@ static int test_param_size_t(int n) return test_param_type_extra(¶m, raw_values[n].value, sizeof(size_t)); } +static int test_param_time_t(int n) +{ + time_t in, out; + unsigned char buf[MAX_LEN], cmp[sizeof(size_t)]; + const size_t len = raw_values[n].len >= sizeof(size_t) + ? sizeof(time_t) : raw_values[n].len; + OSSL_PARAM param = OSSL_PARAM_time_t("a", NULL); + + memset(buf, 0, sizeof(buf)); + le_copy(buf, raw_values[n].value, sizeof(in)); + memcpy(&in, buf, sizeof(in)); + param.data = &out; + if (!TEST_true(OSSL_PARAM_set_time_t(¶m, in))) + return 0; + le_copy(cmp, &out, sizeof(out)); + if (!TEST_mem_eq(cmp, len, raw_values[n].value, len)) + return 0; + in = 0; + if (!TEST_true(OSSL_PARAM_get_time_t(¶m, &in))) + return 0; + le_copy(cmp, &in, sizeof(in)); + if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in))) + return 0; + param.data = &out; + return test_param_type_extra(¶m, raw_values[n].value, sizeof(size_t)); +} + static int test_param_bignum(int n) { unsigned char buf[MAX_LEN], bnbuf[MAX_LEN]; @@ -608,6 +635,7 @@ int setup_tests(void) ADD_ALL_TESTS(test_param_int32, OSSL_NELEM(raw_values)); ADD_ALL_TESTS(test_param_uint32, OSSL_NELEM(raw_values)); ADD_ALL_TESTS(test_param_size_t, OSSL_NELEM(raw_values)); + ADD_ALL_TESTS(test_param_time_t, OSSL_NELEM(raw_values)); ADD_ALL_TESTS(test_param_int64, OSSL_NELEM(raw_values)); ADD_ALL_TESTS(test_param_uint64, OSSL_NELEM(raw_values)); ADD_ALL_TESTS(test_param_bignum, OSSL_NELEM(raw_values)); diff --git a/util/libcrypto.num b/util/libcrypto.num index 86110db2af..ee3aa6d97c 100644 --- a/util/libcrypto.num +++ b/util/libcrypto.num @@ -5114,6 +5114,10 @@ EVP_default_properties_is_fips_enabled ? 3_0_0 EXIST::FUNCTION: EVP_default_properties_enable_fips ? 3_0_0 EXIST::FUNCTION: EVP_PKEY_new_raw_private_key_with_libctx ? 3_0_0 EXIST::FUNCTION: EVP_PKEY_new_raw_public_key_with_libctx ? 3_0_0 EXIST::FUNCTION: +OSSL_PARAM_BLD_push_time_t ? 3_0_0 EXIST::FUNCTION: +OSSL_PARAM_construct_time_t ? 3_0_0 EXIST::FUNCTION: +OSSL_PARAM_get_time_t ? 3_0_0 EXIST::FUNCTION: +OSSL_PARAM_set_time_t ? 3_0_0 EXIST::FUNCTION: OSSL_STORE_attach ? 3_0_0 EXIST::FUNCTION: OSSL_STORE_LOADER_set_attach ? 3_0_0 EXIST::FUNCTION: EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen ? 3_0_0 EXIST::FUNCTION:RSA -- 2.25.1