Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSL client with options |
| 3 | * |
Paul Bakker | 3c5ef71 | 2013-06-25 16:37:45 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, Brainspark B.V. |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 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 | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 26 | #include "polarssl/config.h" |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 27 | |
| 28 | #if defined(_WIN32) |
| 29 | #include <windows.h> |
| 30 | #endif |
| 31 | |
| 32 | #include <string.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <stdio.h> |
| 35 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 36 | #include "polarssl/net.h" |
| 37 | #include "polarssl/ssl.h" |
| 38 | #include "polarssl/entropy.h" |
| 39 | #include "polarssl/ctr_drbg.h" |
| 40 | #include "polarssl/certs.h" |
| 41 | #include "polarssl/x509.h" |
| 42 | #include "polarssl/error.h" |
| 43 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 44 | #if defined(POLARSSL_SSL_CACHE_C) |
| 45 | #include "polarssl/ssl_cache.h" |
| 46 | #endif |
| 47 | |
Paul Bakker | 82024bf | 2013-07-04 11:52:32 +0200 | [diff] [blame] | 48 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) |
| 49 | #include "polarssl/memory.h" |
| 50 | #endif |
| 51 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 52 | #define DFL_SERVER_PORT 4433 |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 53 | #define DFL_DEBUG_LEVEL 0 |
| 54 | #define DFL_CA_FILE "" |
| 55 | #define DFL_CA_PATH "" |
| 56 | #define DFL_CRT_FILE "" |
| 57 | #define DFL_KEY_FILE "" |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 58 | #define DFL_CRT_FILE2 "" |
| 59 | #define DFL_KEY_FILE2 "" |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 60 | #define DFL_PSK "" |
| 61 | #define DFL_PSK_IDENTITY "Client_identity" |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 62 | #define DFL_FORCE_CIPHER 0 |
| 63 | #define DFL_RENEGOTIATION SSL_RENEGOTIATION_ENABLED |
| 64 | #define DFL_ALLOW_LEGACY SSL_LEGACY_NO_RENEGOTIATION |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 65 | #define DFL_MIN_VERSION -1 |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 66 | #define DFL_MAX_VERSION -1 |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 67 | #define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 68 | #define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 69 | #define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 70 | |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 71 | #define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 72 | "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 73 | "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 74 | "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 75 | "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 76 | "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 77 | "07-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah</p>\r\n" |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 78 | |
Paul Bakker | 8e714d7 | 2013-07-18 11:05:13 +0200 | [diff] [blame] | 79 | /* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer |
| 80 | * packets (for fragmentation purposes) */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 81 | #define HTTP_RESPONSE \ |
| 82 | "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \ |
| 83 | "<h2>PolarSSL Test Server</h2>\r\n" \ |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 84 | "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 85 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 86 | /* Uncomment to test server-initiated renegotiation */ |
Manuel Pégourié-Gonnard | f3dc2f6 | 2013-10-29 18:17:41 +0100 | [diff] [blame] | 87 | // #define TEST_RENEGO |
| 88 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 89 | /* |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 90 | * global options |
| 91 | */ |
| 92 | struct options |
| 93 | { |
| 94 | int server_port; /* port on which the ssl service runs */ |
| 95 | int debug_level; /* level of debugging */ |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 96 | const char *ca_file; /* the file with the CA certificate(s) */ |
| 97 | const char *ca_path; /* the path with the CA certificate(s) reside */ |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 98 | const char *crt_file; /* the file with the server certificate */ |
| 99 | const char *key_file; /* the file with the server key */ |
| 100 | const char *crt_file2; /* the file with the 2nd server certificate */ |
| 101 | const char *key_file2; /* the file with the 2nd server key */ |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 102 | const char *psk; /* the pre-shared key */ |
| 103 | const char *psk_identity; /* the pre-shared key identity */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 104 | int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */ |
| 105 | int renegotiation; /* enable / disable renegotiation */ |
| 106 | int allow_legacy; /* allow legacy renegotiation */ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 107 | int min_version; /* minimum protocol version accepted */ |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 108 | int max_version; /* maximum protocol version accepted */ |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 109 | int auth_mode; /* verify mode for connection */ |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 110 | unsigned char mfl_code; /* code for maximum fragment length */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 111 | int tickets; /* enable / disable session tickets */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 112 | } opt; |
| 113 | |
Paul Bakker | 3c5ef71 | 2013-06-25 16:37:45 +0200 | [diff] [blame] | 114 | static void my_debug( void *ctx, int level, const char *str ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 115 | { |
| 116 | if( level < opt.debug_level ) |
| 117 | { |
| 118 | fprintf( (FILE *) ctx, "%s", str ); |
| 119 | fflush( (FILE *) ctx ); |
| 120 | } |
| 121 | } |
| 122 | |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 123 | |
| 124 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 125 | #if defined(POLARSSL_FS_IO) |
| 126 | #define USAGE_IO \ |
Paul Bakker | 1f9d02d | 2012-11-20 10:30:55 +0100 | [diff] [blame] | 127 | " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \ |
| 128 | " default: \"\" (pre-loaded)\n" \ |
| 129 | " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \ |
| 130 | " default: \"\" (pre-loaded) (overrides ca_file)\n" \ |
| 131 | " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \ |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 132 | " default: see note after key_file2\n" \ |
| 133 | " key_file=%%s default: see note after key_file2\n" \ |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 134 | " crt_file2=%%s Your second cert and chain (in bottom to top order, top may be omitted)\n" \ |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 135 | " default: see note after key_file2\n" \ |
| 136 | " key_file2=%%s default: see note below\n" \ |
| 137 | " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \ |
| 138 | " preloaded certificate(s) and key(s) are used if available\n" |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 139 | #else |
| 140 | #define USAGE_IO \ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 141 | "\n" \ |
| 142 | " No file operations available (POLARSSL_FS_IO not defined)\n" \ |
| 143 | "\n" |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 144 | #endif /* POLARSSL_FS_IO */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 145 | #else |
| 146 | #define USAGE_IO "" |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 147 | #endif /* POLARSSL_X509_CRT_PARSE_C */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 148 | |
Manuel Pégourié-Gonnard | 8a3c64d | 2013-10-14 19:54:10 +0200 | [diff] [blame] | 149 | #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 150 | #define USAGE_PSK \ |
| 151 | " psk=%%s default: \"\" (in hex, without 0x)\n" \ |
| 152 | " psk_identity=%%s default: \"Client_identity\"\n" |
| 153 | #else |
| 154 | #define USAGE_PSK "" |
Manuel Pégourié-Gonnard | 8a3c64d | 2013-10-14 19:54:10 +0200 | [diff] [blame] | 155 | #endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 156 | |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 157 | #if defined(POLARSSL_SSL_SESSION_TICKETS) |
| 158 | #define USAGE_TICKETS \ |
| 159 | " tickets=%%d default: 1 (enabled)\n" |
| 160 | #else |
| 161 | #define USAGE_TICKETS "" |
| 162 | #endif /* POLARSSL_SSL_SESSION_TICKETS */ |
| 163 | |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 164 | #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH) |
| 165 | #define USAGE_MAX_FRAG_LEN \ |
| 166 | " max_frag_len=%%d default: 16384 (tls default)\n" \ |
| 167 | " options: 512, 1024, 2048, 4096\n" |
| 168 | #else |
| 169 | #define USAGE_MAX_FRAG_LEN "" |
| 170 | #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */ |
| 171 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 172 | #define USAGE \ |
| 173 | "\n usage: ssl_server2 param=<>...\n" \ |
| 174 | "\n acceptable parameters:\n" \ |
| 175 | " server_port=%%d default: 4433\n" \ |
| 176 | " debug_level=%%d default: 0 (disabled)\n" \ |
| 177 | USAGE_IO \ |
| 178 | " request_page=%%s default: \".\"\n" \ |
| 179 | " renegotiation=%%d default: 1 (enabled)\n" \ |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 180 | USAGE_TICKETS \ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 181 | " allow_legacy=%%d default: 0 (disabled)\n" \ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 182 | " min_version=%%s default: \"ssl3\"\n" \ |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 183 | " max_version=%%s default: \"tls1_2\"\n" \ |
| 184 | " force_version=%%s default: \"\" (none)\n" \ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 185 | " options: ssl3, tls1, tls1_1, tls1_2\n" \ |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 186 | " auth_mode=%%s default: \"optional\"\n" \ |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 187 | " options: none, optional, required\n" \ |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 188 | USAGE_MAX_FRAG_LEN \ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 189 | USAGE_PSK \ |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 190 | "\n" \ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 191 | " force_ciphersuite=<name> default: all enabled\n"\ |
| 192 | " acceptable ciphersuite names:\n" |
| 193 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 194 | #if !defined(POLARSSL_ENTROPY_C) || \ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 195 | !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 196 | !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 197 | int main( int argc, char *argv[] ) |
| 198 | { |
| 199 | ((void) argc); |
| 200 | ((void) argv); |
| 201 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 202 | printf("POLARSSL_ENTROPY_C and/or " |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 203 | "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or " |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 204 | "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n"); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 205 | return( 0 ); |
| 206 | } |
| 207 | #else |
| 208 | int main( int argc, char *argv[] ) |
| 209 | { |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 210 | int ret = 0, len, written, frags; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 211 | int listen_fd; |
| 212 | int client_fd = -1; |
| 213 | unsigned char buf[1024]; |
Manuel Pégourié-Gonnard | 8a3c64d | 2013-10-14 19:54:10 +0200 | [diff] [blame] | 214 | #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 215 | unsigned char psk[256]; |
| 216 | size_t psk_len = 0; |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 217 | #endif |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 218 | const char *pers = "ssl_server2"; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 219 | |
| 220 | entropy_context entropy; |
| 221 | ctr_drbg_context ctr_drbg; |
| 222 | ssl_context ssl; |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 223 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 224 | x509_crt cacert; |
| 225 | x509_crt srvcert; |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 226 | pk_context pkey; |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 227 | x509_crt srvcert2; |
| 228 | pk_context pkey2; |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 229 | int key_cert_init = 0, key_cert_init2 = 0; |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 230 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 231 | #if defined(POLARSSL_SSL_CACHE_C) |
| 232 | ssl_cache_context cache; |
| 233 | #endif |
Paul Bakker | 82024bf | 2013-07-04 11:52:32 +0200 | [diff] [blame] | 234 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) |
| 235 | unsigned char alloc_buf[100000]; |
| 236 | #endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 237 | |
| 238 | int i; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 239 | char *p, *q; |
| 240 | const int *list; |
| 241 | |
Paul Bakker | 82024bf | 2013-07-04 11:52:32 +0200 | [diff] [blame] | 242 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) |
| 243 | memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) ); |
| 244 | #endif |
| 245 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 246 | /* |
Manuel Pégourié-Gonnard | 3bd2aae | 2013-09-20 13:10:13 +0200 | [diff] [blame] | 247 | * Make sure memory references are valid in case we exit early. |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 248 | */ |
| 249 | listen_fd = 0; |
Manuel Pégourié-Gonnard | 3bd2aae | 2013-09-20 13:10:13 +0200 | [diff] [blame] | 250 | memset( &ssl, 0, sizeof( ssl_context ) ); |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 251 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 252 | x509_crt_init( &cacert ); |
| 253 | x509_crt_init( &srvcert ); |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 254 | pk_init( &pkey ); |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 255 | x509_crt_init( &srvcert2 ); |
| 256 | pk_init( &pkey2 ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 257 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 258 | #if defined(POLARSSL_SSL_CACHE_C) |
| 259 | ssl_cache_init( &cache ); |
| 260 | #endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 261 | |
| 262 | if( argc == 0 ) |
| 263 | { |
| 264 | usage: |
| 265 | if( ret == 0 ) |
| 266 | ret = 1; |
| 267 | |
| 268 | printf( USAGE ); |
| 269 | |
| 270 | list = ssl_list_ciphersuites(); |
| 271 | while( *list ) |
| 272 | { |
Paul Bakker | bcbe2d8 | 2013-04-19 09:10:20 +0200 | [diff] [blame] | 273 | printf(" %-42s", ssl_get_ciphersuite_name( *list ) ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 274 | list++; |
| 275 | if( !*list ) |
| 276 | break; |
| 277 | printf(" %s\n", ssl_get_ciphersuite_name( *list ) ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 278 | list++; |
| 279 | } |
| 280 | printf("\n"); |
| 281 | goto exit; |
| 282 | } |
| 283 | |
| 284 | opt.server_port = DFL_SERVER_PORT; |
| 285 | opt.debug_level = DFL_DEBUG_LEVEL; |
| 286 | opt.ca_file = DFL_CA_FILE; |
| 287 | opt.ca_path = DFL_CA_PATH; |
| 288 | opt.crt_file = DFL_CRT_FILE; |
| 289 | opt.key_file = DFL_KEY_FILE; |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 290 | opt.crt_file2 = DFL_CRT_FILE2; |
| 291 | opt.key_file2 = DFL_KEY_FILE2; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 292 | opt.psk = DFL_PSK; |
| 293 | opt.psk_identity = DFL_PSK_IDENTITY; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 294 | opt.force_ciphersuite[0]= DFL_FORCE_CIPHER; |
| 295 | opt.renegotiation = DFL_RENEGOTIATION; |
| 296 | opt.allow_legacy = DFL_ALLOW_LEGACY; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 297 | opt.min_version = DFL_MIN_VERSION; |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 298 | opt.max_version = DFL_MAX_VERSION; |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 299 | opt.auth_mode = DFL_AUTH_MODE; |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 300 | opt.mfl_code = DFL_MFL_CODE; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 301 | opt.tickets = DFL_TICKETS; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 302 | |
| 303 | for( i = 1; i < argc; i++ ) |
| 304 | { |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 305 | p = argv[i]; |
| 306 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 307 | goto usage; |
| 308 | *q++ = '\0'; |
| 309 | |
| 310 | if( strcmp( p, "server_port" ) == 0 ) |
| 311 | { |
| 312 | opt.server_port = atoi( q ); |
| 313 | if( opt.server_port < 1 || opt.server_port > 65535 ) |
| 314 | goto usage; |
| 315 | } |
| 316 | else if( strcmp( p, "debug_level" ) == 0 ) |
| 317 | { |
| 318 | opt.debug_level = atoi( q ); |
| 319 | if( opt.debug_level < 0 || opt.debug_level > 65535 ) |
| 320 | goto usage; |
| 321 | } |
| 322 | else if( strcmp( p, "ca_file" ) == 0 ) |
| 323 | opt.ca_file = q; |
| 324 | else if( strcmp( p, "ca_path" ) == 0 ) |
| 325 | opt.ca_path = q; |
| 326 | else if( strcmp( p, "crt_file" ) == 0 ) |
| 327 | opt.crt_file = q; |
| 328 | else if( strcmp( p, "key_file" ) == 0 ) |
| 329 | opt.key_file = q; |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 330 | else if( strcmp( p, "crt_file2" ) == 0 ) |
| 331 | opt.crt_file2 = q; |
| 332 | else if( strcmp( p, "key_file2" ) == 0 ) |
| 333 | opt.key_file2 = q; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 334 | else if( strcmp( p, "psk" ) == 0 ) |
| 335 | opt.psk = q; |
| 336 | else if( strcmp( p, "psk_identity" ) == 0 ) |
| 337 | opt.psk_identity = q; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 338 | else if( strcmp( p, "force_ciphersuite" ) == 0 ) |
| 339 | { |
| 340 | opt.force_ciphersuite[0] = -1; |
| 341 | |
| 342 | opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q ); |
| 343 | |
| 344 | if( opt.force_ciphersuite[0] <= 0 ) |
| 345 | { |
| 346 | ret = 2; |
| 347 | goto usage; |
| 348 | } |
| 349 | opt.force_ciphersuite[1] = 0; |
| 350 | } |
| 351 | else if( strcmp( p, "renegotiation" ) == 0 ) |
| 352 | { |
| 353 | opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED : |
| 354 | SSL_RENEGOTIATION_DISABLED; |
| 355 | } |
| 356 | else if( strcmp( p, "allow_legacy" ) == 0 ) |
| 357 | { |
| 358 | opt.allow_legacy = atoi( q ); |
| 359 | if( opt.allow_legacy < 0 || opt.allow_legacy > 1 ) |
| 360 | goto usage; |
| 361 | } |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 362 | else if( strcmp( p, "min_version" ) == 0 ) |
| 363 | { |
| 364 | if( strcmp( q, "ssl3" ) == 0 ) |
| 365 | opt.min_version = SSL_MINOR_VERSION_0; |
| 366 | else if( strcmp( q, "tls1" ) == 0 ) |
| 367 | opt.min_version = SSL_MINOR_VERSION_1; |
| 368 | else if( strcmp( q, "tls1_1" ) == 0 ) |
| 369 | opt.min_version = SSL_MINOR_VERSION_2; |
| 370 | else if( strcmp( q, "tls1_2" ) == 0 ) |
| 371 | opt.min_version = SSL_MINOR_VERSION_3; |
| 372 | else |
| 373 | goto usage; |
| 374 | } |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 375 | else if( strcmp( p, "max_version" ) == 0 ) |
| 376 | { |
| 377 | if( strcmp( q, "ssl3" ) == 0 ) |
| 378 | opt.max_version = SSL_MINOR_VERSION_0; |
| 379 | else if( strcmp( q, "tls1" ) == 0 ) |
| 380 | opt.max_version = SSL_MINOR_VERSION_1; |
| 381 | else if( strcmp( q, "tls1_1" ) == 0 ) |
| 382 | opt.max_version = SSL_MINOR_VERSION_2; |
| 383 | else if( strcmp( q, "tls1_2" ) == 0 ) |
| 384 | opt.max_version = SSL_MINOR_VERSION_3; |
| 385 | else |
| 386 | goto usage; |
| 387 | } |
| 388 | else if( strcmp( p, "force_version" ) == 0 ) |
| 389 | { |
| 390 | if( strcmp( q, "ssl3" ) == 0 ) |
| 391 | { |
| 392 | opt.min_version = SSL_MINOR_VERSION_0; |
| 393 | opt.max_version = SSL_MINOR_VERSION_0; |
| 394 | } |
| 395 | else if( strcmp( q, "tls1" ) == 0 ) |
| 396 | { |
| 397 | opt.min_version = SSL_MINOR_VERSION_1; |
| 398 | opt.max_version = SSL_MINOR_VERSION_1; |
| 399 | } |
| 400 | else if( strcmp( q, "tls1_1" ) == 0 ) |
| 401 | { |
| 402 | opt.min_version = SSL_MINOR_VERSION_2; |
| 403 | opt.max_version = SSL_MINOR_VERSION_2; |
| 404 | } |
| 405 | else if( strcmp( q, "tls1_2" ) == 0 ) |
| 406 | { |
| 407 | opt.min_version = SSL_MINOR_VERSION_3; |
| 408 | opt.max_version = SSL_MINOR_VERSION_3; |
| 409 | } |
| 410 | else |
| 411 | goto usage; |
| 412 | } |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 413 | else if( strcmp( p, "auth_mode" ) == 0 ) |
| 414 | { |
| 415 | if( strcmp( q, "none" ) == 0 ) |
| 416 | opt.auth_mode = SSL_VERIFY_NONE; |
| 417 | else if( strcmp( q, "optional" ) == 0 ) |
| 418 | opt.auth_mode = SSL_VERIFY_OPTIONAL; |
| 419 | else if( strcmp( q, "required" ) == 0 ) |
| 420 | opt.auth_mode = SSL_VERIFY_REQUIRED; |
| 421 | else |
| 422 | goto usage; |
| 423 | } |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 424 | else if( strcmp( p, "max_frag_len" ) == 0 ) |
| 425 | { |
| 426 | if( strcmp( q, "512" ) == 0 ) |
| 427 | opt.mfl_code = SSL_MAX_FRAG_LEN_512; |
| 428 | else if( strcmp( q, "1024" ) == 0 ) |
| 429 | opt.mfl_code = SSL_MAX_FRAG_LEN_1024; |
| 430 | else if( strcmp( q, "2048" ) == 0 ) |
| 431 | opt.mfl_code = SSL_MAX_FRAG_LEN_2048; |
| 432 | else if( strcmp( q, "4096" ) == 0 ) |
| 433 | opt.mfl_code = SSL_MAX_FRAG_LEN_4096; |
| 434 | else |
| 435 | goto usage; |
| 436 | } |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 437 | else if( strcmp( p, "tickets" ) == 0 ) |
| 438 | { |
| 439 | opt.tickets = atoi( q ); |
| 440 | if( opt.tickets < 0 || opt.tickets > 1 ) |
| 441 | goto usage; |
| 442 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 443 | else |
| 444 | goto usage; |
| 445 | } |
| 446 | |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 447 | if( opt.force_ciphersuite[0] > 0 ) |
| 448 | { |
| 449 | const ssl_ciphersuite_t *ciphersuite_info; |
| 450 | ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] ); |
| 451 | |
Paul Bakker | 5b55b79 | 2013-07-19 13:43:43 +0200 | [diff] [blame] | 452 | if( opt.max_version != -1 && |
| 453 | ciphersuite_info->min_minor_ver > opt.max_version ) |
| 454 | { |
| 455 | printf("forced ciphersuite not allowed with this protocol version\n"); |
| 456 | ret = 2; |
| 457 | goto usage; |
| 458 | } |
| 459 | if( opt.min_version != -1 && |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 460 | ciphersuite_info->max_minor_ver < opt.min_version ) |
| 461 | { |
| 462 | printf("forced ciphersuite not allowed with this protocol version\n"); |
| 463 | ret = 2; |
| 464 | goto usage; |
| 465 | } |
Paul Bakker | 5b55b79 | 2013-07-19 13:43:43 +0200 | [diff] [blame] | 466 | if( opt.max_version > ciphersuite_info->max_minor_ver ) |
| 467 | opt.max_version = ciphersuite_info->max_minor_ver; |
| 468 | if( opt.min_version < ciphersuite_info->min_minor_ver ) |
| 469 | opt.min_version = ciphersuite_info->min_minor_ver; |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 470 | } |
| 471 | |
Manuel Pégourié-Gonnard | 8a3c64d | 2013-10-14 19:54:10 +0200 | [diff] [blame] | 472 | #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 473 | /* |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 474 | * Unhexify the pre-shared key if any is given |
| 475 | */ |
| 476 | if( strlen( opt.psk ) ) |
| 477 | { |
| 478 | unsigned char c; |
| 479 | size_t j; |
| 480 | |
| 481 | if( strlen( opt.psk ) % 2 != 0 ) |
| 482 | { |
| 483 | printf("pre-shared key not valid hex\n"); |
| 484 | goto exit; |
| 485 | } |
| 486 | |
| 487 | psk_len = strlen( opt.psk ) / 2; |
| 488 | |
| 489 | for( j = 0; j < strlen( opt.psk ); j += 2 ) |
| 490 | { |
| 491 | c = opt.psk[j]; |
| 492 | if( c >= '0' && c <= '9' ) |
| 493 | c -= '0'; |
| 494 | else if( c >= 'a' && c <= 'f' ) |
| 495 | c -= 'a' - 10; |
| 496 | else if( c >= 'A' && c <= 'F' ) |
| 497 | c -= 'A' - 10; |
| 498 | else |
| 499 | { |
| 500 | printf("pre-shared key not valid hex\n"); |
| 501 | goto exit; |
| 502 | } |
| 503 | psk[ j / 2 ] = c << 4; |
| 504 | |
| 505 | c = opt.psk[j + 1]; |
| 506 | if( c >= '0' && c <= '9' ) |
| 507 | c -= '0'; |
| 508 | else if( c >= 'a' && c <= 'f' ) |
| 509 | c -= 'a' - 10; |
| 510 | else if( c >= 'A' && c <= 'F' ) |
| 511 | c -= 'A' - 10; |
| 512 | else |
| 513 | { |
| 514 | printf("pre-shared key not valid hex\n"); |
| 515 | goto exit; |
| 516 | } |
| 517 | psk[ j / 2 ] |= c; |
| 518 | } |
| 519 | } |
Manuel Pégourié-Gonnard | 8a3c64d | 2013-10-14 19:54:10 +0200 | [diff] [blame] | 520 | #endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 521 | |
| 522 | /* |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 523 | * 0. Initialize the RNG and the session data |
| 524 | */ |
| 525 | printf( "\n . Seeding the random number generator..." ); |
| 526 | fflush( stdout ); |
| 527 | |
| 528 | entropy_init( &entropy ); |
| 529 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 530 | (const unsigned char *) pers, |
| 531 | strlen( pers ) ) ) != 0 ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 532 | { |
| 533 | printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret ); |
| 534 | goto exit; |
| 535 | } |
| 536 | |
| 537 | printf( " ok\n" ); |
| 538 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 539 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 540 | /* |
| 541 | * 1.1. Load the trusted CA |
| 542 | */ |
| 543 | printf( " . Loading the CA root certificate ..." ); |
| 544 | fflush( stdout ); |
| 545 | |
| 546 | #if defined(POLARSSL_FS_IO) |
| 547 | if( strlen( opt.ca_path ) ) |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 548 | ret = x509_crt_parse_path( &cacert, opt.ca_path ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 549 | else if( strlen( opt.ca_file ) ) |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 550 | ret = x509_crt_parse_file( &cacert, opt.ca_file ); |
| 551 | else |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 552 | #endif |
| 553 | #if defined(POLARSSL_CERTS_C) |
Manuel Pégourié-Gonnard | 641de71 | 2013-09-25 13:23:33 +0200 | [diff] [blame] | 554 | ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list, |
| 555 | strlen( test_ca_list ) ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 556 | #else |
| 557 | { |
| 558 | ret = 1; |
| 559 | printf("POLARSSL_CERTS_C not defined."); |
| 560 | } |
| 561 | #endif |
| 562 | if( ret < 0 ) |
| 563 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 564 | printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 565 | goto exit; |
| 566 | } |
| 567 | |
| 568 | printf( " ok (%d skipped)\n", ret ); |
| 569 | |
| 570 | /* |
| 571 | * 1.2. Load own certificate and private key |
| 572 | */ |
| 573 | printf( " . Loading the server cert. and key..." ); |
| 574 | fflush( stdout ); |
| 575 | |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 576 | #if defined(POLARSSL_FS_IO) |
| 577 | if( strlen( opt.crt_file ) ) |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 578 | { |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 579 | key_cert_init++; |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 580 | if( ( ret = x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 ) |
| 581 | { |
| 582 | printf( " failed\n ! x509_crt_parse_file returned -0x%x\n\n", |
| 583 | -ret ); |
| 584 | goto exit; |
| 585 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 586 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 587 | if( strlen( opt.key_file ) ) |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 588 | { |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 589 | key_cert_init++; |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 590 | if( ( ret = pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 ) |
| 591 | { |
| 592 | printf( " failed\n ! pk_parse_keyfile returned -0x%x\n\n", -ret ); |
| 593 | goto exit; |
| 594 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 595 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 596 | if( key_cert_init == 1 ) |
| 597 | { |
| 598 | printf( " failed\n ! crt_file without key_file or vice-versa\n\n" ); |
| 599 | goto exit; |
| 600 | } |
| 601 | |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 602 | if( strlen( opt.crt_file2 ) ) |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 603 | { |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 604 | key_cert_init2++; |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 605 | if( ( ret = x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 ) |
| 606 | { |
| 607 | printf( " failed\n ! x509_crt_parse_file(2) returned -0x%x\n\n", |
| 608 | -ret ); |
| 609 | goto exit; |
| 610 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 611 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 612 | if( strlen( opt.key_file2 ) ) |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 613 | { |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 614 | key_cert_init2++; |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 615 | if( ( ret = pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 ) |
| 616 | { |
| 617 | printf( " failed\n ! pk_parse_keyfile(2) returned -0x%x\n\n", |
| 618 | -ret ); |
| 619 | goto exit; |
| 620 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 621 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 622 | if( key_cert_init2 == 1 ) |
| 623 | { |
| 624 | printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" ); |
| 625 | goto exit; |
| 626 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 627 | #endif |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 628 | if( key_cert_init == 0 && key_cert_init2 == 0 ) |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 629 | { |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 630 | #if !defined(POLARSSL_CERTS_C) |
| 631 | printf( "Not certificated or key provided, and \n" |
| 632 | "POLARSSL_CERTS_C not defined!\n" ); |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 633 | goto exit; |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 634 | #else |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 635 | #if defined(POLARSSL_RSA_C) |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 636 | if( ( ret = x509_crt_parse( &srvcert, |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 637 | (const unsigned char *) test_srv_crt_rsa, |
| 638 | strlen( test_srv_crt_rsa ) ) ) != 0 ) |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 639 | { |
| 640 | printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret ); |
| 641 | goto exit; |
| 642 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 643 | if( ( ret = pk_parse_key( &pkey, |
| 644 | (const unsigned char *) test_srv_key_rsa, |
| 645 | strlen( test_srv_key_rsa ), NULL, 0 ) ) != 0 ) |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 646 | { |
| 647 | printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret ); |
| 648 | goto exit; |
| 649 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 650 | key_cert_init = 2; |
| 651 | #endif /* POLARSSL_RSA_C */ |
| 652 | #if defined(POLARSSL_ECDSA_C) |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 653 | if( ( ret = x509_crt_parse( &srvcert2, |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 654 | (const unsigned char *) test_srv_crt_ec, |
| 655 | strlen( test_srv_crt_ec ) ) ) != 0 ) |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 656 | { |
| 657 | printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret ); |
| 658 | goto exit; |
| 659 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 660 | if( ( ret = pk_parse_key( &pkey2, |
| 661 | (const unsigned char *) test_srv_key_ec, |
| 662 | strlen( test_srv_key_ec ), NULL, 0 ) ) != 0 ) |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 663 | { |
| 664 | printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret ); |
| 665 | goto exit; |
| 666 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 667 | key_cert_init2 = 2; |
| 668 | #endif /* POLARSSL_ECDSA_C */ |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 669 | #endif /* POLARSSL_CERTS_C */ |
| 670 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 671 | |
| 672 | printf( " ok\n" ); |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 673 | #endif /* POLARSSL_X509_CRT_PARSE_C */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 674 | |
| 675 | /* |
| 676 | * 2. Setup the listening TCP socket |
| 677 | */ |
| 678 | printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port ); |
| 679 | fflush( stdout ); |
| 680 | |
| 681 | if( ( ret = net_bind( &listen_fd, NULL, opt.server_port ) ) != 0 ) |
| 682 | { |
| 683 | printf( " failed\n ! net_bind returned -0x%x\n\n", -ret ); |
| 684 | goto exit; |
| 685 | } |
| 686 | |
| 687 | printf( " ok\n" ); |
| 688 | |
| 689 | /* |
| 690 | * 3. Setup stuff |
| 691 | */ |
| 692 | printf( " . Setting up the SSL/TLS structure..." ); |
| 693 | fflush( stdout ); |
| 694 | |
| 695 | if( ( ret = ssl_init( &ssl ) ) != 0 ) |
| 696 | { |
| 697 | printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret ); |
| 698 | goto exit; |
| 699 | } |
| 700 | |
| 701 | ssl_set_endpoint( &ssl, SSL_IS_SERVER ); |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 702 | ssl_set_authmode( &ssl, opt.auth_mode ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 703 | |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 704 | #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 705 | ssl_set_max_frag_len( &ssl, opt.mfl_code ); |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 706 | #endif |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 707 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 708 | ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg ); |
| 709 | ssl_set_dbg( &ssl, my_debug, stdout ); |
| 710 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 711 | #if defined(POLARSSL_SSL_CACHE_C) |
| 712 | ssl_set_session_cache( &ssl, ssl_cache_get, &cache, |
| 713 | ssl_cache_set, &cache ); |
| 714 | #endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 715 | |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 716 | #if defined(POLARSSL_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 717 | ssl_set_session_tickets( &ssl, opt.tickets ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 718 | #endif |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 719 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 720 | if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 721 | ssl_set_ciphersuites( &ssl, opt.force_ciphersuite ); |
| 722 | |
| 723 | ssl_set_renegotiation( &ssl, opt.renegotiation ); |
| 724 | ssl_legacy_renegotiation( &ssl, opt.allow_legacy ); |
| 725 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 726 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 727 | ssl_set_ca_chain( &ssl, &cacert, NULL, NULL ); |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 728 | if( key_cert_init ) |
| 729 | ssl_set_own_cert( &ssl, &srvcert, &pkey ); |
| 730 | if( key_cert_init2 ) |
| 731 | ssl_set_own_cert( &ssl, &srvcert2, &pkey2 ); |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 732 | #endif |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 733 | |
Manuel Pégourié-Gonnard | 8a3c64d | 2013-10-14 19:54:10 +0200 | [diff] [blame] | 734 | #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Paul Bakker | 3c5ef71 | 2013-06-25 16:37:45 +0200 | [diff] [blame] | 735 | ssl_set_psk( &ssl, psk, psk_len, (const unsigned char *) opt.psk_identity, |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 736 | strlen( opt.psk_identity ) ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 737 | #endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 738 | |
| 739 | #if defined(POLARSSL_DHM_C) |
Paul Bakker | 5d19f86 | 2012-09-28 07:33:00 +0000 | [diff] [blame] | 740 | /* |
| 741 | * Use different group than default DHM group |
| 742 | */ |
Paul Bakker | d432410 | 2012-09-26 08:29:38 +0000 | [diff] [blame] | 743 | ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P, |
| 744 | POLARSSL_DHM_RFC5114_MODP_2048_G ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 745 | #endif |
| 746 | |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 747 | if( opt.min_version != -1 ) |
| 748 | ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version ); |
| 749 | |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 750 | if( opt.max_version != -1 ) |
| 751 | ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version ); |
| 752 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 753 | printf( " ok\n" ); |
| 754 | |
| 755 | reset: |
| 756 | #ifdef POLARSSL_ERROR_C |
| 757 | if( ret != 0 ) |
| 758 | { |
| 759 | char error_buf[100]; |
Paul Bakker | 03a8a79 | 2013-06-30 12:18:08 +0200 | [diff] [blame] | 760 | polarssl_strerror( ret, error_buf, 100 ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 761 | printf("Last error was: %d - %s\n\n", ret, error_buf ); |
| 762 | } |
| 763 | #endif |
| 764 | |
| 765 | if( client_fd != -1 ) |
| 766 | net_close( client_fd ); |
| 767 | |
| 768 | ssl_session_reset( &ssl ); |
| 769 | |
| 770 | /* |
| 771 | * 3. Wait until a client connects |
| 772 | */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 773 | client_fd = -1; |
| 774 | |
| 775 | printf( " . Waiting for a remote connection ..." ); |
| 776 | fflush( stdout ); |
| 777 | |
| 778 | if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 ) |
| 779 | { |
| 780 | printf( " failed\n ! net_accept returned -0x%x\n\n", -ret ); |
| 781 | goto exit; |
| 782 | } |
| 783 | |
| 784 | ssl_set_bio( &ssl, net_recv, &client_fd, |
| 785 | net_send, &client_fd ); |
| 786 | |
| 787 | printf( " ok\n" ); |
| 788 | |
| 789 | /* |
| 790 | * 4. Handshake |
| 791 | */ |
| 792 | printf( " . Performing the SSL/TLS handshake..." ); |
| 793 | fflush( stdout ); |
| 794 | |
| 795 | while( ( ret = ssl_handshake( &ssl ) ) != 0 ) |
| 796 | { |
| 797 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 798 | { |
| 799 | printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret ); |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 800 | goto reset; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | |
| 804 | printf( " ok\n [ Ciphersuite is %s ]\n", |
| 805 | ssl_get_ciphersuite( &ssl ) ); |
| 806 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 807 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 808 | /* |
| 809 | * 5. Verify the server certificate |
| 810 | */ |
| 811 | printf( " . Verifying peer X.509 certificate..." ); |
| 812 | |
| 813 | if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 ) |
| 814 | { |
| 815 | printf( " failed\n" ); |
| 816 | |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 817 | if( !ssl_get_peer_cert( &ssl ) ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 818 | printf( " ! no client certificate sent\n" ); |
| 819 | |
| 820 | if( ( ret & BADCERT_EXPIRED ) != 0 ) |
| 821 | printf( " ! client certificate has expired\n" ); |
| 822 | |
| 823 | if( ( ret & BADCERT_REVOKED ) != 0 ) |
| 824 | printf( " ! client certificate has been revoked\n" ); |
| 825 | |
| 826 | if( ( ret & BADCERT_NOT_TRUSTED ) != 0 ) |
| 827 | printf( " ! self-signed or not signed by a trusted CA\n" ); |
| 828 | |
| 829 | printf( "\n" ); |
| 830 | } |
| 831 | else |
| 832 | printf( " ok\n" ); |
| 833 | |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 834 | if( ssl_get_peer_cert( &ssl ) ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 835 | { |
| 836 | printf( " . Peer certificate information ...\n" ); |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 837 | x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ", |
| 838 | ssl_get_peer_cert( &ssl ) ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 839 | printf( "%s\n", buf ); |
| 840 | } |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 841 | #endif /* POLARSSL_X509_CRT_PARSE_C */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 842 | |
| 843 | /* |
| 844 | * 6. Read the HTTP Request |
| 845 | */ |
| 846 | printf( " < Read from client:" ); |
| 847 | fflush( stdout ); |
| 848 | |
| 849 | do |
| 850 | { |
| 851 | len = sizeof( buf ) - 1; |
| 852 | memset( buf, 0, sizeof( buf ) ); |
| 853 | ret = ssl_read( &ssl, buf, len ); |
| 854 | |
| 855 | if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE ) |
| 856 | continue; |
| 857 | |
| 858 | if( ret <= 0 ) |
| 859 | { |
| 860 | switch( ret ) |
| 861 | { |
| 862 | case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY: |
| 863 | printf( " connection was closed gracefully\n" ); |
| 864 | break; |
| 865 | |
| 866 | case POLARSSL_ERR_NET_CONN_RESET: |
| 867 | printf( " connection was reset by peer\n" ); |
| 868 | break; |
| 869 | |
| 870 | default: |
| 871 | printf( " ssl_read returned -0x%x\n", -ret ); |
| 872 | break; |
| 873 | } |
| 874 | |
| 875 | break; |
| 876 | } |
| 877 | |
| 878 | len = ret; |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 879 | printf( " %d bytes read\n\n%s\n", len, (char *) buf ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 880 | |
Paul Bakker | 82024bf | 2013-07-04 11:52:32 +0200 | [diff] [blame] | 881 | if( memcmp( buf, "SERVERQUIT", 10 ) == 0 ) |
Manuel Pégourié-Gonnard | e8ea0c0 | 2013-09-07 17:09:14 +0200 | [diff] [blame] | 882 | { |
| 883 | ret = 0; |
Paul Bakker | 82024bf | 2013-07-04 11:52:32 +0200 | [diff] [blame] | 884 | goto exit; |
Manuel Pégourié-Gonnard | e8ea0c0 | 2013-09-07 17:09:14 +0200 | [diff] [blame] | 885 | } |
Paul Bakker | 82024bf | 2013-07-04 11:52:32 +0200 | [diff] [blame] | 886 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 887 | if( ret > 0 ) |
| 888 | break; |
| 889 | } |
| 890 | while( 1 ); |
| 891 | |
| 892 | /* |
| 893 | * 7. Write the 200 Response |
| 894 | */ |
| 895 | printf( " > Write to client:" ); |
| 896 | fflush( stdout ); |
| 897 | |
| 898 | len = sprintf( (char *) buf, HTTP_RESPONSE, |
| 899 | ssl_get_ciphersuite( &ssl ) ); |
| 900 | |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 901 | for( written = 0, frags = 0; written < len; written += ret, frags++ ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 902 | { |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 903 | while( ( ret = ssl_write( &ssl, buf + written, len - written ) ) <= 0 ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 904 | { |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 905 | if( ret == POLARSSL_ERR_NET_CONN_RESET ) |
| 906 | { |
| 907 | printf( " failed\n ! peer closed the connection\n\n" ); |
| 908 | goto reset; |
| 909 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 910 | |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 911 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 912 | { |
| 913 | printf( " failed\n ! ssl_write returned %d\n\n", ret ); |
| 914 | goto exit; |
| 915 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 919 | buf[written] = '\0'; |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 920 | printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 921 | |
Manuel Pégourié-Gonnard | f3dc2f6 | 2013-10-29 18:17:41 +0100 | [diff] [blame] | 922 | #ifdef TEST_RENEGO |
| 923 | /* |
| 924 | * Request renegotiation (this must be done when the client is still |
| 925 | * waiting for input from our side). |
| 926 | */ |
| 927 | printf( " . Requestion renegotiation..." ); |
| 928 | fflush( stdout ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 929 | while( ( ret = ssl_renegotiate( &ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | f3dc2f6 | 2013-10-29 18:17:41 +0100 | [diff] [blame] | 930 | { |
| 931 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 932 | { |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 933 | printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | f3dc2f6 | 2013-10-29 18:17:41 +0100 | [diff] [blame] | 934 | goto exit; |
| 935 | } |
| 936 | } |
| 937 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 938 | /* |
| 939 | * Should be a while loop, not an if, but here we're not actually |
| 940 | * expecting data from the client, and since we're running tests locally, |
| 941 | * we can just hope the handshake will finish the during the first call. |
| 942 | */ |
Manuel Pégourié-Gonnard | f3dc2f6 | 2013-10-29 18:17:41 +0100 | [diff] [blame] | 943 | if( ( ret = ssl_read( &ssl, buf, 0 ) ) != 0 ) |
| 944 | { |
| 945 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 946 | { |
| 947 | printf( " failed\n ! ssl_read returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 948 | |
| 949 | /* Unexpected message probably means client didn't renegotiate */ |
| 950 | if( ret == POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE ) |
| 951 | goto reset; |
| 952 | else |
| 953 | goto exit; |
Manuel Pégourié-Gonnard | f3dc2f6 | 2013-10-29 18:17:41 +0100 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | |
| 957 | printf( " ok\n" ); |
| 958 | #endif |
| 959 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 960 | ret = 0; |
| 961 | goto reset; |
| 962 | |
| 963 | exit: |
| 964 | |
| 965 | #ifdef POLARSSL_ERROR_C |
| 966 | if( ret != 0 ) |
| 967 | { |
| 968 | char error_buf[100]; |
Paul Bakker | 03a8a79 | 2013-06-30 12:18:08 +0200 | [diff] [blame] | 969 | polarssl_strerror( ret, error_buf, 100 ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 970 | printf("Last error was: -0x%X - %s\n\n", -ret, error_buf ); |
| 971 | } |
| 972 | #endif |
| 973 | |
| 974 | net_close( client_fd ); |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 975 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 976 | x509_crt_free( &cacert ); |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 977 | x509_crt_free( &srvcert ); |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 978 | pk_free( &pkey ); |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 979 | x509_crt_free( &srvcert2 ); |
| 980 | pk_free( &pkey2 ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 981 | #endif |
| 982 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 983 | ssl_free( &ssl ); |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 984 | entropy_free( &entropy ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 985 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 986 | #if defined(POLARSSL_SSL_CACHE_C) |
| 987 | ssl_cache_free( &cache ); |
| 988 | #endif |
| 989 | |
Paul Bakker | 1337aff | 2013-09-29 14:45:34 +0200 | [diff] [blame] | 990 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) |
| 991 | #if defined(POLARSSL_MEMORY_DEBUG) |
Paul Bakker | 82024bf | 2013-07-04 11:52:32 +0200 | [diff] [blame] | 992 | memory_buffer_alloc_status(); |
| 993 | #endif |
Paul Bakker | 1337aff | 2013-09-29 14:45:34 +0200 | [diff] [blame] | 994 | memory_buffer_alloc_free(); |
| 995 | #endif |
Paul Bakker | 82024bf | 2013-07-04 11:52:32 +0200 | [diff] [blame] | 996 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 997 | #if defined(_WIN32) |
| 998 | printf( " + Press Enter to exit this program.\n" ); |
| 999 | fflush( stdout ); getchar(); |
| 1000 | #endif |
| 1001 | |
Paul Bakker | dbd79ca | 2013-07-24 16:28:35 +0200 | [diff] [blame] | 1002 | // Shell can not handle large exit numbers -> 1 for errors |
| 1003 | if( ret < 0 ) |
| 1004 | ret = 1; |
| 1005 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1006 | return( ret ); |
| 1007 | } |
| 1008 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C && |
| 1009 | POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C && |
| 1010 | POLARSSL_CTR_DRBG_C */ |