s_client.c 48 KB

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