2
0

s_server.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674
  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. #ifndef OPENSSL_NO_DTLS1
  280. static int cert_chain = 0;
  281. #endif
  282. #ifndef OPENSSL_NO_PSK
  283. static char *psk_identity="Client_identity";
  284. char *psk_key=NULL; /* by default PSK is not used */
  285. static unsigned int psk_server_cb(SSL *ssl, const char *identity,
  286. unsigned char *psk, unsigned int max_psk_len)
  287. {
  288. unsigned int psk_len = 0;
  289. int ret;
  290. BIGNUM *bn = NULL;
  291. if (s_debug)
  292. BIO_printf(bio_s_out,"psk_server_cb\n");
  293. if (!identity)
  294. {
  295. BIO_printf(bio_err,"Error: client did not send PSK identity\n");
  296. goto out_err;
  297. }
  298. if (s_debug)
  299. BIO_printf(bio_s_out,"identity_len=%d identity=%s\n",
  300. identity ? (int)strlen(identity) : 0, identity);
  301. /* here we could lookup the given identity e.g. from a database */
  302. if (strcmp(identity, psk_identity) != 0)
  303. {
  304. BIO_printf(bio_s_out, "PSK error: client identity not found"
  305. " (got '%s' expected '%s')\n", identity,
  306. psk_identity);
  307. goto out_err;
  308. }
  309. if (s_debug)
  310. BIO_printf(bio_s_out, "PSK client identity found\n");
  311. /* convert the PSK key to binary */
  312. ret = BN_hex2bn(&bn, psk_key);
  313. if (!ret)
  314. {
  315. BIO_printf(bio_err,"Could not convert PSK key '%s' to BIGNUM\n", psk_key);
  316. if (bn)
  317. BN_free(bn);
  318. return 0;
  319. }
  320. if (BN_num_bytes(bn) > (int)max_psk_len)
  321. {
  322. BIO_printf(bio_err,"psk buffer of callback is too small (%d) for key (%d)\n",
  323. max_psk_len, BN_num_bytes(bn));
  324. BN_free(bn);
  325. return 0;
  326. }
  327. ret = BN_bn2bin(bn, psk);
  328. BN_free(bn);
  329. if (ret < 0)
  330. goto out_err;
  331. psk_len = (unsigned int)ret;
  332. if (s_debug)
  333. BIO_printf(bio_s_out, "fetched PSK len=%d\n", psk_len);
  334. return psk_len;
  335. out_err:
  336. if (s_debug)
  337. BIO_printf(bio_err, "Error in PSK server callback\n");
  338. return 0;
  339. }
  340. #endif
  341. #ifdef MONOLITH
  342. static void s_server_init(void)
  343. {
  344. accept_socket=-1;
  345. cipher=NULL;
  346. s_server_verify=SSL_VERIFY_NONE;
  347. s_dcert_file=NULL;
  348. s_dkey_file=NULL;
  349. s_cert_file=TEST_CERT;
  350. s_key_file=NULL;
  351. #ifndef OPENSSL_NO_TLSEXT
  352. s_cert_file2=TEST_CERT2;
  353. s_key_file2=NULL;
  354. ctx2=NULL;
  355. #endif
  356. #ifdef FIONBIO
  357. s_nbio=0;
  358. #endif
  359. s_nbio_test=0;
  360. ctx=NULL;
  361. www=0;
  362. bio_s_out=NULL;
  363. s_debug=0;
  364. s_msg=0;
  365. s_quiet=0;
  366. hack=0;
  367. #ifndef OPENSSL_NO_ENGINE
  368. engine_id=NULL;
  369. #endif
  370. }
  371. #endif
  372. static void sv_usage(void)
  373. {
  374. BIO_printf(bio_err,"usage: s_server [args ...]\n");
  375. BIO_printf(bio_err,"\n");
  376. BIO_printf(bio_err," -accept arg - port to accept on (default is %d)\n",PORT);
  377. BIO_printf(bio_err," -context arg - set session ID context\n");
  378. BIO_printf(bio_err," -verify arg - turn on peer certificate verification\n");
  379. BIO_printf(bio_err," -Verify arg - turn on peer certificate verification, must have a cert.\n");
  380. BIO_printf(bio_err," -cert arg - certificate file to use\n");
  381. BIO_printf(bio_err," (default is %s)\n",TEST_CERT);
  382. BIO_printf(bio_err," -crl_check - check the peer certificate has not been revoked by its CA.\n" \
  383. " The CRL(s) are appended to the certificate file\n");
  384. BIO_printf(bio_err," -crl_check_all - check the peer certificate has not been revoked by its CA\n" \
  385. " or any other CRL in the CA chain. CRL(s) are appened to the\n" \
  386. " the certificate file.\n");
  387. BIO_printf(bio_err," -certform arg - certificate format (PEM or DER) PEM default\n");
  388. BIO_printf(bio_err," -key arg - Private Key file to use, in cert file if\n");
  389. BIO_printf(bio_err," not specified (default is %s)\n",TEST_CERT);
  390. BIO_printf(bio_err," -keyform arg - key format (PEM, DER or ENGINE) PEM default\n");
  391. BIO_printf(bio_err," -pass arg - private key file pass phrase source\n");
  392. BIO_printf(bio_err," -dcert arg - second certificate file to use (usually for DSA)\n");
  393. BIO_printf(bio_err," -dcertform x - second certificate format (PEM or DER) PEM default\n");
  394. BIO_printf(bio_err," -dkey arg - second private key file to use (usually for DSA)\n");
  395. BIO_printf(bio_err," -dkeyform arg - second key format (PEM, DER or ENGINE) PEM default\n");
  396. BIO_printf(bio_err," -dpass arg - second private key file pass phrase source\n");
  397. BIO_printf(bio_err," -dhparam arg - DH parameter file to use, in cert file if not specified\n");
  398. BIO_printf(bio_err," or a default set of parameters is used\n");
  399. #ifndef OPENSSL_NO_ECDH
  400. BIO_printf(bio_err," -named_curve arg - Elliptic curve name to use for ephemeral ECDH keys.\n" \
  401. " Use \"openssl ecparam -list_curves\" for all names\n" \
  402. " (default is nistp256).\n");
  403. #endif
  404. #ifdef FIONBIO
  405. BIO_printf(bio_err," -nbio - Run with non-blocking IO\n");
  406. #endif
  407. BIO_printf(bio_err," -nbio_test - test with the non-blocking test bio\n");
  408. BIO_printf(bio_err," -crlf - convert LF from terminal into CRLF\n");
  409. BIO_printf(bio_err," -debug - Print more output\n");
  410. BIO_printf(bio_err," -msg - Show protocol messages\n");
  411. BIO_printf(bio_err," -state - Print the SSL states\n");
  412. BIO_printf(bio_err," -CApath arg - PEM format directory of CA's\n");
  413. BIO_printf(bio_err," -CAfile arg - PEM format file of CA's\n");
  414. BIO_printf(bio_err," -nocert - Don't use any certificates (Anon-DH)\n");
  415. BIO_printf(bio_err," -cipher arg - play with 'openssl ciphers' to see what goes here\n");
  416. BIO_printf(bio_err," -serverpref - Use server's cipher preferences\n");
  417. BIO_printf(bio_err," -quiet - No server output\n");
  418. BIO_printf(bio_err," -no_tmp_rsa - Do not generate a tmp RSA key\n");
  419. #ifndef OPENSSL_NO_PSK
  420. BIO_printf(bio_err," -psk_hint arg - PSK identity hint to use\n");
  421. BIO_printf(bio_err," -psk arg - PSK in hex (without 0x)\n");
  422. # ifndef OPENSSL_NO_JPAKE
  423. BIO_printf(bio_err," -jpake arg - JPAKE secret to use\n");
  424. # endif
  425. #endif
  426. BIO_printf(bio_err," -ssl2 - Just talk SSLv2\n");
  427. BIO_printf(bio_err," -ssl3 - Just talk SSLv3\n");
  428. BIO_printf(bio_err," -tls1 - Just talk TLSv1\n");
  429. BIO_printf(bio_err," -dtls1 - Just talk DTLSv1\n");
  430. BIO_printf(bio_err," -timeout - Enable timeouts\n");
  431. BIO_printf(bio_err," -mtu - Set link layer MTU\n");
  432. BIO_printf(bio_err," -chain - Read a certificate chain\n");
  433. BIO_printf(bio_err," -no_ssl2 - Just disable SSLv2\n");
  434. BIO_printf(bio_err," -no_ssl3 - Just disable SSLv3\n");
  435. BIO_printf(bio_err," -no_tls1 - Just disable TLSv1\n");
  436. #ifndef OPENSSL_NO_DH
  437. BIO_printf(bio_err," -no_dhe - Disable ephemeral DH\n");
  438. #endif
  439. #ifndef OPENSSL_NO_ECDH
  440. BIO_printf(bio_err," -no_ecdhe - Disable ephemeral ECDH\n");
  441. #endif
  442. BIO_printf(bio_err," -bugs - Turn on SSL bug compatibility\n");
  443. BIO_printf(bio_err," -www - Respond to a 'GET /' with a status page\n");
  444. BIO_printf(bio_err," -WWW - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
  445. BIO_printf(bio_err," -HTTP - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
  446. BIO_printf(bio_err," with the assumption it contains a complete HTTP response.\n");
  447. #ifndef OPENSSL_NO_ENGINE
  448. BIO_printf(bio_err," -engine id - Initialise and use the specified engine\n");
  449. #endif
  450. BIO_printf(bio_err," -id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'\n");
  451. BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
  452. #ifndef OPENSSL_NO_TLSEXT
  453. BIO_printf(bio_err," -servername host - servername for HostName TLS extension\n");
  454. BIO_printf(bio_err," -servername_fatal - on mismatch send fatal alert (default warning alert)\n");
  455. BIO_printf(bio_err," -cert2 arg - certificate file to use for servername\n");
  456. BIO_printf(bio_err," (default is %s)\n",TEST_CERT2);
  457. BIO_printf(bio_err," -key2 arg - Private Key file to use for servername, in cert file if\n");
  458. BIO_printf(bio_err," not specified (default is %s)\n",TEST_CERT2);
  459. BIO_printf(bio_err," -tlsextdebug - hex dump of all TLS extensions received\n");
  460. BIO_printf(bio_err," -no_ticket - disable use of RFC4507bis session tickets\n");
  461. #endif
  462. }
  463. static int local_argc=0;
  464. static char **local_argv;
  465. #ifdef CHARSET_EBCDIC
  466. static int ebcdic_new(BIO *bi);
  467. static int ebcdic_free(BIO *a);
  468. static int ebcdic_read(BIO *b, char *out, int outl);
  469. static int ebcdic_write(BIO *b, const char *in, int inl);
  470. static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr);
  471. static int ebcdic_gets(BIO *bp, char *buf, int size);
  472. static int ebcdic_puts(BIO *bp, const char *str);
  473. #define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
  474. static BIO_METHOD methods_ebcdic=
  475. {
  476. BIO_TYPE_EBCDIC_FILTER,
  477. "EBCDIC/ASCII filter",
  478. ebcdic_write,
  479. ebcdic_read,
  480. ebcdic_puts,
  481. ebcdic_gets,
  482. ebcdic_ctrl,
  483. ebcdic_new,
  484. ebcdic_free,
  485. };
  486. typedef struct
  487. {
  488. size_t alloced;
  489. char buff[1];
  490. } EBCDIC_OUTBUFF;
  491. BIO_METHOD *BIO_f_ebcdic_filter()
  492. {
  493. return(&methods_ebcdic);
  494. }
  495. static int ebcdic_new(BIO *bi)
  496. {
  497. EBCDIC_OUTBUFF *wbuf;
  498. wbuf = (EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + 1024);
  499. wbuf->alloced = 1024;
  500. wbuf->buff[0] = '\0';
  501. bi->ptr=(char *)wbuf;
  502. bi->init=1;
  503. bi->flags=0;
  504. return(1);
  505. }
  506. static int ebcdic_free(BIO *a)
  507. {
  508. if (a == NULL) return(0);
  509. if (a->ptr != NULL)
  510. OPENSSL_free(a->ptr);
  511. a->ptr=NULL;
  512. a->init=0;
  513. a->flags=0;
  514. return(1);
  515. }
  516. static int ebcdic_read(BIO *b, char *out, int outl)
  517. {
  518. int ret=0;
  519. if (out == NULL || outl == 0) return(0);
  520. if (b->next_bio == NULL) return(0);
  521. ret=BIO_read(b->next_bio,out,outl);
  522. if (ret > 0)
  523. ascii2ebcdic(out,out,ret);
  524. return(ret);
  525. }
  526. static int ebcdic_write(BIO *b, const char *in, int inl)
  527. {
  528. EBCDIC_OUTBUFF *wbuf;
  529. int ret=0;
  530. int num;
  531. unsigned char n;
  532. if ((in == NULL) || (inl <= 0)) return(0);
  533. if (b->next_bio == NULL) return(0);
  534. wbuf=(EBCDIC_OUTBUFF *)b->ptr;
  535. if (inl > (num = wbuf->alloced))
  536. {
  537. num = num + num; /* double the size */
  538. if (num < inl)
  539. num = inl;
  540. OPENSSL_free(wbuf);
  541. wbuf=(EBCDIC_OUTBUFF *)OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + num);
  542. wbuf->alloced = num;
  543. wbuf->buff[0] = '\0';
  544. b->ptr=(char *)wbuf;
  545. }
  546. ebcdic2ascii(wbuf->buff, in, inl);
  547. ret=BIO_write(b->next_bio, wbuf->buff, inl);
  548. return(ret);
  549. }
  550. static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr)
  551. {
  552. long ret;
  553. if (b->next_bio == NULL) return(0);
  554. switch (cmd)
  555. {
  556. case BIO_CTRL_DUP:
  557. ret=0L;
  558. break;
  559. default:
  560. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  561. break;
  562. }
  563. return(ret);
  564. }
  565. static int ebcdic_gets(BIO *bp, char *buf, int size)
  566. {
  567. int i, ret=0;
  568. if (bp->next_bio == NULL) return(0);
  569. /* return(BIO_gets(bp->next_bio,buf,size));*/
  570. for (i=0; i<size-1; ++i)
  571. {
  572. ret = ebcdic_read(bp,&buf[i],1);
  573. if (ret <= 0)
  574. break;
  575. else if (buf[i] == '\n')
  576. {
  577. ++i;
  578. break;
  579. }
  580. }
  581. if (i < size)
  582. buf[i] = '\0';
  583. return (ret < 0 && i == 0) ? ret : i;
  584. }
  585. static int ebcdic_puts(BIO *bp, const char *str)
  586. {
  587. if (bp->next_bio == NULL) return(0);
  588. return ebcdic_write(bp, str, strlen(str));
  589. }
  590. #endif
  591. #ifndef OPENSSL_NO_TLSEXT
  592. /* This is a context that we pass to callbacks */
  593. typedef struct tlsextctx_st {
  594. char * servername;
  595. BIO * biodebug;
  596. int extension_error;
  597. } tlsextctx;
  598. static int MS_CALLBACK ssl_servername_cb(SSL *s, int *ad, void *arg)
  599. {
  600. tlsextctx * p = (tlsextctx *) arg;
  601. const char * servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
  602. if (servername && p->biodebug)
  603. BIO_printf(p->biodebug,"Hostname in TLS extension: \"%s\"\n",servername);
  604. if (!p->servername)
  605. return SSL_TLSEXT_ERR_NOACK;
  606. if (servername)
  607. {
  608. if (strcmp(servername,p->servername))
  609. return p->extension_error;
  610. if (ctx2)
  611. {
  612. BIO_printf(p->biodebug,"Switching server context.\n");
  613. SSL_set_SSL_CTX(s,ctx2);
  614. }
  615. }
  616. return SSL_TLSEXT_ERR_OK;
  617. }
  618. /* Structure passed to cert status callback */
  619. typedef struct tlsextstatusctx_st {
  620. /* Default responder to use */
  621. char *host, *path, *port;
  622. int use_ssl;
  623. int timeout;
  624. BIO *err;
  625. int verbose;
  626. } tlsextstatusctx;
  627. static tlsextstatusctx tlscstatp = {NULL, NULL, NULL, 0, -1, NULL, 0};
  628. /* Certificate Status callback. This is called when a client includes a
  629. * certificate status request extension.
  630. *
  631. * This is a simplified version. It examines certificates each time and
  632. * makes one OCSP responder query for each request.
  633. *
  634. * A full version would store details such as the OCSP certificate IDs and
  635. * minimise the number of OCSP responses by caching them until they were
  636. * considered "expired".
  637. */
  638. static int cert_status_cb(SSL *s, void *arg)
  639. {
  640. tlsextstatusctx *srctx = arg;
  641. BIO *err = srctx->err;
  642. char *host, *port, *path;
  643. int use_ssl;
  644. unsigned char *rspder = NULL;
  645. int rspderlen;
  646. STACK_OF(OPENSSL_STRING) *aia = NULL;
  647. X509 *x = NULL;
  648. X509_STORE_CTX inctx;
  649. X509_OBJECT obj;
  650. OCSP_REQUEST *req = NULL;
  651. OCSP_RESPONSE *resp = NULL;
  652. OCSP_CERTID *id = NULL;
  653. STACK_OF(X509_EXTENSION) *exts;
  654. int ret = SSL_TLSEXT_ERR_NOACK;
  655. int i;
  656. #if 0
  657. STACK_OF(OCSP_RESPID) *ids;
  658. SSL_get_tlsext_status_ids(s, &ids);
  659. BIO_printf(err, "cert_status: received %d ids\n", sk_OCSP_RESPID_num(ids));
  660. #endif
  661. if (srctx->verbose)
  662. BIO_puts(err, "cert_status: callback called\n");
  663. /* Build up OCSP query from server certificate */
  664. x = SSL_get_certificate(s);
  665. aia = X509_get1_ocsp(x);
  666. if (aia)
  667. {
  668. if (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia, 0),
  669. &host, &port, &path, &use_ssl))
  670. {
  671. BIO_puts(err, "cert_status: can't parse AIA URL\n");
  672. goto err;
  673. }
  674. if (srctx->verbose)
  675. BIO_printf(err, "cert_status: AIA URL: %s\n",
  676. sk_OPENSSL_STRING_value(aia, 0));
  677. }
  678. else
  679. {
  680. if (!srctx->host)
  681. {
  682. BIO_puts(srctx->err, "cert_status: no AIA and no default responder URL\n");
  683. goto done;
  684. }
  685. host = srctx->host;
  686. path = srctx->path;
  687. port = srctx->port;
  688. use_ssl = srctx->use_ssl;
  689. }
  690. if (!X509_STORE_CTX_init(&inctx,
  691. SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)),
  692. NULL, NULL))
  693. goto err;
  694. if (X509_STORE_get_by_subject(&inctx,X509_LU_X509,
  695. X509_get_issuer_name(x),&obj) <= 0)
  696. {
  697. BIO_puts(err, "cert_status: Can't retrieve issuer certificate.\n");
  698. X509_STORE_CTX_cleanup(&inctx);
  699. goto done;
  700. }
  701. req = OCSP_REQUEST_new();
  702. if (!req)
  703. goto err;
  704. id = OCSP_cert_to_id(NULL, x, obj.data.x509);
  705. X509_free(obj.data.x509);
  706. X509_STORE_CTX_cleanup(&inctx);
  707. if (!id)
  708. goto err;
  709. if (!OCSP_request_add0_id(req, id))
  710. goto err;
  711. id = NULL;
  712. /* Add any extensions to the request */
  713. SSL_get_tlsext_status_exts(s, &exts);
  714. for (i = 0; i < sk_X509_EXTENSION_num(exts); i++)
  715. {
  716. X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
  717. if (!OCSP_REQUEST_add_ext(req, ext, -1))
  718. goto err;
  719. }
  720. resp = process_responder(err, req, host, path, port, use_ssl,
  721. srctx->timeout);
  722. if (!resp)
  723. {
  724. BIO_puts(err, "cert_status: error querying responder\n");
  725. goto done;
  726. }
  727. rspderlen = i2d_OCSP_RESPONSE(resp, &rspder);
  728. if (rspderlen <= 0)
  729. goto err;
  730. SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen);
  731. if (srctx->verbose)
  732. {
  733. BIO_puts(err, "cert_status: ocsp response sent:\n");
  734. OCSP_RESPONSE_print(err, resp, 2);
  735. }
  736. ret = SSL_TLSEXT_ERR_OK;
  737. done:
  738. if (ret != SSL_TLSEXT_ERR_OK)
  739. ERR_print_errors(err);
  740. if (aia)
  741. {
  742. OPENSSL_free(host);
  743. OPENSSL_free(path);
  744. OPENSSL_free(port);
  745. X509_email_free(aia);
  746. }
  747. if (id)
  748. OCSP_CERTID_free(id);
  749. if (req)
  750. OCSP_REQUEST_free(req);
  751. if (resp)
  752. OCSP_RESPONSE_free(resp);
  753. return ret;
  754. err:
  755. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  756. goto done;
  757. }
  758. #endif
  759. int MAIN(int, char **);
  760. #ifndef OPENSSL_NO_JPAKE
  761. static char *jpake_secret = NULL;
  762. #endif
  763. int MAIN(int argc, char *argv[])
  764. {
  765. X509_VERIFY_PARAM *vpm = NULL;
  766. int badarg = 0;
  767. short port=PORT;
  768. char *CApath=NULL,*CAfile=NULL;
  769. unsigned char *context = NULL;
  770. char *dhfile = NULL;
  771. #ifndef OPENSSL_NO_ECDH
  772. char *named_curve = NULL;
  773. #endif
  774. int badop=0,bugs=0;
  775. int ret=1;
  776. int off=0;
  777. int no_tmp_rsa=0,no_dhe=0,no_ecdhe=0,nocert=0;
  778. int state=0;
  779. const SSL_METHOD *meth=NULL;
  780. int socket_type=SOCK_STREAM;
  781. ENGINE *e=NULL;
  782. char *inrand=NULL;
  783. int s_cert_format = FORMAT_PEM, s_key_format = FORMAT_PEM;
  784. char *passarg = NULL, *pass = NULL;
  785. char *dpassarg = NULL, *dpass = NULL;
  786. int s_dcert_format = FORMAT_PEM, s_dkey_format = FORMAT_PEM;
  787. X509 *s_cert = NULL, *s_dcert = NULL;
  788. EVP_PKEY *s_key = NULL, *s_dkey = NULL;
  789. #ifndef OPENSSL_NO_TLSEXT
  790. EVP_PKEY *s_key2 = NULL;
  791. X509 *s_cert2 = NULL;
  792. #endif
  793. #ifndef OPENSSL_NO_TLSEXT
  794. tlsextctx tlsextcbp = {NULL, NULL, SSL_TLSEXT_ERR_ALERT_WARNING};
  795. #endif
  796. #ifndef OPENSSL_NO_PSK
  797. /* by default do not send a PSK identity hint */
  798. static char *psk_identity_hint=NULL;
  799. #endif
  800. #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
  801. meth=SSLv23_server_method();
  802. #elif !defined(OPENSSL_NO_SSL3)
  803. meth=SSLv3_server_method();
  804. #elif !defined(OPENSSL_NO_SSL2)
  805. meth=SSLv2_server_method();
  806. #endif
  807. local_argc=argc;
  808. local_argv=argv;
  809. apps_startup();
  810. #ifdef MONOLITH
  811. s_server_init();
  812. #endif
  813. if (bio_err == NULL)
  814. bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
  815. if (!load_config(bio_err, NULL))
  816. goto end;
  817. verify_depth=0;
  818. #ifdef FIONBIO
  819. s_nbio=0;
  820. #endif
  821. s_nbio_test=0;
  822. argc--;
  823. argv++;
  824. while (argc >= 1)
  825. {
  826. if ((strcmp(*argv,"-port") == 0) ||
  827. (strcmp(*argv,"-accept") == 0))
  828. {
  829. if (--argc < 1) goto bad;
  830. if (!extract_port(*(++argv),&port))
  831. goto bad;
  832. }
  833. else if (strcmp(*argv,"-verify") == 0)
  834. {
  835. s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE;
  836. if (--argc < 1) goto bad;
  837. verify_depth=atoi(*(++argv));
  838. BIO_printf(bio_err,"verify depth is %d\n",verify_depth);
  839. }
  840. else if (strcmp(*argv,"-Verify") == 0)
  841. {
  842. s_server_verify=SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT|
  843. SSL_VERIFY_CLIENT_ONCE;
  844. if (--argc < 1) goto bad;
  845. verify_depth=atoi(*(++argv));
  846. BIO_printf(bio_err,"verify depth is %d, must return a certificate\n",verify_depth);
  847. }
  848. else if (strcmp(*argv,"-context") == 0)
  849. {
  850. if (--argc < 1) goto bad;
  851. context= (unsigned char *)*(++argv);
  852. }
  853. else if (strcmp(*argv,"-cert") == 0)
  854. {
  855. if (--argc < 1) goto bad;
  856. s_cert_file= *(++argv);
  857. }
  858. else if (strcmp(*argv,"-certform") == 0)
  859. {
  860. if (--argc < 1) goto bad;
  861. s_cert_format = str2fmt(*(++argv));
  862. }
  863. else if (strcmp(*argv,"-key") == 0)
  864. {
  865. if (--argc < 1) goto bad;
  866. s_key_file= *(++argv);
  867. }
  868. else if (strcmp(*argv,"-keyform") == 0)
  869. {
  870. if (--argc < 1) goto bad;
  871. s_key_format = str2fmt(*(++argv));
  872. }
  873. else if (strcmp(*argv,"-pass") == 0)
  874. {
  875. if (--argc < 1) goto bad;
  876. passarg = *(++argv);
  877. }
  878. else if (strcmp(*argv,"-dhparam") == 0)
  879. {
  880. if (--argc < 1) goto bad;
  881. dhfile = *(++argv);
  882. }
  883. #ifndef OPENSSL_NO_ECDH
  884. else if (strcmp(*argv,"-named_curve") == 0)
  885. {
  886. if (--argc < 1) goto bad;
  887. named_curve = *(++argv);
  888. }
  889. #endif
  890. else if (strcmp(*argv,"-dcertform") == 0)
  891. {
  892. if (--argc < 1) goto bad;
  893. s_dcert_format = str2fmt(*(++argv));
  894. }
  895. else if (strcmp(*argv,"-dcert") == 0)
  896. {
  897. if (--argc < 1) goto bad;
  898. s_dcert_file= *(++argv);
  899. }
  900. else if (strcmp(*argv,"-dkeyform") == 0)
  901. {
  902. if (--argc < 1) goto bad;
  903. s_dkey_format = str2fmt(*(++argv));
  904. }
  905. else if (strcmp(*argv,"-dpass") == 0)
  906. {
  907. if (--argc < 1) goto bad;
  908. dpassarg = *(++argv);
  909. }
  910. else if (strcmp(*argv,"-dkey") == 0)
  911. {
  912. if (--argc < 1) goto bad;
  913. s_dkey_file= *(++argv);
  914. }
  915. else if (strcmp(*argv,"-nocert") == 0)
  916. {
  917. nocert=1;
  918. }
  919. else if (strcmp(*argv,"-CApath") == 0)
  920. {
  921. if (--argc < 1) goto bad;
  922. CApath= *(++argv);
  923. }
  924. else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm))
  925. {
  926. if (badarg)
  927. goto bad;
  928. continue;
  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. if (vpm)
  1303. SSL_CTX_set1_param(ctx, vpm);
  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. if (vpm)
  1349. SSL_CTX_set1_param(ctx2, vpm);
  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. struct timeval timeout;
  1613. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)
  1614. struct timeval tv;
  1615. #else
  1616. struct timeval *timeoutp;
  1617. #endif
  1618. if ((buf=OPENSSL_malloc(bufsize)) == NULL)
  1619. {
  1620. BIO_printf(bio_err,"out of memory\n");
  1621. goto err;
  1622. }
  1623. #ifdef FIONBIO
  1624. if (s_nbio)
  1625. {
  1626. unsigned long sl=1;
  1627. if (!s_quiet)
  1628. BIO_printf(bio_err,"turning on non blocking io\n");
  1629. if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)
  1630. ERR_print_errors(bio_err);
  1631. }
  1632. #endif
  1633. if (con == NULL) {
  1634. con=SSL_new(ctx);
  1635. #ifndef OPENSSL_NO_TLSEXT
  1636. if (s_tlsextdebug)
  1637. {
  1638. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  1639. SSL_set_tlsext_debug_arg(con, bio_s_out);
  1640. }
  1641. if (s_tlsextstatus)
  1642. {
  1643. SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb);
  1644. tlscstatp.err = bio_err;
  1645. SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp);
  1646. }
  1647. #endif
  1648. #ifndef OPENSSL_NO_KRB5
  1649. if ((con->kssl_ctx = kssl_ctx_new()) != NULL)
  1650. {
  1651. kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE,
  1652. KRB5SVC);
  1653. kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB,
  1654. KRB5KEYTAB);
  1655. }
  1656. #endif /* OPENSSL_NO_KRB5 */
  1657. if(context)
  1658. SSL_set_session_id_context(con, context,
  1659. strlen((char *)context));
  1660. }
  1661. SSL_clear(con);
  1662. #if 0
  1663. #ifdef TLSEXT_TYPE_opaque_prf_input
  1664. SSL_set_tlsext_opaque_prf_input(con, "Test server", 11);
  1665. #endif
  1666. #endif
  1667. if (SSL_version(con) == DTLS1_VERSION)
  1668. {
  1669. sbio=BIO_new_dgram(s,BIO_NOCLOSE);
  1670. if (enable_timeouts)
  1671. {
  1672. timeout.tv_sec = 0;
  1673. timeout.tv_usec = DGRAM_RCV_TIMEOUT;
  1674. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
  1675. timeout.tv_sec = 0;
  1676. timeout.tv_usec = DGRAM_SND_TIMEOUT;
  1677. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
  1678. }
  1679. if (socket_mtu > 28)
  1680. {
  1681. SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
  1682. SSL_set_mtu(con, socket_mtu - 28);
  1683. }
  1684. else
  1685. /* want to do MTU discovery */
  1686. BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
  1687. /* turn on cookie exchange */
  1688. SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE);
  1689. }
  1690. else
  1691. sbio=BIO_new_socket(s,BIO_NOCLOSE);
  1692. if (s_nbio_test)
  1693. {
  1694. BIO *test;
  1695. test=BIO_new(BIO_f_nbio_test());
  1696. sbio=BIO_push(test,sbio);
  1697. }
  1698. #ifndef OPENSSL_NO_JPAKE
  1699. if(jpake_secret)
  1700. jpake_server_auth(bio_s_out, sbio, jpake_secret);
  1701. #endif
  1702. SSL_set_bio(con,sbio,sbio);
  1703. SSL_set_accept_state(con);
  1704. /* SSL_set_fd(con,s); */
  1705. if (s_debug)
  1706. {
  1707. con->debug=1;
  1708. BIO_set_callback(SSL_get_rbio(con),bio_dump_callback);
  1709. BIO_set_callback_arg(SSL_get_rbio(con),(char *)bio_s_out);
  1710. }
  1711. if (s_msg)
  1712. {
  1713. SSL_set_msg_callback(con, msg_cb);
  1714. SSL_set_msg_callback_arg(con, bio_s_out);
  1715. }
  1716. #ifndef OPENSSL_NO_TLSEXT
  1717. if (s_tlsextdebug)
  1718. {
  1719. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  1720. SSL_set_tlsext_debug_arg(con, bio_s_out);
  1721. }
  1722. #endif
  1723. width=s+1;
  1724. for (;;)
  1725. {
  1726. int read_from_terminal;
  1727. int read_from_sslcon;
  1728. read_from_terminal = 0;
  1729. read_from_sslcon = SSL_pending(con);
  1730. if (!read_from_sslcon)
  1731. {
  1732. FD_ZERO(&readfds);
  1733. #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) && !defined(OPENSSL_SYS_BEOS_R5)
  1734. openssl_fdset(fileno(stdin),&readfds);
  1735. #endif
  1736. openssl_fdset(s,&readfds);
  1737. /* Note: under VMS with SOCKETSHR the second parameter is
  1738. * currently of type (int *) whereas under other systems
  1739. * it is (void *) if you don't have a cast it will choke
  1740. * the compiler: if you do have a cast then you can either
  1741. * go for (int *) or (void *).
  1742. */
  1743. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)
  1744. /* Under DOS (non-djgpp) and Windows we can't select on stdin: only
  1745. * on sockets. As a workaround we timeout the select every
  1746. * second and check for any keypress. In a proper Windows
  1747. * application we wouldn't do this because it is inefficient.
  1748. */
  1749. tv.tv_sec = 1;
  1750. tv.tv_usec = 0;
  1751. i=select(width,(void *)&readfds,NULL,NULL,&tv);
  1752. if((i < 0) || (!i && !_kbhit() ) )continue;
  1753. if(_kbhit())
  1754. read_from_terminal = 1;
  1755. #elif defined(OPENSSL_SYS_BEOS_R5)
  1756. /* Under BeOS-R5 the situation is similar to DOS */
  1757. tv.tv_sec = 1;
  1758. tv.tv_usec = 0;
  1759. (void)fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
  1760. i=select(width,(void *)&readfds,NULL,NULL,&tv);
  1761. if ((i < 0) || (!i && read(fileno(stdin), buf, 0) < 0))
  1762. continue;
  1763. if (read(fileno(stdin), buf, 0) >= 0)
  1764. read_from_terminal = 1;
  1765. (void)fcntl(fileno(stdin), F_SETFL, 0);
  1766. #else
  1767. if ((SSL_version(con) == DTLS1_VERSION) &&
  1768. DTLSv1_get_timeout(con, &timeout))
  1769. timeoutp = &timeout;
  1770. else
  1771. timeoutp = NULL;
  1772. i=select(width,(void *)&readfds,NULL,NULL,timeoutp);
  1773. if ((SSL_version(con) == DTLS1_VERSION) && DTLSv1_handle_timeout(con) > 0)
  1774. {
  1775. BIO_printf(bio_err,"TIMEOUT occured\n");
  1776. }
  1777. if (i <= 0) continue;
  1778. if (FD_ISSET(fileno(stdin),&readfds))
  1779. read_from_terminal = 1;
  1780. #endif
  1781. if (FD_ISSET(s,&readfds))
  1782. read_from_sslcon = 1;
  1783. }
  1784. if (read_from_terminal)
  1785. {
  1786. if (s_crlf)
  1787. {
  1788. int j, lf_num;
  1789. i=raw_read_stdin(buf, bufsize/2);
  1790. lf_num = 0;
  1791. /* both loops are skipped when i <= 0 */
  1792. for (j = 0; j < i; j++)
  1793. if (buf[j] == '\n')
  1794. lf_num++;
  1795. for (j = i-1; j >= 0; j--)
  1796. {
  1797. buf[j+lf_num] = buf[j];
  1798. if (buf[j] == '\n')
  1799. {
  1800. lf_num--;
  1801. i++;
  1802. buf[j+lf_num] = '\r';
  1803. }
  1804. }
  1805. assert(lf_num == 0);
  1806. }
  1807. else
  1808. i=raw_read_stdin(buf,bufsize);
  1809. if (!s_quiet)
  1810. {
  1811. if ((i <= 0) || (buf[0] == 'Q'))
  1812. {
  1813. BIO_printf(bio_s_out,"DONE\n");
  1814. SHUTDOWN(s);
  1815. close_accept_socket();
  1816. ret= -11;
  1817. goto err;
  1818. }
  1819. if ((i <= 0) || (buf[0] == 'q'))
  1820. {
  1821. BIO_printf(bio_s_out,"DONE\n");
  1822. if (SSL_version(con) != DTLS1_VERSION)
  1823. SHUTDOWN(s);
  1824. /* close_accept_socket();
  1825. ret= -11;*/
  1826. goto err;
  1827. }
  1828. if ((buf[0] == 'r') &&
  1829. ((buf[1] == '\n') || (buf[1] == '\r')))
  1830. {
  1831. SSL_renegotiate(con);
  1832. i=SSL_do_handshake(con);
  1833. printf("SSL_do_handshake -> %d\n",i);
  1834. i=0; /*13; */
  1835. continue;
  1836. /* strcpy(buf,"server side RE-NEGOTIATE\n"); */
  1837. }
  1838. if ((buf[0] == 'R') &&
  1839. ((buf[1] == '\n') || (buf[1] == '\r')))
  1840. {
  1841. SSL_set_verify(con,
  1842. SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,NULL);
  1843. SSL_renegotiate(con);
  1844. i=SSL_do_handshake(con);
  1845. printf("SSL_do_handshake -> %d\n",i);
  1846. i=0; /* 13; */
  1847. continue;
  1848. /* strcpy(buf,"server side RE-NEGOTIATE asking for client cert\n"); */
  1849. }
  1850. if (buf[0] == 'P')
  1851. {
  1852. static const char *str="Lets print some clear text\n";
  1853. BIO_write(SSL_get_wbio(con),str,strlen(str));
  1854. }
  1855. if (buf[0] == 'S')
  1856. {
  1857. print_stats(bio_s_out,SSL_get_SSL_CTX(con));
  1858. }
  1859. }
  1860. #ifdef CHARSET_EBCDIC
  1861. ebcdic2ascii(buf,buf,i);
  1862. #endif
  1863. l=k=0;
  1864. for (;;)
  1865. {
  1866. /* should do a select for the write */
  1867. #ifdef RENEG
  1868. { static count=0; if (++count == 100) { count=0; SSL_renegotiate(con); } }
  1869. #endif
  1870. k=SSL_write(con,&(buf[l]),(unsigned int)i);
  1871. switch (SSL_get_error(con,k))
  1872. {
  1873. case SSL_ERROR_NONE:
  1874. break;
  1875. case SSL_ERROR_WANT_WRITE:
  1876. case SSL_ERROR_WANT_READ:
  1877. case SSL_ERROR_WANT_X509_LOOKUP:
  1878. BIO_printf(bio_s_out,"Write BLOCK\n");
  1879. break;
  1880. case SSL_ERROR_SYSCALL:
  1881. case SSL_ERROR_SSL:
  1882. BIO_printf(bio_s_out,"ERROR\n");
  1883. ERR_print_errors(bio_err);
  1884. ret=1;
  1885. goto err;
  1886. /* break; */
  1887. case SSL_ERROR_ZERO_RETURN:
  1888. BIO_printf(bio_s_out,"DONE\n");
  1889. ret=1;
  1890. goto err;
  1891. }
  1892. l+=k;
  1893. i-=k;
  1894. if (i <= 0) break;
  1895. }
  1896. }
  1897. if (read_from_sslcon)
  1898. {
  1899. if (!SSL_is_init_finished(con))
  1900. {
  1901. i=init_ssl_connection(con);
  1902. if (i < 0)
  1903. {
  1904. ret=0;
  1905. goto err;
  1906. }
  1907. else if (i == 0)
  1908. {
  1909. ret=1;
  1910. goto err;
  1911. }
  1912. }
  1913. else
  1914. {
  1915. again:
  1916. i=SSL_read(con,(char *)buf,bufsize);
  1917. switch (SSL_get_error(con,i))
  1918. {
  1919. case SSL_ERROR_NONE:
  1920. #ifdef CHARSET_EBCDIC
  1921. ascii2ebcdic(buf,buf,i);
  1922. #endif
  1923. raw_write_stdout(buf,
  1924. (unsigned int)i);
  1925. if (SSL_pending(con)) goto again;
  1926. break;
  1927. case SSL_ERROR_WANT_WRITE:
  1928. case SSL_ERROR_WANT_READ:
  1929. case SSL_ERROR_WANT_X509_LOOKUP:
  1930. BIO_printf(bio_s_out,"Read BLOCK\n");
  1931. break;
  1932. case SSL_ERROR_SYSCALL:
  1933. case SSL_ERROR_SSL:
  1934. BIO_printf(bio_s_out,"ERROR\n");
  1935. ERR_print_errors(bio_err);
  1936. ret=1;
  1937. goto err;
  1938. case SSL_ERROR_ZERO_RETURN:
  1939. BIO_printf(bio_s_out,"DONE\n");
  1940. ret=1;
  1941. goto err;
  1942. }
  1943. }
  1944. }
  1945. }
  1946. err:
  1947. if (con != NULL)
  1948. {
  1949. BIO_printf(bio_s_out,"shutting down SSL\n");
  1950. #if 1
  1951. SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
  1952. #else
  1953. SSL_shutdown(con);
  1954. #endif
  1955. SSL_free(con);
  1956. }
  1957. BIO_printf(bio_s_out,"CONNECTION CLOSED\n");
  1958. if (buf != NULL)
  1959. {
  1960. OPENSSL_cleanse(buf,bufsize);
  1961. OPENSSL_free(buf);
  1962. }
  1963. if (ret >= 0)
  1964. BIO_printf(bio_s_out,"ACCEPT\n");
  1965. return(ret);
  1966. }
  1967. static void close_accept_socket(void)
  1968. {
  1969. BIO_printf(bio_err,"shutdown accept socket\n");
  1970. if (accept_socket >= 0)
  1971. {
  1972. SHUTDOWN2(accept_socket);
  1973. }
  1974. }
  1975. static int init_ssl_connection(SSL *con)
  1976. {
  1977. int i;
  1978. const char *str;
  1979. X509 *peer;
  1980. long verify_error;
  1981. MS_STATIC char buf[BUFSIZ];
  1982. if ((i=SSL_accept(con)) <= 0)
  1983. {
  1984. if (BIO_sock_should_retry(i))
  1985. {
  1986. BIO_printf(bio_s_out,"DELAY\n");
  1987. return(1);
  1988. }
  1989. BIO_printf(bio_err,"ERROR\n");
  1990. verify_error=SSL_get_verify_result(con);
  1991. if (verify_error != X509_V_OK)
  1992. {
  1993. BIO_printf(bio_err,"verify error:%s\n",
  1994. X509_verify_cert_error_string(verify_error));
  1995. }
  1996. else
  1997. ERR_print_errors(bio_err);
  1998. return(0);
  1999. }
  2000. PEM_write_bio_SSL_SESSION(bio_s_out,SSL_get_session(con));
  2001. peer=SSL_get_peer_certificate(con);
  2002. if (peer != NULL)
  2003. {
  2004. BIO_printf(bio_s_out,"Client certificate\n");
  2005. PEM_write_bio_X509(bio_s_out,peer);
  2006. X509_NAME_oneline(X509_get_subject_name(peer),buf,sizeof buf);
  2007. BIO_printf(bio_s_out,"subject=%s\n",buf);
  2008. X509_NAME_oneline(X509_get_issuer_name(peer),buf,sizeof buf);
  2009. BIO_printf(bio_s_out,"issuer=%s\n",buf);
  2010. X509_free(peer);
  2011. }
  2012. if (SSL_get_shared_ciphers(con,buf,sizeof buf) != NULL)
  2013. BIO_printf(bio_s_out,"Shared ciphers:%s\n",buf);
  2014. str=SSL_CIPHER_get_name(SSL_get_current_cipher(con));
  2015. BIO_printf(bio_s_out,"CIPHER is %s\n",(str != NULL)?str:"(NONE)");
  2016. if (con->hit) BIO_printf(bio_s_out,"Reused session-id\n");
  2017. if (SSL_ctrl(con,SSL_CTRL_GET_FLAGS,0,NULL) &
  2018. TLS1_FLAGS_TLS_PADDING_BUG)
  2019. BIO_printf(bio_s_out,"Peer has incorrect TLSv1 block padding\n");
  2020. #ifndef OPENSSL_NO_KRB5
  2021. if (con->kssl_ctx->client_princ != NULL)
  2022. {
  2023. BIO_printf(bio_s_out,"Kerberos peer principal is %s\n",
  2024. con->kssl_ctx->client_princ);
  2025. }
  2026. #endif /* OPENSSL_NO_KRB5 */
  2027. return(1);
  2028. }
  2029. #ifndef OPENSSL_NO_DH
  2030. static DH *load_dh_param(const char *dhfile)
  2031. {
  2032. DH *ret=NULL;
  2033. BIO *bio;
  2034. if ((bio=BIO_new_file(dhfile,"r")) == NULL)
  2035. goto err;
  2036. ret=PEM_read_bio_DHparams(bio,NULL,NULL,NULL);
  2037. err:
  2038. if (bio != NULL) BIO_free(bio);
  2039. return(ret);
  2040. }
  2041. #endif
  2042. #if 0
  2043. static int load_CA(SSL_CTX *ctx, char *file)
  2044. {
  2045. FILE *in;
  2046. X509 *x=NULL;
  2047. if ((in=fopen(file,"r")) == NULL)
  2048. return(0);
  2049. for (;;)
  2050. {
  2051. if (PEM_read_X509(in,&x,NULL) == NULL)
  2052. break;
  2053. SSL_CTX_add_client_CA(ctx,x);
  2054. }
  2055. if (x != NULL) X509_free(x);
  2056. fclose(in);
  2057. return(1);
  2058. }
  2059. #endif
  2060. static int www_body(char *hostname, int s, unsigned char *context)
  2061. {
  2062. char *buf=NULL;
  2063. int ret=1;
  2064. int i,j,k,blank,dot;
  2065. SSL *con;
  2066. const SSL_CIPHER *c;
  2067. BIO *io,*ssl_bio,*sbio;
  2068. long total_bytes;
  2069. buf=OPENSSL_malloc(bufsize);
  2070. if (buf == NULL) return(0);
  2071. io=BIO_new(BIO_f_buffer());
  2072. ssl_bio=BIO_new(BIO_f_ssl());
  2073. if ((io == NULL) || (ssl_bio == NULL)) goto err;
  2074. #ifdef FIONBIO
  2075. if (s_nbio)
  2076. {
  2077. unsigned long sl=1;
  2078. if (!s_quiet)
  2079. BIO_printf(bio_err,"turning on non blocking io\n");
  2080. if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)
  2081. ERR_print_errors(bio_err);
  2082. }
  2083. #endif
  2084. /* lets make the output buffer a reasonable size */
  2085. if (!BIO_set_write_buffer_size(io,bufsize)) goto err;
  2086. if ((con=SSL_new(ctx)) == NULL) goto err;
  2087. #ifndef OPENSSL_NO_TLSEXT
  2088. if (s_tlsextdebug)
  2089. {
  2090. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  2091. SSL_set_tlsext_debug_arg(con, bio_s_out);
  2092. }
  2093. #endif
  2094. #ifndef OPENSSL_NO_KRB5
  2095. if ((con->kssl_ctx = kssl_ctx_new()) != NULL)
  2096. {
  2097. kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE, KRB5SVC);
  2098. kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB, KRB5KEYTAB);
  2099. }
  2100. #endif /* OPENSSL_NO_KRB5 */
  2101. if(context) SSL_set_session_id_context(con, context,
  2102. strlen((char *)context));
  2103. sbio=BIO_new_socket(s,BIO_NOCLOSE);
  2104. if (s_nbio_test)
  2105. {
  2106. BIO *test;
  2107. test=BIO_new(BIO_f_nbio_test());
  2108. sbio=BIO_push(test,sbio);
  2109. }
  2110. SSL_set_bio(con,sbio,sbio);
  2111. SSL_set_accept_state(con);
  2112. /* SSL_set_fd(con,s); */
  2113. BIO_set_ssl(ssl_bio,con,BIO_CLOSE);
  2114. BIO_push(io,ssl_bio);
  2115. #ifdef CHARSET_EBCDIC
  2116. io = BIO_push(BIO_new(BIO_f_ebcdic_filter()),io);
  2117. #endif
  2118. if (s_debug)
  2119. {
  2120. con->debug=1;
  2121. BIO_set_callback(SSL_get_rbio(con),bio_dump_callback);
  2122. BIO_set_callback_arg(SSL_get_rbio(con),(char *)bio_s_out);
  2123. }
  2124. if (s_msg)
  2125. {
  2126. SSL_set_msg_callback(con, msg_cb);
  2127. SSL_set_msg_callback_arg(con, bio_s_out);
  2128. }
  2129. blank=0;
  2130. for (;;)
  2131. {
  2132. if (hack)
  2133. {
  2134. i=SSL_accept(con);
  2135. switch (SSL_get_error(con,i))
  2136. {
  2137. case SSL_ERROR_NONE:
  2138. break;
  2139. case SSL_ERROR_WANT_WRITE:
  2140. case SSL_ERROR_WANT_READ:
  2141. case SSL_ERROR_WANT_X509_LOOKUP:
  2142. continue;
  2143. case SSL_ERROR_SYSCALL:
  2144. case SSL_ERROR_SSL:
  2145. case SSL_ERROR_ZERO_RETURN:
  2146. ret=1;
  2147. goto err;
  2148. /* break; */
  2149. }
  2150. SSL_renegotiate(con);
  2151. SSL_write(con,NULL,0);
  2152. }
  2153. i=BIO_gets(io,buf,bufsize-1);
  2154. if (i < 0) /* error */
  2155. {
  2156. if (!BIO_should_retry(io))
  2157. {
  2158. if (!s_quiet)
  2159. ERR_print_errors(bio_err);
  2160. goto err;
  2161. }
  2162. else
  2163. {
  2164. BIO_printf(bio_s_out,"read R BLOCK\n");
  2165. #if defined(OPENSSL_SYS_NETWARE)
  2166. delay(1000);
  2167. #elif !defined(OPENSSL_SYS_MSDOS) && !defined(__DJGPP__)
  2168. sleep(1);
  2169. #endif
  2170. continue;
  2171. }
  2172. }
  2173. else if (i == 0) /* end of input */
  2174. {
  2175. ret=1;
  2176. goto end;
  2177. }
  2178. /* else we have data */
  2179. if ( ((www == 1) && (strncmp("GET ",buf,4) == 0)) ||
  2180. ((www == 2) && (strncmp("GET /stats ",buf,10) == 0)))
  2181. {
  2182. char *p;
  2183. X509 *peer;
  2184. STACK_OF(SSL_CIPHER) *sk;
  2185. static const char *space=" ";
  2186. BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  2187. BIO_puts(io,"<HTML><BODY BGCOLOR=\"#ffffff\">\n");
  2188. BIO_puts(io,"<pre>\n");
  2189. /* BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/
  2190. BIO_puts(io,"\n");
  2191. for (i=0; i<local_argc; i++)
  2192. {
  2193. BIO_puts(io,local_argv[i]);
  2194. BIO_write(io," ",1);
  2195. }
  2196. BIO_puts(io,"\n");
  2197. /* The following is evil and should not really
  2198. * be done */
  2199. BIO_printf(io,"Ciphers supported in s_server binary\n");
  2200. sk=SSL_get_ciphers(con);
  2201. j=sk_SSL_CIPHER_num(sk);
  2202. for (i=0; i<j; i++)
  2203. {
  2204. c=sk_SSL_CIPHER_value(sk,i);
  2205. BIO_printf(io,"%-11s:%-25s",
  2206. SSL_CIPHER_get_version(c),
  2207. SSL_CIPHER_get_name(c));
  2208. if ((((i+1)%2) == 0) && (i+1 != j))
  2209. BIO_puts(io,"\n");
  2210. }
  2211. BIO_puts(io,"\n");
  2212. p=SSL_get_shared_ciphers(con,buf,bufsize);
  2213. if (p != NULL)
  2214. {
  2215. BIO_printf(io,"---\nCiphers common between both SSL end points:\n");
  2216. j=i=0;
  2217. while (*p)
  2218. {
  2219. if (*p == ':')
  2220. {
  2221. BIO_write(io,space,26-j);
  2222. i++;
  2223. j=0;
  2224. BIO_write(io,((i%3)?" ":"\n"),1);
  2225. }
  2226. else
  2227. {
  2228. BIO_write(io,p,1);
  2229. j++;
  2230. }
  2231. p++;
  2232. }
  2233. BIO_puts(io,"\n");
  2234. }
  2235. BIO_printf(io,((con->hit)
  2236. ?"---\nReused, "
  2237. :"---\nNew, "));
  2238. c=SSL_get_current_cipher(con);
  2239. BIO_printf(io,"%s, Cipher is %s\n",
  2240. SSL_CIPHER_get_version(c),
  2241. SSL_CIPHER_get_name(c));
  2242. SSL_SESSION_print(io,SSL_get_session(con));
  2243. BIO_printf(io,"---\n");
  2244. print_stats(io,SSL_get_SSL_CTX(con));
  2245. BIO_printf(io,"---\n");
  2246. peer=SSL_get_peer_certificate(con);
  2247. if (peer != NULL)
  2248. {
  2249. BIO_printf(io,"Client certificate\n");
  2250. X509_print(io,peer);
  2251. PEM_write_bio_X509(io,peer);
  2252. }
  2253. else
  2254. BIO_puts(io,"no client certificate available\n");
  2255. BIO_puts(io,"</BODY></HTML>\r\n\r\n");
  2256. break;
  2257. }
  2258. else if ((www == 2 || www == 3)
  2259. && (strncmp("GET /",buf,5) == 0))
  2260. {
  2261. BIO *file;
  2262. char *p,*e;
  2263. static const char *text="HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
  2264. /* skip the '/' */
  2265. p= &(buf[5]);
  2266. dot = 1;
  2267. for (e=p; *e != '\0'; e++)
  2268. {
  2269. if (e[0] == ' ')
  2270. break;
  2271. switch (dot)
  2272. {
  2273. case 1:
  2274. dot = (e[0] == '.') ? 2 : 0;
  2275. break;
  2276. case 2:
  2277. dot = (e[0] == '.') ? 3 : 0;
  2278. break;
  2279. case 3:
  2280. dot = (e[0] == '/') ? -1 : 0;
  2281. break;
  2282. }
  2283. if (dot == 0)
  2284. dot = (e[0] == '/') ? 1 : 0;
  2285. }
  2286. dot = (dot == 3) || (dot == -1); /* filename contains ".." component */
  2287. if (*e == '\0')
  2288. {
  2289. BIO_puts(io,text);
  2290. BIO_printf(io,"'%s' is an invalid file name\r\n",p);
  2291. break;
  2292. }
  2293. *e='\0';
  2294. if (dot)
  2295. {
  2296. BIO_puts(io,text);
  2297. BIO_printf(io,"'%s' contains '..' reference\r\n",p);
  2298. break;
  2299. }
  2300. if (*p == '/')
  2301. {
  2302. BIO_puts(io,text);
  2303. BIO_printf(io,"'%s' is an invalid path\r\n",p);
  2304. break;
  2305. }
  2306. #if 0
  2307. /* append if a directory lookup */
  2308. if (e[-1] == '/')
  2309. strcat(p,"index.html");
  2310. #endif
  2311. /* if a directory, do the index thang */
  2312. if (app_isdir(p)>0)
  2313. {
  2314. #if 0 /* must check buffer size */
  2315. strcat(p,"/index.html");
  2316. #else
  2317. BIO_puts(io,text);
  2318. BIO_printf(io,"'%s' is a directory\r\n",p);
  2319. break;
  2320. #endif
  2321. }
  2322. if ((file=BIO_new_file(p,"r")) == NULL)
  2323. {
  2324. BIO_puts(io,text);
  2325. BIO_printf(io,"Error opening '%s'\r\n",p);
  2326. ERR_print_errors(io);
  2327. break;
  2328. }
  2329. if (!s_quiet)
  2330. BIO_printf(bio_err,"FILE:%s\n",p);
  2331. if (www == 2)
  2332. {
  2333. i=strlen(p);
  2334. if ( ((i > 5) && (strcmp(&(p[i-5]),".html") == 0)) ||
  2335. ((i > 4) && (strcmp(&(p[i-4]),".php") == 0)) ||
  2336. ((i > 4) && (strcmp(&(p[i-4]),".htm") == 0)))
  2337. BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  2338. else
  2339. BIO_puts(io,"HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
  2340. }
  2341. /* send the file */
  2342. total_bytes=0;
  2343. for (;;)
  2344. {
  2345. i=BIO_read(file,buf,bufsize);
  2346. if (i <= 0) break;
  2347. #ifdef RENEG
  2348. total_bytes+=i;
  2349. fprintf(stderr,"%d\n",i);
  2350. if (total_bytes > 3*1024)
  2351. {
  2352. total_bytes=0;
  2353. fprintf(stderr,"RENEGOTIATE\n");
  2354. SSL_renegotiate(con);
  2355. }
  2356. #endif
  2357. for (j=0; j<i; )
  2358. {
  2359. #ifdef RENEG
  2360. { static count=0; if (++count == 13) { SSL_renegotiate(con); } }
  2361. #endif
  2362. k=BIO_write(io,&(buf[j]),i-j);
  2363. if (k <= 0)
  2364. {
  2365. if (!BIO_should_retry(io))
  2366. goto write_error;
  2367. else
  2368. {
  2369. BIO_printf(bio_s_out,"rwrite W BLOCK\n");
  2370. }
  2371. }
  2372. else
  2373. {
  2374. j+=k;
  2375. }
  2376. }
  2377. }
  2378. write_error:
  2379. BIO_free(file);
  2380. break;
  2381. }
  2382. }
  2383. for (;;)
  2384. {
  2385. i=(int)BIO_flush(io);
  2386. if (i <= 0)
  2387. {
  2388. if (!BIO_should_retry(io))
  2389. break;
  2390. }
  2391. else
  2392. break;
  2393. }
  2394. end:
  2395. #if 1
  2396. /* make sure we re-use sessions */
  2397. SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
  2398. #else
  2399. /* This kills performance */
  2400. /* SSL_shutdown(con); A shutdown gets sent in the
  2401. * BIO_free_all(io) procession */
  2402. #endif
  2403. err:
  2404. if (ret >= 0)
  2405. BIO_printf(bio_s_out,"ACCEPT\n");
  2406. if (buf != NULL) OPENSSL_free(buf);
  2407. if (io != NULL) BIO_free_all(io);
  2408. /* if (ssl_bio != NULL) BIO_free(ssl_bio);*/
  2409. return(ret);
  2410. }
  2411. #ifndef OPENSSL_NO_RSA
  2412. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
  2413. {
  2414. BIGNUM *bn = NULL;
  2415. static RSA *rsa_tmp=NULL;
  2416. if (!rsa_tmp && ((bn = BN_new()) == NULL))
  2417. BIO_printf(bio_err,"Allocation error in generating RSA key\n");
  2418. if (!rsa_tmp && bn)
  2419. {
  2420. if (!s_quiet)
  2421. {
  2422. BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
  2423. (void)BIO_flush(bio_err);
  2424. }
  2425. if(!BN_set_word(bn, RSA_F4) || ((rsa_tmp = RSA_new()) == NULL) ||
  2426. !RSA_generate_key_ex(rsa_tmp, keylength, bn, NULL))
  2427. {
  2428. if(rsa_tmp) RSA_free(rsa_tmp);
  2429. rsa_tmp = NULL;
  2430. }
  2431. if (!s_quiet)
  2432. {
  2433. BIO_printf(bio_err,"\n");
  2434. (void)BIO_flush(bio_err);
  2435. }
  2436. BN_free(bn);
  2437. }
  2438. return(rsa_tmp);
  2439. }
  2440. #endif
  2441. #define MAX_SESSION_ID_ATTEMPTS 10
  2442. static int generate_session_id(const SSL *ssl, unsigned char *id,
  2443. unsigned int *id_len)
  2444. {
  2445. unsigned int count = 0;
  2446. do {
  2447. RAND_pseudo_bytes(id, *id_len);
  2448. /* Prefix the session_id with the required prefix. NB: If our
  2449. * prefix is too long, clip it - but there will be worse effects
  2450. * anyway, eg. the server could only possibly create 1 session
  2451. * ID (ie. the prefix!) so all future session negotiations will
  2452. * fail due to conflicts. */
  2453. memcpy(id, session_id_prefix,
  2454. (strlen(session_id_prefix) < *id_len) ?
  2455. strlen(session_id_prefix) : *id_len);
  2456. }
  2457. while(SSL_has_matching_session_id(ssl, id, *id_len) &&
  2458. (++count < MAX_SESSION_ID_ATTEMPTS));
  2459. if(count >= MAX_SESSION_ID_ATTEMPTS)
  2460. return 0;
  2461. return 1;
  2462. }