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