Annotation of src/usr.bin/openssl/s_server.c, Revision 1.33
1.33 ! jsing 1: /* $OpenBSD: s_server.c,v 1.32 2019/10/04 09:47:34 bcook 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");
303: BIO_printf(bio_err, " -tls1_2 - Just talk TLSv1.2\n");
304: BIO_printf(bio_err, " -tls1_1 - Just talk TLSv1.1\n");
305: BIO_printf(bio_err, " -tls1 - Just talk TLSv1\n");
306: BIO_printf(bio_err, " -dtls1 - Just talk DTLSv1\n");
307: BIO_printf(bio_err, " -timeout - Enable timeouts\n");
308: BIO_printf(bio_err, " -mtu - Set link layer MTU\n");
309: BIO_printf(bio_err, " -chain - Read a certificate chain\n");
310: BIO_printf(bio_err, " -no_ssl2 - Just disable SSLv2\n");
311: BIO_printf(bio_err, " -no_ssl3 - Just disable SSLv3\n");
312: BIO_printf(bio_err, " -no_tls1 - Just disable TLSv1\n");
313: BIO_printf(bio_err, " -no_tls1_1 - Just disable TLSv1.1\n");
314: BIO_printf(bio_err, " -no_tls1_2 - Just disable TLSv1.2\n");
315: #ifndef OPENSSL_NO_DH
316: BIO_printf(bio_err, " -no_dhe - Disable ephemeral DH\n");
317: #endif
318: BIO_printf(bio_err, " -no_ecdhe - Disable ephemeral ECDH\n");
319: BIO_printf(bio_err, " -bugs - Turn on SSL bug compatibility\n");
320: BIO_printf(bio_err, " -www - Respond to a 'GET /' with a status page\n");
321: BIO_printf(bio_err, " -WWW - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
322: BIO_printf(bio_err, " -HTTP - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
323: BIO_printf(bio_err, " with the assumption it contains a complete HTTP response.\n");
324: BIO_printf(bio_err, " -id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'\n");
325: BIO_printf(bio_err, " -servername host - servername for HostName TLS extension\n");
326: BIO_printf(bio_err, " -servername_fatal - on mismatch send fatal alert (default warning alert)\n");
327: BIO_printf(bio_err, " -cert2 arg - certificate file to use for servername\n");
328: BIO_printf(bio_err, " (default is %s)\n", TEST_CERT2);
329: BIO_printf(bio_err, " -key2 arg - Private Key file to use for servername, in cert file if\n");
330: BIO_printf(bio_err, " not specified (default is %s)\n", TEST_CERT2);
331: BIO_printf(bio_err, " -tlsextdebug - hex dump of all TLS extensions received\n");
332: BIO_printf(bio_err, " -no_ticket - disable use of RFC4507bis session tickets\n");
1.33 ! jsing 333: BIO_printf(bio_err, " -alpn arg - set the advertised protocols for the ALPN extension (comma-separated list)\n");
! 334: BIO_printf(bio_err, " -groups arg - specify EC groups (colon-separated list)\n");
1.1 jsing 335: #ifndef OPENSSL_NO_SRTP
336: BIO_printf(bio_err, " -use_srtp profiles - Offer SRTP key management with a colon-separated profile list\n");
337: #endif
338: BIO_printf(bio_err, " -keymatexport label - Export keying material using label\n");
339: BIO_printf(bio_err, " -keymatexportlen len - Export len bytes of keying material (default 20)\n");
340: }
341:
342: static int local_argc = 0;
343: static char **local_argv;
344:
345:
346: /* This is a context that we pass to callbacks */
347: typedef struct tlsextctx_st {
348: char *servername;
349: BIO *biodebug;
350: int extension_error;
351: } tlsextctx;
352:
353:
354: static int
355: ssl_servername_cb(SSL * s, int *ad, void *arg)
356: {
357: tlsextctx *p = (tlsextctx *) arg;
358: const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
359: if (servername && p->biodebug)
360: BIO_printf(p->biodebug, "Hostname in TLS extension: \"%s\"\n", servername);
361:
362: if (!p->servername)
363: return SSL_TLSEXT_ERR_NOACK;
364:
365: if (servername) {
366: if (strcmp(servername, p->servername))
367: return p->extension_error;
368: if (ctx2) {
369: BIO_printf(p->biodebug, "Switching server context.\n");
370: SSL_set_SSL_CTX(s, ctx2);
371: }
372: }
373: return SSL_TLSEXT_ERR_OK;
374: }
375:
376: /* Structure passed to cert status callback */
377:
378: typedef struct tlsextstatusctx_st {
379: /* Default responder to use */
380: char *host, *path, *port;
381: int use_ssl;
382: int timeout;
383: BIO *err;
384: int verbose;
385: } tlsextstatusctx;
386:
387: static tlsextstatusctx tlscstatp = {NULL, NULL, NULL, 0, -1, NULL, 0};
388:
389: /* Certificate Status callback. This is called when a client includes a
390: * certificate status request extension.
391: *
392: * This is a simplified version. It examines certificates each time and
393: * makes one OCSP responder query for each request.
394: *
395: * A full version would store details such as the OCSP certificate IDs and
396: * minimise the number of OCSP responses by caching them until they were
397: * considered "expired".
398: */
399:
400: static int
401: cert_status_cb(SSL * s, void *arg)
402: {
403: tlsextstatusctx *srctx = arg;
404: BIO *err = srctx->err;
1.28 inoguchi 405: char *host = NULL, *port = NULL, *path = NULL;
1.1 jsing 406: int use_ssl;
407: unsigned char *rspder = NULL;
408: int rspderlen;
409: STACK_OF(OPENSSL_STRING) * aia = NULL;
410: X509 *x = NULL;
411: X509_STORE_CTX inctx;
412: X509_OBJECT obj;
413: OCSP_REQUEST *req = NULL;
414: OCSP_RESPONSE *resp = NULL;
415: OCSP_CERTID *id = NULL;
416: STACK_OF(X509_EXTENSION) * exts;
417: int ret = SSL_TLSEXT_ERR_NOACK;
418: int i;
419:
420: if (srctx->verbose)
421: BIO_puts(err, "cert_status: callback called\n");
422: /* Build up OCSP query from server certificate */
423: x = SSL_get_certificate(s);
424: aia = X509_get1_ocsp(x);
425: if (aia) {
426: if (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia, 0),
427: &host, &port, &path, &use_ssl)) {
428: BIO_puts(err, "cert_status: can't parse AIA URL\n");
429: goto err;
430: }
431: if (srctx->verbose)
432: BIO_printf(err, "cert_status: AIA URL: %s\n",
433: sk_OPENSSL_STRING_value(aia, 0));
434: } else {
435: if (!srctx->host) {
436: BIO_puts(srctx->err, "cert_status: no AIA and no default responder URL\n");
437: goto done;
438: }
439: host = srctx->host;
440: path = srctx->path;
441: port = srctx->port;
442: use_ssl = srctx->use_ssl;
443: }
444:
445: if (!X509_STORE_CTX_init(&inctx,
446: SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)),
447: NULL, NULL))
448: goto err;
449: if (X509_STORE_get_by_subject(&inctx, X509_LU_X509,
450: X509_get_issuer_name(x), &obj) <= 0) {
451: BIO_puts(err, "cert_status: Can't retrieve issuer certificate.\n");
452: X509_STORE_CTX_cleanup(&inctx);
453: goto done;
454: }
455: req = OCSP_REQUEST_new();
456: if (!req)
457: goto err;
458: id = OCSP_cert_to_id(NULL, x, obj.data.x509);
459: X509_free(obj.data.x509);
460: X509_STORE_CTX_cleanup(&inctx);
461: if (!id)
462: goto err;
463: if (!OCSP_request_add0_id(req, id))
464: goto err;
465: id = NULL;
466: /* Add any extensions to the request */
467: SSL_get_tlsext_status_exts(s, &exts);
468: for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
469: X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
470: if (!OCSP_REQUEST_add_ext(req, ext, -1))
471: goto err;
472: }
473: resp = process_responder(err, req, host, path, port, use_ssl, NULL,
474: srctx->timeout);
475: if (!resp) {
476: BIO_puts(err, "cert_status: error querying responder\n");
477: goto done;
478: }
479: rspderlen = i2d_OCSP_RESPONSE(resp, &rspder);
480: if (rspderlen <= 0)
481: goto err;
482: SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen);
483: if (srctx->verbose) {
484: BIO_puts(err, "cert_status: ocsp response sent:\n");
485: OCSP_RESPONSE_print(err, resp, 2);
486: }
487: ret = SSL_TLSEXT_ERR_OK;
1.30 jsing 488: done:
1.1 jsing 489: if (ret != SSL_TLSEXT_ERR_OK)
490: ERR_print_errors(err);
491: if (aia) {
492: free(host);
493: free(path);
494: free(port);
495: X509_email_free(aia);
496: }
497: if (id)
498: OCSP_CERTID_free(id);
499: if (req)
500: OCSP_REQUEST_free(req);
501: if (resp)
502: OCSP_RESPONSE_free(resp);
503: return ret;
1.30 jsing 504: err:
1.1 jsing 505: ret = SSL_TLSEXT_ERR_ALERT_FATAL;
506: goto done;
507: }
508:
1.8 jsing 509: /* This the context that we pass to alpn_cb */
510: typedef struct tlsextalpnctx_st {
511: unsigned char *data;
512: unsigned short len;
513: } tlsextalpnctx;
514:
515: static int
516: alpn_cb(SSL *s, const unsigned char **out, unsigned char *outlen,
517: const unsigned char *in, unsigned int inlen, void *arg)
518: {
519: tlsextalpnctx *alpn_ctx = arg;
520:
521: if (!s_quiet) {
522: /* We can assume that in is syntactically valid. */
523: unsigned i;
524:
525: BIO_printf(bio_s_out,
526: "ALPN protocols advertised by the client: ");
527: for (i = 0; i < inlen; ) {
528: if (i)
529: BIO_write(bio_s_out, ", ", 2);
530: BIO_write(bio_s_out, &in[i + 1], in[i]);
531: i += in[i] + 1;
532: }
533: BIO_write(bio_s_out, "\n", 1);
534: }
535:
536: if (SSL_select_next_proto((unsigned char**)out, outlen, alpn_ctx->data,
537: alpn_ctx->len, in, inlen) != OPENSSL_NPN_NEGOTIATED)
538: return (SSL_TLSEXT_ERR_NOACK);
539:
540: if (!s_quiet) {
541: BIO_printf(bio_s_out, "ALPN protocols selected: ");
542: BIO_write(bio_s_out, *out, *outlen);
543: BIO_write(bio_s_out, "\n", 1);
544: }
545:
546: return (SSL_TLSEXT_ERR_OK);
547: }
1.1 jsing 548:
549: #ifndef OPENSSL_NO_SRTP
550: static char *srtp_profiles = NULL;
551: #endif
552:
553: int
554: s_server_main(int argc, char *argv[])
555: {
556: X509_VERIFY_PARAM *vpm = NULL;
557: int badarg = 0;
558: short port = PORT;
559: char *CApath = NULL, *CAfile = NULL;
560: unsigned char *context = NULL;
561: char *dhfile = NULL;
562: char *named_curve = NULL;
563: int badop = 0, bugs = 0;
564: int ret = 1;
565: int off = 0;
1.5 jsing 566: int no_dhe = 0, no_ecdhe = 0, nocert = 0;
1.1 jsing 567: int state = 0;
568: const SSL_METHOD *meth = NULL;
569: int socket_type = SOCK_STREAM;
570: int s_cert_format = FORMAT_PEM, s_key_format = FORMAT_PEM;
571: char *passarg = NULL, *pass = NULL;
572: char *dpassarg = NULL, *dpass = NULL;
573: int s_dcert_format = FORMAT_PEM, s_dkey_format = FORMAT_PEM;
574: X509 *s_cert = NULL, *s_dcert = NULL;
575: EVP_PKEY *s_key = NULL, *s_dkey = NULL;
576: int no_cache = 0;
577: const char *errstr = NULL;
578: EVP_PKEY *s_key2 = NULL;
579: X509 *s_cert2 = NULL;
580: tlsextctx tlsextcbp = {NULL, NULL, SSL_TLSEXT_ERR_ALERT_WARNING};
1.8 jsing 581: const char *alpn_in = NULL;
1.33 ! jsing 582: const char *groups_in = NULL;
1.8 jsing 583: tlsextalpnctx alpn_ctx = { NULL, 0 };
1.20 doug 584:
585: if (single_execution) {
1.25 deraadt 586: if (pledge("stdio rpath inet dns tty", NULL) == -1) {
1.20 doug 587: perror("pledge");
1.22 doug 588: exit(1);
589: }
1.20 doug 590: }
591:
1.1 jsing 592: meth = SSLv23_server_method();
593:
594: local_argc = argc;
595: local_argv = argv;
596:
597: s_server_init();
598:
599: verify_depth = 0;
600: s_nbio = 0;
601: s_nbio_test = 0;
602:
603: argc--;
604: argv++;
605:
606: while (argc >= 1) {
607: if ((strcmp(*argv, "-port") == 0) ||
608: (strcmp(*argv, "-accept") == 0)) {
609: if (--argc < 1)
610: goto bad;
611: if (!extract_port(*(++argv), &port))
612: goto bad;
613: } else if (strcmp(*argv, "-verify") == 0) {
614: s_server_verify = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE;
615: if (--argc < 1)
616: goto bad;
617: verify_depth = strtonum(*(++argv), 0, INT_MAX, &errstr);
618: if (errstr)
619: goto bad;
620: BIO_printf(bio_err, "verify depth is %d\n", verify_depth);
621: } else if (strcmp(*argv, "-Verify") == 0) {
622: s_server_verify = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
623: SSL_VERIFY_CLIENT_ONCE;
624: if (--argc < 1)
625: goto bad;
626: verify_depth = strtonum(*(++argv), 0, INT_MAX, &errstr);
627: if (errstr)
628: goto bad;
629: BIO_printf(bio_err, "verify depth is %d, must return a certificate\n", verify_depth);
630: } else if (strcmp(*argv, "-context") == 0) {
631: if (--argc < 1)
632: goto bad;
633: context = (unsigned char *) *(++argv);
634: } else if (strcmp(*argv, "-cert") == 0) {
635: if (--argc < 1)
636: goto bad;
637: s_cert_file = *(++argv);
638: } else if (strcmp(*argv, "-certform") == 0) {
639: if (--argc < 1)
640: goto bad;
641: s_cert_format = str2fmt(*(++argv));
642: } else if (strcmp(*argv, "-key") == 0) {
643: if (--argc < 1)
644: goto bad;
645: s_key_file = *(++argv);
646: } else if (strcmp(*argv, "-keyform") == 0) {
647: if (--argc < 1)
648: goto bad;
649: s_key_format = str2fmt(*(++argv));
650: } else if (strcmp(*argv, "-pass") == 0) {
651: if (--argc < 1)
652: goto bad;
653: passarg = *(++argv);
654: } else if (strcmp(*argv, "-dhparam") == 0) {
655: if (--argc < 1)
656: goto bad;
657: dhfile = *(++argv);
1.33 ! jsing 658: } else if (strcmp(*argv, "-named_curve") == 0) {
1.1 jsing 659: if (--argc < 1)
660: goto bad;
661: named_curve = *(++argv);
1.33 ! jsing 662: } else if (strcmp(*argv, "-dcertform") == 0) {
1.1 jsing 663: if (--argc < 1)
664: goto bad;
665: s_dcert_format = str2fmt(*(++argv));
666: } else if (strcmp(*argv, "-dcert") == 0) {
667: if (--argc < 1)
668: goto bad;
669: s_dcert_file = *(++argv);
670: } else if (strcmp(*argv, "-dkeyform") == 0) {
671: if (--argc < 1)
672: goto bad;
673: s_dkey_format = str2fmt(*(++argv));
674: } else if (strcmp(*argv, "-dpass") == 0) {
675: if (--argc < 1)
676: goto bad;
677: dpassarg = *(++argv);
678: } else if (strcmp(*argv, "-dkey") == 0) {
679: if (--argc < 1)
680: goto bad;
681: s_dkey_file = *(++argv);
682: } else if (strcmp(*argv, "-nocert") == 0) {
683: nocert = 1;
684: } else if (strcmp(*argv, "-CApath") == 0) {
685: if (--argc < 1)
686: goto bad;
687: CApath = *(++argv);
688: } else if (strcmp(*argv, "-no_cache") == 0)
689: no_cache = 1;
690: else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm)) {
691: if (badarg)
692: goto bad;
693: continue;
694: } else if (strcmp(*argv, "-verify_return_error") == 0)
695: verify_return_error = 1;
696: else if (strcmp(*argv, "-serverpref") == 0) {
697: off |= SSL_OP_CIPHER_SERVER_PREFERENCE;
698: } else if (strcmp(*argv, "-legacy_renegotiation") == 0)
699: ; /* no-op */
700: else if (strcmp(*argv, "-cipher") == 0) {
701: if (--argc < 1)
702: goto bad;
703: cipher = *(++argv);
704: } else if (strcmp(*argv, "-CAfile") == 0) {
705: if (--argc < 1)
706: goto bad;
707: CAfile = *(++argv);
708: }
709: else if (strcmp(*argv, "-nbio") == 0) {
710: s_nbio = 1;
711: }
712: else if (strcmp(*argv, "-nbio_test") == 0) {
713: s_nbio = 1;
714: s_nbio_test = 1;
715: } else if (strcmp(*argv, "-debug") == 0) {
716: s_debug = 1;
717: }
718: else if (strcmp(*argv, "-tlsextdebug") == 0)
719: s_tlsextdebug = 1;
720: else if (strcmp(*argv, "-status") == 0)
721: s_tlsextstatus = 1;
722: else if (strcmp(*argv, "-status_verbose") == 0) {
723: s_tlsextstatus = 1;
724: tlscstatp.verbose = 1;
725: } else if (!strcmp(*argv, "-status_timeout")) {
726: s_tlsextstatus = 1;
727: if (--argc < 1)
728: goto bad;
729: tlscstatp.timeout = strtonum(*(++argv), 0, INT_MAX, &errstr);
730: if (errstr)
731: goto bad;
732: } else if (!strcmp(*argv, "-status_url")) {
733: s_tlsextstatus = 1;
734: if (--argc < 1)
735: goto bad;
736: if (!OCSP_parse_url(*(++argv),
737: &tlscstatp.host,
738: &tlscstatp.port,
739: &tlscstatp.path,
740: &tlscstatp.use_ssl)) {
741: BIO_printf(bio_err, "Error parsing URL\n");
742: goto bad;
743: }
744: }
745: else if (strcmp(*argv, "-msg") == 0) {
746: s_msg = 1;
747: } else if (strcmp(*argv, "-state") == 0) {
748: state = 1;
749: } else if (strcmp(*argv, "-crlf") == 0) {
750: s_crlf = 1;
751: } else if (strcmp(*argv, "-quiet") == 0) {
752: s_quiet = 1;
753: } else if (strcmp(*argv, "-bugs") == 0) {
754: bugs = 1;
755: } else if (strcmp(*argv, "-no_tmp_rsa") == 0) {
1.5 jsing 756: /* No-op. */
1.1 jsing 757: } else if (strcmp(*argv, "-no_dhe") == 0) {
758: no_dhe = 1;
759: } else if (strcmp(*argv, "-no_ecdhe") == 0) {
760: no_ecdhe = 1;
1.5 jsing 761: } else if (strcmp(*argv, "-www") == 0) {
1.1 jsing 762: www = 1;
763: } else if (strcmp(*argv, "-WWW") == 0) {
764: www = 2;
765: } else if (strcmp(*argv, "-HTTP") == 0) {
766: www = 3;
767: } else if (strcmp(*argv, "-no_ssl2") == 0) {
768: off |= SSL_OP_NO_SSLv2;
769: } else if (strcmp(*argv, "-no_ssl3") == 0) {
770: off |= SSL_OP_NO_SSLv3;
771: } else if (strcmp(*argv, "-no_tls1") == 0) {
772: off |= SSL_OP_NO_TLSv1;
773: } else if (strcmp(*argv, "-no_tls1_1") == 0) {
774: off |= SSL_OP_NO_TLSv1_1;
775: } else if (strcmp(*argv, "-no_tls1_2") == 0) {
776: off |= SSL_OP_NO_TLSv1_2;
777: } else if (strcmp(*argv, "-no_comp") == 0) {
778: off |= SSL_OP_NO_COMPRESSION;
1.13 doug 779: } else if (strcmp(*argv, "-no_ticket") == 0) {
1.1 jsing 780: off |= SSL_OP_NO_TICKET;
781: } else if (strcmp(*argv, "-tls1") == 0) {
782: meth = TLSv1_server_method();
783: } else if (strcmp(*argv, "-tls1_1") == 0) {
784: meth = TLSv1_1_server_method();
785: } else if (strcmp(*argv, "-tls1_2") == 0) {
786: meth = TLSv1_2_server_method();
787: }
788: #ifndef OPENSSL_NO_DTLS1
789: else if (strcmp(*argv, "-dtls1") == 0) {
790: meth = DTLSv1_server_method();
791: socket_type = SOCK_DGRAM;
792: } else if (strcmp(*argv, "-timeout") == 0)
793: enable_timeouts = 1;
794: else if (strcmp(*argv, "-mtu") == 0) {
795: if (--argc < 1)
796: goto bad;
797: socket_mtu = strtonum(*(++argv), 0, LONG_MAX, &errstr);
798: if (errstr)
799: goto bad;
800: } else if (strcmp(*argv, "-chain") == 0)
801: cert_chain = 1;
802: #endif
803: else if (strcmp(*argv, "-id_prefix") == 0) {
804: if (--argc < 1)
805: goto bad;
806: session_id_prefix = *(++argv);
807: }
808: else if (strcmp(*argv, "-servername") == 0) {
809: if (--argc < 1)
810: goto bad;
811: tlsextcbp.servername = *(++argv);
812: } else if (strcmp(*argv, "-servername_fatal") == 0) {
813: tlsextcbp.extension_error = SSL_TLSEXT_ERR_ALERT_FATAL;
814: } else if (strcmp(*argv, "-cert2") == 0) {
815: if (--argc < 1)
816: goto bad;
817: s_cert_file2 = *(++argv);
818: } else if (strcmp(*argv, "-key2") == 0) {
819: if (--argc < 1)
820: goto bad;
821: s_key_file2 = *(++argv);
1.27 jsing 822: } else if (strcmp(*argv, "-nextprotoneg") == 0) {
823: /* Ignored. */
1.1 jsing 824: if (--argc < 1)
825: goto bad;
1.27 jsing 826: ++argv;
827: } else if (strcmp(*argv,"-alpn") == 0) {
1.8 jsing 828: if (--argc < 1)
829: goto bad;
830: alpn_in = *(++argv);
1.33 ! jsing 831: } else if (strcmp(*argv, "-groups") == 0) {
! 832: if (--argc < 1)
! 833: goto bad;
! 834: groups_in = *(++argv);
1.8 jsing 835: }
1.1 jsing 836: #ifndef OPENSSL_NO_SRTP
837: else if (strcmp(*argv, "-use_srtp") == 0) {
838: if (--argc < 1)
839: goto bad;
840: srtp_profiles = *(++argv);
841: }
842: #endif
843: else if (strcmp(*argv, "-keymatexport") == 0) {
844: if (--argc < 1)
845: goto bad;
846: keymatexportlabel = *(++argv);
847: } else if (strcmp(*argv, "-keymatexportlen") == 0) {
848: if (--argc < 1)
849: goto bad;
850: keymatexportlen = strtonum(*(++argv), 1, INT_MAX, &errstr);
851: if (errstr)
852: goto bad;
853: } else {
854: BIO_printf(bio_err, "unknown option %s\n", *argv);
855: badop = 1;
856: break;
857: }
858: argc--;
859: argv++;
860: }
861: if (badop) {
1.30 jsing 862: bad:
1.1 jsing 863: if (errstr)
864: BIO_printf(bio_err, "invalid argument %s: %s\n",
865: *argv, errstr);
866: else
867: sv_usage();
868: goto end;
869: }
870:
871: if (!app_passwd(bio_err, passarg, dpassarg, &pass, &dpass)) {
872: BIO_printf(bio_err, "Error getting password\n");
873: goto end;
874: }
875: if (s_key_file == NULL)
876: s_key_file = s_cert_file;
877: if (s_key_file2 == NULL)
878: s_key_file2 = s_cert_file2;
879:
880: if (nocert == 0) {
1.17 bcook 881: s_key = load_key(bio_err, s_key_file, s_key_format, 0, pass,
1.1 jsing 882: "server certificate private key file");
883: if (!s_key) {
884: ERR_print_errors(bio_err);
885: goto end;
886: }
887: s_cert = load_cert(bio_err, s_cert_file, s_cert_format,
1.17 bcook 888: NULL, "server certificate file");
1.1 jsing 889:
890: if (!s_cert) {
891: ERR_print_errors(bio_err);
892: goto end;
893: }
894: if (tlsextcbp.servername) {
1.17 bcook 895: s_key2 = load_key(bio_err, s_key_file2, s_key_format, 0, pass,
1.1 jsing 896: "second server certificate private key file");
897: if (!s_key2) {
898: ERR_print_errors(bio_err);
899: goto end;
900: }
901: s_cert2 = load_cert(bio_err, s_cert_file2, s_cert_format,
1.17 bcook 902: NULL, "second server certificate file");
1.1 jsing 903:
904: if (!s_cert2) {
905: ERR_print_errors(bio_err);
906: goto end;
907: }
908: }
909: }
1.8 jsing 910: alpn_ctx.data = NULL;
911: if (alpn_in) {
912: unsigned short len;
913: alpn_ctx.data = next_protos_parse(&len, alpn_in);
914: if (alpn_ctx.data == NULL)
915: goto end;
916: alpn_ctx.len = len;
917: }
1.1 jsing 918:
919: if (s_dcert_file) {
920:
921: if (s_dkey_file == NULL)
922: s_dkey_file = s_dcert_file;
923:
924: s_dkey = load_key(bio_err, s_dkey_file, s_dkey_format,
1.17 bcook 925: 0, dpass, "second certificate private key file");
1.1 jsing 926: if (!s_dkey) {
927: ERR_print_errors(bio_err);
928: goto end;
929: }
930: s_dcert = load_cert(bio_err, s_dcert_file, s_dcert_format,
1.17 bcook 931: NULL, "second server certificate file");
1.1 jsing 932:
933: if (!s_dcert) {
934: ERR_print_errors(bio_err);
935: goto end;
936: }
937: }
938: if (bio_s_out == NULL) {
939: if (s_quiet && !s_debug && !s_msg) {
940: bio_s_out = BIO_new(BIO_s_null());
941: } else {
942: if (bio_s_out == NULL)
943: bio_s_out = BIO_new_fp(stdout, BIO_NOCLOSE);
944: }
945: }
1.8 jsing 946: if (nocert) {
1.1 jsing 947: s_cert_file = NULL;
948: s_key_file = NULL;
949: s_dcert_file = NULL;
950: s_dkey_file = NULL;
951: s_cert_file2 = NULL;
952: s_key_file2 = NULL;
953: }
954: ctx = SSL_CTX_new(meth);
955: if (ctx == NULL) {
956: ERR_print_errors(bio_err);
957: goto end;
958: }
959: if (session_id_prefix) {
960: if (strlen(session_id_prefix) >= 32)
961: BIO_printf(bio_err,
962: "warning: id_prefix is too long, only one new session will be possible\n");
963: else if (strlen(session_id_prefix) >= 16)
964: BIO_printf(bio_err,
965: "warning: id_prefix is too long if you use SSLv2\n");
966: if (!SSL_CTX_set_generate_session_id(ctx, generate_session_id)) {
967: BIO_printf(bio_err, "error setting 'id_prefix'\n");
968: ERR_print_errors(bio_err);
969: goto end;
970: }
971: BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
972: }
973: SSL_CTX_set_quiet_shutdown(ctx, 1);
974: if (bugs)
975: SSL_CTX_set_options(ctx, SSL_OP_ALL);
976: SSL_CTX_set_options(ctx, off);
977: /*
978: * DTLS: partial reads end up discarding unread UDP bytes :-( Setting
979: * read ahead solves this problem.
980: */
981: if (socket_type == SOCK_DGRAM)
982: SSL_CTX_set_read_ahead(ctx, 1);
983:
984: if (state)
985: SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
986: if (no_cache)
987: SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
988: else
989: SSL_CTX_sess_set_cache_size(ctx, 128);
990:
991: #ifndef OPENSSL_NO_SRTP
992: if (srtp_profiles != NULL)
993: SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles);
994: #endif
995:
996:
997: if ((!SSL_CTX_load_verify_locations(ctx, CAfile, CApath)) ||
998: (!SSL_CTX_set_default_verify_paths(ctx))) {
999: /* BIO_printf(bio_err,"X509_load_verify_locations\n"); */
1000: ERR_print_errors(bio_err);
1001: /* goto end; */
1002: }
1003: if (vpm)
1004: SSL_CTX_set1_param(ctx, vpm);
1005:
1006: if (s_cert2) {
1007: ctx2 = SSL_CTX_new(meth);
1008: if (ctx2 == NULL) {
1009: ERR_print_errors(bio_err);
1010: goto end;
1011: }
1012: }
1013: if (ctx2) {
1014: BIO_printf(bio_s_out, "Setting secondary ctx parameters\n");
1015:
1016: if (session_id_prefix) {
1017: if (strlen(session_id_prefix) >= 32)
1018: BIO_printf(bio_err,
1019: "warning: id_prefix is too long, only one new session will be possible\n");
1020: else if (strlen(session_id_prefix) >= 16)
1021: BIO_printf(bio_err,
1022: "warning: id_prefix is too long if you use SSLv2\n");
1023: if (!SSL_CTX_set_generate_session_id(ctx2, generate_session_id)) {
1024: BIO_printf(bio_err, "error setting 'id_prefix'\n");
1025: ERR_print_errors(bio_err);
1026: goto end;
1027: }
1028: BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
1029: }
1030: SSL_CTX_set_quiet_shutdown(ctx2, 1);
1031: if (bugs)
1032: SSL_CTX_set_options(ctx2, SSL_OP_ALL);
1033: SSL_CTX_set_options(ctx2, off);
1034: /*
1035: * DTLS: partial reads end up discarding unread UDP bytes :-(
1036: * Setting read ahead solves this problem.
1037: */
1038: if (socket_type == SOCK_DGRAM)
1039: SSL_CTX_set_read_ahead(ctx2, 1);
1040:
1041: if (state)
1042: SSL_CTX_set_info_callback(ctx2, apps_ssl_info_callback);
1043:
1044: if (no_cache)
1045: SSL_CTX_set_session_cache_mode(ctx2, SSL_SESS_CACHE_OFF);
1046: else
1047: SSL_CTX_sess_set_cache_size(ctx2, 128);
1048:
1049: if ((!SSL_CTX_load_verify_locations(ctx2, CAfile, CApath)) ||
1050: (!SSL_CTX_set_default_verify_paths(ctx2))) {
1051: ERR_print_errors(bio_err);
1052: }
1053: if (vpm)
1054: SSL_CTX_set1_param(ctx2, vpm);
1055: }
1.8 jsing 1056: if (alpn_ctx.data)
1057: SSL_CTX_set_alpn_select_cb(ctx, alpn_cb, &alpn_ctx);
1.1 jsing 1058:
1.33 ! jsing 1059: if (groups_in != NULL) {
! 1060: if (SSL_CTX_set1_groups_list(ctx, groups_in) != 1) {
! 1061: BIO_printf(bio_err, "Failed to set groups '%s'\n",
! 1062: groups_in);
! 1063: goto end;
! 1064: }
! 1065: }
! 1066:
1.1 jsing 1067: #ifndef OPENSSL_NO_DH
1068: if (!no_dhe) {
1069: DH *dh = NULL;
1070:
1071: if (dhfile)
1072: dh = load_dh_param(dhfile);
1073: else if (s_cert_file)
1074: dh = load_dh_param(s_cert_file);
1075:
1.4 jsing 1076: if (dh != NULL)
1.1 jsing 1077: BIO_printf(bio_s_out, "Setting temp DH parameters\n");
1.4 jsing 1078: else
1079: BIO_printf(bio_s_out, "Using auto DH parameters\n");
1080: (void) BIO_flush(bio_s_out);
1081:
1082: if (dh == NULL)
1083: SSL_CTX_set_dh_auto(ctx, 1);
1084: else if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
1085: BIO_printf(bio_err,
1086: "Error setting temp DH parameters\n");
1087: ERR_print_errors(bio_err);
1088: DH_free(dh);
1089: goto end;
1.1 jsing 1090: }
1091:
1092: if (ctx2) {
1093: if (!dhfile) {
1.14 doug 1094: DH *dh2 = NULL;
1095:
1096: if (s_cert_file2 != NULL)
1097: dh2 = load_dh_param(s_cert_file2);
1.1 jsing 1098: if (dh2 != NULL) {
1099: BIO_printf(bio_s_out, "Setting temp DH parameters\n");
1100: (void) BIO_flush(bio_s_out);
1101:
1102: DH_free(dh);
1103: dh = dh2;
1104: }
1105: }
1.4 jsing 1106: if (dh == NULL)
1107: SSL_CTX_set_dh_auto(ctx2, 1);
1108: else if (!SSL_CTX_set_tmp_dh(ctx2, dh)) {
1109: BIO_printf(bio_err,
1110: "Error setting temp DH parameters\n");
1111: ERR_print_errors(bio_err);
1112: DH_free(dh);
1113: goto end;
1114: }
1.1 jsing 1115: }
1116: DH_free(dh);
1117: }
1118: #endif
1119:
1.33 ! jsing 1120: if (!no_ecdhe && named_curve != NULL) {
1.1 jsing 1121: EC_KEY *ecdh = NULL;
1.33 ! jsing 1122: int nid;
1.1 jsing 1123:
1.33 ! jsing 1124: if ((nid = OBJ_sn2nid(named_curve)) == 0) {
! 1125: BIO_printf(bio_err, "unknown curve name (%s)\n",
! 1126: named_curve);
! 1127: goto end;
1.1 jsing 1128: }
1.33 ! jsing 1129: if ((ecdh = EC_KEY_new_by_curve_name(nid)) == NULL) {
! 1130: BIO_printf(bio_err, "unable to create curve (%s)\n",
! 1131: named_curve);
! 1132: goto end;
1.1 jsing 1133: }
1.33 ! jsing 1134: BIO_printf(bio_s_out, "Setting temp ECDH parameters\n");
1.1 jsing 1135: (void) BIO_flush(bio_s_out);
1136:
1137: SSL_CTX_set_tmp_ecdh(ctx, ecdh);
1138: if (ctx2)
1139: SSL_CTX_set_tmp_ecdh(ctx2, ecdh);
1140: EC_KEY_free(ecdh);
1141: }
1142:
1143: if (!set_cert_key_stuff(ctx, s_cert, s_key))
1144: goto end;
1145: if (ctx2 && !set_cert_key_stuff(ctx2, s_cert2, s_key2))
1146: goto end;
1147: if (s_dcert != NULL) {
1148: if (!set_cert_key_stuff(ctx, s_dcert, s_dkey))
1149: goto end;
1150: }
1151:
1152: if (cipher != NULL) {
1153: if (!SSL_CTX_set_cipher_list(ctx, cipher)) {
1154: BIO_printf(bio_err, "error setting cipher list\n");
1155: ERR_print_errors(bio_err);
1156: goto end;
1157: }
1158: if (ctx2 && !SSL_CTX_set_cipher_list(ctx2, cipher)) {
1159: BIO_printf(bio_err, "error setting cipher list\n");
1160: ERR_print_errors(bio_err);
1161: goto end;
1162: }
1163: }
1164: SSL_CTX_set_verify(ctx, s_server_verify, verify_callback);
1165: SSL_CTX_set_session_id_context(ctx, (void *) &s_server_session_id_context,
1166: sizeof s_server_session_id_context);
1167:
1168: /* Set DTLS cookie generation and verification callbacks */
1169: SSL_CTX_set_cookie_generate_cb(ctx, generate_cookie_callback);
1170: SSL_CTX_set_cookie_verify_cb(ctx, verify_cookie_callback);
1171:
1172: if (ctx2) {
1173: SSL_CTX_set_verify(ctx2, s_server_verify, verify_callback);
1174: SSL_CTX_set_session_id_context(ctx2, (void *) &s_server_session_id_context,
1175: sizeof s_server_session_id_context);
1176:
1177: tlsextcbp.biodebug = bio_s_out;
1178: SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb);
1179: SSL_CTX_set_tlsext_servername_arg(ctx2, &tlsextcbp);
1180: SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
1181: SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
1182: }
1183:
1184: if (CAfile != NULL) {
1185: SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(CAfile));
1186: if (ctx2)
1187: SSL_CTX_set_client_CA_list(ctx2, SSL_load_client_CA_file(CAfile));
1188: }
1189: BIO_printf(bio_s_out, "ACCEPT\n");
1190: (void) BIO_flush(bio_s_out);
1191: if (www)
1192: do_server(port, socket_type, &accept_socket, www_body, context);
1193: else
1194: do_server(port, socket_type, &accept_socket, sv_body, context);
1195: print_stats(bio_s_out, ctx);
1196: ret = 0;
1.30 jsing 1197: end:
1.29 jsing 1198: SSL_CTX_free(ctx);
1199: X509_free(s_cert);
1200: X509_free(s_dcert);
1201: EVP_PKEY_free(s_key);
1202: EVP_PKEY_free(s_dkey);
1.1 jsing 1203: free(pass);
1204: free(dpass);
1.29 jsing 1205: X509_VERIFY_PARAM_free(vpm);
1.1 jsing 1206: free(tlscstatp.host);
1207: free(tlscstatp.port);
1208: free(tlscstatp.path);
1.29 jsing 1209: SSL_CTX_free(ctx2);
1210: X509_free(s_cert2);
1211: EVP_PKEY_free(s_key2);
1.8 jsing 1212: free(alpn_ctx.data);
1.1 jsing 1213: if (bio_s_out != NULL) {
1214: BIO_free(bio_s_out);
1215: bio_s_out = NULL;
1216: }
1217:
1218: return (ret);
1219: }
1220:
1221: static void
1222: print_stats(BIO * bio, SSL_CTX * ssl_ctx)
1223: {
1224: BIO_printf(bio, "%4ld items in the session cache\n",
1225: SSL_CTX_sess_number(ssl_ctx));
1226: BIO_printf(bio, "%4ld client connects (SSL_connect())\n",
1227: SSL_CTX_sess_connect(ssl_ctx));
1228: BIO_printf(bio, "%4ld client renegotiates (SSL_connect())\n",
1229: SSL_CTX_sess_connect_renegotiate(ssl_ctx));
1230: BIO_printf(bio, "%4ld client connects that finished\n",
1231: SSL_CTX_sess_connect_good(ssl_ctx));
1232: BIO_printf(bio, "%4ld server accepts (SSL_accept())\n",
1233: SSL_CTX_sess_accept(ssl_ctx));
1234: BIO_printf(bio, "%4ld server renegotiates (SSL_accept())\n",
1235: SSL_CTX_sess_accept_renegotiate(ssl_ctx));
1236: BIO_printf(bio, "%4ld server accepts that finished\n",
1237: SSL_CTX_sess_accept_good(ssl_ctx));
1238: BIO_printf(bio, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx));
1239: BIO_printf(bio, "%4ld session cache misses\n", SSL_CTX_sess_misses(ssl_ctx));
1240: BIO_printf(bio, "%4ld session cache timeouts\n", SSL_CTX_sess_timeouts(ssl_ctx));
1241: BIO_printf(bio, "%4ld callback cache hits\n", SSL_CTX_sess_cb_hits(ssl_ctx));
1242: BIO_printf(bio, "%4ld cache full overflows (%ld allowed)\n",
1243: SSL_CTX_sess_cache_full(ssl_ctx),
1244: SSL_CTX_sess_get_cache_size(ssl_ctx));
1245: }
1246:
1247: static int
1248: sv_body(char *hostname, int s, unsigned char *context)
1249: {
1250: char *buf = NULL;
1.7 deraadt 1251: int ret = 1;
1.1 jsing 1252: int k, i;
1253: unsigned long l;
1254: SSL *con = NULL;
1255: BIO *sbio;
1256: struct timeval timeout;
1257:
1258: if ((buf = malloc(bufsize)) == NULL) {
1259: BIO_printf(bio_err, "out of memory\n");
1260: goto err;
1261: }
1262: if (s_nbio) {
1263: if (!s_quiet)
1264: BIO_printf(bio_err, "turning on non blocking io\n");
1.2 bcook 1265: if (!BIO_socket_nbio(s, 1))
1.1 jsing 1266: ERR_print_errors(bio_err);
1267: }
1268:
1269: if (con == NULL) {
1270: con = SSL_new(ctx);
1271: if (s_tlsextdebug) {
1272: SSL_set_tlsext_debug_callback(con, tlsext_cb);
1273: SSL_set_tlsext_debug_arg(con, bio_s_out);
1274: }
1275: if (s_tlsextstatus) {
1276: SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb);
1277: tlscstatp.err = bio_err;
1278: SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp);
1279: }
1280: if (context)
1281: SSL_set_session_id_context(con, context,
1282: strlen((char *) context));
1283: }
1284: SSL_clear(con);
1285:
1286: if (SSL_version(con) == DTLS1_VERSION) {
1287:
1288: sbio = BIO_new_dgram(s, BIO_NOCLOSE);
1289:
1290: if (enable_timeouts) {
1291: timeout.tv_sec = 0;
1292: timeout.tv_usec = DGRAM_RCV_TIMEOUT;
1293: BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
1294:
1295: timeout.tv_sec = 0;
1296: timeout.tv_usec = DGRAM_SND_TIMEOUT;
1297: BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
1298: }
1299: if (socket_mtu > 28) {
1300: SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
1301: SSL_set_mtu(con, socket_mtu - 28);
1302: } else
1303: /* want to do MTU discovery */
1304: BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
1305:
1306: /* turn on cookie exchange */
1307: SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE);
1308: } else
1309: sbio = BIO_new_socket(s, BIO_NOCLOSE);
1310:
1311: if (s_nbio_test) {
1312: BIO *test;
1313:
1314: test = BIO_new(BIO_f_nbio_test());
1315: sbio = BIO_push(test, sbio);
1316: }
1317:
1318: SSL_set_bio(con, sbio, sbio);
1319: SSL_set_accept_state(con);
1320: /* SSL_set_fd(con,s); */
1321:
1322: if (s_debug) {
1323: SSL_set_debug(con, 1);
1324: BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
1325: BIO_set_callback_arg(SSL_get_rbio(con), (char *) bio_s_out);
1326: }
1327: if (s_msg) {
1328: SSL_set_msg_callback(con, msg_cb);
1329: SSL_set_msg_callback_arg(con, bio_s_out);
1330: }
1331: if (s_tlsextdebug) {
1332: SSL_set_tlsext_debug_callback(con, tlsext_cb);
1333: SSL_set_tlsext_debug_arg(con, bio_s_out);
1334: }
1335:
1336: for (;;) {
1337: int read_from_terminal;
1338: int read_from_sslcon;
1.7 deraadt 1339: struct pollfd pfd[2];
1340: int ptimeout;
1.1 jsing 1341:
1342: read_from_terminal = 0;
1343: read_from_sslcon = SSL_pending(con);
1344:
1345: if (!read_from_sslcon) {
1.7 deraadt 1346: pfd[0].fd = fileno(stdin);
1347: pfd[0].events = POLLIN;
1348: pfd[1].fd = s;
1349: pfd[1].events = POLLIN;
1350:
1.1 jsing 1351: if ((SSL_version(con) == DTLS1_VERSION) &&
1352: DTLSv1_get_timeout(con, &timeout))
1.7 deraadt 1353: ptimeout = timeout.tv_sec * 1000 +
1354: timeout.tv_usec / 1000;
1.1 jsing 1355: else
1.7 deraadt 1356: ptimeout = -1;
1.1 jsing 1357:
1.7 deraadt 1358: i = poll(pfd, 2, ptimeout);
1.1 jsing 1359:
1360: if ((SSL_version(con) == DTLS1_VERSION) && DTLSv1_handle_timeout(con) > 0) {
1361: BIO_printf(bio_err, "TIMEOUT occured\n");
1362: }
1363: if (i <= 0)
1364: continue;
1.7 deraadt 1365: if (pfd[0].revents) {
1366: if ((pfd[0].revents & (POLLERR|POLLNVAL)))
1367: continue;
1.1 jsing 1368: read_from_terminal = 1;
1.7 deraadt 1369: }
1370: if (pfd[1].revents) {
1371: if ((pfd[1].revents & (POLLERR|POLLNVAL)))
1372: continue;
1.1 jsing 1373: read_from_sslcon = 1;
1.7 deraadt 1374: }
1.1 jsing 1375: }
1376: if (read_from_terminal) {
1377: if (s_crlf) {
1378: int j, lf_num;
1379:
1380: i = read(fileno(stdin), buf, bufsize / 2);
1381: lf_num = 0;
1382: /* both loops are skipped when i <= 0 */
1383: for (j = 0; j < i; j++)
1384: if (buf[j] == '\n')
1385: lf_num++;
1386: for (j = i - 1; j >= 0; j--) {
1387: buf[j + lf_num] = buf[j];
1388: if (buf[j] == '\n') {
1389: lf_num--;
1390: i++;
1391: buf[j + lf_num] = '\r';
1392: }
1393: }
1394: assert(lf_num == 0);
1395: } else
1396: i = read(fileno(stdin), buf, bufsize);
1397: if (!s_quiet) {
1398: if ((i <= 0) || (buf[0] == 'Q')) {
1399: BIO_printf(bio_s_out, "DONE\n");
1400: shutdown(s, SHUT_RD);
1401: close(s);
1402: close_accept_socket();
1403: ret = -11;
1404: goto err;
1405: }
1406: if ((i <= 0) || (buf[0] == 'q')) {
1407: BIO_printf(bio_s_out, "DONE\n");
1408: if (SSL_version(con) != DTLS1_VERSION) {
1409: shutdown(s, SHUT_RD);
1410: close(s);
1411: }
1412: /*
1413: * close_accept_socket(); ret= -11;
1414: */
1415: goto err;
1416: }
1417: if ((buf[0] == 'r') &&
1418: ((buf[1] == '\n') || (buf[1] == '\r'))) {
1419: SSL_renegotiate(con);
1420: i = SSL_do_handshake(con);
1421: printf("SSL_do_handshake -> %d\n", i);
1422: i = 0; /* 13; */
1423: continue;
1424: /*
1425: * RE-NEGOTIATE\n");
1426: */
1427: }
1428: if ((buf[0] == 'R') &&
1429: ((buf[1] == '\n') || (buf[1] == '\r'))) {
1430: SSL_set_verify(con,
1431: SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, NULL);
1432: SSL_renegotiate(con);
1433: i = SSL_do_handshake(con);
1434: printf("SSL_do_handshake -> %d\n", i);
1435: i = 0; /* 13; */
1436: continue;
1437: /*
1438: * RE-NEGOTIATE asking for client
1439: * cert\n");
1440: */
1441: }
1442: if (buf[0] == 'P') {
1443: static const char *str = "Lets print some clear text\n";
1444: BIO_write(SSL_get_wbio(con), str, strlen(str));
1445: }
1446: if (buf[0] == 'S') {
1447: print_stats(bio_s_out, SSL_get_SSL_CTX(con));
1448: }
1449: }
1450: l = k = 0;
1451: for (;;) {
1452: /* should do a select for the write */
1453: #ifdef RENEG
1454: {
1455: static count = 0;
1456: if (++count == 100) {
1457: count = 0;
1458: SSL_renegotiate(con);
1459: }
1460: }
1461: #endif
1462: k = SSL_write(con, &(buf[l]), (unsigned int) i);
1463: switch (SSL_get_error(con, k)) {
1464: case SSL_ERROR_NONE:
1465: break;
1466: case SSL_ERROR_WANT_WRITE:
1467: case SSL_ERROR_WANT_READ:
1468: case SSL_ERROR_WANT_X509_LOOKUP:
1469: BIO_printf(bio_s_out, "Write BLOCK\n");
1470: break;
1471: case SSL_ERROR_SYSCALL:
1472: case SSL_ERROR_SSL:
1473: BIO_printf(bio_s_out, "ERROR\n");
1474: ERR_print_errors(bio_err);
1475: ret = 1;
1476: goto err;
1477: /* break; */
1478: case SSL_ERROR_ZERO_RETURN:
1479: BIO_printf(bio_s_out, "DONE\n");
1480: ret = 1;
1481: goto err;
1482: }
1483: l += k;
1484: i -= k;
1485: if (i <= 0)
1486: break;
1487: }
1488: }
1489: if (read_from_sslcon) {
1490: if (!SSL_is_init_finished(con)) {
1491: i = init_ssl_connection(con);
1492:
1493: if (i < 0) {
1494: ret = 0;
1495: goto err;
1496: } else if (i == 0) {
1497: ret = 1;
1498: goto err;
1499: }
1500: } else {
1501: again:
1502: i = SSL_read(con, (char *) buf, bufsize);
1503: switch (SSL_get_error(con, i)) {
1504: case SSL_ERROR_NONE: {
1505: int len, n;
1506: for (len = 0; len < i;) {
1507: do {
1508: n = write(fileno(stdout), buf + len, i - len);
1509: } while (n == -1 && errno == EINTR);
1510:
1.31 deraadt 1511: if (n == -1) {
1.1 jsing 1512: BIO_printf(bio_s_out, "ERROR\n");
1513: goto err;
1514: }
1515: len += n;
1516: }
1517: }
1518: if (SSL_pending(con))
1519: goto again;
1520: break;
1521: case SSL_ERROR_WANT_WRITE:
1522: case SSL_ERROR_WANT_READ:
1523: BIO_printf(bio_s_out, "Read BLOCK\n");
1524: break;
1525: case SSL_ERROR_SYSCALL:
1526: case SSL_ERROR_SSL:
1527: BIO_printf(bio_s_out, "ERROR\n");
1528: ERR_print_errors(bio_err);
1529: ret = 1;
1530: goto err;
1531: case SSL_ERROR_ZERO_RETURN:
1532: BIO_printf(bio_s_out, "DONE\n");
1533: ret = 1;
1534: goto err;
1535: }
1536: }
1537: }
1538: }
1.30 jsing 1539: err:
1.1 jsing 1540: if (con != NULL) {
1541: BIO_printf(bio_s_out, "shutting down SSL\n");
1542: SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
1543: SSL_free(con);
1544: }
1545: BIO_printf(bio_s_out, "CONNECTION CLOSED\n");
1.26 deraadt 1546: freezero(buf, bufsize);
1.1 jsing 1547: if (ret >= 0)
1548: BIO_printf(bio_s_out, "ACCEPT\n");
1549: return (ret);
1550: }
1551:
1552: static void
1553: close_accept_socket(void)
1554: {
1555: BIO_printf(bio_err, "shutdown accept socket\n");
1556: if (accept_socket >= 0) {
1557: shutdown(accept_socket, SHUT_RDWR);
1558: close(accept_socket);
1559: }
1560: }
1561:
1562: static int
1563: init_ssl_connection(SSL * con)
1564: {
1565: int i;
1566: const char *str;
1567: X509 *peer;
1568: long verify_error;
1569: char buf[BUFSIZ];
1570: unsigned char *exportedkeymat;
1571:
1572: i = SSL_accept(con);
1573: if (i <= 0) {
1574: if (BIO_sock_should_retry(i)) {
1575: BIO_printf(bio_s_out, "DELAY\n");
1576: return (1);
1577: }
1578: BIO_printf(bio_err, "ERROR\n");
1579: verify_error = SSL_get_verify_result(con);
1580: if (verify_error != X509_V_OK) {
1581: BIO_printf(bio_err, "verify error:%s\n",
1582: X509_verify_cert_error_string(verify_error));
1583: } else
1584: ERR_print_errors(bio_err);
1585: return (0);
1586: }
1587: PEM_write_bio_SSL_SESSION(bio_s_out, SSL_get_session(con));
1588:
1589: peer = SSL_get_peer_certificate(con);
1590: if (peer != NULL) {
1591: BIO_printf(bio_s_out, "Client certificate\n");
1592: PEM_write_bio_X509(bio_s_out, peer);
1593: X509_NAME_oneline(X509_get_subject_name(peer), buf, sizeof buf);
1594: BIO_printf(bio_s_out, "subject=%s\n", buf);
1595: X509_NAME_oneline(X509_get_issuer_name(peer), buf, sizeof buf);
1596: BIO_printf(bio_s_out, "issuer=%s\n", buf);
1597: X509_free(peer);
1598: }
1599: if (SSL_get_shared_ciphers(con, buf, sizeof buf) != NULL)
1600: BIO_printf(bio_s_out, "Shared ciphers:%s\n", buf);
1601: str = SSL_CIPHER_get_name(SSL_get_current_cipher(con));
1602: BIO_printf(bio_s_out, "CIPHER is %s\n", (str != NULL) ? str : "(NONE)");
1603:
1604: #ifndef OPENSSL_NO_SRTP
1605: {
1606: SRTP_PROTECTION_PROFILE *srtp_profile
1607: = SSL_get_selected_srtp_profile(con);
1608:
1609: if (srtp_profile)
1610: BIO_printf(bio_s_out, "SRTP Extension negotiated, profile=%s\n",
1611: srtp_profile->name);
1612: }
1613: #endif
1614: if (SSL_cache_hit(con))
1615: BIO_printf(bio_s_out, "Reused session-id\n");
1616: BIO_printf(bio_s_out, "Secure Renegotiation IS%s supported\n",
1617: SSL_get_secure_renegotiation_support(con) ? "" : " NOT");
1618: if (keymatexportlabel != NULL) {
1619: BIO_printf(bio_s_out, "Keying material exporter:\n");
1620: BIO_printf(bio_s_out, " Label: '%s'\n", keymatexportlabel);
1621: BIO_printf(bio_s_out, " Length: %i bytes\n",
1622: keymatexportlen);
1623: exportedkeymat = malloc(keymatexportlen);
1624: if (exportedkeymat != NULL) {
1625: if (!SSL_export_keying_material(con, exportedkeymat,
1626: keymatexportlen,
1627: keymatexportlabel,
1628: strlen(keymatexportlabel),
1629: NULL, 0, 0)) {
1630: BIO_printf(bio_s_out, " Error\n");
1631: } else {
1632: BIO_printf(bio_s_out, " Keying material: ");
1633: for (i = 0; i < keymatexportlen; i++)
1634: BIO_printf(bio_s_out, "%02X",
1635: exportedkeymat[i]);
1636: BIO_printf(bio_s_out, "\n");
1637: }
1638: free(exportedkeymat);
1639: }
1640: }
1641: return (1);
1642: }
1643:
1644: #ifndef OPENSSL_NO_DH
1645: static DH *
1646: load_dh_param(const char *dhfile)
1647: {
1648: DH *ret = NULL;
1649: BIO *bio;
1650:
1651: if ((bio = BIO_new_file(dhfile, "r")) == NULL)
1652: goto err;
1653: ret = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
1.30 jsing 1654: err:
1.1 jsing 1655: BIO_free(bio);
1656: return (ret);
1657: }
1658: #endif
1659:
1660: static int
1661: www_body(char *hostname, int s, unsigned char *context)
1662: {
1663: char *buf = NULL;
1664: int ret = 1;
1665: int i, j, k, dot;
1666: SSL *con;
1667: const SSL_CIPHER *c;
1668: BIO *io, *ssl_bio, *sbio;
1669:
1670: buf = malloc(bufsize);
1671: if (buf == NULL)
1672: return (0);
1673: io = BIO_new(BIO_f_buffer());
1674: ssl_bio = BIO_new(BIO_f_ssl());
1675: if ((io == NULL) || (ssl_bio == NULL))
1676: goto err;
1677:
1678: if (s_nbio) {
1679: if (!s_quiet)
1680: BIO_printf(bio_err, "turning on non blocking io\n");
1.2 bcook 1681: if (!BIO_socket_nbio(s, 1))
1.1 jsing 1682: ERR_print_errors(bio_err);
1683: }
1684:
1685: /* lets make the output buffer a reasonable size */
1686: if (!BIO_set_write_buffer_size(io, bufsize))
1687: goto err;
1688:
1689: if ((con = SSL_new(ctx)) == NULL)
1690: goto err;
1691: if (s_tlsextdebug) {
1692: SSL_set_tlsext_debug_callback(con, tlsext_cb);
1693: SSL_set_tlsext_debug_arg(con, bio_s_out);
1694: }
1695: if (context)
1696: SSL_set_session_id_context(con, context,
1697: strlen((char *) context));
1698:
1699: sbio = BIO_new_socket(s, BIO_NOCLOSE);
1700: if (s_nbio_test) {
1701: BIO *test;
1702:
1703: test = BIO_new(BIO_f_nbio_test());
1704: sbio = BIO_push(test, sbio);
1705: }
1706: SSL_set_bio(con, sbio, sbio);
1707: SSL_set_accept_state(con);
1708:
1709: /* SSL_set_fd(con,s); */
1710: BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
1711: BIO_push(io, ssl_bio);
1712:
1713: if (s_debug) {
1714: SSL_set_debug(con, 1);
1715: BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
1716: BIO_set_callback_arg(SSL_get_rbio(con), (char *) bio_s_out);
1717: }
1718: if (s_msg) {
1719: SSL_set_msg_callback(con, msg_cb);
1720: SSL_set_msg_callback_arg(con, bio_s_out);
1721: }
1722: for (;;) {
1723: i = BIO_gets(io, buf, bufsize - 1);
1724: if (i < 0) { /* error */
1725: if (!BIO_should_retry(io)) {
1726: if (!s_quiet)
1727: ERR_print_errors(bio_err);
1728: goto err;
1729: } else {
1730: BIO_printf(bio_s_out, "read R BLOCK\n");
1731: sleep(1);
1732: continue;
1733: }
1734: } else if (i == 0) { /* end of input */
1735: ret = 1;
1736: goto end;
1737: }
1738: /* else we have data */
1739: if (((www == 1) && (strncmp("GET ", buf, 4) == 0)) ||
1740: ((www == 2) && (strncmp("GET /stats ", buf, 11) == 0))) {
1741: char *p;
1742: X509 *peer;
1743: STACK_OF(SSL_CIPHER) * sk;
1744: static const char *space = " ";
1745:
1746: BIO_puts(io, "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
1747: BIO_puts(io, "<HTML><BODY BGCOLOR=\"#ffffff\">\n");
1748: BIO_puts(io, "<pre>\n");
1749: /* BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/
1750: BIO_puts(io, "\n");
1751: for (i = 0; i < local_argc; i++) {
1752: BIO_puts(io, local_argv[i]);
1753: BIO_write(io, " ", 1);
1754: }
1755: BIO_puts(io, "\n");
1756:
1757: BIO_printf(io,
1758: "Secure Renegotiation IS%s supported\n",
1759: SSL_get_secure_renegotiation_support(con) ?
1760: "" : " NOT");
1761:
1762: /*
1763: * The following is evil and should not really be
1764: * done
1765: */
1766: BIO_printf(io, "Ciphers supported in s_server binary\n");
1767: sk = SSL_get_ciphers(con);
1768: j = sk_SSL_CIPHER_num(sk);
1769: for (i = 0; i < j; i++) {
1770: c = sk_SSL_CIPHER_value(sk, i);
1771: BIO_printf(io, "%-11s:%-25s",
1772: SSL_CIPHER_get_version(c),
1773: SSL_CIPHER_get_name(c));
1774: if ((((i + 1) % 2) == 0) && (i + 1 != j))
1775: BIO_puts(io, "\n");
1776: }
1777: BIO_puts(io, "\n");
1778: p = SSL_get_shared_ciphers(con, buf, bufsize);
1779: if (p != NULL) {
1780: BIO_printf(io, "---\nCiphers common between both SSL end points:\n");
1781: j = i = 0;
1782: while (*p) {
1783: if (*p == ':') {
1784: BIO_write(io, space, 26 - j);
1785: i++;
1786: j = 0;
1787: BIO_write(io, ((i % 3) ? " " : "\n"), 1);
1788: } else {
1789: BIO_write(io, p, 1);
1790: j++;
1791: }
1792: p++;
1793: }
1794: BIO_puts(io, "\n");
1795: }
1796: BIO_printf(io, (SSL_cache_hit(con)
1797: ? "---\nReused, "
1798: : "---\nNew, "));
1799: c = SSL_get_current_cipher(con);
1800: BIO_printf(io, "%s, Cipher is %s\n",
1801: SSL_CIPHER_get_version(c),
1802: SSL_CIPHER_get_name(c));
1803: SSL_SESSION_print(io, SSL_get_session(con));
1804: BIO_printf(io, "---\n");
1805: print_stats(io, SSL_get_SSL_CTX(con));
1806: BIO_printf(io, "---\n");
1807: peer = SSL_get_peer_certificate(con);
1808: if (peer != NULL) {
1809: BIO_printf(io, "Client certificate\n");
1810: X509_print(io, peer);
1811: PEM_write_bio_X509(io, peer);
1812: } else
1813: BIO_puts(io, "no client certificate available\n");
1814: BIO_puts(io, "</BODY></HTML>\r\n\r\n");
1815: break;
1816: } else if ((www == 2 || www == 3)
1817: && (strncmp("GET /", buf, 5) == 0)) {
1818: BIO *file;
1819: char *p, *e;
1820: static const char *text = "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
1821:
1822: /* skip the '/' */
1823: p = &(buf[5]);
1824:
1825: dot = 1;
1826: for (e = p; *e != '\0'; e++) {
1827: if (e[0] == ' ')
1828: break;
1829:
1830: switch (dot) {
1831: case 1:
1832: dot = (e[0] == '.') ? 2 : 0;
1833: break;
1834: case 2:
1835: dot = (e[0] == '.') ? 3 : 0;
1836: break;
1837: case 3:
1.32 bcook 1838: dot = (e[0] == '/' || e[0] == '\\') ? -1 : 0;
1.1 jsing 1839: break;
1840: }
1841: if (dot == 0)
1.32 bcook 1842: dot = (e[0] == '/' || e[0] == '\\') ? 1 : 0;
1.1 jsing 1843: }
1844: dot = (dot == 3) || (dot == -1); /* filename contains
1845: * ".." component */
1846:
1847: if (*e == '\0') {
1848: BIO_puts(io, text);
1849: BIO_printf(io, "'%s' is an invalid file name\r\n", p);
1850: break;
1851: }
1852: *e = '\0';
1853:
1854: if (dot) {
1855: BIO_puts(io, text);
1856: BIO_printf(io, "'%s' contains '..' reference\r\n", p);
1857: break;
1858: }
1859: if (*p == '/') {
1860: BIO_puts(io, text);
1861: BIO_printf(io, "'%s' is an invalid path\r\n", p);
1862: break;
1863: }
1864: /* if a directory, do the index thang */
1865: if (app_isdir(p) > 0) {
1866: BIO_puts(io, text);
1867: BIO_printf(io, "'%s' is a directory\r\n", p);
1868: break;
1869: }
1870: if ((file = BIO_new_file(p, "r")) == NULL) {
1871: BIO_puts(io, text);
1872: BIO_printf(io, "Error opening '%s'\r\n", p);
1873: ERR_print_errors(io);
1874: break;
1875: }
1876: if (!s_quiet)
1877: BIO_printf(bio_err, "FILE:%s\n", p);
1878:
1879: if (www == 2) {
1880: i = strlen(p);
1881: if (((i > 5) && (strcmp(&(p[i - 5]), ".html") == 0)) ||
1882: ((i > 4) && (strcmp(&(p[i - 4]), ".php") == 0)) ||
1883: ((i > 4) && (strcmp(&(p[i - 4]), ".htm") == 0)))
1884: BIO_puts(io, "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
1885: else
1886: BIO_puts(io, "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
1887: }
1888: /* send the file */
1889: for (;;) {
1890: i = BIO_read(file, buf, bufsize);
1891: if (i <= 0)
1892: break;
1893:
1894: #ifdef RENEG
1895: total_bytes += i;
1896: fprintf(stderr, "%d\n", i);
1897: if (total_bytes > 3 * 1024) {
1898: total_bytes = 0;
1899: fprintf(stderr, "RENEGOTIATE\n");
1900: SSL_renegotiate(con);
1901: }
1902: #endif
1903:
1904: for (j = 0; j < i;) {
1905: #ifdef RENEG
1906: {
1907: static count = 0;
1908: if (++count == 13) {
1909: SSL_renegotiate(con);
1910: }
1911: }
1912: #endif
1913: k = BIO_write(io, &(buf[j]), i - j);
1914: if (k <= 0) {
1915: if (!BIO_should_retry(io))
1916: goto write_error;
1917: else {
1918: BIO_printf(bio_s_out, "rwrite W BLOCK\n");
1919: }
1920: } else {
1921: j += k;
1922: }
1923: }
1924: }
1925: write_error:
1926: BIO_free(file);
1927: break;
1928: }
1929: }
1930:
1931: for (;;) {
1932: i = (int) BIO_flush(io);
1933: if (i <= 0) {
1934: if (!BIO_should_retry(io))
1935: break;
1936: } else
1937: break;
1938: }
1.30 jsing 1939: end:
1.1 jsing 1940: /* make sure we re-use sessions */
1941: SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
1942:
1.30 jsing 1943: err:
1.1 jsing 1944:
1945: if (ret >= 0)
1946: BIO_printf(bio_s_out, "ACCEPT\n");
1947:
1.24 mmcc 1948: free(buf);
1.29 jsing 1949: BIO_free_all(io);
1.1 jsing 1950: /* if (ssl_bio != NULL) BIO_free(ssl_bio);*/
1951: return (ret);
1952: }
1953:
1954: #define MAX_SESSION_ID_ATTEMPTS 10
1955: static int
1956: generate_session_id(const SSL * ssl, unsigned char *id,
1957: unsigned int *id_len)
1958: {
1959: unsigned int count = 0;
1960: do {
1.3 jsing 1961: arc4random_buf(id, *id_len);
1.1 jsing 1962: /*
1963: * Prefix the session_id with the required prefix. NB: If our
1964: * prefix is too long, clip it - but there will be worse
1965: * effects anyway, eg. the server could only possibly create
1966: * 1 session ID (ie. the prefix!) so all future session
1967: * negotiations will fail due to conflicts.
1968: */
1969: memcpy(id, session_id_prefix,
1970: (strlen(session_id_prefix) < *id_len) ?
1971: strlen(session_id_prefix) : *id_len);
1972: }
1973: while (SSL_has_matching_session_id(ssl, id, *id_len) &&
1974: (++count < MAX_SESSION_ID_ATTEMPTS));
1975: if (count >= MAX_SESSION_ID_ATTEMPTS)
1976: return 0;
1977: return 1;
1978: }