@@ -130,6 +130,19 @@ struct py_ssl_library_code {
130
130
# define HAVE_ALPN
131
131
#endif
132
132
133
+ /* We cannot rely on OPENSSL_NO_NEXTPROTONEG because LibreSSL 2.6.1 dropped
134
+ * NPN support but did not set OPENSSL_NO_NEXTPROTONEG for compatibility
135
+ * reasons. The check for TLSEXT_TYPE_next_proto_neg works with
136
+ * OpenSSL 1.0.1+ and LibreSSL.
137
+ */
138
+ #ifdef OPENSSL_NO_NEXTPROTONEG
139
+ # define HAVE_NPN 0
140
+ #elif defined(TLSEXT_TYPE_next_proto_neg )
141
+ # define HAVE_NPN 1
142
+ #else
143
+ # define HAVE_NPN 0
144
+ # endif
145
+
133
146
#ifndef INVALID_SOCKET /* MS defines this */
134
147
#define INVALID_SOCKET (-1)
135
148
#endif
@@ -284,7 +297,7 @@ static unsigned int _ssl_locks_count = 0;
284
297
typedef struct {
285
298
PyObject_HEAD
286
299
SSL_CTX * ctx ;
287
- #if defined( OPENSSL_NPN_NEGOTIATED ) && !defined( OPENSSL_NO_NEXTPROTONEG )
300
+ #ifdef HAVE_NPN
288
301
unsigned char * npn_protocols ;
289
302
int npn_protocols_len ;
290
303
#endif
@@ -1776,7 +1789,7 @@ _ssl__SSLSocket_version_impl(PySSLSocket *self)
1776
1789
return PyUnicode_FromString (version );
1777
1790
}
1778
1791
1779
- #if defined( OPENSSL_NPN_NEGOTIATED ) && !defined( OPENSSL_NO_NEXTPROTONEG )
1792
+ #ifdef HAVE_NPN
1780
1793
/*[clinic input]
1781
1794
_ssl._SSLSocket.selected_npn_protocol
1782
1795
[clinic start generated code]*/
@@ -2729,7 +2742,7 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
2729
2742
return NULL ;
2730
2743
}
2731
2744
self -> ctx = ctx ;
2732
- #if defined( OPENSSL_NPN_NEGOTIATED ) && !defined( OPENSSL_NO_NEXTPROTONEG )
2745
+ #ifdef HAVE_NPN
2733
2746
self -> npn_protocols = NULL ;
2734
2747
#endif
2735
2748
#ifdef HAVE_ALPN
@@ -2864,7 +2877,7 @@ context_dealloc(PySSLContext *self)
2864
2877
PyObject_GC_UnTrack (self );
2865
2878
context_clear (self );
2866
2879
SSL_CTX_free (self -> ctx );
2867
- #if defined( OPENSSL_NPN_NEGOTIATED ) && !defined( OPENSSL_NO_NEXTPROTONEG )
2880
+ #ifdef HAVE_NPN
2868
2881
PyMem_FREE (self -> npn_protocols );
2869
2882
#endif
2870
2883
#ifdef HAVE_ALPN
@@ -2942,7 +2955,7 @@ _ssl__SSLContext_get_ciphers_impl(PySSLContext *self)
2942
2955
#endif
2943
2956
2944
2957
2945
- #if defined(OPENSSL_NPN_NEGOTIATED ) && !defined( OPENSSL_NO_NEXTPROTONEG ) || defined(HAVE_ALPN )
2958
+ #if defined(HAVE_NPN ) || defined(HAVE_ALPN )
2946
2959
static int
2947
2960
do_protocol_selection (int alpn , unsigned char * * out , unsigned char * outlen ,
2948
2961
const unsigned char * server_protocols , unsigned int server_protocols_len ,
@@ -2968,7 +2981,7 @@ do_protocol_selection(int alpn, unsigned char **out, unsigned char *outlen,
2968
2981
}
2969
2982
#endif
2970
2983
2971
- #if defined( OPENSSL_NPN_NEGOTIATED ) && !defined( OPENSSL_NO_NEXTPROTONEG )
2984
+ #ifdef HAVE_NPN
2972
2985
/* this callback gets passed to SSL_CTX_set_next_protos_advertise_cb */
2973
2986
static int
2974
2987
_advertiseNPN_cb (SSL * s ,
@@ -3011,7 +3024,7 @@ _ssl__SSLContext__set_npn_protocols_impl(PySSLContext *self,
3011
3024
Py_buffer * protos )
3012
3025
/*[clinic end generated code: output=72b002c3324390c6 input=319fcb66abf95bd7]*/
3013
3026
{
3014
- #if defined( OPENSSL_NPN_NEGOTIATED ) && !defined( OPENSSL_NO_NEXTPROTONEG )
3027
+ #ifdef HAVE_NPN
3015
3028
PyMem_Free (self -> npn_protocols );
3016
3029
self -> npn_protocols = PyMem_Malloc (protos -> len );
3017
3030
if (self -> npn_protocols == NULL )
@@ -5481,7 +5494,7 @@ PyInit__ssl(void)
5481
5494
Py_INCREF (r );
5482
5495
PyModule_AddObject (m , "HAS_ECDH" , r );
5483
5496
5484
- #if defined( OPENSSL_NPN_NEGOTIATED ) && !defined( OPENSSL_NO_NEXTPROTONEG )
5497
+ #ifdef HAVE_NPN
5485
5498
r = Py_True ;
5486
5499
#else
5487
5500
r = Py_False ;
0 commit comments