Fix an OK but odd-looking use of ASN1_TYPE

X509_REQ_print_ex accessed an ASN1_TYPE's value.asn1_string union arm
before checking the type was an ASN1_STRING-based type. This is fine[*]
because it doesn't dereference the pointer until after the type is
checked.

Nonetheless, we can simply move the access slightly further down to make
it more straightforwardly correct.

While I'm here:

- Use the accessors for ASN1_STRING. One less thing to touch when
  ASN1_STRING becomes opaque later.

- There's no need for the cast. BIO_write already takes void*

- While they're all typedefs, accesssing the field as ASN1_BIT_STRING is
  bizarre when this function does not accept ASN1_BIT_STRING anyway!

[*] Strictly speaking, it relies on the more lax C union rules, which
allow type-punned direct (but not pointer) access to an inactive union
arm. C++ does not allow this, but AIUI every C++ compiler takes the C
interpretation in practice.

Change-Id: I675b828680a70cbcb540308d3c5dc628b6ad0c48
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/88047
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: Adam Langley <agl@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
diff --git a/crypto/x509/t_req.cc b/crypto/x509/t_req.cc
index 4d02d0d..073d417 100644
--- a/crypto/x509/t_req.cc
+++ b/crypto/x509/t_req.cc
@@ -106,8 +106,7 @@
         goto err;
       }
     } else {
-      size_t i;
-      for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
+      for (size_t i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
         // TODO(crbug.com/442860745): |X509_ATTRIBUTE| accessors are not
         // const-correct.
         X509_ATTRIBUTE *a = sk_X509_ATTRIBUTE_value(sk, i);
@@ -131,14 +130,8 @@
           }
         }
 
-        int j;
-        for (j = 0; j < num_attrs; j++) {
-          const ASN1_TYPE *at = X509_ATTRIBUTE_get0_type(a, j);
-          const int type = at->type;
-          const ASN1_BIT_STRING *bs = at->value.asn1_string;
-
-          int k;
-          for (k = 25 - obj_str_len; k > 0; k--) {
+        for (int j = 0; j < num_attrs; j++) {
+          for (int k = 25 - obj_str_len; k > 0; k--) {
             if (BIO_write(bio, " ", 1) != 1) {
               goto err;
             }
@@ -148,10 +141,14 @@
             goto err;
           }
 
-          if (type == V_ASN1_PRINTABLESTRING || type == V_ASN1_UTF8STRING ||
-              type == V_ASN1_IA5STRING || type == V_ASN1_T61STRING) {
-            if (BIO_write(bio, (const char *)bs->data, bs->length) !=
-                bs->length) {
+          const ASN1_TYPE *at = X509_ATTRIBUTE_get0_type(a, j);
+          if (at->type == V_ASN1_PRINTABLESTRING ||
+              at->type == V_ASN1_UTF8STRING || at->type == V_ASN1_IA5STRING ||
+              at->type == V_ASN1_T61STRING) {
+            const ASN1_STRING *str = at->value.asn1_string;
+            int str_len = ASN1_STRING_length(str);
+            if (BIO_write(bio, ASN1_STRING_get0_data(str), str_len) !=
+                str_len) {
               goto err;
             }
             BIO_puts(bio, "\n");