Index: aclocal/tcltls_openssl.m4
==================================================================
--- aclocal/tcltls_openssl.m4
+++ aclocal/tcltls_openssl.m4
@@ -13,14 +13,19 @@
 			])
 		], [
 			AC_LANG_PUSH(C)
 			AC_MSG_CHECKING([for $3 protocol support])
 			AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
-#include <openssl/ssl.h>
-#include <openssl/opensslv.h>
+#ifdef OPENSSL_HEADER_PREFIX
+#  define OPENSSL_HEADER(header) <OPENSSL_HEADER_PREFIX/header>
+#else
+#  define OPENSSL_HEADER(header) <header>
+#endif
+#include OPENSSL_HEADER(openssl/ssl.h)
+#include OPENSSL_HEADER(openssl/opensslv.h)
 #if (SSLEAY_VERSION_NUMBER >= 0x0907000L)
-# include <openssl/conf.h>
+# include OPENSSL_HEADER(openssl/conf.h)
 #endif
 			], [
 int x = $5;
 			])], [
 				AC_MSG_RESULT([yes])
@@ -81,10 +86,13 @@
 			openssldir="`AS_DIRNAME(["$openssldir"])`"
 		else
 			TCLTLS_SSL_LIBS="-L$openssldir/lib -lssl -lcrypto"
 		fi
 		TCLTLS_SSL_CFLAGS="-I$openssldir/include"
+		if test -n "$openssldir"; then
+			AC_DEFINE_UNQUOTED(OPENSSL_HEADER_PREFIX, [$openssldir/include], [Path to OpenSSL headers])
+		fi
 		TCLTLS_SSL_CPPFLAGS="-I$openssldir/include"
 	fi
 
 	pkgConfigExtraArgs=''
 	if test "$TCLEXT_BUILD" = "static" -o "$TCLEXT_TLS_STATIC_SSL" = 'yes'; then
@@ -112,10 +120,14 @@
 	if test -z "$TCLTLS_SSL_CFLAGS"; then
 		TCLTLS_SSL_CFLAGS="`"${PKGCONFIG}" openssl --cflags-only-other $pkgConfigExtraArgs`" || AC_MSG_ERROR([Unable to get OpenSSL Configuration])
 	fi
 	if test -z "$TCLTLS_SSL_CPPFLAGS"; then
 		TCLTLS_SSL_CPPFLAGS="`"${PKGCONFIG}" openssl --cflags-only-I $pkgConfigExtraArgs`" || AC_MSG_ERROR([Unable to get OpenSSL Configuration])
+		opensslincdir="$(echo "${TCLTLS_SSL_CPPFLAGS}" | sed 's@^.*-I@@')"
+		if test -n "$opensslincdir"; then
+			AC_DEFINE_UNQUOTED(OPENSSL_HEADER_PREFIX, [$opensslincdir], [Path to OpenSSL headers])
+		fi
 	fi
 	PKG_CONFIG_PATH="${PKG_CONFIG_PATH_SAVE}"
 
 	if test "$TCLEXT_BUILD" = "static"; then
 		dnl If we are doing a static build, save the linker flags for other programs to consume
@@ -164,14 +176,19 @@
 
 	dnl Verify that basic functionality is there
 	AC_LANG_PUSH(C)
 	AC_MSG_CHECKING([if a basic OpenSSL program works])
 	AC_LINK_IFELSE([AC_LANG_PROGRAM([
-#include <openssl/ssl.h>
-#include <openssl/opensslv.h>
+#ifdef OPENSSL_HEADER_PREFIX
+#  define OPENSSL_HEADER(header) <OPENSSL_HEADER_PREFIX/header>
+#else
+#  define OPENSSL_HEADER(header) <header>
+#endif
+#include OPENSSL_HEADER(openssl/ssl.h)
+#include OPENSSL_HEADER(openssl/opensslv.h)
 #if (SSLEAY_VERSION_NUMBER >= 0x0907000L)
-# include <openssl/conf.h>
+# include OPENSSL_HEADER(openssl/conf.h)
 #endif
 		], [
   SSL_library_init();
   SSL_load_error_strings();
 		])], [
@@ -192,13 +209,18 @@
 
 	AC_CACHE_VAL([tcltls_cv_func_tlsext_hostname], [
 		AC_LANG_PUSH(C)
 		AC_MSG_CHECKING([for SSL_set_tlsext_host_name])
 		AC_LINK_IFELSE([AC_LANG_PROGRAM([
-#include <openssl/ssl.h>
+#ifdef OPENSSL_HEADER_PREFIX
+#  define OPENSSL_HEADER(header) <OPENSSL_HEADER_PREFIX/header>
+#else
+#  define OPENSSL_HEADER(header) <header>
+#endif
+#include OPENSSL_HEADER(openssl/ssl.h)
 #if (SSLEAY_VERSION_NUMBER >= 0x0907000L)
-# include <openssl/conf.h>
+# include OPENSSL_HEADER(openssl/conf.h)
 #endif
 			], [
   (void)SSL_set_tlsext_host_name((void *) 0, (void *) 0);
 			])], [
 			AC_MSG_RESULT([yes])

Index: tls.c
==================================================================
--- tls.c
+++ tls.c
@@ -106,14 +106,14 @@
  * Thread-Safe TLS Code
  */
 
 #ifdef TCL_THREADS
 #define OPENSSL_THREAD_DEFINES
-#include <openssl/opensslconf.h>
+#include OPENSSL_HEADER(openssl/opensslconf.h)
 
 #ifdef OPENSSL_THREADS
-#include <openssl/crypto.h>
+#include OPENSSL_HEADER(openssl/crypto.h)
 
 /*
  * Threaded operation requires locking callbacks
  * Based from /crypto/cryptlib.c of OpenSSL and NSOpenSSL.
  */

Index: tls.h
==================================================================
--- tls.h
+++ tls.h
@@ -16,11 +16,15 @@
  *
  */
 #ifndef _TLS_H
 #define _TLS_H
 
+#define mp_digit __TCL_MP_DIGIT
+#define mp_int   __TCL_MP_INT
 #include <tcl.h>
+#undef mp_digit
+#undef mp_int
 
 /*
  * Initialization routines -- our entire public C API.
  */
 int Tls_Init(Tcl_Interp *interp);

Index: tlsInt.h
==================================================================
--- tlsInt.h
+++ tlsInt.h
@@ -20,10 +20,16 @@
 
 #include "tls.h"
 #include <errno.h>
 #include <string.h>
 #include <stdint.h>
+
+#ifdef OPENSSL_HEADER_PREFIX
+#  define OPENSSL_HEADER(header) <OPENSSL_HEADER_PREFIX/header>
+#else
+#  define OPENSSL_HEADER(header) <header>
+#endif
 
 #ifdef __WIN32__
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 #include <wincrypt.h> /* OpenSSL needs this on Windows */
@@ -43,14 +49,14 @@
 #  ifndef NO_SSL2
 #    define NO_SSL2
 #  endif
 #endif
 
-#include <openssl/ssl.h>
-#include <openssl/err.h>
-#include <openssl/rand.h>
-#include <openssl/opensslv.h>
+#include OPENSSL_HEADER(openssl/ssl.h)
+#include OPENSSL_HEADER(openssl/err.h)
+#include OPENSSL_HEADER(openssl/rand.h)
+#include OPENSSL_HEADER(openssl/opensslv.h)
 
 /*
  * Determine if we should use the pre-OpenSSL 1.1.0 API
  */
 #undef TCLTLS_OPENSSL_PRE_1_1