Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSL client with options |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 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. |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 30 | #else |
Manuel Pégourié-Gonnard | 8a4d571 | 2014-06-24 14:19:59 +0200 | [diff] [blame] | 31 | #include <stdio.h> |
Simon Butcher | d3138c3 | 2016-04-27 01:26:50 +0100 | [diff] [blame] | 32 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #define mbedtls_free free |
Simon Butcher | d3138c3 | 2016-04-27 01:26:50 +0100 | [diff] [blame] | 34 | #define mbedtls_time time |
| 35 | #define mbedtls_time_t time_t |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 36 | #define mbedtls_calloc calloc |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | #define mbedtls_fprintf fprintf |
| 38 | #define mbedtls_printf printf |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 39 | #endif |
Manuel Pégourié-Gonnard | 8a4d571 | 2014-06-24 14:19:59 +0200 | [diff] [blame] | 40 | |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 41 | #if !defined(MBEDTLS_ENTROPY_C) || \ |
| 42 | !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_SRV_C) || \ |
Manuel Pégourié-Gonnard | d2377e7 | 2015-05-13 13:58:56 +0200 | [diff] [blame] | 43 | !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C) |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 44 | int main( void ) |
| 45 | { |
| 46 | mbedtls_printf("MBEDTLS_ENTROPY_C and/or " |
| 47 | "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or " |
Manuel Pégourié-Gonnard | d2377e7 | 2015-05-13 13:58:56 +0200 | [diff] [blame] | 48 | "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n"); |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 49 | return( 0 ); |
| 50 | } |
| 51 | #else |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 52 | |
Andres AG | 788aa4a | 2016-09-14 14:32:09 +0100 | [diff] [blame] | 53 | #include "mbedtls/net_sockets.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 54 | #include "mbedtls/ssl.h" |
| 55 | #include "mbedtls/entropy.h" |
| 56 | #include "mbedtls/ctr_drbg.h" |
| 57 | #include "mbedtls/certs.h" |
| 58 | #include "mbedtls/x509.h" |
| 59 | #include "mbedtls/error.h" |
| 60 | #include "mbedtls/debug.h" |
Manuel Pégourié-Gonnard | 56273da | 2015-05-26 12:19:45 +0200 | [diff] [blame] | 61 | #include "mbedtls/timing.h" |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 62 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 63 | #include <stdio.h> |
| 64 | #include <stdlib.h> |
| 65 | #include <string.h> |
Andres AG | 692ad84 | 2017-01-19 16:30:57 +0000 | [diff] [blame] | 66 | #include <stdint.h> |
Andres AG | 0b736db | 2017-02-08 14:05:57 +0000 | [diff] [blame] | 67 | |
| 68 | #if !defined(_MSC_VER) |
Andres AG | 692ad84 | 2017-01-19 16:30:57 +0000 | [diff] [blame] | 69 | #include <inttypes.h> |
Andres AG | 0b736db | 2017-02-08 14:05:57 +0000 | [diff] [blame] | 70 | #endif |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 71 | |
| 72 | #if !defined(_WIN32) |
| 73 | #include <signal.h> |
| 74 | #endif |
| 75 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 76 | #if defined(MBEDTLS_SSL_CACHE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 77 | #include "mbedtls/ssl_cache.h" |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 78 | #endif |
| 79 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 80 | #if defined(MBEDTLS_SSL_TICKET_C) |
| 81 | #include "mbedtls/ssl_ticket.h" |
| 82 | #endif |
| 83 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 84 | #if defined(MBEDTLS_SSL_COOKIE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 85 | #include "mbedtls/ssl_cookie.h" |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 86 | #endif |
| 87 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 88 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 89 | #include "mbedtls/memory_buffer_alloc.h" |
Paul Bakker | 82024bf | 2013-07-04 11:52:32 +0200 | [diff] [blame] | 90 | #endif |
| 91 | |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 92 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_FS_IO) |
| 93 | #define SNI_OPTION |
| 94 | #endif |
| 95 | |
| 96 | #if defined(_WIN32) |
| 97 | #include <windows.h> |
| 98 | #endif |
| 99 | |
Manuel Pégourié-Gonnard | 18d31f8 | 2013-12-13 16:21:41 +0100 | [diff] [blame] | 100 | #define DFL_SERVER_ADDR NULL |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 101 | #define DFL_SERVER_PORT "4433" |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 102 | #define DFL_DEBUG_LEVEL 0 |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 103 | #define DFL_NBIO 0 |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 104 | #define DFL_READ_TIMEOUT 0 |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 105 | #define DFL_CA_FILE "" |
| 106 | #define DFL_CA_PATH "" |
| 107 | #define DFL_CRT_FILE "" |
| 108 | #define DFL_KEY_FILE "" |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 109 | #define DFL_CRT_FILE2 "" |
| 110 | #define DFL_KEY_FILE2 "" |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 111 | #define DFL_PSK "" |
| 112 | #define DFL_PSK_IDENTITY "Client_identity" |
Manuel Pégourié-Gonnard | 70905a7 | 2015-09-16 11:08:34 +0200 | [diff] [blame] | 113 | #define DFL_ECJPAKE_PW NULL |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 114 | #define DFL_PSK_LIST NULL |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 115 | #define DFL_FORCE_CIPHER 0 |
Manuel Pégourié-Gonnard | 6dc0781 | 2014-06-11 13:50:34 +0200 | [diff] [blame] | 116 | #define DFL_VERSION_SUITES NULL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 117 | #define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED |
Manuel Pégourié-Gonnard | 85d915b | 2014-11-03 20:10:36 +0100 | [diff] [blame] | 118 | #define DFL_ALLOW_LEGACY -2 |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 119 | #define DFL_RENEGOTIATE 0 |
Manuel Pégourié-Gonnard | fae355e | 2014-07-04 14:32:27 +0200 | [diff] [blame] | 120 | #define DFL_RENEGO_DELAY -2 |
Andres AG | 692ad84 | 2017-01-19 16:30:57 +0000 | [diff] [blame] | 121 | #define DFL_RENEGO_PERIOD ( (uint64_t)-1 ) |
Manuel Pégourié-Gonnard | 67686c4 | 2014-08-15 11:17:27 +0200 | [diff] [blame] | 122 | #define DFL_EXCHANGES 1 |
Manuel Pégourié-Gonnard | 8c8be1e | 2015-03-31 14:21:11 +0200 | [diff] [blame] | 123 | #define DFL_MIN_VERSION -1 |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 124 | #define DFL_MAX_VERSION -1 |
Manuel Pégourié-Gonnard | d42b7c8 | 2015-03-20 19:44:04 +0000 | [diff] [blame] | 125 | #define DFL_ARC4 -1 |
Gilles Peskine | bc70a18 | 2017-05-09 15:59:24 +0200 | [diff] [blame] | 126 | #define DFL_SHA1 -1 |
Manuel Pégourié-Gonnard | fa44f20 | 2015-03-27 17:52:25 +0100 | [diff] [blame] | 127 | #define DFL_AUTH_MODE -1 |
Janos Follath | 4817e27 | 2017-04-10 13:44:33 +0100 | [diff] [blame] | 128 | #define DFL_CERT_REQ_CA_LIST MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 129 | #define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE |
Manuel Pégourié-Gonnard | e117a8f | 2015-01-09 12:39:35 +0100 | [diff] [blame] | 130 | #define DFL_TRUNC_HMAC -1 |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 131 | #define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 132 | #define DFL_TICKET_TIMEOUT 86400 |
Manuel Pégourié-Gonnard | 4c88345 | 2014-02-20 21:32:41 +0100 | [diff] [blame] | 133 | #define DFL_CACHE_MAX -1 |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 134 | #define DFL_CACHE_TIMEOUT -1 |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 135 | #define DFL_SNI NULL |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 136 | #define DFL_ALPN_STRING NULL |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 137 | #define DFL_CURVES NULL |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 138 | #define DFL_DHM_FILE NULL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 139 | #define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM |
Manuel Pégourié-Gonnard | 26820e3 | 2014-07-23 19:34:59 +0200 | [diff] [blame] | 140 | #define DFL_COOKIES 1 |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 141 | #define DFL_ANTI_REPLAY -1 |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 142 | #define DFL_HS_TO_MIN 0 |
| 143 | #define DFL_HS_TO_MAX 0 |
Manuel Pégourié-Gonnard | e698f59 | 2014-10-14 19:36:36 +0200 | [diff] [blame] | 144 | #define DFL_BADMAC_LIMIT -1 |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 145 | #define DFL_EXTENDED_MS -1 |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 146 | #define DFL_ETM -1 |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 147 | |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 148 | #define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 149 | "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 150 | "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 151 | "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 152 | "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 153 | "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 154 | "07-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah</p>\r\n" |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 155 | |
Paul Bakker | 8e714d7 | 2013-07-18 11:05:13 +0200 | [diff] [blame] | 156 | /* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer |
| 157 | * packets (for fragmentation purposes) */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 158 | #define HTTP_RESPONSE \ |
| 159 | "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \ |
Manuel Pégourié-Gonnard | 9169921 | 2015-01-22 16:26:39 +0000 | [diff] [blame] | 160 | "<h2>mbed TLS Test Server</h2>\r\n" \ |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 161 | "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 162 | |
Manuel Pégourié-Gonnard | 95c0a63 | 2014-06-11 18:32:36 +0200 | [diff] [blame] | 163 | /* |
| 164 | * Size of the basic I/O buffer. Able to hold our default response. |
| 165 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | * You will need to adapt the mbedtls_ssl_get_bytes_avail() test in ssl-opt.sh |
Manuel Pégourié-Gonnard | 95c0a63 | 2014-06-11 18:32:36 +0200 | [diff] [blame] | 167 | * if you change this value to something outside the range <= 100 or > 500 |
| 168 | */ |
Manuel Pégourié-Gonnard | e7a3b10 | 2014-06-11 18:21:20 +0200 | [diff] [blame] | 169 | #define IO_BUF_LEN 200 |
| 170 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 171 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 172 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 173 | #define USAGE_IO \ |
Paul Bakker | 1f9d02d | 2012-11-20 10:30:55 +0100 | [diff] [blame] | 174 | " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \ |
| 175 | " default: \"\" (pre-loaded)\n" \ |
| 176 | " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \ |
| 177 | " default: \"\" (pre-loaded) (overrides ca_file)\n" \ |
| 178 | " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \ |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 179 | " default: see note after key_file2\n" \ |
| 180 | " key_file=%%s default: see note after key_file2\n" \ |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 181 | " crt_file2=%%s Your second cert and chain (in bottom to top order, top may be omitted)\n" \ |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 182 | " default: see note after key_file2\n" \ |
| 183 | " key_file2=%%s default: see note below\n" \ |
| 184 | " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \ |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 185 | " preloaded certificate(s) and key(s) are used if available\n" \ |
| 186 | " dhm_file=%%s File containing Diffie-Hellman parameters\n" \ |
| 187 | " default: preloaded parameters\n" |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 188 | #else |
| 189 | #define USAGE_IO \ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 190 | "\n" \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 191 | " No file operations available (MBEDTLS_FS_IO not defined)\n" \ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 192 | "\n" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 193 | #endif /* MBEDTLS_FS_IO */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 194 | #else |
| 195 | #define USAGE_IO "" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 196 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 197 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 198 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 199 | #define USAGE_PSK \ |
| 200 | " psk=%%s default: \"\" (in hex, without 0x)\n" \ |
| 201 | " psk_identity=%%s default: \"Client_identity\"\n" |
| 202 | #else |
| 203 | #define USAGE_PSK "" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 205 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 207 | #define USAGE_TICKETS \ |
Manuel Pégourié-Gonnard | dbe1ee1 | 2014-02-21 09:18:13 +0100 | [diff] [blame] | 208 | " tickets=%%d default: 1 (enabled)\n" \ |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 209 | " ticket_timeout=%%d default: 86400 (one day)\n" |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 210 | #else |
| 211 | #define USAGE_TICKETS "" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 212 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 213 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 214 | #if defined(MBEDTLS_SSL_CACHE_C) |
Manuel Pégourié-Gonnard | 4c88345 | 2014-02-20 21:32:41 +0100 | [diff] [blame] | 215 | #define USAGE_CACHE \ |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 216 | " cache_max=%%d default: cache default (50)\n" \ |
| 217 | " cache_timeout=%%d default: cache default (1d)\n" |
Manuel Pégourié-Gonnard | 4c88345 | 2014-02-20 21:32:41 +0100 | [diff] [blame] | 218 | #else |
| 219 | #define USAGE_CACHE "" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 220 | #endif /* MBEDTLS_SSL_CACHE_C */ |
Manuel Pégourié-Gonnard | 4c88345 | 2014-02-20 21:32:41 +0100 | [diff] [blame] | 221 | |
Manuel Pégourié-Gonnard | 6c7af4c | 2015-04-03 16:41:52 +0200 | [diff] [blame] | 222 | #if defined(SNI_OPTION) |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 223 | #define USAGE_SNI \ |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 224 | " sni=%%s name1,cert1,key1,ca1,crl1,auth1[,...]\n" \ |
| 225 | " default: disabled\n" |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 226 | #else |
| 227 | #define USAGE_SNI "" |
Manuel Pégourié-Gonnard | 6c7af4c | 2015-04-03 16:41:52 +0200 | [diff] [blame] | 228 | #endif /* SNI_OPTION */ |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 229 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 230 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 231 | #define USAGE_MAX_FRAG_LEN \ |
| 232 | " max_frag_len=%%d default: 16384 (tls default)\n" \ |
| 233 | " options: 512, 1024, 2048, 4096\n" |
| 234 | #else |
| 235 | #define USAGE_MAX_FRAG_LEN "" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 237 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 238 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | e117a8f | 2015-01-09 12:39:35 +0100 | [diff] [blame] | 239 | #define USAGE_TRUNC_HMAC \ |
| 240 | " trunc_hmac=%%d default: library default\n" |
| 241 | #else |
| 242 | #define USAGE_TRUNC_HMAC "" |
| 243 | #endif |
| 244 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 245 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 246 | #define USAGE_ALPN \ |
| 247 | " alpn=%%s default: \"\" (disabled)\n" \ |
| 248 | " example: spdy/1,http/1.1\n" |
| 249 | #else |
| 250 | #define USAGE_ALPN "" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 251 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 252 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 253 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) |
Manuel Pégourié-Gonnard | 26820e3 | 2014-07-23 19:34:59 +0200 | [diff] [blame] | 254 | #define USAGE_COOKIES \ |
| 255 | " cookies=0/1/-1 default: 1 (enabled)\n" \ |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 256 | " 0: disabled, -1: library default (broken)\n" |
Manuel Pégourié-Gonnard | 26820e3 | 2014-07-23 19:34:59 +0200 | [diff] [blame] | 257 | #else |
| 258 | #define USAGE_COOKIES "" |
| 259 | #endif |
| 260 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 261 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 262 | #define USAGE_ANTI_REPLAY \ |
Manuel Pégourié-Gonnard | e698f59 | 2014-10-14 19:36:36 +0200 | [diff] [blame] | 263 | " anti_replay=0/1 default: (library default: enabled)\n" |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 264 | #else |
| 265 | #define USAGE_ANTI_REPLAY "" |
| 266 | #endif |
| 267 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 268 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Manuel Pégourié-Gonnard | e698f59 | 2014-10-14 19:36:36 +0200 | [diff] [blame] | 269 | #define USAGE_BADMAC_LIMIT \ |
| 270 | " badmac_limit=%%d default: (library default: disabled)\n" |
| 271 | #else |
| 272 | #define USAGE_BADMAC_LIMIT "" |
| 273 | #endif |
| 274 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 275 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 276 | #define USAGE_DTLS \ |
| 277 | " dtls=%%d default: 0 (TLS)\n" \ |
| 278 | " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \ |
| 279 | " range of DTLS handshake timeouts in millisecs\n" |
| 280 | #else |
| 281 | #define USAGE_DTLS "" |
| 282 | #endif |
| 283 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 284 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 285 | #define USAGE_EMS \ |
| 286 | " extended_ms=0/1 default: (library default: on)\n" |
| 287 | #else |
| 288 | #define USAGE_EMS "" |
| 289 | #endif |
| 290 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 291 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 292 | #define USAGE_ETM \ |
| 293 | " etm=0/1 default: (library default: on)\n" |
| 294 | #else |
| 295 | #define USAGE_ETM "" |
| 296 | #endif |
| 297 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 298 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 299 | #define USAGE_RENEGO \ |
| 300 | " renegotiation=%%d default: 0 (disabled)\n" \ |
| 301 | " renegotiate=%%d default: 0 (disabled)\n" \ |
Manuel Pégourié-Gonnard | 590f416 | 2014-11-05 14:23:03 +0100 | [diff] [blame] | 302 | " renego_delay=%%d default: -2 (library default)\n" \ |
Andres AG | 692ad84 | 2017-01-19 16:30:57 +0000 | [diff] [blame] | 303 | " renego_period=%%d default: (2^64 - 1 for TLS, 2^48 - 1 for DTLS)\n" |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 304 | #else |
| 305 | #define USAGE_RENEGO "" |
| 306 | #endif |
| 307 | |
Manuel Pégourié-Gonnard | 70905a7 | 2015-09-16 11:08:34 +0200 | [diff] [blame] | 308 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 309 | #define USAGE_ECJPAKE \ |
| 310 | " ecjpake_pw=%%s default: none (disabled)\n" |
| 311 | #else |
| 312 | #define USAGE_ECJPAKE "" |
| 313 | #endif |
| 314 | |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 315 | #if defined(MBEDTLS_ECP_C) |
| 316 | #define USAGE_CURVES \ |
| 317 | " curves=a,b,c,d default: \"default\" (library default)\n" \ |
| 318 | " example: \"secp521r1,brainpoolP512r1\"\n" \ |
| 319 | " - use \"none\" for empty list\n" \ |
| 320 | " - see mbedtls_ecp_curve_list()\n" \ |
| 321 | " for acceptable curve names\n" |
| 322 | #else |
| 323 | #define USAGE_CURVES "" |
| 324 | #endif |
| 325 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 326 | #define USAGE \ |
| 327 | "\n usage: ssl_server2 param=<>...\n" \ |
| 328 | "\n acceptable parameters:\n" \ |
Ron Eldor | 71f68c4 | 2017-09-26 11:29:11 +0300 | [diff] [blame] | 329 | " server_addr=%%s default: (all interfaces)\n" \ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 330 | " server_port=%%d default: 4433\n" \ |
| 331 | " debug_level=%%d default: 0 (disabled)\n" \ |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 332 | " nbio=%%d default: 0 (blocking I/O)\n" \ |
| 333 | " options: 1 (non-blocking), 2 (added delays)\n" \ |
Manuel Pégourié-Gonnard | 22311ae | 2015-09-09 11:22:58 +0200 | [diff] [blame] | 334 | " read_timeout=%%d default: 0 ms (no timeout)\n" \ |
Manuel Pégourié-Gonnard | 2fc243d | 2014-02-19 18:22:59 +0100 | [diff] [blame] | 335 | "\n" \ |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 336 | USAGE_DTLS \ |
| 337 | USAGE_COOKIES \ |
| 338 | USAGE_ANTI_REPLAY \ |
Manuel Pégourié-Gonnard | e698f59 | 2014-10-14 19:36:36 +0200 | [diff] [blame] | 339 | USAGE_BADMAC_LIMIT \ |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 340 | "\n" \ |
Manuel Pégourié-Gonnard | ee6139c | 2015-05-07 10:18:26 +0100 | [diff] [blame] | 341 | " auth_mode=%%s default: (library default: none)\n" \ |
Manuel Pégourié-Gonnard | 2fc243d | 2014-02-19 18:22:59 +0100 | [diff] [blame] | 342 | " options: none, optional, required\n" \ |
Janos Follath | 4817e27 | 2017-04-10 13:44:33 +0100 | [diff] [blame] | 343 | " cert_req_ca_list=%%d default: 1 (send ca list)\n" \ |
| 344 | " options: 1 (send ca list), 0 (don't send)\n" \ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 345 | USAGE_IO \ |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 346 | USAGE_SNI \ |
Manuel Pégourié-Gonnard | 2fc243d | 2014-02-19 18:22:59 +0100 | [diff] [blame] | 347 | "\n" \ |
| 348 | USAGE_PSK \ |
Manuel Pégourié-Gonnard | 70905a7 | 2015-09-16 11:08:34 +0200 | [diff] [blame] | 349 | USAGE_ECJPAKE \ |
Manuel Pégourié-Gonnard | 2fc243d | 2014-02-19 18:22:59 +0100 | [diff] [blame] | 350 | "\n" \ |
Manuel Pégourié-Gonnard | 85d915b | 2014-11-03 20:10:36 +0100 | [diff] [blame] | 351 | " allow_legacy=%%d default: (library default: no)\n" \ |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 352 | USAGE_RENEGO \ |
Manuel Pégourié-Gonnard | 67686c4 | 2014-08-15 11:17:27 +0200 | [diff] [blame] | 353 | " exchanges=%%d default: 1\n" \ |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 354 | "\n" \ |
Manuel Pégourié-Gonnard | 2fc243d | 2014-02-19 18:22:59 +0100 | [diff] [blame] | 355 | USAGE_TICKETS \ |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 356 | USAGE_CACHE \ |
Manuel Pégourié-Gonnard | 2fc243d | 2014-02-19 18:22:59 +0100 | [diff] [blame] | 357 | USAGE_MAX_FRAG_LEN \ |
Manuel Pégourié-Gonnard | e117a8f | 2015-01-09 12:39:35 +0100 | [diff] [blame] | 358 | USAGE_TRUNC_HMAC \ |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 359 | USAGE_ALPN \ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 360 | USAGE_EMS \ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 361 | USAGE_ETM \ |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 362 | USAGE_CURVES \ |
Manuel Pégourié-Gonnard | 2fc243d | 2014-02-19 18:22:59 +0100 | [diff] [blame] | 363 | "\n" \ |
Manuel Pégourié-Gonnard | fa44f20 | 2015-03-27 17:52:25 +0100 | [diff] [blame] | 364 | " arc4=%%d default: (library default: 0)\n" \ |
Gilles Peskine | bc70a18 | 2017-05-09 15:59:24 +0200 | [diff] [blame] | 365 | " allow_sha1=%%d default: 0\n" \ |
Manuel Pégourié-Gonnard | 8c8be1e | 2015-03-31 14:21:11 +0200 | [diff] [blame] | 366 | " min_version=%%s default: (library default: tls1)\n" \ |
| 367 | " max_version=%%s default: (library default: tls1_2)\n" \ |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 368 | " force_version=%%s default: \"\" (none)\n" \ |
Manuel Pégourié-Gonnard | 83218f1 | 2014-02-12 11:11:12 +0100 | [diff] [blame] | 369 | " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \ |
Manuel Pégourié-Gonnard | 6dc0781 | 2014-06-11 13:50:34 +0200 | [diff] [blame] | 370 | "\n" \ |
| 371 | " version_suites=a,b,c,d per-version ciphersuites\n" \ |
| 372 | " in order from ssl3 to tls1_2\n" \ |
| 373 | " default: all enabled\n" \ |
| 374 | " force_ciphersuite=<name> default: all enabled\n" \ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 375 | " acceptable ciphersuite names:\n" |
| 376 | |
Andres AG | 692ad84 | 2017-01-19 16:30:57 +0000 | [diff] [blame] | 377 | |
Hanno Becker | 8651a43 | 2017-06-09 16:13:22 +0100 | [diff] [blame] | 378 | #define ALPN_LIST_SIZE 10 |
| 379 | #define CURVE_LIST_SIZE 20 |
| 380 | |
Andres AG | 692ad84 | 2017-01-19 16:30:57 +0000 | [diff] [blame] | 381 | #define PUT_UINT64_BE(out_be,in_le,i) \ |
| 382 | { \ |
| 383 | (out_be)[(i) + 0] = (unsigned char)( ( (in_le) >> 56 ) & 0xFF ); \ |
| 384 | (out_be)[(i) + 1] = (unsigned char)( ( (in_le) >> 48 ) & 0xFF ); \ |
| 385 | (out_be)[(i) + 2] = (unsigned char)( ( (in_le) >> 40 ) & 0xFF ); \ |
| 386 | (out_be)[(i) + 3] = (unsigned char)( ( (in_le) >> 32 ) & 0xFF ); \ |
| 387 | (out_be)[(i) + 4] = (unsigned char)( ( (in_le) >> 24 ) & 0xFF ); \ |
| 388 | (out_be)[(i) + 5] = (unsigned char)( ( (in_le) >> 16 ) & 0xFF ); \ |
| 389 | (out_be)[(i) + 6] = (unsigned char)( ( (in_le) >> 8 ) & 0xFF ); \ |
| 390 | (out_be)[(i) + 7] = (unsigned char)( ( (in_le) >> 0 ) & 0xFF ); \ |
| 391 | } |
| 392 | |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 393 | /* |
| 394 | * global options |
| 395 | */ |
| 396 | struct options |
| 397 | { |
| 398 | const char *server_addr; /* address on which the ssl service runs */ |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 399 | const char *server_port; /* port on which the ssl service runs */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 400 | int debug_level; /* level of debugging */ |
| 401 | int nbio; /* should I/O be blocking? */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 402 | uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 403 | const char *ca_file; /* the file with the CA certificate(s) */ |
| 404 | const char *ca_path; /* the path with the CA certificate(s) reside */ |
| 405 | const char *crt_file; /* the file with the server certificate */ |
| 406 | const char *key_file; /* the file with the server key */ |
| 407 | const char *crt_file2; /* the file with the 2nd server certificate */ |
| 408 | const char *key_file2; /* the file with the 2nd server key */ |
| 409 | const char *psk; /* the pre-shared key */ |
| 410 | const char *psk_identity; /* the pre-shared key identity */ |
| 411 | char *psk_list; /* list of PSK id/key pairs for callback */ |
Manuel Pégourié-Gonnard | 70905a7 | 2015-09-16 11:08:34 +0200 | [diff] [blame] | 412 | const char *ecjpake_pw; /* the EC J-PAKE password */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 413 | int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */ |
| 414 | const char *version_suites; /* per-version ciphersuites */ |
| 415 | int renegotiation; /* enable / disable renegotiation */ |
| 416 | int allow_legacy; /* allow legacy renegotiation */ |
| 417 | int renegotiate; /* attempt renegotiation? */ |
| 418 | int renego_delay; /* delay before enforcing renegotiation */ |
Andres AG | 692ad84 | 2017-01-19 16:30:57 +0000 | [diff] [blame] | 419 | uint64_t renego_period; /* period for automatic renegotiation */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 420 | int exchanges; /* number of data exchanges */ |
| 421 | int min_version; /* minimum protocol version accepted */ |
| 422 | int max_version; /* maximum protocol version accepted */ |
| 423 | int arc4; /* flag for arc4 suites support */ |
Gilles Peskine | bc70a18 | 2017-05-09 15:59:24 +0200 | [diff] [blame] | 424 | int allow_sha1; /* flag for SHA-1 support */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 425 | int auth_mode; /* verify mode for connection */ |
Janos Follath | 4817e27 | 2017-04-10 13:44:33 +0100 | [diff] [blame] | 426 | int cert_req_ca_list; /* should we send the CA list? */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 427 | unsigned char mfl_code; /* code for maximum fragment length */ |
| 428 | int trunc_hmac; /* accept truncated hmac? */ |
| 429 | int tickets; /* enable / disable session tickets */ |
| 430 | int ticket_timeout; /* session ticket lifetime */ |
| 431 | int cache_max; /* max number of session cache entries */ |
| 432 | int cache_timeout; /* expiration delay of session cache entries */ |
| 433 | char *sni; /* string describing sni information */ |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 434 | const char *curves; /* list of supported elliptic curves */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 435 | const char *alpn_string; /* ALPN supported protocols */ |
| 436 | const char *dhm_file; /* the file with the DH parameters */ |
| 437 | int extended_ms; /* allow negotiation of extended MS? */ |
| 438 | int etm; /* allow negotiation of encrypt-then-MAC? */ |
Manuel Pégourié-Gonnard | d901d17 | 2015-02-16 18:37:53 +0000 | [diff] [blame] | 439 | int transport; /* TLS or DTLS? */ |
| 440 | int cookies; /* Use cookies for DTLS? -1 to break them */ |
| 441 | int anti_replay; /* Use anti-replay for DTLS? -1 for default */ |
| 442 | uint32_t hs_to_min; /* Initial value of DTLS handshake timer */ |
| 443 | uint32_t hs_to_max; /* Max value of DTLS handshake timer */ |
| 444 | int badmac_limit; /* Limit of records with bad MAC */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 445 | } opt; |
| 446 | |
Manuel Pégourié-Gonnard | 61ee351 | 2015-06-23 17:35:03 +0200 | [diff] [blame] | 447 | static void my_debug( void *ctx, int level, |
| 448 | const char *file, int line, |
| 449 | const char *str ) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 450 | { |
Manuel Pégourié-Gonnard | 052f288 | 2015-07-01 11:50:23 +0200 | [diff] [blame] | 451 | const char *p, *basename; |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 452 | |
Manuel Pégourié-Gonnard | 052f288 | 2015-07-01 11:50:23 +0200 | [diff] [blame] | 453 | /* Extract basename from file */ |
| 454 | for( p = basename = file; *p != '\0'; p++ ) |
| 455 | if( *p == '/' || *p == '\\' ) |
| 456 | basename = p + 1; |
| 457 | |
| 458 | mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str ); |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 459 | fflush( (FILE *) ctx ); |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * Test recv/send functions that make sure each try returns |
| 464 | * WANT_READ/WANT_WRITE at least once before sucesseding |
| 465 | */ |
| 466 | static int my_recv( void *ctx, unsigned char *buf, size_t len ) |
| 467 | { |
| 468 | static int first_try = 1; |
| 469 | int ret; |
| 470 | |
| 471 | if( first_try ) |
| 472 | { |
| 473 | first_try = 0; |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 474 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 477 | ret = mbedtls_net_recv( ctx, buf, len ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 478 | if( ret != MBEDTLS_ERR_SSL_WANT_READ ) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 479 | first_try = 1; /* Next call will be a new operation */ |
| 480 | return( ret ); |
| 481 | } |
| 482 | |
| 483 | static int my_send( void *ctx, const unsigned char *buf, size_t len ) |
| 484 | { |
| 485 | static int first_try = 1; |
| 486 | int ret; |
| 487 | |
| 488 | if( first_try ) |
| 489 | { |
| 490 | first_try = 0; |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 491 | return( MBEDTLS_ERR_SSL_WANT_WRITE ); |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 494 | ret = mbedtls_net_send( ctx, buf, len ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 495 | if( ret != MBEDTLS_ERR_SSL_WANT_WRITE ) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 496 | first_try = 1; /* Next call will be a new operation */ |
| 497 | return( ret ); |
| 498 | } |
| 499 | |
Manuel Pégourié-Gonnard | fdee74b | 2014-06-10 15:15:06 +0200 | [diff] [blame] | 500 | /* |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 501 | * Return authmode from string, or -1 on error |
| 502 | */ |
| 503 | static int get_auth_mode( const char *s ) |
| 504 | { |
| 505 | if( strcmp( s, "none" ) == 0 ) |
| 506 | return( MBEDTLS_SSL_VERIFY_NONE ); |
| 507 | if( strcmp( s, "optional" ) == 0 ) |
| 508 | return( MBEDTLS_SSL_VERIFY_OPTIONAL ); |
| 509 | if( strcmp( s, "required" ) == 0 ) |
| 510 | return( MBEDTLS_SSL_VERIFY_REQUIRED ); |
| 511 | |
| 512 | return( -1 ); |
| 513 | } |
| 514 | |
| 515 | /* |
Manuel Pégourié-Gonnard | fdee74b | 2014-06-10 15:15:06 +0200 | [diff] [blame] | 516 | * Used by sni_parse and psk_parse to handle coma-separated lists |
| 517 | */ |
| 518 | #define GET_ITEM( dst ) \ |
| 519 | dst = p; \ |
| 520 | while( *p != ',' ) \ |
| 521 | if( ++p > end ) \ |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 522 | goto error; \ |
Manuel Pégourié-Gonnard | fdee74b | 2014-06-10 15:15:06 +0200 | [diff] [blame] | 523 | *p++ = '\0'; |
| 524 | |
Manuel Pégourié-Gonnard | 6c7af4c | 2015-04-03 16:41:52 +0200 | [diff] [blame] | 525 | #if defined(SNI_OPTION) |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 526 | typedef struct _sni_entry sni_entry; |
| 527 | |
| 528 | struct _sni_entry { |
| 529 | const char *name; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 530 | mbedtls_x509_crt *cert; |
| 531 | mbedtls_pk_context *key; |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 532 | mbedtls_x509_crt* ca; |
| 533 | mbedtls_x509_crl* crl; |
| 534 | int authmode; |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 535 | sni_entry *next; |
| 536 | }; |
| 537 | |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 538 | void sni_free( sni_entry *head ) |
| 539 | { |
| 540 | sni_entry *cur = head, *next; |
| 541 | |
| 542 | while( cur != NULL ) |
| 543 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 544 | mbedtls_x509_crt_free( cur->cert ); |
| 545 | mbedtls_free( cur->cert ); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 546 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 547 | mbedtls_pk_free( cur->key ); |
| 548 | mbedtls_free( cur->key ); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 549 | |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 550 | mbedtls_x509_crt_free( cur->ca ); |
| 551 | mbedtls_free( cur->ca ); |
| 552 | |
| 553 | mbedtls_x509_crl_free( cur->crl ); |
| 554 | mbedtls_free( cur->crl ); |
| 555 | |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 556 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 557 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 558 | cur = next; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | /* |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 563 | * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...] |
| 564 | * into a usable sni_entry list. For ca1, crl1, auth1, the special value |
| 565 | * '-' means unset. If ca1 is unset, then crl1 is ignored too. |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 566 | * |
| 567 | * Modifies the input string! This is not production quality! |
| 568 | */ |
| 569 | sni_entry *sni_parse( char *sni_string ) |
| 570 | { |
| 571 | sni_entry *cur = NULL, *new = NULL; |
| 572 | char *p = sni_string; |
| 573 | char *end = p; |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 574 | char *crt_file, *key_file, *ca_file, *crl_file, *auth_str; |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 575 | |
| 576 | while( *end != '\0' ) |
| 577 | ++end; |
| 578 | *end = ','; |
| 579 | |
| 580 | while( p <= end ) |
| 581 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 582 | if( ( new = mbedtls_calloc( 1, sizeof( sni_entry ) ) ) == NULL ) |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 583 | { |
| 584 | sni_free( cur ); |
| 585 | return( NULL ); |
| 586 | } |
| 587 | |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 588 | GET_ITEM( new->name ); |
| 589 | GET_ITEM( crt_file ); |
| 590 | GET_ITEM( key_file ); |
| 591 | GET_ITEM( ca_file ); |
| 592 | GET_ITEM( crl_file ); |
| 593 | GET_ITEM( auth_str ); |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 594 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 595 | if( ( new->cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL || |
| 596 | ( new->key = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) ) ) == NULL ) |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 597 | goto error; |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 598 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 599 | mbedtls_x509_crt_init( new->cert ); |
| 600 | mbedtls_pk_init( new->key ); |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 601 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 602 | if( mbedtls_x509_crt_parse_file( new->cert, crt_file ) != 0 || |
| 603 | mbedtls_pk_parse_keyfile( new->key, key_file, "" ) != 0 ) |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 604 | goto error; |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 605 | |
| 606 | if( strcmp( ca_file, "-" ) != 0 ) |
| 607 | { |
| 608 | if( ( new->ca = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL ) |
| 609 | goto error; |
| 610 | |
| 611 | mbedtls_x509_crt_init( new->ca ); |
| 612 | |
| 613 | if( mbedtls_x509_crt_parse_file( new->ca, ca_file ) != 0 ) |
| 614 | goto error; |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 617 | if( strcmp( crl_file, "-" ) != 0 ) |
| 618 | { |
| 619 | if( ( new->crl = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) ) ) == NULL ) |
| 620 | goto error; |
| 621 | |
| 622 | mbedtls_x509_crl_init( new->crl ); |
| 623 | |
| 624 | if( mbedtls_x509_crl_parse_file( new->crl, crl_file ) != 0 ) |
| 625 | goto error; |
| 626 | } |
| 627 | |
| 628 | if( strcmp( auth_str, "-" ) != 0 ) |
| 629 | { |
| 630 | if( ( new->authmode = get_auth_mode( auth_str ) ) < 0 ) |
| 631 | goto error; |
| 632 | } |
| 633 | else |
| 634 | new->authmode = DFL_AUTH_MODE; |
| 635 | |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 636 | new->next = cur; |
| 637 | cur = new; |
| 638 | } |
| 639 | |
| 640 | return( cur ); |
| 641 | |
| 642 | error: |
| 643 | sni_free( new ); |
| 644 | sni_free( cur ); |
| 645 | return( NULL ); |
| 646 | } |
| 647 | |
| 648 | /* |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 649 | * SNI callback. |
| 650 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 651 | int sni_callback( void *p_info, mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 652 | const unsigned char *name, size_t name_len ) |
| 653 | { |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 654 | const sni_entry *cur = (const sni_entry *) p_info; |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 655 | |
| 656 | while( cur != NULL ) |
| 657 | { |
| 658 | if( name_len == strlen( cur->name ) && |
| 659 | memcmp( name, cur->name, name_len ) == 0 ) |
| 660 | { |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 661 | if( cur->ca != NULL ) |
| 662 | mbedtls_ssl_set_hs_ca_chain( ssl, cur->ca, cur->crl ); |
| 663 | |
| 664 | if( cur->authmode != DFL_AUTH_MODE ) |
| 665 | mbedtls_ssl_set_hs_authmode( ssl, cur->authmode ); |
| 666 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 667 | return( mbedtls_ssl_set_hs_own_cert( ssl, cur->cert, cur->key ) ); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | cur = cur->next; |
| 671 | } |
| 672 | |
| 673 | return( -1 ); |
| 674 | } |
| 675 | |
Manuel Pégourié-Gonnard | 6c7af4c | 2015-04-03 16:41:52 +0200 | [diff] [blame] | 676 | #endif /* SNI_OPTION */ |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 677 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 678 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Manuel Pégourié-Gonnard | 9e27163 | 2014-06-09 19:06:00 +0200 | [diff] [blame] | 679 | |
| 680 | #define HEX2NUM( c ) \ |
| 681 | if( c >= '0' && c <= '9' ) \ |
| 682 | c -= '0'; \ |
| 683 | else if( c >= 'a' && c <= 'f' ) \ |
| 684 | c -= 'a' - 10; \ |
| 685 | else if( c >= 'A' && c <= 'F' ) \ |
| 686 | c -= 'A' - 10; \ |
| 687 | else \ |
| 688 | return( -1 ); |
| 689 | |
| 690 | /* |
| 691 | * Convert a hex string to bytes. |
| 692 | * Return 0 on success, -1 on error. |
| 693 | */ |
| 694 | int unhexify( unsigned char *output, const char *input, size_t *olen ) |
| 695 | { |
| 696 | unsigned char c; |
| 697 | size_t j; |
| 698 | |
| 699 | *olen = strlen( input ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 700 | if( *olen % 2 != 0 || *olen / 2 > MBEDTLS_PSK_MAX_LEN ) |
Manuel Pégourié-Gonnard | 9e27163 | 2014-06-09 19:06:00 +0200 | [diff] [blame] | 701 | return( -1 ); |
| 702 | *olen /= 2; |
| 703 | |
| 704 | for( j = 0; j < *olen * 2; j += 2 ) |
| 705 | { |
| 706 | c = input[j]; |
| 707 | HEX2NUM( c ); |
| 708 | output[ j / 2 ] = c << 4; |
| 709 | |
| 710 | c = input[j + 1]; |
| 711 | HEX2NUM( c ); |
| 712 | output[ j / 2 ] |= c; |
| 713 | } |
| 714 | |
| 715 | return( 0 ); |
| 716 | } |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 717 | |
| 718 | typedef struct _psk_entry psk_entry; |
| 719 | |
| 720 | struct _psk_entry |
| 721 | { |
| 722 | const char *name; |
| 723 | size_t key_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 724 | unsigned char key[MBEDTLS_PSK_MAX_LEN]; |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 725 | psk_entry *next; |
| 726 | }; |
| 727 | |
| 728 | /* |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 729 | * Free a list of psk_entry's |
| 730 | */ |
| 731 | void psk_free( psk_entry *head ) |
| 732 | { |
| 733 | psk_entry *next; |
| 734 | |
| 735 | while( head != NULL ) |
| 736 | { |
| 737 | next = head->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 738 | mbedtls_free( head ); |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 739 | head = next; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | /* |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 744 | * Parse a string of pairs name1,key1[,name2,key2[,...]] |
| 745 | * into a usable psk_entry list. |
| 746 | * |
| 747 | * Modifies the input string! This is not production quality! |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 748 | */ |
| 749 | psk_entry *psk_parse( char *psk_string ) |
| 750 | { |
| 751 | psk_entry *cur = NULL, *new = NULL; |
| 752 | char *p = psk_string; |
| 753 | char *end = p; |
| 754 | char *key_hex; |
| 755 | |
| 756 | while( *end != '\0' ) |
| 757 | ++end; |
| 758 | *end = ','; |
| 759 | |
| 760 | while( p <= end ) |
| 761 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 762 | if( ( new = mbedtls_calloc( 1, sizeof( psk_entry ) ) ) == NULL ) |
Manuel Pégourié-Gonnard | b199095 | 2015-02-18 09:32:06 +0000 | [diff] [blame] | 763 | goto error; |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 764 | |
| 765 | memset( new, 0, sizeof( psk_entry ) ); |
| 766 | |
Manuel Pégourié-Gonnard | fdee74b | 2014-06-10 15:15:06 +0200 | [diff] [blame] | 767 | GET_ITEM( new->name ); |
| 768 | GET_ITEM( key_hex ); |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 769 | |
| 770 | if( unhexify( new->key, key_hex, &new->key_len ) != 0 ) |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 771 | goto error; |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 772 | |
| 773 | new->next = cur; |
| 774 | cur = new; |
| 775 | } |
| 776 | |
| 777 | return( cur ); |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 778 | |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 779 | error: |
| 780 | psk_free( new ); |
| 781 | psk_free( cur ); |
| 782 | return( 0 ); |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | /* |
| 786 | * PSK callback |
| 787 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 788 | int psk_callback( void *p_info, mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 789 | const unsigned char *name, size_t name_len ) |
| 790 | { |
| 791 | psk_entry *cur = (psk_entry *) p_info; |
| 792 | |
| 793 | while( cur != NULL ) |
| 794 | { |
| 795 | if( name_len == strlen( cur->name ) && |
| 796 | memcmp( name, cur->name, name_len ) == 0 ) |
| 797 | { |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 798 | return( mbedtls_ssl_set_hs_psk( ssl, cur->key, cur->key_len ) ); |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | cur = cur->next; |
| 802 | } |
| 803 | |
| 804 | return( -1 ); |
| 805 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 806 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 9e27163 | 2014-06-09 19:06:00 +0200 | [diff] [blame] | 807 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 808 | static mbedtls_net_context listen_fd, client_fd; |
Paul Bakker | bc3e54c | 2014-08-18 14:36:17 +0200 | [diff] [blame] | 809 | |
| 810 | /* Interruption handler to ensure clean exit (for valgrind testing) */ |
| 811 | #if !defined(_WIN32) |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 812 | static int received_sigterm = 0; |
| 813 | void term_handler( int sig ) |
| 814 | { |
| 815 | ((void) sig); |
| 816 | received_sigterm = 1; |
Manuel Pégourié-Gonnard | 3d7d00a | 2015-06-30 15:55:03 +0200 | [diff] [blame] | 817 | mbedtls_net_free( &listen_fd ); /* causes mbedtls_net_accept() to abort */ |
| 818 | mbedtls_net_free( &client_fd ); /* causes net_read() to abort */ |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 819 | } |
Paul Bakker | c1283d3 | 2014-08-18 11:05:51 +0200 | [diff] [blame] | 820 | #endif |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 821 | |
Gilles Peskine | 682df09 | 2017-05-11 19:01:11 +0200 | [diff] [blame] | 822 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 823 | static int ssl_sig_hashes_for_test[] = { |
| 824 | #if defined(MBEDTLS_SHA512_C) |
| 825 | MBEDTLS_MD_SHA512, |
| 826 | MBEDTLS_MD_SHA384, |
| 827 | #endif |
| 828 | #if defined(MBEDTLS_SHA256_C) |
| 829 | MBEDTLS_MD_SHA256, |
| 830 | MBEDTLS_MD_SHA224, |
| 831 | #endif |
| 832 | #if defined(MBEDTLS_SHA1_C) |
| 833 | /* Allow SHA-1 as we use it extensively in tests. */ |
| 834 | MBEDTLS_MD_SHA1, |
| 835 | #endif |
| 836 | MBEDTLS_MD_NONE |
| 837 | }; |
| 838 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 839 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 840 | int main( int argc, char *argv[] ) |
| 841 | { |
Manuel Pégourié-Gonnard | 6a0017b | 2015-01-22 10:33:29 +0000 | [diff] [blame] | 842 | int ret = 0, len, written, frags, exchanges_left; |
Manuel Pégourié-Gonnard | 6dc0781 | 2014-06-11 13:50:34 +0200 | [diff] [blame] | 843 | int version_suites[4][2]; |
Manuel Pégourié-Gonnard | e7a3b10 | 2014-06-11 18:21:20 +0200 | [diff] [blame] | 844 | unsigned char buf[IO_BUF_LEN]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 845 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 846 | unsigned char psk[MBEDTLS_PSK_MAX_LEN]; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 847 | size_t psk_len = 0; |
Paul Bakker | 9b7fb6f | 2014-06-12 23:01:43 +0200 | [diff] [blame] | 848 | psk_entry *psk_info = NULL; |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 849 | #endif |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 850 | const char *pers = "ssl_server2"; |
Manuel Pégourié-Gonnard | 336b824 | 2014-07-22 17:57:43 +0200 | [diff] [blame] | 851 | unsigned char client_ip[16] = { 0 }; |
Manuel Pégourié-Gonnard | 0b104b0 | 2015-05-14 21:52:40 +0200 | [diff] [blame] | 852 | size_t cliip_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 853 | #if defined(MBEDTLS_SSL_COOKIE_C) |
| 854 | mbedtls_ssl_cookie_ctx cookie_ctx; |
Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 855 | #endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 856 | |
Gilles Peskine | ef86ab2 | 2017-05-05 18:59:02 +0200 | [diff] [blame] | 857 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 858 | mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default; |
| 859 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 860 | mbedtls_entropy_context entropy; |
| 861 | mbedtls_ctr_drbg_context ctr_drbg; |
| 862 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 863 | mbedtls_ssl_config conf; |
Manuel Pégourié-Gonnard | d2377e7 | 2015-05-13 13:58:56 +0200 | [diff] [blame] | 864 | #if defined(MBEDTLS_TIMING_C) |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 865 | mbedtls_timing_delay_context timer; |
Manuel Pégourié-Gonnard | d2377e7 | 2015-05-13 13:58:56 +0200 | [diff] [blame] | 866 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 867 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 590f416 | 2014-11-05 14:23:03 +0100 | [diff] [blame] | 868 | unsigned char renego_period[8] = { 0 }; |
| 869 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 870 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | db1cc76 | 2015-05-12 11:27:25 +0200 | [diff] [blame] | 871 | uint32_t flags; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 872 | mbedtls_x509_crt cacert; |
| 873 | mbedtls_x509_crt srvcert; |
| 874 | mbedtls_pk_context pkey; |
| 875 | mbedtls_x509_crt srvcert2; |
| 876 | mbedtls_pk_context pkey2; |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 877 | int key_cert_init = 0, key_cert_init2 = 0; |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 878 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 879 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO) |
| 880 | mbedtls_dhm_context dhm; |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 881 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 882 | #if defined(MBEDTLS_SSL_CACHE_C) |
| 883 | mbedtls_ssl_cache_context cache; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 884 | #endif |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 885 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 886 | mbedtls_ssl_ticket_context ticket_ctx; |
| 887 | #endif |
Manuel Pégourié-Gonnard | 6c7af4c | 2015-04-03 16:41:52 +0200 | [diff] [blame] | 888 | #if defined(SNI_OPTION) |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 889 | sni_entry *sni_info = NULL; |
| 890 | #endif |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 891 | #if defined(MBEDTLS_ECP_C) |
Hanno Becker | 8651a43 | 2017-06-09 16:13:22 +0100 | [diff] [blame] | 892 | mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE]; |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 893 | const mbedtls_ecp_curve_info * curve_cur; |
| 894 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 895 | #if defined(MBEDTLS_SSL_ALPN) |
Hanno Becker | 8651a43 | 2017-06-09 16:13:22 +0100 | [diff] [blame] | 896 | const char *alpn_list[ALPN_LIST_SIZE]; |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 897 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 898 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
Paul Bakker | 82024bf | 2013-07-04 11:52:32 +0200 | [diff] [blame] | 899 | unsigned char alloc_buf[100000]; |
| 900 | #endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 901 | |
| 902 | int i; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 903 | char *p, *q; |
| 904 | const int *list; |
| 905 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 906 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 907 | mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) ); |
Paul Bakker | 82024bf | 2013-07-04 11:52:32 +0200 | [diff] [blame] | 908 | #endif |
| 909 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 910 | /* |
Manuel Pégourié-Gonnard | 3bd2aae | 2013-09-20 13:10:13 +0200 | [diff] [blame] | 911 | * Make sure memory references are valid in case we exit early. |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 912 | */ |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 913 | mbedtls_net_init( &client_fd ); |
| 914 | mbedtls_net_init( &listen_fd ); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 915 | mbedtls_ssl_init( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 916 | mbedtls_ssl_config_init( &conf ); |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 917 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 918 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 919 | mbedtls_x509_crt_init( &cacert ); |
| 920 | mbedtls_x509_crt_init( &srvcert ); |
| 921 | mbedtls_pk_init( &pkey ); |
| 922 | mbedtls_x509_crt_init( &srvcert2 ); |
| 923 | mbedtls_pk_init( &pkey2 ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 924 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 925 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO) |
| 926 | mbedtls_dhm_init( &dhm ); |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 927 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 928 | #if defined(MBEDTLS_SSL_CACHE_C) |
| 929 | mbedtls_ssl_cache_init( &cache ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 930 | #endif |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 931 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 932 | mbedtls_ssl_ticket_init( &ticket_ctx ); |
| 933 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 934 | #if defined(MBEDTLS_SSL_ALPN) |
Paul Bakker | 525f875 | 2014-05-01 10:58:57 +0200 | [diff] [blame] | 935 | memset( (void *) alpn_list, 0, sizeof( alpn_list ) ); |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 936 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 937 | #if defined(MBEDTLS_SSL_COOKIE_C) |
| 938 | mbedtls_ssl_cookie_init( &cookie_ctx ); |
Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 939 | #endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 940 | |
Paul Bakker | c1283d3 | 2014-08-18 11:05:51 +0200 | [diff] [blame] | 941 | #if !defined(_WIN32) |
Manuel Pégourié-Gonnard | 403a86f | 2014-11-17 12:46:49 +0100 | [diff] [blame] | 942 | /* Abort cleanly on SIGTERM and SIGINT */ |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 943 | signal( SIGTERM, term_handler ); |
Manuel Pégourié-Gonnard | 403a86f | 2014-11-17 12:46:49 +0100 | [diff] [blame] | 944 | signal( SIGINT, term_handler ); |
Paul Bakker | c1283d3 | 2014-08-18 11:05:51 +0200 | [diff] [blame] | 945 | #endif |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 946 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 947 | if( argc == 0 ) |
| 948 | { |
| 949 | usage: |
| 950 | if( ret == 0 ) |
| 951 | ret = 1; |
| 952 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 953 | mbedtls_printf( USAGE ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 954 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 955 | list = mbedtls_ssl_list_ciphersuites(); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 956 | while( *list ) |
| 957 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 958 | mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 959 | list++; |
| 960 | if( !*list ) |
| 961 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 962 | mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 963 | list++; |
| 964 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 965 | mbedtls_printf("\n"); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 966 | goto exit; |
| 967 | } |
| 968 | |
Manuel Pégourié-Gonnard | 18d31f8 | 2013-12-13 16:21:41 +0100 | [diff] [blame] | 969 | opt.server_addr = DFL_SERVER_ADDR; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 970 | opt.server_port = DFL_SERVER_PORT; |
| 971 | opt.debug_level = DFL_DEBUG_LEVEL; |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 972 | opt.nbio = DFL_NBIO; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 973 | opt.read_timeout = DFL_READ_TIMEOUT; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 974 | opt.ca_file = DFL_CA_FILE; |
| 975 | opt.ca_path = DFL_CA_PATH; |
| 976 | opt.crt_file = DFL_CRT_FILE; |
| 977 | opt.key_file = DFL_KEY_FILE; |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 978 | opt.crt_file2 = DFL_CRT_FILE2; |
| 979 | opt.key_file2 = DFL_KEY_FILE2; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 980 | opt.psk = DFL_PSK; |
| 981 | opt.psk_identity = DFL_PSK_IDENTITY; |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 982 | opt.psk_list = DFL_PSK_LIST; |
Manuel Pégourié-Gonnard | 70905a7 | 2015-09-16 11:08:34 +0200 | [diff] [blame] | 983 | opt.ecjpake_pw = DFL_ECJPAKE_PW; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 984 | opt.force_ciphersuite[0]= DFL_FORCE_CIPHER; |
Manuel Pégourié-Gonnard | 6dc0781 | 2014-06-11 13:50:34 +0200 | [diff] [blame] | 985 | opt.version_suites = DFL_VERSION_SUITES; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 986 | opt.renegotiation = DFL_RENEGOTIATION; |
| 987 | opt.allow_legacy = DFL_ALLOW_LEGACY; |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 988 | opt.renegotiate = DFL_RENEGOTIATE; |
Manuel Pégourié-Gonnard | fae355e | 2014-07-04 14:32:27 +0200 | [diff] [blame] | 989 | opt.renego_delay = DFL_RENEGO_DELAY; |
Manuel Pégourié-Gonnard | 590f416 | 2014-11-05 14:23:03 +0100 | [diff] [blame] | 990 | opt.renego_period = DFL_RENEGO_PERIOD; |
Manuel Pégourié-Gonnard | 67686c4 | 2014-08-15 11:17:27 +0200 | [diff] [blame] | 991 | opt.exchanges = DFL_EXCHANGES; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 992 | opt.min_version = DFL_MIN_VERSION; |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 993 | opt.max_version = DFL_MAX_VERSION; |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 994 | opt.arc4 = DFL_ARC4; |
Gilles Peskine | bc70a18 | 2017-05-09 15:59:24 +0200 | [diff] [blame] | 995 | opt.allow_sha1 = DFL_SHA1; |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 996 | opt.auth_mode = DFL_AUTH_MODE; |
Janos Follath | 4817e27 | 2017-04-10 13:44:33 +0100 | [diff] [blame] | 997 | opt.cert_req_ca_list = DFL_CERT_REQ_CA_LIST; |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 998 | opt.mfl_code = DFL_MFL_CODE; |
Manuel Pégourié-Gonnard | e117a8f | 2015-01-09 12:39:35 +0100 | [diff] [blame] | 999 | opt.trunc_hmac = DFL_TRUNC_HMAC; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 1000 | opt.tickets = DFL_TICKETS; |
Manuel Pégourié-Gonnard | dbe1ee1 | 2014-02-21 09:18:13 +0100 | [diff] [blame] | 1001 | opt.ticket_timeout = DFL_TICKET_TIMEOUT; |
Manuel Pégourié-Gonnard | 4c88345 | 2014-02-20 21:32:41 +0100 | [diff] [blame] | 1002 | opt.cache_max = DFL_CACHE_MAX; |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 1003 | opt.cache_timeout = DFL_CACHE_TIMEOUT; |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 1004 | opt.sni = DFL_SNI; |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1005 | opt.alpn_string = DFL_ALPN_STRING; |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1006 | opt.curves = DFL_CURVES; |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 1007 | opt.dhm_file = DFL_DHM_FILE; |
Manuel Pégourié-Gonnard | e29fd4b | 2014-02-06 14:02:55 +0100 | [diff] [blame] | 1008 | opt.transport = DFL_TRANSPORT; |
Manuel Pégourié-Gonnard | 26820e3 | 2014-07-23 19:34:59 +0200 | [diff] [blame] | 1009 | opt.cookies = DFL_COOKIES; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1010 | opt.anti_replay = DFL_ANTI_REPLAY; |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 1011 | opt.hs_to_min = DFL_HS_TO_MIN; |
| 1012 | opt.hs_to_max = DFL_HS_TO_MAX; |
Manuel Pégourié-Gonnard | e698f59 | 2014-10-14 19:36:36 +0200 | [diff] [blame] | 1013 | opt.badmac_limit = DFL_BADMAC_LIMIT; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1014 | opt.extended_ms = DFL_EXTENDED_MS; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1015 | opt.etm = DFL_ETM; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1016 | |
| 1017 | for( i = 1; i < argc; i++ ) |
| 1018 | { |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1019 | p = argv[i]; |
| 1020 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 1021 | goto usage; |
| 1022 | *q++ = '\0'; |
| 1023 | |
| 1024 | if( strcmp( p, "server_port" ) == 0 ) |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 1025 | opt.server_port = q; |
Manuel Pégourié-Gonnard | 18d31f8 | 2013-12-13 16:21:41 +0100 | [diff] [blame] | 1026 | else if( strcmp( p, "server_addr" ) == 0 ) |
| 1027 | opt.server_addr = q; |
Manuel Pégourié-Gonnard | e29fd4b | 2014-02-06 14:02:55 +0100 | [diff] [blame] | 1028 | else if( strcmp( p, "dtls" ) == 0 ) |
| 1029 | { |
| 1030 | int t = atoi( q ); |
| 1031 | if( t == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1032 | opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM; |
Manuel Pégourié-Gonnard | e29fd4b | 2014-02-06 14:02:55 +0100 | [diff] [blame] | 1033 | else if( t == 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1034 | opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM; |
Manuel Pégourié-Gonnard | e29fd4b | 2014-02-06 14:02:55 +0100 | [diff] [blame] | 1035 | else |
| 1036 | goto usage; |
| 1037 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1038 | else if( strcmp( p, "debug_level" ) == 0 ) |
| 1039 | { |
| 1040 | opt.debug_level = atoi( q ); |
| 1041 | if( opt.debug_level < 0 || opt.debug_level > 65535 ) |
| 1042 | goto usage; |
| 1043 | } |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 1044 | else if( strcmp( p, "nbio" ) == 0 ) |
| 1045 | { |
| 1046 | opt.nbio = atoi( q ); |
| 1047 | if( opt.nbio < 0 || opt.nbio > 2 ) |
| 1048 | goto usage; |
| 1049 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 1050 | else if( strcmp( p, "read_timeout" ) == 0 ) |
| 1051 | opt.read_timeout = atoi( q ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1052 | else if( strcmp( p, "ca_file" ) == 0 ) |
| 1053 | opt.ca_file = q; |
| 1054 | else if( strcmp( p, "ca_path" ) == 0 ) |
| 1055 | opt.ca_path = q; |
| 1056 | else if( strcmp( p, "crt_file" ) == 0 ) |
| 1057 | opt.crt_file = q; |
| 1058 | else if( strcmp( p, "key_file" ) == 0 ) |
| 1059 | opt.key_file = q; |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 1060 | else if( strcmp( p, "crt_file2" ) == 0 ) |
| 1061 | opt.crt_file2 = q; |
| 1062 | else if( strcmp( p, "key_file2" ) == 0 ) |
| 1063 | opt.key_file2 = q; |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 1064 | else if( strcmp( p, "dhm_file" ) == 0 ) |
| 1065 | opt.dhm_file = q; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1066 | else if( strcmp( p, "psk" ) == 0 ) |
| 1067 | opt.psk = q; |
| 1068 | else if( strcmp( p, "psk_identity" ) == 0 ) |
| 1069 | opt.psk_identity = q; |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 1070 | else if( strcmp( p, "psk_list" ) == 0 ) |
| 1071 | opt.psk_list = q; |
Manuel Pégourié-Gonnard | 70905a7 | 2015-09-16 11:08:34 +0200 | [diff] [blame] | 1072 | else if( strcmp( p, "ecjpake_pw" ) == 0 ) |
| 1073 | opt.ecjpake_pw = q; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1074 | else if( strcmp( p, "force_ciphersuite" ) == 0 ) |
| 1075 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1076 | opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1077 | |
Manuel Pégourié-Gonnard | 8de259b | 2014-06-11 14:19:06 +0200 | [diff] [blame] | 1078 | if( opt.force_ciphersuite[0] == 0 ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1079 | { |
| 1080 | ret = 2; |
| 1081 | goto usage; |
| 1082 | } |
| 1083 | opt.force_ciphersuite[1] = 0; |
| 1084 | } |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1085 | else if( strcmp( p, "curves" ) == 0 ) |
| 1086 | opt.curves = q; |
Manuel Pégourié-Gonnard | 6dc0781 | 2014-06-11 13:50:34 +0200 | [diff] [blame] | 1087 | else if( strcmp( p, "version_suites" ) == 0 ) |
| 1088 | opt.version_suites = q; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1089 | else if( strcmp( p, "renegotiation" ) == 0 ) |
| 1090 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1091 | opt.renegotiation = (atoi( q )) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : |
| 1092 | MBEDTLS_SSL_RENEGOTIATION_DISABLED; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1093 | } |
| 1094 | else if( strcmp( p, "allow_legacy" ) == 0 ) |
| 1095 | { |
Manuel Pégourié-Gonnard | 85d915b | 2014-11-03 20:10:36 +0100 | [diff] [blame] | 1096 | switch( atoi( q ) ) |
| 1097 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1098 | case -1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE; break; |
| 1099 | case 0: opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION; break; |
| 1100 | case 1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION; break; |
Manuel Pégourié-Gonnard | 85d915b | 2014-11-03 20:10:36 +0100 | [diff] [blame] | 1101 | default: goto usage; |
| 1102 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1103 | } |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 1104 | else if( strcmp( p, "renegotiate" ) == 0 ) |
| 1105 | { |
| 1106 | opt.renegotiate = atoi( q ); |
| 1107 | if( opt.renegotiate < 0 || opt.renegotiate > 1 ) |
| 1108 | goto usage; |
| 1109 | } |
Manuel Pégourié-Gonnard | fae355e | 2014-07-04 14:32:27 +0200 | [diff] [blame] | 1110 | else if( strcmp( p, "renego_delay" ) == 0 ) |
| 1111 | { |
| 1112 | opt.renego_delay = atoi( q ); |
| 1113 | } |
Manuel Pégourié-Gonnard | 590f416 | 2014-11-05 14:23:03 +0100 | [diff] [blame] | 1114 | else if( strcmp( p, "renego_period" ) == 0 ) |
| 1115 | { |
Andres AG | 0b736db | 2017-02-08 14:05:57 +0000 | [diff] [blame] | 1116 | #if defined(_MSC_VER) |
| 1117 | opt.renego_period = _strtoui64( q, NULL, 10 ); |
| 1118 | #else |
| 1119 | if( sscanf( q, "%" SCNu64, &opt.renego_period ) != 1 ) |
| 1120 | goto usage; |
| 1121 | #endif /* _MSC_VER */ |
| 1122 | if( opt.renego_period < 2 ) |
Manuel Pégourié-Gonnard | 590f416 | 2014-11-05 14:23:03 +0100 | [diff] [blame] | 1123 | goto usage; |
| 1124 | } |
Manuel Pégourié-Gonnard | 67686c4 | 2014-08-15 11:17:27 +0200 | [diff] [blame] | 1125 | else if( strcmp( p, "exchanges" ) == 0 ) |
| 1126 | { |
| 1127 | opt.exchanges = atoi( q ); |
Manuel Pégourié-Gonnard | 6a2bc23 | 2014-10-09 15:33:13 +0200 | [diff] [blame] | 1128 | if( opt.exchanges < 0 ) |
Manuel Pégourié-Gonnard | 67686c4 | 2014-08-15 11:17:27 +0200 | [diff] [blame] | 1129 | goto usage; |
| 1130 | } |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1131 | else if( strcmp( p, "min_version" ) == 0 ) |
| 1132 | { |
| 1133 | if( strcmp( q, "ssl3" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1134 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1135 | else if( strcmp( q, "tls1" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1136 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1; |
Manuel Pégourié-Gonnard | 83218f1 | 2014-02-12 11:11:12 +0100 | [diff] [blame] | 1137 | else if( strcmp( q, "tls1_1" ) == 0 || |
| 1138 | strcmp( q, "dtls1" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1139 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2; |
Manuel Pégourié-Gonnard | 83218f1 | 2014-02-12 11:11:12 +0100 | [diff] [blame] | 1140 | else if( strcmp( q, "tls1_2" ) == 0 || |
| 1141 | strcmp( q, "dtls1_2" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1142 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1143 | else |
| 1144 | goto usage; |
| 1145 | } |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1146 | else if( strcmp( p, "max_version" ) == 0 ) |
| 1147 | { |
| 1148 | if( strcmp( q, "ssl3" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1149 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0; |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1150 | else if( strcmp( q, "tls1" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1151 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1; |
Manuel Pégourié-Gonnard | 83218f1 | 2014-02-12 11:11:12 +0100 | [diff] [blame] | 1152 | else if( strcmp( q, "tls1_1" ) == 0 || |
| 1153 | strcmp( q, "dtls1" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1154 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2; |
Manuel Pégourié-Gonnard | 83218f1 | 2014-02-12 11:11:12 +0100 | [diff] [blame] | 1155 | else if( strcmp( q, "tls1_2" ) == 0 || |
| 1156 | strcmp( q, "dtls1_2" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1157 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3; |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1158 | else |
| 1159 | goto usage; |
| 1160 | } |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 1161 | else if( strcmp( p, "arc4" ) == 0 ) |
| 1162 | { |
| 1163 | switch( atoi( q ) ) |
| 1164 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1165 | case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break; |
| 1166 | case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break; |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 1167 | default: goto usage; |
| 1168 | } |
| 1169 | } |
Gilles Peskine | bc70a18 | 2017-05-09 15:59:24 +0200 | [diff] [blame] | 1170 | else if( strcmp( p, "allow_sha1" ) == 0 ) |
| 1171 | { |
| 1172 | switch( atoi( q ) ) |
| 1173 | { |
| 1174 | case 0: opt.allow_sha1 = 0; break; |
| 1175 | case 1: opt.allow_sha1 = 1; break; |
| 1176 | default: goto usage; |
| 1177 | } |
| 1178 | } |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1179 | else if( strcmp( p, "force_version" ) == 0 ) |
| 1180 | { |
| 1181 | if( strcmp( q, "ssl3" ) == 0 ) |
| 1182 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1183 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0; |
| 1184 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0; |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1185 | } |
| 1186 | else if( strcmp( q, "tls1" ) == 0 ) |
| 1187 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1188 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1; |
| 1189 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1; |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1190 | } |
Manuel Pégourié-Gonnard | fe3f73b | 2014-03-26 12:16:44 +0100 | [diff] [blame] | 1191 | else if( strcmp( q, "tls1_1" ) == 0 ) |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1192 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1193 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2; |
| 1194 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2; |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1195 | } |
Manuel Pégourié-Gonnard | fe3f73b | 2014-03-26 12:16:44 +0100 | [diff] [blame] | 1196 | else if( strcmp( q, "tls1_2" ) == 0 ) |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1197 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1198 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3; |
| 1199 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3; |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1200 | } |
Manuel Pégourié-Gonnard | fe3f73b | 2014-03-26 12:16:44 +0100 | [diff] [blame] | 1201 | else if( strcmp( q, "dtls1" ) == 0 ) |
| 1202 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1203 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2; |
| 1204 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2; |
| 1205 | opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM; |
Manuel Pégourié-Gonnard | fe3f73b | 2014-03-26 12:16:44 +0100 | [diff] [blame] | 1206 | } |
| 1207 | else if( strcmp( q, "dtls1_2" ) == 0 ) |
| 1208 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1209 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3; |
| 1210 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3; |
| 1211 | opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM; |
Manuel Pégourié-Gonnard | fe3f73b | 2014-03-26 12:16:44 +0100 | [diff] [blame] | 1212 | } |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1213 | else |
| 1214 | goto usage; |
| 1215 | } |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 1216 | else if( strcmp( p, "auth_mode" ) == 0 ) |
| 1217 | { |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 1218 | if( ( opt.auth_mode = get_auth_mode( q ) ) < 0 ) |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 1219 | goto usage; |
| 1220 | } |
Janos Follath | 4817e27 | 2017-04-10 13:44:33 +0100 | [diff] [blame] | 1221 | else if( strcmp( p, "cert_req_ca_list" ) == 0 ) |
| 1222 | { |
| 1223 | opt.cert_req_ca_list = atoi( q ); |
| 1224 | if( opt.cert_req_ca_list < 0 || opt.cert_req_ca_list > 1 ) |
| 1225 | goto usage; |
| 1226 | } |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 1227 | else if( strcmp( p, "max_frag_len" ) == 0 ) |
| 1228 | { |
| 1229 | if( strcmp( q, "512" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1230 | opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512; |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 1231 | else if( strcmp( q, "1024" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1232 | opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024; |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 1233 | else if( strcmp( q, "2048" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1234 | opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048; |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 1235 | else if( strcmp( q, "4096" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1236 | opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096; |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 1237 | else |
| 1238 | goto usage; |
| 1239 | } |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1240 | else if( strcmp( p, "alpn" ) == 0 ) |
| 1241 | { |
| 1242 | opt.alpn_string = q; |
| 1243 | } |
Manuel Pégourié-Gonnard | e117a8f | 2015-01-09 12:39:35 +0100 | [diff] [blame] | 1244 | else if( strcmp( p, "trunc_hmac" ) == 0 ) |
| 1245 | { |
| 1246 | switch( atoi( q ) ) |
| 1247 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1248 | case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break; |
| 1249 | case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break; |
Manuel Pégourié-Gonnard | e117a8f | 2015-01-09 12:39:35 +0100 | [diff] [blame] | 1250 | default: goto usage; |
| 1251 | } |
| 1252 | } |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1253 | else if( strcmp( p, "extended_ms" ) == 0 ) |
| 1254 | { |
| 1255 | switch( atoi( q ) ) |
| 1256 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1257 | case 0: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED; break; |
| 1258 | case 1: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; break; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1259 | default: goto usage; |
| 1260 | } |
| 1261 | } |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1262 | else if( strcmp( p, "etm" ) == 0 ) |
| 1263 | { |
| 1264 | switch( atoi( q ) ) |
| 1265 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1266 | case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break; |
| 1267 | case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1268 | default: goto usage; |
| 1269 | } |
| 1270 | } |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 1271 | else if( strcmp( p, "tickets" ) == 0 ) |
| 1272 | { |
| 1273 | opt.tickets = atoi( q ); |
| 1274 | if( opt.tickets < 0 || opt.tickets > 1 ) |
| 1275 | goto usage; |
| 1276 | } |
Manuel Pégourié-Gonnard | dbe1ee1 | 2014-02-21 09:18:13 +0100 | [diff] [blame] | 1277 | else if( strcmp( p, "ticket_timeout" ) == 0 ) |
| 1278 | { |
| 1279 | opt.ticket_timeout = atoi( q ); |
| 1280 | if( opt.ticket_timeout < 0 ) |
| 1281 | goto usage; |
| 1282 | } |
Manuel Pégourié-Gonnard | 4c88345 | 2014-02-20 21:32:41 +0100 | [diff] [blame] | 1283 | else if( strcmp( p, "cache_max" ) == 0 ) |
| 1284 | { |
| 1285 | opt.cache_max = atoi( q ); |
| 1286 | if( opt.cache_max < 0 ) |
| 1287 | goto usage; |
| 1288 | } |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 1289 | else if( strcmp( p, "cache_timeout" ) == 0 ) |
| 1290 | { |
| 1291 | opt.cache_timeout = atoi( q ); |
| 1292 | if( opt.cache_timeout < 0 ) |
| 1293 | goto usage; |
| 1294 | } |
Manuel Pégourié-Gonnard | 26820e3 | 2014-07-23 19:34:59 +0200 | [diff] [blame] | 1295 | else if( strcmp( p, "cookies" ) == 0 ) |
| 1296 | { |
| 1297 | opt.cookies = atoi( q ); |
| 1298 | if( opt.cookies < -1 || opt.cookies > 1) |
| 1299 | goto usage; |
| 1300 | } |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1301 | else if( strcmp( p, "anti_replay" ) == 0 ) |
| 1302 | { |
| 1303 | opt.anti_replay = atoi( q ); |
| 1304 | if( opt.anti_replay < 0 || opt.anti_replay > 1) |
| 1305 | goto usage; |
| 1306 | } |
Manuel Pégourié-Gonnard | e698f59 | 2014-10-14 19:36:36 +0200 | [diff] [blame] | 1307 | else if( strcmp( p, "badmac_limit" ) == 0 ) |
| 1308 | { |
| 1309 | opt.badmac_limit = atoi( q ); |
| 1310 | if( opt.badmac_limit < 0 ) |
| 1311 | goto usage; |
| 1312 | } |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 1313 | else if( strcmp( p, "hs_timeout" ) == 0 ) |
| 1314 | { |
| 1315 | if( ( p = strchr( q, '-' ) ) == NULL ) |
| 1316 | goto usage; |
| 1317 | *p++ = '\0'; |
| 1318 | opt.hs_to_min = atoi( q ); |
| 1319 | opt.hs_to_max = atoi( p ); |
| 1320 | if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min ) |
| 1321 | goto usage; |
| 1322 | } |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 1323 | else if( strcmp( p, "sni" ) == 0 ) |
| 1324 | { |
| 1325 | opt.sni = q; |
| 1326 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1327 | else |
| 1328 | goto usage; |
| 1329 | } |
| 1330 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1331 | #if defined(MBEDTLS_DEBUG_C) |
| 1332 | mbedtls_debug_set_threshold( opt.debug_level ); |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 1333 | #endif |
| 1334 | |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1335 | if( opt.force_ciphersuite[0] > 0 ) |
| 1336 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1337 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
| 1338 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] ); |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1339 | |
Paul Bakker | 5b55b79 | 2013-07-19 13:43:43 +0200 | [diff] [blame] | 1340 | if( opt.max_version != -1 && |
| 1341 | ciphersuite_info->min_minor_ver > opt.max_version ) |
| 1342 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1343 | mbedtls_printf("forced ciphersuite not allowed with this protocol version\n"); |
Paul Bakker | 5b55b79 | 2013-07-19 13:43:43 +0200 | [diff] [blame] | 1344 | ret = 2; |
| 1345 | goto usage; |
| 1346 | } |
| 1347 | if( opt.min_version != -1 && |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1348 | ciphersuite_info->max_minor_ver < opt.min_version ) |
| 1349 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1350 | mbedtls_printf("forced ciphersuite not allowed with this protocol version\n"); |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1351 | ret = 2; |
| 1352 | goto usage; |
| 1353 | } |
Manuel Pégourié-Gonnard | 798f15a | 2014-03-26 18:12:04 +0100 | [diff] [blame] | 1354 | |
| 1355 | /* If we select a version that's not supported by |
| 1356 | * this suite, then there will be no common ciphersuite... */ |
| 1357 | if( opt.max_version == -1 || |
| 1358 | opt.max_version > ciphersuite_info->max_minor_ver ) |
| 1359 | { |
Paul Bakker | 5b55b79 | 2013-07-19 13:43:43 +0200 | [diff] [blame] | 1360 | opt.max_version = ciphersuite_info->max_minor_ver; |
Manuel Pégourié-Gonnard | 798f15a | 2014-03-26 18:12:04 +0100 | [diff] [blame] | 1361 | } |
Paul Bakker | 5b55b79 | 2013-07-19 13:43:43 +0200 | [diff] [blame] | 1362 | if( opt.min_version < ciphersuite_info->min_minor_ver ) |
Manuel Pégourié-Gonnard | 798f15a | 2014-03-26 18:12:04 +0100 | [diff] [blame] | 1363 | { |
Paul Bakker | 5b55b79 | 2013-07-19 13:43:43 +0200 | [diff] [blame] | 1364 | opt.min_version = ciphersuite_info->min_minor_ver; |
Manuel Pégourié-Gonnard | 798f15a | 2014-03-26 18:12:04 +0100 | [diff] [blame] | 1365 | /* DTLS starts with TLS 1.1 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1366 | if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 1367 | opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 ) |
| 1368 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2; |
Manuel Pégourié-Gonnard | 798f15a | 2014-03-26 18:12:04 +0100 | [diff] [blame] | 1369 | } |
Manuel Pégourié-Gonnard | d42b7c8 | 2015-03-20 19:44:04 +0000 | [diff] [blame] | 1370 | |
| 1371 | /* Enable RC4 if needed and not explicitly disabled */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1372 | if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 ) |
Manuel Pégourié-Gonnard | d42b7c8 | 2015-03-20 19:44:04 +0000 | [diff] [blame] | 1373 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1374 | if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED ) |
Manuel Pégourié-Gonnard | d42b7c8 | 2015-03-20 19:44:04 +0000 | [diff] [blame] | 1375 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1376 | mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n"); |
Manuel Pégourié-Gonnard | d42b7c8 | 2015-03-20 19:44:04 +0000 | [diff] [blame] | 1377 | ret = 2; |
| 1378 | goto usage; |
| 1379 | } |
| 1380 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1381 | opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; |
Manuel Pégourié-Gonnard | d42b7c8 | 2015-03-20 19:44:04 +0000 | [diff] [blame] | 1382 | } |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1383 | } |
| 1384 | |
Manuel Pégourié-Gonnard | 6dc0781 | 2014-06-11 13:50:34 +0200 | [diff] [blame] | 1385 | if( opt.version_suites != NULL ) |
| 1386 | { |
| 1387 | const char *name[4] = { 0 }; |
| 1388 | |
| 1389 | /* Parse 4-element coma-separated list */ |
| 1390 | for( i = 0, p = (char *) opt.version_suites; |
| 1391 | i < 4 && *p != '\0'; |
| 1392 | i++ ) |
| 1393 | { |
| 1394 | name[i] = p; |
| 1395 | |
| 1396 | /* Terminate the current string and move on to next one */ |
| 1397 | while( *p != ',' && *p != '\0' ) |
| 1398 | p++; |
| 1399 | if( *p == ',' ) |
| 1400 | *p++ = '\0'; |
| 1401 | } |
| 1402 | |
| 1403 | if( i != 4 ) |
| 1404 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1405 | mbedtls_printf( "too few values for version_suites\n" ); |
Manuel Pégourié-Gonnard | 6dc0781 | 2014-06-11 13:50:34 +0200 | [diff] [blame] | 1406 | ret = 1; |
| 1407 | goto exit; |
| 1408 | } |
| 1409 | |
| 1410 | memset( version_suites, 0, sizeof( version_suites ) ); |
| 1411 | |
| 1412 | /* Get the suites identifiers from their name */ |
| 1413 | for( i = 0; i < 4; i++ ) |
| 1414 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1415 | version_suites[i][0] = mbedtls_ssl_get_ciphersuite_id( name[i] ); |
Manuel Pégourié-Gonnard | 6dc0781 | 2014-06-11 13:50:34 +0200 | [diff] [blame] | 1416 | |
| 1417 | if( version_suites[i][0] == 0 ) |
| 1418 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1419 | mbedtls_printf( "unknown ciphersuite: '%s'\n", name[i] ); |
Manuel Pégourié-Gonnard | 6dc0781 | 2014-06-11 13:50:34 +0200 | [diff] [blame] | 1420 | ret = 2; |
| 1421 | goto usage; |
| 1422 | } |
| 1423 | } |
| 1424 | } |
| 1425 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1426 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1427 | /* |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 1428 | * Unhexify the pre-shared key and parse the list if any given |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1429 | */ |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 1430 | if( unhexify( psk, opt.psk, &psk_len ) != 0 ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1431 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1432 | mbedtls_printf( "pre-shared key not valid hex\n" ); |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 1433 | goto exit; |
| 1434 | } |
| 1435 | |
| 1436 | if( opt.psk_list != NULL ) |
| 1437 | { |
| 1438 | if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1439 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1440 | mbedtls_printf( "psk_list invalid" ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1441 | goto exit; |
| 1442 | } |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1443 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1444 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1445 | |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1446 | #if defined(MBEDTLS_ECP_C) |
| 1447 | if( opt.curves != NULL ) |
| 1448 | { |
| 1449 | p = (char *) opt.curves; |
| 1450 | i = 0; |
| 1451 | |
| 1452 | if( strcmp( p, "none" ) == 0 ) |
| 1453 | { |
| 1454 | curve_list[0] = MBEDTLS_ECP_DP_NONE; |
| 1455 | } |
| 1456 | else if( strcmp( p, "default" ) != 0 ) |
| 1457 | { |
| 1458 | /* Leave room for a final NULL in curve list */ |
Hanno Becker | 8651a43 | 2017-06-09 16:13:22 +0100 | [diff] [blame] | 1459 | while( i < CURVE_LIST_SIZE - 1 && *p != '\0' ) |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1460 | { |
| 1461 | q = p; |
| 1462 | |
| 1463 | /* Terminate the current string */ |
| 1464 | while( *p != ',' && *p != '\0' ) |
| 1465 | p++; |
| 1466 | if( *p == ',' ) |
| 1467 | *p++ = '\0'; |
| 1468 | |
| 1469 | if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL ) |
| 1470 | { |
| 1471 | curve_list[i++] = curve_cur->grp_id; |
| 1472 | } |
| 1473 | else |
| 1474 | { |
| 1475 | mbedtls_printf( "unknown curve %s\n", q ); |
| 1476 | mbedtls_printf( "supported curves: " ); |
| 1477 | for( curve_cur = mbedtls_ecp_curve_list(); |
| 1478 | curve_cur->grp_id != MBEDTLS_ECP_DP_NONE; |
| 1479 | curve_cur++ ) |
| 1480 | { |
| 1481 | mbedtls_printf( "%s ", curve_cur->name ); |
| 1482 | } |
| 1483 | mbedtls_printf( "\n" ); |
| 1484 | goto exit; |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | mbedtls_printf("Number of curves: %d\n", i ); |
| 1489 | |
Hanno Becker | 8651a43 | 2017-06-09 16:13:22 +0100 | [diff] [blame] | 1490 | if( i == CURVE_LIST_SIZE - 1 && *p != '\0' ) |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1491 | { |
Hanno Becker | 8651a43 | 2017-06-09 16:13:22 +0100 | [diff] [blame] | 1492 | mbedtls_printf( "curves list too long, maximum %d", |
| 1493 | CURVE_LIST_SIZE - 1 ); |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1494 | goto exit; |
| 1495 | } |
| 1496 | |
| 1497 | curve_list[i] = MBEDTLS_ECP_DP_NONE; |
| 1498 | } |
| 1499 | } |
| 1500 | #endif /* MBEDTLS_ECP_C */ |
| 1501 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1502 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1503 | if( opt.alpn_string != NULL ) |
| 1504 | { |
| 1505 | p = (char *) opt.alpn_string; |
| 1506 | i = 0; |
| 1507 | |
| 1508 | /* Leave room for a final NULL in alpn_list */ |
Hanno Becker | 8651a43 | 2017-06-09 16:13:22 +0100 | [diff] [blame] | 1509 | while( i < ALPN_LIST_SIZE - 1 && *p != '\0' ) |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1510 | { |
| 1511 | alpn_list[i++] = p; |
| 1512 | |
| 1513 | /* Terminate the current string and move on to next one */ |
| 1514 | while( *p != ',' && *p != '\0' ) |
| 1515 | p++; |
| 1516 | if( *p == ',' ) |
| 1517 | *p++ = '\0'; |
| 1518 | } |
| 1519 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1520 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1521 | |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1522 | /* |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1523 | * 0. Initialize the RNG and the session data |
| 1524 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1525 | mbedtls_printf( "\n . Seeding the random number generator..." ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1526 | fflush( stdout ); |
| 1527 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1528 | mbedtls_entropy_init( &entropy ); |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 1529 | if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 1530 | (const unsigned char *) pers, |
| 1531 | strlen( pers ) ) ) != 0 ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1532 | { |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 1533 | mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n", -ret ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1534 | goto exit; |
| 1535 | } |
| 1536 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1537 | mbedtls_printf( " ok\n" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1538 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1539 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1540 | /* |
| 1541 | * 1.1. Load the trusted CA |
| 1542 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1543 | mbedtls_printf( " . Loading the CA root certificate ..." ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1544 | fflush( stdout ); |
| 1545 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1546 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1547 | if( strlen( opt.ca_path ) ) |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1548 | if( strcmp( opt.ca_path, "none" ) == 0 ) |
| 1549 | ret = 0; |
| 1550 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1551 | ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1552 | else if( strlen( opt.ca_file ) ) |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1553 | if( strcmp( opt.ca_file, "none" ) == 0 ) |
| 1554 | ret = 0; |
| 1555 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1556 | ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file ); |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 1557 | else |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1558 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1559 | #if defined(MBEDTLS_CERTS_C) |
| 1560 | for( i = 0; mbedtls_test_cas[i] != NULL; i++ ) |
Manuel Pégourié-Gonnard | 2f16506 | 2015-03-27 10:20:26 +0100 | [diff] [blame] | 1561 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1562 | ret = mbedtls_x509_crt_parse( &cacert, |
| 1563 | (const unsigned char *) mbedtls_test_cas[i], |
| 1564 | mbedtls_test_cas_len[i] ); |
Manuel Pégourié-Gonnard | 2f16506 | 2015-03-27 10:20:26 +0100 | [diff] [blame] | 1565 | if( ret != 0 ) |
| 1566 | break; |
| 1567 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1568 | #else |
| 1569 | { |
| 1570 | ret = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1571 | mbedtls_printf("MBEDTLS_CERTS_C not defined."); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1572 | } |
| 1573 | #endif |
| 1574 | if( ret < 0 ) |
| 1575 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1576 | mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1577 | goto exit; |
| 1578 | } |
| 1579 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1580 | mbedtls_printf( " ok (%d skipped)\n", ret ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1581 | |
| 1582 | /* |
| 1583 | * 1.2. Load own certificate and private key |
| 1584 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1585 | mbedtls_printf( " . Loading the server cert. and key..." ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1586 | fflush( stdout ); |
| 1587 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1588 | #if defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1589 | if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 ) |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 1590 | { |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1591 | key_cert_init++; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1592 | if( ( ret = mbedtls_x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 ) |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1593 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1594 | mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n", |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1595 | -ret ); |
| 1596 | goto exit; |
| 1597 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 1598 | } |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1599 | if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 ) |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 1600 | { |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1601 | key_cert_init++; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1602 | if( ( ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 ) |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1603 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1604 | mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n", -ret ); |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1605 | goto exit; |
| 1606 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 1607 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1608 | if( key_cert_init == 1 ) |
| 1609 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1610 | mbedtls_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" ); |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1611 | goto exit; |
| 1612 | } |
| 1613 | |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1614 | if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 ) |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 1615 | { |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1616 | key_cert_init2++; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1617 | if( ( ret = mbedtls_x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 ) |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1618 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1619 | mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n", |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1620 | -ret ); |
| 1621 | goto exit; |
| 1622 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 1623 | } |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1624 | if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 ) |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 1625 | { |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1626 | key_cert_init2++; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1627 | if( ( ret = mbedtls_pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 ) |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1628 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1629 | mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n", |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1630 | -ret ); |
| 1631 | goto exit; |
| 1632 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 1633 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1634 | if( key_cert_init2 == 1 ) |
| 1635 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1636 | mbedtls_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" ); |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1637 | goto exit; |
| 1638 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 1639 | #endif |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1640 | if( key_cert_init == 0 && |
| 1641 | strcmp( opt.crt_file, "none" ) != 0 && |
| 1642 | strcmp( opt.key_file, "none" ) != 0 && |
| 1643 | key_cert_init2 == 0 && |
| 1644 | strcmp( opt.crt_file2, "none" ) != 0 && |
| 1645 | strcmp( opt.key_file2, "none" ) != 0 ) |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 1646 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1647 | #if !defined(MBEDTLS_CERTS_C) |
| 1648 | mbedtls_printf( "Not certificated or key provided, and \n" |
| 1649 | "MBEDTLS_CERTS_C not defined!\n" ); |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 1650 | goto exit; |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1651 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1652 | #if defined(MBEDTLS_RSA_C) |
| 1653 | if( ( ret = mbedtls_x509_crt_parse( &srvcert, |
| 1654 | (const unsigned char *) mbedtls_test_srv_crt_rsa, |
| 1655 | mbedtls_test_srv_crt_rsa_len ) ) != 0 ) |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1656 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1657 | mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret ); |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1658 | goto exit; |
| 1659 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1660 | if( ( ret = mbedtls_pk_parse_key( &pkey, |
| 1661 | (const unsigned char *) mbedtls_test_srv_key_rsa, |
| 1662 | mbedtls_test_srv_key_rsa_len, NULL, 0 ) ) != 0 ) |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1663 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1664 | mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n", -ret ); |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1665 | goto exit; |
| 1666 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1667 | key_cert_init = 2; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1668 | #endif /* MBEDTLS_RSA_C */ |
| 1669 | #if defined(MBEDTLS_ECDSA_C) |
| 1670 | if( ( ret = mbedtls_x509_crt_parse( &srvcert2, |
| 1671 | (const unsigned char *) mbedtls_test_srv_crt_ec, |
| 1672 | mbedtls_test_srv_crt_ec_len ) ) != 0 ) |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1673 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1674 | mbedtls_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret ); |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1675 | goto exit; |
| 1676 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1677 | if( ( ret = mbedtls_pk_parse_key( &pkey2, |
| 1678 | (const unsigned char *) mbedtls_test_srv_key_ec, |
| 1679 | mbedtls_test_srv_key_ec_len, NULL, 0 ) ) != 0 ) |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1680 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1681 | mbedtls_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret ); |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1682 | goto exit; |
| 1683 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1684 | key_cert_init2 = 2; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1685 | #endif /* MBEDTLS_ECDSA_C */ |
| 1686 | #endif /* MBEDTLS_CERTS_C */ |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1687 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1688 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1689 | mbedtls_printf( " ok\n" ); |
| 1690 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1691 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1692 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 1693 | if( opt.dhm_file != NULL ) |
| 1694 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1695 | mbedtls_printf( " . Loading DHM parameters..." ); |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 1696 | fflush( stdout ); |
| 1697 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1698 | if( ( ret = mbedtls_dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 ) |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 1699 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1700 | mbedtls_printf( " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n", |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 1701 | -ret ); |
| 1702 | goto exit; |
| 1703 | } |
| 1704 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1705 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 1706 | } |
| 1707 | #endif |
| 1708 | |
Manuel Pégourié-Gonnard | 6c7af4c | 2015-04-03 16:41:52 +0200 | [diff] [blame] | 1709 | #if defined(SNI_OPTION) |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 1710 | if( opt.sni != NULL ) |
| 1711 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1712 | mbedtls_printf( " . Setting up SNI information..." ); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 1713 | fflush( stdout ); |
| 1714 | |
| 1715 | if( ( sni_info = sni_parse( opt.sni ) ) == NULL ) |
| 1716 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1717 | mbedtls_printf( " failed\n" ); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 1718 | goto exit; |
| 1719 | } |
| 1720 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1721 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 1722 | } |
Manuel Pégourié-Gonnard | 6c7af4c | 2015-04-03 16:41:52 +0200 | [diff] [blame] | 1723 | #endif /* SNI_OPTION */ |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 1724 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1725 | /* |
| 1726 | * 2. Setup the listening TCP socket |
| 1727 | */ |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 1728 | mbedtls_printf( " . Bind on %s://%s:%s/ ...", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1729 | opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp", |
Manuel Pégourié-Gonnard | 8a06d9c | 2014-03-23 18:23:41 +0100 | [diff] [blame] | 1730 | opt.server_addr ? opt.server_addr : "*", |
| 1731 | opt.server_port ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1732 | fflush( stdout ); |
| 1733 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1734 | if( ( ret = mbedtls_net_bind( &listen_fd, opt.server_addr, opt.server_port, |
| 1735 | opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? |
| 1736 | MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1737 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1738 | mbedtls_printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1739 | goto exit; |
| 1740 | } |
| 1741 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1742 | mbedtls_printf( " ok\n" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1743 | |
| 1744 | /* |
| 1745 | * 3. Setup stuff |
| 1746 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1747 | mbedtls_printf( " . Setting up the SSL/TLS structure..." ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1748 | fflush( stdout ); |
| 1749 | |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 1750 | if( ( ret = mbedtls_ssl_config_defaults( &conf, |
| 1751 | MBEDTLS_SSL_IS_SERVER, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 1752 | opt.transport, |
| 1753 | MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 ) |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1754 | { |
| 1755 | mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret ); |
| 1756 | goto exit; |
| 1757 | } |
| 1758 | |
Gilles Peskine | ef86ab2 | 2017-05-05 18:59:02 +0200 | [diff] [blame] | 1759 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1760 | /* The default algorithms profile disables SHA-1, but our tests still |
| 1761 | rely on it heavily. Hence we allow it here. A real-world server |
| 1762 | should use the default profile unless there is a good reason not to. */ |
Gilles Peskine | bc70a18 | 2017-05-09 15:59:24 +0200 | [diff] [blame] | 1763 | if( opt.allow_sha1 > 0 ) |
| 1764 | { |
| 1765 | crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ); |
| 1766 | mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test ); |
Gilles Peskine | 682df09 | 2017-05-11 19:01:11 +0200 | [diff] [blame] | 1767 | mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test ); |
Gilles Peskine | bc70a18 | 2017-05-09 15:59:24 +0200 | [diff] [blame] | 1768 | } |
Gilles Peskine | ef86ab2 | 2017-05-05 18:59:02 +0200 | [diff] [blame] | 1769 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 1770 | |
Manuel Pégourié-Gonnard | fa44f20 | 2015-03-27 17:52:25 +0100 | [diff] [blame] | 1771 | if( opt.auth_mode != DFL_AUTH_MODE ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1772 | mbedtls_ssl_conf_authmode( &conf, opt.auth_mode ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1773 | |
Janos Follath | 4817e27 | 2017-04-10 13:44:33 +0100 | [diff] [blame] | 1774 | if( opt.cert_req_ca_list != DFL_CERT_REQ_CA_LIST ) |
| 1775 | mbedtls_ssl_conf_cert_req_ca_list( &conf, opt.cert_req_ca_list ); |
| 1776 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1777 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 1778 | if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1779 | mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1780 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 1781 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1782 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1783 | if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 ) |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1784 | { |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1785 | mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1786 | goto exit; |
| 1787 | }; |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 1788 | #endif |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 1789 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1790 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | e117a8f | 2015-01-09 12:39:35 +0100 | [diff] [blame] | 1791 | if( opt.trunc_hmac != DFL_TRUNC_HMAC ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1792 | mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac ); |
Manuel Pégourié-Gonnard | e117a8f | 2015-01-09 12:39:35 +0100 | [diff] [blame] | 1793 | #endif |
| 1794 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1795 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1796 | if( opt.extended_ms != DFL_EXTENDED_MS ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1797 | mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1798 | #endif |
| 1799 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1800 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1801 | if( opt.etm != DFL_ETM ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1802 | mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1803 | #endif |
| 1804 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1805 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1806 | if( opt.alpn_string != NULL ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1807 | if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 ) |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1808 | { |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1809 | mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1810 | goto exit; |
| 1811 | } |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1812 | #endif |
| 1813 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1814 | mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg ); |
| 1815 | mbedtls_ssl_conf_dbg( &conf, my_debug, stdout ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1816 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1817 | #if defined(MBEDTLS_SSL_CACHE_C) |
Manuel Pégourié-Gonnard | 4c88345 | 2014-02-20 21:32:41 +0100 | [diff] [blame] | 1818 | if( opt.cache_max != -1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1819 | mbedtls_ssl_cache_set_max_entries( &cache, opt.cache_max ); |
Manuel Pégourié-Gonnard | 4c88345 | 2014-02-20 21:32:41 +0100 | [diff] [blame] | 1820 | |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 1821 | if( opt.cache_timeout != -1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1822 | mbedtls_ssl_cache_set_timeout( &cache, opt.cache_timeout ); |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 1823 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1824 | mbedtls_ssl_conf_session_cache( &conf, &cache, |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1825 | mbedtls_ssl_cache_get, |
| 1826 | mbedtls_ssl_cache_set ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1827 | #endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1828 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1829 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 1830 | if( opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED ) |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1831 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 1832 | if( ( ret = mbedtls_ssl_ticket_setup( &ticket_ctx, |
| 1833 | mbedtls_ctr_drbg_random, &ctr_drbg, |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame] | 1834 | MBEDTLS_CIPHER_AES_256_GCM, |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 1835 | opt.ticket_timeout ) ) != 0 ) |
| 1836 | { |
| 1837 | mbedtls_printf( " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret ); |
| 1838 | goto exit; |
| 1839 | } |
Manuel Pégourié-Gonnard | dbe1ee1 | 2014-02-21 09:18:13 +0100 | [diff] [blame] | 1840 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 1841 | mbedtls_ssl_conf_session_tickets_cb( &conf, |
| 1842 | mbedtls_ssl_ticket_write, |
| 1843 | mbedtls_ssl_ticket_parse, |
| 1844 | &ticket_ctx ); |
| 1845 | } |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 1846 | #endif |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 1847 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1848 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1849 | if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 98545f1 | 2014-07-22 22:10:43 +0200 | [diff] [blame] | 1850 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1851 | #if defined(MBEDTLS_SSL_COOKIE_C) |
Manuel Pégourié-Gonnard | 26820e3 | 2014-07-23 19:34:59 +0200 | [diff] [blame] | 1852 | if( opt.cookies > 0 ) |
Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 1853 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1854 | if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx, |
| 1855 | mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26820e3 | 2014-07-23 19:34:59 +0200 | [diff] [blame] | 1856 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1857 | mbedtls_printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | 26820e3 | 2014-07-23 19:34:59 +0200 | [diff] [blame] | 1858 | goto exit; |
| 1859 | } |
Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 1860 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1861 | mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check, |
Manuel Pégourié-Gonnard | 26820e3 | 2014-07-23 19:34:59 +0200 | [diff] [blame] | 1862 | &cookie_ctx ); |
| 1863 | } |
| 1864 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1865 | #endif /* MBEDTLS_SSL_COOKIE_C */ |
| 1866 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) |
Manuel Pégourié-Gonnard | 26820e3 | 2014-07-23 19:34:59 +0200 | [diff] [blame] | 1867 | if( opt.cookies == 0 ) |
| 1868 | { |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1869 | mbedtls_ssl_conf_dtls_cookies( &conf, NULL, NULL, NULL ); |
Manuel Pégourié-Gonnard | 26820e3 | 2014-07-23 19:34:59 +0200 | [diff] [blame] | 1870 | } |
| 1871 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1872 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ |
Manuel Pégourié-Gonnard | 26820e3 | 2014-07-23 19:34:59 +0200 | [diff] [blame] | 1873 | { |
| 1874 | ; /* Nothing to do */ |
| 1875 | } |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1876 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1877 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1878 | if( opt.anti_replay != DFL_ANTI_REPLAY ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1879 | mbedtls_ssl_conf_dtls_anti_replay( &conf, opt.anti_replay ); |
Manuel Pégourié-Gonnard | e698f59 | 2014-10-14 19:36:36 +0200 | [diff] [blame] | 1880 | #endif |
| 1881 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1882 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Manuel Pégourié-Gonnard | e698f59 | 2014-10-14 19:36:36 +0200 | [diff] [blame] | 1883 | if( opt.badmac_limit != DFL_BADMAC_LIMIT ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1884 | mbedtls_ssl_conf_dtls_badmac_limit( &conf, opt.badmac_limit ); |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1885 | #endif |
Manuel Pégourié-Gonnard | 98545f1 | 2014-07-22 22:10:43 +0200 | [diff] [blame] | 1886 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1887 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 98545f1 | 2014-07-22 22:10:43 +0200 | [diff] [blame] | 1888 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1889 | if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1890 | mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite ); |
Manuel Pégourié-Gonnard | d42b7c8 | 2015-03-20 19:44:04 +0000 | [diff] [blame] | 1891 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 1892 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | d42b7c8 | 2015-03-20 19:44:04 +0000 | [diff] [blame] | 1893 | if( opt.arc4 != DFL_ARC4 ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1894 | mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 ); |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 1895 | #endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1896 | |
Manuel Pégourié-Gonnard | 6dc0781 | 2014-06-11 13:50:34 +0200 | [diff] [blame] | 1897 | if( opt.version_suites != NULL ) |
| 1898 | { |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1899 | mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[0], |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1900 | MBEDTLS_SSL_MAJOR_VERSION_3, |
| 1901 | MBEDTLS_SSL_MINOR_VERSION_0 ); |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1902 | mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[1], |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1903 | MBEDTLS_SSL_MAJOR_VERSION_3, |
| 1904 | MBEDTLS_SSL_MINOR_VERSION_1 ); |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1905 | mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[2], |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1906 | MBEDTLS_SSL_MAJOR_VERSION_3, |
| 1907 | MBEDTLS_SSL_MINOR_VERSION_2 ); |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1908 | mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[3], |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1909 | MBEDTLS_SSL_MAJOR_VERSION_3, |
| 1910 | MBEDTLS_SSL_MINOR_VERSION_3 ); |
Manuel Pégourié-Gonnard | 6dc0781 | 2014-06-11 13:50:34 +0200 | [diff] [blame] | 1911 | } |
| 1912 | |
Manuel Pégourié-Gonnard | 85d915b | 2014-11-03 20:10:36 +0100 | [diff] [blame] | 1913 | if( opt.allow_legacy != DFL_ALLOW_LEGACY ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1914 | mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1915 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1916 | mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation ); |
Manuel Pégourié-Gonnard | 590f416 | 2014-11-05 14:23:03 +0100 | [diff] [blame] | 1917 | |
Manuel Pégourié-Gonnard | fae355e | 2014-07-04 14:32:27 +0200 | [diff] [blame] | 1918 | if( opt.renego_delay != DFL_RENEGO_DELAY ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1919 | mbedtls_ssl_conf_renegotiation_enforced( &conf, opt.renego_delay ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1920 | |
Manuel Pégourié-Gonnard | 590f416 | 2014-11-05 14:23:03 +0100 | [diff] [blame] | 1921 | if( opt.renego_period != DFL_RENEGO_PERIOD ) |
| 1922 | { |
Andres AG | 692ad84 | 2017-01-19 16:30:57 +0000 | [diff] [blame] | 1923 | PUT_UINT64_BE( renego_period, opt.renego_period, 0 ); |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1924 | mbedtls_ssl_conf_renegotiation_period( &conf, renego_period ); |
Manuel Pégourié-Gonnard | 590f416 | 2014-11-05 14:23:03 +0100 | [diff] [blame] | 1925 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1926 | #endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1927 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1928 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1929 | if( strcmp( opt.ca_path, "none" ) != 0 && |
| 1930 | strcmp( opt.ca_file, "none" ) != 0 ) |
| 1931 | { |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1932 | mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL ); |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1933 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1934 | if( key_cert_init ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1935 | if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 ) |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1936 | { |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1937 | mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1938 | goto exit; |
| 1939 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1940 | if( key_cert_init2 ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1941 | if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, &pkey2 ) ) != 0 ) |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1942 | { |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1943 | mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1944 | goto exit; |
| 1945 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 1946 | #endif |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 1947 | |
Manuel Pégourié-Gonnard | 6c7af4c | 2015-04-03 16:41:52 +0200 | [diff] [blame] | 1948 | #if defined(SNI_OPTION) |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 1949 | if( opt.sni != NULL ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1950 | mbedtls_ssl_conf_sni( &conf, sni_callback, sni_info ); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 1951 | #endif |
| 1952 | |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1953 | #if defined(MBEDTLS_ECP_C) |
| 1954 | if( opt.curves != NULL && |
| 1955 | strcmp( opt.curves, "default" ) != 0 ) |
| 1956 | { |
| 1957 | mbedtls_ssl_conf_curves( &conf, curve_list ); |
| 1958 | } |
| 1959 | #endif |
| 1960 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1961 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Manuel Pégourié-Gonnard | dc019b9 | 2014-06-10 15:24:51 +0200 | [diff] [blame] | 1962 | if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 ) |
| 1963 | { |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1964 | ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len, |
Manuel Pégourié-Gonnard | dc019b9 | 2014-06-10 15:24:51 +0200 | [diff] [blame] | 1965 | (const unsigned char *) opt.psk_identity, |
| 1966 | strlen( opt.psk_identity ) ); |
| 1967 | if( ret != 0 ) |
| 1968 | { |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1969 | mbedtls_printf( " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", - ret ); |
Manuel Pégourié-Gonnard | dc019b9 | 2014-06-10 15:24:51 +0200 | [diff] [blame] | 1970 | goto exit; |
| 1971 | } |
| 1972 | } |
| 1973 | |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 1974 | if( opt.psk_list != NULL ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1975 | mbedtls_ssl_conf_psk_cb( &conf, psk_callback, psk_info ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 1976 | #endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1977 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1978 | #if defined(MBEDTLS_DHM_C) |
Paul Bakker | 5d19f86 | 2012-09-28 07:33:00 +0000 | [diff] [blame] | 1979 | /* |
| 1980 | * Use different group than default DHM group |
| 1981 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1982 | #if defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 1983 | if( opt.dhm_file != NULL ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1984 | ret = mbedtls_ssl_conf_dh_param_ctx( &conf, &dhm ); |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 1985 | #endif |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 1986 | if( ret != 0 ) |
| 1987 | { |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1988 | mbedtls_printf( " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", - ret ); |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 1989 | goto exit; |
| 1990 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1991 | #endif |
| 1992 | |
Manuel Pégourié-Gonnard | 8c8be1e | 2015-03-31 14:21:11 +0200 | [diff] [blame] | 1993 | if( opt.min_version != DFL_MIN_VERSION ) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 1994 | mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version ); |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1995 | |
Manuel Pégourié-Gonnard | 8c8be1e | 2015-03-31 14:21:11 +0200 | [diff] [blame] | 1996 | if( opt.max_version != DFL_MIN_VERSION ) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 1997 | mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version ); |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1998 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1999 | if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 ) |
| 2000 | { |
| 2001 | mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret ); |
| 2002 | goto exit; |
| 2003 | } |
| 2004 | |
| 2005 | if( opt.nbio == 2 ) |
| 2006 | mbedtls_ssl_set_bio( &ssl, &client_fd, my_send, my_recv, NULL ); |
| 2007 | else |
| 2008 | mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, |
Manuel Pégourié-Gonnard | d4f04db | 2015-05-14 18:58:17 +0200 | [diff] [blame] | 2009 | opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 2010 | |
Manuel Pégourié-Gonnard | d2377e7 | 2015-05-13 13:58:56 +0200 | [diff] [blame] | 2011 | #if defined(MBEDTLS_TIMING_C) |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 2012 | mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay, |
| 2013 | mbedtls_timing_get_delay ); |
Manuel Pégourié-Gonnard | d2377e7 | 2015-05-13 13:58:56 +0200 | [diff] [blame] | 2014 | #endif |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 2015 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2016 | mbedtls_printf( " ok\n" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2017 | |
| 2018 | reset: |
Manuel Pégourié-Gonnard | b6440a4 | 2014-09-20 12:03:00 +0200 | [diff] [blame] | 2019 | #if !defined(_WIN32) |
| 2020 | if( received_sigterm ) |
| 2021 | { |
Manuel Pégourié-Gonnard | 4fa619f | 2018-01-22 10:55:10 +0100 | [diff] [blame^] | 2022 | mbedtls_printf( " interrupted by SIGTERM (not in net_accept())\n" ); |
| 2023 | if( ret == MBEDTLS_ERR_NET_INVALID_CONTEXT ) |
| 2024 | ret = 0; |
| 2025 | |
Manuel Pégourié-Gonnard | b6440a4 | 2014-09-20 12:03:00 +0200 | [diff] [blame] | 2026 | goto exit; |
| 2027 | } |
| 2028 | #endif |
| 2029 | |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 2030 | if( ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT ) |
| 2031 | { |
| 2032 | mbedtls_printf( " ! Client initiated reconnection from same port\n" ); |
| 2033 | goto handshake; |
| 2034 | } |
| 2035 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2036 | #ifdef MBEDTLS_ERROR_C |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2037 | if( ret != 0 ) |
| 2038 | { |
| 2039 | char error_buf[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2040 | mbedtls_strerror( ret, error_buf, 100 ); |
| 2041 | mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2042 | } |
| 2043 | #endif |
| 2044 | |
Manuel Pégourié-Gonnard | 3d7d00a | 2015-06-30 15:55:03 +0200 | [diff] [blame] | 2045 | mbedtls_net_free( &client_fd ); |
Manuel Pégourié-Gonnard | 8a06d9c | 2014-03-23 18:23:41 +0100 | [diff] [blame] | 2046 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2047 | mbedtls_ssl_session_reset( &ssl ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2048 | |
| 2049 | /* |
| 2050 | * 3. Wait until a client connects |
| 2051 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2052 | mbedtls_printf( " . Waiting for a remote connection ..." ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2053 | fflush( stdout ); |
| 2054 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 2055 | if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd, |
Manuel Pégourié-Gonnard | 0b104b0 | 2015-05-14 21:52:40 +0200 | [diff] [blame] | 2056 | client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2057 | { |
Paul Bakker | c1283d3 | 2014-08-18 11:05:51 +0200 | [diff] [blame] | 2058 | #if !defined(_WIN32) |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 2059 | if( received_sigterm ) |
| 2060 | { |
Manuel Pégourié-Gonnard | 4fa619f | 2018-01-22 10:55:10 +0100 | [diff] [blame^] | 2061 | mbedtls_printf( " interrupted by SIGTERM (in net_accept())\n" ); |
| 2062 | if( ret == MBEDTLS_ERR_NET_ACCEPT_FAILED ) |
| 2063 | ret = 0; |
| 2064 | |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 2065 | goto exit; |
| 2066 | } |
Paul Bakker | c1283d3 | 2014-08-18 11:05:51 +0200 | [diff] [blame] | 2067 | #endif |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 2068 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2069 | mbedtls_printf( " failed\n ! mbedtls_net_accept returned -0x%x\n\n", -ret ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2070 | goto exit; |
| 2071 | } |
| 2072 | |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 2073 | if( opt.nbio > 0 ) |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 2074 | ret = mbedtls_net_set_nonblock( &client_fd ); |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 2075 | else |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 2076 | ret = mbedtls_net_set_block( &client_fd ); |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 2077 | if( ret != 0 ) |
| 2078 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2079 | mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret ); |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 2080 | goto exit; |
| 2081 | } |
| 2082 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2083 | mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2084 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2085 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) |
| 2086 | if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 336b824 | 2014-07-22 17:57:43 +0200 | [diff] [blame] | 2087 | { |
Manuel Pégourié-Gonnard | 0b104b0 | 2015-05-14 21:52:40 +0200 | [diff] [blame] | 2088 | if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl, |
| 2089 | client_ip, cliip_len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 336b824 | 2014-07-22 17:57:43 +0200 | [diff] [blame] | 2090 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2091 | mbedtls_printf( " failed\n ! " |
Manuel Pégourié-Gonnard | 151dc77 | 2015-05-14 13:55:51 +0200 | [diff] [blame] | 2092 | "mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n", -ret ); |
Manuel Pégourié-Gonnard | 336b824 | 2014-07-22 17:57:43 +0200 | [diff] [blame] | 2093 | goto exit; |
| 2094 | } |
| 2095 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2096 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ |
Manuel Pégourié-Gonnard | 336b824 | 2014-07-22 17:57:43 +0200 | [diff] [blame] | 2097 | |
Manuel Pégourié-Gonnard | 70905a7 | 2015-09-16 11:08:34 +0200 | [diff] [blame] | 2098 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 2099 | if( opt.ecjpake_pw != DFL_ECJPAKE_PW ) |
| 2100 | { |
| 2101 | if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl, |
| 2102 | (const unsigned char *) opt.ecjpake_pw, |
| 2103 | strlen( opt.ecjpake_pw ) ) ) != 0 ) |
| 2104 | { |
| 2105 | mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret ); |
| 2106 | goto exit; |
| 2107 | } |
| 2108 | } |
| 2109 | #endif |
| 2110 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2111 | mbedtls_printf( " ok\n" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2112 | |
| 2113 | /* |
| 2114 | * 4. Handshake |
| 2115 | */ |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 2116 | handshake: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2117 | mbedtls_printf( " . Performing the SSL/TLS handshake..." ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2118 | fflush( stdout ); |
| 2119 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2120 | do ret = mbedtls_ssl_handshake( &ssl ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2121 | while( ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 2122 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2123 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2124 | if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ) |
Manuel Pégourié-Gonnard | caecdae | 2014-10-13 19:04:37 +0200 | [diff] [blame] | 2125 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2126 | mbedtls_printf( " hello verification requested\n" ); |
Manuel Pégourié-Gonnard | caecdae | 2014-10-13 19:04:37 +0200 | [diff] [blame] | 2127 | ret = 0; |
| 2128 | goto reset; |
| 2129 | } |
| 2130 | else if( ret != 0 ) |
| 2131 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2132 | mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret ); |
Manuel Pégourié-Gonnard | 6ea831d | 2015-06-22 16:50:52 +0200 | [diff] [blame] | 2133 | |
| 2134 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 2135 | if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ) |
| 2136 | { |
| 2137 | char vrfy_buf[512]; |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 2138 | flags = mbedtls_ssl_get_verify_result( &ssl ); |
Manuel Pégourié-Gonnard | 6ea831d | 2015-06-22 16:50:52 +0200 | [diff] [blame] | 2139 | |
| 2140 | mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags ); |
| 2141 | |
| 2142 | mbedtls_printf( "%s\n", vrfy_buf ); |
| 2143 | } |
| 2144 | #endif |
| 2145 | |
Manuel Pégourié-Gonnard | caecdae | 2014-10-13 19:04:37 +0200 | [diff] [blame] | 2146 | goto reset; |
| 2147 | } |
| 2148 | else /* ret == 0 */ |
| 2149 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2150 | mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n", |
| 2151 | mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) ); |
Manuel Pégourié-Gonnard | caecdae | 2014-10-13 19:04:37 +0200 | [diff] [blame] | 2152 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2153 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2154 | if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 ) |
| 2155 | mbedtls_printf( " [ Record expansion is %d ]\n", ret ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 2156 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2157 | mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 2158 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2159 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2160 | mbedtls_printf( " [ Maximum fragment length is %u ]\n", |
| 2161 | (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) ); |
| 2162 | #endif |
| 2163 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2164 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 2165 | if( opt.alpn_string != NULL ) |
| 2166 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2167 | const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl ); |
| 2168 | mbedtls_printf( " [ Application Layer Protocol is %s ]\n", |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 2169 | alp ? alp : "(none)" ); |
| 2170 | } |
| 2171 | #endif |
| 2172 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2173 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2174 | /* |
Ron Eldor | 2a47be5 | 2017-06-20 15:23:23 +0300 | [diff] [blame] | 2175 | * 5. Verify the client certificate |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2176 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2177 | mbedtls_printf( " . Verifying peer X.509 certificate..." ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2178 | |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 2179 | if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2180 | { |
Manuel Pégourié-Gonnard | 89addc4 | 2015-04-20 10:56:18 +0100 | [diff] [blame] | 2181 | char vrfy_buf[512]; |
| 2182 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2183 | mbedtls_printf( " failed\n" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2184 | |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 2185 | mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2186 | |
Manuel Pégourié-Gonnard | 89addc4 | 2015-04-20 10:56:18 +0100 | [diff] [blame] | 2187 | mbedtls_printf( "%s\n", vrfy_buf ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2188 | } |
| 2189 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2190 | mbedtls_printf( " ok\n" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2191 | |
Manuel Pégourié-Gonnard | 1c5b9fc | 2015-06-27 14:38:51 +0200 | [diff] [blame] | 2192 | if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2193 | { |
Manuel Pégourié-Gonnard | 1c5b9fc | 2015-06-27 14:38:51 +0200 | [diff] [blame] | 2194 | char crt_buf[512]; |
| 2195 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2196 | mbedtls_printf( " . Peer certificate information ...\n" ); |
Manuel Pégourié-Gonnard | 1c5b9fc | 2015-06-27 14:38:51 +0200 | [diff] [blame] | 2197 | mbedtls_x509_crt_info( crt_buf, sizeof( crt_buf ), " ", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2198 | mbedtls_ssl_get_peer_cert( &ssl ) ); |
Manuel Pégourié-Gonnard | 6755717 | 2015-07-02 11:15:48 +0200 | [diff] [blame] | 2199 | mbedtls_printf( "%s\n", crt_buf ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2200 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2201 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2202 | |
Manuel Pégourié-Gonnard | 6a2bc23 | 2014-10-09 15:33:13 +0200 | [diff] [blame] | 2203 | if( opt.exchanges == 0 ) |
| 2204 | goto close_notify; |
| 2205 | |
Manuel Pégourié-Gonnard | 6a0017b | 2015-01-22 10:33:29 +0000 | [diff] [blame] | 2206 | exchanges_left = opt.exchanges; |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 2207 | data_exchange: |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2208 | /* |
| 2209 | * 6. Read the HTTP Request |
| 2210 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2211 | mbedtls_printf( " < Read from client:" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2212 | fflush( stdout ); |
| 2213 | |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2214 | /* |
| 2215 | * TLS and DTLS need different reading styles (stream vs datagram) |
| 2216 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2217 | if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2218 | { |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2219 | do |
| 2220 | { |
| 2221 | int terminated = 0; |
| 2222 | len = sizeof( buf ) - 1; |
| 2223 | memset( buf, 0, sizeof( buf ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2224 | ret = mbedtls_ssl_read( &ssl, buf, len ); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2225 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2226 | if( ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 2227 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ) |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2228 | continue; |
| 2229 | |
| 2230 | if( ret <= 0 ) |
| 2231 | { |
| 2232 | switch( ret ) |
| 2233 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2234 | case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: |
| 2235 | mbedtls_printf( " connection was closed gracefully\n" ); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2236 | goto close_notify; |
| 2237 | |
| 2238 | case 0: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2239 | case MBEDTLS_ERR_NET_CONN_RESET: |
| 2240 | mbedtls_printf( " connection was reset by peer\n" ); |
| 2241 | ret = MBEDTLS_ERR_NET_CONN_RESET; |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2242 | goto reset; |
| 2243 | |
| 2244 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2245 | mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret ); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2246 | goto reset; |
| 2247 | } |
| 2248 | } |
| 2249 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2250 | if( mbedtls_ssl_get_bytes_avail( &ssl ) == 0 ) |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2251 | { |
| 2252 | len = ret; |
| 2253 | buf[len] = '\0'; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2254 | mbedtls_printf( " %d bytes read\n\n%s\n", len, (char *) buf ); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2255 | |
| 2256 | /* End of message should be detected according to the syntax of the |
| 2257 | * application protocol (eg HTTP), just use a dummy test here. */ |
| 2258 | if( buf[len - 1] == '\n' ) |
| 2259 | terminated = 1; |
| 2260 | } |
| 2261 | else |
| 2262 | { |
| 2263 | int extra_len, ori_len; |
| 2264 | unsigned char *larger_buf; |
| 2265 | |
| 2266 | ori_len = ret; |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 2267 | extra_len = (int) mbedtls_ssl_get_bytes_avail( &ssl ); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2268 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 2269 | larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 ); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2270 | if( larger_buf == NULL ) |
| 2271 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2272 | mbedtls_printf( " ! memory allocation failed\n" ); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2273 | ret = 1; |
| 2274 | goto reset; |
| 2275 | } |
| 2276 | |
| 2277 | memset( larger_buf, 0, ori_len + extra_len ); |
| 2278 | memcpy( larger_buf, buf, ori_len ); |
| 2279 | |
| 2280 | /* This read should never fail and get the whole cached data */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2281 | ret = mbedtls_ssl_read( &ssl, larger_buf + ori_len, extra_len ); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2282 | if( ret != extra_len || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2283 | mbedtls_ssl_get_bytes_avail( &ssl ) != 0 ) |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2284 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2285 | mbedtls_printf( " ! mbedtls_ssl_read failed on cached data\n" ); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2286 | ret = 1; |
| 2287 | goto reset; |
| 2288 | } |
| 2289 | |
| 2290 | larger_buf[ori_len + extra_len] = '\0'; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2291 | mbedtls_printf( " %u bytes read (%u + %u)\n\n%s\n", |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2292 | ori_len + extra_len, ori_len, extra_len, |
| 2293 | (char *) larger_buf ); |
| 2294 | |
| 2295 | /* End of message should be detected according to the syntax of the |
| 2296 | * application protocol (eg HTTP), just use a dummy test here. */ |
| 2297 | if( larger_buf[ori_len + extra_len - 1] == '\n' ) |
| 2298 | terminated = 1; |
| 2299 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2300 | mbedtls_free( larger_buf ); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | if( terminated ) |
| 2304 | { |
| 2305 | ret = 0; |
| 2306 | break; |
| 2307 | } |
| 2308 | } |
| 2309 | while( 1 ); |
| 2310 | } |
| 2311 | else /* Not stream, so datagram */ |
| 2312 | { |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2313 | len = sizeof( buf ) - 1; |
| 2314 | memset( buf, 0, sizeof( buf ) ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2315 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2316 | do ret = mbedtls_ssl_read( &ssl, buf, len ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2317 | while( ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 2318 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2319 | |
| 2320 | if( ret <= 0 ) |
| 2321 | { |
| 2322 | switch( ret ) |
| 2323 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2324 | case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: |
| 2325 | mbedtls_printf( " connection was closed gracefully\n" ); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2326 | ret = 0; |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 2327 | goto close_notify; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2328 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2329 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2330 | mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret ); |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 2331 | goto reset; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2332 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2335 | len = ret; |
| 2336 | buf[len] = '\0'; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2337 | mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf ); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2338 | ret = 0; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2339 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2340 | |
| 2341 | /* |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 2342 | * 7a. Request renegotiation while client is waiting for input from us. |
Manuel Pégourié-Gonnard | a6ace04 | 2014-10-15 12:44:41 +0200 | [diff] [blame] | 2343 | * (only on the first exchange, to be able to test retransmission) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2344 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2345 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 3a173f4 | 2015-01-22 13:30:33 +0000 | [diff] [blame] | 2346 | if( opt.renegotiate && exchanges_left == opt.exchanges ) |
Manuel Pégourié-Gonnard | 296e3b1 | 2014-08-19 12:59:03 +0200 | [diff] [blame] | 2347 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2348 | mbedtls_printf( " . Requestion renegotiation..." ); |
Manuel Pégourié-Gonnard | 296e3b1 | 2014-08-19 12:59:03 +0200 | [diff] [blame] | 2349 | fflush( stdout ); |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 2350 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2351 | while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 296e3b1 | 2014-08-19 12:59:03 +0200 | [diff] [blame] | 2352 | { |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2353 | if( ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 2354 | ret != MBEDTLS_ERR_SSL_WANT_WRITE ) |
Manuel Pégourié-Gonnard | 296e3b1 | 2014-08-19 12:59:03 +0200 | [diff] [blame] | 2355 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2356 | mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | 296e3b1 | 2014-08-19 12:59:03 +0200 | [diff] [blame] | 2357 | goto reset; |
| 2358 | } |
| 2359 | } |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 2360 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2361 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | 296e3b1 | 2014-08-19 12:59:03 +0200 | [diff] [blame] | 2362 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2363 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 296e3b1 | 2014-08-19 12:59:03 +0200 | [diff] [blame] | 2364 | |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 2365 | /* |
| 2366 | * 7. Write the 200 Response |
| 2367 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2368 | mbedtls_printf( " > Write to client:" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2369 | fflush( stdout ); |
| 2370 | |
| 2371 | len = sprintf( (char *) buf, HTTP_RESPONSE, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2372 | mbedtls_ssl_get_ciphersuite( &ssl ) ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2373 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2374 | if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2375 | { |
Manuel Pégourié-Gonnard | 2d87e41 | 2014-10-13 18:38:36 +0200 | [diff] [blame] | 2376 | for( written = 0, frags = 0; written < len; written += ret, frags++ ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2377 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2378 | while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) ) |
Manuel Pégourié-Gonnard | 2d87e41 | 2014-10-13 18:38:36 +0200 | [diff] [blame] | 2379 | <= 0 ) |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 2380 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2381 | if( ret == MBEDTLS_ERR_NET_CONN_RESET ) |
Manuel Pégourié-Gonnard | 2d87e41 | 2014-10-13 18:38:36 +0200 | [diff] [blame] | 2382 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2383 | mbedtls_printf( " failed\n ! peer closed the connection\n\n" ); |
Manuel Pégourié-Gonnard | 2d87e41 | 2014-10-13 18:38:36 +0200 | [diff] [blame] | 2384 | goto reset; |
| 2385 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2386 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2387 | if( ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 2388 | ret != MBEDTLS_ERR_SSL_WANT_WRITE ) |
Manuel Pégourié-Gonnard | 2d87e41 | 2014-10-13 18:38:36 +0200 | [diff] [blame] | 2389 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2390 | mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | 2d87e41 | 2014-10-13 18:38:36 +0200 | [diff] [blame] | 2391 | goto reset; |
| 2392 | } |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 2393 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2394 | } |
| 2395 | } |
Manuel Pégourié-Gonnard | 2d87e41 | 2014-10-13 18:38:36 +0200 | [diff] [blame] | 2396 | else /* Not stream, so datagram */ |
| 2397 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2398 | do ret = mbedtls_ssl_write( &ssl, buf, len ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2399 | while( ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 2400 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
Manuel Pégourié-Gonnard | 2d87e41 | 2014-10-13 18:38:36 +0200 | [diff] [blame] | 2401 | |
| 2402 | if( ret < 0 ) |
| 2403 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2404 | mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | 2d87e41 | 2014-10-13 18:38:36 +0200 | [diff] [blame] | 2405 | goto reset; |
| 2406 | } |
| 2407 | |
| 2408 | frags = 1; |
| 2409 | written = ret; |
| 2410 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2411 | |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 2412 | buf[written] = '\0'; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2413 | mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf ); |
Manuel Pégourié-Gonnard | a92ed48 | 2015-01-14 10:46:08 +0100 | [diff] [blame] | 2414 | ret = 0; |
Manuel Pégourié-Gonnard | f3dc2f6 | 2013-10-29 18:17:41 +0100 | [diff] [blame] | 2415 | |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 2416 | /* |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 2417 | * 7b. Continue doing data exchanges? |
| 2418 | */ |
Manuel Pégourié-Gonnard | 6a0017b | 2015-01-22 10:33:29 +0000 | [diff] [blame] | 2419 | if( --exchanges_left > 0 ) |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 2420 | goto data_exchange; |
| 2421 | |
| 2422 | /* |
| 2423 | * 8. Done, cleanly close the connection |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 2424 | */ |
| 2425 | close_notify: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2426 | mbedtls_printf( " . Closing the connection..." ); |
Manuel Pégourié-Gonnard | 6b0d268 | 2014-03-25 11:24:43 +0100 | [diff] [blame] | 2427 | |
Manuel Pégourié-Gonnard | 994f8b5 | 2014-10-09 19:56:44 +0200 | [diff] [blame] | 2428 | /* No error checking, the connection might be closed already */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2429 | do ret = mbedtls_ssl_close_notify( &ssl ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2430 | while( ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
Manuel Pégourié-Gonnard | 994f8b5 | 2014-10-09 19:56:44 +0200 | [diff] [blame] | 2431 | ret = 0; |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 2432 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2433 | mbedtls_printf( " done\n" ); |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 2434 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2435 | goto reset; |
| 2436 | |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 2437 | /* |
| 2438 | * Cleanup and exit |
| 2439 | */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2440 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2441 | #ifdef MBEDTLS_ERROR_C |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2442 | if( ret != 0 ) |
| 2443 | { |
| 2444 | char error_buf[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2445 | mbedtls_strerror( ret, error_buf, 100 ); |
| 2446 | mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2447 | } |
| 2448 | #endif |
| 2449 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2450 | mbedtls_printf( " . Cleaning up..." ); |
Manuel Pégourié-Gonnard | f29e5de | 2014-11-21 11:54:41 +0100 | [diff] [blame] | 2451 | fflush( stdout ); |
| 2452 | |
Manuel Pégourié-Gonnard | 3d7d00a | 2015-06-30 15:55:03 +0200 | [diff] [blame] | 2453 | mbedtls_net_free( &client_fd ); |
| 2454 | mbedtls_net_free( &listen_fd ); |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 2455 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2456 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO) |
| 2457 | mbedtls_dhm_free( &dhm ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 2458 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2459 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 2460 | mbedtls_x509_crt_free( &cacert ); |
| 2461 | mbedtls_x509_crt_free( &srvcert ); |
| 2462 | mbedtls_pk_free( &pkey ); |
| 2463 | mbedtls_x509_crt_free( &srvcert2 ); |
| 2464 | mbedtls_pk_free( &pkey2 ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 2465 | #endif |
Manuel Pégourié-Gonnard | 6c7af4c | 2015-04-03 16:41:52 +0200 | [diff] [blame] | 2466 | #if defined(SNI_OPTION) |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 2467 | sni_free( sni_info ); |
| 2468 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2469 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Manuel Pégourié-Gonnard | 4505ed3 | 2014-06-19 20:56:52 +0200 | [diff] [blame] | 2470 | psk_free( psk_info ); |
| 2471 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2472 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO) |
| 2473 | mbedtls_dhm_free( &dhm ); |
Manuel Pégourié-Gonnard | 4505ed3 | 2014-06-19 20:56:52 +0200 | [diff] [blame] | 2474 | #endif |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 2475 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2476 | mbedtls_ssl_free( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 2477 | mbedtls_ssl_config_free( &conf ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2478 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 2479 | mbedtls_entropy_free( &entropy ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2480 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2481 | #if defined(MBEDTLS_SSL_CACHE_C) |
| 2482 | mbedtls_ssl_cache_free( &cache ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2483 | #endif |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2484 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 2485 | mbedtls_ssl_ticket_free( &ticket_ctx ); |
| 2486 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2487 | #if defined(MBEDTLS_SSL_COOKIE_C) |
| 2488 | mbedtls_ssl_cookie_free( &cookie_ctx ); |
Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 2489 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2490 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2491 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 2492 | #if defined(MBEDTLS_MEMORY_DEBUG) |
| 2493 | mbedtls_memory_buffer_alloc_status(); |
Paul Bakker | 82024bf | 2013-07-04 11:52:32 +0200 | [diff] [blame] | 2494 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2495 | mbedtls_memory_buffer_alloc_free(); |
Paul Bakker | 1337aff | 2013-09-29 14:45:34 +0200 | [diff] [blame] | 2496 | #endif |
Paul Bakker | 82024bf | 2013-07-04 11:52:32 +0200 | [diff] [blame] | 2497 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2498 | mbedtls_printf( " done.\n" ); |
Manuel Pégourié-Gonnard | f29e5de | 2014-11-21 11:54:41 +0100 | [diff] [blame] | 2499 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2500 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2501 | mbedtls_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2502 | fflush( stdout ); getchar(); |
| 2503 | #endif |
| 2504 | |
Paul Bakker | dbd79ca | 2013-07-24 16:28:35 +0200 | [diff] [blame] | 2505 | // Shell can not handle large exit numbers -> 1 for errors |
| 2506 | if( ret < 0 ) |
| 2507 | ret = 1; |
| 2508 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2509 | return( ret ); |
| 2510 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2511 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C && |
| 2512 | MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && MBEDTLS_RSA_C && |
Manuel Pégourié-Gonnard | d2377e7 | 2015-05-13 13:58:56 +0200 | [diff] [blame] | 2513 | MBEDTLS_CTR_DRBG_C */ |