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