Avoid calling SSL_SESSION_dup from tests

This is an internal function and will want to return the internal opaque
type later, but that gets in the way of the shared library build.

Avoid this by:

- Gating the unit tests on BORINGSSL_SHARED_LIBRARY. That seems a fine
  end state.

- Making bssl_shim copy the session a different way. This is not great
  either, but the long-term answer is crbug.com/527997772.

This gets these symbols out of the way for making SSL_SESSION an opaque
struct.

Bug: 500444613
Change-Id: Id3c423ce7a7bbdc17a8f68ea8734461fc5fe986b
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/103147
Presubmit-BoringSSL-Verified: boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com <boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: David Benjamin <davidben@google.com>
Reviewed-by: Lily Chen <chlily@google.com>
Commit-Queue: Lily Chen <chlily@google.com>
diff --git a/ssl/internal.h b/ssl/internal.h
index 39c132d..3817a98 100644
--- a/ssl/internal.h
+++ b/ssl/internal.h
@@ -3761,8 +3761,8 @@
 // SSL_SESSION_dup returns a newly-allocated `SSL_SESSION` with a copy of the
 // fields in `session` or nullptr on error. The new session is non-resumable and
 // must be explicitly marked resumable once it has been filled in.
-OPENSSL_EXPORT UniquePtr<SSL_SESSION> SSL_SESSION_dup(
-    const SSL_SESSION *session, int dup_flags);
+UniquePtr<SSL_SESSION> SSL_SESSION_dup(const SSL_SESSION *session,
+                                       int dup_flags);
 
 // ssl_session_rebase_time updates `session`'s start time to the current time,
 // adjusting the timeout so the expiration time is unchanged.
diff --git a/ssl/ssl_test.cc b/ssl/ssl_test.cc
index 5137d3a..efa46e5 100644
--- a/ssl/ssl_test.cc
+++ b/ssl/ssl_test.cc
@@ -3110,6 +3110,7 @@
                                          nullptr, 0, 0));
 }
 
+#if !defined(BORINGSSL_SHARED_LIBRARY)
 static void AppendSession(SSL_SESSION *session, void *arg) {
   std::vector<SSL_SESSION *> *out =
       reinterpret_cast<std::vector<SSL_SESSION *> *>(arg);
@@ -3222,6 +3223,7 @@
   ASSERT_TRUE(CacheEquals(ctx.get(), {collision.get(), sessions[9].get(),
                                       sessions[8].get(), sessions[5].get()}));
 }
+#endif  //  !BORINGSSL_SHARED_LIBRARY
 
 static uint16_t EpochFromSequence(uint64_t seq) {
   return static_cast<uint16_t>(seq >> 48);
@@ -3912,6 +3914,7 @@
   EXPECT_EQ(Bytes(received), Bytes(kNewWrite));
 }
 
+#if !defined(BORINGSSL_SHARED_LIBRARY)
 TEST(SSLTest, SessionDuplication) {
   bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method()));
   bssl::UniquePtr<SSL_CTX> server_ctx =
@@ -3943,6 +3946,7 @@
 
   EXPECT_EQ(Bytes(s0_bytes, s0_len), Bytes(s1_bytes, s1_len));
 }
+#endif  // !BORINGSSL_SHARED_LIBRARY
 
 static void ExpectFDs(const SSL *ssl, int rfd, int wfd) {
   EXPECT_EQ(rfd, SSL_get_fd(ssl));
diff --git a/ssl/test/test_state.cc b/ssl/test/test_state.cc
index d439c62..7f47dc0 100644
--- a/ssl/test/test_state.cc
+++ b/ssl/test/test_state.cc
@@ -69,8 +69,15 @@
 
 static void ssl_ctx_add_session(SSL_SESSION *session, void *void_param) {
   SSL_CTX *ctx = reinterpret_cast<SSL_CTX *>(void_param);
-  UniquePtr<SSL_SESSION> new_session = SSL_SESSION_dup(
-      session, SSL_SESSION_INCLUDE_NONAUTH | SSL_SESSION_INCLUDE_TICKET);
+  // Make a copy of the session.
+  // TODO(crbug.com/527997772): Remove this copy.
+  uint8_t *data;
+  size_t len;
+  if (!SSL_SESSION_to_bytes(session, &data, &len)) {
+    fprintf(stderr, "Error copying SSL_SESSION.\n");
+  }
+  UniquePtr<uint8_t> free_data(data);
+  UniquePtr<SSL_SESSION> new_session(SSL_SESSION_from_bytes(data, len, ctx));
   if (new_session != nullptr) {
     SSL_CTX_add_session(ctx, new_session.get());
   }