@@ -1777,6 +1777,36 @@ static int wolfssl_pk_verify_rsa_pss(const gnutls_datum_t *vdata,
17771777 return 0 ;
17781778}
17791779
1780+ /**
1781+ * Parse DER length field.
1782+ *
1783+ * @param [in] sig_data Signature data.
1784+ * @param [in] sig_len Signature data length.
1785+ * @param [in,out] idx Current index (updated on success).
1786+ * @param [out] len Parsed length value.
1787+ * @return 0 on success.
1788+ * @return Negative on parsing error.
1789+ */
1790+ static int parse_der_length (const byte * sig_data , word32 sig_len ,
1791+ word32 * idx , word32 * len )
1792+ {
1793+ if (* idx >= sig_len ) return -1 ;
1794+
1795+ * len = sig_data [(* idx )++ ];
1796+
1797+ if (* len & 0x80 ) {
1798+ /* Long form length */
1799+ word32 num_bytes = * len & 0x7F ;
1800+ if (num_bytes > 4 || * idx + num_bytes > sig_len ) return -1 ;
1801+ * len = 0 ;
1802+ while (num_bytes -- ) {
1803+ * len = (* len << 8 ) | sig_data [(* idx )++ ];
1804+ }
1805+ }
1806+
1807+ return 0 ;
1808+ }
1809+
17801810/**
17811811 * Parse a lenient DER-encoded ECDSA signature to extract r and s.
17821812 *
@@ -1803,17 +1833,7 @@ static int parse_lenient_der_ecdsa_signature(const byte* sig_data, word32 sig_le
18031833 }
18041834
18051835 /* Parse SEQUENCE length */
1806- if (idx >= sig_len ) return -1 ;
1807- len = sig_data [idx ++ ];
1808- if (len & 0x80 ) {
1809- /* Long form length */
1810- word32 num_bytes = len & 0x7F ;
1811- if (num_bytes > 4 || idx + num_bytes > sig_len ) return -1 ;
1812- len = 0 ;
1813- while (num_bytes -- ) {
1814- len = (len << 8 ) | sig_data [idx ++ ];
1815- }
1816- }
1836+ if (parse_der_length (sig_data , sig_len , & idx , & len ) != 0 ) return -1 ;
18171837
18181838 /* Parse first INTEGER (r) tag */
18191839 if (idx >= sig_len || sig_data [idx ++ ] != 0x02 ) {
@@ -1822,19 +1842,13 @@ static int parse_lenient_der_ecdsa_signature(const byte* sig_data, word32 sig_le
18221842 }
18231843
18241844 /* Parse r length */
1825- if (idx >= sig_len ) return -1 ;
1826- len = sig_data [idx ++ ];
1827- if (len & 0x80 ) {
1828- word32 num_bytes = len & 0x7F ;
1829- if (num_bytes > 4 || idx + num_bytes > sig_len ) return -1 ;
1830- len = 0 ;
1831- while (num_bytes -- ) {
1832- len = (len << 8 ) | sig_data [idx ++ ];
1833- }
1834- }
1845+ if (parse_der_length (sig_data , sig_len , & idx , & len ) != 0 ) return -1 ;
18351846
18361847 /* Skip leading zero byte if present (sign byte) */
1837- if (len > 0 && idx < sig_len && sig_data [idx ] == 0x00 ) {
1848+ if (len <= 0 )
1849+ return -1 ;
1850+
1851+ if (idx < sig_len && sig_data [idx ] == 0x00 ) {
18381852 idx ++ ;
18391853 len -- ;
18401854 }
@@ -1854,16 +1868,7 @@ static int parse_lenient_der_ecdsa_signature(const byte* sig_data, word32 sig_le
18541868 }
18551869
18561870 /* Parse s length */
1857- if (idx >= sig_len ) return -1 ;
1858- len = sig_data [idx ++ ];
1859- if (len & 0x80 ) {
1860- word32 num_bytes = len & 0x7F ;
1861- if (num_bytes > 4 || idx + num_bytes > sig_len ) return -1 ;
1862- len = 0 ;
1863- while (num_bytes -- ) {
1864- len = (len << 8 ) | sig_data [idx ++ ];
1865- }
1866- }
1871+ if (parse_der_length (sig_data , sig_len , & idx , & len ) != 0 ) return -1 ;
18671872
18681873 /* Skip leading zero byte if present (sign byte) */
18691874 if (len > 0 && idx < sig_len && sig_data [idx ] == 0x00 ) {
0 commit comments