64
64
#define PY_EVP_MD_fetch (algorithm , properties ) EVP_MD_fetch(NULL, algorithm, properties)
65
65
#define PY_EVP_MD_up_ref (md ) EVP_MD_up_ref(md)
66
66
#define PY_EVP_MD_free (md ) EVP_MD_free(md)
67
+
68
+ #define PY_EVP_MD_CTX_md (CTX ) EVP_MD_CTX_get0_md(CTX)
67
69
#else
68
70
#define PY_EVP_MD const EVP_MD
69
71
#define PY_EVP_MD_fetch (algorithm , properties ) EVP_get_digestbyname(algorithm)
70
72
#define PY_EVP_MD_up_ref (md ) do {} while(0)
71
73
#define PY_EVP_MD_free (md ) do {} while(0)
74
+
75
+ #define PY_EVP_MD_CTX_md (CTX ) EVP_MD_CTX_md(CTX)
72
76
#endif
73
77
74
78
/* hash alias map and fast lookup
@@ -308,6 +312,14 @@ class _hashlib.HMAC "HMACobject *" "&PyType_Type"
308
312
309
313
/* LCOV_EXCL_START */
310
314
315
+ /* Thin wrapper around ERR_reason_error_string() returning non-NULL text. */
316
+ static const char *
317
+ py_wrapper_ERR_reason_error_string (unsigned long errcode )
318
+ {
319
+ const char * reason = ERR_reason_error_string (errcode );
320
+ return reason ? reason : "no reason" ;
321
+ }
322
+
311
323
/* Set an exception of given type using the given OpenSSL error code. */
312
324
static void
313
325
set_ssl_exception_from_errcode (PyObject * exc_type , unsigned long errcode )
@@ -317,8 +329,13 @@ set_ssl_exception_from_errcode(PyObject *exc_type, unsigned long errcode)
317
329
318
330
/* ERR_ERROR_STRING(3) ensures that the messages below are ASCII */
319
331
const char * lib = ERR_lib_error_string (errcode );
332
+ #ifdef Py_HAS_OPENSSL3_SUPPORT
333
+ // Since OpenSSL 3.0, ERR_func_error_string() always returns NULL.
334
+ const char * func = NULL ;
335
+ #else
320
336
const char * func = ERR_func_error_string (errcode );
321
- const char * reason = ERR_reason_error_string (errcode );
337
+ #endif
338
+ const char * reason = py_wrapper_ERR_reason_error_string (errcode );
322
339
323
340
if (lib && func ) {
324
341
PyErr_Format (exc_type , "[%s: %s] %s" , lib , func , reason );
@@ -838,7 +855,7 @@ static PyObject *
838
855
_hashlib_HASH_get_name (PyObject * op , void * Py_UNUSED (closure ))
839
856
{
840
857
HASHobject * self = HASHobject_CAST (op );
841
- const EVP_MD * md = EVP_MD_CTX_md (self -> ctx );
858
+ const EVP_MD * md = PY_EVP_MD_CTX_md (self -> ctx );
842
859
if (md == NULL ) {
843
860
notify_ssl_error_occurred ("missing EVP_MD for HASH context" );
844
861
return NULL ;
0 commit comments