Skip to content

Commit

Permalink
Cleanups for DTLS 1.3 client/server examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgarske committed Aug 28, 2024
1 parent f707ad8 commit eaa8e8e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 26 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,15 @@ android/wolfssljni-ndk-sample/proguard-project.txt
/dtls/client-dtls-resume
/dtls/client-dtls-shared
/dtls/client-dtls
/dtls/client-dtls13
/dtls/client-udp
/dtls/server-dtls-callback
/dtls/server-dtls-ipv6
/dtls/server-dtls-nonblocking
/dtls/server-dtls-threaded
/dtls/server-dtls
/dtls/server-dtls13
/dtls/server-dtls13-event
/dtls/server-udp

/psk/client-psk-bio-custom
Expand Down
29 changes: 17 additions & 12 deletions dtls/client-dtls13.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* client-dtls13.c
*
* Copyright (C) 2006-2022 wolfSSL Inc.
* Copyright (C) 2006-2024 wolfSSL Inc.
*
* This file is part of wolfSSL. (formerly known as CyaSSL)
*
Expand Down Expand Up @@ -29,7 +29,9 @@
* Define USE_DTLS12 to use DTLS 1.2 instead of DTLS 1.3
*/

#ifndef WOLFSSL_USER_SETTINGS
#include <wolfssl/options.h>
#endif
#include <unistd.h>
#include <wolfssl/ssl.h>
#include <netdb.h>
Expand All @@ -45,7 +47,7 @@

int main (int argc, char** argv)
{
/* standard variables used in a dtls client*/
/* standard variables used in a dtls client */
int n = 0;
int sockfd = INVALID_SOCKET;
int err;
Expand All @@ -65,14 +67,14 @@ int main (int argc, char** argv)

/* Initialize wolfSSL before assigning ctx */
if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
fprintf(stderr, "wolfSSL_CTX_new error.\n");
fprintf(stderr, "wolfSSL_Init error.\n");
return exitVal;
}

/* No-op when debugging is not compiled in */
wolfSSL_Debugging_ON();

