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