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