Add duplication APIs to ASN1_TIME and related types
authorPaul Yang <kaishen.yy@antfin.com>
Mon, 13 Jan 2020 06:26:11 +0000 (14:26 +0800)
committerPaul Yang <kaishen.yy@antfin.com>
Fri, 17 Jan 2020 03:30:33 +0000 (11:30 +0800)
Fixes #10600.

Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/10823)

crypto/asn1/a_gentm.c
crypto/asn1/a_time.c
crypto/asn1/a_utctm.c
doc/man3/ASN1_TIME_set.pod
include/openssl/asn1.h
test/asn1_time_test.c
util/libcrypto.num

index d82126b0e4f7a2ff93893eaf2095c2327ade54e9..4d31fef8fda1d61782132ebdad92b9b0a0f2c5dd 100644 (file)
@@ -16,6 +16,9 @@
 #include "internal/cryptlib.h"
 #include <openssl/asn1.h>
 #include "asn1_local.h"
+#include <openssl/asn1t.h>
+
+IMPLEMENT_ASN1_DUP_FUNCTION(ASN1_GENERALIZEDTIME)
 
 /* This is the primary function used to parse ASN1_GENERALIZEDTIME */
 int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d)
index c978248d3e909f5c3812f1852090e25419b8826a..4c9bf0c430b4417ec6b6dd9f0e2c6c40603b23b6 100644 (file)
@@ -24,6 +24,7 @@
 IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME)
 
 IMPLEMENT_ASN1_FUNCTIONS(ASN1_TIME)
+IMPLEMENT_ASN1_DUP_FUNCTION(ASN1_TIME)
 
 static int is_utc(const int year)
 {
index 47eb93fe8480423d4cdc38d05ce43de4c77244fe..596599353211eda240431ffdf2e930b35e3338f1 100644 (file)
@@ -12,6 +12,9 @@
 #include "internal/cryptlib.h"
 #include <openssl/asn1.h>
 #include "asn1_local.h"
+#include <openssl/asn1t.h>
+
+IMPLEMENT_ASN1_DUP_FUNCTION(ASN1_UTCTIME)
 
 /* This is the primary function used to parse ASN1_UTCTIME */
 int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d)
index ddae0fa3261b78e79870da0bb1eec14a93bd1d92..fc2b9308af07aaa2f9a9e27416dff48239031c99 100644 (file)
@@ -13,7 +13,8 @@ ASN1_TIME_print, ASN1_UTCTIME_print, ASN1_GENERALIZEDTIME_print,
 ASN1_TIME_diff,
 ASN1_TIME_cmp_time_t, ASN1_UTCTIME_cmp_time_t,
 ASN1_TIME_compare,
-ASN1_TIME_to_generalizedtime - ASN.1 Time functions
+ASN1_TIME_to_generalizedtime,
+ASN1_TIME_dup, ASN1_UTCTIME_dup, ASN1_GENERALIZEDTIME_dup - ASN.1 Time functions
 
 =head1 SYNOPSIS
 
@@ -58,6 +59,10 @@ ASN1_TIME_to_generalizedtime - ASN.1 Time functions
  ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(ASN1_TIME *t,
                                                     ASN1_GENERALIZEDTIME **out);
 
+ ASN1_TIME *ASN1_TIME_dup(const ASN1_TIME *t);
+ ASN1_UTCTIME *ASN1_UTCTIME_dup(const ASN1_UTCTIME *t);
+ ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_dup(const ASN1_GENERALIZEDTIME *t);
+
 =head1 DESCRIPTION
 
 The ASN1_TIME_set(), ASN1_UTCTIME_set() and ASN1_GENERALIZEDTIME_set()
@@ -131,6 +136,10 @@ The ASN1_TIME_to_generalizedtime() function converts an B<ASN1_TIME> to an
 B<ASN1_GENERALIZEDTIME>, regardless of year. If either I<out> or
 I<*out> are NULL, then a new object is allocated and must be freed after use.
 
+The ASN1_TIME_dup(), ASN1_UTCTIME_dup() and ASN1_GENERALIZEDTIME_dup() functions
+duplicate the time structure I<t> and return the duplicated result
+correspondingly.
+
 =head1 NOTES
 
 The B<ASN1_TIME> structure corresponds to the ASN.1 structure B<Time>
@@ -210,6 +219,9 @@ or 1 if I<a> is after I<b>. -2 is returned on error.
 ASN1_TIME_to_generalizedtime() returns a pointer to the appropriate time
 structure on success or NULL if an error occurred.
 
+ASN1_TIME_dup(), ASN1_UTCTIME_dup() and ASN1_GENERALIZEDTIME_dup() return a
+pointer to a time structure or NULL if an error occurred.
+
 =head1 EXAMPLES
 
 Set a time structure to one hour after the current time and print it out:
