1e71b7053SJung-uk Kim /*
2*e0c4386eSCy Schubert * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
3e71b7053SJung-uk Kim * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
41f13597dSJung-uk Kim * Copyright 2005 Nokia. All rights reserved.
51f13597dSJung-uk Kim *
6b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
7e71b7053SJung-uk Kim * this file except in compliance with the License. You can obtain a copy
8e71b7053SJung-uk Kim * in the file LICENSE in the source distribution or at
9e71b7053SJung-uk Kim * https://www.openssl.org/source/license.html
101f13597dSJung-uk Kim */
113b4e3dcbSSimon L. B. Nielsen
121f13597dSJung-uk Kim #include <ctype.h>
1374664626SKris Kennaway #include <stdio.h>
1474664626SKris Kennaway #include <stdlib.h>
1574664626SKris Kennaway #include <string.h>
16e71b7053SJung-uk Kim #if defined(_WIN32)
17e71b7053SJung-uk Kim /* Included before async.h to avoid some warnings */
18e71b7053SJung-uk Kim # include <windows.h>
19e71b7053SJung-uk Kim #endif
203b4e3dcbSSimon L. B. Nielsen
215c87c606SMark Murray #include <openssl/e_os2.h>
22e71b7053SJung-uk Kim #include <openssl/async.h>
23e71b7053SJung-uk Kim #include <openssl/ssl.h>
24b077aed3SPierre Pronchery #include <openssl/decoder.h>
2574664626SKris Kennaway
26e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SOCK
273b4e3dcbSSimon L. B. Nielsen
286f9291ceSJung-uk Kim /*
296f9291ceSJung-uk Kim * With IPv6, it looks like Digital has mixed up the proper order of
306f9291ceSJung-uk Kim * recursive header file inclusion, resulting in the compiler complaining
316f9291ceSJung-uk Kim * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
326f9291ceSJung-uk Kim * needed to have fileno() declared correctly... So let's define u_int
336f9291ceSJung-uk Kim */
345c87c606SMark Murray #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
3574664626SKris Kennaway # define __U_INT
3674664626SKris Kennaway typedef unsigned int u_int;
3774664626SKris Kennaway #endif
3874664626SKris Kennaway
3974664626SKris Kennaway #include <openssl/bn.h>
4074664626SKris Kennaway #include "apps.h"
41e71b7053SJung-uk Kim #include "progs.h"
4274664626SKris Kennaway #include <openssl/err.h>
4374664626SKris Kennaway #include <openssl/pem.h>
4474664626SKris Kennaway #include <openssl/x509.h>
4574664626SKris Kennaway #include <openssl/ssl.h>
465740a5e3SKris Kennaway #include <openssl/rand.h>
47db522d3aSSimon L. B. Nielsen #include <openssl/ocsp.h>
483b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_DH
493b4e3dcbSSimon L. B. Nielsen # include <openssl/dh.h>
503b4e3dcbSSimon L. B. Nielsen #endif
513b4e3dcbSSimon L. B. Nielsen #include <openssl/rsa.h>
5274664626SKris Kennaway #include "s_apps.h"
533b4e3dcbSSimon L. B. Nielsen #include "timeouts.h"
54e71b7053SJung-uk Kim #ifdef CHARSET_EBCDIC
55e71b7053SJung-uk Kim #include <openssl/ebcdic.h>
5674664626SKris Kennaway #endif
57e71b7053SJung-uk Kim #include "internal/sockets.h"
5874664626SKris Kennaway
59e71b7053SJung-uk Kim static int not_resumable_sess_cb(SSL *s, int is_forward_secure);
60e71b7053SJung-uk Kim static int sv_body(int s, int stype, int prot, unsigned char *context);
61e71b7053SJung-uk Kim static int www_body(int s, int stype, int prot, unsigned char *context);
62e71b7053SJung-uk Kim static int rev_body(int s, int stype, int prot, unsigned char *context);
6374664626SKris Kennaway static void close_accept_socket(void);
6474664626SKris Kennaway static int init_ssl_connection(SSL *s);
6574664626SKris Kennaway static void print_stats(BIO *bp, SSL_CTX *ctx);
66e71b7053SJung-uk Kim static int generate_session_id(SSL *ssl, unsigned char *id,
675c87c606SMark Murray unsigned int *id_len);
687bded2dbSJung-uk Kim static void init_session_cache_ctx(SSL_CTX *sctx);
697bded2dbSJung-uk Kim static void free_sessions(void);
70e71b7053SJung-uk Kim static void print_connection_info(SSL *con);
713b4e3dcbSSimon L. B. Nielsen
72e71b7053SJung-uk Kim static const int bufsize = 16 * 1024;
7374664626SKris Kennaway static int accept_socket = -1;
7474664626SKris Kennaway
7574664626SKris Kennaway #define TEST_CERT "server.pem"
76db522d3aSSimon L. B. Nielsen #define TEST_CERT2 "server2.pem"
7774664626SKris Kennaway
7874664626SKris Kennaway static int s_nbio = 0;
7974664626SKris Kennaway static int s_nbio_test = 0;
80e71b7053SJung-uk Kim static int s_crlf = 0;
8174664626SKris Kennaway static SSL_CTX *ctx = NULL;
82db522d3aSSimon L. B. Nielsen static SSL_CTX *ctx2 = NULL;
8374664626SKris Kennaway static int www = 0;
8474664626SKris Kennaway
8574664626SKris Kennaway static BIO *bio_s_out = NULL;
867bded2dbSJung-uk Kim static BIO *bio_s_msg = NULL;
8774664626SKris Kennaway static int s_debug = 0;
88db522d3aSSimon L. B. Nielsen static int s_tlsextdebug = 0;
895c87c606SMark Murray static int s_msg = 0;
9074664626SKris Kennaway static int s_quiet = 0;
917bded2dbSJung-uk Kim static int s_ign_eof = 0;
927bded2dbSJung-uk Kim static int s_brief = 0;
9374664626SKris Kennaway
941f13597dSJung-uk Kim static char *keymatexportlabel = NULL;
951f13597dSJung-uk Kim static int keymatexportlen = 20;
961f13597dSJung-uk Kim
97e71b7053SJung-uk Kim static int async = 0;
98e71b7053SJung-uk Kim
99b077aed3SPierre Pronchery static int use_sendfile = 0;
100b077aed3SPierre Pronchery
1015c87c606SMark Murray static const char *session_id_prefix = NULL;
102f579bf8eSKris Kennaway
103e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS
1043b4e3dcbSSimon L. B. Nielsen static int enable_timeouts = 0;
1056a599222SSimon L. B. Nielsen static long socket_mtu;
1066a599222SSimon L. B. Nielsen #endif
1073b4e3dcbSSimon L. B. Nielsen
108e71b7053SJung-uk Kim /*
109e71b7053SJung-uk Kim * We define this but make it always be 0 in no-dtls builds to simplify the
110e71b7053SJung-uk Kim * code.
111e71b7053SJung-uk Kim */
112e71b7053SJung-uk Kim static int dtlslisten = 0;
113e71b7053SJung-uk Kim static int stateless = 0;
1147bded2dbSJung-uk Kim
115e71b7053SJung-uk Kim static int early_data = 0;
116e71b7053SJung-uk Kim static SSL_SESSION *psksess = NULL;
1177bded2dbSJung-uk Kim
1181f13597dSJung-uk Kim static char *psk_identity = "Client_identity";
1191f13597dSJung-uk Kim char *psk_key = NULL; /* by default PSK is not used */
1201f13597dSJung-uk Kim
121b077aed3SPierre Pronchery static char http_server_binmode = 0; /* for now: 0/1 = default/binary */
122b077aed3SPierre Pronchery
123e71b7053SJung-uk Kim #ifndef OPENSSL_NO_PSK
psk_server_cb(SSL * ssl,const char * identity,unsigned char * psk,unsigned int max_psk_len)1241f13597dSJung-uk Kim static unsigned int psk_server_cb(SSL *ssl, const char *identity,
1256f9291ceSJung-uk Kim unsigned char *psk,
1266f9291ceSJung-uk Kim unsigned int max_psk_len)
1271f13597dSJung-uk Kim {
128aeb5019cSJung-uk Kim long key_len = 0;
129aeb5019cSJung-uk Kim unsigned char *key;
1301f13597dSJung-uk Kim
1311f13597dSJung-uk Kim if (s_debug)
1321f13597dSJung-uk Kim BIO_printf(bio_s_out, "psk_server_cb\n");
1339a3ae0cdSJung-uk Kim
134b2bf0c7eSJung-uk Kim if (!SSL_is_dtls(ssl) && SSL_version(ssl) >= TLS1_3_VERSION) {
1359a3ae0cdSJung-uk Kim /*
136b2bf0c7eSJung-uk Kim * This callback is designed for use in (D)TLSv1.2 (or below). It is
137b2bf0c7eSJung-uk Kim * possible to use a single callback for all protocol versions - but it
138b2bf0c7eSJung-uk Kim * is preferred to use a dedicated callback for TLSv1.3. For TLSv1.3 we
139b2bf0c7eSJung-uk Kim * have psk_find_session_cb.
1409a3ae0cdSJung-uk Kim */
1419a3ae0cdSJung-uk Kim return 0;
1429a3ae0cdSJung-uk Kim }
1439a3ae0cdSJung-uk Kim
144e71b7053SJung-uk Kim if (identity == NULL) {
1451f13597dSJung-uk Kim BIO_printf(bio_err, "Error: client did not send PSK identity\n");
1461f13597dSJung-uk Kim goto out_err;
1471f13597dSJung-uk Kim }
1481f13597dSJung-uk Kim if (s_debug)
1491f13597dSJung-uk Kim BIO_printf(bio_s_out, "identity_len=%d identity=%s\n",
1506f9291ceSJung-uk Kim (int)strlen(identity), identity);
1511f13597dSJung-uk Kim
1521f13597dSJung-uk Kim /* here we could lookup the given identity e.g. from a database */
1536f9291ceSJung-uk Kim if (strcmp(identity, psk_identity) != 0) {
154e71b7053SJung-uk Kim BIO_printf(bio_s_out, "PSK warning: client identity not what we expected"
1556f9291ceSJung-uk Kim " (got '%s' expected '%s')\n", identity, psk_identity);
156e71b7053SJung-uk Kim } else {
1571f13597dSJung-uk Kim if (s_debug)
1581f13597dSJung-uk Kim BIO_printf(bio_s_out, "PSK client identity found\n");
159e71b7053SJung-uk Kim }
1601f13597dSJung-uk Kim
1611f13597dSJung-uk Kim /* convert the PSK key to binary */
162e71b7053SJung-uk Kim key = OPENSSL_hexstr2buf(psk_key, &key_len);
163aeb5019cSJung-uk Kim if (key == NULL) {
164aeb5019cSJung-uk Kim BIO_printf(bio_err, "Could not convert PSK key '%s' to buffer\n",
1656f9291ceSJung-uk Kim psk_key);
1661f13597dSJung-uk Kim return 0;
1671f13597dSJung-uk Kim }
168aeb5019cSJung-uk Kim if (key_len > (int)max_psk_len) {
1696f9291ceSJung-uk Kim BIO_printf(bio_err,
170aeb5019cSJung-uk Kim "psk buffer of callback is too small (%d) for key (%ld)\n",
171aeb5019cSJung-uk Kim max_psk_len, key_len);
172aeb5019cSJung-uk Kim OPENSSL_free(key);
1731f13597dSJung-uk Kim return 0;
1741f13597dSJung-uk Kim }
1751f13597dSJung-uk Kim
176aeb5019cSJung-uk Kim memcpy(psk, key, key_len);
177aeb5019cSJung-uk Kim OPENSSL_free(key);
1781f13597dSJung-uk Kim
1791f13597dSJung-uk Kim if (s_debug)
180aeb5019cSJung-uk Kim BIO_printf(bio_s_out, "fetched PSK len=%ld\n", key_len);
181aeb5019cSJung-uk Kim return key_len;
1821f13597dSJung-uk Kim out_err:
1831f13597dSJung-uk Kim if (s_debug)
1841f13597dSJung-uk Kim BIO_printf(bio_err, "Error in PSK server callback\n");
185e71b7053SJung-uk Kim (void)BIO_flush(bio_err);
186e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out);
1871f13597dSJung-uk Kim return 0;
1881f13597dSJung-uk Kim }
1891f13597dSJung-uk Kim #endif
1901f13597dSJung-uk Kim
psk_find_session_cb(SSL * ssl,const unsigned char * identity,size_t identity_len,SSL_SESSION ** sess)191e71b7053SJung-uk Kim static int psk_find_session_cb(SSL *ssl, const unsigned char *identity,
192e71b7053SJung-uk Kim size_t identity_len, SSL_SESSION **sess)
193e71b7053SJung-uk Kim {
194e71b7053SJung-uk Kim SSL_SESSION *tmpsess = NULL;
195e71b7053SJung-uk Kim unsigned char *key;
196e71b7053SJung-uk Kim long key_len;
197e71b7053SJung-uk Kim const SSL_CIPHER *cipher = NULL;
198e71b7053SJung-uk Kim
199e71b7053SJung-uk Kim if (strlen(psk_identity) != identity_len
200e71b7053SJung-uk Kim || memcmp(psk_identity, identity, identity_len) != 0) {
201c9cf7b5cSJung-uk Kim *sess = NULL;
202c9cf7b5cSJung-uk Kim return 1;
203e71b7053SJung-uk Kim }
204e71b7053SJung-uk Kim
205e71b7053SJung-uk Kim if (psksess != NULL) {
206e71b7053SJung-uk Kim SSL_SESSION_up_ref(psksess);
207e71b7053SJung-uk Kim *sess = psksess;
208e71b7053SJung-uk Kim return 1;
209e71b7053SJung-uk Kim }
210e71b7053SJung-uk Kim
211e71b7053SJung-uk Kim key = OPENSSL_hexstr2buf(psk_key, &key_len);
212e71b7053SJung-uk Kim if (key == NULL) {
213e71b7053SJung-uk Kim BIO_printf(bio_err, "Could not convert PSK key '%s' to buffer\n",
214e71b7053SJung-uk Kim psk_key);
215e71b7053SJung-uk Kim return 0;
216e71b7053SJung-uk Kim }
217e71b7053SJung-uk Kim
218e71b7053SJung-uk Kim /* We default to SHA256 */
219e71b7053SJung-uk Kim cipher = SSL_CIPHER_find(ssl, tls13_aes128gcmsha256_id);
220e71b7053SJung-uk Kim if (cipher == NULL) {
221e71b7053SJung-uk Kim BIO_printf(bio_err, "Error finding suitable ciphersuite\n");
222e71b7053SJung-uk Kim OPENSSL_free(key);
223e71b7053SJung-uk Kim return 0;
224e71b7053SJung-uk Kim }
225e71b7053SJung-uk Kim
226e71b7053SJung-uk Kim tmpsess = SSL_SESSION_new();
227e71b7053SJung-uk Kim if (tmpsess == NULL
228e71b7053SJung-uk Kim || !SSL_SESSION_set1_master_key(tmpsess, key, key_len)
229e71b7053SJung-uk Kim || !SSL_SESSION_set_cipher(tmpsess, cipher)
230e71b7053SJung-uk Kim || !SSL_SESSION_set_protocol_version(tmpsess, SSL_version(ssl))) {
231e71b7053SJung-uk Kim OPENSSL_free(key);
232b077aed3SPierre Pronchery SSL_SESSION_free(tmpsess);
233e71b7053SJung-uk Kim return 0;
234e71b7053SJung-uk Kim }
235e71b7053SJung-uk Kim OPENSSL_free(key);
236e71b7053SJung-uk Kim *sess = tmpsess;
237e71b7053SJung-uk Kim
238e71b7053SJung-uk Kim return 1;
239e71b7053SJung-uk Kim }
240e71b7053SJung-uk Kim
2411f13597dSJung-uk Kim #ifndef OPENSSL_NO_SRP
242e71b7053SJung-uk Kim static srpsrvparm srp_callback_parm;
2431f13597dSJung-uk Kim #endif
2441f13597dSJung-uk Kim
24574664626SKris Kennaway static int local_argc = 0;
24674664626SKris Kennaway static char **local_argv;
24774664626SKris Kennaway
24874664626SKris Kennaway #ifdef CHARSET_EBCDIC
24974664626SKris Kennaway static int ebcdic_new(BIO *bi);
25074664626SKris Kennaway static int ebcdic_free(BIO *a);
25174664626SKris Kennaway static int ebcdic_read(BIO *b, char *out, int outl);
2525c87c606SMark Murray static int ebcdic_write(BIO *b, const char *in, int inl);
2535c87c606SMark Murray static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr);
25474664626SKris Kennaway static int ebcdic_gets(BIO *bp, char *buf, int size);
2555c87c606SMark Murray static int ebcdic_puts(BIO *bp, const char *str);
25674664626SKris Kennaway
25774664626SKris Kennaway # define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
258e71b7053SJung-uk Kim static BIO_METHOD *methods_ebcdic = NULL;
25974664626SKris Kennaway
260e71b7053SJung-uk Kim /* This struct is "unwarranted chumminess with the compiler." */
2616f9291ceSJung-uk Kim typedef struct {
26274664626SKris Kennaway size_t alloced;
26374664626SKris Kennaway char buff[1];
26474664626SKris Kennaway } EBCDIC_OUTBUFF;
26574664626SKris Kennaway
BIO_f_ebcdic_filter()266e71b7053SJung-uk Kim static const BIO_METHOD *BIO_f_ebcdic_filter()
26774664626SKris Kennaway {
268e71b7053SJung-uk Kim if (methods_ebcdic == NULL) {
269e71b7053SJung-uk Kim methods_ebcdic = BIO_meth_new(BIO_TYPE_EBCDIC_FILTER,
270e71b7053SJung-uk Kim "EBCDIC/ASCII filter");
271e71b7053SJung-uk Kim if (methods_ebcdic == NULL
272e71b7053SJung-uk Kim || !BIO_meth_set_write(methods_ebcdic, ebcdic_write)
273e71b7053SJung-uk Kim || !BIO_meth_set_read(methods_ebcdic, ebcdic_read)
274e71b7053SJung-uk Kim || !BIO_meth_set_puts(methods_ebcdic, ebcdic_puts)
275e71b7053SJung-uk Kim || !BIO_meth_set_gets(methods_ebcdic, ebcdic_gets)
276e71b7053SJung-uk Kim || !BIO_meth_set_ctrl(methods_ebcdic, ebcdic_ctrl)
277e71b7053SJung-uk Kim || !BIO_meth_set_create(methods_ebcdic, ebcdic_new)
278e71b7053SJung-uk Kim || !BIO_meth_set_destroy(methods_ebcdic, ebcdic_free))
279e71b7053SJung-uk Kim return NULL;
280e71b7053SJung-uk Kim }
281e71b7053SJung-uk Kim return methods_ebcdic;
28274664626SKris Kennaway }
28374664626SKris Kennaway
ebcdic_new(BIO * bi)28474664626SKris Kennaway static int ebcdic_new(BIO *bi)
28574664626SKris Kennaway {
28674664626SKris Kennaway EBCDIC_OUTBUFF *wbuf;
28774664626SKris Kennaway
288e71b7053SJung-uk Kim wbuf = app_malloc(sizeof(*wbuf) + 1024, "ebcdic wbuf");
28974664626SKris Kennaway wbuf->alloced = 1024;
29074664626SKris Kennaway wbuf->buff[0] = '\0';
29174664626SKris Kennaway
292e71b7053SJung-uk Kim BIO_set_data(bi, wbuf);
293e71b7053SJung-uk Kim BIO_set_init(bi, 1);
294e71b7053SJung-uk Kim return 1;
29574664626SKris Kennaway }
29674664626SKris Kennaway
ebcdic_free(BIO * a)29774664626SKris Kennaway static int ebcdic_free(BIO *a)
29874664626SKris Kennaway {
299e71b7053SJung-uk Kim EBCDIC_OUTBUFF *wbuf;
300e71b7053SJung-uk Kim
3016f9291ceSJung-uk Kim if (a == NULL)
302e71b7053SJung-uk Kim return 0;
303e71b7053SJung-uk Kim wbuf = BIO_get_data(a);
304e71b7053SJung-uk Kim OPENSSL_free(wbuf);
305e71b7053SJung-uk Kim BIO_set_data(a, NULL);
306e71b7053SJung-uk Kim BIO_set_init(a, 0);
307e71b7053SJung-uk Kim
308e71b7053SJung-uk Kim return 1;
30974664626SKris Kennaway }
31074664626SKris Kennaway
ebcdic_read(BIO * b,char * out,int outl)31174664626SKris Kennaway static int ebcdic_read(BIO *b, char *out, int outl)
31274664626SKris Kennaway {
31374664626SKris Kennaway int ret = 0;
314e71b7053SJung-uk Kim BIO *next = BIO_next(b);
31574664626SKris Kennaway
3166f9291ceSJung-uk Kim if (out == NULL || outl == 0)
317e71b7053SJung-uk Kim return 0;
318e71b7053SJung-uk Kim if (next == NULL)
319e71b7053SJung-uk Kim return 0;
32074664626SKris Kennaway
321e71b7053SJung-uk Kim ret = BIO_read(next, out, outl);
32274664626SKris Kennaway if (ret > 0)
32374664626SKris Kennaway ascii2ebcdic(out, out, ret);
324e71b7053SJung-uk Kim return ret;
32574664626SKris Kennaway }
32674664626SKris Kennaway
ebcdic_write(BIO * b,const char * in,int inl)3275c87c606SMark Murray static int ebcdic_write(BIO *b, const char *in, int inl)
32874664626SKris Kennaway {
32974664626SKris Kennaway EBCDIC_OUTBUFF *wbuf;
330e71b7053SJung-uk Kim BIO *next = BIO_next(b);
33174664626SKris Kennaway int ret = 0;
33274664626SKris Kennaway int num;
33374664626SKris Kennaway
3346f9291ceSJung-uk Kim if ((in == NULL) || (inl <= 0))
335e71b7053SJung-uk Kim return 0;
336e71b7053SJung-uk Kim if (next == NULL)
337e71b7053SJung-uk Kim return 0;
33874664626SKris Kennaway
339e71b7053SJung-uk Kim wbuf = (EBCDIC_OUTBUFF *) BIO_get_data(b);
34074664626SKris Kennaway
3416f9291ceSJung-uk Kim if (inl > (num = wbuf->alloced)) {
34274664626SKris Kennaway num = num + num; /* double the size */
34374664626SKris Kennaway if (num < inl)
34474664626SKris Kennaway num = inl;
345e71b7053SJung-uk Kim OPENSSL_free(wbuf);
346e71b7053SJung-uk Kim wbuf = app_malloc(sizeof(*wbuf) + num, "grow ebcdic wbuf");
34774664626SKris Kennaway
34874664626SKris Kennaway wbuf->alloced = num;
34974664626SKris Kennaway wbuf->buff[0] = '\0';
35074664626SKris Kennaway
351e71b7053SJung-uk Kim BIO_set_data(b, wbuf);
35274664626SKris Kennaway }
35374664626SKris Kennaway
35474664626SKris Kennaway ebcdic2ascii(wbuf->buff, in, inl);
35574664626SKris Kennaway
356e71b7053SJung-uk Kim ret = BIO_write(next, wbuf->buff, inl);
35774664626SKris Kennaway
358e71b7053SJung-uk Kim return ret;
35974664626SKris Kennaway }
36074664626SKris Kennaway
ebcdic_ctrl(BIO * b,int cmd,long num,void * ptr)3615c87c606SMark Murray static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr)
36274664626SKris Kennaway {
36374664626SKris Kennaway long ret;
364e71b7053SJung-uk Kim BIO *next = BIO_next(b);
36574664626SKris Kennaway
366e71b7053SJung-uk Kim if (next == NULL)
367e71b7053SJung-uk Kim return 0;
3686f9291ceSJung-uk Kim switch (cmd) {
36974664626SKris Kennaway case BIO_CTRL_DUP:
37074664626SKris Kennaway ret = 0L;
37174664626SKris Kennaway break;
37274664626SKris Kennaway default:
373e71b7053SJung-uk Kim ret = BIO_ctrl(next, cmd, num, ptr);
37474664626SKris Kennaway break;
37574664626SKris Kennaway }
376e71b7053SJung-uk Kim return ret;
37774664626SKris Kennaway }
37874664626SKris Kennaway
ebcdic_gets(BIO * bp,char * buf,int size)37974664626SKris Kennaway static int ebcdic_gets(BIO *bp, char *buf, int size)
38074664626SKris Kennaway {
3815c87c606SMark Murray int i, ret = 0;
382e71b7053SJung-uk Kim BIO *next = BIO_next(bp);
383e71b7053SJung-uk Kim
384e71b7053SJung-uk Kim if (next == NULL)
385e71b7053SJung-uk Kim return 0;
38674664626SKris Kennaway /* return(BIO_gets(bp->next_bio,buf,size));*/
3876f9291ceSJung-uk Kim for (i = 0; i < size - 1; ++i) {
38874664626SKris Kennaway ret = ebcdic_read(bp, &buf[i], 1);
38974664626SKris Kennaway if (ret <= 0)
39074664626SKris Kennaway break;
3916f9291ceSJung-uk Kim else if (buf[i] == '\n') {
39274664626SKris Kennaway ++i;
39374664626SKris Kennaway break;
39474664626SKris Kennaway }
39574664626SKris Kennaway }
39674664626SKris Kennaway if (i < size)
39774664626SKris Kennaway buf[i] = '\0';
39874664626SKris Kennaway return (ret < 0 && i == 0) ? ret : i;
39974664626SKris Kennaway }
40074664626SKris Kennaway
ebcdic_puts(BIO * bp,const char * str)4015c87c606SMark Murray static int ebcdic_puts(BIO *bp, const char *str)
40274664626SKris Kennaway {
403e71b7053SJung-uk Kim if (BIO_next(bp) == NULL)
404e71b7053SJung-uk Kim return 0;
40574664626SKris Kennaway return ebcdic_write(bp, str, strlen(str));
40674664626SKris Kennaway }
40774664626SKris Kennaway #endif
40874664626SKris Kennaway
409db522d3aSSimon L. B. Nielsen /* This is a context that we pass to callbacks */
410db522d3aSSimon L. B. Nielsen typedef struct tlsextctx_st {
411db522d3aSSimon L. B. Nielsen char *servername;
412db522d3aSSimon L. B. Nielsen BIO *biodebug;
413db522d3aSSimon L. B. Nielsen int extension_error;
414db522d3aSSimon L. B. Nielsen } tlsextctx;
415db522d3aSSimon L. B. Nielsen
ssl_servername_cb(SSL * s,int * ad,void * arg)416e71b7053SJung-uk Kim static int ssl_servername_cb(SSL *s, int *ad, void *arg)
417db522d3aSSimon L. B. Nielsen {
418db522d3aSSimon L. B. Nielsen tlsextctx *p = (tlsextctx *) arg;
419db522d3aSSimon L. B. Nielsen const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
420db522d3aSSimon L. B. Nielsen
421e71b7053SJung-uk Kim if (servername != NULL && p->biodebug != NULL) {
422e71b7053SJung-uk Kim const char *cp = servername;
423e71b7053SJung-uk Kim unsigned char uc;
424e71b7053SJung-uk Kim
425e71b7053SJung-uk Kim BIO_printf(p->biodebug, "Hostname in TLS extension: \"");
426e71b7053SJung-uk Kim while ((uc = *cp++) != 0)
427e71b7053SJung-uk Kim BIO_printf(p->biodebug,
428b077aed3SPierre Pronchery (((uc) & ~127) == 0) && isprint(uc) ? "%c" : "\\x%02x", uc);
429e71b7053SJung-uk Kim BIO_printf(p->biodebug, "\"\n");
430e71b7053SJung-uk Kim }
431e71b7053SJung-uk Kim
432e71b7053SJung-uk Kim if (p->servername == NULL)
433db522d3aSSimon L. B. Nielsen return SSL_TLSEXT_ERR_NOACK;
434db522d3aSSimon L. B. Nielsen
435e71b7053SJung-uk Kim if (servername != NULL) {
436b077aed3SPierre Pronchery if (OPENSSL_strcasecmp(servername, p->servername))
437db522d3aSSimon L. B. Nielsen return p->extension_error;
438e71b7053SJung-uk Kim if (ctx2 != NULL) {
4391f13597dSJung-uk Kim BIO_printf(p->biodebug, "Switching server context.\n");
440db522d3aSSimon L. B. Nielsen SSL_set_SSL_CTX(s, ctx2);
441db522d3aSSimon L. B. Nielsen }
442db522d3aSSimon L. B. Nielsen }
443db522d3aSSimon L. B. Nielsen return SSL_TLSEXT_ERR_OK;
444db522d3aSSimon L. B. Nielsen }
445db522d3aSSimon L. B. Nielsen
446db522d3aSSimon L. B. Nielsen /* Structure passed to cert status callback */
447db522d3aSSimon L. B. Nielsen typedef struct tlsextstatusctx_st {
448e71b7053SJung-uk Kim int timeout;
449e71b7053SJung-uk Kim /* File to load OCSP Response from (or NULL if no file) */
450e71b7053SJung-uk Kim char *respin;
451db522d3aSSimon L. B. Nielsen /* Default responder to use */
452db522d3aSSimon L. B. Nielsen char *host, *path, *port;
453b077aed3SPierre Pronchery char *proxy, *no_proxy;
454db522d3aSSimon L. B. Nielsen int use_ssl;
455db522d3aSSimon L. B. Nielsen int verbose;
456db522d3aSSimon L. B. Nielsen } tlsextstatusctx;
457db522d3aSSimon L. B. Nielsen
458e71b7053SJung-uk Kim static tlsextstatusctx tlscstatp = { -1 };
459e71b7053SJung-uk Kim
460e71b7053SJung-uk Kim #ifndef OPENSSL_NO_OCSP
461db522d3aSSimon L. B. Nielsen
4626f9291ceSJung-uk Kim /*
463e71b7053SJung-uk Kim * Helper function to get an OCSP_RESPONSE from a responder. This is a
464e71b7053SJung-uk Kim * simplified version. It examines certificates each time and makes one OCSP
465e71b7053SJung-uk Kim * responder query for each request. A full version would store details such as
466e71b7053SJung-uk Kim * the OCSP certificate IDs and minimise the number of OCSP responses by caching
467e71b7053SJung-uk Kim * them until they were considered "expired".
468db522d3aSSimon L. B. Nielsen */
get_ocsp_resp_from_responder(SSL * s,tlsextstatusctx * srctx,OCSP_RESPONSE ** resp)469e71b7053SJung-uk Kim static int get_ocsp_resp_from_responder(SSL *s, tlsextstatusctx *srctx,
470e71b7053SJung-uk Kim OCSP_RESPONSE **resp)
471db522d3aSSimon L. B. Nielsen {
472e71b7053SJung-uk Kim char *host = NULL, *port = NULL, *path = NULL;
473b077aed3SPierre Pronchery char *proxy = NULL, *no_proxy = NULL;
474db522d3aSSimon L. B. Nielsen int use_ssl;
4751f13597dSJung-uk Kim STACK_OF(OPENSSL_STRING) *aia = NULL;
476db522d3aSSimon L. B. Nielsen X509 *x = NULL;
477e71b7053SJung-uk Kim X509_STORE_CTX *inctx = NULL;
478e71b7053SJung-uk Kim X509_OBJECT *obj;
479db522d3aSSimon L. B. Nielsen OCSP_REQUEST *req = NULL;
480db522d3aSSimon L. B. Nielsen OCSP_CERTID *id = NULL;
481db522d3aSSimon L. B. Nielsen STACK_OF(X509_EXTENSION) *exts;
482db522d3aSSimon L. B. Nielsen int ret = SSL_TLSEXT_ERR_NOACK;
483db522d3aSSimon L. B. Nielsen int i;
484e71b7053SJung-uk Kim
485db522d3aSSimon L. B. Nielsen /* Build up OCSP query from server certificate */
486db522d3aSSimon L. B. Nielsen x = SSL_get_certificate(s);
487db522d3aSSimon L. B. Nielsen aia = X509_get1_ocsp(x);
488e71b7053SJung-uk Kim if (aia != NULL) {
489b077aed3SPierre Pronchery if (!OSSL_HTTP_parse_url(sk_OPENSSL_STRING_value(aia, 0), &use_ssl,
490b077aed3SPierre Pronchery NULL, &host, &port, NULL, &path, NULL, NULL)) {
491e71b7053SJung-uk Kim BIO_puts(bio_err, "cert_status: can't parse AIA URL\n");
492db522d3aSSimon L. B. Nielsen goto err;
493db522d3aSSimon L. B. Nielsen }
494db522d3aSSimon L. B. Nielsen if (srctx->verbose)
495e71b7053SJung-uk Kim BIO_printf(bio_err, "cert_status: AIA URL: %s\n",
4961f13597dSJung-uk Kim sk_OPENSSL_STRING_value(aia, 0));
4976f9291ceSJung-uk Kim } else {
498e71b7053SJung-uk Kim if (srctx->host == NULL) {
499e71b7053SJung-uk Kim BIO_puts(bio_err,
5006f9291ceSJung-uk Kim "cert_status: no AIA and no default responder URL\n");
501db522d3aSSimon L. B. Nielsen goto done;
502db522d3aSSimon L. B. Nielsen }
503db522d3aSSimon L. B. Nielsen host = srctx->host;
504db522d3aSSimon L. B. Nielsen path = srctx->path;
505db522d3aSSimon L. B. Nielsen port = srctx->port;
506db522d3aSSimon L. B. Nielsen use_ssl = srctx->use_ssl;
507db522d3aSSimon L. B. Nielsen }
508b077aed3SPierre Pronchery proxy = srctx->proxy;
509b077aed3SPierre Pronchery no_proxy = srctx->no_proxy;
510db522d3aSSimon L. B. Nielsen
511e71b7053SJung-uk Kim inctx = X509_STORE_CTX_new();
512e71b7053SJung-uk Kim if (inctx == NULL)
513e71b7053SJung-uk Kim goto err;
514e71b7053SJung-uk Kim if (!X509_STORE_CTX_init(inctx,
515db522d3aSSimon L. B. Nielsen SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)),
516db522d3aSSimon L. B. Nielsen NULL, NULL))
517db522d3aSSimon L. B. Nielsen goto err;
518e71b7053SJung-uk Kim obj = X509_STORE_CTX_get_obj_by_subject(inctx, X509_LU_X509,
519e71b7053SJung-uk Kim X509_get_issuer_name(x));
520e71b7053SJung-uk Kim if (obj == NULL) {
521e71b7053SJung-uk Kim BIO_puts(bio_err, "cert_status: Can't retrieve issuer certificate.\n");
522db522d3aSSimon L. B. Nielsen goto done;
523db522d3aSSimon L. B. Nielsen }
524e71b7053SJung-uk Kim id = OCSP_cert_to_id(NULL, x, X509_OBJECT_get0_X509(obj));
525e71b7053SJung-uk Kim X509_OBJECT_free(obj);
526e71b7053SJung-uk Kim if (id == NULL)
527db522d3aSSimon L. B. Nielsen goto err;
528e71b7053SJung-uk Kim req = OCSP_REQUEST_new();
529e71b7053SJung-uk Kim if (req == NULL)
530db522d3aSSimon L. B. Nielsen goto err;
531db522d3aSSimon L. B. Nielsen if (!OCSP_request_add0_id(req, id))
532db522d3aSSimon L. B. Nielsen goto err;
533db522d3aSSimon L. B. Nielsen id = NULL;
534db522d3aSSimon L. B. Nielsen /* Add any extensions to the request */
535db522d3aSSimon L. B. Nielsen SSL_get_tlsext_status_exts(s, &exts);
5366f9291ceSJung-uk Kim for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
537db522d3aSSimon L. B. Nielsen X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
538db522d3aSSimon L. B. Nielsen if (!OCSP_REQUEST_add_ext(req, ext, -1))
539db522d3aSSimon L. B. Nielsen goto err;
540db522d3aSSimon L. B. Nielsen }
541b077aed3SPierre Pronchery *resp = process_responder(req, host, port, path, proxy, no_proxy,
542b077aed3SPierre Pronchery use_ssl, NULL /* headers */, srctx->timeout);
543e71b7053SJung-uk Kim if (*resp == NULL) {
544e71b7053SJung-uk Kim BIO_puts(bio_err, "cert_status: error querying responder\n");
545db522d3aSSimon L. B. Nielsen goto done;
546db522d3aSSimon L. B. Nielsen }
547e71b7053SJung-uk Kim
548db522d3aSSimon L. B. Nielsen ret = SSL_TLSEXT_ERR_OK;
549e71b7053SJung-uk Kim goto done;
550e71b7053SJung-uk Kim
551e71b7053SJung-uk Kim err:
552e71b7053SJung-uk Kim ret = SSL_TLSEXT_ERR_ALERT_FATAL;
553db522d3aSSimon L. B. Nielsen done:
554e71b7053SJung-uk Kim /*
555e71b7053SJung-uk Kim * If we parsed aia we need to free; otherwise they were copied and we
556e71b7053SJung-uk Kim * don't
557e71b7053SJung-uk Kim */
558e71b7053SJung-uk Kim if (aia != NULL) {
559db522d3aSSimon L. B. Nielsen OPENSSL_free(host);
560db522d3aSSimon L. B. Nielsen OPENSSL_free(path);
561db522d3aSSimon L. B. Nielsen OPENSSL_free(port);
562db522d3aSSimon L. B. Nielsen X509_email_free(aia);
563db522d3aSSimon L. B. Nielsen }
564db522d3aSSimon L. B. Nielsen OCSP_CERTID_free(id);
565db522d3aSSimon L. B. Nielsen OCSP_REQUEST_free(req);
566e71b7053SJung-uk Kim X509_STORE_CTX_free(inctx);
567db522d3aSSimon L. B. Nielsen return ret;
568db522d3aSSimon L. B. Nielsen }
5691f13597dSJung-uk Kim
570e71b7053SJung-uk Kim /*
571e71b7053SJung-uk Kim * Certificate Status callback. This is called when a client includes a
572e71b7053SJung-uk Kim * certificate status request extension. The response is either obtained from a
573e71b7053SJung-uk Kim * file, or from an OCSP responder.
574e71b7053SJung-uk Kim */
cert_status_cb(SSL * s,void * arg)575e71b7053SJung-uk Kim static int cert_status_cb(SSL *s, void *arg)
576e71b7053SJung-uk Kim {
577e71b7053SJung-uk Kim tlsextstatusctx *srctx = arg;
578e71b7053SJung-uk Kim OCSP_RESPONSE *resp = NULL;
579e71b7053SJung-uk Kim unsigned char *rspder = NULL;
580e71b7053SJung-uk Kim int rspderlen;
581e71b7053SJung-uk Kim int ret = SSL_TLSEXT_ERR_ALERT_FATAL;
582e71b7053SJung-uk Kim
583e71b7053SJung-uk Kim if (srctx->verbose)
584e71b7053SJung-uk Kim BIO_puts(bio_err, "cert_status: callback called\n");
585e71b7053SJung-uk Kim
586e71b7053SJung-uk Kim if (srctx->respin != NULL) {
587e71b7053SJung-uk Kim BIO *derbio = bio_open_default(srctx->respin, 'r', FORMAT_ASN1);
588e71b7053SJung-uk Kim if (derbio == NULL) {
589e71b7053SJung-uk Kim BIO_puts(bio_err, "cert_status: Cannot open OCSP response file\n");
590e71b7053SJung-uk Kim goto err;
591e71b7053SJung-uk Kim }
592e71b7053SJung-uk Kim resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
593e71b7053SJung-uk Kim BIO_free(derbio);
594e71b7053SJung-uk Kim if (resp == NULL) {
595e71b7053SJung-uk Kim BIO_puts(bio_err, "cert_status: Error reading OCSP response\n");
596e71b7053SJung-uk Kim goto err;
597e71b7053SJung-uk Kim }
598e71b7053SJung-uk Kim } else {
599e71b7053SJung-uk Kim ret = get_ocsp_resp_from_responder(s, srctx, &resp);
600e71b7053SJung-uk Kim if (ret != SSL_TLSEXT_ERR_OK)
601e71b7053SJung-uk Kim goto err;
602e71b7053SJung-uk Kim }
603e71b7053SJung-uk Kim
604e71b7053SJung-uk Kim rspderlen = i2d_OCSP_RESPONSE(resp, &rspder);
605e71b7053SJung-uk Kim if (rspderlen <= 0)
606e71b7053SJung-uk Kim goto err;
607e71b7053SJung-uk Kim
608e71b7053SJung-uk Kim SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen);
609e71b7053SJung-uk Kim if (srctx->verbose) {
610e71b7053SJung-uk Kim BIO_puts(bio_err, "cert_status: ocsp response sent:\n");
611e71b7053SJung-uk Kim OCSP_RESPONSE_print(bio_err, resp, 2);
612e71b7053SJung-uk Kim }
613e71b7053SJung-uk Kim
614e71b7053SJung-uk Kim ret = SSL_TLSEXT_ERR_OK;
615e71b7053SJung-uk Kim
616e71b7053SJung-uk Kim err:
617e71b7053SJung-uk Kim if (ret != SSL_TLSEXT_ERR_OK)
618e71b7053SJung-uk Kim ERR_print_errors(bio_err);
619e71b7053SJung-uk Kim
620e71b7053SJung-uk Kim OCSP_RESPONSE_free(resp);
621e71b7053SJung-uk Kim
622e71b7053SJung-uk Kim return ret;
623e71b7053SJung-uk Kim }
624e71b7053SJung-uk Kim #endif
625e71b7053SJung-uk Kim
6261f13597dSJung-uk Kim #ifndef OPENSSL_NO_NEXTPROTONEG
6271f13597dSJung-uk Kim /* This is the context that we pass to next_proto_cb */
6281f13597dSJung-uk Kim typedef struct tlsextnextprotoctx_st {
6291f13597dSJung-uk Kim unsigned char *data;
630e71b7053SJung-uk Kim size_t len;
6311f13597dSJung-uk Kim } tlsextnextprotoctx;
6321f13597dSJung-uk Kim
next_proto_cb(SSL * s,const unsigned char ** data,unsigned int * len,void * arg)6336f9291ceSJung-uk Kim static int next_proto_cb(SSL *s, const unsigned char **data,
6346f9291ceSJung-uk Kim unsigned int *len, void *arg)
6351f13597dSJung-uk Kim {
6361f13597dSJung-uk Kim tlsextnextprotoctx *next_proto = arg;
6371f13597dSJung-uk Kim
6381f13597dSJung-uk Kim *data = next_proto->data;
6391f13597dSJung-uk Kim *len = next_proto->len;
6401f13597dSJung-uk Kim
6411f13597dSJung-uk Kim return SSL_TLSEXT_ERR_OK;
6421f13597dSJung-uk Kim }
6431f13597dSJung-uk Kim #endif /* ndef OPENSSL_NO_NEXTPROTONEG */
6441f13597dSJung-uk Kim
6457bded2dbSJung-uk Kim /* This the context that we pass to alpn_cb */
6467bded2dbSJung-uk Kim typedef struct tlsextalpnctx_st {
6477bded2dbSJung-uk Kim unsigned char *data;
648e71b7053SJung-uk Kim size_t len;
6497bded2dbSJung-uk Kim } tlsextalpnctx;
6507bded2dbSJung-uk Kim
alpn_cb(SSL * s,const unsigned char ** out,unsigned char * outlen,const unsigned char * in,unsigned int inlen,void * arg)6517bded2dbSJung-uk Kim static int alpn_cb(SSL *s, const unsigned char **out, unsigned char *outlen,
6527bded2dbSJung-uk Kim const unsigned char *in, unsigned int inlen, void *arg)
6537bded2dbSJung-uk Kim {
6547bded2dbSJung-uk Kim tlsextalpnctx *alpn_ctx = arg;
6557bded2dbSJung-uk Kim
6567bded2dbSJung-uk Kim if (!s_quiet) {
6577bded2dbSJung-uk Kim /* We can assume that |in| is syntactically valid. */
658e71b7053SJung-uk Kim unsigned int i;
6597bded2dbSJung-uk Kim BIO_printf(bio_s_out, "ALPN protocols advertised by the client: ");
6607bded2dbSJung-uk Kim for (i = 0; i < inlen;) {
6617bded2dbSJung-uk Kim if (i)
6627bded2dbSJung-uk Kim BIO_write(bio_s_out, ", ", 2);
6637bded2dbSJung-uk Kim BIO_write(bio_s_out, &in[i + 1], in[i]);
6647bded2dbSJung-uk Kim i += in[i] + 1;
6657bded2dbSJung-uk Kim }
6667bded2dbSJung-uk Kim BIO_write(bio_s_out, "\n", 1);
6677bded2dbSJung-uk Kim }
6687bded2dbSJung-uk Kim
6697bded2dbSJung-uk Kim if (SSL_select_next_proto
6707bded2dbSJung-uk Kim ((unsigned char **)out, outlen, alpn_ctx->data, alpn_ctx->len, in,
6717bded2dbSJung-uk Kim inlen) != OPENSSL_NPN_NEGOTIATED) {
672b077aed3SPierre Pronchery return SSL_TLSEXT_ERR_ALERT_FATAL;
6737bded2dbSJung-uk Kim }
6747bded2dbSJung-uk Kim
6757bded2dbSJung-uk Kim if (!s_quiet) {
6767bded2dbSJung-uk Kim BIO_printf(bio_s_out, "ALPN protocols selected: ");
6777bded2dbSJung-uk Kim BIO_write(bio_s_out, *out, *outlen);
6787bded2dbSJung-uk Kim BIO_write(bio_s_out, "\n", 1);
6797bded2dbSJung-uk Kim }
6807bded2dbSJung-uk Kim
6817bded2dbSJung-uk Kim return SSL_TLSEXT_ERR_OK;
6827bded2dbSJung-uk Kim }
6831f13597dSJung-uk Kim
not_resumable_sess_cb(SSL * s,int is_forward_secure)684e71b7053SJung-uk Kim static int not_resumable_sess_cb(SSL *s, int is_forward_secure)
685e71b7053SJung-uk Kim {
686e71b7053SJung-uk Kim /* disable resumption for sessions with forward secure ciphers */
687e71b7053SJung-uk Kim return is_forward_secure;
688e71b7053SJung-uk Kim }
689f579bf8eSKris Kennaway
690e71b7053SJung-uk Kim typedef enum OPTION_choice {
691b077aed3SPierre Pronchery OPT_COMMON,
692b077aed3SPierre Pronchery OPT_ENGINE,
693e71b7053SJung-uk Kim OPT_4, OPT_6, OPT_ACCEPT, OPT_PORT, OPT_UNIX, OPT_UNLINK, OPT_NACCEPT,
694e71b7053SJung-uk Kim OPT_VERIFY, OPT_NAMEOPT, OPT_UPPER_V_VERIFY, OPT_CONTEXT, OPT_CERT, OPT_CRL,
695e71b7053SJung-uk Kim OPT_CRL_DOWNLOAD, OPT_SERVERINFO, OPT_CERTFORM, OPT_KEY, OPT_KEYFORM,
696e71b7053SJung-uk Kim OPT_PASS, OPT_CERT_CHAIN, OPT_DHPARAM, OPT_DCERTFORM, OPT_DCERT,
697e71b7053SJung-uk Kim OPT_DKEYFORM, OPT_DPASS, OPT_DKEY, OPT_DCERT_CHAIN, OPT_NOCERT,
698e71b7053SJung-uk Kim OPT_CAPATH, OPT_NOCAPATH, OPT_CHAINCAPATH, OPT_VERIFYCAPATH, OPT_NO_CACHE,
699e71b7053SJung-uk Kim OPT_EXT_CACHE, OPT_CRLFORM, OPT_VERIFY_RET_ERROR, OPT_VERIFY_QUIET,
700e71b7053SJung-uk Kim OPT_BUILD_CHAIN, OPT_CAFILE, OPT_NOCAFILE, OPT_CHAINCAFILE,
701b077aed3SPierre Pronchery OPT_VERIFYCAFILE,
702b077aed3SPierre Pronchery OPT_CASTORE, OPT_NOCASTORE, OPT_CHAINCASTORE, OPT_VERIFYCASTORE,
703b077aed3SPierre Pronchery OPT_NBIO, OPT_NBIO_TEST, OPT_IGN_EOF, OPT_NO_IGN_EOF,
704e71b7053SJung-uk Kim OPT_DEBUG, OPT_TLSEXTDEBUG, OPT_STATUS, OPT_STATUS_VERBOSE,
705b077aed3SPierre Pronchery OPT_STATUS_TIMEOUT, OPT_PROXY, OPT_NO_PROXY, OPT_STATUS_URL,
706b077aed3SPierre Pronchery OPT_STATUS_FILE, OPT_MSG, OPT_MSGFILE,
707e71b7053SJung-uk Kim OPT_TRACE, OPT_SECURITY_DEBUG, OPT_SECURITY_DEBUG_VERBOSE, OPT_STATE,
708e71b7053SJung-uk Kim OPT_CRLF, OPT_QUIET, OPT_BRIEF, OPT_NO_DHE,
709e71b7053SJung-uk Kim OPT_NO_RESUME_EPHEMERAL, OPT_PSK_IDENTITY, OPT_PSK_HINT, OPT_PSK,
710e71b7053SJung-uk Kim OPT_PSK_SESS, OPT_SRPVFILE, OPT_SRPUSERSEED, OPT_REV, OPT_WWW,
711e71b7053SJung-uk Kim OPT_UPPER_WWW, OPT_HTTP, OPT_ASYNC, OPT_SSL_CONFIG,
712e71b7053SJung-uk Kim OPT_MAX_SEND_FRAG, OPT_SPLIT_SEND_FRAG, OPT_MAX_PIPELINES, OPT_READ_BUF,
713e71b7053SJung-uk Kim OPT_SSL3, OPT_TLS1_3, OPT_TLS1_2, OPT_TLS1_1, OPT_TLS1, OPT_DTLS, OPT_DTLS1,
714e71b7053SJung-uk Kim OPT_DTLS1_2, OPT_SCTP, OPT_TIMEOUT, OPT_MTU, OPT_LISTEN, OPT_STATELESS,
715e71b7053SJung-uk Kim OPT_ID_PREFIX, OPT_SERVERNAME, OPT_SERVERNAME_FATAL,
716b077aed3SPierre Pronchery OPT_CERT2, OPT_KEY2, OPT_NEXTPROTONEG, OPT_ALPN, OPT_SENDFILE,
717e71b7053SJung-uk Kim OPT_SRTP_PROFILES, OPT_KEYMATEXPORT, OPT_KEYMATEXPORTLEN,
718e71b7053SJung-uk Kim OPT_KEYLOG_FILE, OPT_MAX_EARLY, OPT_RECV_MAX_EARLY, OPT_EARLY_DATA,
7196935a639SJung-uk Kim OPT_S_NUM_TICKETS, OPT_ANTI_REPLAY, OPT_NO_ANTI_REPLAY, OPT_SCTP_LABEL_BUG,
720b077aed3SPierre Pronchery OPT_HTTP_SERVER_BINMODE, OPT_NOCANAMES, OPT_IGNORE_UNEXPECTED_EOF,
721e71b7053SJung-uk Kim OPT_R_ENUM,
722e71b7053SJung-uk Kim OPT_S_ENUM,
723e71b7053SJung-uk Kim OPT_V_ENUM,
724b077aed3SPierre Pronchery OPT_X_ENUM,
725b077aed3SPierre Pronchery OPT_PROV_ENUM
726e71b7053SJung-uk Kim } OPTION_CHOICE;
727e71b7053SJung-uk Kim
728e71b7053SJung-uk Kim const OPTIONS s_server_options[] = {
729b077aed3SPierre Pronchery OPT_SECTION("General"),
730e71b7053SJung-uk Kim {"help", OPT_HELP, '-', "Display this summary"},
731b077aed3SPierre Pronchery {"ssl_config", OPT_SSL_CONFIG, 's',
732b077aed3SPierre Pronchery "Configure SSL_CTX using the given configuration value"},
733b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SSL_TRACE
734b077aed3SPierre Pronchery {"trace", OPT_TRACE, '-', "trace protocol messages"},
735b077aed3SPierre Pronchery #endif
736b077aed3SPierre Pronchery #ifndef OPENSSL_NO_ENGINE
737b077aed3SPierre Pronchery {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
738b077aed3SPierre Pronchery #endif
739b077aed3SPierre Pronchery
740b077aed3SPierre Pronchery OPT_SECTION("Network"),
741e71b7053SJung-uk Kim {"port", OPT_PORT, 'p',
742e71b7053SJung-uk Kim "TCP/IP port to listen on for connections (default is " PORT ")"},
743e71b7053SJung-uk Kim {"accept", OPT_ACCEPT, 's',
744e71b7053SJung-uk Kim "TCP/IP optional host and port to listen on for connections (default is *:" PORT ")"},
745e71b7053SJung-uk Kim #ifdef AF_UNIX
746e71b7053SJung-uk Kim {"unix", OPT_UNIX, 's', "Unix domain socket to accept on"},
747b077aed3SPierre Pronchery {"unlink", OPT_UNLINK, '-', "For -unix, unlink existing socket first"},
748db522d3aSSimon L. B. Nielsen #endif
749e71b7053SJung-uk Kim {"4", OPT_4, '-', "Use IPv4 only"},
750e71b7053SJung-uk Kim {"6", OPT_6, '-', "Use IPv6 only"},
751b077aed3SPierre Pronchery
752b077aed3SPierre Pronchery OPT_SECTION("Identity"),
753e71b7053SJung-uk Kim {"context", OPT_CONTEXT, 's', "Set session ID context"},
754e71b7053SJung-uk Kim {"CAfile", OPT_CAFILE, '<', "PEM format file of CA's"},
755e71b7053SJung-uk Kim {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
756b077aed3SPierre Pronchery {"CAstore", OPT_CASTORE, ':', "URI to store of CA's"},
757e71b7053SJung-uk Kim {"no-CAfile", OPT_NOCAFILE, '-',
758e71b7053SJung-uk Kim "Do not load the default certificates file"},
759e71b7053SJung-uk Kim {"no-CApath", OPT_NOCAPATH, '-',
760e71b7053SJung-uk Kim "Do not load certificates from the default certificates directory"},
761b077aed3SPierre Pronchery {"no-CAstore", OPT_NOCASTORE, '-',
762b077aed3SPierre Pronchery "Do not load certificates from the default certificates store URI"},
763e71b7053SJung-uk Kim {"nocert", OPT_NOCERT, '-', "Don't use any certificates (Anon-DH)"},
764b077aed3SPierre Pronchery {"verify", OPT_VERIFY, 'n', "Turn on peer certificate verification"},
765b077aed3SPierre Pronchery {"Verify", OPT_UPPER_V_VERIFY, 'n',
766b077aed3SPierre Pronchery "Turn on peer certificate verification, must have a cert"},
767b077aed3SPierre Pronchery {"nameopt", OPT_NAMEOPT, 's', "Certificate subject/issuer name printing options"},
768b077aed3SPierre Pronchery {"cert", OPT_CERT, '<', "Server certificate file to use; default " TEST_CERT},
769b077aed3SPierre Pronchery {"cert2", OPT_CERT2, '<',
770b077aed3SPierre Pronchery "Certificate file to use for servername; default " TEST_CERT2},
771b077aed3SPierre Pronchery {"certform", OPT_CERTFORM, 'F',
772b077aed3SPierre Pronchery "Server certificate file format (PEM/DER/P12); has no effect"},
773b077aed3SPierre Pronchery {"cert_chain", OPT_CERT_CHAIN, '<',
774b077aed3SPierre Pronchery "Server certificate chain file in PEM format"},
775b077aed3SPierre Pronchery {"build_chain", OPT_BUILD_CHAIN, '-', "Build server certificate chain"},
776b077aed3SPierre Pronchery {"serverinfo", OPT_SERVERINFO, 's',
777b077aed3SPierre Pronchery "PEM serverinfo file for certificate"},
778b077aed3SPierre Pronchery {"key", OPT_KEY, 's',
779b077aed3SPierre Pronchery "Private key file to use; default is -cert file or else" TEST_CERT},
780b077aed3SPierre Pronchery {"key2", OPT_KEY2, '<',
781b077aed3SPierre Pronchery "-Private Key file to use for servername if not in -cert2"},
782b077aed3SPierre Pronchery {"keyform", OPT_KEYFORM, 'f', "Key format (ENGINE, other values ignored)"},
783b077aed3SPierre Pronchery {"pass", OPT_PASS, 's', "Private key and cert file pass phrase source"},
784b077aed3SPierre Pronchery {"dcert", OPT_DCERT, '<',
785b077aed3SPierre Pronchery "Second server certificate file to use (usually for DSA)"},
786b077aed3SPierre Pronchery {"dcertform", OPT_DCERTFORM, 'F',
787b077aed3SPierre Pronchery "Second server certificate file format (PEM/DER/P12); has no effect"},
788b077aed3SPierre Pronchery {"dcert_chain", OPT_DCERT_CHAIN, '<',
789b077aed3SPierre Pronchery "second server certificate chain file in PEM format"},
790b077aed3SPierre Pronchery {"dkey", OPT_DKEY, '<',
791b077aed3SPierre Pronchery "Second private key file to use (usually for DSA)"},
7926f1af0d7SPierre Pronchery {"dkeyform", OPT_DKEYFORM, 'f',
793b077aed3SPierre Pronchery "Second key file format (ENGINE, other values ignored)"},
794b077aed3SPierre Pronchery {"dpass", OPT_DPASS, 's',
795b077aed3SPierre Pronchery "Second private key and cert file pass phrase source"},
796b077aed3SPierre Pronchery {"dhparam", OPT_DHPARAM, '<', "DH parameters file to use"},
797b077aed3SPierre Pronchery {"servername", OPT_SERVERNAME, 's',
798b077aed3SPierre Pronchery "Servername for HostName TLS extension"},
799b077aed3SPierre Pronchery {"servername_fatal", OPT_SERVERNAME_FATAL, '-',
800b077aed3SPierre Pronchery "On servername mismatch send fatal alert (default warning alert)"},
801b077aed3SPierre Pronchery {"nbio_test", OPT_NBIO_TEST, '-', "Test with the non-blocking test bio"},
802b077aed3SPierre Pronchery {"crlf", OPT_CRLF, '-', "Convert LF from terminal into CRLF"},
803e71b7053SJung-uk Kim {"quiet", OPT_QUIET, '-', "No server output"},
804e71b7053SJung-uk Kim {"no_resume_ephemeral", OPT_NO_RESUME_EPHEMERAL, '-',
805e71b7053SJung-uk Kim "Disable caching and tickets if ephemeral (EC)DH is used"},
806e71b7053SJung-uk Kim {"www", OPT_WWW, '-', "Respond to a 'GET /' with a status page"},
807e71b7053SJung-uk Kim {"WWW", OPT_UPPER_WWW, '-', "Respond to a 'GET with the file ./path"},
808b077aed3SPierre Pronchery {"ignore_unexpected_eof", OPT_IGNORE_UNEXPECTED_EOF, '-',
809b077aed3SPierre Pronchery "Do not treat lack of close_notify from a peer as an error"},
810e71b7053SJung-uk Kim {"tlsextdebug", OPT_TLSEXTDEBUG, '-',
811e71b7053SJung-uk Kim "Hex dump of all TLS extensions received"},
812e71b7053SJung-uk Kim {"HTTP", OPT_HTTP, '-', "Like -WWW but ./path includes HTTP headers"},
813e71b7053SJung-uk Kim {"id_prefix", OPT_ID_PREFIX, 's',
814e71b7053SJung-uk Kim "Generate SSL/TLS session IDs prefixed by arg"},
815e71b7053SJung-uk Kim {"keymatexport", OPT_KEYMATEXPORT, 's',
816e71b7053SJung-uk Kim "Export keying material using label"},
817e71b7053SJung-uk Kim {"keymatexportlen", OPT_KEYMATEXPORTLEN, 'p',
818b077aed3SPierre Pronchery "Export len bytes of keying material; default 20"},
819e71b7053SJung-uk Kim {"CRL", OPT_CRL, '<', "CRL file to use"},
820b077aed3SPierre Pronchery {"CRLform", OPT_CRLFORM, 'F', "CRL file format (PEM or DER); default PEM"},
821e71b7053SJung-uk Kim {"crl_download", OPT_CRL_DOWNLOAD, '-',
822b077aed3SPierre Pronchery "Download CRLs from distribution points in certificate CDP entries"},
823b077aed3SPierre Pronchery {"chainCAfile", OPT_CHAINCAFILE, '<',
824b077aed3SPierre Pronchery "CA file for certificate chain (PEM format)"},
825e71b7053SJung-uk Kim {"chainCApath", OPT_CHAINCAPATH, '/',
826e71b7053SJung-uk Kim "use dir as certificate store path to build CA certificate chain"},
827b077aed3SPierre Pronchery {"chainCAstore", OPT_CHAINCASTORE, ':',
828b077aed3SPierre Pronchery "use URI as certificate store to build CA certificate chain"},
829b077aed3SPierre Pronchery {"verifyCAfile", OPT_VERIFYCAFILE, '<',
830b077aed3SPierre Pronchery "CA file for certificate verification (PEM format)"},
831e71b7053SJung-uk Kim {"verifyCApath", OPT_VERIFYCAPATH, '/',
832e71b7053SJung-uk Kim "use dir as certificate store path to verify CA certificate"},
833b077aed3SPierre Pronchery {"verifyCAstore", OPT_VERIFYCASTORE, ':',
834b077aed3SPierre Pronchery "use URI as certificate store to verify CA certificate"},
835e71b7053SJung-uk Kim {"no_cache", OPT_NO_CACHE, '-', "Disable session cache"},
836e71b7053SJung-uk Kim {"ext_cache", OPT_EXT_CACHE, '-',
837e71b7053SJung-uk Kim "Disable internal cache, set up and use external cache"},
838e71b7053SJung-uk Kim {"verify_return_error", OPT_VERIFY_RET_ERROR, '-',
839e71b7053SJung-uk Kim "Close connection on verification error"},
840e71b7053SJung-uk Kim {"verify_quiet", OPT_VERIFY_QUIET, '-',
841e71b7053SJung-uk Kim "No verify output except verify errors"},
842b077aed3SPierre Pronchery {"ign_eof", OPT_IGN_EOF, '-', "Ignore input EOF (default when -quiet)"},
843b077aed3SPierre Pronchery {"no_ign_eof", OPT_NO_IGN_EOF, '-', "Do not ignore input EOF"},
844b077aed3SPierre Pronchery
845e71b7053SJung-uk Kim #ifndef OPENSSL_NO_OCSP
846b077aed3SPierre Pronchery OPT_SECTION("OCSP"),
847e71b7053SJung-uk Kim {"status", OPT_STATUS, '-', "Request certificate status from server"},
848e71b7053SJung-uk Kim {"status_verbose", OPT_STATUS_VERBOSE, '-',
849e71b7053SJung-uk Kim "Print more output in certificate status callback"},
850e71b7053SJung-uk Kim {"status_timeout", OPT_STATUS_TIMEOUT, 'n',
851e71b7053SJung-uk Kim "Status request responder timeout"},
852e71b7053SJung-uk Kim {"status_url", OPT_STATUS_URL, 's', "Status request fallback URL"},
853b077aed3SPierre Pronchery {"proxy", OPT_PROXY, 's',
854b077aed3SPierre Pronchery "[http[s]://]host[:port][/path] of HTTP(S) proxy to use; path is ignored"},
855b077aed3SPierre Pronchery {"no_proxy", OPT_NO_PROXY, 's',
856b077aed3SPierre Pronchery "List of addresses of servers not to use HTTP(S) proxy for"},
857b077aed3SPierre Pronchery {OPT_MORE_STR, 0, 0,
858b077aed3SPierre Pronchery "Default from environment variable 'no_proxy', else 'NO_PROXY', else none"},
859e71b7053SJung-uk Kim {"status_file", OPT_STATUS_FILE, '<',
860e71b7053SJung-uk Kim "File containing DER encoded OCSP Response"},
861e71b7053SJung-uk Kim #endif
862b077aed3SPierre Pronchery
863b077aed3SPierre Pronchery OPT_SECTION("Debug"),
864e71b7053SJung-uk Kim {"security_debug", OPT_SECURITY_DEBUG, '-',
865e71b7053SJung-uk Kim "Print output from SSL/TLS security framework"},
866e71b7053SJung-uk Kim {"security_debug_verbose", OPT_SECURITY_DEBUG_VERBOSE, '-',
867e71b7053SJung-uk Kim "Print more output from SSL/TLS security framework"},
868e71b7053SJung-uk Kim {"brief", OPT_BRIEF, '-',
869e71b7053SJung-uk Kim "Restrict output to brief summary of connection parameters"},
870e71b7053SJung-uk Kim {"rev", OPT_REV, '-',
871b077aed3SPierre Pronchery "act as an echo server that sends back received text reversed"},
872b077aed3SPierre Pronchery {"debug", OPT_DEBUG, '-', "Print more output"},
873b077aed3SPierre Pronchery {"msg", OPT_MSG, '-', "Show protocol messages"},
874b077aed3SPierre Pronchery {"msgfile", OPT_MSGFILE, '>',
875b077aed3SPierre Pronchery "File to send output of -msg or -trace, instead of stdout"},
876b077aed3SPierre Pronchery {"state", OPT_STATE, '-', "Print the SSL states"},
877e71b7053SJung-uk Kim {"async", OPT_ASYNC, '-', "Operate in asynchronous mode"},
878e71b7053SJung-uk Kim {"max_pipelines", OPT_MAX_PIPELINES, 'p',
879e71b7053SJung-uk Kim "Maximum number of encrypt/decrypt pipelines to be used"},
880b077aed3SPierre Pronchery {"naccept", OPT_NACCEPT, 'p', "Terminate after #num connections"},
881b077aed3SPierre Pronchery {"keylogfile", OPT_KEYLOG_FILE, '>', "Write TLS secrets to file"},
882b077aed3SPierre Pronchery
883b077aed3SPierre Pronchery OPT_SECTION("Network"),
884b077aed3SPierre Pronchery {"nbio", OPT_NBIO, '-', "Use non-blocking IO"},
885b077aed3SPierre Pronchery {"timeout", OPT_TIMEOUT, '-', "Enable timeouts"},
886b077aed3SPierre Pronchery {"mtu", OPT_MTU, 'p', "Set link-layer MTU"},
887e71b7053SJung-uk Kim {"read_buf", OPT_READ_BUF, 'p',
888e71b7053SJung-uk Kim "Default read buffer size to be used for connections"},
889b077aed3SPierre Pronchery {"split_send_frag", OPT_SPLIT_SEND_FRAG, 'p',
890b077aed3SPierre Pronchery "Size used to split data for encrypt pipelines"},
891b077aed3SPierre Pronchery {"max_send_frag", OPT_MAX_SEND_FRAG, 'p', "Maximum Size of send frames "},
892b077aed3SPierre Pronchery
893b077aed3SPierre Pronchery OPT_SECTION("Server identity"),
894e71b7053SJung-uk Kim {"psk_identity", OPT_PSK_IDENTITY, 's', "PSK identity to expect"},
895e71b7053SJung-uk Kim #ifndef OPENSSL_NO_PSK
896e71b7053SJung-uk Kim {"psk_hint", OPT_PSK_HINT, 's', "PSK identity hint to use"},
897e71b7053SJung-uk Kim #endif
898e71b7053SJung-uk Kim {"psk", OPT_PSK, 's', "PSK in hex (without 0x)"},
899e71b7053SJung-uk Kim {"psk_session", OPT_PSK_SESS, '<', "File to read PSK SSL session from"},
9001f13597dSJung-uk Kim #ifndef OPENSSL_NO_SRP
901b077aed3SPierre Pronchery {"srpvfile", OPT_SRPVFILE, '<', "(deprecated) The verifier file for SRP"},
902e71b7053SJung-uk Kim {"srpuserseed", OPT_SRPUSERSEED, 's',
903b077aed3SPierre Pronchery "(deprecated) A seed string for a default user salt"},
904e71b7053SJung-uk Kim #endif
905b077aed3SPierre Pronchery
906b077aed3SPierre Pronchery OPT_SECTION("Protocol and version"),
907b077aed3SPierre Pronchery {"max_early_data", OPT_MAX_EARLY, 'n',
908b077aed3SPierre Pronchery "The maximum number of bytes of early data as advertised in tickets"},
909b077aed3SPierre Pronchery {"recv_max_early_data", OPT_RECV_MAX_EARLY, 'n',
910b077aed3SPierre Pronchery "The maximum number of bytes of early data (hard limit)"},
911b077aed3SPierre Pronchery {"early_data", OPT_EARLY_DATA, '-', "Attempt to read early data"},
912b077aed3SPierre Pronchery {"num_tickets", OPT_S_NUM_TICKETS, 'n',
913b077aed3SPierre Pronchery "The number of TLSv1.3 session tickets that a server will automatically issue" },
914b077aed3SPierre Pronchery {"anti_replay", OPT_ANTI_REPLAY, '-', "Switch on anti-replay protection (default)"},
915b077aed3SPierre Pronchery {"no_anti_replay", OPT_NO_ANTI_REPLAY, '-', "Switch off anti-replay protection"},
916b077aed3SPierre Pronchery {"http_server_binmode", OPT_HTTP_SERVER_BINMODE, '-', "opening files in binary mode when acting as http server (-WWW and -HTTP)"},
917b077aed3SPierre Pronchery {"no_ca_names", OPT_NOCANAMES, '-',
918b077aed3SPierre Pronchery "Disable TLS Extension CA Names"},
919b077aed3SPierre Pronchery {"stateless", OPT_STATELESS, '-', "Require TLSv1.3 cookies"},
920e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SSL3
921e71b7053SJung-uk Kim {"ssl3", OPT_SSL3, '-', "Just talk SSLv3"},
922e71b7053SJung-uk Kim #endif
923e71b7053SJung-uk Kim #ifndef OPENSSL_NO_TLS1
924e71b7053SJung-uk Kim {"tls1", OPT_TLS1, '-', "Just talk TLSv1"},
925e71b7053SJung-uk Kim #endif
926e71b7053SJung-uk Kim #ifndef OPENSSL_NO_TLS1_1
927e71b7053SJung-uk Kim {"tls1_1", OPT_TLS1_1, '-', "Just talk TLSv1.1"},
928e71b7053SJung-uk Kim #endif
929e71b7053SJung-uk Kim #ifndef OPENSSL_NO_TLS1_2
930e71b7053SJung-uk Kim {"tls1_2", OPT_TLS1_2, '-', "just talk TLSv1.2"},
931e71b7053SJung-uk Kim #endif
932e71b7053SJung-uk Kim #ifndef OPENSSL_NO_TLS1_3
933e71b7053SJung-uk Kim {"tls1_3", OPT_TLS1_3, '-', "just talk TLSv1.3"},
934e71b7053SJung-uk Kim #endif
935e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS
936e71b7053SJung-uk Kim {"dtls", OPT_DTLS, '-', "Use any DTLS version"},
937e71b7053SJung-uk Kim {"listen", OPT_LISTEN, '-',
938e71b7053SJung-uk Kim "Listen for a DTLS ClientHello with a cookie and then connect"},
939e71b7053SJung-uk Kim #endif
940e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS1
941e71b7053SJung-uk Kim {"dtls1", OPT_DTLS1, '-', "Just talk DTLSv1"},
942e71b7053SJung-uk Kim #endif
943e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS1_2
944e71b7053SJung-uk Kim {"dtls1_2", OPT_DTLS1_2, '-', "Just talk DTLSv1.2"},
945e71b7053SJung-uk Kim #endif
946e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SCTP
947e71b7053SJung-uk Kim {"sctp", OPT_SCTP, '-', "Use SCTP"},
9486935a639SJung-uk Kim {"sctp_label_bug", OPT_SCTP_LABEL_BUG, '-', "Enable SCTP label length bug"},
949e71b7053SJung-uk Kim #endif
95009286989SJung-uk Kim #ifndef OPENSSL_NO_SRTP
951e71b7053SJung-uk Kim {"use_srtp", OPT_SRTP_PROFILES, 's',
952e71b7053SJung-uk Kim "Offer SRTP key management with a colon-separated profile list"},
95309286989SJung-uk Kim #endif
954b077aed3SPierre Pronchery {"no_dhe", OPT_NO_DHE, '-', "Disable ephemeral DH"},
955b077aed3SPierre Pronchery #ifndef OPENSSL_NO_NEXTPROTONEG
956b077aed3SPierre Pronchery {"nextprotoneg", OPT_NEXTPROTONEG, 's',
957b077aed3SPierre Pronchery "Set the advertised protocols for the NPN extension (comma-separated list)"},
958b077aed3SPierre Pronchery #endif
959e71b7053SJung-uk Kim {"alpn", OPT_ALPN, 's',
960e71b7053SJung-uk Kim "Set the advertised protocols for the ALPN extension (comma-separated list)"},
961b077aed3SPierre Pronchery #ifndef OPENSSL_NO_KTLS
962b077aed3SPierre Pronchery {"sendfile", OPT_SENDFILE, '-', "Use sendfile to response file with -WWW"},
963e71b7053SJung-uk Kim #endif
964b077aed3SPierre Pronchery
965b077aed3SPierre Pronchery OPT_R_OPTIONS,
966b077aed3SPierre Pronchery OPT_S_OPTIONS,
967b077aed3SPierre Pronchery OPT_V_OPTIONS,
968b077aed3SPierre Pronchery OPT_X_OPTIONS,
969b077aed3SPierre Pronchery OPT_PROV_OPTIONS,
970b077aed3SPierre Pronchery {NULL}
971e71b7053SJung-uk Kim };
972db522d3aSSimon L. B. Nielsen
973e71b7053SJung-uk Kim #define IS_PROT_FLAG(o) \
974e71b7053SJung-uk Kim (o == OPT_SSL3 || o == OPT_TLS1 || o == OPT_TLS1_1 || o == OPT_TLS1_2 \
975e71b7053SJung-uk Kim || o == OPT_TLS1_3 || o == OPT_DTLS || o == OPT_DTLS1 || o == OPT_DTLS1_2)
976e71b7053SJung-uk Kim
s_server_main(int argc,char * argv[])977e71b7053SJung-uk Kim int s_server_main(int argc, char *argv[])
97874664626SKris Kennaway {
979e71b7053SJung-uk Kim ENGINE *engine = NULL;
980e71b7053SJung-uk Kim EVP_PKEY *s_key = NULL, *s_dkey = NULL;
981e71b7053SJung-uk Kim SSL_CONF_CTX *cctx = NULL;
982e71b7053SJung-uk Kim const SSL_METHOD *meth = TLS_server_method();
983e71b7053SJung-uk Kim SSL_EXCERT *exc = NULL;
984e71b7053SJung-uk Kim STACK_OF(OPENSSL_STRING) *ssl_args = NULL;
985e71b7053SJung-uk Kim STACK_OF(X509) *s_chain = NULL, *s_dchain = NULL;
986e71b7053SJung-uk Kim STACK_OF(X509_CRL) *crls = NULL;
987e71b7053SJung-uk Kim X509 *s_cert = NULL, *s_dcert = NULL;
9881f13597dSJung-uk Kim X509_VERIFY_PARAM *vpm = NULL;
989b077aed3SPierre Pronchery const char *CApath = NULL, *CAfile = NULL, *CAstore = NULL;
990b077aed3SPierre Pronchery const char *chCApath = NULL, *chCAfile = NULL, *chCAstore = NULL;
991e71b7053SJung-uk Kim char *dpassarg = NULL, *dpass = NULL;
992b077aed3SPierre Pronchery char *passarg = NULL, *pass = NULL;
993b077aed3SPierre Pronchery char *vfyCApath = NULL, *vfyCAfile = NULL, *vfyCAstore = NULL;
994e71b7053SJung-uk Kim char *crl_file = NULL, *prog;
995e71b7053SJung-uk Kim #ifdef AF_UNIX
996e71b7053SJung-uk Kim int unlink_unix_path = 0;
997e71b7053SJung-uk Kim #endif
998e71b7053SJung-uk Kim do_server_cb server_cb;
999e71b7053SJung-uk Kim int vpmtouched = 0, build_chain = 0, no_cache = 0, ext_cache = 0;
1000f579bf8eSKris Kennaway char *dhfile = NULL;
1001dea77ea6SJung-uk Kim int no_dhe = 0;
1002e71b7053SJung-uk Kim int nocert = 0, ret = 1;
1003b077aed3SPierre Pronchery int noCApath = 0, noCAfile = 0, noCAstore = 0;
1004b077aed3SPierre Pronchery int s_cert_format = FORMAT_UNDEF, s_key_format = FORMAT_UNDEF;
1005b077aed3SPierre Pronchery int s_dcert_format = FORMAT_UNDEF, s_dkey_format = FORMAT_UNDEF;
1006e71b7053SJung-uk Kim int rev = 0, naccept = -1, sdebug = 0;
1007e71b7053SJung-uk Kim int socket_family = AF_UNSPEC, socket_type = SOCK_STREAM, protocol = 0;
1008b077aed3SPierre Pronchery int state = 0, crl_format = FORMAT_UNDEF, crl_download = 0;
1009e71b7053SJung-uk Kim char *host = NULL;
1010b077aed3SPierre Pronchery char *port = NULL;
1011e71b7053SJung-uk Kim unsigned char *context = NULL;
1012e71b7053SJung-uk Kim OPTION_CHOICE o;
1013db522d3aSSimon L. B. Nielsen EVP_PKEY *s_key2 = NULL;
1014db522d3aSSimon L. B. Nielsen X509 *s_cert2 = NULL;
1015db522d3aSSimon L. B. Nielsen tlsextctx tlsextcbp = { NULL, NULL, SSL_TLSEXT_ERR_ALERT_WARNING };
1016e71b7053SJung-uk Kim const char *ssl_config = NULL;
1017e71b7053SJung-uk Kim int read_buf_len = 0;
10181f13597dSJung-uk Kim #ifndef OPENSSL_NO_NEXTPROTONEG
10191f13597dSJung-uk Kim const char *next_proto_neg_in = NULL;
10207bded2dbSJung-uk Kim tlsextnextprotoctx next_proto = { NULL, 0 };
1021db522d3aSSimon L. B. Nielsen #endif
10227bded2dbSJung-uk Kim const char *alpn_in = NULL;
10237bded2dbSJung-uk Kim tlsextalpnctx alpn_ctx = { NULL, 0 };
10241f13597dSJung-uk Kim #ifndef OPENSSL_NO_PSK
10251f13597dSJung-uk Kim /* by default do not send a PSK identity hint */
1026e71b7053SJung-uk Kim char *psk_identity_hint = NULL;
10271f13597dSJung-uk Kim #endif
1028e71b7053SJung-uk Kim char *p;
10291f13597dSJung-uk Kim #ifndef OPENSSL_NO_SRP
10301f13597dSJung-uk Kim char *srpuserseed = NULL;
10311f13597dSJung-uk Kim char *srp_verifier_file = NULL;
10321f13597dSJung-uk Kim #endif
1033e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SRTP
1034e71b7053SJung-uk Kim char *srtp_profiles = NULL;
1035e71b7053SJung-uk Kim #endif
1036e71b7053SJung-uk Kim int min_version = 0, max_version = 0, prot_opt = 0, no_prot_opt = 0;
1037e71b7053SJung-uk Kim int s_server_verify = SSL_VERIFY_NONE;
1038e71b7053SJung-uk Kim int s_server_session_id_context = 1; /* anything will do */
1039e71b7053SJung-uk Kim const char *s_cert_file = TEST_CERT, *s_key_file = NULL, *s_chain_file = NULL;
1040e71b7053SJung-uk Kim const char *s_cert_file2 = TEST_CERT2, *s_key_file2 = NULL;
1041e71b7053SJung-uk Kim char *s_dcert_file = NULL, *s_dkey_file = NULL, *s_dchain_file = NULL;
1042e71b7053SJung-uk Kim #ifndef OPENSSL_NO_OCSP
1043e71b7053SJung-uk Kim int s_tlsextstatus = 0;
1044e71b7053SJung-uk Kim #endif
1045e71b7053SJung-uk Kim int no_resume_ephemeral = 0;
1046e71b7053SJung-uk Kim unsigned int max_send_fragment = 0;
1047e71b7053SJung-uk Kim unsigned int split_send_fragment = 0, max_pipelines = 0;
1048e71b7053SJung-uk Kim const char *s_serverinfo_file = NULL;
1049e71b7053SJung-uk Kim const char *keylog_file = NULL;
1050e71b7053SJung-uk Kim int max_early_data = -1, recv_max_early_data = -1;
1051e71b7053SJung-uk Kim char *psksessf = NULL;
1052b077aed3SPierre Pronchery int no_ca_names = 0;
10536935a639SJung-uk Kim #ifndef OPENSSL_NO_SCTP
10546935a639SJung-uk Kim int sctp_label_bug = 0;
10556935a639SJung-uk Kim #endif
1056b077aed3SPierre Pronchery int ignore_unexpected_eof = 0;
10577bded2dbSJung-uk Kim
1058e71b7053SJung-uk Kim /* Init of few remaining global variables */
105974664626SKris Kennaway local_argc = argc;
106074664626SKris Kennaway local_argv = argv;
106174664626SKris Kennaway
1062e71b7053SJung-uk Kim ctx = ctx2 = NULL;
1063e71b7053SJung-uk Kim s_nbio = s_nbio_test = 0;
1064e71b7053SJung-uk Kim www = 0;
1065e71b7053SJung-uk Kim bio_s_out = NULL;
1066e71b7053SJung-uk Kim s_debug = 0;
1067e71b7053SJung-uk Kim s_msg = 0;
1068e71b7053SJung-uk Kim s_quiet = 0;
1069e71b7053SJung-uk Kim s_brief = 0;
1070e71b7053SJung-uk Kim async = 0;
1071b077aed3SPierre Pronchery use_sendfile = 0;
10725c87c606SMark Murray
1073b077aed3SPierre Pronchery port = OPENSSL_strdup(PORT);
10747bded2dbSJung-uk Kim cctx = SSL_CONF_CTX_new();
1075e71b7053SJung-uk Kim vpm = X509_VERIFY_PARAM_new();
1076b077aed3SPierre Pronchery if (port == NULL || cctx == NULL || vpm == NULL)
10777bded2dbSJung-uk Kim goto end;
1078e71b7053SJung-uk Kim SSL_CONF_CTX_set_flags(cctx,
1079e71b7053SJung-uk Kim SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CMDLINE);
10807bded2dbSJung-uk Kim
1081e71b7053SJung-uk Kim prog = opt_init(argc, argv, s_server_options);
1082e71b7053SJung-uk Kim while ((o = opt_next()) != OPT_EOF) {
1083e71b7053SJung-uk Kim if (IS_PROT_FLAG(o) && ++prot_opt > 1) {
1084e71b7053SJung-uk Kim BIO_printf(bio_err, "Cannot supply multiple protocol flags\n");
1085e71b7053SJung-uk Kim goto end;
10867bded2dbSJung-uk Kim }
1087e71b7053SJung-uk Kim if (IS_NO_PROT_FLAG(o))
1088e71b7053SJung-uk Kim no_prot_opt++;
1089e71b7053SJung-uk Kim if (prot_opt == 1 && no_prot_opt) {
1090e71b7053SJung-uk Kim BIO_printf(bio_err,
1091e71b7053SJung-uk Kim "Cannot supply both a protocol flag and '-no_<prot>'\n");
1092e71b7053SJung-uk Kim goto end;
1093e71b7053SJung-uk Kim }
1094e71b7053SJung-uk Kim switch (o) {
1095e71b7053SJung-uk Kim case OPT_EOF:
1096e71b7053SJung-uk Kim case OPT_ERR:
1097e71b7053SJung-uk Kim opthelp:
1098e71b7053SJung-uk Kim BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
1099e71b7053SJung-uk Kim goto end;
1100e71b7053SJung-uk Kim case OPT_HELP:
1101e71b7053SJung-uk Kim opt_help(s_server_options);
1102e71b7053SJung-uk Kim ret = 0;
1103e71b7053SJung-uk Kim goto end;
1104e71b7053SJung-uk Kim
1105e71b7053SJung-uk Kim case OPT_4:
1106e71b7053SJung-uk Kim #ifdef AF_UNIX
1107e71b7053SJung-uk Kim if (socket_family == AF_UNIX) {
1108e71b7053SJung-uk Kim OPENSSL_free(host); host = NULL;
1109e71b7053SJung-uk Kim OPENSSL_free(port); port = NULL;
1110e71b7053SJung-uk Kim }
1111e71b7053SJung-uk Kim #endif
1112e71b7053SJung-uk Kim socket_family = AF_INET;
1113e71b7053SJung-uk Kim break;
1114e71b7053SJung-uk Kim case OPT_6:
1115e71b7053SJung-uk Kim if (1) {
1116e71b7053SJung-uk Kim #ifdef AF_INET6
1117e71b7053SJung-uk Kim #ifdef AF_UNIX
1118e71b7053SJung-uk Kim if (socket_family == AF_UNIX) {
1119e71b7053SJung-uk Kim OPENSSL_free(host); host = NULL;
1120e71b7053SJung-uk Kim OPENSSL_free(port); port = NULL;
1121e71b7053SJung-uk Kim }
1122e71b7053SJung-uk Kim #endif
1123e71b7053SJung-uk Kim socket_family = AF_INET6;
1124e71b7053SJung-uk Kim } else {
1125e71b7053SJung-uk Kim #endif
1126e71b7053SJung-uk Kim BIO_printf(bio_err, "%s: IPv6 domain sockets unsupported\n", prog);
1127e71b7053SJung-uk Kim goto end;
1128e71b7053SJung-uk Kim }
1129e71b7053SJung-uk Kim break;
1130e71b7053SJung-uk Kim case OPT_PORT:
1131e71b7053SJung-uk Kim #ifdef AF_UNIX
1132e71b7053SJung-uk Kim if (socket_family == AF_UNIX) {
1133e71b7053SJung-uk Kim socket_family = AF_UNSPEC;
1134e71b7053SJung-uk Kim }
1135e71b7053SJung-uk Kim #endif
1136e71b7053SJung-uk Kim OPENSSL_free(port); port = NULL;
1137e71b7053SJung-uk Kim OPENSSL_free(host); host = NULL;
1138e71b7053SJung-uk Kim if (BIO_parse_hostserv(opt_arg(), NULL, &port, BIO_PARSE_PRIO_SERV) < 1) {
1139e71b7053SJung-uk Kim BIO_printf(bio_err,
1140e71b7053SJung-uk Kim "%s: -port argument malformed or ambiguous\n",
1141e71b7053SJung-uk Kim port);
1142e71b7053SJung-uk Kim goto end;
1143e71b7053SJung-uk Kim }
1144e71b7053SJung-uk Kim break;
1145e71b7053SJung-uk Kim case OPT_ACCEPT:
1146e71b7053SJung-uk Kim #ifdef AF_UNIX
1147e71b7053SJung-uk Kim if (socket_family == AF_UNIX) {
1148e71b7053SJung-uk Kim socket_family = AF_UNSPEC;
1149e71b7053SJung-uk Kim }
1150e71b7053SJung-uk Kim #endif
1151e71b7053SJung-uk Kim OPENSSL_free(port); port = NULL;
1152e71b7053SJung-uk Kim OPENSSL_free(host); host = NULL;
1153e71b7053SJung-uk Kim if (BIO_parse_hostserv(opt_arg(), &host, &port, BIO_PARSE_PRIO_SERV) < 1) {
1154e71b7053SJung-uk Kim BIO_printf(bio_err,
1155e71b7053SJung-uk Kim "%s: -accept argument malformed or ambiguous\n",
1156e71b7053SJung-uk Kim port);
1157e71b7053SJung-uk Kim goto end;
1158e71b7053SJung-uk Kim }
1159e71b7053SJung-uk Kim break;
1160e71b7053SJung-uk Kim #ifdef AF_UNIX
1161e71b7053SJung-uk Kim case OPT_UNIX:
1162e71b7053SJung-uk Kim socket_family = AF_UNIX;
1163b077aed3SPierre Pronchery OPENSSL_free(host); host = OPENSSL_strdup(opt_arg());
1164b077aed3SPierre Pronchery if (host == NULL)
1165b077aed3SPierre Pronchery goto end;
1166e71b7053SJung-uk Kim OPENSSL_free(port); port = NULL;
1167e71b7053SJung-uk Kim break;
1168e71b7053SJung-uk Kim case OPT_UNLINK:
1169e71b7053SJung-uk Kim unlink_unix_path = 1;
1170e71b7053SJung-uk Kim break;
1171e71b7053SJung-uk Kim #endif
1172e71b7053SJung-uk Kim case OPT_NACCEPT:
1173e71b7053SJung-uk Kim naccept = atol(opt_arg());
1174e71b7053SJung-uk Kim break;
1175e71b7053SJung-uk Kim case OPT_VERIFY:
117674664626SKris Kennaway s_server_verify = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE;
1177e71b7053SJung-uk Kim verify_args.depth = atoi(opt_arg());
11787bded2dbSJung-uk Kim if (!s_quiet)
1179e71b7053SJung-uk Kim BIO_printf(bio_err, "verify depth is %d\n", verify_args.depth);
1180e71b7053SJung-uk Kim break;
1181e71b7053SJung-uk Kim case OPT_UPPER_V_VERIFY:
11826f9291ceSJung-uk Kim s_server_verify =
11836f9291ceSJung-uk Kim SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
118474664626SKris Kennaway SSL_VERIFY_CLIENT_ONCE;
1185e71b7053SJung-uk Kim verify_args.depth = atoi(opt_arg());
11867bded2dbSJung-uk Kim if (!s_quiet)
11876f9291ceSJung-uk Kim BIO_printf(bio_err,
11886f9291ceSJung-uk Kim "verify depth is %d, must return a certificate\n",
1189e71b7053SJung-uk Kim verify_args.depth);
1190e71b7053SJung-uk Kim break;
1191e71b7053SJung-uk Kim case OPT_CONTEXT:
1192e71b7053SJung-uk Kim context = (unsigned char *)opt_arg();
1193e71b7053SJung-uk Kim break;
1194e71b7053SJung-uk Kim case OPT_CERT:
1195e71b7053SJung-uk Kim s_cert_file = opt_arg();
1196e71b7053SJung-uk Kim break;
1197e71b7053SJung-uk Kim case OPT_NAMEOPT:
1198e71b7053SJung-uk Kim if (!set_nameopt(opt_arg()))
1199e71b7053SJung-uk Kim goto end;
1200e71b7053SJung-uk Kim break;
1201e71b7053SJung-uk Kim case OPT_CRL:
1202e71b7053SJung-uk Kim crl_file = opt_arg();
1203e71b7053SJung-uk Kim break;
1204e71b7053SJung-uk Kim case OPT_CRL_DOWNLOAD:
12057bded2dbSJung-uk Kim crl_download = 1;
1206e71b7053SJung-uk Kim break;
1207e71b7053SJung-uk Kim case OPT_SERVERINFO:
1208e71b7053SJung-uk Kim s_serverinfo_file = opt_arg();
1209e71b7053SJung-uk Kim break;
1210e71b7053SJung-uk Kim case OPT_CERTFORM:
1211b077aed3SPierre Pronchery if (!opt_format(opt_arg(), OPT_FMT_ANY, &s_cert_format))
1212e71b7053SJung-uk Kim goto opthelp;
1213e71b7053SJung-uk Kim break;
1214e71b7053SJung-uk Kim case OPT_KEY:
1215e71b7053SJung-uk Kim s_key_file = opt_arg();
1216e71b7053SJung-uk Kim break;
1217e71b7053SJung-uk Kim case OPT_KEYFORM:
1218e71b7053SJung-uk Kim if (!opt_format(opt_arg(), OPT_FMT_ANY, &s_key_format))
1219e71b7053SJung-uk Kim goto opthelp;
1220e71b7053SJung-uk Kim break;
1221e71b7053SJung-uk Kim case OPT_PASS:
1222e71b7053SJung-uk Kim passarg = opt_arg();
1223e71b7053SJung-uk Kim break;
1224e71b7053SJung-uk Kim case OPT_CERT_CHAIN:
1225e71b7053SJung-uk Kim s_chain_file = opt_arg();
1226e71b7053SJung-uk Kim break;
1227e71b7053SJung-uk Kim case OPT_DHPARAM:
1228e71b7053SJung-uk Kim dhfile = opt_arg();
1229e71b7053SJung-uk Kim break;
1230e71b7053SJung-uk Kim case OPT_DCERTFORM:
1231b077aed3SPierre Pronchery if (!opt_format(opt_arg(), OPT_FMT_ANY, &s_dcert_format))
1232e71b7053SJung-uk Kim goto opthelp;
1233e71b7053SJung-uk Kim break;
1234e71b7053SJung-uk Kim case OPT_DCERT:
1235e71b7053SJung-uk Kim s_dcert_file = opt_arg();
1236e71b7053SJung-uk Kim break;
1237e71b7053SJung-uk Kim case OPT_DKEYFORM:
1238b077aed3SPierre Pronchery if (!opt_format(opt_arg(), OPT_FMT_ANY, &s_dkey_format))
1239e71b7053SJung-uk Kim goto opthelp;
1240e71b7053SJung-uk Kim break;
1241e71b7053SJung-uk Kim case OPT_DPASS:
1242e71b7053SJung-uk Kim dpassarg = opt_arg();
1243e71b7053SJung-uk Kim break;
1244e71b7053SJung-uk Kim case OPT_DKEY:
1245e71b7053SJung-uk Kim s_dkey_file = opt_arg();
1246e71b7053SJung-uk Kim break;
1247e71b7053SJung-uk Kim case OPT_DCERT_CHAIN:
1248e71b7053SJung-uk Kim s_dchain_file = opt_arg();
1249e71b7053SJung-uk Kim break;
1250e71b7053SJung-uk Kim case OPT_NOCERT:
125174664626SKris Kennaway nocert = 1;
1252e71b7053SJung-uk Kim break;
1253e71b7053SJung-uk Kim case OPT_CAPATH:
1254e71b7053SJung-uk Kim CApath = opt_arg();
1255e71b7053SJung-uk Kim break;
1256e71b7053SJung-uk Kim case OPT_NOCAPATH:
1257e71b7053SJung-uk Kim noCApath = 1;
1258e71b7053SJung-uk Kim break;
1259e71b7053SJung-uk Kim case OPT_CHAINCAPATH:
1260e71b7053SJung-uk Kim chCApath = opt_arg();
1261e71b7053SJung-uk Kim break;
1262e71b7053SJung-uk Kim case OPT_VERIFYCAPATH:
1263e71b7053SJung-uk Kim vfyCApath = opt_arg();
1264e71b7053SJung-uk Kim break;
1265b077aed3SPierre Pronchery case OPT_CASTORE:
1266b077aed3SPierre Pronchery CAstore = opt_arg();
1267b077aed3SPierre Pronchery break;
1268b077aed3SPierre Pronchery case OPT_NOCASTORE:
1269b077aed3SPierre Pronchery noCAstore = 1;
1270b077aed3SPierre Pronchery break;
1271b077aed3SPierre Pronchery case OPT_CHAINCASTORE:
1272b077aed3SPierre Pronchery chCAstore = opt_arg();
1273b077aed3SPierre Pronchery break;
1274b077aed3SPierre Pronchery case OPT_VERIFYCASTORE:
1275b077aed3SPierre Pronchery vfyCAstore = opt_arg();
1276b077aed3SPierre Pronchery break;
1277e71b7053SJung-uk Kim case OPT_NO_CACHE:
12786a599222SSimon L. B. Nielsen no_cache = 1;
1279e71b7053SJung-uk Kim break;
1280e71b7053SJung-uk Kim case OPT_EXT_CACHE:
12817bded2dbSJung-uk Kim ext_cache = 1;
1282e71b7053SJung-uk Kim break;
1283e71b7053SJung-uk Kim case OPT_CRLFORM:
1284e71b7053SJung-uk Kim if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &crl_format))
1285e71b7053SJung-uk Kim goto opthelp;
1286e71b7053SJung-uk Kim break;
1287e71b7053SJung-uk Kim case OPT_S_CASES:
1288e71b7053SJung-uk Kim case OPT_S_NUM_TICKETS:
1289e71b7053SJung-uk Kim case OPT_ANTI_REPLAY:
1290e71b7053SJung-uk Kim case OPT_NO_ANTI_REPLAY:
1291e71b7053SJung-uk Kim if (ssl_args == NULL)
1292e71b7053SJung-uk Kim ssl_args = sk_OPENSSL_STRING_new_null();
1293e71b7053SJung-uk Kim if (ssl_args == NULL
1294e71b7053SJung-uk Kim || !sk_OPENSSL_STRING_push(ssl_args, opt_flag())
1295e71b7053SJung-uk Kim || !sk_OPENSSL_STRING_push(ssl_args, opt_arg())) {
1296e71b7053SJung-uk Kim BIO_printf(bio_err, "%s: Memory allocation failure\n", prog);
1297e71b7053SJung-uk Kim goto end;
1298e71b7053SJung-uk Kim }
1299e71b7053SJung-uk Kim break;
1300e71b7053SJung-uk Kim case OPT_V_CASES:
1301e71b7053SJung-uk Kim if (!opt_verify(o, vpm))
1302e71b7053SJung-uk Kim goto end;
1303e71b7053SJung-uk Kim vpmtouched++;
1304e71b7053SJung-uk Kim break;
1305e71b7053SJung-uk Kim case OPT_X_CASES:
1306e71b7053SJung-uk Kim if (!args_excert(o, &exc))
1307e71b7053SJung-uk Kim goto end;
1308e71b7053SJung-uk Kim break;
1309e71b7053SJung-uk Kim case OPT_VERIFY_RET_ERROR:
1310e71b7053SJung-uk Kim verify_args.return_error = 1;
1311e71b7053SJung-uk Kim break;
1312e71b7053SJung-uk Kim case OPT_VERIFY_QUIET:
1313e71b7053SJung-uk Kim verify_args.quiet = 1;
1314e71b7053SJung-uk Kim break;
1315e71b7053SJung-uk Kim case OPT_BUILD_CHAIN:
13167bded2dbSJung-uk Kim build_chain = 1;
1317e71b7053SJung-uk Kim break;
1318e71b7053SJung-uk Kim case OPT_CAFILE:
1319e71b7053SJung-uk Kim CAfile = opt_arg();
1320e71b7053SJung-uk Kim break;
1321e71b7053SJung-uk Kim case OPT_NOCAFILE:
1322e71b7053SJung-uk Kim noCAfile = 1;
1323e71b7053SJung-uk Kim break;
1324e71b7053SJung-uk Kim case OPT_CHAINCAFILE:
1325e71b7053SJung-uk Kim chCAfile = opt_arg();
1326e71b7053SJung-uk Kim break;
1327e71b7053SJung-uk Kim case OPT_VERIFYCAFILE:
1328e71b7053SJung-uk Kim vfyCAfile = opt_arg();
1329e71b7053SJung-uk Kim break;
1330e71b7053SJung-uk Kim case OPT_NBIO:
13316f9291ceSJung-uk Kim s_nbio = 1;
1332e71b7053SJung-uk Kim break;
1333e71b7053SJung-uk Kim case OPT_NBIO_TEST:
1334e71b7053SJung-uk Kim s_nbio = s_nbio_test = 1;
1335e71b7053SJung-uk Kim break;
1336e71b7053SJung-uk Kim case OPT_IGN_EOF:
13377bded2dbSJung-uk Kim s_ign_eof = 1;
1338e71b7053SJung-uk Kim break;
1339e71b7053SJung-uk Kim case OPT_NO_IGN_EOF:
13407bded2dbSJung-uk Kim s_ign_eof = 0;
1341e71b7053SJung-uk Kim break;
1342e71b7053SJung-uk Kim case OPT_DEBUG:
13436f9291ceSJung-uk Kim s_debug = 1;
1344e71b7053SJung-uk Kim break;
1345e71b7053SJung-uk Kim case OPT_TLSEXTDEBUG:
1346db522d3aSSimon L. B. Nielsen s_tlsextdebug = 1;
1347e71b7053SJung-uk Kim break;
1348e71b7053SJung-uk Kim case OPT_STATUS:
1349e71b7053SJung-uk Kim #ifndef OPENSSL_NO_OCSP
1350db522d3aSSimon L. B. Nielsen s_tlsextstatus = 1;
1351e71b7053SJung-uk Kim #endif
1352e71b7053SJung-uk Kim break;
1353e71b7053SJung-uk Kim case OPT_STATUS_VERBOSE:
1354e71b7053SJung-uk Kim #ifndef OPENSSL_NO_OCSP
1355e71b7053SJung-uk Kim s_tlsextstatus = tlscstatp.verbose = 1;
1356e71b7053SJung-uk Kim #endif
1357e71b7053SJung-uk Kim break;
1358e71b7053SJung-uk Kim case OPT_STATUS_TIMEOUT:
1359e71b7053SJung-uk Kim #ifndef OPENSSL_NO_OCSP
1360db522d3aSSimon L. B. Nielsen s_tlsextstatus = 1;
1361e71b7053SJung-uk Kim tlscstatp.timeout = atoi(opt_arg());
1362e71b7053SJung-uk Kim #endif
1363e71b7053SJung-uk Kim break;
1364b077aed3SPierre Pronchery case OPT_PROXY:
1365b077aed3SPierre Pronchery #ifndef OPENSSL_NO_OCSP
1366b077aed3SPierre Pronchery tlscstatp.proxy = opt_arg();
1367b077aed3SPierre Pronchery #endif
1368b077aed3SPierre Pronchery break;
1369b077aed3SPierre Pronchery case OPT_NO_PROXY:
1370b077aed3SPierre Pronchery #ifndef OPENSSL_NO_OCSP
1371b077aed3SPierre Pronchery tlscstatp.no_proxy = opt_arg();
1372b077aed3SPierre Pronchery #endif
1373b077aed3SPierre Pronchery break;
1374e71b7053SJung-uk Kim case OPT_STATUS_URL:
1375e71b7053SJung-uk Kim #ifndef OPENSSL_NO_OCSP
1376db522d3aSSimon L. B. Nielsen s_tlsextstatus = 1;
1377b077aed3SPierre Pronchery if (!OSSL_HTTP_parse_url(opt_arg(), &tlscstatp.use_ssl, NULL,
1378b077aed3SPierre Pronchery &tlscstatp.host, &tlscstatp.port, NULL,
1379b077aed3SPierre Pronchery &tlscstatp.path, NULL, NULL)) {
1380b077aed3SPierre Pronchery BIO_printf(bio_err, "Error parsing -status_url argument\n");
138174664626SKris Kennaway goto end;
138274664626SKris Kennaway }
1383e71b7053SJung-uk Kim #endif
1384e71b7053SJung-uk Kim break;
1385e71b7053SJung-uk Kim case OPT_STATUS_FILE:
1386e71b7053SJung-uk Kim #ifndef OPENSSL_NO_OCSP
1387e71b7053SJung-uk Kim s_tlsextstatus = 1;
1388e71b7053SJung-uk Kim tlscstatp.respin = opt_arg();
1389e71b7053SJung-uk Kim #endif
1390e71b7053SJung-uk Kim break;
1391e71b7053SJung-uk Kim case OPT_MSG:
1392e71b7053SJung-uk Kim s_msg = 1;
1393e71b7053SJung-uk Kim break;
1394e71b7053SJung-uk Kim case OPT_MSGFILE:
1395e71b7053SJung-uk Kim bio_s_msg = BIO_new_file(opt_arg(), "w");
1396b077aed3SPierre Pronchery if (bio_s_msg == NULL) {
1397b077aed3SPierre Pronchery BIO_printf(bio_err, "Error writing file %s\n", opt_arg());
1398b077aed3SPierre Pronchery goto end;
1399b077aed3SPierre Pronchery }
1400e71b7053SJung-uk Kim break;
1401e71b7053SJung-uk Kim case OPT_TRACE:
1402e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SSL_TRACE
1403e71b7053SJung-uk Kim s_msg = 2;
1404e71b7053SJung-uk Kim #endif
1405e71b7053SJung-uk Kim break;
1406e71b7053SJung-uk Kim case OPT_SECURITY_DEBUG:
1407e71b7053SJung-uk Kim sdebug = 1;
1408e71b7053SJung-uk Kim break;
1409e71b7053SJung-uk Kim case OPT_SECURITY_DEBUG_VERBOSE:
1410e71b7053SJung-uk Kim sdebug = 2;
1411e71b7053SJung-uk Kim break;
1412e71b7053SJung-uk Kim case OPT_STATE:
1413e71b7053SJung-uk Kim state = 1;
1414e71b7053SJung-uk Kim break;
1415e71b7053SJung-uk Kim case OPT_CRLF:
1416e71b7053SJung-uk Kim s_crlf = 1;
1417e71b7053SJung-uk Kim break;
1418e71b7053SJung-uk Kim case OPT_QUIET:
1419e71b7053SJung-uk Kim s_quiet = 1;
1420e71b7053SJung-uk Kim break;
1421e71b7053SJung-uk Kim case OPT_BRIEF:
1422e71b7053SJung-uk Kim s_quiet = s_brief = verify_args.quiet = 1;
1423e71b7053SJung-uk Kim break;
1424e71b7053SJung-uk Kim case OPT_NO_DHE:
1425e71b7053SJung-uk Kim no_dhe = 1;
1426e71b7053SJung-uk Kim break;
1427e71b7053SJung-uk Kim case OPT_NO_RESUME_EPHEMERAL:
1428e71b7053SJung-uk Kim no_resume_ephemeral = 1;
1429e71b7053SJung-uk Kim break;
1430e71b7053SJung-uk Kim case OPT_PSK_IDENTITY:
1431e71b7053SJung-uk Kim psk_identity = opt_arg();
1432e71b7053SJung-uk Kim break;
1433e71b7053SJung-uk Kim case OPT_PSK_HINT:
1434e71b7053SJung-uk Kim #ifndef OPENSSL_NO_PSK
1435e71b7053SJung-uk Kim psk_identity_hint = opt_arg();
1436e71b7053SJung-uk Kim #endif
1437e71b7053SJung-uk Kim break;
1438e71b7053SJung-uk Kim case OPT_PSK:
1439e71b7053SJung-uk Kim for (p = psk_key = opt_arg(); *p; p++) {
1440e71b7053SJung-uk Kim if (isxdigit(_UC(*p)))
1441e71b7053SJung-uk Kim continue;
14426935a639SJung-uk Kim BIO_printf(bio_err, "Not a hex number '%s'\n", psk_key);
1443e71b7053SJung-uk Kim goto end;
1444e71b7053SJung-uk Kim }
1445e71b7053SJung-uk Kim break;
1446e71b7053SJung-uk Kim case OPT_PSK_SESS:
1447e71b7053SJung-uk Kim psksessf = opt_arg();
1448e71b7053SJung-uk Kim break;
1449e71b7053SJung-uk Kim case OPT_SRPVFILE:
1450e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SRP
1451e71b7053SJung-uk Kim srp_verifier_file = opt_arg();
1452e71b7053SJung-uk Kim if (min_version < TLS1_VERSION)
1453e71b7053SJung-uk Kim min_version = TLS1_VERSION;
1454e71b7053SJung-uk Kim #endif
1455e71b7053SJung-uk Kim break;
1456e71b7053SJung-uk Kim case OPT_SRPUSERSEED:
1457e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SRP
1458e71b7053SJung-uk Kim srpuserseed = opt_arg();
1459e71b7053SJung-uk Kim if (min_version < TLS1_VERSION)
1460e71b7053SJung-uk Kim min_version = TLS1_VERSION;
1461e71b7053SJung-uk Kim #endif
1462e71b7053SJung-uk Kim break;
1463e71b7053SJung-uk Kim case OPT_REV:
1464e71b7053SJung-uk Kim rev = 1;
1465e71b7053SJung-uk Kim break;
1466e71b7053SJung-uk Kim case OPT_WWW:
1467e71b7053SJung-uk Kim www = 1;
1468e71b7053SJung-uk Kim break;
1469e71b7053SJung-uk Kim case OPT_UPPER_WWW:
1470e71b7053SJung-uk Kim www = 2;
1471e71b7053SJung-uk Kim break;
1472e71b7053SJung-uk Kim case OPT_HTTP:
1473e71b7053SJung-uk Kim www = 3;
1474e71b7053SJung-uk Kim break;
1475e71b7053SJung-uk Kim case OPT_SSL_CONFIG:
1476e71b7053SJung-uk Kim ssl_config = opt_arg();
1477e71b7053SJung-uk Kim break;
1478e71b7053SJung-uk Kim case OPT_SSL3:
1479e71b7053SJung-uk Kim min_version = SSL3_VERSION;
1480e71b7053SJung-uk Kim max_version = SSL3_VERSION;
1481e71b7053SJung-uk Kim break;
1482e71b7053SJung-uk Kim case OPT_TLS1_3:
1483e71b7053SJung-uk Kim min_version = TLS1_3_VERSION;
1484e71b7053SJung-uk Kim max_version = TLS1_3_VERSION;
1485e71b7053SJung-uk Kim break;
1486e71b7053SJung-uk Kim case OPT_TLS1_2:
1487e71b7053SJung-uk Kim min_version = TLS1_2_VERSION;
1488e71b7053SJung-uk Kim max_version = TLS1_2_VERSION;
1489e71b7053SJung-uk Kim break;
1490e71b7053SJung-uk Kim case OPT_TLS1_1:
1491e71b7053SJung-uk Kim min_version = TLS1_1_VERSION;
1492e71b7053SJung-uk Kim max_version = TLS1_1_VERSION;
1493e71b7053SJung-uk Kim break;
1494e71b7053SJung-uk Kim case OPT_TLS1:
1495e71b7053SJung-uk Kim min_version = TLS1_VERSION;
1496e71b7053SJung-uk Kim max_version = TLS1_VERSION;
1497e71b7053SJung-uk Kim break;
1498e71b7053SJung-uk Kim case OPT_DTLS:
1499e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS
1500e71b7053SJung-uk Kim meth = DTLS_server_method();
1501e71b7053SJung-uk Kim socket_type = SOCK_DGRAM;
1502e71b7053SJung-uk Kim #endif
1503e71b7053SJung-uk Kim break;
1504e71b7053SJung-uk Kim case OPT_DTLS1:
1505e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS
1506e71b7053SJung-uk Kim meth = DTLS_server_method();
1507e71b7053SJung-uk Kim min_version = DTLS1_VERSION;
1508e71b7053SJung-uk Kim max_version = DTLS1_VERSION;
1509e71b7053SJung-uk Kim socket_type = SOCK_DGRAM;
1510e71b7053SJung-uk Kim #endif
1511e71b7053SJung-uk Kim break;
1512e71b7053SJung-uk Kim case OPT_DTLS1_2:
1513e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS
1514e71b7053SJung-uk Kim meth = DTLS_server_method();
1515e71b7053SJung-uk Kim min_version = DTLS1_2_VERSION;
1516e71b7053SJung-uk Kim max_version = DTLS1_2_VERSION;
1517e71b7053SJung-uk Kim socket_type = SOCK_DGRAM;
1518e71b7053SJung-uk Kim #endif
1519e71b7053SJung-uk Kim break;
1520e71b7053SJung-uk Kim case OPT_SCTP:
1521e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SCTP
1522e71b7053SJung-uk Kim protocol = IPPROTO_SCTP;
1523e71b7053SJung-uk Kim #endif
1524e71b7053SJung-uk Kim break;
15256935a639SJung-uk Kim case OPT_SCTP_LABEL_BUG:
15266935a639SJung-uk Kim #ifndef OPENSSL_NO_SCTP
15276935a639SJung-uk Kim sctp_label_bug = 1;
15286935a639SJung-uk Kim #endif
15296935a639SJung-uk Kim break;
1530e71b7053SJung-uk Kim case OPT_TIMEOUT:
1531e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS
1532e71b7053SJung-uk Kim enable_timeouts = 1;
1533e71b7053SJung-uk Kim #endif
1534e71b7053SJung-uk Kim break;
1535e71b7053SJung-uk Kim case OPT_MTU:
1536e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS
1537e71b7053SJung-uk Kim socket_mtu = atol(opt_arg());
1538e71b7053SJung-uk Kim #endif
1539e71b7053SJung-uk Kim break;
1540e71b7053SJung-uk Kim case OPT_LISTEN:
1541e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS
1542e71b7053SJung-uk Kim dtlslisten = 1;
1543e71b7053SJung-uk Kim #endif
1544e71b7053SJung-uk Kim break;
1545e71b7053SJung-uk Kim case OPT_STATELESS:
1546e71b7053SJung-uk Kim stateless = 1;
1547e71b7053SJung-uk Kim break;
1548e71b7053SJung-uk Kim case OPT_ID_PREFIX:
1549e71b7053SJung-uk Kim session_id_prefix = opt_arg();
1550e71b7053SJung-uk Kim break;
1551e71b7053SJung-uk Kim case OPT_ENGINE:
1552b077aed3SPierre Pronchery #ifndef OPENSSL_NO_ENGINE
1553b077aed3SPierre Pronchery engine = setup_engine(opt_arg(), s_debug);
1554b077aed3SPierre Pronchery #endif
1555e71b7053SJung-uk Kim break;
1556e71b7053SJung-uk Kim case OPT_R_CASES:
1557e71b7053SJung-uk Kim if (!opt_rand(o))
1558e71b7053SJung-uk Kim goto end;
1559e71b7053SJung-uk Kim break;
1560b077aed3SPierre Pronchery case OPT_PROV_CASES:
1561b077aed3SPierre Pronchery if (!opt_provider(o))
1562b077aed3SPierre Pronchery goto end;
1563b077aed3SPierre Pronchery break;
1564e71b7053SJung-uk Kim case OPT_SERVERNAME:
1565e71b7053SJung-uk Kim tlsextcbp.servername = opt_arg();
1566e71b7053SJung-uk Kim break;
1567e71b7053SJung-uk Kim case OPT_SERVERNAME_FATAL:
1568e71b7053SJung-uk Kim tlsextcbp.extension_error = SSL_TLSEXT_ERR_ALERT_FATAL;
1569e71b7053SJung-uk Kim break;
1570e71b7053SJung-uk Kim case OPT_CERT2:
1571e71b7053SJung-uk Kim s_cert_file2 = opt_arg();
1572e71b7053SJung-uk Kim break;
1573e71b7053SJung-uk Kim case OPT_KEY2:
1574e71b7053SJung-uk Kim s_key_file2 = opt_arg();
1575e71b7053SJung-uk Kim break;
1576e71b7053SJung-uk Kim case OPT_NEXTPROTONEG:
1577e71b7053SJung-uk Kim # ifndef OPENSSL_NO_NEXTPROTONEG
1578e71b7053SJung-uk Kim next_proto_neg_in = opt_arg();
1579e71b7053SJung-uk Kim #endif
1580e71b7053SJung-uk Kim break;
1581e71b7053SJung-uk Kim case OPT_ALPN:
1582e71b7053SJung-uk Kim alpn_in = opt_arg();
1583e71b7053SJung-uk Kim break;
1584e71b7053SJung-uk Kim case OPT_SRTP_PROFILES:
1585e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SRTP
1586e71b7053SJung-uk Kim srtp_profiles = opt_arg();
1587e71b7053SJung-uk Kim #endif
1588e71b7053SJung-uk Kim break;
1589e71b7053SJung-uk Kim case OPT_KEYMATEXPORT:
1590e71b7053SJung-uk Kim keymatexportlabel = opt_arg();
1591e71b7053SJung-uk Kim break;
1592e71b7053SJung-uk Kim case OPT_KEYMATEXPORTLEN:
1593e71b7053SJung-uk Kim keymatexportlen = atoi(opt_arg());
1594e71b7053SJung-uk Kim break;
1595e71b7053SJung-uk Kim case OPT_ASYNC:
1596e71b7053SJung-uk Kim async = 1;
1597e71b7053SJung-uk Kim break;
1598e71b7053SJung-uk Kim case OPT_MAX_SEND_FRAG:
1599e71b7053SJung-uk Kim max_send_fragment = atoi(opt_arg());
1600e71b7053SJung-uk Kim break;
1601e71b7053SJung-uk Kim case OPT_SPLIT_SEND_FRAG:
1602e71b7053SJung-uk Kim split_send_fragment = atoi(opt_arg());
1603e71b7053SJung-uk Kim break;
1604e71b7053SJung-uk Kim case OPT_MAX_PIPELINES:
1605e71b7053SJung-uk Kim max_pipelines = atoi(opt_arg());
1606e71b7053SJung-uk Kim break;
1607e71b7053SJung-uk Kim case OPT_READ_BUF:
1608e71b7053SJung-uk Kim read_buf_len = atoi(opt_arg());
1609e71b7053SJung-uk Kim break;
1610e71b7053SJung-uk Kim case OPT_KEYLOG_FILE:
1611e71b7053SJung-uk Kim keylog_file = opt_arg();
1612e71b7053SJung-uk Kim break;
1613e71b7053SJung-uk Kim case OPT_MAX_EARLY:
1614e71b7053SJung-uk Kim max_early_data = atoi(opt_arg());
1615e71b7053SJung-uk Kim if (max_early_data < 0) {
1616e71b7053SJung-uk Kim BIO_printf(bio_err, "Invalid value for max_early_data\n");
1617e71b7053SJung-uk Kim goto end;
1618e71b7053SJung-uk Kim }
1619e71b7053SJung-uk Kim break;
1620e71b7053SJung-uk Kim case OPT_RECV_MAX_EARLY:
1621e71b7053SJung-uk Kim recv_max_early_data = atoi(opt_arg());
1622e71b7053SJung-uk Kim if (recv_max_early_data < 0) {
1623e71b7053SJung-uk Kim BIO_printf(bio_err, "Invalid value for recv_max_early_data\n");
1624e71b7053SJung-uk Kim goto end;
1625e71b7053SJung-uk Kim }
1626e71b7053SJung-uk Kim break;
1627e71b7053SJung-uk Kim case OPT_EARLY_DATA:
1628e71b7053SJung-uk Kim early_data = 1;
1629e71b7053SJung-uk Kim if (max_early_data == -1)
1630e71b7053SJung-uk Kim max_early_data = SSL3_RT_MAX_PLAIN_LENGTH;
1631e71b7053SJung-uk Kim break;
1632b077aed3SPierre Pronchery case OPT_HTTP_SERVER_BINMODE:
1633b077aed3SPierre Pronchery http_server_binmode = 1;
1634b077aed3SPierre Pronchery break;
1635b077aed3SPierre Pronchery case OPT_NOCANAMES:
1636b077aed3SPierre Pronchery no_ca_names = 1;
1637b077aed3SPierre Pronchery break;
1638b077aed3SPierre Pronchery case OPT_SENDFILE:
1639b077aed3SPierre Pronchery #ifndef OPENSSL_NO_KTLS
1640b077aed3SPierre Pronchery use_sendfile = 1;
1641b077aed3SPierre Pronchery #endif
1642b077aed3SPierre Pronchery break;
1643b077aed3SPierre Pronchery case OPT_IGNORE_UNEXPECTED_EOF:
1644b077aed3SPierre Pronchery ignore_unexpected_eof = 1;
1645b077aed3SPierre Pronchery break;
1646e71b7053SJung-uk Kim }
1647e71b7053SJung-uk Kim }
1648b077aed3SPierre Pronchery
1649b077aed3SPierre Pronchery /* No extra arguments. */
1650e71b7053SJung-uk Kim argc = opt_num_rest();
1651b077aed3SPierre Pronchery if (argc != 0)
1652b077aed3SPierre Pronchery goto opthelp;
1653b077aed3SPierre Pronchery
1654b077aed3SPierre Pronchery if (!app_RAND_load())
1655b077aed3SPierre Pronchery goto end;
1656e71b7053SJung-uk Kim
1657e71b7053SJung-uk Kim #ifndef OPENSSL_NO_NEXTPROTONEG
1658e71b7053SJung-uk Kim if (min_version == TLS1_3_VERSION && next_proto_neg_in != NULL) {
1659e71b7053SJung-uk Kim BIO_printf(bio_err, "Cannot supply -nextprotoneg with TLSv1.3\n");
1660e71b7053SJung-uk Kim goto opthelp;
1661e71b7053SJung-uk Kim }
1662e71b7053SJung-uk Kim #endif
1663e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS
16646f9291ceSJung-uk Kim if (www && socket_type == SOCK_DGRAM) {
16656f9291ceSJung-uk Kim BIO_printf(bio_err, "Can't use -HTTP, -www or -WWW with DTLS\n");
1666a93cbc2bSJung-uk Kim goto end;
1667a93cbc2bSJung-uk Kim }
166874664626SKris Kennaway
1669e71b7053SJung-uk Kim if (dtlslisten && socket_type != SOCK_DGRAM) {
1670e71b7053SJung-uk Kim BIO_printf(bio_err, "Can only use -listen with DTLS\n");
16711f13597dSJung-uk Kim goto end;
16721f13597dSJung-uk Kim }
1673*e0c4386eSCy Schubert
1674*e0c4386eSCy Schubert if (rev && socket_type == SOCK_DGRAM) {
1675*e0c4386eSCy Schubert BIO_printf(bio_err, "Can't use -rev with DTLS\n");
1676*e0c4386eSCy Schubert goto end;
1677*e0c4386eSCy Schubert }
16781f13597dSJung-uk Kim #endif
16791f13597dSJung-uk Kim
1680e71b7053SJung-uk Kim if (stateless && socket_type != SOCK_STREAM) {
1681e71b7053SJung-uk Kim BIO_printf(bio_err, "Can only use --stateless with TLS\n");
1682aeb5019cSJung-uk Kim goto end;
1683aeb5019cSJung-uk Kim }
1684aeb5019cSJung-uk Kim
1685e71b7053SJung-uk Kim #ifdef AF_UNIX
1686e71b7053SJung-uk Kim if (socket_family == AF_UNIX && socket_type != SOCK_STREAM) {
1687e71b7053SJung-uk Kim BIO_printf(bio_err,
1688e71b7053SJung-uk Kim "Can't use unix sockets and datagrams together\n");
1689aeb5019cSJung-uk Kim goto end;
1690aeb5019cSJung-uk Kim }
1691e71b7053SJung-uk Kim #endif
1692c9cf7b5cSJung-uk Kim if (early_data && (www > 0 || rev)) {
1693c9cf7b5cSJung-uk Kim BIO_printf(bio_err,
1694c9cf7b5cSJung-uk Kim "Can't use -early_data in combination with -www, -WWW, -HTTP, or -rev\n");
1695c9cf7b5cSJung-uk Kim goto end;
1696c9cf7b5cSJung-uk Kim }
1697aeb5019cSJung-uk Kim
1698e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SCTP
1699e71b7053SJung-uk Kim if (protocol == IPPROTO_SCTP) {
1700e71b7053SJung-uk Kim if (socket_type != SOCK_DGRAM) {
1701e71b7053SJung-uk Kim BIO_printf(bio_err, "Can't use -sctp without DTLS\n");
1702e71b7053SJung-uk Kim goto end;
1703e71b7053SJung-uk Kim }
1704e71b7053SJung-uk Kim /* SCTP is unusual. It uses DTLS over a SOCK_STREAM protocol */
1705e71b7053SJung-uk Kim socket_type = SOCK_STREAM;
1706e71b7053SJung-uk Kim }
1707e71b7053SJung-uk Kim #endif
17085c87c606SMark Murray
1709b077aed3SPierre Pronchery #ifndef OPENSSL_NO_KTLS
1710b077aed3SPierre Pronchery if (use_sendfile && www <= 1) {
1711b077aed3SPierre Pronchery BIO_printf(bio_err, "Can't use -sendfile without -WWW or -HTTP\n");
1712b077aed3SPierre Pronchery goto end;
1713b077aed3SPierre Pronchery }
1714b077aed3SPierre Pronchery #endif
1715b077aed3SPierre Pronchery
1716e71b7053SJung-uk Kim if (!app_passwd(passarg, dpassarg, &pass, &dpass)) {
17173b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err, "Error getting password\n");
17183b4e3dcbSSimon L. B. Nielsen goto end;
17193b4e3dcbSSimon L. B. Nielsen }
17203b4e3dcbSSimon L. B. Nielsen
17213b4e3dcbSSimon L. B. Nielsen if (s_key_file == NULL)
17223b4e3dcbSSimon L. B. Nielsen s_key_file = s_cert_file;
1723e71b7053SJung-uk Kim
1724db522d3aSSimon L. B. Nielsen if (s_key_file2 == NULL)
1725db522d3aSSimon L. B. Nielsen s_key_file2 = s_cert_file2;
17263b4e3dcbSSimon L. B. Nielsen
1727e71b7053SJung-uk Kim if (!load_excert(&exc))
17287bded2dbSJung-uk Kim goto end;
17297bded2dbSJung-uk Kim
17306f9291ceSJung-uk Kim if (nocert == 0) {
1731e71b7053SJung-uk Kim s_key = load_key(s_key_file, s_key_format, 0, pass, engine,
1732b077aed3SPierre Pronchery "server certificate private key");
1733b077aed3SPierre Pronchery if (s_key == NULL)
17343b4e3dcbSSimon L. B. Nielsen goto end;
17353b4e3dcbSSimon L. B. Nielsen
1736b077aed3SPierre Pronchery s_cert = load_cert_pass(s_cert_file, s_cert_format, 1, pass,
1737b077aed3SPierre Pronchery "server certificate");
17383b4e3dcbSSimon L. B. Nielsen
1739b077aed3SPierre Pronchery if (s_cert == NULL)
17403b4e3dcbSSimon L. B. Nielsen goto end;
1741e71b7053SJung-uk Kim if (s_chain_file != NULL) {
1742b077aed3SPierre Pronchery if (!load_certs(s_chain_file, 0, &s_chain, NULL,
1743e71b7053SJung-uk Kim "server certificate chain"))
17447bded2dbSJung-uk Kim goto end;
17457bded2dbSJung-uk Kim }
1746e71b7053SJung-uk Kim
1747e71b7053SJung-uk Kim if (tlsextcbp.servername != NULL) {
1748e71b7053SJung-uk Kim s_key2 = load_key(s_key_file2, s_key_format, 0, pass, engine,
1749b077aed3SPierre Pronchery "second server certificate private key");
1750b077aed3SPierre Pronchery if (s_key2 == NULL)
1751db522d3aSSimon L. B. Nielsen goto end;
17523b4e3dcbSSimon L. B. Nielsen
1753b077aed3SPierre Pronchery s_cert2 = load_cert_pass(s_cert_file2, s_cert_format, 1, pass,
1754b077aed3SPierre Pronchery "second server certificate");
1755db522d3aSSimon L. B. Nielsen
1756b077aed3SPierre Pronchery if (s_cert2 == NULL)
1757db522d3aSSimon L. B. Nielsen goto end;
1758db522d3aSSimon L. B. Nielsen }
1759db522d3aSSimon L. B. Nielsen }
17607bded2dbSJung-uk Kim #if !defined(OPENSSL_NO_NEXTPROTONEG)
17616f9291ceSJung-uk Kim if (next_proto_neg_in) {
1762e71b7053SJung-uk Kim next_proto.data = next_protos_parse(&next_proto.len, next_proto_neg_in);
17631f13597dSJung-uk Kim if (next_proto.data == NULL)
17641f13597dSJung-uk Kim goto end;
17651f13597dSJung-uk Kim }
17661f13597dSJung-uk Kim #endif
17677bded2dbSJung-uk Kim alpn_ctx.data = NULL;
17687bded2dbSJung-uk Kim if (alpn_in) {
1769e71b7053SJung-uk Kim alpn_ctx.data = next_protos_parse(&alpn_ctx.len, alpn_in);
17707bded2dbSJung-uk Kim if (alpn_ctx.data == NULL)
17717bded2dbSJung-uk Kim goto end;
17727bded2dbSJung-uk Kim }
17737bded2dbSJung-uk Kim
1774e71b7053SJung-uk Kim if (crl_file != NULL) {
17757bded2dbSJung-uk Kim X509_CRL *crl;
1776b077aed3SPierre Pronchery crl = load_crl(crl_file, crl_format, 0, "CRL");
1777b077aed3SPierre Pronchery if (crl == NULL)
17787bded2dbSJung-uk Kim goto end;
17797bded2dbSJung-uk Kim crls = sk_X509_CRL_new_null();
1780e71b7053SJung-uk Kim if (crls == NULL || !sk_X509_CRL_push(crls, crl)) {
17817bded2dbSJung-uk Kim BIO_puts(bio_err, "Error adding CRL\n");
17827bded2dbSJung-uk Kim ERR_print_errors(bio_err);
17837bded2dbSJung-uk Kim X509_CRL_free(crl);
17847bded2dbSJung-uk Kim goto end;
17857bded2dbSJung-uk Kim }
17867bded2dbSJung-uk Kim }
17871f13597dSJung-uk Kim
1788e71b7053SJung-uk Kim if (s_dcert_file != NULL) {
17893b4e3dcbSSimon L. B. Nielsen
17903b4e3dcbSSimon L. B. Nielsen if (s_dkey_file == NULL)
17913b4e3dcbSSimon L. B. Nielsen s_dkey_file = s_dcert_file;
17923b4e3dcbSSimon L. B. Nielsen
1793e71b7053SJung-uk Kim s_dkey = load_key(s_dkey_file, s_dkey_format,
1794b077aed3SPierre Pronchery 0, dpass, engine, "second certificate private key");
1795b077aed3SPierre Pronchery if (s_dkey == NULL)
17963b4e3dcbSSimon L. B. Nielsen goto end;
17973b4e3dcbSSimon L. B. Nielsen
1798b077aed3SPierre Pronchery s_dcert = load_cert_pass(s_dcert_file, s_dcert_format, 1, dpass,
1799b077aed3SPierre Pronchery "second server certificate");
18003b4e3dcbSSimon L. B. Nielsen
1801e71b7053SJung-uk Kim if (s_dcert == NULL) {
18023b4e3dcbSSimon L. B. Nielsen ERR_print_errors(bio_err);
18033b4e3dcbSSimon L. B. Nielsen goto end;
18043b4e3dcbSSimon L. B. Nielsen }
1805e71b7053SJung-uk Kim if (s_dchain_file != NULL) {
1806b077aed3SPierre Pronchery if (!load_certs(s_dchain_file, 0, &s_dchain, NULL,
1807e71b7053SJung-uk Kim "second server certificate chain"))
18087bded2dbSJung-uk Kim goto end;
18097bded2dbSJung-uk Kim }
18103b4e3dcbSSimon L. B. Nielsen
18113b4e3dcbSSimon L. B. Nielsen }
18123b4e3dcbSSimon L. B. Nielsen
18136f9291ceSJung-uk Kim if (bio_s_out == NULL) {
18147bded2dbSJung-uk Kim if (s_quiet && !s_debug) {
181574664626SKris Kennaway bio_s_out = BIO_new(BIO_s_null());
1816b077aed3SPierre Pronchery if (s_msg && bio_s_msg == NULL) {
1817e71b7053SJung-uk Kim bio_s_msg = dup_bio_out(FORMAT_TEXT);
1818b077aed3SPierre Pronchery if (bio_s_msg == NULL) {
1819b077aed3SPierre Pronchery BIO_printf(bio_err, "Out of memory\n");
1820b077aed3SPierre Pronchery goto end;
1821b077aed3SPierre Pronchery }
1822b077aed3SPierre Pronchery }
18236f9291ceSJung-uk Kim } else {
1824e71b7053SJung-uk Kim bio_s_out = dup_bio_out(FORMAT_TEXT);
182574664626SKris Kennaway }
182674664626SKris Kennaway }
1827b077aed3SPierre Pronchery
1828b077aed3SPierre Pronchery if (bio_s_out == NULL)
1829b077aed3SPierre Pronchery goto end;
1830b077aed3SPierre Pronchery
1831b077aed3SPierre Pronchery if (nocert) {
183274664626SKris Kennaway s_cert_file = NULL;
183374664626SKris Kennaway s_key_file = NULL;
183474664626SKris Kennaway s_dcert_file = NULL;
183574664626SKris Kennaway s_dkey_file = NULL;
1836db522d3aSSimon L. B. Nielsen s_cert_file2 = NULL;
1837db522d3aSSimon L. B. Nielsen s_key_file2 = NULL;
183874664626SKris Kennaway }
183974664626SKris Kennaway
1840b077aed3SPierre Pronchery ctx = SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth);
18416f9291ceSJung-uk Kim if (ctx == NULL) {
184274664626SKris Kennaway ERR_print_errors(bio_err);
184374664626SKris Kennaway goto end;
184474664626SKris Kennaway }
1845e71b7053SJung-uk Kim
1846e71b7053SJung-uk Kim SSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY);
1847e71b7053SJung-uk Kim
1848e71b7053SJung-uk Kim if (sdebug)
1849e71b7053SJung-uk Kim ssl_ctx_security_debug(ctx, sdebug);
1850e71b7053SJung-uk Kim
1851e71b7053SJung-uk Kim if (!config_ctx(cctx, ssl_args, ctx))
1852e71b7053SJung-uk Kim goto end;
1853e71b7053SJung-uk Kim
1854e71b7053SJung-uk Kim if (ssl_config) {
1855e71b7053SJung-uk Kim if (SSL_CTX_config(ctx, ssl_config) == 0) {
1856e71b7053SJung-uk Kim BIO_printf(bio_err, "Error using configuration \"%s\"\n",
1857e71b7053SJung-uk Kim ssl_config);
1858e71b7053SJung-uk Kim ERR_print_errors(bio_err);
1859e71b7053SJung-uk Kim goto end;
1860e71b7053SJung-uk Kim }
1861e71b7053SJung-uk Kim }
18626935a639SJung-uk Kim #ifndef OPENSSL_NO_SCTP
18636935a639SJung-uk Kim if (protocol == IPPROTO_SCTP && sctp_label_bug == 1)
18646935a639SJung-uk Kim SSL_CTX_set_mode(ctx, SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG);
18656935a639SJung-uk Kim #endif
18666935a639SJung-uk Kim
1867e71b7053SJung-uk Kim if (min_version != 0
1868e71b7053SJung-uk Kim && SSL_CTX_set_min_proto_version(ctx, min_version) == 0)
1869e71b7053SJung-uk Kim goto end;
1870e71b7053SJung-uk Kim if (max_version != 0
1871e71b7053SJung-uk Kim && SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
1872e71b7053SJung-uk Kim goto end;
1873e71b7053SJung-uk Kim
18746f9291ceSJung-uk Kim if (session_id_prefix) {
18755c87c606SMark Murray if (strlen(session_id_prefix) >= 32)
18765c87c606SMark Murray BIO_printf(bio_err,
18775c87c606SMark Murray "warning: id_prefix is too long, only one new session will be possible\n");
18786f9291ceSJung-uk Kim if (!SSL_CTX_set_generate_session_id(ctx, generate_session_id)) {
18795c87c606SMark Murray BIO_printf(bio_err, "error setting 'id_prefix'\n");
18805c87c606SMark Murray ERR_print_errors(bio_err);
18815c87c606SMark Murray goto end;
18825c87c606SMark Murray }
18835c87c606SMark Murray BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
18845c87c606SMark Murray }
1885e71b7053SJung-uk Kim if (exc != NULL)
18867bded2dbSJung-uk Kim ssl_ctx_set_excert(ctx, exc);
188774664626SKris Kennaway
18886f9291ceSJung-uk Kim if (state)
18896f9291ceSJung-uk Kim SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
18906a599222SSimon L. B. Nielsen if (no_cache)
18916a599222SSimon L. B. Nielsen SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
18927bded2dbSJung-uk Kim else if (ext_cache)
18937bded2dbSJung-uk Kim init_session_cache_ctx(ctx);
18946a599222SSimon L. B. Nielsen else
189574664626SKris Kennaway SSL_CTX_sess_set_cache_size(ctx, 128);
189674664626SKris Kennaway
1897e71b7053SJung-uk Kim if (async) {
1898e71b7053SJung-uk Kim SSL_CTX_set_mode(ctx, SSL_MODE_ASYNC);
1899e71b7053SJung-uk Kim }
19001f13597dSJung-uk Kim
1901b077aed3SPierre Pronchery if (no_ca_names) {
1902b077aed3SPierre Pronchery SSL_CTX_set_options(ctx, SSL_OP_DISABLE_TLSEXT_CA_NAMES);
1903b077aed3SPierre Pronchery }
1904b077aed3SPierre Pronchery
1905b077aed3SPierre Pronchery if (ignore_unexpected_eof)
1906b077aed3SPierre Pronchery SSL_CTX_set_options(ctx, SSL_OP_IGNORE_UNEXPECTED_EOF);
1907b077aed3SPierre Pronchery
1908e71b7053SJung-uk Kim if (max_send_fragment > 0
1909e71b7053SJung-uk Kim && !SSL_CTX_set_max_send_fragment(ctx, max_send_fragment)) {
1910e71b7053SJung-uk Kim BIO_printf(bio_err, "%s: Max send fragment size %u is out of permitted range\n",
1911e71b7053SJung-uk Kim prog, max_send_fragment);
191274664626SKris Kennaway goto end;
191374664626SKris Kennaway }
1914e71b7053SJung-uk Kim
1915e71b7053SJung-uk Kim if (split_send_fragment > 0
1916e71b7053SJung-uk Kim && !SSL_CTX_set_split_send_fragment(ctx, split_send_fragment)) {
1917e71b7053SJung-uk Kim BIO_printf(bio_err, "%s: Split send fragment size %u is out of permitted range\n",
1918e71b7053SJung-uk Kim prog, split_send_fragment);
1919e71b7053SJung-uk Kim goto end;
1920e71b7053SJung-uk Kim }
1921e71b7053SJung-uk Kim if (max_pipelines > 0
1922e71b7053SJung-uk Kim && !SSL_CTX_set_max_pipelines(ctx, max_pipelines)) {
1923e71b7053SJung-uk Kim BIO_printf(bio_err, "%s: Max pipelines %u is out of permitted range\n",
1924e71b7053SJung-uk Kim prog, max_pipelines);
1925e71b7053SJung-uk Kim goto end;
1926e71b7053SJung-uk Kim }
1927e71b7053SJung-uk Kim
1928e71b7053SJung-uk Kim if (read_buf_len > 0) {
1929e71b7053SJung-uk Kim SSL_CTX_set_default_read_buffer_len(ctx, read_buf_len);
1930e71b7053SJung-uk Kim }
1931e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SRTP
1932e71b7053SJung-uk Kim if (srtp_profiles != NULL) {
1933e71b7053SJung-uk Kim /* Returns 0 on success! */
1934e71b7053SJung-uk Kim if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles) != 0) {
1935e71b7053SJung-uk Kim BIO_printf(bio_err, "Error setting SRTP profile\n");
1936e71b7053SJung-uk Kim ERR_print_errors(bio_err);
1937e71b7053SJung-uk Kim goto end;
1938e71b7053SJung-uk Kim }
1939e71b7053SJung-uk Kim }
194074664626SKris Kennaway #endif
194174664626SKris Kennaway
1942b077aed3SPierre Pronchery if (!ctx_set_verify_locations(ctx, CAfile, noCAfile, CApath, noCApath,
1943b077aed3SPierre Pronchery CAstore, noCAstore)) {
194474664626SKris Kennaway ERR_print_errors(bio_err);
1945e71b7053SJung-uk Kim goto end;
194674664626SKris Kennaway }
1947e71b7053SJung-uk Kim if (vpmtouched && !SSL_CTX_set1_param(ctx, vpm)) {
1948e71b7053SJung-uk Kim BIO_printf(bio_err, "Error setting verify params\n");
1949e71b7053SJung-uk Kim ERR_print_errors(bio_err);
1950e71b7053SJung-uk Kim goto end;
1951e71b7053SJung-uk Kim }
19521f13597dSJung-uk Kim
19537bded2dbSJung-uk Kim ssl_ctx_add_crls(ctx, crls, 0);
19547bded2dbSJung-uk Kim
1955b077aed3SPierre Pronchery if (!ssl_load_stores(ctx,
1956b077aed3SPierre Pronchery vfyCApath, vfyCAfile, vfyCAstore,
1957b077aed3SPierre Pronchery chCApath, chCAfile, chCAstore,
19587bded2dbSJung-uk Kim crls, crl_download)) {
19597bded2dbSJung-uk Kim BIO_printf(bio_err, "Error loading store locations\n");
19607bded2dbSJung-uk Kim ERR_print_errors(bio_err);
19617bded2dbSJung-uk Kim goto end;
19627bded2dbSJung-uk Kim }
1963e71b7053SJung-uk Kim
19646f9291ceSJung-uk Kim if (s_cert2) {
1965b077aed3SPierre Pronchery ctx2 = SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth);
19666f9291ceSJung-uk Kim if (ctx2 == NULL) {
1967db522d3aSSimon L. B. Nielsen ERR_print_errors(bio_err);
1968db522d3aSSimon L. B. Nielsen goto end;
1969db522d3aSSimon L. B. Nielsen }
1970db522d3aSSimon L. B. Nielsen }
1971db522d3aSSimon L. B. Nielsen
1972e71b7053SJung-uk Kim if (ctx2 != NULL) {
1973db522d3aSSimon L. B. Nielsen BIO_printf(bio_s_out, "Setting secondary ctx parameters\n");
1974db522d3aSSimon L. B. Nielsen
1975e71b7053SJung-uk Kim if (sdebug)
197611c7efe3SJung-uk Kim ssl_ctx_security_debug(ctx2, sdebug);
1977e71b7053SJung-uk Kim
19786f9291ceSJung-uk Kim if (session_id_prefix) {
1979db522d3aSSimon L. B. Nielsen if (strlen(session_id_prefix) >= 32)
1980db522d3aSSimon L. B. Nielsen BIO_printf(bio_err,
1981db522d3aSSimon L. B. Nielsen "warning: id_prefix is too long, only one new session will be possible\n");
19826f9291ceSJung-uk Kim if (!SSL_CTX_set_generate_session_id(ctx2, generate_session_id)) {
1983db522d3aSSimon L. B. Nielsen BIO_printf(bio_err, "error setting 'id_prefix'\n");
1984db522d3aSSimon L. B. Nielsen ERR_print_errors(bio_err);
1985db522d3aSSimon L. B. Nielsen goto end;
1986db522d3aSSimon L. B. Nielsen }
1987db522d3aSSimon L. B. Nielsen BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
1988db522d3aSSimon L. B. Nielsen }
1989e71b7053SJung-uk Kim if (exc != NULL)
19907bded2dbSJung-uk Kim ssl_ctx_set_excert(ctx2, exc);
1991db522d3aSSimon L. B. Nielsen
19926f9291ceSJung-uk Kim if (state)
19936f9291ceSJung-uk Kim SSL_CTX_set_info_callback(ctx2, apps_ssl_info_callback);
1994db522d3aSSimon L. B. Nielsen
19956a599222SSimon L. B. Nielsen if (no_cache)
19966a599222SSimon L. B. Nielsen SSL_CTX_set_session_cache_mode(ctx2, SSL_SESS_CACHE_OFF);
19977bded2dbSJung-uk Kim else if (ext_cache)
19987bded2dbSJung-uk Kim init_session_cache_ctx(ctx2);
19996a599222SSimon L. B. Nielsen else
2000db522d3aSSimon L. B. Nielsen SSL_CTX_sess_set_cache_size(ctx2, 128);
2001db522d3aSSimon L. B. Nielsen
2002e71b7053SJung-uk Kim if (async)
2003e71b7053SJung-uk Kim SSL_CTX_set_mode(ctx2, SSL_MODE_ASYNC);
2004e71b7053SJung-uk Kim
2005b077aed3SPierre Pronchery if (!ctx_set_verify_locations(ctx2, CAfile, noCAfile, CApath,
2006b077aed3SPierre Pronchery noCApath, CAstore, noCAstore)) {
2007db522d3aSSimon L. B. Nielsen ERR_print_errors(bio_err);
2008e71b7053SJung-uk Kim goto end;
2009db522d3aSSimon L. B. Nielsen }
2010e71b7053SJung-uk Kim if (vpmtouched && !SSL_CTX_set1_param(ctx2, vpm)) {
2011e71b7053SJung-uk Kim BIO_printf(bio_err, "Error setting verify params\n");
2012e71b7053SJung-uk Kim ERR_print_errors(bio_err);
2013e71b7053SJung-uk Kim goto end;
2014e71b7053SJung-uk Kim }
20157bded2dbSJung-uk Kim
20167bded2dbSJung-uk Kim ssl_ctx_add_crls(ctx2, crls, 0);
2017e71b7053SJung-uk Kim if (!config_ctx(cctx, ssl_args, ctx2))
20187bded2dbSJung-uk Kim goto end;
2019db522d3aSSimon L. B. Nielsen }
20201f13597dSJung-uk Kim #ifndef OPENSSL_NO_NEXTPROTONEG
20211f13597dSJung-uk Kim if (next_proto.data)
20226f9291ceSJung-uk Kim SSL_CTX_set_next_protos_advertised_cb(ctx, next_proto_cb,
20236f9291ceSJung-uk Kim &next_proto);
20241f13597dSJung-uk Kim #endif
20257bded2dbSJung-uk Kim if (alpn_ctx.data)
20267bded2dbSJung-uk Kim SSL_CTX_set_alpn_select_cb(ctx, alpn_cb, &alpn_ctx);
202774664626SKris Kennaway
20286f9291ceSJung-uk Kim if (!no_dhe) {
2029b077aed3SPierre Pronchery EVP_PKEY *dhpkey = NULL;
20305c87c606SMark Murray
2031e71b7053SJung-uk Kim if (dhfile != NULL)
2032b077aed3SPierre Pronchery dhpkey = load_keyparams(dhfile, FORMAT_UNDEF, 0, "DH", "DH parameters");
2033e71b7053SJung-uk Kim else if (s_cert_file != NULL)
2034b077aed3SPierre Pronchery dhpkey = load_keyparams_suppress(s_cert_file, FORMAT_UNDEF, 0, "DH",
2035b077aed3SPierre Pronchery "DH parameters", 1);
20365c87c606SMark Murray
2037b077aed3SPierre Pronchery if (dhpkey != NULL) {
203874664626SKris Kennaway BIO_printf(bio_s_out, "Setting temp DH parameters\n");
20396f9291ceSJung-uk Kim } else {
204074664626SKris Kennaway BIO_printf(bio_s_out, "Using default temp DH parameters\n");
204174664626SKris Kennaway }
204274664626SKris Kennaway (void)BIO_flush(bio_s_out);
204374664626SKris Kennaway
2044b077aed3SPierre Pronchery if (dhpkey == NULL) {
2045e71b7053SJung-uk Kim SSL_CTX_set_dh_auto(ctx, 1);
2046b077aed3SPierre Pronchery } else {
2047b077aed3SPierre Pronchery /*
2048b077aed3SPierre Pronchery * We need 2 references: one for use by ctx and one for use by
2049b077aed3SPierre Pronchery * ctx2
2050b077aed3SPierre Pronchery */
2051b077aed3SPierre Pronchery if (!EVP_PKEY_up_ref(dhpkey)) {
2052b077aed3SPierre Pronchery EVP_PKEY_free(dhpkey);
2053b077aed3SPierre Pronchery goto end;
2054b077aed3SPierre Pronchery }
2055b077aed3SPierre Pronchery if (!SSL_CTX_set0_tmp_dh_pkey(ctx, dhpkey)) {
2056e71b7053SJung-uk Kim BIO_puts(bio_err, "Error setting temp DH parameters\n");
2057e71b7053SJung-uk Kim ERR_print_errors(bio_err);
2058b077aed3SPierre Pronchery /* Free 2 references */
2059b077aed3SPierre Pronchery EVP_PKEY_free(dhpkey);
2060b077aed3SPierre Pronchery EVP_PKEY_free(dhpkey);
2061e71b7053SJung-uk Kim goto end;
2062e71b7053SJung-uk Kim }
2063b077aed3SPierre Pronchery }
2064e71b7053SJung-uk Kim
2065e71b7053SJung-uk Kim if (ctx2 != NULL) {
2066b077aed3SPierre Pronchery if (dhfile != NULL) {
2067b077aed3SPierre Pronchery EVP_PKEY *dhpkey2 = load_keyparams_suppress(s_cert_file2,
2068b077aed3SPierre Pronchery FORMAT_UNDEF,
2069b077aed3SPierre Pronchery 0, "DH",
2070b077aed3SPierre Pronchery "DH parameters", 1);
2071b077aed3SPierre Pronchery
2072b077aed3SPierre Pronchery if (dhpkey2 != NULL) {
2073db522d3aSSimon L. B. Nielsen BIO_printf(bio_s_out, "Setting temp DH parameters\n");
2074db522d3aSSimon L. B. Nielsen (void)BIO_flush(bio_s_out);
2075db522d3aSSimon L. B. Nielsen
2076b077aed3SPierre Pronchery EVP_PKEY_free(dhpkey);
2077b077aed3SPierre Pronchery dhpkey = dhpkey2;
2078db522d3aSSimon L. B. Nielsen }
2079db522d3aSSimon L. B. Nielsen }
2080b077aed3SPierre Pronchery if (dhpkey == NULL) {
2081e71b7053SJung-uk Kim SSL_CTX_set_dh_auto(ctx2, 1);
2082b077aed3SPierre Pronchery } else if (!SSL_CTX_set0_tmp_dh_pkey(ctx2, dhpkey)) {
2083e71b7053SJung-uk Kim BIO_puts(bio_err, "Error setting temp DH parameters\n");
2084e71b7053SJung-uk Kim ERR_print_errors(bio_err);
2085b077aed3SPierre Pronchery EVP_PKEY_free(dhpkey);
2086e71b7053SJung-uk Kim goto end;
2087db522d3aSSimon L. B. Nielsen }
2088b077aed3SPierre Pronchery dhpkey = NULL;
2089e71b7053SJung-uk Kim }
2090b077aed3SPierre Pronchery EVP_PKEY_free(dhpkey);
209174664626SKris Kennaway }
209274664626SKris Kennaway
20937bded2dbSJung-uk Kim if (!set_cert_key_stuff(ctx, s_cert, s_key, s_chain, build_chain))
20943b4e3dcbSSimon L. B. Nielsen goto end;
2095e71b7053SJung-uk Kim
20967bded2dbSJung-uk Kim if (s_serverinfo_file != NULL
20977bded2dbSJung-uk Kim && !SSL_CTX_use_serverinfo_file(ctx, s_serverinfo_file)) {
20987bded2dbSJung-uk Kim ERR_print_errors(bio_err);
20997bded2dbSJung-uk Kim goto end;
21007bded2dbSJung-uk Kim }
2101e71b7053SJung-uk Kim
2102e71b7053SJung-uk Kim if (ctx2 != NULL
2103e71b7053SJung-uk Kim && !set_cert_key_stuff(ctx2, s_cert2, s_key2, NULL, build_chain))
2104db522d3aSSimon L. B. Nielsen goto end;
2105e71b7053SJung-uk Kim
21066f9291ceSJung-uk Kim if (s_dcert != NULL) {
21077bded2dbSJung-uk Kim if (!set_cert_key_stuff(ctx, s_dcert, s_dkey, s_dchain, build_chain))
210874664626SKris Kennaway goto end;
210974664626SKris Kennaway }
211074664626SKris Kennaway
2111e71b7053SJung-uk Kim if (no_resume_ephemeral) {
2112e71b7053SJung-uk Kim SSL_CTX_set_not_resumable_session_callback(ctx,
2113e71b7053SJung-uk Kim not_resumable_sess_cb);
211474664626SKris Kennaway
2115e71b7053SJung-uk Kim if (ctx2 != NULL)
2116e71b7053SJung-uk Kim SSL_CTX_set_not_resumable_session_callback(ctx2,
2117e71b7053SJung-uk Kim not_resumable_sess_cb);
211874664626SKris Kennaway }
21191f13597dSJung-uk Kim #ifndef OPENSSL_NO_PSK
2120e71b7053SJung-uk Kim if (psk_key != NULL) {
21211f13597dSJung-uk Kim if (s_debug)
2122e71b7053SJung-uk Kim BIO_printf(bio_s_out, "PSK key given, setting server callback\n");
21231f13597dSJung-uk Kim SSL_CTX_set_psk_server_callback(ctx, psk_server_cb);
21241f13597dSJung-uk Kim }
21251f13597dSJung-uk Kim
2126b077aed3SPierre Pronchery if (psk_identity_hint != NULL) {
2127b077aed3SPierre Pronchery if (min_version == TLS1_3_VERSION) {
2128b077aed3SPierre Pronchery BIO_printf(bio_s_out, "PSK warning: there is NO identity hint in TLSv1.3\n");
2129b077aed3SPierre Pronchery } else {
21306f9291ceSJung-uk Kim if (!SSL_CTX_use_psk_identity_hint(ctx, psk_identity_hint)) {
21311f13597dSJung-uk Kim BIO_printf(bio_err, "error setting PSK identity hint to context\n");
21321f13597dSJung-uk Kim ERR_print_errors(bio_err);
21331f13597dSJung-uk Kim goto end;
21341f13597dSJung-uk Kim }
2135b077aed3SPierre Pronchery }
2136b077aed3SPierre Pronchery }
21371f13597dSJung-uk Kim #endif
2138e71b7053SJung-uk Kim if (psksessf != NULL) {
2139e71b7053SJung-uk Kim BIO *stmp = BIO_new_file(psksessf, "r");
2140e71b7053SJung-uk Kim
2141e71b7053SJung-uk Kim if (stmp == NULL) {
2142e71b7053SJung-uk Kim BIO_printf(bio_err, "Can't open PSK session file %s\n", psksessf);
2143e71b7053SJung-uk Kim ERR_print_errors(bio_err);
2144e71b7053SJung-uk Kim goto end;
2145e71b7053SJung-uk Kim }
2146e71b7053SJung-uk Kim psksess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);
2147e71b7053SJung-uk Kim BIO_free(stmp);
2148e71b7053SJung-uk Kim if (psksess == NULL) {
2149e71b7053SJung-uk Kim BIO_printf(bio_err, "Can't read PSK session file %s\n", psksessf);
2150e71b7053SJung-uk Kim ERR_print_errors(bio_err);
2151e71b7053SJung-uk Kim goto end;
2152e71b7053SJung-uk Kim }
2153e71b7053SJung-uk Kim
2154e71b7053SJung-uk Kim }
2155e71b7053SJung-uk Kim
2156e71b7053SJung-uk Kim if (psk_key != NULL || psksess != NULL)
2157e71b7053SJung-uk Kim SSL_CTX_set_psk_find_session_callback(ctx, psk_find_session_cb);
21581f13597dSJung-uk Kim
215974664626SKris Kennaway SSL_CTX_set_verify(ctx, s_server_verify, verify_callback);
2160e71b7053SJung-uk Kim if (!SSL_CTX_set_session_id_context(ctx,
2161e71b7053SJung-uk Kim (void *)&s_server_session_id_context,
2162e71b7053SJung-uk Kim sizeof(s_server_session_id_context))) {
2163e71b7053SJung-uk Kim BIO_printf(bio_err, "error setting session id context\n");
2164e71b7053SJung-uk Kim ERR_print_errors(bio_err);
2165e71b7053SJung-uk Kim goto end;
2166e71b7053SJung-uk Kim }
216774664626SKris Kennaway
21686a599222SSimon L. B. Nielsen /* Set DTLS cookie generation and verification callbacks */
21696a599222SSimon L. B. Nielsen SSL_CTX_set_cookie_generate_cb(ctx, generate_cookie_callback);
21706a599222SSimon L. B. Nielsen SSL_CTX_set_cookie_verify_cb(ctx, verify_cookie_callback);
21716a599222SSimon L. B. Nielsen
2172e71b7053SJung-uk Kim /* Set TLS1.3 cookie generation and verification callbacks */
2173e71b7053SJung-uk Kim SSL_CTX_set_stateless_cookie_generate_cb(ctx, generate_stateless_cookie_callback);
2174e71b7053SJung-uk Kim SSL_CTX_set_stateless_cookie_verify_cb(ctx, verify_stateless_cookie_callback);
217574664626SKris Kennaway
2176e71b7053SJung-uk Kim if (ctx2 != NULL) {
2177e71b7053SJung-uk Kim SSL_CTX_set_verify(ctx2, s_server_verify, verify_callback);
2178e71b7053SJung-uk Kim if (!SSL_CTX_set_session_id_context(ctx2,
2179e71b7053SJung-uk Kim (void *)&s_server_session_id_context,
2180e71b7053SJung-uk Kim sizeof(s_server_session_id_context))) {
2181e71b7053SJung-uk Kim BIO_printf(bio_err, "error setting session id context\n");
2182e71b7053SJung-uk Kim ERR_print_errors(bio_err);
2183e71b7053SJung-uk Kim goto end;
2184e71b7053SJung-uk Kim }
2185db522d3aSSimon L. B. Nielsen tlsextcbp.biodebug = bio_s_out;
2186db522d3aSSimon L. B. Nielsen SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb);
2187db522d3aSSimon L. B. Nielsen SSL_CTX_set_tlsext_servername_arg(ctx2, &tlsextcbp);
2188db522d3aSSimon L. B. Nielsen SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
2189db522d3aSSimon L. B. Nielsen SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
2190db522d3aSSimon L. B. Nielsen }
21911f13597dSJung-uk Kim
21921f13597dSJung-uk Kim #ifndef OPENSSL_NO_SRP
21936f9291ceSJung-uk Kim if (srp_verifier_file != NULL) {
2194b077aed3SPierre Pronchery if (!set_up_srp_verifier_file(ctx, &srp_callback_parm, srpuserseed,
2195b077aed3SPierre Pronchery srp_verifier_file))
21961f13597dSJung-uk Kim goto end;
21976f9291ceSJung-uk Kim } else
21981f13597dSJung-uk Kim #endif
21996f9291ceSJung-uk Kim if (CAfile != NULL) {
2200db522d3aSSimon L. B. Nielsen SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(CAfile));
2201e71b7053SJung-uk Kim
2202db522d3aSSimon L. B. Nielsen if (ctx2)
2203db522d3aSSimon L. B. Nielsen SSL_CTX_set_client_CA_list(ctx2, SSL_load_client_CA_file(CAfile));
2204db522d3aSSimon L. B. Nielsen }
2205e71b7053SJung-uk Kim #ifndef OPENSSL_NO_OCSP
2206e71b7053SJung-uk Kim if (s_tlsextstatus) {
2207e71b7053SJung-uk Kim SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb);
2208e71b7053SJung-uk Kim SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp);
2209e71b7053SJung-uk Kim if (ctx2) {
2210e71b7053SJung-uk Kim SSL_CTX_set_tlsext_status_cb(ctx2, cert_status_cb);
2211e71b7053SJung-uk Kim SSL_CTX_set_tlsext_status_arg(ctx2, &tlscstatp);
2212e71b7053SJung-uk Kim }
2213e71b7053SJung-uk Kim }
2214e71b7053SJung-uk Kim #endif
2215e71b7053SJung-uk Kim if (set_keylog_file(ctx, keylog_file))
2216e71b7053SJung-uk Kim goto end;
22171f13597dSJung-uk Kim
2218e71b7053SJung-uk Kim if (max_early_data >= 0)
2219e71b7053SJung-uk Kim SSL_CTX_set_max_early_data(ctx, max_early_data);
2220e71b7053SJung-uk Kim if (recv_max_early_data >= 0)
2221e71b7053SJung-uk Kim SSL_CTX_set_recv_max_early_data(ctx, recv_max_early_data);
2222e71b7053SJung-uk Kim
22237bded2dbSJung-uk Kim if (rev)
2224e71b7053SJung-uk Kim server_cb = rev_body;
22257bded2dbSJung-uk Kim else if (www)
2226e71b7053SJung-uk Kim server_cb = www_body;
222774664626SKris Kennaway else
2228e71b7053SJung-uk Kim server_cb = sv_body;
2229e71b7053SJung-uk Kim #ifdef AF_UNIX
2230e71b7053SJung-uk Kim if (socket_family == AF_UNIX
2231e71b7053SJung-uk Kim && unlink_unix_path)
2232e71b7053SJung-uk Kim unlink(host);
2233e71b7053SJung-uk Kim #endif
2234e71b7053SJung-uk Kim do_server(&accept_socket, host, port, socket_family, socket_type, protocol,
2235e71b7053SJung-uk Kim server_cb, context, naccept, bio_s_out);
223674664626SKris Kennaway print_stats(bio_s_out, ctx);
223774664626SKris Kennaway ret = 0;
223874664626SKris Kennaway end:
22396f9291ceSJung-uk Kim SSL_CTX_free(ctx);
2240e71b7053SJung-uk Kim SSL_SESSION_free(psksess);
2241e71b7053SJung-uk Kim set_keylog_file(NULL, NULL);
22423b4e3dcbSSimon L. B. Nielsen X509_free(s_cert);
22437bded2dbSJung-uk Kim sk_X509_CRL_pop_free(crls, X509_CRL_free);
22443b4e3dcbSSimon L. B. Nielsen X509_free(s_dcert);
22453b4e3dcbSSimon L. B. Nielsen EVP_PKEY_free(s_key);
22463b4e3dcbSSimon L. B. Nielsen EVP_PKEY_free(s_dkey);
22477bded2dbSJung-uk Kim sk_X509_pop_free(s_chain, X509_free);
22487bded2dbSJung-uk Kim sk_X509_pop_free(s_dchain, X509_free);
22493b4e3dcbSSimon L. B. Nielsen OPENSSL_free(pass);
22503b4e3dcbSSimon L. B. Nielsen OPENSSL_free(dpass);
2251e71b7053SJung-uk Kim OPENSSL_free(host);
2252e71b7053SJung-uk Kim OPENSSL_free(port);
225309286989SJung-uk Kim X509_VERIFY_PARAM_free(vpm);
22547bded2dbSJung-uk Kim free_sessions();
225509286989SJung-uk Kim OPENSSL_free(tlscstatp.host);
225609286989SJung-uk Kim OPENSSL_free(tlscstatp.port);
225709286989SJung-uk Kim OPENSSL_free(tlscstatp.path);
22586f9291ceSJung-uk Kim SSL_CTX_free(ctx2);
2259db522d3aSSimon L. B. Nielsen X509_free(s_cert2);
2260db522d3aSSimon L. B. Nielsen EVP_PKEY_free(s_key2);
22617bded2dbSJung-uk Kim #ifndef OPENSSL_NO_NEXTPROTONEG
22627bded2dbSJung-uk Kim OPENSSL_free(next_proto.data);
22637bded2dbSJung-uk Kim #endif
22647bded2dbSJung-uk Kim OPENSSL_free(alpn_ctx.data);
22657bded2dbSJung-uk Kim ssl_excert_free(exc);
22667bded2dbSJung-uk Kim sk_OPENSSL_STRING_free(ssl_args);
22677bded2dbSJung-uk Kim SSL_CONF_CTX_free(cctx);
2268e71b7053SJung-uk Kim release_engine(engine);
226974664626SKris Kennaway BIO_free(bio_s_out);
227074664626SKris Kennaway bio_s_out = NULL;
22717bded2dbSJung-uk Kim BIO_free(bio_s_msg);
22727bded2dbSJung-uk Kim bio_s_msg = NULL;
2273e71b7053SJung-uk Kim #ifdef CHARSET_EBCDIC
2274e71b7053SJung-uk Kim BIO_meth_free(methods_ebcdic);
2275e71b7053SJung-uk Kim #endif
2276e71b7053SJung-uk Kim return ret;
227774664626SKris Kennaway }
227874664626SKris Kennaway
print_stats(BIO * bio,SSL_CTX * ssl_ctx)227974664626SKris Kennaway static void print_stats(BIO *bio, SSL_CTX *ssl_ctx)
228074664626SKris Kennaway {
228174664626SKris Kennaway BIO_printf(bio, "%4ld items in the session cache\n",
228274664626SKris Kennaway SSL_CTX_sess_number(ssl_ctx));
22833b4e3dcbSSimon L. B. Nielsen BIO_printf(bio, "%4ld client connects (SSL_connect())\n",
228474664626SKris Kennaway SSL_CTX_sess_connect(ssl_ctx));
22853b4e3dcbSSimon L. B. Nielsen BIO_printf(bio, "%4ld client renegotiates (SSL_connect())\n",
228674664626SKris Kennaway SSL_CTX_sess_connect_renegotiate(ssl_ctx));
22873b4e3dcbSSimon L. B. Nielsen BIO_printf(bio, "%4ld client connects that finished\n",
228874664626SKris Kennaway SSL_CTX_sess_connect_good(ssl_ctx));
22893b4e3dcbSSimon L. B. Nielsen BIO_printf(bio, "%4ld server accepts (SSL_accept())\n",
229074664626SKris Kennaway SSL_CTX_sess_accept(ssl_ctx));
22913b4e3dcbSSimon L. B. Nielsen BIO_printf(bio, "%4ld server renegotiates (SSL_accept())\n",
229274664626SKris Kennaway SSL_CTX_sess_accept_renegotiate(ssl_ctx));
22933b4e3dcbSSimon L. B. Nielsen BIO_printf(bio, "%4ld server accepts that finished\n",
229474664626SKris Kennaway SSL_CTX_sess_accept_good(ssl_ctx));
22953b4e3dcbSSimon L. B. Nielsen BIO_printf(bio, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx));
22966f9291ceSJung-uk Kim BIO_printf(bio, "%4ld session cache misses\n",
22976f9291ceSJung-uk Kim SSL_CTX_sess_misses(ssl_ctx));
22986f9291ceSJung-uk Kim BIO_printf(bio, "%4ld session cache timeouts\n",
22996f9291ceSJung-uk Kim SSL_CTX_sess_timeouts(ssl_ctx));
23006f9291ceSJung-uk Kim BIO_printf(bio, "%4ld callback cache hits\n",
23016f9291ceSJung-uk Kim SSL_CTX_sess_cb_hits(ssl_ctx));
23023b4e3dcbSSimon L. B. Nielsen BIO_printf(bio, "%4ld cache full overflows (%ld allowed)\n",
230374664626SKris Kennaway SSL_CTX_sess_cache_full(ssl_ctx),
230474664626SKris Kennaway SSL_CTX_sess_get_cache_size(ssl_ctx));
230574664626SKris Kennaway }
230674664626SKris Kennaway
count_reads_callback(BIO * bio,int cmd,const char * argp,size_t len,int argi,long argl,int ret,size_t * processed)2307b077aed3SPierre Pronchery static long int count_reads_callback(BIO *bio, int cmd, const char *argp, size_t len,
2308b077aed3SPierre Pronchery int argi, long argl, int ret, size_t *processed)
230983eaf7aeSJung-uk Kim {
231083eaf7aeSJung-uk Kim unsigned int *p_counter = (unsigned int *)BIO_get_callback_arg(bio);
231183eaf7aeSJung-uk Kim
231283eaf7aeSJung-uk Kim switch (cmd) {
231383eaf7aeSJung-uk Kim case BIO_CB_READ: /* No break here */
231483eaf7aeSJung-uk Kim case BIO_CB_GETS:
231583eaf7aeSJung-uk Kim if (p_counter != NULL)
231683eaf7aeSJung-uk Kim ++*p_counter;
231783eaf7aeSJung-uk Kim break;
231883eaf7aeSJung-uk Kim default:
231983eaf7aeSJung-uk Kim break;
232083eaf7aeSJung-uk Kim }
232183eaf7aeSJung-uk Kim
232283eaf7aeSJung-uk Kim if (s_debug) {
232383eaf7aeSJung-uk Kim BIO_set_callback_arg(bio, (char *)bio_s_out);
2324b077aed3SPierre Pronchery ret = (int)bio_dump_callback(bio, cmd, argp, len, argi, argl, ret, processed);
232583eaf7aeSJung-uk Kim BIO_set_callback_arg(bio, (char *)p_counter);
232683eaf7aeSJung-uk Kim }
232783eaf7aeSJung-uk Kim
232883eaf7aeSJung-uk Kim return ret;
232983eaf7aeSJung-uk Kim }
233083eaf7aeSJung-uk Kim
sv_body(int s,int stype,int prot,unsigned char * context)2331e71b7053SJung-uk Kim static int sv_body(int s, int stype, int prot, unsigned char *context)
233274664626SKris Kennaway {
233374664626SKris Kennaway char *buf = NULL;
233474664626SKris Kennaway fd_set readfds;
233574664626SKris Kennaway int ret = 1, width;
233674664626SKris Kennaway int k, i;
233774664626SKris Kennaway unsigned long l;
233874664626SKris Kennaway SSL *con = NULL;
233974664626SKris Kennaway BIO *sbio;
23406a599222SSimon L. B. Nielsen struct timeval timeout;
2341e71b7053SJung-uk Kim #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS))
23426a599222SSimon L. B. Nielsen struct timeval *timeoutp;
2343f579bf8eSKris Kennaway #endif
2344e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS
2345e71b7053SJung-uk Kim # ifndef OPENSSL_NO_SCTP
2346e71b7053SJung-uk Kim int isdtls = (stype == SOCK_DGRAM || prot == IPPROTO_SCTP);
2347e71b7053SJung-uk Kim # else
2348e71b7053SJung-uk Kim int isdtls = (stype == SOCK_DGRAM);
2349e71b7053SJung-uk Kim # endif
235074664626SKris Kennaway #endif
235174664626SKris Kennaway
2352e71b7053SJung-uk Kim buf = app_malloc(bufsize, "server buffer");
2353e71b7053SJung-uk Kim if (s_nbio) {
2354e71b7053SJung-uk Kim if (!BIO_socket_nbio(s, 1))
2355e71b7053SJung-uk Kim ERR_print_errors(bio_err);
2356e71b7053SJung-uk Kim else if (!s_quiet)
2357e71b7053SJung-uk Kim BIO_printf(bio_err, "Turned on non blocking io\n");
2358e71b7053SJung-uk Kim }
2359e71b7053SJung-uk Kim
2360f579bf8eSKris Kennaway con = SSL_new(ctx);
2361e71b7053SJung-uk Kim if (con == NULL) {
2362e71b7053SJung-uk Kim ret = -1;
2363e71b7053SJung-uk Kim goto err;
2364e71b7053SJung-uk Kim }
2365e71b7053SJung-uk Kim
23666f9291ceSJung-uk Kim if (s_tlsextdebug) {
2367db522d3aSSimon L. B. Nielsen SSL_set_tlsext_debug_callback(con, tlsext_cb);
2368db522d3aSSimon L. B. Nielsen SSL_set_tlsext_debug_arg(con, bio_s_out);
2369db522d3aSSimon L. B. Nielsen }
237074664626SKris Kennaway
2371e71b7053SJung-uk Kim if (context != NULL
2372e71b7053SJung-uk Kim && !SSL_set_session_id_context(con, context,
2373e71b7053SJung-uk Kim strlen((char *)context))) {
2374e71b7053SJung-uk Kim BIO_printf(bio_err, "Error setting session id context\n");
2375e71b7053SJung-uk Kim ret = -1;
2376e71b7053SJung-uk Kim goto err;
2377e71b7053SJung-uk Kim }
23783b4e3dcbSSimon L. B. Nielsen
2379e71b7053SJung-uk Kim if (!SSL_clear(con)) {
2380e71b7053SJung-uk Kim BIO_printf(bio_err, "Error clearing SSL connection\n");
2381e71b7053SJung-uk Kim ret = -1;
2382e71b7053SJung-uk Kim goto err;
2383e71b7053SJung-uk Kim }
2384e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS
2385e71b7053SJung-uk Kim if (isdtls) {
2386e71b7053SJung-uk Kim # ifndef OPENSSL_NO_SCTP
2387e71b7053SJung-uk Kim if (prot == IPPROTO_SCTP)
2388e71b7053SJung-uk Kim sbio = BIO_new_dgram_sctp(s, BIO_NOCLOSE);
2389e71b7053SJung-uk Kim else
2390e71b7053SJung-uk Kim # endif
23913b4e3dcbSSimon L. B. Nielsen sbio = BIO_new_dgram(s, BIO_NOCLOSE);
2392b077aed3SPierre Pronchery if (sbio == NULL) {
2393b077aed3SPierre Pronchery BIO_printf(bio_err, "Unable to create BIO\n");
2394b077aed3SPierre Pronchery ERR_print_errors(bio_err);
2395b077aed3SPierre Pronchery goto err;
2396b077aed3SPierre Pronchery }
23973b4e3dcbSSimon L. B. Nielsen
23986f9291ceSJung-uk Kim if (enable_timeouts) {
23993b4e3dcbSSimon L. B. Nielsen timeout.tv_sec = 0;
24003b4e3dcbSSimon L. B. Nielsen timeout.tv_usec = DGRAM_RCV_TIMEOUT;
24013b4e3dcbSSimon L. B. Nielsen BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
24023b4e3dcbSSimon L. B. Nielsen
24033b4e3dcbSSimon L. B. Nielsen timeout.tv_sec = 0;
24043b4e3dcbSSimon L. B. Nielsen timeout.tv_usec = DGRAM_SND_TIMEOUT;
24053b4e3dcbSSimon L. B. Nielsen BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
24063b4e3dcbSSimon L. B. Nielsen }
24073b4e3dcbSSimon L. B. Nielsen
24086f9291ceSJung-uk Kim if (socket_mtu) {
24096f9291ceSJung-uk Kim if (socket_mtu < DTLS_get_link_min_mtu(con)) {
2410751d2991SJung-uk Kim BIO_printf(bio_err, "MTU too small. Must be at least %ld\n",
2411751d2991SJung-uk Kim DTLS_get_link_min_mtu(con));
2412751d2991SJung-uk Kim ret = -1;
2413751d2991SJung-uk Kim BIO_free(sbio);
2414751d2991SJung-uk Kim goto err;
2415751d2991SJung-uk Kim }
24163b4e3dcbSSimon L. B. Nielsen SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
24176f9291ceSJung-uk Kim if (!DTLS_set_link_mtu(con, socket_mtu)) {
2418751d2991SJung-uk Kim BIO_printf(bio_err, "Failed to set MTU\n");
2419751d2991SJung-uk Kim ret = -1;
2420751d2991SJung-uk Kim BIO_free(sbio);
2421751d2991SJung-uk Kim goto err;
2422751d2991SJung-uk Kim }
24236f9291ceSJung-uk Kim } else
24243b4e3dcbSSimon L. B. Nielsen /* want to do MTU discovery */
24253b4e3dcbSSimon L. B. Nielsen BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
24263b4e3dcbSSimon L. B. Nielsen
2427e71b7053SJung-uk Kim # ifndef OPENSSL_NO_SCTP
2428e71b7053SJung-uk Kim if (prot != IPPROTO_SCTP)
2429e71b7053SJung-uk Kim # endif
2430e71b7053SJung-uk Kim /* Turn on cookie exchange. Not necessary for SCTP */
24313b4e3dcbSSimon L. B. Nielsen SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE);
24326f9291ceSJung-uk Kim } else
2433e71b7053SJung-uk Kim #endif
243474664626SKris Kennaway sbio = BIO_new_socket(s, BIO_NOCLOSE);
24353b4e3dcbSSimon L. B. Nielsen
2436e71b7053SJung-uk Kim if (sbio == NULL) {
2437e71b7053SJung-uk Kim BIO_printf(bio_err, "Unable to create BIO\n");
2438e71b7053SJung-uk Kim ERR_print_errors(bio_err);
2439e71b7053SJung-uk Kim goto err;
2440e71b7053SJung-uk Kim }
2441e71b7053SJung-uk Kim
24426f9291ceSJung-uk Kim if (s_nbio_test) {
244374664626SKris Kennaway BIO *test;
244474664626SKris Kennaway
244574664626SKris Kennaway test = BIO_new(BIO_f_nbio_test());
2446b077aed3SPierre Pronchery if (test == NULL) {
2447b077aed3SPierre Pronchery BIO_printf(bio_err, "Unable to create BIO\n");
2448b077aed3SPierre Pronchery ret = -1;
2449b077aed3SPierre Pronchery BIO_free(sbio);
2450b077aed3SPierre Pronchery goto err;
2451b077aed3SPierre Pronchery }
2452b077aed3SPierre Pronchery
245374664626SKris Kennaway sbio = BIO_push(test, sbio);
245474664626SKris Kennaway }
2455db522d3aSSimon L. B. Nielsen
245674664626SKris Kennaway SSL_set_bio(con, sbio, sbio);
245774664626SKris Kennaway SSL_set_accept_state(con);
245874664626SKris Kennaway /* SSL_set_fd(con,s); */
245974664626SKris Kennaway
2460b077aed3SPierre Pronchery BIO_set_callback_ex(SSL_get_rbio(con), count_reads_callback);
24616f9291ceSJung-uk Kim if (s_msg) {
24627bded2dbSJung-uk Kim #ifndef OPENSSL_NO_SSL_TRACE
24637bded2dbSJung-uk Kim if (s_msg == 2)
24647bded2dbSJung-uk Kim SSL_set_msg_callback(con, SSL_trace);
24657bded2dbSJung-uk Kim else
24667bded2dbSJung-uk Kim #endif
24675c87c606SMark Murray SSL_set_msg_callback(con, msg_cb);
24687bded2dbSJung-uk Kim SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
24695c87c606SMark Murray }
2470e71b7053SJung-uk Kim
24716f9291ceSJung-uk Kim if (s_tlsextdebug) {
2472db522d3aSSimon L. B. Nielsen SSL_set_tlsext_debug_callback(con, tlsext_cb);
2473db522d3aSSimon L. B. Nielsen SSL_set_tlsext_debug_arg(con, bio_s_out);
2474db522d3aSSimon L. B. Nielsen }
2475e71b7053SJung-uk Kim
2476e71b7053SJung-uk Kim if (early_data) {
2477e71b7053SJung-uk Kim int write_header = 1, edret = SSL_READ_EARLY_DATA_ERROR;
2478e71b7053SJung-uk Kim size_t readbytes;
2479e71b7053SJung-uk Kim
2480e71b7053SJung-uk Kim while (edret != SSL_READ_EARLY_DATA_FINISH) {
2481e71b7053SJung-uk Kim for (;;) {
2482e71b7053SJung-uk Kim edret = SSL_read_early_data(con, buf, bufsize, &readbytes);
2483e71b7053SJung-uk Kim if (edret != SSL_READ_EARLY_DATA_ERROR)
2484e71b7053SJung-uk Kim break;
2485e71b7053SJung-uk Kim
2486e71b7053SJung-uk Kim switch (SSL_get_error(con, 0)) {
2487e71b7053SJung-uk Kim case SSL_ERROR_WANT_WRITE:
2488e71b7053SJung-uk Kim case SSL_ERROR_WANT_ASYNC:
2489e71b7053SJung-uk Kim case SSL_ERROR_WANT_READ:
2490e71b7053SJung-uk Kim /* Just keep trying - busy waiting */
2491e71b7053SJung-uk Kim continue;
2492e71b7053SJung-uk Kim default:
2493e71b7053SJung-uk Kim BIO_printf(bio_err, "Error reading early data\n");
2494e71b7053SJung-uk Kim ERR_print_errors(bio_err);
2495e71b7053SJung-uk Kim goto err;
2496e71b7053SJung-uk Kim }
2497e71b7053SJung-uk Kim }
2498e71b7053SJung-uk Kim if (readbytes > 0) {
2499e71b7053SJung-uk Kim if (write_header) {
2500e71b7053SJung-uk Kim BIO_printf(bio_s_out, "Early data received:\n");
2501e71b7053SJung-uk Kim write_header = 0;
2502e71b7053SJung-uk Kim }
2503e71b7053SJung-uk Kim raw_write_stdout(buf, (unsigned int)readbytes);
2504e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out);
2505e71b7053SJung-uk Kim }
2506e71b7053SJung-uk Kim }
2507e71b7053SJung-uk Kim if (write_header) {
2508e71b7053SJung-uk Kim if (SSL_get_early_data_status(con) == SSL_EARLY_DATA_NOT_SENT)
2509e71b7053SJung-uk Kim BIO_printf(bio_s_out, "No early data received\n");
2510e71b7053SJung-uk Kim else
2511e71b7053SJung-uk Kim BIO_printf(bio_s_out, "Early data was rejected\n");
2512e71b7053SJung-uk Kim } else {
2513e71b7053SJung-uk Kim BIO_printf(bio_s_out, "\nEnd of early data\n");
2514e71b7053SJung-uk Kim }
2515e71b7053SJung-uk Kim if (SSL_is_init_finished(con))
2516e71b7053SJung-uk Kim print_connection_info(con);
2517e71b7053SJung-uk Kim }
251874664626SKris Kennaway
2519aeb5019cSJung-uk Kim if (fileno_stdin() > s)
2520aeb5019cSJung-uk Kim width = fileno_stdin() + 1;
2521aeb5019cSJung-uk Kim else
252274664626SKris Kennaway width = s + 1;
25236f9291ceSJung-uk Kim for (;;) {
2524f579bf8eSKris Kennaway int read_from_terminal;
2525f579bf8eSKris Kennaway int read_from_sslcon;
2526f579bf8eSKris Kennaway
2527f579bf8eSKris Kennaway read_from_terminal = 0;
2528e71b7053SJung-uk Kim read_from_sslcon = SSL_has_pending(con)
2529e71b7053SJung-uk Kim || (async && SSL_waiting_for_async(con));
2530f579bf8eSKris Kennaway
25316f9291ceSJung-uk Kim if (!read_from_sslcon) {
253274664626SKris Kennaway FD_ZERO(&readfds);
2533e71b7053SJung-uk Kim #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
2534aeb5019cSJung-uk Kim openssl_fdset(fileno_stdin(), &readfds);
253574664626SKris Kennaway #endif
25361f13597dSJung-uk Kim openssl_fdset(s, &readfds);
25376f9291ceSJung-uk Kim /*
25386f9291ceSJung-uk Kim * Note: under VMS with SOCKETSHR the second parameter is
25396f9291ceSJung-uk Kim * currently of type (int *) whereas under other systems it is
25406f9291ceSJung-uk Kim * (void *) if you don't have a cast it will choke the compiler:
25416f9291ceSJung-uk Kim * if you do have a cast then you can either go for (int *) or
25426f9291ceSJung-uk Kim * (void *).
254374664626SKris Kennaway */
2544e71b7053SJung-uk Kim #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
25456f9291ceSJung-uk Kim /*
25466f9291ceSJung-uk Kim * Under DOS (non-djgpp) and Windows we can't select on stdin:
25476f9291ceSJung-uk Kim * only on sockets. As a workaround we timeout the select every
2548f579bf8eSKris Kennaway * second and check for any keypress. In a proper Windows
2549f579bf8eSKris Kennaway * application we wouldn't do this because it is inefficient.
2550f579bf8eSKris Kennaway */
2551e71b7053SJung-uk Kim timeout.tv_sec = 1;
2552e71b7053SJung-uk Kim timeout.tv_usec = 0;
2553e71b7053SJung-uk Kim i = select(width, (void *)&readfds, NULL, NULL, &timeout);
2554e71b7053SJung-uk Kim if (has_stdin_waiting())
2555f579bf8eSKris Kennaway read_from_terminal = 1;
2556e71b7053SJung-uk Kim if ((i < 0) || (!i && !read_from_terminal))
25571f13597dSJung-uk Kim continue;
2558f579bf8eSKris Kennaway #else
2559e71b7053SJung-uk Kim if (SSL_is_dtls(con) && DTLSv1_get_timeout(con, &timeout))
25606a599222SSimon L. B. Nielsen timeoutp = &timeout;
25616a599222SSimon L. B. Nielsen else
25626a599222SSimon L. B. Nielsen timeoutp = NULL;
25636a599222SSimon L. B. Nielsen
25646a599222SSimon L. B. Nielsen i = select(width, (void *)&readfds, NULL, NULL, timeoutp);
25656a599222SSimon L. B. Nielsen
2566e71b7053SJung-uk Kim if ((SSL_is_dtls(con)) && DTLSv1_handle_timeout(con) > 0)
2567e71b7053SJung-uk Kim BIO_printf(bio_err, "TIMEOUT occurred\n");
25686a599222SSimon L. B. Nielsen
25696f9291ceSJung-uk Kim if (i <= 0)
25706f9291ceSJung-uk Kim continue;
2571aeb5019cSJung-uk Kim if (FD_ISSET(fileno_stdin(), &readfds))
2572f579bf8eSKris Kennaway read_from_terminal = 1;
2573f579bf8eSKris Kennaway #endif
2574f579bf8eSKris Kennaway if (FD_ISSET(s, &readfds))
2575f579bf8eSKris Kennaway read_from_sslcon = 1;
2576f579bf8eSKris Kennaway }
25776f9291ceSJung-uk Kim if (read_from_terminal) {
25786f9291ceSJung-uk Kim if (s_crlf) {
257974664626SKris Kennaway int j, lf_num;
258074664626SKris Kennaway
25811f13597dSJung-uk Kim i = raw_read_stdin(buf, bufsize / 2);
258274664626SKris Kennaway lf_num = 0;
258374664626SKris Kennaway /* both loops are skipped when i <= 0 */
258474664626SKris Kennaway for (j = 0; j < i; j++)
258574664626SKris Kennaway if (buf[j] == '\n')
258674664626SKris Kennaway lf_num++;
25876f9291ceSJung-uk Kim for (j = i - 1; j >= 0; j--) {
258874664626SKris Kennaway buf[j + lf_num] = buf[j];
25896f9291ceSJung-uk Kim if (buf[j] == '\n') {
259074664626SKris Kennaway lf_num--;
259174664626SKris Kennaway i++;
259274664626SKris Kennaway buf[j + lf_num] = '\r';
259374664626SKris Kennaway }
259474664626SKris Kennaway }
259574664626SKris Kennaway assert(lf_num == 0);
2596e71b7053SJung-uk Kim } else {
25971f13597dSJung-uk Kim i = raw_read_stdin(buf, bufsize);
2598e71b7053SJung-uk Kim }
2599aeb5019cSJung-uk Kim
26007bded2dbSJung-uk Kim if (!s_quiet && !s_brief) {
26016f9291ceSJung-uk Kim if ((i <= 0) || (buf[0] == 'Q')) {
260274664626SKris Kennaway BIO_printf(bio_s_out, "DONE\n");
2603e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out);
2604e71b7053SJung-uk Kim BIO_closesocket(s);
260574664626SKris Kennaway close_accept_socket();
260674664626SKris Kennaway ret = -11;
260774664626SKris Kennaway goto err;
260874664626SKris Kennaway }
26096f9291ceSJung-uk Kim if ((i <= 0) || (buf[0] == 'q')) {
261074664626SKris Kennaway BIO_printf(bio_s_out, "DONE\n");
2611e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out);
26123b4e3dcbSSimon L. B. Nielsen if (SSL_version(con) != DTLS1_VERSION)
2613e71b7053SJung-uk Kim BIO_closesocket(s);
26146f9291ceSJung-uk Kim /*
26156f9291ceSJung-uk Kim * close_accept_socket(); ret= -11;
26166f9291ceSJung-uk Kim */
261774664626SKris Kennaway goto err;
261874664626SKris Kennaway }
26196f9291ceSJung-uk Kim if ((buf[0] == 'r') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
262074664626SKris Kennaway SSL_renegotiate(con);
262174664626SKris Kennaway i = SSL_do_handshake(con);
262274664626SKris Kennaway printf("SSL_do_handshake -> %d\n", i);
262374664626SKris Kennaway i = 0; /* 13; */
262474664626SKris Kennaway continue;
262574664626SKris Kennaway }
26266f9291ceSJung-uk Kim if ((buf[0] == 'R') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
262774664626SKris Kennaway SSL_set_verify(con,
26286f9291ceSJung-uk Kim SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
26296f9291ceSJung-uk Kim NULL);
263074664626SKris Kennaway SSL_renegotiate(con);
263174664626SKris Kennaway i = SSL_do_handshake(con);
263274664626SKris Kennaway printf("SSL_do_handshake -> %d\n", i);
263374664626SKris Kennaway i = 0; /* 13; */
263474664626SKris Kennaway continue;
2635e71b7053SJung-uk Kim }
2636e71b7053SJung-uk Kim if ((buf[0] == 'K' || buf[0] == 'k')
2637e71b7053SJung-uk Kim && ((buf[1] == '\n') || (buf[1] == '\r'))) {
2638e71b7053SJung-uk Kim SSL_key_update(con, buf[0] == 'K' ?
2639e71b7053SJung-uk Kim SSL_KEY_UPDATE_REQUESTED
2640e71b7053SJung-uk Kim : SSL_KEY_UPDATE_NOT_REQUESTED);
2641e71b7053SJung-uk Kim i = SSL_do_handshake(con);
2642e71b7053SJung-uk Kim printf("SSL_do_handshake -> %d\n", i);
2643e71b7053SJung-uk Kim i = 0;
2644e71b7053SJung-uk Kim continue;
2645e71b7053SJung-uk Kim }
2646e71b7053SJung-uk Kim if (buf[0] == 'c' && ((buf[1] == '\n') || (buf[1] == '\r'))) {
2647e71b7053SJung-uk Kim SSL_set_verify(con, SSL_VERIFY_PEER, NULL);
2648e71b7053SJung-uk Kim i = SSL_verify_client_post_handshake(con);
2649e71b7053SJung-uk Kim if (i == 0) {
2650e71b7053SJung-uk Kim printf("Failed to initiate request\n");
2651e71b7053SJung-uk Kim ERR_print_errors(bio_err);
2652e71b7053SJung-uk Kim } else {
2653e71b7053SJung-uk Kim i = SSL_do_handshake(con);
2654e71b7053SJung-uk Kim printf("SSL_do_handshake -> %d\n", i);
2655e71b7053SJung-uk Kim i = 0;
2656e71b7053SJung-uk Kim }
2657e71b7053SJung-uk Kim continue;
265874664626SKris Kennaway }
26596f9291ceSJung-uk Kim if (buf[0] == 'P') {
2660b077aed3SPierre Pronchery static const char str[] = "Lets print some clear text\n";
2661b077aed3SPierre Pronchery BIO_write(SSL_get_wbio(con), str, sizeof(str) -1);
266274664626SKris Kennaway }
26636f9291ceSJung-uk Kim if (buf[0] == 'S') {
266474664626SKris Kennaway print_stats(bio_s_out, SSL_get_SSL_CTX(con));
266574664626SKris Kennaway }
266674664626SKris Kennaway }
266774664626SKris Kennaway #ifdef CHARSET_EBCDIC
266874664626SKris Kennaway ebcdic2ascii(buf, buf, i);
266974664626SKris Kennaway #endif
267074664626SKris Kennaway l = k = 0;
26716f9291ceSJung-uk Kim for (;;) {
267274664626SKris Kennaway /* should do a select for the write */
267374664626SKris Kennaway #ifdef RENEG
26746f9291ceSJung-uk Kim static count = 0;
26756f9291ceSJung-uk Kim if (++count == 100) {
26766f9291ceSJung-uk Kim count = 0;
26776f9291ceSJung-uk Kim SSL_renegotiate(con);
26786f9291ceSJung-uk Kim }
267974664626SKris Kennaway #endif
268074664626SKris Kennaway k = SSL_write(con, &(buf[l]), (unsigned int)i);
26811f13597dSJung-uk Kim #ifndef OPENSSL_NO_SRP
26826f9291ceSJung-uk Kim while (SSL_get_error(con, k) == SSL_ERROR_WANT_X509_LOOKUP) {
26831f13597dSJung-uk Kim BIO_printf(bio_s_out, "LOOKUP renego during write\n");
2684b077aed3SPierre Pronchery
2685b077aed3SPierre Pronchery lookup_srp_user(&srp_callback_parm, bio_s_out);
2686b077aed3SPierre Pronchery
26871f13597dSJung-uk Kim k = SSL_write(con, &(buf[l]), (unsigned int)i);
26881f13597dSJung-uk Kim }
26891f13597dSJung-uk Kim #endif
26906f9291ceSJung-uk Kim switch (SSL_get_error(con, k)) {
269174664626SKris Kennaway case SSL_ERROR_NONE:
269274664626SKris Kennaway break;
2693e71b7053SJung-uk Kim case SSL_ERROR_WANT_ASYNC:
2694e71b7053SJung-uk Kim BIO_printf(bio_s_out, "Write BLOCK (Async)\n");
2695e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out);
2696e71b7053SJung-uk Kim wait_for_async(con);
2697e71b7053SJung-uk Kim break;
269874664626SKris Kennaway case SSL_ERROR_WANT_WRITE:
269974664626SKris Kennaway case SSL_ERROR_WANT_READ:
270074664626SKris Kennaway case SSL_ERROR_WANT_X509_LOOKUP:
270174664626SKris Kennaway BIO_printf(bio_s_out, "Write BLOCK\n");
2702e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out);
270374664626SKris Kennaway break;
2704e71b7053SJung-uk Kim case SSL_ERROR_WANT_ASYNC_JOB:
2705e71b7053SJung-uk Kim /*
2706e71b7053SJung-uk Kim * This shouldn't ever happen in s_server. Treat as an error
2707e71b7053SJung-uk Kim */
270874664626SKris Kennaway case SSL_ERROR_SYSCALL:
270974664626SKris Kennaway case SSL_ERROR_SSL:
271074664626SKris Kennaway BIO_printf(bio_s_out, "ERROR\n");
2711e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out);
271274664626SKris Kennaway ERR_print_errors(bio_err);
271374664626SKris Kennaway ret = 1;
271474664626SKris Kennaway goto err;
271574664626SKris Kennaway /* break; */
271674664626SKris Kennaway case SSL_ERROR_ZERO_RETURN:
271774664626SKris Kennaway BIO_printf(bio_s_out, "DONE\n");
2718e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out);
271974664626SKris Kennaway ret = 1;
272074664626SKris Kennaway goto err;
272174664626SKris Kennaway }
2722ed6b93beSJung-uk Kim if (k > 0) {
272374664626SKris Kennaway l += k;
272474664626SKris Kennaway i -= k;
2725ed6b93beSJung-uk Kim }
27266f9291ceSJung-uk Kim if (i <= 0)
27276f9291ceSJung-uk Kim break;
272874664626SKris Kennaway }
272974664626SKris Kennaway }
27306f9291ceSJung-uk Kim if (read_from_sslcon) {
2731e71b7053SJung-uk Kim /*
2732e71b7053SJung-uk Kim * init_ssl_connection handles all async events itself so if we're
2733e71b7053SJung-uk Kim * waiting for async then we shouldn't go back into
2734e71b7053SJung-uk Kim * init_ssl_connection
2735e71b7053SJung-uk Kim */
2736e71b7053SJung-uk Kim if ((!async || !SSL_waiting_for_async(con))
2737e71b7053SJung-uk Kim && !SSL_is_init_finished(con)) {
273883eaf7aeSJung-uk Kim /*
273983eaf7aeSJung-uk Kim * Count number of reads during init_ssl_connection.
274083eaf7aeSJung-uk Kim * It helps us to distinguish configuration errors from errors
274183eaf7aeSJung-uk Kim * caused by a client.
274283eaf7aeSJung-uk Kim */
274383eaf7aeSJung-uk Kim unsigned int read_counter = 0;
274483eaf7aeSJung-uk Kim
274583eaf7aeSJung-uk Kim BIO_set_callback_arg(SSL_get_rbio(con), (char *)&read_counter);
274674664626SKris Kennaway i = init_ssl_connection(con);
274783eaf7aeSJung-uk Kim BIO_set_callback_arg(SSL_get_rbio(con), NULL);
274883eaf7aeSJung-uk Kim
274983eaf7aeSJung-uk Kim /*
275083eaf7aeSJung-uk Kim * If initialization fails without reads, then
275183eaf7aeSJung-uk Kim * there was a fatal error in configuration.
275283eaf7aeSJung-uk Kim */
275383eaf7aeSJung-uk Kim if (i <= 0 && read_counter == 0) {
275483eaf7aeSJung-uk Kim ret = -1;
275583eaf7aeSJung-uk Kim goto err;
275683eaf7aeSJung-uk Kim }
27576f9291ceSJung-uk Kim if (i < 0) {
275874664626SKris Kennaway ret = 0;
275974664626SKris Kennaway goto err;
27606f9291ceSJung-uk Kim } else if (i == 0) {
276174664626SKris Kennaway ret = 1;
276274664626SKris Kennaway goto err;
276374664626SKris Kennaway }
27646f9291ceSJung-uk Kim } else {
276574664626SKris Kennaway again:
276674664626SKris Kennaway i = SSL_read(con, (char *)buf, bufsize);
27671f13597dSJung-uk Kim #ifndef OPENSSL_NO_SRP
27686f9291ceSJung-uk Kim while (SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) {
27691f13597dSJung-uk Kim BIO_printf(bio_s_out, "LOOKUP renego during read\n");
2770b077aed3SPierre Pronchery
2771b077aed3SPierre Pronchery lookup_srp_user(&srp_callback_parm, bio_s_out);
2772b077aed3SPierre Pronchery
27731f13597dSJung-uk Kim i = SSL_read(con, (char *)buf, bufsize);
27741f13597dSJung-uk Kim }
27751f13597dSJung-uk Kim #endif
27766f9291ceSJung-uk Kim switch (SSL_get_error(con, i)) {
277774664626SKris Kennaway case SSL_ERROR_NONE:
277874664626SKris Kennaway #ifdef CHARSET_EBCDIC
277974664626SKris Kennaway ascii2ebcdic(buf, buf, i);
278074664626SKris Kennaway #endif
27816f9291ceSJung-uk Kim raw_write_stdout(buf, (unsigned int)i);
2782e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out);
2783e71b7053SJung-uk Kim if (SSL_has_pending(con))
27846f9291ceSJung-uk Kim goto again;
278574664626SKris Kennaway break;
2786e71b7053SJung-uk Kim case SSL_ERROR_WANT_ASYNC:
2787e71b7053SJung-uk Kim BIO_printf(bio_s_out, "Read BLOCK (Async)\n");
2788e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out);
2789e71b7053SJung-uk Kim wait_for_async(con);
2790e71b7053SJung-uk Kim break;
279174664626SKris Kennaway case SSL_ERROR_WANT_WRITE:
279274664626SKris Kennaway case SSL_ERROR_WANT_READ:
279374664626SKris Kennaway BIO_printf(bio_s_out, "Read BLOCK\n");
2794e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out);
279574664626SKris Kennaway break;
2796e71b7053SJung-uk Kim case SSL_ERROR_WANT_ASYNC_JOB:
2797e71b7053SJung-uk Kim /*
2798e71b7053SJung-uk Kim * This shouldn't ever happen in s_server. Treat as an error
2799e71b7053SJung-uk Kim */
280074664626SKris Kennaway case SSL_ERROR_SYSCALL:
280174664626SKris Kennaway case SSL_ERROR_SSL:
280274664626SKris Kennaway BIO_printf(bio_s_out, "ERROR\n");
2803e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out);
280474664626SKris Kennaway ERR_print_errors(bio_err);
280574664626SKris Kennaway ret = 1;
280674664626SKris Kennaway goto err;
280774664626SKris Kennaway case SSL_ERROR_ZERO_RETURN:
280874664626SKris Kennaway BIO_printf(bio_s_out, "DONE\n");
2809e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out);
281074664626SKris Kennaway ret = 1;
281174664626SKris Kennaway goto err;
281274664626SKris Kennaway }
281374664626SKris Kennaway }
281474664626SKris Kennaway }
281574664626SKris Kennaway }
281674664626SKris Kennaway err:
28176f9291ceSJung-uk Kim if (con != NULL) {
281874664626SKris Kennaway BIO_printf(bio_s_out, "shutting down SSL\n");
2819b077aed3SPierre Pronchery do_ssl_shutdown(con);
28201f13597dSJung-uk Kim SSL_free(con);
28211f13597dSJung-uk Kim }
282274664626SKris Kennaway BIO_printf(bio_s_out, "CONNECTION CLOSED\n");
2823e71b7053SJung-uk Kim OPENSSL_clear_free(buf, bufsize);
2824e71b7053SJung-uk Kim return ret;
282574664626SKris Kennaway }
282674664626SKris Kennaway
close_accept_socket(void)282774664626SKris Kennaway static void close_accept_socket(void)
282874664626SKris Kennaway {
282974664626SKris Kennaway BIO_printf(bio_err, "shutdown accept socket\n");
28306f9291ceSJung-uk Kim if (accept_socket >= 0) {
2831e71b7053SJung-uk Kim BIO_closesocket(accept_socket);
283274664626SKris Kennaway }
283374664626SKris Kennaway }
283474664626SKris Kennaway
is_retryable(SSL * con,int i)2835e71b7053SJung-uk Kim static int is_retryable(SSL *con, int i)
2836e71b7053SJung-uk Kim {
2837e71b7053SJung-uk Kim int err = SSL_get_error(con, i);
2838e71b7053SJung-uk Kim
2839e71b7053SJung-uk Kim /* If it's not a fatal error, it must be retryable */
2840e71b7053SJung-uk Kim return (err != SSL_ERROR_SSL)
2841e71b7053SJung-uk Kim && (err != SSL_ERROR_SYSCALL)
2842e71b7053SJung-uk Kim && (err != SSL_ERROR_ZERO_RETURN);
2843e71b7053SJung-uk Kim }
2844e71b7053SJung-uk Kim
init_ssl_connection(SSL * con)284574664626SKris Kennaway static int init_ssl_connection(SSL *con)
284674664626SKris Kennaway {
284774664626SKris Kennaway int i;
2848e71b7053SJung-uk Kim long verify_err;
2849e71b7053SJung-uk Kim int retry = 0;
285074664626SKris Kennaway
2851e71b7053SJung-uk Kim if (dtlslisten || stateless) {
2852e71b7053SJung-uk Kim BIO_ADDR *client = NULL;
2853e71b7053SJung-uk Kim
2854e71b7053SJung-uk Kim if (dtlslisten) {
2855e71b7053SJung-uk Kim if ((client = BIO_ADDR_new()) == NULL) {
2856e71b7053SJung-uk Kim BIO_printf(bio_err, "ERROR - memory\n");
2857e71b7053SJung-uk Kim return 0;
2858e71b7053SJung-uk Kim }
2859e71b7053SJung-uk Kim i = DTLSv1_listen(con, client);
2860e71b7053SJung-uk Kim } else {
2861e71b7053SJung-uk Kim i = SSL_stateless(con);
2862e71b7053SJung-uk Kim }
2863e71b7053SJung-uk Kim if (i > 0) {
2864e71b7053SJung-uk Kim BIO *wbio;
2865e71b7053SJung-uk Kim int fd = -1;
2866e71b7053SJung-uk Kim
2867e71b7053SJung-uk Kim if (dtlslisten) {
2868e71b7053SJung-uk Kim wbio = SSL_get_wbio(con);
2869e71b7053SJung-uk Kim if (wbio) {
2870e71b7053SJung-uk Kim BIO_get_fd(wbio, &fd);
2871e71b7053SJung-uk Kim }
2872e71b7053SJung-uk Kim
2873e71b7053SJung-uk Kim if (!wbio || BIO_connect(fd, client, 0) == 0) {
2874e71b7053SJung-uk Kim BIO_printf(bio_err, "ERROR - unable to connect\n");
2875e71b7053SJung-uk Kim BIO_ADDR_free(client);
2876e71b7053SJung-uk Kim return 0;
2877e71b7053SJung-uk Kim }
28786935a639SJung-uk Kim
28796935a639SJung-uk Kim (void)BIO_ctrl_set_connected(wbio, client);
2880e71b7053SJung-uk Kim BIO_ADDR_free(client);
2881e71b7053SJung-uk Kim dtlslisten = 0;
2882e71b7053SJung-uk Kim } else {
2883e71b7053SJung-uk Kim stateless = 0;
2884e71b7053SJung-uk Kim }
28851f13597dSJung-uk Kim i = SSL_accept(con);
2886e71b7053SJung-uk Kim } else {
2887e71b7053SJung-uk Kim BIO_ADDR_free(client);
2888e71b7053SJung-uk Kim }
2889e71b7053SJung-uk Kim } else {
2890e71b7053SJung-uk Kim do {
2891e71b7053SJung-uk Kim i = SSL_accept(con);
2892e71b7053SJung-uk Kim
2893e71b7053SJung-uk Kim if (i <= 0)
2894e71b7053SJung-uk Kim retry = is_retryable(con, i);
28957bded2dbSJung-uk Kim #ifdef CERT_CB_TEST_RETRY
28967bded2dbSJung-uk Kim {
2897e71b7053SJung-uk Kim while (i <= 0
2898e71b7053SJung-uk Kim && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP
2899e71b7053SJung-uk Kim && SSL_get_state(con) == TLS_ST_SR_CLNT_HELLO) {
2900e71b7053SJung-uk Kim BIO_printf(bio_err,
29017bded2dbSJung-uk Kim "LOOKUP from certificate callback during accept\n");
29027bded2dbSJung-uk Kim i = SSL_accept(con);
2903e71b7053SJung-uk Kim if (i <= 0)
2904e71b7053SJung-uk Kim retry = is_retryable(con, i);
29057bded2dbSJung-uk Kim }
29067bded2dbSJung-uk Kim }
29077bded2dbSJung-uk Kim #endif
2908e71b7053SJung-uk Kim
29091f13597dSJung-uk Kim #ifndef OPENSSL_NO_SRP
2910e71b7053SJung-uk Kim while (i <= 0
2911e71b7053SJung-uk Kim && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) {
29126f9291ceSJung-uk Kim BIO_printf(bio_s_out, "LOOKUP during accept %s\n",
29136f9291ceSJung-uk Kim srp_callback_parm.login);
2914b077aed3SPierre Pronchery
2915b077aed3SPierre Pronchery lookup_srp_user(&srp_callback_parm, bio_s_out);
2916b077aed3SPierre Pronchery
29171f13597dSJung-uk Kim i = SSL_accept(con);
2918e71b7053SJung-uk Kim if (i <= 0)
2919e71b7053SJung-uk Kim retry = is_retryable(con, i);
29201f13597dSJung-uk Kim }
29211f13597dSJung-uk Kim #endif
2922e71b7053SJung-uk Kim } while (i < 0 && SSL_waiting_for_async(con));
2923e71b7053SJung-uk Kim }
29247bded2dbSJung-uk Kim
29256f9291ceSJung-uk Kim if (i <= 0) {
2926e71b7053SJung-uk Kim if (((dtlslisten || stateless) && i == 0)
2927e71b7053SJung-uk Kim || (!dtlslisten && !stateless && retry)) {
292874664626SKris Kennaway BIO_printf(bio_s_out, "DELAY\n");
2929e71b7053SJung-uk Kim return 1;
293074664626SKris Kennaway }
293174664626SKris Kennaway
293274664626SKris Kennaway BIO_printf(bio_err, "ERROR\n");
2933e71b7053SJung-uk Kim
2934e71b7053SJung-uk Kim verify_err = SSL_get_verify_result(con);
2935e71b7053SJung-uk Kim if (verify_err != X509_V_OK) {
293674664626SKris Kennaway BIO_printf(bio_err, "verify error:%s\n",
2937e71b7053SJung-uk Kim X509_verify_cert_error_string(verify_err));
29387bded2dbSJung-uk Kim }
29397bded2dbSJung-uk Kim /* Always print any error messages */
294074664626SKris Kennaway ERR_print_errors(bio_err);
2941e71b7053SJung-uk Kim return 0;
294274664626SKris Kennaway }
294374664626SKris Kennaway
2944e71b7053SJung-uk Kim print_connection_info(con);
2945e71b7053SJung-uk Kim return 1;
2946e71b7053SJung-uk Kim }
2947e71b7053SJung-uk Kim
print_connection_info(SSL * con)2948e71b7053SJung-uk Kim static void print_connection_info(SSL *con)
2949e71b7053SJung-uk Kim {
2950e71b7053SJung-uk Kim const char *str;
2951e71b7053SJung-uk Kim X509 *peer;
2952e71b7053SJung-uk Kim char buf[BUFSIZ];
2953e71b7053SJung-uk Kim #if !defined(OPENSSL_NO_NEXTPROTONEG)
2954e71b7053SJung-uk Kim const unsigned char *next_proto_neg;
2955e71b7053SJung-uk Kim unsigned next_proto_neg_len;
2956e71b7053SJung-uk Kim #endif
2957e71b7053SJung-uk Kim unsigned char *exportedkeymat;
2958e71b7053SJung-uk Kim int i;
2959e71b7053SJung-uk Kim
29607bded2dbSJung-uk Kim if (s_brief)
2961e71b7053SJung-uk Kim print_ssl_summary(con);
29627bded2dbSJung-uk Kim
296374664626SKris Kennaway PEM_write_bio_SSL_SESSION(bio_s_out, SSL_get_session(con));
296474664626SKris Kennaway
2965b077aed3SPierre Pronchery peer = SSL_get0_peer_certificate(con);
29666f9291ceSJung-uk Kim if (peer != NULL) {
296774664626SKris Kennaway BIO_printf(bio_s_out, "Client certificate\n");
296874664626SKris Kennaway PEM_write_bio_X509(bio_s_out, peer);
2969e71b7053SJung-uk Kim dump_cert_text(bio_s_out, peer);
2970e71b7053SJung-uk Kim peer = NULL;
297174664626SKris Kennaway }
297274664626SKris Kennaway
2973dee36b4fSJung-uk Kim if (SSL_get_shared_ciphers(con, buf, sizeof(buf)) != NULL)
297474664626SKris Kennaway BIO_printf(bio_s_out, "Shared ciphers:%s\n", buf);
297574664626SKris Kennaway str = SSL_CIPHER_get_name(SSL_get_current_cipher(con));
29767bded2dbSJung-uk Kim ssl_print_sigalgs(bio_s_out, con);
29777bded2dbSJung-uk Kim #ifndef OPENSSL_NO_EC
29787bded2dbSJung-uk Kim ssl_print_point_formats(bio_s_out, con);
2979e71b7053SJung-uk Kim ssl_print_groups(bio_s_out, con, 0);
29807bded2dbSJung-uk Kim #endif
2981e71b7053SJung-uk Kim print_ca_names(bio_s_out, con);
298274664626SKris Kennaway BIO_printf(bio_s_out, "CIPHER is %s\n", (str != NULL) ? str : "(NONE)");
298309286989SJung-uk Kim
2984e71b7053SJung-uk Kim #if !defined(OPENSSL_NO_NEXTPROTONEG)
29851f13597dSJung-uk Kim SSL_get0_next_proto_negotiated(con, &next_proto_neg, &next_proto_neg_len);
29866f9291ceSJung-uk Kim if (next_proto_neg) {
29871f13597dSJung-uk Kim BIO_printf(bio_s_out, "NEXTPROTO is ");
29881f13597dSJung-uk Kim BIO_write(bio_s_out, next_proto_neg, next_proto_neg_len);
29891f13597dSJung-uk Kim BIO_printf(bio_s_out, "\n");
29901f13597dSJung-uk Kim }
29911f13597dSJung-uk Kim #endif
299209286989SJung-uk Kim #ifndef OPENSSL_NO_SRTP
29931f13597dSJung-uk Kim {
29941f13597dSJung-uk Kim SRTP_PROTECTION_PROFILE *srtp_profile
29951f13597dSJung-uk Kim = SSL_get_selected_srtp_profile(con);
29961f13597dSJung-uk Kim
29971f13597dSJung-uk Kim if (srtp_profile)
29981f13597dSJung-uk Kim BIO_printf(bio_s_out, "SRTP Extension negotiated, profile=%s\n",
29991f13597dSJung-uk Kim srtp_profile->name);
30001f13597dSJung-uk Kim }
300109286989SJung-uk Kim #endif
3002e71b7053SJung-uk Kim if (SSL_session_reused(con))
30036f9291ceSJung-uk Kim BIO_printf(bio_s_out, "Reused session-id\n");
30046a599222SSimon L. B. Nielsen BIO_printf(bio_s_out, "Secure Renegotiation IS%s supported\n",
30056a599222SSimon L. B. Nielsen SSL_get_secure_renegotiation_support(con) ? "" : " NOT");
3006e71b7053SJung-uk Kim if ((SSL_get_options(con) & SSL_OP_NO_RENEGOTIATION))
3007e71b7053SJung-uk Kim BIO_printf(bio_s_out, "Renegotiation is DISABLED\n");
3008e71b7053SJung-uk Kim
30096f9291ceSJung-uk Kim if (keymatexportlabel != NULL) {
30101f13597dSJung-uk Kim BIO_printf(bio_s_out, "Keying material exporter:\n");
30111f13597dSJung-uk Kim BIO_printf(bio_s_out, " Label: '%s'\n", keymatexportlabel);
30126f9291ceSJung-uk Kim BIO_printf(bio_s_out, " Length: %i bytes\n", keymatexportlen);
3013e71b7053SJung-uk Kim exportedkeymat = app_malloc(keymatexportlen, "export key");
3014b077aed3SPierre Pronchery if (SSL_export_keying_material(con, exportedkeymat,
30151f13597dSJung-uk Kim keymatexportlen,
30161f13597dSJung-uk Kim keymatexportlabel,
30171f13597dSJung-uk Kim strlen(keymatexportlabel),
3018b077aed3SPierre Pronchery NULL, 0, 0) <= 0) {
30191f13597dSJung-uk Kim BIO_printf(bio_s_out, " Error\n");
30206f9291ceSJung-uk Kim } else {
30211f13597dSJung-uk Kim BIO_printf(bio_s_out, " Keying material: ");
30221f13597dSJung-uk Kim for (i = 0; i < keymatexportlen; i++)
30236f9291ceSJung-uk Kim BIO_printf(bio_s_out, "%02X", exportedkeymat[i]);
30241f13597dSJung-uk Kim BIO_printf(bio_s_out, "\n");
30251f13597dSJung-uk Kim }
30261f13597dSJung-uk Kim OPENSSL_free(exportedkeymat);
30271f13597dSJung-uk Kim }
3028aa906e2aSJohn Baldwin #ifndef OPENSSL_NO_KTLS
3029aa906e2aSJohn Baldwin if (BIO_get_ktls_send(SSL_get_wbio(con)))
3030aa906e2aSJohn Baldwin BIO_printf(bio_err, "Using Kernel TLS for sending\n");
3031aa906e2aSJohn Baldwin if (BIO_get_ktls_recv(SSL_get_rbio(con)))
3032aa906e2aSJohn Baldwin BIO_printf(bio_err, "Using Kernel TLS for receiving\n");
3033aa906e2aSJohn Baldwin #endif
30341f13597dSJung-uk Kim
3035e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out);
303674664626SKris Kennaway }
303774664626SKris Kennaway
www_body(int s,int stype,int prot,unsigned char * context)3038e71b7053SJung-uk Kim static int www_body(int s, int stype, int prot, unsigned char *context)
303974664626SKris Kennaway {
304074664626SKris Kennaway char *buf = NULL;
304174664626SKris Kennaway int ret = 1;
3042a3ddd25aSSimon L. B. Nielsen int i, j, k, dot;
304374664626SKris Kennaway SSL *con;
30441f13597dSJung-uk Kim const SSL_CIPHER *c;
304574664626SKris Kennaway BIO *io, *ssl_bio, *sbio;
3046e71b7053SJung-uk Kim #ifdef RENEG
3047e71b7053SJung-uk Kim int total_bytes = 0;
3048a3ddd25aSSimon L. B. Nielsen #endif
3049e71b7053SJung-uk Kim int width;
3050b077aed3SPierre Pronchery #ifndef OPENSSL_NO_KTLS
3051b077aed3SPierre Pronchery int use_sendfile_for_req = use_sendfile;
3052b077aed3SPierre Pronchery #endif
3053e71b7053SJung-uk Kim fd_set readfds;
3054b077aed3SPierre Pronchery const char *opmode;
3055b077aed3SPierre Pronchery #ifdef CHARSET_EBCDIC
3056b077aed3SPierre Pronchery BIO *filter;
3057b077aed3SPierre Pronchery #endif
305874664626SKris Kennaway
3059e71b7053SJung-uk Kim /* Set width for a select call if needed */
3060e71b7053SJung-uk Kim width = s + 1;
3061e71b7053SJung-uk Kim
3062b077aed3SPierre Pronchery /* as we use BIO_gets(), and it always null terminates data, we need
3063b077aed3SPierre Pronchery * to allocate 1 byte longer buffer to fit the full 2^14 byte record */
3064b077aed3SPierre Pronchery buf = app_malloc(bufsize + 1, "server www buffer");
306574664626SKris Kennaway io = BIO_new(BIO_f_buffer());
306674664626SKris Kennaway ssl_bio = BIO_new(BIO_f_ssl());
30676f9291ceSJung-uk Kim if ((io == NULL) || (ssl_bio == NULL))
30686f9291ceSJung-uk Kim goto err;
306974664626SKris Kennaway
30706f9291ceSJung-uk Kim if (s_nbio) {
3071e71b7053SJung-uk Kim if (!BIO_socket_nbio(s, 1))
307274664626SKris Kennaway ERR_print_errors(bio_err);
3073e71b7053SJung-uk Kim else if (!s_quiet)
3074e71b7053SJung-uk Kim BIO_printf(bio_err, "Turned on non blocking io\n");
307574664626SKris Kennaway }
307674664626SKris Kennaway
307774664626SKris Kennaway /* lets make the output buffer a reasonable size */
3078b077aed3SPierre Pronchery if (BIO_set_write_buffer_size(io, bufsize) <= 0)
30796f9291ceSJung-uk Kim goto err;
308074664626SKris Kennaway
30816f9291ceSJung-uk Kim if ((con = SSL_new(ctx)) == NULL)
30826f9291ceSJung-uk Kim goto err;
3083e71b7053SJung-uk Kim
30846f9291ceSJung-uk Kim if (s_tlsextdebug) {
3085db522d3aSSimon L. B. Nielsen SSL_set_tlsext_debug_callback(con, tlsext_cb);
3086db522d3aSSimon L. B. Nielsen SSL_set_tlsext_debug_arg(con, bio_s_out);
3087db522d3aSSimon L. B. Nielsen }
3088e71b7053SJung-uk Kim
3089e71b7053SJung-uk Kim if (context != NULL
3090e71b7053SJung-uk Kim && !SSL_set_session_id_context(con, context,
3091e71b7053SJung-uk Kim strlen((char *)context))) {
3092e71b7053SJung-uk Kim SSL_free(con);
3093e71b7053SJung-uk Kim goto err;
30945c87c606SMark Murray }
309574664626SKris Kennaway
309674664626SKris Kennaway sbio = BIO_new_socket(s, BIO_NOCLOSE);
3097b077aed3SPierre Pronchery if (sbio == NULL) {
3098b077aed3SPierre Pronchery SSL_free(con);
3099b077aed3SPierre Pronchery goto err;
3100b077aed3SPierre Pronchery }
3101b077aed3SPierre Pronchery
31026f9291ceSJung-uk Kim if (s_nbio_test) {
310374664626SKris Kennaway BIO *test;
310474664626SKris Kennaway
310574664626SKris Kennaway test = BIO_new(BIO_f_nbio_test());
3106b077aed3SPierre Pronchery if (test == NULL) {
3107b077aed3SPierre Pronchery SSL_free(con);
3108b077aed3SPierre Pronchery BIO_free(sbio);
3109b077aed3SPierre Pronchery goto err;
3110b077aed3SPierre Pronchery }
3111b077aed3SPierre Pronchery
311274664626SKris Kennaway sbio = BIO_push(test, sbio);
311374664626SKris Kennaway }
311474664626SKris Kennaway SSL_set_bio(con, sbio, sbio);
311574664626SKris Kennaway SSL_set_accept_state(con);
311674664626SKris Kennaway
3117e71b7053SJung-uk Kim /* No need to free |con| after this. Done by BIO_free(ssl_bio) */
311874664626SKris Kennaway BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
311974664626SKris Kennaway BIO_push(io, ssl_bio);
3120b077aed3SPierre Pronchery ssl_bio = NULL;
312174664626SKris Kennaway #ifdef CHARSET_EBCDIC
3122b077aed3SPierre Pronchery filter = BIO_new(BIO_f_ebcdic_filter());
3123b077aed3SPierre Pronchery if (filter == NULL)
3124b077aed3SPierre Pronchery goto err;
3125b077aed3SPierre Pronchery
3126b077aed3SPierre Pronchery io = BIO_push(filter, io);
312774664626SKris Kennaway #endif
312874664626SKris Kennaway
31296f9291ceSJung-uk Kim if (s_debug) {
3130b077aed3SPierre Pronchery BIO_set_callback_ex(SSL_get_rbio(con), bio_dump_callback);
31315471f83eSSimon L. B. Nielsen BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
313274664626SKris Kennaway }
31336f9291ceSJung-uk Kim if (s_msg) {
31347bded2dbSJung-uk Kim #ifndef OPENSSL_NO_SSL_TRACE
31357bded2dbSJung-uk Kim if (s_msg == 2)
31367bded2dbSJung-uk Kim SSL_set_msg_callback(con, SSL_trace);
31377bded2dbSJung-uk Kim else
31387bded2dbSJung-uk Kim #endif
31395c87c606SMark Murray SSL_set_msg_callback(con, msg_cb);
31407bded2dbSJung-uk Kim SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
31415c87c606SMark Murray }
314274664626SKris Kennaway
31436f9291ceSJung-uk Kim for (;;) {
3144b077aed3SPierre Pronchery i = BIO_gets(io, buf, bufsize + 1);
31456f9291ceSJung-uk Kim if (i < 0) { /* error */
3146e71b7053SJung-uk Kim if (!BIO_should_retry(io) && !SSL_waiting_for_async(con)) {
314774664626SKris Kennaway if (!s_quiet)
314874664626SKris Kennaway ERR_print_errors(bio_err);
314974664626SKris Kennaway goto err;
31506f9291ceSJung-uk Kim } else {
315174664626SKris Kennaway BIO_printf(bio_s_out, "read R BLOCK\n");
315280815a77SJung-uk Kim #ifndef OPENSSL_NO_SRP
315380815a77SJung-uk Kim if (BIO_should_io_special(io)
315480815a77SJung-uk Kim && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
315580815a77SJung-uk Kim BIO_printf(bio_s_out, "LOOKUP renego during read\n");
3156b077aed3SPierre Pronchery
3157b077aed3SPierre Pronchery lookup_srp_user(&srp_callback_parm, bio_s_out);
3158b077aed3SPierre Pronchery
315980815a77SJung-uk Kim continue;
316080815a77SJung-uk Kim }
316180815a77SJung-uk Kim #endif
3162b077aed3SPierre Pronchery ossl_sleep(1000);
316374664626SKris Kennaway continue;
316474664626SKris Kennaway }
31656f9291ceSJung-uk Kim } else if (i == 0) { /* end of input */
316674664626SKris Kennaway ret = 1;
316774664626SKris Kennaway goto end;
316874664626SKris Kennaway }
316974664626SKris Kennaway
317074664626SKris Kennaway /* else we have data */
317174664626SKris Kennaway if (((www == 1) && (strncmp("GET ", buf, 4) == 0)) ||
31726f9291ceSJung-uk Kim ((www == 2) && (strncmp("GET /stats ", buf, 11) == 0))) {
317374664626SKris Kennaway char *p;
3174e71b7053SJung-uk Kim X509 *peer = NULL;
317574664626SKris Kennaway STACK_OF(SSL_CIPHER) *sk;
31763b4e3dcbSSimon L. B. Nielsen static const char *space = " ";
317774664626SKris Kennaway
3178e71b7053SJung-uk Kim if (www == 1 && strncmp("GET /reneg", buf, 10) == 0) {
3179e71b7053SJung-uk Kim if (strncmp("GET /renegcert", buf, 14) == 0)
3180e71b7053SJung-uk Kim SSL_set_verify(con,
3181e71b7053SJung-uk Kim SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
3182e71b7053SJung-uk Kim NULL);
3183e71b7053SJung-uk Kim i = SSL_renegotiate(con);
3184e71b7053SJung-uk Kim BIO_printf(bio_s_out, "SSL_renegotiate -> %d\n", i);
3185e71b7053SJung-uk Kim /* Send the HelloRequest */
3186e71b7053SJung-uk Kim i = SSL_do_handshake(con);
3187e71b7053SJung-uk Kim if (i <= 0) {
3188e71b7053SJung-uk Kim BIO_printf(bio_s_out, "SSL_do_handshake() Retval %d\n",
3189e71b7053SJung-uk Kim SSL_get_error(con, i));
3190e71b7053SJung-uk Kim ERR_print_errors(bio_err);
3191e71b7053SJung-uk Kim goto err;
3192e71b7053SJung-uk Kim }
3193e71b7053SJung-uk Kim /* Wait for a ClientHello to come back */
3194e71b7053SJung-uk Kim FD_ZERO(&readfds);
3195e71b7053SJung-uk Kim openssl_fdset(s, &readfds);
3196e71b7053SJung-uk Kim i = select(width, (void *)&readfds, NULL, NULL, NULL);
3197e71b7053SJung-uk Kim if (i <= 0 || !FD_ISSET(s, &readfds)) {
3198e71b7053SJung-uk Kim BIO_printf(bio_s_out,
3199e71b7053SJung-uk Kim "Error waiting for client response\n");
3200e71b7053SJung-uk Kim ERR_print_errors(bio_err);
3201e71b7053SJung-uk Kim goto err;
3202e71b7053SJung-uk Kim }
3203e71b7053SJung-uk Kim /*
3204e71b7053SJung-uk Kim * We're not actually expecting any data here and we ignore
3205e71b7053SJung-uk Kim * any that is sent. This is just to force the handshake that
3206e71b7053SJung-uk Kim * we're expecting to come from the client. If they haven't
3207e71b7053SJung-uk Kim * sent one there's not much we can do.
3208e71b7053SJung-uk Kim */
3209b077aed3SPierre Pronchery BIO_gets(io, buf, bufsize + 1);
3210e71b7053SJung-uk Kim }
3211e71b7053SJung-uk Kim
32126f9291ceSJung-uk Kim BIO_puts(io,
32136f9291ceSJung-uk Kim "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
321474664626SKris Kennaway BIO_puts(io, "<HTML><BODY BGCOLOR=\"#ffffff\">\n");
321574664626SKris Kennaway BIO_puts(io, "<pre>\n");
3216e71b7053SJung-uk Kim /* BIO_puts(io, OpenSSL_version(OPENSSL_VERSION)); */
321774664626SKris Kennaway BIO_puts(io, "\n");
32186f9291ceSJung-uk Kim for (i = 0; i < local_argc; i++) {
3219e71b7053SJung-uk Kim const char *myp;
3220e71b7053SJung-uk Kim for (myp = local_argv[i]; *myp; myp++)
3221e71b7053SJung-uk Kim switch (*myp) {
3222e71b7053SJung-uk Kim case '<':
3223e71b7053SJung-uk Kim BIO_puts(io, "<");
3224e71b7053SJung-uk Kim break;
3225e71b7053SJung-uk Kim case '>':
3226e71b7053SJung-uk Kim BIO_puts(io, ">");
3227e71b7053SJung-uk Kim break;
3228e71b7053SJung-uk Kim case '&':
3229e71b7053SJung-uk Kim BIO_puts(io, "&");
3230e71b7053SJung-uk Kim break;
3231e71b7053SJung-uk Kim default:
3232e71b7053SJung-uk Kim BIO_write(io, myp, 1);
3233e71b7053SJung-uk Kim break;
3234e71b7053SJung-uk Kim }
323574664626SKris Kennaway BIO_write(io, " ", 1);
323674664626SKris Kennaway }
323774664626SKris Kennaway BIO_puts(io, "\n");
323874664626SKris Kennaway
323909286989SJung-uk Kim BIO_printf(io,
324009286989SJung-uk Kim "Secure Renegotiation IS%s supported\n",
324109286989SJung-uk Kim SSL_get_secure_renegotiation_support(con) ?
324209286989SJung-uk Kim "" : " NOT");
324309286989SJung-uk Kim
32446f9291ceSJung-uk Kim /*
32456f9291ceSJung-uk Kim * The following is evil and should not really be done
32466f9291ceSJung-uk Kim */
324774664626SKris Kennaway BIO_printf(io, "Ciphers supported in s_server binary\n");
324874664626SKris Kennaway sk = SSL_get_ciphers(con);
324974664626SKris Kennaway j = sk_SSL_CIPHER_num(sk);
32506f9291ceSJung-uk Kim for (i = 0; i < j; i++) {
325174664626SKris Kennaway c = sk_SSL_CIPHER_value(sk, i);
325274664626SKris Kennaway BIO_printf(io, "%-11s:%-25s ",
32536f9291ceSJung-uk Kim SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
325474664626SKris Kennaway if ((((i + 1) % 2) == 0) && (i + 1 != j))
325574664626SKris Kennaway BIO_puts(io, "\n");
325674664626SKris Kennaway }
325774664626SKris Kennaway BIO_puts(io, "\n");
325874664626SKris Kennaway p = SSL_get_shared_ciphers(con, buf, bufsize);
32596f9291ceSJung-uk Kim if (p != NULL) {
32606f9291ceSJung-uk Kim BIO_printf(io,
32616f9291ceSJung-uk Kim "---\nCiphers common between both SSL end points:\n");
326274664626SKris Kennaway j = i = 0;
32636f9291ceSJung-uk Kim while (*p) {
32646f9291ceSJung-uk Kim if (*p == ':') {
326574664626SKris Kennaway BIO_write(io, space, 26 - j);
326674664626SKris Kennaway i++;
326774664626SKris Kennaway j = 0;
326874664626SKris Kennaway BIO_write(io, ((i % 3) ? " " : "\n"), 1);
32696f9291ceSJung-uk Kim } else {
327074664626SKris Kennaway BIO_write(io, p, 1);
327174664626SKris Kennaway j++;
327274664626SKris Kennaway }
327374664626SKris Kennaway p++;
327474664626SKris Kennaway }
327574664626SKris Kennaway BIO_puts(io, "\n");
327674664626SKris Kennaway }
32777bded2dbSJung-uk Kim ssl_print_sigalgs(io, con);
32787bded2dbSJung-uk Kim #ifndef OPENSSL_NO_EC
3279e71b7053SJung-uk Kim ssl_print_groups(io, con, 0);
32807bded2dbSJung-uk Kim #endif
3281e71b7053SJung-uk Kim print_ca_names(io, con);
3282e71b7053SJung-uk Kim BIO_printf(io, (SSL_session_reused(con)
32836f9291ceSJung-uk Kim ? "---\nReused, " : "---\nNew, "));
328474664626SKris Kennaway c = SSL_get_current_cipher(con);
328574664626SKris Kennaway BIO_printf(io, "%s, Cipher is %s\n",
32866f9291ceSJung-uk Kim SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
328774664626SKris Kennaway SSL_SESSION_print(io, SSL_get_session(con));
328874664626SKris Kennaway BIO_printf(io, "---\n");
328974664626SKris Kennaway print_stats(io, SSL_get_SSL_CTX(con));
329074664626SKris Kennaway BIO_printf(io, "---\n");
3291b077aed3SPierre Pronchery peer = SSL_get0_peer_certificate(con);
32926f9291ceSJung-uk Kim if (peer != NULL) {
329374664626SKris Kennaway BIO_printf(io, "Client certificate\n");
329474664626SKris Kennaway X509_print(io, peer);
329574664626SKris Kennaway PEM_write_bio_X509(io, peer);
3296e71b7053SJung-uk Kim peer = NULL;
3297e71b7053SJung-uk Kim } else {
329874664626SKris Kennaway BIO_puts(io, "no client certificate available\n");
3299e71b7053SJung-uk Kim }
330047902a71SJung-uk Kim BIO_puts(io, "</pre></BODY></HTML>\r\n\r\n");
330174664626SKris Kennaway break;
33026f9291ceSJung-uk Kim } else if ((www == 2 || www == 3)
33036f9291ceSJung-uk Kim && (strncmp("GET /", buf, 5) == 0)) {
330474664626SKris Kennaway BIO *file;
330574664626SKris Kennaway char *p, *e;
33066f9291ceSJung-uk Kim static const char *text =
33076f9291ceSJung-uk Kim "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
330874664626SKris Kennaway
330974664626SKris Kennaway /* skip the '/' */
331074664626SKris Kennaway p = &(buf[5]);
33115740a5e3SKris Kennaway
33125740a5e3SKris Kennaway dot = 1;
33136f9291ceSJung-uk Kim for (e = p; *e != '\0'; e++) {
33145740a5e3SKris Kennaway if (e[0] == ' ')
33155740a5e3SKris Kennaway break;
331674664626SKris Kennaway
331717f01e99SJung-uk Kim if (e[0] == ':') {
331817f01e99SJung-uk Kim /* Windows drive. We treat this the same way as ".." */
331917f01e99SJung-uk Kim dot = -1;
332017f01e99SJung-uk Kim break;
332117f01e99SJung-uk Kim }
332217f01e99SJung-uk Kim
33236f9291ceSJung-uk Kim switch (dot) {
33245740a5e3SKris Kennaway case 1:
33255740a5e3SKris Kennaway dot = (e[0] == '.') ? 2 : 0;
33265740a5e3SKris Kennaway break;
33275740a5e3SKris Kennaway case 2:
33285740a5e3SKris Kennaway dot = (e[0] == '.') ? 3 : 0;
33295740a5e3SKris Kennaway break;
33305740a5e3SKris Kennaway case 3:
333117f01e99SJung-uk Kim dot = (e[0] == '/' || e[0] == '\\') ? -1 : 0;
33325740a5e3SKris Kennaway break;
33335740a5e3SKris Kennaway }
33345740a5e3SKris Kennaway if (dot == 0)
333517f01e99SJung-uk Kim dot = (e[0] == '/' || e[0] == '\\') ? 1 : 0;
33365740a5e3SKris Kennaway }
33376f9291ceSJung-uk Kim dot = (dot == 3) || (dot == -1); /* filename contains ".."
33386f9291ceSJung-uk Kim * component */
333974664626SKris Kennaway
33406f9291ceSJung-uk Kim if (*e == '\0') {
334174664626SKris Kennaway BIO_puts(io, text);
334274664626SKris Kennaway BIO_printf(io, "'%s' is an invalid file name\r\n", p);
334374664626SKris Kennaway break;
334474664626SKris Kennaway }
334574664626SKris Kennaway *e = '\0';
334674664626SKris Kennaway
33476f9291ceSJung-uk Kim if (dot) {
334874664626SKris Kennaway BIO_puts(io, text);
334917f01e99SJung-uk Kim BIO_printf(io, "'%s' contains '..' or ':'\r\n", p);
335074664626SKris Kennaway break;
335174664626SKris Kennaway }
335274664626SKris Kennaway
335317f01e99SJung-uk Kim if (*p == '/' || *p == '\\') {
335474664626SKris Kennaway BIO_puts(io, text);
335574664626SKris Kennaway BIO_printf(io, "'%s' is an invalid path\r\n", p);
335674664626SKris Kennaway break;
335774664626SKris Kennaway }
335874664626SKris Kennaway
335974664626SKris Kennaway /* if a directory, do the index thang */
33606f9291ceSJung-uk Kim if (app_isdir(p) > 0) {
33615740a5e3SKris Kennaway BIO_puts(io, text);
33625740a5e3SKris Kennaway BIO_printf(io, "'%s' is a directory\r\n", p);
33635740a5e3SKris Kennaway break;
336474664626SKris Kennaway }
336574664626SKris Kennaway
3366b077aed3SPierre Pronchery opmode = (http_server_binmode == 1) ? "rb" : "r";
3367b077aed3SPierre Pronchery if ((file = BIO_new_file(p, opmode)) == NULL) {
336874664626SKris Kennaway BIO_puts(io, text);
3369b077aed3SPierre Pronchery BIO_printf(io, "Error opening '%s' mode='%s'\r\n", p, opmode);
337074664626SKris Kennaway ERR_print_errors(io);
337174664626SKris Kennaway break;
337274664626SKris Kennaway }
337374664626SKris Kennaway
337474664626SKris Kennaway if (!s_quiet)
337574664626SKris Kennaway BIO_printf(bio_err, "FILE:%s\n", p);
337674664626SKris Kennaway
33776f9291ceSJung-uk Kim if (www == 2) {
337874664626SKris Kennaway i = strlen(p);
337974664626SKris Kennaway if (((i > 5) && (strcmp(&(p[i - 5]), ".html") == 0)) ||
338074664626SKris Kennaway ((i > 4) && (strcmp(&(p[i - 4]), ".php") == 0)) ||
338174664626SKris Kennaway ((i > 4) && (strcmp(&(p[i - 4]), ".htm") == 0)))
33826f9291ceSJung-uk Kim BIO_puts(io,
33836f9291ceSJung-uk Kim "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
338474664626SKris Kennaway else
33856f9291ceSJung-uk Kim BIO_puts(io,
33866f9291ceSJung-uk Kim "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
33875c87c606SMark Murray }
338874664626SKris Kennaway /* send the file */
3389b077aed3SPierre Pronchery #ifndef OPENSSL_NO_KTLS
3390b077aed3SPierre Pronchery if (use_sendfile_for_req && !BIO_get_ktls_send(SSL_get_wbio(con))) {
3391b077aed3SPierre Pronchery BIO_printf(bio_err, "Warning: sendfile requested but KTLS is not available\n");
3392b077aed3SPierre Pronchery use_sendfile_for_req = 0;
3393b077aed3SPierre Pronchery }
3394b077aed3SPierre Pronchery if (use_sendfile_for_req) {
3395b077aed3SPierre Pronchery FILE *fp = NULL;
3396b077aed3SPierre Pronchery int fd;
3397b077aed3SPierre Pronchery struct stat st;
3398b077aed3SPierre Pronchery off_t offset = 0;
3399b077aed3SPierre Pronchery size_t filesize;
3400b077aed3SPierre Pronchery
3401b077aed3SPierre Pronchery BIO_get_fp(file, &fp);
3402b077aed3SPierre Pronchery fd = fileno(fp);
3403b077aed3SPierre Pronchery if (fstat(fd, &st) < 0) {
3404b077aed3SPierre Pronchery BIO_printf(io, "Error fstat '%s'\r\n", p);
3405b077aed3SPierre Pronchery ERR_print_errors(io);
3406b077aed3SPierre Pronchery goto write_error;
3407b077aed3SPierre Pronchery }
3408b077aed3SPierre Pronchery
3409b077aed3SPierre Pronchery filesize = st.st_size;
3410b077aed3SPierre Pronchery if (((int)BIO_flush(io)) < 0)
3411b077aed3SPierre Pronchery goto write_error;
3412b077aed3SPierre Pronchery
3413b077aed3SPierre Pronchery for (;;) {
3414b077aed3SPierre Pronchery i = SSL_sendfile(con, fd, offset, filesize, 0);
3415b077aed3SPierre Pronchery if (i < 0) {
3416b077aed3SPierre Pronchery BIO_printf(io, "Error SSL_sendfile '%s'\r\n", p);
3417b077aed3SPierre Pronchery ERR_print_errors(io);
3418b077aed3SPierre Pronchery break;
3419b077aed3SPierre Pronchery } else {
3420b077aed3SPierre Pronchery offset += i;
3421b077aed3SPierre Pronchery filesize -= i;
3422b077aed3SPierre Pronchery }
3423b077aed3SPierre Pronchery
3424b077aed3SPierre Pronchery if (filesize <= 0) {
3425b077aed3SPierre Pronchery if (!s_quiet)
3426b077aed3SPierre Pronchery BIO_printf(bio_err, "KTLS SENDFILE '%s' OK\n", p);
3427b077aed3SPierre Pronchery
3428b077aed3SPierre Pronchery break;
3429b077aed3SPierre Pronchery }
3430b077aed3SPierre Pronchery }
3431b077aed3SPierre Pronchery } else
3432b077aed3SPierre Pronchery #endif
3433b077aed3SPierre Pronchery {
34346f9291ceSJung-uk Kim for (;;) {
343574664626SKris Kennaway i = BIO_read(file, buf, bufsize);
34366f9291ceSJung-uk Kim if (i <= 0)
34376f9291ceSJung-uk Kim break;
343874664626SKris Kennaway
343974664626SKris Kennaway #ifdef RENEG
344074664626SKris Kennaway total_bytes += i;
3441e71b7053SJung-uk Kim BIO_printf(bio_err, "%d\n", i);
34426f9291ceSJung-uk Kim if (total_bytes > 3 * 1024) {
344374664626SKris Kennaway total_bytes = 0;
3444e71b7053SJung-uk Kim BIO_printf(bio_err, "RENEGOTIATE\n");
344574664626SKris Kennaway SSL_renegotiate(con);
344674664626SKris Kennaway }
344774664626SKris Kennaway #endif
344874664626SKris Kennaway
34496f9291ceSJung-uk Kim for (j = 0; j < i;) {
345074664626SKris Kennaway #ifdef RENEG
34516f9291ceSJung-uk Kim static count = 0;
3452b077aed3SPierre Pronchery if (++count == 13)
34536f9291ceSJung-uk Kim SSL_renegotiate(con);
345474664626SKris Kennaway #endif
345574664626SKris Kennaway k = BIO_write(io, &(buf[j]), i - j);
34566f9291ceSJung-uk Kim if (k <= 0) {
3457e71b7053SJung-uk Kim if (!BIO_should_retry(io)
3458b077aed3SPierre Pronchery && !SSL_waiting_for_async(con)) {
345974664626SKris Kennaway goto write_error;
3460b077aed3SPierre Pronchery } else {
346174664626SKris Kennaway BIO_printf(bio_s_out, "rwrite W BLOCK\n");
346274664626SKris Kennaway }
34636f9291ceSJung-uk Kim } else {
346474664626SKris Kennaway j += k;
346574664626SKris Kennaway }
346674664626SKris Kennaway }
346774664626SKris Kennaway }
3468b077aed3SPierre Pronchery }
346974664626SKris Kennaway write_error:
347074664626SKris Kennaway BIO_free(file);
347174664626SKris Kennaway break;
347274664626SKris Kennaway }
347374664626SKris Kennaway }
347474664626SKris Kennaway
34756f9291ceSJung-uk Kim for (;;) {
347674664626SKris Kennaway i = (int)BIO_flush(io);
34776f9291ceSJung-uk Kim if (i <= 0) {
347874664626SKris Kennaway if (!BIO_should_retry(io))
347974664626SKris Kennaway break;
34806f9291ceSJung-uk Kim } else
348174664626SKris Kennaway break;
348274664626SKris Kennaway }
348374664626SKris Kennaway end:
348474664626SKris Kennaway /* make sure we re-use sessions */
3485b077aed3SPierre Pronchery do_ssl_shutdown(con);
348674664626SKris Kennaway
348774664626SKris Kennaway err:
34886f9291ceSJung-uk Kim OPENSSL_free(buf);
3489b077aed3SPierre Pronchery BIO_free(ssl_bio);
34906f9291ceSJung-uk Kim BIO_free_all(io);
3491e71b7053SJung-uk Kim return ret;
349274664626SKris Kennaway }
349374664626SKris Kennaway
rev_body(int s,int stype,int prot,unsigned char * context)3494e71b7053SJung-uk Kim static int rev_body(int s, int stype, int prot, unsigned char *context)
34957bded2dbSJung-uk Kim {
34967bded2dbSJung-uk Kim char *buf = NULL;
34977bded2dbSJung-uk Kim int i;
34987bded2dbSJung-uk Kim int ret = 1;
34997bded2dbSJung-uk Kim SSL *con;
35007bded2dbSJung-uk Kim BIO *io, *ssl_bio, *sbio;
3501b077aed3SPierre Pronchery #ifdef CHARSET_EBCDIC
3502b077aed3SPierre Pronchery BIO *filter;
3503b077aed3SPierre Pronchery #endif
35047bded2dbSJung-uk Kim
3505b077aed3SPierre Pronchery /* as we use BIO_gets(), and it always null terminates data, we need
3506b077aed3SPierre Pronchery * to allocate 1 byte longer buffer to fit the full 2^14 byte record */
3507b077aed3SPierre Pronchery buf = app_malloc(bufsize + 1, "server rev buffer");
35087bded2dbSJung-uk Kim io = BIO_new(BIO_f_buffer());
35097bded2dbSJung-uk Kim ssl_bio = BIO_new(BIO_f_ssl());
35107bded2dbSJung-uk Kim if ((io == NULL) || (ssl_bio == NULL))
35117bded2dbSJung-uk Kim goto err;
35127bded2dbSJung-uk Kim
35137bded2dbSJung-uk Kim /* lets make the output buffer a reasonable size */
3514b077aed3SPierre Pronchery if (BIO_set_write_buffer_size(io, bufsize) <= 0)
35157bded2dbSJung-uk Kim goto err;
35167bded2dbSJung-uk Kim
35177bded2dbSJung-uk Kim if ((con = SSL_new(ctx)) == NULL)
35187bded2dbSJung-uk Kim goto err;
3519e71b7053SJung-uk Kim
35207bded2dbSJung-uk Kim if (s_tlsextdebug) {
35217bded2dbSJung-uk Kim SSL_set_tlsext_debug_callback(con, tlsext_cb);
35227bded2dbSJung-uk Kim SSL_set_tlsext_debug_arg(con, bio_s_out);
35237bded2dbSJung-uk Kim }
3524e71b7053SJung-uk Kim if (context != NULL
3525e71b7053SJung-uk Kim && !SSL_set_session_id_context(con, context,
3526e71b7053SJung-uk Kim strlen((char *)context))) {
3527e71b7053SJung-uk Kim SSL_free(con);
3528e71b7053SJung-uk Kim ERR_print_errors(bio_err);
3529e71b7053SJung-uk Kim goto err;
35307bded2dbSJung-uk Kim }
35317bded2dbSJung-uk Kim
35327bded2dbSJung-uk Kim sbio = BIO_new_socket(s, BIO_NOCLOSE);
3533b077aed3SPierre Pronchery if (sbio == NULL) {
3534b077aed3SPierre Pronchery SSL_free(con);
3535b077aed3SPierre Pronchery ERR_print_errors(bio_err);
3536b077aed3SPierre Pronchery goto err;
3537b077aed3SPierre Pronchery }
3538b077aed3SPierre Pronchery
35397bded2dbSJung-uk Kim SSL_set_bio(con, sbio, sbio);
35407bded2dbSJung-uk Kim SSL_set_accept_state(con);
35417bded2dbSJung-uk Kim
3542e71b7053SJung-uk Kim /* No need to free |con| after this. Done by BIO_free(ssl_bio) */
35437bded2dbSJung-uk Kim BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
35447bded2dbSJung-uk Kim BIO_push(io, ssl_bio);
3545b077aed3SPierre Pronchery ssl_bio = NULL;
35467bded2dbSJung-uk Kim #ifdef CHARSET_EBCDIC
3547b077aed3SPierre Pronchery filter = BIO_new(BIO_f_ebcdic_filter());
3548b077aed3SPierre Pronchery if (filter == NULL)
3549b077aed3SPierre Pronchery goto err;
3550b077aed3SPierre Pronchery
3551b077aed3SPierre Pronchery io = BIO_push(filter, io);
35527bded2dbSJung-uk Kim #endif
35537bded2dbSJung-uk Kim
35547bded2dbSJung-uk Kim if (s_debug) {
3555b077aed3SPierre Pronchery BIO_set_callback_ex(SSL_get_rbio(con), bio_dump_callback);
35567bded2dbSJung-uk Kim BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
35577bded2dbSJung-uk Kim }
35587bded2dbSJung-uk Kim if (s_msg) {
35597bded2dbSJung-uk Kim #ifndef OPENSSL_NO_SSL_TRACE
35607bded2dbSJung-uk Kim if (s_msg == 2)
35617bded2dbSJung-uk Kim SSL_set_msg_callback(con, SSL_trace);
35627bded2dbSJung-uk Kim else
35637bded2dbSJung-uk Kim #endif
35647bded2dbSJung-uk Kim SSL_set_msg_callback(con, msg_cb);
35657bded2dbSJung-uk Kim SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
35667bded2dbSJung-uk Kim }
35677bded2dbSJung-uk Kim
35687bded2dbSJung-uk Kim for (;;) {
35697bded2dbSJung-uk Kim i = BIO_do_handshake(io);
35707bded2dbSJung-uk Kim if (i > 0)
35717bded2dbSJung-uk Kim break;
35727bded2dbSJung-uk Kim if (!BIO_should_retry(io)) {
35737bded2dbSJung-uk Kim BIO_puts(bio_err, "CONNECTION FAILURE\n");
35747bded2dbSJung-uk Kim ERR_print_errors(bio_err);
35757bded2dbSJung-uk Kim goto end;
35767bded2dbSJung-uk Kim }
357780815a77SJung-uk Kim #ifndef OPENSSL_NO_SRP
357880815a77SJung-uk Kim if (BIO_should_io_special(io)
357980815a77SJung-uk Kim && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
358080815a77SJung-uk Kim BIO_printf(bio_s_out, "LOOKUP renego during accept\n");
3581b077aed3SPierre Pronchery
3582b077aed3SPierre Pronchery lookup_srp_user(&srp_callback_parm, bio_s_out);
3583b077aed3SPierre Pronchery
358480815a77SJung-uk Kim continue;
358580815a77SJung-uk Kim }
358680815a77SJung-uk Kim #endif
35877bded2dbSJung-uk Kim }
35887bded2dbSJung-uk Kim BIO_printf(bio_err, "CONNECTION ESTABLISHED\n");
3589e71b7053SJung-uk Kim print_ssl_summary(con);
35907bded2dbSJung-uk Kim
35917bded2dbSJung-uk Kim for (;;) {
3592b077aed3SPierre Pronchery i = BIO_gets(io, buf, bufsize + 1);
35937bded2dbSJung-uk Kim if (i < 0) { /* error */
35947bded2dbSJung-uk Kim if (!BIO_should_retry(io)) {
35957bded2dbSJung-uk Kim if (!s_quiet)
35967bded2dbSJung-uk Kim ERR_print_errors(bio_err);
35977bded2dbSJung-uk Kim goto err;
35987bded2dbSJung-uk Kim } else {
35997bded2dbSJung-uk Kim BIO_printf(bio_s_out, "read R BLOCK\n");
360080815a77SJung-uk Kim #ifndef OPENSSL_NO_SRP
360180815a77SJung-uk Kim if (BIO_should_io_special(io)
360280815a77SJung-uk Kim && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
360380815a77SJung-uk Kim BIO_printf(bio_s_out, "LOOKUP renego during read\n");
3604b077aed3SPierre Pronchery
3605b077aed3SPierre Pronchery lookup_srp_user(&srp_callback_parm, bio_s_out);
3606b077aed3SPierre Pronchery
360780815a77SJung-uk Kim continue;
360880815a77SJung-uk Kim }
360980815a77SJung-uk Kim #endif
3610b077aed3SPierre Pronchery ossl_sleep(1000);
36117bded2dbSJung-uk Kim continue;
36127bded2dbSJung-uk Kim }
36137bded2dbSJung-uk Kim } else if (i == 0) { /* end of input */
36147bded2dbSJung-uk Kim ret = 1;
36157bded2dbSJung-uk Kim BIO_printf(bio_err, "CONNECTION CLOSED\n");
36167bded2dbSJung-uk Kim goto end;
36177bded2dbSJung-uk Kim } else {
36187bded2dbSJung-uk Kim char *p = buf + i - 1;
36197bded2dbSJung-uk Kim while (i && (*p == '\n' || *p == '\r')) {
36207bded2dbSJung-uk Kim p--;
36217bded2dbSJung-uk Kim i--;
36227bded2dbSJung-uk Kim }
3623e71b7053SJung-uk Kim if (!s_ign_eof && (i == 5) && (strncmp(buf, "CLOSE", 5) == 0)) {
36247bded2dbSJung-uk Kim ret = 1;
36257bded2dbSJung-uk Kim BIO_printf(bio_err, "CONNECTION CLOSED\n");
36267bded2dbSJung-uk Kim goto end;
36277bded2dbSJung-uk Kim }
36287bded2dbSJung-uk Kim BUF_reverse((unsigned char *)buf, NULL, i);
36297bded2dbSJung-uk Kim buf[i] = '\n';
36307bded2dbSJung-uk Kim BIO_write(io, buf, i + 1);
36317bded2dbSJung-uk Kim for (;;) {
36327bded2dbSJung-uk Kim i = BIO_flush(io);
36337bded2dbSJung-uk Kim if (i > 0)
36347bded2dbSJung-uk Kim break;
36357bded2dbSJung-uk Kim if (!BIO_should_retry(io))
36367bded2dbSJung-uk Kim goto end;
36377bded2dbSJung-uk Kim }
36387bded2dbSJung-uk Kim }
36397bded2dbSJung-uk Kim }
36407bded2dbSJung-uk Kim end:
36417bded2dbSJung-uk Kim /* make sure we re-use sessions */
3642b077aed3SPierre Pronchery do_ssl_shutdown(con);
36437bded2dbSJung-uk Kim
36447bded2dbSJung-uk Kim err:
36457bded2dbSJung-uk Kim
36467bded2dbSJung-uk Kim OPENSSL_free(buf);
3647b077aed3SPierre Pronchery BIO_free(ssl_bio);
36487bded2dbSJung-uk Kim BIO_free_all(io);
3649e71b7053SJung-uk Kim return ret;
36507bded2dbSJung-uk Kim }
36517bded2dbSJung-uk Kim
36525c87c606SMark Murray #define MAX_SESSION_ID_ATTEMPTS 10
generate_session_id(SSL * ssl,unsigned char * id,unsigned int * id_len)3653e71b7053SJung-uk Kim static int generate_session_id(SSL *ssl, unsigned char *id,
36545c87c606SMark Murray unsigned int *id_len)
36555c87c606SMark Murray {
36565c87c606SMark Murray unsigned int count = 0;
3657b077aed3SPierre Pronchery unsigned int session_id_prefix_len = strlen(session_id_prefix);
3658b077aed3SPierre Pronchery
36595c87c606SMark Murray do {
3660aeb5019cSJung-uk Kim if (RAND_bytes(id, *id_len) <= 0)
3661ed6b93beSJung-uk Kim return 0;
36626f9291ceSJung-uk Kim /*
36636f9291ceSJung-uk Kim * Prefix the session_id with the required prefix. NB: If our prefix
36646f9291ceSJung-uk Kim * is too long, clip it - but there will be worse effects anyway, eg.
36656f9291ceSJung-uk Kim * the server could only possibly create 1 session ID (ie. the
36666f9291ceSJung-uk Kim * prefix!) so all future session negotiations will fail due to
36676f9291ceSJung-uk Kim * conflicts.
36686f9291ceSJung-uk Kim */
36695c87c606SMark Murray memcpy(id, session_id_prefix,
3670b077aed3SPierre Pronchery (session_id_prefix_len < *id_len) ?
3671b077aed3SPierre Pronchery session_id_prefix_len : *id_len);
36725c87c606SMark Murray }
36735c87c606SMark Murray while (SSL_has_matching_session_id(ssl, id, *id_len) &&
36745c87c606SMark Murray (++count < MAX_SESSION_ID_ATTEMPTS));
36755c87c606SMark Murray if (count >= MAX_SESSION_ID_ATTEMPTS)
36765c87c606SMark Murray return 0;
36775c87c606SMark Murray return 1;
36785c87c606SMark Murray }
36797bded2dbSJung-uk Kim
36807bded2dbSJung-uk Kim /*
36817bded2dbSJung-uk Kim * By default s_server uses an in-memory cache which caches SSL_SESSION
3682b077aed3SPierre Pronchery * structures without any serialization. This hides some bugs which only
36837bded2dbSJung-uk Kim * become apparent in deployed servers. By implementing a basic external
36847bded2dbSJung-uk Kim * session cache some issues can be debugged using s_server.
36857bded2dbSJung-uk Kim */
36867bded2dbSJung-uk Kim
36877bded2dbSJung-uk Kim typedef struct simple_ssl_session_st {
36887bded2dbSJung-uk Kim unsigned char *id;
36897bded2dbSJung-uk Kim unsigned int idlen;
36907bded2dbSJung-uk Kim unsigned char *der;
36917bded2dbSJung-uk Kim int derlen;
36927bded2dbSJung-uk Kim struct simple_ssl_session_st *next;
36937bded2dbSJung-uk Kim } simple_ssl_session;
36947bded2dbSJung-uk Kim
36957bded2dbSJung-uk Kim static simple_ssl_session *first = NULL;
36967bded2dbSJung-uk Kim
add_session(SSL * ssl,SSL_SESSION * session)36977bded2dbSJung-uk Kim static int add_session(SSL *ssl, SSL_SESSION *session)
36987bded2dbSJung-uk Kim {
3699e71b7053SJung-uk Kim simple_ssl_session *sess = app_malloc(sizeof(*sess), "get session");
37007bded2dbSJung-uk Kim unsigned char *p;
37017bded2dbSJung-uk Kim
37027bded2dbSJung-uk Kim SSL_SESSION_get_id(session, &sess->idlen);
37037bded2dbSJung-uk Kim sess->derlen = i2d_SSL_SESSION(session, NULL);
3704e71b7053SJung-uk Kim if (sess->derlen < 0) {
3705e71b7053SJung-uk Kim BIO_printf(bio_err, "Error encoding session\n");
3706e71b7053SJung-uk Kim OPENSSL_free(sess);
3707e71b7053SJung-uk Kim return 0;
3708e71b7053SJung-uk Kim }
37097bded2dbSJung-uk Kim
3710e71b7053SJung-uk Kim sess->id = OPENSSL_memdup(SSL_SESSION_get_id(session, NULL), sess->idlen);
3711e71b7053SJung-uk Kim sess->der = app_malloc(sess->derlen, "get session buffer");
3712e71b7053SJung-uk Kim if (!sess->id) {
3713e71b7053SJung-uk Kim BIO_printf(bio_err, "Out of memory adding to external cache\n");
37147bded2dbSJung-uk Kim OPENSSL_free(sess->id);
37157bded2dbSJung-uk Kim OPENSSL_free(sess->der);
37167bded2dbSJung-uk Kim OPENSSL_free(sess);
37177bded2dbSJung-uk Kim return 0;
37187bded2dbSJung-uk Kim }
37197bded2dbSJung-uk Kim p = sess->der;
3720e71b7053SJung-uk Kim
3721e71b7053SJung-uk Kim /* Assume it still works. */
3722e71b7053SJung-uk Kim if (i2d_SSL_SESSION(session, &p) != sess->derlen) {
3723e71b7053SJung-uk Kim BIO_printf(bio_err, "Unexpected session encoding length\n");
3724e71b7053SJung-uk Kim OPENSSL_free(sess->id);
3725e71b7053SJung-uk Kim OPENSSL_free(sess->der);
3726e71b7053SJung-uk Kim OPENSSL_free(sess);
3727e71b7053SJung-uk Kim return 0;
3728e71b7053SJung-uk Kim }
37297bded2dbSJung-uk Kim
37307bded2dbSJung-uk Kim sess->next = first;
37317bded2dbSJung-uk Kim first = sess;
37327bded2dbSJung-uk Kim BIO_printf(bio_err, "New session added to external cache\n");
37337bded2dbSJung-uk Kim return 0;
37347bded2dbSJung-uk Kim }
37357bded2dbSJung-uk Kim
get_session(SSL * ssl,const unsigned char * id,int idlen,int * do_copy)3736e71b7053SJung-uk Kim static SSL_SESSION *get_session(SSL *ssl, const unsigned char *id, int idlen,
37377bded2dbSJung-uk Kim int *do_copy)
37387bded2dbSJung-uk Kim {
37397bded2dbSJung-uk Kim simple_ssl_session *sess;
37407bded2dbSJung-uk Kim *do_copy = 0;
37417bded2dbSJung-uk Kim for (sess = first; sess; sess = sess->next) {
37427bded2dbSJung-uk Kim if (idlen == (int)sess->idlen && !memcmp(sess->id, id, idlen)) {
37437bded2dbSJung-uk Kim const unsigned char *p = sess->der;
37447bded2dbSJung-uk Kim BIO_printf(bio_err, "Lookup session: cache hit\n");
37457bded2dbSJung-uk Kim return d2i_SSL_SESSION(NULL, &p, sess->derlen);
37467bded2dbSJung-uk Kim }
37477bded2dbSJung-uk Kim }
37487bded2dbSJung-uk Kim BIO_printf(bio_err, "Lookup session: cache miss\n");
37497bded2dbSJung-uk Kim return NULL;
37507bded2dbSJung-uk Kim }
37517bded2dbSJung-uk Kim
del_session(SSL_CTX * sctx,SSL_SESSION * session)37527bded2dbSJung-uk Kim static void del_session(SSL_CTX *sctx, SSL_SESSION *session)
37537bded2dbSJung-uk Kim {
37547bded2dbSJung-uk Kim simple_ssl_session *sess, *prev = NULL;
37557bded2dbSJung-uk Kim const unsigned char *id;
37567bded2dbSJung-uk Kim unsigned int idlen;
37577bded2dbSJung-uk Kim id = SSL_SESSION_get_id(session, &idlen);
37587bded2dbSJung-uk Kim for (sess = first; sess; sess = sess->next) {
37597bded2dbSJung-uk Kim if (idlen == sess->idlen && !memcmp(sess->id, id, idlen)) {
37607bded2dbSJung-uk Kim if (prev)
37617bded2dbSJung-uk Kim prev->next = sess->next;
37627bded2dbSJung-uk Kim else
37637bded2dbSJung-uk Kim first = sess->next;
37647bded2dbSJung-uk Kim OPENSSL_free(sess->id);
37657bded2dbSJung-uk Kim OPENSSL_free(sess->der);
37667bded2dbSJung-uk Kim OPENSSL_free(sess);
37677bded2dbSJung-uk Kim return;
37687bded2dbSJung-uk Kim }
37697bded2dbSJung-uk Kim prev = sess;
37707bded2dbSJung-uk Kim }
37717bded2dbSJung-uk Kim }
37727bded2dbSJung-uk Kim
init_session_cache_ctx(SSL_CTX * sctx)37737bded2dbSJung-uk Kim static void init_session_cache_ctx(SSL_CTX *sctx)
37747bded2dbSJung-uk Kim {
37757bded2dbSJung-uk Kim SSL_CTX_set_session_cache_mode(sctx,
37767bded2dbSJung-uk Kim SSL_SESS_CACHE_NO_INTERNAL |
37777bded2dbSJung-uk Kim SSL_SESS_CACHE_SERVER);
37787bded2dbSJung-uk Kim SSL_CTX_sess_set_new_cb(sctx, add_session);
37797bded2dbSJung-uk Kim SSL_CTX_sess_set_get_cb(sctx, get_session);
37807bded2dbSJung-uk Kim SSL_CTX_sess_set_remove_cb(sctx, del_session);
37817bded2dbSJung-uk Kim }
37827bded2dbSJung-uk Kim
free_sessions(void)37837bded2dbSJung-uk Kim static void free_sessions(void)
37847bded2dbSJung-uk Kim {
37857bded2dbSJung-uk Kim simple_ssl_session *sess, *tsess;
37867bded2dbSJung-uk Kim for (sess = first; sess;) {
37877bded2dbSJung-uk Kim OPENSSL_free(sess->id);
37887bded2dbSJung-uk Kim OPENSSL_free(sess->der);
37897bded2dbSJung-uk Kim tsess = sess;
37907bded2dbSJung-uk Kim sess = sess->next;
37917bded2dbSJung-uk Kim OPENSSL_free(tsess);
37927bded2dbSJung-uk Kim }
37937bded2dbSJung-uk Kim first = NULL;
37947bded2dbSJung-uk Kim }
3795e71b7053SJung-uk Kim
3796e71b7053SJung-uk Kim #endif /* OPENSSL_NO_SOCK */
3797