s_server.c 96 KB

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