Skip to content

Commit a79591c

Browse files
authored
[3.6] bpo-30622: Fix NPN for OpenSSL 1.1.1-pre1 (GH-5876) (#5881)
Signed-off-by: Christian Heimes <[email protected]>. (cherry picked from commit 29eab55) Co-authored-by: Christian Heimes <[email protected]>
1 parent 192bff4 commit a79591c

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

Modules/_ssl.c

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,26 @@ struct py_ssl_library_code {
127127
#endif
128128

129129
#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
130-
# define HAVE_ALPN
130+
# define HAVE_ALPN 1
131+
#else
132+
# define HAVE_ALPN 0
131133
#endif
132134

133135
/* We cannot rely on OPENSSL_NO_NEXTPROTONEG because LibreSSL 2.6.1 dropped
134136
* NPN support but did not set OPENSSL_NO_NEXTPROTONEG for compatibility
135137
* reasons. The check for TLSEXT_TYPE_next_proto_neg works with
136138
* OpenSSL 1.0.1+ and LibreSSL.
139+
* OpenSSL 1.1.1-pre1 dropped NPN but still has TLSEXT_TYPE_next_proto_neg.
137140
*/
138141
#ifdef OPENSSL_NO_NEXTPROTONEG
139-
# define HAVE_NPN 0
142+
# define HAVE_NPN 0
143+
#elif (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER)
144+
# define HAVE_NPN 0
140145
#elif defined(TLSEXT_TYPE_next_proto_neg)
141-
# define HAVE_NPN 1
146+
# define HAVE_NPN 1
142147
#else
143-
# define HAVE_NPN 0
144-
# endif
148+
# define HAVE_NPN 0
149+
#endif
145150

146151
#ifndef INVALID_SOCKET /* MS defines this */
147152
#define INVALID_SOCKET (-1)
@@ -297,11 +302,11 @@ static unsigned int _ssl_locks_count = 0;
297302
typedef struct {
298303
PyObject_HEAD
299304
SSL_CTX *ctx;
300-
#ifdef HAVE_NPN
305+
#if HAVE_NPN
301306
unsigned char *npn_protocols;
302307
int npn_protocols_len;
303308
#endif
304-
#ifdef HAVE_ALPN
309+
#if HAVE_ALPN
305310
unsigned char *alpn_protocols;
306311
int alpn_protocols_len;
307312
#endif
@@ -1789,7 +1794,7 @@ _ssl__SSLSocket_version_impl(PySSLSocket *self)
17891794
return PyUnicode_FromString(version);
17901795
}
17911796

1792-
#ifdef HAVE_NPN
1797+
#if HAVE_NPN
17931798
/*[clinic input]
17941799
_ssl._SSLSocket.selected_npn_protocol
17951800
[clinic start generated code]*/
@@ -1810,7 +1815,7 @@ _ssl__SSLSocket_selected_npn_protocol_impl(PySSLSocket *self)
18101815
}
18111816
#endif
18121817

1813-
#ifdef HAVE_ALPN
1818+
#if HAVE_ALPN
18141819
/*[clinic input]
18151820
_ssl._SSLSocket.selected_alpn_protocol
18161821
[clinic start generated code]*/
@@ -2745,7 +2750,7 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
27452750
#ifdef HAVE_NPN
27462751
self->npn_protocols = NULL;
27472752
#endif
2748-
#ifdef HAVE_ALPN
2753+
#if HAVE_ALPN
27492754
self->alpn_protocols = NULL;
27502755
#endif
27512756
#ifndef OPENSSL_NO_TLSEXT
@@ -2877,10 +2882,10 @@ context_dealloc(PySSLContext *self)
28772882
PyObject_GC_UnTrack(self);
28782883
context_clear(self);
28792884
SSL_CTX_free(self->ctx);
2880-
#ifdef HAVE_NPN
2885+
#if HAVE_NPN
28812886
PyMem_FREE(self->npn_protocols);
28822887
#endif
2883-
#ifdef HAVE_ALPN
2888+
#if HAVE_ALPN
28842889
PyMem_FREE(self->alpn_protocols);
28852890
#endif
28862891
Py_TYPE(self)->tp_free(self);
@@ -2955,7 +2960,7 @@ _ssl__SSLContext_get_ciphers_impl(PySSLContext *self)
29552960
#endif
29562961

29572962

2958-
#if defined(HAVE_NPN) || defined(HAVE_ALPN)
2963+
#if HAVE_NPN || HAVE_ALPN
29592964
static int
29602965
do_protocol_selection(int alpn, unsigned char **out, unsigned char *outlen,
29612966
const unsigned char *server_protocols, unsigned int server_protocols_len,
@@ -2981,7 +2986,7 @@ do_protocol_selection(int alpn, unsigned char **out, unsigned char *outlen,
29812986
}
29822987
#endif
29832988

2984-
#ifdef HAVE_NPN
2989+
#if HAVE_NPN
29852990
/* this callback gets passed to SSL_CTX_set_next_protos_advertise_cb */
29862991
static int
29872992
_advertiseNPN_cb(SSL *s,
@@ -3024,7 +3029,7 @@ _ssl__SSLContext__set_npn_protocols_impl(PySSLContext *self,
30243029
Py_buffer *protos)
30253030
/*[clinic end generated code: output=72b002c3324390c6 input=319fcb66abf95bd7]*/
30263031
{
3027-
#ifdef HAVE_NPN
3032+
#if HAVE_NPN
30283033
PyMem_Free(self->npn_protocols);
30293034
self->npn_protocols = PyMem_Malloc(protos->len);
30303035
if (self->npn_protocols == NULL)
@@ -3049,7 +3054,7 @@ _ssl__SSLContext__set_npn_protocols_impl(PySSLContext *self,
30493054
#endif
30503055
}
30513056

3052-
#ifdef HAVE_ALPN
3057+
#if HAVE_ALPN
30533058
static int
30543059
_selectALPN_cb(SSL *s,
30553060
const unsigned char **out, unsigned char *outlen,
@@ -3074,7 +3079,7 @@ _ssl__SSLContext__set_alpn_protocols_impl(PySSLContext *self,
30743079
Py_buffer *protos)
30753080
/*[clinic end generated code: output=87599a7f76651a9b input=9bba964595d519be]*/
30763081
{
3077-
#ifdef HAVE_ALPN
3082+
#if HAVE_ALPN
30783083
if ((size_t)protos->len > UINT_MAX) {
30793084
PyErr_Format(PyExc_OverflowError,
30803085
"protocols longer than %d bytes", UINT_MAX);
@@ -5494,15 +5499,15 @@ PyInit__ssl(void)
54945499
Py_INCREF(r);
54955500
PyModule_AddObject(m, "HAS_ECDH", r);
54965501

5497-
#ifdef HAVE_NPN
5502+
#if HAVE_NPN
54985503
r = Py_True;
54995504
#else
55005505
r = Py_False;
55015506
#endif
55025507
Py_INCREF(r);
55035508
PyModule_AddObject(m, "HAS_NPN", r);
55045509

5505-
#ifdef HAVE_ALPN
5510+
#if HAVE_ALPN
55065511
r = Py_True;
55075512
#else
55085513
r = Py_False;

Modules/clinic/_ssl.c.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ _ssl__SSLSocket_version(PySSLSocket *self, PyObject *Py_UNUSED(ignored))
132132
return _ssl__SSLSocket_version_impl(self);
133133
}
134134

135-
#if defined(HAVE_NPN)
135+
#if (HAVE_NPN)
136136

137137
PyDoc_STRVAR(_ssl__SSLSocket_selected_npn_protocol__doc__,
138138
"selected_npn_protocol($self, /)\n"
@@ -151,9 +151,9 @@ _ssl__SSLSocket_selected_npn_protocol(PySSLSocket *self, PyObject *Py_UNUSED(ign
151151
return _ssl__SSLSocket_selected_npn_protocol_impl(self);
152152
}
153153

154-
#endif /* defined(HAVE_NPN) */
154+
#endif /* (HAVE_NPN) */
155155

156-
#if defined(HAVE_ALPN)
156+
#if (HAVE_ALPN)
157157

158158
PyDoc_STRVAR(_ssl__SSLSocket_selected_alpn_protocol__doc__,
159159
"selected_alpn_protocol($self, /)\n"
@@ -172,7 +172,7 @@ _ssl__SSLSocket_selected_alpn_protocol(PySSLSocket *self, PyObject *Py_UNUSED(ig
172172
return _ssl__SSLSocket_selected_alpn_protocol_impl(self);
173173
}
174174

175-
#endif /* defined(HAVE_ALPN) */
175+
#endif /* (HAVE_ALPN) */
176176

177177
PyDoc_STRVAR(_ssl__SSLSocket_compression__doc__,
178178
"compression($self, /)\n"
@@ -1168,4 +1168,4 @@ _ssl_enum_crls(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kw
11681168
#ifndef _SSL_ENUM_CRLS_METHODDEF
11691169
#define _SSL_ENUM_CRLS_METHODDEF
11701170
#endif /* !defined(_SSL_ENUM_CRLS_METHODDEF) */
1171-
/*[clinic end generated code: output=3d801e1145e7a94e input=a9049054013a1b77]*/
1171+
/*[clinic end generated code: output=c79fb0dfd3c90784 input=a9049054013a1b77]*/

0 commit comments

Comments
 (0)