@@ -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
@@ -199,6 +217,12 @@ static void _PySSLFixErrno(void) {
199
217
#define TLS_method SSLv23_method
200
218
#define TLS_client_method SSLv23_client_method
201
219
#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
202
226
203
227
static int X509_NAME_ENTRY_set (const X509_NAME_ENTRY * ne )
204
228
{
@@ -857,7 +881,7 @@ _ssl_configure_hostname(PySSLSocket *self, const char* server_hostname)
857
881
goto error ;
858
882
}
859
883
} 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 ),
861
885
ASN1_STRING_length (ip ))) {
862
886
_setSSLError (NULL , 0 , __FILE__ , __LINE__ );
863
887
goto error ;
@@ -1330,7 +1354,7 @@ _get_peer_alt_names (X509 *certificate) {
1330
1354
goto fail ;
1331
1355
}
1332
1356
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 ),
1334
1358
ASN1_STRING_length (as ));
1335
1359
if (v == NULL ) {
1336
1360
Py_DECREF (t );
@@ -1626,7 +1650,7 @@ _decode_certificate(X509 *certificate) {
1626
1650
ASN1_INTEGER * serialNumber ;
1627
1651
char buf [2048 ];
1628
1652
int len , result ;
1629
- ASN1_TIME * notBefore , * notAfter ;
1653
+ const ASN1_TIME * notBefore , * notAfter ;
1630
1654
PyObject * pnotBefore , * pnotAfter ;
1631
1655
1632
1656
retval = PyDict_New ();
@@ -1688,7 +1712,7 @@ _decode_certificate(X509 *certificate) {
1688
1712
Py_DECREF (sn_obj );
1689
1713
1690
1714
(void ) BIO_reset (biobuf );
1691
- notBefore = X509_get_notBefore (certificate );
1715
+ notBefore = X509_get0_notBefore (certificate );
1692
1716
ASN1_TIME_print (biobuf , notBefore );
1693
1717
len = BIO_gets (biobuf , buf , sizeof (buf )- 1 );
1694
1718
if (len < 0 ) {
@@ -1705,7 +1729,7 @@ _decode_certificate(X509 *certificate) {
1705
1729
Py_DECREF (pnotBefore );
1706
1730
1707
1731
(void ) BIO_reset (biobuf );
1708
- notAfter = X509_get_notAfter (certificate );
1732
+ notAfter = X509_get0_notAfter (certificate );
1709
1733
ASN1_TIME_print (biobuf , notAfter );
1710
1734
len = BIO_gets (biobuf , buf , sizeof (buf )- 1 );
1711
1735
if (len < 0 ) {
@@ -3023,17 +3047,23 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
3023
3047
ctx = SSL_CTX_new (SSLv3_method ());
3024
3048
break ;
3025
3049
#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 ))
3027
3053
case PY_SSL_VERSION_TLS1 :
3028
3054
ctx = SSL_CTX_new (TLSv1_method ());
3029
3055
break ;
3030
3056
#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 ))
3032
3060
case PY_SSL_VERSION_TLS1_1 :
3033
3061
ctx = SSL_CTX_new (TLSv1_1_method ());
3034
3062
break ;
3035
3063
#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 ))
3037
3067
case PY_SSL_VERSION_TLS1_2 :
3038
3068
ctx = SSL_CTX_new (TLSv1_2_method ());
3039
3069
break ;
@@ -3146,7 +3176,7 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
3146
3176
conservative and assume it wasn't fixed until release. We do this check
3147
3177
at runtime to avoid problems from the dynamic linker.
3148
3178
See #25672 for more on this. */
3149
- libver = SSLeay ();
3179
+ libver = OpenSSL_version_num ();
3150
3180
if (!(libver >= 0x10001000UL && libver < 0x1000108fUL ) &&
3151
3181
!(libver >= 0x10000000UL && libver < 0x100000dfUL )) {
3152
3182
SSL_CTX_set_mode (self -> ctx , SSL_MODE_RELEASE_BUFFERS );
@@ -5156,7 +5186,11 @@ PySSL_RAND(int len, int pseudo)
5156
5186
if (bytes == NULL )
5157
5187
return NULL ;
5158
5188
if (pseudo ) {
5189
+ #ifdef PY_OPENSSL_1_1_API
5190
+ ok = RAND_bytes ((unsigned char * )PyBytes_AS_STRING (bytes ), len );
5191
+ #else
5159
5192
ok = RAND_pseudo_bytes ((unsigned char * )PyBytes_AS_STRING (bytes ), len );
5193
+ #endif
5160
5194
if (ok == 0 || ok == 1 )
5161
5195
return Py_BuildValue ("NO" , bytes , ok == 1 ? Py_True : Py_False );
5162
5196
}
@@ -6240,7 +6274,7 @@ PyInit__ssl(void)
6240
6274
/* SSLeay() gives us the version of the library linked against,
6241
6275
which could be different from the headers version.
6242
6276
*/
6243
- libver = SSLeay ();
6277
+ libver = OpenSSL_version_num ();
6244
6278
r = PyLong_FromUnsignedLong (libver );
6245
6279
if (r == NULL )
6246
6280
return NULL ;
@@ -6250,7 +6284,7 @@ PyInit__ssl(void)
6250
6284
r = Py_BuildValue ("IIIII" , major , minor , fix , patch , status );
6251
6285
if (r == NULL || PyModule_AddObject (m , "OPENSSL_VERSION_INFO" , r ))
6252
6286
return NULL ;
6253
- r = PyUnicode_FromString (SSLeay_version ( SSLEAY_VERSION ));
6287
+ r = PyUnicode_FromString (OpenSSL_version ( OPENSSL_VERSION ));
6254
6288
if (r == NULL || PyModule_AddObject (m , "OPENSSL_VERSION" , r ))
6255
6289
return NULL ;
6256
6290
0 commit comments