Eliminate unnecessary includes from low-level crypto modules.

Beyond generally eliminating unnecessary includes, eliminate as many
includes of headers that declare/define particularly error-prone
functionality like strlen, malloc, and free. crypto/err/internal.h was
added to remove the dependency on openssl/thread.h from the public
openssl/err.h header. The include of <stdlib.h> in openssl/mem.h was
retained since it defines OPENSSL_malloc and friends as macros around
the stdlib.h functions. The public x509.h, x509v3.h, and ssl.h headers
were not changed in order to minimize breakage of source compatibility
with external code.

Change-Id: I0d264b73ad0a720587774430b2ab8f8275960329
Reviewed-on: https://boringssl-review.googlesource.com/4220
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/asn1/a_gentm.c b/crypto/asn1/a_gentm.c
index 355feff..be093a4 100644
--- a/crypto/asn1/a_gentm.c
+++ b/crypto/asn1/a_gentm.c
@@ -57,6 +57,7 @@
 #include <openssl/asn1.h>
 
 #include <string.h>
+#include <time.h>
 
 #include <openssl/err.h>
 #include <openssl/mem.h>
diff --git a/crypto/asn1/a_time.c b/crypto/asn1/a_time.c
index f0badcc..e02e858 100644
--- a/crypto/asn1/a_time.c
+++ b/crypto/asn1/a_time.c
@@ -57,6 +57,7 @@
 #include <openssl/asn1.h>
 
 #include <string.h>
+#include <time.h>
 
 #include <openssl/asn1t.h>
 #include <openssl/buf.h>
diff --git a/crypto/asn1/a_utctm.c b/crypto/asn1/a_utctm.c
index 9304377..52b010f 100644
--- a/crypto/asn1/a_utctm.c
+++ b/crypto/asn1/a_utctm.c
@@ -57,6 +57,7 @@
 #include <openssl/asn1.h>
 
 #include <string.h>
+#include <time.h>
 
 #include <openssl/err.h>
 #include <openssl/mem.h>
diff --git a/crypto/asn1/tasn_utl.c b/crypto/asn1/tasn_utl.c
index f6045e4..1b9de94 100644
--- a/crypto/asn1/tasn_utl.c
+++ b/crypto/asn1/tasn_utl.c
@@ -62,6 +62,7 @@
 #include <openssl/mem.h>
 #include <openssl/obj.h>
 #include <openssl/err.h>
+#include <openssl/thread.h>
 
 
 /* Utility functions for manipulating fields and offsets */
diff --git a/crypto/bio/bio.c b/crypto/bio/bio.c
index 86fb853..4a07762 100644
--- a/crypto/bio/bio.c
+++ b/crypto/bio/bio.c
@@ -58,7 +58,6 @@
 
 #include <errno.h>
 #include <limits.h>
-#include <stddef.h>
 #include <string.h>
 
 #include <openssl/err.h>
diff --git a/crypto/bio/bio_test.cc b/crypto/bio/bio_test.cc
index b56f641..4c88df5 100644
--- a/crypto/bio/bio_test.cc
+++ b/crypto/bio/bio_test.cc
@@ -36,6 +36,7 @@
 #include <openssl/bio.h>
 #include <openssl/crypto.h>
 #include <openssl/err.h>
+#include <openssl/mem.h>
 
 #include <algorithm>
 
diff --git a/crypto/cipher/aead_test.c b/crypto/cipher/aead_test.c
index 2bdff03..84fb338 100644
--- a/crypto/cipher/aead_test.c
+++ b/crypto/cipher/aead_test.c
@@ -13,8 +13,6 @@
  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
 
 #include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 
 #include <openssl/aead.h>
diff --git a/crypto/cipher/cipher_test.c b/crypto/cipher/cipher_test.c
index 72bbb61..cdfecdc 100644
--- a/crypto/cipher/cipher_test.c
+++ b/crypto/cipher/cipher_test.c
@@ -54,7 +54,7 @@
  * copied and put under another distribution licence
  * [including the GNU Public Licence.] */
 
