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