1 /*
2 * Simple DTLS client demonstration program
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 #include "mbedtls/build_info.h"
21
22 #include "mbedtls/platform.h"
23
24 #if !defined(MBEDTLS_SSL_CLI_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) || \
25 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_TIMING_C) || \
26 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
27 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_RSA_C) || \
28 !defined(MBEDTLS_PEM_PARSE_C)
main(void)29 int main(void)
30 {
31 mbedtls_printf("MBEDTLS_SSL_CLI_C and/or MBEDTLS_SSL_PROTO_DTLS and/or "
32 "MBEDTLS_NET_C and/or MBEDTLS_TIMING_C and/or "
33 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
34 "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_RSA_C and/or "
35 "MBEDTLS_PEM_PARSE_C not defined.\n");
36 mbedtls_exit(0);
37 }
38 #else
39
40 #include <string.h>
41
42 #include "mbedtls/net_sockets.h"
43 #include "mbedtls/debug.h"
44 #include "mbedtls/ssl.h"
45 #include "mbedtls/entropy.h"
46 #include "mbedtls/ctr_drbg.h"
47 #include "mbedtls/error.h"
48 #include "mbedtls/timing.h"
49 #include "test/certs.h"
50
51 /* Uncomment out the following line to default to IPv4 and disable IPv6 */
52 //#define FORCE_IPV4
53
54 #define SERVER_PORT "4433"
55 #define SERVER_NAME "localhost"
56
57 #ifdef FORCE_IPV4
58 #define SERVER_ADDR "127.0.0.1" /* Forces IPv4 */
59 #else
60 #define SERVER_ADDR "::1"
61 #endif
62
63 #define MESSAGE "Echo this"
64
65 #define READ_TIMEOUT_MS 1000
66 #define MAX_RETRY 5
67
68 #define DEBUG_LEVEL 0
69
70
my_debug(void * ctx,int level,const char * file,int line,const char * str)71 static void my_debug(void *ctx, int level,
72 const char *file, int line,
73 const char *str)
74 {
75 ((void) level);
76
77 mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str);
78 fflush((FILE *) ctx);
79 }
80
main(int argc,char * argv[])81 int main(int argc, char *argv[])
82 {
83 int ret, len;
84 mbedtls_net_context server_fd;
85 uint32_t flags;
86 unsigned char buf[1024];
87 const char *pers = "dtls_client";
88 int retry_left = MAX_RETRY;
89
90 mbedtls_entropy_context entropy;
91 mbedtls_ctr_drbg_context ctr_drbg;
92 mbedtls_ssl_context ssl;
93 mbedtls_ssl_config conf;
94 mbedtls_x509_crt cacert;
95 mbedtls_timing_delay_context timer;
96
97 ((void) argc);
98 ((void) argv);
99
100 #if defined(MBEDTLS_DEBUG_C)
101 mbedtls_debug_set_threshold(DEBUG_LEVEL);
102 #endif
103
104 /*
105 * 0. Initialize the RNG and the session data
106 */
107 mbedtls_net_init(&server_fd);
108 mbedtls_ssl_init(&ssl);
109 mbedtls_ssl_config_init(&conf);
110 mbedtls_x509_crt_init(&cacert);
111 mbedtls_ctr_drbg_init(&ctr_drbg);
112
113 mbedtls_printf("\n . Seeding the random number generator...");
114 fflush(stdout);
115
116 mbedtls_entropy_init(&entropy);
117 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
118 (const unsigned char *) pers,
119 strlen(pers))) != 0) {
120 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
121 goto exit;
122 }
123
124 mbedtls_printf(" ok\n");
125
126 /*
127 * 0. Load certificates
128 */
129 mbedtls_printf(" . Loading the CA root certificate ...");
130 fflush(stdout);
131
132 ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_cas_pem,
133 mbedtls_test_cas_pem_len);
134 if (ret < 0) {
135 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
136 (unsigned int) -ret);
137 goto exit;
138 }
139
140 mbedtls_printf(" ok (%d skipped)\n", ret);
141
142 /*
143 * 1. Start the connection
144 */
145 mbedtls_printf(" . Connecting to udp/%s/%s...", SERVER_NAME, SERVER_PORT);
146 fflush(stdout);
147
148 if ((ret = mbedtls_net_connect(&server_fd, SERVER_ADDR,
149 SERVER_PORT, MBEDTLS_NET_PROTO_UDP)) != 0) {
150 mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
151 goto exit;
152 }
153
154 mbedtls_printf(" ok\n");
155
156 /*
157 * 2. Setup stuff
158 */
159 mbedtls_printf(" . Setting up the DTLS structure...");
160 fflush(stdout);
161
162 if ((ret = mbedtls_ssl_config_defaults(&conf,
163 MBEDTLS_SSL_IS_CLIENT,
164 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
165 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
166 mbedtls_printf(" failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret);
167 goto exit;
168 }
169
170 /* OPTIONAL is usually a bad choice for security, but makes interop easier
171 * in this simplified example, in which the ca chain is hardcoded.
172 * Production code should set a proper ca chain and use REQUIRED. */
173 mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
174 mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
175 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
176 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
177 mbedtls_ssl_conf_read_timeout(&conf, READ_TIMEOUT_MS);
178
179 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
180 mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned %d\n\n", ret);
181 goto exit;
182 }
183
184 if ((ret = mbedtls_ssl_set_hostname(&ssl, SERVER_NAME)) != 0) {
185 mbedtls_printf(" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret);
186 goto exit;
187 }
188
189 mbedtls_ssl_set_bio(&ssl, &server_fd,
190 mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout);
191
192 mbedtls_ssl_set_timer_cb(&ssl, &timer, mbedtls_timing_set_delay,
193 mbedtls_timing_get_delay);
194
195 mbedtls_printf(" ok\n");
196
197 /*
198 * 4. Handshake
199 */
200 mbedtls_printf(" . Performing the DTLS handshake...");
201 fflush(stdout);
202
203 do {
204 ret = mbedtls_ssl_handshake(&ssl);
205 } while (ret == MBEDTLS_ERR_SSL_WANT_READ ||
206 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
207
208 if (ret != 0) {
209 mbedtls_printf(" failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
210 (unsigned int) -ret);
211 goto exit;
212 }
213
214 mbedtls_printf(" ok\n");
215
216 /*
217 * 5. Verify the server certificate
218 */
219 mbedtls_printf(" . Verifying peer X.509 certificate...");
220
221 /* In real life, we would have used MBEDTLS_SSL_VERIFY_REQUIRED so that the
222 * handshake would not succeed if the peer's cert is bad. Even if we used
223 * MBEDTLS_SSL_VERIFY_OPTIONAL, we would bail out here if ret != 0 */
224 if ((flags = mbedtls_ssl_get_verify_result(&ssl)) != 0) {
225 #if !defined(MBEDTLS_X509_REMOVE_INFO)
226 char vrfy_buf[512];
227 #endif
228
229 mbedtls_printf(" failed\n");
230
231 #if !defined(MBEDTLS_X509_REMOVE_INFO)
232 mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags);
233
234 mbedtls_printf("%s\n", vrfy_buf);
235 #endif
236 } else {
237 mbedtls_printf(" ok\n");
238 }
239
240 /*
241 * 6. Write the echo request
242 */
243 send_request:
244 mbedtls_printf(" > Write to server:");
245 fflush(stdout);
246
247 len = sizeof(MESSAGE) - 1;
248
249 do {
250 ret = mbedtls_ssl_write(&ssl, (unsigned char *) MESSAGE, len);
251 } while (ret == MBEDTLS_ERR_SSL_WANT_READ ||
252 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
253
254 if (ret < 0) {
255 mbedtls_printf(" failed\n ! mbedtls_ssl_write returned %d\n\n", ret);
256 goto exit;
257 }
258
259 len = ret;
260 mbedtls_printf(" %d bytes written\n\n%s\n\n", len, MESSAGE);
261
262 /*
263 * 7. Read the echo response
264 */
265 mbedtls_printf(" < Read from server:");
266 fflush(stdout);
267
268 len = sizeof(buf) - 1;
269 memset(buf, 0, sizeof(buf));
270
271 do {
272 ret = mbedtls_ssl_read(&ssl, buf, len);
273 } while (ret == MBEDTLS_ERR_SSL_WANT_READ ||
274 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
275
276 if (ret <= 0) {
277 switch (ret) {
278 case MBEDTLS_ERR_SSL_TIMEOUT:
279 mbedtls_printf(" timeout\n\n");
280 if (retry_left-- > 0) {
281 goto send_request;
282 }
283 goto exit;
284
285 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
286 mbedtls_printf(" connection was closed gracefully\n");
287 ret = 0;
288 goto close_notify;
289
290 default:
291 mbedtls_printf(" mbedtls_ssl_read returned -0x%x\n\n", (unsigned int) -ret);
292 goto exit;
293 }
294 }
295
296 len = ret;
297 mbedtls_printf(" %d bytes read\n\n%s\n\n", len, buf);
298
299 /*
300 * 8. Done, cleanly close the connection
301 */
302 close_notify:
303 mbedtls_printf(" . Closing the connection...");
304
305 /* No error checking, the connection might be closed already */
306 do {
307 ret = mbedtls_ssl_close_notify(&ssl);
308 } while (ret == MBEDTLS_ERR_SSL_WANT_WRITE);
309 ret = 0;
310
311 mbedtls_printf(" done\n");
312
313 /*
314 * 9. Final clean-ups and exit
315 */
316 exit:
317
318 #ifdef MBEDTLS_ERROR_C
319 if (ret != 0) {
320 char error_buf[100];
321 mbedtls_strerror(ret, error_buf, 100);
322 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf);
323 }
324 #endif
325
326 mbedtls_net_free(&server_fd);
327
328 mbedtls_x509_crt_free(&cacert);
329 mbedtls_ssl_free(&ssl);
330 mbedtls_ssl_config_free(&conf);
331 mbedtls_ctr_drbg_free(&ctr_drbg);
332 mbedtls_entropy_free(&entropy);
333
334 /* Shell can not handle large exit numbers -> 1 for errors */
335 if (ret < 0) {
336 ret = 1;
337 }
338
339 mbedtls_exit(ret);
340 }
341 #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_NET_C &&
342 MBEDTLS_TIMING_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
343 MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C && MBEDTLS_PEM_PARSE_C */
344