s_client.c 71 KB

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