145 #ifdef OPENSSL_NO_DEPRECATED
146 #undef OPENSSL_NO_DEPRECATED
156 #ifdef OPENSSL_NO_STDIO
160 #if !defined(OPENSSL_SYS_NETWARE)
161 #include <sys/types.h>
168 #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
170 typedef unsigned int u_int;
183 #ifndef OPENSSL_NO_DH
186 #ifndef OPENSSL_NO_RSA
189 #ifndef OPENSSL_NO_SRP
195 #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
200 #if defined(OPENSSL_SYS_BEOS_R5)
204 #ifndef OPENSSL_NO_RSA
207 static int sv_body(
char *hostname,
int s,
unsigned char *context);
208 static int www_body(
char *hostname,
int s,
unsigned char *context);
209 static void close_accept_socket(
void );
210 static void sv_usage(
void);
211 static int init_ssl_connection(
SSL *s);
213 static int generate_session_id(
const SSL *ssl,
unsigned char *
id,
214 unsigned int *id_len);
215 #ifndef OPENSSL_NO_DH
216 static DH *load_dh_param(
const char *dhfile);
217 static DH *get_dh512(
void);
221 static void s_server_init(
void);
224 #ifndef OPENSSL_NO_DH
225 static unsigned char dh512_p[]={
226 0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
227 0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
228 0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3,
229 0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12,
230 0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C,
233 static unsigned char dh512_g[]={
237 static DH *get_dh512(
void)
241 if ((dh=
DH_new()) == NULL)
return(NULL);
242 dh->
p=
BN_bin2bn(dh512_p,
sizeof(dh512_p),NULL);
243 dh->
g=
BN_bin2bn(dh512_g,
sizeof(dh512_g),NULL);
244 if ((dh->
p == NULL) || (dh->
g == NULL))
254 #define BUFSIZZ 16*1024
256 static int accept_socket= -1;
258 #define TEST_CERT "server.pem"
259 #ifndef OPENSSL_NO_TLSEXT
260 #define TEST_CERT2 "server2.pem"
263 #define PROG s_server_main
267 static char *cipher=NULL;
269 static int s_server_session_id_context = 1;
270 static const char *s_cert_file=
TEST_CERT,*s_key_file=NULL;
271 #ifndef OPENSSL_NO_TLSEXT
272 static const char *s_cert_file2=
TEST_CERT2,*s_key_file2=NULL;
274 static char *s_dcert_file=NULL,*s_dkey_file=NULL;
278 static int s_nbio_test=0;
281 #ifndef OPENSSL_NO_TLSEXT
286 static BIO *bio_s_out=NULL;
287 static int s_debug=0;
288 #ifndef OPENSSL_NO_TLSEXT
289 static int s_tlsextdebug=0;
290 static int s_tlsextstatus=0;
291 static int cert_status_cb(
SSL *s,
void *
arg);
294 static int s_quiet=0;
296 static char *keymatexportlabel=NULL;
297 static int keymatexportlen=20;
300 #ifndef OPENSSL_NO_ENGINE
301 static char *engine_id=NULL;
303 static const char *session_id_prefix=NULL;
305 static int enable_timeouts = 0;
306 static long socket_mtu;
307 #ifndef OPENSSL_NO_DTLS1
308 static int cert_chain = 0;
312 #ifndef OPENSSL_NO_PSK
313 static char *psk_identity=
"Client_identity";
316 static unsigned int psk_server_cb(
SSL *ssl,
const char *identity,
317 unsigned char *psk,
unsigned int max_psk_len)
319 unsigned int psk_len = 0;
331 BIO_printf(bio_s_out,
"identity_len=%d identity=%s\n",
332 identity ? (
int)strlen(identity) : 0, identity);
335 if (strcmp(identity, psk_identity) != 0)
337 BIO_printf(bio_s_out,
"PSK error: client identity not found"
338 " (got '%s' expected '%s')\n", identity,
343 BIO_printf(bio_s_out,
"PSK client identity found\n");
367 psk_len = (
unsigned int)ret;
370 BIO_printf(bio_s_out,
"fetched PSK len=%d\n", psk_len);
379 #ifndef OPENSSL_NO_SRP
398 if (p->
login == NULL && p->
user == NULL )
426 static void s_server_init(
void)
435 #ifndef OPENSSL_NO_TLSEXT
452 #ifndef OPENSSL_NO_ENGINE
458 static void sv_usage(
void)
465 BIO_printf(
bio_err,
" -Verify arg - turn on peer certificate verification, must have a cert.\n");
468 BIO_printf(
bio_err,
" -crl_check - check the peer certificate has not been revoked by its CA.\n" \
469 " The CRL(s) are appended to the certificate file\n");
470 BIO_printf(
bio_err,
" -crl_check_all - check the peer certificate has not been revoked by its CA\n" \
471 " or any other CRL in the CA chain. CRL(s) are appened to the\n" \
472 " the certificate file.\n");
473 BIO_printf(
bio_err,
" -certform arg - certificate format (PEM or DER) PEM default\n");
476 BIO_printf(
bio_err,
" -keyform arg - key format (PEM, DER or ENGINE) PEM default\n");
478 BIO_printf(
bio_err,
" -dcert arg - second certificate file to use (usually for DSA)\n");
479 BIO_printf(
bio_err,
" -dcertform x - second certificate format (PEM or DER) PEM default\n");
480 BIO_printf(
bio_err,
" -dkey arg - second private key file to use (usually for DSA)\n");
481 BIO_printf(
bio_err,
" -dkeyform arg - second key format (PEM, DER or ENGINE) PEM default\n");
482 BIO_printf(
bio_err,
" -dpass arg - second private key file pass phrase source\n");
483 BIO_printf(
bio_err,
" -dhparam arg - DH parameter file to use, in cert file if not specified\n");
485 #ifndef OPENSSL_NO_ECDH
486 BIO_printf(
bio_err,
" -named_curve arg - Elliptic curve name to use for ephemeral ECDH keys.\n" \
487 " Use \"openssl ecparam -list_curves\" for all names\n" \
488 " (default is nistp256).\n");
501 BIO_printf(
bio_err,
" -cipher arg - play with 'openssl ciphers' to see what goes here\n");
505 #ifndef OPENSSL_NO_PSK
508 # ifndef OPENSSL_NO_JPAKE
512 #ifndef OPENSSL_NO_SRP
514 BIO_printf(
bio_err,
" -srpuserseed string - A seed string for a default user salt.\n");
530 #ifndef OPENSSL_NO_DH
533 #ifndef OPENSSL_NO_ECDH
538 BIO_printf(
bio_err,
" -WWW - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
539 BIO_printf(
bio_err,
" -HTTP - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
540 BIO_printf(
bio_err,
" with the assumption it contains a complete HTTP response.\n");
541 #ifndef OPENSSL_NO_ENGINE
544 BIO_printf(
bio_err,
" -id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'\n");
546 #ifndef OPENSSL_NO_TLSEXT
547 BIO_printf(
bio_err,
" -servername host - servername for HostName TLS extension\n");
548 BIO_printf(
bio_err,
" -servername_fatal - on mismatch send fatal alert (default warning alert)\n");
551 BIO_printf(
bio_err,
" -key2 arg - Private Key file to use for servername, in cert file if\n");
555 BIO_printf(
bio_err,
" -legacy_renegotiation - enable use of legacy renegotiation (dangerous)\n");
556 # ifndef OPENSSL_NO_NEXTPROTONEG
557 BIO_printf(
bio_err,
" -nextprotoneg arg - set the advertised protocols for the NPN extension (comma-separated list)\n");
559 BIO_printf(
bio_err,
" -use_srtp profiles - Offer SRTP key management with a colon-separated profile list\n");
561 BIO_printf(
bio_err,
" -keymatexport label - Export keying material using label\n");
562 BIO_printf(
bio_err,
" -keymatexportlen len - Export len bytes of keying material (default 20)\n");
565 static int local_argc=0;
566 static char **local_argv;
568 #ifdef CHARSET_EBCDIC
569 static int ebcdic_new(
BIO *bi);
570 static int ebcdic_free(
BIO *
a);
571 static int ebcdic_read(
BIO *
b,
char *out,
int outl);
572 static int ebcdic_write(
BIO *
b,
const char *in,
int inl);
573 static long ebcdic_ctrl(
BIO *
b,
int cmd,
long num,
void *
ptr);
574 static int ebcdic_gets(
BIO *
bp,
char *buf,
int size);
575 static int ebcdic_puts(
BIO *
bp,
const char *str);
577 #define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
580 BIO_TYPE_EBCDIC_FILTER,
581 "EBCDIC/ASCII filter",
599 return(&methods_ebcdic);
602 static int ebcdic_new(
BIO *bi)
604 EBCDIC_OUTBUFF *wbuf;
606 wbuf = (EBCDIC_OUTBUFF *)
OPENSSL_malloc(
sizeof(EBCDIC_OUTBUFF) + 1024);
607 wbuf->alloced = 1024;
608 wbuf->buff[0] =
'\0';
610 bi->
ptr=(
char *)wbuf;
616 static int ebcdic_free(
BIO *
a)
618 if (a == NULL)
return(0);
627 static int ebcdic_read(
BIO *
b,
char *out,
int outl)
631 if (out == NULL || outl == 0)
return(0);
640 static int ebcdic_write(
BIO *b,
const char *in,
int inl)
642 EBCDIC_OUTBUFF *wbuf;
647 if ((in == NULL) || (inl <= 0))
return(0);
650 wbuf=(EBCDIC_OUTBUFF *)b->
ptr;
652 if (inl > (num = wbuf->alloced))
661 wbuf->buff[0] =
'\0';
673 static long ebcdic_ctrl(
BIO *b,
int cmd,
long num,
void *
ptr)
690 static int ebcdic_gets(
BIO *
bp,
char *buf,
int size)
693 if (bp->
next_bio == NULL)
return(0);
695 for (i=0; i<size-1; ++i)
697 ret = ebcdic_read(bp,&buf[i],1);
700 else if (buf[i] ==
'\n')
708 return (ret < 0 && i == 0) ? ret : i;
711 static int ebcdic_puts(
BIO *bp,
const char *str)
713 if (bp->
next_bio == NULL)
return(0);
714 return ebcdic_write(bp, str, strlen(str));
718 #ifndef OPENSSL_NO_TLSEXT
728 static int MS_CALLBACK ssl_servername_cb(
SSL *s,
int *ad,
void *arg)
775 static int cert_status_cb(
SSL *s,
void *arg)
779 char *host, *port, *path;
781 unsigned char *rspder = NULL;
799 BIO_puts(err,
"cert_status: callback called\n");
802 aia = X509_get1_ocsp(x);
806 &host, &port, &path, &use_ssl))
808 BIO_puts(err,
"cert_status: can't parse AIA URL\n");
819 BIO_puts(srctx->
err,
"cert_status: no AIA and no default responder URL\n");
835 BIO_puts(err,
"cert_status: Can't retrieve issuer certificate.\n");
839 req = OCSP_REQUEST_new();
862 BIO_puts(err,
"cert_status: error querying responder\n");
865 rspderlen = i2d_OCSP_RESPONSE(resp, &rspder);
871 BIO_puts(err,
"cert_status: ocsp response sent:\n");
886 OCSP_CERTID_free(
id);
888 OCSP_REQUEST_free(req);
890 OCSP_RESPONSE_free(resp);
897 # ifndef OPENSSL_NO_NEXTPROTONEG
904 static int next_proto_cb(
SSL *s,
const unsigned char **
data,
unsigned int *
len,
void *arg)
908 *data = next_proto->
data;
909 *len = next_proto->
len;
918 int MAIN(
int,
char **);
920 #ifndef OPENSSL_NO_JPAKE
921 static char *jpake_secret = NULL;
923 #ifndef OPENSSL_NO_SRP
928 int MAIN(
int argc,
char *argv[])
933 char *CApath=NULL,*CAfile=NULL;
934 unsigned char *context = NULL;
936 #ifndef OPENSSL_NO_ECDH
937 char *named_curve = NULL;
942 int no_tmp_rsa=0,no_dhe=0,no_ecdhe=0,nocert=0;
945 int socket_type=SOCK_STREAM;
949 char *passarg = NULL, *
pass = NULL;
950 char *dpassarg = NULL, *dpass = NULL;
952 X509 *s_cert = NULL, *s_dcert = NULL;
953 EVP_PKEY *s_key = NULL, *s_dkey = NULL;
955 #ifndef OPENSSL_NO_TLSEXT
957 X509 *s_cert2 = NULL;
959 # ifndef OPENSSL_NO_NEXTPROTONEG
960 const char *next_proto_neg_in = NULL;
964 #ifndef OPENSSL_NO_PSK
966 static char *psk_identity_hint=NULL;
968 #ifndef OPENSSL_NO_SRP
969 char *srpuserseed = NULL;
970 char *srp_verifier_file = NULL;
999 if ((strcmp(*argv,
"-port") == 0) ||
1000 (strcmp(*argv,
"-accept") == 0))
1002 if (--argc < 1)
goto bad;
1006 else if (strcmp(*argv,
"-verify") == 0)
1009 if (--argc < 1)
goto bad;
1010 verify_depth=atoi(*(++argv));
1013 else if (strcmp(*argv,
"-Verify") == 0)
1017 if (--argc < 1)
goto bad;
1018 verify_depth=atoi(*(++argv));
1019 BIO_printf(
bio_err,
"verify depth is %d, must return a certificate\n",verify_depth);
1021 else if (strcmp(*argv,
"-context") == 0)
1023 if (--argc < 1)
goto bad;
1024 context= (
unsigned char *)*(++argv);
1026 else if (strcmp(*argv,
"-cert") == 0)
1028 if (--argc < 1)
goto bad;
1029 s_cert_file= *(++argv);
1031 else if (strcmp(*argv,
"-certform") == 0)
1033 if (--argc < 1)
goto bad;
1034 s_cert_format =
str2fmt(*(++argv));
1036 else if (strcmp(*argv,
"-key") == 0)
1038 if (--argc < 1)
goto bad;
1039 s_key_file= *(++argv);
1041 else if (strcmp(*argv,
"-keyform") == 0)
1043 if (--argc < 1)
goto bad;
1044 s_key_format =
str2fmt(*(++argv));
1046 else if (strcmp(*argv,
"-pass") == 0)
1048 if (--argc < 1)
goto bad;
1049 passarg = *(++argv);
1051 else if (strcmp(*argv,
"-dhparam") == 0)
1053 if (--argc < 1)
goto bad;
1056 #ifndef OPENSSL_NO_ECDH
1057 else if (strcmp(*argv,
"-named_curve") == 0)
1059 if (--argc < 1)
goto bad;
1060 named_curve = *(++argv);
1063 else if (strcmp(*argv,
"-dcertform") == 0)
1065 if (--argc < 1)
goto bad;
1066 s_dcert_format =
str2fmt(*(++argv));
1068 else if (strcmp(*argv,
"-dcert") == 0)
1070 if (--argc < 1)
goto bad;
1071 s_dcert_file= *(++argv);
1073 else if (strcmp(*argv,
"-dkeyform") == 0)
1075 if (--argc < 1)
goto bad;
1076 s_dkey_format =
str2fmt(*(++argv));
1078 else if (strcmp(*argv,
"-dpass") == 0)
1080 if (--argc < 1)
goto bad;
1081 dpassarg = *(++argv);
1083 else if (strcmp(*argv,
"-dkey") == 0)
1085 if (--argc < 1)
goto bad;
1086 s_dkey_file= *(++argv);
1088 else if (strcmp(*argv,
"-nocert") == 0)
1092 else if (strcmp(*argv,
"-CApath") == 0)
1094 if (--argc < 1)
goto bad;
1097 else if (strcmp(*argv,
"-no_cache") == 0)
1105 else if (strcmp(*argv,
"-verify_return_error") == 0)
1106 verify_return_error = 1;
1107 else if (strcmp(*argv,
"-serverpref") == 0)
1109 else if (strcmp(*argv,
"-legacy_renegotiation") == 0)
1111 else if (strcmp(*argv,
"-cipher") == 0)
1113 if (--argc < 1)
goto bad;
1116 else if (strcmp(*argv,
"-CAfile") == 0)
1118 if (--argc < 1)
goto bad;
1122 else if (strcmp(*argv,
"-nbio") == 0)
1125 else if (strcmp(*argv,
"-nbio_test") == 0)
1132 else if (strcmp(*argv,
"-debug") == 0)
1134 #ifndef OPENSSL_NO_TLSEXT
1135 else if (strcmp(*argv,
"-tlsextdebug") == 0)
1137 else if (strcmp(*argv,
"-status") == 0)
1139 else if (strcmp(*argv,
"-status_verbose") == 0)
1144 else if (!strcmp(*argv,
"-status_timeout"))
1147 if (--argc < 1)
goto bad;
1148 tlscstatp.
timeout = atoi(*(++argv));
1150 else if (!strcmp(*argv,
"-status_url"))
1153 if (--argc < 1)
goto bad;
1165 else if (strcmp(*argv,
"-msg") == 0)
1167 else if (strcmp(*argv,
"-hack") == 0)
1169 else if (strcmp(*argv,
"-state") == 0)
1171 else if (strcmp(*argv,
"-crlf") == 0)
1173 else if (strcmp(*argv,
"-quiet") == 0)
1175 else if (strcmp(*argv,
"-bugs") == 0)
1177 else if (strcmp(*argv,
"-no_tmp_rsa") == 0)
1179 else if (strcmp(*argv,
"-no_dhe") == 0)
1181 else if (strcmp(*argv,
"-no_ecdhe") == 0)
1183 #ifndef OPENSSL_NO_PSK
1184 else if (strcmp(*argv,
"-psk_hint") == 0)
1186 if (--argc < 1)
goto bad;
1187 psk_identity_hint= *(++argv);
1189 else if (strcmp(*argv,
"-psk") == 0)
1193 if (--argc < 1)
goto bad;
1195 for (i=0; i<strlen(psk_key); i++)
1197 if (isxdigit((
unsigned char)psk_key[i]))
1204 #ifndef OPENSSL_NO_SRP
1205 else if (strcmp(*argv,
"-srpvfile") == 0)
1207 if (--argc < 1)
goto bad;
1208 srp_verifier_file = *(++argv);
1211 else if (strcmp(*argv,
"-srpuserseed") == 0)
1213 if (--argc < 1)
goto bad;
1214 srpuserseed = *(++argv);
1218 else if (strcmp(*argv,
"-www") == 0)
1220 else if (strcmp(*argv,
"-WWW") == 0)
1222 else if (strcmp(*argv,
"-HTTP") == 0)
1224 else if (strcmp(*argv,
"-no_ssl2") == 0)
1226 else if (strcmp(*argv,
"-no_ssl3") == 0)
1228 else if (strcmp(*argv,
"-no_tls1") == 0)
1230 else if (strcmp(*argv,
"-no_tls1_1") == 0)
1232 else if (strcmp(*argv,
"-no_tls1_2") == 0)
1234 else if (strcmp(*argv,
"-no_comp") == 0)
1236 #ifndef OPENSSL_NO_TLSEXT
1237 else if (strcmp(*argv,
"-no_ticket") == 0)
1240 #ifndef OPENSSL_NO_SSL2
1241 else if (strcmp(*argv,
"-ssl2") == 0)
1244 #ifndef OPENSSL_NO_SSL3
1245 else if (strcmp(*argv,
"-ssl3") == 0)
1248 #ifndef OPENSSL_NO_TLS1
1249 else if (strcmp(*argv,
"-tls1") == 0)
1251 else if (strcmp(*argv,
"-tls1_1") == 0)
1253 else if (strcmp(*argv,
"-tls1_2") == 0)
1256 #ifndef OPENSSL_NO_DTLS1
1257 else if (strcmp(*argv,
"-dtls1") == 0)
1260 socket_type = SOCK_DGRAM;
1262 else if (strcmp(*argv,
"-timeout") == 0)
1263 enable_timeouts = 1;
1264 else if (strcmp(*argv,
"-mtu") == 0)
1266 if (--argc < 1)
goto bad;
1267 socket_mtu = atol(*(++argv));
1269 else if (strcmp(*argv,
"-chain") == 0)
1272 else if (strcmp(*argv,
"-id_prefix") == 0)
1274 if (--argc < 1)
goto bad;
1275 session_id_prefix = *(++argv);
1277 #ifndef OPENSSL_NO_ENGINE
1278 else if (strcmp(*argv,
"-engine") == 0)
1280 if (--argc < 1)
goto bad;
1281 engine_id= *(++argv);
1284 else if (strcmp(*argv,
"-rand") == 0)
1286 if (--argc < 1)
goto bad;
1289 #ifndef OPENSSL_NO_TLSEXT
1290 else if (strcmp(*argv,
"-servername") == 0)
1292 if (--argc < 1)
goto bad;
1295 else if (strcmp(*argv,
"-servername_fatal") == 0)
1297 else if (strcmp(*argv,
"-cert2") == 0)
1299 if (--argc < 1)
goto bad;
1300 s_cert_file2= *(++argv);
1302 else if (strcmp(*argv,
"-key2") == 0)
1304 if (--argc < 1)
goto bad;
1305 s_key_file2= *(++argv);
1307 # ifndef OPENSSL_NO_NEXTPROTONEG
1308 else if (strcmp(*argv,
"-nextprotoneg") == 0)
1310 if (--argc < 1)
goto bad;
1311 next_proto_neg_in = *(++argv);
1315 #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
1316 else if (strcmp(*argv,
"-jpake") == 0)
1318 if (--argc < 1)
goto bad;
1319 jpake_secret = *(++argv);
1322 else if (strcmp(*argv,
"-use_srtp") == 0)
1324 if (--argc < 1)
goto bad;
1325 srtp_profiles = *(++argv);
1327 else if (strcmp(*argv,
"-keymatexport") == 0)
1329 if (--argc < 1)
goto bad;
1330 keymatexportlabel= *(++argv);
1332 else if (strcmp(*argv,
"-keymatexportlen") == 0)
1334 if (--argc < 1)
goto bad;
1335 keymatexportlen=atoi(*(++argv));
1336 if (keymatexportlen == 0)
goto bad;
1354 #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
1360 "Can't use JPAKE and PSK together\n");
1363 psk_identity =
"JPAKE";
1377 #ifndef OPENSSL_NO_ENGINE
1388 if (s_key_file == NULL)
1389 s_key_file = s_cert_file;
1390 #ifndef OPENSSL_NO_TLSEXT
1391 if (s_key_file2 == NULL)
1392 s_key_file2 = s_cert_file2;
1398 "server certificate private key file");
1406 NULL, e,
"server certificate file");
1414 #ifndef OPENSSL_NO_TLSEXT
1418 "second server certificate private key file");
1426 NULL, e,
"second server certificate file");
1435 # ifndef OPENSSL_NO_NEXTPROTONEG
1436 if (next_proto_neg_in)
1441 if (next_proto.
data == NULL)
1447 next_proto.
data = NULL;
1457 if (s_dkey_file == NULL)
1458 s_dkey_file = s_dcert_file;
1462 "second certificate private key file");
1470 NULL, e,
"second server certificate file");
1483 BIO_printf(
bio_err,
"warning, not much extra random data, consider using the -rand option\n");
1489 if (bio_s_out == NULL)
1491 if (s_quiet && !s_debug && !s_msg)
1497 if (bio_s_out == NULL)
1502 #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
1510 #ifndef OPENSSL_NO_TLSEXT
1522 if (session_id_prefix)
1524 if(strlen(session_id_prefix) >= 32)
1526 "warning: id_prefix is too long, only one new session will be possible\n");
1527 else if(strlen(session_id_prefix) >= 16)
1529 "warning: id_prefix is too long if you use SSLv2\n");
1553 if (srtp_profiles != NULL)
1557 if (cipher == NULL) cipher=getenv(
"SSL_CIPHER");
1561 if (s_cert_file == NULL)
1563 BIO_printf(
bio_err,
"You must specify a certificate file for the server to use\n");
1578 #ifndef OPENSSL_NO_TLSEXT
1591 BIO_printf(bio_s_out,
"Setting secondary ctx parameters\n");
1593 if (session_id_prefix)
1595 if(strlen(session_id_prefix) >= 32)
1597 "warning: id_prefix is too long, only one new session will be possible\n");
1598 else if(strlen(session_id_prefix) >= 16)
1600 "warning: id_prefix is too long if you use SSLv2\n");
1634 # ifndef OPENSSL_NO_NEXTPROTONEG
1635 if (next_proto.
data)
1640 #ifndef OPENSSL_NO_DH
1646 dh = load_dh_param(dhfile);
1647 else if (s_cert_file)
1648 dh = load_dh_param(s_cert_file);
1652 BIO_printf(bio_s_out,
"Setting temp DH parameters\n");
1656 BIO_printf(bio_s_out,
"Using default temp DH parameters\n");
1662 #ifndef OPENSSL_NO_TLSEXT
1667 DH *dh2=load_dh_param(s_cert_file2);
1670 BIO_printf(bio_s_out,
"Setting temp DH parameters\n");
1684 #ifndef OPENSSL_NO_ECDH
1710 BIO_printf(bio_s_out,
"Setting temp ECDH parameters\n");
1714 BIO_printf(bio_s_out,
"Using default temp ECDH parameters\n");
1725 #ifndef OPENSSL_NO_TLSEXT
1735 #ifndef OPENSSL_NO_TLSEXT
1739 if (s_dcert != NULL)
1745 #ifndef OPENSSL_NO_RSA
1750 #ifndef OPENSSL_NO_TLSEXT
1760 BIO_printf(bio_s_out,
"Generating temp (512 bit) RSA key...");
1770 #ifndef OPENSSL_NO_TLSEXT
1786 #ifndef OPENSSL_NO_PSK
1787 #ifdef OPENSSL_NO_JPAKE
1788 if (psk_key != NULL)
1790 if (psk_key != NULL || jpake_secret)
1794 BIO_printf(bio_s_out,
"PSK key given or JPAKE in use, setting server callback\n");
1814 #ifndef OPENSSL_NO_TLSEXT
1825 sizeof s_server_session_id_context);
1831 #ifndef OPENSSL_NO_TLSEXT
1836 sizeof s_server_session_id_context);
1846 #ifndef OPENSSL_NO_SRP
1847 if (srp_verifier_file != NULL)
1850 srp_callback_parm.
user = NULL;
1851 srp_callback_parm.
login = NULL;
1855 "Cannot initialize SRP verifier file \"%s\":ret=%d\n",
1856 srp_verifier_file, ret);
1868 #ifndef OPENSSL_NO_TLSEXT
1877 do_server(port,socket_type,&accept_socket,www_body, context);
1879 do_server(port,socket_type,&accept_socket,sv_body, context);
1880 print_stats(bio_s_out,ctx);
1896 #ifndef OPENSSL_NO_TLSEXT
1903 if (bio_s_out != NULL)
1912 static void print_stats(
BIO *bio,
SSL_CTX *ssl_ctx)
1914 BIO_printf(bio,
"%4ld items in the session cache\n",
1916 BIO_printf(bio,
"%4ld client connects (SSL_connect())\n",
1918 BIO_printf(bio,
"%4ld client renegotiates (SSL_connect())\n",
1920 BIO_printf(bio,
"%4ld client connects that finished\n",
1922 BIO_printf(bio,
"%4ld server accepts (SSL_accept())\n",
1924 BIO_printf(bio,
"%4ld server renegotiates (SSL_accept())\n",
1926 BIO_printf(bio,
"%4ld server accepts that finished\n",
1932 BIO_printf(bio,
"%4ld cache full overflows (%ld allowed)\n",
1937 static int sv_body(
char *hostname,
int s,
unsigned char *context)
1946 #ifndef OPENSSL_NO_KRB5
1949 struct timeval timeout;
1950 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)
1953 struct timeval *timeoutp;
1975 #ifndef OPENSSL_NO_TLSEXT
1988 #ifndef OPENSSL_NO_KRB5
1998 strlen((
char *)context));
2002 #ifdef TLSEXT_TYPE_opaque_prf_input
2012 if (enable_timeouts)
2023 if (socket_mtu > 28)
2045 #ifndef OPENSSL_NO_JPAKE
2065 #ifndef OPENSSL_NO_TLSEXT
2076 int read_from_terminal;
2077 int read_from_sslcon;
2079 read_from_terminal = 0;
2082 if (!read_from_sslcon)
2085 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) && !defined(OPENSSL_SYS_BEOS_R5)
2095 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)
2103 i=select(width,(
void *)&readfds,NULL,NULL,&tv);
2104 if((i < 0) || (!i && !_kbhit() ) )
continue;
2106 read_from_terminal = 1;
2107 #elif defined(OPENSSL_SYS_BEOS_R5)
2111 (
void)fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
2112 i=select(width,(
void *)&readfds,NULL,NULL,&tv);
2113 if ((i < 0) || (!i &&
read(fileno(stdin), buf, 0) < 0))
2115 if (
read(fileno(stdin), buf, 0) >= 0)
2116 read_from_terminal = 1;
2117 (
void)fcntl(fileno(stdin), F_SETFL, 0);
2121 timeoutp = &timeout;
2125 i=select(width,(
void *)&readfds,NULL,NULL,timeoutp);
2132 if (i <= 0)
continue;
2133 if (FD_ISSET(fileno(stdin),&readfds))
2134 read_from_terminal = 1;
2136 if (FD_ISSET(s,&readfds))
2137 read_from_sslcon = 1;
2139 if (read_from_terminal)
2148 for (j = 0; j < i; j++)
2151 for (j = i-1; j >= 0; j--)
2153 buf[j+lf_num] = buf[j];
2158 buf[j+lf_num] =
'\r';
2161 assert(lf_num == 0);
2167 if ((i <= 0) || (buf[0] ==
'Q'))
2171 close_accept_socket();
2175 if ((i <= 0) || (buf[0] ==
'q'))
2185 #ifndef OPENSSL_NO_HEARTBEATS
2186 if ((buf[0] ==
'B') &&
2187 ((buf[1] ==
'\n') || (buf[1] ==
'\r')))
2195 if ((buf[0] ==
'r') &&
2196 ((buf[1] ==
'\n') || (buf[1] ==
'\r')))
2200 printf(
"SSL_do_handshake -> %d\n",i);
2205 if ((buf[0] ==
'R') &&
2206 ((buf[1] ==
'\n') || (buf[1] ==
'\r')))
2212 printf(
"SSL_do_handshake -> %d\n",i);
2219 static const char *str=
"Lets print some clear text\n";
2227 #ifdef CHARSET_EBCDIC
2235 {
static count=0;
if (++count == 100) { count=0;
SSL_renegotiate(con); } }
2237 k=
SSL_write(con,&(buf[l]),(
unsigned int)i);
2238 #ifndef OPENSSL_NO_SRP
2241 BIO_printf(bio_s_out,
"LOOKUP renego during write\n");
2243 if (srp_callback_parm.
user)
2246 BIO_printf(bio_s_out,
"LOOKUP not successful\n");
2247 k=
SSL_write(con,&(buf[l]),(
unsigned int)i);
2276 if (read_from_sslcon)
2280 i=init_ssl_connection(con);
2296 i=
SSL_read(con,(
char *)buf,bufsize);
2297 #ifndef OPENSSL_NO_SRP
2300 BIO_printf(bio_s_out,
"LOOKUP renego during read\n");
2302 if (srp_callback_parm.
user)
2305 BIO_printf(bio_s_out,
"LOOKUP not successful\n");
2306 i=
SSL_read(con,(
char *)buf,bufsize);
2312 #ifdef CHARSET_EBCDIC
2359 static void close_accept_socket(
void)
2362 if (accept_socket >= 0)
2364 SHUTDOWN2(accept_socket);
2368 static int init_ssl_connection(
SSL *con)
2375 #ifndef OPENSSL_NO_KRB5
2378 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
2379 const unsigned char *next_proto_neg;
2380 unsigned next_proto_neg_len;
2382 unsigned char *exportedkeymat;
2386 #ifndef OPENSSL_NO_SRP
2387 while (i <= 0 &&
SSL_get_error(con,i) == SSL_ERROR_WANT_X509_LOOKUP)
2389 BIO_printf(bio_s_out,
"LOOKUP during accept %s\n",srp_callback_parm.
login);
2391 if (srp_callback_parm.
user)
2394 BIO_printf(bio_s_out,
"LOOKUP not successful\n");
2423 BIO_printf(bio_s_out,
"Client certificate\n");
2424 PEM_write_bio_X509(bio_s_out,peer);
2433 BIO_printf(bio_s_out,
"Shared ciphers:%s\n",buf);
2435 BIO_printf(bio_s_out,
"CIPHER is %s\n",(str != NULL)?str:
"(NONE)");
2436 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
2441 BIO_write(bio_s_out, next_proto_neg, next_proto_neg_len);
2450 BIO_printf(bio_s_out,
"SRTP Extension negotiated, profile=%s\n",
2451 srtp_profile->
name);
2454 if (
SSL_ctrl(con,SSL_CTRL_GET_FLAGS,0,NULL) &
2457 "Peer has incorrect TLSv1 block padding\n");
2458 #ifndef OPENSSL_NO_KRB5
2460 if (client_princ != NULL)
2462 BIO_printf(bio_s_out,
"Kerberos peer principal is %s\n",
2466 BIO_printf(bio_s_out,
"Secure Renegotiation IS%s supported\n",
2468 if (keymatexportlabel != NULL)
2470 BIO_printf(bio_s_out,
"Keying material exporter:\n");
2471 BIO_printf(bio_s_out,
" Label: '%s'\n", keymatexportlabel);
2475 if (exportedkeymat != NULL)
2480 strlen(keymatexportlabel),
2488 for (i=0; i<keymatexportlen; i++)
2500 #ifndef OPENSSL_NO_DH
2501 static DH *load_dh_param(
const char *dhfile)
2508 ret=PEM_read_bio_DHparams(bio,NULL,NULL,NULL);
2514 #ifndef OPENSSL_NO_KRB5
2524 if ((in=fopen(file,
"r")) == NULL)
2529 if (PEM_read_X509(in,&x,NULL) == NULL)
2533 if (x != NULL) X509_free(x);
2539 static int www_body(
char *hostname,
int s,
unsigned char *context)
2546 BIO *io,*ssl_bio,*sbio;
2547 #ifndef OPENSSL_NO_KRB5
2552 if (buf == NULL)
return(0);
2555 if ((io == NULL) || (ssl_bio == NULL))
goto err;
2572 if ((con=
SSL_new(ctx)) == NULL)
goto err;
2573 #ifndef OPENSSL_NO_TLSEXT
2580 #ifndef OPENSSL_NO_KRB5
2588 strlen((
char *)context));
2604 #ifdef CHARSET_EBCDIC
2625 #ifndef OPENSSL_NO_SRP
2626 while (i <= 0 &&
SSL_get_error(con,i) == SSL_ERROR_WANT_X509_LOOKUP)
2628 BIO_printf(bio_s_out,
"LOOKUP during accept %s\n",srp_callback_parm.
login);
2630 if (srp_callback_parm.
user)
2633 BIO_printf(bio_s_out,
"LOOKUP not successful\n");
2669 #if defined(OPENSSL_SYS_NETWARE)
2671 #elif !defined(OPENSSL_SYS_MSDOS) && !defined(__DJGPP__)
2684 if ( ((www == 1) && (strncmp(
"GET ",buf,4) == 0)) ||
2685 ((www == 2) && (strncmp(
"GET /stats ",buf,10) == 0)))
2690 static const char *space=
" ";
2692 BIO_puts(io,
"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
2693 BIO_puts(io,
"<HTML><BODY BGCOLOR=\"#ffffff\">\n");
2697 for (i=0; i<local_argc; i++)
2706 BIO_printf(io,
"Ciphers supported in s_server binary\n");
2707 sk=SSL_get_ciphers(con);
2715 if ((((i+1)%2) == 0) && (i+1 != j))
2722 BIO_printf(io,
"---\nCiphers common between both SSL end points:\n");
2758 PEM_write_bio_X509(io,peer);
2761 BIO_puts(io,
"no client certificate available\n");
2762 BIO_puts(io,
"</BODY></HTML>\r\n\r\n");
2765 else if ((www == 2 || www == 3)
2766 && (strncmp(
"GET /",buf,5) == 0))
2770 static const char *
text=
"HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
2776 for (e=p; *e !=
'\0'; e++)
2784 dot = (e[0] ==
'.') ? 2 : 0;
2787 dot = (e[0] ==
'.') ? 3 : 0;
2790 dot = (e[0] ==
'/') ? -1 : 0;
2794 dot = (e[0] ==
'/') ? 1 : 0;
2796 dot = (dot == 3) || (dot == -1);
2801 BIO_printf(io,
"'%s' is an invalid file name\r\n",p);
2809 BIO_printf(io,
"'%s' contains '..' reference\r\n",p);
2816 BIO_printf(io,
"'%s' is an invalid path\r\n",p);
2823 strcat(p,
"index.html");
2830 strcat(p,
"/index.html");
2852 if ( ((i > 5) && (strcmp(&(p[i-5]),
".html") == 0)) ||
2853 ((i > 4) && (strcmp(&(p[i-4]),
".php") == 0)) ||
2854 ((i > 4) && (strcmp(&(p[i-4]),
".htm") == 0)))
2855 BIO_puts(io,
"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
2857 BIO_puts(io,
"HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
2867 fprintf(stderr,
"%d\n",i);
2868 if (total_bytes > 3*1024)
2871 fprintf(stderr,
"RENEGOTIATE\n");
2935 #ifndef OPENSSL_NO_RSA
2939 static RSA *rsa_tmp=NULL;
2941 if (!rsa_tmp && ((bn =
BN_new()) == NULL))
2967 #define MAX_SESSION_ID_ATTEMPTS 10
2968 static int generate_session_id(
const SSL *ssl,
unsigned char *
id,
2969 unsigned int *id_len)
2971 unsigned int count = 0;
2979 memcpy(
id, session_id_prefix,
2980 (strlen(session_id_prefix) < *id_len) ?
2981 strlen(session_id_prefix) : *id_len);