s_client.c 47 KB

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