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