Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Simple DTLS server demonstration program |
| 3 | * |
| 4 | * Copyright (C) 2014, Brainspark B.V. |
| 5 | * |
Manuel Pégourié-Gonnard | e4d4890 | 2015-03-06 13:40:52 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 25 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 27 | #endif |
| 28 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame] | 31 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 32 | #define mbedtls_printf printf |
| 33 | #define mbedtls_fprintf fprintf |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | #if !defined(MBEDTLS_SSL_SRV_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) || \ |
| 37 | !defined(MBEDTLS_SSL_COOKIE_C) || !defined(MBEDTLS_NET_C) || \ |
| 38 | !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \ |
| 39 | !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_RSA_C) || \ |
| 40 | !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C) |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 41 | |
| 42 | #include <stdio.h> |
| 43 | int main( void ) |
| 44 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 45 | printf( "MBEDTLS_SSL_SRV_C and/or MBEDTLS_SSL_PROTO_DTLS and/or " |
| 46 | "MBEDTLS_SSL_COOKIE_C and/or MBEDTLS_NET_C and/or " |
| 47 | "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or " |
| 48 | "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_RSA_C and/or " |
| 49 | "MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C not defined.\n" ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 50 | return( 0 ); |
| 51 | } |
| 52 | #else |
| 53 | |
| 54 | #if defined(_WIN32) |
| 55 | #include <windows.h> |
| 56 | #endif |
| 57 | |
| 58 | #include <string.h> |
| 59 | #include <stdlib.h> |
| 60 | #include <stdio.h> |
| 61 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 62 | #include "mbedtls/entropy.h" |
| 63 | #include "mbedtls/ctr_drbg.h" |
| 64 | #include "mbedtls/certs.h" |
| 65 | #include "mbedtls/x509.h" |
| 66 | #include "mbedtls/ssl.h" |
| 67 | #include "mbedtls/ssl_cookie.h" |
| 68 | #include "mbedtls/net.h" |
| 69 | #include "mbedtls/error.h" |
| 70 | #include "mbedtls/debug.h" |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 71 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 72 | #if defined(MBEDTLS_SSL_CACHE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 73 | #include "mbedtls/ssl_cache.h" |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 74 | #endif |
| 75 | |
| 76 | #define READ_TIMEOUT_MS 10000 /* 5 seconds */ |
| 77 | #define DEBUG_LEVEL 0 |
| 78 | |
| 79 | static void my_debug( void *ctx, int level, const char *str ) |
| 80 | { |
| 81 | ((void) level); |
| 82 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 83 | mbedtls_fprintf( (FILE *) ctx, "%s", str ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 84 | fflush( (FILE *) ctx ); |
| 85 | } |
| 86 | |
| 87 | int main( void ) |
| 88 | { |
| 89 | int ret, len; |
| 90 | int listen_fd; |
| 91 | int client_fd = -1; |
| 92 | unsigned char buf[1024]; |
| 93 | const char *pers = "dtls_server"; |
| 94 | unsigned char client_ip[16] = { 0 }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 95 | mbedtls_ssl_cookie_ctx cookie_ctx; |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 96 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 97 | mbedtls_entropy_context entropy; |
| 98 | mbedtls_ctr_drbg_context ctr_drbg; |
| 99 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 100 | mbedtls_ssl_config conf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 101 | mbedtls_x509_crt srvcert; |
| 102 | mbedtls_pk_context pkey; |
| 103 | #if defined(MBEDTLS_SSL_CACHE_C) |
| 104 | mbedtls_ssl_cache_context cache; |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 105 | #endif |
| 106 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 107 | mbedtls_ssl_init( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 108 | mbedtls_ssl_config_init( &conf ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 109 | mbedtls_ssl_cookie_init( &cookie_ctx ); |
| 110 | #if defined(MBEDTLS_SSL_CACHE_C) |
| 111 | mbedtls_ssl_cache_init( &cache ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 112 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 113 | mbedtls_x509_crt_init( &srvcert ); |
| 114 | mbedtls_pk_init( &pkey ); |
| 115 | mbedtls_entropy_init( &entropy ); |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 116 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 117 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 118 | #if defined(MBEDTLS_DEBUG_C) |
| 119 | mbedtls_debug_set_threshold( DEBUG_LEVEL ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 120 | #endif |
| 121 | |
| 122 | /* |
| 123 | * 1. Load the certificates and private RSA key |
| 124 | */ |
| 125 | printf( "\n . Loading the server cert. and key..." ); |
| 126 | fflush( stdout ); |
| 127 | |
| 128 | /* |
| 129 | * This demonstration program uses embedded test certificates. |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the |
| 131 | * server and CA certificates, as well as mbedtls_pk_parse_keyfile(). |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 132 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt, |
| 134 | mbedtls_test_srv_crt_len ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 135 | if( ret != 0 ) |
| 136 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 137 | printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 138 | goto exit; |
| 139 | } |
| 140 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 141 | ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem, |
| 142 | mbedtls_test_cas_pem_len ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 143 | if( ret != 0 ) |
| 144 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 146 | goto exit; |
| 147 | } |
| 148 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 149 | ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key, |
| 150 | mbedtls_test_srv_key_len, NULL, 0 ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 151 | if( ret != 0 ) |
| 152 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 153 | printf( " failed\n ! mbedtls_pk_parse_key returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 154 | goto exit; |
| 155 | } |
| 156 | |
| 157 | printf( " ok\n" ); |
| 158 | |
| 159 | /* |
| 160 | * 2. Setup the "listening" UDP socket |
| 161 | */ |
| 162 | printf( " . Bind on udp/*/4433 ..." ); |
| 163 | fflush( stdout ); |
| 164 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 165 | if( ( ret = mbedtls_net_bind( &listen_fd, NULL, 4433, MBEDTLS_NET_PROTO_UDP ) ) != 0 ) |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 166 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 168 | goto exit; |
| 169 | } |
| 170 | |
| 171 | printf( " ok\n" ); |
| 172 | |
| 173 | /* |
| 174 | * 3. Seed the RNG |
| 175 | */ |
| 176 | printf( " . Seeding the random number generator..." ); |
| 177 | fflush( stdout ); |
| 178 | |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 179 | if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 180 | (const unsigned char *) pers, |
| 181 | strlen( pers ) ) ) != 0 ) |
| 182 | { |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 183 | printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 184 | goto exit; |
| 185 | } |
| 186 | |
| 187 | printf( " ok\n" ); |
| 188 | |
| 189 | /* |
| 190 | * 4. Setup stuff |
| 191 | */ |
| 192 | printf( " . Setting up the DTLS data..." ); |
| 193 | fflush( stdout ); |
| 194 | |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 195 | if( ( ret = mbedtls_ssl_config_defaults( &conf, |
| 196 | MBEDTLS_SSL_IS_SERVER, |
| 197 | MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ) != 0 ) |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 198 | { |
| 199 | mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret ); |
| 200 | goto exit; |
| 201 | } |
| 202 | |
| 203 | if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 ) |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 204 | { |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 205 | printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 206 | goto exit; |
| 207 | } |
| 208 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 209 | mbedtls_ssl_set_rng( &ssl, mbedtls_ctr_drbg_random, &ctr_drbg ); |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 210 | mbedtls_ssl_set_dbg( &conf, my_debug, stdout ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 211 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 212 | #if defined(MBEDTLS_SSL_CACHE_C) |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame^] | 213 | mbedtls_ssl_set_session_cache( &conf, &cache, |
| 214 | mbedtls_ssl_cache_get, |
| 215 | mbedtls_ssl_cache_set ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 216 | #endif |
| 217 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 218 | mbedtls_ssl_set_ca_chain( &conf, srvcert.next, NULL ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 219 | if( ( ret = mbedtls_ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 ) |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 220 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 221 | printf( " failed\n ! mbedtls_ssl_set_own_cert returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 222 | goto exit; |
| 223 | } |
| 224 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx, |
| 226 | mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 227 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 228 | printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 229 | goto exit; |
| 230 | } |
| 231 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 232 | mbedtls_ssl_set_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check, |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 233 | &cookie_ctx ); |
| 234 | |
| 235 | printf( " ok\n" ); |
| 236 | |
| 237 | reset: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 238 | #ifdef MBEDTLS_ERROR_C |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 239 | if( ret != 0 ) |
| 240 | { |
| 241 | char error_buf[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 242 | mbedtls_strerror( ret, error_buf, 100 ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 243 | printf("Last error was: %d - %s\n\n", ret, error_buf ); |
| 244 | } |
| 245 | #endif |
| 246 | |
| 247 | if( client_fd != -1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 248 | mbedtls_net_close( client_fd ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 249 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 250 | mbedtls_ssl_session_reset( &ssl ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 251 | |
| 252 | /* |
| 253 | * 3. Wait until a client connects |
| 254 | */ |
| 255 | client_fd = -1; |
| 256 | |
| 257 | printf( " . Waiting for a remote connection ..." ); |
| 258 | fflush( stdout ); |
| 259 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 260 | if( ( ret = mbedtls_net_accept( listen_fd, &client_fd, client_ip ) ) != 0 ) |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 261 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 262 | printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 263 | goto exit; |
| 264 | } |
| 265 | |
| 266 | /* With UDP, bind_fd is hijacked by client_fd, so bind a new one */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 267 | if( ( ret = mbedtls_net_bind( &listen_fd, NULL, 4433, MBEDTLS_NET_PROTO_UDP ) ) != 0 ) |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 268 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 269 | printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 270 | goto exit; |
| 271 | } |
| 272 | |
| 273 | /* For HelloVerifyRequest cookies */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 274 | if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl, client_ip, |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 275 | sizeof( client_ip ) ) ) != 0 ) |
| 276 | { |
| 277 | printf( " failed\n ! " |
| 278 | "ssl_set_client_tranport_id() returned -0x%x\n\n", -ret ); |
| 279 | goto exit; |
| 280 | } |
| 281 | |
Manuel Pégourié-Gonnard | 1b511f9 | 2015-05-06 15:54:23 +0100 | [diff] [blame] | 282 | mbedtls_ssl_set_bio( &ssl, &client_fd, |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 283 | mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 284 | |
| 285 | printf( " ok\n" ); |
| 286 | |
| 287 | /* |
| 288 | * 5. Handshake |
| 289 | */ |
| 290 | printf( " . Performing the DTLS handshake..." ); |
| 291 | fflush( stdout ); |
| 292 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 293 | do ret = mbedtls_ssl_handshake( &ssl ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 294 | while( ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 295 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 296 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 297 | if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ) |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 298 | { |
| 299 | printf( " hello verification requested\n" ); |
| 300 | ret = 0; |
| 301 | goto reset; |
| 302 | } |
| 303 | else if( ret != 0 ) |
| 304 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 305 | printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 306 | goto reset; |
| 307 | } |
| 308 | |
| 309 | printf( " ok\n" ); |
| 310 | |
| 311 | /* |
| 312 | * 6. Read the echo Request |
| 313 | */ |
| 314 | printf( " < Read from client:" ); |
| 315 | fflush( stdout ); |
| 316 | |
| 317 | len = sizeof( buf ) - 1; |
| 318 | memset( buf, 0, sizeof( buf ) ); |
| 319 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 320 | do ret = mbedtls_ssl_read( &ssl, buf, len ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 321 | while( ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 322 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 323 | |
| 324 | if( ret <= 0 ) |
| 325 | { |
| 326 | switch( ret ) |
| 327 | { |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 328 | case MBEDTLS_ERR_SSL_TIMEOUT: |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 329 | printf( " timeout\n\n" ); |
| 330 | goto reset; |
| 331 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 332 | case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 333 | printf( " connection was closed gracefully\n" ); |
| 334 | ret = 0; |
| 335 | goto close_notify; |
| 336 | |
| 337 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 338 | printf( " mbedtls_ssl_read returned -0x%x\n\n", -ret ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 339 | goto reset; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | len = ret; |
| 344 | printf( " %d bytes read\n\n%s\n\n", len, buf ); |
| 345 | |
| 346 | /* |
| 347 | * 7. Write the 200 Response |
| 348 | */ |
| 349 | printf( " > Write to client:" ); |
| 350 | fflush( stdout ); |
| 351 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 352 | do ret = mbedtls_ssl_write( &ssl, buf, len ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 353 | while( ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 354 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 355 | |
| 356 | if( ret < 0 ) |
| 357 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 358 | printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 359 | goto exit; |
| 360 | } |
| 361 | |
| 362 | len = ret; |
| 363 | printf( " %d bytes written\n\n%s\n\n", len, buf ); |
| 364 | |
| 365 | /* |
| 366 | * 8. Done, cleanly close the connection |
| 367 | */ |
| 368 | close_notify: |
| 369 | printf( " . Closing the connection..." ); |
| 370 | |
| 371 | /* No error checking, the connection might be closed already */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 372 | do ret = mbedtls_ssl_close_notify( &ssl ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 373 | while( ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 374 | ret = 0; |
| 375 | |
| 376 | printf( " done\n" ); |
| 377 | |
| 378 | goto reset; |
| 379 | |
| 380 | /* |
| 381 | * Final clean-ups and exit |
| 382 | */ |
| 383 | exit: |
| 384 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 385 | #ifdef MBEDTLS_ERROR_C |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 386 | if( ret != 0 ) |
| 387 | { |
| 388 | char error_buf[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 389 | mbedtls_strerror( ret, error_buf, 100 ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 390 | printf( "Last error was: %d - %s\n\n", ret, error_buf ); |
| 391 | } |
| 392 | #endif |
| 393 | |
| 394 | if( client_fd != -1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 395 | mbedtls_net_close( client_fd ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 396 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 397 | mbedtls_x509_crt_free( &srvcert ); |
| 398 | mbedtls_pk_free( &pkey ); |
| 399 | mbedtls_ssl_free( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 400 | mbedtls_ssl_config_free( &conf ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 401 | mbedtls_ssl_cookie_free( &cookie_ctx ); |
| 402 | #if defined(MBEDTLS_SSL_CACHE_C) |
| 403 | mbedtls_ssl_cache_free( &cache ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 404 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 405 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 406 | mbedtls_entropy_free( &entropy ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 407 | |
| 408 | #if defined(_WIN32) |
| 409 | printf( " Press Enter to exit this program.\n" ); |
| 410 | fflush( stdout ); getchar(); |
| 411 | #endif |
| 412 | |
| 413 | /* Shell can not handle large exit numbers -> 1 for errors */ |
| 414 | if( ret < 0 ) |
| 415 | ret = 1; |
| 416 | |
| 417 | return( ret ); |
| 418 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 419 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_DTLS && |
| 420 | MBEDTLS_SSL_COOKIE_C && MBEDTLS_NET_C && MBEDTLS_ENTROPY_C && |
| 421 | MBEDTLS_CTR_DRBG_C && MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C |
| 422 | && MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C */ |