Fix EC public key output in EVP_PKEY_print_*

BIO_hexdump does not really fit here. This matches OpenSSL.

Change-Id: I5c8e2b992c2711fb7986aa549578da9495360536
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/54951
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Bob Beck <bbe@google.com>
diff --git a/crypto/evp/evp_extra_test.cc b/crypto/evp/evp_extra_test.cc
index c88ad7f..1b504aa 100644
--- a/crypto/evp/evp_extra_test.cc
+++ b/crypto/evp/evp_extra_test.cc
@@ -762,8 +762,7 @@
       61
 )");
 
-  // TODO(davidben): This output is a little off. |BIO_hexdump| does not really
-  // fit for printing the public key, and we've lost the curve name.
+  // TODO(davidben): This output should include the curve name.
   bssl::UniquePtr<EVP_PKEY> ec =
       ParsePrivateKey(EVP_PKEY_EC, kExampleECKeyDER, sizeof(kExampleECKeyDER));
   ASSERT_TRUE(ec);
@@ -771,11 +770,12 @@
             "  ECDSA-Parameters: (256 bit)\n");
   EXPECT_EQ(PrintToString(ec.get(), /*indent=*/2, &EVP_PKEY_print_public),
             R"(  Public-Key: (256 bit)
-  00000000  04 e6 2b 69 e2 bf 65 9f  97 be 2f 1e 0d 94 8a 4c  |..+i..e.../....L|
-  00000010  d5 97 6b b7 a9 1e 0d 46  fb dd a9 a9 1e 9d dc ba  |..k....F........|
-  00000020  5a 01 e7 d6 97 a8 0a 18  f9 c3 c4 a3 1e 56 e2 7c  |Z............V.||
-  00000030  83 48 db 16 1a 1c f5 1d  7e f1 94 2d 4b cf 72 22  |.H......~..-K.r"|
-  00000040  c1                                                |.|
+  pub:
+      04:e6:2b:69:e2:bf:65:9f:97:be:2f:1e:0d:94:8a:
+      4c:d5:97:6b:b7:a9:1e:0d:46:fb:dd:a9:a9:1e:9d:
+      dc:ba:5a:01:e7:d6:97:a8:0a:18:f9:c3:c4:a3:1e:
+      56:e2:7c:83:48:db:16:1a:1c:f5:1d:7e:f1:94:2d:
+      4b:cf:72:22:c1
 )");
   EXPECT_EQ(PrintToString(ec.get(), /*indent=*/2, &EVP_PKEY_print_private),
             R"(  Private-Key: (256 bit)
@@ -783,11 +783,12 @@
       07:0f:08:72:7a:d4:a0:4a:9c:dd:59:c9:4d:89:68:
       77:08:b5:6f:c9:5d:30:77:0e:e8:d1:c9:ce:0a:8b:
       b4:6a
-  00000000  04 e6 2b 69 e2 bf 65 9f  97 be 2f 1e 0d 94 8a 4c  |..+i..e.../....L|
-  00000010  d5 97 6b b7 a9 1e 0d 46  fb dd a9 a9 1e 9d dc ba  |..k....F........|
-  00000020  5a 01 e7 d6 97 a8 0a 18  f9 c3 c4 a3 1e 56 e2 7c  |Z............V.||
-  00000030  83 48 db 16 1a 1c f5 1d  7e f1 94 2d 4b cf 72 22  |.H......~..-K.r"|
-  00000040  c1                                                |.|
+  pub:
+      04:e6:2b:69:e2:bf:65:9f:97:be:2f:1e:0d:94:8a:
+      4c:d5:97:6b:b7:a9:1e:0d:46:fb:dd:a9:a9:1e:9d:
+      dc:ba:5a:01:e7:d6:97:a8:0a:18:f9:c3:c4:a3:1e:
+      56:e2:7c:83:48:db:16:1a:1c:f5:1d:7e:f1:94:2d:
+      4b:cf:72:22:c1
 )");
 }
 
diff --git a/crypto/evp/print.c b/crypto/evp/print.c
index ff3868e..aad1e6d 100644
--- a/crypto/evp/print.c
+++ b/crypto/evp/print.c
@@ -64,6 +64,24 @@
 #include "../fipsmodule/rsa/internal.h"
 
 
+static int print_hex(BIO *bp, const uint8_t *data, size_t len, int off) {
+  for (size_t i = 0; i < len; i++) {
+    if ((i % 15) == 0) {
+      if (BIO_puts(bp, "\n") <= 0 ||  //
+          !BIO_indent(bp, off + 4, 128)) {
+        return 0;
+      }
+    }
+    if (BIO_printf(bp, "%02x%s", data[i], (i + 1 == len) ? "" : ":") <= 0) {
+      return 0;
+    }
+  }
+  if (BIO_write(bp, "\n", 1) <= 0) {
+    return 0;
+  }
+  return 1;
+}
+
 static int bn_print(BIO *bp, const char *name, const BIGNUM *num, int off) {
   if (num == NULL) {
     return 1;
@@ -105,32 +123,14 @@
 
   buf[0] = 0;
   BN_bn2bin(num, buf + 1);
-  const uint8_t *data = buf;
+  int ret;
   if (len > 0 && (buf[1] & 0x80) != 0) {
-    len++;  // Print the whole buffer.
+    // Print the whole buffer.
+    ret = print_hex(bp, buf, len + 1, off);
   } else {
-    data = buf + 1;  // Skip the leading zero.
+    // Skip the leading zero.
+    ret = print_hex(bp, buf + 1, len, off);
   }
-
-  int ret = 0;
-  for (size_t i = 0; i < len; i++) {
-    if ((i % 15) == 0) {
-      if (BIO_puts(bp, "\n") <= 0 ||  //
-          !BIO_indent(bp, off + 4, 128)) {
-        goto err;
-      }
-    }
-    if (BIO_printf(bp, "%02x%s", data[i], (i + 1 == len) ? "" : ":") <= 0) {
-      goto err;
-    }
-  }
-  if (BIO_write(bp, "\n", 1) <= 0) {
-    goto err;
-  }
-
-  ret = 1;
-
-err:
   OPENSSL_free(buf);
   return ret;
 }
@@ -278,7 +278,9 @@
     if (pub_len == 0) {
       return 0;
     }
-    int ret = BIO_hexdump(bp, pub, pub_len, off);
+    int ret = BIO_indent(bp, off, 128) &&  //
+              BIO_puts(bp, "pub:") > 0 &&  //
+              print_hex(bp, pub, pub_len, off);
     OPENSSL_free(pub);
     if (!ret) {
       return 0;