s_client.c 58 KB

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