-#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
 #include <openssl/cipher.h>
diff --git a/crypto/cipher/internal.h b/crypto/cipher/internal.h
index e70d9ce..e6220d4 100644
--- a/crypto/cipher/internal.h
+++ b/crypto/cipher/internal.h
@@ -60,7 +60,6 @@
 #include <openssl/base.h>
 
 #include <openssl/aead.h>
-#include <openssl/asn1t.h>
 
 #if defined(__cplusplus)
 extern "C" {
diff --git a/crypto/cpu-intel.c b/crypto/cpu-intel.c
index 69f4570..df0e127 100644
--- a/crypto/cpu-intel.c
+++ b/crypto/cpu-intel.c
@@ -64,6 +64,7 @@
 #if !defined(OPENSSL_NO_ASM) && (defined(OPENSSL_X86) || defined(OPENSSL_X86_64))
 
 #include <inttypes.h>
+#include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 
diff --git a/crypto/des/des.c b/crypto/des/des.c
index d1ddb7a..56a2996 100644
--- a/crypto/des/des.c
+++ b/crypto/des/des.c
@@ -56,6 +56,8 @@
 
 #include <openssl/des.h>
 
+#include <stdlib.h>
+
 #include "internal.h"
 
 
diff --git a/crypto/dh/dh_impl.c b/crypto/dh/dh_impl.c
index 9f416b7..5c4d637 100644
--- a/crypto/dh/dh_impl.c
+++ b/crypto/dh/dh_impl.c
@@ -58,6 +58,7 @@
 
 #include <openssl/bn.h>
 #include <openssl/err.h>
+#include <openssl/thread.h>
 
 #include "internal.h"
 
diff --git a/crypto/digest/md32_common.h b/crypto/digest/md32_common.h
index a4ce355..51508a6 100644
--- a/crypto/digest/md32_common.h
+++ b/crypto/digest/md32_common.h
@@ -51,8 +51,6 @@
 
 #include <openssl/base.h>
 
-#include <string.h>
-
 
 #if defined(__cplusplus)
 extern "C" {
diff --git a/crypto/directory_posix.c b/crypto/directory_posix.c
index c16701d..b944b69 100644
--- a/crypto/directory_posix.c
+++ b/crypto/directory_posix.c
@@ -35,6 +35,7 @@
 
 #include <dirent.h>
 #include <errno.h>
+#include <stdlib.h>
 #include <string.h>
 
 #if defined(OPENSSL_PNACL)
diff --git a/crypto/directory_win.c b/crypto/directory_win.c
index 2e4600f..4ebacf2 100644
--- a/crypto/directory_win.c
+++ b/crypto/directory_win.c
@@ -41,6 +41,9 @@
 #define NAME_MAX 255
 #endif
 
+#include <openssl/mem.h>
+
+
 struct OPENSSL_dir_context_st {
   WIN32_FIND_DATA ctx;
   HANDLE handle;
diff --git a/crypto/dsa/dsa.c b/crypto/dsa/dsa.c
index eca073a..5303714 100644
--- a/crypto/dsa/dsa.c
+++ b/crypto/dsa/dsa.c
@@ -67,6 +67,7 @@
 #include <openssl/err.h>
 #include <openssl/ex_data.h>
 #include <openssl/mem.h>
+#include <openssl/thread.h>
 
 #include "internal.h"
 
diff --git a/crypto/dsa/dsa_impl.c b/crypto/dsa/dsa_impl.c
index 9efd008..aba7f85 100644
--- a/crypto/dsa/dsa_impl.c
+++ b/crypto/dsa/dsa_impl.c
@@ -66,6 +66,7 @@
 #include <openssl/err.h>
 #include <openssl/rand.h>
 #include <openssl/sha.h>
+#include <openssl/thread.h>
 
 #include "internal.h"
 
diff --git a/crypto/ec/ec_key.c b/crypto/ec/ec_key.c
index 050d77f..b4671ed 100644
--- a/crypto/ec/ec_key.c
+++ b/crypto/ec/ec_key.c
@@ -74,6 +74,7 @@
 #include <openssl/err.h>
 #include <openssl/ex_data.h>
 #include <openssl/mem.h>
+#include <openssl/thread.h>
 
 #include "internal.h"
 
diff --git a/crypto/ec/ec_test.c b/crypto/ec/ec_test.c
index cffd332..269005b 100644
--- a/crypto/ec/ec_test.c
+++ b/crypto/ec/ec_test.c
@@ -18,6 +18,7 @@
 #include <openssl/crypto.h>
 #include <openssl/ec_key.h>
 #include <openssl/err.h>
+#include <openssl/mem.h>
 
 #include "internal.h"
 
diff --git a/crypto/ec/wnaf.c b/crypto/ec/wnaf.c
index e36efe2..b6d1a54 100644
--- a/crypto/ec/wnaf.c
+++ b/crypto/ec/wnaf.c
@@ -72,6 +72,7 @@
 #include <openssl/bn.h>
 #include <openssl/err.h>
 #include <openssl/mem.h>
+#include <openssl/thread.h>
 
 #include "internal.h"
 
diff --git a/crypto/ecdsa/ecdsa.c b/crypto/ecdsa/ecdsa.c
index ab6a5a0..b8030cb 100644
--- a/crypto/ecdsa/ecdsa.c
+++ b/crypto/ecdsa/ecdsa.c
@@ -57,7 +57,6 @@
 #include <openssl/bn.h>
 #include <openssl/err.h>
 #include <openssl/mem.h>
-#include <openssl/thread.h>
 
 #include "../ec/internal.h"
 
diff --git a/crypto/err/err.c b/crypto/err/err.c
index 680663b..936ee50 100644
--- a/crypto/err/err.c
+++ b/crypto/err/err.c
@@ -111,8 +111,6 @@
 #include <assert.h>
 #include <errno.h>
 #include <inttypes.h>
-#include <stdarg.h>
-#include <stdio.h>
 #include <string.h>
 
 #if defined(OPENSSL_WINDOWS)
@@ -121,7 +119,6 @@
 #pragma warning(pop)
 #endif
 
-#include <openssl/lhash.h>
 #include <openssl/mem.h>
 #include <openssl/thread.h>
 
diff --git a/crypto/evp/evp.c b/crypto/evp/evp.c
index 64ffed6..5716ced 100644
--- a/crypto/evp/evp.c
+++ b/crypto/evp/evp.c
@@ -67,6 +67,7 @@
 #include <openssl/mem.h>
 #include <openssl/obj.h>
 #include <openssl/rsa.h>
+#include <openssl/thread.h>
 
 #include "internal.h"
 
diff --git a/crypto/ex_data.c b/crypto/ex_data.c
index 0c2503e..d399b83 100644
--- a/crypto/ex_data.c
+++ b/crypto/ex_data.c
@@ -108,7 +108,11 @@
 
 #include <openssl/ex_data.h>
 
+#include <assert.h>
+#include <string.h>
+
 #include <openssl/err.h>
+#include <openssl/mem.h>
 #include <openssl/thread.h>
 
 #include "crypto_error.h"
diff --git a/crypto/md4/md4.c b/crypto/md4/md4.c
index 8fb357b..6150b96 100644
--- a/crypto/md4/md4.c
+++ b/crypto/md4/md4.c
@@ -56,6 +56,7 @@
 
 #include <openssl/md4.h>
 
+#include <stdlib.h>
 #include <string.h>
 
 
diff --git a/crypto/modes/internal.h b/crypto/modes/internal.h
index 9662e0d..d12405e 100644
--- a/crypto/modes/internal.h
+++ b/crypto/modes/internal.h
@@ -121,6 +121,9 @@
 #endif
 #elif defined(_MSC_VER)
 #if _MSC_VER >= 1300
+#pragma warning(push, 3)
+#include <intrin.h>
+#pragma warning(pop)
 #pragma intrinsic(_byteswap_uint64, _byteswap_ulong)
 #define BSWAP8(x) _byteswap_uint64((uint64_t)(x))
 #define BSWAP4(x) _byteswap_ulong((uint32_t)(x))
diff --git a/crypto/pem/pem_pk8.c b/crypto/pem/pem_pk8.c
index 18cfb92..383a524 100644
--- a/crypto/pem/pem_pk8.c
+++ b/crypto/pem/pem_pk8.c
@@ -59,6 +59,7 @@
 #include <openssl/buf.h>
 #include <openssl/err.h>
 #include <openssl/evp.h>
+#include <openssl/mem.h>
 #include <openssl/obj.h>
 #include <openssl/pkcs8.h>
 #include <openssl/rand.h>
diff --git a/crypto/pkcs8/pkcs12_test.c b/crypto/pkcs8/pkcs12_test.c
index e401786..11a3090 100644
--- a/crypto/pkcs8/pkcs12_test.c
+++ b/crypto/pkcs8/pkcs12_test.c
@@ -13,7 +13,6 @@
  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
 
 #include <stdio.h>
-#include <stdlib.h>
 
 #include <openssl/bio.h>
 #include <openssl/bytestring.h>
diff --git a/crypto/rand/urandom.c b/crypto/rand/urandom.c
index a874974..05043fe 100644
--- a/crypto/rand/urandom.c
+++ b/crypto/rand/urandom.c
@@ -19,7 +19,6 @@
 #include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
-#include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
diff --git a/crypto/rsa/blinding.c b/crypto/rsa/blinding.c
index 16c34bb..c5a1604 100644
--- a/crypto/rsa/blinding.c
+++ b/crypto/rsa/blinding.c
@@ -113,6 +113,7 @@
 #include <openssl/bn.h>
 #include <openssl/mem.h>
 #include <openssl/err.h>
+#include <openssl/thread.h>
 
 #include "internal.h"
 
diff --git a/crypto/rsa/internal.h b/crypto/rsa/internal.h
index 2763253..d15f2a5 100644
--- a/crypto/rsa/internal.h
+++ b/crypto/rsa/internal.h
@@ -60,7 +60,7 @@
 #include <openssl/base.h>
 
 #include <openssl/asn1.h>
-#include <openssl/thread.h>
+
 
 #if defined(__cplusplus)
 extern "C" {
diff --git a/crypto/rsa/rsa.c b/crypto/rsa/rsa.c
index 2de2add..884f67e 100644
--- a/crypto/rsa/rsa.c
+++ b/crypto/rsa/rsa.c
@@ -64,6 +64,7 @@
 #include <openssl/ex_data.h>
 #include <openssl/mem.h>
 #include <openssl/obj.h>
+#include <openssl/thread.h>
 
 #include "internal.h"
 
diff --git a/crypto/rsa/rsa_impl.c b/crypto/rsa/rsa_impl.c
index 064513b..f790e64 100644
--- a/crypto/rsa/rsa_impl.c
+++ b/crypto/rsa/rsa_impl.c
@@ -61,6 +61,7 @@
 #include <openssl/bn.h>
 #include <openssl/err.h>
 #include <openssl/mem.h>
+#include <openssl/thread.h>
 
 #include "internal.h"
 
diff --git a/crypto/rsa/rsa_test.c b/crypto/rsa/rsa_test.c
index 640ffb8..10a6e1d 100644
--- a/crypto/rsa/rsa_test.c
+++ b/crypto/rsa/rsa_test.c
@@ -56,6 +56,7 @@
 
 #include <openssl/rsa.h>
 
+#include <stdlib.h>
 #include <string.h>
 
 #include <openssl/bn.h>
diff --git a/crypto/thread.c b/crypto/thread.c
index 48d5bbc..abc8b6f 100644
--- a/crypto/thread.c
+++ b/crypto/thread.c
@@ -56,10 +56,11 @@
 
 #include <openssl/thread.h>
 
-#include <errno.h>
 #include <string.h>
 
-#if defined(OPENSSL_WINDOWS)
+#if !defined(OPENSSL_WINDOWS)
+#include <errno.h>
+#else
 #pragma warning(push, 3)
 #include <windows.h>
 #pragma warning(pop)
diff --git a/crypto/time_support.c b/crypto/time_support.c
index 9aa64ce..bf9daed 100644
--- a/crypto/time_support.c
+++ b/crypto/time_support.c
@@ -67,6 +67,9 @@
 
 #include <openssl/time_support.h>
 
+#include <time.h>
+
+
 #define SECS_PER_DAY (24 * 60 * 60)
 
 struct tm *OPENSSL_gmtime(const time_t *time, struct tm *result) {
diff --git a/crypto/x509/by_dir.c b/crypto/x509/by_dir.c
index 3573c86..098c1bd 100644
--- a/crypto/x509/by_dir.c
+++ b/crypto/x509/by_dir.c
@@ -63,6 +63,7 @@
 #include <openssl/err.h>
 #include <openssl/lhash.h>
 #include <openssl/mem.h>
+#include <openssl/thread.h>
 #include <openssl/x509.h>
 
 
diff --git a/crypto/x509/by_file.c b/crypto/x509/by_file.c
index 2649631..2fdbce4 100644
--- a/crypto/x509/by_file.c
+++ b/crypto/x509/by_file.c
@@ -55,11 +55,14 @@
  * copied and put under another distribution licence
  * [including the GNU Public Licence.] */
 
+#include <stdlib.h>
+
 #include <openssl/buf.h>
 #include <openssl/err.h>
 #include <openssl/lhash.h>
 #include <openssl/pem.h>
-#include <openssl/x509.h>
+#include <openssl/thread.h>
+
 
 #ifndef OPENSSL_NO_STDIO
 
diff --git a/crypto/x509/pkcs7.c b/crypto/x509/pkcs7.c
index bb86077..99ee3da 100644
--- a/crypto/x509/pkcs7.c
+++ b/crypto/x509/pkcs7.c
@@ -18,6 +18,7 @@
 
 #include <openssl/bytestring.h>
 #include <openssl/err.h>
+#include <openssl/mem.h>
 #include <openssl/obj.h>
 #include <openssl/pem.h>
 #include <openssl/stack.h>
diff --git a/crypto/x509/pkcs7_test.c b/crypto/x509/pkcs7_test.c
index bac9fb2..38beb3e 100644
--- a/crypto/x509/pkcs7_test.c
+++ b/crypto/x509/pkcs7_test.c
@@ -18,6 +18,7 @@
 
 #include <openssl/bytestring.h>
 #include <openssl/crypto.h>
+#include <openssl/mem.h>
 #include <openssl/stack.h>
 #include <openssl/x509.h>
 
diff --git a/crypto/x509/x509_lu.c b/crypto/x509/x509_lu.c
index 090d341..cfac81d 100644
--- a/crypto/x509/x509_lu.c
+++ b/crypto/x509/x509_lu.c
@@ -60,6 +60,7 @@
 #include <openssl/err.h>
 #include <openssl/lhash.h>
 #include <openssl/mem.h>
+#include <openssl/thread.h>
 #include <openssl/x509.h>
 #include <openssl/x509v3.h>
 
diff --git a/crypto/x509/x509_vfy.c b/crypto/x509/x509_vfy.c
index e556fa4..dae3376 100644
--- a/crypto/x509/x509_vfy.c
+++ b/crypto/x509/x509_vfy.c
@@ -64,6 +64,7 @@
 #include <openssl/lhash.h>
 #include <openssl/mem.h>
 #include <openssl/obj.h>
+#include <openssl/thread.h>
 #include <openssl/x509.h>
 #include <openssl/x509v3.h>
 
diff --git a/crypto/x509/x_crl.c b/crypto/x509/x_crl.c
index bb23b57..aa92fa9 100644
--- a/crypto/x509/x_crl.c
+++ b/crypto/x509/x_crl.c
@@ -61,6 +61,7 @@
 #include <openssl/mem.h>
 #include <openssl/obj.h>
 #include <openssl/stack.h>
+#include <openssl/thread.h>
 #include <openssl/x509.h>
 #include <openssl/x509v3.h>
 
diff --git a/crypto/x509/x_info.c b/crypto/x509/x_info.c
index 8047c71..6807b24 100644
--- a/crypto/x509/x_info.c
+++ b/crypto/x509/x_info.c
@@ -59,6 +59,7 @@
 #include <openssl/asn1.h>
 #include <openssl/err.h>
 #include <openssl/mem.h>
+#include <openssl/thread.h>
 
 
 X509_INFO *X509_INFO_new(void)
diff --git a/crypto/x509/x_pkey.c b/crypto/x509/x_pkey.c
index 550078b..5acbe5b 100644
--- a/crypto/x509/x_pkey.c
+++ b/crypto/x509/x_pkey.c
@@ -59,8 +59,9 @@
 #include <string.h>
 
 #include <openssl/asn1.h>
-#include <openssl/mem.h>
 #include <openssl/err.h>
+#include <openssl/mem.h>
+#include <openssl/thread.h>
 
 
 X509_PKEY *X509_PKEY_new(void)
diff --git a/crypto/x509/x_pubkey.c b/crypto/x509/x_pubkey.c
index c285aa6..4b96ed7 100644
--- a/crypto/x509/x_pubkey.c
+++ b/crypto/x509/x_pubkey.c
@@ -60,6 +60,7 @@
 #include <openssl/evp.h>
 #include <openssl/mem.h>
 #include <openssl/obj.h>
+#include <openssl/thread.h>
 #include <openssl/x509.h>
 
 #include "../evp/internal.h"
diff --git a/crypto/x509v3/v3_purp.c b/crypto/x509v3/v3_purp.c
index 5826c7b..3f175c9 100644
--- a/crypto/x509v3/v3_purp.c
+++ b/crypto/x509v3/v3_purp.c
@@ -63,6 +63,7 @@
 #include <openssl/digest.h>
 #include <openssl/mem.h>
 #include <openssl/obj.h>
+#include <openssl/thread.h>
 #include <openssl/x509_vfy.h>
 #include <openssl/x509v3.h>
 
diff --git a/crypto/x509v3/v3nametest.c b/crypto/x509v3/v3nametest.c
index b6d3632..a3197e6 100644
--- a/crypto/x509v3/v3nametest.c
+++ b/crypto/x509v3/v3nametest.c
@@ -52,6 +52,7 @@
  * (eay@cryptsoft.com).  This product includes software written by Tim
  * Hudson (tjh@cryptsoft.com). */
 
+#include <stdarg.h>
 #include <string.h>
 
 #include <openssl/crypto.h>
diff --git a/decrepit/cast/cast.c b/decrepit/cast/cast.c
index bd2c0e9..68bcbe3 100644
--- a/decrepit/cast/cast.c
+++ b/decrepit/cast/cast.c
@@ -56,6 +56,12 @@
 
 #include <openssl/cast.h>
 
+#if defined(OPENSSL_WINDOWS)
+#pragma warning(push, 3)
+#include <intrin.h>
+#pragma warning(pop)
+#endif
+
 #include "../macros.h"
 
 
diff --git a/include/openssl/asn1.h b/include/openssl/asn1.h
index 4baf81c..7c62161 100644
--- a/include/openssl/asn1.h
+++ b/include/openssl/asn1.h
@@ -61,8 +61,6 @@
 
 #include <openssl/base.h>
 
-#include <time.h>
-
 #include <openssl/bio.h>
 #include <openssl/stack.h>
 
diff --git a/include/openssl/asn1t.h b/include/openssl/asn1t.h
index 72eb2cb..6c91134 100644
--- a/include/openssl/asn1t.h
+++ b/include/openssl/asn1t.h
@@ -58,7 +58,7 @@
 #ifndef HEADER_ASN1T_H
 #define HEADER_ASN1T_H
 
-#include <stddef.h>
+#include <openssl/base.h>
 #include <openssl/asn1.h>
 
 #ifdef OPENSSL_BUILD_SHLIBCRYPTO
diff --git a/include/openssl/base.h b/include/openssl/base.h
index 217908c..77549f7 100644
--- a/include/openssl/base.h
+++ b/include/openssl/base.h
@@ -56,8 +56,8 @@
 
 /* This file should be the first included by all BoringSSL headers. */
 
+#include <stddef.h>
 #include <stdint.h>
-#include <stdlib.h>
 #include <sys/types.h>
 
 #include <openssl/opensslfeatures.h>
@@ -175,6 +175,8 @@
 typedef struct cbb_st CBB;
 typedef struct cbs_st CBS;
 typedef struct conf_st CONF;
+typedef struct conf_value_st CONF_VALUE;
+typedef int CRYPTO_THREADID;
 typedef struct dh_method DH_METHOD;
 typedef struct dh_st DH;
 typedef struct dsa_method DSA_METHOD;
diff --git a/include/openssl/bio.h b/include/openssl/bio.h
index 0a637e4..6ce25bb 100644
--- a/include/openssl/bio.h
+++ b/include/openssl/bio.h
@@ -59,7 +59,6 @@
 
 #include <openssl/base.h>
 
-#include <stdarg.h>
 #include <stdio.h>  /* For FILE */
 
 #include <openssl/err.h> /* for ERR_print_errors_fp */
diff --git a/include/openssl/conf.h b/include/openssl/conf.h
index 05bad9b..84fc94f 100644
--- a/include/openssl/conf.h
+++ b/include/openssl/conf.h
@@ -79,11 +79,11 @@
  *
  * Config files are representated by a |CONF|. */
 
-typedef struct {
+struct conf_value_st {
   char *section;
   char *name;
   char *value;
-} CONF_VALUE;
+};
 
 struct conf_st {
   LHASH_OF(CONF_VALUE) *data;
diff --git a/include/openssl/crypto.h b/include/openssl/crypto.h
index 44793f4..1dc8346 100644
--- a/include/openssl/crypto.h
+++ b/include/openssl/crypto.h
@@ -17,7 +17,6 @@
 
 #include <openssl/base.h>
 
-#include <openssl/mem.h>
 
 #if defined(__cplusplus)
 extern "C" {
diff --git a/include/openssl/err.h b/include/openssl/err.h
index 738e8c5..552ea92 100644
--- a/include/openssl/err.h
+++ b/include/openssl/err.h
@@ -112,8 +112,6 @@
 #include <stdio.h>
 
 #include <openssl/base.h>
-#include <openssl/thread.h>
-#include <openssl/lhash.h>
 
 #if defined(__cplusplus)
 extern "C" {
diff --git a/include/openssl/evp.h b/include/openssl/evp.h
index 24fd038..ded7a58 100644
--- a/include/openssl/evp.h
+++ b/include/openssl/evp.h
@@ -58,7 +58,6 @@
 #define OPENSSL_HEADER_EVP_H
 
 #include <openssl/base.h>
-#include <openssl/stack.h>
 
 /* OpenSSL included digest and cipher functions in this header so we include
  * them for users that still expect that.
@@ -67,9 +66,7 @@
 #include <openssl/aead.h>
 #include <openssl/cipher.h>
 #include <openssl/digest.h>
-#include <openssl/mem.h>
 #include <openssl/obj.h>
-#include <openssl/thread.h>
 
 #if defined(__cplusplus)
 extern "C" {
diff --git a/include/openssl/mem.h b/include/openssl/mem.h
index 3bd01c0..42ec46a 100644
--- a/include/openssl/mem.h
+++ b/include/openssl/mem.h
@@ -58,6 +58,7 @@
 
 #include <openssl/base.h>
 
+#include <stdlib.h>
 #include <stdarg.h>
 
 #if defined(__cplusplus)
diff --git a/include/openssl/pkcs8.h b/include/openssl/pkcs8.h
index e0d7b10..8dc7731 100644
--- a/include/openssl/pkcs8.h
+++ b/include/openssl/pkcs8.h
@@ -58,11 +58,9 @@
 #define OPENSSL_HEADER_PKCS8_H
 
 #include <openssl/base.h>
-
-#include <stdio.h>
-
 #include <openssl/x509.h>
 
+
 #if defined(__cplusplus)
 extern "C" {
 #endif
diff --git a/include/openssl/time_support.h b/include/openssl/time_support.h
index d03a99d..912e672 100644
--- a/include/openssl/time_support.h
+++ b/include/openssl/time_support.h
@@ -60,7 +60,6 @@
 
 #include <openssl/base.h>
 
-#include <time.h>
 
 #if defined(__cplusplus)
 extern "C" {
diff --git a/ssl/ssl_asn1.c b/ssl/ssl_asn1.c
index 712af49..e4a7f0d 100644
--- a/ssl/ssl_asn1.c
+++ b/ssl/ssl_asn1.c
@@ -85,6 +85,7 @@
 
 #include <openssl/bytestring.h>
 #include <openssl/err.h>
+#include <openssl/mem.h>
 #include <openssl/x509.h>
 
 #include "internal.h"
diff --git a/tool/args.cc b/tool/args.cc
index a164476..b7d7047 100644
--- a/tool/args.cc
+++ b/tool/args.cc
@@ -16,7 +16,6 @@
 #include <vector>
 
 #include <stdio.h>
-#include <string.h>
 
 #include "internal.h"
 
@@ -30,7 +29,7 @@
     const std::string &arg = args[i];
     const struct argument *templ = nullptr;
     for (size_t j = 0; templates[j].name[0] != 0; j++) {
-      if (strcmp(arg.c_str(), templates[j].name) == 0) {
+      if (arg != std::string(templates[j].name)) {
         templ = &templates[j];
         break;
       }
diff --git a/tool/client.cc b/tool/client.cc
index f278478..1de0df2 100644
--- a/tool/client.cc
+++ b/tool/client.cc
@@ -14,13 +14,6 @@
 
 #include <openssl/base.h>
 
-#include <string>
-#include <vector>
-
-#include <errno.h>
-#include <stdlib.h>
-#include <sys/types.h>
-
 #include <openssl/err.h>
 #include <openssl/pem.h>
 #include <openssl/ssl.h>
diff --git a/tool/const.cc b/tool/const.cc
index 219fd3c..5222667 100644
--- a/tool/const.cc
+++ b/tool/const.cc
@@ -12,8 +12,8 @@
  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
 
+#include <stddef.h>
 #include <stdint.h>
-#include <stdlib.h>
 
 extern "C" {
 
diff --git a/tool/digest.cc b/tool/digest.cc
index 4acf97c..bed591c 100644
--- a/tool/digest.cc
+++ b/tool/digest.cc
@@ -21,12 +21,11 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <limits.h>
-#include <stdio.h>
-#include <string.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 
 #if !defined(OPENSSL_WINDOWS)
+#include <string.h>
 #include <unistd.h>
 #if !defined(O_BINARY)
 #define O_BINARY 0
diff --git a/tool/server.cc b/tool/server.cc
index 2890b09..164d6a5 100644
--- a/tool/server.cc
+++ b/tool/server.cc
@@ -14,13 +14,6 @@
 
 #include <openssl/base.h>
 
-#include <string>
-#include <vector>
-
-#include <errno.h>
-#include <stdlib.h>
-#include <sys/types.h>
-
 #include <openssl/err.h>
 #include <openssl/ssl.h>
 
diff --git a/tool/speed.cc b/tool/speed.cc
index 3834a68..f3698f5 100644
--- a/tool/speed.cc
+++ b/tool/speed.cc
@@ -19,7 +19,6 @@
 
 #include <stdint.h>
 #include <string.h>
-#include <time.h>
 
 #include <openssl/aead.h>
 #include <openssl/digest.h>
diff --git a/tool/transport_common.cc b/tool/transport_common.cc
index 8d6013b..ac4dfe1 100644
--- a/tool/transport_common.cc
+++ b/tool/transport_common.cc
@@ -18,7 +18,7 @@
 #include <vector>
 
 #include <errno.h>
-#include <stdlib.h>
+#include <stddef.h>
 #include <string.h>
 #include <sys/types.h>