Set is_first and is_last correctly with ASN1_STRFLGS_UTF8_CONVERT.

The comment says (in the now outdated orflags terms) that we don't need
to worry about this case because is_first/is_last only affect ASCII
codepoints, but it's easier to just set it correctly.

Change-Id: Ib6db66adb162a555da50f563ffc9af9da4a878ec
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/53126
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/asn1/a_strex.c b/crypto/asn1/a_strex.c
index 1bb574b..52d979e 100644
--- a/crypto/asn1/a_strex.c
+++ b/crypto/asn1/a_strex.c
@@ -192,11 +192,8 @@
       int utflen;
       utflen = UTF8_putc(utfbuf, sizeof utfbuf, c);
       for (int i = 0; i < utflen; i++) {
-        // We don't need to worry about setting orflags correctly
-        // because if utflen==1 its value will be correct anyway
-        // otherwise each character will be > 0x7f and so the
-        // character will never be escaped on first and last.
-        int len = do_esc_char(utfbuf[i], flags, quotes, out, is_first, is_last);
+        int len = do_esc_char(utfbuf[i], flags, quotes, out, is_first && i == 0,
+                              is_last && i == utflen - 1);
         if (len < 0) {
           return -1;
         }
diff --git a/crypto/asn1/asn1_test.cc b/crypto/asn1/asn1_test.cc
index ec6b371..c4f47bb 100644
--- a/crypto/asn1/asn1_test.cc
+++ b/crypto/asn1/asn1_test.cc
@@ -1072,6 +1072,8 @@
       {V_ASN1_T61STRING, StringToVector("   "), 0, ASN1_STRFLGS_ESC_2253,
        "\\  \\ "},
       {V_ASN1_T61STRING, StringToVector("   "), 0,
+       ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_UTF8_CONVERT, "\\  \\ "},
+      {V_ASN1_T61STRING, StringToVector("   "), 0,
        ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_ESC_QUOTE, "\"   \""},
 
       // RFC 2253 only escapes # at the start of a string.