]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/s_server.c
DOC: Fix nits found by new check on SYNOPSIS and OPTIONS consistency
[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(s_cert_file, FORMAT_UNDEF, 0, "DH", "DH parameters");
2007
2008 if (dhpkey != NULL) {
2009 BIO_printf(bio_s_out, "Setting temp DH parameters\n");
2010 } else {
2011 BIO_printf(bio_s_out, "Using default temp DH parameters\n");
2012 }
2013 (void)BIO_flush(bio_s_out);
2014
2015 if (dhpkey == NULL) {
2016 SSL_CTX_set_dh_auto(ctx, 1);
2017 } else {
2018 /*
2019 * We need 2 references: one for use by ctx and one for use by
2020 * ctx2
2021 */
2022 if (!EVP_PKEY_up_ref(dhpkey)) {
2023 EVP_PKEY_free(dhpkey);
2024 goto end;
2025 }
2026 if (!SSL_CTX_set0_tmp_dh_pkey(ctx, dhpkey)) {
2027 BIO_puts(bio_err, "Error setting temp DH parameters\n");
2028 ERR_print_errors(bio_err);
2029 /* Free 2 references */
2030 EVP_PKEY_free(dhpkey);
2031 EVP_PKEY_free(dhpkey);
2032 goto end;
2033 }
2034 }
2035
2036 if (ctx2 != NULL) {
2037 if (dhfile != NULL) {
2038 EVP_PKEY *dhpkey2 = load_keyparams(s_cert_file2, FORMAT_UNDEF,
2039 0, "DH",
2040 "DH parameters");
2041
2042 if (dhpkey2 != NULL) {
2043 BIO_printf(bio_s_out, "Setting temp DH parameters\n");
2044 (void)BIO_flush(bio_s_out);
2045
2046 EVP_PKEY_free(dhpkey);
2047 dhpkey = dhpkey2;
2048 }
2049 }
2050 if (dhpkey == NULL) {
2051 SSL_CTX_set_dh_auto(ctx2, 1);
2052 } else if (!SSL_CTX_set0_tmp_dh_pkey(ctx2, dhpkey)) {
2053 BIO_puts(bio_err, "Error setting temp DH parameters\n");
2054 ERR_print_errors(bio_err);
2055 EVP_PKEY_free(dhpkey);
2056 goto end;
2057 }
2058 dhpkey = NULL;
2059 }
2060 EVP_PKEY_free(dhpkey);
2061 }
2062
2063 if (!set_cert_key_stuff(ctx, s_cert, s_key, s_chain, build_chain))
2064 goto end;
2065
2066 if (s_serverinfo_file != NULL
2067 && !SSL_CTX_use_serverinfo_file(ctx, s_serverinfo_file)) {
2068 ERR_print_errors(bio_err);
2069 goto end;
2070 }
2071
2072 if (ctx2 != NULL
2073 && !set_cert_key_stuff(ctx2, s_cert2, s_key2, NULL, build_chain))
2074 goto end;
2075
2076 if (s_dcert != NULL) {
2077 if (!set_cert_key_stuff(ctx, s_dcert, s_dkey, s_dchain, build_chain))
2078 goto end;
2079 }
2080
2081 if (no_resume_ephemeral) {
2082 SSL_CTX_set_not_resumable_session_callback(ctx,
2083 not_resumable_sess_cb);
2084
2085 if (ctx2 != NULL)
2086 SSL_CTX_set_not_resumable_session_callback(ctx2,
2087 not_resumable_sess_cb);
2088 }
2089 #ifndef OPENSSL_NO_PSK
2090 if (psk_key != NULL) {
2091 if (s_debug)
2092 BIO_printf(bio_s_out, "PSK key given, setting server callback\n");
2093 SSL_CTX_set_psk_server_callback(ctx, psk_server_cb);
2094 }
2095
2096 if (psk_identity_hint != NULL) {
2097 if (min_version == TLS1_3_VERSION) {
2098 BIO_printf(bio_s_out, "PSK warning: there is NO identity hint in TLSv1.3\n");
2099 } else {
2100 if (!SSL_CTX_use_psk_identity_hint(ctx, psk_identity_hint)) {
2101 BIO_printf(bio_err, "error setting PSK identity hint to context\n");
2102 ERR_print_errors(bio_err);
2103 goto end;
2104 }
2105 }
2106 }
2107 #endif
2108 if (psksessf != NULL) {
2109 BIO *stmp = BIO_new_file(psksessf, "r");
2110
2111 if (stmp == NULL) {
2112 BIO_printf(bio_err, "Can't open PSK session file %s\n", psksessf);
2113 ERR_print_errors(bio_err);
2114 goto end;
2115 }
2116 psksess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);
2117 BIO_free(stmp);
2118 if (psksess == NULL) {
2119 BIO_printf(bio_err, "Can't read PSK session file %s\n", psksessf);
2120 ERR_print_errors(bio_err);
2121 goto end;
2122 }
2123
2124 }
2125
2126 if (psk_key != NULL || psksess != NULL)
2127 SSL_CTX_set_psk_find_session_callback(ctx, psk_find_session_cb);
2128
2129 SSL_CTX_set_verify(ctx, s_server_verify, verify_callback);
2130 if (!SSL_CTX_set_session_id_context(ctx,
2131 (void *)&s_server_session_id_context,
2132 sizeof(s_server_session_id_context))) {
2133 BIO_printf(bio_err, "error setting session id context\n");
2134 ERR_print_errors(bio_err);
2135 goto end;
2136 }
2137
2138 /* Set DTLS cookie generation and verification callbacks */
2139 SSL_CTX_set_cookie_generate_cb(ctx, generate_cookie_callback);
2140 SSL_CTX_set_cookie_verify_cb(ctx, verify_cookie_callback);
2141
2142 /* Set TLS1.3 cookie generation and verification callbacks */
2143 SSL_CTX_set_stateless_cookie_generate_cb(ctx, generate_stateless_cookie_callback);
2144 SSL_CTX_set_stateless_cookie_verify_cb(ctx, verify_stateless_cookie_callback);
2145
2146 if (ctx2 != NULL) {
2147 SSL_CTX_set_verify(ctx2, s_server_verify, verify_callback);
2148 if (!SSL_CTX_set_session_id_context(ctx2,
2149 (void *)&s_server_session_id_context,
2150 sizeof(s_server_session_id_context))) {
2151 BIO_printf(bio_err, "error setting session id context\n");
2152 ERR_print_errors(bio_err);
2153 goto end;
2154 }
2155 tlsextcbp.biodebug = bio_s_out;
2156 SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb);
2157 SSL_CTX_set_tlsext_servername_arg(ctx2, &tlsextcbp);
2158 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
2159 SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
2160 }
2161
2162 #ifndef OPENSSL_NO_SRP
2163 if (srp_verifier_file != NULL) {
2164 if (!set_up_srp_verifier_file(ctx, &srp_callback_parm, srpuserseed,
2165 srp_verifier_file))
2166 goto end;
2167 } else
2168 #endif
2169 if (CAfile != NULL) {
2170 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(CAfile));
2171
2172 if (ctx2)
2173 SSL_CTX_set_client_CA_list(ctx2, SSL_load_client_CA_file(CAfile));
2174 }
2175 #ifndef OPENSSL_NO_OCSP
2176 if (s_tlsextstatus) {
2177 SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb);
2178 SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp);
2179 if (ctx2) {
2180 SSL_CTX_set_tlsext_status_cb(ctx2, cert_status_cb);
2181 SSL_CTX_set_tlsext_status_arg(ctx2, &tlscstatp);
2182 }
2183 }
2184 #endif
2185 if (set_keylog_file(ctx, keylog_file))
2186 goto end;
2187
2188 if (max_early_data >= 0)
2189 SSL_CTX_set_max_early_data(ctx, max_early_data);
2190 if (recv_max_early_data >= 0)
2191 SSL_CTX_set_recv_max_early_data(ctx, recv_max_early_data);
2192
2193 if (rev)
2194 server_cb = rev_body;
2195 else if (www)
2196 server_cb = www_body;
2197 else
2198 server_cb = sv_body;
2199 #ifdef AF_UNIX
2200 if (socket_family == AF_UNIX
2201 && unlink_unix_path)
2202 unlink(host);
2203 #endif
2204 do_server(&accept_socket, host, port, socket_family, socket_type, protocol,
2205 server_cb, context, naccept, bio_s_out);
2206 print_stats(bio_s_out, ctx);
2207 ret = 0;
2208 end:
2209 SSL_CTX_free(ctx);
2210 SSL_SESSION_free(psksess);
2211 set_keylog_file(NULL, NULL);
2212 X509_free(s_cert);
2213 sk_X509_CRL_pop_free(crls, X509_CRL_free);
2214 X509_free(s_dcert);
2215 EVP_PKEY_free(s_key);
2216 EVP_PKEY_free(s_dkey);
2217 sk_X509_pop_free(s_chain, X509_free);
2218 sk_X509_pop_free(s_dchain, X509_free);
2219 OPENSSL_free(pass);
2220 OPENSSL_free(dpass);
2221 OPENSSL_free(host);
2222 OPENSSL_free(port);
2223 X509_VERIFY_PARAM_free(vpm);
2224 free_sessions();
2225 OPENSSL_free(tlscstatp.host);
2226 OPENSSL_free(tlscstatp.port);
2227 OPENSSL_free(tlscstatp.path);
2228 SSL_CTX_free(ctx2);
2229 X509_free(s_cert2);
2230 EVP_PKEY_free(s_key2);
2231 #ifndef OPENSSL_NO_NEXTPROTONEG
2232 OPENSSL_free(next_proto.data);
2233 #endif
2234 OPENSSL_free(alpn_ctx.data);
2235 ssl_excert_free(exc);
2236 sk_OPENSSL_STRING_free(ssl_args);
2237 SSL_CONF_CTX_free(cctx);
2238 release_engine(engine);
2239 BIO_free(bio_s_out);
2240 bio_s_out = NULL;
2241 BIO_free(bio_s_msg);
2242 bio_s_msg = NULL;
2243 #ifdef CHARSET_EBCDIC
2244 BIO_meth_free(methods_ebcdic);
2245 #endif
2246 return ret;
2247 }
2248
2249 static void print_stats(BIO *bio, SSL_CTX *ssl_ctx)
2250 {
2251 BIO_printf(bio, "%4ld items in the session cache\n",
2252 SSL_CTX_sess_number(ssl_ctx));
2253 BIO_printf(bio, "%4ld client connects (SSL_connect())\n",
2254 SSL_CTX_sess_connect(ssl_ctx));
2255 BIO_printf(bio, "%4ld client renegotiates (SSL_connect())\n",
2256 SSL_CTX_sess_connect_renegotiate(ssl_ctx));
2257 BIO_printf(bio, "%4ld client connects that finished\n",
2258 SSL_CTX_sess_connect_good(ssl_ctx));
2259 BIO_printf(bio, "%4ld server accepts (SSL_accept())\n",
2260 SSL_CTX_sess_accept(ssl_ctx));
2261 BIO_printf(bio, "%4ld server renegotiates (SSL_accept())\n",
2262 SSL_CTX_sess_accept_renegotiate(ssl_ctx));
2263 BIO_printf(bio, "%4ld server accepts that finished\n",
2264 SSL_CTX_sess_accept_good(ssl_ctx));
2265 BIO_printf(bio, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx));
2266 BIO_printf(bio, "%4ld session cache misses\n",
2267 SSL_CTX_sess_misses(ssl_ctx));
2268 BIO_printf(bio, "%4ld session cache timeouts\n",
2269 SSL_CTX_sess_timeouts(ssl_ctx));
2270 BIO_printf(bio, "%4ld callback cache hits\n",
2271 SSL_CTX_sess_cb_hits(ssl_ctx));
2272 BIO_printf(bio, "%4ld cache full overflows (%ld allowed)\n",
2273 SSL_CTX_sess_cache_full(ssl_ctx),
2274 SSL_CTX_sess_get_cache_size(ssl_ctx));
2275 }
2276
2277 static int sv_body(int s, int stype, int prot, unsigned char *context)
2278 {
2279 char *buf = NULL;
2280 fd_set readfds;
2281 int ret = 1, width;
2282 int k, i;
2283 unsigned long l;
2284 SSL *con = NULL;
2285 BIO *sbio;
2286 struct timeval timeout;
2287 #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS))
2288 struct timeval *timeoutp;
2289 #endif
2290 #ifndef OPENSSL_NO_DTLS
2291 # ifndef OPENSSL_NO_SCTP
2292 int isdtls = (stype == SOCK_DGRAM || prot == IPPROTO_SCTP);
2293 # else
2294 int isdtls = (stype == SOCK_DGRAM);
2295 # endif
2296 #endif
2297
2298 buf = app_malloc(bufsize, "server buffer");
2299 if (s_nbio) {
2300 if (!BIO_socket_nbio(s, 1))
2301 ERR_print_errors(bio_err);
2302 else if (!s_quiet)
2303 BIO_printf(bio_err, "Turned on non blocking io\n");
2304 }
2305
2306 con = SSL_new(ctx);
2307 if (con == NULL) {
2308 ret = -1;
2309 goto err;
2310 }
2311
2312 if (s_tlsextdebug) {
2313 SSL_set_tlsext_debug_callback(con, tlsext_cb);
2314 SSL_set_tlsext_debug_arg(con, bio_s_out);
2315 }
2316
2317 if (context != NULL
2318 && !SSL_set_session_id_context(con, context,
2319 strlen((char *)context))) {
2320 BIO_printf(bio_err, "Error setting session id context\n");
2321 ret = -1;
2322 goto err;
2323 }
2324
2325 if (!SSL_clear(con)) {
2326 BIO_printf(bio_err, "Error clearing SSL connection\n");
2327 ret = -1;
2328 goto err;
2329 }
2330 #ifndef OPENSSL_NO_DTLS
2331 if (isdtls) {
2332 # ifndef OPENSSL_NO_SCTP
2333 if (prot == IPPROTO_SCTP)
2334 sbio = BIO_new_dgram_sctp(s, BIO_NOCLOSE);
2335 else
2336 # endif
2337 sbio = BIO_new_dgram(s, BIO_NOCLOSE);
2338
2339 if (enable_timeouts) {
2340 timeout.tv_sec = 0;
2341 timeout.tv_usec = DGRAM_RCV_TIMEOUT;
2342 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
2343
2344 timeout.tv_sec = 0;
2345 timeout.tv_usec = DGRAM_SND_TIMEOUT;
2346 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
2347 }
2348
2349 if (socket_mtu) {
2350 if (socket_mtu < DTLS_get_link_min_mtu(con)) {
2351 BIO_printf(bio_err, "MTU too small. Must be at least %ld\n",
2352 DTLS_get_link_min_mtu(con));
2353 ret = -1;
2354 BIO_free(sbio);
2355 goto err;
2356 }
2357 SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
2358 if (!DTLS_set_link_mtu(con, socket_mtu)) {
2359 BIO_printf(bio_err, "Failed to set MTU\n");
2360 ret = -1;
2361 BIO_free(sbio);
2362 goto err;
2363 }
2364 } else
2365 /* want to do MTU discovery */
2366 BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
2367
2368 # ifndef OPENSSL_NO_SCTP
2369 if (prot != IPPROTO_SCTP)
2370 # endif
2371 /* Turn on cookie exchange. Not necessary for SCTP */
2372 SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE);
2373 } else
2374 #endif
2375 sbio = BIO_new_socket(s, BIO_NOCLOSE);
2376
2377 if (sbio == NULL) {
2378 BIO_printf(bio_err, "Unable to create BIO\n");
2379 ERR_print_errors(bio_err);
2380 goto err;
2381 }
2382
2383 if (s_nbio_test) {
2384 BIO *test;
2385
2386 test = BIO_new(BIO_f_nbio_test());
2387 sbio = BIO_push(test, sbio);
2388 }
2389
2390 SSL_set_bio(con, sbio, sbio);
2391 SSL_set_accept_state(con);
2392 /* SSL_set_fd(con,s); */
2393
2394 if (s_debug) {
2395 BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
2396 BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
2397 }
2398 if (s_msg) {
2399 #ifndef OPENSSL_NO_SSL_TRACE
2400 if (s_msg == 2)
2401 SSL_set_msg_callback(con, SSL_trace);
2402 else
2403 #endif
2404 SSL_set_msg_callback(con, msg_cb);
2405 SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
2406 }
2407
2408 if (s_tlsextdebug) {
2409 SSL_set_tlsext_debug_callback(con, tlsext_cb);
2410 SSL_set_tlsext_debug_arg(con, bio_s_out);
2411 }
2412
2413 if (early_data) {
2414 int write_header = 1, edret = SSL_READ_EARLY_DATA_ERROR;
2415 size_t readbytes;
2416
2417 while (edret != SSL_READ_EARLY_DATA_FINISH) {
2418 for (;;) {
2419 edret = SSL_read_early_data(con, buf, bufsize, &readbytes);
2420 if (edret != SSL_READ_EARLY_DATA_ERROR)
2421 break;
2422
2423 switch (SSL_get_error(con, 0)) {
2424 case SSL_ERROR_WANT_WRITE:
2425 case SSL_ERROR_WANT_ASYNC:
2426 case SSL_ERROR_WANT_READ:
2427 /* Just keep trying - busy waiting */
2428 continue;
2429 default:
2430 BIO_printf(bio_err, "Error reading early data\n");
2431 ERR_print_errors(bio_err);
2432 goto err;
2433 }
2434 }
2435 if (readbytes > 0) {
2436 if (write_header) {
2437 BIO_printf(bio_s_out, "Early data received:\n");
2438 write_header = 0;
2439 }
2440 raw_write_stdout(buf, (unsigned int)readbytes);
2441 (void)BIO_flush(bio_s_out);
2442 }
2443 }
2444 if (write_header) {
2445 if (SSL_get_early_data_status(con) == SSL_EARLY_DATA_NOT_SENT)
2446 BIO_printf(bio_s_out, "No early data received\n");
2447 else
2448 BIO_printf(bio_s_out, "Early data was rejected\n");
2449 } else {
2450 BIO_printf(bio_s_out, "\nEnd of early data\n");
2451 }
2452 if (SSL_is_init_finished(con))
2453 print_connection_info(con);
2454 }
2455
2456 if (fileno_stdin() > s)
2457 width = fileno_stdin() + 1;
2458 else
2459 width = s + 1;
2460 for (;;) {
2461 int read_from_terminal;
2462 int read_from_sslcon;
2463
2464 read_from_terminal = 0;
2465 read_from_sslcon = SSL_has_pending(con)
2466 || (async && SSL_waiting_for_async(con));
2467
2468 if (!read_from_sslcon) {
2469 FD_ZERO(&readfds);
2470 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
2471 openssl_fdset(fileno_stdin(), &readfds);
2472 #endif
2473 openssl_fdset(s, &readfds);
2474 /*
2475 * Note: under VMS with SOCKETSHR the second parameter is
2476 * currently of type (int *) whereas under other systems it is
2477 * (void *) if you don't have a cast it will choke the compiler:
2478 * if you do have a cast then you can either go for (int *) or
2479 * (void *).
2480 */
2481 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
2482 /*
2483 * Under DOS (non-djgpp) and Windows we can't select on stdin:
2484 * only on sockets. As a workaround we timeout the select every
2485 * second and check for any keypress. In a proper Windows
2486 * application we wouldn't do this because it is inefficient.
2487 */
2488 timeout.tv_sec = 1;
2489 timeout.tv_usec = 0;
2490 i = select(width, (void *)&readfds, NULL, NULL, &timeout);
2491 if (has_stdin_waiting())
2492 read_from_terminal = 1;
2493 if ((i < 0) || (!i && !read_from_terminal))
2494 continue;
2495 #else
2496 if (SSL_is_dtls(con) && DTLSv1_get_timeout(con, &timeout))
2497 timeoutp = &timeout;
2498 else
2499 timeoutp = NULL;
2500
2501 i = select(width, (void *)&readfds, NULL, NULL, timeoutp);
2502
2503 if ((SSL_is_dtls(con)) && DTLSv1_handle_timeout(con) > 0)
2504 BIO_printf(bio_err, "TIMEOUT occurred\n");
2505
2506 if (i <= 0)
2507 continue;
2508 if (FD_ISSET(fileno_stdin(), &readfds))
2509 read_from_terminal = 1;
2510 #endif
2511 if (FD_ISSET(s, &readfds))
2512 read_from_sslcon = 1;
2513 }
2514 if (read_from_terminal) {
2515 if (s_crlf) {
2516 int j, lf_num;
2517
2518 i = raw_read_stdin(buf, bufsize / 2);
2519 lf_num = 0;
2520 /* both loops are skipped when i <= 0 */
2521 for (j = 0; j < i; j++)
2522 if (buf[j] == '\n')
2523 lf_num++;
2524 for (j = i - 1; j >= 0; j--) {
2525 buf[j + lf_num] = buf[j];
2526 if (buf[j] == '\n') {
2527 lf_num--;
2528 i++;
2529 buf[j + lf_num] = '\r';
2530 }
2531 }
2532 assert(lf_num == 0);
2533 } else {
2534 i = raw_read_stdin(buf, bufsize);
2535 }
2536
2537 if (!s_quiet && !s_brief) {
2538 if ((i <= 0) || (buf[0] == 'Q')) {
2539 BIO_printf(bio_s_out, "DONE\n");
2540 (void)BIO_flush(bio_s_out);
2541 BIO_closesocket(s);
2542 close_accept_socket();
2543 ret = -11;
2544 goto err;
2545 }
2546 if ((i <= 0) || (buf[0] == 'q')) {
2547 BIO_printf(bio_s_out, "DONE\n");
2548 (void)BIO_flush(bio_s_out);
2549 if (SSL_version(con) != DTLS1_VERSION)
2550 BIO_closesocket(s);
2551 /*
2552 * close_accept_socket(); ret= -11;
2553 */
2554 goto err;
2555 }
2556 if ((buf[0] == 'r') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
2557 SSL_renegotiate(con);
2558 i = SSL_do_handshake(con);
2559 printf("SSL_do_handshake -> %d\n", i);
2560 i = 0; /* 13; */
2561 continue;
2562 }
2563 if ((buf[0] == 'R') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
2564 SSL_set_verify(con,
2565 SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
2566 NULL);
2567 SSL_renegotiate(con);
2568 i = SSL_do_handshake(con);
2569 printf("SSL_do_handshake -> %d\n", i);
2570 i = 0; /* 13; */
2571 continue;
2572 }
2573 if ((buf[0] == 'K' || buf[0] == 'k')
2574 && ((buf[1] == '\n') || (buf[1] == '\r'))) {
2575 SSL_key_update(con, buf[0] == 'K' ?
2576 SSL_KEY_UPDATE_REQUESTED
2577 : SSL_KEY_UPDATE_NOT_REQUESTED);
2578 i = SSL_do_handshake(con);
2579 printf("SSL_do_handshake -> %d\n", i);
2580 i = 0;
2581 continue;
2582 }
2583 if (buf[0] == 'c' && ((buf[1] == '\n') || (buf[1] == '\r'))) {
2584 SSL_set_verify(con, SSL_VERIFY_PEER, NULL);
2585 i = SSL_verify_client_post_handshake(con);
2586 if (i == 0) {
2587 printf("Failed to initiate request\n");
2588 ERR_print_errors(bio_err);
2589 } else {
2590 i = SSL_do_handshake(con);
2591 printf("SSL_do_handshake -> %d\n", i);
2592 i = 0;
2593 }
2594 continue;
2595 }
2596 if (buf[0] == 'P') {
2597 static const char str[] = "Lets print some clear text\n";
2598 BIO_write(SSL_get_wbio(con), str, sizeof(str) -1);
2599 }
2600 if (buf[0] == 'S') {
2601 print_stats(bio_s_out, SSL_get_SSL_CTX(con));
2602 }
2603 }
2604 #ifdef CHARSET_EBCDIC
2605 ebcdic2ascii(buf, buf, i);
2606 #endif
2607 l = k = 0;
2608 for (;;) {
2609 /* should do a select for the write */
2610 #ifdef RENEG
2611 static count = 0;
2612 if (++count == 100) {
2613 count = 0;
2614 SSL_renegotiate(con);
2615 }
2616 #endif
2617 k = SSL_write(con, &(buf[l]), (unsigned int)i);
2618 #ifndef OPENSSL_NO_SRP
2619 while (SSL_get_error(con, k) == SSL_ERROR_WANT_X509_LOOKUP) {
2620 BIO_printf(bio_s_out, "LOOKUP renego during write\n");
2621
2622 lookup_srp_user(&srp_callback_parm, bio_s_out);
2623
2624 k = SSL_write(con, &(buf[l]), (unsigned int)i);
2625 }
2626 #endif
2627 switch (SSL_get_error(con, k)) {
2628 case SSL_ERROR_NONE:
2629 break;
2630 case SSL_ERROR_WANT_ASYNC:
2631 BIO_printf(bio_s_out, "Write BLOCK (Async)\n");
2632 (void)BIO_flush(bio_s_out);
2633 wait_for_async(con);
2634 break;
2635 case SSL_ERROR_WANT_WRITE:
2636 case SSL_ERROR_WANT_READ:
2637 case SSL_ERROR_WANT_X509_LOOKUP:
2638 BIO_printf(bio_s_out, "Write BLOCK\n");
2639 (void)BIO_flush(bio_s_out);
2640 break;
2641 case SSL_ERROR_WANT_ASYNC_JOB:
2642 /*
2643 * This shouldn't ever happen in s_server. Treat as an error
2644 */
2645 case SSL_ERROR_SYSCALL:
2646 case SSL_ERROR_SSL:
2647 BIO_printf(bio_s_out, "ERROR\n");
2648 (void)BIO_flush(bio_s_out);
2649 ERR_print_errors(bio_err);
2650 ret = 1;
2651 goto err;
2652 /* break; */
2653 case SSL_ERROR_ZERO_RETURN:
2654 BIO_printf(bio_s_out, "DONE\n");
2655 (void)BIO_flush(bio_s_out);
2656 ret = 1;
2657 goto err;
2658 }
2659 if (k > 0) {
2660 l += k;
2661 i -= k;
2662 }
2663 if (i <= 0)
2664 break;
2665 }
2666 }
2667 if (read_from_sslcon) {
2668 /*
2669 * init_ssl_connection handles all async events itself so if we're
2670 * waiting for async then we shouldn't go back into
2671 * init_ssl_connection
2672 */
2673 if ((!async || !SSL_waiting_for_async(con))
2674 && !SSL_is_init_finished(con)) {
2675 i = init_ssl_connection(con);
2676
2677 if (i < 0) {
2678 ret = 0;
2679 goto err;
2680 } else if (i == 0) {
2681 ret = 1;
2682 goto err;
2683 }
2684 } else {
2685 again:
2686 i = SSL_read(con, (char *)buf, bufsize);
2687 #ifndef OPENSSL_NO_SRP
2688 while (SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) {
2689 BIO_printf(bio_s_out, "LOOKUP renego during read\n");
2690
2691 lookup_srp_user(&srp_callback_parm, bio_s_out);
2692
2693 i = SSL_read(con, (char *)buf, bufsize);
2694 }
2695 #endif
2696 switch (SSL_get_error(con, i)) {
2697 case SSL_ERROR_NONE:
2698 #ifdef CHARSET_EBCDIC
2699 ascii2ebcdic(buf, buf, i);
2700 #endif
2701 raw_write_stdout(buf, (unsigned int)i);
2702 (void)BIO_flush(bio_s_out);
2703 if (SSL_has_pending(con))
2704 goto again;
2705 break;
2706 case SSL_ERROR_WANT_ASYNC:
2707 BIO_printf(bio_s_out, "Read BLOCK (Async)\n");
2708 (void)BIO_flush(bio_s_out);
2709 wait_for_async(con);
2710 break;
2711 case SSL_ERROR_WANT_WRITE:
2712 case SSL_ERROR_WANT_READ:
2713 BIO_printf(bio_s_out, "Read BLOCK\n");
2714 (void)BIO_flush(bio_s_out);
2715 break;
2716 case SSL_ERROR_WANT_ASYNC_JOB:
2717 /*
2718 * This shouldn't ever happen in s_server. Treat as an error
2719 */
2720 case SSL_ERROR_SYSCALL:
2721 case SSL_ERROR_SSL:
2722 BIO_printf(bio_s_out, "ERROR\n");
2723 (void)BIO_flush(bio_s_out);
2724 ERR_print_errors(bio_err);
2725 ret = 1;
2726 goto err;
2727 case SSL_ERROR_ZERO_RETURN:
2728 BIO_printf(bio_s_out, "DONE\n");
2729 (void)BIO_flush(bio_s_out);
2730 ret = 1;
2731 goto err;
2732 }
2733 }
2734 }
2735 }
2736 err:
2737 if (con != NULL) {
2738 BIO_printf(bio_s_out, "shutting down SSL\n");
2739 do_ssl_shutdown(con);
2740 SSL_free(con);
2741 }
2742 BIO_printf(bio_s_out, "CONNECTION CLOSED\n");
2743 OPENSSL_clear_free(buf, bufsize);
2744 return ret;
2745 }
2746
2747 static void close_accept_socket(void)
2748 {
2749 BIO_printf(bio_err, "shutdown accept socket\n");
2750 if (accept_socket >= 0) {
2751 BIO_closesocket(accept_socket);
2752 }
2753 }
2754
2755 static int is_retryable(SSL *con, int i)
2756 {
2757 int err = SSL_get_error(con, i);
2758
2759 /* If it's not a fatal error, it must be retryable */
2760 return (err != SSL_ERROR_SSL)
2761 && (err != SSL_ERROR_SYSCALL)
2762 && (err != SSL_ERROR_ZERO_RETURN);
2763 }
2764
2765 static int init_ssl_connection(SSL *con)
2766 {
2767 int i;
2768 long verify_err;
2769 int retry = 0;
2770
2771 if (dtlslisten || stateless) {
2772 BIO_ADDR *client = NULL;
2773
2774 if (dtlslisten) {
2775 if ((client = BIO_ADDR_new()) == NULL) {
2776 BIO_printf(bio_err, "ERROR - memory\n");
2777 return 0;
2778 }
2779 i = DTLSv1_listen(con, client);
2780 } else {
2781 i = SSL_stateless(con);
2782 }
2783 if (i > 0) {
2784 BIO *wbio;
2785 int fd = -1;
2786
2787 if (dtlslisten) {
2788 wbio = SSL_get_wbio(con);
2789 if (wbio) {
2790 BIO_get_fd(wbio, &fd);
2791 }
2792
2793 if (!wbio || BIO_connect(fd, client, 0) == 0) {
2794 BIO_printf(bio_err, "ERROR - unable to connect\n");
2795 BIO_ADDR_free(client);
2796 return 0;
2797 }
2798
2799 (void)BIO_ctrl_set_connected(wbio, client);
2800 BIO_ADDR_free(client);
2801 dtlslisten = 0;
2802 } else {
2803 stateless = 0;
2804 }
2805 i = SSL_accept(con);
2806 } else {
2807 BIO_ADDR_free(client);
2808 }
2809 } else {
2810 do {
2811 i = SSL_accept(con);
2812 if (immediate_reneg)
2813 SSL_renegotiate(con);
2814
2815 if (i <= 0)
2816 retry = is_retryable(con, i);
2817 #ifdef CERT_CB_TEST_RETRY
2818 {
2819 while (i <= 0
2820 && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP
2821 && SSL_get_state(con) == TLS_ST_SR_CLNT_HELLO) {
2822 BIO_printf(bio_err,
2823 "LOOKUP from certificate callback during accept\n");
2824 i = SSL_accept(con);
2825 if (i <= 0)
2826 retry = is_retryable(con, i);
2827 }
2828 }
2829 #endif
2830
2831 #ifndef OPENSSL_NO_SRP
2832 while (i <= 0
2833 && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) {
2834 BIO_printf(bio_s_out, "LOOKUP during accept %s\n",
2835 srp_callback_parm.login);
2836
2837 lookup_srp_user(&srp_callback_parm, bio_s_out);
2838
2839 i = SSL_accept(con);
2840 if (i <= 0)
2841 retry = is_retryable(con, i);
2842 }
2843 #endif
2844 } while (i < 0 && SSL_waiting_for_async(con));
2845 }
2846
2847 if (i <= 0) {
2848 if (((dtlslisten || stateless) && i == 0)
2849 || (!dtlslisten && !stateless && retry)) {
2850 BIO_printf(bio_s_out, "DELAY\n");
2851 return 1;
2852 }
2853
2854 BIO_printf(bio_err, "ERROR\n");
2855
2856 verify_err = SSL_get_verify_result(con);
2857 if (verify_err != X509_V_OK) {
2858 BIO_printf(bio_err, "verify error:%s\n",
2859 X509_verify_cert_error_string(verify_err));
2860 }
2861 /* Always print any error messages */
2862 ERR_print_errors(bio_err);
2863 return 0;
2864 }
2865
2866 print_connection_info(con);
2867 return 1;
2868 }
2869
2870 static void print_connection_info(SSL *con)
2871 {
2872 const char *str;
2873 X509 *peer;
2874 char buf[BUFSIZ];
2875 #if !defined(OPENSSL_NO_NEXTPROTONEG)
2876 const unsigned char *next_proto_neg;
2877 unsigned next_proto_neg_len;
2878 #endif
2879 unsigned char *exportedkeymat;
2880 int i;
2881
2882 if (s_brief)
2883 print_ssl_summary(con);
2884
2885 PEM_write_bio_SSL_SESSION(bio_s_out, SSL_get_session(con));
2886
2887 peer = SSL_get0_peer_certificate(con);
2888 if (peer != NULL) {
2889 BIO_printf(bio_s_out, "Client certificate\n");
2890 PEM_write_bio_X509(bio_s_out, peer);
2891 dump_cert_text(bio_s_out, peer);
2892 peer = NULL;
2893 }
2894
2895 if (SSL_get_shared_ciphers(con, buf, sizeof(buf)) != NULL)
2896 BIO_printf(bio_s_out, "Shared ciphers:%s\n", buf);
2897 str = SSL_CIPHER_get_name(SSL_get_current_cipher(con));
2898 ssl_print_sigalgs(bio_s_out, con);
2899 #ifndef OPENSSL_NO_EC
2900 ssl_print_point_formats(bio_s_out, con);
2901 ssl_print_groups(bio_s_out, con, 0);
2902 #endif
2903 print_ca_names(bio_s_out, con);
2904 BIO_printf(bio_s_out, "CIPHER is %s\n", (str != NULL) ? str : "(NONE)");
2905
2906 #if !defined(OPENSSL_NO_NEXTPROTONEG)
2907 SSL_get0_next_proto_negotiated(con, &next_proto_neg, &next_proto_neg_len);
2908 if (next_proto_neg) {
2909 BIO_printf(bio_s_out, "NEXTPROTO is ");
2910 BIO_write(bio_s_out, next_proto_neg, next_proto_neg_len);
2911 BIO_printf(bio_s_out, "\n");
2912 }
2913 #endif
2914 #ifndef OPENSSL_NO_SRTP
2915 {
2916 SRTP_PROTECTION_PROFILE *srtp_profile
2917 = SSL_get_selected_srtp_profile(con);
2918
2919 if (srtp_profile)
2920 BIO_printf(bio_s_out, "SRTP Extension negotiated, profile=%s\n",
2921 srtp_profile->name);
2922 }
2923 #endif
2924 if (SSL_session_reused(con))
2925 BIO_printf(bio_s_out, "Reused session-id\n");
2926 BIO_printf(bio_s_out, "Secure Renegotiation IS%s supported\n",
2927 SSL_get_secure_renegotiation_support(con) ? "" : " NOT");
2928 if ((SSL_get_options(con) & SSL_OP_NO_RENEGOTIATION))
2929 BIO_printf(bio_s_out, "Renegotiation is DISABLED\n");
2930
2931 if (keymatexportlabel != NULL) {
2932 BIO_printf(bio_s_out, "Keying material exporter:\n");
2933 BIO_printf(bio_s_out, " Label: '%s'\n", keymatexportlabel);
2934 BIO_printf(bio_s_out, " Length: %i bytes\n", keymatexportlen);
2935 exportedkeymat = app_malloc(keymatexportlen, "export key");
2936 if (!SSL_export_keying_material(con, exportedkeymat,
2937 keymatexportlen,
2938 keymatexportlabel,
2939 strlen(keymatexportlabel),
2940 NULL, 0, 0)) {
2941 BIO_printf(bio_s_out, " Error\n");
2942 } else {
2943 BIO_printf(bio_s_out, " Keying material: ");
2944 for (i = 0; i < keymatexportlen; i++)
2945 BIO_printf(bio_s_out, "%02X", exportedkeymat[i]);
2946 BIO_printf(bio_s_out, "\n");
2947 }
2948 OPENSSL_free(exportedkeymat);
2949 }
2950 #ifndef OPENSSL_NO_KTLS
2951 if (BIO_get_ktls_send(SSL_get_wbio(con)))
2952 BIO_printf(bio_err, "Using Kernel TLS for sending\n");
2953 if (BIO_get_ktls_recv(SSL_get_rbio(con)))
2954 BIO_printf(bio_err, "Using Kernel TLS for receiving\n");
2955 #endif
2956
2957 (void)BIO_flush(bio_s_out);
2958 }
2959
2960 static int www_body(int s, int stype, int prot, unsigned char *context)
2961 {
2962 char *buf = NULL;
2963 int ret = 1;
2964 int i, j, k, dot;
2965 SSL *con;
2966 const SSL_CIPHER *c;
2967 BIO *io, *ssl_bio, *sbio;
2968 #ifdef RENEG
2969 int total_bytes = 0;
2970 #endif
2971 int width;
2972 fd_set readfds;
2973 const char *opmode;
2974
2975 /* Set width for a select call if needed */
2976 width = s + 1;
2977
2978 buf = app_malloc(bufsize, "server www buffer");
2979 io = BIO_new(BIO_f_buffer());
2980 ssl_bio = BIO_new(BIO_f_ssl());
2981 if ((io == NULL) || (ssl_bio == NULL))
2982 goto err;
2983
2984 if (s_nbio) {
2985 if (!BIO_socket_nbio(s, 1))
2986 ERR_print_errors(bio_err);
2987 else if (!s_quiet)
2988 BIO_printf(bio_err, "Turned on non blocking io\n");
2989 }
2990
2991 /* lets make the output buffer a reasonable size */
2992 if (!BIO_set_write_buffer_size(io, bufsize))
2993 goto err;
2994
2995 if ((con = SSL_new(ctx)) == NULL)
2996 goto err;
2997
2998 if (s_tlsextdebug) {
2999 SSL_set_tlsext_debug_callback(con, tlsext_cb);
3000 SSL_set_tlsext_debug_arg(con, bio_s_out);
3001 }
3002
3003 if (context != NULL
3004 && !SSL_set_session_id_context(con, context,
3005 strlen((char *)context))) {
3006 SSL_free(con);
3007 goto err;
3008 }
3009
3010 sbio = BIO_new_socket(s, BIO_NOCLOSE);
3011 if (s_nbio_test) {
3012 BIO *test;
3013
3014 test = BIO_new(BIO_f_nbio_test());
3015 sbio = BIO_push(test, sbio);
3016 }
3017 SSL_set_bio(con, sbio, sbio);
3018 SSL_set_accept_state(con);
3019
3020 /* No need to free |con| after this. Done by BIO_free(ssl_bio) */
3021 BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
3022 BIO_push(io, ssl_bio);
3023 #ifdef CHARSET_EBCDIC
3024 io = BIO_push(BIO_new(BIO_f_ebcdic_filter()), io);
3025 #endif
3026
3027 if (s_debug) {
3028 BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
3029 BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
3030 }
3031 if (s_msg) {
3032 #ifndef OPENSSL_NO_SSL_TRACE
3033 if (s_msg == 2)
3034 SSL_set_msg_callback(con, SSL_trace);
3035 else
3036 #endif
3037 SSL_set_msg_callback(con, msg_cb);
3038 SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
3039 }
3040
3041 for (;;) {
3042 i = BIO_gets(io, buf, bufsize - 1);
3043 if (i < 0) { /* error */
3044 if (!BIO_should_retry(io) && !SSL_waiting_for_async(con)) {
3045 if (!s_quiet)
3046 ERR_print_errors(bio_err);
3047 goto err;
3048 } else {
3049 BIO_printf(bio_s_out, "read R BLOCK\n");
3050 #ifndef OPENSSL_NO_SRP
3051 if (BIO_should_io_special(io)
3052 && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
3053 BIO_printf(bio_s_out, "LOOKUP renego during read\n");
3054
3055 lookup_srp_user(&srp_callback_parm, bio_s_out);
3056
3057 continue;
3058 }
3059 #endif
3060 ossl_sleep(1000);
3061 continue;
3062 }
3063 } else if (i == 0) { /* end of input */
3064 ret = 1;
3065 goto end;
3066 }
3067
3068 /* else we have data */
3069 if (((www == 1) && (strncmp("GET ", buf, 4) == 0)) ||
3070 ((www == 2) && (strncmp("GET /stats ", buf, 11) == 0))) {
3071 char *p;
3072 X509 *peer = NULL;
3073 STACK_OF(SSL_CIPHER) *sk;
3074 static const char *space = " ";
3075
3076 if (www == 1 && strncmp("GET /reneg", buf, 10) == 0) {
3077 if (strncmp("GET /renegcert", buf, 14) == 0)
3078 SSL_set_verify(con,
3079 SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
3080 NULL);
3081 i = SSL_renegotiate(con);
3082 BIO_printf(bio_s_out, "SSL_renegotiate -> %d\n", i);
3083 /* Send the HelloRequest */
3084 i = SSL_do_handshake(con);
3085 if (i <= 0) {
3086 BIO_printf(bio_s_out, "SSL_do_handshake() Retval %d\n",
3087 SSL_get_error(con, i));
3088 ERR_print_errors(bio_err);
3089 goto err;
3090 }
3091 /* Wait for a ClientHello to come back */
3092 FD_ZERO(&readfds);
3093 openssl_fdset(s, &readfds);
3094 i = select(width, (void *)&readfds, NULL, NULL, NULL);
3095 if (i <= 0 || !FD_ISSET(s, &readfds)) {
3096 BIO_printf(bio_s_out,
3097 "Error waiting for client response\n");
3098 ERR_print_errors(bio_err);
3099 goto err;
3100 }
3101 /*
3102 * We're not actually expecting any data here and we ignore
3103 * any that is sent. This is just to force the handshake that
3104 * we're expecting to come from the client. If they haven't
3105 * sent one there's not much we can do.
3106 */
3107 BIO_gets(io, buf, bufsize - 1);
3108 }
3109
3110 BIO_puts(io,
3111 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
3112 BIO_puts(io, "<HTML><BODY BGCOLOR=\"#ffffff\">\n");
3113 BIO_puts(io, "<pre>\n");
3114 /* BIO_puts(io, OpenSSL_version(OPENSSL_VERSION)); */
3115 BIO_puts(io, "\n");
3116 for (i = 0; i < local_argc; i++) {
3117 const char *myp;
3118 for (myp = local_argv[i]; *myp; myp++)
3119 switch (*myp) {
3120 case '<':
3121 BIO_puts(io, "&lt;");
3122 break;
3123 case '>':
3124 BIO_puts(io, "&gt;");
3125 break;
3126 case '&':
3127 BIO_puts(io, "&amp;");
3128 break;
3129 default:
3130 BIO_write(io, myp, 1);
3131 break;
3132 }
3133 BIO_write(io, " ", 1);
3134 }
3135 BIO_puts(io, "\n");
3136
3137 BIO_printf(io,
3138 "Secure Renegotiation IS%s supported\n",
3139 SSL_get_secure_renegotiation_support(con) ?
3140 "" : " NOT");
3141
3142 /*
3143 * The following is evil and should not really be done
3144 */
3145 BIO_printf(io, "Ciphers supported in s_server binary\n");
3146 sk = SSL_get_ciphers(con);
3147 j = sk_SSL_CIPHER_num(sk);
3148 for (i = 0; i < j; i++) {
3149 c = sk_SSL_CIPHER_value(sk, i);
3150 BIO_printf(io, "%-11s:%-25s ",
3151 SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
3152 if ((((i + 1) % 2) == 0) && (i + 1 != j))
3153 BIO_puts(io, "\n");
3154 }
3155 BIO_puts(io, "\n");
3156 p = SSL_get_shared_ciphers(con, buf, bufsize);
3157 if (p != NULL) {
3158 BIO_printf(io,
3159 "---\nCiphers common between both SSL end points:\n");
3160 j = i = 0;
3161 while (*p) {
3162 if (*p == ':') {
3163 BIO_write(io, space, 26 - j);
3164 i++;
3165 j = 0;
3166 BIO_write(io, ((i % 3) ? " " : "\n"), 1);
3167 } else {
3168 BIO_write(io, p, 1);
3169 j++;
3170 }
3171 p++;
3172 }
3173 BIO_puts(io, "\n");
3174 }
3175 ssl_print_sigalgs(io, con);
3176 #ifndef OPENSSL_NO_EC
3177 ssl_print_groups(io, con, 0);
3178 #endif
3179 print_ca_names(io, con);
3180 BIO_printf(io, (SSL_session_reused(con)
3181 ? "---\nReused, " : "---\nNew, "));
3182 c = SSL_get_current_cipher(con);
3183 BIO_printf(io, "%s, Cipher is %s\n",
3184 SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
3185 SSL_SESSION_print(io, SSL_get_session(con));
3186 BIO_printf(io, "---\n");
3187 print_stats(io, SSL_get_SSL_CTX(con));
3188 BIO_printf(io, "---\n");
3189 peer = SSL_get0_peer_certificate(con);
3190 if (peer != NULL) {
3191 BIO_printf(io, "Client certificate\n");
3192 X509_print(io, peer);
3193 PEM_write_bio_X509(io, peer);
3194 peer = NULL;
3195 } else {
3196 BIO_puts(io, "no client certificate available\n");
3197 }
3198 BIO_puts(io, "</pre></BODY></HTML>\r\n\r\n");
3199 break;
3200 } else if ((www == 2 || www == 3)
3201 && (strncmp("GET /", buf, 5) == 0)) {
3202 BIO *file;
3203 char *p, *e;
3204 static const char *text =
3205 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
3206
3207 /* skip the '/' */
3208 p = &(buf[5]);
3209
3210 dot = 1;
3211 for (e = p; *e != '\0'; e++) {
3212 if (e[0] == ' ')
3213 break;
3214
3215 if (e[0] == ':') {
3216 /* Windows drive. We treat this the same way as ".." */
3217 dot = -1;
3218 break;
3219 }
3220
3221 switch (dot) {
3222 case 1:
3223 dot = (e[0] == '.') ? 2 : 0;
3224 break;
3225 case 2:
3226 dot = (e[0] == '.') ? 3 : 0;
3227 break;
3228 case 3:
3229 dot = (e[0] == '/' || e[0] == '\\') ? -1 : 0;
3230 break;
3231 }
3232 if (dot == 0)
3233 dot = (e[0] == '/' || e[0] == '\\') ? 1 : 0;
3234 }
3235 dot = (dot == 3) || (dot == -1); /* filename contains ".."
3236 * component */
3237
3238 if (*e == '\0') {
3239 BIO_puts(io, text);
3240 BIO_printf(io, "'%s' is an invalid file name\r\n", p);
3241 break;
3242 }
3243 *e = '\0';
3244
3245 if (dot) {
3246 BIO_puts(io, text);
3247 BIO_printf(io, "'%s' contains '..' or ':'\r\n", p);
3248 break;
3249 }
3250
3251 if (*p == '/' || *p == '\\') {
3252 BIO_puts(io, text);
3253 BIO_printf(io, "'%s' is an invalid path\r\n", p);
3254 break;
3255 }
3256
3257 /* if a directory, do the index thang */
3258 if (app_isdir(p) > 0) {
3259 BIO_puts(io, text);
3260 BIO_printf(io, "'%s' is a directory\r\n", p);
3261 break;
3262 }
3263
3264 opmode = (http_server_binmode == 1) ? "rb" : "r";
3265 if ((file = BIO_new_file(p, opmode)) == NULL) {
3266 BIO_puts(io, text);
3267 BIO_printf(io, "Error opening '%s' mode='%s'\r\n", p, opmode);
3268 ERR_print_errors(io);
3269 break;
3270 }
3271
3272 if (!s_quiet)
3273 BIO_printf(bio_err, "FILE:%s\n", p);
3274
3275 if (www == 2) {
3276 i = strlen(p);
3277 if (((i > 5) && (strcmp(&(p[i - 5]), ".html") == 0)) ||
3278 ((i > 4) && (strcmp(&(p[i - 4]), ".php") == 0)) ||
3279 ((i > 4) && (strcmp(&(p[i - 4]), ".htm") == 0)))
3280 BIO_puts(io,
3281 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
3282 else
3283 BIO_puts(io,
3284 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
3285 }
3286 /* send the file */
3287 #ifndef OPENSSL_NO_KTLS
3288 if (use_sendfile) {
3289 FILE *fp = NULL;
3290 int fd;
3291 struct stat st;
3292 off_t offset = 0;
3293 size_t filesize;
3294
3295 BIO_get_fp(file, &fp);
3296 fd = fileno(fp);
3297 if (fstat(fd, &st) < 0) {
3298 BIO_printf(io, "Error fstat '%s'\r\n", p);
3299 ERR_print_errors(io);
3300 goto write_error;
3301 }
3302
3303 filesize = st.st_size;
3304 if (((int)BIO_flush(io)) < 0)
3305 goto write_error;
3306
3307 for (;;) {
3308 i = SSL_sendfile(con, fd, offset, filesize, 0);
3309 if (i < 0) {
3310 BIO_printf(io, "Error SSL_sendfile '%s'\r\n", p);
3311 ERR_print_errors(io);
3312 break;
3313 } else {
3314 offset += i;
3315 filesize -= i;
3316 }
3317
3318 if (filesize <= 0) {
3319 if (!s_quiet)
3320 BIO_printf(bio_err, "KTLS SENDFILE '%s' OK\n", p);
3321
3322 break;
3323 }
3324 }
3325 } else
3326 #endif
3327 {
3328 for (;;) {
3329 i = BIO_read(file, buf, bufsize);
3330 if (i <= 0)
3331 break;
3332
3333 #ifdef RENEG
3334 total_bytes += i;
3335 BIO_printf(bio_err, "%d\n", i);
3336 if (total_bytes > 3 * 1024) {
3337 total_bytes = 0;
3338 BIO_printf(bio_err, "RENEGOTIATE\n");
3339 SSL_renegotiate(con);
3340 }
3341 #endif
3342
3343 for (j = 0; j < i;) {
3344 #ifdef RENEG
3345 static count = 0;
3346 if (++count == 13)
3347 SSL_renegotiate(con);
3348 #endif
3349 k = BIO_write(io, &(buf[j]), i - j);
3350 if (k <= 0) {
3351 if (!BIO_should_retry(io)
3352 && !SSL_waiting_for_async(con)) {
3353 goto write_error;
3354 } else {
3355 BIO_printf(bio_s_out, "rwrite W BLOCK\n");
3356 }
3357 } else {
3358 j += k;
3359 }
3360 }
3361 }
3362 }
3363 write_error:
3364 BIO_free(file);
3365 break;
3366 }
3367 }
3368
3369 for (;;) {
3370 i = (int)BIO_flush(io);
3371 if (i <= 0) {
3372 if (!BIO_should_retry(io))
3373 break;
3374 } else
3375 break;
3376 }
3377 end:
3378 /* make sure we re-use sessions */
3379 do_ssl_shutdown(con);
3380
3381 err:
3382 OPENSSL_free(buf);
3383 BIO_free_all(io);
3384 return ret;
3385 }
3386
3387 static int rev_body(int s, int stype, int prot, unsigned char *context)
3388 {
3389 char *buf = NULL;
3390 int i;
3391 int ret = 1;
3392 SSL *con;
3393 BIO *io, *ssl_bio, *sbio;
3394
3395 buf = app_malloc(bufsize, "server rev buffer");
3396 io = BIO_new(BIO_f_buffer());
3397 ssl_bio = BIO_new(BIO_f_ssl());
3398 if ((io == NULL) || (ssl_bio == NULL))
3399 goto err;
3400
3401 /* lets make the output buffer a reasonable size */
3402 if (!BIO_set_write_buffer_size(io, bufsize))
3403 goto err;
3404
3405 if ((con = SSL_new(ctx)) == NULL)
3406 goto err;
3407
3408 if (s_tlsextdebug) {
3409 SSL_set_tlsext_debug_callback(con, tlsext_cb);
3410 SSL_set_tlsext_debug_arg(con, bio_s_out);
3411 }
3412 if (context != NULL
3413 && !SSL_set_session_id_context(con, context,
3414 strlen((char *)context))) {
3415 SSL_free(con);
3416 ERR_print_errors(bio_err);
3417 goto err;
3418 }
3419
3420 sbio = BIO_new_socket(s, BIO_NOCLOSE);
3421 SSL_set_bio(con, sbio, sbio);
3422 SSL_set_accept_state(con);
3423
3424 /* No need to free |con| after this. Done by BIO_free(ssl_bio) */
3425 BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
3426 BIO_push(io, ssl_bio);
3427 #ifdef CHARSET_EBCDIC
3428 io = BIO_push(BIO_new(BIO_f_ebcdic_filter()), io);
3429 #endif
3430
3431 if (s_debug) {
3432 BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
3433 BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
3434 }
3435 if (s_msg) {
3436 #ifndef OPENSSL_NO_SSL_TRACE
3437 if (s_msg == 2)
3438 SSL_set_msg_callback(con, SSL_trace);
3439 else
3440 #endif
3441 SSL_set_msg_callback(con, msg_cb);
3442 SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
3443 }
3444
3445 for (;;) {
3446 i = BIO_do_handshake(io);
3447 if (i > 0)
3448 break;
3449 if (!BIO_should_retry(io)) {
3450 BIO_puts(bio_err, "CONNECTION FAILURE\n");
3451 ERR_print_errors(bio_err);
3452 goto end;
3453 }
3454 #ifndef OPENSSL_NO_SRP
3455 if (BIO_should_io_special(io)
3456 && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
3457 BIO_printf(bio_s_out, "LOOKUP renego during accept\n");
3458
3459 lookup_srp_user(&srp_callback_parm, bio_s_out);
3460
3461 continue;
3462 }
3463 #endif
3464 }
3465 BIO_printf(bio_err, "CONNECTION ESTABLISHED\n");
3466 print_ssl_summary(con);
3467
3468 for (;;) {
3469 i = BIO_gets(io, buf, bufsize - 1);
3470 if (i < 0) { /* error */
3471 if (!BIO_should_retry(io)) {
3472 if (!s_quiet)
3473 ERR_print_errors(bio_err);
3474 goto err;
3475 } else {
3476 BIO_printf(bio_s_out, "read R BLOCK\n");
3477 #ifndef OPENSSL_NO_SRP
3478 if (BIO_should_io_special(io)
3479 && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
3480 BIO_printf(bio_s_out, "LOOKUP renego during read\n");
3481
3482 lookup_srp_user(&srp_callback_parm, bio_s_out);
3483
3484 continue;
3485 }
3486 #endif
3487 ossl_sleep(1000);
3488 continue;
3489 }
3490 } else if (i == 0) { /* end of input */
3491 ret = 1;
3492 BIO_printf(bio_err, "CONNECTION CLOSED\n");
3493 goto end;
3494 } else {
3495 char *p = buf + i - 1;
3496 while (i && (*p == '\n' || *p == '\r')) {
3497 p--;
3498 i--;
3499 }
3500 if (!s_ign_eof && (i == 5) && (strncmp(buf, "CLOSE", 5) == 0)) {
3501 ret = 1;
3502 BIO_printf(bio_err, "CONNECTION CLOSED\n");
3503 goto end;
3504 }
3505 BUF_reverse((unsigned char *)buf, NULL, i);
3506 buf[i] = '\n';
3507 BIO_write(io, buf, i + 1);
3508 for (;;) {
3509 i = BIO_flush(io);
3510 if (i > 0)
3511 break;
3512 if (!BIO_should_retry(io))
3513 goto end;
3514 }
3515 }
3516 }
3517 end:
3518 /* make sure we re-use sessions */
3519 do_ssl_shutdown(con);
3520
3521 err:
3522
3523 OPENSSL_free(buf);
3524 BIO_free_all(io);
3525 return ret;
3526 }
3527
3528 #define MAX_SESSION_ID_ATTEMPTS 10
3529 static int generate_session_id(SSL *ssl, unsigned char *id,
3530 unsigned int *id_len)
3531 {
3532 unsigned int count = 0;
3533 unsigned int session_id_prefix_len = strlen(session_id_prefix);
3534
3535 do {
3536 if (RAND_bytes(id, *id_len) <= 0)
3537 return 0;
3538 /*
3539 * Prefix the session_id with the required prefix. NB: If our prefix
3540 * is too long, clip it - but there will be worse effects anyway, eg.
3541 * the server could only possibly create 1 session ID (ie. the
3542 * prefix!) so all future session negotiations will fail due to
3543 * conflicts.
3544 */
3545 memcpy(id, session_id_prefix,
3546 (session_id_prefix_len < *id_len) ?
3547 session_id_prefix_len : *id_len);
3548 }
3549 while (SSL_has_matching_session_id(ssl, id, *id_len) &&
3550 (++count < MAX_SESSION_ID_ATTEMPTS));
3551 if (count >= MAX_SESSION_ID_ATTEMPTS)
3552 return 0;
3553 return 1;
3554 }
3555
3556 /*
3557 * By default s_server uses an in-memory cache which caches SSL_SESSION
3558 * structures without any serialization. This hides some bugs which only
3559 * become apparent in deployed servers. By implementing a basic external
3560 * session cache some issues can be debugged using s_server.
3561 */
3562
3563 typedef struct simple_ssl_session_st {
3564 unsigned char *id;
3565 unsigned int idlen;
3566 unsigned char *der;
3567 int derlen;
3568 struct simple_ssl_session_st *next;
3569 } simple_ssl_session;
3570
3571 static simple_ssl_session *first = NULL;
3572
3573 static int add_session(SSL *ssl, SSL_SESSION *session)
3574 {
3575 simple_ssl_session *sess = app_malloc(sizeof(*sess), "get session");
3576 unsigned char *p;
3577
3578 SSL_SESSION_get_id(session, &sess->idlen);
3579 sess->derlen = i2d_SSL_SESSION(session, NULL);
3580 if (sess->derlen < 0) {
3581 BIO_printf(bio_err, "Error encoding session\n");
3582 OPENSSL_free(sess);
3583 return 0;
3584 }
3585
3586 sess->id = OPENSSL_memdup(SSL_SESSION_get_id(session, NULL), sess->idlen);
3587 sess->der = app_malloc(sess->derlen, "get session buffer");
3588 if (!sess->id) {
3589 BIO_printf(bio_err, "Out of memory adding to external cache\n");
3590 OPENSSL_free(sess->id);
3591 OPENSSL_free(sess->der);
3592 OPENSSL_free(sess);
3593 return 0;
3594 }
3595 p = sess->der;
3596
3597 /* Assume it still works. */
3598 if (i2d_SSL_SESSION(session, &p) != sess->derlen) {
3599 BIO_printf(bio_err, "Unexpected session encoding length\n");
3600 OPENSSL_free(sess->id);
3601 OPENSSL_free(sess->der);
3602 OPENSSL_free(sess);
3603 return 0;
3604 }
3605
3606 sess->next = first;
3607 first = sess;
3608 BIO_printf(bio_err, "New session added to external cache\n");
3609 return 0;
3610 }
3611
3612 static SSL_SESSION *get_session(SSL *ssl, const unsigned char *id, int idlen,
3613 int *do_copy)
3614 {
3615 simple_ssl_session *sess;
3616 *do_copy = 0;
3617 for (sess = first; sess; sess = sess->next) {
3618 if (idlen == (int)sess->idlen && !memcmp(sess->id, id, idlen)) {
3619 const unsigned char *p = sess->der;
3620 BIO_printf(bio_err, "Lookup session: cache hit\n");
3621 return d2i_SSL_SESSION(NULL, &p, sess->derlen);
3622 }
3623 }
3624 BIO_printf(bio_err, "Lookup session: cache miss\n");
3625 return NULL;
3626 }
3627
3628 static void del_session(SSL_CTX *sctx, SSL_SESSION *session)
3629 {
3630 simple_ssl_session *sess, *prev = NULL;
3631 const unsigned char *id;
3632 unsigned int idlen;
3633 id = SSL_SESSION_get_id(session, &idlen);
3634 for (sess = first; sess; sess = sess->next) {
3635 if (idlen == sess->idlen && !memcmp(sess->id, id, idlen)) {
3636 if (prev)
3637 prev->next = sess->next;
3638 else
3639 first = sess->next;
3640 OPENSSL_free(sess->id);
3641 OPENSSL_free(sess->der);
3642 OPENSSL_free(sess);
3643 return;
3644 }
3645 prev = sess;
3646 }
3647 }
3648
3649 static void init_session_cache_ctx(SSL_CTX *sctx)
3650 {
3651 SSL_CTX_set_session_cache_mode(sctx,
3652 SSL_SESS_CACHE_NO_INTERNAL |
3653 SSL_SESS_CACHE_SERVER);
3654 SSL_CTX_sess_set_new_cb(sctx, add_session);
3655 SSL_CTX_sess_set_get_cb(sctx, get_session);
3656 SSL_CTX_sess_set_remove_cb(sctx, del_session);
3657 }
3658
3659 static void free_sessions(void)
3660 {
3661 simple_ssl_session *sess, *tsess;
3662 for (sess = first; sess;) {
3663 OPENSSL_free(sess->id);
3664 OPENSSL_free(sess->der);
3665 tsess = sess;
3666 sess = sess->next;
3667 OPENSSL_free(tsess);
3668 }
3669 first = NULL;
3670 }
3671
3672 #endif /* OPENSSL_NO_SOCK */