Annotation of src/usr.bin/openssl/s_server.c, Revision 1.15
1.15 ! jsing 1: /* $OpenBSD: s_server.c,v 1.14 2015/07/20 18:31:01 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: #ifndef OPENSSL_NO_SRTP
581: static char *srtp_profiles = NULL;
582: #endif
583:
584: int
585: s_server_main(int argc, char *argv[])
586: {
587: X509_VERIFY_PARAM *vpm = NULL;
588: int badarg = 0;
589: short port = PORT;
590: char *CApath = NULL, *CAfile = NULL;
591: unsigned char *context = NULL;
592: char *dhfile = NULL;
593: char *named_curve = NULL;
594: int badop = 0, bugs = 0;
595: int ret = 1;
596: int off = 0;
1.5 jsing 597: int no_dhe = 0, no_ecdhe = 0, nocert = 0;
1.1 jsing 598: int state = 0;
599: const SSL_METHOD *meth = NULL;
600: int socket_type = SOCK_STREAM;
601: ENGINE *e = NULL;
602: int s_cert_format = FORMAT_PEM, s_key_format = FORMAT_PEM;
603: char *passarg = NULL, *pass = NULL;
604: char *dpassarg = NULL, *dpass = NULL;
605: int s_dcert_format = FORMAT_PEM, s_dkey_format = FORMAT_PEM;
606: X509 *s_cert = NULL, *s_dcert = NULL;
607: EVP_PKEY *s_key = NULL, *s_dkey = NULL;
608: int no_cache = 0;
609: const char *errstr = NULL;
610: EVP_PKEY *s_key2 = NULL;
611: X509 *s_cert2 = NULL;
612: tlsextctx tlsextcbp = {NULL, NULL, SSL_TLSEXT_ERR_ALERT_WARNING};
613: const char *next_proto_neg_in = NULL;
1.8 jsing 614: tlsextnextprotoctx next_proto = { NULL, 0 };
615: const char *alpn_in = NULL;
616: tlsextalpnctx alpn_ctx = { NULL, 0 };
1.1 jsing 617: meth = SSLv23_server_method();
618:
619: local_argc = argc;
620: local_argv = argv;
621:
622: s_server_init();
623:
624: verify_depth = 0;
625: s_nbio = 0;
626: s_nbio_test = 0;
627:
628: argc--;
629: argv++;
630:
631: while (argc >= 1) {
632: if ((strcmp(*argv, "-port") == 0) ||
633: (strcmp(*argv, "-accept") == 0)) {
634: if (--argc < 1)
635: goto bad;
636: if (!extract_port(*(++argv), &port))
637: goto bad;
638: } else if (strcmp(*argv, "-verify") == 0) {
639: s_server_verify = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE;
640: if (--argc < 1)
641: goto bad;
642: verify_depth = strtonum(*(++argv), 0, INT_MAX, &errstr);
643: if (errstr)
644: goto bad;
645: BIO_printf(bio_err, "verify depth is %d\n", verify_depth);
646: } else if (strcmp(*argv, "-Verify") == 0) {
647: s_server_verify = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
648: SSL_VERIFY_CLIENT_ONCE;
649: if (--argc < 1)
650: goto bad;
651: verify_depth = strtonum(*(++argv), 0, INT_MAX, &errstr);
652: if (errstr)
653: goto bad;
654: BIO_printf(bio_err, "verify depth is %d, must return a certificate\n", verify_depth);
655: } else if (strcmp(*argv, "-context") == 0) {
656: if (--argc < 1)
657: goto bad;
658: context = (unsigned char *) *(++argv);
659: } else if (strcmp(*argv, "-cert") == 0) {
660: if (--argc < 1)
661: goto bad;
662: s_cert_file = *(++argv);
663: } else if (strcmp(*argv, "-certform") == 0) {
664: if (--argc < 1)
665: goto bad;
666: s_cert_format = str2fmt(*(++argv));
667: } else if (strcmp(*argv, "-key") == 0) {
668: if (--argc < 1)
669: goto bad;
670: s_key_file = *(++argv);
671: } else if (strcmp(*argv, "-keyform") == 0) {
672: if (--argc < 1)
673: goto bad;
674: s_key_format = str2fmt(*(++argv));
675: } else if (strcmp(*argv, "-pass") == 0) {
676: if (--argc < 1)
677: goto bad;
678: passarg = *(++argv);
679: } else if (strcmp(*argv, "-dhparam") == 0) {
680: if (--argc < 1)
681: goto bad;
682: dhfile = *(++argv);
683: }
684: else if (strcmp(*argv, "-named_curve") == 0) {
685: if (--argc < 1)
686: goto bad;
687: named_curve = *(++argv);
688: }
689: else if (strcmp(*argv, "-dcertform") == 0) {
690: if (--argc < 1)
691: goto bad;
692: s_dcert_format = str2fmt(*(++argv));
693: } else if (strcmp(*argv, "-dcert") == 0) {
694: if (--argc < 1)
695: goto bad;
696: s_dcert_file = *(++argv);
697: } else if (strcmp(*argv, "-dkeyform") == 0) {
698: if (--argc < 1)
699: goto bad;
700: s_dkey_format = str2fmt(*(++argv));
701: } else if (strcmp(*argv, "-dpass") == 0) {
702: if (--argc < 1)
703: goto bad;
704: dpassarg = *(++argv);
705: } else if (strcmp(*argv, "-dkey") == 0) {
706: if (--argc < 1)
707: goto bad;
708: s_dkey_file = *(++argv);
709: } else if (strcmp(*argv, "-nocert") == 0) {
710: nocert = 1;
711: } else if (strcmp(*argv, "-CApath") == 0) {
712: if (--argc < 1)
713: goto bad;
714: CApath = *(++argv);
715: } else if (strcmp(*argv, "-no_cache") == 0)
716: no_cache = 1;
717: else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm)) {
718: if (badarg)
719: goto bad;
720: continue;
721: } else if (strcmp(*argv, "-verify_return_error") == 0)
722: verify_return_error = 1;
723: else if (strcmp(*argv, "-serverpref") == 0) {
724: off |= SSL_OP_CIPHER_SERVER_PREFERENCE;
725: } else if (strcmp(*argv, "-legacy_renegotiation") == 0)
726: ; /* no-op */
727: else if (strcmp(*argv, "-cipher") == 0) {
728: if (--argc < 1)
729: goto bad;
730: cipher = *(++argv);
731: } else if (strcmp(*argv, "-CAfile") == 0) {
732: if (--argc < 1)
733: goto bad;
734: CAfile = *(++argv);
735: }
736: else if (strcmp(*argv, "-nbio") == 0) {
737: s_nbio = 1;
738: }
739: else if (strcmp(*argv, "-nbio_test") == 0) {
740: s_nbio = 1;
741: s_nbio_test = 1;
742: } else if (strcmp(*argv, "-debug") == 0) {
743: s_debug = 1;
744: }
745: else if (strcmp(*argv, "-tlsextdebug") == 0)
746: s_tlsextdebug = 1;
747: else if (strcmp(*argv, "-status") == 0)
748: s_tlsextstatus = 1;
749: else if (strcmp(*argv, "-status_verbose") == 0) {
750: s_tlsextstatus = 1;
751: tlscstatp.verbose = 1;
752: } else if (!strcmp(*argv, "-status_timeout")) {
753: s_tlsextstatus = 1;
754: if (--argc < 1)
755: goto bad;
756: tlscstatp.timeout = strtonum(*(++argv), 0, INT_MAX, &errstr);
757: if (errstr)
758: goto bad;
759: } else if (!strcmp(*argv, "-status_url")) {
760: s_tlsextstatus = 1;
761: if (--argc < 1)
762: goto bad;
763: if (!OCSP_parse_url(*(++argv),
764: &tlscstatp.host,
765: &tlscstatp.port,
766: &tlscstatp.path,
767: &tlscstatp.use_ssl)) {
768: BIO_printf(bio_err, "Error parsing URL\n");
769: goto bad;
770: }
771: }
772: else if (strcmp(*argv, "-msg") == 0) {
773: s_msg = 1;
774: } else if (strcmp(*argv, "-state") == 0) {
775: state = 1;
776: } else if (strcmp(*argv, "-crlf") == 0) {
777: s_crlf = 1;
778: } else if (strcmp(*argv, "-quiet") == 0) {
779: s_quiet = 1;
780: } else if (strcmp(*argv, "-bugs") == 0) {
781: bugs = 1;
782: } else if (strcmp(*argv, "-no_tmp_rsa") == 0) {
1.5 jsing 783: /* No-op. */
1.1 jsing 784: } else if (strcmp(*argv, "-no_dhe") == 0) {
785: no_dhe = 1;
786: } else if (strcmp(*argv, "-no_ecdhe") == 0) {
787: no_ecdhe = 1;
1.5 jsing 788: } else if (strcmp(*argv, "-www") == 0) {
1.1 jsing 789: www = 1;
790: } else if (strcmp(*argv, "-WWW") == 0) {
791: www = 2;
792: } else if (strcmp(*argv, "-HTTP") == 0) {
793: www = 3;
794: } else if (strcmp(*argv, "-no_ssl2") == 0) {
795: off |= SSL_OP_NO_SSLv2;
796: } else if (strcmp(*argv, "-no_ssl3") == 0) {
797: off |= SSL_OP_NO_SSLv3;
798: } else if (strcmp(*argv, "-no_tls1") == 0) {
799: off |= SSL_OP_NO_TLSv1;
800: } else if (strcmp(*argv, "-no_tls1_1") == 0) {
801: off |= SSL_OP_NO_TLSv1_1;
802: } else if (strcmp(*argv, "-no_tls1_2") == 0) {
803: off |= SSL_OP_NO_TLSv1_2;
804: } else if (strcmp(*argv, "-no_comp") == 0) {
805: off |= SSL_OP_NO_COMPRESSION;
1.13 doug 806: } else if (strcmp(*argv, "-no_ticket") == 0) {
1.1 jsing 807: off |= SSL_OP_NO_TICKET;
808: } else if (strcmp(*argv, "-tls1") == 0) {
809: meth = TLSv1_server_method();
810: } else if (strcmp(*argv, "-tls1_1") == 0) {
811: meth = TLSv1_1_server_method();
812: } else if (strcmp(*argv, "-tls1_2") == 0) {
813: meth = TLSv1_2_server_method();
814: }
815: #ifndef OPENSSL_NO_DTLS1
816: else if (strcmp(*argv, "-dtls1") == 0) {
817: meth = DTLSv1_server_method();
818: socket_type = SOCK_DGRAM;
819: } else if (strcmp(*argv, "-timeout") == 0)
820: enable_timeouts = 1;
821: else if (strcmp(*argv, "-mtu") == 0) {
822: if (--argc < 1)
823: goto bad;
824: socket_mtu = strtonum(*(++argv), 0, LONG_MAX, &errstr);
825: if (errstr)
826: goto bad;
827: } else if (strcmp(*argv, "-chain") == 0)
828: cert_chain = 1;
829: #endif
830: else if (strcmp(*argv, "-id_prefix") == 0) {
831: if (--argc < 1)
832: goto bad;
833: session_id_prefix = *(++argv);
834: }
835: #ifndef OPENSSL_NO_ENGINE
836: else if (strcmp(*argv, "-engine") == 0) {
837: if (--argc < 1)
838: goto bad;
839: engine_id = *(++argv);
840: }
841: #endif
842: else if (strcmp(*argv, "-servername") == 0) {
843: if (--argc < 1)
844: goto bad;
845: tlsextcbp.servername = *(++argv);
846: } else if (strcmp(*argv, "-servername_fatal") == 0) {
847: tlsextcbp.extension_error = SSL_TLSEXT_ERR_ALERT_FATAL;
848: } else if (strcmp(*argv, "-cert2") == 0) {
849: if (--argc < 1)
850: goto bad;
851: s_cert_file2 = *(++argv);
852: } else if (strcmp(*argv, "-key2") == 0) {
853: if (--argc < 1)
854: goto bad;
855: s_key_file2 = *(++argv);
856: }
857: else if (strcmp(*argv, "-nextprotoneg") == 0) {
858: if (--argc < 1)
859: goto bad;
860: next_proto_neg_in = *(++argv);
861: }
1.8 jsing 862: else if (strcmp(*argv,"-alpn") == 0) {
863: if (--argc < 1)
864: goto bad;
865: alpn_in = *(++argv);
866: }
1.1 jsing 867: #ifndef OPENSSL_NO_SRTP
868: else if (strcmp(*argv, "-use_srtp") == 0) {
869: if (--argc < 1)
870: goto bad;
871: srtp_profiles = *(++argv);
872: }
873: #endif
874: else if (strcmp(*argv, "-keymatexport") == 0) {
875: if (--argc < 1)
876: goto bad;
877: keymatexportlabel = *(++argv);
878: } else if (strcmp(*argv, "-keymatexportlen") == 0) {
879: if (--argc < 1)
880: goto bad;
881: keymatexportlen = strtonum(*(++argv), 1, INT_MAX, &errstr);
882: if (errstr)
883: goto bad;
884: } else {
885: BIO_printf(bio_err, "unknown option %s\n", *argv);
886: badop = 1;
887: break;
888: }
889: argc--;
890: argv++;
891: }
892: if (badop) {
893: bad:
894: if (errstr)
895: BIO_printf(bio_err, "invalid argument %s: %s\n",
896: *argv, errstr);
897: else
898: sv_usage();
899: goto end;
900: }
901:
902: #ifndef OPENSSL_NO_ENGINE
903: e = setup_engine(bio_err, engine_id, 1);
904: #endif
905:
906: if (!app_passwd(bio_err, passarg, dpassarg, &pass, &dpass)) {
907: BIO_printf(bio_err, "Error getting password\n");
908: goto end;
909: }
910: if (s_key_file == NULL)
911: s_key_file = s_cert_file;
912: if (s_key_file2 == NULL)
913: s_key_file2 = s_cert_file2;
914:
915: if (nocert == 0) {
916: s_key = load_key(bio_err, s_key_file, s_key_format, 0, pass, e,
917: "server certificate private key file");
918: if (!s_key) {
919: ERR_print_errors(bio_err);
920: goto end;
921: }
922: s_cert = load_cert(bio_err, s_cert_file, s_cert_format,
923: NULL, e, "server certificate file");
924:
925: if (!s_cert) {
926: ERR_print_errors(bio_err);
927: goto end;
928: }
929: if (tlsextcbp.servername) {
930: s_key2 = load_key(bio_err, s_key_file2, s_key_format, 0, pass, e,
931: "second server certificate private key file");
932: if (!s_key2) {
933: ERR_print_errors(bio_err);
934: goto end;
935: }
936: s_cert2 = load_cert(bio_err, s_cert_file2, s_cert_format,
937: NULL, e, "second server certificate file");
938:
939: if (!s_cert2) {
940: ERR_print_errors(bio_err);
941: goto end;
942: }
943: }
944: }
945: if (next_proto_neg_in) {
946: unsigned short len;
947: next_proto.data = next_protos_parse(&len, next_proto_neg_in);
948: if (next_proto.data == NULL)
949: goto end;
950: next_proto.len = len;
951: } else {
952: next_proto.data = NULL;
953: }
1.8 jsing 954: alpn_ctx.data = NULL;
955: if (alpn_in) {
956: unsigned short len;
957: alpn_ctx.data = next_protos_parse(&len, alpn_in);
958: if (alpn_ctx.data == NULL)
959: goto end;
960: alpn_ctx.len = len;
961: }
1.1 jsing 962:
963: if (s_dcert_file) {
964:
965: if (s_dkey_file == NULL)
966: s_dkey_file = s_dcert_file;
967:
968: s_dkey = load_key(bio_err, s_dkey_file, s_dkey_format,
969: 0, dpass, e,
970: "second certificate private key file");
971: if (!s_dkey) {
972: ERR_print_errors(bio_err);
973: goto end;
974: }
975: s_dcert = load_cert(bio_err, s_dcert_file, s_dcert_format,
976: NULL, e, "second server certificate file");
977:
978: if (!s_dcert) {
979: ERR_print_errors(bio_err);
980: goto end;
981: }
982: }
983: if (bio_s_out == NULL) {
984: if (s_quiet && !s_debug && !s_msg) {
985: bio_s_out = BIO_new(BIO_s_null());
986: } else {
987: if (bio_s_out == NULL)
988: bio_s_out = BIO_new_fp(stdout, BIO_NOCLOSE);
989: }
990: }
1.8 jsing 991: if (nocert) {
1.1 jsing 992: s_cert_file = NULL;
993: s_key_file = NULL;
994: s_dcert_file = NULL;
995: s_dkey_file = NULL;
996: s_cert_file2 = NULL;
997: s_key_file2 = NULL;
998: }
999: ctx = SSL_CTX_new(meth);
1000: if (ctx == NULL) {
1001: ERR_print_errors(bio_err);
1002: goto end;
1003: }
1004: if (session_id_prefix) {
1005: if (strlen(session_id_prefix) >= 32)
1006: BIO_printf(bio_err,
1007: "warning: id_prefix is too long, only one new session will be possible\n");
1008: else if (strlen(session_id_prefix) >= 16)
1009: BIO_printf(bio_err,
1010: "warning: id_prefix is too long if you use SSLv2\n");
1011: if (!SSL_CTX_set_generate_session_id(ctx, generate_session_id)) {
1012: BIO_printf(bio_err, "error setting 'id_prefix'\n");
1013: ERR_print_errors(bio_err);
1014: goto end;
1015: }
1016: BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
1017: }
1018: SSL_CTX_set_quiet_shutdown(ctx, 1);
1019: if (bugs)
1020: SSL_CTX_set_options(ctx, SSL_OP_ALL);
1021: SSL_CTX_set_options(ctx, off);
1022: /*
1023: * DTLS: partial reads end up discarding unread UDP bytes :-( Setting
1024: * read ahead solves this problem.
1025: */
1026: if (socket_type == SOCK_DGRAM)
1027: SSL_CTX_set_read_ahead(ctx, 1);
1028:
1029: if (state)
1030: SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
1031: if (no_cache)
1032: SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
1033: else
1034: SSL_CTX_sess_set_cache_size(ctx, 128);
1035:
1036: #ifndef OPENSSL_NO_SRTP
1037: if (srtp_profiles != NULL)
1038: SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles);
1039: #endif
1040:
1041:
1042: if ((!SSL_CTX_load_verify_locations(ctx, CAfile, CApath)) ||
1043: (!SSL_CTX_set_default_verify_paths(ctx))) {
1044: /* BIO_printf(bio_err,"X509_load_verify_locations\n"); */
1045: ERR_print_errors(bio_err);
1046: /* goto end; */
1047: }
1048: if (vpm)
1049: SSL_CTX_set1_param(ctx, vpm);
1050:
1051: if (s_cert2) {
1052: ctx2 = SSL_CTX_new(meth);
1053: if (ctx2 == NULL) {
1054: ERR_print_errors(bio_err);
1055: goto end;
1056: }
1057: }
1058: if (ctx2) {
1059: BIO_printf(bio_s_out, "Setting secondary ctx parameters\n");
1060:
1061: if (session_id_prefix) {
1062: if (strlen(session_id_prefix) >= 32)
1063: BIO_printf(bio_err,
1064: "warning: id_prefix is too long, only one new session will be possible\n");
1065: else if (strlen(session_id_prefix) >= 16)
1066: BIO_printf(bio_err,
1067: "warning: id_prefix is too long if you use SSLv2\n");
1068: if (!SSL_CTX_set_generate_session_id(ctx2, generate_session_id)) {
1069: BIO_printf(bio_err, "error setting 'id_prefix'\n");
1070: ERR_print_errors(bio_err);
1071: goto end;
1072: }
1073: BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
1074: }
1075: SSL_CTX_set_quiet_shutdown(ctx2, 1);
1076: if (bugs)
1077: SSL_CTX_set_options(ctx2, SSL_OP_ALL);
1078: SSL_CTX_set_options(ctx2, off);
1079: /*
1080: * DTLS: partial reads end up discarding unread UDP bytes :-(
1081: * Setting read ahead solves this problem.
1082: */
1083: if (socket_type == SOCK_DGRAM)
1084: SSL_CTX_set_read_ahead(ctx2, 1);
1085:
1086: if (state)
1087: SSL_CTX_set_info_callback(ctx2, apps_ssl_info_callback);
1088:
1089: if (no_cache)
1090: SSL_CTX_set_session_cache_mode(ctx2, SSL_SESS_CACHE_OFF);
1091: else
1092: SSL_CTX_sess_set_cache_size(ctx2, 128);
1093:
1094: if ((!SSL_CTX_load_verify_locations(ctx2, CAfile, CApath)) ||
1095: (!SSL_CTX_set_default_verify_paths(ctx2))) {
1096: ERR_print_errors(bio_err);
1097: }
1098: if (vpm)
1099: SSL_CTX_set1_param(ctx2, vpm);
1100: }
1101: if (next_proto.data)
1102: SSL_CTX_set_next_protos_advertised_cb(ctx, next_proto_cb, &next_proto);
1.8 jsing 1103: if (alpn_ctx.data)
1104: SSL_CTX_set_alpn_select_cb(ctx, alpn_cb, &alpn_ctx);
1.1 jsing 1105:
1106: #ifndef OPENSSL_NO_DH
1107: if (!no_dhe) {
1108: DH *dh = NULL;
1109:
1110: if (dhfile)
1111: dh = load_dh_param(dhfile);
1112: else if (s_cert_file)
1113: dh = load_dh_param(s_cert_file);
1114:
1.4 jsing 1115: if (dh != NULL)
1.1 jsing 1116: BIO_printf(bio_s_out, "Setting temp DH parameters\n");
1.4 jsing 1117: else
1118: BIO_printf(bio_s_out, "Using auto DH parameters\n");
1119: (void) BIO_flush(bio_s_out);
1120:
1121: if (dh == NULL)
1122: SSL_CTX_set_dh_auto(ctx, 1);
1123: else if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
1124: BIO_printf(bio_err,
1125: "Error setting temp DH parameters\n");
1126: ERR_print_errors(bio_err);
1127: DH_free(dh);
1128: goto end;
1.1 jsing 1129: }
1130:
1131: if (ctx2) {
1132: if (!dhfile) {
1.14 doug 1133: DH *dh2 = NULL;
1134:
1135: if (s_cert_file2 != NULL)
1136: dh2 = load_dh_param(s_cert_file2);
1.1 jsing 1137: if (dh2 != NULL) {
1138: BIO_printf(bio_s_out, "Setting temp DH parameters\n");
1139: (void) BIO_flush(bio_s_out);
1140:
1141: DH_free(dh);
1142: dh = dh2;
1143: }
1144: }
1.4 jsing 1145: if (dh == NULL)
1146: SSL_CTX_set_dh_auto(ctx2, 1);
1147: else if (!SSL_CTX_set_tmp_dh(ctx2, dh)) {
1148: BIO_printf(bio_err,
1149: "Error setting temp DH parameters\n");
1150: ERR_print_errors(bio_err);
1151: DH_free(dh);
1152: goto end;
1153: }
1.1 jsing 1154: }
1155: DH_free(dh);
1156: }
1157: #endif
1158:
1159: if (!no_ecdhe) {
1160: EC_KEY *ecdh = NULL;
1161:
1162: if (named_curve) {
1163: int nid = OBJ_sn2nid(named_curve);
1164:
1165: if (nid == 0) {
1166: BIO_printf(bio_err, "unknown curve name (%s)\n",
1167: named_curve);
1168: goto end;
1169: }
1170: ecdh = EC_KEY_new_by_curve_name(nid);
1171: if (ecdh == NULL) {
1172: BIO_printf(bio_err, "unable to create curve (%s)\n",
1173: named_curve);
1174: goto end;
1175: }
1176: }
1177: if (ecdh != NULL) {
1178: BIO_printf(bio_s_out, "Setting temp ECDH parameters\n");
1179: } else {
1180: BIO_printf(bio_s_out, "Using default temp ECDH parameters\n");
1181: ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
1182: if (ecdh == NULL) {
1183: BIO_printf(bio_err, "unable to create curve (nistp256)\n");
1184: goto end;
1185: }
1186: }
1187: (void) BIO_flush(bio_s_out);
1188:
1189: SSL_CTX_set_tmp_ecdh(ctx, ecdh);
1190: if (ctx2)
1191: SSL_CTX_set_tmp_ecdh(ctx2, ecdh);
1192: EC_KEY_free(ecdh);
1193: }
1194:
1195: if (!set_cert_key_stuff(ctx, s_cert, s_key))
1196: goto end;
1197: if (ctx2 && !set_cert_key_stuff(ctx2, s_cert2, s_key2))
1198: goto end;
1199: if (s_dcert != NULL) {
1200: if (!set_cert_key_stuff(ctx, s_dcert, s_dkey))
1201: goto end;
1202: }
1203:
1204: if (cipher != NULL) {
1205: if (!SSL_CTX_set_cipher_list(ctx, cipher)) {
1206: BIO_printf(bio_err, "error setting cipher list\n");
1207: ERR_print_errors(bio_err);
1208: goto end;
1209: }
1210: if (ctx2 && !SSL_CTX_set_cipher_list(ctx2, cipher)) {
1211: BIO_printf(bio_err, "error setting cipher list\n");
1212: ERR_print_errors(bio_err);
1213: goto end;
1214: }
1215: }
1216: SSL_CTX_set_verify(ctx, s_server_verify, verify_callback);
1217: SSL_CTX_set_session_id_context(ctx, (void *) &s_server_session_id_context,
1218: sizeof s_server_session_id_context);
1219:
1220: /* Set DTLS cookie generation and verification callbacks */
1221: SSL_CTX_set_cookie_generate_cb(ctx, generate_cookie_callback);
1222: SSL_CTX_set_cookie_verify_cb(ctx, verify_cookie_callback);
1223:
1224: if (ctx2) {
1225: SSL_CTX_set_verify(ctx2, s_server_verify, verify_callback);
1226: SSL_CTX_set_session_id_context(ctx2, (void *) &s_server_session_id_context,
1227: sizeof s_server_session_id_context);
1228:
1229: tlsextcbp.biodebug = bio_s_out;
1230: SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb);
1231: SSL_CTX_set_tlsext_servername_arg(ctx2, &tlsextcbp);
1232: SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
1233: SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
1234: }
1235:
1236: if (CAfile != NULL) {
1237: SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(CAfile));
1238: if (ctx2)
1239: SSL_CTX_set_client_CA_list(ctx2, SSL_load_client_CA_file(CAfile));
1240: }
1241: BIO_printf(bio_s_out, "ACCEPT\n");
1242: (void) BIO_flush(bio_s_out);
1243: if (www)
1244: do_server(port, socket_type, &accept_socket, www_body, context);
1245: else
1246: do_server(port, socket_type, &accept_socket, sv_body, context);
1247: print_stats(bio_s_out, ctx);
1248: ret = 0;
1249: end:
1250: if (ctx != NULL)
1251: SSL_CTX_free(ctx);
1252: if (s_cert)
1253: X509_free(s_cert);
1254: if (s_dcert)
1255: X509_free(s_dcert);
1256: if (s_key)
1257: EVP_PKEY_free(s_key);
1258: if (s_dkey)
1259: EVP_PKEY_free(s_dkey);
1260: free(pass);
1261: free(dpass);
1262: if (vpm)
1263: X509_VERIFY_PARAM_free(vpm);
1264: free(tlscstatp.host);
1265: free(tlscstatp.port);
1266: free(tlscstatp.path);
1267: if (ctx2 != NULL)
1268: SSL_CTX_free(ctx2);
1269: if (s_cert2)
1270: X509_free(s_cert2);
1271: if (s_key2)
1272: EVP_PKEY_free(s_key2);
1.8 jsing 1273: free(next_proto.data);
1274: free(alpn_ctx.data);
1.1 jsing 1275: if (bio_s_out != NULL) {
1276: BIO_free(bio_s_out);
1277: bio_s_out = NULL;
1278: }
1279:
1280: return (ret);
1281: }
1282:
1283: static void
1284: print_stats(BIO * bio, SSL_CTX * ssl_ctx)
1285: {
1286: BIO_printf(bio, "%4ld items in the session cache\n",
1287: SSL_CTX_sess_number(ssl_ctx));
1288: BIO_printf(bio, "%4ld client connects (SSL_connect())\n",
1289: SSL_CTX_sess_connect(ssl_ctx));
1290: BIO_printf(bio, "%4ld client renegotiates (SSL_connect())\n",
1291: SSL_CTX_sess_connect_renegotiate(ssl_ctx));
1292: BIO_printf(bio, "%4ld client connects that finished\n",
1293: SSL_CTX_sess_connect_good(ssl_ctx));
1294: BIO_printf(bio, "%4ld server accepts (SSL_accept())\n",
1295: SSL_CTX_sess_accept(ssl_ctx));
1296: BIO_printf(bio, "%4ld server renegotiates (SSL_accept())\n",
1297: SSL_CTX_sess_accept_renegotiate(ssl_ctx));
1298: BIO_printf(bio, "%4ld server accepts that finished\n",
1299: SSL_CTX_sess_accept_good(ssl_ctx));
1300: BIO_printf(bio, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx));
1301: BIO_printf(bio, "%4ld session cache misses\n", SSL_CTX_sess_misses(ssl_ctx));
1302: BIO_printf(bio, "%4ld session cache timeouts\n", SSL_CTX_sess_timeouts(ssl_ctx));
1303: BIO_printf(bio, "%4ld callback cache hits\n", SSL_CTX_sess_cb_hits(ssl_ctx));
1304: BIO_printf(bio, "%4ld cache full overflows (%ld allowed)\n",
1305: SSL_CTX_sess_cache_full(ssl_ctx),
1306: SSL_CTX_sess_get_cache_size(ssl_ctx));
1307: }
1308:
1309: static int
1310: sv_body(char *hostname, int s, unsigned char *context)
1311: {
1312: char *buf = NULL;
1.7 deraadt 1313: int ret = 1;
1.1 jsing 1314: int k, i;
1315: unsigned long l;
1316: SSL *con = NULL;
1317: BIO *sbio;
1318: struct timeval timeout;
1319:
1320: if ((buf = malloc(bufsize)) == NULL) {
1321: BIO_printf(bio_err, "out of memory\n");
1322: goto err;
1323: }
1324: if (s_nbio) {
1325: if (!s_quiet)
1326: BIO_printf(bio_err, "turning on non blocking io\n");
1.2 bcook 1327: if (!BIO_socket_nbio(s, 1))
1.1 jsing 1328: ERR_print_errors(bio_err);
1329: }
1330:
1331: if (con == NULL) {
1332: con = SSL_new(ctx);
1333: if (s_tlsextdebug) {
1334: SSL_set_tlsext_debug_callback(con, tlsext_cb);
1335: SSL_set_tlsext_debug_arg(con, bio_s_out);
1336: }
1337: if (s_tlsextstatus) {
1338: SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb);
1339: tlscstatp.err = bio_err;
1340: SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp);
1341: }
1342: if (context)
1343: SSL_set_session_id_context(con, context,
1344: strlen((char *) context));
1345: }
1346: SSL_clear(con);
1347:
1348: if (SSL_version(con) == DTLS1_VERSION) {
1349:
1350: sbio = BIO_new_dgram(s, BIO_NOCLOSE);
1351:
1352: if (enable_timeouts) {
1353: timeout.tv_sec = 0;
1354: timeout.tv_usec = DGRAM_RCV_TIMEOUT;
1355: BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
1356:
1357: timeout.tv_sec = 0;
1358: timeout.tv_usec = DGRAM_SND_TIMEOUT;
1359: BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
1360: }
1361: if (socket_mtu > 28) {
1362: SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
1363: SSL_set_mtu(con, socket_mtu - 28);
1364: } else
1365: /* want to do MTU discovery */
1366: BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
1367:
1368: /* turn on cookie exchange */
1369: SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE);
1370: } else
1371: sbio = BIO_new_socket(s, BIO_NOCLOSE);
1372:
1373: if (s_nbio_test) {
1374: BIO *test;
1375:
1376: test = BIO_new(BIO_f_nbio_test());
1377: sbio = BIO_push(test, sbio);
1378: }
1379:
1380: SSL_set_bio(con, sbio, sbio);
1381: SSL_set_accept_state(con);
1382: /* SSL_set_fd(con,s); */
1383:
1384: if (s_debug) {
1385: SSL_set_debug(con, 1);
1386: BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
1387: BIO_set_callback_arg(SSL_get_rbio(con), (char *) bio_s_out);
1388: }
1389: if (s_msg) {
1390: SSL_set_msg_callback(con, msg_cb);
1391: SSL_set_msg_callback_arg(con, bio_s_out);
1392: }
1393: if (s_tlsextdebug) {
1394: SSL_set_tlsext_debug_callback(con, tlsext_cb);
1395: SSL_set_tlsext_debug_arg(con, bio_s_out);
1396: }
1397:
1398: for (;;) {
1399: int read_from_terminal;
1400: int read_from_sslcon;
1.7 deraadt 1401: struct pollfd pfd[2];
1402: int ptimeout;
1.1 jsing 1403:
1404: read_from_terminal = 0;
1405: read_from_sslcon = SSL_pending(con);
1406:
1407: if (!read_from_sslcon) {
1.7 deraadt 1408: pfd[0].fd = fileno(stdin);
1409: pfd[0].events = POLLIN;
1410: pfd[1].fd = s;
1411: pfd[1].events = POLLIN;
1412:
1.1 jsing 1413: if ((SSL_version(con) == DTLS1_VERSION) &&
1414: DTLSv1_get_timeout(con, &timeout))
1.7 deraadt 1415: ptimeout = timeout.tv_sec * 1000 +
1416: timeout.tv_usec / 1000;
1.1 jsing 1417: else
1.7 deraadt 1418: ptimeout = -1;
1.1 jsing 1419:
1.7 deraadt 1420: i = poll(pfd, 2, ptimeout);
1.1 jsing 1421:
1422: if ((SSL_version(con) == DTLS1_VERSION) && DTLSv1_handle_timeout(con) > 0) {
1423: BIO_printf(bio_err, "TIMEOUT occured\n");
1424: }
1425: if (i <= 0)
1426: continue;
1.7 deraadt 1427: if (pfd[0].revents) {
1428: if ((pfd[0].revents & (POLLERR|POLLNVAL)))
1429: continue;
1.1 jsing 1430: read_from_terminal = 1;
1.7 deraadt 1431: }
1432: if (pfd[1].revents) {
1433: if ((pfd[1].revents & (POLLERR|POLLNVAL)))
1434: continue;
1.1 jsing 1435: read_from_sslcon = 1;
1.7 deraadt 1436: }
1.1 jsing 1437: }
1438: if (read_from_terminal) {
1439: if (s_crlf) {
1440: int j, lf_num;
1441:
1442: i = read(fileno(stdin), buf, bufsize / 2);
1443: lf_num = 0;
1444: /* both loops are skipped when i <= 0 */
1445: for (j = 0; j < i; j++)
1446: if (buf[j] == '\n')
1447: lf_num++;
1448: for (j = i - 1; j >= 0; j--) {
1449: buf[j + lf_num] = buf[j];
1450: if (buf[j] == '\n') {
1451: lf_num--;
1452: i++;
1453: buf[j + lf_num] = '\r';
1454: }
1455: }
1456: assert(lf_num == 0);
1457: } else
1458: i = read(fileno(stdin), buf, bufsize);
1459: if (!s_quiet) {
1460: if ((i <= 0) || (buf[0] == 'Q')) {
1461: BIO_printf(bio_s_out, "DONE\n");
1462: shutdown(s, SHUT_RD);
1463: close(s);
1464: close_accept_socket();
1465: ret = -11;
1466: goto err;
1467: }
1468: if ((i <= 0) || (buf[0] == 'q')) {
1469: BIO_printf(bio_s_out, "DONE\n");
1470: if (SSL_version(con) != DTLS1_VERSION) {
1471: shutdown(s, SHUT_RD);
1472: close(s);
1473: }
1474: /*
1475: * close_accept_socket(); ret= -11;
1476: */
1477: goto err;
1478: }
1479: if ((buf[0] == 'r') &&
1480: ((buf[1] == '\n') || (buf[1] == '\r'))) {
1481: SSL_renegotiate(con);
1482: i = SSL_do_handshake(con);
1483: printf("SSL_do_handshake -> %d\n", i);
1484: i = 0; /* 13; */
1485: continue;
1486: /*
1487: * strcpy(buf,"server side
1488: * RE-NEGOTIATE\n");
1489: */
1490: }
1491: if ((buf[0] == 'R') &&
1492: ((buf[1] == '\n') || (buf[1] == '\r'))) {
1493: SSL_set_verify(con,
1494: SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, NULL);
1495: SSL_renegotiate(con);
1496: i = SSL_do_handshake(con);
1497: printf("SSL_do_handshake -> %d\n", i);
1498: i = 0; /* 13; */
1499: continue;
1500: /*
1501: * strcpy(buf,"server side
1502: * RE-NEGOTIATE asking for client
1503: * cert\n");
1504: */
1505: }
1506: if (buf[0] == 'P') {
1507: static const char *str = "Lets print some clear text\n";
1508: BIO_write(SSL_get_wbio(con), str, strlen(str));
1509: }
1510: if (buf[0] == 'S') {
1511: print_stats(bio_s_out, SSL_get_SSL_CTX(con));
1512: }
1513: }
1514: l = k = 0;
1515: for (;;) {
1516: /* should do a select for the write */
1517: #ifdef RENEG
1518: {
1519: static count = 0;
1520: if (++count == 100) {
1521: count = 0;
1522: SSL_renegotiate(con);
1523: }
1524: }
1525: #endif
1526: k = SSL_write(con, &(buf[l]), (unsigned int) i);
1527: switch (SSL_get_error(con, k)) {
1528: case SSL_ERROR_NONE:
1529: break;
1530: case SSL_ERROR_WANT_WRITE:
1531: case SSL_ERROR_WANT_READ:
1532: case SSL_ERROR_WANT_X509_LOOKUP:
1533: BIO_printf(bio_s_out, "Write BLOCK\n");
1534: break;
1535: case SSL_ERROR_SYSCALL:
1536: case SSL_ERROR_SSL:
1537: BIO_printf(bio_s_out, "ERROR\n");
1538: ERR_print_errors(bio_err);
1539: ret = 1;
1540: goto err;
1541: /* break; */
1542: case SSL_ERROR_ZERO_RETURN:
1543: BIO_printf(bio_s_out, "DONE\n");
1544: ret = 1;
1545: goto err;
1546: }
1547: l += k;
1548: i -= k;
1549: if (i <= 0)
1550: break;
1551: }
1552: }
1553: if (read_from_sslcon) {
1554: if (!SSL_is_init_finished(con)) {
1555: i = init_ssl_connection(con);
1556:
1557: if (i < 0) {
1558: ret = 0;
1559: goto err;
1560: } else if (i == 0) {
1561: ret = 1;
1562: goto err;
1563: }
1564: } else {
1565: again:
1566: i = SSL_read(con, (char *) buf, bufsize);
1567: switch (SSL_get_error(con, i)) {
1568: case SSL_ERROR_NONE: {
1569: int len, n;
1570: for (len = 0; len < i;) {
1571: do {
1572: n = write(fileno(stdout), buf + len, i - len);
1573: } while (n == -1 && errno == EINTR);
1574:
1575: if (n < 0) {
1576: BIO_printf(bio_s_out, "ERROR\n");
1577: goto err;
1578: }
1579: len += n;
1580: }
1581: }
1582: if (SSL_pending(con))
1583: goto again;
1584: break;
1585: case SSL_ERROR_WANT_WRITE:
1586: case SSL_ERROR_WANT_READ:
1587: BIO_printf(bio_s_out, "Read BLOCK\n");
1588: break;
1589: case SSL_ERROR_SYSCALL:
1590: case SSL_ERROR_SSL:
1591: BIO_printf(bio_s_out, "ERROR\n");
1592: ERR_print_errors(bio_err);
1593: ret = 1;
1594: goto err;
1595: case SSL_ERROR_ZERO_RETURN:
1596: BIO_printf(bio_s_out, "DONE\n");
1597: ret = 1;
1598: goto err;
1599: }
1600: }
1601: }
1602: }
1603: err:
1604: if (con != NULL) {
1605: BIO_printf(bio_s_out, "shutting down SSL\n");
1606: SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
1607: SSL_free(con);
1608: }
1609: BIO_printf(bio_s_out, "CONNECTION CLOSED\n");
1610: if (buf != NULL) {
1611: OPENSSL_cleanse(buf, bufsize);
1612: free(buf);
1613: }
1614: if (ret >= 0)
1615: BIO_printf(bio_s_out, "ACCEPT\n");
1616: return (ret);
1617: }
1618:
1619: static void
1620: close_accept_socket(void)
1621: {
1622: BIO_printf(bio_err, "shutdown accept socket\n");
1623: if (accept_socket >= 0) {
1624: shutdown(accept_socket, SHUT_RDWR);
1625: close(accept_socket);
1626: }
1627: }
1628:
1629: static int
1630: init_ssl_connection(SSL * con)
1631: {
1632: int i;
1633: const char *str;
1634: X509 *peer;
1635: long verify_error;
1636: char buf[BUFSIZ];
1637: const unsigned char *next_proto_neg;
1638: unsigned next_proto_neg_len;
1639: unsigned char *exportedkeymat;
1640:
1641: i = SSL_accept(con);
1642: if (i <= 0) {
1643: if (BIO_sock_should_retry(i)) {
1644: BIO_printf(bio_s_out, "DELAY\n");
1645: return (1);
1646: }
1647: BIO_printf(bio_err, "ERROR\n");
1648: verify_error = SSL_get_verify_result(con);
1649: if (verify_error != X509_V_OK) {
1650: BIO_printf(bio_err, "verify error:%s\n",
1651: X509_verify_cert_error_string(verify_error));
1652: } else
1653: ERR_print_errors(bio_err);
1654: return (0);
1655: }
1656: PEM_write_bio_SSL_SESSION(bio_s_out, SSL_get_session(con));
1657:
1658: peer = SSL_get_peer_certificate(con);
1659: if (peer != NULL) {
1660: BIO_printf(bio_s_out, "Client certificate\n");
1661: PEM_write_bio_X509(bio_s_out, peer);
1662: X509_NAME_oneline(X509_get_subject_name(peer), buf, sizeof buf);
1663: BIO_printf(bio_s_out, "subject=%s\n", buf);
1664: X509_NAME_oneline(X509_get_issuer_name(peer), buf, sizeof buf);
1665: BIO_printf(bio_s_out, "issuer=%s\n", buf);
1666: X509_free(peer);
1667: }
1668: if (SSL_get_shared_ciphers(con, buf, sizeof buf) != NULL)
1669: BIO_printf(bio_s_out, "Shared ciphers:%s\n", buf);
1670: str = SSL_CIPHER_get_name(SSL_get_current_cipher(con));
1671: BIO_printf(bio_s_out, "CIPHER is %s\n", (str != NULL) ? str : "(NONE)");
1672:
1673: SSL_get0_next_proto_negotiated(con, &next_proto_neg, &next_proto_neg_len);
1674: if (next_proto_neg) {
1675: BIO_printf(bio_s_out, "NEXTPROTO is ");
1676: BIO_write(bio_s_out, next_proto_neg, next_proto_neg_len);
1677: BIO_printf(bio_s_out, "\n");
1678: }
1679: #ifndef OPENSSL_NO_SRTP
1680: {
1681: SRTP_PROTECTION_PROFILE *srtp_profile
1682: = SSL_get_selected_srtp_profile(con);
1683:
1684: if (srtp_profile)
1685: BIO_printf(bio_s_out, "SRTP Extension negotiated, profile=%s\n",
1686: srtp_profile->name);
1687: }
1688: #endif
1689: if (SSL_cache_hit(con))
1690: BIO_printf(bio_s_out, "Reused session-id\n");
1691: BIO_printf(bio_s_out, "Secure Renegotiation IS%s supported\n",
1692: SSL_get_secure_renegotiation_support(con) ? "" : " NOT");
1693: if (keymatexportlabel != NULL) {
1694: BIO_printf(bio_s_out, "Keying material exporter:\n");
1695: BIO_printf(bio_s_out, " Label: '%s'\n", keymatexportlabel);
1696: BIO_printf(bio_s_out, " Length: %i bytes\n",
1697: keymatexportlen);
1698: exportedkeymat = malloc(keymatexportlen);
1699: if (exportedkeymat != NULL) {
1700: if (!SSL_export_keying_material(con, exportedkeymat,
1701: keymatexportlen,
1702: keymatexportlabel,
1703: strlen(keymatexportlabel),
1704: NULL, 0, 0)) {
1705: BIO_printf(bio_s_out, " Error\n");
1706: } else {
1707: BIO_printf(bio_s_out, " Keying material: ");
1708: for (i = 0; i < keymatexportlen; i++)
1709: BIO_printf(bio_s_out, "%02X",
1710: exportedkeymat[i]);
1711: BIO_printf(bio_s_out, "\n");
1712: }
1713: free(exportedkeymat);
1714: }
1715: }
1716: return (1);
1717: }
1718:
1719: #ifndef OPENSSL_NO_DH
1720: static DH *
1721: load_dh_param(const char *dhfile)
1722: {
1723: DH *ret = NULL;
1724: BIO *bio;
1725:
1726: if ((bio = BIO_new_file(dhfile, "r")) == NULL)
1727: goto err;
1728: ret = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
1729: err:
1730: BIO_free(bio);
1731: return (ret);
1732: }
1733: #endif
1734:
1735: static int
1736: www_body(char *hostname, int s, unsigned char *context)
1737: {
1738: char *buf = NULL;
1739: int ret = 1;
1740: int i, j, k, dot;
1741: SSL *con;
1742: const SSL_CIPHER *c;
1743: BIO *io, *ssl_bio, *sbio;
1744:
1745: buf = malloc(bufsize);
1746: if (buf == NULL)
1747: return (0);
1748: io = BIO_new(BIO_f_buffer());
1749: ssl_bio = BIO_new(BIO_f_ssl());
1750: if ((io == NULL) || (ssl_bio == NULL))
1751: goto err;
1752:
1753: if (s_nbio) {
1754: if (!s_quiet)
1755: BIO_printf(bio_err, "turning on non blocking io\n");
1.2 bcook 1756: if (!BIO_socket_nbio(s, 1))
1.1 jsing 1757: ERR_print_errors(bio_err);
1758: }
1759:
1760: /* lets make the output buffer a reasonable size */
1761: if (!BIO_set_write_buffer_size(io, bufsize))
1762: goto err;
1763:
1764: if ((con = SSL_new(ctx)) == NULL)
1765: goto err;
1766: if (s_tlsextdebug) {
1767: SSL_set_tlsext_debug_callback(con, tlsext_cb);
1768: SSL_set_tlsext_debug_arg(con, bio_s_out);
1769: }
1770: if (context)
1771: SSL_set_session_id_context(con, context,
1772: strlen((char *) context));
1773:
1774: sbio = BIO_new_socket(s, BIO_NOCLOSE);
1775: if (s_nbio_test) {
1776: BIO *test;
1777:
1778: test = BIO_new(BIO_f_nbio_test());
1779: sbio = BIO_push(test, sbio);
1780: }
1781: SSL_set_bio(con, sbio, sbio);
1782: SSL_set_accept_state(con);
1783:
1784: /* SSL_set_fd(con,s); */
1785: BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
1786: BIO_push(io, ssl_bio);
1787:
1788: if (s_debug) {
1789: SSL_set_debug(con, 1);
1790: BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
1791: BIO_set_callback_arg(SSL_get_rbio(con), (char *) bio_s_out);
1792: }
1793: if (s_msg) {
1794: SSL_set_msg_callback(con, msg_cb);
1795: SSL_set_msg_callback_arg(con, bio_s_out);
1796: }
1797: for (;;) {
1798: i = BIO_gets(io, buf, bufsize - 1);
1799: if (i < 0) { /* error */
1800: if (!BIO_should_retry(io)) {
1801: if (!s_quiet)
1802: ERR_print_errors(bio_err);
1803: goto err;
1804: } else {
1805: BIO_printf(bio_s_out, "read R BLOCK\n");
1806: sleep(1);
1807: continue;
1808: }
1809: } else if (i == 0) { /* end of input */
1810: ret = 1;
1811: goto end;
1812: }
1813: /* else we have data */
1814: if (((www == 1) && (strncmp("GET ", buf, 4) == 0)) ||
1815: ((www == 2) && (strncmp("GET /stats ", buf, 11) == 0))) {
1816: char *p;
1817: X509 *peer;
1818: STACK_OF(SSL_CIPHER) * sk;
1819: static const char *space = " ";
1820:
1821: BIO_puts(io, "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
1822: BIO_puts(io, "<HTML><BODY BGCOLOR=\"#ffffff\">\n");
1823: BIO_puts(io, "<pre>\n");
1824: /* BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/
1825: BIO_puts(io, "\n");
1826: for (i = 0; i < local_argc; i++) {
1827: BIO_puts(io, local_argv[i]);
1828: BIO_write(io, " ", 1);
1829: }
1830: BIO_puts(io, "\n");
1831:
1832: BIO_printf(io,
1833: "Secure Renegotiation IS%s supported\n",
1834: SSL_get_secure_renegotiation_support(con) ?
1835: "" : " NOT");
1836:
1837: /*
1838: * The following is evil and should not really be
1839: * done
1840: */
1841: BIO_printf(io, "Ciphers supported in s_server binary\n");
1842: sk = SSL_get_ciphers(con);
1843: j = sk_SSL_CIPHER_num(sk);
1844: for (i = 0; i < j; i++) {
1845: c = sk_SSL_CIPHER_value(sk, i);
1846: BIO_printf(io, "%-11s:%-25s",
1847: SSL_CIPHER_get_version(c),
1848: SSL_CIPHER_get_name(c));
1849: if ((((i + 1) % 2) == 0) && (i + 1 != j))
1850: BIO_puts(io, "\n");
1851: }
1852: BIO_puts(io, "\n");
1853: p = SSL_get_shared_ciphers(con, buf, bufsize);
1854: if (p != NULL) {
1855: BIO_printf(io, "---\nCiphers common between both SSL end points:\n");
1856: j = i = 0;
1857: while (*p) {
1858: if (*p == ':') {
1859: BIO_write(io, space, 26 - j);
1860: i++;
1861: j = 0;
1862: BIO_write(io, ((i % 3) ? " " : "\n"), 1);
1863: } else {
1864: BIO_write(io, p, 1);
1865: j++;
1866: }
1867: p++;
1868: }
1869: BIO_puts(io, "\n");
1870: }
1871: BIO_printf(io, (SSL_cache_hit(con)
1872: ? "---\nReused, "
1873: : "---\nNew, "));
1874: c = SSL_get_current_cipher(con);
1875: BIO_printf(io, "%s, Cipher is %s\n",
1876: SSL_CIPHER_get_version(c),
1877: SSL_CIPHER_get_name(c));
1878: SSL_SESSION_print(io, SSL_get_session(con));
1879: BIO_printf(io, "---\n");
1880: print_stats(io, SSL_get_SSL_CTX(con));
1881: BIO_printf(io, "---\n");
1882: peer = SSL_get_peer_certificate(con);
1883: if (peer != NULL) {
1884: BIO_printf(io, "Client certificate\n");
1885: X509_print(io, peer);
1886: PEM_write_bio_X509(io, peer);
1887: } else
1888: BIO_puts(io, "no client certificate available\n");
1889: BIO_puts(io, "</BODY></HTML>\r\n\r\n");
1890: break;
1891: } else if ((www == 2 || www == 3)
1892: && (strncmp("GET /", buf, 5) == 0)) {
1893: BIO *file;
1894: char *p, *e;
1895: static const char *text = "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
1896:
1897: /* skip the '/' */
1898: p = &(buf[5]);
1899:
1900: dot = 1;
1901: for (e = p; *e != '\0'; e++) {
1902: if (e[0] == ' ')
1903: break;
1904:
1905: switch (dot) {
1906: case 1:
1907: dot = (e[0] == '.') ? 2 : 0;
1908: break;
1909: case 2:
1910: dot = (e[0] == '.') ? 3 : 0;
1911: break;
1912: case 3:
1913: dot = (e[0] == '/') ? -1 : 0;
1914: break;
1915: }
1916: if (dot == 0)
1917: dot = (e[0] == '/') ? 1 : 0;
1918: }
1919: dot = (dot == 3) || (dot == -1); /* filename contains
1920: * ".." component */
1921:
1922: if (*e == '\0') {
1923: BIO_puts(io, text);
1924: BIO_printf(io, "'%s' is an invalid file name\r\n", p);
1925: break;
1926: }
1927: *e = '\0';
1928:
1929: if (dot) {
1930: BIO_puts(io, text);
1931: BIO_printf(io, "'%s' contains '..' reference\r\n", p);
1932: break;
1933: }
1934: if (*p == '/') {
1935: BIO_puts(io, text);
1936: BIO_printf(io, "'%s' is an invalid path\r\n", p);
1937: break;
1938: }
1939: /* if a directory, do the index thang */
1940: if (app_isdir(p) > 0) {
1941: BIO_puts(io, text);
1942: BIO_printf(io, "'%s' is a directory\r\n", p);
1943: break;
1944: }
1945: if ((file = BIO_new_file(p, "r")) == NULL) {
1946: BIO_puts(io, text);
1947: BIO_printf(io, "Error opening '%s'\r\n", p);
1948: ERR_print_errors(io);
1949: break;
1950: }
1951: if (!s_quiet)
1952: BIO_printf(bio_err, "FILE:%s\n", p);
1953:
1954: if (www == 2) {
1955: i = strlen(p);
1956: if (((i > 5) && (strcmp(&(p[i - 5]), ".html") == 0)) ||
1957: ((i > 4) && (strcmp(&(p[i - 4]), ".php") == 0)) ||
1958: ((i > 4) && (strcmp(&(p[i - 4]), ".htm") == 0)))
1959: BIO_puts(io, "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
1960: else
1961: BIO_puts(io, "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
1962: }
1963: /* send the file */
1964: for (;;) {
1965: i = BIO_read(file, buf, bufsize);
1966: if (i <= 0)
1967: break;
1968:
1969: #ifdef RENEG
1970: total_bytes += i;
1971: fprintf(stderr, "%d\n", i);
1972: if (total_bytes > 3 * 1024) {
1973: total_bytes = 0;
1974: fprintf(stderr, "RENEGOTIATE\n");
1975: SSL_renegotiate(con);
1976: }
1977: #endif
1978:
1979: for (j = 0; j < i;) {
1980: #ifdef RENEG
1981: {
1982: static count = 0;
1983: if (++count == 13) {
1984: SSL_renegotiate(con);
1985: }
1986: }
1987: #endif
1988: k = BIO_write(io, &(buf[j]), i - j);
1989: if (k <= 0) {
1990: if (!BIO_should_retry(io))
1991: goto write_error;
1992: else {
1993: BIO_printf(bio_s_out, "rwrite W BLOCK\n");
1994: }
1995: } else {
1996: j += k;
1997: }
1998: }
1999: }
2000: write_error:
2001: BIO_free(file);
2002: break;
2003: }
2004: }
2005:
2006: for (;;) {
2007: i = (int) BIO_flush(io);
2008: if (i <= 0) {
2009: if (!BIO_should_retry(io))
2010: break;
2011: } else
2012: break;
2013: }
2014: end:
2015: /* make sure we re-use sessions */
2016: SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
2017:
2018: err:
2019:
2020: if (ret >= 0)
2021: BIO_printf(bio_s_out, "ACCEPT\n");
2022:
2023: if (buf != NULL)
2024: free(buf);
2025: if (io != NULL)
2026: BIO_free_all(io);
2027: /* if (ssl_bio != NULL) BIO_free(ssl_bio);*/
2028: return (ret);
2029: }
2030:
2031: #define MAX_SESSION_ID_ATTEMPTS 10
2032: static int
2033: generate_session_id(const SSL * ssl, unsigned char *id,
2034: unsigned int *id_len)
2035: {
2036: unsigned int count = 0;
2037: do {
1.3 jsing 2038: arc4random_buf(id, *id_len);
1.1 jsing 2039: /*
2040: * Prefix the session_id with the required prefix. NB: If our
2041: * prefix is too long, clip it - but there will be worse
2042: * effects anyway, eg. the server could only possibly create
2043: * 1 session ID (ie. the prefix!) so all future session
2044: * negotiations will fail due to conflicts.
2045: */
2046: memcpy(id, session_id_prefix,
2047: (strlen(session_id_prefix) < *id_len) ?
2048: strlen(session_id_prefix) : *id_len);
2049: }
2050: while (SSL_has_matching_session_id(ssl, id, *id_len) &&
2051: (++count < MAX_SESSION_ID_ATTEMPTS));
2052: if (count >= MAX_SESSION_ID_ATTEMPTS)
2053: return 0;
2054: return 1;
2055: }