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