@@ -300,14 +300,29 @@ class _hashlib.HMAC "HMACobject *" "((_hashlibstate *)PyModule_GetState(module))
300300
301301/* LCOV_EXCL_START */
302302static PyObject *
303- format_openssl_error_code (PyObject * exc , unsigned long errcode )
303+ _setException (PyObject * exc , const char * altmsg , ... )
304304{
305- assert (errcode != 0 );
305+ unsigned long errcode = ERR_peek_last_error ();
306+ const char * lib , * func , * reason ;
307+ va_list vargs ;
308+
309+ va_start (vargs , altmsg );
310+ if (!errcode ) {
311+ if (altmsg == NULL ) {
312+ PyErr_SetString (exc , "no reason supplied" );
313+ } else {
314+ PyErr_FormatV (exc , altmsg , vargs );
315+ }
316+ va_end (vargs );
317+ return NULL ;
318+ }
319+ va_end (vargs );
320+ ERR_clear_error ();
306321
307322 /* ERR_ERROR_STRING(3) ensures that the messages below are ASCII */
308- const char * lib = ERR_lib_error_string (errcode );
309- const char * func = ERR_func_error_string (errcode );
310- const char * reason = ERR_reason_error_string (errcode );
323+ lib = ERR_lib_error_string (errcode );
324+ func = ERR_func_error_string (errcode );
325+ reason = ERR_reason_error_string (errcode );
311326
312327 if (lib && func ) {
313328 PyErr_Format (exc , "[%s: %s] %s" , lib , func , reason );
@@ -320,40 +335,6 @@ format_openssl_error_code(PyObject *exc, unsigned long errcode)
320335 }
321336 return NULL ;
322337}
323-
324- static PyObject *
325- format_openssl_exception (PyObject * exc , const char * alt_format , ...)
326- {
327- assert (alt_format != NULL );
328- unsigned long errcode = ERR_peek_last_error ();
329- if (!errcode ) {
330- va_list vargs ;
331- va_start (vargs , alt_format );
332- PyErr_FormatV (exc , alt_format , vargs );
333- va_end (vargs );
334- return NULL ;
335- }
336- ERR_clear_error ();
337- return format_openssl_error_code (exc , errcode );
338- }
339-
340- static void
341- set_openssl_exception (PyObject * exc , const char * alt_message )
342- {
343- unsigned long errcode = ERR_peek_last_error ();
344- if (!errcode ) {
345- PyErr_SetString (exc , alt_message );
346- return ;
347- }
348- ERR_clear_error ();
349- (void )format_openssl_error_code (exc , errcode );
350- }
351-
352- static inline void
353- set_simple_openssl_exception (void )
354- {
355- set_openssl_exception (PyExc_ValueError , "no reason supplied" );
356- }
357338/* LCOV_EXCL_STOP */
358339
359340static PyObject *
@@ -427,8 +408,8 @@ py_digest_by_name(PyObject *module, const char *name, enum Py_hash_type py_ht)
427408 }
428409 }
429410 if (digest == NULL ) {
430- format_openssl_exception (state -> unsupported_digestmod_error ,
431- "unsupported hash type %s" , name );
411+ _setException (state -> unsupported_digestmod_error ,
412+ "unsupported hash type %s" , name );
432413 return NULL ;
433414 }
434415 return digest ;
@@ -501,7 +482,7 @@ EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
501482 else
502483 process = Py_SAFE_DOWNCAST (len , Py_ssize_t , unsigned int );
503484 if (!EVP_DigestUpdate (self -> ctx , (const void * )cp , process )) {
504- set_simple_openssl_exception ( );
485+ _setException ( PyExc_ValueError , NULL );
505486 return -1 ;
506487 }
507488 len -= process ;
@@ -550,7 +531,7 @@ EVP_copy_impl(EVPobject *self)
550531
551532 if (!locked_EVP_MD_CTX_copy (newobj -> ctx , self )) {
552533 Py_DECREF (newobj );
553- set_simple_openssl_exception ( );
534+ _setException ( PyExc_ValueError , NULL );
554535 return NULL ;
555536 }
556537 return (PyObject * )newobj ;
@@ -591,7 +572,7 @@ EVP_digest_impl(EVPobject *self)
591572
592573error :
593574 EVP_MD_CTX_free (temp_ctx );
594- set_simple_openssl_exception ( );
575+ _setException ( PyExc_ValueError , NULL );
595576 return NULL ;
596577}
597578
@@ -630,7 +611,7 @@ EVP_hexdigest_impl(EVPobject *self)
630611
631612error :
632613 EVP_MD_CTX_free (temp_ctx );
633- set_simple_openssl_exception ( );
614+ _setException ( PyExc_ValueError , NULL );
634615 return NULL ;
635616}
636617
@@ -701,7 +682,7 @@ EVP_get_name(EVPobject *self, void *closure)
701682{
702683 const EVP_MD * md = EVP_MD_CTX_md (self -> ctx );
703684 if (md == NULL ) {
704- set_simple_openssl_exception ( );
685+ _setException ( PyExc_ValueError , NULL );
705686 return NULL ;
706687 }
707688 return py_digest_name (md );
@@ -816,7 +797,7 @@ EVPXOF_digest_impl(EVPobject *self, Py_ssize_t length)
816797error :
817798 Py_DECREF (retval );
818799 EVP_MD_CTX_free (temp_ctx );
819- set_simple_openssl_exception ( );
800+ _setException ( PyExc_ValueError , NULL );
820801 return NULL ;
821802}
822803
@@ -866,7 +847,7 @@ EVPXOF_hexdigest_impl(EVPobject *self, Py_ssize_t length)
866847error :
867848 PyMem_Free (digest );
868849 EVP_MD_CTX_free (temp_ctx );
869- set_simple_openssl_exception ( );
850+ _setException ( PyExc_ValueError , NULL );
870851 return NULL ;
871852}
872853
@@ -999,7 +980,7 @@ py_evp_fromname(PyObject *module, const char *digestname, PyObject *data_obj,
999980
1000981error :
1001982 Py_CLEAR (self );
1002- set_simple_openssl_exception ( );
983+ _setException ( PyExc_ValueError , NULL );
1003984 goto exit ;
1004985}
1005986
@@ -1361,7 +1342,7 @@ pbkdf2_hmac_impl(PyObject *module, const char *hash_name,
13611342
13621343 if (!retval ) {
13631344 Py_CLEAR (key_obj );
1364- set_simple_openssl_exception ( );
1345+ _setException ( PyExc_ValueError , NULL );
13651346 goto end ;
13661347 }
13671348
@@ -1467,8 +1448,8 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
14671448 /* let OpenSSL validate the rest */
14681449 retval = EVP_PBE_scrypt (NULL , 0 , NULL , 0 , n , r , p , maxmem , NULL , 0 );
14691450 if (!retval ) {
1470- set_openssl_exception (PyExc_ValueError ,
1471- "Invalid parameter combination for n, r, p, maxmem." );
1451+ _setException (PyExc_ValueError ,
1452+ "Invalid parameter combination for n, r, p, maxmem." );
14721453 return NULL ;
14731454 }
14741455
@@ -1489,7 +1470,7 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
14891470
14901471 if (!retval ) {
14911472 Py_CLEAR (key_obj );
1492- set_simple_openssl_exception ( );
1473+ _setException ( PyExc_ValueError , NULL );
14931474 return NULL ;
14941475 }
14951476 return key_obj ;
@@ -1546,7 +1527,7 @@ _hashlib_hmac_singleshot_impl(PyObject *module, Py_buffer *key,
15461527 PY_EVP_MD_free (evp );
15471528
15481529 if (result == NULL ) {
1549- set_simple_openssl_exception ( );
1530+ _setException ( PyExc_ValueError , NULL );
15501531 return NULL ;
15511532 }
15521533 return PyBytes_FromStringAndSize ((const char * )md , md_len );
@@ -1597,7 +1578,7 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
15971578 ctx = HMAC_CTX_new ();
15981579 if (ctx == NULL ) {
15991580 PY_EVP_MD_free (digest );
1600- set_simple_openssl_exception ( );
1581+ _setException ( PyExc_ValueError , NULL );
16011582 return NULL ;
16021583 }
16031584
@@ -1609,7 +1590,7 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
16091590 NULL /*impl*/ );
16101591 PY_EVP_MD_free (digest );
16111592 if (r == 0 ) {
1612- set_simple_openssl_exception ( );
1593+ _setException ( PyExc_ValueError , NULL );
16131594 goto error ;
16141595 }
16151596
@@ -1653,13 +1634,13 @@ _hmac_digest_size(HMACobject *self)
16531634{
16541635 const EVP_MD * md = HMAC_CTX_get_md (self -> ctx );
16551636 if (md == NULL ) {
1656- set_simple_openssl_exception ( );
1637+ _setException ( PyExc_ValueError , NULL );
16571638 return 0 ;
16581639 }
16591640 unsigned int digest_size = EVP_MD_size (md );
16601641 assert (digest_size <= EVP_MAX_MD_SIZE );
16611642 if (digest_size == 0 ) {
1662- set_simple_openssl_exception ( );
1643+ _setException ( PyExc_ValueError , NULL );
16631644 }
16641645 return digest_size ;
16651646}
@@ -1688,7 +1669,7 @@ _hmac_update(HMACobject *self, PyObject *obj)
16881669 PyBuffer_Release (& view );
16891670
16901671 if (r == 0 ) {
1691- set_simple_openssl_exception ( );
1672+ _setException ( PyExc_ValueError , NULL );
16921673 return 0 ;
16931674 }
16941675 return 1 ;
@@ -1708,12 +1689,12 @@ _hashlib_HMAC_copy_impl(HMACobject *self)
17081689
17091690 HMAC_CTX * ctx = HMAC_CTX_new ();
17101691 if (ctx == NULL ) {
1711- set_simple_openssl_exception ( );
1692+ _setException ( PyExc_ValueError , NULL );
17121693 return NULL ;
17131694 }
17141695 if (!locked_HMAC_CTX_copy (ctx , self )) {
17151696 HMAC_CTX_free (ctx );
1716- set_simple_openssl_exception ( );
1697+ _setException ( PyExc_ValueError , NULL );
17171698 return NULL ;
17181699 }
17191700
@@ -1742,7 +1723,7 @@ _hmac_repr(HMACobject *self)
17421723{
17431724 const EVP_MD * md = HMAC_CTX_get_md (self -> ctx );
17441725 if (md == NULL ) {
1745- set_simple_openssl_exception ( );
1726+ _setException ( PyExc_ValueError , NULL );
17461727 return NULL ;
17471728 }
17481729 PyObject * digest_name = py_digest_name (md );
@@ -1782,13 +1763,13 @@ _hmac_digest(HMACobject *self, unsigned char *buf, unsigned int len)
17821763 return 0 ;
17831764 }
17841765 if (!locked_HMAC_CTX_copy (temp_ctx , self )) {
1785- set_simple_openssl_exception ( );
1766+ _setException ( PyExc_ValueError , NULL );
17861767 return 0 ;
17871768 }
17881769 int r = HMAC_Final (temp_ctx , buf , & len );
17891770 HMAC_CTX_free (temp_ctx );
17901771 if (r == 0 ) {
1791- set_simple_openssl_exception ( );
1772+ _setException ( PyExc_ValueError , NULL );
17921773 return 0 ;
17931774 }
17941775 return 1 ;
@@ -1858,7 +1839,7 @@ _hashlib_hmac_get_block_size(HMACobject *self, void *closure)
18581839{
18591840 const EVP_MD * md = HMAC_CTX_get_md (self -> ctx );
18601841 if (md == NULL ) {
1861- set_simple_openssl_exception ( );
1842+ _setException ( PyExc_ValueError , NULL );
18621843 return NULL ;
18631844 }
18641845 return PyLong_FromLong (EVP_MD_block_size (md ));
@@ -1869,7 +1850,7 @@ _hashlib_hmac_get_name(HMACobject *self, void *closure)
18691850{
18701851 const EVP_MD * md = HMAC_CTX_get_md (self -> ctx );
18711852 if (md == NULL ) {
1872- set_simple_openssl_exception ( );
1853+ _setException ( PyExc_ValueError , NULL );
18731854 return NULL ;
18741855 }
18751856 PyObject * digest_name = py_digest_name (md );
@@ -2023,7 +2004,7 @@ _hashlib_get_fips_mode_impl(PyObject *module)
20232004 // But 0 is also a valid result value.
20242005 unsigned long errcode = ERR_peek_last_error ();
20252006 if (errcode ) {
2026- set_simple_openssl_exception ( );
2007+ _setException ( PyExc_ValueError , NULL );
20272008 return -1 ;
20282009 }
20292010 }
0 commit comments