@@ -144,6 +144,24 @@ static void _PySSLFixErrno(void) {
144
144
# define PY_OPENSSL_1_1_API 1
145
145
#endif
146
146
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
+
147
165
/* LibreSSL 2.7.0 provides necessary OpenSSL 1.1.0 APIs */
148
166
#if defined(LIBRESSL_VERSION_NUMBER ) && LIBRESSL_VERSION_NUMBER >= 0x2070000fL
149
167
# define PY_OPENSSL_1_1_API 1
@@ -203,6 +221,12 @@ static void _PySSLFixErrno(void) {
203
221
#define TLS_method SSLv23_method
204
222
#define TLS_client_method SSLv23_client_method
205
223
#define TLS_server_method SSLv23_server_method
224
+ #define ASN1_STRING_get0_data ASN1_STRING_data
225
+ #define X509_get0_notBefore X509_get_notBefore
226
+ #define X509_get0_notAfter X509_get_notAfter
227
+ #define OpenSSL_version_num SSLeay
228
+ #define OpenSSL_version SSLeay_version
229
+ #define OPENSSL_VERSION SSLEAY_VERSION
206
230
207
231
static int X509_NAME_ENTRY_set (const X509_NAME_ENTRY * ne )
208
232
{
@@ -887,7 +911,7 @@ _ssl_configure_hostname(PySSLSocket *self, const char* server_hostname)
887
911
goto error ;
888
912
}
889
913
} else {
890
- if (!X509_VERIFY_PARAM_set1_ip (param , ASN1_STRING_data (ip ),
914
+ if (!X509_VERIFY_PARAM_set1_ip (param , ASN1_STRING_get0_data (ip ),
891
915
ASN1_STRING_length (ip ))) {
892
916
_setSSLError (NULL , 0 , __FILE__ , __LINE__ );
893
917
goto error ;
@@ -1363,7 +1387,7 @@ _get_peer_alt_names (X509 *certificate) {
1363
1387
goto fail ;
1364
1388
}
1365
1389
PyTuple_SET_ITEM (t , 0 , v );
1366
- v = PyUnicode_FromStringAndSize ((char * )ASN1_STRING_data (as ),
1390
+ v = PyUnicode_FromStringAndSize ((char * )ASN1_STRING_get0_data (as ),
1367
1391
ASN1_STRING_length (as ));
1368
1392
if (v == NULL ) {
1369
1393
Py_DECREF (t );
@@ -1659,7 +1683,7 @@ _decode_certificate(X509 *certificate) {
1659
1683
ASN1_INTEGER * serialNumber ;
1660
1684
char buf [2048 ];
1661
1685
int len , result ;
1662
- ASN1_TIME * notBefore , * notAfter ;
1686
+ const ASN1_TIME * notBefore , * notAfter ;
1663
1687
PyObject * pnotBefore , * pnotAfter ;
1664
1688
1665
1689
retval = PyDict_New ();
@@ -1721,7 +1745,7 @@ _decode_certificate(X509 *certificate) {
1721
1745
Py_DECREF (sn_obj );
1722
1746
1723
1747
(void ) BIO_reset (biobuf );
1724
- notBefore = X509_get_notBefore (certificate );
1748
+ notBefore = X509_get0_notBefore (certificate );
1725
1749
ASN1_TIME_print (biobuf , notBefore );
1726
1750
len = BIO_gets (biobuf , buf , sizeof (buf )- 1 );
1727
1751
if (len < 0 ) {
@@ -1738,7 +1762,7 @@ _decode_certificate(X509 *certificate) {
1738
1762
Py_DECREF (pnotBefore );
1739
1763
1740
1764
(void ) BIO_reset (biobuf );
1741
- notAfter = X509_get_notAfter (certificate );
1765
+ notAfter = X509_get0_notAfter (certificate );
1742
1766
ASN1_TIME_print (biobuf , notAfter );
1743
1767
len = BIO_gets (biobuf , buf , sizeof (buf )- 1 );
1744
1768
if (len < 0 ) {
@@ -3081,17 +3105,23 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
3081
3105
ctx = SSL_CTX_new (SSLv3_method ());
3082
3106
break ;
3083
3107
#endif
3084
- #if defined(TLS1_VERSION ) && !defined(OPENSSL_NO_TLS1 )
3108
+ #if (defined(TLS1_VERSION ) && \
3109
+ !defined(OPENSSL_NO_TLS1 ) && \
3110
+ !defined(OPENSSL_NO_TLS1_METHOD ))
3085
3111
case PY_SSL_VERSION_TLS1 :
3086
3112
ctx = SSL_CTX_new (TLSv1_method ());
3087
3113
break ;
3088
3114
#endif
3089
- #if defined(TLS1_1_VERSION ) && !defined(OPENSSL_NO_TLS1_1 )
3115
+ #if (defined(TLS1_1_VERSION ) && \
3116
+ !defined(OPENSSL_NO_TLS1_1 ) && \
3117
+ !defined(OPENSSL_NO_TLS1_1_METHOD ))
3090
3118
case PY_SSL_VERSION_TLS1_1 :
3091
3119
ctx = SSL_CTX_new (TLSv1_1_method ());
3092
3120
break ;
3093
3121
#endif
3094
- #if defined(TLS1_2_VERSION ) && !defined(OPENSSL_NO_TLS1_2 )
3122
+ #if (defined(TLS1_2_VERSION ) && \
3123
+ !defined(OPENSSL_NO_TLS1_2 ) && \
3124
+ !defined(OPENSSL_NO_TLS1_2_METHOD ))
3095
3125
case PY_SSL_VERSION_TLS1_2 :
3096
3126
ctx = SSL_CTX_new (TLSv1_2_method ());
3097
3127
break ;
@@ -3209,7 +3239,7 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
3209
3239
conservative and assume it wasn't fixed until release. We do this check
3210
3240
at runtime to avoid problems from the dynamic linker.
3211
3241
See #25672 for more on this. */
3212
- libver = SSLeay ();
3242
+ libver = OpenSSL_version_num ();
3213
3243
if (!(libver >= 0x10001000UL && libver < 0x1000108fUL ) &&
3214
3244
!(libver >= 0x10000000UL && libver < 0x100000dfUL )) {
3215
3245
SSL_CTX_set_mode (self -> ctx , SSL_MODE_RELEASE_BUFFERS );
@@ -5289,7 +5319,11 @@ PySSL_RAND(int len, int pseudo)
5289
5319
if (bytes == NULL )
5290
5320
return NULL ;
5291
5321
if (pseudo ) {
5322
+ #ifdef PY_OPENSSL_1_1_API
5323
+ ok = RAND_bytes ((unsigned char * )PyBytes_AS_STRING (bytes ), len );
5324
+ #else
5292
5325
ok = RAND_pseudo_bytes ((unsigned char * )PyBytes_AS_STRING (bytes ), len );
5326
+ #endif
5293
5327
if (ok == 0 || ok == 1 )
5294
5328
return Py_BuildValue ("NO" , bytes , ok == 1 ? Py_True : Py_False );
5295
5329
}
@@ -6376,7 +6410,7 @@ PyInit__ssl(void)
6376
6410
/* SSLeay() gives us the version of the library linked against,
6377
6411
which could be different from the headers version.
6378
6412
*/
6379
- libver = SSLeay ();
6413
+ libver = OpenSSL_version_num ();
6380
6414
r = PyLong_FromUnsignedLong (libver );
6381
6415
if (r == NULL )
6382
6416
return NULL ;
@@ -6386,7 +6420,7 @@ PyInit__ssl(void)
6386
6420
r = Py_BuildValue ("IIIII" , major , minor , fix , patch , status );
6387
6421
if (r == NULL || PyModule_AddObject (m , "OPENSSL_VERSION_INFO" , r ))
6388
6422
return NULL ;
6389
- r = PyUnicode_FromString (SSLeay_version ( SSLEAY_VERSION ));
6423
+ r = PyUnicode_FromString (OpenSSL_version ( OPENSSL_VERSION ));
6390
6424
if (r == NULL || PyModule_AddObject (m , "OPENSSL_VERSION" , r ))
6391
6425
return NULL ;
6392
6426
0 commit comments