FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-135234: improve `_hashlib` exceptions when reporting an OpenSSL function failure by picnixz · Pull Request #135250 · python/cpython · GitHub

Repository navigation

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension .c  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
Prev Previous commit
Next Next commit
set remaining exceptions after an OpenSSL failure
  • Loading branch information
picnixz committed Jun 8, 2025
commit 14b87df1b0f7c63f9555b3e265baf085f11a787c
49 changes: 23 additions & 26 deletions Modules/_hashopenssl.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ notify_ssl_error_occurred_in(const char *funcname)
"error in OpenSSL function: %s", funcname);
}

/* Same as notify_smart_ssl_error_occurred() for failed OpenSSL functions. */
/* Same as notify_ssl_error_occurred() with smart exception types. */
static inline void
notify_smart_ssl_error_occurred_in(const char *funcname)
{
Expand Down Expand Up @@ -609,7 +609,7 @@ new_hash_object(PyTypeObject *type)
retval->ctx = EVP_MD_CTX_new();
if (retval->ctx == NULL) {
Py_DECREF(retval);
PyErr_NoMemory();
notify_smart_ssl_error_occurred_in(Py_STRINGIFY(EVP_MD_CTX_new));
return NULL;
}

Expand All @@ -627,7 +627,7 @@ _hashlib_HASH_hash(HASHobject *self, const void *vp, Py_ssize_t len)
else
process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int);
if (!EVP_DigestUpdate(self->ctx, (const void*)cp, process)) {
notify_ssl_error_occurred();
notify_ssl_error_occurred_in(Py_STRINGIFY(EVP_DigestUpdate));
return -1;
}
len -= process;
Expand Down Expand Up @@ -681,7 +681,6 @@ _hashlib_HASH_copy_impl(HASHobject *self)

if (_hashlib_HASH_copy_locked(self, newobj->ctx) < 0) {
Py_DECREF(newobj);
notify_ssl_error_occurred();
return NULL;
}
return (PyObject *)newobj;
Expand All @@ -692,7 +691,7 @@ _hashlib_HASH_digest_compute(HASHobject *self, unsigned char *digest)
{
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
if (ctx == NULL) {
notify_ssl_error_occurred_in(Py_STRINGIFY(EVP_MD_CTX_new));
notify_smart_ssl_error_occurred_in(Py_STRINGIFY(EVP_MD_CTX_new));
return -1;
}
if (_hashlib_HASH_copy_locked(self, ctx) < 0) {
Expand Down Expand Up @@ -809,7 +808,7 @@ _hashlib_HASH_get_name(PyObject *op, void *Py_UNUSED(closure))
HASHobject *self = HASHobject_CAST(op);
const EVP_MD *md = EVP_MD_CTX_md(self->ctx);
if (md == NULL) {
notify_ssl_error_occurred();
notify_ssl_error_occurred("missing EVP_MD");
return NULL;
}
return get_openssl_evp_md_name(md);
Expand Down Expand Up @@ -899,7 +898,7 @@ _hashlib_HASHXOF_digest_impl(HASHobject *self, Py_ssize_t length)
temp_ctx = EVP_MD_CTX_new();
if (temp_ctx == NULL) {
Py_DECREF(retval);
PyErr_NoMemory();
notify_smart_ssl_error_occurred_in(Py_STRINGIFY(EVP_MD_CTX_new));
return NULL;
}

Expand All @@ -910,6 +909,7 @@ _hashlib_HASHXOF_digest_impl(HASHobject *self, Py_ssize_t length)
(unsigned char*)PyBytes_AS_STRING(retval),
length))
{
notify_ssl_error_occurred_in(Py_STRINGIFY(EVP_DigestFinalXOF));
goto error;
}

Expand All @@ -919,7 +919,6 @@ _hashlib_HASHXOF_digest_impl(HASHobject *self, Py_ssize_t length)
error:
Py_DECREF(retval);
EVP_MD_CTX_free(temp_ctx);
notify_ssl_error_occurred();
return NULL;
}

Expand Down Expand Up @@ -948,7 +947,7 @@ _hashlib_HASHXOF_hexdigest_impl(HASHobject *self, Py_ssize_t length)
temp_ctx = EVP_MD_CTX_new();
if (temp_ctx == NULL) {
PyMem_Free(digest);
PyErr_NoMemory();
notify_smart_ssl_error_occurred_in(Py_STRINGIFY(EVP_MD_CTX_new));
return NULL;
}

Expand All @@ -957,6 +956,7 @@ _hashlib_HASHXOF_hexdigest_impl(HASHobject *self, Py_ssize_t length)
goto error;
}
if (!EVP_DigestFinalXOF(temp_ctx, digest, length)) {
notify_ssl_error_occurred_in(Py_STRINGIFY(EVP_DigestFinalXOF));
goto error;
}

