@@ -430,7 +430,8 @@ py_digest_by_name(PyObject *module, const char *name, enum Py_hash_type py_ht)
430
430
}
431
431
}
432
432
if (digest == NULL ) {
433
- _setException (state -> unsupported_digestmod_error , "unsupported hash type %s" , name );
433
+ (void )_setException (state -> unsupported_digestmod_error ,
434
+ "unsupported hash type %s" , name );
434
435
return NULL ;
435
436
}
436
437
return digest ;
@@ -445,7 +446,6 @@ py_digest_by_name(PyObject *module, const char *name, enum Py_hash_type py_ht)
445
446
*/
446
447
static PY_EVP_MD *
447
448
py_digest_by_digestmod (PyObject * module , PyObject * digestmod , enum Py_hash_type py_ht ) {
448
- PY_EVP_MD * evp ;
449
449
PyObject * name_obj = NULL ;
450
450
const char * name ;
451
451
@@ -471,12 +471,7 @@ py_digest_by_digestmod(PyObject *module, PyObject *digestmod, enum Py_hash_type
471
471
return NULL ;
472
472
}
473
473
474
- evp = py_digest_by_name (module , name , py_ht );
475
- if (evp == NULL ) {
476
- return NULL ;
477
- }
478
-
479
- return evp ;
474
+ return py_digest_by_name (module , name , py_ht );
480
475
}
481
476
482
477
static EVPobject *
@@ -509,7 +504,7 @@ EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
509
504
else
510
505
process = Py_SAFE_DOWNCAST (len , Py_ssize_t , unsigned int );
511
506
if (!EVP_DigestUpdate (self -> ctx , (const void * )cp , process )) {
512
- _setException (PyExc_ValueError , NULL );
507
+ ( void ) _setException (PyExc_ValueError , NULL );
513
508
return -1 ;
514
509
}
515
510
len -= process ;
@@ -586,17 +581,20 @@ EVP_digest_impl(EVPobject *self)
586
581
}
587
582
588
583
if (!locked_EVP_MD_CTX_copy (temp_ctx , self )) {
589
- return _setException ( PyExc_ValueError , NULL ) ;
584
+ goto error ;
590
585
}
591
586
digest_size = EVP_MD_CTX_size (temp_ctx );
592
587
if (!EVP_DigestFinal (temp_ctx , digest , NULL )) {
593
- _setException (PyExc_ValueError , NULL );
594
- return NULL ;
588
+ goto error ;
595
589
}
596
590
597
591
retval = PyBytes_FromStringAndSize ((const char * )digest , digest_size );
598
592
EVP_MD_CTX_free (temp_ctx );
599
593
return retval ;
594
+
595
+ error :
596
+ EVP_MD_CTX_free (temp_ctx );
597
+ return _setException (PyExc_ValueError , NULL );
600
598
}
601
599
602
600
/*[clinic input]
@@ -621,17 +619,20 @@ EVP_hexdigest_impl(EVPobject *self)
621
619
622
620
/* Get the raw (binary) digest value */
623
621
if (!locked_EVP_MD_CTX_copy (temp_ctx , self )) {
624
- return _setException ( PyExc_ValueError , NULL ) ;
622
+ goto error ;
625
623
}
626
624
digest_size = EVP_MD_CTX_size (temp_ctx );
627
625
if (!EVP_DigestFinal (temp_ctx , digest , NULL )) {
628
- _setException (PyExc_ValueError , NULL );
629
- return NULL ;
626
+ goto error ;
630
627
}
631
628
632
629
EVP_MD_CTX_free (temp_ctx );
633
630
634
631
return _Py_strhex ((const char * )digest , (Py_ssize_t )digest_size );
632
+
633
+ error :
634
+ EVP_MD_CTX_free (temp_ctx );
635
+ return _setException (PyExc_ValueError , NULL );
635
636
}
636
637
637
638
/*[clinic input]
@@ -700,8 +701,11 @@ static PyObject *
700
701
EVP_get_name (PyObject * op , void * Py_UNUSED (closure ))
701
702
{
702
703
EVPobject * self = EVPobject_CAST (op );
703
- // NOTE(picnixz): NULL EVP context will be handled by gh-127667.
704
- return py_digest_name (EVP_MD_CTX_md (self -> ctx ));
704
+ const EVP_MD * md = EVP_MD_CTX_md (self -> ctx );
705
+ if (md == NULL ) {
706
+ return _setException (PyExc_ValueError , NULL );
707
+ }
708
+ return py_digest_name (md );
705
709
}
706
710
707
711
static PyGetSetDef EVP_getseters [] = {
@@ -789,21 +793,22 @@ EVPXOF_digest_impl(EVPobject *self, Py_ssize_t length)
789
793
}
790
794
791
795
if (!locked_EVP_MD_CTX_copy (temp_ctx , self )) {
792
- Py_DECREF (retval );
793
- EVP_MD_CTX_free (temp_ctx );
794
- return _setException (PyExc_ValueError , NULL );
796
+ goto error ;
795
797
}
796
798
if (!EVP_DigestFinalXOF (temp_ctx ,
797
799
(unsigned char * )PyBytes_AS_STRING (retval ),
798
- length )) {
799
- Py_DECREF (retval );
800
- EVP_MD_CTX_free (temp_ctx );
801
- _setException (PyExc_ValueError , NULL );
802
- return NULL ;
800
+ length ))
801
+ {
802
+ goto error ;
803
803
}
804
804
805
805
EVP_MD_CTX_free (temp_ctx );
806
806
return retval ;
807
+
808
+ error :
809
+ Py_DECREF (retval );
810
+ EVP_MD_CTX_free (temp_ctx );
811
+ return _setException (PyExc_ValueError , NULL );
807
812
}
808
813
809
814
/*[clinic input]
@@ -837,22 +842,22 @@ EVPXOF_hexdigest_impl(EVPobject *self, Py_ssize_t length)
837
842
838
843
/* Get the raw (binary) digest value */
839
844
if (!locked_EVP_MD_CTX_copy (temp_ctx , self )) {
840
- PyMem_Free (digest );
841
- EVP_MD_CTX_free (temp_ctx );
842
- return _setException (PyExc_ValueError , NULL );
845
+ goto error ;
843
846
}
844
847
if (!EVP_DigestFinalXOF (temp_ctx , digest , length )) {
845
- PyMem_Free (digest );
846
- EVP_MD_CTX_free (temp_ctx );
847
- _setException (PyExc_ValueError , NULL );
848
- return NULL ;
848
+ goto error ;
849
849
}
850
850
851
851
EVP_MD_CTX_free (temp_ctx );
852
852
853
853
retval = _Py_strhex ((const char * )digest , length );
854
854
PyMem_Free (digest );
855
855
return retval ;
856
+
857
+ error :
858
+ PyMem_Free (digest );
859
+ EVP_MD_CTX_free (temp_ctx );
860
+ return _setException (PyExc_ValueError , NULL );
856
861
}
857
862
858
863
static PyMethodDef EVPXOF_methods [] = {
@@ -950,7 +955,7 @@ py_evp_fromname(PyObject *module, const char *digestname, PyObject *data_obj,
950
955
951
956
int result = EVP_DigestInit_ex (self -> ctx , digest , NULL );
952
957
if (!result ) {
953
- _setException (PyExc_ValueError , NULL );
958
+ ( void ) _setException (PyExc_ValueError , NULL );
954
959
Py_CLEAR (self );
955
960
goto exit ;
956
961
}
@@ -971,7 +976,7 @@ py_evp_fromname(PyObject *module, const char *digestname, PyObject *data_obj,
971
976
}
972
977
}
973
978
974
- exit :
979
+ exit :
975
980
if (data_obj != NULL ) {
976
981
PyBuffer_Release (& view );
977
982
}
@@ -1416,14 +1421,14 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
1416
1421
r = PyLong_AsUnsignedLong (r_obj );
1417
1422
if (r == (unsigned long ) -1 && PyErr_Occurred ()) {
1418
1423
PyErr_SetString (PyExc_TypeError ,
1419
- "r is required and must be an unsigned int" );
1424
+ "r is required and must be an unsigned int" );
1420
1425
return NULL ;
1421
1426
}
1422
1427
1423
1428
p = PyLong_AsUnsignedLong (p_obj );
1424
1429
if (p == (unsigned long ) -1 && PyErr_Occurred ()) {
1425
1430
PyErr_SetString (PyExc_TypeError ,
1426
- "p is required and must be an unsigned int" );
1431
+ "p is required and must be an unsigned int" );
1427
1432
return NULL ;
1428
1433
}
1429
1434
@@ -1432,22 +1437,22 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
1432
1437
future. The maxmem constant is private to OpenSSL. */
1433
1438
PyErr_Format (PyExc_ValueError ,
1434
1439
"maxmem must be positive and smaller than %d" ,
1435
- INT_MAX );
1440
+ INT_MAX );
1436
1441
return NULL ;
1437
1442
}
1438
1443
1439
1444
if (dklen < 1 || dklen > INT_MAX ) {
1440
1445
PyErr_Format (PyExc_ValueError ,
1441
- "dklen must be greater than 0 and smaller than %d" ,
1442
- INT_MAX );
1446
+ "dklen must be greater than 0 and smaller than %d" ,
1447
+ INT_MAX );
1443
1448
return NULL ;
1444
1449
}
1445
1450
1446
1451
/* let OpenSSL validate the rest */
1447
1452
retval = EVP_PBE_scrypt (NULL , 0 , NULL , 0 , n , r , p , maxmem , NULL , 0 );
1448
1453
if (!retval ) {
1449
- _setException (PyExc_ValueError , "Invalid parameter combination for n, r, p, maxmem." );
1450
- return NULL ;
1454
+ return _setException (PyExc_ValueError ,
1455
+ "Invalid parameter combination for n, r, p, maxmem." ) ;
1451
1456
}
1452
1457
1453
1458
key_obj = PyBytes_FromStringAndSize (NULL , dklen );
@@ -1467,8 +1472,7 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
1467
1472
1468
1473
if (!retval ) {
1469
1474
Py_CLEAR (key_obj );
1470
- _setException (PyExc_ValueError , NULL );
1471
- return NULL ;
1475
+ return _setException (PyExc_ValueError , NULL );
1472
1476
}
1473
1477
return key_obj ;
1474
1478
}
@@ -1524,8 +1528,7 @@ _hashlib_hmac_singleshot_impl(PyObject *module, Py_buffer *key,
1524
1528
PY_EVP_MD_free (evp );
1525
1529
1526
1530
if (result == NULL ) {
1527
- _setException (PyExc_ValueError , NULL );
1528
- return NULL ;
1531
+ return _setException (PyExc_ValueError , NULL );
1529
1532
}
1530
1533
return PyBytes_FromStringAndSize ((const char * )md , md_len );
1531
1534
}
@@ -1574,14 +1577,15 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
1574
1577
1575
1578
ctx = HMAC_CTX_new ();
1576
1579
if (ctx == NULL ) {
1580
+ PY_EVP_MD_free (digest );
1577
1581
PyErr_NoMemory ();
1578
1582
goto error ;
1579
1583
}
1580
1584
1581
1585
r = HMAC_Init_ex (ctx , key -> buf , (int )key -> len , digest , NULL /* impl */ );
1582
1586
PY_EVP_MD_free (digest );
1583
1587
if (r == 0 ) {
1584
- _setException (PyExc_ValueError , NULL );
1588
+ ( void ) _setException (PyExc_ValueError , NULL );
1585
1589
goto error ;
1586
1590
}
1587
1591
@@ -1619,12 +1623,20 @@ locked_HMAC_CTX_copy(HMAC_CTX *new_ctx_p, HMACobject *self)
1619
1623
return result ;
1620
1624
}
1621
1625
1626
+ /* returning 0 means that an error occurred and an exception is set */
1622
1627
static unsigned int
1623
1628
_hmac_digest_size (HMACobject * self )
1624
1629
{
1625
- // TODO(picnixz): NULL EVP context should also handled by gh-127667.
1626
- unsigned int digest_size = EVP_MD_size (HMAC_CTX_get_md (self -> ctx ));
1630
+ const EVP_MD * md = HMAC_CTX_get_md (self -> ctx );
1631
+ if (md == NULL ) {
1632
+ (void )_setException (PyExc_ValueError , NULL );
1633
+ return 0 ;
1634
+ }
1635
+ unsigned int digest_size = EVP_MD_size (md );
1627
1636
assert (digest_size <= EVP_MAX_MD_SIZE );
1637
+ if (digest_size == 0 ) {
1638
+ (void )_setException (PyExc_ValueError , NULL );
1639
+ }
1628
1640
return digest_size ;
1629
1641
}
1630
1642
@@ -1652,7 +1664,7 @@ _hmac_update(HMACobject *self, PyObject *obj)
1652
1664
PyBuffer_Release (& view );
1653
1665
1654
1666
if (r == 0 ) {
1655
- _setException (PyExc_ValueError , NULL );
1667
+ ( void ) _setException (PyExc_ValueError , NULL );
1656
1668
return 0 ;
1657
1669
}
1658
1670
return 1 ;
@@ -1707,7 +1719,11 @@ static PyObject *
1707
1719
_hmac_repr (PyObject * op )
1708
1720
{
1709
1721
HMACobject * self = HMACobject_CAST (op );
1710
- PyObject * digest_name = py_digest_name (HMAC_CTX_get_md (self -> ctx ));
1722
+ const EVP_MD * md = HMAC_CTX_get_md (self -> ctx );
1723
+ if (md == NULL ) {
1724
+ return _setException (PyExc_ValueError , NULL );
1725
+ }
1726
+ PyObject * digest_name = py_digest_name (md );
1711
1727
if (digest_name == NULL ) {
1712
1728
return NULL ;
1713
1729
}
@@ -1740,18 +1756,18 @@ _hmac_digest(HMACobject *self, unsigned char *buf, unsigned int len)
1740
1756
{
1741
1757
HMAC_CTX * temp_ctx = HMAC_CTX_new ();
1742
1758
if (temp_ctx == NULL ) {
1743
- PyErr_NoMemory ();
1759
+ ( void ) PyErr_NoMemory ();
1744
1760
return 0 ;
1745
1761
}
1746
1762
if (!locked_HMAC_CTX_copy (temp_ctx , self )) {
1747
1763
HMAC_CTX_free (temp_ctx );
1748
- _setException (PyExc_ValueError , NULL );
1764
+ ( void ) _setException (PyExc_ValueError , NULL );
1749
1765
return 0 ;
1750
1766
}
1751
1767
int r = HMAC_Final (temp_ctx , buf , & len );
1752
1768
HMAC_CTX_free (temp_ctx );
1753
1769
if (r == 0 ) {
1754
- _setException (PyExc_ValueError , NULL );
1770
+ ( void ) _setException (PyExc_ValueError , NULL );
1755
1771
return 0 ;
1756
1772
}
1757
1773
return 1 ;
@@ -1769,7 +1785,7 @@ _hashlib_HMAC_digest_impl(HMACobject *self)
1769
1785
unsigned char digest [EVP_MAX_MD_SIZE ];
1770
1786
unsigned int digest_size = _hmac_digest_size (self );
1771
1787
if (digest_size == 0 ) {
1772
- return _setException ( PyExc_ValueError , NULL ) ;
1788
+ return NULL ;
1773
1789
}
1774
1790
int r = _hmac_digest (self , digest , digest_size );
1775
1791
if (r == 0 ) {
@@ -1794,7 +1810,7 @@ _hashlib_HMAC_hexdigest_impl(HMACobject *self)
1794
1810
unsigned char digest [EVP_MAX_MD_SIZE ];
1795
1811
unsigned int digest_size = _hmac_digest_size (self );
1796
1812
if (digest_size == 0 ) {
1797
- return _setException ( PyExc_ValueError , NULL ) ;
1813
+ return NULL ;
1798
1814
}
1799
1815
int r = _hmac_digest (self , digest , digest_size );
1800
1816
if (r == 0 ) {
@@ -1809,7 +1825,7 @@ _hashlib_hmac_get_digest_size(PyObject *op, void *Py_UNUSED(closure))
1809
1825
HMACobject * self = HMACobject_CAST (op );
1810
1826
unsigned int digest_size = _hmac_digest_size (self );
1811
1827
if (digest_size == 0 ) {
1812
- return _setException ( PyExc_ValueError , NULL ) ;
1828
+ return NULL ;
1813
1829
}
1814
1830
return PyLong_FromLong (digest_size );
1815
1831
}
@@ -1829,7 +1845,11 @@ static PyObject *
1829
1845
_hashlib_hmac_get_name (PyObject * op , void * Py_UNUSED (closure ))
1830
1846
{
1831
1847
HMACobject * self = HMACobject_CAST (op );
1832
- PyObject * digest_name = py_digest_name (HMAC_CTX_get_md (self -> ctx ));
1848
+ const EVP_MD * md = HMAC_CTX_get_md (self -> ctx );
1849
+ if (md == NULL ) {
1850
+ return _setException (PyExc_ValueError , NULL );
1851
+ }
1852
+ PyObject * digest_name = py_digest_name (md );
1833
1853
if (digest_name == NULL ) {
1834
1854
return NULL ;
1835
1855
}
@@ -1980,7 +2000,7 @@ _hashlib_get_fips_mode_impl(PyObject *module)
1980
2000
// But 0 is also a valid result value.
1981
2001
unsigned long errcode = ERR_peek_last_error ();
1982
2002
if (errcode ) {
1983
- _setException (PyExc_ValueError , NULL );
2003
+ ( void ) _setException (PyExc_ValueError , NULL );
1984
2004
return -1 ;
1985
2005
}
1986
2006
}
0 commit comments