1e71b7053SJung-uk Kim /* 2b077aed3SPierre Pronchery * Copyright 1995-2023 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 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 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 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 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 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 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 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 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 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 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 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 */ 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 */ 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 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 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 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)"}, 792*6f1af0d7SPierre 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 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 } 16731f13597dSJung-uk Kim #endif 16741f13597dSJung-uk Kim 1675e71b7053SJung-uk Kim if (stateless && socket_type != SOCK_STREAM) { 1676e71b7053SJung-uk Kim BIO_printf(bio_err, "Can only use --stateless with TLS\n"); 1677aeb5019cSJung-uk Kim goto end; 1678aeb5019cSJung-uk Kim } 1679aeb5019cSJung-uk Kim 1680e71b7053SJung-uk Kim #ifdef AF_UNIX 1681e71b7053SJung-uk Kim if (socket_family == AF_UNIX && socket_type != SOCK_STREAM) { 1682e71b7053SJung-uk Kim BIO_printf(bio_err, 1683e71b7053SJung-uk Kim "Can't use unix sockets and datagrams together\n"); 1684aeb5019cSJung-uk Kim goto end; 1685aeb5019cSJung-uk Kim } 1686e71b7053SJung-uk Kim #endif 1687c9cf7b5cSJung-uk Kim if (early_data && (www > 0 || rev)) { 1688c9cf7b5cSJung-uk Kim BIO_printf(bio_err, 1689c9cf7b5cSJung-uk Kim "Can't use -early_data in combination with -www, -WWW, -HTTP, or -rev\n"); 1690c9cf7b5cSJung-uk Kim goto end; 1691c9cf7b5cSJung-uk Kim } 1692aeb5019cSJung-uk Kim 1693e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SCTP 1694e71b7053SJung-uk Kim if (protocol == IPPROTO_SCTP) { 1695e71b7053SJung-uk Kim if (socket_type != SOCK_DGRAM) { 1696e71b7053SJung-uk Kim BIO_printf(bio_err, "Can't use -sctp without DTLS\n"); 1697e71b7053SJung-uk Kim goto end; 1698e71b7053SJung-uk Kim } 1699e71b7053SJung-uk Kim /* SCTP is unusual. It uses DTLS over a SOCK_STREAM protocol */ 1700e71b7053SJung-uk Kim socket_type = SOCK_STREAM; 1701e71b7053SJung-uk Kim } 1702e71b7053SJung-uk Kim #endif 17035c87c606SMark Murray 1704b077aed3SPierre Pronchery #ifndef OPENSSL_NO_KTLS 1705b077aed3SPierre Pronchery if (use_sendfile && www <= 1) { 1706b077aed3SPierre Pronchery BIO_printf(bio_err, "Can't use -sendfile without -WWW or -HTTP\n"); 1707b077aed3SPierre Pronchery goto end; 1708b077aed3SPierre Pronchery } 1709b077aed3SPierre Pronchery #endif 1710b077aed3SPierre Pronchery 1711e71b7053SJung-uk Kim if (!app_passwd(passarg, dpassarg, &pass, &dpass)) { 17123b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err, "Error getting password\n"); 17133b4e3dcbSSimon L. B. Nielsen goto end; 17143b4e3dcbSSimon L. B. Nielsen } 17153b4e3dcbSSimon L. B. Nielsen 17163b4e3dcbSSimon L. B. Nielsen if (s_key_file == NULL) 17173b4e3dcbSSimon L. B. Nielsen s_key_file = s_cert_file; 1718e71b7053SJung-uk Kim 1719db522d3aSSimon L. B. Nielsen if (s_key_file2 == NULL) 1720db522d3aSSimon L. B. Nielsen s_key_file2 = s_cert_file2; 17213b4e3dcbSSimon L. B. Nielsen 1722e71b7053SJung-uk Kim if (!load_excert(&exc)) 17237bded2dbSJung-uk Kim goto end; 17247bded2dbSJung-uk Kim 17256f9291ceSJung-uk Kim if (nocert == 0) { 1726e71b7053SJung-uk Kim s_key = load_key(s_key_file, s_key_format, 0, pass, engine, 1727b077aed3SPierre Pronchery "server certificate private key"); 1728b077aed3SPierre Pronchery if (s_key == NULL) 17293b4e3dcbSSimon L. B. Nielsen goto end; 17303b4e3dcbSSimon L. B. Nielsen 1731b077aed3SPierre Pronchery s_cert = load_cert_pass(s_cert_file, s_cert_format, 1, pass, 1732b077aed3SPierre Pronchery "server certificate"); 17333b4e3dcbSSimon L. B. Nielsen 1734b077aed3SPierre Pronchery if (s_cert == NULL) 17353b4e3dcbSSimon L. B. Nielsen goto end; 1736e71b7053SJung-uk Kim if (s_chain_file != NULL) { 1737b077aed3SPierre Pronchery if (!load_certs(s_chain_file, 0, &s_chain, NULL, 1738e71b7053SJung-uk Kim "server certificate chain")) 17397bded2dbSJung-uk Kim goto end; 17407bded2dbSJung-uk Kim } 1741e71b7053SJung-uk Kim 1742e71b7053SJung-uk Kim if (tlsextcbp.servername != NULL) { 1743e71b7053SJung-uk Kim s_key2 = load_key(s_key_file2, s_key_format, 0, pass, engine, 1744b077aed3SPierre Pronchery "second server certificate private key"); 1745b077aed3SPierre Pronchery if (s_key2 == NULL) 1746db522d3aSSimon L. B. Nielsen goto end; 17473b4e3dcbSSimon L. B. Nielsen 1748b077aed3SPierre Pronchery s_cert2 = load_cert_pass(s_cert_file2, s_cert_format, 1, pass, 1749b077aed3SPierre Pronchery "second server certificate"); 1750db522d3aSSimon L. B. Nielsen 1751b077aed3SPierre Pronchery if (s_cert2 == NULL) 1752db522d3aSSimon L. B. Nielsen goto end; 1753db522d3aSSimon L. B. Nielsen } 1754db522d3aSSimon L. B. Nielsen } 17557bded2dbSJung-uk Kim #if !defined(OPENSSL_NO_NEXTPROTONEG) 17566f9291ceSJung-uk Kim if (next_proto_neg_in) { 1757e71b7053SJung-uk Kim next_proto.data = next_protos_parse(&next_proto.len, next_proto_neg_in); 17581f13597dSJung-uk Kim if (next_proto.data == NULL) 17591f13597dSJung-uk Kim goto end; 17601f13597dSJung-uk Kim } 17611f13597dSJung-uk Kim #endif 17627bded2dbSJung-uk Kim alpn_ctx.data = NULL; 17637bded2dbSJung-uk Kim if (alpn_in) { 1764e71b7053SJung-uk Kim alpn_ctx.data = next_protos_parse(&alpn_ctx.len, alpn_in); 17657bded2dbSJung-uk Kim if (alpn_ctx.data == NULL) 17667bded2dbSJung-uk Kim goto end; 17677bded2dbSJung-uk Kim } 17687bded2dbSJung-uk Kim 1769e71b7053SJung-uk Kim if (crl_file != NULL) { 17707bded2dbSJung-uk Kim X509_CRL *crl; 1771b077aed3SPierre Pronchery crl = load_crl(crl_file, crl_format, 0, "CRL"); 1772b077aed3SPierre Pronchery if (crl == NULL) 17737bded2dbSJung-uk Kim goto end; 17747bded2dbSJung-uk Kim crls = sk_X509_CRL_new_null(); 1775e71b7053SJung-uk Kim if (crls == NULL || !sk_X509_CRL_push(crls, crl)) { 17767bded2dbSJung-uk Kim BIO_puts(bio_err, "Error adding CRL\n"); 17777bded2dbSJung-uk Kim ERR_print_errors(bio_err); 17787bded2dbSJung-uk Kim X509_CRL_free(crl); 17797bded2dbSJung-uk Kim goto end; 17807bded2dbSJung-uk Kim } 17817bded2dbSJung-uk Kim } 17821f13597dSJung-uk Kim 1783e71b7053SJung-uk Kim if (s_dcert_file != NULL) { 17843b4e3dcbSSimon L. B. Nielsen 17853b4e3dcbSSimon L. B. Nielsen if (s_dkey_file == NULL) 17863b4e3dcbSSimon L. B. Nielsen s_dkey_file = s_dcert_file; 17873b4e3dcbSSimon L. B. Nielsen 1788e71b7053SJung-uk Kim s_dkey = load_key(s_dkey_file, s_dkey_format, 1789b077aed3SPierre Pronchery 0, dpass, engine, "second certificate private key"); 1790b077aed3SPierre Pronchery if (s_dkey == NULL) 17913b4e3dcbSSimon L. B. Nielsen goto end; 17923b4e3dcbSSimon L. B. Nielsen 1793b077aed3SPierre Pronchery s_dcert = load_cert_pass(s_dcert_file, s_dcert_format, 1, dpass, 1794b077aed3SPierre Pronchery "second server certificate"); 17953b4e3dcbSSimon L. B. Nielsen 1796e71b7053SJung-uk Kim if (s_dcert == NULL) { 17973b4e3dcbSSimon L. B. Nielsen ERR_print_errors(bio_err); 17983b4e3dcbSSimon L. B. Nielsen goto end; 17993b4e3dcbSSimon L. B. Nielsen } 1800e71b7053SJung-uk Kim if (s_dchain_file != NULL) { 1801b077aed3SPierre Pronchery if (!load_certs(s_dchain_file, 0, &s_dchain, NULL, 1802e71b7053SJung-uk Kim "second server certificate chain")) 18037bded2dbSJung-uk Kim goto end; 18047bded2dbSJung-uk Kim } 18053b4e3dcbSSimon L. B. Nielsen 18063b4e3dcbSSimon L. B. Nielsen } 18073b4e3dcbSSimon L. B. Nielsen 18086f9291ceSJung-uk Kim if (bio_s_out == NULL) { 18097bded2dbSJung-uk Kim if (s_quiet && !s_debug) { 181074664626SKris Kennaway bio_s_out = BIO_new(BIO_s_null()); 1811b077aed3SPierre Pronchery if (s_msg && bio_s_msg == NULL) { 1812e71b7053SJung-uk Kim bio_s_msg = dup_bio_out(FORMAT_TEXT); 1813b077aed3SPierre Pronchery if (bio_s_msg == NULL) { 1814b077aed3SPierre Pronchery BIO_printf(bio_err, "Out of memory\n"); 1815b077aed3SPierre Pronchery goto end; 1816b077aed3SPierre Pronchery } 1817b077aed3SPierre Pronchery } 18186f9291ceSJung-uk Kim } else { 1819e71b7053SJung-uk Kim bio_s_out = dup_bio_out(FORMAT_TEXT); 182074664626SKris Kennaway } 182174664626SKris Kennaway } 1822b077aed3SPierre Pronchery 1823b077aed3SPierre Pronchery if (bio_s_out == NULL) 1824b077aed3SPierre Pronchery goto end; 1825b077aed3SPierre Pronchery 1826b077aed3SPierre Pronchery if (nocert) { 182774664626SKris Kennaway s_cert_file = NULL; 182874664626SKris Kennaway s_key_file = NULL; 182974664626SKris Kennaway s_dcert_file = NULL; 183074664626SKris Kennaway s_dkey_file = NULL; 1831db522d3aSSimon L. B. Nielsen s_cert_file2 = NULL; 1832db522d3aSSimon L. B. Nielsen s_key_file2 = NULL; 183374664626SKris Kennaway } 183474664626SKris Kennaway 1835b077aed3SPierre Pronchery ctx = SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth); 18366f9291ceSJung-uk Kim if (ctx == NULL) { 183774664626SKris Kennaway ERR_print_errors(bio_err); 183874664626SKris Kennaway goto end; 183974664626SKris Kennaway } 1840e71b7053SJung-uk Kim 1841e71b7053SJung-uk Kim SSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY); 1842e71b7053SJung-uk Kim 1843e71b7053SJung-uk Kim if (sdebug) 1844e71b7053SJung-uk Kim ssl_ctx_security_debug(ctx, sdebug); 1845e71b7053SJung-uk Kim 1846e71b7053SJung-uk Kim if (!config_ctx(cctx, ssl_args, ctx)) 1847e71b7053SJung-uk Kim goto end; 1848e71b7053SJung-uk Kim 1849e71b7053SJung-uk Kim if (ssl_config) { 1850e71b7053SJung-uk Kim if (SSL_CTX_config(ctx, ssl_config) == 0) { 1851e71b7053SJung-uk Kim BIO_printf(bio_err, "Error using configuration \"%s\"\n", 1852e71b7053SJung-uk Kim ssl_config); 1853e71b7053SJung-uk Kim ERR_print_errors(bio_err); 1854e71b7053SJung-uk Kim goto end; 1855e71b7053SJung-uk Kim } 1856e71b7053SJung-uk Kim } 18576935a639SJung-uk Kim #ifndef OPENSSL_NO_SCTP 18586935a639SJung-uk Kim if (protocol == IPPROTO_SCTP && sctp_label_bug == 1) 18596935a639SJung-uk Kim SSL_CTX_set_mode(ctx, SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG); 18606935a639SJung-uk Kim #endif 18616935a639SJung-uk Kim 1862e71b7053SJung-uk Kim if (min_version != 0 1863e71b7053SJung-uk Kim && SSL_CTX_set_min_proto_version(ctx, min_version) == 0) 1864e71b7053SJung-uk Kim goto end; 1865e71b7053SJung-uk Kim if (max_version != 0 1866e71b7053SJung-uk Kim && SSL_CTX_set_max_proto_version(ctx, max_version) == 0) 1867e71b7053SJung-uk Kim goto end; 1868e71b7053SJung-uk Kim 18696f9291ceSJung-uk Kim if (session_id_prefix) { 18705c87c606SMark Murray if (strlen(session_id_prefix) >= 32) 18715c87c606SMark Murray BIO_printf(bio_err, 18725c87c606SMark Murray "warning: id_prefix is too long, only one new session will be possible\n"); 18736f9291ceSJung-uk Kim if (!SSL_CTX_set_generate_session_id(ctx, generate_session_id)) { 18745c87c606SMark Murray BIO_printf(bio_err, "error setting 'id_prefix'\n"); 18755c87c606SMark Murray ERR_print_errors(bio_err); 18765c87c606SMark Murray goto end; 18775c87c606SMark Murray } 18785c87c606SMark Murray BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix); 18795c87c606SMark Murray } 1880e71b7053SJung-uk Kim if (exc != NULL) 18817bded2dbSJung-uk Kim ssl_ctx_set_excert(ctx, exc); 188274664626SKris Kennaway 18836f9291ceSJung-uk Kim if (state) 18846f9291ceSJung-uk Kim SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback); 18856a599222SSimon L. B. Nielsen if (no_cache) 18866a599222SSimon L. B. Nielsen SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); 18877bded2dbSJung-uk Kim else if (ext_cache) 18887bded2dbSJung-uk Kim init_session_cache_ctx(ctx); 18896a599222SSimon L. B. Nielsen else 189074664626SKris Kennaway SSL_CTX_sess_set_cache_size(ctx, 128); 189174664626SKris Kennaway 1892e71b7053SJung-uk Kim if (async) { 1893e71b7053SJung-uk Kim SSL_CTX_set_mode(ctx, SSL_MODE_ASYNC); 1894e71b7053SJung-uk Kim } 18951f13597dSJung-uk Kim 1896b077aed3SPierre Pronchery if (no_ca_names) { 1897b077aed3SPierre Pronchery SSL_CTX_set_options(ctx, SSL_OP_DISABLE_TLSEXT_CA_NAMES); 1898b077aed3SPierre Pronchery } 1899b077aed3SPierre Pronchery 1900b077aed3SPierre Pronchery if (ignore_unexpected_eof) 1901b077aed3SPierre Pronchery SSL_CTX_set_options(ctx, SSL_OP_IGNORE_UNEXPECTED_EOF); 1902b077aed3SPierre Pronchery 1903e71b7053SJung-uk Kim if (max_send_fragment > 0 1904e71b7053SJung-uk Kim && !SSL_CTX_set_max_send_fragment(ctx, max_send_fragment)) { 1905e71b7053SJung-uk Kim BIO_printf(bio_err, "%s: Max send fragment size %u is out of permitted range\n", 1906e71b7053SJung-uk Kim prog, max_send_fragment); 190774664626SKris Kennaway goto end; 190874664626SKris Kennaway } 1909e71b7053SJung-uk Kim 1910e71b7053SJung-uk Kim if (split_send_fragment > 0 1911e71b7053SJung-uk Kim && !SSL_CTX_set_split_send_fragment(ctx, split_send_fragment)) { 1912e71b7053SJung-uk Kim BIO_printf(bio_err, "%s: Split send fragment size %u is out of permitted range\n", 1913e71b7053SJung-uk Kim prog, split_send_fragment); 1914e71b7053SJung-uk Kim goto end; 1915e71b7053SJung-uk Kim } 1916e71b7053SJung-uk Kim if (max_pipelines > 0 1917e71b7053SJung-uk Kim && !SSL_CTX_set_max_pipelines(ctx, max_pipelines)) { 1918e71b7053SJung-uk Kim BIO_printf(bio_err, "%s: Max pipelines %u is out of permitted range\n", 1919e71b7053SJung-uk Kim prog, max_pipelines); 1920e71b7053SJung-uk Kim goto end; 1921e71b7053SJung-uk Kim } 1922e71b7053SJung-uk Kim 1923e71b7053SJung-uk Kim if (read_buf_len > 0) { 1924e71b7053SJung-uk Kim SSL_CTX_set_default_read_buffer_len(ctx, read_buf_len); 1925e71b7053SJung-uk Kim } 1926e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SRTP 1927e71b7053SJung-uk Kim if (srtp_profiles != NULL) { 1928e71b7053SJung-uk Kim /* Returns 0 on success! */ 1929e71b7053SJung-uk Kim if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles) != 0) { 1930e71b7053SJung-uk Kim BIO_printf(bio_err, "Error setting SRTP profile\n"); 1931e71b7053SJung-uk Kim ERR_print_errors(bio_err); 1932e71b7053SJung-uk Kim goto end; 1933e71b7053SJung-uk Kim } 1934e71b7053SJung-uk Kim } 193574664626SKris Kennaway #endif 193674664626SKris Kennaway 1937b077aed3SPierre Pronchery if (!ctx_set_verify_locations(ctx, CAfile, noCAfile, CApath, noCApath, 1938b077aed3SPierre Pronchery CAstore, noCAstore)) { 193974664626SKris Kennaway ERR_print_errors(bio_err); 1940e71b7053SJung-uk Kim goto end; 194174664626SKris Kennaway } 1942e71b7053SJung-uk Kim if (vpmtouched && !SSL_CTX_set1_param(ctx, vpm)) { 1943e71b7053SJung-uk Kim BIO_printf(bio_err, "Error setting verify params\n"); 1944e71b7053SJung-uk Kim ERR_print_errors(bio_err); 1945e71b7053SJung-uk Kim goto end; 1946e71b7053SJung-uk Kim } 19471f13597dSJung-uk Kim 19487bded2dbSJung-uk Kim ssl_ctx_add_crls(ctx, crls, 0); 19497bded2dbSJung-uk Kim 1950b077aed3SPierre Pronchery if (!ssl_load_stores(ctx, 1951b077aed3SPierre Pronchery vfyCApath, vfyCAfile, vfyCAstore, 1952b077aed3SPierre Pronchery chCApath, chCAfile, chCAstore, 19537bded2dbSJung-uk Kim crls, crl_download)) { 19547bded2dbSJung-uk Kim BIO_printf(bio_err, "Error loading store locations\n"); 19557bded2dbSJung-uk Kim ERR_print_errors(bio_err); 19567bded2dbSJung-uk Kim goto end; 19577bded2dbSJung-uk Kim } 1958e71b7053SJung-uk Kim 19596f9291ceSJung-uk Kim if (s_cert2) { 1960b077aed3SPierre Pronchery ctx2 = SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth); 19616f9291ceSJung-uk Kim if (ctx2 == NULL) { 1962db522d3aSSimon L. B. Nielsen ERR_print_errors(bio_err); 1963db522d3aSSimon L. B. Nielsen goto end; 1964db522d3aSSimon L. B. Nielsen } 1965db522d3aSSimon L. B. Nielsen } 1966db522d3aSSimon L. B. Nielsen 1967e71b7053SJung-uk Kim if (ctx2 != NULL) { 1968db522d3aSSimon L. B. Nielsen BIO_printf(bio_s_out, "Setting secondary ctx parameters\n"); 1969db522d3aSSimon L. B. Nielsen 1970e71b7053SJung-uk Kim if (sdebug) 197111c7efe3SJung-uk Kim ssl_ctx_security_debug(ctx2, sdebug); 1972e71b7053SJung-uk Kim 19736f9291ceSJung-uk Kim if (session_id_prefix) { 1974db522d3aSSimon L. B. Nielsen if (strlen(session_id_prefix) >= 32) 1975db522d3aSSimon L. B. Nielsen BIO_printf(bio_err, 1976db522d3aSSimon L. B. Nielsen "warning: id_prefix is too long, only one new session will be possible\n"); 19776f9291ceSJung-uk Kim if (!SSL_CTX_set_generate_session_id(ctx2, generate_session_id)) { 1978db522d3aSSimon L. B. Nielsen BIO_printf(bio_err, "error setting 'id_prefix'\n"); 1979db522d3aSSimon L. B. Nielsen ERR_print_errors(bio_err); 1980db522d3aSSimon L. B. Nielsen goto end; 1981db522d3aSSimon L. B. Nielsen } 1982db522d3aSSimon L. B. Nielsen BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix); 1983db522d3aSSimon L. B. Nielsen } 1984e71b7053SJung-uk Kim if (exc != NULL) 19857bded2dbSJung-uk Kim ssl_ctx_set_excert(ctx2, exc); 1986db522d3aSSimon L. B. Nielsen 19876f9291ceSJung-uk Kim if (state) 19886f9291ceSJung-uk Kim SSL_CTX_set_info_callback(ctx2, apps_ssl_info_callback); 1989db522d3aSSimon L. B. Nielsen 19906a599222SSimon L. B. Nielsen if (no_cache) 19916a599222SSimon L. B. Nielsen SSL_CTX_set_session_cache_mode(ctx2, SSL_SESS_CACHE_OFF); 19927bded2dbSJung-uk Kim else if (ext_cache) 19937bded2dbSJung-uk Kim init_session_cache_ctx(ctx2); 19946a599222SSimon L. B. Nielsen else 1995db522d3aSSimon L. B. Nielsen SSL_CTX_sess_set_cache_size(ctx2, 128); 1996db522d3aSSimon L. B. Nielsen 1997e71b7053SJung-uk Kim if (async) 1998e71b7053SJung-uk Kim SSL_CTX_set_mode(ctx2, SSL_MODE_ASYNC); 1999e71b7053SJung-uk Kim 2000b077aed3SPierre Pronchery if (!ctx_set_verify_locations(ctx2, CAfile, noCAfile, CApath, 2001b077aed3SPierre Pronchery noCApath, CAstore, noCAstore)) { 2002db522d3aSSimon L. B. Nielsen ERR_print_errors(bio_err); 2003e71b7053SJung-uk Kim goto end; 2004db522d3aSSimon L. B. Nielsen } 2005e71b7053SJung-uk Kim if (vpmtouched && !SSL_CTX_set1_param(ctx2, vpm)) { 2006e71b7053SJung-uk Kim BIO_printf(bio_err, "Error setting verify params\n"); 2007e71b7053SJung-uk Kim ERR_print_errors(bio_err); 2008e71b7053SJung-uk Kim goto end; 2009e71b7053SJung-uk Kim } 20107bded2dbSJung-uk Kim 20117bded2dbSJung-uk Kim ssl_ctx_add_crls(ctx2, crls, 0); 2012e71b7053SJung-uk Kim if (!config_ctx(cctx, ssl_args, ctx2)) 20137bded2dbSJung-uk Kim goto end; 2014db522d3aSSimon L. B. Nielsen } 20151f13597dSJung-uk Kim #ifndef OPENSSL_NO_NEXTPROTONEG 20161f13597dSJung-uk Kim if (next_proto.data) 20176f9291ceSJung-uk Kim SSL_CTX_set_next_protos_advertised_cb(ctx, next_proto_cb, 20186f9291ceSJung-uk Kim &next_proto); 20191f13597dSJung-uk Kim #endif 20207bded2dbSJung-uk Kim if (alpn_ctx.data) 20217bded2dbSJung-uk Kim SSL_CTX_set_alpn_select_cb(ctx, alpn_cb, &alpn_ctx); 202274664626SKris Kennaway 20236f9291ceSJung-uk Kim if (!no_dhe) { 2024b077aed3SPierre Pronchery EVP_PKEY *dhpkey = NULL; 20255c87c606SMark Murray 2026e71b7053SJung-uk Kim if (dhfile != NULL) 2027b077aed3SPierre Pronchery dhpkey = load_keyparams(dhfile, FORMAT_UNDEF, 0, "DH", "DH parameters"); 2028e71b7053SJung-uk Kim else if (s_cert_file != NULL) 2029b077aed3SPierre Pronchery dhpkey = load_keyparams_suppress(s_cert_file, FORMAT_UNDEF, 0, "DH", 2030b077aed3SPierre Pronchery "DH parameters", 1); 20315c87c606SMark Murray 2032b077aed3SPierre Pronchery if (dhpkey != NULL) { 203374664626SKris Kennaway BIO_printf(bio_s_out, "Setting temp DH parameters\n"); 20346f9291ceSJung-uk Kim } else { 203574664626SKris Kennaway BIO_printf(bio_s_out, "Using default temp DH parameters\n"); 203674664626SKris Kennaway } 203774664626SKris Kennaway (void)BIO_flush(bio_s_out); 203874664626SKris Kennaway 2039b077aed3SPierre Pronchery if (dhpkey == NULL) { 2040e71b7053SJung-uk Kim SSL_CTX_set_dh_auto(ctx, 1); 2041b077aed3SPierre Pronchery } else { 2042b077aed3SPierre Pronchery /* 2043b077aed3SPierre Pronchery * We need 2 references: one for use by ctx and one for use by 2044b077aed3SPierre Pronchery * ctx2 2045b077aed3SPierre Pronchery */ 2046b077aed3SPierre Pronchery if (!EVP_PKEY_up_ref(dhpkey)) { 2047b077aed3SPierre Pronchery EVP_PKEY_free(dhpkey); 2048b077aed3SPierre Pronchery goto end; 2049b077aed3SPierre Pronchery } 2050b077aed3SPierre Pronchery if (!SSL_CTX_set0_tmp_dh_pkey(ctx, dhpkey)) { 2051e71b7053SJung-uk Kim BIO_puts(bio_err, "Error setting temp DH parameters\n"); 2052e71b7053SJung-uk Kim ERR_print_errors(bio_err); 2053b077aed3SPierre Pronchery /* Free 2 references */ 2054b077aed3SPierre Pronchery EVP_PKEY_free(dhpkey); 2055b077aed3SPierre Pronchery EVP_PKEY_free(dhpkey); 2056e71b7053SJung-uk Kim goto end; 2057e71b7053SJung-uk Kim } 2058b077aed3SPierre Pronchery } 2059e71b7053SJung-uk Kim 2060e71b7053SJung-uk Kim if (ctx2 != NULL) { 2061b077aed3SPierre Pronchery if (dhfile != NULL) { 2062b077aed3SPierre Pronchery EVP_PKEY *dhpkey2 = load_keyparams_suppress(s_cert_file2, 2063b077aed3SPierre Pronchery FORMAT_UNDEF, 2064b077aed3SPierre Pronchery 0, "DH", 2065b077aed3SPierre Pronchery "DH parameters", 1); 2066b077aed3SPierre Pronchery 2067b077aed3SPierre Pronchery if (dhpkey2 != NULL) { 2068db522d3aSSimon L. B. Nielsen BIO_printf(bio_s_out, "Setting temp DH parameters\n"); 2069db522d3aSSimon L. B. Nielsen (void)BIO_flush(bio_s_out); 2070db522d3aSSimon L. B. Nielsen 2071b077aed3SPierre Pronchery EVP_PKEY_free(dhpkey); 2072b077aed3SPierre Pronchery dhpkey = dhpkey2; 2073db522d3aSSimon L. B. Nielsen } 2074db522d3aSSimon L. B. Nielsen } 2075b077aed3SPierre Pronchery if (dhpkey == NULL) { 2076e71b7053SJung-uk Kim SSL_CTX_set_dh_auto(ctx2, 1); 2077b077aed3SPierre Pronchery } else if (!SSL_CTX_set0_tmp_dh_pkey(ctx2, dhpkey)) { 2078e71b7053SJung-uk Kim BIO_puts(bio_err, "Error setting temp DH parameters\n"); 2079e71b7053SJung-uk Kim ERR_print_errors(bio_err); 2080b077aed3SPierre Pronchery EVP_PKEY_free(dhpkey); 2081e71b7053SJung-uk Kim goto end; 2082db522d3aSSimon L. B. Nielsen } 2083b077aed3SPierre Pronchery dhpkey = NULL; 2084e71b7053SJung-uk Kim } 2085b077aed3SPierre Pronchery EVP_PKEY_free(dhpkey); 208674664626SKris Kennaway } 208774664626SKris Kennaway 20887bded2dbSJung-uk Kim if (!set_cert_key_stuff(ctx, s_cert, s_key, s_chain, build_chain)) 20893b4e3dcbSSimon L. B. Nielsen goto end; 2090e71b7053SJung-uk Kim 20917bded2dbSJung-uk Kim if (s_serverinfo_file != NULL 20927bded2dbSJung-uk Kim && !SSL_CTX_use_serverinfo_file(ctx, s_serverinfo_file)) { 20937bded2dbSJung-uk Kim ERR_print_errors(bio_err); 20947bded2dbSJung-uk Kim goto end; 20957bded2dbSJung-uk Kim } 2096e71b7053SJung-uk Kim 2097e71b7053SJung-uk Kim if (ctx2 != NULL 2098e71b7053SJung-uk Kim && !set_cert_key_stuff(ctx2, s_cert2, s_key2, NULL, build_chain)) 2099db522d3aSSimon L. B. Nielsen goto end; 2100e71b7053SJung-uk Kim 21016f9291ceSJung-uk Kim if (s_dcert != NULL) { 21027bded2dbSJung-uk Kim if (!set_cert_key_stuff(ctx, s_dcert, s_dkey, s_dchain, build_chain)) 210374664626SKris Kennaway goto end; 210474664626SKris Kennaway } 210574664626SKris Kennaway 2106e71b7053SJung-uk Kim if (no_resume_ephemeral) { 2107e71b7053SJung-uk Kim SSL_CTX_set_not_resumable_session_callback(ctx, 2108e71b7053SJung-uk Kim not_resumable_sess_cb); 210974664626SKris Kennaway 2110e71b7053SJung-uk Kim if (ctx2 != NULL) 2111e71b7053SJung-uk Kim SSL_CTX_set_not_resumable_session_callback(ctx2, 2112e71b7053SJung-uk Kim not_resumable_sess_cb); 211374664626SKris Kennaway } 21141f13597dSJung-uk Kim #ifndef OPENSSL_NO_PSK 2115e71b7053SJung-uk Kim if (psk_key != NULL) { 21161f13597dSJung-uk Kim if (s_debug) 2117e71b7053SJung-uk Kim BIO_printf(bio_s_out, "PSK key given, setting server callback\n"); 21181f13597dSJung-uk Kim SSL_CTX_set_psk_server_callback(ctx, psk_server_cb); 21191f13597dSJung-uk Kim } 21201f13597dSJung-uk Kim 2121b077aed3SPierre Pronchery if (psk_identity_hint != NULL) { 2122b077aed3SPierre Pronchery if (min_version == TLS1_3_VERSION) { 2123b077aed3SPierre Pronchery BIO_printf(bio_s_out, "PSK warning: there is NO identity hint in TLSv1.3\n"); 2124b077aed3SPierre Pronchery } else { 21256f9291ceSJung-uk Kim if (!SSL_CTX_use_psk_identity_hint(ctx, psk_identity_hint)) { 21261f13597dSJung-uk Kim BIO_printf(bio_err, "error setting PSK identity hint to context\n"); 21271f13597dSJung-uk Kim ERR_print_errors(bio_err); 21281f13597dSJung-uk Kim goto end; 21291f13597dSJung-uk Kim } 2130b077aed3SPierre Pronchery } 2131b077aed3SPierre Pronchery } 21321f13597dSJung-uk Kim #endif 2133e71b7053SJung-uk Kim if (psksessf != NULL) { 2134e71b7053SJung-uk Kim BIO *stmp = BIO_new_file(psksessf, "r"); 2135e71b7053SJung-uk Kim 2136e71b7053SJung-uk Kim if (stmp == NULL) { 2137e71b7053SJung-uk Kim BIO_printf(bio_err, "Can't open PSK session file %s\n", psksessf); 2138e71b7053SJung-uk Kim ERR_print_errors(bio_err); 2139e71b7053SJung-uk Kim goto end; 2140e71b7053SJung-uk Kim } 2141e71b7053SJung-uk Kim psksess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL); 2142e71b7053SJung-uk Kim BIO_free(stmp); 2143e71b7053SJung-uk Kim if (psksess == NULL) { 2144e71b7053SJung-uk Kim BIO_printf(bio_err, "Can't read PSK session file %s\n", psksessf); 2145e71b7053SJung-uk Kim ERR_print_errors(bio_err); 2146e71b7053SJung-uk Kim goto end; 2147e71b7053SJung-uk Kim } 2148e71b7053SJung-uk Kim 2149e71b7053SJung-uk Kim } 2150e71b7053SJung-uk Kim 2151e71b7053SJung-uk Kim if (psk_key != NULL || psksess != NULL) 2152e71b7053SJung-uk Kim SSL_CTX_set_psk_find_session_callback(ctx, psk_find_session_cb); 21531f13597dSJung-uk Kim 215474664626SKris Kennaway SSL_CTX_set_verify(ctx, s_server_verify, verify_callback); 2155e71b7053SJung-uk Kim if (!SSL_CTX_set_session_id_context(ctx, 2156e71b7053SJung-uk Kim (void *)&s_server_session_id_context, 2157e71b7053SJung-uk Kim sizeof(s_server_session_id_context))) { 2158e71b7053SJung-uk Kim BIO_printf(bio_err, "error setting session id context\n"); 2159e71b7053SJung-uk Kim ERR_print_errors(bio_err); 2160e71b7053SJung-uk Kim goto end; 2161e71b7053SJung-uk Kim } 216274664626SKris Kennaway 21636a599222SSimon L. B. Nielsen /* Set DTLS cookie generation and verification callbacks */ 21646a599222SSimon L. B. Nielsen SSL_CTX_set_cookie_generate_cb(ctx, generate_cookie_callback); 21656a599222SSimon L. B. Nielsen SSL_CTX_set_cookie_verify_cb(ctx, verify_cookie_callback); 21666a599222SSimon L. B. Nielsen 2167e71b7053SJung-uk Kim /* Set TLS1.3 cookie generation and verification callbacks */ 2168e71b7053SJung-uk Kim SSL_CTX_set_stateless_cookie_generate_cb(ctx, generate_stateless_cookie_callback); 2169e71b7053SJung-uk Kim SSL_CTX_set_stateless_cookie_verify_cb(ctx, verify_stateless_cookie_callback); 217074664626SKris Kennaway 2171e71b7053SJung-uk Kim if (ctx2 != NULL) { 2172e71b7053SJung-uk Kim SSL_CTX_set_verify(ctx2, s_server_verify, verify_callback); 2173e71b7053SJung-uk Kim if (!SSL_CTX_set_session_id_context(ctx2, 2174e71b7053SJung-uk Kim (void *)&s_server_session_id_context, 2175e71b7053SJung-uk Kim sizeof(s_server_session_id_context))) { 2176e71b7053SJung-uk Kim BIO_printf(bio_err, "error setting session id context\n"); 2177e71b7053SJung-uk Kim ERR_print_errors(bio_err); 2178e71b7053SJung-uk Kim goto end; 2179e71b7053SJung-uk Kim } 2180db522d3aSSimon L. B. Nielsen tlsextcbp.biodebug = bio_s_out; 2181db522d3aSSimon L. B. Nielsen SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb); 2182db522d3aSSimon L. B. Nielsen SSL_CTX_set_tlsext_servername_arg(ctx2, &tlsextcbp); 2183db522d3aSSimon L. B. Nielsen SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb); 2184db522d3aSSimon L. B. Nielsen SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp); 2185db522d3aSSimon L. B. Nielsen } 21861f13597dSJung-uk Kim 21871f13597dSJung-uk Kim #ifndef OPENSSL_NO_SRP 21886f9291ceSJung-uk Kim if (srp_verifier_file != NULL) { 2189b077aed3SPierre Pronchery if (!set_up_srp_verifier_file(ctx, &srp_callback_parm, srpuserseed, 2190b077aed3SPierre Pronchery srp_verifier_file)) 21911f13597dSJung-uk Kim goto end; 21926f9291ceSJung-uk Kim } else 21931f13597dSJung-uk Kim #endif 21946f9291ceSJung-uk Kim if (CAfile != NULL) { 2195db522d3aSSimon L. B. Nielsen SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(CAfile)); 2196e71b7053SJung-uk Kim 2197db522d3aSSimon L. B. Nielsen if (ctx2) 2198db522d3aSSimon L. B. Nielsen SSL_CTX_set_client_CA_list(ctx2, SSL_load_client_CA_file(CAfile)); 2199db522d3aSSimon L. B. Nielsen } 2200e71b7053SJung-uk Kim #ifndef OPENSSL_NO_OCSP 2201e71b7053SJung-uk Kim if (s_tlsextstatus) { 2202e71b7053SJung-uk Kim SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb); 2203e71b7053SJung-uk Kim SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp); 2204e71b7053SJung-uk Kim if (ctx2) { 2205e71b7053SJung-uk Kim SSL_CTX_set_tlsext_status_cb(ctx2, cert_status_cb); 2206e71b7053SJung-uk Kim SSL_CTX_set_tlsext_status_arg(ctx2, &tlscstatp); 2207e71b7053SJung-uk Kim } 2208e71b7053SJung-uk Kim } 2209e71b7053SJung-uk Kim #endif 2210e71b7053SJung-uk Kim if (set_keylog_file(ctx, keylog_file)) 2211e71b7053SJung-uk Kim goto end; 22121f13597dSJung-uk Kim 2213e71b7053SJung-uk Kim if (max_early_data >= 0) 2214e71b7053SJung-uk Kim SSL_CTX_set_max_early_data(ctx, max_early_data); 2215e71b7053SJung-uk Kim if (recv_max_early_data >= 0) 2216e71b7053SJung-uk Kim SSL_CTX_set_recv_max_early_data(ctx, recv_max_early_data); 2217e71b7053SJung-uk Kim 22187bded2dbSJung-uk Kim if (rev) 2219e71b7053SJung-uk Kim server_cb = rev_body; 22207bded2dbSJung-uk Kim else if (www) 2221e71b7053SJung-uk Kim server_cb = www_body; 222274664626SKris Kennaway else 2223e71b7053SJung-uk Kim server_cb = sv_body; 2224e71b7053SJung-uk Kim #ifdef AF_UNIX 2225e71b7053SJung-uk Kim if (socket_family == AF_UNIX 2226e71b7053SJung-uk Kim && unlink_unix_path) 2227e71b7053SJung-uk Kim unlink(host); 2228e71b7053SJung-uk Kim #endif 2229e71b7053SJung-uk Kim do_server(&accept_socket, host, port, socket_family, socket_type, protocol, 2230e71b7053SJung-uk Kim server_cb, context, naccept, bio_s_out); 223174664626SKris Kennaway print_stats(bio_s_out, ctx); 223274664626SKris Kennaway ret = 0; 223374664626SKris Kennaway end: 22346f9291ceSJung-uk Kim SSL_CTX_free(ctx); 2235e71b7053SJung-uk Kim SSL_SESSION_free(psksess); 2236e71b7053SJung-uk Kim set_keylog_file(NULL, NULL); 22373b4e3dcbSSimon L. B. Nielsen X509_free(s_cert); 22387bded2dbSJung-uk Kim sk_X509_CRL_pop_free(crls, X509_CRL_free); 22393b4e3dcbSSimon L. B. Nielsen X509_free(s_dcert); 22403b4e3dcbSSimon L. B. Nielsen EVP_PKEY_free(s_key); 22413b4e3dcbSSimon L. B. Nielsen EVP_PKEY_free(s_dkey); 22427bded2dbSJung-uk Kim sk_X509_pop_free(s_chain, X509_free); 22437bded2dbSJung-uk Kim sk_X509_pop_free(s_dchain, X509_free); 22443b4e3dcbSSimon L. B. Nielsen OPENSSL_free(pass); 22453b4e3dcbSSimon L. B. Nielsen OPENSSL_free(dpass); 2246e71b7053SJung-uk Kim OPENSSL_free(host); 2247e71b7053SJung-uk Kim OPENSSL_free(port); 224809286989SJung-uk Kim X509_VERIFY_PARAM_free(vpm); 22497bded2dbSJung-uk Kim free_sessions(); 225009286989SJung-uk Kim OPENSSL_free(tlscstatp.host); 225109286989SJung-uk Kim OPENSSL_free(tlscstatp.port); 225209286989SJung-uk Kim OPENSSL_free(tlscstatp.path); 22536f9291ceSJung-uk Kim SSL_CTX_free(ctx2); 2254db522d3aSSimon L. B. Nielsen X509_free(s_cert2); 2255db522d3aSSimon L. B. Nielsen EVP_PKEY_free(s_key2); 22567bded2dbSJung-uk Kim #ifndef OPENSSL_NO_NEXTPROTONEG 22577bded2dbSJung-uk Kim OPENSSL_free(next_proto.data); 22587bded2dbSJung-uk Kim #endif 22597bded2dbSJung-uk Kim OPENSSL_free(alpn_ctx.data); 22607bded2dbSJung-uk Kim ssl_excert_free(exc); 22617bded2dbSJung-uk Kim sk_OPENSSL_STRING_free(ssl_args); 22627bded2dbSJung-uk Kim SSL_CONF_CTX_free(cctx); 2263e71b7053SJung-uk Kim release_engine(engine); 226474664626SKris Kennaway BIO_free(bio_s_out); 226574664626SKris Kennaway bio_s_out = NULL; 22667bded2dbSJung-uk Kim BIO_free(bio_s_msg); 22677bded2dbSJung-uk Kim bio_s_msg = NULL; 2268e71b7053SJung-uk Kim #ifdef CHARSET_EBCDIC 2269e71b7053SJung-uk Kim BIO_meth_free(methods_ebcdic); 2270e71b7053SJung-uk Kim #endif 2271e71b7053SJung-uk Kim return ret; 227274664626SKris Kennaway } 227374664626SKris Kennaway 227474664626SKris Kennaway static void print_stats(BIO *bio, SSL_CTX *ssl_ctx) 227574664626SKris Kennaway { 227674664626SKris Kennaway BIO_printf(bio, "%4ld items in the session cache\n", 227774664626SKris Kennaway SSL_CTX_sess_number(ssl_ctx)); 22783b4e3dcbSSimon L. B. Nielsen BIO_printf(bio, "%4ld client connects (SSL_connect())\n", 227974664626SKris Kennaway SSL_CTX_sess_connect(ssl_ctx)); 22803b4e3dcbSSimon L. B. Nielsen BIO_printf(bio, "%4ld client renegotiates (SSL_connect())\n", 228174664626SKris Kennaway SSL_CTX_sess_connect_renegotiate(ssl_ctx)); 22823b4e3dcbSSimon L. B. Nielsen BIO_printf(bio, "%4ld client connects that finished\n", 228374664626SKris Kennaway SSL_CTX_sess_connect_good(ssl_ctx)); 22843b4e3dcbSSimon L. B. Nielsen BIO_printf(bio, "%4ld server accepts (SSL_accept())\n", 228574664626SKris Kennaway SSL_CTX_sess_accept(ssl_ctx)); 22863b4e3dcbSSimon L. B. Nielsen BIO_printf(bio, "%4ld server renegotiates (SSL_accept())\n", 228774664626SKris Kennaway SSL_CTX_sess_accept_renegotiate(ssl_ctx)); 22883b4e3dcbSSimon L. B. Nielsen BIO_printf(bio, "%4ld server accepts that finished\n", 228974664626SKris Kennaway SSL_CTX_sess_accept_good(ssl_ctx)); 22903b4e3dcbSSimon L. B. Nielsen BIO_printf(bio, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx)); 22916f9291ceSJung-uk Kim BIO_printf(bio, "%4ld session cache misses\n", 22926f9291ceSJung-uk Kim SSL_CTX_sess_misses(ssl_ctx)); 22936f9291ceSJung-uk Kim BIO_printf(bio, "%4ld session cache timeouts\n", 22946f9291ceSJung-uk Kim SSL_CTX_sess_timeouts(ssl_ctx)); 22956f9291ceSJung-uk Kim BIO_printf(bio, "%4ld callback cache hits\n", 22966f9291ceSJung-uk Kim SSL_CTX_sess_cb_hits(ssl_ctx)); 22973b4e3dcbSSimon L. B. Nielsen BIO_printf(bio, "%4ld cache full overflows (%ld allowed)\n", 229874664626SKris Kennaway SSL_CTX_sess_cache_full(ssl_ctx), 229974664626SKris Kennaway SSL_CTX_sess_get_cache_size(ssl_ctx)); 230074664626SKris Kennaway } 230174664626SKris Kennaway 2302b077aed3SPierre Pronchery static long int count_reads_callback(BIO *bio, int cmd, const char *argp, size_t len, 2303b077aed3SPierre Pronchery int argi, long argl, int ret, size_t *processed) 230483eaf7aeSJung-uk Kim { 230583eaf7aeSJung-uk Kim unsigned int *p_counter = (unsigned int *)BIO_get_callback_arg(bio); 230683eaf7aeSJung-uk Kim 230783eaf7aeSJung-uk Kim switch (cmd) { 230883eaf7aeSJung-uk Kim case BIO_CB_READ: /* No break here */ 230983eaf7aeSJung-uk Kim case BIO_CB_GETS: 231083eaf7aeSJung-uk Kim if (p_counter != NULL) 231183eaf7aeSJung-uk Kim ++*p_counter; 231283eaf7aeSJung-uk Kim break; 231383eaf7aeSJung-uk Kim default: 231483eaf7aeSJung-uk Kim break; 231583eaf7aeSJung-uk Kim } 231683eaf7aeSJung-uk Kim 231783eaf7aeSJung-uk Kim if (s_debug) { 231883eaf7aeSJung-uk Kim BIO_set_callback_arg(bio, (char *)bio_s_out); 2319b077aed3SPierre Pronchery ret = (int)bio_dump_callback(bio, cmd, argp, len, argi, argl, ret, processed); 232083eaf7aeSJung-uk Kim BIO_set_callback_arg(bio, (char *)p_counter); 232183eaf7aeSJung-uk Kim } 232283eaf7aeSJung-uk Kim 232383eaf7aeSJung-uk Kim return ret; 232483eaf7aeSJung-uk Kim } 232583eaf7aeSJung-uk Kim 2326e71b7053SJung-uk Kim static int sv_body(int s, int stype, int prot, unsigned char *context) 232774664626SKris Kennaway { 232874664626SKris Kennaway char *buf = NULL; 232974664626SKris Kennaway fd_set readfds; 233074664626SKris Kennaway int ret = 1, width; 233174664626SKris Kennaway int k, i; 233274664626SKris Kennaway unsigned long l; 233374664626SKris Kennaway SSL *con = NULL; 233474664626SKris Kennaway BIO *sbio; 23356a599222SSimon L. B. Nielsen struct timeval timeout; 2336e71b7053SJung-uk Kim #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)) 23376a599222SSimon L. B. Nielsen struct timeval *timeoutp; 2338f579bf8eSKris Kennaway #endif 2339e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS 2340e71b7053SJung-uk Kim # ifndef OPENSSL_NO_SCTP 2341e71b7053SJung-uk Kim int isdtls = (stype == SOCK_DGRAM || prot == IPPROTO_SCTP); 2342e71b7053SJung-uk Kim # else 2343e71b7053SJung-uk Kim int isdtls = (stype == SOCK_DGRAM); 2344e71b7053SJung-uk Kim # endif 234574664626SKris Kennaway #endif 234674664626SKris Kennaway 2347e71b7053SJung-uk Kim buf = app_malloc(bufsize, "server buffer"); 2348e71b7053SJung-uk Kim if (s_nbio) { 2349e71b7053SJung-uk Kim if (!BIO_socket_nbio(s, 1)) 2350e71b7053SJung-uk Kim ERR_print_errors(bio_err); 2351e71b7053SJung-uk Kim else if (!s_quiet) 2352e71b7053SJung-uk Kim BIO_printf(bio_err, "Turned on non blocking io\n"); 2353e71b7053SJung-uk Kim } 2354e71b7053SJung-uk Kim 2355f579bf8eSKris Kennaway con = SSL_new(ctx); 2356e71b7053SJung-uk Kim if (con == NULL) { 2357e71b7053SJung-uk Kim ret = -1; 2358e71b7053SJung-uk Kim goto err; 2359e71b7053SJung-uk Kim } 2360e71b7053SJung-uk Kim 23616f9291ceSJung-uk Kim if (s_tlsextdebug) { 2362db522d3aSSimon L. B. Nielsen SSL_set_tlsext_debug_callback(con, tlsext_cb); 2363db522d3aSSimon L. B. Nielsen SSL_set_tlsext_debug_arg(con, bio_s_out); 2364db522d3aSSimon L. B. Nielsen } 236574664626SKris Kennaway 2366e71b7053SJung-uk Kim if (context != NULL 2367e71b7053SJung-uk Kim && !SSL_set_session_id_context(con, context, 2368e71b7053SJung-uk Kim strlen((char *)context))) { 2369e71b7053SJung-uk Kim BIO_printf(bio_err, "Error setting session id context\n"); 2370e71b7053SJung-uk Kim ret = -1; 2371e71b7053SJung-uk Kim goto err; 2372e71b7053SJung-uk Kim } 23733b4e3dcbSSimon L. B. Nielsen 2374e71b7053SJung-uk Kim if (!SSL_clear(con)) { 2375e71b7053SJung-uk Kim BIO_printf(bio_err, "Error clearing SSL connection\n"); 2376e71b7053SJung-uk Kim ret = -1; 2377e71b7053SJung-uk Kim goto err; 2378e71b7053SJung-uk Kim } 2379e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS 2380e71b7053SJung-uk Kim if (isdtls) { 2381e71b7053SJung-uk Kim # ifndef OPENSSL_NO_SCTP 2382e71b7053SJung-uk Kim if (prot == IPPROTO_SCTP) 2383e71b7053SJung-uk Kim sbio = BIO_new_dgram_sctp(s, BIO_NOCLOSE); 2384e71b7053SJung-uk Kim else 2385e71b7053SJung-uk Kim # endif 23863b4e3dcbSSimon L. B. Nielsen sbio = BIO_new_dgram(s, BIO_NOCLOSE); 2387b077aed3SPierre Pronchery if (sbio == NULL) { 2388b077aed3SPierre Pronchery BIO_printf(bio_err, "Unable to create BIO\n"); 2389b077aed3SPierre Pronchery ERR_print_errors(bio_err); 2390b077aed3SPierre Pronchery goto err; 2391b077aed3SPierre Pronchery } 23923b4e3dcbSSimon L. B. Nielsen 23936f9291ceSJung-uk Kim if (enable_timeouts) { 23943b4e3dcbSSimon L. B. Nielsen timeout.tv_sec = 0; 23953b4e3dcbSSimon L. B. Nielsen timeout.tv_usec = DGRAM_RCV_TIMEOUT; 23963b4e3dcbSSimon L. B. Nielsen BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout); 23973b4e3dcbSSimon L. B. Nielsen 23983b4e3dcbSSimon L. B. Nielsen timeout.tv_sec = 0; 23993b4e3dcbSSimon L. B. Nielsen timeout.tv_usec = DGRAM_SND_TIMEOUT; 24003b4e3dcbSSimon L. B. Nielsen BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout); 24013b4e3dcbSSimon L. B. Nielsen } 24023b4e3dcbSSimon L. B. Nielsen 24036f9291ceSJung-uk Kim if (socket_mtu) { 24046f9291ceSJung-uk Kim if (socket_mtu < DTLS_get_link_min_mtu(con)) { 2405751d2991SJung-uk Kim BIO_printf(bio_err, "MTU too small. Must be at least %ld\n", 2406751d2991SJung-uk Kim DTLS_get_link_min_mtu(con)); 2407751d2991SJung-uk Kim ret = -1; 2408751d2991SJung-uk Kim BIO_free(sbio); 2409751d2991SJung-uk Kim goto err; 2410751d2991SJung-uk Kim } 24113b4e3dcbSSimon L. B. Nielsen SSL_set_options(con, SSL_OP_NO_QUERY_MTU); 24126f9291ceSJung-uk Kim if (!DTLS_set_link_mtu(con, socket_mtu)) { 2413751d2991SJung-uk Kim BIO_printf(bio_err, "Failed to set MTU\n"); 2414751d2991SJung-uk Kim ret = -1; 2415751d2991SJung-uk Kim BIO_free(sbio); 2416751d2991SJung-uk Kim goto err; 2417751d2991SJung-uk Kim } 24186f9291ceSJung-uk Kim } else 24193b4e3dcbSSimon L. B. Nielsen /* want to do MTU discovery */ 24203b4e3dcbSSimon L. B. Nielsen BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL); 24213b4e3dcbSSimon L. B. Nielsen 2422e71b7053SJung-uk Kim # ifndef OPENSSL_NO_SCTP 2423e71b7053SJung-uk Kim if (prot != IPPROTO_SCTP) 2424e71b7053SJung-uk Kim # endif 2425e71b7053SJung-uk Kim /* Turn on cookie exchange. Not necessary for SCTP */ 24263b4e3dcbSSimon L. B. Nielsen SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE); 24276f9291ceSJung-uk Kim } else 2428e71b7053SJung-uk Kim #endif 242974664626SKris Kennaway sbio = BIO_new_socket(s, BIO_NOCLOSE); 24303b4e3dcbSSimon L. B. Nielsen 2431e71b7053SJung-uk Kim if (sbio == NULL) { 2432e71b7053SJung-uk Kim BIO_printf(bio_err, "Unable to create BIO\n"); 2433e71b7053SJung-uk Kim ERR_print_errors(bio_err); 2434e71b7053SJung-uk Kim goto err; 2435e71b7053SJung-uk Kim } 2436e71b7053SJung-uk Kim 24376f9291ceSJung-uk Kim if (s_nbio_test) { 243874664626SKris Kennaway BIO *test; 243974664626SKris Kennaway 244074664626SKris Kennaway test = BIO_new(BIO_f_nbio_test()); 2441b077aed3SPierre Pronchery if (test == NULL) { 2442b077aed3SPierre Pronchery BIO_printf(bio_err, "Unable to create BIO\n"); 2443b077aed3SPierre Pronchery ret = -1; 2444b077aed3SPierre Pronchery BIO_free(sbio); 2445b077aed3SPierre Pronchery goto err; 2446b077aed3SPierre Pronchery } 2447b077aed3SPierre Pronchery 244874664626SKris Kennaway sbio = BIO_push(test, sbio); 244974664626SKris Kennaway } 2450db522d3aSSimon L. B. Nielsen 245174664626SKris Kennaway SSL_set_bio(con, sbio, sbio); 245274664626SKris Kennaway SSL_set_accept_state(con); 245374664626SKris Kennaway /* SSL_set_fd(con,s); */ 245474664626SKris Kennaway 2455b077aed3SPierre Pronchery BIO_set_callback_ex(SSL_get_rbio(con), count_reads_callback); 24566f9291ceSJung-uk Kim if (s_msg) { 24577bded2dbSJung-uk Kim #ifndef OPENSSL_NO_SSL_TRACE 24587bded2dbSJung-uk Kim if (s_msg == 2) 24597bded2dbSJung-uk Kim SSL_set_msg_callback(con, SSL_trace); 24607bded2dbSJung-uk Kim else 24617bded2dbSJung-uk Kim #endif 24625c87c606SMark Murray SSL_set_msg_callback(con, msg_cb); 24637bded2dbSJung-uk Kim SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out); 24645c87c606SMark Murray } 2465e71b7053SJung-uk Kim 24666f9291ceSJung-uk Kim if (s_tlsextdebug) { 2467db522d3aSSimon L. B. Nielsen SSL_set_tlsext_debug_callback(con, tlsext_cb); 2468db522d3aSSimon L. B. Nielsen SSL_set_tlsext_debug_arg(con, bio_s_out); 2469db522d3aSSimon L. B. Nielsen } 2470e71b7053SJung-uk Kim 2471e71b7053SJung-uk Kim if (early_data) { 2472e71b7053SJung-uk Kim int write_header = 1, edret = SSL_READ_EARLY_DATA_ERROR; 2473e71b7053SJung-uk Kim size_t readbytes; 2474e71b7053SJung-uk Kim 2475e71b7053SJung-uk Kim while (edret != SSL_READ_EARLY_DATA_FINISH) { 2476e71b7053SJung-uk Kim for (;;) { 2477e71b7053SJung-uk Kim edret = SSL_read_early_data(con, buf, bufsize, &readbytes); 2478e71b7053SJung-uk Kim if (edret != SSL_READ_EARLY_DATA_ERROR) 2479e71b7053SJung-uk Kim break; 2480e71b7053SJung-uk Kim 2481e71b7053SJung-uk Kim switch (SSL_get_error(con, 0)) { 2482e71b7053SJung-uk Kim case SSL_ERROR_WANT_WRITE: 2483e71b7053SJung-uk Kim case SSL_ERROR_WANT_ASYNC: 2484e71b7053SJung-uk Kim case SSL_ERROR_WANT_READ: 2485e71b7053SJung-uk Kim /* Just keep trying - busy waiting */ 2486e71b7053SJung-uk Kim continue; 2487e71b7053SJung-uk Kim default: 2488e71b7053SJung-uk Kim BIO_printf(bio_err, "Error reading early data\n"); 2489e71b7053SJung-uk Kim ERR_print_errors(bio_err); 2490e71b7053SJung-uk Kim goto err; 2491e71b7053SJung-uk Kim } 2492e71b7053SJung-uk Kim } 2493e71b7053SJung-uk Kim if (readbytes > 0) { 2494e71b7053SJung-uk Kim if (write_header) { 2495e71b7053SJung-uk Kim BIO_printf(bio_s_out, "Early data received:\n"); 2496e71b7053SJung-uk Kim write_header = 0; 2497e71b7053SJung-uk Kim } 2498e71b7053SJung-uk Kim raw_write_stdout(buf, (unsigned int)readbytes); 2499e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out); 2500e71b7053SJung-uk Kim } 2501e71b7053SJung-uk Kim } 2502e71b7053SJung-uk Kim if (write_header) { 2503e71b7053SJung-uk Kim if (SSL_get_early_data_status(con) == SSL_EARLY_DATA_NOT_SENT) 2504e71b7053SJung-uk Kim BIO_printf(bio_s_out, "No early data received\n"); 2505e71b7053SJung-uk Kim else 2506e71b7053SJung-uk Kim BIO_printf(bio_s_out, "Early data was rejected\n"); 2507e71b7053SJung-uk Kim } else { 2508e71b7053SJung-uk Kim BIO_printf(bio_s_out, "\nEnd of early data\n"); 2509e71b7053SJung-uk Kim } 2510e71b7053SJung-uk Kim if (SSL_is_init_finished(con)) 2511e71b7053SJung-uk Kim print_connection_info(con); 2512e71b7053SJung-uk Kim } 251374664626SKris Kennaway 2514aeb5019cSJung-uk Kim if (fileno_stdin() > s) 2515aeb5019cSJung-uk Kim width = fileno_stdin() + 1; 2516aeb5019cSJung-uk Kim else 251774664626SKris Kennaway width = s + 1; 25186f9291ceSJung-uk Kim for (;;) { 2519f579bf8eSKris Kennaway int read_from_terminal; 2520f579bf8eSKris Kennaway int read_from_sslcon; 2521f579bf8eSKris Kennaway 2522f579bf8eSKris Kennaway read_from_terminal = 0; 2523e71b7053SJung-uk Kim read_from_sslcon = SSL_has_pending(con) 2524e71b7053SJung-uk Kim || (async && SSL_waiting_for_async(con)); 2525f579bf8eSKris Kennaway 25266f9291ceSJung-uk Kim if (!read_from_sslcon) { 252774664626SKris Kennaway FD_ZERO(&readfds); 2528e71b7053SJung-uk Kim #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) 2529aeb5019cSJung-uk Kim openssl_fdset(fileno_stdin(), &readfds); 253074664626SKris Kennaway #endif 25311f13597dSJung-uk Kim openssl_fdset(s, &readfds); 25326f9291ceSJung-uk Kim /* 25336f9291ceSJung-uk Kim * Note: under VMS with SOCKETSHR the second parameter is 25346f9291ceSJung-uk Kim * currently of type (int *) whereas under other systems it is 25356f9291ceSJung-uk Kim * (void *) if you don't have a cast it will choke the compiler: 25366f9291ceSJung-uk Kim * if you do have a cast then you can either go for (int *) or 25376f9291ceSJung-uk Kim * (void *). 253874664626SKris Kennaway */ 2539e71b7053SJung-uk Kim #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) 25406f9291ceSJung-uk Kim /* 25416f9291ceSJung-uk Kim * Under DOS (non-djgpp) and Windows we can't select on stdin: 25426f9291ceSJung-uk Kim * only on sockets. As a workaround we timeout the select every 2543f579bf8eSKris Kennaway * second and check for any keypress. In a proper Windows 2544f579bf8eSKris Kennaway * application we wouldn't do this because it is inefficient. 2545f579bf8eSKris Kennaway */ 2546e71b7053SJung-uk Kim timeout.tv_sec = 1; 2547e71b7053SJung-uk Kim timeout.tv_usec = 0; 2548e71b7053SJung-uk Kim i = select(width, (void *)&readfds, NULL, NULL, &timeout); 2549e71b7053SJung-uk Kim if (has_stdin_waiting()) 2550f579bf8eSKris Kennaway read_from_terminal = 1; 2551e71b7053SJung-uk Kim if ((i < 0) || (!i && !read_from_terminal)) 25521f13597dSJung-uk Kim continue; 2553f579bf8eSKris Kennaway #else 2554e71b7053SJung-uk Kim if (SSL_is_dtls(con) && DTLSv1_get_timeout(con, &timeout)) 25556a599222SSimon L. B. Nielsen timeoutp = &timeout; 25566a599222SSimon L. B. Nielsen else 25576a599222SSimon L. B. Nielsen timeoutp = NULL; 25586a599222SSimon L. B. Nielsen 25596a599222SSimon L. B. Nielsen i = select(width, (void *)&readfds, NULL, NULL, timeoutp); 25606a599222SSimon L. B. Nielsen 2561e71b7053SJung-uk Kim if ((SSL_is_dtls(con)) && DTLSv1_handle_timeout(con) > 0) 2562e71b7053SJung-uk Kim BIO_printf(bio_err, "TIMEOUT occurred\n"); 25636a599222SSimon L. B. Nielsen 25646f9291ceSJung-uk Kim if (i <= 0) 25656f9291ceSJung-uk Kim continue; 2566aeb5019cSJung-uk Kim if (FD_ISSET(fileno_stdin(), &readfds)) 2567f579bf8eSKris Kennaway read_from_terminal = 1; 2568f579bf8eSKris Kennaway #endif 2569f579bf8eSKris Kennaway if (FD_ISSET(s, &readfds)) 2570f579bf8eSKris Kennaway read_from_sslcon = 1; 2571f579bf8eSKris Kennaway } 25726f9291ceSJung-uk Kim if (read_from_terminal) { 25736f9291ceSJung-uk Kim if (s_crlf) { 257474664626SKris Kennaway int j, lf_num; 257574664626SKris Kennaway 25761f13597dSJung-uk Kim i = raw_read_stdin(buf, bufsize / 2); 257774664626SKris Kennaway lf_num = 0; 257874664626SKris Kennaway /* both loops are skipped when i <= 0 */ 257974664626SKris Kennaway for (j = 0; j < i; j++) 258074664626SKris Kennaway if (buf[j] == '\n') 258174664626SKris Kennaway lf_num++; 25826f9291ceSJung-uk Kim for (j = i - 1; j >= 0; j--) { 258374664626SKris Kennaway buf[j + lf_num] = buf[j]; 25846f9291ceSJung-uk Kim if (buf[j] == '\n') { 258574664626SKris Kennaway lf_num--; 258674664626SKris Kennaway i++; 258774664626SKris Kennaway buf[j + lf_num] = '\r'; 258874664626SKris Kennaway } 258974664626SKris Kennaway } 259074664626SKris Kennaway assert(lf_num == 0); 2591e71b7053SJung-uk Kim } else { 25921f13597dSJung-uk Kim i = raw_read_stdin(buf, bufsize); 2593e71b7053SJung-uk Kim } 2594aeb5019cSJung-uk Kim 25957bded2dbSJung-uk Kim if (!s_quiet && !s_brief) { 25966f9291ceSJung-uk Kim if ((i <= 0) || (buf[0] == 'Q')) { 259774664626SKris Kennaway BIO_printf(bio_s_out, "DONE\n"); 2598e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out); 2599e71b7053SJung-uk Kim BIO_closesocket(s); 260074664626SKris Kennaway close_accept_socket(); 260174664626SKris Kennaway ret = -11; 260274664626SKris Kennaway goto err; 260374664626SKris Kennaway } 26046f9291ceSJung-uk Kim if ((i <= 0) || (buf[0] == 'q')) { 260574664626SKris Kennaway BIO_printf(bio_s_out, "DONE\n"); 2606e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out); 26073b4e3dcbSSimon L. B. Nielsen if (SSL_version(con) != DTLS1_VERSION) 2608e71b7053SJung-uk Kim BIO_closesocket(s); 26096f9291ceSJung-uk Kim /* 26106f9291ceSJung-uk Kim * close_accept_socket(); ret= -11; 26116f9291ceSJung-uk Kim */ 261274664626SKris Kennaway goto err; 261374664626SKris Kennaway } 26146f9291ceSJung-uk Kim if ((buf[0] == 'r') && ((buf[1] == '\n') || (buf[1] == '\r'))) { 261574664626SKris Kennaway SSL_renegotiate(con); 261674664626SKris Kennaway i = SSL_do_handshake(con); 261774664626SKris Kennaway printf("SSL_do_handshake -> %d\n", i); 261874664626SKris Kennaway i = 0; /* 13; */ 261974664626SKris Kennaway continue; 262074664626SKris Kennaway } 26216f9291ceSJung-uk Kim if ((buf[0] == 'R') && ((buf[1] == '\n') || (buf[1] == '\r'))) { 262274664626SKris Kennaway SSL_set_verify(con, 26236f9291ceSJung-uk Kim SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, 26246f9291ceSJung-uk Kim NULL); 262574664626SKris Kennaway SSL_renegotiate(con); 262674664626SKris Kennaway i = SSL_do_handshake(con); 262774664626SKris Kennaway printf("SSL_do_handshake -> %d\n", i); 262874664626SKris Kennaway i = 0; /* 13; */ 262974664626SKris Kennaway continue; 2630e71b7053SJung-uk Kim } 2631e71b7053SJung-uk Kim if ((buf[0] == 'K' || buf[0] == 'k') 2632e71b7053SJung-uk Kim && ((buf[1] == '\n') || (buf[1] == '\r'))) { 2633e71b7053SJung-uk Kim SSL_key_update(con, buf[0] == 'K' ? 2634e71b7053SJung-uk Kim SSL_KEY_UPDATE_REQUESTED 2635e71b7053SJung-uk Kim : SSL_KEY_UPDATE_NOT_REQUESTED); 2636e71b7053SJung-uk Kim i = SSL_do_handshake(con); 2637e71b7053SJung-uk Kim printf("SSL_do_handshake -> %d\n", i); 2638e71b7053SJung-uk Kim i = 0; 2639e71b7053SJung-uk Kim continue; 2640e71b7053SJung-uk Kim } 2641e71b7053SJung-uk Kim if (buf[0] == 'c' && ((buf[1] == '\n') || (buf[1] == '\r'))) { 2642e71b7053SJung-uk Kim SSL_set_verify(con, SSL_VERIFY_PEER, NULL); 2643e71b7053SJung-uk Kim i = SSL_verify_client_post_handshake(con); 2644e71b7053SJung-uk Kim if (i == 0) { 2645e71b7053SJung-uk Kim printf("Failed to initiate request\n"); 2646e71b7053SJung-uk Kim ERR_print_errors(bio_err); 2647e71b7053SJung-uk Kim } else { 2648e71b7053SJung-uk Kim i = SSL_do_handshake(con); 2649e71b7053SJung-uk Kim printf("SSL_do_handshake -> %d\n", i); 2650e71b7053SJung-uk Kim i = 0; 2651e71b7053SJung-uk Kim } 2652e71b7053SJung-uk Kim continue; 265374664626SKris Kennaway } 26546f9291ceSJung-uk Kim if (buf[0] == 'P') { 2655b077aed3SPierre Pronchery static const char str[] = "Lets print some clear text\n"; 2656b077aed3SPierre Pronchery BIO_write(SSL_get_wbio(con), str, sizeof(str) -1); 265774664626SKris Kennaway } 26586f9291ceSJung-uk Kim if (buf[0] == 'S') { 265974664626SKris Kennaway print_stats(bio_s_out, SSL_get_SSL_CTX(con)); 266074664626SKris Kennaway } 266174664626SKris Kennaway } 266274664626SKris Kennaway #ifdef CHARSET_EBCDIC 266374664626SKris Kennaway ebcdic2ascii(buf, buf, i); 266474664626SKris Kennaway #endif 266574664626SKris Kennaway l = k = 0; 26666f9291ceSJung-uk Kim for (;;) { 266774664626SKris Kennaway /* should do a select for the write */ 266874664626SKris Kennaway #ifdef RENEG 26696f9291ceSJung-uk Kim static count = 0; 26706f9291ceSJung-uk Kim if (++count == 100) { 26716f9291ceSJung-uk Kim count = 0; 26726f9291ceSJung-uk Kim SSL_renegotiate(con); 26736f9291ceSJung-uk Kim } 267474664626SKris Kennaway #endif 267574664626SKris Kennaway k = SSL_write(con, &(buf[l]), (unsigned int)i); 26761f13597dSJung-uk Kim #ifndef OPENSSL_NO_SRP 26776f9291ceSJung-uk Kim while (SSL_get_error(con, k) == SSL_ERROR_WANT_X509_LOOKUP) { 26781f13597dSJung-uk Kim BIO_printf(bio_s_out, "LOOKUP renego during write\n"); 2679b077aed3SPierre Pronchery 2680b077aed3SPierre Pronchery lookup_srp_user(&srp_callback_parm, bio_s_out); 2681b077aed3SPierre Pronchery 26821f13597dSJung-uk Kim k = SSL_write(con, &(buf[l]), (unsigned int)i); 26831f13597dSJung-uk Kim } 26841f13597dSJung-uk Kim #endif 26856f9291ceSJung-uk Kim switch (SSL_get_error(con, k)) { 268674664626SKris Kennaway case SSL_ERROR_NONE: 268774664626SKris Kennaway break; 2688e71b7053SJung-uk Kim case SSL_ERROR_WANT_ASYNC: 2689e71b7053SJung-uk Kim BIO_printf(bio_s_out, "Write BLOCK (Async)\n"); 2690e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out); 2691e71b7053SJung-uk Kim wait_for_async(con); 2692e71b7053SJung-uk Kim break; 269374664626SKris Kennaway case SSL_ERROR_WANT_WRITE: 269474664626SKris Kennaway case SSL_ERROR_WANT_READ: 269574664626SKris Kennaway case SSL_ERROR_WANT_X509_LOOKUP: 269674664626SKris Kennaway BIO_printf(bio_s_out, "Write BLOCK\n"); 2697e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out); 269874664626SKris Kennaway break; 2699e71b7053SJung-uk Kim case SSL_ERROR_WANT_ASYNC_JOB: 2700e71b7053SJung-uk Kim /* 2701e71b7053SJung-uk Kim * This shouldn't ever happen in s_server. Treat as an error 2702e71b7053SJung-uk Kim */ 270374664626SKris Kennaway case SSL_ERROR_SYSCALL: 270474664626SKris Kennaway case SSL_ERROR_SSL: 270574664626SKris Kennaway BIO_printf(bio_s_out, "ERROR\n"); 2706e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out); 270774664626SKris Kennaway ERR_print_errors(bio_err); 270874664626SKris Kennaway ret = 1; 270974664626SKris Kennaway goto err; 271074664626SKris Kennaway /* break; */ 271174664626SKris Kennaway case SSL_ERROR_ZERO_RETURN: 271274664626SKris Kennaway BIO_printf(bio_s_out, "DONE\n"); 2713e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out); 271474664626SKris Kennaway ret = 1; 271574664626SKris Kennaway goto err; 271674664626SKris Kennaway } 2717ed6b93beSJung-uk Kim if (k > 0) { 271874664626SKris Kennaway l += k; 271974664626SKris Kennaway i -= k; 2720ed6b93beSJung-uk Kim } 27216f9291ceSJung-uk Kim if (i <= 0) 27226f9291ceSJung-uk Kim break; 272374664626SKris Kennaway } 272474664626SKris Kennaway } 27256f9291ceSJung-uk Kim if (read_from_sslcon) { 2726e71b7053SJung-uk Kim /* 2727e71b7053SJung-uk Kim * init_ssl_connection handles all async events itself so if we're 2728e71b7053SJung-uk Kim * waiting for async then we shouldn't go back into 2729e71b7053SJung-uk Kim * init_ssl_connection 2730e71b7053SJung-uk Kim */ 2731e71b7053SJung-uk Kim if ((!async || !SSL_waiting_for_async(con)) 2732e71b7053SJung-uk Kim && !SSL_is_init_finished(con)) { 273383eaf7aeSJung-uk Kim /* 273483eaf7aeSJung-uk Kim * Count number of reads during init_ssl_connection. 273583eaf7aeSJung-uk Kim * It helps us to distinguish configuration errors from errors 273683eaf7aeSJung-uk Kim * caused by a client. 273783eaf7aeSJung-uk Kim */ 273883eaf7aeSJung-uk Kim unsigned int read_counter = 0; 273983eaf7aeSJung-uk Kim 274083eaf7aeSJung-uk Kim BIO_set_callback_arg(SSL_get_rbio(con), (char *)&read_counter); 274174664626SKris Kennaway i = init_ssl_connection(con); 274283eaf7aeSJung-uk Kim BIO_set_callback_arg(SSL_get_rbio(con), NULL); 274383eaf7aeSJung-uk Kim 274483eaf7aeSJung-uk Kim /* 274583eaf7aeSJung-uk Kim * If initialization fails without reads, then 274683eaf7aeSJung-uk Kim * there was a fatal error in configuration. 274783eaf7aeSJung-uk Kim */ 274883eaf7aeSJung-uk Kim if (i <= 0 && read_counter == 0) { 274983eaf7aeSJung-uk Kim ret = -1; 275083eaf7aeSJung-uk Kim goto err; 275183eaf7aeSJung-uk Kim } 27526f9291ceSJung-uk Kim if (i < 0) { 275374664626SKris Kennaway ret = 0; 275474664626SKris Kennaway goto err; 27556f9291ceSJung-uk Kim } else if (i == 0) { 275674664626SKris Kennaway ret = 1; 275774664626SKris Kennaway goto err; 275874664626SKris Kennaway } 27596f9291ceSJung-uk Kim } else { 276074664626SKris Kennaway again: 276174664626SKris Kennaway i = SSL_read(con, (char *)buf, bufsize); 27621f13597dSJung-uk Kim #ifndef OPENSSL_NO_SRP 27636f9291ceSJung-uk Kim while (SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) { 27641f13597dSJung-uk Kim BIO_printf(bio_s_out, "LOOKUP renego during read\n"); 2765b077aed3SPierre Pronchery 2766b077aed3SPierre Pronchery lookup_srp_user(&srp_callback_parm, bio_s_out); 2767b077aed3SPierre Pronchery 27681f13597dSJung-uk Kim i = SSL_read(con, (char *)buf, bufsize); 27691f13597dSJung-uk Kim } 27701f13597dSJung-uk Kim #endif 27716f9291ceSJung-uk Kim switch (SSL_get_error(con, i)) { 277274664626SKris Kennaway case SSL_ERROR_NONE: 277374664626SKris Kennaway #ifdef CHARSET_EBCDIC 277474664626SKris Kennaway ascii2ebcdic(buf, buf, i); 277574664626SKris Kennaway #endif 27766f9291ceSJung-uk Kim raw_write_stdout(buf, (unsigned int)i); 2777e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out); 2778e71b7053SJung-uk Kim if (SSL_has_pending(con)) 27796f9291ceSJung-uk Kim goto again; 278074664626SKris Kennaway break; 2781e71b7053SJung-uk Kim case SSL_ERROR_WANT_ASYNC: 2782e71b7053SJung-uk Kim BIO_printf(bio_s_out, "Read BLOCK (Async)\n"); 2783e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out); 2784e71b7053SJung-uk Kim wait_for_async(con); 2785e71b7053SJung-uk Kim break; 278674664626SKris Kennaway case SSL_ERROR_WANT_WRITE: 278774664626SKris Kennaway case SSL_ERROR_WANT_READ: 278874664626SKris Kennaway BIO_printf(bio_s_out, "Read BLOCK\n"); 2789e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out); 279074664626SKris Kennaway break; 2791e71b7053SJung-uk Kim case SSL_ERROR_WANT_ASYNC_JOB: 2792e71b7053SJung-uk Kim /* 2793e71b7053SJung-uk Kim * This shouldn't ever happen in s_server. Treat as an error 2794e71b7053SJung-uk Kim */ 279574664626SKris Kennaway case SSL_ERROR_SYSCALL: 279674664626SKris Kennaway case SSL_ERROR_SSL: 279774664626SKris Kennaway BIO_printf(bio_s_out, "ERROR\n"); 2798e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out); 279974664626SKris Kennaway ERR_print_errors(bio_err); 280074664626SKris Kennaway ret = 1; 280174664626SKris Kennaway goto err; 280274664626SKris Kennaway case SSL_ERROR_ZERO_RETURN: 280374664626SKris Kennaway BIO_printf(bio_s_out, "DONE\n"); 2804e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out); 280574664626SKris Kennaway ret = 1; 280674664626SKris Kennaway goto err; 280774664626SKris Kennaway } 280874664626SKris Kennaway } 280974664626SKris Kennaway } 281074664626SKris Kennaway } 281174664626SKris Kennaway err: 28126f9291ceSJung-uk Kim if (con != NULL) { 281374664626SKris Kennaway BIO_printf(bio_s_out, "shutting down SSL\n"); 2814b077aed3SPierre Pronchery do_ssl_shutdown(con); 28151f13597dSJung-uk Kim SSL_free(con); 28161f13597dSJung-uk Kim } 281774664626SKris Kennaway BIO_printf(bio_s_out, "CONNECTION CLOSED\n"); 2818e71b7053SJung-uk Kim OPENSSL_clear_free(buf, bufsize); 2819e71b7053SJung-uk Kim return ret; 282074664626SKris Kennaway } 282174664626SKris Kennaway 282274664626SKris Kennaway static void close_accept_socket(void) 282374664626SKris Kennaway { 282474664626SKris Kennaway BIO_printf(bio_err, "shutdown accept socket\n"); 28256f9291ceSJung-uk Kim if (accept_socket >= 0) { 2826e71b7053SJung-uk Kim BIO_closesocket(accept_socket); 282774664626SKris Kennaway } 282874664626SKris Kennaway } 282974664626SKris Kennaway 2830e71b7053SJung-uk Kim static int is_retryable(SSL *con, int i) 2831e71b7053SJung-uk Kim { 2832e71b7053SJung-uk Kim int err = SSL_get_error(con, i); 2833e71b7053SJung-uk Kim 2834e71b7053SJung-uk Kim /* If it's not a fatal error, it must be retryable */ 2835e71b7053SJung-uk Kim return (err != SSL_ERROR_SSL) 2836e71b7053SJung-uk Kim && (err != SSL_ERROR_SYSCALL) 2837e71b7053SJung-uk Kim && (err != SSL_ERROR_ZERO_RETURN); 2838e71b7053SJung-uk Kim } 2839e71b7053SJung-uk Kim 284074664626SKris Kennaway static int init_ssl_connection(SSL *con) 284174664626SKris Kennaway { 284274664626SKris Kennaway int i; 2843e71b7053SJung-uk Kim long verify_err; 2844e71b7053SJung-uk Kim int retry = 0; 284574664626SKris Kennaway 2846e71b7053SJung-uk Kim if (dtlslisten || stateless) { 2847e71b7053SJung-uk Kim BIO_ADDR *client = NULL; 2848e71b7053SJung-uk Kim 2849e71b7053SJung-uk Kim if (dtlslisten) { 2850e71b7053SJung-uk Kim if ((client = BIO_ADDR_new()) == NULL) { 2851e71b7053SJung-uk Kim BIO_printf(bio_err, "ERROR - memory\n"); 2852e71b7053SJung-uk Kim return 0; 2853e71b7053SJung-uk Kim } 2854e71b7053SJung-uk Kim i = DTLSv1_listen(con, client); 2855e71b7053SJung-uk Kim } else { 2856e71b7053SJung-uk Kim i = SSL_stateless(con); 2857e71b7053SJung-uk Kim } 2858e71b7053SJung-uk Kim if (i > 0) { 2859e71b7053SJung-uk Kim BIO *wbio; 2860e71b7053SJung-uk Kim int fd = -1; 2861e71b7053SJung-uk Kim 2862e71b7053SJung-uk Kim if (dtlslisten) { 2863e71b7053SJung-uk Kim wbio = SSL_get_wbio(con); 2864e71b7053SJung-uk Kim if (wbio) { 2865e71b7053SJung-uk Kim BIO_get_fd(wbio, &fd); 2866e71b7053SJung-uk Kim } 2867e71b7053SJung-uk Kim 2868e71b7053SJung-uk Kim if (!wbio || BIO_connect(fd, client, 0) == 0) { 2869e71b7053SJung-uk Kim BIO_printf(bio_err, "ERROR - unable to connect\n"); 2870e71b7053SJung-uk Kim BIO_ADDR_free(client); 2871e71b7053SJung-uk Kim return 0; 2872e71b7053SJung-uk Kim } 28736935a639SJung-uk Kim 28746935a639SJung-uk Kim (void)BIO_ctrl_set_connected(wbio, client); 2875e71b7053SJung-uk Kim BIO_ADDR_free(client); 2876e71b7053SJung-uk Kim dtlslisten = 0; 2877e71b7053SJung-uk Kim } else { 2878e71b7053SJung-uk Kim stateless = 0; 2879e71b7053SJung-uk Kim } 28801f13597dSJung-uk Kim i = SSL_accept(con); 2881e71b7053SJung-uk Kim } else { 2882e71b7053SJung-uk Kim BIO_ADDR_free(client); 2883e71b7053SJung-uk Kim } 2884e71b7053SJung-uk Kim } else { 2885e71b7053SJung-uk Kim do { 2886e71b7053SJung-uk Kim i = SSL_accept(con); 2887e71b7053SJung-uk Kim 2888e71b7053SJung-uk Kim if (i <= 0) 2889e71b7053SJung-uk Kim retry = is_retryable(con, i); 28907bded2dbSJung-uk Kim #ifdef CERT_CB_TEST_RETRY 28917bded2dbSJung-uk Kim { 2892e71b7053SJung-uk Kim while (i <= 0 2893e71b7053SJung-uk Kim && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP 2894e71b7053SJung-uk Kim && SSL_get_state(con) == TLS_ST_SR_CLNT_HELLO) { 2895e71b7053SJung-uk Kim BIO_printf(bio_err, 28967bded2dbSJung-uk Kim "LOOKUP from certificate callback during accept\n"); 28977bded2dbSJung-uk Kim i = SSL_accept(con); 2898e71b7053SJung-uk Kim if (i <= 0) 2899e71b7053SJung-uk Kim retry = is_retryable(con, i); 29007bded2dbSJung-uk Kim } 29017bded2dbSJung-uk Kim } 29027bded2dbSJung-uk Kim #endif 2903e71b7053SJung-uk Kim 29041f13597dSJung-uk Kim #ifndef OPENSSL_NO_SRP 2905e71b7053SJung-uk Kim while (i <= 0 2906e71b7053SJung-uk Kim && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) { 29076f9291ceSJung-uk Kim BIO_printf(bio_s_out, "LOOKUP during accept %s\n", 29086f9291ceSJung-uk Kim srp_callback_parm.login); 2909b077aed3SPierre Pronchery 2910b077aed3SPierre Pronchery lookup_srp_user(&srp_callback_parm, bio_s_out); 2911b077aed3SPierre Pronchery 29121f13597dSJung-uk Kim i = SSL_accept(con); 2913e71b7053SJung-uk Kim if (i <= 0) 2914e71b7053SJung-uk Kim retry = is_retryable(con, i); 29151f13597dSJung-uk Kim } 29161f13597dSJung-uk Kim #endif 2917e71b7053SJung-uk Kim } while (i < 0 && SSL_waiting_for_async(con)); 2918e71b7053SJung-uk Kim } 29197bded2dbSJung-uk Kim 29206f9291ceSJung-uk Kim if (i <= 0) { 2921e71b7053SJung-uk Kim if (((dtlslisten || stateless) && i == 0) 2922e71b7053SJung-uk Kim || (!dtlslisten && !stateless && retry)) { 292374664626SKris Kennaway BIO_printf(bio_s_out, "DELAY\n"); 2924e71b7053SJung-uk Kim return 1; 292574664626SKris Kennaway } 292674664626SKris Kennaway 292774664626SKris Kennaway BIO_printf(bio_err, "ERROR\n"); 2928e71b7053SJung-uk Kim 2929e71b7053SJung-uk Kim verify_err = SSL_get_verify_result(con); 2930e71b7053SJung-uk Kim if (verify_err != X509_V_OK) { 293174664626SKris Kennaway BIO_printf(bio_err, "verify error:%s\n", 2932e71b7053SJung-uk Kim X509_verify_cert_error_string(verify_err)); 29337bded2dbSJung-uk Kim } 29347bded2dbSJung-uk Kim /* Always print any error messages */ 293574664626SKris Kennaway ERR_print_errors(bio_err); 2936e71b7053SJung-uk Kim return 0; 293774664626SKris Kennaway } 293874664626SKris Kennaway 2939e71b7053SJung-uk Kim print_connection_info(con); 2940e71b7053SJung-uk Kim return 1; 2941e71b7053SJung-uk Kim } 2942e71b7053SJung-uk Kim 2943e71b7053SJung-uk Kim static void print_connection_info(SSL *con) 2944e71b7053SJung-uk Kim { 2945e71b7053SJung-uk Kim const char *str; 2946e71b7053SJung-uk Kim X509 *peer; 2947e71b7053SJung-uk Kim char buf[BUFSIZ]; 2948e71b7053SJung-uk Kim #if !defined(OPENSSL_NO_NEXTPROTONEG) 2949e71b7053SJung-uk Kim const unsigned char *next_proto_neg; 2950e71b7053SJung-uk Kim unsigned next_proto_neg_len; 2951e71b7053SJung-uk Kim #endif 2952e71b7053SJung-uk Kim unsigned char *exportedkeymat; 2953e71b7053SJung-uk Kim int i; 2954e71b7053SJung-uk Kim 29557bded2dbSJung-uk Kim if (s_brief) 2956e71b7053SJung-uk Kim print_ssl_summary(con); 29577bded2dbSJung-uk Kim 295874664626SKris Kennaway PEM_write_bio_SSL_SESSION(bio_s_out, SSL_get_session(con)); 295974664626SKris Kennaway 2960b077aed3SPierre Pronchery peer = SSL_get0_peer_certificate(con); 29616f9291ceSJung-uk Kim if (peer != NULL) { 296274664626SKris Kennaway BIO_printf(bio_s_out, "Client certificate\n"); 296374664626SKris Kennaway PEM_write_bio_X509(bio_s_out, peer); 2964e71b7053SJung-uk Kim dump_cert_text(bio_s_out, peer); 2965e71b7053SJung-uk Kim peer = NULL; 296674664626SKris Kennaway } 296774664626SKris Kennaway 2968dee36b4fSJung-uk Kim if (SSL_get_shared_ciphers(con, buf, sizeof(buf)) != NULL) 296974664626SKris Kennaway BIO_printf(bio_s_out, "Shared ciphers:%s\n", buf); 297074664626SKris Kennaway str = SSL_CIPHER_get_name(SSL_get_current_cipher(con)); 29717bded2dbSJung-uk Kim ssl_print_sigalgs(bio_s_out, con); 29727bded2dbSJung-uk Kim #ifndef OPENSSL_NO_EC 29737bded2dbSJung-uk Kim ssl_print_point_formats(bio_s_out, con); 2974e71b7053SJung-uk Kim ssl_print_groups(bio_s_out, con, 0); 29757bded2dbSJung-uk Kim #endif 2976e71b7053SJung-uk Kim print_ca_names(bio_s_out, con); 297774664626SKris Kennaway BIO_printf(bio_s_out, "CIPHER is %s\n", (str != NULL) ? str : "(NONE)"); 297809286989SJung-uk Kim 2979e71b7053SJung-uk Kim #if !defined(OPENSSL_NO_NEXTPROTONEG) 29801f13597dSJung-uk Kim SSL_get0_next_proto_negotiated(con, &next_proto_neg, &next_proto_neg_len); 29816f9291ceSJung-uk Kim if (next_proto_neg) { 29821f13597dSJung-uk Kim BIO_printf(bio_s_out, "NEXTPROTO is "); 29831f13597dSJung-uk Kim BIO_write(bio_s_out, next_proto_neg, next_proto_neg_len); 29841f13597dSJung-uk Kim BIO_printf(bio_s_out, "\n"); 29851f13597dSJung-uk Kim } 29861f13597dSJung-uk Kim #endif 298709286989SJung-uk Kim #ifndef OPENSSL_NO_SRTP 29881f13597dSJung-uk Kim { 29891f13597dSJung-uk Kim SRTP_PROTECTION_PROFILE *srtp_profile 29901f13597dSJung-uk Kim = SSL_get_selected_srtp_profile(con); 29911f13597dSJung-uk Kim 29921f13597dSJung-uk Kim if (srtp_profile) 29931f13597dSJung-uk Kim BIO_printf(bio_s_out, "SRTP Extension negotiated, profile=%s\n", 29941f13597dSJung-uk Kim srtp_profile->name); 29951f13597dSJung-uk Kim } 299609286989SJung-uk Kim #endif 2997e71b7053SJung-uk Kim if (SSL_session_reused(con)) 29986f9291ceSJung-uk Kim BIO_printf(bio_s_out, "Reused session-id\n"); 29996a599222SSimon L. B. Nielsen BIO_printf(bio_s_out, "Secure Renegotiation IS%s supported\n", 30006a599222SSimon L. B. Nielsen SSL_get_secure_renegotiation_support(con) ? "" : " NOT"); 3001e71b7053SJung-uk Kim if ((SSL_get_options(con) & SSL_OP_NO_RENEGOTIATION)) 3002e71b7053SJung-uk Kim BIO_printf(bio_s_out, "Renegotiation is DISABLED\n"); 3003e71b7053SJung-uk Kim 30046f9291ceSJung-uk Kim if (keymatexportlabel != NULL) { 30051f13597dSJung-uk Kim BIO_printf(bio_s_out, "Keying material exporter:\n"); 30061f13597dSJung-uk Kim BIO_printf(bio_s_out, " Label: '%s'\n", keymatexportlabel); 30076f9291ceSJung-uk Kim BIO_printf(bio_s_out, " Length: %i bytes\n", keymatexportlen); 3008e71b7053SJung-uk Kim exportedkeymat = app_malloc(keymatexportlen, "export key"); 3009b077aed3SPierre Pronchery if (SSL_export_keying_material(con, exportedkeymat, 30101f13597dSJung-uk Kim keymatexportlen, 30111f13597dSJung-uk Kim keymatexportlabel, 30121f13597dSJung-uk Kim strlen(keymatexportlabel), 3013b077aed3SPierre Pronchery NULL, 0, 0) <= 0) { 30141f13597dSJung-uk Kim BIO_printf(bio_s_out, " Error\n"); 30156f9291ceSJung-uk Kim } else { 30161f13597dSJung-uk Kim BIO_printf(bio_s_out, " Keying material: "); 30171f13597dSJung-uk Kim for (i = 0; i < keymatexportlen; i++) 30186f9291ceSJung-uk Kim BIO_printf(bio_s_out, "%02X", exportedkeymat[i]); 30191f13597dSJung-uk Kim BIO_printf(bio_s_out, "\n"); 30201f13597dSJung-uk Kim } 30211f13597dSJung-uk Kim OPENSSL_free(exportedkeymat); 30221f13597dSJung-uk Kim } 3023aa906e2aSJohn Baldwin #ifndef OPENSSL_NO_KTLS 3024aa906e2aSJohn Baldwin if (BIO_get_ktls_send(SSL_get_wbio(con))) 3025aa906e2aSJohn Baldwin BIO_printf(bio_err, "Using Kernel TLS for sending\n"); 3026aa906e2aSJohn Baldwin if (BIO_get_ktls_recv(SSL_get_rbio(con))) 3027aa906e2aSJohn Baldwin BIO_printf(bio_err, "Using Kernel TLS for receiving\n"); 3028aa906e2aSJohn Baldwin #endif 30291f13597dSJung-uk Kim 3030e71b7053SJung-uk Kim (void)BIO_flush(bio_s_out); 303174664626SKris Kennaway } 303274664626SKris Kennaway 3033e71b7053SJung-uk Kim static int www_body(int s, int stype, int prot, unsigned char *context) 303474664626SKris Kennaway { 303574664626SKris Kennaway char *buf = NULL; 303674664626SKris Kennaway int ret = 1; 3037a3ddd25aSSimon L. B. Nielsen int i, j, k, dot; 303874664626SKris Kennaway SSL *con; 30391f13597dSJung-uk Kim const SSL_CIPHER *c; 304074664626SKris Kennaway BIO *io, *ssl_bio, *sbio; 3041e71b7053SJung-uk Kim #ifdef RENEG 3042e71b7053SJung-uk Kim int total_bytes = 0; 3043a3ddd25aSSimon L. B. Nielsen #endif 3044e71b7053SJung-uk Kim int width; 3045b077aed3SPierre Pronchery #ifndef OPENSSL_NO_KTLS 3046b077aed3SPierre Pronchery int use_sendfile_for_req = use_sendfile; 3047b077aed3SPierre Pronchery #endif 3048e71b7053SJung-uk Kim fd_set readfds; 3049b077aed3SPierre Pronchery const char *opmode; 3050b077aed3SPierre Pronchery #ifdef CHARSET_EBCDIC 3051b077aed3SPierre Pronchery BIO *filter; 3052b077aed3SPierre Pronchery #endif 305374664626SKris Kennaway 3054e71b7053SJung-uk Kim /* Set width for a select call if needed */ 3055e71b7053SJung-uk Kim width = s + 1; 3056e71b7053SJung-uk Kim 3057b077aed3SPierre Pronchery /* as we use BIO_gets(), and it always null terminates data, we need 3058b077aed3SPierre Pronchery * to allocate 1 byte longer buffer to fit the full 2^14 byte record */ 3059b077aed3SPierre Pronchery buf = app_malloc(bufsize + 1, "server www buffer"); 306074664626SKris Kennaway io = BIO_new(BIO_f_buffer()); 306174664626SKris Kennaway ssl_bio = BIO_new(BIO_f_ssl()); 30626f9291ceSJung-uk Kim if ((io == NULL) || (ssl_bio == NULL)) 30636f9291ceSJung-uk Kim goto err; 306474664626SKris Kennaway 30656f9291ceSJung-uk Kim if (s_nbio) { 3066e71b7053SJung-uk Kim if (!BIO_socket_nbio(s, 1)) 306774664626SKris Kennaway ERR_print_errors(bio_err); 3068e71b7053SJung-uk Kim else if (!s_quiet) 3069e71b7053SJung-uk Kim BIO_printf(bio_err, "Turned on non blocking io\n"); 307074664626SKris Kennaway } 307174664626SKris Kennaway 307274664626SKris Kennaway /* lets make the output buffer a reasonable size */ 3073b077aed3SPierre Pronchery if (BIO_set_write_buffer_size(io, bufsize) <= 0) 30746f9291ceSJung-uk Kim goto err; 307574664626SKris Kennaway 30766f9291ceSJung-uk Kim if ((con = SSL_new(ctx)) == NULL) 30776f9291ceSJung-uk Kim goto err; 3078e71b7053SJung-uk Kim 30796f9291ceSJung-uk Kim if (s_tlsextdebug) { 3080db522d3aSSimon L. B. Nielsen SSL_set_tlsext_debug_callback(con, tlsext_cb); 3081db522d3aSSimon L. B. Nielsen SSL_set_tlsext_debug_arg(con, bio_s_out); 3082db522d3aSSimon L. B. Nielsen } 3083e71b7053SJung-uk Kim 3084e71b7053SJung-uk Kim if (context != NULL 3085e71b7053SJung-uk Kim && !SSL_set_session_id_context(con, context, 3086e71b7053SJung-uk Kim strlen((char *)context))) { 3087e71b7053SJung-uk Kim SSL_free(con); 3088e71b7053SJung-uk Kim goto err; 30895c87c606SMark Murray } 309074664626SKris Kennaway 309174664626SKris Kennaway sbio = BIO_new_socket(s, BIO_NOCLOSE); 3092b077aed3SPierre Pronchery if (sbio == NULL) { 3093b077aed3SPierre Pronchery SSL_free(con); 3094b077aed3SPierre Pronchery goto err; 3095b077aed3SPierre Pronchery } 3096b077aed3SPierre Pronchery 30976f9291ceSJung-uk Kim if (s_nbio_test) { 309874664626SKris Kennaway BIO *test; 309974664626SKris Kennaway 310074664626SKris Kennaway test = BIO_new(BIO_f_nbio_test()); 3101b077aed3SPierre Pronchery if (test == NULL) { 3102b077aed3SPierre Pronchery SSL_free(con); 3103b077aed3SPierre Pronchery BIO_free(sbio); 3104b077aed3SPierre Pronchery goto err; 3105b077aed3SPierre Pronchery } 3106b077aed3SPierre Pronchery 310774664626SKris Kennaway sbio = BIO_push(test, sbio); 310874664626SKris Kennaway } 310974664626SKris Kennaway SSL_set_bio(con, sbio, sbio); 311074664626SKris Kennaway SSL_set_accept_state(con); 311174664626SKris Kennaway 3112e71b7053SJung-uk Kim /* No need to free |con| after this. Done by BIO_free(ssl_bio) */ 311374664626SKris Kennaway BIO_set_ssl(ssl_bio, con, BIO_CLOSE); 311474664626SKris Kennaway BIO_push(io, ssl_bio); 3115b077aed3SPierre Pronchery ssl_bio = NULL; 311674664626SKris Kennaway #ifdef CHARSET_EBCDIC 3117b077aed3SPierre Pronchery filter = BIO_new(BIO_f_ebcdic_filter()); 3118b077aed3SPierre Pronchery if (filter == NULL) 3119b077aed3SPierre Pronchery goto err; 3120b077aed3SPierre Pronchery 3121b077aed3SPierre Pronchery io = BIO_push(filter, io); 312274664626SKris Kennaway #endif 312374664626SKris Kennaway 31246f9291ceSJung-uk Kim if (s_debug) { 3125b077aed3SPierre Pronchery BIO_set_callback_ex(SSL_get_rbio(con), bio_dump_callback); 31265471f83eSSimon L. B. Nielsen BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out); 312774664626SKris Kennaway } 31286f9291ceSJung-uk Kim if (s_msg) { 31297bded2dbSJung-uk Kim #ifndef OPENSSL_NO_SSL_TRACE 31307bded2dbSJung-uk Kim if (s_msg == 2) 31317bded2dbSJung-uk Kim SSL_set_msg_callback(con, SSL_trace); 31327bded2dbSJung-uk Kim else 31337bded2dbSJung-uk Kim #endif 31345c87c606SMark Murray SSL_set_msg_callback(con, msg_cb); 31357bded2dbSJung-uk Kim SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out); 31365c87c606SMark Murray } 313774664626SKris Kennaway 31386f9291ceSJung-uk Kim for (;;) { 3139b077aed3SPierre Pronchery i = BIO_gets(io, buf, bufsize + 1); 31406f9291ceSJung-uk Kim if (i < 0) { /* error */ 3141e71b7053SJung-uk Kim if (!BIO_should_retry(io) && !SSL_waiting_for_async(con)) { 314274664626SKris Kennaway if (!s_quiet) 314374664626SKris Kennaway ERR_print_errors(bio_err); 314474664626SKris Kennaway goto err; 31456f9291ceSJung-uk Kim } else { 314674664626SKris Kennaway BIO_printf(bio_s_out, "read R BLOCK\n"); 314780815a77SJung-uk Kim #ifndef OPENSSL_NO_SRP 314880815a77SJung-uk Kim if (BIO_should_io_special(io) 314980815a77SJung-uk Kim && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) { 315080815a77SJung-uk Kim BIO_printf(bio_s_out, "LOOKUP renego during read\n"); 3151b077aed3SPierre Pronchery 3152b077aed3SPierre Pronchery lookup_srp_user(&srp_callback_parm, bio_s_out); 3153b077aed3SPierre Pronchery 315480815a77SJung-uk Kim continue; 315580815a77SJung-uk Kim } 315680815a77SJung-uk Kim #endif 3157b077aed3SPierre Pronchery ossl_sleep(1000); 315874664626SKris Kennaway continue; 315974664626SKris Kennaway } 31606f9291ceSJung-uk Kim } else if (i == 0) { /* end of input */ 316174664626SKris Kennaway ret = 1; 316274664626SKris Kennaway goto end; 316374664626SKris Kennaway } 316474664626SKris Kennaway 316574664626SKris Kennaway /* else we have data */ 316674664626SKris Kennaway if (((www == 1) && (strncmp("GET ", buf, 4) == 0)) || 31676f9291ceSJung-uk Kim ((www == 2) && (strncmp("GET /stats ", buf, 11) == 0))) { 316874664626SKris Kennaway char *p; 3169e71b7053SJung-uk Kim X509 *peer = NULL; 317074664626SKris Kennaway STACK_OF(SSL_CIPHER) *sk; 31713b4e3dcbSSimon L. B. Nielsen static const char *space = " "; 317274664626SKris Kennaway 3173e71b7053SJung-uk Kim if (www == 1 && strncmp("GET /reneg", buf, 10) == 0) { 3174e71b7053SJung-uk Kim if (strncmp("GET /renegcert", buf, 14) == 0) 3175e71b7053SJung-uk Kim SSL_set_verify(con, 3176e71b7053SJung-uk Kim SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, 3177e71b7053SJung-uk Kim NULL); 3178e71b7053SJung-uk Kim i = SSL_renegotiate(con); 3179e71b7053SJung-uk Kim BIO_printf(bio_s_out, "SSL_renegotiate -> %d\n", i); 3180e71b7053SJung-uk Kim /* Send the HelloRequest */ 3181e71b7053SJung-uk Kim i = SSL_do_handshake(con); 3182e71b7053SJung-uk Kim if (i <= 0) { 3183e71b7053SJung-uk Kim BIO_printf(bio_s_out, "SSL_do_handshake() Retval %d\n", 3184e71b7053SJung-uk Kim SSL_get_error(con, i)); 3185e71b7053SJung-uk Kim ERR_print_errors(bio_err); 3186e71b7053SJung-uk Kim goto err; 3187e71b7053SJung-uk Kim } 3188e71b7053SJung-uk Kim /* Wait for a ClientHello to come back */ 3189e71b7053SJung-uk Kim FD_ZERO(&readfds); 3190e71b7053SJung-uk Kim openssl_fdset(s, &readfds); 3191e71b7053SJung-uk Kim i = select(width, (void *)&readfds, NULL, NULL, NULL); 3192e71b7053SJung-uk Kim if (i <= 0 || !FD_ISSET(s, &readfds)) { 3193e71b7053SJung-uk Kim BIO_printf(bio_s_out, 3194e71b7053SJung-uk Kim "Error waiting for client response\n"); 3195e71b7053SJung-uk Kim ERR_print_errors(bio_err); 3196e71b7053SJung-uk Kim goto err; 3197e71b7053SJung-uk Kim } 3198e71b7053SJung-uk Kim /* 3199e71b7053SJung-uk Kim * We're not actually expecting any data here and we ignore 3200e71b7053SJung-uk Kim * any that is sent. This is just to force the handshake that 3201e71b7053SJung-uk Kim * we're expecting to come from the client. If they haven't 3202e71b7053SJung-uk Kim * sent one there's not much we can do. 3203e71b7053SJung-uk Kim */ 3204b077aed3SPierre Pronchery BIO_gets(io, buf, bufsize + 1); 3205e71b7053SJung-uk Kim } 3206e71b7053SJung-uk Kim 32076f9291ceSJung-uk Kim BIO_puts(io, 32086f9291ceSJung-uk Kim "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n"); 320974664626SKris Kennaway BIO_puts(io, "<HTML><BODY BGCOLOR=\"#ffffff\">\n"); 321074664626SKris Kennaway BIO_puts(io, "<pre>\n"); 3211e71b7053SJung-uk Kim /* BIO_puts(io, OpenSSL_version(OPENSSL_VERSION)); */ 321274664626SKris Kennaway BIO_puts(io, "\n"); 32136f9291ceSJung-uk Kim for (i = 0; i < local_argc; i++) { 3214e71b7053SJung-uk Kim const char *myp; 3215e71b7053SJung-uk Kim for (myp = local_argv[i]; *myp; myp++) 3216e71b7053SJung-uk Kim switch (*myp) { 3217e71b7053SJung-uk Kim case '<': 3218e71b7053SJung-uk Kim BIO_puts(io, "<"); 3219e71b7053SJung-uk Kim break; 3220e71b7053SJung-uk Kim case '>': 3221e71b7053SJung-uk Kim BIO_puts(io, ">"); 3222e71b7053SJung-uk Kim break; 3223e71b7053SJung-uk Kim case '&': 3224e71b7053SJung-uk Kim BIO_puts(io, "&"); 3225e71b7053SJung-uk Kim break; 3226e71b7053SJung-uk Kim default: 3227e71b7053SJung-uk Kim BIO_write(io, myp, 1); 3228e71b7053SJung-uk Kim break; 3229e71b7053SJung-uk Kim } 323074664626SKris Kennaway BIO_write(io, " ", 1); 323174664626SKris Kennaway } 323274664626SKris Kennaway BIO_puts(io, "\n"); 323374664626SKris Kennaway 323409286989SJung-uk Kim BIO_printf(io, 323509286989SJung-uk Kim "Secure Renegotiation IS%s supported\n", 323609286989SJung-uk Kim SSL_get_secure_renegotiation_support(con) ? 323709286989SJung-uk Kim "" : " NOT"); 323809286989SJung-uk Kim 32396f9291ceSJung-uk Kim /* 32406f9291ceSJung-uk Kim * The following is evil and should not really be done 32416f9291ceSJung-uk Kim */ 324274664626SKris Kennaway BIO_printf(io, "Ciphers supported in s_server binary\n"); 324374664626SKris Kennaway sk = SSL_get_ciphers(con); 324474664626SKris Kennaway j = sk_SSL_CIPHER_num(sk); 32456f9291ceSJung-uk Kim for (i = 0; i < j; i++) { 324674664626SKris Kennaway c = sk_SSL_CIPHER_value(sk, i); 324774664626SKris Kennaway BIO_printf(io, "%-11s:%-25s ", 32486f9291ceSJung-uk Kim SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c)); 324974664626SKris Kennaway if ((((i + 1) % 2) == 0) && (i + 1 != j)) 325074664626SKris Kennaway BIO_puts(io, "\n"); 325174664626SKris Kennaway } 325274664626SKris Kennaway BIO_puts(io, "\n"); 325374664626SKris Kennaway p = SSL_get_shared_ciphers(con, buf, bufsize); 32546f9291ceSJung-uk Kim if (p != NULL) { 32556f9291ceSJung-uk Kim BIO_printf(io, 32566f9291ceSJung-uk Kim "---\nCiphers common between both SSL end points:\n"); 325774664626SKris Kennaway j = i = 0; 32586f9291ceSJung-uk Kim while (*p) { 32596f9291ceSJung-uk Kim if (*p == ':') { 326074664626SKris Kennaway BIO_write(io, space, 26 - j); 326174664626SKris Kennaway i++; 326274664626SKris Kennaway j = 0; 326374664626SKris Kennaway BIO_write(io, ((i % 3) ? " " : "\n"), 1); 32646f9291ceSJung-uk Kim } else { 326574664626SKris Kennaway BIO_write(io, p, 1); 326674664626SKris Kennaway j++; 326774664626SKris Kennaway } 326874664626SKris Kennaway p++; 326974664626SKris Kennaway } 327074664626SKris Kennaway BIO_puts(io, "\n"); 327174664626SKris Kennaway } 32727bded2dbSJung-uk Kim ssl_print_sigalgs(io, con); 32737bded2dbSJung-uk Kim #ifndef OPENSSL_NO_EC 3274e71b7053SJung-uk Kim ssl_print_groups(io, con, 0); 32757bded2dbSJung-uk Kim #endif 3276e71b7053SJung-uk Kim print_ca_names(io, con); 3277e71b7053SJung-uk Kim BIO_printf(io, (SSL_session_reused(con) 32786f9291ceSJung-uk Kim ? "---\nReused, " : "---\nNew, ")); 327974664626SKris Kennaway c = SSL_get_current_cipher(con); 328074664626SKris Kennaway BIO_printf(io, "%s, Cipher is %s\n", 32816f9291ceSJung-uk Kim SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c)); 328274664626SKris Kennaway SSL_SESSION_print(io, SSL_get_session(con)); 328374664626SKris Kennaway BIO_printf(io, "---\n"); 328474664626SKris Kennaway print_stats(io, SSL_get_SSL_CTX(con)); 328574664626SKris Kennaway BIO_printf(io, "---\n"); 3286b077aed3SPierre Pronchery peer = SSL_get0_peer_certificate(con); 32876f9291ceSJung-uk Kim if (peer != NULL) { 328874664626SKris Kennaway BIO_printf(io, "Client certificate\n"); 328974664626SKris Kennaway X509_print(io, peer); 329074664626SKris Kennaway PEM_write_bio_X509(io, peer); 3291e71b7053SJung-uk Kim peer = NULL; 3292e71b7053SJung-uk Kim } else { 329374664626SKris Kennaway BIO_puts(io, "no client certificate available\n"); 3294e71b7053SJung-uk Kim } 329547902a71SJung-uk Kim BIO_puts(io, "</pre></BODY></HTML>\r\n\r\n"); 329674664626SKris Kennaway break; 32976f9291ceSJung-uk Kim } else if ((www == 2 || www == 3) 32986f9291ceSJung-uk Kim && (strncmp("GET /", buf, 5) == 0)) { 329974664626SKris Kennaway BIO *file; 330074664626SKris Kennaway char *p, *e; 33016f9291ceSJung-uk Kim static const char *text = 33026f9291ceSJung-uk Kim "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n"; 330374664626SKris Kennaway 330474664626SKris Kennaway /* skip the '/' */ 330574664626SKris Kennaway p = &(buf[5]); 33065740a5e3SKris Kennaway 33075740a5e3SKris Kennaway dot = 1; 33086f9291ceSJung-uk Kim for (e = p; *e != '\0'; e++) { 33095740a5e3SKris Kennaway if (e[0] == ' ') 33105740a5e3SKris Kennaway break; 331174664626SKris Kennaway 331217f01e99SJung-uk Kim if (e[0] == ':') { 331317f01e99SJung-uk Kim /* Windows drive. We treat this the same way as ".." */ 331417f01e99SJung-uk Kim dot = -1; 331517f01e99SJung-uk Kim break; 331617f01e99SJung-uk Kim } 331717f01e99SJung-uk Kim 33186f9291ceSJung-uk Kim switch (dot) { 33195740a5e3SKris Kennaway case 1: 33205740a5e3SKris Kennaway dot = (e[0] == '.') ? 2 : 0; 33215740a5e3SKris Kennaway break; 33225740a5e3SKris Kennaway case 2: 33235740a5e3SKris Kennaway dot = (e[0] == '.') ? 3 : 0; 33245740a5e3SKris Kennaway break; 33255740a5e3SKris Kennaway case 3: 332617f01e99SJung-uk Kim dot = (e[0] == '/' || e[0] == '\\') ? -1 : 0; 33275740a5e3SKris Kennaway break; 33285740a5e3SKris Kennaway } 33295740a5e3SKris Kennaway if (dot == 0) 333017f01e99SJung-uk Kim dot = (e[0] == '/' || e[0] == '\\') ? 1 : 0; 33315740a5e3SKris Kennaway } 33326f9291ceSJung-uk Kim dot = (dot == 3) || (dot == -1); /* filename contains ".." 33336f9291ceSJung-uk Kim * component */ 333474664626SKris Kennaway 33356f9291ceSJung-uk Kim if (*e == '\0') { 333674664626SKris Kennaway BIO_puts(io, text); 333774664626SKris Kennaway BIO_printf(io, "'%s' is an invalid file name\r\n", p); 333874664626SKris Kennaway break; 333974664626SKris Kennaway } 334074664626SKris Kennaway *e = '\0'; 334174664626SKris Kennaway 33426f9291ceSJung-uk Kim if (dot) { 334374664626SKris Kennaway BIO_puts(io, text); 334417f01e99SJung-uk Kim BIO_printf(io, "'%s' contains '..' or ':'\r\n", p); 334574664626SKris Kennaway break; 334674664626SKris Kennaway } 334774664626SKris Kennaway 334817f01e99SJung-uk Kim if (*p == '/' || *p == '\\') { 334974664626SKris Kennaway BIO_puts(io, text); 335074664626SKris Kennaway BIO_printf(io, "'%s' is an invalid path\r\n", p); 335174664626SKris Kennaway break; 335274664626SKris Kennaway } 335374664626SKris Kennaway 335474664626SKris Kennaway /* if a directory, do the index thang */ 33556f9291ceSJung-uk Kim if (app_isdir(p) > 0) { 33565740a5e3SKris Kennaway BIO_puts(io, text); 33575740a5e3SKris Kennaway BIO_printf(io, "'%s' is a directory\r\n", p); 33585740a5e3SKris Kennaway break; 335974664626SKris Kennaway } 336074664626SKris Kennaway 3361b077aed3SPierre Pronchery opmode = (http_server_binmode == 1) ? "rb" : "r"; 3362b077aed3SPierre Pronchery if ((file = BIO_new_file(p, opmode)) == NULL) { 336374664626SKris Kennaway BIO_puts(io, text); 3364b077aed3SPierre Pronchery BIO_printf(io, "Error opening '%s' mode='%s'\r\n", p, opmode); 336574664626SKris Kennaway ERR_print_errors(io); 336674664626SKris Kennaway break; 336774664626SKris Kennaway } 336874664626SKris Kennaway 336974664626SKris Kennaway if (!s_quiet) 337074664626SKris Kennaway BIO_printf(bio_err, "FILE:%s\n", p); 337174664626SKris Kennaway 33726f9291ceSJung-uk Kim if (www == 2) { 337374664626SKris Kennaway i = strlen(p); 337474664626SKris Kennaway if (((i > 5) && (strcmp(&(p[i - 5]), ".html") == 0)) || 337574664626SKris Kennaway ((i > 4) && (strcmp(&(p[i - 4]), ".php") == 0)) || 337674664626SKris Kennaway ((i > 4) && (strcmp(&(p[i - 4]), ".htm") == 0))) 33776f9291ceSJung-uk Kim BIO_puts(io, 33786f9291ceSJung-uk Kim "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n"); 337974664626SKris Kennaway else 33806f9291ceSJung-uk Kim BIO_puts(io, 33816f9291ceSJung-uk Kim "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n"); 33825c87c606SMark Murray } 338374664626SKris Kennaway /* send the file */ 3384b077aed3SPierre Pronchery #ifndef OPENSSL_NO_KTLS 3385b077aed3SPierre Pronchery if (use_sendfile_for_req && !BIO_get_ktls_send(SSL_get_wbio(con))) { 3386b077aed3SPierre Pronchery BIO_printf(bio_err, "Warning: sendfile requested but KTLS is not available\n"); 3387b077aed3SPierre Pronchery use_sendfile_for_req = 0; 3388b077aed3SPierre Pronchery } 3389b077aed3SPierre Pronchery if (use_sendfile_for_req) { 3390b077aed3SPierre Pronchery FILE *fp = NULL; 3391b077aed3SPierre Pronchery int fd; 3392b077aed3SPierre Pronchery struct stat st; 3393b077aed3SPierre Pronchery off_t offset = 0; 3394b077aed3SPierre Pronchery size_t filesize; 3395b077aed3SPierre Pronchery 3396b077aed3SPierre Pronchery BIO_get_fp(file, &fp); 3397b077aed3SPierre Pronchery fd = fileno(fp); 3398b077aed3SPierre Pronchery if (fstat(fd, &st) < 0) { 3399b077aed3SPierre Pronchery BIO_printf(io, "Error fstat '%s'\r\n", p); 3400b077aed3SPierre Pronchery ERR_print_errors(io); 3401b077aed3SPierre Pronchery goto write_error; 3402b077aed3SPierre Pronchery } 3403b077aed3SPierre Pronchery 3404b077aed3SPierre Pronchery filesize = st.st_size; 3405b077aed3SPierre Pronchery if (((int)BIO_flush(io)) < 0) 3406b077aed3SPierre Pronchery goto write_error; 3407b077aed3SPierre Pronchery 3408b077aed3SPierre Pronchery for (;;) { 3409b077aed3SPierre Pronchery i = SSL_sendfile(con, fd, offset, filesize, 0); 3410b077aed3SPierre Pronchery if (i < 0) { 3411b077aed3SPierre Pronchery BIO_printf(io, "Error SSL_sendfile '%s'\r\n", p); 3412b077aed3SPierre Pronchery ERR_print_errors(io); 3413b077aed3SPierre Pronchery break; 3414b077aed3SPierre Pronchery } else { 3415b077aed3SPierre Pronchery offset += i; 3416b077aed3SPierre Pronchery filesize -= i; 3417b077aed3SPierre Pronchery } 3418b077aed3SPierre Pronchery 3419b077aed3SPierre Pronchery if (filesize <= 0) { 3420b077aed3SPierre Pronchery if (!s_quiet) 3421b077aed3SPierre Pronchery BIO_printf(bio_err, "KTLS SENDFILE '%s' OK\n", p); 3422b077aed3SPierre Pronchery 3423b077aed3SPierre Pronchery break; 3424b077aed3SPierre Pronchery } 3425b077aed3SPierre Pronchery } 3426b077aed3SPierre Pronchery } else 3427b077aed3SPierre Pronchery #endif 3428b077aed3SPierre Pronchery { 34296f9291ceSJung-uk Kim for (;;) { 343074664626SKris Kennaway i = BIO_read(file, buf, bufsize); 34316f9291ceSJung-uk Kim if (i <= 0) 34326f9291ceSJung-uk Kim break; 343374664626SKris Kennaway 343474664626SKris Kennaway #ifdef RENEG 343574664626SKris Kennaway total_bytes += i; 3436e71b7053SJung-uk Kim BIO_printf(bio_err, "%d\n", i); 34376f9291ceSJung-uk Kim if (total_bytes > 3 * 1024) { 343874664626SKris Kennaway total_bytes = 0; 3439e71b7053SJung-uk Kim BIO_printf(bio_err, "RENEGOTIATE\n"); 344074664626SKris Kennaway SSL_renegotiate(con); 344174664626SKris Kennaway } 344274664626SKris Kennaway #endif 344374664626SKris Kennaway 34446f9291ceSJung-uk Kim for (j = 0; j < i;) { 344574664626SKris Kennaway #ifdef RENEG 34466f9291ceSJung-uk Kim static count = 0; 3447b077aed3SPierre Pronchery if (++count == 13) 34486f9291ceSJung-uk Kim SSL_renegotiate(con); 344974664626SKris Kennaway #endif 345074664626SKris Kennaway k = BIO_write(io, &(buf[j]), i - j); 34516f9291ceSJung-uk Kim if (k <= 0) { 3452e71b7053SJung-uk Kim if (!BIO_should_retry(io) 3453b077aed3SPierre Pronchery && !SSL_waiting_for_async(con)) { 345474664626SKris Kennaway goto write_error; 3455b077aed3SPierre Pronchery } else { 345674664626SKris Kennaway BIO_printf(bio_s_out, "rwrite W BLOCK\n"); 345774664626SKris Kennaway } 34586f9291ceSJung-uk Kim } else { 345974664626SKris Kennaway j += k; 346074664626SKris Kennaway } 346174664626SKris Kennaway } 346274664626SKris Kennaway } 3463b077aed3SPierre Pronchery } 346474664626SKris Kennaway write_error: 346574664626SKris Kennaway BIO_free(file); 346674664626SKris Kennaway break; 346774664626SKris Kennaway } 346874664626SKris Kennaway } 346974664626SKris Kennaway 34706f9291ceSJung-uk Kim for (;;) { 347174664626SKris Kennaway i = (int)BIO_flush(io); 34726f9291ceSJung-uk Kim if (i <= 0) { 347374664626SKris Kennaway if (!BIO_should_retry(io)) 347474664626SKris Kennaway break; 34756f9291ceSJung-uk Kim } else 347674664626SKris Kennaway break; 347774664626SKris Kennaway } 347874664626SKris Kennaway end: 347974664626SKris Kennaway /* make sure we re-use sessions */ 3480b077aed3SPierre Pronchery do_ssl_shutdown(con); 348174664626SKris Kennaway 348274664626SKris Kennaway err: 34836f9291ceSJung-uk Kim OPENSSL_free(buf); 3484b077aed3SPierre Pronchery BIO_free(ssl_bio); 34856f9291ceSJung-uk Kim BIO_free_all(io); 3486e71b7053SJung-uk Kim return ret; 348774664626SKris Kennaway } 348874664626SKris Kennaway 3489e71b7053SJung-uk Kim static int rev_body(int s, int stype, int prot, unsigned char *context) 34907bded2dbSJung-uk Kim { 34917bded2dbSJung-uk Kim char *buf = NULL; 34927bded2dbSJung-uk Kim int i; 34937bded2dbSJung-uk Kim int ret = 1; 34947bded2dbSJung-uk Kim SSL *con; 34957bded2dbSJung-uk Kim BIO *io, *ssl_bio, *sbio; 3496b077aed3SPierre Pronchery #ifdef CHARSET_EBCDIC 3497b077aed3SPierre Pronchery BIO *filter; 3498b077aed3SPierre Pronchery #endif 34997bded2dbSJung-uk Kim 3500b077aed3SPierre Pronchery /* as we use BIO_gets(), and it always null terminates data, we need 3501b077aed3SPierre Pronchery * to allocate 1 byte longer buffer to fit the full 2^14 byte record */ 3502b077aed3SPierre Pronchery buf = app_malloc(bufsize + 1, "server rev buffer"); 35037bded2dbSJung-uk Kim io = BIO_new(BIO_f_buffer()); 35047bded2dbSJung-uk Kim ssl_bio = BIO_new(BIO_f_ssl()); 35057bded2dbSJung-uk Kim if ((io == NULL) || (ssl_bio == NULL)) 35067bded2dbSJung-uk Kim goto err; 35077bded2dbSJung-uk Kim 35087bded2dbSJung-uk Kim /* lets make the output buffer a reasonable size */ 3509b077aed3SPierre Pronchery if (BIO_set_write_buffer_size(io, bufsize) <= 0) 35107bded2dbSJung-uk Kim goto err; 35117bded2dbSJung-uk Kim 35127bded2dbSJung-uk Kim if ((con = SSL_new(ctx)) == NULL) 35137bded2dbSJung-uk Kim goto err; 3514e71b7053SJung-uk Kim 35157bded2dbSJung-uk Kim if (s_tlsextdebug) { 35167bded2dbSJung-uk Kim SSL_set_tlsext_debug_callback(con, tlsext_cb); 35177bded2dbSJung-uk Kim SSL_set_tlsext_debug_arg(con, bio_s_out); 35187bded2dbSJung-uk Kim } 3519e71b7053SJung-uk Kim if (context != NULL 3520e71b7053SJung-uk Kim && !SSL_set_session_id_context(con, context, 3521e71b7053SJung-uk Kim strlen((char *)context))) { 3522e71b7053SJung-uk Kim SSL_free(con); 3523e71b7053SJung-uk Kim ERR_print_errors(bio_err); 3524e71b7053SJung-uk Kim goto err; 35257bded2dbSJung-uk Kim } 35267bded2dbSJung-uk Kim 35277bded2dbSJung-uk Kim sbio = BIO_new_socket(s, BIO_NOCLOSE); 3528b077aed3SPierre Pronchery if (sbio == NULL) { 3529b077aed3SPierre Pronchery SSL_free(con); 3530b077aed3SPierre Pronchery ERR_print_errors(bio_err); 3531b077aed3SPierre Pronchery goto err; 3532b077aed3SPierre Pronchery } 3533b077aed3SPierre Pronchery 35347bded2dbSJung-uk Kim SSL_set_bio(con, sbio, sbio); 35357bded2dbSJung-uk Kim SSL_set_accept_state(con); 35367bded2dbSJung-uk Kim 3537e71b7053SJung-uk Kim /* No need to free |con| after this. Done by BIO_free(ssl_bio) */ 35387bded2dbSJung-uk Kim BIO_set_ssl(ssl_bio, con, BIO_CLOSE); 35397bded2dbSJung-uk Kim BIO_push(io, ssl_bio); 3540b077aed3SPierre Pronchery ssl_bio = NULL; 35417bded2dbSJung-uk Kim #ifdef CHARSET_EBCDIC 3542b077aed3SPierre Pronchery filter = BIO_new(BIO_f_ebcdic_filter()); 3543b077aed3SPierre Pronchery if (filter == NULL) 3544b077aed3SPierre Pronchery goto err; 3545b077aed3SPierre Pronchery 3546b077aed3SPierre Pronchery io = BIO_push(filter, io); 35477bded2dbSJung-uk Kim #endif 35487bded2dbSJung-uk Kim 35497bded2dbSJung-uk Kim if (s_debug) { 3550b077aed3SPierre Pronchery BIO_set_callback_ex(SSL_get_rbio(con), bio_dump_callback); 35517bded2dbSJung-uk Kim BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out); 35527bded2dbSJung-uk Kim } 35537bded2dbSJung-uk Kim if (s_msg) { 35547bded2dbSJung-uk Kim #ifndef OPENSSL_NO_SSL_TRACE 35557bded2dbSJung-uk Kim if (s_msg == 2) 35567bded2dbSJung-uk Kim SSL_set_msg_callback(con, SSL_trace); 35577bded2dbSJung-uk Kim else 35587bded2dbSJung-uk Kim #endif 35597bded2dbSJung-uk Kim SSL_set_msg_callback(con, msg_cb); 35607bded2dbSJung-uk Kim SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out); 35617bded2dbSJung-uk Kim } 35627bded2dbSJung-uk Kim 35637bded2dbSJung-uk Kim for (;;) { 35647bded2dbSJung-uk Kim i = BIO_do_handshake(io); 35657bded2dbSJung-uk Kim if (i > 0) 35667bded2dbSJung-uk Kim break; 35677bded2dbSJung-uk Kim if (!BIO_should_retry(io)) { 35687bded2dbSJung-uk Kim BIO_puts(bio_err, "CONNECTION FAILURE\n"); 35697bded2dbSJung-uk Kim ERR_print_errors(bio_err); 35707bded2dbSJung-uk Kim goto end; 35717bded2dbSJung-uk Kim } 357280815a77SJung-uk Kim #ifndef OPENSSL_NO_SRP 357380815a77SJung-uk Kim if (BIO_should_io_special(io) 357480815a77SJung-uk Kim && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) { 357580815a77SJung-uk Kim BIO_printf(bio_s_out, "LOOKUP renego during accept\n"); 3576b077aed3SPierre Pronchery 3577b077aed3SPierre Pronchery lookup_srp_user(&srp_callback_parm, bio_s_out); 3578b077aed3SPierre Pronchery 357980815a77SJung-uk Kim continue; 358080815a77SJung-uk Kim } 358180815a77SJung-uk Kim #endif 35827bded2dbSJung-uk Kim } 35837bded2dbSJung-uk Kim BIO_printf(bio_err, "CONNECTION ESTABLISHED\n"); 3584e71b7053SJung-uk Kim print_ssl_summary(con); 35857bded2dbSJung-uk Kim 35867bded2dbSJung-uk Kim for (;;) { 3587b077aed3SPierre Pronchery i = BIO_gets(io, buf, bufsize + 1); 35887bded2dbSJung-uk Kim if (i < 0) { /* error */ 35897bded2dbSJung-uk Kim if (!BIO_should_retry(io)) { 35907bded2dbSJung-uk Kim if (!s_quiet) 35917bded2dbSJung-uk Kim ERR_print_errors(bio_err); 35927bded2dbSJung-uk Kim goto err; 35937bded2dbSJung-uk Kim } else { 35947bded2dbSJung-uk Kim BIO_printf(bio_s_out, "read R BLOCK\n"); 359580815a77SJung-uk Kim #ifndef OPENSSL_NO_SRP 359680815a77SJung-uk Kim if (BIO_should_io_special(io) 359780815a77SJung-uk Kim && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) { 359880815a77SJung-uk Kim BIO_printf(bio_s_out, "LOOKUP renego during read\n"); 3599b077aed3SPierre Pronchery 3600b077aed3SPierre Pronchery lookup_srp_user(&srp_callback_parm, bio_s_out); 3601b077aed3SPierre Pronchery 360280815a77SJung-uk Kim continue; 360380815a77SJung-uk Kim } 360480815a77SJung-uk Kim #endif 3605b077aed3SPierre Pronchery ossl_sleep(1000); 36067bded2dbSJung-uk Kim continue; 36077bded2dbSJung-uk Kim } 36087bded2dbSJung-uk Kim } else if (i == 0) { /* end of input */ 36097bded2dbSJung-uk Kim ret = 1; 36107bded2dbSJung-uk Kim BIO_printf(bio_err, "CONNECTION CLOSED\n"); 36117bded2dbSJung-uk Kim goto end; 36127bded2dbSJung-uk Kim } else { 36137bded2dbSJung-uk Kim char *p = buf + i - 1; 36147bded2dbSJung-uk Kim while (i && (*p == '\n' || *p == '\r')) { 36157bded2dbSJung-uk Kim p--; 36167bded2dbSJung-uk Kim i--; 36177bded2dbSJung-uk Kim } 3618e71b7053SJung-uk Kim if (!s_ign_eof && (i == 5) && (strncmp(buf, "CLOSE", 5) == 0)) { 36197bded2dbSJung-uk Kim ret = 1; 36207bded2dbSJung-uk Kim BIO_printf(bio_err, "CONNECTION CLOSED\n"); 36217bded2dbSJung-uk Kim goto end; 36227bded2dbSJung-uk Kim } 36237bded2dbSJung-uk Kim BUF_reverse((unsigned char *)buf, NULL, i); 36247bded2dbSJung-uk Kim buf[i] = '\n'; 36257bded2dbSJung-uk Kim BIO_write(io, buf, i + 1); 36267bded2dbSJung-uk Kim for (;;) { 36277bded2dbSJung-uk Kim i = BIO_flush(io); 36287bded2dbSJung-uk Kim if (i > 0) 36297bded2dbSJung-uk Kim break; 36307bded2dbSJung-uk Kim if (!BIO_should_retry(io)) 36317bded2dbSJung-uk Kim goto end; 36327bded2dbSJung-uk Kim } 36337bded2dbSJung-uk Kim } 36347bded2dbSJung-uk Kim } 36357bded2dbSJung-uk Kim end: 36367bded2dbSJung-uk Kim /* make sure we re-use sessions */ 3637b077aed3SPierre Pronchery do_ssl_shutdown(con); 36387bded2dbSJung-uk Kim 36397bded2dbSJung-uk Kim err: 36407bded2dbSJung-uk Kim 36417bded2dbSJung-uk Kim OPENSSL_free(buf); 3642b077aed3SPierre Pronchery BIO_free(ssl_bio); 36437bded2dbSJung-uk Kim BIO_free_all(io); 3644e71b7053SJung-uk Kim return ret; 36457bded2dbSJung-uk Kim } 36467bded2dbSJung-uk Kim 36475c87c606SMark Murray #define MAX_SESSION_ID_ATTEMPTS 10 3648e71b7053SJung-uk Kim static int generate_session_id(SSL *ssl, unsigned char *id, 36495c87c606SMark Murray unsigned int *id_len) 36505c87c606SMark Murray { 36515c87c606SMark Murray unsigned int count = 0; 3652b077aed3SPierre Pronchery unsigned int session_id_prefix_len = strlen(session_id_prefix); 3653b077aed3SPierre Pronchery 36545c87c606SMark Murray do { 3655aeb5019cSJung-uk Kim if (RAND_bytes(id, *id_len) <= 0) 3656ed6b93beSJung-uk Kim return 0; 36576f9291ceSJung-uk Kim /* 36586f9291ceSJung-uk Kim * Prefix the session_id with the required prefix. NB: If our prefix 36596f9291ceSJung-uk Kim * is too long, clip it - but there will be worse effects anyway, eg. 36606f9291ceSJung-uk Kim * the server could only possibly create 1 session ID (ie. the 36616f9291ceSJung-uk Kim * prefix!) so all future session negotiations will fail due to 36626f9291ceSJung-uk Kim * conflicts. 36636f9291ceSJung-uk Kim */ 36645c87c606SMark Murray memcpy(id, session_id_prefix, 3665b077aed3SPierre Pronchery (session_id_prefix_len < *id_len) ? 3666b077aed3SPierre Pronchery session_id_prefix_len : *id_len); 36675c87c606SMark Murray } 36685c87c606SMark Murray while (SSL_has_matching_session_id(ssl, id, *id_len) && 36695c87c606SMark Murray (++count < MAX_SESSION_ID_ATTEMPTS)); 36705c87c606SMark Murray if (count >= MAX_SESSION_ID_ATTEMPTS) 36715c87c606SMark Murray return 0; 36725c87c606SMark Murray return 1; 36735c87c606SMark Murray } 36747bded2dbSJung-uk Kim 36757bded2dbSJung-uk Kim /* 36767bded2dbSJung-uk Kim * By default s_server uses an in-memory cache which caches SSL_SESSION 3677b077aed3SPierre Pronchery * structures without any serialization. This hides some bugs which only 36787bded2dbSJung-uk Kim * become apparent in deployed servers. By implementing a basic external 36797bded2dbSJung-uk Kim * session cache some issues can be debugged using s_server. 36807bded2dbSJung-uk Kim */ 36817bded2dbSJung-uk Kim 36827bded2dbSJung-uk Kim typedef struct simple_ssl_session_st { 36837bded2dbSJung-uk Kim unsigned char *id; 36847bded2dbSJung-uk Kim unsigned int idlen; 36857bded2dbSJung-uk Kim unsigned char *der; 36867bded2dbSJung-uk Kim int derlen; 36877bded2dbSJung-uk Kim struct simple_ssl_session_st *next; 36887bded2dbSJung-uk Kim } simple_ssl_session; 36897bded2dbSJung-uk Kim 36907bded2dbSJung-uk Kim static simple_ssl_session *first = NULL; 36917bded2dbSJung-uk Kim 36927bded2dbSJung-uk Kim static int add_session(SSL *ssl, SSL_SESSION *session) 36937bded2dbSJung-uk Kim { 3694e71b7053SJung-uk Kim simple_ssl_session *sess = app_malloc(sizeof(*sess), "get session"); 36957bded2dbSJung-uk Kim unsigned char *p; 36967bded2dbSJung-uk Kim 36977bded2dbSJung-uk Kim SSL_SESSION_get_id(session, &sess->idlen); 36987bded2dbSJung-uk Kim sess->derlen = i2d_SSL_SESSION(session, NULL); 3699e71b7053SJung-uk Kim if (sess->derlen < 0) { 3700e71b7053SJung-uk Kim BIO_printf(bio_err, "Error encoding session\n"); 3701e71b7053SJung-uk Kim OPENSSL_free(sess); 3702e71b7053SJung-uk Kim return 0; 3703e71b7053SJung-uk Kim } 37047bded2dbSJung-uk Kim 3705e71b7053SJung-uk Kim sess->id = OPENSSL_memdup(SSL_SESSION_get_id(session, NULL), sess->idlen); 3706e71b7053SJung-uk Kim sess->der = app_malloc(sess->derlen, "get session buffer"); 3707e71b7053SJung-uk Kim if (!sess->id) { 3708e71b7053SJung-uk Kim BIO_printf(bio_err, "Out of memory adding to external cache\n"); 37097bded2dbSJung-uk Kim OPENSSL_free(sess->id); 37107bded2dbSJung-uk Kim OPENSSL_free(sess->der); 37117bded2dbSJung-uk Kim OPENSSL_free(sess); 37127bded2dbSJung-uk Kim return 0; 37137bded2dbSJung-uk Kim } 37147bded2dbSJung-uk Kim p = sess->der; 3715e71b7053SJung-uk Kim 3716e71b7053SJung-uk Kim /* Assume it still works. */ 3717e71b7053SJung-uk Kim if (i2d_SSL_SESSION(session, &p) != sess->derlen) { 3718e71b7053SJung-uk Kim BIO_printf(bio_err, "Unexpected session encoding length\n"); 3719e71b7053SJung-uk Kim OPENSSL_free(sess->id); 3720e71b7053SJung-uk Kim OPENSSL_free(sess->der); 3721e71b7053SJung-uk Kim OPENSSL_free(sess); 3722e71b7053SJung-uk Kim return 0; 3723e71b7053SJung-uk Kim } 37247bded2dbSJung-uk Kim 37257bded2dbSJung-uk Kim sess->next = first; 37267bded2dbSJung-uk Kim first = sess; 37277bded2dbSJung-uk Kim BIO_printf(bio_err, "New session added to external cache\n"); 37287bded2dbSJung-uk Kim return 0; 37297bded2dbSJung-uk Kim } 37307bded2dbSJung-uk Kim 3731e71b7053SJung-uk Kim static SSL_SESSION *get_session(SSL *ssl, const unsigned char *id, int idlen, 37327bded2dbSJung-uk Kim int *do_copy) 37337bded2dbSJung-uk Kim { 37347bded2dbSJung-uk Kim simple_ssl_session *sess; 37357bded2dbSJung-uk Kim *do_copy = 0; 37367bded2dbSJung-uk Kim for (sess = first; sess; sess = sess->next) { 37377bded2dbSJung-uk Kim if (idlen == (int)sess->idlen && !memcmp(sess->id, id, idlen)) { 37387bded2dbSJung-uk Kim const unsigned char *p = sess->der; 37397bded2dbSJung-uk Kim BIO_printf(bio_err, "Lookup session: cache hit\n"); 37407bded2dbSJung-uk Kim return d2i_SSL_SESSION(NULL, &p, sess->derlen); 37417bded2dbSJung-uk Kim } 37427bded2dbSJung-uk Kim } 37437bded2dbSJung-uk Kim BIO_printf(bio_err, "Lookup session: cache miss\n"); 37447bded2dbSJung-uk Kim return NULL; 37457bded2dbSJung-uk Kim } 37467bded2dbSJung-uk Kim 37477bded2dbSJung-uk Kim static void del_session(SSL_CTX *sctx, SSL_SESSION *session) 37487bded2dbSJung-uk Kim { 37497bded2dbSJung-uk Kim simple_ssl_session *sess, *prev = NULL; 37507bded2dbSJung-uk Kim const unsigned char *id; 37517bded2dbSJung-uk Kim unsigned int idlen; 37527bded2dbSJung-uk Kim id = SSL_SESSION_get_id(session, &idlen); 37537bded2dbSJung-uk Kim for (sess = first; sess; sess = sess->next) { 37547bded2dbSJung-uk Kim if (idlen == sess->idlen && !memcmp(sess->id, id, idlen)) { 37557bded2dbSJung-uk Kim if (prev) 37567bded2dbSJung-uk Kim prev->next = sess->next; 37577bded2dbSJung-uk Kim else 37587bded2dbSJung-uk Kim first = sess->next; 37597bded2dbSJung-uk Kim OPENSSL_free(sess->id); 37607bded2dbSJung-uk Kim OPENSSL_free(sess->der); 37617bded2dbSJung-uk Kim OPENSSL_free(sess); 37627bded2dbSJung-uk Kim return; 37637bded2dbSJung-uk Kim } 37647bded2dbSJung-uk Kim prev = sess; 37657bded2dbSJung-uk Kim } 37667bded2dbSJung-uk Kim } 37677bded2dbSJung-uk Kim 37687bded2dbSJung-uk Kim static void init_session_cache_ctx(SSL_CTX *sctx) 37697bded2dbSJung-uk Kim { 37707bded2dbSJung-uk Kim SSL_CTX_set_session_cache_mode(sctx, 37717bded2dbSJung-uk Kim SSL_SESS_CACHE_NO_INTERNAL | 37727bded2dbSJung-uk Kim SSL_SESS_CACHE_SERVER); 37737bded2dbSJung-uk Kim SSL_CTX_sess_set_new_cb(sctx, add_session); 37747bded2dbSJung-uk Kim SSL_CTX_sess_set_get_cb(sctx, get_session); 37757bded2dbSJung-uk Kim SSL_CTX_sess_set_remove_cb(sctx, del_session); 37767bded2dbSJung-uk Kim } 37777bded2dbSJung-uk Kim 37787bded2dbSJung-uk Kim static void free_sessions(void) 37797bded2dbSJung-uk Kim { 37807bded2dbSJung-uk Kim simple_ssl_session *sess, *tsess; 37817bded2dbSJung-uk Kim for (sess = first; sess;) { 37827bded2dbSJung-uk Kim OPENSSL_free(sess->id); 37837bded2dbSJung-uk Kim OPENSSL_free(sess->der); 37847bded2dbSJung-uk Kim tsess = sess; 37857bded2dbSJung-uk Kim sess = sess->next; 37867bded2dbSJung-uk Kim OPENSSL_free(tsess); 37877bded2dbSJung-uk Kim } 37887bded2dbSJung-uk Kim first = NULL; 37897bded2dbSJung-uk Kim } 3790e71b7053SJung-uk Kim 3791e71b7053SJung-uk Kim #endif /* OPENSSL_NO_SOCK */ 3792