index 07db817d429fe056cfa52bc5e6daa855ed95b1f5..5863fef15fc806a03e2a5d5d3413e20a9736ec74 100644 (file)
@@ -599,6 +599,10 @@ DECLARE_ASN1_FUNCTIONS(ASN1_UTCTIME)
 DECLARE_ASN1_FUNCTIONS(ASN1_GENERALIZEDTIME)
 DECLARE_ASN1_FUNCTIONS(ASN1_TIME)
 
+DECLARE_ASN1_DUP_FUNCTION(ASN1_TIME)
+DECLARE_ASN1_DUP_FUNCTION(ASN1_UTCTIME)
+DECLARE_ASN1_DUP_FUNCTION(ASN1_GENERALIZEDTIME)
+
 DECLARE_ASN1_ITEM(ASN1_OCTET_STRING_NDEF)
 
 ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t);
index a619b373024bdaa79887142aaf7e8b6178ce99c4..b1a107da613e936a4ffa9d1cc3966d4b11ab1383 100644 (file)
@@ -320,6 +320,65 @@ static int test_table_compare(int idx)
     return TEST_int_eq(ASN1_TIME_compare(&td->t1, &td->t2), td->result);
 }
 
+static int test_time_dup(void)
+{
+    int ret = 0;
+    ASN1_TIME *asn1_time = NULL;
+    ASN1_TIME *asn1_time_dup = NULL;
+    ASN1_TIME *asn1_gentime = NULL;
+
+    asn1_time = ASN1_TIME_adj(NULL, time(NULL), 0, 0);
+    if (asn1_time == NULL) {
+        TEST_info("Internal error.");
+        goto err;
+    }
+
+    asn1_gentime = ASN1_TIME_to_generalizedtime(asn1_time, NULL);
+    if (asn1_gentime == NULL) {
+        TEST_info("Internal error.");
+        goto err;
+    }
+
+    asn1_time_dup = ASN1_TIME_dup(asn1_time);
+    if (!TEST_ptr_ne(asn1_time_dup, NULL)) {
+        TEST_info("ASN1_TIME_dup() failed.");
+        goto err;
+    }
+    if (!TEST_int_eq(ASN1_TIME_compare(asn1_time, asn1_time_dup), 0)) {
+        TEST_info("ASN1_TIME_dup() duplicated non-identical value.");
+        goto err;
+    }
+    ASN1_STRING_free(asn1_time_dup);
+
+    asn1_time_dup = ASN1_UTCTIME_dup(asn1_time);
+    if (!TEST_ptr_ne(asn1_time_dup, NULL)) {
+        TEST_info("ASN1_UTCTIME_dup() failed.");
+        goto err;
+    }
+    if (!TEST_int_eq(ASN1_TIME_compare(asn1_time, asn1_time_dup), 0)) {
+        TEST_info("ASN1_UTCTIME_dup() duplicated non-identical UTCTIME value.");
+        goto err;
+    }
+    ASN1_STRING_free(asn1_time_dup);
+
+    asn1_time_dup = ASN1_GENERALIZEDTIME_dup(asn1_gentime);
+    if (!TEST_ptr_ne(asn1_time_dup, NULL)) {
+        TEST_info("ASN1_GENERALIZEDTIME_dup() failed.");
+        goto err;
+    }
+    if (!TEST_int_eq(ASN1_TIME_compare(asn1_gentime, asn1_time_dup), 0)) {
+        TEST_info("ASN1_GENERALIZEDTIME_dup() dup'ed non-identical value.");
+        goto err;
+    }
+
+    ret = 1;
+ err:
+    ASN1_STRING_free(asn1_time);
+    ASN1_STRING_free(asn1_gentime);
+    ASN1_STRING_free(asn1_time_dup);
+    return ret;
+}
+
 int setup_tests(void)
 {
     /*
@@ -354,5 +413,6 @@ int setup_tests(void)
 #endif
     }
     ADD_ALL_TESTS(test_table_compare, OSSL_NELEM(tbl_compare_testdata));
+    ADD_TEST(test_time_dup);
     return 1;
 }
index 049380a715139a20f65b399cbc84cd590a7cd14c..2d034afaa4b032c0ba981814b51fd3c19db51d5f 100644 (file)
@@ -4915,3 +4915,6 @@ EVP_PKEY_CTX_new_from_name              ? 3_0_0   EXIST::FUNCTION:
 EVP_PKEY_CTX_new_from_pkey              ?      3_0_0   EXIST::FUNCTION:
 OSSL_SELF_TEST_set_callback             ?      3_0_0   EXIST::FUNCTION:
 OSSL_SELF_TEST_get_callback             ?      3_0_0   EXIST::FUNCTION:
+ASN1_TIME_dup                           ?      3_0_0   EXIST::FUNCTION:
+ASN1_UTCTIME_dup                        ?      3_0_0   EXIST::FUNCTION:
+ASN1_GENERALIZEDTIME_dup                ?      3_0_0   EXIST::FUNCTION: