Minor improvements. Tested with RSA and ECC for both DTLS v1.2 and v1.3. Tested with/without calling accept/connect. Tested with calling `wolfSSL_is_init_finished`.

pull/477/head
David Garske 2025-01-03 11:59:26 -08:00
parent 8ec1a5e14a
commit 8775b5c06d
2 changed files with 52 additions and 19 deletions

1
.gitignore vendored
View File

@ -56,6 +56,7 @@ android/wolfssljni-ndk-sample/proguard-project.txt
/dtls/client-dtls /dtls/client-dtls
/dtls/client-dtls13 /dtls/client-dtls13
/dtls/client-udp /dtls/client-udp
/dtls/memory-bio-dtls
/dtls/server-dtls-callback /dtls/server-dtls-callback
/dtls/server-dtls-ipv6 /dtls/server-dtls-ipv6
/dtls/server-dtls-nonblocking /dtls/server-dtls-nonblocking

View File

@ -1,6 +1,6 @@
/* memory-bio-dtls.c /* memory-bio-dtls.c
* *
* Copyright (C) 2006-2020 wolfSSL Inc. * Copyright (C) 2006-2025 wolfSSL Inc.
* *
* This file is part of wolfSSL. (formerly known as CyaSSL) * This file is part of wolfSSL. (formerly known as CyaSSL)
* *
@ -22,11 +22,18 @@
/* in memory TLS connection with I/O callbacks, no sockets /* in memory TLS connection with I/O callbacks, no sockets
* *
gcc -Wall memory-tls.c -l wolfssl -lpthread ./configure --enable-opensslall --enable-dtls --enable-dtls13 --enable-debug
make
sudo make install
gcc -o memory-bio-dtls -Wall memory-bio-dtls.c -lwolfssl -lpthread
*/ */
#ifndef WOLFSSL_USER_SETTINGS
#include <wolfssl/options.h> #include <wolfssl/options.h>
#endif
#include <wolfssl/ssl.h> #include <wolfssl/ssl.h>
#include <stdio.h> #include <stdio.h>
@ -43,10 +50,16 @@ static void err_sys(const char* msg)
exit(1); exit(1);
} }
#ifndef NO_RSA
#define CERT_FILE "../certs/server-cert.pem"
#define KEY_FILE "../certs/server-key.pem"
#define CA_FILE "../certs/ca-cert.pem"
#else
#define CERT_FILE "../certs/server-ecc.pem"
#define KEY_FILE "../certs/ecc-key.pem"
#define CA_FILE "../certs/ca-ecc-cert.pem"
#endif
#define key "../certs/server-key.pem"
#define cert "../certs/server-cert.pem"
#define cacert "../certs/ca-cert.pem"
typedef struct IO_HANDLES { typedef struct IO_HANDLES {
WOLFSSL_BIO* rbio; WOLFSSL_BIO* rbio;
@ -64,16 +77,16 @@ static void* client_thread(void* args)
/* set up client */ /* set up client */
cli_ctx = wolfSSL_CTX_new( cli_ctx = wolfSSL_CTX_new(
#ifdef WOLFSSL_DTLS13 #ifdef WOLFSSL_DTLS13
wolfDTLSv1_3_client_method() wolfDTLSv1_3_client_method()
#else #else
wolfDTLSv1_2_client_method() wolfDTLSv1_2_client_method()
#endif #endif
); );
if (cli_ctx == NULL) { if (cli_ctx == NULL) {
err_sys("bad client ctx new"); err_sys("bad client ctx new");
} }
ret = wolfSSL_CTX_load_verify_locations(cli_ctx, cacert, NULL); ret = wolfSSL_CTX_load_verify_locations(cli_ctx, CA_FILE, NULL);
if (ret != WOLFSSL_SUCCESS) { if (ret != WOLFSSL_SUCCESS) {
err_sys("bad ca load"); err_sys("bad ca load");
} }
@ -84,23 +97,26 @@ static void* client_thread(void* args)
} }
wolfSSL_set_bio(cli_ssl, io->wbio, io->rbio); wolfSSL_set_bio(cli_ssl, io->wbio, io->rbio);
#if 1
err = 0; err = 0;
do { do {
sem_wait(&io->bioSem); sem_wait(&io->bioSem);
ret = wolfSSL_connect(cli_ssl); ret = wolfSSL_connect(cli_ssl);
sem_post(&io->bioSem); sem_post(&io->bioSem);
err = wolfSSL_get_error(cli_ssl, ret); err = wolfSSL_get_error(cli_ssl, ret);
} while (ret != WOLFSSL_SUCCESS && } while (ret != WOLFSSL_SUCCESS &&
((err == WOLFSSL_ERROR_WANT_READ) || (err == WOLFSSL_ERROR_WANT_WRITE))); ((err == WOLFSSL_ERROR_WANT_READ) || (err == WOLFSSL_ERROR_WANT_WRITE)));
if (ret != WOLFSSL_SUCCESS) err_sys("bad client tls connect"); if (ret != WOLFSSL_SUCCESS) err_sys("bad client tls connect");
printf("wolfSSL client success!\n"); printf("wolfSSL client success!\n");
#endif
do { do {
sem_wait(&io->bioSem); sem_wait(&io->bioSem);
ret = wolfSSL_write(cli_ssl, "hello memory wolfSSL!", 21); ret = wolfSSL_write(cli_ssl, "hello memory wolfSSL!", 21);
sem_post(&io->bioSem); sem_post(&io->bioSem);
err = wolfSSL_get_error(cli_ssl, ret); err = wolfSSL_get_error(cli_ssl, ret);
} while (ret <= 0 && } while (ret <= 0 &&
((err == WOLFSSL_ERROR_WANT_READ) || (err == WOLFSSL_ERROR_WANT_WRITE))); ((err == WOLFSSL_ERROR_WANT_READ) || (err == WOLFSSL_ERROR_WANT_WRITE)));
/* clean up, wolfSSL_free would also free the WOLFSSL_BIO's so set as NULL /* clean up, wolfSSL_free would also free the WOLFSSL_BIO's so set as NULL
@ -120,8 +136,15 @@ int main()
int ret, err; int ret, err;
WOLFSSL_CTX* srv_ctx = NULL; WOLFSSL_CTX* srv_ctx = NULL;
WOLFSSL* srv_ssl = NULL; WOLFSSL* srv_ssl = NULL;
WOLFSSL_CIPHER* cipher;
const char *name;
pthread_t tid; pthread_t tid;
#if 0
wolfSSL_Debugging_ON();
#endif
wolfSSL_Init();
io.rbio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()); io.rbio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
io.wbio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()); io.wbio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
sem_init(&io.bioSem, 0, 1); sem_init(&io.bioSem, 0, 1);
@ -136,12 +159,12 @@ int main()
); );
if (srv_ctx == NULL) err_sys("bad server ctx new"); if (srv_ctx == NULL) err_sys("bad server ctx new");
ret = wolfSSL_CTX_use_PrivateKey_file(srv_ctx, key, WOLFSSL_FILETYPE_PEM); ret = wolfSSL_CTX_use_PrivateKey_file(srv_ctx, KEY_FILE, WOLFSSL_FILETYPE_PEM);
if (ret != WOLFSSL_SUCCESS) { if (ret != WOLFSSL_SUCCESS) {
err_sys("bad server key file load"); err_sys("bad server key file load");
} }
ret = wolfSSL_CTX_use_certificate_file(srv_ctx, cert, WOLFSSL_FILETYPE_PEM); ret = wolfSSL_CTX_use_certificate_file(srv_ctx, CERT_FILE, WOLFSSL_FILETYPE_PEM);
if (ret != WOLFSSL_SUCCESS) { if (ret != WOLFSSL_SUCCESS) {
err_sys("bad server cert file load"); err_sys("bad server cert file load");
} }
@ -157,26 +180,34 @@ int main()
/* start client thread */ /* start client thread */
pthread_create(&tid, 0, client_thread, (void*)&io); pthread_create(&tid, 0, client_thread, (void*)&io);
#if 1
/* accept tls connection without tcp sockets */ /* accept tls connection without tcp sockets */
err = 0; err = 0;
do { do {
sem_wait(&io.bioSem); sem_wait(&io.bioSem);
ret = wolfSSL_accept(srv_ssl); ret = wolfSSL_accept(srv_ssl);
sem_post(&io.bioSem); sem_post(&io.bioSem);
err = wolfSSL_get_error(srv_ssl, ret); err = wolfSSL_get_error(srv_ssl, ret);
} while (ret != WOLFSSL_SUCCESS && } while (ret != WOLFSSL_SUCCESS &&
((err == WOLFSSL_ERROR_WANT_READ) || (err == WOLFSSL_ERROR_WANT_WRITE))); ((err == WOLFSSL_ERROR_WANT_READ) || (err == WOLFSSL_ERROR_WANT_WRITE)));
if (ret != WOLFSSL_SUCCESS) err_sys("bad server tls accept"); if (ret != WOLFSSL_SUCCESS) err_sys("bad server tls accept");
printf("wolfSSL accept success!\n"); printf("wolfSSL accept success!\n");
printf("Version: %s\n", wolfSSL_get_version(srv_ssl));
cipher = wolfSSL_get_current_cipher(srv_ssl);
printf("Cipher Suite: %s\n", wolfSSL_CIPHER_get_name(cipher));
if ((name = wolfSSL_get_curve_name(srv_ssl)) != NULL)
printf("Curve: %s\n", name);
#endif
/* read msg post handshake from client */ /* read msg post handshake from client */
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
do { do {
sem_wait(&io.bioSem); sem_wait(&io.bioSem);
ret = wolfSSL_read(srv_ssl, buf, sizeof(buf)-1); ret = wolfSSL_read(srv_ssl, buf, sizeof(buf)-1);
sem_post(&io.bioSem); sem_post(&io.bioSem);
err = wolfSSL_get_error(srv_ssl, ret); err = wolfSSL_get_error(srv_ssl, ret);
} while (ret != 0 && } while (ret != 0 &&
((err == WOLFSSL_ERROR_WANT_READ) || (err == WOLFSSL_ERROR_WANT_WRITE))); ((err == WOLFSSL_ERROR_WANT_READ) || (err == WOLFSSL_ERROR_WANT_WRITE)));
if (ret >= 0) { if (ret >= 0) {
printf("client msg = %s\n", buf); printf("client msg = %s\n", buf);
@ -189,6 +220,7 @@ int main()
wolfSSL_free(srv_ssl); /* This also does free on rbio and wbio */ wolfSSL_free(srv_ssl); /* This also does free on rbio and wbio */
wolfSSL_CTX_free(srv_ctx); wolfSSL_CTX_free(srv_ctx);
wolfSSL_Cleanup();
return 0; return 0;
} }