s_client.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  1. /* apps/s_client.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. /* ====================================================================
  112. * Copyright 2005 Nokia. All rights reserved.
  113. *
  114. * The portions of the attached software ("Contribution") is developed by
  115. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  116. * license.
  117. *
  118. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  119. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  120. * support (see RFC 4279) to OpenSSL.
  121. *
  122. * No patent licenses or other rights except those expressly stated in
  123. * the OpenSSL open source license shall be deemed granted or received
  124. * expressly, by implication, estoppel, or otherwise.
  125. *
  126. * No assurances are provided by Nokia that the Contribution does not
  127. * infringe the patent or other intellectual property rights of any third
  128. * party or that the license provides you with all the necessary rights
  129. * to make use of the Contribution.
  130. *
  131. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  132. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  133. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  134. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  135. * OTHERWISE.
  136. */
  137. #include <assert.h>
  138. #include <ctype.h>
  139. #include <stdio.h>
  140. #include <stdlib.h>
  141. #include <string.h>
  142. #include <openssl/e_os2.h>
  143. #ifdef OPENSSL_NO_STDIO
  144. #define APPS_WIN16
  145. #endif
  146. /* With IPv6, it looks like Digital has mixed up the proper order of
  147. recursive header file inclusion, resulting in the compiler complaining
  148. that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
  149. is needed to have fileno() declared correctly... So let's define u_int */
  150. #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
  151. #define __U_INT
  152. typedef unsigned int u_int;
  153. #endif
  154. #define USE_SOCKETS
  155. #include "apps.h"
  156. #include <openssl/x509.h>
  157. #include <openssl/ssl.h>
  158. #include <openssl/err.h>
  159. #include <openssl/pem.h>
  160. #include <openssl/rand.h>
  161. #include <openssl/ocsp.h>
  162. #include <openssl/bn.h>
  163. #include "s_apps.h"
  164. #include "timeouts.h"
  165. #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
  166. /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
  167. #undef FIONBIO
  168. #endif
  169. #if defined(OPENSSL_SYS_BEOS_R5)
  170. #include <fcntl.h>
  171. #endif
  172. #undef PROG
  173. #define PROG s_client_main
  174. /*#define SSL_HOST_NAME "www.netscape.com" */
  175. /*#define SSL_HOST_NAME "193.118.187.102" */
  176. #define SSL_HOST_NAME "localhost"
  177. /*#define TEST_CERT "client.pem" */ /* no default cert. */
  178. #undef BUFSIZZ
  179. #define BUFSIZZ 1024*8
  180. extern int verify_depth;
  181. extern int verify_error;
  182. extern int verify_return_error;
  183. #ifdef FIONBIO
  184. static int c_nbio=0;
  185. #endif
  186. static int c_Pause=0;
  187. static int c_debug=0;
  188. #ifndef OPENSSL_NO_TLSEXT
  189. static int c_tlsextdebug=0;
  190. static int c_status_req=0;
  191. #endif
  192. static int c_msg=0;
  193. static int c_showcerts=0;
  194. static void sc_usage(void);
  195. static void print_stuff(BIO *berr,SSL *con,int full);
  196. static int ocsp_resp_cb(SSL *s, void *arg);
  197. static BIO *bio_c_out=NULL;
  198. static int c_quiet=0;
  199. static int c_ign_eof=0;
  200. #ifndef OPENSSL_NO_PSK
  201. /* Default PSK identity and key */
  202. static char *psk_identity="Client_identity";
  203. static char *psk_key=NULL; /* by default PSK is not used */
  204. static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *identity,
  205. unsigned int max_identity_len, unsigned char *psk,
  206. unsigned int max_psk_len)
  207. {
  208. unsigned int psk_len = 0;
  209. int ret;
  210. BIGNUM *bn=NULL;
  211. if (c_debug)
  212. BIO_printf(bio_c_out, "psk_client_cb\n");
  213. if (!hint)
  214. {
  215. /* no ServerKeyExchange message*/
  216. if (c_debug)
  217. BIO_printf(bio_c_out,"NULL received PSK identity hint, continuing anyway\n");
  218. }
  219. else if (c_debug)
  220. BIO_printf(bio_c_out, "Received PSK identity hint '%s'\n", hint);
  221. /* lookup PSK identity and PSK key based on the given identity hint here */
  222. ret = BIO_snprintf(identity, max_identity_len, psk_identity);
  223. if (ret < 0 || (unsigned int)ret > max_identity_len)
  224. goto out_err;
  225. if (c_debug)
  226. BIO_printf(bio_c_out, "created identity '%s' len=%d\n", identity, ret);
  227. ret=BN_hex2bn(&bn, psk_key);
  228. if (!ret)
  229. {
  230. BIO_printf(bio_err,"Could not convert PSK key '%s' to BIGNUM\n", psk_key);
  231. if (bn)
  232. BN_free(bn);
  233. return 0;
  234. }
  235. if ((unsigned int)BN_num_bytes(bn) > max_psk_len)
  236. {
  237. BIO_printf(bio_err,"psk buffer of callback is too small (%d) for key (%d)\n",
  238. max_psk_len, BN_num_bytes(bn));
  239. BN_free(bn);
  240. return 0;
  241. }
  242. psk_len=BN_bn2bin(bn, psk);
  243. BN_free(bn);
  244. if (psk_len == 0)
  245. goto out_err;
  246. if (c_debug)
  247. BIO_printf(bio_c_out, "created PSK len=%d\n", psk_len);
  248. return psk_len;
  249. out_err:
  250. if (c_debug)
  251. BIO_printf(bio_err, "Error in PSK client callback\n");
  252. return 0;
  253. }
  254. #endif
  255. static void sc_usage(void)
  256. {
  257. BIO_printf(bio_err,"usage: s_client args\n");
  258. BIO_printf(bio_err,"\n");
  259. BIO_printf(bio_err," -host host - use -connect instead\n");
  260. BIO_printf(bio_err," -port port - use -connect instead\n");
  261. BIO_printf(bio_err," -connect host:port - who to connect to (default is %s:%s)\n",SSL_HOST_NAME,PORT_STR);
  262. BIO_printf(bio_err," -verify arg - turn on peer certificate verification\n");
  263. BIO_printf(bio_err," -cert arg - certificate file to use, PEM format assumed\n");
  264. BIO_printf(bio_err," -certform arg - certificate format (PEM or DER) PEM default\n");
  265. BIO_printf(bio_err," -key arg - Private key file to use, in cert file if\n");
  266. BIO_printf(bio_err," not specified but cert file is.\n");
  267. BIO_printf(bio_err," -keyform arg - key format (PEM or DER) PEM default\n");
  268. BIO_printf(bio_err," -pass arg - private key file pass phrase source\n");
  269. BIO_printf(bio_err," -CApath arg - PEM format directory of CA's\n");
  270. BIO_printf(bio_err," -CAfile arg - PEM format file of CA's\n");
  271. BIO_printf(bio_err," -reconnect - Drop and re-make the connection with the same Session-ID\n");
  272. BIO_printf(bio_err," -pause - sleep(1) after each read(2) and write(2) system call\n");
  273. BIO_printf(bio_err," -showcerts - show all certificates in the chain\n");
  274. BIO_printf(bio_err," -debug - extra output\n");
  275. #ifdef WATT32
  276. BIO_printf(bio_err," -wdebug - WATT-32 tcp debugging\n");
  277. #endif
  278. BIO_printf(bio_err," -msg - Show protocol messages\n");
  279. BIO_printf(bio_err," -nbio_test - more ssl protocol testing\n");
  280. BIO_printf(bio_err," -state - print the 'ssl' states\n");
  281. #ifdef FIONBIO
  282. BIO_printf(bio_err," -nbio - Run with non-blocking IO\n");
  283. #endif
  284. BIO_printf(bio_err," -crlf - convert LF from terminal into CRLF\n");
  285. BIO_printf(bio_err," -quiet - no s_client output\n");
  286. BIO_printf(bio_err," -ign_eof - ignore input eof (default when -quiet)\n");
  287. #ifndef OPENSSL_NO_PSK
  288. BIO_printf(bio_err," -psk_identity arg - PSK identity\n");
  289. BIO_printf(bio_err," -psk arg - PSK in hex (without 0x)\n");
  290. #endif
  291. BIO_printf(bio_err," -ssl2 - just use SSLv2\n");
  292. BIO_printf(bio_err," -ssl3 - just use SSLv3\n");
  293. BIO_printf(bio_err," -tls1 - just use TLSv1\n");
  294. BIO_printf(bio_err," -dtls1 - just use DTLSv1\n");
  295. BIO_printf(bio_err," -mtu - set the MTU\n");
  296. BIO_printf(bio_err," -no_tls1/-no_ssl3/-no_ssl2 - turn off that protocol\n");
  297. BIO_printf(bio_err," -bugs - Switch on all SSL implementation bug workarounds\n");
  298. BIO_printf(bio_err," -serverpref - Use server's cipher preferences (only SSLv2)\n");
  299. BIO_printf(bio_err," -cipher - preferred cipher to use, use the 'openssl ciphers'\n");
  300. BIO_printf(bio_err," command to see what is available\n");
  301. BIO_printf(bio_err," -starttls prot - use the STARTTLS command before starting TLS\n");
  302. BIO_printf(bio_err," for those protocols that support it, where\n");
  303. BIO_printf(bio_err," 'prot' defines which one to assume. Currently,\n");
  304. BIO_printf(bio_err," only \"smtp\", \"pop3\", \"imap\", and \"ftp\" are supported.\n");
  305. #ifndef OPENSSL_NO_ENGINE
  306. BIO_printf(bio_err," -engine id - Initialise and use the specified engine\n");
  307. #endif
  308. BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
  309. BIO_printf(bio_err," -sess_out arg - file to write SSL session to\n");
  310. BIO_printf(bio_err," -sess_in arg - file to read SSL session from\n");
  311. #ifndef OPENSSL_NO_TLSEXT
  312. BIO_printf(bio_err," -servername host - Set TLS extension servername in ClientHello\n");
  313. BIO_printf(bio_err," -tlsextdebug - hex dump of all TLS extensions received\n");
  314. BIO_printf(bio_err," -status - request certificate status from server\n");
  315. BIO_printf(bio_err," -no_ticket - disable use of RFC4507bis session tickets\n");
  316. #endif
  317. }
  318. #ifndef OPENSSL_NO_TLSEXT
  319. /* This is a context that we pass to callbacks */
  320. typedef struct tlsextctx_st {
  321. BIO * biodebug;
  322. int ack;
  323. } tlsextctx;
  324. static int MS_CALLBACK ssl_servername_cb(SSL *s, int *ad, void *arg)
  325. {
  326. tlsextctx * p = (tlsextctx *) arg;
  327. const char * hn= SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
  328. if (SSL_get_servername_type(s) != -1)
  329. p->ack = !SSL_session_reused(s) && hn != NULL;
  330. else
  331. BIO_printf(bio_err,"Can't use SSL_get_servername\n");
  332. return SSL_TLSEXT_ERR_OK;
  333. }
  334. #endif
  335. enum
  336. {
  337. PROTO_OFF = 0,
  338. PROTO_SMTP,
  339. PROTO_POP3,
  340. PROTO_IMAP,
  341. PROTO_FTP
  342. };
  343. int MAIN(int, char **);
  344. int MAIN(int argc, char **argv)
  345. {
  346. int off=0;
  347. SSL *con=NULL;
  348. X509_STORE *store = NULL;
  349. int s,k,width,state=0;
  350. char *cbuf=NULL,*sbuf=NULL,*mbuf=NULL;
  351. int cbuf_len,cbuf_off;
  352. int sbuf_len,sbuf_off;
  353. fd_set readfds,writefds;
  354. short port=PORT;
  355. int full_log=1;
  356. char *host=SSL_HOST_NAME;
  357. char *cert_file=NULL,*key_file=NULL;
  358. int cert_format = FORMAT_PEM, key_format = FORMAT_PEM;
  359. char *passarg = NULL, *pass = NULL;
  360. X509 *cert = NULL;
  361. EVP_PKEY *key = NULL;
  362. char *CApath=NULL,*CAfile=NULL,*cipher=NULL;
  363. int reconnect=0,badop=0,verify=SSL_VERIFY_NONE,bugs=0;
  364. int crlf=0;
  365. int write_tty,read_tty,write_ssl,read_ssl,tty_on,ssl_pending;
  366. SSL_CTX *ctx=NULL;
  367. int ret=1,in_init=1,i,nbio_test=0;
  368. int starttls_proto = PROTO_OFF;
  369. int prexit = 0, vflags = 0;
  370. const SSL_METHOD *meth=NULL;
  371. int socket_type=SOCK_STREAM;
  372. BIO *sbio;
  373. char *inrand=NULL;
  374. int mbuf_len=0;
  375. #ifndef OPENSSL_NO_ENGINE
  376. char *engine_id=NULL;
  377. ENGINE *e=NULL;
  378. #endif
  379. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)
  380. struct timeval tv;
  381. #if defined(OPENSSL_SYS_BEOS_R5)
  382. int stdin_set = 0;
  383. #endif
  384. #endif
  385. #ifndef OPENSSL_NO_TLSEXT
  386. char *servername = NULL;
  387. tlsextctx tlsextcbp =
  388. {NULL,0};
  389. #endif
  390. char *sess_in = NULL;
  391. char *sess_out = NULL;
  392. struct sockaddr peer;
  393. int peerlen = sizeof(peer);
  394. int enable_timeouts = 0 ;
  395. long socket_mtu = 0;
  396. #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
  397. meth=SSLv23_client_method();
  398. #elif !defined(OPENSSL_NO_SSL3)
  399. meth=SSLv3_client_method();
  400. #elif !defined(OPENSSL_NO_SSL2)
  401. meth=SSLv2_client_method();
  402. #endif
  403. apps_startup();
  404. c_Pause=0;
  405. c_quiet=0;
  406. c_ign_eof=0;
  407. c_debug=0;
  408. c_msg=0;
  409. c_showcerts=0;
  410. if (bio_err == NULL)
  411. bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
  412. if (!load_config(bio_err, NULL))
  413. goto end;
  414. if ( ((cbuf=OPENSSL_malloc(BUFSIZZ)) == NULL) ||
  415. ((sbuf=OPENSSL_malloc(BUFSIZZ)) == NULL) ||
  416. ((mbuf=OPENSSL_malloc(BUFSIZZ)) == NULL))
  417. {
  418. BIO_printf(bio_err,"out of memory\n");
  419. goto end;
  420. }
  421. verify_depth=0;
  422. verify_error=X509_V_OK;
  423. #ifdef FIONBIO
  424. c_nbio=0;
  425. #endif
  426. argc--;
  427. argv++;
  428. while (argc >= 1)
  429. {
  430. if (strcmp(*argv,"-host") == 0)
  431. {
  432. if (--argc < 1) goto bad;
  433. host= *(++argv);
  434. }
  435. else if (strcmp(*argv,"-port") == 0)
  436. {
  437. if (--argc < 1) goto bad;
  438. port=atoi(*(++argv));
  439. if (port == 0) goto bad;
  440. }
  441. else if (strcmp(*argv,"-connect") == 0)
  442. {
  443. if (--argc < 1) goto bad;
  444. if (!extract_host_port(*(++argv),&host,NULL,&port))
  445. goto bad;
  446. }
  447. else if (strcmp(*argv,"-verify") == 0)
  448. {
  449. verify=SSL_VERIFY_PEER;
  450. if (--argc < 1) goto bad;
  451. verify_depth=atoi(*(++argv));
  452. BIO_printf(bio_err,"verify depth is %d\n",verify_depth);
  453. }
  454. else if (strcmp(*argv,"-cert") == 0)
  455. {
  456. if (--argc < 1) goto bad;
  457. cert_file= *(++argv);
  458. }
  459. else if (strcmp(*argv,"-sess_out") == 0)
  460. {
  461. if (--argc < 1) goto bad;
  462. sess_out = *(++argv);
  463. }
  464. else if (strcmp(*argv,"-sess_in") == 0)
  465. {
  466. if (--argc < 1) goto bad;
  467. sess_in = *(++argv);
  468. }
  469. else if (strcmp(*argv,"-certform") == 0)
  470. {
  471. if (--argc < 1) goto bad;
  472. cert_format = str2fmt(*(++argv));
  473. }
  474. else if (strcmp(*argv,"-crl_check") == 0)
  475. vflags |= X509_V_FLAG_CRL_CHECK;
  476. else if (strcmp(*argv,"-crl_check_all") == 0)
  477. vflags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;
  478. else if (strcmp(*argv,"-verify_return_error") == 0)
  479. verify_return_error = 1;
  480. else if (strcmp(*argv,"-prexit") == 0)
  481. prexit=1;
  482. else if (strcmp(*argv,"-crlf") == 0)
  483. crlf=1;
  484. else if (strcmp(*argv,"-quiet") == 0)
  485. {
  486. c_quiet=1;
  487. c_ign_eof=1;
  488. }
  489. else if (strcmp(*argv,"-ign_eof") == 0)
  490. c_ign_eof=1;
  491. else if (strcmp(*argv,"-pause") == 0)
  492. c_Pause=1;
  493. else if (strcmp(*argv,"-debug") == 0)
  494. c_debug=1;
  495. #ifndef OPENSSL_NO_TLSEXT
  496. else if (strcmp(*argv,"-tlsextdebug") == 0)
  497. c_tlsextdebug=1;
  498. else if (strcmp(*argv,"-status") == 0)
  499. c_status_req=1;
  500. #endif
  501. #ifdef WATT32
  502. else if (strcmp(*argv,"-wdebug") == 0)
  503. dbug_init();
  504. #endif
  505. else if (strcmp(*argv,"-msg") == 0)
  506. c_msg=1;
  507. else if (strcmp(*argv,"-showcerts") == 0)
  508. c_showcerts=1;
  509. else if (strcmp(*argv,"-nbio_test") == 0)
  510. nbio_test=1;
  511. else if (strcmp(*argv,"-state") == 0)
  512. state=1;
  513. #ifndef OPENSSL_NO_PSK
  514. else if (strcmp(*argv,"-psk_identity") == 0)
  515. {
  516. if (--argc < 1) goto bad;
  517. psk_identity=*(++argv);
  518. }
  519. else if (strcmp(*argv,"-psk") == 0)
  520. {
  521. size_t j;
  522. if (--argc < 1) goto bad;
  523. psk_key=*(++argv);
  524. for (j = 0; j < strlen(psk_key); j++)
  525. {
  526. if (isxdigit((int)psk_key[j]))
  527. continue;
  528. BIO_printf(bio_err,"Not a hex number '%s'\n",*argv);
  529. goto bad;
  530. }
  531. }
  532. #endif
  533. #ifndef OPENSSL_NO_SSL2
  534. else if (strcmp(*argv,"-ssl2") == 0)
  535. meth=SSLv2_client_method();
  536. #endif
  537. #ifndef OPENSSL_NO_SSL3
  538. else if (strcmp(*argv,"-ssl3") == 0)
  539. meth=SSLv3_client_method();
  540. #endif
  541. #ifndef OPENSSL_NO_TLS1
  542. else if (strcmp(*argv,"-tls1") == 0)
  543. meth=TLSv1_client_method();
  544. #endif
  545. #ifndef OPENSSL_NO_DTLS1
  546. else if (strcmp(*argv,"-dtls1") == 0)
  547. {
  548. meth=DTLSv1_client_method();
  549. socket_type=SOCK_DGRAM;
  550. }
  551. else if (strcmp(*argv,"-timeout") == 0)
  552. enable_timeouts=1;
  553. else if (strcmp(*argv,"-mtu") == 0)
  554. {
  555. if (--argc < 1) goto bad;
  556. socket_mtu = atol(*(++argv));
  557. }
  558. #endif
  559. else if (strcmp(*argv,"-bugs") == 0)
  560. bugs=1;
  561. else if (strcmp(*argv,"-keyform") == 0)
  562. {
  563. if (--argc < 1) goto bad;
  564. key_format = str2fmt(*(++argv));
  565. }
  566. else if (strcmp(*argv,"-pass") == 0)
  567. {
  568. if (--argc < 1) goto bad;
  569. passarg = *(++argv);
  570. }
  571. else if (strcmp(*argv,"-key") == 0)
  572. {
  573. if (--argc < 1) goto bad;
  574. key_file= *(++argv);
  575. }
  576. else if (strcmp(*argv,"-reconnect") == 0)
  577. {
  578. reconnect=5;
  579. }
  580. else if (strcmp(*argv,"-CApath") == 0)
  581. {
  582. if (--argc < 1) goto bad;
  583. CApath= *(++argv);
  584. }
  585. else if (strcmp(*argv,"-CAfile") == 0)
  586. {
  587. if (--argc < 1) goto bad;
  588. CAfile= *(++argv);
  589. }
  590. else if (strcmp(*argv,"-no_tls1") == 0)
  591. off|=SSL_OP_NO_TLSv1;
  592. else if (strcmp(*argv,"-no_ssl3") == 0)
  593. off|=SSL_OP_NO_SSLv3;
  594. else if (strcmp(*argv,"-no_ssl2") == 0)
  595. off|=SSL_OP_NO_SSLv2;
  596. else if (strcmp(*argv,"-no_comp") == 0)
  597. { off|=SSL_OP_NO_COMPRESSION; }
  598. #ifndef OPENSSL_NO_TLSEXT
  599. else if (strcmp(*argv,"-no_ticket") == 0)
  600. { off|=SSL_OP_NO_TICKET; }
  601. #endif
  602. else if (strcmp(*argv,"-serverpref") == 0)
  603. off|=SSL_OP_CIPHER_SERVER_PREFERENCE;
  604. else if (strcmp(*argv,"-cipher") == 0)
  605. {
  606. if (--argc < 1) goto bad;
  607. cipher= *(++argv);
  608. }
  609. #ifdef FIONBIO
  610. else if (strcmp(*argv,"-nbio") == 0)
  611. { c_nbio=1; }
  612. #endif
  613. else if (strcmp(*argv,"-starttls") == 0)
  614. {
  615. if (--argc < 1) goto bad;
  616. ++argv;
  617. if (strcmp(*argv,"smtp") == 0)
  618. starttls_proto = PROTO_SMTP;
  619. else if (strcmp(*argv,"pop3") == 0)
  620. starttls_proto = PROTO_POP3;
  621. else if (strcmp(*argv,"imap") == 0)
  622. starttls_proto = PROTO_IMAP;
  623. else if (strcmp(*argv,"ftp") == 0)
  624. starttls_proto = PROTO_FTP;
  625. else
  626. goto bad;
  627. }
  628. #ifndef OPENSSL_NO_ENGINE
  629. else if (strcmp(*argv,"-engine") == 0)
  630. {
  631. if (--argc < 1) goto bad;
  632. engine_id = *(++argv);
  633. }
  634. #endif
  635. else if (strcmp(*argv,"-rand") == 0)
  636. {
  637. if (--argc < 1) goto bad;
  638. inrand= *(++argv);
  639. }
  640. #ifndef OPENSSL_NO_TLSEXT
  641. else if (strcmp(*argv,"-servername") == 0)
  642. {
  643. if (--argc < 1) goto bad;
  644. servername= *(++argv);
  645. /* meth=TLSv1_client_method(); */
  646. }
  647. #endif
  648. else
  649. {
  650. BIO_printf(bio_err,"unknown option %s\n",*argv);
  651. badop=1;
  652. break;
  653. }
  654. argc--;
  655. argv++;
  656. }
  657. if (badop)
  658. {
  659. bad:
  660. sc_usage();
  661. goto end;
  662. }
  663. OpenSSL_add_ssl_algorithms();
  664. SSL_load_error_strings();
  665. #ifndef OPENSSL_NO_ENGINE
  666. e = setup_engine(bio_err, engine_id, 1);
  667. #endif
  668. if (!app_passwd(bio_err, passarg, NULL, &pass, NULL))
  669. {
  670. BIO_printf(bio_err, "Error getting password\n");
  671. goto end;
  672. }
  673. if (key_file == NULL)
  674. key_file = cert_file;
  675. if (key_file)
  676. {
  677. key = load_key(bio_err, key_file, key_format, 0, pass, e,
  678. "client certificate private key file");
  679. if (!key)
  680. {
  681. ERR_print_errors(bio_err);
  682. goto end;
  683. }
  684. }
  685. if (cert_file)
  686. {
  687. cert = load_cert(bio_err,cert_file,cert_format,
  688. NULL, e, "client certificate file");
  689. if (!cert)
  690. {
  691. ERR_print_errors(bio_err);
  692. goto end;
  693. }
  694. }
  695. if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
  696. && !RAND_status())
  697. {
  698. BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
  699. }
  700. if (inrand != NULL)
  701. BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
  702. app_RAND_load_files(inrand));
  703. if (bio_c_out == NULL)
  704. {
  705. if (c_quiet && !c_debug && !c_msg)
  706. {
  707. bio_c_out=BIO_new(BIO_s_null());
  708. }
  709. else
  710. {
  711. if (bio_c_out == NULL)
  712. bio_c_out=BIO_new_fp(stdout,BIO_NOCLOSE);
  713. }
  714. }
  715. ctx=SSL_CTX_new(meth);
  716. if (ctx == NULL)
  717. {
  718. ERR_print_errors(bio_err);
  719. goto end;
  720. }
  721. #ifndef OPENSSL_NO_PSK
  722. if (psk_key != NULL)
  723. {
  724. if (c_debug)
  725. BIO_printf(bio_c_out, "PSK key given, setting client callback\n");
  726. SSL_CTX_set_psk_client_callback(ctx, psk_client_cb);
  727. }
  728. #endif
  729. if (bugs)
  730. SSL_CTX_set_options(ctx,SSL_OP_ALL|off);
  731. else
  732. SSL_CTX_set_options(ctx,off);
  733. /* DTLS: partial reads end up discarding unread UDP bytes :-(
  734. * Setting read ahead solves this problem.
  735. */
  736. if (socket_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx, 1);
  737. if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);
  738. if (cipher != NULL)
  739. if(!SSL_CTX_set_cipher_list(ctx,cipher)) {
  740. BIO_printf(bio_err,"error setting cipher list\n");
  741. ERR_print_errors(bio_err);
  742. goto end;
  743. }
  744. #if 0
  745. else
  746. SSL_CTX_set_cipher_list(ctx,getenv("SSL_CIPHER"));
  747. #endif
  748. SSL_CTX_set_verify(ctx,verify,verify_callback);
  749. if (!set_cert_key_stuff(ctx,cert,key))
  750. goto end;
  751. if ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) ||
  752. (!SSL_CTX_set_default_verify_paths(ctx)))
  753. {
  754. /* BIO_printf(bio_err,"error setting default verify locations\n"); */
  755. ERR_print_errors(bio_err);
  756. /* goto end; */
  757. }
  758. store = SSL_CTX_get_cert_store(ctx);
  759. X509_STORE_set_flags(store, vflags);
  760. #ifndef OPENSSL_NO_TLSEXT
  761. if (servername != NULL)
  762. {
  763. tlsextcbp.biodebug = bio_err;
  764. SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
  765. SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
  766. }
  767. #endif
  768. con=SSL_new(ctx);
  769. if (sess_in)
  770. {
  771. SSL_SESSION *sess;
  772. BIO *stmp = BIO_new_file(sess_in, "r");
  773. if (!stmp)
  774. {
  775. BIO_printf(bio_err, "Can't open session file %s\n",
  776. sess_in);
  777. ERR_print_errors(bio_err);
  778. goto end;
  779. }
  780. sess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);
  781. BIO_free(stmp);
  782. if (!sess)
  783. {
  784. BIO_printf(bio_err, "Can't open session file %s\n",
  785. sess_in);
  786. ERR_print_errors(bio_err);
  787. goto end;
  788. }
  789. SSL_set_session(con, sess);
  790. SSL_SESSION_free(sess);
  791. }
  792. #ifndef OPENSSL_NO_TLSEXT
  793. if (servername != NULL)
  794. {
  795. if (!SSL_set_tlsext_host_name(con,servername))
  796. {
  797. BIO_printf(bio_err,"Unable to set TLS servername extension.\n");
  798. ERR_print_errors(bio_err);
  799. goto end;
  800. }
  801. }
  802. #endif
  803. #ifndef OPENSSL_NO_KRB5
  804. if (con && (con->kssl_ctx = kssl_ctx_new()) != NULL)
  805. {
  806. kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVER, host);
  807. }
  808. #endif /* OPENSSL_NO_KRB5 */
  809. /* SSL_set_cipher_list(con,"RC4-MD5"); */
  810. #if 0
  811. #ifdef TLSEXT_TYPE_opaque_prf_input
  812. SSL_set_tlsext_opaque_prf_input(con, "Test client", 11);
  813. #endif
  814. #endif
  815. re_start:
  816. if (init_client(&s,host,port,socket_type) == 0)
  817. {
  818. BIO_printf(bio_err,"connect:errno=%d\n",get_last_socket_error());
  819. SHUTDOWN(s);
  820. goto end;
  821. }
  822. BIO_printf(bio_c_out,"CONNECTED(%08X)\n",s);
  823. #ifdef FIONBIO
  824. if (c_nbio)
  825. {
  826. unsigned long l=1;
  827. BIO_printf(bio_c_out,"turning on non blocking io\n");
  828. if (BIO_socket_ioctl(s,FIONBIO,&l) < 0)
  829. {
  830. ERR_print_errors(bio_err);
  831. goto end;
  832. }
  833. }
  834. #endif
  835. if (c_Pause & 0x01) con->debug=1;
  836. if ( SSL_version(con) == DTLS1_VERSION)
  837. {
  838. struct timeval timeout;
  839. sbio=BIO_new_dgram(s,BIO_NOCLOSE);
  840. if (getsockname(s, &peer, (void *)&peerlen) < 0)
  841. {
  842. BIO_printf(bio_err, "getsockname:errno=%d\n",
  843. get_last_socket_error());
  844. SHUTDOWN(s);
  845. goto end;
  846. }
  847. (void)BIO_ctrl_set_connected(sbio, 1, &peer);
  848. if (enable_timeouts)
  849. {
  850. timeout.tv_sec = 0;
  851. timeout.tv_usec = DGRAM_RCV_TIMEOUT;
  852. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
  853. timeout.tv_sec = 0;
  854. timeout.tv_usec = DGRAM_SND_TIMEOUT;
  855. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
  856. }
  857. if (socket_mtu > 0)
  858. {
  859. SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
  860. SSL_set_mtu(con, socket_mtu);
  861. }
  862. else
  863. /* want to do MTU discovery */
  864. BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
  865. }
  866. else
  867. sbio=BIO_new_socket(s,BIO_NOCLOSE);
  868. if (nbio_test)
  869. {
  870. BIO *test;
  871. test=BIO_new(BIO_f_nbio_test());
  872. sbio=BIO_push(test,sbio);
  873. }
  874. if (c_debug)
  875. {
  876. con->debug=1;
  877. BIO_set_callback(sbio,bio_dump_callback);
  878. BIO_set_callback_arg(sbio,(char *)bio_c_out);
  879. }
  880. if (c_msg)
  881. {
  882. SSL_set_msg_callback(con, msg_cb);
  883. SSL_set_msg_callback_arg(con, bio_c_out);
  884. }
  885. #ifndef OPENSSL_NO_TLSEXT
  886. if (c_tlsextdebug)
  887. {
  888. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  889. SSL_set_tlsext_debug_arg(con, bio_c_out);
  890. }
  891. if (c_status_req)
  892. {
  893. SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp);
  894. SSL_CTX_set_tlsext_status_cb(ctx, ocsp_resp_cb);
  895. SSL_CTX_set_tlsext_status_arg(ctx, bio_c_out);
  896. #if 0
  897. {
  898. STACK_OF(OCSP_RESPID) *ids = sk_OCSP_RESPID_new_null();
  899. OCSP_RESPID *id = OCSP_RESPID_new();
  900. id->value.byKey = ASN1_OCTET_STRING_new();
  901. id->type = V_OCSP_RESPID_KEY;
  902. ASN1_STRING_set(id->value.byKey, "Hello World", -1);
  903. sk_OCSP_RESPID_push(ids, id);
  904. SSL_set_tlsext_status_ids(con, ids);
  905. }
  906. #endif
  907. }
  908. #endif
  909. SSL_set_bio(con,sbio,sbio);
  910. SSL_set_connect_state(con);
  911. /* ok, lets connect */
  912. width=SSL_get_fd(con)+1;
  913. read_tty=1;
  914. write_tty=0;
  915. tty_on=0;
  916. read_ssl=1;
  917. write_ssl=1;
  918. cbuf_len=0;
  919. cbuf_off=0;
  920. sbuf_len=0;
  921. sbuf_off=0;
  922. /* This is an ugly hack that does a lot of assumptions */
  923. /* We do have to handle multi-line responses which may come
  924. in a single packet or not. We therefore have to use
  925. BIO_gets() which does need a buffering BIO. So during
  926. the initial chitchat we do push a buffering BIO into the
  927. chain that is removed again later on to not disturb the
  928. rest of the s_client operation. */
  929. if (starttls_proto == PROTO_SMTP)
  930. {
  931. int foundit=0;
  932. BIO *fbio = BIO_new(BIO_f_buffer());
  933. BIO_push(fbio, sbio);
  934. /* wait for multi-line response to end from SMTP */
  935. do
  936. {
  937. mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ);
  938. }
  939. while (mbuf_len>3 && mbuf[3]=='-');
  940. /* STARTTLS command requires EHLO... */
  941. BIO_printf(fbio,"EHLO openssl.client.net\r\n");
  942. (void)BIO_flush(fbio);
  943. /* wait for multi-line response to end EHLO SMTP response */
  944. do
  945. {
  946. mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ);
  947. if (strstr(mbuf,"STARTTLS"))
  948. foundit=1;
  949. }
  950. while (mbuf_len>3 && mbuf[3]=='-');
  951. (void)BIO_flush(fbio);
  952. BIO_pop(fbio);
  953. BIO_free(fbio);
  954. if (!foundit)
  955. BIO_printf(bio_err,
  956. "didn't found starttls in server response,"
  957. " try anyway...\n");
  958. BIO_printf(sbio,"STARTTLS\r\n");
  959. BIO_read(sbio,sbuf,BUFSIZZ);
  960. }
  961. else if (starttls_proto == PROTO_POP3)
  962. {
  963. BIO_read(sbio,mbuf,BUFSIZZ);
  964. BIO_printf(sbio,"STLS\r\n");
  965. BIO_read(sbio,sbuf,BUFSIZZ);
  966. }
  967. else if (starttls_proto == PROTO_IMAP)
  968. {
  969. int foundit=0;
  970. BIO *fbio = BIO_new(BIO_f_buffer());
  971. BIO_push(fbio, sbio);
  972. BIO_gets(fbio,mbuf,BUFSIZZ);
  973. /* STARTTLS command requires CAPABILITY... */
  974. BIO_printf(fbio,". CAPABILITY\r\n");
  975. (void)BIO_flush(fbio);
  976. /* wait for multi-line CAPABILITY response */
  977. do
  978. {
  979. mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ);
  980. if (strstr(mbuf,"STARTTLS"))
  981. foundit=1;
  982. }
  983. while (mbuf_len>3 && mbuf[0]!='.');
  984. (void)BIO_flush(fbio);
  985. BIO_pop(fbio);
  986. BIO_free(fbio);
  987. if (!foundit)
  988. BIO_printf(bio_err,
  989. "didn't found STARTTLS in server response,"
  990. " try anyway...\n");
  991. BIO_printf(sbio,". STARTTLS\r\n");
  992. BIO_read(sbio,sbuf,BUFSIZZ);
  993. }
  994. else if (starttls_proto == PROTO_FTP)
  995. {
  996. BIO *fbio = BIO_new(BIO_f_buffer());
  997. BIO_push(fbio, sbio);
  998. /* wait for multi-line response to end from FTP */
  999. do
  1000. {
  1001. mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ);
  1002. }
  1003. while (mbuf_len>3 && mbuf[3]=='-');
  1004. (void)BIO_flush(fbio);
  1005. BIO_pop(fbio);
  1006. BIO_free(fbio);
  1007. BIO_printf(sbio,"AUTH TLS\r\n");
  1008. BIO_read(sbio,sbuf,BUFSIZZ);
  1009. }
  1010. for (;;)
  1011. {
  1012. FD_ZERO(&readfds);
  1013. FD_ZERO(&writefds);
  1014. if (SSL_in_init(con) && !SSL_total_renegotiations(con))
  1015. {
  1016. in_init=1;
  1017. tty_on=0;
  1018. }
  1019. else
  1020. {
  1021. tty_on=1;
  1022. if (in_init)
  1023. {
  1024. in_init=0;
  1025. #if 0 /* This test doesn't really work as intended (needs to be fixed) */
  1026. #ifndef OPENSSL_NO_TLSEXT
  1027. if (servername != NULL && !SSL_session_reused(con))
  1028. {
  1029. BIO_printf(bio_c_out,"Server did %sacknowledge servername extension.\n",tlsextcbp.ack?"":"not ");
  1030. }
  1031. #endif
  1032. #endif
  1033. if (sess_out)
  1034. {
  1035. BIO *stmp = BIO_new_file(sess_out, "w");
  1036. if (stmp)
  1037. {
  1038. PEM_write_bio_SSL_SESSION(stmp, SSL_get_session(con));
  1039. BIO_free(stmp);
  1040. }
  1041. else
  1042. BIO_printf(bio_err, "Error writing session file %s\n", sess_out);
  1043. }
  1044. print_stuff(bio_c_out,con,full_log);
  1045. if (full_log > 0) full_log--;
  1046. if (starttls_proto)
  1047. {
  1048. BIO_printf(bio_err,"%s",mbuf);
  1049. /* We don't need to know any more */
  1050. starttls_proto = PROTO_OFF;
  1051. }
  1052. if (reconnect)
  1053. {
  1054. reconnect--;
  1055. BIO_printf(bio_c_out,"drop connection and then reconnect\n");
  1056. SSL_shutdown(con);
  1057. SSL_set_connect_state(con);
  1058. SHUTDOWN(SSL_get_fd(con));
  1059. goto re_start;
  1060. }
  1061. }
  1062. }
  1063. ssl_pending = read_ssl && SSL_pending(con);
  1064. if (!ssl_pending)
  1065. {
  1066. #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) && !defined (OPENSSL_SYS_BEOS_R5)
  1067. if (tty_on)
  1068. {
  1069. if (read_tty) openssl_fdset(fileno(stdin),&readfds);
  1070. if (write_tty) openssl_fdset(fileno(stdout),&writefds);
  1071. }
  1072. if (read_ssl)
  1073. openssl_fdset(SSL_get_fd(con),&readfds);
  1074. if (write_ssl)
  1075. openssl_fdset(SSL_get_fd(con),&writefds);
  1076. #else
  1077. if(!tty_on || !write_tty) {
  1078. if (read_ssl)
  1079. openssl_fdset(SSL_get_fd(con),&readfds);
  1080. if (write_ssl)
  1081. openssl_fdset(SSL_get_fd(con),&writefds);
  1082. }
  1083. #endif
  1084. /* printf("mode tty(%d %d%d) ssl(%d%d)\n",
  1085. tty_on,read_tty,write_tty,read_ssl,write_ssl);*/
  1086. /* Note: under VMS with SOCKETSHR the second parameter
  1087. * is currently of type (int *) whereas under other
  1088. * systems it is (void *) if you don't have a cast it
  1089. * will choke the compiler: if you do have a cast then
  1090. * you can either go for (int *) or (void *).
  1091. */
  1092. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
  1093. /* Under Windows/DOS we make the assumption that we can
  1094. * always write to the tty: therefore if we need to
  1095. * write to the tty we just fall through. Otherwise
  1096. * we timeout the select every second and see if there
  1097. * are any keypresses. Note: this is a hack, in a proper
  1098. * Windows application we wouldn't do this.
  1099. */
  1100. i=0;
  1101. if(!write_tty) {
  1102. if(read_tty) {
  1103. tv.tv_sec = 1;
  1104. tv.tv_usec = 0;
  1105. i=select(width,(void *)&readfds,(void *)&writefds,
  1106. NULL,&tv);
  1107. #if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
  1108. if(!i && (!_kbhit() || !read_tty) ) continue;
  1109. #else
  1110. if(!i && (!((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0))) || !read_tty) ) continue;
  1111. #endif
  1112. } else i=select(width,(void *)&readfds,(void *)&writefds,
  1113. NULL,NULL);
  1114. }
  1115. #elif defined(OPENSSL_SYS_NETWARE)
  1116. if(!write_tty) {
  1117. if(read_tty) {
  1118. tv.tv_sec = 1;
  1119. tv.tv_usec = 0;
  1120. i=select(width,(void *)&readfds,(void *)&writefds,
  1121. NULL,&tv);
  1122. } else i=select(width,(void *)&readfds,(void *)&writefds,
  1123. NULL,NULL);
  1124. }
  1125. #elif defined(OPENSSL_SYS_BEOS_R5)
  1126. /* Under BeOS-R5 the situation is similar to DOS */
  1127. i=0;
  1128. stdin_set = 0;
  1129. (void)fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
  1130. if(!write_tty) {
  1131. if(read_tty) {
  1132. tv.tv_sec = 1;
  1133. tv.tv_usec = 0;
  1134. i=select(width,(void *)&readfds,(void *)&writefds,
  1135. NULL,&tv);
  1136. if (read(fileno(stdin), sbuf, 0) >= 0)
  1137. stdin_set = 1;
  1138. if (!i && (stdin_set != 1 || !read_tty))
  1139. continue;
  1140. } else i=select(width,(void *)&readfds,(void *)&writefds,
  1141. NULL,NULL);
  1142. }
  1143. (void)fcntl(fileno(stdin), F_SETFL, 0);
  1144. #else
  1145. i=select(width,(void *)&readfds,(void *)&writefds,
  1146. NULL,NULL);
  1147. #endif
  1148. if ( i < 0)
  1149. {
  1150. BIO_printf(bio_err,"bad select %d\n",
  1151. get_last_socket_error());
  1152. goto shut;
  1153. /* goto end; */
  1154. }
  1155. }
  1156. if (!ssl_pending && FD_ISSET(SSL_get_fd(con),&writefds))
  1157. {
  1158. k=SSL_write(con,&(cbuf[cbuf_off]),
  1159. (unsigned int)cbuf_len);
  1160. switch (SSL_get_error(con,k))
  1161. {
  1162. case SSL_ERROR_NONE:
  1163. cbuf_off+=k;
  1164. cbuf_len-=k;
  1165. if (k <= 0) goto end;
  1166. /* we have done a write(con,NULL,0); */
  1167. if (cbuf_len <= 0)
  1168. {
  1169. read_tty=1;
  1170. write_ssl=0;
  1171. }
  1172. else /* if (cbuf_len > 0) */
  1173. {
  1174. read_tty=0;
  1175. write_ssl=1;
  1176. }
  1177. break;
  1178. case SSL_ERROR_WANT_WRITE:
  1179. BIO_printf(bio_c_out,"write W BLOCK\n");
  1180. write_ssl=1;
  1181. read_tty=0;
  1182. break;
  1183. case SSL_ERROR_WANT_READ:
  1184. BIO_printf(bio_c_out,"write R BLOCK\n");
  1185. write_tty=0;
  1186. read_ssl=1;
  1187. write_ssl=0;
  1188. break;
  1189. case SSL_ERROR_WANT_X509_LOOKUP:
  1190. BIO_printf(bio_c_out,"write X BLOCK\n");
  1191. break;
  1192. case SSL_ERROR_ZERO_RETURN:
  1193. if (cbuf_len != 0)
  1194. {
  1195. BIO_printf(bio_c_out,"shutdown\n");
  1196. ret = 0;
  1197. goto shut;
  1198. }
  1199. else
  1200. {
  1201. read_tty=1;
  1202. write_ssl=0;
  1203. break;
  1204. }
  1205. case SSL_ERROR_SYSCALL:
  1206. if ((k != 0) || (cbuf_len != 0))
  1207. {
  1208. BIO_printf(bio_err,"write:errno=%d\n",
  1209. get_last_socket_error());
  1210. goto shut;
  1211. }
  1212. else
  1213. {
  1214. read_tty=1;
  1215. write_ssl=0;
  1216. }
  1217. break;
  1218. case SSL_ERROR_SSL:
  1219. ERR_print_errors(bio_err);
  1220. goto shut;
  1221. }
  1222. }
  1223. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)
  1224. /* Assume Windows/DOS/BeOS can always write */
  1225. else if (!ssl_pending && write_tty)
  1226. #else
  1227. else if (!ssl_pending && FD_ISSET(fileno(stdout),&writefds))
  1228. #endif
  1229. {
  1230. #ifdef CHARSET_EBCDIC
  1231. ascii2ebcdic(&(sbuf[sbuf_off]),&(sbuf[sbuf_off]),sbuf_len);
  1232. #endif
  1233. i=raw_write_stdout(&(sbuf[sbuf_off]),sbuf_len);
  1234. if (i <= 0)
  1235. {
  1236. BIO_printf(bio_c_out,"DONE\n");
  1237. ret = 0;
  1238. goto shut;
  1239. /* goto end; */
  1240. }
  1241. sbuf_len-=i;;
  1242. sbuf_off+=i;
  1243. if (sbuf_len <= 0)
  1244. {
  1245. read_ssl=1;
  1246. write_tty=0;
  1247. }
  1248. }
  1249. else if (ssl_pending || FD_ISSET(SSL_get_fd(con),&readfds))
  1250. {
  1251. #ifdef RENEG
  1252. { static int iiii; if (++iiii == 52) { SSL_renegotiate(con); iiii=0; } }
  1253. #endif
  1254. #if 1
  1255. k=SSL_read(con,sbuf,1024 /* BUFSIZZ */ );
  1256. #else
  1257. /* Demo for pending and peek :-) */
  1258. k=SSL_read(con,sbuf,16);
  1259. { char zbuf[10240];
  1260. printf("read=%d pending=%d peek=%d\n",k,SSL_pending(con),SSL_peek(con,zbuf,10240));
  1261. }
  1262. #endif
  1263. switch (SSL_get_error(con,k))
  1264. {
  1265. case SSL_ERROR_NONE:
  1266. if (k <= 0)
  1267. goto end;
  1268. sbuf_off=0;
  1269. sbuf_len=k;
  1270. read_ssl=0;
  1271. write_tty=1;
  1272. break;
  1273. case SSL_ERROR_WANT_WRITE:
  1274. BIO_printf(bio_c_out,"read W BLOCK\n");
  1275. write_ssl=1;
  1276. read_tty=0;
  1277. break;
  1278. case SSL_ERROR_WANT_READ:
  1279. BIO_printf(bio_c_out,"read R BLOCK\n");
  1280. write_tty=0;
  1281. read_ssl=1;
  1282. if ((read_tty == 0) && (write_ssl == 0))
  1283. write_ssl=1;
  1284. break;
  1285. case SSL_ERROR_WANT_X509_LOOKUP:
  1286. BIO_printf(bio_c_out,"read X BLOCK\n");
  1287. break;
  1288. case SSL_ERROR_SYSCALL:
  1289. ret=get_last_socket_error();
  1290. BIO_printf(bio_err,"read:errno=%d\n",ret);
  1291. goto shut;
  1292. case SSL_ERROR_ZERO_RETURN:
  1293. BIO_printf(bio_c_out,"closed\n");
  1294. ret=0;
  1295. goto shut;
  1296. case SSL_ERROR_SSL:
  1297. ERR_print_errors(bio_err);
  1298. goto shut;
  1299. /* break; */
  1300. }
  1301. }
  1302. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
  1303. #if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
  1304. else if (_kbhit())
  1305. #else
  1306. else if ((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0)))
  1307. #endif
  1308. #elif defined (OPENSSL_SYS_NETWARE)
  1309. else if (_kbhit())
  1310. #elif defined(OPENSSL_SYS_BEOS_R5)
  1311. else if (stdin_set)
  1312. #else
  1313. else if (FD_ISSET(fileno(stdin),&readfds))
  1314. #endif
  1315. {
  1316. if (crlf)
  1317. {
  1318. int j, lf_num;
  1319. i=raw_read_stdin(cbuf,BUFSIZZ/2);
  1320. lf_num = 0;
  1321. /* both loops are skipped when i <= 0 */
  1322. for (j = 0; j < i; j++)
  1323. if (cbuf[j] == '\n')
  1324. lf_num++;
  1325. for (j = i-1; j >= 0; j--)
  1326. {
  1327. cbuf[j+lf_num] = cbuf[j];
  1328. if (cbuf[j] == '\n')
  1329. {
  1330. lf_num--;
  1331. i++;
  1332. cbuf[j+lf_num] = '\r';
  1333. }
  1334. }
  1335. assert(lf_num == 0);
  1336. }
  1337. else
  1338. i=raw_read_stdin(cbuf,BUFSIZZ);
  1339. if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q')))
  1340. {
  1341. BIO_printf(bio_err,"DONE\n");
  1342. ret=0;
  1343. goto shut;
  1344. }
  1345. if ((!c_ign_eof) && (cbuf[0] == 'R'))
  1346. {
  1347. BIO_printf(bio_err,"RENEGOTIATING\n");
  1348. SSL_renegotiate(con);
  1349. cbuf_len=0;
  1350. }
  1351. else
  1352. {
  1353. cbuf_len=i;
  1354. cbuf_off=0;
  1355. #ifdef CHARSET_EBCDIC
  1356. ebcdic2ascii(cbuf, cbuf, i);
  1357. #endif
  1358. }
  1359. write_ssl=1;
  1360. read_tty=0;
  1361. }
  1362. }
  1363. ret=0;
  1364. shut:
  1365. if (in_init)
  1366. print_stuff(bio_c_out,con,full_log);
  1367. SSL_shutdown(con);
  1368. SHUTDOWN(SSL_get_fd(con));
  1369. end:
  1370. if (con != NULL)
  1371. {
  1372. if (prexit != 0)
  1373. print_stuff(bio_c_out,con,1);
  1374. SSL_free(con);
  1375. }
  1376. if (ctx != NULL) SSL_CTX_free(ctx);
  1377. if (cert)
  1378. X509_free(cert);
  1379. if (key)
  1380. EVP_PKEY_free(key);
  1381. if (pass)
  1382. OPENSSL_free(pass);
  1383. if (cbuf != NULL) { OPENSSL_cleanse(cbuf,BUFSIZZ); OPENSSL_free(cbuf); }
  1384. if (sbuf != NULL) { OPENSSL_cleanse(sbuf,BUFSIZZ); OPENSSL_free(sbuf); }
  1385. if (mbuf != NULL) { OPENSSL_cleanse(mbuf,BUFSIZZ); OPENSSL_free(mbuf); }
  1386. if (bio_c_out != NULL)
  1387. {
  1388. BIO_free(bio_c_out);
  1389. bio_c_out=NULL;
  1390. }
  1391. apps_shutdown();
  1392. OPENSSL_EXIT(ret);
  1393. }
  1394. static void print_stuff(BIO *bio, SSL *s, int full)
  1395. {
  1396. X509 *peer=NULL;
  1397. char *p;
  1398. static const char *space=" ";
  1399. char buf[BUFSIZ];
  1400. STACK_OF(X509) *sk;
  1401. STACK_OF(X509_NAME) *sk2;
  1402. SSL_CIPHER *c;
  1403. X509_NAME *xn;
  1404. int j,i;
  1405. #ifndef OPENSSL_NO_COMP
  1406. const COMP_METHOD *comp, *expansion;
  1407. #endif
  1408. if (full)
  1409. {
  1410. int got_a_chain = 0;
  1411. sk=SSL_get_peer_cert_chain(s);
  1412. if (sk != NULL)
  1413. {
  1414. got_a_chain = 1; /* we don't have it for SSL2 (yet) */
  1415. BIO_printf(bio,"---\nCertificate chain\n");
  1416. for (i=0; i<sk_X509_num(sk); i++)
  1417. {
  1418. X509_NAME_oneline(X509_get_subject_name(
  1419. sk_X509_value(sk,i)),buf,sizeof buf);
  1420. BIO_printf(bio,"%2d s:%s\n",i,buf);
  1421. X509_NAME_oneline(X509_get_issuer_name(
  1422. sk_X509_value(sk,i)),buf,sizeof buf);
  1423. BIO_printf(bio," i:%s\n",buf);
  1424. if (c_showcerts)
  1425. PEM_write_bio_X509(bio,sk_X509_value(sk,i));
  1426. }
  1427. }
  1428. BIO_printf(bio,"---\n");
  1429. peer=SSL_get_peer_certificate(s);
  1430. if (peer != NULL)
  1431. {
  1432. BIO_printf(bio,"Server certificate\n");
  1433. if (!(c_showcerts && got_a_chain)) /* Redundant if we showed the whole chain */
  1434. PEM_write_bio_X509(bio,peer);
  1435. X509_NAME_oneline(X509_get_subject_name(peer),
  1436. buf,sizeof buf);
  1437. BIO_printf(bio,"subject=%s\n",buf);
  1438. X509_NAME_oneline(X509_get_issuer_name(peer),
  1439. buf,sizeof buf);
  1440. BIO_printf(bio,"issuer=%s\n",buf);
  1441. }
  1442. else
  1443. BIO_printf(bio,"no peer certificate available\n");
  1444. sk2=SSL_get_client_CA_list(s);
  1445. if ((sk2 != NULL) && (sk_X509_NAME_num(sk2) > 0))
  1446. {
  1447. BIO_printf(bio,"---\nAcceptable client certificate CA names\n");
  1448. for (i=0; i<sk_X509_NAME_num(sk2); i++)
  1449. {
  1450. xn=sk_X509_NAME_value(sk2,i);
  1451. X509_NAME_oneline(xn,buf,sizeof(buf));
  1452. BIO_write(bio,buf,strlen(buf));
  1453. BIO_write(bio,"\n",1);
  1454. }
  1455. }
  1456. else
  1457. {
  1458. BIO_printf(bio,"---\nNo client certificate CA names sent\n");
  1459. }
  1460. p=SSL_get_shared_ciphers(s,buf,sizeof buf);
  1461. if (p != NULL)
  1462. {
  1463. /* This works only for SSL 2. In later protocol
  1464. * versions, the client does not know what other
  1465. * ciphers (in addition to the one to be used
  1466. * in the current connection) the server supports. */
  1467. BIO_printf(bio,"---\nCiphers common between both SSL endpoints:\n");
  1468. j=i=0;
  1469. while (*p)
  1470. {
  1471. if (*p == ':')
  1472. {
  1473. BIO_write(bio,space,15-j%25);
  1474. i++;
  1475. j=0;
  1476. BIO_write(bio,((i%3)?" ":"\n"),1);
  1477. }
  1478. else
  1479. {
  1480. BIO_write(bio,p,1);
  1481. j++;
  1482. }
  1483. p++;
  1484. }
  1485. BIO_write(bio,"\n",1);
  1486. }
  1487. BIO_printf(bio,"---\nSSL handshake has read %ld bytes and written %ld bytes\n",
  1488. BIO_number_read(SSL_get_rbio(s)),
  1489. BIO_number_written(SSL_get_wbio(s)));
  1490. }
  1491. BIO_printf(bio,((s->hit)?"---\nReused, ":"---\nNew, "));
  1492. c=SSL_get_current_cipher(s);
  1493. BIO_printf(bio,"%s, Cipher is %s\n",
  1494. SSL_CIPHER_get_version(c),
  1495. SSL_CIPHER_get_name(c));
  1496. if (peer != NULL) {
  1497. EVP_PKEY *pktmp;
  1498. pktmp = X509_get_pubkey(peer);
  1499. BIO_printf(bio,"Server public key is %d bit\n",
  1500. EVP_PKEY_bits(pktmp));
  1501. EVP_PKEY_free(pktmp);
  1502. }
  1503. #ifndef OPENSSL_NO_COMP
  1504. comp=SSL_get_current_compression(s);
  1505. expansion=SSL_get_current_expansion(s);
  1506. BIO_printf(bio,"Compression: %s\n",
  1507. comp ? SSL_COMP_get_name(comp) : "NONE");
  1508. BIO_printf(bio,"Expansion: %s\n",
  1509. expansion ? SSL_COMP_get_name(expansion) : "NONE");
  1510. #endif
  1511. SSL_SESSION_print(bio,SSL_get_session(s));
  1512. BIO_printf(bio,"---\n");
  1513. if (peer != NULL)
  1514. X509_free(peer);
  1515. /* flush, or debugging output gets mixed with http response */
  1516. (void)BIO_flush(bio);
  1517. }
  1518. static int ocsp_resp_cb(SSL *s, void *arg)
  1519. {
  1520. const unsigned char *p;
  1521. int len;
  1522. OCSP_RESPONSE *rsp;
  1523. len = SSL_get_tlsext_status_ocsp_resp(s, &p);
  1524. BIO_puts(arg, "OCSP response: ");
  1525. if (!p)
  1526. {
  1527. BIO_puts(arg, "no response sent\n");
  1528. return 1;
  1529. }
  1530. rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
  1531. if (!rsp)
  1532. {
  1533. BIO_puts(arg, "response parse error\n");
  1534. BIO_dump_indent(arg, (char *)p, len, 4);
  1535. return 0;
  1536. }
  1537. BIO_puts(arg, "\n======================================\n");
  1538. OCSP_RESPONSE_print(arg, rsp, 0);
  1539. BIO_puts(arg, "======================================\n");
  1540. OCSP_RESPONSE_free(rsp);
  1541. return 1;
  1542. }