Add X509_PUBKEY_get0_public_key.

This API does not come from OpenSSL, but OpenSSL does not appear to have
any way to get this information. There is X509_get0_pubkey_bitstr, but
that only works for X509 objects, not X509_PUBKEY.

Change-Id: Ifc8be554a4d8cbf830c32b95b953f092980804df
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/46304
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
diff --git a/crypto/x509/x_pubkey.c b/crypto/x509/x_pubkey.c
index 9e2f704..f5d3fee 100644
--- a/crypto/x509/x_pubkey.c
+++ b/crypto/x509/x_pubkey.c
@@ -210,3 +210,7 @@
     }
     return 1;
 }
+
+const ASN1_BIT_STRING *X509_PUBKEY_get0_public_key(const X509_PUBKEY *pub) {
+    return pub->public_key;
+}
diff --git a/include/openssl/x509.h b/include/openssl/x509.h
index 1550b2c..3f26792 100644
--- a/include/openssl/x509.h
+++ b/include/openssl/x509.h
@@ -1604,12 +1604,21 @@
 // is not NULL, it sets |*out_obj| to AlgorithmIdentifier's OID. If |out_key|
 // is not NULL, it sets |*out_key| and |*out_key_len| to the encoded public key.
 // If |out_alg| is not NULL, it sets |*out_alg| to the AlgorithmIdentifier.
+//
+// Note: X.509 SubjectPublicKeyInfo structures store the encoded public key as a
+// BIT STRING. |*out_key| and |*out_key_len| will silently pad the key with zero
+// bits if |pub| did not contain a whole number of bytes. Use
+// |X509_PUBKEY_get0_public_key| to preserve this information.
 OPENSSL_EXPORT int X509_PUBKEY_get0_param(ASN1_OBJECT **out_obj,
                                           const uint8_t **out_key,
                                           int *out_key_len,
                                           X509_ALGOR **out_alg,
                                           X509_PUBKEY *pub);
 
+// X509_PUBKEY_get0_public_key returns |pub|'s encoded public key.
+OPENSSL_EXPORT const ASN1_BIT_STRING *X509_PUBKEY_get0_public_key(
+    const X509_PUBKEY *pub);
+
 OPENSSL_EXPORT int X509_check_trust(X509 *x, int id, int flags);
 OPENSSL_EXPORT int X509_TRUST_get_count(void);
 OPENSSL_EXPORT X509_TRUST *X509_TRUST_get0(int idx);