@@ -1314,7 +1314,11 @@ int wolfSSL_EVP_CipherFinal(WOLFSSL_EVP_CIPHER_CTX *ctx, unsigned char *out,
13141314#ifndef WOLFSSL_AESGCM_STREAM
13151315 if ((ctx -> authBuffer && ctx -> authBufferLen > 0 )
13161316 || (ctx -> authBufferLen == 0 )) {
1317- if (ctx -> enc )
1317+ if (ctx -> authBufferLen > 0 && out == NULL ) {
1318+ ret = WOLFSSL_FAILURE ;
1319+ * outl = 0 ;
1320+ }
1321+ else if (ctx -> enc )
13181322 ret = wc_AesGcmEncrypt (& ctx -> cipher .aes , out ,
13191323 ctx -> authBuffer , ctx -> authBufferLen ,
13201324 ctx -> iv , ctx -> ivSz , ctx -> authTag , ctx -> authTagSz ,
@@ -1397,7 +1401,11 @@ int wolfSSL_EVP_CipherFinal(WOLFSSL_EVP_CIPHER_CTX *ctx, unsigned char *out,
13971401 case WC_AES_256_CCM_TYPE :
13981402 if ((ctx -> authBuffer && ctx -> authBufferLen > 0 )
13991403 || (ctx -> authBufferLen == 0 )) {
1400- if (ctx -> enc ) {
1404+ if (ctx -> authBufferLen > 0 && out == NULL ) {
1405+ ret = WOLFSSL_FAILURE ;
1406+ * outl = 0 ;
1407+ }
1408+ else if (ctx -> enc ) {
14011409 ret = wc_AesCcmEncrypt (& ctx -> cipher .aes , out ,
14021410 ctx -> authBuffer , (word32 )ctx -> authBufferLen ,
14031411 ctx -> iv , (word32 )ctx -> ivSz , ctx -> authTag ,
@@ -4309,16 +4317,19 @@ int wolfSSL_EVP_SignFinal(WOLFSSL_EVP_MD_CTX *ctx, unsigned char *sigret,
43094317#ifndef NO_DSA
43104318 case WC_EVP_PKEY_DSA : {
43114319 int bytes ;
4312- ret = wolfSSL_DSA_do_sign (md , sigret , pkey -> dsa );
4320+ unsigned char tmpSig [DSA_MAX_SIG_SIZE ];
4321+ ret = wolfSSL_DSA_do_sign (md , tmpSig , pkey -> dsa );
43134322 /* wolfSSL_DSA_do_sign() can return WOLFSSL_FATAL_ERROR */
43144323 if (ret != WOLFSSL_SUCCESS )
43154324 return ret ;
43164325 bytes = wolfSSL_BN_num_bytes (pkey -> dsa -> q );
43174326 if (bytes == WC_NO_ERR_TRACE (WOLFSSL_FAILURE ) ||
4318- (int )* siglen < bytes * 2 )
4327+ bytes > DSA_MAX_HALF_SIZE ||
4328+ bytes * 2 > (int )* siglen )
43194329 {
43204330 return WOLFSSL_FAILURE ;
43214331 }
4332+ XMEMCPY (sigret , tmpSig , bytes * 2 );
43224333 * siglen = (unsigned int )(bytes * 2 );
43234334 return WOLFSSL_SUCCESS ;
43244335 }
@@ -4398,7 +4409,8 @@ int wolfSSL_EVP_VerifyFinal(WOLFSSL_EVP_MD_CTX *ctx,
43984409 unsigned char md [WC_MAX_DIGEST_SIZE ];
43994410 unsigned int mdsize ;
44004411
4401- if (ctx == NULL ) return WOLFSSL_FAILURE ;
4412+ if (ctx == NULL || pkey == NULL || sig == NULL )
4413+ return WOLFSSL_FAILURE ;
44024414 WOLFSSL_ENTER ("EVP_VerifyFinal" );
44034415 ret = wolfSSL_EVP_DigestFinal (ctx , md , & mdsize );
44044416 if (ret <= 0 )
@@ -4459,6 +4471,9 @@ WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_new_mac_key(int type, WOLFSSL_ENGINE* e,
44594471 if (type != WC_EVP_PKEY_HMAC || (key == NULL && keylen != 0 ))
44604472 return NULL ;
44614473
4474+ if (keylen < 0 )
4475+ return NULL ;
4476+
44624477 pkey = wolfSSL_EVP_PKEY_new ();
44634478 if (pkey != NULL ) {
44644479 pkey -> pkey .ptr = (char * )XMALLOC ((size_t )keylen , NULL ,
@@ -4870,6 +4885,9 @@ int wolfSSL_EVP_DigestSignFinal(WOLFSSL_EVP_MD_CTX *ctx, unsigned char *sig,
48704885 return WOLFSSL_SUCCESS ;
48714886 }
48724887 }
4888+ else if (ctx -> pctx == NULL || ctx -> pctx -> pkey == NULL ) {
4889+ return WOLFSSL_FAILURE ;
4890+ }
48734891#ifndef NO_RSA
48744892 else if (ctx -> pctx -> pkey -> type == WC_EVP_PKEY_RSA ) {
48754893 if (sig == NULL ) {
@@ -5007,6 +5025,8 @@ int wolfSSL_EVP_DigestVerifyFinal(WOLFSSL_EVP_MD_CTX *ctx,
50075025 return WOLFSSL_FAILURE ;
50085026 }
50095027 else {
5028+ if (ctx -> pctx == NULL || ctx -> pctx -> pkey == NULL )
5029+ return WOLFSSL_FAILURE ;
50105030 /* Verify the signature with the digest. */
50115031 switch (ctx -> pctx -> pkey -> type ) {
50125032 #if !defined(NO_RSA )
@@ -10233,6 +10253,9 @@ int wolfSSL_EVP_Digest(const unsigned char* in, int inSz, unsigned char* out,
1023310253 return WOLFSSL_FAILURE ;
1023410254 }
1023510255
10256+ if (inSz < 0 )
10257+ return WOLFSSL_FAILURE ;
10258+
1023610259 err = wolfSSL_EVP_get_hashinfo (evp , & hashType , & hashSz );
1023710260 if (err != WOLFSSL_SUCCESS )
1023810261 return err ;
@@ -11280,6 +11303,7 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD* type)
1128011303 enum wc_HashType macType ;
1128111304
1128211305 WOLFSSL_ENTER ("wolfSSL_EVP_DigestFinal" );
11306+
1128311307 macType = EvpMd2MacType (wolfSSL_EVP_MD_CTX_md (ctx ));
1128411308 switch (macType ) {
1128511309 case WC_HASH_TYPE_MD4 :
@@ -11305,16 +11329,18 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD* type)
1130511329
1130611330 case WC_HASH_TYPE_SHAKE128 :
1130711331 #if defined(WOLFSSL_SHA3 ) && defined(WOLFSSL_SHAKE128 )
11308- * s = 16 ; /* if mixing up XOF with plain digest 128 bit is
11309- * default for SHAKE128 */
11332+ if (s != NULL )
11333+ * s = 16 ; /* if mixing up XOF with plain digest 128 bit is
11334+ * default for SHAKE128 */
1131011335 #else
1131111336 return WOLFSSL_FAILURE ;
1131211337 #endif
1131311338 break ;
1131411339 case WC_HASH_TYPE_SHAKE256 :
1131511340 #if defined(WOLFSSL_SHA3 ) && defined(WOLFSSL_SHAKE256 )
11316- * s = 32 ; /* if mixing up XOF with plain digest 256 bit is
11317- * default for SHAKE256 */
11341+ if (s != NULL )
11342+ * s = 32 ; /* if mixing up XOF with plain digest 256 bit is
11343+ * default for SHAKE256 */
1131811344 #else
1131911345 return WOLFSSL_FAILURE ;
1132011346 #endif
@@ -12882,6 +12908,9 @@ int wolfSSL_EVP_EncodeBlock(unsigned char *out, const unsigned char *in,
1288212908 if (out == NULL || in == NULL )
1288312909 return WOLFSSL_FATAL_ERROR ;
1288412910
12911+ if (inLen < 0 )
12912+ return WOLFSSL_FATAL_ERROR ;
12913+
1288512914 if (Base64_Encode_NoNl (in , (word32 )inLen , out , & ret ) == 0 )
1288612915 return (int )ret ;
1288712916 else
@@ -12898,6 +12927,9 @@ int wolfSSL_EVP_DecodeBlock(unsigned char *out, const unsigned char *in,
1289812927 if (out == NULL || in == NULL )
1289912928 return WOLFSSL_FATAL_ERROR ;
1290012929
12930+ if (inLen < 0 )
12931+ return WOLFSSL_FATAL_ERROR ;
12932+
1290112933 if (Base64_Decode (in , (word32 )inLen , out , & ret ) == 0 )
1290212934 return (int )ret ;
1290312935 else
0 commit comments