@@ -142,6 +142,24 @@ static void _PySSLFixErrno(void) {
142
142
# define PY_OPENSSL_1_1_API 1
143
143
#endif
144
144
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
+
145
163
/* LibreSSL 2.7.0 provides necessary OpenSSL 1.1.0 APIs */
146
164
#if defined(LIBRESSL_VERSION_NUMBER ) && LIBRESSL_VERSION_NUMBER >= 0x2070000fL
147
165
# define PY_OPENSSL_1_1_API 1
@@ -201,6 +219,12 @@ static void _PySSLFixErrno(void) {
201
219
#define TLS_method SSLv23_method
202
220
#define TLS_client_method SSLv23_client_method
203
221
#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
204
228
205
229
static int X509_NAME_ENTRY_set (const X509_NAME_ENTRY * ne )
206
230
{
@@ -885,7 +909,7 @@ _ssl_configure_hostname(PySSLSocket *self, const char* server_hostname)
885
909
goto error ;
886
910
}
887
911
} 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 ),
889
913
ASN1_STRING_length (ip ))) {
890
914
_setSSLError (NULL , 0 , __FILE__ , __LINE__ );
891
915
goto error ;
@@ -1361,7 +1385,7 @@ _get_peer_alt_names (X509 *certificate) {
1361
1385
goto fail ;
1362
1386
}
1363
1387
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 ),
1365
1389
ASN1_STRING_length (as ));
1366
1390
if (v == NULL ) {
1367
1391
Py_DECREF (t );
@@ -1657,7 +1681,7 @@ _decode_certificate(X509 *certificate) {
1657
1681
ASN1_INTEGER * serialNumber ;
1658
1682
char buf [2048 ];
1659
1683
int len , result ;
1660
- ASN1_TIME * notBefore , * notAfter ;
1684
+ const ASN1_TIME * notBefore , * notAfter ;
1661
1685
PyObject * pnotBefore , * pnotAfter ;
1662
1686
1663
1687
retval = PyDict_New ();
@@ -1719,7 +1743,7 @@ _decode_certificate(X509 *certificate) {
1719
1743
Py_DECREF (sn_obj );
1720
1744
1721
1745
(void ) BIO_reset (biobuf );
1722
- notBefore = X509_get_notBefore (certificate );
1746
+ notBefore = X509_get0_notBefore (certificate );
1723
1747
ASN1_TIME_print (biobuf , notBefore );
1724
1748
len = BIO_gets (biobuf , buf , sizeof (buf )- 1 );
1725
1749
if (len < 0 ) {
@@ -1736,7 +1760,7 @@ _decode_certificate(X509 *certificate) {
1736
1760
Py_DECREF (pnotBefore );
1737
1761
1738
1762
(void ) BIO_reset (biobuf );
1739
- notAfter = X509_get_notAfter (certificate );
1763
+ notAfter = X509_get0_notAfter (certificate );
1740
1764
ASN1_TIME_print (biobuf , notAfter );
1741
1765
len = BIO_gets (biobuf , buf , sizeof (buf )- 1 );
1742
1766
if (len < 0 ) {
@@ -3079,17 +3103,23 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
3079
3103
ctx = SSL_CTX_new (SSLv3_method ());
3080
3104
break ;
3081
3105
#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 ))
3083
3109
case PY_SSL_VERSION_TLS1 :
3084
3110
ctx = SSL_CTX_new (TLSv1_method ());
3085
3111
break ;
3086
3112
#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 ))
3088
3116
case PY_SSL_VERSION_TLS1_1 :
3089
3117
ctx = SSL_CTX_new (TLSv1_1_method ());
3090
3118
break ;
3091
3119
#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 ))
3093
3123
case PY_SSL_VERSION_TLS1_2 :
3094
3124
ctx = SSL_CTX_new (TLSv1_2_method ());
3095
3125
break ;
@@ -3207,7 +3237,7 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
3207
3237
conservative and assume it wasn't fixed until release. We do this check
3208
3238
at runtime to avoid problems from the dynamic linker.
3209
3239
See #25672 for more on this. */
3210
- libver = SSLeay ();
3240
+ libver = OpenSSL_version_num ();
3211
3241
if (!(libver >= 0x10001000UL && libver < 0x1000108fUL ) &&
3212
3242
!(libver >= 0x10000000UL && libver < 0x100000dfUL )) {
3213
3243
SSL_CTX_set_mode (self -> ctx , SSL_MODE_RELEASE_BUFFERS );
@@ -5286,7 +5316,11 @@ PySSL_RAND(int len, int pseudo)
5286
5316
if (bytes == NULL )
5287
5317
return NULL ;
5288
5318
if (pseudo ) {
5319
+ #ifdef PY_OPENSSL_1_1_API
5320
+ ok = RAND_bytes ((unsigned char * )PyBytes_AS_STRING (bytes ), len );
5321
+ #else
5289
5322
ok = RAND_pseudo_bytes ((unsigned char * )PyBytes_AS_STRING (bytes ), len );
5323
+ #endif
5290
5324
if (ok == 0 || ok == 1 )
5291
5325
return Py_BuildValue ("NO" , bytes , ok == 1 ? Py_True : Py_False );
5292
5326
}
@@ -6373,7 +6407,7 @@ PyInit__ssl(void)
6373
6407
/* SSLeay() gives us the version of the library linked against,
6374
6408
which could be different from the headers version.
6375
6409
*/
6376
- libver = SSLeay ();
6410
+ libver = OpenSSL_version_num ();
6377
6411
r = PyLong_FromUnsignedLong (libver );
6378
6412
if (r == NULL )
6379
6413
return NULL ;
@@ -6383,7 +6417,7 @@ PyInit__ssl(void)
6383
6417
r = Py_BuildValue ("IIIII" , major , minor , fix , patch , status );
6384
6418
if (r == NULL || PyModule_AddObject (m , "OPENSSL_VERSION_INFO" , r ))
6385
6419
return NULL ;
6386
- r = PyUnicode_FromString (SSLeay_version ( SSLEAY_VERSION ));
6420
+ r = PyUnicode_FromString (OpenSSL_version ( OPENSSL_VERSION ));
6387
6421
if (r == NULL || PyModule_AddObject (m , "OPENSSL_VERSION" , r ))
6388
6422
return NULL ;
6389
6423
0 commit comments