s_client.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  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-2001 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. #include <assert.h>
  112. #include <stdio.h>
  113. #include <stdlib.h>
  114. #include <string.h>
  115. #include <openssl/e_os2.h>
  116. #ifdef OPENSSL_NO_STDIO
  117. # define APPS_WIN16
  118. #endif
  119. /*
  120. * With IPv6, it looks like Digital has mixed up the proper order of
  121. * recursive header file inclusion, resulting in the compiler complaining
  122. * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
  123. * needed to have fileno() declared correctly... So let's define u_int
  124. */
  125. #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
  126. # define __U_INT
  127. typedef unsigned int u_int;
  128. #endif
  129. #define USE_SOCKETS
  130. #include "apps.h"
  131. #include <openssl/x509.h>
  132. #include <openssl/ssl.h>
  133. #include <openssl/err.h>
  134. #include <openssl/pem.h>
  135. #include <openssl/rand.h>
  136. #include <openssl/ocsp.h>
  137. #include "s_apps.h"
  138. #include "timeouts.h"
  139. #ifdef OPENSSL_SYS_WINCE
  140. /*
  141. * Windows CE incorrectly defines fileno as returning void*, so to avoid
  142. * problems below...
  143. */
  144. # ifdef fileno
  145. # undef fileno
  146. # endif
  147. # define fileno(a) (int)_fileno(a)
  148. #endif
  149. #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
  150. /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
  151. # undef FIONBIO
  152. #endif
  153. #undef PROG
  154. #define PROG s_client_main
  155. /*
  156. * #define SSL_HOST_NAME "www.netscape.com"
  157. */
  158. /*
  159. * #define SSL_HOST_NAME "193.118.187.102"
  160. */
  161. #define SSL_HOST_NAME "localhost"
  162. /* no default cert. */
  163. /*
  164. * #define TEST_CERT "client.pem"
  165. */
  166. #undef BUFSIZZ
  167. #define BUFSIZZ 1024*8
  168. extern int verify_depth;
  169. extern int verify_error;
  170. #ifdef FIONBIO
  171. static int c_nbio = 0;
  172. #endif
  173. static int c_Pause = 0;
  174. static int c_debug = 0;
  175. #ifndef OPENSSL_NO_TLSEXT
  176. static int c_tlsextdebug = 0;
  177. static int c_status_req = 0;
  178. #endif
  179. static int c_msg = 0;
  180. static int c_showcerts = 0;
  181. static void sc_usage(void);
  182. static void print_stuff(BIO *berr, SSL *con, int full);
  183. #ifndef OPENSSL_NO_TLSEXT
  184. static int ocsp_resp_cb(SSL *s, void *arg);
  185. #endif
  186. static BIO *bio_c_out = NULL;
  187. static int c_quiet = 0;
  188. static int c_ign_eof = 0;
  189. static void sc_usage(void)
  190. {
  191. BIO_printf(bio_err, "usage: s_client args\n");
  192. BIO_printf(bio_err, "\n");
  193. BIO_printf(bio_err, " -host host - use -connect instead\n");
  194. BIO_printf(bio_err, " -port port - use -connect instead\n");
  195. BIO_printf(bio_err,
  196. " -connect host:port - who to connect to (default is %s:%s)\n",
  197. SSL_HOST_NAME, PORT_STR);
  198. BIO_printf(bio_err,
  199. " -verify depth - turn on peer certificate verification\n");
  200. BIO_printf(bio_err,
  201. " -cert arg - certificate file to use, PEM format assumed\n");
  202. BIO_printf(bio_err,
  203. " -certform arg - certificate format (PEM or DER) PEM default\n");
  204. BIO_printf(bio_err,
  205. " -key arg - Private key file to use, in cert file if\n");
  206. BIO_printf(bio_err, " not specified but cert file is.\n");
  207. BIO_printf(bio_err,
  208. " -keyform arg - key format (PEM or DER) PEM default\n");
  209. BIO_printf(bio_err,
  210. " -pass arg - private key file pass phrase source\n");
  211. BIO_printf(bio_err, " -CApath arg - PEM format directory of CA's\n");
  212. BIO_printf(bio_err, " -CAfile arg - PEM format file of CA's\n");
  213. BIO_printf(bio_err,
  214. " -reconnect - Drop and re-make the connection with the same Session-ID\n");
  215. BIO_printf(bio_err,
  216. " -pause - sleep(1) after each read(2) and write(2) system call\n");
  217. BIO_printf(bio_err,
  218. " -showcerts - show all certificates in the chain\n");
  219. BIO_printf(bio_err, " -debug - extra output\n");
  220. #ifdef WATT32
  221. BIO_printf(bio_err, " -wdebug - WATT-32 tcp debugging\n");
  222. #endif
  223. BIO_printf(bio_err, " -msg - Show protocol messages\n");
  224. BIO_printf(bio_err, " -nbio_test - more ssl protocol testing\n");
  225. BIO_printf(bio_err, " -state - print the 'ssl' states\n");
  226. #ifdef FIONBIO
  227. BIO_printf(bio_err, " -nbio - Run with non-blocking IO\n");
  228. #endif
  229. BIO_printf(bio_err,
  230. " -crlf - convert LF from terminal into CRLF\n");
  231. BIO_printf(bio_err, " -quiet - no s_client output\n");
  232. BIO_printf(bio_err,
  233. " -ign_eof - ignore input eof (default when -quiet)\n");
  234. BIO_printf(bio_err, " -no_ign_eof - don't ignore input eof\n");
  235. BIO_printf(bio_err, " -ssl2 - just use SSLv2\n");
  236. BIO_printf(bio_err, " -ssl3 - just use SSLv3\n");
  237. BIO_printf(bio_err, " -tls1 - just use TLSv1\n");
  238. BIO_printf(bio_err, " -dtls1 - just use DTLSv1\n");
  239. BIO_printf(bio_err, " -fallback_scsv - send TLS_FALLBACK_SCSV\n");
  240. BIO_printf(bio_err, " -mtu - set the link layer MTU\n");
  241. BIO_printf(bio_err,
  242. " -no_tls1/-no_ssl3/-no_ssl2 - turn off that protocol\n");
  243. BIO_printf(bio_err,
  244. " -bugs - Switch on all SSL implementation bug workarounds\n");
  245. BIO_printf(bio_err,
  246. " -serverpref - Use server's cipher preferences (only SSLv2)\n");
  247. BIO_printf(bio_err,
  248. " -cipher - preferred cipher to use, use the 'openssl ciphers'\n");
  249. BIO_printf(bio_err,
  250. " command to see what is available\n");
  251. BIO_printf(bio_err,
  252. " -starttls prot - use the STARTTLS command before starting TLS\n");
  253. BIO_printf(bio_err,
  254. " for those protocols that support it, where\n");
  255. BIO_printf(bio_err,
  256. " 'prot' defines which one to assume. Currently,\n");
  257. BIO_printf(bio_err,
  258. " only \"smtp\", \"pop3\", \"imap\", \"ftp\" and \"xmpp\"\n");
  259. BIO_printf(bio_err, " are supported.\n");
  260. #ifndef OPENSSL_NO_ENGINE
  261. BIO_printf(bio_err,
  262. " -engine id - Initialise and use the specified engine\n");
  263. #endif
  264. BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
  265. LIST_SEPARATOR_CHAR);
  266. BIO_printf(bio_err, " -sess_out arg - file to write SSL session to\n");
  267. BIO_printf(bio_err, " -sess_in arg - file to read SSL session from\n");
  268. #ifndef OPENSSL_NO_TLSEXT
  269. BIO_printf(bio_err,
  270. " -servername host - Set TLS extension servername in ClientHello\n");
  271. BIO_printf(bio_err,
  272. " -tlsextdebug - hex dump of all TLS extensions received\n");
  273. BIO_printf(bio_err,
  274. " -status - request certificate status from server\n");
  275. BIO_printf(bio_err,
  276. " -no_ticket - disable use of RFC4507bis session tickets\n");
  277. #endif
  278. BIO_printf(bio_err,
  279. " -legacy_renegotiation - enable use of legacy renegotiation (dangerous)\n");
  280. }
  281. #ifndef OPENSSL_NO_TLSEXT
  282. /* This is a context that we pass to callbacks */
  283. typedef struct tlsextctx_st {
  284. BIO *biodebug;
  285. int ack;
  286. } tlsextctx;
  287. static int MS_CALLBACK ssl_servername_cb(SSL *s, int *ad, void *arg)
  288. {
  289. tlsextctx *p = (tlsextctx *) arg;
  290. const char *hn = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
  291. if (SSL_get_servername_type(s) != -1)
  292. p->ack = !SSL_session_reused(s) && hn != NULL;
  293. else
  294. BIO_printf(bio_err, "Can't use SSL_get_servername\n");
  295. return SSL_TLSEXT_ERR_OK;
  296. }
  297. #endif
  298. enum {
  299. PROTO_OFF = 0,
  300. PROTO_SMTP,
  301. PROTO_POP3,
  302. PROTO_IMAP,
  303. PROTO_FTP,
  304. PROTO_XMPP
  305. };
  306. int MAIN(int, char **);
  307. int MAIN(int argc, char **argv)
  308. {
  309. int off = 0, clr = 0;
  310. SSL *con = NULL, *con2 = NULL;
  311. X509_STORE *store = NULL;
  312. int s, k, width, state = 0;
  313. char *cbuf = NULL, *sbuf = NULL, *mbuf = NULL;
  314. int cbuf_len, cbuf_off;
  315. int sbuf_len, sbuf_off;
  316. fd_set readfds, writefds;
  317. short port = PORT;
  318. int full_log = 1;
  319. char *host = SSL_HOST_NAME;
  320. char *cert_file = NULL, *key_file = NULL;
  321. int cert_format = FORMAT_PEM, key_format = FORMAT_PEM;
  322. char *passarg = NULL, *pass = NULL;
  323. X509 *cert = NULL;
  324. EVP_PKEY *key = NULL;
  325. char *CApath = NULL, *CAfile = NULL, *cipher = NULL;
  326. int reconnect = 0, badop = 0, verify = SSL_VERIFY_NONE, bugs = 0;
  327. int crlf = 0;
  328. int write_tty, read_tty, write_ssl, read_ssl, tty_on, ssl_pending;
  329. SSL_CTX *ctx = NULL;
  330. int ret = 1, in_init = 1, i, nbio_test = 0;
  331. int starttls_proto = PROTO_OFF;
  332. int prexit = 0, vflags = 0;
  333. SSL_METHOD *meth = NULL;
  334. #ifdef sock_type
  335. # undef sock_type
  336. #endif
  337. int sock_type = SOCK_STREAM;
  338. BIO *sbio;
  339. char *inrand = NULL;
  340. int mbuf_len = 0;
  341. struct timeval timeout, *timeoutp;
  342. #ifndef OPENSSL_NO_ENGINE
  343. char *engine_id = NULL;
  344. char *ssl_client_engine_id = NULL;
  345. ENGINE *ssl_client_engine = NULL;
  346. #endif
  347. ENGINE *e = NULL;
  348. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)
  349. struct timeval tv;
  350. #endif
  351. #ifndef OPENSSL_NO_TLSEXT
  352. char *servername = NULL;
  353. tlsextctx tlsextcbp = { NULL, 0 };
  354. #endif
  355. char *sess_in = NULL;
  356. char *sess_out = NULL;
  357. struct sockaddr peer;
  358. int peerlen = sizeof(peer);
  359. int fallback_scsv = 0;
  360. int enable_timeouts = 0;
  361. long socket_mtu = 0;
  362. #ifndef OPENSSL_NO_JPAKE
  363. char *jpake_secret = NULL;
  364. #endif
  365. meth = SSLv23_client_method();
  366. apps_startup();
  367. c_Pause = 0;
  368. c_quiet = 0;
  369. c_ign_eof = 0;
  370. c_debug = 0;
  371. c_msg = 0;
  372. c_showcerts = 0;
  373. if (bio_err == NULL)
  374. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  375. if (!load_config(bio_err, NULL))
  376. goto end;
  377. if (((cbuf = OPENSSL_malloc(BUFSIZZ)) == NULL) ||
  378. ((sbuf = OPENSSL_malloc(BUFSIZZ)) == NULL) ||
  379. ((mbuf = OPENSSL_malloc(BUFSIZZ)) == NULL)) {
  380. BIO_printf(bio_err, "out of memory\n");
  381. goto end;
  382. }
  383. verify_depth = 0;
  384. verify_error = X509_V_OK;
  385. #ifdef FIONBIO
  386. c_nbio = 0;
  387. #endif
  388. argc--;
  389. argv++;
  390. while (argc >= 1) {
  391. if (strcmp(*argv, "-host") == 0) {
  392. if (--argc < 1)
  393. goto bad;
  394. host = *(++argv);
  395. } else if (strcmp(*argv, "-port") == 0) {
  396. if (--argc < 1)
  397. goto bad;
  398. port = atoi(*(++argv));
  399. if (port == 0)
  400. goto bad;
  401. } else if (strcmp(*argv, "-connect") == 0) {
  402. if (--argc < 1)
  403. goto bad;
  404. if (!extract_host_port(*(++argv), &host, NULL, &port))
  405. goto bad;
  406. } else if (strcmp(*argv, "-verify") == 0) {
  407. verify = SSL_VERIFY_PEER;
  408. if (--argc < 1)
  409. goto bad;
  410. verify_depth = atoi(*(++argv));
  411. BIO_printf(bio_err, "verify depth is %d\n", verify_depth);
  412. } else if (strcmp(*argv, "-cert") == 0) {
  413. if (--argc < 1)
  414. goto bad;
  415. cert_file = *(++argv);
  416. } else if (strcmp(*argv, "-sess_out") == 0) {
  417. if (--argc < 1)
  418. goto bad;
  419. sess_out = *(++argv);
  420. } else if (strcmp(*argv, "-sess_in") == 0) {
  421. if (--argc < 1)
  422. goto bad;
  423. sess_in = *(++argv);
  424. } else if (strcmp(*argv, "-certform") == 0) {
  425. if (--argc < 1)
  426. goto bad;
  427. cert_format = str2fmt(*(++argv));
  428. } else if (strcmp(*argv, "-crl_check") == 0)
  429. vflags |= X509_V_FLAG_CRL_CHECK;
  430. else if (strcmp(*argv, "-crl_check_all") == 0)
  431. vflags |= X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL;
  432. else if (strcmp(*argv, "-prexit") == 0)
  433. prexit = 1;
  434. else if (strcmp(*argv, "-crlf") == 0)
  435. crlf = 1;
  436. else if (strcmp(*argv, "-quiet") == 0) {
  437. c_quiet = 1;
  438. c_ign_eof = 1;
  439. } else if (strcmp(*argv, "-ign_eof") == 0)
  440. c_ign_eof = 1;
  441. else if (strcmp(*argv, "-no_ign_eof") == 0)
  442. c_ign_eof = 0;
  443. else if (strcmp(*argv, "-pause") == 0)
  444. c_Pause = 1;
  445. else if (strcmp(*argv, "-debug") == 0)
  446. c_debug = 1;
  447. #ifndef OPENSSL_NO_TLSEXT
  448. else if (strcmp(*argv, "-tlsextdebug") == 0)
  449. c_tlsextdebug = 1;
  450. else if (strcmp(*argv, "-status") == 0)
  451. c_status_req = 1;
  452. #endif
  453. #ifdef WATT32
  454. else if (strcmp(*argv, "-wdebug") == 0)
  455. dbug_init();
  456. #endif
  457. else if (strcmp(*argv, "-msg") == 0)
  458. c_msg = 1;
  459. else if (strcmp(*argv, "-showcerts") == 0)
  460. c_showcerts = 1;
  461. else if (strcmp(*argv, "-nbio_test") == 0)
  462. nbio_test = 1;
  463. else if (strcmp(*argv, "-state") == 0)
  464. state = 1;
  465. #ifndef OPENSSL_NO_SSL2
  466. else if (strcmp(*argv, "-ssl2") == 0)
  467. meth = SSLv2_client_method();
  468. #endif
  469. #ifndef OPENSSL_NO_SSL3
  470. else if (strcmp(*argv, "-ssl3") == 0)
  471. meth = SSLv3_client_method();
  472. #endif
  473. #ifndef OPENSSL_NO_TLS1
  474. else if (strcmp(*argv, "-tls1") == 0)
  475. meth = TLSv1_client_method();
  476. #endif
  477. #ifndef OPENSSL_NO_DTLS1
  478. else if (strcmp(*argv, "-dtls1") == 0) {
  479. meth = DTLSv1_client_method();
  480. sock_type = SOCK_DGRAM;
  481. } else if (strcmp(*argv, "-timeout") == 0)
  482. enable_timeouts = 1;
  483. else if (strcmp(*argv, "-mtu") == 0) {
  484. if (--argc < 1)
  485. goto bad;
  486. socket_mtu = atol(*(++argv));
  487. }
  488. #endif
  489. else if (strcmp(*argv, "-fallback_scsv") == 0) {
  490. fallback_scsv = 1;
  491. } else if (strcmp(*argv, "-bugs") == 0)
  492. bugs = 1;
  493. else if (strcmp(*argv, "-keyform") == 0) {
  494. if (--argc < 1)
  495. goto bad;
  496. key_format = str2fmt(*(++argv));
  497. } else if (strcmp(*argv, "-pass") == 0) {
  498. if (--argc < 1)
  499. goto bad;
  500. passarg = *(++argv);
  501. } else if (strcmp(*argv, "-key") == 0) {
  502. if (--argc < 1)
  503. goto bad;
  504. key_file = *(++argv);
  505. } else if (strcmp(*argv, "-reconnect") == 0) {
  506. reconnect = 5;
  507. } else if (strcmp(*argv, "-CApath") == 0) {
  508. if (--argc < 1)
  509. goto bad;
  510. CApath = *(++argv);
  511. } else if (strcmp(*argv, "-CAfile") == 0) {
  512. if (--argc < 1)
  513. goto bad;
  514. CAfile = *(++argv);
  515. } else if (strcmp(*argv, "-no_tls1") == 0)
  516. off |= SSL_OP_NO_TLSv1;
  517. else if (strcmp(*argv, "-no_ssl3") == 0)
  518. off |= SSL_OP_NO_SSLv3;
  519. else if (strcmp(*argv, "-no_ssl2") == 0)
  520. off |= SSL_OP_NO_SSLv2;
  521. #ifndef OPENSSL_NO_TLSEXT
  522. else if (strcmp(*argv, "-no_ticket") == 0) {
  523. off |= SSL_OP_NO_TICKET;
  524. }
  525. #endif
  526. else if (strcmp(*argv, "-serverpref") == 0)
  527. off |= SSL_OP_CIPHER_SERVER_PREFERENCE;
  528. else if (strcmp(*argv, "-legacy_renegotiation") == 0)
  529. off |= SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION;
  530. else if (strcmp(*argv, "-legacy_server_connect") == 0) {
  531. off |= SSL_OP_LEGACY_SERVER_CONNECT;
  532. } else if (strcmp(*argv, "-no_legacy_server_connect") == 0) {
  533. clr |= SSL_OP_LEGACY_SERVER_CONNECT;
  534. } else if (strcmp(*argv, "-cipher") == 0) {
  535. if (--argc < 1)
  536. goto bad;
  537. cipher = *(++argv);
  538. }
  539. #ifdef FIONBIO
  540. else if (strcmp(*argv, "-nbio") == 0) {
  541. c_nbio = 1;
  542. }
  543. #endif
  544. else if (strcmp(*argv, "-starttls") == 0) {
  545. if (--argc < 1)
  546. goto bad;
  547. ++argv;
  548. if (strcmp(*argv, "smtp") == 0)
  549. starttls_proto = PROTO_SMTP;
  550. else if (strcmp(*argv, "pop3") == 0)
  551. starttls_proto = PROTO_POP3;
  552. else if (strcmp(*argv, "imap") == 0)
  553. starttls_proto = PROTO_IMAP;
  554. else if (strcmp(*argv, "ftp") == 0)
  555. starttls_proto = PROTO_FTP;
  556. else if (strcmp(*argv, "xmpp") == 0)
  557. starttls_proto = PROTO_XMPP;
  558. else
  559. goto bad;
  560. }
  561. #ifndef OPENSSL_NO_ENGINE
  562. else if (strcmp(*argv, "-engine") == 0) {
  563. if (--argc < 1)
  564. goto bad;
  565. engine_id = *(++argv);
  566. } else if (strcmp(*argv, "-ssl_client_engine") == 0) {
  567. if (--argc < 1)
  568. goto bad;
  569. ssl_client_engine_id = *(++argv);
  570. }
  571. #endif
  572. else if (strcmp(*argv, "-rand") == 0) {
  573. if (--argc < 1)
  574. goto bad;
  575. inrand = *(++argv);
  576. }
  577. #ifndef OPENSSL_NO_TLSEXT
  578. else if (strcmp(*argv, "-servername") == 0) {
  579. if (--argc < 1)
  580. goto bad;
  581. servername = *(++argv);
  582. /* meth=TLSv1_client_method(); */
  583. }
  584. #endif
  585. #ifndef OPENSSL_NO_JPAKE
  586. else if (strcmp(*argv, "-jpake") == 0) {
  587. if (--argc < 1)
  588. goto bad;
  589. jpake_secret = *++argv;
  590. }
  591. #endif
  592. else {
  593. BIO_printf(bio_err, "unknown option %s\n", *argv);
  594. badop = 1;
  595. break;
  596. }
  597. argc--;
  598. argv++;
  599. }
  600. if (badop) {
  601. bad:
  602. sc_usage();
  603. goto end;
  604. }
  605. OpenSSL_add_ssl_algorithms();
  606. SSL_load_error_strings();
  607. #ifndef OPENSSL_NO_ENGINE
  608. e = setup_engine(bio_err, engine_id, 1);
  609. if (ssl_client_engine_id) {
  610. ssl_client_engine = ENGINE_by_id(ssl_client_engine_id);
  611. if (!ssl_client_engine) {
  612. BIO_printf(bio_err, "Error getting client auth engine\n");
  613. goto end;
  614. }
  615. }
  616. #endif
  617. if (!app_passwd(bio_err, passarg, NULL, &pass, NULL)) {
  618. BIO_printf(bio_err, "Error getting password\n");
  619. goto end;
  620. }
  621. if (key_file == NULL)
  622. key_file = cert_file;
  623. if (key_file) {
  624. key = load_key(bio_err, key_file, key_format, 0, pass, e,
  625. "client certificate private key file");
  626. if (!key) {
  627. ERR_print_errors(bio_err);
  628. goto end;
  629. }
  630. }
  631. if (cert_file) {
  632. cert = load_cert(bio_err, cert_file, cert_format,
  633. NULL, e, "client certificate file");
  634. if (!cert) {
  635. ERR_print_errors(bio_err);
  636. goto end;
  637. }
  638. }
  639. if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
  640. && !RAND_status()) {
  641. BIO_printf(bio_err,
  642. "warning, not much extra random data, consider using the -rand option\n");
  643. }
  644. if (inrand != NULL)
  645. BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
  646. app_RAND_load_files(inrand));
  647. if (bio_c_out == NULL) {
  648. if (c_quiet && !c_debug && !c_msg) {
  649. bio_c_out = BIO_new(BIO_s_null());
  650. } else {
  651. if (bio_c_out == NULL)
  652. bio_c_out = BIO_new_fp(stdout, BIO_NOCLOSE);
  653. }
  654. }
  655. ctx = SSL_CTX_new(meth);
  656. if (ctx == NULL) {
  657. ERR_print_errors(bio_err);
  658. goto end;
  659. }
  660. #ifndef OPENSSL_NO_ENGINE
  661. if (ssl_client_engine) {
  662. if (!SSL_CTX_set_client_cert_engine(ctx, ssl_client_engine)) {
  663. BIO_puts(bio_err, "Error setting client auth engine\n");
  664. ERR_print_errors(bio_err);
  665. ENGINE_free(ssl_client_engine);
  666. goto end;
  667. }
  668. ENGINE_free(ssl_client_engine);
  669. }
  670. #endif
  671. if (bugs)
  672. SSL_CTX_set_options(ctx, SSL_OP_ALL | off);
  673. else
  674. SSL_CTX_set_options(ctx, off);
  675. if (clr)
  676. SSL_CTX_clear_options(ctx, clr);
  677. /*
  678. * DTLS: partial reads end up discarding unread UDP bytes :-( Setting
  679. * read ahead solves this problem.
  680. */
  681. if (sock_type == SOCK_DGRAM)
  682. SSL_CTX_set_read_ahead(ctx, 1);
  683. if (state)
  684. SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
  685. if (cipher != NULL)
  686. if (!SSL_CTX_set_cipher_list(ctx, cipher)) {
  687. BIO_printf(bio_err, "error setting cipher list\n");
  688. ERR_print_errors(bio_err);
  689. goto end;
  690. }
  691. #if 0
  692. else
  693. SSL_CTX_set_cipher_list(ctx, getenv("SSL_CIPHER"));
  694. #endif
  695. SSL_CTX_set_verify(ctx, verify, verify_callback);
  696. if (!set_cert_key_stuff(ctx, cert, key))
  697. goto end;
  698. if ((!SSL_CTX_load_verify_locations(ctx, CAfile, CApath)) ||
  699. (!SSL_CTX_set_default_verify_paths(ctx))) {
  700. /*
  701. * BIO_printf(bio_err,"error setting default verify locations\n");
  702. */
  703. ERR_print_errors(bio_err);
  704. /* goto end; */
  705. }
  706. store = SSL_CTX_get_cert_store(ctx);
  707. X509_STORE_set_flags(store, vflags);
  708. #ifndef OPENSSL_NO_TLSEXT
  709. if (servername != NULL) {
  710. tlsextcbp.biodebug = bio_err;
  711. SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
  712. SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
  713. }
  714. #endif
  715. con = SSL_new(ctx);
  716. if (sess_in) {
  717. SSL_SESSION *sess;
  718. BIO *stmp = BIO_new_file(sess_in, "r");
  719. if (!stmp) {
  720. BIO_printf(bio_err, "Can't open session file %s\n", sess_in);
  721. ERR_print_errors(bio_err);
  722. goto end;
  723. }
  724. sess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);
  725. BIO_free(stmp);
  726. if (!sess) {
  727. BIO_printf(bio_err, "Can't open session file %s\n", sess_in);
  728. ERR_print_errors(bio_err);
  729. goto end;
  730. }
  731. SSL_set_session(con, sess);
  732. SSL_SESSION_free(sess);
  733. }
  734. if (fallback_scsv)
  735. SSL_set_mode(con, SSL_MODE_SEND_FALLBACK_SCSV);
  736. #ifndef OPENSSL_NO_TLSEXT
  737. if (servername != NULL) {
  738. if (!SSL_set_tlsext_host_name(con, servername)) {
  739. BIO_printf(bio_err, "Unable to set TLS servername extension.\n");
  740. ERR_print_errors(bio_err);
  741. goto end;
  742. }
  743. }
  744. #endif
  745. #ifndef OPENSSL_NO_KRB5
  746. if (con && (con->kssl_ctx = kssl_ctx_new()) != NULL) {
  747. kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVER, host);
  748. }
  749. #endif /* OPENSSL_NO_KRB5 */
  750. /* SSL_set_cipher_list(con,"RC4-MD5"); */
  751. re_start:
  752. if (init_client(&s, host, port, sock_type) == 0) {
  753. BIO_printf(bio_err, "connect:errno=%d\n", get_last_socket_error());
  754. SHUTDOWN(s);
  755. goto end;
  756. }
  757. BIO_printf(bio_c_out, "CONNECTED(%08X)\n", s);
  758. #ifdef FIONBIO
  759. if (c_nbio) {
  760. unsigned long l = 1;
  761. BIO_printf(bio_c_out, "turning on non blocking io\n");
  762. if (BIO_socket_ioctl(s, FIONBIO, &l) < 0) {
  763. ERR_print_errors(bio_err);
  764. goto end;
  765. }
  766. }
  767. #endif
  768. if (c_Pause & 0x01)
  769. con->debug = 1;
  770. if (SSL_version(con) == DTLS1_VERSION) {
  771. sbio = BIO_new_dgram(s, BIO_NOCLOSE);
  772. if (getsockname(s, &peer, (void *)&peerlen) < 0) {
  773. BIO_printf(bio_err, "getsockname:errno=%d\n",
  774. get_last_socket_error());
  775. SHUTDOWN(s);
  776. goto end;
  777. }
  778. (void)BIO_ctrl_set_connected(sbio, 1, &peer);
  779. if (enable_timeouts) {
  780. timeout.tv_sec = 0;
  781. timeout.tv_usec = DGRAM_RCV_TIMEOUT;
  782. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
  783. timeout.tv_sec = 0;
  784. timeout.tv_usec = DGRAM_SND_TIMEOUT;
  785. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
  786. }
  787. if (socket_mtu > 28) {
  788. SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
  789. SSL_set_mtu(con, socket_mtu - 28);
  790. } else
  791. /* want to do MTU discovery */
  792. BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
  793. } else
  794. sbio = BIO_new_socket(s, BIO_NOCLOSE);
  795. if (nbio_test) {
  796. BIO *test;
  797. test = BIO_new(BIO_f_nbio_test());
  798. sbio = BIO_push(test, sbio);
  799. }
  800. if (c_debug) {
  801. con->debug = 1;
  802. BIO_set_callback(sbio, bio_dump_callback);
  803. BIO_set_callback_arg(sbio, (char *)bio_c_out);
  804. }
  805. if (c_msg) {
  806. SSL_set_msg_callback(con, msg_cb);
  807. SSL_set_msg_callback_arg(con, bio_c_out);
  808. }
  809. #ifndef OPENSSL_NO_TLSEXT
  810. if (c_tlsextdebug) {
  811. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  812. SSL_set_tlsext_debug_arg(con, bio_c_out);
  813. }
  814. if (c_status_req) {
  815. SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp);
  816. SSL_CTX_set_tlsext_status_cb(ctx, ocsp_resp_cb);
  817. SSL_CTX_set_tlsext_status_arg(ctx, bio_c_out);
  818. # if 0
  819. {
  820. STACK_OF(OCSP_RESPID) *ids = sk_OCSP_RESPID_new_null();
  821. OCSP_RESPID *id = OCSP_RESPID_new();
  822. id->value.byKey = ASN1_OCTET_STRING_new();
  823. id->type = V_OCSP_RESPID_KEY;
  824. ASN1_STRING_set(id->value.byKey, "Hello World", -1);
  825. sk_OCSP_RESPID_push(ids, id);
  826. SSL_set_tlsext_status_ids(con, ids);
  827. }
  828. # endif
  829. }
  830. #endif
  831. #ifndef OPENSSL_NO_JPAKE
  832. if (jpake_secret)
  833. jpake_client_auth(bio_c_out, sbio, jpake_secret);
  834. #endif
  835. SSL_set_bio(con, sbio, sbio);
  836. SSL_set_connect_state(con);
  837. /* ok, lets connect */
  838. width = SSL_get_fd(con) + 1;
  839. read_tty = 1;
  840. write_tty = 0;
  841. tty_on = 0;
  842. read_ssl = 1;
  843. write_ssl = 1;
  844. cbuf_len = 0;
  845. cbuf_off = 0;
  846. sbuf_len = 0;
  847. sbuf_off = 0;
  848. /* This is an ugly hack that does a lot of assumptions */
  849. /*
  850. * We do have to handle multi-line responses which may come in a single
  851. * packet or not. We therefore have to use BIO_gets() which does need a
  852. * buffering BIO. So during the initial chitchat we do push a buffering
  853. * BIO into the chain that is removed again later on to not disturb the
  854. * rest of the s_client operation.
  855. */
  856. if (starttls_proto == PROTO_SMTP) {
  857. int foundit = 0;
  858. BIO *fbio = BIO_new(BIO_f_buffer());
  859. BIO_push(fbio, sbio);
  860. /* wait for multi-line response to end from SMTP */
  861. do {
  862. mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
  863. }
  864. while (mbuf_len > 3 && mbuf[3] == '-');
  865. /* STARTTLS command requires EHLO... */
  866. BIO_printf(fbio, "EHLO openssl.client.net\r\n");
  867. (void)BIO_flush(fbio);
  868. /* wait for multi-line response to end EHLO SMTP response */
  869. do {
  870. mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
  871. if (strstr(mbuf, "STARTTLS"))
  872. foundit = 1;
  873. }
  874. while (mbuf_len > 3 && mbuf[3] == '-');
  875. (void)BIO_flush(fbio);
  876. BIO_pop(fbio);
  877. BIO_free(fbio);
  878. if (!foundit)
  879. BIO_printf(bio_err,
  880. "didn't found starttls in server response,"
  881. " try anyway...\n");
  882. BIO_printf(sbio, "STARTTLS\r\n");
  883. BIO_read(sbio, sbuf, BUFSIZZ);
  884. } else if (starttls_proto == PROTO_POP3) {
  885. BIO_read(sbio, mbuf, BUFSIZZ);
  886. BIO_printf(sbio, "STLS\r\n");
  887. BIO_read(sbio, sbuf, BUFSIZZ);
  888. } else if (starttls_proto == PROTO_IMAP) {
  889. int foundit = 0;
  890. BIO *fbio = BIO_new(BIO_f_buffer());
  891. BIO_push(fbio, sbio);
  892. BIO_gets(fbio, mbuf, BUFSIZZ);
  893. /* STARTTLS command requires CAPABILITY... */
  894. BIO_printf(fbio, ". CAPABILITY\r\n");
  895. (void)BIO_flush(fbio);
  896. /* wait for multi-line CAPABILITY response */
  897. do {
  898. mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
  899. if (strstr(mbuf, "STARTTLS"))
  900. foundit = 1;
  901. }
  902. while (mbuf_len > 3 && mbuf[0] != '.');
  903. (void)BIO_flush(fbio);
  904. BIO_pop(fbio);
  905. BIO_free(fbio);
  906. if (!foundit)
  907. BIO_printf(bio_err,
  908. "didn't found STARTTLS in server response,"
  909. " try anyway...\n");
  910. BIO_printf(sbio, ". STARTTLS\r\n");
  911. BIO_read(sbio, sbuf, BUFSIZZ);
  912. } else if (starttls_proto == PROTO_FTP) {
  913. BIO *fbio = BIO_new(BIO_f_buffer());
  914. BIO_push(fbio, sbio);
  915. /* wait for multi-line response to end from FTP */
  916. do {
  917. mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
  918. }
  919. while (mbuf_len > 3 && mbuf[3] == '-');
  920. (void)BIO_flush(fbio);
  921. BIO_pop(fbio);
  922. BIO_free(fbio);
  923. BIO_printf(sbio, "AUTH TLS\r\n");
  924. BIO_read(sbio, sbuf, BUFSIZZ);
  925. }
  926. if (starttls_proto == PROTO_XMPP) {
  927. int seen = 0;
  928. BIO_printf(sbio, "<stream:stream "
  929. "xmlns:stream='http://etherx.jabber.org/streams' "
  930. "xmlns='jabber:client' to='%s' version='1.0'>", host);
  931. seen = BIO_read(sbio, mbuf, BUFSIZZ);
  932. mbuf[seen] = 0;
  933. while (!strstr
  934. (mbuf, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'")) {
  935. if (strstr(mbuf, "/stream:features>"))
  936. goto shut;
  937. seen = BIO_read(sbio, mbuf, BUFSIZZ);
  938. mbuf[seen] = 0;
  939. }
  940. BIO_printf(sbio,
  941. "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
  942. seen = BIO_read(sbio, sbuf, BUFSIZZ);
  943. sbuf[seen] = 0;
  944. if (!strstr(sbuf, "<proceed"))
  945. goto shut;
  946. mbuf[0] = 0;
  947. }
  948. for (;;) {
  949. FD_ZERO(&readfds);
  950. FD_ZERO(&writefds);
  951. if ((SSL_version(con) == DTLS1_VERSION) &&
  952. DTLSv1_get_timeout(con, &timeout))
  953. timeoutp = &timeout;
  954. else
  955. timeoutp = NULL;
  956. if (SSL_in_init(con) && !SSL_total_renegotiations(con)) {
  957. in_init = 1;
  958. tty_on = 0;
  959. } else {
  960. tty_on = 1;
  961. if (in_init) {
  962. in_init = 0;
  963. if (sess_out) {
  964. BIO *stmp = BIO_new_file(sess_out, "w");
  965. if (stmp) {
  966. PEM_write_bio_SSL_SESSION(stmp, SSL_get_session(con));
  967. BIO_free(stmp);
  968. } else
  969. BIO_printf(bio_err, "Error writing session file %s\n",
  970. sess_out);
  971. }
  972. print_stuff(bio_c_out, con, full_log);
  973. if (full_log > 0)
  974. full_log--;
  975. if (starttls_proto) {
  976. BIO_printf(bio_err, "%s", mbuf);
  977. /* We don't need to know any more */
  978. starttls_proto = PROTO_OFF;
  979. }
  980. if (reconnect) {
  981. reconnect--;
  982. BIO_printf(bio_c_out,
  983. "drop connection and then reconnect\n");
  984. SSL_shutdown(con);
  985. SSL_set_connect_state(con);
  986. SHUTDOWN(SSL_get_fd(con));
  987. goto re_start;
  988. }
  989. }
  990. }
  991. ssl_pending = read_ssl && SSL_pending(con);
  992. if (!ssl_pending) {
  993. #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE)
  994. if (tty_on) {
  995. if (read_tty)
  996. FD_SET(fileno(stdin), &readfds);
  997. if (write_tty)
  998. FD_SET(fileno(stdout), &writefds);
  999. }
  1000. if (read_ssl)
  1001. FD_SET(SSL_get_fd(con), &readfds);
  1002. if (write_ssl)
  1003. FD_SET(SSL_get_fd(con), &writefds);
  1004. #else
  1005. if (!tty_on || !write_tty) {
  1006. if (read_ssl)
  1007. FD_SET(SSL_get_fd(con), &readfds);
  1008. if (write_ssl)
  1009. FD_SET(SSL_get_fd(con), &writefds);
  1010. }
  1011. #endif
  1012. /*- printf("mode tty(%d %d%d) ssl(%d%d)\n",
  1013. tty_on,read_tty,write_tty,read_ssl,write_ssl);*/
  1014. /*
  1015. * Note: under VMS with SOCKETSHR the second parameter is
  1016. * currently of type (int *) whereas under other systems it is
  1017. * (void *) if you don't have a cast it will choke the compiler:
  1018. * if you do have a cast then you can either go for (int *) or
  1019. * (void *).
  1020. */
  1021. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
  1022. /*
  1023. * Under Windows/DOS we make the assumption that we can always
  1024. * write to the tty: therefore if we need to write to the tty we
  1025. * just fall through. Otherwise we timeout the select every
  1026. * second and see if there are any keypresses. Note: this is a
  1027. * hack, in a proper Windows application we wouldn't do this.
  1028. */
  1029. i = 0;
  1030. if (!write_tty) {
  1031. if (read_tty) {
  1032. tv.tv_sec = 1;
  1033. tv.tv_usec = 0;
  1034. i = select(width, (void *)&readfds, (void *)&writefds,
  1035. NULL, &tv);
  1036. # if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
  1037. if (!i && (!_kbhit() || !read_tty))
  1038. continue;
  1039. # else
  1040. if (!i && (!((_kbhit())
  1041. || (WAIT_OBJECT_0 ==
  1042. WaitForSingleObject(GetStdHandle
  1043. (STD_INPUT_HANDLE),
  1044. 0)))
  1045. || !read_tty))
  1046. continue;
  1047. # endif
  1048. } else
  1049. i = select(width, (void *)&readfds, (void *)&writefds,
  1050. NULL, timeoutp);
  1051. }
  1052. #elif defined(OPENSSL_SYS_NETWARE)
  1053. if (!write_tty) {
  1054. if (read_tty) {
  1055. tv.tv_sec = 1;
  1056. tv.tv_usec = 0;
  1057. i = select(width, (void *)&readfds, (void *)&writefds,
  1058. NULL, &tv);
  1059. } else
  1060. i = select(width, (void *)&readfds, (void *)&writefds,
  1061. NULL, timeoutp);
  1062. }
  1063. #else
  1064. i = select(width, (void *)&readfds, (void *)&writefds,
  1065. NULL, timeoutp);
  1066. #endif
  1067. if (i < 0) {
  1068. BIO_printf(bio_err, "bad select %d\n",
  1069. get_last_socket_error());
  1070. goto shut;
  1071. /* goto end; */
  1072. }
  1073. }
  1074. if ((SSL_version(con) == DTLS1_VERSION)
  1075. && DTLSv1_handle_timeout(con) > 0) {
  1076. BIO_printf(bio_err, "TIMEOUT occured\n");
  1077. }
  1078. if (!ssl_pending && FD_ISSET(SSL_get_fd(con), &writefds)) {
  1079. k = SSL_write(con, &(cbuf[cbuf_off]), (unsigned int)cbuf_len);
  1080. switch (SSL_get_error(con, k)) {
  1081. case SSL_ERROR_NONE:
  1082. cbuf_off += k;
  1083. cbuf_len -= k;
  1084. if (k <= 0)
  1085. goto end;
  1086. /* we have done a write(con,NULL,0); */
  1087. if (cbuf_len <= 0) {
  1088. read_tty = 1;
  1089. write_ssl = 0;
  1090. } else { /* if (cbuf_len > 0) */
  1091. read_tty = 0;
  1092. write_ssl = 1;
  1093. }
  1094. break;
  1095. case SSL_ERROR_WANT_WRITE:
  1096. BIO_printf(bio_c_out, "write W BLOCK\n");
  1097. write_ssl = 1;
  1098. read_tty = 0;
  1099. break;
  1100. case SSL_ERROR_WANT_READ:
  1101. BIO_printf(bio_c_out, "write R BLOCK\n");
  1102. write_tty = 0;
  1103. read_ssl = 1;
  1104. write_ssl = 0;
  1105. break;
  1106. case SSL_ERROR_WANT_X509_LOOKUP:
  1107. BIO_printf(bio_c_out, "write X BLOCK\n");
  1108. break;
  1109. case SSL_ERROR_ZERO_RETURN:
  1110. if (cbuf_len != 0) {
  1111. BIO_printf(bio_c_out, "shutdown\n");
  1112. goto shut;
  1113. } else {
  1114. read_tty = 1;
  1115. write_ssl = 0;
  1116. break;
  1117. }
  1118. case SSL_ERROR_SYSCALL:
  1119. if ((k != 0) || (cbuf_len != 0)) {
  1120. BIO_printf(bio_err, "write:errno=%d\n",
  1121. get_last_socket_error());
  1122. goto shut;
  1123. } else {
  1124. read_tty = 1;
  1125. write_ssl = 0;
  1126. }
  1127. break;
  1128. case SSL_ERROR_SSL:
  1129. ERR_print_errors(bio_err);
  1130. goto shut;
  1131. }
  1132. }
  1133. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)
  1134. /* Assume Windows/DOS can always write */
  1135. else if (!ssl_pending && write_tty)
  1136. #else
  1137. else if (!ssl_pending && FD_ISSET(fileno(stdout), &writefds))
  1138. #endif
  1139. {
  1140. #ifdef CHARSET_EBCDIC
  1141. ascii2ebcdic(&(sbuf[sbuf_off]), &(sbuf[sbuf_off]), sbuf_len);
  1142. #endif
  1143. i = write(fileno(stdout), &(sbuf[sbuf_off]), sbuf_len);
  1144. if (i <= 0) {
  1145. BIO_printf(bio_c_out, "DONE\n");
  1146. goto shut;
  1147. /* goto end; */
  1148. }
  1149. sbuf_len -= i;;
  1150. sbuf_off += i;
  1151. if (sbuf_len <= 0) {
  1152. read_ssl = 1;
  1153. write_tty = 0;
  1154. }
  1155. } else if (ssl_pending || FD_ISSET(SSL_get_fd(con), &readfds)) {
  1156. #ifdef RENEG
  1157. {
  1158. static int iiii;
  1159. if (++iiii == 52) {
  1160. SSL_renegotiate(con);
  1161. iiii = 0;
  1162. }
  1163. }
  1164. #endif
  1165. #if 1
  1166. k = SSL_read(con, sbuf, 1024 /* BUFSIZZ */ );
  1167. #else
  1168. /* Demo for pending and peek :-) */
  1169. k = SSL_read(con, sbuf, 16);
  1170. {
  1171. char zbuf[10240];
  1172. printf("read=%d pending=%d peek=%d\n", k, SSL_pending(con),
  1173. SSL_peek(con, zbuf, 10240));
  1174. }
  1175. #endif
  1176. switch (SSL_get_error(con, k)) {
  1177. case SSL_ERROR_NONE:
  1178. if (k <= 0)
  1179. goto end;
  1180. sbuf_off = 0;
  1181. sbuf_len = k;
  1182. read_ssl = 0;
  1183. write_tty = 1;
  1184. break;
  1185. case SSL_ERROR_WANT_WRITE:
  1186. BIO_printf(bio_c_out, "read W BLOCK\n");
  1187. write_ssl = 1;
  1188. read_tty = 0;
  1189. break;
  1190. case SSL_ERROR_WANT_READ:
  1191. BIO_printf(bio_c_out, "read R BLOCK\n");
  1192. write_tty = 0;
  1193. read_ssl = 1;
  1194. if ((read_tty == 0) && (write_ssl == 0))
  1195. write_ssl = 1;
  1196. break;
  1197. case SSL_ERROR_WANT_X509_LOOKUP:
  1198. BIO_printf(bio_c_out, "read X BLOCK\n");
  1199. break;
  1200. case SSL_ERROR_SYSCALL:
  1201. BIO_printf(bio_err, "read:errno=%d\n",
  1202. get_last_socket_error());
  1203. goto shut;
  1204. case SSL_ERROR_ZERO_RETURN:
  1205. BIO_printf(bio_c_out, "closed\n");
  1206. goto shut;
  1207. case SSL_ERROR_SSL:
  1208. ERR_print_errors(bio_err);
  1209. goto shut;
  1210. /* break; */
  1211. }
  1212. }
  1213. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
  1214. # if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
  1215. else if (_kbhit())
  1216. # else
  1217. else if ((_kbhit())
  1218. || (WAIT_OBJECT_0 ==
  1219. WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0)))
  1220. # endif
  1221. #elif defined (OPENSSL_SYS_NETWARE)
  1222. else if (_kbhit())
  1223. #else
  1224. else if (FD_ISSET(fileno(stdin), &readfds))
  1225. #endif
  1226. {
  1227. if (crlf) {
  1228. int j, lf_num;
  1229. i = read(fileno(stdin), cbuf, BUFSIZZ / 2);
  1230. lf_num = 0;
  1231. /* both loops are skipped when i <= 0 */
  1232. for (j = 0; j < i; j++)
  1233. if (cbuf[j] == '\n')
  1234. lf_num++;
  1235. for (j = i - 1; j >= 0; j--) {
  1236. cbuf[j + lf_num] = cbuf[j];
  1237. if (cbuf[j] == '\n') {
  1238. lf_num--;
  1239. i++;
  1240. cbuf[j + lf_num] = '\r';
  1241. }
  1242. }
  1243. assert(lf_num == 0);
  1244. } else
  1245. i = read(fileno(stdin), cbuf, BUFSIZZ);
  1246. if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q'))) {
  1247. BIO_printf(bio_err, "DONE\n");
  1248. goto shut;
  1249. }
  1250. if ((!c_ign_eof) && (cbuf[0] == 'R')) {
  1251. BIO_printf(bio_err, "RENEGOTIATING\n");
  1252. SSL_renegotiate(con);
  1253. cbuf_len = 0;
  1254. } else {
  1255. cbuf_len = i;
  1256. cbuf_off = 0;
  1257. #ifdef CHARSET_EBCDIC
  1258. ebcdic2ascii(cbuf, cbuf, i);
  1259. #endif
  1260. }
  1261. write_ssl = 1;
  1262. read_tty = 0;
  1263. }
  1264. }
  1265. shut:
  1266. SSL_shutdown(con);
  1267. SHUTDOWN(SSL_get_fd(con));
  1268. ret = 0;
  1269. end:
  1270. if (prexit)
  1271. print_stuff(bio_c_out, con, 1);
  1272. if (con != NULL)
  1273. SSL_free(con);
  1274. if (con2 != NULL)
  1275. SSL_free(con2);
  1276. if (ctx != NULL)
  1277. SSL_CTX_free(ctx);
  1278. if (cert)
  1279. X509_free(cert);
  1280. if (key)
  1281. EVP_PKEY_free(key);
  1282. if (pass)
  1283. OPENSSL_free(pass);
  1284. if (cbuf != NULL) {
  1285. OPENSSL_cleanse(cbuf, BUFSIZZ);
  1286. OPENSSL_free(cbuf);
  1287. }
  1288. if (sbuf != NULL) {
  1289. OPENSSL_cleanse(sbuf, BUFSIZZ);
  1290. OPENSSL_free(sbuf);
  1291. }
  1292. if (mbuf != NULL) {
  1293. OPENSSL_cleanse(mbuf, BUFSIZZ);
  1294. OPENSSL_free(mbuf);
  1295. }
  1296. if (bio_c_out != NULL) {
  1297. BIO_free(bio_c_out);
  1298. bio_c_out = NULL;
  1299. }
  1300. apps_shutdown();
  1301. OPENSSL_EXIT(ret);
  1302. }
  1303. static void print_stuff(BIO *bio, SSL *s, int full)
  1304. {
  1305. X509 *peer = NULL;
  1306. char *p;
  1307. static const char *space = " ";
  1308. char buf[BUFSIZ];
  1309. STACK_OF(X509) *sk;
  1310. STACK_OF(X509_NAME) *sk2;
  1311. SSL_CIPHER *c;
  1312. X509_NAME *xn;
  1313. int j, i;
  1314. #ifndef OPENSSL_NO_COMP
  1315. const COMP_METHOD *comp, *expansion;
  1316. #endif
  1317. if (full) {
  1318. int got_a_chain = 0;
  1319. sk = SSL_get_peer_cert_chain(s);
  1320. if (sk != NULL) {
  1321. got_a_chain = 1; /* we don't have it for SSL2 (yet) */
  1322. BIO_printf(bio, "---\nCertificate chain\n");
  1323. for (i = 0; i < sk_X509_num(sk); i++) {
  1324. X509_NAME_oneline(X509_get_subject_name(sk_X509_value(sk, i)),
  1325. buf, sizeof buf);
  1326. BIO_printf(bio, "%2d s:%s\n", i, buf);
  1327. X509_NAME_oneline(X509_get_issuer_name(sk_X509_value(sk, i)),
  1328. buf, sizeof buf);
  1329. BIO_printf(bio, " i:%s\n", buf);
  1330. if (c_showcerts)
  1331. PEM_write_bio_X509(bio, sk_X509_value(sk, i));
  1332. }
  1333. }
  1334. BIO_printf(bio, "---\n");
  1335. peer = SSL_get_peer_certificate(s);
  1336. if (peer != NULL) {
  1337. BIO_printf(bio, "Server certificate\n");
  1338. /* Redundant if we showed the whole chain */
  1339. if (!(c_showcerts && got_a_chain))
  1340. PEM_write_bio_X509(bio, peer);
  1341. X509_NAME_oneline(X509_get_subject_name(peer), buf, sizeof buf);
  1342. BIO_printf(bio, "subject=%s\n", buf);
  1343. X509_NAME_oneline(X509_get_issuer_name(peer), buf, sizeof buf);
  1344. BIO_printf(bio, "issuer=%s\n", buf);
  1345. } else
  1346. BIO_printf(bio, "no peer certificate available\n");
  1347. sk2 = SSL_get_client_CA_list(s);
  1348. if ((sk2 != NULL) && (sk_X509_NAME_num(sk2) > 0)) {
  1349. BIO_printf(bio, "---\nAcceptable client certificate CA names\n");
  1350. for (i = 0; i < sk_X509_NAME_num(sk2); i++) {
  1351. xn = sk_X509_NAME_value(sk2, i);
  1352. X509_NAME_oneline(xn, buf, sizeof(buf));
  1353. BIO_write(bio, buf, strlen(buf));
  1354. BIO_write(bio, "\n", 1);
  1355. }
  1356. } else {
  1357. BIO_printf(bio, "---\nNo client certificate CA names sent\n");
  1358. }
  1359. p = SSL_get_shared_ciphers(s, buf, sizeof buf);
  1360. if (p != NULL) {
  1361. /*
  1362. * This works only for SSL 2. In later protocol versions, the
  1363. * client does not know what other ciphers (in addition to the
  1364. * one to be used in the current connection) the server supports.
  1365. */
  1366. BIO_printf(bio,
  1367. "---\nCiphers common between both SSL endpoints:\n");
  1368. j = i = 0;
  1369. while (*p) {
  1370. if (*p == ':') {
  1371. BIO_write(bio, space, 15 - j % 25);
  1372. i++;
  1373. j = 0;
  1374. BIO_write(bio, ((i % 3) ? " " : "\n"), 1);
  1375. } else {
  1376. BIO_write(bio, p, 1);
  1377. j++;
  1378. }
  1379. p++;
  1380. }
  1381. BIO_write(bio, "\n", 1);
  1382. }
  1383. BIO_printf(bio,
  1384. "---\nSSL handshake has read %ld bytes and written %ld bytes\n",
  1385. BIO_number_read(SSL_get_rbio(s)),
  1386. BIO_number_written(SSL_get_wbio(s)));
  1387. }
  1388. BIO_printf(bio, ((s->hit) ? "---\nReused, " : "---\nNew, "));
  1389. c = SSL_get_current_cipher(s);
  1390. BIO_printf(bio, "%s, Cipher is %s\n",
  1391. SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
  1392. if (peer != NULL) {
  1393. EVP_PKEY *pktmp;
  1394. pktmp = X509_get_pubkey(peer);
  1395. BIO_printf(bio, "Server public key is %d bit\n",
  1396. EVP_PKEY_bits(pktmp));
  1397. EVP_PKEY_free(pktmp);
  1398. }
  1399. BIO_printf(bio, "Secure Renegotiation IS%s supported\n",
  1400. SSL_get_secure_renegotiation_support(s) ? "" : " NOT");
  1401. #ifndef OPENSSL_NO_COMP
  1402. comp = SSL_get_current_compression(s);
  1403. expansion = SSL_get_current_expansion(s);
  1404. BIO_printf(bio, "Compression: %s\n",
  1405. comp ? SSL_COMP_get_name(comp) : "NONE");
  1406. BIO_printf(bio, "Expansion: %s\n",
  1407. expansion ? SSL_COMP_get_name(expansion) : "NONE");
  1408. #endif
  1409. SSL_SESSION_print(bio, SSL_get_session(s));
  1410. BIO_printf(bio, "---\n");
  1411. if (peer != NULL)
  1412. X509_free(peer);
  1413. /* flush, or debugging output gets mixed with http response */
  1414. (void)BIO_flush(bio);
  1415. }
  1416. #ifndef OPENSSL_NO_TLSEXT
  1417. static int ocsp_resp_cb(SSL *s, void *arg)
  1418. {
  1419. const unsigned char *p;
  1420. int len;
  1421. OCSP_RESPONSE *rsp;
  1422. len = SSL_get_tlsext_status_ocsp_resp(s, &p);
  1423. BIO_puts(arg, "OCSP response: ");
  1424. if (!p) {
  1425. BIO_puts(arg, "no response sent\n");
  1426. return 1;
  1427. }
  1428. rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
  1429. if (!rsp) {
  1430. BIO_puts(arg, "response parse error\n");
  1431. BIO_dump_indent(arg, (char *)p, len, 4);
  1432. return 0;
  1433. }
  1434. BIO_puts(arg, "\n======================================\n");
  1435. OCSP_RESPONSE_print(arg, rsp, 0);
  1436. BIO_puts(arg, "======================================\n");
  1437. OCSP_RESPONSE_free(rsp);
  1438. return 1;
  1439. }
  1440. #endif /* ndef OPENSSL_NO_TLSEXT */