@@ -46755,25 +46755,12 @@ static int test_extra_alerts_bad_psk(void)
4675546755}
4675646756#endif
4675746757
46758- #if defined(OPENSSL_EXTRA) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
46759- /*
46760- * Emulates wolfSSL_shutdown that goes on EAGAIN,
46761- * by returning on output WOLFSSL_ERROR_WANT_WRITE.*/
46762- static int custom_wolfSSL_shutdown(WOLFSSL *ssl, char *buf,
46763- int sz, void *ctx)
46764- {
46765- (void)ssl;
46766- (void)buf;
46767- (void)ctx;
46768- (void)sz;
46769-
46770- return WOLFSSL_CBIO_ERR_WANT_WRITE;
46771- }
46772-
46773- static int test_multiple_alerts_EAGAIN(void)
46758+ #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
46759+ static int test_multiple_shutdown_nonblocking(void)
4677446760{
4677546761 EXPECT_DECLS;
4677646762 size_t size_of_last_packet = 0;
46763+ int dummy_recv_buffer;
4677746764
4677846765 /* declare wolfSSL objects */
4677946766 struct test_memio_ctx test_ctx;
@@ -46783,46 +46770,68 @@ static int test_multiple_alerts_EAGAIN(void)
4678346770 XMEMSET(&test_ctx, 0, sizeof(test_ctx));
4678446771
4678546772 /* Create and initialize WOLFSSL_CTX and WOLFSSL objects */
46786- #ifdef USE_TLSV13
46787- ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
46788- wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
46789- #else
4679046773 ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
4679146774 wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
46792- #endif
46775+
4679346776 ExpectNotNull(ctx_c);
4679446777 ExpectNotNull(ssl_c);
4679546778 ExpectNotNull(ctx_s);
4679646779 ExpectNotNull(ssl_s);
4679746780
46798- /* Load client certificates into WOLFSSL_CTX */
46799- ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_c, "./certs/ca-cert.pem", NULL), WOLFSSL_SUCCESS);
46800-
4680146781 ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
4680246782
46803- /*
46804- * We set the custom callback for the IO to emulate multiple EAGAINs
46805- * on shutdown, so we can check that we don't send multiple packets.
46806- * */
46807- wolfSSL_SSLSetIOSend(ssl_c, custom_wolfSSL_shutdown);
46783+ /* buffers should be empty now */
46784+ ExpectIntEQ(test_ctx.c_len, 0);
46785+ ExpectIntEQ(test_ctx.s_len, 0);
46786+ ExpectIntEQ(ssl_c->buffers.outputBuffer.length, 0);
46787+
46788+ test_memio_simulate_want_write(&test_ctx, 0, 1);
4680846789
4680946790 /*
46810- * We call wolfSSL_shutdown multiple times to reproduce the behaviour,
46811- * to check that it doesn't add the CLOSE_NOTIFY packet multiple times
46812- * on the output buffer.
46791+ * We call wolfSSL_shutdown multiple times to to check that it doesn't add
46792+ * the CLOSE_NOTIFY packet multiple times on the output buffer.
4681346793 * */
46814- wolfSSL_shutdown(ssl_c);
46815- wolfSSL_shutdown( ssl_c);
46794+ ExpectIntEQ( wolfSSL_shutdown(ssl_c), -1 );
46795+ ExpectIntEQ(wolfSSL_get_error( ssl_c, 0), WOLFSSL_ERROR_WANT_WRITE );
4681646796
46797+ /* store the size of the packet */
4681746798 if (ssl_c != NULL) {
4681846799 size_of_last_packet = ssl_c->buffers.outputBuffer.length;
4681946800 }
46820- wolfSSL_shutdown(ssl_c);
4682146801
46822- /*
46823- * Finally we check the length of the output buffer.
46824- * */
46825- ExpectIntEQ((ssl_c->buffers.outputBuffer.length - size_of_last_packet), 0);
46802+ /* invoke it multiple times shouldn't change the wolfssl internal output buffer size */
46803+ ExpectIntEQ(wolfSSL_shutdown(ssl_c), -1);
46804+ ExpectIntEQ(wolfSSL_get_error(ssl_c, 0), WOLFSSL_ERROR_WANT_WRITE);
46805+ ExpectIntEQ(wolfSSL_shutdown(ssl_c), -1);
46806+ ExpectIntEQ(wolfSSL_get_error(ssl_c, 0), WOLFSSL_ERROR_WANT_WRITE);
46807+
46808+ ExpectIntEQ(ssl_c->buffers.outputBuffer.length, size_of_last_packet);
46809+
46810+ /* now send the CLOSE_NOTIFY to the server for real, expecting shutdown not done */
46811+ test_memio_simulate_want_write(&test_ctx, 0, 0);
46812+ ExpectIntEQ(wolfSSL_shutdown(ssl_c), WOLFSSL_SHUTDOWN_NOT_DONE);
46813+
46814+ /* output buffer should be empty and socket buffer should contain the message */
46815+ ExpectIntEQ(ssl_c->buffers.outputBuffer.length, 0);
46816+ ExpectIntEQ(test_ctx.s_len, size_of_last_packet);
46817+
46818+
46819+ /* this should try to read from the socket */
46820+ ExpectIntEQ(wolfSSL_shutdown(ssl_c), -1);
46821+ ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
46822+
46823+ /* complete the bidirectional shutdown */
46824+
46825+ /* check that server received the shutdown alert */
46826+ ExpectIntEQ(wolfSSL_recv(ssl_s, &dummy_recv_buffer, 0, 0), 0);
46827+ ExpectIntEQ(wolfSSL_get_error(ssl_s, 0), WOLFSSL_ERROR_ZERO_RETURN);
46828+
46829+ /* send the shutdown from the server side */
46830+ ExpectIntEQ(wolfSSL_shutdown(ssl_s), WOLFSSL_SUCCESS);
46831+
46832+ /* This should return success and zero return */
46833+ ExpectIntEQ(wolfSSL_shutdown(ssl_c), WOLFSSL_SUCCESS);
46834+ ExpectIntEQ(wolfSSL_get_error(ssl_c, 0), WOLFSSL_ERROR_ZERO_RETURN);
4682646835
4682746836 /* Cleanup and return */
4682846837 wolfSSL_CTX_free(ctx_c);
@@ -46833,7 +46842,7 @@ static int test_multiple_alerts_EAGAIN(void)
4683346842 return EXPECT_RESULT();
4683446843}
4683546844#else
46836- static int test_multiple_alerts_EAGAIN (void)
46845+ static int test_multiple_shutdown_nonblocking (void)
4683746846{
4683846847 return TEST_SKIPPED;
4683946848}
@@ -51369,7 +51378,7 @@ TEST_CASE testCases[] = {
5136951378 TEST_DECL(test_extra_alerts_wrong_cs),
5137051379 TEST_DECL(test_extra_alerts_skip_hs),
5137151380 TEST_DECL(test_extra_alerts_bad_psk),
51372- TEST_DECL(test_multiple_alerts_EAGAIN ),
51381+ TEST_DECL(test_multiple_shutdown_nonblocking ),
5137351382 /* Can't memory test as client/server Asserts. */
5137451383 TEST_DECL(test_harden_no_secure_renegotiation),
5137551384 TEST_DECL(test_override_alt_cert_chain),
0 commit comments