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.

218 lines
7.8 KiB

  1. From 991f0176e188227647bf4c993d8da81cf794b3ae Mon Sep 17 00:00:00 2001
  2. From: Christian Heimes <christian@python.org>
  3. Date: Sun, 25 Feb 2018 20:03:07 +0100
  4. Subject: [PATCH] bpo-30008: SSL module: emulate tls methods
  5. OpenSSL 1.1 compatility: emulate version specific TLS methods with
  6. SSL_CTX_set_min/max_proto_version().
  7. ---
  8. .../2018-02-25-20-05-51.bpo-30008.6Bmyhr.rst | 4 +
  9. Modules/_ssl.c | 134 ++++++++++++++----
  10. 2 files changed, 108 insertions(+), 30 deletions(-)
  11. create mode 100644 Misc/NEWS.d/next/Library/2018-02-25-20-05-51.bpo-30008.6Bmyhr.rst
  12. --- /dev/null
  13. +++ b/Misc/NEWS.d/next/Library/2018-02-25-20-05-51.bpo-30008.6Bmyhr.rst
  14. @@ -0,0 +1,4 @@
  15. +The ssl module no longer uses function that are deprecated since OpenSSL
  16. +1.1.0. The version specific TLS methods are emulated with TLS_method() plus
  17. +SSL_CTX_set_min/max_proto_version(). Pseudo random numbers are generated
  18. +with RAND_bytes().
  19. --- a/Modules/_ssl.c
  20. +++ b/Modules/_ssl.c
  21. @@ -45,14 +45,6 @@ static PySocketModule_APIObject PySocket
  22. #include <sys/poll.h>
  23. #endif
  24. -/* Don't warn about deprecated functions */
  25. -#ifdef __GNUC__
  26. -#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  27. -#endif
  28. -#ifdef __clang__
  29. -#pragma clang diagnostic ignored "-Wdeprecated-declarations"
  30. -#endif
  31. -
  32. /* Include OpenSSL header files */
  33. #include "openssl/rsa.h"
  34. #include "openssl/crypto.h"
  35. @@ -205,6 +197,7 @@ static void _PySSLFixErrno(void) {
  36. #ifndef PY_OPENSSL_1_1_API
  37. /* OpenSSL 1.1 API shims for OpenSSL < 1.1.0 and LibreSSL < 2.7.0 */
  38. +#define ASN1_STRING_get0_data ASN1_STRING_data
  39. #define TLS_method SSLv23_method
  40. #define TLS_client_method SSLv23_client_method
  41. #define TLS_server_method SSLv23_server_method
  42. @@ -896,7 +889,7 @@ _ssl_configure_hostname(PySSLSocket *sel
  43. goto error;
  44. }
  45. } else {
  46. - if (!X509_VERIFY_PARAM_set1_ip(param, ASN1_STRING_data(ip),
  47. + if (!X509_VERIFY_PARAM_set1_ip(param, ASN1_STRING_get0_data(ip),
  48. ASN1_STRING_length(ip))) {
  49. _setSSLError(NULL, 0, __FILE__, __LINE__);
  50. goto error;
  51. @@ -1372,8 +1365,9 @@ _get_peer_alt_names (X509 *certificate)
  52. goto fail;
  53. }
  54. PyTuple_SET_ITEM(t, 0, v);
  55. - v = PyUnicode_FromStringAndSize((char *)ASN1_STRING_data(as),
  56. - ASN1_STRING_length(as));
  57. + v = PyUnicode_FromStringAndSize(
  58. + (char *)ASN1_STRING_get0_data(as),
  59. + ASN1_STRING_length(as));
  60. if (v == NULL) {
  61. Py_DECREF(t);
  62. goto fail;
  63. @@ -3078,44 +3072,124 @@ _ssl__SSLContext_impl(PyTypeObject *type
  64. long options;
  65. SSL_CTX *ctx = NULL;
  66. X509_VERIFY_PARAM *params;
  67. - int result;
  68. + int result = 0;
  69. #if defined(SSL_MODE_RELEASE_BUFFERS)
  70. unsigned long libver;
  71. #endif
  72. PySSL_BEGIN_ALLOW_THREADS
  73. - if (proto_version == PY_SSL_VERSION_TLS1)
  74. + switch (proto_version) {
  75. +#if OPENSSL_VERSION_NUMBER <= 0x10100000L
  76. + /* OpenSSL < 1.1.0 or not LibreSSL
  77. + * Use old-style methods for OpenSSL 1.0.2
  78. + */
  79. +#if defined(SSL2_VERSION) && !defined(OPENSSL_NO_SSL2)
  80. + case PY_SSL_VERSION_SSL2:
  81. + ctx = SSL_CTX_new(SSLv2_method());
  82. + break;
  83. +#endif
  84. +#if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3)
  85. + case PY_SSL_VERSION_SSL3:
  86. + ctx = SSL_CTX_new(SSLv3_method());
  87. + break;
  88. +#endif
  89. +#if defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1)
  90. + case PY_SSL_VERSION_TLS1:
  91. ctx = SSL_CTX_new(TLSv1_method());
  92. -#if HAVE_TLSv1_2
  93. - else if (proto_version == PY_SSL_VERSION_TLS1_1)
  94. + break;
  95. +#endif
  96. +#if defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1)
  97. + case PY_SSL_VERSION_TLS1_1:
  98. ctx = SSL_CTX_new(TLSv1_1_method());
  99. - else if (proto_version == PY_SSL_VERSION_TLS1_2)
  100. + break;
  101. +#endif
  102. +#if defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2)
  103. + case PY_SSL_VERSION_TLS1_2:
  104. ctx = SSL_CTX_new(TLSv1_2_method());
  105. + break;
  106. #endif
  107. -#ifndef OPENSSL_NO_SSL3
  108. - else if (proto_version == PY_SSL_VERSION_SSL3)
  109. - ctx = SSL_CTX_new(SSLv3_method());
  110. +#else
  111. + /* OpenSSL >= 1.1 or LibreSSL
  112. + * create context with TLS_method for all protocols
  113. + * no SSLv2_method in OpenSSL 1.1.
  114. + */
  115. +#if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3)
  116. + case PY_SSL_VERSION_SSL3:
  117. + ctx = SSL_CTX_new(TLS_method());
  118. + if (ctx != NULL) {
  119. + /* OpenSSL 1.1.0 sets SSL_OP_NO_SSLv3 for TLS_method by default */
  120. + SSL_CTX_clear_options(ctx, SSL_OP_NO_SSLv3);
  121. + if (!SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION))
  122. + result = -2;
  123. + if (!SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION))
  124. + result = -2;
  125. + }
  126. + break;
  127. #endif
  128. -#ifndef OPENSSL_NO_SSL2
  129. - else if (proto_version == PY_SSL_VERSION_SSL2)
  130. - ctx = SSL_CTX_new(SSLv2_method());
  131. +#if defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1)
  132. + case PY_SSL_VERSION_TLS1:
  133. + ctx = SSL_CTX_new(TLS_method());
  134. + if (ctx != NULL) {
  135. + SSL_CTX_clear_options(ctx, SSL_OP_NO_TLSv1);
  136. + if (!SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION))
  137. + result = -2;
  138. + if (!SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION))
  139. + result = -2;
  140. + }
  141. + break;
  142. +#endif
  143. +#if defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1)
  144. + case PY_SSL_VERSION_TLS1_1:
  145. + ctx = SSL_CTX_new(TLS_method());
  146. + if (ctx != NULL) {
  147. + SSL_CTX_clear_options(ctx, SSL_OP_NO_TLSv1_1);
  148. + if (!SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION))
  149. + result = -2;
  150. + if (!SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION))
  151. + result = -2;
  152. + }
  153. + break;
  154. +#endif
  155. +#if defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2)
  156. + case PY_SSL_VERSION_TLS1_2:
  157. + ctx = SSL_CTX_new(TLS_method());
  158. + if (ctx != NULL) {
  159. + SSL_CTX_clear_options(ctx, SSL_OP_NO_TLSv1_2);
  160. + if (!SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION))
  161. + result = -2;
  162. + if (!SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION))
  163. + result = -2;
  164. + }
  165. + break;
  166. #endif
  167. - else if (proto_version == PY_SSL_VERSION_TLS) /* SSLv23 */
  168. +#endif /* OpenSSL >= 1.1 */
  169. + case PY_SSL_VERSION_TLS:
  170. + /* SSLv23 */
  171. ctx = SSL_CTX_new(TLS_method());
  172. - else if (proto_version == PY_SSL_VERSION_TLS_CLIENT)
  173. + break;
  174. + case PY_SSL_VERSION_TLS_CLIENT:
  175. ctx = SSL_CTX_new(TLS_client_method());
  176. - else if (proto_version == PY_SSL_VERSION_TLS_SERVER)
  177. + break;
  178. + case PY_SSL_VERSION_TLS_SERVER:
  179. ctx = SSL_CTX_new(TLS_server_method());
  180. - else
  181. - proto_version = -1;
  182. + break;
  183. + default:
  184. + result = -1;
  185. + break;
  186. + }
  187. PySSL_END_ALLOW_THREADS
  188. - if (proto_version == -1) {
  189. + if (result == -1) {
  190. PyErr_SetString(PyExc_ValueError,
  191. "invalid protocol version");
  192. return NULL;
  193. }
  194. - if (ctx == NULL) {
  195. + else if (result == -2) {
  196. + PyErr_SetString(PyExc_ValueError,
  197. + "protocol configuration error");
  198. + return NULL;
  199. + }
  200. + else if (ctx == NULL) {
  201. _setSSLError(NULL, 0, __FILE__, __LINE__);
  202. return NULL;
  203. }
  204. @@ -5288,7 +5362,7 @@ PySSL_RAND(int len, int pseudo)
  205. if (bytes == NULL)
  206. return NULL;
  207. if (pseudo) {
  208. - ok = RAND_pseudo_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len);
  209. + ok = (_PyOS_URandom((unsigned char*)PyBytes_AS_STRING(bytes), len) == 0 ? 1 : 0);
  210. if (ok == 0 || ok == 1)
  211. return Py_BuildValue("NO", bytes, ok == 1 ? Py_True : Py_False);
  212. }