Skip to content

Commit a871f69

Browse files
tiranmarkwright
andauthored
bpo-30008: Fix OpenSSL no-deprecated compilation (GH-20397)
Fix :mod:`ssl`` code to be compatible with OpenSSL 1.1.x builds that use ``no-deprecated`` and ``--api=1.1.0``. Note: Tests assume full OpenSSL API and fail with limited API. Signed-off-by: Christian Heimes <[email protected]> Co-authored-by: Mark Wright <[email protected]>
1 parent 2f172d8 commit a871f69

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :mod:`ssl` code to be compatible with OpenSSL 1.1.x builds that use
2+
``no-deprecated`` and ``--api=1.1.0``.

Modules/_ssl.c

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,24 @@ static void _PySSLFixErrno(void) {
142142
# define PY_OPENSSL_1_1_API 1
143143
#endif
144144

145+
/* OpenSSL API compat */
146+
#ifdef OPENSSL_API_COMPAT
147+
#if OPENSSL_API_COMPAT >= 0x10100000L
148+
149+
/* OpenSSL API 1.1.0+ does not include version methods */
150+
#ifndef OPENSSL_NO_TLS1_METHOD
151+
#define OPENSSL_NO_TLS1_METHOD 1
152+
#endif
153+
#ifndef OPENSSL_NO_TLS1_1_METHOD
154+
#define OPENSSL_NO_TLS1_1_METHOD 1
155+
#endif
156+
#ifndef OPENSSL_NO_TLS1_2_METHOD
157+
#define OPENSSL_NO_TLS1_2_METHOD 1
158+
#endif
159+
160+
#endif /* >= 1.1.0 compcat */
161+
#endif /* OPENSSL_API_COMPAT */
162+
145163
/* LibreSSL 2.7.0 provides necessary OpenSSL 1.1.0 APIs */
146164
#if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x2070000fL
147165
# define PY_OPENSSL_1_1_API 1
@@ -201,6 +219,12 @@ static void _PySSLFixErrno(void) {
201219
#define TLS_method SSLv23_method
202220
#define TLS_client_method SSLv23_client_method
203221
#define TLS_server_method SSLv23_server_method
222+
#define ASN1_STRING_get0_data ASN1_STRING_data
223+
#define X509_get0_notBefore X509_get_notBefore
224+
#define X509_get0_notAfter X509_get_notAfter
225+
#define OpenSSL_version_num SSLeay
226+
#define OpenSSL_version SSLeay_version
227+
#define OPENSSL_VERSION SSLEAY_VERSION
204228

205229
static int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne)
206230
{
@@ -885,7 +909,7 @@ _ssl_configure_hostname(PySSLSocket *self, const char* server_hostname)
885909
goto error;
886910
}
887911
} else {
888-
if (!X509_VERIFY_PARAM_set1_ip(param, ASN1_STRING_data(ip),
912+
if (!X509_VERIFY_PARAM_set1_ip(param, ASN1_STRING_get0_data(ip),
889913
ASN1_STRING_length(ip))) {
890914
_setSSLError(NULL, 0, __FILE__, __LINE__);
891915
goto error;
@@ -1361,7 +1385,7 @@ _get_peer_alt_names (X509 *certificate) {
13611385
goto fail;
13621386
}
13631387
PyTuple_SET_ITEM(t, 0, v);
1364-
v = PyUnicode_FromStringAndSize((char *)ASN1_STRING_data(as),
1388+
v = PyUnicode_FromStringAndSize((char *)ASN1_STRING_get0_data(as),
13651389
ASN1_STRING_length(as));
13661390
if (v == NULL) {
13671391
Py_DECREF(t);
@@ -1657,7 +1681,7 @@ _decode_certificate(X509 *certificate) {
16571681
ASN1_INTEGER *serialNumber;
16581682
char buf[2048];
16591683
int len, result;
1660-
ASN1_TIME *notBefore, *notAfter;
1684+
const ASN1_TIME *notBefore, *notAfter;
16611685
PyObject *pnotBefore, *pnotAfter;
16621686

16631687
retval = PyDict_New();
@@ -1719,7 +1743,7 @@ _decode_certificate(X509 *certificate) {
17191743
Py_DECREF(sn_obj);
17201744

17211745
(void) BIO_reset(biobuf);
1722-
notBefore = X509_get_notBefore(certificate);
1746+
notBefore = X509_get0_notBefore(certificate);
17231747
ASN1_TIME_print(biobuf, notBefore);
17241748
len = BIO_gets(biobuf, buf, sizeof(buf)-1);
17251749
if (len < 0) {
@@ -1736,7 +1760,7 @@ _decode_certificate(X509 *certificate) {
17361760
Py_DECREF(pnotBefore);
17371761

17381762
(void) BIO_reset(biobuf);
1739-
notAfter = X509_get_notAfter(certificate);
1763+
notAfter = X509_get0_notAfter(certificate);
17401764
ASN1_TIME_print(biobuf, notAfter);
17411765
len = BIO_gets(biobuf, buf, sizeof(buf)-1);
17421766
if (len < 0) {
@@ -3079,17 +3103,23 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
30793103
ctx = SSL_CTX_new(SSLv3_method());
30803104
break;
30813105
#endif
3082-
#if defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1)
3106+
#if (defined(TLS1_VERSION) && \
3107+
!defined(OPENSSL_NO_TLS1) && \
3108+
!defined(OPENSSL_NO_TLS1_METHOD))
30833109
case PY_SSL_VERSION_TLS1:
30843110
ctx = SSL_CTX_new(TLSv1_method());
30853111
break;
30863112
#endif
3087-
#if defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1)
3113+
#if (defined(TLS1_1_VERSION) && \
3114+
!defined(OPENSSL_NO_TLS1_1) && \
3115+
!defined(OPENSSL_NO_TLS1_1_METHOD))
30883116
case PY_SSL_VERSION_TLS1_1:
30893117
ctx = SSL_CTX_new(TLSv1_1_method());
30903118
break;
30913119
#endif
3092-
#if defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2)
3120+
#if (defined(TLS1_2_VERSION) && \
3121+
!defined(OPENSSL_NO_TLS1_2) && \
3122+
!defined(OPENSSL_NO_TLS1_2_METHOD))
30933123
case PY_SSL_VERSION_TLS1_2:
30943124
ctx = SSL_CTX_new(TLSv1_2_method());
30953125
break;
@@ -3207,7 +3237,7 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
32073237
conservative and assume it wasn't fixed until release. We do this check
32083238
at runtime to avoid problems from the dynamic linker.
32093239
See #25672 for more on this. */
3210-
libver = SSLeay();
3240+
libver = OpenSSL_version_num();
32113241
if (!(libver >= 0x10001000UL && libver < 0x1000108fUL) &&
32123242
!(libver >= 0x10000000UL && libver < 0x100000dfUL)) {
32133243
SSL_CTX_set_mode(self->ctx, SSL_MODE_RELEASE_BUFFERS);
@@ -5286,7 +5316,11 @@ PySSL_RAND(int len, int pseudo)
52865316
if (bytes == NULL)
52875317
return NULL;
52885318
if (pseudo) {
5319+
#ifdef PY_OPENSSL_1_1_API
5320+
ok = RAND_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len);
5321+
#else
52895322
ok = RAND_pseudo_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len);
5323+
#endif
52905324
if (ok == 0 || ok == 1)
52915325
return Py_BuildValue("NO", bytes, ok == 1 ? Py_True : Py_False);
52925326
}
@@ -6373,7 +6407,7 @@ PyInit__ssl(void)
63736407
/* SSLeay() gives us the version of the library linked against,
63746408
which could be different from the headers version.
63756409
*/
6376-
libver = SSLeay();
6410+
libver = OpenSSL_version_num();
63776411
r = PyLong_FromUnsignedLong(libver);
63786412
if (r == NULL)
63796413
return NULL;
@@ -6383,7 +6417,7 @@ PyInit__ssl(void)
63836417
r = Py_BuildValue("IIIII", major, minor, fix, patch, status);
63846418
if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r))
63856419
return NULL;
6386-
r = PyUnicode_FromString(SSLeay_version(SSLEAY_VERSION));
6420+
r = PyUnicode_FromString(OpenSSL_version(OPENSSL_VERSION));
63876421
if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r))
63886422
return NULL;
63896423

Tools/ssl/multissltests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ def _build_src(self):
314314
"shared", "--debug",
315315
"--prefix={}".format(self.install_dir)
316316
]
317+
# cmd.extend(["no-deprecated", "--api=1.1.0"])
317318
env = os.environ.copy()
318319
# set rpath
319320
env["LD_RUN_PATH"] = self.lib_dir

0 commit comments

Comments
 (0)