s_client.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364
  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-2018 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 sent by the server\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. inlen &= 0xffff; /* for formal memcpy correctness */
  584. ext_buf[0] = (unsigned char)(ext_type >> 8);
  585. ext_buf[1] = (unsigned char)(ext_type);
  586. ext_buf[2] = (unsigned char)(inlen >> 8);
  587. ext_buf[3] = (unsigned char)(inlen);
  588. memcpy(ext_buf + 4, in, inlen);
  589. BIO_snprintf(pem_name, sizeof(pem_name), "SERVERINFO FOR EXTENSION %d",
  590. ext_type);
  591. PEM_write_bio(bio_c_out, pem_name, "", ext_buf, 4 + inlen);
  592. return 1;
  593. }
  594. #endif
  595. enum {
  596. PROTO_OFF = 0,
  597. PROTO_SMTP,
  598. PROTO_POP3,
  599. PROTO_IMAP,
  600. PROTO_FTP,
  601. PROTO_XMPP
  602. };
  603. int MAIN(int, char **);
  604. int MAIN(int argc, char **argv)
  605. {
  606. int build_chain = 0;
  607. SSL *con = NULL;
  608. #ifndef OPENSSL_NO_KRB5
  609. KSSL_CTX *kctx;
  610. #endif
  611. int s, k, width, state = 0;
  612. char *cbuf = NULL, *sbuf = NULL, *mbuf = NULL;
  613. int cbuf_len, cbuf_off;
  614. int sbuf_len, sbuf_off;
  615. fd_set readfds, writefds;
  616. short port = PORT;
  617. int full_log = 1;
  618. char *host = SSL_HOST_NAME;
  619. char *cert_file = NULL, *key_file = NULL, *chain_file = NULL;
  620. int cert_format = FORMAT_PEM, key_format = FORMAT_PEM;
  621. char *passarg = NULL, *pass = NULL;
  622. X509 *cert = NULL;
  623. EVP_PKEY *key = NULL;
  624. STACK_OF(X509) *chain = NULL;
  625. char *CApath = NULL, *CAfile = NULL;
  626. char *chCApath = NULL, *chCAfile = NULL;
  627. char *vfyCApath = NULL, *vfyCAfile = NULL;
  628. int reconnect = 0, badop = 0, verify = SSL_VERIFY_NONE;
  629. int crlf = 0;
  630. int write_tty, read_tty, write_ssl, read_ssl, tty_on, ssl_pending;
  631. SSL_CTX *ctx = NULL;
  632. int ret = 1, in_init = 1, i, nbio_test = 0;
  633. int starttls_proto = PROTO_OFF;
  634. int prexit = 0;
  635. X509_VERIFY_PARAM *vpm = NULL;
  636. int badarg = 0;
  637. const SSL_METHOD *meth = NULL;
  638. int socket_type = SOCK_STREAM;
  639. BIO *sbio;
  640. char *inrand = NULL;
  641. int mbuf_len = 0;
  642. struct timeval timeout, *timeoutp;
  643. char *engine_id = NULL;
  644. ENGINE *e = NULL;
  645. #ifndef OPENSSL_NO_ENGINE
  646. char *ssl_client_engine_id = NULL;
  647. ENGINE *ssl_client_engine = NULL;
  648. #endif
  649. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)
  650. struct timeval tv;
  651. # if defined(OPENSSL_SYS_BEOS_R5)
  652. int stdin_set = 0;
  653. # endif
  654. #endif
  655. #ifndef OPENSSL_NO_TLSEXT
  656. char *servername = NULL;
  657. tlsextctx tlsextcbp = { NULL, 0 };
  658. # ifndef OPENSSL_NO_NEXTPROTONEG
  659. const char *next_proto_neg_in = NULL;
  660. # endif
  661. const char *alpn_in = NULL;
  662. # define MAX_SI_TYPES 100
  663. unsigned short serverinfo_types[MAX_SI_TYPES];
  664. int serverinfo_types_count = 0;
  665. #endif
  666. char *sess_in = NULL;
  667. char *sess_out = NULL;
  668. struct sockaddr peer;
  669. int peerlen = sizeof(peer);
  670. int fallback_scsv = 0;
  671. int enable_timeouts = 0;
  672. long socket_mtu = 0;
  673. #ifndef OPENSSL_NO_JPAKE
  674. static char *jpake_secret = NULL;
  675. # define no_jpake !jpake_secret
  676. #else
  677. # define no_jpake 1
  678. #endif
  679. #ifndef OPENSSL_NO_SRP
  680. char *srppass = NULL;
  681. int srp_lateuser = 0;
  682. SRP_ARG srp_arg = { NULL, NULL, 0, 0, 0, 1024 };
  683. #endif
  684. SSL_EXCERT *exc = NULL;
  685. SSL_CONF_CTX *cctx = NULL;
  686. STACK_OF(OPENSSL_STRING) *ssl_args = NULL;
  687. char *crl_file = NULL;
  688. int crl_format = FORMAT_PEM;
  689. int crl_download = 0;
  690. STACK_OF(X509_CRL) *crls = NULL;
  691. int prot_opt = 0, no_prot_opt = 0;
  692. meth = SSLv23_client_method();
  693. apps_startup();
  694. c_Pause = 0;
  695. c_quiet = 0;
  696. c_ign_eof = 0;
  697. c_debug = 0;
  698. c_msg = 0;
  699. c_showcerts = 0;
  700. if (bio_err == NULL)
  701. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  702. if (!load_config(bio_err, NULL))
  703. goto end;
  704. cctx = SSL_CONF_CTX_new();
  705. if (!cctx)
  706. goto end;
  707. SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT);
  708. SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CMDLINE);
  709. if (((cbuf = OPENSSL_malloc(BUFSIZZ)) == NULL) ||
  710. ((sbuf = OPENSSL_malloc(BUFSIZZ)) == NULL) ||
  711. ((mbuf = OPENSSL_malloc(BUFSIZZ)) == NULL)) {
  712. BIO_printf(bio_err, "out of memory\n");
  713. goto end;
  714. }
  715. verify_depth = 0;
  716. verify_error = X509_V_OK;
  717. #ifdef FIONBIO
  718. c_nbio = 0;
  719. #endif
  720. argc--;
  721. argv++;
  722. while (argc >= 1) {
  723. if (strcmp(*argv, "-host") == 0) {
  724. if (--argc < 1)
  725. goto bad;
  726. host = *(++argv);
  727. } else if (strcmp(*argv, "-port") == 0) {
  728. if (--argc < 1)
  729. goto bad;
  730. port = atoi(*(++argv));
  731. if (port == 0)
  732. goto bad;
  733. } else if (strcmp(*argv, "-connect") == 0) {
  734. if (--argc < 1)
  735. goto bad;
  736. if (!extract_host_port(*(++argv), &host, NULL, &port))
  737. goto bad;
  738. } else if (strcmp(*argv, "-verify") == 0) {
  739. verify = SSL_VERIFY_PEER;
  740. if (--argc < 1)
  741. goto bad;
  742. verify_depth = atoi(*(++argv));
  743. if (!c_quiet)
  744. BIO_printf(bio_err, "verify depth is %d\n", verify_depth);
  745. } else if (strcmp(*argv, "-cert") == 0) {
  746. if (--argc < 1)
  747. goto bad;
  748. cert_file = *(++argv);
  749. } else if (strcmp(*argv, "-CRL") == 0) {
  750. if (--argc < 1)
  751. goto bad;
  752. crl_file = *(++argv);
  753. } else if (strcmp(*argv, "-crl_download") == 0)
  754. crl_download = 1;
  755. else if (strcmp(*argv, "-sess_out") == 0) {
  756. if (--argc < 1)
  757. goto bad;
  758. sess_out = *(++argv);
  759. } else if (strcmp(*argv, "-sess_in") == 0) {
  760. if (--argc < 1)
  761. goto bad;
  762. sess_in = *(++argv);
  763. } else if (strcmp(*argv, "-certform") == 0) {
  764. if (--argc < 1)
  765. goto bad;
  766. cert_format = str2fmt(*(++argv));
  767. } else if (strcmp(*argv, "-CRLform") == 0) {
  768. if (--argc < 1)
  769. goto bad;
  770. crl_format = str2fmt(*(++argv));
  771. } else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm)) {
  772. if (badarg)
  773. goto bad;
  774. continue;
  775. } else if (strcmp(*argv, "-verify_return_error") == 0)
  776. verify_return_error = 1;
  777. else if (strcmp(*argv, "-verify_quiet") == 0)
  778. verify_quiet = 1;
  779. else if (strcmp(*argv, "-brief") == 0) {
  780. c_brief = 1;
  781. verify_quiet = 1;
  782. c_quiet = 1;
  783. } else if (args_excert(&argv, &argc, &badarg, bio_err, &exc)) {
  784. if (badarg)
  785. goto bad;
  786. continue;
  787. } else if (args_ssl(&argv, &argc, cctx, &badarg, bio_err, &ssl_args,
  788. &no_prot_opt)) {
  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. prot_opt++;
  882. }
  883. #endif
  884. #ifndef OPENSSL_NO_SSL3_METHOD
  885. else if (strcmp(*argv, "-ssl3") == 0) {
  886. meth = SSLv3_client_method();
  887. prot_opt++;
  888. }
  889. #endif
  890. #ifndef OPENSSL_NO_TLS1
  891. else if (strcmp(*argv, "-tls1_2") == 0) {
  892. meth = TLSv1_2_client_method();
  893. prot_opt++;
  894. } else if (strcmp(*argv, "-tls1_1") == 0) {
  895. meth = TLSv1_1_client_method();
  896. prot_opt++;
  897. } else if (strcmp(*argv, "-tls1") == 0) {
  898. meth = TLSv1_client_method();
  899. prot_opt++;
  900. }
  901. #endif
  902. #ifndef OPENSSL_NO_DTLS1
  903. else if (strcmp(*argv, "-dtls") == 0) {
  904. meth = DTLS_client_method();
  905. socket_type = SOCK_DGRAM;
  906. prot_opt++;
  907. } else if (strcmp(*argv, "-dtls1") == 0) {
  908. meth = DTLSv1_client_method();
  909. socket_type = SOCK_DGRAM;
  910. prot_opt++;
  911. } else if (strcmp(*argv, "-dtls1_2") == 0) {
  912. meth = DTLSv1_2_client_method();
  913. socket_type = SOCK_DGRAM;
  914. prot_opt++;
  915. } else if (strcmp(*argv, "-timeout") == 0)
  916. enable_timeouts = 1;
  917. else if (strcmp(*argv, "-mtu") == 0) {
  918. if (--argc < 1)
  919. goto bad;
  920. socket_mtu = atol(*(++argv));
  921. }
  922. #endif
  923. else if (strcmp(*argv, "-fallback_scsv") == 0) {
  924. fallback_scsv = 1;
  925. } else if (strcmp(*argv, "-keyform") == 0) {
  926. if (--argc < 1)
  927. goto bad;
  928. key_format = str2fmt(*(++argv));
  929. } else if (strcmp(*argv, "-pass") == 0) {
  930. if (--argc < 1)
  931. goto bad;
  932. passarg = *(++argv);
  933. } else if (strcmp(*argv, "-cert_chain") == 0) {
  934. if (--argc < 1)
  935. goto bad;
  936. chain_file = *(++argv);
  937. } else if (strcmp(*argv, "-key") == 0) {
  938. if (--argc < 1)
  939. goto bad;
  940. key_file = *(++argv);
  941. } else if (strcmp(*argv, "-reconnect") == 0) {
  942. reconnect = 5;
  943. } else if (strcmp(*argv, "-CApath") == 0) {
  944. if (--argc < 1)
  945. goto bad;
  946. CApath = *(++argv);
  947. } else if (strcmp(*argv, "-chainCApath") == 0) {
  948. if (--argc < 1)
  949. goto bad;
  950. chCApath = *(++argv);
  951. } else if (strcmp(*argv, "-verifyCApath") == 0) {
  952. if (--argc < 1)
  953. goto bad;
  954. vfyCApath = *(++argv);
  955. } else if (strcmp(*argv, "-build_chain") == 0)
  956. build_chain = 1;
  957. else if (strcmp(*argv, "-CAfile") == 0) {
  958. if (--argc < 1)
  959. goto bad;
  960. CAfile = *(++argv);
  961. } else if (strcmp(*argv, "-chainCAfile") == 0) {
  962. if (--argc < 1)
  963. goto bad;
  964. chCAfile = *(++argv);
  965. } else if (strcmp(*argv, "-verifyCAfile") == 0) {
  966. if (--argc < 1)
  967. goto bad;
  968. vfyCAfile = *(++argv);
  969. }
  970. #ifndef OPENSSL_NO_TLSEXT
  971. # ifndef OPENSSL_NO_NEXTPROTONEG
  972. else if (strcmp(*argv, "-nextprotoneg") == 0) {
  973. if (--argc < 1)
  974. goto bad;
  975. next_proto_neg_in = *(++argv);
  976. }
  977. # endif
  978. else if (strcmp(*argv, "-alpn") == 0) {
  979. if (--argc < 1)
  980. goto bad;
  981. alpn_in = *(++argv);
  982. } else if (strcmp(*argv, "-serverinfo") == 0) {
  983. char *c;
  984. int start = 0;
  985. int len;
  986. if (--argc < 1)
  987. goto bad;
  988. c = *(++argv);
  989. serverinfo_types_count = 0;
  990. len = strlen(c);
  991. for (i = 0; i <= len; ++i) {
  992. if (i == len || c[i] == ',') {
  993. serverinfo_types[serverinfo_types_count]
  994. = atoi(c + start);
  995. serverinfo_types_count++;
  996. start = i + 1;
  997. }
  998. if (serverinfo_types_count == MAX_SI_TYPES)
  999. break;
  1000. }
  1001. }
  1002. #endif
  1003. #ifdef FIONBIO
  1004. else if (strcmp(*argv, "-nbio") == 0) {
  1005. c_nbio = 1;
  1006. }
  1007. #endif
  1008. else if (strcmp(*argv, "-starttls") == 0) {
  1009. if (--argc < 1)
  1010. goto bad;
  1011. ++argv;
  1012. if (strcmp(*argv, "smtp") == 0)
  1013. starttls_proto = PROTO_SMTP;
  1014. else if (strcmp(*argv, "pop3") == 0)
  1015. starttls_proto = PROTO_POP3;
  1016. else if (strcmp(*argv, "imap") == 0)
  1017. starttls_proto = PROTO_IMAP;
  1018. else if (strcmp(*argv, "ftp") == 0)
  1019. starttls_proto = PROTO_FTP;
  1020. else if (strcmp(*argv, "xmpp") == 0)
  1021. starttls_proto = PROTO_XMPP;
  1022. else
  1023. goto bad;
  1024. }
  1025. #ifndef OPENSSL_NO_ENGINE
  1026. else if (strcmp(*argv, "-engine") == 0) {
  1027. if (--argc < 1)
  1028. goto bad;
  1029. engine_id = *(++argv);
  1030. } else if (strcmp(*argv, "-ssl_client_engine") == 0) {
  1031. if (--argc < 1)
  1032. goto bad;
  1033. ssl_client_engine_id = *(++argv);
  1034. }
  1035. #endif
  1036. else if (strcmp(*argv, "-rand") == 0) {
  1037. if (--argc < 1)
  1038. goto bad;
  1039. inrand = *(++argv);
  1040. }
  1041. #ifndef OPENSSL_NO_TLSEXT
  1042. else if (strcmp(*argv, "-servername") == 0) {
  1043. if (--argc < 1)
  1044. goto bad;
  1045. servername = *(++argv);
  1046. /* meth=TLSv1_client_method(); */
  1047. }
  1048. #endif
  1049. #ifndef OPENSSL_NO_JPAKE
  1050. else if (strcmp(*argv, "-jpake") == 0) {
  1051. if (--argc < 1)
  1052. goto bad;
  1053. jpake_secret = *++argv;
  1054. }
  1055. #endif
  1056. #ifndef OPENSSL_NO_SRTP
  1057. else if (strcmp(*argv, "-use_srtp") == 0) {
  1058. if (--argc < 1)
  1059. goto bad;
  1060. srtp_profiles = *(++argv);
  1061. }
  1062. #endif
  1063. else if (strcmp(*argv, "-keymatexport") == 0) {
  1064. if (--argc < 1)
  1065. goto bad;
  1066. keymatexportlabel = *(++argv);
  1067. } else if (strcmp(*argv, "-keymatexportlen") == 0) {
  1068. if (--argc < 1)
  1069. goto bad;
  1070. keymatexportlen = atoi(*(++argv));
  1071. if (keymatexportlen == 0)
  1072. goto bad;
  1073. } else {
  1074. BIO_printf(bio_err, "unknown option %s\n", *argv);
  1075. badop = 1;
  1076. break;
  1077. }
  1078. argc--;
  1079. argv++;
  1080. }
  1081. if (badop) {
  1082. bad:
  1083. sc_usage();
  1084. goto end;
  1085. }
  1086. #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
  1087. if (jpake_secret) {
  1088. if (psk_key) {
  1089. BIO_printf(bio_err, "Can't use JPAKE and PSK together\n");
  1090. goto end;
  1091. }
  1092. psk_identity = "JPAKE";
  1093. }
  1094. #endif
  1095. if (prot_opt > 1) {
  1096. BIO_printf(bio_err, "Cannot supply multiple protocol flags\n");
  1097. goto end;
  1098. }
  1099. if (prot_opt == 1 && no_prot_opt) {
  1100. BIO_printf(bio_err, "Cannot supply both a protocol flag and "
  1101. "\"-no_<prot>\"\n");
  1102. goto end;
  1103. }
  1104. OpenSSL_add_ssl_algorithms();
  1105. SSL_load_error_strings();
  1106. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
  1107. next_proto.status = -1;
  1108. if (next_proto_neg_in) {
  1109. next_proto.data =
  1110. next_protos_parse(&next_proto.len, next_proto_neg_in);
  1111. if (next_proto.data == NULL) {
  1112. BIO_printf(bio_err, "Error parsing -nextprotoneg argument\n");
  1113. goto end;
  1114. }
  1115. } else
  1116. next_proto.data = NULL;
  1117. #endif
  1118. e = setup_engine(bio_err, engine_id, 1);
  1119. #ifndef OPENSSL_NO_ENGINE
  1120. if (ssl_client_engine_id) {
  1121. ssl_client_engine = ENGINE_by_id(ssl_client_engine_id);
  1122. if (!ssl_client_engine) {
  1123. BIO_printf(bio_err, "Error getting client auth engine\n");
  1124. goto end;
  1125. }
  1126. }
  1127. #endif
  1128. if (!app_passwd(bio_err, passarg, NULL, &pass, NULL)) {
  1129. BIO_printf(bio_err, "Error getting password\n");
  1130. goto end;
  1131. }
  1132. if (key_file == NULL)
  1133. key_file = cert_file;
  1134. if (key_file) {
  1135. key = load_key(bio_err, key_file, key_format, 0, pass, e,
  1136. "client certificate private key file");
  1137. if (!key) {
  1138. ERR_print_errors(bio_err);
  1139. goto end;
  1140. }
  1141. }
  1142. if (cert_file) {
  1143. cert = load_cert(bio_err, cert_file, cert_format,
  1144. NULL, e, "client certificate file");
  1145. if (!cert) {
  1146. ERR_print_errors(bio_err);
  1147. goto end;
  1148. }
  1149. }
  1150. if (chain_file) {
  1151. chain = load_certs(bio_err, chain_file, FORMAT_PEM,
  1152. NULL, e, "client certificate chain");
  1153. if (!chain)
  1154. goto end;
  1155. }
  1156. if (crl_file) {
  1157. X509_CRL *crl;
  1158. crl = load_crl(crl_file, crl_format);
  1159. if (!crl) {
  1160. BIO_puts(bio_err, "Error loading CRL\n");
  1161. ERR_print_errors(bio_err);
  1162. goto end;
  1163. }
  1164. crls = sk_X509_CRL_new_null();
  1165. if (!crls || !sk_X509_CRL_push(crls, crl)) {
  1166. BIO_puts(bio_err, "Error adding CRL\n");
  1167. ERR_print_errors(bio_err);
  1168. X509_CRL_free(crl);
  1169. goto end;
  1170. }
  1171. }
  1172. if (!load_excert(&exc, bio_err))
  1173. goto end;
  1174. if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
  1175. && !RAND_status()) {
  1176. BIO_printf(bio_err,
  1177. "warning, not much extra random data, consider using the -rand option\n");
  1178. }
  1179. if (inrand != NULL)
  1180. BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
  1181. app_RAND_load_files(inrand));
  1182. if (bio_c_out == NULL) {
  1183. if (c_quiet && !c_debug) {
  1184. bio_c_out = BIO_new(BIO_s_null());
  1185. if (c_msg && !bio_c_msg)
  1186. bio_c_msg = BIO_new_fp(stdout, BIO_NOCLOSE);
  1187. } else {
  1188. if (bio_c_out == NULL)
  1189. bio_c_out = BIO_new_fp(stdout, BIO_NOCLOSE);
  1190. }
  1191. }
  1192. #ifndef OPENSSL_NO_SRP
  1193. if (!app_passwd(bio_err, srppass, NULL, &srp_arg.srppassin, NULL)) {
  1194. BIO_printf(bio_err, "Error getting password\n");
  1195. goto end;
  1196. }
  1197. #endif
  1198. ctx = SSL_CTX_new(meth);
  1199. if (ctx == NULL) {
  1200. ERR_print_errors(bio_err);
  1201. goto end;
  1202. }
  1203. if (vpm)
  1204. SSL_CTX_set1_param(ctx, vpm);
  1205. if (!args_ssl_call(ctx, bio_err, cctx, ssl_args, 1, no_jpake)) {
  1206. ERR_print_errors(bio_err);
  1207. goto end;
  1208. }
  1209. if (!ssl_load_stores(ctx, vfyCApath, vfyCAfile, chCApath, chCAfile,
  1210. crls, crl_download)) {
  1211. BIO_printf(bio_err, "Error loading store locations\n");
  1212. ERR_print_errors(bio_err);
  1213. goto end;
  1214. }
  1215. #ifndef OPENSSL_NO_ENGINE
  1216. if (ssl_client_engine) {
  1217. if (!SSL_CTX_set_client_cert_engine(ctx, ssl_client_engine)) {
  1218. BIO_puts(bio_err, "Error setting client auth engine\n");
  1219. ERR_print_errors(bio_err);
  1220. ENGINE_free(ssl_client_engine);
  1221. goto end;
  1222. }
  1223. ENGINE_free(ssl_client_engine);
  1224. }
  1225. #endif
  1226. #ifndef OPENSSL_NO_PSK
  1227. # ifdef OPENSSL_NO_JPAKE
  1228. if (psk_key != NULL)
  1229. # else
  1230. if (psk_key != NULL || jpake_secret)
  1231. # endif
  1232. {
  1233. if (c_debug)
  1234. BIO_printf(bio_c_out,
  1235. "PSK key given or JPAKE in use, setting client callback\n");
  1236. SSL_CTX_set_psk_client_callback(ctx, psk_client_cb);
  1237. }
  1238. #endif
  1239. #ifndef OPENSSL_NO_SRTP
  1240. if (srtp_profiles != NULL)
  1241. SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles);
  1242. #endif
  1243. if (exc)
  1244. ssl_ctx_set_excert(ctx, exc);
  1245. #if !defined(OPENSSL_NO_TLSEXT)
  1246. # if !defined(OPENSSL_NO_NEXTPROTONEG)
  1247. if (next_proto.data)
  1248. SSL_CTX_set_next_proto_select_cb(ctx, next_proto_cb, &next_proto);
  1249. # endif
  1250. if (alpn_in) {
  1251. unsigned short alpn_len;
  1252. unsigned char *alpn = next_protos_parse(&alpn_len, alpn_in);
  1253. if (alpn == NULL) {
  1254. BIO_printf(bio_err, "Error parsing -alpn argument\n");
  1255. goto end;
  1256. }
  1257. SSL_CTX_set_alpn_protos(ctx, alpn, alpn_len);
  1258. OPENSSL_free(alpn);
  1259. }
  1260. #endif
  1261. #ifndef OPENSSL_NO_TLSEXT
  1262. for (i = 0; i < serverinfo_types_count; i++) {
  1263. SSL_CTX_add_client_custom_ext(ctx,
  1264. serverinfo_types[i],
  1265. NULL, NULL, NULL,
  1266. serverinfo_cli_parse_cb, NULL);
  1267. }
  1268. #endif
  1269. if (state)
  1270. SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
  1271. #if 0
  1272. else
  1273. SSL_CTX_set_cipher_list(ctx, getenv("SSL_CIPHER"));
  1274. #endif
  1275. SSL_CTX_set_verify(ctx, verify, verify_callback);
  1276. if ((CAfile || CApath)
  1277. && !SSL_CTX_load_verify_locations(ctx, CAfile, CApath)) {
  1278. ERR_print_errors(bio_err);
  1279. }
  1280. if (!SSL_CTX_set_default_verify_paths(ctx)) {
  1281. ERR_print_errors(bio_err);
  1282. }
  1283. ssl_ctx_add_crls(ctx, crls, crl_download);
  1284. if (!set_cert_key_stuff(ctx, cert, key, chain, build_chain))
  1285. goto end;
  1286. #ifndef OPENSSL_NO_TLSEXT
  1287. if (servername != NULL) {
  1288. tlsextcbp.biodebug = bio_err;
  1289. SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
  1290. SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
  1291. }
  1292. # ifndef OPENSSL_NO_SRP
  1293. if (srp_arg.srplogin) {
  1294. if (!srp_lateuser && !SSL_CTX_set_srp_username(ctx, srp_arg.srplogin)) {
  1295. BIO_printf(bio_err, "Unable to set SRP username\n");
  1296. goto end;
  1297. }
  1298. srp_arg.msg = c_msg;
  1299. srp_arg.debug = c_debug;
  1300. SSL_CTX_set_srp_cb_arg(ctx, &srp_arg);
  1301. SSL_CTX_set_srp_client_pwd_callback(ctx, ssl_give_srp_client_pwd_cb);
  1302. SSL_CTX_set_srp_strength(ctx, srp_arg.strength);
  1303. if (c_msg || c_debug || srp_arg.amp == 0)
  1304. SSL_CTX_set_srp_verify_param_callback(ctx,
  1305. ssl_srp_verify_param_cb);
  1306. }
  1307. # endif
  1308. #endif
  1309. con = SSL_new(ctx);
  1310. if (sess_in) {
  1311. SSL_SESSION *sess;
  1312. BIO *stmp = BIO_new_file(sess_in, "r");
  1313. if (!stmp) {
  1314. BIO_printf(bio_err, "Can't open session file %s\n", sess_in);
  1315. ERR_print_errors(bio_err);
  1316. goto end;
  1317. }
  1318. sess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);
  1319. BIO_free(stmp);
  1320. if (!sess) {
  1321. BIO_printf(bio_err, "Can't open session file %s\n", sess_in);
  1322. ERR_print_errors(bio_err);
  1323. goto end;
  1324. }
  1325. SSL_set_session(con, sess);
  1326. SSL_SESSION_free(sess);
  1327. }
  1328. if (fallback_scsv)
  1329. SSL_set_mode(con, SSL_MODE_SEND_FALLBACK_SCSV);
  1330. #ifndef OPENSSL_NO_TLSEXT
  1331. if (servername != NULL) {
  1332. if (!SSL_set_tlsext_host_name(con, servername)) {
  1333. BIO_printf(bio_err, "Unable to set TLS servername extension.\n");
  1334. ERR_print_errors(bio_err);
  1335. goto end;
  1336. }
  1337. }
  1338. #endif
  1339. #ifndef OPENSSL_NO_KRB5
  1340. if (con && (kctx = kssl_ctx_new()) != NULL) {
  1341. SSL_set0_kssl_ctx(con, kctx);
  1342. kssl_ctx_setstring(kctx, KSSL_SERVER, host);
  1343. }
  1344. #endif /* OPENSSL_NO_KRB5 */
  1345. /* SSL_set_cipher_list(con,"RC4-MD5"); */
  1346. #if 0
  1347. # ifdef TLSEXT_TYPE_opaque_prf_input
  1348. SSL_set_tlsext_opaque_prf_input(con, "Test client", 11);
  1349. # endif
  1350. #endif
  1351. re_start:
  1352. if (init_client(&s, host, port, socket_type) == 0) {
  1353. BIO_printf(bio_err, "connect:errno=%d\n", get_last_socket_error());
  1354. SHUTDOWN(s);
  1355. goto end;
  1356. }
  1357. BIO_printf(bio_c_out, "CONNECTED(%08X)\n", s);
  1358. #ifdef FIONBIO
  1359. if (c_nbio) {
  1360. unsigned long l = 1;
  1361. BIO_printf(bio_c_out, "turning on non blocking io\n");
  1362. if (BIO_socket_ioctl(s, FIONBIO, &l) < 0) {
  1363. ERR_print_errors(bio_err);
  1364. goto end;
  1365. }
  1366. }
  1367. #endif
  1368. if (c_Pause & 0x01)
  1369. SSL_set_debug(con, 1);
  1370. if (socket_type == SOCK_DGRAM) {
  1371. sbio = BIO_new_dgram(s, BIO_NOCLOSE);
  1372. if (getsockname(s, &peer, (void *)&peerlen) < 0) {
  1373. BIO_printf(bio_err, "getsockname:errno=%d\n",
  1374. get_last_socket_error());
  1375. SHUTDOWN(s);
  1376. goto end;
  1377. }
  1378. (void)BIO_ctrl_set_connected(sbio, 1, &peer);
  1379. if (enable_timeouts) {
  1380. timeout.tv_sec = 0;
  1381. timeout.tv_usec = DGRAM_RCV_TIMEOUT;
  1382. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
  1383. timeout.tv_sec = 0;
  1384. timeout.tv_usec = DGRAM_SND_TIMEOUT;
  1385. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
  1386. }
  1387. if (socket_mtu) {
  1388. if (socket_mtu < DTLS_get_link_min_mtu(con)) {
  1389. BIO_printf(bio_err, "MTU too small. Must be at least %ld\n",
  1390. DTLS_get_link_min_mtu(con));
  1391. BIO_free(sbio);
  1392. goto shut;
  1393. }
  1394. SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
  1395. if (!DTLS_set_link_mtu(con, socket_mtu)) {
  1396. BIO_printf(bio_err, "Failed to set MTU\n");
  1397. BIO_free(sbio);
  1398. goto shut;
  1399. }
  1400. } else
  1401. /* want to do MTU discovery */
  1402. BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
  1403. } else
  1404. sbio = BIO_new_socket(s, BIO_NOCLOSE);
  1405. if (nbio_test) {
  1406. BIO *test;
  1407. test = BIO_new(BIO_f_nbio_test());
  1408. sbio = BIO_push(test, sbio);
  1409. }
  1410. if (c_debug) {
  1411. SSL_set_debug(con, 1);
  1412. BIO_set_callback(sbio, bio_dump_callback);
  1413. BIO_set_callback_arg(sbio, (char *)bio_c_out);
  1414. }
  1415. if (c_msg) {
  1416. #ifndef OPENSSL_NO_SSL_TRACE
  1417. if (c_msg == 2)
  1418. SSL_set_msg_callback(con, SSL_trace);
  1419. else
  1420. #endif
  1421. SSL_set_msg_callback(con, msg_cb);
  1422. SSL_set_msg_callback_arg(con, bio_c_msg ? bio_c_msg : bio_c_out);
  1423. }
  1424. #ifndef OPENSSL_NO_TLSEXT
  1425. if (c_tlsextdebug) {
  1426. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  1427. SSL_set_tlsext_debug_arg(con, bio_c_out);
  1428. }
  1429. if (c_status_req) {
  1430. SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp);
  1431. SSL_CTX_set_tlsext_status_cb(ctx, ocsp_resp_cb);
  1432. SSL_CTX_set_tlsext_status_arg(ctx, bio_c_out);
  1433. # if 0
  1434. {
  1435. STACK_OF(OCSP_RESPID) *ids = sk_OCSP_RESPID_new_null();
  1436. OCSP_RESPID *id = OCSP_RESPID_new();
  1437. id->value.byKey = ASN1_OCTET_STRING_new();
  1438. id->type = V_OCSP_RESPID_KEY;
  1439. ASN1_STRING_set(id->value.byKey, "Hello World", -1);
  1440. sk_OCSP_RESPID_push(ids, id);
  1441. SSL_set_tlsext_status_ids(con, ids);
  1442. }
  1443. # endif
  1444. }
  1445. #endif
  1446. #ifndef OPENSSL_NO_JPAKE
  1447. if (jpake_secret)
  1448. jpake_client_auth(bio_c_out, sbio, jpake_secret);
  1449. #endif
  1450. SSL_set_bio(con, sbio, sbio);
  1451. SSL_set_connect_state(con);
  1452. /* ok, lets connect */
  1453. if (fileno_stdin() > SSL_get_fd(con))
  1454. width = fileno_stdin() + 1;
  1455. else
  1456. width = SSL_get_fd(con) + 1;
  1457. read_tty = 1;
  1458. write_tty = 0;
  1459. tty_on = 0;
  1460. read_ssl = 1;
  1461. write_ssl = 1;
  1462. cbuf_len = 0;
  1463. cbuf_off = 0;
  1464. sbuf_len = 0;
  1465. sbuf_off = 0;
  1466. /* This is an ugly hack that does a lot of assumptions */
  1467. /*
  1468. * We do have to handle multi-line responses which may come in a single
  1469. * packet or not. We therefore have to use BIO_gets() which does need a
  1470. * buffering BIO. So during the initial chitchat we do push a buffering
  1471. * BIO into the chain that is removed again later on to not disturb the
  1472. * rest of the s_client operation.
  1473. */
  1474. if (starttls_proto == PROTO_SMTP) {
  1475. int foundit = 0;
  1476. BIO *fbio = BIO_new(BIO_f_buffer());
  1477. BIO_push(fbio, sbio);
  1478. /* wait for multi-line response to end from SMTP */
  1479. do {
  1480. mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
  1481. }
  1482. while (mbuf_len > 3 && mbuf[3] == '-');
  1483. /* STARTTLS command requires EHLO... */
  1484. BIO_printf(fbio, "EHLO openssl.client.net\r\n");
  1485. (void)BIO_flush(fbio);
  1486. /* wait for multi-line response to end EHLO SMTP response */
  1487. do {
  1488. mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
  1489. if (strstr(mbuf, "STARTTLS"))
  1490. foundit = 1;
  1491. }
  1492. while (mbuf_len > 3 && mbuf[3] == '-');
  1493. (void)BIO_flush(fbio);
  1494. BIO_pop(fbio);
  1495. BIO_free(fbio);
  1496. if (!foundit)
  1497. BIO_printf(bio_err,
  1498. "didn't found starttls in server response,"
  1499. " try anyway...\n");
  1500. BIO_printf(sbio, "STARTTLS\r\n");
  1501. BIO_read(sbio, sbuf, BUFSIZZ);
  1502. } else if (starttls_proto == PROTO_POP3) {
  1503. BIO_read(sbio, mbuf, BUFSIZZ);
  1504. BIO_printf(sbio, "STLS\r\n");
  1505. BIO_read(sbio, sbuf, BUFSIZZ);
  1506. } else if (starttls_proto == PROTO_IMAP) {
  1507. int foundit = 0;
  1508. BIO *fbio = BIO_new(BIO_f_buffer());
  1509. BIO_push(fbio, sbio);
  1510. BIO_gets(fbio, mbuf, BUFSIZZ);
  1511. /* STARTTLS command requires CAPABILITY... */
  1512. BIO_printf(fbio, ". CAPABILITY\r\n");
  1513. (void)BIO_flush(fbio);
  1514. /* wait for multi-line CAPABILITY response */
  1515. do {
  1516. mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
  1517. if (strstr(mbuf, "STARTTLS"))
  1518. foundit = 1;
  1519. }
  1520. while (mbuf_len > 3 && mbuf[0] != '.');
  1521. (void)BIO_flush(fbio);
  1522. BIO_pop(fbio);
  1523. BIO_free(fbio);
  1524. if (!foundit)
  1525. BIO_printf(bio_err,
  1526. "didn't found STARTTLS in server response,"
  1527. " try anyway...\n");
  1528. BIO_printf(sbio, ". STARTTLS\r\n");
  1529. BIO_read(sbio, sbuf, BUFSIZZ);
  1530. } else if (starttls_proto == PROTO_FTP) {
  1531. BIO *fbio = BIO_new(BIO_f_buffer());
  1532. BIO_push(fbio, sbio);
  1533. /* wait for multi-line response to end from FTP */
  1534. do {
  1535. mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
  1536. }
  1537. while (mbuf_len > 3 && mbuf[3] == '-');
  1538. (void)BIO_flush(fbio);
  1539. BIO_pop(fbio);
  1540. BIO_free(fbio);
  1541. BIO_printf(sbio, "AUTH TLS\r\n");
  1542. BIO_read(sbio, sbuf, BUFSIZZ);
  1543. }
  1544. if (starttls_proto == PROTO_XMPP) {
  1545. int seen = 0;
  1546. BIO_printf(sbio, "<stream:stream "
  1547. "xmlns:stream='http://etherx.jabber.org/streams' "
  1548. "xmlns='jabber:client' to='%s' version='1.0'>", host);
  1549. seen = BIO_read(sbio, mbuf, BUFSIZZ);
  1550. mbuf[seen] = 0;
  1551. while (!strstr
  1552. (mbuf, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'")) {
  1553. if (strstr(mbuf, "/stream:features>"))
  1554. goto shut;
  1555. seen = BIO_read(sbio, mbuf, BUFSIZZ);
  1556. if (seen <= 0)
  1557. goto shut;
  1558. mbuf[seen] = 0;
  1559. }
  1560. BIO_printf(sbio,
  1561. "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
  1562. seen = BIO_read(sbio, sbuf, BUFSIZZ);
  1563. sbuf[seen] = 0;
  1564. if (!strstr(sbuf, "<proceed"))
  1565. goto shut;
  1566. mbuf[0] = 0;
  1567. }
  1568. for (;;) {
  1569. FD_ZERO(&readfds);
  1570. FD_ZERO(&writefds);
  1571. if ((SSL_version(con) == DTLS1_VERSION) &&
  1572. DTLSv1_get_timeout(con, &timeout))
  1573. timeoutp = &timeout;
  1574. else
  1575. timeoutp = NULL;
  1576. if (SSL_in_init(con) && !SSL_total_renegotiations(con)) {
  1577. in_init = 1;
  1578. tty_on = 0;
  1579. } else {
  1580. tty_on = 1;
  1581. if (in_init) {
  1582. in_init = 0;
  1583. #if 0 /* This test doesn't really work as intended
  1584. * (needs to be fixed) */
  1585. # ifndef OPENSSL_NO_TLSEXT
  1586. if (servername != NULL && !SSL_session_reused(con)) {
  1587. BIO_printf(bio_c_out,
  1588. "Server did %sacknowledge servername extension.\n",
  1589. tlsextcbp.ack ? "" : "not ");
  1590. }
  1591. # endif
  1592. #endif
  1593. if (sess_out) {
  1594. BIO *stmp = BIO_new_file(sess_out, "w");
  1595. if (stmp) {
  1596. PEM_write_bio_SSL_SESSION(stmp, SSL_get_session(con));
  1597. BIO_free(stmp);
  1598. } else
  1599. BIO_printf(bio_err, "Error writing session file %s\n",
  1600. sess_out);
  1601. }
  1602. if (c_brief) {
  1603. BIO_puts(bio_err, "CONNECTION ESTABLISHED\n");
  1604. print_ssl_summary(bio_err, con);
  1605. }
  1606. print_stuff(bio_c_out, con, full_log);
  1607. if (full_log > 0)
  1608. full_log--;
  1609. if (starttls_proto) {
  1610. BIO_printf(bio_err, "%s", mbuf);
  1611. /* We don't need to know any more */
  1612. starttls_proto = PROTO_OFF;
  1613. }
  1614. if (reconnect) {
  1615. reconnect--;
  1616. BIO_printf(bio_c_out,
  1617. "drop connection and then reconnect\n");
  1618. SSL_shutdown(con);
  1619. SSL_set_connect_state(con);
  1620. SHUTDOWN(SSL_get_fd(con));
  1621. goto re_start;
  1622. }
  1623. }
  1624. }
  1625. ssl_pending = read_ssl && SSL_pending(con);
  1626. if (!ssl_pending) {
  1627. #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) && !defined (OPENSSL_SYS_BEOS_R5)
  1628. if (tty_on) {
  1629. if (read_tty)
  1630. openssl_fdset(fileno_stdin(), &readfds);
  1631. #if !defined(OPENSSL_SYS_VMS)
  1632. if (write_tty)
  1633. openssl_fdset(fileno_stdout(), &writefds);
  1634. #endif
  1635. }
  1636. if (read_ssl)
  1637. openssl_fdset(SSL_get_fd(con), &readfds);
  1638. if (write_ssl)
  1639. openssl_fdset(SSL_get_fd(con), &writefds);
  1640. #else
  1641. if (!tty_on || !write_tty) {
  1642. if (read_ssl)
  1643. openssl_fdset(SSL_get_fd(con), &readfds);
  1644. if (write_ssl)
  1645. openssl_fdset(SSL_get_fd(con), &writefds);
  1646. }
  1647. #endif
  1648. /*- printf("mode tty(%d %d%d) ssl(%d%d)\n",
  1649. tty_on,read_tty,write_tty,read_ssl,write_ssl);*/
  1650. /*
  1651. * Note: under VMS with SOCKETSHR the second parameter is
  1652. * currently of type (int *) whereas under other systems it is
  1653. * (void *) if you don't have a cast it will choke the compiler:
  1654. * if you do have a cast then you can either go for (int *) or
  1655. * (void *).
  1656. */
  1657. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
  1658. /*
  1659. * Under Windows/DOS we make the assumption that we can always
  1660. * write to the tty: therefore if we need to write to the tty we
  1661. * just fall through. Otherwise we timeout the select every
  1662. * second and see if there are any keypresses. Note: this is a
  1663. * hack, in a proper Windows application we wouldn't do this.
  1664. */
  1665. i = 0;
  1666. if (!write_tty) {
  1667. if (read_tty) {
  1668. tv.tv_sec = 1;
  1669. tv.tv_usec = 0;
  1670. i = select(width, (void *)&readfds, (void *)&writefds,
  1671. NULL, &tv);
  1672. # if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
  1673. if (!i && (!_kbhit() || !read_tty))
  1674. continue;
  1675. # else
  1676. if (!i && (!((_kbhit())
  1677. || (WAIT_OBJECT_0 ==
  1678. WaitForSingleObject(GetStdHandle
  1679. (STD_INPUT_HANDLE),
  1680. 0)))
  1681. || !read_tty))
  1682. continue;
  1683. # endif
  1684. } else
  1685. i = select(width, (void *)&readfds, (void *)&writefds,
  1686. NULL, timeoutp);
  1687. }
  1688. #elif defined(OPENSSL_SYS_NETWARE)
  1689. if (!write_tty) {
  1690. if (read_tty) {
  1691. tv.tv_sec = 1;
  1692. tv.tv_usec = 0;
  1693. i = select(width, (void *)&readfds, (void *)&writefds,
  1694. NULL, &tv);
  1695. } else
  1696. i = select(width, (void *)&readfds, (void *)&writefds,
  1697. NULL, timeoutp);
  1698. }
  1699. #elif defined(OPENSSL_SYS_BEOS_R5)
  1700. /* Under BeOS-R5 the situation is similar to DOS */
  1701. i = 0;
  1702. stdin_set = 0;
  1703. (void)fcntl(fileno_stdin(), F_SETFL, O_NONBLOCK);
  1704. if (!write_tty) {
  1705. if (read_tty) {
  1706. tv.tv_sec = 1;
  1707. tv.tv_usec = 0;
  1708. i = select(width, (void *)&readfds, (void *)&writefds,
  1709. NULL, &tv);
  1710. if (read(fileno_stdin(), sbuf, 0) >= 0)
  1711. stdin_set = 1;
  1712. if (!i && (stdin_set != 1 || !read_tty))
  1713. continue;
  1714. } else
  1715. i = select(width, (void *)&readfds, (void *)&writefds,
  1716. NULL, timeoutp);
  1717. }
  1718. (void)fcntl(fileno_stdin(), F_SETFL, 0);
  1719. #else
  1720. i = select(width, (void *)&readfds, (void *)&writefds,
  1721. NULL, timeoutp);
  1722. #endif
  1723. if (i < 0) {
  1724. BIO_printf(bio_err, "bad select %d\n",
  1725. get_last_socket_error());
  1726. goto shut;
  1727. /* goto end; */
  1728. }
  1729. }
  1730. if ((SSL_version(con) == DTLS1_VERSION)
  1731. && DTLSv1_handle_timeout(con) > 0) {
  1732. BIO_printf(bio_err, "TIMEOUT occured\n");
  1733. }
  1734. if (!ssl_pending && FD_ISSET(SSL_get_fd(con), &writefds)) {
  1735. k = SSL_write(con, &(cbuf[cbuf_off]), (unsigned int)cbuf_len);
  1736. switch (SSL_get_error(con, k)) {
  1737. case SSL_ERROR_NONE:
  1738. cbuf_off += k;
  1739. cbuf_len -= k;
  1740. if (k <= 0)
  1741. goto end;
  1742. /* we have done a write(con,NULL,0); */
  1743. if (cbuf_len <= 0) {
  1744. read_tty = 1;
  1745. write_ssl = 0;
  1746. } else { /* if (cbuf_len > 0) */
  1747. read_tty = 0;
  1748. write_ssl = 1;
  1749. }
  1750. break;
  1751. case SSL_ERROR_WANT_WRITE:
  1752. BIO_printf(bio_c_out, "write W BLOCK\n");
  1753. write_ssl = 1;
  1754. read_tty = 0;
  1755. break;
  1756. case SSL_ERROR_WANT_READ:
  1757. BIO_printf(bio_c_out, "write R BLOCK\n");
  1758. write_tty = 0;
  1759. read_ssl = 1;
  1760. write_ssl = 0;
  1761. break;
  1762. case SSL_ERROR_WANT_X509_LOOKUP:
  1763. BIO_printf(bio_c_out, "write X BLOCK\n");
  1764. break;
  1765. case SSL_ERROR_ZERO_RETURN:
  1766. if (cbuf_len != 0) {
  1767. BIO_printf(bio_c_out, "shutdown\n");
  1768. ret = 0;
  1769. goto shut;
  1770. } else {
  1771. read_tty = 1;
  1772. write_ssl = 0;
  1773. break;
  1774. }
  1775. case SSL_ERROR_SYSCALL:
  1776. if ((k != 0) || (cbuf_len != 0)) {
  1777. BIO_printf(bio_err, "write:errno=%d\n",
  1778. get_last_socket_error());
  1779. goto shut;
  1780. } else {
  1781. read_tty = 1;
  1782. write_ssl = 0;
  1783. }
  1784. break;
  1785. case SSL_ERROR_SSL:
  1786. ERR_print_errors(bio_err);
  1787. goto shut;
  1788. }
  1789. }
  1790. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5) || defined(OPENSSL_SYS_VMS)
  1791. /* Assume Windows/DOS/BeOS can always write */
  1792. else if (!ssl_pending && write_tty)
  1793. #else
  1794. else if (!ssl_pending && FD_ISSET(fileno_stdout(), &writefds))
  1795. #endif
  1796. {
  1797. #ifdef CHARSET_EBCDIC
  1798. ascii2ebcdic(&(sbuf[sbuf_off]), &(sbuf[sbuf_off]), sbuf_len);
  1799. #endif
  1800. i = raw_write_stdout(&(sbuf[sbuf_off]), sbuf_len);
  1801. if (i <= 0) {
  1802. BIO_printf(bio_c_out, "DONE\n");
  1803. ret = 0;
  1804. goto shut;
  1805. /* goto end; */
  1806. }
  1807. sbuf_len -= i;;
  1808. sbuf_off += i;
  1809. if (sbuf_len <= 0) {
  1810. read_ssl = 1;
  1811. write_tty = 0;
  1812. }
  1813. } else if (ssl_pending || FD_ISSET(SSL_get_fd(con), &readfds)) {
  1814. #ifdef RENEG
  1815. {
  1816. static int iiii;
  1817. if (++iiii == 52) {
  1818. SSL_renegotiate(con);
  1819. iiii = 0;
  1820. }
  1821. }
  1822. #endif
  1823. #if 1
  1824. k = SSL_read(con, sbuf, 1024 /* BUFSIZZ */ );
  1825. #else
  1826. /* Demo for pending and peek :-) */
  1827. k = SSL_read(con, sbuf, 16);
  1828. {
  1829. char zbuf[10240];
  1830. printf("read=%d pending=%d peek=%d\n", k, SSL_pending(con),
  1831. SSL_peek(con, zbuf, 10240));
  1832. }
  1833. #endif
  1834. switch (SSL_get_error(con, k)) {
  1835. case SSL_ERROR_NONE:
  1836. if (k <= 0)
  1837. goto end;
  1838. sbuf_off = 0;
  1839. sbuf_len = k;
  1840. read_ssl = 0;
  1841. write_tty = 1;
  1842. break;
  1843. case SSL_ERROR_WANT_WRITE:
  1844. BIO_printf(bio_c_out, "read W BLOCK\n");
  1845. write_ssl = 1;
  1846. read_tty = 0;
  1847. break;
  1848. case SSL_ERROR_WANT_READ:
  1849. BIO_printf(bio_c_out, "read R BLOCK\n");
  1850. write_tty = 0;
  1851. read_ssl = 1;
  1852. if ((read_tty == 0) && (write_ssl == 0))
  1853. write_ssl = 1;
  1854. break;
  1855. case SSL_ERROR_WANT_X509_LOOKUP:
  1856. BIO_printf(bio_c_out, "read X BLOCK\n");
  1857. break;
  1858. case SSL_ERROR_SYSCALL:
  1859. ret = get_last_socket_error();
  1860. if (c_brief)
  1861. BIO_puts(bio_err, "CONNECTION CLOSED BY SERVER\n");
  1862. else
  1863. BIO_printf(bio_err, "read:errno=%d\n", ret);
  1864. goto shut;
  1865. case SSL_ERROR_ZERO_RETURN:
  1866. BIO_printf(bio_c_out, "closed\n");
  1867. ret = 0;
  1868. goto shut;
  1869. case SSL_ERROR_SSL:
  1870. ERR_print_errors(bio_err);
  1871. goto shut;
  1872. /* break; */
  1873. }
  1874. }
  1875. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
  1876. # if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
  1877. else if (_kbhit())
  1878. # else
  1879. else if ((_kbhit())
  1880. || (WAIT_OBJECT_0 ==
  1881. WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0)))
  1882. # endif
  1883. #elif defined (OPENSSL_SYS_NETWARE)
  1884. else if (_kbhit())
  1885. #elif defined(OPENSSL_SYS_BEOS_R5)
  1886. else if (stdin_set)
  1887. #else
  1888. else if (FD_ISSET(fileno_stdin(), &readfds))
  1889. #endif
  1890. {
  1891. if (crlf) {
  1892. int j, lf_num;
  1893. i = raw_read_stdin(cbuf, BUFSIZZ / 2);
  1894. lf_num = 0;
  1895. /* both loops are skipped when i <= 0 */
  1896. for (j = 0; j < i; j++)
  1897. if (cbuf[j] == '\n')
  1898. lf_num++;
  1899. for (j = i - 1; j >= 0; j--) {
  1900. cbuf[j + lf_num] = cbuf[j];
  1901. if (cbuf[j] == '\n') {
  1902. lf_num--;
  1903. i++;
  1904. cbuf[j + lf_num] = '\r';
  1905. }
  1906. }
  1907. assert(lf_num == 0);
  1908. } else
  1909. i = raw_read_stdin(cbuf, BUFSIZZ);
  1910. if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q'))) {
  1911. BIO_printf(bio_err, "DONE\n");
  1912. ret = 0;
  1913. goto shut;
  1914. }
  1915. if ((!c_ign_eof) && (cbuf[0] == 'R')) {
  1916. BIO_printf(bio_err, "RENEGOTIATING\n");
  1917. SSL_renegotiate(con);
  1918. cbuf_len = 0;
  1919. }
  1920. #ifndef OPENSSL_NO_HEARTBEATS
  1921. else if ((!c_ign_eof) && (cbuf[0] == 'B')) {
  1922. BIO_printf(bio_err, "HEARTBEATING\n");
  1923. SSL_heartbeat(con);
  1924. cbuf_len = 0;
  1925. }
  1926. #endif
  1927. else {
  1928. cbuf_len = i;
  1929. cbuf_off = 0;
  1930. #ifdef CHARSET_EBCDIC
  1931. ebcdic2ascii(cbuf, cbuf, i);
  1932. #endif
  1933. }
  1934. write_ssl = 1;
  1935. read_tty = 0;
  1936. }
  1937. }
  1938. ret = 0;
  1939. shut:
  1940. if (in_init)
  1941. print_stuff(bio_c_out, con, full_log);
  1942. SSL_shutdown(con);
  1943. SHUTDOWN(SSL_get_fd(con));
  1944. end:
  1945. if (con != NULL) {
  1946. if (prexit != 0)
  1947. print_stuff(bio_c_out, con, 1);
  1948. SSL_free(con);
  1949. }
  1950. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
  1951. if (next_proto.data)
  1952. OPENSSL_free(next_proto.data);
  1953. #endif
  1954. if (ctx != NULL)
  1955. SSL_CTX_free(ctx);
  1956. if (cert)
  1957. X509_free(cert);
  1958. if (crls)
  1959. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  1960. if (key)
  1961. EVP_PKEY_free(key);
  1962. if (chain)
  1963. sk_X509_pop_free(chain, X509_free);
  1964. if (pass)
  1965. OPENSSL_free(pass);
  1966. #ifndef OPENSSL_NO_SRP
  1967. OPENSSL_free(srp_arg.srppassin);
  1968. #endif
  1969. if (vpm)
  1970. X509_VERIFY_PARAM_free(vpm);
  1971. ssl_excert_free(exc);
  1972. if (ssl_args)
  1973. sk_OPENSSL_STRING_free(ssl_args);
  1974. if (cctx)
  1975. SSL_CONF_CTX_free(cctx);
  1976. #ifndef OPENSSL_NO_JPAKE
  1977. if (jpake_secret && psk_key)
  1978. OPENSSL_free(psk_key);
  1979. #endif
  1980. if (cbuf != NULL) {
  1981. OPENSSL_cleanse(cbuf, BUFSIZZ);
  1982. OPENSSL_free(cbuf);
  1983. }
  1984. if (sbuf != NULL) {
  1985. OPENSSL_cleanse(sbuf, BUFSIZZ);
  1986. OPENSSL_free(sbuf);
  1987. }
  1988. if (mbuf != NULL) {
  1989. OPENSSL_cleanse(mbuf, BUFSIZZ);
  1990. OPENSSL_free(mbuf);
  1991. }
  1992. release_engine(e);
  1993. if (bio_c_out != NULL) {
  1994. BIO_free(bio_c_out);
  1995. bio_c_out = NULL;
  1996. }
  1997. if (bio_c_msg != NULL) {
  1998. BIO_free(bio_c_msg);
  1999. bio_c_msg = NULL;
  2000. }
  2001. SSL_COMP_free_compression_methods();
  2002. apps_shutdown();
  2003. OPENSSL_EXIT(ret);
  2004. }
  2005. static void print_stuff(BIO *bio, SSL *s, int full)
  2006. {
  2007. X509 *peer = NULL;
  2008. char *p;
  2009. static const char *space = " ";
  2010. char buf[BUFSIZ];
  2011. STACK_OF(X509) *sk;
  2012. STACK_OF(X509_NAME) *sk2;
  2013. const SSL_CIPHER *c;
  2014. X509_NAME *xn;
  2015. int j, i;
  2016. #ifndef OPENSSL_NO_COMP
  2017. const COMP_METHOD *comp, *expansion;
  2018. #endif
  2019. unsigned char *exportedkeymat;
  2020. if (full) {
  2021. int got_a_chain = 0;
  2022. sk = SSL_get_peer_cert_chain(s);
  2023. if (sk != NULL) {
  2024. got_a_chain = 1; /* we don't have it for SSL2 (yet) */
  2025. BIO_printf(bio, "---\nCertificate chain\n");
  2026. for (i = 0; i < sk_X509_num(sk); i++) {
  2027. X509_NAME_oneline(X509_get_subject_name(sk_X509_value(sk, i)),
  2028. buf, sizeof(buf));
  2029. BIO_printf(bio, "%2d s:%s\n", i, buf);
  2030. X509_NAME_oneline(X509_get_issuer_name(sk_X509_value(sk, i)),
  2031. buf, sizeof(buf));
  2032. BIO_printf(bio, " i:%s\n", buf);
  2033. if (c_showcerts)
  2034. PEM_write_bio_X509(bio, sk_X509_value(sk, i));
  2035. }
  2036. }
  2037. BIO_printf(bio, "---\n");
  2038. peer = SSL_get_peer_certificate(s);
  2039. if (peer != NULL) {
  2040. BIO_printf(bio, "Server certificate\n");
  2041. /* Redundant if we showed the whole chain */
  2042. if (!(c_showcerts && got_a_chain))
  2043. PEM_write_bio_X509(bio, peer);
  2044. X509_NAME_oneline(X509_get_subject_name(peer), buf, sizeof(buf));
  2045. BIO_printf(bio, "subject=%s\n", buf);
  2046. X509_NAME_oneline(X509_get_issuer_name(peer), buf, sizeof(buf));
  2047. BIO_printf(bio, "issuer=%s\n", buf);
  2048. } else
  2049. BIO_printf(bio, "no peer certificate available\n");
  2050. sk2 = SSL_get_client_CA_list(s);
  2051. if ((sk2 != NULL) && (sk_X509_NAME_num(sk2) > 0)) {
  2052. BIO_printf(bio, "---\nAcceptable client certificate CA names\n");
  2053. for (i = 0; i < sk_X509_NAME_num(sk2); i++) {
  2054. xn = sk_X509_NAME_value(sk2, i);
  2055. X509_NAME_oneline(xn, buf, sizeof(buf));
  2056. BIO_write(bio, buf, strlen(buf));
  2057. BIO_write(bio, "\n", 1);
  2058. }
  2059. } else {
  2060. BIO_printf(bio, "---\nNo client certificate CA names sent\n");
  2061. }
  2062. p = SSL_get_shared_ciphers(s, buf, sizeof(buf));
  2063. if (p != NULL) {
  2064. /*
  2065. * This works only for SSL 2. In later protocol versions, the
  2066. * client does not know what other ciphers (in addition to the
  2067. * one to be used in the current connection) the server supports.
  2068. */
  2069. BIO_printf(bio,
  2070. "---\nCiphers common between both SSL endpoints:\n");
  2071. j = i = 0;
  2072. while (*p) {
  2073. if (*p == ':') {
  2074. BIO_write(bio, space, 15 - j % 25);
  2075. i++;
  2076. j = 0;
  2077. BIO_write(bio, ((i % 3) ? " " : "\n"), 1);
  2078. } else {
  2079. BIO_write(bio, p, 1);
  2080. j++;
  2081. }
  2082. p++;
  2083. }
  2084. BIO_write(bio, "\n", 1);
  2085. }
  2086. ssl_print_sigalgs(bio, s);
  2087. ssl_print_tmp_key(bio, s);
  2088. BIO_printf(bio,
  2089. "---\nSSL handshake has read %ld bytes and written %ld bytes\n",
  2090. BIO_number_read(SSL_get_rbio(s)),
  2091. BIO_number_written(SSL_get_wbio(s)));
  2092. }
  2093. BIO_printf(bio, (SSL_cache_hit(s) ? "---\nReused, " : "---\nNew, "));
  2094. c = SSL_get_current_cipher(s);
  2095. BIO_printf(bio, "%s, Cipher is %s\n",
  2096. SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
  2097. if (peer != NULL) {
  2098. EVP_PKEY *pktmp;
  2099. pktmp = X509_get_pubkey(peer);
  2100. BIO_printf(bio, "Server public key is %d bit\n",
  2101. EVP_PKEY_bits(pktmp));
  2102. EVP_PKEY_free(pktmp);
  2103. }
  2104. BIO_printf(bio, "Secure Renegotiation IS%s supported\n",
  2105. SSL_get_secure_renegotiation_support(s) ? "" : " NOT");
  2106. #ifndef OPENSSL_NO_COMP
  2107. comp = SSL_get_current_compression(s);
  2108. expansion = SSL_get_current_expansion(s);
  2109. BIO_printf(bio, "Compression: %s\n",
  2110. comp ? SSL_COMP_get_name(comp) : "NONE");
  2111. BIO_printf(bio, "Expansion: %s\n",
  2112. expansion ? SSL_COMP_get_name(expansion) : "NONE");
  2113. #endif
  2114. #ifdef SSL_DEBUG
  2115. {
  2116. /* Print out local port of connection: useful for debugging */
  2117. int sock;
  2118. struct sockaddr_in ladd;
  2119. socklen_t ladd_size = sizeof(ladd);
  2120. sock = SSL_get_fd(s);
  2121. getsockname(sock, (struct sockaddr *)&ladd, &ladd_size);
  2122. BIO_printf(bio_c_out, "LOCAL PORT is %u\n", ntohs(ladd.sin_port));
  2123. }
  2124. #endif
  2125. #if !defined(OPENSSL_NO_TLSEXT)
  2126. # if !defined(OPENSSL_NO_NEXTPROTONEG)
  2127. if (next_proto.status != -1) {
  2128. const unsigned char *proto;
  2129. unsigned int proto_len;
  2130. SSL_get0_next_proto_negotiated(s, &proto, &proto_len);
  2131. BIO_printf(bio, "Next protocol: (%d) ", next_proto.status);
  2132. BIO_write(bio, proto, proto_len);
  2133. BIO_write(bio, "\n", 1);
  2134. }
  2135. # endif
  2136. {
  2137. const unsigned char *proto;
  2138. unsigned int proto_len;
  2139. SSL_get0_alpn_selected(s, &proto, &proto_len);
  2140. if (proto_len > 0) {
  2141. BIO_printf(bio, "ALPN protocol: ");
  2142. BIO_write(bio, proto, proto_len);
  2143. BIO_write(bio, "\n", 1);
  2144. } else
  2145. BIO_printf(bio, "No ALPN negotiated\n");
  2146. }
  2147. #endif
  2148. #ifndef OPENSSL_NO_SRTP
  2149. {
  2150. SRTP_PROTECTION_PROFILE *srtp_profile =
  2151. SSL_get_selected_srtp_profile(s);
  2152. if (srtp_profile)
  2153. BIO_printf(bio, "SRTP Extension negotiated, profile=%s\n",
  2154. srtp_profile->name);
  2155. }
  2156. #endif
  2157. SSL_SESSION_print(bio, SSL_get_session(s));
  2158. if (keymatexportlabel != NULL) {
  2159. BIO_printf(bio, "Keying material exporter:\n");
  2160. BIO_printf(bio, " Label: '%s'\n", keymatexportlabel);
  2161. BIO_printf(bio, " Length: %i bytes\n", keymatexportlen);
  2162. exportedkeymat = OPENSSL_malloc(keymatexportlen);
  2163. if (exportedkeymat != NULL) {
  2164. if (!SSL_export_keying_material(s, exportedkeymat,
  2165. keymatexportlen,
  2166. keymatexportlabel,
  2167. strlen(keymatexportlabel),
  2168. NULL, 0, 0)) {
  2169. BIO_printf(bio, " Error\n");
  2170. } else {
  2171. BIO_printf(bio, " Keying material: ");
  2172. for (i = 0; i < keymatexportlen; i++)
  2173. BIO_printf(bio, "%02X", exportedkeymat[i]);
  2174. BIO_printf(bio, "\n");
  2175. }
  2176. OPENSSL_free(exportedkeymat);
  2177. }
  2178. }
  2179. BIO_printf(bio, "---\n");
  2180. if (peer != NULL)
  2181. X509_free(peer);
  2182. /* flush, or debugging output gets mixed with http response */
  2183. (void)BIO_flush(bio);
  2184. }
  2185. #ifndef OPENSSL_NO_TLSEXT
  2186. static int ocsp_resp_cb(SSL *s, void *arg)
  2187. {
  2188. const unsigned char *p;
  2189. int len;
  2190. OCSP_RESPONSE *rsp;
  2191. len = SSL_get_tlsext_status_ocsp_resp(s, &p);
  2192. BIO_puts(arg, "OCSP response: ");
  2193. if (!p) {
  2194. BIO_puts(arg, "no response sent\n");
  2195. return 1;
  2196. }
  2197. rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
  2198. if (!rsp) {
  2199. BIO_puts(arg, "response parse error\n");
  2200. BIO_dump_indent(arg, (char *)p, len, 4);
  2201. return 0;
  2202. }
  2203. BIO_puts(arg, "\n======================================\n");
  2204. OCSP_RESPONSE_print(arg, rsp, 0);
  2205. BIO_puts(arg, "======================================\n");
  2206. OCSP_RESPONSE_free(rsp);
  2207. return 1;
  2208. }
  2209. #endif