Skip to content

gh-135234: improve _hashlib exceptions when reporting an OpenSSL function failure #135250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jun 9, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor _hashlib_HASH_copy_locked and locked_HMAC_CTX_copy
  • Loading branch information
picnixz committed Jun 8, 2025
commit 4bc2808cad2148ed151eb3127aa224b8b8cf7e0b
24 changes: 15 additions & 9 deletions Modules/_hashopenssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,11 @@ _hashlib_HASH_copy_locked(HASHobject *self, EVP_MD_CTX *new_ctx_p)
ENTER_HASHLIB(self);
result = EVP_MD_CTX_copy(new_ctx_p, self->ctx);
LEAVE_HASHLIB(self);
return result;
if (result == 0) {
notify_ssl_error_occurred_in(Py_STRINGIFY(EVP_MD_CTX_copy));
return -1;
}
return 0;
}

/* External methods for a hash object */
Expand All @@ -675,7 +679,7 @@ _hashlib_HASH_copy_impl(HASHobject *self)
if ((newobj = new_hash_object(Py_TYPE(self))) == NULL)
return NULL;

if (!_hashlib_HASH_copy_locked(self, newobj->ctx)) {
if (_hashlib_HASH_copy_locked(self, newobj->ctx) < 0) {
Py_DECREF(newobj);
notify_ssl_error_occurred();
return NULL;
Expand Down Expand Up @@ -899,7 +903,7 @@ _hashlib_HASHXOF_digest_impl(HASHobject *self, Py_ssize_t length)
return NULL;
}

if (!_hashlib_HASH_copy_locked(self, temp_ctx)) {
if (_hashlib_HASH_copy_locked(self, temp_ctx) < 0) {
goto error;
}
if (!EVP_DigestFinalXOF(temp_ctx,
Expand Down Expand Up @@ -949,7 +953,7 @@ _hashlib_HASHXOF_hexdigest_impl(HASHobject *self, Py_ssize_t length)
}

/* Get the raw (binary) digest value */
if (!_hashlib_HASH_copy_locked(self, temp_ctx)) {
if (_hashlib_HASH_copy_locked(self, temp_ctx) < 0) {
goto error;
}
if (!EVP_DigestFinalXOF(temp_ctx, digest, length)) {
Expand Down Expand Up @@ -1736,7 +1740,11 @@ locked_HMAC_CTX_copy(HMAC_CTX *new_ctx_p, HMACobject *self)
ENTER_HASHLIB(self);
result = HMAC_CTX_copy(new_ctx_p, self->ctx);
LEAVE_HASHLIB(self);
return result;
if (result == 0) {
notify_ssl_error_occurred_in(Py_STRINGIFY(HMAC_CTX_copy));
return -1;
}
return 0;
}

/* returning 0 means that an error occurred and an exception is set */
Expand Down Expand Up @@ -1805,9 +1813,8 @@ _hashlib_HMAC_copy_impl(HMACobject *self)
if (ctx == NULL) {
return PyErr_NoMemory();
}
if (!locked_HMAC_CTX_copy(ctx, self)) {
if (locked_HMAC_CTX_copy(ctx, self) < 0) {
HMAC_CTX_free(ctx);
notify_ssl_error_occurred();
return NULL;
}

Expand Down Expand Up @@ -1879,9 +1886,8 @@ _hmac_digest(HMACobject *self, unsigned char *buf, unsigned int len)
(void)PyErr_NoMemory();
return 0;
}
if (!locked_HMAC_CTX_copy(temp_ctx, self)) {
if (locked_HMAC_CTX_copy(temp_ctx, self) < 0) {
HMAC_CTX_free(temp_ctx);
notify_ssl_error_occurred();
return 0;
}
int r = HMAC_Final(temp_ctx, buf, &len);
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy