@@ -160,6 +160,19 @@ static void _PySSLFixErrno(void) {
160160# define HAVE_ALPN
161161#endif
162162
163+ /* We cannot rely on OPENSSL_NO_NEXTPROTONEG because LibreSSL 2.6.1 dropped
164+ * NPN support but did not set OPENSSL_NO_NEXTPROTONEG for compatibility
165+ * reasons. The check for TLSEXT_TYPE_next_proto_neg works with
166+ * OpenSSL 1.0.1+ and LibreSSL.
167+ */
168+ #ifdef OPENSSL_NO_NEXTPROTONEG
169+ # define HAVE_NPN 0
170+ #elif defined(TLSEXT_TYPE_next_proto_neg )
171+ # define HAVE_NPN 1
172+ #else
173+ # define HAVE_NPN 0
174+ # endif
175+
163176#ifndef INVALID_SOCKET /* MS defines this */
164177#define INVALID_SOCKET (-1)
165178#endif
@@ -328,7 +341,7 @@ static unsigned int _ssl_locks_count = 0;
328341typedef struct {
329342 PyObject_HEAD
330343 SSL_CTX * ctx ;
331- #if defined( OPENSSL_NPN_NEGOTIATED ) && !defined( OPENSSL_NO_NEXTPROTONEG )
344+ #ifdef HAVE_NPN
332345 unsigned char * npn_protocols ;
333346 int npn_protocols_len ;
334347#endif
@@ -1909,7 +1922,7 @@ _ssl__SSLSocket_version_impl(PySSLSocket *self)
19091922 return PyUnicode_FromString (version );
19101923}
19111924
1912- #if defined( OPENSSL_NPN_NEGOTIATED ) && !defined( OPENSSL_NO_NEXTPROTONEG )
1925+ #ifdef HAVE_NPN
19131926/*[clinic input]
19141927_ssl._SSLSocket.selected_npn_protocol
19151928[clinic start generated code]*/
@@ -2874,7 +2887,7 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
28742887 self -> ctx = ctx ;
28752888 self -> hostflags = X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS ;
28762889 self -> protocol = proto_version ;
2877- #if defined( OPENSSL_NPN_NEGOTIATED ) && !defined( OPENSSL_NO_NEXTPROTONEG )
2890+ #ifdef HAVE_NPN
28782891 self -> npn_protocols = NULL ;
28792892#endif
28802893#ifdef HAVE_ALPN
@@ -3013,7 +3026,7 @@ context_dealloc(PySSLContext *self)
30133026 PyObject_GC_UnTrack (self );
30143027 context_clear (self );
30153028 SSL_CTX_free (self -> ctx );
3016- #if defined( OPENSSL_NPN_NEGOTIATED ) && !defined( OPENSSL_NO_NEXTPROTONEG )
3029+ #ifdef HAVE_NPN
30173030 PyMem_FREE (self -> npn_protocols );
30183031#endif
30193032#ifdef HAVE_ALPN
@@ -3091,7 +3104,7 @@ _ssl__SSLContext_get_ciphers_impl(PySSLContext *self)
30913104#endif
30923105
30933106
3094- #if defined(OPENSSL_NPN_NEGOTIATED ) && !defined( OPENSSL_NO_NEXTPROTONEG ) || defined(HAVE_ALPN )
3107+ #if defined(HAVE_NPN ) || defined(HAVE_ALPN )
30953108static int
30963109do_protocol_selection (int alpn , unsigned char * * out , unsigned char * outlen ,
30973110 const unsigned char * server_protocols , unsigned int server_protocols_len ,
@@ -3117,7 +3130,7 @@ do_protocol_selection(int alpn, unsigned char **out, unsigned char *outlen,
31173130}
31183131#endif
31193132
3120- #if defined( OPENSSL_NPN_NEGOTIATED ) && !defined( OPENSSL_NO_NEXTPROTONEG )
3133+ #ifdef HAVE_NPN
31213134/* this callback gets passed to SSL_CTX_set_next_protos_advertise_cb */
31223135static int
31233136_advertiseNPN_cb (SSL * s ,
@@ -3160,7 +3173,7 @@ _ssl__SSLContext__set_npn_protocols_impl(PySSLContext *self,
31603173 Py_buffer * protos )
31613174/*[clinic end generated code: output=72b002c3324390c6 input=319fcb66abf95bd7]*/
31623175{
3163- #if defined( OPENSSL_NPN_NEGOTIATED ) && !defined( OPENSSL_NO_NEXTPROTONEG )
3176+ #ifdef HAVE_NPN
31643177 PyMem_Free (self -> npn_protocols );
31653178 self -> npn_protocols = PyMem_Malloc (protos -> len );
31663179 if (self -> npn_protocols == NULL )
@@ -5705,7 +5718,7 @@ PyInit__ssl(void)
57055718 Py_INCREF (r );
57065719 PyModule_AddObject (m , "HAS_ECDH" , r );
57075720
5708- #if defined( OPENSSL_NPN_NEGOTIATED ) && !defined( OPENSSL_NO_NEXTPROTONEG )
5721+ #ifdef HAVE_NPN
57095722 r = Py_True ;
57105723#else
57115724 r = Py_False ;
0 commit comments