-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathclient-dtls13.c
198 lines (175 loc) · 5.97 KB
/
client-dtls13.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
/*
* client-dtls13.c
*
* Copyright (C) 2006-2024 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 1.3 client for instructional/learning purposes.
* This example uses blocking sockets for simplicity.
*
* To exit the sending loop enter "end" or Ctrl+D
*
* 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>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dtls-common.h"
int main (int argc, char** argv)
{
/* standard variables used in a dtls client */
int n = 0;
int sockfd = INVALID_SOCKET;
int err;
int ret;
int exitVal = 1;
struct sockaddr_in servAddr;
WOLFSSL* ssl = NULL;
WOLFSSL_CTX* ctx = NULL;
char sendLine[MAXLINE];
char recvLine[MAXLINE - 1];
/* Program argument checking */
if (argc != 2) {
fprintf(stderr, "usage: %s <IP address>\n", argv[0]);
return exitVal;
}
/* Initialize wolfSSL before assigning ctx */
if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
fprintf(stderr, "wolfSSL_Init error.\n");
return exitVal;
}
/* No-op when debugging is not compiled in */
wolfSSL_Debugging_ON();
if ((ctx = wolfSSL_CTX_new(
#ifdef WOLFSSL_DTLS13
wolfDTLSv1_3_client_method()
#else
wolfDTLSv1_2_client_method()
#endif
)) == NULL) {
fprintf(stderr, "wolfSSL_CTX_new error.\n");
goto cleanup;
}
/* Load certificates into ctx variable */
if (wolfSSL_CTX_load_verify_locations(ctx, caCertLoc, NULL)
!= WOLFSSL_SUCCESS) {
fprintf(stderr, "Error loading %s, please check the file.\n", caCertLoc);
goto cleanup;
}
/* Assign ssl variable */
ssl = wolfSSL_new(ctx);
if (ssl == NULL) {
fprintf(stderr, "unable to get ssl object\n");
goto cleanup;
}
/* servAddr setup */
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(SERV_PORT);
if (inet_pton(AF_INET, argv[1], &servAddr.sin_addr) < 1) {
perror("inet_pton()");
goto cleanup;
}
if (wolfSSL_dtls_set_peer(ssl, &servAddr, sizeof(servAddr))
!= WOLFSSL_SUCCESS) {
fprintf(stderr, "wolfSSL_dtls_set_peer failed\n");
goto cleanup;
}
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket()");
goto cleanup;
}
/* Set the file descriptor for ssl */
if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
fprintf(stderr, "cannot set socket file descriptor\n");
goto cleanup;
}
/* Perform SSL connection */
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");
goto cleanup;
}
showConnInfo(ssl);
/*****************************************************************************/
/* Code for sending datagram to server */
while (1) {
if (fgets(sendLine, MAXLINE, stdin) == NULL)
break;
if (strncmp(sendLine, "end", 3) == 0)
break;
/* 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, "wolfSSL_write failed\n");
goto cleanup;
}
/* n is the # of bytes received */
n = wolfSSL_read(ssl, recvLine, sizeof(recvLine)-1);
if (n > 0) {
/* Add a terminating character to the generic server message */
recvLine[n] = '\0';
printf("%s\n", recvLine);
}
else {
err = wolfSSL_get_error(ssl, 0);
fprintf(stderr, "err = %d, %s\n", err,
wolfSSL_ERR_reason_error_string(err));
fprintf(stderr, "wolfSSL_read failed\n");
goto cleanup;
}
}
/* End code for sending datagram to server */
/*****************************************************************************/
exitVal = 0;
cleanup:
if (ssl != NULL) {
/* Attempt a full shutdown */
ret = wolfSSL_shutdown(ssl);
if (ret == WOLFSSL_SHUTDOWN_NOT_DONE)
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, "wolfSSL_shutdown failed\n");
}
wolfSSL_free(ssl);
}
if (sockfd != INVALID_SOCKET)
close(sockfd);
if (ctx != NULL)
wolfSSL_CTX_free(ctx);
wolfSSL_Cleanup();
return exitVal;
}