@@ -24399,10 +24399,10 @@ int PemToDer(const unsigned char* buff, long longSz, int type,
2439924399 const char* headerEnd = NULL;
2440024400 const char* footerEnd = NULL;
2440124401 const char* consumedEnd = NULL;
24402- const char* bufferEnd = (const char*)(buff + longSz) ;
24402+ const char* bufferEnd = NULL ;
2440324403 long neededSz;
2440424404 int ret = 0;
24405- word32 sz = (word32)longSz ;
24405+ word32 sz = 0 ;
2440624406 int encrypted_key = 0;
2440724407 DerBuffer* der;
2440824408 word32 algId = 0;
@@ -24423,6 +24423,14 @@ int PemToDer(const unsigned char* buff, long longSz, int type,
2442324423
2442424424 WOLFSSL_ENTER("PemToDer");
2442524425
24426+ /* Reject negative size - would wrap word32 and corrupt pointer arithmetic. */
24427+ if (longSz < 0) {
24428+ return BAD_FUNC_ARG;
24429+ }
24430+
24431+ bufferEnd = (const char*)(buff + longSz);
24432+ sz = (word32)longSz;
24433+
2442624434 /* get PEM header and footer based on type */
2442724435 ret = wc_PemGetHeaderFooter(type, &header, &footer);
2442824436 if (ret != 0)
@@ -29679,6 +29687,9 @@ int wc_SetAuthKeyIdFromCert(Cert *cert, const byte *der, int derSz)
2967929687 if (cert == NULL) {
2968029688 ret = BAD_FUNC_ARG;
2968129689 }
29690+ else if (derSz < 0) {
29691+ ret = BAD_FUNC_ARG;
29692+ }
2968229693 else {
2968329694 /* Check if decodedCert is cached */
2968429695 if (cert->der != der) {
@@ -30183,6 +30194,9 @@ int wc_SetIssuerBuffer(Cert* cert, const byte* der, int derSz)
3018330194 if (cert == NULL) {
3018430195 ret = BAD_FUNC_ARG;
3018530196 }
30197+ else if (derSz < 0) {
30198+ ret = BAD_FUNC_ARG;
30199+ }
3018630200 else {
3018730201 cert->selfSigned = 0;
3018830202
@@ -30212,6 +30226,9 @@ int wc_SetSubjectBuffer(Cert* cert, const byte* der, int derSz)
3021230226 if (cert == NULL) {
3021330227 ret = BAD_FUNC_ARG;
3021430228 }
30229+ else if (derSz < 0) {
30230+ ret = BAD_FUNC_ARG;
30231+ }
3021530232 else {
3021630233 /* Check if decodedCert is cached */
3021730234 if (cert->der != der) {
@@ -30239,6 +30256,9 @@ int wc_SetSubjectRaw(Cert* cert, const byte* der, int derSz)
3023930256 if (cert == NULL) {
3024030257 ret = BAD_FUNC_ARG;
3024130258 }
30259+ else if (derSz < 0) {
30260+ ret = BAD_FUNC_ARG;
30261+ }
3024230262 else {
3024330263 /* Check if decodedCert is cached */
3024430264 if (cert->der != der) {
@@ -30273,6 +30293,9 @@ int wc_SetIssuerRaw(Cert* cert, const byte* der, int derSz)
3027330293 if (cert == NULL) {
3027430294 ret = BAD_FUNC_ARG;
3027530295 }
30296+ else if (derSz < 0) {
30297+ ret = BAD_FUNC_ARG;
30298+ }
3027630299 else {
3027730300 /* Check if decodedCert is cached */
3027830301 if (cert->der != der) {
@@ -30310,6 +30333,9 @@ int wc_SetAltNamesBuffer(Cert* cert, const byte* der, int derSz)
3031030333 if (cert == NULL) {
3031130334 ret = BAD_FUNC_ARG;
3031230335 }
30336+ else if (derSz < 0) {
30337+ ret = BAD_FUNC_ARG;
30338+ }
3031330339 else {
3031430340 /* Check if decodedCert is cached */
3031530341 if (cert->der != der) {
@@ -30337,6 +30363,9 @@ int wc_SetDatesBuffer(Cert* cert, const byte* der, int derSz)
3033730363 if (cert == NULL) {
3033830364 ret = BAD_FUNC_ARG;
3033930365 }
30366+ else if (derSz < 0) {
30367+ ret = BAD_FUNC_ARG;
30368+ }
3034030369 else {
3034130370 /* Check if decodedCert is cached */
3034230371 if (cert->der != der) {
0 commit comments