Annotation of src/usr.bin/openssl/s_server.c, Revision 1.35
1.35 ! inoguchi 1: /* $OpenBSD: s_server.c,v 1.34 2020/05/10 16:55:28 beck Exp $ */
1.1 jsing 2: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3: * All rights reserved.
4: *
5: * This package is an SSL implementation written
6: * by Eric Young (eay@cryptsoft.com).
7: * The implementation was written so as to conform with Netscapes SSL.
8: *
9: * This library is free for commercial and non-commercial use as long as
10: * the following conditions are aheared to. The following conditions
11: * apply to all code found in this distribution, be it the RC4, RSA,
12: * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13: * included with this distribution is covered by the same copyright terms
14: * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15: *
16: * Copyright remains Eric Young's, and as such any Copyright notices in
17: * the code are not to be removed.
18: * If this package is used in a product, Eric Young should be given attribution
19: * as the author of the parts of the library used.
20: * This can be in the form of a textual message at program startup or
21: * in documentation (online or textual) provided with the package.
22: *
23: * Redistribution and use in source and binary forms, with or without
24: * modification, are permitted provided that the following conditions
25: * are met:
26: * 1. Redistributions of source code must retain the copyright
27: * notice, this list of conditions and the following disclaimer.
28: * 2. Redistributions in binary form must reproduce the above copyright
29: * notice, this list of conditions and the following disclaimer in the
30: * documentation and/or other materials provided with the distribution.
31: * 3. All advertising materials mentioning features or use of this software
32: * must display the following acknowledgement:
33: * "This product includes cryptographic software written by
34: * Eric Young (eay@cryptsoft.com)"
35: * The word 'cryptographic' can be left out if the rouines from the library
36: * being used are not cryptographic related :-).
37: * 4. If you include any Windows specific code (or a derivative thereof) from
38: * the apps directory (application code) you must include an acknowledgement:
39: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40: *
41: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51: * SUCH DAMAGE.
52: *
53: * The licence and distribution terms for any publically available version or
54: * derivative of this code cannot be changed. i.e. this code cannot simply be
55: * copied and put under another distribution licence
56: * [including the GNU Public Licence.]
57: */
58: /* ====================================================================
59: * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
60: *
61: * Redistribution and use in source and binary forms, with or without
62: * modification, are permitted provided that the following conditions
63: * are met:
64: *
65: * 1. Redistributions of source code must retain the above copyright
66: * notice, this list of conditions and the following disclaimer.
67: *
68: * 2. Redistributions in binary form must reproduce the above copyright
69: * notice, this list of conditions and the following disclaimer in
70: * the documentation and/or other materials provided with the
71: * distribution.
72: *
73: * 3. All advertising materials mentioning features or use of this
74: * software must display the following acknowledgment:
75: * "This product includes software developed by the OpenSSL Project
76: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77: *
78: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79: * endorse or promote products derived from this software without
80: * prior written permission. For written permission, please contact
81: * openssl-core@openssl.org.
82: *
83: * 5. Products derived from this software may not be called "OpenSSL"
84: * nor may "OpenSSL" appear in their names without prior written
85: * permission of the OpenSSL Project.
86: *
87: * 6. Redistributions of any form whatsoever must retain the following
88: * acknowledgment:
89: * "This product includes software developed by the OpenSSL Project
90: * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91: *
92: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103: * OF THE POSSIBILITY OF SUCH DAMAGE.
104: * ====================================================================
105: *
106: * This product includes cryptographic software written by Eric Young
107: * (eay@cryptsoft.com). This product includes software written by Tim
108: * Hudson (tjh@cryptsoft.com).
109: *
110: */
111: /* ====================================================================
112: * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113: * ECC cipher suite support in OpenSSL originally developed by
114: * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
115: */
116: /* ====================================================================
117: * Copyright 2005 Nokia. All rights reserved.
118: *
119: * The portions of the attached software ("Contribution") is developed by
120: * Nokia Corporation and is licensed pursuant to the OpenSSL open source
121: * license.
122: *
123: * The Contribution, originally written by Mika Kousa and Pasi Eronen of
124: * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
125: * support (see RFC 4279) to OpenSSL.
126: *
127: * No patent licenses or other rights except those expressly stated in
128: * the OpenSSL open source license shall be deemed granted or received
129: * expressly, by implication, estoppel, or otherwise.
130: *
131: * No assurances are provided by Nokia that the Contribution does not
132: * infringe the patent or other intellectual property rights of any third
133: * party or that the license provides you with all the necessary rights
134: * to make use of the Contribution.
135: *
136: * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
137: * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
138: * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
139: * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
140: * OTHERWISE.
141: */
142:
143: /* Until the key-gen callbacks are modified to use newer prototypes, we allow
144: * deprecated functions for openssl-internal code */
145: #ifdef OPENSSL_NO_DEPRECATED
146: #undef OPENSSL_NO_DEPRECATED
147: #endif
148:
149: #include <sys/types.h>
150: #include <sys/socket.h>
151:
152: #include <assert.h>
153: #include <ctype.h>
154: #include <stdio.h>
155: #include <stdlib.h>
156: #include <limits.h>
157: #include <string.h>
158: #include <unistd.h>
1.7 deraadt 159: #include <poll.h>
1.1 jsing 160:
161: #include "apps.h"
162:
163: #include <openssl/bn.h>
164: #include <openssl/err.h>
165: #include <openssl/lhash.h>
166: #include <openssl/ocsp.h>
167: #include <openssl/pem.h>
168: #include <openssl/ssl.h>
169: #include <openssl/x509.h>
170:
171: #ifndef OPENSSL_NO_DH
172: #include <openssl/dh.h>
173: #endif
174:
175: #include <openssl/rsa.h>
176:
177: #include "s_apps.h"
178: #include "timeouts.h"
179:
180: static int sv_body(char *hostname, int s, unsigned char *context);
181: static int www_body(char *hostname, int s, unsigned char *context);
182: static void close_accept_socket(void);
183: static void sv_usage(void);
184: static int init_ssl_connection(SSL * s);
185: static void print_stats(BIO * bp, SSL_CTX * ctx);
186: static int
187: generate_session_id(const SSL * ssl, unsigned char *id,
188: unsigned int *id_len);
189: #ifndef OPENSSL_NO_DH
190: static DH *load_dh_param(const char *dhfile);
191: #endif
192:
193: static void s_server_init(void);
194:
195: /* static int load_CA(SSL_CTX *ctx, char *file);*/
196:
197: #define BUFSIZZ 16*1024
198: static int bufsize = BUFSIZZ;
199: static int accept_socket = -1;
200:
201: #define TEST_CERT "server.pem"
202: #define TEST_CERT2 "server2.pem"
203:
204: static char *cipher = NULL;
205: static int s_server_verify = SSL_VERIFY_NONE;
206: static int s_server_session_id_context = 1; /* anything will do */
207: static const char *s_cert_file = TEST_CERT, *s_key_file = NULL;
208: static const char *s_cert_file2 = TEST_CERT2, *s_key_file2 = NULL;
209: static char *s_dcert_file = NULL, *s_dkey_file = NULL;
210: static int s_nbio = 0;
211: static int s_nbio_test = 0;
212: int s_crlf = 0;
213: static SSL_CTX *ctx = NULL;
214: static SSL_CTX *ctx2 = NULL;
215: static int www = 0;
216:
217: static BIO *bio_s_out = NULL;
218: static int s_debug = 0;
219: static int s_tlsextdebug = 0;
220: static int s_tlsextstatus = 0;
221: static int cert_status_cb(SSL * s, void *arg);
222: static int s_msg = 0;
223: static int s_quiet = 0;
224:
225: static char *keymatexportlabel = NULL;
226: static int keymatexportlen = 20;
227:
228: static const char *session_id_prefix = NULL;
229:
230: static int enable_timeouts = 0;
231: static long socket_mtu;
232: #ifndef OPENSSL_NO_DTLS1
233: static int cert_chain = 0;
234: #endif
235:
236:
237:
238:
239: static void
240: s_server_init(void)
241: {
242: accept_socket = -1;
243: cipher = NULL;
244: s_server_verify = SSL_VERIFY_NONE;
245: s_dcert_file = NULL;
246: s_dkey_file = NULL;
247: s_cert_file = TEST_CERT;
248: s_key_file = NULL;
249: s_cert_file2 = TEST_CERT2;
250: s_key_file2 = NULL;
251: ctx2 = NULL;
252: s_nbio = 0;
253: s_nbio_test = 0;
254: ctx = NULL;
255: www = 0;
256:
257: bio_s_out = NULL;
258: s_debug = 0;
259: s_msg = 0;
260: s_quiet = 0;
261: }
262:
263: static void
264: sv_usage(void)
265: {
266: BIO_printf(bio_err, "usage: s_server [args ...]\n");
267: BIO_printf(bio_err, "\n");
268: BIO_printf(bio_err, " -accept arg - port to accept on (default is %d)\n", PORT);
269: BIO_printf(bio_err, " -context arg - set session ID context\n");
270: BIO_printf(bio_err, " -verify arg - turn on peer certificate verification\n");
271: BIO_printf(bio_err, " -Verify arg - turn on peer certificate verification, must have a cert.\n");
272: BIO_printf(bio_err, " -cert arg - certificate file to use\n");
273: BIO_printf(bio_err, " (default is %s)\n", TEST_CERT);
274: BIO_printf(bio_err, " -crl_check - check the peer certificate has not been revoked by its CA.\n" \
275: " The CRL(s) are appended to the certificate file\n");
276: BIO_printf(bio_err, " -crl_check_all - check the peer certificate has not been revoked by its CA\n" \
277: " or any other CRL in the CA chain. CRL(s) are appended to the\n" \
278: " the certificate file.\n");
279: BIO_printf(bio_err, " -certform arg - certificate format (PEM or DER) PEM default\n");
280: BIO_printf(bio_err, " -key arg - Private Key file to use, in cert file if\n");
281: BIO_printf(bio_err, " not specified (default is %s)\n", TEST_CERT);
1.17 bcook 282: BIO_printf(bio_err, " -keyform arg - key format (PEM or DER) PEM default\n");
1.1 jsing 283: BIO_printf(bio_err, " -pass arg - private key file pass phrase source\n");
284: BIO_printf(bio_err, " -dcert arg - second certificate file to use (usually for DSA)\n");
285: BIO_printf(bio_err, " -dcertform x - second certificate format (PEM or DER) PEM default\n");
286: BIO_printf(bio_err, " -dkey arg - second private key file to use (usually for DSA)\n");
1.17 bcook 287: BIO_printf(bio_err, " -dkeyform arg - second key format (PEM or DER) PEM default\n");
1.1 jsing 288: BIO_printf(bio_err, " -dpass arg - second private key file pass phrase source\n");
289: BIO_printf(bio_err, " -dhparam arg - DH parameter file to use, in cert file if not specified\n");
290: BIO_printf(bio_err, " or a default set of parameters is used\n");
291: BIO_printf(bio_err, " -nbio - Run with non-blocking IO\n");
292: BIO_printf(bio_err, " -nbio_test - test with the non-blocking test bio\n");
293: BIO_printf(bio_err, " -crlf - convert LF from terminal into CRLF\n");
294: BIO_printf(bio_err, " -debug - Print more output\n");
295: BIO_printf(bio_err, " -msg - Show protocol messages\n");
296: BIO_printf(bio_err, " -state - Print the SSL states\n");
297: BIO_printf(bio_err, " -CApath arg - PEM format directory of CA's\n");
298: BIO_printf(bio_err, " -CAfile arg - PEM format file of CA's\n");
299: BIO_printf(bio_err, " -nocert - Don't use any certificates (Anon-DH)\n");
300: BIO_printf(bio_err, " -cipher arg - play with 'openssl ciphers' to see what goes here\n");
301: BIO_printf(bio_err, " -serverpref - Use server's cipher preferences\n");
302: BIO_printf(bio_err, " -quiet - Inhibit printing of session and certificate information\n");
1.35 ! inoguchi 303: BIO_printf(bio_err, " -tls1_3 - Just talk TLSv1.3\n");
1.1 jsing 304: BIO_printf(bio_err, " -tls1_2 - Just talk TLSv1.2\n");
305: BIO_printf(bio_err, " -tls1_1 - Just talk TLSv1.1\n");
306: BIO_printf(bio_err, " -tls1 - Just talk TLSv1\n");
307: BIO_printf(bio_err, " -dtls1 - Just talk DTLSv1\n");
308: BIO_printf(bio_err, " -timeout - Enable timeouts\n");
309: BIO_printf(bio_err, " -mtu - Set link layer MTU\n");
310: BIO_printf(bio_err, " -chain - Read a certificate chain\n");
311: BIO_printf(bio_err, " -no_ssl2 - Just disable SSLv2\n");
312: BIO_printf(bio_err, " -no_ssl3 - Just disable SSLv3\n");
313: BIO_printf(bio_err, " -no_tls1 - Just disable TLSv1\n");
314: BIO_printf(bio_err, " -no_tls1_1 - Just disable TLSv1.1\n");
315: BIO_printf(bio_err, " -no_tls1_2 - Just disable TLSv1.2\n");
1.35 ! inoguchi 316: BIO_printf(bio_err, " -no_tls1_3 - Just disable TLSv1.3\n");
1.1 jsing 317: #ifndef OPENSSL_NO_DH
318: BIO_printf(bio_err, " -no_dhe - Disable ephemeral DH\n");
319: #endif
320: BIO_printf(bio_err, " -no_ecdhe - Disable ephemeral ECDH\n");
321: BIO_printf(bio_err, " -bugs - Turn on SSL bug compatibility\n");
322: BIO_printf(bio_err, " -www - Respond to a 'GET /' with a status page\n");
323: BIO_printf(bio_err, " -WWW - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
324: BIO_printf(bio_err, " -HTTP - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
325: BIO_printf(bio_err, " with the assumption it contains a complete HTTP response.\n");
326: BIO_printf(bio_err, " -id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'\n");
327: BIO_printf(bio_err, " -servername host - servername for HostName TLS extension\n");
328: BIO_printf(bio_err, " -servername_fatal - on mismatch send fatal alert (default warning alert)\n");
329: BIO_printf(bio_err, " -cert2 arg - certificate file to use for servername\n");
330: BIO_printf(bio_err, " (default is %s)\n", TEST_CERT2);
331: BIO_printf(bio_err, " -key2 arg - Private Key file to use for servername, in cert file if\n");
332: BIO_printf(bio_err, " not specified (default is %s)\n", TEST_CERT2);
333: BIO_printf(bio_err, " -tlsextdebug - hex dump of all TLS extensions received\n");
334: BIO_printf(bio_err, " -no_ticket - disable use of RFC4507bis session tickets\n");
1.33 jsing 335: BIO_printf(bio_err, " -alpn arg - set the advertised protocols for the ALPN extension (comma-separated list)\n");
336: BIO_printf(bio_err, " -groups arg - specify EC groups (colon-separated list)\n");
1.1 jsing 337: #ifndef OPENSSL_NO_SRTP
338: BIO_printf(bio_err, " -use_srtp profiles - Offer SRTP key management with a colon-separated profile list\n");
339: #endif
340: BIO_printf(bio_err, " -keymatexport label - Export keying material using label\n");
341: BIO_printf(bio_err, " -keymatexportlen len - Export len bytes of keying material (default 20)\n");
342: }
343:
344: static int local_argc = 0;
345: static char **local_argv;
346:
347:
348: /* This is a context that we pass to callbacks */
349: typedef struct tlsextctx_st {
350: char *servername;
351: BIO *biodebug;
352: int extension_error;
353: } tlsextctx;
354:
355:
356: static int
357: ssl_servername_cb(SSL * s, int *ad, void *arg)
358: {
359: tlsextctx *p = (tlsextctx *) arg;
360: const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
361: if (servername && p->biodebug)
362: BIO_printf(p->biodebug, "Hostname in TLS extension: \"%s\"\n", servername);
363:
364: if (!p->servername)
365: return SSL_TLSEXT_ERR_NOACK;
366:
367: if (servername) {
368: if (strcmp(servername, p->servername))
369: return p->extension_error;
370: if (ctx2) {
371: BIO_printf(p->biodebug, "Switching server context.\n");
372: SSL_set_SSL_CTX(s, ctx2);
373: }
374: }
375: return SSL_TLSEXT_ERR_OK;
376: }
377:
378: /* Structure passed to cert status callback */
379:
380: typedef struct tlsextstatusctx_st {
381: /* Default responder to use */
382: char *host, *path, *port;
383: int use_ssl;
384: int timeout;
385: BIO *err;
386: int verbose;
387: } tlsextstatusctx;
388:
389: static tlsextstatusctx tlscstatp = {NULL, NULL, NULL, 0, -1, NULL, 0};
390:
391: /* Certificate Status callback. This is called when a client includes a
392: * certificate status request extension.
393: *
394: * This is a simplified version. It examines certificates each time and
395: * makes one OCSP responder query for each request.
396: *
397: * A full version would store details such as the OCSP certificate IDs and
398: * minimise the number of OCSP responses by caching them until they were
399: * considered "expired".
400: */
401:
402: static int
403: cert_status_cb(SSL * s, void *arg)
404: {
405: tlsextstatusctx *srctx = arg;
406: BIO *err = srctx->err;
1.28 inoguchi 407: char *host = NULL, *port = NULL, *path = NULL;
1.1 jsing 408: int use_ssl;
409: unsigned char *rspder = NULL;
410: int rspderlen;
411: STACK_OF(OPENSSL_STRING) * aia = NULL;
412: X509 *x = NULL;
413: X509_STORE_CTX inctx;
414: X509_OBJECT obj;
415: OCSP_REQUEST *req = NULL;
416: OCSP_RESPONSE *resp = NULL;
417: OCSP_CERTID *id = NULL;
418: STACK_OF(X509_EXTENSION) * exts;
419: int ret = SSL_TLSEXT_ERR_NOACK;
420: int i;
421:
422: if (srctx->verbose)
423: BIO_puts(err, "cert_status: callback called\n");
424: /* Build up OCSP query from server certificate */
425: x = SSL_get_certificate(s);
426: aia = X509_get1_ocsp(x);
427: if (aia) {
428: if (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia, 0),
429: &host, &port, &path, &use_ssl)) {
430: BIO_puts(err, "cert_status: can't parse AIA URL\n");
431: goto err;
432: }
433: if (srctx->verbose)
434: BIO_printf(err, "cert_status: AIA URL: %s\n",
435: sk_OPENSSL_STRING_value(aia, 0));
436: } else {
437: if (!srctx->host) {
438: BIO_puts(srctx->err, "cert_status: no AIA and no default responder URL\n");
439: goto done;
440: }
441: host = srctx->host;
442: path = srctx->path;
443: port = srctx->port;
444: use_ssl = srctx->use_ssl;
445: }
446:
447: if (!X509_STORE_CTX_init(&inctx,
448: SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)),
449: NULL, NULL))
450: goto err;
451: if (X509_STORE_get_by_subject(&inctx, X509_LU_X509,
452: X509_get_issuer_name(x), &obj) <= 0) {
453: BIO_puts(err, "cert_status: Can't retrieve issuer certificate.\n");
454: X509_STORE_CTX_cleanup(&inctx);
455: goto done;
456: }
457: req = OCSP_REQUEST_new();
458: if (!req)
459: goto err;
460: id = OCSP_cert_to_id(NULL, x, obj.data.x509);
461: X509_free(obj.data.x509);
462: X509_STORE_CTX_cleanup(&inctx);
463: if (!id)
464: goto err;
465: if (!OCSP_request_add0_id(req, id))
466: goto err;
467: id = NULL;
468: /* Add any extensions to the request */
469: SSL_get_tlsext_status_exts(s, &exts);
470: for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
471: X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
472: if (!OCSP_REQUEST_add_ext(req, ext, -1))
473: goto err;
474: }
475: resp = process_responder(err, req, host, path, port, use_ssl, NULL,
476: srctx->timeout);
477: if (!resp) {
478: BIO_puts(err, "cert_status: error querying responder\n");
479: goto done;
480: }
481: rspderlen = i2d_OCSP_RESPONSE(resp, &rspder);
482: if (rspderlen <= 0)
483: goto err;
484: SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen);
485: if (srctx->verbose) {
486: BIO_puts(err, "cert_status: ocsp response sent:\n");
487: OCSP_RESPONSE_print(err, resp, 2);
488: }
489: ret = SSL_TLSEXT_ERR_OK;
1.30 jsing 490: done:
1.1 jsing 491: if (ret != SSL_TLSEXT_ERR_OK)
492: ERR_print_errors(err);
493: if (aia) {
494: free(host);
495: free(path);
496: free(port);
497: X509_email_free(aia);
498: }
499: if (id)
500: OCSP_CERTID_free(id);
501: if (req)
502: OCSP_REQUEST_free(req);
503: if (resp)
504: OCSP_RESPONSE_free(resp);
505: return ret;
1.30 jsing 506: err:
1.1 jsing 507: ret = SSL_TLSEXT_ERR_ALERT_FATAL;
508: goto done;
509: }
510:
1.8 jsing 511: /* This the context that we pass to alpn_cb */
512: typedef struct tlsextalpnctx_st {
513: unsigned char *data;
514: unsigned short len;
515: } tlsextalpnctx;
516:
517: static int
518: alpn_cb(SSL *s, const unsigned char **out, unsigned char *outlen,
519: const unsigned char *in, unsigned int inlen, void *arg)
520: {
521: tlsextalpnctx *alpn_ctx = arg;
522:
523: if (!s_quiet) {
524: /* We can assume that in is syntactically valid. */
525: unsigned i;
526:
527: BIO_printf(bio_s_out,
528: "ALPN protocols advertised by the client: ");
529: for (i = 0; i < inlen; ) {
530: if (i)
531: BIO_write(bio_s_out, ", ", 2);
532: BIO_write(bio_s_out, &in[i + 1], in[i]);
533: i += in[i] + 1;
534: }
535: BIO_write(bio_s_out, "\n", 1);
536: }
537:
538: if (SSL_select_next_proto((unsigned char**)out, outlen, alpn_ctx->data,
539: alpn_ctx->len, in, inlen) != OPENSSL_NPN_NEGOTIATED)
540: return (SSL_TLSEXT_ERR_NOACK);
541:
542: if (!s_quiet) {
543: BIO_printf(bio_s_out, "ALPN protocols selected: ");
544: BIO_write(bio_s_out, *out, *outlen);
545: BIO_write(bio_s_out, "\n", 1);
546: }
547:
548: return (SSL_TLSEXT_ERR_OK);
549: }
1.1 jsing 550:
551: #ifndef OPENSSL_NO_SRTP
552: static char *srtp_profiles = NULL;
553: #endif
554:
555: int
556: s_server_main(int argc, char *argv[])
557: {
558: X509_VERIFY_PARAM *vpm = NULL;
559: int badarg = 0;
560: short port = PORT;
561: char *CApath = NULL, *CAfile = NULL;
562: unsigned char *context = NULL;
563: char *dhfile = NULL;
564: char *named_curve = NULL;
565: int badop = 0, bugs = 0;
566: int ret = 1;
567: int off = 0;
1.5 jsing 568: int no_dhe = 0, no_ecdhe = 0, nocert = 0;
1.1 jsing 569: int state = 0;
570: const SSL_METHOD *meth = NULL;
571: int socket_type = SOCK_STREAM;
572: int s_cert_format = FORMAT_PEM, s_key_format = FORMAT_PEM;
573: char *passarg = NULL, *pass = NULL;
574: char *dpassarg = NULL, *dpass = NULL;
575: int s_dcert_format = FORMAT_PEM, s_dkey_format = FORMAT_PEM;
576: X509 *s_cert = NULL, *s_dcert = NULL;
577: EVP_PKEY *s_key = NULL, *s_dkey = NULL;
578: int no_cache = 0;
579: const char *errstr = NULL;
580: EVP_PKEY *s_key2 = NULL;
581: X509 *s_cert2 = NULL;
582: tlsextctx tlsextcbp = {NULL, NULL, SSL_TLSEXT_ERR_ALERT_WARNING};
1.8 jsing 583: const char *alpn_in = NULL;
1.33 jsing 584: const char *groups_in = NULL;
1.8 jsing 585: tlsextalpnctx alpn_ctx = { NULL, 0 };
1.35 ! inoguchi 586: uint16_t min_version = 0, max_version = 0;
1.20 doug 587:
588: if (single_execution) {
1.25 deraadt 589: if (pledge("stdio rpath inet dns tty", NULL) == -1) {
1.20 doug 590: perror("pledge");
1.22 doug 591: exit(1);
592: }
1.20 doug 593: }
594:
1.35 ! inoguchi 595: meth = TLS_server_method();
1.1 jsing 596:
597: local_argc = argc;
598: local_argv = argv;
599:
600: s_server_init();
601:
602: verify_depth = 0;
603: s_nbio = 0;
604: s_nbio_test = 0;
605:
606: argc--;
607: argv++;
608:
609: while (argc >= 1) {
610: if ((strcmp(*argv, "-port") == 0) ||
611: (strcmp(*argv, "-accept") == 0)) {
612: if (--argc < 1)
613: goto bad;
614: if (!extract_port(*(++argv), &port))
615: goto bad;
616: } else if (strcmp(*argv, "-verify") == 0) {
617: s_server_verify = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE;
618: if (--argc < 1)
619: goto bad;
620: verify_depth = strtonum(*(++argv), 0, INT_MAX, &errstr);
621: if (errstr)
622: goto bad;
623: BIO_printf(bio_err, "verify depth is %d\n", verify_depth);
624: } else if (strcmp(*argv, "-Verify") == 0) {
625: s_server_verify = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
626: SSL_VERIFY_CLIENT_ONCE;
627: if (--argc < 1)
628: goto bad;
629: verify_depth = strtonum(*(++argv), 0, INT_MAX, &errstr);
630: if (errstr)
631: goto bad;
632: BIO_printf(bio_err, "verify depth is %d, must return a certificate\n", verify_depth);
633: } else if (strcmp(*argv, "-context") == 0) {
634: if (--argc < 1)
635: goto bad;
636: context = (unsigned char *) *(++argv);
637: } else if (strcmp(*argv, "-cert") == 0) {
638: if (--argc < 1)
639: goto bad;
640: s_cert_file = *(++argv);
641: } else if (strcmp(*argv, "-certform") == 0) {
642: if (--argc < 1)
643: goto bad;
644: s_cert_format = str2fmt(*(++argv));
645: } else if (strcmp(*argv, "-key") == 0) {
646: if (--argc < 1)
647: goto bad;
648: s_key_file = *(++argv);
649: } else if (strcmp(*argv, "-keyform") == 0) {
650: if (--argc < 1)
651: goto bad;
652: s_key_format = str2fmt(*(++argv));
653: } else if (strcmp(*argv, "-pass") == 0) {
654: if (--argc < 1)
655: goto bad;
656: passarg = *(++argv);
657: } else if (strcmp(*argv, "-dhparam") == 0) {
658: if (--argc < 1)
659: goto bad;
660: dhfile = *(++argv);
1.33 jsing 661: } else if (strcmp(*argv, "-named_curve") == 0) {
1.1 jsing 662: if (--argc < 1)
663: goto bad;
664: named_curve = *(++argv);
1.33 jsing 665: } else if (strcmp(*argv, "-dcertform") == 0) {
1.1 jsing 666: if (--argc < 1)
667: goto bad;
668: s_dcert_format = str2fmt(*(++argv));
669: } else if (strcmp(*argv, "-dcert") == 0) {
670: if (--argc < 1)
671: goto bad;
672: s_dcert_file = *(++argv);
673: } else if (strcmp(*argv, "-dkeyform") == 0) {
674: if (--argc < 1)
675: goto bad;
676: s_dkey_format = str2fmt(*(++argv));
677: } else if (strcmp(*argv, "-dpass") == 0) {
678: if (--argc < 1)
679: goto bad;
680: dpassarg = *(++argv);
681: } else if (strcmp(*argv, "-dkey") == 0) {
682: if (--argc < 1)
683: goto bad;
684: s_dkey_file = *(++argv);
685: } else if (strcmp(*argv, "-nocert") == 0) {
686: nocert = 1;
687: } else if (strcmp(*argv, "-CApath") == 0) {
688: if (--argc < 1)
689: goto bad;
690: CApath = *(++argv);
691: } else if (strcmp(*argv, "-no_cache") == 0)
692: no_cache = 1;
693: else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm)) {
694: if (badarg)
695: goto bad;
696: continue;
697: } else if (strcmp(*argv, "-verify_return_error") == 0)
698: verify_return_error = 1;
699: else if (strcmp(*argv, "-serverpref") == 0) {
700: off |= SSL_OP_CIPHER_SERVER_PREFERENCE;
701: } else if (strcmp(*argv, "-legacy_renegotiation") == 0)
702: ; /* no-op */
703: else if (strcmp(*argv, "-cipher") == 0) {
704: if (--argc < 1)
705: goto bad;
706: cipher = *(++argv);
707: } else if (strcmp(*argv, "-CAfile") == 0) {
708: if (--argc < 1)
709: goto bad;
710: CAfile = *(++argv);
711: }
712: else if (strcmp(*argv, "-nbio") == 0) {
713: s_nbio = 1;
714: }
715: else if (strcmp(*argv, "-nbio_test") == 0) {
716: s_nbio = 1;
717: s_nbio_test = 1;
718: } else if (strcmp(*argv, "-debug") == 0) {
719: s_debug = 1;
720: }
721: else if (strcmp(*argv, "-tlsextdebug") == 0)
722: s_tlsextdebug = 1;
723: else if (strcmp(*argv, "-status") == 0)
724: s_tlsextstatus = 1;
725: else if (strcmp(*argv, "-status_verbose") == 0) {
726: s_tlsextstatus = 1;
727: tlscstatp.verbose = 1;
728: } else if (!strcmp(*argv, "-status_timeout")) {
729: s_tlsextstatus = 1;
730: if (--argc < 1)
731: goto bad;
732: tlscstatp.timeout = strtonum(*(++argv), 0, INT_MAX, &errstr);
733: if (errstr)
734: goto bad;
735: } else if (!strcmp(*argv, "-status_url")) {
736: s_tlsextstatus = 1;
737: if (--argc < 1)
738: goto bad;
739: if (!OCSP_parse_url(*(++argv),
740: &tlscstatp.host,
741: &tlscstatp.port,
742: &tlscstatp.path,
743: &tlscstatp.use_ssl)) {
744: BIO_printf(bio_err, "Error parsing URL\n");
745: goto bad;
746: }
747: }
748: else if (strcmp(*argv, "-msg") == 0) {
749: s_msg = 1;
750: } else if (strcmp(*argv, "-state") == 0) {
751: state = 1;
752: } else if (strcmp(*argv, "-crlf") == 0) {
753: s_crlf = 1;
754: } else if (strcmp(*argv, "-quiet") == 0) {
755: s_quiet = 1;
756: } else if (strcmp(*argv, "-bugs") == 0) {
757: bugs = 1;
758: } else if (strcmp(*argv, "-no_tmp_rsa") == 0) {
1.5 jsing 759: /* No-op. */
1.1 jsing 760: } else if (strcmp(*argv, "-no_dhe") == 0) {
761: no_dhe = 1;
762: } else if (strcmp(*argv, "-no_ecdhe") == 0) {
763: no_ecdhe = 1;
1.5 jsing 764: } else if (strcmp(*argv, "-www") == 0) {
1.1 jsing 765: www = 1;
766: } else if (strcmp(*argv, "-WWW") == 0) {
767: www = 2;
768: } else if (strcmp(*argv, "-HTTP") == 0) {
769: www = 3;
770: } else if (strcmp(*argv, "-no_ssl2") == 0) {
771: off |= SSL_OP_NO_SSLv2;
772: } else if (strcmp(*argv, "-no_ssl3") == 0) {
773: off |= SSL_OP_NO_SSLv3;
774: } else if (strcmp(*argv, "-no_tls1") == 0) {
775: off |= SSL_OP_NO_TLSv1;
776: } else if (strcmp(*argv, "-no_tls1_1") == 0) {
777: off |= SSL_OP_NO_TLSv1_1;
778: } else if (strcmp(*argv, "-no_tls1_2") == 0) {
779: off |= SSL_OP_NO_TLSv1_2;
1.35 ! inoguchi 780: } else if (strcmp(*argv, "-no_tls1_3") == 0) {
! 781: off |= SSL_OP_NO_TLSv1_3;
1.1 jsing 782: } else if (strcmp(*argv, "-no_comp") == 0) {
783: off |= SSL_OP_NO_COMPRESSION;
1.13 doug 784: } else if (strcmp(*argv, "-no_ticket") == 0) {
1.1 jsing 785: off |= SSL_OP_NO_TICKET;
786: } else if (strcmp(*argv, "-tls1") == 0) {
1.35 ! inoguchi 787: min_version = TLS1_VERSION;
! 788: max_version = TLS1_VERSION;
1.1 jsing 789: } else if (strcmp(*argv, "-tls1_1") == 0) {
1.35 ! inoguchi 790: min_version = TLS1_1_VERSION;
! 791: max_version = TLS1_1_VERSION;
1.1 jsing 792: } else if (strcmp(*argv, "-tls1_2") == 0) {
1.35 ! inoguchi 793: min_version = TLS1_2_VERSION;
! 794: max_version = TLS1_2_VERSION;
! 795: } else if (strcmp(*argv, "-tls1_3") == 0) {
! 796: min_version = TLS1_3_VERSION;
! 797: max_version = TLS1_3_VERSION;
1.1 jsing 798: }
799: #ifndef OPENSSL_NO_DTLS1
800: else if (strcmp(*argv, "-dtls1") == 0) {
1.35 ! inoguchi 801: meth = DTLS_server_method();
1.1 jsing 802: socket_type = SOCK_DGRAM;
803: } else if (strcmp(*argv, "-timeout") == 0)
804: enable_timeouts = 1;
805: else if (strcmp(*argv, "-mtu") == 0) {
806: if (--argc < 1)
807: goto bad;
808: socket_mtu = strtonum(*(++argv), 0, LONG_MAX, &errstr);
809: if (errstr)
810: goto bad;
811: } else if (strcmp(*argv, "-chain") == 0)
812: cert_chain = 1;
813: #endif
814: else if (strcmp(*argv, "-id_prefix") == 0) {
815: if (--argc < 1)
816: goto bad;
817: session_id_prefix = *(++argv);
818: }
819: else if (strcmp(*argv, "-servername") == 0) {
820: if (--argc < 1)
821: goto bad;
822: tlsextcbp.servername = *(++argv);
823: } else if (strcmp(*argv, "-servername_fatal") == 0) {
824: tlsextcbp.extension_error = SSL_TLSEXT_ERR_ALERT_FATAL;
825: } else if (strcmp(*argv, "-cert2") == 0) {
826: if (--argc < 1)
827: goto bad;
828: s_cert_file2 = *(++argv);
829: } else if (strcmp(*argv, "-key2") == 0) {
830: if (--argc < 1)
831: goto bad;
832: s_key_file2 = *(++argv);
1.27 jsing 833: } else if (strcmp(*argv, "-nextprotoneg") == 0) {
834: /* Ignored. */
1.1 jsing 835: if (--argc < 1)
836: goto bad;
1.27 jsing 837: ++argv;
838: } else if (strcmp(*argv,"-alpn") == 0) {
1.8 jsing 839: if (--argc < 1)
840: goto bad;
841: alpn_in = *(++argv);
1.33 jsing 842: } else if (strcmp(*argv, "-groups") == 0) {
843: if (--argc < 1)
844: goto bad;
845: groups_in = *(++argv);
1.8 jsing 846: }
1.1 jsing 847: #ifndef OPENSSL_NO_SRTP
848: else if (strcmp(*argv, "-use_srtp") == 0) {
849: if (--argc < 1)
850: goto bad;
851: srtp_profiles = *(++argv);
852: }
853: #endif
854: else if (strcmp(*argv, "-keymatexport") == 0) {
855: if (--argc < 1)
856: goto bad;
857: keymatexportlabel = *(++argv);
858: } else if (strcmp(*argv, "-keymatexportlen") == 0) {
859: if (--argc < 1)
860: goto bad;
861: keymatexportlen = strtonum(*(++argv), 1, INT_MAX, &errstr);
862: if (errstr)
863: goto bad;
864: } else {
865: BIO_printf(bio_err, "unknown option %s\n", *argv);
866: badop = 1;
867: break;
868: }
869: argc--;
870: argv++;
871: }
872: if (badop) {
1.30 jsing 873: bad:
1.1 jsing 874: if (errstr)
875: BIO_printf(bio_err, "invalid argument %s: %s\n",
876: *argv, errstr);
877: else
878: sv_usage();
879: goto end;
880: }
881:
882: if (!app_passwd(bio_err, passarg, dpassarg, &pass, &dpass)) {
883: BIO_printf(bio_err, "Error getting password\n");
884: goto end;
885: }
886: if (s_key_file == NULL)
887: s_key_file = s_cert_file;
888: if (s_key_file2 == NULL)
889: s_key_file2 = s_cert_file2;
890:
891: if (nocert == 0) {
1.17 bcook 892: s_key = load_key(bio_err, s_key_file, s_key_format, 0, pass,
1.1 jsing 893: "server certificate private key file");
894: if (!s_key) {
895: ERR_print_errors(bio_err);
896: goto end;
897: }
898: s_cert = load_cert(bio_err, s_cert_file, s_cert_format,
1.17 bcook 899: NULL, "server certificate file");
1.1 jsing 900:
901: if (!s_cert) {
902: ERR_print_errors(bio_err);
903: goto end;
904: }
905: if (tlsextcbp.servername) {
1.17 bcook 906: s_key2 = load_key(bio_err, s_key_file2, s_key_format, 0, pass,
1.1 jsing 907: "second server certificate private key file");
908: if (!s_key2) {
909: ERR_print_errors(bio_err);
910: goto end;
911: }
912: s_cert2 = load_cert(bio_err, s_cert_file2, s_cert_format,
1.17 bcook 913: NULL, "second server certificate file");
1.1 jsing 914:
915: if (!s_cert2) {
916: ERR_print_errors(bio_err);
917: goto end;
918: }
919: }
920: }
1.8 jsing 921: alpn_ctx.data = NULL;
922: if (alpn_in) {
923: unsigned short len;
924: alpn_ctx.data = next_protos_parse(&len, alpn_in);
925: if (alpn_ctx.data == NULL)
926: goto end;
927: alpn_ctx.len = len;
928: }
1.1 jsing 929:
930: if (s_dcert_file) {
931:
932: if (s_dkey_file == NULL)
933: s_dkey_file = s_dcert_file;
934:
935: s_dkey = load_key(bio_err, s_dkey_file, s_dkey_format,
1.17 bcook 936: 0, dpass, "second certificate private key file");
1.1 jsing 937: if (!s_dkey) {
938: ERR_print_errors(bio_err);
939: goto end;
940: }
941: s_dcert = load_cert(bio_err, s_dcert_file, s_dcert_format,
1.17 bcook 942: NULL, "second server certificate file");
1.1 jsing 943:
944: if (!s_dcert) {
945: ERR_print_errors(bio_err);
946: goto end;
947: }
948: }
949: if (bio_s_out == NULL) {
950: if (s_quiet && !s_debug && !s_msg) {
951: bio_s_out = BIO_new(BIO_s_null());
952: } else {
953: if (bio_s_out == NULL)
954: bio_s_out = BIO_new_fp(stdout, BIO_NOCLOSE);
955: }
956: }
1.8 jsing 957: if (nocert) {
1.1 jsing 958: s_cert_file = NULL;
959: s_key_file = NULL;
960: s_dcert_file = NULL;
961: s_dkey_file = NULL;
962: s_cert_file2 = NULL;
963: s_key_file2 = NULL;
964: }
965: ctx = SSL_CTX_new(meth);
966: if (ctx == NULL) {
967: ERR_print_errors(bio_err);
968: goto end;
969: }
1.35 ! inoguchi 970:
! 971: if (!SSL_CTX_set_min_proto_version(ctx, min_version))
! 972: goto end;
! 973: if (!SSL_CTX_set_max_proto_version(ctx, max_version))
! 974: goto end;
! 975:
1.1 jsing 976: if (session_id_prefix) {
977: if (strlen(session_id_prefix) >= 32)
978: BIO_printf(bio_err,
979: "warning: id_prefix is too long, only one new session will be possible\n");
980: else if (strlen(session_id_prefix) >= 16)
981: BIO_printf(bio_err,
982: "warning: id_prefix is too long if you use SSLv2\n");
983: if (!SSL_CTX_set_generate_session_id(ctx, generate_session_id)) {
984: BIO_printf(bio_err, "error setting 'id_prefix'\n");
985: ERR_print_errors(bio_err);
986: goto end;
987: }
988: BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
989: }
990: SSL_CTX_set_quiet_shutdown(ctx, 1);
991: if (bugs)
992: SSL_CTX_set_options(ctx, SSL_OP_ALL);
993: SSL_CTX_set_options(ctx, off);
994: /*
995: * DTLS: partial reads end up discarding unread UDP bytes :-( Setting
996: * read ahead solves this problem.
997: */
998: if (socket_type == SOCK_DGRAM)
999: SSL_CTX_set_read_ahead(ctx, 1);
1000:
1001: if (state)
1002: SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
1003: if (no_cache)
1004: SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
1005: else
1006: SSL_CTX_sess_set_cache_size(ctx, 128);
1007:
1008: #ifndef OPENSSL_NO_SRTP
1009: if (srtp_profiles != NULL)
1010: SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles);
1011: #endif
1012:
1013:
1014: if ((!SSL_CTX_load_verify_locations(ctx, CAfile, CApath)) ||
1015: (!SSL_CTX_set_default_verify_paths(ctx))) {
1016: /* BIO_printf(bio_err,"X509_load_verify_locations\n"); */
1017: ERR_print_errors(bio_err);
1018: /* goto end; */
1019: }
1020: if (vpm)
1021: SSL_CTX_set1_param(ctx, vpm);
1022:
1023: if (s_cert2) {
1024: ctx2 = SSL_CTX_new(meth);
1025: if (ctx2 == NULL) {
1026: ERR_print_errors(bio_err);
1027: goto end;
1028: }
1.35 ! inoguchi 1029:
! 1030: if (!SSL_CTX_set_min_proto_version(ctx2, min_version))
! 1031: goto end;
! 1032: if (!SSL_CTX_set_max_proto_version(ctx2, max_version))
! 1033: goto end;
1.1 jsing 1034: }
1035: if (ctx2) {
1036: BIO_printf(bio_s_out, "Setting secondary ctx parameters\n");
1037:
1038: if (session_id_prefix) {
1039: if (strlen(session_id_prefix) >= 32)
1040: BIO_printf(bio_err,
1041: "warning: id_prefix is too long, only one new session will be possible\n");
1042: else if (strlen(session_id_prefix) >= 16)
1043: BIO_printf(bio_err,
1044: "warning: id_prefix is too long if you use SSLv2\n");
1045: if (!SSL_CTX_set_generate_session_id(ctx2, generate_session_id)) {
1046: BIO_printf(bio_err, "error setting 'id_prefix'\n");
1047: ERR_print_errors(bio_err);
1048: goto end;
1049: }
1050: BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
1051: }
1052: SSL_CTX_set_quiet_shutdown(ctx2, 1);
1053: if (bugs)
1054: SSL_CTX_set_options(ctx2, SSL_OP_ALL);
1055: SSL_CTX_set_options(ctx2, off);
1056: /*
1057: * DTLS: partial reads end up discarding unread UDP bytes :-(
1058: * Setting read ahead solves this problem.
1059: */
1060: if (socket_type == SOCK_DGRAM)
1061: SSL_CTX_set_read_ahead(ctx2, 1);
1062:
1063: if (state)
1064: SSL_CTX_set_info_callback(ctx2, apps_ssl_info_callback);
1065:
1066: if (no_cache)
1067: SSL_CTX_set_session_cache_mode(ctx2, SSL_SESS_CACHE_OFF);
1068: else
1069: SSL_CTX_sess_set_cache_size(ctx2, 128);
1070:
1071: if ((!SSL_CTX_load_verify_locations(ctx2, CAfile, CApath)) ||
1072: (!SSL_CTX_set_default_verify_paths(ctx2))) {
1073: ERR_print_errors(bio_err);
1074: }
1075: if (vpm)
1076: SSL_CTX_set1_param(ctx2, vpm);
1077: }
1.8 jsing 1078: if (alpn_ctx.data)
1079: SSL_CTX_set_alpn_select_cb(ctx, alpn_cb, &alpn_ctx);
1.1 jsing 1080:
1.33 jsing 1081: if (groups_in != NULL) {
1082: if (SSL_CTX_set1_groups_list(ctx, groups_in) != 1) {
1083: BIO_printf(bio_err, "Failed to set groups '%s'\n",
1084: groups_in);
1085: goto end;
1086: }
1087: }
1088:
1.1 jsing 1089: #ifndef OPENSSL_NO_DH
1090: if (!no_dhe) {
1091: DH *dh = NULL;
1092:
1093: if (dhfile)
1094: dh = load_dh_param(dhfile);
1095: else if (s_cert_file)
1096: dh = load_dh_param(s_cert_file);
1097:
1.4 jsing 1098: if (dh != NULL)
1.1 jsing 1099: BIO_printf(bio_s_out, "Setting temp DH parameters\n");
1.4 jsing 1100: else
1101: BIO_printf(bio_s_out, "Using auto DH parameters\n");
1102: (void) BIO_flush(bio_s_out);
1103:
1104: if (dh == NULL)
1105: SSL_CTX_set_dh_auto(ctx, 1);
1106: else if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
1107: BIO_printf(bio_err,
1108: "Error setting temp DH parameters\n");
1109: ERR_print_errors(bio_err);
1110: DH_free(dh);
1111: goto end;
1.1 jsing 1112: }
1113:
1114: if (ctx2) {
1115: if (!dhfile) {
1.14 doug 1116: DH *dh2 = NULL;
1117:
1118: if (s_cert_file2 != NULL)
1119: dh2 = load_dh_param(s_cert_file2);
1.1 jsing 1120: if (dh2 != NULL) {
1121: BIO_printf(bio_s_out, "Setting temp DH parameters\n");
1122: (void) BIO_flush(bio_s_out);
1123:
1124: DH_free(dh);
1125: dh = dh2;
1126: }
1127: }
1.4 jsing 1128: if (dh == NULL)
1129: SSL_CTX_set_dh_auto(ctx2, 1);
1130: else if (!SSL_CTX_set_tmp_dh(ctx2, dh)) {
1131: BIO_printf(bio_err,
1132: "Error setting temp DH parameters\n");
1133: ERR_print_errors(bio_err);
1134: DH_free(dh);
1135: goto end;
1136: }
1.1 jsing 1137: }
1138: DH_free(dh);
1139: }
1140: #endif
1141:
1.33 jsing 1142: if (!no_ecdhe && named_curve != NULL) {
1.1 jsing 1143: EC_KEY *ecdh = NULL;
1.33 jsing 1144: int nid;
1.1 jsing 1145:
1.33 jsing 1146: if ((nid = OBJ_sn2nid(named_curve)) == 0) {
1147: BIO_printf(bio_err, "unknown curve name (%s)\n",
1148: named_curve);
1149: goto end;
1.1 jsing 1150: }
1.33 jsing 1151: if ((ecdh = EC_KEY_new_by_curve_name(nid)) == NULL) {
1152: BIO_printf(bio_err, "unable to create curve (%s)\n",
1153: named_curve);
1154: goto end;
1.1 jsing 1155: }
1.33 jsing 1156: BIO_printf(bio_s_out, "Setting temp ECDH parameters\n");
1.1 jsing 1157: (void) BIO_flush(bio_s_out);
1158:
1159: SSL_CTX_set_tmp_ecdh(ctx, ecdh);
1160: if (ctx2)
1161: SSL_CTX_set_tmp_ecdh(ctx2, ecdh);
1162: EC_KEY_free(ecdh);
1163: }
1164:
1165: if (!set_cert_key_stuff(ctx, s_cert, s_key))
1166: goto end;
1167: if (ctx2 && !set_cert_key_stuff(ctx2, s_cert2, s_key2))
1168: goto end;
1169: if (s_dcert != NULL) {
1170: if (!set_cert_key_stuff(ctx, s_dcert, s_dkey))
1171: goto end;
1172: }
1173:
1174: if (cipher != NULL) {
1175: if (!SSL_CTX_set_cipher_list(ctx, cipher)) {
1176: BIO_printf(bio_err, "error setting cipher list\n");
1177: ERR_print_errors(bio_err);
1178: goto end;
1179: }
1180: if (ctx2 && !SSL_CTX_set_cipher_list(ctx2, cipher)) {
1181: BIO_printf(bio_err, "error setting cipher list\n");
1182: ERR_print_errors(bio_err);
1183: goto end;
1184: }
1185: }
1186: SSL_CTX_set_verify(ctx, s_server_verify, verify_callback);
1187: SSL_CTX_set_session_id_context(ctx, (void *) &s_server_session_id_context,
1188: sizeof s_server_session_id_context);
1189:
1190: /* Set DTLS cookie generation and verification callbacks */
1191: SSL_CTX_set_cookie_generate_cb(ctx, generate_cookie_callback);
1192: SSL_CTX_set_cookie_verify_cb(ctx, verify_cookie_callback);
1193:
1194: if (ctx2) {
1195: SSL_CTX_set_verify(ctx2, s_server_verify, verify_callback);
1196: SSL_CTX_set_session_id_context(ctx2, (void *) &s_server_session_id_context,
1197: sizeof s_server_session_id_context);
1198:
1199: tlsextcbp.biodebug = bio_s_out;
1200: SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb);
1201: SSL_CTX_set_tlsext_servername_arg(ctx2, &tlsextcbp);
1202: SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
1203: SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
1204: }
1205:
1206: if (CAfile != NULL) {
1207: SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(CAfile));
1208: if (ctx2)
1209: SSL_CTX_set_client_CA_list(ctx2, SSL_load_client_CA_file(CAfile));
1210: }
1211: BIO_printf(bio_s_out, "ACCEPT\n");
1212: (void) BIO_flush(bio_s_out);
1213: if (www)
1214: do_server(port, socket_type, &accept_socket, www_body, context);
1215: else
1216: do_server(port, socket_type, &accept_socket, sv_body, context);
1217: print_stats(bio_s_out, ctx);
1218: ret = 0;
1.30 jsing 1219: end:
1.29 jsing 1220: SSL_CTX_free(ctx);
1221: X509_free(s_cert);
1222: X509_free(s_dcert);
1223: EVP_PKEY_free(s_key);
1224: EVP_PKEY_free(s_dkey);
1.1 jsing 1225: free(pass);
1226: free(dpass);
1.29 jsing 1227: X509_VERIFY_PARAM_free(vpm);
1.1 jsing 1228: free(tlscstatp.host);
1229: free(tlscstatp.port);
1230: free(tlscstatp.path);
1.29 jsing 1231: SSL_CTX_free(ctx2);
1232: X509_free(s_cert2);
1233: EVP_PKEY_free(s_key2);
1.8 jsing 1234: free(alpn_ctx.data);
1.1 jsing 1235: if (bio_s_out != NULL) {
1236: BIO_free(bio_s_out);
1237: bio_s_out = NULL;
1238: }
1239:
1240: return (ret);
1241: }
1242:
1243: static void
1244: print_stats(BIO * bio, SSL_CTX * ssl_ctx)
1245: {
1246: BIO_printf(bio, "%4ld items in the session cache\n",
1247: SSL_CTX_sess_number(ssl_ctx));
1248: BIO_printf(bio, "%4ld client connects (SSL_connect())\n",
1249: SSL_CTX_sess_connect(ssl_ctx));
1250: BIO_printf(bio, "%4ld client renegotiates (SSL_connect())\n",
1251: SSL_CTX_sess_connect_renegotiate(ssl_ctx));
1252: BIO_printf(bio, "%4ld client connects that finished\n",
1253: SSL_CTX_sess_connect_good(ssl_ctx));
1254: BIO_printf(bio, "%4ld server accepts (SSL_accept())\n",
1255: SSL_CTX_sess_accept(ssl_ctx));
1256: BIO_printf(bio, "%4ld server renegotiates (SSL_accept())\n",
1257: SSL_CTX_sess_accept_renegotiate(ssl_ctx));
1258: BIO_printf(bio, "%4ld server accepts that finished\n",
1259: SSL_CTX_sess_accept_good(ssl_ctx));
1260: BIO_printf(bio, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx));
1261: BIO_printf(bio, "%4ld session cache misses\n", SSL_CTX_sess_misses(ssl_ctx));
1262: BIO_printf(bio, "%4ld session cache timeouts\n", SSL_CTX_sess_timeouts(ssl_ctx));
1263: BIO_printf(bio, "%4ld callback cache hits\n", SSL_CTX_sess_cb_hits(ssl_ctx));
1264: BIO_printf(bio, "%4ld cache full overflows (%ld allowed)\n",
1265: SSL_CTX_sess_cache_full(ssl_ctx),
1266: SSL_CTX_sess_get_cache_size(ssl_ctx));
1267: }
1268:
1269: static int
1270: sv_body(char *hostname, int s, unsigned char *context)
1271: {
1272: char *buf = NULL;
1.7 deraadt 1273: int ret = 1;
1.1 jsing 1274: int k, i;
1275: unsigned long l;
1276: SSL *con = NULL;
1277: BIO *sbio;
1278: struct timeval timeout;
1279:
1280: if ((buf = malloc(bufsize)) == NULL) {
1281: BIO_printf(bio_err, "out of memory\n");
1282: goto err;
1283: }
1284: if (s_nbio) {
1285: if (!s_quiet)
1286: BIO_printf(bio_err, "turning on non blocking io\n");
1.2 bcook 1287: if (!BIO_socket_nbio(s, 1))
1.1 jsing 1288: ERR_print_errors(bio_err);
1289: }
1290:
1291: if (con == NULL) {
1292: con = SSL_new(ctx);
1293: if (s_tlsextdebug) {
1294: SSL_set_tlsext_debug_callback(con, tlsext_cb);
1295: SSL_set_tlsext_debug_arg(con, bio_s_out);
1296: }
1297: if (s_tlsextstatus) {
1298: SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb);
1299: tlscstatp.err = bio_err;
1300: SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp);
1301: }
1302: if (context)
1303: SSL_set_session_id_context(con, context,
1304: strlen((char *) context));
1305: }
1306: SSL_clear(con);
1307:
1308: if (SSL_version(con) == DTLS1_VERSION) {
1309:
1310: sbio = BIO_new_dgram(s, BIO_NOCLOSE);
1311:
1312: if (enable_timeouts) {
1313: timeout.tv_sec = 0;
1314: timeout.tv_usec = DGRAM_RCV_TIMEOUT;
1315: BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
1316:
1317: timeout.tv_sec = 0;
1318: timeout.tv_usec = DGRAM_SND_TIMEOUT;
1319: BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
1320: }
1321: if (socket_mtu > 28) {
1322: SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
1323: SSL_set_mtu(con, socket_mtu - 28);
1324: } else
1325: /* want to do MTU discovery */
1326: BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
1327:
1328: /* turn on cookie exchange */
1329: SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE);
1330: } else
1331: sbio = BIO_new_socket(s, BIO_NOCLOSE);
1332:
1333: if (s_nbio_test) {
1334: BIO *test;
1335:
1336: test = BIO_new(BIO_f_nbio_test());
1337: sbio = BIO_push(test, sbio);
1338: }
1339:
1340: SSL_set_bio(con, sbio, sbio);
1341: SSL_set_accept_state(con);
1342: /* SSL_set_fd(con,s); */
1343:
1344: if (s_debug) {
1345: SSL_set_debug(con, 1);
1346: BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
1347: BIO_set_callback_arg(SSL_get_rbio(con), (char *) bio_s_out);
1348: }
1349: if (s_msg) {
1350: SSL_set_msg_callback(con, msg_cb);
1351: SSL_set_msg_callback_arg(con, bio_s_out);
1352: }
1353: if (s_tlsextdebug) {
1354: SSL_set_tlsext_debug_callback(con, tlsext_cb);
1355: SSL_set_tlsext_debug_arg(con, bio_s_out);
1356: }
1357:
1358: for (;;) {
1359: int read_from_terminal;
1360: int read_from_sslcon;
1.7 deraadt 1361: struct pollfd pfd[2];
1362: int ptimeout;
1.1 jsing 1363:
1364: read_from_terminal = 0;
1365: read_from_sslcon = SSL_pending(con);
1366:
1367: if (!read_from_sslcon) {
1.7 deraadt 1368: pfd[0].fd = fileno(stdin);
1369: pfd[0].events = POLLIN;
1370: pfd[1].fd = s;
1371: pfd[1].events = POLLIN;
1372:
1.1 jsing 1373: if ((SSL_version(con) == DTLS1_VERSION) &&
1374: DTLSv1_get_timeout(con, &timeout))
1.7 deraadt 1375: ptimeout = timeout.tv_sec * 1000 +
1376: timeout.tv_usec / 1000;
1.1 jsing 1377: else
1.7 deraadt 1378: ptimeout = -1;
1.1 jsing 1379:
1.7 deraadt 1380: i = poll(pfd, 2, ptimeout);
1.1 jsing 1381:
1382: if ((SSL_version(con) == DTLS1_VERSION) && DTLSv1_handle_timeout(con) > 0) {
1383: BIO_printf(bio_err, "TIMEOUT occured\n");
1384: }
1385: if (i <= 0)
1386: continue;
1.7 deraadt 1387: if (pfd[0].revents) {
1388: if ((pfd[0].revents & (POLLERR|POLLNVAL)))
1389: continue;
1.1 jsing 1390: read_from_terminal = 1;
1.7 deraadt 1391: }
1392: if (pfd[1].revents) {
1393: if ((pfd[1].revents & (POLLERR|POLLNVAL)))
1394: continue;
1.1 jsing 1395: read_from_sslcon = 1;
1.7 deraadt 1396: }
1.1 jsing 1397: }
1398: if (read_from_terminal) {
1399: if (s_crlf) {
1400: int j, lf_num;
1401:
1402: i = read(fileno(stdin), buf, bufsize / 2);
1403: lf_num = 0;
1404: /* both loops are skipped when i <= 0 */
1405: for (j = 0; j < i; j++)
1406: if (buf[j] == '\n')
1407: lf_num++;
1408: for (j = i - 1; j >= 0; j--) {
1409: buf[j + lf_num] = buf[j];
1410: if (buf[j] == '\n') {
1411: lf_num--;
1412: i++;
1413: buf[j + lf_num] = '\r';
1414: }
1415: }
1416: assert(lf_num == 0);
1417: } else
1418: i = read(fileno(stdin), buf, bufsize);
1419: if (!s_quiet) {
1420: if ((i <= 0) || (buf[0] == 'Q')) {
1421: BIO_printf(bio_s_out, "DONE\n");
1422: shutdown(s, SHUT_RD);
1423: close(s);
1424: close_accept_socket();
1425: ret = -11;
1426: goto err;
1427: }
1428: if ((i <= 0) || (buf[0] == 'q')) {
1429: BIO_printf(bio_s_out, "DONE\n");
1430: if (SSL_version(con) != DTLS1_VERSION) {
1431: shutdown(s, SHUT_RD);
1432: close(s);
1433: }
1434: /*
1435: * close_accept_socket(); ret= -11;
1436: */
1437: goto err;
1438: }
1439: if ((buf[0] == 'r') &&
1440: ((buf[1] == '\n') || (buf[1] == '\r'))) {
1441: SSL_renegotiate(con);
1442: i = SSL_do_handshake(con);
1443: printf("SSL_do_handshake -> %d\n", i);
1444: i = 0; /* 13; */
1445: continue;
1446: /*
1447: * RE-NEGOTIATE\n");
1448: */
1449: }
1450: if ((buf[0] == 'R') &&
1451: ((buf[1] == '\n') || (buf[1] == '\r'))) {
1452: SSL_set_verify(con,
1453: SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, NULL);
1454: SSL_renegotiate(con);
1455: i = SSL_do_handshake(con);
1456: printf("SSL_do_handshake -> %d\n", i);
1457: i = 0; /* 13; */
1458: continue;
1459: /*
1460: * RE-NEGOTIATE asking for client
1461: * cert\n");
1462: */
1463: }
1464: if (buf[0] == 'P') {
1465: static const char *str = "Lets print some clear text\n";
1466: BIO_write(SSL_get_wbio(con), str, strlen(str));
1467: }
1468: if (buf[0] == 'S') {
1469: print_stats(bio_s_out, SSL_get_SSL_CTX(con));
1470: }
1471: }
1472: l = k = 0;
1473: for (;;) {
1474: /* should do a select for the write */
1475: #ifdef RENEG
1476: {
1477: static count = 0;
1478: if (++count == 100) {
1479: count = 0;
1480: SSL_renegotiate(con);
1481: }
1482: }
1483: #endif
1484: k = SSL_write(con, &(buf[l]), (unsigned int) i);
1485: switch (SSL_get_error(con, k)) {
1486: case SSL_ERROR_NONE:
1487: break;
1488: case SSL_ERROR_WANT_WRITE:
1489: case SSL_ERROR_WANT_READ:
1490: case SSL_ERROR_WANT_X509_LOOKUP:
1491: BIO_printf(bio_s_out, "Write BLOCK\n");
1492: break;
1493: case SSL_ERROR_SYSCALL:
1494: case SSL_ERROR_SSL:
1495: BIO_printf(bio_s_out, "ERROR\n");
1496: ERR_print_errors(bio_err);
1497: ret = 1;
1498: goto err;
1499: /* break; */
1500: case SSL_ERROR_ZERO_RETURN:
1501: BIO_printf(bio_s_out, "DONE\n");
1502: ret = 1;
1503: goto err;
1504: }
1505: l += k;
1506: i -= k;
1507: if (i <= 0)
1508: break;
1509: }
1510: }
1511: if (read_from_sslcon) {
1512: if (!SSL_is_init_finished(con)) {
1513: i = init_ssl_connection(con);
1514:
1515: if (i < 0) {
1516: ret = 0;
1517: goto err;
1518: } else if (i == 0) {
1519: ret = 1;
1520: goto err;
1521: }
1522: } else {
1523: again:
1524: i = SSL_read(con, (char *) buf, bufsize);
1525: switch (SSL_get_error(con, i)) {
1526: case SSL_ERROR_NONE: {
1527: int len, n;
1528: for (len = 0; len < i;) {
1529: do {
1530: n = write(fileno(stdout), buf + len, i - len);
1531: } while (n == -1 && errno == EINTR);
1532:
1.31 deraadt 1533: if (n == -1) {
1.1 jsing 1534: BIO_printf(bio_s_out, "ERROR\n");
1535: goto err;
1536: }
1537: len += n;
1538: }
1539: }
1540: if (SSL_pending(con))
1541: goto again;
1542: break;
1543: case SSL_ERROR_WANT_WRITE:
1544: case SSL_ERROR_WANT_READ:
1545: BIO_printf(bio_s_out, "Read BLOCK\n");
1546: break;
1547: case SSL_ERROR_SYSCALL:
1548: case SSL_ERROR_SSL:
1549: BIO_printf(bio_s_out, "ERROR\n");
1550: ERR_print_errors(bio_err);
1551: ret = 1;
1552: goto err;
1553: case SSL_ERROR_ZERO_RETURN:
1554: BIO_printf(bio_s_out, "DONE\n");
1555: ret = 1;
1556: goto err;
1557: }
1558: }
1559: }
1560: }
1.30 jsing 1561: err:
1.1 jsing 1562: if (con != NULL) {
1563: BIO_printf(bio_s_out, "shutting down SSL\n");
1564: SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
1565: SSL_free(con);
1566: }
1567: BIO_printf(bio_s_out, "CONNECTION CLOSED\n");
1.26 deraadt 1568: freezero(buf, bufsize);
1.1 jsing 1569: if (ret >= 0)
1570: BIO_printf(bio_s_out, "ACCEPT\n");
1571: return (ret);
1572: }
1573:
1574: static void
1575: close_accept_socket(void)
1576: {
1577: BIO_printf(bio_err, "shutdown accept socket\n");
1578: if (accept_socket >= 0) {
1579: shutdown(accept_socket, SHUT_RDWR);
1580: close(accept_socket);
1581: }
1582: }
1583:
1584: static int
1585: init_ssl_connection(SSL * con)
1586: {
1587: int i;
1588: const char *str;
1589: X509 *peer;
1590: long verify_error;
1591: char buf[BUFSIZ];
1592: unsigned char *exportedkeymat;
1593:
1594: i = SSL_accept(con);
1595: if (i <= 0) {
1596: if (BIO_sock_should_retry(i)) {
1597: BIO_printf(bio_s_out, "DELAY\n");
1598: return (1);
1599: }
1600: BIO_printf(bio_err, "ERROR\n");
1601: verify_error = SSL_get_verify_result(con);
1602: if (verify_error != X509_V_OK) {
1603: BIO_printf(bio_err, "verify error:%s\n",
1604: X509_verify_cert_error_string(verify_error));
1605: } else
1606: ERR_print_errors(bio_err);
1607: return (0);
1608: }
1609: PEM_write_bio_SSL_SESSION(bio_s_out, SSL_get_session(con));
1610:
1611: peer = SSL_get_peer_certificate(con);
1612: if (peer != NULL) {
1613: BIO_printf(bio_s_out, "Client certificate\n");
1614: PEM_write_bio_X509(bio_s_out, peer);
1615: X509_NAME_oneline(X509_get_subject_name(peer), buf, sizeof buf);
1616: BIO_printf(bio_s_out, "subject=%s\n", buf);
1617: X509_NAME_oneline(X509_get_issuer_name(peer), buf, sizeof buf);
1618: BIO_printf(bio_s_out, "issuer=%s\n", buf);
1619: X509_free(peer);
1620: }
1621: if (SSL_get_shared_ciphers(con, buf, sizeof buf) != NULL)
1622: BIO_printf(bio_s_out, "Shared ciphers:%s\n", buf);
1623: str = SSL_CIPHER_get_name(SSL_get_current_cipher(con));
1624: BIO_printf(bio_s_out, "CIPHER is %s\n", (str != NULL) ? str : "(NONE)");
1625:
1626: #ifndef OPENSSL_NO_SRTP
1627: {
1628: SRTP_PROTECTION_PROFILE *srtp_profile
1629: = SSL_get_selected_srtp_profile(con);
1630:
1631: if (srtp_profile)
1632: BIO_printf(bio_s_out, "SRTP Extension negotiated, profile=%s\n",
1633: srtp_profile->name);
1634: }
1635: #endif
1636: if (SSL_cache_hit(con))
1637: BIO_printf(bio_s_out, "Reused session-id\n");
1638: BIO_printf(bio_s_out, "Secure Renegotiation IS%s supported\n",
1639: SSL_get_secure_renegotiation_support(con) ? "" : " NOT");
1640: if (keymatexportlabel != NULL) {
1641: BIO_printf(bio_s_out, "Keying material exporter:\n");
1642: BIO_printf(bio_s_out, " Label: '%s'\n", keymatexportlabel);
1643: BIO_printf(bio_s_out, " Length: %i bytes\n",
1644: keymatexportlen);
1645: exportedkeymat = malloc(keymatexportlen);
1646: if (exportedkeymat != NULL) {
1647: if (!SSL_export_keying_material(con, exportedkeymat,
1648: keymatexportlen,
1649: keymatexportlabel,
1650: strlen(keymatexportlabel),
1651: NULL, 0, 0)) {
1652: BIO_printf(bio_s_out, " Error\n");
1653: } else {
1654: BIO_printf(bio_s_out, " Keying material: ");
1655: for (i = 0; i < keymatexportlen; i++)
1656: BIO_printf(bio_s_out, "%02X",
1657: exportedkeymat[i]);
1658: BIO_printf(bio_s_out, "\n");
1659: }
1660: free(exportedkeymat);
1661: }
1662: }
1663: return (1);
1664: }
1665:
1666: #ifndef OPENSSL_NO_DH
1667: static DH *
1668: load_dh_param(const char *dhfile)
1669: {
1670: DH *ret = NULL;
1671: BIO *bio;
1672:
1673: if ((bio = BIO_new_file(dhfile, "r")) == NULL)
1674: goto err;
1675: ret = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
1.30 jsing 1676: err:
1.1 jsing 1677: BIO_free(bio);
1678: return (ret);
1679: }
1680: #endif
1681:
1682: static int
1683: www_body(char *hostname, int s, unsigned char *context)
1684: {
1685: char *buf = NULL;
1686: int ret = 1;
1687: int i, j, k, dot;
1688: SSL *con;
1689: const SSL_CIPHER *c;
1690: BIO *io, *ssl_bio, *sbio;
1691:
1692: buf = malloc(bufsize);
1693: if (buf == NULL)
1694: return (0);
1695: io = BIO_new(BIO_f_buffer());
1696: ssl_bio = BIO_new(BIO_f_ssl());
1697: if ((io == NULL) || (ssl_bio == NULL))
1698: goto err;
1699:
1700: if (s_nbio) {
1701: if (!s_quiet)
1702: BIO_printf(bio_err, "turning on non blocking io\n");
1.2 bcook 1703: if (!BIO_socket_nbio(s, 1))
1.1 jsing 1704: ERR_print_errors(bio_err);
1705: }
1706:
1707: /* lets make the output buffer a reasonable size */
1708: if (!BIO_set_write_buffer_size(io, bufsize))
1709: goto err;
1710:
1711: if ((con = SSL_new(ctx)) == NULL)
1712: goto err;
1713: if (s_tlsextdebug) {
1714: SSL_set_tlsext_debug_callback(con, tlsext_cb);
1715: SSL_set_tlsext_debug_arg(con, bio_s_out);
1716: }
1717: if (context)
1718: SSL_set_session_id_context(con, context,
1719: strlen((char *) context));
1720:
1721: sbio = BIO_new_socket(s, BIO_NOCLOSE);
1722: if (s_nbio_test) {
1723: BIO *test;
1724:
1725: test = BIO_new(BIO_f_nbio_test());
1726: sbio = BIO_push(test, sbio);
1727: }
1728: SSL_set_bio(con, sbio, sbio);
1729: SSL_set_accept_state(con);
1730:
1731: /* SSL_set_fd(con,s); */
1732: BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
1733: BIO_push(io, ssl_bio);
1734:
1735: if (s_debug) {
1736: SSL_set_debug(con, 1);
1737: BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
1738: BIO_set_callback_arg(SSL_get_rbio(con), (char *) bio_s_out);
1739: }
1740: if (s_msg) {
1741: SSL_set_msg_callback(con, msg_cb);
1742: SSL_set_msg_callback_arg(con, bio_s_out);
1743: }
1744: for (;;) {
1745: i = BIO_gets(io, buf, bufsize - 1);
1746: if (i < 0) { /* error */
1747: if (!BIO_should_retry(io)) {
1748: if (!s_quiet)
1749: ERR_print_errors(bio_err);
1750: goto err;
1751: } else {
1.34 beck 1752: if (s_debug) {
1753: BIO_printf(bio_s_out, "read R BLOCK\n");
1754: sleep(1);
1755: }
1.1 jsing 1756: continue;
1757: }
1758: } else if (i == 0) { /* end of input */
1759: ret = 1;
1760: goto end;
1761: }
1762: /* else we have data */
1763: if (((www == 1) && (strncmp("GET ", buf, 4) == 0)) ||
1764: ((www == 2) && (strncmp("GET /stats ", buf, 11) == 0))) {
1765: char *p;
1766: X509 *peer;
1767: STACK_OF(SSL_CIPHER) * sk;
1768: static const char *space = " ";
1769:
1770: BIO_puts(io, "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
1771: BIO_puts(io, "<HTML><BODY BGCOLOR=\"#ffffff\">\n");
1772: BIO_puts(io, "<pre>\n");
1773: /* BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/
1774: BIO_puts(io, "\n");
1775: for (i = 0; i < local_argc; i++) {
1776: BIO_puts(io, local_argv[i]);
1777: BIO_write(io, " ", 1);
1778: }
1779: BIO_puts(io, "\n");
1780:
1781: BIO_printf(io,
1782: "Secure Renegotiation IS%s supported\n",
1783: SSL_get_secure_renegotiation_support(con) ?
1784: "" : " NOT");
1785:
1786: /*
1787: * The following is evil and should not really be
1788: * done
1789: */
1790: BIO_printf(io, "Ciphers supported in s_server binary\n");
1791: sk = SSL_get_ciphers(con);
1792: j = sk_SSL_CIPHER_num(sk);
1793: for (i = 0; i < j; i++) {
1794: c = sk_SSL_CIPHER_value(sk, i);
1795: BIO_printf(io, "%-11s:%-25s",
1796: SSL_CIPHER_get_version(c),
1797: SSL_CIPHER_get_name(c));
1798: if ((((i + 1) % 2) == 0) && (i + 1 != j))
1799: BIO_puts(io, "\n");
1800: }
1801: BIO_puts(io, "\n");
1802: p = SSL_get_shared_ciphers(con, buf, bufsize);
1803: if (p != NULL) {
1804: BIO_printf(io, "---\nCiphers common between both SSL end points:\n");
1805: j = i = 0;
1806: while (*p) {
1807: if (*p == ':') {
1808: BIO_write(io, space, 26 - j);
1809: i++;
1810: j = 0;
1811: BIO_write(io, ((i % 3) ? " " : "\n"), 1);
1812: } else {
1813: BIO_write(io, p, 1);
1814: j++;
1815: }
1816: p++;
1817: }
1818: BIO_puts(io, "\n");
1819: }
1820: BIO_printf(io, (SSL_cache_hit(con)
1821: ? "---\nReused, "
1822: : "---\nNew, "));
1823: c = SSL_get_current_cipher(con);
1824: BIO_printf(io, "%s, Cipher is %s\n",
1825: SSL_CIPHER_get_version(c),
1826: SSL_CIPHER_get_name(c));
1827: SSL_SESSION_print(io, SSL_get_session(con));
1828: BIO_printf(io, "---\n");
1829: print_stats(io, SSL_get_SSL_CTX(con));
1830: BIO_printf(io, "---\n");
1831: peer = SSL_get_peer_certificate(con);
1832: if (peer != NULL) {
1833: BIO_printf(io, "Client certificate\n");
1834: X509_print(io, peer);
1835: PEM_write_bio_X509(io, peer);
1836: } else
1837: BIO_puts(io, "no client certificate available\n");
1838: BIO_puts(io, "</BODY></HTML>\r\n\r\n");
1839: break;
1840: } else if ((www == 2 || www == 3)
1841: && (strncmp("GET /", buf, 5) == 0)) {
1842: BIO *file;
1843: char *p, *e;
1844: static const char *text = "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
1845:
1846: /* skip the '/' */
1847: p = &(buf[5]);
1848:
1849: dot = 1;
1850: for (e = p; *e != '\0'; e++) {
1851: if (e[0] == ' ')
1852: break;
1853:
1854: switch (dot) {
1855: case 1:
1856: dot = (e[0] == '.') ? 2 : 0;
1857: break;
1858: case 2:
1859: dot = (e[0] == '.') ? 3 : 0;
1860: break;
1861: case 3:
1.32 bcook 1862: dot = (e[0] == '/' || e[0] == '\\') ? -1 : 0;
1.1 jsing 1863: break;
1864: }
1865: if (dot == 0)
1.32 bcook 1866: dot = (e[0] == '/' || e[0] == '\\') ? 1 : 0;
1.1 jsing 1867: }
1868: dot = (dot == 3) || (dot == -1); /* filename contains
1869: * ".." component */
1870:
1871: if (*e == '\0') {
1872: BIO_puts(io, text);
1873: BIO_printf(io, "'%s' is an invalid file name\r\n", p);
1874: break;
1875: }
1876: *e = '\0';
1877:
1878: if (dot) {
1879: BIO_puts(io, text);
1880: BIO_printf(io, "'%s' contains '..' reference\r\n", p);
1881: break;
1882: }
1883: if (*p == '/') {
1884: BIO_puts(io, text);
1885: BIO_printf(io, "'%s' is an invalid path\r\n", p);
1886: break;
1887: }
1888: /* if a directory, do the index thang */
1889: if (app_isdir(p) > 0) {
1890: BIO_puts(io, text);
1891: BIO_printf(io, "'%s' is a directory\r\n", p);
1892: break;
1893: }
1894: if ((file = BIO_new_file(p, "r")) == NULL) {
1895: BIO_puts(io, text);
1896: BIO_printf(io, "Error opening '%s'\r\n", p);
1897: ERR_print_errors(io);
1898: break;
1899: }
1900: if (!s_quiet)
1901: BIO_printf(bio_err, "FILE:%s\n", p);
1902:
1903: if (www == 2) {
1904: i = strlen(p);
1905: if (((i > 5) && (strcmp(&(p[i - 5]), ".html") == 0)) ||
1906: ((i > 4) && (strcmp(&(p[i - 4]), ".php") == 0)) ||
1907: ((i > 4) && (strcmp(&(p[i - 4]), ".htm") == 0)))
1908: BIO_puts(io, "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
1909: else
1910: BIO_puts(io, "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
1911: }
1912: /* send the file */
1913: for (;;) {
1914: i = BIO_read(file, buf, bufsize);
1915: if (i <= 0)
1916: break;
1917:
1918: #ifdef RENEG
1919: total_bytes += i;
1920: fprintf(stderr, "%d\n", i);
1921: if (total_bytes > 3 * 1024) {
1922: total_bytes = 0;
1923: fprintf(stderr, "RENEGOTIATE\n");
1924: SSL_renegotiate(con);
1925: }
1926: #endif
1927:
1928: for (j = 0; j < i;) {
1929: #ifdef RENEG
1930: {
1931: static count = 0;
1932: if (++count == 13) {
1933: SSL_renegotiate(con);
1934: }
1935: }
1936: #endif
1937: k = BIO_write(io, &(buf[j]), i - j);
1938: if (k <= 0) {
1939: if (!BIO_should_retry(io))
1940: goto write_error;
1941: else {
1942: BIO_printf(bio_s_out, "rwrite W BLOCK\n");
1943: }
1944: } else {
1945: j += k;
1946: }
1947: }
1948: }
1949: write_error:
1950: BIO_free(file);
1951: break;
1952: }
1953: }
1954:
1955: for (;;) {
1956: i = (int) BIO_flush(io);
1957: if (i <= 0) {
1958: if (!BIO_should_retry(io))
1959: break;
1960: } else
1961: break;
1962: }
1.30 jsing 1963: end:
1.1 jsing 1964: /* make sure we re-use sessions */
1965: SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
1966:
1.30 jsing 1967: err:
1.1 jsing 1968:
1969: if (ret >= 0)
1970: BIO_printf(bio_s_out, "ACCEPT\n");
1971:
1.24 mmcc 1972: free(buf);
1.29 jsing 1973: BIO_free_all(io);
1.1 jsing 1974: /* if (ssl_bio != NULL) BIO_free(ssl_bio);*/
1975: return (ret);
1976: }
1977:
1978: #define MAX_SESSION_ID_ATTEMPTS 10
1979: static int
1980: generate_session_id(const SSL * ssl, unsigned char *id,
1981: unsigned int *id_len)
1982: {
1983: unsigned int count = 0;
1984: do {
1.3 jsing 1985: arc4random_buf(id, *id_len);
1.1 jsing 1986: /*
1987: * Prefix the session_id with the required prefix. NB: If our
1988: * prefix is too long, clip it - but there will be worse
1989: * effects anyway, eg. the server could only possibly create
1990: * 1 session ID (ie. the prefix!) so all future session
1991: * negotiations will fail due to conflicts.
1992: */
1993: memcpy(id, session_id_prefix,
1994: (strlen(session_id_prefix) < *id_len) ?
1995: strlen(session_id_prefix) : *id_len);
1996: }
1997: while (SSL_has_matching_session_id(ssl, id, *id_len) &&
1998: (++count < MAX_SESSION_ID_ATTEMPTS));
1999: if (count >= MAX_SESSION_ID_ATTEMPTS)
2000: return 0;
2001: return 1;
2002: }