ssl_server2.c 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258
  1. /*
  2. * SSL client with options
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_PLATFORM_C)
  52. #include "mbedtls/platform.h"
  53. #else
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #define mbedtls_free free
  57. #define mbedtls_time time
  58. #define mbedtls_time_t time_t
  59. #define mbedtls_calloc calloc
  60. #define mbedtls_fprintf fprintf
  61. #define mbedtls_printf printf
  62. #define mbedtls_exit exit
  63. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  64. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  65. #endif
  66. #if !defined(MBEDTLS_ENTROPY_C) || \
  67. !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_SRV_C) || \
  68. !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
  69. int main( void )
  70. {
  71. mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
  72. "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
  73. "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
  74. mbedtls_exit( 0 );
  75. }
  76. #else
  77. #include "mbedtls/net_sockets.h"
  78. #include "mbedtls/ssl.h"
  79. #include "mbedtls/entropy.h"
  80. #include "mbedtls/ctr_drbg.h"
  81. #include "mbedtls/certs.h"
  82. #include "mbedtls/x509.h"
  83. #include "mbedtls/error.h"
  84. #include "mbedtls/debug.h"
  85. #include "mbedtls/timing.h"
  86. #include <stdio.h>
  87. #include <stdlib.h>
  88. #include <string.h>
  89. #include <stdint.h>
  90. #if !defined(_MSC_VER)
  91. #include <inttypes.h>
  92. #endif
  93. #if !defined(_WIN32)
  94. #include <signal.h>
  95. #endif
  96. #if defined(MBEDTLS_SSL_CACHE_C)
  97. #include "mbedtls/ssl_cache.h"
  98. #endif
  99. #if defined(MBEDTLS_SSL_TICKET_C)
  100. #include "mbedtls/ssl_ticket.h"
  101. #endif
  102. #if defined(MBEDTLS_SSL_COOKIE_C)
  103. #include "mbedtls/ssl_cookie.h"
  104. #endif
  105. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  106. #include "mbedtls/memory_buffer_alloc.h"
  107. #endif
  108. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_FS_IO)
  109. #define SNI_OPTION
  110. #endif
  111. #if defined(_WIN32)
  112. #include <windows.h>
  113. #endif
  114. /* Size of memory to be allocated for the heap, when using the library's memory
  115. * management and MBEDTLS_MEMORY_BUFFER_ALLOC_C is enabled. */
  116. #define MEMORY_HEAP_SIZE 120000
  117. #define DFL_SERVER_ADDR NULL
  118. #define DFL_SERVER_PORT "4433"
  119. #define DFL_RESPONSE_SIZE -1
  120. #define DFL_DEBUG_LEVEL 0
  121. #define DFL_NBIO 0
  122. #define DFL_EVENT 0
  123. #define DFL_READ_TIMEOUT 0
  124. #define DFL_CA_FILE ""
  125. #define DFL_CA_PATH ""
  126. #define DFL_CRT_FILE ""
  127. #define DFL_KEY_FILE ""
  128. #define DFL_CRT_FILE2 ""
  129. #define DFL_KEY_FILE2 ""
  130. #define DFL_ASYNC_OPERATIONS "-"
  131. #define DFL_ASYNC_PRIVATE_DELAY1 ( -1 )
  132. #define DFL_ASYNC_PRIVATE_DELAY2 ( -1 )
  133. #define DFL_ASYNC_PRIVATE_ERROR ( 0 )
  134. #define DFL_PSK ""
  135. #define DFL_PSK_IDENTITY "Client_identity"
  136. #define DFL_ECJPAKE_PW NULL
  137. #define DFL_PSK_LIST NULL
  138. #define DFL_FORCE_CIPHER 0
  139. #define DFL_VERSION_SUITES NULL
  140. #define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
  141. #define DFL_ALLOW_LEGACY -2
  142. #define DFL_RENEGOTIATE 0
  143. #define DFL_RENEGO_DELAY -2
  144. #define DFL_RENEGO_PERIOD ( (uint64_t)-1 )
  145. #define DFL_EXCHANGES 1
  146. #define DFL_MIN_VERSION -1
  147. #define DFL_MAX_VERSION -1
  148. #define DFL_ARC4 -1
  149. #define DFL_SHA1 -1
  150. #define DFL_AUTH_MODE -1
  151. #define DFL_CERT_REQ_CA_LIST MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED
  152. #define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
  153. #define DFL_TRUNC_HMAC -1
  154. #define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
  155. #define DFL_TICKET_TIMEOUT 86400
  156. #define DFL_CACHE_MAX -1
  157. #define DFL_CACHE_TIMEOUT -1
  158. #define DFL_SNI NULL
  159. #define DFL_ALPN_STRING NULL
  160. #define DFL_CURVES NULL
  161. #define DFL_DHM_FILE NULL
  162. #define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
  163. #define DFL_COOKIES 1
  164. #define DFL_ANTI_REPLAY -1
  165. #define DFL_HS_TO_MIN 0
  166. #define DFL_HS_TO_MAX 0
  167. #define DFL_DTLS_MTU -1
  168. #define DFL_BADMAC_LIMIT -1
  169. #define DFL_DGRAM_PACKING 1
  170. #define DFL_EXTENDED_MS -1
  171. #define DFL_ETM -1
  172. #define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  173. "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  174. "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  175. "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  176. "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  177. "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  178. "07-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah</p>\r\n"
  179. /* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
  180. * packets (for fragmentation purposes) */
  181. #define HTTP_RESPONSE \
  182. "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
  183. "<h2>mbed TLS Test Server</h2>\r\n" \
  184. "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
  185. /*
  186. * Size of the basic I/O buffer. Able to hold our default response.
  187. *
  188. * You will need to adapt the mbedtls_ssl_get_bytes_avail() test in ssl-opt.sh
  189. * if you change this value to something outside the range <= 100 or > 500
  190. */
  191. #define DFL_IO_BUF_LEN 200
  192. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  193. #if defined(MBEDTLS_FS_IO)
  194. #define USAGE_IO \
  195. " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
  196. " default: \"\" (pre-loaded)\n" \
  197. " use \"none\" to skip loading any top-level CAs.\n" \
  198. " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
  199. " default: \"\" (pre-loaded) (overrides ca_file)\n" \
  200. " use \"none\" to skip loading any top-level CAs.\n" \
  201. " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
  202. " default: see note after key_file2\n" \
  203. " key_file=%%s default: see note after key_file2\n" \
  204. " crt_file2=%%s Your second cert and chain (in bottom to top order, top may be omitted)\n" \
  205. " default: see note after key_file2\n" \
  206. " key_file2=%%s default: see note below\n" \
  207. " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
  208. " preloaded certificate(s) and key(s) are used if available\n" \
  209. " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
  210. " default: preloaded parameters\n"
  211. #else
  212. #define USAGE_IO \
  213. "\n" \
  214. " No file operations available (MBEDTLS_FS_IO not defined)\n" \
  215. "\n"
  216. #endif /* MBEDTLS_FS_IO */
  217. #else
  218. #define USAGE_IO ""
  219. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  220. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  221. #define USAGE_SSL_ASYNC \
  222. " async_operations=%%c... d=decrypt, s=sign (default: -=off)\n" \
  223. " async_private_delay1=%%d Asynchronous delay for key_file or preloaded key\n" \
  224. " async_private_delay2=%%d Asynchronous delay for key_file2 and sni\n" \
  225. " default: -1 (not asynchronous)\n" \
  226. " async_private_error=%%d Async callback error injection (default=0=none,\n" \
  227. " 1=start, 2=cancel, 3=resume, negative=first time only)"
  228. #else
  229. #define USAGE_SSL_ASYNC ""
  230. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  231. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  232. #define USAGE_PSK \
  233. " psk=%%s default: \"\" (in hex, without 0x)\n" \
  234. " psk_list=%%s default: \"\"\n" \
  235. " A list of (PSK identity, PSK value) pairs.\n" \
  236. " The PSK values are in hex, without 0x.\n" \
  237. " id1,psk1[,id2,psk2[,...]]\n" \
  238. " psk_identity=%%s default: \"Client_identity\"\n"
  239. #else
  240. #define USAGE_PSK ""
  241. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  242. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  243. #define USAGE_TICKETS \
  244. " tickets=%%d default: 1 (enabled)\n" \
  245. " ticket_timeout=%%d default: 86400 (one day)\n"
  246. #else
  247. #define USAGE_TICKETS ""
  248. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  249. #if defined(MBEDTLS_SSL_CACHE_C)
  250. #define USAGE_CACHE \
  251. " cache_max=%%d default: cache default (50)\n" \
  252. " cache_timeout=%%d default: cache default (1d)\n"
  253. #else
  254. #define USAGE_CACHE ""
  255. #endif /* MBEDTLS_SSL_CACHE_C */
  256. #if defined(SNI_OPTION)
  257. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  258. #define SNI_CRL ",crl"
  259. #else
  260. #define SNI_CRL ""
  261. #endif
  262. #define USAGE_SNI \
  263. " sni=%%s name1,cert1,key1,ca1"SNI_CRL",auth1[,...]\n" \
  264. " default: disabled\n"
  265. #else
  266. #define USAGE_SNI ""
  267. #endif /* SNI_OPTION */
  268. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  269. #define USAGE_MAX_FRAG_LEN \
  270. " max_frag_len=%%d default: 16384 (tls default)\n" \
  271. " options: 512, 1024, 2048, 4096\n"
  272. #else
  273. #define USAGE_MAX_FRAG_LEN ""
  274. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  275. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  276. #define USAGE_TRUNC_HMAC \
  277. " trunc_hmac=%%d default: library default\n"
  278. #else
  279. #define USAGE_TRUNC_HMAC ""
  280. #endif
  281. #if defined(MBEDTLS_SSL_ALPN)
  282. #define USAGE_ALPN \
  283. " alpn=%%s default: \"\" (disabled)\n" \
  284. " example: spdy/1,http/1.1\n"
  285. #else
  286. #define USAGE_ALPN ""
  287. #endif /* MBEDTLS_SSL_ALPN */
  288. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  289. #define USAGE_COOKIES \
  290. " cookies=0/1/-1 default: 1 (enabled)\n" \
  291. " 0: disabled, -1: library default (broken)\n"
  292. #else
  293. #define USAGE_COOKIES ""
  294. #endif
  295. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  296. #define USAGE_ANTI_REPLAY \
  297. " anti_replay=0/1 default: (library default: enabled)\n"
  298. #else
  299. #define USAGE_ANTI_REPLAY ""
  300. #endif
  301. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  302. #define USAGE_BADMAC_LIMIT \
  303. " badmac_limit=%%d default: (library default: disabled)\n"
  304. #else
  305. #define USAGE_BADMAC_LIMIT ""
  306. #endif
  307. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  308. #define USAGE_DTLS \
  309. " dtls=%%d default: 0 (TLS)\n" \
  310. " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
  311. " range of DTLS handshake timeouts in millisecs\n" \
  312. " mtu=%%d default: (library default: unlimited)\n" \
  313. " dgram_packing=%%d default: 1 (allowed)\n" \
  314. " allow or forbid packing of multiple\n" \
  315. " records within a single datgram.\n"
  316. #else
  317. #define USAGE_DTLS ""
  318. #endif
  319. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  320. #define USAGE_EMS \
  321. " extended_ms=0/1 default: (library default: on)\n"
  322. #else
  323. #define USAGE_EMS ""
  324. #endif
  325. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  326. #define USAGE_ETM \
  327. " etm=0/1 default: (library default: on)\n"
  328. #else
  329. #define USAGE_ETM ""
  330. #endif
  331. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  332. #define USAGE_RENEGO \
  333. " renegotiation=%%d default: 0 (disabled)\n" \
  334. " renegotiate=%%d default: 0 (disabled)\n" \
  335. " renego_delay=%%d default: -2 (library default)\n" \
  336. " renego_period=%%d default: (2^64 - 1 for TLS, 2^48 - 1 for DTLS)\n"
  337. #else
  338. #define USAGE_RENEGO ""
  339. #endif
  340. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  341. #define USAGE_ECJPAKE \
  342. " ecjpake_pw=%%s default: none (disabled)\n"
  343. #else
  344. #define USAGE_ECJPAKE ""
  345. #endif
  346. #if defined(MBEDTLS_ECP_C)
  347. #define USAGE_CURVES \
  348. " curves=a,b,c,d default: \"default\" (library default)\n" \
  349. " example: \"secp521r1,brainpoolP512r1\"\n" \
  350. " - use \"none\" for empty list\n" \
  351. " - see mbedtls_ecp_curve_list()\n" \
  352. " for acceptable curve names\n"
  353. #else
  354. #define USAGE_CURVES ""
  355. #endif
  356. /* USAGE is arbitrarily split to stay under the portable string literal
  357. * length limit: 4095 bytes in C99. */
  358. #define USAGE1 \
  359. "\n usage: ssl_server2 param=<>...\n" \
  360. "\n acceptable parameters:\n" \
  361. " server_addr=%%s default: (all interfaces)\n" \
  362. " server_port=%%d default: 4433\n" \
  363. " debug_level=%%d default: 0 (disabled)\n" \
  364. " buffer_size=%%d default: 200 \n" \
  365. " (minimum: 1, max: 16385)\n" \
  366. " response_size=%%d default: about 152 (basic response)\n" \
  367. " (minimum: 0, max: 16384)\n" \
  368. " increases buffer_size if bigger\n"\
  369. " nbio=%%d default: 0 (blocking I/O)\n" \
  370. " options: 1 (non-blocking), 2 (added delays)\n" \
  371. " event=%%d default: 0 (loop)\n" \
  372. " options: 1 (level-triggered, implies nbio=1),\n" \
  373. " read_timeout=%%d default: 0 ms (no timeout)\n" \
  374. "\n" \
  375. USAGE_DTLS \
  376. USAGE_COOKIES \
  377. USAGE_ANTI_REPLAY \
  378. USAGE_BADMAC_LIMIT \
  379. "\n"
  380. #define USAGE2 \
  381. " auth_mode=%%s default: (library default: none)\n" \
  382. " options: none, optional, required\n" \
  383. " cert_req_ca_list=%%d default: 1 (send ca list)\n" \
  384. " options: 1 (send ca list), 0 (don't send)\n" \
  385. USAGE_IO \
  386. USAGE_SSL_ASYNC \
  387. USAGE_SNI \
  388. "\n" \
  389. USAGE_PSK \
  390. USAGE_ECJPAKE \
  391. "\n"
  392. #define USAGE3 \
  393. " allow_legacy=%%d default: (library default: no)\n" \
  394. USAGE_RENEGO \
  395. " exchanges=%%d default: 1\n" \
  396. "\n" \
  397. USAGE_TICKETS \
  398. USAGE_CACHE \
  399. USAGE_MAX_FRAG_LEN \
  400. USAGE_TRUNC_HMAC \
  401. USAGE_ALPN \
  402. USAGE_EMS \
  403. USAGE_ETM \
  404. USAGE_CURVES \
  405. "\n"
  406. #define USAGE4 \
  407. " arc4=%%d default: (library default: 0)\n" \
  408. " allow_sha1=%%d default: 0\n" \
  409. " min_version=%%s default: (library default: tls1)\n" \
  410. " max_version=%%s default: (library default: tls1_2)\n" \
  411. " force_version=%%s default: \"\" (none)\n" \
  412. " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
  413. "\n" \
  414. " version_suites=a,b,c,d per-version ciphersuites\n" \
  415. " in order from ssl3 to tls1_2\n" \
  416. " default: all enabled\n" \
  417. " force_ciphersuite=<name> default: all enabled\n" \
  418. " query_config=<name> return 0 if the specified\n" \
  419. " configuration macro is defined and 1\n" \
  420. " otherwise. The expansion of the macro\n" \
  421. " is printed if it is defined\n" \
  422. " acceptable ciphersuite names:\n"
  423. #define ALPN_LIST_SIZE 10
  424. #define CURVE_LIST_SIZE 20
  425. #define PUT_UINT64_BE(out_be,in_le,i) \
  426. { \
  427. (out_be)[(i) + 0] = (unsigned char)( ( (in_le) >> 56 ) & 0xFF ); \
  428. (out_be)[(i) + 1] = (unsigned char)( ( (in_le) >> 48 ) & 0xFF ); \
  429. (out_be)[(i) + 2] = (unsigned char)( ( (in_le) >> 40 ) & 0xFF ); \
  430. (out_be)[(i) + 3] = (unsigned char)( ( (in_le) >> 32 ) & 0xFF ); \
  431. (out_be)[(i) + 4] = (unsigned char)( ( (in_le) >> 24 ) & 0xFF ); \
  432. (out_be)[(i) + 5] = (unsigned char)( ( (in_le) >> 16 ) & 0xFF ); \
  433. (out_be)[(i) + 6] = (unsigned char)( ( (in_le) >> 8 ) & 0xFF ); \
  434. (out_be)[(i) + 7] = (unsigned char)( ( (in_le) >> 0 ) & 0xFF ); \
  435. }
  436. /*
  437. * global options
  438. */
  439. struct options
  440. {
  441. const char *server_addr; /* address on which the ssl service runs */
  442. const char *server_port; /* port on which the ssl service runs */
  443. int debug_level; /* level of debugging */
  444. int nbio; /* should I/O be blocking? */
  445. int event; /* loop or event-driven IO? level or edge triggered? */
  446. uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
  447. int response_size; /* pad response with header to requested size */
  448. uint16_t buffer_size; /* IO buffer size */
  449. const char *ca_file; /* the file with the CA certificate(s) */
  450. const char *ca_path; /* the path with the CA certificate(s) reside */
  451. const char *crt_file; /* the file with the server certificate */
  452. const char *key_file; /* the file with the server key */
  453. const char *crt_file2; /* the file with the 2nd server certificate */
  454. const char *key_file2; /* the file with the 2nd server key */
  455. const char *async_operations; /* supported SSL asynchronous operations */
  456. int async_private_delay1; /* number of times f_async_resume needs to be called for key 1, or -1 for no async */
  457. int async_private_delay2; /* number of times f_async_resume needs to be called for key 2, or -1 for no async */
  458. int async_private_error; /* inject error in async private callback */
  459. const char *psk; /* the pre-shared key */
  460. const char *psk_identity; /* the pre-shared key identity */
  461. char *psk_list; /* list of PSK id/key pairs for callback */
  462. const char *ecjpake_pw; /* the EC J-PAKE password */
  463. int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
  464. const char *version_suites; /* per-version ciphersuites */
  465. int renegotiation; /* enable / disable renegotiation */
  466. int allow_legacy; /* allow legacy renegotiation */
  467. int renegotiate; /* attempt renegotiation? */
  468. int renego_delay; /* delay before enforcing renegotiation */
  469. uint64_t renego_period; /* period for automatic renegotiation */
  470. int exchanges; /* number of data exchanges */
  471. int min_version; /* minimum protocol version accepted */
  472. int max_version; /* maximum protocol version accepted */
  473. int arc4; /* flag for arc4 suites support */
  474. int allow_sha1; /* flag for SHA-1 support */
  475. int auth_mode; /* verify mode for connection */
  476. int cert_req_ca_list; /* should we send the CA list? */
  477. unsigned char mfl_code; /* code for maximum fragment length */
  478. int trunc_hmac; /* accept truncated hmac? */
  479. int tickets; /* enable / disable session tickets */
  480. int ticket_timeout; /* session ticket lifetime */
  481. int cache_max; /* max number of session cache entries */
  482. int cache_timeout; /* expiration delay of session cache entries */
  483. char *sni; /* string describing sni information */
  484. const char *curves; /* list of supported elliptic curves */
  485. const char *alpn_string; /* ALPN supported protocols */
  486. const char *dhm_file; /* the file with the DH parameters */
  487. int extended_ms; /* allow negotiation of extended MS? */
  488. int etm; /* allow negotiation of encrypt-then-MAC? */
  489. int transport; /* TLS or DTLS? */
  490. int cookies; /* Use cookies for DTLS? -1 to break them */
  491. int anti_replay; /* Use anti-replay for DTLS? -1 for default */
  492. uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
  493. uint32_t hs_to_max; /* Max value of DTLS handshake timer */
  494. int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
  495. int dgram_packing; /* allow/forbid datagram packing */
  496. int badmac_limit; /* Limit of records with bad MAC */
  497. } opt;
  498. int query_config( const char *config );
  499. static void my_debug( void *ctx, int level,
  500. const char *file, int line,
  501. const char *str )
  502. {
  503. const char *p, *basename;
  504. /* Extract basename from file */
  505. for( p = basename = file; *p != '\0'; p++ )
  506. if( *p == '/' || *p == '\\' )
  507. basename = p + 1;
  508. mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
  509. fflush( (FILE *) ctx );
  510. }
  511. /*
  512. * Test recv/send functions that make sure each try returns
  513. * WANT_READ/WANT_WRITE at least once before sucesseding
  514. */
  515. static int my_recv( void *ctx, unsigned char *buf, size_t len )
  516. {
  517. static int first_try = 1;
  518. int ret;
  519. if( first_try )
  520. {
  521. first_try = 0;
  522. return( MBEDTLS_ERR_SSL_WANT_READ );
  523. }
  524. ret = mbedtls_net_recv( ctx, buf, len );
  525. if( ret != MBEDTLS_ERR_SSL_WANT_READ )
  526. first_try = 1; /* Next call will be a new operation */
  527. return( ret );
  528. }
  529. static int my_send( void *ctx, const unsigned char *buf, size_t len )
  530. {
  531. static int first_try = 1;
  532. int ret;
  533. if( first_try )
  534. {
  535. first_try = 0;
  536. return( MBEDTLS_ERR_SSL_WANT_WRITE );
  537. }
  538. ret = mbedtls_net_send( ctx, buf, len );
  539. if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  540. first_try = 1; /* Next call will be a new operation */
  541. return( ret );
  542. }
  543. /*
  544. * Return authmode from string, or -1 on error
  545. */
  546. static int get_auth_mode( const char *s )
  547. {
  548. if( strcmp( s, "none" ) == 0 )
  549. return( MBEDTLS_SSL_VERIFY_NONE );
  550. if( strcmp( s, "optional" ) == 0 )
  551. return( MBEDTLS_SSL_VERIFY_OPTIONAL );
  552. if( strcmp( s, "required" ) == 0 )
  553. return( MBEDTLS_SSL_VERIFY_REQUIRED );
  554. return( -1 );
  555. }
  556. /*
  557. * Used by sni_parse and psk_parse to handle coma-separated lists
  558. */
  559. #define GET_ITEM( dst ) \
  560. do \
  561. { \
  562. (dst) = p; \
  563. while( *p != ',' ) \
  564. if( ++p > end ) \
  565. goto error; \
  566. *p++ = '\0'; \
  567. } while( 0 )
  568. #if defined(SNI_OPTION)
  569. typedef struct _sni_entry sni_entry;
  570. struct _sni_entry {
  571. const char *name;
  572. mbedtls_x509_crt *cert;
  573. mbedtls_pk_context *key;
  574. mbedtls_x509_crt* ca;
  575. mbedtls_x509_crl* crl;
  576. int authmode;
  577. sni_entry *next;
  578. };
  579. void sni_free( sni_entry *head )
  580. {
  581. sni_entry *cur = head, *next;
  582. while( cur != NULL )
  583. {
  584. mbedtls_x509_crt_free( cur->cert );
  585. mbedtls_free( cur->cert );
  586. mbedtls_pk_free( cur->key );
  587. mbedtls_free( cur->key );
  588. mbedtls_x509_crt_free( cur->ca );
  589. mbedtls_free( cur->ca );
  590. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  591. mbedtls_x509_crl_free( cur->crl );
  592. mbedtls_free( cur->crl );
  593. #endif
  594. next = cur->next;
  595. mbedtls_free( cur );
  596. cur = next;
  597. }
  598. }
  599. /*
  600. * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...]
  601. * into a usable sni_entry list. For ca1, crl1, auth1, the special value
  602. * '-' means unset. If ca1 is unset, then crl1 is ignored too.
  603. *
  604. * Modifies the input string! This is not production quality!
  605. */
  606. sni_entry *sni_parse( char *sni_string )
  607. {
  608. sni_entry *cur = NULL, *new = NULL;
  609. char *p = sni_string;
  610. char *end = p;
  611. char *crt_file, *key_file, *ca_file, *auth_str;
  612. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  613. char *crl_file;
  614. #endif
  615. while( *end != '\0' )
  616. ++end;
  617. *end = ',';
  618. while( p <= end )
  619. {
  620. if( ( new = mbedtls_calloc( 1, sizeof( sni_entry ) ) ) == NULL )
  621. {
  622. sni_free( cur );
  623. return( NULL );
  624. }
  625. GET_ITEM( new->name );
  626. GET_ITEM( crt_file );
  627. GET_ITEM( key_file );
  628. GET_ITEM( ca_file );
  629. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  630. GET_ITEM( crl_file );
  631. #endif
  632. GET_ITEM( auth_str );
  633. if( ( new->cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL ||
  634. ( new->key = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) ) ) == NULL )
  635. goto error;
  636. mbedtls_x509_crt_init( new->cert );
  637. mbedtls_pk_init( new->key );
  638. if( mbedtls_x509_crt_parse_file( new->cert, crt_file ) != 0 ||
  639. mbedtls_pk_parse_keyfile( new->key, key_file, "" ) != 0 )
  640. goto error;
  641. if( strcmp( ca_file, "-" ) != 0 )
  642. {
  643. if( ( new->ca = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
  644. goto error;
  645. mbedtls_x509_crt_init( new->ca );
  646. if( mbedtls_x509_crt_parse_file( new->ca, ca_file ) != 0 )
  647. goto error;
  648. }
  649. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  650. if( strcmp( crl_file, "-" ) != 0 )
  651. {
  652. if( ( new->crl = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) ) ) == NULL )
  653. goto error;
  654. mbedtls_x509_crl_init( new->crl );
  655. if( mbedtls_x509_crl_parse_file( new->crl, crl_file ) != 0 )
  656. goto error;
  657. }
  658. #endif
  659. if( strcmp( auth_str, "-" ) != 0 )
  660. {
  661. if( ( new->authmode = get_auth_mode( auth_str ) ) < 0 )
  662. goto error;
  663. }
  664. else
  665. new->authmode = DFL_AUTH_MODE;
  666. new->next = cur;
  667. cur = new;
  668. }
  669. return( cur );
  670. error:
  671. sni_free( new );
  672. sni_free( cur );
  673. return( NULL );
  674. }
  675. /*
  676. * SNI callback.
  677. */
  678. int sni_callback( void *p_info, mbedtls_ssl_context *ssl,
  679. const unsigned char *name, size_t name_len )
  680. {
  681. const sni_entry *cur = (const sni_entry *) p_info;
  682. while( cur != NULL )
  683. {
  684. if( name_len == strlen( cur->name ) &&
  685. memcmp( name, cur->name, name_len ) == 0 )
  686. {
  687. if( cur->ca != NULL )
  688. mbedtls_ssl_set_hs_ca_chain( ssl, cur->ca, cur->crl );
  689. if( cur->authmode != DFL_AUTH_MODE )
  690. mbedtls_ssl_set_hs_authmode( ssl, cur->authmode );
  691. return( mbedtls_ssl_set_hs_own_cert( ssl, cur->cert, cur->key ) );
  692. }
  693. cur = cur->next;
  694. }
  695. return( -1 );
  696. }
  697. #endif /* SNI_OPTION */
  698. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  699. #define HEX2NUM( c ) \
  700. do \
  701. { \
  702. if( (c) >= '0' && (c) <= '9' ) \
  703. (c) -= '0'; \
  704. else if( (c) >= 'a' && (c) <= 'f' ) \
  705. (c) -= 'a' - 10; \
  706. else if( (c) >= 'A' && (c) <= 'F' ) \
  707. (c) -= 'A' - 10; \
  708. else \
  709. return( -1 ); \
  710. } while( 0 )
  711. /*
  712. * Convert a hex string to bytes.
  713. * Return 0 on success, -1 on error.
  714. */
  715. int unhexify( unsigned char *output, const char *input, size_t *olen )
  716. {
  717. unsigned char c;
  718. size_t j;
  719. *olen = strlen( input );
  720. if( *olen % 2 != 0 || *olen / 2 > MBEDTLS_PSK_MAX_LEN )
  721. return( -1 );
  722. *olen /= 2;
  723. for( j = 0; j < *olen * 2; j += 2 )
  724. {
  725. c = input[j];
  726. HEX2NUM( c );
  727. output[ j / 2 ] = c << 4;
  728. c = input[j + 1];
  729. HEX2NUM( c );
  730. output[ j / 2 ] |= c;
  731. }
  732. return( 0 );
  733. }
  734. typedef struct _psk_entry psk_entry;
  735. struct _psk_entry
  736. {
  737. const char *name;
  738. size_t key_len;
  739. unsigned char key[MBEDTLS_PSK_MAX_LEN];
  740. psk_entry *next;
  741. };
  742. /*
  743. * Free a list of psk_entry's
  744. */
  745. void psk_free( psk_entry *head )
  746. {
  747. psk_entry *next;
  748. while( head != NULL )
  749. {
  750. next = head->next;
  751. mbedtls_free( head );
  752. head = next;
  753. }
  754. }
  755. /*
  756. * Parse a string of pairs name1,key1[,name2,key2[,...]]
  757. * into a usable psk_entry list.
  758. *
  759. * Modifies the input string! This is not production quality!
  760. */
  761. psk_entry *psk_parse( char *psk_string )
  762. {
  763. psk_entry *cur = NULL, *new = NULL;
  764. char *p = psk_string;
  765. char *end = p;
  766. char *key_hex;
  767. while( *end != '\0' )
  768. ++end;
  769. *end = ',';
  770. while( p <= end )
  771. {
  772. if( ( new = mbedtls_calloc( 1, sizeof( psk_entry ) ) ) == NULL )
  773. goto error;
  774. memset( new, 0, sizeof( psk_entry ) );
  775. GET_ITEM( new->name );
  776. GET_ITEM( key_hex );
  777. if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
  778. goto error;
  779. new->next = cur;
  780. cur = new;
  781. }
  782. return( cur );
  783. error:
  784. psk_free( new );
  785. psk_free( cur );
  786. return( 0 );
  787. }
  788. /*
  789. * PSK callback
  790. */
  791. int psk_callback( void *p_info, mbedtls_ssl_context *ssl,
  792. const unsigned char *name, size_t name_len )
  793. {
  794. psk_entry *cur = (psk_entry *) p_info;
  795. while( cur != NULL )
  796. {
  797. if( name_len == strlen( cur->name ) &&
  798. memcmp( name, cur->name, name_len ) == 0 )
  799. {
  800. return( mbedtls_ssl_set_hs_psk( ssl, cur->key, cur->key_len ) );
  801. }
  802. cur = cur->next;
  803. }
  804. return( -1 );
  805. }
  806. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  807. static mbedtls_net_context listen_fd, client_fd;
  808. /* Interruption handler to ensure clean exit (for valgrind testing) */
  809. #if !defined(_WIN32)
  810. static int received_sigterm = 0;
  811. void term_handler( int sig )
  812. {
  813. ((void) sig);
  814. received_sigterm = 1;
  815. mbedtls_net_free( &listen_fd ); /* causes mbedtls_net_accept() to abort */
  816. mbedtls_net_free( &client_fd ); /* causes net_read() to abort */
  817. }
  818. #endif
  819. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  820. static int ssl_sig_hashes_for_test[] = {
  821. #if defined(MBEDTLS_SHA512_C)
  822. MBEDTLS_MD_SHA512,
  823. MBEDTLS_MD_SHA384,
  824. #endif
  825. #if defined(MBEDTLS_SHA256_C)
  826. MBEDTLS_MD_SHA256,
  827. MBEDTLS_MD_SHA224,
  828. #endif
  829. #if defined(MBEDTLS_SHA1_C)
  830. /* Allow SHA-1 as we use it extensively in tests. */
  831. MBEDTLS_MD_SHA1,
  832. #endif
  833. MBEDTLS_MD_NONE
  834. };
  835. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  836. /** Return true if \p ret is a status code indicating that there is an
  837. * operation in progress on an SSL connection, and false if it indicates
  838. * success or a fatal error.
  839. *
  840. * The possible operations in progress are:
  841. *
  842. * - A read, when the SSL input buffer does not contain a full message.
  843. * - A write, when the SSL output buffer contains some data that has not
  844. * been sent over the network yet.
  845. * - An asynchronous callback that has not completed yet. */
  846. static int mbedtls_status_is_ssl_in_progress( int ret )
  847. {
  848. return( ret == MBEDTLS_ERR_SSL_WANT_READ ||
  849. ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
  850. ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
  851. }
  852. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  853. typedef struct
  854. {
  855. mbedtls_x509_crt *cert; /*!< Certificate corresponding to the key */
  856. mbedtls_pk_context *pk; /*!< Private key */
  857. unsigned delay; /*!< Number of resume steps to go through */
  858. unsigned pk_owned : 1; /*!< Whether to free the pk object on exit */
  859. } ssl_async_key_slot_t;
  860. typedef enum {
  861. SSL_ASYNC_INJECT_ERROR_NONE = 0, /*!< Let the callbacks succeed */
  862. SSL_ASYNC_INJECT_ERROR_START, /*!< Inject error during start */
  863. SSL_ASYNC_INJECT_ERROR_CANCEL, /*!< Close the connection after async start */
  864. SSL_ASYNC_INJECT_ERROR_RESUME, /*!< Inject error during resume */
  865. #define SSL_ASYNC_INJECT_ERROR_MAX SSL_ASYNC_INJECT_ERROR_RESUME
  866. } ssl_async_inject_error_t;
  867. typedef struct
  868. {
  869. ssl_async_key_slot_t slots[4]; /* key, key2, sni1, sni2 */
  870. size_t slots_used;
  871. ssl_async_inject_error_t inject_error;
  872. int (*f_rng)(void *, unsigned char *, size_t);
  873. void *p_rng;
  874. } ssl_async_key_context_t;
  875. int ssl_async_set_key( ssl_async_key_context_t *ctx,
  876. mbedtls_x509_crt *cert,
  877. mbedtls_pk_context *pk,
  878. int pk_take_ownership,
  879. unsigned delay )
  880. {
  881. if( ctx->slots_used >= sizeof( ctx->slots ) / sizeof( *ctx->slots ) )
  882. return( -1 );
  883. ctx->slots[ctx->slots_used].cert = cert;
  884. ctx->slots[ctx->slots_used].pk = pk;
  885. ctx->slots[ctx->slots_used].delay = delay;
  886. ctx->slots[ctx->slots_used].pk_owned = pk_take_ownership;
  887. ++ctx->slots_used;
  888. return( 0 );
  889. }
  890. #define SSL_ASYNC_INPUT_MAX_SIZE 512
  891. typedef enum
  892. {
  893. ASYNC_OP_SIGN,
  894. ASYNC_OP_DECRYPT,
  895. } ssl_async_operation_type_t;
  896. /* Note that the enum above and the array below need to be kept in sync!
  897. * `ssl_async_operation_names[op]` is the name of op for each value `op`
  898. * of type `ssl_async_operation_type_t`. */
  899. static const char *const ssl_async_operation_names[] =
  900. {
  901. "sign",
  902. "decrypt",
  903. };
  904. typedef struct
  905. {
  906. unsigned slot;
  907. ssl_async_operation_type_t operation_type;
  908. mbedtls_md_type_t md_alg;
  909. unsigned char input[SSL_ASYNC_INPUT_MAX_SIZE];
  910. size_t input_len;
  911. unsigned remaining_delay;
  912. } ssl_async_operation_context_t;
  913. static int ssl_async_start( mbedtls_ssl_context *ssl,
  914. mbedtls_x509_crt *cert,
  915. ssl_async_operation_type_t op_type,
  916. mbedtls_md_type_t md_alg,
  917. const unsigned char *input,
  918. size_t input_len )
  919. {
  920. ssl_async_key_context_t *config_data =
  921. mbedtls_ssl_conf_get_async_config_data( ssl->conf );
  922. unsigned slot;
  923. ssl_async_operation_context_t *ctx = NULL;
  924. const char *op_name = ssl_async_operation_names[op_type];
  925. {
  926. char dn[100];
  927. if( mbedtls_x509_dn_gets( dn, sizeof( dn ), &cert->subject ) > 0 )
  928. mbedtls_printf( "Async %s callback: looking for DN=%s\n",
  929. op_name, dn );
  930. }
  931. /* Look for a private key that matches the public key in cert.
  932. * Since this test code has the private key inside Mbed TLS,
  933. * we call mbedtls_pk_check_pair to match a private key with the
  934. * public key. */
  935. for( slot = 0; slot < config_data->slots_used; slot++ )
  936. {
  937. if( mbedtls_pk_check_pair( &cert->pk,
  938. config_data->slots[slot].pk ) == 0 )
  939. break;
  940. }
  941. if( slot == config_data->slots_used )
  942. {
  943. mbedtls_printf( "Async %s callback: no key matches this certificate.\n",
  944. op_name );
  945. return( MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH );
  946. }
  947. mbedtls_printf( "Async %s callback: using key slot %u, delay=%u.\n",
  948. op_name, slot, config_data->slots[slot].delay );
  949. if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_START )
  950. {
  951. mbedtls_printf( "Async %s callback: injected error\n", op_name );
  952. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  953. }
  954. if( input_len > SSL_ASYNC_INPUT_MAX_SIZE )
  955. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  956. ctx = mbedtls_calloc( 1, sizeof( *ctx ) );
  957. if( ctx == NULL )
  958. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  959. ctx->slot = slot;
  960. ctx->operation_type = op_type;
  961. ctx->md_alg = md_alg;
  962. memcpy( ctx->input, input, input_len );
  963. ctx->input_len = input_len;
  964. ctx->remaining_delay = config_data->slots[slot].delay;
  965. mbedtls_ssl_set_async_operation_data( ssl, ctx );
  966. if( ctx->remaining_delay == 0 )
  967. return( 0 );
  968. else
  969. return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
  970. }
  971. static int ssl_async_sign( mbedtls_ssl_context *ssl,
  972. mbedtls_x509_crt *cert,
  973. mbedtls_md_type_t md_alg,
  974. const unsigned char *hash,
  975. size_t hash_len )
  976. {
  977. return( ssl_async_start( ssl, cert,
  978. ASYNC_OP_SIGN, md_alg,
  979. hash, hash_len ) );
  980. }
  981. static int ssl_async_decrypt( mbedtls_ssl_context *ssl,
  982. mbedtls_x509_crt *cert,
  983. const unsigned char *input,
  984. size_t input_len )
  985. {
  986. return( ssl_async_start( ssl, cert,
  987. ASYNC_OP_DECRYPT, MBEDTLS_MD_NONE,
  988. input, input_len ) );
  989. }
  990. static int ssl_async_resume( mbedtls_ssl_context *ssl,
  991. unsigned char *output,
  992. size_t *output_len,
  993. size_t output_size )
  994. {
  995. ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
  996. ssl_async_key_context_t *config_data =
  997. mbedtls_ssl_conf_get_async_config_data( ssl->conf );
  998. ssl_async_key_slot_t *key_slot = &config_data->slots[ctx->slot];
  999. int ret;
  1000. const char *op_name;
  1001. if( ctx->remaining_delay > 0 )
  1002. {
  1003. --ctx->remaining_delay;
  1004. mbedtls_printf( "Async resume (slot %u): call %u more times.\n",
  1005. ctx->slot, ctx->remaining_delay );
  1006. return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
  1007. }
  1008. switch( ctx->operation_type )
  1009. {
  1010. case ASYNC_OP_DECRYPT:
  1011. ret = mbedtls_pk_decrypt( key_slot->pk,
  1012. ctx->input, ctx->input_len,
  1013. output, output_len, output_size,
  1014. config_data->f_rng, config_data->p_rng );
  1015. break;
  1016. case ASYNC_OP_SIGN:
  1017. ret = mbedtls_pk_sign( key_slot->pk,
  1018. ctx->md_alg,
  1019. ctx->input, ctx->input_len,
  1020. output, output_len,
  1021. config_data->f_rng, config_data->p_rng );
  1022. break;
  1023. default:
  1024. mbedtls_printf( "Async resume (slot %u): unknown operation type %ld. This shouldn't happen.\n",
  1025. ctx->slot, (long) ctx->operation_type );
  1026. mbedtls_free( ctx );
  1027. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  1028. break;
  1029. }
  1030. op_name = ssl_async_operation_names[ctx->operation_type];
  1031. if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_RESUME )
  1032. {
  1033. mbedtls_printf( "Async resume callback: %s done but injected error\n",
  1034. op_name );
  1035. mbedtls_free( ctx );
  1036. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  1037. }
  1038. mbedtls_printf( "Async resume (slot %u): %s done, status=%d.\n",
  1039. ctx->slot, op_name, ret );
  1040. mbedtls_free( ctx );
  1041. return( ret );
  1042. }
  1043. static void ssl_async_cancel( mbedtls_ssl_context *ssl )
  1044. {
  1045. ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
  1046. mbedtls_printf( "Async cancel callback.\n" );
  1047. mbedtls_free( ctx );
  1048. }
  1049. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  1050. /*
  1051. * Wait for an event from the underlying transport or the timer
  1052. * (Used in event-driven IO mode).
  1053. */
  1054. #if !defined(MBEDTLS_TIMING_C)
  1055. int idle( mbedtls_net_context *fd,
  1056. int idle_reason )
  1057. #else
  1058. int idle( mbedtls_net_context *fd,
  1059. mbedtls_timing_delay_context *timer,
  1060. int idle_reason )
  1061. #endif
  1062. {
  1063. int ret;
  1064. int poll_type = 0;
  1065. if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
  1066. poll_type = MBEDTLS_NET_POLL_WRITE;
  1067. else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
  1068. poll_type = MBEDTLS_NET_POLL_READ;
  1069. #if !defined(MBEDTLS_TIMING_C)
  1070. else
  1071. return( 0 );
  1072. #endif
  1073. while( 1 )
  1074. {
  1075. /* Check if timer has expired */
  1076. #if defined(MBEDTLS_TIMING_C)
  1077. if( timer != NULL &&
  1078. mbedtls_timing_get_delay( timer ) == 2 )
  1079. {
  1080. break;
  1081. }
  1082. #endif /* MBEDTLS_TIMING_C */
  1083. /* Check if underlying transport became available */
  1084. if( poll_type != 0 )
  1085. {
  1086. ret = mbedtls_net_poll( fd, poll_type, 0 );
  1087. if( ret < 0 )
  1088. return( ret );
  1089. if( ret == poll_type )
  1090. break;
  1091. }
  1092. }
  1093. return( 0 );
  1094. }
  1095. int main( int argc, char *argv[] )
  1096. {
  1097. int ret = 0, len, written, frags, exchanges_left;
  1098. int version_suites[4][2];
  1099. unsigned char* buf = 0;
  1100. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  1101. unsigned char psk[MBEDTLS_PSK_MAX_LEN];
  1102. size_t psk_len = 0;
  1103. psk_entry *psk_info = NULL;
  1104. #endif
  1105. const char *pers = "ssl_server2";
  1106. unsigned char client_ip[16] = { 0 };
  1107. size_t cliip_len;
  1108. #if defined(MBEDTLS_SSL_COOKIE_C)
  1109. mbedtls_ssl_cookie_ctx cookie_ctx;
  1110. #endif
  1111. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1112. mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
  1113. #endif
  1114. mbedtls_entropy_context entropy;
  1115. mbedtls_ctr_drbg_context ctr_drbg;
  1116. mbedtls_ssl_context ssl;
  1117. mbedtls_ssl_config conf;
  1118. #if defined(MBEDTLS_TIMING_C)
  1119. mbedtls_timing_delay_context timer;
  1120. #endif
  1121. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1122. unsigned char renego_period[8] = { 0 };
  1123. #endif
  1124. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1125. uint32_t flags;
  1126. mbedtls_x509_crt cacert;
  1127. mbedtls_x509_crt srvcert;
  1128. mbedtls_pk_context pkey;
  1129. mbedtls_x509_crt srvcert2;
  1130. mbedtls_pk_context pkey2;
  1131. int key_cert_init = 0, key_cert_init2 = 0;
  1132. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  1133. ssl_async_key_context_t ssl_async_keys;
  1134. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  1135. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  1136. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
  1137. mbedtls_dhm_context dhm;
  1138. #endif
  1139. #if defined(MBEDTLS_SSL_CACHE_C)
  1140. mbedtls_ssl_cache_context cache;
  1141. #endif
  1142. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1143. mbedtls_ssl_ticket_context ticket_ctx;
  1144. #endif
  1145. #if defined(SNI_OPTION)
  1146. sni_entry *sni_info = NULL;
  1147. #endif
  1148. #if defined(MBEDTLS_ECP_C)
  1149. mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
  1150. const mbedtls_ecp_curve_info * curve_cur;
  1151. #endif
  1152. #if defined(MBEDTLS_SSL_ALPN)
  1153. const char *alpn_list[ALPN_LIST_SIZE];
  1154. #endif
  1155. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  1156. unsigned char alloc_buf[MEMORY_HEAP_SIZE];
  1157. #endif
  1158. int i;
  1159. char *p, *q;
  1160. const int *list;
  1161. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  1162. mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
  1163. #endif
  1164. /*
  1165. * Make sure memory references are valid in case we exit early.
  1166. */
  1167. mbedtls_net_init( &client_fd );
  1168. mbedtls_net_init( &listen_fd );
  1169. mbedtls_ssl_init( &ssl );
  1170. mbedtls_ssl_config_init( &conf );
  1171. mbedtls_ctr_drbg_init( &ctr_drbg );
  1172. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1173. mbedtls_x509_crt_init( &cacert );
  1174. mbedtls_x509_crt_init( &srvcert );
  1175. mbedtls_pk_init( &pkey );
  1176. mbedtls_x509_crt_init( &srvcert2 );
  1177. mbedtls_pk_init( &pkey2 );
  1178. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  1179. memset( &ssl_async_keys, 0, sizeof( ssl_async_keys ) );
  1180. #endif
  1181. #endif
  1182. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
  1183. mbedtls_dhm_init( &dhm );
  1184. #endif
  1185. #if defined(MBEDTLS_SSL_CACHE_C)
  1186. mbedtls_ssl_cache_init( &cache );
  1187. #endif
  1188. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1189. mbedtls_ssl_ticket_init( &ticket_ctx );
  1190. #endif
  1191. #if defined(MBEDTLS_SSL_ALPN)
  1192. memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
  1193. #endif
  1194. #if defined(MBEDTLS_SSL_COOKIE_C)
  1195. mbedtls_ssl_cookie_init( &cookie_ctx );
  1196. #endif
  1197. #if !defined(_WIN32)
  1198. /* Abort cleanly on SIGTERM and SIGINT */
  1199. signal( SIGTERM, term_handler );
  1200. signal( SIGINT, term_handler );
  1201. #endif
  1202. if( argc == 0 )
  1203. {
  1204. usage:
  1205. if( ret == 0 )
  1206. ret = 1;
  1207. mbedtls_printf( USAGE1 );
  1208. mbedtls_printf( USAGE2 );
  1209. mbedtls_printf( USAGE3 );
  1210. mbedtls_printf( USAGE4 );
  1211. list = mbedtls_ssl_list_ciphersuites();
  1212. while( *list )
  1213. {
  1214. mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
  1215. list++;
  1216. if( !*list )
  1217. break;
  1218. mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
  1219. list++;
  1220. }
  1221. mbedtls_printf("\n");
  1222. goto exit;
  1223. }
  1224. opt.buffer_size = DFL_IO_BUF_LEN;
  1225. opt.server_addr = DFL_SERVER_ADDR;
  1226. opt.server_port = DFL_SERVER_PORT;
  1227. opt.debug_level = DFL_DEBUG_LEVEL;
  1228. opt.event = DFL_EVENT;
  1229. opt.response_size = DFL_RESPONSE_SIZE;
  1230. opt.nbio = DFL_NBIO;
  1231. opt.read_timeout = DFL_READ_TIMEOUT;
  1232. opt.ca_file = DFL_CA_FILE;
  1233. opt.ca_path = DFL_CA_PATH;
  1234. opt.crt_file = DFL_CRT_FILE;
  1235. opt.key_file = DFL_KEY_FILE;
  1236. opt.crt_file2 = DFL_CRT_FILE2;
  1237. opt.key_file2 = DFL_KEY_FILE2;
  1238. opt.async_operations = DFL_ASYNC_OPERATIONS;
  1239. opt.async_private_delay1 = DFL_ASYNC_PRIVATE_DELAY1;
  1240. opt.async_private_delay2 = DFL_ASYNC_PRIVATE_DELAY2;
  1241. opt.async_private_error = DFL_ASYNC_PRIVATE_ERROR;
  1242. opt.psk = DFL_PSK;
  1243. opt.psk_identity = DFL_PSK_IDENTITY;
  1244. opt.psk_list = DFL_PSK_LIST;
  1245. opt.ecjpake_pw = DFL_ECJPAKE_PW;
  1246. opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
  1247. opt.version_suites = DFL_VERSION_SUITES;
  1248. opt.renegotiation = DFL_RENEGOTIATION;
  1249. opt.allow_legacy = DFL_ALLOW_LEGACY;
  1250. opt.renegotiate = DFL_RENEGOTIATE;
  1251. opt.renego_delay = DFL_RENEGO_DELAY;
  1252. opt.renego_period = DFL_RENEGO_PERIOD;
  1253. opt.exchanges = DFL_EXCHANGES;
  1254. opt.min_version = DFL_MIN_VERSION;
  1255. opt.max_version = DFL_MAX_VERSION;
  1256. opt.arc4 = DFL_ARC4;
  1257. opt.allow_sha1 = DFL_SHA1;
  1258. opt.auth_mode = DFL_AUTH_MODE;
  1259. opt.cert_req_ca_list = DFL_CERT_REQ_CA_LIST;
  1260. opt.mfl_code = DFL_MFL_CODE;
  1261. opt.trunc_hmac = DFL_TRUNC_HMAC;
  1262. opt.tickets = DFL_TICKETS;
  1263. opt.ticket_timeout = DFL_TICKET_TIMEOUT;
  1264. opt.cache_max = DFL_CACHE_MAX;
  1265. opt.cache_timeout = DFL_CACHE_TIMEOUT;
  1266. opt.sni = DFL_SNI;
  1267. opt.alpn_string = DFL_ALPN_STRING;
  1268. opt.curves = DFL_CURVES;
  1269. opt.dhm_file = DFL_DHM_FILE;
  1270. opt.transport = DFL_TRANSPORT;
  1271. opt.cookies = DFL_COOKIES;
  1272. opt.anti_replay = DFL_ANTI_REPLAY;
  1273. opt.hs_to_min = DFL_HS_TO_MIN;
  1274. opt.hs_to_max = DFL_HS_TO_MAX;
  1275. opt.dtls_mtu = DFL_DTLS_MTU;
  1276. opt.dgram_packing = DFL_DGRAM_PACKING;
  1277. opt.badmac_limit = DFL_BADMAC_LIMIT;
  1278. opt.extended_ms = DFL_EXTENDED_MS;
  1279. opt.etm = DFL_ETM;
  1280. for( i = 1; i < argc; i++ )
  1281. {
  1282. p = argv[i];
  1283. if( ( q = strchr( p, '=' ) ) == NULL )
  1284. goto usage;
  1285. *q++ = '\0';
  1286. if( strcmp( p, "server_port" ) == 0 )
  1287. opt.server_port = q;
  1288. else if( strcmp( p, "server_addr" ) == 0 )
  1289. opt.server_addr = q;
  1290. else if( strcmp( p, "dtls" ) == 0 )
  1291. {
  1292. int t = atoi( q );
  1293. if( t == 0 )
  1294. opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
  1295. else if( t == 1 )
  1296. opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
  1297. else
  1298. goto usage;
  1299. }
  1300. else if( strcmp( p, "debug_level" ) == 0 )
  1301. {
  1302. opt.debug_level = atoi( q );
  1303. if( opt.debug_level < 0 || opt.debug_level > 65535 )
  1304. goto usage;
  1305. }
  1306. else if( strcmp( p, "nbio" ) == 0 )
  1307. {
  1308. opt.nbio = atoi( q );
  1309. if( opt.nbio < 0 || opt.nbio > 2 )
  1310. goto usage;
  1311. }
  1312. else if( strcmp( p, "event" ) == 0 )
  1313. {
  1314. opt.event = atoi( q );
  1315. if( opt.event < 0 || opt.event > 2 )
  1316. goto usage;
  1317. }
  1318. else if( strcmp( p, "read_timeout" ) == 0 )
  1319. opt.read_timeout = atoi( q );
  1320. else if( strcmp( p, "buffer_size" ) == 0 )
  1321. {
  1322. opt.buffer_size = atoi( q );
  1323. if( opt.buffer_size < 1 || opt.buffer_size > MBEDTLS_SSL_MAX_CONTENT_LEN + 1 )
  1324. goto usage;
  1325. }
  1326. else if( strcmp( p, "response_size" ) == 0 )
  1327. {
  1328. opt.response_size = atoi( q );
  1329. if( opt.response_size < 0 || opt.response_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
  1330. goto usage;
  1331. if( opt.buffer_size < opt.response_size )
  1332. opt.buffer_size = opt.response_size;
  1333. }
  1334. else if( strcmp( p, "ca_file" ) == 0 )
  1335. opt.ca_file = q;
  1336. else if( strcmp( p, "ca_path" ) == 0 )
  1337. opt.ca_path = q;
  1338. else if( strcmp( p, "crt_file" ) == 0 )
  1339. opt.crt_file = q;
  1340. else if( strcmp( p, "key_file" ) == 0 )
  1341. opt.key_file = q;
  1342. else if( strcmp( p, "crt_file2" ) == 0 )
  1343. opt.crt_file2 = q;
  1344. else if( strcmp( p, "key_file2" ) == 0 )
  1345. opt.key_file2 = q;
  1346. else if( strcmp( p, "dhm_file" ) == 0 )
  1347. opt.dhm_file = q;
  1348. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  1349. else if( strcmp( p, "async_operations" ) == 0 )
  1350. opt.async_operations = q;
  1351. else if( strcmp( p, "async_private_delay1" ) == 0 )
  1352. opt.async_private_delay1 = atoi( q );
  1353. else if( strcmp( p, "async_private_delay2" ) == 0 )
  1354. opt.async_private_delay2 = atoi( q );
  1355. else if( strcmp( p, "async_private_error" ) == 0 )
  1356. {
  1357. int n = atoi( q );
  1358. if( n < -SSL_ASYNC_INJECT_ERROR_MAX ||
  1359. n > SSL_ASYNC_INJECT_ERROR_MAX )
  1360. {
  1361. ret = 2;
  1362. goto usage;
  1363. }
  1364. opt.async_private_error = n;
  1365. }
  1366. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  1367. else if( strcmp( p, "psk" ) == 0 )
  1368. opt.psk = q;
  1369. else if( strcmp( p, "psk_identity" ) == 0 )
  1370. opt.psk_identity = q;
  1371. else if( strcmp( p, "psk_list" ) == 0 )
  1372. opt.psk_list = q;
  1373. else if( strcmp( p, "ecjpake_pw" ) == 0 )
  1374. opt.ecjpake_pw = q;
  1375. else if( strcmp( p, "force_ciphersuite" ) == 0 )
  1376. {
  1377. opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
  1378. if( opt.force_ciphersuite[0] == 0 )
  1379. {
  1380. ret = 2;
  1381. goto usage;
  1382. }
  1383. opt.force_ciphersuite[1] = 0;
  1384. }
  1385. else if( strcmp( p, "curves" ) == 0 )
  1386. opt.curves = q;
  1387. else if( strcmp( p, "version_suites" ) == 0 )
  1388. opt.version_suites = q;
  1389. else if( strcmp( p, "renegotiation" ) == 0 )
  1390. {
  1391. opt.renegotiation = (atoi( q )) ?
  1392. MBEDTLS_SSL_RENEGOTIATION_ENABLED :
  1393. MBEDTLS_SSL_RENEGOTIATION_DISABLED;
  1394. }
  1395. else if( strcmp( p, "allow_legacy" ) == 0 )
  1396. {
  1397. switch( atoi( q ) )
  1398. {
  1399. case -1:
  1400. opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
  1401. break;
  1402. case 0:
  1403. opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
  1404. break;
  1405. case 1:
  1406. opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
  1407. break;
  1408. default: goto usage;
  1409. }
  1410. }
  1411. else if( strcmp( p, "renegotiate" ) == 0 )
  1412. {
  1413. opt.renegotiate = atoi( q );
  1414. if( opt.renegotiate < 0 || opt.renegotiate > 1 )
  1415. goto usage;
  1416. }
  1417. else if( strcmp( p, "renego_delay" ) == 0 )
  1418. {
  1419. opt.renego_delay = atoi( q );
  1420. }
  1421. else if( strcmp( p, "renego_period" ) == 0 )
  1422. {
  1423. #if defined(_MSC_VER)
  1424. opt.renego_period = _strtoui64( q, NULL, 10 );
  1425. #else
  1426. if( sscanf( q, "%" SCNu64, &opt.renego_period ) != 1 )
  1427. goto usage;
  1428. #endif /* _MSC_VER */
  1429. if( opt.renego_period < 2 )
  1430. goto usage;
  1431. }
  1432. else if( strcmp( p, "exchanges" ) == 0 )
  1433. {
  1434. opt.exchanges = atoi( q );
  1435. if( opt.exchanges < 0 )
  1436. goto usage;
  1437. }
  1438. else if( strcmp( p, "min_version" ) == 0 )
  1439. {
  1440. if( strcmp( q, "ssl3" ) == 0 )
  1441. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
  1442. else if( strcmp( q, "tls1" ) == 0 )
  1443. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
  1444. else if( strcmp( q, "tls1_1" ) == 0 ||
  1445. strcmp( q, "dtls1" ) == 0 )
  1446. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
  1447. else if( strcmp( q, "tls1_2" ) == 0 ||
  1448. strcmp( q, "dtls1_2" ) == 0 )
  1449. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1450. else
  1451. goto usage;
  1452. }
  1453. else if( strcmp( p, "max_version" ) == 0 )
  1454. {
  1455. if( strcmp( q, "ssl3" ) == 0 )
  1456. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
  1457. else if( strcmp( q, "tls1" ) == 0 )
  1458. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
  1459. else if( strcmp( q, "tls1_1" ) == 0 ||
  1460. strcmp( q, "dtls1" ) == 0 )
  1461. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
  1462. else if( strcmp( q, "tls1_2" ) == 0 ||
  1463. strcmp( q, "dtls1_2" ) == 0 )
  1464. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1465. else
  1466. goto usage;
  1467. }
  1468. else if( strcmp( p, "arc4" ) == 0 )
  1469. {
  1470. switch( atoi( q ) )
  1471. {
  1472. case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
  1473. case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
  1474. default: goto usage;
  1475. }
  1476. }
  1477. else if( strcmp( p, "allow_sha1" ) == 0 )
  1478. {
  1479. switch( atoi( q ) )
  1480. {
  1481. case 0: opt.allow_sha1 = 0; break;
  1482. case 1: opt.allow_sha1 = 1; break;
  1483. default: goto usage;
  1484. }
  1485. }
  1486. else if( strcmp( p, "force_version" ) == 0 )
  1487. {
  1488. if( strcmp( q, "ssl3" ) == 0 )
  1489. {
  1490. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
  1491. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
  1492. }
  1493. else if( strcmp( q, "tls1" ) == 0 )
  1494. {
  1495. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
  1496. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
  1497. }
  1498. else if( strcmp( q, "tls1_1" ) == 0 )
  1499. {
  1500. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
  1501. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
  1502. }
  1503. else if( strcmp( q, "tls1_2" ) == 0 )
  1504. {
  1505. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1506. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1507. }
  1508. else if( strcmp( q, "dtls1" ) == 0 )
  1509. {
  1510. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
  1511. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
  1512. opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
  1513. }
  1514. else if( strcmp( q, "dtls1_2" ) == 0 )
  1515. {
  1516. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1517. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1518. opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
  1519. }
  1520. else
  1521. goto usage;
  1522. }
  1523. else if( strcmp( p, "auth_mode" ) == 0 )
  1524. {
  1525. if( ( opt.auth_mode = get_auth_mode( q ) ) < 0 )
  1526. goto usage;
  1527. }
  1528. else if( strcmp( p, "cert_req_ca_list" ) == 0 )
  1529. {
  1530. opt.cert_req_ca_list = atoi( q );
  1531. if( opt.cert_req_ca_list < 0 || opt.cert_req_ca_list > 1 )
  1532. goto usage;
  1533. }
  1534. else if( strcmp( p, "max_frag_len" ) == 0 )
  1535. {
  1536. if( strcmp( q, "512" ) == 0 )
  1537. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
  1538. else if( strcmp( q, "1024" ) == 0 )
  1539. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
  1540. else if( strcmp( q, "2048" ) == 0 )
  1541. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
  1542. else if( strcmp( q, "4096" ) == 0 )
  1543. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
  1544. else
  1545. goto usage;
  1546. }
  1547. else if( strcmp( p, "alpn" ) == 0 )
  1548. {
  1549. opt.alpn_string = q;
  1550. }
  1551. else if( strcmp( p, "trunc_hmac" ) == 0 )
  1552. {
  1553. switch( atoi( q ) )
  1554. {
  1555. case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
  1556. case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
  1557. default: goto usage;
  1558. }
  1559. }
  1560. else if( strcmp( p, "extended_ms" ) == 0 )
  1561. {
  1562. switch( atoi( q ) )
  1563. {
  1564. case 0:
  1565. opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
  1566. break;
  1567. case 1:
  1568. opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
  1569. break;
  1570. default: goto usage;
  1571. }
  1572. }
  1573. else if( strcmp( p, "etm" ) == 0 )
  1574. {
  1575. switch( atoi( q ) )
  1576. {
  1577. case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
  1578. case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
  1579. default: goto usage;
  1580. }
  1581. }
  1582. else if( strcmp( p, "tickets" ) == 0 )
  1583. {
  1584. opt.tickets = atoi( q );
  1585. if( opt.tickets < 0 || opt.tickets > 1 )
  1586. goto usage;
  1587. }
  1588. else if( strcmp( p, "ticket_timeout" ) == 0 )
  1589. {
  1590. opt.ticket_timeout = atoi( q );
  1591. if( opt.ticket_timeout < 0 )
  1592. goto usage;
  1593. }
  1594. else if( strcmp( p, "cache_max" ) == 0 )
  1595. {
  1596. opt.cache_max = atoi( q );
  1597. if( opt.cache_max < 0 )
  1598. goto usage;
  1599. }
  1600. else if( strcmp( p, "cache_timeout" ) == 0 )
  1601. {
  1602. opt.cache_timeout = atoi( q );
  1603. if( opt.cache_timeout < 0 )
  1604. goto usage;
  1605. }
  1606. else if( strcmp( p, "cookies" ) == 0 )
  1607. {
  1608. opt.cookies = atoi( q );
  1609. if( opt.cookies < -1 || opt.cookies > 1)
  1610. goto usage;
  1611. }
  1612. else if( strcmp( p, "anti_replay" ) == 0 )
  1613. {
  1614. opt.anti_replay = atoi( q );
  1615. if( opt.anti_replay < 0 || opt.anti_replay > 1)
  1616. goto usage;
  1617. }
  1618. else if( strcmp( p, "badmac_limit" ) == 0 )
  1619. {
  1620. opt.badmac_limit = atoi( q );
  1621. if( opt.badmac_limit < 0 )
  1622. goto usage;
  1623. }
  1624. else if( strcmp( p, "hs_timeout" ) == 0 )
  1625. {
  1626. if( ( p = strchr( q, '-' ) ) == NULL )
  1627. goto usage;
  1628. *p++ = '\0';
  1629. opt.hs_to_min = atoi( q );
  1630. opt.hs_to_max = atoi( p );
  1631. if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
  1632. goto usage;
  1633. }
  1634. else if( strcmp( p, "mtu" ) == 0 )
  1635. {
  1636. opt.dtls_mtu = atoi( q );
  1637. if( opt.dtls_mtu < 0 )
  1638. goto usage;
  1639. }
  1640. else if( strcmp( p, "dgram_packing" ) == 0 )
  1641. {
  1642. opt.dgram_packing = atoi( q );
  1643. if( opt.dgram_packing != 0 &&
  1644. opt.dgram_packing != 1 )
  1645. {
  1646. goto usage;
  1647. }
  1648. }
  1649. else if( strcmp( p, "sni" ) == 0 )
  1650. {
  1651. opt.sni = q;
  1652. }
  1653. else if( strcmp( p, "query_config" ) == 0 )
  1654. {
  1655. mbedtls_exit( query_config( q ) );
  1656. }
  1657. else
  1658. goto usage;
  1659. }
  1660. /* Event-driven IO is incompatible with the above custom
  1661. * receive and send functions, as the polling builds on
  1662. * refers to the underlying net_context. */
  1663. if( opt.event == 1 && opt.nbio != 1 )
  1664. {
  1665. mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
  1666. opt.nbio = 1;
  1667. }
  1668. #if defined(MBEDTLS_DEBUG_C)
  1669. mbedtls_debug_set_threshold( opt.debug_level );
  1670. #endif
  1671. buf = mbedtls_calloc( 1, opt.buffer_size + 1 );
  1672. if( buf == NULL )
  1673. {
  1674. mbedtls_printf( "Could not allocate %u bytes\n", opt.buffer_size );
  1675. ret = 3;
  1676. goto exit;
  1677. }
  1678. if( opt.force_ciphersuite[0] > 0 )
  1679. {
  1680. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  1681. ciphersuite_info =
  1682. mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
  1683. if( opt.max_version != -1 &&
  1684. ciphersuite_info->min_minor_ver > opt.max_version )
  1685. {
  1686. mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
  1687. ret = 2;
  1688. goto usage;
  1689. }
  1690. if( opt.min_version != -1 &&
  1691. ciphersuite_info->max_minor_ver < opt.min_version )
  1692. {
  1693. mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
  1694. ret = 2;
  1695. goto usage;
  1696. }
  1697. /* If we select a version that's not supported by
  1698. * this suite, then there will be no common ciphersuite... */
  1699. if( opt.max_version == -1 ||
  1700. opt.max_version > ciphersuite_info->max_minor_ver )
  1701. {
  1702. opt.max_version = ciphersuite_info->max_minor_ver;
  1703. }
  1704. if( opt.min_version < ciphersuite_info->min_minor_ver )
  1705. {
  1706. opt.min_version = ciphersuite_info->min_minor_ver;
  1707. /* DTLS starts with TLS 1.1 */
  1708. if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  1709. opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
  1710. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
  1711. }
  1712. /* Enable RC4 if needed and not explicitly disabled */
  1713. if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
  1714. {
  1715. if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
  1716. {
  1717. mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
  1718. ret = 2;
  1719. goto usage;
  1720. }
  1721. opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
  1722. }
  1723. }
  1724. if( opt.version_suites != NULL )
  1725. {
  1726. const char *name[4] = { 0 };
  1727. /* Parse 4-element coma-separated list */
  1728. for( i = 0, p = (char *) opt.version_suites;
  1729. i < 4 && *p != '\0';
  1730. i++ )
  1731. {
  1732. name[i] = p;
  1733. /* Terminate the current string and move on to next one */
  1734. while( *p != ',' && *p != '\0' )
  1735. p++;
  1736. if( *p == ',' )
  1737. *p++ = '\0';
  1738. }
  1739. if( i != 4 )
  1740. {
  1741. mbedtls_printf( "too few values for version_suites\n" );
  1742. ret = 1;
  1743. goto exit;
  1744. }
  1745. memset( version_suites, 0, sizeof( version_suites ) );
  1746. /* Get the suites identifiers from their name */
  1747. for( i = 0; i < 4; i++ )
  1748. {
  1749. version_suites[i][0] = mbedtls_ssl_get_ciphersuite_id( name[i] );
  1750. if( version_suites[i][0] == 0 )
  1751. {
  1752. mbedtls_printf( "unknown ciphersuite: '%s'\n", name[i] );
  1753. ret = 2;
  1754. goto usage;
  1755. }
  1756. }
  1757. }
  1758. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  1759. /*
  1760. * Unhexify the pre-shared key and parse the list if any given
  1761. */
  1762. if( unhexify( psk, opt.psk, &psk_len ) != 0 )
  1763. {
  1764. mbedtls_printf( "pre-shared key not valid hex\n" );
  1765. goto exit;
  1766. }
  1767. if( opt.psk_list != NULL )
  1768. {
  1769. if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
  1770. {
  1771. mbedtls_printf( "psk_list invalid" );
  1772. goto exit;
  1773. }
  1774. }
  1775. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  1776. #if defined(MBEDTLS_ECP_C)
  1777. if( opt.curves != NULL )
  1778. {
  1779. p = (char *) opt.curves;
  1780. i = 0;
  1781. if( strcmp( p, "none" ) == 0 )
  1782. {
  1783. curve_list[0] = MBEDTLS_ECP_DP_NONE;
  1784. }
  1785. else if( strcmp( p, "default" ) != 0 )
  1786. {
  1787. /* Leave room for a final NULL in curve list */
  1788. while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
  1789. {
  1790. q = p;
  1791. /* Terminate the current string */
  1792. while( *p != ',' && *p != '\0' )
  1793. p++;
  1794. if( *p == ',' )
  1795. *p++ = '\0';
  1796. if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
  1797. {
  1798. curve_list[i++] = curve_cur->grp_id;
  1799. }
  1800. else
  1801. {
  1802. mbedtls_printf( "unknown curve %s\n", q );
  1803. mbedtls_printf( "supported curves: " );
  1804. for( curve_cur = mbedtls_ecp_curve_list();
  1805. curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
  1806. curve_cur++ )
  1807. {
  1808. mbedtls_printf( "%s ", curve_cur->name );
  1809. }
  1810. mbedtls_printf( "\n" );
  1811. goto exit;
  1812. }
  1813. }
  1814. mbedtls_printf("Number of curves: %d\n", i );
  1815. if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
  1816. {
  1817. mbedtls_printf( "curves list too long, maximum %d",
  1818. CURVE_LIST_SIZE - 1 );
  1819. goto exit;
  1820. }
  1821. curve_list[i] = MBEDTLS_ECP_DP_NONE;
  1822. }
  1823. }
  1824. #endif /* MBEDTLS_ECP_C */
  1825. #if defined(MBEDTLS_SSL_ALPN)
  1826. if( opt.alpn_string != NULL )
  1827. {
  1828. p = (char *) opt.alpn_string;
  1829. i = 0;
  1830. /* Leave room for a final NULL in alpn_list */
  1831. while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
  1832. {
  1833. alpn_list[i++] = p;
  1834. /* Terminate the current string and move on to next one */
  1835. while( *p != ',' && *p != '\0' )
  1836. p++;
  1837. if( *p == ',' )
  1838. *p++ = '\0';
  1839. }
  1840. }
  1841. #endif /* MBEDTLS_SSL_ALPN */
  1842. /*
  1843. * 0. Initialize the RNG and the session data
  1844. */
  1845. mbedtls_printf( "\n . Seeding the random number generator..." );
  1846. fflush( stdout );
  1847. mbedtls_entropy_init( &entropy );
  1848. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
  1849. &entropy, (const unsigned char *) pers,
  1850. strlen( pers ) ) ) != 0 )
  1851. {
  1852. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
  1853. -ret );
  1854. goto exit;
  1855. }
  1856. mbedtls_printf( " ok\n" );
  1857. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1858. /*
  1859. * 1.1. Load the trusted CA
  1860. */
  1861. mbedtls_printf( " . Loading the CA root certificate ..." );
  1862. fflush( stdout );
  1863. if( strcmp( opt.ca_path, "none" ) == 0 ||
  1864. strcmp( opt.ca_file, "none" ) == 0 )
  1865. {
  1866. ret = 0;
  1867. }
  1868. else
  1869. #if defined(MBEDTLS_FS_IO)
  1870. if( strlen( opt.ca_path ) )
  1871. ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
  1872. else if( strlen( opt.ca_file ) )
  1873. ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
  1874. else
  1875. #endif
  1876. #if defined(MBEDTLS_CERTS_C)
  1877. {
  1878. #if defined(MBEDTLS_PEM_PARSE_C)
  1879. for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
  1880. {
  1881. ret = mbedtls_x509_crt_parse( &cacert,
  1882. (const unsigned char *) mbedtls_test_cas[i],
  1883. mbedtls_test_cas_len[i] );
  1884. if( ret != 0 )
  1885. break;
  1886. }
  1887. if( ret == 0 )
  1888. #endif /* MBEDTLS_PEM_PARSE_C */
  1889. for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
  1890. {
  1891. ret = mbedtls_x509_crt_parse_der( &cacert,
  1892. (const unsigned char *) mbedtls_test_cas_der[i],
  1893. mbedtls_test_cas_der_len[i] );
  1894. if( ret != 0 )
  1895. break;
  1896. }
  1897. }
  1898. #else
  1899. {
  1900. ret = 1;
  1901. mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
  1902. }
  1903. #endif /* MBEDTLS_CERTS_C */
  1904. if( ret < 0 )
  1905. {
  1906. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
  1907. goto exit;
  1908. }
  1909. mbedtls_printf( " ok (%d skipped)\n", ret );
  1910. /*
  1911. * 1.2. Load own certificate and private key
  1912. */
  1913. mbedtls_printf( " . Loading the server cert. and key..." );
  1914. fflush( stdout );
  1915. #if defined(MBEDTLS_FS_IO)
  1916. if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
  1917. {
  1918. key_cert_init++;
  1919. if( ( ret = mbedtls_x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
  1920. {
  1921. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
  1922. -ret );
  1923. goto exit;
  1924. }
  1925. }
  1926. if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
  1927. {
  1928. key_cert_init++;
  1929. if( ( ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
  1930. {
  1931. mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n", -ret );
  1932. goto exit;
  1933. }
  1934. }
  1935. if( key_cert_init == 1 )
  1936. {
  1937. mbedtls_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
  1938. goto exit;
  1939. }
  1940. if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
  1941. {
  1942. key_cert_init2++;
  1943. if( ( ret = mbedtls_x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
  1944. {
  1945. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n",
  1946. -ret );
  1947. goto exit;
  1948. }
  1949. }
  1950. if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
  1951. {
  1952. key_cert_init2++;
  1953. if( ( ret = mbedtls_pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
  1954. {
  1955. mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n",
  1956. -ret );
  1957. goto exit;
  1958. }
  1959. }
  1960. if( key_cert_init2 == 1 )
  1961. {
  1962. mbedtls_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
  1963. goto exit;
  1964. }
  1965. #endif
  1966. if( key_cert_init == 0 &&
  1967. strcmp( opt.crt_file, "none" ) != 0 &&
  1968. strcmp( opt.key_file, "none" ) != 0 &&
  1969. key_cert_init2 == 0 &&
  1970. strcmp( opt.crt_file2, "none" ) != 0 &&
  1971. strcmp( opt.key_file2, "none" ) != 0 )
  1972. {
  1973. #if !defined(MBEDTLS_CERTS_C)
  1974. mbedtls_printf( "Not certificated or key provided, and \nMBEDTLS_CERTS_C not defined!\n" );
  1975. goto exit;
  1976. #else
  1977. #if defined(MBEDTLS_RSA_C)
  1978. if( ( ret = mbedtls_x509_crt_parse( &srvcert,
  1979. (const unsigned char *) mbedtls_test_srv_crt_rsa,
  1980. mbedtls_test_srv_crt_rsa_len ) ) != 0 )
  1981. {
  1982. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
  1983. -ret );
  1984. goto exit;
  1985. }
  1986. if( ( ret = mbedtls_pk_parse_key( &pkey,
  1987. (const unsigned char *) mbedtls_test_srv_key_rsa,
  1988. mbedtls_test_srv_key_rsa_len, NULL, 0 ) ) != 0 )
  1989. {
  1990. mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
  1991. -ret );
  1992. goto exit;
  1993. }
  1994. key_cert_init = 2;
  1995. #endif /* MBEDTLS_RSA_C */
  1996. #if defined(MBEDTLS_ECDSA_C)
  1997. if( ( ret = mbedtls_x509_crt_parse( &srvcert2,
  1998. (const unsigned char *) mbedtls_test_srv_crt_ec,
  1999. mbedtls_test_srv_crt_ec_len ) ) != 0 )
  2000. {
  2001. mbedtls_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n",
  2002. -ret );
  2003. goto exit;
  2004. }
  2005. if( ( ret = mbedtls_pk_parse_key( &pkey2,
  2006. (const unsigned char *) mbedtls_test_srv_key_ec,
  2007. mbedtls_test_srv_key_ec_len, NULL, 0 ) ) != 0 )
  2008. {
  2009. mbedtls_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n",
  2010. -ret );
  2011. goto exit;
  2012. }
  2013. key_cert_init2 = 2;
  2014. #endif /* MBEDTLS_ECDSA_C */
  2015. #endif /* MBEDTLS_CERTS_C */
  2016. }
  2017. mbedtls_printf( " ok\n" );
  2018. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2019. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
  2020. if( opt.dhm_file != NULL )
  2021. {
  2022. mbedtls_printf( " . Loading DHM parameters..." );
  2023. fflush( stdout );
  2024. if( ( ret = mbedtls_dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
  2025. {
  2026. mbedtls_printf( " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n",
  2027. -ret );
  2028. goto exit;
  2029. }
  2030. mbedtls_printf( " ok\n" );
  2031. }
  2032. #endif
  2033. #if defined(SNI_OPTION)
  2034. if( opt.sni != NULL )
  2035. {
  2036. mbedtls_printf( " . Setting up SNI information..." );
  2037. fflush( stdout );
  2038. if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
  2039. {
  2040. mbedtls_printf( " failed\n" );
  2041. goto exit;
  2042. }
  2043. mbedtls_printf( " ok\n" );
  2044. }
  2045. #endif /* SNI_OPTION */
  2046. /*
  2047. * 2. Setup the listening TCP socket
  2048. */
  2049. mbedtls_printf( " . Bind on %s://%s:%s/ ...",
  2050. opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
  2051. opt.server_addr ? opt.server_addr : "*",
  2052. opt.server_port );
  2053. fflush( stdout );
  2054. if( ( ret = mbedtls_net_bind( &listen_fd, opt.server_addr, opt.server_port,
  2055. opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
  2056. MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
  2057. {
  2058. mbedtls_printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret );
  2059. goto exit;
  2060. }
  2061. mbedtls_printf( " ok\n" );
  2062. /*
  2063. * 3. Setup stuff
  2064. */
  2065. mbedtls_printf( " . Setting up the SSL/TLS structure..." );
  2066. fflush( stdout );
  2067. if( ( ret = mbedtls_ssl_config_defaults( &conf,
  2068. MBEDTLS_SSL_IS_SERVER,
  2069. opt.transport,
  2070. MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
  2071. {
  2072. mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
  2073. goto exit;
  2074. }
  2075. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2076. /* The default algorithms profile disables SHA-1, but our tests still
  2077. rely on it heavily. Hence we allow it here. A real-world server
  2078. should use the default profile unless there is a good reason not to. */
  2079. if( opt.allow_sha1 > 0 )
  2080. {
  2081. crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
  2082. mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
  2083. mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
  2084. }
  2085. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2086. if( opt.auth_mode != DFL_AUTH_MODE )
  2087. mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
  2088. if( opt.cert_req_ca_list != DFL_CERT_REQ_CA_LIST )
  2089. mbedtls_ssl_conf_cert_req_ca_list( &conf, opt.cert_req_ca_list );
  2090. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  2091. if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
  2092. mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
  2093. if( opt.dgram_packing != DFL_DGRAM_PACKING )
  2094. mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
  2095. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  2096. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  2097. if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
  2098. {
  2099. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
  2100. goto exit;
  2101. };
  2102. #endif
  2103. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  2104. if( opt.trunc_hmac != DFL_TRUNC_HMAC )
  2105. mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
  2106. #endif
  2107. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  2108. if( opt.extended_ms != DFL_EXTENDED_MS )
  2109. mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
  2110. #endif
  2111. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  2112. if( opt.etm != DFL_ETM )
  2113. mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
  2114. #endif
  2115. #if defined(MBEDTLS_SSL_ALPN)
  2116. if( opt.alpn_string != NULL )
  2117. if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
  2118. {
  2119. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
  2120. goto exit;
  2121. }
  2122. #endif
  2123. mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
  2124. mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
  2125. #if defined(MBEDTLS_SSL_CACHE_C)
  2126. if( opt.cache_max != -1 )
  2127. mbedtls_ssl_cache_set_max_entries( &cache, opt.cache_max );
  2128. if( opt.cache_timeout != -1 )
  2129. mbedtls_ssl_cache_set_timeout( &cache, opt.cache_timeout );
  2130. mbedtls_ssl_conf_session_cache( &conf, &cache,
  2131. mbedtls_ssl_cache_get,
  2132. mbedtls_ssl_cache_set );
  2133. #endif
  2134. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2135. if( opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED )
  2136. {
  2137. if( ( ret = mbedtls_ssl_ticket_setup( &ticket_ctx,
  2138. mbedtls_ctr_drbg_random, &ctr_drbg,
  2139. MBEDTLS_CIPHER_AES_256_GCM,
  2140. opt.ticket_timeout ) ) != 0 )
  2141. {
  2142. mbedtls_printf( " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret );
  2143. goto exit;
  2144. }
  2145. mbedtls_ssl_conf_session_tickets_cb( &conf,
  2146. mbedtls_ssl_ticket_write,
  2147. mbedtls_ssl_ticket_parse,
  2148. &ticket_ctx );
  2149. }
  2150. #endif
  2151. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  2152. if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  2153. {
  2154. #if defined(MBEDTLS_SSL_COOKIE_C)
  2155. if( opt.cookies > 0 )
  2156. {
  2157. if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
  2158. mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  2159. {
  2160. mbedtls_printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
  2161. goto exit;
  2162. }
  2163. mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
  2164. &cookie_ctx );
  2165. }
  2166. else
  2167. #endif /* MBEDTLS_SSL_COOKIE_C */
  2168. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  2169. if( opt.cookies == 0 )
  2170. {
  2171. mbedtls_ssl_conf_dtls_cookies( &conf, NULL, NULL, NULL );
  2172. }
  2173. else
  2174. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
  2175. {
  2176. ; /* Nothing to do */
  2177. }
  2178. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  2179. if( opt.anti_replay != DFL_ANTI_REPLAY )
  2180. mbedtls_ssl_conf_dtls_anti_replay( &conf, opt.anti_replay );
  2181. #endif
  2182. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  2183. if( opt.badmac_limit != DFL_BADMAC_LIMIT )
  2184. mbedtls_ssl_conf_dtls_badmac_limit( &conf, opt.badmac_limit );
  2185. #endif
  2186. }
  2187. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  2188. if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
  2189. mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
  2190. #if defined(MBEDTLS_ARC4_C)
  2191. if( opt.arc4 != DFL_ARC4 )
  2192. mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
  2193. #endif
  2194. if( opt.version_suites != NULL )
  2195. {
  2196. mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[0],
  2197. MBEDTLS_SSL_MAJOR_VERSION_3,
  2198. MBEDTLS_SSL_MINOR_VERSION_0 );
  2199. mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[1],
  2200. MBEDTLS_SSL_MAJOR_VERSION_3,
  2201. MBEDTLS_SSL_MINOR_VERSION_1 );
  2202. mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[2],
  2203. MBEDTLS_SSL_MAJOR_VERSION_3,
  2204. MBEDTLS_SSL_MINOR_VERSION_2 );
  2205. mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[3],
  2206. MBEDTLS_SSL_MAJOR_VERSION_3,
  2207. MBEDTLS_SSL_MINOR_VERSION_3 );
  2208. }
  2209. if( opt.allow_legacy != DFL_ALLOW_LEGACY )
  2210. mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
  2211. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  2212. mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
  2213. if( opt.renego_delay != DFL_RENEGO_DELAY )
  2214. mbedtls_ssl_conf_renegotiation_enforced( &conf, opt.renego_delay );
  2215. if( opt.renego_period != DFL_RENEGO_PERIOD )
  2216. {
  2217. PUT_UINT64_BE( renego_period, opt.renego_period, 0 );
  2218. mbedtls_ssl_conf_renegotiation_period( &conf, renego_period );
  2219. }
  2220. #endif
  2221. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2222. if( strcmp( opt.ca_path, "none" ) != 0 &&
  2223. strcmp( opt.ca_file, "none" ) != 0 )
  2224. {
  2225. mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
  2226. }
  2227. if( key_cert_init )
  2228. {
  2229. mbedtls_pk_context *pk = &pkey;
  2230. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2231. if( opt.async_private_delay1 >= 0 )
  2232. {
  2233. ret = ssl_async_set_key( &ssl_async_keys, &srvcert, pk, 0,
  2234. opt.async_private_delay1 );
  2235. if( ret < 0 )
  2236. {
  2237. mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
  2238. ret );
  2239. goto exit;
  2240. }
  2241. pk = NULL;
  2242. }
  2243. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  2244. if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, pk ) ) != 0 )
  2245. {
  2246. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
  2247. goto exit;
  2248. }
  2249. }
  2250. if( key_cert_init2 )
  2251. {
  2252. mbedtls_pk_context *pk = &pkey2;
  2253. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2254. if( opt.async_private_delay2 >= 0 )
  2255. {
  2256. ret = ssl_async_set_key( &ssl_async_keys, &srvcert2, pk, 0,
  2257. opt.async_private_delay2 );
  2258. if( ret < 0 )
  2259. {
  2260. mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
  2261. ret );
  2262. goto exit;
  2263. }
  2264. pk = NULL;
  2265. }
  2266. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  2267. if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, pk ) ) != 0 )
  2268. {
  2269. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
  2270. goto exit;
  2271. }
  2272. }
  2273. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2274. if( opt.async_operations[0] != '-' )
  2275. {
  2276. mbedtls_ssl_async_sign_t *sign = NULL;
  2277. mbedtls_ssl_async_decrypt_t *decrypt = NULL;
  2278. const char *r;
  2279. for( r = opt.async_operations; *r; r++ )
  2280. {
  2281. switch( *r )
  2282. {
  2283. case 'd':
  2284. decrypt = ssl_async_decrypt;
  2285. break;
  2286. case 's':
  2287. sign = ssl_async_sign;
  2288. break;
  2289. }
  2290. }
  2291. ssl_async_keys.inject_error = ( opt.async_private_error < 0 ?
  2292. - opt.async_private_error :
  2293. opt.async_private_error );
  2294. ssl_async_keys.f_rng = mbedtls_ctr_drbg_random;
  2295. ssl_async_keys.p_rng = &ctr_drbg;
  2296. mbedtls_ssl_conf_async_private_cb( &conf,
  2297. sign,
  2298. decrypt,
  2299. ssl_async_resume,
  2300. ssl_async_cancel,
  2301. &ssl_async_keys );
  2302. }
  2303. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  2304. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2305. #if defined(SNI_OPTION)
  2306. if( opt.sni != NULL )
  2307. {
  2308. mbedtls_ssl_conf_sni( &conf, sni_callback, sni_info );
  2309. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2310. if( opt.async_private_delay2 >= 0 )
  2311. {
  2312. sni_entry *cur;
  2313. for( cur = sni_info; cur != NULL; cur = cur->next )
  2314. {
  2315. ret = ssl_async_set_key( &ssl_async_keys,
  2316. cur->cert, cur->key, 1,
  2317. opt.async_private_delay2 );
  2318. if( ret < 0 )
  2319. {
  2320. mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
  2321. ret );
  2322. goto exit;
  2323. }
  2324. cur->key = NULL;
  2325. }
  2326. }
  2327. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  2328. }
  2329. #endif
  2330. #if defined(MBEDTLS_ECP_C)
  2331. if( opt.curves != NULL &&
  2332. strcmp( opt.curves, "default" ) != 0 )
  2333. {
  2334. mbedtls_ssl_conf_curves( &conf, curve_list );
  2335. }
  2336. #endif
  2337. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  2338. if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
  2339. {
  2340. ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
  2341. (const unsigned char *) opt.psk_identity,
  2342. strlen( opt.psk_identity ) );
  2343. if( ret != 0 )
  2344. {
  2345. mbedtls_printf( " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", - ret );
  2346. goto exit;
  2347. }
  2348. }
  2349. if( opt.psk_list != NULL )
  2350. mbedtls_ssl_conf_psk_cb( &conf, psk_callback, psk_info );
  2351. #endif
  2352. #if defined(MBEDTLS_DHM_C)
  2353. /*
  2354. * Use different group than default DHM group
  2355. */
  2356. #if defined(MBEDTLS_FS_IO)
  2357. if( opt.dhm_file != NULL )
  2358. ret = mbedtls_ssl_conf_dh_param_ctx( &conf, &dhm );
  2359. #endif
  2360. if( ret != 0 )
  2361. {
  2362. mbedtls_printf( " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", - ret );
  2363. goto exit;
  2364. }
  2365. #endif
  2366. if( opt.min_version != DFL_MIN_VERSION )
  2367. mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
  2368. if( opt.max_version != DFL_MIN_VERSION )
  2369. mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
  2370. if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
  2371. {
  2372. mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
  2373. goto exit;
  2374. }
  2375. if( opt.nbio == 2 )
  2376. mbedtls_ssl_set_bio( &ssl, &client_fd, my_send, my_recv, NULL );
  2377. else
  2378. mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv,
  2379. opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
  2380. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  2381. if( opt.dtls_mtu != DFL_DTLS_MTU )
  2382. mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
  2383. #endif
  2384. #if defined(MBEDTLS_TIMING_C)
  2385. mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
  2386. mbedtls_timing_get_delay );
  2387. #endif
  2388. mbedtls_printf( " ok\n" );
  2389. reset:
  2390. #if !defined(_WIN32)
  2391. if( received_sigterm )
  2392. {
  2393. mbedtls_printf( " interrupted by SIGTERM (not in net_accept())\n" );
  2394. if( ret == MBEDTLS_ERR_NET_INVALID_CONTEXT )
  2395. ret = 0;
  2396. goto exit;
  2397. }
  2398. #endif
  2399. if( ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
  2400. {
  2401. mbedtls_printf( " ! Client initiated reconnection from same port\n" );
  2402. goto handshake;
  2403. }
  2404. #ifdef MBEDTLS_ERROR_C
  2405. if( ret != 0 )
  2406. {
  2407. char error_buf[100];
  2408. mbedtls_strerror( ret, error_buf, 100 );
  2409. mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
  2410. }
  2411. #endif
  2412. mbedtls_net_free( &client_fd );
  2413. mbedtls_ssl_session_reset( &ssl );
  2414. /*
  2415. * 3. Wait until a client connects
  2416. */
  2417. mbedtls_printf( " . Waiting for a remote connection ..." );
  2418. fflush( stdout );
  2419. if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
  2420. client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
  2421. {
  2422. #if !defined(_WIN32)
  2423. if( received_sigterm )
  2424. {
  2425. mbedtls_printf( " interrupted by SIGTERM (in net_accept())\n" );
  2426. if( ret == MBEDTLS_ERR_NET_ACCEPT_FAILED )
  2427. ret = 0;
  2428. goto exit;
  2429. }
  2430. #endif
  2431. mbedtls_printf( " failed\n ! mbedtls_net_accept returned -0x%x\n\n", -ret );
  2432. goto exit;
  2433. }
  2434. if( opt.nbio > 0 )
  2435. ret = mbedtls_net_set_nonblock( &client_fd );
  2436. else
  2437. ret = mbedtls_net_set_block( &client_fd );
  2438. if( ret != 0 )
  2439. {
  2440. mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
  2441. goto exit;
  2442. }
  2443. mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
  2444. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  2445. if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  2446. {
  2447. if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
  2448. client_ip, cliip_len ) ) != 0 )
  2449. {
  2450. mbedtls_printf( " failed\n ! mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n",
  2451. -ret );
  2452. goto exit;
  2453. }
  2454. }
  2455. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
  2456. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2457. if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
  2458. {
  2459. if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
  2460. (const unsigned char *) opt.ecjpake_pw,
  2461. strlen( opt.ecjpake_pw ) ) ) != 0 )
  2462. {
  2463. mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
  2464. goto exit;
  2465. }
  2466. }
  2467. #endif
  2468. mbedtls_printf( " ok\n" );
  2469. /*
  2470. * 4. Handshake
  2471. */
  2472. handshake:
  2473. mbedtls_printf( " . Performing the SSL/TLS handshake..." );
  2474. fflush( stdout );
  2475. while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
  2476. {
  2477. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2478. if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS &&
  2479. ssl_async_keys.inject_error == SSL_ASYNC_INJECT_ERROR_CANCEL )
  2480. {
  2481. mbedtls_printf( " cancelling on injected error\n" );
  2482. break;
  2483. }
  2484. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  2485. if( ! mbedtls_status_is_ssl_in_progress( ret ) )
  2486. break;
  2487. /* For event-driven IO, wait for socket to become available */
  2488. if( opt.event == 1 /* level triggered IO */ )
  2489. {
  2490. #if defined(MBEDTLS_TIMING_C)
  2491. ret = idle( &client_fd, &timer, ret );
  2492. #else
  2493. ret = idle( &client_fd, ret );
  2494. #endif
  2495. if( ret != 0 )
  2496. goto reset;
  2497. }
  2498. }
  2499. if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
  2500. {
  2501. mbedtls_printf( " hello verification requested\n" );
  2502. ret = 0;
  2503. goto reset;
  2504. }
  2505. else if( ret != 0 )
  2506. {
  2507. mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
  2508. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2509. if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
  2510. {
  2511. char vrfy_buf[512];
  2512. flags = mbedtls_ssl_get_verify_result( &ssl );
  2513. mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
  2514. mbedtls_printf( "%s\n", vrfy_buf );
  2515. }
  2516. #endif
  2517. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2518. if( opt.async_private_error < 0 )
  2519. /* Injected error only the first time round, to test reset */
  2520. ssl_async_keys.inject_error = SSL_ASYNC_INJECT_ERROR_NONE;
  2521. #endif
  2522. goto reset;
  2523. }
  2524. else /* ret == 0 */
  2525. {
  2526. mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
  2527. mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
  2528. }
  2529. if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
  2530. mbedtls_printf( " [ Record expansion is %d ]\n", ret );
  2531. else
  2532. mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
  2533. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  2534. mbedtls_printf( " [ Maximum fragment length is %u ]\n",
  2535. (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
  2536. #endif
  2537. #if defined(MBEDTLS_SSL_ALPN)
  2538. if( opt.alpn_string != NULL )
  2539. {
  2540. const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
  2541. mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
  2542. alp ? alp : "(none)" );
  2543. }
  2544. #endif
  2545. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2546. /*
  2547. * 5. Verify the client certificate
  2548. */
  2549. mbedtls_printf( " . Verifying peer X.509 certificate..." );
  2550. if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
  2551. {
  2552. char vrfy_buf[512];
  2553. mbedtls_printf( " failed\n" );
  2554. mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
  2555. mbedtls_printf( "%s\n", vrfy_buf );
  2556. }
  2557. else
  2558. mbedtls_printf( " ok\n" );
  2559. if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
  2560. {
  2561. char crt_buf[512];
  2562. mbedtls_printf( " . Peer certificate information ...\n" );
  2563. mbedtls_x509_crt_info( crt_buf, sizeof( crt_buf ), " ",
  2564. mbedtls_ssl_get_peer_cert( &ssl ) );
  2565. mbedtls_printf( "%s\n", crt_buf );
  2566. }
  2567. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2568. if( opt.exchanges == 0 )
  2569. goto close_notify;
  2570. exchanges_left = opt.exchanges;
  2571. data_exchange:
  2572. /*
  2573. * 6. Read the HTTP Request
  2574. */
  2575. mbedtls_printf( " < Read from client:" );
  2576. fflush( stdout );
  2577. /*
  2578. * TLS and DTLS need different reading styles (stream vs datagram)
  2579. */
  2580. if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
  2581. {
  2582. do
  2583. {
  2584. int terminated = 0;
  2585. len = opt.buffer_size - 1;
  2586. memset( buf, 0, opt.buffer_size );
  2587. ret = mbedtls_ssl_read( &ssl, buf, len );
  2588. if( mbedtls_status_is_ssl_in_progress( ret ) )
  2589. {
  2590. if( opt.event == 1 /* level triggered IO */ )
  2591. {
  2592. #if defined(MBEDTLS_TIMING_C)
  2593. idle( &client_fd, &timer, ret );
  2594. #else
  2595. idle( &client_fd, ret );
  2596. #endif
  2597. }
  2598. continue;
  2599. }
  2600. if( ret <= 0 )
  2601. {
  2602. switch( ret )
  2603. {
  2604. case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
  2605. mbedtls_printf( " connection was closed gracefully\n" );
  2606. goto close_notify;
  2607. case 0:
  2608. case MBEDTLS_ERR_NET_CONN_RESET:
  2609. mbedtls_printf( " connection was reset by peer\n" );
  2610. ret = MBEDTLS_ERR_NET_CONN_RESET;
  2611. goto reset;
  2612. default:
  2613. mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
  2614. goto reset;
  2615. }
  2616. }
  2617. if( mbedtls_ssl_get_bytes_avail( &ssl ) == 0 )
  2618. {
  2619. len = ret;
  2620. buf[len] = '\0';
  2621. mbedtls_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
  2622. /* End of message should be detected according to the syntax of the
  2623. * application protocol (eg HTTP), just use a dummy test here. */
  2624. if( buf[len - 1] == '\n' )
  2625. terminated = 1;
  2626. }
  2627. else
  2628. {
  2629. int extra_len, ori_len;
  2630. unsigned char *larger_buf;
  2631. ori_len = ret;
  2632. extra_len = (int) mbedtls_ssl_get_bytes_avail( &ssl );
  2633. larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 );
  2634. if( larger_buf == NULL )
  2635. {
  2636. mbedtls_printf( " ! memory allocation failed\n" );
  2637. ret = 1;
  2638. goto reset;
  2639. }
  2640. memset( larger_buf, 0, ori_len + extra_len );
  2641. memcpy( larger_buf, buf, ori_len );
  2642. /* This read should never fail and get the whole cached data */
  2643. ret = mbedtls_ssl_read( &ssl, larger_buf + ori_len, extra_len );
  2644. if( ret != extra_len ||
  2645. mbedtls_ssl_get_bytes_avail( &ssl ) != 0 )
  2646. {
  2647. mbedtls_printf( " ! mbedtls_ssl_read failed on cached data\n" );
  2648. ret = 1;
  2649. goto reset;
  2650. }
  2651. larger_buf[ori_len + extra_len] = '\0';
  2652. mbedtls_printf( " %u bytes read (%u + %u)\n\n%s\n",
  2653. ori_len + extra_len, ori_len, extra_len,
  2654. (char *) larger_buf );
  2655. /* End of message should be detected according to the syntax of the
  2656. * application protocol (eg HTTP), just use a dummy test here. */
  2657. if( larger_buf[ori_len + extra_len - 1] == '\n' )
  2658. terminated = 1;
  2659. mbedtls_free( larger_buf );
  2660. }
  2661. if( terminated )
  2662. {
  2663. ret = 0;
  2664. break;
  2665. }
  2666. }
  2667. while( 1 );
  2668. }
  2669. else /* Not stream, so datagram */
  2670. {
  2671. len = opt.buffer_size - 1;
  2672. memset( buf, 0, opt.buffer_size );
  2673. do
  2674. {
  2675. /* Without the call to `mbedtls_ssl_check_pending`, it might
  2676. * happen that the client sends application data in the same
  2677. * datagram as the Finished message concluding the handshake.
  2678. * In this case, the application data would be ready to be
  2679. * processed while the underlying transport wouldn't signal
  2680. * any further incoming data.
  2681. *
  2682. * See the test 'Event-driven I/O: session-id resume, UDP packing'
  2683. * in tests/ssl-opt.sh.
  2684. */
  2685. /* For event-driven IO, wait for socket to become available */
  2686. if( mbedtls_ssl_check_pending( &ssl ) == 0 &&
  2687. opt.event == 1 /* level triggered IO */ )
  2688. {
  2689. #if defined(MBEDTLS_TIMING_C)
  2690. idle( &client_fd, &timer, MBEDTLS_ERR_SSL_WANT_READ );
  2691. #else
  2692. idle( &client_fd, MBEDTLS_ERR_SSL_WANT_READ );
  2693. #endif
  2694. }
  2695. ret = mbedtls_ssl_read( &ssl, buf, len );
  2696. /* Note that even if `mbedtls_ssl_check_pending` returns true,
  2697. * it can happen that the subsequent call to `mbedtls_ssl_read`
  2698. * returns `MBEDTLS_ERR_SSL_WANT_READ`, because the pending messages
  2699. * might be discarded (e.g. because they are retransmissions). */
  2700. }
  2701. while( mbedtls_status_is_ssl_in_progress( ret ) );
  2702. if( ret <= 0 )
  2703. {
  2704. switch( ret )
  2705. {
  2706. case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
  2707. mbedtls_printf( " connection was closed gracefully\n" );
  2708. ret = 0;
  2709. goto close_notify;
  2710. default:
  2711. mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
  2712. goto reset;
  2713. }
  2714. }
  2715. len = ret;
  2716. buf[len] = '\0';
  2717. mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
  2718. ret = 0;
  2719. }
  2720. /*
  2721. * 7a. Request renegotiation while client is waiting for input from us.
  2722. * (only on the first exchange, to be able to test retransmission)
  2723. */
  2724. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  2725. if( opt.renegotiate && exchanges_left == opt.exchanges )
  2726. {
  2727. mbedtls_printf( " . Requestion renegotiation..." );
  2728. fflush( stdout );
  2729. while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
  2730. {
  2731. if( ! mbedtls_status_is_ssl_in_progress( ret ) )
  2732. {
  2733. mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
  2734. goto reset;
  2735. }
  2736. /* For event-driven IO, wait for socket to become available */
  2737. if( opt.event == 1 /* level triggered IO */ )
  2738. {
  2739. #if defined(MBEDTLS_TIMING_C)
  2740. idle( &client_fd, &timer, ret );
  2741. #else
  2742. idle( &client_fd, ret );
  2743. #endif
  2744. }
  2745. }
  2746. mbedtls_printf( " ok\n" );
  2747. }
  2748. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  2749. /*
  2750. * 7. Write the 200 Response
  2751. */
  2752. mbedtls_printf( " > Write to client:" );
  2753. fflush( stdout );
  2754. len = sprintf( (char *) buf, HTTP_RESPONSE,
  2755. mbedtls_ssl_get_ciphersuite( &ssl ) );
  2756. /* Add padding to the response to reach opt.response_size in length */
  2757. if( opt.response_size != DFL_RESPONSE_SIZE &&
  2758. len < opt.response_size )
  2759. {
  2760. memset( buf + len, 'B', opt.response_size - len );
  2761. len += opt.response_size - len;
  2762. }
  2763. /* Truncate if response size is smaller than the "natural" size */
  2764. if( opt.response_size != DFL_RESPONSE_SIZE &&
  2765. len > opt.response_size )
  2766. {
  2767. len = opt.response_size;
  2768. /* Still end with \r\n unless that's really not possible */
  2769. if( len >= 2 ) buf[len - 2] = '\r';
  2770. if( len >= 1 ) buf[len - 1] = '\n';
  2771. }
  2772. if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
  2773. {
  2774. for( written = 0, frags = 0; written < len; written += ret, frags++ )
  2775. {
  2776. while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
  2777. <= 0 )
  2778. {
  2779. if( ret == MBEDTLS_ERR_NET_CONN_RESET )
  2780. {
  2781. mbedtls_printf( " failed\n ! peer closed the connection\n\n" );
  2782. goto reset;
  2783. }
  2784. if( ! mbedtls_status_is_ssl_in_progress( ret ) )
  2785. {
  2786. mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
  2787. goto reset;
  2788. }
  2789. /* For event-driven IO, wait for socket to become available */
  2790. if( opt.event == 1 /* level triggered IO */ )
  2791. {
  2792. #if defined(MBEDTLS_TIMING_C)
  2793. idle( &client_fd, &timer, ret );
  2794. #else
  2795. idle( &client_fd, ret );
  2796. #endif
  2797. }
  2798. }
  2799. }
  2800. }
  2801. else /* Not stream, so datagram */
  2802. {
  2803. while( 1 )
  2804. {
  2805. ret = mbedtls_ssl_write( &ssl, buf, len );
  2806. if( ! mbedtls_status_is_ssl_in_progress( ret ) )
  2807. break;
  2808. /* For event-driven IO, wait for socket to become available */
  2809. if( opt.event == 1 /* level triggered IO */ )
  2810. {
  2811. #if defined(MBEDTLS_TIMING_C)
  2812. idle( &client_fd, &timer, ret );
  2813. #else
  2814. idle( &client_fd, ret );
  2815. #endif
  2816. }
  2817. }
  2818. if( ret < 0 )
  2819. {
  2820. mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
  2821. goto reset;
  2822. }
  2823. frags = 1;
  2824. written = ret;
  2825. }
  2826. buf[written] = '\0';
  2827. mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
  2828. ret = 0;
  2829. /*
  2830. * 7b. Continue doing data exchanges?
  2831. */
  2832. if( --exchanges_left > 0 )
  2833. goto data_exchange;
  2834. /*
  2835. * 8. Done, cleanly close the connection
  2836. */
  2837. close_notify:
  2838. mbedtls_printf( " . Closing the connection..." );
  2839. /* No error checking, the connection might be closed already */
  2840. do ret = mbedtls_ssl_close_notify( &ssl );
  2841. while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  2842. ret = 0;
  2843. mbedtls_printf( " done\n" );
  2844. goto reset;
  2845. /*
  2846. * Cleanup and exit
  2847. */
  2848. exit:
  2849. #ifdef MBEDTLS_ERROR_C
  2850. if( ret != 0 )
  2851. {
  2852. char error_buf[100];
  2853. mbedtls_strerror( ret, error_buf, 100 );
  2854. mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
  2855. }
  2856. #endif
  2857. mbedtls_printf( " . Cleaning up..." );
  2858. fflush( stdout );
  2859. mbedtls_net_free( &client_fd );
  2860. mbedtls_net_free( &listen_fd );
  2861. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
  2862. mbedtls_dhm_free( &dhm );
  2863. #endif
  2864. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2865. mbedtls_x509_crt_free( &cacert );
  2866. mbedtls_x509_crt_free( &srvcert );
  2867. mbedtls_pk_free( &pkey );
  2868. mbedtls_x509_crt_free( &srvcert2 );
  2869. mbedtls_pk_free( &pkey2 );
  2870. #endif
  2871. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2872. for( i = 0; (size_t) i < ssl_async_keys.slots_used; i++ )
  2873. {
  2874. if( ssl_async_keys.slots[i].pk_owned )
  2875. {
  2876. mbedtls_pk_free( ssl_async_keys.slots[i].pk );
  2877. mbedtls_free( ssl_async_keys.slots[i].pk );
  2878. ssl_async_keys.slots[i].pk = NULL;
  2879. }
  2880. }
  2881. #endif
  2882. #if defined(SNI_OPTION)
  2883. sni_free( sni_info );
  2884. #endif
  2885. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  2886. psk_free( psk_info );
  2887. #endif
  2888. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
  2889. mbedtls_dhm_free( &dhm );
  2890. #endif
  2891. mbedtls_ssl_free( &ssl );
  2892. mbedtls_ssl_config_free( &conf );
  2893. mbedtls_ctr_drbg_free( &ctr_drbg );
  2894. mbedtls_entropy_free( &entropy );
  2895. #if defined(MBEDTLS_SSL_CACHE_C)
  2896. mbedtls_ssl_cache_free( &cache );
  2897. #endif
  2898. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2899. mbedtls_ssl_ticket_free( &ticket_ctx );
  2900. #endif
  2901. #if defined(MBEDTLS_SSL_COOKIE_C)
  2902. mbedtls_ssl_cookie_free( &cookie_ctx );
  2903. #endif
  2904. mbedtls_free( buf );
  2905. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  2906. #if defined(MBEDTLS_MEMORY_DEBUG)
  2907. mbedtls_memory_buffer_alloc_status();
  2908. #endif
  2909. mbedtls_memory_buffer_alloc_free();
  2910. #endif
  2911. mbedtls_printf( " done.\n" );
  2912. #if defined(_WIN32)
  2913. mbedtls_printf( " + Press Enter to exit this program.\n" );
  2914. fflush( stdout ); getchar();
  2915. #endif
  2916. // Shell can not handle large exit numbers -> 1 for errors
  2917. if( ret < 0 )
  2918. ret = 1;
  2919. mbedtls_exit( ret );
  2920. }
  2921. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
  2922. MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
  2923. MBEDTLS_CTR_DRBG_C */