2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
58 /* ====================================================================
59 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
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.
116 /* ====================================================================
117 * Copyright 2005 Nokia. All rights reserved.
119 * The portions of the attached software ("Contribution") is developed by
120 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
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.
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.
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.
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
149 #include <openssl/e_os2.h>
151 /* conflicts with winsock2 stuff on netware */
152 #if !defined(OPENSSL_SYS_NETWARE)
153 # include <sys/types.h>
157 * With IPv6, it looks like Digital has mixed up the proper order of
158 * recursive header file inclusion, resulting in the compiler complaining
159 * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
160 * needed to have fileno() declared correctly... So let's define u_int
162 #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
164 typedef unsigned int u_int;
167 #include <openssl/lhash.h>
168 #include <openssl/bn.h>
171 #include <openssl/err.h>
172 #include <openssl/pem.h>
173 #include <openssl/x509.h>
174 #include <openssl/ssl.h>
175 #include <openssl/rand.h>
176 #include <openssl/ocsp.h>
177 #ifndef OPENSSL_NO_DH
178 # include <openssl/dh.h>
180 #ifndef OPENSSL_NO_RSA
181 # include <openssl/rsa.h>
183 #ifndef OPENSSL_NO_SRP
184 # include <openssl/srp.h>
187 #include "timeouts.h"
189 #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
190 /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
194 #ifndef OPENSSL_NO_RSA
195 static RSA *tmp_rsa_cb(SSL *s, int is_export, int keylength);
197 static int not_resumable_sess_cb(SSL *s, int is_forward_secure);
198 static int sv_body(char *hostname, int s, int stype, unsigned char *context);
199 static int www_body(char *hostname, int s, int stype, unsigned char *context);
200 static int rev_body(char *hostname, int s, int stype, unsigned char *context);
201 static void close_accept_socket(void);
202 static void sv_usage(void);
203 static int init_ssl_connection(SSL *s);
204 static void print_stats(BIO *bp, SSL_CTX *ctx);
205 static int generate_session_id(const SSL *ssl, unsigned char *id,
206 unsigned int *id_len);
207 static void init_session_cache_ctx(SSL_CTX *sctx);
208 static void free_sessions(void);
209 #ifndef OPENSSL_NO_DH
210 static DH *load_dh_param(const char *dhfile);
214 static void s_server_init(void);
217 /* static int load_CA(SSL_CTX *ctx, char *file);*/
220 #define BUFSIZZ 16*1024
221 static int bufsize = BUFSIZZ;
222 static int accept_socket = -1;
224 #define TEST_CERT "server.pem"
225 #ifndef OPENSSL_NO_TLSEXT
226 # define TEST_CERT2 "server2.pem"
229 #define PROG s_server_main
231 extern int verify_depth, verify_return_error, verify_quiet;
233 static int s_server_verify = SSL_VERIFY_NONE;
234 static int s_server_session_id_context = 1; /* anything will do */
235 static const char *s_cert_file = TEST_CERT, *s_key_file =
236 NULL, *s_chain_file = NULL;
237 #ifndef OPENSSL_NO_TLSEXT
238 static const char *s_cert_file2 = TEST_CERT2, *s_key_file2 = NULL;
240 static char *s_dcert_file = NULL, *s_dkey_file = NULL, *s_dchain_file = NULL;
242 static int s_nbio = 0;
244 static int s_nbio_test = 0;
246 static SSL_CTX *ctx = NULL;
247 #ifndef OPENSSL_NO_TLSEXT
248 static SSL_CTX *ctx2 = NULL;
252 static BIO *bio_s_out = NULL;
253 static BIO *bio_s_msg = NULL;
254 static int s_debug = 0;
255 #ifndef OPENSSL_NO_TLSEXT
256 static int s_tlsextdebug = 0;
257 static int s_tlsextstatus = 0;
258 static int cert_status_cb(SSL *s, void *arg);
260 static int no_resume_ephemeral = 0;
261 static int s_msg = 0;
262 static int s_quiet = 0;
263 static int s_ign_eof = 0;
264 static int s_brief = 0;
266 static char *keymatexportlabel = NULL;
267 static int keymatexportlen = 20;
269 #ifndef OPENSSL_NO_ENGINE
270 static char *engine_id = NULL;
272 static const char *session_id_prefix = NULL;
274 static int enable_timeouts = 0;
275 static long socket_mtu;
276 #ifndef OPENSSL_NO_DTLS1
277 static int cert_chain = 0;
280 #ifndef OPENSSL_NO_TLSEXT
281 static BIO *serverinfo_in = NULL;
282 static const char *s_serverinfo_file = NULL;
286 #ifndef OPENSSL_NO_PSK
287 static char *psk_identity = "Client_identity";
288 char *psk_key = NULL; /* by default PSK is not used */
290 static unsigned int psk_server_cb(SSL *ssl, const char *identity,
292 unsigned int max_psk_len)
294 unsigned int psk_len = 0;
299 BIO_printf(bio_s_out, "psk_server_cb\n");
301 BIO_printf(bio_err, "Error: client did not send PSK identity\n");
305 BIO_printf(bio_s_out, "identity_len=%d identity=%s\n",
306 identity ? (int)strlen(identity) : 0, identity);
308 /* here we could lookup the given identity e.g. from a database */
309 if (strcmp(identity, psk_identity) != 0) {
310 BIO_printf(bio_s_out, "PSK error: client identity not found"
311 " (got '%s' expected '%s')\n", identity, psk_identity);
315 BIO_printf(bio_s_out, "PSK client identity found\n");
317 /* convert the PSK key to binary */
318 ret = BN_hex2bn(&bn, psk_key);
320 BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n",
326 if (BN_num_bytes(bn) > (int)max_psk_len) {
328 "psk buffer of callback is too small (%d) for key (%d)\n",
329 max_psk_len, BN_num_bytes(bn));
334 ret = BN_bn2bin(bn, psk);
339 psk_len = (unsigned int)ret;
342 BIO_printf(bio_s_out, "fetched PSK len=%d\n", psk_len);
346 BIO_printf(bio_err, "Error in PSK server callback\n");
351 #ifndef OPENSSL_NO_SRP
352 /* This is a context that we pass to callbacks */
353 typedef struct srpsrvparm_st {
360 * This callback pretends to require some asynchronous logic in order to
361 * obtain a verifier. When the callback is called for a new connection we
362 * return with a negative value. This will provoke the accept etc to return
363 * with an LOOKUP_X509. The main logic of the reinvokes the suspended call
364 * (which would normally occur after a worker has finished) and we set the
367 static int ssl_srp_server_param_cb(SSL *s, int *ad, void *arg)
369 srpsrvparm *p = (srpsrvparm *) arg;
370 if (p->login == NULL && p->user == NULL) {
371 p->login = SSL_get_srp_username(s);
372 BIO_printf(bio_err, "SRP username = \"%s\"\n", p->login);
376 if (p->user == NULL) {
377 BIO_printf(bio_err, "User %s doesn't exist\n", p->login);
378 return SSL3_AL_FATAL;
380 if (SSL_set_srp_server_param
381 (s, p->user->N, p->user->g, p->user->s, p->user->v,
382 p->user->info) < 0) {
383 *ad = SSL_AD_INTERNAL_ERROR;
384 return SSL3_AL_FATAL;
387 "SRP parameters set: username = \"%s\" info=\"%s\" \n",
388 p->login, p->user->info);
389 /* need to check whether there are memory leaks */
392 return SSL_ERROR_NONE;
398 static void s_server_init(void)
401 s_server_verify = SSL_VERIFY_NONE;
404 s_dchain_file = NULL;
405 s_cert_file = TEST_CERT;
408 # ifndef OPENSSL_NO_TLSEXT
409 s_cert_file2 = TEST_CERT2;
425 # ifndef OPENSSL_NO_ENGINE
431 static void sv_usage(void)
433 BIO_printf(bio_err, "usage: s_server [args ...]\n");
434 BIO_printf(bio_err, "\n");
436 " -accept port - TCP/IP port to accept on (default is %d)\n",
438 BIO_printf(bio_err, " -unix path - unix domain socket to accept on\n");
440 " -unlink - for -unix, unlink existing socket first\n");
441 BIO_printf(bio_err, " -context arg - set session ID context\n");
443 " -verify arg - turn on peer certificate verification\n");
445 " -Verify arg - turn on peer certificate verification, must have a cert.\n");
447 " -verify_return_error - return verification errors\n");
448 BIO_printf(bio_err, " -cert arg - certificate file to use\n");
449 BIO_printf(bio_err, " (default is %s)\n", TEST_CERT);
451 " -naccept arg - terminate after 'arg' connections\n");
452 #ifndef OPENSSL_NO_TLSEXT
454 " -serverinfo arg - PEM serverinfo file for certificate\n");
457 " -no_resumption_on_reneg - set SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION flag\n");
459 " -crl_check - check the peer certificate has not been revoked by its CA.\n"
460 " The CRL(s) are appended to the certificate file\n");
462 " -crl_check_all - check the peer certificate has not been revoked by its CA\n"
463 " or any other CRL in the CA chain. CRL(s) are appened to the\n"
464 " the certificate file.\n");
466 " -certform arg - certificate format (PEM or DER) PEM default\n");
468 " -key arg - Private Key file to use, in cert file if\n");
469 BIO_printf(bio_err, " not specified (default is %s)\n",
472 " -keyform arg - key format (PEM, DER or ENGINE) PEM default\n");
474 " -pass arg - private key file pass phrase source\n");
476 " -dcert arg - second certificate file to use (usually for DSA)\n");
478 " -dcertform x - second certificate format (PEM or DER) PEM default\n");
480 " -dkey arg - second private key file to use (usually for DSA)\n");
482 " -dkeyform arg - second key format (PEM, DER or ENGINE) PEM default\n");
484 " -dpass arg - second private key file pass phrase source\n");
486 " -dhparam arg - DH parameter file to use, in cert file if not specified\n");
488 " or a default set of parameters is used\n");
489 #ifndef OPENSSL_NO_ECDH
491 " -named_curve arg - Elliptic curve name to use for ephemeral ECDH keys.\n"
492 " Use \"openssl ecparam -list_curves\" for all names\n"
493 " (default is nistp256).\n");
496 BIO_printf(bio_err, " -nbio - Run with non-blocking IO\n");
499 " -nbio_test - test with the non-blocking test bio\n");
501 " -crlf - convert LF from terminal into CRLF\n");
502 BIO_printf(bio_err, " -debug - Print more output\n");
503 BIO_printf(bio_err, " -msg - Show protocol messages\n");
504 BIO_printf(bio_err, " -state - Print the SSL states\n");
505 BIO_printf(bio_err, " -CApath arg - PEM format directory of CA's\n");
506 BIO_printf(bio_err, " -CAfile arg - PEM format file of CA's\n");
508 " -trusted_first - Use locally trusted CA's first when building trust chain\n");
510 " -no_alt_chains - only ever use the first certificate chain found\n");
512 " -nocert - Don't use any certificates (Anon-DH)\n");
514 " -cipher arg - play with 'openssl ciphers' to see what goes here\n");
515 BIO_printf(bio_err, " -serverpref - Use server's cipher preferences\n");
516 BIO_printf(bio_err, " -quiet - No server output\n");
517 BIO_printf(bio_err, " -no_tmp_rsa - Do not generate a tmp RSA key\n");
518 #ifndef OPENSSL_NO_PSK
519 BIO_printf(bio_err, " -psk_hint arg - PSK identity hint to use\n");
520 BIO_printf(bio_err, " -psk arg - PSK in hex (without 0x)\n");
521 # ifndef OPENSSL_NO_JPAKE
522 BIO_printf(bio_err, " -jpake arg - JPAKE secret to use\n");
525 #ifndef OPENSSL_NO_SRP
526 BIO_printf(bio_err, " -srpvfile file - The verifier file for SRP\n");
528 " -srpuserseed string - A seed string for a default user salt.\n");
530 #ifndef OPENSSL_NO_SSL3_METHOD
531 BIO_printf(bio_err, " -ssl3 - Just talk SSLv3\n");
533 BIO_printf(bio_err, " -tls1_2 - Just talk TLSv1.2\n");
534 BIO_printf(bio_err, " -tls1_1 - Just talk TLSv1.1\n");
535 BIO_printf(bio_err, " -tls1 - Just talk TLSv1\n");
536 BIO_printf(bio_err, " -dtls1 - Just talk DTLSv1\n");
537 BIO_printf(bio_err, " -dtls1_2 - Just talk DTLSv1.2\n");
538 BIO_printf(bio_err, " -timeout - Enable timeouts\n");
539 BIO_printf(bio_err, " -mtu - Set link layer MTU\n");
540 BIO_printf(bio_err, " -chain - Read a certificate chain\n");
541 BIO_printf(bio_err, " -no_ssl3 - Just disable SSLv3\n");
542 BIO_printf(bio_err, " -no_tls1 - Just disable TLSv1\n");
543 BIO_printf(bio_err, " -no_tls1_1 - Just disable TLSv1.1\n");
544 BIO_printf(bio_err, " -no_tls1_2 - Just disable TLSv1.2\n");
545 #ifndef OPENSSL_NO_DH
546 BIO_printf(bio_err, " -no_dhe - Disable ephemeral DH\n");
548 #ifndef OPENSSL_NO_ECDH
549 BIO_printf(bio_err, " -no_ecdhe - Disable ephemeral ECDH\n");
552 "-no_resume_ephemeral - Disable caching and tickets if ephemeral (EC)DH is used\n");
553 BIO_printf(bio_err, " -bugs - Turn on SSL bug compatibility\n");
555 " -www - Respond to a 'GET /' with a status page\n");
557 " -WWW - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
559 " -HTTP - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
561 " with the assumption it contains a complete HTTP response.\n");
562 #ifndef OPENSSL_NO_ENGINE
564 " -engine id - Initialise and use the specified engine\n");
567 " -id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'\n");
568 BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
569 LIST_SEPARATOR_CHAR);
570 #ifndef OPENSSL_NO_TLSEXT
572 " -servername host - servername for HostName TLS extension\n");
574 " -servername_fatal - on mismatch send fatal alert (default warning alert)\n");
576 " -cert2 arg - certificate file to use for servername\n");
577 BIO_printf(bio_err, " (default is %s)\n", TEST_CERT2);
579 " -key2 arg - Private Key file to use for servername, in cert file if\n");
580 BIO_printf(bio_err, " not specified (default is %s)\n",
583 " -tlsextdebug - hex dump of all TLS extensions received\n");
585 " -no_ticket - disable use of RFC4507bis session tickets\n");
587 " -legacy_renegotiation - enable use of legacy renegotiation (dangerous)\n");
588 # ifndef OPENSSL_NO_NEXTPROTONEG
590 " -nextprotoneg arg - set the advertised protocols for the NPN extension (comma-separated list)\n");
592 # ifndef OPENSSL_NO_SRTP
594 " -use_srtp profiles - Offer SRTP key management with a colon-separated profile list\n");
597 " -alpn arg - set the advertised protocols for the ALPN extension (comma-separated list)\n");
600 " -keymatexport label - Export keying material using label\n");
602 " -keymatexportlen len - Export len bytes of keying material (default 20)\n");
604 " -status - respond to certificate status requests\n");
606 " -status_verbose - enable status request verbose printout\n");
608 " -status_timeout n - status request responder timeout\n");
609 BIO_printf(bio_err, " -status_url URL - status request fallback URL\n");
612 static int local_argc = 0;
613 static char **local_argv;
615 #ifdef CHARSET_EBCDIC
616 static int ebcdic_new(BIO *bi);
617 static int ebcdic_free(BIO *a);
618 static int ebcdic_read(BIO *b, char *out, int outl);
619 static int ebcdic_write(BIO *b, const char *in, int inl);
620 static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr);
621 static int ebcdic_gets(BIO *bp, char *buf, int size);
622 static int ebcdic_puts(BIO *bp, const char *str);
624 # define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
625 static BIO_METHOD methods_ebcdic = {
626 BIO_TYPE_EBCDIC_FILTER,
627 "EBCDIC/ASCII filter",
642 BIO_METHOD *BIO_f_ebcdic_filter()
644 return (&methods_ebcdic);
647 static int ebcdic_new(BIO *bi)
649 EBCDIC_OUTBUFF *wbuf;
651 wbuf = (EBCDIC_OUTBUFF *) OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + 1024);
652 wbuf->alloced = 1024;
653 wbuf->buff[0] = '\0';
655 bi->ptr = (char *)wbuf;
661 static int ebcdic_free(BIO *a)
666 OPENSSL_free(a->ptr);
673 static int ebcdic_read(BIO *b, char *out, int outl)
677 if (out == NULL || outl == 0)
679 if (b->next_bio == NULL)
682 ret = BIO_read(b->next_bio, out, outl);
684 ascii2ebcdic(out, out, ret);
688 static int ebcdic_write(BIO *b, const char *in, int inl)
690 EBCDIC_OUTBUFF *wbuf;
695 if ((in == NULL) || (inl <= 0))
697 if (b->next_bio == NULL)
700 wbuf = (EBCDIC_OUTBUFF *) b->ptr;
702 if (inl > (num = wbuf->alloced)) {
703 num = num + num; /* double the size */
708 (EBCDIC_OUTBUFF *) OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + num);
711 wbuf->buff[0] = '\0';
713 b->ptr = (char *)wbuf;
716 ebcdic2ascii(wbuf->buff, in, inl);
718 ret = BIO_write(b->next_bio, wbuf->buff, inl);
723 static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr)
727 if (b->next_bio == NULL)
734 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
740 static int ebcdic_gets(BIO *bp, char *buf, int size)
743 if (bp->next_bio == NULL)
745 /* return(BIO_gets(bp->next_bio,buf,size));*/
746 for (i = 0; i < size - 1; ++i) {
747 ret = ebcdic_read(bp, &buf[i], 1);
750 else if (buf[i] == '\n') {
757 return (ret < 0 && i == 0) ? ret : i;
760 static int ebcdic_puts(BIO *bp, const char *str)
762 if (bp->next_bio == NULL)
764 return ebcdic_write(bp, str, strlen(str));
768 #ifndef OPENSSL_NO_TLSEXT
770 /* This is a context that we pass to callbacks */
771 typedef struct tlsextctx_st {
777 static int ssl_servername_cb(SSL *s, int *ad, void *arg)
779 tlsextctx *p = (tlsextctx *) arg;
780 const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
781 if (servername && p->biodebug)
782 BIO_printf(p->biodebug, "Hostname in TLS extension: \"%s\"\n",
786 return SSL_TLSEXT_ERR_NOACK;
789 if (strcasecmp(servername, p->servername))
790 return p->extension_error;
792 BIO_printf(p->biodebug, "Switching server context.\n");
793 SSL_set_SSL_CTX(s, ctx2);
796 return SSL_TLSEXT_ERR_OK;
799 /* Structure passed to cert status callback */
801 typedef struct tlsextstatusctx_st {
802 /* Default responder to use */
803 char *host, *path, *port;
810 static tlsextstatusctx tlscstatp = { NULL, NULL, NULL, 0, -1, NULL, 0 };
813 * Certificate Status callback. This is called when a client includes a
814 * certificate status request extension. This is a simplified version. It
815 * examines certificates each time and makes one OCSP responder query for
816 * each request. A full version would store details such as the OCSP
817 * certificate IDs and minimise the number of OCSP responses by caching them
818 * until they were considered "expired".
821 static int cert_status_cb(SSL *s, void *arg)
823 tlsextstatusctx *srctx = arg;
824 BIO *err = srctx->err;
825 char *host, *port, *path;
827 unsigned char *rspder = NULL;
829 STACK_OF(OPENSSL_STRING) *aia = NULL;
831 X509_STORE_CTX inctx;
833 OCSP_REQUEST *req = NULL;
834 OCSP_RESPONSE *resp = NULL;
835 OCSP_CERTID *id = NULL;
836 STACK_OF(X509_EXTENSION) *exts;
837 int ret = SSL_TLSEXT_ERR_NOACK;
840 BIO_puts(err, "cert_status: callback called\n");
841 /* Build up OCSP query from server certificate */
842 x = SSL_get_certificate(s);
843 aia = X509_get1_ocsp(x);
845 if (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia, 0),
846 &host, &port, &path, &use_ssl)) {
847 BIO_puts(err, "cert_status: can't parse AIA URL\n");
851 BIO_printf(err, "cert_status: AIA URL: %s\n",
852 sk_OPENSSL_STRING_value(aia, 0));
856 "cert_status: no AIA and no default responder URL\n");
862 use_ssl = srctx->use_ssl;
865 if (!X509_STORE_CTX_init(&inctx,
866 SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)),
869 if (X509_STORE_get_by_subject(&inctx, X509_LU_X509,
870 X509_get_issuer_name(x), &obj) <= 0) {
871 BIO_puts(err, "cert_status: Can't retrieve issuer certificate.\n");
872 X509_STORE_CTX_cleanup(&inctx);
875 req = OCSP_REQUEST_new();
878 id = OCSP_cert_to_id(NULL, x, obj.data.x509);
879 X509_free(obj.data.x509);
880 X509_STORE_CTX_cleanup(&inctx);
883 if (!OCSP_request_add0_id(req, id))
886 /* Add any extensions to the request */
887 SSL_get_tlsext_status_exts(s, &exts);
888 for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
889 X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
890 if (!OCSP_REQUEST_add_ext(req, ext, -1))
893 resp = process_responder(err, req, host, path, port, use_ssl, NULL,
896 BIO_puts(err, "cert_status: error querying responder\n");
899 rspderlen = i2d_OCSP_RESPONSE(resp, &rspder);
902 SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen);
903 if (srctx->verbose) {
904 BIO_puts(err, "cert_status: ocsp response sent:\n");
905 OCSP_RESPONSE_print(err, resp, 2);
907 ret = SSL_TLSEXT_ERR_OK;
909 if (ret != SSL_TLSEXT_ERR_OK)
910 ERR_print_errors(err);
915 X509_email_free(aia);
918 OCSP_CERTID_free(id);
920 OCSP_REQUEST_free(req);
922 OCSP_RESPONSE_free(resp);
925 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
929 # ifndef OPENSSL_NO_NEXTPROTONEG
930 /* This is the context that we pass to next_proto_cb */
931 typedef struct tlsextnextprotoctx_st {
934 } tlsextnextprotoctx;
936 static int next_proto_cb(SSL *s, const unsigned char **data,
937 unsigned int *len, void *arg)
939 tlsextnextprotoctx *next_proto = arg;
941 *data = next_proto->data;
942 *len = next_proto->len;
944 return SSL_TLSEXT_ERR_OK;
946 # endif /* ndef OPENSSL_NO_NEXTPROTONEG */
948 /* This the context that we pass to alpn_cb */
949 typedef struct tlsextalpnctx_st {
954 static int alpn_cb(SSL *s, const unsigned char **out, unsigned char *outlen,
955 const unsigned char *in, unsigned int inlen, void *arg)
957 tlsextalpnctx *alpn_ctx = arg;
960 /* We can assume that |in| is syntactically valid. */
962 BIO_printf(bio_s_out, "ALPN protocols advertised by the client: ");
963 for (i = 0; i < inlen;) {
965 BIO_write(bio_s_out, ", ", 2);
966 BIO_write(bio_s_out, &in[i + 1], in[i]);
969 BIO_write(bio_s_out, "\n", 1);
972 if (SSL_select_next_proto
973 ((unsigned char **)out, outlen, alpn_ctx->data, alpn_ctx->len, in,
974 inlen) != OPENSSL_NPN_NEGOTIATED) {
975 return SSL_TLSEXT_ERR_NOACK;
979 BIO_printf(bio_s_out, "ALPN protocols selected: ");
980 BIO_write(bio_s_out, *out, *outlen);
981 BIO_write(bio_s_out, "\n", 1);
984 return SSL_TLSEXT_ERR_OK;
986 #endif /* ndef OPENSSL_NO_TLSEXT */
988 static int not_resumable_sess_cb(SSL *s, int is_forward_secure)
990 /* disable resumption for sessions with forward secure ciphers */
991 return is_forward_secure;
994 int MAIN(int, char **);
996 #ifndef OPENSSL_NO_JPAKE
997 static char *jpake_secret = NULL;
998 # define no_jpake !jpake_secret
1002 #ifndef OPENSSL_NO_SRP
1003 static srpsrvparm srp_callback_parm;
1005 #ifndef OPENSSL_NO_SRTP
1006 static char *srtp_profiles = NULL;
1009 int MAIN(int argc, char *argv[])
1011 X509_VERIFY_PARAM *vpm = NULL;
1014 const char *unix_path = NULL;
1016 int unlink_unix_path = 0;
1018 int (*server_cb) (char *hostname, int s, int stype,
1019 unsigned char *context);
1020 char *CApath = NULL, *CAfile = NULL;
1021 char *chCApath = NULL, *chCAfile = NULL;
1022 char *vfyCApath = NULL, *vfyCAfile = NULL;
1023 unsigned char *context = NULL;
1024 char *dhfile = NULL;
1027 int build_chain = 0;
1028 int no_tmp_rsa = 0, no_dhe = 0, no_ecdhe = 0, nocert = 0;
1030 const SSL_METHOD *meth = NULL;
1031 int socket_type = SOCK_STREAM;
1033 char *inrand = NULL;
1034 int s_cert_format = FORMAT_PEM, s_key_format = FORMAT_PEM;
1035 char *passarg = NULL, *pass = NULL;
1036 char *dpassarg = NULL, *dpass = NULL;
1037 int s_dcert_format = FORMAT_PEM, s_dkey_format = FORMAT_PEM;
1038 X509 *s_cert = NULL, *s_dcert = NULL;
1039 STACK_OF(X509) *s_chain = NULL, *s_dchain = NULL;
1040 EVP_PKEY *s_key = NULL, *s_dkey = NULL;
1041 int no_cache = 0, ext_cache = 0;
1042 int rev = 0, naccept = -1;
1044 #ifndef OPENSSL_NO_TLSEXT
1045 EVP_PKEY *s_key2 = NULL;
1046 X509 *s_cert2 = NULL;
1047 tlsextctx tlsextcbp = { NULL, NULL, SSL_TLSEXT_ERR_ALERT_WARNING };
1048 # ifndef OPENSSL_NO_NEXTPROTONEG
1049 const char *next_proto_neg_in = NULL;
1050 tlsextnextprotoctx next_proto = { NULL, 0 };
1052 const char *alpn_in = NULL;
1053 tlsextalpnctx alpn_ctx = { NULL, 0 };
1055 #ifndef OPENSSL_NO_PSK
1056 /* by default do not send a PSK identity hint */
1057 static char *psk_identity_hint = NULL;
1059 #ifndef OPENSSL_NO_SRP
1060 char *srpuserseed = NULL;
1061 char *srp_verifier_file = NULL;
1063 SSL_EXCERT *exc = NULL;
1064 SSL_CONF_CTX *cctx = NULL;
1065 STACK_OF(OPENSSL_STRING) *ssl_args = NULL;
1067 char *crl_file = NULL;
1068 int crl_format = FORMAT_PEM;
1069 int crl_download = 0;
1070 STACK_OF(X509_CRL) *crls = NULL;
1072 meth = SSLv23_server_method();
1082 if (bio_err == NULL)
1083 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
1085 if (!load_config(bio_err, NULL))
1088 cctx = SSL_CONF_CTX_new();
1091 SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER);
1092 SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CMDLINE);
1104 if ((strcmp(*argv, "-port") == 0) || (strcmp(*argv, "-accept") == 0)) {
1107 if (!extract_port(*(++argv), &port))
1109 } else if (strcmp(*argv, "-unix") == 0) {
1111 BIO_printf(bio_err, "unix domain sockets unsupported\n");
1116 unix_path = *(++argv);
1118 } else if (strcmp(*argv, "-unlink") == 0) {
1120 BIO_printf(bio_err, "unix domain sockets unsupported\n");
1123 unlink_unix_path = 1;
1125 } else if (strcmp(*argv, "-naccept") == 0) {
1128 naccept = atol(*(++argv));
1130 BIO_printf(bio_err, "bad accept value %s\n", *argv);
1133 } else if (strcmp(*argv, "-verify") == 0) {
1134 s_server_verify = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE;
1137 verify_depth = atoi(*(++argv));
1139 BIO_printf(bio_err, "verify depth is %d\n", verify_depth);
1140 } else if (strcmp(*argv, "-Verify") == 0) {
1142 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
1143 SSL_VERIFY_CLIENT_ONCE;
1146 verify_depth = atoi(*(++argv));
1149 "verify depth is %d, must return a certificate\n",
1151 } else if (strcmp(*argv, "-context") == 0) {
1154 context = (unsigned char *)*(++argv);
1155 } else if (strcmp(*argv, "-cert") == 0) {
1158 s_cert_file = *(++argv);
1159 } else if (strcmp(*argv, "-CRL") == 0) {
1162 crl_file = *(++argv);
1163 } else if (strcmp(*argv, "-crl_download") == 0)
1165 #ifndef OPENSSL_NO_TLSEXT
1166 else if (strcmp(*argv, "-serverinfo") == 0) {
1169 s_serverinfo_file = *(++argv);
1172 else if (strcmp(*argv, "-certform") == 0) {
1175 s_cert_format = str2fmt(*(++argv));
1176 } else if (strcmp(*argv, "-key") == 0) {
1179 s_key_file = *(++argv);
1180 } else if (strcmp(*argv, "-keyform") == 0) {
1183 s_key_format = str2fmt(*(++argv));
1184 } else if (strcmp(*argv, "-pass") == 0) {
1187 passarg = *(++argv);
1188 } else if (strcmp(*argv, "-cert_chain") == 0) {
1191 s_chain_file = *(++argv);
1192 } else if (strcmp(*argv, "-dhparam") == 0) {
1196 } else if (strcmp(*argv, "-dcertform") == 0) {
1199 s_dcert_format = str2fmt(*(++argv));
1200 } else if (strcmp(*argv, "-dcert") == 0) {
1203 s_dcert_file = *(++argv);
1204 } else if (strcmp(*argv, "-dkeyform") == 0) {
1207 s_dkey_format = str2fmt(*(++argv));
1208 } else if (strcmp(*argv, "-dpass") == 0) {
1211 dpassarg = *(++argv);
1212 } else if (strcmp(*argv, "-dkey") == 0) {
1215 s_dkey_file = *(++argv);
1216 } else if (strcmp(*argv, "-dcert_chain") == 0) {
1219 s_dchain_file = *(++argv);
1220 } else if (strcmp(*argv, "-nocert") == 0) {
1222 } else if (strcmp(*argv, "-CApath") == 0) {
1226 } else if (strcmp(*argv, "-chainCApath") == 0) {
1229 chCApath = *(++argv);
1230 } else if (strcmp(*argv, "-verifyCApath") == 0) {
1233 vfyCApath = *(++argv);
1234 } else if (strcmp(*argv, "-no_cache") == 0)
1236 else if (strcmp(*argv, "-ext_cache") == 0)
1238 else if (strcmp(*argv, "-CRLform") == 0) {
1241 crl_format = str2fmt(*(++argv));
1242 } else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm)) {
1246 } else if (args_excert(&argv, &argc, &badarg, bio_err, &exc)) {
1250 } else if (args_ssl(&argv, &argc, cctx, &badarg, bio_err, &ssl_args)) {
1254 } else if (strcmp(*argv, "-verify_return_error") == 0)
1255 verify_return_error = 1;
1256 else if (strcmp(*argv, "-verify_quiet") == 0)
1258 else if (strcmp(*argv, "-build_chain") == 0)
1260 else if (strcmp(*argv, "-CAfile") == 0) {
1264 } else if (strcmp(*argv, "-chainCAfile") == 0) {
1267 chCAfile = *(++argv);
1268 } else if (strcmp(*argv, "-verifyCAfile") == 0) {
1271 vfyCAfile = *(++argv);
1274 else if (strcmp(*argv, "-nbio") == 0) {
1278 else if (strcmp(*argv, "-nbio_test") == 0) {
1283 } else if (strcmp(*argv, "-ign_eof") == 0)
1285 else if (strcmp(*argv, "-no_ign_eof") == 0)
1287 else if (strcmp(*argv, "-debug") == 0) {
1290 #ifndef OPENSSL_NO_TLSEXT
1291 else if (strcmp(*argv, "-tlsextdebug") == 0)
1293 else if (strcmp(*argv, "-status") == 0)
1295 else if (strcmp(*argv, "-status_verbose") == 0) {
1297 tlscstatp.verbose = 1;
1298 } else if (!strcmp(*argv, "-status_timeout")) {
1302 tlscstatp.timeout = atoi(*(++argv));
1303 } else if (!strcmp(*argv, "-status_url")) {
1307 if (!OCSP_parse_url(*(++argv),
1310 &tlscstatp.path, &tlscstatp.use_ssl)) {
1311 BIO_printf(bio_err, "Error parsing URL\n");
1316 else if (strcmp(*argv, "-msg") == 0) {
1318 } else if (strcmp(*argv, "-msgfile") == 0) {
1321 bio_s_msg = BIO_new_file(*(++argv), "w");
1323 #ifndef OPENSSL_NO_SSL_TRACE
1324 else if (strcmp(*argv, "-trace") == 0) {
1328 else if (strcmp(*argv, "-security_debug") == 0) {
1330 } else if (strcmp(*argv, "-security_debug_verbose") == 0) {
1332 } else if (strcmp(*argv, "-state") == 0) {
1334 } else if (strcmp(*argv, "-crlf") == 0) {
1336 } else if (strcmp(*argv, "-quiet") == 0) {
1338 } else if (strcmp(*argv, "-brief") == 0) {
1342 } else if (strcmp(*argv, "-no_tmp_rsa") == 0) {
1344 } else if (strcmp(*argv, "-no_dhe") == 0) {
1346 } else if (strcmp(*argv, "-no_ecdhe") == 0) {
1348 } else if (strcmp(*argv, "-no_resume_ephemeral") == 0) {
1349 no_resume_ephemeral = 1;
1351 #ifndef OPENSSL_NO_PSK
1352 else if (strcmp(*argv, "-psk_hint") == 0) {
1355 psk_identity_hint = *(++argv);
1356 } else if (strcmp(*argv, "-psk") == 0) {
1361 psk_key = *(++argv);
1362 for (i = 0; i < strlen(psk_key); i++) {
1363 if (isxdigit((unsigned char)psk_key[i]))
1365 BIO_printf(bio_err, "Not a hex number '%s'\n", *argv);
1370 #ifndef OPENSSL_NO_SRP
1371 else if (strcmp(*argv, "-srpvfile") == 0) {
1374 srp_verifier_file = *(++argv);
1375 meth = TLSv1_server_method();
1376 } else if (strcmp(*argv, "-srpuserseed") == 0) {
1379 srpuserseed = *(++argv);
1380 meth = TLSv1_server_method();
1383 else if (strcmp(*argv, "-rev") == 0) {
1385 } else if (strcmp(*argv, "-www") == 0) {
1387 } else if (strcmp(*argv, "-WWW") == 0) {
1389 } else if (strcmp(*argv, "-HTTP") == 0) {
1392 #ifndef OPENSSL_NO_SSL3_METHOD
1393 else if (strcmp(*argv, "-ssl3") == 0) {
1394 meth = SSLv3_server_method();
1397 else if (strcmp(*argv, "-tls1") == 0) {
1398 meth = TLSv1_server_method();
1399 } else if (strcmp(*argv, "-tls1_1") == 0) {
1400 meth = TLSv1_1_server_method();
1401 } else if (strcmp(*argv, "-tls1_2") == 0) {
1402 meth = TLSv1_2_server_method();
1404 #ifndef OPENSSL_NO_DTLS1
1405 else if (strcmp(*argv, "-dtls") == 0) {
1406 meth = DTLS_server_method();
1407 socket_type = SOCK_DGRAM;
1408 } else if (strcmp(*argv, "-dtls1") == 0) {
1409 meth = DTLSv1_server_method();
1410 socket_type = SOCK_DGRAM;
1411 } else if (strcmp(*argv, "-dtls1_2") == 0) {
1412 meth = DTLSv1_2_server_method();
1413 socket_type = SOCK_DGRAM;
1414 } else if (strcmp(*argv, "-timeout") == 0)
1415 enable_timeouts = 1;
1416 else if (strcmp(*argv, "-mtu") == 0) {
1419 socket_mtu = atol(*(++argv));
1420 } else if (strcmp(*argv, "-chain") == 0)
1423 else if (strcmp(*argv, "-id_prefix") == 0) {
1426 session_id_prefix = *(++argv);
1428 #ifndef OPENSSL_NO_ENGINE
1429 else if (strcmp(*argv, "-engine") == 0) {
1432 engine_id = *(++argv);
1435 else if (strcmp(*argv, "-rand") == 0) {
1440 #ifndef OPENSSL_NO_TLSEXT
1441 else if (strcmp(*argv, "-servername") == 0) {
1444 tlsextcbp.servername = *(++argv);
1445 } else if (strcmp(*argv, "-servername_fatal") == 0) {
1446 tlsextcbp.extension_error = SSL_TLSEXT_ERR_ALERT_FATAL;
1447 } else if (strcmp(*argv, "-cert2") == 0) {
1450 s_cert_file2 = *(++argv);
1451 } else if (strcmp(*argv, "-key2") == 0) {
1454 s_key_file2 = *(++argv);
1456 # ifndef OPENSSL_NO_NEXTPROTONEG
1457 else if (strcmp(*argv, "-nextprotoneg") == 0) {
1460 next_proto_neg_in = *(++argv);
1463 else if (strcmp(*argv, "-alpn") == 0) {
1466 alpn_in = *(++argv);
1469 #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
1470 else if (strcmp(*argv, "-jpake") == 0) {
1473 jpake_secret = *(++argv);
1476 #ifndef OPENSSL_NO_SRTP
1477 else if (strcmp(*argv, "-use_srtp") == 0) {
1480 srtp_profiles = *(++argv);
1483 else if (strcmp(*argv, "-keymatexport") == 0) {
1486 keymatexportlabel = *(++argv);
1487 } else if (strcmp(*argv, "-keymatexportlen") == 0) {
1490 keymatexportlen = atoi(*(++argv));
1491 if (keymatexportlen == 0)
1494 BIO_printf(bio_err, "unknown option %s\n", *argv);
1506 #ifndef OPENSSL_NO_DTLS1
1507 if (www && socket_type == SOCK_DGRAM) {
1508 BIO_printf(bio_err, "Can't use -HTTP, -www or -WWW with DTLS\n");
1513 if (unix_path && (socket_type != SOCK_STREAM)) {
1515 "Can't use unix sockets and datagrams together\n");
1518 #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
1521 BIO_printf(bio_err, "Can't use JPAKE and PSK together\n");
1524 psk_identity = "JPAKE";
1528 SSL_load_error_strings();
1529 OpenSSL_add_ssl_algorithms();
1531 #ifndef OPENSSL_NO_ENGINE
1532 e = setup_engine(bio_err, engine_id, 1);
1535 if (!app_passwd(bio_err, passarg, dpassarg, &pass, &dpass)) {
1536 BIO_printf(bio_err, "Error getting password\n");
1540 if (s_key_file == NULL)
1541 s_key_file = s_cert_file;
1542 #ifndef OPENSSL_NO_TLSEXT
1543 if (s_key_file2 == NULL)
1544 s_key_file2 = s_cert_file2;
1547 if (!load_excert(&exc, bio_err))
1551 s_key = load_key(bio_err, s_key_file, s_key_format, 0, pass, e,
1552 "server certificate private key file");
1554 ERR_print_errors(bio_err);
1558 s_cert = load_cert(bio_err, s_cert_file, s_cert_format,
1559 NULL, e, "server certificate file");
1562 ERR_print_errors(bio_err);
1566 s_chain = load_certs(bio_err, s_chain_file, FORMAT_PEM,
1567 NULL, e, "server certificate chain");
1571 #ifndef OPENSSL_NO_TLSEXT
1572 if (tlsextcbp.servername) {
1573 s_key2 = load_key(bio_err, s_key_file2, s_key_format, 0, pass, e,
1574 "second server certificate private key file");
1576 ERR_print_errors(bio_err);
1580 s_cert2 = load_cert(bio_err, s_cert_file2, s_cert_format,
1581 NULL, e, "second server certificate file");
1584 ERR_print_errors(bio_err);
1588 #endif /* OPENSSL_NO_TLSEXT */
1590 #if !defined(OPENSSL_NO_TLSEXT)
1591 # if !defined(OPENSSL_NO_NEXTPROTONEG)
1592 if (next_proto_neg_in) {
1594 next_proto.data = next_protos_parse(&len, next_proto_neg_in);
1595 if (next_proto.data == NULL)
1597 next_proto.len = len;
1599 next_proto.data = NULL;
1602 alpn_ctx.data = NULL;
1605 alpn_ctx.data = next_protos_parse(&len, alpn_in);
1606 if (alpn_ctx.data == NULL)
1614 crl = load_crl(crl_file, crl_format);
1616 BIO_puts(bio_err, "Error loading CRL\n");
1617 ERR_print_errors(bio_err);
1620 crls = sk_X509_CRL_new_null();
1621 if (!crls || !sk_X509_CRL_push(crls, crl)) {
1622 BIO_puts(bio_err, "Error adding CRL\n");
1623 ERR_print_errors(bio_err);
1631 if (s_dkey_file == NULL)
1632 s_dkey_file = s_dcert_file;
1634 s_dkey = load_key(bio_err, s_dkey_file, s_dkey_format,
1635 0, dpass, e, "second certificate private key file");
1637 ERR_print_errors(bio_err);
1641 s_dcert = load_cert(bio_err, s_dcert_file, s_dcert_format,
1642 NULL, e, "second server certificate file");
1645 ERR_print_errors(bio_err);
1648 if (s_dchain_file) {
1649 s_dchain = load_certs(bio_err, s_dchain_file, FORMAT_PEM,
1650 NULL, e, "second server certificate chain");
1657 if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
1658 && !RAND_status()) {
1660 "warning, not much extra random data, consider using the -rand option\n");
1663 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
1664 app_RAND_load_files(inrand));
1666 if (bio_s_out == NULL) {
1667 if (s_quiet && !s_debug) {
1668 bio_s_out = BIO_new(BIO_s_null());
1669 if (s_msg && !bio_s_msg)
1670 bio_s_msg = BIO_new_fp(stdout, BIO_NOCLOSE);
1672 if (bio_s_out == NULL)
1673 bio_s_out = BIO_new_fp(stdout, BIO_NOCLOSE);
1676 #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
1682 s_dcert_file = NULL;
1684 #ifndef OPENSSL_NO_TLSEXT
1685 s_cert_file2 = NULL;
1690 ctx = SSL_CTX_new(meth);
1692 ssl_ctx_security_debug(ctx, bio_err, sdebug);
1694 ERR_print_errors(bio_err);
1697 if (session_id_prefix) {
1698 if (strlen(session_id_prefix) >= 32)
1700 "warning: id_prefix is too long, only one new session will be possible\n");
1701 if (!SSL_CTX_set_generate_session_id(ctx, generate_session_id)) {
1702 BIO_printf(bio_err, "error setting 'id_prefix'\n");
1703 ERR_print_errors(bio_err);
1706 BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
1708 SSL_CTX_set_quiet_shutdown(ctx, 1);
1710 ssl_ctx_set_excert(ctx, exc);
1713 SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
1715 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
1717 init_session_cache_ctx(ctx);
1719 SSL_CTX_sess_set_cache_size(ctx, 128);
1721 #ifndef OPENSSL_NO_SRTP
1722 if (srtp_profiles != NULL)
1723 SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles);
1726 if ((!SSL_CTX_load_verify_locations(ctx, CAfile, CApath)) ||
1727 (!SSL_CTX_set_default_verify_paths(ctx))) {
1728 /* BIO_printf(bio_err,"X509_load_verify_locations\n"); */
1729 ERR_print_errors(bio_err);
1733 SSL_CTX_set1_param(ctx, vpm);
1735 ssl_ctx_add_crls(ctx, crls, 0);
1736 if (!args_ssl_call(ctx, bio_err, cctx, ssl_args, no_ecdhe, no_jpake))
1739 if (!ssl_load_stores(ctx, vfyCApath, vfyCAfile, chCApath, chCAfile,
1740 crls, crl_download)) {
1741 BIO_printf(bio_err, "Error loading store locations\n");
1742 ERR_print_errors(bio_err);
1745 #ifndef OPENSSL_NO_TLSEXT
1747 ctx2 = SSL_CTX_new(meth);
1749 ERR_print_errors(bio_err);
1755 BIO_printf(bio_s_out, "Setting secondary ctx parameters\n");
1758 ssl_ctx_security_debug(ctx, bio_err, sdebug);
1760 if (session_id_prefix) {
1761 if (strlen(session_id_prefix) >= 32)
1763 "warning: id_prefix is too long, only one new session will be possible\n");
1764 if (!SSL_CTX_set_generate_session_id(ctx2, generate_session_id)) {
1765 BIO_printf(bio_err, "error setting 'id_prefix'\n");
1766 ERR_print_errors(bio_err);
1769 BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
1771 SSL_CTX_set_quiet_shutdown(ctx2, 1);
1773 ssl_ctx_set_excert(ctx2, exc);
1776 SSL_CTX_set_info_callback(ctx2, apps_ssl_info_callback);
1779 SSL_CTX_set_session_cache_mode(ctx2, SSL_SESS_CACHE_OFF);
1781 init_session_cache_ctx(ctx2);
1783 SSL_CTX_sess_set_cache_size(ctx2, 128);
1785 if ((!SSL_CTX_load_verify_locations(ctx2, CAfile, CApath)) ||
1786 (!SSL_CTX_set_default_verify_paths(ctx2))) {
1787 ERR_print_errors(bio_err);
1790 SSL_CTX_set1_param(ctx2, vpm);
1792 ssl_ctx_add_crls(ctx2, crls, 0);
1793 if (!args_ssl_call(ctx2, bio_err, cctx, ssl_args, no_ecdhe, no_jpake))
1796 # ifndef OPENSSL_NO_NEXTPROTONEG
1797 if (next_proto.data)
1798 SSL_CTX_set_next_protos_advertised_cb(ctx, next_proto_cb,
1802 SSL_CTX_set_alpn_select_cb(ctx, alpn_cb, &alpn_ctx);
1805 #ifndef OPENSSL_NO_DH
1810 dh = load_dh_param(dhfile);
1811 else if (s_cert_file)
1812 dh = load_dh_param(s_cert_file);
1815 BIO_printf(bio_s_out, "Setting temp DH parameters\n");
1817 BIO_printf(bio_s_out, "Using default temp DH parameters\n");
1819 (void)BIO_flush(bio_s_out);
1822 SSL_CTX_set_dh_auto(ctx, 1);
1823 else if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
1824 BIO_puts(bio_err, "Error setting temp DH parameters\n");
1825 ERR_print_errors(bio_err);
1829 # ifndef OPENSSL_NO_TLSEXT
1832 DH *dh2 = load_dh_param(s_cert_file2);
1834 BIO_printf(bio_s_out, "Setting temp DH parameters\n");
1835 (void)BIO_flush(bio_s_out);
1842 SSL_CTX_set_dh_auto(ctx2, 1);
1843 else if (!SSL_CTX_set_tmp_dh(ctx2, dh)) {
1844 BIO_puts(bio_err, "Error setting temp DH parameters\n");
1845 ERR_print_errors(bio_err);
1855 if (!set_cert_key_stuff(ctx, s_cert, s_key, s_chain, build_chain))
1857 #ifndef OPENSSL_NO_TLSEXT
1858 if (s_serverinfo_file != NULL
1859 && !SSL_CTX_use_serverinfo_file(ctx, s_serverinfo_file)) {
1860 ERR_print_errors(bio_err);
1864 #ifndef OPENSSL_NO_TLSEXT
1865 if (ctx2 && !set_cert_key_stuff(ctx2, s_cert2, s_key2, NULL, build_chain))
1868 if (s_dcert != NULL) {
1869 if (!set_cert_key_stuff(ctx, s_dcert, s_dkey, s_dchain, build_chain))
1872 #ifndef OPENSSL_NO_RSA
1874 SSL_CTX_set_tmp_rsa_callback(ctx, tmp_rsa_cb);
1875 # ifndef OPENSSL_NO_TLSEXT
1877 SSL_CTX_set_tmp_rsa_callback(ctx2, tmp_rsa_cb);
1882 if (no_resume_ephemeral) {
1883 SSL_CTX_set_not_resumable_session_callback(ctx,
1884 not_resumable_sess_cb);
1885 #ifndef OPENSSL_NO_TLSEXT
1887 SSL_CTX_set_not_resumable_session_callback(ctx2,
1888 not_resumable_sess_cb);
1891 #ifndef OPENSSL_NO_PSK
1892 # ifdef OPENSSL_NO_JPAKE
1893 if (psk_key != NULL)
1895 if (psk_key != NULL || jpake_secret)
1899 BIO_printf(bio_s_out,
1900 "PSK key given or JPAKE in use, setting server callback\n");
1901 SSL_CTX_set_psk_server_callback(ctx, psk_server_cb);
1904 if (!SSL_CTX_use_psk_identity_hint(ctx, psk_identity_hint)) {
1905 BIO_printf(bio_err, "error setting PSK identity hint to context\n");
1906 ERR_print_errors(bio_err);
1911 SSL_CTX_set_verify(ctx, s_server_verify, verify_callback);
1912 SSL_CTX_set_session_id_context(ctx, (void *)&s_server_session_id_context,
1913 sizeof s_server_session_id_context);
1915 /* Set DTLS cookie generation and verification callbacks */
1916 SSL_CTX_set_cookie_generate_cb(ctx, generate_cookie_callback);
1917 SSL_CTX_set_cookie_verify_cb(ctx, verify_cookie_callback);
1919 #ifndef OPENSSL_NO_TLSEXT
1921 SSL_CTX_set_verify(ctx2, s_server_verify, verify_callback);
1922 SSL_CTX_set_session_id_context(ctx2,
1923 (void *)&s_server_session_id_context,
1924 sizeof s_server_session_id_context);
1926 tlsextcbp.biodebug = bio_s_out;
1927 SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb);
1928 SSL_CTX_set_tlsext_servername_arg(ctx2, &tlsextcbp);
1929 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
1930 SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
1934 #ifndef OPENSSL_NO_SRP
1935 if (srp_verifier_file != NULL) {
1936 srp_callback_parm.vb = SRP_VBASE_new(srpuserseed);
1937 srp_callback_parm.user = NULL;
1938 srp_callback_parm.login = NULL;
1940 SRP_VBASE_init(srp_callback_parm.vb,
1941 srp_verifier_file)) != SRP_NO_ERROR) {
1943 "Cannot initialize SRP verifier file \"%s\":ret=%d\n",
1944 srp_verifier_file, ret);
1947 SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, verify_callback);
1948 SSL_CTX_set_srp_cb_arg(ctx, &srp_callback_parm);
1949 SSL_CTX_set_srp_username_callback(ctx, ssl_srp_server_param_cb);
1952 if (CAfile != NULL) {
1953 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(CAfile));
1954 #ifndef OPENSSL_NO_TLSEXT
1956 SSL_CTX_set_client_CA_list(ctx2, SSL_load_client_CA_file(CAfile));
1960 BIO_printf(bio_s_out, "ACCEPT\n");
1961 (void)BIO_flush(bio_s_out);
1963 server_cb = rev_body;
1965 server_cb = www_body;
1967 server_cb = sv_body;
1970 if (unlink_unix_path)
1972 do_server_unix(unix_path, &accept_socket, server_cb, context,
1976 do_server(port, socket_type, &accept_socket, server_cb, context,
1978 print_stats(bio_s_out, ctx);
1986 sk_X509_CRL_pop_free(crls, X509_CRL_free);
1990 EVP_PKEY_free(s_key);
1992 EVP_PKEY_free(s_dkey);
1994 sk_X509_pop_free(s_chain, X509_free);
1996 sk_X509_pop_free(s_dchain, X509_free);
2000 OPENSSL_free(dpass);
2002 X509_VERIFY_PARAM_free(vpm);
2004 #ifndef OPENSSL_NO_TLSEXT
2006 OPENSSL_free(tlscstatp.host);
2008 OPENSSL_free(tlscstatp.port);
2010 OPENSSL_free(tlscstatp.path);
2016 EVP_PKEY_free(s_key2);
2017 if (serverinfo_in != NULL)
2018 BIO_free(serverinfo_in);
2019 # ifndef OPENSSL_NO_NEXTPROTONEG
2020 if (next_proto.data)
2021 OPENSSL_free(next_proto.data);
2024 OPENSSL_free(alpn_ctx.data);
2026 ssl_excert_free(exc);
2028 sk_OPENSSL_STRING_free(ssl_args);
2030 SSL_CONF_CTX_free(cctx);
2031 #ifndef OPENSSL_NO_JPAKE
2032 if (jpake_secret && psk_key)
2033 OPENSSL_free(psk_key);
2035 if (bio_s_out != NULL) {
2036 BIO_free(bio_s_out);
2039 if (bio_s_msg != NULL) {
2040 BIO_free(bio_s_msg);
2047 static void print_stats(BIO *bio, SSL_CTX *ssl_ctx)
2049 BIO_printf(bio, "%4ld items in the session cache\n",
2050 SSL_CTX_sess_number(ssl_ctx));
2051 BIO_printf(bio, "%4ld client connects (SSL_connect())\n",
2052 SSL_CTX_sess_connect(ssl_ctx));
2053 BIO_printf(bio, "%4ld client renegotiates (SSL_connect())\n",
2054 SSL_CTX_sess_connect_renegotiate(ssl_ctx));
2055 BIO_printf(bio, "%4ld client connects that finished\n",
2056 SSL_CTX_sess_connect_good(ssl_ctx));
2057 BIO_printf(bio, "%4ld server accepts (SSL_accept())\n",
2058 SSL_CTX_sess_accept(ssl_ctx));
2059 BIO_printf(bio, "%4ld server renegotiates (SSL_accept())\n",
2060 SSL_CTX_sess_accept_renegotiate(ssl_ctx));
2061 BIO_printf(bio, "%4ld server accepts that finished\n",
2062 SSL_CTX_sess_accept_good(ssl_ctx));
2063 BIO_printf(bio, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx));
2064 BIO_printf(bio, "%4ld session cache misses\n",
2065 SSL_CTX_sess_misses(ssl_ctx));
2066 BIO_printf(bio, "%4ld session cache timeouts\n",
2067 SSL_CTX_sess_timeouts(ssl_ctx));
2068 BIO_printf(bio, "%4ld callback cache hits\n",
2069 SSL_CTX_sess_cb_hits(ssl_ctx));
2070 BIO_printf(bio, "%4ld cache full overflows (%ld allowed)\n",
2071 SSL_CTX_sess_cache_full(ssl_ctx),
2072 SSL_CTX_sess_get_cache_size(ssl_ctx));
2075 static int sv_body(char *hostname, int s, int stype, unsigned char *context)
2084 #ifndef OPENSSL_NO_KRB5
2087 struct timeval timeout;
2088 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)
2091 struct timeval *timeoutp;
2094 if ((buf = OPENSSL_malloc(bufsize)) == NULL) {
2095 BIO_printf(bio_err, "out of memory\n");
2100 unsigned long sl = 1;
2103 BIO_printf(bio_err, "turning on non blocking io\n");
2104 if (BIO_socket_ioctl(s, FIONBIO, &sl) < 0)
2105 ERR_print_errors(bio_err);
2111 #ifndef OPENSSL_NO_TLSEXT
2112 if (s_tlsextdebug) {
2113 SSL_set_tlsext_debug_callback(con, tlsext_cb);
2114 SSL_set_tlsext_debug_arg(con, bio_s_out);
2116 if (s_tlsextstatus) {
2117 SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb);
2118 tlscstatp.err = bio_err;
2119 SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp);
2122 #ifndef OPENSSL_NO_KRB5
2123 if ((kctx = kssl_ctx_new()) != NULL) {
2124 SSL_set0_kssl_ctx(con, kctx);
2125 kssl_ctx_setstring(kctx, KSSL_SERVICE, KRB5SVC);
2126 kssl_ctx_setstring(kctx, KSSL_KEYTAB, KRB5KEYTAB);
2128 #endif /* OPENSSL_NO_KRB5 */
2130 SSL_set_session_id_context(con, context, strlen((char *)context));
2134 if (stype == SOCK_DGRAM) {
2136 sbio = BIO_new_dgram(s, BIO_NOCLOSE);
2138 if (enable_timeouts) {
2140 timeout.tv_usec = DGRAM_RCV_TIMEOUT;
2141 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
2144 timeout.tv_usec = DGRAM_SND_TIMEOUT;
2145 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
2149 if (socket_mtu < DTLS_get_link_min_mtu(con)) {
2150 BIO_printf(bio_err, "MTU too small. Must be at least %ld\n",
2151 DTLS_get_link_min_mtu(con));
2156 SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
2157 if (!DTLS_set_link_mtu(con, socket_mtu)) {
2158 BIO_printf(bio_err, "Failed to set MTU\n");
2164 /* want to do MTU discovery */
2165 BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
2167 /* turn on cookie exchange */
2168 SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE);
2170 sbio = BIO_new_socket(s, BIO_NOCLOSE);
2175 test = BIO_new(BIO_f_nbio_test());
2176 sbio = BIO_push(test, sbio);
2178 #ifndef OPENSSL_NO_JPAKE
2180 jpake_server_auth(bio_s_out, sbio, jpake_secret);
2183 SSL_set_bio(con, sbio, sbio);
2184 SSL_set_accept_state(con);
2185 /* SSL_set_fd(con,s); */
2188 SSL_set_debug(con, 1);
2189 BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
2190 BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
2193 #ifndef OPENSSL_NO_SSL_TRACE
2195 SSL_set_msg_callback(con, SSL_trace);
2198 SSL_set_msg_callback(con, msg_cb);
2199 SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
2201 #ifndef OPENSSL_NO_TLSEXT
2202 if (s_tlsextdebug) {
2203 SSL_set_tlsext_debug_callback(con, tlsext_cb);
2204 SSL_set_tlsext_debug_arg(con, bio_s_out);
2210 int read_from_terminal;
2211 int read_from_sslcon;
2213 read_from_terminal = 0;
2214 read_from_sslcon = SSL_pending(con);
2216 if (!read_from_sslcon) {
2218 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE)
2219 openssl_fdset(fileno(stdin), &readfds);
2221 openssl_fdset(s, &readfds);
2223 * Note: under VMS with SOCKETSHR the second parameter is
2224 * currently of type (int *) whereas under other systems it is
2225 * (void *) if you don't have a cast it will choke the compiler:
2226 * if you do have a cast then you can either go for (int *) or
2229 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)
2231 * Under DOS (non-djgpp) and Windows we can't select on stdin:
2232 * only on sockets. As a workaround we timeout the select every
2233 * second and check for any keypress. In a proper Windows
2234 * application we wouldn't do this because it is inefficient.
2238 i = select(width, (void *)&readfds, NULL, NULL, &tv);
2239 if ((i < 0) || (!i && !_kbhit()))
2242 read_from_terminal = 1;
2244 if ((SSL_version(con) == DTLS1_VERSION) &&
2245 DTLSv1_get_timeout(con, &timeout))
2246 timeoutp = &timeout;
2250 i = select(width, (void *)&readfds, NULL, NULL, timeoutp);
2252 if ((SSL_version(con) == DTLS1_VERSION)
2253 && DTLSv1_handle_timeout(con) > 0) {
2254 BIO_printf(bio_err, "TIMEOUT occurred\n");
2259 if (FD_ISSET(fileno(stdin), &readfds))
2260 read_from_terminal = 1;
2262 if (FD_ISSET(s, &readfds))
2263 read_from_sslcon = 1;
2265 if (read_from_terminal) {
2269 i = raw_read_stdin(buf, bufsize / 2);
2271 /* both loops are skipped when i <= 0 */
2272 for (j = 0; j < i; j++)
2275 for (j = i - 1; j >= 0; j--) {
2276 buf[j + lf_num] = buf[j];
2277 if (buf[j] == '\n') {
2280 buf[j + lf_num] = '\r';
2283 assert(lf_num == 0);
2285 i = raw_read_stdin(buf, bufsize);
2286 if (!s_quiet && !s_brief) {
2287 if ((i <= 0) || (buf[0] == 'Q')) {
2288 BIO_printf(bio_s_out, "DONE\n");
2290 close_accept_socket();
2294 if ((i <= 0) || (buf[0] == 'q')) {
2295 BIO_printf(bio_s_out, "DONE\n");
2296 if (SSL_version(con) != DTLS1_VERSION)
2299 * close_accept_socket(); ret= -11;
2303 #ifndef OPENSSL_NO_HEARTBEATS
2304 if ((buf[0] == 'B') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
2305 BIO_printf(bio_err, "HEARTBEATING\n");
2311 if ((buf[0] == 'r') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
2312 SSL_renegotiate(con);
2313 i = SSL_do_handshake(con);
2314 printf("SSL_do_handshake -> %d\n", i);
2318 * strcpy(buf,"server side RE-NEGOTIATE\n");
2321 if ((buf[0] == 'R') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
2323 SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
2325 SSL_renegotiate(con);
2326 i = SSL_do_handshake(con);
2327 printf("SSL_do_handshake -> %d\n", i);
2331 * strcpy(buf,"server side RE-NEGOTIATE asking for client
2335 if (buf[0] == 'P') {
2336 static const char *str = "Lets print some clear text\n";
2337 BIO_write(SSL_get_wbio(con), str, strlen(str));
2339 if (buf[0] == 'S') {
2340 print_stats(bio_s_out, SSL_get_SSL_CTX(con));
2343 #ifdef CHARSET_EBCDIC
2344 ebcdic2ascii(buf, buf, i);
2348 /* should do a select for the write */
2352 if (++count == 100) {
2354 SSL_renegotiate(con);
2358 k = SSL_write(con, &(buf[l]), (unsigned int)i);
2359 #ifndef OPENSSL_NO_SRP
2360 while (SSL_get_error(con, k) == SSL_ERROR_WANT_X509_LOOKUP) {
2361 BIO_printf(bio_s_out, "LOOKUP renego during write\n");
2362 srp_callback_parm.user =
2363 SRP_VBASE_get_by_user(srp_callback_parm.vb,
2364 srp_callback_parm.login);
2365 if (srp_callback_parm.user)
2366 BIO_printf(bio_s_out, "LOOKUP done %s\n",
2367 srp_callback_parm.user->info);
2369 BIO_printf(bio_s_out, "LOOKUP not successful\n");
2370 k = SSL_write(con, &(buf[l]), (unsigned int)i);
2373 switch (SSL_get_error(con, k)) {
2374 case SSL_ERROR_NONE:
2376 case SSL_ERROR_WANT_WRITE:
2377 case SSL_ERROR_WANT_READ:
2378 case SSL_ERROR_WANT_X509_LOOKUP:
2379 BIO_printf(bio_s_out, "Write BLOCK\n");
2381 case SSL_ERROR_SYSCALL:
2383 BIO_printf(bio_s_out, "ERROR\n");
2384 ERR_print_errors(bio_err);
2388 case SSL_ERROR_ZERO_RETURN:
2389 BIO_printf(bio_s_out, "DONE\n");
2399 if (read_from_sslcon) {
2400 if (!SSL_is_init_finished(con)) {
2401 i = init_ssl_connection(con);
2406 } else if (i == 0) {
2412 i = SSL_read(con, (char *)buf, bufsize);
2413 #ifndef OPENSSL_NO_SRP
2414 while (SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) {
2415 BIO_printf(bio_s_out, "LOOKUP renego during read\n");
2416 srp_callback_parm.user =
2417 SRP_VBASE_get_by_user(srp_callback_parm.vb,
2418 srp_callback_parm.login);
2419 if (srp_callback_parm.user)
2420 BIO_printf(bio_s_out, "LOOKUP done %s\n",
2421 srp_callback_parm.user->info);
2423 BIO_printf(bio_s_out, "LOOKUP not successful\n");
2424 i = SSL_read(con, (char *)buf, bufsize);
2427 switch (SSL_get_error(con, i)) {
2428 case SSL_ERROR_NONE:
2429 #ifdef CHARSET_EBCDIC
2430 ascii2ebcdic(buf, buf, i);
2432 raw_write_stdout(buf, (unsigned int)i);
2433 if (SSL_pending(con))
2436 case SSL_ERROR_WANT_WRITE:
2437 case SSL_ERROR_WANT_READ:
2438 BIO_printf(bio_s_out, "Read BLOCK\n");
2440 case SSL_ERROR_SYSCALL:
2442 BIO_printf(bio_s_out, "ERROR\n");
2443 ERR_print_errors(bio_err);
2446 case SSL_ERROR_ZERO_RETURN:
2447 BIO_printf(bio_s_out, "DONE\n");
2456 BIO_printf(bio_s_out, "shutting down SSL\n");
2457 SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
2460 BIO_printf(bio_s_out, "CONNECTION CLOSED\n");
2462 OPENSSL_cleanse(buf, bufsize);
2466 BIO_printf(bio_s_out, "ACCEPT\n");
2470 static void close_accept_socket(void)
2472 BIO_printf(bio_err, "shutdown accept socket\n");
2473 if (accept_socket >= 0) {
2474 SHUTDOWN2(accept_socket);
2478 static int init_ssl_connection(SSL *con)
2485 #ifndef OPENSSL_NO_KRB5
2488 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
2489 const unsigned char *next_proto_neg;
2490 unsigned next_proto_neg_len;
2492 unsigned char *exportedkeymat;
2494 i = SSL_accept(con);
2495 #ifdef CERT_CB_TEST_RETRY
2497 while (i <= 0 && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP
2498 && SSL_state(con) == SSL3_ST_SR_CLNT_HELLO_C) {
2500 "LOOKUP from certificate callback during accept\n");
2501 i = SSL_accept(con);
2505 #ifndef OPENSSL_NO_SRP
2506 while (i <= 0 && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) {
2507 BIO_printf(bio_s_out, "LOOKUP during accept %s\n",
2508 srp_callback_parm.login);
2509 srp_callback_parm.user =
2510 SRP_VBASE_get_by_user(srp_callback_parm.vb,
2511 srp_callback_parm.login);
2512 if (srp_callback_parm.user)
2513 BIO_printf(bio_s_out, "LOOKUP done %s\n",
2514 srp_callback_parm.user->info);
2516 BIO_printf(bio_s_out, "LOOKUP not successful\n");
2517 i = SSL_accept(con);
2522 if (BIO_sock_should_retry(i)) {
2523 BIO_printf(bio_s_out, "DELAY\n");
2527 BIO_printf(bio_err, "ERROR\n");
2528 verify_error = SSL_get_verify_result(con);
2529 if (verify_error != X509_V_OK) {
2530 BIO_printf(bio_err, "verify error:%s\n",
2531 X509_verify_cert_error_string(verify_error));
2533 /* Always print any error messages */
2534 ERR_print_errors(bio_err);
2539 print_ssl_summary(bio_err, con);
2541 PEM_write_bio_SSL_SESSION(bio_s_out, SSL_get_session(con));
2543 peer = SSL_get_peer_certificate(con);
2545 BIO_printf(bio_s_out, "Client certificate\n");
2546 PEM_write_bio_X509(bio_s_out, peer);
2547 X509_NAME_oneline(X509_get_subject_name(peer), buf, sizeof buf);
2548 BIO_printf(bio_s_out, "subject=%s\n", buf);
2549 X509_NAME_oneline(X509_get_issuer_name(peer), buf, sizeof buf);
2550 BIO_printf(bio_s_out, "issuer=%s\n", buf);
2554 if (SSL_get_shared_ciphers(con, buf, sizeof buf) != NULL)
2555 BIO_printf(bio_s_out, "Shared ciphers:%s\n", buf);
2556 str = SSL_CIPHER_get_name(SSL_get_current_cipher(con));
2557 ssl_print_sigalgs(bio_s_out, con);
2558 #ifndef OPENSSL_NO_EC
2559 ssl_print_point_formats(bio_s_out, con);
2560 ssl_print_curves(bio_s_out, con, 0);
2562 BIO_printf(bio_s_out, "CIPHER is %s\n", (str != NULL) ? str : "(NONE)");
2564 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
2565 SSL_get0_next_proto_negotiated(con, &next_proto_neg, &next_proto_neg_len);
2566 if (next_proto_neg) {
2567 BIO_printf(bio_s_out, "NEXTPROTO is ");
2568 BIO_write(bio_s_out, next_proto_neg, next_proto_neg_len);
2569 BIO_printf(bio_s_out, "\n");
2572 #ifndef OPENSSL_NO_SRTP
2574 SRTP_PROTECTION_PROFILE *srtp_profile
2575 = SSL_get_selected_srtp_profile(con);
2578 BIO_printf(bio_s_out, "SRTP Extension negotiated, profile=%s\n",
2579 srtp_profile->name);
2582 if (SSL_cache_hit(con))
2583 BIO_printf(bio_s_out, "Reused session-id\n");
2584 if (SSL_ctrl(con, SSL_CTRL_GET_FLAGS, 0, NULL) &
2585 TLS1_FLAGS_TLS_PADDING_BUG)
2586 BIO_printf(bio_s_out, "Peer has incorrect TLSv1 block padding\n");
2587 #ifndef OPENSSL_NO_KRB5
2588 client_princ = kssl_ctx_get0_client_princ(SSL_get0_kssl_ctx(con));
2589 if (client_princ != NULL) {
2590 BIO_printf(bio_s_out, "Kerberos peer principal is %s\n",
2593 #endif /* OPENSSL_NO_KRB5 */
2594 BIO_printf(bio_s_out, "Secure Renegotiation IS%s supported\n",
2595 SSL_get_secure_renegotiation_support(con) ? "" : " NOT");
2596 if (keymatexportlabel != NULL) {
2597 BIO_printf(bio_s_out, "Keying material exporter:\n");
2598 BIO_printf(bio_s_out, " Label: '%s'\n", keymatexportlabel);
2599 BIO_printf(bio_s_out, " Length: %i bytes\n", keymatexportlen);
2600 exportedkeymat = OPENSSL_malloc(keymatexportlen);
2601 if (exportedkeymat != NULL) {
2602 if (!SSL_export_keying_material(con, exportedkeymat,
2605 strlen(keymatexportlabel),
2607 BIO_printf(bio_s_out, " Error\n");
2609 BIO_printf(bio_s_out, " Keying material: ");
2610 for (i = 0; i < keymatexportlen; i++)
2611 BIO_printf(bio_s_out, "%02X", exportedkeymat[i]);
2612 BIO_printf(bio_s_out, "\n");
2614 OPENSSL_free(exportedkeymat);
2621 #ifndef OPENSSL_NO_DH
2622 static DH *load_dh_param(const char *dhfile)
2627 if ((bio = BIO_new_file(dhfile, "r")) == NULL)
2629 ret = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
2637 static int www_body(char *hostname, int s, int stype, unsigned char *context)
2643 const SSL_CIPHER *c;
2644 BIO *io, *ssl_bio, *sbio;
2645 #ifndef OPENSSL_NO_KRB5
2649 buf = OPENSSL_malloc(bufsize);
2652 io = BIO_new(BIO_f_buffer());
2653 ssl_bio = BIO_new(BIO_f_ssl());
2654 if ((io == NULL) || (ssl_bio == NULL))
2659 unsigned long sl = 1;
2662 BIO_printf(bio_err, "turning on non blocking io\n");
2663 if (BIO_socket_ioctl(s, FIONBIO, &sl) < 0)
2664 ERR_print_errors(bio_err);
2668 /* lets make the output buffer a reasonable size */
2669 if (!BIO_set_write_buffer_size(io, bufsize))
2672 if ((con = SSL_new(ctx)) == NULL)
2674 #ifndef OPENSSL_NO_TLSEXT
2675 if (s_tlsextdebug) {
2676 SSL_set_tlsext_debug_callback(con, tlsext_cb);
2677 SSL_set_tlsext_debug_arg(con, bio_s_out);
2680 #ifndef OPENSSL_NO_KRB5
2681 if ((kctx = kssl_ctx_new()) != NULL) {
2682 kssl_ctx_setstring(kctx, KSSL_SERVICE, KRB5SVC);
2683 kssl_ctx_setstring(kctx, KSSL_KEYTAB, KRB5KEYTAB);
2685 #endif /* OPENSSL_NO_KRB5 */
2687 SSL_set_session_id_context(con, context, strlen((char *)context));
2689 sbio = BIO_new_socket(s, BIO_NOCLOSE);
2693 test = BIO_new(BIO_f_nbio_test());
2694 sbio = BIO_push(test, sbio);
2696 SSL_set_bio(con, sbio, sbio);
2697 SSL_set_accept_state(con);
2699 /* SSL_set_fd(con,s); */
2700 BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
2701 BIO_push(io, ssl_bio);
2702 #ifdef CHARSET_EBCDIC
2703 io = BIO_push(BIO_new(BIO_f_ebcdic_filter()), io);
2707 SSL_set_debug(con, 1);
2708 BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
2709 BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
2712 #ifndef OPENSSL_NO_SSL_TRACE
2714 SSL_set_msg_callback(con, SSL_trace);
2717 SSL_set_msg_callback(con, msg_cb);
2718 SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
2722 i = BIO_gets(io, buf, bufsize - 1);
2723 if (i < 0) { /* error */
2724 if (!BIO_should_retry(io)) {
2726 ERR_print_errors(bio_err);
2729 BIO_printf(bio_s_out, "read R BLOCK\n");
2730 #if defined(OPENSSL_SYS_NETWARE)
2732 #elif !defined(OPENSSL_SYS_MSDOS)
2737 } else if (i == 0) { /* end of input */
2742 /* else we have data */
2743 if (((www == 1) && (strncmp("GET ", buf, 4) == 0)) ||
2744 ((www == 2) && (strncmp("GET /stats ", buf, 10) == 0))) {
2747 STACK_OF(SSL_CIPHER) *sk;
2748 static const char *space = " ";
2750 if (www == 1 && strncmp("GET /reneg", buf, 10) == 0) {
2751 if (strncmp("GET /renegcert", buf, 14) == 0)
2753 SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
2755 i = SSL_renegotiate(con);
2756 BIO_printf(bio_s_out, "SSL_renegotiate -> %d\n", i);
2757 i = SSL_do_handshake(con);
2759 BIO_printf(bio_s_out, "SSL_do_handshake() Retval %d\n",
2760 SSL_get_error(con, i));
2761 ERR_print_errors(bio_err);
2765 SSL_set_state(con, SSL_ST_ACCEPT);
2766 i = SSL_do_handshake(con);
2767 BIO_printf(bio_s_out, "SSL_do_handshake -> %d\n", i);
2769 BIO_printf(bio_s_out, "SSL_do_handshake() Retval %d\n",
2770 SSL_get_error(con, i));
2771 ERR_print_errors(bio_err);
2777 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
2778 BIO_puts(io, "<HTML><BODY BGCOLOR=\"#ffffff\">\n");
2779 BIO_puts(io, "<pre>\n");
2780 /* BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/
2782 for (i = 0; i < local_argc; i++) {
2783 BIO_puts(io, local_argv[i]);
2784 BIO_write(io, " ", 1);
2789 "Secure Renegotiation IS%s supported\n",
2790 SSL_get_secure_renegotiation_support(con) ?
2794 * The following is evil and should not really be done
2796 BIO_printf(io, "Ciphers supported in s_server binary\n");
2797 sk = SSL_get_ciphers(con);
2798 j = sk_SSL_CIPHER_num(sk);
2799 for (i = 0; i < j; i++) {
2800 c = sk_SSL_CIPHER_value(sk, i);
2801 BIO_printf(io, "%-11s:%-25s",
2802 SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
2803 if ((((i + 1) % 2) == 0) && (i + 1 != j))
2807 p = SSL_get_shared_ciphers(con, buf, bufsize);
2810 "---\nCiphers common between both SSL end points:\n");
2814 BIO_write(io, space, 26 - j);
2817 BIO_write(io, ((i % 3) ? " " : "\n"), 1);
2819 BIO_write(io, p, 1);
2826 ssl_print_sigalgs(io, con);
2827 #ifndef OPENSSL_NO_EC
2828 ssl_print_curves(io, con, 0);
2830 BIO_printf(io, (SSL_cache_hit(con)
2831 ? "---\nReused, " : "---\nNew, "));
2832 c = SSL_get_current_cipher(con);
2833 BIO_printf(io, "%s, Cipher is %s\n",
2834 SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
2835 SSL_SESSION_print(io, SSL_get_session(con));
2836 BIO_printf(io, "---\n");
2837 print_stats(io, SSL_get_SSL_CTX(con));
2838 BIO_printf(io, "---\n");
2839 peer = SSL_get_peer_certificate(con);
2841 BIO_printf(io, "Client certificate\n");
2842 X509_print(io, peer);
2843 PEM_write_bio_X509(io, peer);
2845 BIO_puts(io, "no client certificate available\n");
2846 BIO_puts(io, "</BODY></HTML>\r\n\r\n");
2848 } else if ((www == 2 || www == 3)
2849 && (strncmp("GET /", buf, 5) == 0)) {
2852 static const char *text =
2853 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
2859 for (e = p; *e != '\0'; e++) {
2865 dot = (e[0] == '.') ? 2 : 0;
2868 dot = (e[0] == '.') ? 3 : 0;
2871 dot = (e[0] == '/') ? -1 : 0;
2875 dot = (e[0] == '/') ? 1 : 0;
2877 dot = (dot == 3) || (dot == -1); /* filename contains ".."
2882 BIO_printf(io, "'%s' is an invalid file name\r\n", p);
2889 BIO_printf(io, "'%s' contains '..' reference\r\n", p);
2895 BIO_printf(io, "'%s' is an invalid path\r\n", p);
2899 /* if a directory, do the index thang */
2900 if (app_isdir(p) > 0) {
2902 BIO_printf(io, "'%s' is a directory\r\n", p);
2906 if ((file = BIO_new_file(p, "r")) == NULL) {
2908 BIO_printf(io, "Error opening '%s'\r\n", p);
2909 ERR_print_errors(io);
2914 BIO_printf(bio_err, "FILE:%s\n", p);
2918 if (((i > 5) && (strcmp(&(p[i - 5]), ".html") == 0)) ||
2919 ((i > 4) && (strcmp(&(p[i - 4]), ".php") == 0)) ||
2920 ((i > 4) && (strcmp(&(p[i - 4]), ".htm") == 0)))
2922 "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
2925 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
2929 i = BIO_read(file, buf, bufsize);
2935 fprintf(stderr, "%d\n", i);
2936 if (total_bytes > 3 * 1024) {
2938 fprintf(stderr, "RENEGOTIATE\n");
2939 SSL_renegotiate(con);
2943 for (j = 0; j < i;) {
2947 if (++count == 13) {
2948 SSL_renegotiate(con);
2952 k = BIO_write(io, &(buf[j]), i - j);
2954 if (!BIO_should_retry(io))
2957 BIO_printf(bio_s_out, "rwrite W BLOCK\n");
2971 i = (int)BIO_flush(io);
2973 if (!BIO_should_retry(io))
2979 /* make sure we re-use sessions */
2980 SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
2985 BIO_printf(bio_s_out, "ACCEPT\n");
2991 /* if (ssl_bio != NULL) BIO_free(ssl_bio);*/
2995 static int rev_body(char *hostname, int s, int stype, unsigned char *context)
3001 BIO *io, *ssl_bio, *sbio;
3002 #ifndef OPENSSL_NO_KRB5
3006 buf = OPENSSL_malloc(bufsize);
3009 io = BIO_new(BIO_f_buffer());
3010 ssl_bio = BIO_new(BIO_f_ssl());
3011 if ((io == NULL) || (ssl_bio == NULL))
3014 /* lets make the output buffer a reasonable size */
3015 if (!BIO_set_write_buffer_size(io, bufsize))
3018 if ((con = SSL_new(ctx)) == NULL)
3020 #ifndef OPENSSL_NO_TLSEXT
3021 if (s_tlsextdebug) {
3022 SSL_set_tlsext_debug_callback(con, tlsext_cb);
3023 SSL_set_tlsext_debug_arg(con, bio_s_out);
3026 #ifndef OPENSSL_NO_KRB5
3027 if ((kctx = kssl_ctx_new()) != NULL) {
3028 kssl_ctx_setstring(kctx, KSSL_SERVICE, KRB5SVC);
3029 kssl_ctx_setstring(kctx, KSSL_KEYTAB, KRB5KEYTAB);
3031 #endif /* OPENSSL_NO_KRB5 */
3033 SSL_set_session_id_context(con, context, strlen((char *)context));
3035 sbio = BIO_new_socket(s, BIO_NOCLOSE);
3036 SSL_set_bio(con, sbio, sbio);
3037 SSL_set_accept_state(con);
3039 BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
3040 BIO_push(io, ssl_bio);
3041 #ifdef CHARSET_EBCDIC
3042 io = BIO_push(BIO_new(BIO_f_ebcdic_filter()), io);
3046 SSL_set_debug(con, 1);
3047 BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
3048 BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
3051 #ifndef OPENSSL_NO_SSL_TRACE
3053 SSL_set_msg_callback(con, SSL_trace);
3056 SSL_set_msg_callback(con, msg_cb);
3057 SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
3061 i = BIO_do_handshake(io);
3064 if (!BIO_should_retry(io)) {
3065 BIO_puts(bio_err, "CONNECTION FAILURE\n");
3066 ERR_print_errors(bio_err);
3070 BIO_printf(bio_err, "CONNECTION ESTABLISHED\n");
3071 print_ssl_summary(bio_err, con);
3074 i = BIO_gets(io, buf, bufsize - 1);
3075 if (i < 0) { /* error */
3076 if (!BIO_should_retry(io)) {
3078 ERR_print_errors(bio_err);
3081 BIO_printf(bio_s_out, "read R BLOCK\n");
3082 #if defined(OPENSSL_SYS_NETWARE)
3084 #elif !defined(OPENSSL_SYS_MSDOS)
3089 } else if (i == 0) { /* end of input */
3091 BIO_printf(bio_err, "CONNECTION CLOSED\n");
3094 char *p = buf + i - 1;
3095 while (i && (*p == '\n' || *p == '\r')) {
3099 if (!s_ign_eof && i == 5 && !strncmp(buf, "CLOSE", 5)) {
3101 BIO_printf(bio_err, "CONNECTION CLOSED\n");
3104 BUF_reverse((unsigned char *)buf, NULL, i);
3106 BIO_write(io, buf, i + 1);
3111 if (!BIO_should_retry(io))
3117 /* make sure we re-use sessions */
3118 SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
3129 #ifndef OPENSSL_NO_RSA
3130 static RSA *tmp_rsa_cb(SSL *s, int is_export, int keylength)
3133 static RSA *rsa_tmp = NULL;
3135 if (!rsa_tmp && ((bn = BN_new()) == NULL))
3136 BIO_printf(bio_err, "Allocation error in generating RSA key\n");
3137 if (!rsa_tmp && bn) {
3139 BIO_printf(bio_err, "Generating temp (%d bit) RSA key...",
3141 (void)BIO_flush(bio_err);
3143 if (!BN_set_word(bn, RSA_F4) || ((rsa_tmp = RSA_new()) == NULL) ||
3144 !RSA_generate_key_ex(rsa_tmp, keylength, bn, NULL)) {
3150 BIO_printf(bio_err, "\n");
3151 (void)BIO_flush(bio_err);
3159 #define MAX_SESSION_ID_ATTEMPTS 10
3160 static int generate_session_id(const SSL *ssl, unsigned char *id,
3161 unsigned int *id_len)
3163 unsigned int count = 0;
3165 RAND_pseudo_bytes(id, *id_len);
3167 * Prefix the session_id with the required prefix. NB: If our prefix
3168 * is too long, clip it - but there will be worse effects anyway, eg.
3169 * the server could only possibly create 1 session ID (ie. the
3170 * prefix!) so all future session negotiations will fail due to
3173 memcpy(id, session_id_prefix,
3174 (strlen(session_id_prefix) < *id_len) ?
3175 strlen(session_id_prefix) : *id_len);
3177 while (SSL_has_matching_session_id(ssl, id, *id_len) &&
3178 (++count < MAX_SESSION_ID_ATTEMPTS));
3179 if (count >= MAX_SESSION_ID_ATTEMPTS)
3185 * By default s_server uses an in-memory cache which caches SSL_SESSION
3186 * structures without any serialisation. This hides some bugs which only
3187 * become apparent in deployed servers. By implementing a basic external
3188 * session cache some issues can be debugged using s_server.
3191 typedef struct simple_ssl_session_st {
3196 struct simple_ssl_session_st *next;
3197 } simple_ssl_session;
3199 static simple_ssl_session *first = NULL;
3201 static int add_session(SSL *ssl, SSL_SESSION *session)
3203 simple_ssl_session *sess;
3206 sess = OPENSSL_malloc(sizeof(simple_ssl_session));
3208 SSL_SESSION_get_id(session, &sess->idlen);
3209 sess->derlen = i2d_SSL_SESSION(session, NULL);
3211 sess->id = BUF_memdup(SSL_SESSION_get_id(session, NULL), sess->idlen);
3213 sess->der = OPENSSL_malloc(sess->derlen);
3215 i2d_SSL_SESSION(session, &p);
3219 BIO_printf(bio_err, "New session added to external cache\n");
3223 static SSL_SESSION *get_session(SSL *ssl, unsigned char *id, int idlen,
3226 simple_ssl_session *sess;
3228 for (sess = first; sess; sess = sess->next) {
3229 if (idlen == (int)sess->idlen && !memcmp(sess->id, id, idlen)) {
3230 const unsigned char *p = sess->der;
3231 BIO_printf(bio_err, "Lookup session: cache hit\n");
3232 return d2i_SSL_SESSION(NULL, &p, sess->derlen);
3235 BIO_printf(bio_err, "Lookup session: cache miss\n");
3239 static void del_session(SSL_CTX *sctx, SSL_SESSION *session)
3241 simple_ssl_session *sess, *prev = NULL;
3242 const unsigned char *id;
3244 id = SSL_SESSION_get_id(session, &idlen);
3245 for (sess = first; sess; sess = sess->next) {
3246 if (idlen == sess->idlen && !memcmp(sess->id, id, idlen)) {
3248 prev->next = sess->next;
3251 OPENSSL_free(sess->id);
3252 OPENSSL_free(sess->der);
3260 static void init_session_cache_ctx(SSL_CTX *sctx)
3262 SSL_CTX_set_session_cache_mode(sctx,
3263 SSL_SESS_CACHE_NO_INTERNAL |
3264 SSL_SESS_CACHE_SERVER);
3265 SSL_CTX_sess_set_new_cb(sctx, add_session);
3266 SSL_CTX_sess_set_get_cb(sctx, get_session);
3267 SSL_CTX_sess_set_remove_cb(sctx, del_session);
3270 static void free_sessions(void)
3272 simple_ssl_session *sess, *tsess;
3273 for (sess = first; sess;) {
3274 OPENSSL_free(sess->id);
3275 OPENSSL_free(sess->der);
3278 OPENSSL_free(tsess);