Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Include/internal/pycore_global_objects_fini_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(locals)
STRUCT_FOR_ID(logoption)
STRUCT_FOR_ID(loop)
STRUCT_FOR_ID(lower)
STRUCT_FOR_ID(manual_reset)
STRUCT_FOR_ID(mapping)
STRUCT_FOR_ID(markers)
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_runtime_init_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Include/internal/pycore_unicodeobject_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Lib/test/test_hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,15 @@ def test_unknown_hash(self):
def test_new_upper_to_lower(self):
self.assertEqual(hashlib.new("SHA256").name, "sha256")

@hashlib_helper.requires_openssl_hashdigest("SHA256-192")
def test_name_attribute_without_nid(self):
# OpenSSL 3 provider digest without NID.
# See https://github.com/python/cpython/issues/162712.
h = hashlib.new("SHA256-192")
self.assertNotEqual(h.name, "undefined")
self.assertEqual(h.name, h.name.lower())
self.assertEqual(hashlib.new(h.name).name, h.name)

@support.thread_unsafe("modifies sys.modules")
def test_get_builtin_constructor(self):
get_builtin_constructor = getattr(hashlib,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:mod:`hashlib`: the :attr:`name <hashlib.hash.name>` of OpenSSL hash objects whose
digest has no OpenSSL NID, such as ``SHA256-192``, is now the lowercase name
given by the OpenSSL provider instead of ``"undefined"``.
Patch by Bénédikt Tran.
49 changes: 46 additions & 3 deletions Modules/_hashopenssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#endif

#include "Python.h"
#include "pycore_global_strings.h" // _Py_ID()
#include "pycore_hashtable.h"
#include "pycore_runtime.h" // _PyRuntime
#include "pycore_strhex.h" // _Py_strhex()
#include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_LOAD_PTR_RELAXED
#include "hashlib.h"
Expand Down Expand Up @@ -656,13 +658,46 @@ get_openssl_utf8name_by_nid(int nid)
}
#endif

/* Same as get_hashlib_utf8name_by_nid() but using an EVP_MD object. */
/*
* Same as get_hashlib_utf8name_by_nid() but using an EVP_MD object.
*
* With OpenSSL 3, a provider digest without NID, such as SHA2-256/192,
* is named by its provider, if any, and is named as NID_undef otherwise.
*/
static const char *
get_hashlib_utf8name_by_evp_md(const EVP_MD *md)
{
assert(md != NULL);
return get_hashlib_utf8name_by_nid(EVP_MD_nid(md));
int nid = EVP_MD_nid(md);
#ifdef Py_HAS_OPENSSL3_SUPPORT
if (nid == NID_undef) {
const char *name = EVP_MD_get0_name(md);
if (name != NULL) {
return name;
}
}
#endif
return get_hashlib_utf8name_by_nid(nid);
}

#ifdef Py_HAS_OPENSSL3_SUPPORT
/*
* Convert the UTF-8 name of a provider digest into a lowercase string.
*
* On error, set an exception and return NULL.
*/
static PyObject *
get_hashlib_lowercase_name(const char *name)
{
PyObject *str = PyUnicode_FromString(name);
if (str == NULL) {
return NULL;
}
PyObject *res = PyObject_CallMethodNoArgs(str, &_Py_ID(lower));
Py_DECREF(str);
return res;
}
#endif

/*
* Return 1 if the property query clause [1] must be "-fips" and 0 otherwise.
Expand Down Expand Up @@ -1043,7 +1078,15 @@ _hashlib_HASH_get_name(PyObject *op, void *Py_UNUSED(closure))
}
const char *name = get_hashlib_utf8name_by_evp_md(md);
assert(name != NULL || PyErr_Occurred());
return name == NULL ? NULL : PyUnicode_FromString(name);
if (name == NULL) {
return NULL;
}
#ifdef Py_HAS_OPENSSL3_SUPPORT
if (EVP_MD_nid(md) == NID_undef) {
return get_hashlib_lowercase_name(name);
}
#endif
return PyUnicode_FromString(name);
}

static PyGetSetDef HASH_getsets[] = {
Expand Down
Loading