Restore ASN1_TIME_set_string's behavior on NULL.

I inadvertently removed it, but set_string(NULL, str) would validate str
without writing an object. OpenSSL's habit of dual-use functions isn't
great (this behavior masked a bug in another project), but I apparently
even documented it in the header, so restore the behavior.

Change-Id: I8b4dbe5a2b21eb59cb20e4c845b17761329b34a1
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/55785
Auto-Submit: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: Adam Langley <agl@google.com>
diff --git a/crypto/asn1/a_gentm.c b/crypto/asn1/a_gentm.c
index 0104af0..096e912 100644
--- a/crypto/asn1/a_gentm.c
+++ b/crypto/asn1/a_gentm.c
@@ -85,11 +85,15 @@
   CBS cbs;
   CBS_init(&cbs, (const uint8_t *)str, len);
   if (!CBS_parse_generalized_time(&cbs, /*out_tm=*/NULL,
-                                  /*allow_timezone_offset=*/0) ||
-      !ASN1_STRING_set(s, str, len)) {
+                                  /*allow_timezone_offset=*/0)) {
     return 0;
   }
-  s->type = V_ASN1_GENERALIZEDTIME;
+  if (s != NULL) {
+    if (!ASN1_STRING_set(s, str, len)) {
+      return 0;
+    }
+    s->type = V_ASN1_GENERALIZEDTIME;
+  }
   return 1;
 }
 
diff --git a/crypto/asn1/a_utctm.c b/crypto/asn1/a_utctm.c
index bf09c90..d8d26ff 100644
--- a/crypto/asn1/a_utctm.c
+++ b/crypto/asn1/a_utctm.c
@@ -86,11 +86,15 @@
   CBS cbs;
   CBS_init(&cbs, (const uint8_t *)str, len);
   if (!CBS_parse_utc_time(&cbs, /*out_tm=*/NULL,
-                          /*allow_timezone_offset=*/1) ||
-      !ASN1_STRING_set(s, str, len)) {
+                          /*allow_timezone_offset=*/1)) {
     return 0;
   }
-  s->type = V_ASN1_UTCTIME;
+  if (s != NULL) {
+    if (!ASN1_STRING_set(s, str, len)) {
+      return 0;
+    }
+    s->type = V_ASN1_UTCTIME;
+  }
   return 1;
 }
 
diff --git a/crypto/asn1/asn1_test.cc b/crypto/asn1/asn1_test.cc
index fc17f40..268280f 100644
--- a/crypto/asn1/asn1_test.cc
+++ b/crypto/asn1/asn1_test.cc
@@ -1072,8 +1072,20 @@
 
   // Invalid inputs are rejected.
   EXPECT_FALSE(ASN1_UTCTIME_set_string(s.get(), "nope"));
+  EXPECT_FALSE(ASN1_UTCTIME_set_string(s.get(), "19700101000000Z"));
   EXPECT_FALSE(ASN1_GENERALIZEDTIME_set_string(s.get(), "nope"));
+  EXPECT_FALSE(ASN1_GENERALIZEDTIME_set_string(s.get(), "700101000000Z"));
   EXPECT_FALSE(ASN1_TIME_set_string(s.get(), "nope"));
+
+  // If passed a null object, the functions validate the input without writing
+  // to anything.
+  EXPECT_TRUE(ASN1_UTCTIME_set_string(nullptr, "700101000000Z"));
+  EXPECT_TRUE(ASN1_TIME_set_string(nullptr, "700101000000Z"));
+  EXPECT_TRUE(ASN1_GENERALIZEDTIME_set_string(nullptr, "19700101000000Z"));
+  EXPECT_TRUE(ASN1_TIME_set_string(nullptr, "19700101000000Z"));
+  EXPECT_FALSE(ASN1_UTCTIME_set_string(nullptr, "nope"));
+  EXPECT_FALSE(ASN1_GENERALIZEDTIME_set_string(nullptr, "nope"));
+  EXPECT_FALSE(ASN1_TIME_set_string(nullptr, "nope"));
 }
 
 TEST(ASN1Test, AdjTime) {