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