Add an implicit CBS to Span<const uint8_t> conversion.

They are exactly the same structure. Doing it in CBS allows us to switch
bssl::Span to absl::Span or a standard std::span in the future.

Bug: 132
Change-Id: Ibc96673c23233d557a1dd4d8768d2659d7a4ca0c
Reviewed-on: https://boringssl-review.googlesource.com/20669
Reviewed-by: David Benjamin <davidben@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
diff --git a/include/openssl/bytestring.h b/include/openssl/bytestring.h
index a09b49c..66f6204 100644
--- a/include/openssl/bytestring.h
+++ b/include/openssl/bytestring.h
@@ -17,6 +17,8 @@
 
 #include <openssl/base.h>
 
+#include <openssl/span.h>
+
 #if defined(__cplusplus)
 extern "C" {
 #endif
@@ -37,6 +39,13 @@
 struct cbs_st {
   const uint8_t *data;
   size_t len;
+
+#if !defined(BORINGSSL_NO_CXX)
+  // Allow implicit conversions to bssl::Span<const uint8_t>.
+  operator bssl::Span<const uint8_t>() const {
+    return bssl::MakeConstSpan(data, len);
+  }
+#endif
 };
 
 // CBS_init sets |cbs| to point to |data|. It does not take ownership of
diff --git a/ssl/handshake_server.cc b/ssl/handshake_server.cc
index 722b835..002e5bb 100644
--- a/ssl/handshake_server.cc
+++ b/ssl/handshake_server.cc
@@ -1180,9 +1180,7 @@
 
     // Compute the premaster.
     uint8_t alert = SSL_AD_DECODE_ERROR;
-    if (!hs->key_share->Finish(
-            &premaster_secret, &alert,
-            MakeConstSpan(CBS_data(&peer_key), CBS_len(&peer_key)))) {
+    if (!hs->key_share->Finish(&premaster_secret, &alert, peer_key)) {
       ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
       return ssl_hs_error;
     }
diff --git a/ssl/t1_lib.cc b/ssl/t1_lib.cc
index 32311ff..6301505 100644
--- a/ssl/t1_lib.cc
+++ b/ssl/t1_lib.cc
@@ -2174,9 +2174,7 @@
     return 0;
   }
 
-  if (!hs->key_share->Finish(
-          out_secret, out_alert,
-          MakeConstSpan(CBS_data(&peer_key), CBS_len(&peer_key)))) {
+  if (!hs->key_share->Finish(out_secret, out_alert, peer_key)) {
     *out_alert = SSL_AD_INTERNAL_ERROR;
     return 0;
   }
@@ -2238,10 +2236,9 @@
   Array<uint8_t> secret;
   ScopedCBB public_key;
   UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id);
-  if (!key_share || !CBB_init(public_key.get(), 32) ||
-      !key_share->Accept(
-          public_key.get(), &secret, out_alert,
-          MakeConstSpan(CBS_data(&peer_key), CBS_len(&peer_key))) ||
+  if (!key_share ||
+      !CBB_init(public_key.get(), 32) ||
+      !key_share->Accept(public_key.get(), &secret, out_alert, peer_key) ||
       !CBB_finish(public_key.get(), &hs->ecdh_public_key,
                   &hs->ecdh_public_key_len)) {
     *out_alert = SSL_AD_ILLEGAL_PARAMETER;