Ticket #56965: 356to366_Modules__ssl.c.diff
File 356to366_Modules__ssl.c.diff, 6.2 KB (added by dubiousjim, 6 years ago) |
---|
-
.6/Modules/_ssl.c
old new 101 101 102 102 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER) 103 103 # define OPENSSL_VERSION_1_1 1 104 # define PY_OPENSSL_1_1_API 1 105 #endif 106 107 /* LibreSSL 2.7.0 provides necessary OpenSSL 1.1.0 APIs */ 108 #if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x2070000fL 109 # define PY_OPENSSL_1_1_API 1 104 110 #endif 105 111 106 112 /* Openssl comes with TLSv1.1 and TLSv1.2 between 1.0.0h and 1.0.1 … … 122 128 #endif 123 129 124 130 #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation 125 # define HAVE_ALPN 131 # define HAVE_ALPN 1 132 #else 133 # define HAVE_ALPN 0 134 #endif 135 136 /* We cannot rely on OPENSSL_NO_NEXTPROTONEG because LibreSSL 2.6.1 dropped 137 * NPN support but did not set OPENSSL_NO_NEXTPROTONEG for compatibility 138 * reasons. The check for TLSEXT_TYPE_next_proto_neg works with 139 * OpenSSL 1.0.1+ and LibreSSL. 140 * OpenSSL 1.1.1-pre1 dropped NPN but still has TLSEXT_TYPE_next_proto_neg. 141 */ 142 #ifdef OPENSSL_NO_NEXTPROTONEG 143 # define HAVE_NPN 0 144 #elif (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER) 145 # define HAVE_NPN 0 146 #elif defined(TLSEXT_TYPE_next_proto_neg) 147 # define HAVE_NPN 1 148 #else 149 # define HAVE_NPN 0 126 150 #endif 127 151 128 152 #ifndef INVALID_SOCKET /* MS defines this */ 129 153 #define INVALID_SOCKET (-1) 130 154 #endif 131 155 132 #ifdef OPENSSL_VERSION_1_1 133 /* OpenSSL 1.1.0+ */ 134 #ifndef OPENSSL_NO_SSL2 135 #define OPENSSL_NO_SSL2 136 #endif 137 #else /* OpenSSL < 1.1.0 */ 138 #if defined(WITH_THREAD) 156 /* OpenSSL 1.0.2 and LibreSSL needs extra code for locking */ 157 #if !defined(OPENSSL_VERSION_1_1) && defined(WITH_THREAD) 139 158 #define HAVE_OPENSSL_CRYPTO_LOCK 140 159 #endif 141 160 161 #if defined(OPENSSL_VERSION_1_1) && !defined(OPENSSL_NO_SSL2) 162 #define OPENSSL_NO_SSL2 163 #endif 164 165 #ifndef PY_OPENSSL_1_1_API 166 /* OpenSSL 1.1 API shims for OpenSSL < 1.1.0 and LibreSSL < 2.7.0 */ 167 142 168 #define TLS_method SSLv23_method 143 169 144 170 static int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne) … … 187 213 { 188 214 return store->param; 189 215 } 190 #endif /* OpenSSL < 1.1.0 or LibreSSL */216 #endif /* OpenSSL < 1.1.0 or LibreSSL < 2.7.0 */ 191 217 192 218 193 219 enum py_ssl_error { … … 260 286 typedef struct { 261 287 PyObject_HEAD 262 288 SSL_CTX *ctx; 263 #if def OPENSSL_NPN_NEGOTIATED289 #if HAVE_NPN 264 290 unsigned char *npn_protocols; 265 291 int npn_protocols_len; 266 292 #endif 267 #if defHAVE_ALPN293 #if HAVE_ALPN 268 294 unsigned char *alpn_protocols; 269 295 int alpn_protocols_len; 270 296 #endif … … 611 637 612 638 #if HAVE_SNI 613 639 if (server_hostname != NULL) 614 SSL_set_tlsext_host_name(self->ssl, server_hostname);640 SSL_set_tlsext_host_name(self->ssl, server_hostname); 615 641 #endif 616 642 617 643 /* If the socket is in non-blocking mode or timeout mode, set the BIO … … 1605 1631 return PyUnicode_FromString(version); 1606 1632 } 1607 1633 1608 #if def OPENSSL_NPN_NEGOTIATED1634 #if HAVE_NPN 1609 1635 /*[clinic input] 1610 1636 _ssl._SSLSocket.selected_npn_protocol 1611 1637 [clinic start generated code]*/ … … 1626 1652 } 1627 1653 #endif 1628 1654 1629 #if defHAVE_ALPN1655 #if HAVE_ALPN 1630 1656 /*[clinic input] 1631 1657 _ssl._SSLSocket.selected_alpn_protocol 1632 1658 [clinic start generated code]*/ … … 2375 2401 return NULL; 2376 2402 } 2377 2403 self->ctx = ctx; 2378 #if def OPENSSL_NPN_NEGOTIATED2404 #if HAVE_NPN 2379 2405 self->npn_protocols = NULL; 2380 2406 #endif 2381 #if defHAVE_ALPN2407 #if HAVE_ALPN 2382 2408 self->alpn_protocols = NULL; 2383 2409 #endif 2384 2410 #ifndef OPENSSL_NO_TLSEXT 2385 2411 self->set_hostname = NULL; 2386 2412 #endif 2387 2413 /* Don't check host name by default */ 2388 self->check_hostname = 0;2414 self->check_hostname = 0; 2389 2415 /* Defaults */ 2390 2416 SSL_CTX_set_verify(self->ctx, SSL_VERIFY_NONE, NULL); 2391 2417 options = SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; … … 2469 2495 PyObject_GC_UnTrack(self); 2470 2496 context_clear(self); 2471 2497 SSL_CTX_free(self->ctx); 2472 #if def OPENSSL_NPN_NEGOTIATED2498 #if HAVE_NPN 2473 2499 PyMem_FREE(self->npn_protocols); 2474 2500 #endif 2475 #if defHAVE_ALPN2501 #if HAVE_ALPN 2476 2502 PyMem_FREE(self->alpn_protocols); 2477 2503 #endif 2478 2504 Py_TYPE(self)->tp_free(self); … … 2501 2527 Py_RETURN_NONE; 2502 2528 } 2503 2529 2504 #if def OPENSSL_NPN_NEGOTIATED2530 #if HAVE_NPN || HAVE_ALPN 2505 2531 static int 2506 2532 do_protocol_selection(int alpn, unsigned char **out, unsigned char *outlen, 2507 2533 const unsigned char *server_protocols, unsigned int server_protocols_len, … … 2525 2551 2526 2552 return SSL_TLSEXT_ERR_OK; 2527 2553 } 2554 #endif 2528 2555 2556 #if HAVE_NPN 2529 2557 /* this callback gets passed to SSL_CTX_set_next_protos_advertise_cb */ 2530 2558 static int 2531 2559 _advertiseNPN_cb(SSL *s, … … 2568 2596 Py_buffer *protos) 2569 2597 /*[clinic end generated code: output=72b002c3324390c6 input=319fcb66abf95bd7]*/ 2570 2598 { 2571 #if def OPENSSL_NPN_NEGOTIATED2599 #if HAVE_NPN 2572 2600 PyMem_Free(self->npn_protocols); 2573 2601 self->npn_protocols = PyMem_Malloc(protos->len); 2574 2602 if (self->npn_protocols == NULL) … … 2593 2621 #endif 2594 2622 } 2595 2623 2596 #if defHAVE_ALPN2624 #if HAVE_ALPN 2597 2625 static int 2598 2626 _selectALPN_cb(SSL *s, 2599 2627 const unsigned char **out, unsigned char *outlen, … … 2618 2646 Py_buffer *protos) 2619 2647 /*[clinic end generated code: output=87599a7f76651a9b input=9bba964595d519be]*/ 2620 2648 { 2621 #if defHAVE_ALPN2649 #if HAVE_ALPN 2622 2650 PyMem_FREE(self->alpn_protocols); 2623 2651 self->alpn_protocols = PyMem_Malloc(protos->len); 2624 2652 if (!self->alpn_protocols) … … 4630 4658 return NULL; 4631 4659 PySocketModule = *socket_api; 4632 4660 4661 #ifndef OPENSSL_VERSION_1_1 4662 /* Load all algorithms and initialize cpuid */ 4663 OPENSSL_add_all_algorithms_noconf(); 4633 4664 /* Init OpenSSL */ 4634 4665 SSL_load_error_strings(); 4635 4666 SSL_library_init(); 4667 #endif 4668 4636 4669 #ifdef WITH_THREAD 4637 4670 #ifdef HAVE_OPENSSL_CRYPTO_LOCK 4638 4671 /* note that this will start threading if not already started */ … … 4644 4677 _ssl_locks_count++; 4645 4678 #endif 4646 4679 #endif /* WITH_THREAD */ 4647 OpenSSL_add_all_algorithms();4648 4680 4649 4681 /* Add symbols to module dict */ 4650 4682 sslerror_type_slots[0].pfunc = PyExc_OSError; … … 4843 4875 Py_INCREF(r); 4844 4876 PyModule_AddObject(m, "HAS_ECDH", r); 4845 4877 4846 #if def OPENSSL_NPN_NEGOTIATED4878 #if HAVE_NPN 4847 4879 r = Py_True; 4848 4880 #else 4849 4881 r = Py_False; … … 4851 4883 Py_INCREF(r); 4852 4884 PyModule_AddObject(m, "HAS_NPN", r); 4853 4885 4854 #if defHAVE_ALPN4886 #if HAVE_ALPN 4855 4887 r = Py_True; 4856 4888 #else 4857 4889 r = Py_False;