]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/s_server.c
Fix some conversion from size_t to const int errors
[thirdparty/openssl.git] / apps / s_server.c
CommitLineData
846e33c7 1/*
0c679f55 2 * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
aa8f3d76 3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
c80149d9 4 * Copyright 2005 Nokia. All rights reserved.
a661b653 5 *
dffa7520 6 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
7 * this file except in compliance with the License. You can obtain a copy
8 * in the file LICENSE in the source distribution or at
9 * https://www.openssl.org/source/license.html
a661b653 10 */
846e33c7 11
23b795d3
F
12#include "internal/e_os.h"
13
ddac1974 14#include <ctype.h>
8c197cc5
UM
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
54463e4f
F
18#if defined(_WIN32)
19/* Included before async.h to avoid some warnings */
20# include <windows.h>
21#endif
4d8743f4 22
be1bd923 23#include <openssl/e_os2.h>
54463e4f
F
24#include <openssl/async.h>
25#include <openssl/ssl.h>
163f6dc1 26#include <openssl/decoder.h>
23b795d3 27#include "internal/sockets.h" /* for openssl_fdset() */
8c197cc5 28
f9e55034
MC
29#ifndef OPENSSL_NO_SOCK
30
0f113f3e
MC
31/*
32 * With IPv6, it looks like Digital has mixed up the proper order of
33 * recursive header file inclusion, resulting in the compiler complaining
34 * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
35 * needed to have fileno() declared correctly... So let's define u_int
36 */
bc36ee62 37#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
0f113f3e 38# define __U_INT
7d7d2cbc
UM
39typedef unsigned int u_int;
40#endif
41
ec577822 42#include <openssl/bn.h>
d02b48c6 43#include "apps.h"
dab2cd68 44#include "progs.h"
ec577822
BM
45#include <openssl/err.h>
46#include <openssl/pem.h>
47#include <openssl/x509.h>
1372965e 48#include <openssl/rand.h>
67c8e7f4 49#include <openssl/ocsp.h>
3eeaab4b 50#ifndef OPENSSL_NO_DH
0f113f3e 51# include <openssl/dh.h>
3eeaab4b 52#endif
3a1ee3c1 53#include <openssl/rsa.h>
d02b48c6 54#include "s_apps.h"
36d16f8e 55#include "timeouts.h"
5fd1478d
MC
56#ifdef CHARSET_EBCDIC
57#include <openssl/ebcdic.h>
58#endif
0e97f1e1 59#include "internal/sockets.h"
d02b48c6 60
7c2d4fee 61static int not_resumable_sess_cb(SSL *s, int is_forward_secure);
72d0bc84
MC
62static int sv_body(int s, int stype, int prot, unsigned char *context);
63static int www_body(int s, int stype, int prot, unsigned char *context);
64static int rev_body(int s, int stype, int prot, unsigned char *context);
0f113f3e 65static void close_accept_socket(void);
d02b48c6 66static int init_ssl_connection(SSL *s);
0f113f3e 67static void print_stats(BIO *bp, SSL_CTX *ctx);
ae3947de 68static int generate_session_id(SSL *ssl, unsigned char *id,
0f113f3e 69 unsigned int *id_len);
35b0ea4e
DSH
70static void init_session_cache_ctx(SSL_CTX *sctx);
71static void free_sessions(void);
ade1e888 72static void print_connection_info(SSL *con);
ea262260 73
d6073e27 74static const int bufsize = 16 * 1024;
0f113f3e 75static int accept_socket = -1;
d02b48c6 76
0f113f3e 77#define TEST_CERT "server.pem"
e481f9b9 78#define TEST_CERT2 "server2.pem"
d02b48c6 79
0f113f3e 80static int s_nbio = 0;
0f113f3e 81static int s_nbio_test = 0;
df2ee0e2 82static int s_crlf = 0;
0f113f3e 83static SSL_CTX *ctx = NULL;
0f113f3e 84static SSL_CTX *ctx2 = NULL;
0f113f3e 85static int www = 0;
d02b48c6 86
0f113f3e 87static BIO *bio_s_out = NULL;
93ab9e42 88static BIO *bio_s_msg = NULL;
0f113f3e 89static int s_debug = 0;
0f113f3e 90static int s_tlsextdebug = 0;
0f113f3e
MC
91static int s_msg = 0;
92static int s_quiet = 0;
93static int s_ign_eof = 0;
94static int s_brief = 0;
d02b48c6 95
0f113f3e
MC
96static char *keymatexportlabel = NULL;
97static int keymatexportlen = 20;
e0af0405 98
7e25dd6d
MC
99static int async = 0;
100
cd81ac7b 101static int use_sendfile = 0;
cd715b7e 102static int use_zc_sendfile = 0;
cd81ac7b 103
0f113f3e 104static const char *session_id_prefix = NULL;
b74ba295 105
3c95ef22
TS
106static const unsigned char cert_type_rpk[] = { TLSEXT_cert_type_rpk, TLSEXT_cert_type_x509 };
107static int enable_client_rpk = 0;
108
a7a14a23 109#ifndef OPENSSL_NO_DTLS
36d16f8e 110static int enable_timeouts = 0;
b1277b99 111static long socket_mtu;
f2ff1432 112#endif
36d16f8e 113
9998b32c
MC
114/*
115 * We define this but make it always be 0 in no-dtls builds to simplify the
116 * code.
117 */
118static int dtlslisten = 0;
c2f9648d 119static int stateless = 0;
9998b32c 120
593a2aa3 121static int early_data = 0;
df894947 122static SSL_SESSION *psksess = NULL;
593a2aa3 123
720b6cbe 124static char *psk_identity = "Client_identity";
0f113f3e 125char *psk_key = NULL; /* by default PSK is not used */
ddac1974 126
c3be39f2
LZ
127static char http_server_binmode = 0; /* for now: 0/1 = default/binary */
128
14e35350 129#ifndef OPENSSL_NO_PSK
ddac1974 130static unsigned int psk_server_cb(SSL *ssl, const char *identity,
0f113f3e
MC
131 unsigned char *psk,
132 unsigned int max_psk_len)
133{
6ec6d520
DSH
134 long key_len = 0;
135 unsigned char *key;
0f113f3e
MC
136
137 if (s_debug)
138 BIO_printf(bio_s_out, "psk_server_cb\n");
0007ff25 139
8b09a9c7 140 if (!SSL_is_dtls(ssl) && SSL_version(ssl) >= TLS1_3_VERSION) {
0007ff25 141 /*
8b09a9c7
MC
142 * This callback is designed for use in (D)TLSv1.2 (or below). It is
143 * possible to use a single callback for all protocol versions - but it
144 * is preferred to use a dedicated callback for TLSv1.3. For TLSv1.3 we
145 * have psk_find_session_cb.
0007ff25
MC
146 */
147 return 0;
148 }
149
2234212c 150 if (identity == NULL) {
0f113f3e
MC
151 BIO_printf(bio_err, "Error: client did not send PSK identity\n");
152 goto out_err;
153 }
154 if (s_debug)
155 BIO_printf(bio_s_out, "identity_len=%d identity=%s\n",
11abf922 156 (int)strlen(identity), identity);
0f113f3e
MC
157
158 /* here we could lookup the given identity e.g. from a database */
159 if (strcmp(identity, psk_identity) != 0) {
720b6cbe 160 BIO_printf(bio_s_out, "PSK warning: client identity not what we expected"
0f113f3e 161 " (got '%s' expected '%s')\n", identity, psk_identity);
720b6cbe
DKG
162 } else {
163 if (s_debug)
0f113f3e 164 BIO_printf(bio_s_out, "PSK client identity found\n");
720b6cbe 165 }
0f113f3e
MC
166
167 /* convert the PSK key to binary */
6ec6d520
DSH
168 key = OPENSSL_hexstr2buf(psk_key, &key_len);
169 if (key == NULL) {
170 BIO_printf(bio_err, "Could not convert PSK key '%s' to buffer\n",
0f113f3e 171 psk_key);
0f113f3e
MC
172 return 0;
173 }
6ec6d520 174 if (key_len > (int)max_psk_len) {
0f113f3e 175 BIO_printf(bio_err,
6ec6d520
DSH
176 "psk buffer of callback is too small (%d) for key (%ld)\n",
177 max_psk_len, key_len);
178 OPENSSL_free(key);
0f113f3e
MC
179 return 0;
180 }
181
6ec6d520
DSH
182 memcpy(psk, key, key_len);
183 OPENSSL_free(key);
0f113f3e
MC
184
185 if (s_debug)
6ec6d520
DSH
186 BIO_printf(bio_s_out, "fetched PSK len=%ld\n", key_len);
187 return key_len;
ddac1974 188 out_err:
0f113f3e
MC
189 if (s_debug)
190 BIO_printf(bio_err, "Error in PSK server callback\n");
c54cc2b1
RS
191 (void)BIO_flush(bio_err);
192 (void)BIO_flush(bio_s_out);
0f113f3e
MC
193 return 0;
194}
ddac1974 195#endif
36d16f8e 196
df894947
MC
197static int psk_find_session_cb(SSL *ssl, const unsigned char *identity,
198 size_t identity_len, SSL_SESSION **sess)
199{
5ffff599
MC
200 SSL_SESSION *tmpsess = NULL;
201 unsigned char *key;
202 long key_len;
203 const SSL_CIPHER *cipher = NULL;
204
df894947 205 if (strlen(psk_identity) != identity_len
c9d6fdd6 206 || memcmp(psk_identity, identity, identity_len) != 0) {
2d015189
MC
207 *sess = NULL;
208 return 1;
c9d6fdd6 209 }
df894947 210
5ffff599 211 if (psksess != NULL) {
00fbc969
FWH
212 if (!SSL_SESSION_up_ref(psksess))
213 return 0;
214
5ffff599
MC
215 *sess = psksess;
216 return 1;
217 }
218
219 key = OPENSSL_hexstr2buf(psk_key, &key_len);
220 if (key == NULL) {
221 BIO_printf(bio_err, "Could not convert PSK key '%s' to buffer\n",
222 psk_key);
223 return 0;
224 }
225
e73c6eae
MC
226 /* We default to SHA256 */
227 cipher = SSL_CIPHER_find(ssl, tls13_aes128gcmsha256_id);
5ffff599 228 if (cipher == NULL) {
e73c6eae 229 BIO_printf(bio_err, "Error finding suitable ciphersuite\n");
fbccfedf 230 OPENSSL_free(key);
5ffff599
MC
231 return 0;
232 }
233
234 tmpsess = SSL_SESSION_new();
235 if (tmpsess == NULL
236 || !SSL_SESSION_set1_master_key(tmpsess, key, key_len)
237 || !SSL_SESSION_set_cipher(tmpsess, cipher)
238 || !SSL_SESSION_set_protocol_version(tmpsess, SSL_version(ssl))) {
239 OPENSSL_free(key);
8e2552b1 240 SSL_SESSION_free(tmpsess);
5ffff599
MC
241 return 0;
242 }
243 OPENSSL_free(key);
244 *sess = tmpsess;
df894947
MC
245
246 return 1;
247}
248
edc032b5 249#ifndef OPENSSL_NO_SRP
1fb6b0bf 250static srpsrvparm srp_callback_parm;
edc032b5
BL
251#endif
252
0f113f3e 253static int local_argc = 0;
d02b48c6 254static char **local_argv;
d02b48c6 255
a53955d8
UM
256#ifdef CHARSET_EBCDIC
257static int ebcdic_new(BIO *bi);
258static int ebcdic_free(BIO *a);
259static int ebcdic_read(BIO *b, char *out, int outl);
0fd05a2f
BM
260static int ebcdic_write(BIO *b, const char *in, int inl);
261static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr);
a53955d8 262static int ebcdic_gets(BIO *bp, char *buf, int size);
0fd05a2f 263static int ebcdic_puts(BIO *bp, const char *str);
a53955d8 264
0f113f3e 265# define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
5fd1478d 266static BIO_METHOD *methods_ebcdic = NULL;
0f113f3e 267
68dc6824 268/* This struct is "unwarranted chumminess with the compiler." */
0f113f3e
MC
269typedef struct {
270 size_t alloced;
271 char buff[1];
a53955d8
UM
272} EBCDIC_OUTBUFF;
273
e22ebb89 274static const BIO_METHOD *BIO_f_ebcdic_filter(void)
a53955d8 275{
5fd1478d
MC
276 if (methods_ebcdic == NULL) {
277 methods_ebcdic = BIO_meth_new(BIO_TYPE_EBCDIC_FILTER,
d6073e27
F
278 "EBCDIC/ASCII filter");
279 if (methods_ebcdic == NULL
5fd1478d
MC
280 || !BIO_meth_set_write(methods_ebcdic, ebcdic_write)
281 || !BIO_meth_set_read(methods_ebcdic, ebcdic_read)
282 || !BIO_meth_set_puts(methods_ebcdic, ebcdic_puts)
283 || !BIO_meth_set_gets(methods_ebcdic, ebcdic_gets)
284 || !BIO_meth_set_ctrl(methods_ebcdic, ebcdic_ctrl)
285 || !BIO_meth_set_create(methods_ebcdic, ebcdic_new)
286 || !BIO_meth_set_destroy(methods_ebcdic, ebcdic_free))
287 return NULL;
288 }
289 return methods_ebcdic;
a53955d8
UM
290}
291
292static int ebcdic_new(BIO *bi)
293{
0f113f3e 294 EBCDIC_OUTBUFF *wbuf;
a53955d8 295
b4faea50 296 wbuf = app_malloc(sizeof(*wbuf) + 1024, "ebcdic wbuf");
0f113f3e
MC
297 wbuf->alloced = 1024;
298 wbuf->buff[0] = '\0';
a53955d8 299
5fd1478d
MC
300 BIO_set_data(bi, wbuf);
301 BIO_set_init(bi, 1);
302 return 1;
a53955d8
UM
303}
304
305static int ebcdic_free(BIO *a)
306{
5fd1478d
MC
307 EBCDIC_OUTBUFF *wbuf;
308
0f113f3e 309 if (a == NULL)
5fd1478d
MC
310 return 0;
311 wbuf = BIO_get_data(a);
312 OPENSSL_free(wbuf);
313 BIO_set_data(a, NULL);
314 BIO_set_init(a, 0);
315
316 return 1;
a53955d8 317}
0f113f3e 318
a53955d8
UM
319static int ebcdic_read(BIO *b, char *out, int outl)
320{
0f113f3e 321 int ret = 0;
5fd1478d 322 BIO *next = BIO_next(b);
a53955d8 323
0f113f3e 324 if (out == NULL || outl == 0)
26a7d938 325 return 0;
5fd1478d 326 if (next == NULL)
26a7d938 327 return 0;
a53955d8 328
5fd1478d 329 ret = BIO_read(next, out, outl);
0f113f3e
MC
330 if (ret > 0)
331 ascii2ebcdic(out, out, ret);
5fd1478d 332 return ret;
a53955d8
UM
333}
334
0fd05a2f 335static int ebcdic_write(BIO *b, const char *in, int inl)
a53955d8 336{
0f113f3e 337 EBCDIC_OUTBUFF *wbuf;
5fd1478d 338 BIO *next = BIO_next(b);
0f113f3e
MC
339 int ret = 0;
340 int num;
a53955d8 341
0f113f3e 342 if ((in == NULL) || (inl <= 0))
26a7d938 343 return 0;
5fd1478d
MC
344 if (next == NULL)
345 return 0;
a53955d8 346
5fd1478d 347 wbuf = (EBCDIC_OUTBUFF *) BIO_get_data(b);
a53955d8 348
0f113f3e
MC
349 if (inl > (num = wbuf->alloced)) {
350 num = num + num; /* double the size */
351 if (num < inl)
352 num = inl;
5fd1478d 353 OPENSSL_free(wbuf);
b4faea50 354 wbuf = app_malloc(sizeof(*wbuf) + num, "grow ebcdic wbuf");
a53955d8 355
0f113f3e
MC
356 wbuf->alloced = num;
357 wbuf->buff[0] = '\0';
a53955d8 358
5fd1478d 359 BIO_set_data(b, wbuf);
0f113f3e 360 }
a53955d8 361
0f113f3e 362 ebcdic2ascii(wbuf->buff, in, inl);
a53955d8 363
5fd1478d 364 ret = BIO_write(next, wbuf->buff, inl);
a53955d8 365
26a7d938 366 return ret;
a53955d8
UM
367}
368
0fd05a2f 369static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr)
a53955d8 370{
0f113f3e 371 long ret;
5fd1478d 372 BIO *next = BIO_next(b);
0f113f3e 373
5fd1478d 374 if (next == NULL)
26a7d938 375 return 0;
0f113f3e
MC
376 switch (cmd) {
377 case BIO_CTRL_DUP:
378 ret = 0L;
379 break;
380 default:
5fd1478d 381 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
382 break;
383 }
26a7d938 384 return ret;
a53955d8
UM
385}
386
387static int ebcdic_gets(BIO *bp, char *buf, int size)
388{
0f113f3e 389 int i, ret = 0;
5fd1478d
MC
390 BIO *next = BIO_next(bp);
391
392 if (next == NULL)
393 return 0;
0f113f3e
MC
394/* return(BIO_gets(bp->next_bio,buf,size));*/
395 for (i = 0; i < size - 1; ++i) {
396 ret = ebcdic_read(bp, &buf[i], 1);
397 if (ret <= 0)
398 break;
399 else if (buf[i] == '\n') {
400 ++i;
401 break;
402 }
403 }
404 if (i < size)
405 buf[i] = '\0';
406 return (ret < 0 && i == 0) ? ret : i;
a53955d8
UM
407}
408
0fd05a2f 409static int ebcdic_puts(BIO *bp, const char *str)
a53955d8 410{
5fd1478d
MC
411 if (BIO_next(bp) == NULL)
412 return 0;
0f113f3e 413 return ebcdic_write(bp, str, strlen(str));
a53955d8
UM
414}
415#endif
416
ed3883d2
BM
417/* This is a context that we pass to callbacks */
418typedef struct tlsextctx_st {
0f113f3e
MC
419 char *servername;
420 BIO *biodebug;
421 int extension_error;
ed3883d2
BM
422} tlsextctx;
423
6d23cf97 424static int ssl_servername_cb(SSL *s, int *ad, void *arg)
0f113f3e
MC
425{
426 tlsextctx *p = (tlsextctx *) arg;
427 const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
0d68367a
RS
428
429 if (servername != NULL && p->biodebug != NULL) {
430 const char *cp = servername;
431 unsigned char uc;
432
433 BIO_printf(p->biodebug, "Hostname in TLS extension: \"");
434 while ((uc = *cp++) != 0)
435 BIO_printf(p->biodebug,
17860288 436 (((uc) & ~127) == 0) && isprint(uc) ? "%c" : "\\x%02x", uc);
0d68367a
RS
437 BIO_printf(p->biodebug, "\"\n");
438 }
0f113f3e 439
2234212c 440 if (p->servername == NULL)
0f113f3e
MC
441 return SSL_TLSEXT_ERR_NOACK;
442
2234212c 443 if (servername != NULL) {
fba140c7 444 if (OPENSSL_strcasecmp(servername, p->servername))
0f113f3e 445 return p->extension_error;
2234212c 446 if (ctx2 != NULL) {
0f113f3e
MC
447 BIO_printf(p->biodebug, "Switching server context.\n");
448 SSL_set_SSL_CTX(s, ctx2);
449 }
450 }
451 return SSL_TLSEXT_ERR_OK;
ed3883d2 452}
67c8e7f4
DSH
453
454/* Structure passed to cert status callback */
67c8e7f4 455typedef struct tlsextstatusctx_st {
f5ca0b04 456 int timeout;
acf65ae5
MC
457 /* File to load OCSP Response from (or NULL if no file) */
458 char *respin;
0f113f3e
MC
459 /* Default responder to use */
460 char *host, *path, *port;
80a4ac57 461 char *proxy, *no_proxy;
0f113f3e 462 int use_ssl;
0f113f3e 463 int verbose;
67c8e7f4
DSH
464} tlsextstatusctx;
465
f5ca0b04 466static tlsextstatusctx tlscstatp = { -1 };
67c8e7f4 467
3e41ac35 468#ifndef OPENSSL_NO_OCSP
acf65ae5 469
0f113f3e 470/*
acf65ae5
MC
471 * Helper function to get an OCSP_RESPONSE from a responder. This is a
472 * simplified version. It examines certificates each time and makes one OCSP
473 * responder query for each request. A full version would store details such as
474 * the OCSP certificate IDs and minimise the number of OCSP responses by caching
475 * them until they were considered "expired".
67c8e7f4 476 */
acf65ae5
MC
477static int get_ocsp_resp_from_responder(SSL *s, tlsextstatusctx *srctx,
478 OCSP_RESPONSE **resp)
0f113f3e 479{
4c9b0a03 480 char *host = NULL, *port = NULL, *path = NULL;
80a4ac57 481 char *proxy = NULL, *no_proxy = NULL;
0f113f3e 482 int use_ssl;
0f113f3e 483 STACK_OF(OPENSSL_STRING) *aia = NULL;
d6aafeb1 484 X509 *x = NULL, *cert;
485 X509_NAME *iname;
486 STACK_OF(X509) *chain = NULL;
487 SSL_CTX *ssl_ctx;
f0e0fd51
RS
488 X509_STORE_CTX *inctx = NULL;
489 X509_OBJECT *obj;
0f113f3e 490 OCSP_REQUEST *req = NULL;
0f113f3e
MC
491 OCSP_CERTID *id = NULL;
492 STACK_OF(X509_EXTENSION) *exts;
493 int ret = SSL_TLSEXT_ERR_NOACK;
494 int i;
7e1b7485 495
0f113f3e
MC
496 /* Build up OCSP query from server certificate */
497 x = SSL_get_certificate(s);
d6aafeb1 498 iname = X509_get_issuer_name(x);
0f113f3e 499 aia = X509_get1_ocsp(x);
2234212c 500 if (aia != NULL) {
7932982b
DDO
501 if (!OSSL_HTTP_parse_url(sk_OPENSSL_STRING_value(aia, 0), &use_ssl,
502 NULL, &host, &port, NULL, &path, NULL, NULL)) {
7e1b7485 503 BIO_puts(bio_err, "cert_status: can't parse AIA URL\n");
0f113f3e
MC
504 goto err;
505 }
506 if (srctx->verbose)
7e1b7485 507 BIO_printf(bio_err, "cert_status: AIA URL: %s\n",
0f113f3e
MC
508 sk_OPENSSL_STRING_value(aia, 0));
509 } else {
2234212c 510 if (srctx->host == NULL) {
7e1b7485 511 BIO_puts(bio_err,
0f113f3e
MC
512 "cert_status: no AIA and no default responder URL\n");
513 goto done;
514 }
515 host = srctx->host;
516 path = srctx->path;
517 port = srctx->port;
518 use_ssl = srctx->use_ssl;
519 }
80a4ac57
DDO
520 proxy = srctx->proxy;
521 no_proxy = srctx->no_proxy;
0f113f3e 522
d6aafeb1 523 ssl_ctx = SSL_get_SSL_CTX(s);
524 if (!SSL_CTX_get0_chain_certs(ssl_ctx, &chain))
f0e0fd51 525 goto err;
d6aafeb1 526 for (i = 0; i < sk_X509_num(chain); i++) {
527 /* check the untrusted certificate chain (-cert_chain option) */
528 cert = sk_X509_value(chain, i);
529 if (X509_name_cmp(iname, X509_get_subject_name(cert)) == 0) {
530 /* the issuer certificate is found */
531 id = OCSP_cert_to_id(NULL, x, cert);
532 break;
533 }
534 }
535 if (id == NULL) {
536 inctx = X509_STORE_CTX_new();
537 if (inctx == NULL)
538 goto err;
539 if (!X509_STORE_CTX_init(inctx, SSL_CTX_get_cert_store(ssl_ctx),
540 NULL, NULL))
541 goto err;
542 obj = X509_STORE_CTX_get_obj_by_subject(inctx, X509_LU_X509, iname);
543 if (obj == NULL) {
544 BIO_puts(bio_err, "cert_status: Can't retrieve issuer certificate.\n");
545 goto done;
546 }
547 id = OCSP_cert_to_id(NULL, x, X509_OBJECT_get0_X509(obj));
548 X509_OBJECT_free(obj);
0f113f3e 549 }
2234212c 550 if (id == NULL)
0f113f3e 551 goto err;
0461b7ea
MC
552 req = OCSP_REQUEST_new();
553 if (req == NULL)
554 goto err;
0f113f3e
MC
555 if (!OCSP_request_add0_id(req, id))
556 goto err;
557 id = NULL;
558 /* Add any extensions to the request */
559 SSL_get_tlsext_status_exts(s, &exts);
560 for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
561 X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
562 if (!OCSP_REQUEST_add_ext(req, ext, -1))
563 goto err;
564 }
88d96983
DDO
565 *resp = process_responder(req, host, port, path, proxy, no_proxy,
566 use_ssl, NULL /* headers */, srctx->timeout);
acf65ae5 567 if (*resp == NULL) {
7e1b7485 568 BIO_puts(bio_err, "cert_status: error querying responder\n");
0f113f3e
MC
569 goto done;
570 }
acf65ae5 571
0f113f3e 572 ret = SSL_TLSEXT_ERR_OK;
f0e0fd51
RS
573 goto done;
574
575 err:
576 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
0f113f3e 577 done:
6530c490
MC
578 /*
579 * If we parsed aia we need to free; otherwise they were copied and we
580 * don't
581 */
f5ca0b04 582 if (aia != NULL) {
0f113f3e
MC
583 OPENSSL_free(host);
584 OPENSSL_free(path);
585 OPENSSL_free(port);
586 X509_email_free(aia);
587 }
25aaa98a
RS
588 OCSP_CERTID_free(id);
589 OCSP_REQUEST_free(req);
f0e0fd51 590 X509_STORE_CTX_free(inctx);
0f113f3e 591 return ret;
0f113f3e 592}
acf65ae5
MC
593
594/*
595 * Certificate Status callback. This is called when a client includes a
596 * certificate status request extension. The response is either obtained from a
597 * file, or from an OCSP responder.
598 */
599static int cert_status_cb(SSL *s, void *arg)
600{
601 tlsextstatusctx *srctx = arg;
602 OCSP_RESPONSE *resp = NULL;
603 unsigned char *rspder = NULL;
604 int rspderlen;
605 int ret = SSL_TLSEXT_ERR_ALERT_FATAL;
606
607 if (srctx->verbose)
608 BIO_puts(bio_err, "cert_status: callback called\n");
609
610 if (srctx->respin != NULL) {
611 BIO *derbio = bio_open_default(srctx->respin, 'r', FORMAT_ASN1);
612 if (derbio == NULL) {
613 BIO_puts(bio_err, "cert_status: Cannot open OCSP response file\n");
614 goto err;
615 }
616 resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
617 BIO_free(derbio);
f5ca0b04 618 if (resp == NULL) {
acf65ae5
MC
619 BIO_puts(bio_err, "cert_status: Error reading OCSP response\n");
620 goto err;
621 }
622 } else {
623 ret = get_ocsp_resp_from_responder(s, srctx, &resp);
624 if (ret != SSL_TLSEXT_ERR_OK)
625 goto err;
626 }
627
628 rspderlen = i2d_OCSP_RESPONSE(resp, &rspder);
629 if (rspderlen <= 0)
630 goto err;
631
632 SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen);
633 if (srctx->verbose) {
634 BIO_puts(bio_err, "cert_status: ocsp response sent:\n");
635 OCSP_RESPONSE_print(bio_err, resp, 2);
636 }
637
638 ret = SSL_TLSEXT_ERR_OK;
639
640 err:
641 if (ret != SSL_TLSEXT_ERR_OK)
642 ERR_print_errors(bio_err);
643
644 OCSP_RESPONSE_free(resp);
645
646 return ret;
647}
3e41ac35 648#endif
ee2ffc27 649
e481f9b9 650#ifndef OPENSSL_NO_NEXTPROTONEG
ee2ffc27
BL
651/* This is the context that we pass to next_proto_cb */
652typedef struct tlsextnextprotoctx_st {
0f113f3e 653 unsigned char *data;
f2ff1432 654 size_t len;
ee2ffc27
BL
655} tlsextnextprotoctx;
656
0f113f3e
MC
657static int next_proto_cb(SSL *s, const unsigned char **data,
658 unsigned int *len, void *arg)
659{
660 tlsextnextprotoctx *next_proto = arg;
ee2ffc27 661
0f113f3e 662 *data = next_proto->data;
c62cd07d 663 *len = (unsigned int)next_proto->len;
ee2ffc27 664
0f113f3e
MC
665 return SSL_TLSEXT_ERR_OK;
666}
e481f9b9 667#endif /* ndef OPENSSL_NO_NEXTPROTONEG */
6f017a8f
AL
668
669/* This the context that we pass to alpn_cb */
670typedef struct tlsextalpnctx_st {
0f113f3e 671 unsigned char *data;
817cd0d5 672 size_t len;
6f017a8f
AL
673} tlsextalpnctx;
674
0f113f3e
MC
675static int alpn_cb(SSL *s, const unsigned char **out, unsigned char *outlen,
676 const unsigned char *in, unsigned int inlen, void *arg)
677{
678 tlsextalpnctx *alpn_ctx = arg;
679
680 if (!s_quiet) {
681 /* We can assume that |in| is syntactically valid. */
817cd0d5 682 unsigned int i;
0f113f3e
MC
683 BIO_printf(bio_s_out, "ALPN protocols advertised by the client: ");
684 for (i = 0; i < inlen;) {
685 if (i)
686 BIO_write(bio_s_out, ", ", 2);
687 BIO_write(bio_s_out, &in[i + 1], in[i]);
688 i += in[i] + 1;
689 }
690 BIO_write(bio_s_out, "\n", 1);
691 }
692
693 if (SSL_select_next_proto
c62cd07d
TM
694 ((unsigned char **)out, outlen, alpn_ctx->data,
695 (unsigned int)alpn_ctx->len, in, inlen) != OPENSSL_NPN_NEGOTIATED) {
9e885a70 696 return SSL_TLSEXT_ERR_ALERT_FATAL;
0f113f3e
MC
697 }
698
699 if (!s_quiet) {
700 BIO_printf(bio_s_out, "ALPN protocols selected: ");
701 BIO_write(bio_s_out, *out, *outlen);
702 BIO_write(bio_s_out, "\n", 1);
703 }
704
705 return SSL_TLSEXT_ERR_OK;
706}
ed3883d2 707
7c2d4fee 708static int not_resumable_sess_cb(SSL *s, int is_forward_secure)
0f113f3e
MC
709{
710 /* disable resumption for sessions with forward secure ciphers */
711 return is_forward_secure;
712}
7c2d4fee 713
7e1b7485 714typedef enum OPTION_choice {
b0f96018
DDO
715 OPT_COMMON,
716 OPT_ENGINE,
ab69ac00 717 OPT_4, OPT_6, OPT_ACCEPT, OPT_PORT, OPT_UNIX, OPT_UNLINK, OPT_NACCEPT,
a7c04f2b 718 OPT_VERIFY, OPT_NAMEOPT, OPT_UPPER_V_VERIFY, OPT_CONTEXT, OPT_CERT, OPT_CRL,
7e1b7485
RS
719 OPT_CRL_DOWNLOAD, OPT_SERVERINFO, OPT_CERTFORM, OPT_KEY, OPT_KEYFORM,
720 OPT_PASS, OPT_CERT_CHAIN, OPT_DHPARAM, OPT_DCERTFORM, OPT_DCERT,
721 OPT_DKEYFORM, OPT_DPASS, OPT_DKEY, OPT_DCERT_CHAIN, OPT_NOCERT,
2b6bcb70 722 OPT_CAPATH, OPT_NOCAPATH, OPT_CHAINCAPATH, OPT_VERIFYCAPATH, OPT_NO_CACHE,
7e1b7485 723 OPT_EXT_CACHE, OPT_CRLFORM, OPT_VERIFY_RET_ERROR, OPT_VERIFY_QUIET,
2b6bcb70 724 OPT_BUILD_CHAIN, OPT_CAFILE, OPT_NOCAFILE, OPT_CHAINCAFILE,
fd3397fc
RL
725 OPT_VERIFYCAFILE,
726 OPT_CASTORE, OPT_NOCASTORE, OPT_CHAINCASTORE, OPT_VERIFYCASTORE,
727 OPT_NBIO, OPT_NBIO_TEST, OPT_IGN_EOF, OPT_NO_IGN_EOF,
2b6bcb70 728 OPT_DEBUG, OPT_TLSEXTDEBUG, OPT_STATUS, OPT_STATUS_VERBOSE,
80a4ac57
DDO
729 OPT_STATUS_TIMEOUT, OPT_PROXY, OPT_NO_PROXY, OPT_STATUS_URL,
730 OPT_STATUS_FILE, OPT_MSG, OPT_MSGFILE,
acf65ae5
MC
731 OPT_TRACE, OPT_SECURITY_DEBUG, OPT_SECURITY_DEBUG_VERBOSE, OPT_STATE,
732 OPT_CRLF, OPT_QUIET, OPT_BRIEF, OPT_NO_DHE,
df894947
MC
733 OPT_NO_RESUME_EPHEMERAL, OPT_PSK_IDENTITY, OPT_PSK_HINT, OPT_PSK,
734 OPT_PSK_SESS, OPT_SRPVFILE, OPT_SRPUSERSEED, OPT_REV, OPT_WWW,
735 OPT_UPPER_WWW, OPT_HTTP, OPT_ASYNC, OPT_SSL_CONFIG,
28e5ea88 736 OPT_MAX_SEND_FRAG, OPT_SPLIT_SEND_FRAG, OPT_MAX_PIPELINES, OPT_READ_BUF,
582a17d6 737 OPT_SSL3, OPT_TLS1_3, OPT_TLS1_2, OPT_TLS1_1, OPT_TLS1, OPT_DTLS, OPT_DTLS1,
c2f9648d 738 OPT_DTLS1_2, OPT_SCTP, OPT_TIMEOUT, OPT_MTU, OPT_LISTEN, OPT_STATELESS,
3ee1eac2 739 OPT_ID_PREFIX, OPT_SERVERNAME, OPT_SERVERNAME_FATAL,
cd81ac7b 740 OPT_CERT2, OPT_KEY2, OPT_NEXTPROTONEG, OPT_ALPN, OPT_SENDFILE,
7e1b7485 741 OPT_SRTP_PROFILES, OPT_KEYMATEXPORT, OPT_KEYMATEXPORTLEN,
bafe9cf5 742 OPT_KEYLOG_FILE, OPT_MAX_EARLY, OPT_RECV_MAX_EARLY, OPT_EARLY_DATA,
09d62b33 743 OPT_S_NUM_TICKETS, OPT_ANTI_REPLAY, OPT_NO_ANTI_REPLAY, OPT_SCTP_LABEL_BUG,
e2ef7f12 744 OPT_HTTP_SERVER_BINMODE, OPT_NOCANAMES, OPT_IGNORE_UNEXPECTED_EOF, OPT_KTLS,
cd715b7e 745 OPT_USE_ZC_SENDFILE,
b67cb09f 746 OPT_TFO, OPT_CERT_COMP,
3c95ef22
TS
747 OPT_ENABLE_SERVER_RPK,
748 OPT_ENABLE_CLIENT_RPK,
3ee1eac2 749 OPT_R_ENUM,
7e1b7485
RS
750 OPT_S_ENUM,
751 OPT_V_ENUM,
6bd4e3f2
P
752 OPT_X_ENUM,
753 OPT_PROV_ENUM
7e1b7485
RS
754} OPTION_CHOICE;
755
44c83ebd 756const OPTIONS s_server_options[] = {
5388f986 757 OPT_SECTION("General"),
7e1b7485 758 {"help", OPT_HELP, '-', "Display this summary"},
5388f986 759 {"ssl_config", OPT_SSL_CONFIG, 's',
359efeac 760 "Configure SSL_CTX using the given configuration value"},
5388f986
RS
761#ifndef OPENSSL_NO_SSL_TRACE
762 {"trace", OPT_TRACE, '-', "trace protocol messages"},
763#endif
764#ifndef OPENSSL_NO_ENGINE
765 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
766#endif
767
768 OPT_SECTION("Network"),
32eabe34
MR
769 {"port", OPT_PORT, 'p',
770 "TCP/IP port to listen on for connections (default is " PORT ")"},
ab69ac00 771 {"accept", OPT_ACCEPT, 's',
a22f9c84 772 "TCP/IP optional host and port to listen on for connections (default is *:" PORT ")"},
ab69ac00 773#ifdef AF_UNIX
7e1b7485 774 {"unix", OPT_UNIX, 's', "Unix domain socket to accept on"},
5388f986 775 {"unlink", OPT_UNLINK, '-', "For -unix, unlink existing socket first"},
ab69ac00
RL
776#endif
777 {"4", OPT_4, '-', "Use IPv4 only"},
778 {"6", OPT_6, '-', "Use IPv6 only"},
a3e53d56
TS
779#if defined(TCP_FASTOPEN) && !defined(OPENSSL_NO_TFO)
780 {"tfo", OPT_TFO, '-', "Listen for TCP Fast Open connections"},
781#endif
5388f986
RS
782
783 OPT_SECTION("Identity"),
7e1b7485 784 {"context", OPT_CONTEXT, 's', "Set session ID context"},
5388f986
RS
785 {"CAfile", OPT_CAFILE, '<', "PEM format file of CA's"},
786 {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
787 {"CAstore", OPT_CASTORE, ':', "URI to store of CA's"},
788 {"no-CAfile", OPT_NOCAFILE, '-',
789 "Do not load the default certificates file"},
790 {"no-CApath", OPT_NOCAPATH, '-',
791 "Do not load certificates from the default certificates directory"},
792 {"no-CAstore", OPT_NOCASTORE, '-',
793 "Do not load certificates from the default certificates store URI"},
794 {"nocert", OPT_NOCERT, '-', "Don't use any certificates (Anon-DH)"},
7e1b7485
RS
795 {"verify", OPT_VERIFY, 'n', "Turn on peer certificate verification"},
796 {"Verify", OPT_UPPER_V_VERIFY, 'n',
797 "Turn on peer certificate verification, must have a cert"},
2b264aee 798 {"nameopt", OPT_NAMEOPT, 's', "Certificate subject/issuer name printing options"},
2a33470b 799 {"cert", OPT_CERT, '<', "Server certificate file to use; default " TEST_CERT},
5388f986 800 {"cert2", OPT_CERT2, '<',
2a33470b 801 "Certificate file to use for servername; default " TEST_CERT2},
2b264aee 802 {"certform", OPT_CERTFORM, 'F',
6d382c74 803 "Server certificate file format (PEM/DER/P12); has no effect"},
2b264aee
DDO
804 {"cert_chain", OPT_CERT_CHAIN, '<',
805 "Server certificate chain file in PEM format"},
806 {"build_chain", OPT_BUILD_CHAIN, '-', "Build server certificate chain"},
7e1b7485
RS
807 {"serverinfo", OPT_SERVERINFO, 's',
808 "PEM serverinfo file for certificate"},
75c445e4 809 {"key", OPT_KEY, 's',
2b264aee
DDO
810 "Private key file to use; default is -cert file or else" TEST_CERT},
811 {"key2", OPT_KEY2, '<',
812 "-Private Key file to use for servername if not in -cert2"},
6d382c74 813 {"keyform", OPT_KEYFORM, 'f', "Key format (ENGINE, other values ignored)"},
2a33470b 814 {"pass", OPT_PASS, 's', "Private key and cert file pass phrase source"},
7e1b7485 815 {"dcert", OPT_DCERT, '<',
2b264aee 816 "Second server certificate file to use (usually for DSA)"},
7e1b7485 817 {"dcertform", OPT_DCERTFORM, 'F',
6d382c74 818 "Second server certificate file format (PEM/DER/P12); has no effect"},
2b264aee
DDO
819 {"dcert_chain", OPT_DCERT_CHAIN, '<',
820 "second server certificate chain file in PEM format"},
7e1b7485
RS
821 {"dkey", OPT_DKEY, '<',
822 "Second private key file to use (usually for DSA)"},
b9a189ce 823 {"dkeyform", OPT_DKEYFORM, 'f',
6d382c74 824 "Second key file format (ENGINE, other values ignored)"},
9c73e48a
DO
825 {"dpass", OPT_DPASS, 's',
826 "Second private key and cert file pass phrase source"},
2b264aee 827 {"dhparam", OPT_DHPARAM, '<', "DH parameters file to use"},
5388f986
RS
828 {"servername", OPT_SERVERNAME, 's',
829 "Servername for HostName TLS extension"},
830 {"servername_fatal", OPT_SERVERNAME_FATAL, '-',
359efeac 831 "On servername mismatch send fatal alert (default warning alert)"},
7e1b7485
RS
832 {"nbio_test", OPT_NBIO_TEST, '-', "Test with the non-blocking test bio"},
833 {"crlf", OPT_CRLF, '-', "Convert LF from terminal into CRLF"},
7e1b7485 834 {"quiet", OPT_QUIET, '-', "No server output"},
7e1b7485
RS
835 {"no_resume_ephemeral", OPT_NO_RESUME_EPHEMERAL, '-',
836 "Disable caching and tickets if ephemeral (EC)DH is used"},
837 {"www", OPT_WWW, '-', "Respond to a 'GET /' with a status page"},
838 {"WWW", OPT_UPPER_WWW, '-', "Respond to a 'GET with the file ./path"},
09b90e0e
DB
839 {"ignore_unexpected_eof", OPT_IGNORE_UNEXPECTED_EOF, '-',
840 "Do not treat lack of close_notify from a peer as an error"},
7e1b7485
RS
841 {"tlsextdebug", OPT_TLSEXTDEBUG, '-',
842 "Hex dump of all TLS extensions received"},
ceab33e2 843 {"HTTP", OPT_HTTP, '-', "Like -WWW but ./path includes HTTP headers"},
9c3bcfa0
RS
844 {"id_prefix", OPT_ID_PREFIX, 's',
845 "Generate SSL/TLS session IDs prefixed by arg"},
7e1b7485
RS
846 {"keymatexport", OPT_KEYMATEXPORT, 's',
847 "Export keying material using label"},
848 {"keymatexportlen", OPT_KEYMATEXPORTLEN, 'p',
2a33470b 849 "Export len bytes of keying material; default 20"},
32eabe34 850 {"CRL", OPT_CRL, '<', "CRL file to use"},
2b264aee 851 {"CRLform", OPT_CRLFORM, 'F', "CRL file format (PEM or DER); default PEM"},
32eabe34 852 {"crl_download", OPT_CRL_DOWNLOAD, '-',
2b264aee
DDO
853 "Download CRLs from distribution points in certificate CDP entries"},
854 {"chainCAfile", OPT_CHAINCAFILE, '<',
855 "CA file for certificate chain (PEM format)"},
32eabe34
MR
856 {"chainCApath", OPT_CHAINCAPATH, '/',
857 "use dir as certificate store path to build CA certificate chain"},
fd3397fc
RL
858 {"chainCAstore", OPT_CHAINCASTORE, ':',
859 "use URI as certificate store to build CA certificate chain"},
2b264aee
DDO
860 {"verifyCAfile", OPT_VERIFYCAFILE, '<',
861 "CA file for certificate verification (PEM format)"},
32eabe34
MR
862 {"verifyCApath", OPT_VERIFYCAPATH, '/',
863 "use dir as certificate store path to verify CA certificate"},
fd3397fc
RL
864 {"verifyCAstore", OPT_VERIFYCASTORE, ':',
865 "use URI as certificate store to verify CA certificate"},
32eabe34
MR
866 {"no_cache", OPT_NO_CACHE, '-', "Disable session cache"},
867 {"ext_cache", OPT_EXT_CACHE, '-',
359efeac 868 "Disable internal cache, set up and use external cache"},
32eabe34
MR
869 {"verify_return_error", OPT_VERIFY_RET_ERROR, '-',
870 "Close connection on verification error"},
871 {"verify_quiet", OPT_VERIFY_QUIET, '-',
872 "No verify output except verify errors"},
359efeac
DDO
873 {"ign_eof", OPT_IGN_EOF, '-', "Ignore input EOF (default when -quiet)"},
874 {"no_ign_eof", OPT_NO_IGN_EOF, '-', "Do not ignore input EOF"},
b67cb09f
TS
875#ifndef OPENSSL_NO_COMP_ALG
876 {"cert_comp", OPT_CERT_COMP, '-', "Pre-compress server certificates"},
877#endif
5388f986 878
3e41ac35 879#ifndef OPENSSL_NO_OCSP
5388f986 880 OPT_SECTION("OCSP"),
32eabe34
MR
881 {"status", OPT_STATUS, '-', "Request certificate status from server"},
882 {"status_verbose", OPT_STATUS_VERBOSE, '-',
883 "Print more output in certificate status callback"},
884 {"status_timeout", OPT_STATUS_TIMEOUT, 'n',
885 "Status request responder timeout"},
886 {"status_url", OPT_STATUS_URL, 's', "Status request fallback URL"},
80a4ac57
DDO
887 {"proxy", OPT_PROXY, 's',
888 "[http[s]://]host[:port][/path] of HTTP(S) proxy to use; path is ignored"},
889 {"no_proxy", OPT_NO_PROXY, 's',
890 "List of addresses of servers not to use HTTP(S) proxy for"},
891 {OPT_MORE_STR, 0, 0,
892 "Default from environment variable 'no_proxy', else 'NO_PROXY', else none"},
acf65ae5
MC
893 {"status_file", OPT_STATUS_FILE, '<',
894 "File containing DER encoded OCSP Response"},
3e41ac35 895#endif
5388f986
RS
896
897 OPT_SECTION("Debug"),
32eabe34
MR
898 {"security_debug", OPT_SECURITY_DEBUG, '-',
899 "Print output from SSL/TLS security framework"},
900 {"security_debug_verbose", OPT_SECURITY_DEBUG_VERBOSE, '-',
901 "Print more output from SSL/TLS security framework"},
d6073e27 902 {"brief", OPT_BRIEF, '-',
32eabe34
MR
903 "Restrict output to brief summary of connection parameters"},
904 {"rev", OPT_REV, '-',
04fb4ec8 905 "act as an echo server that sends back received text reversed"},
5388f986
RS
906 {"debug", OPT_DEBUG, '-', "Print more output"},
907 {"msg", OPT_MSG, '-', "Show protocol messages"},
908 {"msgfile", OPT_MSGFILE, '>',
909 "File to send output of -msg or -trace, instead of stdout"},
910 {"state", OPT_STATE, '-', "Print the SSL states"},
7e25dd6d 911 {"async", OPT_ASYNC, '-', "Operate in asynchronous mode"},
36b2cfb1 912 {"max_pipelines", OPT_MAX_PIPELINES, 'p',
032c6d21 913 "Maximum number of encrypt/decrypt pipelines to be used"},
5388f986
RS
914 {"naccept", OPT_NACCEPT, 'p', "Terminate after #num connections"},
915 {"keylogfile", OPT_KEYLOG_FILE, '>', "Write TLS secrets to file"},
916
917 OPT_SECTION("Network"),
918 {"nbio", OPT_NBIO, '-', "Use non-blocking IO"},
919 {"timeout", OPT_TIMEOUT, '-', "Enable timeouts"},
359efeac 920 {"mtu", OPT_MTU, 'p', "Set link-layer MTU"},
36b2cfb1 921 {"read_buf", OPT_READ_BUF, 'p',
dad78fb1 922 "Default read buffer size to be used for connections"},
5388f986
RS
923 {"split_send_frag", OPT_SPLIT_SEND_FRAG, 'p',
924 "Size used to split data for encrypt pipelines"},
925 {"max_send_frag", OPT_MAX_SEND_FRAG, 'p', "Maximum Size of send frames "},
926
927 OPT_SECTION("Server identity"),
720b6cbe 928 {"psk_identity", OPT_PSK_IDENTITY, 's', "PSK identity to expect"},
14e35350 929#ifndef OPENSSL_NO_PSK
9c3bcfa0 930 {"psk_hint", OPT_PSK_HINT, 's', "PSK identity hint to use"},
9c3bcfa0 931#endif
14e35350 932 {"psk", OPT_PSK, 's', "PSK in hex (without 0x)"},
df894947 933 {"psk_session", OPT_PSK_SESS, '<', "File to read PSK SSL session from"},
9c3bcfa0 934#ifndef OPENSSL_NO_SRP
6d2a1eff 935 {"srpvfile", OPT_SRPVFILE, '<', "(deprecated) The verifier file for SRP"},
9c3bcfa0 936 {"srpuserseed", OPT_SRPUSERSEED, 's',
6d2a1eff 937 "(deprecated) A seed string for a default user salt"},
9c3bcfa0 938#endif
5388f986
RS
939
940 OPT_SECTION("Protocol and version"),
941 {"max_early_data", OPT_MAX_EARLY, 'n',
942 "The maximum number of bytes of early data as advertised in tickets"},
943 {"recv_max_early_data", OPT_RECV_MAX_EARLY, 'n',
944 "The maximum number of bytes of early data (hard limit)"},
945 {"early_data", OPT_EARLY_DATA, '-', "Attempt to read early data"},
946 {"num_tickets", OPT_S_NUM_TICKETS, 'n',
947 "The number of TLSv1.3 session tickets that a server will automatically issue" },
948 {"anti_replay", OPT_ANTI_REPLAY, '-', "Switch on anti-replay protection (default)"},
949 {"no_anti_replay", OPT_NO_ANTI_REPLAY, '-', "Switch off anti-replay protection"},
950 {"http_server_binmode", OPT_HTTP_SERVER_BINMODE, '-', "opening files in binary mode when acting as http server (-WWW and -HTTP)"},
90fc2c26
NM
951 {"no_ca_names", OPT_NOCANAMES, '-',
952 "Disable TLS Extension CA Names"},
5388f986 953 {"stateless", OPT_STATELESS, '-', "Require TLSv1.3 cookies"},
9c3bcfa0
RS
954#ifndef OPENSSL_NO_SSL3
955 {"ssl3", OPT_SSL3, '-', "Just talk SSLv3"},
956#endif
6b01bed2
VD
957#ifndef OPENSSL_NO_TLS1
958 {"tls1", OPT_TLS1, '-', "Just talk TLSv1"},
959#endif
960#ifndef OPENSSL_NO_TLS1_1
961 {"tls1_1", OPT_TLS1_1, '-', "Just talk TLSv1.1"},
962#endif
963#ifndef OPENSSL_NO_TLS1_2
964 {"tls1_2", OPT_TLS1_2, '-', "just talk TLSv1.2"},
965#endif
582a17d6
MC
966#ifndef OPENSSL_NO_TLS1_3
967 {"tls1_3", OPT_TLS1_3, '-', "just talk TLSv1.3"},
968#endif
a5ecdc6a 969#ifndef OPENSSL_NO_DTLS
32eabe34 970 {"dtls", OPT_DTLS, '-', "Use any DTLS version"},
fd4e98ec
MC
971 {"listen", OPT_LISTEN, '-',
972 "Listen for a DTLS ClientHello with a cookie and then connect"},
9c3bcfa0 973#endif
6b01bed2
VD
974#ifndef OPENSSL_NO_DTLS1
975 {"dtls1", OPT_DTLS1, '-', "Just talk DTLSv1"},
976#endif
977#ifndef OPENSSL_NO_DTLS1_2
978 {"dtls1_2", OPT_DTLS1_2, '-', "Just talk DTLSv1.2"},
979#endif
72d0bc84
MC
980#ifndef OPENSSL_NO_SCTP
981 {"sctp", OPT_SCTP, '-', "Use SCTP"},
09d62b33 982 {"sctp_label_bug", OPT_SCTP_LABEL_BUG, '-', "Enable SCTP label length bug"},
72d0bc84 983#endif
5388f986
RS
984#ifndef OPENSSL_NO_SRTP
985 {"use_srtp", OPT_SRTP_PROFILES, 's',
986 "Offer SRTP key management with a colon-separated profile list"},
987#endif
9c3bcfa0 988 {"no_dhe", OPT_NO_DHE, '-', "Disable ephemeral DH"},
9c3bcfa0
RS
989#ifndef OPENSSL_NO_NEXTPROTONEG
990 {"nextprotoneg", OPT_NEXTPROTONEG, 's',
991 "Set the advertised protocols for the NPN extension (comma-separated list)"},
b07c703f 992#endif
9c3bcfa0
RS
993 {"alpn", OPT_ALPN, 's',
994 "Set the advertised protocols for the ALPN extension (comma-separated list)"},
cd81ac7b 995#ifndef OPENSSL_NO_KTLS
e2ef7f12 996 {"ktls", OPT_KTLS, '-', "Enable Kernel TLS for sending and receiving"},
cd81ac7b 997 {"sendfile", OPT_SENDFILE, '-', "Use sendfile to response file with -WWW"},
cd715b7e 998 {"zerocopy_sendfile", OPT_USE_ZC_SENDFILE, '-', "Use zerocopy mode of KTLS sendfile"},
cd81ac7b 999#endif
3c95ef22
TS
1000 {"enable_server_rpk", OPT_ENABLE_SERVER_RPK, '-', "Enable raw public keys (RFC7250) from the server"},
1001 {"enable_client_rpk", OPT_ENABLE_CLIENT_RPK, '-', "Enable raw public keys (RFC7250) from the client"},
5388f986
RS
1002 OPT_R_OPTIONS,
1003 OPT_S_OPTIONS,
1004 OPT_V_OPTIONS,
5388f986 1005 OPT_X_OPTIONS,
6bd4e3f2 1006 OPT_PROV_OPTIONS,
5388f986 1007 {NULL}
7e1b7485
RS
1008};
1009
4bbd4ba6
MC
1010#define IS_PROT_FLAG(o) \
1011 (o == OPT_SSL3 || o == OPT_TLS1 || o == OPT_TLS1_1 || o == OPT_TLS1_2 \
582a17d6 1012 || o == OPT_TLS1_3 || o == OPT_DTLS || o == OPT_DTLS1 || o == OPT_DTLS1_2)
4bbd4ba6 1013
7e1b7485 1014int s_server_main(int argc, char *argv[])
0f113f3e 1015{
bde136c8 1016 ENGINE *engine = NULL;
7e1b7485
RS
1017 EVP_PKEY *s_key = NULL, *s_dkey = NULL;
1018 SSL_CONF_CTX *cctx = NULL;
32ec4153 1019 const SSL_METHOD *meth = TLS_server_method();
7e1b7485
RS
1020 SSL_EXCERT *exc = NULL;
1021 STACK_OF(OPENSSL_STRING) *ssl_args = NULL;
1022 STACK_OF(X509) *s_chain = NULL, *s_dchain = NULL;
1023 STACK_OF(X509_CRL) *crls = NULL;
1024 X509 *s_cert = NULL, *s_dcert = NULL;
0f113f3e 1025 X509_VERIFY_PARAM *vpm = NULL;
fd3397fc
RL
1026 const char *CApath = NULL, *CAfile = NULL, *CAstore = NULL;
1027 const char *chCApath = NULL, *chCAfile = NULL, *chCAstore = NULL;
3ee1eac2 1028 char *dpassarg = NULL, *dpass = NULL;
fd3397fc
RL
1029 char *passarg = NULL, *pass = NULL;
1030 char *vfyCApath = NULL, *vfyCAfile = NULL, *vfyCAstore = NULL;
a7f82a1a 1031 char *crl_file = NULL, *prog;
ab69ac00 1032#ifdef AF_UNIX
0f113f3e
MC
1033 int unlink_unix_path = 0;
1034#endif
a773b52a 1035 do_server_cb server_cb;
7e1b7485 1036 int vpmtouched = 0, build_chain = 0, no_cache = 0, ext_cache = 0;
54463e4f 1037 char *dhfile = NULL;
37f3a3b3 1038 int no_dhe = 0;
8caab744 1039 int nocert = 0, ret = 1;
fd3397fc 1040 int noCApath = 0, noCAfile = 0, noCAstore = 0;
d382e796
TM
1041 int s_cert_format = FORMAT_UNDEF, s_key_format = FORMAT_UNDEF;
1042 int s_dcert_format = FORMAT_UNDEF, s_dkey_format = FORMAT_UNDEF;
ab69ac00 1043 int rev = 0, naccept = -1, sdebug = 0;
72d0bc84 1044 int socket_family = AF_UNSPEC, socket_type = SOCK_STREAM, protocol = 0;
d382e796 1045 int state = 0, crl_format = FORMAT_UNDEF, crl_download = 0;
ab69ac00 1046 char *host = NULL;
0c590558 1047 char *port = NULL;
7e1b7485
RS
1048 unsigned char *context = NULL;
1049 OPTION_CHOICE o;
0f113f3e
MC
1050 EVP_PKEY *s_key2 = NULL;
1051 X509 *s_cert2 = NULL;
1052 tlsextctx tlsextcbp = { NULL, NULL, SSL_TLSEXT_ERR_ALERT_WARNING };
287d0b94 1053 const char *ssl_config = NULL;
dad78fb1 1054 int read_buf_len = 0;
e481f9b9 1055#ifndef OPENSSL_NO_NEXTPROTONEG
0f113f3e
MC
1056 const char *next_proto_neg_in = NULL;
1057 tlsextnextprotoctx next_proto = { NULL, 0 };
e481f9b9 1058#endif
0f113f3e
MC
1059 const char *alpn_in = NULL;
1060 tlsextalpnctx alpn_ctx = { NULL, 0 };
ddac1974 1061#ifndef OPENSSL_NO_PSK
0f113f3e 1062 /* by default do not send a PSK identity hint */
f2ff1432 1063 char *psk_identity_hint = NULL;
ddac1974 1064#endif
14e35350 1065 char *p;
edc032b5 1066#ifndef OPENSSL_NO_SRP
0f113f3e
MC
1067 char *srpuserseed = NULL;
1068 char *srp_verifier_file = NULL;
edc032b5 1069#endif
dad88680 1070#ifndef OPENSSL_NO_SRTP
1fb6b0bf 1071 char *srtp_profiles = NULL;
dad88680 1072#endif
4bbd4ba6 1073 int min_version = 0, max_version = 0, prot_opt = 0, no_prot_opt = 0;
54463e4f
F
1074 int s_server_verify = SSL_VERIFY_NONE;
1075 int s_server_session_id_context = 1; /* anything will do */
1076 const char *s_cert_file = TEST_CERT, *s_key_file = NULL, *s_chain_file = NULL;
1077 const char *s_cert_file2 = TEST_CERT2, *s_key_file2 = NULL;
1078 char *s_dcert_file = NULL, *s_dkey_file = NULL, *s_dchain_file = NULL;
057c676a
RL
1079#ifndef OPENSSL_NO_OCSP
1080 int s_tlsextstatus = 0;
1081#endif
1082 int no_resume_ephemeral = 0;
28e5ea88 1083 unsigned int max_send_fragment = 0;
54463e4f
F
1084 unsigned int split_send_fragment = 0, max_pipelines = 0;
1085 const char *s_serverinfo_file = NULL;
4bf73e9f 1086 const char *keylog_file = NULL;
bafe9cf5 1087 int max_early_data = -1, recv_max_early_data = -1;
df894947 1088 char *psksessf = NULL;
90fc2c26 1089 int no_ca_names = 0;
09d62b33
MT
1090#ifndef OPENSSL_NO_SCTP
1091 int sctp_label_bug = 0;
1092#endif
09b90e0e 1093 int ignore_unexpected_eof = 0;
e2ef7f12
TZ
1094#ifndef OPENSSL_NO_KTLS
1095 int enable_ktls = 0;
1096#endif
a3e53d56 1097 int tfo = 0;
b67cb09f 1098 int cert_comp = 0;
3c95ef22 1099 int enable_server_rpk = 0;
54463e4f
F
1100
1101 /* Init of few remaining global variables */
0f113f3e
MC
1102 local_argc = argc;
1103 local_argv = argv;
d02b48c6 1104
54463e4f
F
1105 ctx = ctx2 = NULL;
1106 s_nbio = s_nbio_test = 0;
1107 www = 0;
1108 bio_s_out = NULL;
1109 s_debug = 0;
1110 s_msg = 0;
1111 s_quiet = 0;
1112 s_brief = 0;
1113 async = 0;
cd81ac7b 1114 use_sendfile = 0;
cd715b7e 1115 use_zc_sendfile = 0;
54463e4f 1116
0c590558 1117 port = OPENSSL_strdup(PORT);
0f113f3e 1118 cctx = SSL_CONF_CTX_new();
7e1b7485 1119 vpm = X509_VERIFY_PARAM_new();
0c590558 1120 if (port == NULL || cctx == NULL || vpm == NULL)
0f113f3e 1121 goto end;
d6073e27
F
1122 SSL_CONF_CTX_set_flags(cctx,
1123 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CMDLINE);
7e1b7485
RS
1124
1125 prog = opt_init(argc, argv, s_server_options);
1126 while ((o = opt_next()) != OPT_EOF) {
4bbd4ba6
MC
1127 if (IS_PROT_FLAG(o) && ++prot_opt > 1) {
1128 BIO_printf(bio_err, "Cannot supply multiple protocol flags\n");
1129 goto end;
1130 }
1131 if (IS_NO_PROT_FLAG(o))
1132 no_prot_opt++;
1133 if (prot_opt == 1 && no_prot_opt) {
d6073e27
F
1134 BIO_printf(bio_err,
1135 "Cannot supply both a protocol flag and '-no_<prot>'\n");
4bbd4ba6
MC
1136 goto end;
1137 }
7e1b7485
RS
1138 switch (o) {
1139 case OPT_EOF:
1140 case OPT_ERR:
1141 opthelp:
1142 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
1143 goto end;
1144 case OPT_HELP:
1145 opt_help(s_server_options);
1146 ret = 0;
1147 goto end;
0f113f3e 1148
ab69ac00
RL
1149 case OPT_4:
1150#ifdef AF_UNIX
1151 if (socket_family == AF_UNIX) {
1152 OPENSSL_free(host); host = NULL;
1153 OPENSSL_free(port); port = NULL;
1154 }
1155#endif
1156 socket_family = AF_INET;
1157 break;
1158 case OPT_6:
1159 if (1) {
1160#ifdef AF_INET6
1161#ifdef AF_UNIX
1162 if (socket_family == AF_UNIX) {
1163 OPENSSL_free(host); host = NULL;
1164 OPENSSL_free(port); port = NULL;
1165 }
1166#endif
1167 socket_family = AF_INET6;
1168 } else {
1169#endif
1170 BIO_printf(bio_err, "%s: IPv6 domain sockets unsupported\n", prog);
1171 goto end;
1172 }
1173 break;
7e1b7485 1174 case OPT_PORT:
ab69ac00
RL
1175#ifdef AF_UNIX
1176 if (socket_family == AF_UNIX) {
1177 socket_family = AF_UNSPEC;
1178 }
1179#endif
1180 OPENSSL_free(port); port = NULL;
1181 OPENSSL_free(host); host = NULL;
1182 if (BIO_parse_hostserv(opt_arg(), NULL, &port, BIO_PARSE_PRIO_SERV) < 1) {
1183 BIO_printf(bio_err,
1184 "%s: -port argument malformed or ambiguous\n",
1185 port);
1186 goto end;
1187 }
1188 break;
1189 case OPT_ACCEPT:
1190#ifdef AF_UNIX
1191 if (socket_family == AF_UNIX) {
1192 socket_family = AF_UNSPEC;
1193 }
1194#endif
1195 OPENSSL_free(port); port = NULL;
1196 OPENSSL_free(host); host = NULL;
1197 if (BIO_parse_hostserv(opt_arg(), &host, &port, BIO_PARSE_PRIO_SERV) < 1) {
1198 BIO_printf(bio_err,
1199 "%s: -accept argument malformed or ambiguous\n",
1200 port);
7e1b7485 1201 goto end;
ab69ac00 1202 }
7e1b7485 1203 break;
ab69ac00 1204#ifdef AF_UNIX
7e1b7485 1205 case OPT_UNIX:
ab69ac00 1206 socket_family = AF_UNIX;
3d484574 1207 OPENSSL_free(host); host = OPENSSL_strdup(opt_arg());
a6a2dd9f
JJ
1208 if (host == NULL)
1209 goto end;
ab69ac00 1210 OPENSSL_free(port); port = NULL;
7e1b7485
RS
1211 break;
1212 case OPT_UNLINK:
0f113f3e 1213 unlink_unix_path = 1;
7e1b7485 1214 break;
ab69ac00 1215#endif
7e1b7485
RS
1216 case OPT_NACCEPT:
1217 naccept = atol(opt_arg());
1218 break;
1219 case OPT_VERIFY:
0f113f3e 1220 s_server_verify = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE;
acc00492 1221 verify_args.depth = atoi(opt_arg());
0f113f3e 1222 if (!s_quiet)
acc00492 1223 BIO_printf(bio_err, "verify depth is %d\n", verify_args.depth);
7e1b7485
RS
1224 break;
1225 case OPT_UPPER_V_VERIFY:
0f113f3e
MC
1226 s_server_verify =
1227 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
1228 SSL_VERIFY_CLIENT_ONCE;
acc00492 1229 verify_args.depth = atoi(opt_arg());
0f113f3e
MC
1230 if (!s_quiet)
1231 BIO_printf(bio_err,
1232 "verify depth is %d, must return a certificate\n",
acc00492 1233 verify_args.depth);
7e1b7485
RS
1234 break;
1235 case OPT_CONTEXT:
1236 context = (unsigned char *)opt_arg();
1237 break;
1238 case OPT_CERT:
1239 s_cert_file = opt_arg();
1240 break;
a7c04f2b
DB
1241 case OPT_NAMEOPT:
1242 if (!set_nameopt(opt_arg()))
1243 goto end;
1244 break;
7e1b7485
RS
1245 case OPT_CRL:
1246 crl_file = opt_arg();
1247 break;
1248 case OPT_CRL_DOWNLOAD:
0f113f3e 1249 crl_download = 1;
7e1b7485 1250 break;
7e1b7485
RS
1251 case OPT_SERVERINFO:
1252 s_serverinfo_file = opt_arg();
1253 break;
7e1b7485 1254 case OPT_CERTFORM:
6d382c74 1255 if (!opt_format(opt_arg(), OPT_FMT_ANY, &s_cert_format))
7e1b7485
RS
1256 goto opthelp;
1257 break;
1258 case OPT_KEY:
1259 s_key_file = opt_arg();
1260 break;
1261 case OPT_KEYFORM:
6d382c74 1262 if (!opt_format(opt_arg(), OPT_FMT_ANY, &s_key_format))
7e1b7485
RS
1263 goto opthelp;
1264 break;
1265 case OPT_PASS:
1266 passarg = opt_arg();
1267 break;
1268 case OPT_CERT_CHAIN:
1269 s_chain_file = opt_arg();
1270 break;
1271 case OPT_DHPARAM:
1272 dhfile = opt_arg();
1273 break;
1274 case OPT_DCERTFORM:
6d382c74 1275 if (!opt_format(opt_arg(), OPT_FMT_ANY, &s_dcert_format))
7e1b7485
RS
1276 goto opthelp;
1277 break;
1278 case OPT_DCERT:
1279 s_dcert_file = opt_arg();
1280 break;
1281 case OPT_DKEYFORM:
6d382c74 1282 if (!opt_format(opt_arg(), OPT_FMT_ANY, &s_dkey_format))
7e1b7485
RS
1283 goto opthelp;
1284 break;
1285 case OPT_DPASS:
1286 dpassarg = opt_arg();
1287 break;
1288 case OPT_DKEY:
1289 s_dkey_file = opt_arg();
1290 break;
1291 case OPT_DCERT_CHAIN:
1292 s_dchain_file = opt_arg();
1293 break;
1294 case OPT_NOCERT:
0f113f3e 1295 nocert = 1;
7e1b7485
RS
1296 break;
1297 case OPT_CAPATH:
1298 CApath = opt_arg();
1299 break;
2b6bcb70
MC
1300 case OPT_NOCAPATH:
1301 noCApath = 1;
1302 break;
7e1b7485
RS
1303 case OPT_CHAINCAPATH:
1304 chCApath = opt_arg();
1305 break;
1306 case OPT_VERIFYCAPATH:
1307 vfyCApath = opt_arg();
1308 break;
fd3397fc
RL
1309 case OPT_CASTORE:
1310 CAstore = opt_arg();
1311 break;
1312 case OPT_NOCASTORE:
1313 noCAstore = 1;
1314 break;
1315 case OPT_CHAINCASTORE:
1316 chCAstore = opt_arg();
1317 break;
1318 case OPT_VERIFYCASTORE:
1319 vfyCAstore = opt_arg();
1320 break;
7e1b7485 1321 case OPT_NO_CACHE:
0f113f3e 1322 no_cache = 1;
7e1b7485
RS
1323 break;
1324 case OPT_EXT_CACHE:
0f113f3e 1325 ext_cache = 1;
7e1b7485
RS
1326 break;
1327 case OPT_CRLFORM:
1328 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &crl_format))
1329 goto opthelp;
1330 break;
1331 case OPT_S_CASES:
394159da 1332 case OPT_S_NUM_TICKETS:
3bb5e5b0
MC
1333 case OPT_ANTI_REPLAY:
1334 case OPT_NO_ANTI_REPLAY:
7e1b7485
RS
1335 if (ssl_args == NULL)
1336 ssl_args = sk_OPENSSL_STRING_new_null();
1337 if (ssl_args == NULL
1338 || !sk_OPENSSL_STRING_push(ssl_args, opt_flag())
1339 || !sk_OPENSSL_STRING_push(ssl_args, opt_arg())) {
1340 BIO_printf(bio_err, "%s: Memory allocation failure\n", prog);
1341 goto end;
1342 }
1343 break;
1344 case OPT_V_CASES:
1345 if (!opt_verify(o, vpm))
1346 goto end;
1347 vpmtouched++;
1348 break;
1349 case OPT_X_CASES:
1350 if (!args_excert(o, &exc))
1351 goto end;
1352 break;
1353 case OPT_VERIFY_RET_ERROR:
acc00492 1354 verify_args.return_error = 1;
7e1b7485
RS
1355 break;
1356 case OPT_VERIFY_QUIET:
acc00492 1357 verify_args.quiet = 1;
7e1b7485
RS
1358 break;
1359 case OPT_BUILD_CHAIN:
0f113f3e 1360 build_chain = 1;
7e1b7485
RS
1361 break;
1362 case OPT_CAFILE:
1363 CAfile = opt_arg();
1364 break;
2b6bcb70
MC
1365 case OPT_NOCAFILE:
1366 noCAfile = 1;
1367 break;
7e1b7485
RS
1368 case OPT_CHAINCAFILE:
1369 chCAfile = opt_arg();
1370 break;
1371 case OPT_VERIFYCAFILE:
1372 vfyCAfile = opt_arg();
1373 break;
1374 case OPT_NBIO:
0f113f3e 1375 s_nbio = 1;
7e1b7485
RS
1376 break;
1377 case OPT_NBIO_TEST:
1378 s_nbio = s_nbio_test = 1;
1379 break;
1380 case OPT_IGN_EOF:
0f113f3e 1381 s_ign_eof = 1;
7e1b7485
RS
1382 break;
1383 case OPT_NO_IGN_EOF:
0f113f3e 1384 s_ign_eof = 0;
7e1b7485
RS
1385 break;
1386 case OPT_DEBUG:
0f113f3e 1387 s_debug = 1;
7e1b7485 1388 break;
7e1b7485 1389 case OPT_TLSEXTDEBUG:
0f113f3e 1390 s_tlsextdebug = 1;
7e1b7485
RS
1391 break;
1392 case OPT_STATUS:
057c676a 1393#ifndef OPENSSL_NO_OCSP
0f113f3e 1394 s_tlsextstatus = 1;
057c676a 1395#endif
7e1b7485
RS
1396 break;
1397 case OPT_STATUS_VERBOSE:
057c676a 1398#ifndef OPENSSL_NO_OCSP
7e1b7485 1399 s_tlsextstatus = tlscstatp.verbose = 1;
057c676a 1400#endif
7e1b7485
RS
1401 break;
1402 case OPT_STATUS_TIMEOUT:
057c676a 1403#ifndef OPENSSL_NO_OCSP
0f113f3e 1404 s_tlsextstatus = 1;
7e1b7485 1405 tlscstatp.timeout = atoi(opt_arg());
80a4ac57
DDO
1406#endif
1407 break;
1408 case OPT_PROXY:
1409#ifndef OPENSSL_NO_OCSP
1410 tlscstatp.proxy = opt_arg();
1411#endif
1412 break;
1413 case OPT_NO_PROXY:
1414#ifndef OPENSSL_NO_OCSP
1415 tlscstatp.no_proxy = opt_arg();
057c676a 1416#endif
7e1b7485
RS
1417 break;
1418 case OPT_STATUS_URL:
3e41ac35 1419#ifndef OPENSSL_NO_OCSP
0f113f3e 1420 s_tlsextstatus = 1;
7932982b 1421 if (!OSSL_HTTP_parse_url(opt_arg(), &tlscstatp.use_ssl, NULL,
d7fcee3b 1422 &tlscstatp.host, &tlscstatp.port, NULL,
7932982b
DDO
1423 &tlscstatp.path, NULL, NULL)) {
1424 BIO_printf(bio_err, "Error parsing -status_url argument\n");
7e1b7485 1425 goto end;
0f113f3e 1426 }
acf65ae5
MC
1427#endif
1428 break;
1429 case OPT_STATUS_FILE:
1430#ifndef OPENSSL_NO_OCSP
1431 s_tlsextstatus = 1;
1432 tlscstatp.respin = opt_arg();
3e41ac35 1433#endif
7e1b7485 1434 break;
7e1b7485 1435 case OPT_MSG:
0f113f3e 1436 s_msg = 1;
7e1b7485
RS
1437 break;
1438 case OPT_MSGFILE:
1439 bio_s_msg = BIO_new_file(opt_arg(), "w");
625b0990 1440 if (bio_s_msg == NULL) {
1441 BIO_printf(bio_err, "Error writing file %s\n", opt_arg());
1442 goto end;
1443 }
7e1b7485 1444 break;
7e1b7485 1445 case OPT_TRACE:
9c3bcfa0 1446#ifndef OPENSSL_NO_SSL_TRACE
0f113f3e 1447 s_msg = 2;
0f113f3e 1448#endif
1c03c81f 1449 break;
7e1b7485 1450 case OPT_SECURITY_DEBUG:
0f113f3e 1451 sdebug = 1;
7e1b7485
RS
1452 break;
1453 case OPT_SECURITY_DEBUG_VERBOSE:
0f113f3e 1454 sdebug = 2;
7e1b7485
RS
1455 break;
1456 case OPT_STATE:
0f113f3e 1457 state = 1;
7e1b7485
RS
1458 break;
1459 case OPT_CRLF:
0f113f3e 1460 s_crlf = 1;
7e1b7485
RS
1461 break;
1462 case OPT_QUIET:
0f113f3e 1463 s_quiet = 1;
7e1b7485
RS
1464 break;
1465 case OPT_BRIEF:
acc00492 1466 s_quiet = s_brief = verify_args.quiet = 1;
7e1b7485 1467 break;
7e1b7485 1468 case OPT_NO_DHE:
0f113f3e 1469 no_dhe = 1;
7e1b7485 1470 break;
7e1b7485 1471 case OPT_NO_RESUME_EPHEMERAL:
0f113f3e 1472 no_resume_ephemeral = 1;
7e1b7485 1473 break;
720b6cbe 1474 case OPT_PSK_IDENTITY:
720b6cbe 1475 psk_identity = opt_arg();
720b6cbe 1476 break;
7e1b7485 1477 case OPT_PSK_HINT:
6b01bed2 1478#ifndef OPENSSL_NO_PSK
7e1b7485 1479 psk_identity_hint = opt_arg();
6b01bed2 1480#endif
7e1b7485
RS
1481 break;
1482 case OPT_PSK:
1483 for (p = psk_key = opt_arg(); *p; p++) {
18295f0c 1484 if (isxdigit(_UC(*p)))
0f113f3e 1485 continue;
e5712012 1486 BIO_printf(bio_err, "Not a hex number '%s'\n", psk_key);
7e1b7485 1487 goto end;
0f113f3e 1488 }
6b01bed2 1489 break;
df894947
MC
1490 case OPT_PSK_SESS:
1491 psksessf = opt_arg();
1492 break;
7e1b7485 1493 case OPT_SRPVFILE:
6b01bed2 1494#ifndef OPENSSL_NO_SRP
7e1b7485 1495 srp_verifier_file = opt_arg();
0d5301af
KR
1496 if (min_version < TLS1_VERSION)
1497 min_version = TLS1_VERSION;
6b01bed2 1498#endif
7e1b7485
RS
1499 break;
1500 case OPT_SRPUSERSEED:
6b01bed2 1501#ifndef OPENSSL_NO_SRP
7e1b7485 1502 srpuserseed = opt_arg();
0d5301af
KR
1503 if (min_version < TLS1_VERSION)
1504 min_version = TLS1_VERSION;
0f113f3e 1505#endif
6b01bed2 1506 break;
7e1b7485 1507 case OPT_REV:
0f113f3e 1508 rev = 1;
7e1b7485
RS
1509 break;
1510 case OPT_WWW:
0f113f3e 1511 www = 1;
7e1b7485
RS
1512 break;
1513 case OPT_UPPER_WWW:
0f113f3e 1514 www = 2;
7e1b7485
RS
1515 break;
1516 case OPT_HTTP:
0f113f3e 1517 www = 3;
7e1b7485 1518 break;
287d0b94
DSH
1519 case OPT_SSL_CONFIG:
1520 ssl_config = opt_arg();
1521 break;
7e1b7485 1522 case OPT_SSL3:
0d5301af
KR
1523 min_version = SSL3_VERSION;
1524 max_version = SSL3_VERSION;
9c3bcfa0 1525 break;
582a17d6
MC
1526 case OPT_TLS1_3:
1527 min_version = TLS1_3_VERSION;
1528 max_version = TLS1_3_VERSION;
1529 break;
7e1b7485 1530 case OPT_TLS1_2:
0d5301af
KR
1531 min_version = TLS1_2_VERSION;
1532 max_version = TLS1_2_VERSION;
7e1b7485
RS
1533 break;
1534 case OPT_TLS1_1:
0d5301af
KR
1535 min_version = TLS1_1_VERSION;
1536 max_version = TLS1_1_VERSION;
7e1b7485
RS
1537 break;
1538 case OPT_TLS1:
0d5301af
KR
1539 min_version = TLS1_VERSION;
1540 max_version = TLS1_VERSION;
7e1b7485 1541 break;
7e1b7485 1542 case OPT_DTLS:
6b01bed2 1543#ifndef OPENSSL_NO_DTLS
4407d070 1544 meth = DTLS_server_method();
0f113f3e 1545 socket_type = SOCK_DGRAM;
6b01bed2 1546#endif
7e1b7485
RS
1547 break;
1548 case OPT_DTLS1:
0d5301af
KR
1549#ifndef OPENSSL_NO_DTLS
1550 meth = DTLS_server_method();
1551 min_version = DTLS1_VERSION;
1552 max_version = DTLS1_VERSION;
0f113f3e 1553 socket_type = SOCK_DGRAM;
6b01bed2 1554#endif
7e1b7485
RS
1555 break;
1556 case OPT_DTLS1_2:
0d5301af
KR
1557#ifndef OPENSSL_NO_DTLS
1558 meth = DTLS_server_method();
1559 min_version = DTLS1_2_VERSION;
1560 max_version = DTLS1_2_VERSION;
0f113f3e 1561 socket_type = SOCK_DGRAM;
72d0bc84
MC
1562#endif
1563 break;
1564 case OPT_SCTP:
1565#ifndef OPENSSL_NO_SCTP
1566 protocol = IPPROTO_SCTP;
09d62b33
MT
1567#endif
1568 break;
1569 case OPT_SCTP_LABEL_BUG:
1570#ifndef OPENSSL_NO_SCTP
1571 sctp_label_bug = 1;
6b01bed2 1572#endif
7e1b7485
RS
1573 break;
1574 case OPT_TIMEOUT:
6b01bed2 1575#ifndef OPENSSL_NO_DTLS
0f113f3e 1576 enable_timeouts = 1;
6b01bed2 1577#endif
7e1b7485
RS
1578 break;
1579 case OPT_MTU:
6b01bed2 1580#ifndef OPENSSL_NO_DTLS
7e1b7485 1581 socket_mtu = atol(opt_arg());
6b01bed2 1582#endif
7e1b7485 1583 break;
fd4e98ec 1584 case OPT_LISTEN:
6b01bed2 1585#ifndef OPENSSL_NO_DTLS
fd4e98ec 1586 dtlslisten = 1;
0f113f3e 1587#endif
6b01bed2 1588 break;
c2f9648d
MC
1589 case OPT_STATELESS:
1590 stateless = 1;
1591 break;
7e1b7485
RS
1592 case OPT_ID_PREFIX:
1593 session_id_prefix = opt_arg();
1594 break;
1595 case OPT_ENGINE:
538404d2
DO
1596#ifndef OPENSSL_NO_ENGINE
1597 engine = setup_engine(opt_arg(), s_debug);
1598#endif
7e1b7485 1599 break;
3ee1eac2
RS
1600 case OPT_R_CASES:
1601 if (!opt_rand(o))
1602 goto end;
7e1b7485 1603 break;
6bd4e3f2
P
1604 case OPT_PROV_CASES:
1605 if (!opt_provider(o))
1606 goto end;
1607 break;
7e1b7485
RS
1608 case OPT_SERVERNAME:
1609 tlsextcbp.servername = opt_arg();
1610 break;
1611 case OPT_SERVERNAME_FATAL:
0f113f3e 1612 tlsextcbp.extension_error = SSL_TLSEXT_ERR_ALERT_FATAL;
7e1b7485
RS
1613 break;
1614 case OPT_CERT2:
1615 s_cert_file2 = opt_arg();
1616 break;
1617 case OPT_KEY2:
1618 s_key_file2 = opt_arg();
1619 break;
7e1b7485 1620 case OPT_NEXTPROTONEG:
9c3bcfa0 1621# ifndef OPENSSL_NO_NEXTPROTONEG
7e1b7485 1622 next_proto_neg_in = opt_arg();
e481f9b9 1623#endif
9c3bcfa0 1624 break;
7e1b7485
RS
1625 case OPT_ALPN:
1626 alpn_in = opt_arg();
1627 break;
7e1b7485 1628 case OPT_SRTP_PROFILES:
dad88680 1629#ifndef OPENSSL_NO_SRTP
7e1b7485 1630 srtp_profiles = opt_arg();
dad88680 1631#endif
d6316025 1632 break;
7e1b7485
RS
1633 case OPT_KEYMATEXPORT:
1634 keymatexportlabel = opt_arg();
1635 break;
1636 case OPT_KEYMATEXPORTLEN:
1637 keymatexportlen = atoi(opt_arg());
0f113f3e 1638 break;
7e25dd6d
MC
1639 case OPT_ASYNC:
1640 async = 1;
1641 break;
28e5ea88
F
1642 case OPT_MAX_SEND_FRAG:
1643 max_send_fragment = atoi(opt_arg());
28e5ea88 1644 break;
032c6d21
MC
1645 case OPT_SPLIT_SEND_FRAG:
1646 split_send_fragment = atoi(opt_arg());
032c6d21
MC
1647 break;
1648 case OPT_MAX_PIPELINES:
1649 max_pipelines = atoi(opt_arg());
1650 break;
dad78fb1
MC
1651 case OPT_READ_BUF:
1652 read_buf_len = atoi(opt_arg());
1653 break;
4bf73e9f
PW
1654 case OPT_KEYLOG_FILE:
1655 keylog_file = opt_arg();
1656 break;
048b1893
MC
1657 case OPT_MAX_EARLY:
1658 max_early_data = atoi(opt_arg());
6746648c
MC
1659 if (max_early_data < 0) {
1660 BIO_printf(bio_err, "Invalid value for max_early_data\n");
1661 goto end;
1662 }
048b1893 1663 break;
bafe9cf5
MC
1664 case OPT_RECV_MAX_EARLY:
1665 recv_max_early_data = atoi(opt_arg());
1666 if (recv_max_early_data < 0) {
1667 BIO_printf(bio_err, "Invalid value for recv_max_early_data\n");
1668 goto end;
1669 }
1670 break;
e0655186
MC
1671 case OPT_EARLY_DATA:
1672 early_data = 1;
c39e4048
BK
1673 if (max_early_data == -1)
1674 max_early_data = SSL3_RT_MAX_PLAIN_LENGTH;
e0655186 1675 break;
c3be39f2
LZ
1676 case OPT_HTTP_SERVER_BINMODE:
1677 http_server_binmode = 1;
1678 break;
90fc2c26
NM
1679 case OPT_NOCANAMES:
1680 no_ca_names = 1;
1681 break;
e2ef7f12
TZ
1682 case OPT_KTLS:
1683#ifndef OPENSSL_NO_KTLS
1684 enable_ktls = 1;
1685#endif
1686 break;
cd81ac7b
TZ
1687 case OPT_SENDFILE:
1688#ifndef OPENSSL_NO_KTLS
1689 use_sendfile = 1;
cd715b7e
MM
1690#endif
1691 break;
1692 case OPT_USE_ZC_SENDFILE:
1693#ifndef OPENSSL_NO_KTLS
1694 use_zc_sendfile = 1;
cd81ac7b
TZ
1695#endif
1696 break;
09b90e0e
DB
1697 case OPT_IGNORE_UNEXPECTED_EOF:
1698 ignore_unexpected_eof = 1;
1699 break;
a3e53d56
TS
1700 case OPT_TFO:
1701 tfo = 1;
1702 break;
b67cb09f
TS
1703 case OPT_CERT_COMP:
1704 cert_comp = 1;
1705 break;
3c95ef22
TS
1706 case OPT_ENABLE_SERVER_RPK:
1707 enable_server_rpk = 1;
1708 break;
1709 case OPT_ENABLE_CLIENT_RPK:
1710 enable_client_rpk = 1;
1711 break;
0f113f3e 1712 }
0f113f3e 1713 }
021410ea
RS
1714
1715 /* No extra arguments. */
d9f07357 1716 if (!opt_check_rest_arg(NULL))
021410ea 1717 goto opthelp;
7e1b7485 1718
3ad60309
DDO
1719 if (!app_RAND_load())
1720 goto end;
1721
837f87c2
PY
1722#ifndef OPENSSL_NO_NEXTPROTONEG
1723 if (min_version == TLS1_3_VERSION && next_proto_neg_in != NULL) {
1724 BIO_printf(bio_err, "Cannot supply -nextprotoneg with TLSv1.3\n");
1725 goto opthelp;
1726 }
1727#endif
a5ecdc6a 1728#ifndef OPENSSL_NO_DTLS
0f113f3e
MC
1729 if (www && socket_type == SOCK_DGRAM) {
1730 BIO_printf(bio_err, "Can't use -HTTP, -www or -WWW with DTLS\n");
1731 goto end;
1732 }
fd4e98ec
MC
1733
1734 if (dtlslisten && socket_type != SOCK_DGRAM) {
1735 BIO_printf(bio_err, "Can only use -listen with DTLS\n");
1736 goto end;
1737 }
575117ef
FWH
1738
1739 if (rev && socket_type == SOCK_DGRAM) {
1740 BIO_printf(bio_err, "Can't use -rev with DTLS\n");
1741 goto end;
1742 }
0f113f3e
MC
1743#endif
1744
a3e53d56
TS
1745 if (tfo && socket_type != SOCK_STREAM) {
1746 BIO_printf(bio_err, "Can only use -tfo with TLS\n");
1747 goto end;
1748 }
1749
c2f9648d
MC
1750 if (stateless && socket_type != SOCK_STREAM) {
1751 BIO_printf(bio_err, "Can only use --stateless with TLS\n");
1752 goto end;
1753 }
1754
ab69ac00
RL
1755#ifdef AF_UNIX
1756 if (socket_family == AF_UNIX && socket_type != SOCK_STREAM) {
0f113f3e
MC
1757 BIO_printf(bio_err,
1758 "Can't use unix sockets and datagrams together\n");
1759 goto end;
1760 }
ab69ac00 1761#endif
f37dea41 1762 if (early_data && rev) {
6ef40f1f 1763 BIO_printf(bio_err,
f37dea41 1764 "Can't use -early_data in combination with -rev\n");
6ef40f1f
MC
1765 goto end;
1766 }
2900fc8a 1767
72d0bc84
MC
1768#ifndef OPENSSL_NO_SCTP
1769 if (protocol == IPPROTO_SCTP) {
1770 if (socket_type != SOCK_DGRAM) {
1771 BIO_printf(bio_err, "Can't use -sctp without DTLS\n");
1772 goto end;
1773 }
1774 /* SCTP is unusual. It uses DTLS over a SOCK_STREAM protocol */
1775 socket_type = SOCK_STREAM;
1776 }
1777#endif
032c6d21 1778
cd81ac7b 1779#ifndef OPENSSL_NO_KTLS
cd715b7e
MM
1780 if (use_zc_sendfile && !use_sendfile) {
1781 BIO_printf(bio_out, "Warning: -zerocopy_sendfile depends on -sendfile, enabling -sendfile now.\n");
1782 use_sendfile = 1;
1783 }
1784
e2ef7f12
TZ
1785 if (use_sendfile && enable_ktls == 0) {
1786 BIO_printf(bio_out, "Warning: -sendfile depends on -ktls, enabling -ktls now.\n");
1787 enable_ktls = 1;
1788 }
1789
cd81ac7b
TZ
1790 if (use_sendfile && www <= 1) {
1791 BIO_printf(bio_err, "Can't use -sendfile without -WWW or -HTTP\n");
1792 goto end;
1793 }
1794#endif
1795
7e1b7485 1796 if (!app_passwd(passarg, dpassarg, &pass, &dpass)) {
0f113f3e
MC
1797 BIO_printf(bio_err, "Error getting password\n");
1798 goto end;
1799 }
826a42a0 1800
0f113f3e
MC
1801 if (s_key_file == NULL)
1802 s_key_file = s_cert_file;
e481f9b9 1803
0f113f3e
MC
1804 if (s_key_file2 == NULL)
1805 s_key_file2 = s_cert_file2;
ed3883d2 1806
7e1b7485 1807 if (!load_excert(&exc))
0f113f3e
MC
1808 goto end;
1809
1810 if (nocert == 0) {
bde136c8 1811 s_key = load_key(s_key_file, s_key_format, 0, pass, engine,
50eb2a50 1812 "server certificate private key");
01c12100 1813 if (s_key == NULL)
0f113f3e 1814 goto end;
826a42a0 1815
d382e796
TM
1816 s_cert = load_cert_pass(s_cert_file, s_cert_format, 1, pass,
1817 "server certificate");
0f113f3e 1818
01c12100 1819 if (s_cert == NULL)
0f113f3e 1820 goto end;
2234212c 1821 if (s_chain_file != NULL) {
ea51096e 1822 if (!load_certs(s_chain_file, 0, &s_chain, NULL,
0996dc54 1823 "server certificate chain"))
0f113f3e
MC
1824 goto end;
1825 }
e481f9b9 1826
2234212c 1827 if (tlsextcbp.servername != NULL) {
bde136c8 1828 s_key2 = load_key(s_key_file2, s_key_format, 0, pass, engine,
50eb2a50 1829 "second server certificate private key");
01c12100 1830 if (s_key2 == NULL)
0f113f3e 1831 goto end;
0f113f3e 1832
d382e796 1833 s_cert2 = load_cert_pass(s_cert_file2, s_cert_format, 1, pass,
50eb2a50 1834 "second server certificate");
0f113f3e 1835
01c12100 1836 if (s_cert2 == NULL)
0f113f3e 1837 goto end;
0f113f3e 1838 }
0f113f3e 1839 }
e481f9b9 1840#if !defined(OPENSSL_NO_NEXTPROTONEG)
0f113f3e 1841 if (next_proto_neg_in) {
f2ff1432 1842 next_proto.data = next_protos_parse(&next_proto.len, next_proto_neg_in);
0f113f3e
MC
1843 if (next_proto.data == NULL)
1844 goto end;
0f113f3e 1845 }
e481f9b9 1846#endif
0f113f3e
MC
1847 alpn_ctx.data = NULL;
1848 if (alpn_in) {
f2ff1432 1849 alpn_ctx.data = next_protos_parse(&alpn_ctx.len, alpn_in);
0f113f3e
MC
1850 if (alpn_ctx.data == NULL)
1851 goto end;
0f113f3e 1852 }
0f113f3e 1853
2234212c 1854 if (crl_file != NULL) {
0f113f3e 1855 X509_CRL *crl;
d382e796 1856 crl = load_crl(crl_file, crl_format, 0, "CRL");
9d5aca65 1857 if (crl == NULL)
0f113f3e 1858 goto end;
0f113f3e 1859 crls = sk_X509_CRL_new_null();
2234212c 1860 if (crls == NULL || !sk_X509_CRL_push(crls, crl)) {
0f113f3e
MC
1861 BIO_puts(bio_err, "Error adding CRL\n");
1862 ERR_print_errors(bio_err);
1863 X509_CRL_free(crl);
1864 goto end;
1865 }
1866 }
1867
2234212c 1868 if (s_dcert_file != NULL) {
d02b48c6 1869
0f113f3e
MC
1870 if (s_dkey_file == NULL)
1871 s_dkey_file = s_dcert_file;
1872
7e1b7485 1873 s_dkey = load_key(s_dkey_file, s_dkey_format,
50eb2a50 1874 0, dpass, engine, "second certificate private key");
01c12100 1875 if (s_dkey == NULL)
0f113f3e 1876 goto end;
0f113f3e 1877
d382e796 1878 s_dcert = load_cert_pass(s_dcert_file, s_dcert_format, 1, dpass,
22dddfb9 1879 "second server certificate");
0f113f3e 1880
2234212c 1881 if (s_dcert == NULL) {
0f113f3e
MC
1882 ERR_print_errors(bio_err);
1883 goto end;
1884 }
2234212c 1885 if (s_dchain_file != NULL) {
ea51096e 1886 if (!load_certs(s_dchain_file, 0, &s_dchain, NULL,
0996dc54 1887 "second server certificate chain"))
0f113f3e
MC
1888 goto end;
1889 }
1890
1891 }
1892
0f113f3e
MC
1893 if (bio_s_out == NULL) {
1894 if (s_quiet && !s_debug) {
1895 bio_s_out = BIO_new(BIO_s_null());
fa17f5c9 1896 if (s_msg && bio_s_msg == NULL) {
a60994df 1897 bio_s_msg = dup_bio_out(FORMAT_TEXT);
fa17f5c9 1898 if (bio_s_msg == NULL) {
1899 BIO_printf(bio_err, "Out of memory\n");
1900 goto end;
1901 }
1902 }
0f113f3e 1903 } else {
ba0b60c6 1904 bio_s_out = dup_bio_out(FORMAT_TEXT);
0f113f3e
MC
1905 }
1906 }
ba0b60c6
JJ
1907
1908 if (bio_s_out == NULL)
1909 goto end;
1910
c2403f36 1911 if (nocert) {
0f113f3e
MC
1912 s_cert_file = NULL;
1913 s_key_file = NULL;
1914 s_dcert_file = NULL;
1915 s_dkey_file = NULL;
0f113f3e
MC
1916 s_cert_file2 = NULL;
1917 s_key_file2 = NULL;
0f113f3e
MC
1918 }
1919
574ca403 1920 ctx = SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth);
0f113f3e
MC
1921 if (ctx == NULL) {
1922 ERR_print_errors(bio_err);
1923 goto end;
1924 }
693cf80c
KR
1925
1926 SSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY);
1927
32eabe34
MR
1928 if (sdebug)
1929 ssl_ctx_security_debug(ctx, sdebug);
8f8be103
RL
1930
1931 if (!config_ctx(cctx, ssl_args, ctx))
1932 goto end;
1933
287d0b94
DSH
1934 if (ssl_config) {
1935 if (SSL_CTX_config(ctx, ssl_config) == 0) {
1936 BIO_printf(bio_err, "Error using configuration \"%s\"\n",
1937 ssl_config);
d6073e27
F
1938 ERR_print_errors(bio_err);
1939 goto end;
287d0b94
DSH
1940 }
1941 }
09d62b33
MT
1942#ifndef OPENSSL_NO_SCTP
1943 if (protocol == IPPROTO_SCTP && sctp_label_bug == 1)
1944 SSL_CTX_set_mode(ctx, SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG);
1945#endif
1946
8f8be103
RL
1947 if (min_version != 0
1948 && SSL_CTX_set_min_proto_version(ctx, min_version) == 0)
0d5301af 1949 goto end;
8f8be103
RL
1950 if (max_version != 0
1951 && SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
0d5301af 1952 goto end;
287d0b94 1953
0f113f3e
MC
1954 if (session_id_prefix) {
1955 if (strlen(session_id_prefix) >= 32)
1956 BIO_printf(bio_err,
1957 "warning: id_prefix is too long, only one new session will be possible\n");
1958 if (!SSL_CTX_set_generate_session_id(ctx, generate_session_id)) {
1959 BIO_printf(bio_err, "error setting 'id_prefix'\n");
1960 ERR_print_errors(bio_err);
1961 goto end;
1962 }
1963 BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
1964 }
2234212c 1965 if (exc != NULL)
0f113f3e 1966 ssl_ctx_set_excert(ctx, exc);
0f113f3e
MC
1967
1968 if (state)
1969 SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
1970 if (no_cache)
1971 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
1972 else if (ext_cache)
1973 init_session_cache_ctx(ctx);
1974 else
1975 SSL_CTX_sess_set_cache_size(ctx, 128);
58964a49 1976
252d6d3a 1977 if (async) {
7e25dd6d 1978 SSL_CTX_set_mode(ctx, SSL_MODE_ASYNC);
252d6d3a 1979 }
28e5ea88 1980
90fc2c26
NM
1981 if (no_ca_names) {
1982 SSL_CTX_set_options(ctx, SSL_OP_DISABLE_TLSEXT_CA_NAMES);
1983 }
1984
09b90e0e
DB
1985 if (ignore_unexpected_eof)
1986 SSL_CTX_set_options(ctx, SSL_OP_IGNORE_UNEXPECTED_EOF);
e2ef7f12
TZ
1987#ifndef OPENSSL_NO_KTLS
1988 if (enable_ktls)
1989 SSL_CTX_set_options(ctx, SSL_OP_ENABLE_KTLS);
cd715b7e
MM
1990 if (use_zc_sendfile)
1991 SSL_CTX_set_options(ctx, SSL_OP_ENABLE_KTLS_TX_ZEROCOPY_SENDFILE);
e2ef7f12 1992#endif
09b90e0e 1993
36b2cfb1
F
1994 if (max_send_fragment > 0
1995 && !SSL_CTX_set_max_send_fragment(ctx, max_send_fragment)) {
1996 BIO_printf(bio_err, "%s: Max send fragment size %u is out of permitted range\n",
1997 prog, max_send_fragment);
1998 goto end;
1999 }
28e5ea88 2000
36b2cfb1
F
2001 if (split_send_fragment > 0
2002 && !SSL_CTX_set_split_send_fragment(ctx, split_send_fragment)) {
2003 BIO_printf(bio_err, "%s: Split send fragment size %u is out of permitted range\n",
2004 prog, split_send_fragment);
2005 goto end;
032c6d21 2006 }
36b2cfb1
F
2007 if (max_pipelines > 0
2008 && !SSL_CTX_set_max_pipelines(ctx, max_pipelines)) {
2009 BIO_printf(bio_err, "%s: Max pipelines %u is out of permitted range\n",
2010 prog, max_pipelines);
2011 goto end;
032c6d21 2012 }
7e25dd6d 2013
dad78fb1
MC
2014 if (read_buf_len > 0) {
2015 SSL_CTX_set_default_read_buffer_len(ctx, read_buf_len);
2016 }
e783bae2 2017#ifndef OPENSSL_NO_SRTP
ac59d705 2018 if (srtp_profiles != NULL) {
7e1b7485
RS
2019 /* Returns 0 on success! */
2020 if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles) != 0) {
ac59d705
MC
2021 BIO_printf(bio_err, "Error setting SRTP profile\n");
2022 ERR_print_errors(bio_err);
2023 goto end;
2024 }
2025 }
e783bae2 2026#endif
333f926d 2027
fd3397fc
RL
2028 if (!ctx_set_verify_locations(ctx, CAfile, noCAfile, CApath, noCApath,
2029 CAstore, noCAstore)) {
0f113f3e 2030 ERR_print_errors(bio_err);
7e1b7485 2031 goto end;
0f113f3e 2032 }
7e1b7485
RS
2033 if (vpmtouched && !SSL_CTX_set1_param(ctx, vpm)) {
2034 BIO_printf(bio_err, "Error setting verify params\n");
ac59d705
MC
2035 ERR_print_errors(bio_err);
2036 goto end;
2037 }
0f113f3e
MC
2038
2039 ssl_ctx_add_crls(ctx, crls, 0);
0f113f3e 2040
fd3397fc
RL
2041 if (!ssl_load_stores(ctx,
2042 vfyCApath, vfyCAfile, vfyCAstore,
2043 chCApath, chCAfile, chCAstore,
0f113f3e
MC
2044 crls, crl_download)) {
2045 BIO_printf(bio_err, "Error loading store locations\n");
2046 ERR_print_errors(bio_err);
2047 goto end;
2048 }
e481f9b9 2049
0f113f3e 2050 if (s_cert2) {
574ca403 2051 ctx2 = SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth);
0f113f3e
MC
2052 if (ctx2 == NULL) {
2053 ERR_print_errors(bio_err);
2054 goto end;
2055 }
2056 }
2057
2234212c 2058 if (ctx2 != NULL) {
0f113f3e
MC
2059 BIO_printf(bio_s_out, "Setting secondary ctx parameters\n");
2060
2061 if (sdebug)
402b00d5 2062 ssl_ctx_security_debug(ctx2, sdebug);
0f113f3e
MC
2063
2064 if (session_id_prefix) {
2065 if (strlen(session_id_prefix) >= 32)
2066 BIO_printf(bio_err,
2067 "warning: id_prefix is too long, only one new session will be possible\n");
2068 if (!SSL_CTX_set_generate_session_id(ctx2, generate_session_id)) {
2069 BIO_printf(bio_err, "error setting 'id_prefix'\n");
2070 ERR_print_errors(bio_err);
2071 goto end;
2072 }
2073 BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
2074 }
2234212c 2075 if (exc != NULL)
0f113f3e 2076 ssl_ctx_set_excert(ctx2, exc);
0f113f3e
MC
2077
2078 if (state)
2079 SSL_CTX_set_info_callback(ctx2, apps_ssl_info_callback);
2080
2081 if (no_cache)
2082 SSL_CTX_set_session_cache_mode(ctx2, SSL_SESS_CACHE_OFF);
2083 else if (ext_cache)
2084 init_session_cache_ctx(ctx2);
2085 else
2086 SSL_CTX_sess_set_cache_size(ctx2, 128);
2087
7e25dd6d 2088 if (async)
f4da39d2 2089 SSL_CTX_set_mode(ctx2, SSL_MODE_ASYNC);
7e25dd6d 2090
fd3397fc
RL
2091 if (!ctx_set_verify_locations(ctx2, CAfile, noCAfile, CApath,
2092 noCApath, CAstore, noCAstore)) {
0f113f3e 2093 ERR_print_errors(bio_err);
f65a8c1e 2094 goto end;
0f113f3e 2095 }
7e1b7485
RS
2096 if (vpmtouched && !SSL_CTX_set1_param(ctx2, vpm)) {
2097 BIO_printf(bio_err, "Error setting verify params\n");
ac59d705
MC
2098 ERR_print_errors(bio_err);
2099 goto end;
2100 }
ee2ffc27 2101
0f113f3e 2102 ssl_ctx_add_crls(ctx2, crls, 0);
dba31777 2103 if (!config_ctx(cctx, ssl_args, ctx2))
0f113f3e
MC
2104 goto end;
2105 }
e481f9b9 2106#ifndef OPENSSL_NO_NEXTPROTONEG
0f113f3e
MC
2107 if (next_proto.data)
2108 SSL_CTX_set_next_protos_advertised_cb(ctx, next_proto_cb,
2109 &next_proto);
e481f9b9 2110#endif
0f113f3e
MC
2111 if (alpn_ctx.data)
2112 SSL_CTX_set_alpn_select_cb(ctx, alpn_cb, &alpn_ctx);
b1277b99 2113
0f113f3e 2114 if (!no_dhe) {
163f6dc1 2115 EVP_PKEY *dhpkey = NULL;
0f113f3e 2116
2234212c 2117 if (dhfile != NULL)
d382e796 2118 dhpkey = load_keyparams(dhfile, FORMAT_UNDEF, 0, "DH", "DH parameters");
2234212c 2119 else if (s_cert_file != NULL)
ef044913
SL
2120 dhpkey = load_keyparams_suppress(s_cert_file, FORMAT_UNDEF, 0, "DH",
2121 "DH parameters", 1);
0f113f3e 2122
163f6dc1 2123 if (dhpkey != NULL) {
0f113f3e
MC
2124 BIO_printf(bio_s_out, "Setting temp DH parameters\n");
2125 } else {
2126 BIO_printf(bio_s_out, "Using default temp DH parameters\n");
2127 }
2128 (void)BIO_flush(bio_s_out);
2129
163f6dc1 2130 if (dhpkey == NULL) {
0f113f3e 2131 SSL_CTX_set_dh_auto(ctx, 1);
163f6dc1
MC
2132 } else {
2133 /*
2134 * We need 2 references: one for use by ctx and one for use by
2135 * ctx2
2136 */
2137 if (!EVP_PKEY_up_ref(dhpkey)) {
2138 EVP_PKEY_free(dhpkey);
2139 goto end;
2140 }
2141 if (!SSL_CTX_set0_tmp_dh_pkey(ctx, dhpkey)) {
2142 BIO_puts(bio_err, "Error setting temp DH parameters\n");
2143 ERR_print_errors(bio_err);
2144 /* Free 2 references */
2145 EVP_PKEY_free(dhpkey);
2146 EVP_PKEY_free(dhpkey);
2147 goto end;
2148 }
9912be1b 2149 }
e481f9b9 2150
2234212c 2151 if (ctx2 != NULL) {
163f6dc1 2152 if (dhfile != NULL) {
ef044913
SL
2153 EVP_PKEY *dhpkey2 = load_keyparams_suppress(s_cert_file2,
2154 FORMAT_UNDEF,
2155 0, "DH",
2156 "DH parameters", 1);
163f6dc1
MC
2157
2158 if (dhpkey2 != NULL) {
0f113f3e
MC
2159 BIO_printf(bio_s_out, "Setting temp DH parameters\n");
2160 (void)BIO_flush(bio_s_out);
2161
163f6dc1
MC
2162 EVP_PKEY_free(dhpkey);
2163 dhpkey = dhpkey2;
0f113f3e
MC
2164 }
2165 }
163f6dc1 2166 if (dhpkey == NULL) {
0f113f3e 2167 SSL_CTX_set_dh_auto(ctx2, 1);
163f6dc1 2168 } else if (!SSL_CTX_set0_tmp_dh_pkey(ctx2, dhpkey)) {
0f113f3e
MC
2169 BIO_puts(bio_err, "Error setting temp DH parameters\n");
2170 ERR_print_errors(bio_err);
163f6dc1 2171 EVP_PKEY_free(dhpkey);
0f113f3e
MC
2172 goto end;
2173 }
163f6dc1 2174 dhpkey = NULL;
0f113f3e 2175 }
163f6dc1 2176 EVP_PKEY_free(dhpkey);
0f113f3e 2177 }
d02b48c6 2178
0f113f3e
MC
2179 if (!set_cert_key_stuff(ctx, s_cert, s_key, s_chain, build_chain))
2180 goto end;
e481f9b9 2181
0f113f3e
MC
2182 if (s_serverinfo_file != NULL
2183 && !SSL_CTX_use_serverinfo_file(ctx, s_serverinfo_file)) {
2184 ERR_print_errors(bio_err);
2185 goto end;
2186 }
e481f9b9 2187
2234212c
PY
2188 if (ctx2 != NULL
2189 && !set_cert_key_stuff(ctx2, s_cert2, s_key2, NULL, build_chain))
0f113f3e 2190 goto end;
e481f9b9 2191
0f113f3e
MC
2192 if (s_dcert != NULL) {
2193 if (!set_cert_key_stuff(ctx, s_dcert, s_dkey, s_dchain, build_chain))
2194 goto end;
2195 }
d02b48c6 2196
0f113f3e
MC
2197 if (no_resume_ephemeral) {
2198 SSL_CTX_set_not_resumable_session_callback(ctx,
2199 not_resumable_sess_cb);
e481f9b9 2200
2234212c 2201 if (ctx2 != NULL)
0f113f3e
MC
2202 SSL_CTX_set_not_resumable_session_callback(ctx2,
2203 not_resumable_sess_cb);
0f113f3e 2204 }
ddac1974 2205#ifndef OPENSSL_NO_PSK
b5292f7b 2206 if (psk_key != NULL) {
0f113f3e 2207 if (s_debug)
d6073e27 2208 BIO_printf(bio_s_out, "PSK key given, setting server callback\n");
0f113f3e
MC
2209 SSL_CTX_set_psk_server_callback(ctx, psk_server_cb);
2210 }
ddac1974 2211
9a1c170d
E
2212 if (psk_identity_hint != NULL) {
2213 if (min_version == TLS1_3_VERSION) {
2214 BIO_printf(bio_s_out, "PSK warning: there is NO identity hint in TLSv1.3\n");
2215 } else {
2216 if (!SSL_CTX_use_psk_identity_hint(ctx, psk_identity_hint)) {
2217 BIO_printf(bio_err, "error setting PSK identity hint to context\n");
2218 ERR_print_errors(bio_err);
2219 goto end;
2220 }
2221 }
0f113f3e 2222 }
ddac1974 2223#endif
df894947
MC
2224 if (psksessf != NULL) {
2225 BIO *stmp = BIO_new_file(psksessf, "r");
2226
2227 if (stmp == NULL) {
2228 BIO_printf(bio_err, "Can't open PSK session file %s\n", psksessf);
2229 ERR_print_errors(bio_err);
2230 goto end;
2231 }
2232 psksess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);
2233 BIO_free(stmp);
2234 if (psksess == NULL) {
2235 BIO_printf(bio_err, "Can't read PSK session file %s\n", psksessf);
2236 ERR_print_errors(bio_err);
2237 goto end;
2238 }
5ffff599 2239
df894947 2240 }
ddac1974 2241
5ffff599
MC
2242 if (psk_key != NULL || psksess != NULL)
2243 SSL_CTX_set_psk_find_session_callback(ctx, psk_find_session_cb);
2244
0f113f3e 2245 SSL_CTX_set_verify(ctx, s_server_verify, verify_callback);
61986d32 2246 if (!SSL_CTX_set_session_id_context(ctx,
d6073e27 2247 (void *)&s_server_session_id_context,
cbe29648 2248 sizeof(s_server_session_id_context))) {
ac59d705
MC
2249 BIO_printf(bio_err, "error setting session id context\n");
2250 ERR_print_errors(bio_err);
2251 goto end;
2252 }
d02b48c6 2253
0f113f3e
MC
2254 /* Set DTLS cookie generation and verification callbacks */
2255 SSL_CTX_set_cookie_generate_cb(ctx, generate_cookie_callback);
2256 SSL_CTX_set_cookie_verify_cb(ctx, verify_cookie_callback);
07a9d1a2 2257
3fa2812f
BS
2258 /* Set TLS1.3 cookie generation and verification callbacks */
2259 SSL_CTX_set_stateless_cookie_generate_cb(ctx, generate_stateless_cookie_callback);
2260 SSL_CTX_set_stateless_cookie_verify_cb(ctx, verify_stateless_cookie_callback);
2261
2234212c 2262 if (ctx2 != NULL) {
0f113f3e 2263 SSL_CTX_set_verify(ctx2, s_server_verify, verify_callback);
61986d32 2264 if (!SSL_CTX_set_session_id_context(ctx2,
7e1b7485 2265 (void *)&s_server_session_id_context,
cbe29648 2266 sizeof(s_server_session_id_context))) {
ac59d705
MC
2267 BIO_printf(bio_err, "error setting session id context\n");
2268 ERR_print_errors(bio_err);
2269 goto end;
2270 }
0f113f3e
MC
2271 tlsextcbp.biodebug = bio_s_out;
2272 SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb);
2273 SSL_CTX_set_tlsext_servername_arg(ctx2, &tlsextcbp);
2274 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
2275 SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
2276 }
f1fd4544 2277
edc032b5 2278#ifndef OPENSSL_NO_SRP
0f113f3e 2279 if (srp_verifier_file != NULL) {
6d2a1eff
MC
2280 if (!set_up_srp_verifier_file(ctx, &srp_callback_parm, srpuserseed,
2281 srp_verifier_file))
0f113f3e 2282 goto end;
0f113f3e
MC
2283 } else
2284#endif
2285 if (CAfile != NULL) {
2286 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(CAfile));
e481f9b9 2287
0f113f3e
MC
2288 if (ctx2)
2289 SSL_CTX_set_client_CA_list(ctx2, SSL_load_client_CA_file(CAfile));
0f113f3e 2290 }
3e41ac35 2291#ifndef OPENSSL_NO_OCSP
be0c0361
AE
2292 if (s_tlsextstatus) {
2293 SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb);
2294 SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp);
2295 if (ctx2) {
2296 SSL_CTX_set_tlsext_status_cb(ctx2, cert_status_cb);
2297 SSL_CTX_set_tlsext_status_arg(ctx2, &tlscstatp);
2298 }
2299 }
3e41ac35 2300#endif
4bf73e9f
PW
2301 if (set_keylog_file(ctx, keylog_file))
2302 goto end;
0f113f3e 2303
6746648c 2304 if (max_early_data >= 0)
048b1893 2305 SSL_CTX_set_max_early_data(ctx, max_early_data);
bafe9cf5
MC
2306 if (recv_max_early_data >= 0)
2307 SSL_CTX_set_recv_max_early_data(ctx, recv_max_early_data);
048b1893 2308
b67cb09f
TS
2309 if (cert_comp) {
2310 BIO_printf(bio_s_out, "Compressing certificates\n");
2311 if (!SSL_CTX_compress_certs(ctx, 0))
2312 BIO_printf(bio_s_out, "Error compressing certs on ctx\n");
2313 if (ctx2 != NULL && !SSL_CTX_compress_certs(ctx2, 0))
2314 BIO_printf(bio_s_out, "Error compressing certs on ctx2\n");
2315 }
3c95ef22
TS
2316 if (enable_server_rpk)
2317 if (!SSL_CTX_set1_server_cert_type(ctx, cert_type_rpk, sizeof(cert_type_rpk))) {
2318 BIO_printf(bio_s_out, "Error setting server certificate types\n");
2319 goto end;
2320 }
2321 if (enable_client_rpk)
2322 if (!SSL_CTX_set1_client_cert_type(ctx, cert_type_rpk, sizeof(cert_type_rpk))) {
2323 BIO_printf(bio_s_out, "Error setting server certificate types\n");
2324 goto end;
2325 }
b67cb09f 2326
0f113f3e
MC
2327 if (rev)
2328 server_cb = rev_body;
2329 else if (www)
2330 server_cb = www_body;
2331 else
2332 server_cb = sv_body;
ab69ac00
RL
2333#ifdef AF_UNIX
2334 if (socket_family == AF_UNIX
2335 && unlink_unix_path)
2336 unlink(host);
0f113f3e 2337#endif
a3e53d56
TS
2338 if (tfo)
2339 BIO_printf(bio_s_out, "Listening for TFO\n");
72d0bc84 2340 do_server(&accept_socket, host, port, socket_family, socket_type, protocol,
a3e53d56 2341 server_cb, context, naccept, bio_s_out, tfo);
0f113f3e
MC
2342 print_stats(bio_s_out, ctx);
2343 ret = 0;
2344 end:
62adbcee 2345 SSL_CTX_free(ctx);
9561e2a1 2346 SSL_SESSION_free(psksess);
4bf73e9f 2347 set_keylog_file(NULL, NULL);
222561fe
RS
2348 X509_free(s_cert);
2349 sk_X509_CRL_pop_free(crls, X509_CRL_free);
2350 X509_free(s_dcert);
c5ba2d99
RS
2351 EVP_PKEY_free(s_key);
2352 EVP_PKEY_free(s_dkey);
79b2a2f2
DDO
2353 OSSL_STACK_OF_X509_free(s_chain);
2354 OSSL_STACK_OF_X509_free(s_dchain);
25aaa98a
RS
2355 OPENSSL_free(pass);
2356 OPENSSL_free(dpass);
ab69ac00
RL
2357 OPENSSL_free(host);
2358 OPENSSL_free(port);
222561fe 2359 X509_VERIFY_PARAM_free(vpm);
0f113f3e 2360 free_sessions();
25aaa98a
RS
2361 OPENSSL_free(tlscstatp.host);
2362 OPENSSL_free(tlscstatp.port);
2363 OPENSSL_free(tlscstatp.path);
62adbcee 2364 SSL_CTX_free(ctx2);
222561fe 2365 X509_free(s_cert2);
c5ba2d99 2366 EVP_PKEY_free(s_key2);
e481f9b9 2367#ifndef OPENSSL_NO_NEXTPROTONEG
25aaa98a 2368 OPENSSL_free(next_proto.data);
0f113f3e 2369#endif
e481f9b9 2370 OPENSSL_free(alpn_ctx.data);
0f113f3e 2371 ssl_excert_free(exc);
7e1b7485 2372 sk_OPENSSL_STRING_free(ssl_args);
62adbcee 2373 SSL_CONF_CTX_free(cctx);
dd1abd44 2374 release_engine(engine);
ca3a82c3
RS
2375 BIO_free(bio_s_out);
2376 bio_s_out = NULL;
2377 BIO_free(bio_s_msg);
2378 bio_s_msg = NULL;
5fd1478d
MC
2379#ifdef CHARSET_EBCDIC
2380 BIO_meth_free(methods_ebcdic);
2381#endif
26a7d938 2382 return ret;
0f113f3e 2383}
d02b48c6 2384
6b691a5c 2385static void print_stats(BIO *bio, SSL_CTX *ssl_ctx)
0f113f3e
MC
2386{
2387 BIO_printf(bio, "%4ld items in the session cache\n",
2388 SSL_CTX_sess_number(ssl_ctx));
2389 BIO_printf(bio, "%4ld client connects (SSL_connect())\n",
2390 SSL_CTX_sess_connect(ssl_ctx));
2391 BIO_printf(bio, "%4ld client renegotiates (SSL_connect())\n",
2392 SSL_CTX_sess_connect_renegotiate(ssl_ctx));
2393 BIO_printf(bio, "%4ld client connects that finished\n",
2394 SSL_CTX_sess_connect_good(ssl_ctx));
2395 BIO_printf(bio, "%4ld server accepts (SSL_accept())\n",
2396 SSL_CTX_sess_accept(ssl_ctx));
2397 BIO_printf(bio, "%4ld server renegotiates (SSL_accept())\n",
2398 SSL_CTX_sess_accept_renegotiate(ssl_ctx));
2399 BIO_printf(bio, "%4ld server accepts that finished\n",
2400 SSL_CTX_sess_accept_good(ssl_ctx));
2401 BIO_printf(bio, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx));
2402 BIO_printf(bio, "%4ld session cache misses\n",
2403 SSL_CTX_sess_misses(ssl_ctx));
2404 BIO_printf(bio, "%4ld session cache timeouts\n",
2405 SSL_CTX_sess_timeouts(ssl_ctx));
2406 BIO_printf(bio, "%4ld callback cache hits\n",
2407 SSL_CTX_sess_cb_hits(ssl_ctx));
2408 BIO_printf(bio, "%4ld cache full overflows (%ld allowed)\n",
2409 SSL_CTX_sess_cache_full(ssl_ctx),
2410 SSL_CTX_sess_get_cache_size(ssl_ctx));
2411}
d02b48c6 2412
a6d52f17
DF
2413static long int count_reads_callback(BIO *bio, int cmd, const char *argp, size_t len,
2414 int argi, long argl, int ret, size_t *processed)
2415{
2416 unsigned int *p_counter = (unsigned int *)BIO_get_callback_arg(bio);
2417
2418 switch (cmd) {
2419 case BIO_CB_READ: /* No break here */
2420 case BIO_CB_GETS:
2421 if (p_counter != NULL)
2422 ++*p_counter;
2423 break;
2424 default:
2425 break;
2426 }
2427
2428 if (s_debug) {
2429 BIO_set_callback_arg(bio, (char *)bio_s_out);
2430 ret = (int)bio_dump_callback(bio, cmd, argp, len, argi, argl, ret, processed);
2431 BIO_set_callback_arg(bio, (char *)p_counter);
2432 }
2433
2434 return ret;
2435}
2436
72d0bc84 2437static int sv_body(int s, int stype, int prot, unsigned char *context)
0f113f3e
MC
2438{
2439 char *buf = NULL;
2440 fd_set readfds;
2441 int ret = 1, width;
82b81161 2442 int k;
0f113f3e
MC
2443 unsigned long l;
2444 SSL *con = NULL;
2445 BIO *sbio;
0f113f3e 2446 struct timeval timeout;
ff25dd1a 2447#if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS))
0f113f3e 2448 struct timeval *timeoutp;
06f4536a 2449#endif
72d0bc84 2450#ifndef OPENSSL_NO_DTLS
d88ab353 2451# ifndef OPENSSL_NO_SCTP
72d0bc84 2452 int isdtls = (stype == SOCK_DGRAM || prot == IPPROTO_SCTP);
d88ab353 2453# else
72d0bc84 2454 int isdtls = (stype == SOCK_DGRAM);
d88ab353 2455# endif
72d0bc84 2456#endif
d02b48c6 2457
68dc6824 2458 buf = app_malloc(bufsize, "server buffer");
0f113f3e 2459 if (s_nbio) {
ba810815 2460 if (!BIO_socket_nbio(s, 1))
0f113f3e 2461 ERR_print_errors(bio_err);
ba810815
RS
2462 else if (!s_quiet)
2463 BIO_printf(bio_err, "Turned on non blocking io\n");
0f113f3e 2464 }
d02b48c6 2465
f84a648c 2466 con = SSL_new(ctx);
0f113f3e 2467 if (con == NULL) {
f84a648c
K
2468 ret = -1;
2469 goto err;
2470 }
e481f9b9 2471
f84a648c
K
2472 if (s_tlsextdebug) {
2473 SSL_set_tlsext_debug_callback(con, tlsext_cb);
2474 SSL_set_tlsext_debug_arg(con, bio_s_out);
2475 }
e481f9b9 2476
f84a648c
K
2477 if (context != NULL
2478 && !SSL_set_session_id_context(con, context,
c62cd07d 2479 (unsigned int)strlen((char *)context))) {
f84a648c
K
2480 BIO_printf(bio_err, "Error setting session id context\n");
2481 ret = -1;
2482 goto err;
ac59d705 2483 }
f84a648c 2484
61986d32 2485 if (!SSL_clear(con)) {
ac59d705
MC
2486 BIO_printf(bio_err, "Error clearing SSL connection\n");
2487 ret = -1;
2488 goto err;
0f113f3e 2489 }
a7a14a23 2490#ifndef OPENSSL_NO_DTLS
72d0bc84 2491 if (isdtls) {
d88ab353 2492# ifndef OPENSSL_NO_SCTP
72d0bc84
MC
2493 if (prot == IPPROTO_SCTP)
2494 sbio = BIO_new_dgram_sctp(s, BIO_NOCLOSE);
2495 else
d88ab353 2496# endif
72d0bc84 2497 sbio = BIO_new_dgram(s, BIO_NOCLOSE);
ba0b60c6
JJ
2498 if (sbio == NULL) {
2499 BIO_printf(bio_err, "Unable to create BIO\n");
2500 ERR_print_errors(bio_err);
2501 goto err;
2502 }
0f113f3e
MC
2503
2504 if (enable_timeouts) {
2505 timeout.tv_sec = 0;
2506 timeout.tv_usec = DGRAM_RCV_TIMEOUT;
2507 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
2508
2509 timeout.tv_sec = 0;
2510 timeout.tv_usec = DGRAM_SND_TIMEOUT;
2511 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
2512 }
2513
2514 if (socket_mtu) {
2515 if (socket_mtu < DTLS_get_link_min_mtu(con)) {
2516 BIO_printf(bio_err, "MTU too small. Must be at least %ld\n",
2517 DTLS_get_link_min_mtu(con));
2518 ret = -1;
2519 BIO_free(sbio);
2520 goto err;
2521 }
2522 SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
2523 if (!DTLS_set_link_mtu(con, socket_mtu)) {
2524 BIO_printf(bio_err, "Failed to set MTU\n");
2525 ret = -1;
2526 BIO_free(sbio);
2527 goto err;
2528 }
2529 } else
2530 /* want to do MTU discovery */
2531 BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
36d16f8e 2532
a5eef31e 2533# ifndef OPENSSL_NO_SCTP
1b3011ab
MC
2534 if (prot != IPPROTO_SCTP)
2535# endif
72d0bc84
MC
2536 /* Turn on cookie exchange. Not necessary for SCTP */
2537 SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE);
0f113f3e 2538 } else
a7a14a23 2539#endif
0f113f3e 2540 sbio = BIO_new_socket(s, BIO_NOCLOSE);
36d16f8e 2541
72d0bc84
MC
2542 if (sbio == NULL) {
2543 BIO_printf(bio_err, "Unable to create BIO\n");
2544 ERR_print_errors(bio_err);
2545 goto err;
2546 }
2547
0f113f3e
MC
2548 if (s_nbio_test) {
2549 BIO *test;
d02b48c6 2550
0f113f3e 2551 test = BIO_new(BIO_f_nbio_test());
ba0b60c6
JJ
2552 if (test == NULL) {
2553 BIO_printf(bio_err, "Unable to create BIO\n");
2554 ret = -1;
2555 BIO_free(sbio);
2556 goto err;
2557 }
0f113f3e
MC
2558 sbio = BIO_push(test, sbio);
2559 }
0f113f3e
MC
2560
2561 SSL_set_bio(con, sbio, sbio);
2562 SSL_set_accept_state(con);
2563 /* SSL_set_fd(con,s); */
2564
a6d52f17 2565 BIO_set_callback_ex(SSL_get_rbio(con), count_reads_callback);
0f113f3e 2566 if (s_msg) {
93ab9e42 2567#ifndef OPENSSL_NO_SSL_TRACE
0f113f3e
MC
2568 if (s_msg == 2)
2569 SSL_set_msg_callback(con, SSL_trace);
2570 else
93ab9e42 2571#endif
0f113f3e
MC
2572 SSL_set_msg_callback(con, msg_cb);
2573 SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
2574 }
e481f9b9 2575
0f113f3e
MC
2576 if (s_tlsextdebug) {
2577 SSL_set_tlsext_debug_callback(con, tlsext_cb);
2578 SSL_set_tlsext_debug_arg(con, bio_s_out);
2579 }
d02b48c6 2580
e0655186 2581 if (early_data) {
f533fbd4 2582 int write_header = 1, edret = SSL_READ_EARLY_DATA_ERROR;
e0655186
MC
2583 size_t readbytes;
2584
f533fbd4 2585 while (edret != SSL_READ_EARLY_DATA_FINISH) {
e0655186 2586 for (;;) {
f533fbd4
MC
2587 edret = SSL_read_early_data(con, buf, bufsize, &readbytes);
2588 if (edret != SSL_READ_EARLY_DATA_ERROR)
e0655186
MC
2589 break;
2590
2591 switch (SSL_get_error(con, 0)) {
2592 case SSL_ERROR_WANT_WRITE:
2593 case SSL_ERROR_WANT_ASYNC:
2594 case SSL_ERROR_WANT_READ:
2595 /* Just keep trying - busy waiting */
2596 continue;
2597 default:
2598 BIO_printf(bio_err, "Error reading early data\n");
2599 ERR_print_errors(bio_err);
2600 goto err;
2601 }
2602 }
2603 if (readbytes > 0) {
2604 if (write_header) {
2605 BIO_printf(bio_s_out, "Early data received:\n");
2606 write_header = 0;
2607 }
2608 raw_write_stdout(buf, (unsigned int)readbytes);
2609 (void)BIO_flush(bio_s_out);
2610 }
2611 }
3b587356
MC
2612 if (write_header) {
2613 if (SSL_get_early_data_status(con) == SSL_EARLY_DATA_NOT_SENT)
2614 BIO_printf(bio_s_out, "No early data received\n");
2615 else
2616 BIO_printf(bio_s_out, "Early data was rejected\n");
2617 } else {
e0655186 2618 BIO_printf(bio_s_out, "\nEnd of early data\n");
3b587356 2619 }
ade1e888
MC
2620 if (SSL_is_init_finished(con))
2621 print_connection_info(con);
e0655186
MC
2622 }
2623
51e5133d
RL
2624 if (fileno_stdin() > s)
2625 width = fileno_stdin() + 1;
c7bdb6a3
RL
2626 else
2627 width = s + 1;
0f113f3e 2628 for (;;) {
82b81161 2629 int i;
0f113f3e
MC
2630 int read_from_terminal;
2631 int read_from_sslcon;
a2a01589 2632
0f113f3e 2633 read_from_terminal = 0;
fd068d50 2634 read_from_sslcon = SSL_has_pending(con)
64c07bd2 2635 || (async && SSL_waiting_for_async(con));
a2a01589 2636
0f113f3e
MC
2637 if (!read_from_sslcon) {
2638 FD_ZERO(&readfds);
1fbab1dc 2639#if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
51e5133d 2640 openssl_fdset(fileno_stdin(), &readfds);
0f113f3e
MC
2641#endif
2642 openssl_fdset(s, &readfds);
2643 /*
2644 * Note: under VMS with SOCKETSHR the second parameter is
2645 * currently of type (int *) whereas under other systems it is
2646 * (void *) if you don't have a cast it will choke the compiler:
2647 * if you do have a cast then you can either go for (int *) or
2648 * (void *).
2649 */
1fbab1dc 2650#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
0f113f3e
MC
2651 /*
2652 * Under DOS (non-djgpp) and Windows we can't select on stdin:
2653 * only on sockets. As a workaround we timeout the select every
2654 * second and check for any keypress. In a proper Windows
2655 * application we wouldn't do this because it is inefficient.
2656 */
ff25dd1a
RL
2657 timeout.tv_sec = 1;
2658 timeout.tv_usec = 0;
2659 i = select(width, (void *)&readfds, NULL, NULL, &timeout);
75dd6c1a 2660 if (has_stdin_waiting())
0f113f3e 2661 read_from_terminal = 1;
75dd6c1a
MC
2662 if ((i < 0) || (!i && !read_from_terminal))
2663 continue;
06f4536a 2664#else
6f6da2fe 2665 if (SSL_is_dtls(con) && DTLSv1_get_timeout(con, &timeout))
0f113f3e
MC
2666 timeoutp = &timeout;
2667 else
2668 timeoutp = NULL;
2669
2670 i = select(width, (void *)&readfds, NULL, NULL, timeoutp);
2671
6f6da2fe 2672 if ((SSL_is_dtls(con)) && DTLSv1_handle_timeout(con) > 0)
0f113f3e 2673 BIO_printf(bio_err, "TIMEOUT occurred\n");
0f113f3e
MC
2674
2675 if (i <= 0)
2676 continue;
51e5133d 2677 if (FD_ISSET(fileno_stdin(), &readfds))
0f113f3e
MC
2678 read_from_terminal = 1;
2679#endif
2680 if (FD_ISSET(s, &readfds))
2681 read_from_sslcon = 1;
2682 }
2683 if (read_from_terminal) {
2684 if (s_crlf) {
2685 int j, lf_num;
2686
c7bdb6a3 2687 i = raw_read_stdin(buf, bufsize / 2);
c7bdb6a3 2688 lf_num = 0;
0f113f3e
MC
2689 /* both loops are skipped when i <= 0 */
2690 for (j = 0; j < i; j++)
2691 if (buf[j] == '\n')
2692 lf_num++;
2693 for (j = i - 1; j >= 0; j--) {
2694 buf[j + lf_num] = buf[j];
2695 if (buf[j] == '\n') {
2696 lf_num--;
2697 i++;
2698 buf[j + lf_num] = '\r';
2699 }
2700 }
2701 assert(lf_num == 0);
2234212c 2702 } else {
c7bdb6a3 2703 i = raw_read_stdin(buf, bufsize);
2234212c 2704 }
51e5133d 2705
0f113f3e
MC
2706 if (!s_quiet && !s_brief) {
2707 if ((i <= 0) || (buf[0] == 'Q')) {
2708 BIO_printf(bio_s_out, "DONE\n");
7e1b7485 2709 (void)BIO_flush(bio_s_out);
8731a4fc 2710 BIO_closesocket(s);
0f113f3e
MC
2711 close_accept_socket();
2712 ret = -11;
2713 goto err;
2714 }
2715 if ((i <= 0) || (buf[0] == 'q')) {
2716 BIO_printf(bio_s_out, "DONE\n");
7e1b7485 2717 (void)BIO_flush(bio_s_out);
0f113f3e 2718 if (SSL_version(con) != DTLS1_VERSION)
8731a4fc 2719 BIO_closesocket(s);
0f113f3e
MC
2720 /*
2721 * close_accept_socket(); ret= -11;
2722 */
2723 goto err;
2724 }
0f113f3e
MC
2725 if ((buf[0] == 'r') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
2726 SSL_renegotiate(con);
2727 i = SSL_do_handshake(con);
2728 printf("SSL_do_handshake -> %d\n", i);
0f113f3e 2729 continue;
0f113f3e
MC
2730 }
2731 if ((buf[0] == 'R') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
2732 SSL_set_verify(con,
2733 SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
2734 NULL);
2735 SSL_renegotiate(con);
2736 i = SSL_do_handshake(con);
2737 printf("SSL_do_handshake -> %d\n", i);
0f113f3e 2738 continue;
0f113f3e 2739 }
34df45b5
MC
2740 if ((buf[0] == 'K' || buf[0] == 'k')
2741 && ((buf[1] == '\n') || (buf[1] == '\r'))) {
2742 SSL_key_update(con, buf[0] == 'K' ?
2743 SSL_KEY_UPDATE_REQUESTED
2744 : SSL_KEY_UPDATE_NOT_REQUESTED);
2745 i = SSL_do_handshake(con);
2746 printf("SSL_do_handshake -> %d\n", i);
34df45b5 2747 continue;
34df45b5 2748 }
9d75dce3
TS
2749 if (buf[0] == 'c' && ((buf[1] == '\n') || (buf[1] == '\r'))) {
2750 SSL_set_verify(con, SSL_VERIFY_PEER, NULL);
2751 i = SSL_verify_client_post_handshake(con);
2752 if (i == 0) {
2753 printf("Failed to initiate request\n");
2754 ERR_print_errors(bio_err);
2755 } else {
2756 i = SSL_do_handshake(con);
2757 printf("SSL_do_handshake -> %d\n", i);
9d75dce3
TS
2758 }
2759 continue;
2760 }
0f113f3e 2761 if (buf[0] == 'P') {
4bac25e1 2762 static const char str[] = "Lets print some clear text\n";
2763 BIO_write(SSL_get_wbio(con), str, sizeof(str) -1);
0f113f3e
MC
2764 }
2765 if (buf[0] == 'S') {
2766 print_stats(bio_s_out, SSL_get_SSL_CTX(con));
2767 }
2768 }
a53955d8 2769#ifdef CHARSET_EBCDIC
0f113f3e 2770 ebcdic2ascii(buf, buf, i);
a53955d8 2771#endif
0f113f3e
MC
2772 l = k = 0;
2773 for (;;) {
2774 /* should do a select for the write */
58964a49 2775#ifdef RENEG
54463e4f
F
2776 static count = 0;
2777 if (++count == 100) {
2778 count = 0;
2779 SSL_renegotiate(con);
0f113f3e 2780 }
d02b48c6 2781#endif
0f113f3e 2782 k = SSL_write(con, &(buf[l]), (unsigned int)i);
9641be2a 2783#ifndef OPENSSL_NO_SRP
0f113f3e
MC
2784 while (SSL_get_error(con, k) == SSL_ERROR_WANT_X509_LOOKUP) {
2785 BIO_printf(bio_s_out, "LOOKUP renego during write\n");
6d2a1eff
MC
2786
2787 lookup_srp_user(&srp_callback_parm, bio_s_out);
2788
0f113f3e
MC
2789 k = SSL_write(con, &(buf[l]), (unsigned int)i);
2790 }
2791#endif
2792 switch (SSL_get_error(con, k)) {
2793 case SSL_ERROR_NONE:
2794 break;
7e25dd6d
MC
2795 case SSL_ERROR_WANT_ASYNC:
2796 BIO_printf(bio_s_out, "Write BLOCK (Async)\n");
384f08dc 2797 (void)BIO_flush(bio_s_out);
f4da39d2 2798 wait_for_async(con);
7e25dd6d 2799 break;
0f113f3e
MC
2800 case SSL_ERROR_WANT_WRITE:
2801 case SSL_ERROR_WANT_READ:
2802 case SSL_ERROR_WANT_X509_LOOKUP:
2803 BIO_printf(bio_s_out, "Write BLOCK\n");
384f08dc 2804 (void)BIO_flush(bio_s_out);
0f113f3e 2805 break;
fc7f190c
MC
2806 case SSL_ERROR_WANT_ASYNC_JOB:
2807 /*
2808 * This shouldn't ever happen in s_server. Treat as an error
2809 */
0f113f3e
MC
2810 case SSL_ERROR_SYSCALL:
2811 case SSL_ERROR_SSL:
2812 BIO_printf(bio_s_out, "ERROR\n");
7e1b7485 2813 (void)BIO_flush(bio_s_out);
0f113f3e
MC
2814 ERR_print_errors(bio_err);
2815 ret = 1;
2816 goto err;
2817 /* break; */
2818 case SSL_ERROR_ZERO_RETURN:
2819 BIO_printf(bio_s_out, "DONE\n");
7e1b7485 2820 (void)BIO_flush(bio_s_out);
0f113f3e
MC
2821 ret = 1;
2822 goto err;
2823 }
00d565cf
RS
2824 if (k > 0) {
2825 l += k;
2826 i -= k;
2827 }
0f113f3e
MC
2828 if (i <= 0)
2829 break;
2830 }
2831 }
2832 if (read_from_sslcon) {
64c07bd2
MC
2833 /*
2834 * init_ssl_connection handles all async events itself so if we're
2835 * waiting for async then we shouldn't go back into
2836 * init_ssl_connection
2837 */
2838 if ((!async || !SSL_waiting_for_async(con))
2839 && !SSL_is_init_finished(con)) {
a6d52f17
DF
2840 /*
2841 * Count number of reads during init_ssl_connection.
2842 * It helps us to distinguish configuration errors from errors
2843 * caused by a client.
2844 */
2845 unsigned int read_counter = 0;
2846
2847 BIO_set_callback_arg(SSL_get_rbio(con), (char *)&read_counter);
0f113f3e 2848 i = init_ssl_connection(con);
a6d52f17
DF
2849 BIO_set_callback_arg(SSL_get_rbio(con), NULL);
2850
2851 /*
2852 * If initialization fails without reads, then
2853 * there was a fatal error in configuration.
2854 */
2855 if (i <= 0 && read_counter == 0) {
2856 ret = -1;
2857 goto err;
2858 }
0f113f3e
MC
2859 if (i < 0) {
2860 ret = 0;
2861 goto err;
2862 } else if (i == 0) {
2863 ret = 1;
2864 goto err;
2865 }
2866 } else {
2867 again:
2868 i = SSL_read(con, (char *)buf, bufsize);
9641be2a 2869#ifndef OPENSSL_NO_SRP
0f113f3e
MC
2870 while (SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) {
2871 BIO_printf(bio_s_out, "LOOKUP renego during read\n");
6d2a1eff
MC
2872
2873 lookup_srp_user(&srp_callback_parm, bio_s_out);
2874
0f113f3e
MC
2875 i = SSL_read(con, (char *)buf, bufsize);
2876 }
2877#endif
2878 switch (SSL_get_error(con, i)) {
2879 case SSL_ERROR_NONE:
a53955d8 2880#ifdef CHARSET_EBCDIC
0f113f3e
MC
2881 ascii2ebcdic(buf, buf, i);
2882#endif
2883 raw_write_stdout(buf, (unsigned int)i);
384f08dc 2884 (void)BIO_flush(bio_s_out);
fd068d50 2885 if (SSL_has_pending(con))
0f113f3e
MC
2886 goto again;
2887 break;
7e25dd6d 2888 case SSL_ERROR_WANT_ASYNC:
f4da39d2 2889 BIO_printf(bio_s_out, "Read BLOCK (Async)\n");
384f08dc 2890 (void)BIO_flush(bio_s_out);
f4da39d2
MC
2891 wait_for_async(con);
2892 break;
0f113f3e
MC
2893 case SSL_ERROR_WANT_WRITE:
2894 case SSL_ERROR_WANT_READ:
2895 BIO_printf(bio_s_out, "Read BLOCK\n");
384f08dc 2896 (void)BIO_flush(bio_s_out);
0f113f3e 2897 break;
fc7f190c
MC
2898 case SSL_ERROR_WANT_ASYNC_JOB:
2899 /*
2900 * This shouldn't ever happen in s_server. Treat as an error
2901 */
0f113f3e
MC
2902 case SSL_ERROR_SYSCALL:
2903 case SSL_ERROR_SSL:
2904 BIO_printf(bio_s_out, "ERROR\n");
7e1b7485 2905 (void)BIO_flush(bio_s_out);
0f113f3e
MC
2906 ERR_print_errors(bio_err);
2907 ret = 1;
2908 goto err;
2909 case SSL_ERROR_ZERO_RETURN:
2910 BIO_printf(bio_s_out, "DONE\n");
7e1b7485 2911 (void)BIO_flush(bio_s_out);
0f113f3e
MC
2912 ret = 1;
2913 goto err;
2914 }
2915 }
2916 }
2917 }
2918 err:
2919 if (con != NULL) {
2920 BIO_printf(bio_s_out, "shutting down SSL\n");
edbb56ee 2921 do_ssl_shutdown(con);
0f113f3e
MC
2922 SSL_free(con);
2923 }
2924 BIO_printf(bio_s_out, "CONNECTION CLOSED\n");
4b45c6e5 2925 OPENSSL_clear_free(buf, bufsize);
26a7d938 2926 return ret;
0f113f3e 2927}
d02b48c6 2928
6b691a5c 2929static void close_accept_socket(void)
0f113f3e
MC
2930{
2931 BIO_printf(bio_err, "shutdown accept socket\n");
2932 if (accept_socket >= 0) {
8731a4fc 2933 BIO_closesocket(accept_socket);
0f113f3e
MC
2934 }
2935}
d02b48c6 2936
068e3d73
MC
2937static int is_retryable(SSL *con, int i)
2938{
2939 int err = SSL_get_error(con, i);
2940
2941 /* If it's not a fatal error, it must be retryable */
2942 return (err != SSL_ERROR_SSL)
2943 && (err != SSL_ERROR_SYSCALL)
2944 && (err != SSL_ERROR_ZERO_RETURN);
2945}
2946
6b691a5c 2947static int init_ssl_connection(SSL *con)
0f113f3e
MC
2948{
2949 int i;
df2ee0e2 2950 long verify_err;
384f08dc 2951 int retry = 0;
fd4e98ec 2952
c2f9648d 2953 if (dtlslisten || stateless) {
d858c876
RL
2954 BIO_ADDR *client = NULL;
2955
c2f9648d
MC
2956 if (dtlslisten) {
2957 if ((client = BIO_ADDR_new()) == NULL) {
2958 BIO_printf(bio_err, "ERROR - memory\n");
2959 return 0;
2960 }
2961 i = DTLSv1_listen(con, client);
2962 } else {
2963 i = SSL_stateless(con);
d858c876 2964 }
fd4e98ec
MC
2965 if (i > 0) {
2966 BIO *wbio;
3a796184 2967 int fd = -1;
fd4e98ec 2968
c2f9648d
MC
2969 if (dtlslisten) {
2970 wbio = SSL_get_wbio(con);
2971 if (wbio) {
2972 BIO_get_fd(wbio, &fd);
2973 }
fd4e98ec 2974
c2f9648d
MC
2975 if (!wbio || BIO_connect(fd, client, 0) == 0) {
2976 BIO_printf(bio_err, "ERROR - unable to connect\n");
2977 BIO_ADDR_free(client);
2978 return 0;
2979 }
b92678f4
MH
2980
2981 (void)BIO_ctrl_set_connected(wbio, client);
d858c876 2982 BIO_ADDR_free(client);
c2f9648d
MC
2983 dtlslisten = 0;
2984 } else {
2985 stateless = 0;
fd4e98ec 2986 }
fd4e98ec 2987 i = SSL_accept(con);
a3768e0c
MC
2988 } else {
2989 BIO_ADDR_free(client);
fd4e98ec 2990 }
c2f9648d
MC
2991 } else {
2992 do {
2993 i = SSL_accept(con);
d02b48c6 2994
c2f9648d
MC
2995 if (i <= 0)
2996 retry = is_retryable(con, i);
3323314f 2997#ifdef CERT_CB_TEST_RETRY
c2f9648d
MC
2998 {
2999 while (i <= 0
3000 && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP
3001 && SSL_get_state(con) == TLS_ST_SR_CLNT_HELLO) {
3002 BIO_printf(bio_err,
3003 "LOOKUP from certificate callback during accept\n");
3004 i = SSL_accept(con);
3005 if (i <= 0)
3006 retry = is_retryable(con, i);
3007 }
3008 }
3009#endif
3010
3011#ifndef OPENSSL_NO_SRP
d6073e27 3012 while (i <= 0
c2f9648d
MC
3013 && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) {
3014 BIO_printf(bio_s_out, "LOOKUP during accept %s\n",
3015 srp_callback_parm.login);
6d2a1eff
MC
3016
3017 lookup_srp_user(&srp_callback_parm, bio_s_out);
3018
7e25dd6d 3019 i = SSL_accept(con);
384f08dc 3020 if (i <= 0)
068e3d73 3021 retry = is_retryable(con, i);
7e25dd6d 3022 }
3323314f 3023#endif
c2f9648d
MC
3024 } while (i < 0 && SSL_waiting_for_async(con));
3025 }
0f113f3e
MC
3026
3027 if (i <= 0) {
c2f9648d
MC
3028 if (((dtlslisten || stateless) && i == 0)
3029 || (!dtlslisten && !stateless && retry)) {
0f113f3e 3030 BIO_printf(bio_s_out, "DELAY\n");
208fb891 3031 return 1;
0f113f3e
MC
3032 }
3033
3034 BIO_printf(bio_err, "ERROR\n");
7e1b7485 3035
df2ee0e2
BL
3036 verify_err = SSL_get_verify_result(con);
3037 if (verify_err != X509_V_OK) {
0f113f3e 3038 BIO_printf(bio_err, "verify error:%s\n",
df2ee0e2 3039 X509_verify_cert_error_string(verify_err));
0f113f3e
MC
3040 }
3041 /* Always print any error messages */
3042 ERR_print_errors(bio_err);
26a7d938 3043 return 0;
0f113f3e
MC
3044 }
3045
ade1e888
MC
3046 print_connection_info(con);
3047 return 1;
3048}
3049
3050static void print_connection_info(SSL *con)
3051{
3052 const char *str;
3053 X509 *peer;
3054 char buf[BUFSIZ];
3055#if !defined(OPENSSL_NO_NEXTPROTONEG)
3056 const unsigned char *next_proto_neg;
3057 unsigned next_proto_neg_len;
3058#endif
3059 unsigned char *exportedkeymat;
3060 int i;
3061
0f113f3e 3062 if (s_brief)
ecf3a1fb 3063 print_ssl_summary(con);
0f113f3e
MC
3064
3065 PEM_write_bio_SSL_SESSION(bio_s_out, SSL_get_session(con));
3066
8c2bfd25 3067 peer = SSL_get0_peer_certificate(con);
0f113f3e
MC
3068 if (peer != NULL) {
3069 BIO_printf(bio_s_out, "Client certificate\n");
3070 PEM_write_bio_X509(bio_s_out, peer);
b5c4209b 3071 dump_cert_text(bio_s_out, peer);
049f3655 3072 peer = NULL;
0f113f3e 3073 }
3c95ef22
TS
3074 /* Only display RPK information if configured */
3075 if (SSL_get_negotiated_server_cert_type(con) == TLSEXT_cert_type_rpk)
3076 BIO_printf(bio_s_out, "Server-to-client raw public key negotiated\n");
3077 if (SSL_get_negotiated_client_cert_type(con) == TLSEXT_cert_type_rpk)
3078 BIO_printf(bio_s_out, "Client-to-server raw public key negotiated\n");
3079 if (enable_client_rpk) {
3080 EVP_PKEY *client_rpk = SSL_get0_peer_rpk(con);
3081
3082 if (client_rpk != NULL) {
3083 BIO_printf(bio_s_out, "Client raw public key\n");
3084 EVP_PKEY_print_public(bio_s_out, client_rpk, 2, NULL);
3085 }
3086 }
0f113f3e 3087
cbe29648 3088 if (SSL_get_shared_ciphers(con, buf, sizeof(buf)) != NULL)
0f113f3e
MC
3089 BIO_printf(bio_s_out, "Shared ciphers:%s\n", buf);
3090 str = SSL_CIPHER_get_name(SSL_get_current_cipher(con));
3091 ssl_print_sigalgs(bio_s_out, con);
14536c8c 3092#ifndef OPENSSL_NO_EC
0f113f3e 3093 ssl_print_point_formats(bio_s_out, con);
de4d764e 3094 ssl_print_groups(bio_s_out, con, 0);
14536c8c 3095#endif
5969a2dd 3096 print_ca_names(bio_s_out, con);
0f113f3e 3097 BIO_printf(bio_s_out, "CIPHER is %s\n", (str != NULL) ? str : "(NONE)");
e7f8ff43 3098
e481f9b9 3099#if !defined(OPENSSL_NO_NEXTPROTONEG)
0f113f3e
MC
3100 SSL_get0_next_proto_negotiated(con, &next_proto_neg, &next_proto_neg_len);
3101 if (next_proto_neg) {
3102 BIO_printf(bio_s_out, "NEXTPROTO is ");
3103 BIO_write(bio_s_out, next_proto_neg, next_proto_neg_len);
3104 BIO_printf(bio_s_out, "\n");
3105 }
ee2ffc27 3106#endif
e783bae2 3107#ifndef OPENSSL_NO_SRTP
0f113f3e
MC
3108 {
3109 SRTP_PROTECTION_PROFILE *srtp_profile
3110 = SSL_get_selected_srtp_profile(con);
3111
3112 if (srtp_profile)
3113 BIO_printf(bio_s_out, "SRTP Extension negotiated, profile=%s\n",
3114 srtp_profile->name);
3115 }
3116#endif
b577fd0b 3117 if (SSL_session_reused(con))
0f113f3e 3118 BIO_printf(bio_s_out, "Reused session-id\n");
af5e63e1
FG
3119
3120 ssl_print_secure_renegotiation_notes(bio_s_out, con);
3121
db0f35dd
TS
3122 if ((SSL_get_options(con) & SSL_OP_NO_RENEGOTIATION))
3123 BIO_printf(bio_s_out, "Renegotiation is DISABLED\n");
3124
0f113f3e
MC
3125 if (keymatexportlabel != NULL) {
3126 BIO_printf(bio_s_out, "Keying material exporter:\n");
3127 BIO_printf(bio_s_out, " Label: '%s'\n", keymatexportlabel);
3128 BIO_printf(bio_s_out, " Length: %i bytes\n", keymatexportlen);
68dc6824 3129 exportedkeymat = app_malloc(keymatexportlen, "export key");
40649e36 3130 if (SSL_export_keying_material(con, exportedkeymat,
68dc6824
RS
3131 keymatexportlen,
3132 keymatexportlabel,
3133 strlen(keymatexportlabel),
40649e36 3134 NULL, 0, 0) <= 0) {
68dc6824
RS
3135 BIO_printf(bio_s_out, " Error\n");
3136 } else {
3137 BIO_printf(bio_s_out, " Keying material: ");
3138 for (i = 0; i < keymatexportlen; i++)
3139 BIO_printf(bio_s_out, "%02X", exportedkeymat[i]);
3140 BIO_printf(bio_s_out, "\n");
0f113f3e 3141 }
68dc6824 3142 OPENSSL_free(exportedkeymat);
0f113f3e 3143 }
d6c3c189
BP
3144#ifndef OPENSSL_NO_KTLS
3145 if (BIO_get_ktls_send(SSL_get_wbio(con)))
3146 BIO_printf(bio_err, "Using Kernel TLS for sending\n");
005080aa
BP
3147 if (BIO_get_ktls_recv(SSL_get_rbio(con)))
3148 BIO_printf(bio_err, "Using Kernel TLS for receiving\n");
d6c3c189 3149#endif
0f113f3e 3150
d6073e27 3151 (void)BIO_flush(bio_s_out);
0f113f3e 3152}
d02b48c6 3153
72d0bc84 3154static int www_body(int s, int stype, int prot, unsigned char *context)
0f113f3e 3155{
2ff286c2 3156 char *buf = NULL, *p;
0f113f3e
MC
3157 int ret = 1;
3158 int i, j, k, dot;
3159 SSL *con;
3160 const SSL_CIPHER *c;
f37dea41 3161 BIO *io, *ssl_bio, *sbio, *edio;
7e1b7485
RS
3162#ifdef RENEG
3163 int total_bytes = 0;
3164#endif
075c8795 3165 int width;
aea68b0d
HL
3166#ifndef OPENSSL_NO_KTLS
3167 int use_sendfile_for_req = use_sendfile;
3168#endif
075c8795 3169 fd_set readfds;
c3be39f2 3170 const char *opmode;
ba0b60c6
JJ
3171#ifdef CHARSET_EBCDIC
3172 BIO *filter;
3173#endif
075c8795
MC
3174
3175 /* Set width for a select call if needed */
3176 width = s + 1;
d02b48c6 3177
148b592d
HK
3178 /* as we use BIO_gets(), and it always null terminates data, we need
3179 * to allocate 1 byte longer buffer to fit the full 2^14 byte record */
3180 p = buf = app_malloc(bufsize + 1, "server www buffer");
0f113f3e
MC
3181 io = BIO_new(BIO_f_buffer());
3182 ssl_bio = BIO_new(BIO_f_ssl());
f37dea41
DU
3183 edio = BIO_new(BIO_s_mem());
3184 if ((io == NULL) || (ssl_bio == NULL) || (edio == NULL))
0f113f3e 3185 goto err;
d02b48c6 3186
0f113f3e 3187 if (s_nbio) {
ba810815 3188 if (!BIO_socket_nbio(s, 1))
0f113f3e 3189 ERR_print_errors(bio_err);
ba810815
RS
3190 else if (!s_quiet)
3191 BIO_printf(bio_err, "Turned on non blocking io\n");
0f113f3e 3192 }
d02b48c6 3193
0f113f3e 3194 /* lets make the output buffer a reasonable size */
25d02f33 3195 if (BIO_set_write_buffer_size(io, bufsize) <= 0)
0f113f3e 3196 goto err;
d02b48c6 3197
0f113f3e
MC
3198 if ((con = SSL_new(ctx)) == NULL)
3199 goto err;
e481f9b9 3200
0f113f3e
MC
3201 if (s_tlsextdebug) {
3202 SSL_set_tlsext_debug_callback(con, tlsext_cb);
3203 SSL_set_tlsext_debug_arg(con, bio_s_out);
3204 }
e481f9b9 3205
2234212c 3206 if (context != NULL
d6073e27 3207 && !SSL_set_session_id_context(con, context,
c62cd07d 3208 (unsigned int)strlen((char *)context))) {
67afcfd3 3209 SSL_free(con);
ac59d705 3210 goto err;
67afcfd3 3211 }
0f113f3e
MC
3212
3213 sbio = BIO_new_socket(s, BIO_NOCLOSE);
ba0b60c6
JJ
3214 if (sbio == NULL) {
3215 SSL_free(con);
3216 goto err;
3217 }
3218
0f113f3e
MC
3219 if (s_nbio_test) {
3220 BIO *test;
3221
3222 test = BIO_new(BIO_f_nbio_test());
ba0b60c6
JJ
3223 if (test == NULL) {
3224 SSL_free(con);
3225 BIO_free(sbio);
3226 goto err;
3227 }
3228
0f113f3e
MC
3229 sbio = BIO_push(test, sbio);
3230 }
3231 SSL_set_bio(con, sbio, sbio);
3232 SSL_set_accept_state(con);
3233
67afcfd3 3234 /* No need to free |con| after this. Done by BIO_free(ssl_bio) */
0f113f3e
MC
3235 BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
3236 BIO_push(io, ssl_bio);
1f3f8a3d 3237 ssl_bio = NULL;
a53955d8 3238#ifdef CHARSET_EBCDIC
ba0b60c6
JJ
3239 filter = BIO_new(BIO_f_ebcdic_filter());
3240 if (filter == NULL)
3241 goto err;
3242
3243 io = BIO_push(filter, io);
f37dea41
DU
3244
3245 filter = BIO_new(BIO_f_ebcdic_filter());
3246 if (filter == NULL)
3247 goto err;
3248
3249 edio = BIO_push(filter, edio);
a53955d8 3250#endif
d02b48c6 3251
0f113f3e 3252 if (s_debug) {
0800318a 3253 BIO_set_callback_ex(SSL_get_rbio(con), bio_dump_callback);
0f113f3e
MC
3254 BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
3255 }
3256 if (s_msg) {
93ab9e42 3257#ifndef OPENSSL_NO_SSL_TRACE
0f113f3e
MC
3258 if (s_msg == 2)
3259 SSL_set_msg_callback(con, SSL_trace);
3260 else
3261#endif
3262 SSL_set_msg_callback(con, msg_cb);
3263 SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
3264 }
3265
f37dea41
DU
3266 if (early_data) {
3267 int edret = SSL_READ_EARLY_DATA_ERROR;
3268 size_t readbytes;
3269
3270 while (edret != SSL_READ_EARLY_DATA_FINISH) {
3271 for (;;) {
3272 edret = SSL_read_early_data(con, buf, bufsize, &readbytes);
3273 if (edret != SSL_READ_EARLY_DATA_ERROR)
3274 break;
3275
3276 switch (SSL_get_error(con, 0)) {
3277 case SSL_ERROR_WANT_WRITE:
3278 case SSL_ERROR_WANT_ASYNC:
3279 case SSL_ERROR_WANT_READ:
3280 /* Just keep trying - busy waiting */
3281 continue;
3282 default:
3283 BIO_printf(bio_err, "Error reading early data\n");
3284 ERR_print_errors(bio_err);
3285 goto err;
3286 }
3287 }
3288 if (readbytes > 0)
3289 BIO_write(edio, buf, (int)readbytes);
3290 }
3291 }
3292
0f113f3e 3293 for (;;) {
f37dea41 3294 i = BIO_gets(!BIO_eof(edio) ? edio : io, buf, bufsize + 1);
0f113f3e 3295 if (i < 0) { /* error */
4cfa6204 3296 if (!BIO_should_retry(io) && !SSL_waiting_for_async(con)) {
0f113f3e
MC
3297 if (!s_quiet)
3298 ERR_print_errors(bio_err);
3299 goto err;
3300 } else {
3301 BIO_printf(bio_s_out, "read R BLOCK\n");
4e7e6230
DSH
3302#ifndef OPENSSL_NO_SRP
3303 if (BIO_should_io_special(io)
3304 && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
3305 BIO_printf(bio_s_out, "LOOKUP renego during read\n");
6d2a1eff
MC
3306
3307 lookup_srp_user(&srp_callback_parm, bio_s_out);
3308
4e7e6230
DSH
3309 continue;
3310 }
3311#endif
5139dec2 3312 OSSL_sleep(1000);
0f113f3e
MC
3313 continue;
3314 }
3315 } else if (i == 0) { /* end of input */
3316 ret = 1;
3317 goto end;
3318 }
d02b48c6 3319
0f113f3e 3320 /* else we have data */
2ff286c2
DDO
3321 if ((www == 1 && HAS_PREFIX(buf, "GET "))
3322 || (www == 2 && HAS_PREFIX(buf, "GET /stats "))) {
049f3655 3323 X509 *peer = NULL;
0f113f3e
MC
3324 STACK_OF(SSL_CIPHER) *sk;
3325 static const char *space = " ";
3326
2ff286c2
DDO
3327 if (www == 1 && HAS_PREFIX(buf, "GET /reneg")) {
3328 if (HAS_PREFIX(buf, "GET /renegcert"))
0f113f3e
MC
3329 SSL_set_verify(con,
3330 SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
3331 NULL);
3332 i = SSL_renegotiate(con);
3333 BIO_printf(bio_s_out, "SSL_renegotiate -> %d\n", i);
075c8795 3334 /* Send the HelloRequest */
0f113f3e
MC
3335 i = SSL_do_handshake(con);
3336 if (i <= 0) {
3337 BIO_printf(bio_s_out, "SSL_do_handshake() Retval %d\n",
3338 SSL_get_error(con, i));
3339 ERR_print_errors(bio_err);
3340 goto err;
3341 }
075c8795
MC
3342 /* Wait for a ClientHello to come back */
3343 FD_ZERO(&readfds);
3344 openssl_fdset(s, &readfds);
3345 i = select(width, (void *)&readfds, NULL, NULL, NULL);
3346 if (i <= 0 || !FD_ISSET(s, &readfds)) {
d6073e27
F
3347 BIO_printf(bio_s_out,
3348 "Error waiting for client response\n");
0f113f3e
MC
3349 ERR_print_errors(bio_err);
3350 goto err;
3351 }
075c8795 3352 /*
049f3655 3353 * We're not actually expecting any data here and we ignore
075c8795
MC
3354 * any that is sent. This is just to force the handshake that
3355 * we're expecting to come from the client. If they haven't
3356 * sent one there's not much we can do.
3357 */
148b592d 3358 BIO_gets(io, buf, bufsize + 1);
0f113f3e
MC
3359 }
3360
3361 BIO_puts(io,
3362 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
3363 BIO_puts(io, "<HTML><BODY BGCOLOR=\"#ffffff\">\n");
3364 BIO_puts(io, "<pre>\n");
049f3655 3365 /* BIO_puts(io, OpenSSL_version(OPENSSL_VERSION)); */
0f113f3e
MC
3366 BIO_puts(io, "\n");
3367 for (i = 0; i < local_argc; i++) {
f92beb98 3368 const char *myp;
2ff286c2 3369
f92beb98
RS
3370 for (myp = local_argv[i]; *myp; myp++)
3371 switch (*myp) {
3372 case '<':
3373 BIO_puts(io, "&lt;");
3374 break;
3375 case '>':
3376 BIO_puts(io, "&gt;");
3377 break;
3378 case '&':
3379 BIO_puts(io, "&amp;");
3380 break;
3381 default:
3382 BIO_write(io, myp, 1);
3383 break;
3384 }
0f113f3e
MC
3385 BIO_write(io, " ", 1);
3386 }
3387 BIO_puts(io, "\n");
3388
af5e63e1 3389 ssl_print_secure_renegotiation_notes(io, con);
0f113f3e
MC
3390
3391 /*
3392 * The following is evil and should not really be done
3393 */
3394 BIO_printf(io, "Ciphers supported in s_server binary\n");
3395 sk = SSL_get_ciphers(con);
3396 j = sk_SSL_CIPHER_num(sk);
3397 for (i = 0; i < j; i++) {
3398 c = sk_SSL_CIPHER_value(sk, i);
7e1b7485 3399 BIO_printf(io, "%-11s:%-25s ",
0f113f3e
MC
3400 SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
3401 if ((((i + 1) % 2) == 0) && (i + 1 != j))
3402 BIO_puts(io, "\n");
3403 }
3404 BIO_puts(io, "\n");
3405 p = SSL_get_shared_ciphers(con, buf, bufsize);
3406 if (p != NULL) {
3407 BIO_printf(io,
3408 "---\nCiphers common between both SSL end points:\n");
3409 j = i = 0;
3410 while (*p) {
3411 if (*p == ':') {
3412 BIO_write(io, space, 26 - j);
3413 i++;
3414 j = 0;
3415 BIO_write(io, ((i % 3) ? " " : "\n"), 1);
3416 } else {
3417 BIO_write(io, p, 1);
3418 j++;
3419 }
3420 p++;
3421 }
3422 BIO_puts(io, "\n");
3423 }
3424 ssl_print_sigalgs(io, con);
3425#ifndef OPENSSL_NO_EC
de4d764e 3426 ssl_print_groups(io, con, 0);
0f113f3e 3427#endif
5969a2dd 3428 print_ca_names(io, con);
b577fd0b 3429 BIO_printf(io, (SSL_session_reused(con)
0f113f3e
MC
3430 ? "---\nReused, " : "---\nNew, "));
3431 c = SSL_get_current_cipher(con);
3432 BIO_printf(io, "%s, Cipher is %s\n",
3433 SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
3434 SSL_SESSION_print(io, SSL_get_session(con));
3435 BIO_printf(io, "---\n");
3436 print_stats(io, SSL_get_SSL_CTX(con));
3437 BIO_printf(io, "---\n");
8c2bfd25 3438 peer = SSL_get0_peer_certificate(con);
0f113f3e
MC
3439 if (peer != NULL) {
3440 BIO_printf(io, "Client certificate\n");
3441 X509_print(io, peer);
3442 PEM_write_bio_X509(io, peer);
049f3655 3443 peer = NULL;
1a9f5cf0 3444 } else {
0f113f3e 3445 BIO_puts(io, "no client certificate available\n");
1a9f5cf0
XL
3446 }
3447 BIO_puts(io, "</pre></BODY></HTML>\r\n\r\n");
0f113f3e 3448 break;
2e3b8292 3449 } else if ((www == 2 || www == 3) && CHECK_AND_SKIP_PREFIX(p, "GET /")) {
0f113f3e 3450 BIO *file;
2ff286c2 3451 char *e;
0f113f3e
MC
3452 static const char *text =
3453 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
3454
0f113f3e
MC
3455 dot = 1;
3456 for (e = p; *e != '\0'; e++) {
3457 if (e[0] == ' ')
3458 break;
3459
0a4d6c67
MC
3460 if (e[0] == ':') {
3461 /* Windows drive. We treat this the same way as ".." */
3462 dot = -1;
3463 break;
3464 }
3465
0f113f3e
MC
3466 switch (dot) {
3467 case 1:
3468 dot = (e[0] == '.') ? 2 : 0;
3469 break;
3470 case 2:
3471 dot = (e[0] == '.') ? 3 : 0;
3472 break;
3473 case 3:
0a4d6c67 3474 dot = (e[0] == '/' || e[0] == '\\') ? -1 : 0;
0f113f3e
MC
3475 break;
3476 }
3477 if (dot == 0)
0a4d6c67 3478 dot = (e[0] == '/' || e[0] == '\\') ? 1 : 0;
0f113f3e
MC
3479 }
3480 dot = (dot == 3) || (dot == -1); /* filename contains ".."
3481 * component */
3482
3483 if (*e == '\0') {
3484 BIO_puts(io, text);
3485 BIO_printf(io, "'%s' is an invalid file name\r\n", p);
3486 break;
3487 }
3488 *e = '\0';
3489
3490 if (dot) {
3491 BIO_puts(io, text);
0a4d6c67 3492 BIO_printf(io, "'%s' contains '..' or ':'\r\n", p);
0f113f3e
MC
3493 break;
3494 }
3495
0a4d6c67 3496 if (*p == '/' || *p == '\\') {
0f113f3e
MC
3497 BIO_puts(io, text);
3498 BIO_printf(io, "'%s' is an invalid path\r\n", p);
3499 break;
3500 }
d02b48c6 3501
0f113f3e
MC
3502 /* if a directory, do the index thang */
3503 if (app_isdir(p) > 0) {
0f113f3e
MC
3504 BIO_puts(io, text);
3505 BIO_printf(io, "'%s' is a directory\r\n", p);
3506 break;
0f113f3e
MC
3507 }
3508
c3be39f2
LZ
3509 opmode = (http_server_binmode == 1) ? "rb" : "r";
3510 if ((file = BIO_new_file(p, opmode)) == NULL) {
0f113f3e 3511 BIO_puts(io, text);
c3be39f2 3512 BIO_printf(io, "Error opening '%s' mode='%s'\r\n", p, opmode);
0f113f3e
MC
3513 ERR_print_errors(io);
3514 break;
3515 }
3516
3517 if (!s_quiet)
3518 BIO_printf(bio_err, "FILE:%s\n", p);
3519
3520 if (www == 2) {
c62cd07d 3521 i = (int)strlen(p);
0f113f3e
MC
3522 if (((i > 5) && (strcmp(&(p[i - 5]), ".html") == 0)) ||
3523 ((i > 4) && (strcmp(&(p[i - 4]), ".php") == 0)) ||
3524 ((i > 4) && (strcmp(&(p[i - 4]), ".htm") == 0)))
3525 BIO_puts(io,
3526 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
3527 else
3528 BIO_puts(io,
3529 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
3530 }
3531 /* send the file */
cd81ac7b 3532#ifndef OPENSSL_NO_KTLS
aea68b0d
HL
3533 if (use_sendfile_for_req && !BIO_get_ktls_send(SSL_get_wbio(con))) {
3534 BIO_printf(bio_err, "Warning: sendfile requested but KTLS is not available\n");
3535 use_sendfile_for_req = 0;
3536 }
3537 if (use_sendfile_for_req) {
cd81ac7b
TZ
3538 FILE *fp = NULL;
3539 int fd;
3540 struct stat st;
3541 off_t offset = 0;
3542 size_t filesize;
3543
3544 BIO_get_fp(file, &fp);
3545 fd = fileno(fp);
3546 if (fstat(fd, &st) < 0) {
3547 BIO_printf(io, "Error fstat '%s'\r\n", p);
3548 ERR_print_errors(io);
3549 goto write_error;
3550 }
d02b48c6 3551
cd81ac7b
TZ
3552 filesize = st.st_size;
3553 if (((int)BIO_flush(io)) < 0)
3554 goto write_error;
3555
3556 for (;;) {
3557 i = SSL_sendfile(con, fd, offset, filesize, 0);
3558 if (i < 0) {
3559 BIO_printf(io, "Error SSL_sendfile '%s'\r\n", p);
3560 ERR_print_errors(io);
3561 break;
3562 } else {
3563 offset += i;
3564 filesize -= i;
3565 }
3566
3567 if (filesize <= 0) {
3568 if (!s_quiet)
3569 BIO_printf(bio_err, "KTLS SENDFILE '%s' OK\n", p);
3570
3571 break;
3572 }
0f113f3e 3573 }
cd81ac7b 3574 } else
0f113f3e 3575#endif
cd81ac7b
TZ
3576 {
3577 for (;;) {
3578 i = BIO_read(file, buf, bufsize);
3579 if (i <= 0)
3580 break;
0f113f3e 3581
58964a49 3582#ifdef RENEG
cd81ac7b
TZ
3583 total_bytes += i;
3584 BIO_printf(bio_err, "%d\n", i);
3585 if (total_bytes > 3 * 1024) {
3586 total_bytes = 0;
3587 BIO_printf(bio_err, "RENEGOTIATE\n");
54463e4f 3588 SSL_renegotiate(con);
0f113f3e
MC
3589 }
3590#endif
cd81ac7b
TZ
3591
3592 for (j = 0; j < i;) {
3593#ifdef RENEG
3594 static count = 0;
3595 if (++count == 13)
3596 SSL_renegotiate(con);
3597#endif
3598 k = BIO_write(io, &(buf[j]), i - j);
3599 if (k <= 0) {
3600 if (!BIO_should_retry(io)
3601 && !SSL_waiting_for_async(con)) {
3602 goto write_error;
3603 } else {
3604 BIO_printf(bio_s_out, "rwrite W BLOCK\n");
3605 }
3606 } else {
3607 j += k;
0f113f3e 3608 }
0f113f3e
MC
3609 }
3610 }
3611 }
3612 write_error:
3613 BIO_free(file);
3614 break;
3615 }
3616 }
3617
3618 for (;;) {
3619 i = (int)BIO_flush(io);
3620 if (i <= 0) {
3621 if (!BIO_should_retry(io))
3622 break;
3623 } else
3624 break;
3625 }
3626 end:
a024ab98 3627 /* make sure we reuse sessions */
edbb56ee 3628 do_ssl_shutdown(con);
d02b48c6 3629
0f113f3e 3630 err:
b548a1f1 3631 OPENSSL_free(buf);
1f3f8a3d 3632 BIO_free(ssl_bio);
ca3a82c3 3633 BIO_free_all(io);
f37dea41 3634 BIO_free_all(edio);
26a7d938 3635 return ret;
0f113f3e 3636}
d02b48c6 3637
72d0bc84 3638static int rev_body(int s, int stype, int prot, unsigned char *context)
0f113f3e
MC
3639{
3640 char *buf = NULL;
3641 int i;
3642 int ret = 1;
3643 SSL *con;
3644 BIO *io, *ssl_bio, *sbio;
ba0b60c6
JJ
3645#ifdef CHARSET_EBCDIC
3646 BIO *filter;
3647#endif
4f3df8be 3648
148b592d
HK
3649 /* as we use BIO_gets(), and it always null terminates data, we need
3650 * to allocate 1 byte longer buffer to fit the full 2^14 byte record */
3651 buf = app_malloc(bufsize + 1, "server rev buffer");
0f113f3e
MC
3652 io = BIO_new(BIO_f_buffer());
3653 ssl_bio = BIO_new(BIO_f_ssl());
3654 if ((io == NULL) || (ssl_bio == NULL))
3655 goto err;
4f3df8be 3656
0f113f3e 3657 /* lets make the output buffer a reasonable size */
25d02f33 3658 if (BIO_set_write_buffer_size(io, bufsize) <= 0)
0f113f3e 3659 goto err;
4f3df8be 3660
0f113f3e
MC
3661 if ((con = SSL_new(ctx)) == NULL)
3662 goto err;
e481f9b9 3663
0f113f3e
MC
3664 if (s_tlsextdebug) {
3665 SSL_set_tlsext_debug_callback(con, tlsext_cb);
3666 SSL_set_tlsext_debug_arg(con, bio_s_out);
3667 }
2234212c 3668 if (context != NULL
d6073e27 3669 && !SSL_set_session_id_context(con, context,
c62cd07d 3670 (unsigned int)strlen((char *)context))) {
67afcfd3 3671 SSL_free(con);
ac59d705
MC
3672 ERR_print_errors(bio_err);
3673 goto err;
3674 }
0f113f3e
MC
3675
3676 sbio = BIO_new_socket(s, BIO_NOCLOSE);
ba0b60c6
JJ
3677 if (sbio == NULL) {
3678 SSL_free(con);
3679 ERR_print_errors(bio_err);
3680 goto err;
3681 }
3682
0f113f3e
MC
3683 SSL_set_bio(con, sbio, sbio);
3684 SSL_set_accept_state(con);
3685
67afcfd3 3686 /* No need to free |con| after this. Done by BIO_free(ssl_bio) */
0f113f3e
MC
3687 BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
3688 BIO_push(io, ssl_bio);
1f3f8a3d 3689 ssl_bio = NULL;
4f3df8be 3690#ifdef CHARSET_EBCDIC
ba0b60c6
JJ
3691 filter = BIO_new(BIO_f_ebcdic_filter());
3692 if (filter == NULL)
3693 goto err;
3694
3695 io = BIO_push(filter, io);
4f3df8be
DSH
3696#endif
3697
0f113f3e 3698 if (s_debug) {
0800318a 3699 BIO_set_callback_ex(SSL_get_rbio(con), bio_dump_callback);
0f113f3e
MC
3700 BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
3701 }
3702 if (s_msg) {
4f3df8be 3703#ifndef OPENSSL_NO_SSL_TRACE
0f113f3e
MC
3704 if (s_msg == 2)
3705 SSL_set_msg_callback(con, SSL_trace);
3706 else
3707#endif
3708 SSL_set_msg_callback(con, msg_cb);
3709 SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
3710 }
3711
3712 for (;;) {
3713 i = BIO_do_handshake(io);
3714 if (i > 0)
3715 break;
3716 if (!BIO_should_retry(io)) {
3717 BIO_puts(bio_err, "CONNECTION FAILURE\n");
3718 ERR_print_errors(bio_err);
3719 goto end;
3720 }
4e7e6230
DSH
3721#ifndef OPENSSL_NO_SRP
3722 if (BIO_should_io_special(io)
3723 && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
3724 BIO_printf(bio_s_out, "LOOKUP renego during accept\n");
6d2a1eff
MC
3725
3726 lookup_srp_user(&srp_callback_parm, bio_s_out);
3727
4e7e6230
DSH
3728 continue;
3729 }
3730#endif
0f113f3e
MC
3731 }
3732 BIO_printf(bio_err, "CONNECTION ESTABLISHED\n");
ecf3a1fb 3733 print_ssl_summary(con);
0f113f3e
MC
3734
3735 for (;;) {
148b592d 3736 i = BIO_gets(io, buf, bufsize + 1);
0f113f3e
MC
3737 if (i < 0) { /* error */
3738 if (!BIO_should_retry(io)) {
3739 if (!s_quiet)
3740 ERR_print_errors(bio_err);
3741 goto err;
3742 } else {
3743 BIO_printf(bio_s_out, "read R BLOCK\n");
4e7e6230
DSH
3744#ifndef OPENSSL_NO_SRP
3745 if (BIO_should_io_special(io)
3746 && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
3747 BIO_printf(bio_s_out, "LOOKUP renego during read\n");
6d2a1eff
MC
3748
3749 lookup_srp_user(&srp_callback_parm, bio_s_out);
3750
4e7e6230
DSH
3751 continue;
3752 }
3753#endif
5139dec2 3754 OSSL_sleep(1000);
0f113f3e
MC
3755 continue;
3756 }
3757 } else if (i == 0) { /* end of input */
3758 ret = 1;
3759 BIO_printf(bio_err, "CONNECTION CLOSED\n");
3760 goto end;
3761 } else {
3762 char *p = buf + i - 1;
3763 while (i && (*p == '\n' || *p == '\r')) {
3764 p--;
3765 i--;
3766 }
2ff286c2 3767 if (!s_ign_eof && i == 5 && HAS_PREFIX(buf, "CLOSE")) {
0f113f3e
MC
3768 ret = 1;
3769 BIO_printf(bio_err, "CONNECTION CLOSED\n");
3770 goto end;
3771 }
3772 BUF_reverse((unsigned char *)buf, NULL, i);
3773 buf[i] = '\n';
3774 BIO_write(io, buf, i + 1);
3775 for (;;) {
3776 i = BIO_flush(io);
3777 if (i > 0)
3778 break;
3779 if (!BIO_should_retry(io))
3780 goto end;
3781 }
3782 }
3783 }
3784 end:
a024ab98 3785 /* make sure we reuse sessions */
edbb56ee 3786 do_ssl_shutdown(con);
0f113f3e
MC
3787
3788 err:
3789
b548a1f1 3790 OPENSSL_free(buf);
1f3f8a3d 3791 BIO_free(ssl_bio);
ca3a82c3 3792 BIO_free_all(io);
26a7d938 3793 return ret;
0f113f3e 3794}
4f3df8be 3795
1aa0d947 3796#define MAX_SESSION_ID_ATTEMPTS 10
ae3947de 3797static int generate_session_id(SSL *ssl, unsigned char *id,
0f113f3e
MC
3798 unsigned int *id_len)
3799{
3800 unsigned int count = 0;
c62cd07d 3801 unsigned int session_id_prefix_len = (unsigned int)strlen(session_id_prefix);
29f178bd 3802
0f113f3e 3803 do {
266483d2
MC
3804 if (RAND_bytes(id, *id_len) <= 0)
3805 return 0;
0f113f3e
MC
3806 /*
3807 * Prefix the session_id with the required prefix. NB: If our prefix
3808 * is too long, clip it - but there will be worse effects anyway, eg.
3809 * the server could only possibly create 1 session ID (ie. the
3810 * prefix!) so all future session negotiations will fail due to
3811 * conflicts.
3812 */
3813 memcpy(id, session_id_prefix,
4bac25e1 3814 (session_id_prefix_len < *id_len) ?
3815 session_id_prefix_len : *id_len);
0f113f3e
MC
3816 }
3817 while (SSL_has_matching_session_id(ssl, id, *id_len) &&
3818 (++count < MAX_SESSION_ID_ATTEMPTS));
3819 if (count >= MAX_SESSION_ID_ATTEMPTS)
3820 return 0;
3821 return 1;
3822}
3823
3824/*
3825 * By default s_server uses an in-memory cache which caches SSL_SESSION
aa97970c 3826 * structures without any serialization. This hides some bugs which only
35b0ea4e
DSH
3827 * become apparent in deployed servers. By implementing a basic external
3828 * session cache some issues can be debugged using s_server.
3829 */
3830
0f113f3e
MC
3831typedef struct simple_ssl_session_st {
3832 unsigned char *id;
3833 unsigned int idlen;
3834 unsigned char *der;
3835 int derlen;
3836 struct simple_ssl_session_st *next;
3837} simple_ssl_session;
35b0ea4e
DSH
3838
3839static simple_ssl_session *first = NULL;
3840
3841static int add_session(SSL *ssl, SSL_SESSION *session)
0f113f3e 3842{
b4faea50 3843 simple_ssl_session *sess = app_malloc(sizeof(*sess), "get session");
0f113f3e 3844 unsigned char *p;
35b0ea4e 3845
0f113f3e
MC
3846 SSL_SESSION_get_id(session, &sess->idlen);
3847 sess->derlen = i2d_SSL_SESSION(session, NULL);
7e1b7485
RS
3848 if (sess->derlen < 0) {
3849 BIO_printf(bio_err, "Error encoding session\n");
a194ee7b 3850 OPENSSL_free(sess);
7e1b7485
RS
3851 return 0;
3852 }
35b0ea4e 3853
7644a9ae 3854 sess->id = OPENSSL_memdup(SSL_SESSION_get_id(session, NULL), sess->idlen);
68dc6824
RS
3855 sess->der = app_malloc(sess->derlen, "get session buffer");
3856 if (!sess->id) {
7e1b7485 3857 BIO_printf(bio_err, "Out of memory adding to external cache\n");
a194ee7b
RS
3858 OPENSSL_free(sess->id);
3859 OPENSSL_free(sess->der);
918bb865
MC
3860 OPENSSL_free(sess);
3861 return 0;
3862 }
0f113f3e 3863 p = sess->der;
7e1b7485
RS
3864
3865 /* Assume it still works. */
3866 if (i2d_SSL_SESSION(session, &p) != sess->derlen) {
ce6766de 3867 BIO_printf(bio_err, "Unexpected session encoding length\n");
a194ee7b
RS
3868 OPENSSL_free(sess->id);
3869 OPENSSL_free(sess->der);
3870 OPENSSL_free(sess);
ac59d705
MC
3871 return 0;
3872 }
35b0ea4e 3873
0f113f3e
MC
3874 sess->next = first;
3875 first = sess;
3876 BIO_printf(bio_err, "New session added to external cache\n");
3877 return 0;
3878}
35b0ea4e 3879
b6981744 3880static SSL_SESSION *get_session(SSL *ssl, const unsigned char *id, int idlen,
0f113f3e
MC
3881 int *do_copy)
3882{
3883 simple_ssl_session *sess;
3884 *do_copy = 0;
3885 for (sess = first; sess; sess = sess->next) {
3886 if (idlen == (int)sess->idlen && !memcmp(sess->id, id, idlen)) {
3887 const unsigned char *p = sess->der;
3888 BIO_printf(bio_err, "Lookup session: cache hit\n");
3c95ef22
TS
3889 return d2i_SSL_SESSION_ex(NULL, &p, sess->derlen, app_get0_libctx(),
3890 app_get0_propq());
0f113f3e
MC
3891 }
3892 }
3893 BIO_printf(bio_err, "Lookup session: cache miss\n");
3894 return NULL;
3895}
35b0ea4e
DSH
3896
3897static void del_session(SSL_CTX *sctx, SSL_SESSION *session)
0f113f3e
MC
3898{
3899 simple_ssl_session *sess, *prev = NULL;
3900 const unsigned char *id;
3901 unsigned int idlen;
3902 id = SSL_SESSION_get_id(session, &idlen);
3903 for (sess = first; sess; sess = sess->next) {
3904 if (idlen == sess->idlen && !memcmp(sess->id, id, idlen)) {
3905 if (prev)
3906 prev->next = sess->next;
3907 else
3908 first = sess->next;
3909 OPENSSL_free(sess->id);
3910 OPENSSL_free(sess->der);
3911 OPENSSL_free(sess);
3912 return;
3913 }
3914 prev = sess;
3915 }
3916}
35b0ea4e
DSH
3917
3918static void init_session_cache_ctx(SSL_CTX *sctx)
0f113f3e
MC
3919{
3920 SSL_CTX_set_session_cache_mode(sctx,
3921 SSL_SESS_CACHE_NO_INTERNAL |
3922 SSL_SESS_CACHE_SERVER);
3923 SSL_CTX_sess_set_new_cb(sctx, add_session);
3924 SSL_CTX_sess_set_get_cb(sctx, get_session);
3925 SSL_CTX_sess_set_remove_cb(sctx, del_session);
3926}
35b0ea4e
DSH
3927
3928static void free_sessions(void)
0f113f3e
MC
3929{
3930 simple_ssl_session *sess, *tsess;
3931 for (sess = first; sess;) {
3932 OPENSSL_free(sess->id);
3933 OPENSSL_free(sess->der);
3934 tsess = sess;
3935 sess = sess->next;
3936 OPENSSL_free(tsess);
3937 }
3938 first = NULL;
3939}
f9e55034 3940
d6073e27 3941#endif /* OPENSSL_NO_SOCK */