VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.9/apps/s_server.c@ 100948

最後變更 在這個檔案從100948是 100487,由 vboxsync 提交於 21 月 前

openssl-3.0.9: Applied and adjusted our OpenSSL changes we made to 3.0.7. bugref:10484

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette