s_server.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660
  1. /* apps/s_server.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 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  113. * ECC cipher suite support in OpenSSL originally developed by
  114. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  115. */
  116. /* ====================================================================
  117. * Copyright 2005 Nokia. All rights reserved.
  118. *
  119. * The portions of the attached software ("Contribution") is developed by
  120. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  121. * license.
  122. *
  123. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  124. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  125. * support (see RFC 4279) to OpenSSL.
  126. *
  127. * No patent licenses or other rights except those expressly stated in
  128. * the OpenSSL open source license shall be deemed granted or received
  129. * expressly, by implication, estoppel, or otherwise.
  130. *
  131. * No assurances are provided by Nokia that the Contribution does not
  132. * infringe the patent or other intellectual property rights of any third
  133. * party or that the license provides you with all the necessary rights
  134. * to make use of the Contribution.
  135. *
  136. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  137. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  138. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  139. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  140. * OTHERWISE.
  141. */
  142. /* Until the key-gen callbacks are modified to use newer prototypes, we allow
  143. * deprecated functions for openssl-internal code */
  144. #ifdef OPENSSL_NO_DEPRECATED
  145. #undef OPENSSL_NO_DEPRECATED
  146. #endif
  147. #include <assert.h>
  148. #include <ctype.h>
  149. #include <stdio.h>
  150. #include <stdlib.h>
  151. #include <string.h>
  152. #include <openssl/e_os2.h>
  153. #ifdef OPENSSL_NO_STDIO
  154. #define APPS_WIN16
  155. #endif
  156. #if !defined(OPENSSL_SYS_NETWARE) /* conflicts with winsock2 stuff on netware */
  157. #include <sys/types.h>
  158. #endif
  159. /* With IPv6, it looks like Digital has mixed up the proper order of
  160. recursive header file inclusion, resulting in the compiler complaining
  161. that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
  162. is needed to have fileno() declared correctly... So let's define u_int */
  163. #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
  164. #define __U_INT
  165. typedef unsigned int u_int;
  166. #endif
  167. #include <openssl/lhash.h>
  168. #include <openssl/bn.h>
  169. #define USE_SOCKETS
  170. #include "apps.h"
  171. #include <openssl/err.h>
  172. #include <openssl/pem.h>
  173. #include <openssl/x509.h>
  174. #include <openssl/ssl.h>
  175. #include <openssl/rand.h>
  176. #include <openssl/ocsp.h>
  177. #ifndef OPENSSL_NO_DH
  178. #include <openssl/dh.h>
  179. #endif
  180. #ifndef OPENSSL_NO_RSA
  181. #include <openssl/rsa.h>
  182. #endif
  183. #include "s_apps.h"
  184. #include "timeouts.h"
  185. #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
  186. /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
  187. #undef FIONBIO
  188. #endif
  189. #if defined(OPENSSL_SYS_BEOS_R5)
  190. #include <fcntl.h>
  191. #endif
  192. #ifndef OPENSSL_NO_RSA
  193. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength);
  194. #endif
  195. static int sv_body(char *hostname, int s, unsigned char *context);
  196. static int www_body(char *hostname, int s, unsigned char *context);
  197. static void close_accept_socket(void );
  198. static void sv_usage(void);
  199. static int init_ssl_connection(SSL *s);
  200. static void print_stats(BIO *bp,SSL_CTX *ctx);
  201. static int generate_session_id(const SSL *ssl, unsigned char *id,
  202. unsigned int *id_len);
  203. #ifndef OPENSSL_NO_DH
  204. static DH *load_dh_param(const char *dhfile);
  205. static DH *get_dh512(void);
  206. #endif
  207. #ifdef MONOLITH
  208. static void s_server_init(void);
  209. #endif
  210. #ifndef OPENSSL_NO_DH
  211. static unsigned char dh512_p[]={
  212. 0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
  213. 0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
  214. 0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3,
  215. 0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12,
  216. 0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C,
  217. 0x47,0x74,0xE8,0x33,
  218. };
  219. static unsigned char dh512_g[]={
  220. 0x02,
  221. };
  222. static DH *get_dh512(void)
  223. {
  224. DH *dh=NULL;
  225. if ((dh=DH_new()) == NULL) return(NULL);
  226. dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
  227. dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
  228. if ((dh->p == NULL) || (dh->g == NULL))
  229. return(NULL);
  230. return(dh);
  231. }
  232. #endif
  233. /* static int load_CA(SSL_CTX *ctx, char *file);*/
  234. #undef BUFSIZZ
  235. #define BUFSIZZ 16*1024
  236. static int bufsize=BUFSIZZ;
  237. static int accept_socket= -1;
  238. #define TEST_CERT "server.pem"
  239. #ifndef OPENSSL_NO_TLSEXT
  240. #define TEST_CERT2 "server2.pem"
  241. #endif
  242. #undef PROG
  243. #define PROG s_server_main
  244. extern int verify_depth, verify_return_error;
  245. static char *cipher=NULL;
  246. static int s_server_verify=SSL_VERIFY_NONE;
  247. static int s_server_session_id_context = 1; /* anything will do */
  248. static const char *s_cert_file=TEST_CERT,*s_key_file=NULL;
  249. #ifndef OPENSSL_NO_TLSEXT
  250. static const char *s_cert_file2=TEST_CERT2,*s_key_file2=NULL;
  251. #endif
  252. static char *s_dcert_file=NULL,*s_dkey_file=NULL;
  253. #ifdef FIONBIO
  254. static int s_nbio=0;
  255. #endif
  256. static int s_nbio_test=0;
  257. int s_crlf=0;
  258. static SSL_CTX *ctx=NULL;
  259. #ifndef OPENSSL_NO_TLSEXT
  260. static SSL_CTX *ctx2=NULL;
  261. #endif
  262. static int www=0;
  263. static BIO *bio_s_out=NULL;
  264. static int s_debug=0;
  265. #ifndef OPENSSL_NO_TLSEXT
  266. static int s_tlsextdebug=0;
  267. static int s_tlsextstatus=0;
  268. static int cert_status_cb(SSL *s, void *arg);
  269. #endif
  270. static int s_msg=0;
  271. static int s_quiet=0;
  272. static int hack=0;
  273. #ifndef OPENSSL_NO_ENGINE
  274. static char *engine_id=NULL;
  275. #endif
  276. static const char *session_id_prefix=NULL;
  277. static int enable_timeouts = 0;
  278. static long socket_mtu;
  279. static int cert_chain = 0;
  280. #ifndef OPENSSL_NO_PSK
  281. static char *psk_identity="Client_identity";
  282. char *psk_key=NULL; /* by default PSK is not used */
  283. static unsigned int psk_server_cb(SSL *ssl, const char *identity,
  284. unsigned char *psk, unsigned int max_psk_len)
  285. {
  286. unsigned int psk_len = 0;
  287. int ret;
  288. BIGNUM *bn = NULL;
  289. if (s_debug)
  290. BIO_printf(bio_s_out,"psk_server_cb\n");
  291. if (!identity)
  292. {
  293. BIO_printf(bio_err,"Error: client did not send PSK identity\n");
  294. goto out_err;
  295. }
  296. if (s_debug)
  297. BIO_printf(bio_s_out,"identity_len=%d identity=%s\n",
  298. identity ? (int)strlen(identity) : 0, identity);
  299. /* here we could lookup the given identity e.g. from a database */
  300. if (strcmp(identity, psk_identity) != 0)
  301. {
  302. BIO_printf(bio_s_out, "PSK error: client identity not found"
  303. " (got '%s' expected '%s')\n", identity,
  304. psk_identity);
  305. goto out_err;
  306. }
  307. if (s_debug)
  308. BIO_printf(bio_s_out, "PSK client identity found\n");
  309. /* convert the PSK key to binary */
  310. ret = BN_hex2bn(&bn, psk_key);
  311. if (!ret)
  312. {
  313. BIO_printf(bio_err,"Could not convert PSK key '%s' to BIGNUM\n", psk_key);
  314. if (bn)
  315. BN_free(bn);
  316. return 0;
  317. }
  318. if (BN_num_bytes(bn) > (int)max_psk_len)
  319. {
  320. BIO_printf(bio_err,"psk buffer of callback is too small (%d) for key (%d)\n",
  321. max_psk_len, BN_num_bytes(bn));
  322. BN_free(bn);
  323. return 0;
  324. }
  325. ret = BN_bn2bin(bn, psk);
  326. BN_free(bn);
  327. if (ret < 0)
  328. goto out_err;
  329. psk_len = (unsigned int)ret;
  330. if (s_debug)
  331. BIO_printf(bio_s_out, "fetched PSK len=%d\n", psk_len);
  332. return psk_len;
  333. out_err:
  334. if (s_debug)
  335. BIO_printf(bio_err, "Error in PSK server callback\n");
  336. return 0;
  337. }
  338. #endif
  339. #ifdef MONOLITH
  340. static void s_server_init(void)
  341. {
  342. accept_socket=-1;
  343. cipher=NULL;
  344. s_server_verify=SSL_VERIFY_NONE;
  345. s_dcert_file=NULL;
  346. s_dkey_file=NULL;
  347. s_cert_file=TEST_CERT;
  348. s_key_file=NULL;
  349. #ifndef OPENSSL_NO_TLSEXT
  350. s_cert_file2=TEST_CERT2;
  351. s_key_file2=NULL;
  352. ctx2=NULL;
  353. #endif
  354. #ifdef FIONBIO
  355. s_nbio=0;
  356. #endif
  357. s_nbio_test=0;
  358. ctx=NULL;
  359. www=0;
  360. bio_s_out=NULL;
  361. s_debug=0;
  362. s_msg=0;
  363. s_quiet=0;
  364. hack=0;
  365. #ifndef OPENSSL_NO_ENGINE
  366. engine_id=NULL;
  367. #endif
  368. }
  369. #endif
  370. static void sv_usage(void)
  371. {
  372. BIO_printf(bio_err,"usage: s_server [args ...]\n");
  373. BIO_printf(bio_err,"\n");
  374. BIO_printf(bio_err," -accept arg - port to accept on (default is %d)\n",PORT);
  375. BIO_printf(bio_err," -context arg - set session ID context\n");
  376. BIO_printf(bio_err," -verify arg - turn on peer certificate verification\n");
  377. BIO_printf(bio_err," -Verify arg - turn on peer certificate verification, must have a cert.\n");
  378. BIO_printf(bio_err," -cert arg - certificate file to use\n");
  379. BIO_printf(bio_err," (default is %s)\n",TEST_CERT);
  380. BIO_printf(bio_err," -crl_check - check the peer certificate has not been revoked by its CA.\n" \
  381. " The CRL(s) are appended to the certificate file\n");
  382. BIO_printf(bio_err," -crl_check_all - check the peer certificate has not been revoked by its CA\n" \
  383. " or any other CRL in the CA chain. CRL(s) are appened to the\n" \
  384. " the certificate file.\n");
  385. BIO_printf(bio_err," -certform arg - certificate format (PEM or DER) PEM default\n");
  386. BIO_printf(bio_err," -key arg - Private Key file to use, in cert file if\n");
  387. BIO_printf(bio_err," not specified (default is %s)\n",TEST_CERT);
  388. BIO_printf(bio_err," -keyform arg - key format (PEM, DER or ENGINE) PEM default\n");
  389. BIO_printf(bio_err," -pass arg - private key file pass phrase source\n");
  390. BIO_printf(bio_err," -dcert arg - second certificate file to use (usually for DSA)\n");
  391. BIO_printf(bio_err," -dcertform x - second certificate format (PEM or DER) PEM default\n");
  392. BIO_printf(bio_err," -dkey arg - second private key file to use (usually for DSA)\n");
  393. BIO_printf(bio_err," -dkeyform arg - second key format (PEM, DER or ENGINE) PEM default\n");
  394. BIO_printf(bio_err," -dpass arg - second private key file pass phrase source\n");
  395. BIO_printf(bio_err," -dhparam arg - DH parameter file to use, in cert file if not specified\n");
  396. BIO_printf(bio_err," or a default set of parameters is used\n");
  397. #ifndef OPENSSL_NO_ECDH
  398. BIO_printf(bio_err," -named_curve arg - Elliptic curve name to use for ephemeral ECDH keys.\n" \
  399. " Use \"openssl ecparam -list_curves\" for all names\n" \
  400. " (default is nistp256).\n");
  401. #endif
  402. #ifdef FIONBIO
  403. BIO_printf(bio_err," -nbio - Run with non-blocking IO\n");
  404. #endif
  405. BIO_printf(bio_err," -nbio_test - test with the non-blocking test bio\n");
  406. BIO_printf(bio_err," -crlf - convert LF from terminal into CRLF\n");
  407. BIO_printf(bio_err," -debug - Print more output\n");
  408. BIO_printf(bio_err," -msg - Show protocol messages\n");
  409. BIO_printf(bio_err," -state - Print the SSL states\n");
  410. BIO_printf(bio_err," -CApath arg - PEM format directory of CA's\n");
  411. BIO_printf(bio_err," -CAfile arg - PEM format file of CA's\n");
  412. BIO_printf(bio_err," -nocert - Don't use any certificates (Anon-DH)\n");
  413. BIO_printf(bio_err," -cipher arg - play with 'openssl ciphers' to see what goes here\n");
  414. BIO_printf(bio_err," -serverpref - Use server's cipher preferences\n");
  415. BIO_printf(bio_err," -quiet - No server output\n");
  416. BIO_printf(bio_err," -no_tmp_rsa - Do not generate a tmp RSA key\n");
  417. #ifndef OPENSSL_NO_PSK
  418. BIO_printf(bio_err," -psk_hint arg - PSK identity hint to use\n");
  419. BIO_printf(bio_err," -psk arg - PSK in hex (without 0x)\n");
  420. # ifndef OPENSSL_NO_JPAKE
  421. BIO_printf(bio_err," -jpake arg - JPAKE secret to use\n");
  422. # endif
  423. #endif
  424. BIO_printf(bio_err," -ssl2 - Just talk SSLv2\n");
  425. BIO_printf(bio_err," -ssl3 - Just talk SSLv3\n");
  426. BIO_printf(bio_err," -tls1 - Just talk TLSv1\n");
  427. BIO_printf(bio_err," -dtls1 - Just talk DTLSv1\n");
  428. BIO_printf(bio_err," -timeout - Enable timeouts\n");
  429. BIO_printf(bio_err," -mtu - Set MTU\n");
  430. BIO_printf(bio_err," -chain - Read a certificate chain\n");
  431. BIO_printf(bio_err," -no_ssl2 - Just disable SSLv2\n");
  432. BIO_printf(bio_err," -no_ssl3 - Just disable SSLv3\n");
  433. BIO_printf(bio_err," -no_tls1 - Just disable TLSv1\n");
  434. #ifndef OPENSSL_NO_DH
  435. BIO_printf(bio_err," -no_dhe - Disable ephemeral DH\n");
  436. #endif
  437. #ifndef OPENSSL_NO_ECDH
  438. BIO_printf(bio_err," -no_ecdhe - Disable ephemeral ECDH\n");
  439. #endif
  440. BIO_printf(bio_err," -bugs - Turn on SSL bug compatibility\n");
  441. BIO_printf(bio_err," -www - Respond to a 'GET /' with a status page\n");
  442. BIO_printf(bio_err," -WWW - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
  443. BIO_printf(bio_err," -HTTP - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
  444. BIO_printf(bio_err," with the assumption it contains a complete HTTP response.\n");
  445. #ifndef OPENSSL_NO_ENGINE
  446. BIO_printf(bio_err," -engine id - Initialise and use the specified engine\n");
  447. #endif
  448. BIO_printf(bio_err," -id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'\n");
  449. BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
  450. #ifndef OPENSSL_NO_TLSEXT
  451. BIO_printf(bio_err," -servername host - servername for HostName TLS extension\n");
  452. BIO_printf(bio_err," -servername_fatal - on mismatch send fatal alert (default warning alert)\n");
  453. BIO_printf(bio_err," -cert2 arg - certificate file to use for servername\n");
  454. BIO_printf(bio_err," (default is %s)\n",TEST_CERT2);
  455. BIO_printf(bio_err," -key2 arg - Private Key file to use for servername, in cert file if\n");
  456. BIO_printf(bio_err," not specified (default is %s)\n",TEST_CERT2);
  457. BIO_printf(bio_err," -tlsextdebug - hex dump of all TLS extensions received\n");
  458. BIO_printf(bio_err," -no_ticket - disable use of RFC4507bis session tickets\n");
  459. #endif
  460. }
  461. static int local_argc=0;
  462. static char **local_argv;
  463. #ifdef CHARSET_EBCDIC
  464. static int ebcdic_new(BIO *bi);
  465. static int ebcdic_free(BIO *a);
  466. static int ebcdic_read(BIO *b, char *out, int outl);
  467. static int ebcdic_write(BIO *b, const char *in, int inl);
  468. static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr);
  469. static int ebcdic_gets(BIO *bp, char *buf, int size);
  470. static int ebcdic_puts(BIO *bp, const char *str);
  471. #define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
  472. static BIO_METHOD methods_ebcdic=
  473. {
  474. BIO_TYPE_EBCDIC_FILTER,
  475. "EBCDIC/ASCII filter",
  476. ebcdic_write,
  477. ebcdic_read,
  478. ebcdic_puts,
  479. ebcdic_gets,
  480. ebcdic_ctrl,
  481. ebcdic_new,
  482. ebcdic_free,
  483. };
  484. typedef struct
  485. {
  486. size_t alloced;
  487. char buff[1];
  488. } EBCDIC_OUTBUFF;
  489. BIO_METHOD *BIO_f_ebcdic_filter()
  490. {
  491. return(&methods_ebcdic);
  492. }
  493. static int ebcdic_new(BIO *bi)
  494. {
  495. EBCDIC_OUTBUFF *wbuf;
  496. wbuf = (EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + 1024);
  497. wbuf->alloced = 1024;
  498. wbuf->buff[0] = '\0';
  499. bi->ptr=(char *)wbuf;
  500. bi->init=1;
  501. bi->flags=0;
  502. return(1);
  503. }
  504. static int ebcdic_free(BIO *a)
  505. {
  506. if (a == NULL) return(0);
  507. if (a->ptr != NULL)
  508. OPENSSL_free(a->ptr);
  509. a->ptr=NULL;
  510. a->init=0;
  511. a->flags=0;
  512. return(1);
  513. }
  514. static int ebcdic_read(BIO *b, char *out, int outl)
  515. {
  516. int ret=0;
  517. if (out == NULL || outl == 0) return(0);
  518. if (b->next_bio == NULL) return(0);
  519. ret=BIO_read(b->next_bio,out,outl);
  520. if (ret > 0)
  521. ascii2ebcdic(out,out,ret);
  522. return(ret);
  523. }
  524. static int ebcdic_write(BIO *b, const char *in, int inl)
  525. {
  526. EBCDIC_OUTBUFF *wbuf;
  527. int ret=0;
  528. int num;
  529. unsigned char n;
  530. if ((in == NULL) || (inl <= 0)) return(0);
  531. if (b->next_bio == NULL) return(0);
  532. wbuf=(EBCDIC_OUTBUFF *)b->ptr;
  533. if (inl > (num = wbuf->alloced))
  534. {
  535. num = num + num; /* double the size */
  536. if (num < inl)
  537. num = inl;
  538. OPENSSL_free(wbuf);
  539. wbuf=(EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + num);
  540. wbuf->alloced = num;
  541. wbuf->buff[0] = '\0';
  542. b->ptr=(char *)wbuf;
  543. }
  544. ebcdic2ascii(wbuf->buff, in, inl);
  545. ret=BIO_write(b->next_bio, wbuf->buff, inl);
  546. return(ret);
  547. }
  548. static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr)
  549. {
  550. long ret;
  551. if (b->next_bio == NULL) return(0);
  552. switch (cmd)
  553. {
  554. case BIO_CTRL_DUP:
  555. ret=0L;
  556. break;
  557. default:
  558. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  559. break;
  560. }
  561. return(ret);
  562. }
  563. static int ebcdic_gets(BIO *bp, char *buf, int size)
  564. {
  565. int i, ret=0;
  566. if (bp->next_bio == NULL) return(0);
  567. /* return(BIO_gets(bp->next_bio,buf,size));*/
  568. for (i=0; i<size-1; ++i)
  569. {
  570. ret = ebcdic_read(bp,&buf[i],1);
  571. if (ret <= 0)
  572. break;
  573. else if (buf[i] == '\n')
  574. {
  575. ++i;
  576. break;
  577. }
  578. }
  579. if (i < size)
  580. buf[i] = '\0';
  581. return (ret < 0 && i == 0) ? ret : i;
  582. }
  583. static int ebcdic_puts(BIO *bp, const char *str)
  584. {
  585. if (bp->next_bio == NULL) return(0);
  586. return ebcdic_write(bp, str, strlen(str));
  587. }
  588. #endif
  589. #ifndef OPENSSL_NO_TLSEXT
  590. /* This is a context that we pass to callbacks */
  591. typedef struct tlsextctx_st {
  592. char * servername;
  593. BIO * biodebug;
  594. int extension_error;
  595. } tlsextctx;
  596. static int MS_CALLBACK ssl_servername_cb(SSL *s, int *ad, void *arg)
  597. {
  598. tlsextctx * p = (tlsextctx *) arg;
  599. const char * servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
  600. if (servername && p->biodebug)
  601. BIO_printf(p->biodebug,"Hostname in TLS extension: \"%s\"\n",servername);
  602. if (!p->servername)
  603. return SSL_TLSEXT_ERR_NOACK;
  604. if (servername)
  605. {
  606. if (strcmp(servername,p->servername))
  607. return p->extension_error;
  608. if (ctx2)
  609. {
  610. BIO_printf(p->biodebug,"Swiching server context.\n");
  611. SSL_set_SSL_CTX(s,ctx2);
  612. }
  613. }
  614. return SSL_TLSEXT_ERR_OK;
  615. }
  616. /* Structure passed to cert status callback */
  617. typedef struct tlsextstatusctx_st {
  618. /* Default responder to use */
  619. char *host, *path, *port;
  620. int use_ssl;
  621. int timeout;
  622. BIO *err;
  623. int verbose;
  624. } tlsextstatusctx;
  625. static tlsextstatusctx tlscstatp = {NULL, NULL, NULL, 0, -1, NULL, 0};
  626. /* Certificate Status callback. This is called when a client includes a
  627. * certificate status request extension.
  628. *
  629. * This is a simplified version. It examines certificates each time and
  630. * makes one OCSP responder query for each request.
  631. *
  632. * A full version would store details such as the OCSP certificate IDs and
  633. * minimise the number of OCSP responses by caching them until they were
  634. * considered "expired".
  635. */
  636. static int cert_status_cb(SSL *s, void *arg)
  637. {
  638. tlsextstatusctx *srctx = arg;
  639. BIO *err = srctx->err;
  640. char *host, *port, *path;
  641. int use_ssl;
  642. unsigned char *rspder = NULL;
  643. int rspderlen;
  644. STACK_OF(STRING) *aia = NULL;
  645. X509 *x = NULL;
  646. X509_STORE_CTX inctx;
  647. X509_OBJECT obj;
  648. OCSP_REQUEST *req = NULL;
  649. OCSP_RESPONSE *resp = NULL;
  650. OCSP_CERTID *id = NULL;
  651. STACK_OF(X509_EXTENSION) *exts;
  652. int ret = SSL_TLSEXT_ERR_NOACK;
  653. int i;
  654. #if 0
  655. STACK_OF(OCSP_RESPID) *ids;
  656. SSL_get_tlsext_status_ids(s, &ids);
  657. BIO_printf(err, "cert_status: received %d ids\n", sk_OCSP_RESPID_num(ids));
  658. #endif
  659. if (srctx->verbose)
  660. BIO_puts(err, "cert_status: callback called\n");
  661. /* Build up OCSP query from server certificate */
  662. x = SSL_get_certificate(s);
  663. aia = X509_get1_ocsp(x);
  664. if (aia)
  665. {
  666. if (!OCSP_parse_url(sk_STRING_value(aia, 0),
  667. &host, &port, &path, &use_ssl))
  668. {
  669. BIO_puts(err, "cert_status: can't parse AIA URL\n");
  670. goto err;
  671. }
  672. if (srctx->verbose)
  673. BIO_printf(err, "cert_status: AIA URL: %s\n",
  674. sk_STRING_value(aia, 0));
  675. }
  676. else
  677. {
  678. if (!srctx->host)
  679. {
  680. BIO_puts(srctx->err, "cert_status: no AIA and no default responder URL\n");
  681. goto done;
  682. }
  683. host = srctx->host;
  684. path = srctx->path;
  685. port = srctx->port;
  686. use_ssl = srctx->use_ssl;
  687. }
  688. if (!X509_STORE_CTX_init(&inctx,
  689. SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)),
  690. NULL, NULL))
  691. goto err;
  692. if (X509_STORE_get_by_subject(&inctx,X509_LU_X509,
  693. X509_get_issuer_name(x),&obj) <= 0)
  694. {
  695. BIO_puts(err, "cert_status: Can't retrieve issuer certificate.\n");
  696. X509_STORE_CTX_cleanup(&inctx);
  697. goto done;
  698. }
  699. req = OCSP_REQUEST_new();
  700. if (!req)
  701. goto err;
  702. id = OCSP_cert_to_id(NULL, x, obj.data.x509);
  703. X509_free(obj.data.x509);
  704. X509_STORE_CTX_cleanup(&inctx);
  705. if (!id)
  706. goto err;
  707. if (!OCSP_request_add0_id(req, id))
  708. goto err;
  709. id = NULL;
  710. /* Add any extensions to the request */
  711. SSL_get_tlsext_status_exts(s, &exts);
  712. for (i = 0; i < sk_X509_EXTENSION_num(exts); i++)
  713. {
  714. X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
  715. if (!OCSP_REQUEST_add_ext(req, ext, -1))
  716. goto err;
  717. }
  718. resp = process_responder(err, req, host, path, port, use_ssl,
  719. srctx->timeout);
  720. if (!resp)
  721. {
  722. BIO_puts(err, "cert_status: error querying responder\n");
  723. goto done;
  724. }
  725. rspderlen = i2d_OCSP_RESPONSE(resp, &rspder);
  726. if (rspderlen <= 0)
  727. goto err;
  728. SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen);
  729. if (srctx->verbose)
  730. {
  731. BIO_puts(err, "cert_status: ocsp response sent:\n");
  732. OCSP_RESPONSE_print(err, resp, 2);
  733. }
  734. ret = SSL_TLSEXT_ERR_OK;
  735. done:
  736. if (ret != SSL_TLSEXT_ERR_OK)
  737. ERR_print_errors(err);
  738. if (aia)
  739. {
  740. OPENSSL_free(host);
  741. OPENSSL_free(path);
  742. OPENSSL_free(port);
  743. X509_email_free(aia);
  744. }
  745. if (id)
  746. OCSP_CERTID_free(id);
  747. if (req)
  748. OCSP_REQUEST_free(req);
  749. if (resp)
  750. OCSP_RESPONSE_free(resp);
  751. return ret;
  752. err:
  753. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  754. goto done;
  755. }
  756. #endif
  757. int MAIN(int, char **);
  758. #ifndef OPENSSL_NO_JPAKE
  759. static char *jpake_secret = NULL;
  760. #endif
  761. int MAIN(int argc, char *argv[])
  762. {
  763. X509_STORE *store = NULL;
  764. int vflags = 0;
  765. short port=PORT;
  766. char *CApath=NULL,*CAfile=NULL;
  767. unsigned char *context = NULL;
  768. char *dhfile = NULL;
  769. #ifndef OPENSSL_NO_ECDH
  770. char *named_curve = NULL;
  771. #endif
  772. int badop=0,bugs=0;
  773. int ret=1;
  774. int off=0;
  775. int no_tmp_rsa=0,no_dhe=0,no_ecdhe=0,nocert=0;
  776. int state=0;
  777. const SSL_METHOD *meth=NULL;
  778. int socket_type=SOCK_STREAM;
  779. ENGINE *e=NULL;
  780. char *inrand=NULL;
  781. int s_cert_format = FORMAT_PEM, s_key_format = FORMAT_PEM;
  782. char *passarg = NULL, *pass = NULL;
  783. char *dpassarg = NULL, *dpass = NULL;
  784. int s_dcert_format = FORMAT_PEM, s_dkey_format = FORMAT_PEM;
  785. X509 *s_cert = NULL, *s_dcert = NULL;
  786. EVP_PKEY *s_key = NULL, *s_dkey = NULL;
  787. #ifndef OPENSSL_NO_TLSEXT
  788. EVP_PKEY *s_key2 = NULL;
  789. X509 *s_cert2 = NULL;
  790. #endif
  791. #ifndef OPENSSL_NO_TLSEXT
  792. tlsextctx tlsextcbp = {NULL, NULL, SSL_TLSEXT_ERR_ALERT_WARNING};
  793. #endif
  794. #ifndef OPENSSL_NO_PSK
  795. /* by default do not send a PSK identity hint */
  796. static char *psk_identity_hint=NULL;
  797. #endif
  798. #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
  799. meth=SSLv23_server_method();
  800. #elif !defined(OPENSSL_NO_SSL3)
  801. meth=SSLv3_server_method();
  802. #elif !defined(OPENSSL_NO_SSL2)
  803. meth=SSLv2_server_method();
  804. #endif
  805. local_argc=argc;
  806. local_argv=argv;
  807. apps_startup();
  808. #ifdef MONOLITH
  809. s_server_init();
  810. #endif
  811. if (bio_err == NULL)
  812. bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
  813. if (!load_config(bio_err, NULL))
  814. goto end;
  815. verify_depth=0;
  816. #ifdef FIONBIO
  817. s_nbio=0;
  818. #endif
  819. s_nbio_test=0;
  820. argc--;
  821. argv++;
  822. while (argc >= 1)
  823. {
  824. if ((strcmp(*argv,"-port") == 0) ||
  825. (strcmp(*argv,"-accept") == 0))
  826. {
  827. if (--argc < 1) goto bad;
  828. if (!extract_port(*(++argv),&port))
  829. goto bad;
  830. }
  831. else if (strcmp(*argv,"-verify") == 0)
  832. {
  833. s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE;
  834. if (--argc < 1) goto bad;
  835. verify_depth=atoi(*(++argv));
  836. BIO_printf(bio_err,"verify depth is %d\n",verify_depth);
  837. }
  838. else if (strcmp(*argv,"-Verify") == 0)
  839. {
  840. s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT|
  841. SSL_VERIFY_CLIENT_ONCE;
  842. if (--argc < 1) goto bad;
  843. verify_depth=atoi(*(++argv));
  844. BIO_printf(bio_err,"verify depth is %d, must return a certificate\n",verify_depth);
  845. }
  846. else if (strcmp(*argv,"-context") == 0)
  847. {
  848. if (--argc < 1) goto bad;
  849. context= (unsigned char *)*(++argv);
  850. }
  851. else if (strcmp(*argv,"-cert") == 0)
  852. {
  853. if (--argc < 1) goto bad;
  854. s_cert_file= *(++argv);
  855. }
  856. else if (strcmp(*argv,"-certform") == 0)
  857. {
  858. if (--argc < 1) goto bad;
  859. s_cert_format = str2fmt(*(++argv));
  860. }
  861. else if (strcmp(*argv,"-key") == 0)
  862. {
  863. if (--argc < 1) goto bad;
  864. s_key_file= *(++argv);
  865. }
  866. else if (strcmp(*argv,"-keyform") == 0)
  867. {
  868. if (--argc < 1) goto bad;
  869. s_key_format = str2fmt(*(++argv));
  870. }
  871. else if (strcmp(*argv,"-pass") == 0)
  872. {
  873. if (--argc < 1) goto bad;
  874. passarg = *(++argv);
  875. }
  876. else if (strcmp(*argv,"-dhparam") == 0)
  877. {
  878. if (--argc < 1) goto bad;
  879. dhfile = *(++argv);
  880. }
  881. #ifndef OPENSSL_NO_ECDH
  882. else if (strcmp(*argv,"-named_curve") == 0)
  883. {
  884. if (--argc < 1) goto bad;
  885. named_curve = *(++argv);
  886. }
  887. #endif
  888. else if (strcmp(*argv,"-dcertform") == 0)
  889. {
  890. if (--argc < 1) goto bad;
  891. s_dcert_format = str2fmt(*(++argv));
  892. }
  893. else if (strcmp(*argv,"-dcert") == 0)
  894. {
  895. if (--argc < 1) goto bad;
  896. s_dcert_file= *(++argv);
  897. }
  898. else if (strcmp(*argv,"-dkeyform") == 0)
  899. {
  900. if (--argc < 1) goto bad;
  901. s_dkey_format = str2fmt(*(++argv));
  902. }
  903. else if (strcmp(*argv,"-dpass") == 0)
  904. {
  905. if (--argc < 1) goto bad;
  906. dpassarg = *(++argv);
  907. }
  908. else if (strcmp(*argv,"-dkey") == 0)
  909. {
  910. if (--argc < 1) goto bad;
  911. s_dkey_file= *(++argv);
  912. }
  913. else if (strcmp(*argv,"-nocert") == 0)
  914. {
  915. nocert=1;
  916. }
  917. else if (strcmp(*argv,"-CApath") == 0)
  918. {
  919. if (--argc < 1) goto bad;
  920. CApath= *(++argv);
  921. }
  922. else if (strcmp(*argv,"-crl_check") == 0)
  923. {
  924. vflags |= X509_V_FLAG_CRL_CHECK;
  925. }
  926. else if (strcmp(*argv,"-crl_check_all") == 0)
  927. {
  928. vflags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;
  929. }
  930. else if (strcmp(*argv,"-verify_return_error") == 0)
  931. verify_return_error = 1;
  932. else if (strcmp(*argv,"-serverpref") == 0)
  933. { off|=SSL_OP_CIPHER_SERVER_PREFERENCE; }
  934. else if (strcmp(*argv,"-cipher") == 0)
  935. {
  936. if (--argc < 1) goto bad;
  937. cipher= *(++argv);
  938. }
  939. else if (strcmp(*argv,"-CAfile") == 0)
  940. {
  941. if (--argc < 1) goto bad;
  942. CAfile= *(++argv);
  943. }
  944. #ifdef FIONBIO
  945. else if (strcmp(*argv,"-nbio") == 0)
  946. { s_nbio=1; }
  947. #endif
  948. else if (strcmp(*argv,"-nbio_test") == 0)
  949. {
  950. #ifdef FIONBIO
  951. s_nbio=1;
  952. #endif
  953. s_nbio_test=1;
  954. }
  955. else if (strcmp(*argv,"-debug") == 0)
  956. { s_debug=1; }
  957. #ifndef OPENSSL_NO_TLSEXT
  958. else if (strcmp(*argv,"-tlsextdebug") == 0)
  959. s_tlsextdebug=1;
  960. else if (strcmp(*argv,"-status") == 0)
  961. s_tlsextstatus=1;
  962. else if (strcmp(*argv,"-status_verbose") == 0)
  963. {
  964. s_tlsextstatus=1;
  965. tlscstatp.verbose = 1;
  966. }
  967. else if (!strcmp(*argv, "-status_timeout"))
  968. {
  969. s_tlsextstatus=1;
  970. if (--argc < 1) goto bad;
  971. tlscstatp.timeout = atoi(*(++argv));
  972. }
  973. else if (!strcmp(*argv, "-status_url"))
  974. {
  975. s_tlsextstatus=1;
  976. if (--argc < 1) goto bad;
  977. if (!OCSP_parse_url(*(++argv),
  978. &tlscstatp.host,
  979. &tlscstatp.port,
  980. &tlscstatp.path,
  981. &tlscstatp.use_ssl))
  982. {
  983. BIO_printf(bio_err, "Error parsing URL\n");
  984. goto bad;
  985. }
  986. }
  987. #endif
  988. else if (strcmp(*argv,"-msg") == 0)
  989. { s_msg=1; }
  990. else if (strcmp(*argv,"-hack") == 0)
  991. { hack=1; }
  992. else if (strcmp(*argv,"-state") == 0)
  993. { state=1; }
  994. else if (strcmp(*argv,"-crlf") == 0)
  995. { s_crlf=1; }
  996. else if (strcmp(*argv,"-quiet") == 0)
  997. { s_quiet=1; }
  998. else if (strcmp(*argv,"-bugs") == 0)
  999. { bugs=1; }
  1000. else if (strcmp(*argv,"-no_tmp_rsa") == 0)
  1001. { no_tmp_rsa=1; }
  1002. else if (strcmp(*argv,"-no_dhe") == 0)
  1003. { no_dhe=1; }
  1004. else if (strcmp(*argv,"-no_ecdhe") == 0)
  1005. { no_ecdhe=1; }
  1006. #ifndef OPENSSL_NO_PSK
  1007. else if (strcmp(*argv,"-psk_hint") == 0)
  1008. {
  1009. if (--argc < 1) goto bad;
  1010. psk_identity_hint= *(++argv);
  1011. }
  1012. else if (strcmp(*argv,"-psk") == 0)
  1013. {
  1014. size_t i;
  1015. if (--argc < 1) goto bad;
  1016. psk_key=*(++argv);
  1017. for (i=0; i<strlen(psk_key); i++)
  1018. {
  1019. if (isxdigit((int)psk_key[i]))
  1020. continue;
  1021. BIO_printf(bio_err,"Not a hex number '%s'\n",*argv);
  1022. goto bad;
  1023. }
  1024. }
  1025. #endif
  1026. else if (strcmp(*argv,"-www") == 0)
  1027. { www=1; }
  1028. else if (strcmp(*argv,"-WWW") == 0)
  1029. { www=2; }
  1030. else if (strcmp(*argv,"-HTTP") == 0)
  1031. { www=3; }
  1032. else if (strcmp(*argv,"-no_ssl2") == 0)
  1033. { off|=SSL_OP_NO_SSLv2; }
  1034. else if (strcmp(*argv,"-no_ssl3") == 0)
  1035. { off|=SSL_OP_NO_SSLv3; }
  1036. else if (strcmp(*argv,"-no_tls1") == 0)
  1037. { off|=SSL_OP_NO_TLSv1; }
  1038. else if (strcmp(*argv,"-no_comp") == 0)
  1039. { off|=SSL_OP_NO_COMPRESSION; }
  1040. #ifndef OPENSSL_NO_TLSEXT
  1041. else if (strcmp(*argv,"-no_ticket") == 0)
  1042. { off|=SSL_OP_NO_TICKET; }
  1043. #endif
  1044. #ifndef OPENSSL_NO_SSL2
  1045. else if (strcmp(*argv,"-ssl2") == 0)
  1046. { meth=SSLv2_server_method(); }
  1047. #endif
  1048. #ifndef OPENSSL_NO_SSL3
  1049. else if (strcmp(*argv,"-ssl3") == 0)
  1050. { meth=SSLv3_server_method(); }
  1051. #endif
  1052. #ifndef OPENSSL_NO_TLS1
  1053. else if (strcmp(*argv,"-tls1") == 0)
  1054. { meth=TLSv1_server_method(); }
  1055. #endif
  1056. #ifndef OPENSSL_NO_DTLS1
  1057. else if (strcmp(*argv,"-dtls1") == 0)
  1058. {
  1059. meth=DTLSv1_server_method();
  1060. socket_type = SOCK_DGRAM;
  1061. }
  1062. else if (strcmp(*argv,"-timeout") == 0)
  1063. enable_timeouts = 1;
  1064. else if (strcmp(*argv,"-mtu") == 0)
  1065. {
  1066. if (--argc < 1) goto bad;
  1067. socket_mtu = atol(*(++argv));
  1068. }
  1069. else if (strcmp(*argv, "-chain") == 0)
  1070. cert_chain = 1;
  1071. #endif
  1072. else if (strcmp(*argv, "-id_prefix") == 0)
  1073. {
  1074. if (--argc < 1) goto bad;
  1075. session_id_prefix = *(++argv);
  1076. }
  1077. #ifndef OPENSSL_NO_ENGINE
  1078. else if (strcmp(*argv,"-engine") == 0)
  1079. {
  1080. if (--argc < 1) goto bad;
  1081. engine_id= *(++argv);
  1082. }
  1083. #endif
  1084. else if (strcmp(*argv,"-rand") == 0)
  1085. {
  1086. if (--argc < 1) goto bad;
  1087. inrand= *(++argv);
  1088. }
  1089. #ifndef OPENSSL_NO_TLSEXT
  1090. else if (strcmp(*argv,"-servername") == 0)
  1091. {
  1092. if (--argc < 1) goto bad;
  1093. tlsextcbp.servername= *(++argv);
  1094. }
  1095. else if (strcmp(*argv,"-servername_fatal") == 0)
  1096. { tlsextcbp.extension_error = SSL_TLSEXT_ERR_ALERT_FATAL; }
  1097. else if (strcmp(*argv,"-cert2") == 0)
  1098. {
  1099. if (--argc < 1) goto bad;
  1100. s_cert_file2= *(++argv);
  1101. }
  1102. else if (strcmp(*argv,"-key2") == 0)
  1103. {
  1104. if (--argc < 1) goto bad;
  1105. s_key_file2= *(++argv);
  1106. }
  1107. #endif
  1108. #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
  1109. else if (strcmp(*argv,"-jpake") == 0)
  1110. {
  1111. if (--argc < 1) goto bad;
  1112. jpake_secret = *(++argv);
  1113. }
  1114. #endif
  1115. else
  1116. {
  1117. BIO_printf(bio_err,"unknown option %s\n",*argv);
  1118. badop=1;
  1119. break;
  1120. }
  1121. argc--;
  1122. argv++;
  1123. }
  1124. if (badop)
  1125. {
  1126. bad:
  1127. sv_usage();
  1128. goto end;
  1129. }
  1130. #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
  1131. if (jpake_secret)
  1132. {
  1133. if (psk_key)
  1134. {
  1135. BIO_printf(bio_err,
  1136. "Can't use JPAKE and PSK together\n");
  1137. goto end;
  1138. }
  1139. psk_identity = "JPAKE";
  1140. if (cipher)
  1141. {
  1142. BIO_printf(bio_err, "JPAKE sets cipher to PSK\n");
  1143. goto end;
  1144. }
  1145. cipher = "PSK";
  1146. }
  1147. #endif
  1148. SSL_load_error_strings();
  1149. OpenSSL_add_ssl_algorithms();
  1150. #ifndef OPENSSL_NO_ENGINE
  1151. e = setup_engine(bio_err, engine_id, 1);
  1152. #endif
  1153. if (!app_passwd(bio_err, passarg, dpassarg, &pass, &dpass))
  1154. {
  1155. BIO_printf(bio_err, "Error getting password\n");
  1156. goto end;
  1157. }
  1158. if (s_key_file == NULL)
  1159. s_key_file = s_cert_file;
  1160. #ifndef OPENSSL_NO_TLSEXT
  1161. if (s_key_file2 == NULL)
  1162. s_key_file2 = s_cert_file2;
  1163. #endif
  1164. if (nocert == 0)
  1165. {
  1166. s_key = load_key(bio_err, s_key_file, s_key_format, 0, pass, e,
  1167. "server certificate private key file");
  1168. if (!s_key)
  1169. {
  1170. ERR_print_errors(bio_err);
  1171. goto end;
  1172. }
  1173. s_cert = load_cert(bio_err,s_cert_file,s_cert_format,
  1174. NULL, e, "server certificate file");
  1175. if (!s_cert)
  1176. {
  1177. ERR_print_errors(bio_err);
  1178. goto end;
  1179. }
  1180. #ifndef OPENSSL_NO_TLSEXT
  1181. if (tlsextcbp.servername)
  1182. {
  1183. s_key2 = load_key(bio_err, s_key_file2, s_key_format, 0, pass, e,
  1184. "second server certificate private key file");
  1185. if (!s_key2)
  1186. {
  1187. ERR_print_errors(bio_err);
  1188. goto end;
  1189. }
  1190. s_cert2 = load_cert(bio_err,s_cert_file2,s_cert_format,
  1191. NULL, e, "second server certificate file");
  1192. if (!s_cert2)
  1193. {
  1194. ERR_print_errors(bio_err);
  1195. goto end;
  1196. }
  1197. }
  1198. #endif
  1199. }
  1200. if (s_dcert_file)
  1201. {
  1202. if (s_dkey_file == NULL)
  1203. s_dkey_file = s_dcert_file;
  1204. s_dkey = load_key(bio_err, s_dkey_file, s_dkey_format,
  1205. 0, dpass, e,
  1206. "second certificate private key file");
  1207. if (!s_dkey)
  1208. {
  1209. ERR_print_errors(bio_err);
  1210. goto end;
  1211. }
  1212. s_dcert = load_cert(bio_err,s_dcert_file,s_dcert_format,
  1213. NULL, e, "second server certificate file");
  1214. if (!s_dcert)
  1215. {
  1216. ERR_print_errors(bio_err);
  1217. goto end;
  1218. }
  1219. }
  1220. if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
  1221. && !RAND_status())
  1222. {
  1223. BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
  1224. }
  1225. if (inrand != NULL)
  1226. BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
  1227. app_RAND_load_files(inrand));
  1228. if (bio_s_out == NULL)
  1229. {
  1230. if (s_quiet && !s_debug && !s_msg)
  1231. {
  1232. bio_s_out=BIO_new(BIO_s_null());
  1233. }
  1234. else
  1235. {
  1236. if (bio_s_out == NULL)
  1237. bio_s_out=BIO_new_fp(stdout,BIO_NOCLOSE);
  1238. }
  1239. }
  1240. #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
  1241. if (nocert)
  1242. #endif
  1243. {
  1244. s_cert_file=NULL;
  1245. s_key_file=NULL;
  1246. s_dcert_file=NULL;
  1247. s_dkey_file=NULL;
  1248. #ifndef OPENSSL_NO_TLSEXT
  1249. s_cert_file2=NULL;
  1250. s_key_file2=NULL;
  1251. #endif
  1252. }
  1253. ctx=SSL_CTX_new(meth);
  1254. if (ctx == NULL)
  1255. {
  1256. ERR_print_errors(bio_err);
  1257. goto end;
  1258. }
  1259. if (session_id_prefix)
  1260. {
  1261. if(strlen(session_id_prefix) >= 32)
  1262. BIO_printf(bio_err,
  1263. "warning: id_prefix is too long, only one new session will be possible\n");
  1264. else if(strlen(session_id_prefix) >= 16)
  1265. BIO_printf(bio_err,
  1266. "warning: id_prefix is too long if you use SSLv2\n");
  1267. if(!SSL_CTX_set_generate_session_id(ctx, generate_session_id))
  1268. {
  1269. BIO_printf(bio_err,"error setting 'id_prefix'\n");
  1270. ERR_print_errors(bio_err);
  1271. goto end;
  1272. }
  1273. BIO_printf(bio_err,"id_prefix '%s' set.\n", session_id_prefix);
  1274. }
  1275. SSL_CTX_set_quiet_shutdown(ctx,1);
  1276. if (bugs) SSL_CTX_set_options(ctx,SSL_OP_ALL);
  1277. if (hack) SSL_CTX_set_options(ctx,SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
  1278. SSL_CTX_set_options(ctx,off);
  1279. /* DTLS: partial reads end up discarding unread UDP bytes :-(
  1280. * Setting read ahead solves this problem.
  1281. */
  1282. if (socket_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx, 1);
  1283. if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);
  1284. SSL_CTX_sess_set_cache_size(ctx,128);
  1285. #if 0
  1286. if (cipher == NULL) cipher=getenv("SSL_CIPHER");
  1287. #endif
  1288. #if 0
  1289. if (s_cert_file == NULL)
  1290. {
  1291. BIO_printf(bio_err,"You must specify a certificate file for the server to use\n");
  1292. goto end;
  1293. }
  1294. #endif
  1295. if ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) ||
  1296. (!SSL_CTX_set_default_verify_paths(ctx)))
  1297. {
  1298. /* BIO_printf(bio_err,"X509_load_verify_locations\n"); */
  1299. ERR_print_errors(bio_err);
  1300. /* goto end; */
  1301. }
  1302. store = SSL_CTX_get_cert_store(ctx);
  1303. X509_STORE_set_flags(store, vflags);
  1304. #ifndef OPENSSL_NO_TLSEXT
  1305. if (s_cert2)
  1306. {
  1307. ctx2=SSL_CTX_new(meth);
  1308. if (ctx2 == NULL)
  1309. {
  1310. ERR_print_errors(bio_err);
  1311. goto end;
  1312. }
  1313. }
  1314. if (ctx2)
  1315. {
  1316. BIO_printf(bio_s_out,"Setting secondary ctx parameters\n");
  1317. if (session_id_prefix)
  1318. {
  1319. if(strlen(session_id_prefix) >= 32)
  1320. BIO_printf(bio_err,
  1321. "warning: id_prefix is too long, only one new session will be possible\n");
  1322. else if(strlen(session_id_prefix) >= 16)
  1323. BIO_printf(bio_err,
  1324. "warning: id_prefix is too long if you use SSLv2\n");
  1325. if(!SSL_CTX_set_generate_session_id(ctx2, generate_session_id))
  1326. {
  1327. BIO_printf(bio_err,"error setting 'id_prefix'\n");
  1328. ERR_print_errors(bio_err);
  1329. goto end;
  1330. }
  1331. BIO_printf(bio_err,"id_prefix '%s' set.\n", session_id_prefix);
  1332. }
  1333. SSL_CTX_set_quiet_shutdown(ctx2,1);
  1334. if (bugs) SSL_CTX_set_options(ctx2,SSL_OP_ALL);
  1335. if (hack) SSL_CTX_set_options(ctx2,SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
  1336. SSL_CTX_set_options(ctx2,off);
  1337. /* DTLS: partial reads end up discarding unread UDP bytes :-(
  1338. * Setting read ahead solves this problem.
  1339. */
  1340. if (socket_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx2, 1);
  1341. if (state) SSL_CTX_set_info_callback(ctx2,apps_ssl_info_callback);
  1342. SSL_CTX_sess_set_cache_size(ctx2,128);
  1343. if ((!SSL_CTX_load_verify_locations(ctx2,CAfile,CApath)) ||
  1344. (!SSL_CTX_set_default_verify_paths(ctx2)))
  1345. {
  1346. ERR_print_errors(bio_err);
  1347. }
  1348. store = SSL_CTX_get_cert_store(ctx2);
  1349. X509_STORE_set_flags(store, vflags);
  1350. }
  1351. #endif
  1352. #ifndef OPENSSL_NO_DH
  1353. if (!no_dhe)
  1354. {
  1355. DH *dh=NULL;
  1356. if (dhfile)
  1357. dh = load_dh_param(dhfile);
  1358. else if (s_cert_file)
  1359. dh = load_dh_param(s_cert_file);
  1360. if (dh != NULL)
  1361. {
  1362. BIO_printf(bio_s_out,"Setting temp DH parameters\n");
  1363. }
  1364. else
  1365. {
  1366. BIO_printf(bio_s_out,"Using default temp DH parameters\n");
  1367. dh=get_dh512();
  1368. }
  1369. (void)BIO_flush(bio_s_out);
  1370. SSL_CTX_set_tmp_dh(ctx,dh);
  1371. #ifndef OPENSSL_NO_TLSEXT
  1372. if (ctx2)
  1373. {
  1374. if (!dhfile)
  1375. {
  1376. DH *dh2=load_dh_param(s_cert_file2);
  1377. if (dh2 != NULL)
  1378. {
  1379. BIO_printf(bio_s_out,"Setting temp DH parameters\n");
  1380. (void)BIO_flush(bio_s_out);
  1381. DH_free(dh);
  1382. dh = dh2;
  1383. }
  1384. }
  1385. SSL_CTX_set_tmp_dh(ctx2,dh);
  1386. }
  1387. #endif
  1388. DH_free(dh);
  1389. }
  1390. #endif
  1391. #ifndef OPENSSL_NO_ECDH
  1392. if (!no_ecdhe)
  1393. {
  1394. EC_KEY *ecdh=NULL;
  1395. if (named_curve)
  1396. {
  1397. int nid = OBJ_sn2nid(named_curve);
  1398. if (nid == 0)
  1399. {
  1400. BIO_printf(bio_err, "unknown curve name (%s)\n",
  1401. named_curve);
  1402. goto end;
  1403. }
  1404. ecdh = EC_KEY_new_by_curve_name(nid);
  1405. if (ecdh == NULL)
  1406. {
  1407. BIO_printf(bio_err, "unable to create curve (%s)\n",
  1408. named_curve);
  1409. goto end;
  1410. }
  1411. }
  1412. if (ecdh != NULL)
  1413. {
  1414. BIO_printf(bio_s_out,"Setting temp ECDH parameters\n");
  1415. }
  1416. else
  1417. {
  1418. BIO_printf(bio_s_out,"Using default temp ECDH parameters\n");
  1419. ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  1420. if (ecdh == NULL)
  1421. {
  1422. BIO_printf(bio_err, "unable to create curve (nistp256)\n");
  1423. goto end;
  1424. }
  1425. }
  1426. (void)BIO_flush(bio_s_out);
  1427. SSL_CTX_set_tmp_ecdh(ctx,ecdh);
  1428. #ifndef OPENSSL_NO_TLSEXT
  1429. if (ctx2)
  1430. SSL_CTX_set_tmp_ecdh(ctx2,ecdh);
  1431. #endif
  1432. EC_KEY_free(ecdh);
  1433. }
  1434. #endif
  1435. if (!set_cert_key_stuff(ctx,s_cert,s_key))
  1436. goto end;
  1437. #ifndef OPENSSL_NO_TLSEXT
  1438. if (ctx2 && !set_cert_key_stuff(ctx2,s_cert2,s_key2))
  1439. goto end;
  1440. #endif
  1441. if (s_dcert != NULL)
  1442. {
  1443. if (!set_cert_key_stuff(ctx,s_dcert,s_dkey))
  1444. goto end;
  1445. }
  1446. #ifndef OPENSSL_NO_RSA
  1447. #if 1
  1448. if (!no_tmp_rsa)
  1449. {
  1450. SSL_CTX_set_tmp_rsa_callback(ctx,tmp_rsa_cb);
  1451. #ifndef OPENSSL_NO_TLSEXT
  1452. if (ctx2)
  1453. SSL_CTX_set_tmp_rsa_callback(ctx2,tmp_rsa_cb);
  1454. #endif
  1455. }
  1456. #else
  1457. if (!no_tmp_rsa && SSL_CTX_need_tmp_RSA(ctx))
  1458. {
  1459. RSA *rsa;
  1460. BIO_printf(bio_s_out,"Generating temp (512 bit) RSA key...");
  1461. BIO_flush(bio_s_out);
  1462. rsa=RSA_generate_key(512,RSA_F4,NULL);
  1463. if (!SSL_CTX_set_tmp_rsa(ctx,rsa))
  1464. {
  1465. ERR_print_errors(bio_err);
  1466. goto end;
  1467. }
  1468. #ifndef OPENSSL_NO_TLSEXT
  1469. if (ctx2)
  1470. {
  1471. if (!SSL_CTX_set_tmp_rsa(ctx2,rsa))
  1472. {
  1473. ERR_print_errors(bio_err);
  1474. goto end;
  1475. }
  1476. }
  1477. #endif
  1478. RSA_free(rsa);
  1479. BIO_printf(bio_s_out,"\n");
  1480. }
  1481. #endif
  1482. #endif
  1483. #ifndef OPENSSL_NO_PSK
  1484. #ifdef OPENSSL_NO_JPAKE
  1485. if (psk_key != NULL)
  1486. #else
  1487. if (psk_key != NULL || jpake_secret)
  1488. #endif
  1489. {
  1490. if (s_debug)
  1491. BIO_printf(bio_s_out, "PSK key given or JPAKE in use, setting server callback\n");
  1492. SSL_CTX_set_psk_server_callback(ctx, psk_server_cb);
  1493. }
  1494. if (!SSL_CTX_use_psk_identity_hint(ctx, psk_identity_hint))
  1495. {
  1496. BIO_printf(bio_err,"error setting PSK identity hint to context\n");
  1497. ERR_print_errors(bio_err);
  1498. goto end;
  1499. }
  1500. #endif
  1501. if (cipher != NULL)
  1502. {
  1503. if(!SSL_CTX_set_cipher_list(ctx,cipher))
  1504. {
  1505. BIO_printf(bio_err,"error setting cipher list\n");
  1506. ERR_print_errors(bio_err);
  1507. goto end;
  1508. }
  1509. #ifndef OPENSSL_NO_TLSEXT
  1510. if (ctx2 && !SSL_CTX_set_cipher_list(ctx2,cipher))
  1511. {
  1512. BIO_printf(bio_err,"error setting cipher list\n");
  1513. ERR_print_errors(bio_err);
  1514. goto end;
  1515. }
  1516. #endif
  1517. }
  1518. SSL_CTX_set_verify(ctx,s_server_verify,verify_callback);
  1519. SSL_CTX_set_session_id_context(ctx,(void*)&s_server_session_id_context,
  1520. sizeof s_server_session_id_context);
  1521. #ifndef OPENSSL_NO_TLSEXT
  1522. if (ctx2)
  1523. {
  1524. SSL_CTX_set_verify(ctx2,s_server_verify,verify_callback);
  1525. SSL_CTX_set_session_id_context(ctx2,(void*)&s_server_session_id_context,
  1526. sizeof s_server_session_id_context);
  1527. tlsextcbp.biodebug = bio_s_out;
  1528. SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb);
  1529. SSL_CTX_set_tlsext_servername_arg(ctx2, &tlsextcbp);
  1530. SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
  1531. SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
  1532. }
  1533. #endif
  1534. if (CAfile != NULL)
  1535. {
  1536. SSL_CTX_set_client_CA_list(ctx,SSL_load_client_CA_file(CAfile));
  1537. #ifndef OPENSSL_NO_TLSEXT
  1538. if (ctx2)
  1539. SSL_CTX_set_client_CA_list(ctx2,SSL_load_client_CA_file(CAfile));
  1540. #endif
  1541. }
  1542. BIO_printf(bio_s_out,"ACCEPT\n");
  1543. (void)BIO_flush(bio_s_out);
  1544. if (www)
  1545. do_server(port,socket_type,&accept_socket,www_body, context);
  1546. else
  1547. do_server(port,socket_type,&accept_socket,sv_body, context);
  1548. print_stats(bio_s_out,ctx);
  1549. ret=0;
  1550. end:
  1551. if (ctx != NULL) SSL_CTX_free(ctx);
  1552. if (s_cert)
  1553. X509_free(s_cert);
  1554. if (s_dcert)
  1555. X509_free(s_dcert);
  1556. if (s_key)
  1557. EVP_PKEY_free(s_key);
  1558. if (s_dkey)
  1559. EVP_PKEY_free(s_dkey);
  1560. if (pass)
  1561. OPENSSL_free(pass);
  1562. if (dpass)
  1563. OPENSSL_free(dpass);
  1564. #ifndef OPENSSL_NO_TLSEXT
  1565. if (ctx2 != NULL) SSL_CTX_free(ctx2);
  1566. if (s_cert2)
  1567. X509_free(s_cert2);
  1568. if (s_key2)
  1569. EVP_PKEY_free(s_key2);
  1570. #endif
  1571. if (bio_s_out != NULL)
  1572. {
  1573. BIO_free(bio_s_out);
  1574. bio_s_out=NULL;
  1575. }
  1576. apps_shutdown();
  1577. OPENSSL_EXIT(ret);
  1578. }
  1579. static void print_stats(BIO *bio, SSL_CTX *ssl_ctx)
  1580. {
  1581. BIO_printf(bio,"%4ld items in the session cache\n",
  1582. SSL_CTX_sess_number(ssl_ctx));
  1583. BIO_printf(bio,"%4ld client connects (SSL_connect())\n",
  1584. SSL_CTX_sess_connect(ssl_ctx));
  1585. BIO_printf(bio,"%4ld client renegotiates (SSL_connect())\n",
  1586. SSL_CTX_sess_connect_renegotiate(ssl_ctx));
  1587. BIO_printf(bio,"%4ld client connects that finished\n",
  1588. SSL_CTX_sess_connect_good(ssl_ctx));
  1589. BIO_printf(bio,"%4ld server accepts (SSL_accept())\n",
  1590. SSL_CTX_sess_accept(ssl_ctx));
  1591. BIO_printf(bio,"%4ld server renegotiates (SSL_accept())\n",
  1592. SSL_CTX_sess_accept_renegotiate(ssl_ctx));
  1593. BIO_printf(bio,"%4ld server accepts that finished\n",
  1594. SSL_CTX_sess_accept_good(ssl_ctx));
  1595. BIO_printf(bio,"%4ld session cache hits\n",SSL_CTX_sess_hits(ssl_ctx));
  1596. BIO_printf(bio,"%4ld session cache misses\n",SSL_CTX_sess_misses(ssl_ctx));
  1597. BIO_printf(bio,"%4ld session cache timeouts\n",SSL_CTX_sess_timeouts(ssl_ctx));
  1598. BIO_printf(bio,"%4ld callback cache hits\n",SSL_CTX_sess_cb_hits(ssl_ctx));
  1599. BIO_printf(bio,"%4ld cache full overflows (%ld allowed)\n",
  1600. SSL_CTX_sess_cache_full(ssl_ctx),
  1601. SSL_CTX_sess_get_cache_size(ssl_ctx));
  1602. }
  1603. static int sv_body(char *hostname, int s, unsigned char *context)
  1604. {
  1605. char *buf=NULL;
  1606. fd_set readfds;
  1607. int ret=1,width;
  1608. int k,i;
  1609. unsigned long l;
  1610. SSL *con=NULL;
  1611. BIO *sbio;
  1612. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)
  1613. struct timeval tv;
  1614. #endif
  1615. if ((buf=OPENSSL_malloc(bufsize)) == NULL)
  1616. {
  1617. BIO_printf(bio_err,"out of memory\n");
  1618. goto err;
  1619. }
  1620. #ifdef FIONBIO
  1621. if (s_nbio)
  1622. {
  1623. unsigned long sl=1;
  1624. if (!s_quiet)
  1625. BIO_printf(bio_err,"turning on non blocking io\n");
  1626. if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)
  1627. ERR_print_errors(bio_err);
  1628. }
  1629. #endif
  1630. if (con == NULL) {
  1631. con=SSL_new(ctx);
  1632. #ifndef OPENSSL_NO_TLSEXT
  1633. if (s_tlsextdebug)
  1634. {
  1635. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  1636. SSL_set_tlsext_debug_arg(con, bio_s_out);
  1637. }
  1638. if (s_tlsextstatus)
  1639. {
  1640. SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb);
  1641. tlscstatp.err = bio_err;
  1642. SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp);
  1643. }
  1644. #endif
  1645. #ifndef OPENSSL_NO_KRB5
  1646. if ((con->kssl_ctx = kssl_ctx_new()) != NULL)
  1647. {
  1648. kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE,
  1649. KRB5SVC);
  1650. kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB,
  1651. KRB5KEYTAB);
  1652. }
  1653. #endif /* OPENSSL_NO_KRB5 */
  1654. if(context)
  1655. SSL_set_session_id_context(con, context,
  1656. strlen((char *)context));
  1657. }
  1658. SSL_clear(con);
  1659. #if 0
  1660. #ifdef TLSEXT_TYPE_opaque_prf_input
  1661. SSL_set_tlsext_opaque_prf_input(con, "Test server", 11);
  1662. #endif
  1663. #endif
  1664. if (SSL_version(con) == DTLS1_VERSION)
  1665. {
  1666. struct timeval timeout;
  1667. sbio=BIO_new_dgram(s,BIO_NOCLOSE);
  1668. if (enable_timeouts)
  1669. {
  1670. timeout.tv_sec = 0;
  1671. timeout.tv_usec = DGRAM_RCV_TIMEOUT;
  1672. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
  1673. timeout.tv_sec = 0;
  1674. timeout.tv_usec = DGRAM_SND_TIMEOUT;
  1675. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
  1676. }
  1677. if (socket_mtu > 0)
  1678. {
  1679. SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
  1680. SSL_set_mtu(con, socket_mtu);
  1681. }
  1682. else
  1683. /* want to do MTU discovery */
  1684. BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
  1685. /* turn on cookie exchange */
  1686. SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE);
  1687. }
  1688. else
  1689. sbio=BIO_new_socket(s,BIO_NOCLOSE);
  1690. if (s_nbio_test)
  1691. {
  1692. BIO *test;
  1693. test=BIO_new(BIO_f_nbio_test());
  1694. sbio=BIO_push(test,sbio);
  1695. }
  1696. #ifndef OPENSSL_NO_JPAKE
  1697. if(jpake_secret)
  1698. jpake_server_auth(bio_s_out, sbio, jpake_secret);
  1699. #endif
  1700. SSL_set_bio(con,sbio,sbio);
  1701. SSL_set_accept_state(con);
  1702. /* SSL_set_fd(con,s); */
  1703. if (s_debug)
  1704. {
  1705. con->debug=1;
  1706. BIO_set_callback(SSL_get_rbio(con),bio_dump_callback);
  1707. BIO_set_callback_arg(SSL_get_rbio(con),(char *)bio_s_out);
  1708. }
  1709. if (s_msg)
  1710. {
  1711. SSL_set_msg_callback(con, msg_cb);
  1712. SSL_set_msg_callback_arg(con, bio_s_out);
  1713. }
  1714. #ifndef OPENSSL_NO_TLSEXT
  1715. if (s_tlsextdebug)
  1716. {
  1717. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  1718. SSL_set_tlsext_debug_arg(con, bio_s_out);
  1719. }
  1720. #endif
  1721. width=s+1;
  1722. for (;;)
  1723. {
  1724. int read_from_terminal;
  1725. int read_from_sslcon;
  1726. read_from_terminal = 0;
  1727. read_from_sslcon = SSL_pending(con);
  1728. if (!read_from_sslcon)
  1729. {
  1730. FD_ZERO(&readfds);
  1731. #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) && !defined(OPENSSL_SYS_BEOS_R5)
  1732. openssl_fdset(fileno(stdin),&readfds);
  1733. #endif
  1734. openssl_fdset(s,&readfds);
  1735. /* Note: under VMS with SOCKETSHR the second parameter is
  1736. * currently of type (int *) whereas under other systems
  1737. * it is (void *) if you don't have a cast it will choke
  1738. * the compiler: if you do have a cast then you can either
  1739. * go for (int *) or (void *).
  1740. */
  1741. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)
  1742. /* Under DOS (non-djgpp) and Windows we can't select on stdin: only
  1743. * on sockets. As a workaround we timeout the select every
  1744. * second and check for any keypress. In a proper Windows
  1745. * application we wouldn't do this because it is inefficient.
  1746. */
  1747. tv.tv_sec = 1;
  1748. tv.tv_usec = 0;
  1749. i=select(width,(void *)&readfds,NULL,NULL,&tv);
  1750. if((i < 0) || (!i && !_kbhit() ) )continue;
  1751. if(_kbhit())
  1752. read_from_terminal = 1;
  1753. #elif defined(OPENSSL_SYS_BEOS_R5)
  1754. /* Under BeOS-R5 the situation is similar to DOS */
  1755. tv.tv_sec = 1;
  1756. tv.tv_usec = 0;
  1757. (void)fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
  1758. i=select(width,(void *)&readfds,NULL,NULL,&tv);
  1759. if ((i < 0) || (!i && read(fileno(stdin), buf, 0) < 0))
  1760. continue;
  1761. if (read(fileno(stdin), buf, 0) >= 0)
  1762. read_from_terminal = 1;
  1763. (void)fcntl(fileno(stdin), F_SETFL, 0);
  1764. #else
  1765. i=select(width,(void *)&readfds,NULL,NULL,NULL);
  1766. if (i <= 0) continue;
  1767. if (FD_ISSET(fileno(stdin),&readfds))
  1768. read_from_terminal = 1;
  1769. #endif
  1770. if (FD_ISSET(s,&readfds))
  1771. read_from_sslcon = 1;
  1772. }
  1773. if (read_from_terminal)
  1774. {
  1775. if (s_crlf)
  1776. {
  1777. int j, lf_num;
  1778. i=raw_read_stdin(buf, bufsize/2);
  1779. lf_num = 0;
  1780. /* both loops are skipped when i <= 0 */
  1781. for (j = 0; j < i; j++)
  1782. if (buf[j] == '\n')
  1783. lf_num++;
  1784. for (j = i-1; j >= 0; j--)
  1785. {
  1786. buf[j+lf_num] = buf[j];
  1787. if (buf[j] == '\n')
  1788. {
  1789. lf_num--;
  1790. i++;
  1791. buf[j+lf_num] = '\r';
  1792. }
  1793. }
  1794. assert(lf_num == 0);
  1795. }
  1796. else
  1797. i=raw_read_stdin(buf,bufsize);
  1798. if (!s_quiet)
  1799. {
  1800. if ((i <= 0) || (buf[0] == 'Q'))
  1801. {
  1802. BIO_printf(bio_s_out,"DONE\n");
  1803. SHUTDOWN(s);
  1804. close_accept_socket();
  1805. ret= -11;
  1806. goto err;
  1807. }
  1808. if ((i <= 0) || (buf[0] == 'q'))
  1809. {
  1810. BIO_printf(bio_s_out,"DONE\n");
  1811. if (SSL_version(con) != DTLS1_VERSION)
  1812. SHUTDOWN(s);
  1813. /* close_accept_socket();
  1814. ret= -11;*/
  1815. goto err;
  1816. }
  1817. if ((buf[0] == 'r') &&
  1818. ((buf[1] == '\n') || (buf[1] == '\r')))
  1819. {
  1820. SSL_renegotiate(con);
  1821. i=SSL_do_handshake(con);
  1822. printf("SSL_do_handshake -> %d\n",i);
  1823. i=0; /*13; */
  1824. continue;
  1825. /* strcpy(buf,"server side RE-NEGOTIATE\n"); */
  1826. }
  1827. if ((buf[0] == 'R') &&
  1828. ((buf[1] == '\n') || (buf[1] == '\r')))
  1829. {
  1830. SSL_set_verify(con,
  1831. SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,NULL);
  1832. SSL_renegotiate(con);
  1833. i=SSL_do_handshake(con);
  1834. printf("SSL_do_handshake -> %d\n",i);
  1835. i=0; /* 13; */
  1836. continue;
  1837. /* strcpy(buf,"server side RE-NEGOTIATE asking for client cert\n"); */
  1838. }
  1839. if (buf[0] == 'P')
  1840. {
  1841. static const char *str="Lets print some clear text\n";
  1842. BIO_write(SSL_get_wbio(con),str,strlen(str));
  1843. }
  1844. if (buf[0] == 'S')
  1845. {
  1846. print_stats(bio_s_out,SSL_get_SSL_CTX(con));
  1847. }
  1848. }
  1849. #ifdef CHARSET_EBCDIC
  1850. ebcdic2ascii(buf,buf,i);
  1851. #endif
  1852. l=k=0;
  1853. for (;;)
  1854. {
  1855. /* should do a select for the write */
  1856. #ifdef RENEG
  1857. { static count=0; if (++count == 100) { count=0; SSL_renegotiate(con); } }
  1858. #endif
  1859. k=SSL_write(con,&(buf[l]),(unsigned int)i);
  1860. switch (SSL_get_error(con,k))
  1861. {
  1862. case SSL_ERROR_NONE:
  1863. break;
  1864. case SSL_ERROR_WANT_WRITE:
  1865. case SSL_ERROR_WANT_READ:
  1866. case SSL_ERROR_WANT_X509_LOOKUP:
  1867. BIO_printf(bio_s_out,"Write BLOCK\n");
  1868. break;
  1869. case SSL_ERROR_SYSCALL:
  1870. case SSL_ERROR_SSL:
  1871. BIO_printf(bio_s_out,"ERROR\n");
  1872. ERR_print_errors(bio_err);
  1873. ret=1;
  1874. goto err;
  1875. /* break; */
  1876. case SSL_ERROR_ZERO_RETURN:
  1877. BIO_printf(bio_s_out,"DONE\n");
  1878. ret=1;
  1879. goto err;
  1880. }
  1881. l+=k;
  1882. i-=k;
  1883. if (i <= 0) break;
  1884. }
  1885. }
  1886. if (read_from_sslcon)
  1887. {
  1888. if (!SSL_is_init_finished(con))
  1889. {
  1890. i=init_ssl_connection(con);
  1891. if (i < 0)
  1892. {
  1893. ret=0;
  1894. goto err;
  1895. }
  1896. else if (i == 0)
  1897. {
  1898. ret=1;
  1899. goto err;
  1900. }
  1901. }
  1902. else
  1903. {
  1904. again:
  1905. i=SSL_read(con,(char *)buf,bufsize);
  1906. switch (SSL_get_error(con,i))
  1907. {
  1908. case SSL_ERROR_NONE:
  1909. #ifdef CHARSET_EBCDIC
  1910. ascii2ebcdic(buf,buf,i);
  1911. #endif
  1912. raw_write_stdout(buf,
  1913. (unsigned int)i);
  1914. if (SSL_pending(con)) goto again;
  1915. break;
  1916. case SSL_ERROR_WANT_WRITE:
  1917. case SSL_ERROR_WANT_READ:
  1918. case SSL_ERROR_WANT_X509_LOOKUP:
  1919. BIO_printf(bio_s_out,"Read BLOCK\n");
  1920. break;
  1921. case SSL_ERROR_SYSCALL:
  1922. case SSL_ERROR_SSL:
  1923. BIO_printf(bio_s_out,"ERROR\n");
  1924. ERR_print_errors(bio_err);
  1925. ret=1;
  1926. goto err;
  1927. case SSL_ERROR_ZERO_RETURN:
  1928. BIO_printf(bio_s_out,"DONE\n");
  1929. ret=1;
  1930. goto err;
  1931. }
  1932. }
  1933. }
  1934. }
  1935. err:
  1936. if (con != NULL)
  1937. {
  1938. BIO_printf(bio_s_out,"shutting down SSL\n");
  1939. #if 1
  1940. SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
  1941. #else
  1942. SSL_shutdown(con);
  1943. #endif
  1944. SSL_free(con);
  1945. }
  1946. BIO_printf(bio_s_out,"CONNECTION CLOSED\n");
  1947. if (buf != NULL)
  1948. {
  1949. OPENSSL_cleanse(buf,bufsize);
  1950. OPENSSL_free(buf);
  1951. }
  1952. if (ret >= 0)
  1953. BIO_printf(bio_s_out,"ACCEPT\n");
  1954. return(ret);
  1955. }
  1956. static void close_accept_socket(void)
  1957. {
  1958. BIO_printf(bio_err,"shutdown accept socket\n");
  1959. if (accept_socket >= 0)
  1960. {
  1961. SHUTDOWN2(accept_socket);
  1962. }
  1963. }
  1964. static int init_ssl_connection(SSL *con)
  1965. {
  1966. int i;
  1967. const char *str;
  1968. X509 *peer;
  1969. long verify_error;
  1970. MS_STATIC char buf[BUFSIZ];
  1971. if ((i=SSL_accept(con)) <= 0)
  1972. {
  1973. if (BIO_sock_should_retry(i))
  1974. {
  1975. BIO_printf(bio_s_out,"DELAY\n");
  1976. return(1);
  1977. }
  1978. BIO_printf(bio_err,"ERROR\n");
  1979. verify_error=SSL_get_verify_result(con);
  1980. if (verify_error != X509_V_OK)
  1981. {
  1982. BIO_printf(bio_err,"verify error:%s\n",
  1983. X509_verify_cert_error_string(verify_error));
  1984. }
  1985. else
  1986. ERR_print_errors(bio_err);
  1987. return(0);
  1988. }
  1989. PEM_write_bio_SSL_SESSION(bio_s_out,SSL_get_session(con));
  1990. peer=SSL_get_peer_certificate(con);
  1991. if (peer != NULL)
  1992. {
  1993. BIO_printf(bio_s_out,"Client certificate\n");
  1994. PEM_write_bio_X509(bio_s_out,peer);
  1995. X509_NAME_oneline(X509_get_subject_name(peer),buf,sizeof buf);
  1996. BIO_printf(bio_s_out,"subject=%s\n",buf);
  1997. X509_NAME_oneline(X509_get_issuer_name(peer),buf,sizeof buf);
  1998. BIO_printf(bio_s_out,"issuer=%s\n",buf);
  1999. X509_free(peer);
  2000. }
  2001. if (SSL_get_shared_ciphers(con,buf,sizeof buf) != NULL)
  2002. BIO_printf(bio_s_out,"Shared ciphers:%s\n",buf);
  2003. str=SSL_CIPHER_get_name(SSL_get_current_cipher(con));
  2004. BIO_printf(bio_s_out,"CIPHER is %s\n",(str != NULL)?str:"(NONE)");
  2005. if (con->hit) BIO_printf(bio_s_out,"Reused session-id\n");
  2006. if (SSL_ctrl(con,SSL_CTRL_GET_FLAGS,0,NULL) &
  2007. TLS1_FLAGS_TLS_PADDING_BUG)
  2008. BIO_printf(bio_s_out,"Peer has incorrect TLSv1 block padding\n");
  2009. #ifndef OPENSSL_NO_KRB5
  2010. if (con->kssl_ctx->client_princ != NULL)
  2011. {
  2012. BIO_printf(bio_s_out,"Kerberos peer principal is %s\n",
  2013. con->kssl_ctx->client_princ);
  2014. }
  2015. #endif /* OPENSSL_NO_KRB5 */
  2016. return(1);
  2017. }
  2018. #ifndef OPENSSL_NO_DH
  2019. static DH *load_dh_param(const char *dhfile)
  2020. {
  2021. DH *ret=NULL;
  2022. BIO *bio;
  2023. if ((bio=BIO_new_file(dhfile,"r")) == NULL)
  2024. goto err;
  2025. ret=PEM_read_bio_DHparams(bio,NULL,NULL,NULL);
  2026. err:
  2027. if (bio != NULL) BIO_free(bio);
  2028. return(ret);
  2029. }
  2030. #endif
  2031. #if 0
  2032. static int load_CA(SSL_CTX *ctx, char *file)
  2033. {
  2034. FILE *in;
  2035. X509 *x=NULL;
  2036. if ((in=fopen(file,"r")) == NULL)
  2037. return(0);
  2038. for (;;)
  2039. {
  2040. if (PEM_read_X509(in,&x,NULL) == NULL)
  2041. break;
  2042. SSL_CTX_add_client_CA(ctx,x);
  2043. }
  2044. if (x != NULL) X509_free(x);
  2045. fclose(in);
  2046. return(1);
  2047. }
  2048. #endif
  2049. static int www_body(char *hostname, int s, unsigned char *context)
  2050. {
  2051. char *buf=NULL;
  2052. int ret=1;
  2053. int i,j,k,blank,dot;
  2054. SSL *con;
  2055. const SSL_CIPHER *c;
  2056. BIO *io,*ssl_bio,*sbio;
  2057. long total_bytes;
  2058. buf=OPENSSL_malloc(bufsize);
  2059. if (buf == NULL) return(0);
  2060. io=BIO_new(BIO_f_buffer());
  2061. ssl_bio=BIO_new(BIO_f_ssl());
  2062. if ((io == NULL) || (ssl_bio == NULL)) goto err;
  2063. #ifdef FIONBIO
  2064. if (s_nbio)
  2065. {
  2066. unsigned long sl=1;
  2067. if (!s_quiet)
  2068. BIO_printf(bio_err,"turning on non blocking io\n");
  2069. if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)
  2070. ERR_print_errors(bio_err);
  2071. }
  2072. #endif
  2073. /* lets make the output buffer a reasonable size */
  2074. if (!BIO_set_write_buffer_size(io,bufsize)) goto err;
  2075. if ((con=SSL_new(ctx)) == NULL) goto err;
  2076. #ifndef OPENSSL_NO_TLSEXT
  2077. if (s_tlsextdebug)
  2078. {
  2079. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  2080. SSL_set_tlsext_debug_arg(con, bio_s_out);
  2081. }
  2082. #endif
  2083. #ifndef OPENSSL_NO_KRB5
  2084. if ((con->kssl_ctx = kssl_ctx_new()) != NULL)
  2085. {
  2086. kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE, KRB5SVC);
  2087. kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB, KRB5KEYTAB);
  2088. }
  2089. #endif /* OPENSSL_NO_KRB5 */
  2090. if(context) SSL_set_session_id_context(con, context,
  2091. strlen((char *)context));
  2092. sbio=BIO_new_socket(s,BIO_NOCLOSE);
  2093. if (s_nbio_test)
  2094. {
  2095. BIO *test;
  2096. test=BIO_new(BIO_f_nbio_test());
  2097. sbio=BIO_push(test,sbio);
  2098. }
  2099. SSL_set_bio(con,sbio,sbio);
  2100. SSL_set_accept_state(con);
  2101. /* SSL_set_fd(con,s); */
  2102. BIO_set_ssl(ssl_bio,con,BIO_CLOSE);
  2103. BIO_push(io,ssl_bio);
  2104. #ifdef CHARSET_EBCDIC
  2105. io = BIO_push(BIO_new(BIO_f_ebcdic_filter()),io);
  2106. #endif
  2107. if (s_debug)
  2108. {
  2109. con->debug=1;
  2110. BIO_set_callback(SSL_get_rbio(con),bio_dump_callback);
  2111. BIO_set_callback_arg(SSL_get_rbio(con),(char *)bio_s_out);
  2112. }
  2113. if (s_msg)
  2114. {
  2115. SSL_set_msg_callback(con, msg_cb);
  2116. SSL_set_msg_callback_arg(con, bio_s_out);
  2117. }
  2118. blank=0;
  2119. for (;;)
  2120. {
  2121. if (hack)
  2122. {
  2123. i=SSL_accept(con);
  2124. switch (SSL_get_error(con,i))
  2125. {
  2126. case SSL_ERROR_NONE:
  2127. break;
  2128. case SSL_ERROR_WANT_WRITE:
  2129. case SSL_ERROR_WANT_READ:
  2130. case SSL_ERROR_WANT_X509_LOOKUP:
  2131. continue;
  2132. case SSL_ERROR_SYSCALL:
  2133. case SSL_ERROR_SSL:
  2134. case SSL_ERROR_ZERO_RETURN:
  2135. ret=1;
  2136. goto err;
  2137. /* break; */
  2138. }
  2139. SSL_renegotiate(con);
  2140. SSL_write(con,NULL,0);
  2141. }
  2142. i=BIO_gets(io,buf,bufsize-1);
  2143. if (i < 0) /* error */
  2144. {
  2145. if (!BIO_should_retry(io))
  2146. {
  2147. if (!s_quiet)
  2148. ERR_print_errors(bio_err);
  2149. goto err;
  2150. }
  2151. else
  2152. {
  2153. BIO_printf(bio_s_out,"read R BLOCK\n");
  2154. #if defined(OPENSSL_SYS_NETWARE)
  2155. delay(1000);
  2156. #elif !defined(OPENSSL_SYS_MSDOS) && !defined(__DJGPP__)
  2157. sleep(1);
  2158. #endif
  2159. continue;
  2160. }
  2161. }
  2162. else if (i == 0) /* end of input */
  2163. {
  2164. ret=1;
  2165. goto end;
  2166. }
  2167. /* else we have data */
  2168. if ( ((www == 1) && (strncmp("GET ",buf,4) == 0)) ||
  2169. ((www == 2) && (strncmp("GET /stats ",buf,10) == 0)))
  2170. {
  2171. char *p;
  2172. X509 *peer;
  2173. STACK_OF(SSL_CIPHER) *sk;
  2174. static const char *space=" ";
  2175. BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  2176. BIO_puts(io,"<HTML><BODY BGCOLOR=\"#ffffff\">\n");
  2177. BIO_puts(io,"<pre>\n");
  2178. /* BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/
  2179. BIO_puts(io,"\n");
  2180. for (i=0; i<local_argc; i++)
  2181. {
  2182. BIO_puts(io,local_argv[i]);
  2183. BIO_write(io," ",1);
  2184. }
  2185. BIO_puts(io,"\n");
  2186. /* The following is evil and should not really
  2187. * be done */
  2188. BIO_printf(io,"Ciphers supported in s_server binary\n");
  2189. sk=SSL_get_ciphers(con);
  2190. j=sk_SSL_CIPHER_num(sk);
  2191. for (i=0; i<j; i++)
  2192. {
  2193. c=sk_SSL_CIPHER_value(sk,i);
  2194. BIO_printf(io,"%-11s:%-25s",
  2195. SSL_CIPHER_get_version(c),
  2196. SSL_CIPHER_get_name(c));
  2197. if ((((i+1)%2) == 0) && (i+1 != j))
  2198. BIO_puts(io,"\n");
  2199. }
  2200. BIO_puts(io,"\n");
  2201. p=SSL_get_shared_ciphers(con,buf,bufsize);
  2202. if (p != NULL)
  2203. {
  2204. BIO_printf(io,"---\nCiphers common between both SSL end points:\n");
  2205. j=i=0;
  2206. while (*p)
  2207. {
  2208. if (*p == ':')
  2209. {
  2210. BIO_write(io,space,26-j);
  2211. i++;
  2212. j=0;
  2213. BIO_write(io,((i%3)?" ":"\n"),1);
  2214. }
  2215. else
  2216. {
  2217. BIO_write(io,p,1);
  2218. j++;
  2219. }
  2220. p++;
  2221. }
  2222. BIO_puts(io,"\n");
  2223. }
  2224. BIO_printf(io,((con->hit)
  2225. ?"---\nReused, "
  2226. :"---\nNew, "));
  2227. c=SSL_get_current_cipher(con);
  2228. BIO_printf(io,"%s, Cipher is %s\n",
  2229. SSL_CIPHER_get_version(c),
  2230. SSL_CIPHER_get_name(c));
  2231. SSL_SESSION_print(io,SSL_get_session(con));
  2232. BIO_printf(io,"---\n");
  2233. print_stats(io,SSL_get_SSL_CTX(con));
  2234. BIO_printf(io,"---\n");
  2235. peer=SSL_get_peer_certificate(con);
  2236. if (peer != NULL)
  2237. {
  2238. BIO_printf(io,"Client certificate\n");
  2239. X509_print(io,peer);
  2240. PEM_write_bio_X509(io,peer);
  2241. }
  2242. else
  2243. BIO_puts(io,"no client certificate available\n");
  2244. BIO_puts(io,"</BODY></HTML>\r\n\r\n");
  2245. break;
  2246. }
  2247. else if ((www == 2 || www == 3)
  2248. && (strncmp("GET /",buf,5) == 0))
  2249. {
  2250. BIO *file;
  2251. char *p,*e;
  2252. static const char *text="HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
  2253. /* skip the '/' */
  2254. p= &(buf[5]);
  2255. dot = 1;
  2256. for (e=p; *e != '\0'; e++)
  2257. {
  2258. if (e[0] == ' ')
  2259. break;
  2260. switch (dot)
  2261. {
  2262. case 1:
  2263. dot = (e[0] == '.') ? 2 : 0;
  2264. break;
  2265. case 2:
  2266. dot = (e[0] == '.') ? 3 : 0;
  2267. break;
  2268. case 3:
  2269. dot = (e[0] == '/') ? -1 : 0;
  2270. break;
  2271. }
  2272. if (dot == 0)
  2273. dot = (e[0] == '/') ? 1 : 0;
  2274. }
  2275. dot = (dot == 3) || (dot == -1); /* filename contains ".." component */
  2276. if (*e == '\0')
  2277. {
  2278. BIO_puts(io,text);
  2279. BIO_printf(io,"'%s' is an invalid file name\r\n",p);
  2280. break;
  2281. }
  2282. *e='\0';
  2283. if (dot)
  2284. {
  2285. BIO_puts(io,text);
  2286. BIO_printf(io,"'%s' contains '..' reference\r\n",p);
  2287. break;
  2288. }
  2289. if (*p == '/')
  2290. {
  2291. BIO_puts(io,text);
  2292. BIO_printf(io,"'%s' is an invalid path\r\n",p);
  2293. break;
  2294. }
  2295. #if 0
  2296. /* append if a directory lookup */
  2297. if (e[-1] == '/')
  2298. strcat(p,"index.html");
  2299. #endif
  2300. /* if a directory, do the index thang */
  2301. if (app_isdir(p)>0)
  2302. {
  2303. #if 0 /* must check buffer size */
  2304. strcat(p,"/index.html");
  2305. #else
  2306. BIO_puts(io,text);
  2307. BIO_printf(io,"'%s' is a directory\r\n",p);
  2308. break;
  2309. #endif
  2310. }
  2311. if ((file=BIO_new_file(p,"r")) == NULL)
  2312. {
  2313. BIO_puts(io,text);
  2314. BIO_printf(io,"Error opening '%s'\r\n",p);
  2315. ERR_print_errors(io);
  2316. break;
  2317. }
  2318. if (!s_quiet)
  2319. BIO_printf(bio_err,"FILE:%s\n",p);
  2320. if (www == 2)
  2321. {
  2322. i=strlen(p);
  2323. if ( ((i > 5) && (strcmp(&(p[i-5]),".html") == 0)) ||
  2324. ((i > 4) && (strcmp(&(p[i-4]),".php") == 0)) ||
  2325. ((i > 4) && (strcmp(&(p[i-4]),".htm") == 0)))
  2326. BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  2327. else
  2328. BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
  2329. }
  2330. /* send the file */
  2331. total_bytes=0;
  2332. for (;;)
  2333. {
  2334. i=BIO_read(file,buf,bufsize);
  2335. if (i <= 0) break;
  2336. #ifdef RENEG
  2337. total_bytes+=i;
  2338. fprintf(stderr,"%d\n",i);
  2339. if (total_bytes > 3*1024)
  2340. {
  2341. total_bytes=0;
  2342. fprintf(stderr,"RENEGOTIATE\n");
  2343. SSL_renegotiate(con);
  2344. }
  2345. #endif
  2346. for (j=0; j<i; )
  2347. {
  2348. #ifdef RENEG
  2349. { static count=0; if (++count == 13) { SSL_renegotiate(con); } }
  2350. #endif
  2351. k=BIO_write(io,&(buf[j]),i-j);
  2352. if (k <= 0)
  2353. {
  2354. if (!BIO_should_retry(io))
  2355. goto write_error;
  2356. else
  2357. {
  2358. BIO_printf(bio_s_out,"rwrite W BLOCK\n");
  2359. }
  2360. }
  2361. else
  2362. {
  2363. j+=k;
  2364. }
  2365. }
  2366. }
  2367. write_error:
  2368. BIO_free(file);
  2369. break;
  2370. }
  2371. }
  2372. for (;;)
  2373. {
  2374. i=(int)BIO_flush(io);
  2375. if (i <= 0)
  2376. {
  2377. if (!BIO_should_retry(io))
  2378. break;
  2379. }
  2380. else
  2381. break;
  2382. }
  2383. end:
  2384. #if 1
  2385. /* make sure we re-use sessions */
  2386. SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
  2387. #else
  2388. /* This kills performance */
  2389. /* SSL_shutdown(con); A shutdown gets sent in the
  2390. * BIO_free_all(io) procession */
  2391. #endif
  2392. err:
  2393. if (ret >= 0)
  2394. BIO_printf(bio_s_out,"ACCEPT\n");
  2395. if (buf != NULL) OPENSSL_free(buf);
  2396. if (io != NULL) BIO_free_all(io);
  2397. /* if (ssl_bio != NULL) BIO_free(ssl_bio);*/
  2398. return(ret);
  2399. }
  2400. #ifndef OPENSSL_NO_RSA
  2401. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
  2402. {
  2403. BIGNUM *bn = NULL;
  2404. static RSA *rsa_tmp=NULL;
  2405. if (!rsa_tmp && ((bn = BN_new()) == NULL))
  2406. BIO_printf(bio_err,"Allocation error in generating RSA key\n");
  2407. if (!rsa_tmp && bn)
  2408. {
  2409. if (!s_quiet)
  2410. {
  2411. BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
  2412. (void)BIO_flush(bio_err);
  2413. }
  2414. if(!BN_set_word(bn, RSA_F4) || ((rsa_tmp = RSA_new()) == NULL) ||
  2415. !RSA_generate_key_ex(rsa_tmp, keylength, bn, NULL))
  2416. {
  2417. if(rsa_tmp) RSA_free(rsa_tmp);
  2418. rsa_tmp = NULL;
  2419. }
  2420. if (!s_quiet)
  2421. {
  2422. BIO_printf(bio_err,"\n");
  2423. (void)BIO_flush(bio_err);
  2424. }
  2425. BN_free(bn);
  2426. }
  2427. return(rsa_tmp);
  2428. }
  2429. #endif
  2430. #define MAX_SESSION_ID_ATTEMPTS 10
  2431. static int generate_session_id(const SSL *ssl, unsigned char *id,
  2432. unsigned int *id_len)
  2433. {
  2434. unsigned int count = 0;
  2435. do {
  2436. RAND_pseudo_bytes(id, *id_len);
  2437. /* Prefix the session_id with the required prefix. NB: If our
  2438. * prefix is too long, clip it - but there will be worse effects
  2439. * anyway, eg. the server could only possibly create 1 session
  2440. * ID (ie. the prefix!) so all future session negotiations will
  2441. * fail due to conflicts. */
  2442. memcpy(id, session_id_prefix,
  2443. (strlen(session_id_prefix) < *id_len) ?
  2444. strlen(session_id_prefix) : *id_len);
  2445. }
  2446. while(SSL_has_matching_session_id(ssl, id, *id_len) &&
  2447. (++count < MAX_SESSION_ID_ATTEMPTS));
  2448. if(count >= MAX_SESSION_ID_ATTEMPTS)
  2449. return 0;
  2450. return 1;
  2451. }