blob: 7f2041b51381dd87d8203772ba8c8302ebb1e9bc [file] [log] [blame]
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001/*
2 * SSL client with options
3 *
Paul Bakker3c5ef712013-06-25 16:37:45 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb60b95f2012-09-25 09:05:17 +00005 *
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é-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +000031
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010032#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) && defined(POLARSSL_FS_IO)
33#define POLARSSL_SNI
34#endif
35
36#if defined(POLARSSL_PLATFORM_C)
37#include "polarssl/platform.h"
38#else
39#define polarssl_malloc malloc
40#define polarssl_free free
41#endif
42
Paul Bakkerb60b95f2012-09-25 09:05:17 +000043#if defined(_WIN32)
44#include <windows.h>
45#endif
46
47#include <string.h>
48#include <stdlib.h>
49#include <stdio.h>
50
Paul Bakkerb60b95f2012-09-25 09:05:17 +000051#include "polarssl/net.h"
52#include "polarssl/ssl.h"
53#include "polarssl/entropy.h"
54#include "polarssl/ctr_drbg.h"
55#include "polarssl/certs.h"
56#include "polarssl/x509.h"
57#include "polarssl/error.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020058#include "polarssl/debug.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000059
Paul Bakker0a597072012-09-25 21:55:46 +000060#if defined(POLARSSL_SSL_CACHE_C)
61#include "polarssl/ssl_cache.h"
62#endif
63
Paul Bakker82024bf2013-07-04 11:52:32 +020064#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
65#include "polarssl/memory.h"
66#endif
67
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +010068#define DFL_SERVER_ADDR NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000069#define DFL_SERVER_PORT 4433
Paul Bakkerb60b95f2012-09-25 09:05:17 +000070#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010071#define DFL_NBIO 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +000072#define DFL_CA_FILE ""
73#define DFL_CA_PATH ""
74#define DFL_CRT_FILE ""
75#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +020076#define DFL_CRT_FILE2 ""
77#define DFL_KEY_FILE2 ""
Paul Bakkerfbb17802013-04-17 19:10:21 +020078#define DFL_PSK ""
79#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +020080#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000081#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +020082#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard00d538f2014-03-31 10:44:40 +020083#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
Paul Bakkerb60b95f2012-09-25 09:05:17 +000084#define DFL_ALLOW_LEGACY SSL_LEGACY_NO_RENEGOTIATION
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +010085#define DFL_RENEGOTIATE 0
Paul Bakker1d29fb52012-09-28 13:28:45 +000086#define DFL_MIN_VERSION -1
Paul Bakkerc1516be2013-06-29 16:01:32 +020087#define DFL_MAX_VERSION -1
Paul Bakker91ebfb52012-11-23 14:04:08 +010088#define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +020089#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +020090#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +010091#define DFL_TICKET_TIMEOUT -1
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +010092#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +010093#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010094#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +020095#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +020096#define DFL_DHM_FILE NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000097
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +020098#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
99 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
100 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
101 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
102 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
103 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
104 "07-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah</p>\r\n"
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200105
Paul Bakker8e714d72013-07-18 11:05:13 +0200106/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
107 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000108#define HTTP_RESPONSE \
109 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
110 "<h2>PolarSSL Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200111 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000112
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200113#define MAX_PSK_LEN 256
114
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200115/* Size of the basic I/O buffer. Able to hold our default response. */
116#define IO_BUF_LEN 200
117
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000118/*
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000119 * global options
120 */
121struct options
122{
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100123 const char *server_addr; /* address on which the ssl service runs */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000124 int server_port; /* port on which the ssl service runs */
125 int debug_level; /* level of debugging */
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100126 int nbio; /* should I/O be blocking? */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200127 const char *ca_file; /* the file with the CA certificate(s) */
128 const char *ca_path; /* the path with the CA certificate(s) reside */
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200129 const char *crt_file; /* the file with the server certificate */
130 const char *key_file; /* the file with the server key */
131 const char *crt_file2; /* the file with the 2nd server certificate */
132 const char *key_file2; /* the file with the 2nd server key */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200133 const char *psk; /* the pre-shared key */
134 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200135 char *psk_list; /* list of PSK id/key pairs for callback */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000136 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200137 const char *version_suites; /* per-version ciphersuites */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000138 int renegotiation; /* enable / disable renegotiation */
139 int allow_legacy; /* allow legacy renegotiation */
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100140 int renegotiate; /* attempt renegotiation? */
Paul Bakker1d29fb52012-09-28 13:28:45 +0000141 int min_version; /* minimum protocol version accepted */
Paul Bakkerc1516be2013-06-29 16:01:32 +0200142 int max_version; /* maximum protocol version accepted */
Paul Bakker91ebfb52012-11-23 14:04:08 +0100143 int auth_mode; /* verify mode for connection */
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200144 unsigned char mfl_code; /* code for maximum fragment length */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200145 int tickets; /* enable / disable session tickets */
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100146 int ticket_timeout; /* session ticket lifetime */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100147 int cache_max; /* max number of session cache entries */
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100148 int cache_timeout; /* expiration delay of session cache entries */
Paul Bakker1ebc0c52014-05-22 15:47:58 +0200149 char *sni; /* string describing sni information */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200150 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200151 const char *dhm_file; /* the file with the DH parameters */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000152} opt;
153
Paul Bakker3c5ef712013-06-25 16:37:45 +0200154static void my_debug( void *ctx, int level, const char *str )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000155{
Paul Bakkerc73079a2014-04-25 16:34:30 +0200156 ((void) level);
157
158 fprintf( (FILE *) ctx, "%s", str );
159 fflush( (FILE *) ctx );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000160}
161
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100162/*
163 * Test recv/send functions that make sure each try returns
164 * WANT_READ/WANT_WRITE at least once before sucesseding
165 */
166static int my_recv( void *ctx, unsigned char *buf, size_t len )
167{
168 static int first_try = 1;
169 int ret;
170
171 if( first_try )
172 {
173 first_try = 0;
174 return( POLARSSL_ERR_NET_WANT_READ );
175 }
176
177 ret = net_recv( ctx, buf, len );
178 if( ret != POLARSSL_ERR_NET_WANT_READ )
179 first_try = 1; /* Next call will be a new operation */
180 return( ret );
181}
182
183static int my_send( void *ctx, const unsigned char *buf, size_t len )
184{
185 static int first_try = 1;
186 int ret;
187
188 if( first_try )
189 {
190 first_try = 0;
191 return( POLARSSL_ERR_NET_WANT_WRITE );
192 }
193
194 ret = net_send( ctx, buf, len );
195 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
196 first_try = 1; /* Next call will be a new operation */
197 return( ret );
198}
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200199
200#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000201#if defined(POLARSSL_FS_IO)
202#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100203 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
204 " default: \"\" (pre-loaded)\n" \
205 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
206 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
207 " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +0200208 " default: see note after key_file2\n" \
209 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200210 " crt_file2=%%s Your second cert and chain (in bottom to top order, top may be omitted)\n" \
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +0200211 " default: see note after key_file2\n" \
212 " key_file2=%%s default: see note below\n" \
213 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200214 " preloaded certificate(s) and key(s) are used if available\n" \
215 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
216 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000217#else
218#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200219 "\n" \
220 " No file operations available (POLARSSL_FS_IO not defined)\n" \
221 "\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000222#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200223#else
224#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200225#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200226
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200227#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200228#define USAGE_PSK \
229 " psk=%%s default: \"\" (in hex, without 0x)\n" \
230 " psk_identity=%%s default: \"Client_identity\"\n"
231#else
232#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200233#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000234
Paul Bakkera503a632013-08-14 13:48:06 +0200235#if defined(POLARSSL_SSL_SESSION_TICKETS)
236#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100237 " tickets=%%d default: 1 (enabled)\n" \
238 " ticket_timeout=%%d default: ticket default (1d)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200239#else
240#define USAGE_TICKETS ""
241#endif /* POLARSSL_SSL_SESSION_TICKETS */
242
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100243#if defined(POLARSSL_SSL_CACHE_C)
244#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100245 " cache_max=%%d default: cache default (50)\n" \
246 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100247#else
248#define USAGE_CACHE ""
249#endif /* POLARSSL_SSL_CACHE_C */
250
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100251#if defined(POLARSSL_SNI)
252#define USAGE_SNI \
253 " sni=%%s name1,cert1,key1[,name2,cert2,key2[,...]]\n" \
254 " default: disabled\n"
255#else
256#define USAGE_SNI ""
257#endif /* POLARSSL_SNI */
258
Paul Bakker05decb22013-08-15 13:33:48 +0200259#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
260#define USAGE_MAX_FRAG_LEN \
261 " max_frag_len=%%d default: 16384 (tls default)\n" \
262 " options: 512, 1024, 2048, 4096\n"
263#else
264#define USAGE_MAX_FRAG_LEN ""
265#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
266
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200267#if defined(POLARSSL_SSL_ALPN)
268#define USAGE_ALPN \
269 " alpn=%%s default: \"\" (disabled)\n" \
270 " example: spdy/1,http/1.1\n"
271#else
272#define USAGE_ALPN ""
273#endif /* POLARSSL_SSL_ALPN */
274
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000275#define USAGE \
276 "\n usage: ssl_server2 param=<>...\n" \
277 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100278 " server_addr=%%d default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000279 " server_port=%%d default: 4433\n" \
280 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100281 " nbio=%%d default: 0 (blocking I/O)\n" \
282 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100283 "\n" \
284 " auth_mode=%%s default: \"optional\"\n" \
285 " options: none, optional, required\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000286 USAGE_IO \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100287 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100288 "\n" \
289 USAGE_PSK \
290 "\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000291 " renegotiation=%%d default: 1 (enabled)\n" \
292 " allow_legacy=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100293 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100294 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100295 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100296 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200297 USAGE_ALPN \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100298 "\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000299 " min_version=%%s default: \"ssl3\"\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200300 " max_version=%%s default: \"tls1_2\"\n" \
301 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200302 " options: ssl3, tls1, tls1_1, tls1_2\n" \
303 "\n" \
304 " version_suites=a,b,c,d per-version ciphersuites\n" \
305 " in order from ssl3 to tls1_2\n" \
306 " default: all enabled\n" \
307 " force_ciphersuite=<name> default: all enabled\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000308 " acceptable ciphersuite names:\n"
309
Paul Bakkered27a042013-04-18 22:46:23 +0200310#if !defined(POLARSSL_ENTROPY_C) || \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000311 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
Paul Bakkered27a042013-04-18 22:46:23 +0200312 !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000313int main( int argc, char *argv[] )
314{
315 ((void) argc);
316 ((void) argv);
317
Paul Bakkered27a042013-04-18 22:46:23 +0200318 printf("POLARSSL_ENTROPY_C and/or "
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000319 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
Paul Bakkered27a042013-04-18 22:46:23 +0200320 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000321 return( 0 );
322}
323#else
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100324
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200325/*
326 * Used by sni_parse and psk_parse to handle coma-separated lists
327 */
328#define GET_ITEM( dst ) \
329 dst = p; \
330 while( *p != ',' ) \
331 if( ++p > end ) \
332 return( NULL ); \
333 *p++ = '\0';
334
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100335#if defined(POLARSSL_SNI)
336typedef struct _sni_entry sni_entry;
337
338struct _sni_entry {
339 const char *name;
340 x509_crt *cert;
341 pk_context *key;
342 sni_entry *next;
343};
344
345/*
346 * Parse a string of triplets name1,crt1,key1[,name2,crt2,key2[,...]]
347 * into a usable sni_entry list.
348 *
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200349 * Modifies the input string! This is not production quality!
350 * (leaks memory if parsing fails, no error reporting, ...)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100351 */
352sni_entry *sni_parse( char *sni_string )
353{
354 sni_entry *cur = NULL, *new = NULL;
355 char *p = sni_string;
356 char *end = p;
357 char *crt_file, *key_file;
358
359 while( *end != '\0' )
360 ++end;
361 *end = ',';
362
363 while( p <= end )
364 {
365 if( ( new = polarssl_malloc( sizeof( sni_entry ) ) ) == NULL )
366 return( NULL );
367
368 memset( new, 0, sizeof( sni_entry ) );
369
370 if( ( new->cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL ||
371 ( new->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200372 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100373
374 x509_crt_init( new->cert );
375 pk_init( new->key );
376
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200377 GET_ITEM( new->name );
378 GET_ITEM( crt_file );
379 GET_ITEM( key_file );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100380
381 if( x509_crt_parse_file( new->cert, crt_file ) != 0 ||
382 pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200383 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100384
385 new->next = cur;
386 cur = new;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100387 }
388
389 return( cur );
390}
391
392void sni_free( sni_entry *head )
393{
394 sni_entry *cur = head, *next;
395
396 while( cur != NULL )
397 {
398 x509_crt_free( cur->cert );
399 polarssl_free( cur->cert );
400
401 pk_free( cur->key );
402 polarssl_free( cur->key );
403
404 next = cur->next;
405 polarssl_free( cur );
406 cur = next;
407 }
408}
409
410/*
411 * SNI callback.
412 */
413int sni_callback( void *p_info, ssl_context *ssl,
414 const unsigned char *name, size_t name_len )
415{
416 sni_entry *cur = (sni_entry *) p_info;
417
418 while( cur != NULL )
419 {
420 if( name_len == strlen( cur->name ) &&
421 memcmp( name, cur->name, name_len ) == 0 )
422 {
423 ssl_set_own_cert( ssl, cur->cert, cur->key );
424 return( 0 );
425 }
426
427 cur = cur->next;
428 }
429
430 return( -1 );
431}
432
433#endif /* POLARSSL_SNI */
434
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200435#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
436
437#define HEX2NUM( c ) \
438 if( c >= '0' && c <= '9' ) \
439 c -= '0'; \
440 else if( c >= 'a' && c <= 'f' ) \
441 c -= 'a' - 10; \
442 else if( c >= 'A' && c <= 'F' ) \
443 c -= 'A' - 10; \
444 else \
445 return( -1 );
446
447/*
448 * Convert a hex string to bytes.
449 * Return 0 on success, -1 on error.
450 */
451int unhexify( unsigned char *output, const char *input, size_t *olen )
452{
453 unsigned char c;
454 size_t j;
455
456 *olen = strlen( input );
457 if( *olen % 2 != 0 || *olen / 2 > MAX_PSK_LEN )
458 return( -1 );
459 *olen /= 2;
460
461 for( j = 0; j < *olen * 2; j += 2 )
462 {
463 c = input[j];
464 HEX2NUM( c );
465 output[ j / 2 ] = c << 4;
466
467 c = input[j + 1];
468 HEX2NUM( c );
469 output[ j / 2 ] |= c;
470 }
471
472 return( 0 );
473}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200474
475typedef struct _psk_entry psk_entry;
476
477struct _psk_entry
478{
479 const char *name;
480 size_t key_len;
481 unsigned char key[MAX_PSK_LEN];
482 psk_entry *next;
483};
484
485/*
486 * Parse a string of pairs name1,key1[,name2,key2[,...]]
487 * into a usable psk_entry list.
488 *
489 * Modifies the input string! This is not production quality!
490 * (leaks memory if parsing fails, no error reporting, ...)
491 */
492psk_entry *psk_parse( char *psk_string )
493{
494 psk_entry *cur = NULL, *new = NULL;
495 char *p = psk_string;
496 char *end = p;
497 char *key_hex;
498
499 while( *end != '\0' )
500 ++end;
501 *end = ',';
502
503 while( p <= end )
504 {
505 if( ( new = polarssl_malloc( sizeof( psk_entry ) ) ) == NULL )
506 return( NULL );
507
508 memset( new, 0, sizeof( psk_entry ) );
509
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200510 GET_ITEM( new->name );
511 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200512
513 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
514 return( NULL );
515
516 new->next = cur;
517 cur = new;
518 }
519
520 return( cur );
521}
522
523/*
524 * Free a list of psk_entry's
525 */
526void psk_free( psk_entry *head )
527{
528 psk_entry *next;
529
530 while( head != NULL )
531 {
532 next = head->next;
533 polarssl_free( head );
534 head = next;
535 }
536}
537
538/*
539 * PSK callback
540 */
541int psk_callback( void *p_info, ssl_context *ssl,
542 const unsigned char *name, size_t name_len )
543{
544 psk_entry *cur = (psk_entry *) p_info;
545
546 while( cur != NULL )
547 {
548 if( name_len == strlen( cur->name ) &&
549 memcmp( name, cur->name, name_len ) == 0 )
550 {
551 return( ssl_set_psk( ssl, cur->key, cur->key_len,
552 name, name_len ) );
553 }
554
555 cur = cur->next;
556 }
557
558 return( -1 );
559}
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200560#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
561
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000562int main( int argc, char *argv[] )
563{
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200564 int ret = 0, len, written, frags;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000565 int listen_fd;
566 int client_fd = -1;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200567 int version_suites[4][2];
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200568 unsigned char buf[IO_BUF_LEN];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200569#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200570 unsigned char psk[MAX_PSK_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +0200571 size_t psk_len = 0;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200572 psk_entry *psk_info;
Paul Bakkered27a042013-04-18 22:46:23 +0200573#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200574 const char *pers = "ssl_server2";
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000575
576 entropy_context entropy;
577 ctr_drbg_context ctr_drbg;
578 ssl_context ssl;
Paul Bakker36713e82013-09-17 13:25:29 +0200579#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200580 x509_crt cacert;
581 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200582 pk_context pkey;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200583 x509_crt srvcert2;
584 pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200585 int key_cert_init = 0, key_cert_init2 = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200586#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200587#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
588 dhm_context dhm;
589#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000590#if defined(POLARSSL_SSL_CACHE_C)
591 ssl_cache_context cache;
592#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100593#if defined(POLARSSL_SNI)
594 sni_entry *sni_info = NULL;
595#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200596#if defined(POLARSSL_SSL_ALPN)
597 const char *alpn_list[10];
598#endif
Paul Bakker82024bf2013-07-04 11:52:32 +0200599#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
600 unsigned char alloc_buf[100000];
601#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000602
603 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000604 char *p, *q;
605 const int *list;
606
Paul Bakker82024bf2013-07-04 11:52:32 +0200607#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
608 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
609#endif
610
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000611 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200612 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000613 */
614 listen_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200615 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200616#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200617 x509_crt_init( &cacert );
618 x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200619 pk_init( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200620 x509_crt_init( &srvcert2 );
621 pk_init( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +0200622#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200623#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
624 memset( &dhm, 0, sizeof( dhm_context ) );
625#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000626#if defined(POLARSSL_SSL_CACHE_C)
627 ssl_cache_init( &cache );
628#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200629#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200630 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200631#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000632
633 if( argc == 0 )
634 {
635 usage:
636 if( ret == 0 )
637 ret = 1;
638
639 printf( USAGE );
640
641 list = ssl_list_ciphersuites();
642 while( *list )
643 {
Paul Bakkerbcbe2d82013-04-19 09:10:20 +0200644 printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200645 list++;
646 if( !*list )
647 break;
648 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000649 list++;
650 }
651 printf("\n");
652 goto exit;
653 }
654
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100655 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000656 opt.server_port = DFL_SERVER_PORT;
657 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100658 opt.nbio = DFL_NBIO;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000659 opt.ca_file = DFL_CA_FILE;
660 opt.ca_path = DFL_CA_PATH;
661 opt.crt_file = DFL_CRT_FILE;
662 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200663 opt.crt_file2 = DFL_CRT_FILE2;
664 opt.key_file2 = DFL_KEY_FILE2;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200665 opt.psk = DFL_PSK;
666 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200667 opt.psk_list = DFL_PSK_LIST;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000668 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200669 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000670 opt.renegotiation = DFL_RENEGOTIATION;
671 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100672 opt.renegotiate = DFL_RENEGOTIATE;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000673 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200674 opt.max_version = DFL_MAX_VERSION;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100675 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200676 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200677 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100678 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100679 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100680 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100681 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200682 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200683 opt.dhm_file = DFL_DHM_FILE;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000684
685 for( i = 1; i < argc; i++ )
686 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000687 p = argv[i];
688 if( ( q = strchr( p, '=' ) ) == NULL )
689 goto usage;
690 *q++ = '\0';
691
692 if( strcmp( p, "server_port" ) == 0 )
693 {
694 opt.server_port = atoi( q );
695 if( opt.server_port < 1 || opt.server_port > 65535 )
696 goto usage;
697 }
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100698 else if( strcmp( p, "server_addr" ) == 0 )
699 opt.server_addr = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000700 else if( strcmp( p, "debug_level" ) == 0 )
701 {
702 opt.debug_level = atoi( q );
703 if( opt.debug_level < 0 || opt.debug_level > 65535 )
704 goto usage;
705 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100706 else if( strcmp( p, "nbio" ) == 0 )
707 {
708 opt.nbio = atoi( q );
709 if( opt.nbio < 0 || opt.nbio > 2 )
710 goto usage;
711 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000712 else if( strcmp( p, "ca_file" ) == 0 )
713 opt.ca_file = q;
714 else if( strcmp( p, "ca_path" ) == 0 )
715 opt.ca_path = q;
716 else if( strcmp( p, "crt_file" ) == 0 )
717 opt.crt_file = q;
718 else if( strcmp( p, "key_file" ) == 0 )
719 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200720 else if( strcmp( p, "crt_file2" ) == 0 )
721 opt.crt_file2 = q;
722 else if( strcmp( p, "key_file2" ) == 0 )
723 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200724 else if( strcmp( p, "dhm_file" ) == 0 )
725 opt.dhm_file = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200726 else if( strcmp( p, "psk" ) == 0 )
727 opt.psk = q;
728 else if( strcmp( p, "psk_identity" ) == 0 )
729 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200730 else if( strcmp( p, "psk_list" ) == 0 )
731 opt.psk_list = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000732 else if( strcmp( p, "force_ciphersuite" ) == 0 )
733 {
734 opt.force_ciphersuite[0] = -1;
735
736 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
737
738 if( opt.force_ciphersuite[0] <= 0 )
739 {
740 ret = 2;
741 goto usage;
742 }
743 opt.force_ciphersuite[1] = 0;
744 }
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200745 else if( strcmp( p, "version_suites" ) == 0 )
746 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000747 else if( strcmp( p, "renegotiation" ) == 0 )
748 {
749 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
750 SSL_RENEGOTIATION_DISABLED;
751 }
752 else if( strcmp( p, "allow_legacy" ) == 0 )
753 {
754 opt.allow_legacy = atoi( q );
755 if( opt.allow_legacy < 0 || opt.allow_legacy > 1 )
756 goto usage;
757 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100758 else if( strcmp( p, "renegotiate" ) == 0 )
759 {
760 opt.renegotiate = atoi( q );
761 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
762 goto usage;
763 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000764 else if( strcmp( p, "min_version" ) == 0 )
765 {
766 if( strcmp( q, "ssl3" ) == 0 )
767 opt.min_version = SSL_MINOR_VERSION_0;
768 else if( strcmp( q, "tls1" ) == 0 )
769 opt.min_version = SSL_MINOR_VERSION_1;
770 else if( strcmp( q, "tls1_1" ) == 0 )
771 opt.min_version = SSL_MINOR_VERSION_2;
772 else if( strcmp( q, "tls1_2" ) == 0 )
773 opt.min_version = SSL_MINOR_VERSION_3;
774 else
775 goto usage;
776 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200777 else if( strcmp( p, "max_version" ) == 0 )
778 {
779 if( strcmp( q, "ssl3" ) == 0 )
780 opt.max_version = SSL_MINOR_VERSION_0;
781 else if( strcmp( q, "tls1" ) == 0 )
782 opt.max_version = SSL_MINOR_VERSION_1;
783 else if( strcmp( q, "tls1_1" ) == 0 )
784 opt.max_version = SSL_MINOR_VERSION_2;
785 else if( strcmp( q, "tls1_2" ) == 0 )
786 opt.max_version = SSL_MINOR_VERSION_3;
787 else
788 goto usage;
789 }
790 else if( strcmp( p, "force_version" ) == 0 )
791 {
792 if( strcmp( q, "ssl3" ) == 0 )
793 {
794 opt.min_version = SSL_MINOR_VERSION_0;
795 opt.max_version = SSL_MINOR_VERSION_0;
796 }
797 else if( strcmp( q, "tls1" ) == 0 )
798 {
799 opt.min_version = SSL_MINOR_VERSION_1;
800 opt.max_version = SSL_MINOR_VERSION_1;
801 }
802 else if( strcmp( q, "tls1_1" ) == 0 )
803 {
804 opt.min_version = SSL_MINOR_VERSION_2;
805 opt.max_version = SSL_MINOR_VERSION_2;
806 }
807 else if( strcmp( q, "tls1_2" ) == 0 )
808 {
809 opt.min_version = SSL_MINOR_VERSION_3;
810 opt.max_version = SSL_MINOR_VERSION_3;
811 }
812 else
813 goto usage;
814 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100815 else if( strcmp( p, "auth_mode" ) == 0 )
816 {
817 if( strcmp( q, "none" ) == 0 )
818 opt.auth_mode = SSL_VERIFY_NONE;
819 else if( strcmp( q, "optional" ) == 0 )
820 opt.auth_mode = SSL_VERIFY_OPTIONAL;
821 else if( strcmp( q, "required" ) == 0 )
822 opt.auth_mode = SSL_VERIFY_REQUIRED;
823 else
824 goto usage;
825 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200826 else if( strcmp( p, "max_frag_len" ) == 0 )
827 {
828 if( strcmp( q, "512" ) == 0 )
829 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
830 else if( strcmp( q, "1024" ) == 0 )
831 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
832 else if( strcmp( q, "2048" ) == 0 )
833 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
834 else if( strcmp( q, "4096" ) == 0 )
835 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
836 else
837 goto usage;
838 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200839 else if( strcmp( p, "alpn" ) == 0 )
840 {
841 opt.alpn_string = q;
842 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200843 else if( strcmp( p, "tickets" ) == 0 )
844 {
845 opt.tickets = atoi( q );
846 if( opt.tickets < 0 || opt.tickets > 1 )
847 goto usage;
848 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100849 else if( strcmp( p, "ticket_timeout" ) == 0 )
850 {
851 opt.ticket_timeout = atoi( q );
852 if( opt.ticket_timeout < 0 )
853 goto usage;
854 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100855 else if( strcmp( p, "cache_max" ) == 0 )
856 {
857 opt.cache_max = atoi( q );
858 if( opt.cache_max < 0 )
859 goto usage;
860 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100861 else if( strcmp( p, "cache_timeout" ) == 0 )
862 {
863 opt.cache_timeout = atoi( q );
864 if( opt.cache_timeout < 0 )
865 goto usage;
866 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100867 else if( strcmp( p, "sni" ) == 0 )
868 {
869 opt.sni = q;
870 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000871 else
872 goto usage;
873 }
874
Paul Bakkerc73079a2014-04-25 16:34:30 +0200875#if defined(POLARSSL_DEBUG_C)
876 debug_set_threshold( opt.debug_level );
877#endif
878
Paul Bakkerc1516be2013-06-29 16:01:32 +0200879 if( opt.force_ciphersuite[0] > 0 )
880 {
881 const ssl_ciphersuite_t *ciphersuite_info;
882 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
883
Paul Bakker5b55b792013-07-19 13:43:43 +0200884 if( opt.max_version != -1 &&
885 ciphersuite_info->min_minor_ver > opt.max_version )
886 {
887 printf("forced ciphersuite not allowed with this protocol version\n");
888 ret = 2;
889 goto usage;
890 }
891 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +0200892 ciphersuite_info->max_minor_ver < opt.min_version )
893 {
894 printf("forced ciphersuite not allowed with this protocol version\n");
895 ret = 2;
896 goto usage;
897 }
Paul Bakker5b55b792013-07-19 13:43:43 +0200898 if( opt.max_version > ciphersuite_info->max_minor_ver )
899 opt.max_version = ciphersuite_info->max_minor_ver;
900 if( opt.min_version < ciphersuite_info->min_minor_ver )
901 opt.min_version = ciphersuite_info->min_minor_ver;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200902 }
903
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200904 if( opt.version_suites != NULL )
905 {
906 const char *name[4] = { 0 };
907
908 /* Parse 4-element coma-separated list */
909 for( i = 0, p = (char *) opt.version_suites;
910 i < 4 && *p != '\0';
911 i++ )
912 {
913 name[i] = p;
914
915 /* Terminate the current string and move on to next one */
916 while( *p != ',' && *p != '\0' )
917 p++;
918 if( *p == ',' )
919 *p++ = '\0';
920 }
921
922 if( i != 4 )
923 {
924 printf( "too few values for version_suites\n" );
925 ret = 1;
926 goto exit;
927 }
928
929 memset( version_suites, 0, sizeof( version_suites ) );
930
931 /* Get the suites identifiers from their name */
932 for( i = 0; i < 4; i++ )
933 {
934 version_suites[i][0] = ssl_get_ciphersuite_id( name[i] );
935
936 if( version_suites[i][0] == 0 )
937 {
938 printf( "unknown ciphersuite: '%s'\n", name[i] );
939 ret = 2;
940 goto usage;
941 }
942 }
943 }
944
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200945#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000946 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200947 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +0200948 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200949 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +0200950 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200951 printf( "pre-shared key not valid hex\n" );
952 goto exit;
953 }
954
955 if( opt.psk_list != NULL )
956 {
957 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +0200958 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200959 printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +0200960 goto exit;
961 }
Paul Bakkerfbb17802013-04-17 19:10:21 +0200962 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200963#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +0200964
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200965#if defined(POLARSSL_SSL_ALPN)
966 if( opt.alpn_string != NULL )
967 {
968 p = (char *) opt.alpn_string;
969 i = 0;
970
971 /* Leave room for a final NULL in alpn_list */
972 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
973 {
974 alpn_list[i++] = p;
975
976 /* Terminate the current string and move on to next one */
977 while( *p != ',' && *p != '\0' )
978 p++;
979 if( *p == ',' )
980 *p++ = '\0';
981 }
982 }
983#endif /* POLARSSL_SSL_ALPN */
984
Paul Bakkerfbb17802013-04-17 19:10:21 +0200985 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000986 * 0. Initialize the RNG and the session data
987 */
988 printf( "\n . Seeding the random number generator..." );
989 fflush( stdout );
990
991 entropy_init( &entropy );
992 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200993 (const unsigned char *) pers,
994 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000995 {
996 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
997 goto exit;
998 }
999
1000 printf( " ok\n" );
1001
Paul Bakker36713e82013-09-17 13:25:29 +02001002#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001003 /*
1004 * 1.1. Load the trusted CA
1005 */
1006 printf( " . Loading the CA root certificate ..." );
1007 fflush( stdout );
1008
1009#if defined(POLARSSL_FS_IO)
1010 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001011 if( strcmp( opt.ca_path, "none" ) == 0 )
1012 ret = 0;
1013 else
1014 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001015 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001016 if( strcmp( opt.ca_file, "none" ) == 0 )
1017 ret = 0;
1018 else
1019 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001020 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001021#endif
1022#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +02001023 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
1024 strlen( test_ca_list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001025#else
1026 {
1027 ret = 1;
1028 printf("POLARSSL_CERTS_C not defined.");
1029 }
1030#endif
1031 if( ret < 0 )
1032 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001033 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001034 goto exit;
1035 }
1036
1037 printf( " ok (%d skipped)\n", ret );
1038
1039 /*
1040 * 1.2. Load own certificate and private key
1041 */
1042 printf( " . Loading the server cert. and key..." );
1043 fflush( stdout );
1044
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001045#if defined(POLARSSL_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001046 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001047 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001048 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001049 if( ( ret = x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
1050 {
1051 printf( " failed\n ! x509_crt_parse_file returned -0x%x\n\n",
1052 -ret );
1053 goto exit;
1054 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001055 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001056 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001057 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001058 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001059 if( ( ret = pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
1060 {
1061 printf( " failed\n ! pk_parse_keyfile returned -0x%x\n\n", -ret );
1062 goto exit;
1063 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001064 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001065 if( key_cert_init == 1 )
1066 {
1067 printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
1068 goto exit;
1069 }
1070
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001071 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001072 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001073 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001074 if( ( ret = x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
1075 {
1076 printf( " failed\n ! x509_crt_parse_file(2) returned -0x%x\n\n",
1077 -ret );
1078 goto exit;
1079 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001080 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001081 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001082 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001083 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001084 if( ( ret = pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
1085 {
1086 printf( " failed\n ! pk_parse_keyfile(2) returned -0x%x\n\n",
1087 -ret );
1088 goto exit;
1089 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001090 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001091 if( key_cert_init2 == 1 )
1092 {
1093 printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
1094 goto exit;
1095 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001096#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001097 if( key_cert_init == 0 &&
1098 strcmp( opt.crt_file, "none" ) != 0 &&
1099 strcmp( opt.key_file, "none" ) != 0 &&
1100 key_cert_init2 == 0 &&
1101 strcmp( opt.crt_file2, "none" ) != 0 &&
1102 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001103 {
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001104#if !defined(POLARSSL_CERTS_C)
1105 printf( "Not certificated or key provided, and \n"
1106 "POLARSSL_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001107 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001108#else
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001109#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001110 if( ( ret = x509_crt_parse( &srvcert,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001111 (const unsigned char *) test_srv_crt_rsa,
1112 strlen( test_srv_crt_rsa ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001113 {
1114 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
1115 goto exit;
1116 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001117 if( ( ret = pk_parse_key( &pkey,
1118 (const unsigned char *) test_srv_key_rsa,
1119 strlen( test_srv_key_rsa ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001120 {
1121 printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
1122 goto exit;
1123 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001124 key_cert_init = 2;
1125#endif /* POLARSSL_RSA_C */
1126#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001127 if( ( ret = x509_crt_parse( &srvcert2,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001128 (const unsigned char *) test_srv_crt_ec,
1129 strlen( test_srv_crt_ec ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001130 {
1131 printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret );
1132 goto exit;
1133 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001134 if( ( ret = pk_parse_key( &pkey2,
1135 (const unsigned char *) test_srv_key_ec,
1136 strlen( test_srv_key_ec ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001137 {
1138 printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret );
1139 goto exit;
1140 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001141 key_cert_init2 = 2;
1142#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001143#endif /* POLARSSL_CERTS_C */
1144 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001145
1146 printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +02001147#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001148
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001149#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1150 if( opt.dhm_file != NULL )
1151 {
1152 printf( " . Loading DHM parameters..." );
1153 fflush( stdout );
1154
1155 if( ( ret = dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
1156 {
1157 printf( " failed\n ! dhm_parse_dhmfile returned -0x%04X\n\n",
1158 -ret );
1159 goto exit;
1160 }
1161
1162 printf( " ok\n" );
1163 }
1164#endif
1165
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001166#if defined(POLARSSL_SNI)
1167 if( opt.sni != NULL )
1168 {
1169 printf( " . Setting up SNI information..." );
1170 fflush( stdout );
1171
1172 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
1173 {
1174 printf( " failed\n" );
1175 goto exit;
1176 }
1177
1178 printf( " ok\n" );
1179 }
1180#endif /* POLARSSL_SNI */
1181
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001182 /*
1183 * 2. Setup the listening TCP socket
1184 */
1185 printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port );
1186 fflush( stdout );
1187
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001188 if( ( ret = net_bind( &listen_fd, opt.server_addr,
1189 opt.server_port ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001190 {
1191 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
1192 goto exit;
1193 }
1194
1195 printf( " ok\n" );
1196
1197 /*
1198 * 3. Setup stuff
1199 */
1200 printf( " . Setting up the SSL/TLS structure..." );
1201 fflush( stdout );
1202
1203 if( ( ret = ssl_init( &ssl ) ) != 0 )
1204 {
1205 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
1206 goto exit;
1207 }
1208
1209 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +01001210 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001211
Paul Bakker05decb22013-08-15 13:33:48 +02001212#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001213 ssl_set_max_frag_len( &ssl, opt.mfl_code );
Paul Bakker05decb22013-08-15 13:33:48 +02001214#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001215
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001216#if defined(POLARSSL_SSL_ALPN)
1217 if( opt.alpn_string != NULL )
1218 ssl_set_alpn_protocols( &ssl, alpn_list );
1219#endif
1220
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001221 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
1222 ssl_set_dbg( &ssl, my_debug, stdout );
1223
Paul Bakker0a597072012-09-25 21:55:46 +00001224#if defined(POLARSSL_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001225 if( opt.cache_max != -1 )
1226 ssl_cache_set_max_entries( &cache, opt.cache_max );
1227
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001228 if( opt.cache_timeout != -1 )
1229 ssl_cache_set_timeout( &cache, opt.cache_timeout );
1230
Paul Bakker0a597072012-09-25 21:55:46 +00001231 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
1232 ssl_cache_set, &cache );
1233#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001234
Paul Bakkera503a632013-08-14 13:48:06 +02001235#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001236 ssl_set_session_tickets( &ssl, opt.tickets );
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001237
1238 if( opt.ticket_timeout != -1 )
1239 ssl_set_session_ticket_lifetime( &ssl, opt.ticket_timeout );
Paul Bakkera503a632013-08-14 13:48:06 +02001240#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001241
Paul Bakker41c83d32013-03-20 14:39:14 +01001242 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001243 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
1244
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001245 if( opt.version_suites != NULL )
1246 {
1247 ssl_set_ciphersuites_for_version( &ssl, version_suites[0],
1248 SSL_MAJOR_VERSION_3,
1249 SSL_MINOR_VERSION_0 );
1250 ssl_set_ciphersuites_for_version( &ssl, version_suites[1],
1251 SSL_MAJOR_VERSION_3,
1252 SSL_MINOR_VERSION_1 );
1253 ssl_set_ciphersuites_for_version( &ssl, version_suites[2],
1254 SSL_MAJOR_VERSION_3,
1255 SSL_MINOR_VERSION_2 );
1256 ssl_set_ciphersuites_for_version( &ssl, version_suites[3],
1257 SSL_MAJOR_VERSION_3,
1258 SSL_MINOR_VERSION_3 );
1259 }
1260
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001261 ssl_set_renegotiation( &ssl, opt.renegotiation );
1262 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
1263
Paul Bakker36713e82013-09-17 13:25:29 +02001264#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001265 if( strcmp( opt.ca_path, "none" ) != 0 &&
1266 strcmp( opt.ca_file, "none" ) != 0 )
1267 {
1268 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
1269 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001270 if( key_cert_init )
1271 ssl_set_own_cert( &ssl, &srvcert, &pkey );
1272 if( key_cert_init2 )
1273 ssl_set_own_cert( &ssl, &srvcert2, &pkey2 );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001274#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001275
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001276#if defined(POLARSSL_SNI)
1277 if( opt.sni != NULL )
1278 ssl_set_sni( &ssl, sni_callback, sni_info );
1279#endif
1280
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001281#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001282 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
1283 {
1284 ret = ssl_set_psk( &ssl, psk, psk_len,
1285 (const unsigned char *) opt.psk_identity,
1286 strlen( opt.psk_identity ) );
1287 if( ret != 0 )
1288 {
1289 printf( " failed\n ssl_set_psk returned -0x%04X\n\n", - ret );
1290 goto exit;
1291 }
1292 }
1293
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001294 if( opt.psk_list != NULL )
1295 ssl_set_psk_cb( &ssl, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02001296#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001297
1298#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00001299 /*
1300 * Use different group than default DHM group
1301 */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001302#if defined(POLARSSL_FS_IO)
1303 if( opt.dhm_file != NULL )
1304 ret = ssl_set_dh_param_ctx( &ssl, &dhm );
1305 else
1306#endif
1307 ret = ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
1308 POLARSSL_DHM_RFC5114_MODP_2048_G );
1309
1310 if( ret != 0 )
1311 {
1312 printf( " failed\n ssl_set_dh_param returned -0x%04X\n\n", - ret );
1313 goto exit;
1314 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001315#endif
1316
Paul Bakker1d29fb52012-09-28 13:28:45 +00001317 if( opt.min_version != -1 )
1318 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
1319
Paul Bakkerc1516be2013-06-29 16:01:32 +02001320 if( opt.max_version != -1 )
1321 ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1322
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001323 printf( " ok\n" );
1324
1325reset:
1326#ifdef POLARSSL_ERROR_C
1327 if( ret != 0 )
1328 {
1329 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001330 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001331 printf("Last error was: %d - %s\n\n", ret, error_buf );
1332 }
1333#endif
1334
1335 if( client_fd != -1 )
1336 net_close( client_fd );
1337
1338 ssl_session_reset( &ssl );
1339
1340 /*
1341 * 3. Wait until a client connects
1342 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001343 client_fd = -1;
1344
1345 printf( " . Waiting for a remote connection ..." );
1346 fflush( stdout );
1347
1348 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
1349 {
1350 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
1351 goto exit;
1352 }
1353
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001354 if( opt.nbio > 0 )
1355 ret = net_set_nonblock( client_fd );
1356 else
1357 ret = net_set_block( client_fd );
1358 if( ret != 0 )
1359 {
1360 printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
1361 goto exit;
1362 }
1363
1364 if( opt.nbio == 2 )
1365 ssl_set_bio( &ssl, my_recv, &client_fd, my_send, &client_fd );
1366 else
1367 ssl_set_bio( &ssl, net_recv, &client_fd, net_send, &client_fd );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001368
1369 printf( " ok\n" );
1370
1371 /*
1372 * 4. Handshake
1373 */
1374 printf( " . Performing the SSL/TLS handshake..." );
1375 fflush( stdout );
1376
1377 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1378 {
1379 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
1380 {
1381 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001382 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001383 }
1384 }
1385
Manuel Pégourié-Gonnardc580a002014-02-12 10:15:30 +01001386 printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1387 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001388
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001389#if defined(POLARSSL_SSL_ALPN)
1390 if( opt.alpn_string != NULL )
1391 {
1392 const char *alp = ssl_get_alpn_protocol( &ssl );
1393 printf( " [ Application Layer Protocol is %s ]\n",
1394 alp ? alp : "(none)" );
1395 }
1396#endif
1397
Paul Bakker36713e82013-09-17 13:25:29 +02001398#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001399 /*
1400 * 5. Verify the server certificate
1401 */
1402 printf( " . Verifying peer X.509 certificate..." );
1403
1404 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1405 {
1406 printf( " failed\n" );
1407
Paul Bakkerb0550d92012-10-30 07:51:03 +00001408 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001409 printf( " ! no client certificate sent\n" );
1410
1411 if( ( ret & BADCERT_EXPIRED ) != 0 )
1412 printf( " ! client certificate has expired\n" );
1413
1414 if( ( ret & BADCERT_REVOKED ) != 0 )
1415 printf( " ! client certificate has been revoked\n" );
1416
1417 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
1418 printf( " ! self-signed or not signed by a trusted CA\n" );
1419
1420 printf( "\n" );
1421 }
1422 else
1423 printf( " ok\n" );
1424
Paul Bakkerb0550d92012-10-30 07:51:03 +00001425 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001426 {
1427 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001428 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1429 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001430 printf( "%s\n", buf );
1431 }
Paul Bakker36713e82013-09-17 13:25:29 +02001432#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001433
1434 /*
1435 * 6. Read the HTTP Request
1436 */
1437 printf( " < Read from client:" );
1438 fflush( stdout );
1439
1440 do
1441 {
1442 len = sizeof( buf ) - 1;
1443 memset( buf, 0, sizeof( buf ) );
1444 ret = ssl_read( &ssl, buf, len );
1445
1446 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
1447 continue;
1448
1449 if( ret <= 0 )
1450 {
1451 switch( ret )
1452 {
1453 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1454 printf( " connection was closed gracefully\n" );
1455 break;
1456
1457 case POLARSSL_ERR_NET_CONN_RESET:
1458 printf( " connection was reset by peer\n" );
1459 break;
1460
1461 default:
1462 printf( " ssl_read returned -0x%x\n", -ret );
1463 break;
1464 }
1465
1466 break;
1467 }
1468
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001469 if( ssl_get_bytes_avail( &ssl ) == 0 )
1470 {
1471 len = ret;
1472 buf[len] = '\0';
1473 printf( " %d bytes read\n\n%s\n", len, (char *) buf );
1474 }
1475 else
1476 {
1477 int extra_len, ori_len;
1478 unsigned char *larger_buf;
1479
1480 ori_len = ret;
1481 extra_len = ssl_get_bytes_avail( &ssl );
1482
1483 larger_buf = polarssl_malloc( ori_len + extra_len + 1 );
1484 if( larger_buf == NULL )
1485 {
1486 printf( " ! memory allocation failed\n" );
1487 ret = 1;
1488 goto exit;
1489 }
1490
1491 memset( larger_buf, 0, ori_len + extra_len );
1492 memcpy( larger_buf, buf, ori_len );
1493
1494 /* This read should never fail */
1495 ret = ssl_read( &ssl, larger_buf + ori_len, extra_len );
1496 if( ret != extra_len )
1497 {
1498 printf( " ! ssl_read failed on cached data\n" );
1499 ret = 1;
1500 goto exit;
1501 }
1502
1503 larger_buf[ori_len + extra_len] = '\0';
1504 printf( " %u bytes read (%u + %u)\n\n%s\n",
1505 ori_len + extra_len, ori_len, extra_len, (char *) buf );
1506
1507 polarssl_free( larger_buf );
1508 }
1509
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001510
Paul Bakker82024bf2013-07-04 11:52:32 +02001511 if( memcmp( buf, "SERVERQUIT", 10 ) == 0 )
Manuel Pégourié-Gonnarde8ea0c02013-09-07 17:09:14 +02001512 {
1513 ret = 0;
Paul Bakker82024bf2013-07-04 11:52:32 +02001514 goto exit;
Manuel Pégourié-Gonnarde8ea0c02013-09-07 17:09:14 +02001515 }
Paul Bakker82024bf2013-07-04 11:52:32 +02001516
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001517 if( ret > 0 )
1518 break;
1519 }
1520 while( 1 );
1521
1522 /*
1523 * 7. Write the 200 Response
1524 */
1525 printf( " > Write to client:" );
1526 fflush( stdout );
1527
1528 len = sprintf( (char *) buf, HTTP_RESPONSE,
1529 ssl_get_ciphersuite( &ssl ) );
1530
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001531 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001532 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001533 while( ( ret = ssl_write( &ssl, buf + written, len - written ) ) <= 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001534 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001535 if( ret == POLARSSL_ERR_NET_CONN_RESET )
1536 {
1537 printf( " failed\n ! peer closed the connection\n\n" );
1538 goto reset;
1539 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001540
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001541 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
1542 {
1543 printf( " failed\n ! ssl_write returned %d\n\n", ret );
1544 goto exit;
1545 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001546 }
1547 }
1548
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001549 buf[written] = '\0';
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001550 printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001551
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001552 if( opt.renegotiate )
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001553 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001554 /*
1555 * Request renegotiation (this must be done when the client is still
1556 * waiting for input from our side).
1557 */
1558 printf( " . Requestion renegotiation..." );
1559 fflush( stdout );
1560 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001561 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001562 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1563 ret != POLARSSL_ERR_NET_WANT_WRITE )
1564 {
1565 printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01001566 goto exit;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001567 }
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001568 }
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001569
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001570 /*
1571 * Should be a while loop, not an if, but here we're not actually
1572 * expecting data from the client, and since we're running tests
1573 * locally, we can just hope the handshake will finish the during the
1574 * first call.
1575 */
1576 if( ( ret = ssl_read( &ssl, buf, 0 ) ) != 0 )
1577 {
1578 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1579 ret != POLARSSL_ERR_NET_WANT_WRITE )
1580 {
1581 printf( " failed\n ! ssl_read returned %d\n\n", ret );
1582
1583 /* Unexpected message probably means client didn't renegotiate
1584 * as requested */
1585 if( ret == POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE )
1586 goto reset;
1587 else
1588 goto exit;
1589 }
1590 }
1591
1592 printf( " ok\n" );
1593 }
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001594
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001595 printf( " . Closing the connection..." );
1596
1597 while( ( ret = ssl_close_notify( &ssl ) ) < 0 )
1598 {
1599 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1600 ret != POLARSSL_ERR_NET_WANT_WRITE )
1601 {
1602 printf( " failed\n ! ssl_close_notify returned %d\n\n", ret );
1603 goto reset;
1604 }
1605 }
1606
1607 printf( " ok\n" );
1608
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001609 ret = 0;
1610 goto reset;
1611
1612exit:
1613
1614#ifdef POLARSSL_ERROR_C
1615 if( ret != 0 )
1616 {
1617 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001618 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001619 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
1620 }
1621#endif
1622
Paul Bakker0c226102014-04-17 16:02:36 +02001623 if( client_fd != -1 )
1624 net_close( client_fd );
1625
Paul Bakker36713e82013-09-17 13:25:29 +02001626#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker36713e82013-09-17 13:25:29 +02001627 x509_crt_free( &cacert );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001628 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02001629 pk_free( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001630 x509_crt_free( &srvcert2 );
1631 pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02001632#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001633#if defined(POLARSSL_SNI)
1634 sni_free( sni_info );
1635#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001636
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001637 ssl_free( &ssl );
Paul Bakker1ffefac2013-09-28 15:23:03 +02001638 entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001639
Paul Bakker0a597072012-09-25 21:55:46 +00001640#if defined(POLARSSL_SSL_CACHE_C)
1641 ssl_cache_free( &cache );
1642#endif
1643
Paul Bakker1337aff2013-09-29 14:45:34 +02001644#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1645#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker82024bf2013-07-04 11:52:32 +02001646 memory_buffer_alloc_status();
1647#endif
Paul Bakker1337aff2013-09-29 14:45:34 +02001648 memory_buffer_alloc_free();
1649#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02001650
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001651#if defined(_WIN32)
1652 printf( " + Press Enter to exit this program.\n" );
1653 fflush( stdout ); getchar();
1654#endif
1655
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001656 // Shell can not handle large exit numbers -> 1 for errors
1657 if( ret < 0 )
1658 ret = 1;
1659
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001660 return( ret );
1661}
1662#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
1663 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
1664 POLARSSL_CTR_DRBG_C */