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