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