s_server.c 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505
  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. /*
  143. * Until the key-gen callbacks are modified to use newer prototypes, we allow
  144. * deprecated functions for openssl-internal code
  145. */
  146. #ifdef OPENSSL_NO_DEPRECATED
  147. # undef OPENSSL_NO_DEPRECATED
  148. #endif
  149. #include <assert.h>
  150. #include <ctype.h>
  151. #include <stdio.h>
  152. #include <stdlib.h>
  153. #include <string.h>
  154. #include <openssl/e_os2.h>
  155. #ifdef OPENSSL_NO_STDIO
  156. # define APPS_WIN16
  157. #endif
  158. /* conflicts with winsock2 stuff on netware */
  159. #if !defined(OPENSSL_SYS_NETWARE)
  160. # include <sys/types.h>
  161. #endif
  162. /*
  163. * With IPv6, it looks like Digital has mixed up the proper order of
  164. * recursive header file inclusion, resulting in the compiler complaining
  165. * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
  166. * needed to have fileno() declared correctly... So let's define u_int
  167. */
  168. #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
  169. # define __U_INT
  170. typedef unsigned int u_int;
  171. #endif
  172. #include <openssl/lhash.h>
  173. #include <openssl/bn.h>
  174. #define USE_SOCKETS
  175. #include "apps.h"
  176. #include <openssl/err.h>
  177. #include <openssl/pem.h>
  178. #include <openssl/x509.h>
  179. #include <openssl/ssl.h>
  180. #include <openssl/rand.h>
  181. #include <openssl/ocsp.h>
  182. #ifndef OPENSSL_NO_DH
  183. # include <openssl/dh.h>
  184. #endif
  185. #ifndef OPENSSL_NO_RSA
  186. # include <openssl/rsa.h>
  187. #endif
  188. #ifndef OPENSSL_NO_SRP
  189. # include <openssl/srp.h>
  190. #endif
  191. #include "s_apps.h"
  192. #include "timeouts.h"
  193. #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
  194. /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
  195. # undef FIONBIO
  196. #endif
  197. #if defined(OPENSSL_SYS_BEOS_R5)
  198. # include <fcntl.h>
  199. #endif
  200. #ifndef OPENSSL_NO_RSA
  201. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength);
  202. #endif
  203. static int sv_body(char *hostname, int s, int stype, unsigned char *context);
  204. static int www_body(char *hostname, int s, int stype, unsigned char *context);
  205. static int rev_body(char *hostname, int s, int stype, unsigned char *context);
  206. static void close_accept_socket(void);
  207. static void sv_usage(void);
  208. static int init_ssl_connection(SSL *s);
  209. static void print_stats(BIO *bp, SSL_CTX *ctx);
  210. static int generate_session_id(const SSL *ssl, unsigned char *id,
  211. unsigned int *id_len);
  212. static void init_session_cache_ctx(SSL_CTX *sctx);
  213. static void free_sessions(void);
  214. #ifndef OPENSSL_NO_DH
  215. static DH *load_dh_param(const char *dhfile);
  216. static DH *get_dh2048(void);
  217. #endif
  218. #ifdef MONOLITH
  219. static void s_server_init(void);
  220. #endif
  221. #ifndef OPENSSL_NO_DH
  222. static unsigned char dh2048_p[] = {
  223. 0xF6,0x42,0x57,0xB7,0x08,0x7F,0x08,0x17,0x72,0xA2,0xBA,0xD6,
  224. 0xA9,0x42,0xF3,0x05,0xE8,0xF9,0x53,0x11,0x39,0x4F,0xB6,0xF1,
  225. 0x6E,0xB9,0x4B,0x38,0x20,0xDA,0x01,0xA7,0x56,0xA3,0x14,0xE9,
  226. 0x8F,0x40,0x55,0xF3,0xD0,0x07,0xC6,0xCB,0x43,0xA9,0x94,0xAD,
  227. 0xF7,0x4C,0x64,0x86,0x49,0xF8,0x0C,0x83,0xBD,0x65,0xE9,0x17,
  228. 0xD4,0xA1,0xD3,0x50,0xF8,0xF5,0x59,0x5F,0xDC,0x76,0x52,0x4F,
  229. 0x3D,0x3D,0x8D,0xDB,0xCE,0x99,0xE1,0x57,0x92,0x59,0xCD,0xFD,
  230. 0xB8,0xAE,0x74,0x4F,0xC5,0xFC,0x76,0xBC,0x83,0xC5,0x47,0x30,
  231. 0x61,0xCE,0x7C,0xC9,0x66,0xFF,0x15,0xF9,0xBB,0xFD,0x91,0x5E,
  232. 0xC7,0x01,0xAA,0xD3,0x5B,0x9E,0x8D,0xA0,0xA5,0x72,0x3A,0xD4,
  233. 0x1A,0xF0,0xBF,0x46,0x00,0x58,0x2B,0xE5,0xF4,0x88,0xFD,0x58,
  234. 0x4E,0x49,0xDB,0xCD,0x20,0xB4,0x9D,0xE4,0x91,0x07,0x36,0x6B,
  235. 0x33,0x6C,0x38,0x0D,0x45,0x1D,0x0F,0x7C,0x88,0xB3,0x1C,0x7C,
  236. 0x5B,0x2D,0x8E,0xF6,0xF3,0xC9,0x23,0xC0,0x43,0xF0,0xA5,0x5B,
  237. 0x18,0x8D,0x8E,0xBB,0x55,0x8C,0xB8,0x5D,0x38,0xD3,0x34,0xFD,
  238. 0x7C,0x17,0x57,0x43,0xA3,0x1D,0x18,0x6C,0xDE,0x33,0x21,0x2C,
  239. 0xB5,0x2A,0xFF,0x3C,0xE1,0xB1,0x29,0x40,0x18,0x11,0x8D,0x7C,
  240. 0x84,0xA7,0x0A,0x72,0xD6,0x86,0xC4,0x03,0x19,0xC8,0x07,0x29,
  241. 0x7A,0xCA,0x95,0x0C,0xD9,0x96,0x9F,0xAB,0xD0,0x0A,0x50,0x9B,
  242. 0x02,0x46,0xD3,0x08,0x3D,0x66,0xA4,0x5D,0x41,0x9F,0x9C,0x7C,
  243. 0xBD,0x89,0x4B,0x22,0x19,0x26,0xBA,0xAB,0xA2,0x5E,0xC3,0x55,
  244. 0xE9,0x32,0x0B,0x3B,
  245. };
  246. static unsigned char dh2048_g[] = {
  247. 0x02,
  248. };
  249. DH *get_dh2048()
  250. {
  251. DH *dh;
  252. if ((dh = DH_new()) == NULL)
  253. return NULL;
  254. dh->p=BN_bin2bn(dh2048_p, sizeof(dh2048_p), NULL);
  255. dh->g=BN_bin2bn(dh2048_g, sizeof(dh2048_g), NULL);
  256. if (dh->p == NULL || dh->g == NULL) {
  257. DH_free(dh);
  258. return NULL;
  259. }
  260. return dh;
  261. }
  262. #endif
  263. /* static int load_CA(SSL_CTX *ctx, char *file);*/
  264. #undef BUFSIZZ
  265. #define BUFSIZZ 16*1024
  266. static int bufsize = BUFSIZZ;
  267. static int accept_socket = -1;
  268. #define TEST_CERT "server.pem"
  269. #ifndef OPENSSL_NO_TLSEXT
  270. # define TEST_CERT2 "server2.pem"
  271. #endif
  272. #undef PROG
  273. #define PROG s_server_main
  274. extern int verify_depth, verify_return_error, verify_quiet;
  275. static int s_server_verify = SSL_VERIFY_NONE;
  276. static int s_server_session_id_context = 1; /* anything will do */
  277. static const char *s_cert_file = TEST_CERT, *s_key_file =
  278. NULL, *s_chain_file = NULL;
  279. #ifndef OPENSSL_NO_TLSEXT
  280. static const char *s_cert_file2 = TEST_CERT2, *s_key_file2 = NULL;
  281. #endif
  282. static char *s_dcert_file = NULL, *s_dkey_file = NULL, *s_dchain_file = NULL;
  283. #ifdef FIONBIO
  284. static int s_nbio = 0;
  285. #endif
  286. static int s_nbio_test = 0;
  287. int s_crlf = 0;
  288. static SSL_CTX *ctx = NULL;
  289. #ifndef OPENSSL_NO_TLSEXT
  290. static SSL_CTX *ctx2 = NULL;
  291. #endif
  292. static int www = 0;
  293. static BIO *bio_s_out = NULL;
  294. static BIO *bio_s_msg = NULL;
  295. static int s_debug = 0;
  296. #ifndef OPENSSL_NO_TLSEXT
  297. static int s_tlsextdebug = 0;
  298. static int s_tlsextstatus = 0;
  299. static int cert_status_cb(SSL *s, void *arg);
  300. #endif
  301. static int no_resume_ephemeral = 0;
  302. static int s_msg = 0;
  303. static int s_quiet = 0;
  304. static int s_ign_eof = 0;
  305. static int s_brief = 0;
  306. static char *keymatexportlabel = NULL;
  307. static int keymatexportlen = 20;
  308. static int hack = 0;
  309. #ifndef OPENSSL_NO_ENGINE
  310. static char *engine_id = NULL;
  311. #endif
  312. static const char *session_id_prefix = NULL;
  313. static int enable_timeouts = 0;
  314. static long socket_mtu;
  315. #ifndef OPENSSL_NO_DTLS1
  316. static int cert_chain = 0;
  317. #endif
  318. #ifndef OPENSSL_NO_TLSEXT
  319. static BIO *serverinfo_in = NULL;
  320. static const char *s_serverinfo_file = NULL;
  321. #endif
  322. #ifndef OPENSSL_NO_PSK
  323. static char *psk_identity = "Client_identity";
  324. char *psk_key = NULL; /* by default PSK is not used */
  325. static unsigned int psk_server_cb(SSL *ssl, const char *identity,
  326. unsigned char *psk,
  327. unsigned int max_psk_len)
  328. {
  329. unsigned int psk_len = 0;
  330. int ret;
  331. BIGNUM *bn = NULL;
  332. if (s_debug)
  333. BIO_printf(bio_s_out, "psk_server_cb\n");
  334. if (!identity) {
  335. BIO_printf(bio_err, "Error: client did not send PSK identity\n");
  336. goto out_err;
  337. }
  338. if (s_debug)
  339. BIO_printf(bio_s_out, "identity_len=%d identity=%s\n",
  340. (int)strlen(identity), identity);
  341. /* here we could lookup the given identity e.g. from a database */
  342. if (strcmp(identity, psk_identity) != 0) {
  343. BIO_printf(bio_s_out, "PSK error: client identity not found"
  344. " (got '%s' expected '%s')\n", identity, psk_identity);
  345. goto out_err;
  346. }
  347. if (s_debug)
  348. BIO_printf(bio_s_out, "PSK client identity found\n");
  349. /* convert the PSK key to binary */
  350. ret = BN_hex2bn(&bn, psk_key);
  351. if (!ret) {
  352. BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n",
  353. psk_key);
  354. if (bn)
  355. BN_free(bn);
  356. return 0;
  357. }
  358. if (BN_num_bytes(bn) > (int)max_psk_len) {
  359. BIO_printf(bio_err,
  360. "psk buffer of callback is too small (%d) for key (%d)\n",
  361. max_psk_len, BN_num_bytes(bn));
  362. BN_free(bn);
  363. return 0;
  364. }
  365. ret = BN_bn2bin(bn, psk);
  366. BN_free(bn);
  367. if (ret < 0)
  368. goto out_err;
  369. psk_len = (unsigned int)ret;
  370. if (s_debug)
  371. BIO_printf(bio_s_out, "fetched PSK len=%d\n", psk_len);
  372. return psk_len;
  373. out_err:
  374. if (s_debug)
  375. BIO_printf(bio_err, "Error in PSK server callback\n");
  376. return 0;
  377. }
  378. #endif
  379. #ifndef OPENSSL_NO_SRP
  380. /* This is a context that we pass to callbacks */
  381. typedef struct srpsrvparm_st {
  382. char *login;
  383. SRP_VBASE *vb;
  384. SRP_user_pwd *user;
  385. } srpsrvparm;
  386. /*
  387. * This callback pretends to require some asynchronous logic in order to
  388. * obtain a verifier. When the callback is called for a new connection we
  389. * return with a negative value. This will provoke the accept etc to return
  390. * with an LOOKUP_X509. The main logic of the reinvokes the suspended call
  391. * (which would normally occur after a worker has finished) and we set the
  392. * user parameters.
  393. */
  394. static int MS_CALLBACK ssl_srp_server_param_cb(SSL *s, int *ad, void *arg)
  395. {
  396. srpsrvparm *p = (srpsrvparm *) arg;
  397. int ret = SSL3_AL_FATAL;
  398. if (p->login == NULL && p->user == NULL) {
  399. p->login = SSL_get_srp_username(s);
  400. BIO_printf(bio_err, "SRP username = \"%s\"\n", p->login);
  401. return (-1);
  402. }
  403. if (p->user == NULL) {
  404. BIO_printf(bio_err, "User %s doesn't exist\n", p->login);
  405. goto err;
  406. }
  407. if (SSL_set_srp_server_param
  408. (s, p->user->N, p->user->g, p->user->s, p->user->v,
  409. p->user->info) < 0) {
  410. *ad = SSL_AD_INTERNAL_ERROR;
  411. goto err;
  412. }
  413. BIO_printf(bio_err,
  414. "SRP parameters set: username = \"%s\" info=\"%s\" \n",
  415. p->login, p->user->info);
  416. ret = SSL_ERROR_NONE;
  417. err:
  418. SRP_user_pwd_free(p->user);
  419. p->user = NULL;
  420. p->login = NULL;
  421. return ret;
  422. }
  423. #endif
  424. #ifdef MONOLITH
  425. static void s_server_init(void)
  426. {
  427. accept_socket = -1;
  428. s_server_verify = SSL_VERIFY_NONE;
  429. s_dcert_file = NULL;
  430. s_dkey_file = NULL;
  431. s_dchain_file = NULL;
  432. s_cert_file = TEST_CERT;
  433. s_key_file = NULL;
  434. s_chain_file = NULL;
  435. # ifndef OPENSSL_NO_TLSEXT
  436. s_cert_file2 = TEST_CERT2;
  437. s_key_file2 = NULL;
  438. ctx2 = NULL;
  439. # endif
  440. # ifdef FIONBIO
  441. s_nbio = 0;
  442. # endif
  443. s_nbio_test = 0;
  444. ctx = NULL;
  445. www = 0;
  446. bio_s_out = NULL;
  447. s_debug = 0;
  448. s_msg = 0;
  449. s_quiet = 0;
  450. s_brief = 0;
  451. hack = 0;
  452. # ifndef OPENSSL_NO_ENGINE
  453. engine_id = NULL;
  454. # endif
  455. }
  456. #endif
  457. static void sv_usage(void)
  458. {
  459. BIO_printf(bio_err, "usage: s_server [args ...]\n");
  460. BIO_printf(bio_err, "\n");
  461. BIO_printf(bio_err,
  462. " -accept arg - port to accept on (default is %d)\n", PORT);
  463. BIO_printf(bio_err,
  464. " -verify_hostname host - check peer certificate matches \"host\"\n");
  465. BIO_printf(bio_err,
  466. " -verify_email email - check peer certificate matches \"email\"\n");
  467. BIO_printf(bio_err,
  468. " -verify_ip ipaddr - check peer certificate matches \"ipaddr\"\n");
  469. BIO_printf(bio_err, " -context arg - set session ID context\n");
  470. BIO_printf(bio_err,
  471. " -verify arg - turn on peer certificate verification\n");
  472. BIO_printf(bio_err,
  473. " -Verify arg - turn on peer certificate verification, must have a cert.\n");
  474. BIO_printf(bio_err,
  475. " -verify_return_error - return verification errors\n");
  476. BIO_printf(bio_err, " -cert arg - certificate file to use\n");
  477. BIO_printf(bio_err, " (default is %s)\n", TEST_CERT);
  478. #ifndef OPENSSL_NO_TLSEXT
  479. BIO_printf(bio_err,
  480. " -serverinfo arg - PEM serverinfo file for certificate\n");
  481. BIO_printf(bio_err,
  482. " -auth - send and receive RFC 5878 TLS auth extensions and supplemental data\n");
  483. BIO_printf(bio_err,
  484. " -auth_require_reneg - Do not send TLS auth extensions until renegotiation\n");
  485. #endif
  486. BIO_printf(bio_err,
  487. " -no_resumption_on_reneg - set SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION flag\n");
  488. BIO_printf(bio_err,
  489. " -crl_check - check the peer certificate has not been revoked by its CA.\n"
  490. " The CRL(s) are appended to the certificate file\n");
  491. BIO_printf(bio_err,
  492. " -crl_check_all - check the peer certificate has not been revoked by its CA\n"
  493. " or any other CRL in the CA chain. CRL(s) are appened to the\n"
  494. " the certificate file.\n");
  495. BIO_printf(bio_err,
  496. " -certform arg - certificate format (PEM or DER) PEM default\n");
  497. BIO_printf(bio_err,
  498. " -key arg - Private Key file to use, in cert file if\n");
  499. BIO_printf(bio_err, " not specified (default is %s)\n",
  500. TEST_CERT);
  501. BIO_printf(bio_err,
  502. " -keyform arg - key format (PEM, DER or ENGINE) PEM default\n");
  503. BIO_printf(bio_err,
  504. " -pass arg - private key file pass phrase source\n");
  505. BIO_printf(bio_err,
  506. " -dcert arg - second certificate file to use (usually for DSA)\n");
  507. BIO_printf(bio_err,
  508. " -dcertform x - second certificate format (PEM or DER) PEM default\n");
  509. BIO_printf(bio_err,
  510. " -dkey arg - second private key file to use (usually for DSA)\n");
  511. BIO_printf(bio_err,
  512. " -dkeyform arg - second key format (PEM, DER or ENGINE) PEM default\n");
  513. BIO_printf(bio_err,
  514. " -dpass arg - second private key file pass phrase source\n");
  515. BIO_printf(bio_err,
  516. " -dhparam arg - DH parameter file to use, in cert file if not specified\n");
  517. BIO_printf(bio_err,
  518. " or a default set of parameters is used\n");
  519. #ifndef OPENSSL_NO_ECDH
  520. BIO_printf(bio_err,
  521. " -named_curve arg - Elliptic curve name to use for ephemeral ECDH keys.\n"
  522. " Use \"openssl ecparam -list_curves\" for all names\n"
  523. " (default is nistp256).\n");
  524. #endif
  525. #ifdef FIONBIO
  526. BIO_printf(bio_err, " -nbio - Run with non-blocking IO\n");
  527. #endif
  528. BIO_printf(bio_err,
  529. " -nbio_test - test with the non-blocking test bio\n");
  530. BIO_printf(bio_err,
  531. " -crlf - convert LF from terminal into CRLF\n");
  532. BIO_printf(bio_err, " -debug - Print more output\n");
  533. BIO_printf(bio_err, " -msg - Show protocol messages\n");
  534. BIO_printf(bio_err, " -state - Print the SSL states\n");
  535. BIO_printf(bio_err, " -CApath arg - PEM format directory of CA's\n");
  536. BIO_printf(bio_err, " -CAfile arg - PEM format file of CA's\n");
  537. BIO_printf(bio_err,
  538. " -no_alt_chains - only ever use the first certificate chain found\n");
  539. BIO_printf(bio_err,
  540. " -nocert - Don't use any certificates (Anon-DH)\n");
  541. BIO_printf(bio_err,
  542. " -cipher arg - play with 'openssl ciphers' to see what goes here\n");
  543. BIO_printf(bio_err, " -serverpref - Use server's cipher preferences\n");
  544. BIO_printf(bio_err, " -quiet - No server output\n");
  545. BIO_printf(bio_err, " -no_tmp_rsa - Do not generate a tmp RSA key\n");
  546. #ifndef OPENSSL_NO_PSK
  547. BIO_printf(bio_err, " -psk_hint arg - PSK identity hint to use\n");
  548. BIO_printf(bio_err, " -psk arg - PSK in hex (without 0x)\n");
  549. # ifndef OPENSSL_NO_JPAKE
  550. BIO_printf(bio_err, " -jpake arg - JPAKE secret to use\n");
  551. # endif
  552. #endif
  553. #ifndef OPENSSL_NO_SRP
  554. BIO_printf(bio_err, " -srpvfile file - The verifier file for SRP\n");
  555. BIO_printf(bio_err,
  556. " -srpuserseed string - A seed string for a default user salt.\n");
  557. #endif
  558. BIO_printf(bio_err, " -ssl2 - Just talk SSLv2\n");
  559. #ifndef OPENSSL_NO_SSL3_METHOD
  560. BIO_printf(bio_err, " -ssl3 - Just talk SSLv3\n");
  561. #endif
  562. BIO_printf(bio_err, " -tls1_2 - Just talk TLSv1.2\n");
  563. BIO_printf(bio_err, " -tls1_1 - Just talk TLSv1.1\n");
  564. BIO_printf(bio_err, " -tls1 - Just talk TLSv1\n");
  565. BIO_printf(bio_err, " -dtls1 - Just talk DTLSv1\n");
  566. BIO_printf(bio_err, " -dtls1_2 - Just talk DTLSv1.2\n");
  567. BIO_printf(bio_err, " -timeout - Enable timeouts\n");
  568. BIO_printf(bio_err, " -mtu - Set link layer MTU\n");
  569. BIO_printf(bio_err, " -chain - Read a certificate chain\n");
  570. BIO_printf(bio_err, " -no_ssl2 - Just disable SSLv2\n");
  571. BIO_printf(bio_err, " -no_ssl3 - Just disable SSLv3\n");
  572. BIO_printf(bio_err, " -no_tls1 - Just disable TLSv1\n");
  573. BIO_printf(bio_err, " -no_tls1_1 - Just disable TLSv1.1\n");
  574. BIO_printf(bio_err, " -no_tls1_2 - Just disable TLSv1.2\n");
  575. #ifndef OPENSSL_NO_DH
  576. BIO_printf(bio_err, " -no_dhe - Disable ephemeral DH\n");
  577. #endif
  578. #ifndef OPENSSL_NO_ECDH
  579. BIO_printf(bio_err, " -no_ecdhe - Disable ephemeral ECDH\n");
  580. #endif
  581. BIO_printf(bio_err, " -bugs - Turn on SSL bug compatibility\n");
  582. BIO_printf(bio_err,
  583. " -hack - workaround for early Netscape code\n");
  584. BIO_printf(bio_err,
  585. " -www - Respond to a 'GET /' with a status page\n");
  586. BIO_printf(bio_err,
  587. " -WWW - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
  588. BIO_printf(bio_err,
  589. " -HTTP - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
  590. BIO_printf(bio_err,
  591. " with the assumption it contains a complete HTTP response.\n");
  592. #ifndef OPENSSL_NO_ENGINE
  593. BIO_printf(bio_err,
  594. " -engine id - Initialise and use the specified engine\n");
  595. #endif
  596. BIO_printf(bio_err,
  597. " -id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'\n");
  598. BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
  599. LIST_SEPARATOR_CHAR);
  600. #ifndef OPENSSL_NO_TLSEXT
  601. BIO_printf(bio_err,
  602. " -servername host - servername for HostName TLS extension\n");
  603. BIO_printf(bio_err,
  604. " -servername_fatal - on mismatch send fatal alert (default warning alert)\n");
  605. BIO_printf(bio_err,
  606. " -cert2 arg - certificate file to use for servername\n");
  607. BIO_printf(bio_err, " (default is %s)\n", TEST_CERT2);
  608. BIO_printf(bio_err,
  609. " -key2 arg - Private Key file to use for servername, in cert file if\n");
  610. BIO_printf(bio_err, " not specified (default is %s)\n",
  611. TEST_CERT2);
  612. BIO_printf(bio_err,
  613. " -tlsextdebug - hex dump of all TLS extensions received\n");
  614. BIO_printf(bio_err,
  615. " -no_ticket - disable use of RFC4507bis session tickets\n");
  616. BIO_printf(bio_err,
  617. " -legacy_renegotiation - enable use of legacy renegotiation (dangerous)\n");
  618. BIO_printf(bio_err,
  619. " -sigalgs arg - Signature algorithms to support (colon-separated list)\n");
  620. BIO_printf(bio_err,
  621. " -client_sigalgs arg - Signature algorithms to support for client \n");
  622. BIO_printf(bio_err,
  623. " certificate authentication (colon-separated list)\n");
  624. # ifndef OPENSSL_NO_NEXTPROTONEG
  625. BIO_printf(bio_err,
  626. " -nextprotoneg arg - set the advertised protocols for the NPN extension (comma-separated list)\n");
  627. # endif
  628. # ifndef OPENSSL_NO_SRTP
  629. BIO_printf(bio_err,
  630. " -use_srtp profiles - Offer SRTP key management with a colon-separated profile list\n");
  631. # endif
  632. BIO_printf(bio_err,
  633. " -alpn arg - set the advertised protocols for the ALPN extension (comma-separated list)\n");
  634. #endif
  635. BIO_printf(bio_err,
  636. " -keymatexport label - Export keying material using label\n");
  637. BIO_printf(bio_err,
  638. " -keymatexportlen len - Export len bytes of keying material (default 20)\n");
  639. BIO_printf(bio_err,
  640. " -status - respond to certificate status requests\n");
  641. BIO_printf(bio_err,
  642. " -status_verbose - enable status request verbose printout\n");
  643. BIO_printf(bio_err,
  644. " -status_timeout n - status request responder timeout\n");
  645. BIO_printf(bio_err, " -status_url URL - status request fallback URL\n");
  646. }
  647. static int local_argc = 0;
  648. static char **local_argv;
  649. #ifdef CHARSET_EBCDIC
  650. static int ebcdic_new(BIO *bi);
  651. static int ebcdic_free(BIO *a);
  652. static int ebcdic_read(BIO *b, char *out, int outl);
  653. static int ebcdic_write(BIO *b, const char *in, int inl);
  654. static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr);
  655. static int ebcdic_gets(BIO *bp, char *buf, int size);
  656. static int ebcdic_puts(BIO *bp, const char *str);
  657. # define BIO_TYPE_EBCDIC_FILTER (18|0x0200)
  658. static BIO_METHOD methods_ebcdic = {
  659. BIO_TYPE_EBCDIC_FILTER,
  660. "EBCDIC/ASCII filter",
  661. ebcdic_write,
  662. ebcdic_read,
  663. ebcdic_puts,
  664. ebcdic_gets,
  665. ebcdic_ctrl,
  666. ebcdic_new,
  667. ebcdic_free,
  668. };
  669. typedef struct {
  670. size_t alloced;
  671. char buff[1];
  672. } EBCDIC_OUTBUFF;
  673. BIO_METHOD *BIO_f_ebcdic_filter()
  674. {
  675. return (&methods_ebcdic);
  676. }
  677. static int ebcdic_new(BIO *bi)
  678. {
  679. EBCDIC_OUTBUFF *wbuf;
  680. wbuf = (EBCDIC_OUTBUFF *) OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + 1024);
  681. if (!wbuf)
  682. return 0;
  683. wbuf->alloced = 1024;
  684. wbuf->buff[0] = '\0';
  685. bi->ptr = (char *)wbuf;
  686. bi->init = 1;
  687. bi->flags = 0;
  688. return (1);
  689. }
  690. static int ebcdic_free(BIO *a)
  691. {
  692. if (a == NULL)
  693. return (0);
  694. if (a->ptr != NULL)
  695. OPENSSL_free(a->ptr);
  696. a->ptr = NULL;
  697. a->init = 0;
  698. a->flags = 0;
  699. return (1);
  700. }
  701. static int ebcdic_read(BIO *b, char *out, int outl)
  702. {
  703. int ret = 0;
  704. if (out == NULL || outl == 0)
  705. return (0);
  706. if (b->next_bio == NULL)
  707. return (0);
  708. ret = BIO_read(b->next_bio, out, outl);
  709. if (ret > 0)
  710. ascii2ebcdic(out, out, ret);
  711. return (ret);
  712. }
  713. static int ebcdic_write(BIO *b, const char *in, int inl)
  714. {
  715. EBCDIC_OUTBUFF *wbuf;
  716. int ret = 0;
  717. int num;
  718. unsigned char n;
  719. if ((in == NULL) || (inl <= 0))
  720. return (0);
  721. if (b->next_bio == NULL)
  722. return (0);
  723. wbuf = (EBCDIC_OUTBUFF *) b->ptr;
  724. if (inl > (num = wbuf->alloced)) {
  725. num = num + num; /* double the size */
  726. if (num < inl)
  727. num = inl;
  728. wbuf =
  729. (EBCDIC_OUTBUFF *) OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + num);
  730. if (!wbuf)
  731. return 0;
  732. OPENSSL_free(b->ptr);
  733. wbuf->alloced = num;
  734. wbuf->buff[0] = '\0';
  735. b->ptr = (char *)wbuf;
  736. }
  737. ebcdic2ascii(wbuf->buff, in, inl);
  738. ret = BIO_write(b->next_bio, wbuf->buff, inl);
  739. return (ret);
  740. }
  741. static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr)
  742. {
  743. long ret;
  744. if (b->next_bio == NULL)
  745. return (0);
  746. switch (cmd) {
  747. case BIO_CTRL_DUP:
  748. ret = 0L;
  749. break;
  750. default:
  751. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  752. break;
  753. }
  754. return (ret);
  755. }
  756. static int ebcdic_gets(BIO *bp, char *buf, int size)
  757. {
  758. int i, ret = 0;
  759. if (bp->next_bio == NULL)
  760. return (0);
  761. /* return(BIO_gets(bp->next_bio,buf,size));*/
  762. for (i = 0; i < size - 1; ++i) {
  763. ret = ebcdic_read(bp, &buf[i], 1);
  764. if (ret <= 0)
  765. break;
  766. else if (buf[i] == '\n') {
  767. ++i;
  768. break;
  769. }
  770. }
  771. if (i < size)
  772. buf[i] = '\0';
  773. return (ret < 0 && i == 0) ? ret : i;
  774. }
  775. static int ebcdic_puts(BIO *bp, const char *str)
  776. {
  777. if (bp->next_bio == NULL)
  778. return (0);
  779. return ebcdic_write(bp, str, strlen(str));
  780. }
  781. #endif
  782. #ifndef OPENSSL_NO_TLSEXT
  783. /* This is a context that we pass to callbacks */
  784. typedef struct tlsextctx_st {
  785. char *servername;
  786. BIO *biodebug;
  787. int extension_error;
  788. } tlsextctx;
  789. static int MS_CALLBACK ssl_servername_cb(SSL *s, int *ad, void *arg)
  790. {
  791. tlsextctx *p = (tlsextctx *) arg;
  792. const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
  793. if (servername && p->biodebug)
  794. BIO_printf(p->biodebug, "Hostname in TLS extension: \"%s\"\n",
  795. servername);
  796. if (!p->servername)
  797. return SSL_TLSEXT_ERR_NOACK;
  798. if (servername) {
  799. if (strcasecmp(servername, p->servername))
  800. return p->extension_error;
  801. if (ctx2) {
  802. BIO_printf(p->biodebug, "Switching server context.\n");
  803. SSL_set_SSL_CTX(s, ctx2);
  804. }
  805. }
  806. return SSL_TLSEXT_ERR_OK;
  807. }
  808. /* Structure passed to cert status callback */
  809. typedef struct tlsextstatusctx_st {
  810. /* Default responder to use */
  811. char *host, *path, *port;
  812. int use_ssl;
  813. int timeout;
  814. BIO *err;
  815. int verbose;
  816. } tlsextstatusctx;
  817. static tlsextstatusctx tlscstatp = { NULL, NULL, NULL, 0, -1, NULL, 0 };
  818. /*
  819. * Certificate Status callback. This is called when a client includes a
  820. * certificate status request extension. This is a simplified version. It
  821. * examines certificates each time and makes one OCSP responder query for
  822. * each request. A full version would store details such as the OCSP
  823. * certificate IDs and minimise the number of OCSP responses by caching them
  824. * until they were considered "expired".
  825. */
  826. static int cert_status_cb(SSL *s, void *arg)
  827. {
  828. tlsextstatusctx *srctx = arg;
  829. BIO *err = srctx->err;
  830. char *host, *port, *path;
  831. int use_ssl;
  832. unsigned char *rspder = NULL;
  833. int rspderlen;
  834. STACK_OF(OPENSSL_STRING) *aia = NULL;
  835. X509 *x = NULL;
  836. X509_STORE_CTX inctx;
  837. X509_OBJECT obj;
  838. OCSP_REQUEST *req = NULL;
  839. OCSP_RESPONSE *resp = NULL;
  840. OCSP_CERTID *id = NULL;
  841. STACK_OF(X509_EXTENSION) *exts;
  842. int ret = SSL_TLSEXT_ERR_NOACK;
  843. int i;
  844. # if 0
  845. STACK_OF(OCSP_RESPID) *ids;
  846. SSL_get_tlsext_status_ids(s, &ids);
  847. BIO_printf(err, "cert_status: received %d ids\n",
  848. sk_OCSP_RESPID_num(ids));
  849. # endif
  850. if (srctx->verbose)
  851. BIO_puts(err, "cert_status: callback called\n");
  852. /* Build up OCSP query from server certificate */
  853. x = SSL_get_certificate(s);
  854. aia = X509_get1_ocsp(x);
  855. if (aia) {
  856. if (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia, 0),
  857. &host, &port, &path, &use_ssl)) {
  858. BIO_puts(err, "cert_status: can't parse AIA URL\n");
  859. goto err;
  860. }
  861. if (srctx->verbose)
  862. BIO_printf(err, "cert_status: AIA URL: %s\n",
  863. sk_OPENSSL_STRING_value(aia, 0));
  864. } else {
  865. if (!srctx->host) {
  866. BIO_puts(srctx->err,
  867. "cert_status: no AIA and no default responder URL\n");
  868. goto done;
  869. }
  870. host = srctx->host;
  871. path = srctx->path;
  872. port = srctx->port;
  873. use_ssl = srctx->use_ssl;
  874. }
  875. if (!X509_STORE_CTX_init(&inctx,
  876. SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)),
  877. NULL, NULL))
  878. goto err;
  879. if (X509_STORE_get_by_subject(&inctx, X509_LU_X509,
  880. X509_get_issuer_name(x), &obj) <= 0) {
  881. BIO_puts(err, "cert_status: Can't retrieve issuer certificate.\n");
  882. X509_STORE_CTX_cleanup(&inctx);
  883. goto done;
  884. }
  885. req = OCSP_REQUEST_new();
  886. if (!req)
  887. goto err;
  888. id = OCSP_cert_to_id(NULL, x, obj.data.x509);
  889. X509_free(obj.data.x509);
  890. X509_STORE_CTX_cleanup(&inctx);
  891. if (!id)
  892. goto err;
  893. if (!OCSP_request_add0_id(req, id))
  894. goto err;
  895. id = NULL;
  896. /* Add any extensions to the request */
  897. SSL_get_tlsext_status_exts(s, &exts);
  898. for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
  899. X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
  900. if (!OCSP_REQUEST_add_ext(req, ext, -1))
  901. goto err;
  902. }
  903. resp = process_responder(err, req, host, path, port, use_ssl, NULL,
  904. srctx->timeout);
  905. if (!resp) {
  906. BIO_puts(err, "cert_status: error querying responder\n");
  907. goto done;
  908. }
  909. rspderlen = i2d_OCSP_RESPONSE(resp, &rspder);
  910. if (rspderlen <= 0)
  911. goto err;
  912. SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen);
  913. if (srctx->verbose) {
  914. BIO_puts(err, "cert_status: ocsp response sent:\n");
  915. OCSP_RESPONSE_print(err, resp, 2);
  916. }
  917. ret = SSL_TLSEXT_ERR_OK;
  918. done:
  919. if (ret != SSL_TLSEXT_ERR_OK)
  920. ERR_print_errors(err);
  921. if (aia) {
  922. OPENSSL_free(host);
  923. OPENSSL_free(path);
  924. OPENSSL_free(port);
  925. X509_email_free(aia);
  926. }
  927. if (id)
  928. OCSP_CERTID_free(id);
  929. if (req)
  930. OCSP_REQUEST_free(req);
  931. if (resp)
  932. OCSP_RESPONSE_free(resp);
  933. return ret;
  934. err:
  935. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  936. goto done;
  937. }
  938. # ifndef OPENSSL_NO_NEXTPROTONEG
  939. /* This is the context that we pass to next_proto_cb */
  940. typedef struct tlsextnextprotoctx_st {
  941. unsigned char *data;
  942. unsigned int len;
  943. } tlsextnextprotoctx;
  944. static int next_proto_cb(SSL *s, const unsigned char **data,
  945. unsigned int *len, void *arg)
  946. {
  947. tlsextnextprotoctx *next_proto = arg;
  948. *data = next_proto->data;
  949. *len = next_proto->len;
  950. return SSL_TLSEXT_ERR_OK;
  951. }
  952. # endif /* ndef OPENSSL_NO_NEXTPROTONEG */
  953. /* This the context that we pass to alpn_cb */
  954. typedef struct tlsextalpnctx_st {
  955. unsigned char *data;
  956. unsigned short len;
  957. } tlsextalpnctx;
  958. static int alpn_cb(SSL *s, const unsigned char **out, unsigned char *outlen,
  959. const unsigned char *in, unsigned int inlen, void *arg)
  960. {
  961. tlsextalpnctx *alpn_ctx = arg;
  962. if (!s_quiet) {
  963. /* We can assume that |in| is syntactically valid. */
  964. unsigned i;
  965. BIO_printf(bio_s_out, "ALPN protocols advertised by the client: ");
  966. for (i = 0; i < inlen;) {
  967. if (i)
  968. BIO_write(bio_s_out, ", ", 2);
  969. BIO_write(bio_s_out, &in[i + 1], in[i]);
  970. i += in[i] + 1;
  971. }
  972. BIO_write(bio_s_out, "\n", 1);
  973. }
  974. if (SSL_select_next_proto
  975. ((unsigned char **)out, outlen, alpn_ctx->data, alpn_ctx->len, in,
  976. inlen) != OPENSSL_NPN_NEGOTIATED) {
  977. return SSL_TLSEXT_ERR_NOACK;
  978. }
  979. if (!s_quiet) {
  980. BIO_printf(bio_s_out, "ALPN protocols selected: ");
  981. BIO_write(bio_s_out, *out, *outlen);
  982. BIO_write(bio_s_out, "\n", 1);
  983. }
  984. return SSL_TLSEXT_ERR_OK;
  985. }
  986. #endif /* ndef OPENSSL_NO_TLSEXT */
  987. int MAIN(int, char **);
  988. #ifndef OPENSSL_NO_JPAKE
  989. static char *jpake_secret = NULL;
  990. # define no_jpake !jpake_secret
  991. #else
  992. # define no_jpake 1
  993. #endif
  994. #ifndef OPENSSL_NO_SRP
  995. static srpsrvparm srp_callback_parm;
  996. #endif
  997. #ifndef OPENSSL_NO_SRTP
  998. static char *srtp_profiles = NULL;
  999. #endif
  1000. int MAIN(int argc, char *argv[])
  1001. {
  1002. X509_VERIFY_PARAM *vpm = NULL;
  1003. int badarg = 0;
  1004. short port = PORT;
  1005. char *CApath = NULL, *CAfile = NULL;
  1006. char *chCApath = NULL, *chCAfile = NULL;
  1007. char *vfyCApath = NULL, *vfyCAfile = NULL;
  1008. unsigned char *context = NULL;
  1009. char *dhfile = NULL;
  1010. int badop = 0;
  1011. int ret = 1;
  1012. int build_chain = 0;
  1013. int no_tmp_rsa = 0, no_dhe = 0, no_ecdhe = 0, nocert = 0;
  1014. int state = 0;
  1015. const SSL_METHOD *meth = NULL;
  1016. int socket_type = SOCK_STREAM;
  1017. ENGINE *e = NULL;
  1018. char *inrand = NULL;
  1019. int s_cert_format = FORMAT_PEM, s_key_format = FORMAT_PEM;
  1020. char *passarg = NULL, *pass = NULL;
  1021. char *dpassarg = NULL, *dpass = NULL;
  1022. int s_dcert_format = FORMAT_PEM, s_dkey_format = FORMAT_PEM;
  1023. X509 *s_cert = NULL, *s_dcert = NULL;
  1024. STACK_OF(X509) *s_chain = NULL, *s_dchain = NULL;
  1025. EVP_PKEY *s_key = NULL, *s_dkey = NULL;
  1026. int no_cache = 0, ext_cache = 0;
  1027. int rev = 0, naccept = -1;
  1028. #ifndef OPENSSL_NO_TLSEXT
  1029. EVP_PKEY *s_key2 = NULL;
  1030. X509 *s_cert2 = NULL;
  1031. tlsextctx tlsextcbp = { NULL, NULL, SSL_TLSEXT_ERR_ALERT_WARNING };
  1032. # ifndef OPENSSL_NO_NEXTPROTONEG
  1033. const char *next_proto_neg_in = NULL;
  1034. tlsextnextprotoctx next_proto = { NULL, 0 };
  1035. # endif
  1036. const char *alpn_in = NULL;
  1037. tlsextalpnctx alpn_ctx = { NULL, 0 };
  1038. #endif
  1039. #ifndef OPENSSL_NO_PSK
  1040. /* by default do not send a PSK identity hint */
  1041. static char *psk_identity_hint = NULL;
  1042. #endif
  1043. #ifndef OPENSSL_NO_SRP
  1044. char *srpuserseed = NULL;
  1045. char *srp_verifier_file = NULL;
  1046. #endif
  1047. SSL_EXCERT *exc = NULL;
  1048. SSL_CONF_CTX *cctx = NULL;
  1049. STACK_OF(OPENSSL_STRING) *ssl_args = NULL;
  1050. char *crl_file = NULL;
  1051. int crl_format = FORMAT_PEM;
  1052. int crl_download = 0;
  1053. STACK_OF(X509_CRL) *crls = NULL;
  1054. meth = SSLv23_server_method();
  1055. local_argc = argc;
  1056. local_argv = argv;
  1057. apps_startup();
  1058. #ifdef MONOLITH
  1059. s_server_init();
  1060. #endif
  1061. if (bio_err == NULL)
  1062. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  1063. if (!load_config(bio_err, NULL))
  1064. goto end;
  1065. cctx = SSL_CONF_CTX_new();
  1066. if (!cctx)
  1067. goto end;
  1068. SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER);
  1069. SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CMDLINE);
  1070. verify_depth = 0;
  1071. #ifdef FIONBIO
  1072. s_nbio = 0;
  1073. #endif
  1074. s_nbio_test = 0;
  1075. argc--;
  1076. argv++;
  1077. while (argc >= 1) {
  1078. if ((strcmp(*argv, "-port") == 0) || (strcmp(*argv, "-accept") == 0)) {
  1079. if (--argc < 1)
  1080. goto bad;
  1081. if (!extract_port(*(++argv), &port))
  1082. goto bad;
  1083. } else if (strcmp(*argv, "-naccept") == 0) {
  1084. if (--argc < 1)
  1085. goto bad;
  1086. naccept = atol(*(++argv));
  1087. if (naccept <= 0) {
  1088. BIO_printf(bio_err, "bad accept value %s\n", *argv);
  1089. goto bad;
  1090. }
  1091. } else if (strcmp(*argv, "-verify") == 0) {
  1092. s_server_verify = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE;
  1093. if (--argc < 1)
  1094. goto bad;
  1095. verify_depth = atoi(*(++argv));
  1096. if (!s_quiet)
  1097. BIO_printf(bio_err, "verify depth is %d\n", verify_depth);
  1098. } else if (strcmp(*argv, "-Verify") == 0) {
  1099. s_server_verify =
  1100. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
  1101. SSL_VERIFY_CLIENT_ONCE;
  1102. if (--argc < 1)
  1103. goto bad;
  1104. verify_depth = atoi(*(++argv));
  1105. if (!s_quiet)
  1106. BIO_printf(bio_err,
  1107. "verify depth is %d, must return a certificate\n",
  1108. verify_depth);
  1109. } else if (strcmp(*argv, "-context") == 0) {
  1110. if (--argc < 1)
  1111. goto bad;
  1112. context = (unsigned char *)*(++argv);
  1113. } else if (strcmp(*argv, "-cert") == 0) {
  1114. if (--argc < 1)
  1115. goto bad;
  1116. s_cert_file = *(++argv);
  1117. } else if (strcmp(*argv, "-CRL") == 0) {
  1118. if (--argc < 1)
  1119. goto bad;
  1120. crl_file = *(++argv);
  1121. } else if (strcmp(*argv, "-crl_download") == 0)
  1122. crl_download = 1;
  1123. #ifndef OPENSSL_NO_TLSEXT
  1124. else if (strcmp(*argv, "-serverinfo") == 0) {
  1125. if (--argc < 1)
  1126. goto bad;
  1127. s_serverinfo_file = *(++argv);
  1128. }
  1129. #endif
  1130. else if (strcmp(*argv, "-certform") == 0) {
  1131. if (--argc < 1)
  1132. goto bad;
  1133. s_cert_format = str2fmt(*(++argv));
  1134. } else if (strcmp(*argv, "-key") == 0) {
  1135. if (--argc < 1)
  1136. goto bad;
  1137. s_key_file = *(++argv);
  1138. } else if (strcmp(*argv, "-keyform") == 0) {
  1139. if (--argc < 1)
  1140. goto bad;
  1141. s_key_format = str2fmt(*(++argv));
  1142. } else if (strcmp(*argv, "-pass") == 0) {
  1143. if (--argc < 1)
  1144. goto bad;
  1145. passarg = *(++argv);
  1146. } else if (strcmp(*argv, "-cert_chain") == 0) {
  1147. if (--argc < 1)
  1148. goto bad;
  1149. s_chain_file = *(++argv);
  1150. } else if (strcmp(*argv, "-dhparam") == 0) {
  1151. if (--argc < 1)
  1152. goto bad;
  1153. dhfile = *(++argv);
  1154. } else if (strcmp(*argv, "-dcertform") == 0) {
  1155. if (--argc < 1)
  1156. goto bad;
  1157. s_dcert_format = str2fmt(*(++argv));
  1158. } else if (strcmp(*argv, "-dcert") == 0) {
  1159. if (--argc < 1)
  1160. goto bad;
  1161. s_dcert_file = *(++argv);
  1162. } else if (strcmp(*argv, "-dkeyform") == 0) {
  1163. if (--argc < 1)
  1164. goto bad;
  1165. s_dkey_format = str2fmt(*(++argv));
  1166. } else if (strcmp(*argv, "-dpass") == 0) {
  1167. if (--argc < 1)
  1168. goto bad;
  1169. dpassarg = *(++argv);
  1170. } else if (strcmp(*argv, "-dkey") == 0) {
  1171. if (--argc < 1)
  1172. goto bad;
  1173. s_dkey_file = *(++argv);
  1174. } else if (strcmp(*argv, "-dcert_chain") == 0) {
  1175. if (--argc < 1)
  1176. goto bad;
  1177. s_dchain_file = *(++argv);
  1178. } else if (strcmp(*argv, "-nocert") == 0) {
  1179. nocert = 1;
  1180. } else if (strcmp(*argv, "-CApath") == 0) {
  1181. if (--argc < 1)
  1182. goto bad;
  1183. CApath = *(++argv);
  1184. } else if (strcmp(*argv, "-chainCApath") == 0) {
  1185. if (--argc < 1)
  1186. goto bad;
  1187. chCApath = *(++argv);
  1188. } else if (strcmp(*argv, "-verifyCApath") == 0) {
  1189. if (--argc < 1)
  1190. goto bad;
  1191. vfyCApath = *(++argv);
  1192. } else if (strcmp(*argv, "-no_cache") == 0)
  1193. no_cache = 1;
  1194. else if (strcmp(*argv, "-ext_cache") == 0)
  1195. ext_cache = 1;
  1196. else if (strcmp(*argv, "-CRLform") == 0) {
  1197. if (--argc < 1)
  1198. goto bad;
  1199. crl_format = str2fmt(*(++argv));
  1200. } else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm)) {
  1201. if (badarg)
  1202. goto bad;
  1203. continue;
  1204. } else if (args_excert(&argv, &argc, &badarg, bio_err, &exc)) {
  1205. if (badarg)
  1206. goto bad;
  1207. continue;
  1208. } else if (args_ssl(&argv, &argc, cctx, &badarg, bio_err, &ssl_args)) {
  1209. if (badarg)
  1210. goto bad;
  1211. continue;
  1212. } else if (strcmp(*argv, "-verify_return_error") == 0)
  1213. verify_return_error = 1;
  1214. else if (strcmp(*argv, "-verify_quiet") == 0)
  1215. verify_quiet = 1;
  1216. else if (strcmp(*argv, "-build_chain") == 0)
  1217. build_chain = 1;
  1218. else if (strcmp(*argv, "-CAfile") == 0) {
  1219. if (--argc < 1)
  1220. goto bad;
  1221. CAfile = *(++argv);
  1222. } else if (strcmp(*argv, "-chainCAfile") == 0) {
  1223. if (--argc < 1)
  1224. goto bad;
  1225. chCAfile = *(++argv);
  1226. } else if (strcmp(*argv, "-verifyCAfile") == 0) {
  1227. if (--argc < 1)
  1228. goto bad;
  1229. vfyCAfile = *(++argv);
  1230. }
  1231. #ifdef FIONBIO
  1232. else if (strcmp(*argv, "-nbio") == 0) {
  1233. s_nbio = 1;
  1234. }
  1235. #endif
  1236. else if (strcmp(*argv, "-nbio_test") == 0) {
  1237. #ifdef FIONBIO
  1238. s_nbio = 1;
  1239. #endif
  1240. s_nbio_test = 1;
  1241. } else if (strcmp(*argv, "-ign_eof") == 0)
  1242. s_ign_eof = 1;
  1243. else if (strcmp(*argv, "-no_ign_eof") == 0)
  1244. s_ign_eof = 0;
  1245. else if (strcmp(*argv, "-debug") == 0) {
  1246. s_debug = 1;
  1247. }
  1248. #ifndef OPENSSL_NO_TLSEXT
  1249. else if (strcmp(*argv, "-tlsextdebug") == 0)
  1250. s_tlsextdebug = 1;
  1251. else if (strcmp(*argv, "-status") == 0)
  1252. s_tlsextstatus = 1;
  1253. else if (strcmp(*argv, "-status_verbose") == 0) {
  1254. s_tlsextstatus = 1;
  1255. tlscstatp.verbose = 1;
  1256. } else if (!strcmp(*argv, "-status_timeout")) {
  1257. s_tlsextstatus = 1;
  1258. if (--argc < 1)
  1259. goto bad;
  1260. tlscstatp.timeout = atoi(*(++argv));
  1261. } else if (!strcmp(*argv, "-status_url")) {
  1262. s_tlsextstatus = 1;
  1263. if (--argc < 1)
  1264. goto bad;
  1265. if (!OCSP_parse_url(*(++argv),
  1266. &tlscstatp.host,
  1267. &tlscstatp.port,
  1268. &tlscstatp.path, &tlscstatp.use_ssl)) {
  1269. BIO_printf(bio_err, "Error parsing URL\n");
  1270. goto bad;
  1271. }
  1272. }
  1273. #endif
  1274. else if (strcmp(*argv, "-msg") == 0) {
  1275. s_msg = 1;
  1276. } else if (strcmp(*argv, "-msgfile") == 0) {
  1277. if (--argc < 1)
  1278. goto bad;
  1279. bio_s_msg = BIO_new_file(*(++argv), "w");
  1280. }
  1281. #ifndef OPENSSL_NO_SSL_TRACE
  1282. else if (strcmp(*argv, "-trace") == 0) {
  1283. s_msg = 2;
  1284. }
  1285. #endif
  1286. else if (strcmp(*argv, "-hack") == 0) {
  1287. hack = 1;
  1288. } else if (strcmp(*argv, "-state") == 0) {
  1289. state = 1;
  1290. } else if (strcmp(*argv, "-crlf") == 0) {
  1291. s_crlf = 1;
  1292. } else if (strcmp(*argv, "-quiet") == 0) {
  1293. s_quiet = 1;
  1294. } else if (strcmp(*argv, "-brief") == 0) {
  1295. s_quiet = 1;
  1296. s_brief = 1;
  1297. verify_quiet = 1;
  1298. } else if (strcmp(*argv, "-no_tmp_rsa") == 0) {
  1299. no_tmp_rsa = 1;
  1300. } else if (strcmp(*argv, "-no_dhe") == 0) {
  1301. no_dhe = 1;
  1302. } else if (strcmp(*argv, "-no_ecdhe") == 0) {
  1303. no_ecdhe = 1;
  1304. } else if (strcmp(*argv, "-no_resume_ephemeral") == 0) {
  1305. no_resume_ephemeral = 1;
  1306. }
  1307. #ifndef OPENSSL_NO_PSK
  1308. else if (strcmp(*argv, "-psk_hint") == 0) {
  1309. if (--argc < 1)
  1310. goto bad;
  1311. psk_identity_hint = *(++argv);
  1312. } else if (strcmp(*argv, "-psk") == 0) {
  1313. size_t i;
  1314. if (--argc < 1)
  1315. goto bad;
  1316. psk_key = *(++argv);
  1317. for (i = 0; i < strlen(psk_key); i++) {
  1318. if (isxdigit((unsigned char)psk_key[i]))
  1319. continue;
  1320. BIO_printf(bio_err, "Not a hex number '%s'\n", *argv);
  1321. goto bad;
  1322. }
  1323. }
  1324. #endif
  1325. #ifndef OPENSSL_NO_SRP
  1326. else if (strcmp(*argv, "-srpvfile") == 0) {
  1327. if (--argc < 1)
  1328. goto bad;
  1329. srp_verifier_file = *(++argv);
  1330. meth = TLSv1_server_method();
  1331. } else if (strcmp(*argv, "-srpuserseed") == 0) {
  1332. if (--argc < 1)
  1333. goto bad;
  1334. srpuserseed = *(++argv);
  1335. meth = TLSv1_server_method();
  1336. }
  1337. #endif
  1338. else if (strcmp(*argv, "-rev") == 0) {
  1339. rev = 1;
  1340. } else if (strcmp(*argv, "-www") == 0) {
  1341. www = 1;
  1342. } else if (strcmp(*argv, "-WWW") == 0) {
  1343. www = 2;
  1344. } else if (strcmp(*argv, "-HTTP") == 0) {
  1345. www = 3;
  1346. }
  1347. #ifndef OPENSSL_NO_SSL2
  1348. else if (strcmp(*argv, "-ssl2") == 0) {
  1349. no_ecdhe = 1;
  1350. meth = SSLv2_server_method();
  1351. }
  1352. #endif
  1353. #ifndef OPENSSL_NO_SSL3_METHOD
  1354. else if (strcmp(*argv, "-ssl3") == 0) {
  1355. meth = SSLv3_server_method();
  1356. }
  1357. #endif
  1358. #ifndef OPENSSL_NO_TLS1
  1359. else if (strcmp(*argv, "-tls1") == 0) {
  1360. meth = TLSv1_server_method();
  1361. } else if (strcmp(*argv, "-tls1_1") == 0) {
  1362. meth = TLSv1_1_server_method();
  1363. } else if (strcmp(*argv, "-tls1_2") == 0) {
  1364. meth = TLSv1_2_server_method();
  1365. }
  1366. #endif
  1367. #ifndef OPENSSL_NO_DTLS1
  1368. else if (strcmp(*argv, "-dtls") == 0) {
  1369. meth = DTLS_server_method();
  1370. socket_type = SOCK_DGRAM;
  1371. } else if (strcmp(*argv, "-dtls1") == 0) {
  1372. meth = DTLSv1_server_method();
  1373. socket_type = SOCK_DGRAM;
  1374. } else if (strcmp(*argv, "-dtls1_2") == 0) {
  1375. meth = DTLSv1_2_server_method();
  1376. socket_type = SOCK_DGRAM;
  1377. } else if (strcmp(*argv, "-timeout") == 0)
  1378. enable_timeouts = 1;
  1379. else if (strcmp(*argv, "-mtu") == 0) {
  1380. if (--argc < 1)
  1381. goto bad;
  1382. socket_mtu = atol(*(++argv));
  1383. } else if (strcmp(*argv, "-chain") == 0)
  1384. cert_chain = 1;
  1385. #endif
  1386. else if (strcmp(*argv, "-id_prefix") == 0) {
  1387. if (--argc < 1)
  1388. goto bad;
  1389. session_id_prefix = *(++argv);
  1390. }
  1391. #ifndef OPENSSL_NO_ENGINE
  1392. else if (strcmp(*argv, "-engine") == 0) {
  1393. if (--argc < 1)
  1394. goto bad;
  1395. engine_id = *(++argv);
  1396. }
  1397. #endif
  1398. else if (strcmp(*argv, "-rand") == 0) {
  1399. if (--argc < 1)
  1400. goto bad;
  1401. inrand = *(++argv);
  1402. }
  1403. #ifndef OPENSSL_NO_TLSEXT
  1404. else if (strcmp(*argv, "-servername") == 0) {
  1405. if (--argc < 1)
  1406. goto bad;
  1407. tlsextcbp.servername = *(++argv);
  1408. } else if (strcmp(*argv, "-servername_fatal") == 0) {
  1409. tlsextcbp.extension_error = SSL_TLSEXT_ERR_ALERT_FATAL;
  1410. } else if (strcmp(*argv, "-cert2") == 0) {
  1411. if (--argc < 1)
  1412. goto bad;
  1413. s_cert_file2 = *(++argv);
  1414. } else if (strcmp(*argv, "-key2") == 0) {
  1415. if (--argc < 1)
  1416. goto bad;
  1417. s_key_file2 = *(++argv);
  1418. }
  1419. # ifndef OPENSSL_NO_NEXTPROTONEG
  1420. else if (strcmp(*argv, "-nextprotoneg") == 0) {
  1421. if (--argc < 1)
  1422. goto bad;
  1423. next_proto_neg_in = *(++argv);
  1424. }
  1425. # endif
  1426. else if (strcmp(*argv, "-alpn") == 0) {
  1427. if (--argc < 1)
  1428. goto bad;
  1429. alpn_in = *(++argv);
  1430. }
  1431. #endif
  1432. #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
  1433. else if (strcmp(*argv, "-jpake") == 0) {
  1434. if (--argc < 1)
  1435. goto bad;
  1436. jpake_secret = *(++argv);
  1437. }
  1438. #endif
  1439. #ifndef OPENSSL_NO_SRTP
  1440. else if (strcmp(*argv, "-use_srtp") == 0) {
  1441. if (--argc < 1)
  1442. goto bad;
  1443. srtp_profiles = *(++argv);
  1444. }
  1445. #endif
  1446. else if (strcmp(*argv, "-keymatexport") == 0) {
  1447. if (--argc < 1)
  1448. goto bad;
  1449. keymatexportlabel = *(++argv);
  1450. } else if (strcmp(*argv, "-keymatexportlen") == 0) {
  1451. if (--argc < 1)
  1452. goto bad;
  1453. keymatexportlen = atoi(*(++argv));
  1454. if (keymatexportlen == 0)
  1455. goto bad;
  1456. } else {
  1457. BIO_printf(bio_err, "unknown option %s\n", *argv);
  1458. badop = 1;
  1459. break;
  1460. }
  1461. argc--;
  1462. argv++;
  1463. }
  1464. if (badop) {
  1465. bad:
  1466. sv_usage();
  1467. goto end;
  1468. }
  1469. #ifndef OPENSSL_NO_DTLS1
  1470. if (www && socket_type == SOCK_DGRAM) {
  1471. BIO_printf(bio_err, "Can't use -HTTP, -www or -WWW with DTLS\n");
  1472. goto end;
  1473. }
  1474. #endif
  1475. #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
  1476. if (jpake_secret) {
  1477. if (psk_key) {
  1478. BIO_printf(bio_err, "Can't use JPAKE and PSK together\n");
  1479. goto end;
  1480. }
  1481. psk_identity = "JPAKE";
  1482. }
  1483. #endif
  1484. SSL_load_error_strings();
  1485. OpenSSL_add_ssl_algorithms();
  1486. #ifndef OPENSSL_NO_ENGINE
  1487. e = setup_engine(bio_err, engine_id, 1);
  1488. #endif
  1489. if (!app_passwd(bio_err, passarg, dpassarg, &pass, &dpass)) {
  1490. BIO_printf(bio_err, "Error getting password\n");
  1491. goto end;
  1492. }
  1493. if (s_key_file == NULL)
  1494. s_key_file = s_cert_file;
  1495. #ifndef OPENSSL_NO_TLSEXT
  1496. if (s_key_file2 == NULL)
  1497. s_key_file2 = s_cert_file2;
  1498. #endif
  1499. if (!load_excert(&exc, bio_err))
  1500. goto end;
  1501. if (nocert == 0) {
  1502. s_key = load_key(bio_err, s_key_file, s_key_format, 0, pass, e,
  1503. "server certificate private key file");
  1504. if (!s_key) {
  1505. ERR_print_errors(bio_err);
  1506. goto end;
  1507. }
  1508. s_cert = load_cert(bio_err, s_cert_file, s_cert_format,
  1509. NULL, e, "server certificate file");
  1510. if (!s_cert) {
  1511. ERR_print_errors(bio_err);
  1512. goto end;
  1513. }
  1514. if (s_chain_file) {
  1515. s_chain = load_certs(bio_err, s_chain_file, FORMAT_PEM,
  1516. NULL, e, "server certificate chain");
  1517. if (!s_chain)
  1518. goto end;
  1519. }
  1520. #ifndef OPENSSL_NO_TLSEXT
  1521. if (tlsextcbp.servername) {
  1522. s_key2 = load_key(bio_err, s_key_file2, s_key_format, 0, pass, e,
  1523. "second server certificate private key file");
  1524. if (!s_key2) {
  1525. ERR_print_errors(bio_err);
  1526. goto end;
  1527. }
  1528. s_cert2 = load_cert(bio_err, s_cert_file2, s_cert_format,
  1529. NULL, e, "second server certificate file");
  1530. if (!s_cert2) {
  1531. ERR_print_errors(bio_err);
  1532. goto end;
  1533. }
  1534. }
  1535. #endif /* OPENSSL_NO_TLSEXT */
  1536. }
  1537. #if !defined(OPENSSL_NO_TLSEXT)
  1538. # if !defined(OPENSSL_NO_NEXTPROTONEG)
  1539. if (next_proto_neg_in) {
  1540. unsigned short len;
  1541. next_proto.data = next_protos_parse(&len, next_proto_neg_in);
  1542. if (next_proto.data == NULL)
  1543. goto end;
  1544. next_proto.len = len;
  1545. } else {
  1546. next_proto.data = NULL;
  1547. }
  1548. # endif
  1549. alpn_ctx.data = NULL;
  1550. if (alpn_in) {
  1551. unsigned short len;
  1552. alpn_ctx.data = next_protos_parse(&len, alpn_in);
  1553. if (alpn_ctx.data == NULL)
  1554. goto end;
  1555. alpn_ctx.len = len;
  1556. }
  1557. #endif
  1558. if (crl_file) {
  1559. X509_CRL *crl;
  1560. crl = load_crl(crl_file, crl_format);
  1561. if (!crl) {
  1562. BIO_puts(bio_err, "Error loading CRL\n");
  1563. ERR_print_errors(bio_err);
  1564. goto end;
  1565. }
  1566. crls = sk_X509_CRL_new_null();
  1567. if (!crls || !sk_X509_CRL_push(crls, crl)) {
  1568. BIO_puts(bio_err, "Error adding CRL\n");
  1569. ERR_print_errors(bio_err);
  1570. X509_CRL_free(crl);
  1571. goto end;
  1572. }
  1573. }
  1574. if (s_dcert_file) {
  1575. if (s_dkey_file == NULL)
  1576. s_dkey_file = s_dcert_file;
  1577. s_dkey = load_key(bio_err, s_dkey_file, s_dkey_format,
  1578. 0, dpass, e, "second certificate private key file");
  1579. if (!s_dkey) {
  1580. ERR_print_errors(bio_err);
  1581. goto end;
  1582. }
  1583. s_dcert = load_cert(bio_err, s_dcert_file, s_dcert_format,
  1584. NULL, e, "second server certificate file");
  1585. if (!s_dcert) {
  1586. ERR_print_errors(bio_err);
  1587. goto end;
  1588. }
  1589. if (s_dchain_file) {
  1590. s_dchain = load_certs(bio_err, s_dchain_file, FORMAT_PEM,
  1591. NULL, e, "second server certificate chain");
  1592. if (!s_dchain)
  1593. goto end;
  1594. }
  1595. }
  1596. if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
  1597. && !RAND_status()) {
  1598. BIO_printf(bio_err,
  1599. "warning, not much extra random data, consider using the -rand option\n");
  1600. }
  1601. if (inrand != NULL)
  1602. BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
  1603. app_RAND_load_files(inrand));
  1604. if (bio_s_out == NULL) {
  1605. if (s_quiet && !s_debug) {
  1606. bio_s_out = BIO_new(BIO_s_null());
  1607. if (s_msg && !bio_s_msg)
  1608. bio_s_msg = BIO_new_fp(stdout, BIO_NOCLOSE);
  1609. } else {
  1610. if (bio_s_out == NULL)
  1611. bio_s_out = BIO_new_fp(stdout, BIO_NOCLOSE);
  1612. }
  1613. }
  1614. #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
  1615. if (nocert)
  1616. #endif
  1617. {
  1618. s_cert_file = NULL;
  1619. s_key_file = NULL;
  1620. s_dcert_file = NULL;
  1621. s_dkey_file = NULL;
  1622. #ifndef OPENSSL_NO_TLSEXT
  1623. s_cert_file2 = NULL;
  1624. s_key_file2 = NULL;
  1625. #endif
  1626. }
  1627. ctx = SSL_CTX_new(meth);
  1628. if (ctx == NULL) {
  1629. ERR_print_errors(bio_err);
  1630. goto end;
  1631. }
  1632. if (session_id_prefix) {
  1633. if (strlen(session_id_prefix) >= 32)
  1634. BIO_printf(bio_err,
  1635. "warning: id_prefix is too long, only one new session will be possible\n");
  1636. else if (strlen(session_id_prefix) >= 16)
  1637. BIO_printf(bio_err,
  1638. "warning: id_prefix is too long if you use SSLv2\n");
  1639. if (!SSL_CTX_set_generate_session_id(ctx, generate_session_id)) {
  1640. BIO_printf(bio_err, "error setting 'id_prefix'\n");
  1641. ERR_print_errors(bio_err);
  1642. goto end;
  1643. }
  1644. BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
  1645. }
  1646. SSL_CTX_set_quiet_shutdown(ctx, 1);
  1647. if (hack)
  1648. SSL_CTX_set_options(ctx, SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
  1649. if (exc)
  1650. ssl_ctx_set_excert(ctx, exc);
  1651. if (state)
  1652. SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
  1653. if (no_cache)
  1654. SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
  1655. else if (ext_cache)
  1656. init_session_cache_ctx(ctx);
  1657. else
  1658. SSL_CTX_sess_set_cache_size(ctx, 128);
  1659. #ifndef OPENSSL_NO_SRTP
  1660. if (srtp_profiles != NULL)
  1661. SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles);
  1662. #endif
  1663. #if 0
  1664. if (cipher == NULL)
  1665. cipher = getenv("SSL_CIPHER");
  1666. #endif
  1667. #if 0
  1668. if (s_cert_file == NULL) {
  1669. BIO_printf(bio_err,
  1670. "You must specify a certificate file for the server to use\n");
  1671. goto end;
  1672. }
  1673. #endif
  1674. if ((!SSL_CTX_load_verify_locations(ctx, CAfile, CApath)) ||
  1675. (!SSL_CTX_set_default_verify_paths(ctx))) {
  1676. /* BIO_printf(bio_err,"X509_load_verify_locations\n"); */
  1677. ERR_print_errors(bio_err);
  1678. /* goto end; */
  1679. }
  1680. if (vpm)
  1681. SSL_CTX_set1_param(ctx, vpm);
  1682. ssl_ctx_add_crls(ctx, crls, 0);
  1683. if (!args_ssl_call(ctx, bio_err, cctx, ssl_args, no_ecdhe, no_jpake))
  1684. goto end;
  1685. if (!ssl_load_stores(ctx, vfyCApath, vfyCAfile, chCApath, chCAfile,
  1686. crls, crl_download)) {
  1687. BIO_printf(bio_err, "Error loading store locations\n");
  1688. ERR_print_errors(bio_err);
  1689. goto end;
  1690. }
  1691. #ifndef OPENSSL_NO_TLSEXT
  1692. if (s_cert2) {
  1693. ctx2 = SSL_CTX_new(meth);
  1694. if (ctx2 == NULL) {
  1695. ERR_print_errors(bio_err);
  1696. goto end;
  1697. }
  1698. }
  1699. if (ctx2) {
  1700. BIO_printf(bio_s_out, "Setting secondary ctx parameters\n");
  1701. if (session_id_prefix) {
  1702. if (strlen(session_id_prefix) >= 32)
  1703. BIO_printf(bio_err,
  1704. "warning: id_prefix is too long, only one new session will be possible\n");
  1705. else if (strlen(session_id_prefix) >= 16)
  1706. BIO_printf(bio_err,
  1707. "warning: id_prefix is too long if you use SSLv2\n");
  1708. if (!SSL_CTX_set_generate_session_id(ctx2, generate_session_id)) {
  1709. BIO_printf(bio_err, "error setting 'id_prefix'\n");
  1710. ERR_print_errors(bio_err);
  1711. goto end;
  1712. }
  1713. BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix);
  1714. }
  1715. SSL_CTX_set_quiet_shutdown(ctx2, 1);
  1716. if (hack)
  1717. SSL_CTX_set_options(ctx2, SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
  1718. if (exc)
  1719. ssl_ctx_set_excert(ctx2, exc);
  1720. if (state)
  1721. SSL_CTX_set_info_callback(ctx2, apps_ssl_info_callback);
  1722. if (no_cache)
  1723. SSL_CTX_set_session_cache_mode(ctx2, SSL_SESS_CACHE_OFF);
  1724. else if (ext_cache)
  1725. init_session_cache_ctx(ctx2);
  1726. else
  1727. SSL_CTX_sess_set_cache_size(ctx2, 128);
  1728. if ((!SSL_CTX_load_verify_locations(ctx2, CAfile, CApath)) ||
  1729. (!SSL_CTX_set_default_verify_paths(ctx2))) {
  1730. ERR_print_errors(bio_err);
  1731. }
  1732. if (vpm)
  1733. SSL_CTX_set1_param(ctx2, vpm);
  1734. ssl_ctx_add_crls(ctx2, crls, 0);
  1735. if (!args_ssl_call(ctx2, bio_err, cctx, ssl_args, no_ecdhe, no_jpake))
  1736. goto end;
  1737. }
  1738. # ifndef OPENSSL_NO_NEXTPROTONEG
  1739. if (next_proto.data)
  1740. SSL_CTX_set_next_protos_advertised_cb(ctx, next_proto_cb,
  1741. &next_proto);
  1742. # endif
  1743. if (alpn_ctx.data)
  1744. SSL_CTX_set_alpn_select_cb(ctx, alpn_cb, &alpn_ctx);
  1745. #endif
  1746. #ifndef OPENSSL_NO_DH
  1747. if (!no_dhe) {
  1748. DH *dh = NULL;
  1749. if (dhfile)
  1750. dh = load_dh_param(dhfile);
  1751. else if (s_cert_file)
  1752. dh = load_dh_param(s_cert_file);
  1753. if (dh != NULL) {
  1754. BIO_printf(bio_s_out, "Setting temp DH parameters\n");
  1755. } else {
  1756. BIO_printf(bio_s_out, "Using default temp DH parameters\n");
  1757. dh = get_dh2048();
  1758. if (dh == NULL) {
  1759. ERR_print_errors(bio_err);
  1760. goto end;
  1761. }
  1762. }
  1763. (void)BIO_flush(bio_s_out);
  1764. SSL_CTX_set_tmp_dh(ctx, dh);
  1765. # ifndef OPENSSL_NO_TLSEXT
  1766. if (ctx2) {
  1767. if (!dhfile) {
  1768. DH *dh2 = load_dh_param(s_cert_file2);
  1769. if (dh2 != NULL) {
  1770. BIO_printf(bio_s_out, "Setting temp DH parameters\n");
  1771. (void)BIO_flush(bio_s_out);
  1772. DH_free(dh);
  1773. dh = dh2;
  1774. }
  1775. }
  1776. SSL_CTX_set_tmp_dh(ctx2, dh);
  1777. }
  1778. # endif
  1779. DH_free(dh);
  1780. }
  1781. #endif
  1782. if (!set_cert_key_stuff(ctx, s_cert, s_key, s_chain, build_chain))
  1783. goto end;
  1784. #ifndef OPENSSL_NO_TLSEXT
  1785. if (s_serverinfo_file != NULL
  1786. && !SSL_CTX_use_serverinfo_file(ctx, s_serverinfo_file)) {
  1787. ERR_print_errors(bio_err);
  1788. goto end;
  1789. }
  1790. #endif
  1791. #ifndef OPENSSL_NO_TLSEXT
  1792. if (ctx2 && !set_cert_key_stuff(ctx2, s_cert2, s_key2, NULL, build_chain))
  1793. goto end;
  1794. #endif
  1795. if (s_dcert != NULL) {
  1796. if (!set_cert_key_stuff(ctx, s_dcert, s_dkey, s_dchain, build_chain))
  1797. goto end;
  1798. }
  1799. #ifndef OPENSSL_NO_RSA
  1800. # if 1
  1801. if (!no_tmp_rsa) {
  1802. SSL_CTX_set_tmp_rsa_callback(ctx, tmp_rsa_cb);
  1803. # ifndef OPENSSL_NO_TLSEXT
  1804. if (ctx2)
  1805. SSL_CTX_set_tmp_rsa_callback(ctx2, tmp_rsa_cb);
  1806. # endif
  1807. }
  1808. # else
  1809. if (!no_tmp_rsa && SSL_CTX_need_tmp_RSA(ctx)) {
  1810. RSA *rsa;
  1811. BIO_printf(bio_s_out, "Generating temp (512 bit) RSA key...");
  1812. BIO_flush(bio_s_out);
  1813. rsa = RSA_generate_key(512, RSA_F4, NULL);
  1814. if (!SSL_CTX_set_tmp_rsa(ctx, rsa)) {
  1815. ERR_print_errors(bio_err);
  1816. goto end;
  1817. }
  1818. # ifndef OPENSSL_NO_TLSEXT
  1819. if (ctx2) {
  1820. if (!SSL_CTX_set_tmp_rsa(ctx2, rsa)) {
  1821. ERR_print_errors(bio_err);
  1822. goto end;
  1823. }
  1824. }
  1825. # endif
  1826. RSA_free(rsa);
  1827. BIO_printf(bio_s_out, "\n");
  1828. }
  1829. # endif
  1830. #endif
  1831. #ifndef OPENSSL_NO_PSK
  1832. # ifdef OPENSSL_NO_JPAKE
  1833. if (psk_key != NULL)
  1834. # else
  1835. if (psk_key != NULL || jpake_secret)
  1836. # endif
  1837. {
  1838. if (s_debug)
  1839. BIO_printf(bio_s_out,
  1840. "PSK key given or JPAKE in use, setting server callback\n");
  1841. SSL_CTX_set_psk_server_callback(ctx, psk_server_cb);
  1842. }
  1843. if (!SSL_CTX_use_psk_identity_hint(ctx, psk_identity_hint)) {
  1844. BIO_printf(bio_err, "error setting PSK identity hint to context\n");
  1845. ERR_print_errors(bio_err);
  1846. goto end;
  1847. }
  1848. #endif
  1849. SSL_CTX_set_verify(ctx, s_server_verify, verify_callback);
  1850. SSL_CTX_set_session_id_context(ctx, (void *)&s_server_session_id_context,
  1851. sizeof s_server_session_id_context);
  1852. /* Set DTLS cookie generation and verification callbacks */
  1853. SSL_CTX_set_cookie_generate_cb(ctx, generate_cookie_callback);
  1854. SSL_CTX_set_cookie_verify_cb(ctx, verify_cookie_callback);
  1855. #ifndef OPENSSL_NO_TLSEXT
  1856. if (ctx2) {
  1857. SSL_CTX_set_verify(ctx2, s_server_verify, verify_callback);
  1858. SSL_CTX_set_session_id_context(ctx2,
  1859. (void *)&s_server_session_id_context,
  1860. sizeof s_server_session_id_context);
  1861. tlsextcbp.biodebug = bio_s_out;
  1862. SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb);
  1863. SSL_CTX_set_tlsext_servername_arg(ctx2, &tlsextcbp);
  1864. SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
  1865. SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
  1866. }
  1867. #endif
  1868. #ifndef OPENSSL_NO_SRP
  1869. if (srp_verifier_file != NULL) {
  1870. srp_callback_parm.vb = SRP_VBASE_new(srpuserseed);
  1871. srp_callback_parm.user = NULL;
  1872. srp_callback_parm.login = NULL;
  1873. if ((ret =
  1874. SRP_VBASE_init(srp_callback_parm.vb,
  1875. srp_verifier_file)) != SRP_NO_ERROR) {
  1876. BIO_printf(bio_err,
  1877. "Cannot initialize SRP verifier file \"%s\":ret=%d\n",
  1878. srp_verifier_file, ret);
  1879. goto end;
  1880. }
  1881. SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, verify_callback);
  1882. SSL_CTX_set_srp_cb_arg(ctx, &srp_callback_parm);
  1883. SSL_CTX_set_srp_username_callback(ctx, ssl_srp_server_param_cb);
  1884. } else
  1885. #endif
  1886. if (CAfile != NULL) {
  1887. SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(CAfile));
  1888. #ifndef OPENSSL_NO_TLSEXT
  1889. if (ctx2)
  1890. SSL_CTX_set_client_CA_list(ctx2, SSL_load_client_CA_file(CAfile));
  1891. #endif
  1892. }
  1893. BIO_printf(bio_s_out, "ACCEPT\n");
  1894. (void)BIO_flush(bio_s_out);
  1895. if (rev)
  1896. do_server(port, socket_type, &accept_socket, rev_body, context,
  1897. naccept);
  1898. else if (www)
  1899. do_server(port, socket_type, &accept_socket, www_body, context,
  1900. naccept);
  1901. else
  1902. do_server(port, socket_type, &accept_socket, sv_body, context,
  1903. naccept);
  1904. print_stats(bio_s_out, ctx);
  1905. ret = 0;
  1906. end:
  1907. if (ctx != NULL)
  1908. SSL_CTX_free(ctx);
  1909. if (s_cert)
  1910. X509_free(s_cert);
  1911. if (crls)
  1912. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  1913. if (s_dcert)
  1914. X509_free(s_dcert);
  1915. if (s_key)
  1916. EVP_PKEY_free(s_key);
  1917. if (s_dkey)
  1918. EVP_PKEY_free(s_dkey);
  1919. if (s_chain)
  1920. sk_X509_pop_free(s_chain, X509_free);
  1921. if (s_dchain)
  1922. sk_X509_pop_free(s_dchain, X509_free);
  1923. if (pass)
  1924. OPENSSL_free(pass);
  1925. if (dpass)
  1926. OPENSSL_free(dpass);
  1927. if (vpm)
  1928. X509_VERIFY_PARAM_free(vpm);
  1929. free_sessions();
  1930. #ifndef OPENSSL_NO_TLSEXT
  1931. if (tlscstatp.host)
  1932. OPENSSL_free(tlscstatp.host);
  1933. if (tlscstatp.port)
  1934. OPENSSL_free(tlscstatp.port);
  1935. if (tlscstatp.path)
  1936. OPENSSL_free(tlscstatp.path);
  1937. if (ctx2 != NULL)
  1938. SSL_CTX_free(ctx2);
  1939. if (s_cert2)
  1940. X509_free(s_cert2);
  1941. if (s_key2)
  1942. EVP_PKEY_free(s_key2);
  1943. if (serverinfo_in != NULL)
  1944. BIO_free(serverinfo_in);
  1945. # ifndef OPENSSL_NO_NEXTPROTONEG
  1946. if (next_proto.data)
  1947. OPENSSL_free(next_proto.data);
  1948. # endif
  1949. if (alpn_ctx.data)
  1950. OPENSSL_free(alpn_ctx.data);
  1951. #endif
  1952. ssl_excert_free(exc);
  1953. if (ssl_args)
  1954. sk_OPENSSL_STRING_free(ssl_args);
  1955. if (cctx)
  1956. SSL_CONF_CTX_free(cctx);
  1957. #ifndef OPENSSL_NO_JPAKE
  1958. if (jpake_secret && psk_key)
  1959. OPENSSL_free(psk_key);
  1960. #endif
  1961. if (bio_s_out != NULL) {
  1962. BIO_free(bio_s_out);
  1963. bio_s_out = NULL;
  1964. }
  1965. if (bio_s_msg != NULL) {
  1966. BIO_free(bio_s_msg);
  1967. bio_s_msg = NULL;
  1968. }
  1969. apps_shutdown();
  1970. OPENSSL_EXIT(ret);
  1971. }
  1972. static void print_stats(BIO *bio, SSL_CTX *ssl_ctx)
  1973. {
  1974. BIO_printf(bio, "%4ld items in the session cache\n",
  1975. SSL_CTX_sess_number(ssl_ctx));
  1976. BIO_printf(bio, "%4ld client connects (SSL_connect())\n",
  1977. SSL_CTX_sess_connect(ssl_ctx));
  1978. BIO_printf(bio, "%4ld client renegotiates (SSL_connect())\n",
  1979. SSL_CTX_sess_connect_renegotiate(ssl_ctx));
  1980. BIO_printf(bio, "%4ld client connects that finished\n",
  1981. SSL_CTX_sess_connect_good(ssl_ctx));
  1982. BIO_printf(bio, "%4ld server accepts (SSL_accept())\n",
  1983. SSL_CTX_sess_accept(ssl_ctx));
  1984. BIO_printf(bio, "%4ld server renegotiates (SSL_accept())\n",
  1985. SSL_CTX_sess_accept_renegotiate(ssl_ctx));
  1986. BIO_printf(bio, "%4ld server accepts that finished\n",
  1987. SSL_CTX_sess_accept_good(ssl_ctx));
  1988. BIO_printf(bio, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx));
  1989. BIO_printf(bio, "%4ld session cache misses\n",
  1990. SSL_CTX_sess_misses(ssl_ctx));
  1991. BIO_printf(bio, "%4ld session cache timeouts\n",
  1992. SSL_CTX_sess_timeouts(ssl_ctx));
  1993. BIO_printf(bio, "%4ld callback cache hits\n",
  1994. SSL_CTX_sess_cb_hits(ssl_ctx));
  1995. BIO_printf(bio, "%4ld cache full overflows (%ld allowed)\n",
  1996. SSL_CTX_sess_cache_full(ssl_ctx),
  1997. SSL_CTX_sess_get_cache_size(ssl_ctx));
  1998. }
  1999. static int sv_body(char *hostname, int s, int stype, unsigned char *context)
  2000. {
  2001. char *buf = NULL;
  2002. fd_set readfds;
  2003. int ret = 1, width;
  2004. int k, i;
  2005. unsigned long l;
  2006. SSL *con = NULL;
  2007. BIO *sbio;
  2008. #ifndef OPENSSL_NO_KRB5
  2009. KSSL_CTX *kctx;
  2010. #endif
  2011. struct timeval timeout;
  2012. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)
  2013. struct timeval tv;
  2014. #else
  2015. struct timeval *timeoutp;
  2016. #endif
  2017. if ((buf = OPENSSL_malloc(bufsize)) == NULL) {
  2018. BIO_printf(bio_err, "out of memory\n");
  2019. goto err;
  2020. }
  2021. #ifdef FIONBIO
  2022. if (s_nbio) {
  2023. unsigned long sl = 1;
  2024. if (!s_quiet)
  2025. BIO_printf(bio_err, "turning on non blocking io\n");
  2026. if (BIO_socket_ioctl(s, FIONBIO, &sl) < 0)
  2027. ERR_print_errors(bio_err);
  2028. }
  2029. #endif
  2030. if (con == NULL) {
  2031. con = SSL_new(ctx);
  2032. #ifndef OPENSSL_NO_TLSEXT
  2033. if (s_tlsextdebug) {
  2034. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  2035. SSL_set_tlsext_debug_arg(con, bio_s_out);
  2036. }
  2037. if (s_tlsextstatus) {
  2038. SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb);
  2039. tlscstatp.err = bio_err;
  2040. SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp);
  2041. }
  2042. #endif
  2043. #ifndef OPENSSL_NO_KRB5
  2044. if ((kctx = kssl_ctx_new()) != NULL) {
  2045. SSL_set0_kssl_ctx(con, kctx);
  2046. kssl_ctx_setstring(kctx, KSSL_SERVICE, KRB5SVC);
  2047. kssl_ctx_setstring(kctx, KSSL_KEYTAB, KRB5KEYTAB);
  2048. }
  2049. #endif /* OPENSSL_NO_KRB5 */
  2050. if (context)
  2051. SSL_set_session_id_context(con, context, strlen((char *)context));
  2052. }
  2053. SSL_clear(con);
  2054. #if 0
  2055. # ifdef TLSEXT_TYPE_opaque_prf_input
  2056. SSL_set_tlsext_opaque_prf_input(con, "Test server", 11);
  2057. # endif
  2058. #endif
  2059. if (stype == SOCK_DGRAM) {
  2060. sbio = BIO_new_dgram(s, BIO_NOCLOSE);
  2061. if (enable_timeouts) {
  2062. timeout.tv_sec = 0;
  2063. timeout.tv_usec = DGRAM_RCV_TIMEOUT;
  2064. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
  2065. timeout.tv_sec = 0;
  2066. timeout.tv_usec = DGRAM_SND_TIMEOUT;
  2067. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
  2068. }
  2069. if (socket_mtu) {
  2070. if (socket_mtu < DTLS_get_link_min_mtu(con)) {
  2071. BIO_printf(bio_err, "MTU too small. Must be at least %ld\n",
  2072. DTLS_get_link_min_mtu(con));
  2073. ret = -1;
  2074. BIO_free(sbio);
  2075. goto err;
  2076. }
  2077. SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
  2078. if (!DTLS_set_link_mtu(con, socket_mtu)) {
  2079. BIO_printf(bio_err, "Failed to set MTU\n");
  2080. ret = -1;
  2081. BIO_free(sbio);
  2082. goto err;
  2083. }
  2084. } else
  2085. /* want to do MTU discovery */
  2086. BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
  2087. /* turn on cookie exchange */
  2088. SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE);
  2089. } else
  2090. sbio = BIO_new_socket(s, BIO_NOCLOSE);
  2091. if (s_nbio_test) {
  2092. BIO *test;
  2093. test = BIO_new(BIO_f_nbio_test());
  2094. sbio = BIO_push(test, sbio);
  2095. }
  2096. #ifndef OPENSSL_NO_JPAKE
  2097. if (jpake_secret)
  2098. jpake_server_auth(bio_s_out, sbio, jpake_secret);
  2099. #endif
  2100. SSL_set_bio(con, sbio, sbio);
  2101. SSL_set_accept_state(con);
  2102. /* SSL_set_fd(con,s); */
  2103. if (s_debug) {
  2104. SSL_set_debug(con, 1);
  2105. BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
  2106. BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
  2107. }
  2108. if (s_msg) {
  2109. #ifndef OPENSSL_NO_SSL_TRACE
  2110. if (s_msg == 2)
  2111. SSL_set_msg_callback(con, SSL_trace);
  2112. else
  2113. #endif
  2114. SSL_set_msg_callback(con, msg_cb);
  2115. SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
  2116. }
  2117. #ifndef OPENSSL_NO_TLSEXT
  2118. if (s_tlsextdebug) {
  2119. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  2120. SSL_set_tlsext_debug_arg(con, bio_s_out);
  2121. }
  2122. #endif
  2123. width = s + 1;
  2124. for (;;) {
  2125. int read_from_terminal;
  2126. int read_from_sslcon;
  2127. read_from_terminal = 0;
  2128. read_from_sslcon = SSL_pending(con);
  2129. if (!read_from_sslcon) {
  2130. FD_ZERO(&readfds);
  2131. #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) && !defined(OPENSSL_SYS_BEOS_R5)
  2132. openssl_fdset(fileno(stdin), &readfds);
  2133. #endif
  2134. openssl_fdset(s, &readfds);
  2135. /*
  2136. * Note: under VMS with SOCKETSHR the second parameter is
  2137. * currently of type (int *) whereas under other systems it is
  2138. * (void *) if you don't have a cast it will choke the compiler:
  2139. * if you do have a cast then you can either go for (int *) or
  2140. * (void *).
  2141. */
  2142. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)
  2143. /*
  2144. * Under DOS (non-djgpp) and Windows we can't select on stdin:
  2145. * only on sockets. As a workaround we timeout the select every
  2146. * second and check for any keypress. In a proper Windows
  2147. * application we wouldn't do this because it is inefficient.
  2148. */
  2149. tv.tv_sec = 1;
  2150. tv.tv_usec = 0;
  2151. i = select(width, (void *)&readfds, NULL, NULL, &tv);
  2152. if ((i < 0) || (!i && !_kbhit()))
  2153. continue;
  2154. if (_kbhit())
  2155. read_from_terminal = 1;
  2156. #elif defined(OPENSSL_SYS_BEOS_R5)
  2157. /* Under BeOS-R5 the situation is similar to DOS */
  2158. tv.tv_sec = 1;
  2159. tv.tv_usec = 0;
  2160. (void)fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
  2161. i = select(width, (void *)&readfds, NULL, NULL, &tv);
  2162. if ((i < 0) || (!i && read(fileno(stdin), buf, 0) < 0))
  2163. continue;
  2164. if (read(fileno(stdin), buf, 0) >= 0)
  2165. read_from_terminal = 1;
  2166. (void)fcntl(fileno(stdin), F_SETFL, 0);
  2167. #else
  2168. if ((SSL_version(con) == DTLS1_VERSION) &&
  2169. DTLSv1_get_timeout(con, &timeout))
  2170. timeoutp = &timeout;
  2171. else
  2172. timeoutp = NULL;
  2173. i = select(width, (void *)&readfds, NULL, NULL, timeoutp);
  2174. if ((SSL_version(con) == DTLS1_VERSION)
  2175. && DTLSv1_handle_timeout(con) > 0) {
  2176. BIO_printf(bio_err, "TIMEOUT occured\n");
  2177. }
  2178. if (i <= 0)
  2179. continue;
  2180. if (FD_ISSET(fileno(stdin), &readfds))
  2181. read_from_terminal = 1;
  2182. #endif
  2183. if (FD_ISSET(s, &readfds))
  2184. read_from_sslcon = 1;
  2185. }
  2186. if (read_from_terminal) {
  2187. if (s_crlf) {
  2188. int j, lf_num;
  2189. i = raw_read_stdin(buf, bufsize / 2);
  2190. lf_num = 0;
  2191. /* both loops are skipped when i <= 0 */
  2192. for (j = 0; j < i; j++)
  2193. if (buf[j] == '\n')
  2194. lf_num++;
  2195. for (j = i - 1; j >= 0; j--) {
  2196. buf[j + lf_num] = buf[j];
  2197. if (buf[j] == '\n') {
  2198. lf_num--;
  2199. i++;
  2200. buf[j + lf_num] = '\r';
  2201. }
  2202. }
  2203. assert(lf_num == 0);
  2204. } else
  2205. i = raw_read_stdin(buf, bufsize);
  2206. if (!s_quiet && !s_brief) {
  2207. if ((i <= 0) || (buf[0] == 'Q')) {
  2208. BIO_printf(bio_s_out, "DONE\n");
  2209. SHUTDOWN(s);
  2210. close_accept_socket();
  2211. ret = -11;
  2212. goto err;
  2213. }
  2214. if ((i <= 0) || (buf[0] == 'q')) {
  2215. BIO_printf(bio_s_out, "DONE\n");
  2216. if (SSL_version(con) != DTLS1_VERSION)
  2217. SHUTDOWN(s);
  2218. /*
  2219. * close_accept_socket(); ret= -11;
  2220. */
  2221. goto err;
  2222. }
  2223. #ifndef OPENSSL_NO_HEARTBEATS
  2224. if ((buf[0] == 'B') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
  2225. BIO_printf(bio_err, "HEARTBEATING\n");
  2226. SSL_heartbeat(con);
  2227. i = 0;
  2228. continue;
  2229. }
  2230. #endif
  2231. if ((buf[0] == 'r') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
  2232. SSL_renegotiate(con);
  2233. i = SSL_do_handshake(con);
  2234. printf("SSL_do_handshake -> %d\n", i);
  2235. i = 0; /* 13; */
  2236. continue;
  2237. /*
  2238. * strcpy(buf,"server side RE-NEGOTIATE\n");
  2239. */
  2240. }
  2241. if ((buf[0] == 'R') && ((buf[1] == '\n') || (buf[1] == '\r'))) {
  2242. SSL_set_verify(con,
  2243. SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
  2244. NULL);
  2245. SSL_renegotiate(con);
  2246. i = SSL_do_handshake(con);
  2247. printf("SSL_do_handshake -> %d\n", i);
  2248. i = 0; /* 13; */
  2249. continue;
  2250. /*
  2251. * strcpy(buf,"server side RE-NEGOTIATE asking for client
  2252. * cert\n");
  2253. */
  2254. }
  2255. if (buf[0] == 'P') {
  2256. static const char *str = "Lets print some clear text\n";
  2257. BIO_write(SSL_get_wbio(con), str, strlen(str));
  2258. }
  2259. if (buf[0] == 'S') {
  2260. print_stats(bio_s_out, SSL_get_SSL_CTX(con));
  2261. }
  2262. }
  2263. #ifdef CHARSET_EBCDIC
  2264. ebcdic2ascii(buf, buf, i);
  2265. #endif
  2266. l = k = 0;
  2267. for (;;) {
  2268. /* should do a select for the write */
  2269. #ifdef RENEG
  2270. {
  2271. static count = 0;
  2272. if (++count == 100) {
  2273. count = 0;
  2274. SSL_renegotiate(con);
  2275. }
  2276. }
  2277. #endif
  2278. k = SSL_write(con, &(buf[l]), (unsigned int)i);
  2279. #ifndef OPENSSL_NO_SRP
  2280. while (SSL_get_error(con, k) == SSL_ERROR_WANT_X509_LOOKUP) {
  2281. BIO_printf(bio_s_out, "LOOKUP renego during write\n");
  2282. SRP_user_pwd_free(srp_callback_parm.user);
  2283. srp_callback_parm.user =
  2284. SRP_VBASE_get1_by_user(srp_callback_parm.vb,
  2285. srp_callback_parm.login);
  2286. if (srp_callback_parm.user)
  2287. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  2288. srp_callback_parm.user->info);
  2289. else
  2290. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  2291. k = SSL_write(con, &(buf[l]), (unsigned int)i);
  2292. }
  2293. #endif
  2294. switch (SSL_get_error(con, k)) {
  2295. case SSL_ERROR_NONE:
  2296. break;
  2297. case SSL_ERROR_WANT_WRITE:
  2298. case SSL_ERROR_WANT_READ:
  2299. case SSL_ERROR_WANT_X509_LOOKUP:
  2300. BIO_printf(bio_s_out, "Write BLOCK\n");
  2301. break;
  2302. case SSL_ERROR_SYSCALL:
  2303. case SSL_ERROR_SSL:
  2304. BIO_printf(bio_s_out, "ERROR\n");
  2305. ERR_print_errors(bio_err);
  2306. ret = 1;
  2307. goto err;
  2308. /* break; */
  2309. case SSL_ERROR_ZERO_RETURN:
  2310. BIO_printf(bio_s_out, "DONE\n");
  2311. ret = 1;
  2312. goto err;
  2313. }
  2314. if (k > 0) {
  2315. l += k;
  2316. i -= k;
  2317. }
  2318. if (i <= 0)
  2319. break;
  2320. }
  2321. }
  2322. if (read_from_sslcon) {
  2323. if (!SSL_is_init_finished(con)) {
  2324. i = init_ssl_connection(con);
  2325. if (i < 0) {
  2326. ret = 0;
  2327. goto err;
  2328. } else if (i == 0) {
  2329. ret = 1;
  2330. goto err;
  2331. }
  2332. } else {
  2333. again:
  2334. i = SSL_read(con, (char *)buf, bufsize);
  2335. #ifndef OPENSSL_NO_SRP
  2336. while (SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) {
  2337. BIO_printf(bio_s_out, "LOOKUP renego during read\n");
  2338. SRP_user_pwd_free(srp_callback_parm.user);
  2339. srp_callback_parm.user =
  2340. SRP_VBASE_get1_by_user(srp_callback_parm.vb,
  2341. srp_callback_parm.login);
  2342. if (srp_callback_parm.user)
  2343. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  2344. srp_callback_parm.user->info);
  2345. else
  2346. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  2347. i = SSL_read(con, (char *)buf, bufsize);
  2348. }
  2349. #endif
  2350. switch (SSL_get_error(con, i)) {
  2351. case SSL_ERROR_NONE:
  2352. #ifdef CHARSET_EBCDIC
  2353. ascii2ebcdic(buf, buf, i);
  2354. #endif
  2355. raw_write_stdout(buf, (unsigned int)i);
  2356. if (SSL_pending(con))
  2357. goto again;
  2358. break;
  2359. case SSL_ERROR_WANT_WRITE:
  2360. case SSL_ERROR_WANT_READ:
  2361. BIO_printf(bio_s_out, "Read BLOCK\n");
  2362. break;
  2363. case SSL_ERROR_SYSCALL:
  2364. case SSL_ERROR_SSL:
  2365. BIO_printf(bio_s_out, "ERROR\n");
  2366. ERR_print_errors(bio_err);
  2367. ret = 1;
  2368. goto err;
  2369. case SSL_ERROR_ZERO_RETURN:
  2370. BIO_printf(bio_s_out, "DONE\n");
  2371. ret = 1;
  2372. goto err;
  2373. }
  2374. }
  2375. }
  2376. }
  2377. err:
  2378. if (con != NULL) {
  2379. BIO_printf(bio_s_out, "shutting down SSL\n");
  2380. #if 1
  2381. SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  2382. #else
  2383. SSL_shutdown(con);
  2384. #endif
  2385. SSL_free(con);
  2386. }
  2387. BIO_printf(bio_s_out, "CONNECTION CLOSED\n");
  2388. if (buf != NULL) {
  2389. OPENSSL_cleanse(buf, bufsize);
  2390. OPENSSL_free(buf);
  2391. }
  2392. if (ret >= 0)
  2393. BIO_printf(bio_s_out, "ACCEPT\n");
  2394. return (ret);
  2395. }
  2396. static void close_accept_socket(void)
  2397. {
  2398. BIO_printf(bio_err, "shutdown accept socket\n");
  2399. if (accept_socket >= 0) {
  2400. SHUTDOWN2(accept_socket);
  2401. }
  2402. }
  2403. static int init_ssl_connection(SSL *con)
  2404. {
  2405. int i;
  2406. const char *str;
  2407. X509 *peer;
  2408. long verify_error;
  2409. MS_STATIC char buf[BUFSIZ];
  2410. #ifndef OPENSSL_NO_KRB5
  2411. char *client_princ;
  2412. #endif
  2413. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
  2414. const unsigned char *next_proto_neg;
  2415. unsigned next_proto_neg_len;
  2416. #endif
  2417. unsigned char *exportedkeymat;
  2418. i = SSL_accept(con);
  2419. #ifdef CERT_CB_TEST_RETRY
  2420. {
  2421. while (i <= 0 && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP
  2422. && SSL_state(con) == SSL3_ST_SR_CLNT_HELLO_C) {
  2423. fprintf(stderr,
  2424. "LOOKUP from certificate callback during accept\n");
  2425. i = SSL_accept(con);
  2426. }
  2427. }
  2428. #endif
  2429. #ifndef OPENSSL_NO_SRP
  2430. while (i <= 0 && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) {
  2431. BIO_printf(bio_s_out, "LOOKUP during accept %s\n",
  2432. srp_callback_parm.login);
  2433. SRP_user_pwd_free(srp_callback_parm.user);
  2434. srp_callback_parm.user =
  2435. SRP_VBASE_get1_by_user(srp_callback_parm.vb,
  2436. srp_callback_parm.login);
  2437. if (srp_callback_parm.user)
  2438. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  2439. srp_callback_parm.user->info);
  2440. else
  2441. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  2442. i = SSL_accept(con);
  2443. }
  2444. #endif
  2445. if (i <= 0) {
  2446. if (BIO_sock_should_retry(i)) {
  2447. BIO_printf(bio_s_out, "DELAY\n");
  2448. return (1);
  2449. }
  2450. BIO_printf(bio_err, "ERROR\n");
  2451. verify_error = SSL_get_verify_result(con);
  2452. if (verify_error != X509_V_OK) {
  2453. BIO_printf(bio_err, "verify error:%s\n",
  2454. X509_verify_cert_error_string(verify_error));
  2455. }
  2456. /* Always print any error messages */
  2457. ERR_print_errors(bio_err);
  2458. return (0);
  2459. }
  2460. if (s_brief)
  2461. print_ssl_summary(bio_err, con);
  2462. PEM_write_bio_SSL_SESSION(bio_s_out, SSL_get_session(con));
  2463. peer = SSL_get_peer_certificate(con);
  2464. if (peer != NULL) {
  2465. BIO_printf(bio_s_out, "Client certificate\n");
  2466. PEM_write_bio_X509(bio_s_out, peer);
  2467. X509_NAME_oneline(X509_get_subject_name(peer), buf, sizeof buf);
  2468. BIO_printf(bio_s_out, "subject=%s\n", buf);
  2469. X509_NAME_oneline(X509_get_issuer_name(peer), buf, sizeof buf);
  2470. BIO_printf(bio_s_out, "issuer=%s\n", buf);
  2471. X509_free(peer);
  2472. }
  2473. if (SSL_get_shared_ciphers(con, buf, sizeof buf) != NULL)
  2474. BIO_printf(bio_s_out, "Shared ciphers:%s\n", buf);
  2475. str = SSL_CIPHER_get_name(SSL_get_current_cipher(con));
  2476. ssl_print_sigalgs(bio_s_out, con);
  2477. #ifndef OPENSSL_NO_EC
  2478. ssl_print_point_formats(bio_s_out, con);
  2479. ssl_print_curves(bio_s_out, con, 0);
  2480. #endif
  2481. BIO_printf(bio_s_out, "CIPHER is %s\n", (str != NULL) ? str : "(NONE)");
  2482. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
  2483. SSL_get0_next_proto_negotiated(con, &next_proto_neg, &next_proto_neg_len);
  2484. if (next_proto_neg) {
  2485. BIO_printf(bio_s_out, "NEXTPROTO is ");
  2486. BIO_write(bio_s_out, next_proto_neg, next_proto_neg_len);
  2487. BIO_printf(bio_s_out, "\n");
  2488. }
  2489. #endif
  2490. #ifndef OPENSSL_NO_SRTP
  2491. {
  2492. SRTP_PROTECTION_PROFILE *srtp_profile
  2493. = SSL_get_selected_srtp_profile(con);
  2494. if (srtp_profile)
  2495. BIO_printf(bio_s_out, "SRTP Extension negotiated, profile=%s\n",
  2496. srtp_profile->name);
  2497. }
  2498. #endif
  2499. if (SSL_cache_hit(con))
  2500. BIO_printf(bio_s_out, "Reused session-id\n");
  2501. if (SSL_ctrl(con, SSL_CTRL_GET_FLAGS, 0, NULL) &
  2502. TLS1_FLAGS_TLS_PADDING_BUG)
  2503. BIO_printf(bio_s_out, "Peer has incorrect TLSv1 block padding\n");
  2504. #ifndef OPENSSL_NO_KRB5
  2505. client_princ = kssl_ctx_get0_client_princ(SSL_get0_kssl_ctx(con));
  2506. if (client_princ != NULL) {
  2507. BIO_printf(bio_s_out, "Kerberos peer principal is %s\n",
  2508. client_princ);
  2509. }
  2510. #endif /* OPENSSL_NO_KRB5 */
  2511. BIO_printf(bio_s_out, "Secure Renegotiation IS%s supported\n",
  2512. SSL_get_secure_renegotiation_support(con) ? "" : " NOT");
  2513. if (keymatexportlabel != NULL) {
  2514. BIO_printf(bio_s_out, "Keying material exporter:\n");
  2515. BIO_printf(bio_s_out, " Label: '%s'\n", keymatexportlabel);
  2516. BIO_printf(bio_s_out, " Length: %i bytes\n", keymatexportlen);
  2517. exportedkeymat = OPENSSL_malloc(keymatexportlen);
  2518. if (exportedkeymat != NULL) {
  2519. if (!SSL_export_keying_material(con, exportedkeymat,
  2520. keymatexportlen,
  2521. keymatexportlabel,
  2522. strlen(keymatexportlabel),
  2523. NULL, 0, 0)) {
  2524. BIO_printf(bio_s_out, " Error\n");
  2525. } else {
  2526. BIO_printf(bio_s_out, " Keying material: ");
  2527. for (i = 0; i < keymatexportlen; i++)
  2528. BIO_printf(bio_s_out, "%02X", exportedkeymat[i]);
  2529. BIO_printf(bio_s_out, "\n");
  2530. }
  2531. OPENSSL_free(exportedkeymat);
  2532. }
  2533. }
  2534. return (1);
  2535. }
  2536. #ifndef OPENSSL_NO_DH
  2537. static DH *load_dh_param(const char *dhfile)
  2538. {
  2539. DH *ret = NULL;
  2540. BIO *bio;
  2541. if ((bio = BIO_new_file(dhfile, "r")) == NULL)
  2542. goto err;
  2543. ret = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
  2544. err:
  2545. if (bio != NULL)
  2546. BIO_free(bio);
  2547. return (ret);
  2548. }
  2549. #endif
  2550. #ifndef OPENSSL_NO_KRB5
  2551. char *client_princ;
  2552. #endif
  2553. #if 0
  2554. static int load_CA(SSL_CTX *ctx, char *file)
  2555. {
  2556. FILE *in;
  2557. X509 *x = NULL;
  2558. if ((in = fopen(file, "r")) == NULL)
  2559. return (0);
  2560. for (;;) {
  2561. if (PEM_read_X509(in, &x, NULL) == NULL)
  2562. break;
  2563. SSL_CTX_add_client_CA(ctx, x);
  2564. }
  2565. if (x != NULL)
  2566. X509_free(x);
  2567. fclose(in);
  2568. return (1);
  2569. }
  2570. #endif
  2571. static int www_body(char *hostname, int s, int stype, unsigned char *context)
  2572. {
  2573. char *buf = NULL;
  2574. int ret = 1;
  2575. int i, j, k, dot;
  2576. SSL *con;
  2577. const SSL_CIPHER *c;
  2578. BIO *io, *ssl_bio, *sbio;
  2579. #ifndef OPENSSL_NO_KRB5
  2580. KSSL_CTX *kctx;
  2581. #endif
  2582. buf = OPENSSL_malloc(bufsize);
  2583. if (buf == NULL)
  2584. return (0);
  2585. io = BIO_new(BIO_f_buffer());
  2586. ssl_bio = BIO_new(BIO_f_ssl());
  2587. if ((io == NULL) || (ssl_bio == NULL))
  2588. goto err;
  2589. #ifdef FIONBIO
  2590. if (s_nbio) {
  2591. unsigned long sl = 1;
  2592. if (!s_quiet)
  2593. BIO_printf(bio_err, "turning on non blocking io\n");
  2594. if (BIO_socket_ioctl(s, FIONBIO, &sl) < 0)
  2595. ERR_print_errors(bio_err);
  2596. }
  2597. #endif
  2598. /* lets make the output buffer a reasonable size */
  2599. if (!BIO_set_write_buffer_size(io, bufsize))
  2600. goto err;
  2601. if ((con = SSL_new(ctx)) == NULL)
  2602. goto err;
  2603. #ifndef OPENSSL_NO_TLSEXT
  2604. if (s_tlsextdebug) {
  2605. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  2606. SSL_set_tlsext_debug_arg(con, bio_s_out);
  2607. }
  2608. #endif
  2609. #ifndef OPENSSL_NO_KRB5
  2610. if ((kctx = kssl_ctx_new()) != NULL) {
  2611. kssl_ctx_setstring(kctx, KSSL_SERVICE, KRB5SVC);
  2612. kssl_ctx_setstring(kctx, KSSL_KEYTAB, KRB5KEYTAB);
  2613. }
  2614. #endif /* OPENSSL_NO_KRB5 */
  2615. if (context)
  2616. SSL_set_session_id_context(con, context, strlen((char *)context));
  2617. sbio = BIO_new_socket(s, BIO_NOCLOSE);
  2618. if (s_nbio_test) {
  2619. BIO *test;
  2620. test = BIO_new(BIO_f_nbio_test());
  2621. sbio = BIO_push(test, sbio);
  2622. }
  2623. SSL_set_bio(con, sbio, sbio);
  2624. SSL_set_accept_state(con);
  2625. /* SSL_set_fd(con,s); */
  2626. BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
  2627. BIO_push(io, ssl_bio);
  2628. #ifdef CHARSET_EBCDIC
  2629. io = BIO_push(BIO_new(BIO_f_ebcdic_filter()), io);
  2630. #endif
  2631. if (s_debug) {
  2632. SSL_set_debug(con, 1);
  2633. BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
  2634. BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
  2635. }
  2636. if (s_msg) {
  2637. #ifndef OPENSSL_NO_SSL_TRACE
  2638. if (s_msg == 2)
  2639. SSL_set_msg_callback(con, SSL_trace);
  2640. else
  2641. #endif
  2642. SSL_set_msg_callback(con, msg_cb);
  2643. SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
  2644. }
  2645. for (;;) {
  2646. if (hack) {
  2647. i = SSL_accept(con);
  2648. #ifndef OPENSSL_NO_SRP
  2649. while (i <= 0
  2650. && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) {
  2651. BIO_printf(bio_s_out, "LOOKUP during accept %s\n",
  2652. srp_callback_parm.login);
  2653. SRP_user_pwd_free(srp_callback_parm.user);
  2654. srp_callback_parm.user =
  2655. SRP_VBASE_get1_by_user(srp_callback_parm.vb,
  2656. srp_callback_parm.login);
  2657. if (srp_callback_parm.user)
  2658. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  2659. srp_callback_parm.user->info);
  2660. else
  2661. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  2662. i = SSL_accept(con);
  2663. }
  2664. #endif
  2665. switch (SSL_get_error(con, i)) {
  2666. case SSL_ERROR_NONE:
  2667. break;
  2668. case SSL_ERROR_WANT_WRITE:
  2669. case SSL_ERROR_WANT_READ:
  2670. case SSL_ERROR_WANT_X509_LOOKUP:
  2671. continue;
  2672. case SSL_ERROR_SYSCALL:
  2673. case SSL_ERROR_SSL:
  2674. case SSL_ERROR_ZERO_RETURN:
  2675. ret = 1;
  2676. goto err;
  2677. /* break; */
  2678. }
  2679. SSL_renegotiate(con);
  2680. SSL_write(con, NULL, 0);
  2681. }
  2682. i = BIO_gets(io, buf, bufsize - 1);
  2683. if (i < 0) { /* error */
  2684. if (!BIO_should_retry(io)) {
  2685. if (!s_quiet)
  2686. ERR_print_errors(bio_err);
  2687. goto err;
  2688. } else {
  2689. BIO_printf(bio_s_out, "read R BLOCK\n");
  2690. #ifndef OPENSSL_NO_SRP
  2691. if (BIO_should_io_special(io)
  2692. && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
  2693. BIO_printf(bio_s_out, "LOOKUP renego during read\n");
  2694. SRP_user_pwd_free(srp_callback_parm.user);
  2695. srp_callback_parm.user =
  2696. SRP_VBASE_get1_by_user(srp_callback_parm.vb,
  2697. srp_callback_parm.login);
  2698. if (srp_callback_parm.user)
  2699. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  2700. srp_callback_parm.user->info);
  2701. else
  2702. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  2703. continue;
  2704. }
  2705. #endif
  2706. #if defined(OPENSSL_SYS_NETWARE)
  2707. delay(1000);
  2708. #elif !defined(OPENSSL_SYS_MSDOS) && !defined(__DJGPP__)
  2709. sleep(1);
  2710. #endif
  2711. continue;
  2712. }
  2713. } else if (i == 0) { /* end of input */
  2714. ret = 1;
  2715. goto end;
  2716. }
  2717. /* else we have data */
  2718. if (((www == 1) && (strncmp("GET ", buf, 4) == 0)) ||
  2719. ((www == 2) && (strncmp("GET /stats ", buf, 11) == 0))) {
  2720. char *p;
  2721. X509 *peer;
  2722. STACK_OF(SSL_CIPHER) *sk;
  2723. static const char *space = " ";
  2724. BIO_puts(io,
  2725. "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  2726. BIO_puts(io, "<HTML><BODY BGCOLOR=\"#ffffff\">\n");
  2727. BIO_puts(io, "<pre>\n");
  2728. /* BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/
  2729. BIO_puts(io, "\n");
  2730. for (i = 0; i < local_argc; i++) {
  2731. BIO_puts(io, local_argv[i]);
  2732. BIO_write(io, " ", 1);
  2733. }
  2734. BIO_puts(io, "\n");
  2735. BIO_printf(io,
  2736. "Secure Renegotiation IS%s supported\n",
  2737. SSL_get_secure_renegotiation_support(con) ?
  2738. "" : " NOT");
  2739. /*
  2740. * The following is evil and should not really be done
  2741. */
  2742. BIO_printf(io, "Ciphers supported in s_server binary\n");
  2743. sk = SSL_get_ciphers(con);
  2744. j = sk_SSL_CIPHER_num(sk);
  2745. for (i = 0; i < j; i++) {
  2746. c = sk_SSL_CIPHER_value(sk, i);
  2747. BIO_printf(io, "%-11s:%-25s",
  2748. SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
  2749. if ((((i + 1) % 2) == 0) && (i + 1 != j))
  2750. BIO_puts(io, "\n");
  2751. }
  2752. BIO_puts(io, "\n");
  2753. p = SSL_get_shared_ciphers(con, buf, bufsize);
  2754. if (p != NULL) {
  2755. BIO_printf(io,
  2756. "---\nCiphers common between both SSL end points:\n");
  2757. j = i = 0;
  2758. while (*p) {
  2759. if (*p == ':') {
  2760. BIO_write(io, space, 26 - j);
  2761. i++;
  2762. j = 0;
  2763. BIO_write(io, ((i % 3) ? " " : "\n"), 1);
  2764. } else {
  2765. BIO_write(io, p, 1);
  2766. j++;
  2767. }
  2768. p++;
  2769. }
  2770. BIO_puts(io, "\n");
  2771. }
  2772. ssl_print_sigalgs(io, con);
  2773. #ifndef OPENSSL_NO_EC
  2774. ssl_print_curves(io, con, 0);
  2775. #endif
  2776. BIO_printf(io, (SSL_cache_hit(con)
  2777. ? "---\nReused, " : "---\nNew, "));
  2778. c = SSL_get_current_cipher(con);
  2779. BIO_printf(io, "%s, Cipher is %s\n",
  2780. SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
  2781. SSL_SESSION_print(io, SSL_get_session(con));
  2782. BIO_printf(io, "---\n");
  2783. print_stats(io, SSL_get_SSL_CTX(con));
  2784. BIO_printf(io, "---\n");
  2785. peer = SSL_get_peer_certificate(con);
  2786. if (peer != NULL) {
  2787. BIO_printf(io, "Client certificate\n");
  2788. X509_print(io, peer);
  2789. PEM_write_bio_X509(io, peer);
  2790. } else
  2791. BIO_puts(io, "no client certificate available\n");
  2792. BIO_puts(io, "</BODY></HTML>\r\n\r\n");
  2793. break;
  2794. } else if ((www == 2 || www == 3)
  2795. && (strncmp("GET /", buf, 5) == 0)) {
  2796. BIO *file;
  2797. char *p, *e;
  2798. static const char *text =
  2799. "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
  2800. /* skip the '/' */
  2801. p = &(buf[5]);
  2802. dot = 1;
  2803. for (e = p; *e != '\0'; e++) {
  2804. if (e[0] == ' ')
  2805. break;
  2806. switch (dot) {
  2807. case 1:
  2808. dot = (e[0] == '.') ? 2 : 0;
  2809. break;
  2810. case 2:
  2811. dot = (e[0] == '.') ? 3 : 0;
  2812. break;
  2813. case 3:
  2814. dot = (e[0] == '/') ? -1 : 0;
  2815. break;
  2816. }
  2817. if (dot == 0)
  2818. dot = (e[0] == '/') ? 1 : 0;
  2819. }
  2820. dot = (dot == 3) || (dot == -1); /* filename contains ".."
  2821. * component */
  2822. if (*e == '\0') {
  2823. BIO_puts(io, text);
  2824. BIO_printf(io, "'%s' is an invalid file name\r\n", p);
  2825. break;
  2826. }
  2827. *e = '\0';
  2828. if (dot) {
  2829. BIO_puts(io, text);
  2830. BIO_printf(io, "'%s' contains '..' reference\r\n", p);
  2831. break;
  2832. }
  2833. if (*p == '/') {
  2834. BIO_puts(io, text);
  2835. BIO_printf(io, "'%s' is an invalid path\r\n", p);
  2836. break;
  2837. }
  2838. #if 0
  2839. /* append if a directory lookup */
  2840. if (e[-1] == '/')
  2841. strcat(p, "index.html");
  2842. #endif
  2843. /* if a directory, do the index thang */
  2844. if (app_isdir(p) > 0) {
  2845. #if 0 /* must check buffer size */
  2846. strcat(p, "/index.html");
  2847. #else
  2848. BIO_puts(io, text);
  2849. BIO_printf(io, "'%s' is a directory\r\n", p);
  2850. break;
  2851. #endif
  2852. }
  2853. if ((file = BIO_new_file(p, "r")) == NULL) {
  2854. BIO_puts(io, text);
  2855. BIO_printf(io, "Error opening '%s'\r\n", p);
  2856. ERR_print_errors(io);
  2857. break;
  2858. }
  2859. if (!s_quiet)
  2860. BIO_printf(bio_err, "FILE:%s\n", p);
  2861. if (www == 2) {
  2862. i = strlen(p);
  2863. if (((i > 5) && (strcmp(&(p[i - 5]), ".html") == 0)) ||
  2864. ((i > 4) && (strcmp(&(p[i - 4]), ".php") == 0)) ||
  2865. ((i > 4) && (strcmp(&(p[i - 4]), ".htm") == 0)))
  2866. BIO_puts(io,
  2867. "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
  2868. else
  2869. BIO_puts(io,
  2870. "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
  2871. }
  2872. /* send the file */
  2873. for (;;) {
  2874. i = BIO_read(file, buf, bufsize);
  2875. if (i <= 0)
  2876. break;
  2877. #ifdef RENEG
  2878. total_bytes += i;
  2879. fprintf(stderr, "%d\n", i);
  2880. if (total_bytes > 3 * 1024) {
  2881. total_bytes = 0;
  2882. fprintf(stderr, "RENEGOTIATE\n");
  2883. SSL_renegotiate(con);
  2884. }
  2885. #endif
  2886. for (j = 0; j < i;) {
  2887. #ifdef RENEG
  2888. {
  2889. static count = 0;
  2890. if (++count == 13) {
  2891. SSL_renegotiate(con);
  2892. }
  2893. }
  2894. #endif
  2895. k = BIO_write(io, &(buf[j]), i - j);
  2896. if (k <= 0) {
  2897. if (!BIO_should_retry(io))
  2898. goto write_error;
  2899. else {
  2900. BIO_printf(bio_s_out, "rwrite W BLOCK\n");
  2901. }
  2902. } else {
  2903. j += k;
  2904. }
  2905. }
  2906. }
  2907. write_error:
  2908. BIO_free(file);
  2909. break;
  2910. }
  2911. }
  2912. for (;;) {
  2913. i = (int)BIO_flush(io);
  2914. if (i <= 0) {
  2915. if (!BIO_should_retry(io))
  2916. break;
  2917. } else
  2918. break;
  2919. }
  2920. end:
  2921. #if 1
  2922. /* make sure we re-use sessions */
  2923. SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  2924. #else
  2925. /* This kills performance */
  2926. /*
  2927. * SSL_shutdown(con); A shutdown gets sent in the BIO_free_all(io)
  2928. * procession
  2929. */
  2930. #endif
  2931. err:
  2932. if (ret >= 0)
  2933. BIO_printf(bio_s_out, "ACCEPT\n");
  2934. if (buf != NULL)
  2935. OPENSSL_free(buf);
  2936. if (io != NULL)
  2937. BIO_free_all(io);
  2938. /* if (ssl_bio != NULL) BIO_free(ssl_bio);*/
  2939. return (ret);
  2940. }
  2941. static int rev_body(char *hostname, int s, int stype, unsigned char *context)
  2942. {
  2943. char *buf = NULL;
  2944. int i;
  2945. int ret = 1;
  2946. SSL *con;
  2947. BIO *io, *ssl_bio, *sbio;
  2948. #ifndef OPENSSL_NO_KRB5
  2949. KSSL_CTX *kctx;
  2950. #endif
  2951. buf = OPENSSL_malloc(bufsize);
  2952. if (buf == NULL)
  2953. return (0);
  2954. io = BIO_new(BIO_f_buffer());
  2955. ssl_bio = BIO_new(BIO_f_ssl());
  2956. if ((io == NULL) || (ssl_bio == NULL))
  2957. goto err;
  2958. /* lets make the output buffer a reasonable size */
  2959. if (!BIO_set_write_buffer_size(io, bufsize))
  2960. goto err;
  2961. if ((con = SSL_new(ctx)) == NULL)
  2962. goto err;
  2963. #ifndef OPENSSL_NO_TLSEXT
  2964. if (s_tlsextdebug) {
  2965. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  2966. SSL_set_tlsext_debug_arg(con, bio_s_out);
  2967. }
  2968. #endif
  2969. #ifndef OPENSSL_NO_KRB5
  2970. if ((kctx = kssl_ctx_new()) != NULL) {
  2971. kssl_ctx_setstring(kctx, KSSL_SERVICE, KRB5SVC);
  2972. kssl_ctx_setstring(kctx, KSSL_KEYTAB, KRB5KEYTAB);
  2973. }
  2974. #endif /* OPENSSL_NO_KRB5 */
  2975. if (context)
  2976. SSL_set_session_id_context(con, context, strlen((char *)context));
  2977. sbio = BIO_new_socket(s, BIO_NOCLOSE);
  2978. SSL_set_bio(con, sbio, sbio);
  2979. SSL_set_accept_state(con);
  2980. BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
  2981. BIO_push(io, ssl_bio);
  2982. #ifdef CHARSET_EBCDIC
  2983. io = BIO_push(BIO_new(BIO_f_ebcdic_filter()), io);
  2984. #endif
  2985. if (s_debug) {
  2986. SSL_set_debug(con, 1);
  2987. BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
  2988. BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
  2989. }
  2990. if (s_msg) {
  2991. #ifndef OPENSSL_NO_SSL_TRACE
  2992. if (s_msg == 2)
  2993. SSL_set_msg_callback(con, SSL_trace);
  2994. else
  2995. #endif
  2996. SSL_set_msg_callback(con, msg_cb);
  2997. SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
  2998. }
  2999. for (;;) {
  3000. i = BIO_do_handshake(io);
  3001. if (i > 0)
  3002. break;
  3003. if (!BIO_should_retry(io)) {
  3004. BIO_puts(bio_err, "CONNECTION FAILURE\n");
  3005. ERR_print_errors(bio_err);
  3006. goto end;
  3007. }
  3008. #ifndef OPENSSL_NO_SRP
  3009. if (BIO_should_io_special(io)
  3010. && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
  3011. BIO_printf(bio_s_out, "LOOKUP renego during accept\n");
  3012. SRP_user_pwd_free(srp_callback_parm.user);
  3013. srp_callback_parm.user =
  3014. SRP_VBASE_get1_by_user(srp_callback_parm.vb,
  3015. srp_callback_parm.login);
  3016. if (srp_callback_parm.user)
  3017. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  3018. srp_callback_parm.user->info);
  3019. else
  3020. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  3021. continue;
  3022. }
  3023. #endif
  3024. }
  3025. BIO_printf(bio_err, "CONNECTION ESTABLISHED\n");
  3026. print_ssl_summary(bio_err, con);
  3027. for (;;) {
  3028. i = BIO_gets(io, buf, bufsize - 1);
  3029. if (i < 0) { /* error */
  3030. if (!BIO_should_retry(io)) {
  3031. if (!s_quiet)
  3032. ERR_print_errors(bio_err);
  3033. goto err;
  3034. } else {
  3035. BIO_printf(bio_s_out, "read R BLOCK\n");
  3036. #ifndef OPENSSL_NO_SRP
  3037. if (BIO_should_io_special(io)
  3038. && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
  3039. BIO_printf(bio_s_out, "LOOKUP renego during read\n");
  3040. SRP_user_pwd_free(srp_callback_parm.user);
  3041. srp_callback_parm.user =
  3042. SRP_VBASE_get1_by_user(srp_callback_parm.vb,
  3043. srp_callback_parm.login);
  3044. if (srp_callback_parm.user)
  3045. BIO_printf(bio_s_out, "LOOKUP done %s\n",
  3046. srp_callback_parm.user->info);
  3047. else
  3048. BIO_printf(bio_s_out, "LOOKUP not successful\n");
  3049. continue;
  3050. }
  3051. #endif
  3052. #if defined(OPENSSL_SYS_NETWARE)
  3053. delay(1000);
  3054. #elif !defined(OPENSSL_SYS_MSDOS) && !defined(__DJGPP__)
  3055. sleep(1);
  3056. #endif
  3057. continue;
  3058. }
  3059. } else if (i == 0) { /* end of input */
  3060. ret = 1;
  3061. BIO_printf(bio_err, "CONNECTION CLOSED\n");
  3062. goto end;
  3063. } else {
  3064. char *p = buf + i - 1;
  3065. while (i && (*p == '\n' || *p == '\r')) {
  3066. p--;
  3067. i--;
  3068. }
  3069. if (!s_ign_eof && i == 5 && !strncmp(buf, "CLOSE", 5)) {
  3070. ret = 1;
  3071. BIO_printf(bio_err, "CONNECTION CLOSED\n");
  3072. goto end;
  3073. }
  3074. BUF_reverse((unsigned char *)buf, NULL, i);
  3075. buf[i] = '\n';
  3076. BIO_write(io, buf, i + 1);
  3077. for (;;) {
  3078. i = BIO_flush(io);
  3079. if (i > 0)
  3080. break;
  3081. if (!BIO_should_retry(io))
  3082. goto end;
  3083. }
  3084. }
  3085. }
  3086. end:
  3087. /* make sure we re-use sessions */
  3088. SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
  3089. err:
  3090. if (buf != NULL)
  3091. OPENSSL_free(buf);
  3092. if (io != NULL)
  3093. BIO_free_all(io);
  3094. return (ret);
  3095. }
  3096. #ifndef OPENSSL_NO_RSA
  3097. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
  3098. {
  3099. BIGNUM *bn = NULL;
  3100. static RSA *rsa_tmp = NULL;
  3101. if (!rsa_tmp && ((bn = BN_new()) == NULL))
  3102. BIO_printf(bio_err, "Allocation error in generating RSA key\n");
  3103. if (!rsa_tmp && bn) {
  3104. if (!s_quiet) {
  3105. BIO_printf(bio_err, "Generating temp (%d bit) RSA key...",
  3106. keylength);
  3107. (void)BIO_flush(bio_err);
  3108. }
  3109. if (!BN_set_word(bn, RSA_F4) || ((rsa_tmp = RSA_new()) == NULL) ||
  3110. !RSA_generate_key_ex(rsa_tmp, keylength, bn, NULL)) {
  3111. if (rsa_tmp)
  3112. RSA_free(rsa_tmp);
  3113. rsa_tmp = NULL;
  3114. }
  3115. if (!s_quiet) {
  3116. BIO_printf(bio_err, "\n");
  3117. (void)BIO_flush(bio_err);
  3118. }
  3119. BN_free(bn);
  3120. }
  3121. return (rsa_tmp);
  3122. }
  3123. #endif
  3124. #define MAX_SESSION_ID_ATTEMPTS 10
  3125. static int generate_session_id(const SSL *ssl, unsigned char *id,
  3126. unsigned int *id_len)
  3127. {
  3128. unsigned int count = 0;
  3129. do {
  3130. if (RAND_pseudo_bytes(id, *id_len) < 0)
  3131. return 0;
  3132. /*
  3133. * Prefix the session_id with the required prefix. NB: If our prefix
  3134. * is too long, clip it - but there will be worse effects anyway, eg.
  3135. * the server could only possibly create 1 session ID (ie. the
  3136. * prefix!) so all future session negotiations will fail due to
  3137. * conflicts.
  3138. */
  3139. memcpy(id, session_id_prefix,
  3140. (strlen(session_id_prefix) < *id_len) ?
  3141. strlen(session_id_prefix) : *id_len);
  3142. }
  3143. while (SSL_has_matching_session_id(ssl, id, *id_len) &&
  3144. (++count < MAX_SESSION_ID_ATTEMPTS));
  3145. if (count >= MAX_SESSION_ID_ATTEMPTS)
  3146. return 0;
  3147. return 1;
  3148. }
  3149. /*
  3150. * By default s_server uses an in-memory cache which caches SSL_SESSION
  3151. * structures without any serialisation. This hides some bugs which only
  3152. * become apparent in deployed servers. By implementing a basic external
  3153. * session cache some issues can be debugged using s_server.
  3154. */
  3155. typedef struct simple_ssl_session_st {
  3156. unsigned char *id;
  3157. unsigned int idlen;
  3158. unsigned char *der;
  3159. int derlen;
  3160. struct simple_ssl_session_st *next;
  3161. } simple_ssl_session;
  3162. static simple_ssl_session *first = NULL;
  3163. static int add_session(SSL *ssl, SSL_SESSION *session)
  3164. {
  3165. simple_ssl_session *sess;
  3166. unsigned char *p;
  3167. sess = OPENSSL_malloc(sizeof(simple_ssl_session));
  3168. if (!sess) {
  3169. BIO_printf(bio_err, "Out of memory adding session to external cache\n");
  3170. return 0;
  3171. }
  3172. SSL_SESSION_get_id(session, &sess->idlen);
  3173. sess->derlen = i2d_SSL_SESSION(session, NULL);
  3174. sess->id = BUF_memdup(SSL_SESSION_get_id(session, NULL), sess->idlen);
  3175. sess->der = OPENSSL_malloc(sess->derlen);
  3176. if (!sess->id || !sess->der) {
  3177. BIO_printf(bio_err, "Out of memory adding session to external cache\n");
  3178. if (sess->id)
  3179. OPENSSL_free(sess->id);
  3180. if (sess->der)
  3181. OPENSSL_free(sess->der);
  3182. OPENSSL_free(sess);
  3183. return 0;
  3184. }
  3185. p = sess->der;
  3186. i2d_SSL_SESSION(session, &p);
  3187. sess->next = first;
  3188. first = sess;
  3189. BIO_printf(bio_err, "New session added to external cache\n");
  3190. return 0;
  3191. }
  3192. static SSL_SESSION *get_session(SSL *ssl, unsigned char *id, int idlen,
  3193. int *do_copy)
  3194. {
  3195. simple_ssl_session *sess;
  3196. *do_copy = 0;
  3197. for (sess = first; sess; sess = sess->next) {
  3198. if (idlen == (int)sess->idlen && !memcmp(sess->id, id, idlen)) {
  3199. const unsigned char *p = sess->der;
  3200. BIO_printf(bio_err, "Lookup session: cache hit\n");
  3201. return d2i_SSL_SESSION(NULL, &p, sess->derlen);
  3202. }
  3203. }
  3204. BIO_printf(bio_err, "Lookup session: cache miss\n");
  3205. return NULL;
  3206. }
  3207. static void del_session(SSL_CTX *sctx, SSL_SESSION *session)
  3208. {
  3209. simple_ssl_session *sess, *prev = NULL;
  3210. const unsigned char *id;
  3211. unsigned int idlen;
  3212. id = SSL_SESSION_get_id(session, &idlen);
  3213. for (sess = first; sess; sess = sess->next) {
  3214. if (idlen == sess->idlen && !memcmp(sess->id, id, idlen)) {
  3215. if (prev)
  3216. prev->next = sess->next;
  3217. else
  3218. first = sess->next;
  3219. OPENSSL_free(sess->id);
  3220. OPENSSL_free(sess->der);
  3221. OPENSSL_free(sess);
  3222. return;
  3223. }
  3224. prev = sess;
  3225. }
  3226. }
  3227. static void init_session_cache_ctx(SSL_CTX *sctx)
  3228. {
  3229. SSL_CTX_set_session_cache_mode(sctx,
  3230. SSL_SESS_CACHE_NO_INTERNAL |
  3231. SSL_SESS_CACHE_SERVER);
  3232. SSL_CTX_sess_set_new_cb(sctx, add_session);
  3233. SSL_CTX_sess_set_get_cb(sctx, get_session);
  3234. SSL_CTX_sess_set_remove_cb(sctx, del_session);
  3235. }
  3236. static void free_sessions(void)
  3237. {
  3238. simple_ssl_session *sess, *tsess;
  3239. for (sess = first; sess;) {
  3240. OPENSSL_free(sess->id);
  3241. OPENSSL_free(sess->der);
  3242. tsess = sess;
  3243. sess = sess->next;
  3244. OPENSSL_free(tsess);
  3245. }
  3246. first = NULL;
  3247. }