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