Skip to content
Prev Previous commit
Next Next commit
Better error reporting
  • Loading branch information
tiran committed Jan 7, 2022
commit 1668f6692e7da0d90e7c71b8e962cdbb846457bf
86 changes: 43 additions & 43 deletions Modules/_hashopenssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,16 +265,26 @@ class _hashlib.HMAC "HMACobject *" "((_hashlibstate *)PyModule_GetState(module))

/* LCOV_EXCL_START */
static PyObject *
_setException(PyObject *exc)
_setException(PyObject *exc, const char* altmsg, ...)
{
unsigned long errcode;
unsigned long errcode = ERR_peek_last_error();
const char *lib, *func, *reason;
va_list vargs;

errcode = ERR_peek_last_error();
#ifdef HAVE_STDARG_PROTOTYPES
va_start(vargs, altmsg);
#else
va_start(vargs);
#endif
if (!errcode) {
PyErr_SetString(exc, "unknown reasons");
if (altmsg == NULL) {
PyErr_SetString(exc, "unknown reasons");
Comment thread
gpshead marked this conversation as resolved.
Outdated
} else {
PyErr_FormatV(exc, altmsg, vargs);
}
return NULL;
}
va_end(vargs);
ERR_clear_error();

lib = ERR_lib_error_string(errcode);
Expand Down Expand Up @@ -365,7 +375,7 @@ py_digest_by_name(PyObject *module, const char *name, enum Py_hash_type py_ht)
}
}
if (digest == NULL) {
PyErr_Format(PyExc_ValueError, "unsupported hash type %s", name);
_setException(PyExc_ValueError, "unsupported hash type %s", name);
return NULL;
}
return digest;
Expand Down Expand Up @@ -444,7 +454,7 @@ EVP_hash(EVPobject *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)) {
_setException(PyExc_ValueError);
_setException(PyExc_ValueError, NULL);
return -1;
}
len -= process;
Expand Down Expand Up @@ -495,7 +505,7 @@ EVP_copy_impl(EVPobject *self)

if (!locked_EVP_MD_CTX_copy(newobj->ctx, self)) {
Py_DECREF(newobj);
return _setException(PyExc_ValueError);
return _setException(PyExc_ValueError, NULL);
}
return (PyObject *)newobj;
}
Expand All @@ -522,11 +532,11 @@ EVP_digest_impl(EVPobject *self)
}

if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) {
return _setException(PyExc_ValueError);
return _setException(PyExc_ValueError, NULL);
}
digest_size = EVP_MD_CTX_size(temp_ctx);
if (!EVP_DigestFinal(temp_ctx, digest, NULL)) {
_setException(PyExc_ValueError);
_setException(PyExc_ValueError, NULL);
return NULL;
}

Expand Down Expand Up @@ -557,11 +567,11 @@ EVP_hexdigest_impl(EVPobject *self)

/* Get the raw (binary) digest value */
if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) {
return _setException(PyExc_ValueError);
return _setException(PyExc_ValueError, NULL);
}
digest_size = EVP_MD_CTX_size(temp_ctx);
if (!EVP_DigestFinal(temp_ctx, digest, NULL)) {
_setException(PyExc_ValueError);
_setException(PyExc_ValueError, NULL);
return NULL;
}

Expand Down Expand Up @@ -737,14 +747,14 @@ EVPXOF_digest_impl(EVPobject *self, Py_ssize_t length)
if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) {
Py_DECREF(retval);
EVP_MD_CTX_free(temp_ctx);
return _setException(PyExc_ValueError);
return _setException(PyExc_ValueError, NULL);
}
if (!EVP_DigestFinalXOF(temp_ctx,
(unsigned char*)PyBytes_AS_STRING(retval),
length)) {
Py_DECREF(retval);
EVP_MD_CTX_free(temp_ctx);
_setException(PyExc_ValueError);
_setException(PyExc_ValueError, NULL);
return NULL;
}

Expand Down Expand Up @@ -785,12 +795,12 @@ EVPXOF_hexdigest_impl(EVPobject *self, Py_ssize_t length)
if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) {
PyMem_Free(digest);
EVP_MD_CTX_free(temp_ctx);
return _setException(PyExc_ValueError);
return _setException(PyExc_ValueError, NULL);
}
if (!EVP_DigestFinalXOF(temp_ctx, digest, length)) {
PyMem_Free(digest);
EVP_MD_CTX_free(temp_ctx);
_setException(PyExc_ValueError);
_setException(PyExc_ValueError, NULL);
return NULL;
}

