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