blob: a09aa3b0aa24c6ebe1d60284ca8c3d9917f2c945 [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é-Gonnard8a4d5712014-06-24 14:19:59 +020032#if !defined(POLARSSL_ENTROPY_C) || \
33 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \
34 !defined(POLARSSL_NET_C) || !defined(POLARSSL_CTR_DRBG_C)
35#include <stdio.h>
36int main( int argc, char *argv[] )
37{
38 ((void) argc);
39 ((void) argv);
40
41 printf("POLARSSL_ENTROPY_C and/or "
42 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
43 "POLARSSL_NET_C and/or POLARSSL_CTR_DRBG_C not defined.\n");
44 return( 0 );
45}
46#else
47
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +010048#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) && defined(POLARSSL_FS_IO)
49#define POLARSSL_SNI
50#endif
51
52#if defined(POLARSSL_PLATFORM_C)
53#include "polarssl/platform.h"
54#else
55#define polarssl_malloc malloc
56#define polarssl_free free
57#endif
58
Paul Bakkerb60b95f2012-09-25 09:05:17 +000059#if defined(_WIN32)
60#include <windows.h>
61#endif
62
63#include <string.h>
64#include <stdlib.h>
65#include <stdio.h>
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +020066#include <signal.h>
Paul Bakkerb60b95f2012-09-25 09:05:17 +000067
Paul Bakkerb60b95f2012-09-25 09:05:17 +000068#include "polarssl/net.h"
69#include "polarssl/ssl.h"
70#include "polarssl/entropy.h"
71#include "polarssl/ctr_drbg.h"
72#include "polarssl/certs.h"
73#include "polarssl/x509.h"
74#include "polarssl/error.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020075#include "polarssl/debug.h"
Paul Bakkerb60b95f2012-09-25 09:05:17 +000076
Paul Bakker0a597072012-09-25 21:55:46 +000077#if defined(POLARSSL_SSL_CACHE_C)
78#include "polarssl/ssl_cache.h"
79#endif
80
Paul Bakker82024bf2013-07-04 11:52:32 +020081#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
82#include "polarssl/memory.h"
83#endif
84
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +010085#define DFL_SERVER_ADDR NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000086#define DFL_SERVER_PORT 4433
Paul Bakkerb60b95f2012-09-25 09:05:17 +000087#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +010088#define DFL_NBIO 0
Paul Bakkerb60b95f2012-09-25 09:05:17 +000089#define DFL_CA_FILE ""
90#define DFL_CA_PATH ""
91#define DFL_CRT_FILE ""
92#define DFL_KEY_FILE ""
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +020093#define DFL_CRT_FILE2 ""
94#define DFL_KEY_FILE2 ""
Paul Bakkerfbb17802013-04-17 19:10:21 +020095#define DFL_PSK ""
96#define DFL_PSK_IDENTITY "Client_identity"
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +020097#define DFL_PSK_LIST NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +000098#define DFL_FORCE_CIPHER 0
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +020099#define DFL_VERSION_SUITES NULL
Manuel Pégourié-Gonnard00d538f2014-03-31 10:44:40 +0200100#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000101#define DFL_ALLOW_LEGACY SSL_LEGACY_NO_RENEGOTIATION
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100102#define DFL_RENEGOTIATE 0
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200103#define DFL_RENEGO_DELAY -2
Paul Bakker1d29fb52012-09-28 13:28:45 +0000104#define DFL_MIN_VERSION -1
Paul Bakkerc1516be2013-06-29 16:01:32 +0200105#define DFL_MAX_VERSION -1
Paul Bakker91ebfb52012-11-23 14:04:08 +0100106#define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200107#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200108#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100109#define DFL_TICKET_TIMEOUT -1
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100110#define DFL_CACHE_MAX -1
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100111#define DFL_CACHE_TIMEOUT -1
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100112#define DFL_SNI NULL
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200113#define DFL_ALPN_STRING NULL
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200114#define DFL_DHM_FILE NULL
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000115
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200116#define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
117 "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
118 "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
119 "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
120 "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
121 "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
122 "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 +0200123
Paul Bakker8e714d72013-07-18 11:05:13 +0200124/* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
125 * packets (for fragmentation purposes) */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000126#define HTTP_RESPONSE \
127 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
128 "<h2>PolarSSL Test Server</h2>\r\n" \
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +0200129 "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000130
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +0200131/*
132 * Size of the basic I/O buffer. Able to hold our default response.
133 *
134 * You will need to adapt the ssl_get_bytes_avail() test in ssl-opt.sh
135 * if you change this value to something outside the range <= 100 or > 500
136 */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200137#define IO_BUF_LEN 200
138
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000139/*
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000140 * global options
141 */
142struct options
143{
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100144 const char *server_addr; /* address on which the ssl service runs */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000145 int server_port; /* port on which the ssl service runs */
146 int debug_level; /* level of debugging */
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100147 int nbio; /* should I/O be blocking? */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200148 const char *ca_file; /* the file with the CA certificate(s) */
149 const char *ca_path; /* the path with the CA certificate(s) reside */
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200150 const char *crt_file; /* the file with the server certificate */
151 const char *key_file; /* the file with the server key */
152 const char *crt_file2; /* the file with the 2nd server certificate */
153 const char *key_file2; /* the file with the 2nd server key */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200154 const char *psk; /* the pre-shared key */
155 const char *psk_identity; /* the pre-shared key identity */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200156 char *psk_list; /* list of PSK id/key pairs for callback */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000157 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200158 const char *version_suites; /* per-version ciphersuites */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000159 int renegotiation; /* enable / disable renegotiation */
160 int allow_legacy; /* allow legacy renegotiation */
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100161 int renegotiate; /* attempt renegotiation? */
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200162 int renego_delay; /* delay before enforcing renegotiation */
Paul Bakker1d29fb52012-09-28 13:28:45 +0000163 int min_version; /* minimum protocol version accepted */
Paul Bakkerc1516be2013-06-29 16:01:32 +0200164 int max_version; /* maximum protocol version accepted */
Paul Bakker91ebfb52012-11-23 14:04:08 +0100165 int auth_mode; /* verify mode for connection */
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200166 unsigned char mfl_code; /* code for maximum fragment length */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200167 int tickets; /* enable / disable session tickets */
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100168 int ticket_timeout; /* session ticket lifetime */
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100169 int cache_max; /* max number of session cache entries */
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100170 int cache_timeout; /* expiration delay of session cache entries */
Paul Bakker1ebc0c52014-05-22 15:47:58 +0200171 char *sni; /* string describing sni information */
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200172 const char *alpn_string; /* ALPN supported protocols */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200173 const char *dhm_file; /* the file with the DH parameters */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000174} opt;
175
Paul Bakker3c5ef712013-06-25 16:37:45 +0200176static void my_debug( void *ctx, int level, const char *str )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000177{
Paul Bakkerc73079a2014-04-25 16:34:30 +0200178 ((void) level);
179
180 fprintf( (FILE *) ctx, "%s", str );
181 fflush( (FILE *) ctx );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000182}
183
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100184/*
185 * Test recv/send functions that make sure each try returns
186 * WANT_READ/WANT_WRITE at least once before sucesseding
187 */
188static int my_recv( void *ctx, unsigned char *buf, size_t len )
189{
190 static int first_try = 1;
191 int ret;
192
193 if( first_try )
194 {
195 first_try = 0;
196 return( POLARSSL_ERR_NET_WANT_READ );
197 }
198
199 ret = net_recv( ctx, buf, len );
200 if( ret != POLARSSL_ERR_NET_WANT_READ )
201 first_try = 1; /* Next call will be a new operation */
202 return( ret );
203}
204
205static int my_send( void *ctx, const unsigned char *buf, size_t len )
206{
207 static int first_try = 1;
208 int ret;
209
210 if( first_try )
211 {
212 first_try = 0;
213 return( POLARSSL_ERR_NET_WANT_WRITE );
214 }
215
216 ret = net_send( ctx, buf, len );
217 if( ret != POLARSSL_ERR_NET_WANT_WRITE )
218 first_try = 1; /* Next call will be a new operation */
219 return( ret );
220}
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200221
222#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000223#if defined(POLARSSL_FS_IO)
224#define USAGE_IO \
Paul Bakker1f9d02d2012-11-20 10:30:55 +0100225 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
226 " default: \"\" (pre-loaded)\n" \
227 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
228 " default: \"\" (pre-loaded) (overrides ca_file)\n" \
229 " 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 +0200230 " default: see note after key_file2\n" \
231 " key_file=%%s default: see note after key_file2\n" \
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200232 " 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 +0200233 " default: see note after key_file2\n" \
234 " key_file2=%%s default: see note below\n" \
235 " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200236 " preloaded certificate(s) and key(s) are used if available\n" \
237 " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
238 " default: preloaded parameters\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000239#else
240#define USAGE_IO \
Paul Bakkered27a042013-04-18 22:46:23 +0200241 "\n" \
242 " No file operations available (POLARSSL_FS_IO not defined)\n" \
243 "\n"
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000244#endif /* POLARSSL_FS_IO */
Paul Bakkered27a042013-04-18 22:46:23 +0200245#else
246#define USAGE_IO ""
Paul Bakker36713e82013-09-17 13:25:29 +0200247#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200248
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200249#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +0200250#define USAGE_PSK \
251 " psk=%%s default: \"\" (in hex, without 0x)\n" \
252 " psk_identity=%%s default: \"Client_identity\"\n"
253#else
254#define USAGE_PSK ""
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200255#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000256
Paul Bakkera503a632013-08-14 13:48:06 +0200257#if defined(POLARSSL_SSL_SESSION_TICKETS)
258#define USAGE_TICKETS \
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100259 " tickets=%%d default: 1 (enabled)\n" \
260 " ticket_timeout=%%d default: ticket default (1d)\n"
Paul Bakkera503a632013-08-14 13:48:06 +0200261#else
262#define USAGE_TICKETS ""
263#endif /* POLARSSL_SSL_SESSION_TICKETS */
264
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100265#if defined(POLARSSL_SSL_CACHE_C)
266#define USAGE_CACHE \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100267 " cache_max=%%d default: cache default (50)\n" \
268 " cache_timeout=%%d default: cache default (1d)\n"
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100269#else
270#define USAGE_CACHE ""
271#endif /* POLARSSL_SSL_CACHE_C */
272
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100273#if defined(POLARSSL_SNI)
274#define USAGE_SNI \
275 " sni=%%s name1,cert1,key1[,name2,cert2,key2[,...]]\n" \
276 " default: disabled\n"
277#else
278#define USAGE_SNI ""
279#endif /* POLARSSL_SNI */
280
Paul Bakker05decb22013-08-15 13:33:48 +0200281#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
282#define USAGE_MAX_FRAG_LEN \
283 " max_frag_len=%%d default: 16384 (tls default)\n" \
284 " options: 512, 1024, 2048, 4096\n"
285#else
286#define USAGE_MAX_FRAG_LEN ""
287#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
288
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200289#if defined(POLARSSL_SSL_ALPN)
290#define USAGE_ALPN \
291 " alpn=%%s default: \"\" (disabled)\n" \
292 " example: spdy/1,http/1.1\n"
293#else
294#define USAGE_ALPN ""
295#endif /* POLARSSL_SSL_ALPN */
296
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000297#define USAGE \
298 "\n usage: ssl_server2 param=<>...\n" \
299 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100300 " server_addr=%%d default: (all interfaces)\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000301 " server_port=%%d default: 4433\n" \
302 " debug_level=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100303 " nbio=%%d default: 0 (blocking I/O)\n" \
304 " options: 1 (non-blocking), 2 (added delays)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100305 "\n" \
306 " auth_mode=%%s default: \"optional\"\n" \
307 " options: none, optional, required\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000308 USAGE_IO \
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100309 USAGE_SNI \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100310 "\n" \
311 USAGE_PSK \
312 "\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000313 " renegotiation=%%d default: 1 (enabled)\n" \
314 " allow_legacy=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100315 " renegotiate=%%d default: 0 (disabled)\n" \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100316 USAGE_TICKETS \
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100317 USAGE_CACHE \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100318 USAGE_MAX_FRAG_LEN \
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200319 USAGE_ALPN \
Manuel Pégourié-Gonnard2fc243d2014-02-19 18:22:59 +0100320 "\n" \
Paul Bakker1d29fb52012-09-28 13:28:45 +0000321 " min_version=%%s default: \"ssl3\"\n" \
Paul Bakkerc1516be2013-06-29 16:01:32 +0200322 " max_version=%%s default: \"tls1_2\"\n" \
323 " force_version=%%s default: \"\" (none)\n" \
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200324 " options: ssl3, tls1, tls1_1, tls1_2\n" \
325 "\n" \
326 " version_suites=a,b,c,d per-version ciphersuites\n" \
327 " in order from ssl3 to tls1_2\n" \
328 " default: all enabled\n" \
329 " force_ciphersuite=<name> default: all enabled\n" \
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000330 " acceptable ciphersuite names:\n"
331
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200332/*
333 * Used by sni_parse and psk_parse to handle coma-separated lists
334 */
335#define GET_ITEM( dst ) \
336 dst = p; \
337 while( *p != ',' ) \
338 if( ++p > end ) \
339 return( NULL ); \
340 *p++ = '\0';
341
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100342#if defined(POLARSSL_SNI)
343typedef struct _sni_entry sni_entry;
344
345struct _sni_entry {
346 const char *name;
347 x509_crt *cert;
348 pk_context *key;
349 sni_entry *next;
350};
351
352/*
353 * Parse a string of triplets name1,crt1,key1[,name2,crt2,key2[,...]]
354 * into a usable sni_entry list.
355 *
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200356 * Modifies the input string! This is not production quality!
357 * (leaks memory if parsing fails, no error reporting, ...)
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100358 */
359sni_entry *sni_parse( char *sni_string )
360{
361 sni_entry *cur = NULL, *new = NULL;
362 char *p = sni_string;
363 char *end = p;
364 char *crt_file, *key_file;
365
366 while( *end != '\0' )
367 ++end;
368 *end = ',';
369
370 while( p <= end )
371 {
372 if( ( new = polarssl_malloc( sizeof( sni_entry ) ) ) == NULL )
373 return( NULL );
374
375 memset( new, 0, sizeof( sni_entry ) );
376
377 if( ( new->cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL ||
378 ( new->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200379 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100380
381 x509_crt_init( new->cert );
382 pk_init( new->key );
383
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200384 GET_ITEM( new->name );
385 GET_ITEM( crt_file );
386 GET_ITEM( key_file );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100387
388 if( x509_crt_parse_file( new->cert, crt_file ) != 0 ||
389 pk_parse_keyfile( new->key, key_file, "" ) != 0 )
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200390 return( NULL );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100391
392 new->next = cur;
393 cur = new;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100394 }
395
396 return( cur );
397}
398
399void sni_free( sni_entry *head )
400{
401 sni_entry *cur = head, *next;
402
403 while( cur != NULL )
404 {
405 x509_crt_free( cur->cert );
406 polarssl_free( cur->cert );
407
408 pk_free( cur->key );
409 polarssl_free( cur->key );
410
411 next = cur->next;
412 polarssl_free( cur );
413 cur = next;
414 }
415}
416
417/*
418 * SNI callback.
419 */
420int sni_callback( void *p_info, ssl_context *ssl,
421 const unsigned char *name, size_t name_len )
422{
423 sni_entry *cur = (sni_entry *) p_info;
424
425 while( cur != NULL )
426 {
427 if( name_len == strlen( cur->name ) &&
428 memcmp( name, cur->name, name_len ) == 0 )
429 {
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200430 return( ssl_set_own_cert( ssl, cur->cert, cur->key ) );
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100431 }
432
433 cur = cur->next;
434 }
435
436 return( -1 );
437}
438
439#endif /* POLARSSL_SNI */
440
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200441#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
442
443#define HEX2NUM( c ) \
444 if( c >= '0' && c <= '9' ) \
445 c -= '0'; \
446 else if( c >= 'a' && c <= 'f' ) \
447 c -= 'a' - 10; \
448 else if( c >= 'A' && c <= 'F' ) \
449 c -= 'A' - 10; \
450 else \
451 return( -1 );
452
453/*
454 * Convert a hex string to bytes.
455 * Return 0 on success, -1 on error.
456 */
457int unhexify( unsigned char *output, const char *input, size_t *olen )
458{
459 unsigned char c;
460 size_t j;
461
462 *olen = strlen( input );
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200463 if( *olen % 2 != 0 || *olen / 2 > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200464 return( -1 );
465 *olen /= 2;
466
467 for( j = 0; j < *olen * 2; j += 2 )
468 {
469 c = input[j];
470 HEX2NUM( c );
471 output[ j / 2 ] = c << 4;
472
473 c = input[j + 1];
474 HEX2NUM( c );
475 output[ j / 2 ] |= c;
476 }
477
478 return( 0 );
479}
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200480
481typedef struct _psk_entry psk_entry;
482
483struct _psk_entry
484{
485 const char *name;
486 size_t key_len;
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200487 unsigned char key[POLARSSL_PSK_MAX_LEN];
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200488 psk_entry *next;
489};
490
491/*
492 * Parse a string of pairs name1,key1[,name2,key2[,...]]
493 * into a usable psk_entry list.
494 *
495 * Modifies the input string! This is not production quality!
496 * (leaks memory if parsing fails, no error reporting, ...)
497 */
498psk_entry *psk_parse( char *psk_string )
499{
500 psk_entry *cur = NULL, *new = NULL;
501 char *p = psk_string;
502 char *end = p;
503 char *key_hex;
504
505 while( *end != '\0' )
506 ++end;
507 *end = ',';
508
509 while( p <= end )
510 {
511 if( ( new = polarssl_malloc( sizeof( psk_entry ) ) ) == NULL )
512 return( NULL );
513
514 memset( new, 0, sizeof( psk_entry ) );
515
Manuel Pégourié-Gonnardfdee74b2014-06-10 15:15:06 +0200516 GET_ITEM( new->name );
517 GET_ITEM( key_hex );
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200518
519 if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
520 return( NULL );
521
522 new->next = cur;
523 cur = new;
524 }
525
526 return( cur );
527}
528
529/*
530 * Free a list of psk_entry's
531 */
532void psk_free( psk_entry *head )
533{
534 psk_entry *next;
535
536 while( head != NULL )
537 {
538 next = head->next;
539 polarssl_free( head );
540 head = next;
541 }
542}
543
544/*
545 * PSK callback
546 */
547int psk_callback( void *p_info, ssl_context *ssl,
548 const unsigned char *name, size_t name_len )
549{
550 psk_entry *cur = (psk_entry *) p_info;
551
552 while( cur != NULL )
553 {
554 if( name_len == strlen( cur->name ) &&
555 memcmp( name, cur->name, name_len ) == 0 )
556 {
557 return( ssl_set_psk( ssl, cur->key, cur->key_len,
558 name, name_len ) );
559 }
560
561 cur = cur->next;
562 }
563
564 return( -1 );
565}
Manuel Pégourié-Gonnard9e271632014-06-09 19:06:00 +0200566#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
567
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200568/* Interruption handler to ensure clean exit (for valgrind testing) */
569static int listen_fd;
570static int received_sigterm = 0;
571void term_handler( int sig )
572{
573 ((void) sig);
574 received_sigterm = 1;
575 net_close( listen_fd ); /* causes net_accept() to abort */
576}
577
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000578int main( int argc, char *argv[] )
579{
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200580 int ret = 0, len, written, frags;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000581 int client_fd = -1;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200582 int version_suites[4][2];
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +0200583 unsigned char buf[IO_BUF_LEN];
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200584#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +0200585 unsigned char psk[POLARSSL_PSK_MAX_LEN];
Paul Bakkerfbb17802013-04-17 19:10:21 +0200586 size_t psk_len = 0;
Paul Bakker9b7fb6f2014-06-12 23:01:43 +0200587 psk_entry *psk_info = NULL;
Paul Bakkered27a042013-04-18 22:46:23 +0200588#endif
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200589 const char *pers = "ssl_server2";
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000590
591 entropy_context entropy;
592 ctr_drbg_context ctr_drbg;
593 ssl_context ssl;
Paul Bakker36713e82013-09-17 13:25:29 +0200594#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200595 x509_crt cacert;
596 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200597 pk_context pkey;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200598 x509_crt srvcert2;
599 pk_context pkey2;
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +0200600 int key_cert_init = 0, key_cert_init2 = 0;
Paul Bakkered27a042013-04-18 22:46:23 +0200601#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200602#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
603 dhm_context dhm;
604#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000605#if defined(POLARSSL_SSL_CACHE_C)
606 ssl_cache_context cache;
607#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100608#if defined(POLARSSL_SNI)
609 sni_entry *sni_info = NULL;
610#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200611#if defined(POLARSSL_SSL_ALPN)
612 const char *alpn_list[10];
613#endif
Paul Bakker82024bf2013-07-04 11:52:32 +0200614#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
615 unsigned char alloc_buf[100000];
616#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000617
618 int i;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000619 char *p, *q;
620 const int *list;
621
Paul Bakker82024bf2013-07-04 11:52:32 +0200622#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
623 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
624#endif
625
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000626 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200627 * Make sure memory references are valid in case we exit early.
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000628 */
629 listen_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200630 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker36713e82013-09-17 13:25:29 +0200631#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200632 x509_crt_init( &cacert );
633 x509_crt_init( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200634 pk_init( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200635 x509_crt_init( &srvcert2 );
636 pk_init( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +0200637#endif
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200638#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
Paul Bakkera317a982014-06-18 16:44:11 +0200639 dhm_init( &dhm );
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200640#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000641#if defined(POLARSSL_SSL_CACHE_C)
642 ssl_cache_init( &cache );
643#endif
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200644#if defined(POLARSSL_SSL_ALPN)
Paul Bakker525f8752014-05-01 10:58:57 +0200645 memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200646#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000647
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +0200648 /* Abort cleanly on SIGTERM */
649 signal( SIGTERM, term_handler );
650
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000651 if( argc == 0 )
652 {
653 usage:
654 if( ret == 0 )
655 ret = 1;
656
657 printf( USAGE );
658
659 list = ssl_list_ciphersuites();
660 while( *list )
661 {
Paul Bakkerbcbe2d82013-04-19 09:10:20 +0200662 printf(" %-42s", ssl_get_ciphersuite_name( *list ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200663 list++;
664 if( !*list )
665 break;
666 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000667 list++;
668 }
669 printf("\n");
670 goto exit;
671 }
672
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100673 opt.server_addr = DFL_SERVER_ADDR;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000674 opt.server_port = DFL_SERVER_PORT;
675 opt.debug_level = DFL_DEBUG_LEVEL;
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100676 opt.nbio = DFL_NBIO;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000677 opt.ca_file = DFL_CA_FILE;
678 opt.ca_path = DFL_CA_PATH;
679 opt.crt_file = DFL_CRT_FILE;
680 opt.key_file = DFL_KEY_FILE;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200681 opt.crt_file2 = DFL_CRT_FILE2;
682 opt.key_file2 = DFL_KEY_FILE2;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200683 opt.psk = DFL_PSK;
684 opt.psk_identity = DFL_PSK_IDENTITY;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200685 opt.psk_list = DFL_PSK_LIST;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000686 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200687 opt.version_suites = DFL_VERSION_SUITES;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000688 opt.renegotiation = DFL_RENEGOTIATION;
689 opt.allow_legacy = DFL_ALLOW_LEGACY;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100690 opt.renegotiate = DFL_RENEGOTIATE;
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200691 opt.renego_delay = DFL_RENEGO_DELAY;
Paul Bakker1d29fb52012-09-28 13:28:45 +0000692 opt.min_version = DFL_MIN_VERSION;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200693 opt.max_version = DFL_MAX_VERSION;
Paul Bakker91ebfb52012-11-23 14:04:08 +0100694 opt.auth_mode = DFL_AUTH_MODE;
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200695 opt.mfl_code = DFL_MFL_CODE;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200696 opt.tickets = DFL_TICKETS;
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100697 opt.ticket_timeout = DFL_TICKET_TIMEOUT;
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100698 opt.cache_max = DFL_CACHE_MAX;
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100699 opt.cache_timeout = DFL_CACHE_TIMEOUT;
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100700 opt.sni = DFL_SNI;
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200701 opt.alpn_string = DFL_ALPN_STRING;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200702 opt.dhm_file = DFL_DHM_FILE;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000703
704 for( i = 1; i < argc; i++ )
705 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000706 p = argv[i];
707 if( ( q = strchr( p, '=' ) ) == NULL )
708 goto usage;
709 *q++ = '\0';
710
711 if( strcmp( p, "server_port" ) == 0 )
712 {
713 opt.server_port = atoi( q );
714 if( opt.server_port < 1 || opt.server_port > 65535 )
715 goto usage;
716 }
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +0100717 else if( strcmp( p, "server_addr" ) == 0 )
718 opt.server_addr = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000719 else if( strcmp( p, "debug_level" ) == 0 )
720 {
721 opt.debug_level = atoi( q );
722 if( opt.debug_level < 0 || opt.debug_level > 65535 )
723 goto usage;
724 }
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +0100725 else if( strcmp( p, "nbio" ) == 0 )
726 {
727 opt.nbio = atoi( q );
728 if( opt.nbio < 0 || opt.nbio > 2 )
729 goto usage;
730 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000731 else if( strcmp( p, "ca_file" ) == 0 )
732 opt.ca_file = q;
733 else if( strcmp( p, "ca_path" ) == 0 )
734 opt.ca_path = q;
735 else if( strcmp( p, "crt_file" ) == 0 )
736 opt.crt_file = q;
737 else if( strcmp( p, "key_file" ) == 0 )
738 opt.key_file = q;
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +0200739 else if( strcmp( p, "crt_file2" ) == 0 )
740 opt.crt_file2 = q;
741 else if( strcmp( p, "key_file2" ) == 0 )
742 opt.key_file2 = q;
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +0200743 else if( strcmp( p, "dhm_file" ) == 0 )
744 opt.dhm_file = q;
Paul Bakkerfbb17802013-04-17 19:10:21 +0200745 else if( strcmp( p, "psk" ) == 0 )
746 opt.psk = q;
747 else if( strcmp( p, "psk_identity" ) == 0 )
748 opt.psk_identity = q;
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200749 else if( strcmp( p, "psk_list" ) == 0 )
750 opt.psk_list = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000751 else if( strcmp( p, "force_ciphersuite" ) == 0 )
752 {
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000753 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
754
Manuel Pégourié-Gonnard8de259b2014-06-11 14:19:06 +0200755 if( opt.force_ciphersuite[0] == 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000756 {
757 ret = 2;
758 goto usage;
759 }
760 opt.force_ciphersuite[1] = 0;
761 }
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200762 else if( strcmp( p, "version_suites" ) == 0 )
763 opt.version_suites = q;
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000764 else if( strcmp( p, "renegotiation" ) == 0 )
765 {
766 opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED :
767 SSL_RENEGOTIATION_DISABLED;
768 }
769 else if( strcmp( p, "allow_legacy" ) == 0 )
770 {
771 opt.allow_legacy = atoi( q );
772 if( opt.allow_legacy < 0 || opt.allow_legacy > 1 )
773 goto usage;
774 }
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +0100775 else if( strcmp( p, "renegotiate" ) == 0 )
776 {
777 opt.renegotiate = atoi( q );
778 if( opt.renegotiate < 0 || opt.renegotiate > 1 )
779 goto usage;
780 }
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +0200781 else if( strcmp( p, "renego_delay" ) == 0 )
782 {
783 opt.renego_delay = atoi( q );
784 }
Paul Bakker1d29fb52012-09-28 13:28:45 +0000785 else if( strcmp( p, "min_version" ) == 0 )
786 {
787 if( strcmp( q, "ssl3" ) == 0 )
788 opt.min_version = SSL_MINOR_VERSION_0;
789 else if( strcmp( q, "tls1" ) == 0 )
790 opt.min_version = SSL_MINOR_VERSION_1;
791 else if( strcmp( q, "tls1_1" ) == 0 )
792 opt.min_version = SSL_MINOR_VERSION_2;
793 else if( strcmp( q, "tls1_2" ) == 0 )
794 opt.min_version = SSL_MINOR_VERSION_3;
795 else
796 goto usage;
797 }
Paul Bakkerc1516be2013-06-29 16:01:32 +0200798 else if( strcmp( p, "max_version" ) == 0 )
799 {
800 if( strcmp( q, "ssl3" ) == 0 )
801 opt.max_version = SSL_MINOR_VERSION_0;
802 else if( strcmp( q, "tls1" ) == 0 )
803 opt.max_version = SSL_MINOR_VERSION_1;
804 else if( strcmp( q, "tls1_1" ) == 0 )
805 opt.max_version = SSL_MINOR_VERSION_2;
806 else if( strcmp( q, "tls1_2" ) == 0 )
807 opt.max_version = SSL_MINOR_VERSION_3;
808 else
809 goto usage;
810 }
811 else if( strcmp( p, "force_version" ) == 0 )
812 {
813 if( strcmp( q, "ssl3" ) == 0 )
814 {
815 opt.min_version = SSL_MINOR_VERSION_0;
816 opt.max_version = SSL_MINOR_VERSION_0;
817 }
818 else if( strcmp( q, "tls1" ) == 0 )
819 {
820 opt.min_version = SSL_MINOR_VERSION_1;
821 opt.max_version = SSL_MINOR_VERSION_1;
822 }
823 else if( strcmp( q, "tls1_1" ) == 0 )
824 {
825 opt.min_version = SSL_MINOR_VERSION_2;
826 opt.max_version = SSL_MINOR_VERSION_2;
827 }
828 else if( strcmp( q, "tls1_2" ) == 0 )
829 {
830 opt.min_version = SSL_MINOR_VERSION_3;
831 opt.max_version = SSL_MINOR_VERSION_3;
832 }
833 else
834 goto usage;
835 }
Paul Bakker91ebfb52012-11-23 14:04:08 +0100836 else if( strcmp( p, "auth_mode" ) == 0 )
837 {
838 if( strcmp( q, "none" ) == 0 )
839 opt.auth_mode = SSL_VERIFY_NONE;
840 else if( strcmp( q, "optional" ) == 0 )
841 opt.auth_mode = SSL_VERIFY_OPTIONAL;
842 else if( strcmp( q, "required" ) == 0 )
843 opt.auth_mode = SSL_VERIFY_REQUIRED;
844 else
845 goto usage;
846 }
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +0200847 else if( strcmp( p, "max_frag_len" ) == 0 )
848 {
849 if( strcmp( q, "512" ) == 0 )
850 opt.mfl_code = SSL_MAX_FRAG_LEN_512;
851 else if( strcmp( q, "1024" ) == 0 )
852 opt.mfl_code = SSL_MAX_FRAG_LEN_1024;
853 else if( strcmp( q, "2048" ) == 0 )
854 opt.mfl_code = SSL_MAX_FRAG_LEN_2048;
855 else if( strcmp( q, "4096" ) == 0 )
856 opt.mfl_code = SSL_MAX_FRAG_LEN_4096;
857 else
858 goto usage;
859 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200860 else if( strcmp( p, "alpn" ) == 0 )
861 {
862 opt.alpn_string = q;
863 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200864 else if( strcmp( p, "tickets" ) == 0 )
865 {
866 opt.tickets = atoi( q );
867 if( opt.tickets < 0 || opt.tickets > 1 )
868 goto usage;
869 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +0100870 else if( strcmp( p, "ticket_timeout" ) == 0 )
871 {
872 opt.ticket_timeout = atoi( q );
873 if( opt.ticket_timeout < 0 )
874 goto usage;
875 }
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +0100876 else if( strcmp( p, "cache_max" ) == 0 )
877 {
878 opt.cache_max = atoi( q );
879 if( opt.cache_max < 0 )
880 goto usage;
881 }
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +0100882 else if( strcmp( p, "cache_timeout" ) == 0 )
883 {
884 opt.cache_timeout = atoi( q );
885 if( opt.cache_timeout < 0 )
886 goto usage;
887 }
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +0100888 else if( strcmp( p, "sni" ) == 0 )
889 {
890 opt.sni = q;
891 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000892 else
893 goto usage;
894 }
895
Paul Bakkerc73079a2014-04-25 16:34:30 +0200896#if defined(POLARSSL_DEBUG_C)
897 debug_set_threshold( opt.debug_level );
898#endif
899
Paul Bakkerc1516be2013-06-29 16:01:32 +0200900 if( opt.force_ciphersuite[0] > 0 )
901 {
902 const ssl_ciphersuite_t *ciphersuite_info;
903 ciphersuite_info = ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
904
Paul Bakker5b55b792013-07-19 13:43:43 +0200905 if( opt.max_version != -1 &&
906 ciphersuite_info->min_minor_ver > opt.max_version )
907 {
908 printf("forced ciphersuite not allowed with this protocol version\n");
909 ret = 2;
910 goto usage;
911 }
912 if( opt.min_version != -1 &&
Paul Bakkerc1516be2013-06-29 16:01:32 +0200913 ciphersuite_info->max_minor_ver < opt.min_version )
914 {
915 printf("forced ciphersuite not allowed with this protocol version\n");
916 ret = 2;
917 goto usage;
918 }
Paul Bakker5b55b792013-07-19 13:43:43 +0200919 if( opt.max_version > ciphersuite_info->max_minor_ver )
920 opt.max_version = ciphersuite_info->max_minor_ver;
921 if( opt.min_version < ciphersuite_info->min_minor_ver )
922 opt.min_version = ciphersuite_info->min_minor_ver;
Paul Bakkerc1516be2013-06-29 16:01:32 +0200923 }
924
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +0200925 if( opt.version_suites != NULL )
926 {
927 const char *name[4] = { 0 };
928
929 /* Parse 4-element coma-separated list */
930 for( i = 0, p = (char *) opt.version_suites;
931 i < 4 && *p != '\0';
932 i++ )
933 {
934 name[i] = p;
935
936 /* Terminate the current string and move on to next one */
937 while( *p != ',' && *p != '\0' )
938 p++;
939 if( *p == ',' )
940 *p++ = '\0';
941 }
942
943 if( i != 4 )
944 {
945 printf( "too few values for version_suites\n" );
946 ret = 1;
947 goto exit;
948 }
949
950 memset( version_suites, 0, sizeof( version_suites ) );
951
952 /* Get the suites identifiers from their name */
953 for( i = 0; i < 4; i++ )
954 {
955 version_suites[i][0] = ssl_get_ciphersuite_id( name[i] );
956
957 if( version_suites[i][0] == 0 )
958 {
959 printf( "unknown ciphersuite: '%s'\n", name[i] );
960 ret = 2;
961 goto usage;
962 }
963 }
964 }
965
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200966#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerb60b95f2012-09-25 09:05:17 +0000967 /*
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200968 * Unhexify the pre-shared key and parse the list if any given
Paul Bakkerfbb17802013-04-17 19:10:21 +0200969 */
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200970 if( unhexify( psk, opt.psk, &psk_len ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +0200971 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200972 printf( "pre-shared key not valid hex\n" );
973 goto exit;
974 }
975
976 if( opt.psk_list != NULL )
977 {
978 if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
Paul Bakkerfbb17802013-04-17 19:10:21 +0200979 {
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +0200980 printf( "psk_list invalid" );
Paul Bakkerfbb17802013-04-17 19:10:21 +0200981 goto exit;
982 }
Paul Bakkerfbb17802013-04-17 19:10:21 +0200983 }
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200984#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +0200985
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +0200986#if defined(POLARSSL_SSL_ALPN)
987 if( opt.alpn_string != NULL )
988 {
989 p = (char *) opt.alpn_string;
990 i = 0;
991
992 /* Leave room for a final NULL in alpn_list */
993 while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
994 {
995 alpn_list[i++] = p;
996
997 /* Terminate the current string and move on to next one */
998 while( *p != ',' && *p != '\0' )
999 p++;
1000 if( *p == ',' )
1001 *p++ = '\0';
1002 }
1003 }
1004#endif /* POLARSSL_SSL_ALPN */
1005
Paul Bakkerfbb17802013-04-17 19:10:21 +02001006 /*
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001007 * 0. Initialize the RNG and the session data
1008 */
1009 printf( "\n . Seeding the random number generator..." );
1010 fflush( stdout );
1011
1012 entropy_init( &entropy );
1013 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001014 (const unsigned char *) pers,
1015 strlen( pers ) ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001016 {
1017 printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
1018 goto exit;
1019 }
1020
1021 printf( " ok\n" );
1022
Paul Bakker36713e82013-09-17 13:25:29 +02001023#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001024 /*
1025 * 1.1. Load the trusted CA
1026 */
1027 printf( " . Loading the CA root certificate ..." );
1028 fflush( stdout );
1029
1030#if defined(POLARSSL_FS_IO)
1031 if( strlen( opt.ca_path ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001032 if( strcmp( opt.ca_path, "none" ) == 0 )
1033 ret = 0;
1034 else
1035 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001036 else if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001037 if( strcmp( opt.ca_file, "none" ) == 0 )
1038 ret = 0;
1039 else
1040 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001041 else
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001042#endif
1043#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +02001044 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
1045 strlen( test_ca_list ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001046#else
1047 {
1048 ret = 1;
1049 printf("POLARSSL_CERTS_C not defined.");
1050 }
1051#endif
1052 if( ret < 0 )
1053 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02001054 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001055 goto exit;
1056 }
1057
1058 printf( " ok (%d skipped)\n", ret );
1059
1060 /*
1061 * 1.2. Load own certificate and private key
1062 */
1063 printf( " . Loading the server cert. and key..." );
1064 fflush( stdout );
1065
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001066#if defined(POLARSSL_FS_IO)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001067 if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001068 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001069 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001070 if( ( ret = x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
1071 {
1072 printf( " failed\n ! x509_crt_parse_file returned -0x%x\n\n",
1073 -ret );
1074 goto exit;
1075 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001076 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001077 if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001078 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001079 key_cert_init++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001080 if( ( ret = pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
1081 {
1082 printf( " failed\n ! pk_parse_keyfile returned -0x%x\n\n", -ret );
1083 goto exit;
1084 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001085 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001086 if( key_cert_init == 1 )
1087 {
1088 printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
1089 goto exit;
1090 }
1091
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001092 if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001093 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001094 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001095 if( ( ret = x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
1096 {
1097 printf( " failed\n ! x509_crt_parse_file(2) returned -0x%x\n\n",
1098 -ret );
1099 goto exit;
1100 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001101 }
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001102 if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001103 {
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001104 key_cert_init2++;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001105 if( ( ret = pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
1106 {
1107 printf( " failed\n ! pk_parse_keyfile(2) returned -0x%x\n\n",
1108 -ret );
1109 goto exit;
1110 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001111 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001112 if( key_cert_init2 == 1 )
1113 {
1114 printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
1115 goto exit;
1116 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001117#endif
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001118 if( key_cert_init == 0 &&
1119 strcmp( opt.crt_file, "none" ) != 0 &&
1120 strcmp( opt.key_file, "none" ) != 0 &&
1121 key_cert_init2 == 0 &&
1122 strcmp( opt.crt_file2, "none" ) != 0 &&
1123 strcmp( opt.key_file2, "none" ) != 0 )
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001124 {
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001125#if !defined(POLARSSL_CERTS_C)
1126 printf( "Not certificated or key provided, and \n"
1127 "POLARSSL_CERTS_C not defined!\n" );
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001128 goto exit;
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001129#else
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001130#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001131 if( ( ret = x509_crt_parse( &srvcert,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001132 (const unsigned char *) test_srv_crt_rsa,
1133 strlen( test_srv_crt_rsa ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001134 {
1135 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
1136 goto exit;
1137 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001138 if( ( ret = pk_parse_key( &pkey,
1139 (const unsigned char *) test_srv_key_rsa,
1140 strlen( test_srv_key_rsa ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001141 {
1142 printf( " failed\n ! pk_parse_key returned -0x%x\n\n", -ret );
1143 goto exit;
1144 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001145 key_cert_init = 2;
1146#endif /* POLARSSL_RSA_C */
1147#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001148 if( ( ret = x509_crt_parse( &srvcert2,
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001149 (const unsigned char *) test_srv_crt_ec,
1150 strlen( test_srv_crt_ec ) ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001151 {
1152 printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret );
1153 goto exit;
1154 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001155 if( ( ret = pk_parse_key( &pkey2,
1156 (const unsigned char *) test_srv_key_ec,
1157 strlen( test_srv_key_ec ), NULL, 0 ) ) != 0 )
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001158 {
1159 printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret );
1160 goto exit;
1161 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001162 key_cert_init2 = 2;
1163#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardac8474f2013-09-25 11:35:15 +02001164#endif /* POLARSSL_CERTS_C */
1165 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001166
1167 printf( " ok\n" );
Paul Bakker36713e82013-09-17 13:25:29 +02001168#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001169
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001170#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1171 if( opt.dhm_file != NULL )
1172 {
1173 printf( " . Loading DHM parameters..." );
1174 fflush( stdout );
1175
1176 if( ( ret = dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
1177 {
1178 printf( " failed\n ! dhm_parse_dhmfile returned -0x%04X\n\n",
1179 -ret );
1180 goto exit;
1181 }
1182
1183 printf( " ok\n" );
1184 }
1185#endif
1186
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001187#if defined(POLARSSL_SNI)
1188 if( opt.sni != NULL )
1189 {
1190 printf( " . Setting up SNI information..." );
1191 fflush( stdout );
1192
1193 if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
1194 {
1195 printf( " failed\n" );
1196 goto exit;
1197 }
1198
1199 printf( " ok\n" );
1200 }
1201#endif /* POLARSSL_SNI */
1202
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001203 /*
1204 * 2. Setup the listening TCP socket
1205 */
1206 printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port );
1207 fflush( stdout );
1208
Manuel Pégourié-Gonnard18d31f82013-12-13 16:21:41 +01001209 if( ( ret = net_bind( &listen_fd, opt.server_addr,
1210 opt.server_port ) ) != 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001211 {
1212 printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
1213 goto exit;
1214 }
1215
1216 printf( " ok\n" );
1217
1218 /*
1219 * 3. Setup stuff
1220 */
1221 printf( " . Setting up the SSL/TLS structure..." );
1222 fflush( stdout );
1223
1224 if( ( ret = ssl_init( &ssl ) ) != 0 )
1225 {
1226 printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
1227 goto exit;
1228 }
1229
1230 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
Paul Bakker91ebfb52012-11-23 14:04:08 +01001231 ssl_set_authmode( &ssl, opt.auth_mode );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001232
Paul Bakker05decb22013-08-15 13:33:48 +02001233#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001234 if( ( ret = ssl_set_max_frag_len( &ssl, opt.mfl_code ) ) != 0 )
1235 {
1236 printf( " failed\n ! ssl_set_max_frag_len returned %d\n\n", ret );
1237 goto exit;
1238 };
Paul Bakker05decb22013-08-15 13:33:48 +02001239#endif
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001240
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001241#if defined(POLARSSL_SSL_ALPN)
1242 if( opt.alpn_string != NULL )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001243 if( ( ret = ssl_set_alpn_protocols( &ssl, alpn_list ) ) != 0 )
1244 {
1245 printf( " failed\n ! ssl_set_alpn_protocols returned %d\n\n", ret );
1246 goto exit;
1247 }
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001248#endif
1249
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001250 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
1251 ssl_set_dbg( &ssl, my_debug, stdout );
1252
Paul Bakker0a597072012-09-25 21:55:46 +00001253#if defined(POLARSSL_SSL_CACHE_C)
Manuel Pégourié-Gonnard4c883452014-02-20 21:32:41 +01001254 if( opt.cache_max != -1 )
1255 ssl_cache_set_max_entries( &cache, opt.cache_max );
1256
Manuel Pégourié-Gonnardc55a5b72014-02-20 22:50:56 +01001257 if( opt.cache_timeout != -1 )
1258 ssl_cache_set_timeout( &cache, opt.cache_timeout );
1259
Paul Bakker0a597072012-09-25 21:55:46 +00001260 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
1261 ssl_cache_set, &cache );
1262#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001263
Paul Bakkera503a632013-08-14 13:48:06 +02001264#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001265 if( ( ret = ssl_set_session_tickets( &ssl, opt.tickets ) ) != 0 )
1266 {
1267 printf( " failed\n ! ssl_set_session_tickets returned %d\n\n", ret );
1268 goto exit;
1269 }
Manuel Pégourié-Gonnarddbe1ee12014-02-21 09:18:13 +01001270
1271 if( opt.ticket_timeout != -1 )
1272 ssl_set_session_ticket_lifetime( &ssl, opt.ticket_timeout );
Paul Bakkera503a632013-08-14 13:48:06 +02001273#endif
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001274
Paul Bakker41c83d32013-03-20 14:39:14 +01001275 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001276 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
1277
Manuel Pégourié-Gonnard6dc07812014-06-11 13:50:34 +02001278 if( opt.version_suites != NULL )
1279 {
1280 ssl_set_ciphersuites_for_version( &ssl, version_suites[0],
1281 SSL_MAJOR_VERSION_3,
1282 SSL_MINOR_VERSION_0 );
1283 ssl_set_ciphersuites_for_version( &ssl, version_suites[1],
1284 SSL_MAJOR_VERSION_3,
1285 SSL_MINOR_VERSION_1 );
1286 ssl_set_ciphersuites_for_version( &ssl, version_suites[2],
1287 SSL_MAJOR_VERSION_3,
1288 SSL_MINOR_VERSION_2 );
1289 ssl_set_ciphersuites_for_version( &ssl, version_suites[3],
1290 SSL_MAJOR_VERSION_3,
1291 SSL_MINOR_VERSION_3 );
1292 }
1293
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001294 ssl_set_renegotiation( &ssl, opt.renegotiation );
1295 ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
Manuel Pégourié-Gonnardfae355e2014-07-04 14:32:27 +02001296 if( opt.renego_delay != DFL_RENEGO_DELAY )
1297 ssl_set_renegotiation_enforced( &ssl, opt.renego_delay );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001298
Paul Bakker36713e82013-09-17 13:25:29 +02001299#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard3e1b1782014-02-27 13:35:00 +01001300 if( strcmp( opt.ca_path, "none" ) != 0 &&
1301 strcmp( opt.ca_file, "none" ) != 0 )
1302 {
1303 ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
1304 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001305 if( key_cert_init )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001306 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
1307 {
1308 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1309 goto exit;
1310 }
Manuel Pégourié-Gonnarda0fdf8b2013-09-25 14:05:49 +02001311 if( key_cert_init2 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +02001312 if( ( ret = ssl_set_own_cert( &ssl, &srvcert2, &pkey2 ) ) != 0 )
1313 {
1314 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
1315 goto exit;
1316 }
Manuel Pégourié-Gonnardb095a7b2013-09-24 21:14:51 +02001317#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001318
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001319#if defined(POLARSSL_SNI)
1320 if( opt.sni != NULL )
1321 ssl_set_sni( &ssl, sni_callback, sni_info );
1322#endif
1323
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001324#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnarddc019b92014-06-10 15:24:51 +02001325 if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
1326 {
1327 ret = ssl_set_psk( &ssl, psk, psk_len,
1328 (const unsigned char *) opt.psk_identity,
1329 strlen( opt.psk_identity ) );
1330 if( ret != 0 )
1331 {
1332 printf( " failed\n ssl_set_psk returned -0x%04X\n\n", - ret );
1333 goto exit;
1334 }
1335 }
1336
Manuel Pégourié-Gonnard80c85532014-06-10 14:01:52 +02001337 if( opt.psk_list != NULL )
1338 ssl_set_psk_cb( &ssl, psk_callback, psk_info );
Paul Bakkered27a042013-04-18 22:46:23 +02001339#endif
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001340
1341#if defined(POLARSSL_DHM_C)
Paul Bakker5d19f862012-09-28 07:33:00 +00001342 /*
1343 * Use different group than default DHM group
1344 */
Manuel Pégourié-Gonnard736699c2014-06-09 11:29:50 +02001345#if defined(POLARSSL_FS_IO)
1346 if( opt.dhm_file != NULL )
1347 ret = ssl_set_dh_param_ctx( &ssl, &dhm );
1348 else
1349#endif
1350 ret = ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P,
1351 POLARSSL_DHM_RFC5114_MODP_2048_G );
1352
1353 if( ret != 0 )
1354 {
1355 printf( " failed\n ssl_set_dh_param returned -0x%04X\n\n", - ret );
1356 goto exit;
1357 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001358#endif
1359
Paul Bakker1d29fb52012-09-28 13:28:45 +00001360 if( opt.min_version != -1 )
1361 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
1362
Paul Bakkerc1516be2013-06-29 16:01:32 +02001363 if( opt.max_version != -1 )
1364 ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
1365
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001366 printf( " ok\n" );
1367
1368reset:
1369#ifdef POLARSSL_ERROR_C
1370 if( ret != 0 )
1371 {
1372 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001373 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001374 printf("Last error was: %d - %s\n\n", ret, error_buf );
1375 }
1376#endif
1377
1378 if( client_fd != -1 )
1379 net_close( client_fd );
1380
1381 ssl_session_reset( &ssl );
1382
1383 /*
1384 * 3. Wait until a client connects
1385 */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001386 client_fd = -1;
1387
1388 printf( " . Waiting for a remote connection ..." );
1389 fflush( stdout );
1390
1391 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
1392 {
Manuel Pégourié-Gonnarddb493302014-08-14 15:36:12 +02001393 if( received_sigterm )
1394 {
1395 printf( " interrupted by SIGTERM\n" );
1396 ret = 0;
1397 goto exit;
1398 }
1399
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001400 printf( " failed\n ! net_accept returned -0x%x\n\n", -ret );
1401 goto exit;
1402 }
1403
Manuel Pégourié-Gonnard55753162014-02-26 13:47:08 +01001404 if( opt.nbio > 0 )
1405 ret = net_set_nonblock( client_fd );
1406 else
1407 ret = net_set_block( client_fd );
1408 if( ret != 0 )
1409 {
1410 printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
1411 goto exit;
1412 }
1413
1414 if( opt.nbio == 2 )
1415 ssl_set_bio( &ssl, my_recv, &client_fd, my_send, &client_fd );
1416 else
1417 ssl_set_bio( &ssl, net_recv, &client_fd, net_send, &client_fd );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001418
1419 printf( " ok\n" );
1420
1421 /*
1422 * 4. Handshake
1423 */
1424 printf( " . Performing the SSL/TLS handshake..." );
1425 fflush( stdout );
1426
1427 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
1428 {
1429 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
1430 {
1431 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001432 goto reset;
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001433 }
1434 }
1435
Manuel Pégourié-Gonnardc580a002014-02-12 10:15:30 +01001436 printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
1437 ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001438
Manuel Pégourié-Gonnard1bd22812014-04-05 14:34:07 +02001439#if defined(POLARSSL_SSL_ALPN)
1440 if( opt.alpn_string != NULL )
1441 {
1442 const char *alp = ssl_get_alpn_protocol( &ssl );
1443 printf( " [ Application Layer Protocol is %s ]\n",
1444 alp ? alp : "(none)" );
1445 }
1446#endif
1447
Paul Bakker36713e82013-09-17 13:25:29 +02001448#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001449 /*
1450 * 5. Verify the server certificate
1451 */
1452 printf( " . Verifying peer X.509 certificate..." );
1453
1454 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
1455 {
1456 printf( " failed\n" );
1457
Paul Bakkerb0550d92012-10-30 07:51:03 +00001458 if( !ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001459 printf( " ! no client certificate sent\n" );
1460
1461 if( ( ret & BADCERT_EXPIRED ) != 0 )
1462 printf( " ! client certificate has expired\n" );
1463
1464 if( ( ret & BADCERT_REVOKED ) != 0 )
1465 printf( " ! client certificate has been revoked\n" );
1466
1467 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
1468 printf( " ! self-signed or not signed by a trusted CA\n" );
1469
1470 printf( "\n" );
1471 }
1472 else
1473 printf( " ok\n" );
1474
Paul Bakkerb0550d92012-10-30 07:51:03 +00001475 if( ssl_get_peer_cert( &ssl ) )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001476 {
1477 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +02001478 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
1479 ssl_get_peer_cert( &ssl ) );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001480 printf( "%s\n", buf );
1481 }
Paul Bakker36713e82013-09-17 13:25:29 +02001482#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001483
1484 /*
1485 * 6. Read the HTTP Request
1486 */
1487 printf( " < Read from client:" );
1488 fflush( stdout );
1489
1490 do
1491 {
1492 len = sizeof( buf ) - 1;
1493 memset( buf, 0, sizeof( buf ) );
1494 ret = ssl_read( &ssl, buf, len );
1495
1496 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
1497 continue;
1498
1499 if( ret <= 0 )
1500 {
1501 switch( ret )
1502 {
1503 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
1504 printf( " connection was closed gracefully\n" );
1505 break;
1506
1507 case POLARSSL_ERR_NET_CONN_RESET:
1508 printf( " connection was reset by peer\n" );
1509 break;
1510
1511 default:
1512 printf( " ssl_read returned -0x%x\n", -ret );
1513 break;
1514 }
1515
1516 break;
1517 }
1518
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001519 if( ssl_get_bytes_avail( &ssl ) == 0 )
1520 {
1521 len = ret;
1522 buf[len] = '\0';
1523 printf( " %d bytes read\n\n%s\n", len, (char *) buf );
1524 }
1525 else
1526 {
1527 int extra_len, ori_len;
1528 unsigned char *larger_buf;
1529
1530 ori_len = ret;
1531 extra_len = ssl_get_bytes_avail( &ssl );
1532
1533 larger_buf = polarssl_malloc( ori_len + extra_len + 1 );
1534 if( larger_buf == NULL )
1535 {
1536 printf( " ! memory allocation failed\n" );
1537 ret = 1;
1538 goto exit;
1539 }
1540
1541 memset( larger_buf, 0, ori_len + extra_len );
1542 memcpy( larger_buf, buf, ori_len );
1543
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +02001544 /* This read should never fail and get the whole cached data */
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001545 ret = ssl_read( &ssl, larger_buf + ori_len, extra_len );
Manuel Pégourié-Gonnard95c0a632014-06-11 18:32:36 +02001546 if( ret != extra_len ||
1547 ssl_get_bytes_avail( &ssl ) != 0 )
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001548 {
1549 printf( " ! ssl_read failed on cached data\n" );
1550 ret = 1;
1551 goto exit;
1552 }
1553
1554 larger_buf[ori_len + extra_len] = '\0';
1555 printf( " %u bytes read (%u + %u)\n\n%s\n",
Manuel Pégourié-Gonnard0669f272014-06-18 13:07:20 +02001556 ori_len + extra_len, ori_len, extra_len,
1557 (char *) larger_buf );
Manuel Pégourié-Gonnarde7a3b102014-06-11 18:21:20 +02001558
1559 polarssl_free( larger_buf );
1560 }
1561
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001562
Paul Bakker82024bf2013-07-04 11:52:32 +02001563 if( memcmp( buf, "SERVERQUIT", 10 ) == 0 )
Manuel Pégourié-Gonnarde8ea0c02013-09-07 17:09:14 +02001564 {
1565 ret = 0;
Paul Bakker82024bf2013-07-04 11:52:32 +02001566 goto exit;
Manuel Pégourié-Gonnarde8ea0c02013-09-07 17:09:14 +02001567 }
Paul Bakker82024bf2013-07-04 11:52:32 +02001568
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001569 if( ret > 0 )
1570 break;
1571 }
1572 while( 1 );
1573
1574 /*
1575 * 7. Write the 200 Response
1576 */
1577 printf( " > Write to client:" );
1578 fflush( stdout );
1579
1580 len = sprintf( (char *) buf, HTTP_RESPONSE,
1581 ssl_get_ciphersuite( &ssl ) );
1582
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001583 for( written = 0, frags = 0; written < len; written += ret, frags++ )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001584 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001585 while( ( ret = ssl_write( &ssl, buf + written, len - written ) ) <= 0 )
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001586 {
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001587 if( ret == POLARSSL_ERR_NET_CONN_RESET )
1588 {
1589 printf( " failed\n ! peer closed the connection\n\n" );
1590 goto reset;
1591 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001592
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001593 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
1594 {
1595 printf( " failed\n ! ssl_write returned %d\n\n", ret );
1596 goto exit;
1597 }
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001598 }
1599 }
1600
Manuel Pégourié-Gonnardbd7ce632013-07-17 15:34:17 +02001601 buf[written] = '\0';
Manuel Pégourié-Gonnard0c017a52013-07-18 14:07:36 +02001602 printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001603
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001604 if( opt.renegotiate )
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001605 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001606 /*
1607 * Request renegotiation (this must be done when the client is still
1608 * waiting for input from our side).
1609 */
1610 printf( " . Requestion renegotiation..." );
1611 fflush( stdout );
1612 while( ( ret = ssl_renegotiate( &ssl ) ) != 0 )
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001613 {
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001614 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1615 ret != POLARSSL_ERR_NET_WANT_WRITE )
1616 {
1617 printf( " failed\n ! ssl_renegotiate returned %d\n\n", ret );
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01001618 goto exit;
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001619 }
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001620 }
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001621
Manuel Pégourié-Gonnard780d6712014-02-20 17:19:59 +01001622 /*
1623 * Should be a while loop, not an if, but here we're not actually
1624 * expecting data from the client, and since we're running tests
1625 * locally, we can just hope the handshake will finish the during the
1626 * first call.
1627 */
1628 if( ( ret = ssl_read( &ssl, buf, 0 ) ) != 0 )
1629 {
1630 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1631 ret != POLARSSL_ERR_NET_WANT_WRITE )
1632 {
1633 printf( " failed\n ! ssl_read returned %d\n\n", ret );
1634
1635 /* Unexpected message probably means client didn't renegotiate
1636 * as requested */
1637 if( ret == POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE )
1638 goto reset;
1639 else
1640 goto exit;
1641 }
1642 }
1643
1644 printf( " ok\n" );
1645 }
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001646
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +01001647 printf( " . Closing the connection..." );
1648
1649 while( ( ret = ssl_close_notify( &ssl ) ) < 0 )
1650 {
1651 if( ret != POLARSSL_ERR_NET_WANT_READ &&
1652 ret != POLARSSL_ERR_NET_WANT_WRITE )
1653 {
1654 printf( " failed\n ! ssl_close_notify returned %d\n\n", ret );
1655 goto reset;
1656 }
1657 }
1658
1659 printf( " ok\n" );
1660
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001661 ret = 0;
1662 goto reset;
1663
1664exit:
1665
1666#ifdef POLARSSL_ERROR_C
1667 if( ret != 0 )
1668 {
1669 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +02001670 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001671 printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
1672 }
1673#endif
1674
Paul Bakker0c226102014-04-17 16:02:36 +02001675 if( client_fd != -1 )
1676 net_close( client_fd );
1677
Paul Bakkera317a982014-06-18 16:44:11 +02001678#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1679 dhm_free( &dhm );
1680#endif
Paul Bakker36713e82013-09-17 13:25:29 +02001681#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker36713e82013-09-17 13:25:29 +02001682 x509_crt_free( &cacert );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001683 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02001684 pk_free( &pkey );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001685 x509_crt_free( &srvcert2 );
1686 pk_free( &pkey2 );
Paul Bakkered27a042013-04-18 22:46:23 +02001687#endif
Manuel Pégourié-Gonnard5d917ff2014-02-21 16:52:06 +01001688#if defined(POLARSSL_SNI)
1689 sni_free( sni_info );
1690#endif
Manuel Pégourié-Gonnard4505ed32014-06-19 20:56:52 +02001691#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1692 psk_free( psk_info );
1693#endif
1694#if defined(POLARSSL_DHM_C) && defined(POLARSSL_FS_IO)
1695 dhm_free( &dhm );
1696#endif
Paul Bakkered27a042013-04-18 22:46:23 +02001697
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001698 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +02001699 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +02001700 entropy_free( &entropy );
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001701
Paul Bakker0a597072012-09-25 21:55:46 +00001702#if defined(POLARSSL_SSL_CACHE_C)
1703 ssl_cache_free( &cache );
1704#endif
1705
Paul Bakker1337aff2013-09-29 14:45:34 +02001706#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1707#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker82024bf2013-07-04 11:52:32 +02001708 memory_buffer_alloc_status();
1709#endif
Paul Bakker1337aff2013-09-29 14:45:34 +02001710 memory_buffer_alloc_free();
1711#endif
Paul Bakker82024bf2013-07-04 11:52:32 +02001712
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001713#if defined(_WIN32)
1714 printf( " + Press Enter to exit this program.\n" );
1715 fflush( stdout ); getchar();
1716#endif
1717
Paul Bakkerdbd79ca2013-07-24 16:28:35 +02001718 // Shell can not handle large exit numbers -> 1 for errors
1719 if( ret < 0 )
1720 ret = 1;
1721
Paul Bakkerb60b95f2012-09-25 09:05:17 +00001722 return( ret );
1723}
1724#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
1725 POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
1726 POLARSSL_CTR_DRBG_C */