You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
8.8 KiB

  1. From bb7d7a803665005cc72ad68a388e9e937ff3d2f6 Mon Sep 17 00:00:00 2001
  2. From: Josef Schlehofer <pepe.schlehofer@gmail.com>
  3. Date: Sat, 23 Mar 2019 21:02:17 +0100
  4. Subject: [PATCH] support for mbedTLS
  5. ---
  6. INSTALL.rst | 4 ++--
  7. doc/thread-safety.rst | 2 +-
  8. setup.py | 28 +++++++++++++++++++++-------
  9. src/module.c | 6 ++++--
  10. src/pycurl.h | 7 ++++++-
  11. src/threadsupport.c | 39 +++++++++++++++++++++++++++++++++++++++
  12. 6 files changed, 73 insertions(+), 13 deletions(-)
  13. diff --git a/INSTALL.rst b/INSTALL.rst
  14. index 8ad8b4f..da70d25 100644
  15. --- a/INSTALL.rst
  16. +++ b/INSTALL.rst
  17. @@ -53,7 +53,7 @@ It will then fail at runtime as follows::
  18. To fix this, you need to tell ``setup.py`` what SSL backend is used::
  19. - python setup.py --with-[openssl|gnutls|nss] install
  20. + python setup.py --with-[openssl|gnutls|nss|mbedtls] install
  21. Note: as of PycURL 7.21.5, setup.py accepts ``--with-openssl`` option to
  22. indicate that libcurl is built against OpenSSL. ``--with-ssl`` is an alias
  23. @@ -85,7 +85,7 @@ environment variable::
  24. The same applies to the SSL backend, if you need to specify it (see the SSL
  25. note above)::
  26. - export PYCURL_SSL_LIBRARY=[openssl|gnutls|nss]
  27. + export PYCURL_SSL_LIBRARY=[openssl|gnutls|nss|mbedtls]
  28. easy_install pycurl
  29. diff --git a/doc/thread-safety.rst b/doc/thread-safety.rst
  30. index 5ba3f3e..ae2b9e5 100644
  31. --- a/doc/thread-safety.rst
  32. +++ b/doc/thread-safety.rst
  33. @@ -21,7 +21,7 @@ For Python programs using PycURL, this means:
  34. Python code *outside of a libcurl callback for the PycURL object in question*
  35. is unsafe.
  36. -PycURL handles the necessary SSL locks for OpenSSL/LibreSSL, GnuTLS and NSS.
  37. +PycURL handles the necessary SSL locks for OpenSSL/LibreSSL, GnuTLS, NSS and mbedTLS.
  38. A special situation exists when libcurl uses the standard C library
  39. name resolver (i.e., not threaded nor c-ares resolver). By default libcurl
  40. diff --git a/setup.py b/setup.py
  41. index e1e6925..5ab437f 100644
  42. --- a/setup.py
  43. +++ b/setup.py
  44. @@ -143,6 +143,7 @@ class ExtensionConfiguration(object):
  45. '--with-ssl': self.using_openssl,
  46. '--with-gnutls': self.using_gnutls,
  47. '--with-nss': self.using_nss,
  48. + '--with-mbedtls': self.using_mbedtls,
  49. }
  50. def detect_ssl_option(self):
  51. @@ -152,20 +153,20 @@ class ExtensionConfiguration(object):
  52. if option != other_option:
  53. if scan_argv(self.argv, other_option) is not None:
  54. raise ConfigurationError('Cannot give both %s and %s' % (option, other_option))
  55. -
  56. +
  57. return option
  58. def detect_ssl_backend(self):
  59. ssl_lib_detected = False
  60. -
  61. +
  62. if 'PYCURL_SSL_LIBRARY' in os.environ:
  63. ssl_lib = os.environ['PYCURL_SSL_LIBRARY']
  64. - if ssl_lib in ['openssl', 'gnutls', 'nss']:
  65. + if ssl_lib in ['openssl', 'gnutls', 'nss', 'mbedtls']:
  66. ssl_lib_detected = True
  67. getattr(self, 'using_%s' % ssl_lib)()
  68. else:
  69. raise ConfigurationError('Invalid value "%s" for PYCURL_SSL_LIBRARY' % ssl_lib)
  70. -
  71. +
  72. option = self.detect_ssl_option()
  73. if option:
  74. ssl_lib_detected = True
  75. @@ -194,6 +195,10 @@ class ExtensionConfiguration(object):
  76. self.using_nss()
  77. ssl_lib_detected = True
  78. break
  79. + if arg[2:] == 'mbedtls':
  80. + self.using_nss()
  81. + ssl_lib_detected = True
  82. + break
  83. if not ssl_lib_detected and len(self.argv) == len(self.original_argv) \
  84. and not os.environ.get('PYCURL_CURL_CONFIG') \
  85. @@ -201,7 +206,7 @@ class ExtensionConfiguration(object):
  86. # this path should only be taken when no options or
  87. # configuration environment variables are given to setup.py
  88. ssl_lib_detected = self.detect_ssl_lib_on_centos6()
  89. -
  90. +
  91. self.ssl_lib_detected = ssl_lib_detected
  92. def curl_config(self):
  93. @@ -301,7 +306,7 @@ class ExtensionConfiguration(object):
  94. if errtext:
  95. msg += ":\n" + errtext
  96. raise ConfigurationError(msg)
  97. -
  98. +
  99. # hack
  100. self.sslhintbuf = sslhintbuf
  101. @@ -327,7 +332,7 @@ specify the SSL backend manually.''')
  102. self.library_dirs.append(arg[2:])
  103. else:
  104. self.extra_link_args.append(arg)
  105. -
  106. +
  107. if not self.libraries:
  108. self.libraries.append("curl")
  109. @@ -354,6 +359,9 @@ specify the SSL backend manually.''')
  110. elif ssl_version.startswith('NSS/'):
  111. self.using_nss()
  112. ssl_lib_detected = True
  113. + elif ssl_version.startswith('mbedTLS/'):
  114. + self.using_mbedtls()
  115. + ssl_lib_detected = 'mbedtls'
  116. return ssl_lib_detected
  117. def detect_ssl_lib_on_centos6(self):
  118. @@ -505,6 +513,11 @@ specify the SSL backend manually.''')
  119. self.libraries.append('ssl3')
  120. self.define_macros.append(('HAVE_CURL_SSL', 1))
  121. + def using_mbedtls(self):
  122. + self.define_macros.append(('HAVE_CURL_MBEDTLS', 1))
  123. + self.libraries.append('mbedtls')
  124. + self.define_macros.append(('HAVE_CURL_SSL', 1))
  125. +
  126. def get_bdist_msi_version_hack():
  127. # workaround for distutils/msi version requirement per
  128. # epydoc.sourceforge.net/stdlib/distutils.version.StrictVersion-class.html -
  129. @@ -871,6 +884,7 @@ PycURL Unix options:
  130. --with-ssl legacy alias for --with-openssl
  131. --with-gnutls libcurl is linked against GnuTLS
  132. --with-nss libcurl is linked against NSS
  133. + --with-mbedtls libcurl is linked against mbedTLS
  134. '''
  135. windows_help = '''\
  136. diff --git a/src/module.c b/src/module.c
  137. index 2331ae8..7fdb25a 100644
  138. --- a/src/module.c
  139. +++ b/src/module.c
  140. @@ -328,7 +328,7 @@ initpycurl(void)
  141. PyObject *collections_module = NULL;
  142. PyObject *named_tuple = NULL;
  143. PyObject *arglist = NULL;
  144. -
  145. +
  146. assert(Curl_Type.tp_weaklistoffset > 0);
  147. assert(CurlMulti_Type.tp_weaklistoffset > 0);
  148. assert(CurlShare_Type.tp_weaklistoffset > 0);
  149. @@ -355,6 +355,8 @@ initpycurl(void)
  150. runtime_ssl_lib = "gnutls";
  151. } else if (!strncmp(vi->ssl_version, "NSS/", 4)) {
  152. runtime_ssl_lib = "nss";
  153. + } else if (!strncmp(vi->ssl_version, "mbedTLS/", 2)) {
  154. + runtime_ssl_lib = "mbedtls";
  155. } else {
  156. runtime_ssl_lib = "none/other";
  157. }
  158. @@ -461,7 +463,7 @@ initpycurl(void)
  159. /* constants for ioctl callback argument values */
  160. insint_c(d, "IOCMD_NOP", CURLIOCMD_NOP);
  161. insint_c(d, "IOCMD_RESTARTREAD", CURLIOCMD_RESTARTREAD);
  162. -
  163. +
  164. /* opensocketfunction return value */
  165. insint_c(d, "SOCKET_BAD", CURL_SOCKET_BAD);
  166. diff --git a/src/pycurl.h b/src/pycurl.h
  167. index 65290f7..2294cb8 100644
  168. --- a/src/pycurl.h
  169. +++ b/src/pycurl.h
  170. @@ -174,6 +174,11 @@ pycurl_inet_ntop (int family, void *addr, char *string, size_t string_size);
  171. # define COMPILE_SSL_LIB "gnutls"
  172. # elif defined(HAVE_CURL_NSS)
  173. # define COMPILE_SSL_LIB "nss"
  174. +# elif defined(HAVE_CURL_MBEDTLS)
  175. +# include <mbedtls/ssl.h>
  176. +# define PYCURL_NEED_SSL_TSL
  177. +# define PYCURL_NEED_MBEDTLS_TSL
  178. +# define COMPILE_SSL_LIB "mbedtls"
  179. # else
  180. # ifdef _MSC_VER
  181. /* sigh */
  182. @@ -190,7 +195,7 @@ pycurl_inet_ntop (int family, void *addr, char *string, size_t string_size);
  183. /* since we have no crypto callbacks for other ssl backends,
  184. * no reason to require users match those */
  185. # define COMPILE_SSL_LIB "none/other"
  186. -# endif /* HAVE_CURL_OPENSSL || HAVE_CURL_GNUTLS || HAVE_CURL_NSS */
  187. +# endif /* HAVE_CURL_OPENSSL || HAVE_CURL_GNUTLS || HAVE_CURL_NSS || HAVE_CURL_MBEDTLS */
  188. #else
  189. # define COMPILE_SSL_LIB "none/other"
  190. #endif /* HAVE_CURL_SSL */
  191. diff --git a/src/threadsupport.c b/src/threadsupport.c
  192. index 6ca07f5..51abffd 100644
  193. --- a/src/threadsupport.c
  194. +++ b/src/threadsupport.c
  195. @@ -232,6 +232,45 @@ pycurl_ssl_cleanup(void)
  196. }
  197. #endif
  198. +/* mbedTLS */
  199. +
  200. +#ifdef PYCURL_NEED_MBEDTLS_TSL
  201. +static int
  202. +pycurl_ssl_mutex_create(void **m)
  203. +{
  204. + if ((*((PyThread_type_lock *) m) = PyThread_allocate_lock()) == NULL) {
  205. + return -1;
  206. + } else {
  207. + return 0;
  208. + }
  209. +}
  210. +
  211. +static int
  212. +pycurl_ssl_mutex_destroy(void **m)
  213. +{
  214. + PyThread_free_lock(*((PyThread_type_lock *) m));
  215. + return 0;
  216. +}
  217. +
  218. +static int
  219. +pycurl_ssl_mutex_lock(void **m)
  220. +{
  221. + return !PyThread_acquire_lock(*((PyThread_type_lock *) m), 1);
  222. +}
  223. +
  224. +PYCURL_INTERNAL int
  225. +pycurl_ssl_init(void)
  226. +{
  227. + return 0;
  228. +}
  229. +
  230. +PYCURL_INTERNAL void
  231. +pycurl_ssl_cleanup(void)
  232. +{
  233. + return;
  234. +}
  235. +#endif
  236. +
  237. /*************************************************************************
  238. // CurlShareObject
  239. **************************************************************************/
  240. --
  241. 2.17.0.windows.1