Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSL client with options |
| 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 | b60b95f | 2012-09-25 09:05:17 +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 | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +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 | b60b95f | 2012-09-25 09:05:17 +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 |
| 32 | #define polarssl_printf printf |
| 33 | #define polarssl_fprintf fprintf |
| 34 | #define polarssl_malloc malloc |
| 35 | #define polarssl_free free |
| 36 | #endif |
| 37 | |
Manuel Pégourié-Gonnard | 8a4d571 | 2014-06-24 14:19:59 +0200 | [diff] [blame] | 38 | #if !defined(POLARSSL_ENTROPY_C) || \ |
| 39 | !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \ |
| 40 | !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C) |
| 41 | #include <stdio.h> |
| 42 | int main( int argc, char *argv[] ) |
| 43 | { |
| 44 | ((void) argc); |
| 45 | ((void) argv); |
| 46 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 47 | polarssl_printf("POLARSSL_ENTROPY_C and/or " |
Manuel Pégourié-Gonnard | 8a4d571 | 2014-06-24 14:19:59 +0200 | [diff] [blame] | 48 | "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or " |
| 49 | "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n"); |
| 50 | return( 0 ); |
| 51 | } |
| 52 | #else |
| 53 | |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 54 | #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) && defined(POLARSSL_FS_IO) |
| 55 | #define POLARSSL_SNI |
| 56 | #endif |
| 57 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 58 | #if defined(_WIN32) |
| 59 | #include <windows.h> |
| 60 | #endif |
| 61 | |
| 62 | #include <string.h> |
| 63 | #include <stdlib.h> |
| 64 | #include <stdio.h> |
Paul Bakker | c1283d3 | 2014-08-18 11:05:51 +0200 | [diff] [blame] | 65 | |
| 66 | #if !defined(_WIN32) |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 67 | #include <signal.h> |
Paul Bakker | c1283d3 | 2014-08-18 11:05:51 +0200 | [diff] [blame] | 68 | #endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 69 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 70 | #include "polarssl/net.h" |
| 71 | #include "polarssl/ssl.h" |
| 72 | #include "polarssl/entropy.h" |
| 73 | #include "polarssl/ctr_drbg.h" |
| 74 | #include "polarssl/certs.h" |
| 75 | #include "polarssl/x509.h" |
| 76 | #include "polarssl/error.h" |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 77 | #include "polarssl/debug.h" |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 78 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 79 | #if defined(POLARSSL_SSL_CACHE_C) |
| 80 | #include "polarssl/ssl_cache.h" |
| 81 | #endif |
| 82 | |
Paul Bakker | 82024bf | 2013-07-04 11:52:32 +0200 | [diff] [blame] | 83 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) |
Manuel Pégourié-Gonnard | d43ccb6 | 2015-01-23 17:38:09 +0000 | [diff] [blame] | 84 | #include "polarssl/memory_buffer_alloc.h" |
Paul Bakker | 82024bf | 2013-07-04 11:52:32 +0200 | [diff] [blame] | 85 | #endif |
| 86 | |
Manuel Pégourié-Gonnard | 18d31f8 | 2013-12-13 16:21:41 +0100 | [diff] [blame] | 87 | #define DFL_SERVER_ADDR NULL |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 88 | #define DFL_SERVER_PORT 4433 |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 89 | #define DFL_DEBUG_LEVEL 0 |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 90 | #define DFL_NBIO 0 |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 91 | #define DFL_CA_FILE "" |
| 92 | #define DFL_CA_PATH "" |
| 93 | #define DFL_CRT_FILE "" |
| 94 | #define DFL_KEY_FILE "" |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 95 | #define DFL_CRT_FILE2 "" |
| 96 | #define DFL_KEY_FILE2 "" |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 97 | #define DFL_PSK "" |
| 98 | #define DFL_PSK_IDENTITY "Client_identity" |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 99 | #define DFL_PSK_LIST NULL |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 100 | #define DFL_FORCE_CIPHER 0 |
Manuel Pégourié-Gonnard | 6dc0781 | 2014-06-11 13:50:34 +0200 | [diff] [blame] | 101 | #define DFL_VERSION_SUITES NULL |
Manuel Pégourié-Gonnard | 00d538f | 2014-03-31 10:44:40 +0200 | [diff] [blame] | 102 | #define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED |
Manuel Pégourié-Gonnard | 85d915b | 2014-11-03 20:10:36 +0100 | [diff] [blame] | 103 | #define DFL_ALLOW_LEGACY -2 |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 104 | #define DFL_RENEGOTIATE 0 |
Manuel Pégourié-Gonnard | fae355e | 2014-07-04 14:32:27 +0200 | [diff] [blame] | 105 | #define DFL_RENEGO_DELAY -2 |
Manuel Pégourié-Gonnard | 590f416 | 2014-11-05 14:23:03 +0100 | [diff] [blame] | 106 | #define DFL_RENEGO_PERIOD -1 |
Manuel Pégourié-Gonnard | 67686c4 | 2014-08-15 11:17:27 +0200 | [diff] [blame] | 107 | #define DFL_EXCHANGES 1 |
Manuel Pégourié-Gonnard | 448ea50 | 2015-01-12 11:40:14 +0100 | [diff] [blame] | 108 | #define DFL_MIN_VERSION SSL_MINOR_VERSION_1 |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 109 | #define DFL_MAX_VERSION -1 |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 110 | #define DFL_ARC4 SSL_ARC4_DISABLED |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 111 | #define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 112 | #define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE |
Manuel Pégourié-Gonnard | e117a8f | 2015-01-09 12:39:35 +0100 | [diff] [blame] | 113 | #define DFL_TRUNC_HMAC -1 |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 114 | #define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED |
Manuel Pégourié-Gonnard | dbe1ee1 | 2014-02-21 09:18:13 +0100 | [diff] [blame] | 115 | #define DFL_TICKET_TIMEOUT -1 |
Manuel Pégourié-Gonnard | 4c88345 | 2014-02-20 21:32:41 +0100 | [diff] [blame] | 116 | #define DFL_CACHE_MAX -1 |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 117 | #define DFL_CACHE_TIMEOUT -1 |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 118 | #define DFL_SNI NULL |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 119 | #define DFL_ALPN_STRING NULL |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 120 | #define DFL_DHM_FILE NULL |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 121 | #define DFL_EXTENDED_MS -1 |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 122 | #define DFL_ETM -1 |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 123 | |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 124 | #define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 125 | "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 126 | "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 127 | "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 128 | "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 129 | "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 130 | "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] | 131 | |
Paul Bakker | 8e714d7 | 2013-07-18 11:05:13 +0200 | [diff] [blame] | 132 | /* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer |
| 133 | * packets (for fragmentation purposes) */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 134 | #define HTTP_RESPONSE \ |
| 135 | "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] | 136 | "<h2>mbed TLS Test Server</h2>\r\n" \ |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 137 | "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 138 | |
Manuel Pégourié-Gonnard | 95c0a63 | 2014-06-11 18:32:36 +0200 | [diff] [blame] | 139 | /* |
| 140 | * Size of the basic I/O buffer. Able to hold our default response. |
| 141 | * |
| 142 | * You will need to adapt the ssl_get_bytes_avail() test in ssl-opt.sh |
| 143 | * if you change this value to something outside the range <= 100 or > 500 |
| 144 | */ |
Manuel Pégourié-Gonnard | e7a3b10 | 2014-06-11 18:21:20 +0200 | [diff] [blame] | 145 | #define IO_BUF_LEN 200 |
| 146 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 147 | /* |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 148 | * global options |
| 149 | */ |
| 150 | struct options |
| 151 | { |
Manuel Pégourié-Gonnard | 18d31f8 | 2013-12-13 16:21:41 +0100 | [diff] [blame] | 152 | const char *server_addr; /* address on which the ssl service runs */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 153 | int server_port; /* port on which the ssl service runs */ |
| 154 | int debug_level; /* level of debugging */ |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 155 | int nbio; /* should I/O be blocking? */ |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 156 | const char *ca_file; /* the file with the CA certificate(s) */ |
| 157 | const char *ca_path; /* the path with the CA certificate(s) reside */ |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 158 | const char *crt_file; /* the file with the server certificate */ |
| 159 | const char *key_file; /* the file with the server key */ |
| 160 | const char *crt_file2; /* the file with the 2nd server certificate */ |
| 161 | const char *key_file2; /* the file with the 2nd server key */ |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 162 | const char *psk; /* the pre-shared key */ |
| 163 | const char *psk_identity; /* the pre-shared key identity */ |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 164 | char *psk_list; /* list of PSK id/key pairs for callback */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 165 | int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */ |
Manuel Pégourié-Gonnard | 6dc0781 | 2014-06-11 13:50:34 +0200 | [diff] [blame] | 166 | const char *version_suites; /* per-version ciphersuites */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 167 | int renegotiation; /* enable / disable renegotiation */ |
| 168 | int allow_legacy; /* allow legacy renegotiation */ |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 169 | int renegotiate; /* attempt renegotiation? */ |
Manuel Pégourié-Gonnard | fae355e | 2014-07-04 14:32:27 +0200 | [diff] [blame] | 170 | int renego_delay; /* delay before enforcing renegotiation */ |
Manuel Pégourié-Gonnard | 590f416 | 2014-11-05 14:23:03 +0100 | [diff] [blame] | 171 | int renego_period; /* period for automatic renegotiation */ |
Manuel Pégourié-Gonnard | 67686c4 | 2014-08-15 11:17:27 +0200 | [diff] [blame] | 172 | int exchanges; /* number of data exchanges */ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 173 | int min_version; /* minimum protocol version accepted */ |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 174 | int max_version; /* maximum protocol version accepted */ |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 175 | int arc4; /* flag for arc4 suites support */ |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 176 | int auth_mode; /* verify mode for connection */ |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 177 | unsigned char mfl_code; /* code for maximum fragment length */ |
Manuel Pégourié-Gonnard | e117a8f | 2015-01-09 12:39:35 +0100 | [diff] [blame] | 178 | int trunc_hmac; /* accept truncated hmac? */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 179 | int tickets; /* enable / disable session tickets */ |
Manuel Pégourié-Gonnard | dbe1ee1 | 2014-02-21 09:18:13 +0100 | [diff] [blame] | 180 | int ticket_timeout; /* session ticket lifetime */ |
Manuel Pégourié-Gonnard | 4c88345 | 2014-02-20 21:32:41 +0100 | [diff] [blame] | 181 | int cache_max; /* max number of session cache entries */ |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 182 | int cache_timeout; /* expiration delay of session cache entries */ |
Paul Bakker | 1ebc0c5 | 2014-05-22 15:47:58 +0200 | [diff] [blame] | 183 | char *sni; /* string describing sni information */ |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 184 | const char *alpn_string; /* ALPN supported protocols */ |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 185 | const char *dhm_file; /* the file with the DH parameters */ |
Paul Bakker | b2eaac1 | 2015-01-13 17:15:31 +0100 | [diff] [blame] | 186 | int extended_ms; /* allow negotiation of extended MS? */ |
| 187 | int etm; /* allow negotiation of encrypt-then-MAC? */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 188 | } opt; |
| 189 | |
Paul Bakker | 3c5ef71 | 2013-06-25 16:37:45 +0200 | [diff] [blame] | 190 | static void my_debug( void *ctx, int level, const char *str ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 191 | { |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 192 | ((void) level); |
| 193 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 194 | polarssl_fprintf( (FILE *) ctx, "%s", str ); |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 195 | fflush( (FILE *) ctx ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 198 | /* |
| 199 | * Test recv/send functions that make sure each try returns |
| 200 | * WANT_READ/WANT_WRITE at least once before sucesseding |
| 201 | */ |
| 202 | static int my_recv( void *ctx, unsigned char *buf, size_t len ) |
| 203 | { |
| 204 | static int first_try = 1; |
| 205 | int ret; |
| 206 | |
| 207 | if( first_try ) |
| 208 | { |
| 209 | first_try = 0; |
| 210 | return( POLARSSL_ERR_NET_WANT_READ ); |
| 211 | } |
| 212 | |
| 213 | ret = net_recv( ctx, buf, len ); |
| 214 | if( ret != POLARSSL_ERR_NET_WANT_READ ) |
| 215 | first_try = 1; /* Next call will be a new operation */ |
| 216 | return( ret ); |
| 217 | } |
| 218 | |
| 219 | static int my_send( void *ctx, const unsigned char *buf, size_t len ) |
| 220 | { |
| 221 | static int first_try = 1; |
| 222 | int ret; |
| 223 | |
| 224 | if( first_try ) |
| 225 | { |
| 226 | first_try = 0; |
| 227 | return( POLARSSL_ERR_NET_WANT_WRITE ); |
| 228 | } |
| 229 | |
| 230 | ret = net_send( ctx, buf, len ); |
| 231 | if( ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 232 | first_try = 1; /* Next call will be a new operation */ |
| 233 | return( ret ); |
| 234 | } |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 235 | |
| 236 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 237 | #if defined(POLARSSL_FS_IO) |
| 238 | #define USAGE_IO \ |
Paul Bakker | 1f9d02d | 2012-11-20 10:30:55 +0100 | [diff] [blame] | 239 | " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \ |
| 240 | " default: \"\" (pre-loaded)\n" \ |
| 241 | " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \ |
| 242 | " default: \"\" (pre-loaded) (overrides ca_file)\n" \ |
| 243 | " 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] | 244 | " default: see note after key_file2\n" \ |
| 245 | " key_file=%%s default: see note after key_file2\n" \ |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 246 | " 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] | 247 | " default: see note after key_file2\n" \ |
| 248 | " key_file2=%%s default: see note below\n" \ |
| 249 | " 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] | 250 | " preloaded certificate(s) and key(s) are used if available\n" \ |
| 251 | " dhm_file=%%s File containing Diffie-Hellman parameters\n" \ |
| 252 | " default: preloaded parameters\n" |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 253 | #else |
| 254 | #define USAGE_IO \ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 255 | "\n" \ |
| 256 | " No file operations available (POLARSSL_FS_IO not defined)\n" \ |
| 257 | "\n" |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 258 | #endif /* POLARSSL_FS_IO */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 259 | #else |
| 260 | #define USAGE_IO "" |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 261 | #endif /* POLARSSL_X509_CRT_PARSE_C */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 262 | |
Manuel Pégourié-Gonnard | 8a3c64d | 2013-10-14 19:54:10 +0200 | [diff] [blame] | 263 | #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 264 | #define USAGE_PSK \ |
| 265 | " psk=%%s default: \"\" (in hex, without 0x)\n" \ |
| 266 | " psk_identity=%%s default: \"Client_identity\"\n" |
| 267 | #else |
| 268 | #define USAGE_PSK "" |
Manuel Pégourié-Gonnard | 8a3c64d | 2013-10-14 19:54:10 +0200 | [diff] [blame] | 269 | #endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 270 | |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 271 | #if defined(POLARSSL_SSL_SESSION_TICKETS) |
| 272 | #define USAGE_TICKETS \ |
Manuel Pégourié-Gonnard | dbe1ee1 | 2014-02-21 09:18:13 +0100 | [diff] [blame] | 273 | " tickets=%%d default: 1 (enabled)\n" \ |
| 274 | " ticket_timeout=%%d default: ticket default (1d)\n" |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 275 | #else |
| 276 | #define USAGE_TICKETS "" |
| 277 | #endif /* POLARSSL_SSL_SESSION_TICKETS */ |
| 278 | |
Manuel Pégourié-Gonnard | 4c88345 | 2014-02-20 21:32:41 +0100 | [diff] [blame] | 279 | #if defined(POLARSSL_SSL_CACHE_C) |
| 280 | #define USAGE_CACHE \ |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 281 | " cache_max=%%d default: cache default (50)\n" \ |
| 282 | " cache_timeout=%%d default: cache default (1d)\n" |
Manuel Pégourié-Gonnard | 4c88345 | 2014-02-20 21:32:41 +0100 | [diff] [blame] | 283 | #else |
| 284 | #define USAGE_CACHE "" |
| 285 | #endif /* POLARSSL_SSL_CACHE_C */ |
| 286 | |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 287 | #if defined(POLARSSL_SNI) |
| 288 | #define USAGE_SNI \ |
| 289 | " sni=%%s name1,cert1,key1[,name2,cert2,key2[,...]]\n" \ |
| 290 | " default: disabled\n" |
| 291 | #else |
| 292 | #define USAGE_SNI "" |
| 293 | #endif /* POLARSSL_SNI */ |
| 294 | |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 295 | #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH) |
| 296 | #define USAGE_MAX_FRAG_LEN \ |
| 297 | " max_frag_len=%%d default: 16384 (tls default)\n" \ |
| 298 | " options: 512, 1024, 2048, 4096\n" |
| 299 | #else |
| 300 | #define USAGE_MAX_FRAG_LEN "" |
| 301 | #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */ |
| 302 | |
Manuel Pégourié-Gonnard | e117a8f | 2015-01-09 12:39:35 +0100 | [diff] [blame] | 303 | #if defined(POLARSSL_SSL_TRUNCATED_HMAC) |
| 304 | #define USAGE_TRUNC_HMAC \ |
| 305 | " trunc_hmac=%%d default: library default\n" |
| 306 | #else |
| 307 | #define USAGE_TRUNC_HMAC "" |
| 308 | #endif |
| 309 | |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 310 | #if defined(POLARSSL_SSL_ALPN) |
| 311 | #define USAGE_ALPN \ |
| 312 | " alpn=%%s default: \"\" (disabled)\n" \ |
| 313 | " example: spdy/1,http/1.1\n" |
| 314 | #else |
| 315 | #define USAGE_ALPN "" |
| 316 | #endif /* POLARSSL_SSL_ALPN */ |
| 317 | |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 318 | #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) |
| 319 | #define USAGE_EMS \ |
| 320 | " extended_ms=0/1 default: (library default: on)\n" |
| 321 | #else |
| 322 | #define USAGE_EMS "" |
| 323 | #endif |
| 324 | |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 325 | #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) |
| 326 | #define USAGE_ETM \ |
| 327 | " etm=0/1 default: (library default: on)\n" |
| 328 | #else |
| 329 | #define USAGE_ETM "" |
| 330 | #endif |
| 331 | |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 332 | #if defined(POLARSSL_SSL_RENEGOTIATION) |
| 333 | #define USAGE_RENEGO \ |
| 334 | " renegotiation=%%d default: 0 (disabled)\n" \ |
| 335 | " renegotiate=%%d default: 0 (disabled)\n" \ |
Manuel Pégourié-Gonnard | 590f416 | 2014-11-05 14:23:03 +0100 | [diff] [blame] | 336 | " renego_delay=%%d default: -2 (library default)\n" \ |
| 337 | " renego_period=%%d default: (library default)\n" |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 338 | #else |
| 339 | #define USAGE_RENEGO "" |
| 340 | #endif |
| 341 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 342 | #define USAGE \ |
| 343 | "\n usage: ssl_server2 param=<>...\n" \ |
| 344 | "\n acceptable parameters:\n" \ |
Manuel Pégourié-Gonnard | 18d31f8 | 2013-12-13 16:21:41 +0100 | [diff] [blame] | 345 | " server_addr=%%d default: (all interfaces)\n" \ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 346 | " server_port=%%d default: 4433\n" \ |
| 347 | " debug_level=%%d default: 0 (disabled)\n" \ |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 348 | " nbio=%%d default: 0 (blocking I/O)\n" \ |
| 349 | " options: 1 (non-blocking), 2 (added delays)\n" \ |
Manuel Pégourié-Gonnard | 2fc243d | 2014-02-19 18:22:59 +0100 | [diff] [blame] | 350 | "\n" \ |
| 351 | " auth_mode=%%s default: \"optional\"\n" \ |
| 352 | " options: none, optional, required\n" \ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 353 | USAGE_IO \ |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 354 | USAGE_SNI \ |
Manuel Pégourié-Gonnard | 2fc243d | 2014-02-19 18:22:59 +0100 | [diff] [blame] | 355 | "\n" \ |
| 356 | USAGE_PSK \ |
| 357 | "\n" \ |
Manuel Pégourié-Gonnard | 85d915b | 2014-11-03 20:10:36 +0100 | [diff] [blame] | 358 | " allow_legacy=%%d default: (library default: no)\n" \ |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 359 | USAGE_RENEGO \ |
Manuel Pégourié-Gonnard | 67686c4 | 2014-08-15 11:17:27 +0200 | [diff] [blame] | 360 | " exchanges=%%d default: 1\n" \ |
Manuel Pégourié-Gonnard | 2fc243d | 2014-02-19 18:22:59 +0100 | [diff] [blame] | 361 | USAGE_TICKETS \ |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 362 | USAGE_CACHE \ |
Manuel Pégourié-Gonnard | 2fc243d | 2014-02-19 18:22:59 +0100 | [diff] [blame] | 363 | USAGE_MAX_FRAG_LEN \ |
Manuel Pégourié-Gonnard | e117a8f | 2015-01-09 12:39:35 +0100 | [diff] [blame] | 364 | USAGE_TRUNC_HMAC \ |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 365 | USAGE_ALPN \ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 366 | USAGE_EMS \ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 367 | USAGE_ETM \ |
Manuel Pégourié-Gonnard | 2fc243d | 2014-02-19 18:22:59 +0100 | [diff] [blame] | 368 | "\n" \ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 369 | " min_version=%%s default: \"ssl3\"\n" \ |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 370 | " max_version=%%s default: \"tls1_2\"\n" \ |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 371 | " arc4=%%d default: 0 (disabled)\n" \ |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 372 | " force_version=%%s default: \"\" (none)\n" \ |
Manuel Pégourié-Gonnard | 6dc0781 | 2014-06-11 13:50:34 +0200 | [diff] [blame] | 373 | " options: ssl3, tls1, tls1_1, tls1_2\n" \ |
| 374 | "\n" \ |
| 375 | " version_suites=a,b,c,d per-version ciphersuites\n" \ |
| 376 | " in order from ssl3 to tls1_2\n" \ |
| 377 | " default: all enabled\n" \ |
| 378 | " force_ciphersuite=<name> default: all enabled\n" \ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 379 | " acceptable ciphersuite names:\n" |
| 380 | |
Manuel Pégourié-Gonnard | fdee74b | 2014-06-10 15:15:06 +0200 | [diff] [blame] | 381 | /* |
| 382 | * Used by sni_parse and psk_parse to handle coma-separated lists |
| 383 | */ |
| 384 | #define GET_ITEM( dst ) \ |
| 385 | dst = p; \ |
| 386 | while( *p != ',' ) \ |
| 387 | if( ++p > end ) \ |
| 388 | return( NULL ); \ |
| 389 | *p++ = '\0'; |
| 390 | |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 391 | #if defined(POLARSSL_SNI) |
| 392 | typedef struct _sni_entry sni_entry; |
| 393 | |
| 394 | struct _sni_entry { |
| 395 | const char *name; |
| 396 | x509_crt *cert; |
| 397 | pk_context *key; |
| 398 | sni_entry *next; |
| 399 | }; |
| 400 | |
| 401 | /* |
| 402 | * Parse a string of triplets name1,crt1,key1[,name2,crt2,key2[,...]] |
| 403 | * into a usable sni_entry list. |
| 404 | * |
Manuel Pégourié-Gonnard | fdee74b | 2014-06-10 15:15:06 +0200 | [diff] [blame] | 405 | * Modifies the input string! This is not production quality! |
| 406 | * (leaks memory if parsing fails, no error reporting, ...) |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 407 | */ |
| 408 | sni_entry *sni_parse( char *sni_string ) |
| 409 | { |
| 410 | sni_entry *cur = NULL, *new = NULL; |
| 411 | char *p = sni_string; |
| 412 | char *end = p; |
| 413 | char *crt_file, *key_file; |
| 414 | |
| 415 | while( *end != '\0' ) |
| 416 | ++end; |
| 417 | *end = ','; |
| 418 | |
| 419 | while( p <= end ) |
| 420 | { |
| 421 | if( ( new = polarssl_malloc( sizeof( sni_entry ) ) ) == NULL ) |
| 422 | return( NULL ); |
| 423 | |
| 424 | memset( new, 0, sizeof( sni_entry ) ); |
| 425 | |
| 426 | if( ( new->cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL || |
| 427 | ( new->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL ) |
Manuel Pégourié-Gonnard | fdee74b | 2014-06-10 15:15:06 +0200 | [diff] [blame] | 428 | return( NULL ); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 429 | |
| 430 | x509_crt_init( new->cert ); |
| 431 | pk_init( new->key ); |
| 432 | |
Manuel Pégourié-Gonnard | fdee74b | 2014-06-10 15:15:06 +0200 | [diff] [blame] | 433 | GET_ITEM( new->name ); |
| 434 | GET_ITEM( crt_file ); |
| 435 | GET_ITEM( key_file ); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 436 | |
| 437 | if( x509_crt_parse_file( new->cert, crt_file ) != 0 || |
| 438 | pk_parse_keyfile( new->key, key_file, "" ) != 0 ) |
Manuel Pégourié-Gonnard | fdee74b | 2014-06-10 15:15:06 +0200 | [diff] [blame] | 439 | return( NULL ); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 440 | |
| 441 | new->next = cur; |
| 442 | cur = new; |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | return( cur ); |
| 446 | } |
| 447 | |
| 448 | void sni_free( sni_entry *head ) |
| 449 | { |
| 450 | sni_entry *cur = head, *next; |
| 451 | |
| 452 | while( cur != NULL ) |
| 453 | { |
| 454 | x509_crt_free( cur->cert ); |
| 455 | polarssl_free( cur->cert ); |
| 456 | |
| 457 | pk_free( cur->key ); |
| 458 | polarssl_free( cur->key ); |
| 459 | |
| 460 | next = cur->next; |
| 461 | polarssl_free( cur ); |
| 462 | cur = next; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | /* |
| 467 | * SNI callback. |
| 468 | */ |
| 469 | int sni_callback( void *p_info, ssl_context *ssl, |
| 470 | const unsigned char *name, size_t name_len ) |
| 471 | { |
| 472 | sni_entry *cur = (sni_entry *) p_info; |
| 473 | |
| 474 | while( cur != NULL ) |
| 475 | { |
| 476 | if( name_len == strlen( cur->name ) && |
| 477 | memcmp( name, cur->name, name_len ) == 0 ) |
| 478 | { |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 479 | return( ssl_set_own_cert( ssl, cur->cert, cur->key ) ); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | cur = cur->next; |
| 483 | } |
| 484 | |
| 485 | return( -1 ); |
| 486 | } |
| 487 | |
| 488 | #endif /* POLARSSL_SNI */ |
| 489 | |
Manuel Pégourié-Gonnard | 9e27163 | 2014-06-09 19:06:00 +0200 | [diff] [blame] | 490 | #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 491 | |
| 492 | #define HEX2NUM( c ) \ |
| 493 | if( c >= '0' && c <= '9' ) \ |
| 494 | c -= '0'; \ |
| 495 | else if( c >= 'a' && c <= 'f' ) \ |
| 496 | c -= 'a' - 10; \ |
| 497 | else if( c >= 'A' && c <= 'F' ) \ |
| 498 | c -= 'A' - 10; \ |
| 499 | else \ |
| 500 | return( -1 ); |
| 501 | |
| 502 | /* |
| 503 | * Convert a hex string to bytes. |
| 504 | * Return 0 on success, -1 on error. |
| 505 | */ |
| 506 | int unhexify( unsigned char *output, const char *input, size_t *olen ) |
| 507 | { |
| 508 | unsigned char c; |
| 509 | size_t j; |
| 510 | |
| 511 | *olen = strlen( input ); |
Manuel Pégourié-Gonnard | 481fcfd | 2014-07-03 16:12:50 +0200 | [diff] [blame] | 512 | if( *olen % 2 != 0 || *olen / 2 > POLARSSL_PSK_MAX_LEN ) |
Manuel Pégourié-Gonnard | 9e27163 | 2014-06-09 19:06:00 +0200 | [diff] [blame] | 513 | return( -1 ); |
| 514 | *olen /= 2; |
| 515 | |
| 516 | for( j = 0; j < *olen * 2; j += 2 ) |
| 517 | { |
| 518 | c = input[j]; |
| 519 | HEX2NUM( c ); |
| 520 | output[ j / 2 ] = c << 4; |
| 521 | |
| 522 | c = input[j + 1]; |
| 523 | HEX2NUM( c ); |
| 524 | output[ j / 2 ] |= c; |
| 525 | } |
| 526 | |
| 527 | return( 0 ); |
| 528 | } |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 529 | |
| 530 | typedef struct _psk_entry psk_entry; |
| 531 | |
| 532 | struct _psk_entry |
| 533 | { |
| 534 | const char *name; |
| 535 | size_t key_len; |
Manuel Pégourié-Gonnard | 481fcfd | 2014-07-03 16:12:50 +0200 | [diff] [blame] | 536 | unsigned char key[POLARSSL_PSK_MAX_LEN]; |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 537 | psk_entry *next; |
| 538 | }; |
| 539 | |
| 540 | /* |
| 541 | * Parse a string of pairs name1,key1[,name2,key2[,...]] |
| 542 | * into a usable psk_entry list. |
| 543 | * |
| 544 | * Modifies the input string! This is not production quality! |
| 545 | * (leaks memory if parsing fails, no error reporting, ...) |
| 546 | */ |
| 547 | psk_entry *psk_parse( char *psk_string ) |
| 548 | { |
| 549 | psk_entry *cur = NULL, *new = NULL; |
| 550 | char *p = psk_string; |
| 551 | char *end = p; |
| 552 | char *key_hex; |
| 553 | |
| 554 | while( *end != '\0' ) |
| 555 | ++end; |
| 556 | *end = ','; |
| 557 | |
| 558 | while( p <= end ) |
| 559 | { |
| 560 | if( ( new = polarssl_malloc( sizeof( psk_entry ) ) ) == NULL ) |
| 561 | return( NULL ); |
| 562 | |
| 563 | memset( new, 0, sizeof( psk_entry ) ); |
| 564 | |
Manuel Pégourié-Gonnard | fdee74b | 2014-06-10 15:15:06 +0200 | [diff] [blame] | 565 | GET_ITEM( new->name ); |
| 566 | GET_ITEM( key_hex ); |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 567 | |
| 568 | if( unhexify( new->key, key_hex, &new->key_len ) != 0 ) |
| 569 | return( NULL ); |
| 570 | |
| 571 | new->next = cur; |
| 572 | cur = new; |
| 573 | } |
| 574 | |
| 575 | return( cur ); |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | * Free a list of psk_entry's |
| 580 | */ |
| 581 | void psk_free( psk_entry *head ) |
| 582 | { |
| 583 | psk_entry *next; |
| 584 | |
| 585 | while( head != NULL ) |
| 586 | { |
| 587 | next = head->next; |
| 588 | polarssl_free( head ); |
| 589 | head = next; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | * PSK callback |
| 595 | */ |
| 596 | int psk_callback( void *p_info, ssl_context *ssl, |
| 597 | const unsigned char *name, size_t name_len ) |
| 598 | { |
| 599 | psk_entry *cur = (psk_entry *) p_info; |
| 600 | |
| 601 | while( cur != NULL ) |
| 602 | { |
| 603 | if( name_len == strlen( cur->name ) && |
| 604 | memcmp( name, cur->name, name_len ) == 0 ) |
| 605 | { |
| 606 | return( ssl_set_psk( ssl, cur->key, cur->key_len, |
| 607 | name, name_len ) ); |
| 608 | } |
| 609 | |
| 610 | cur = cur->next; |
| 611 | } |
| 612 | |
| 613 | return( -1 ); |
| 614 | } |
Manuel Pégourié-Gonnard | 9e27163 | 2014-06-09 19:06:00 +0200 | [diff] [blame] | 615 | #endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
| 616 | |
Manuel Pégourié-Gonnard | 3a3066c | 2014-09-20 12:03:00 +0200 | [diff] [blame] | 617 | static int listen_fd, client_fd = -1; |
Paul Bakker | bc3e54c | 2014-08-18 14:36:17 +0200 | [diff] [blame] | 618 | |
| 619 | /* Interruption handler to ensure clean exit (for valgrind testing) */ |
| 620 | #if !defined(_WIN32) |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 621 | static int received_sigterm = 0; |
| 622 | void term_handler( int sig ) |
| 623 | { |
| 624 | ((void) sig); |
| 625 | received_sigterm = 1; |
| 626 | net_close( listen_fd ); /* causes net_accept() to abort */ |
Manuel Pégourié-Gonnard | 3a3066c | 2014-09-20 12:03:00 +0200 | [diff] [blame] | 627 | net_close( client_fd ); /* causes net_read() to abort */ |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 628 | } |
Paul Bakker | c1283d3 | 2014-08-18 11:05:51 +0200 | [diff] [blame] | 629 | #endif |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 630 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 631 | int main( int argc, char *argv[] ) |
| 632 | { |
Manuel Pégourié-Gonnard | 6a0017b | 2015-01-22 10:33:29 +0000 | [diff] [blame] | 633 | int ret = 0, len, written, frags, exchanges_left; |
Manuel Pégourié-Gonnard | 6dc0781 | 2014-06-11 13:50:34 +0200 | [diff] [blame] | 634 | int version_suites[4][2]; |
Manuel Pégourié-Gonnard | e7a3b10 | 2014-06-11 18:21:20 +0200 | [diff] [blame] | 635 | unsigned char buf[IO_BUF_LEN]; |
Manuel Pégourié-Gonnard | 8a3c64d | 2013-10-14 19:54:10 +0200 | [diff] [blame] | 636 | #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Manuel Pégourié-Gonnard | 481fcfd | 2014-07-03 16:12:50 +0200 | [diff] [blame] | 637 | unsigned char psk[POLARSSL_PSK_MAX_LEN]; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 638 | size_t psk_len = 0; |
Paul Bakker | 9b7fb6f | 2014-06-12 23:01:43 +0200 | [diff] [blame] | 639 | psk_entry *psk_info = NULL; |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 640 | #endif |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 641 | const char *pers = "ssl_server2"; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 642 | |
| 643 | entropy_context entropy; |
| 644 | ctr_drbg_context ctr_drbg; |
| 645 | ssl_context ssl; |
Manuel Pégourié-Gonnard | 590f416 | 2014-11-05 14:23:03 +0100 | [diff] [blame] | 646 | #if defined(POLARSSL_SSL_RENEGOTIATION) |
| 647 | unsigned char renego_period[8] = { 0 }; |
| 648 | #endif |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 649 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 650 | x509_crt cacert; |
| 651 | x509_crt srvcert; |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 652 | pk_context pkey; |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 653 | x509_crt srvcert2; |
| 654 | pk_context pkey2; |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 655 | int key_cert_init = 0, key_cert_init2 = 0; |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 656 | #endif |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 657 | #if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO) |
| 658 | dhm_context dhm; |
| 659 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 660 | #if defined(POLARSSL_SSL_CACHE_C) |
| 661 | ssl_cache_context cache; |
| 662 | #endif |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 663 | #if defined(POLARSSL_SNI) |
| 664 | sni_entry *sni_info = NULL; |
| 665 | #endif |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 666 | #if defined(POLARSSL_SSL_ALPN) |
| 667 | const char *alpn_list[10]; |
| 668 | #endif |
Paul Bakker | 82024bf | 2013-07-04 11:52:32 +0200 | [diff] [blame] | 669 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) |
| 670 | unsigned char alloc_buf[100000]; |
| 671 | #endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 672 | |
| 673 | int i; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 674 | char *p, *q; |
| 675 | const int *list; |
| 676 | |
Paul Bakker | 82024bf | 2013-07-04 11:52:32 +0200 | [diff] [blame] | 677 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) |
| 678 | memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) ); |
| 679 | #endif |
| 680 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 681 | /* |
Manuel Pégourié-Gonnard | 3bd2aae | 2013-09-20 13:10:13 +0200 | [diff] [blame] | 682 | * Make sure memory references are valid in case we exit early. |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 683 | */ |
| 684 | listen_fd = 0; |
Manuel Pégourié-Gonnard | 3bd2aae | 2013-09-20 13:10:13 +0200 | [diff] [blame] | 685 | memset( &ssl, 0, sizeof( ssl_context ) ); |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 686 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 687 | x509_crt_init( &cacert ); |
| 688 | x509_crt_init( &srvcert ); |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 689 | pk_init( &pkey ); |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 690 | x509_crt_init( &srvcert2 ); |
| 691 | pk_init( &pkey2 ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 692 | #endif |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 693 | #if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO) |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 694 | dhm_init( &dhm ); |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 695 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 696 | #if defined(POLARSSL_SSL_CACHE_C) |
| 697 | ssl_cache_init( &cache ); |
| 698 | #endif |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 699 | #if defined(POLARSSL_SSL_ALPN) |
Paul Bakker | 525f875 | 2014-05-01 10:58:57 +0200 | [diff] [blame] | 700 | memset( (void *) alpn_list, 0, sizeof( alpn_list ) ); |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 701 | #endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 702 | |
Paul Bakker | c1283d3 | 2014-08-18 11:05:51 +0200 | [diff] [blame] | 703 | #if !defined(_WIN32) |
Manuel Pégourié-Gonnard | 403a86f | 2014-11-17 12:46:49 +0100 | [diff] [blame] | 704 | /* Abort cleanly on SIGTERM and SIGINT */ |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 705 | signal( SIGTERM, term_handler ); |
Manuel Pégourié-Gonnard | 403a86f | 2014-11-17 12:46:49 +0100 | [diff] [blame] | 706 | signal( SIGINT, term_handler ); |
Paul Bakker | c1283d3 | 2014-08-18 11:05:51 +0200 | [diff] [blame] | 707 | #endif |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 708 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 709 | if( argc == 0 ) |
| 710 | { |
| 711 | usage: |
| 712 | if( ret == 0 ) |
| 713 | ret = 1; |
| 714 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 715 | polarssl_printf( USAGE ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 716 | |
| 717 | list = ssl_list_ciphersuites(); |
| 718 | while( *list ) |
| 719 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 720 | polarssl_printf(" %-42s", ssl_get_ciphersuite_name( *list ) ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 721 | list++; |
| 722 | if( !*list ) |
| 723 | break; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 724 | polarssl_printf(" %s\n", ssl_get_ciphersuite_name( *list ) ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 725 | list++; |
| 726 | } |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 727 | polarssl_printf("\n"); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 728 | goto exit; |
| 729 | } |
| 730 | |
Manuel Pégourié-Gonnard | 18d31f8 | 2013-12-13 16:21:41 +0100 | [diff] [blame] | 731 | opt.server_addr = DFL_SERVER_ADDR; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 732 | opt.server_port = DFL_SERVER_PORT; |
| 733 | opt.debug_level = DFL_DEBUG_LEVEL; |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 734 | opt.nbio = DFL_NBIO; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 735 | opt.ca_file = DFL_CA_FILE; |
| 736 | opt.ca_path = DFL_CA_PATH; |
| 737 | opt.crt_file = DFL_CRT_FILE; |
| 738 | opt.key_file = DFL_KEY_FILE; |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 739 | opt.crt_file2 = DFL_CRT_FILE2; |
| 740 | opt.key_file2 = DFL_KEY_FILE2; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 741 | opt.psk = DFL_PSK; |
| 742 | opt.psk_identity = DFL_PSK_IDENTITY; |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 743 | opt.psk_list = DFL_PSK_LIST; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 744 | opt.force_ciphersuite[0]= DFL_FORCE_CIPHER; |
Manuel Pégourié-Gonnard | 6dc0781 | 2014-06-11 13:50:34 +0200 | [diff] [blame] | 745 | opt.version_suites = DFL_VERSION_SUITES; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 746 | opt.renegotiation = DFL_RENEGOTIATION; |
| 747 | opt.allow_legacy = DFL_ALLOW_LEGACY; |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 748 | opt.renegotiate = DFL_RENEGOTIATE; |
Manuel Pégourié-Gonnard | fae355e | 2014-07-04 14:32:27 +0200 | [diff] [blame] | 749 | opt.renego_delay = DFL_RENEGO_DELAY; |
Manuel Pégourié-Gonnard | 590f416 | 2014-11-05 14:23:03 +0100 | [diff] [blame] | 750 | opt.renego_period = DFL_RENEGO_PERIOD; |
Manuel Pégourié-Gonnard | 67686c4 | 2014-08-15 11:17:27 +0200 | [diff] [blame] | 751 | opt.exchanges = DFL_EXCHANGES; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 752 | opt.min_version = DFL_MIN_VERSION; |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 753 | opt.max_version = DFL_MAX_VERSION; |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 754 | opt.arc4 = DFL_ARC4; |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 755 | opt.auth_mode = DFL_AUTH_MODE; |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 756 | opt.mfl_code = DFL_MFL_CODE; |
Manuel Pégourié-Gonnard | e117a8f | 2015-01-09 12:39:35 +0100 | [diff] [blame] | 757 | opt.trunc_hmac = DFL_TRUNC_HMAC; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 758 | opt.tickets = DFL_TICKETS; |
Manuel Pégourié-Gonnard | dbe1ee1 | 2014-02-21 09:18:13 +0100 | [diff] [blame] | 759 | opt.ticket_timeout = DFL_TICKET_TIMEOUT; |
Manuel Pégourié-Gonnard | 4c88345 | 2014-02-20 21:32:41 +0100 | [diff] [blame] | 760 | opt.cache_max = DFL_CACHE_MAX; |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 761 | opt.cache_timeout = DFL_CACHE_TIMEOUT; |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 762 | opt.sni = DFL_SNI; |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 763 | opt.alpn_string = DFL_ALPN_STRING; |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 764 | opt.dhm_file = DFL_DHM_FILE; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 765 | opt.extended_ms = DFL_EXTENDED_MS; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 766 | opt.etm = DFL_ETM; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 767 | |
| 768 | for( i = 1; i < argc; i++ ) |
| 769 | { |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 770 | p = argv[i]; |
| 771 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 772 | goto usage; |
| 773 | *q++ = '\0'; |
| 774 | |
| 775 | if( strcmp( p, "server_port" ) == 0 ) |
| 776 | { |
| 777 | opt.server_port = atoi( q ); |
| 778 | if( opt.server_port < 1 || opt.server_port > 65535 ) |
| 779 | goto usage; |
| 780 | } |
Manuel Pégourié-Gonnard | 18d31f8 | 2013-12-13 16:21:41 +0100 | [diff] [blame] | 781 | else if( strcmp( p, "server_addr" ) == 0 ) |
| 782 | opt.server_addr = q; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 783 | else if( strcmp( p, "debug_level" ) == 0 ) |
| 784 | { |
| 785 | opt.debug_level = atoi( q ); |
| 786 | if( opt.debug_level < 0 || opt.debug_level > 65535 ) |
| 787 | goto usage; |
| 788 | } |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 789 | else if( strcmp( p, "nbio" ) == 0 ) |
| 790 | { |
| 791 | opt.nbio = atoi( q ); |
| 792 | if( opt.nbio < 0 || opt.nbio > 2 ) |
| 793 | goto usage; |
| 794 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 795 | else if( strcmp( p, "ca_file" ) == 0 ) |
| 796 | opt.ca_file = q; |
| 797 | else if( strcmp( p, "ca_path" ) == 0 ) |
| 798 | opt.ca_path = q; |
| 799 | else if( strcmp( p, "crt_file" ) == 0 ) |
| 800 | opt.crt_file = q; |
| 801 | else if( strcmp( p, "key_file" ) == 0 ) |
| 802 | opt.key_file = q; |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 803 | else if( strcmp( p, "crt_file2" ) == 0 ) |
| 804 | opt.crt_file2 = q; |
| 805 | else if( strcmp( p, "key_file2" ) == 0 ) |
| 806 | opt.key_file2 = q; |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 807 | else if( strcmp( p, "dhm_file" ) == 0 ) |
| 808 | opt.dhm_file = q; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 809 | else if( strcmp( p, "psk" ) == 0 ) |
| 810 | opt.psk = q; |
| 811 | else if( strcmp( p, "psk_identity" ) == 0 ) |
| 812 | opt.psk_identity = q; |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 813 | else if( strcmp( p, "psk_list" ) == 0 ) |
| 814 | opt.psk_list = q; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 815 | else if( strcmp( p, "force_ciphersuite" ) == 0 ) |
| 816 | { |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 817 | opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q ); |
| 818 | |
Manuel Pégourié-Gonnard | 8de259b | 2014-06-11 14:19:06 +0200 | [diff] [blame] | 819 | if( opt.force_ciphersuite[0] == 0 ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 820 | { |
| 821 | ret = 2; |
| 822 | goto usage; |
| 823 | } |
| 824 | opt.force_ciphersuite[1] = 0; |
| 825 | } |
Manuel Pégourié-Gonnard | 6dc0781 | 2014-06-11 13:50:34 +0200 | [diff] [blame] | 826 | else if( strcmp( p, "version_suites" ) == 0 ) |
| 827 | opt.version_suites = q; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 828 | else if( strcmp( p, "renegotiation" ) == 0 ) |
| 829 | { |
| 830 | opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED : |
| 831 | SSL_RENEGOTIATION_DISABLED; |
| 832 | } |
| 833 | else if( strcmp( p, "allow_legacy" ) == 0 ) |
| 834 | { |
Manuel Pégourié-Gonnard | 85d915b | 2014-11-03 20:10:36 +0100 | [diff] [blame] | 835 | switch( atoi( q ) ) |
| 836 | { |
| 837 | case -1: opt.allow_legacy = SSL_LEGACY_BREAK_HANDSHAKE; break; |
| 838 | case 0: opt.allow_legacy = SSL_LEGACY_NO_RENEGOTIATION; break; |
| 839 | case 1: opt.allow_legacy = SSL_LEGACY_ALLOW_RENEGOTIATION; break; |
| 840 | default: goto usage; |
| 841 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 842 | } |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 843 | else if( strcmp( p, "renegotiate" ) == 0 ) |
| 844 | { |
| 845 | opt.renegotiate = atoi( q ); |
| 846 | if( opt.renegotiate < 0 || opt.renegotiate > 1 ) |
| 847 | goto usage; |
| 848 | } |
Manuel Pégourié-Gonnard | fae355e | 2014-07-04 14:32:27 +0200 | [diff] [blame] | 849 | else if( strcmp( p, "renego_delay" ) == 0 ) |
| 850 | { |
| 851 | opt.renego_delay = atoi( q ); |
| 852 | } |
Manuel Pégourié-Gonnard | 590f416 | 2014-11-05 14:23:03 +0100 | [diff] [blame] | 853 | else if( strcmp( p, "renego_period" ) == 0 ) |
| 854 | { |
| 855 | opt.renego_period = atoi( q ); |
| 856 | if( opt.renego_period < 2 || opt.renego_period > 255 ) |
| 857 | goto usage; |
| 858 | } |
Manuel Pégourié-Gonnard | 67686c4 | 2014-08-15 11:17:27 +0200 | [diff] [blame] | 859 | else if( strcmp( p, "exchanges" ) == 0 ) |
| 860 | { |
| 861 | opt.exchanges = atoi( q ); |
| 862 | if( opt.exchanges < 1 ) |
| 863 | goto usage; |
| 864 | } |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 865 | else if( strcmp( p, "min_version" ) == 0 ) |
| 866 | { |
| 867 | if( strcmp( q, "ssl3" ) == 0 ) |
| 868 | opt.min_version = SSL_MINOR_VERSION_0; |
| 869 | else if( strcmp( q, "tls1" ) == 0 ) |
| 870 | opt.min_version = SSL_MINOR_VERSION_1; |
| 871 | else if( strcmp( q, "tls1_1" ) == 0 ) |
| 872 | opt.min_version = SSL_MINOR_VERSION_2; |
| 873 | else if( strcmp( q, "tls1_2" ) == 0 ) |
| 874 | opt.min_version = SSL_MINOR_VERSION_3; |
| 875 | else |
| 876 | goto usage; |
| 877 | } |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 878 | else if( strcmp( p, "max_version" ) == 0 ) |
| 879 | { |
| 880 | if( strcmp( q, "ssl3" ) == 0 ) |
| 881 | opt.max_version = SSL_MINOR_VERSION_0; |
| 882 | else if( strcmp( q, "tls1" ) == 0 ) |
| 883 | opt.max_version = SSL_MINOR_VERSION_1; |
| 884 | else if( strcmp( q, "tls1_1" ) == 0 ) |
| 885 | opt.max_version = SSL_MINOR_VERSION_2; |
| 886 | else if( strcmp( q, "tls1_2" ) == 0 ) |
| 887 | opt.max_version = SSL_MINOR_VERSION_3; |
| 888 | else |
| 889 | goto usage; |
| 890 | } |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 891 | else if( strcmp( p, "arc4" ) == 0 ) |
| 892 | { |
| 893 | switch( atoi( q ) ) |
| 894 | { |
| 895 | case 0: opt.arc4 = SSL_ARC4_DISABLED; break; |
| 896 | case 1: opt.arc4 = SSL_ARC4_ENABLED; break; |
| 897 | default: goto usage; |
| 898 | } |
| 899 | } |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 900 | else if( strcmp( p, "force_version" ) == 0 ) |
| 901 | { |
| 902 | if( strcmp( q, "ssl3" ) == 0 ) |
| 903 | { |
| 904 | opt.min_version = SSL_MINOR_VERSION_0; |
| 905 | opt.max_version = SSL_MINOR_VERSION_0; |
| 906 | } |
| 907 | else if( strcmp( q, "tls1" ) == 0 ) |
| 908 | { |
| 909 | opt.min_version = SSL_MINOR_VERSION_1; |
| 910 | opt.max_version = SSL_MINOR_VERSION_1; |
| 911 | } |
| 912 | else if( strcmp( q, "tls1_1" ) == 0 ) |
| 913 | { |
| 914 | opt.min_version = SSL_MINOR_VERSION_2; |
| 915 | opt.max_version = SSL_MINOR_VERSION_2; |
| 916 | } |
| 917 | else if( strcmp( q, "tls1_2" ) == 0 ) |
| 918 | { |
| 919 | opt.min_version = SSL_MINOR_VERSION_3; |
| 920 | opt.max_version = SSL_MINOR_VERSION_3; |
| 921 | } |
| 922 | else |
| 923 | goto usage; |
| 924 | } |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 925 | else if( strcmp( p, "auth_mode" ) == 0 ) |
| 926 | { |
| 927 | if( strcmp( q, "none" ) == 0 ) |
| 928 | opt.auth_mode = SSL_VERIFY_NONE; |
| 929 | else if( strcmp( q, "optional" ) == 0 ) |
| 930 | opt.auth_mode = SSL_VERIFY_OPTIONAL; |
| 931 | else if( strcmp( q, "required" ) == 0 ) |
| 932 | opt.auth_mode = SSL_VERIFY_REQUIRED; |
| 933 | else |
| 934 | goto usage; |
| 935 | } |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 936 | else if( strcmp( p, "max_frag_len" ) == 0 ) |
| 937 | { |
| 938 | if( strcmp( q, "512" ) == 0 ) |
| 939 | opt.mfl_code = SSL_MAX_FRAG_LEN_512; |
| 940 | else if( strcmp( q, "1024" ) == 0 ) |
| 941 | opt.mfl_code = SSL_MAX_FRAG_LEN_1024; |
| 942 | else if( strcmp( q, "2048" ) == 0 ) |
| 943 | opt.mfl_code = SSL_MAX_FRAG_LEN_2048; |
| 944 | else if( strcmp( q, "4096" ) == 0 ) |
| 945 | opt.mfl_code = SSL_MAX_FRAG_LEN_4096; |
| 946 | else |
| 947 | goto usage; |
| 948 | } |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 949 | else if( strcmp( p, "alpn" ) == 0 ) |
| 950 | { |
| 951 | opt.alpn_string = q; |
| 952 | } |
Manuel Pégourié-Gonnard | e117a8f | 2015-01-09 12:39:35 +0100 | [diff] [blame] | 953 | else if( strcmp( p, "trunc_hmac" ) == 0 ) |
| 954 | { |
| 955 | switch( atoi( q ) ) |
| 956 | { |
| 957 | case 0: opt.trunc_hmac = SSL_TRUNC_HMAC_DISABLED; break; |
| 958 | case 1: opt.trunc_hmac = SSL_TRUNC_HMAC_ENABLED; break; |
| 959 | default: goto usage; |
| 960 | } |
| 961 | } |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 962 | else if( strcmp( p, "extended_ms" ) == 0 ) |
| 963 | { |
| 964 | switch( atoi( q ) ) |
| 965 | { |
| 966 | case 0: opt.extended_ms = SSL_EXTENDED_MS_DISABLED; break; |
| 967 | case 1: opt.extended_ms = SSL_EXTENDED_MS_ENABLED; break; |
| 968 | default: goto usage; |
| 969 | } |
| 970 | } |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 971 | else if( strcmp( p, "etm" ) == 0 ) |
| 972 | { |
| 973 | switch( atoi( q ) ) |
| 974 | { |
| 975 | case 0: opt.etm = SSL_ETM_DISABLED; break; |
| 976 | case 1: opt.etm = SSL_ETM_ENABLED; break; |
| 977 | default: goto usage; |
| 978 | } |
| 979 | } |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 980 | else if( strcmp( p, "tickets" ) == 0 ) |
| 981 | { |
| 982 | opt.tickets = atoi( q ); |
| 983 | if( opt.tickets < 0 || opt.tickets > 1 ) |
| 984 | goto usage; |
| 985 | } |
Manuel Pégourié-Gonnard | dbe1ee1 | 2014-02-21 09:18:13 +0100 | [diff] [blame] | 986 | else if( strcmp( p, "ticket_timeout" ) == 0 ) |
| 987 | { |
| 988 | opt.ticket_timeout = atoi( q ); |
| 989 | if( opt.ticket_timeout < 0 ) |
| 990 | goto usage; |
| 991 | } |
Manuel Pégourié-Gonnard | 4c88345 | 2014-02-20 21:32:41 +0100 | [diff] [blame] | 992 | else if( strcmp( p, "cache_max" ) == 0 ) |
| 993 | { |
| 994 | opt.cache_max = atoi( q ); |
| 995 | if( opt.cache_max < 0 ) |
| 996 | goto usage; |
| 997 | } |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 998 | else if( strcmp( p, "cache_timeout" ) == 0 ) |
| 999 | { |
| 1000 | opt.cache_timeout = atoi( q ); |
| 1001 | if( opt.cache_timeout < 0 ) |
| 1002 | goto usage; |
| 1003 | } |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 1004 | else if( strcmp( p, "sni" ) == 0 ) |
| 1005 | { |
| 1006 | opt.sni = q; |
| 1007 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1008 | else |
| 1009 | goto usage; |
| 1010 | } |
| 1011 | |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 1012 | #if defined(POLARSSL_DEBUG_C) |
| 1013 | debug_set_threshold( opt.debug_level ); |
| 1014 | #endif |
| 1015 | |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1016 | if( opt.force_ciphersuite[0] > 0 ) |
| 1017 | { |
| 1018 | const ssl_ciphersuite_t *ciphersuite_info; |
| 1019 | ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] ); |
| 1020 | |
Paul Bakker | 5b55b79 | 2013-07-19 13:43:43 +0200 | [diff] [blame] | 1021 | if( opt.max_version != -1 && |
| 1022 | ciphersuite_info->min_minor_ver > opt.max_version ) |
| 1023 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1024 | polarssl_printf("forced ciphersuite not allowed with this protocol version\n"); |
Paul Bakker | 5b55b79 | 2013-07-19 13:43:43 +0200 | [diff] [blame] | 1025 | ret = 2; |
| 1026 | goto usage; |
| 1027 | } |
| 1028 | if( opt.min_version != -1 && |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1029 | ciphersuite_info->max_minor_ver < opt.min_version ) |
| 1030 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1031 | polarssl_printf("forced ciphersuite not allowed with this protocol version\n"); |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1032 | ret = 2; |
| 1033 | goto usage; |
| 1034 | } |
Paul Bakker | 5b55b79 | 2013-07-19 13:43:43 +0200 | [diff] [blame] | 1035 | if( opt.max_version > ciphersuite_info->max_minor_ver ) |
| 1036 | opt.max_version = ciphersuite_info->max_minor_ver; |
| 1037 | if( opt.min_version < ciphersuite_info->min_minor_ver ) |
| 1038 | opt.min_version = ciphersuite_info->min_minor_ver; |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1039 | } |
| 1040 | |
Manuel Pégourié-Gonnard | 6dc0781 | 2014-06-11 13:50:34 +0200 | [diff] [blame] | 1041 | if( opt.version_suites != NULL ) |
| 1042 | { |
| 1043 | const char *name[4] = { 0 }; |
| 1044 | |
| 1045 | /* Parse 4-element coma-separated list */ |
| 1046 | for( i = 0, p = (char *) opt.version_suites; |
| 1047 | i < 4 && *p != '\0'; |
| 1048 | i++ ) |
| 1049 | { |
| 1050 | name[i] = p; |
| 1051 | |
| 1052 | /* Terminate the current string and move on to next one */ |
| 1053 | while( *p != ',' && *p != '\0' ) |
| 1054 | p++; |
| 1055 | if( *p == ',' ) |
| 1056 | *p++ = '\0'; |
| 1057 | } |
| 1058 | |
| 1059 | if( i != 4 ) |
| 1060 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1061 | polarssl_printf( "too few values for version_suites\n" ); |
Manuel Pégourié-Gonnard | 6dc0781 | 2014-06-11 13:50:34 +0200 | [diff] [blame] | 1062 | ret = 1; |
| 1063 | goto exit; |
| 1064 | } |
| 1065 | |
| 1066 | memset( version_suites, 0, sizeof( version_suites ) ); |
| 1067 | |
| 1068 | /* Get the suites identifiers from their name */ |
| 1069 | for( i = 0; i < 4; i++ ) |
| 1070 | { |
| 1071 | version_suites[i][0] = ssl_get_ciphersuite_id( name[i] ); |
| 1072 | |
| 1073 | if( version_suites[i][0] == 0 ) |
| 1074 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1075 | polarssl_printf( "unknown ciphersuite: '%s'\n", name[i] ); |
Manuel Pégourié-Gonnard | 6dc0781 | 2014-06-11 13:50:34 +0200 | [diff] [blame] | 1076 | ret = 2; |
| 1077 | goto usage; |
| 1078 | } |
| 1079 | } |
| 1080 | } |
| 1081 | |
Manuel Pégourié-Gonnard | 8a3c64d | 2013-10-14 19:54:10 +0200 | [diff] [blame] | 1082 | #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1083 | /* |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 1084 | * Unhexify the pre-shared key and parse the list if any given |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1085 | */ |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 1086 | if( unhexify( psk, opt.psk, &psk_len ) != 0 ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1087 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1088 | polarssl_printf( "pre-shared key not valid hex\n" ); |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 1089 | goto exit; |
| 1090 | } |
| 1091 | |
| 1092 | if( opt.psk_list != NULL ) |
| 1093 | { |
| 1094 | if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1095 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1096 | polarssl_printf( "psk_list invalid" ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1097 | goto exit; |
| 1098 | } |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1099 | } |
Manuel Pégourié-Gonnard | 8a3c64d | 2013-10-14 19:54:10 +0200 | [diff] [blame] | 1100 | #endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1101 | |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1102 | #if defined(POLARSSL_SSL_ALPN) |
| 1103 | if( opt.alpn_string != NULL ) |
| 1104 | { |
| 1105 | p = (char *) opt.alpn_string; |
| 1106 | i = 0; |
| 1107 | |
| 1108 | /* Leave room for a final NULL in alpn_list */ |
| 1109 | while( i < (int) sizeof alpn_list - 1 && *p != '\0' ) |
| 1110 | { |
| 1111 | alpn_list[i++] = p; |
| 1112 | |
| 1113 | /* Terminate the current string and move on to next one */ |
| 1114 | while( *p != ',' && *p != '\0' ) |
| 1115 | p++; |
| 1116 | if( *p == ',' ) |
| 1117 | *p++ = '\0'; |
| 1118 | } |
| 1119 | } |
| 1120 | #endif /* POLARSSL_SSL_ALPN */ |
| 1121 | |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1122 | /* |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1123 | * 0. Initialize the RNG and the session data |
| 1124 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1125 | polarssl_printf( "\n . Seeding the random number generator..." ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1126 | fflush( stdout ); |
| 1127 | |
| 1128 | entropy_init( &entropy ); |
| 1129 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 1130 | (const unsigned char *) pers, |
| 1131 | strlen( pers ) ) ) != 0 ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1132 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1133 | polarssl_printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1134 | goto exit; |
| 1135 | } |
| 1136 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1137 | polarssl_printf( " ok\n" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1138 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 1139 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1140 | /* |
| 1141 | * 1.1. Load the trusted CA |
| 1142 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1143 | polarssl_printf( " . Loading the CA root certificate ..." ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1144 | fflush( stdout ); |
| 1145 | |
| 1146 | #if defined(POLARSSL_FS_IO) |
| 1147 | if( strlen( opt.ca_path ) ) |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1148 | if( strcmp( opt.ca_path, "none" ) == 0 ) |
| 1149 | ret = 0; |
| 1150 | else |
| 1151 | ret = x509_crt_parse_path( &cacert, opt.ca_path ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1152 | else if( strlen( opt.ca_file ) ) |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1153 | if( strcmp( opt.ca_file, "none" ) == 0 ) |
| 1154 | ret = 0; |
| 1155 | else |
| 1156 | ret = x509_crt_parse_file( &cacert, opt.ca_file ); |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 1157 | else |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1158 | #endif |
| 1159 | #if defined(POLARSSL_CERTS_C) |
Manuel Pégourié-Gonnard | 641de71 | 2013-09-25 13:23:33 +0200 | [diff] [blame] | 1160 | ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list, |
| 1161 | strlen( test_ca_list ) ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1162 | #else |
| 1163 | { |
| 1164 | ret = 1; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1165 | polarssl_printf("POLARSSL_CERTS_C not defined."); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1166 | } |
| 1167 | #endif |
| 1168 | if( ret < 0 ) |
| 1169 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1170 | polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1171 | goto exit; |
| 1172 | } |
| 1173 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1174 | polarssl_printf( " ok (%d skipped)\n", ret ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1175 | |
| 1176 | /* |
| 1177 | * 1.2. Load own certificate and private key |
| 1178 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1179 | polarssl_printf( " . Loading the server cert. and key..." ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1180 | fflush( stdout ); |
| 1181 | |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 1182 | #if defined(POLARSSL_FS_IO) |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1183 | 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] | 1184 | { |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1185 | key_cert_init++; |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1186 | if( ( ret = x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 ) |
| 1187 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1188 | polarssl_printf( " failed\n ! x509_crt_parse_file returned -0x%x\n\n", |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1189 | -ret ); |
| 1190 | goto exit; |
| 1191 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 1192 | } |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1193 | 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] | 1194 | { |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1195 | key_cert_init++; |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1196 | if( ( ret = pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 ) |
| 1197 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1198 | polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%x\n\n", -ret ); |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1199 | goto exit; |
| 1200 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 1201 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1202 | if( key_cert_init == 1 ) |
| 1203 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1204 | polarssl_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] | 1205 | goto exit; |
| 1206 | } |
| 1207 | |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1208 | 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] | 1209 | { |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1210 | key_cert_init2++; |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1211 | if( ( ret = x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 ) |
| 1212 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1213 | polarssl_printf( " failed\n ! x509_crt_parse_file(2) returned -0x%x\n\n", |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1214 | -ret ); |
| 1215 | goto exit; |
| 1216 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 1217 | } |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1218 | 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] | 1219 | { |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1220 | key_cert_init2++; |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1221 | if( ( ret = pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 ) |
| 1222 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1223 | polarssl_printf( " failed\n ! pk_parse_keyfile(2) returned -0x%x\n\n", |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1224 | -ret ); |
| 1225 | goto exit; |
| 1226 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 1227 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1228 | if( key_cert_init2 == 1 ) |
| 1229 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1230 | polarssl_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] | 1231 | goto exit; |
| 1232 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 1233 | #endif |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1234 | if( key_cert_init == 0 && |
| 1235 | strcmp( opt.crt_file, "none" ) != 0 && |
| 1236 | strcmp( opt.key_file, "none" ) != 0 && |
| 1237 | key_cert_init2 == 0 && |
| 1238 | strcmp( opt.crt_file2, "none" ) != 0 && |
| 1239 | strcmp( opt.key_file2, "none" ) != 0 ) |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 1240 | { |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1241 | #if !defined(POLARSSL_CERTS_C) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1242 | polarssl_printf( "Not certificated or key provided, and \n" |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1243 | "POLARSSL_CERTS_C not defined!\n" ); |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 1244 | goto exit; |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1245 | #else |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1246 | #if defined(POLARSSL_RSA_C) |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1247 | if( ( ret = x509_crt_parse( &srvcert, |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1248 | (const unsigned char *) test_srv_crt_rsa, |
| 1249 | strlen( test_srv_crt_rsa ) ) ) != 0 ) |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1250 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1251 | polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret ); |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1252 | goto exit; |
| 1253 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1254 | if( ( ret = pk_parse_key( &pkey, |
| 1255 | (const unsigned char *) test_srv_key_rsa, |
| 1256 | strlen( test_srv_key_rsa ), NULL, 0 ) ) != 0 ) |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1257 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1258 | polarssl_printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret ); |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1259 | goto exit; |
| 1260 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1261 | key_cert_init = 2; |
| 1262 | #endif /* POLARSSL_RSA_C */ |
| 1263 | #if defined(POLARSSL_ECDSA_C) |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1264 | if( ( ret = x509_crt_parse( &srvcert2, |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1265 | (const unsigned char *) test_srv_crt_ec, |
| 1266 | strlen( test_srv_crt_ec ) ) ) != 0 ) |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1267 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1268 | polarssl_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] | 1269 | goto exit; |
| 1270 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1271 | if( ( ret = pk_parse_key( &pkey2, |
| 1272 | (const unsigned char *) test_srv_key_ec, |
| 1273 | strlen( test_srv_key_ec ), NULL, 0 ) ) != 0 ) |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1274 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1275 | polarssl_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] | 1276 | goto exit; |
| 1277 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1278 | key_cert_init2 = 2; |
| 1279 | #endif /* POLARSSL_ECDSA_C */ |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 1280 | #endif /* POLARSSL_CERTS_C */ |
| 1281 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1282 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1283 | polarssl_printf( " ok\n" ); |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 1284 | #endif /* POLARSSL_X509_CRT_PARSE_C */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1285 | |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 1286 | #if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO) |
| 1287 | if( opt.dhm_file != NULL ) |
| 1288 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1289 | polarssl_printf( " . Loading DHM parameters..." ); |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 1290 | fflush( stdout ); |
| 1291 | |
| 1292 | if( ( ret = dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 ) |
| 1293 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1294 | polarssl_printf( " failed\n ! dhm_parse_dhmfile returned -0x%04X\n\n", |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 1295 | -ret ); |
| 1296 | goto exit; |
| 1297 | } |
| 1298 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1299 | polarssl_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 1300 | } |
| 1301 | #endif |
| 1302 | |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 1303 | #if defined(POLARSSL_SNI) |
| 1304 | if( opt.sni != NULL ) |
| 1305 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1306 | polarssl_printf( " . Setting up SNI information..." ); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 1307 | fflush( stdout ); |
| 1308 | |
| 1309 | if( ( sni_info = sni_parse( opt.sni ) ) == NULL ) |
| 1310 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1311 | polarssl_printf( " failed\n" ); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 1312 | goto exit; |
| 1313 | } |
| 1314 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1315 | polarssl_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 1316 | } |
| 1317 | #endif /* POLARSSL_SNI */ |
| 1318 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1319 | /* |
| 1320 | * 2. Setup the listening TCP socket |
| 1321 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1322 | polarssl_printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1323 | fflush( stdout ); |
| 1324 | |
Manuel Pégourié-Gonnard | 18d31f8 | 2013-12-13 16:21:41 +0100 | [diff] [blame] | 1325 | if( ( ret = net_bind( &listen_fd, opt.server_addr, |
| 1326 | opt.server_port ) ) != 0 ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1327 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1328 | polarssl_printf( " failed\n ! net_bind returned -0x%x\n\n", -ret ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1329 | goto exit; |
| 1330 | } |
| 1331 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1332 | polarssl_printf( " ok\n" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1333 | |
| 1334 | /* |
| 1335 | * 3. Setup stuff |
| 1336 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1337 | polarssl_printf( " . Setting up the SSL/TLS structure..." ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1338 | fflush( stdout ); |
| 1339 | |
| 1340 | if( ( ret = ssl_init( &ssl ) ) != 0 ) |
| 1341 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1342 | polarssl_printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1343 | goto exit; |
| 1344 | } |
| 1345 | |
| 1346 | ssl_set_endpoint( &ssl, SSL_IS_SERVER ); |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 1347 | ssl_set_authmode( &ssl, opt.auth_mode ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1348 | |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 1349 | #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1350 | if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 ) |
| 1351 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1352 | 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] | 1353 | goto exit; |
| 1354 | }; |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 1355 | #endif |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 1356 | |
Manuel Pégourié-Gonnard | e117a8f | 2015-01-09 12:39:35 +0100 | [diff] [blame] | 1357 | #if defined(POLARSSL_SSL_TRUNCATED_HMAC) |
| 1358 | if( opt.trunc_hmac != DFL_TRUNC_HMAC ) |
| 1359 | ssl_set_truncated_hmac( &ssl, opt.trunc_hmac ); |
| 1360 | #endif |
| 1361 | |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1362 | #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) |
| 1363 | if( opt.extended_ms != DFL_EXTENDED_MS ) |
| 1364 | ssl_set_extended_master_secret( &ssl, opt.extended_ms ); |
| 1365 | #endif |
| 1366 | |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1367 | #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) |
| 1368 | if( opt.etm != DFL_ETM ) |
| 1369 | ssl_set_encrypt_then_mac( &ssl, opt.etm ); |
| 1370 | #endif |
| 1371 | |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1372 | #if defined(POLARSSL_SSL_ALPN) |
| 1373 | if( opt.alpn_string != NULL ) |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1374 | if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 ) |
| 1375 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1376 | 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] | 1377 | goto exit; |
| 1378 | } |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1379 | #endif |
| 1380 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1381 | ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg ); |
| 1382 | ssl_set_dbg( &ssl, my_debug, stdout ); |
| 1383 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1384 | #if defined(POLARSSL_SSL_CACHE_C) |
Manuel Pégourié-Gonnard | 4c88345 | 2014-02-20 21:32:41 +0100 | [diff] [blame] | 1385 | if( opt.cache_max != -1 ) |
| 1386 | ssl_cache_set_max_entries( &cache, opt.cache_max ); |
| 1387 | |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 1388 | if( opt.cache_timeout != -1 ) |
| 1389 | ssl_cache_set_timeout( &cache, opt.cache_timeout ); |
| 1390 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1391 | ssl_set_session_cache( &ssl, ssl_cache_get, &cache, |
| 1392 | ssl_cache_set, &cache ); |
| 1393 | #endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1394 | |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 1395 | #if defined(POLARSSL_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1396 | if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 ) |
| 1397 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1398 | 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] | 1399 | goto exit; |
| 1400 | } |
Manuel Pégourié-Gonnard | dbe1ee1 | 2014-02-21 09:18:13 +0100 | [diff] [blame] | 1401 | |
| 1402 | if( opt.ticket_timeout != -1 ) |
| 1403 | ssl_set_session_ticket_lifetime( &ssl, opt.ticket_timeout ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 1404 | #endif |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 1405 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1406 | if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1407 | ssl_set_ciphersuites( &ssl, opt.force_ciphersuite ); |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 1408 | else |
| 1409 | ssl_set_arc4_support( &ssl, opt.arc4 ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1410 | |
Manuel Pégourié-Gonnard | 6dc0781 | 2014-06-11 13:50:34 +0200 | [diff] [blame] | 1411 | if( opt.version_suites != NULL ) |
| 1412 | { |
| 1413 | ssl_set_ciphersuites_for_version( &ssl, version_suites[0], |
| 1414 | SSL_MAJOR_VERSION_3, |
| 1415 | SSL_MINOR_VERSION_0 ); |
| 1416 | ssl_set_ciphersuites_for_version( &ssl, version_suites[1], |
| 1417 | SSL_MAJOR_VERSION_3, |
| 1418 | SSL_MINOR_VERSION_1 ); |
| 1419 | ssl_set_ciphersuites_for_version( &ssl, version_suites[2], |
| 1420 | SSL_MAJOR_VERSION_3, |
| 1421 | SSL_MINOR_VERSION_2 ); |
| 1422 | ssl_set_ciphersuites_for_version( &ssl, version_suites[3], |
| 1423 | SSL_MAJOR_VERSION_3, |
| 1424 | SSL_MINOR_VERSION_3 ); |
| 1425 | } |
| 1426 | |
Manuel Pégourié-Gonnard | 85d915b | 2014-11-03 20:10:36 +0100 | [diff] [blame] | 1427 | if( opt.allow_legacy != DFL_ALLOW_LEGACY ) |
| 1428 | ssl_legacy_renegotiation( &ssl, opt.allow_legacy ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1429 | #if defined(POLARSSL_SSL_RENEGOTIATION) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1430 | ssl_set_renegotiation( &ssl, opt.renegotiation ); |
Manuel Pégourié-Gonnard | 590f416 | 2014-11-05 14:23:03 +0100 | [diff] [blame] | 1431 | |
Manuel Pégourié-Gonnard | fae355e | 2014-07-04 14:32:27 +0200 | [diff] [blame] | 1432 | if( opt.renego_delay != DFL_RENEGO_DELAY ) |
| 1433 | ssl_set_renegotiation_enforced( &ssl, opt.renego_delay ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1434 | |
Manuel Pégourié-Gonnard | 590f416 | 2014-11-05 14:23:03 +0100 | [diff] [blame] | 1435 | if( opt.renego_period != DFL_RENEGO_PERIOD ) |
| 1436 | { |
| 1437 | renego_period[7] = opt.renego_period; |
| 1438 | ssl_set_renegotiation_period( &ssl, renego_period ); |
| 1439 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1440 | #endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1441 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 1442 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1443 | if( strcmp( opt.ca_path, "none" ) != 0 && |
| 1444 | strcmp( opt.ca_file, "none" ) != 0 ) |
| 1445 | { |
| 1446 | ssl_set_ca_chain( &ssl, &cacert, NULL, NULL ); |
| 1447 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1448 | if( key_cert_init ) |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1449 | if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 ) |
| 1450 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1451 | 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] | 1452 | goto exit; |
| 1453 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1454 | if( key_cert_init2 ) |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1455 | if( ( ret = ssl_set_own_cert( &ssl, &srvcert2, &pkey2 ) ) != 0 ) |
| 1456 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1457 | 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] | 1458 | goto exit; |
| 1459 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 1460 | #endif |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 1461 | |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 1462 | #if defined(POLARSSL_SNI) |
| 1463 | if( opt.sni != NULL ) |
| 1464 | ssl_set_sni( &ssl, sni_callback, sni_info ); |
| 1465 | #endif |
| 1466 | |
Manuel Pégourié-Gonnard | 8a3c64d | 2013-10-14 19:54:10 +0200 | [diff] [blame] | 1467 | #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Manuel Pégourié-Gonnard | dc019b9 | 2014-06-10 15:24:51 +0200 | [diff] [blame] | 1468 | if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 ) |
| 1469 | { |
| 1470 | ret = ssl_set_psk( &ssl, psk, psk_len, |
| 1471 | (const unsigned char *) opt.psk_identity, |
| 1472 | strlen( opt.psk_identity ) ); |
| 1473 | if( ret != 0 ) |
| 1474 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1475 | polarssl_printf( " failed\n ssl_set_psk returned -0x%04X\n\n", - ret ); |
Manuel Pégourié-Gonnard | dc019b9 | 2014-06-10 15:24:51 +0200 | [diff] [blame] | 1476 | goto exit; |
| 1477 | } |
| 1478 | } |
| 1479 | |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 1480 | if( opt.psk_list != NULL ) |
| 1481 | ssl_set_psk_cb( &ssl, psk_callback, psk_info ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 1482 | #endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1483 | |
| 1484 | #if defined(POLARSSL_DHM_C) |
Paul Bakker | 5d19f86 | 2012-09-28 07:33:00 +0000 | [diff] [blame] | 1485 | /* |
| 1486 | * Use different group than default DHM group |
| 1487 | */ |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 1488 | #if defined(POLARSSL_FS_IO) |
| 1489 | if( opt.dhm_file != NULL ) |
| 1490 | ret = ssl_set_dh_param_ctx( &ssl, &dhm ); |
| 1491 | else |
| 1492 | #endif |
| 1493 | ret = ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P, |
| 1494 | POLARSSL_DHM_RFC5114_MODP_2048_G ); |
| 1495 | |
| 1496 | if( ret != 0 ) |
| 1497 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1498 | polarssl_printf( " failed\n ssl_set_dh_param returned -0x%04X\n\n", - ret ); |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 1499 | goto exit; |
| 1500 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1501 | #endif |
| 1502 | |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1503 | if( opt.min_version != -1 ) |
| 1504 | ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version ); |
| 1505 | |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1506 | if( opt.max_version != -1 ) |
| 1507 | ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version ); |
| 1508 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1509 | polarssl_printf( " ok\n" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1510 | |
| 1511 | reset: |
Manuel Pégourié-Gonnard | 3a3066c | 2014-09-20 12:03:00 +0200 | [diff] [blame] | 1512 | #if !defined(_WIN32) |
| 1513 | if( received_sigterm ) |
| 1514 | { |
Manuel Pégourié-Gonnard | 7e81e70 | 2015-01-29 11:47:41 +0000 | [diff] [blame] | 1515 | polarssl_printf( " interrupted by SIGTERM\n" ); |
Manuel Pégourié-Gonnard | 3a3066c | 2014-09-20 12:03:00 +0200 | [diff] [blame] | 1516 | ret = 0; |
| 1517 | goto exit; |
| 1518 | } |
| 1519 | #endif |
| 1520 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1521 | #ifdef POLARSSL_ERROR_C |
| 1522 | if( ret != 0 ) |
| 1523 | { |
| 1524 | char error_buf[100]; |
Paul Bakker | 03a8a79 | 2013-06-30 12:18:08 +0200 | [diff] [blame] | 1525 | polarssl_strerror( ret, error_buf, 100 ); |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1526 | polarssl_printf("Last error was: %d - %s\n\n", ret, error_buf ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1527 | } |
| 1528 | #endif |
| 1529 | |
| 1530 | if( client_fd != -1 ) |
| 1531 | net_close( client_fd ); |
| 1532 | |
| 1533 | ssl_session_reset( &ssl ); |
| 1534 | |
| 1535 | /* |
| 1536 | * 3. Wait until a client connects |
| 1537 | */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1538 | client_fd = -1; |
| 1539 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1540 | polarssl_printf( " . Waiting for a remote connection ..." ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1541 | fflush( stdout ); |
| 1542 | |
| 1543 | if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 ) |
| 1544 | { |
Paul Bakker | c1283d3 | 2014-08-18 11:05:51 +0200 | [diff] [blame] | 1545 | #if !defined(_WIN32) |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 1546 | if( received_sigterm ) |
| 1547 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1548 | polarssl_printf( " interrupted by signal\n" ); |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 1549 | ret = 0; |
| 1550 | goto exit; |
| 1551 | } |
Paul Bakker | c1283d3 | 2014-08-18 11:05:51 +0200 | [diff] [blame] | 1552 | #endif |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 1553 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1554 | polarssl_printf( " failed\n ! net_accept returned -0x%x\n\n", -ret ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1555 | goto exit; |
| 1556 | } |
| 1557 | |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 1558 | if( opt.nbio > 0 ) |
| 1559 | ret = net_set_nonblock( client_fd ); |
| 1560 | else |
| 1561 | ret = net_set_block( client_fd ); |
| 1562 | if( ret != 0 ) |
| 1563 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1564 | 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] | 1565 | goto exit; |
| 1566 | } |
| 1567 | |
| 1568 | if( opt.nbio == 2 ) |
| 1569 | ssl_set_bio( &ssl, my_recv, &client_fd, my_send, &client_fd ); |
| 1570 | else |
| 1571 | ssl_set_bio( &ssl, net_recv, &client_fd, net_send, &client_fd ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1572 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1573 | polarssl_printf( " ok\n" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1574 | |
| 1575 | /* |
| 1576 | * 4. Handshake |
| 1577 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1578 | polarssl_printf( " . Performing the SSL/TLS handshake..." ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1579 | fflush( stdout ); |
| 1580 | |
| 1581 | while( ( ret = ssl_handshake( &ssl ) ) != 0 ) |
| 1582 | { |
| 1583 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 1584 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1585 | polarssl_printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret ); |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1586 | goto reset; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1587 | } |
| 1588 | } |
| 1589 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1590 | 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] | 1591 | ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1592 | |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1593 | #if defined(POLARSSL_SSL_ALPN) |
| 1594 | if( opt.alpn_string != NULL ) |
| 1595 | { |
| 1596 | const char *alp = ssl_get_alpn_protocol( &ssl ); |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1597 | polarssl_printf( " [ Application Layer Protocol is %s ]\n", |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1598 | alp ? alp : "(none)" ); |
| 1599 | } |
| 1600 | #endif |
| 1601 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 1602 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1603 | /* |
| 1604 | * 5. Verify the server certificate |
| 1605 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1606 | polarssl_printf( " . Verifying peer X.509 certificate..." ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1607 | |
| 1608 | if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 ) |
| 1609 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1610 | polarssl_printf( " failed\n" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1611 | |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 1612 | if( !ssl_get_peer_cert( &ssl ) ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1613 | polarssl_printf( " ! no client certificate sent\n" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1614 | |
| 1615 | if( ( ret & BADCERT_EXPIRED ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1616 | polarssl_printf( " ! client certificate has expired\n" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1617 | |
| 1618 | if( ( ret & BADCERT_REVOKED ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1619 | polarssl_printf( " ! client certificate has been revoked\n" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1620 | |
| 1621 | if( ( ret & BADCERT_NOT_TRUSTED ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1622 | polarssl_printf( " ! self-signed or not signed by a trusted CA\n" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1623 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1624 | polarssl_printf( "\n" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1625 | } |
| 1626 | else |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1627 | polarssl_printf( " ok\n" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1628 | |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 1629 | if( ssl_get_peer_cert( &ssl ) ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1630 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1631 | polarssl_printf( " . Peer certificate information ...\n" ); |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 1632 | x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ", |
| 1633 | ssl_get_peer_cert( &ssl ) ); |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1634 | polarssl_printf( "%s\n", buf ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1635 | } |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 1636 | #endif /* POLARSSL_X509_CRT_PARSE_C */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1637 | |
Manuel Pégourié-Gonnard | 6a0017b | 2015-01-22 10:33:29 +0000 | [diff] [blame] | 1638 | exchanges_left = opt.exchanges; |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 1639 | data_exchange: |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1640 | /* |
| 1641 | * 6. Read the HTTP Request |
| 1642 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1643 | polarssl_printf( " < Read from client:" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1644 | fflush( stdout ); |
| 1645 | |
| 1646 | do |
| 1647 | { |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 1648 | int terminated = 0; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1649 | len = sizeof( buf ) - 1; |
| 1650 | memset( buf, 0, sizeof( buf ) ); |
| 1651 | ret = ssl_read( &ssl, buf, len ); |
| 1652 | |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 1653 | if( ret == POLARSSL_ERR_NET_WANT_READ || |
| 1654 | ret == POLARSSL_ERR_NET_WANT_WRITE ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1655 | continue; |
| 1656 | |
| 1657 | if( ret <= 0 ) |
| 1658 | { |
| 1659 | switch( ret ) |
| 1660 | { |
| 1661 | case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY: |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1662 | polarssl_printf( " connection was closed gracefully\n" ); |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 1663 | goto close_notify; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1664 | |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 1665 | case 0: |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1666 | case POLARSSL_ERR_NET_CONN_RESET: |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1667 | polarssl_printf( " connection was reset by peer\n" ); |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 1668 | ret = POLARSSL_ERR_NET_CONN_RESET; |
| 1669 | goto reset; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1670 | |
| 1671 | default: |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1672 | polarssl_printf( " ssl_read returned -0x%x\n", -ret ); |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 1673 | goto reset; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1674 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Manuel Pégourié-Gonnard | e7a3b10 | 2014-06-11 18:21:20 +0200 | [diff] [blame] | 1677 | if( ssl_get_bytes_avail( &ssl ) == 0 ) |
| 1678 | { |
| 1679 | len = ret; |
| 1680 | buf[len] = '\0'; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1681 | polarssl_printf( " %d bytes read\n\n%s\n", len, (char *) buf ); |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 1682 | |
| 1683 | /* End of message should be detected according to the syntax of the |
| 1684 | * application protocol (eg HTTP), just use a dummy test here. */ |
| 1685 | if( buf[len - 1] == '\n' ) |
| 1686 | terminated = 1; |
Manuel Pégourié-Gonnard | e7a3b10 | 2014-06-11 18:21:20 +0200 | [diff] [blame] | 1687 | } |
| 1688 | else |
| 1689 | { |
| 1690 | int extra_len, ori_len; |
| 1691 | unsigned char *larger_buf; |
| 1692 | |
| 1693 | ori_len = ret; |
| 1694 | extra_len = ssl_get_bytes_avail( &ssl ); |
| 1695 | |
| 1696 | larger_buf = polarssl_malloc( ori_len + extra_len + 1 ); |
| 1697 | if( larger_buf == NULL ) |
| 1698 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1699 | polarssl_printf( " ! memory allocation failed\n" ); |
Manuel Pégourié-Gonnard | e7a3b10 | 2014-06-11 18:21:20 +0200 | [diff] [blame] | 1700 | ret = 1; |
Manuel Pégourié-Gonnard | 250b1ca | 2014-08-15 10:59:03 +0200 | [diff] [blame] | 1701 | goto reset; |
Manuel Pégourié-Gonnard | e7a3b10 | 2014-06-11 18:21:20 +0200 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | memset( larger_buf, 0, ori_len + extra_len ); |
| 1705 | memcpy( larger_buf, buf, ori_len ); |
| 1706 | |
Manuel Pégourié-Gonnard | 95c0a63 | 2014-06-11 18:32:36 +0200 | [diff] [blame] | 1707 | /* This read should never fail and get the whole cached data */ |
Manuel Pégourié-Gonnard | e7a3b10 | 2014-06-11 18:21:20 +0200 | [diff] [blame] | 1708 | ret = ssl_read( &ssl, larger_buf + ori_len, extra_len ); |
Manuel Pégourié-Gonnard | 95c0a63 | 2014-06-11 18:32:36 +0200 | [diff] [blame] | 1709 | if( ret != extra_len || |
| 1710 | ssl_get_bytes_avail( &ssl ) != 0 ) |
Manuel Pégourié-Gonnard | e7a3b10 | 2014-06-11 18:21:20 +0200 | [diff] [blame] | 1711 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1712 | polarssl_printf( " ! ssl_read failed on cached data\n" ); |
Manuel Pégourié-Gonnard | e7a3b10 | 2014-06-11 18:21:20 +0200 | [diff] [blame] | 1713 | ret = 1; |
Manuel Pégourié-Gonnard | 250b1ca | 2014-08-15 10:59:03 +0200 | [diff] [blame] | 1714 | goto reset; |
Manuel Pégourié-Gonnard | e7a3b10 | 2014-06-11 18:21:20 +0200 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | larger_buf[ori_len + extra_len] = '\0'; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1718 | polarssl_printf( " %u bytes read (%u + %u)\n\n%s\n", |
Manuel Pégourié-Gonnard | 0669f27 | 2014-06-18 13:07:20 +0200 | [diff] [blame] | 1719 | ori_len + extra_len, ori_len, extra_len, |
| 1720 | (char *) larger_buf ); |
Manuel Pégourié-Gonnard | e7a3b10 | 2014-06-11 18:21:20 +0200 | [diff] [blame] | 1721 | |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 1722 | /* End of message should be detected according to the syntax of the |
| 1723 | * application protocol (eg HTTP), just use a dummy test here. */ |
| 1724 | if( larger_buf[ori_len + extra_len - 1] == '\n' ) |
| 1725 | terminated = 1; |
| 1726 | |
Manuel Pégourié-Gonnard | e7a3b10 | 2014-06-11 18:21:20 +0200 | [diff] [blame] | 1727 | polarssl_free( larger_buf ); |
| 1728 | } |
| 1729 | |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 1730 | if( terminated ) |
| 1731 | { |
| 1732 | ret = 0; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1733 | break; |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 1734 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1735 | } |
| 1736 | while( 1 ); |
| 1737 | |
| 1738 | /* |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 1739 | * 7a. Request renegotiation while client is waiting for input from us. |
| 1740 | * (only if we're going to exhange more data afterwards) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1741 | */ |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1742 | #if defined(POLARSSL_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6a0017b | 2015-01-22 10:33:29 +0000 | [diff] [blame] | 1743 | if( opt.renegotiate && exchanges_left > 1 ) |
Manuel Pégourié-Gonnard | 296e3b1 | 2014-08-19 12:59:03 +0200 | [diff] [blame] | 1744 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1745 | polarssl_printf( " . Requestion renegotiation..." ); |
Manuel Pégourié-Gonnard | 296e3b1 | 2014-08-19 12:59:03 +0200 | [diff] [blame] | 1746 | fflush( stdout ); |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 1747 | |
Manuel Pégourié-Gonnard | 296e3b1 | 2014-08-19 12:59:03 +0200 | [diff] [blame] | 1748 | while( ( ret = ssl_renegotiate( &ssl ) ) != 0 ) |
| 1749 | { |
| 1750 | if( ret != POLARSSL_ERR_NET_WANT_READ && |
| 1751 | ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 1752 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1753 | polarssl_printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | 296e3b1 | 2014-08-19 12:59:03 +0200 | [diff] [blame] | 1754 | goto reset; |
| 1755 | } |
| 1756 | } |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 1757 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1758 | polarssl_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | 296e3b1 | 2014-08-19 12:59:03 +0200 | [diff] [blame] | 1759 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1760 | #endif /* POLARSSL_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 296e3b1 | 2014-08-19 12:59:03 +0200 | [diff] [blame] | 1761 | |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 1762 | /* |
| 1763 | * 7. Write the 200 Response |
| 1764 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1765 | polarssl_printf( " > Write to client:" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1766 | fflush( stdout ); |
| 1767 | |
| 1768 | len = sprintf( (char *) buf, HTTP_RESPONSE, |
| 1769 | ssl_get_ciphersuite( &ssl ) ); |
| 1770 | |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 1771 | for( written = 0, frags = 0; written < len; written += ret, frags++ ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1772 | { |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 1773 | while( ( ret = ssl_write( &ssl, buf + written, len - written ) ) <= 0 ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1774 | { |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 1775 | if( ret == POLARSSL_ERR_NET_CONN_RESET ) |
| 1776 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1777 | polarssl_printf( " failed\n ! peer closed the connection\n\n" ); |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 1778 | goto reset; |
| 1779 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1780 | |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 1781 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 1782 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1783 | polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | 250b1ca | 2014-08-15 10:59:03 +0200 | [diff] [blame] | 1784 | goto reset; |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 1785 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1786 | } |
| 1787 | } |
| 1788 | |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 1789 | buf[written] = '\0'; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1790 | polarssl_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] | 1791 | ret = 0; |
Manuel Pégourié-Gonnard | f3dc2f6 | 2013-10-29 18:17:41 +0100 | [diff] [blame] | 1792 | |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 1793 | /* |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 1794 | * 7b. Continue doing data exchanges? |
| 1795 | */ |
Manuel Pégourié-Gonnard | 6a0017b | 2015-01-22 10:33:29 +0000 | [diff] [blame] | 1796 | if( --exchanges_left > 0 ) |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 1797 | goto data_exchange; |
| 1798 | |
| 1799 | /* |
| 1800 | * 8. Done, cleanly close the connection |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 1801 | */ |
| 1802 | close_notify: |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1803 | polarssl_printf( " . Closing the connection..." ); |
Manuel Pégourié-Gonnard | 6b0d268 | 2014-03-25 11:24:43 +0100 | [diff] [blame] | 1804 | |
Manuel Pégourié-Gonnard | 34377b1 | 2015-01-22 10:46:46 +0000 | [diff] [blame] | 1805 | /* No error checking, the connection might be closed already */ |
| 1806 | do ret = ssl_close_notify( &ssl ); |
| 1807 | while( ret == POLARSSL_ERR_NET_WANT_WRITE ); |
| 1808 | ret = 0; |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 1809 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1810 | polarssl_printf( " done\n" ); |
| 1811 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1812 | goto reset; |
| 1813 | |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 1814 | /* |
| 1815 | * Cleanup and exit |
| 1816 | */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1817 | exit: |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1818 | #ifdef POLARSSL_ERROR_C |
| 1819 | if( ret != 0 ) |
| 1820 | { |
| 1821 | char error_buf[100]; |
Paul Bakker | 03a8a79 | 2013-06-30 12:18:08 +0200 | [diff] [blame] | 1822 | polarssl_strerror( ret, error_buf, 100 ); |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1823 | polarssl_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1824 | } |
| 1825 | #endif |
| 1826 | |
Manuel Pégourié-Gonnard | 7e81e70 | 2015-01-29 11:47:41 +0000 | [diff] [blame] | 1827 | polarssl_printf( " . Cleaning up..." ); |
Manuel Pégourié-Gonnard | f29e5de | 2014-11-21 11:54:41 +0100 | [diff] [blame] | 1828 | fflush( stdout ); |
| 1829 | |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 1830 | if( client_fd != -1 ) |
| 1831 | net_close( client_fd ); |
| 1832 | |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 1833 | #if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO) |
| 1834 | dhm_free( &dhm ); |
| 1835 | #endif |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 1836 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 1837 | x509_crt_free( &cacert ); |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 1838 | x509_crt_free( &srvcert ); |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 1839 | pk_free( &pkey ); |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 1840 | x509_crt_free( &srvcert2 ); |
| 1841 | pk_free( &pkey2 ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 1842 | #endif |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 1843 | #if defined(POLARSSL_SNI) |
| 1844 | sni_free( sni_info ); |
| 1845 | #endif |
Manuel Pégourié-Gonnard | 4505ed3 | 2014-06-19 20:56:52 +0200 | [diff] [blame] | 1846 | #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 1847 | psk_free( psk_info ); |
| 1848 | #endif |
| 1849 | #if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO) |
| 1850 | dhm_free( &dhm ); |
| 1851 | #endif |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 1852 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1853 | ssl_free( &ssl ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 1854 | ctr_drbg_free( &ctr_drbg ); |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 1855 | entropy_free( &entropy ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1856 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1857 | #if defined(POLARSSL_SSL_CACHE_C) |
| 1858 | ssl_cache_free( &cache ); |
| 1859 | #endif |
| 1860 | |
Paul Bakker | 1337aff | 2013-09-29 14:45:34 +0200 | [diff] [blame] | 1861 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) |
| 1862 | #if defined(POLARSSL_MEMORY_DEBUG) |
Paul Bakker | 82024bf | 2013-07-04 11:52:32 +0200 | [diff] [blame] | 1863 | memory_buffer_alloc_status(); |
| 1864 | #endif |
Paul Bakker | 1337aff | 2013-09-29 14:45:34 +0200 | [diff] [blame] | 1865 | memory_buffer_alloc_free(); |
| 1866 | #endif |
Paul Bakker | 82024bf | 2013-07-04 11:52:32 +0200 | [diff] [blame] | 1867 | |
Manuel Pégourié-Gonnard | 7e81e70 | 2015-01-29 11:47:41 +0000 | [diff] [blame] | 1868 | polarssl_printf( " done.\n" ); |
Manuel Pégourié-Gonnard | f29e5de | 2014-11-21 11:54:41 +0100 | [diff] [blame] | 1869 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1870 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 1871 | polarssl_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1872 | fflush( stdout ); getchar(); |
| 1873 | #endif |
| 1874 | |
Paul Bakker | dbd79ca | 2013-07-24 16:28:35 +0200 | [diff] [blame] | 1875 | // Shell can not handle large exit numbers -> 1 for errors |
| 1876 | if( ret < 0 ) |
| 1877 | ret = 1; |
| 1878 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1879 | return( ret ); |
| 1880 | } |
| 1881 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C && |
| 1882 | POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C && |
| 1883 | POLARSSL_CTR_DRBG_C */ |