diff --git a/src/internal.c b/src/internal.c index c123c8cec..596f986c3 100644 --- a/src/internal.c +++ b/src/internal.c @@ -7405,6 +7405,7 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup) /* all done with init, now can return errors, call other stuff */ if ((ret = ReinitSSL(ssl, ctx, writeDup)) != 0) { + WOLFSSL_MSG_EX("ReinitSSL failed. err = %d", ret); return ret; } @@ -7438,6 +7439,7 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup) && ret != NO_PRIVATE_KEY #endif ) { + WOLFSSL_MSG_EX("SetSSL_CTX failed. err = %d", ret); return ret; } ssl->options.dtls = ssl->version.major == DTLS_MAJOR; @@ -7451,8 +7453,10 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup) /* hsHashes */ ret = InitHandshakeHashes(ssl); - if (ret != 0) + if (ret != 0) { + WOLFSSL_MSG_EX("InitHandshakeHashes failed. err = %d", ret); return ret; + } #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER) if (ssl->options.dtls && ssl->options.side == WOLFSSL_SERVER_END) { @@ -7493,10 +7497,10 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup) ssl->session = wolfSSL_NewSession(ssl->heap); if (ssl->session == NULL) { - WOLFSSL_MSG("SSL Session Memory error"); + WOLFSSL_MSG_EX("SSL Session Memory error. wolfSSL_NewSession " + "err = %d", ret); return MEMORY_E; } - #ifdef HAVE_SESSION_TICKET ssl->options.noTicketTls12 = ctx->noTicketTls12; #endif @@ -7573,6 +7577,8 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup) ssl->sigSpec = ctx->sigSpec; ssl->sigSpecSz = ctx->sigSpecSz; #endif /* WOLFSSL_DUAL_ALG_CERTS */ + /* Returns 0 on success, not WOLFSSL_SUCCESS (1) */ + WOLFSSL_MSG_EX("InitSSL done. return 0 (success)"); return 0; } @@ -20736,8 +20742,10 @@ int ProcessReplyEx(WOLFSSL* ssl, int allowSocketErr) #endif ret = RetrySendAlert(ssl); - if (ret != 0) + if (ret != 0) { + WOLFSSL_MSG_EX("RetrySendAlert failed, giving up. err = %d", ret); return ret; + } for (;;) { switch (ssl->options.processReply) { @@ -24634,8 +24642,16 @@ static int SendAlert_ex(WOLFSSL* ssl, int severity, int type) int RetrySendAlert(WOLFSSL* ssl) { - int type = ssl->pendingAlert.code; - int severity = ssl->pendingAlert.level; + int type; + int severity; + WOLFSSL_ENTER("RetrySendAlert"); + + if (ssl == NULL) { + return BAD_FUNC_ARG; + } + + type = ssl->pendingAlert.code; + severity = ssl->pendingAlert.level; if (severity == alert_none) return 0; @@ -24649,6 +24665,12 @@ int RetrySendAlert(WOLFSSL* ssl) /* send alert message */ int SendAlert(WOLFSSL* ssl, int severity, int type) { + WOLFSSL_ENTER("SendAlert"); + + if (ssl == NULL) { + return BAD_FUNC_ARG; + } + if (ssl->pendingAlert.level != alert_none) { int ret = RetrySendAlert(ssl); if (ret != 0) { diff --git a/src/ssl.c b/src/ssl.c index 1f1b612ba..16c61dec7 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -1474,18 +1474,35 @@ WOLFSSL* wolfSSL_new(WOLFSSL_CTX* ctx) WOLFSSL_ENTER("wolfSSL_new"); - if (ctx == NULL) - return ssl; - - ssl = (WOLFSSL*) XMALLOC(sizeof(WOLFSSL), ctx->heap, DYNAMIC_TYPE_SSL); - if (ssl) { - if ( (ret = InitSSL(ssl, ctx, 0)) < 0) { - FreeSSL(ssl, ctx->heap); - ssl = 0; - } + if (ctx == NULL) { + WOLFSSL_MSG("wolfSSL_new ctx is null"); + return NULL; } - WOLFSSL_LEAVE("wolfSSL_new", ret); + ssl = (WOLFSSL*) XMALLOC(sizeof(WOLFSSL), ctx->heap, DYNAMIC_TYPE_SSL); + + if (ssl == NULL) { + WOLFSSL_MSG_EX("ssl xmalloc failed to allocate %d bytes", + (int)sizeof(WOLFSSL)); + } + else { + ret = InitSSL(ssl, ctx, 0); + if (ret < 0) { + WOLFSSL_MSG_EX("wolfSSL_new failed during InitSSL. err = %d", ret); + FreeSSL(ssl, ctx->heap); + ssl = NULL; + } + else if (ret == 0) { + WOLFSSL_MSG("wolfSSL_new InitSSL success"); + } + else { + /* Only success (0) or negative values should ever be seen. */ + WOLFSSL_MSG_EX("WARNING: wolfSSL_new unexpected InitSSL return" + " value = %d", ret); + } /* InitSSL check */ + } /* ssl XMALLOC success */ + + WOLFSSL_LEAVE("wolfSSL_new InitSSL =", ret); (void)ret; return ssl; @@ -1496,8 +1513,14 @@ WOLFSSL_ABI void wolfSSL_free(WOLFSSL* ssl) { WOLFSSL_ENTER("wolfSSL_free"); - if (ssl) + + if (ssl) { + WOLFSSL_MSG_EX("Free SSL: %p", (uintptr_t)ssl); FreeSSL(ssl, ssl->ctx->heap); + } + else { + WOLFSSL_MSG("Free SSL: wolfSSL_free already null"); + } WOLFSSL_LEAVE("wolfSSL_free", 0); } @@ -11906,7 +11929,10 @@ static int wolfSSL_parse_cipher_list(WOLFSSL_CTX* ctx, WOLFSSL* ssl, } /* list contains ciphers either only for TLS 1.3 or <= TLS 1.2 */ - + if (suites->suiteSz == 0) { + WOLFSSL_MSG("Warning suites->suiteSz = 0 set to WOLFSSL_MAX_SUITE_SZ"); + suites->suiteSz = WOLFSSL_MAX_SUITE_SZ; + } #ifdef WOLFSSL_SMALL_STACK if (suites->suiteSz > 0) { suitesCpy = (byte*)XMALLOC(suites->suiteSz, NULL, @@ -12598,10 +12624,13 @@ int wolfSSL_DTLS_SetCookieSecret(WOLFSSL* ssl, return wolfSSL_connect_TLSv13(ssl); #else #ifdef WOLFSSL_TLS13 - if (ssl->options.tls1_3) + if (ssl->options.tls1_3) { + WOLFSSL_MSG("TLS 1.3"); return wolfSSL_connect_TLSv13(ssl); + } #endif + WOLFSSL_MSG("TLS 1.2 or lower"); WOLFSSL_ENTER("wolfSSL_connect"); /* make sure this wolfSSL object has arrays and rng setup. Protects @@ -12719,11 +12748,14 @@ int wolfSSL_DTLS_SetCookieSecret(WOLFSSL* ssl, neededState = SERVER_HELLOVERIFYREQUEST_COMPLETE; #endif /* get response */ + WOLFSSL_MSG("Server state up to needed state."); while (ssl->options.serverState < neededState) { + WOLFSSL_MSG("Progressing server state..."); #ifdef WOLFSSL_TLS13 if (ssl->options.tls1_3) return wolfSSL_connect_TLSv13(ssl); #endif + WOLFSSL_MSG("ProcessReply..."); if ( (ssl->error = ProcessReply(ssl)) < 0) { WOLFSSL_ERROR(ssl->error); return WOLFSSL_FATAL_ERROR; @@ -12739,6 +12771,7 @@ int wolfSSL_DTLS_SetCookieSecret(WOLFSSL* ssl, neededState = SERVER_HELLODONE_COMPLETE; } } + WOLFSSL_MSG("ProcessReply done."); #ifdef WOLFSSL_DTLS13 if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version) @@ -16903,6 +16936,11 @@ cleanup: } #endif +#ifdef NO_FILESYSTEM + WOLFSSL_MSG("wolfSSL_CTX_set_default_verify_paths not supported" + " with NO_FILESYSTEM enabled"); + ret = WOLFSSL_FATAL_ERROR; +#else ret = wolfSSL_CTX_load_system_CA_certs(ctx); if (ret == WOLFSSL_BAD_PATH) { /* @@ -16911,6 +16949,7 @@ cleanup: */ ret = WOLFSSL_SUCCESS; } +#endif WOLFSSL_LEAVE("wolfSSL_CTX_set_default_verify_paths", ret); diff --git a/src/ssl_certman.c b/src/ssl_certman.c index 4c0eafa8b..84f51df20 100644 --- a/src/ssl_certman.c +++ b/src/ssl_certman.c @@ -25,7 +25,7 @@ #include - #include +#include #if !defined(WOLFSSL_SSL_CERTMAN_INCLUDED) #ifndef WOLFSSL_IGNORE_FILE_WARN @@ -89,11 +89,22 @@ WOLFSSL_CERT_MANAGER* wolfSSL_CertManagerNew_ex(void* heap) WOLFSSL_CERT_MANAGER* cm; WOLFSSL_ENTER("wolfSSL_CertManagerNew"); + if (heap == NULL) { + WOLFSSL_MSG("heap param is null"); + } + else { + /* Some systems may have heap in unexpected segments. (IRAM vs DRAM) */ + WOLFSSL_MSG_EX("heap param = %p", heap); + } + WOLFSSL_MSG_EX("DYNAMIC_TYPE_CERT_MANAGER Allocating = %d bytes", + (word32)sizeof(WOLFSSL_CERT_MANAGER)); /* Allocate memory for certificate manager. */ cm = (WOLFSSL_CERT_MANAGER*)XMALLOC(sizeof(WOLFSSL_CERT_MANAGER), heap, DYNAMIC_TYPE_CERT_MANAGER); if (cm == NULL) { + WOLFSSL_MSG_EX("XMALLOC failed to allocate WOLFSSL_CERT_MANAGER %d " + "bytes.", (int)sizeof(WOLFSSL_CERT_MANAGER)); err = 1; } if (!err) { diff --git a/src/tls.c b/src/tls.c index 361e869ca..2e367c5ac 100644 --- a/src/tls.c +++ b/src/tls.c @@ -7459,7 +7459,9 @@ static int TLSX_KeyShare_GenEccKey(WOLFSSL *ssl, KeyShareEntry* kse) /* Allocate an ECC key to hold private key. */ kse->key = (byte*)XMALLOC(sizeof(ecc_key), ssl->heap, DYNAMIC_TYPE_ECC); if (kse->key == NULL) { - WOLFSSL_MSG("EccTempKey Memory error"); + WOLFSSL_MSG_EX("Failed to allocate %d bytes, ssl->heap: %p", + (int)sizeof(ecc_key), (uintptr_t)ssl->heap); + WOLFSSL_MSG("EccTempKey Memory error!"); return MEMORY_E; } diff --git a/src/tls13.c b/src/tls13.c index 8a84fff81..cf4a5a186 100644 --- a/src/tls13.c +++ b/src/tls13.c @@ -12633,7 +12633,7 @@ int wolfSSL_connect_TLSv13(WOLFSSL* ssl) } ssl->options.connectState = CLIENT_HELLO_SENT; - WOLFSSL_MSG("connect state: CLIENT_HELLO_SENT"); + WOLFSSL_MSG("TLSv13 connect state: CLIENT_HELLO_SENT"); #ifdef WOLFSSL_EARLY_DATA if (ssl->earlyData != no_early_data) { #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT) diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index b18350c9d..b83fb4bf3 100644 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -527,10 +527,10 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits #endif /* HAVE_AES_DECRYPT */ #elif defined(WOLFSSL_ESP32_CRYPT) && \ - !defined(NO_WOLFSSL_ESP32_CRYPT_AES) + !defined(NO_WOLFSSL_ESP32_CRYPT_AES) #include #include - const char* TAG = "aes"; + #define TAG "aes" /* We'll use SW for fallback: * unsupported key lengths. (e.g. ESP32-S3) @@ -968,6 +968,10 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits +#if defined(WC_AES_BITSLICED) && !defined(HAVE_AES_ECB) + #error "When WC_AES_BITSLICED is defined, HAVE_AES_ECB is needed." +#endif + #ifdef NEED_AES_TABLES #ifndef WC_AES_BITSLICED diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index fcbe8e776..6b99191ad 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -3828,6 +3828,7 @@ int wc_ecc_mulmod_ex2(const mp_int* k, ecc_point* G, ecc_point* R, mp_int* a, #endif /* k can't have more bits than order */ if (mp_count_bits(k) > mp_count_bits(order)) { + WOLFSSL_MSG("Private key length is greater than order in bits."); return ECC_OUT_OF_RANGE_E; } @@ -5801,19 +5802,32 @@ static int _ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key, /* load curve info */ if (err == MP_OKAY) { ALLOC_CURVE_SPECS(ECC_CURVE_FIELD_COUNT, err); + if (err != MP_OKAY) { + WOLFSSL_MSG("ALLOC_CURVE_SPECS failed"); + } } + if (err == MP_OKAY) { err = wc_ecc_curve_load(key->dp, &curve, ECC_CURVE_FIELD_ALL); + if (err != MP_OKAY) { + WOLFSSL_MSG("wc_ecc_curve_load failed"); + } } /* generate k */ if (err == MP_OKAY) { err = wc_ecc_gen_k(rng, key->dp->size, key->k, curve->order); + if (err != MP_OKAY) { + WOLFSSL_MSG("wc_ecc_gen_k failed"); + } } /* generate public key from k */ if (err == MP_OKAY) { err = ecc_make_pub_ex(key, curve, NULL, rng); + if (err != MP_OKAY) { + WOLFSSL_MSG("ecc_make_pub_ex failed"); + } } if (err == MP_OKAY diff --git a/wolfcrypt/src/md5.c b/wolfcrypt/src/md5.c index 6700fdc94..daab9c9ec 100644 --- a/wolfcrypt/src/md5.c +++ b/wolfcrypt/src/md5.c @@ -454,6 +454,7 @@ int wc_Md5Final(wc_Md5* md5, byte* hash) /* ensure we have a valid buffer length; (-1 to append a byte to length) */ if (md5->buffLen > WC_MD5_BLOCK_SIZE - 1) { + /* some places consider this BAD_STATE_E */ return BUFFER_E; } diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index 518733419..91577d3d3 100644 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -31,6 +31,9 @@ This library contains implementation for the random number generator. #include #include +#if defined(DEBUG_WOLFSSL) + #include +#endif /* on HPUX 11 you may need to install /dev/random see http://h20293.www2.hp.com/portal/swdepot/displayProductInfo.do?productNumber=KRNG11I @@ -1635,9 +1638,12 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz, #ifdef CUSTOM_RAND_GENERATE_BLOCK ret = 0; /* success */ #else + + /* not CUSTOM_RAND_GENERATE_BLOCK follows */ #ifdef HAVE_HASHDRBG - if (nonceSz == 0) + if (nonceSz == 0) { seedSz = MAX_SEED_SZ; + } if (wc_RNG_HealthTestLocal(0, rng->heap, devId) == 0) { #ifndef WOLFSSL_SMALL_STACK @@ -1654,13 +1660,23 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz, (struct DRBG*)XMALLOC(sizeof(DRBG_internal), rng->heap, DYNAMIC_TYPE_RNG); if (rng->drbg == NULL) { + #if defined(DEBUG_WOLFSSL) + WOLFSSL_MSG_EX("_InitRng XMALLOC failed to allocate %d bytes", + sizeof(DRBG_internal)); + #endif ret = MEMORY_E; rng->status = DRBG_FAILED; } #else rng->drbg = (struct DRBG*)&rng->drbg_data; +#endif /* WOLFSSL_NO_MALLOC or WOLFSSL_STATIC_MEMORY */ + + if (ret != 0) { +#if defined(DEBUG_WOLFSSL) + WOLFSSL_MSG_EX("_InitRng failed. err = ", ret); #endif - if (ret == 0) { + } + else { #ifdef WC_RNG_SEED_CB if (seedCb == NULL) { ret = DRBG_NO_SEED_CB; @@ -1673,10 +1689,13 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz, } #else ret = wc_GenerateSeed(&rng->seed, seed, seedSz); -#endif +#endif /* WC_RNG_SEED_CB */ if (ret == 0) ret = wc_RNG_TestSeed(seed, seedSz); else { + #if defined(DEBUG_WOLFSSL) + WOLFSSL_MSG_EX("wc_RNG_TestSeed failed... %d", ret); + #endif ret = DRBG_FAILURE; rng->status = DRBG_FAILED; } @@ -1692,7 +1711,7 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz, #endif rng->drbg = NULL; } - } + } /* ret == 0 */ ForceZero(seed, seedSz); #ifdef WOLFSSL_SMALL_STACK @@ -1705,11 +1724,11 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz, if (ret == DRBG_SUCCESS) { #ifdef WOLFSSL_CHECK_MEM_ZERO -#ifdef HAVE_HASHDRBG + #ifdef HAVE_HASHDRBG struct DRBG_internal* drbg = (struct DRBG_internal*)rng->drbg; wc_MemZero_Add("DRBG V", &drbg->V, sizeof(drbg->V)); wc_MemZero_Add("DRBG C", &drbg->C, sizeof(drbg->C)); -#endif + #endif #endif rng->status = DRBG_OK; @@ -3451,6 +3470,9 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) { + #if defined(DEBUG_WOLFSSL) + WOLFSSL_ENTER("ESP8266 Random"); + #endif word32 rand; while (sz > 0) { word32 len = sizeof(rand); @@ -3465,7 +3487,7 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) return 0; } - #endif /* end WOLFSSL_ESP32 */ + #endif /* end WOLFSSL_ESPIDF */ #elif defined(WOLFSSL_LINUXKM) #include @@ -3744,15 +3766,23 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) #ifndef NO_DEV_URANDOM /* way to disable use of /dev/urandom */ os->fd = open("/dev/urandom", O_RDONLY); + #if defined(DEBUG_WOLFSSL) + WOLFSSL_MSG("opened /dev/urandom."); + #endif if (os->fd == -1) #endif { /* may still have /dev/random */ os->fd = open("/dev/random", O_RDONLY); + #if defined(DEBUG_WOLFSSL) + WOLFSSL_MSG("opened /dev/random."); + #endif if (os->fd == -1) return OPEN_RAN_E; } - + #if defined(DEBUG_WOLFSSL) + WOLFSSL_MSG("rnd read..."); + #endif while (sz) { int len = (int)read(os->fd, output, sz); if (len == -1) { diff --git a/wolfcrypt/src/rsa.c b/wolfcrypt/src/rsa.c index 56a6efa83..ef172c090 100644 --- a/wolfcrypt/src/rsa.c +++ b/wolfcrypt/src/rsa.c @@ -2681,12 +2681,16 @@ static int RsaFunctionSync(const byte* in, word32 inLen, byte* out, NEW_MP_INT_SIZE(tmp, mp_bitsused(&key->n), key->heap, DYNAMIC_TYPE_RSA); #ifdef MP_INT_SIZE_CHECK_NULL - if (tmp == NULL) + if (tmp == NULL) { + WOLFSSL_MSG("NEW_MP_INT_SIZE tmp is NULL, return MEMORY_E"); return MEMORY_E; + } #endif - if (INIT_MP_INT_SIZE(tmp, mp_bitsused(&key->n)) != MP_OKAY) + if (INIT_MP_INT_SIZE(tmp, mp_bitsused(&key->n)) != MP_OKAY) { + WOLFSSL_MSG("INIT_MP_INT_SIZE failed."); ret = MP_INIT_E; + } #ifndef TEST_UNPAD_CONSTANT_TIME if (ret == 0 && mp_read_unsigned_bin(tmp, in, inLen) != MP_OKAY) @@ -2710,8 +2714,10 @@ static int RsaFunctionSync(const byte* in, word32 inLen, byte* out, #endif case RSA_PUBLIC_ENCRYPT: case RSA_PUBLIC_DECRYPT: - if (mp_exptmod_nct(tmp, &key->e, &key->n, tmp) != MP_OKAY) + if (mp_exptmod_nct(tmp, &key->e, &key->n, tmp) != MP_OKAY) { + WOLFSSL_MSG("mp_exptmod_nct failed"); ret = MP_EXPTMOD_E; + } break; default: ret = RSA_WRONG_TYPE_E; @@ -2720,8 +2726,11 @@ static int RsaFunctionSync(const byte* in, word32 inLen, byte* out, } if (ret == 0) { - if (mp_to_unsigned_bin_len_ct(tmp, out, (int)*outLen) != MP_OKAY) - ret = MP_TO_E; + WOLFSSL_MSG("mp_to_unsigned_bin_len_ct..."); + if (mp_to_unsigned_bin_len_ct(tmp, out, (int)*outLen) != MP_OKAY) { + WOLFSSL_MSG("mp_to_unsigned_bin_len_ct failed"); + ret = MP_TO_E; + } } #ifdef WOLFSSL_RSA_CHECK_D_ON_DECRYPT if ((ret == 0) && (type == RSA_PRIVATE_DECRYPT)) { @@ -2757,6 +2766,7 @@ static int wc_RsaFunctionSync(const byte* in, word32 inLen, byte* out, ret = wc_RsaEncryptSize(key); if (ret < 0) { + WOLFSSL_MSG_EX("wc_RsaEncryptSize failed err = %d", ret); return ret; } keyLen = (word32)ret; @@ -2771,6 +2781,7 @@ static int wc_RsaFunctionSync(const byte* in, word32 inLen, byte* out, } if (mp_iseven(&key->n)) { + WOLFSSL_MSG("MP_VAL is even"); return MP_VAL; }