blob: 5858278b466fb791312f58cfd7399734adf619d2 [file] [log] [blame]
Rich Salz846e33c2016-05-17 14:18:30 -04001/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003 *
Rich Salz846e33c2016-05-17 14:18:30 -04004 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00008 */
Rich Salz846e33c2016-05-17 14:18:30 -04009
Bodo Möllerea262262002-08-09 08:56:08 +000010/* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
Matt Caswell0f113f32015-01-22 03:40:55 +000012 * ECC cipher suite support in OpenSSL originally developed by
Bodo Möllerea262262002-08-09 08:56:08 +000013 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
14 */
Nils Larschddac1972006-03-10 23:06:27 +000015/* ====================================================================
16 * Copyright 2005 Nokia. All rights reserved.
17 *
18 * The portions of the attached software ("Contribution") is developed by
19 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
20 * license.
21 *
22 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
23 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
24 * support (see RFC 4279) to OpenSSL.
25 *
26 * No patent licenses or other rights except those expressly stated in
27 * the OpenSSL open source license shall be deemed granted or received
28 * expressly, by implication, estoppel, or otherwise.
29 *
30 * No assurances are provided by Nokia that the Contribution does not
31 * infringe the patent or other intellectual property rights of any third
32 * party or that the license provides you with all the necessary rights
33 * to make use of the Contribution.
34 *
35 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
36 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
37 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
38 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
39 * OTHERWISE.
40 */
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000041
Nils Larschddac1972006-03-10 23:06:27 +000042#include <ctype.h>
Ulf Möller8c197cc1999-07-28 23:25:59 +000043#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
FdaSilvaYY54463e42016-08-03 22:49:25 +020046#if defined(_WIN32)
47/* Included before async.h to avoid some warnings */
48# include <windows.h>
49#endif
Richard Levitte4d8743f2003-11-28 13:10:58 +000050
Richard Levittebe1bd922001-02-20 14:07:03 +000051#include <openssl/e_os2.h>
FdaSilvaYY54463e42016-08-03 22:49:25 +020052#include <openssl/async.h>
53#include <openssl/ssl.h>
Ulf Möller8c197cc1999-07-28 23:25:59 +000054
Matt Caswellf9e55032016-03-21 15:32:40 +000055#ifndef OPENSSL_NO_SOCK
56
Matt Caswell0f113f32015-01-22 03:40:55 +000057/*
58 * With IPv6, it looks like Digital has mixed up the proper order of
59 * recursive header file inclusion, resulting in the compiler complaining
60 * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
61 * needed to have fileno() declared correctly... So let's define u_int
62 */
Richard Levittebc36ee62001-02-20 08:13:47 +000063#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
Matt Caswell0f113f32015-01-22 03:40:55 +000064# define __U_INT
Ulf Möller7d7d2cb1999-05-13 11:37:32 +000065typedef unsigned int u_int;
66#endif
67
Bodo Möllerec577821999-04-23 22:13:45 +000068#include <openssl/lhash.h>
69#include <openssl/bn.h>
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000070#define USE_SOCKETS
71#include "apps.h"
Bodo Möllerec577821999-04-23 22:13:45 +000072#include <openssl/err.h>
73#include <openssl/pem.h>
74#include <openssl/x509.h>
75#include <openssl/ssl.h>
Geoff Thorpe13729652001-09-12 02:39:06 +000076#include <openssl/rand.h>
Dr. Stephen Henson67c8e7f2007-09-26 21:56:59 +000077#include <openssl/ocsp.h>
Nils Larsch3eeaab42005-07-16 12:37:36 +000078#ifndef OPENSSL_NO_DH
Matt Caswell0f113f32015-01-22 03:40:55 +000079# include <openssl/dh.h>
Nils Larsch3eeaab42005-07-16 12:37:36 +000080#endif
81#ifndef OPENSSL_NO_RSA
Matt Caswell0f113f32015-01-22 03:40:55 +000082# include <openssl/rsa.h>
Nils Larsch3eeaab42005-07-16 12:37:36 +000083#endif
Ben Laurieedc032b2011-03-12 17:01:19 +000084#ifndef OPENSSL_NO_SRP
Matt Caswell0f113f32015-01-22 03:40:55 +000085# include <openssl/srp.h>
Ben Laurieedc032b2011-03-12 17:01:19 +000086#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000087#include "s_apps.h"
Ben Laurie36d16f82005-04-26 16:02:40 +000088#include "timeouts.h"
Matt Caswell5fd14782016-04-28 11:34:54 +010089#ifdef CHARSET_EBCDIC
90#include <openssl/ebcdic.h>
91#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000092
Bodo Möller7c2d4fe2010-08-26 15:15:47 +000093static int not_resumable_sess_cb(SSL *s, int is_forward_secure);
Rich Salza773b522016-02-13 22:33:56 -050094static int sv_body(int s, int stype, unsigned char *context);
95static int www_body(int s, int stype, unsigned char *context);
96static int rev_body(int s, int stype, unsigned char *context);
Matt Caswell0f113f32015-01-22 03:40:55 +000097static void close_accept_socket(void);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000098static int init_ssl_connection(SSL *s);
Matt Caswell0f113f32015-01-22 03:40:55 +000099static void print_stats(BIO *bp, SSL_CTX *ctx);
Geoff Thorpe1aa0d942001-02-21 18:38:48 +0000100static int generate_session_id(const SSL *ssl, unsigned char *id,
Matt Caswell0f113f32015-01-22 03:40:55 +0000101 unsigned int *id_len);
Dr. Stephen Henson35b0ea42009-12-27 23:24:45 +0000102static void init_session_cache_ctx(SSL_CTX *sctx);
103static void free_sessions(void);
Richard Levittecf1b7d92001-02-19 16:06:34 +0000104#ifndef OPENSSL_NO_DH
Nils Larscheb3eab22005-04-07 22:48:33 +0000105static DH *load_dh_param(const char *dhfile);
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000106#endif
Matt Caswellade1e882017-02-27 20:55:04 +0000107static void print_connection_info(SSL *con);
Bodo Möllerea262262002-08-09 08:56:08 +0000108
FdaSilvaYYd6073e22016-08-07 12:04:26 +0200109static const int bufsize = 16 * 1024;
Matt Caswell0f113f32015-01-22 03:40:55 +0000110static int accept_socket = -1;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000111
Matt Caswell0f113f32015-01-22 03:40:55 +0000112#define TEST_CERT "server.pem"
Matt Caswelle481f9b2015-05-15 10:49:56 +0100113#define TEST_CERT2 "server2.pem"
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000114
Matt Caswell0f113f32015-01-22 03:40:55 +0000115static int s_nbio = 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000116static int s_nbio_test = 0;
Ben Lauriedf2ee0e2015-09-05 13:32:58 +0100117static int s_crlf = 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000118static SSL_CTX *ctx = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +0000119static SSL_CTX *ctx2 = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +0000120static int www = 0;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000121
Matt Caswell0f113f32015-01-22 03:40:55 +0000122static BIO *bio_s_out = NULL;
Dr. Stephen Henson93ab9e42012-06-15 12:46:09 +0000123static BIO *bio_s_msg = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +0000124static int s_debug = 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000125static int s_tlsextdebug = 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000126static int s_msg = 0;
127static int s_quiet = 0;
128static int s_ign_eof = 0;
129static int s_brief = 0;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000130
Matt Caswell0f113f32015-01-22 03:40:55 +0000131static char *keymatexportlabel = NULL;
132static int keymatexportlen = 20;
Ben Lauriee0af0402011-11-15 23:50:52 +0000133
Matt Caswell7e25dd62015-02-13 23:33:12 +0000134static int async = 0;
135
Matt Caswell0f113f32015-01-22 03:40:55 +0000136static const char *session_id_prefix = NULL;
Bodo Möllerb74ba291999-09-03 23:08:45 +0000137
Ben Lauriea7a14a22015-12-16 13:25:07 +0000138#ifndef OPENSSL_NO_DTLS
Ben Laurie36d16f82005-04-26 16:02:40 +0000139static int enable_timeouts = 0;
Bodo Möllerb1277b92006-01-02 23:29:12 +0000140static long socket_mtu;
FdaSilvaYYf2ff1432016-12-06 00:42:01 +0100141#endif
Ben Laurie36d16f82005-04-26 16:02:40 +0000142
Matt Caswell9998b322017-03-17 10:21:25 +0000143/*
144 * We define this but make it always be 0 in no-dtls builds to simplify the
145 * code.
146 */
147static int dtlslisten = 0;
148
Matt Caswell593a2aa2017-03-06 09:51:54 +0000149static int early_data = 0;
150
Nils Larschddac1972006-03-10 23:06:27 +0000151#ifndef OPENSSL_NO_PSK
FdaSilvaYYf2ff1432016-12-06 00:42:01 +0100152static const char psk_identity[] = "Client_identity";
Matt Caswell0f113f32015-01-22 03:40:55 +0000153char *psk_key = NULL; /* by default PSK is not used */
Nils Larschddac1972006-03-10 23:06:27 +0000154
155static unsigned int psk_server_cb(SSL *ssl, const char *identity,
Matt Caswell0f113f32015-01-22 03:40:55 +0000156 unsigned char *psk,
157 unsigned int max_psk_len)
158{
Dr. Stephen Henson6ec6d522016-06-08 19:01:42 +0100159 long key_len = 0;
160 unsigned char *key;
Nils Larschddac1972006-03-10 23:06:27 +0000161
Matt Caswell0f113f32015-01-22 03:40:55 +0000162 if (s_debug)
163 BIO_printf(bio_s_out, "psk_server_cb\n");
164 if (!identity) {
165 BIO_printf(bio_err, "Error: client did not send PSK identity\n");
166 goto out_err;
167 }
168 if (s_debug)
169 BIO_printf(bio_s_out, "identity_len=%d identity=%s\n",
Matt Caswell11abf922015-03-12 14:09:00 +0000170 (int)strlen(identity), identity);
Nils Larschddac1972006-03-10 23:06:27 +0000171
Matt Caswell0f113f32015-01-22 03:40:55 +0000172 /* here we could lookup the given identity e.g. from a database */
173 if (strcmp(identity, psk_identity) != 0) {
174 BIO_printf(bio_s_out, "PSK error: client identity not found"
175 " (got '%s' expected '%s')\n", identity, psk_identity);
176 goto out_err;
177 }
178 if (s_debug)
179 BIO_printf(bio_s_out, "PSK client identity found\n");
Nils Larschddac1972006-03-10 23:06:27 +0000180
Matt Caswell0f113f32015-01-22 03:40:55 +0000181 /* convert the PSK key to binary */
Dr. Stephen Henson6ec6d522016-06-08 19:01:42 +0100182 key = OPENSSL_hexstr2buf(psk_key, &key_len);
183 if (key == NULL) {
184 BIO_printf(bio_err, "Could not convert PSK key '%s' to buffer\n",
Matt Caswell0f113f32015-01-22 03:40:55 +0000185 psk_key);
Matt Caswell0f113f32015-01-22 03:40:55 +0000186 return 0;
187 }
Dr. Stephen Henson6ec6d522016-06-08 19:01:42 +0100188 if (key_len > (int)max_psk_len) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000189 BIO_printf(bio_err,
Dr. Stephen Henson6ec6d522016-06-08 19:01:42 +0100190 "psk buffer of callback is too small (%d) for key (%ld)\n",
191 max_psk_len, key_len);
192 OPENSSL_free(key);
Matt Caswell0f113f32015-01-22 03:40:55 +0000193 return 0;
194 }
Nils Larschddac1972006-03-10 23:06:27 +0000195
Dr. Stephen Henson6ec6d522016-06-08 19:01:42 +0100196 memcpy(psk, key, key_len);
197 OPENSSL_free(key);
Nils Larschddac1972006-03-10 23:06:27 +0000198
Matt Caswell0f113f32015-01-22 03:40:55 +0000199 if (s_debug)
Dr. Stephen Henson6ec6d522016-06-08 19:01:42 +0100200 BIO_printf(bio_s_out, "fetched PSK len=%ld\n", key_len);
201 return key_len;
Nils Larschddac1972006-03-10 23:06:27 +0000202 out_err:
Matt Caswell0f113f32015-01-22 03:40:55 +0000203 if (s_debug)
204 BIO_printf(bio_err, "Error in PSK server callback\n");
Rich Salzc54cc2b2015-04-25 09:26:48 -0400205 (void)BIO_flush(bio_err);
206 (void)BIO_flush(bio_s_out);
Matt Caswell0f113f32015-01-22 03:40:55 +0000207 return 0;
208}
Nils Larschddac1972006-03-10 23:06:27 +0000209#endif
Ben Laurie36d16f82005-04-26 16:02:40 +0000210
Ben Laurieedc032b2011-03-12 17:01:19 +0000211#ifndef OPENSSL_NO_SRP
212/* This is a context that we pass to callbacks */
Matt Caswell0f113f32015-01-22 03:40:55 +0000213typedef struct srpsrvparm_st {
214 char *login;
215 SRP_VBASE *vb;
216 SRP_user_pwd *user;
217} srpsrvparm;
Ben Laurieedc032b2011-03-12 17:01:19 +0000218
Matt Caswell0f113f32015-01-22 03:40:55 +0000219/*
220 * This callback pretends to require some asynchronous logic in order to
221 * obtain a verifier. When the callback is called for a new connection we
222 * return with a negative value. This will provoke the accept etc to return
223 * with an LOOKUP_X509. The main logic of the reinvokes the suspended call
224 * (which would normally occur after a worker has finished) and we set the
225 * user parameters.
226 */
Rich Salz6d23cf92015-01-12 17:29:26 -0500227static int ssl_srp_server_param_cb(SSL *s, int *ad, void *arg)
Matt Caswell0f113f32015-01-22 03:40:55 +0000228{
229 srpsrvparm *p = (srpsrvparm *) arg;
Emilia Kasper380f18e2016-02-24 12:59:59 +0100230 int ret = SSL3_AL_FATAL;
231
Matt Caswell0f113f32015-01-22 03:40:55 +0000232 if (p->login == NULL && p->user == NULL) {
233 p->login = SSL_get_srp_username(s);
234 BIO_printf(bio_err, "SRP username = \"%s\"\n", p->login);
235 return (-1);
236 }
Ben Laurieedc032b2011-03-12 17:01:19 +0000237
Matt Caswell0f113f32015-01-22 03:40:55 +0000238 if (p->user == NULL) {
239 BIO_printf(bio_err, "User %s doesn't exist\n", p->login);
Emilia Kasper380f18e2016-02-24 12:59:59 +0100240 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +0000241 }
Emilia Kasper380f18e2016-02-24 12:59:59 +0100242
Matt Caswell0f113f32015-01-22 03:40:55 +0000243 if (SSL_set_srp_server_param
244 (s, p->user->N, p->user->g, p->user->s, p->user->v,
245 p->user->info) < 0) {
246 *ad = SSL_AD_INTERNAL_ERROR;
Emilia Kasper380f18e2016-02-24 12:59:59 +0100247 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +0000248 }
249 BIO_printf(bio_err,
250 "SRP parameters set: username = \"%s\" info=\"%s\" \n",
251 p->login, p->user->info);
Emilia Kasper380f18e2016-02-24 12:59:59 +0100252 ret = SSL_ERROR_NONE;
253
FdaSilvaYYd6073e22016-08-07 12:04:26 +0200254 err:
Emilia Kasper380f18e2016-02-24 12:59:59 +0100255 SRP_user_pwd_free(p->user);
Matt Caswell0f113f32015-01-22 03:40:55 +0000256 p->user = NULL;
257 p->login = NULL;
Emilia Kasper380f18e2016-02-24 12:59:59 +0100258 return ret;
Matt Caswell0f113f32015-01-22 03:40:55 +0000259}
Ben Laurieedc032b2011-03-12 17:01:19 +0000260
261#endif
262
Matt Caswell0f113f32015-01-22 03:40:55 +0000263static int local_argc = 0;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000264static char **local_argv;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000265
Ulf Möllera53955d1999-06-04 21:35:58 +0000266#ifdef CHARSET_EBCDIC
267static int ebcdic_new(BIO *bi);
268static int ebcdic_free(BIO *a);
269static int ebcdic_read(BIO *b, char *out, int outl);
Bodo Möller0fd05a22002-08-15 14:52:54 +0000270static int ebcdic_write(BIO *b, const char *in, int inl);
271static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr);
Ulf Möllera53955d1999-06-04 21:35:58 +0000272static int ebcdic_gets(BIO *bp, char *buf, int size);
Bodo Möller0fd05a22002-08-15 14:52:54 +0000273static int ebcdic_puts(BIO *bp, const char *str);
Ulf Möllera53955d1999-06-04 21:35:58 +0000274
Matt Caswell0f113f32015-01-22 03:40:55 +0000275# define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
Matt Caswell5fd14782016-04-28 11:34:54 +0100276static BIO_METHOD *methods_ebcdic = NULL;
Ulf Möllera53955d1999-06-04 21:35:58 +0000277
Rich Salz68dc6822015-04-30 17:48:31 -0400278/* This struct is "unwarranted chumminess with the compiler." */
Matt Caswell0f113f32015-01-22 03:40:55 +0000279typedef struct {
280 size_t alloced;
281 char buff[1];
Ulf Möllera53955d1999-06-04 21:35:58 +0000282} EBCDIC_OUTBUFF;
283
Matt Caswell5fd14782016-04-28 11:34:54 +0100284static const BIO_METHOD *BIO_f_ebcdic_filter()
Ulf Möllera53955d1999-06-04 21:35:58 +0000285{
Matt Caswell5fd14782016-04-28 11:34:54 +0100286 if (methods_ebcdic == NULL) {
287 methods_ebcdic = BIO_meth_new(BIO_TYPE_EBCDIC_FILTER,
FdaSilvaYYd6073e22016-08-07 12:04:26 +0200288 "EBCDIC/ASCII filter");
289 if (methods_ebcdic == NULL
Matt Caswell5fd14782016-04-28 11:34:54 +0100290 || !BIO_meth_set_write(methods_ebcdic, ebcdic_write)
291 || !BIO_meth_set_read(methods_ebcdic, ebcdic_read)
292 || !BIO_meth_set_puts(methods_ebcdic, ebcdic_puts)
293 || !BIO_meth_set_gets(methods_ebcdic, ebcdic_gets)
294 || !BIO_meth_set_ctrl(methods_ebcdic, ebcdic_ctrl)
295 || !BIO_meth_set_create(methods_ebcdic, ebcdic_new)
296 || !BIO_meth_set_destroy(methods_ebcdic, ebcdic_free))
297 return NULL;
298 }
299 return methods_ebcdic;
Ulf Möllera53955d1999-06-04 21:35:58 +0000300}
301
302static int ebcdic_new(BIO *bi)
303{
Matt Caswell0f113f32015-01-22 03:40:55 +0000304 EBCDIC_OUTBUFF *wbuf;
Ulf Möllera53955d1999-06-04 21:35:58 +0000305
Rich Salzb4faea52015-05-01 23:10:31 -0400306 wbuf = app_malloc(sizeof(*wbuf) + 1024, "ebcdic wbuf");
Matt Caswell0f113f32015-01-22 03:40:55 +0000307 wbuf->alloced = 1024;
308 wbuf->buff[0] = '\0';
Ulf Möllera53955d1999-06-04 21:35:58 +0000309
Matt Caswell5fd14782016-04-28 11:34:54 +0100310 BIO_set_data(bi, wbuf);
311 BIO_set_init(bi, 1);
312 return 1;
Ulf Möllera53955d1999-06-04 21:35:58 +0000313}
314
315static int ebcdic_free(BIO *a)
316{
Matt Caswell5fd14782016-04-28 11:34:54 +0100317 EBCDIC_OUTBUFF *wbuf;
318
Matt Caswell0f113f32015-01-22 03:40:55 +0000319 if (a == NULL)
Matt Caswell5fd14782016-04-28 11:34:54 +0100320 return 0;
321 wbuf = BIO_get_data(a);
322 OPENSSL_free(wbuf);
323 BIO_set_data(a, NULL);
324 BIO_set_init(a, 0);
325
326 return 1;
Ulf Möllera53955d1999-06-04 21:35:58 +0000327}
Matt Caswell0f113f32015-01-22 03:40:55 +0000328
Ulf Möllera53955d1999-06-04 21:35:58 +0000329static int ebcdic_read(BIO *b, char *out, int outl)
330{
Matt Caswell0f113f32015-01-22 03:40:55 +0000331 int ret = 0;
Matt Caswell5fd14782016-04-28 11:34:54 +0100332 BIO *next = BIO_next(b);
Ulf Möllera53955d1999-06-04 21:35:58 +0000333
Matt Caswell0f113f32015-01-22 03:40:55 +0000334 if (out == NULL || outl == 0)
335 return (0);
Matt Caswell5fd14782016-04-28 11:34:54 +0100336 if (next == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000337 return (0);
Ulf Möllera53955d1999-06-04 21:35:58 +0000338
Matt Caswell5fd14782016-04-28 11:34:54 +0100339 ret = BIO_read(next, out, outl);
Matt Caswell0f113f32015-01-22 03:40:55 +0000340 if (ret > 0)
341 ascii2ebcdic(out, out, ret);
Matt Caswell5fd14782016-04-28 11:34:54 +0100342 return ret;
Ulf Möllera53955d1999-06-04 21:35:58 +0000343}
344
Bodo Möller0fd05a22002-08-15 14:52:54 +0000345static int ebcdic_write(BIO *b, const char *in, int inl)
Ulf Möllera53955d1999-06-04 21:35:58 +0000346{
Matt Caswell0f113f32015-01-22 03:40:55 +0000347 EBCDIC_OUTBUFF *wbuf;
Matt Caswell5fd14782016-04-28 11:34:54 +0100348 BIO *next = BIO_next(b);
Matt Caswell0f113f32015-01-22 03:40:55 +0000349 int ret = 0;
350 int num;
Ulf Möllera53955d1999-06-04 21:35:58 +0000351
Matt Caswell0f113f32015-01-22 03:40:55 +0000352 if ((in == NULL) || (inl <= 0))
353 return (0);
Matt Caswell5fd14782016-04-28 11:34:54 +0100354 if (next == NULL)
355 return 0;
Ulf Möllera53955d1999-06-04 21:35:58 +0000356
Matt Caswell5fd14782016-04-28 11:34:54 +0100357 wbuf = (EBCDIC_OUTBUFF *) BIO_get_data(b);
Ulf Möllera53955d1999-06-04 21:35:58 +0000358
Matt Caswell0f113f32015-01-22 03:40:55 +0000359 if (inl > (num = wbuf->alloced)) {
360 num = num + num; /* double the size */
361 if (num < inl)
362 num = inl;
Matt Caswell5fd14782016-04-28 11:34:54 +0100363 OPENSSL_free(wbuf);
Rich Salzb4faea52015-05-01 23:10:31 -0400364 wbuf = app_malloc(sizeof(*wbuf) + num, "grow ebcdic wbuf");
Ulf Möllera53955d1999-06-04 21:35:58 +0000365
Matt Caswell0f113f32015-01-22 03:40:55 +0000366 wbuf->alloced = num;
367 wbuf->buff[0] = '\0';
Ulf Möllera53955d1999-06-04 21:35:58 +0000368
Matt Caswell5fd14782016-04-28 11:34:54 +0100369 BIO_set_data(b, wbuf);
Matt Caswell0f113f32015-01-22 03:40:55 +0000370 }
Ulf Möllera53955d1999-06-04 21:35:58 +0000371
Matt Caswell0f113f32015-01-22 03:40:55 +0000372 ebcdic2ascii(wbuf->buff, in, inl);
Ulf Möllera53955d1999-06-04 21:35:58 +0000373
Matt Caswell5fd14782016-04-28 11:34:54 +0100374 ret = BIO_write(next, wbuf->buff, inl);
Ulf Möllera53955d1999-06-04 21:35:58 +0000375
Matt Caswell0f113f32015-01-22 03:40:55 +0000376 return (ret);
Ulf Möllera53955d1999-06-04 21:35:58 +0000377}
378
Bodo Möller0fd05a22002-08-15 14:52:54 +0000379static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr)
Ulf Möllera53955d1999-06-04 21:35:58 +0000380{
Matt Caswell0f113f32015-01-22 03:40:55 +0000381 long ret;
Matt Caswell5fd14782016-04-28 11:34:54 +0100382 BIO *next = BIO_next(b);
Ulf Möllera53955d1999-06-04 21:35:58 +0000383
Matt Caswell5fd14782016-04-28 11:34:54 +0100384 if (next == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000385 return (0);
386 switch (cmd) {
387 case BIO_CTRL_DUP:
388 ret = 0L;
389 break;
390 default:
Matt Caswell5fd14782016-04-28 11:34:54 +0100391 ret = BIO_ctrl(next, cmd, num, ptr);
Matt Caswell0f113f32015-01-22 03:40:55 +0000392 break;
393 }
394 return (ret);
Ulf Möllera53955d1999-06-04 21:35:58 +0000395}
396
397static int ebcdic_gets(BIO *bp, char *buf, int size)
398{
Matt Caswell0f113f32015-01-22 03:40:55 +0000399 int i, ret = 0;
Matt Caswell5fd14782016-04-28 11:34:54 +0100400 BIO *next = BIO_next(bp);
401
402 if (next == NULL)
403 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000404/* return(BIO_gets(bp->next_bio,buf,size));*/
405 for (i = 0; i < size - 1; ++i) {
406 ret = ebcdic_read(bp, &buf[i], 1);
407 if (ret <= 0)
408 break;
409 else if (buf[i] == '\n') {
410 ++i;
411 break;
412 }
413 }
414 if (i < size)
415 buf[i] = '\0';
416 return (ret < 0 && i == 0) ? ret : i;
Ulf Möllera53955d1999-06-04 21:35:58 +0000417}
418
Bodo Möller0fd05a22002-08-15 14:52:54 +0000419static int ebcdic_puts(BIO *bp, const char *str)
Ulf Möllera53955d1999-06-04 21:35:58 +0000420{
Matt Caswell5fd14782016-04-28 11:34:54 +0100421 if (BIO_next(bp) == NULL)
422 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000423 return ebcdic_write(bp, str, strlen(str));
Ulf Möllera53955d1999-06-04 21:35:58 +0000424}
425#endif
426
Bodo Möllered3883d2006-01-02 23:14:37 +0000427/* This is a context that we pass to callbacks */
428typedef struct tlsextctx_st {
Matt Caswell0f113f32015-01-22 03:40:55 +0000429 char *servername;
430 BIO *biodebug;
431 int extension_error;
Bodo Möllered3883d2006-01-02 23:14:37 +0000432} tlsextctx;
433
Rich Salz6d23cf92015-01-12 17:29:26 -0500434static int ssl_servername_cb(SSL *s, int *ad, void *arg)
Matt Caswell0f113f32015-01-22 03:40:55 +0000435{
436 tlsextctx *p = (tlsextctx *) arg;
437 const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
438 if (servername && p->biodebug)
439 BIO_printf(p->biodebug, "Hostname in TLS extension: \"%s\"\n",
440 servername);
441
442 if (!p->servername)
443 return SSL_TLSEXT_ERR_NOACK;
444
445 if (servername) {
446 if (strcasecmp(servername, p->servername))
447 return p->extension_error;
448 if (ctx2) {
449 BIO_printf(p->biodebug, "Switching server context.\n");
450 SSL_set_SSL_CTX(s, ctx2);
451 }
452 }
453 return SSL_TLSEXT_ERR_OK;
Bodo Möllered3883d2006-01-02 23:14:37 +0000454}
Dr. Stephen Henson67c8e7f2007-09-26 21:56:59 +0000455
456/* Structure passed to cert status callback */
Dr. Stephen Henson67c8e7f2007-09-26 21:56:59 +0000457typedef struct tlsextstatusctx_st {
Matt Caswellf5ca0b02016-11-21 12:10:35 +0000458 int timeout;
Matt Caswellacf65ae2016-11-15 14:22:29 +0000459 /* File to load OCSP Response from (or NULL if no file) */
460 char *respin;
Matt Caswell0f113f32015-01-22 03:40:55 +0000461 /* Default responder to use */
462 char *host, *path, *port;
463 int use_ssl;
Matt Caswell0f113f32015-01-22 03:40:55 +0000464 int verbose;
Dr. Stephen Henson67c8e7f2007-09-26 21:56:59 +0000465} tlsextstatusctx;
466
Matt Caswellf5ca0b02016-11-21 12:10:35 +0000467static tlsextstatusctx tlscstatp = { -1 };
Dr. Stephen Henson67c8e7f2007-09-26 21:56:59 +0000468
Matt Caswell3e41ac32016-03-21 16:54:53 +0000469#ifndef OPENSSL_NO_OCSP
Dr. Stephen Henson67c8e7f2007-09-26 21:56:59 +0000470
Matt Caswellacf65ae2016-11-15 14:22:29 +0000471/*
472 * Helper function to get an OCSP_RESPONSE from a responder. This is a
473 * simplified version. It examines certificates each time and makes one OCSP
474 * responder query for each request. A full version would store details such as
475 * the OCSP certificate IDs and minimise the number of OCSP responses by caching
476 * them until they were considered "expired".
477 */
478static int get_ocsp_resp_from_responder(SSL *s, tlsextstatusctx *srctx,
479 OCSP_RESPONSE **resp)
Matt Caswell0f113f32015-01-22 03:40:55 +0000480{
Gunnar Kudrjavets4c9b0a02015-05-06 10:16:55 +0100481 char *host = NULL, *port = NULL, *path = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +0000482 int use_ssl;
Matt Caswell0f113f32015-01-22 03:40:55 +0000483 STACK_OF(OPENSSL_STRING) *aia = NULL;
484 X509 *x = NULL;
Rich Salzf0e0fd52016-04-14 23:59:26 -0400485 X509_STORE_CTX *inctx = NULL;
486 X509_OBJECT *obj;
Matt Caswell0f113f32015-01-22 03:40:55 +0000487 OCSP_REQUEST *req = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +0000488 OCSP_CERTID *id = NULL;
489 STACK_OF(X509_EXTENSION) *exts;
490 int ret = SSL_TLSEXT_ERR_NOACK;
491 int i;
Rich Salz7e1b7482015-04-24 15:26:15 -0400492
Matt Caswell0f113f32015-01-22 03:40:55 +0000493 /* Build up OCSP query from server certificate */
494 x = SSL_get_certificate(s);
495 aia = X509_get1_ocsp(x);
496 if (aia) {
497 if (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia, 0),
498 &host, &port, &path, &use_ssl)) {
Rich Salz7e1b7482015-04-24 15:26:15 -0400499 BIO_puts(bio_err, "cert_status: can't parse AIA URL\n");
Matt Caswell0f113f32015-01-22 03:40:55 +0000500 goto err;
501 }
502 if (srctx->verbose)
Rich Salz7e1b7482015-04-24 15:26:15 -0400503 BIO_printf(bio_err, "cert_status: AIA URL: %s\n",
Matt Caswell0f113f32015-01-22 03:40:55 +0000504 sk_OPENSSL_STRING_value(aia, 0));
505 } else {
506 if (!srctx->host) {
Rich Salz7e1b7482015-04-24 15:26:15 -0400507 BIO_puts(bio_err,
Matt Caswell0f113f32015-01-22 03:40:55 +0000508 "cert_status: no AIA and no default responder URL\n");
509 goto done;
510 }
511 host = srctx->host;
512 path = srctx->path;
513 port = srctx->port;
514 use_ssl = srctx->use_ssl;
515 }
516
Rich Salzf0e0fd52016-04-14 23:59:26 -0400517 inctx = X509_STORE_CTX_new();
518 if (inctx == NULL)
519 goto err;
520 if (!X509_STORE_CTX_init(inctx,
Matt Caswell0f113f32015-01-22 03:40:55 +0000521 SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)),
522 NULL, NULL))
523 goto err;
Rich Salz6ddbb4c2016-05-17 16:06:09 -0400524 obj = X509_STORE_CTX_get_obj_by_subject(inctx, X509_LU_X509,
525 X509_get_issuer_name(x));
Rich Salzf0e0fd52016-04-14 23:59:26 -0400526 if (obj == NULL) {
Rich Salz7e1b7482015-04-24 15:26:15 -0400527 BIO_puts(bio_err, "cert_status: Can't retrieve issuer certificate.\n");
Matt Caswell0f113f32015-01-22 03:40:55 +0000528 goto done;
529 }
Rich Salzf0e0fd52016-04-14 23:59:26 -0400530 id = OCSP_cert_to_id(NULL, x, X509_OBJECT_get0_X509(obj));
531 X509_OBJECT_free(obj);
Matt Caswell0f113f32015-01-22 03:40:55 +0000532 if (!id)
533 goto err;
Matt Caswell0461b7e2016-04-26 18:25:39 +0100534 req = OCSP_REQUEST_new();
535 if (req == NULL)
536 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +0000537 if (!OCSP_request_add0_id(req, id))
538 goto err;
539 id = NULL;
540 /* Add any extensions to the request */
541 SSL_get_tlsext_status_exts(s, &exts);
542 for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
543 X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
544 if (!OCSP_REQUEST_add_ext(req, ext, -1))
545 goto err;
546 }
Matt Caswellacf65ae2016-11-15 14:22:29 +0000547 *resp = process_responder(req, host, path, port, use_ssl, NULL,
Matt Caswell0f113f32015-01-22 03:40:55 +0000548 srctx->timeout);
Matt Caswellacf65ae2016-11-15 14:22:29 +0000549 if (*resp == NULL) {
Rich Salz7e1b7482015-04-24 15:26:15 -0400550 BIO_puts(bio_err, "cert_status: error querying responder\n");
Matt Caswell0f113f32015-01-22 03:40:55 +0000551 goto done;
552 }
Matt Caswellacf65ae2016-11-15 14:22:29 +0000553
Matt Caswell0f113f32015-01-22 03:40:55 +0000554 ret = SSL_TLSEXT_ERR_OK;
Rich Salzf0e0fd52016-04-14 23:59:26 -0400555 goto done;
556
557 err:
558 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
Matt Caswell0f113f32015-01-22 03:40:55 +0000559 done:
Matt Caswell6530c492016-11-23 15:38:32 +0000560 /*
561 * If we parsed aia we need to free; otherwise they were copied and we
562 * don't
563 */
Matt Caswellf5ca0b02016-11-21 12:10:35 +0000564 if (aia != NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000565 OPENSSL_free(host);
566 OPENSSL_free(path);
567 OPENSSL_free(port);
568 X509_email_free(aia);
569 }
Rich Salz25aaa982015-05-01 14:37:16 -0400570 OCSP_CERTID_free(id);
571 OCSP_REQUEST_free(req);
Rich Salzf0e0fd52016-04-14 23:59:26 -0400572 X509_STORE_CTX_free(inctx);
Matt Caswell0f113f32015-01-22 03:40:55 +0000573 return ret;
Matt Caswell0f113f32015-01-22 03:40:55 +0000574}
Matt Caswellacf65ae2016-11-15 14:22:29 +0000575
576/*
577 * Certificate Status callback. This is called when a client includes a
578 * certificate status request extension. The response is either obtained from a
579 * file, or from an OCSP responder.
580 */
581static int cert_status_cb(SSL *s, void *arg)
582{
583 tlsextstatusctx *srctx = arg;
584 OCSP_RESPONSE *resp = NULL;
585 unsigned char *rspder = NULL;
586 int rspderlen;
587 int ret = SSL_TLSEXT_ERR_ALERT_FATAL;
588
589 if (srctx->verbose)
590 BIO_puts(bio_err, "cert_status: callback called\n");
591
592 if (srctx->respin != NULL) {
593 BIO *derbio = bio_open_default(srctx->respin, 'r', FORMAT_ASN1);
594 if (derbio == NULL) {
595 BIO_puts(bio_err, "cert_status: Cannot open OCSP response file\n");
596 goto err;
597 }
598 resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
599 BIO_free(derbio);
Matt Caswellf5ca0b02016-11-21 12:10:35 +0000600 if (resp == NULL) {
Matt Caswellacf65ae2016-11-15 14:22:29 +0000601 BIO_puts(bio_err, "cert_status: Error reading OCSP response\n");
602 goto err;
603 }
604 } else {
605 ret = get_ocsp_resp_from_responder(s, srctx, &resp);
606 if (ret != SSL_TLSEXT_ERR_OK)
607 goto err;
608 }
609
610 rspderlen = i2d_OCSP_RESPONSE(resp, &rspder);
611 if (rspderlen <= 0)
612 goto err;
613
614 SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen);
615 if (srctx->verbose) {
616 BIO_puts(bio_err, "cert_status: ocsp response sent:\n");
617 OCSP_RESPONSE_print(bio_err, resp, 2);
618 }
619
620 ret = SSL_TLSEXT_ERR_OK;
621
622 err:
623 if (ret != SSL_TLSEXT_ERR_OK)
624 ERR_print_errors(bio_err);
625
626 OCSP_RESPONSE_free(resp);
627
628 return ret;
629}
Matt Caswell3e41ac32016-03-21 16:54:53 +0000630#endif
Ben Laurieee2ffc22010-07-28 10:06:55 +0000631
Matt Caswelle481f9b2015-05-15 10:49:56 +0100632#ifndef OPENSSL_NO_NEXTPROTONEG
Ben Laurieee2ffc22010-07-28 10:06:55 +0000633/* This is the context that we pass to next_proto_cb */
634typedef struct tlsextnextprotoctx_st {
Matt Caswell0f113f32015-01-22 03:40:55 +0000635 unsigned char *data;
FdaSilvaYYf2ff1432016-12-06 00:42:01 +0100636 size_t len;
Ben Laurieee2ffc22010-07-28 10:06:55 +0000637} tlsextnextprotoctx;
638
Matt Caswell0f113f32015-01-22 03:40:55 +0000639static int next_proto_cb(SSL *s, const unsigned char **data,
640 unsigned int *len, void *arg)
641{
642 tlsextnextprotoctx *next_proto = arg;
Ben Laurieee2ffc22010-07-28 10:06:55 +0000643
Matt Caswell0f113f32015-01-22 03:40:55 +0000644 *data = next_proto->data;
645 *len = next_proto->len;
Ben Laurieee2ffc22010-07-28 10:06:55 +0000646
Matt Caswell0f113f32015-01-22 03:40:55 +0000647 return SSL_TLSEXT_ERR_OK;
648}
Matt Caswelle481f9b2015-05-15 10:49:56 +0100649#endif /* ndef OPENSSL_NO_NEXTPROTONEG */
Adam Langley6f017a82013-04-15 18:07:47 -0400650
651/* This the context that we pass to alpn_cb */
652typedef struct tlsextalpnctx_st {
Matt Caswell0f113f32015-01-22 03:40:55 +0000653 unsigned char *data;
Todd Short817cd0d2016-03-05 08:47:55 -0500654 size_t len;
Adam Langley6f017a82013-04-15 18:07:47 -0400655} tlsextalpnctx;
656
Matt Caswell0f113f32015-01-22 03:40:55 +0000657static int alpn_cb(SSL *s, const unsigned char **out, unsigned char *outlen,
658 const unsigned char *in, unsigned int inlen, void *arg)
659{
660 tlsextalpnctx *alpn_ctx = arg;
Adam Langley6f017a82013-04-15 18:07:47 -0400661
Matt Caswell0f113f32015-01-22 03:40:55 +0000662 if (!s_quiet) {
663 /* We can assume that |in| is syntactically valid. */
Todd Short817cd0d2016-03-05 08:47:55 -0500664 unsigned int i;
Matt Caswell0f113f32015-01-22 03:40:55 +0000665 BIO_printf(bio_s_out, "ALPN protocols advertised by the client: ");
666 for (i = 0; i < inlen;) {
667 if (i)
668 BIO_write(bio_s_out, ", ", 2);
669 BIO_write(bio_s_out, &in[i + 1], in[i]);
670 i += in[i] + 1;
671 }
672 BIO_write(bio_s_out, "\n", 1);
673 }
Adam Langley6f017a82013-04-15 18:07:47 -0400674
Matt Caswell0f113f32015-01-22 03:40:55 +0000675 if (SSL_select_next_proto
676 ((unsigned char **)out, outlen, alpn_ctx->data, alpn_ctx->len, in,
677 inlen) != OPENSSL_NPN_NEGOTIATED) {
678 return SSL_TLSEXT_ERR_NOACK;
679 }
Adam Langley6f017a82013-04-15 18:07:47 -0400680
Matt Caswell0f113f32015-01-22 03:40:55 +0000681 if (!s_quiet) {
682 BIO_printf(bio_s_out, "ALPN protocols selected: ");
683 BIO_write(bio_s_out, *out, *outlen);
684 BIO_write(bio_s_out, "\n", 1);
685 }
Adam Langley6f017a82013-04-15 18:07:47 -0400686
Matt Caswell0f113f32015-01-22 03:40:55 +0000687 return SSL_TLSEXT_ERR_OK;
688}
Bodo Möllered3883d2006-01-02 23:14:37 +0000689
Bodo Möller7c2d4fe2010-08-26 15:15:47 +0000690static int not_resumable_sess_cb(SSL *s, int is_forward_secure)
Matt Caswell0f113f32015-01-22 03:40:55 +0000691{
692 /* disable resumption for sessions with forward secure ciphers */
693 return is_forward_secure;
694}
Bodo Möller7c2d4fe2010-08-26 15:15:47 +0000695
Dr. Stephen Hensonc79f22c2011-12-27 14:21:45 +0000696#ifndef OPENSSL_NO_SRP
Matt Caswell0f113f32015-01-22 03:40:55 +0000697static srpsrvparm srp_callback_parm;
Dr. Stephen Hensonc79f22c2011-12-27 14:21:45 +0000698#endif
Piotr Sikorae783bae2014-12-22 11:15:51 +0000699#ifndef OPENSSL_NO_SRTP
Ben Laurie333f9262011-11-15 22:59:20 +0000700static char *srtp_profiles = NULL;
Piotr Sikorae783bae2014-12-22 11:15:51 +0000701#endif
Ben Laurie6caa4ed2008-10-26 18:40:52 +0000702
Rich Salz7e1b7482015-04-24 15:26:15 -0400703typedef enum OPTION_choice {
Richard Levitteab69ac02016-02-03 00:47:42 +0100704 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_ENGINE,
705 OPT_4, OPT_6, OPT_ACCEPT, OPT_PORT, OPT_UNIX, OPT_UNLINK, OPT_NACCEPT,
Dmitry Belyavskiya7c04f22017-02-21 14:22:55 +0300706 OPT_VERIFY, OPT_NAMEOPT, OPT_UPPER_V_VERIFY, OPT_CONTEXT, OPT_CERT, OPT_CRL,
Rich Salz7e1b7482015-04-24 15:26:15 -0400707 OPT_CRL_DOWNLOAD, OPT_SERVERINFO, OPT_CERTFORM, OPT_KEY, OPT_KEYFORM,
708 OPT_PASS, OPT_CERT_CHAIN, OPT_DHPARAM, OPT_DCERTFORM, OPT_DCERT,
709 OPT_DKEYFORM, OPT_DPASS, OPT_DKEY, OPT_DCERT_CHAIN, OPT_NOCERT,
Matt Caswell2b6bcb72015-09-22 16:00:52 +0100710 OPT_CAPATH, OPT_NOCAPATH, OPT_CHAINCAPATH, OPT_VERIFYCAPATH, OPT_NO_CACHE,
Rich Salz7e1b7482015-04-24 15:26:15 -0400711 OPT_EXT_CACHE, OPT_CRLFORM, OPT_VERIFY_RET_ERROR, OPT_VERIFY_QUIET,
Matt Caswell2b6bcb72015-09-22 16:00:52 +0100712 OPT_BUILD_CHAIN, OPT_CAFILE, OPT_NOCAFILE, OPT_CHAINCAFILE,
713 OPT_VERIFYCAFILE, OPT_NBIO, OPT_NBIO_TEST, OPT_IGN_EOF, OPT_NO_IGN_EOF,
714 OPT_DEBUG, OPT_TLSEXTDEBUG, OPT_STATUS, OPT_STATUS_VERBOSE,
Matt Caswellacf65ae2016-11-15 14:22:29 +0000715 OPT_STATUS_TIMEOUT, OPT_STATUS_URL, OPT_STATUS_FILE, OPT_MSG, OPT_MSGFILE,
716 OPT_TRACE, OPT_SECURITY_DEBUG, OPT_SECURITY_DEBUG_VERBOSE, OPT_STATE,
717 OPT_CRLF, OPT_QUIET, OPT_BRIEF, OPT_NO_DHE,
Rich Salz7e1b7482015-04-24 15:26:15 -0400718 OPT_NO_RESUME_EPHEMERAL, OPT_PSK_HINT, OPT_PSK, OPT_SRPVFILE,
Matt Caswell7e25dd62015-02-13 23:33:12 +0000719 OPT_SRPUSERSEED, OPT_REV, OPT_WWW, OPT_UPPER_WWW, OPT_HTTP, OPT_ASYNC,
Matt Caswelldad78fb2016-01-13 14:20:25 +0000720 OPT_SSL_CONFIG, OPT_SPLIT_SEND_FRAG, OPT_MAX_PIPELINES, OPT_READ_BUF,
Matt Caswell582a17d2016-10-21 17:39:33 +0100721 OPT_SSL3, OPT_TLS1_3, OPT_TLS1_2, OPT_TLS1_1, OPT_TLS1, OPT_DTLS, OPT_DTLS1,
FdaSilvaYY55e0d0b2016-07-11 19:45:40 +0200722 OPT_DTLS1_2, OPT_TIMEOUT, OPT_MTU, OPT_LISTEN,
Rich Salz7e1b7482015-04-24 15:26:15 -0400723 OPT_ID_PREFIX, OPT_RAND, OPT_SERVERNAME, OPT_SERVERNAME_FATAL,
Rich Salzdba31772016-02-14 00:17:59 -0500724 OPT_CERT2, OPT_KEY2, OPT_NEXTPROTONEG, OPT_ALPN,
Rich Salz7e1b7482015-04-24 15:26:15 -0400725 OPT_SRTP_PROFILES, OPT_KEYMATEXPORT, OPT_KEYMATEXPORTLEN,
Matt Caswelle0655182017-02-22 15:24:11 +0000726 OPT_KEYLOG_FILE, OPT_MAX_EARLY, OPT_EARLY_DATA,
Rich Salz7e1b7482015-04-24 15:26:15 -0400727 OPT_S_ENUM,
728 OPT_V_ENUM,
Matt Caswell55614192015-05-12 10:35:51 +0100729 OPT_X_ENUM
Rich Salz7e1b7482015-04-24 15:26:15 -0400730} OPTION_CHOICE;
731
FdaSilvaYY44c83eb2016-03-13 14:07:50 +0100732const OPTIONS s_server_options[] = {
Rich Salz7e1b7482015-04-24 15:26:15 -0400733 {"help", OPT_HELP, '-', "Display this summary"},
A J Mohan Rao32eabe32016-02-09 10:55:42 -0500734 {"port", OPT_PORT, 'p',
735 "TCP/IP port to listen on for connections (default is " PORT ")"},
Richard Levitteab69ac02016-02-03 00:47:42 +0100736 {"accept", OPT_ACCEPT, 's',
EasySeca22f9c82016-11-12 21:08:32 +0100737 "TCP/IP optional host and port to listen on for connections (default is *:" PORT ")"},
Richard Levitteab69ac02016-02-03 00:47:42 +0100738#ifdef AF_UNIX
Rich Salz7e1b7482015-04-24 15:26:15 -0400739 {"unix", OPT_UNIX, 's', "Unix domain socket to accept on"},
Richard Levitteab69ac02016-02-03 00:47:42 +0100740#endif
741 {"4", OPT_4, '-', "Use IPv4 only"},
742 {"6", OPT_6, '-', "Use IPv6 only"},
A J Mohan Rao32eabe32016-02-09 10:55:42 -0500743#ifdef AF_UNIX
Rich Salz7e1b7482015-04-24 15:26:15 -0400744 {"unlink", OPT_UNLINK, '-', "For -unix, unlink existing socket first"},
A J Mohan Rao32eabe32016-02-09 10:55:42 -0500745#endif
Rich Salz7e1b7482015-04-24 15:26:15 -0400746 {"context", OPT_CONTEXT, 's', "Set session ID context"},
747 {"verify", OPT_VERIFY, 'n', "Turn on peer certificate verification"},
748 {"Verify", OPT_UPPER_V_VERIFY, 'n',
749 "Turn on peer certificate verification, must have a cert"},
750 {"cert", OPT_CERT, '<', "Certificate file to use; default is " TEST_CERT},
Dmitry Belyavskiya7c04f22017-02-21 14:22:55 +0300751 {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
FdaSilvaYYceab33e2016-07-05 21:22:18 +0200752 {"naccept", OPT_NACCEPT, 'p', "Terminate after #num connections"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400753 {"serverinfo", OPT_SERVERINFO, 's',
754 "PEM serverinfo file for certificate"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400755 {"certform", OPT_CERTFORM, 'F',
756 "Certificate format (PEM or DER) PEM default"},
757 {"key", OPT_KEY, '<',
758 "Private Key if not in -cert; default is " TEST_CERT},
759 {"keyform", OPT_KEYFORM, 'f',
760 "Key format (PEM, DER or ENGINE) PEM default"},
761 {"pass", OPT_PASS, 's', "Private key file pass phrase source"},
762 {"dcert", OPT_DCERT, '<',
763 "Second certificate file to use (usually for DSA)"},
764 {"dcertform", OPT_DCERTFORM, 'F',
765 "Second certificate format (PEM or DER) PEM default"},
766 {"dkey", OPT_DKEY, '<',
767 "Second private key file to use (usually for DSA)"},
768 {"dkeyform", OPT_DKEYFORM, 'F',
769 "Second key format (PEM, DER or ENGINE) PEM default"},
770 {"dpass", OPT_DPASS, 's', "Second private key file pass phrase source"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400771 {"nbio_test", OPT_NBIO_TEST, '-', "Test with the non-blocking test bio"},
772 {"crlf", OPT_CRLF, '-', "Convert LF from terminal into CRLF"},
773 {"debug", OPT_DEBUG, '-', "Print more output"},
774 {"msg", OPT_MSG, '-', "Show protocol messages"},
A J Mohan Rao32eabe32016-02-09 10:55:42 -0500775 {"msgfile", OPT_MSGFILE, '>',
776 "File to send output of -msg or -trace, instead of stdout"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400777 {"state", OPT_STATE, '-', "Print the SSL states"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400778 {"CAfile", OPT_CAFILE, '<', "PEM format file of CA's"},
Matt Caswell2b6bcb72015-09-22 16:00:52 +0100779 {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
780 {"no-CAfile", OPT_NOCAFILE, '-',
781 "Do not load the default certificates file"},
782 {"no-CApath", OPT_NOCAPATH, '-',
783 "Do not load certificates from the default certificates directory"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400784 {"nocert", OPT_NOCERT, '-', "Don't use any certificates (Anon-DH)"},
785 {"quiet", OPT_QUIET, '-', "No server output"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400786 {"no_resume_ephemeral", OPT_NO_RESUME_EPHEMERAL, '-',
787 "Disable caching and tickets if ephemeral (EC)DH is used"},
788 {"www", OPT_WWW, '-', "Respond to a 'GET /' with a status page"},
789 {"WWW", OPT_UPPER_WWW, '-', "Respond to a 'GET with the file ./path"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400790 {"servername", OPT_SERVERNAME, 's',
791 "Servername for HostName TLS extension"},
792 {"servername_fatal", OPT_SERVERNAME_FATAL, '-',
793 "mismatch send fatal alert (default warning alert)"},
794 {"cert2", OPT_CERT2, '<',
795 "Certificate file to use for servername; default is" TEST_CERT2},
796 {"key2", OPT_KEY2, '<',
797 "-Private Key file to use for servername if not in -cert2"},
798 {"tlsextdebug", OPT_TLSEXTDEBUG, '-',
799 "Hex dump of all TLS extensions received"},
FdaSilvaYYceab33e2016-07-05 21:22:18 +0200800 {"HTTP", OPT_HTTP, '-', "Like -WWW but ./path includes HTTP headers"},
Rich Salz9c3bcfa2015-05-15 13:50:38 -0400801 {"id_prefix", OPT_ID_PREFIX, 's',
802 "Generate SSL/TLS session IDs prefixed by arg"},
803 {"rand", OPT_RAND, 's',
804 "Load the file(s) into the random number generator"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400805 {"keymatexport", OPT_KEYMATEXPORT, 's',
806 "Export keying material using label"},
807 {"keymatexportlen", OPT_KEYMATEXPORTLEN, 'p',
808 "Export len bytes of keying material (default 20)"},
A J Mohan Rao32eabe32016-02-09 10:55:42 -0500809 {"CRL", OPT_CRL, '<', "CRL file to use"},
810 {"crl_download", OPT_CRL_DOWNLOAD, '-',
811 "Download CRL from distribution points"},
812 {"cert_chain", OPT_CERT_CHAIN, '<',
813 "certificate chain file in PEM format"},
814 {"dcert_chain", OPT_DCERT_CHAIN, '<',
815 "second certificate chain file in PEM format"},
816 {"chainCApath", OPT_CHAINCAPATH, '/',
817 "use dir as certificate store path to build CA certificate chain"},
818 {"verifyCApath", OPT_VERIFYCAPATH, '/',
819 "use dir as certificate store path to verify CA certificate"},
820 {"no_cache", OPT_NO_CACHE, '-', "Disable session cache"},
821 {"ext_cache", OPT_EXT_CACHE, '-',
822 "Disable internal cache, setup and use external cache"},
FdaSilvaYYd6073e22016-08-07 12:04:26 +0200823 {"CRLform", OPT_CRLFORM, 'F', "CRL format (PEM or DER) PEM is default"},
A J Mohan Rao32eabe32016-02-09 10:55:42 -0500824 {"verify_return_error", OPT_VERIFY_RET_ERROR, '-',
825 "Close connection on verification error"},
826 {"verify_quiet", OPT_VERIFY_QUIET, '-',
827 "No verify output except verify errors"},
828 {"build_chain", OPT_BUILD_CHAIN, '-', "Build certificate chain"},
829 {"chainCAfile", OPT_CHAINCAFILE, '<',
830 "CA file for certificate chain (PEM format)"},
831 {"verifyCAfile", OPT_VERIFYCAFILE, '<',
832 "CA file for certificate verification (PEM format)"},
833 {"ign_eof", OPT_IGN_EOF, '-', "ignore input eof (default when -quiet)"},
834 {"no_ign_eof", OPT_NO_IGN_EOF, '-', "Do not ignore input eof"},
Matt Caswell3e41ac32016-03-21 16:54:53 +0000835#ifndef OPENSSL_NO_OCSP
A J Mohan Rao32eabe32016-02-09 10:55:42 -0500836 {"status", OPT_STATUS, '-', "Request certificate status from server"},
837 {"status_verbose", OPT_STATUS_VERBOSE, '-',
838 "Print more output in certificate status callback"},
839 {"status_timeout", OPT_STATUS_TIMEOUT, 'n',
840 "Status request responder timeout"},
841 {"status_url", OPT_STATUS_URL, 's', "Status request fallback URL"},
Matt Caswellacf65ae2016-11-15 14:22:29 +0000842 {"status_file", OPT_STATUS_FILE, '<',
843 "File containing DER encoded OCSP Response"},
Matt Caswell3e41ac32016-03-21 16:54:53 +0000844#endif
A J Mohan Rao32eabe32016-02-09 10:55:42 -0500845#ifndef OPENSSL_NO_SSL_TRACE
846 {"trace", OPT_TRACE, '-', "trace protocol messages"},
847#endif
848 {"security_debug", OPT_SECURITY_DEBUG, '-',
849 "Print output from SSL/TLS security framework"},
850 {"security_debug_verbose", OPT_SECURITY_DEBUG_VERBOSE, '-',
851 "Print more output from SSL/TLS security framework"},
FdaSilvaYYd6073e22016-08-07 12:04:26 +0200852 {"brief", OPT_BRIEF, '-',
A J Mohan Rao32eabe32016-02-09 10:55:42 -0500853 "Restrict output to brief summary of connection parameters"},
854 {"rev", OPT_REV, '-',
855 "act as a simple test server which just sends back with the received text reversed"},
Matt Caswell7e25dd62015-02-13 23:33:12 +0000856 {"async", OPT_ASYNC, '-', "Operate in asynchronous mode"},
FdaSilvaYYd6073e22016-08-07 12:04:26 +0200857 {"ssl_config", OPT_SSL_CONFIG, 's',
A J Mohan Rao32eabe32016-02-09 10:55:42 -0500858 "Configure SSL_CTX using the configuration 'val'"},
Matt Caswell032c6d22015-09-22 11:23:33 +0100859 {"split_send_frag", OPT_SPLIT_SEND_FRAG, 'n',
Matt Caswell0df80882016-02-16 11:13:33 +0000860 "Size used to split data for encrypt pipelines"},
Matt Caswell032c6d22015-09-22 11:23:33 +0100861 {"max_pipelines", OPT_MAX_PIPELINES, 'n',
862 "Maximum number of encrypt/decrypt pipelines to be used"},
Matt Caswelldad78fb2016-01-13 14:20:25 +0000863 {"read_buf", OPT_READ_BUF, 'n',
864 "Default read buffer size to be used for connections"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400865 OPT_S_OPTIONS,
866 OPT_V_OPTIONS,
867 OPT_X_OPTIONS,
Rich Salz9c3bcfa2015-05-15 13:50:38 -0400868 {"nbio", OPT_NBIO, '-', "Use non-blocking IO"},
Rich Salz9c3bcfa2015-05-15 13:50:38 -0400869#ifndef OPENSSL_NO_PSK
870 {"psk_hint", OPT_PSK_HINT, 's', "PSK identity hint to use"},
871 {"psk", OPT_PSK, 's', "PSK in hex (without 0x)"},
Rich Salz9c3bcfa2015-05-15 13:50:38 -0400872#endif
873#ifndef OPENSSL_NO_SRP
874 {"srpvfile", OPT_SRPVFILE, '<', "The verifier file for SRP"},
875 {"srpuserseed", OPT_SRPUSERSEED, 's',
876 "A seed string for a default user salt"},
877#endif
878#ifndef OPENSSL_NO_SSL3
879 {"ssl3", OPT_SSL3, '-', "Just talk SSLv3"},
880#endif
Viktor Dukhovni6b01bed2016-01-18 13:10:21 -0500881#ifndef OPENSSL_NO_TLS1
882 {"tls1", OPT_TLS1, '-', "Just talk TLSv1"},
883#endif
884#ifndef OPENSSL_NO_TLS1_1
885 {"tls1_1", OPT_TLS1_1, '-', "Just talk TLSv1.1"},
886#endif
887#ifndef OPENSSL_NO_TLS1_2
888 {"tls1_2", OPT_TLS1_2, '-', "just talk TLSv1.2"},
889#endif
Matt Caswell582a17d2016-10-21 17:39:33 +0100890#ifndef OPENSSL_NO_TLS1_3
891 {"tls1_3", OPT_TLS1_3, '-', "just talk TLSv1.3"},
892#endif
Kurt Roeckxa5ecdc62015-12-12 11:12:22 +0100893#ifndef OPENSSL_NO_DTLS
A J Mohan Rao32eabe32016-02-09 10:55:42 -0500894 {"dtls", OPT_DTLS, '-', "Use any DTLS version"},
Rich Salz9c3bcfa2015-05-15 13:50:38 -0400895 {"timeout", OPT_TIMEOUT, '-', "Enable timeouts"},
896 {"mtu", OPT_MTU, 'p', "Set link layer MTU"},
Matt Caswellfd4e98e2015-04-09 10:01:05 +0100897 {"listen", OPT_LISTEN, '-',
898 "Listen for a DTLS ClientHello with a cookie and then connect"},
Rich Salz9c3bcfa2015-05-15 13:50:38 -0400899#endif
Viktor Dukhovni6b01bed2016-01-18 13:10:21 -0500900#ifndef OPENSSL_NO_DTLS1
901 {"dtls1", OPT_DTLS1, '-', "Just talk DTLSv1"},
902#endif
903#ifndef OPENSSL_NO_DTLS1_2
904 {"dtls1_2", OPT_DTLS1_2, '-', "Just talk DTLSv1.2"},
905#endif
Rich Salz9c3bcfa2015-05-15 13:50:38 -0400906#ifndef OPENSSL_NO_DH
907 {"no_dhe", OPT_NO_DHE, '-', "Disable ephemeral DH"},
908#endif
Rich Salz9c3bcfa2015-05-15 13:50:38 -0400909#ifndef OPENSSL_NO_NEXTPROTONEG
910 {"nextprotoneg", OPT_NEXTPROTONEG, 's',
911 "Set the advertised protocols for the NPN extension (comma-separated list)"},
912#endif
913#ifndef OPENSSL_NO_SRTP
Matt Caswelle77bdc72015-08-04 19:18:02 +0100914 {"use_srtp", OPT_SRTP_PROFILES, 's',
Rich Salz9c3bcfa2015-05-15 13:50:38 -0400915 "Offer SRTP key management with a colon-separated profile list"},
J Mohan Rao Arisankalab07c7032016-02-27 08:50:07 +0530916#endif
Rich Salz9c3bcfa2015-05-15 13:50:38 -0400917 {"alpn", OPT_ALPN, 's',
918 "Set the advertised protocols for the ALPN extension (comma-separated list)"},
Rich Salz9c3bcfa2015-05-15 13:50:38 -0400919#ifndef OPENSSL_NO_ENGINE
A J Mohan Rao32eabe32016-02-09 10:55:42 -0500920 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
Rich Salz9c3bcfa2015-05-15 13:50:38 -0400921#endif
Peter Wu4bf73e92017-02-01 19:14:27 +0100922 {"keylogfile", OPT_KEYLOG_FILE, '>', "Write TLS secrets to file"},
Matt Caswell67466482017-02-23 16:54:11 +0000923 {"max_early_data", OPT_MAX_EARLY, 'n',
Matt Caswell048b1892017-02-17 17:01:16 +0000924 "The maximum number of bytes of early data"},
Matt Caswelle0655182017-02-22 15:24:11 +0000925 {"early_data", OPT_EARLY_DATA, '-', "Attempt to read early data"},
FdaSilvaYYbde136c2016-03-18 19:02:17 +0100926 {NULL, OPT_EOF, 0, NULL}
Rich Salz7e1b7482015-04-24 15:26:15 -0400927};
928
Matt Caswell4bbd4ba2016-07-07 11:05:31 +0100929#define IS_PROT_FLAG(o) \
930 (o == OPT_SSL3 || o == OPT_TLS1 || o == OPT_TLS1_1 || o == OPT_TLS1_2 \
Matt Caswell582a17d2016-10-21 17:39:33 +0100931 || o == OPT_TLS1_3 || o == OPT_DTLS || o == OPT_DTLS1 || o == OPT_DTLS1_2)
Matt Caswell4bbd4ba2016-07-07 11:05:31 +0100932
Rich Salz7e1b7482015-04-24 15:26:15 -0400933int s_server_main(int argc, char *argv[])
Matt Caswell0f113f32015-01-22 03:40:55 +0000934{
FdaSilvaYYbde136c2016-03-18 19:02:17 +0100935 ENGINE *engine = NULL;
Rich Salz7e1b7482015-04-24 15:26:15 -0400936 EVP_PKEY *s_key = NULL, *s_dkey = NULL;
937 SSL_CONF_CTX *cctx = NULL;
Matt Caswell32ec4152015-03-27 23:01:51 +0000938 const SSL_METHOD *meth = TLS_server_method();
Rich Salz7e1b7482015-04-24 15:26:15 -0400939 SSL_EXCERT *exc = NULL;
940 STACK_OF(OPENSSL_STRING) *ssl_args = NULL;
941 STACK_OF(X509) *s_chain = NULL, *s_dchain = NULL;
942 STACK_OF(X509_CRL) *crls = NULL;
943 X509 *s_cert = NULL, *s_dcert = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +0000944 X509_VERIFY_PARAM *vpm = NULL;
FdaSilvaYYcc696292016-08-04 23:52:22 +0200945 const char *CApath = NULL, *CAfile = NULL, *chCApath = NULL, *chCAfile = NULL;
Dr. Stephen Henson37f3a3b2015-09-19 22:03:15 +0100946 char *dpassarg = NULL, *dpass = NULL, *inrand = NULL;
Rich Salz7e1b7482015-04-24 15:26:15 -0400947 char *passarg = NULL, *pass = NULL, *vfyCApath = NULL, *vfyCAfile = NULL;
Matt Caswella7f82a12015-05-15 09:42:08 +0100948 char *crl_file = NULL, *prog;
Richard Levitteab69ac02016-02-03 00:47:42 +0100949#ifdef AF_UNIX
Matt Caswell0f113f32015-01-22 03:40:55 +0000950 int unlink_unix_path = 0;
Dr. Stephen Henson9cd86ab2014-07-01 12:44:00 +0100951#endif
Rich Salza773b522016-02-13 22:33:56 -0500952 do_server_cb server_cb;
Rich Salz7e1b7482015-04-24 15:26:15 -0400953 int vpmtouched = 0, build_chain = 0, no_cache = 0, ext_cache = 0;
Dr. Stephen Henson37f3a3b2015-09-19 22:03:15 +0100954#ifndef OPENSSL_NO_DH
FdaSilvaYY54463e42016-08-03 22:49:25 +0200955 char *dhfile = NULL;
Dr. Stephen Henson37f3a3b2015-09-19 22:03:15 +0100956 int no_dhe = 0;
957#endif
Matt Caswell8caab742015-12-15 10:43:44 +0000958 int nocert = 0, ret = 1;
Matt Caswell2b6bcb72015-09-22 16:00:52 +0100959 int noCApath = 0, noCAfile = 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000960 int s_cert_format = FORMAT_PEM, s_key_format = FORMAT_PEM;
Matt Caswell0f113f32015-01-22 03:40:55 +0000961 int s_dcert_format = FORMAT_PEM, s_dkey_format = FORMAT_PEM;
Richard Levitteab69ac02016-02-03 00:47:42 +0100962 int rev = 0, naccept = -1, sdebug = 0;
963 int socket_family = AF_UNSPEC, socket_type = SOCK_STREAM;
Rich Salz7e1b7482015-04-24 15:26:15 -0400964 int state = 0, crl_format = FORMAT_PEM, crl_download = 0;
Richard Levitteab69ac02016-02-03 00:47:42 +0100965 char *host = NULL;
966 char *port = BUF_strdup(PORT);
Rich Salz7e1b7482015-04-24 15:26:15 -0400967 unsigned char *context = NULL;
968 OPTION_CHOICE o;
Matt Caswell0f113f32015-01-22 03:40:55 +0000969 EVP_PKEY *s_key2 = NULL;
970 X509 *s_cert2 = NULL;
971 tlsextctx tlsextcbp = { NULL, NULL, SSL_TLSEXT_ERR_ALERT_WARNING };
Dr. Stephen Henson287d0b92015-07-08 23:09:52 +0100972 const char *ssl_config = NULL;
Matt Caswelldad78fb2016-01-13 14:20:25 +0000973 int read_buf_len = 0;
Matt Caswelle481f9b2015-05-15 10:49:56 +0100974#ifndef OPENSSL_NO_NEXTPROTONEG
Matt Caswell0f113f32015-01-22 03:40:55 +0000975 const char *next_proto_neg_in = NULL;
976 tlsextnextprotoctx next_proto = { NULL, 0 };
Matt Caswelle481f9b2015-05-15 10:49:56 +0100977#endif
Matt Caswell0f113f32015-01-22 03:40:55 +0000978 const char *alpn_in = NULL;
979 tlsextalpnctx alpn_ctx = { NULL, 0 };
Nils Larschddac1972006-03-10 23:06:27 +0000980#ifndef OPENSSL_NO_PSK
Matt Caswell0f113f32015-01-22 03:40:55 +0000981 /* by default do not send a PSK identity hint */
FdaSilvaYYf2ff1432016-12-06 00:42:01 +0100982 char *psk_identity_hint = NULL;
FdaSilvaYY54463e42016-08-03 22:49:25 +0200983 char *p;
Nils Larschddac1972006-03-10 23:06:27 +0000984#endif
Ben Laurieedc032b2011-03-12 17:01:19 +0000985#ifndef OPENSSL_NO_SRP
Matt Caswell0f113f32015-01-22 03:40:55 +0000986 char *srpuserseed = NULL;
987 char *srp_verifier_file = NULL;
Ben Laurieedc032b2011-03-12 17:01:19 +0000988#endif
Matt Caswell4bbd4ba2016-07-07 11:05:31 +0100989 int min_version = 0, max_version = 0, prot_opt = 0, no_prot_opt = 0;
FdaSilvaYY54463e42016-08-03 22:49:25 +0200990 int s_server_verify = SSL_VERIFY_NONE;
991 int s_server_session_id_context = 1; /* anything will do */
992 const char *s_cert_file = TEST_CERT, *s_key_file = NULL, *s_chain_file = NULL;
993 const char *s_cert_file2 = TEST_CERT2, *s_key_file2 = NULL;
994 char *s_dcert_file = NULL, *s_dkey_file = NULL, *s_dchain_file = NULL;
Richard Levitte057c6762016-09-19 15:08:58 +0200995#ifndef OPENSSL_NO_OCSP
996 int s_tlsextstatus = 0;
997#endif
998 int no_resume_ephemeral = 0;
FdaSilvaYY54463e42016-08-03 22:49:25 +0200999 unsigned int split_send_fragment = 0, max_pipelines = 0;
1000 const char *s_serverinfo_file = NULL;
Peter Wu4bf73e92017-02-01 19:14:27 +01001001 const char *keylog_file = NULL;
Matt Caswell67466482017-02-23 16:54:11 +00001002 int max_early_data = -1;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001003
FdaSilvaYY54463e42016-08-03 22:49:25 +02001004 /* Init of few remaining global variables */
Matt Caswell0f113f32015-01-22 03:40:55 +00001005 local_argc = argc;
1006 local_argv = argv;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001007
FdaSilvaYY54463e42016-08-03 22:49:25 +02001008 ctx = ctx2 = NULL;
1009 s_nbio = s_nbio_test = 0;
1010 www = 0;
1011 bio_s_out = NULL;
1012 s_debug = 0;
1013 s_msg = 0;
1014 s_quiet = 0;
1015 s_brief = 0;
1016 async = 0;
1017
Matt Caswell0f113f32015-01-22 03:40:55 +00001018 cctx = SSL_CONF_CTX_new();
Rich Salz7e1b7482015-04-24 15:26:15 -04001019 vpm = X509_VERIFY_PARAM_new();
1020 if (cctx == NULL || vpm == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +00001021 goto end;
FdaSilvaYYd6073e22016-08-07 12:04:26 +02001022 SSL_CONF_CTX_set_flags(cctx,
1023 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CMDLINE);
Dr. Stephen Henson5d2e07f2012-11-17 14:42:22 +00001024
Rich Salz7e1b7482015-04-24 15:26:15 -04001025 prog = opt_init(argc, argv, s_server_options);
1026 while ((o = opt_next()) != OPT_EOF) {
Matt Caswell4bbd4ba2016-07-07 11:05:31 +01001027 if (IS_PROT_FLAG(o) && ++prot_opt > 1) {
1028 BIO_printf(bio_err, "Cannot supply multiple protocol flags\n");
1029 goto end;
1030 }
1031 if (IS_NO_PROT_FLAG(o))
1032 no_prot_opt++;
1033 if (prot_opt == 1 && no_prot_opt) {
FdaSilvaYYd6073e22016-08-07 12:04:26 +02001034 BIO_printf(bio_err,
1035 "Cannot supply both a protocol flag and '-no_<prot>'\n");
Matt Caswell4bbd4ba2016-07-07 11:05:31 +01001036 goto end;
1037 }
Rich Salz7e1b7482015-04-24 15:26:15 -04001038 switch (o) {
1039 case OPT_EOF:
1040 case OPT_ERR:
1041 opthelp:
1042 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
1043 goto end;
1044 case OPT_HELP:
1045 opt_help(s_server_options);
1046 ret = 0;
1047 goto end;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001048
Richard Levitteab69ac02016-02-03 00:47:42 +01001049 case OPT_4:
1050#ifdef AF_UNIX
1051 if (socket_family == AF_UNIX) {
1052 OPENSSL_free(host); host = NULL;
1053 OPENSSL_free(port); port = NULL;
1054 }
Geoff Thorpea9351322014-04-26 01:22:54 -04001055#endif
Richard Levitteab69ac02016-02-03 00:47:42 +01001056 socket_family = AF_INET;
1057 break;
1058 case OPT_6:
1059 if (1) {
1060#ifdef AF_INET6
1061#ifdef AF_UNIX
1062 if (socket_family == AF_UNIX) {
1063 OPENSSL_free(host); host = NULL;
1064 OPENSSL_free(port); port = NULL;
1065 }
1066#endif
1067 socket_family = AF_INET6;
1068 } else {
1069#endif
1070 BIO_printf(bio_err, "%s: IPv6 domain sockets unsupported\n", prog);
1071 goto end;
1072 }
1073 break;
1074 case OPT_PORT:
1075#ifdef AF_UNIX
1076 if (socket_family == AF_UNIX) {
1077 socket_family = AF_UNSPEC;
1078 }
1079#endif
1080 OPENSSL_free(port); port = NULL;
1081 OPENSSL_free(host); host = NULL;
1082 if (BIO_parse_hostserv(opt_arg(), NULL, &port, BIO_PARSE_PRIO_SERV) < 1) {
1083 BIO_printf(bio_err,
1084 "%s: -port argument malformed or ambiguous\n",
1085 port);
1086 goto end;
1087 }
1088 break;
1089 case OPT_ACCEPT:
1090#ifdef AF_UNIX
1091 if (socket_family == AF_UNIX) {
1092 socket_family = AF_UNSPEC;
1093 }
1094#endif
1095 OPENSSL_free(port); port = NULL;
1096 OPENSSL_free(host); host = NULL;
1097 if (BIO_parse_hostserv(opt_arg(), &host, &port, BIO_PARSE_PRIO_SERV) < 1) {
1098 BIO_printf(bio_err,
1099 "%s: -accept argument malformed or ambiguous\n",
1100 port);
1101 goto end;
1102 }
1103 break;
1104#ifdef AF_UNIX
1105 case OPT_UNIX:
1106 socket_family = AF_UNIX;
1107 OPENSSL_free(host); host = BUF_strdup(opt_arg());
1108 OPENSSL_free(port); port = NULL;
Rich Salz7e1b7482015-04-24 15:26:15 -04001109 break;
1110 case OPT_UNLINK:
Matt Caswell0f113f32015-01-22 03:40:55 +00001111 unlink_unix_path = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001112 break;
Richard Levitteab69ac02016-02-03 00:47:42 +01001113#endif
Rich Salz7e1b7482015-04-24 15:26:15 -04001114 case OPT_NACCEPT:
1115 naccept = atol(opt_arg());
1116 break;
1117 case OPT_VERIFY:
Matt Caswell0f113f32015-01-22 03:40:55 +00001118 s_server_verify = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE;
FdaSilvaYYacc00492016-08-01 21:30:57 +02001119 verify_args.depth = atoi(opt_arg());
Matt Caswell0f113f32015-01-22 03:40:55 +00001120 if (!s_quiet)
FdaSilvaYYacc00492016-08-01 21:30:57 +02001121 BIO_printf(bio_err, "verify depth is %d\n", verify_args.depth);
Rich Salz7e1b7482015-04-24 15:26:15 -04001122 break;
1123 case OPT_UPPER_V_VERIFY:
Matt Caswell0f113f32015-01-22 03:40:55 +00001124 s_server_verify =
1125 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
1126 SSL_VERIFY_CLIENT_ONCE;
FdaSilvaYYacc00492016-08-01 21:30:57 +02001127 verify_args.depth = atoi(opt_arg());
Matt Caswell0f113f32015-01-22 03:40:55 +00001128 if (!s_quiet)
1129 BIO_printf(bio_err,
1130 "verify depth is %d, must return a certificate\n",
FdaSilvaYYacc00492016-08-01 21:30:57 +02001131 verify_args.depth);
Rich Salz7e1b7482015-04-24 15:26:15 -04001132 break;
1133 case OPT_CONTEXT:
1134 context = (unsigned char *)opt_arg();
1135 break;
1136 case OPT_CERT:
1137 s_cert_file = opt_arg();
1138 break;
Dmitry Belyavskiya7c04f22017-02-21 14:22:55 +03001139 case OPT_NAMEOPT:
1140 if (!set_nameopt(opt_arg()))
1141 goto end;
1142 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001143 case OPT_CRL:
1144 crl_file = opt_arg();
1145 break;
1146 case OPT_CRL_DOWNLOAD:
Matt Caswell0f113f32015-01-22 03:40:55 +00001147 crl_download = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001148 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001149 case OPT_SERVERINFO:
1150 s_serverinfo_file = opt_arg();
1151 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001152 case OPT_CERTFORM:
1153 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &s_cert_format))
1154 goto opthelp;
1155 break;
1156 case OPT_KEY:
1157 s_key_file = opt_arg();
1158 break;
1159 case OPT_KEYFORM:
1160 if (!opt_format(opt_arg(), OPT_FMT_ANY, &s_key_format))
1161 goto opthelp;
1162 break;
1163 case OPT_PASS:
1164 passarg = opt_arg();
1165 break;
1166 case OPT_CERT_CHAIN:
1167 s_chain_file = opt_arg();
1168 break;
1169 case OPT_DHPARAM:
Dr. Stephen Henson37f3a3b2015-09-19 22:03:15 +01001170#ifndef OPENSSL_NO_DH
Rich Salz7e1b7482015-04-24 15:26:15 -04001171 dhfile = opt_arg();
Dr. Stephen Henson37f3a3b2015-09-19 22:03:15 +01001172#endif
Rich Salz7e1b7482015-04-24 15:26:15 -04001173 break;
1174 case OPT_DCERTFORM:
1175 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &s_dcert_format))
1176 goto opthelp;
1177 break;
1178 case OPT_DCERT:
1179 s_dcert_file = opt_arg();
1180 break;
1181 case OPT_DKEYFORM:
1182 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &s_dkey_format))
1183 goto opthelp;
1184 break;
1185 case OPT_DPASS:
1186 dpassarg = opt_arg();
1187 break;
1188 case OPT_DKEY:
1189 s_dkey_file = opt_arg();
1190 break;
1191 case OPT_DCERT_CHAIN:
1192 s_dchain_file = opt_arg();
1193 break;
1194 case OPT_NOCERT:
Matt Caswell0f113f32015-01-22 03:40:55 +00001195 nocert = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001196 break;
1197 case OPT_CAPATH:
1198 CApath = opt_arg();
1199 break;
Matt Caswell2b6bcb72015-09-22 16:00:52 +01001200 case OPT_NOCAPATH:
1201 noCApath = 1;
1202 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001203 case OPT_CHAINCAPATH:
1204 chCApath = opt_arg();
1205 break;
1206 case OPT_VERIFYCAPATH:
1207 vfyCApath = opt_arg();
1208 break;
1209 case OPT_NO_CACHE:
Matt Caswell0f113f32015-01-22 03:40:55 +00001210 no_cache = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001211 break;
1212 case OPT_EXT_CACHE:
Matt Caswell0f113f32015-01-22 03:40:55 +00001213 ext_cache = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001214 break;
1215 case OPT_CRLFORM:
1216 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &crl_format))
1217 goto opthelp;
1218 break;
1219 case OPT_S_CASES:
1220 if (ssl_args == NULL)
1221 ssl_args = sk_OPENSSL_STRING_new_null();
1222 if (ssl_args == NULL
1223 || !sk_OPENSSL_STRING_push(ssl_args, opt_flag())
1224 || !sk_OPENSSL_STRING_push(ssl_args, opt_arg())) {
1225 BIO_printf(bio_err, "%s: Memory allocation failure\n", prog);
1226 goto end;
1227 }
1228 break;
1229 case OPT_V_CASES:
1230 if (!opt_verify(o, vpm))
1231 goto end;
1232 vpmtouched++;
1233 break;
1234 case OPT_X_CASES:
1235 if (!args_excert(o, &exc))
1236 goto end;
1237 break;
1238 case OPT_VERIFY_RET_ERROR:
FdaSilvaYYacc00492016-08-01 21:30:57 +02001239 verify_args.return_error = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001240 break;
1241 case OPT_VERIFY_QUIET:
FdaSilvaYYacc00492016-08-01 21:30:57 +02001242 verify_args.quiet = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001243 break;
1244 case OPT_BUILD_CHAIN:
Matt Caswell0f113f32015-01-22 03:40:55 +00001245 build_chain = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001246 break;
1247 case OPT_CAFILE:
1248 CAfile = opt_arg();
1249 break;
Matt Caswell2b6bcb72015-09-22 16:00:52 +01001250 case OPT_NOCAFILE:
1251 noCAfile = 1;
1252 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001253 case OPT_CHAINCAFILE:
1254 chCAfile = opt_arg();
1255 break;
1256 case OPT_VERIFYCAFILE:
1257 vfyCAfile = opt_arg();
1258 break;
1259 case OPT_NBIO:
Matt Caswell0f113f32015-01-22 03:40:55 +00001260 s_nbio = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001261 break;
1262 case OPT_NBIO_TEST:
1263 s_nbio = s_nbio_test = 1;
1264 break;
1265 case OPT_IGN_EOF:
Matt Caswell0f113f32015-01-22 03:40:55 +00001266 s_ign_eof = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001267 break;
1268 case OPT_NO_IGN_EOF:
Matt Caswell0f113f32015-01-22 03:40:55 +00001269 s_ign_eof = 0;
Rich Salz7e1b7482015-04-24 15:26:15 -04001270 break;
1271 case OPT_DEBUG:
Matt Caswell0f113f32015-01-22 03:40:55 +00001272 s_debug = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001273 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001274 case OPT_TLSEXTDEBUG:
Matt Caswell0f113f32015-01-22 03:40:55 +00001275 s_tlsextdebug = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001276 break;
1277 case OPT_STATUS:
Richard Levitte057c6762016-09-19 15:08:58 +02001278#ifndef OPENSSL_NO_OCSP
Matt Caswell0f113f32015-01-22 03:40:55 +00001279 s_tlsextstatus = 1;
Richard Levitte057c6762016-09-19 15:08:58 +02001280#endif
Rich Salz7e1b7482015-04-24 15:26:15 -04001281 break;
1282 case OPT_STATUS_VERBOSE:
Richard Levitte057c6762016-09-19 15:08:58 +02001283#ifndef OPENSSL_NO_OCSP
Rich Salz7e1b7482015-04-24 15:26:15 -04001284 s_tlsextstatus = tlscstatp.verbose = 1;
Richard Levitte057c6762016-09-19 15:08:58 +02001285#endif
Rich Salz7e1b7482015-04-24 15:26:15 -04001286 break;
1287 case OPT_STATUS_TIMEOUT:
Richard Levitte057c6762016-09-19 15:08:58 +02001288#ifndef OPENSSL_NO_OCSP
Matt Caswell0f113f32015-01-22 03:40:55 +00001289 s_tlsextstatus = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001290 tlscstatp.timeout = atoi(opt_arg());
Richard Levitte057c6762016-09-19 15:08:58 +02001291#endif
Rich Salz7e1b7482015-04-24 15:26:15 -04001292 break;
1293 case OPT_STATUS_URL:
Matt Caswell3e41ac32016-03-21 16:54:53 +00001294#ifndef OPENSSL_NO_OCSP
Matt Caswell0f113f32015-01-22 03:40:55 +00001295 s_tlsextstatus = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001296 if (!OCSP_parse_url(opt_arg(),
Matt Caswell0f113f32015-01-22 03:40:55 +00001297 &tlscstatp.host,
1298 &tlscstatp.port,
1299 &tlscstatp.path, &tlscstatp.use_ssl)) {
1300 BIO_printf(bio_err, "Error parsing URL\n");
Rich Salz7e1b7482015-04-24 15:26:15 -04001301 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +00001302 }
Matt Caswell3e41ac32016-03-21 16:54:53 +00001303#endif
Rich Salz7e1b7482015-04-24 15:26:15 -04001304 break;
Matt Caswellacf65ae2016-11-15 14:22:29 +00001305 case OPT_STATUS_FILE:
1306#ifndef OPENSSL_NO_OCSP
1307 s_tlsextstatus = 1;
1308 tlscstatp.respin = opt_arg();
1309#endif
1310 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001311 case OPT_MSG:
Matt Caswell0f113f32015-01-22 03:40:55 +00001312 s_msg = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001313 break;
1314 case OPT_MSGFILE:
1315 bio_s_msg = BIO_new_file(opt_arg(), "w");
1316 break;
Rich Salz9c3bcfa2015-05-15 13:50:38 -04001317 case OPT_TRACE:
Dr. Stephen Henson93ab9e42012-06-15 12:46:09 +00001318#ifndef OPENSSL_NO_SSL_TRACE
Matt Caswell0f113f32015-01-22 03:40:55 +00001319 s_msg = 2;
Dr. Stephen Henson93ab9e42012-06-15 12:46:09 +00001320#endif
J Mohan Rao Arisankala1c03c812016-02-29 22:23:18 +05301321 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001322 case OPT_SECURITY_DEBUG:
Matt Caswell0f113f32015-01-22 03:40:55 +00001323 sdebug = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001324 break;
1325 case OPT_SECURITY_DEBUG_VERBOSE:
Matt Caswell0f113f32015-01-22 03:40:55 +00001326 sdebug = 2;
Rich Salz7e1b7482015-04-24 15:26:15 -04001327 break;
1328 case OPT_STATE:
Matt Caswell0f113f32015-01-22 03:40:55 +00001329 state = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001330 break;
1331 case OPT_CRLF:
Matt Caswell0f113f32015-01-22 03:40:55 +00001332 s_crlf = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001333 break;
1334 case OPT_QUIET:
Matt Caswell0f113f32015-01-22 03:40:55 +00001335 s_quiet = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001336 break;
1337 case OPT_BRIEF:
FdaSilvaYYacc00492016-08-01 21:30:57 +02001338 s_quiet = s_brief = verify_args.quiet = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001339 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001340 case OPT_NO_DHE:
Dr. Stephen Henson37f3a3b2015-09-19 22:03:15 +01001341#ifndef OPENSSL_NO_DH
Matt Caswell0f113f32015-01-22 03:40:55 +00001342 no_dhe = 1;
Dr. Stephen Henson37f3a3b2015-09-19 22:03:15 +01001343#endif
Rich Salz7e1b7482015-04-24 15:26:15 -04001344 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001345 case OPT_NO_RESUME_EPHEMERAL:
Matt Caswell0f113f32015-01-22 03:40:55 +00001346 no_resume_ephemeral = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001347 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001348 case OPT_PSK_HINT:
Viktor Dukhovni6b01bed2016-01-18 13:10:21 -05001349#ifndef OPENSSL_NO_PSK
Rich Salz7e1b7482015-04-24 15:26:15 -04001350 psk_identity_hint = opt_arg();
Viktor Dukhovni6b01bed2016-01-18 13:10:21 -05001351#endif
Rich Salz7e1b7482015-04-24 15:26:15 -04001352 break;
1353 case OPT_PSK:
Viktor Dukhovni6b01bed2016-01-18 13:10:21 -05001354#ifndef OPENSSL_NO_PSK
Rich Salz7e1b7482015-04-24 15:26:15 -04001355 for (p = psk_key = opt_arg(); *p; p++) {
Richard Levitte18295f02016-02-14 13:02:15 +01001356 if (isxdigit(_UC(*p)))
Matt Caswell0f113f32015-01-22 03:40:55 +00001357 continue;
1358 BIO_printf(bio_err, "Not a hex number '%s'\n", *argv);
Rich Salz7e1b7482015-04-24 15:26:15 -04001359 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +00001360 }
Nils Larschddac1972006-03-10 23:06:27 +00001361#endif
Viktor Dukhovni6b01bed2016-01-18 13:10:21 -05001362 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001363 case OPT_SRPVFILE:
Viktor Dukhovni6b01bed2016-01-18 13:10:21 -05001364#ifndef OPENSSL_NO_SRP
Rich Salz7e1b7482015-04-24 15:26:15 -04001365 srp_verifier_file = opt_arg();
Kurt Roeckx0d5301a2016-02-02 23:58:49 +01001366 if (min_version < TLS1_VERSION)
1367 min_version = TLS1_VERSION;
Viktor Dukhovni6b01bed2016-01-18 13:10:21 -05001368#endif
Rich Salz7e1b7482015-04-24 15:26:15 -04001369 break;
1370 case OPT_SRPUSERSEED:
Viktor Dukhovni6b01bed2016-01-18 13:10:21 -05001371#ifndef OPENSSL_NO_SRP
Rich Salz7e1b7482015-04-24 15:26:15 -04001372 srpuserseed = opt_arg();
Kurt Roeckx0d5301a2016-02-02 23:58:49 +01001373 if (min_version < TLS1_VERSION)
1374 min_version = TLS1_VERSION;
Ben Laurieedc032b2011-03-12 17:01:19 +00001375#endif
Viktor Dukhovni6b01bed2016-01-18 13:10:21 -05001376 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001377 case OPT_REV:
Matt Caswell0f113f32015-01-22 03:40:55 +00001378 rev = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001379 break;
1380 case OPT_WWW:
Matt Caswell0f113f32015-01-22 03:40:55 +00001381 www = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -04001382 break;
1383 case OPT_UPPER_WWW:
Matt Caswell0f113f32015-01-22 03:40:55 +00001384 www = 2;
Rich Salz7e1b7482015-04-24 15:26:15 -04001385 break;
1386 case OPT_HTTP:
Matt Caswell0f113f32015-01-22 03:40:55 +00001387 www = 3;
Rich Salz7e1b7482015-04-24 15:26:15 -04001388 break;
Dr. Stephen Henson287d0b92015-07-08 23:09:52 +01001389 case OPT_SSL_CONFIG:
1390 ssl_config = opt_arg();
1391 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001392 case OPT_SSL3:
Kurt Roeckx0d5301a2016-02-02 23:58:49 +01001393 min_version = SSL3_VERSION;
1394 max_version = SSL3_VERSION;
Rich Salz9c3bcfa2015-05-15 13:50:38 -04001395 break;
Matt Caswell582a17d2016-10-21 17:39:33 +01001396 case OPT_TLS1_3:
1397 min_version = TLS1_3_VERSION;
1398 max_version = TLS1_3_VERSION;
1399 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001400 case OPT_TLS1_2:
Kurt Roeckx0d5301a2016-02-02 23:58:49 +01001401 min_version = TLS1_2_VERSION;
1402 max_version = TLS1_2_VERSION;
Rich Salz7e1b7482015-04-24 15:26:15 -04001403 break;
1404 case OPT_TLS1_1:
Kurt Roeckx0d5301a2016-02-02 23:58:49 +01001405 min_version = TLS1_1_VERSION;
1406 max_version = TLS1_1_VERSION;
Rich Salz7e1b7482015-04-24 15:26:15 -04001407 break;
1408 case OPT_TLS1:
Kurt Roeckx0d5301a2016-02-02 23:58:49 +01001409 min_version = TLS1_VERSION;
1410 max_version = TLS1_VERSION;
Rich Salz7e1b7482015-04-24 15:26:15 -04001411 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001412 case OPT_DTLS:
Viktor Dukhovni6b01bed2016-01-18 13:10:21 -05001413#ifndef OPENSSL_NO_DTLS
Matt Caswell4407d072015-05-06 11:17:07 +01001414 meth = DTLS_server_method();
Matt Caswell0f113f32015-01-22 03:40:55 +00001415 socket_type = SOCK_DGRAM;
Viktor Dukhovni6b01bed2016-01-18 13:10:21 -05001416#endif
Rich Salz7e1b7482015-04-24 15:26:15 -04001417 break;
1418 case OPT_DTLS1:
Kurt Roeckx0d5301a2016-02-02 23:58:49 +01001419#ifndef OPENSSL_NO_DTLS
1420 meth = DTLS_server_method();
1421 min_version = DTLS1_VERSION;
1422 max_version = DTLS1_VERSION;
Matt Caswell0f113f32015-01-22 03:40:55 +00001423 socket_type = SOCK_DGRAM;
Viktor Dukhovni6b01bed2016-01-18 13:10:21 -05001424#endif
Rich Salz7e1b7482015-04-24 15:26:15 -04001425 break;
1426 case OPT_DTLS1_2:
Kurt Roeckx0d5301a2016-02-02 23:58:49 +01001427#ifndef OPENSSL_NO_DTLS
1428 meth = DTLS_server_method();
1429 min_version = DTLS1_2_VERSION;
1430 max_version = DTLS1_2_VERSION;
Matt Caswell0f113f32015-01-22 03:40:55 +00001431 socket_type = SOCK_DGRAM;
Viktor Dukhovni6b01bed2016-01-18 13:10:21 -05001432#endif
Rich Salz7e1b7482015-04-24 15:26:15 -04001433 break;
1434 case OPT_TIMEOUT:
Viktor Dukhovni6b01bed2016-01-18 13:10:21 -05001435#ifndef OPENSSL_NO_DTLS
Matt Caswell0f113f32015-01-22 03:40:55 +00001436 enable_timeouts = 1;
Viktor Dukhovni6b01bed2016-01-18 13:10:21 -05001437#endif
Rich Salz7e1b7482015-04-24 15:26:15 -04001438 break;
1439 case OPT_MTU:
Viktor Dukhovni6b01bed2016-01-18 13:10:21 -05001440#ifndef OPENSSL_NO_DTLS
Rich Salz7e1b7482015-04-24 15:26:15 -04001441 socket_mtu = atol(opt_arg());
Viktor Dukhovni6b01bed2016-01-18 13:10:21 -05001442#endif
Rich Salz7e1b7482015-04-24 15:26:15 -04001443 break;
Matt Caswellfd4e98e2015-04-09 10:01:05 +01001444 case OPT_LISTEN:
Viktor Dukhovni6b01bed2016-01-18 13:10:21 -05001445#ifndef OPENSSL_NO_DTLS
Matt Caswellfd4e98e2015-04-09 10:01:05 +01001446 dtlslisten = 1;
Ben Laurie36d16f82005-04-26 16:02:40 +00001447#endif
Viktor Dukhovni6b01bed2016-01-18 13:10:21 -05001448 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001449 case OPT_ID_PREFIX:
1450 session_id_prefix = opt_arg();
1451 break;
1452 case OPT_ENGINE:
FdaSilvaYYbde136c2016-03-18 19:02:17 +01001453 engine = setup_engine(opt_arg(), 1);
Rich Salz7e1b7482015-04-24 15:26:15 -04001454 break;
1455 case OPT_RAND:
1456 inrand = opt_arg();
1457 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001458 case OPT_SERVERNAME:
1459 tlsextcbp.servername = opt_arg();
1460 break;
1461 case OPT_SERVERNAME_FATAL:
Matt Caswell0f113f32015-01-22 03:40:55 +00001462 tlsextcbp.extension_error = SSL_TLSEXT_ERR_ALERT_FATAL;
Rich Salz7e1b7482015-04-24 15:26:15 -04001463 break;
1464 case OPT_CERT2:
1465 s_cert_file2 = opt_arg();
1466 break;
1467 case OPT_KEY2:
1468 s_key_file2 = opt_arg();
1469 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001470 case OPT_NEXTPROTONEG:
Rich Salz9c3bcfa2015-05-15 13:50:38 -04001471# ifndef OPENSSL_NO_NEXTPROTONEG
Rich Salz7e1b7482015-04-24 15:26:15 -04001472 next_proto_neg_in = opt_arg();
Matt Caswelle481f9b2015-05-15 10:49:56 +01001473#endif
Rich Salz9c3bcfa2015-05-15 13:50:38 -04001474 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001475 case OPT_ALPN:
1476 alpn_in = opt_arg();
1477 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001478 case OPT_SRTP_PROFILES:
J Mohan Rao Arisankalad6316022016-02-27 09:05:51 +05301479#ifndef OPENSSL_NO_SRTP
Rich Salz7e1b7482015-04-24 15:26:15 -04001480 srtp_profiles = opt_arg();
J Mohan Rao Arisankalab07c7032016-02-27 08:50:07 +05301481#endif
J Mohan Rao Arisankalad6316022016-02-27 09:05:51 +05301482 break;
Rich Salz7e1b7482015-04-24 15:26:15 -04001483 case OPT_KEYMATEXPORT:
1484 keymatexportlabel = opt_arg();
1485 break;
1486 case OPT_KEYMATEXPORTLEN:
1487 keymatexportlen = atoi(opt_arg());
Matt Caswell0f113f32015-01-22 03:40:55 +00001488 break;
Matt Caswell7e25dd62015-02-13 23:33:12 +00001489 case OPT_ASYNC:
1490 async = 1;
1491 break;
Matt Caswell032c6d22015-09-22 11:23:33 +01001492 case OPT_SPLIT_SEND_FRAG:
1493 split_send_fragment = atoi(opt_arg());
1494 if (split_send_fragment == 0) {
Matt Caswelle2d51832016-03-07 10:06:42 +00001495 /*
1496 * Not allowed - set to a deliberately bad value so we get an
1497 * error message below
1498 */
1499 split_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH + 1;
Matt Caswell032c6d22015-09-22 11:23:33 +01001500 }
1501 break;
1502 case OPT_MAX_PIPELINES:
1503 max_pipelines = atoi(opt_arg());
1504 break;
Matt Caswelldad78fb2016-01-13 14:20:25 +00001505 case OPT_READ_BUF:
1506 read_buf_len = atoi(opt_arg());
1507 break;
Peter Wu4bf73e92017-02-01 19:14:27 +01001508 case OPT_KEYLOG_FILE:
1509 keylog_file = opt_arg();
1510 break;
Matt Caswell048b1892017-02-17 17:01:16 +00001511 case OPT_MAX_EARLY:
1512 max_early_data = atoi(opt_arg());
Matt Caswell67466482017-02-23 16:54:11 +00001513 if (max_early_data < 0) {
1514 BIO_printf(bio_err, "Invalid value for max_early_data\n");
1515 goto end;
1516 }
Matt Caswell048b1892017-02-17 17:01:16 +00001517 break;
Matt Caswelle0655182017-02-22 15:24:11 +00001518 case OPT_EARLY_DATA:
1519 early_data = 1;
1520 break;
Matt Caswell0f113f32015-01-22 03:40:55 +00001521 }
Matt Caswell0f113f32015-01-22 03:40:55 +00001522 }
Rich Salz7e1b7482015-04-24 15:26:15 -04001523 argc = opt_num_rest();
1524 argv = opt_rest();
1525
Kurt Roeckxa5ecdc62015-12-12 11:12:22 +01001526#ifndef OPENSSL_NO_DTLS
Matt Caswell0f113f32015-01-22 03:40:55 +00001527 if (www && socket_type == SOCK_DGRAM) {
1528 BIO_printf(bio_err, "Can't use -HTTP, -www or -WWW with DTLS\n");
1529 goto end;
1530 }
Matt Caswellfd4e98e2015-04-09 10:01:05 +01001531
1532 if (dtlslisten && socket_type != SOCK_DGRAM) {
1533 BIO_printf(bio_err, "Can only use -listen with DTLS\n");
1534 goto end;
1535 }
Dr. Stephen Henson199772e2014-07-15 12:22:49 +01001536#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001537
Richard Levitteab69ac02016-02-03 00:47:42 +01001538#ifdef AF_UNIX
1539 if (socket_family == AF_UNIX && socket_type != SOCK_STREAM) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001540 BIO_printf(bio_err,
1541 "Can't use unix sockets and datagrams together\n");
1542 goto end;
1543 }
Richard Levitteab69ac02016-02-03 00:47:42 +01001544#endif
Dr. Stephen Henson2900fc82008-11-30 22:01:31 +00001545
Matt Caswell032c6d22015-09-22 11:23:33 +01001546 if (split_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1547 BIO_printf(bio_err, "Bad split send fragment size\n");
1548 goto end;
1549 }
1550
1551 if (max_pipelines > SSL_MAX_PIPELINES) {
1552 BIO_printf(bio_err, "Bad max pipelines value\n");
1553 goto end;
1554 }
1555
Rich Salz7e1b7482015-04-24 15:26:15 -04001556 if (!app_passwd(passarg, dpassarg, &pass, &dpass)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001557 BIO_printf(bio_err, "Error getting password\n");
1558 goto end;
1559 }
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +00001560
Matt Caswell0f113f32015-01-22 03:40:55 +00001561 if (s_key_file == NULL)
1562 s_key_file = s_cert_file;
Matt Caswelle481f9b2015-05-15 10:49:56 +01001563
Matt Caswell0f113f32015-01-22 03:40:55 +00001564 if (s_key_file2 == NULL)
1565 s_key_file2 = s_cert_file2;
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +00001566
Rich Salz7e1b7482015-04-24 15:26:15 -04001567 if (!load_excert(&exc))
Matt Caswell0f113f32015-01-22 03:40:55 +00001568 goto end;
Dr. Stephen Henson18d71582012-06-29 14:24:42 +00001569
Matt Caswell0f113f32015-01-22 03:40:55 +00001570 if (nocert == 0) {
FdaSilvaYYbde136c2016-03-18 19:02:17 +01001571 s_key = load_key(s_key_file, s_key_format, 0, pass, engine,
Matt Caswell0f113f32015-01-22 03:40:55 +00001572 "server certificate private key file");
1573 if (!s_key) {
1574 ERR_print_errors(bio_err);
1575 goto end;
1576 }
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +00001577
Rich Salz7e1b7482015-04-24 15:26:15 -04001578 s_cert = load_cert(s_cert_file, s_cert_format,
Rich Salza773b522016-02-13 22:33:56 -05001579 "server certificate file");
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +00001580
Matt Caswell0f113f32015-01-22 03:40:55 +00001581 if (!s_cert) {
1582 ERR_print_errors(bio_err);
1583 goto end;
1584 }
1585 if (s_chain_file) {
Rich Salza773b522016-02-13 22:33:56 -05001586 if (!load_certs(s_chain_file, &s_chain, FORMAT_PEM, NULL,
Viktor Dukhovni0996dc52016-01-16 00:08:38 -05001587 "server certificate chain"))
Matt Caswell0f113f32015-01-22 03:40:55 +00001588 goto end;
1589 }
Matt Caswelle481f9b2015-05-15 10:49:56 +01001590
Matt Caswell0f113f32015-01-22 03:40:55 +00001591 if (tlsextcbp.servername) {
FdaSilvaYYbde136c2016-03-18 19:02:17 +01001592 s_key2 = load_key(s_key_file2, s_key_format, 0, pass, engine,
Matt Caswell0f113f32015-01-22 03:40:55 +00001593 "second server certificate private key file");
1594 if (!s_key2) {
1595 ERR_print_errors(bio_err);
1596 goto end;
1597 }
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +00001598
Rich Salz7e1b7482015-04-24 15:26:15 -04001599 s_cert2 = load_cert(s_cert_file2, s_cert_format,
Rich Salza773b522016-02-13 22:33:56 -05001600 "second server certificate file");
Matt Caswell0f113f32015-01-22 03:40:55 +00001601
1602 if (!s_cert2) {
1603 ERR_print_errors(bio_err);
1604 goto end;
1605 }
1606 }
Matt Caswell0f113f32015-01-22 03:40:55 +00001607 }
Matt Caswelle481f9b2015-05-15 10:49:56 +01001608#if !defined(OPENSSL_NO_NEXTPROTONEG)
Matt Caswell0f113f32015-01-22 03:40:55 +00001609 if (next_proto_neg_in) {
FdaSilvaYYf2ff1432016-12-06 00:42:01 +01001610 next_proto.data = next_protos_parse(&next_proto.len, next_proto_neg_in);
Matt Caswell0f113f32015-01-22 03:40:55 +00001611 if (next_proto.data == NULL)
1612 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +00001613 }
Matt Caswelle481f9b2015-05-15 10:49:56 +01001614#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00001615 alpn_ctx.data = NULL;
1616 if (alpn_in) {
FdaSilvaYYf2ff1432016-12-06 00:42:01 +01001617 alpn_ctx.data = next_protos_parse(&alpn_ctx.len, alpn_in);
Matt Caswell0f113f32015-01-22 03:40:55 +00001618 if (alpn_ctx.data == NULL)
1619 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +00001620 }
Dr. Stephen Hensondd251652012-07-03 16:37:50 +00001621
Matt Caswell0f113f32015-01-22 03:40:55 +00001622 if (crl_file) {
1623 X509_CRL *crl;
1624 crl = load_crl(crl_file, crl_format);
1625 if (!crl) {
1626 BIO_puts(bio_err, "Error loading CRL\n");
1627 ERR_print_errors(bio_err);
1628 goto end;
1629 }
1630 crls = sk_X509_CRL_new_null();
1631 if (!crls || !sk_X509_CRL_push(crls, crl)) {
1632 BIO_puts(bio_err, "Error adding CRL\n");
1633 ERR_print_errors(bio_err);
1634 X509_CRL_free(crl);
1635 goto end;
1636 }
1637 }
Dr. Stephen Hensonfdb78f32012-12-02 16:16:28 +00001638
Matt Caswell0f113f32015-01-22 03:40:55 +00001639 if (s_dcert_file) {
Bodo Möllered3883d2006-01-02 23:14:37 +00001640
Matt Caswell0f113f32015-01-22 03:40:55 +00001641 if (s_dkey_file == NULL)
1642 s_dkey_file = s_dcert_file;
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +00001643
Rich Salz7e1b7482015-04-24 15:26:15 -04001644 s_dkey = load_key(s_dkey_file, s_dkey_format,
FdaSilvaYYbde136c2016-03-18 19:02:17 +01001645 0, dpass, engine, "second certificate private key file");
Matt Caswell0f113f32015-01-22 03:40:55 +00001646 if (!s_dkey) {
1647 ERR_print_errors(bio_err);
1648 goto end;
1649 }
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +00001650
Rich Salz7e1b7482015-04-24 15:26:15 -04001651 s_dcert = load_cert(s_dcert_file, s_dcert_format,
Rich Salza773b522016-02-13 22:33:56 -05001652 "second server certificate file");
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +00001653
Matt Caswell0f113f32015-01-22 03:40:55 +00001654 if (!s_dcert) {
1655 ERR_print_errors(bio_err);
1656 goto end;
1657 }
1658 if (s_dchain_file) {
Rich Salza773b522016-02-13 22:33:56 -05001659 if (!load_certs(s_dchain_file, &s_dchain, FORMAT_PEM, NULL,
Viktor Dukhovni0996dc52016-01-16 00:08:38 -05001660 "second server certificate chain"))
Matt Caswell0f113f32015-01-22 03:40:55 +00001661 goto end;
1662 }
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +00001663
Matt Caswell0f113f32015-01-22 03:40:55 +00001664 }
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +00001665
Rich Salz7e1b7482015-04-24 15:26:15 -04001666 if (!app_RAND_load_file(NULL, 1) && inrand == NULL
Matt Caswell0f113f32015-01-22 03:40:55 +00001667 && !RAND_status()) {
1668 BIO_printf(bio_err,
1669 "warning, not much extra random data, consider using the -rand option\n");
1670 }
1671 if (inrand != NULL)
1672 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
1673 app_RAND_load_files(inrand));
Dr. Stephen Henson826a42a2004-11-16 17:30:59 +00001674
Matt Caswell0f113f32015-01-22 03:40:55 +00001675 if (bio_s_out == NULL) {
1676 if (s_quiet && !s_debug) {
1677 bio_s_out = BIO_new(BIO_s_null());
1678 if (s_msg && !bio_s_msg)
Richard Levittea60994d2015-09-06 12:20:12 +02001679 bio_s_msg = dup_bio_out(FORMAT_TEXT);
Matt Caswell0f113f32015-01-22 03:40:55 +00001680 } else {
1681 if (bio_s_out == NULL)
Richard Levittea60994d2015-09-06 12:20:12 +02001682 bio_s_out = dup_bio_out(FORMAT_TEXT);
Matt Caswell0f113f32015-01-22 03:40:55 +00001683 }
1684 }
Rich Salz10bf4fc2015-03-10 19:09:27 -04001685#if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
Matt Caswell0f113f32015-01-22 03:40:55 +00001686 if (nocert)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001687#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00001688 {
1689 s_cert_file = NULL;
1690 s_key_file = NULL;
1691 s_dcert_file = NULL;
1692 s_dkey_file = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +00001693 s_cert_file2 = NULL;
1694 s_key_file2 = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +00001695 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001696
Matt Caswell0f113f32015-01-22 03:40:55 +00001697 ctx = SSL_CTX_new(meth);
Matt Caswell0f113f32015-01-22 03:40:55 +00001698 if (ctx == NULL) {
1699 ERR_print_errors(bio_err);
1700 goto end;
1701 }
A J Mohan Rao32eabe32016-02-09 10:55:42 -05001702 if (sdebug)
1703 ssl_ctx_security_debug(ctx, sdebug);
Dr. Stephen Henson287d0b92015-07-08 23:09:52 +01001704 if (ssl_config) {
1705 if (SSL_CTX_config(ctx, ssl_config) == 0) {
1706 BIO_printf(bio_err, "Error using configuration \"%s\"\n",
1707 ssl_config);
FdaSilvaYYd6073e22016-08-07 12:04:26 +02001708 ERR_print_errors(bio_err);
1709 goto end;
Dr. Stephen Henson287d0b92015-07-08 23:09:52 +01001710 }
1711 }
Kurt Roeckx0d5301a2016-02-02 23:58:49 +01001712 if (SSL_CTX_set_min_proto_version(ctx, min_version) == 0)
1713 goto end;
1714 if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
1715 goto end;
Dr. Stephen Henson287d0b92015-07-08 23:09:52 +01001716
Matt Caswell0f113f32015-01-22 03:40:55 +00001717 if (session_id_prefix) {
1718 if (strlen(session_id_prefix) >= 32)
1719 BIO_printf(bio_err,
1720 "warning: id_prefix is too long, only one new session will be possible\n");
1721 if (!SSL_CTX_set_generate_session_id(ctx, generate_session_id)) {
1722 BIO_printf(bio_err, "error setting 'id_prefix'\n");
1723 ERR_print_errors(bio_err);
1724 goto end;
1725 }
1726 BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
1727 }
1728 SSL_CTX_set_quiet_shutdown(ctx, 1);
Matt Caswell0f113f32015-01-22 03:40:55 +00001729 if (exc)
1730 ssl_ctx_set_excert(ctx, exc);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001731
Matt Caswell0f113f32015-01-22 03:40:55 +00001732 if (state)
1733 SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
1734 if (no_cache)
1735 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
1736 else if (ext_cache)
1737 init_session_cache_ctx(ctx);
1738 else
1739 SSL_CTX_sess_set_cache_size(ctx, 128);
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00001740
Matt Caswell252d6d32015-07-22 17:50:51 +01001741 if (async) {
Matt Caswell7e25dd62015-02-13 23:33:12 +00001742 SSL_CTX_set_mode(ctx, SSL_MODE_ASYNC);
Matt Caswell252d6d32015-07-22 17:50:51 +01001743 }
Matt Caswell032c6d22015-09-22 11:23:33 +01001744 if (split_send_fragment > 0) {
1745 SSL_CTX_set_split_send_fragment(ctx, split_send_fragment);
1746 }
1747 if (max_pipelines > 0) {
1748 SSL_CTX_set_max_pipelines(ctx, max_pipelines);
1749 }
Matt Caswell7e25dd62015-02-13 23:33:12 +00001750
Matt Caswelldad78fb2016-01-13 14:20:25 +00001751 if (read_buf_len > 0) {
1752 SSL_CTX_set_default_read_buffer_len(ctx, read_buf_len);
1753 }
Piotr Sikorae783bae2014-12-22 11:15:51 +00001754#ifndef OPENSSL_NO_SRTP
Matt Caswellac59d702015-03-06 14:39:46 +00001755 if (srtp_profiles != NULL) {
Rich Salz7e1b7482015-04-24 15:26:15 -04001756 /* Returns 0 on success! */
1757 if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles) != 0) {
Matt Caswellac59d702015-03-06 14:39:46 +00001758 BIO_printf(bio_err, "Error setting SRTP profile\n");
1759 ERR_print_errors(bio_err);
1760 goto end;
1761 }
1762 }
Piotr Sikorae783bae2014-12-22 11:15:51 +00001763#endif
Ben Laurie333f9262011-11-15 22:59:20 +00001764
Matt Caswell2b6bcb72015-09-22 16:00:52 +01001765 if (!ctx_set_verify_locations(ctx, CAfile, CApath, noCAfile, noCApath)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001766 ERR_print_errors(bio_err);
Rich Salz7e1b7482015-04-24 15:26:15 -04001767 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +00001768 }
Rich Salz7e1b7482015-04-24 15:26:15 -04001769 if (vpmtouched && !SSL_CTX_set1_param(ctx, vpm)) {
1770 BIO_printf(bio_err, "Error setting verify params\n");
Matt Caswellac59d702015-03-06 14:39:46 +00001771 ERR_print_errors(bio_err);
1772 goto end;
1773 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001774
Matt Caswell0f113f32015-01-22 03:40:55 +00001775 ssl_ctx_add_crls(ctx, crls, 0);
Rich Salzdba31772016-02-14 00:17:59 -05001776 if (!config_ctx(cctx, ssl_args, ctx))
Matt Caswell0f113f32015-01-22 03:40:55 +00001777 goto end;
Dr. Stephen Henson5d2e07f2012-11-17 14:42:22 +00001778
Matt Caswell0f113f32015-01-22 03:40:55 +00001779 if (!ssl_load_stores(ctx, vfyCApath, vfyCAfile, chCApath, chCAfile,
1780 crls, crl_download)) {
1781 BIO_printf(bio_err, "Error loading store locations\n");
1782 ERR_print_errors(bio_err);
1783 goto end;
1784 }
Matt Caswelle481f9b2015-05-15 10:49:56 +01001785
Matt Caswell0f113f32015-01-22 03:40:55 +00001786 if (s_cert2) {
1787 ctx2 = SSL_CTX_new(meth);
1788 if (ctx2 == NULL) {
1789 ERR_print_errors(bio_err);
1790 goto end;
1791 }
1792 }
Bodo Möllered3883d2006-01-02 23:14:37 +00001793
Matt Caswell0f113f32015-01-22 03:40:55 +00001794 if (ctx2) {
1795 BIO_printf(bio_s_out, "Setting secondary ctx parameters\n");
Dr. Stephen Hensone03c5b52014-02-17 00:10:00 +00001796
Matt Caswell0f113f32015-01-22 03:40:55 +00001797 if (sdebug)
Rich Salzecf3a1f2015-04-29 11:27:08 -04001798 ssl_ctx_security_debug(ctx, sdebug);
Bodo Möllerb1277b92006-01-02 23:29:12 +00001799
Matt Caswell0f113f32015-01-22 03:40:55 +00001800 if (session_id_prefix) {
1801 if (strlen(session_id_prefix) >= 32)
1802 BIO_printf(bio_err,
1803 "warning: id_prefix is too long, only one new session will be possible\n");
1804 if (!SSL_CTX_set_generate_session_id(ctx2, generate_session_id)) {
1805 BIO_printf(bio_err, "error setting 'id_prefix'\n");
1806 ERR_print_errors(bio_err);
1807 goto end;
1808 }
1809 BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
1810 }
1811 SSL_CTX_set_quiet_shutdown(ctx2, 1);
Matt Caswell0f113f32015-01-22 03:40:55 +00001812 if (exc)
1813 ssl_ctx_set_excert(ctx2, exc);
Bodo Möllerb1277b92006-01-02 23:29:12 +00001814
Matt Caswell0f113f32015-01-22 03:40:55 +00001815 if (state)
1816 SSL_CTX_set_info_callback(ctx2, apps_ssl_info_callback);
Bodo Möllerb1277b92006-01-02 23:29:12 +00001817
Matt Caswell0f113f32015-01-22 03:40:55 +00001818 if (no_cache)
1819 SSL_CTX_set_session_cache_mode(ctx2, SSL_SESS_CACHE_OFF);
1820 else if (ext_cache)
1821 init_session_cache_ctx(ctx2);
1822 else
1823 SSL_CTX_sess_set_cache_size(ctx2, 128);
Dr. Stephen Henson5d2e07f2012-11-17 14:42:22 +00001824
Matt Caswell7e25dd62015-02-13 23:33:12 +00001825 if (async)
Matt Caswellf4da39d2015-07-24 08:15:31 +01001826 SSL_CTX_set_mode(ctx2, SSL_MODE_ASYNC);
Matt Caswell7e25dd62015-02-13 23:33:12 +00001827
Dr. Stephen Hensonf65a8c12016-05-23 18:13:16 +01001828 if (!ctx_set_verify_locations(ctx2, CAfile, CApath, noCAfile,
1829 noCApath)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001830 ERR_print_errors(bio_err);
Dr. Stephen Hensonf65a8c12016-05-23 18:13:16 +01001831 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +00001832 }
Rich Salz7e1b7482015-04-24 15:26:15 -04001833 if (vpmtouched && !SSL_CTX_set1_param(ctx2, vpm)) {
1834 BIO_printf(bio_err, "Error setting verify params\n");
Matt Caswellac59d702015-03-06 14:39:46 +00001835 ERR_print_errors(bio_err);
1836 goto end;
1837 }
Ben Laurieee2ffc22010-07-28 10:06:55 +00001838
Matt Caswell0f113f32015-01-22 03:40:55 +00001839 ssl_ctx_add_crls(ctx2, crls, 0);
Rich Salzdba31772016-02-14 00:17:59 -05001840 if (!config_ctx(cctx, ssl_args, ctx2))
Matt Caswell0f113f32015-01-22 03:40:55 +00001841 goto end;
1842 }
Matt Caswelle481f9b2015-05-15 10:49:56 +01001843#ifndef OPENSSL_NO_NEXTPROTONEG
Matt Caswell0f113f32015-01-22 03:40:55 +00001844 if (next_proto.data)
1845 SSL_CTX_set_next_protos_advertised_cb(ctx, next_proto_cb,
1846 &next_proto);
Matt Caswelle481f9b2015-05-15 10:49:56 +01001847#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00001848 if (alpn_ctx.data)
1849 SSL_CTX_set_alpn_select_cb(ctx, alpn_cb, &alpn_ctx);
Bodo Möllerb1277b92006-01-02 23:29:12 +00001850
Richard Levittecf1b7d92001-02-19 16:06:34 +00001851#ifndef OPENSSL_NO_DH
Matt Caswell0f113f32015-01-22 03:40:55 +00001852 if (!no_dhe) {
1853 DH *dh = NULL;
Bodo Möller15d52dd2000-11-02 10:35:10 +00001854
Matt Caswell0f113f32015-01-22 03:40:55 +00001855 if (dhfile)
1856 dh = load_dh_param(dhfile);
1857 else if (s_cert_file)
1858 dh = load_dh_param(s_cert_file);
Bodo Möller15d52dd2000-11-02 10:35:10 +00001859
Matt Caswell0f113f32015-01-22 03:40:55 +00001860 if (dh != NULL) {
1861 BIO_printf(bio_s_out, "Setting temp DH parameters\n");
1862 } else {
1863 BIO_printf(bio_s_out, "Using default temp DH parameters\n");
1864 }
1865 (void)BIO_flush(bio_s_out);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001866
Matt Caswell0f113f32015-01-22 03:40:55 +00001867 if (dh == NULL)
1868 SSL_CTX_set_dh_auto(ctx, 1);
1869 else if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
1870 BIO_puts(bio_err, "Error setting temp DH parameters\n");
1871 ERR_print_errors(bio_err);
1872 DH_free(dh);
1873 goto end;
1874 }
Matt Caswelle481f9b2015-05-15 10:49:56 +01001875
Matt Caswell0f113f32015-01-22 03:40:55 +00001876 if (ctx2) {
1877 if (!dhfile) {
1878 DH *dh2 = load_dh_param(s_cert_file2);
1879 if (dh2 != NULL) {
1880 BIO_printf(bio_s_out, "Setting temp DH parameters\n");
1881 (void)BIO_flush(bio_s_out);
Bodo Möllered3883d2006-01-02 23:14:37 +00001882
Matt Caswell0f113f32015-01-22 03:40:55 +00001883 DH_free(dh);
1884 dh = dh2;
1885 }
1886 }
1887 if (dh == NULL)
1888 SSL_CTX_set_dh_auto(ctx2, 1);
1889 else if (!SSL_CTX_set_tmp_dh(ctx2, dh)) {
1890 BIO_puts(bio_err, "Error setting temp DH parameters\n");
1891 ERR_print_errors(bio_err);
1892 DH_free(dh);
1893 goto end;
1894 }
1895 }
Matt Caswell0f113f32015-01-22 03:40:55 +00001896 DH_free(dh);
1897 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001898#endif
Bodo Möllerea262262002-08-09 08:56:08 +00001899
Matt Caswell0f113f32015-01-22 03:40:55 +00001900 if (!set_cert_key_stuff(ctx, s_cert, s_key, s_chain, build_chain))
1901 goto end;
Matt Caswelle481f9b2015-05-15 10:49:56 +01001902
Matt Caswell0f113f32015-01-22 03:40:55 +00001903 if (s_serverinfo_file != NULL
1904 && !SSL_CTX_use_serverinfo_file(ctx, s_serverinfo_file)) {
1905 ERR_print_errors(bio_err);
1906 goto end;
1907 }
Matt Caswelle481f9b2015-05-15 10:49:56 +01001908
Matt Caswell0f113f32015-01-22 03:40:55 +00001909 if (ctx2 && !set_cert_key_stuff(ctx2, s_cert2, s_key2, NULL, build_chain))
1910 goto end;
Matt Caswelle481f9b2015-05-15 10:49:56 +01001911
Matt Caswell0f113f32015-01-22 03:40:55 +00001912 if (s_dcert != NULL) {
1913 if (!set_cert_key_stuff(ctx, s_dcert, s_dkey, s_dchain, build_chain))
1914 goto end;
1915 }
Matt Caswell0f113f32015-01-22 03:40:55 +00001916
1917 if (no_resume_ephemeral) {
1918 SSL_CTX_set_not_resumable_session_callback(ctx,
1919 not_resumable_sess_cb);
Matt Caswelle481f9b2015-05-15 10:49:56 +01001920
Matt Caswell0f113f32015-01-22 03:40:55 +00001921 if (ctx2)
1922 SSL_CTX_set_not_resumable_session_callback(ctx2,
1923 not_resumable_sess_cb);
Matt Caswell0f113f32015-01-22 03:40:55 +00001924 }
Nils Larschddac1972006-03-10 23:06:27 +00001925#ifndef OPENSSL_NO_PSK
Flavio Medeirosb5292f72016-01-30 20:14:39 -05001926 if (psk_key != NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001927 if (s_debug)
FdaSilvaYYd6073e22016-08-07 12:04:26 +02001928 BIO_printf(bio_s_out, "PSK key given, setting server callback\n");
Matt Caswell0f113f32015-01-22 03:40:55 +00001929 SSL_CTX_set_psk_server_callback(ctx, psk_server_cb);
1930 }
Nils Larschddac1972006-03-10 23:06:27 +00001931
Matt Caswell0f113f32015-01-22 03:40:55 +00001932 if (!SSL_CTX_use_psk_identity_hint(ctx, psk_identity_hint)) {
1933 BIO_printf(bio_err, "error setting PSK identity hint to context\n");
1934 ERR_print_errors(bio_err);
1935 goto end;
1936 }
Nils Larschddac1972006-03-10 23:06:27 +00001937#endif
1938
Matt Caswell0f113f32015-01-22 03:40:55 +00001939 SSL_CTX_set_verify(ctx, s_server_verify, verify_callback);
Viktor Dukhovni61986d32015-04-16 01:50:03 -04001940 if (!SSL_CTX_set_session_id_context(ctx,
FdaSilvaYYd6073e22016-08-07 12:04:26 +02001941 (void *)&s_server_session_id_context,
1942 sizeof s_server_session_id_context)) {
Matt Caswellac59d702015-03-06 14:39:46 +00001943 BIO_printf(bio_err, "error setting session id context\n");
1944 ERR_print_errors(bio_err);
1945 goto end;
1946 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001947
Matt Caswell0f113f32015-01-22 03:40:55 +00001948 /* Set DTLS cookie generation and verification callbacks */
1949 SSL_CTX_set_cookie_generate_cb(ctx, generate_cookie_callback);
1950 SSL_CTX_set_cookie_verify_cb(ctx, verify_cookie_callback);
Dr. Stephen Henson07a9d1a2009-09-04 17:42:53 +00001951
Matt Caswell0f113f32015-01-22 03:40:55 +00001952 if (ctx2) {
1953 SSL_CTX_set_verify(ctx2, s_server_verify, verify_callback);
Viktor Dukhovni61986d32015-04-16 01:50:03 -04001954 if (!SSL_CTX_set_session_id_context(ctx2,
Rich Salz7e1b7482015-04-24 15:26:15 -04001955 (void *)&s_server_session_id_context,
1956 sizeof s_server_session_id_context)) {
Matt Caswellac59d702015-03-06 14:39:46 +00001957 BIO_printf(bio_err, "error setting session id context\n");
1958 ERR_print_errors(bio_err);
1959 goto end;
1960 }
Matt Caswell0f113f32015-01-22 03:40:55 +00001961 tlsextcbp.biodebug = bio_s_out;
1962 SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb);
1963 SSL_CTX_set_tlsext_servername_arg(ctx2, &tlsextcbp);
1964 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
1965 SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
1966 }
Bodo Möllerf1fd4542006-01-03 03:27:19 +00001967
Ben Laurieedc032b2011-03-12 17:01:19 +00001968#ifndef OPENSSL_NO_SRP
Matt Caswell0f113f32015-01-22 03:40:55 +00001969 if (srp_verifier_file != NULL) {
1970 srp_callback_parm.vb = SRP_VBASE_new(srpuserseed);
1971 srp_callback_parm.user = NULL;
1972 srp_callback_parm.login = NULL;
1973 if ((ret =
1974 SRP_VBASE_init(srp_callback_parm.vb,
1975 srp_verifier_file)) != SRP_NO_ERROR) {
1976 BIO_printf(bio_err,
1977 "Cannot initialize SRP verifier file \"%s\":ret=%d\n",
1978 srp_verifier_file, ret);
1979 goto end;
1980 }
1981 SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, verify_callback);
1982 SSL_CTX_set_srp_cb_arg(ctx, &srp_callback_parm);
1983 SSL_CTX_set_srp_username_callback(ctx, ssl_srp_server_param_cb);
1984 } else
Ben Laurieedc032b2011-03-12 17:01:19 +00001985#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00001986 if (CAfile != NULL) {
1987 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(CAfile));
Matt Caswelle481f9b2015-05-15 10:49:56 +01001988
Matt Caswell0f113f32015-01-22 03:40:55 +00001989 if (ctx2)
1990 SSL_CTX_set_client_CA_list(ctx2, SSL_load_client_CA_file(CAfile));
Matt Caswell0f113f32015-01-22 03:40:55 +00001991 }
Matt Caswell3e41ac32016-03-21 16:54:53 +00001992#ifndef OPENSSL_NO_OCSP
Adam Eijdenbergbe0c0362015-07-29 21:34:35 -04001993 if (s_tlsextstatus) {
1994 SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb);
1995 SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp);
1996 if (ctx2) {
1997 SSL_CTX_set_tlsext_status_cb(ctx2, cert_status_cb);
1998 SSL_CTX_set_tlsext_status_arg(ctx2, &tlscstatp);
1999 }
2000 }
Matt Caswell3e41ac32016-03-21 16:54:53 +00002001#endif
Peter Wu4bf73e92017-02-01 19:14:27 +01002002 if (set_keylog_file(ctx, keylog_file))
2003 goto end;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002004
Matt Caswell67466482017-02-23 16:54:11 +00002005 if (max_early_data >= 0)
Matt Caswell048b1892017-02-17 17:01:16 +00002006 SSL_CTX_set_max_early_data(ctx, max_early_data);
2007
Matt Caswell0f113f32015-01-22 03:40:55 +00002008 BIO_printf(bio_s_out, "ACCEPT\n");
2009 (void)BIO_flush(bio_s_out);
2010 if (rev)
2011 server_cb = rev_body;
2012 else if (www)
2013 server_cb = www_body;
2014 else
2015 server_cb = sv_body;
Richard Levitteab69ac02016-02-03 00:47:42 +01002016#ifdef AF_UNIX
2017 if (socket_family == AF_UNIX
2018 && unlink_unix_path)
2019 unlink(host);
Dr. Stephen Henson9cd86ab2014-07-01 12:44:00 +01002020#endif
Richard Levitteab69ac02016-02-03 00:47:42 +01002021 do_server(&accept_socket, host, port, socket_family, socket_type,
2022 server_cb, context, naccept);
Matt Caswell0f113f32015-01-22 03:40:55 +00002023 print_stats(bio_s_out, ctx);
2024 ret = 0;
2025 end:
Rich Salz62adbce2015-04-11 10:22:36 -04002026 SSL_CTX_free(ctx);
Peter Wu4bf73e92017-02-01 19:14:27 +01002027 set_keylog_file(NULL, NULL);
Rich Salz222561f2015-04-30 17:33:59 -04002028 X509_free(s_cert);
2029 sk_X509_CRL_pop_free(crls, X509_CRL_free);
2030 X509_free(s_dcert);
Rich Salzc5ba2d92015-03-28 10:54:15 -04002031 EVP_PKEY_free(s_key);
2032 EVP_PKEY_free(s_dkey);
Rich Salz222561f2015-04-30 17:33:59 -04002033 sk_X509_pop_free(s_chain, X509_free);
2034 sk_X509_pop_free(s_dchain, X509_free);
Rich Salz25aaa982015-05-01 14:37:16 -04002035 OPENSSL_free(pass);
2036 OPENSSL_free(dpass);
Richard Levitteab69ac02016-02-03 00:47:42 +01002037 OPENSSL_free(host);
2038 OPENSSL_free(port);
Rich Salz222561f2015-04-30 17:33:59 -04002039 X509_VERIFY_PARAM_free(vpm);
Matt Caswell0f113f32015-01-22 03:40:55 +00002040 free_sessions();
Rich Salz25aaa982015-05-01 14:37:16 -04002041 OPENSSL_free(tlscstatp.host);
2042 OPENSSL_free(tlscstatp.port);
2043 OPENSSL_free(tlscstatp.path);
Rich Salz62adbce2015-04-11 10:22:36 -04002044 SSL_CTX_free(ctx2);
Rich Salz222561f2015-04-30 17:33:59 -04002045 X509_free(s_cert2);
Rich Salzc5ba2d92015-03-28 10:54:15 -04002046 EVP_PKEY_free(s_key2);
Matt Caswelle481f9b2015-05-15 10:49:56 +01002047#ifndef OPENSSL_NO_NEXTPROTONEG
Rich Salz25aaa982015-05-01 14:37:16 -04002048 OPENSSL_free(next_proto.data);
Bodo Möllered3883d2006-01-02 23:14:37 +00002049#endif
Matt Caswelle481f9b2015-05-15 10:49:56 +01002050 OPENSSL_free(alpn_ctx.data);
Matt Caswell0f113f32015-01-22 03:40:55 +00002051 ssl_excert_free(exc);
Rich Salz7e1b7482015-04-24 15:26:15 -04002052 sk_OPENSSL_STRING_free(ssl_args);
Rich Salz62adbce2015-04-11 10:22:36 -04002053 SSL_CONF_CTX_free(cctx);
Richard Levittedd1abd42016-09-28 23:39:18 +02002054 release_engine(engine);
Rich Salzca3a82c2015-03-25 11:31:18 -04002055 BIO_free(bio_s_out);
2056 bio_s_out = NULL;
2057 BIO_free(bio_s_msg);
2058 bio_s_msg = NULL;
Matt Caswell5fd14782016-04-28 11:34:54 +01002059#ifdef CHARSET_EBCDIC
2060 BIO_meth_free(methods_ebcdic);
2061#endif
Rich Salz7e1b7482015-04-24 15:26:15 -04002062 return (ret);
Matt Caswell0f113f32015-01-22 03:40:55 +00002063}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002064
Ulf Möller6b691a51999-04-19 21:31:43 +00002065static void print_stats(BIO *bio, SSL_CTX *ssl_ctx)
Matt Caswell0f113f32015-01-22 03:40:55 +00002066{
2067 BIO_printf(bio, "%4ld items in the session cache\n",
2068 SSL_CTX_sess_number(ssl_ctx));
2069 BIO_printf(bio, "%4ld client connects (SSL_connect())\n",
2070 SSL_CTX_sess_connect(ssl_ctx));
2071 BIO_printf(bio, "%4ld client renegotiates (SSL_connect())\n",
2072 SSL_CTX_sess_connect_renegotiate(ssl_ctx));
2073 BIO_printf(bio, "%4ld client connects that finished\n",
2074 SSL_CTX_sess_connect_good(ssl_ctx));
2075 BIO_printf(bio, "%4ld server accepts (SSL_accept())\n",
2076 SSL_CTX_sess_accept(ssl_ctx));
2077 BIO_printf(bio, "%4ld server renegotiates (SSL_accept())\n",
2078 SSL_CTX_sess_accept_renegotiate(ssl_ctx));
2079 BIO_printf(bio, "%4ld server accepts that finished\n",
2080 SSL_CTX_sess_accept_good(ssl_ctx));
2081 BIO_printf(bio, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx));
2082 BIO_printf(bio, "%4ld session cache misses\n",
2083 SSL_CTX_sess_misses(ssl_ctx));
2084 BIO_printf(bio, "%4ld session cache timeouts\n",
2085 SSL_CTX_sess_timeouts(ssl_ctx));
2086 BIO_printf(bio, "%4ld callback cache hits\n",
2087 SSL_CTX_sess_cb_hits(ssl_ctx));
2088 BIO_printf(bio, "%4ld cache full overflows (%ld allowed)\n",
2089 SSL_CTX_sess_cache_full(ssl_ctx),
2090 SSL_CTX_sess_get_cache_size(ssl_ctx));
2091}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002092
Rich Salza773b522016-02-13 22:33:56 -05002093static int sv_body(int s, int stype, unsigned char *context)
Matt Caswell0f113f32015-01-22 03:40:55 +00002094{
2095 char *buf = NULL;
2096 fd_set readfds;
2097 int ret = 1, width;
2098 int k, i;
2099 unsigned long l;
2100 SSL *con = NULL;
2101 BIO *sbio;
Matt Caswell0f113f32015-01-22 03:40:55 +00002102 struct timeval timeout;
Rich Salz1fbab1d2016-03-17 12:53:11 -04002103#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
Matt Caswell0f113f32015-01-22 03:40:55 +00002104 struct timeval tv;
Dr. Stephen Hensonba4526e2009-08-18 11:15:33 +00002105#else
Matt Caswell0f113f32015-01-22 03:40:55 +00002106 struct timeval *timeoutp;
Dr. Stephen Henson06f45361999-09-20 22:09:17 +00002107#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002108
Rich Salz68dc6822015-04-30 17:48:31 -04002109 buf = app_malloc(bufsize, "server buffer");
Matt Caswell0f113f32015-01-22 03:40:55 +00002110 if (s_nbio) {
Rich Salzba810812016-02-27 13:24:28 -05002111 if (!BIO_socket_nbio(s, 1))
Matt Caswell0f113f32015-01-22 03:40:55 +00002112 ERR_print_errors(bio_err);
Rich Salzba810812016-02-27 13:24:28 -05002113 else if (!s_quiet)
2114 BIO_printf(bio_err, "Turned on non blocking io\n");
Matt Caswell0f113f32015-01-22 03:40:55 +00002115 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002116
Matt Caswell0f113f32015-01-22 03:40:55 +00002117 if (con == NULL) {
2118 con = SSL_new(ctx);
Matt Caswelle481f9b2015-05-15 10:49:56 +01002119
Matt Caswell0f113f32015-01-22 03:40:55 +00002120 if (s_tlsextdebug) {
2121 SSL_set_tlsext_debug_callback(con, tlsext_cb);
2122 SSL_set_tlsext_debug_arg(con, bio_s_out);
2123 }
Matt Caswelle481f9b2015-05-15 10:49:56 +01002124
Rich Salz7e1b7482015-04-24 15:26:15 -04002125 if (context
FdaSilvaYYd6073e22016-08-07 12:04:26 +02002126 && !SSL_set_session_id_context(con,
2127 context, strlen((char *)context))) {
Matt Caswellac59d702015-03-06 14:39:46 +00002128 BIO_printf(bio_err, "Error setting session id context\n");
2129 ret = -1;
2130 goto err;
2131 }
Matt Caswell0f113f32015-01-22 03:40:55 +00002132 }
Viktor Dukhovni61986d32015-04-16 01:50:03 -04002133 if (!SSL_clear(con)) {
Matt Caswellac59d702015-03-06 14:39:46 +00002134 BIO_printf(bio_err, "Error clearing SSL connection\n");
2135 ret = -1;
2136 goto err;
2137 }
Ben Lauriea7a14a22015-12-16 13:25:07 +00002138#ifndef OPENSSL_NO_DTLS
Matt Caswell0f113f32015-01-22 03:40:55 +00002139 if (stype == SOCK_DGRAM) {
Ben Laurie36d16f82005-04-26 16:02:40 +00002140
Matt Caswell0f113f32015-01-22 03:40:55 +00002141 sbio = BIO_new_dgram(s, BIO_NOCLOSE);
Ben Laurie36d16f82005-04-26 16:02:40 +00002142
Matt Caswell0f113f32015-01-22 03:40:55 +00002143 if (enable_timeouts) {
2144 timeout.tv_sec = 0;
2145 timeout.tv_usec = DGRAM_RCV_TIMEOUT;
2146 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
Ben Laurie36d16f82005-04-26 16:02:40 +00002147
Matt Caswell0f113f32015-01-22 03:40:55 +00002148 timeout.tv_sec = 0;
2149 timeout.tv_usec = DGRAM_SND_TIMEOUT;
2150 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
2151 }
2152
2153 if (socket_mtu) {
2154 if (socket_mtu < DTLS_get_link_min_mtu(con)) {
2155 BIO_printf(bio_err, "MTU too small. Must be at least %ld\n",
2156 DTLS_get_link_min_mtu(con));
2157 ret = -1;
2158 BIO_free(sbio);
2159 goto err;
2160 }
2161 SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
2162 if (!DTLS_set_link_mtu(con, socket_mtu)) {
2163 BIO_printf(bio_err, "Failed to set MTU\n");
2164 ret = -1;
2165 BIO_free(sbio);
2166 goto err;
2167 }
2168 } else
2169 /* want to do MTU discovery */
2170 BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
Ben Laurie36d16f82005-04-26 16:02:40 +00002171
2172 /* turn on cookie exchange */
2173 SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE);
Matt Caswell0f113f32015-01-22 03:40:55 +00002174 } else
Ben Lauriea7a14a22015-12-16 13:25:07 +00002175#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00002176 sbio = BIO_new_socket(s, BIO_NOCLOSE);
Ben Laurie36d16f82005-04-26 16:02:40 +00002177
Matt Caswell0f113f32015-01-22 03:40:55 +00002178 if (s_nbio_test) {
2179 BIO *test;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002180
Matt Caswell0f113f32015-01-22 03:40:55 +00002181 test = BIO_new(BIO_f_nbio_test());
2182 sbio = BIO_push(test, sbio);
2183 }
Ben Laurie6caa4ed2008-10-26 18:40:52 +00002184
Matt Caswell0f113f32015-01-22 03:40:55 +00002185 SSL_set_bio(con, sbio, sbio);
2186 SSL_set_accept_state(con);
2187 /* SSL_set_fd(con,s); */
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002188
Matt Caswell0f113f32015-01-22 03:40:55 +00002189 if (s_debug) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002190 BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
2191 BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
2192 }
2193 if (s_msg) {
Dr. Stephen Henson93ab9e42012-06-15 12:46:09 +00002194#ifndef OPENSSL_NO_SSL_TRACE
Matt Caswell0f113f32015-01-22 03:40:55 +00002195 if (s_msg == 2)
2196 SSL_set_msg_callback(con, SSL_trace);
2197 else
Dr. Stephen Henson93ab9e42012-06-15 12:46:09 +00002198#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00002199 SSL_set_msg_callback(con, msg_cb);
2200 SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
2201 }
Matt Caswelle481f9b2015-05-15 10:49:56 +01002202
Matt Caswell0f113f32015-01-22 03:40:55 +00002203 if (s_tlsextdebug) {
2204 SSL_set_tlsext_debug_callback(con, tlsext_cb);
2205 SSL_set_tlsext_debug_arg(con, bio_s_out);
2206 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002207
Matt Caswelle0655182017-02-22 15:24:11 +00002208 if (early_data) {
Matt Caswellf533fbd2017-03-02 14:42:55 +00002209 int write_header = 1, edret = SSL_READ_EARLY_DATA_ERROR;
Matt Caswelle0655182017-02-22 15:24:11 +00002210 size_t readbytes;
2211
Matt Caswellf533fbd2017-03-02 14:42:55 +00002212 while (edret != SSL_READ_EARLY_DATA_FINISH) {
Matt Caswelle0655182017-02-22 15:24:11 +00002213 for (;;) {
Matt Caswellf533fbd2017-03-02 14:42:55 +00002214 edret = SSL_read_early_data(con, buf, bufsize, &readbytes);
2215 if (edret != SSL_READ_EARLY_DATA_ERROR)
Matt Caswelle0655182017-02-22 15:24:11 +00002216 break;
2217
2218 switch (SSL_get_error(con, 0)) {
2219 case SSL_ERROR_WANT_WRITE:
2220 case SSL_ERROR_WANT_ASYNC:
2221 case SSL_ERROR_WANT_READ:
2222 /* Just keep trying - busy waiting */
2223 continue;
2224 default:
2225 BIO_printf(bio_err, "Error reading early data\n");
2226 ERR_print_errors(bio_err);
2227 goto err;
2228 }
2229 }
2230 if (readbytes > 0) {
2231 if (write_header) {
2232 BIO_printf(bio_s_out, "Early data received:\n");
2233 write_header = 0;
2234 }
2235 raw_write_stdout(buf, (unsigned int)readbytes);
2236 (void)BIO_flush(bio_s_out);
2237 }
2238 }
2239 if (write_header)
2240 BIO_printf(bio_s_out, "No early data received\n");
2241 else
2242 BIO_printf(bio_s_out, "\nEnd of early data\n");
Matt Caswellade1e882017-02-27 20:55:04 +00002243 if (SSL_is_init_finished(con))
2244 print_connection_info(con);
Matt Caswelle0655182017-02-22 15:24:11 +00002245 }
2246
Richard Levitte51e51332016-09-15 11:20:18 +02002247 if (fileno_stdin() > s)
2248 width = fileno_stdin() + 1;
Richard Levittec7bdb6a2016-09-14 20:54:30 +02002249 else
2250 width = s + 1;
Matt Caswell0f113f32015-01-22 03:40:55 +00002251 for (;;) {
2252 int read_from_terminal;
2253 int read_from_sslcon;
Bodo Möllera2a01582000-02-21 17:09:54 +00002254
Matt Caswell0f113f32015-01-22 03:40:55 +00002255 read_from_terminal = 0;
Matt Caswellfd068d52016-02-12 13:33:45 +00002256 read_from_sslcon = SSL_has_pending(con)
Matt Caswell64c07bd2015-09-16 22:54:54 +01002257 || (async && SSL_waiting_for_async(con));
Bodo Möllera2a01582000-02-21 17:09:54 +00002258
Matt Caswell0f113f32015-01-22 03:40:55 +00002259 if (!read_from_sslcon) {
2260 FD_ZERO(&readfds);
Rich Salz1fbab1d2016-03-17 12:53:11 -04002261#if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
Richard Levitte51e51332016-09-15 11:20:18 +02002262 openssl_fdset(fileno_stdin(), &readfds);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002263#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00002264 openssl_fdset(s, &readfds);
2265 /*
2266 * Note: under VMS with SOCKETSHR the second parameter is
2267 * currently of type (int *) whereas under other systems it is
2268 * (void *) if you don't have a cast it will choke the compiler:
2269 * if you do have a cast then you can either go for (int *) or
2270 * (void *).
2271 */
Rich Salz1fbab1d2016-03-17 12:53:11 -04002272#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
Matt Caswell0f113f32015-01-22 03:40:55 +00002273 /*
2274 * Under DOS (non-djgpp) and Windows we can't select on stdin:
2275 * only on sockets. As a workaround we timeout the select every
2276 * second and check for any keypress. In a proper Windows
2277 * application we wouldn't do this because it is inefficient.
2278 */
2279 tv.tv_sec = 1;
2280 tv.tv_usec = 0;
2281 i = select(width, (void *)&readfds, NULL, NULL, &tv);
Matt Caswell75dd6c12016-05-20 11:53:26 +01002282 if (has_stdin_waiting())
Matt Caswell0f113f32015-01-22 03:40:55 +00002283 read_from_terminal = 1;
Matt Caswell75dd6c12016-05-20 11:53:26 +01002284 if ((i < 0) || (!i && !read_from_terminal))
2285 continue;
Dr. Stephen Henson06f45361999-09-20 22:09:17 +00002286#else
Matt Caswell0f113f32015-01-22 03:40:55 +00002287 if ((SSL_version(con) == DTLS1_VERSION) &&
2288 DTLSv1_get_timeout(con, &timeout))
2289 timeoutp = &timeout;
2290 else
2291 timeoutp = NULL;
Dr. Stephen Hensonb972fba2009-08-12 13:19:54 +00002292
Matt Caswell0f113f32015-01-22 03:40:55 +00002293 i = select(width, (void *)&readfds, NULL, NULL, timeoutp);
Dr. Stephen Hensonb972fba2009-08-12 13:19:54 +00002294
Matt Caswell0f113f32015-01-22 03:40:55 +00002295 if ((SSL_version(con) == DTLS1_VERSION)
2296 && DTLSv1_handle_timeout(con) > 0) {
2297 BIO_printf(bio_err, "TIMEOUT occurred\n");
2298 }
Dr. Stephen Hensonb972fba2009-08-12 13:19:54 +00002299
Matt Caswell0f113f32015-01-22 03:40:55 +00002300 if (i <= 0)
2301 continue;
Richard Levitte51e51332016-09-15 11:20:18 +02002302 if (FD_ISSET(fileno_stdin(), &readfds))
Matt Caswell0f113f32015-01-22 03:40:55 +00002303 read_from_terminal = 1;
Dr. Stephen Henson06f45361999-09-20 22:09:17 +00002304#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00002305 if (FD_ISSET(s, &readfds))
2306 read_from_sslcon = 1;
2307 }
2308 if (read_from_terminal) {
2309 if (s_crlf) {
2310 int j, lf_num;
Bodo Möller1bdb8631999-08-07 02:51:10 +00002311
Richard Levittec7bdb6a2016-09-14 20:54:30 +02002312 i = raw_read_stdin(buf, bufsize / 2);
Richard Levittec7bdb6a2016-09-14 20:54:30 +02002313 lf_num = 0;
Matt Caswell0f113f32015-01-22 03:40:55 +00002314 /* both loops are skipped when i <= 0 */
2315 for (j = 0; j < i; j++)
2316 if (buf[j] == '\n')
2317 lf_num++;
2318 for (j = i - 1; j >= 0; j--) {
2319 buf[j + lf_num] = buf[j];
2320 if (buf[j] == '\n') {
2321 lf_num--;
2322 i++;
2323 buf[j + lf_num] = '\r';
2324 }
2325 }
2326 assert(lf_num == 0);
Richard Levitte51e51332016-09-15 11:20:18 +02002327 } else
Richard Levittec7bdb6a2016-09-14 20:54:30 +02002328 i = raw_read_stdin(buf, bufsize);
Richard Levitte51e51332016-09-15 11:20:18 +02002329
Matt Caswell0f113f32015-01-22 03:40:55 +00002330 if (!s_quiet && !s_brief) {
2331 if ((i <= 0) || (buf[0] == 'Q')) {
2332 BIO_printf(bio_s_out, "DONE\n");
Rich Salz7e1b7482015-04-24 15:26:15 -04002333 (void)BIO_flush(bio_s_out);
Rich Salz8731a4f2016-03-02 16:12:46 -05002334 BIO_closesocket(s);
Matt Caswell0f113f32015-01-22 03:40:55 +00002335 close_accept_socket();
2336 ret = -11;
2337 goto err;
2338 }
2339 if ((i <= 0) || (buf[0] == 'q')) {
2340 BIO_printf(bio_s_out, "DONE\n");
Rich Salz7e1b7482015-04-24 15:26:15 -04002341 (void)BIO_flush(bio_s_out);
Matt Caswell0f113f32015-01-22 03:40:55 +00002342 if (SSL_version(con) != DTLS1_VERSION)
Rich Salz8731a4f2016-03-02 16:12:46 -05002343 BIO_closesocket(s);
Matt Caswell0f113f32015-01-22 03:40:55 +00002344 /*
2345 * close_accept_socket(); ret= -11;
2346 */
2347 goto err;
2348 }
Richard Levitteb6127992016-11-15 14:53:33 +01002349#ifndef OPENSSL_NO_HEARTBEATS
2350 if ((buf[0] == 'B') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
2351 BIO_printf(bio_err, "HEARTBEATING\n");
2352 SSL_heartbeat(con);
2353 i = 0;
2354 continue;
2355 }
2356#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00002357 if ((buf[0] == 'r') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
2358 SSL_renegotiate(con);
2359 i = SSL_do_handshake(con);
2360 printf("SSL_do_handshake -> %d\n", i);
2361 i = 0; /* 13; */
2362 continue;
2363 /*
2364 * strcpy(buf,"server side RE-NEGOTIATE\n");
2365 */
2366 }
2367 if ((buf[0] == 'R') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
2368 SSL_set_verify(con,
2369 SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
2370 NULL);
2371 SSL_renegotiate(con);
2372 i = SSL_do_handshake(con);
2373 printf("SSL_do_handshake -> %d\n", i);
2374 i = 0; /* 13; */
2375 continue;
2376 /*
2377 * strcpy(buf,"server side RE-NEGOTIATE asking for client
2378 * cert\n");
2379 */
2380 }
Matt Caswell34df45b2017-02-08 16:52:23 +00002381 if ((buf[0] == 'K' || buf[0] == 'k')
2382 && ((buf[1] == '\n') || (buf[1] == '\r'))) {
2383 SSL_key_update(con, buf[0] == 'K' ?
2384 SSL_KEY_UPDATE_REQUESTED
2385 : SSL_KEY_UPDATE_NOT_REQUESTED);
2386 i = SSL_do_handshake(con);
2387 printf("SSL_do_handshake -> %d\n", i);
2388 i = 0;
2389 continue;
2390 /*
2391 * strcpy(buf,"server side RE-NEGOTIATE asking for client
2392 * cert\n");
2393 */
2394 }
Matt Caswell0f113f32015-01-22 03:40:55 +00002395 if (buf[0] == 'P') {
2396 static const char *str = "Lets print some clear text\n";
2397 BIO_write(SSL_get_wbio(con), str, strlen(str));
2398 }
2399 if (buf[0] == 'S') {
2400 print_stats(bio_s_out, SSL_get_SSL_CTX(con));
2401 }
2402 }
Ulf Möllera53955d1999-06-04 21:35:58 +00002403#ifdef CHARSET_EBCDIC
Matt Caswell0f113f32015-01-22 03:40:55 +00002404 ebcdic2ascii(buf, buf, i);
Ulf Möllera53955d1999-06-04 21:35:58 +00002405#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00002406 l = k = 0;
2407 for (;;) {
2408 /* should do a select for the write */
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00002409#ifdef RENEG
FdaSilvaYY54463e42016-08-03 22:49:25 +02002410 static count = 0;
2411 if (++count == 100) {
2412 count = 0;
2413 SSL_renegotiate(con);
Matt Caswell0f113f32015-01-22 03:40:55 +00002414 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002415#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00002416 k = SSL_write(con, &(buf[l]), (unsigned int)i);
Dr. Stephen Henson9641be22012-02-10 19:43:14 +00002417#ifndef OPENSSL_NO_SRP
Matt Caswell0f113f32015-01-22 03:40:55 +00002418 while (SSL_get_error(con, k) == SSL_ERROR_WANT_X509_LOOKUP) {
2419 BIO_printf(bio_s_out, "LOOKUP renego during write\n");
Emilia Kasper380f18e2016-02-24 12:59:59 +01002420 SRP_user_pwd_free(srp_callback_parm.user);
Matt Caswell0f113f32015-01-22 03:40:55 +00002421 srp_callback_parm.user =
Emilia Kasper380f18e2016-02-24 12:59:59 +01002422 SRP_VBASE_get1_by_user(srp_callback_parm.vb,
2423 srp_callback_parm.login);
Richard Levittec7bdb6a2016-09-14 20:54:30 +02002424 if (srp_callback_parm.user)
Matt Caswell0f113f32015-01-22 03:40:55 +00002425 BIO_printf(bio_s_out, "LOOKUP done %s\n",
2426 srp_callback_parm.user->info);
2427 else
2428 BIO_printf(bio_s_out, "LOOKUP not successful\n");
2429 k = SSL_write(con, &(buf[l]), (unsigned int)i);
2430 }
Dr. Stephen Henson9641be22012-02-10 19:43:14 +00002431#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00002432 switch (SSL_get_error(con, k)) {
2433 case SSL_ERROR_NONE:
2434 break;
Matt Caswell7e25dd62015-02-13 23:33:12 +00002435 case SSL_ERROR_WANT_ASYNC:
2436 BIO_printf(bio_s_out, "Write BLOCK (Async)\n");
Matt Caswell384f08d2016-05-20 11:20:22 +01002437 (void)BIO_flush(bio_s_out);
Matt Caswellf4da39d2015-07-24 08:15:31 +01002438 wait_for_async(con);
Matt Caswell7e25dd62015-02-13 23:33:12 +00002439 break;
Matt Caswell0f113f32015-01-22 03:40:55 +00002440 case SSL_ERROR_WANT_WRITE:
2441 case SSL_ERROR_WANT_READ:
2442 case SSL_ERROR_WANT_X509_LOOKUP:
2443 BIO_printf(bio_s_out, "Write BLOCK\n");
Matt Caswell384f08d2016-05-20 11:20:22 +01002444 (void)BIO_flush(bio_s_out);
Matt Caswell0f113f32015-01-22 03:40:55 +00002445 break;
Matt Caswellfc7f1902016-05-03 17:55:00 +01002446 case SSL_ERROR_WANT_ASYNC_JOB:
2447 /*
2448 * This shouldn't ever happen in s_server. Treat as an error
2449 */
Matt Caswell0f113f32015-01-22 03:40:55 +00002450 case SSL_ERROR_SYSCALL:
2451 case SSL_ERROR_SSL:
2452 BIO_printf(bio_s_out, "ERROR\n");
Rich Salz7e1b7482015-04-24 15:26:15 -04002453 (void)BIO_flush(bio_s_out);
Matt Caswell0f113f32015-01-22 03:40:55 +00002454 ERR_print_errors(bio_err);
2455 ret = 1;
2456 goto err;
2457 /* break; */
2458 case SSL_ERROR_ZERO_RETURN:
2459 BIO_printf(bio_s_out, "DONE\n");
Rich Salz7e1b7482015-04-24 15:26:15 -04002460 (void)BIO_flush(bio_s_out);
Matt Caswell0f113f32015-01-22 03:40:55 +00002461 ret = 1;
2462 goto err;
2463 }
Robert Swiecki00d565c2015-05-18 19:08:02 -04002464 if (k > 0) {
2465 l += k;
2466 i -= k;
2467 }
Matt Caswell0f113f32015-01-22 03:40:55 +00002468 if (i <= 0)
2469 break;
2470 }
2471 }
2472 if (read_from_sslcon) {
Matt Caswell64c07bd2015-09-16 22:54:54 +01002473 /*
2474 * init_ssl_connection handles all async events itself so if we're
2475 * waiting for async then we shouldn't go back into
2476 * init_ssl_connection
2477 */
2478 if ((!async || !SSL_waiting_for_async(con))
2479 && !SSL_is_init_finished(con)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002480 i = init_ssl_connection(con);
2481
2482 if (i < 0) {
2483 ret = 0;
2484 goto err;
2485 } else if (i == 0) {
2486 ret = 1;
2487 goto err;
2488 }
2489 } else {
2490 again:
2491 i = SSL_read(con, (char *)buf, bufsize);
Dr. Stephen Henson9641be22012-02-10 19:43:14 +00002492#ifndef OPENSSL_NO_SRP
Matt Caswell0f113f32015-01-22 03:40:55 +00002493 while (SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) {
2494 BIO_printf(bio_s_out, "LOOKUP renego during read\n");
Emilia Kasper380f18e2016-02-24 12:59:59 +01002495 SRP_user_pwd_free(srp_callback_parm.user);
Matt Caswell0f113f32015-01-22 03:40:55 +00002496 srp_callback_parm.user =
Emilia Kasper380f18e2016-02-24 12:59:59 +01002497 SRP_VBASE_get1_by_user(srp_callback_parm.vb,
2498 srp_callback_parm.login);
Matt Caswell0f113f32015-01-22 03:40:55 +00002499 if (srp_callback_parm.user)
2500 BIO_printf(bio_s_out, "LOOKUP done %s\n",
2501 srp_callback_parm.user->info);
2502 else
2503 BIO_printf(bio_s_out, "LOOKUP not successful\n");
2504 i = SSL_read(con, (char *)buf, bufsize);
2505 }
Dr. Stephen Henson9641be22012-02-10 19:43:14 +00002506#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00002507 switch (SSL_get_error(con, i)) {
2508 case SSL_ERROR_NONE:
Ulf Möllera53955d1999-06-04 21:35:58 +00002509#ifdef CHARSET_EBCDIC
Matt Caswell0f113f32015-01-22 03:40:55 +00002510 ascii2ebcdic(buf, buf, i);
Ulf Möllera53955d1999-06-04 21:35:58 +00002511#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00002512 raw_write_stdout(buf, (unsigned int)i);
Matt Caswell384f08d2016-05-20 11:20:22 +01002513 (void)BIO_flush(bio_s_out);
Matt Caswellfd068d52016-02-12 13:33:45 +00002514 if (SSL_has_pending(con))
Matt Caswell0f113f32015-01-22 03:40:55 +00002515 goto again;
2516 break;
Matt Caswell7e25dd62015-02-13 23:33:12 +00002517 case SSL_ERROR_WANT_ASYNC:
Matt Caswellf4da39d2015-07-24 08:15:31 +01002518 BIO_printf(bio_s_out, "Read BLOCK (Async)\n");
Matt Caswell384f08d2016-05-20 11:20:22 +01002519 (void)BIO_flush(bio_s_out);
Matt Caswellf4da39d2015-07-24 08:15:31 +01002520 wait_for_async(con);
2521 break;
Matt Caswell0f113f32015-01-22 03:40:55 +00002522 case SSL_ERROR_WANT_WRITE:
2523 case SSL_ERROR_WANT_READ:
2524 BIO_printf(bio_s_out, "Read BLOCK\n");
Matt Caswell384f08d2016-05-20 11:20:22 +01002525 (void)BIO_flush(bio_s_out);
Matt Caswell0f113f32015-01-22 03:40:55 +00002526 break;
Matt Caswellfc7f1902016-05-03 17:55:00 +01002527 case SSL_ERROR_WANT_ASYNC_JOB:
2528 /*
2529 * This shouldn't ever happen in s_server. Treat as an error
2530 */
Matt Caswell0f113f32015-01-22 03:40:55 +00002531 case SSL_ERROR_SYSCALL:
2532 case SSL_ERROR_SSL:
2533 BIO_printf(bio_s_out, "ERROR\n");
Rich Salz7e1b7482015-04-24 15:26:15 -04002534 (void)BIO_flush(bio_s_out);
Matt Caswell0f113f32015-01-22 03:40:55 +00002535 ERR_print_errors(bio_err);
2536 ret = 1;
2537 goto err;
2538 case SSL_ERROR_ZERO_RETURN:
2539 BIO_printf(bio_s_out, "DONE\n");
Rich Salz7e1b7482015-04-24 15:26:15 -04002540 (void)BIO_flush(bio_s_out);
Matt Caswell0f113f32015-01-22 03:40:55 +00002541 ret = 1;
2542 goto err;
2543 }
2544 }
2545 }
2546 }
2547 err:
2548 if (con != NULL) {
2549 BIO_printf(bio_s_out, "shutting down SSL\n");
Matt Caswell0f113f32015-01-22 03:40:55 +00002550 SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
Matt Caswell0f113f32015-01-22 03:40:55 +00002551 SSL_free(con);
2552 }
2553 BIO_printf(bio_s_out, "CONNECTION CLOSED\n");
Rich Salz4b45c6e2015-04-30 17:57:32 -04002554 OPENSSL_clear_free(buf, bufsize);
Matt Caswell0f113f32015-01-22 03:40:55 +00002555 if (ret >= 0)
2556 BIO_printf(bio_s_out, "ACCEPT\n");
Rich Salzc54cc2b2015-04-25 09:26:48 -04002557 (void)BIO_flush(bio_s_out);
Matt Caswell0f113f32015-01-22 03:40:55 +00002558 return (ret);
2559}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002560
Ulf Möller6b691a51999-04-19 21:31:43 +00002561static void close_accept_socket(void)
Matt Caswell0f113f32015-01-22 03:40:55 +00002562{
2563 BIO_printf(bio_err, "shutdown accept socket\n");
2564 if (accept_socket >= 0) {
Rich Salz8731a4f2016-03-02 16:12:46 -05002565 BIO_closesocket(accept_socket);
Matt Caswell0f113f32015-01-22 03:40:55 +00002566 }
2567}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002568
Ulf Möller6b691a51999-04-19 21:31:43 +00002569static int init_ssl_connection(SSL *con)
Matt Caswell0f113f32015-01-22 03:40:55 +00002570{
2571 int i;
Ben Lauriedf2ee0e2015-09-05 13:32:58 +01002572 long verify_err;
Matt Caswell384f08d2016-05-20 11:20:22 +01002573 int retry = 0;
Ben Lauriee0af0402011-11-15 23:50:52 +00002574
Kurt Roeckxa5ecdc62015-12-12 11:12:22 +01002575#ifndef OPENSSL_NO_DTLS
FdaSilvaYYe8aa8b62016-06-29 00:18:50 +02002576 if (dtlslisten) {
Richard Levitted858c872016-02-03 00:27:44 +01002577 BIO_ADDR *client = NULL;
2578
2579 if ((client = BIO_ADDR_new()) == NULL) {
2580 BIO_printf(bio_err, "ERROR - memory\n");
2581 return 0;
2582 }
Matt Caswell3edeb622016-02-05 10:59:42 +00002583 i = DTLSv1_listen(con, client);
Matt Caswellfd4e98e2015-04-09 10:01:05 +01002584 if (i > 0) {
2585 BIO *wbio;
Emilia Kasper3a796182015-09-23 19:57:42 +02002586 int fd = -1;
Matt Caswellfd4e98e2015-04-09 10:01:05 +01002587
2588 wbio = SSL_get_wbio(con);
FdaSilvaYYe8aa8b62016-06-29 00:18:50 +02002589 if (wbio) {
Matt Caswellfd4e98e2015-04-09 10:01:05 +01002590 BIO_get_fd(wbio, &fd);
2591 }
2592
FdaSilvaYYe8aa8b62016-06-29 00:18:50 +02002593 if (!wbio || BIO_connect(fd, client, 0) == 0) {
Matt Caswellfd4e98e2015-04-09 10:01:05 +01002594 BIO_printf(bio_err, "ERROR - unable to connect\n");
Richard Levitted858c872016-02-03 00:27:44 +01002595 BIO_ADDR_free(client);
Matt Caswellfd4e98e2015-04-09 10:01:05 +01002596 return 0;
2597 }
Richard Levitted858c872016-02-03 00:27:44 +01002598 BIO_ADDR_free(client);
Matt Caswellfd4e98e2015-04-09 10:01:05 +01002599 dtlslisten = 0;
2600 i = SSL_accept(con);
Matt Caswella3768e02016-04-26 18:33:03 +01002601 } else {
2602 BIO_ADDR_free(client);
Matt Caswellfd4e98e2015-04-09 10:01:05 +01002603 }
2604 } else
2605#endif
Matt Caswell7e25dd62015-02-13 23:33:12 +00002606
2607 do {
Matt Caswellfd4e98e2015-04-09 10:01:05 +01002608 i = SSL_accept(con);
2609
Matt Caswell384f08d2016-05-20 11:20:22 +01002610 if (i <= 0)
2611 retry = BIO_sock_should_retry(i);
Dr. Stephen Henson33233142014-01-26 00:51:09 +00002612#ifdef CERT_CB_TEST_RETRY
Matt Caswell7e25dd62015-02-13 23:33:12 +00002613 {
FdaSilvaYYd6073e22016-08-07 12:04:26 +02002614 while (i <= 0
2615 && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP
Matt Caswell7e25dd62015-02-13 23:33:12 +00002616 && SSL_get_state(con) == TLS_ST_SR_CLNT_HELLO) {
2617 BIO_printf(bio_err,
FdaSilvaYYd6073e22016-08-07 12:04:26 +02002618 "LOOKUP from certificate callback during accept\n");
Matt Caswell7e25dd62015-02-13 23:33:12 +00002619 i = SSL_accept(con);
Matt Caswell384f08d2016-05-20 11:20:22 +01002620 if (i <= 0)
2621 retry = BIO_sock_should_retry(i);
Matt Caswell7e25dd62015-02-13 23:33:12 +00002622 }
2623 }
2624#endif
2625
2626#ifndef OPENSSL_NO_SRP
FdaSilvaYYd6073e22016-08-07 12:04:26 +02002627 while (i <= 0
2628 && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) {
Matt Caswell7e25dd62015-02-13 23:33:12 +00002629 BIO_printf(bio_s_out, "LOOKUP during accept %s\n",
2630 srp_callback_parm.login);
Emilia Kasper380f18e2016-02-24 12:59:59 +01002631 SRP_user_pwd_free(srp_callback_parm.user);
Matt Caswell7e25dd62015-02-13 23:33:12 +00002632 srp_callback_parm.user =
Emilia Kasper380f18e2016-02-24 12:59:59 +01002633 SRP_VBASE_get1_by_user(srp_callback_parm.vb,
2634 srp_callback_parm.login);
Matt Caswell7e25dd62015-02-13 23:33:12 +00002635 if (srp_callback_parm.user)
2636 BIO_printf(bio_s_out, "LOOKUP done %s\n",
2637 srp_callback_parm.user->info);
2638 else
2639 BIO_printf(bio_s_out, "LOOKUP not successful\n");
Matt Caswell0f113f32015-01-22 03:40:55 +00002640 i = SSL_accept(con);
Matt Caswell384f08d2016-05-20 11:20:22 +01002641 if (i <= 0)
2642 retry = BIO_sock_should_retry(i);
Matt Caswell0f113f32015-01-22 03:40:55 +00002643 }
Dr. Stephen Henson33233142014-01-26 00:51:09 +00002644#endif
Matt Caswell7e25dd62015-02-13 23:33:12 +00002645 } while (i < 0 && SSL_waiting_for_async(con));
Scott Deboy67c408c2013-08-01 11:54:09 -07002646
Matt Caswell0f113f32015-01-22 03:40:55 +00002647 if (i <= 0) {
Matt Caswellfd4e98e2015-04-09 10:01:05 +01002648 if ((dtlslisten && i == 0)
Matt Caswell384f08d2016-05-20 11:20:22 +01002649 || (!dtlslisten && retry)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002650 BIO_printf(bio_s_out, "DELAY\n");
2651 return (1);
2652 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002653
Matt Caswell0f113f32015-01-22 03:40:55 +00002654 BIO_printf(bio_err, "ERROR\n");
Rich Salz7e1b7482015-04-24 15:26:15 -04002655
Ben Lauriedf2ee0e2015-09-05 13:32:58 +01002656 verify_err = SSL_get_verify_result(con);
2657 if (verify_err != X509_V_OK) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002658 BIO_printf(bio_err, "verify error:%s\n",
Ben Lauriedf2ee0e2015-09-05 13:32:58 +01002659 X509_verify_cert_error_string(verify_err));
Matt Caswell0f113f32015-01-22 03:40:55 +00002660 }
2661 /* Always print any error messages */
2662 ERR_print_errors(bio_err);
2663 return (0);
2664 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002665
Matt Caswellade1e882017-02-27 20:55:04 +00002666 print_connection_info(con);
2667 return 1;
2668}
2669
2670static void print_connection_info(SSL *con)
2671{
2672 const char *str;
2673 X509 *peer;
2674 char buf[BUFSIZ];
2675#if !defined(OPENSSL_NO_NEXTPROTONEG)
2676 const unsigned char *next_proto_neg;
2677 unsigned next_proto_neg_len;
2678#endif
2679 unsigned char *exportedkeymat;
2680 int i;
2681
Matt Caswell0f113f32015-01-22 03:40:55 +00002682 if (s_brief)
Rich Salzecf3a1f2015-04-29 11:27:08 -04002683 print_ssl_summary(con);
Dr. Stephen Henson2a7cbe72012-09-12 23:14:28 +00002684
Matt Caswell0f113f32015-01-22 03:40:55 +00002685 PEM_write_bio_SSL_SESSION(bio_s_out, SSL_get_session(con));
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002686
Matt Caswell0f113f32015-01-22 03:40:55 +00002687 peer = SSL_get_peer_certificate(con);
2688 if (peer != NULL) {
2689 BIO_printf(bio_s_out, "Client certificate\n");
2690 PEM_write_bio_X509(bio_s_out, peer);
2691 X509_NAME_oneline(X509_get_subject_name(peer), buf, sizeof buf);
2692 BIO_printf(bio_s_out, "subject=%s\n", buf);
2693 X509_NAME_oneline(X509_get_issuer_name(peer), buf, sizeof buf);
2694 BIO_printf(bio_s_out, "issuer=%s\n", buf);
2695 X509_free(peer);
FdaSilvaYY049f3652016-03-07 21:00:02 +01002696 peer = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +00002697 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002698
Matt Caswell0f113f32015-01-22 03:40:55 +00002699 if (SSL_get_shared_ciphers(con, buf, sizeof buf) != NULL)
2700 BIO_printf(bio_s_out, "Shared ciphers:%s\n", buf);
2701 str = SSL_CIPHER_get_name(SSL_get_current_cipher(con));
2702 ssl_print_sigalgs(bio_s_out, con);
Dr. Stephen Henson14536c82013-08-17 17:40:08 +01002703#ifndef OPENSSL_NO_EC
Matt Caswell0f113f32015-01-22 03:40:55 +00002704 ssl_print_point_formats(bio_s_out, con);
Matt Caswellde4d7642016-11-09 14:51:06 +00002705 ssl_print_groups(bio_s_out, con, 0);
Dr. Stephen Henson14536c82013-08-17 17:40:08 +01002706#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00002707 BIO_printf(bio_s_out, "CIPHER is %s\n", (str != NULL) ? str : "(NONE)");
Dr. Stephen Hensone7f8ff42012-03-06 14:28:21 +00002708
Matt Caswelle481f9b2015-05-15 10:49:56 +01002709#if !defined(OPENSSL_NO_NEXTPROTONEG)
Matt Caswell0f113f32015-01-22 03:40:55 +00002710 SSL_get0_next_proto_negotiated(con, &next_proto_neg, &next_proto_neg_len);
2711 if (next_proto_neg) {
2712 BIO_printf(bio_s_out, "NEXTPROTO is ");
2713 BIO_write(bio_s_out, next_proto_neg, next_proto_neg_len);
2714 BIO_printf(bio_s_out, "\n");
2715 }
Ben Laurieee2ffc22010-07-28 10:06:55 +00002716#endif
Piotr Sikorae783bae2014-12-22 11:15:51 +00002717#ifndef OPENSSL_NO_SRTP
Matt Caswell0f113f32015-01-22 03:40:55 +00002718 {
2719 SRTP_PROTECTION_PROFILE *srtp_profile
2720 = SSL_get_selected_srtp_profile(con);
Ben Laurie333f9262011-11-15 22:59:20 +00002721
Matt Caswell0f113f32015-01-22 03:40:55 +00002722 if (srtp_profile)
2723 BIO_printf(bio_s_out, "SRTP Extension negotiated, profile=%s\n",
2724 srtp_profile->name);
2725 }
Piotr Sikorae783bae2014-12-22 11:15:51 +00002726#endif
Dr. Stephen Hensonb577fd02016-02-08 16:18:26 +00002727 if (SSL_session_reused(con))
Matt Caswell0f113f32015-01-22 03:40:55 +00002728 BIO_printf(bio_s_out, "Reused session-id\n");
Matt Caswell0f113f32015-01-22 03:40:55 +00002729 BIO_printf(bio_s_out, "Secure Renegotiation IS%s supported\n",
2730 SSL_get_secure_renegotiation_support(con) ? "" : " NOT");
2731 if (keymatexportlabel != NULL) {
2732 BIO_printf(bio_s_out, "Keying material exporter:\n");
2733 BIO_printf(bio_s_out, " Label: '%s'\n", keymatexportlabel);
2734 BIO_printf(bio_s_out, " Length: %i bytes\n", keymatexportlen);
Rich Salz68dc6822015-04-30 17:48:31 -04002735 exportedkeymat = app_malloc(keymatexportlen, "export key");
2736 if (!SSL_export_keying_material(con, exportedkeymat,
2737 keymatexportlen,
2738 keymatexportlabel,
2739 strlen(keymatexportlabel),
2740 NULL, 0, 0)) {
2741 BIO_printf(bio_s_out, " Error\n");
2742 } else {
2743 BIO_printf(bio_s_out, " Keying material: ");
2744 for (i = 0; i < keymatexportlen; i++)
2745 BIO_printf(bio_s_out, "%02X", exportedkeymat[i]);
2746 BIO_printf(bio_s_out, "\n");
Matt Caswell0f113f32015-01-22 03:40:55 +00002747 }
Rich Salz68dc6822015-04-30 17:48:31 -04002748 OPENSSL_free(exportedkeymat);
Matt Caswell0f113f32015-01-22 03:40:55 +00002749 }
Ben Lauriee0af0402011-11-15 23:50:52 +00002750
FdaSilvaYYd6073e22016-08-07 12:04:26 +02002751 (void)BIO_flush(bio_s_out);
Matt Caswell0f113f32015-01-22 03:40:55 +00002752}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002753
Richard Levittecf1b7d92001-02-19 16:06:34 +00002754#ifndef OPENSSL_NO_DH
Nils Larscheb3eab22005-04-07 22:48:33 +00002755static DH *load_dh_param(const char *dhfile)
Matt Caswell0f113f32015-01-22 03:40:55 +00002756{
2757 DH *ret = NULL;
2758 BIO *bio;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002759
Matt Caswell0f113f32015-01-22 03:40:55 +00002760 if ((bio = BIO_new_file(dhfile, "r")) == NULL)
2761 goto err;
2762 ret = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
2763 err:
Rich Salzca3a82c2015-03-25 11:31:18 -04002764 BIO_free(bio);
Matt Caswell0f113f32015-01-22 03:40:55 +00002765 return (ret);
2766}
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00002767#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002768
Rich Salza773b522016-02-13 22:33:56 -05002769static int www_body(int s, int stype, unsigned char *context)
Matt Caswell0f113f32015-01-22 03:40:55 +00002770{
2771 char *buf = NULL;
2772 int ret = 1;
2773 int i, j, k, dot;
2774 SSL *con;
2775 const SSL_CIPHER *c;
2776 BIO *io, *ssl_bio, *sbio;
Rich Salz7e1b7482015-04-24 15:26:15 -04002777#ifdef RENEG
2778 int total_bytes = 0;
2779#endif
Matt Caswell075c8792015-09-11 13:11:37 +01002780 int width;
2781 fd_set readfds;
2782
2783 /* Set width for a select call if needed */
2784 width = s + 1;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002785
Rich Salz68dc6822015-04-30 17:48:31 -04002786 buf = app_malloc(bufsize, "server www buffer");
Matt Caswell0f113f32015-01-22 03:40:55 +00002787 io = BIO_new(BIO_f_buffer());
2788 ssl_bio = BIO_new(BIO_f_ssl());
2789 if ((io == NULL) || (ssl_bio == NULL))
2790 goto err;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002791
Matt Caswell0f113f32015-01-22 03:40:55 +00002792 if (s_nbio) {
Rich Salzba810812016-02-27 13:24:28 -05002793 if (!BIO_socket_nbio(s, 1))
Matt Caswell0f113f32015-01-22 03:40:55 +00002794 ERR_print_errors(bio_err);
Rich Salzba810812016-02-27 13:24:28 -05002795 else if (!s_quiet)
2796 BIO_printf(bio_err, "Turned on non blocking io\n");
Matt Caswell0f113f32015-01-22 03:40:55 +00002797 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002798
Matt Caswell0f113f32015-01-22 03:40:55 +00002799 /* lets make the output buffer a reasonable size */
2800 if (!BIO_set_write_buffer_size(io, bufsize))
2801 goto err;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002802
Matt Caswell0f113f32015-01-22 03:40:55 +00002803 if ((con = SSL_new(ctx)) == NULL)
2804 goto err;
Matt Caswelle481f9b2015-05-15 10:49:56 +01002805
Matt Caswell0f113f32015-01-22 03:40:55 +00002806 if (s_tlsextdebug) {
2807 SSL_set_tlsext_debug_callback(con, tlsext_cb);
2808 SSL_set_tlsext_debug_arg(con, bio_s_out);
2809 }
Matt Caswelle481f9b2015-05-15 10:49:56 +01002810
FdaSilvaYYd6073e22016-08-07 12:04:26 +02002811 if (context
2812 && !SSL_set_session_id_context(con, context,
2813 strlen((char *)context)))
Matt Caswellac59d702015-03-06 14:39:46 +00002814 goto err;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002815
Matt Caswell0f113f32015-01-22 03:40:55 +00002816 sbio = BIO_new_socket(s, BIO_NOCLOSE);
2817 if (s_nbio_test) {
2818 BIO *test;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002819
Matt Caswell0f113f32015-01-22 03:40:55 +00002820 test = BIO_new(BIO_f_nbio_test());
2821 sbio = BIO_push(test, sbio);
2822 }
2823 SSL_set_bio(con, sbio, sbio);
2824 SSL_set_accept_state(con);
Ben Laurie71fa4512012-06-03 22:00:21 +00002825
Matt Caswell0f113f32015-01-22 03:40:55 +00002826 /* SSL_set_fd(con,s); */
2827 BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
2828 BIO_push(io, ssl_bio);
Ulf Möllera53955d1999-06-04 21:35:58 +00002829#ifdef CHARSET_EBCDIC
Matt Caswell0f113f32015-01-22 03:40:55 +00002830 io = BIO_push(BIO_new(BIO_f_ebcdic_filter()), io);
Ulf Möllera53955d1999-06-04 21:35:58 +00002831#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002832
Matt Caswell0f113f32015-01-22 03:40:55 +00002833 if (s_debug) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002834 BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
2835 BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
2836 }
2837 if (s_msg) {
Dr. Stephen Henson93ab9e42012-06-15 12:46:09 +00002838#ifndef OPENSSL_NO_SSL_TRACE
Matt Caswell0f113f32015-01-22 03:40:55 +00002839 if (s_msg == 2)
2840 SSL_set_msg_callback(con, SSL_trace);
2841 else
Dr. Stephen Henson93ab9e42012-06-15 12:46:09 +00002842#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00002843 SSL_set_msg_callback(con, msg_cb);
2844 SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
2845 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002846
Matt Caswell0f113f32015-01-22 03:40:55 +00002847 for (;;) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002848 i = BIO_gets(io, buf, bufsize - 1);
2849 if (i < 0) { /* error */
Matt Caswell4cfa6202015-03-27 15:20:24 +00002850 if (!BIO_should_retry(io) && !SSL_waiting_for_async(con)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002851 if (!s_quiet)
2852 ERR_print_errors(bio_err);
2853 goto err;
2854 } else {
2855 BIO_printf(bio_s_out, "read R BLOCK\n");
Dr. Stephen Henson4e7e6232015-09-12 02:37:48 +01002856#ifndef OPENSSL_NO_SRP
2857 if (BIO_should_io_special(io)
2858 && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
2859 BIO_printf(bio_s_out, "LOOKUP renego during read\n");
Emilia Kasper380f18e2016-02-24 12:59:59 +01002860 SRP_user_pwd_free(srp_callback_parm.user);
Dr. Stephen Henson4e7e6232015-09-12 02:37:48 +01002861 srp_callback_parm.user =
Emilia Kasper380f18e2016-02-24 12:59:59 +01002862 SRP_VBASE_get1_by_user(srp_callback_parm.vb,
2863 srp_callback_parm.login);
Dr. Stephen Henson4e7e6232015-09-12 02:37:48 +01002864 if (srp_callback_parm.user)
2865 BIO_printf(bio_s_out, "LOOKUP done %s\n",
2866 srp_callback_parm.user->info);
2867 else
2868 BIO_printf(bio_s_out, "LOOKUP not successful\n");
2869 continue;
2870 }
2871#endif
Rich Salz1fbab1d2016-03-17 12:53:11 -04002872#if !defined(OPENSSL_SYS_MSDOS)
Matt Caswell0f113f32015-01-22 03:40:55 +00002873 sleep(1);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002874#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00002875 continue;
2876 }
2877 } else if (i == 0) { /* end of input */
2878 ret = 1;
2879 goto end;
2880 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002881
Matt Caswell0f113f32015-01-22 03:40:55 +00002882 /* else we have data */
2883 if (((www == 1) && (strncmp("GET ", buf, 4) == 0)) ||
Dmitry-Me0b142f02014-06-01 21:30:52 +04002884 ((www == 2) && (strncmp("GET /stats ", buf, 11) == 0))) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002885 char *p;
FdaSilvaYY049f3652016-03-07 21:00:02 +01002886 X509 *peer = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +00002887 STACK_OF(SSL_CIPHER) *sk;
2888 static const char *space = " ";
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002889
Matt Caswell0f113f32015-01-22 03:40:55 +00002890 if (www == 1 && strncmp("GET /reneg", buf, 10) == 0) {
2891 if (strncmp("GET /renegcert", buf, 14) == 0)
2892 SSL_set_verify(con,
2893 SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
2894 NULL);
2895 i = SSL_renegotiate(con);
2896 BIO_printf(bio_s_out, "SSL_renegotiate -> %d\n", i);
Matt Caswell075c8792015-09-11 13:11:37 +01002897 /* Send the HelloRequest */
Matt Caswell0f113f32015-01-22 03:40:55 +00002898 i = SSL_do_handshake(con);
2899 if (i <= 0) {
2900 BIO_printf(bio_s_out, "SSL_do_handshake() Retval %d\n",
2901 SSL_get_error(con, i));
2902 ERR_print_errors(bio_err);
2903 goto err;
2904 }
Matt Caswell075c8792015-09-11 13:11:37 +01002905 /* Wait for a ClientHello to come back */
2906 FD_ZERO(&readfds);
2907 openssl_fdset(s, &readfds);
2908 i = select(width, (void *)&readfds, NULL, NULL, NULL);
2909 if (i <= 0 || !FD_ISSET(s, &readfds)) {
FdaSilvaYYd6073e22016-08-07 12:04:26 +02002910 BIO_printf(bio_s_out,
2911 "Error waiting for client response\n");
Matt Caswell0f113f32015-01-22 03:40:55 +00002912 ERR_print_errors(bio_err);
2913 goto err;
2914 }
Matt Caswell075c8792015-09-11 13:11:37 +01002915 /*
FdaSilvaYY049f3652016-03-07 21:00:02 +01002916 * We're not actually expecting any data here and we ignore
Matt Caswell075c8792015-09-11 13:11:37 +01002917 * any that is sent. This is just to force the handshake that
2918 * we're expecting to come from the client. If they haven't
2919 * sent one there's not much we can do.
2920 */
2921 BIO_gets(io, buf, bufsize - 1);
Matt Caswell0f113f32015-01-22 03:40:55 +00002922 }
Dr. Stephen Henson08c23972010-01-28 19:48:36 +00002923
Matt Caswell0f113f32015-01-22 03:40:55 +00002924 BIO_puts(io,
2925 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
2926 BIO_puts(io, "<HTML><BODY BGCOLOR=\"#ffffff\">\n");
2927 BIO_puts(io, "<pre>\n");
FdaSilvaYY049f3652016-03-07 21:00:02 +01002928 /* BIO_puts(io, OpenSSL_version(OPENSSL_VERSION)); */
Matt Caswell0f113f32015-01-22 03:40:55 +00002929 BIO_puts(io, "\n");
2930 for (i = 0; i < local_argc; i++) {
Rich Salzf92beb92015-04-25 16:06:19 -04002931 const char *myp;
2932 for (myp = local_argv[i]; *myp; myp++)
2933 switch (*myp) {
2934 case '<':
2935 BIO_puts(io, "&lt;");
2936 break;
2937 case '>':
2938 BIO_puts(io, "&gt;");
2939 break;
2940 case '&':
2941 BIO_puts(io, "&amp;");
2942 break;
2943 default:
2944 BIO_write(io, myp, 1);
2945 break;
2946 }
Matt Caswell0f113f32015-01-22 03:40:55 +00002947 BIO_write(io, " ", 1);
2948 }
2949 BIO_puts(io, "\n");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002950
Matt Caswell0f113f32015-01-22 03:40:55 +00002951 BIO_printf(io,
2952 "Secure Renegotiation IS%s supported\n",
2953 SSL_get_secure_renegotiation_support(con) ?
2954 "" : " NOT");
Dr. Stephen Henson08c23972010-01-28 19:48:36 +00002955
Matt Caswell0f113f32015-01-22 03:40:55 +00002956 /*
2957 * The following is evil and should not really be done
2958 */
2959 BIO_printf(io, "Ciphers supported in s_server binary\n");
2960 sk = SSL_get_ciphers(con);
2961 j = sk_SSL_CIPHER_num(sk);
2962 for (i = 0; i < j; i++) {
2963 c = sk_SSL_CIPHER_value(sk, i);
Rich Salz7e1b7482015-04-24 15:26:15 -04002964 BIO_printf(io, "%-11s:%-25s ",
Matt Caswell0f113f32015-01-22 03:40:55 +00002965 SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
2966 if ((((i + 1) % 2) == 0) && (i + 1 != j))
2967 BIO_puts(io, "\n");
2968 }
2969 BIO_puts(io, "\n");
2970 p = SSL_get_shared_ciphers(con, buf, bufsize);
2971 if (p != NULL) {
2972 BIO_printf(io,
2973 "---\nCiphers common between both SSL end points:\n");
2974 j = i = 0;
2975 while (*p) {
2976 if (*p == ':') {
2977 BIO_write(io, space, 26 - j);
2978 i++;
2979 j = 0;
2980 BIO_write(io, ((i % 3) ? " " : "\n"), 1);
2981 } else {
2982 BIO_write(io, p, 1);
2983 j++;
2984 }
2985 p++;
2986 }
2987 BIO_puts(io, "\n");
2988 }
2989 ssl_print_sigalgs(io, con);
Dr. Stephen Henson14536c82013-08-17 17:40:08 +01002990#ifndef OPENSSL_NO_EC
Matt Caswellde4d7642016-11-09 14:51:06 +00002991 ssl_print_groups(io, con, 0);
Dr. Stephen Henson14536c82013-08-17 17:40:08 +01002992#endif
Dr. Stephen Hensonb577fd02016-02-08 16:18:26 +00002993 BIO_printf(io, (SSL_session_reused(con)
Matt Caswell0f113f32015-01-22 03:40:55 +00002994 ? "---\nReused, " : "---\nNew, "));
2995 c = SSL_get_current_cipher(con);
2996 BIO_printf(io, "%s, Cipher is %s\n",
2997 SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
2998 SSL_SESSION_print(io, SSL_get_session(con));
2999 BIO_printf(io, "---\n");
3000 print_stats(io, SSL_get_SSL_CTX(con));
3001 BIO_printf(io, "---\n");
3002 peer = SSL_get_peer_certificate(con);
3003 if (peer != NULL) {
3004 BIO_printf(io, "Client certificate\n");
3005 X509_print(io, peer);
3006 PEM_write_bio_X509(io, peer);
FdaSilvaYY049f3652016-03-07 21:00:02 +01003007 X509_free(peer);
3008 peer = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +00003009 } else
3010 BIO_puts(io, "no client certificate available\n");
3011 BIO_puts(io, "</BODY></HTML>\r\n\r\n");
3012 break;
3013 } else if ((www == 2 || www == 3)
3014 && (strncmp("GET /", buf, 5) == 0)) {
3015 BIO *file;
3016 char *p, *e;
3017 static const char *text =
3018 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003019
Matt Caswell0f113f32015-01-22 03:40:55 +00003020 /* skip the '/' */
3021 p = &(buf[5]);
Bodo Möller5d3ab9b2001-03-30 10:47:21 +00003022
Matt Caswell0f113f32015-01-22 03:40:55 +00003023 dot = 1;
3024 for (e = p; *e != '\0'; e++) {
3025 if (e[0] == ' ')
3026 break;
Bodo Möller5d3ab9b2001-03-30 10:47:21 +00003027
Matt Caswell0f113f32015-01-22 03:40:55 +00003028 switch (dot) {
3029 case 1:
3030 dot = (e[0] == '.') ? 2 : 0;
3031 break;
3032 case 2:
3033 dot = (e[0] == '.') ? 3 : 0;
3034 break;
3035 case 3:
3036 dot = (e[0] == '/') ? -1 : 0;
3037 break;
3038 }
3039 if (dot == 0)
3040 dot = (e[0] == '/') ? 1 : 0;
3041 }
3042 dot = (dot == 3) || (dot == -1); /* filename contains ".."
3043 * component */
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003044
Matt Caswell0f113f32015-01-22 03:40:55 +00003045 if (*e == '\0') {
3046 BIO_puts(io, text);
3047 BIO_printf(io, "'%s' is an invalid file name\r\n", p);
3048 break;
3049 }
3050 *e = '\0';
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003051
Matt Caswell0f113f32015-01-22 03:40:55 +00003052 if (dot) {
3053 BIO_puts(io, text);
3054 BIO_printf(io, "'%s' contains '..' reference\r\n", p);
3055 break;
3056 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003057
Matt Caswell0f113f32015-01-22 03:40:55 +00003058 if (*p == '/') {
3059 BIO_puts(io, text);
3060 BIO_printf(io, "'%s' is an invalid path\r\n", p);
3061 break;
3062 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003063
Matt Caswell0f113f32015-01-22 03:40:55 +00003064 /* if a directory, do the index thang */
3065 if (app_isdir(p) > 0) {
Matt Caswell0f113f32015-01-22 03:40:55 +00003066 BIO_puts(io, text);
3067 BIO_printf(io, "'%s' is a directory\r\n", p);
3068 break;
Matt Caswell0f113f32015-01-22 03:40:55 +00003069 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003070
Matt Caswell0f113f32015-01-22 03:40:55 +00003071 if ((file = BIO_new_file(p, "r")) == NULL) {
3072 BIO_puts(io, text);
3073 BIO_printf(io, "Error opening '%s'\r\n", p);
3074 ERR_print_errors(io);
3075 break;
3076 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003077
Matt Caswell0f113f32015-01-22 03:40:55 +00003078 if (!s_quiet)
3079 BIO_printf(bio_err, "FILE:%s\n", p);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003080
Matt Caswell0f113f32015-01-22 03:40:55 +00003081 if (www == 2) {
3082 i = strlen(p);
3083 if (((i > 5) && (strcmp(&(p[i - 5]), ".html") == 0)) ||
3084 ((i > 4) && (strcmp(&(p[i - 4]), ".php") == 0)) ||
3085 ((i > 4) && (strcmp(&(p[i - 4]), ".htm") == 0)))
3086 BIO_puts(io,
3087 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
3088 else
3089 BIO_puts(io,
3090 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
3091 }
3092 /* send the file */
3093 for (;;) {
3094 i = BIO_read(file, buf, bufsize);
3095 if (i <= 0)
3096 break;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003097
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +00003098#ifdef RENEG
Matt Caswell0f113f32015-01-22 03:40:55 +00003099 total_bytes += i;
Rich Salz7768e112015-06-04 14:26:55 -04003100 BIO_printf(bio_err, "%d\n", i);
Matt Caswell0f113f32015-01-22 03:40:55 +00003101 if (total_bytes > 3 * 1024) {
3102 total_bytes = 0;
Rich Salz7768e112015-06-04 14:26:55 -04003103 BIO_printf(bio_err, "RENEGOTIATE\n");
Matt Caswell0f113f32015-01-22 03:40:55 +00003104 SSL_renegotiate(con);
3105 }
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +00003106#endif
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00003107
Matt Caswell0f113f32015-01-22 03:40:55 +00003108 for (j = 0; j < i;) {
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00003109#ifdef RENEG
FdaSilvaYY54463e42016-08-03 22:49:25 +02003110 static count = 0;
3111 if (++count == 13) {
3112 SSL_renegotiate(con);
Matt Caswell0f113f32015-01-22 03:40:55 +00003113 }
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00003114#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00003115 k = BIO_write(io, &(buf[j]), i - j);
3116 if (k <= 0) {
FdaSilvaYYd6073e22016-08-07 12:04:26 +02003117 if (!BIO_should_retry(io)
3118 && !SSL_waiting_for_async(con))
Matt Caswell0f113f32015-01-22 03:40:55 +00003119 goto write_error;
3120 else {
3121 BIO_printf(bio_s_out, "rwrite W BLOCK\n");
3122 }
3123 } else {
3124 j += k;
3125 }
3126 }
3127 }
3128 write_error:
3129 BIO_free(file);
3130 break;
3131 }
3132 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003133
Matt Caswell0f113f32015-01-22 03:40:55 +00003134 for (;;) {
3135 i = (int)BIO_flush(io);
3136 if (i <= 0) {
3137 if (!BIO_should_retry(io))
3138 break;
3139 } else
3140 break;
3141 }
3142 end:
Matt Caswell0f113f32015-01-22 03:40:55 +00003143 /* make sure we re-use sessions */
3144 SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003145
Matt Caswell0f113f32015-01-22 03:40:55 +00003146 err:
Matt Caswell0f113f32015-01-22 03:40:55 +00003147 if (ret >= 0)
3148 BIO_printf(bio_s_out, "ACCEPT\n");
Rich Salzb548a1f2015-05-01 10:02:07 -04003149 OPENSSL_free(buf);
Rich Salzca3a82c2015-03-25 11:31:18 -04003150 BIO_free_all(io);
Matt Caswell0f113f32015-01-22 03:40:55 +00003151 return (ret);
3152}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003153
Rich Salza773b522016-02-13 22:33:56 -05003154static int rev_body(int s, int stype, unsigned char *context)
Matt Caswell0f113f32015-01-22 03:40:55 +00003155{
3156 char *buf = NULL;
3157 int i;
3158 int ret = 1;
3159 SSL *con;
3160 BIO *io, *ssl_bio, *sbio;
Dr. Stephen Henson4f3df8b2012-09-14 13:27:05 +00003161
Rich Salz68dc6822015-04-30 17:48:31 -04003162 buf = app_malloc(bufsize, "server rev buffer");
Matt Caswell0f113f32015-01-22 03:40:55 +00003163 io = BIO_new(BIO_f_buffer());
3164 ssl_bio = BIO_new(BIO_f_ssl());
3165 if ((io == NULL) || (ssl_bio == NULL))
3166 goto err;
Dr. Stephen Henson4f3df8b2012-09-14 13:27:05 +00003167
Matt Caswell0f113f32015-01-22 03:40:55 +00003168 /* lets make the output buffer a reasonable size */
3169 if (!BIO_set_write_buffer_size(io, bufsize))
3170 goto err;
Dr. Stephen Henson4f3df8b2012-09-14 13:27:05 +00003171
Matt Caswell0f113f32015-01-22 03:40:55 +00003172 if ((con = SSL_new(ctx)) == NULL)
3173 goto err;
Matt Caswelle481f9b2015-05-15 10:49:56 +01003174
Matt Caswell0f113f32015-01-22 03:40:55 +00003175 if (s_tlsextdebug) {
3176 SSL_set_tlsext_debug_callback(con, tlsext_cb);
3177 SSL_set_tlsext_debug_arg(con, bio_s_out);
3178 }
FdaSilvaYYd6073e22016-08-07 12:04:26 +02003179 if (context
3180 && !SSL_set_session_id_context(con, context,
3181 strlen((char *)context))) {
Matt Caswellac59d702015-03-06 14:39:46 +00003182 ERR_print_errors(bio_err);
3183 goto err;
3184 }
Dr. Stephen Henson4f3df8b2012-09-14 13:27:05 +00003185
Matt Caswell0f113f32015-01-22 03:40:55 +00003186 sbio = BIO_new_socket(s, BIO_NOCLOSE);
3187 SSL_set_bio(con, sbio, sbio);
3188 SSL_set_accept_state(con);
Dr. Stephen Henson4f3df8b2012-09-14 13:27:05 +00003189
Matt Caswell0f113f32015-01-22 03:40:55 +00003190 BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
3191 BIO_push(io, ssl_bio);
Dr. Stephen Henson4f3df8b2012-09-14 13:27:05 +00003192#ifdef CHARSET_EBCDIC
Matt Caswell0f113f32015-01-22 03:40:55 +00003193 io = BIO_push(BIO_new(BIO_f_ebcdic_filter()), io);
Dr. Stephen Henson4f3df8b2012-09-14 13:27:05 +00003194#endif
3195
Matt Caswell0f113f32015-01-22 03:40:55 +00003196 if (s_debug) {
Matt Caswell0f113f32015-01-22 03:40:55 +00003197 BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
3198 BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
3199 }
3200 if (s_msg) {
Dr. Stephen Henson4f3df8b2012-09-14 13:27:05 +00003201#ifndef OPENSSL_NO_SSL_TRACE
Matt Caswell0f113f32015-01-22 03:40:55 +00003202 if (s_msg == 2)
3203 SSL_set_msg_callback(con, SSL_trace);
3204 else
Dr. Stephen Henson4f3df8b2012-09-14 13:27:05 +00003205#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00003206 SSL_set_msg_callback(con, msg_cb);
3207 SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
3208 }
Dr. Stephen Henson4f3df8b2012-09-14 13:27:05 +00003209
Matt Caswell0f113f32015-01-22 03:40:55 +00003210 for (;;) {
3211 i = BIO_do_handshake(io);
3212 if (i > 0)
3213 break;
3214 if (!BIO_should_retry(io)) {
3215 BIO_puts(bio_err, "CONNECTION FAILURE\n");
3216 ERR_print_errors(bio_err);
3217 goto end;
3218 }
Dr. Stephen Henson4e7e6232015-09-12 02:37:48 +01003219#ifndef OPENSSL_NO_SRP
3220 if (BIO_should_io_special(io)
3221 && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
3222 BIO_printf(bio_s_out, "LOOKUP renego during accept\n");
Emilia Kasper380f18e2016-02-24 12:59:59 +01003223 SRP_user_pwd_free(srp_callback_parm.user);
Dr. Stephen Henson4e7e6232015-09-12 02:37:48 +01003224 srp_callback_parm.user =
Emilia Kasper380f18e2016-02-24 12:59:59 +01003225 SRP_VBASE_get1_by_user(srp_callback_parm.vb,
3226 srp_callback_parm.login);
Dr. Stephen Henson4e7e6232015-09-12 02:37:48 +01003227 if (srp_callback_parm.user)
3228 BIO_printf(bio_s_out, "LOOKUP done %s\n",
3229 srp_callback_parm.user->info);
3230 else
3231 BIO_printf(bio_s_out, "LOOKUP not successful\n");
3232 continue;
3233 }
3234#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00003235 }
3236 BIO_printf(bio_err, "CONNECTION ESTABLISHED\n");
Rich Salzecf3a1f2015-04-29 11:27:08 -04003237 print_ssl_summary(con);
Dr. Stephen Henson4f3df8b2012-09-14 13:27:05 +00003238
Matt Caswell0f113f32015-01-22 03:40:55 +00003239 for (;;) {
3240 i = BIO_gets(io, buf, bufsize - 1);
3241 if (i < 0) { /* error */
3242 if (!BIO_should_retry(io)) {
3243 if (!s_quiet)
3244 ERR_print_errors(bio_err);
3245 goto err;
3246 } else {
3247 BIO_printf(bio_s_out, "read R BLOCK\n");
Dr. Stephen Henson4e7e6232015-09-12 02:37:48 +01003248#ifndef OPENSSL_NO_SRP
3249 if (BIO_should_io_special(io)
3250 && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
3251 BIO_printf(bio_s_out, "LOOKUP renego during read\n");
Emilia Kasper380f18e2016-02-24 12:59:59 +01003252 SRP_user_pwd_free(srp_callback_parm.user);
Dr. Stephen Henson4e7e6232015-09-12 02:37:48 +01003253 srp_callback_parm.user =
Emilia Kasper380f18e2016-02-24 12:59:59 +01003254 SRP_VBASE_get1_by_user(srp_callback_parm.vb,
3255 srp_callback_parm.login);
Dr. Stephen Henson4e7e6232015-09-12 02:37:48 +01003256 if (srp_callback_parm.user)
3257 BIO_printf(bio_s_out, "LOOKUP done %s\n",
3258 srp_callback_parm.user->info);
3259 else
3260 BIO_printf(bio_s_out, "LOOKUP not successful\n");
3261 continue;
3262 }
3263#endif
Rich Salz1fbab1d2016-03-17 12:53:11 -04003264#if !defined(OPENSSL_SYS_MSDOS)
Matt Caswell0f113f32015-01-22 03:40:55 +00003265 sleep(1);
Dr. Stephen Henson4f3df8b2012-09-14 13:27:05 +00003266#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00003267 continue;
3268 }
3269 } else if (i == 0) { /* end of input */
3270 ret = 1;
3271 BIO_printf(bio_err, "CONNECTION CLOSED\n");
3272 goto end;
3273 } else {
3274 char *p = buf + i - 1;
3275 while (i && (*p == '\n' || *p == '\r')) {
3276 p--;
3277 i--;
3278 }
Rich Salz86885c22015-05-06 14:56:14 -04003279 if (!s_ign_eof && (i == 5) && (strncmp(buf, "CLOSE", 5) == 0)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00003280 ret = 1;
3281 BIO_printf(bio_err, "CONNECTION CLOSED\n");
3282 goto end;
3283 }
3284 BUF_reverse((unsigned char *)buf, NULL, i);
3285 buf[i] = '\n';
3286 BIO_write(io, buf, i + 1);
3287 for (;;) {
3288 i = BIO_flush(io);
3289 if (i > 0)
3290 break;
3291 if (!BIO_should_retry(io))
3292 goto end;
3293 }
3294 }
3295 }
3296 end:
3297 /* make sure we re-use sessions */
3298 SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
Dr. Stephen Henson4f3df8b2012-09-14 13:27:05 +00003299
Matt Caswell0f113f32015-01-22 03:40:55 +00003300 err:
Dr. Stephen Henson4f3df8b2012-09-14 13:27:05 +00003301
Rich Salzb548a1f2015-05-01 10:02:07 -04003302 OPENSSL_free(buf);
Rich Salzca3a82c2015-03-25 11:31:18 -04003303 BIO_free_all(io);
Matt Caswell0f113f32015-01-22 03:40:55 +00003304 return (ret);
3305}
Dr. Stephen Henson4f3df8b2012-09-14 13:27:05 +00003306
Geoff Thorpe1aa0d942001-02-21 18:38:48 +00003307#define MAX_SESSION_ID_ATTEMPTS 10
3308static int generate_session_id(const SSL *ssl, unsigned char *id,
Matt Caswell0f113f32015-01-22 03:40:55 +00003309 unsigned int *id_len)
3310{
3311 unsigned int count = 0;
3312 do {
Matt Caswell266483d2015-02-26 11:57:37 +00003313 if (RAND_bytes(id, *id_len) <= 0)
3314 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +00003315 /*
3316 * Prefix the session_id with the required prefix. NB: If our prefix
3317 * is too long, clip it - but there will be worse effects anyway, eg.
3318 * the server could only possibly create 1 session ID (ie. the
3319 * prefix!) so all future session negotiations will fail due to
3320 * conflicts.
3321 */
3322 memcpy(id, session_id_prefix,
3323 (strlen(session_id_prefix) < *id_len) ?
3324 strlen(session_id_prefix) : *id_len);
3325 }
3326 while (SSL_has_matching_session_id(ssl, id, *id_len) &&
3327 (++count < MAX_SESSION_ID_ATTEMPTS));
3328 if (count >= MAX_SESSION_ID_ATTEMPTS)
3329 return 0;
3330 return 1;
3331}
Dr. Stephen Henson35b0ea42009-12-27 23:24:45 +00003332
Matt Caswell0f113f32015-01-22 03:40:55 +00003333/*
3334 * By default s_server uses an in-memory cache which caches SSL_SESSION
Dr. Stephen Henson35b0ea42009-12-27 23:24:45 +00003335 * structures without any serialisation. This hides some bugs which only
3336 * become apparent in deployed servers. By implementing a basic external
3337 * session cache some issues can be debugged using s_server.
3338 */
3339
Matt Caswell0f113f32015-01-22 03:40:55 +00003340typedef struct simple_ssl_session_st {
3341 unsigned char *id;
3342 unsigned int idlen;
3343 unsigned char *der;
3344 int derlen;
3345 struct simple_ssl_session_st *next;
3346} simple_ssl_session;
Dr. Stephen Henson35b0ea42009-12-27 23:24:45 +00003347
3348static simple_ssl_session *first = NULL;
3349
3350static int add_session(SSL *ssl, SSL_SESSION *session)
Matt Caswell0f113f32015-01-22 03:40:55 +00003351{
Rich Salzb4faea52015-05-01 23:10:31 -04003352 simple_ssl_session *sess = app_malloc(sizeof(*sess), "get session");
Matt Caswell0f113f32015-01-22 03:40:55 +00003353 unsigned char *p;
Dr. Stephen Henson35b0ea42009-12-27 23:24:45 +00003354
Matt Caswell0f113f32015-01-22 03:40:55 +00003355 SSL_SESSION_get_id(session, &sess->idlen);
3356 sess->derlen = i2d_SSL_SESSION(session, NULL);
Rich Salz7e1b7482015-04-24 15:26:15 -04003357 if (sess->derlen < 0) {
3358 BIO_printf(bio_err, "Error encoding session\n");
Rich Salza194ee72015-04-25 22:55:36 -04003359 OPENSSL_free(sess);
Rich Salz7e1b7482015-04-24 15:26:15 -04003360 return 0;
3361 }
Dr. Stephen Henson35b0ea42009-12-27 23:24:45 +00003362
Rich Salz7644a9a2015-12-16 16:12:24 -05003363 sess->id = OPENSSL_memdup(SSL_SESSION_get_id(session, NULL), sess->idlen);
Rich Salz68dc6822015-04-30 17:48:31 -04003364 sess->der = app_malloc(sess->derlen, "get session buffer");
3365 if (!sess->id) {
Rich Salz7e1b7482015-04-24 15:26:15 -04003366 BIO_printf(bio_err, "Out of memory adding to external cache\n");
Rich Salza194ee72015-04-25 22:55:36 -04003367 OPENSSL_free(sess->id);
3368 OPENSSL_free(sess->der);
Matt Caswell918bb862015-03-04 17:49:51 +00003369 OPENSSL_free(sess);
3370 return 0;
3371 }
Matt Caswell0f113f32015-01-22 03:40:55 +00003372 p = sess->der;
Rich Salz7e1b7482015-04-24 15:26:15 -04003373
3374 /* Assume it still works. */
3375 if (i2d_SSL_SESSION(session, &p) != sess->derlen) {
Rich Salzce6766d2015-04-26 16:43:18 -04003376 BIO_printf(bio_err, "Unexpected session encoding length\n");
Rich Salza194ee72015-04-25 22:55:36 -04003377 OPENSSL_free(sess->id);
3378 OPENSSL_free(sess->der);
3379 OPENSSL_free(sess);
Matt Caswellac59d702015-03-06 14:39:46 +00003380 return 0;
3381 }
Dr. Stephen Henson35b0ea42009-12-27 23:24:45 +00003382
Matt Caswell0f113f32015-01-22 03:40:55 +00003383 sess->next = first;
3384 first = sess;
3385 BIO_printf(bio_err, "New session added to external cache\n");
3386 return 0;
3387}
Dr. Stephen Henson35b0ea42009-12-27 23:24:45 +00003388
Emilia Kasperb6981742016-02-01 15:26:18 +01003389static SSL_SESSION *get_session(SSL *ssl, const unsigned char *id, int idlen,
Matt Caswell0f113f32015-01-22 03:40:55 +00003390 int *do_copy)
3391{
3392 simple_ssl_session *sess;
3393 *do_copy = 0;
3394 for (sess = first; sess; sess = sess->next) {
3395 if (idlen == (int)sess->idlen && !memcmp(sess->id, id, idlen)) {
3396 const unsigned char *p = sess->der;
3397 BIO_printf(bio_err, "Lookup session: cache hit\n");
3398 return d2i_SSL_SESSION(NULL, &p, sess->derlen);
3399 }
3400 }
3401 BIO_printf(bio_err, "Lookup session: cache miss\n");
3402 return NULL;
3403}
Dr. Stephen Henson35b0ea42009-12-27 23:24:45 +00003404
3405static void del_session(SSL_CTX *sctx, SSL_SESSION *session)
Matt Caswell0f113f32015-01-22 03:40:55 +00003406{
3407 simple_ssl_session *sess, *prev = NULL;
3408 const unsigned char *id;
3409 unsigned int idlen;
3410 id = SSL_SESSION_get_id(session, &idlen);
3411 for (sess = first; sess; sess = sess->next) {
3412 if (idlen == sess->idlen && !memcmp(sess->id, id, idlen)) {
3413 if (prev)
3414 prev->next = sess->next;
3415 else
3416 first = sess->next;
3417 OPENSSL_free(sess->id);
3418 OPENSSL_free(sess->der);
3419 OPENSSL_free(sess);
3420 return;
3421 }
3422 prev = sess;
3423 }
3424}
Dr. Stephen Henson35b0ea42009-12-27 23:24:45 +00003425
3426static void init_session_cache_ctx(SSL_CTX *sctx)
Matt Caswell0f113f32015-01-22 03:40:55 +00003427{
3428 SSL_CTX_set_session_cache_mode(sctx,
3429 SSL_SESS_CACHE_NO_INTERNAL |
3430 SSL_SESS_CACHE_SERVER);
3431 SSL_CTX_sess_set_new_cb(sctx, add_session);
3432 SSL_CTX_sess_set_get_cb(sctx, get_session);
3433 SSL_CTX_sess_set_remove_cb(sctx, del_session);
3434}
Dr. Stephen Henson35b0ea42009-12-27 23:24:45 +00003435
3436static void free_sessions(void)
Matt Caswell0f113f32015-01-22 03:40:55 +00003437{
3438 simple_ssl_session *sess, *tsess;
3439 for (sess = first; sess;) {
3440 OPENSSL_free(sess->id);
3441 OPENSSL_free(sess->der);
3442 tsess = sess;
3443 sess = sess->next;
3444 OPENSSL_free(tsess);
3445 }
3446 first = NULL;
3447}
Matt Caswellf9e55032016-03-21 15:32:40 +00003448
FdaSilvaYYd6073e22016-08-07 12:04:26 +02003449#endif /* OPENSSL_NO_SOCK */