if ( (ctx = wolfSSL_CTX_new(
if ((ctx = wolfSSL_CTX_new(
#ifdef WOLFSSL_DTLS13
wolfDTLSv1_3_client_method()
#else
Expand All @@ -84,8 +86,8 @@ int main (int argc, char** argv)
}

/* Load certificates into ctx variable */
if (wolfSSL_CTX_load_verify_locations(ctx, caCertLoc, 0)
!= SSL_SUCCESS) {
if (wolfSSL_CTX_load_verify_locations(ctx, caCertLoc, NULL)
!= WOLFSSL_SUCCESS) {
fprintf(stderr, "Error loading %s, please check the file.\n", caCertLoc);
goto cleanup;
}
Expand All @@ -112,7 +114,7 @@ int main (int argc, char** argv)
goto cleanup;
}

if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket()");
goto cleanup;
}
Expand All @@ -124,7 +126,7 @@ int main (int argc, char** argv)
}

/* Perform SSL connection */
if (wolfSSL_connect(ssl) != SSL_SUCCESS) {
if (wolfSSL_connect(ssl) != WOLFSSL_SUCCESS) {
err = wolfSSL_get_error(ssl, 0);
fprintf(stderr, "err = %d, %s\n", err, wolfSSL_ERR_reason_error_string(err));
fprintf(stderr, "wolfSSL_connect failed\n");
Expand All @@ -145,7 +147,8 @@ int main (int argc, char** argv)
/* Send sendLine to the server */
if (wolfSSL_write(ssl, sendLine, strlen(sendLine)) != strlen(sendLine)) {
err = wolfSSL_get_error(ssl, 0);
fprintf(stderr, "err = %d, %s\n", err, wolfSSL_ERR_reason_error_string(err));
fprintf(stderr, "err = %d, %s\n", err,
wolfSSL_ERR_reason_error_string(err));
fprintf(stderr, "wolfSSL_write failed\n");
goto cleanup;
}
Expand All @@ -160,7 +163,8 @@ int main (int argc, char** argv)
}
else {
err = wolfSSL_get_error(ssl, 0);
fprintf(stderr, "err = %d, %s\n", err, wolfSSL_ERR_reason_error_string(err));
fprintf(stderr, "err = %d, %s\n", err,
wolfSSL_ERR_reason_error_string(err));
fprintf(stderr, "wolfSSL_read failed\n");
goto cleanup;
}
Expand All @@ -177,7 +181,8 @@ int main (int argc, char** argv)
ret = wolfSSL_shutdown(ssl);
if (ret != WOLFSSL_SUCCESS) {
err = wolfSSL_get_error(ssl, 0);
fprintf(stderr, "err = %d, %s\n", err, wolfSSL_ERR_reason_error_string(err));
fprintf(stderr, "err = %d, %s\n", err,
wolfSSL_ERR_reason_error_string(err));
fprintf(stderr, "wolfSSL_shutdown failed\n");
}
wolfSSL_free(ssl);
Expand Down
6 changes: 3 additions & 3 deletions dtls/server-dtls13-event.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ typedef struct conn_ctx {
WOLFSSL* ssl;
struct event* readEv;
struct event* writeEv;
char waitingOnData:1;
unsigned char waitingOnData:1;
} conn_ctx;

WOLFSSL_CTX* ctx = NULL;
Expand Down Expand Up @@ -109,7 +109,7 @@ int main(int argc, char** argv)
goto cleanup;
}
/* Load server certificates */
if (wolfSSL_CTX_use_certificate_file(ctx, servCertLoc, SSL_FILETYPE_PEM) !=
if (wolfSSL_CTX_use_certificate_file(ctx, servCertLoc, SSL_FILETYPE_PEM) !=
SSL_SUCCESS) {
fprintf(stderr, "Error loading %s, please check the file.\n", servCertLoc);
goto cleanup;
Expand Down Expand Up @@ -286,7 +286,7 @@ static void newConn(evutil_socket_t fd, short events, void* arg)
static void setHsTimeout(WOLFSSL* ssl, struct timeval *tv)
{
int timeout = wolfSSL_dtls_get_current_timeout(ssl);
#ifndef USE_DTLS12
#ifdef WOLFSSL_DTLS13
if (wolfSSL_dtls13_use_quick_timeout(ssl)) {
if (timeout >= QUICK_MULT)
tv->tv_sec = timeout / QUICK_MULT;
Expand Down
28 changes: 17 additions & 11 deletions dtls/server-dtls13.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* server-dtls13.c
*
* Copyright (C) 2006-2022 wolfSSL Inc.
* Copyright (C) 2006-2024 wolfSSL Inc.
*
* This file is part of wolfSSL. (formerly known as CyaSSL)
*
Expand All @@ -26,7 +26,9 @@
* Define USE_DTLS12 to use DTLS 1.2 instead of DTLS 1.3
*/

#ifndef WOLFSSL_USER_SETTINGS
#include <wolfssl/options.h>
#endif
#include <stdio.h> /* standard in/out procedures */
#include <stdlib.h> /* defines system calls */
#include <string.h> /* necessary for memset */
Expand Down Expand Up @@ -83,19 +85,19 @@ int main(int argc, char** argv)
}
/* Load CA certificates */
if (wolfSSL_CTX_load_verify_locations(ctx,caCertLoc,0) !=
SSL_SUCCESS) {
WOLFSSL_SUCCESS) {
fprintf(stderr, "Error loading %s, please check the file.\n", caCertLoc);
goto cleanup;
}
/* Load server certificates */
if (wolfSSL_CTX_use_certificate_file(ctx, servCertLoc, SSL_FILETYPE_PEM) !=
SSL_SUCCESS) {
if (wolfSSL_CTX_use_certificate_file(ctx, servCertLoc,
WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
fprintf(stderr, "Error loading %s, please check the file.\n", servCertLoc);
goto cleanup;
}
/* Load server Keys */
if (wolfSSL_CTX_use_PrivateKey_file(ctx, servKeyLoc,
SSL_FILETYPE_PEM) != SSL_SUCCESS) {
WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
fprintf(stderr, "Error loading %s, please check the file.\n", servKeyLoc);
goto cleanup;
}
Expand Down Expand Up @@ -153,9 +155,10 @@ int main(int argc, char** argv)
break;
}

if (wolfSSL_accept(ssl) != SSL_SUCCESS) {
if (wolfSSL_accept(ssl) != WOLFSSL_SUCCESS) {
err = wolfSSL_get_error(ssl, 0);
fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_reason_error_string(err));
fprintf(stderr, "error = %d, %s\n", err,
wolfSSL_ERR_reason_error_string(err));
fprintf(stderr, "SSL_accept failed.\n");
goto cleanup;
}
Expand All @@ -171,14 +174,16 @@ int main(int argc, char** argv)
err = wolfSSL_get_error(ssl, 0);
if (err == WOLFSSL_ERROR_ZERO_RETURN) /* Received shutdown */
break;
fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_reason_error_string(err));
fprintf(stderr, "error = %d, %s\n", err,
wolfSSL_ERR_reason_error_string(err));
fprintf(stderr, "SSL_read failed.\n");
goto cleanup;
}
printf("Sending reply.\n");
if (wolfSSL_write(ssl, ack, sizeof(ack)) < 0) {
err = wolfSSL_get_error(ssl, 0);
fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_reason_error_string(err));
fprintf(stderr, "error = %d, %s\n", err,
wolfSSL_ERR_reason_error_string(err));
fprintf(stderr, "wolfSSL_write failed.\n");
goto cleanup;
}
Expand All @@ -192,15 +197,16 @@ int main(int argc, char** argv)
ret = wolfSSL_shutdown(ssl);
if (ret != WOLFSSL_SUCCESS) {
err = wolfSSL_get_error(ssl, 0);
fprintf(stderr, "err = %d, %s\n", err, wolfSSL_ERR_reason_error_string(err));
fprintf(stderr, "err = %d, %s\n", err,
wolfSSL_ERR_reason_error_string(err));
fprintf(stderr, "wolfSSL_shutdown failed\n");
}
wolfSSL_free(ssl);
ssl = NULL;

printf("Awaiting new connection\n");
}

exitVal = 0;
cleanup:
free_resources();
Expand Down

0 comments on commit eaa8e8e

Please sign in to comment.