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