@@ -2968,6 +2968,7 @@ static int Tls13IntegrityOnly_Decrypt(WOLFSSL* ssl, byte* output,
29682968 /* Copy the input to output if not the same buffer */
29692969 if (ret == 0 && output != input)
29702970 XMEMCPY(output, input, sz);
2971+ ForceZero(hmac, sizeof(hmac));
29712972 return ret;
29722973}
29732974#endif
@@ -6143,15 +6144,17 @@ static int DoPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 inputSz,
61436144 ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
61446145 if (ext == NULL) {
61456146 WOLFSSL_MSG("No pre shared extension keys found");
6146- return BAD_FUNC_ARG;
6147+ ret = BAD_FUNC_ARG;
6148+ goto cleanup;
61476149 }
61486150
61496151 /* Look through all client's pre-shared keys for a match. */
61506152 for (current = (PreSharedKey*)ext->data; current != NULL;
61516153 current = current->next) {
61526154 #ifndef NO_PSK
61536155 if (current->identityLen > MAX_PSK_ID_LEN) {
6154- return BUFFER_ERROR;
6156+ ret = BUFFER_ERROR;
6157+ goto cleanup;
61556158 }
61566159 XMEMCPY(ssl->arrays->client_identity, current->identity,
61576160 current->identityLen);
@@ -6178,7 +6181,7 @@ static int DoPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 inputSz,
61786181
61796182 #ifdef WOLFSSL_ASYNC_CRYPT
61806183 if (ret == WC_NO_ERR_TRACE(WC_PENDING_E))
6181- return ret ;
6184+ goto cleanup ;
61826185 #endif
61836186
61846187 if (ret != WOLFSSL_TICKET_RET_OK && current->sess_free_cb != NULL) {
@@ -6213,45 +6216,45 @@ static int DoPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 inputSz,
62136216 ssl->options.cipherSuite = ssl->session->cipherSuite;
62146217 ret = SetCipherSpecs(ssl);
62156218 if (ret != 0)
6216- return ret ;
6219+ goto cleanup ;
62176220
62186221 /* Resumption PSK is resumption master secret. */
62196222 ssl->arrays->psk_keySz = ssl->specs.hash_size;
62206223 if ((ret = DeriveResumptionPSK(ssl, ssl->session->ticketNonce.data,
62216224 ssl->session->ticketNonce.len, ssl->arrays->psk_key)) != 0) {
6222- return ret ;
6225+ goto cleanup ;
62236226 }
62246227
62256228 /* Derive the early secret using the PSK. */
62266229 ret = DeriveEarlySecret(ssl);
62276230 if (ret != 0)
6228- return ret ;
6231+ goto cleanup ;
62296232
62306233 /* Hash data up to binders for deriving binders in PSK extension. */
62316234 ret = HashInput(ssl, input, (int)inputSz);
62326235 if (ret < 0)
6233- return ret ;
6236+ goto cleanup ;
62346237
62356238 /* Derive the binder key to use with HMAC. */
62366239 ret = DeriveBinderKeyResume(ssl, binderKey);
62376240 if (ret != 0)
6238- return ret ;
6241+ goto cleanup ;
62396242 }
62406243 else
62416244 #endif /* HAVE_SESSION_TICKET */
62426245 #ifndef NO_PSK
62436246 if (FindPsk(ssl, current, suite, &ret)) {
62446247 if (ret != 0)
6245- return ret ;
6248+ goto cleanup ;
62466249
62476250 ret = HashInput(ssl, input, (int)inputSz);
62486251 if (ret < 0)
6249- return ret ;
6252+ goto cleanup ;
62506253
62516254 /* Derive the binder key to use with HMAC. */
62526255 ret = DeriveBinderKey(ssl, binderKey);
62536256 if (ret != 0)
6254- return ret ;
6257+ goto cleanup ;
62556258 }
62566259 else
62576260 #endif
@@ -6266,18 +6269,19 @@ static int DoPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 inputSz,
62666269 ssl->keys.client_write_MAC_secret,
62676270 0 /* neither end */);
62686271 if (ret != 0)
6269- return ret ;
6272+ goto cleanup ;
62706273
62716274 /* Derive the binder and compare with the one in the extension. */
62726275 ret = BuildTls13HandshakeHmac(ssl,
62736276 ssl->keys.client_write_MAC_secret, binder, &binderLen);
62746277 if (ret != 0)
6275- return ret ;
6278+ goto cleanup ;
62766279 if (binderLen != current->binderLen ||
62776280 ConstantCompare(binder, current->binder,
62786281 binderLen) != 0) {
62796282 WOLFSSL_ERROR_VERBOSE(BAD_BINDER);
6280- return BAD_BINDER;
6283+ ret = BAD_BINDER;
6284+ goto cleanup;
62816285 }
62826286
62836287 /* This PSK works, no need to try any more. */
@@ -6289,19 +6293,26 @@ static int DoPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 inputSz,
62896293 if (current == NULL) {
62906294#ifdef WOLFSSL_PSK_ID_PROTECTION
62916295 #ifndef NO_CERTS
6292- if (ssl->buffers.certChainCnt != 0)
6293- return 0;
6296+ if (ssl->buffers.certChainCnt != 0) {
6297+ ret = 0;
6298+ goto cleanup;
6299+ }
62946300 #endif
62956301 WOLFSSL_ERROR_VERBOSE(BAD_BINDER);
6296- return BAD_BINDER;
6302+ ret = BAD_BINDER;
6303+ goto cleanup;
62976304#else
6298- return 0;
6305+ ret = 0;
6306+ goto cleanup;
62996307#endif
63006308 }
63016309
63026310 *first = (current == ext->data);
63036311 *usingPSK = 1;
63046312
6313+ cleanup:
6314+ ForceZero(binderKey, sizeof(binderKey));
6315+ ForceZero(binder, sizeof(binder));
63056316 WOLFSSL_LEAVE("DoPreSharedKeys", ret);
63066317
63076318 return ret;
@@ -11339,28 +11350,30 @@ int DoTls13Finished(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
1133911350 ret = NO_PEER_CERT; /* NO_PEER_VERIFY */
1134011351 WOLFSSL_MSG("TLS v1.3 client did not present peer cert");
1134111352 DoCertFatalAlert(ssl, ret);
11342- return ret ;
11353+ goto cleanup ;
1134311354 }
1134411355 }
1134511356#endif
1134611357
1134711358 /* check against totalSz */
11348- if (*inOutIdx + size > totalSz)
11349- return BUFFER_E;
11359+ if (*inOutIdx + size > totalSz) {
11360+ ret = BUFFER_E;
11361+ goto cleanup;
11362+ }
1135011363
1135111364#if defined(WOLFSSL_RENESAS_TSIP_TLS)
1135211365 ret = tsip_Tls13HandleFinished(ssl, input, inOutIdx, size, totalSz);
1135311366 if (ret == 0) {
1135411367 ssl->options.serverState = SERVER_FINISHED_COMPLETE;
11355- return ret ;
11368+ goto cleanup ;
1135611369 }
1135711370 if (ret == WC_NO_ERR_TRACE(VERIFY_FINISHED_ERROR)) {
1135811371 SendAlert(ssl, alert_fatal, decrypt_error);
11359- return ret ;
11372+ goto cleanup ;
1136011373 }
1136111374 if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
1136211375 /* other errors */
11363- return ret ;
11376+ goto cleanup ;
1136411377 }
1136511378 ret = 0;
1136611379#endif /* WOLFSSL_RENESAS_TSIP_TLS */
@@ -11370,7 +11383,7 @@ int DoTls13Finished(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
1137011383 ssl->keys.client_write_MAC_secret,
1137111384 WOLFSSL_CLIENT_END);
1137211385 if (ret != 0)
11373- return ret ;
11386+ goto cleanup ;
1137411387
1137511388 secret = ssl->keys.client_write_MAC_secret;
1137611389 }
@@ -11382,13 +11395,13 @@ int DoTls13Finished(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
1138211395 ssl->keys.client_write_MAC_secret,
1138311396 WOLFSSL_CLIENT_END);
1138411397 if (ret != 0)
11385- return ret ;
11398+ goto cleanup ;
1138611399
1138711400 ret = DeriveFinishedSecret(ssl, ssl->serverSecret,
1138811401 ssl->keys.server_write_MAC_secret,
1138911402 WOLFSSL_SERVER_END);
1139011403 if (ret != 0)
11391- return ret ;
11404+ goto cleanup ;
1139211405
1139311406 secret = ssl->keys.server_write_MAC_secret;
1139411407 }
@@ -11401,7 +11414,8 @@ int DoTls13Finished(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
1140111414 ret = BuildTls13HandshakeHmac(ssl, secret, mac, &finishedSz);
1140211415 #ifdef WOLFSSL_HAVE_TLS_UNIQUE
1140311416 if (finishedSz > TLS_FINISHED_SZ_MAX) {
11404- return BUFFER_ERROR;
11417+ ret = BUFFER_ERROR;
11418+ goto cleanup;
1140511419 }
1140611420 if (ssl->options.side == WOLFSSL_CLIENT_END) {
1140711421 XMEMCPY(ssl->serverFinished, mac, finishedSz);
@@ -11413,9 +11427,11 @@ int DoTls13Finished(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
1141311427 }
1141411428 #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
1141511429 if (ret != 0)
11416- return ret;
11417- if (size != finishedSz)
11418- return BUFFER_ERROR;
11430+ goto cleanup;
11431+ if (size != finishedSz) {
11432+ ret = BUFFER_ERROR;
11433+ goto cleanup;
11434+ }
1141911435 }
1142011436
1142111437#ifdef WOLFSSL_CALLBACKS
@@ -11430,7 +11446,8 @@ int DoTls13Finished(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
1143011446 WOLFSSL_MSG("Verify finished error on hashes");
1143111447 SendAlert(ssl, alert_fatal, decrypt_error);
1143211448 WOLFSSL_ERROR_VERBOSE(VERIFY_FINISHED_ERROR);
11433- return VERIFY_FINISHED_ERROR;
11449+ ret = VERIFY_FINISHED_ERROR;
11450+ goto cleanup;
1143411451 }
1143511452 }
1143611453
@@ -11443,12 +11460,12 @@ int DoTls13Finished(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
1144311460#ifdef WOLFSSL_EARLY_DATA
1144411461 if (ssl->earlyData != no_early_data) {
1144511462 if ((ret = DeriveTls13Keys(ssl, no_key, DECRYPT_SIDE_ONLY, 1)) != 0)
11446- return ret ;
11463+ goto cleanup ;
1144711464 }
1144811465#endif
1144911466 /* Setup keys for application data messages from client. */
1145011467 if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0)
11451- return ret ;
11468+ goto cleanup ;
1145211469 }
1145311470#endif
1145411471
@@ -11479,10 +11496,13 @@ int DoTls13Finished(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
1147911496 }
1148011497#endif /* WOLFSSL_QUIC && WOLFSSL_EARLY_DATA */
1148111498
11482- WOLFSSL_LEAVE("DoTls13Finished", 0);
11499+ ret = 0;
11500+ cleanup:
11501+ ForceZero(mac, sizeof(mac));
11502+ WOLFSSL_LEAVE("DoTls13Finished", ret);
1148311503 WOLFSSL_END(WC_FUNC_FINISHED_DO);
1148411504
11485- return 0 ;
11505+ return ret ;
1148611506}
1148711507
1148811508#if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
@@ -12307,39 +12327,32 @@ static int ExpectedResumptionSecret(WOLFSSL* ssl)
1230712327 wc_Sha256Free(&ssl->hsHashes->hashSha256);
1230812328 ret = wc_Sha256Copy(&digest.sha256, &ssl->hsHashes->hashSha256);
1230912329 wc_Sha256Free(&digest.sha256);
12310- if (ret != 0)
12311- return ret;
1231212330 break;
1231312331 #endif
1231412332 #ifdef WOLFSSL_SHA384
1231512333 case sha384_mac:
1231612334 wc_Sha384Free(&ssl->hsHashes->hashSha384);
1231712335 ret = wc_Sha384Copy(&digest.sha384, &ssl->hsHashes->hashSha384);
1231812336 wc_Sha384Free(&digest.sha384);
12319- if (ret != 0)
12320- return ret;
1232112337 break;
1232212338 #endif
1232312339 #ifdef WOLFSSL_TLS13_SHA512
1232412340 case sha512_mac:
1232512341 wc_Sha512Free(&ssl->hsHashes->hashSha512);
1232612342 ret = wc_Sha512Copy(&digest.sha512, &ssl->hsHashes->hashSha512);
1232712343 wc_Sha512Free(&digest.sha512);
12328- if (ret != 0)
12329- return ret;
1233012344 break;
1233112345 #endif
1233212346 #ifdef WOLFSSL_SM3
1233312347 case sm3_mac:
1233412348 wc_Sm3Free(&ssl->hsHashes->hashSm3);
1233512349 ret = wc_Sm3Copy(&digest.sm3, &ssl->hsHashes->hashSm3);
1233612350 wc_Sm3Free(&digest.sm3);
12337- if (ret != 0)
12338- return ret;
1233912351 break;
1234012352 #endif
1234112353 }
1234212354
12355+ ForceZero(mac, sizeof(mac));
1234312356 return ret;
1234412357}
1234512358#endif
0 commit comments