@@ -300,14 +300,29 @@ class _hashlib.HMAC "HMACobject *" "((_hashlibstate *)PyModule_GetState(module))
300
300
301
301
/* LCOV_EXCL_START */
302
302
static PyObject *
303
- format_openssl_error_code (PyObject * exc , unsigned long errcode )
303
+ _setException (PyObject * exc , const char * altmsg , ... )
304
304
{
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 ();
306
321
307
322
/* 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 );
311
326
312
327
if (lib && func ) {
313
328
PyErr_Format (exc , "[%s: %s] %s" , lib , func , reason );
@@ -320,40 +335,6 @@ format_openssl_error_code(PyObject *exc, unsigned long errcode)
320
335
}
321
336
return NULL ;
322
337
}
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
- }
357
338
/* LCOV_EXCL_STOP */
358
339
359
340
static PyObject *
@@ -427,8 +408,7 @@ py_digest_by_name(PyObject *module, const char *name, enum Py_hash_type py_ht)
427
408
}
428
409
}
429
410
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 );
432
412
return NULL ;
433
413
}
434
414
return digest ;
@@ -501,7 +481,7 @@ EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
501
481
else
502
482
process = Py_SAFE_DOWNCAST (len , Py_ssize_t , unsigned int );
503
483
if (!EVP_DigestUpdate (self -> ctx , (const void * )cp , process )) {
504
- set_simple_openssl_exception ( );
484
+ _setException ( PyExc_ValueError , NULL );
505
485
return -1 ;
506
486
}
507
487
len -= process ;
@@ -550,7 +530,7 @@ EVP_copy_impl(EVPobject *self)
550
530
551
531
if (!locked_EVP_MD_CTX_copy (newobj -> ctx , self )) {
552
532
Py_DECREF (newobj );
553
- set_simple_openssl_exception ( );
533
+ _setException ( PyExc_ValueError , NULL );
554
534
return NULL ;
555
535
}
556
536
return (PyObject * )newobj ;
@@ -591,7 +571,7 @@ EVP_digest_impl(EVPobject *self)
591
571
592
572
error :
593
573
EVP_MD_CTX_free (temp_ctx );
594
- set_simple_openssl_exception ( );
574
+ _setException ( PyExc_ValueError , NULL );
595
575
return NULL ;
596
576
}
597
577
@@ -630,7 +610,7 @@ EVP_hexdigest_impl(EVPobject *self)
630
610
631
611
error :
632
612
EVP_MD_CTX_free (temp_ctx );
633
- set_simple_openssl_exception ( );
613
+ _setException ( PyExc_ValueError , NULL );
634
614
return NULL ;
635
615
}
636
616
@@ -701,7 +681,7 @@ EVP_get_name(EVPobject *self, void *closure)
701
681
{
702
682
const EVP_MD * md = EVP_MD_CTX_md (self -> ctx );
703
683
if (md == NULL ) {
704
- set_simple_openssl_exception ( );
684
+ _setException ( PyExc_ValueError , NULL );
705
685
return NULL ;
706
686
}
707
687
return py_digest_name (md );
@@ -816,7 +796,7 @@ EVPXOF_digest_impl(EVPobject *self, Py_ssize_t length)
816
796
error :
817
797
Py_DECREF (retval );
818
798
EVP_MD_CTX_free (temp_ctx );
819
- set_simple_openssl_exception ( );
799
+ _setException ( PyExc_ValueError , NULL );
820
800
return NULL ;
821
801
}
822
802
@@ -866,7 +846,7 @@ EVPXOF_hexdigest_impl(EVPobject *self, Py_ssize_t length)
866
846
error :
867
847
PyMem_Free (digest );
868
848
EVP_MD_CTX_free (temp_ctx );
869
- set_simple_openssl_exception ( );
849
+ _setException ( PyExc_ValueError , NULL );
870
850
return NULL ;
871
851
}
872
852
@@ -999,7 +979,7 @@ py_evp_fromname(PyObject *module, const char *digestname, PyObject *data_obj,
999
979
1000
980
error :
1001
981
Py_CLEAR (self );
1002
- set_simple_openssl_exception ( );
982
+ _setException ( PyExc_ValueError , NULL );
1003
983
goto exit ;
1004
984
}
1005
985
@@ -1361,7 +1341,7 @@ pbkdf2_hmac_impl(PyObject *module, const char *hash_name,
1361
1341
1362
1342
if (!retval ) {
1363
1343
Py_CLEAR (key_obj );
1364
- set_simple_openssl_exception ( );
1344
+ _setException ( PyExc_ValueError , NULL );
1365
1345
goto end ;
1366
1346
}
1367
1347
@@ -1437,14 +1417,14 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
1437
1417
r = PyLong_AsUnsignedLong (r_obj );
1438
1418
if (r == (unsigned long ) -1 && PyErr_Occurred ()) {
1439
1419
PyErr_SetString (PyExc_TypeError ,
1440
- "r is required and must be an unsigned int" );
1420
+ "r is required and must be an unsigned int" );
1441
1421
return NULL ;
1442
1422
}
1443
1423
1444
1424
p = PyLong_AsUnsignedLong (p_obj );
1445
1425
if (p == (unsigned long ) -1 && PyErr_Occurred ()) {
1446
1426
PyErr_SetString (PyExc_TypeError ,
1447
- "p is required and must be an unsigned int" );
1427
+ "p is required and must be an unsigned int" );
1448
1428
return NULL ;
1449
1429
}
1450
1430
@@ -1453,22 +1433,21 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
1453
1433
future. The maxmem constant is private to OpenSSL. */
1454
1434
PyErr_Format (PyExc_ValueError ,
1455
1435
"maxmem must be positive and smaller than %d" ,
1456
- INT_MAX );
1436
+ INT_MAX );
1457
1437
return NULL ;
1458
1438
}
1459
1439
1460
1440
if (dklen < 1 || dklen > INT_MAX ) {
1461
1441
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 );
1464
1444
return NULL ;
1465
1445
}
1466
1446
1467
1447
/* let OpenSSL validate the rest */
1468
1448
retval = EVP_PBE_scrypt (NULL , 0 , NULL , 0 , n , r , p , maxmem , NULL , 0 );
1469
1449
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." );
1472
1451
return NULL ;
1473
1452
}
1474
1453
@@ -1489,7 +1468,7 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
1489
1468
1490
1469
if (!retval ) {
1491
1470
Py_CLEAR (key_obj );
1492
- set_simple_openssl_exception ( );
1471
+ _setException ( PyExc_ValueError , NULL );
1493
1472
return NULL ;
1494
1473
}
1495
1474
return key_obj ;
@@ -1546,7 +1525,7 @@ _hashlib_hmac_singleshot_impl(PyObject *module, Py_buffer *key,
1546
1525
PY_EVP_MD_free (evp );
1547
1526
1548
1527
if (result == NULL ) {
1549
- set_simple_openssl_exception ( );
1528
+ _setException ( PyExc_ValueError , NULL );
1550
1529
return NULL ;
1551
1530
}
1552
1531
return PyBytes_FromStringAndSize ((const char * )md , md_len );
@@ -1597,7 +1576,7 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
1597
1576
ctx = HMAC_CTX_new ();
1598
1577
if (ctx == NULL ) {
1599
1578
PY_EVP_MD_free (digest );
1600
- set_simple_openssl_exception ( );
1579
+ _setException ( PyExc_ValueError , NULL );
1601
1580
return NULL ;
1602
1581
}
1603
1582
@@ -1609,7 +1588,7 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
1609
1588
NULL /*impl*/ );
1610
1589
PY_EVP_MD_free (digest );
1611
1590
if (r == 0 ) {
1612
- set_simple_openssl_exception ( );
1591
+ _setException ( PyExc_ValueError , NULL );
1613
1592
goto error ;
1614
1593
}
1615
1594
@@ -1653,13 +1632,13 @@ _hmac_digest_size(HMACobject *self)
1653
1632
{
1654
1633
const EVP_MD * md = HMAC_CTX_get_md (self -> ctx );
1655
1634
if (md == NULL ) {
1656
- set_simple_openssl_exception ( );
1635
+ _setException ( PyExc_ValueError , NULL );
1657
1636
return 0 ;
1658
1637
}
1659
1638
unsigned int digest_size = EVP_MD_size (md );
1660
1639
assert (digest_size <= EVP_MAX_MD_SIZE );
1661
1640
if (digest_size == 0 ) {
1662
- set_simple_openssl_exception ( );
1641
+ _setException ( PyExc_ValueError , NULL );
1663
1642
}
1664
1643
return digest_size ;
1665
1644
}
@@ -1688,7 +1667,7 @@ _hmac_update(HMACobject *self, PyObject *obj)
1688
1667
PyBuffer_Release (& view );
1689
1668
1690
1669
if (r == 0 ) {
1691
- set_simple_openssl_exception ( );
1670
+ _setException ( PyExc_ValueError , NULL );
1692
1671
return 0 ;
1693
1672
}
1694
1673
return 1 ;
@@ -1708,12 +1687,12 @@ _hashlib_HMAC_copy_impl(HMACobject *self)
1708
1687
1709
1688
HMAC_CTX * ctx = HMAC_CTX_new ();
1710
1689
if (ctx == NULL ) {
1711
- set_simple_openssl_exception ( );
1690
+ _setException ( PyExc_ValueError , NULL );
1712
1691
return NULL ;
1713
1692
}
1714
1693
if (!locked_HMAC_CTX_copy (ctx , self )) {
1715
1694
HMAC_CTX_free (ctx );
1716
- set_simple_openssl_exception ( );
1695
+ _setException ( PyExc_ValueError , NULL );
1717
1696
return NULL ;
1718
1697
}
1719
1698
@@ -1742,7 +1721,7 @@ _hmac_repr(HMACobject *self)
1742
1721
{
1743
1722
const EVP_MD * md = HMAC_CTX_get_md (self -> ctx );
1744
1723
if (md == NULL ) {
1745
- set_simple_openssl_exception ( );
1724
+ _setException ( PyExc_ValueError , NULL );
1746
1725
return NULL ;
1747
1726
}
1748
1727
PyObject * digest_name = py_digest_name (md );
@@ -1782,13 +1761,13 @@ _hmac_digest(HMACobject *self, unsigned char *buf, unsigned int len)
1782
1761
return 0 ;
1783
1762
}
1784
1763
if (!locked_HMAC_CTX_copy (temp_ctx , self )) {
1785
- set_simple_openssl_exception ( );
1764
+ _setException ( PyExc_ValueError , NULL );
1786
1765
return 0 ;
1787
1766
}
1788
1767
int r = HMAC_Final (temp_ctx , buf , & len );
1789
1768
HMAC_CTX_free (temp_ctx );
1790
1769
if (r == 0 ) {
1791
- set_simple_openssl_exception ( );
1770
+ _setException ( PyExc_ValueError , NULL );
1792
1771
return 0 ;
1793
1772
}
1794
1773
return 1 ;
@@ -1858,7 +1837,7 @@ _hashlib_hmac_get_block_size(HMACobject *self, void *closure)
1858
1837
{
1859
1838
const EVP_MD * md = HMAC_CTX_get_md (self -> ctx );
1860
1839
if (md == NULL ) {
1861
- set_simple_openssl_exception ( );
1840
+ _setException ( PyExc_ValueError , NULL );
1862
1841
return NULL ;
1863
1842
}
1864
1843
return PyLong_FromLong (EVP_MD_block_size (md ));
@@ -1869,7 +1848,7 @@ _hashlib_hmac_get_name(HMACobject *self, void *closure)
1869
1848
{
1870
1849
const EVP_MD * md = HMAC_CTX_get_md (self -> ctx );
1871
1850
if (md == NULL ) {
1872
- set_simple_openssl_exception ( );
1851
+ _setException ( PyExc_ValueError , NULL );
1873
1852
return NULL ;
1874
1853
}
1875
1854
PyObject * digest_name = py_digest_name (md );
@@ -2023,7 +2002,7 @@ _hashlib_get_fips_mode_impl(PyObject *module)
2023
2002
// But 0 is also a valid result value.
2024
2003
unsigned long errcode = ERR_peek_last_error ();
2025
2004
if (errcode ) {
2026
- set_simple_openssl_exception ( );
2005
+ _setException ( PyExc_ValueError , NULL );
2027
2006
return -1 ;
2028
2007
}
2029
2008
}
0 commit comments