Skip to content

Commit 5aed182

Browse files
committed
bpo-30008: OpenSSL 1.1 compatibility without using deprecated API
Note: RAND_pseudo_bytes() is deprecated so RAND_bytes() is used when pseudo is requested.
1 parent a997c7b commit 5aed182

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

Modules/_hashopenssl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ PyInit__hashlib(void)
10121012
{
10131013
PyObject *m, *openssl_md_meth_names;
10141014

1015-
#ifndef OPENSSL_VERSION_1_1
1015+
#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
10161016
/* Load all digest algorithms and initialize cpuid */
10171017
OPENSSL_add_all_algorithms_noconf();
10181018
ERR_load_crypto_strings();

Modules/_ssl.c

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ static PySocketModule_APIObject PySocketModule;
6363
#include "openssl/err.h"
6464
#include "openssl/rand.h"
6565
#include "openssl/bio.h"
66+
#include "openssl/dh.h"
6667

6768
/* SSL error object */
6869
static PyObject *PySSLErrorObject;
@@ -162,9 +163,17 @@ static void _PySSLFixErrno(void) {
162163
#else /* OpenSSL < 1.1.0 */
163164
#define HAVE_OPENSSL_CRYPTO_LOCK
164165

166+
#ifndef OPENSSL_VERSION_1_1
165167
#define TLS_method SSLv23_method
166168
#define TLS_client_method SSLv23_client_method
167169
#define TLS_server_method SSLv23_server_method
170+
#define X509_get0_notBefore X509_get_notBefore
171+
#define X509_get0_notAfter X509_get_notAfter
172+
#define ASN1_STRING_get0_data ASN1_STRING_data
173+
#define OpenSSL_version_num SSLeay
174+
#define OpenSSL_version SSLeay_version
175+
#define OPENSSL_VERSION SSLEAY_VERSION
176+
#endif
168177

169178
static int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne)
170179
{
@@ -1123,7 +1132,7 @@ _get_peer_alt_names (X509 *certificate) {
11231132
goto fail;
11241133
}
11251134
PyTuple_SET_ITEM(t, 0, v);
1126-
v = PyUnicode_FromStringAndSize((char *)ASN1_STRING_data(as),
1135+
v = PyUnicode_FromStringAndSize((char *)ASN1_STRING_get0_data(as),
11271136
ASN1_STRING_length(as));
11281137
if (v == NULL) {
11291138
Py_DECREF(t);
@@ -1426,7 +1435,7 @@ _decode_certificate(X509 *certificate) {
14261435
Py_DECREF(sn_obj);
14271436

14281437
(void) BIO_reset(biobuf);
1429-
notBefore = X509_get_notBefore(certificate);
1438+
notBefore = X509_get0_notBefore(certificate);
14301439
ASN1_TIME_print(biobuf, notBefore);
14311440
len = BIO_gets(biobuf, buf, sizeof(buf)-1);
14321441
if (len < 0) {
@@ -1443,7 +1452,7 @@ _decode_certificate(X509 *certificate) {
14431452
Py_DECREF(pnotBefore);
14441453

14451454
(void) BIO_reset(biobuf);
1446-
notAfter = X509_get_notAfter(certificate);
1455+
notAfter = X509_get0_notAfter(certificate);
14471456
ASN1_TIME_print(biobuf, notAfter);
14481457
len = BIO_gets(biobuf, buf, sizeof(buf)-1);
14491458
if (len < 0) {
@@ -2822,7 +2831,7 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
28222831
conservative and assume it wasn't fixed until release. We do this check
28232832
at runtime to avoid problems from the dynamic linker.
28242833
See #25672 for more on this. */
2825-
libver = SSLeay();
2834+
libver = OpenSSL_version_num();
28262835
if (!(libver >= 0x10001000UL && libver < 0x1000108fUL) &&
28272836
!(libver >= 0x10000000UL && libver < 0x100000dfUL)) {
28282837
SSL_CTX_set_mode(self->ctx, SSL_MODE_RELEASE_BUFFERS);
@@ -4625,7 +4634,11 @@ PySSL_RAND(int len, int pseudo)
46254634
if (bytes == NULL)
46264635
return NULL;
46274636
if (pseudo) {
4637+
#ifdef OPENSSL_VERSION_1_1
4638+
ok = RAND_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len);
4639+
#else
46284640
ok = RAND_pseudo_bytes((unsigned char*)PyBytes_AS_STRING(bytes), len);
4641+
#endif
46294642
if (ok == 0 || ok == 1)
46304643
return Py_BuildValue("NO", bytes, ok == 1 ? Py_True : Py_False);
46314644
}
@@ -5264,7 +5277,9 @@ PyInit__ssl(void)
52645277
return NULL;
52655278
PySocketModule = *socket_api;
52665279

5267-
#ifndef OPENSSL_VERSION_1_1
5280+
#ifdef OPENSSL_VERSION_1_1
5281+
OPENSSL_init_ssl(0, NULL);
5282+
#else
52685283
/* Load all algorithms and initialize cpuid */
52695284
OPENSSL_add_all_algorithms_noconf();
52705285
/* Init OpenSSL */
@@ -5573,10 +5588,10 @@ PyInit__ssl(void)
55735588
return NULL;
55745589

55755590
/* OpenSSL version */
5576-
/* SSLeay() gives us the version of the library linked against,
5591+
/* OpenSSL_version_num() gives us the version of the library linked against,
55775592
which could be different from the headers version.
55785593
*/
5579-
libver = SSLeay();
5594+
libver = OpenSSL_version_num();
55805595
r = PyLong_FromUnsignedLong(libver);
55815596
if (r == NULL)
55825597
return NULL;
@@ -5586,7 +5601,7 @@ PyInit__ssl(void)
55865601
r = Py_BuildValue("IIIII", major, minor, fix, patch, status);
55875602
if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r))
55885603
return NULL;
5589-
r = PyUnicode_FromString(SSLeay_version(SSLEAY_VERSION));
5604+
r = PyUnicode_FromString(OpenSSL_version(OPENSSL_VERSION));
55905605
if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r))
55915606
return NULL;
55925607

0 commit comments

Comments
 (0)