-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathclient-dtls-callback.c
335 lines (292 loc) · 10.6 KB
/
client-dtls-callback.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* client-dtls-callback.c
*
* Copyright (C) 2006-2020 wolfSSL Inc.
*
* This file is part of wolfSSL. (formerly known as CyaSSL)
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*=============================================================================
*
* Bare-bones example of a DTLS client for instructional/learning purposes.
* Utilizes DTLS 1.2 and custom IO callbacks.
*/
#ifndef WOLFSSL_USER_SETTINGS
#include <wolfssl/options.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/ssl.h>
#include <stdio.h> /* standard in/out procedures */
#include <stdlib.h> /* defines system calls */
#include <string.h> /* necessary for memset */
#include <netdb.h>
#include <sys/socket.h> /* used for all socket calls */
#include <netinet/in.h> /* used for sockaddr_in */
#include <arpa/inet.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#define SERV_PORT 11111 /* define our server port number */
#define MSGLEN 4096
#define DTLS_MTU 1500
static int cleanup = 0; /* To handle shutdown */
static void sig_handler(const int sig)
{
printf("\nSIGINT %d handled\n", sig);
cleanup = 1;
return;
}
typedef struct SharedDtls {
WOLFSSL* ssl; /* WOLFSSL object being shared */
int sd; /* socket fd */
struct sockaddr_in servAddr; /* server sockaddr */
socklen_t servSz; /* length of servAddr */
} SharedDtls;
int my_IORecv(WOLFSSL* ssl, char* buff, int sz, void* ctx)
{
SharedDtls* shared = (SharedDtls*)ctx;
int recvd;
struct sockaddr addr;
socklen_t addrSz = sizeof(addr);
int dtls_timeout;
printf("my_IORecv fd %d, buf %d\n", shared->sd, sz);
/* Get current DTLS handshake timeout. */
dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl);
printf("my_IORecv timeout %d\n", dtls_timeout);
/* If we are performing the DTLS handshake, and we are in blocking mode, we
* set a socket timeout at OS level. */
if (!wolfSSL_is_init_finished(ssl)) {
/* Still in handshake: set the OS timeout */
if (dtls_timeout > 0) {
struct timeval tv;
tv.tv_sec = dtls_timeout;
tv.tv_usec = 0;
if (setsockopt(shared->sd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
fprintf(stderr, "setsockopt failed\n");
return WOLFSSL_CBIO_ERR_GENERAL;
}
}
} else {
struct timeval tv = {0, 0};
if (setsockopt(shared->sd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
fprintf(stderr, "setsockopt failed\n");
return WOLFSSL_CBIO_ERR_GENERAL;
}
}
/* Receive datagram */
recvd = recvfrom(shared->sd, buff, sz, 0, &addr, &addrSz);
if (recvd == -1) {
/* error encountered. Be responsible and report it in wolfSSL terms */
fprintf(stderr, "IO RECEIVE ERROR: %d\n", errno);
switch (errno) {
#if EAGAIN != EWOULDBLOCK
case EAGAIN: /* EAGAIN == EWOULDBLOCK on some systems, but not others */
#endif
case EWOULDBLOCK:
if (!wolfSSL_dtls(ssl) || wolfSSL_get_using_nonblock(ssl)) {
fprintf(stderr, "would block\n");
return WOLFSSL_CBIO_ERR_WANT_READ;
}
else {
fprintf(stderr, "socket timeout\n");
return WOLFSSL_CBIO_ERR_TIMEOUT;
}
case ECONNRESET:
fprintf(stderr, "connection reset\n");
return WOLFSSL_CBIO_ERR_CONN_RST;
case EINTR:
fprintf(stderr, "socket interrupted\n");
cleanup = 1;
return WOLFSSL_CBIO_ERR_GENERAL;
/* NOTE: Don't return WOLFSSL_CBIO_ERR_ISR. It keeps trying to recv */
case ECONNREFUSED:
fprintf(stderr, "connection refused\n");
return WOLFSSL_CBIO_ERR_WANT_READ;
case ECONNABORTED:
fprintf(stderr, "connection aborted\n");
return WOLFSSL_CBIO_ERR_CONN_CLOSE;
default:
fprintf(stderr, "general error\n");
return WOLFSSL_CBIO_ERR_GENERAL;
}
}
else if (recvd == 0) {
printf("Connection closed\n");
return WOLFSSL_CBIO_ERR_CONN_CLOSE;
}
/* successful receive */
printf("my_IORecv: received %d bytes from %d\n", recvd, shared->sd);
return recvd;
}
int my_IOSend(WOLFSSL* ssl, char* buff, int sz, void* ctx)
{
SharedDtls* shared = (SharedDtls*)ctx;
int sent;
const struct sockaddr* addr = NULL;
socklen_t addrSz = 0;
if (shared) {
addr = (const struct sockaddr*)&shared->servAddr;
addrSz = shared->servSz;
}
printf("my_IOSend fd %d, buf %d\n", shared->sd, sz);
/* Send datagram */
sent = sendto(shared->sd, buff, sz, 0, addr, addrSz);
if (sent == -1) {
/* error encountered. Be responsible and report it in wolfSSL terms */
fprintf(stderr, "IO SEND ERROR: %d\n", errno);
switch (errno) {
#if EAGAIN != EWOULDBLOCK
case EAGAIN: /* EAGAIN == EWOULDBLOCK on some systems, but not others */
#endif
case EWOULDBLOCK:
fprintf(stderr, "would block\n");
return WOLFSSL_CBIO_ERR_WANT_READ;
case ECONNRESET:
fprintf(stderr, "connection reset\n");
return WOLFSSL_CBIO_ERR_CONN_RST;
case EINTR:
fprintf(stderr, "socket interrupted\n");
cleanup = 1;
return WOLFSSL_CBIO_ERR_GENERAL;
/* NOTE: Don't return WOLFSSL_CBIO_ERR_ISR. It keeps trying to send */
case EPIPE:
fprintf(stderr, "socket EPIPE\n");
return WOLFSSL_CBIO_ERR_CONN_CLOSE;
default:
fprintf(stderr, "general error\n");
return WOLFSSL_CBIO_ERR_GENERAL;
}
}
else if (sent == 0) {
printf("Connection closed\n");
return 0;
}
/* successful send */
printf("my_IOSend: sent %d bytes to %d\n", sent, shared->sd);
return sent;
}
int main (int argc, char** argv)
{
/* standard variables used in a dtls client */
int ret = 0, err;
int sockfd = -1;
WOLFSSL* ssl = NULL;
WOLFSSL_CTX* ctx = NULL;
const char* ca_cert = "../certs/ca-cert.pem";
char buff[MSGLEN];
int buffLen;
SharedDtls shared;
/* Program argument checking */
if (argc != 2) {
printf("usage: udpcli <IP address>\n");
return 1;
}
/* Code for handling signals */
struct sigaction act, oact;
act.sa_handler = sig_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, &oact);
wolfSSL_Debugging_ON();
/* Initialize wolfSSL before assigning ctx */
wolfSSL_Init();
if ( (ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method())) == NULL) {
fprintf(stderr, "wolfSSL_CTX_new error.\n");
goto exit;
}
/* Register callbacks */
wolfSSL_CTX_SetIORecv(ctx, my_IORecv);
wolfSSL_CTX_SetIOSend(ctx, my_IOSend);
/* Load CA certificates into ctx variable */
if (wolfSSL_CTX_load_verify_locations(ctx, ca_cert, 0)
!= SSL_SUCCESS) {
fprintf(stderr, "Error loading %s, please check the file.\n", ca_cert);
goto exit;
}
/* Assign ssl variable */
ssl = wolfSSL_new(ctx);
if (ssl == NULL) {
printf("unable to get ssl object");
goto exit;
}
memset(&shared, 0, sizeof(shared));
shared.ssl = ssl;
/* servAddr setup */
shared.servSz = sizeof(shared.servAddr);
shared.servAddr.sin_family = AF_INET;
shared.servAddr.sin_port = htons(SERV_PORT);
if (inet_pton(AF_INET, argv[1], &shared.servAddr.sin_addr) < 1) {
printf("Error and/or invalid IP address");
goto exit;
}
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("cannot create a socket.");
goto exit;
}
shared.sd = sockfd;
wolfSSL_SetIOWriteCtx(ssl, &shared);
wolfSSL_SetIOReadCtx(ssl, &shared);
if (wolfSSL_connect(ssl) != SSL_SUCCESS) {
err = wolfSSL_get_error(ssl, 0);
printf("err = %d, %s\n", err, wolfSSL_ERR_reason_error_string(err));
printf("SSL_connect failed\n");
goto exit;
}
/**************************************************************************/
/* Code for sending datagram to server */
if (fgets(buff, sizeof(buff), stdin) != NULL) {
/* Send buffer to the server */
buffLen = strlen(buff);
if (( wolfSSL_write(ssl, buff, buffLen)) != buffLen) {
err = wolfSSL_get_error(ssl, 0);
if (err != SSL_ERROR_WANT_WRITE) {
printf("err = %d, %s\n", err, wolfSSL_ERR_reason_error_string(err));
printf("SSL_write failed\n");
goto exit;
}
}
/* Receive message from server */
ret = wolfSSL_read(ssl, buff, sizeof(buff)-1);
if (ret < 0) {
err = wolfSSL_get_error(ssl, 0);
if (err != SSL_ERROR_WANT_READ) {
printf("err = %d, %s\n", err, wolfSSL_ERR_reason_error_string(err));
printf("SSL_read failed\n");
goto exit;
}
}
buffLen = ret;
ret = 0;
/* Add a terminating character to the generic server message */
buff[buffLen] = '\0';
fputs(buff, stdout);
}
/* End code for sending datagram to server */
/**************************************************************************/
exit:
/* Housekeeping */
if (ssl) {
wolfSSL_shutdown(ssl);
wolfSSL_free(ssl);
}
if (sockfd != -1) {
close(sockfd);
}
if (ctx) {
wolfSSL_CTX_free(ctx);
}
wolfSSL_Cleanup();
return ret;
}