Expand Down Expand Up @@ -866,10 +876,7 @@ EVPnew(PyObject *module, PY_EVP_MD *digest,
EVPobject *self = NULL;
PyTypeObject *type = get_hashlib_state(module)->EVPtype;

if (!digest) {
PyErr_SetString(PyExc_ValueError, "unsupported hash type");
return NULL;
}
assert(digest != NULL);

#ifdef PY_OPENSSL_HAS_SHAKE
if ((EVP_MD_flags(digest) & EVP_MD_FLAG_XOF) == EVP_MD_FLAG_XOF) {
Expand All @@ -891,7 +898,7 @@ EVPnew(PyObject *module, PY_EVP_MD *digest,
result = EVP_DigestInit_ex(self->ctx, digest, NULL);
PY_EVP_MD_free(digest);
if (!result) {
_setException(PyExc_ValueError);
_setException(PyExc_ValueError, NULL);
Py_DECREF(self);
return NULL;
}
Expand Down Expand Up @@ -1299,7 +1306,7 @@ pbkdf2_hmac_impl(PyObject *module, const char *hash_name,

if (!retval) {
Py_CLEAR(key_obj);
_setException(PyExc_ValueError);
_setException(PyExc_ValueError, NULL);
goto end;
}

Expand Down Expand Up @@ -1405,14 +1412,7 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
/* let OpenSSL validate the rest */
retval = EVP_PBE_scrypt(NULL, 0, NULL, 0, n, r, p, maxmem, NULL, 0);
if (!retval) {
unsigned long errcode = ERR_peek_last_error();
if (errcode) {
_setException(PyExc_ValueError);
} else {
/* sorry, can't do much better */
PyErr_SetString(PyExc_ValueError,
"Invalid parameter combination for n, r, p, maxmem.");
}
_setException(PyExc_ValueError, "Invalid parameter combination for n, r, p, maxmem.");
return NULL;
}

Expand All @@ -1433,7 +1433,7 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,

if (!retval) {
Py_CLEAR(key_obj);
_setException(PyExc_ValueError);
_setException(PyExc_ValueError, NULL);
return NULL;
}
return key_obj;
Expand Down Expand Up @@ -1490,7 +1490,7 @@ _hashlib_hmac_singleshot_impl(PyObject *module, Py_buffer *key,
PY_EVP_MD_free(evp);

if (result == NULL) {
_setException(PyExc_ValueError);
_setException(PyExc_ValueError, NULL);
return NULL;
}
return PyBytes_FromStringAndSize((const char*)md, md_len);
Expand Down Expand Up @@ -1541,7 +1541,7 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,

ctx = HMAC_CTX_new();
if (ctx == NULL) {
_setException(PyExc_ValueError);
_setException(PyExc_ValueError, NULL);
goto error;
}

Expand All @@ -1553,7 +1553,7 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
NULL /*impl*/);
PY_EVP_MD_free(digest);
if (r == 0) {
_setException(PyExc_ValueError);
_setException(PyExc_ValueError, NULL);
goto error;
}

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

if (r == 0) {
_setException(PyExc_ValueError);
_setException(PyExc_ValueError, NULL);
return 0;
}
return 1;
Expand All @@ -1643,11 +1643,11 @@ _hashlib_HMAC_copy_impl(HMACobject *self)

HMAC_CTX *ctx = HMAC_CTX_new();
if (ctx == NULL) {
return _setException(PyExc_ValueError);
return _setException(PyExc_ValueError, NULL);
}
if (!locked_HMAC_CTX_copy(ctx, self)) {
HMAC_CTX_free(ctx);
return _setException(PyExc_ValueError);
return _setException(PyExc_ValueError, NULL);
}

retval = (HMACobject *)PyObject_New(HMACobject, Py_TYPE(self));
Expand Down Expand Up @@ -1713,13 +1713,13 @@ _hmac_digest(HMACobject *self, unsigned char *buf, unsigned int len)
return 0;
}
if (!locked_HMAC_CTX_copy(temp_ctx, self)) {
_setException(PyExc_ValueError);
_setException(PyExc_ValueError, NULL);
return 0;
}
int r = HMAC_Final(temp_ctx, buf, &len);
HMAC_CTX_free(temp_ctx);
if (r == 0) {
_setException(PyExc_ValueError);
_setException(PyExc_ValueError, NULL);
return 0;
}
return 1;
Expand All @@ -1737,7 +1737,7 @@ _hashlib_HMAC_digest_impl(HMACobject *self)
unsigned char digest[EVP_MAX_MD_SIZE];
unsigned int digest_size = _hmac_digest_size(self);
if (digest_size == 0) {
return _setException(PyExc_ValueError);
return _setException(PyExc_ValueError, NULL);
}
int r = _hmac_digest(self, digest, digest_size);
if (r == 0) {
Expand All @@ -1762,7 +1762,7 @@ _hashlib_HMAC_hexdigest_impl(HMACobject *self)
unsigned char digest[EVP_MAX_MD_SIZE];
unsigned int digest_size = _hmac_digest_size(self);
if (digest_size == 0) {
return _setException(PyExc_ValueError);
return _setException(PyExc_ValueError, NULL);
}
int r = _hmac_digest(self, digest, digest_size);
if (r == 0) {
Expand All @@ -1776,7 +1776,7 @@ _hashlib_hmac_get_digest_size(HMACobject *self, void *closure)
{
unsigned int digest_size = _hmac_digest_size(self);
if (digest_size == 0) {
return _setException(PyExc_ValueError);
return _setException(PyExc_ValueError, NULL);
}
return PyLong_FromLong(digest_size);
}
Expand All @@ -1786,7 +1786,7 @@ _hashlib_hmac_get_block_size(HMACobject *self, void *closure)
{
const EVP_MD *md = HMAC_CTX_get_md(self->ctx);
if (md == NULL) {
return _setException(PyExc_ValueError);
return _setException(PyExc_ValueError, NULL);
}
return PyLong_FromLong(EVP_MD_block_size(md));
}
Expand Down Expand Up @@ -1939,7 +1939,7 @@ _hashlib_get_fips_mode_impl(PyObject *module)
// But 0 is also a valid result value.
unsigned long errcode = ERR_peek_last_error();
if (errcode) {
_setException(PyExc_ValueError);
_setException(PyExc_ValueError, NULL);
return -1;
}
}
Expand Down