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