Skip to content

Commit

Permalink
fix dtls
Browse files Browse the repository at this point in the history
  • Loading branch information
Ichishino committed Dec 1, 2023
1 parent 909308a commit cc38905
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 19 deletions.
37 changes: 31 additions & 6 deletions examples/dtls_client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,20 @@ void on_my_receive(my_app* self, co_udp_t* client)
}

printf("receive %zd bytes\n", (size_t)size);

co_udp_restart_receive_timer(self->client);
}

co_udp_restart_receive_timer(client);
}

void on_my_receive_timer(my_app* self, co_udp_t* client)
{
printf("receive timeout\n");

co_dtls_udp_client_destroy(client);
self->client = NULL;

// quit app
co_app_stop();
}

void on_my_handshake(my_app* self, co_udp_t* client, int error_code)
Expand All @@ -55,13 +66,13 @@ void on_my_handshake(my_app* self, co_udp_t* client, int error_code)
const char* data = "hello";
co_dtls_udp_send(client, data, strlen(data) + 1);

co_udp_start_receive_timer(self->client);
co_udp_start_receive_timer(client);
}
else
{
printf("handshake failed\n");

co_dtls_udp_client_destroy(self->client);
co_dtls_udp_client_destroy(client);
self->client = NULL;

// quit app
Expand All @@ -70,6 +81,9 @@ void on_my_handshake(my_app* self, co_udp_t* client, int error_code)
}

#ifdef CO_USE_TLS

#ifndef CO_USE_WOLFSSL
// TODO
int on_my_verify_cookie(SSL* ssl, const unsigned char* cookie, unsigned int cookie_len)
{
(void)ssl;
Expand All @@ -79,6 +93,7 @@ int on_my_verify_cookie(SSL* ssl, const unsigned char* cookie, unsigned int cook
// ok
return 1;
}
#endif

int on_my_verify_peer(int preverify_ok, X509_STORE_CTX* x509_ctx)
{
Expand All @@ -88,16 +103,25 @@ int on_my_verify_peer(int preverify_ok, X509_STORE_CTX* x509_ctx)
// ok
return 1;
}

#endif

bool my_tls_setup(co_tls_ctx_st* tls_ctx)
{
#ifdef CO_USE_TLS

#ifdef CO_USE_WOLFSSL
SSL_CTX* ssl_ctx = SSL_CTX_new(wolfDTLS_client_method());
#else
SSL_CTX* ssl_ctx = SSL_CTX_new(DTLS_client_method());
#endif

SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, on_my_verify_peer);

#ifndef CO_USE_WOLFSSL
// TODO
SSL_CTX_set_cookie_verify_cb(ssl_ctx, on_my_verify_cookie);
#endif

tls_ctx->ssl_ctx = ssl_ctx;

Expand Down Expand Up @@ -140,6 +164,7 @@ bool on_my_app_create(my_app* self)
// callback
co_udp_callbacks_st* udp_callbacks = co_udp_get_callbacks(self->client);
udp_callbacks->on_receive = (co_udp_receive_fn)on_my_receive;
udp_callbacks->on_receive_timer = (co_udp_receive_timer_fn)on_my_receive_timer;
co_dtls_udp_callbacks_st* tls_callbacks = co_dtls_udp_get_callbacks(self->client);
tls_callbacks->on_handshake = (co_dtls_udp_handshake_fn)on_my_handshake;

Expand All @@ -160,8 +185,8 @@ void on_my_app_destroy(my_app* self)

int main(int argc, char* argv[])
{
co_tls_log_set_level(CO_LOG_LEVEL_MAX);
co_udp_log_set_level(CO_LOG_LEVEL_MAX);
// co_tls_log_set_level(CO_LOG_LEVEL_MAX);
// co_udp_log_set_level(CO_LOG_LEVEL_MAX);

my_app app = { 0 };

Expand Down
24 changes: 17 additions & 7 deletions examples/dtls_server/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ void on_my_udp_receive(my_app* self, co_udp_t* client)
}

#ifdef CO_USE_TLS
#ifndef CO_USE_WOLFSSL
// TODO
int on_my_generate_cookie(SSL* ssl, unsigned char* cookie, unsigned int* cookie_len)
{
(void)ssl;
Expand All @@ -74,14 +76,19 @@ int on_my_generate_cookie(SSL* ssl, unsigned char* cookie, unsigned int* cookie_
return 1;
}
#endif
#endif

bool my_tls_setup(co_tls_ctx_st* tls_ctx)
{
#ifdef CO_USE_TLS
const char* certificate_file = "../../../test_file/server.crt";
const char* private_key_file = "../../../test_file/server.key";

SSL_CTX* ssl_ctx = SSL_CTX_new(DTLS_server_method());
#ifdef CO_USE_WOLFSSL
SSL_CTX* ssl_ctx = SSL_CTX_new(wolfDTLS_client_method());
#else
SSL_CTX* ssl_ctx = SSL_CTX_new(DTLS_client_method());
#endif

if (SSL_CTX_use_certificate_file(
ssl_ctx, certificate_file, SSL_FILETYPE_PEM) != 1)
Expand All @@ -103,7 +110,10 @@ bool my_tls_setup(co_tls_ctx_st* tls_ctx)
return false;
}

#ifndef CO_USE_WOLFSSL
// TODO
SSL_CTX_set_cookie_generate_cb(ssl_ctx, on_my_generate_cookie);
#endif

tls_ctx->ssl_ctx = ssl_ctx;
#endif
Expand All @@ -115,6 +125,11 @@ void on_my_udp_accept(my_app* self, co_udp_server_t* server, co_udp_t* client)
{
(void)server;

char remote_str[64];
co_net_addr_to_string(
co_socket_get_remote_net_addr(co_udp_get_socket(client)), remote_str, sizeof(remote_str));
printf("accept %s\n", remote_str);

// accept
co_udp_accept((co_thread_t*)self, client);

Expand All @@ -129,19 +144,14 @@ void on_my_udp_accept(my_app* self, co_udp_server_t* server, co_udp_t* client)
// dtls handshake
if (!co_dtls_udp_start_handshake(client, NULL))
{
printf("handshake failed\n");
printf("handshake failed %s\n", remote_str);

co_dtls_udp_client_destroy(client);

return;
}

co_list_add_tail(self->client_list, client);

char remote_str[64];
co_net_addr_to_string(
co_socket_get_remote_net_addr(co_udp_get_socket(client)), remote_str, sizeof(remote_str));
printf("accept %s\n", remote_str);
}

bool on_my_app_create(my_app* self)
Expand Down
14 changes: 8 additions & 6 deletions src/tls/co_tls_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ co_tls_server_cleanup(
co_tls_server_t* tls_server =
(co_tls_server_t*)sock_server->tls;

co_mem_free(tls_server->protocols);
tls_server->protocols = NULL;
tls_server->protocols_length = 0;

SSL_CTX_free(tls_server->ctx.ssl_ctx);
tls_server->ctx.ssl_ctx = NULL;
if (tls_server != NULL)
{
SSL_CTX_free(tls_server->ctx.ssl_ctx);
co_mem_free(tls_server->protocols);
co_mem_free(tls_server);

sock_server->tls = NULL;
}
}
}

Expand Down

0 comments on commit cc38905

Please sign in to comment.