@@ -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,7 @@ 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 , "unsupported hash type %s" , name );
432412 return NULL ;
433413 }
434414 return digest ;
@@ -501,7 +481,7 @@ EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
501481 else
502482 process = Py_SAFE_DOWNCAST (len , Py_ssize_t , unsigned int );
503483 if (!EVP_DigestUpdate (self -> ctx , (const void * )cp , process )) {
504- set_simple_openssl_exception ( );
484+ _setException ( PyExc_ValueError , NULL );
505485 return -1 ;
506486 }
507487 len -= process ;
@@ -550,7 +530,7 @@ EVP_copy_impl(EVPobject *self)
550530
551531 if (!locked_EVP_MD_CTX_copy (newobj -> ctx , self )) {
552532 Py_DECREF (newobj );
553- set_simple_openssl_exception ( );
533+ _setException ( PyExc_ValueError , NULL );
554534 return NULL ;
555535 }
556536 return (PyObject * )newobj ;
@@ -591,7 +571,7 @@ EVP_digest_impl(EVPobject *self)
591571
592572error :
593573 EVP_MD_CTX_free (temp_ctx );
594- set_simple_openssl_exception ( );
574+ _setException ( PyExc_ValueError , NULL );
595575 return NULL ;
596576}
597577
@@ -630,7 +610,7 @@ EVP_hexdigest_impl(EVPobject *self)
630610
631611error :
632612 EVP_MD_CTX_free (temp_ctx );
633- set_simple_openssl_exception ( );
613+ _setException ( PyExc_ValueError , NULL );
634614 return NULL ;
635615}
636616
@@ -701,7 +681,7 @@ EVP_get_name(EVPobject *self, void *closure)
701681{
702682 const EVP_MD * md = EVP_MD_CTX_md (self -> ctx );
703683 if (md == NULL ) {
704- set_simple_openssl_exception ( );
684+ _setException ( PyExc_ValueError , NULL );
705685 return NULL ;
706686 }
707687 return py_digest_name (md );
@@ -816,7 +796,7 @@ EVPXOF_digest_impl(EVPobject *self, Py_ssize_t length)
816796error :
817797 Py_DECREF (retval );
818798 EVP_MD_CTX_free (temp_ctx );
819- set_simple_openssl_exception ( );
799+ _setException ( PyExc_ValueError , NULL );
820800 return NULL ;
821801}
822802
@@ -866,7 +846,7 @@ EVPXOF_hexdigest_impl(EVPobject *self, Py_ssize_t length)
866846error :
867847 PyMem_Free (digest );
868848 EVP_MD_CTX_free (temp_ctx );
869- set_simple_openssl_exception ( );
849+ _setException ( PyExc_ValueError , NULL );
870850 return NULL ;
871851}
872852
@@ -999,7 +979,7 @@ py_evp_fromname(PyObject *module, const char *digestname, PyObject *data_obj,
999979
1000980error :
1001981 Py_CLEAR (self );
1002- set_simple_openssl_exception ( );
982+ _setException ( PyExc_ValueError , NULL );
1003983 goto exit ;
1004984}
1005985
@@ -1361,7 +1341,7 @@ pbkdf2_hmac_impl(PyObject *module, const char *hash_name,
13611341
13621342 if (!retval ) {
13631343 Py_CLEAR (key_obj );
1364- set_simple_openssl_exception ( );
1344+ _setException ( PyExc_ValueError , NULL );
13651345 goto end ;
13661346 }
13671347
@@ -1437,14 +1417,14 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
14371417 r = PyLong_AsUnsignedLong (r_obj );
14381418 if (r == (unsigned long ) -1 && PyErr_Occurred ()) {
14391419 PyErr_SetString (PyExc_TypeError ,
1440- "r is required and must be an unsigned int" );
1420+ "r is required and must be an unsigned int" );
14411421 return NULL ;
14421422 }
14431423
14441424 p = PyLong_AsUnsignedLong (p_obj );
14451425 if (p == (unsigned long ) -1 && PyErr_Occurred ()) {
14461426 PyErr_SetString (PyExc_TypeError ,
1447- "p is required and must be an unsigned int" );
1427+ "p is required and must be an unsigned int" );
14481428 return NULL ;
14491429 }
14501430
@@ -1453,22 +1433,21 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
14531433 future. The maxmem constant is private to OpenSSL. */
14541434 PyErr_Format (PyExc_ValueError ,
14551435 "maxmem must be positive and smaller than %d" ,
1456- INT_MAX );
1436+ INT_MAX );
14571437 return NULL ;
14581438 }
14591439
14601440 if (dklen < 1 || dklen > INT_MAX ) {
14611441 PyErr_Format (PyExc_ValueError ,
1462- "dklen must be greater than 0 and smaller than %d" ,
1463- INT_MAX );
1442+ "dklen must be greater than 0 and smaller than %d" ,
1443+ INT_MAX );
14641444 return NULL ;
14651445 }
14661446
14671447 /* let OpenSSL validate the rest */
14681448 retval = EVP_PBE_scrypt (NULL , 0 , NULL , 0 , n , r , p , maxmem , NULL , 0 );
14691449 if (!retval ) {
1470- set_openssl_exception (PyExc_ValueError ,
1471- "Invalid parameter combination for n, r, p, maxmem." );
1450+ _setException (PyExc_ValueError , "Invalid parameter combination for n, r, p, maxmem." );
14721451 return NULL ;
14731452 }
14741453
@@ -1489,7 +1468,7 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
14891468
14901469 if (!retval ) {
14911470 Py_CLEAR (key_obj );
1492- set_simple_openssl_exception ( );
1471+ _setException ( PyExc_ValueError , NULL );
14931472 return NULL ;
14941473 }
14951474 return key_obj ;
@@ -1546,7 +1525,7 @@ _hashlib_hmac_singleshot_impl(PyObject *module, Py_buffer *key,
15461525 PY_EVP_MD_free (evp );
15471526
15481527 if (result == NULL ) {
1549- set_simple_openssl_exception ( );
1528+ _setException ( PyExc_ValueError , NULL );
15501529 return NULL ;
15511530 }
15521531 return PyBytes_FromStringAndSize ((const char * )md , md_len );
@@ -1597,7 +1576,7 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
15971576 ctx = HMAC_CTX_new ();
15981577 if (ctx == NULL ) {
15991578 PY_EVP_MD_free (digest );
1600- set_simple_openssl_exception ( );
1579+ _setException ( PyExc_ValueError , NULL );
16011580 return NULL ;
16021581 }
16031582
@@ -1609,7 +1588,7 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
16091588 NULL /*impl*/ );
16101589 PY_EVP_MD_free (digest );
16111590 if (r == 0 ) {
1612- set_simple_openssl_exception ( );
1591+ _setException ( PyExc_ValueError , NULL );
16131592 goto error ;
16141593 }
16151594
@@ -1653,13 +1632,13 @@ _hmac_digest_size(HMACobject *self)
16531632{
16541633 const EVP_MD * md = HMAC_CTX_get_md (self -> ctx );
16551634 if (md == NULL ) {
1656- set_simple_openssl_exception ( );
1635+ _setException ( PyExc_ValueError , NULL );
16571636 return 0 ;
16581637 }
16591638 unsigned int digest_size = EVP_MD_size (md );
16601639 assert (digest_size <= EVP_MAX_MD_SIZE );
16611640 if (digest_size == 0 ) {
1662- set_simple_openssl_exception ( );
1641+ _setException ( PyExc_ValueError , NULL );
16631642 }
16641643 return digest_size ;
16651644}
@@ -1688,7 +1667,7 @@ _hmac_update(HMACobject *self, PyObject *obj)
16881667 PyBuffer_Release (& view );
16891668
16901669 if (r == 0 ) {
1691- set_simple_openssl_exception ( );
1670+ _setException ( PyExc_ValueError , NULL );
16921671 return 0 ;
16931672 }
16941673 return 1 ;
@@ -1708,12 +1687,12 @@ _hashlib_HMAC_copy_impl(HMACobject *self)
17081687
17091688 HMAC_CTX * ctx = HMAC_CTX_new ();
17101689 if (ctx == NULL ) {
1711- set_simple_openssl_exception ( );
1690+ _setException ( PyExc_ValueError , NULL );
17121691 return NULL ;
17131692 }
17141693 if (!locked_HMAC_CTX_copy (ctx , self )) {
17151694 HMAC_CTX_free (ctx );
1716- set_simple_openssl_exception ( );
1695+ _setException ( PyExc_ValueError , NULL );
17171696 return NULL ;
17181697 }
17191698
@@ -1742,7 +1721,7 @@ _hmac_repr(HMACobject *self)
17421721{
17431722 const EVP_MD * md = HMAC_CTX_get_md (self -> ctx );
17441723 if (md == NULL ) {
1745- set_simple_openssl_exception ( );
1724+ _setException ( PyExc_ValueError , NULL );
17461725 return NULL ;
17471726 }
17481727 PyObject * digest_name = py_digest_name (md );
@@ -1782,13 +1761,13 @@ _hmac_digest(HMACobject *self, unsigned char *buf, unsigned int len)
17821761 return 0 ;
17831762 }
17841763 if (!locked_HMAC_CTX_copy (temp_ctx , self )) {
1785- set_simple_openssl_exception ( );
1764+ _setException ( PyExc_ValueError , NULL );
17861765 return 0 ;
17871766 }
17881767 int r = HMAC_Final (temp_ctx , buf , & len );
17891768 HMAC_CTX_free (temp_ctx );
17901769 if (r == 0 ) {
1791- set_simple_openssl_exception ( );
1770+ _setException ( PyExc_ValueError , NULL );
17921771 return 0 ;
17931772 }
17941773 return 1 ;
@@ -1858,7 +1837,7 @@ _hashlib_hmac_get_block_size(HMACobject *self, void *closure)
18581837{
18591838 const EVP_MD * md = HMAC_CTX_get_md (self -> ctx );
18601839 if (md == NULL ) {
1861- set_simple_openssl_exception ( );
1840+ _setException ( PyExc_ValueError , NULL );
18621841 return NULL ;
18631842 }
18641843 return PyLong_FromLong (EVP_MD_block_size (md ));
@@ -1869,7 +1848,7 @@ _hashlib_hmac_get_name(HMACobject *self, void *closure)
18691848{
18701849 const EVP_MD * md = HMAC_CTX_get_md (self -> ctx );
18711850 if (md == NULL ) {
1872- set_simple_openssl_exception ( );
1851+ _setException ( PyExc_ValueError , NULL );
18731852 return NULL ;
18741853 }
18751854 PyObject * digest_name = py_digest_name (md );
@@ -2023,7 +2002,7 @@ _hashlib_get_fips_mode_impl(PyObject *module)
20232002 // But 0 is also a valid result value.
20242003 unsigned long errcode = ERR_peek_last_error ();
20252004 if (errcode ) {
2026- set_simple_openssl_exception ( );
2005+ _setException ( PyExc_ValueError , NULL );
20272006 return -1 ;
20282007 }
20292008 }
0 commit comments