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