Convert reference counts in crypto/

This change converts the reference counts in crypto/ to use
|CRYPTO_refcount_t|. The reference counts in |X509_PKEY| and |X509_INFO|
were never actually used and so were dropped.

Change-Id: I75d572cdac1f8c1083c482e29c9519282d7fd16c
Reviewed-on: https://boringssl-review.googlesource.com/4772
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/include/openssl/asn1t.h b/include/openssl/asn1t.h
index 6c91134..2945fa8 100644
--- a/include/openssl/asn1t.h
+++ b/include/openssl/asn1t.h
@@ -894,7 +894,8 @@
 
 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr);
 
-int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it);
+void asn1_refcount_set_one(ASN1_VALUE **pval, const ASN1_ITEM *it);
+int asn1_refcount_dec_and_test_zero(ASN1_VALUE **pval, const ASN1_ITEM *it);
 
 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it);
 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
diff --git a/include/openssl/bio.h b/include/openssl/bio.h
index b70b42f..c0c430a 100644
--- a/include/openssl/bio.h
+++ b/include/openssl/bio.h
@@ -64,6 +64,7 @@
 #include <openssl/err.h> /* for ERR_print_errors_fp */
 #include <openssl/ex_data.h>
 #include <openssl/stack.h>
+#include <openssl/thread.h>
 
 #if defined(__cplusplus)
 extern "C" {
@@ -784,7 +785,7 @@
   /* num is a BIO-specific value. For example, in fd BIOs it's used to store a
    * file descriptor. */
   int num;
-  int references;
+  CRYPTO_refcount_t references;
   void *ptr;
   /* next_bio points to the next |BIO| in a chain. This |BIO| owns a reference
    * to |next_bio|. */
diff --git a/include/openssl/dh.h b/include/openssl/dh.h
index c61e53f..17574d5 100644
--- a/include/openssl/dh.h
+++ b/include/openssl/dh.h
@@ -253,7 +253,7 @@
   BIGNUM *counter;
 
   int flags;
-  int references;
+  CRYPTO_refcount_t references;
   CRYPTO_EX_DATA ex_data;
 };
 
diff --git a/include/openssl/dsa.h b/include/openssl/dsa.h
index 2271915..7274e4c 100644
--- a/include/openssl/dsa.h
+++ b/include/openssl/dsa.h
@@ -354,7 +354,7 @@
   /* Normally used to cache montgomery values */
   CRYPTO_MUTEX method_mont_p_lock;
   BN_MONT_CTX *method_mont_p;
-  int references;
+  CRYPTO_refcount_t references;
   CRYPTO_EX_DATA ex_data;
   DSA_METHOD *meth;
   /* functional reference if 'meth' is ENGINE-provided */
diff --git a/include/openssl/engine.h b/include/openssl/engine.h
index da242f6..d3d278a 100644
--- a/include/openssl/engine.h
+++ b/include/openssl/engine.h
@@ -93,7 +93,7 @@
 /* openssl_method_common_st contains the common part of all method structures.
  * This must be the first member of all method structures. */
 struct openssl_method_common_st {
-  int references;
+  int references;  /* dummy – not used. */
   char is_static;
 };
 
diff --git a/include/openssl/evp.h b/include/openssl/evp.h
index 54ad4be..234754d 100644
--- a/include/openssl/evp.h
+++ b/include/openssl/evp.h
@@ -59,6 +59,8 @@
 
 #include <openssl/base.h>
 
+#include <openssl/thread.h>
+
 /* OpenSSL included digest and cipher functions in this header so we include
  * them for users that still expect that.
  *
@@ -678,7 +680,7 @@
     ENGINE **pengine, const char *name, size_t len);
 
 struct evp_pkey_st {
-  int references;
+  CRYPTO_refcount_t references;
 
   /* type contains one of the EVP_PKEY_* values or NID_undef and determines
    * which element (if any) of the |pkey| union is valid. */
diff --git a/include/openssl/rsa.h b/include/openssl/rsa.h
index 2e24231..9b415d7 100644
--- a/include/openssl/rsa.h
+++ b/include/openssl/rsa.h
@@ -475,7 +475,7 @@
   BIGNUM *iqmp;
   /* be careful using this if the RSA structure is shared */
   CRYPTO_EX_DATA ex_data;
-  int references;
+  CRYPTO_refcount_t references;
   int flags;
 
   CRYPTO_MUTEX lock;
diff --git a/include/openssl/x509.h b/include/openssl/x509.h
index ef1d7fb..69c7da6 100644
--- a/include/openssl/x509.h
+++ b/include/openssl/x509.h
@@ -73,13 +73,14 @@
 #include <openssl/cipher.h>
 #include <openssl/dh.h>
 #include <openssl/dsa.h>
-#include <openssl/ec.h>
 #include <openssl/ecdh.h>
 #include <openssl/ecdsa.h>
+#include <openssl/ec.h>
 #include <openssl/evp.h>
 #include <openssl/rsa.h>
 #include <openssl/sha.h>
 #include <openssl/stack.h>
+#include <openssl/thread.h>
 
 #ifdef  __cplusplus
 extern "C" {
@@ -204,7 +205,7 @@
 	X509_REQ_INFO *req_info;
 	X509_ALGOR *sig_alg;
 	ASN1_BIT_STRING *signature;
-	int references;
+	CRYPTO_refcount_t references;
 	} X509_REQ;
 
 typedef struct x509_cinf_st
@@ -243,7 +244,7 @@
 	X509_ALGOR *sig_alg;
 	ASN1_BIT_STRING *signature;
 	int valid;
-	int references;
+	CRYPTO_refcount_t references;
 	char *name;
 	CRYPTO_EX_DATA ex_data;
 	/* These contain copies of various extension values */
@@ -420,7 +421,7 @@
 	X509_CRL_INFO *crl;
 	X509_ALGOR *sig_alg;
 	ASN1_BIT_STRING *signature;
-	int references;
+	CRYPTO_refcount_t references;
 	int flags;
 	/* Copies of various extensions */
 	AUTHORITY_KEYID *akid;
@@ -457,8 +458,6 @@
 
 	/* expanded version of 'enc_algor' */
 	EVP_CIPHER_INFO cipher;
-
-	int references;
 	} X509_PKEY;
 
 #ifndef OPENSSL_NO_EVP
@@ -472,7 +471,6 @@
 	int enc_len;
 	char *enc_data;
 
-	int references;
 	} X509_INFO;
 
 DECLARE_STACK_OF(X509_INFO)
diff --git a/include/openssl/x509_vfy.h b/include/openssl/x509_vfy.h
index 299cad7..32ba134 100644
--- a/include/openssl/x509_vfy.h
+++ b/include/openssl/x509_vfy.h
@@ -67,6 +67,7 @@
 
 #include <openssl/bio.h>
 #include <openssl/lhash.h>
+#include <openssl/thread.h>
 
 #ifdef  __cplusplus
 extern "C" {
@@ -202,7 +203,7 @@
 	STACK_OF(X509_CRL) * (*lookup_crls)(X509_STORE_CTX *ctx, X509_NAME *nm);
 	int (*cleanup)(X509_STORE_CTX *ctx);
 
-	int references;
+	CRYPTO_refcount_t references;
 	} /* X509_STORE */;
 
 OPENSSL_EXPORT int X509_STORE_set_depth(X509_STORE *store, int depth);