Skip to content

Commit f02daf3

Browse files
committed
improve error if EVP_MD contexts are missing from HMAC objects
1 parent 0c07a7f commit f02daf3

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

Modules/_hashopenssl.c

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,9 @@ EVP_get_name(PyObject *op, void *Py_UNUSED(closure))
728728
EVPobject *self = EVPobject_CAST(op);
729729
const EVP_MD *md = EVP_MD_CTX_md(self->ctx);
730730
if (md == NULL) {
731+
// TODO(picnixz): investigate whether this is dead code or not
732+
// as OpenSSL define macros that do not check if EVP_MD_CTX_md()
733+
// returns NULL or not.
731734
notify_ssl_error_occurred();
732735
return NULL;
733736
}
@@ -1570,6 +1573,16 @@ _hashlib_hmac_singleshot_impl(PyObject *module, Py_buffer *key,
15701573

15711574
static int _hmac_update(HMACobject*, PyObject*);
15721575

1576+
static const EVP_MD *
1577+
_hashlib_hmac_get_md(HMACobject *self)
1578+
{
1579+
const EVP_MD *md = HMAC_CTX_get_md(self->ctx);
1580+
if (md == NULL) {
1581+
raise_ssl_error(PyExc_ValueError, "missing EVP_MD for HMAC context");
1582+
}
1583+
return md;
1584+
}
1585+
15731586
/*[clinic input]
15741587
_hashlib.hmac_new
15751588
@@ -1657,17 +1670,16 @@ locked_HMAC_CTX_copy(HMAC_CTX *new_ctx_p, HMACobject *self)
16571670

16581671
/* returning 0 means that an error occurred and an exception is set */
16591672
static unsigned int
1660-
_hmac_digest_size(HMACobject *self)
1673+
_hashlib_hmac_digest_size(HMACobject *self)
16611674
{
1662-
const EVP_MD *md = HMAC_CTX_get_md(self->ctx);
1675+
const EVP_MD *md = _hashlib_hmac_get_md(self);
16631676
if (md == NULL) {
1664-
notify_ssl_error_occurred();
16651677
return 0;
16661678
}
16671679
unsigned int digest_size = EVP_MD_size(md);
16681680
assert(digest_size <= EVP_MAX_MD_SIZE);
16691681
if (digest_size == 0) {
1670-
notify_ssl_error_occurred();
1682+
raise_ssl_error(PyExc_ValueError, "invalid digest size");
16711683
}
16721684
return digest_size;
16731685
}
@@ -1756,9 +1768,8 @@ static PyObject *
17561768
_hmac_repr(PyObject *op)
17571769
{
17581770
HMACobject *self = HMACobject_CAST(op);
1759-
const EVP_MD *md = HMAC_CTX_get_md(self->ctx);
1771+
const EVP_MD *md = _hashlib_hmac_get_md(self);
17601772
if (md == NULL) {
1761-
notify_ssl_error_occurred();
17621773
return NULL;
17631774
}
17641775
PyObject *digest_name = py_digest_name(md);
@@ -1821,7 +1832,7 @@ _hashlib_HMAC_digest_impl(HMACobject *self)
18211832
/*[clinic end generated code: output=1b1424355af7a41e input=bff07f74da318fb4]*/
18221833
{
18231834
unsigned char digest[EVP_MAX_MD_SIZE];
1824-
unsigned int digest_size = _hmac_digest_size(self);
1835+
unsigned int digest_size = _hashlib_hmac_digest_size(self);
18251836
if (digest_size == 0) {
18261837
return NULL;
18271838
}
@@ -1846,7 +1857,7 @@ _hashlib_HMAC_hexdigest_impl(HMACobject *self)
18461857
/*[clinic end generated code: output=80d825be1eaae6a7 input=5abc42702874ddcf]*/
18471858
{
18481859
unsigned char digest[EVP_MAX_MD_SIZE];
1849-
unsigned int digest_size = _hmac_digest_size(self);
1860+
unsigned int digest_size = _hashlib_hmac_digest_size(self);
18501861
if (digest_size == 0) {
18511862
return NULL;
18521863
}
@@ -1861,32 +1872,24 @@ static PyObject *
18611872
_hashlib_hmac_get_digest_size(PyObject *op, void *Py_UNUSED(closure))
18621873
{
18631874
HMACobject *self = HMACobject_CAST(op);
1864-
unsigned int digest_size = _hmac_digest_size(self);
1865-
if (digest_size == 0) {
1866-
return NULL;
1867-
}
1868-
return PyLong_FromLong(digest_size);
1875+
unsigned int digest_size = _hashlib_hmac_digest_size(self);
1876+
return digest_size == 0 ? NULL : PyLong_FromLong(digest_size);
18691877
}
18701878

18711879
static PyObject *
18721880
_hashlib_hmac_get_block_size(PyObject *op, void *Py_UNUSED(closure))
18731881
{
18741882
HMACobject *self = HMACobject_CAST(op);
1875-
const EVP_MD *md = HMAC_CTX_get_md(self->ctx);
1876-
if (md == NULL) {
1877-
notify_ssl_error_occurred();
1878-
return NULL;
1879-
}
1880-
return PyLong_FromLong(EVP_MD_block_size(md));
1883+
const EVP_MD *md = _hashlib_hmac_get_md(self);
1884+
return md == NULL ? NULL : PyLong_FromLong(EVP_MD_block_size(md));
18811885
}
18821886

18831887
static PyObject *
18841888
_hashlib_hmac_get_name(PyObject *op, void *Py_UNUSED(closure))
18851889
{
18861890
HMACobject *self = HMACobject_CAST(op);
1887-
const EVP_MD *md = HMAC_CTX_get_md(self->ctx);
1891+
const EVP_MD *md = _hashlib_hmac_get_md(self);
18881892
if (md == NULL) {
1889-
notify_ssl_error_occurred();
18901893
return NULL;
18911894
}
18921895
PyObject *digest_name = py_digest_name(md);

0 commit comments

Comments
 (0)