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  
    101101
    102102#if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
    103103#  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
    104110#endif
    105111
    106112/* Openssl comes with TLSv1.1 and TLSv1.2 between 1.0.0h and 1.0.1
     
    122128#endif
    123129
    124130#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
    126150#endif
    127151
    128152#ifndef INVALID_SOCKET /* MS defines this */
    129153#define INVALID_SOCKET (-1)
    130154#endif
    131155
    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)
    139158#define HAVE_OPENSSL_CRYPTO_LOCK
    140159#endif
    141160
     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
    142168#define TLS_method SSLv23_method
    143169
    144170static int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne)
     
    187213{
    188214    return store->param;
    189215}
    190 #endif /* OpenSSL < 1.1.0 or LibreSSL */
     216#endif /* OpenSSL < 1.1.0 or LibreSSL < 2.7.0 */
    191217
    192218
    193219enum py_ssl_error {
     
    260286typedef struct {
    261287    PyObject_HEAD
    262288    SSL_CTX *ctx;
    263 #ifdef OPENSSL_NPN_NEGOTIATED
     289#if HAVE_NPN
    264290    unsigned char *npn_protocols;
    265291    int npn_protocols_len;
    266292#endif
    267 #ifdef HAVE_ALPN
     293#if HAVE_ALPN
    268294    unsigned char *alpn_protocols;
    269295    int alpn_protocols_len;
    270296#endif
     
    611637
    612638#if HAVE_SNI
    613639    if (server_hostname != NULL)
    614         SSL_set_tlsext_host_name(self->ssl, server_hostname);
     640            SSL_set_tlsext_host_name(self->ssl, server_hostname);
    615641#endif
    616642
    617643    /* If the socket is in non-blocking mode or timeout mode, set the BIO
     
    16051631    return PyUnicode_FromString(version);
    16061632}
    16071633
    1608 #ifdef OPENSSL_NPN_NEGOTIATED
     1634#if HAVE_NPN
    16091635/*[clinic input]
    16101636_ssl._SSLSocket.selected_npn_protocol
    16111637[clinic start generated code]*/
     
    16261652}
    16271653#endif
    16281654
    1629 #ifdef HAVE_ALPN
     1655#if HAVE_ALPN
    16301656/*[clinic input]
    16311657_ssl._SSLSocket.selected_alpn_protocol
    16321658[clinic start generated code]*/
     
    23752401        return NULL;
    23762402    }
    23772403    self->ctx = ctx;
    2378 #ifdef OPENSSL_NPN_NEGOTIATED
     2404#if HAVE_NPN
    23792405    self->npn_protocols = NULL;
    23802406#endif
    2381 #ifdef HAVE_ALPN
     2407#if HAVE_ALPN
    23822408    self->alpn_protocols = NULL;
    23832409#endif
    23842410#ifndef OPENSSL_NO_TLSEXT
    23852411    self->set_hostname = NULL;
    23862412#endif
    23872413    /* Don't check host name by default */
    2388     self->check_hostname = 0;
     2414        self->check_hostname = 0;
    23892415    /* Defaults */
    23902416    SSL_CTX_set_verify(self->ctx, SSL_VERIFY_NONE, NULL);
    23912417    options = SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
     
    24692495    PyObject_GC_UnTrack(self);
    24702496    context_clear(self);
    24712497    SSL_CTX_free(self->ctx);
    2472 #ifdef OPENSSL_NPN_NEGOTIATED
     2498#if HAVE_NPN
    24732499    PyMem_FREE(self->npn_protocols);
    24742500#endif
    2475 #ifdef HAVE_ALPN
     2501#if HAVE_ALPN
    24762502    PyMem_FREE(self->alpn_protocols);
    24772503#endif
    24782504    Py_TYPE(self)->tp_free(self);
     
    25012527    Py_RETURN_NONE;
    25022528}
    25032529
    2504 #ifdef OPENSSL_NPN_NEGOTIATED
     2530#if HAVE_NPN || HAVE_ALPN
    25052531static int
    25062532do_protocol_selection(int alpn, unsigned char **out, unsigned char *outlen,
    25072533                      const unsigned char *server_protocols, unsigned int server_protocols_len,
     
    25252551
    25262552    return SSL_TLSEXT_ERR_OK;
    25272553}
     2554#endif
    25282555
     2556#if HAVE_NPN
    25292557/* this callback gets passed to SSL_CTX_set_next_protos_advertise_cb */
    25302558static int
    25312559_advertiseNPN_cb(SSL *s,
     
    25682596                                         Py_buffer *protos)
    25692597/*[clinic end generated code: output=72b002c3324390c6 input=319fcb66abf95bd7]*/
    25702598{
    2571 #ifdef OPENSSL_NPN_NEGOTIATED
     2599#if HAVE_NPN
    25722600    PyMem_Free(self->npn_protocols);
    25732601    self->npn_protocols = PyMem_Malloc(protos->len);
    25742602    if (self->npn_protocols == NULL)
     
    25932621#endif
    25942622}
    25952623
    2596 #ifdef HAVE_ALPN
     2624#if HAVE_ALPN
    25972625static int
    25982626_selectALPN_cb(SSL *s,
    25992627              const unsigned char **out, unsigned char *outlen,
     
    26182646                                          Py_buffer *protos)
    26192647/*[clinic end generated code: output=87599a7f76651a9b input=9bba964595d519be]*/
    26202648{
    2621 #ifdef HAVE_ALPN
     2649#if HAVE_ALPN
    26222650    PyMem_FREE(self->alpn_protocols);
    26232651    self->alpn_protocols = PyMem_Malloc(protos->len);
    26242652    if (!self->alpn_protocols)
     
    46304658        return NULL;
    46314659    PySocketModule = *socket_api;
    46324660
     4661#ifndef OPENSSL_VERSION_1_1
     4662    /* Load all algorithms and initialize cpuid */
     4663    OPENSSL_add_all_algorithms_noconf();
    46334664    /* Init OpenSSL */
    46344665    SSL_load_error_strings();
    46354666    SSL_library_init();
     4667#endif
     4668
    46364669#ifdef WITH_THREAD
    46374670#ifdef HAVE_OPENSSL_CRYPTO_LOCK
    46384671    /* note that this will start threading if not already started */
     
    46444677    _ssl_locks_count++;
    46454678#endif
    46464679#endif  /* WITH_THREAD */
    4647     OpenSSL_add_all_algorithms();
    46484680
    46494681    /* Add symbols to module dict */
    46504682    sslerror_type_slots[0].pfunc = PyExc_OSError;
     
    48434875    Py_INCREF(r);
    48444876    PyModule_AddObject(m, "HAS_ECDH", r);
    48454877
    4846 #ifdef OPENSSL_NPN_NEGOTIATED
     4878#if HAVE_NPN
    48474879    r = Py_True;
    48484880#else
    48494881    r = Py_False;
     
    48514883    Py_INCREF(r);
    48524884    PyModule_AddObject(m, "HAS_NPN", r);
    48534885
    4854 #ifdef HAVE_ALPN
     4886#if HAVE_ALPN
    48554887    r = Py_True;
    48564888#else
    48574889    r = Py_False;