Convert reference counts in ssl/

Convert reference counts in ssl/ to use |CRYPTO_refcount_t|.

Change-Id: I5d60f641b0c89b1ddfe38bfbd9d7285c60377f4c
Reviewed-on: https://boringssl-review.googlesource.com/4773
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/include/openssl/ssl.h b/include/openssl/ssl.h
index 48b3fc0..6d1c29e 100644
--- a/include/openssl/ssl.h
+++ b/include/openssl/ssl.h
@@ -150,15 +150,13 @@
 #include <openssl/hmac.h>
 #include <openssl/lhash.h>
 #include <openssl/pem.h>
+#include <openssl/thread.h>
 #include <openssl/x509.h>
 
 #if !defined(OPENSSL_WINDOWS)
 #include <sys/time.h>
 #endif
 
-/* Some code expected to get the threading functions by including ssl.h. */
-#include <openssl/thread.h>
-
 /* wpa_supplicant expects to get the version functions from ssl.h */
 #include <openssl/crypto.h>
 
@@ -414,7 +412,7 @@
    * not ok, we must remember the error for session reuse: */
   long verify_result; /* only for servers */
 
-  int references;
+  CRYPTO_refcount_t references;
   long timeout;
   long time;
 
@@ -849,7 +847,7 @@
   SSL_SESSION *(*get_session_cb)(struct ssl_st *ssl, uint8_t *data, int len,
                                  int *copy);
 
-  int references;
+  CRYPTO_refcount_t references;
 
   /* if defined, these override the X509_verify_cert() calls */
   int (*app_verify_callback)(X509_STORE_CTX *, void *);
diff --git a/ssl/ssl_lib.c b/ssl/ssl_lib.c
index 068bc63..c4f4a29 100644
--- a/ssl/ssl_lib.c
+++ b/ssl/ssl_lib.c
@@ -293,10 +293,10 @@
   s->quiet_shutdown = ctx->quiet_shutdown;
   s->max_send_fragment = ctx->max_send_fragment;
 
-  CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
+  CRYPTO_refcount_inc(&ctx->references);
   s->ctx = ctx;
   s->tlsext_ticket_expected = 0;
-  CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
+  CRYPTO_refcount_inc(&ctx->references);
   s->initial_ctx = ctx;
   if (ctx->tlsext_ecpointformatlist) {
     s->tlsext_ecpointformatlist = BUF_memdup(
@@ -1758,7 +1758,7 @@
 
 void SSL_CTX_free(SSL_CTX *ctx) {
   if (ctx == NULL ||
-      CRYPTO_add(&ctx->references, -1, CRYPTO_LOCK_SSL_CTX) > 0) {
+      !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
     return;
   }
 
@@ -2320,7 +2320,7 @@
   ssl_cert_free(ssl->cert);
   ssl->cert = ssl_cert_dup(ctx->cert);
 
-  CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
+  CRYPTO_refcount_inc(&ctx->references);
   SSL_CTX_free(ssl->ctx); /* decrement reference count */
   ssl->ctx = ctx;
 
diff --git a/ssl/ssl_sess.c b/ssl/ssl_sess.c
index 3eb428f..b358b5e 100644
--- a/ssl/ssl_sess.c
+++ b/ssl/ssl_sess.c
@@ -607,14 +607,14 @@
 
 SSL_SESSION *SSL_SESSION_up_ref(SSL_SESSION *session) {
   if (session) {
-    CRYPTO_add(&session->references, 1, CRYPTO_LOCK_SSL_SESSION);
+    CRYPTO_refcount_inc(&session->references);
   }
   return session;
 }
 
 void SSL_SESSION_free(SSL_SESSION *session) {
   if (session == NULL ||
-      CRYPTO_add(&session->references, -1, CRYPTO_LOCK_SSL_SESSION) > 0) {
+      !CRYPTO_refcount_dec_and_test_zero(&session->references)) {
     return;
   }