s_client.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2005 Nokia. All rights reserved.
  112. *
  113. * The portions of the attached software ("Contribution") is developed by
  114. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  115. * license.
  116. *
  117. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  118. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  119. * support (see RFC 4279) to OpenSSL.
  120. *
  121. * No patent licenses or other rights except those expressly stated in
  122. * the OpenSSL open source license shall be deemed granted or received
  123. * expressly, by implication, estoppel, or otherwise.
  124. *
  125. * No assurances are provided by Nokia that the Contribution does not
  126. * infringe the patent or other intellectual property rights of any third
  127. * party or that the license provides you with all the necessary rights
  128. * to make use of the Contribution.
  129. *
  130. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  131. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  132. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  133. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  134. * OTHERWISE.
  135. */
  136. #include <ctype.h>
  137. #include <stdio.h>
  138. #include <stdlib.h>
  139. #include <string.h>
  140. #include <errno.h>
  141. #include <openssl/e_os2.h>
  142. /*
  143. * With IPv6, it looks like Digital has mixed up the proper order of
  144. * recursive header file inclusion, resulting in the compiler complaining
  145. * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
  146. * needed to have fileno() declared correctly... So let's define u_int
  147. */
  148. #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
  149. # define __U_INT
  150. typedef unsigned int u_int;
  151. #endif
  152. #define USE_SOCKETS
  153. #include "apps.h"
  154. #include <openssl/x509.h>
  155. #include <openssl/ssl.h>
  156. #include <openssl/err.h>
  157. #include <openssl/pem.h>
  158. #include <openssl/rand.h>
  159. #include <openssl/ocsp.h>
  160. #include <openssl/bn.h>
  161. #include <openssl/async.h>
  162. #ifndef OPENSSL_NO_SRP
  163. # include <openssl/srp.h>
  164. #endif
  165. #include "s_apps.h"
  166. #include "timeouts.h"
  167. #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
  168. /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
  169. # undef FIONBIO
  170. #endif
  171. #define SSL_HOST_NAME "localhost"
  172. #undef BUFSIZZ
  173. #define BUFSIZZ 1024*8
  174. #define S_CLIENT_IRC_READ_TIMEOUT 8
  175. extern int verify_depth;
  176. extern int verify_error;
  177. extern int verify_return_error;
  178. extern int verify_quiet;
  179. static char *prog;
  180. static int async = 0;
  181. static int c_nbio = 0;
  182. static int c_tlsextdebug = 0;
  183. static int c_status_req = 0;
  184. static int c_debug = 0;
  185. static int c_msg = 0;
  186. static int c_showcerts = 0;
  187. static char *keymatexportlabel = NULL;
  188. static int keymatexportlen = 20;
  189. static BIO *bio_c_out = NULL;
  190. static BIO *bio_c_msg = NULL;
  191. static int c_quiet = 0;
  192. static int c_ign_eof = 0;
  193. static int c_brief = 0;
  194. static void print_stuff(BIO *berr, SSL *con, int full);
  195. static int ocsp_resp_cb(SSL *s, void *arg);
  196. static int saved_errno;
  197. static void save_errno(void)
  198. {
  199. saved_errno = errno;
  200. errno = 0;
  201. }
  202. static int restore_errno(void)
  203. {
  204. int ret = errno;
  205. errno = saved_errno;
  206. return ret;
  207. }
  208. #ifndef OPENSSL_NO_PSK
  209. /* Default PSK identity and key */
  210. static char *psk_identity = "Client_identity";
  211. /*
  212. * char *psk_key=NULL; by default PSK is not used
  213. */
  214. static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *identity,
  215. unsigned int max_identity_len,
  216. unsigned char *psk,
  217. unsigned int max_psk_len)
  218. {
  219. unsigned int psk_len = 0;
  220. int ret;
  221. BIGNUM *bn = NULL;
  222. if (c_debug)
  223. BIO_printf(bio_c_out, "psk_client_cb\n");
  224. if (!hint) {
  225. /* no ServerKeyExchange message */
  226. if (c_debug)
  227. BIO_printf(bio_c_out,
  228. "NULL received PSK identity hint, continuing anyway\n");
  229. } else if (c_debug)
  230. BIO_printf(bio_c_out, "Received PSK identity hint '%s'\n", hint);
  231. /*
  232. * lookup PSK identity and PSK key based on the given identity hint here
  233. */
  234. ret = BIO_snprintf(identity, max_identity_len, "%s", psk_identity);
  235. if (ret < 0 || (unsigned int)ret > max_identity_len)
  236. goto out_err;
  237. if (c_debug)
  238. BIO_printf(bio_c_out, "created identity '%s' len=%d\n", identity,
  239. ret);
  240. ret = BN_hex2bn(&bn, psk_key);
  241. if (!ret) {
  242. BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n",
  243. psk_key);
  244. BN_free(bn);
  245. return 0;
  246. }
  247. if ((unsigned int)BN_num_bytes(bn) > max_psk_len) {
  248. BIO_printf(bio_err,
  249. "psk buffer of callback is too small (%d) for key (%d)\n",
  250. max_psk_len, BN_num_bytes(bn));
  251. BN_free(bn);
  252. return 0;
  253. }
  254. psk_len = BN_bn2bin(bn, psk);
  255. BN_free(bn);
  256. if (psk_len == 0)
  257. goto out_err;
  258. if (c_debug)
  259. BIO_printf(bio_c_out, "created PSK len=%d\n", psk_len);
  260. return psk_len;
  261. out_err:
  262. if (c_debug)
  263. BIO_printf(bio_err, "Error in PSK client callback\n");
  264. return 0;
  265. }
  266. #endif
  267. /* This is a context that we pass to callbacks */
  268. typedef struct tlsextctx_st {
  269. BIO *biodebug;
  270. int ack;
  271. } tlsextctx;
  272. static int ssl_servername_cb(SSL *s, int *ad, void *arg)
  273. {
  274. tlsextctx *p = (tlsextctx *) arg;
  275. const char *hn = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
  276. if (SSL_get_servername_type(s) != -1)
  277. p->ack = !SSL_session_reused(s) && hn != NULL;
  278. else
  279. BIO_printf(bio_err, "Can't use SSL_get_servername\n");
  280. return SSL_TLSEXT_ERR_OK;
  281. }
  282. #ifndef OPENSSL_NO_SRP
  283. /* This is a context that we pass to all callbacks */
  284. typedef struct srp_arg_st {
  285. char *srppassin;
  286. char *srplogin;
  287. int msg; /* copy from c_msg */
  288. int debug; /* copy from c_debug */
  289. int amp; /* allow more groups */
  290. int strength /* minimal size for N */ ;
  291. } SRP_ARG;
  292. # define SRP_NUMBER_ITERATIONS_FOR_PRIME 64
  293. static int srp_Verify_N_and_g(const BIGNUM *N, const BIGNUM *g)
  294. {
  295. BN_CTX *bn_ctx = BN_CTX_new();
  296. BIGNUM *p = BN_new();
  297. BIGNUM *r = BN_new();
  298. int ret =
  299. g != NULL && N != NULL && bn_ctx != NULL && BN_is_odd(N) &&
  300. BN_is_prime_ex(N, SRP_NUMBER_ITERATIONS_FOR_PRIME, bn_ctx, NULL) &&
  301. p != NULL && BN_rshift1(p, N) &&
  302. /* p = (N-1)/2 */
  303. BN_is_prime_ex(p, SRP_NUMBER_ITERATIONS_FOR_PRIME, bn_ctx, NULL) &&
  304. r != NULL &&
  305. /* verify g^((N-1)/2) == -1 (mod N) */
  306. BN_mod_exp(r, g, p, N, bn_ctx) &&
  307. BN_add_word(r, 1) && BN_cmp(r, N) == 0;
  308. BN_free(r);
  309. BN_free(p);
  310. BN_CTX_free(bn_ctx);
  311. return ret;
  312. }
  313. /*-
  314. * This callback is used here for two purposes:
  315. * - extended debugging
  316. * - making some primality tests for unknown groups
  317. * The callback is only called for a non default group.
  318. *
  319. * An application does not need the call back at all if
  320. * only the stanard groups are used. In real life situations,
  321. * client and server already share well known groups,
  322. * thus there is no need to verify them.
  323. * Furthermore, in case that a server actually proposes a group that
  324. * is not one of those defined in RFC 5054, it is more appropriate
  325. * to add the group to a static list and then compare since
  326. * primality tests are rather cpu consuming.
  327. */
  328. static int ssl_srp_verify_param_cb(SSL *s, void *arg)
  329. {
  330. SRP_ARG *srp_arg = (SRP_ARG *)arg;
  331. BIGNUM *N = NULL, *g = NULL;
  332. if (((N = SSL_get_srp_N(s)) == NULL) || ((g = SSL_get_srp_g(s)) == NULL))
  333. return 0;
  334. if (srp_arg->debug || srp_arg->msg || srp_arg->amp == 1) {
  335. BIO_printf(bio_err, "SRP parameters:\n");
  336. BIO_printf(bio_err, "\tN=");
  337. BN_print(bio_err, N);
  338. BIO_printf(bio_err, "\n\tg=");
  339. BN_print(bio_err, g);
  340. BIO_printf(bio_err, "\n");
  341. }
  342. if (SRP_check_known_gN_param(g, N))
  343. return 1;
  344. if (srp_arg->amp == 1) {
  345. if (srp_arg->debug)
  346. BIO_printf(bio_err,
  347. "SRP param N and g are not known params, going to check deeper.\n");
  348. /*
  349. * The srp_moregroups is a real debugging feature. Implementors
  350. * should rather add the value to the known ones. The minimal size
  351. * has already been tested.
  352. */
  353. if (BN_num_bits(g) <= BN_BITS && srp_Verify_N_and_g(N, g))
  354. return 1;
  355. }
  356. BIO_printf(bio_err, "SRP param N and g rejected.\n");
  357. return 0;
  358. }
  359. # define PWD_STRLEN 1024
  360. static char *ssl_give_srp_client_pwd_cb(SSL *s, void *arg)
  361. {
  362. SRP_ARG *srp_arg = (SRP_ARG *)arg;
  363. char *pass = app_malloc(PWD_STRLEN + 1, "SRP password buffer");
  364. PW_CB_DATA cb_tmp;
  365. int l;
  366. cb_tmp.password = (char *)srp_arg->srppassin;
  367. cb_tmp.prompt_info = "SRP user";
  368. if ((l = password_callback(pass, PWD_STRLEN, 0, &cb_tmp)) < 0) {
  369. BIO_printf(bio_err, "Can't read Password\n");
  370. OPENSSL_free(pass);
  371. return NULL;
  372. }
  373. *(pass + l) = '\0';
  374. return pass;
  375. }
  376. #endif
  377. static char *srtp_profiles = NULL;
  378. #ifndef OPENSSL_NO_NEXTPROTONEG
  379. /* This the context that we pass to next_proto_cb */
  380. typedef struct tlsextnextprotoctx_st {
  381. unsigned char *data;
  382. unsigned short len;
  383. int status;
  384. } tlsextnextprotoctx;
  385. static tlsextnextprotoctx next_proto;
  386. static int next_proto_cb(SSL *s, unsigned char **out, unsigned char *outlen,
  387. const unsigned char *in, unsigned int inlen,
  388. void *arg)
  389. {
  390. tlsextnextprotoctx *ctx = arg;
  391. if (!c_quiet) {
  392. /* We can assume that |in| is syntactically valid. */
  393. unsigned i;
  394. BIO_printf(bio_c_out, "Protocols advertised by server: ");
  395. for (i = 0; i < inlen;) {
  396. if (i)
  397. BIO_write(bio_c_out, ", ", 2);
  398. BIO_write(bio_c_out, &in[i + 1], in[i]);
  399. i += in[i] + 1;
  400. }
  401. BIO_write(bio_c_out, "\n", 1);
  402. }
  403. ctx->status =
  404. SSL_select_next_proto(out, outlen, in, inlen, ctx->data, ctx->len);
  405. return SSL_TLSEXT_ERR_OK;
  406. }
  407. #endif /* ndef OPENSSL_NO_NEXTPROTONEG */
  408. static int serverinfo_cli_parse_cb(SSL *s, unsigned int ext_type,
  409. const unsigned char *in, size_t inlen,
  410. int *al, void *arg)
  411. {
  412. char pem_name[100];
  413. unsigned char ext_buf[4 + 65536];
  414. /* Reconstruct the type/len fields prior to extension data */
  415. ext_buf[0] = ext_type >> 8;
  416. ext_buf[1] = ext_type & 0xFF;
  417. ext_buf[2] = inlen >> 8;
  418. ext_buf[3] = inlen & 0xFF;
  419. memcpy(ext_buf + 4, in, inlen);
  420. BIO_snprintf(pem_name, sizeof(pem_name), "SERVERINFO FOR EXTENSION %d",
  421. ext_type);
  422. PEM_write_bio(bio_c_out, pem_name, "", ext_buf, 4 + inlen);
  423. return 1;
  424. }
  425. /*
  426. * Hex decoder that tolerates optional whitespace. Returns number of bytes
  427. * produced, advances inptr to end of input string.
  428. */
  429. static ossl_ssize_t hexdecode(const char **inptr, void *result)
  430. {
  431. unsigned char **out = (unsigned char **)result;
  432. const char *in = *inptr;
  433. unsigned char *ret = OPENSSL_malloc(strlen(in)/2);
  434. unsigned char *cp = ret;
  435. uint8_t byte;
  436. int nibble = 0;
  437. if (ret == NULL)
  438. return -1;
  439. for (byte = 0; *in; ++in) {
  440. char c;
  441. if (isspace(*in))
  442. continue;
  443. c = tolower(*in);
  444. if ('0' <= c && c <= '9') {
  445. byte |= c - '0';
  446. } else if ('a' <= c && c <= 'f') {
  447. byte |= c - 'a' + 10;
  448. } else {
  449. OPENSSL_free(ret);
  450. return 0;
  451. }
  452. if ((nibble ^= 1) == 0) {
  453. *cp++ = byte;
  454. byte = 0;
  455. } else {
  456. byte <<= 4;
  457. }
  458. }
  459. if (nibble != 0) {
  460. OPENSSL_free(ret);
  461. return 0;
  462. }
  463. *inptr = in;
  464. return cp - (*out = ret);
  465. }
  466. /*
  467. * Decode unsigned 0..255, returns 1 on success, <= 0 on failure. Advances
  468. * inptr to next field skipping leading whitespace.
  469. */
  470. static ossl_ssize_t checked_uint8(const char **inptr, void *out)
  471. {
  472. uint8_t *result = (uint8_t *)out;
  473. const char *in = *inptr;
  474. char *endp;
  475. long v;
  476. int e;
  477. save_errno();
  478. v = strtol(in, &endp, 10);
  479. e = restore_errno();
  480. if (((v == LONG_MIN || v == LONG_MAX) && e == ERANGE) ||
  481. endp == in || !isspace(*endp) ||
  482. v != (*result = (uint8_t) v)) {
  483. return -1;
  484. }
  485. for (in = endp; isspace(*in); ++in)
  486. continue;
  487. *inptr = in;
  488. return 1;
  489. }
  490. struct tlsa_field {
  491. void *var;
  492. const char *name;
  493. ossl_ssize_t (*parser)(const char **, void *);
  494. };
  495. static int tlsa_import_rr(SSL *con, const char *rrdata)
  496. {
  497. /* Not necessary to re-init these values; the "parsers" do that. */
  498. static uint8_t usage;
  499. static uint8_t selector;
  500. static uint8_t mtype;
  501. static unsigned char *data;
  502. static struct tlsa_field tlsa_fields[] = {
  503. { &usage, "usage", checked_uint8 },
  504. { &selector, "selector", checked_uint8 },
  505. { &mtype, "mtype", checked_uint8 },
  506. { &data, "data", hexdecode },
  507. { NULL, }
  508. };
  509. struct tlsa_field *f;
  510. int ret;
  511. const char *cp = rrdata;
  512. ossl_ssize_t len = 0;
  513. for (f = tlsa_fields; f->var; ++f) {
  514. /* Returns number of bytes produced, advances cp to next field */
  515. if ((len = f->parser(&cp, f->var)) <= 0) {
  516. BIO_printf(bio_err, "%s: warning: bad TLSA %s field in: %s\n",
  517. prog, f->name, rrdata);
  518. return 0;
  519. }
  520. }
  521. /* The data field is last, so len is its length */
  522. ret = SSL_dane_tlsa_add(con, usage, selector, mtype, data, len);
  523. OPENSSL_free(data);
  524. if (ret == 0) {
  525. ERR_print_errors(bio_err);
  526. BIO_printf(bio_err, "%s: warning: unusable TLSA rrdata: %s\n",
  527. prog, rrdata);
  528. return 0;
  529. }
  530. if (ret < 0) {
  531. ERR_print_errors(bio_err);
  532. BIO_printf(bio_err, "%s: warning: error loading TLSA rrdata: %s\n",
  533. prog, rrdata);
  534. return 0;
  535. }
  536. return ret;
  537. }
  538. static int tlsa_import_rrset(SSL *con, STACK_OF(OPENSSL_STRING) *rrset)
  539. {
  540. int num = sk_OPENSSL_STRING_num(rrset);
  541. int count = 0;
  542. int i;
  543. for (i = 0; i < num; ++i) {
  544. char *rrdata = sk_OPENSSL_STRING_value(rrset, i);
  545. if (tlsa_import_rr(con, rrdata) > 0)
  546. ++count;
  547. }
  548. return count > 0;
  549. }
  550. typedef enum OPTION_choice {
  551. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  552. OPT_HOST, OPT_PORT, OPT_CONNECT, OPT_UNIX, OPT_XMPPHOST, OPT_VERIFY,
  553. OPT_CERT, OPT_CRL, OPT_CRL_DOWNLOAD, OPT_SESS_OUT, OPT_SESS_IN,
  554. OPT_CERTFORM, OPT_CRLFORM, OPT_VERIFY_RET_ERROR, OPT_VERIFY_QUIET,
  555. OPT_BRIEF, OPT_PREXIT, OPT_CRLF, OPT_QUIET, OPT_NBIO,
  556. OPT_SSL_CLIENT_ENGINE, OPT_RAND, OPT_IGN_EOF, OPT_NO_IGN_EOF,
  557. OPT_DEBUG, OPT_TLSEXTDEBUG, OPT_STATUS, OPT_WDEBUG,
  558. OPT_MSG, OPT_MSGFILE, OPT_ENGINE, OPT_TRACE, OPT_SECURITY_DEBUG,
  559. OPT_SECURITY_DEBUG_VERBOSE, OPT_SHOWCERTS, OPT_NBIO_TEST, OPT_STATE,
  560. OPT_PSK_IDENTITY, OPT_PSK, OPT_SRPUSER, OPT_SRPPASS, OPT_SRP_STRENGTH,
  561. OPT_SRP_LATEUSER, OPT_SRP_MOREGROUPS, OPT_SSL3, OPT_SSL_CONFIG,
  562. OPT_TLS1_2, OPT_TLS1_1, OPT_TLS1, OPT_DTLS, OPT_DTLS1,
  563. OPT_DTLS1_2, OPT_TIMEOUT, OPT_MTU, OPT_KEYFORM, OPT_PASS,
  564. OPT_CERT_CHAIN, OPT_CAPATH, OPT_NOCAPATH, OPT_CHAINCAPATH, OPT_VERIFYCAPATH,
  565. OPT_KEY, OPT_RECONNECT, OPT_BUILD_CHAIN, OPT_CAFILE, OPT_NOCAFILE,
  566. OPT_CHAINCAFILE, OPT_VERIFYCAFILE, OPT_NEXTPROTONEG, OPT_ALPN,
  567. OPT_SERVERINFO, OPT_STARTTLS, OPT_SERVERNAME, OPT_JPAKE,
  568. OPT_USE_SRTP, OPT_KEYMATEXPORT, OPT_KEYMATEXPORTLEN, OPT_SMTPHOST,
  569. OPT_ASYNC,
  570. OPT_V_ENUM,
  571. OPT_X_ENUM,
  572. OPT_S_ENUM,
  573. OPT_FALLBACKSCSV, OPT_NOCMDS, OPT_PROXY, OPT_DANE_TLSA_DOMAIN,
  574. OPT_DANE_TLSA_RRDATA
  575. } OPTION_CHOICE;
  576. OPTIONS s_client_options[] = {
  577. {"help", OPT_HELP, '-', "Display this summary"},
  578. {"host", OPT_HOST, 's', "Use -connect instead"},
  579. {"port", OPT_PORT, 'p', "Use -connect instead"},
  580. {"connect", OPT_CONNECT, 's',
  581. "TCP/IP where to connect (default is " SSL_HOST_NAME ":" PORT_STR ")"},
  582. {"proxy", OPT_PROXY, 's',
  583. "Connect to via specified proxy to the real server"},
  584. {"unix", OPT_UNIX, 's', "Connect over unix domain sockets"},
  585. {"verify", OPT_VERIFY, 'p', "Turn on peer certificate verification"},
  586. {"cert", OPT_CERT, '<', "Certificate file to use, PEM format assumed"},
  587. {"certform", OPT_CERTFORM, 'F',
  588. "Certificate format (PEM or DER) PEM default"},
  589. {"key", OPT_KEY, '<', "Private key file to use, if not in -cert file"},
  590. {"keyform", OPT_KEYFORM, 'F', "Key format (PEM or DER) PEM default"},
  591. {"pass", OPT_PASS, 's', "Private key file pass phrase source"},
  592. {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
  593. {"CAfile", OPT_CAFILE, '<', "PEM format file of CA's"},
  594. {"no-CAfile", OPT_NOCAFILE, '-',
  595. "Do not load the default certificates file"},
  596. {"no-CApath", OPT_NOCAPATH, '-',
  597. "Do not load certificates from the default certificates directory"},
  598. {"dane_tlsa_domain", OPT_DANE_TLSA_DOMAIN, 's', "DANE TLSA base domain"},
  599. {"dane_tlsa_rrdata", OPT_DANE_TLSA_RRDATA, 's',
  600. "DANE TLSA rrdata presentation form"},
  601. {"reconnect", OPT_RECONNECT, '-',
  602. "Drop and re-make the connection with the same Session-ID"},
  603. {"showcerts", OPT_SHOWCERTS, '-', "Show all certificates in the chain"},
  604. {"debug", OPT_DEBUG, '-', "Extra output"},
  605. {"msg", OPT_MSG, '-', "Show protocol messages"},
  606. {"msgfile", OPT_MSGFILE, '>'},
  607. {"nbio_test", OPT_NBIO_TEST, '-', "More ssl protocol testing"},
  608. {"state", OPT_STATE, '-', "Print the ssl states"},
  609. {"crlf", OPT_CRLF, '-', "Convert LF from terminal into CRLF"},
  610. {"quiet", OPT_QUIET, '-', "No s_client output"},
  611. {"ign_eof", OPT_IGN_EOF, '-', "Ignore input eof (default when -quiet)"},
  612. {"no_ign_eof", OPT_NO_IGN_EOF, '-', "Don't ignore input eof"},
  613. {"starttls", OPT_STARTTLS, 's',
  614. "Use the appropriate STARTTLS command before starting TLS"},
  615. {"xmpphost", OPT_XMPPHOST, 's',
  616. "Host to use with \"-starttls xmpp[-server]\""},
  617. {"rand", OPT_RAND, 's',
  618. "Load the file(s) into the random number generator"},
  619. {"sess_out", OPT_SESS_OUT, '>', "File to write SSL session to"},
  620. {"sess_in", OPT_SESS_IN, '<', "File to read SSL session from"},
  621. {"use_srtp", OPT_USE_SRTP, 's',
  622. "Offer SRTP key management with a colon-separated profile list"},
  623. {"keymatexport", OPT_KEYMATEXPORT, 's',
  624. "Export keying material using label"},
  625. {"keymatexportlen", OPT_KEYMATEXPORTLEN, 'p',
  626. "Export len bytes of keying material (default 20)"},
  627. {"fallback_scsv", OPT_FALLBACKSCSV, '-', "Send the fallback SCSV"},
  628. {"name", OPT_SMTPHOST, 's', "Hostname to use for \"-starttls smtp\""},
  629. {"CRL", OPT_CRL, '<'},
  630. {"crl_download", OPT_CRL_DOWNLOAD, '-'},
  631. {"CRLform", OPT_CRLFORM, 'F'},
  632. {"verify_return_error", OPT_VERIFY_RET_ERROR, '-'},
  633. {"verify_quiet", OPT_VERIFY_QUIET, '-'},
  634. {"brief", OPT_BRIEF, '-'},
  635. {"prexit", OPT_PREXIT, '-'},
  636. {"security_debug", OPT_SECURITY_DEBUG, '-'},
  637. {"security_debug_verbose", OPT_SECURITY_DEBUG_VERBOSE, '-'},
  638. {"cert_chain", OPT_CERT_CHAIN, '<'},
  639. {"chainCApath", OPT_CHAINCAPATH, '/'},
  640. {"verifyCApath", OPT_VERIFYCAPATH, '/'},
  641. {"build_chain", OPT_BUILD_CHAIN, '-'},
  642. {"chainCAfile", OPT_CHAINCAFILE, '<'},
  643. {"verifyCAfile", OPT_VERIFYCAFILE, '<'},
  644. {"nocommands", OPT_NOCMDS, '-', "Do not use interactive command letters"},
  645. {"servername", OPT_SERVERNAME, 's',
  646. "Set TLS extension servername in ClientHello"},
  647. {"tlsextdebug", OPT_TLSEXTDEBUG, '-',
  648. "Hex dump of all TLS extensions received"},
  649. {"status", OPT_STATUS, '-', "Request certificate status from server"},
  650. {"serverinfo", OPT_SERVERINFO, 's',
  651. "types Send empty ClientHello extensions (comma-separated numbers)"},
  652. {"alpn", OPT_ALPN, 's',
  653. "Enable ALPN extension, considering named protocols supported (comma-separated list)"},
  654. {"async", OPT_ASYNC, '-', "Support asynchronous operation"},
  655. {"ssl_config", OPT_SSL_CONFIG, 's'},
  656. OPT_S_OPTIONS,
  657. OPT_V_OPTIONS,
  658. OPT_X_OPTIONS,
  659. #ifndef OPENSSL_NO_SSL3
  660. {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
  661. #endif
  662. #ifndef OPENSSL_NO_TLS1
  663. {"tls1", OPT_TLS1, '-', "Just use TLSv1"},
  664. #endif
  665. #ifndef OPENSSL_NO_TLS1_1
  666. {"tls1_1", OPT_TLS1_1, '-', "Just use TLSv1.1"},
  667. #endif
  668. #ifndef OPENSSL_NO_TLS1_2
  669. {"tls1_2", OPT_TLS1_2, '-', "Just use TLSv1.2"},
  670. #endif
  671. #ifndef OPENSSL_NO_DTLS
  672. {"dtls", OPT_DTLS, '-'},
  673. {"timeout", OPT_TIMEOUT, '-'},
  674. {"mtu", OPT_MTU, 'p', "Set the link layer MTU"},
  675. #endif
  676. #ifndef OPENSSL_NO_DTLS1
  677. {"dtls1", OPT_DTLS1, '-', "Just use DTLSv1"},
  678. #endif
  679. #ifndef OPENSSL_NO_DTLS1_2
  680. {"dtls1_2", OPT_DTLS1_2, '-'},
  681. #endif
  682. #ifndef OPENSSL_NO_SSL_TRACE
  683. {"trace", OPT_TRACE, '-'},
  684. #endif
  685. #ifdef WATT32
  686. {"wdebug", OPT_WDEBUG, '-', "WATT-32 tcp debugging"},
  687. #endif
  688. #ifdef FIONBIO
  689. {"nbio", OPT_NBIO, '-', "Use non-blocking IO"},
  690. #endif
  691. #ifndef OPENSSL_NO_PSK
  692. {"psk_identity", OPT_PSK_IDENTITY, 's', "PSK identity"},
  693. {"psk", OPT_PSK, 's', "PSK in hex (without 0x)"},
  694. # ifndef OPENSSL_NO_JPAKE
  695. {"jpake", OPT_JPAKE, 's', "JPAKE secret to use"},
  696. # endif
  697. #endif
  698. #ifndef OPENSSL_NO_SRP
  699. {"srpuser", OPT_SRPUSER, 's', "SRP authentification for 'user'"},
  700. {"srppass", OPT_SRPPASS, 's', "Password for 'user'"},
  701. {"srp_lateuser", OPT_SRP_LATEUSER, '-',
  702. "SRP username into second ClientHello message"},
  703. {"srp_moregroups", OPT_SRP_MOREGROUPS, '-',
  704. "Tolerate other than the known g N values."},
  705. {"srp_strength", OPT_SRP_STRENGTH, 'p', "Minimal length in bits for N"},
  706. #endif
  707. #ifndef OPENSSL_NO_NEXTPROTONEG
  708. {"nextprotoneg", OPT_NEXTPROTONEG, 's',
  709. "Enable NPN extension, considering named protocols supported (comma-separated list)"},
  710. #endif
  711. #ifndef OPENSSL_NO_ENGINE
  712. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  713. {"ssl_client_engine", OPT_SSL_CLIENT_ENGINE, 's'},
  714. #endif
  715. {NULL}
  716. };
  717. typedef enum PROTOCOL_choice {
  718. PROTO_OFF,
  719. PROTO_SMTP,
  720. PROTO_POP3,
  721. PROTO_IMAP,
  722. PROTO_FTP,
  723. PROTO_TELNET,
  724. PROTO_XMPP,
  725. PROTO_XMPP_SERVER,
  726. PROTO_CONNECT,
  727. PROTO_IRC
  728. } PROTOCOL_CHOICE;
  729. static OPT_PAIR services[] = {
  730. {"smtp", PROTO_SMTP},
  731. {"pop3", PROTO_POP3},
  732. {"imap", PROTO_IMAP},
  733. {"ftp", PROTO_FTP},
  734. {"xmpp", PROTO_XMPP},
  735. {"xmpp-server", PROTO_XMPP_SERVER},
  736. {"telnet", PROTO_TELNET},
  737. {"irc", PROTO_IRC},
  738. {NULL}
  739. };
  740. int s_client_main(int argc, char **argv)
  741. {
  742. BIO *sbio;
  743. EVP_PKEY *key = NULL;
  744. SSL *con = NULL;
  745. SSL_CTX *ctx = NULL;
  746. STACK_OF(X509) *chain = NULL;
  747. X509 *cert = NULL;
  748. X509_VERIFY_PARAM *vpm = NULL;
  749. SSL_EXCERT *exc = NULL;
  750. SSL_CONF_CTX *cctx = NULL;
  751. STACK_OF(OPENSSL_STRING) *ssl_args = NULL;
  752. char *dane_tlsa_domain = NULL;
  753. STACK_OF(OPENSSL_STRING) *dane_tlsa_rrset = NULL;
  754. STACK_OF(X509_CRL) *crls = NULL;
  755. const SSL_METHOD *meth = TLS_client_method();
  756. char *CApath = NULL, *CAfile = NULL, *cbuf = NULL, *sbuf = NULL;
  757. char *mbuf = NULL, *proxystr = NULL, *connectstr = NULL;
  758. char *cert_file = NULL, *key_file = NULL, *chain_file = NULL;
  759. char *chCApath = NULL, *chCAfile = NULL, *host = SSL_HOST_NAME;
  760. char *inrand = NULL;
  761. char *passarg = NULL, *pass = NULL, *vfyCApath = NULL, *vfyCAfile = NULL;
  762. char *sess_in = NULL, *sess_out = NULL, *crl_file = NULL, *p;
  763. char *jpake_secret = NULL, *xmpphost = NULL;
  764. const char *unix_path = NULL;
  765. const char *ehlo = "mail.example.com";
  766. struct sockaddr peer;
  767. struct timeval timeout, *timeoutp;
  768. fd_set readfds, writefds;
  769. int noCApath = 0, noCAfile = 0;
  770. int build_chain = 0, cbuf_len, cbuf_off, cert_format = FORMAT_PEM;
  771. int key_format = FORMAT_PEM, crlf = 0, full_log = 1, mbuf_len = 0;
  772. int prexit = 0;
  773. int enable_timeouts = 0, sdebug = 0, peerlen = sizeof peer;
  774. int reconnect = 0, verify = SSL_VERIFY_NONE, vpmtouched = 0;
  775. int ret = 1, in_init = 1, i, nbio_test = 0, s = -1, k, width, state = 0;
  776. int sbuf_len, sbuf_off, socket_type = SOCK_STREAM, cmdletters = 1;
  777. int starttls_proto = PROTO_OFF, crl_format = FORMAT_PEM, crl_download = 0;
  778. int write_tty, read_tty, write_ssl, read_ssl, tty_on, ssl_pending;
  779. int fallback_scsv = 0;
  780. long socket_mtu = 0, randamt = 0;
  781. unsigned short port = PORT;
  782. OPTION_CHOICE o;
  783. #ifndef OPENSSL_NO_ENGINE
  784. ENGINE *ssl_client_engine = NULL;
  785. #endif
  786. ENGINE *e = NULL;
  787. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)
  788. struct timeval tv;
  789. #endif
  790. char *servername = NULL;
  791. const char *alpn_in = NULL;
  792. tlsextctx tlsextcbp = { NULL, 0 };
  793. const char *ssl_config = NULL;
  794. #define MAX_SI_TYPES 100
  795. unsigned short serverinfo_types[MAX_SI_TYPES];
  796. int serverinfo_count = 0, start = 0, len;
  797. #ifndef OPENSSL_NO_NEXTPROTONEG
  798. const char *next_proto_neg_in = NULL;
  799. #endif
  800. #ifndef OPENSSL_NO_SRP
  801. char *srppass = NULL;
  802. int srp_lateuser = 0;
  803. SRP_ARG srp_arg = { NULL, NULL, 0, 0, 0, 1024 };
  804. #endif
  805. prog = opt_progname(argv[0]);
  806. c_quiet = 0;
  807. c_ign_eof = 0;
  808. c_debug = 0;
  809. c_msg = 0;
  810. c_showcerts = 0;
  811. c_nbio = 0;
  812. verify_depth = 0;
  813. verify_error = X509_V_OK;
  814. vpm = X509_VERIFY_PARAM_new();
  815. cbuf = app_malloc(BUFSIZZ, "cbuf");
  816. sbuf = app_malloc(BUFSIZZ, "sbuf");
  817. mbuf = app_malloc(BUFSIZZ, "mbuf");
  818. cctx = SSL_CONF_CTX_new();
  819. if (vpm == NULL || cctx == NULL) {
  820. BIO_printf(bio_err, "%s: out of memory\n", prog);
  821. goto end;
  822. }
  823. SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT | SSL_CONF_FLAG_CMDLINE);
  824. prog = opt_init(argc, argv, s_client_options);
  825. while ((o = opt_next()) != OPT_EOF) {
  826. switch (o) {
  827. case OPT_EOF:
  828. case OPT_ERR:
  829. opthelp:
  830. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  831. goto end;
  832. case OPT_HELP:
  833. opt_help(s_client_options);
  834. ret = 0;
  835. goto end;
  836. case OPT_HOST:
  837. host = opt_arg();
  838. break;
  839. case OPT_PORT:
  840. port = atoi(opt_arg());
  841. break;
  842. case OPT_CONNECT:
  843. connectstr = opt_arg();
  844. break;
  845. case OPT_PROXY:
  846. proxystr = opt_arg();
  847. starttls_proto = PROTO_CONNECT;
  848. break;
  849. case OPT_UNIX:
  850. unix_path = opt_arg();
  851. break;
  852. case OPT_XMPPHOST:
  853. xmpphost = opt_arg();
  854. break;
  855. case OPT_SMTPHOST:
  856. ehlo = opt_arg();
  857. break;
  858. case OPT_VERIFY:
  859. verify = SSL_VERIFY_PEER;
  860. verify_depth = atoi(opt_arg());
  861. if (!c_quiet)
  862. BIO_printf(bio_err, "verify depth is %d\n", verify_depth);
  863. break;
  864. case OPT_CERT:
  865. cert_file = opt_arg();
  866. break;
  867. case OPT_CRL:
  868. crl_file = opt_arg();
  869. break;
  870. case OPT_CRL_DOWNLOAD:
  871. crl_download = 1;
  872. break;
  873. case OPT_SESS_OUT:
  874. sess_out = opt_arg();
  875. break;
  876. case OPT_SESS_IN:
  877. sess_in = opt_arg();
  878. break;
  879. case OPT_CERTFORM:
  880. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &cert_format))
  881. goto opthelp;
  882. break;
  883. case OPT_CRLFORM:
  884. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &crl_format))
  885. goto opthelp;
  886. break;
  887. case OPT_VERIFY_RET_ERROR:
  888. verify_return_error = 1;
  889. break;
  890. case OPT_VERIFY_QUIET:
  891. verify_quiet = 1;
  892. break;
  893. case OPT_BRIEF:
  894. c_brief = verify_quiet = c_quiet = 1;
  895. break;
  896. case OPT_S_CASES:
  897. if (ssl_args == NULL)
  898. ssl_args = sk_OPENSSL_STRING_new_null();
  899. if (ssl_args == NULL
  900. || !sk_OPENSSL_STRING_push(ssl_args, opt_flag())
  901. || !sk_OPENSSL_STRING_push(ssl_args, opt_arg())) {
  902. BIO_printf(bio_err, "%s: Memory allocation failure\n", prog);
  903. goto end;
  904. }
  905. break;
  906. case OPT_V_CASES:
  907. if (!opt_verify(o, vpm))
  908. goto end;
  909. vpmtouched++;
  910. break;
  911. case OPT_X_CASES:
  912. if (!args_excert(o, &exc))
  913. goto end;
  914. break;
  915. case OPT_PREXIT:
  916. prexit = 1;
  917. break;
  918. case OPT_CRLF:
  919. crlf = 1;
  920. break;
  921. case OPT_QUIET:
  922. c_quiet = c_ign_eof = 1;
  923. break;
  924. case OPT_NBIO:
  925. c_nbio = 1;
  926. break;
  927. case OPT_NOCMDS:
  928. cmdletters = 0;
  929. break;
  930. case OPT_ENGINE:
  931. e = setup_engine(opt_arg(), 1);
  932. break;
  933. case OPT_SSL_CLIENT_ENGINE:
  934. #ifndef OPENSSL_NO_ENGINE
  935. ssl_client_engine = ENGINE_by_id(opt_arg());
  936. if (ssl_client_engine == NULL) {
  937. BIO_printf(bio_err, "Error getting client auth engine\n");
  938. goto opthelp;
  939. }
  940. break;
  941. #endif
  942. break;
  943. case OPT_RAND:
  944. inrand = opt_arg();
  945. break;
  946. case OPT_IGN_EOF:
  947. c_ign_eof = 1;
  948. break;
  949. case OPT_NO_IGN_EOF:
  950. c_ign_eof = 0;
  951. break;
  952. case OPT_DEBUG:
  953. c_debug = 1;
  954. break;
  955. case OPT_TLSEXTDEBUG:
  956. c_tlsextdebug = 1;
  957. break;
  958. case OPT_STATUS:
  959. c_status_req = 1;
  960. break;
  961. case OPT_WDEBUG:
  962. #ifdef WATT32
  963. dbug_init();
  964. #endif
  965. break;
  966. case OPT_MSG:
  967. c_msg = 1;
  968. break;
  969. case OPT_MSGFILE:
  970. bio_c_msg = BIO_new_file(opt_arg(), "w");
  971. break;
  972. case OPT_TRACE:
  973. #ifndef OPENSSL_NO_SSL_TRACE
  974. c_msg = 2;
  975. #endif
  976. break;
  977. case OPT_SECURITY_DEBUG:
  978. sdebug = 1;
  979. break;
  980. case OPT_SECURITY_DEBUG_VERBOSE:
  981. sdebug = 2;
  982. break;
  983. case OPT_SHOWCERTS:
  984. c_showcerts = 1;
  985. break;
  986. case OPT_NBIO_TEST:
  987. nbio_test = 1;
  988. break;
  989. case OPT_STATE:
  990. state = 1;
  991. break;
  992. #ifndef OPENSSL_NO_PSK
  993. case OPT_PSK_IDENTITY:
  994. psk_identity = opt_arg();
  995. break;
  996. case OPT_PSK:
  997. for (p = psk_key = opt_arg(); *p; p++) {
  998. if (isxdigit(*p))
  999. continue;
  1000. BIO_printf(bio_err, "Not a hex number '%s'\n", psk_key);
  1001. goto end;
  1002. }
  1003. break;
  1004. #else
  1005. case OPT_PSK_IDENTITY:
  1006. case OPT_PSK:
  1007. break;
  1008. #endif
  1009. #ifndef OPENSSL_NO_SRP
  1010. case OPT_SRPUSER:
  1011. srp_arg.srplogin = opt_arg();
  1012. meth = TLSv1_client_method();
  1013. break;
  1014. case OPT_SRPPASS:
  1015. srppass = opt_arg();
  1016. meth = TLSv1_client_method();
  1017. break;
  1018. case OPT_SRP_STRENGTH:
  1019. srp_arg.strength = atoi(opt_arg());
  1020. BIO_printf(bio_err, "SRP minimal length for N is %d\n",
  1021. srp_arg.strength);
  1022. meth = TLSv1_client_method();
  1023. break;
  1024. case OPT_SRP_LATEUSER:
  1025. srp_lateuser = 1;
  1026. meth = TLSv1_client_method();
  1027. break;
  1028. case OPT_SRP_MOREGROUPS:
  1029. srp_arg.amp = 1;
  1030. meth = TLSv1_client_method();
  1031. break;
  1032. #else
  1033. case OPT_SRPUSER:
  1034. case OPT_SRPPASS:
  1035. case OPT_SRP_STRENGTH:
  1036. case OPT_SRP_LATEUSER:
  1037. case OPT_SRP_MOREGROUPS:
  1038. break;
  1039. #endif
  1040. case OPT_SSL_CONFIG:
  1041. ssl_config = opt_arg();
  1042. break;
  1043. case OPT_SSL3:
  1044. #ifndef OPENSSL_NO_SSL3
  1045. meth = SSLv3_client_method();
  1046. #endif
  1047. break;
  1048. case OPT_TLS1_2:
  1049. #ifndef OPENSSL_NO_TLS1_2
  1050. meth = TLSv1_2_client_method();
  1051. #endif
  1052. break;
  1053. case OPT_TLS1_1:
  1054. #ifndef OPENSSL_NO_TLS1_1
  1055. meth = TLSv1_1_client_method();
  1056. #endif
  1057. break;
  1058. case OPT_TLS1:
  1059. #ifndef OPENSSL_NO_TLS1
  1060. meth = TLSv1_client_method();
  1061. #endif
  1062. break;
  1063. case OPT_DTLS:
  1064. #ifndef OPENSSL_NO_DTLS
  1065. meth = DTLS_client_method();
  1066. socket_type = SOCK_DGRAM;
  1067. #endif
  1068. break;
  1069. case OPT_DTLS1:
  1070. #ifndef OPENSSL_NO_DTLS1
  1071. meth = DTLSv1_client_method();
  1072. socket_type = SOCK_DGRAM;
  1073. #endif
  1074. break;
  1075. case OPT_DTLS1_2:
  1076. #ifndef OPENSSL_NO_DTLS1_2
  1077. meth = DTLSv1_2_client_method();
  1078. socket_type = SOCK_DGRAM;
  1079. #endif
  1080. break;
  1081. case OPT_TIMEOUT:
  1082. #ifndef OPENSSL_NO_DTLS
  1083. enable_timeouts = 1;
  1084. #endif
  1085. break;
  1086. case OPT_MTU:
  1087. #ifndef OPENSSL_NO_DTLS
  1088. socket_mtu = atol(opt_arg());
  1089. #endif
  1090. break;
  1091. case OPT_FALLBACKSCSV:
  1092. fallback_scsv = 1;
  1093. break;
  1094. case OPT_KEYFORM:
  1095. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &key_format))
  1096. goto opthelp;
  1097. break;
  1098. case OPT_PASS:
  1099. passarg = opt_arg();
  1100. break;
  1101. case OPT_CERT_CHAIN:
  1102. chain_file = opt_arg();
  1103. break;
  1104. case OPT_KEY:
  1105. key_file = opt_arg();
  1106. break;
  1107. case OPT_RECONNECT:
  1108. reconnect = 5;
  1109. break;
  1110. case OPT_CAPATH:
  1111. CApath = opt_arg();
  1112. break;
  1113. case OPT_NOCAPATH:
  1114. noCApath = 1;
  1115. break;
  1116. case OPT_CHAINCAPATH:
  1117. chCApath = opt_arg();
  1118. break;
  1119. case OPT_VERIFYCAPATH:
  1120. vfyCApath = opt_arg();
  1121. break;
  1122. case OPT_BUILD_CHAIN:
  1123. build_chain = 1;
  1124. break;
  1125. case OPT_CAFILE:
  1126. CAfile = opt_arg();
  1127. break;
  1128. case OPT_NOCAFILE:
  1129. noCAfile = 1;
  1130. break;
  1131. case OPT_CHAINCAFILE:
  1132. chCAfile = opt_arg();
  1133. break;
  1134. case OPT_VERIFYCAFILE:
  1135. vfyCAfile = opt_arg();
  1136. break;
  1137. case OPT_DANE_TLSA_DOMAIN:
  1138. dane_tlsa_domain = opt_arg();
  1139. break;
  1140. case OPT_DANE_TLSA_RRDATA:
  1141. if (dane_tlsa_rrset == NULL)
  1142. dane_tlsa_rrset = sk_OPENSSL_STRING_new_null();
  1143. if (dane_tlsa_rrset == NULL ||
  1144. !sk_OPENSSL_STRING_push(dane_tlsa_rrset, opt_arg())) {
  1145. BIO_printf(bio_err, "%s: Memory allocation failure\n", prog);
  1146. goto end;
  1147. }
  1148. break;
  1149. case OPT_NEXTPROTONEG:
  1150. next_proto_neg_in = opt_arg();
  1151. break;
  1152. case OPT_ALPN:
  1153. alpn_in = opt_arg();
  1154. break;
  1155. case OPT_SERVERINFO:
  1156. p = opt_arg();
  1157. len = strlen(p);
  1158. for (start = 0, i = 0; i <= len; ++i) {
  1159. if (i == len || p[i] == ',') {
  1160. serverinfo_types[serverinfo_count] = atoi(p + start);
  1161. if (++serverinfo_count == MAX_SI_TYPES)
  1162. break;
  1163. start = i + 1;
  1164. }
  1165. }
  1166. break;
  1167. case OPT_STARTTLS:
  1168. if (!opt_pair(opt_arg(), services, &starttls_proto))
  1169. goto end;
  1170. case OPT_SERVERNAME:
  1171. servername = opt_arg();
  1172. break;
  1173. case OPT_JPAKE:
  1174. #ifndef OPENSSL_NO_JPAKE
  1175. jpake_secret = opt_arg();
  1176. #endif
  1177. break;
  1178. case OPT_USE_SRTP:
  1179. srtp_profiles = opt_arg();
  1180. break;
  1181. case OPT_KEYMATEXPORT:
  1182. keymatexportlabel = opt_arg();
  1183. break;
  1184. case OPT_KEYMATEXPORTLEN:
  1185. keymatexportlen = atoi(opt_arg());
  1186. break;
  1187. case OPT_ASYNC:
  1188. async = 1;
  1189. break;
  1190. }
  1191. }
  1192. argc = opt_num_rest();
  1193. argv = opt_rest();
  1194. if (proxystr) {
  1195. if (connectstr == NULL) {
  1196. BIO_printf(bio_err, "%s: -proxy requires use of -connect\n", prog);
  1197. goto opthelp;
  1198. }
  1199. if (!extract_host_port(proxystr, &host, NULL, &port))
  1200. goto end;
  1201. }
  1202. else if (connectstr != NULL
  1203. && !extract_host_port(connectstr, &host, NULL, &port))
  1204. goto end;
  1205. if (unix_path && (socket_type != SOCK_STREAM)) {
  1206. BIO_printf(bio_err,
  1207. "Can't use unix sockets and datagrams together\n");
  1208. goto end;
  1209. }
  1210. #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
  1211. if (jpake_secret) {
  1212. if (psk_key) {
  1213. BIO_printf(bio_err, "Can't use JPAKE and PSK together\n");
  1214. goto end;
  1215. }
  1216. psk_identity = "JPAKE";
  1217. }
  1218. #endif
  1219. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  1220. next_proto.status = -1;
  1221. if (next_proto_neg_in) {
  1222. next_proto.data =
  1223. next_protos_parse(&next_proto.len, next_proto_neg_in);
  1224. if (next_proto.data == NULL) {
  1225. BIO_printf(bio_err, "Error parsing -nextprotoneg argument\n");
  1226. goto end;
  1227. }
  1228. } else
  1229. next_proto.data = NULL;
  1230. #endif
  1231. if (!app_passwd(passarg, NULL, &pass, NULL)) {
  1232. BIO_printf(bio_err, "Error getting password\n");
  1233. goto end;
  1234. }
  1235. if (key_file == NULL)
  1236. key_file = cert_file;
  1237. if (key_file) {
  1238. key = load_key(key_file, key_format, 0, pass, e,
  1239. "client certificate private key file");
  1240. if (key == NULL) {
  1241. ERR_print_errors(bio_err);
  1242. goto end;
  1243. }
  1244. }
  1245. if (cert_file) {
  1246. cert = load_cert(cert_file, cert_format,
  1247. NULL, e, "client certificate file");
  1248. if (cert == NULL) {
  1249. ERR_print_errors(bio_err);
  1250. goto end;
  1251. }
  1252. }
  1253. if (chain_file) {
  1254. chain = load_certs(chain_file, FORMAT_PEM,
  1255. NULL, e, "client certificate chain");
  1256. if (!chain)
  1257. goto end;
  1258. }
  1259. if (crl_file) {
  1260. X509_CRL *crl;
  1261. crl = load_crl(crl_file, crl_format);
  1262. if (crl == NULL) {
  1263. BIO_puts(bio_err, "Error loading CRL\n");
  1264. ERR_print_errors(bio_err);
  1265. goto end;
  1266. }
  1267. crls = sk_X509_CRL_new_null();
  1268. if (crls == NULL || !sk_X509_CRL_push(crls, crl)) {
  1269. BIO_puts(bio_err, "Error adding CRL\n");
  1270. ERR_print_errors(bio_err);
  1271. X509_CRL_free(crl);
  1272. goto end;
  1273. }
  1274. }
  1275. if (!load_excert(&exc))
  1276. goto end;
  1277. if (!app_RAND_load_file(NULL, 1) && inrand == NULL
  1278. && !RAND_status()) {
  1279. BIO_printf(bio_err,
  1280. "warning, not much extra random data, consider using the -rand option\n");
  1281. }
  1282. if (inrand != NULL) {
  1283. randamt = app_RAND_load_files(inrand);
  1284. BIO_printf(bio_err, "%ld semi-random bytes loaded\n", randamt);
  1285. }
  1286. if (bio_c_out == NULL) {
  1287. if (c_quiet && !c_debug) {
  1288. bio_c_out = BIO_new(BIO_s_null());
  1289. if (c_msg && !bio_c_msg)
  1290. bio_c_msg = dup_bio_out(FORMAT_TEXT);
  1291. } else if (bio_c_out == NULL)
  1292. bio_c_out = dup_bio_out(FORMAT_TEXT);
  1293. }
  1294. #ifndef OPENSSL_NO_SRP
  1295. if (!app_passwd(srppass, NULL, &srp_arg.srppassin, NULL)) {
  1296. BIO_printf(bio_err, "Error getting password\n");
  1297. goto end;
  1298. }
  1299. #endif
  1300. ctx = SSL_CTX_new(meth);
  1301. if (ctx == NULL) {
  1302. ERR_print_errors(bio_err);
  1303. goto end;
  1304. }
  1305. if (sdebug)
  1306. ssl_ctx_security_debug(ctx, sdebug);
  1307. if (ssl_config) {
  1308. if (SSL_CTX_config(ctx, ssl_config) == 0) {
  1309. BIO_printf(bio_err, "Error using configuration \"%s\"\n",
  1310. ssl_config);
  1311. ERR_print_errors(bio_err);
  1312. goto end;
  1313. }
  1314. }
  1315. if (vpmtouched && !SSL_CTX_set1_param(ctx, vpm)) {
  1316. BIO_printf(bio_err, "Error setting verify params\n");
  1317. ERR_print_errors(bio_err);
  1318. goto end;
  1319. }
  1320. if (async) {
  1321. SSL_CTX_set_mode(ctx, SSL_MODE_ASYNC);
  1322. ASYNC_init(1, 0, 0);
  1323. }
  1324. if (!config_ctx(cctx, ssl_args, ctx, jpake_secret == NULL))
  1325. goto end;
  1326. if (!ssl_load_stores(ctx, vfyCApath, vfyCAfile, chCApath, chCAfile,
  1327. crls, crl_download)) {
  1328. BIO_printf(bio_err, "Error loading store locations\n");
  1329. ERR_print_errors(bio_err);
  1330. goto end;
  1331. }
  1332. #ifndef OPENSSL_NO_ENGINE
  1333. if (ssl_client_engine) {
  1334. if (!SSL_CTX_set_client_cert_engine(ctx, ssl_client_engine)) {
  1335. BIO_puts(bio_err, "Error setting client auth engine\n");
  1336. ERR_print_errors(bio_err);
  1337. ENGINE_free(ssl_client_engine);
  1338. goto end;
  1339. }
  1340. ENGINE_free(ssl_client_engine);
  1341. }
  1342. #endif
  1343. #ifndef OPENSSL_NO_PSK
  1344. if (psk_key != NULL || jpake_secret) {
  1345. if (c_debug)
  1346. BIO_printf(bio_c_out,
  1347. "PSK key given or JPAKE in use, setting client callback\n");
  1348. SSL_CTX_set_psk_client_callback(ctx, psk_client_cb);
  1349. }
  1350. #endif
  1351. #ifndef OPENSSL_NO_SRTP
  1352. if (srtp_profiles != NULL) {
  1353. /* Returns 0 on success! */
  1354. if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles) != 0) {
  1355. BIO_printf(bio_err, "Error setting SRTP profile\n");
  1356. ERR_print_errors(bio_err);
  1357. goto end;
  1358. }
  1359. }
  1360. #endif
  1361. if (exc)
  1362. ssl_ctx_set_excert(ctx, exc);
  1363. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  1364. if (next_proto.data)
  1365. SSL_CTX_set_next_proto_select_cb(ctx, next_proto_cb, &next_proto);
  1366. #endif
  1367. if (alpn_in) {
  1368. unsigned short alpn_len;
  1369. unsigned char *alpn = next_protos_parse(&alpn_len, alpn_in);
  1370. if (alpn == NULL) {
  1371. BIO_printf(bio_err, "Error parsing -alpn argument\n");
  1372. goto end;
  1373. }
  1374. /* Returns 0 on success! */
  1375. if (SSL_CTX_set_alpn_protos(ctx, alpn, alpn_len) != 0) {
  1376. BIO_printf(bio_err, "Error setting ALPN\n");
  1377. goto end;
  1378. }
  1379. OPENSSL_free(alpn);
  1380. }
  1381. for (i = 0; i < serverinfo_count; i++) {
  1382. if (!SSL_CTX_add_client_custom_ext(ctx,
  1383. serverinfo_types[i],
  1384. NULL, NULL, NULL,
  1385. serverinfo_cli_parse_cb, NULL)) {
  1386. BIO_printf(bio_err,
  1387. "Warning: Unable to add custom extension %u, skipping\n",
  1388. serverinfo_types[i]);
  1389. }
  1390. }
  1391. if (state)
  1392. SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
  1393. SSL_CTX_set_verify(ctx, verify, verify_callback);
  1394. if (!ctx_set_verify_locations(ctx, CAfile, CApath, noCAfile, noCApath)) {
  1395. ERR_print_errors(bio_err);
  1396. goto end;
  1397. }
  1398. ssl_ctx_add_crls(ctx, crls, crl_download);
  1399. if (!set_cert_key_stuff(ctx, cert, key, chain, build_chain))
  1400. goto end;
  1401. if (servername != NULL) {
  1402. tlsextcbp.biodebug = bio_err;
  1403. SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
  1404. SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
  1405. }
  1406. # ifndef OPENSSL_NO_SRP
  1407. if (srp_arg.srplogin) {
  1408. if (!srp_lateuser && !SSL_CTX_set_srp_username(ctx, srp_arg.srplogin)) {
  1409. BIO_printf(bio_err, "Unable to set SRP username\n");
  1410. goto end;
  1411. }
  1412. srp_arg.msg = c_msg;
  1413. srp_arg.debug = c_debug;
  1414. SSL_CTX_set_srp_cb_arg(ctx, &srp_arg);
  1415. SSL_CTX_set_srp_client_pwd_callback(ctx, ssl_give_srp_client_pwd_cb);
  1416. SSL_CTX_set_srp_strength(ctx, srp_arg.strength);
  1417. if (c_msg || c_debug || srp_arg.amp == 0)
  1418. SSL_CTX_set_srp_verify_param_callback(ctx,
  1419. ssl_srp_verify_param_cb);
  1420. }
  1421. # endif
  1422. if (dane_tlsa_domain != NULL) {
  1423. if (SSL_CTX_dane_enable(ctx) <= 0) {
  1424. BIO_printf(bio_err,
  1425. "%s: Error enabling DANE TLSA authentication.\n", prog);
  1426. ERR_print_errors(bio_err);
  1427. goto end;
  1428. }
  1429. }
  1430. con = SSL_new(ctx);
  1431. if (sess_in) {
  1432. SSL_SESSION *sess;
  1433. BIO *stmp = BIO_new_file(sess_in, "r");
  1434. if (!stmp) {
  1435. BIO_printf(bio_err, "Can't open session file %s\n", sess_in);
  1436. ERR_print_errors(bio_err);
  1437. goto end;
  1438. }
  1439. sess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);
  1440. BIO_free(stmp);
  1441. if (!sess) {
  1442. BIO_printf(bio_err, "Can't open session file %s\n", sess_in);
  1443. ERR_print_errors(bio_err);
  1444. goto end;
  1445. }
  1446. if (!SSL_set_session(con, sess)) {
  1447. BIO_printf(bio_err, "Can't set session\n");
  1448. ERR_print_errors(bio_err);
  1449. goto end;
  1450. }
  1451. SSL_SESSION_free(sess);
  1452. }
  1453. if (fallback_scsv)
  1454. SSL_set_mode(con, SSL_MODE_SEND_FALLBACK_SCSV);
  1455. if (servername != NULL) {
  1456. if (!SSL_set_tlsext_host_name(con, servername)) {
  1457. BIO_printf(bio_err, "Unable to set TLS servername extension.\n");
  1458. ERR_print_errors(bio_err);
  1459. goto end;
  1460. }
  1461. }
  1462. if (dane_tlsa_domain != NULL) {
  1463. if (SSL_dane_enable(con, dane_tlsa_domain) <= 0) {
  1464. BIO_printf(bio_err, "%s: Error enabling DANE TLSA "
  1465. "authentication.\n", prog);
  1466. ERR_print_errors(bio_err);
  1467. goto end;
  1468. }
  1469. if (dane_tlsa_rrset == NULL) {
  1470. BIO_printf(bio_err, "%s: DANE TLSA authentication requires at "
  1471. "least one -dane_tlsa_rrset option.\n", prog);
  1472. goto end;
  1473. }
  1474. if (tlsa_import_rrset(con, dane_tlsa_rrset) <= 0) {
  1475. BIO_printf(bio_err, "%s: Failed to import any TLSA "
  1476. "records.\n", prog);
  1477. goto end;
  1478. }
  1479. } else if (dane_tlsa_rrset != NULL) {
  1480. BIO_printf(bio_err, "%s: DANE TLSA authentication requires the "
  1481. "-dane_tlsa_domain option.\n", prog);
  1482. goto end;
  1483. }
  1484. re_start:
  1485. #ifdef NO_SYS_UN_H
  1486. if (init_client(&s, host, port, socket_type) == 0)
  1487. #else
  1488. if ((!unix_path && (init_client(&s, host, port, socket_type) == 0)) ||
  1489. (unix_path && (init_client_unix(&s, unix_path) == 0)))
  1490. #endif
  1491. {
  1492. BIO_printf(bio_err, "connect:errno=%d\n", get_last_socket_error());
  1493. SHUTDOWN(s);
  1494. goto end;
  1495. }
  1496. BIO_printf(bio_c_out, "CONNECTED(%08X)\n", s);
  1497. #ifdef FIONBIO
  1498. if (c_nbio) {
  1499. unsigned long l = 1;
  1500. BIO_printf(bio_c_out, "turning on non blocking io\n");
  1501. if (BIO_socket_ioctl(s, FIONBIO, &l) < 0) {
  1502. ERR_print_errors(bio_err);
  1503. goto end;
  1504. }
  1505. }
  1506. #endif
  1507. if (socket_type == SOCK_DGRAM) {
  1508. sbio = BIO_new_dgram(s, BIO_NOCLOSE);
  1509. if (getsockname(s, &peer, (void *)&peerlen) < 0) {
  1510. BIO_printf(bio_err, "getsockname:errno=%d\n",
  1511. get_last_socket_error());
  1512. SHUTDOWN(s);
  1513. goto end;
  1514. }
  1515. (void)BIO_ctrl_set_connected(sbio, &peer);
  1516. if (enable_timeouts) {
  1517. timeout.tv_sec = 0;
  1518. timeout.tv_usec = DGRAM_RCV_TIMEOUT;
  1519. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
  1520. timeout.tv_sec = 0;
  1521. timeout.tv_usec = DGRAM_SND_TIMEOUT;
  1522. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
  1523. }
  1524. if (socket_mtu) {
  1525. if (socket_mtu < DTLS_get_link_min_mtu(con)) {
  1526. BIO_printf(bio_err, "MTU too small. Must be at least %ld\n",
  1527. DTLS_get_link_min_mtu(con));
  1528. BIO_free(sbio);
  1529. goto shut;
  1530. }
  1531. SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
  1532. if (!DTLS_set_link_mtu(con, socket_mtu)) {
  1533. BIO_printf(bio_err, "Failed to set MTU\n");
  1534. BIO_free(sbio);
  1535. goto shut;
  1536. }
  1537. } else
  1538. /* want to do MTU discovery */
  1539. BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
  1540. } else
  1541. sbio = BIO_new_socket(s, BIO_NOCLOSE);
  1542. if (nbio_test) {
  1543. BIO *test;
  1544. test = BIO_new(BIO_f_nbio_test());
  1545. sbio = BIO_push(test, sbio);
  1546. }
  1547. if (c_debug) {
  1548. BIO_set_callback(sbio, bio_dump_callback);
  1549. BIO_set_callback_arg(sbio, (char *)bio_c_out);
  1550. }
  1551. if (c_msg) {
  1552. #ifndef OPENSSL_NO_SSL_TRACE
  1553. if (c_msg == 2)
  1554. SSL_set_msg_callback(con, SSL_trace);
  1555. else
  1556. #endif
  1557. SSL_set_msg_callback(con, msg_cb);
  1558. SSL_set_msg_callback_arg(con, bio_c_msg ? bio_c_msg : bio_c_out);
  1559. }
  1560. if (c_tlsextdebug) {
  1561. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  1562. SSL_set_tlsext_debug_arg(con, bio_c_out);
  1563. }
  1564. if (c_status_req) {
  1565. SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp);
  1566. SSL_CTX_set_tlsext_status_cb(ctx, ocsp_resp_cb);
  1567. SSL_CTX_set_tlsext_status_arg(ctx, bio_c_out);
  1568. }
  1569. #ifndef OPENSSL_NO_JPAKE
  1570. if (jpake_secret)
  1571. jpake_client_auth(bio_c_out, sbio, jpake_secret);
  1572. #endif
  1573. SSL_set_bio(con, sbio, sbio);
  1574. SSL_set_connect_state(con);
  1575. /* ok, lets connect */
  1576. width = SSL_get_fd(con) + 1;
  1577. read_tty = 1;
  1578. write_tty = 0;
  1579. tty_on = 0;
  1580. read_ssl = 1;
  1581. write_ssl = 1;
  1582. cbuf_len = 0;
  1583. cbuf_off = 0;
  1584. sbuf_len = 0;
  1585. sbuf_off = 0;
  1586. switch ((PROTOCOL_CHOICE) starttls_proto) {
  1587. case PROTO_OFF:
  1588. break;
  1589. case PROTO_SMTP:
  1590. {
  1591. /*
  1592. * This is an ugly hack that does a lot of assumptions. We do
  1593. * have to handle multi-line responses which may come in a single
  1594. * packet or not. We therefore have to use BIO_gets() which does
  1595. * need a buffering BIO. So during the initial chitchat we do
  1596. * push a buffering BIO into the chain that is removed again
  1597. * later on to not disturb the rest of the s_client operation.
  1598. */
  1599. int foundit = 0;
  1600. BIO *fbio = BIO_new(BIO_f_buffer());
  1601. BIO_push(fbio, sbio);
  1602. /* wait for multi-line response to end from SMTP */
  1603. do {
  1604. mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
  1605. }
  1606. while (mbuf_len > 3 && mbuf[3] == '-');
  1607. BIO_printf(fbio, "EHLO %s\r\n", ehlo);
  1608. (void)BIO_flush(fbio);
  1609. /* wait for multi-line response to end EHLO SMTP response */
  1610. do {
  1611. mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
  1612. if (strstr(mbuf, "STARTTLS"))
  1613. foundit = 1;
  1614. }
  1615. while (mbuf_len > 3 && mbuf[3] == '-');
  1616. (void)BIO_flush(fbio);
  1617. BIO_pop(fbio);
  1618. BIO_free(fbio);
  1619. if (!foundit)
  1620. BIO_printf(bio_err,
  1621. "didn't find starttls in server response,"
  1622. " trying anyway...\n");
  1623. BIO_printf(sbio, "STARTTLS\r\n");
  1624. BIO_read(sbio, sbuf, BUFSIZZ);
  1625. }
  1626. break;
  1627. case PROTO_POP3:
  1628. {
  1629. BIO_read(sbio, mbuf, BUFSIZZ);
  1630. BIO_printf(sbio, "STLS\r\n");
  1631. mbuf_len = BIO_read(sbio, sbuf, BUFSIZZ);
  1632. if (mbuf_len < 0) {
  1633. BIO_printf(bio_err, "BIO_read failed\n");
  1634. goto end;
  1635. }
  1636. }
  1637. break;
  1638. case PROTO_IMAP:
  1639. {
  1640. int foundit = 0;
  1641. BIO *fbio = BIO_new(BIO_f_buffer());
  1642. BIO_push(fbio, sbio);
  1643. BIO_gets(fbio, mbuf, BUFSIZZ);
  1644. /* STARTTLS command requires CAPABILITY... */
  1645. BIO_printf(fbio, ". CAPABILITY\r\n");
  1646. (void)BIO_flush(fbio);
  1647. /* wait for multi-line CAPABILITY response */
  1648. do {
  1649. mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
  1650. if (strstr(mbuf, "STARTTLS"))
  1651. foundit = 1;
  1652. }
  1653. while (mbuf_len > 3 && mbuf[0] != '.');
  1654. (void)BIO_flush(fbio);
  1655. BIO_pop(fbio);
  1656. BIO_free(fbio);
  1657. if (!foundit)
  1658. BIO_printf(bio_err,
  1659. "didn't find STARTTLS in server response,"
  1660. " trying anyway...\n");
  1661. BIO_printf(sbio, ". STARTTLS\r\n");
  1662. BIO_read(sbio, sbuf, BUFSIZZ);
  1663. }
  1664. break;
  1665. case PROTO_FTP:
  1666. {
  1667. BIO *fbio = BIO_new(BIO_f_buffer());
  1668. BIO_push(fbio, sbio);
  1669. /* wait for multi-line response to end from FTP */
  1670. do {
  1671. mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
  1672. }
  1673. while (mbuf_len > 3 && mbuf[3] == '-');
  1674. (void)BIO_flush(fbio);
  1675. BIO_pop(fbio);
  1676. BIO_free(fbio);
  1677. BIO_printf(sbio, "AUTH TLS\r\n");
  1678. BIO_read(sbio, sbuf, BUFSIZZ);
  1679. }
  1680. break;
  1681. case PROTO_XMPP:
  1682. case PROTO_XMPP_SERVER:
  1683. {
  1684. int seen = 0;
  1685. BIO_printf(sbio, "<stream:stream "
  1686. "xmlns:stream='http://etherx.jabber.org/streams' "
  1687. "xmlns='jabber:%s' to='%s' version='1.0'>",
  1688. starttls_proto == PROTO_XMPP ? "client" : "server",
  1689. xmpphost ? xmpphost : host);
  1690. seen = BIO_read(sbio, mbuf, BUFSIZZ);
  1691. mbuf[seen] = 0;
  1692. while (!strstr
  1693. (mbuf, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'")
  1694. && !strstr(mbuf,
  1695. "<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\""))
  1696. {
  1697. seen = BIO_read(sbio, mbuf, BUFSIZZ);
  1698. if (seen <= 0)
  1699. goto shut;
  1700. mbuf[seen] = 0;
  1701. }
  1702. BIO_printf(sbio,
  1703. "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
  1704. seen = BIO_read(sbio, sbuf, BUFSIZZ);
  1705. sbuf[seen] = 0;
  1706. if (!strstr(sbuf, "<proceed"))
  1707. goto shut;
  1708. mbuf[0] = 0;
  1709. }
  1710. break;
  1711. case PROTO_TELNET:
  1712. {
  1713. static const unsigned char tls_do[] = {
  1714. /* IAC DO START_TLS */
  1715. 255, 253, 46
  1716. };
  1717. static const unsigned char tls_will[] = {
  1718. /* IAC WILL START_TLS */
  1719. 255, 251, 46
  1720. };
  1721. static const unsigned char tls_follows[] = {
  1722. /* IAC SB START_TLS FOLLOWS IAC SE */
  1723. 255, 250, 46, 1, 255, 240
  1724. };
  1725. int bytes;
  1726. /* Telnet server should demand we issue START_TLS */
  1727. bytes = BIO_read(sbio, mbuf, BUFSIZZ);
  1728. if (bytes != 3 || memcmp(mbuf, tls_do, 3) != 0)
  1729. goto shut;
  1730. /* Agree to issue START_TLS and send the FOLLOWS sub-command */
  1731. BIO_write(sbio, tls_will, 3);
  1732. BIO_write(sbio, tls_follows, 6);
  1733. (void)BIO_flush(sbio);
  1734. /* Telnet server also sent the FOLLOWS sub-command */
  1735. bytes = BIO_read(sbio, mbuf, BUFSIZZ);
  1736. if (bytes != 6 || memcmp(mbuf, tls_follows, 6) != 0)
  1737. goto shut;
  1738. }
  1739. break;
  1740. case PROTO_CONNECT:
  1741. {
  1742. int foundit = 0;
  1743. BIO *fbio = BIO_new(BIO_f_buffer());
  1744. BIO_push(fbio, sbio);
  1745. BIO_printf(fbio, "CONNECT %s\r\n\r\n", connectstr);
  1746. (void)BIO_flush(fbio);
  1747. /* wait for multi-line response to end CONNECT response */
  1748. do {
  1749. mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
  1750. if (strstr(mbuf, "200") != NULL
  1751. && strstr(mbuf, "established") != NULL)
  1752. foundit++;
  1753. } while (mbuf_len > 3 && foundit == 0);
  1754. (void)BIO_flush(fbio);
  1755. BIO_pop(fbio);
  1756. BIO_free(fbio);
  1757. if (!foundit) {
  1758. BIO_printf(bio_err, "%s: HTTP CONNECT failed\n", prog);
  1759. goto shut;
  1760. }
  1761. }
  1762. break;
  1763. case PROTO_IRC:
  1764. {
  1765. int numeric;
  1766. BIO *fbio = BIO_new(BIO_f_buffer());
  1767. BIO_push(fbio, sbio);
  1768. BIO_printf(fbio, "STARTTLS\r\n");
  1769. (void)BIO_flush(fbio);
  1770. width = SSL_get_fd(con) + 1;
  1771. do {
  1772. numeric = 0;
  1773. FD_ZERO(&readfds);
  1774. openssl_fdset(SSL_get_fd(con), &readfds);
  1775. timeout.tv_sec = S_CLIENT_IRC_READ_TIMEOUT;
  1776. timeout.tv_usec = 0;
  1777. /*
  1778. * If the IRCd doesn't respond within
  1779. * S_CLIENT_IRC_READ_TIMEOUT seconds, assume
  1780. * it doesn't support STARTTLS. Many IRCds
  1781. * will not give _any_ sort of response to a
  1782. * STARTTLS command when it's not supported.
  1783. */
  1784. if (!BIO_get_buffer_num_lines(fbio)
  1785. && !BIO_pending(fbio)
  1786. && !BIO_pending(sbio)
  1787. && select(width, (void *)&readfds, NULL, NULL,
  1788. &timeout) < 1) {
  1789. BIO_printf(bio_err,
  1790. "Timeout waiting for response (%d seconds).\n",
  1791. S_CLIENT_IRC_READ_TIMEOUT);
  1792. break;
  1793. }
  1794. mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
  1795. if (mbuf_len < 1 || sscanf(mbuf, "%*s %d", &numeric) != 1)
  1796. break;
  1797. /* :example.net 451 STARTTLS :You have not registered */
  1798. /* :example.net 421 STARTTLS :Unknown command */
  1799. if ((numeric == 451 || numeric == 421)
  1800. && strstr(mbuf, "STARTTLS") != NULL) {
  1801. BIO_printf(bio_err, "STARTTLS not supported: %s", mbuf);
  1802. break;
  1803. }
  1804. if (numeric == 691) {
  1805. BIO_printf(bio_err, "STARTTLS negotiation failed: ");
  1806. ERR_print_errors(bio_err);
  1807. break;
  1808. }
  1809. } while (numeric != 670);
  1810. (void)BIO_flush(fbio);
  1811. BIO_pop(fbio);
  1812. BIO_free(fbio);
  1813. if (numeric != 670) {
  1814. BIO_printf(bio_err, "Server does not support STARTTLS.\n");
  1815. ret = 1;
  1816. goto shut;
  1817. }
  1818. }
  1819. }
  1820. for (;;) {
  1821. FD_ZERO(&readfds);
  1822. FD_ZERO(&writefds);
  1823. if ((SSL_version(con) == DTLS1_VERSION) &&
  1824. DTLSv1_get_timeout(con, &timeout))
  1825. timeoutp = &timeout;
  1826. else
  1827. timeoutp = NULL;
  1828. if (SSL_in_init(con) && !SSL_total_renegotiations(con)) {
  1829. in_init = 1;
  1830. tty_on = 0;
  1831. } else {
  1832. tty_on = 1;
  1833. if (in_init) {
  1834. in_init = 0;
  1835. if (servername != NULL && !SSL_session_reused(con)) {
  1836. BIO_printf(bio_c_out,
  1837. "Server did %sacknowledge servername extension.\n",
  1838. tlsextcbp.ack ? "" : "not ");
  1839. }
  1840. if (sess_out) {
  1841. BIO *stmp = BIO_new_file(sess_out, "w");
  1842. if (stmp) {
  1843. PEM_write_bio_SSL_SESSION(stmp, SSL_get_session(con));
  1844. BIO_free(stmp);
  1845. } else
  1846. BIO_printf(bio_err, "Error writing session file %s\n",
  1847. sess_out);
  1848. }
  1849. if (c_brief) {
  1850. BIO_puts(bio_err, "CONNECTION ESTABLISHED\n");
  1851. print_ssl_summary(con);
  1852. }
  1853. print_stuff(bio_c_out, con, full_log);
  1854. if (full_log > 0)
  1855. full_log--;
  1856. if (starttls_proto) {
  1857. BIO_write(bio_err, mbuf, mbuf_len);
  1858. /* We don't need to know any more */
  1859. if (!reconnect)
  1860. starttls_proto = PROTO_OFF;
  1861. }
  1862. if (reconnect) {
  1863. reconnect--;
  1864. BIO_printf(bio_c_out,
  1865. "drop connection and then reconnect\n");
  1866. SSL_shutdown(con);
  1867. SSL_set_connect_state(con);
  1868. SHUTDOWN(SSL_get_fd(con));
  1869. goto re_start;
  1870. }
  1871. }
  1872. }
  1873. ssl_pending = read_ssl && SSL_pending(con);
  1874. if (!ssl_pending) {
  1875. #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE)
  1876. if (tty_on) {
  1877. if (read_tty)
  1878. openssl_fdset(fileno(stdin), &readfds);
  1879. if (write_tty)
  1880. openssl_fdset(fileno(stdout), &writefds);
  1881. }
  1882. if (read_ssl)
  1883. openssl_fdset(SSL_get_fd(con), &readfds);
  1884. if (write_ssl)
  1885. openssl_fdset(SSL_get_fd(con), &writefds);
  1886. #else
  1887. if (!tty_on || !write_tty) {
  1888. if (read_ssl)
  1889. openssl_fdset(SSL_get_fd(con), &readfds);
  1890. if (write_ssl)
  1891. openssl_fdset(SSL_get_fd(con), &writefds);
  1892. }
  1893. #endif
  1894. /*
  1895. * Note: under VMS with SOCKETSHR the second parameter is
  1896. * currently of type (int *) whereas under other systems it is
  1897. * (void *) if you don't have a cast it will choke the compiler:
  1898. * if you do have a cast then you can either go for (int *) or
  1899. * (void *).
  1900. */
  1901. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
  1902. /*
  1903. * Under Windows/DOS we make the assumption that we can always
  1904. * write to the tty: therefore if we need to write to the tty we
  1905. * just fall through. Otherwise we timeout the select every
  1906. * second and see if there are any keypresses. Note: this is a
  1907. * hack, in a proper Windows application we wouldn't do this.
  1908. */
  1909. i = 0;
  1910. if (!write_tty) {
  1911. if (read_tty) {
  1912. tv.tv_sec = 1;
  1913. tv.tv_usec = 0;
  1914. i = select(width, (void *)&readfds, (void *)&writefds,
  1915. NULL, &tv);
  1916. # if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
  1917. if (!i && (!_kbhit() || !read_tty))
  1918. continue;
  1919. # else
  1920. if (!i && (!((_kbhit())
  1921. || (WAIT_OBJECT_0 ==
  1922. WaitForSingleObject(GetStdHandle
  1923. (STD_INPUT_HANDLE),
  1924. 0)))
  1925. || !read_tty))
  1926. continue;
  1927. # endif
  1928. } else
  1929. i = select(width, (void *)&readfds, (void *)&writefds,
  1930. NULL, timeoutp);
  1931. }
  1932. #elif defined(OPENSSL_SYS_NETWARE)
  1933. if (!write_tty) {
  1934. if (read_tty) {
  1935. tv.tv_sec = 1;
  1936. tv.tv_usec = 0;
  1937. i = select(width, (void *)&readfds, (void *)&writefds,
  1938. NULL, &tv);
  1939. } else
  1940. i = select(width, (void *)&readfds, (void *)&writefds,
  1941. NULL, timeoutp);
  1942. }
  1943. #else
  1944. i = select(width, (void *)&readfds, (void *)&writefds,
  1945. NULL, timeoutp);
  1946. #endif
  1947. if (i < 0) {
  1948. BIO_printf(bio_err, "bad select %d\n",
  1949. get_last_socket_error());
  1950. goto shut;
  1951. /* goto end; */
  1952. }
  1953. }
  1954. if ((SSL_version(con) == DTLS1_VERSION)
  1955. && DTLSv1_handle_timeout(con) > 0) {
  1956. BIO_printf(bio_err, "TIMEOUT occurred\n");
  1957. }
  1958. if (!ssl_pending && FD_ISSET(SSL_get_fd(con), &writefds)) {
  1959. k = SSL_write(con, &(cbuf[cbuf_off]), (unsigned int)cbuf_len);
  1960. switch (SSL_get_error(con, k)) {
  1961. case SSL_ERROR_NONE:
  1962. cbuf_off += k;
  1963. cbuf_len -= k;
  1964. if (k <= 0)
  1965. goto end;
  1966. /* we have done a write(con,NULL,0); */
  1967. if (cbuf_len <= 0) {
  1968. read_tty = 1;
  1969. write_ssl = 0;
  1970. } else { /* if (cbuf_len > 0) */
  1971. read_tty = 0;
  1972. write_ssl = 1;
  1973. }
  1974. break;
  1975. case SSL_ERROR_WANT_WRITE:
  1976. BIO_printf(bio_c_out, "write W BLOCK\n");
  1977. write_ssl = 1;
  1978. read_tty = 0;
  1979. break;
  1980. case SSL_ERROR_WANT_ASYNC:
  1981. BIO_printf(bio_c_out, "write A BLOCK\n");
  1982. wait_for_async(con);
  1983. write_ssl = 1;
  1984. read_tty = 0;
  1985. break;
  1986. case SSL_ERROR_WANT_READ:
  1987. BIO_printf(bio_c_out, "write R BLOCK\n");
  1988. write_tty = 0;
  1989. read_ssl = 1;
  1990. write_ssl = 0;
  1991. break;
  1992. case SSL_ERROR_WANT_X509_LOOKUP:
  1993. BIO_printf(bio_c_out, "write X BLOCK\n");
  1994. break;
  1995. case SSL_ERROR_ZERO_RETURN:
  1996. if (cbuf_len != 0) {
  1997. BIO_printf(bio_c_out, "shutdown\n");
  1998. ret = 0;
  1999. goto shut;
  2000. } else {
  2001. read_tty = 1;
  2002. write_ssl = 0;
  2003. break;
  2004. }
  2005. case SSL_ERROR_SYSCALL:
  2006. if ((k != 0) || (cbuf_len != 0)) {
  2007. BIO_printf(bio_err, "write:errno=%d\n",
  2008. get_last_socket_error());
  2009. goto shut;
  2010. } else {
  2011. read_tty = 1;
  2012. write_ssl = 0;
  2013. }
  2014. break;
  2015. case SSL_ERROR_SSL:
  2016. ERR_print_errors(bio_err);
  2017. goto shut;
  2018. }
  2019. }
  2020. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)
  2021. /* Assume Windows/DOS/BeOS can always write */
  2022. else if (!ssl_pending && write_tty)
  2023. #else
  2024. else if (!ssl_pending && FD_ISSET(fileno(stdout), &writefds))
  2025. #endif
  2026. {
  2027. #ifdef CHARSET_EBCDIC
  2028. ascii2ebcdic(&(sbuf[sbuf_off]), &(sbuf[sbuf_off]), sbuf_len);
  2029. #endif
  2030. i = raw_write_stdout(&(sbuf[sbuf_off]), sbuf_len);
  2031. if (i <= 0) {
  2032. BIO_printf(bio_c_out, "DONE\n");
  2033. ret = 0;
  2034. goto shut;
  2035. /* goto end; */
  2036. }
  2037. sbuf_len -= i;;
  2038. sbuf_off += i;
  2039. if (sbuf_len <= 0) {
  2040. read_ssl = 1;
  2041. write_tty = 0;
  2042. }
  2043. } else if (ssl_pending || FD_ISSET(SSL_get_fd(con), &readfds)) {
  2044. #ifdef RENEG
  2045. {
  2046. static int iiii;
  2047. if (++iiii == 52) {
  2048. SSL_renegotiate(con);
  2049. iiii = 0;
  2050. }
  2051. }
  2052. #endif
  2053. k = SSL_read(con, sbuf, 1024 /* BUFSIZZ */ );
  2054. switch (SSL_get_error(con, k)) {
  2055. case SSL_ERROR_NONE:
  2056. if (k <= 0)
  2057. goto end;
  2058. sbuf_off = 0;
  2059. sbuf_len = k;
  2060. read_ssl = 0;
  2061. write_tty = 1;
  2062. break;
  2063. case SSL_ERROR_WANT_ASYNC:
  2064. BIO_printf(bio_c_out, "read A BLOCK\n");
  2065. wait_for_async(con);
  2066. write_tty = 0;
  2067. read_ssl = 1;
  2068. if ((read_tty == 0) && (write_ssl == 0))
  2069. write_ssl = 1;
  2070. break;
  2071. case SSL_ERROR_WANT_WRITE:
  2072. BIO_printf(bio_c_out, "read W BLOCK\n");
  2073. write_ssl = 1;
  2074. read_tty = 0;
  2075. break;
  2076. case SSL_ERROR_WANT_READ:
  2077. BIO_printf(bio_c_out, "read R BLOCK\n");
  2078. write_tty = 0;
  2079. read_ssl = 1;
  2080. if ((read_tty == 0) && (write_ssl == 0))
  2081. write_ssl = 1;
  2082. break;
  2083. case SSL_ERROR_WANT_X509_LOOKUP:
  2084. BIO_printf(bio_c_out, "read X BLOCK\n");
  2085. break;
  2086. case SSL_ERROR_SYSCALL:
  2087. ret = get_last_socket_error();
  2088. if (c_brief)
  2089. BIO_puts(bio_err, "CONNECTION CLOSED BY SERVER\n");
  2090. else
  2091. BIO_printf(bio_err, "read:errno=%d\n", ret);
  2092. goto shut;
  2093. case SSL_ERROR_ZERO_RETURN:
  2094. BIO_printf(bio_c_out, "closed\n");
  2095. ret = 0;
  2096. goto shut;
  2097. case SSL_ERROR_SSL:
  2098. ERR_print_errors(bio_err);
  2099. goto shut;
  2100. /* break; */
  2101. }
  2102. }
  2103. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
  2104. # if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
  2105. else if (_kbhit())
  2106. # else
  2107. else if ((_kbhit())
  2108. || (WAIT_OBJECT_0 ==
  2109. WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0)))
  2110. # endif
  2111. #elif defined (OPENSSL_SYS_NETWARE)
  2112. else if (_kbhit())
  2113. #else
  2114. else if (FD_ISSET(fileno(stdin), &readfds))
  2115. #endif
  2116. {
  2117. if (crlf) {
  2118. int j, lf_num;
  2119. i = raw_read_stdin(cbuf, BUFSIZZ / 2);
  2120. lf_num = 0;
  2121. /* both loops are skipped when i <= 0 */
  2122. for (j = 0; j < i; j++)
  2123. if (cbuf[j] == '\n')
  2124. lf_num++;
  2125. for (j = i - 1; j >= 0; j--) {
  2126. cbuf[j + lf_num] = cbuf[j];
  2127. if (cbuf[j] == '\n') {
  2128. lf_num--;
  2129. i++;
  2130. cbuf[j + lf_num] = '\r';
  2131. }
  2132. }
  2133. assert(lf_num == 0);
  2134. } else
  2135. i = raw_read_stdin(cbuf, BUFSIZZ);
  2136. if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q' && cmdletters))) {
  2137. BIO_printf(bio_err, "DONE\n");
  2138. ret = 0;
  2139. goto shut;
  2140. }
  2141. if ((!c_ign_eof) && (cbuf[0] == 'R' && cmdletters)) {
  2142. BIO_printf(bio_err, "RENEGOTIATING\n");
  2143. SSL_renegotiate(con);
  2144. cbuf_len = 0;
  2145. }
  2146. #ifndef OPENSSL_NO_HEARTBEATS
  2147. else if ((!c_ign_eof) && (cbuf[0] == 'B' && cmdletters)) {
  2148. BIO_printf(bio_err, "HEARTBEATING\n");
  2149. SSL_heartbeat(con);
  2150. cbuf_len = 0;
  2151. }
  2152. #endif
  2153. else {
  2154. cbuf_len = i;
  2155. cbuf_off = 0;
  2156. #ifdef CHARSET_EBCDIC
  2157. ebcdic2ascii(cbuf, cbuf, i);
  2158. #endif
  2159. }
  2160. write_ssl = 1;
  2161. read_tty = 0;
  2162. }
  2163. }
  2164. ret = 0;
  2165. shut:
  2166. if (in_init)
  2167. print_stuff(bio_c_out, con, full_log);
  2168. SSL_shutdown(con);
  2169. SHUTDOWN(SSL_get_fd(con));
  2170. end:
  2171. if (con != NULL) {
  2172. if (prexit != 0)
  2173. print_stuff(bio_c_out, con, 1);
  2174. SSL_free(con);
  2175. }
  2176. if (async) {
  2177. ASYNC_cleanup(1);
  2178. }
  2179. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  2180. OPENSSL_free(next_proto.data);
  2181. #endif
  2182. SSL_CTX_free(ctx);
  2183. X509_free(cert);
  2184. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  2185. EVP_PKEY_free(key);
  2186. sk_X509_pop_free(chain, X509_free);
  2187. OPENSSL_free(pass);
  2188. #ifndef OPENSSL_NO_SRP
  2189. OPENSSL_free(srp_arg.srppassin);
  2190. #endif
  2191. X509_VERIFY_PARAM_free(vpm);
  2192. ssl_excert_free(exc);
  2193. sk_OPENSSL_STRING_free(ssl_args);
  2194. sk_OPENSSL_STRING_free(dane_tlsa_rrset);
  2195. SSL_CONF_CTX_free(cctx);
  2196. OPENSSL_clear_free(cbuf, BUFSIZZ);
  2197. OPENSSL_clear_free(sbuf, BUFSIZZ);
  2198. OPENSSL_clear_free(mbuf, BUFSIZZ);
  2199. BIO_free(bio_c_out);
  2200. bio_c_out = NULL;
  2201. BIO_free(bio_c_msg);
  2202. bio_c_msg = NULL;
  2203. return (ret);
  2204. }
  2205. static void print_stuff(BIO *bio, SSL *s, int full)
  2206. {
  2207. X509 *peer = NULL;
  2208. char buf[BUFSIZ];
  2209. STACK_OF(X509) *sk;
  2210. STACK_OF(X509_NAME) *sk2;
  2211. const SSL_CIPHER *c;
  2212. X509_NAME *xn;
  2213. int i;
  2214. int mdpth;
  2215. EVP_PKEY *mspki;
  2216. const char *peername;
  2217. #ifndef OPENSSL_NO_COMP
  2218. const COMP_METHOD *comp, *expansion;
  2219. #endif
  2220. unsigned char *exportedkeymat;
  2221. if (full) {
  2222. int got_a_chain = 0;
  2223. sk = SSL_get_peer_cert_chain(s);
  2224. if (sk != NULL) {
  2225. got_a_chain = 1;
  2226. BIO_printf(bio, "---\nCertificate chain\n");
  2227. for (i = 0; i < sk_X509_num(sk); i++) {
  2228. X509_NAME_oneline(X509_get_subject_name(sk_X509_value(sk, i)),
  2229. buf, sizeof buf);
  2230. BIO_printf(bio, "%2d s:%s\n", i, buf);
  2231. X509_NAME_oneline(X509_get_issuer_name(sk_X509_value(sk, i)),
  2232. buf, sizeof buf);
  2233. BIO_printf(bio, " i:%s\n", buf);
  2234. if (c_showcerts)
  2235. PEM_write_bio_X509(bio, sk_X509_value(sk, i));
  2236. }
  2237. }
  2238. BIO_printf(bio, "---\n");
  2239. peer = SSL_get_peer_certificate(s);
  2240. if (peer != NULL) {
  2241. BIO_printf(bio, "Server certificate\n");
  2242. /* Redundant if we showed the whole chain */
  2243. if (!(c_showcerts && got_a_chain))
  2244. PEM_write_bio_X509(bio, peer);
  2245. X509_NAME_oneline(X509_get_subject_name(peer), buf, sizeof buf);
  2246. BIO_printf(bio, "subject=%s\n", buf);
  2247. X509_NAME_oneline(X509_get_issuer_name(peer), buf, sizeof buf);
  2248. BIO_printf(bio, "issuer=%s\n", buf);
  2249. } else
  2250. BIO_printf(bio, "no peer certificate available\n");
  2251. sk2 = SSL_get_client_CA_list(s);
  2252. if ((sk2 != NULL) && (sk_X509_NAME_num(sk2) > 0)) {
  2253. BIO_printf(bio, "---\nAcceptable client certificate CA names\n");
  2254. for (i = 0; i < sk_X509_NAME_num(sk2); i++) {
  2255. xn = sk_X509_NAME_value(sk2, i);
  2256. X509_NAME_oneline(xn, buf, sizeof(buf));
  2257. BIO_write(bio, buf, strlen(buf));
  2258. BIO_write(bio, "\n", 1);
  2259. }
  2260. } else {
  2261. BIO_printf(bio, "---\nNo client certificate CA names sent\n");
  2262. }
  2263. ssl_print_sigalgs(bio, s);
  2264. ssl_print_tmp_key(bio, s);
  2265. BIO_printf(bio,
  2266. "---\nSSL handshake has read %"PRIu64" bytes and written %"PRIu64" bytes\n",
  2267. BIO_number_read(SSL_get_rbio(s)),
  2268. BIO_number_written(SSL_get_wbio(s)));
  2269. }
  2270. if ((mdpth = SSL_get0_dane_authority(s, NULL, &mspki)) >= 0) {
  2271. uint8_t usage, selector, mtype;
  2272. mdpth = SSL_get0_dane_tlsa(s, &usage, &selector, &mtype, NULL, NULL);
  2273. BIO_printf(bio, "DANE TLSA %d %d %d %s at depth %d\n",
  2274. usage, selector, mtype,
  2275. (mspki != NULL) ? "TA public key verified certificate" :
  2276. mdpth ? "matched TA certificate" : "matched EE certificate",
  2277. mdpth);
  2278. }
  2279. if (SSL_get_verify_result(s) == X509_V_OK &&
  2280. (peername = SSL_get0_peername(s)) != NULL)
  2281. BIO_printf(bio, "Verified peername: %s\n", peername);
  2282. BIO_printf(bio, (SSL_cache_hit(s) ? "---\nReused, " : "---\nNew, "));
  2283. c = SSL_get_current_cipher(s);
  2284. BIO_printf(bio, "%s, Cipher is %s\n",
  2285. SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
  2286. if (peer != NULL) {
  2287. EVP_PKEY *pktmp;
  2288. pktmp = X509_get0_pubkey(peer);
  2289. BIO_printf(bio, "Server public key is %d bit\n",
  2290. EVP_PKEY_bits(pktmp));
  2291. }
  2292. BIO_printf(bio, "Secure Renegotiation IS%s supported\n",
  2293. SSL_get_secure_renegotiation_support(s) ? "" : " NOT");
  2294. #ifndef OPENSSL_NO_COMP
  2295. comp = SSL_get_current_compression(s);
  2296. expansion = SSL_get_current_expansion(s);
  2297. BIO_printf(bio, "Compression: %s\n",
  2298. comp ? SSL_COMP_get_name(comp) : "NONE");
  2299. BIO_printf(bio, "Expansion: %s\n",
  2300. expansion ? SSL_COMP_get_name(expansion) : "NONE");
  2301. #endif
  2302. #ifdef SSL_DEBUG
  2303. {
  2304. /* Print out local port of connection: useful for debugging */
  2305. int sock;
  2306. struct sockaddr_in ladd;
  2307. socklen_t ladd_size = sizeof(ladd);
  2308. sock = SSL_get_fd(s);
  2309. getsockname(sock, (struct sockaddr *)&ladd, &ladd_size);
  2310. BIO_printf(bio_c_out, "LOCAL PORT is %u\n", ntohs(ladd.sin_port));
  2311. }
  2312. #endif
  2313. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  2314. if (next_proto.status != -1) {
  2315. const unsigned char *proto;
  2316. unsigned int proto_len;
  2317. SSL_get0_next_proto_negotiated(s, &proto, &proto_len);
  2318. BIO_printf(bio, "Next protocol: (%d) ", next_proto.status);
  2319. BIO_write(bio, proto, proto_len);
  2320. BIO_write(bio, "\n", 1);
  2321. }
  2322. #endif
  2323. {
  2324. const unsigned char *proto;
  2325. unsigned int proto_len;
  2326. SSL_get0_alpn_selected(s, &proto, &proto_len);
  2327. if (proto_len > 0) {
  2328. BIO_printf(bio, "ALPN protocol: ");
  2329. BIO_write(bio, proto, proto_len);
  2330. BIO_write(bio, "\n", 1);
  2331. } else
  2332. BIO_printf(bio, "No ALPN negotiated\n");
  2333. }
  2334. #ifndef OPENSSL_NO_SRTP
  2335. {
  2336. SRTP_PROTECTION_PROFILE *srtp_profile =
  2337. SSL_get_selected_srtp_profile(s);
  2338. if (srtp_profile)
  2339. BIO_printf(bio, "SRTP Extension negotiated, profile=%s\n",
  2340. srtp_profile->name);
  2341. }
  2342. #endif
  2343. SSL_SESSION_print(bio, SSL_get_session(s));
  2344. if (keymatexportlabel != NULL) {
  2345. BIO_printf(bio, "Keying material exporter:\n");
  2346. BIO_printf(bio, " Label: '%s'\n", keymatexportlabel);
  2347. BIO_printf(bio, " Length: %i bytes\n", keymatexportlen);
  2348. exportedkeymat = app_malloc(keymatexportlen, "export key");
  2349. if (!SSL_export_keying_material(s, exportedkeymat,
  2350. keymatexportlen,
  2351. keymatexportlabel,
  2352. strlen(keymatexportlabel),
  2353. NULL, 0, 0)) {
  2354. BIO_printf(bio, " Error\n");
  2355. } else {
  2356. BIO_printf(bio, " Keying material: ");
  2357. for (i = 0; i < keymatexportlen; i++)
  2358. BIO_printf(bio, "%02X", exportedkeymat[i]);
  2359. BIO_printf(bio, "\n");
  2360. }
  2361. OPENSSL_free(exportedkeymat);
  2362. }
  2363. BIO_printf(bio, "---\n");
  2364. X509_free(peer);
  2365. /* flush, or debugging output gets mixed with http response */
  2366. (void)BIO_flush(bio);
  2367. }
  2368. static int ocsp_resp_cb(SSL *s, void *arg)
  2369. {
  2370. const unsigned char *p;
  2371. int len;
  2372. OCSP_RESPONSE *rsp;
  2373. len = SSL_get_tlsext_status_ocsp_resp(s, &p);
  2374. BIO_puts(arg, "OCSP response: ");
  2375. if (!p) {
  2376. BIO_puts(arg, "no response sent\n");
  2377. return 1;
  2378. }
  2379. rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
  2380. if (!rsp) {
  2381. BIO_puts(arg, "response parse error\n");
  2382. BIO_dump_indent(arg, (char *)p, len, 4);
  2383. return 0;
  2384. }
  2385. BIO_puts(arg, "\n======================================\n");
  2386. OCSP_RESPONSE_print(arg, rsp, 0);
  2387. BIO_puts(arg, "======================================\n");
  2388. OCSP_RESPONSE_free(rsp);
  2389. return 1;
  2390. }