s_client.c 78 KB

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