Skip to content
Merged
Show file tree
Hide file tree
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
Add new internal exception to indicate unsupported constructor
  • Loading branch information
tiran committed Mar 19, 2021
commit 83ae2d38612f915fe27b21f4fd633a4e68295bc5
4 changes: 2 additions & 2 deletions Lib/hmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(self, key, msg=None, digestmod=''):
if _hashopenssl and isinstance(digestmod, (str, _functype)):
try:
self._init_hmac(key, msg, digestmod)
except ValueError:
except _hashopenssl.UnsupportedDigestmodError:
self._init_old(key, msg, digestmod)
else:
self._init_old(key, msg, digestmod)
Expand Down Expand Up @@ -196,7 +196,7 @@ def digest(key, msg, digest):
if _hashopenssl is not None and isinstance(digest, (str, _functype)):
try:
return _hashopenssl.hmac_digest(key, msg, digest)
except ValueError:
except _hashopenssl.UnsupportedDigestmodError:
pass
Comment thread
gpshead marked this conversation as resolved.

if callable(digest):
Expand Down
37 changes: 33 additions & 4 deletions Modules/_hashopenssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ typedef struct {
PyTypeObject *EVPXOFtype;
#endif
PyObject *constructs;
PyObject *unsupported_digestmod_error;
} _hashlibstate;

static inline _hashlibstate*
Expand Down Expand Up @@ -290,6 +291,11 @@ py_digest_by_name(const char *name)
#endif
}

if (digest == NULL) {
PyErr_Format(PyExc_ValueError, "unsupported hash type %s", name);
return NULL;
}

return digest;
}

Expand All @@ -314,8 +320,11 @@ py_digest_by_digestmod(PyObject *module, PyObject *digestmod) {
name_obj = PyDict_GetItem(state->constructs, digestmod);
}
if (name_obj == NULL) {
_hashlibstate *state = get_hashlib_state(module);
PyErr_Clear();
PyErr_Format(PyExc_ValueError, "Unsupported digestmod %R", digestmod);
PyErr_Format(
state->unsupported_digestmod_error,
"Unsupported digestmod %R", digestmod);
return NULL;
}

Expand All @@ -326,7 +335,6 @@ py_digest_by_digestmod(PyObject *module, PyObject *digestmod) {

evp = py_digest_by_name(name);
if (evp == NULL) {
PyErr_Format(PyExc_ValueError, "unsupported hash type %s", name);
return NULL;
}

Expand Down Expand Up @@ -870,6 +878,9 @@ EVP_new_impl(PyObject *module, PyObject *name_obj, PyObject *data_obj,
GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);

digest = py_digest_by_name(name);
if (digest == NULL) {
return NULL;
}

ret_obj = EVPnew(module, digest,
(unsigned char*)view.buf, view.len,
Expand Down Expand Up @@ -1165,7 +1176,6 @@ pbkdf2_hmac_impl(PyObject *module, const char *hash_name,

digest = py_digest_by_name(hash_name);
if (digest == NULL) {
PyErr_SetString(PyExc_ValueError, "unsupported hash type");
goto end;
}

Expand Down Expand Up @@ -2026,6 +2036,7 @@ hashlib_traverse(PyObject *m, visitproc visit, void *arg)
Py_VISIT(state->EVPXOFtype);
#endif
Py_VISIT(state->constructs);
Py_VISIT(state->unsupported_digestmod_error);
return 0;
}

Expand All @@ -2039,6 +2050,7 @@ hashlib_clear(PyObject *m)
Py_CLEAR(state->EVPXOFtype);
#endif
Py_CLEAR(state->constructs);
Py_CLEAR(state->unsupported_digestmod_error);
return 0;
}

Expand Down Expand Up @@ -2158,7 +2170,23 @@ hashlib_init_constructors(PyObject *module)
if (proxy == NULL) {
return -1;
}
if (PyModule_AddObjectRef(module, "constructors", proxy) < 0) {
if (PyModule_AddObjectRef(module, "_constructors", proxy) < 0) {
return -1;
}
return 0;
}

static int
hashlib_exception(PyObject *module)
{
_hashlibstate *state = get_hashlib_state(module);
state->unsupported_digestmod_error = PyErr_NewException(
"_hashlib.UnsupportedDigestmodError", PyExc_ValueError, NULL);
if (state->unsupported_digestmod_error == NULL) {
return -1;
}
if (PyModule_AddObjectRef(module, "UnsupportedDigestmodError",
state->unsupported_digestmod_error) < 0) {
return -1;
}
return 0;
Expand All @@ -2173,6 +2201,7 @@ static PyModuleDef_Slot hashlib_slots[] = {
{Py_mod_exec, hashlib_init_hmactype},
{Py_mod_exec, hashlib_md_meth_names},
{Py_mod_exec, hashlib_init_constructors},
{Py_mod_exec, hashlib_exception},
{0, NULL}
};

Expand Down