VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.3/apps/s_server.c@ 96081

最後變更 在這個檔案從96081是 95219,由 vboxsync 提交於 3 年 前

libs/openssl: Switched to v3.0.3, bugref:10128

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

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