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