Skip to content

Commit 8d63fb5

Browse files
Merge pull request #7590 from julek-wolfssl/expose-alerts
Allow user to send a user_canceled alert
2 parents 592a452 + ede8cde commit 8d63fb5

4 files changed

Lines changed: 99 additions & 1 deletion

File tree

src/internal.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24894,6 +24894,11 @@ static int SendAlert_ex(WOLFSSL* ssl, int severity, int type)
2489424894
#endif /* WOLFSSL_DTLS13 */
2489524895
{
2489624896
AddRecordHeader(output, ALERT_SIZE, alert, ssl, CUR_ORDER);
24897+
#ifdef WOLFSSL_DTLS
24898+
/* AddRecordHeader doesn't increment the seq number */
24899+
if (ssl->options.dtls)
24900+
DtlsSEQIncrement(ssl, CUR_ORDER);
24901+
#endif
2489724902
}
2489824903

2489924904
output += RECORD_HEADER_SZ;

src/ssl.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3973,6 +3973,25 @@ int wolfSSL_recv(WOLFSSL* ssl, void* data, int sz, int flags)
39733973
}
39743974
#endif
39753975

3976+
int wolfSSL_SendUserCanceled(WOLFSSL* ssl)
3977+
{
3978+
int ret = WOLFSSL_FAILURE;
3979+
WOLFSSL_ENTER("wolfSSL_recv");
3980+
3981+
if (ssl != NULL) {
3982+
ssl->error = SendAlert(ssl, alert_warning, user_canceled);
3983+
if (ssl->error < 0) {
3984+
WOLFSSL_ERROR(ssl->error);
3985+
}
3986+
else {
3987+
ret = wolfSSL_shutdown(ssl);
3988+
}
3989+
}
3990+
3991+
WOLFSSL_LEAVE("wolfSSL_SendUserCanceled", ret);
3992+
3993+
return ret;
3994+
}
39763995

39773996
/* WOLFSSL_SUCCESS on ok */
39783997
WOLFSSL_ABI

tests/api.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72660,6 +72660,77 @@ static int test_tls_cert_store_unchanged(void)
7266072660
return EXPECT_RESULT();
7266172661
}
7266272662

72663+
static int test_wolfSSL_SendUserCanceled(void)
72664+
{
72665+
EXPECT_DECLS;
72666+
#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
72667+
size_t i;
72668+
struct {
72669+
method_provider client_meth;
72670+
method_provider server_meth;
72671+
const char* tls_version;
72672+
} params[] = {
72673+
#if defined(WOLFSSL_TLS13)
72674+
/* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
72675+
{ wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
72676+
#ifdef WOLFSSL_DTLS13
72677+
{ wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
72678+
#endif
72679+
#endif
72680+
#ifndef WOLFSSL_NO_TLS12
72681+
{ wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
72682+
#ifdef WOLFSSL_DTLS
72683+
{ wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
72684+
#endif
72685+
#endif
72686+
#if !defined(NO_OLD_TLS)
72687+
{ wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
72688+
#ifdef WOLFSSL_DTLS
72689+
{ wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
72690+
#endif
72691+
#endif
72692+
};
72693+
72694+
for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
72695+
WOLFSSL_CTX *ctx_c = NULL;
72696+
WOLFSSL_CTX *ctx_s = NULL;
72697+
WOLFSSL *ssl_c = NULL;
72698+
WOLFSSL *ssl_s = NULL;
72699+
struct test_memio_ctx test_ctx;
72700+
WOLFSSL_ALERT_HISTORY h;
72701+
72702+
printf("Testing %s\n", params[i].tls_version);
72703+
72704+
XMEMSET(&h, 0, sizeof(h));
72705+
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
72706+
ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
72707+
params[i].client_meth, params[i].server_meth), 0);
72708+
72709+
/* CH1 */
72710+
ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
72711+
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
72712+
72713+
ExpectIntEQ(wolfSSL_SendUserCanceled(ssl_s), WOLFSSL_SHUTDOWN_NOT_DONE);
72714+
72715+
/* Alert closed connection */
72716+
ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
72717+
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_ZERO_RETURN);
72718+
72719+
/* Last alert will be close notify because user_canceled should be
72720+
* followed by a close_notify */
72721+
ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
72722+
ExpectIntEQ(h.last_rx.code, close_notify);
72723+
ExpectIntEQ(h.last_rx.level, alert_warning);
72724+
72725+
wolfSSL_free(ssl_c);
72726+
wolfSSL_free(ssl_s);
72727+
wolfSSL_CTX_free(ctx_c);
72728+
wolfSSL_CTX_free(ctx_s);
72729+
}
72730+
#endif
72731+
return EXPECT_RESULT();
72732+
}
72733+
7266372734
/*----------------------------------------------------------------------------*
7266472735
| Main
7266572736
*----------------------------------------------------------------------------*/
@@ -73989,6 +74060,7 @@ TEST_CASE testCases[] = {
7398974060
TEST_DECL(test_read_write_hs),
7399074061
TEST_DECL(test_get_signature_nid),
7399174062
TEST_DECL(test_tls_cert_store_unchanged),
74063+
TEST_DECL(test_wolfSSL_SendUserCanceled),
7399274064
/* This test needs to stay at the end to clean up any caches allocated. */
7399374065
TEST_DECL(test_wolfSSL_Cleanup)
7399474066
};

wolfssl/ssl.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,7 @@ WOLFSSL_API unsigned int wolfSSL_SESSION_get_max_early_data(const WOLFSSL_SESSIO
12491249
WOLFSSL_ABI WOLFSSL_API void wolfSSL_CTX_free(WOLFSSL_CTX* ctx);
12501250
WOLFSSL_ABI WOLFSSL_API void wolfSSL_free(WOLFSSL* ssl);
12511251
WOLFSSL_ABI WOLFSSL_API int wolfSSL_shutdown(WOLFSSL* ssl);
1252+
WOLFSSL_API int wolfSSL_SendUserCanceled(WOLFSSL* ssl);
12521253
WOLFSSL_API int wolfSSL_send(WOLFSSL* ssl, const void* data, int sz, int flags);
12531254
WOLFSSL_API int wolfSSL_recv(WOLFSSL* ssl, void* data, int sz, int flags);
12541255

@@ -2576,7 +2577,8 @@ enum { /* ssl Constants */
25762577
WOLFSSL_FAILURE = 0, /* for some functions */
25772578
WOLFSSL_SUCCESS = 1,
25782579

2579-
/* WOLFSSL_SHUTDOWN_NOT_DONE is returned by wolfSSL_shutdown when the other end
2580+
/* WOLFSSL_SHUTDOWN_NOT_DONE is returned by wolfSSL_shutdown and
2581+
* wolfSSL_SendUserCanceled when the other end
25802582
* of the connection has yet to send its close notify alert as part of the
25812583
* bidirectional shutdown. To complete the shutdown, either keep calling
25822584
* wolfSSL_shutdown until it returns WOLFSSL_SUCCESS or call wolfSSL_read until

0 commit comments

Comments
 (0)