@@ -123,22 +123,27 @@ struct py_ssl_library_code {
123
123
#endif
124
124
125
125
/* ALPN added in OpenSSL 1.0.2 */
126
- #if !defined(LIBRESSL_VERSION_NUMBER ) && OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined(OPENSSL_NO_TLSEXT )
127
- # define HAVE_ALPN
126
+ #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
127
+ # define HAVE_ALPN 1
128
+ #else
129
+ # define HAVE_ALPN 0
128
130
#endif
129
131
130
132
/* We cannot rely on OPENSSL_NO_NEXTPROTONEG because LibreSSL 2.6.1 dropped
131
133
* NPN support but did not set OPENSSL_NO_NEXTPROTONEG for compatibility
132
134
* reasons. The check for TLSEXT_TYPE_next_proto_neg works with
133
135
* OpenSSL 1.0.1+ and LibreSSL.
136
+ * OpenSSL 1.1.1-pre1 dropped NPN but still has TLSEXT_TYPE_next_proto_neg.
134
137
*/
135
138
#ifdef OPENSSL_NO_NEXTPROTONEG
136
- # define HAVE_NPN 0
139
+ # define HAVE_NPN 0
140
+ #elif (OPENSSL_VERSION_NUMBER >= 0x10101000L ) && !defined(LIBRESSL_VERSION_NUMBER )
141
+ # define HAVE_NPN 0
137
142
#elif defined(TLSEXT_TYPE_next_proto_neg )
138
- # define HAVE_NPN 1
143
+ # define HAVE_NPN 1
139
144
#else
140
- # define HAVE_NPN 0
141
- # endif
145
+ # define HAVE_NPN 0
146
+ #endif
142
147
143
148
#ifndef INVALID_SOCKET /* MS defines this */
144
149
#define INVALID_SOCKET (-1)
@@ -298,11 +303,11 @@ static unsigned int _ssl_locks_count = 0;
298
303
typedef struct {
299
304
PyObject_HEAD
300
305
SSL_CTX * ctx ;
301
- #ifdef HAVE_NPN
306
+ #if HAVE_NPN
302
307
unsigned char * npn_protocols ;
303
308
int npn_protocols_len ;
304
309
#endif
305
- #ifdef HAVE_ALPN
310
+ #if HAVE_ALPN
306
311
unsigned char * alpn_protocols ;
307
312
int alpn_protocols_len ;
308
313
#endif
@@ -1586,7 +1591,7 @@ static PyObject *PySSL_selected_npn_protocol(PySSLSocket *self) {
1586
1591
}
1587
1592
#endif
1588
1593
1589
- #ifdef HAVE_ALPN
1594
+ #if HAVE_ALPN
1590
1595
static PyObject * PySSL_selected_alpn_protocol (PySSLSocket * self ) {
1591
1596
const unsigned char * out ;
1592
1597
unsigned int outlen ;
@@ -2103,7 +2108,7 @@ static PyMethodDef PySSLMethods[] = {
2103
2108
#ifdef OPENSSL_NPN_NEGOTIATED
2104
2109
{"selected_npn_protocol" , (PyCFunction )PySSL_selected_npn_protocol , METH_NOARGS },
2105
2110
#endif
2106
- #ifdef HAVE_ALPN
2111
+ #if HAVE_ALPN
2107
2112
{"selected_alpn_protocol" , (PyCFunction )PySSL_selected_alpn_protocol , METH_NOARGS },
2108
2113
#endif
2109
2114
{"compression" , (PyCFunction )PySSL_compression , METH_NOARGS },
@@ -2209,10 +2214,10 @@ context_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2209
2214
return NULL ;
2210
2215
}
2211
2216
self -> ctx = ctx ;
2212
- #ifdef HAVE_NPN
2217
+ #if HAVE_NPN
2213
2218
self -> npn_protocols = NULL ;
2214
2219
#endif
2215
- #ifdef HAVE_ALPN
2220
+ #if HAVE_ALPN
2216
2221
self -> alpn_protocols = NULL ;
2217
2222
#endif
2218
2223
#ifndef OPENSSL_NO_TLSEXT
@@ -2287,10 +2292,10 @@ context_dealloc(PySSLContext *self)
2287
2292
PyObject_GC_UnTrack (self );
2288
2293
context_clear (self );
2289
2294
SSL_CTX_free (self -> ctx );
2290
- #ifdef HAVE_NPN
2295
+ #if HAVE_NPN
2291
2296
PyMem_FREE (self -> npn_protocols );
2292
2297
#endif
2293
- #ifdef HAVE_ALPN
2298
+ #if HAVE_ALPN
2294
2299
PyMem_FREE (self -> alpn_protocols );
2295
2300
#endif
2296
2301
Py_TYPE (self )-> tp_free (self );
@@ -2317,7 +2322,7 @@ set_ciphers(PySSLContext *self, PyObject *args)
2317
2322
Py_RETURN_NONE ;
2318
2323
}
2319
2324
2320
- #if defined( HAVE_NPN ) || defined( HAVE_ALPN )
2325
+ #if HAVE_NPN || HAVE_ALPN
2321
2326
static int
2322
2327
do_protocol_selection (int alpn , unsigned char * * out , unsigned char * outlen ,
2323
2328
const unsigned char * server_protocols , unsigned int server_protocols_len ,
@@ -2343,7 +2348,7 @@ do_protocol_selection(int alpn, unsigned char **out, unsigned char *outlen,
2343
2348
}
2344
2349
#endif
2345
2350
2346
- #ifdef HAVE_NPN
2351
+ #if HAVE_NPN
2347
2352
/* this callback gets passed to SSL_CTX_set_next_protos_advertise_cb */
2348
2353
static int
2349
2354
_advertiseNPN_cb (SSL * s ,
@@ -2378,7 +2383,7 @@ _selectNPN_cb(SSL *s,
2378
2383
static PyObject *
2379
2384
_set_npn_protocols (PySSLContext * self , PyObject * args )
2380
2385
{
2381
- #ifdef HAVE_NPN
2386
+ #if HAVE_NPN
2382
2387
Py_buffer protos ;
2383
2388
2384
2389
if (!PyArg_ParseTuple (args , "s*:set_npn_protocols" , & protos ))
@@ -2414,7 +2419,7 @@ _set_npn_protocols(PySSLContext *self, PyObject *args)
2414
2419
#endif
2415
2420
}
2416
2421
2417
- #ifdef HAVE_ALPN
2422
+ #if HAVE_ALPN
2418
2423
static int
2419
2424
_selectALPN_cb (SSL * s ,
2420
2425
const unsigned char * * out , unsigned char * outlen ,
@@ -2431,7 +2436,7 @@ _selectALPN_cb(SSL *s,
2431
2436
static PyObject *
2432
2437
_set_alpn_protocols (PySSLContext * self , PyObject * args )
2433
2438
{
2434
- #ifdef HAVE_ALPN
2439
+ #if HAVE_ALPN
2435
2440
Py_buffer protos ;
2436
2441
2437
2442
if (!PyArg_ParseTuple (args , "s*:set_npn_protocols" , & protos ))
@@ -4387,15 +4392,15 @@ init_ssl(void)
4387
4392
Py_INCREF (r );
4388
4393
PyModule_AddObject (m , "HAS_ECDH" , r );
4389
4394
4390
- #ifdef HAVE_NPN
4395
+ #if HAVE_NPN
4391
4396
r = Py_True ;
4392
4397
#else
4393
4398
r = Py_False ;
4394
4399
#endif
4395
4400
Py_INCREF (r );
4396
4401
PyModule_AddObject (m , "HAS_NPN" , r );
4397
4402
4398
- #ifdef HAVE_ALPN
4403
+ #if HAVE_ALPN
4399
4404
r = Py_True ;
4400
4405
#else
4401
4406
r = Py_False ;
0 commit comments