s_client.c 55 KB

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