Sync pki to chromium ce4bc9571462aa298d79b591df9d997323cf5157

Bug: chromium:1322914
Change-Id: Ic5a1349013bcfb279e5fee9f9838c63558d663b7
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/63025
Auto-Submit: Bob Beck <bbe@google.com>
Commit-Queue: Bob Beck <bbe@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
diff --git a/pki/parse_values.cc b/pki/parse_values.cc
index 7c142f8..505a10c 100644
--- a/pki/parse_values.cc
+++ b/pki/parse_values.cc
@@ -4,17 +4,13 @@
 
 #include "parse_values.h"
 
+#include <stdlib.h>
+
 #include <tuple>
-#include <vector>
 
-
-#include "fillins/fillins_string_util.h"
-
-#include "fillins/utf_string_conversions.h"
-#include "fillins/inet.h"
-#include "fillins/utf_string_conversions.h"
 #include <openssl/base.h>
 #include <openssl/bytestring.h>
+#include <openssl/mem.h>
 
 namespace bssl::der {
 
@@ -116,7 +112,7 @@
       }
       break;
     default:
-      abort(); //NOTREACHED_NORETURN;
+      abort();
   }
   return true;
 }
@@ -369,7 +365,7 @@
 
 bool ParsePrintableString(Input in, std::string* out) {
   for (char c : in.AsStringView()) {
-    if (!(fillins::IsAsciiAlpha(c) || c == ' ' || (c >= '\'' && c <= ':') ||
+    if (!(OPENSSL_isalpha(c) || c == ' ' || (c >= '\'' && c <= ':') ||
           c == '=' || c == '?')) {
       return false;
     }
@@ -402,44 +398,50 @@
 }
 
 bool ParseUniversalString(Input in, std::string* out) {
-  if (in.Length() % 4 != 0)
+  if (in.Length() % 4 != 0) {
     return false;
-
-  out->clear();
-  std::vector<uint32_t> in_32bit(in.Length() / 4);
-  if (in.Length())
-    memcpy(in_32bit.data(), in.UnsafeData(), in.Length());
-  for (const uint32_t c : in_32bit) {
-    // UniversalString is UCS-4 in big-endian order.
-    auto codepoint = static_cast<uint32_t>(ntohl(c));
-    if (!CBU_IS_UNICODE_CHAR(codepoint))
-      return false;
-
-    fillins::WriteUnicodeCharacter(codepoint, out);
   }
+
+  CBS cbs;
+  CBS_init(&cbs, in.UnsafeData(), in.Length());
+  bssl::ScopedCBB cbb;
+  if (!CBB_init(cbb.get(), in.Length())) {
+    return false;
+  }
+
+  while (CBS_len(&cbs) != 0) {
+    uint32_t c;
+    if (!CBS_get_utf32_be(&cbs, &c) ||  //
+        !CBB_add_utf8(cbb.get(), c)) {
+      return false;
+    }
+  }
+
+  out->assign(CBB_data(cbb.get()), CBB_data(cbb.get()) + CBB_len(cbb.get()));
   return true;
 }
 
 bool ParseBmpString(Input in, std::string* out) {
-  if (in.Length() % 2 != 0)
+  if (in.Length() % 2 != 0) {
     return false;
-
-  out->clear();
-  std::vector<uint16_t> in_16bit(in.Length() / 2);
-  if (in.Length())
-    memcpy(in_16bit.data(), in.UnsafeData(), in.Length());
-  for (const uint16_t c : in_16bit) {
-    // BMPString is UCS-2 in big-endian order.
-    uint32_t codepoint = ntohs(c);
-
-    // BMPString only supports codepoints in the Basic Multilingual Plane;
-    // surrogates are not allowed. CBU_IS_UNICODE_CHAR excludes the surrogate
-    // code points, among other invalid values.
-    if (!CBU_IS_UNICODE_CHAR(codepoint))
-      return false;
-
-    fillins::WriteUnicodeCharacter(codepoint, out);
   }
+
+  CBS cbs;
+  CBS_init(&cbs, in.UnsafeData(), in.Length());
+  bssl::ScopedCBB cbb;
+  if (!CBB_init(cbb.get(), in.Length())) {
+    return false;
+  }
+
+  while (CBS_len(&cbs) != 0) {
+    uint32_t c;
+    if (!CBS_get_ucs2_be(&cbs, &c) ||  //
+        !CBB_add_utf8(cbb.get(), c)) {
+      return false;
+    }
+  }
+
+  out->assign(CBB_data(cbb.get()), CBB_data(cbb.get()) + CBB_len(cbb.get()));
   return true;
 }