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; |
| 100 | mbedtls_x509_crt srvcert; |
| 101 | mbedtls_pk_context pkey; |
| 102 | #if defined(MBEDTLS_SSL_CACHE_C) |
| 103 | mbedtls_ssl_cache_context cache; |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 104 | #endif |
| 105 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 106 | memset( &ssl, 0, sizeof(mbedtls_ssl_context) ); |
| 107 | mbedtls_ssl_cookie_init( &cookie_ctx ); |
| 108 | #if defined(MBEDTLS_SSL_CACHE_C) |
| 109 | mbedtls_ssl_cache_init( &cache ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 110 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 111 | mbedtls_x509_crt_init( &srvcert ); |
| 112 | mbedtls_pk_init( &pkey ); |
| 113 | mbedtls_entropy_init( &entropy ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 114 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 115 | #if defined(MBEDTLS_DEBUG_C) |
| 116 | mbedtls_debug_set_threshold( DEBUG_LEVEL ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 117 | #endif |
| 118 | |
| 119 | /* |
| 120 | * 1. Load the certificates and private RSA key |
| 121 | */ |
| 122 | printf( "\n . Loading the server cert. and key..." ); |
| 123 | fflush( stdout ); |
| 124 | |
| 125 | /* |
| 126 | * This demonstration program uses embedded test certificates. |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 127 | * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the |
| 128 | * 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] | 129 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 130 | ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt, |
| 131 | mbedtls_test_srv_crt_len ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 132 | if( ret != 0 ) |
| 133 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 134 | 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] | 135 | goto exit; |
| 136 | } |
| 137 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 138 | ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem, |
| 139 | mbedtls_test_cas_pem_len ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 140 | if( ret != 0 ) |
| 141 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 142 | 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] | 143 | goto exit; |
| 144 | } |
| 145 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 146 | ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key, |
| 147 | mbedtls_test_srv_key_len, NULL, 0 ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 148 | if( ret != 0 ) |
| 149 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 150 | 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] | 151 | goto exit; |
| 152 | } |
| 153 | |
| 154 | printf( " ok\n" ); |
| 155 | |
| 156 | /* |
| 157 | * 2. Setup the "listening" UDP socket |
| 158 | */ |
| 159 | printf( " . Bind on udp/*/4433 ..." ); |
| 160 | fflush( stdout ); |
| 161 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 162 | 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] | 163 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 164 | 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] | 165 | goto exit; |
| 166 | } |
| 167 | |
| 168 | printf( " ok\n" ); |
| 169 | |
| 170 | /* |
| 171 | * 3. Seed the RNG |
| 172 | */ |
| 173 | printf( " . Seeding the random number generator..." ); |
| 174 | fflush( stdout ); |
| 175 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 176 | if( ( ret = mbedtls_ctr_drbg_init( &ctr_drbg, mbedtls_entropy_func, &entropy, |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 177 | (const unsigned char *) pers, |
| 178 | strlen( pers ) ) ) != 0 ) |
| 179 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 180 | printf( " failed\n ! mbedtls_ctr_drbg_init returned %d\n", ret ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 181 | goto exit; |
| 182 | } |
| 183 | |
| 184 | printf( " ok\n" ); |
| 185 | |
| 186 | /* |
| 187 | * 4. Setup stuff |
| 188 | */ |
| 189 | printf( " . Setting up the DTLS data..." ); |
| 190 | fflush( stdout ); |
| 191 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 192 | if( ( ret = mbedtls_ssl_init( &ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 193 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 194 | printf( " failed\n ! mbedtls_ssl_init returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 195 | goto exit; |
| 196 | } |
| 197 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 198 | mbedtls_ssl_set_endpoint( &ssl, MBEDTLS_SSL_IS_SERVER ); |
| 199 | mbedtls_ssl_set_transport( &ssl, MBEDTLS_SSL_TRANSPORT_DATAGRAM ); |
| 200 | mbedtls_ssl_set_authmode( &ssl, MBEDTLS_SSL_VERIFY_NONE ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 201 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 202 | mbedtls_ssl_set_rng( &ssl, mbedtls_ctr_drbg_random, &ctr_drbg ); |
| 203 | mbedtls_ssl_set_dbg( &ssl, my_debug, stdout ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 204 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 205 | #if defined(MBEDTLS_SSL_CACHE_C) |
| 206 | mbedtls_ssl_set_session_cache( &ssl, mbedtls_ssl_cache_get, &cache, |
| 207 | mbedtls_ssl_cache_set, &cache ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 208 | #endif |
| 209 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 210 | mbedtls_ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL ); |
| 211 | 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] | 212 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 213 | 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] | 214 | goto exit; |
| 215 | } |
| 216 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 217 | if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx, |
| 218 | mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 219 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 220 | 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] | 221 | goto exit; |
| 222 | } |
| 223 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 224 | mbedtls_ssl_set_dtls_cookies( &ssl, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check, |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 225 | &cookie_ctx ); |
| 226 | |
| 227 | printf( " ok\n" ); |
| 228 | |
| 229 | reset: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 230 | #ifdef MBEDTLS_ERROR_C |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 231 | if( ret != 0 ) |
| 232 | { |
| 233 | char error_buf[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 234 | mbedtls_strerror( ret, error_buf, 100 ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 235 | printf("Last error was: %d - %s\n\n", ret, error_buf ); |
| 236 | } |
| 237 | #endif |
| 238 | |
| 239 | if( client_fd != -1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 240 | mbedtls_net_close( client_fd ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 241 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 242 | mbedtls_ssl_session_reset( &ssl ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 243 | |
| 244 | /* |
| 245 | * 3. Wait until a client connects |
| 246 | */ |
| 247 | client_fd = -1; |
| 248 | |
| 249 | printf( " . Waiting for a remote connection ..." ); |
| 250 | fflush( stdout ); |
| 251 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 252 | 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] | 253 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 254 | 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] | 255 | goto exit; |
| 256 | } |
| 257 | |
| 258 | /* 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^] | 259 | 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] | 260 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 261 | 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] | 262 | goto exit; |
| 263 | } |
| 264 | |
| 265 | /* For HelloVerifyRequest cookies */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 266 | 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] | 267 | sizeof( client_ip ) ) ) != 0 ) |
| 268 | { |
| 269 | printf( " failed\n ! " |
| 270 | "ssl_set_client_tranport_id() returned -0x%x\n\n", -ret ); |
| 271 | goto exit; |
| 272 | } |
| 273 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 274 | mbedtls_ssl_set_bio_timeout( &ssl, &client_fd, |
| 275 | mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout, |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 276 | READ_TIMEOUT_MS ); |
| 277 | |
| 278 | printf( " ok\n" ); |
| 279 | |
| 280 | /* |
| 281 | * 5. Handshake |
| 282 | */ |
| 283 | printf( " . Performing the DTLS handshake..." ); |
| 284 | fflush( stdout ); |
| 285 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 286 | do ret = mbedtls_ssl_handshake( &ssl ); |
| 287 | while( ret == MBEDTLS_ERR_NET_WANT_READ || |
| 288 | ret == MBEDTLS_ERR_NET_WANT_WRITE ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 289 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 290 | if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ) |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 291 | { |
| 292 | printf( " hello verification requested\n" ); |
| 293 | ret = 0; |
| 294 | goto reset; |
| 295 | } |
| 296 | else if( ret != 0 ) |
| 297 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 298 | 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] | 299 | goto reset; |
| 300 | } |
| 301 | |
| 302 | printf( " ok\n" ); |
| 303 | |
| 304 | /* |
| 305 | * 6. Read the echo Request |
| 306 | */ |
| 307 | printf( " < Read from client:" ); |
| 308 | fflush( stdout ); |
| 309 | |
| 310 | len = sizeof( buf ) - 1; |
| 311 | memset( buf, 0, sizeof( buf ) ); |
| 312 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 313 | do ret = mbedtls_ssl_read( &ssl, buf, len ); |
| 314 | while( ret == MBEDTLS_ERR_NET_WANT_READ || |
| 315 | ret == MBEDTLS_ERR_NET_WANT_WRITE ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 316 | |
| 317 | if( ret <= 0 ) |
| 318 | { |
| 319 | switch( ret ) |
| 320 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 321 | case MBEDTLS_ERR_NET_TIMEOUT: |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 322 | printf( " timeout\n\n" ); |
| 323 | goto reset; |
| 324 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 325 | case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 326 | printf( " connection was closed gracefully\n" ); |
| 327 | ret = 0; |
| 328 | goto close_notify; |
| 329 | |
| 330 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 331 | printf( " mbedtls_ssl_read returned -0x%x\n\n", -ret ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 332 | goto reset; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | len = ret; |
| 337 | printf( " %d bytes read\n\n%s\n\n", len, buf ); |
| 338 | |
| 339 | /* |
| 340 | * 7. Write the 200 Response |
| 341 | */ |
| 342 | printf( " > Write to client:" ); |
| 343 | fflush( stdout ); |
| 344 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 345 | do ret = mbedtls_ssl_write( &ssl, buf, len ); |
| 346 | while( ret == MBEDTLS_ERR_NET_WANT_READ || |
| 347 | ret == MBEDTLS_ERR_NET_WANT_WRITE ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 348 | |
| 349 | if( ret < 0 ) |
| 350 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 351 | 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] | 352 | goto exit; |
| 353 | } |
| 354 | |
| 355 | len = ret; |
| 356 | printf( " %d bytes written\n\n%s\n\n", len, buf ); |
| 357 | |
| 358 | /* |
| 359 | * 8. Done, cleanly close the connection |
| 360 | */ |
| 361 | close_notify: |
| 362 | printf( " . Closing the connection..." ); |
| 363 | |
| 364 | /* No error checking, the connection might be closed already */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 365 | do ret = mbedtls_ssl_close_notify( &ssl ); |
| 366 | while( ret == MBEDTLS_ERR_NET_WANT_WRITE ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 367 | ret = 0; |
| 368 | |
| 369 | printf( " done\n" ); |
| 370 | |
| 371 | goto reset; |
| 372 | |
| 373 | /* |
| 374 | * Final clean-ups and exit |
| 375 | */ |
| 376 | exit: |
| 377 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 378 | #ifdef MBEDTLS_ERROR_C |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 379 | if( ret != 0 ) |
| 380 | { |
| 381 | char error_buf[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 382 | mbedtls_strerror( ret, error_buf, 100 ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 383 | printf( "Last error was: %d - %s\n\n", ret, error_buf ); |
| 384 | } |
| 385 | #endif |
| 386 | |
| 387 | if( client_fd != -1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 388 | mbedtls_net_close( client_fd ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 389 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 390 | mbedtls_x509_crt_free( &srvcert ); |
| 391 | mbedtls_pk_free( &pkey ); |
| 392 | mbedtls_ssl_free( &ssl ); |
| 393 | mbedtls_ssl_cookie_free( &cookie_ctx ); |
| 394 | #if defined(MBEDTLS_SSL_CACHE_C) |
| 395 | mbedtls_ssl_cache_free( &cache ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 396 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 397 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 398 | mbedtls_entropy_free( &entropy ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 399 | |
| 400 | #if defined(_WIN32) |
| 401 | printf( " Press Enter to exit this program.\n" ); |
| 402 | fflush( stdout ); getchar(); |
| 403 | #endif |
| 404 | |
| 405 | /* Shell can not handle large exit numbers -> 1 for errors */ |
| 406 | if( ret < 0 ) |
| 407 | ret = 1; |
| 408 | |
| 409 | return( ret ); |
| 410 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 411 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_DTLS && |
| 412 | MBEDTLS_SSL_COOKIE_C && MBEDTLS_NET_C && MBEDTLS_ENTROPY_C && |
| 413 | MBEDTLS_CTR_DRBG_C && MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C |
| 414 | && MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C */ |