diff --git a/src/internal.c b/src/internal.c index 67c90431ff1..69abc2faef5 100644 --- a/src/internal.c +++ b/src/internal.c @@ -8202,10 +8202,9 @@ int InitSSL_Suites(WOLFSSL* ssl) writeDup flag indicating this is a write dup only WOLFSSL_SUCCESS return value on success */ -#ifdef OPENSSL_EXTRA /* Apply the protocol version mask and downgrade rules. Returns VERSION_ERROR * when the requested options are inconsistent, 0 otherwise. */ -static int SetSSL_CTX_CheckVersion(WOLFSSL* ssl, WOLFSSL_CTX* ctx) +int SetSSL_CTX_CheckVersion(WOLFSSL* ssl, WOLFSSL_CTX* ctx) { #ifdef WOLFSSL_TLS13 if (ssl->version.minor == TLSv1_3_MINOR && @@ -8271,7 +8270,6 @@ static int SetSSL_CTX_CheckVersion(WOLFSSL* ssl, WOLFSSL_CTX* ctx) return 0; } -#endif /* OPENSSL_EXTRA */ #ifndef NO_CERTS /* Give the SSL object the certificate, certificate chain and private key of @@ -9024,6 +9022,13 @@ int ReinitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup) } (void)ctx; + /* Called on every connect/accept, so only when a connection starts. */ + if (!ssl->options.handShakeDone && + (((ssl->options.side == WOLFSSL_CLIENT_END) ? + ssl->options.connectState : ssl->options.acceptState) == 0)) { + ResetPeerResults(ssl); + } + ssl->options.shutdownDone = 0; if (ssl->session != NULL) ssl->session->side = (byte)ssl->options.side; @@ -9434,18 +9439,7 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup) #endif -#ifndef WOLFSSL_AEAD_ONLY - #ifndef NO_OLD_TLS - ssl->hmac = SSL_hmac; /* default to SSLv3 */ - #elif !defined(WOLFSSL_NO_TLS12) && !defined(NO_TLS) - #if !defined(WOLFSSL_RENESAS_FSPSM_TLS) && \ - !defined(WOLFSSL_RENESAS_TSIP_TLS) - ssl->hmac = TLS_hmac; - #else - ssl->hmac = Renesas_cmn_TLS_hmac; - #endif - #endif -#endif /* WOLFSSL_AEAD_ONLY */ + SetDefaultHmac(ssl); #if defined(WOLFSSL_OPENVPN) && defined(HAVE_KEYING_MATERIAL) /* Save arrays by default for OpenVPN */ @@ -10332,6 +10326,40 @@ void FreeKeyExchange(WOLFSSL* ssl) #endif } +#ifdef WOLFSSL_ASYNC_CRYPT +/* Take the object's event off the CTX queue before its owner is freed. A + * completed event can still be queued, and a removed one keeps stale links, so + * look it up in the queue rather than trusting its state. */ +int AsyncDetachEvent(WOLFSSL* ssl) +{ + WOLF_EVENT* event; + WOLF_EVENT* cur; + + if (ssl->asyncDev == NULL) + return 0; + event = &ssl->asyncDev->event; + if (ssl->ctx != NULL) { + WOLF_EVENT_QUEUE* queue = &ssl->ctx->event_queue; + #ifndef SINGLE_THREADED + if (wc_LockMutex(&queue->lock) != 0) + return BAD_MUTEX_E; + #endif + for (cur = queue->head; cur != NULL; cur = cur->next) { + if (cur == event) { + (void)wolfEventQueue_Remove(queue, event); + break; + } + } + #ifndef SINGLE_THREADED + wc_UnLockMutex(&queue->lock); + #endif + } + XMEMSET(event, 0, sizeof(*event)); + ssl->asyncDev = NULL; + return 0; +} +#endif + #if defined(WOLFSSL_ASYNC_CRYPT) && \ (!defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ defined(HAVE_ED448)) @@ -10340,17 +10368,8 @@ static void DetachPendingAsyncDev(WOLFSSL* ssl, WC_ASYNC_DEV* asyncDev) if (ssl == NULL || asyncDev == NULL) return; - /* asyncDev is set when the event is queued. Init alone sets PENDING and - * leaves the event unlinked, so do not remove in that window. */ - if (ssl->asyncDev == asyncDev) { - if (asyncDev->event.state == WOLF_EVENT_STATE_PENDING && - ssl->ctx != NULL) { - (void)wolfEventQueue_Remove(&ssl->ctx->event_queue, - &asyncDev->event); - } - XMEMSET(&asyncDev->event, 0, sizeof(asyncDev->event)); - ssl->asyncDev = NULL; - } + if (ssl->asyncDev == asyncDev) + (void)AsyncDetachEvent(ssl); } #endif @@ -10951,7 +10970,65 @@ void wolfSSL_ResourceFree(WOLFSSL* ssl) #endif /* WOLFSSL_STATIC_MEMORY */ } +/* The MAC function a new object starts with. */ +void SetDefaultHmac(WOLFSSL* ssl) +{ +#ifndef WOLFSSL_AEAD_ONLY + #ifndef NO_OLD_TLS + ssl->hmac = SSL_hmac; /* default to SSLv3 */ + #elif !defined(WOLFSSL_NO_TLS12) && !defined(NO_TLS) + #if !defined(WOLFSSL_RENESAS_FSPSM_TLS) && \ + !defined(WOLFSSL_RENESAS_TSIP_TLS) + ssl->hmac = TLS_hmac; + #else + ssl->hmac = Renesas_cmn_TLS_hmac; + #endif + #endif +#endif /* WOLFSSL_AEAD_ONLY */ + (void)ssl; +} + +/* Drop what the application could query about the previous connection. */ +void ResetPeerResults(WOLFSSL* ssl) +{ + ssl->alert_history.last_rx.code = -1; + ssl->alert_history.last_rx.level = -1; + ssl->alert_history.last_tx.code = -1; + ssl->alert_history.last_tx.level = -1; +#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \ + defined(OPENSSL_ALL) + ssl->peerVerifyRet = 0; +#endif +} + /* Free any handshake resources no longer needed */ +/* Free the ephemeral key exchange material. Each handshake, a renegotiation + * included, makes its own. */ +void FreeEphemeralKeys(WOLFSSL* ssl) +{ +#if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448) + /* Present holds the type; only an ECC key is left allocated without it. */ + FreeKey(ssl, (ssl->eccTempKeyPresent != 0) ? ssl->eccTempKeyPresent : + DYNAMIC_TYPE_ECC, (void**)&ssl->eccTempKey); + ssl->eccTempKeyPresent = 0; +#endif +#ifndef NO_DH + if (ssl->buffers.serverDH_Priv.buffer != NULL) { + ForceZero(ssl->buffers.serverDH_Priv.buffer, + ssl->buffers.serverDH_Priv.length); + } + XFREE(ssl->buffers.serverDH_Priv.buffer, ssl->heap, + DYNAMIC_TYPE_PRIVATE_KEY); + ssl->buffers.serverDH_Priv.buffer = NULL; + ssl->buffers.serverDH_Priv.length = 0; + XFREE(ssl->buffers.serverDH_Pub.buffer, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY); + ssl->buffers.serverDH_Pub.buffer = NULL; + ssl->buffers.serverDH_Pub.length = 0; + FreeKey(ssl, DYNAMIC_TYPE_DH, (void**)&ssl->buffers.serverDH_Key); +#endif + (void)ssl; +} + void FreeHandshakeResources(WOLFSSL* ssl) { WOLFSSL_ENTER("FreeHandshakeResources"); @@ -10975,6 +11052,8 @@ void FreeHandshakeResources(WOLFSSL* ssl) } #endif + FreeEphemeralKeys(ssl); + #ifdef HAVE_SECURE_RENEGOTIATION /* advertiseOnly clients never renegotiate, so do not retain (and leave * unzeroed) the master secret and other handshake resources for them. */ @@ -11048,35 +11127,6 @@ void FreeHandshakeResources(WOLFSSL* ssl) FreeKey(ssl, DYNAMIC_TYPE_ECC, (void**)&ssl->peerEccKey); ssl->peerEccKeyPresent = 0; #endif -#if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448) - { - int dtype; - #ifdef HAVE_ECC - dtype = DYNAMIC_TYPE_ECC; - #elif defined(HAVE_CURVE25519) - dtype = DYNAMIC_TYPE_CURVE25519; - #else - dtype = DYNAMIC_TYPE_CURVE448; - #endif - #if defined(HAVE_ECC) && defined(HAVE_CURVE25519) - if (ssl->peerX25519KeyPresent || - ssl->eccTempKeyPresent == DYNAMIC_TYPE_CURVE25519) - { - dtype = DYNAMIC_TYPE_CURVE25519; - } - #endif - #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519)) && \ - defined(HAVE_CURVE448) - if (ssl->peerX448KeyPresent || - ssl->eccTempKeyPresent == DYNAMIC_TYPE_CURVE448) - { - dtype = DYNAMIC_TYPE_CURVE448; - } - #endif - FreeKey(ssl, dtype, (void**)&ssl->eccTempKey); - ssl->eccTempKeyPresent = 0; - } -#endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */ #ifdef HAVE_CURVE25519 FreeKey(ssl, DYNAMIC_TYPE_CURVE25519, (void**)&ssl->peerX25519Key); ssl->peerX25519KeyPresent = 0; @@ -11087,14 +11137,6 @@ void FreeHandshakeResources(WOLFSSL* ssl) #endif #ifndef NO_DH - if (ssl->buffers.serverDH_Priv.buffer) { - ForceZero(ssl->buffers.serverDH_Priv.buffer, - ssl->buffers.serverDH_Priv.length); - } - XFREE(ssl->buffers.serverDH_Priv.buffer, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY); - ssl->buffers.serverDH_Priv.buffer = NULL; - XFREE(ssl->buffers.serverDH_Pub.buffer, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY); - ssl->buffers.serverDH_Pub.buffer = NULL; /* Release the parameters (p,g) back here rather than hold them for * the life of the connection. */ if (ssl->buffers.weOwnDH) { @@ -22732,6 +22774,7 @@ static int ResetHandshakeStateForReneg(WOLFSSL* ssl) ssl->options.acceptState = ACCEPT_FIRST_REPLY_DONE; ssl->options.handShakeState = NULL_STATE; ssl->secure_renegotiation->cache_status = SCR_CACHE_NEEDED; + FreeEphemeralKeys(ssl); /* Reset for the renegotiation_info presence check below. */ ssl->secure_renegotiation->renegInfoSeen = 0; @@ -37359,6 +37402,34 @@ static int IsTls13CipherSuite(byte first, byte second) return 1; } + /* A full handshake makes a new session. Drop what would resume the + * previous one, so it is not kept beside this handshake's secret. */ + int ResetSessionForFullHandshake(WOLFSSL* ssl) + { + WOLFSSL_SESSION* session = ssl->session; + + if ((session->sessionIDSz == 0) && !session->haveAltSessionID + #ifdef HAVE_SESSION_TICKET + && (session->ticketLen == 0) + #endif + ) { + return 0; + } + if (!HaveUniqueSessionObj(ssl)) + return MEMORY_ERROR; + session = ssl->session; + + ForceZero(session->sessionID, ID_LEN); + session->sessionIDSz = 0; + ForceZero(session->altSessionID, ID_LEN); + session->haveAltSessionID = 0; + #ifdef HAVE_SESSION_TICKET + return SetSessionTicket(session, NULL, 0); + #else + return 0; + #endif + } + #ifndef WOLFSSL_NO_TLS12 /* handle generation of client_hello (1) */ @@ -38334,6 +38405,9 @@ static int IsTls13CipherSuite(byte first, byte second) ssl->options.resuming = 0; /* server denied resumption try */ } } + ret = ResetSessionForFullHandshake(ssl); + if (ret != 0) + return ret; return SetCipherSpecs(ssl); } @@ -45034,6 +45108,11 @@ static int AddPSKtoPreMasterSecret(WOLFSSL* ssl) } XMEMCPY(ssl->clSuites->suites, input + i, ssl->clSuites->suiteSz); + #ifdef OPENSSL_EXTRA + /* Built from the previous hello's list. */ + wolfSSL_sk_SSL_CIPHER_free(ssl->clSuitesStack); + ssl->clSuitesStack = NULL; + #endif #ifdef HAVE_SERVER_RENEGOTIATION_INFO /* check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV suite */ diff --git a/src/ssl.c b/src/ssl.c index 8e8f61f3566..51814af271b 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -5777,7 +5777,9 @@ size_t wolfSSL_get_client_random(const WOLFSSL* ssl, unsigned char* out, defined(HAVE_CURL) #endif - int wolfSSL_clear(WOLFSSL* ssl) + /* keepResults: wolfSSL_shutdown() leaves what the application can still + * query about the ended connection. */ + int ClearSSL(WOLFSSL* ssl, byte keepResults) { WOLFSSL_ENTER("wolfSSL_clear"); @@ -5815,6 +5817,32 @@ size_t wolfSSL_get_client_random(const WOLFSSL* ssl, unsigned char* out, SslWaivedCAsFree(ssl); #endif + if (!keepResults) { + /* The peer's version is not the limit for the next connection. */ + if (ssl->options.downgrade) { + ssl->version.minor = ssl->options.versionSet ? + ssl->options.maxVersionMinor : + ssl->ctx->method->version.minor; + /* The version mask only covers TLS. */ + if (!ssl->options.dtls && + (SetSSL_CTX_CheckVersion(ssl, ssl->ctx) != 0)) { + return WOLFSSL_FAILURE; + } + } + + ResetPeerResults(ssl); + #if !defined(WOLFSSL_NO_CA_NAMES) && defined(OPENSSL_EXTRA) + wolfSSL_sk_X509_NAME_pop_free(ssl->peer_ca_names, NULL); + ssl->peer_ca_names = NULL; + #endif + #ifdef OPENSSL_EXTRA + wolfSSL_sk_X509_pop_free(ssl->peerCertChain, NULL); + ssl->peerCertChain = NULL; + wolfSSL_sk_X509_pop_free(ssl->verifiedChain, NULL); + ssl->verifiedChain = NULL; + #endif + } + /* reset error */ ssl->error = 0; @@ -5834,13 +5862,6 @@ size_t wolfSSL_get_client_random(const WOLFSSL* ssl, unsigned char* out, ssl->options.acceptState = ACCEPT_BEGIN; ssl->options.handShakeState = NULL_STATE; ssl->options.handShakeDone = 0; -#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WOLFSSL_ASYNC_CERT_YIELD) - /* A per-certificate yield (WOLFSSL_ASYNC_CERT_YIELD) sets this and it is - * normally cleared on the next ProcessPeerCerts re-entry. Clear it here - * so reusing this object after abandoning a yielded handshake cannot - * skip the ProcessPeerCerts state reset on the next fresh entry. */ - ssl->options.certYieldPending = 0; -#endif ssl->recordSzOverhead = 0; #ifdef WOLFSSL_TLS13_STREAM_CERT_VERIFY /* Drop any half-sent streamed CertificateVerify. Left in place, the @@ -5921,11 +5942,58 @@ size_t wolfSSL_get_client_random(const WOLFSSL* ssl, unsigned char* out, #ifdef HAVE_CURVE448 ssl->peerX448KeyPresent = 0; #endif + #ifdef WOLFSSL_ASYNC_CRYPT + /* An abandoned handshake can leave a queued event, whose key is freed + * below, and a handler's resume state. */ + if (AsyncDetachEvent(ssl) != 0) + return WOLFSSL_FAILURE; + #ifdef WOLFSSL_TLS13 + ssl->options.buildArgs13Set = 0; + ssl->options.chHashInput = 0; + ssl->options.asyncReplayMsg = 0; + #endif + #ifdef WOLFSSL_ASYNC_CERT_YIELD + ssl->options.certYieldPending = 0; + #endif + #endif + #ifdef WOLFSSL_ASYNC_IO + FreeAsyncCtx(ssl, 1); + #endif + ssl->options.asyncState = TLS_ASYNC_BEGIN; /* Drop the peer certificate public key left by an abandoned handshake. */ FreePeerCertKeys(ssl); + #ifdef HAVE_PK_CALLBACKS + #ifdef HAVE_ECC + XFREE(ssl->buffers.peerEccDsaKey.buffer, ssl->heap, DYNAMIC_TYPE_ECC); + ssl->buffers.peerEccDsaKey.buffer = NULL; + ssl->buffers.peerEccDsaKey.length = 0; + #endif + #ifndef NO_RSA + XFREE(ssl->buffers.peerRsaKey.buffer, ssl->heap, DYNAMIC_TYPE_RSA); + ssl->buffers.peerRsaKey.buffer = NULL; + ssl->buffers.peerRsaKey.length = 0; + #endif + #ifdef HAVE_ED25519 + XFREE(ssl->buffers.peerEd25519Key.buffer, ssl->heap, + DYNAMIC_TYPE_ED25519); + ssl->buffers.peerEd25519Key.buffer = NULL; + ssl->buffers.peerEd25519Key.length = 0; + #endif + #ifdef HAVE_ED448 + XFREE(ssl->buffers.peerEd448Key.buffer, ssl->heap, DYNAMIC_TYPE_ED448); + ssl->buffers.peerEd448Key.buffer = NULL; + ssl->buffers.peerEd448Key.length = 0; + #endif + #endif /* HAVE_PK_CALLBACKS */ + FreeEphemeralKeys(ssl); + + ssl->pssAlgo = 0; #ifdef WOLFSSL_DTLS ssl->options.dtlsStateful = 0; #endif + #if defined(WOLFSSL_DTLS_CID) && defined(WOLFSSL_DTLS13) + ssl->options.haveSupportedVersions = 0; + #endif #ifdef WOLFSSL_TLS13 #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) ssl->options.pskNegotiated = 0; @@ -5944,22 +6012,6 @@ size_t wolfSSL_get_client_random(const WOLFSSL* ssl, unsigned char* out, #if defined(WOLFSSL_ASYNC_REINVOKE) && !defined(NO_HMAC) Tls13FreeHsHmac(ssl); #endif - #ifdef WOLFSSL_ASYNC_CRYPT - ssl->options.buildArgs13Set = 0; - /* An abandoned handshake can leave a mid-flight handler resume - * state and a queued key-schedule event behind; a reused object - * must start fresh. */ - ssl->options.asyncState = TLS_ASYNC_BEGIN; - if (ssl->asyncDev == &ssl->kdfAsyncDev) { - if (ssl->kdfAsyncDev.event.state == WOLF_EVENT_STATE_PENDING && - ssl->ctx != NULL) { - (void)wolfEventQueue_Remove(&ssl->ctx->event_queue, - &ssl->kdfAsyncDev.event); - } - XMEMSET(&ssl->kdfAsyncDev.event, 0, sizeof(WOLF_EVENT)); - ssl->asyncDev = NULL; - } - #endif #endif #ifdef HAVE_SESSION_TICKET #ifdef WOLFSSL_TLS13 @@ -6001,6 +6053,11 @@ size_t wolfSSL_get_client_random(const WOLFSSL* ssl, unsigned char* out, TLSX_CertificateAuthorities_FreeAll(ssl->ws_peer_ca_names, ssl->heap); ssl->ws_peer_ca_names = NULL; #endif + #if defined(HAVE_TLS_EXTENSIONS) && defined(HAVE_ALPN) + XFREE(ssl->alpn_peer_requested, ssl->heap, DYNAMIC_TYPE_ALPN); + ssl->alpn_peer_requested = NULL; + ssl->alpn_peer_requested_length = 0; + #endif #if defined(HAVE_TLS_EXTENSIONS) && !defined(NO_TLS) TLSX_FreeAll(ssl->extensions, ssl->heap); @@ -6028,6 +6085,12 @@ size_t wolfSSL_get_client_random(const WOLFSSL* ssl, unsigned char* out, ssl->buffers.clearOutputBuffer.length = 0; ssl->buffers.inputBuffer.idx = 0; ssl->buffers.inputBuffer.length = 0; + /* Output the last connection could not send is not the next peer's. */ + ShrinkOutputBuffer(ssl); + ssl->buffers.prevSent = 0; + ssl->buffers.plainSz = 0; + ssl->pendingAlert.code = 0; + ssl->pendingAlert.level = alert_none; /* Recycling the object for a new connection must not carry the * previous connection's key material along with it. A freshly * created object has all of this zeroed. */ @@ -6124,6 +6187,9 @@ size_t wolfSSL_get_client_random(const WOLFSSL* ssl, unsigned char* out, FreeCiphers(ssl); InitCiphers(ssl); + SetDefaultHmac(ssl); + ssl->encrypt.state = CIPHER_STATE_BEGIN; + ssl->decrypt.state = CIPHER_STATE_BEGIN; InitCipherSpecs(&ssl->specs); if (InitSSL_Suites(ssl) != WOLFSSL_SUCCESS) @@ -6147,6 +6213,11 @@ size_t wolfSSL_get_client_random(const WOLFSSL* ssl, unsigned char* out, return WOLFSSL_SUCCESS; } + int wolfSSL_clear(WOLFSSL* ssl) + { + return ClearSSL(ssl, 0); + } + #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || defined(HAVE_MEMCACHED) long wolfSSL_CTX_set_mode(WOLFSSL_CTX* ctx, long mode) { diff --git a/src/ssl_api_ext.c b/src/ssl_api_ext.c index 89f442536f2..c1cbc543589 100644 --- a/src/ssl_api_ext.c +++ b/src/ssl_api_ext.c @@ -1079,6 +1079,8 @@ static int wolfssl_rehandshake_prepare(WOLFSSL* ssl) XMEMSET(&ssl->msgsReceived, 0, sizeof(ssl->msgsReceived)); ssl->secure_renegotiation->cache_status = SCR_CACHE_NEEDED; + /* Freed at handshake end only when resources are not retained. */ + FreeEphemeralKeys(ssl); #if !defined(NO_WOLFSSL_SERVER) && !defined(WOLFSSL_NO_TLS12) if (ssl->options.side == WOLFSSL_SERVER_END) { diff --git a/src/ssl_api_rw.c b/src/ssl_api_rw.c index f8453a72501..3acf59bfc96 100644 --- a/src/ssl_api_rw.c +++ b/src/ssl_api_rw.c @@ -1184,7 +1184,7 @@ static int wolfssl_shutdown_internal(WOLFSSL* ssl, int allowInInit) #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL) /* reset WOLFSSL structure state for possible reuse */ if (ret == WOLFSSL_SUCCESS) { - if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) { + if (ClearSSL(ssl, 1) != WOLFSSL_SUCCESS) { WOLFSSL_MSG("could not clear WOLFSSL"); ret = WOLFSSL_FATAL_ERROR; } diff --git a/src/tls13.c b/src/tls13.c index c98185feb06..34dc1c8277f 100644 --- a/src/tls13.c +++ b/src/tls13.c @@ -6495,6 +6495,8 @@ int DoTls13ServerHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx, ssl->options.resuming = 0; ssl->arrays->psk_keySz = 0; XMEMSET(ssl->arrays->psk_key, 0, MAX_PSK_KEY_LEN); + if ((ret = ResetSessionForFullHandshake(ssl)) != 0) + return ret; } else { #if defined(HAVE_ECH) @@ -6521,10 +6523,16 @@ int DoTls13ServerHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx, if ((ret = SetupPskKey(ssl, psk, 0)) != 0) return ret; ssl->options.pskNegotiated = 1; + if (!psk->resumption && + ((ret = ResetSessionForFullHandshake(ssl)) != 0)) { + return ret; + } } #else /* no resumption possible */ ssl->options.resuming = 0; + if ((ret = ResetSessionForFullHandshake(ssl)) != 0) + return ret; #endif /* sanity check on PSK / KSE */ @@ -8511,6 +8519,11 @@ int DoTls13ClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx, if (ssl->clSuites->suiteSz > WOLFSSL_MAX_SUITE_SZ) ERROR_OUT(BUFFER_ERROR, exit_dch); XMEMCPY(ssl->clSuites->suites, input + args->idx, ssl->clSuites->suiteSz); +#ifdef OPENSSL_EXTRA + /* Built from the previous hello's list. */ + wolfSSL_sk_SSL_CIPHER_free(ssl->clSuitesStack); + ssl->clSuitesStack = NULL; +#endif args->idx += ssl->clSuites->suiteSz; ssl->clSuites->hashSigAlgoSz = 0; #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG) diff --git a/tests/api/test_tls.c b/tests/api/test_tls.c index 16a3a55b3d0..48d1d951dd5 100644 --- a/tests/api/test_tls.c +++ b/tests/api/test_tls.c @@ -5046,6 +5046,874 @@ int test_tls12_reuse_clears_use_ticket(void) return EXPECT_RESULT(); } +#ifdef HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES +#define TEST_CLEAR_PSK 0x01 /* ECDHE-PSK suite */ +#define TEST_CLEAR_TICKET 0x02 /* client asks for a session ticket */ +#define TEST_CLEAR_ALPN 0x04 /* client offers ALPN */ +#define TEST_CLEAR_SCR 0x08 /* secure renegotiation on both sides */ +#define TEST_CLEAR_STOP_C 0x10 /* client stops with its ClientHello unsent */ +#define TEST_CLEAR_STOP_S 0x20 /* server stops, its whole flight unsent */ +#define TEST_CLEAR_BAD_CA 0x40 /* client does not trust the server */ +#define TEST_CLEAR_CA_NAMES 0x80 /* both sides list CA names */ +#define TEST_CLEAR_DHE 0x100 /* DHE-RSA suite */ + +/* Compare an object reused with wolfSSL_clear() against one that never + * connected. Words that differ between two never-connected objects (pointers + * to their own allocations, generated values) say nothing and are skipped. */ +static int test_clear_cmp(const WOLFSSL* reused, const WOLFSSL* fresh, + const WOLFSSL* fresh2) +{ + EXPECT_DECLS; + WOLFSSL* r = NULL; + const byte* a; + const byte* b; + const byte* c; + word32 k; + int diffs = 0; + + ExpectNotNull(r = (WOLFSSL*)XMALLOC(sizeof(WOLFSSL), NULL, + DYNAMIC_TYPE_TMP_BUFFER)); + if ((r == NULL) || (reused == NULL) || (fresh == NULL) || + (fresh2 == NULL)) { + XFREE(r, NULL, DYNAMIC_TYPE_TMP_BUFFER); + return EXPECT_RESULT(); + } + XMEMCPY(r, reused, sizeof(WOLFSSL)); + + /* Negotiated results, kept on purpose for the accessors. */ + r->options.cipherSuite0 = fresh->options.cipherSuite0; + r->options.cipherSuite = fresh->options.cipherSuite; + r->options.hashAlgo = fresh->options.hashAlgo; + r->options.sigAlgo = fresh->options.sigAlgo; + r->options.peerHashAlgo = fresh->options.peerHashAlgo; + r->options.peerSigAlgo = fresh->options.peerSigAlgo; +#ifndef NO_DH + r->options.dhKeySz = fresh->options.dhKeySz; +#endif + /* wolfSSL_get_shutdown() reports a completed bidirectional shutdown + * from this after a clear, by documented intent. */ + r->options.shutdownDone = fresh->options.shutdownDone; + /* Reset only under WOLFSSL_ASYNC_CRYPT. */ + r->options.asyncState = fresh->options.asyncState; +#if !defined(NO_DH) && !defined(WOLFSSL_OLD_PRIME_CHECK) && \ + !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) + /* The parameters were checked, and are the same ones. */ + r->options.dhKeyTested = fresh->options.dhKeyTested; +#endif +#ifndef NO_DH + /* Not reset by wolfSSL_clear() yet. A server's own parameters may already + * be gone, freed at the end of a handshake or replaced by a client's FFDHE + * group, so what is left depends on the last connection. */ + r->buffers.serverDH_P = fresh->buffers.serverDH_P; + r->buffers.serverDH_G = fresh->buffers.serverDH_G; + r->buffers.weOwnDH = fresh->buffers.weOwnDH; + r->options.haveDH = fresh->options.haveDH; +#if !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(HAVE_FIPS) && \ + !defined(HAVE_SELFTEST) + r->options.dhDoKeyTest = fresh->options.dhDoKeyTest; +#endif +#endif + /* A client keeps its session for resumption. */ +#ifndef NO_CLIENT_CACHE + r->clientSession = fresh->clientSession; +#endif + /* Configuration, but a client also takes the ticket lifetime into it. */ + r->timeout = fresh->timeout; + /* Rebuilt in an allocation of its own. */ + r->suites = fresh->suites; + /* Kept for wolfSSL_get_client_ciphers() until the next ClientHello. */ + r->clSuites = fresh->clSuites; +#ifdef OPENSSL_EXTRA + r->clSuitesStack = fresh->clSuitesStack; +#endif + /* Released with the handshake and made again by the next one. */ + r->options.weOwnRng = fresh->options.weOwnRng; + /* Cursors and records of the last message, rewritten before use. */ + XMEMCPY(&r->buffers.inputBuffer, &fresh->buffers.inputBuffer, + sizeof(r->buffers.inputBuffer)); + r->buffers.sig.length = fresh->buffers.sig.length; +#if defined(HAVE_PK_CALLBACKS) && !defined(NO_RSA) + r->buffers.peerRsaKey.length = fresh->buffers.peerRsaKey.length; +#endif + r->hsType = fresh->hsType; + r->curSize = fresh->curSize; + r->curStartIdx = fresh->curStartIdx; + XMEMCPY(&r->curRL, &fresh->curRL, sizeof(r->curRL)); + XMEMCPY(&r->chVersion, &fresh->chVersion, sizeof(r->chVersion)); +#ifdef OPENSSL_EXTRA + r->cbmode = fresh->cbmode; +#endif + + a = (const byte*)r; + b = (const byte*)fresh; + c = (const byte*)fresh2; + for (k = 0; k + sizeof(void*) <= sizeof(WOLFSSL); k += sizeof(void*)) { + if ((XMEMCMP(b + k, c + k, sizeof(void*)) == 0) && + (XMEMCMP(a + k, b + k, sizeof(void*)) != 0)) { + word32 i; + /* Offsets rather than names, as for the Options bitfields. */ + fprintf(stderr, "%s WOLFSSL differs at offset %u: reused", + (reused->options.side == WOLFSSL_CLIENT_END) ? "client" : + "server", k); + for (i = 0; i < (word32)sizeof(void*); i++) + fprintf(stderr, " %02x", a[k + i]); + fprintf(stderr, " fresh"); + for (i = 0; i < (word32)sizeof(void*); i++) + fprintf(stderr, " %02x", b[k + i]); + fprintf(stderr, "\n"); + diffs++; + } + } + ExpectIntEQ(diffs, 0); + + XFREE(r, NULL, DYNAMIC_TYPE_TMP_BUFFER); + return EXPECT_RESULT(); +} + +static void test_clear_cfg(WOLFSSL* ssl_c, WOLFSSL* ssl_s, int flags) +{ +#if !defined(NO_PSK) && defined(BUILD_TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256) + /* The CTX suite list predates the callback; this rebuilds the object's. */ + if ((flags & TEST_CLEAR_PSK) && (ssl_s != NULL)) + wolfSSL_set_psk_server_callback(ssl_s, test_clear_psk_server_cb); +#endif +#ifdef HAVE_ALPN + /* On the objects only, as wolfSSL_clear() drops extensions. */ + if (flags & TEST_CLEAR_ALPN) { + (void)wolfSSL_UseALPN(ssl_c, (char*)"first-proto", 11, + WOLFSSL_ALPN_CONTINUE_ON_MISMATCH); + (void)wolfSSL_UseALPN(ssl_s, (char*)"other-proto", 11, + WOLFSSL_ALPN_CONTINUE_ON_MISMATCH); + } +#endif + (void)ssl_c; + (void)ssl_s; + (void)flags; +} + +static int test_clear_reuse(method_provider cm, method_provider sm, int flags) +{ + EXPECT_DECLS; + WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL; + WOLFSSL *ssl_c = NULL, *ssl_s = NULL; + WOLFSSL *fresh_c = NULL, *fresh_s = NULL, *fresh_c2 = NULL; + WOLFSSL *fresh_s2 = NULL; + struct test_memio_ctx test_ctx; + char msg[] = "test"; + char reply[sizeof(msg)]; +#ifdef OPENSSL_ALL + WOLF_STACK_OF(WOLFSSL_CIPHER)* clCiphers = NULL; + int clCiphersNum = 0; + const char* cipherName = NULL; +#endif +#if defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) + WOLF_STACK_OF(WOLFSSL_X509)* peerChain = NULL; + int peerChainNum = 0; +#endif + const char* verC = NULL; + const char* verS = NULL; + + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, NULL, NULL, + cm, sm), 0); + /* On the CTXs, so the never-connected objects are configured alike. */ +#if !defined(NO_PSK) && defined(BUILD_TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256) + if (flags & TEST_CLEAR_PSK) { + wolfSSL_CTX_set_psk_client_callback(ctx_c, test_clear_psk_client_cb); + wolfSSL_CTX_set_psk_server_callback(ctx_s, test_clear_psk_server_cb); + ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx_c, + "ECDHE-PSK-AES128-GCM-SHA256"), WOLFSSL_SUCCESS); + } +#endif +#if !defined(NO_DH) && defined(BUILD_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256) + if (flags & TEST_CLEAR_DHE) { + ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx_c, + "DHE-RSA-AES128-GCM-SHA256"), WOLFSSL_SUCCESS); + } +#endif + /* So the whole flight, key exchange included, is made before the stop. */ + if (flags & TEST_CLEAR_STOP_S) + ExpectIntEQ(wolfSSL_CTX_set_group_messages(ctx_s), WOLFSSL_SUCCESS); +#if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) + if (flags & TEST_CLEAR_TICKET) + ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS); +#endif +#ifdef HAVE_SECURE_RENEGOTIATION + if (flags & TEST_CLEAR_SCR) { + ExpectIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx_c), + WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx_s), + WOLFSSL_SUCCESS); + } +#endif +#if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_CA_NAMES) && \ + !defined(NO_BIO) && !defined(NO_FILESYSTEM) + if (flags & TEST_CLEAR_CA_NAMES) { + /* The server's go in its CertificateRequest, so it asks for one. */ + wolfSSL_CTX_set_verify(ctx_s, WOLFSSL_VERIFY_PEER, NULL); + wolfSSL_CTX_set_client_CA_list(ctx_s, + wolfSSL_load_client_CA_file(caCertFile)); + wolfSSL_CTX_set0_CA_list(ctx_c, + wolfSSL_load_client_CA_file(caCertFile)); + } +#endif +#if !defined(NO_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) + if (flags & TEST_CLEAR_BAD_CA) { + /* Self-signed, so the client finds no signer. */ + ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, cliCertFile, + CERT_FILETYPE), WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, cliKeyFile, + CERT_FILETYPE), WOLFSSL_SUCCESS); + wolfSSL_CTX_set_verify(ctx_c, WOLFSSL_VERIFY_PEER, NULL); + } +#endif + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, + cm, sm), 0); + test_clear_cfg(ssl_c, ssl_s, flags); + + if (flags & TEST_CLEAR_STOP_C) { + test_memio_simulate_want_write(&test_ctx, 1, 1); + ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR); + ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR), + WOLFSSL_ERROR_WANT_WRITE); + test_memio_simulate_want_write(&test_ctx, 1, 0); + } + else if (flags & TEST_CLEAR_STOP_S) { + ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR); + ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR), + WOLFSSL_ERROR_WANT_READ); + test_memio_simulate_want_write(&test_ctx, 0, 1); + ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR); + ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR), + WOLFSSL_ERROR_WANT_WRITE); + test_memio_simulate_want_write(&test_ctx, 0, 0); + /* Left for wolfSSL_clear() alone to release. */ + #ifndef NO_DH + if ((flags & TEST_CLEAR_DHE) && (ssl_s != NULL)) + ExpectNotNull(ssl_s->buffers.serverDH_Priv.buffer); + #endif + #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \ + defined(HAVE_CURVE448) + if (!(flags & TEST_CLEAR_DHE) && (ssl_s != NULL)) + ExpectNotNull(ssl_s->eccTempKey); + #endif + } + else if (flags & TEST_CLEAR_BAD_CA) { + int err; + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), -1); + /* Some builds report the certificate as self-signed instead. */ + err = wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR); + ExpectTrue((err == WC_NO_ERR_TRACE(ASN_NO_SIGNER_E)) || + (err == WC_NO_ERR_TRACE(ASN_SELF_SIGNED_E))); + } + else { + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); + /* Fill what the accessors cache. */ + #if defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) + if (!(flags & TEST_CLEAR_PSK)) { + ExpectNotNull(peerChain = wolfSSL_get_peer_cert_chain(ssl_c)); + ExpectIntGT(peerChainNum = wolfSSL_sk_X509_num(peerChain), 0); + } + #endif + verC = wolfSSL_get_version(ssl_c); + verS = wolfSSL_get_version(ssl_s); + #ifdef OPENSSL_ALL + ExpectNotNull(clCiphers = wolfSSL_get_client_ciphers(ssl_s)); + clCiphersNum = wolfSSL_sk_SSL_CIPHER_num(clCiphers); + ExpectIntGT(clCiphersNum, 0); + ExpectNotNull(cipherName = wolfSSL_get_cipher_name(ssl_c)); + #endif + /* Data both ways and a bidirectional close, so record-layer and + * shutdown state is covered too, not just the handshake. */ + ExpectIntEQ(wolfSSL_write(ssl_c, msg, (int)sizeof(msg)), + (int)sizeof(msg)); + ExpectIntEQ(wolfSSL_read(ssl_s, reply, (int)sizeof(reply)), + (int)sizeof(msg)); + ExpectIntEQ(wolfSSL_write(ssl_s, msg, (int)sizeof(msg)), + (int)sizeof(msg)); + ExpectIntEQ(wolfSSL_read(ssl_c, reply, (int)sizeof(reply)), + (int)sizeof(msg)); + ExpectIntEQ(wolfSSL_shutdown(ssl_c), WOLFSSL_SHUTDOWN_NOT_DONE); + ExpectIntEQ(wolfSSL_read(ssl_s, reply, (int)sizeof(reply)), 0); + ExpectIntEQ(wolfSSL_shutdown(ssl_s), WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_shutdown(ssl_c), WOLFSSL_SUCCESS); + /* Still queryable after the shutdown, until the clear. */ + ExpectStrEQ(wolfSSL_get_version(ssl_c), verC); + ExpectStrEQ(wolfSSL_get_version(ssl_s), verS); + #if defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) + if (peerChain != NULL) { + ExpectPtrEq(wolfSSL_get_peer_cert_chain(ssl_c), peerChain); + ExpectIntEQ(wolfSSL_sk_X509_num(peerChain), peerChainNum); + } + #endif + } + + /* Created after the connection so the CTXs are fully configured. */ + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &fresh_c, &fresh_s, + cm, sm), 0); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &fresh_c2, + &fresh_s2, cm, sm), 0); + ExpectIntEQ(wolfSSL_clear(ssl_c), WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_clear(ssl_s), WOLFSSL_SUCCESS); + if (EXPECT_SUCCESS()) { + /* Both before asserting, so a failure reports both sides. */ + int cmpC = test_clear_cmp(ssl_c, fresh_c, fresh_c2); + int cmpS = test_clear_cmp(ssl_s, fresh_s, fresh_s2); + ExpectIntEQ(cmpC, TEST_SUCCESS); + ExpectIntEQ(cmpS, TEST_SUCCESS); + } +#ifdef OPENSSL_ALL + /* Still valid after the shutdown and the clear, as in OpenSSL. */ + if (clCiphers != NULL) { + ExpectPtrEq(wolfSSL_get_client_ciphers(ssl_s), clCiphers); + ExpectIntEQ(wolfSSL_sk_SSL_CIPHER_num(clCiphers), clCiphersNum); + } + /* The next client offers one suite. */ + if (cipherName != NULL) + ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, cipherName), + WOLFSSL_SUCCESS); +#endif + + if (!(flags & TEST_CLEAR_BAD_CA)) { + /* Nothing of the last connection reaches the next one's peer. */ + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + test_clear_cfg(NULL, ssl_s, flags & TEST_CLEAR_PSK); + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); + } +#ifdef OPENSSL_ALL + if (cipherName != NULL) { + /* What a never-connected server reports for the same client. */ + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + test_clear_cfg(NULL, fresh_s, flags & TEST_CLEAR_PSK); + ExpectIntEQ(wolfSSL_set_cipher_list(fresh_c, cipherName), + WOLFSSL_SUCCESS); + ExpectIntEQ(test_memio_do_handshake(fresh_c, fresh_s, 10, NULL), 0); + ExpectIntEQ(wolfSSL_sk_SSL_CIPHER_num( + wolfSSL_get_client_ciphers(ssl_s)), wolfSSL_sk_SSL_CIPHER_num( + wolfSSL_get_client_ciphers(fresh_s))); + } +#endif + + wolfSSL_free(fresh_c); + wolfSSL_free(fresh_s); + wolfSSL_free(fresh_c2); + wolfSSL_free(fresh_s2); + wolfSSL_free(ssl_c); + wolfSSL_free(ssl_s); + wolfSSL_CTX_free(ctx_c); + wolfSSL_CTX_free(ctx_s); + return EXPECT_RESULT(); +} + +#if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) +/* A version the application turned off stays off on reuse. */ +static int test_clear_keeps_version_mask(void) +{ + EXPECT_DECLS; + struct test_memio_ctx test_ctx; + WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL; + WOLFSSL *ssl_c = NULL, *ssl_s = NULL; + int i; + + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, NULL, &ssl_s, + wolfSSLv23_client_method, wolfSSLv23_server_method), 0); + if (ssl_s != NULL) + (void)wolfSSL_set_options(ssl_s, WOLFSSL_OP_NO_TLSv1_3); + for (i = 0; i < 2; i++) { + /* A new client each time, so none offers a TLS 1.2 session. */ + wolfSSL_free(ssl_c); + ssl_c = NULL; + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, NULL, + wolfSSLv23_client_method, wolfSSLv23_server_method), 0); + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); + if (ssl_s != NULL) + ExpectIntEQ(ssl_s->version.minor, TLSv1_2_MINOR); + ExpectIntEQ(wolfSSL_clear(ssl_s), WOLFSSL_SUCCESS); + if (ssl_s != NULL) + ExpectIntEQ(ssl_s->version.minor, TLSv1_2_MINOR); + } + + wolfSSL_free(ssl_c); + wolfSSL_free(ssl_s); + wolfSSL_CTX_free(ctx_c); + wolfSSL_CTX_free(ctx_s); + return EXPECT_RESULT(); +} +#endif +#endif + +#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA) && \ + !defined(NO_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \ + !defined(WOLFSSL_NO_TLS12) +static int test_clear_verify_accept(int preverify, + WOLFSSL_X509_STORE_CTX* store) +{ + (void)preverify; + (void)store; + return 1; +} + +/* The shutdown keeps the verify result and alert history for the + * application, and the next connection starts without them. */ +static int test_shutdown_keeps_peer_results(void) +{ + EXPECT_DECLS; + struct test_memio_ctx test_ctx; + WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL, *ctx_s2 = NULL; + WOLFSSL *ssl_c = NULL, *ssl_s = NULL, *ssl_s2 = NULL; + WOLFSSL_ALERT_HISTORY h; + long verifyRet = 0; + char buf[1]; + + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, NULL, NULL, + wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0); + /* Self-signed, so the client finds no signer. */ + ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, cliCertFile, + CERT_FILETYPE), WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, cliKeyFile, + CERT_FILETYPE), WOLFSSL_SUCCESS); + wolfSSL_CTX_set_verify(ctx_c, WOLFSSL_VERIFY_PEER, + test_clear_verify_accept); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, + wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0); + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); + ExpectIntNE(verifyRet = wolfSSL_get_verify_result(ssl_c), X509_V_OK); + + ExpectIntEQ(wolfSSL_shutdown(ssl_c), WOLFSSL_SHUTDOWN_NOT_DONE); + ExpectIntEQ(wolfSSL_read(ssl_s, buf, (int)sizeof(buf)), 0); + ExpectIntEQ(wolfSSL_shutdown(ssl_s), WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_shutdown(ssl_c), WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_get_verify_result(ssl_c), verifyRet); + ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS); + ExpectIntEQ(h.last_tx.code, close_notify); + ExpectIntEQ(h.last_rx.code, close_notify); + + /* Reused without wolfSSL_clear(), with a server the client trusts. */ + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s2, NULL, &ssl_s2, + wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0); + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s2, 10, NULL), 0); + ExpectIntEQ(wolfSSL_get_verify_result(ssl_c), X509_V_OK); + ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS); + ExpectIntEQ(h.last_tx.code, -1); + ExpectIntEQ(h.last_rx.code, -1); + + wolfSSL_free(ssl_c); + wolfSSL_free(ssl_s); + wolfSSL_free(ssl_s2); + wolfSSL_CTX_free(ctx_c); + wolfSSL_CTX_free(ctx_s); + wolfSSL_CTX_free(ctx_s2); + return EXPECT_RESULT(); +} +#endif + +/* Catches per-connection state that wolfSSL_clear() forgets, rather than one + * field at a time, after connections that end in different ways. Every + * WOLFSSL is born inside XMEMSET(ssl, 0, sizeof(WOLFSSL)), so padding and + * bitfield slack are zero in both objects and only written fields can + * differ. */ +int test_tls_clear_resets_state(void) +{ + EXPECT_DECLS; +#ifdef HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES +#ifndef WOLFSSL_NO_TLS12 + ExpectIntEQ(test_clear_reuse(wolfTLSv1_2_client_method, + wolfTLSv1_2_server_method, + TEST_CLEAR_TICKET | TEST_CLEAR_ALPN | TEST_CLEAR_CA_NAMES), + TEST_SUCCESS); +#if !defined(NO_PSK) && defined(BUILD_TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256) + /* A PSK suite leaves different state behind. */ + ExpectIntEQ(test_clear_reuse(wolfTLSv1_2_client_method, + wolfTLSv1_2_server_method, TEST_CLEAR_PSK), TEST_SUCCESS); +#endif +#ifdef HAVE_SECURE_RENEGOTIATION + /* Keeps handshake resources past the handshake. */ + ExpectIntEQ(test_clear_reuse(wolfTLSv1_2_client_method, + wolfTLSv1_2_server_method, TEST_CLEAR_SCR), TEST_SUCCESS); +#endif + ExpectIntEQ(test_clear_reuse(wolfTLSv1_2_client_method, + wolfTLSv1_2_server_method, TEST_CLEAR_STOP_S), TEST_SUCCESS); +#if !defined(NO_DH) && defined(BUILD_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256) + /* Stopped with the server's DH key pair made. */ + ExpectIntEQ(test_clear_reuse(wolfTLSv1_2_client_method, + wolfTLSv1_2_server_method, TEST_CLEAR_DHE | TEST_CLEAR_STOP_S), + TEST_SUCCESS); +#ifdef HAVE_SECURE_RENEGOTIATION + ExpectIntEQ(test_clear_reuse(wolfTLSv1_2_client_method, + wolfTLSv1_2_server_method, TEST_CLEAR_DHE | TEST_CLEAR_SCR), + TEST_SUCCESS); +#endif +#endif +#ifdef WOLFSSL_TLS13 + ExpectIntEQ(test_clear_keeps_version_mask(), TEST_SUCCESS); + /* Each side with a peer that only does TLS 1.2. */ + ExpectIntEQ(test_clear_reuse(wolfSSLv23_client_method, + wolfTLSv1_2_server_method, 0), TEST_SUCCESS); + ExpectIntEQ(test_clear_reuse(wolfTLSv1_2_client_method, + wolfSSLv23_server_method, 0), TEST_SUCCESS); +#endif +#endif /* !WOLFSSL_NO_TLS12 */ +#ifdef WOLFSSL_TLS13 + ExpectIntEQ(test_clear_reuse(wolfTLSv1_3_client_method, + wolfTLSv1_3_server_method, TEST_CLEAR_ALPN | TEST_CLEAR_CA_NAMES), + TEST_SUCCESS); +#endif + ExpectIntEQ(test_clear_reuse(wolfSSLv23_client_method, + wolfSSLv23_server_method, TEST_CLEAR_STOP_C), TEST_SUCCESS); +#if !defined(NO_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) + ExpectIntEQ(test_clear_reuse(wolfSSLv23_client_method, + wolfSSLv23_server_method, TEST_CLEAR_BAD_CA), TEST_SUCCESS); +#endif +#if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \ + !defined(NO_CERTS) && !defined(WOLFSSL_NO_TLS12) + ExpectIntEQ(test_shutdown_keeps_peer_results(), TEST_SUCCESS); +#endif +#endif + return EXPECT_RESULT(); +} + +#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \ + defined(HAVE_SECURE_RENEGOTIATION) && !defined(WOLFSSL_NO_TLS12) +/* The server's ephemeral public key, once its flight is out. */ +static int test_scr_server_pub(WOLFSSL* ssl, byte* pub, word32* pubSz) +{ + EXPECT_DECLS; + + ExpectNotNull(ssl); + if (ssl == NULL) + return EXPECT_RESULT(); +#if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) + if (ssl->specs.kea == ecc_diffie_hellman_kea) { + /* The client offers only P-256. */ + ExpectIntEQ(ssl->eccTempKeyPresent, DYNAMIC_TYPE_ECC); + if (EXPECT_SUCCESS()) + ExpectIntEQ(wc_ecc_export_x963(ssl->eccTempKey, pub, pubSz), 0); + } + else +#endif +#ifndef NO_DH + if (ssl->specs.kea == diffie_hellman_kea) { + ExpectNotNull(ssl->buffers.serverDH_Pub.buffer); + ExpectIntLE(ssl->buffers.serverDH_Pub.length, *pubSz); + if (EXPECT_SUCCESS()) { + *pubSz = ssl->buffers.serverDH_Pub.length; + XMEMCPY(pub, ssl->buffers.serverDH_Pub.buffer, *pubSz); + } + } + else +#endif + { + ExpectTrue(0); + } + return EXPECT_RESULT(); +} + +/* Nothing of a handshake's key exchange is left once it is over. */ +static int test_scr_no_ephemeral(WOLFSSL* ssl) +{ + EXPECT_DECLS; + + ExpectNotNull(ssl); + if (ssl != NULL) { + #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \ + defined(HAVE_CURVE448) + ExpectNull(ssl->eccTempKey); + #endif + #ifndef NO_DH + ExpectNull(ssl->buffers.serverDH_Priv.buffer); + ExpectNull(ssl->buffers.serverDH_Key); + #endif + } + return EXPECT_RESULT(); +} + +/* first lists two suites, second one of them. keep retains the handshake + * resources, so only a new handshake can free the keys. */ +static int test_tls12_scr_fresh_state_ex(const char* first, const char* second, + byte keep) +{ + EXPECT_DECLS; + struct test_memio_ctx test_ctx; + WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL; + WOLFSSL *ssl_c = NULL, *ssl_s = NULL; + byte pub1[1024]; + byte pub2[1024]; + word32 pub1Sz = (word32)sizeof(pub1); + word32 pub2Sz = (word32)sizeof(pub2); + byte buf[1]; + + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + test_ctx.c_ciphers = first; + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, + wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0); +#if !defined(NO_DH) && !defined(NO_FILESYSTEM) + ExpectIntEQ(wolfSSL_SetTmpDH_file(ssl_s, dhParamFile, CERT_FILETYPE), + WOLFSSL_SUCCESS); +#endif +#if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES) && \ + (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) + ExpectIntEQ(wolfSSL_UseSupportedCurve(ssl_c, WOLFSSL_ECC_SECP256R1), + WOLFSSL_SUCCESS); +#endif + ExpectIntEQ(wolfSSL_UseSecureRenegotiation(ssl_c), WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_UseSecureRenegotiation(ssl_s), WOLFSSL_SUCCESS); + if (keep) + ExpectIntEQ(wolfSSL_KeepHandshakeResources(ssl_s), 0); + + ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR); + ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR), + WOLFSSL_ERROR_WANT_READ); + ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR); + ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR), + WOLFSSL_ERROR_WANT_READ); + ExpectIntEQ(test_scr_server_pub(ssl_s, pub1, &pub1Sz), TEST_SUCCESS); + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); + if (!keep) + ExpectIntEQ(test_scr_no_ephemeral(ssl_s), TEST_SUCCESS); +#ifdef OPENSSL_ALL + ExpectIntEQ(wolfSSL_sk_SSL_CIPHER_num(wolfSSL_get_client_ciphers(ssl_s)), + 2); +#endif + + ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, second), WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_Rehandshake(ssl_c), WOLFSSL_FATAL_ERROR); + ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR), + WOLFSSL_ERROR_WANT_READ); + ExpectIntEQ(wolfSSL_read(ssl_s, buf, (int)sizeof(buf)), + WOLFSSL_FATAL_ERROR); + ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR), + WOLFSSL_ERROR_WANT_READ); + ExpectIntEQ(test_scr_server_pub(ssl_s, pub2, &pub2Sz), TEST_SUCCESS); + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); + if (!keep) + ExpectIntEQ(test_scr_no_ephemeral(ssl_s), TEST_SUCCESS); +#ifdef OPENSSL_ALL + /* The renegotiating hello's list, not the first one's. */ + ExpectIntEQ(wolfSSL_sk_SSL_CIPHER_num(wolfSSL_get_client_ciphers(ssl_s)), + 1); +#endif + /* A DH public value can be a byte shorter. */ + ExpectTrue((pub2Sz != pub1Sz) || (XMEMCMP(pub2, pub1, pub1Sz) != 0)); + + wolfSSL_free(ssl_c); + wolfSSL_free(ssl_s); + wolfSSL_CTX_free(ctx_c); + wolfSSL_CTX_free(ctx_s); + return EXPECT_RESULT(); +} +#endif + +/* A renegotiation is a new handshake: the server makes new ephemeral keys + * for it, keeps none between handshakes, and reports the new hello. */ +int test_tls12_scr_fresh_state(void) +{ + EXPECT_DECLS; +#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \ + defined(HAVE_SECURE_RENEGOTIATION) && !defined(WOLFSSL_NO_TLS12) +#if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \ + defined(HAVE_SUPPORTED_CURVES) && \ + (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \ + defined(BUILD_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) && \ + defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) + ExpectIntEQ(test_tls12_scr_fresh_state_ex( + "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384", + "ECDHE-RSA-AES256-GCM-SHA384", 0), TEST_SUCCESS); + ExpectIntEQ(test_tls12_scr_fresh_state_ex( + "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384", + "ECDHE-RSA-AES256-GCM-SHA384", 1), TEST_SUCCESS); +#endif +#if !defined(NO_DH) && !defined(NO_FILESYSTEM) && \ + defined(BUILD_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256) && \ + defined(BUILD_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384) + ExpectIntEQ(test_tls12_scr_fresh_state_ex( + "DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384", + "DHE-RSA-AES256-GCM-SHA384", 0), TEST_SUCCESS); +#endif +#endif + return EXPECT_RESULT(); +} + +/* A full handshake on an object reused with wolfSSL_clear() makes a new + * session. The last connection's ticket must not stay beside the new secret, + * and a session held from before the clear must still resume. */ +int test_tls13_clear_full_handshake_session(void) +{ + EXPECT_DECLS; +#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \ + defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \ + !defined(NO_SESSION_CACHE) + WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL, *ctx_s2 = NULL; + WOLFSSL *ssl_c = NULL, *ssl_s = NULL, *ssl_s2 = NULL; + WOLFSSL *ssl_c3 = NULL, *ssl_s3 = NULL; + WOLFSSL_SESSION* held = NULL; + WOLFSSL_SESSION* after = NULL; + struct test_memio_ctx test_ctx; + char buf[8]; + + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, + wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0); + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); + /* Takes the server's ticket. */ + ExpectIntEQ(wolfSSL_read(ssl_c, buf, sizeof(buf)), -1); + ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ); + ExpectNotNull(held = wolfSSL_get1_session(ssl_c)); + if (held != NULL) + ExpectIntNE(held->ticketLen, 0); + ExpectIntEQ(wolfSSL_clear(ssl_c), WOLFSSL_SUCCESS); + + /* A server with other ticket keys, whose ticket is not read. */ + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s2, NULL, &ssl_s2, + wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0); + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s2, 10, NULL), 0); + ExpectIntEQ(wolfSSL_session_reused(ssl_c), 0); + if (ssl_c != NULL) { + ExpectIntEQ(ssl_c->session->ticketLen, 0); + ExpectIntEQ(ssl_c->session->sessionIDSz, 0); + } + ExpectNotNull(after = wolfSSL_get1_session(ssl_c)); + + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c3, &ssl_s3, + wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0); + ExpectIntEQ(wolfSSL_set_session(ssl_c3, held), WOLFSSL_SUCCESS); + ExpectIntEQ(test_memio_do_handshake(ssl_c3, ssl_s3, 10, NULL), 0); + ExpectIntEQ(wolfSSL_session_reused(ssl_c3), 1); + wolfSSL_free(ssl_c3); + ssl_c3 = NULL; + wolfSSL_free(ssl_s3); + ssl_s3 = NULL; + + /* Nothing to resume, so a full handshake rather than a failed one. */ + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c3, &ssl_s3, + wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0); + ExpectIntEQ(wolfSSL_set_session(ssl_c3, after), WOLFSSL_SUCCESS); + ExpectIntEQ(test_memio_do_handshake(ssl_c3, ssl_s3, 10, NULL), 0); + ExpectIntEQ(wolfSSL_session_reused(ssl_c3), 0); + + wolfSSL_SESSION_free(held); + wolfSSL_SESSION_free(after); + wolfSSL_free(ssl_c3); + wolfSSL_free(ssl_s3); + wolfSSL_free(ssl_c); + wolfSSL_free(ssl_s); + wolfSSL_free(ssl_s2); + wolfSSL_CTX_free(ctx_c); + wolfSSL_CTX_free(ctx_s); + wolfSSL_CTX_free(ctx_s2); +#endif + return EXPECT_RESULT(); +} + +/* An external PSK handshake on a reused client is not a resumption of the + * last connection's session, so its ticket must not stay beside the new + * secret. */ +int test_tls13_clear_external_psk_session(void) +{ + EXPECT_DECLS; +#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \ + defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \ + !defined(NO_PSK) + WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL, *ctx_s2 = NULL; + WOLFSSL *ssl_c = NULL, *ssl_s = NULL, *ssl_s2 = NULL; + struct test_memio_ctx test_ctx; + char buf[8]; + + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, + wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0); + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); + /* Takes the server's ticket. */ + ExpectIntEQ(wolfSSL_read(ssl_c, buf, sizeof(buf)), -1); + ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ); + if (ssl_c != NULL) + ExpectIntGT(ssl_c->session->ticketLen, 0); + ExpectIntEQ(wolfSSL_clear(ssl_c), WOLFSSL_SUCCESS); + + /* A server with other ticket keys, whose ticket is not read. */ + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s2, NULL, &ssl_s2, + wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0); + wolfSSL_set_psk_client_callback(ssl_c, my_psk_client_cb); + wolfSSL_set_psk_server_callback(ssl_s2, my_psk_server_cb); + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s2, 10, NULL), 0); + if (ssl_c != NULL) { + ExpectIntEQ(ssl_c->options.pskNegotiated, 1); + ExpectIntEQ(ssl_c->session->ticketLen, 0); + ExpectIntEQ(ssl_c->session->sessionIDSz, 0); + } + + wolfSSL_free(ssl_c); + wolfSSL_free(ssl_s); + wolfSSL_free(ssl_s2); + wolfSSL_CTX_free(ctx_c); + wolfSSL_CTX_free(ctx_s); + wolfSSL_CTX_free(ctx_s2); +#endif + return EXPECT_RESULT(); +} + +/* Clearing a server whose key generation is still pending leaves no event + * queued for the freed key, and the object can be used again. */ +int test_tls12_clear_pending_key(void) +{ + EXPECT_DECLS; +#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \ + defined(WOLFSSL_ASYNC_CRYPT_SW) && defined(WC_ASYNC_ENABLE_ECC) && \ + !defined(WOLFSSL_NO_TLS12) && defined(HAVE_ECC) && \ + defined(BUILD_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) + WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL; + WOLFSSL *ssl_c = NULL, *ssl_s = NULL, *ssl_c2 = NULL; + struct test_memio_ctx test_ctx; + WOLF_EVENT* events[4]; + int count = -1; + int devId = INVALID_DEVID; + + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + test_ctx.c_ciphers = test_ctx.s_ciphers = "ECDHE-RSA-AES128-GCM-SHA256"; + ExpectIntGE(wolfAsync_DevOpen(&devId), 0); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, + wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0); + ExpectIntEQ(wolfSSL_SetDevId(ssl_s, devId), WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR); + ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR), + WOLFSSL_ERROR_WANT_READ); + ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR); + ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR), + WC_NO_ERR_TRACE(WC_PENDING_E)); + if (ssl_s != NULL) { + ExpectNotNull(ssl_s->asyncDev); + ExpectNotNull(ssl_s->eccTempKey); + } + + ExpectIntEQ(wolfSSL_clear(ssl_s), WOLFSSL_SUCCESS); + if (ssl_s != NULL) + ExpectNull(ssl_s->asyncDev); + ExpectIntEQ(wolfSSL_CTX_AsyncPoll(ctx_s, events, 4, + WOLF_POLL_FLAG_CHECK_HW, &count), 0); + ExpectIntEQ(count, 0); + + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c2, NULL, + wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0); + ExpectIntEQ(test_memio_do_handshake(ssl_c2, ssl_s, 10, NULL), 0); + + wolfSSL_free(ssl_c); + wolfSSL_free(ssl_c2); + wolfSSL_free(ssl_s); + wolfSSL_CTX_free(ctx_c); + wolfSSL_CTX_free(ctx_s); + wolfAsync_DevClose(&devId); +#endif + return EXPECT_RESULT(); +} + /* A ticket the server can't honor must fall back to a full handshake (RFC 5077 * 3.4), even under a different suite than the cached ticket session - the * F-5811 suite check must not abort it. The second handshake uses a fresh diff --git a/tests/api/test_tls.h b/tests/api/test_tls.h index 1dc1563da69..ccef1e55746 100644 --- a/tests/api/test_tls.h +++ b/tests/api/test_tls.h @@ -84,6 +84,11 @@ int test_tls13_pha_unsolicited_blank_cert(void); int test_tls13_unsolicited_cert_not_recorded(void); int test_tls12_resume_ticket_client_auth_ok(void); int test_tls12_reuse_clears_use_ticket(void); +int test_tls_clear_resets_state(void); +int test_tls12_scr_fresh_state(void); +int test_tls13_clear_full_handshake_session(void); +int test_tls13_clear_external_psk_session(void); +int test_tls12_clear_pending_key(void); int test_tls12_resume_ticket_decline_fallback(void); int test_tls12_ticket_dropped_on_bad_finished(void); int test_tls12_ticket_cached_after_finished(void); @@ -202,6 +207,11 @@ int test_tls_unset_fd_no_stdin_io(void); TEST_DECL_GROUP("tls", test_tls13_unsolicited_cert_not_recorded), \ TEST_DECL_GROUP("tls", test_tls12_resume_ticket_client_auth_ok), \ TEST_DECL_GROUP("tls", test_tls12_reuse_clears_use_ticket), \ + TEST_DECL_GROUP("tls", test_tls_clear_resets_state), \ + TEST_DECL_GROUP("tls", test_tls12_scr_fresh_state), \ + TEST_DECL_GROUP("tls", test_tls13_clear_full_handshake_session), \ + TEST_DECL_GROUP("tls", test_tls13_clear_external_psk_session), \ + TEST_DECL_GROUP("tls", test_tls12_clear_pending_key), \ TEST_DECL_GROUP("tls", test_tls12_resume_ticket_decline_fallback), \ TEST_DECL_GROUP("tls", test_tls12_ticket_dropped_on_bad_finished), \ TEST_DECL_GROUP("tls", test_tls12_ticket_cached_after_finished), \ diff --git a/wolfssl/internal.h b/wolfssl/internal.h index 8497db9a4a6..4c04892a5eb 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -7755,6 +7755,8 @@ do { \ } while (0) WOLFSSL_LOCAL int SetSSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup); +WOLFSSL_LOCAL int SetSSL_CTX_CheckVersion(WOLFSSL* ssl, WOLFSSL_CTX* ctx); +WOLFSSL_LOCAL int ClearSSL(WOLFSSL* ssl, byte keepResults); WOLFSSL_LOCAL int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup); WOLFSSL_LOCAL int ReinitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup); WOLFSSL_LOCAL void FreeSSL(WOLFSSL* ssl, void* heap); @@ -7985,6 +7987,12 @@ WOLFSSL_LOCAL int IsEncryptionOn(const WOLFSSL* ssl, int isSend); WOLFSSL_LOCAL int TLSv1_3_Capable(WOLFSSL* ssl); WOLFSSL_LOCAL void FreeHandshakeResources(WOLFSSL* ssl); +WOLFSSL_LOCAL void FreeEphemeralKeys(WOLFSSL* ssl); +WOLFSSL_LOCAL void ResetPeerResults(WOLFSSL* ssl); +WOLFSSL_LOCAL void SetDefaultHmac(WOLFSSL* ssl); +#ifdef WOLFSSL_ASYNC_CRYPT +WOLFSSL_LOCAL int AsyncDetachEvent(WOLFSSL* ssl); +#endif WOLFSSL_LOCAL void ShrinkInputBuffer(WOLFSSL* ssl, int forcedFree); WOLFSSL_LOCAL void ShrinkOutputBuffer(WOLFSSL* ssl); WOLFSSL_LOCAL byte* GetOutputBuffer(WOLFSSL* ssl); @@ -8147,6 +8155,7 @@ WOLFSSL_LOCAL word32 MacSize(const WOLFSSL* ssl); #ifndef NO_WOLFSSL_CLIENT WOLFSSL_LOCAL int HaveUniqueSessionObj(WOLFSSL* ssl); + WOLFSSL_LOCAL int ResetSessionForFullHandshake(WOLFSSL* ssl); WOLFSSL_LOCAL int SendClientHello(WOLFSSL* ssl); WOLFSSL_LOCAL int DoHelloVerifyRequest(WOLFSSL* ssl, const byte* input, word32* inOutIdx, word32 size);