Expand All @@ -969,7 +969,6 @@ _hashlib_HASHXOF_hexdigest_impl(HASHobject *self, Py_ssize_t length)
error:
PyMem_Free(digest);
EVP_MD_CTX_free(temp_ctx);
notify_ssl_error_occurred();
return NULL;
}

Expand Down Expand Up @@ -1073,7 +1072,7 @@ _hashlib_HASH(PyObject *module, const char *digestname, PyObject *data_obj,

int result = EVP_DigestInit_ex(self->ctx, digest, NULL);
if (!result) {
notify_ssl_error_occurred();
notify_ssl_error_occurred_in(Py_STRINGIFY(EVP_DigestInit_ex));
Py_CLEAR(self);
goto exit;
}
Expand Down Expand Up @@ -1482,7 +1481,7 @@ pbkdf2_hmac_impl(PyObject *module, const char *hash_name,

if (!retval) {
Py_CLEAR(key_obj);
notify_ssl_error_occurred();
notify_ssl_error_occurred_in(Py_STRINGIFY(PKCS5_PBKDF2_HMAC));
goto end;
}

Expand Down Expand Up @@ -1580,7 +1579,7 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,

if (!retval) {
Py_CLEAR(key_obj);
notify_ssl_error_occurred();
notify_ssl_error_occurred_in(Py_STRINGIFY(EVP_PBE_scrypt));
return NULL;
}
return key_obj;
Expand Down Expand Up @@ -1637,7 +1636,7 @@ _hashlib_hmac_singleshot_impl(PyObject *module, Py_buffer *key,
PY_EVP_MD_free(evp);

if (result == NULL) {
notify_ssl_error_occurred();
notify_ssl_error_occurred_in(Py_STRINGIFY(HMAC));
return NULL;
}
return PyBytes_FromStringAndSize((const char*)md, md_len);
Expand Down Expand Up @@ -1698,14 +1697,14 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
ctx = HMAC_CTX_new();
if (ctx == NULL) {
PY_EVP_MD_free(digest);
PyErr_NoMemory();
notify_smart_ssl_error_occurred_in(Py_STRINGIFY(HMAC_CTX_new));
goto error;
}

r = HMAC_Init_ex(ctx, key->buf, (int)key->len, digest, NULL /* impl */);
PY_EVP_MD_free(digest);
if (r == 0) {
notify_ssl_error_occurred();
notify_ssl_error_occurred_in(Py_STRINGIFY(HMAC_Init_ex));
goto error;
}

Expand Down Expand Up @@ -1791,7 +1790,7 @@ _hmac_update(HMACobject *self, PyObject *obj)
PyBuffer_Release(&view);

if (r == 0) {
notify_ssl_error_occurred();
notify_ssl_error_occurred_in(Py_STRINGIFY(HMAC_Update));
return 0;
}
return 1;
Expand All @@ -1811,7 +1810,8 @@ _hashlib_HMAC_copy_impl(HMACobject *self)

HMAC_CTX *ctx = HMAC_CTX_new();
if (ctx == NULL) {
return PyErr_NoMemory();
notify_smart_ssl_error_occurred_in(Py_STRINGIFY(HMAC_CTX_new));
return NULL;
}
if (locked_HMAC_CTX_copy(ctx, self) < 0) {
HMAC_CTX_free(ctx);
Expand Down Expand Up @@ -1883,7 +1883,7 @@ _hmac_digest(HMACobject *self, unsigned char *buf, unsigned int len)
{
HMAC_CTX *temp_ctx = HMAC_CTX_new();
if (temp_ctx == NULL) {
(void)PyErr_NoMemory();
notify_smart_ssl_error_occurred_in(Py_STRINGIFY(HMAC_CTX_new));
return 0;
}
if (locked_HMAC_CTX_copy(temp_ctx, self) < 0) {
Expand All @@ -1893,7 +1893,7 @@ _hmac_digest(HMACobject *self, unsigned char *buf, unsigned int len)
int r = HMAC_Final(temp_ctx, buf, &len);
HMAC_CTX_free(temp_ctx);
if (r == 0) {
notify_ssl_error_occurred();
notify_ssl_error_occurred_in(Py_STRINGIFY(HMAC_Final));
return 0;
}
return 1;
Expand Down Expand Up @@ -2113,16 +2113,13 @@ _hashlib_get_fips_mode_impl(PyObject *module)
#else
ERR_clear_error();
int result = FIPS_mode();
if (result == 0) {
if (result == 0 && ERR_peek_last_error()) {
// "If the library was built without support of the FIPS Object Module,
// then the function will return 0 with an error code of
// CRYPTO_R_FIPS_MODE_NOT_SUPPORTED (0x0f06d065)."
// But 0 is also a valid result value.
unsigned long errcode = ERR_peek_last_error();
if (errcode) {
notify_ssl_error_occurred();
return -1;
}
notify_ssl_error_occurred();
return -1;
}
return result;
#endif
Expand Down

Back | FazBrowse Home | New Git URL