s_client.c 55 KB

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