Skip to content

Commit 8ec1a5e

Browse files
add DTLS bio example
1 parent 63893d5 commit 8ec1a5e

File tree

1 file changed

+194
-0
lines changed

1 file changed

+194
-0
lines changed

dtls/memory-bio-dtls.c

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
/* memory-bio-dtls.c
2+
*
3+
* Copyright (C) 2006-2020 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL. (formerly known as CyaSSL)
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20+
*/
21+
22+
23+
/* in memory TLS connection with I/O callbacks, no sockets
24+
*
25+
gcc -Wall memory-tls.c -l wolfssl -lpthread
26+
27+
*/
28+
29+
#include <wolfssl/options.h>
30+
#include <wolfssl/ssl.h>
31+
32+
#include <stdio.h>
33+
#include <stdlib.h>
34+
#include <string.h>
35+
#include <pthread.h>
36+
#include <unistd.h>
37+
#include <semaphore.h>
38+
39+
40+
static void err_sys(const char* msg)
41+
{
42+
printf("wolfSSL error: %s\n", msg);
43+
exit(1);
44+
}
45+
46+
47+
#define key "../certs/server-key.pem"
48+
#define cert "../certs/server-cert.pem"
49+
#define cacert "../certs/ca-cert.pem"
50+
51+
typedef struct IO_HANDLES {
52+
WOLFSSL_BIO* rbio;
53+
WOLFSSL_BIO* wbio;
54+
sem_t bioSem;
55+
} IO_HANDLES;
56+
57+
static void* client_thread(void* args)
58+
{
59+
IO_HANDLES* io = (IO_HANDLES*)args;
60+
WOLFSSL_CTX* cli_ctx = NULL;
61+
WOLFSSL* cli_ssl = NULL;
62+
int err, ret;
63+
64+
/* set up client */
65+
cli_ctx = wolfSSL_CTX_new(
66+
#ifdef WOLFSSL_DTLS13
67+
wolfDTLSv1_3_client_method()
68+
#else
69+
wolfDTLSv1_2_client_method()
70+
#endif
71+
);
72+
if (cli_ctx == NULL) {
73+
err_sys("bad client ctx new");
74+
}
75+
76+
ret = wolfSSL_CTX_load_verify_locations(cli_ctx, cacert, NULL);
77+
if (ret != WOLFSSL_SUCCESS) {
78+
err_sys("bad ca load");
79+
}
80+
81+
cli_ssl = wolfSSL_new(cli_ctx);
82+
if (cli_ctx == NULL) {
83+
err_sys("bad client new");
84+
}
85+
86+
wolfSSL_set_bio(cli_ssl, io->wbio, io->rbio);
87+
err = 0;
88+
do {
89+
sem_wait(&io->bioSem);
90+
ret = wolfSSL_connect(cli_ssl);
91+
sem_post(&io->bioSem);
92+
err = wolfSSL_get_error(cli_ssl, ret);
93+
} while (ret != WOLFSSL_SUCCESS &&
94+
((err == WOLFSSL_ERROR_WANT_READ) || (err == WOLFSSL_ERROR_WANT_WRITE)));
95+
if (ret != WOLFSSL_SUCCESS) err_sys("bad client tls connect");
96+
printf("wolfSSL client success!\n");
97+
98+
do {
99+
sem_wait(&io->bioSem);
100+
ret = wolfSSL_write(cli_ssl, "hello memory wolfSSL!", 21);
101+
sem_post(&io->bioSem);
102+
err = wolfSSL_get_error(cli_ssl, ret);
103+
} while (ret <= 0 &&
104+
((err == WOLFSSL_ERROR_WANT_READ) || (err == WOLFSSL_ERROR_WANT_WRITE)));
105+
106+
/* clean up, wolfSSL_free would also free the WOLFSSL_BIO's so set as NULL
107+
* since they are also being used with srv_ssl and will be free'd there. */
108+
wolfSSL_set_bio(cli_ssl, NULL, NULL);
109+
wolfSSL_free(cli_ssl);
110+
wolfSSL_CTX_free(cli_ctx);
111+
112+
return NULL;
113+
}
114+
115+
116+
int main()
117+
{
118+
IO_HANDLES io;
119+
unsigned char buf[80];
120+
int ret, err;
121+
WOLFSSL_CTX* srv_ctx = NULL;
122+
WOLFSSL* srv_ssl = NULL;
123+
pthread_t tid;
124+
125+
io.rbio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
126+
io.wbio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
127+
sem_init(&io.bioSem, 0, 1);
128+
129+
/* set up server */
130+
srv_ctx = wolfSSL_CTX_new(
131+
#ifdef WOLFSSL_DTLS13
132+
wolfDTLSv1_3_server_method()
133+
#else
134+
wolfDTLSv1_2_server_method()
135+
#endif
136+
);
137+
if (srv_ctx == NULL) err_sys("bad server ctx new");
138+
139+
ret = wolfSSL_CTX_use_PrivateKey_file(srv_ctx, key, WOLFSSL_FILETYPE_PEM);
140+
if (ret != WOLFSSL_SUCCESS) {
141+
err_sys("bad server key file load");
142+
}
143+
144+
ret = wolfSSL_CTX_use_certificate_file(srv_ctx, cert, WOLFSSL_FILETYPE_PEM);
145+
if (ret != WOLFSSL_SUCCESS) {
146+
err_sys("bad server cert file load");
147+
}
148+
149+
srv_ssl = wolfSSL_new(srv_ctx);
150+
if (srv_ctx == NULL) {
151+
err_sys("bad server new");
152+
}
153+
154+
/* set memory BIO's to use for IO */
155+
wolfSSL_set_bio(srv_ssl, io.rbio, io.wbio);
156+
157+
/* start client thread */
158+
pthread_create(&tid, 0, client_thread, (void*)&io);
159+
160+
/* accept tls connection without tcp sockets */
161+
err = 0;
162+
do {
163+
sem_wait(&io.bioSem);
164+
ret = wolfSSL_accept(srv_ssl);
165+
sem_post(&io.bioSem);
166+
err = wolfSSL_get_error(srv_ssl, ret);
167+
} while (ret != WOLFSSL_SUCCESS &&
168+
((err == WOLFSSL_ERROR_WANT_READ) || (err == WOLFSSL_ERROR_WANT_WRITE)));
169+
if (ret != WOLFSSL_SUCCESS) err_sys("bad server tls accept");
170+
printf("wolfSSL accept success!\n");
171+
172+
/* read msg post handshake from client */
173+
memset(buf, 0, sizeof(buf));
174+
do {
175+
sem_wait(&io.bioSem);
176+
ret = wolfSSL_read(srv_ssl, buf, sizeof(buf)-1);
177+
sem_post(&io.bioSem);
178+
err = wolfSSL_get_error(srv_ssl, ret);
179+
} while (ret != 0 &&
180+
((err == WOLFSSL_ERROR_WANT_READ) || (err == WOLFSSL_ERROR_WANT_WRITE)));
181+
if (ret >= 0) {
182+
printf("client msg = %s\n", buf);
183+
}
184+
185+
pthread_join(tid, NULL);
186+
187+
/* clean up */
188+
sem_destroy(&io.bioSem);
189+
wolfSSL_free(srv_ssl); /* This also does free on rbio and wbio */
190+
wolfSSL_CTX_free(srv_ctx);
191+
192+
return 0;
193+
}
194+

0 commit comments

Comments
 (0)