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