Skip to content

Commit 296db8c

Browse files
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]> (cherry picked from commit a871f69) Co-authored-by: Christian Heimes <[email protected]>
1 parent a9dbae4 commit 296db8c

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
@@ -144,6 +144,24 @@ static void _PySSLFixErrno(void) {
144144
# define PY_OPENSSL_1_1_API 1
145145
#endif
146146

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

203227
static int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne)
204228
{
@@ -857,7 +881,7 @@ _ssl_configure_hostname(PySSLSocket *self, const char* server_hostname)
857881
goto error;
858882
}
859883
} else {
860-
if (!X509_VERIFY_PARAM_set1_ip(param, ASN1_STRING_data(ip),
884+
if (!X509_VERIFY_PARAM_set1_ip(param, ASN1_STRING_get0_data(ip),
861885
ASN1_STRING_length(ip))) {
862886
_setSSLError(NULL, 0, __FILE__, __LINE__);
863887
goto error;
@@ -1330,7 +1354,7 @@ _get_peer_alt_names (X509 *certificate) {
13301354
goto fail;
13311355
}
13321356
PyTuple_SET_ITEM(t, 0, v);
1333-
v = PyUnicode_FromStringAndSize((char *)ASN1_STRING_data(as),
1357+
v = PyUnicode_FromStringAndSize((char *)ASN1_STRING_get0_data(as),
13341358
ASN1_STRING_length(as));
13351359
if (v == NULL) {
13361360
Py_DECREF(t);
@@ -1626,7 +1650,7 @@ _decode_certificate(X509 *certificate) {
16261650
ASN1_INTEGER *serialNumber;
16271651
char buf[2048];
16281652
int len, result;
1629-
ASN1_TIME *notBefore, *notAfter;
1653+
const ASN1_TIME *notBefore, *notAfter;
16301654
PyObject *pnotBefore, *pnotAfter;
16311655

16321656
retval = PyDict_New();
@@ -1688,7 +1712,7 @@ _decode_certificate(X509 *certificate) {
16881712
Py_DECREF(sn_obj);
16891713

16901714
(void) BIO_reset(biobuf);
1691-
notBefore = X509_get_notBefore(certificate);
1715+
notBefore = X509_get0_notBefore(certificate);
16921716
ASN1_TIME_print(biobuf, notBefore);
16931717
len = BIO_gets(biobuf, buf, sizeof(buf)-1);
16941718
if (len < 0) {
@@ -1705,7 +1729,7 @@ _decode_certificate(X509 *certificate) {
17051729
Py_DECREF(pnotBefore);
17061730

17071731
(void) BIO_reset(biobuf);
1708-
notAfter = X509_get_notAfter(certificate);
1732+
notAfter = X509_get0_notAfter(certificate);
17091733
ASN1_TIME_print(biobuf, notAfter);
17101734
len = BIO_gets(biobuf, buf, sizeof(buf)-1);
17111735
if (len < 0) {
@@ -3023,17 +3047,23 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
30233047
ctx = SSL_CTX_new(SSLv3_method());
30243048
break;
30253049
#endif
3026-
#if defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1)
3050+
#if (defined(TLS1_VERSION) && \
3051+
!defined(OPENSSL_NO_TLS1) && \
3052+
!defined(OPENSSL_NO_TLS1_METHOD))
30273053
case PY_SSL_VERSION_TLS1:
30283054
ctx = SSL_CTX_new(TLSv1_method());
30293055
break;
30303056
#endif
3031-
#if defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1)
3057+
#if (defined(TLS1_1_VERSION) && \
3058+
!defined(OPENSSL_NO_TLS1_1) && \
3059+
!defined(OPENSSL_NO_TLS1_1_METHOD))
30323060
case PY_SSL_VERSION_TLS1_1:
30333061
ctx = SSL_CTX_new(TLSv1_1_method());
30343062
break;
30353063
#endif
3036-
#if defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2)
3064+
#if (defined(TLS1_2_VERSION) && \
3065+
!defined(OPENSSL_NO_TLS1_2) && \
3066+
!defined(OPENSSL_NO_TLS1_2_METHOD))
30373067
case PY_SSL_VERSION_TLS1_2:
30383068
ctx = SSL_CTX_new(TLSv1_2_method());
30393069
break;
@@ -3146,7 +3176,7 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
31463176
conservative and assume it wasn't fixed until release. We do this check
31473177
at runtime to avoid problems from the dynamic linker.
31483178
See #25672 for more on this. */
3149-
libver = SSLeay();
3179+
libver = OpenSSL_version_num();
31503180
if (!(libver >= 0x10001000UL && libver < 0x1000108fUL) &&
31513181
!(libver >= 0x10000000UL && libver < 0x100000dfUL)) {
31523182
SSL_CTX_set_mode(self->ctx, SSL_MODE_RELEASE_BUFFERS);
@@ -5156,7 +5186,11 @@ PySSL_RAND(int len, int pseudo)
51565186
if (bytes == NULL)
51575187
return NULL;
51585188
if (pseudo) {
5189+
#ifdef PY_OPENSSL_1_1_API
5190+
ok = RAND_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len);
5191+
#else
51595192
ok = RAND_pseudo_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len);
5193+
#endif
51605194
if (ok == 0 || ok == 1)
51615195
return Py_BuildValue("NO", bytes, ok == 1 ? Py_True : Py_False);
51625196
}
@@ -6240,7 +6274,7 @@ PyInit__ssl(void)
62406274
/* SSLeay() gives us the version of the library linked against,
62416275
which could be different from the headers version.
62426276
*/
6243-
libver = SSLeay();
6277+
libver = OpenSSL_version_num();
62446278
r = PyLong_FromUnsignedLong(libver);
62456279
if (r == NULL)
62466280
return NULL;
@@ -6250,7 +6284,7 @@ PyInit__ssl(void)
62506284
r = Py_BuildValue("IIIII", major, minor, fix, patch, status);
62516285
if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r))
62526286
return NULL;
6253-
r = PyUnicode_FromString(SSLeay_version(SSLEAY_VERSION));
6287+
r = PyUnicode_FromString(OpenSSL_version(OPENSSL_VERSION));
62546288
if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r))
62556289
return NULL;
62566290

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)