Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSL client with certificate authentication |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 860b516 | 2015-01-28 17:12:07 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://polarssl.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 24 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 29 | #if defined(POLARSSL_PLATFORM_C) |
| 30 | #include "polarssl/platform.h" |
| 31 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 33 | #define polarssl_fprintf fprintf |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 34 | #define polarssl_printf printf |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 35 | #endif |
| 36 | |
Manuel Pégourié-Gonnard | 013bffe | 2015-02-13 14:09:44 +0000 | [diff] [blame^] | 37 | #if defined(POLARSSL_ENTROPY_C) && defined(POLARSSL_FS_IO) && \ |
| 38 | defined(POLARSSL_SSL_TLS_C) && defined(POLARSSL_SSL_CLI_C) && \ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 39 | defined(POLARSSL_NET_C) && defined(POLARSSL_CTR_DRBG_C) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 40 | #include "polarssl/net.h" |
| 41 | #include "polarssl/ssl.h" |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 42 | #include "polarssl/entropy.h" |
| 43 | #include "polarssl/ctr_drbg.h" |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 44 | #include "polarssl/certs.h" |
| 45 | #include "polarssl/x509.h" |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 46 | #include "polarssl/error.h" |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 47 | #include "polarssl/debug.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 48 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 49 | #include <stdio.h> |
| 50 | #include <stdlib.h> |
| 51 | #include <string.h> |
| 52 | #endif |
| 53 | |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 54 | #if defined(POLARSSL_TIMING_C) |
| 55 | #include "polarssl/timing.h" |
| 56 | #endif |
| 57 | |
Paul Bakker | 93c32b2 | 2014-04-25 13:40:05 +0200 | [diff] [blame] | 58 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
| 59 | #if !defined snprintf |
| 60 | #define snprintf _snprintf |
| 61 | #endif |
| 62 | #endif |
| 63 | |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 64 | #define DFL_SERVER_NAME "localhost" |
Manuel Pégourié-Gonnard | 0d8780b | 2014-02-25 11:11:26 +0100 | [diff] [blame] | 65 | #define DFL_SERVER_ADDR NULL |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 66 | #define DFL_SERVER_PORT 4433 |
| 67 | #define DFL_REQUEST_PAGE "/" |
Manuel Pégourié-Gonnard | dea29c5 | 2014-06-18 13:07:56 +0200 | [diff] [blame] | 68 | #define DFL_REQUEST_SIZE -1 |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 69 | #define DFL_DEBUG_LEVEL 0 |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 70 | #define DFL_NBIO 0 |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 71 | #define DFL_CA_FILE "" |
Paul Bakker | 8d91458 | 2012-06-04 12:46:42 +0000 | [diff] [blame] | 72 | #define DFL_CA_PATH "" |
Paul Bakker | 6796839 | 2010-07-18 08:28:20 +0000 | [diff] [blame] | 73 | #define DFL_CRT_FILE "" |
| 74 | #define DFL_KEY_FILE "" |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 75 | #define DFL_PSK "" |
| 76 | #define DFL_PSK_IDENTITY "Client_identity" |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 77 | #define DFL_FORCE_CIPHER 0 |
Manuel Pégourié-Gonnard | 00d538f | 2014-03-31 10:44:40 +0200 | [diff] [blame] | 78 | #define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED |
Manuel Pégourié-Gonnard | 85d915b | 2014-11-03 20:10:36 +0100 | [diff] [blame] | 79 | #define DFL_ALLOW_LEGACY -2 |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 80 | #define DFL_RENEGOTIATE 0 |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 81 | #define DFL_EXCHANGES 1 |
Manuel Pégourié-Gonnard | 448ea50 | 2015-01-12 11:40:14 +0100 | [diff] [blame] | 82 | #define DFL_MIN_VERSION SSL_MINOR_VERSION_1 |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 83 | #define DFL_MAX_VERSION -1 |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 84 | #define DFL_ARC4 SSL_ARC4_DISABLED |
Manuel Pégourié-Gonnard | fcf2fc2 | 2014-03-11 11:10:27 +0100 | [diff] [blame] | 85 | #define DFL_AUTH_MODE SSL_VERIFY_REQUIRED |
Manuel Pégourié-Gonnard | 0df6b1f | 2013-07-16 13:39:57 +0200 | [diff] [blame] | 86 | #define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE |
Manuel Pégourié-Gonnard | 265fe99 | 2015-01-09 12:43:35 +0100 | [diff] [blame] | 87 | #define DFL_TRUNC_HMAC -1 |
Manuel Pégourié-Gonnard | c82ee35 | 2015-01-07 16:35:25 +0100 | [diff] [blame] | 88 | #define DFL_RECSPLIT -1 |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 89 | #define DFL_RECONNECT 0 |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 90 | #define DFL_RECO_DELAY 0 |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 91 | #define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 92 | #define DFL_ALPN_STRING NULL |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 93 | #define DFL_FALLBACK -1 |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 94 | #define DFL_EXTENDED_MS -1 |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 95 | #define DFL_ETM -1 |
Paul Bakker | 0e6975b | 2009-02-10 22:19:10 +0000 | [diff] [blame] | 96 | |
Paul Bakker | 93c32b2 | 2014-04-25 13:40:05 +0200 | [diff] [blame] | 97 | #define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: " |
| 98 | #define GET_REQUEST_END "\r\n\r\n" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 99 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 100 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 101 | #if defined(POLARSSL_FS_IO) |
| 102 | #define USAGE_IO \ |
Paul Bakker | 1f9d02d | 2012-11-20 10:30:55 +0100 | [diff] [blame] | 103 | " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \ |
| 104 | " default: \"\" (pre-loaded)\n" \ |
| 105 | " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \ |
| 106 | " default: \"\" (pre-loaded) (overrides ca_file)\n" \ |
| 107 | " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \ |
| 108 | " default: \"\" (pre-loaded)\n" \ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 109 | " key_file=%%s default: \"\" (pre-loaded)\n" |
| 110 | #else |
| 111 | #define USAGE_IO \ |
| 112 | " No file operations available (POLARSSL_FS_IO not defined)\n" |
| 113 | #endif /* POLARSSL_FS_IO */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 114 | #else |
| 115 | #define USAGE_IO "" |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 116 | #endif /* POLARSSL_X509_CRT_PARSE_C */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 117 | |
Manuel Pégourié-Gonnard | 8a3c64d | 2013-10-14 19:54:10 +0200 | [diff] [blame] | 118 | #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 119 | #define USAGE_PSK \ |
| 120 | " psk=%%s default: \"\" (in hex, without 0x)\n" \ |
| 121 | " psk_identity=%%s default: \"Client_identity\"\n" |
| 122 | #else |
| 123 | #define USAGE_PSK "" |
Manuel Pégourié-Gonnard | 8a3c64d | 2013-10-14 19:54:10 +0200 | [diff] [blame] | 124 | #endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 125 | |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 126 | #if defined(POLARSSL_SSL_SESSION_TICKETS) |
| 127 | #define USAGE_TICKETS \ |
| 128 | " tickets=%%d default: 1 (enabled)\n" |
| 129 | #else |
| 130 | #define USAGE_TICKETS "" |
| 131 | #endif /* POLARSSL_SSL_SESSION_TICKETS */ |
| 132 | |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 133 | #if defined(POLARSSL_SSL_TRUNCATED_HMAC) |
| 134 | #define USAGE_TRUNC_HMAC \ |
Manuel Pégourié-Gonnard | 265fe99 | 2015-01-09 12:43:35 +0100 | [diff] [blame] | 135 | " trunc_hmac=%%d default: library default\n" |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 136 | #else |
| 137 | #define USAGE_TRUNC_HMAC "" |
| 138 | #endif /* POLARSSL_SSL_TRUNCATED_HMAC */ |
| 139 | |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 140 | #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH) |
| 141 | #define USAGE_MAX_FRAG_LEN \ |
| 142 | " max_frag_len=%%d default: 16384 (tls default)\n" \ |
| 143 | " options: 512, 1024, 2048, 4096\n" |
| 144 | #else |
| 145 | #define USAGE_MAX_FRAG_LEN "" |
| 146 | #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */ |
| 147 | |
Manuel Pégourié-Gonnard | c82ee35 | 2015-01-07 16:35:25 +0100 | [diff] [blame] | 148 | #if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING) |
| 149 | #define USAGE_RECSPLIT \ |
| 150 | " recplit=%%d default: (library default)\n" |
| 151 | #else |
| 152 | #define USAGE_RECSPLIT |
| 153 | #endif |
| 154 | |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 155 | #if defined(POLARSSL_TIMING_C) |
| 156 | #define USAGE_TIME \ |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 157 | " reco_delay=%%d default: 0 seconds\n" |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 158 | #else |
| 159 | #define USAGE_TIME "" |
| 160 | #endif /* POLARSSL_TIMING_C */ |
| 161 | |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 162 | #if defined(POLARSSL_SSL_ALPN) |
| 163 | #define USAGE_ALPN \ |
| 164 | " alpn=%%s default: \"\" (disabled)\n" \ |
| 165 | " example: spdy/1,http/1.1\n" |
| 166 | #else |
| 167 | #define USAGE_ALPN "" |
| 168 | #endif /* POLARSSL_SSL_ALPN */ |
| 169 | |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 170 | #if defined(POLARSSL_SSL_FALLBACK_SCSV) |
| 171 | #define USAGE_FALLBACK \ |
| 172 | " fallback=0/1 default: (library default: off)\n" |
| 173 | #else |
| 174 | #define USAGE_FALLBACK "" |
| 175 | #endif |
| 176 | |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 177 | #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) |
| 178 | #define USAGE_EMS \ |
| 179 | " extended_ms=0/1 default: (library default: on)\n" |
| 180 | #else |
| 181 | #define USAGE_EMS "" |
| 182 | #endif |
| 183 | |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 184 | #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) |
| 185 | #define USAGE_ETM \ |
| 186 | " etm=0/1 default: (library default: on)\n" |
| 187 | #else |
| 188 | #define USAGE_ETM "" |
| 189 | #endif |
| 190 | |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 191 | #if defined(POLARSSL_SSL_RENEGOTIATION) |
| 192 | #define USAGE_RENEGO \ |
| 193 | " renegotiation=%%d default: 0 (disabled)\n" \ |
| 194 | " renegotiate=%%d default: 0 (disabled)\n" |
| 195 | #else |
| 196 | #define USAGE_RENEGO "" |
| 197 | #endif |
| 198 | |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 199 | #define USAGE \ |
Paul Bakker | 6796839 | 2010-07-18 08:28:20 +0000 | [diff] [blame] | 200 | "\n usage: ssl_client2 param=<>...\n" \ |
| 201 | "\n acceptable parameters:\n" \ |
| 202 | " server_name=%%s default: localhost\n" \ |
Manuel Pégourié-Gonnard | 0d8780b | 2014-02-25 11:11:26 +0100 | [diff] [blame] | 203 | " server_addr=%%s default: given by name\n" \ |
Paul Bakker | 6796839 | 2010-07-18 08:28:20 +0000 | [diff] [blame] | 204 | " server_port=%%d default: 4433\n" \ |
Paul Bakker | 6796839 | 2010-07-18 08:28:20 +0000 | [diff] [blame] | 205 | " request_page=%%s default: \".\"\n" \ |
Manuel Pégourié-Gonnard | dea29c5 | 2014-06-18 13:07:56 +0200 | [diff] [blame] | 206 | " request_size=%%d default: about 34 (basic request)\n" \ |
| 207 | " (minimum: 0, max: 16384)\n" \ |
Manuel Pégourié-Gonnard | 2fc243d | 2014-02-19 18:22:59 +0100 | [diff] [blame] | 208 | " debug_level=%%d default: 0 (disabled)\n" \ |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 209 | " nbio=%%d default: 0 (blocking I/O)\n" \ |
| 210 | " options: 1 (non-blocking), 2 (added delays)\n" \ |
Manuel Pégourié-Gonnard | 2fc243d | 2014-02-19 18:22:59 +0100 | [diff] [blame] | 211 | "\n" \ |
Manuel Pégourié-Gonnard | 478fac4 | 2015-01-19 16:40:56 +0000 | [diff] [blame] | 212 | " auth_mode=%%s default: \"required\"\n" \ |
Manuel Pégourié-Gonnard | 2fc243d | 2014-02-19 18:22:59 +0100 | [diff] [blame] | 213 | " options: none, optional, required\n" \ |
| 214 | USAGE_IO \ |
| 215 | "\n" \ |
| 216 | USAGE_PSK \ |
| 217 | "\n" \ |
Manuel Pégourié-Gonnard | 85d915b | 2014-11-03 20:10:36 +0100 | [diff] [blame] | 218 | " allow_legacy=%%d default: (library default: no)\n" \ |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 219 | USAGE_RENEGO \ |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 220 | " exchanges=%%d default: 1\n" \ |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 221 | " reconnect=%%d default: 0 (disabled)\n" \ |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 222 | USAGE_TIME \ |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 223 | USAGE_TICKETS \ |
Manuel Pégourié-Gonnard | 2fc243d | 2014-02-19 18:22:59 +0100 | [diff] [blame] | 224 | USAGE_MAX_FRAG_LEN \ |
| 225 | USAGE_TRUNC_HMAC \ |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 226 | USAGE_ALPN \ |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 227 | USAGE_FALLBACK \ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 228 | USAGE_EMS \ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 229 | USAGE_ETM \ |
Manuel Pégourié-Gonnard | c82ee35 | 2015-01-07 16:35:25 +0100 | [diff] [blame] | 230 | USAGE_RECSPLIT \ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 231 | "\n" \ |
| 232 | " min_version=%%s default: \"\" (ssl3)\n" \ |
| 233 | " max_version=%%s default: \"\" (tls1_2)\n" \ |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 234 | " arc4=%%d default: 0 (disabled)\n" \ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 235 | " force_version=%%s default: \"\" (none)\n" \ |
| 236 | " options: ssl3, tls1, tls1_1, tls1_2\n" \ |
| 237 | "\n" \ |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 238 | " force_ciphersuite=<name> default: all enabled\n"\ |
| 239 | " acceptable ciphersuite names:\n" |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 240 | |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 241 | #if !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_FS_IO) || \ |
| 242 | !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \ |
| 243 | !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C) |
| 244 | int main( void ) |
| 245 | { |
| 246 | polarssl_printf("POLARSSL_ENTROPY_C and/or " |
| 247 | "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or " |
| 248 | "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n"); |
| 249 | return( 0 ); |
| 250 | } |
| 251 | #else |
| 252 | /* |
| 253 | * global options |
| 254 | */ |
| 255 | struct options |
| 256 | { |
| 257 | const char *server_name; /* hostname of the server (client only) */ |
| 258 | const char *server_addr; /* address of the server (client only) */ |
| 259 | int server_port; /* port on which the ssl service runs */ |
| 260 | int debug_level; /* level of debugging */ |
| 261 | int nbio; /* should I/O be blocking? */ |
| 262 | const char *request_page; /* page on server to request */ |
| 263 | int request_size; /* pad request with header to requested size */ |
| 264 | const char *ca_file; /* the file with the CA certificate(s) */ |
| 265 | const char *ca_path; /* the path with the CA certificate(s) reside */ |
| 266 | const char *crt_file; /* the file with the client certificate */ |
| 267 | const char *key_file; /* the file with the client key */ |
| 268 | const char *psk; /* the pre-shared key */ |
| 269 | const char *psk_identity; /* the pre-shared key identity */ |
| 270 | int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */ |
| 271 | int renegotiation; /* enable / disable renegotiation */ |
| 272 | int allow_legacy; /* allow legacy renegotiation */ |
| 273 | int renegotiate; /* attempt renegotiation? */ |
| 274 | int renego_delay; /* delay before enforcing renegotiation */ |
| 275 | int exchanges; /* number of data exchanges */ |
| 276 | int min_version; /* minimum protocol version accepted */ |
| 277 | int max_version; /* maximum protocol version accepted */ |
| 278 | int arc4; /* flag for arc4 suites support */ |
| 279 | int auth_mode; /* verify mode for connection */ |
| 280 | unsigned char mfl_code; /* code for maximum fragment length */ |
| 281 | int trunc_hmac; /* negotiate truncated hmac or not */ |
| 282 | int recsplit; /* enable record splitting? */ |
| 283 | int reconnect; /* attempt to resume session */ |
| 284 | int reco_delay; /* delay in seconds before resuming session */ |
| 285 | int tickets; /* enable / disable session tickets */ |
| 286 | const char *alpn_string; /* ALPN supported protocols */ |
| 287 | int fallback; /* is this a fallback connection? */ |
| 288 | int extended_ms; /* negotiate extended master secret? */ |
| 289 | int etm; /* negotiate encrypt then mac? */ |
| 290 | } opt; |
| 291 | |
| 292 | static void my_debug( void *ctx, int level, const char *str ) |
| 293 | { |
| 294 | ((void) level); |
| 295 | |
| 296 | polarssl_fprintf( (FILE *) ctx, "%s", str ); |
| 297 | fflush( (FILE *) ctx ); |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Test recv/send functions that make sure each try returns |
| 302 | * WANT_READ/WANT_WRITE at least once before sucesseding |
| 303 | */ |
| 304 | static int my_recv( void *ctx, unsigned char *buf, size_t len ) |
| 305 | { |
| 306 | static int first_try = 1; |
| 307 | int ret; |
| 308 | |
| 309 | if( first_try ) |
| 310 | { |
| 311 | first_try = 0; |
| 312 | return( POLARSSL_ERR_NET_WANT_READ ); |
| 313 | } |
| 314 | |
| 315 | ret = net_recv( ctx, buf, len ); |
| 316 | if( ret != POLARSSL_ERR_NET_WANT_READ ) |
| 317 | first_try = 1; /* Next call will be a new operation */ |
| 318 | return( ret ); |
| 319 | } |
| 320 | |
| 321 | static int my_send( void *ctx, const unsigned char *buf, size_t len ) |
| 322 | { |
| 323 | static int first_try = 1; |
| 324 | int ret; |
| 325 | |
| 326 | if( first_try ) |
| 327 | { |
| 328 | first_try = 0; |
| 329 | return( POLARSSL_ERR_NET_WANT_WRITE ); |
| 330 | } |
| 331 | |
| 332 | ret = net_send( ctx, buf, len ); |
| 333 | if( ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 334 | first_try = 1; /* Next call will be a new operation */ |
| 335 | return( ret ); |
| 336 | } |
| 337 | |
| 338 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
| 339 | /* |
| 340 | * Enabled if debug_level > 1 in code below |
| 341 | */ |
| 342 | static int my_verify( void *data, x509_crt *crt, int depth, int *flags ) |
| 343 | { |
| 344 | char buf[1024]; |
| 345 | ((void) data); |
| 346 | |
| 347 | polarssl_printf( "\nVerify requested for (Depth %d):\n", depth ); |
| 348 | x509_crt_info( buf, sizeof( buf ) - 1, "", crt ); |
| 349 | polarssl_printf( "%s", buf ); |
| 350 | |
| 351 | if( ( (*flags) & BADCERT_EXPIRED ) != 0 ) |
| 352 | polarssl_printf( " ! server certificate has expired\n" ); |
| 353 | |
| 354 | if( ( (*flags) & BADCERT_REVOKED ) != 0 ) |
| 355 | polarssl_printf( " ! server certificate has been revoked\n" ); |
| 356 | |
| 357 | if( ( (*flags) & BADCERT_CN_MISMATCH ) != 0 ) |
| 358 | polarssl_printf( " ! CN mismatch\n" ); |
| 359 | |
| 360 | if( ( (*flags) & BADCERT_NOT_TRUSTED ) != 0 ) |
| 361 | polarssl_printf( " ! self-signed or not signed by a trusted CA\n" ); |
| 362 | |
| 363 | if( ( (*flags) & BADCRL_NOT_TRUSTED ) != 0 ) |
| 364 | polarssl_printf( " ! CRL not trusted\n" ); |
| 365 | |
| 366 | if( ( (*flags) & BADCRL_EXPIRED ) != 0 ) |
| 367 | polarssl_printf( " ! CRL expired\n" ); |
| 368 | |
| 369 | if( ( (*flags) & BADCERT_OTHER ) != 0 ) |
| 370 | polarssl_printf( " ! other (unknown) flag\n" ); |
| 371 | |
| 372 | if ( ( *flags ) == 0 ) |
| 373 | polarssl_printf( " This certificate has no flags\n" ); |
| 374 | |
| 375 | return( 0 ); |
| 376 | } |
| 377 | #endif /* POLARSSL_X509_CRT_PARSE_C */ |
| 378 | |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 379 | int main( int argc, char *argv[] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 380 | { |
Manuel Pégourié-Gonnard | dcab293 | 2014-08-14 17:47:17 +0200 | [diff] [blame] | 381 | int ret = 0, len, tail_len, server_fd, i, written, frags; |
Paul Bakker | 93c32b2 | 2014-04-25 13:40:05 +0200 | [diff] [blame] | 382 | unsigned char buf[SSL_MAX_CONTENT_LEN + 1]; |
Manuel Pégourié-Gonnard | 8a3c64d | 2013-10-14 19:54:10 +0200 | [diff] [blame] | 383 | #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Manuel Pégourié-Gonnard | 481fcfd | 2014-07-03 16:12:50 +0200 | [diff] [blame] | 384 | unsigned char psk[POLARSSL_PSK_MAX_LEN]; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 385 | size_t psk_len = 0; |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 386 | #endif |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 387 | #if defined(POLARSSL_SSL_ALPN) |
| 388 | const char *alpn_list[10]; |
| 389 | #endif |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 390 | const char *pers = "ssl_client2"; |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 391 | |
| 392 | entropy_context entropy; |
| 393 | ctr_drbg_context ctr_drbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 394 | ssl_context ssl; |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 395 | ssl_session saved_session; |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 396 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 397 | x509_crt cacert; |
| 398 | x509_crt clicert; |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 399 | pk_context pkey; |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 400 | #endif |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 401 | char *p, *q; |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 402 | const int *list; |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 403 | |
Paul Bakker | 1a207ec | 2011-02-06 13:22:40 +0000 | [diff] [blame] | 404 | /* |
| 405 | * Make sure memory references are valid. |
| 406 | */ |
| 407 | server_fd = 0; |
Paul Bakker | 1a207ec | 2011-02-06 13:22:40 +0000 | [diff] [blame] | 408 | memset( &ssl, 0, sizeof( ssl_context ) ); |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 409 | memset( &saved_session, 0, sizeof( ssl_session ) ); |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 410 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 411 | x509_crt_init( &cacert ); |
| 412 | x509_crt_init( &clicert ); |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 413 | pk_init( &pkey ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 414 | #endif |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 415 | #if defined(POLARSSL_SSL_ALPN) |
Paul Bakker | 525f875 | 2014-05-01 10:58:57 +0200 | [diff] [blame] | 416 | memset( (void * ) alpn_list, 0, sizeof( alpn_list ) ); |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 417 | #endif |
Paul Bakker | 1a207ec | 2011-02-06 13:22:40 +0000 | [diff] [blame] | 418 | |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 419 | if( argc == 0 ) |
| 420 | { |
| 421 | usage: |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 422 | if( ret == 0 ) |
| 423 | ret = 1; |
| 424 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 425 | polarssl_printf( USAGE ); |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 426 | |
| 427 | list = ssl_list_ciphersuites(); |
| 428 | while( *list ) |
| 429 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 430 | polarssl_printf(" %-42s", ssl_get_ciphersuite_name( *list ) ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 431 | list++; |
| 432 | if( !*list ) |
| 433 | break; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 434 | polarssl_printf(" %s\n", ssl_get_ciphersuite_name( *list ) ); |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 435 | list++; |
| 436 | } |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 437 | polarssl_printf("\n"); |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 438 | goto exit; |
| 439 | } |
| 440 | |
| 441 | opt.server_name = DFL_SERVER_NAME; |
Manuel Pégourié-Gonnard | 0d8780b | 2014-02-25 11:11:26 +0100 | [diff] [blame] | 442 | opt.server_addr = DFL_SERVER_ADDR; |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 443 | opt.server_port = DFL_SERVER_PORT; |
| 444 | opt.debug_level = DFL_DEBUG_LEVEL; |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 445 | opt.nbio = DFL_NBIO; |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 446 | opt.request_page = DFL_REQUEST_PAGE; |
Paul Bakker | 93c32b2 | 2014-04-25 13:40:05 +0200 | [diff] [blame] | 447 | opt.request_size = DFL_REQUEST_SIZE; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 448 | opt.ca_file = DFL_CA_FILE; |
Paul Bakker | 8d91458 | 2012-06-04 12:46:42 +0000 | [diff] [blame] | 449 | opt.ca_path = DFL_CA_PATH; |
Paul Bakker | 6796839 | 2010-07-18 08:28:20 +0000 | [diff] [blame] | 450 | opt.crt_file = DFL_CRT_FILE; |
| 451 | opt.key_file = DFL_KEY_FILE; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 452 | opt.psk = DFL_PSK; |
| 453 | opt.psk_identity = DFL_PSK_IDENTITY; |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 454 | opt.force_ciphersuite[0]= DFL_FORCE_CIPHER; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 455 | opt.renegotiation = DFL_RENEGOTIATION; |
| 456 | opt.allow_legacy = DFL_ALLOW_LEGACY; |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 457 | opt.renegotiate = DFL_RENEGOTIATE; |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 458 | opt.exchanges = DFL_EXCHANGES; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 459 | opt.min_version = DFL_MIN_VERSION; |
| 460 | opt.max_version = DFL_MAX_VERSION; |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 461 | opt.arc4 = DFL_ARC4; |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 462 | opt.auth_mode = DFL_AUTH_MODE; |
Manuel Pégourié-Gonnard | 0df6b1f | 2013-07-16 13:39:57 +0200 | [diff] [blame] | 463 | opt.mfl_code = DFL_MFL_CODE; |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 464 | opt.trunc_hmac = DFL_TRUNC_HMAC; |
Manuel Pégourié-Gonnard | c82ee35 | 2015-01-07 16:35:25 +0100 | [diff] [blame] | 465 | opt.recsplit = DFL_RECSPLIT; |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 466 | opt.reconnect = DFL_RECONNECT; |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 467 | opt.reco_delay = DFL_RECO_DELAY; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 468 | opt.tickets = DFL_TICKETS; |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 469 | opt.alpn_string = DFL_ALPN_STRING; |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 470 | opt.fallback = DFL_FALLBACK; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 471 | opt.extended_ms = DFL_EXTENDED_MS; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 472 | opt.etm = DFL_ETM; |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 473 | |
| 474 | for( i = 1; i < argc; i++ ) |
| 475 | { |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 476 | p = argv[i]; |
| 477 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 478 | goto usage; |
| 479 | *q++ = '\0'; |
| 480 | |
| 481 | if( strcmp( p, "server_name" ) == 0 ) |
| 482 | opt.server_name = q; |
Manuel Pégourié-Gonnard | 0d8780b | 2014-02-25 11:11:26 +0100 | [diff] [blame] | 483 | else if( strcmp( p, "server_addr" ) == 0 ) |
| 484 | opt.server_addr = q; |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 485 | else if( strcmp( p, "server_port" ) == 0 ) |
| 486 | { |
| 487 | opt.server_port = atoi( q ); |
| 488 | if( opt.server_port < 1 || opt.server_port > 65535 ) |
| 489 | goto usage; |
| 490 | } |
| 491 | else if( strcmp( p, "debug_level" ) == 0 ) |
| 492 | { |
| 493 | opt.debug_level = atoi( q ); |
| 494 | if( opt.debug_level < 0 || opt.debug_level > 65535 ) |
| 495 | goto usage; |
| 496 | } |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 497 | else if( strcmp( p, "nbio" ) == 0 ) |
| 498 | { |
| 499 | opt.nbio = atoi( q ); |
| 500 | if( opt.nbio < 0 || opt.nbio > 2 ) |
| 501 | goto usage; |
| 502 | } |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 503 | else if( strcmp( p, "request_page" ) == 0 ) |
| 504 | opt.request_page = q; |
Paul Bakker | 93c32b2 | 2014-04-25 13:40:05 +0200 | [diff] [blame] | 505 | else if( strcmp( p, "request_size" ) == 0 ) |
| 506 | { |
| 507 | opt.request_size = atoi( q ); |
| 508 | if( opt.request_size < 0 || opt.request_size > SSL_MAX_CONTENT_LEN ) |
| 509 | goto usage; |
| 510 | } |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 511 | else if( strcmp( p, "ca_file" ) == 0 ) |
| 512 | opt.ca_file = q; |
Paul Bakker | 8d91458 | 2012-06-04 12:46:42 +0000 | [diff] [blame] | 513 | else if( strcmp( p, "ca_path" ) == 0 ) |
| 514 | opt.ca_path = q; |
Paul Bakker | 6796839 | 2010-07-18 08:28:20 +0000 | [diff] [blame] | 515 | else if( strcmp( p, "crt_file" ) == 0 ) |
| 516 | opt.crt_file = q; |
| 517 | else if( strcmp( p, "key_file" ) == 0 ) |
| 518 | opt.key_file = q; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 519 | else if( strcmp( p, "psk" ) == 0 ) |
| 520 | opt.psk = q; |
| 521 | else if( strcmp( p, "psk_identity" ) == 0 ) |
| 522 | opt.psk_identity = q; |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 523 | else if( strcmp( p, "force_ciphersuite" ) == 0 ) |
| 524 | { |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 525 | opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q ); |
| 526 | |
Manuel Pégourié-Gonnard | 8de259b | 2014-06-11 14:19:06 +0200 | [diff] [blame] | 527 | if( opt.force_ciphersuite[0] == 0 ) |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 528 | { |
| 529 | ret = 2; |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 530 | goto usage; |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 531 | } |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 532 | opt.force_ciphersuite[1] = 0; |
| 533 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 534 | else if( strcmp( p, "renegotiation" ) == 0 ) |
| 535 | { |
| 536 | opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED : |
| 537 | SSL_RENEGOTIATION_DISABLED; |
| 538 | } |
| 539 | else if( strcmp( p, "allow_legacy" ) == 0 ) |
| 540 | { |
Manuel Pégourié-Gonnard | 85d915b | 2014-11-03 20:10:36 +0100 | [diff] [blame] | 541 | switch( atoi( q ) ) |
| 542 | { |
| 543 | case -1: opt.allow_legacy = SSL_LEGACY_BREAK_HANDSHAKE; break; |
| 544 | case 0: opt.allow_legacy = SSL_LEGACY_NO_RENEGOTIATION; break; |
| 545 | case 1: opt.allow_legacy = SSL_LEGACY_ALLOW_RENEGOTIATION; break; |
| 546 | default: goto usage; |
| 547 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 548 | } |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 549 | else if( strcmp( p, "renegotiate" ) == 0 ) |
| 550 | { |
| 551 | opt.renegotiate = atoi( q ); |
| 552 | if( opt.renegotiate < 0 || opt.renegotiate > 1 ) |
| 553 | goto usage; |
| 554 | } |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 555 | else if( strcmp( p, "exchanges" ) == 0 ) |
| 556 | { |
| 557 | opt.exchanges = atoi( q ); |
| 558 | if( opt.exchanges < 1 ) |
| 559 | goto usage; |
| 560 | } |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 561 | else if( strcmp( p, "reconnect" ) == 0 ) |
| 562 | { |
| 563 | opt.reconnect = atoi( q ); |
Manuel Pégourié-Gonnard | cf2e97e | 2013-08-02 15:04:36 +0200 | [diff] [blame] | 564 | if( opt.reconnect < 0 || opt.reconnect > 2 ) |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 565 | goto usage; |
| 566 | } |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 567 | else if( strcmp( p, "reco_delay" ) == 0 ) |
| 568 | { |
| 569 | opt.reco_delay = atoi( q ); |
| 570 | if( opt.reco_delay < 0 ) |
| 571 | goto usage; |
| 572 | } |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 573 | else if( strcmp( p, "tickets" ) == 0 ) |
| 574 | { |
| 575 | opt.tickets = atoi( q ); |
| 576 | if( opt.tickets < 0 || opt.tickets > 2 ) |
| 577 | goto usage; |
| 578 | } |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 579 | else if( strcmp( p, "alpn" ) == 0 ) |
| 580 | { |
| 581 | opt.alpn_string = q; |
| 582 | } |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 583 | else if( strcmp( p, "fallback" ) == 0 ) |
| 584 | { |
| 585 | switch( atoi( q ) ) |
| 586 | { |
| 587 | case 0: opt.fallback = SSL_IS_NOT_FALLBACK; break; |
| 588 | case 1: opt.fallback = SSL_IS_FALLBACK; break; |
| 589 | default: goto usage; |
| 590 | } |
| 591 | } |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 592 | else if( strcmp( p, "extended_ms" ) == 0 ) |
| 593 | { |
| 594 | switch( atoi( q ) ) |
| 595 | { |
| 596 | case 0: opt.extended_ms = SSL_EXTENDED_MS_DISABLED; break; |
| 597 | case 1: opt.extended_ms = SSL_EXTENDED_MS_ENABLED; break; |
| 598 | default: goto usage; |
| 599 | } |
| 600 | } |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 601 | else if( strcmp( p, "etm" ) == 0 ) |
| 602 | { |
| 603 | switch( atoi( q ) ) |
| 604 | { |
| 605 | case 0: opt.etm = SSL_ETM_DISABLED; break; |
| 606 | case 1: opt.etm = SSL_ETM_ENABLED; break; |
| 607 | default: goto usage; |
| 608 | } |
| 609 | } |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 610 | else if( strcmp( p, "min_version" ) == 0 ) |
| 611 | { |
| 612 | if( strcmp( q, "ssl3" ) == 0 ) |
| 613 | opt.min_version = SSL_MINOR_VERSION_0; |
| 614 | else if( strcmp( q, "tls1" ) == 0 ) |
| 615 | opt.min_version = SSL_MINOR_VERSION_1; |
| 616 | else if( strcmp( q, "tls1_1" ) == 0 ) |
| 617 | opt.min_version = SSL_MINOR_VERSION_2; |
| 618 | else if( strcmp( q, "tls1_2" ) == 0 ) |
| 619 | opt.min_version = SSL_MINOR_VERSION_3; |
| 620 | else |
| 621 | goto usage; |
| 622 | } |
| 623 | else if( strcmp( p, "max_version" ) == 0 ) |
| 624 | { |
| 625 | if( strcmp( q, "ssl3" ) == 0 ) |
| 626 | opt.max_version = SSL_MINOR_VERSION_0; |
| 627 | else if( strcmp( q, "tls1" ) == 0 ) |
| 628 | opt.max_version = SSL_MINOR_VERSION_1; |
| 629 | else if( strcmp( q, "tls1_1" ) == 0 ) |
| 630 | opt.max_version = SSL_MINOR_VERSION_2; |
| 631 | else if( strcmp( q, "tls1_2" ) == 0 ) |
| 632 | opt.max_version = SSL_MINOR_VERSION_3; |
| 633 | else |
| 634 | goto usage; |
| 635 | } |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 636 | else if( strcmp( p, "arc4" ) == 0 ) |
| 637 | { |
| 638 | switch( atoi( q ) ) |
| 639 | { |
| 640 | case 0: opt.arc4 = SSL_ARC4_DISABLED; break; |
| 641 | case 1: opt.arc4 = SSL_ARC4_ENABLED; break; |
| 642 | default: goto usage; |
| 643 | } |
| 644 | } |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 645 | else if( strcmp( p, "force_version" ) == 0 ) |
| 646 | { |
| 647 | if( strcmp( q, "ssl3" ) == 0 ) |
| 648 | { |
| 649 | opt.min_version = SSL_MINOR_VERSION_0; |
| 650 | opt.max_version = SSL_MINOR_VERSION_0; |
| 651 | } |
| 652 | else if( strcmp( q, "tls1" ) == 0 ) |
| 653 | { |
| 654 | opt.min_version = SSL_MINOR_VERSION_1; |
| 655 | opt.max_version = SSL_MINOR_VERSION_1; |
| 656 | } |
| 657 | else if( strcmp( q, "tls1_1" ) == 0 ) |
| 658 | { |
| 659 | opt.min_version = SSL_MINOR_VERSION_2; |
| 660 | opt.max_version = SSL_MINOR_VERSION_2; |
| 661 | } |
| 662 | else if( strcmp( q, "tls1_2" ) == 0 ) |
| 663 | { |
| 664 | opt.min_version = SSL_MINOR_VERSION_3; |
| 665 | opt.max_version = SSL_MINOR_VERSION_3; |
| 666 | } |
| 667 | else |
| 668 | goto usage; |
| 669 | } |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 670 | else if( strcmp( p, "auth_mode" ) == 0 ) |
| 671 | { |
| 672 | if( strcmp( q, "none" ) == 0 ) |
| 673 | opt.auth_mode = SSL_VERIFY_NONE; |
| 674 | else if( strcmp( q, "optional" ) == 0 ) |
| 675 | opt.auth_mode = SSL_VERIFY_OPTIONAL; |
| 676 | else if( strcmp( q, "required" ) == 0 ) |
| 677 | opt.auth_mode = SSL_VERIFY_REQUIRED; |
| 678 | else |
| 679 | goto usage; |
| 680 | } |
Manuel Pégourié-Gonnard | 0df6b1f | 2013-07-16 13:39:57 +0200 | [diff] [blame] | 681 | else if( strcmp( p, "max_frag_len" ) == 0 ) |
| 682 | { |
| 683 | if( strcmp( q, "512" ) == 0 ) |
| 684 | opt.mfl_code = SSL_MAX_FRAG_LEN_512; |
| 685 | else if( strcmp( q, "1024" ) == 0 ) |
| 686 | opt.mfl_code = SSL_MAX_FRAG_LEN_1024; |
| 687 | else if( strcmp( q, "2048" ) == 0 ) |
| 688 | opt.mfl_code = SSL_MAX_FRAG_LEN_2048; |
| 689 | else if( strcmp( q, "4096" ) == 0 ) |
| 690 | opt.mfl_code = SSL_MAX_FRAG_LEN_4096; |
| 691 | else |
| 692 | goto usage; |
| 693 | } |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 694 | else if( strcmp( p, "trunc_hmac" ) == 0 ) |
| 695 | { |
Manuel Pégourié-Gonnard | 265fe99 | 2015-01-09 12:43:35 +0100 | [diff] [blame] | 696 | switch( atoi( q ) ) |
| 697 | { |
| 698 | case 0: opt.trunc_hmac = SSL_TRUNC_HMAC_DISABLED; break; |
| 699 | case 1: opt.trunc_hmac = SSL_TRUNC_HMAC_ENABLED; break; |
| 700 | default: goto usage; |
| 701 | } |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 702 | } |
Manuel Pégourié-Gonnard | c82ee35 | 2015-01-07 16:35:25 +0100 | [diff] [blame] | 703 | else if( strcmp( p, "recsplit" ) == 0 ) |
| 704 | { |
| 705 | opt.recsplit = atoi( q ); |
| 706 | if( opt.recsplit < 0 || opt.recsplit > 1 ) |
| 707 | goto usage; |
| 708 | } |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 709 | else |
| 710 | goto usage; |
| 711 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 712 | |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 713 | #if defined(POLARSSL_DEBUG_C) |
| 714 | debug_set_threshold( opt.debug_level ); |
| 715 | #endif |
| 716 | |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 717 | if( opt.force_ciphersuite[0] > 0 ) |
| 718 | { |
| 719 | const ssl_ciphersuite_t *ciphersuite_info; |
| 720 | ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] ); |
| 721 | |
Paul Bakker | 66c4810 | 2013-07-26 14:05:32 +0200 | [diff] [blame] | 722 | if( opt.max_version != -1 && |
| 723 | ciphersuite_info->min_minor_ver > opt.max_version ) |
| 724 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 725 | polarssl_printf("forced ciphersuite not allowed with this protocol version\n"); |
Paul Bakker | 66c4810 | 2013-07-26 14:05:32 +0200 | [diff] [blame] | 726 | ret = 2; |
| 727 | goto usage; |
| 728 | } |
| 729 | if( opt.min_version != -1 && |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 730 | ciphersuite_info->max_minor_ver < opt.min_version ) |
| 731 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 732 | polarssl_printf("forced ciphersuite not allowed with this protocol version\n"); |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 733 | ret = 2; |
| 734 | goto usage; |
| 735 | } |
Paul Bakker | 66c4810 | 2013-07-26 14:05:32 +0200 | [diff] [blame] | 736 | if( opt.max_version > ciphersuite_info->max_minor_ver ) |
| 737 | opt.max_version = ciphersuite_info->max_minor_ver; |
| 738 | if( opt.min_version < ciphersuite_info->min_minor_ver ) |
| 739 | opt.min_version = ciphersuite_info->min_minor_ver; |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 740 | } |
| 741 | |
Manuel Pégourié-Gonnard | 8a3c64d | 2013-10-14 19:54:10 +0200 | [diff] [blame] | 742 | #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 743 | /* |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 744 | * Unhexify the pre-shared key if any is given |
| 745 | */ |
| 746 | if( strlen( opt.psk ) ) |
| 747 | { |
| 748 | unsigned char c; |
| 749 | size_t j; |
| 750 | |
| 751 | if( strlen( opt.psk ) % 2 != 0 ) |
| 752 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 753 | polarssl_printf("pre-shared key not valid hex\n"); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 754 | goto exit; |
| 755 | } |
| 756 | |
| 757 | psk_len = strlen( opt.psk ) / 2; |
| 758 | |
| 759 | for( j = 0; j < strlen( opt.psk ); j += 2 ) |
| 760 | { |
| 761 | c = opt.psk[j]; |
| 762 | if( c >= '0' && c <= '9' ) |
| 763 | c -= '0'; |
| 764 | else if( c >= 'a' && c <= 'f' ) |
| 765 | c -= 'a' - 10; |
| 766 | else if( c >= 'A' && c <= 'F' ) |
| 767 | c -= 'A' - 10; |
| 768 | else |
| 769 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 770 | polarssl_printf("pre-shared key not valid hex\n"); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 771 | goto exit; |
| 772 | } |
| 773 | psk[ j / 2 ] = c << 4; |
| 774 | |
| 775 | c = opt.psk[j + 1]; |
| 776 | if( c >= '0' && c <= '9' ) |
| 777 | c -= '0'; |
| 778 | else if( c >= 'a' && c <= 'f' ) |
| 779 | c -= 'a' - 10; |
| 780 | else if( c >= 'A' && c <= 'F' ) |
| 781 | c -= 'A' - 10; |
| 782 | else |
| 783 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 784 | polarssl_printf("pre-shared key not valid hex\n"); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 785 | goto exit; |
| 786 | } |
| 787 | psk[ j / 2 ] |= c; |
| 788 | } |
| 789 | } |
Manuel Pégourié-Gonnard | 8a3c64d | 2013-10-14 19:54:10 +0200 | [diff] [blame] | 790 | #endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 791 | |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 792 | #if defined(POLARSSL_SSL_ALPN) |
| 793 | if( opt.alpn_string != NULL ) |
| 794 | { |
| 795 | p = (char *) opt.alpn_string; |
| 796 | i = 0; |
| 797 | |
| 798 | /* Leave room for a final NULL in alpn_list */ |
| 799 | while( i < (int) sizeof alpn_list - 1 && *p != '\0' ) |
| 800 | { |
| 801 | alpn_list[i++] = p; |
| 802 | |
| 803 | /* Terminate the current string and move on to next one */ |
| 804 | while( *p != ',' && *p != '\0' ) |
| 805 | p++; |
| 806 | if( *p == ',' ) |
| 807 | *p++ = '\0'; |
| 808 | } |
| 809 | } |
| 810 | #endif /* POLARSSL_SSL_ALPN */ |
| 811 | |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 812 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 813 | * 0. Initialize the RNG and the session data |
| 814 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 815 | polarssl_printf( "\n . Seeding the random number generator..." ); |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 816 | fflush( stdout ); |
| 817 | |
| 818 | entropy_init( &entropy ); |
| 819 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 820 | (const unsigned char *) pers, |
| 821 | strlen( pers ) ) ) != 0 ) |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 822 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 823 | polarssl_printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret ); |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 824 | goto exit; |
| 825 | } |
| 826 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 827 | polarssl_printf( " ok\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 828 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 829 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 830 | /* |
| 831 | * 1.1. Load the trusted CA |
| 832 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 833 | polarssl_printf( " . Loading the CA root certificate ..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 834 | fflush( stdout ); |
| 835 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 836 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 8d91458 | 2012-06-04 12:46:42 +0000 | [diff] [blame] | 837 | if( strlen( opt.ca_path ) ) |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 838 | if( strcmp( opt.ca_path, "none" ) == 0 ) |
| 839 | ret = 0; |
| 840 | else |
| 841 | ret = x509_crt_parse_path( &cacert, opt.ca_path ); |
Paul Bakker | 8d91458 | 2012-06-04 12:46:42 +0000 | [diff] [blame] | 842 | else if( strlen( opt.ca_file ) ) |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 843 | if( strcmp( opt.ca_file, "none" ) == 0 ) |
| 844 | ret = 0; |
| 845 | else |
| 846 | ret = x509_crt_parse_file( &cacert, opt.ca_file ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 847 | else |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 848 | #endif |
| 849 | #if defined(POLARSSL_CERTS_C) |
Manuel Pégourié-Gonnard | 641de71 | 2013-09-25 13:23:33 +0200 | [diff] [blame] | 850 | ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list, |
| 851 | strlen( test_ca_list ) ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 852 | #else |
| 853 | { |
| 854 | ret = 1; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 855 | polarssl_printf("POLARSSL_CERTS_C not defined."); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 856 | } |
| 857 | #endif |
Paul Bakker | 4248823 | 2012-05-16 08:21:05 +0000 | [diff] [blame] | 858 | if( ret < 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 859 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 860 | polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 861 | goto exit; |
| 862 | } |
| 863 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 864 | polarssl_printf( " ok (%d skipped)\n", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 865 | |
| 866 | /* |
| 867 | * 1.2. Load own certificate and private key |
| 868 | * |
| 869 | * (can be skipped if client authentication is not required) |
| 870 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 871 | polarssl_printf( " . Loading the client cert. and key..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 872 | fflush( stdout ); |
| 873 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 874 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 6796839 | 2010-07-18 08:28:20 +0000 | [diff] [blame] | 875 | if( strlen( opt.crt_file ) ) |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 876 | if( strcmp( opt.crt_file, "none" ) == 0 ) |
| 877 | ret = 0; |
| 878 | else |
| 879 | ret = x509_crt_parse_file( &clicert, opt.crt_file ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 880 | else |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 881 | #endif |
| 882 | #if defined(POLARSSL_CERTS_C) |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 883 | ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 884 | strlen( test_cli_crt ) ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 885 | #else |
| 886 | { |
| 887 | ret = 1; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 888 | polarssl_printf("POLARSSL_CERTS_C not defined."); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 889 | } |
| 890 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 891 | if( ret != 0 ) |
| 892 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 893 | polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 894 | goto exit; |
| 895 | } |
| 896 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 897 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 6796839 | 2010-07-18 08:28:20 +0000 | [diff] [blame] | 898 | if( strlen( opt.key_file ) ) |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 899 | if( strcmp( opt.key_file, "none" ) == 0 ) |
| 900 | ret = 0; |
| 901 | else |
| 902 | ret = pk_parse_keyfile( &pkey, opt.key_file, "" ); |
Paul Bakker | 6796839 | 2010-07-18 08:28:20 +0000 | [diff] [blame] | 903 | else |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 904 | #endif |
| 905 | #if defined(POLARSSL_CERTS_C) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 906 | ret = pk_parse_key( &pkey, (const unsigned char *) test_cli_key, |
Paul Bakker | 6796839 | 2010-07-18 08:28:20 +0000 | [diff] [blame] | 907 | strlen( test_cli_key ), NULL, 0 ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 908 | #else |
| 909 | { |
| 910 | ret = 1; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 911 | polarssl_printf("POLARSSL_CERTS_C not defined."); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 912 | } |
| 913 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 914 | if( ret != 0 ) |
| 915 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 916 | polarssl_printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 917 | goto exit; |
| 918 | } |
| 919 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 920 | polarssl_printf( " ok\n" ); |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 921 | #endif /* POLARSSL_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 922 | |
| 923 | /* |
| 924 | * 2. Start the connection |
| 925 | */ |
Manuel Pégourié-Gonnard | 0d8780b | 2014-02-25 11:11:26 +0100 | [diff] [blame] | 926 | if( opt.server_addr == NULL) |
| 927 | opt.server_addr = opt.server_name; |
| 928 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 929 | polarssl_printf( " . Connecting to tcp/%s/%-4d...", opt.server_addr, |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 930 | opt.server_port ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 931 | fflush( stdout ); |
| 932 | |
Manuel Pégourié-Gonnard | 0d8780b | 2014-02-25 11:11:26 +0100 | [diff] [blame] | 933 | if( ( ret = net_connect( &server_fd, opt.server_addr, |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 934 | opt.server_port ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 935 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 936 | polarssl_printf( " failed\n ! net_connect returned -0x%x\n\n", -ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 937 | goto exit; |
| 938 | } |
| 939 | |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 940 | if( opt.nbio > 0 ) |
| 941 | ret = net_set_nonblock( server_fd ); |
| 942 | else |
| 943 | ret = net_set_block( server_fd ); |
| 944 | if( ret != 0 ) |
| 945 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 946 | polarssl_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] | 947 | goto exit; |
| 948 | } |
| 949 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 950 | polarssl_printf( " ok\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 951 | |
| 952 | /* |
| 953 | * 3. Setup stuff |
| 954 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 955 | polarssl_printf( " . Setting up the SSL/TLS structure..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 956 | fflush( stdout ); |
| 957 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 958 | if( ( ret = ssl_init( &ssl ) ) != 0 ) |
| 959 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 960 | polarssl_printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 961 | goto exit; |
| 962 | } |
| 963 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 964 | polarssl_printf( " ok\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 965 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 966 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | 915275b | 2012-09-28 07:10:55 +0000 | [diff] [blame] | 967 | if( opt.debug_level > 0 ) |
| 968 | ssl_set_verify( &ssl, my_verify, NULL ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 969 | #endif |
Paul Bakker | 915275b | 2012-09-28 07:10:55 +0000 | [diff] [blame] | 970 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 971 | ssl_set_endpoint( &ssl, SSL_IS_CLIENT ); |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 972 | ssl_set_authmode( &ssl, opt.auth_mode ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 973 | |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 974 | #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 975 | if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 ) |
| 976 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 977 | polarssl_printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 978 | goto exit; |
| 979 | } |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 980 | #endif |
Manuel Pégourié-Gonnard | 0df6b1f | 2013-07-16 13:39:57 +0200 | [diff] [blame] | 981 | |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 982 | #if defined(POLARSSL_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 265fe99 | 2015-01-09 12:43:35 +0100 | [diff] [blame] | 983 | if( opt.trunc_hmac != DFL_TRUNC_HMAC ) |
| 984 | ssl_set_truncated_hmac( &ssl, opt.trunc_hmac ); |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 985 | #endif |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 986 | |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 987 | #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) |
| 988 | if( opt.extended_ms != DFL_EXTENDED_MS ) |
| 989 | ssl_set_extended_master_secret( &ssl, opt.extended_ms ); |
| 990 | #endif |
| 991 | |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 992 | #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) |
| 993 | if( opt.etm != DFL_ETM ) |
| 994 | ssl_set_encrypt_then_mac( &ssl, opt.etm ); |
| 995 | #endif |
| 996 | |
Manuel Pégourié-Gonnard | c82ee35 | 2015-01-07 16:35:25 +0100 | [diff] [blame] | 997 | #if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING) |
| 998 | if( opt.recsplit != DFL_RECSPLIT ) |
| 999 | ssl_set_cbc_record_splitting( &ssl, opt.recsplit |
| 1000 | ? SSL_CBC_RECORD_SPLITTING_ENABLED |
| 1001 | : SSL_CBC_RECORD_SPLITTING_DISABLED ); |
| 1002 | #endif |
| 1003 | |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1004 | #if defined(POLARSSL_SSL_ALPN) |
| 1005 | if( opt.alpn_string != NULL ) |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1006 | if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 ) |
| 1007 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1008 | polarssl_printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1009 | goto exit; |
| 1010 | } |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1011 | #endif |
| 1012 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 1013 | ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg ); |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 1014 | ssl_set_dbg( &ssl, my_debug, stdout ); |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 1015 | |
| 1016 | if( opt.nbio == 2 ) |
| 1017 | ssl_set_bio( &ssl, my_recv, &server_fd, my_send, &server_fd ); |
| 1018 | else |
| 1019 | ssl_set_bio( &ssl, net_recv, &server_fd, net_send, &server_fd ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1020 | |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 1021 | #if defined(POLARSSL_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1022 | if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 ) |
| 1023 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1024 | polarssl_printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1025 | goto exit; |
| 1026 | } |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 1027 | #endif |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 1028 | |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 1029 | /* RC4 setting is redundant if we use only one ciphersuite */ |
Paul Bakker | 645ce3a | 2012-10-31 12:32:41 +0000 | [diff] [blame] | 1030 | if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER ) |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 1031 | ssl_set_ciphersuites( &ssl, opt.force_ciphersuite ); |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 1032 | else |
| 1033 | ssl_set_arc4_support( &ssl, opt.arc4 ); |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 1034 | |
Manuel Pégourié-Gonnard | 85d915b | 2014-11-03 20:10:36 +0100 | [diff] [blame] | 1035 | if( opt.allow_legacy != DFL_ALLOW_LEGACY ) |
| 1036 | ssl_legacy_renegotiation( &ssl, opt.allow_legacy ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1037 | #if defined(POLARSSL_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1038 | ssl_set_renegotiation( &ssl, opt.renegotiation ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1039 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1040 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 1041 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1042 | if( strcmp( opt.ca_path, "none" ) != 0 && |
| 1043 | strcmp( opt.ca_file, "none" ) != 0 ) |
| 1044 | { |
| 1045 | ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name ); |
| 1046 | } |
| 1047 | if( strcmp( opt.crt_file, "none" ) != 0 && |
| 1048 | strcmp( opt.key_file, "none" ) != 0 ) |
| 1049 | { |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1050 | if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 ) |
| 1051 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1052 | polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1053 | goto exit; |
| 1054 | } |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1055 | } |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 1056 | #endif |
| 1057 | |
Manuel Pégourié-Gonnard | 8a3c64d | 2013-10-14 19:54:10 +0200 | [diff] [blame] | 1058 | #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1059 | if( ( ret = ssl_set_psk( &ssl, psk, psk_len, |
| 1060 | (const unsigned char *) opt.psk_identity, |
| 1061 | strlen( opt.psk_identity ) ) ) != 0 ) |
| 1062 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1063 | polarssl_printf( " failed\n ! ssl_set_psk returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1064 | goto exit; |
| 1065 | } |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 1066 | #endif |
| 1067 | |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 1068 | #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1069 | if( ( ret = ssl_set_hostname( &ssl, opt.server_name ) ) != 0 ) |
| 1070 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1071 | polarssl_printf( " failed\n ! ssl_set_hostname returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1072 | goto exit; |
| 1073 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 1074 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1075 | |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1076 | if( opt.min_version != -1 ) |
| 1077 | ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version ); |
| 1078 | if( opt.max_version != -1 ) |
| 1079 | ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version ); |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 1080 | #if defined(POLARSSL_SSL_FALLBACK_SCSV) |
| 1081 | if( opt.fallback != DFL_FALLBACK ) |
| 1082 | ssl_set_fallback( &ssl, opt.fallback ); |
| 1083 | #endif |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1084 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1085 | /* |
| 1086 | * 4. Handshake |
| 1087 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1088 | polarssl_printf( " . Performing the SSL/TLS handshake..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1089 | fflush( stdout ); |
| 1090 | |
| 1091 | while( ( ret = ssl_handshake( &ssl ) ) != 0 ) |
| 1092 | { |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 1093 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1094 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1095 | polarssl_printf( " failed\n ! ssl_handshake returned -0x%x\n", -ret ); |
Manuel Pégourié-Gonnard | fcf2fc2 | 2014-03-11 11:10:27 +0100 | [diff] [blame] | 1096 | if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1097 | polarssl_printf( |
Manuel Pégourié-Gonnard | fcf2fc2 | 2014-03-11 11:10:27 +0100 | [diff] [blame] | 1098 | " Unable to verify the server's certificate. " |
| 1099 | "Either it is invalid,\n" |
| 1100 | " or you didn't set ca_file or ca_path " |
| 1101 | "to an appropriate value.\n" |
| 1102 | " Alternatively, you may want to use " |
| 1103 | "auth_mode=optional for testing purposes.\n" ); |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1104 | polarssl_printf( "\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1105 | goto exit; |
| 1106 | } |
| 1107 | } |
| 1108 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1109 | polarssl_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n", |
Manuel Pégourié-Gonnard | c580a00 | 2014-02-12 10:15:30 +0100 | [diff] [blame] | 1110 | ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1111 | |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1112 | #if defined(POLARSSL_SSL_ALPN) |
| 1113 | if( opt.alpn_string != NULL ) |
| 1114 | { |
| 1115 | const char *alp = ssl_get_alpn_protocol( &ssl ); |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1116 | polarssl_printf( " [ Application Layer Protocol is %s ]\n", |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1117 | alp ? alp : "(none)" ); |
| 1118 | } |
| 1119 | #endif |
| 1120 | |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 1121 | if( opt.reconnect != 0 ) |
| 1122 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1123 | polarssl_printf(" . Saving session for reuse..." ); |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 1124 | fflush( stdout ); |
| 1125 | |
| 1126 | if( ( ret = ssl_get_session( &ssl, &saved_session ) ) != 0 ) |
| 1127 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1128 | polarssl_printf( " failed\n ! ssl_get_session returned -0x%x\n\n", -ret ); |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 1129 | goto exit; |
| 1130 | } |
| 1131 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1132 | polarssl_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 1133 | } |
| 1134 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 1135 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1136 | /* |
| 1137 | * 5. Verify the server certificate |
| 1138 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1139 | polarssl_printf( " . Verifying peer X.509 certificate..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1140 | |
| 1141 | if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 ) |
| 1142 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1143 | polarssl_printf( " failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1144 | |
| 1145 | if( ( ret & BADCERT_EXPIRED ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1146 | polarssl_printf( " ! server certificate has expired\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1147 | |
| 1148 | if( ( ret & BADCERT_REVOKED ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1149 | polarssl_printf( " ! server certificate has been revoked\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1150 | |
| 1151 | if( ( ret & BADCERT_CN_MISMATCH ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1152 | polarssl_printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1153 | |
| 1154 | if( ( ret & BADCERT_NOT_TRUSTED ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1155 | polarssl_printf( " ! self-signed or not signed by a trusted CA\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1156 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1157 | polarssl_printf( "\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1158 | } |
| 1159 | else |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1160 | polarssl_printf( " ok\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1161 | |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1162 | if( ssl_get_peer_cert( &ssl ) != NULL ) |
| 1163 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1164 | polarssl_printf( " . Peer certificate information ...\n" ); |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 1165 | x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ", |
| 1166 | ssl_get_peer_cert( &ssl ) ); |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1167 | polarssl_printf( "%s\n", buf ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1168 | } |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 1169 | #endif /* POLARSSL_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1170 | |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1171 | #if defined(POLARSSL_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 1172 | if( opt.renegotiate ) |
Manuel Pégourié-Gonnard | 53b3e06 | 2013-10-29 18:16:38 +0100 | [diff] [blame] | 1173 | { |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 1174 | /* |
| 1175 | * Perform renegotiation (this must be done when the server is waiting |
| 1176 | * for input from our side). |
| 1177 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1178 | polarssl_printf( " . Performing renegotiation..." ); |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 1179 | fflush( stdout ); |
| 1180 | while( ( ret = ssl_renegotiate( &ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 53b3e06 | 2013-10-29 18:16:38 +0100 | [diff] [blame] | 1181 | { |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 1182 | if( ret != POLARSSL_ERR_NET_WANT_READ && |
| 1183 | ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 1184 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1185 | polarssl_printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 1186 | goto exit; |
| 1187 | } |
Manuel Pégourié-Gonnard | 53b3e06 | 2013-10-29 18:16:38 +0100 | [diff] [blame] | 1188 | } |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1189 | polarssl_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | 53b3e06 | 2013-10-29 18:16:38 +0100 | [diff] [blame] | 1190 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1191 | #endif /* POLARSSL_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 53b3e06 | 2013-10-29 18:16:38 +0100 | [diff] [blame] | 1192 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1193 | /* |
| 1194 | * 6. Write the GET request |
| 1195 | */ |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 1196 | send_request: |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1197 | polarssl_printf( " > Write to server:" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1198 | fflush( stdout ); |
| 1199 | |
Manuel Pégourié-Gonnard | dcab293 | 2014-08-14 17:47:17 +0200 | [diff] [blame] | 1200 | len = snprintf( (char *) buf, sizeof(buf) - 1, GET_REQUEST, |
| 1201 | opt.request_page ); |
| 1202 | tail_len = strlen( GET_REQUEST_END ); |
| 1203 | |
| 1204 | /* Add padding to GET request to reach opt.request_size in length */ |
| 1205 | if( opt.request_size != DFL_REQUEST_SIZE && |
| 1206 | len + tail_len < opt.request_size ) |
Paul Bakker | 93c32b2 | 2014-04-25 13:40:05 +0200 | [diff] [blame] | 1207 | { |
Manuel Pégourié-Gonnard | dcab293 | 2014-08-14 17:47:17 +0200 | [diff] [blame] | 1208 | memset( buf + len, 'A', opt.request_size - len - tail_len ); |
| 1209 | len += opt.request_size - len - tail_len; |
Paul Bakker | 93c32b2 | 2014-04-25 13:40:05 +0200 | [diff] [blame] | 1210 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1211 | |
Manuel Pégourié-Gonnard | dcab293 | 2014-08-14 17:47:17 +0200 | [diff] [blame] | 1212 | strncpy( (char *) buf + len, GET_REQUEST_END, sizeof(buf) - len - 1 ); |
| 1213 | len += tail_len; |
| 1214 | |
Manuel Pégourié-Gonnard | dea29c5 | 2014-06-18 13:07:56 +0200 | [diff] [blame] | 1215 | /* Truncate if request size is smaller than the "natural" size */ |
| 1216 | if( opt.request_size != DFL_REQUEST_SIZE && |
| 1217 | len > opt.request_size ) |
| 1218 | { |
| 1219 | len = opt.request_size; |
| 1220 | |
| 1221 | /* Still end with \r\n unless that's really not possible */ |
| 1222 | if( len >= 2 ) buf[len - 2] = '\r'; |
| 1223 | if( len >= 1 ) buf[len - 1] = '\n'; |
| 1224 | } |
| 1225 | |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 1226 | for( written = 0, frags = 0; written < len; written += ret, frags++ ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1227 | { |
Manuel Pégourié-Gonnard | 787b658 | 2013-07-16 15:43:17 +0200 | [diff] [blame] | 1228 | while( ( ret = ssl_write( &ssl, buf + written, len - written ) ) <= 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1229 | { |
Manuel Pégourié-Gonnard | 787b658 | 2013-07-16 15:43:17 +0200 | [diff] [blame] | 1230 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 1231 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1232 | polarssl_printf( " failed\n ! ssl_write returned -0x%x\n\n", -ret ); |
Manuel Pégourié-Gonnard | 787b658 | 2013-07-16 15:43:17 +0200 | [diff] [blame] | 1233 | goto exit; |
| 1234 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1235 | } |
| 1236 | } |
| 1237 | |
Manuel Pégourié-Gonnard | 787b658 | 2013-07-16 15:43:17 +0200 | [diff] [blame] | 1238 | buf[written] = '\0'; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1239 | polarssl_printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1240 | |
| 1241 | /* |
| 1242 | * 7. Read the HTTP response |
| 1243 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1244 | polarssl_printf( " < Read from server:" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1245 | fflush( stdout ); |
| 1246 | |
| 1247 | do |
| 1248 | { |
| 1249 | len = sizeof( buf ) - 1; |
| 1250 | memset( buf, 0, sizeof( buf ) ); |
| 1251 | ret = ssl_read( &ssl, buf, len ); |
| 1252 | |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 1253 | if( ret == POLARSSL_ERR_NET_WANT_READ || |
| 1254 | ret == POLARSSL_ERR_NET_WANT_WRITE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1255 | continue; |
| 1256 | |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 1257 | if( ret <= 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1258 | { |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 1259 | switch( ret ) |
| 1260 | { |
| 1261 | case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY: |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1262 | polarssl_printf( " connection was closed gracefully\n" ); |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 1263 | ret = 0; |
Manuel Pégourié-Gonnard | f138874 | 2014-08-19 16:14:36 +0200 | [diff] [blame] | 1264 | goto close_notify; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1265 | |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 1266 | case 0: |
| 1267 | case POLARSSL_ERR_NET_CONN_RESET: |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1268 | polarssl_printf( " connection was reset by peer\n" ); |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 1269 | ret = 0; |
| 1270 | goto reconnect; |
| 1271 | |
| 1272 | default: |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1273 | polarssl_printf( " ssl_read returned -0x%x\n", -ret ); |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 1274 | goto exit; |
| 1275 | } |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1278 | len = ret; |
Paul Bakker | 93c32b2 | 2014-04-25 13:40:05 +0200 | [diff] [blame] | 1279 | buf[len] = '\0'; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1280 | polarssl_printf( " %d bytes read\n\n%s", len, (char *) buf ); |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 1281 | |
| 1282 | /* End of message should be detected according to the syntax of the |
| 1283 | * application protocol (eg HTTP), just use a dummy test here. */ |
| 1284 | if( ret > 0 && buf[len-1] == '\n' ) |
| 1285 | { |
| 1286 | ret = 0; |
| 1287 | break; |
| 1288 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1289 | } |
Paul Bakker | f357131 | 2011-05-20 12:32:35 +0000 | [diff] [blame] | 1290 | while( 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1291 | |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 1292 | /* |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 1293 | * 7b. Continue doing data exchanges? |
| 1294 | */ |
| 1295 | if( --opt.exchanges > 0 ) |
| 1296 | goto send_request; |
| 1297 | |
| 1298 | /* |
Manuel Pégourié-Gonnard | f138874 | 2014-08-19 16:14:36 +0200 | [diff] [blame] | 1299 | * 8. Done, cleanly close the connection |
| 1300 | */ |
| 1301 | close_notify: |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1302 | polarssl_printf( " . Closing the connection..." ); |
Manuel Pégourié-Gonnard | f138874 | 2014-08-19 16:14:36 +0200 | [diff] [blame] | 1303 | |
Manuel Pégourié-Gonnard | 34377b1 | 2015-01-22 10:46:46 +0000 | [diff] [blame] | 1304 | /* No error checking, the connection might be closed already */ |
| 1305 | do ret = ssl_close_notify( &ssl ); |
| 1306 | while( ret == POLARSSL_ERR_NET_WANT_WRITE ); |
| 1307 | ret = 0; |
Manuel Pégourié-Gonnard | f138874 | 2014-08-19 16:14:36 +0200 | [diff] [blame] | 1308 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1309 | polarssl_printf( " done\n" ); |
Manuel Pégourié-Gonnard | f138874 | 2014-08-19 16:14:36 +0200 | [diff] [blame] | 1310 | |
| 1311 | /* |
| 1312 | * 9. Reconnect? |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 1313 | */ |
| 1314 | reconnect: |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 1315 | if( opt.reconnect != 0 ) |
| 1316 | { |
Manuel Pégourié-Gonnard | cf2e97e | 2013-08-02 15:04:36 +0200 | [diff] [blame] | 1317 | --opt.reconnect; |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 1318 | |
Manuel Pégourié-Gonnard | 6b0d268 | 2014-03-25 11:24:43 +0100 | [diff] [blame] | 1319 | net_close( server_fd ); |
| 1320 | |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 1321 | #if defined(POLARSSL_TIMING_C) |
| 1322 | if( opt.reco_delay > 0 ) |
| 1323 | m_sleep( 1000 * opt.reco_delay ); |
| 1324 | #endif |
Manuel Pégourié-Gonnard | 38d1eba | 2013-08-23 10:44:29 +0200 | [diff] [blame] | 1325 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1326 | polarssl_printf( " . Reconnecting with saved session..." ); |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 1327 | fflush( stdout ); |
| 1328 | |
| 1329 | if( ( ret = ssl_session_reset( &ssl ) ) != 0 ) |
| 1330 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1331 | polarssl_printf( " failed\n ! ssl_session_reset returned -0x%x\n\n", -ret ); |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 1332 | goto exit; |
| 1333 | } |
| 1334 | |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1335 | if( ( ret = ssl_set_session( &ssl, &saved_session ) ) != 0 ) |
| 1336 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1337 | polarssl_printf( " failed\n ! ssl_set_session returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1338 | goto exit; |
| 1339 | } |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 1340 | |
Manuel Pégourié-Gonnard | d3b90f7 | 2014-11-24 11:54:02 +0100 | [diff] [blame] | 1341 | if( ( ret = net_connect( &server_fd, opt.server_addr, |
| 1342 | opt.server_port ) ) != 0 ) |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 1343 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1344 | polarssl_printf( " failed\n ! net_connect returned -0x%x\n\n", -ret ); |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 1345 | goto exit; |
| 1346 | } |
| 1347 | |
| 1348 | while( ( ret = ssl_handshake( &ssl ) ) != 0 ) |
| 1349 | { |
| 1350 | if( ret != POLARSSL_ERR_NET_WANT_READ && |
| 1351 | ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 1352 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1353 | polarssl_printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret ); |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 1354 | goto exit; |
| 1355 | } |
| 1356 | } |
| 1357 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1358 | polarssl_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 1359 | |
| 1360 | goto send_request; |
| 1361 | } |
| 1362 | |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 1363 | /* |
| 1364 | * Cleanup and exit |
| 1365 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1366 | exit: |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1367 | #ifdef POLARSSL_ERROR_C |
| 1368 | if( ret != 0 ) |
| 1369 | { |
| 1370 | char error_buf[100]; |
Paul Bakker | 03a8a79 | 2013-06-30 12:18:08 +0200 | [diff] [blame] | 1371 | polarssl_strerror( ret, error_buf, 100 ); |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1372 | polarssl_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1373 | } |
| 1374 | #endif |
| 1375 | |
Paul Bakker | 1a207ec | 2011-02-06 13:22:40 +0000 | [diff] [blame] | 1376 | if( server_fd ) |
| 1377 | net_close( server_fd ); |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 1378 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 1379 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
| 1380 | x509_crt_free( &clicert ); |
| 1381 | x509_crt_free( &cacert ); |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 1382 | pk_free( &pkey ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 1383 | #endif |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1384 | ssl_session_free( &saved_session ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1385 | ssl_free( &ssl ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 1386 | ctr_drbg_free( &ctr_drbg ); |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 1387 | entropy_free( &entropy ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1388 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 1389 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1390 | polarssl_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1391 | fflush( stdout ); getchar(); |
| 1392 | #endif |
| 1393 | |
Paul Bakker | dbd79ca | 2013-07-24 16:28:35 +0200 | [diff] [blame] | 1394 | // Shell can not handle large exit numbers -> 1 for errors |
| 1395 | if( ret < 0 ) |
| 1396 | ret = 1; |
| 1397 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1398 | return( ret ); |
| 1399 | } |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 1400 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C && |
| 1401 | POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C && |
| 1402 | POLARSSL_CTR_DRBG_C */ |