s_client.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. /* apps/s_client.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. /* ====================================================================
  112. * Copyright 2005 Nokia. All rights reserved.
  113. *
  114. * The portions of the attached software ("Contribution") is developed by
  115. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  116. * license.
  117. *
  118. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  119. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  120. * support (see RFC 4279) to OpenSSL.
  121. *
  122. * No patent licenses or other rights except those expressly stated in
  123. * the OpenSSL open source license shall be deemed granted or received
  124. * expressly, by implication, estoppel, or otherwise.
  125. *
  126. * No assurances are provided by Nokia that the Contribution does not
  127. * infringe the patent or other intellectual property rights of any third
  128. * party or that the license provides you with all the necessary rights
  129. * to make use of the Contribution.
  130. *
  131. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  132. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  133. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  134. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  135. * OTHERWISE.
  136. */
  137. #include <assert.h>
  138. #include <ctype.h>
  139. #include <stdio.h>
  140. #include <stdlib.h>
  141. #include <string.h>
  142. #include <openssl/e_os2.h>
  143. #ifdef OPENSSL_NO_STDIO
  144. #define APPS_WIN16
  145. #endif
  146. /* With IPv6, it looks like Digital has mixed up the proper order of
  147. recursive header file inclusion, resulting in the compiler complaining
  148. that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
  149. is needed to have fileno() declared correctly... So let's define u_int */
  150. #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
  151. #define __U_INT
  152. typedef unsigned int u_int;
  153. #endif
  154. #define USE_SOCKETS
  155. #include "apps.h"
  156. #include <openssl/x509.h>
  157. #include <openssl/ssl.h>
  158. #include <openssl/err.h>
  159. #include <openssl/pem.h>
  160. #include <openssl/rand.h>
  161. #include "s_apps.h"
  162. #include "timeouts.h"
  163. #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
  164. /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
  165. #undef FIONBIO
  166. #endif
  167. #if defined(OPENSSL_SYS_BEOS_R5)
  168. #include <fcntl.h>
  169. #endif
  170. #undef PROG
  171. #define PROG s_client_main
  172. /*#define SSL_HOST_NAME "www.netscape.com" */
  173. /*#define SSL_HOST_NAME "193.118.187.102" */
  174. #define SSL_HOST_NAME "localhost"
  175. /*#define TEST_CERT "client.pem" */ /* no default cert. */
  176. #undef BUFSIZZ
  177. #define BUFSIZZ 1024*8
  178. extern int verify_depth;
  179. extern int verify_error;
  180. #ifdef FIONBIO
  181. static int c_nbio=0;
  182. #endif
  183. static int c_Pause=0;
  184. static int c_debug=0;
  185. static int c_msg=0;
  186. static int c_showcerts=0;
  187. static void sc_usage(void);
  188. static void print_stuff(BIO *berr,SSL *con,int full);
  189. static BIO *bio_c_out=NULL;
  190. static int c_quiet=0;
  191. static int c_ign_eof=0;
  192. #ifndef OPENSSL_NO_PSK
  193. /* Default PSK identity and key */
  194. static char *psk_identity="Client_identity";
  195. static char *psk_key=NULL; /* by default PSK is not used */
  196. static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *identity,
  197. unsigned int max_identity_len, unsigned char *psk,
  198. unsigned int max_psk_len)
  199. {
  200. unsigned int psk_len = 0;
  201. int ret;
  202. BIGNUM *bn=NULL;
  203. if (c_debug)
  204. BIO_printf(bio_c_out, "psk_client_cb\n");
  205. if (!hint)
  206. {
  207. /* no ServerKeyExchange message*/
  208. if (c_debug)
  209. BIO_printf(bio_c_out,"NULL received PSK identity hint, continuing anyway\n");
  210. }
  211. else if (c_debug)
  212. BIO_printf(bio_c_out, "Received PSK identity hint '%s'\n", hint);
  213. /* lookup PSK identity and PSK key based on the given identity hint here */
  214. ret = BIO_snprintf(identity, max_identity_len, psk_identity);
  215. if (ret < 0 || (unsigned int)ret > max_identity_len)
  216. goto out_err;
  217. if (c_debug)
  218. BIO_printf(bio_c_out, "created identity '%s' len=%d\n", identity, ret);
  219. ret=BN_hex2bn(&bn, psk_key);
  220. if (!ret)
  221. {
  222. BIO_printf(bio_err,"Could not convert PSK key '%s' to BIGNUM\n", psk_key);
  223. if (bn)
  224. BN_free(bn);
  225. return 0;
  226. }
  227. if ((unsigned int)BN_num_bytes(bn) > max_psk_len)
  228. {
  229. BIO_printf(bio_err,"psk buffer of callback is too small (%d) for key (%d)\n",
  230. max_psk_len, BN_num_bytes(bn));
  231. BN_free(bn);
  232. return 0;
  233. }
  234. psk_len=BN_bn2bin(bn, psk);
  235. BN_free(bn);
  236. if (psk_len == 0)
  237. goto out_err;
  238. if (c_debug)
  239. BIO_printf(bio_c_out, "created PSK len=%d\n", psk_len);
  240. return psk_len;
  241. out_err:
  242. if (c_debug)
  243. BIO_printf(bio_err, "Error in PSK client callback\n");
  244. return 0;
  245. }
  246. #endif
  247. static void sc_usage(void)
  248. {
  249. BIO_printf(bio_err,"usage: s_client args\n");
  250. BIO_printf(bio_err,"\n");
  251. BIO_printf(bio_err," -host host - use -connect instead\n");
  252. BIO_printf(bio_err," -port port - use -connect instead\n");
  253. BIO_printf(bio_err," -connect host:port - who to connect to (default is %s:%s)\n",SSL_HOST_NAME,PORT_STR);
  254. BIO_printf(bio_err," -verify arg - turn on peer certificate verification\n");
  255. BIO_printf(bio_err," -cert arg - certificate file to use, PEM format assumed\n");
  256. BIO_printf(bio_err," -certform arg - certificate format (PEM or DER) PEM default\n");
  257. BIO_printf(bio_err," -key arg - Private key file to use, in cert file if\n");
  258. BIO_printf(bio_err," not specified but cert file is.\n");
  259. BIO_printf(bio_err," -keyform arg - key format (PEM or DER) PEM default\n");
  260. BIO_printf(bio_err," -pass arg - private key file pass phrase source\n");
  261. BIO_printf(bio_err," -CApath arg - PEM format directory of CA's\n");
  262. BIO_printf(bio_err," -CAfile arg - PEM format file of CA's\n");
  263. BIO_printf(bio_err," -reconnect - Drop and re-make the connection with the same Session-ID\n");
  264. BIO_printf(bio_err," -pause - sleep(1) after each read(2) and write(2) system call\n");
  265. BIO_printf(bio_err," -showcerts - show all certificates in the chain\n");
  266. BIO_printf(bio_err," -debug - extra output\n");
  267. #ifdef WATT32
  268. BIO_printf(bio_err," -wdebug - WATT-32 tcp debugging\n");
  269. #endif
  270. BIO_printf(bio_err," -msg - Show protocol messages\n");
  271. BIO_printf(bio_err," -nbio_test - more ssl protocol testing\n");
  272. BIO_printf(bio_err," -state - print the 'ssl' states\n");
  273. #ifdef FIONBIO
  274. BIO_printf(bio_err," -nbio - Run with non-blocking IO\n");
  275. #endif
  276. BIO_printf(bio_err," -crlf - convert LF from terminal into CRLF\n");
  277. BIO_printf(bio_err," -quiet - no s_client output\n");
  278. BIO_printf(bio_err," -ign_eof - ignore input eof (default when -quiet)\n");
  279. #ifndef OPENSSL_NO_PSK
  280. BIO_printf(bio_err," -psk_identity arg - PSK identity\n");
  281. BIO_printf(bio_err," -psk arg - PSK in hex (without 0x)\n");
  282. #endif
  283. BIO_printf(bio_err," -ssl2 - just use SSLv2\n");
  284. BIO_printf(bio_err," -ssl3 - just use SSLv3\n");
  285. BIO_printf(bio_err," -tls1 - just use TLSv1\n");
  286. BIO_printf(bio_err," -dtls1 - just use DTLSv1\n");
  287. BIO_printf(bio_err," -mtu - set the MTU\n");
  288. BIO_printf(bio_err," -no_tls1/-no_ssl3/-no_ssl2 - turn off that protocol\n");
  289. BIO_printf(bio_err," -bugs - Switch on all SSL implementation bug workarounds\n");
  290. BIO_printf(bio_err," -serverpref - Use server's cipher preferences (only SSLv2)\n");
  291. BIO_printf(bio_err," -cipher - preferred cipher to use, use the 'openssl ciphers'\n");
  292. BIO_printf(bio_err," command to see what is available\n");
  293. BIO_printf(bio_err," -starttls prot - use the STARTTLS command before starting TLS\n");
  294. BIO_printf(bio_err," for those protocols that support it, where\n");
  295. BIO_printf(bio_err," 'prot' defines which one to assume. Currently,\n");
  296. BIO_printf(bio_err," only \"smtp\" and \"pop3\" are supported.\n");
  297. #ifndef OPENSSL_NO_ENGINE
  298. BIO_printf(bio_err," -engine id - Initialise and use the specified engine\n");
  299. #endif
  300. BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
  301. #ifndef OPENSSL_NO_TLSEXT
  302. BIO_printf(bio_err," -servername host - Set TLS extension servername in ClientHello\n");
  303. #endif
  304. }
  305. #ifndef OPENSSL_NO_TLSEXT
  306. /* This is a context that we pass to callbacks */
  307. typedef struct tlsextctx_st {
  308. BIO * biodebug;
  309. int ack;
  310. } tlsextctx;
  311. static int MS_CALLBACK ssl_servername_cb(SSL *s, int *ad, void *arg)
  312. {
  313. tlsextctx * p = (tlsextctx *) arg;
  314. const char * hn= SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
  315. if (SSL_get_servername_type(s) != -1)
  316. p->ack = !SSL_session_reused(s) && hn != NULL;
  317. else
  318. BIO_printf(bio_err,"Can't use SSL_get_servername\n");
  319. return SSL_TLSEXT_ERR_OK;
  320. }
  321. #endif
  322. int MAIN(int, char **);
  323. int MAIN(int argc, char **argv)
  324. {
  325. int off=0;
  326. SSL *con=NULL;
  327. X509_STORE *store = NULL;
  328. int s,k,width,state=0;
  329. char *cbuf=NULL,*sbuf=NULL,*mbuf=NULL;
  330. int cbuf_len,cbuf_off;
  331. int sbuf_len,sbuf_off;
  332. fd_set readfds,writefds;
  333. short port=PORT;
  334. int full_log=1;
  335. char *host=SSL_HOST_NAME;
  336. char *cert_file=NULL,*key_file=NULL;
  337. int cert_format = FORMAT_PEM, key_format = FORMAT_PEM;
  338. char *passarg = NULL, *pass = NULL;
  339. X509 *cert = NULL;
  340. EVP_PKEY *key = NULL;
  341. char *CApath=NULL,*CAfile=NULL,*cipher=NULL;
  342. int reconnect=0,badop=0,verify=SSL_VERIFY_NONE,bugs=0;
  343. int crlf=0;
  344. int write_tty,read_tty,write_ssl,read_ssl,tty_on,ssl_pending;
  345. SSL_CTX *ctx=NULL;
  346. int ret=1,in_init=1,i,nbio_test=0;
  347. int starttls_proto = 0;
  348. int prexit = 0, vflags = 0;
  349. const SSL_METHOD *meth=NULL;
  350. int socket_type=SOCK_STREAM;
  351. BIO *sbio;
  352. char *inrand=NULL;
  353. #ifndef OPENSSL_NO_ENGINE
  354. char *engine_id=NULL;
  355. ENGINE *e=NULL;
  356. #endif
  357. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)
  358. struct timeval tv;
  359. #if defined(OPENSSL_SYS_BEOS_R5)
  360. int stdin_set = 0;
  361. #endif
  362. #endif
  363. #ifndef OPENSSL_NO_TLSEXT
  364. char *servername = NULL;
  365. tlsextctx tlsextcbp =
  366. {NULL,0};
  367. #endif
  368. struct sockaddr peer;
  369. int peerlen = sizeof(peer);
  370. int enable_timeouts = 0 ;
  371. long socket_mtu = 0;
  372. #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
  373. meth=SSLv23_client_method();
  374. #elif !defined(OPENSSL_NO_SSL3)
  375. meth=SSLv3_client_method();
  376. #elif !defined(OPENSSL_NO_SSL2)
  377. meth=SSLv2_client_method();
  378. #endif
  379. apps_startup();
  380. c_Pause=0;
  381. c_quiet=0;
  382. c_ign_eof=0;
  383. c_debug=0;
  384. c_msg=0;
  385. c_showcerts=0;
  386. if (bio_err == NULL)
  387. bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
  388. if (!load_config(bio_err, NULL))
  389. goto end;
  390. if ( ((cbuf=OPENSSL_malloc(BUFSIZZ)) == NULL) ||
  391. ((sbuf=OPENSSL_malloc(BUFSIZZ)) == NULL) ||
  392. ((mbuf=OPENSSL_malloc(BUFSIZZ)) == NULL))
  393. {
  394. BIO_printf(bio_err,"out of memory\n");
  395. goto end;
  396. }
  397. verify_depth=0;
  398. verify_error=X509_V_OK;
  399. #ifdef FIONBIO
  400. c_nbio=0;
  401. #endif
  402. argc--;
  403. argv++;
  404. while (argc >= 1)
  405. {
  406. if (strcmp(*argv,"-host") == 0)
  407. {
  408. if (--argc < 1) goto bad;
  409. host= *(++argv);
  410. }
  411. else if (strcmp(*argv,"-port") == 0)
  412. {
  413. if (--argc < 1) goto bad;
  414. port=atoi(*(++argv));
  415. if (port == 0) goto bad;
  416. }
  417. else if (strcmp(*argv,"-connect") == 0)
  418. {
  419. if (--argc < 1) goto bad;
  420. if (!extract_host_port(*(++argv),&host,NULL,&port))
  421. goto bad;
  422. }
  423. else if (strcmp(*argv,"-verify") == 0)
  424. {
  425. verify=SSL_VERIFY_PEER;
  426. if (--argc < 1) goto bad;
  427. verify_depth=atoi(*(++argv));
  428. BIO_printf(bio_err,"verify depth is %d\n",verify_depth);
  429. }
  430. else if (strcmp(*argv,"-cert") == 0)
  431. {
  432. if (--argc < 1) goto bad;
  433. cert_file= *(++argv);
  434. }
  435. else if (strcmp(*argv,"-certform") == 0)
  436. {
  437. if (--argc < 1) goto bad;
  438. cert_format = str2fmt(*(++argv));
  439. }
  440. else if (strcmp(*argv,"-crl_check") == 0)
  441. vflags |= X509_V_FLAG_CRL_CHECK;
  442. else if (strcmp(*argv,"-crl_check_all") == 0)
  443. vflags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;
  444. else if (strcmp(*argv,"-prexit") == 0)
  445. prexit=1;
  446. else if (strcmp(*argv,"-crlf") == 0)
  447. crlf=1;
  448. else if (strcmp(*argv,"-quiet") == 0)
  449. {
  450. c_quiet=1;
  451. c_ign_eof=1;
  452. }
  453. else if (strcmp(*argv,"-ign_eof") == 0)
  454. c_ign_eof=1;
  455. else if (strcmp(*argv,"-pause") == 0)
  456. c_Pause=1;
  457. else if (strcmp(*argv,"-debug") == 0)
  458. c_debug=1;
  459. #ifdef WATT32
  460. else if (strcmp(*argv,"-wdebug") == 0)
  461. dbug_init();
  462. #endif
  463. else if (strcmp(*argv,"-msg") == 0)
  464. c_msg=1;
  465. else if (strcmp(*argv,"-showcerts") == 0)
  466. c_showcerts=1;
  467. else if (strcmp(*argv,"-nbio_test") == 0)
  468. nbio_test=1;
  469. else if (strcmp(*argv,"-state") == 0)
  470. state=1;
  471. #ifndef OPENSSL_NO_PSK
  472. else if (strcmp(*argv,"-psk_identity") == 0)
  473. {
  474. if (--argc < 1) goto bad;
  475. psk_identity=*(++argv);
  476. }
  477. else if (strcmp(*argv,"-psk") == 0)
  478. {
  479. size_t j;
  480. if (--argc < 1) goto bad;
  481. psk_key=*(++argv);
  482. for (j = 0; j < strlen(psk_key); j++)
  483. {
  484. if (isxdigit((int)psk_key[j]))
  485. continue;
  486. BIO_printf(bio_err,"Not a hex number '%s'\n",*argv);
  487. goto bad;
  488. }
  489. }
  490. #endif
  491. #ifndef OPENSSL_NO_SSL2
  492. else if (strcmp(*argv,"-ssl2") == 0)
  493. meth=SSLv2_client_method();
  494. #endif
  495. #ifndef OPENSSL_NO_SSL3
  496. else if (strcmp(*argv,"-ssl3") == 0)
  497. meth=SSLv3_client_method();
  498. #endif
  499. #ifndef OPENSSL_NO_TLS1
  500. else if (strcmp(*argv,"-tls1") == 0)
  501. meth=TLSv1_client_method();
  502. #endif
  503. #ifndef OPENSSL_NO_DTLS1
  504. else if (strcmp(*argv,"-dtls1") == 0)
  505. {
  506. meth=DTLSv1_client_method();
  507. socket_type=SOCK_DGRAM;
  508. }
  509. else if (strcmp(*argv,"-timeout") == 0)
  510. enable_timeouts=1;
  511. else if (strcmp(*argv,"-mtu") == 0)
  512. {
  513. if (--argc < 1) goto bad;
  514. socket_mtu = atol(*(++argv));
  515. }
  516. #endif
  517. else if (strcmp(*argv,"-bugs") == 0)
  518. bugs=1;
  519. else if (strcmp(*argv,"-keyform") == 0)
  520. {
  521. if (--argc < 1) goto bad;
  522. key_format = str2fmt(*(++argv));
  523. }
  524. else if (strcmp(*argv,"-pass") == 0)
  525. {
  526. if (--argc < 1) goto bad;
  527. passarg = *(++argv);
  528. }
  529. else if (strcmp(*argv,"-key") == 0)
  530. {
  531. if (--argc < 1) goto bad;
  532. key_file= *(++argv);
  533. }
  534. else if (strcmp(*argv,"-reconnect") == 0)
  535. {
  536. reconnect=5;
  537. }
  538. else if (strcmp(*argv,"-CApath") == 0)
  539. {
  540. if (--argc < 1) goto bad;
  541. CApath= *(++argv);
  542. }
  543. else if (strcmp(*argv,"-CAfile") == 0)
  544. {
  545. if (--argc < 1) goto bad;
  546. CAfile= *(++argv);
  547. }
  548. else if (strcmp(*argv,"-no_tls1") == 0)
  549. off|=SSL_OP_NO_TLSv1;
  550. else if (strcmp(*argv,"-no_ssl3") == 0)
  551. off|=SSL_OP_NO_SSLv3;
  552. else if (strcmp(*argv,"-no_ssl2") == 0)
  553. off|=SSL_OP_NO_SSLv2;
  554. else if (strcmp(*argv,"-no_comp") == 0)
  555. { off|=SSL_OP_NO_COMPRESSION; }
  556. else if (strcmp(*argv,"-serverpref") == 0)
  557. off|=SSL_OP_CIPHER_SERVER_PREFERENCE;
  558. else if (strcmp(*argv,"-cipher") == 0)
  559. {
  560. if (--argc < 1) goto bad;
  561. cipher= *(++argv);
  562. }
  563. #ifdef FIONBIO
  564. else if (strcmp(*argv,"-nbio") == 0)
  565. { c_nbio=1; }
  566. #endif
  567. else if (strcmp(*argv,"-starttls") == 0)
  568. {
  569. if (--argc < 1) goto bad;
  570. ++argv;
  571. if (strcmp(*argv,"smtp") == 0)
  572. starttls_proto = 1;
  573. else if (strcmp(*argv,"pop3") == 0)
  574. starttls_proto = 2;
  575. else
  576. goto bad;
  577. }
  578. #ifndef OPENSSL_NO_ENGINE
  579. else if (strcmp(*argv,"-engine") == 0)
  580. {
  581. if (--argc < 1) goto bad;
  582. engine_id = *(++argv);
  583. }
  584. #endif
  585. else if (strcmp(*argv,"-rand") == 0)
  586. {
  587. if (--argc < 1) goto bad;
  588. inrand= *(++argv);
  589. }
  590. #ifndef OPENSSL_NO_TLSEXT
  591. else if (strcmp(*argv,"-servername") == 0)
  592. {
  593. if (--argc < 1) goto bad;
  594. servername= *(++argv);
  595. /* meth=TLSv1_client_method(); */
  596. }
  597. #endif
  598. else
  599. {
  600. BIO_printf(bio_err,"unknown option %s\n",*argv);
  601. badop=1;
  602. break;
  603. }
  604. argc--;
  605. argv++;
  606. }
  607. if (badop)
  608. {
  609. bad:
  610. sc_usage();
  611. goto end;
  612. }
  613. OpenSSL_add_ssl_algorithms();
  614. SSL_load_error_strings();
  615. #ifndef OPENSSL_NO_ENGINE
  616. e = setup_engine(bio_err, engine_id, 1);
  617. #endif
  618. if (!app_passwd(bio_err, passarg, NULL, &pass, NULL))
  619. {
  620. BIO_printf(bio_err, "Error getting password\n");
  621. goto end;
  622. }
  623. if (key_file == NULL)
  624. key_file = cert_file;
  625. if (key_file)
  626. {
  627. key = load_key(bio_err, key_file, key_format, 0, pass, e,
  628. "client certificate private key file");
  629. if (!key)
  630. {
  631. ERR_print_errors(bio_err);
  632. goto end;
  633. }
  634. }
  635. if (cert_file)
  636. {
  637. cert = load_cert(bio_err,cert_file,cert_format,
  638. NULL, e, "client certificate file");
  639. if (!cert)
  640. {
  641. ERR_print_errors(bio_err);
  642. goto end;
  643. }
  644. }
  645. if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
  646. && !RAND_status())
  647. {
  648. BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
  649. }
  650. if (inrand != NULL)
  651. BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
  652. app_RAND_load_files(inrand));
  653. if (bio_c_out == NULL)
  654. {
  655. if (c_quiet && !c_debug && !c_msg)
  656. {
  657. bio_c_out=BIO_new(BIO_s_null());
  658. }
  659. else
  660. {
  661. if (bio_c_out == NULL)
  662. bio_c_out=BIO_new_fp(stdout,BIO_NOCLOSE);
  663. }
  664. }
  665. ctx=SSL_CTX_new(meth);
  666. if (ctx == NULL)
  667. {
  668. ERR_print_errors(bio_err);
  669. goto end;
  670. }
  671. #ifndef OPENSSL_NO_PSK
  672. if (psk_key != NULL)
  673. {
  674. if (c_debug)
  675. BIO_printf(bio_c_out, "PSK key given, setting client callback\n");
  676. SSL_CTX_set_psk_client_callback(ctx, psk_client_cb);
  677. }
  678. #endif
  679. if (bugs)
  680. SSL_CTX_set_options(ctx,SSL_OP_ALL|off);
  681. else
  682. SSL_CTX_set_options(ctx,off);
  683. /* DTLS: partial reads end up discarding unread UDP bytes :-(
  684. * Setting read ahead solves this problem.
  685. */
  686. if (socket_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx, 1);
  687. if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);
  688. if (cipher != NULL)
  689. if(!SSL_CTX_set_cipher_list(ctx,cipher)) {
  690. BIO_printf(bio_err,"error setting cipher list\n");
  691. ERR_print_errors(bio_err);
  692. goto end;
  693. }
  694. #if 0
  695. else
  696. SSL_CTX_set_cipher_list(ctx,getenv("SSL_CIPHER"));
  697. #endif
  698. SSL_CTX_set_verify(ctx,verify,verify_callback);
  699. if (!set_cert_key_stuff(ctx,cert,key))
  700. goto end;
  701. if ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) ||
  702. (!SSL_CTX_set_default_verify_paths(ctx)))
  703. {
  704. /* BIO_printf(bio_err,"error setting default verify locations\n"); */
  705. ERR_print_errors(bio_err);
  706. /* goto end; */
  707. }
  708. store = SSL_CTX_get_cert_store(ctx);
  709. X509_STORE_set_flags(store, vflags);
  710. #ifndef OPENSSL_NO_TLSEXT
  711. if (servername != NULL)
  712. {
  713. tlsextcbp.biodebug = bio_err;
  714. SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
  715. SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
  716. }
  717. #endif
  718. con=SSL_new(ctx);
  719. #ifndef OPENSSL_NO_TLSEXT
  720. if (servername != NULL)
  721. {
  722. if (!SSL_set_tlsext_host_name(con,servername))
  723. {
  724. BIO_printf(bio_err,"Unable to set TLS servername extension.\n");
  725. ERR_print_errors(bio_err);
  726. goto end;
  727. }
  728. }
  729. #endif
  730. #ifndef OPENSSL_NO_KRB5
  731. if (con && (con->kssl_ctx = kssl_ctx_new()) != NULL)
  732. {
  733. kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVER, host);
  734. }
  735. #endif /* OPENSSL_NO_KRB5 */
  736. /* SSL_set_cipher_list(con,"RC4-MD5"); */
  737. re_start:
  738. if (init_client(&s,host,port,socket_type) == 0)
  739. {
  740. BIO_printf(bio_err,"connect:errno=%d\n",get_last_socket_error());
  741. SHUTDOWN(s);
  742. goto end;
  743. }
  744. BIO_printf(bio_c_out,"CONNECTED(%08X)\n",s);
  745. #ifdef FIONBIO
  746. if (c_nbio)
  747. {
  748. unsigned long l=1;
  749. BIO_printf(bio_c_out,"turning on non blocking io\n");
  750. if (BIO_socket_ioctl(s,FIONBIO,&l) < 0)
  751. {
  752. ERR_print_errors(bio_err);
  753. goto end;
  754. }
  755. }
  756. #endif
  757. if (c_Pause & 0x01) con->debug=1;
  758. if ( SSL_version(con) == DTLS1_VERSION)
  759. {
  760. struct timeval timeout;
  761. sbio=BIO_new_dgram(s,BIO_NOCLOSE);
  762. if (getsockname(s, &peer, (void *)&peerlen) < 0)
  763. {
  764. BIO_printf(bio_err, "getsockname:errno=%d\n",
  765. get_last_socket_error());
  766. SHUTDOWN(s);
  767. goto end;
  768. }
  769. BIO_ctrl_set_connected(sbio, 1, &peer);
  770. if (enable_timeouts)
  771. {
  772. timeout.tv_sec = 0;
  773. timeout.tv_usec = DGRAM_RCV_TIMEOUT;
  774. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
  775. timeout.tv_sec = 0;
  776. timeout.tv_usec = DGRAM_SND_TIMEOUT;
  777. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
  778. }
  779. if (socket_mtu > 0)
  780. {
  781. SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
  782. SSL_set_mtu(con, socket_mtu);
  783. }
  784. else
  785. /* want to do MTU discovery */
  786. BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
  787. }
  788. else
  789. sbio=BIO_new_socket(s,BIO_NOCLOSE);
  790. if (nbio_test)
  791. {
  792. BIO *test;
  793. test=BIO_new(BIO_f_nbio_test());
  794. sbio=BIO_push(test,sbio);
  795. }
  796. if (c_debug)
  797. {
  798. con->debug=1;
  799. BIO_set_callback(sbio,bio_dump_callback);
  800. BIO_set_callback_arg(sbio,bio_c_out);
  801. }
  802. if (c_msg)
  803. {
  804. SSL_set_msg_callback(con, msg_cb);
  805. SSL_set_msg_callback_arg(con, bio_c_out);
  806. }
  807. SSL_set_bio(con,sbio,sbio);
  808. SSL_set_connect_state(con);
  809. /* ok, lets connect */
  810. width=SSL_get_fd(con)+1;
  811. read_tty=1;
  812. write_tty=0;
  813. tty_on=0;
  814. read_ssl=1;
  815. write_ssl=1;
  816. cbuf_len=0;
  817. cbuf_off=0;
  818. sbuf_len=0;
  819. sbuf_off=0;
  820. /* This is an ugly hack that does a lot of assumptions */
  821. if (starttls_proto == 1)
  822. {
  823. BIO_read(sbio,mbuf,BUFSIZZ);
  824. BIO_printf(sbio,"STARTTLS\r\n");
  825. BIO_read(sbio,sbuf,BUFSIZZ);
  826. }
  827. if (starttls_proto == 2)
  828. {
  829. BIO_read(sbio,mbuf,BUFSIZZ);
  830. BIO_printf(sbio,"STLS\r\n");
  831. BIO_read(sbio,sbuf,BUFSIZZ);
  832. }
  833. for (;;)
  834. {
  835. FD_ZERO(&readfds);
  836. FD_ZERO(&writefds);
  837. if (SSL_in_init(con) && !SSL_total_renegotiations(con))
  838. {
  839. in_init=1;
  840. tty_on=0;
  841. }
  842. else
  843. {
  844. tty_on=1;
  845. if (in_init)
  846. {
  847. in_init=0;
  848. #ifndef OPENSSL_NO_TLSEXT
  849. if (servername != NULL && !SSL_session_reused(con))
  850. {
  851. BIO_printf(bio_c_out,"Server did %sacknowledge servername extension.\n",tlsextcbp.ack?"":"not ");
  852. }
  853. #endif
  854. print_stuff(bio_c_out,con,full_log);
  855. if (full_log > 0) full_log--;
  856. if (starttls_proto)
  857. {
  858. BIO_printf(bio_err,"%s",mbuf);
  859. /* We don't need to know any more */
  860. starttls_proto = 0;
  861. }
  862. if (reconnect)
  863. {
  864. reconnect--;
  865. BIO_printf(bio_c_out,"drop connection and then reconnect\n");
  866. SSL_shutdown(con);
  867. SSL_set_connect_state(con);
  868. SHUTDOWN(SSL_get_fd(con));
  869. goto re_start;
  870. }
  871. }
  872. }
  873. ssl_pending = read_ssl && SSL_pending(con);
  874. if (!ssl_pending)
  875. {
  876. #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) && !defined (OPENSSL_SYS_BEOS_R5)
  877. if (tty_on)
  878. {
  879. if (read_tty) openssl_fdset(fileno(stdin),&readfds);
  880. if (write_tty) openssl_fdset(fileno(stdout),&writefds);
  881. }
  882. if (read_ssl)
  883. openssl_fdset(SSL_get_fd(con),&readfds);
  884. if (write_ssl)
  885. openssl_fdset(SSL_get_fd(con),&writefds);
  886. #else
  887. if(!tty_on || !write_tty) {
  888. if (read_ssl)
  889. openssl_fdset(SSL_get_fd(con),&readfds);
  890. if (write_ssl)
  891. openssl_fdset(SSL_get_fd(con),&writefds);
  892. }
  893. #endif
  894. /* printf("mode tty(%d %d%d) ssl(%d%d)\n",
  895. tty_on,read_tty,write_tty,read_ssl,write_ssl);*/
  896. /* Note: under VMS with SOCKETSHR the second parameter
  897. * is currently of type (int *) whereas under other
  898. * systems it is (void *) if you don't have a cast it
  899. * will choke the compiler: if you do have a cast then
  900. * you can either go for (int *) or (void *).
  901. */
  902. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
  903. /* Under Windows/DOS we make the assumption that we can
  904. * always write to the tty: therefore if we need to
  905. * write to the tty we just fall through. Otherwise
  906. * we timeout the select every second and see if there
  907. * are any keypresses. Note: this is a hack, in a proper
  908. * Windows application we wouldn't do this.
  909. */
  910. i=0;
  911. if(!write_tty) {
  912. if(read_tty) {
  913. tv.tv_sec = 1;
  914. tv.tv_usec = 0;
  915. i=select(width,(void *)&readfds,(void *)&writefds,
  916. NULL,&tv);
  917. #if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
  918. if(!i && (!_kbhit() || !read_tty) ) continue;
  919. #else
  920. if(!i && (!((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0))) || !read_tty) ) continue;
  921. #endif
  922. } else i=select(width,(void *)&readfds,(void *)&writefds,
  923. NULL,NULL);
  924. }
  925. #elif defined(OPENSSL_SYS_NETWARE)
  926. if(!write_tty) {
  927. if(read_tty) {
  928. tv.tv_sec = 1;
  929. tv.tv_usec = 0;
  930. i=select(width,(void *)&readfds,(void *)&writefds,
  931. NULL,&tv);
  932. } else i=select(width,(void *)&readfds,(void *)&writefds,
  933. NULL,NULL);
  934. }
  935. #elif defined(OPENSSL_SYS_BEOS_R5)
  936. /* Under BeOS-R5 the situation is similar to DOS */
  937. i=0;
  938. stdin_set = 0;
  939. (void)fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
  940. if(!write_tty) {
  941. if(read_tty) {
  942. tv.tv_sec = 1;
  943. tv.tv_usec = 0;
  944. i=select(width,(void *)&readfds,(void *)&writefds,
  945. NULL,&tv);
  946. if (read(fileno(stdin), sbuf, 0) >= 0)
  947. stdin_set = 1;
  948. if (!i && (stdin_set != 1 || !read_tty))
  949. continue;
  950. } else i=select(width,(void *)&readfds,(void *)&writefds,
  951. NULL,NULL);
  952. }
  953. (void)fcntl(fileno(stdin), F_SETFL, 0);
  954. #else
  955. i=select(width,(void *)&readfds,(void *)&writefds,
  956. NULL,NULL);
  957. #endif
  958. if ( i < 0)
  959. {
  960. BIO_printf(bio_err,"bad select %d\n",
  961. get_last_socket_error());
  962. goto shut;
  963. /* goto end; */
  964. }
  965. }
  966. if (!ssl_pending && FD_ISSET(SSL_get_fd(con),&writefds))
  967. {
  968. k=SSL_write(con,&(cbuf[cbuf_off]),
  969. (unsigned int)cbuf_len);
  970. switch (SSL_get_error(con,k))
  971. {
  972. case SSL_ERROR_NONE:
  973. cbuf_off+=k;
  974. cbuf_len-=k;
  975. if (k <= 0) goto end;
  976. /* we have done a write(con,NULL,0); */
  977. if (cbuf_len <= 0)
  978. {
  979. read_tty=1;
  980. write_ssl=0;
  981. }
  982. else /* if (cbuf_len > 0) */
  983. {
  984. read_tty=0;
  985. write_ssl=1;
  986. }
  987. break;
  988. case SSL_ERROR_WANT_WRITE:
  989. BIO_printf(bio_c_out,"write W BLOCK\n");
  990. write_ssl=1;
  991. read_tty=0;
  992. break;
  993. case SSL_ERROR_WANT_READ:
  994. BIO_printf(bio_c_out,"write R BLOCK\n");
  995. write_tty=0;
  996. read_ssl=1;
  997. write_ssl=0;
  998. break;
  999. case SSL_ERROR_WANT_X509_LOOKUP:
  1000. BIO_printf(bio_c_out,"write X BLOCK\n");
  1001. break;
  1002. case SSL_ERROR_ZERO_RETURN:
  1003. if (cbuf_len != 0)
  1004. {
  1005. BIO_printf(bio_c_out,"shutdown\n");
  1006. goto shut;
  1007. }
  1008. else
  1009. {
  1010. read_tty=1;
  1011. write_ssl=0;
  1012. break;
  1013. }
  1014. case SSL_ERROR_SYSCALL:
  1015. if ((k != 0) || (cbuf_len != 0))
  1016. {
  1017. BIO_printf(bio_err,"write:errno=%d\n",
  1018. get_last_socket_error());
  1019. goto shut;
  1020. }
  1021. else
  1022. {
  1023. read_tty=1;
  1024. write_ssl=0;
  1025. }
  1026. break;
  1027. case SSL_ERROR_SSL:
  1028. ERR_print_errors(bio_err);
  1029. goto shut;
  1030. }
  1031. }
  1032. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)
  1033. /* Assume Windows/DOS/BeOS can always write */
  1034. else if (!ssl_pending && write_tty)
  1035. #else
  1036. else if (!ssl_pending && FD_ISSET(fileno(stdout),&writefds))
  1037. #endif
  1038. {
  1039. #ifdef CHARSET_EBCDIC
  1040. ascii2ebcdic(&(sbuf[sbuf_off]),&(sbuf[sbuf_off]),sbuf_len);
  1041. #endif
  1042. i=raw_write_stdout(&(sbuf[sbuf_off]),sbuf_len);
  1043. if (i <= 0)
  1044. {
  1045. BIO_printf(bio_c_out,"DONE\n");
  1046. goto shut;
  1047. /* goto end; */
  1048. }
  1049. sbuf_len-=i;;
  1050. sbuf_off+=i;
  1051. if (sbuf_len <= 0)
  1052. {
  1053. read_ssl=1;
  1054. write_tty=0;
  1055. }
  1056. }
  1057. else if (ssl_pending || FD_ISSET(SSL_get_fd(con),&readfds))
  1058. {
  1059. #ifdef RENEG
  1060. { static int iiii; if (++iiii == 52) { SSL_renegotiate(con); iiii=0; } }
  1061. #endif
  1062. #if 1
  1063. k=SSL_read(con,sbuf,1024 /* BUFSIZZ */ );
  1064. #else
  1065. /* Demo for pending and peek :-) */
  1066. k=SSL_read(con,sbuf,16);
  1067. { char zbuf[10240];
  1068. printf("read=%d pending=%d peek=%d\n",k,SSL_pending(con),SSL_peek(con,zbuf,10240));
  1069. }
  1070. #endif
  1071. switch (SSL_get_error(con,k))
  1072. {
  1073. case SSL_ERROR_NONE:
  1074. if (k <= 0)
  1075. goto end;
  1076. sbuf_off=0;
  1077. sbuf_len=k;
  1078. read_ssl=0;
  1079. write_tty=1;
  1080. break;
  1081. case SSL_ERROR_WANT_WRITE:
  1082. BIO_printf(bio_c_out,"read W BLOCK\n");
  1083. write_ssl=1;
  1084. read_tty=0;
  1085. break;
  1086. case SSL_ERROR_WANT_READ:
  1087. BIO_printf(bio_c_out,"read R BLOCK\n");
  1088. write_tty=0;
  1089. read_ssl=1;
  1090. if ((read_tty == 0) && (write_ssl == 0))
  1091. write_ssl=1;
  1092. break;
  1093. case SSL_ERROR_WANT_X509_LOOKUP:
  1094. BIO_printf(bio_c_out,"read X BLOCK\n");
  1095. break;
  1096. case SSL_ERROR_SYSCALL:
  1097. BIO_printf(bio_err,"read:errno=%d\n",get_last_socket_error());
  1098. goto shut;
  1099. case SSL_ERROR_ZERO_RETURN:
  1100. BIO_printf(bio_c_out,"closed\n");
  1101. goto shut;
  1102. case SSL_ERROR_SSL:
  1103. ERR_print_errors(bio_err);
  1104. goto shut;
  1105. /* break; */
  1106. }
  1107. }
  1108. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
  1109. #if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
  1110. else if (_kbhit())
  1111. #else
  1112. else if ((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0)))
  1113. #endif
  1114. #elif defined (OPENSSL_SYS_NETWARE)
  1115. else if (_kbhit())
  1116. #elif defined(OPENSSL_SYS_BEOS_R5)
  1117. else if (stdin_set)
  1118. #else
  1119. else if (FD_ISSET(fileno(stdin),&readfds))
  1120. #endif
  1121. {
  1122. if (crlf)
  1123. {
  1124. int j, lf_num;
  1125. i=raw_read_stdin(cbuf,BUFSIZZ/2);
  1126. lf_num = 0;
  1127. /* both loops are skipped when i <= 0 */
  1128. for (j = 0; j < i; j++)
  1129. if (cbuf[j] == '\n')
  1130. lf_num++;
  1131. for (j = i-1; j >= 0; j--)
  1132. {
  1133. cbuf[j+lf_num] = cbuf[j];
  1134. if (cbuf[j] == '\n')
  1135. {
  1136. lf_num--;
  1137. i++;
  1138. cbuf[j+lf_num] = '\r';
  1139. }
  1140. }
  1141. assert(lf_num == 0);
  1142. }
  1143. else
  1144. i=raw_read_stdin(cbuf,BUFSIZZ);
  1145. if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q')))
  1146. {
  1147. BIO_printf(bio_err,"DONE\n");
  1148. goto shut;
  1149. }
  1150. if ((!c_ign_eof) && (cbuf[0] == 'R'))
  1151. {
  1152. BIO_printf(bio_err,"RENEGOTIATING\n");
  1153. SSL_renegotiate(con);
  1154. cbuf_len=0;
  1155. }
  1156. else
  1157. {
  1158. cbuf_len=i;
  1159. cbuf_off=0;
  1160. #ifdef CHARSET_EBCDIC
  1161. ebcdic2ascii(cbuf, cbuf, i);
  1162. #endif
  1163. }
  1164. write_ssl=1;
  1165. read_tty=0;
  1166. }
  1167. }
  1168. shut:
  1169. SSL_shutdown(con);
  1170. SHUTDOWN(SSL_get_fd(con));
  1171. ret=0;
  1172. end:
  1173. if (con != NULL)
  1174. {
  1175. if (prexit != 0)
  1176. print_stuff(bio_c_out,con,1);
  1177. SSL_free(con);
  1178. }
  1179. if (ctx != NULL) SSL_CTX_free(ctx);
  1180. if (cert)
  1181. X509_free(cert);
  1182. if (key)
  1183. EVP_PKEY_free(key);
  1184. if (pass)
  1185. OPENSSL_free(pass);
  1186. if (cbuf != NULL) { OPENSSL_cleanse(cbuf,BUFSIZZ); OPENSSL_free(cbuf); }
  1187. if (sbuf != NULL) { OPENSSL_cleanse(sbuf,BUFSIZZ); OPENSSL_free(sbuf); }
  1188. if (mbuf != NULL) { OPENSSL_cleanse(mbuf,BUFSIZZ); OPENSSL_free(mbuf); }
  1189. if (bio_c_out != NULL)
  1190. {
  1191. BIO_free(bio_c_out);
  1192. bio_c_out=NULL;
  1193. }
  1194. apps_shutdown();
  1195. OPENSSL_EXIT(ret);
  1196. }
  1197. static void print_stuff(BIO *bio, SSL *s, int full)
  1198. {
  1199. X509 *peer=NULL;
  1200. char *p;
  1201. static const char *space=" ";
  1202. char buf[BUFSIZ];
  1203. STACK_OF(X509) *sk;
  1204. STACK_OF(X509_NAME) *sk2;
  1205. SSL_CIPHER *c;
  1206. X509_NAME *xn;
  1207. int j,i;
  1208. #ifndef OPENSSL_NO_COMP
  1209. const COMP_METHOD *comp, *expansion;
  1210. #endif
  1211. if (full)
  1212. {
  1213. int got_a_chain = 0;
  1214. sk=SSL_get_peer_cert_chain(s);
  1215. if (sk != NULL)
  1216. {
  1217. got_a_chain = 1; /* we don't have it for SSL2 (yet) */
  1218. BIO_printf(bio,"---\nCertificate chain\n");
  1219. for (i=0; i<sk_X509_num(sk); i++)
  1220. {
  1221. X509_NAME_oneline(X509_get_subject_name(
  1222. sk_X509_value(sk,i)),buf,sizeof buf);
  1223. BIO_printf(bio,"%2d s:%s\n",i,buf);
  1224. X509_NAME_oneline(X509_get_issuer_name(
  1225. sk_X509_value(sk,i)),buf,sizeof buf);
  1226. BIO_printf(bio," i:%s\n",buf);
  1227. if (c_showcerts)
  1228. PEM_write_bio_X509(bio,sk_X509_value(sk,i));
  1229. }
  1230. }
  1231. BIO_printf(bio,"---\n");
  1232. peer=SSL_get_peer_certificate(s);
  1233. if (peer != NULL)
  1234. {
  1235. BIO_printf(bio,"Server certificate\n");
  1236. if (!(c_showcerts && got_a_chain)) /* Redundant if we showed the whole chain */
  1237. PEM_write_bio_X509(bio,peer);
  1238. X509_NAME_oneline(X509_get_subject_name(peer),
  1239. buf,sizeof buf);
  1240. BIO_printf(bio,"subject=%s\n",buf);
  1241. X509_NAME_oneline(X509_get_issuer_name(peer),
  1242. buf,sizeof buf);
  1243. BIO_printf(bio,"issuer=%s\n",buf);
  1244. }
  1245. else
  1246. BIO_printf(bio,"no peer certificate available\n");
  1247. sk2=SSL_get_client_CA_list(s);
  1248. if ((sk2 != NULL) && (sk_X509_NAME_num(sk2) > 0))
  1249. {
  1250. BIO_printf(bio,"---\nAcceptable client certificate CA names\n");
  1251. for (i=0; i<sk_X509_NAME_num(sk2); i++)
  1252. {
  1253. xn=sk_X509_NAME_value(sk2,i);
  1254. X509_NAME_oneline(xn,buf,sizeof(buf));
  1255. BIO_write(bio,buf,strlen(buf));
  1256. BIO_write(bio,"\n",1);
  1257. }
  1258. }
  1259. else
  1260. {
  1261. BIO_printf(bio,"---\nNo client certificate CA names sent\n");
  1262. }
  1263. p=SSL_get_shared_ciphers(s,buf,sizeof buf);
  1264. if (p != NULL)
  1265. {
  1266. /* This works only for SSL 2. In later protocol
  1267. * versions, the client does not know what other
  1268. * ciphers (in addition to the one to be used
  1269. * in the current connection) the server supports. */
  1270. BIO_printf(bio,"---\nCiphers common between both SSL endpoints:\n");
  1271. j=i=0;
  1272. while (*p)
  1273. {
  1274. if (*p == ':')
  1275. {
  1276. BIO_write(bio,space,15-j%25);
  1277. i++;
  1278. j=0;
  1279. BIO_write(bio,((i%3)?" ":"\n"),1);
  1280. }
  1281. else
  1282. {
  1283. BIO_write(bio,p,1);
  1284. j++;
  1285. }
  1286. p++;
  1287. }
  1288. BIO_write(bio,"\n",1);
  1289. }
  1290. BIO_printf(bio,"---\nSSL handshake has read %ld bytes and written %ld bytes\n",
  1291. BIO_number_read(SSL_get_rbio(s)),
  1292. BIO_number_written(SSL_get_wbio(s)));
  1293. }
  1294. BIO_printf(bio,((s->hit)?"---\nReused, ":"---\nNew, "));
  1295. c=SSL_get_current_cipher(s);
  1296. BIO_printf(bio,"%s, Cipher is %s\n",
  1297. SSL_CIPHER_get_version(c),
  1298. SSL_CIPHER_get_name(c));
  1299. if (peer != NULL) {
  1300. EVP_PKEY *pktmp;
  1301. pktmp = X509_get_pubkey(peer);
  1302. BIO_printf(bio,"Server public key is %d bit\n",
  1303. EVP_PKEY_bits(pktmp));
  1304. EVP_PKEY_free(pktmp);
  1305. }
  1306. #ifndef OPENSSL_NO_COMP
  1307. comp=SSL_get_current_compression(s);
  1308. expansion=SSL_get_current_expansion(s);
  1309. BIO_printf(bio,"Compression: %s\n",
  1310. comp ? SSL_COMP_get_name(comp) : "NONE");
  1311. BIO_printf(bio,"Expansion: %s\n",
  1312. expansion ? SSL_COMP_get_name(expansion) : "NONE");
  1313. #endif
  1314. SSL_SESSION_print(bio,SSL_get_session(s));
  1315. BIO_printf(bio,"---\n");
  1316. if (peer != NULL)
  1317. X509_free(peer);
  1318. /* flush, or debugging output gets mixed with http response */
  1319. BIO_flush(bio);
  1320. }