params: add OSSL_PARAM helpers for time_t.
authorPauli <paul.dale@oracle.com>
Sun, 10 May 2020 23:37:45 +0000 (09:37 +1000)
committerPauli <paul.dale@oracle.com>
Wed, 24 Jun 2020 10:05:41 +0000 (20:05 +1000)
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 <Matthias.St.Pierre@ncp-e.com>
(Merged from https://github.com/openssl/openssl/pull/11682)

crypto/param_build.c
crypto/params.c
doc/man3/OSSL_PARAM_int.pod
include/openssl/param_build.h
include/openssl/params.h
test/param_build_test.c
test/params_api_test.c
util/libcrypto.num

index 76522cd3770f26a047815345c6541f4e5f25d398..de829d354f5e2c7ffc2f15acadc7f35bea9d47f2 100644 (file)
@@ -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)
 {
index 9bccc517601b988ec5a19ddf079d1afddb890795..67ca4f0c83781ebe2a184fc70b10283944dcaf18 100644 (file)
@@ -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;
index 739c2a8c2dd16b77551e7e199b9e028c4a928659..ca4e9d066eca179a44d917b5ad5243297a0dd9d9 100644 (file)
@@ -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 *
index 45ed1bf13f1a9f2d76f5b636d0482ec3ee2c1356..58ad9be73234b9edefe8a2cf12e9988796e96e2a 100644 (file)
@@ -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,
index 993ee8188ecb8e2ce6091c75ccd74f990618ccb3..44fc1a6a3801c3d8fb27dc4ce92e4ca40d99e347 100644 (file)
@@ -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);
index d2cf78dc972e2d56316a4815347677869d0430b2..7a3bfa54b5e97ced693d25c62d39ff058920651a 100644 (file)
@@ -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))
index 110820c8d1183bbef3f158248bbd6fe3ac6855dc..8ba05120c33c051dd40ad6bd18fe50a31b3a3430 100644 (file)
@@ -379,6 +379,33 @@ static int test_param_size_t(int n)
     return test_param_type_extra(&param, 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(&param, 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(&param, &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(&param, 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));
index 86110db2af6ae881a3459c540e9db09b6967fd63..ee3aa6d97cfc1e5e5ef9827ef2c9b58a28f24a92 100644 (file)
@@ -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