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