s_client.c 78 KB

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