s_server.c 72 KB

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