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