ssl_server2.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361
  1. /*
  2. * SSL client with options
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_PLATFORM_C)
  27. #include "mbedtls/platform.h"
  28. #else
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #define mbedtls_free free
  32. #define mbedtls_time time
  33. #define mbedtls_time_t time_t
  34. #define mbedtls_calloc calloc
  35. #define mbedtls_fprintf fprintf
  36. #define mbedtls_printf printf
  37. #endif
  38. #if !defined(MBEDTLS_ENTROPY_C) || \
  39. !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_SRV_C) || \
  40. !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
  41. int main( void )
  42. {
  43. mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
  44. "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
  45. "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
  46. return( 0 );
  47. }
  48. #else
  49. #include "mbedtls/net_sockets.h"
  50. #include "mbedtls/ssl.h"
  51. #include "mbedtls/entropy.h"
  52. #include "mbedtls/ctr_drbg.h"
  53. #include "mbedtls/certs.h"
  54. #include "mbedtls/x509.h"
  55. #include "mbedtls/error.h"
  56. #include "mbedtls/debug.h"
  57. #include "mbedtls/timing.h"
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include <stdint.h>
  62. #if !defined(_MSC_VER)
  63. #include <inttypes.h>
  64. #endif
  65. #if !defined(_WIN32)
  66. #include <signal.h>
  67. #endif
  68. #if defined(MBEDTLS_SSL_CACHE_C)
  69. #include "mbedtls/ssl_cache.h"
  70. #endif
  71. #if defined(MBEDTLS_SSL_TICKET_C)
  72. #include "mbedtls/ssl_ticket.h"
  73. #endif
  74. #if defined(MBEDTLS_SSL_COOKIE_C)
  75. #include "mbedtls/ssl_cookie.h"
  76. #endif
  77. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  78. #include "mbedtls/memory_buffer_alloc.h"
  79. #endif
  80. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_FS_IO)
  81. #define SNI_OPTION
  82. #endif
  83. #if defined(_WIN32)
  84. #include <windows.h>
  85. #endif
  86. #define DFL_SERVER_ADDR NULL
  87. #define DFL_SERVER_PORT "4433"
  88. #define DFL_DEBUG_LEVEL 0
  89. #define DFL_NBIO 0
  90. #define DFL_READ_TIMEOUT 0
  91. #define DFL_CA_FILE ""
  92. #define DFL_CA_PATH ""
  93. #define DFL_CRT_FILE ""
  94. #define DFL_KEY_FILE ""
  95. #define DFL_CRT_FILE2 ""
  96. #define DFL_KEY_FILE2 ""
  97. #define DFL_PSK ""
  98. #define DFL_PSK_IDENTITY "Client_identity"
  99. #define DFL_ECJPAKE_PW NULL
  100. #define DFL_PSK_LIST NULL
  101. #define DFL_FORCE_CIPHER 0
  102. #define DFL_VERSION_SUITES NULL
  103. #define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
  104. #define DFL_ALLOW_LEGACY -2
  105. #define DFL_RENEGOTIATE 0
  106. #define DFL_RENEGO_DELAY -2
  107. #define DFL_RENEGO_PERIOD ( (uint64_t)-1 )
  108. #define DFL_EXCHANGES 1
  109. #define DFL_MIN_VERSION -1
  110. #define DFL_MAX_VERSION -1
  111. #define DFL_ARC4 -1
  112. #define DFL_AUTH_MODE -1
  113. #define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
  114. #define DFL_TRUNC_HMAC -1
  115. #define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
  116. #define DFL_TICKET_TIMEOUT 86400
  117. #define DFL_CACHE_MAX -1
  118. #define DFL_CACHE_TIMEOUT -1
  119. #define DFL_SNI NULL
  120. #define DFL_ALPN_STRING NULL
  121. #define DFL_DHM_FILE NULL
  122. #define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
  123. #define DFL_COOKIES 1
  124. #define DFL_ANTI_REPLAY -1
  125. #define DFL_HS_TO_MIN 0
  126. #define DFL_HS_TO_MAX 0
  127. #define DFL_BADMAC_LIMIT -1
  128. #define DFL_EXTENDED_MS -1
  129. #define DFL_ETM -1
  130. #define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  131. "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  132. "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  133. "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  134. "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  135. "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  136. "07-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah</p>\r\n"
  137. /* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
  138. * packets (for fragmentation purposes) */
  139. #define HTTP_RESPONSE \
  140. "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
  141. "<h2>mbed TLS Test Server</h2>\r\n" \
  142. "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
  143. /*
  144. * Size of the basic I/O buffer. Able to hold our default response.
  145. *
  146. * You will need to adapt the mbedtls_ssl_get_bytes_avail() test in ssl-opt.sh
  147. * if you change this value to something outside the range <= 100 or > 500
  148. */
  149. #define IO_BUF_LEN 200
  150. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  151. #if defined(MBEDTLS_FS_IO)
  152. #define USAGE_IO \
  153. " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
  154. " default: \"\" (pre-loaded)\n" \
  155. " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
  156. " default: \"\" (pre-loaded) (overrides ca_file)\n" \
  157. " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
  158. " default: see note after key_file2\n" \
  159. " key_file=%%s default: see note after key_file2\n" \
  160. " crt_file2=%%s Your second cert and chain (in bottom to top order, top may be omitted)\n" \
  161. " default: see note after key_file2\n" \
  162. " key_file2=%%s default: see note below\n" \
  163. " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
  164. " preloaded certificate(s) and key(s) are used if available\n" \
  165. " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
  166. " default: preloaded parameters\n"
  167. #else
  168. #define USAGE_IO \
  169. "\n" \
  170. " No file operations available (MBEDTLS_FS_IO not defined)\n" \
  171. "\n"
  172. #endif /* MBEDTLS_FS_IO */
  173. #else
  174. #define USAGE_IO ""
  175. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  176. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  177. #define USAGE_PSK \
  178. " psk=%%s default: \"\" (in hex, without 0x)\n" \
  179. " psk_identity=%%s default: \"Client_identity\"\n"
  180. #else
  181. #define USAGE_PSK ""
  182. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  183. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  184. #define USAGE_TICKETS \
  185. " tickets=%%d default: 1 (enabled)\n" \
  186. " ticket_timeout=%%d default: 86400 (one day)\n"
  187. #else
  188. #define USAGE_TICKETS ""
  189. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  190. #if defined(MBEDTLS_SSL_CACHE_C)
  191. #define USAGE_CACHE \
  192. " cache_max=%%d default: cache default (50)\n" \
  193. " cache_timeout=%%d default: cache default (1d)\n"
  194. #else
  195. #define USAGE_CACHE ""
  196. #endif /* MBEDTLS_SSL_CACHE_C */
  197. #if defined(SNI_OPTION)
  198. #define USAGE_SNI \
  199. " sni=%%s name1,cert1,key1,ca1,crl1,auth1[,...]\n" \
  200. " default: disabled\n"
  201. #else
  202. #define USAGE_SNI ""
  203. #endif /* SNI_OPTION */
  204. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  205. #define USAGE_MAX_FRAG_LEN \
  206. " max_frag_len=%%d default: 16384 (tls default)\n" \
  207. " options: 512, 1024, 2048, 4096\n"
  208. #else
  209. #define USAGE_MAX_FRAG_LEN ""
  210. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  211. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  212. #define USAGE_TRUNC_HMAC \
  213. " trunc_hmac=%%d default: library default\n"
  214. #else
  215. #define USAGE_TRUNC_HMAC ""
  216. #endif
  217. #if defined(MBEDTLS_SSL_ALPN)
  218. #define USAGE_ALPN \
  219. " alpn=%%s default: \"\" (disabled)\n" \
  220. " example: spdy/1,http/1.1\n"
  221. #else
  222. #define USAGE_ALPN ""
  223. #endif /* MBEDTLS_SSL_ALPN */
  224. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  225. #define USAGE_COOKIES \
  226. " cookies=0/1/-1 default: 1 (enabled)\n" \
  227. " 0: disabled, -1: library default (broken)\n"
  228. #else
  229. #define USAGE_COOKIES ""
  230. #endif
  231. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  232. #define USAGE_ANTI_REPLAY \
  233. " anti_replay=0/1 default: (library default: enabled)\n"
  234. #else
  235. #define USAGE_ANTI_REPLAY ""
  236. #endif
  237. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  238. #define USAGE_BADMAC_LIMIT \
  239. " badmac_limit=%%d default: (library default: disabled)\n"
  240. #else
  241. #define USAGE_BADMAC_LIMIT ""
  242. #endif
  243. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  244. #define USAGE_DTLS \
  245. " dtls=%%d default: 0 (TLS)\n" \
  246. " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
  247. " range of DTLS handshake timeouts in millisecs\n"
  248. #else
  249. #define USAGE_DTLS ""
  250. #endif
  251. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  252. #define USAGE_EMS \
  253. " extended_ms=0/1 default: (library default: on)\n"
  254. #else
  255. #define USAGE_EMS ""
  256. #endif
  257. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  258. #define USAGE_ETM \
  259. " etm=0/1 default: (library default: on)\n"
  260. #else
  261. #define USAGE_ETM ""
  262. #endif
  263. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  264. #define USAGE_RENEGO \
  265. " renegotiation=%%d default: 0 (disabled)\n" \
  266. " renegotiate=%%d default: 0 (disabled)\n" \
  267. " renego_delay=%%d default: -2 (library default)\n" \
  268. " renego_period=%%d default: (2^64 - 1 for TLS, 2^48 - 1 for DTLS)\n"
  269. #else
  270. #define USAGE_RENEGO ""
  271. #endif
  272. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  273. #define USAGE_ECJPAKE \
  274. " ecjpake_pw=%%s default: none (disabled)\n"
  275. #else
  276. #define USAGE_ECJPAKE ""
  277. #endif
  278. #define USAGE \
  279. "\n usage: ssl_server2 param=<>...\n" \
  280. "\n acceptable parameters:\n" \
  281. " server_addr=%%d default: (all interfaces)\n" \
  282. " server_port=%%d default: 4433\n" \
  283. " debug_level=%%d default: 0 (disabled)\n" \
  284. " nbio=%%d default: 0 (blocking I/O)\n" \
  285. " options: 1 (non-blocking), 2 (added delays)\n" \
  286. " read_timeout=%%d default: 0 ms (no timeout)\n" \
  287. "\n" \
  288. USAGE_DTLS \
  289. USAGE_COOKIES \
  290. USAGE_ANTI_REPLAY \
  291. USAGE_BADMAC_LIMIT \
  292. "\n" \
  293. " auth_mode=%%s default: (library default: none)\n" \
  294. " options: none, optional, required\n" \
  295. USAGE_IO \
  296. USAGE_SNI \
  297. "\n" \
  298. USAGE_PSK \
  299. USAGE_ECJPAKE \
  300. "\n" \
  301. " allow_legacy=%%d default: (library default: no)\n" \
  302. USAGE_RENEGO \
  303. " exchanges=%%d default: 1\n" \
  304. "\n" \
  305. USAGE_TICKETS \
  306. USAGE_CACHE \
  307. USAGE_MAX_FRAG_LEN \
  308. USAGE_TRUNC_HMAC \
  309. USAGE_ALPN \
  310. USAGE_EMS \
  311. USAGE_ETM \
  312. "\n" \
  313. " arc4=%%d default: (library default: 0)\n" \
  314. " min_version=%%s default: (library default: tls1)\n" \
  315. " max_version=%%s default: (library default: tls1_2)\n" \
  316. " force_version=%%s default: \"\" (none)\n" \
  317. " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
  318. "\n" \
  319. " version_suites=a,b,c,d per-version ciphersuites\n" \
  320. " in order from ssl3 to tls1_2\n" \
  321. " default: all enabled\n" \
  322. " force_ciphersuite=<name> default: all enabled\n" \
  323. " acceptable ciphersuite names:\n"
  324. #define PUT_UINT64_BE(out_be,in_le,i) \
  325. { \
  326. (out_be)[(i) + 0] = (unsigned char)( ( (in_le) >> 56 ) & 0xFF ); \
  327. (out_be)[(i) + 1] = (unsigned char)( ( (in_le) >> 48 ) & 0xFF ); \
  328. (out_be)[(i) + 2] = (unsigned char)( ( (in_le) >> 40 ) & 0xFF ); \
  329. (out_be)[(i) + 3] = (unsigned char)( ( (in_le) >> 32 ) & 0xFF ); \
  330. (out_be)[(i) + 4] = (unsigned char)( ( (in_le) >> 24 ) & 0xFF ); \
  331. (out_be)[(i) + 5] = (unsigned char)( ( (in_le) >> 16 ) & 0xFF ); \
  332. (out_be)[(i) + 6] = (unsigned char)( ( (in_le) >> 8 ) & 0xFF ); \
  333. (out_be)[(i) + 7] = (unsigned char)( ( (in_le) >> 0 ) & 0xFF ); \
  334. }
  335. /*
  336. * global options
  337. */
  338. struct options
  339. {
  340. const char *server_addr; /* address on which the ssl service runs */
  341. const char *server_port; /* port on which the ssl service runs */
  342. int debug_level; /* level of debugging */
  343. int nbio; /* should I/O be blocking? */
  344. uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
  345. const char *ca_file; /* the file with the CA certificate(s) */
  346. const char *ca_path; /* the path with the CA certificate(s) reside */
  347. const char *crt_file; /* the file with the server certificate */
  348. const char *key_file; /* the file with the server key */
  349. const char *crt_file2; /* the file with the 2nd server certificate */
  350. const char *key_file2; /* the file with the 2nd server key */
  351. const char *psk; /* the pre-shared key */
  352. const char *psk_identity; /* the pre-shared key identity */
  353. char *psk_list; /* list of PSK id/key pairs for callback */
  354. const char *ecjpake_pw; /* the EC J-PAKE password */
  355. int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
  356. const char *version_suites; /* per-version ciphersuites */
  357. int renegotiation; /* enable / disable renegotiation */
  358. int allow_legacy; /* allow legacy renegotiation */
  359. int renegotiate; /* attempt renegotiation? */
  360. int renego_delay; /* delay before enforcing renegotiation */
  361. uint64_t renego_period; /* period for automatic renegotiation */
  362. int exchanges; /* number of data exchanges */
  363. int min_version; /* minimum protocol version accepted */
  364. int max_version; /* maximum protocol version accepted */
  365. int arc4; /* flag for arc4 suites support */
  366. int auth_mode; /* verify mode for connection */
  367. unsigned char mfl_code; /* code for maximum fragment length */
  368. int trunc_hmac; /* accept truncated hmac? */
  369. int tickets; /* enable / disable session tickets */
  370. int ticket_timeout; /* session ticket lifetime */
  371. int cache_max; /* max number of session cache entries */
  372. int cache_timeout; /* expiration delay of session cache entries */
  373. char *sni; /* string describing sni information */
  374. const char *alpn_string; /* ALPN supported protocols */
  375. const char *dhm_file; /* the file with the DH parameters */
  376. int extended_ms; /* allow negotiation of extended MS? */
  377. int etm; /* allow negotiation of encrypt-then-MAC? */
  378. int transport; /* TLS or DTLS? */
  379. int cookies; /* Use cookies for DTLS? -1 to break them */
  380. int anti_replay; /* Use anti-replay for DTLS? -1 for default */
  381. uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
  382. uint32_t hs_to_max; /* Max value of DTLS handshake timer */
  383. int badmac_limit; /* Limit of records with bad MAC */
  384. } opt;
  385. static void my_debug( void *ctx, int level,
  386. const char *file, int line,
  387. const char *str )
  388. {
  389. const char *p, *basename;
  390. /* Extract basename from file */
  391. for( p = basename = file; *p != '\0'; p++ )
  392. if( *p == '/' || *p == '\\' )
  393. basename = p + 1;
  394. mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", basename, line, level, str );
  395. fflush( (FILE *) ctx );
  396. }
  397. /*
  398. * Test recv/send functions that make sure each try returns
  399. * WANT_READ/WANT_WRITE at least once before sucesseding
  400. */
  401. static int my_recv( void *ctx, unsigned char *buf, size_t len )
  402. {
  403. static int first_try = 1;
  404. int ret;
  405. if( first_try )
  406. {
  407. first_try = 0;
  408. return( MBEDTLS_ERR_SSL_WANT_READ );
  409. }
  410. ret = mbedtls_net_recv( ctx, buf, len );
  411. if( ret != MBEDTLS_ERR_SSL_WANT_READ )
  412. first_try = 1; /* Next call will be a new operation */
  413. return( ret );
  414. }
  415. static int my_send( void *ctx, const unsigned char *buf, size_t len )
  416. {
  417. static int first_try = 1;
  418. int ret;
  419. if( first_try )
  420. {
  421. first_try = 0;
  422. return( MBEDTLS_ERR_SSL_WANT_WRITE );
  423. }
  424. ret = mbedtls_net_send( ctx, buf, len );
  425. if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  426. first_try = 1; /* Next call will be a new operation */
  427. return( ret );
  428. }
  429. /*
  430. * Return authmode from string, or -1 on error
  431. */
  432. static int get_auth_mode( const char *s )
  433. {
  434. if( strcmp( s, "none" ) == 0 )
  435. return( MBEDTLS_SSL_VERIFY_NONE );
  436. if( strcmp( s, "optional" ) == 0 )
  437. return( MBEDTLS_SSL_VERIFY_OPTIONAL );
  438. if( strcmp( s, "required" ) == 0 )
  439. return( MBEDTLS_SSL_VERIFY_REQUIRED );
  440. return( -1 );
  441. }
  442. /*
  443. * Used by sni_parse and psk_parse to handle coma-separated lists
  444. */
  445. #define GET_ITEM( dst ) \
  446. dst = p; \
  447. while( *p != ',' ) \
  448. if( ++p > end ) \
  449. goto error; \
  450. *p++ = '\0';
  451. #if defined(SNI_OPTION)
  452. typedef struct _sni_entry sni_entry;
  453. struct _sni_entry {
  454. const char *name;
  455. mbedtls_x509_crt *cert;
  456. mbedtls_pk_context *key;
  457. mbedtls_x509_crt* ca;
  458. mbedtls_x509_crl* crl;
  459. int authmode;
  460. sni_entry *next;
  461. };
  462. void sni_free( sni_entry *head )
  463. {
  464. sni_entry *cur = head, *next;
  465. while( cur != NULL )
  466. {
  467. mbedtls_x509_crt_free( cur->cert );
  468. mbedtls_free( cur->cert );
  469. mbedtls_pk_free( cur->key );
  470. mbedtls_free( cur->key );
  471. mbedtls_x509_crt_free( cur->ca );
  472. mbedtls_free( cur->ca );
  473. mbedtls_x509_crl_free( cur->crl );
  474. mbedtls_free( cur->crl );
  475. next = cur->next;
  476. mbedtls_free( cur );
  477. cur = next;
  478. }
  479. }
  480. /*
  481. * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...]
  482. * into a usable sni_entry list. For ca1, crl1, auth1, the special value
  483. * '-' means unset. If ca1 is unset, then crl1 is ignored too.
  484. *
  485. * Modifies the input string! This is not production quality!
  486. */
  487. sni_entry *sni_parse( char *sni_string )
  488. {
  489. sni_entry *cur = NULL, *new = NULL;
  490. char *p = sni_string;
  491. char *end = p;
  492. char *crt_file, *key_file, *ca_file, *crl_file, *auth_str;
  493. while( *end != '\0' )
  494. ++end;
  495. *end = ',';
  496. while( p <= end )
  497. {
  498. if( ( new = mbedtls_calloc( 1, sizeof( sni_entry ) ) ) == NULL )
  499. {
  500. sni_free( cur );
  501. return( NULL );
  502. }
  503. GET_ITEM( new->name );
  504. GET_ITEM( crt_file );
  505. GET_ITEM( key_file );
  506. GET_ITEM( ca_file );
  507. GET_ITEM( crl_file );
  508. GET_ITEM( auth_str );
  509. if( ( new->cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL ||
  510. ( new->key = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) ) ) == NULL )
  511. goto error;
  512. mbedtls_x509_crt_init( new->cert );
  513. mbedtls_pk_init( new->key );
  514. if( mbedtls_x509_crt_parse_file( new->cert, crt_file ) != 0 ||
  515. mbedtls_pk_parse_keyfile( new->key, key_file, "" ) != 0 )
  516. goto error;
  517. if( strcmp( ca_file, "-" ) != 0 )
  518. {
  519. if( ( new->ca = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
  520. goto error;
  521. mbedtls_x509_crt_init( new->ca );
  522. if( mbedtls_x509_crt_parse_file( new->ca, ca_file ) != 0 )
  523. goto error;
  524. }
  525. if( strcmp( crl_file, "-" ) != 0 )
  526. {
  527. if( ( new->crl = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) ) ) == NULL )
  528. goto error;
  529. mbedtls_x509_crl_init( new->crl );
  530. if( mbedtls_x509_crl_parse_file( new->crl, crl_file ) != 0 )
  531. goto error;
  532. }
  533. if( strcmp( auth_str, "-" ) != 0 )
  534. {
  535. if( ( new->authmode = get_auth_mode( auth_str ) ) < 0 )
  536. goto error;
  537. }
  538. else
  539. new->authmode = DFL_AUTH_MODE;
  540. new->next = cur;
  541. cur = new;
  542. }
  543. return( cur );
  544. error:
  545. sni_free( new );
  546. sni_free( cur );
  547. return( NULL );
  548. }
  549. /*
  550. * SNI callback.
  551. */
  552. int sni_callback( void *p_info, mbedtls_ssl_context *ssl,
  553. const unsigned char *name, size_t name_len )
  554. {
  555. const sni_entry *cur = (const sni_entry *) p_info;
  556. while( cur != NULL )
  557. {
  558. if( name_len == strlen( cur->name ) &&
  559. memcmp( name, cur->name, name_len ) == 0 )
  560. {
  561. if( cur->ca != NULL )
  562. mbedtls_ssl_set_hs_ca_chain( ssl, cur->ca, cur->crl );
  563. if( cur->authmode != DFL_AUTH_MODE )
  564. mbedtls_ssl_set_hs_authmode( ssl, cur->authmode );
  565. return( mbedtls_ssl_set_hs_own_cert( ssl, cur->cert, cur->key ) );
  566. }
  567. cur = cur->next;
  568. }
  569. return( -1 );
  570. }
  571. #endif /* SNI_OPTION */
  572. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  573. #define HEX2NUM( c ) \
  574. if( c >= '0' && c <= '9' ) \
  575. c -= '0'; \
  576. else if( c >= 'a' && c <= 'f' ) \
  577. c -= 'a' - 10; \
  578. else if( c >= 'A' && c <= 'F' ) \
  579. c -= 'A' - 10; \
  580. else \
  581. return( -1 );
  582. /*
  583. * Convert a hex string to bytes.
  584. * Return 0 on success, -1 on error.
  585. */
  586. int unhexify( unsigned char *output, const char *input, size_t *olen )
  587. {
  588. unsigned char c;
  589. size_t j;
  590. *olen = strlen( input );
  591. if( *olen % 2 != 0 || *olen / 2 > MBEDTLS_PSK_MAX_LEN )
  592. return( -1 );
  593. *olen /= 2;
  594. for( j = 0; j < *olen * 2; j += 2 )
  595. {
  596. c = input[j];
  597. HEX2NUM( c );
  598. output[ j / 2 ] = c << 4;
  599. c = input[j + 1];
  600. HEX2NUM( c );
  601. output[ j / 2 ] |= c;
  602. }
  603. return( 0 );
  604. }
  605. typedef struct _psk_entry psk_entry;
  606. struct _psk_entry
  607. {
  608. const char *name;
  609. size_t key_len;
  610. unsigned char key[MBEDTLS_PSK_MAX_LEN];
  611. psk_entry *next;
  612. };
  613. /*
  614. * Free a list of psk_entry's
  615. */
  616. void psk_free( psk_entry *head )
  617. {
  618. psk_entry *next;
  619. while( head != NULL )
  620. {
  621. next = head->next;
  622. mbedtls_free( head );
  623. head = next;
  624. }
  625. }
  626. /*
  627. * Parse a string of pairs name1,key1[,name2,key2[,...]]
  628. * into a usable psk_entry list.
  629. *
  630. * Modifies the input string! This is not production quality!
  631. */
  632. psk_entry *psk_parse( char *psk_string )
  633. {
  634. psk_entry *cur = NULL, *new = NULL;
  635. char *p = psk_string;
  636. char *end = p;
  637. char *key_hex;
  638. while( *end != '\0' )
  639. ++end;
  640. *end = ',';
  641. while( p <= end )
  642. {
  643. if( ( new = mbedtls_calloc( 1, sizeof( psk_entry ) ) ) == NULL )
  644. goto error;
  645. memset( new, 0, sizeof( psk_entry ) );
  646. GET_ITEM( new->name );
  647. GET_ITEM( key_hex );
  648. if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
  649. goto error;
  650. new->next = cur;
  651. cur = new;
  652. }
  653. return( cur );
  654. error:
  655. psk_free( new );
  656. psk_free( cur );
  657. return( 0 );
  658. }
  659. /*
  660. * PSK callback
  661. */
  662. int psk_callback( void *p_info, mbedtls_ssl_context *ssl,
  663. const unsigned char *name, size_t name_len )
  664. {
  665. psk_entry *cur = (psk_entry *) p_info;
  666. while( cur != NULL )
  667. {
  668. if( name_len == strlen( cur->name ) &&
  669. memcmp( name, cur->name, name_len ) == 0 )
  670. {
  671. return( mbedtls_ssl_set_hs_psk( ssl, cur->key, cur->key_len ) );
  672. }
  673. cur = cur->next;
  674. }
  675. return( -1 );
  676. }
  677. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  678. static mbedtls_net_context listen_fd, client_fd;
  679. /* Interruption handler to ensure clean exit (for valgrind testing) */
  680. #if !defined(_WIN32)
  681. static int received_sigterm = 0;
  682. void term_handler( int sig )
  683. {
  684. ((void) sig);
  685. received_sigterm = 1;
  686. mbedtls_net_free( &listen_fd ); /* causes mbedtls_net_accept() to abort */
  687. mbedtls_net_free( &client_fd ); /* causes net_read() to abort */
  688. }
  689. #endif
  690. int main( int argc, char *argv[] )
  691. {
  692. int ret = 0, len, written, frags, exchanges_left;
  693. int version_suites[4][2];
  694. unsigned char buf[IO_BUF_LEN];
  695. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  696. unsigned char psk[MBEDTLS_PSK_MAX_LEN];
  697. size_t psk_len = 0;
  698. psk_entry *psk_info = NULL;
  699. #endif
  700. const char *pers = "ssl_server2";
  701. unsigned char client_ip[16] = { 0 };
  702. size_t cliip_len;
  703. #if defined(MBEDTLS_SSL_COOKIE_C)
  704. mbedtls_ssl_cookie_ctx cookie_ctx;
  705. #endif
  706. mbedtls_entropy_context entropy;
  707. mbedtls_ctr_drbg_context ctr_drbg;
  708. mbedtls_ssl_context ssl;
  709. mbedtls_ssl_config conf;
  710. #if defined(MBEDTLS_TIMING_C)
  711. mbedtls_timing_delay_context timer;
  712. #endif
  713. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  714. unsigned char renego_period[8] = { 0 };
  715. #endif
  716. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  717. uint32_t flags;
  718. mbedtls_x509_crt cacert;
  719. mbedtls_x509_crt srvcert;
  720. mbedtls_pk_context pkey;
  721. mbedtls_x509_crt srvcert2;
  722. mbedtls_pk_context pkey2;
  723. int key_cert_init = 0, key_cert_init2 = 0;
  724. #endif
  725. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
  726. mbedtls_dhm_context dhm;
  727. #endif
  728. #if defined(MBEDTLS_SSL_CACHE_C)
  729. mbedtls_ssl_cache_context cache;
  730. #endif
  731. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  732. mbedtls_ssl_ticket_context ticket_ctx;
  733. #endif
  734. #if defined(SNI_OPTION)
  735. sni_entry *sni_info = NULL;
  736. #endif
  737. #if defined(MBEDTLS_SSL_ALPN)
  738. const char *alpn_list[10];
  739. #endif
  740. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  741. unsigned char alloc_buf[100000];
  742. #endif
  743. int i;
  744. char *p, *q;
  745. const int *list;
  746. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  747. mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
  748. #endif
  749. /*
  750. * Make sure memory references are valid in case we exit early.
  751. */
  752. mbedtls_net_init( &client_fd );
  753. mbedtls_net_init( &listen_fd );
  754. mbedtls_ssl_init( &ssl );
  755. mbedtls_ssl_config_init( &conf );
  756. mbedtls_ctr_drbg_init( &ctr_drbg );
  757. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  758. mbedtls_x509_crt_init( &cacert );
  759. mbedtls_x509_crt_init( &srvcert );
  760. mbedtls_pk_init( &pkey );
  761. mbedtls_x509_crt_init( &srvcert2 );
  762. mbedtls_pk_init( &pkey2 );
  763. #endif
  764. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
  765. mbedtls_dhm_init( &dhm );
  766. #endif
  767. #if defined(MBEDTLS_SSL_CACHE_C)
  768. mbedtls_ssl_cache_init( &cache );
  769. #endif
  770. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  771. mbedtls_ssl_ticket_init( &ticket_ctx );
  772. #endif
  773. #if defined(MBEDTLS_SSL_ALPN)
  774. memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
  775. #endif
  776. #if defined(MBEDTLS_SSL_COOKIE_C)
  777. mbedtls_ssl_cookie_init( &cookie_ctx );
  778. #endif
  779. #if !defined(_WIN32)
  780. /* Abort cleanly on SIGTERM and SIGINT */
  781. signal( SIGTERM, term_handler );
  782. signal( SIGINT, term_handler );
  783. #endif
  784. if( argc == 0 )
  785. {
  786. usage:
  787. if( ret == 0 )
  788. ret = 1;
  789. mbedtls_printf( USAGE );
  790. list = mbedtls_ssl_list_ciphersuites();
  791. while( *list )
  792. {
  793. mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
  794. list++;
  795. if( !*list )
  796. break;
  797. mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
  798. list++;
  799. }
  800. mbedtls_printf("\n");
  801. goto exit;
  802. }
  803. opt.server_addr = DFL_SERVER_ADDR;
  804. opt.server_port = DFL_SERVER_PORT;
  805. opt.debug_level = DFL_DEBUG_LEVEL;
  806. opt.nbio = DFL_NBIO;
  807. opt.read_timeout = DFL_READ_TIMEOUT;
  808. opt.ca_file = DFL_CA_FILE;
  809. opt.ca_path = DFL_CA_PATH;
  810. opt.crt_file = DFL_CRT_FILE;
  811. opt.key_file = DFL_KEY_FILE;
  812. opt.crt_file2 = DFL_CRT_FILE2;
  813. opt.key_file2 = DFL_KEY_FILE2;
  814. opt.psk = DFL_PSK;
  815. opt.psk_identity = DFL_PSK_IDENTITY;
  816. opt.psk_list = DFL_PSK_LIST;
  817. opt.ecjpake_pw = DFL_ECJPAKE_PW;
  818. opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
  819. opt.version_suites = DFL_VERSION_SUITES;
  820. opt.renegotiation = DFL_RENEGOTIATION;
  821. opt.allow_legacy = DFL_ALLOW_LEGACY;
  822. opt.renegotiate = DFL_RENEGOTIATE;
  823. opt.renego_delay = DFL_RENEGO_DELAY;
  824. opt.renego_period = DFL_RENEGO_PERIOD;
  825. opt.exchanges = DFL_EXCHANGES;
  826. opt.min_version = DFL_MIN_VERSION;
  827. opt.max_version = DFL_MAX_VERSION;
  828. opt.arc4 = DFL_ARC4;
  829. opt.auth_mode = DFL_AUTH_MODE;
  830. opt.mfl_code = DFL_MFL_CODE;
  831. opt.trunc_hmac = DFL_TRUNC_HMAC;
  832. opt.tickets = DFL_TICKETS;
  833. opt.ticket_timeout = DFL_TICKET_TIMEOUT;
  834. opt.cache_max = DFL_CACHE_MAX;
  835. opt.cache_timeout = DFL_CACHE_TIMEOUT;
  836. opt.sni = DFL_SNI;
  837. opt.alpn_string = DFL_ALPN_STRING;
  838. opt.dhm_file = DFL_DHM_FILE;
  839. opt.transport = DFL_TRANSPORT;
  840. opt.cookies = DFL_COOKIES;
  841. opt.anti_replay = DFL_ANTI_REPLAY;
  842. opt.hs_to_min = DFL_HS_TO_MIN;
  843. opt.hs_to_max = DFL_HS_TO_MAX;
  844. opt.badmac_limit = DFL_BADMAC_LIMIT;
  845. opt.extended_ms = DFL_EXTENDED_MS;
  846. opt.etm = DFL_ETM;
  847. for( i = 1; i < argc; i++ )
  848. {
  849. p = argv[i];
  850. if( ( q = strchr( p, '=' ) ) == NULL )
  851. goto usage;
  852. *q++ = '\0';
  853. if( strcmp( p, "server_port" ) == 0 )
  854. opt.server_port = q;
  855. else if( strcmp( p, "server_addr" ) == 0 )
  856. opt.server_addr = q;
  857. else if( strcmp( p, "dtls" ) == 0 )
  858. {
  859. int t = atoi( q );
  860. if( t == 0 )
  861. opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
  862. else if( t == 1 )
  863. opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
  864. else
  865. goto usage;
  866. }
  867. else if( strcmp( p, "debug_level" ) == 0 )
  868. {
  869. opt.debug_level = atoi( q );
  870. if( opt.debug_level < 0 || opt.debug_level > 65535 )
  871. goto usage;
  872. }
  873. else if( strcmp( p, "nbio" ) == 0 )
  874. {
  875. opt.nbio = atoi( q );
  876. if( opt.nbio < 0 || opt.nbio > 2 )
  877. goto usage;
  878. }
  879. else if( strcmp( p, "read_timeout" ) == 0 )
  880. opt.read_timeout = atoi( q );
  881. else if( strcmp( p, "ca_file" ) == 0 )
  882. opt.ca_file = q;
  883. else if( strcmp( p, "ca_path" ) == 0 )
  884. opt.ca_path = q;
  885. else if( strcmp( p, "crt_file" ) == 0 )
  886. opt.crt_file = q;
  887. else if( strcmp( p, "key_file" ) == 0 )
  888. opt.key_file = q;
  889. else if( strcmp( p, "crt_file2" ) == 0 )
  890. opt.crt_file2 = q;
  891. else if( strcmp( p, "key_file2" ) == 0 )
  892. opt.key_file2 = q;
  893. else if( strcmp( p, "dhm_file" ) == 0 )
  894. opt.dhm_file = q;
  895. else if( strcmp( p, "psk" ) == 0 )
  896. opt.psk = q;
  897. else if( strcmp( p, "psk_identity" ) == 0 )
  898. opt.psk_identity = q;
  899. else if( strcmp( p, "psk_list" ) == 0 )
  900. opt.psk_list = q;
  901. else if( strcmp( p, "ecjpake_pw" ) == 0 )
  902. opt.ecjpake_pw = q;
  903. else if( strcmp( p, "force_ciphersuite" ) == 0 )
  904. {
  905. opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
  906. if( opt.force_ciphersuite[0] == 0 )
  907. {
  908. ret = 2;
  909. goto usage;
  910. }
  911. opt.force_ciphersuite[1] = 0;
  912. }
  913. else if( strcmp( p, "version_suites" ) == 0 )
  914. opt.version_suites = q;
  915. else if( strcmp( p, "renegotiation" ) == 0 )
  916. {
  917. opt.renegotiation = (atoi( q )) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED :
  918. MBEDTLS_SSL_RENEGOTIATION_DISABLED;
  919. }
  920. else if( strcmp( p, "allow_legacy" ) == 0 )
  921. {
  922. switch( atoi( q ) )
  923. {
  924. case -1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE; break;
  925. case 0: opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION; break;
  926. case 1: opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION; break;
  927. default: goto usage;
  928. }
  929. }
  930. else if( strcmp( p, "renegotiate" ) == 0 )
  931. {
  932. opt.renegotiate = atoi( q );
  933. if( opt.renegotiate < 0 || opt.renegotiate > 1 )
  934. goto usage;
  935. }
  936. else if( strcmp( p, "renego_delay" ) == 0 )
  937. {
  938. opt.renego_delay = atoi( q );
  939. }
  940. else if( strcmp( p, "renego_period" ) == 0 )
  941. {
  942. #if defined(_MSC_VER)
  943. opt.renego_period = _strtoui64( q, NULL, 10 );
  944. #else
  945. if( sscanf( q, "%" SCNu64, &opt.renego_period ) != 1 )
  946. goto usage;
  947. #endif /* _MSC_VER */
  948. if( opt.renego_period < 2 )
  949. goto usage;
  950. }
  951. else if( strcmp( p, "exchanges" ) == 0 )
  952. {
  953. opt.exchanges = atoi( q );
  954. if( opt.exchanges < 0 )
  955. goto usage;
  956. }
  957. else if( strcmp( p, "min_version" ) == 0 )
  958. {
  959. if( strcmp( q, "ssl3" ) == 0 )
  960. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
  961. else if( strcmp( q, "tls1" ) == 0 )
  962. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
  963. else if( strcmp( q, "tls1_1" ) == 0 ||
  964. strcmp( q, "dtls1" ) == 0 )
  965. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
  966. else if( strcmp( q, "tls1_2" ) == 0 ||
  967. strcmp( q, "dtls1_2" ) == 0 )
  968. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
  969. else
  970. goto usage;
  971. }
  972. else if( strcmp( p, "max_version" ) == 0 )
  973. {
  974. if( strcmp( q, "ssl3" ) == 0 )
  975. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
  976. else if( strcmp( q, "tls1" ) == 0 )
  977. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
  978. else if( strcmp( q, "tls1_1" ) == 0 ||
  979. strcmp( q, "dtls1" ) == 0 )
  980. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
  981. else if( strcmp( q, "tls1_2" ) == 0 ||
  982. strcmp( q, "dtls1_2" ) == 0 )
  983. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
  984. else
  985. goto usage;
  986. }
  987. else if( strcmp( p, "arc4" ) == 0 )
  988. {
  989. switch( atoi( q ) )
  990. {
  991. case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
  992. case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
  993. default: goto usage;
  994. }
  995. }
  996. else if( strcmp( p, "force_version" ) == 0 )
  997. {
  998. if( strcmp( q, "ssl3" ) == 0 )
  999. {
  1000. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
  1001. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
  1002. }
  1003. else if( strcmp( q, "tls1" ) == 0 )
  1004. {
  1005. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
  1006. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
  1007. }
  1008. else if( strcmp( q, "tls1_1" ) == 0 )
  1009. {
  1010. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
  1011. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
  1012. }
  1013. else if( strcmp( q, "tls1_2" ) == 0 )
  1014. {
  1015. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1016. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1017. }
  1018. else if( strcmp( q, "dtls1" ) == 0 )
  1019. {
  1020. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
  1021. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
  1022. opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
  1023. }
  1024. else if( strcmp( q, "dtls1_2" ) == 0 )
  1025. {
  1026. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1027. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1028. opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
  1029. }
  1030. else
  1031. goto usage;
  1032. }
  1033. else if( strcmp( p, "auth_mode" ) == 0 )
  1034. {
  1035. if( ( opt.auth_mode = get_auth_mode( q ) ) < 0 )
  1036. goto usage;
  1037. }
  1038. else if( strcmp( p, "max_frag_len" ) == 0 )
  1039. {
  1040. if( strcmp( q, "512" ) == 0 )
  1041. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
  1042. else if( strcmp( q, "1024" ) == 0 )
  1043. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
  1044. else if( strcmp( q, "2048" ) == 0 )
  1045. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
  1046. else if( strcmp( q, "4096" ) == 0 )
  1047. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
  1048. else
  1049. goto usage;
  1050. }
  1051. else if( strcmp( p, "alpn" ) == 0 )
  1052. {
  1053. opt.alpn_string = q;
  1054. }
  1055. else if( strcmp( p, "trunc_hmac" ) == 0 )
  1056. {
  1057. switch( atoi( q ) )
  1058. {
  1059. case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
  1060. case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
  1061. default: goto usage;
  1062. }
  1063. }
  1064. else if( strcmp( p, "extended_ms" ) == 0 )
  1065. {
  1066. switch( atoi( q ) )
  1067. {
  1068. case 0: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED; break;
  1069. case 1: opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; break;
  1070. default: goto usage;
  1071. }
  1072. }
  1073. else if( strcmp( p, "etm" ) == 0 )
  1074. {
  1075. switch( atoi( q ) )
  1076. {
  1077. case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
  1078. case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
  1079. default: goto usage;
  1080. }
  1081. }
  1082. else if( strcmp( p, "tickets" ) == 0 )
  1083. {
  1084. opt.tickets = atoi( q );
  1085. if( opt.tickets < 0 || opt.tickets > 1 )
  1086. goto usage;
  1087. }
  1088. else if( strcmp( p, "ticket_timeout" ) == 0 )
  1089. {
  1090. opt.ticket_timeout = atoi( q );
  1091. if( opt.ticket_timeout < 0 )
  1092. goto usage;
  1093. }
  1094. else if( strcmp( p, "cache_max" ) == 0 )
  1095. {
  1096. opt.cache_max = atoi( q );
  1097. if( opt.cache_max < 0 )
  1098. goto usage;
  1099. }
  1100. else if( strcmp( p, "cache_timeout" ) == 0 )
  1101. {
  1102. opt.cache_timeout = atoi( q );
  1103. if( opt.cache_timeout < 0 )
  1104. goto usage;
  1105. }
  1106. else if( strcmp( p, "cookies" ) == 0 )
  1107. {
  1108. opt.cookies = atoi( q );
  1109. if( opt.cookies < -1 || opt.cookies > 1)
  1110. goto usage;
  1111. }
  1112. else if( strcmp( p, "anti_replay" ) == 0 )
  1113. {
  1114. opt.anti_replay = atoi( q );
  1115. if( opt.anti_replay < 0 || opt.anti_replay > 1)
  1116. goto usage;
  1117. }
  1118. else if( strcmp( p, "badmac_limit" ) == 0 )
  1119. {
  1120. opt.badmac_limit = atoi( q );
  1121. if( opt.badmac_limit < 0 )
  1122. goto usage;
  1123. }
  1124. else if( strcmp( p, "hs_timeout" ) == 0 )
  1125. {
  1126. if( ( p = strchr( q, '-' ) ) == NULL )
  1127. goto usage;
  1128. *p++ = '\0';
  1129. opt.hs_to_min = atoi( q );
  1130. opt.hs_to_max = atoi( p );
  1131. if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
  1132. goto usage;
  1133. }
  1134. else if( strcmp( p, "sni" ) == 0 )
  1135. {
  1136. opt.sni = q;
  1137. }
  1138. else
  1139. goto usage;
  1140. }
  1141. #if defined(MBEDTLS_DEBUG_C)
  1142. mbedtls_debug_set_threshold( opt.debug_level );
  1143. #endif
  1144. if( opt.force_ciphersuite[0] > 0 )
  1145. {
  1146. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  1147. ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
  1148. if( opt.max_version != -1 &&
  1149. ciphersuite_info->min_minor_ver > opt.max_version )
  1150. {
  1151. mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
  1152. ret = 2;
  1153. goto usage;
  1154. }
  1155. if( opt.min_version != -1 &&
  1156. ciphersuite_info->max_minor_ver < opt.min_version )
  1157. {
  1158. mbedtls_printf("forced ciphersuite not allowed with this protocol version\n");
  1159. ret = 2;
  1160. goto usage;
  1161. }
  1162. /* If we select a version that's not supported by
  1163. * this suite, then there will be no common ciphersuite... */
  1164. if( opt.max_version == -1 ||
  1165. opt.max_version > ciphersuite_info->max_minor_ver )
  1166. {
  1167. opt.max_version = ciphersuite_info->max_minor_ver;
  1168. }
  1169. if( opt.min_version < ciphersuite_info->min_minor_ver )
  1170. {
  1171. opt.min_version = ciphersuite_info->min_minor_ver;
  1172. /* DTLS starts with TLS 1.1 */
  1173. if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  1174. opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
  1175. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
  1176. }
  1177. /* Enable RC4 if needed and not explicitly disabled */
  1178. if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
  1179. {
  1180. if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
  1181. {
  1182. mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
  1183. ret = 2;
  1184. goto usage;
  1185. }
  1186. opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
  1187. }
  1188. }
  1189. if( opt.version_suites != NULL )
  1190. {
  1191. const char *name[4] = { 0 };
  1192. /* Parse 4-element coma-separated list */
  1193. for( i = 0, p = (char *) opt.version_suites;
  1194. i < 4 && *p != '\0';
  1195. i++ )
  1196. {
  1197. name[i] = p;
  1198. /* Terminate the current string and move on to next one */
  1199. while( *p != ',' && *p != '\0' )
  1200. p++;
  1201. if( *p == ',' )
  1202. *p++ = '\0';
  1203. }
  1204. if( i != 4 )
  1205. {
  1206. mbedtls_printf( "too few values for version_suites\n" );
  1207. ret = 1;
  1208. goto exit;
  1209. }
  1210. memset( version_suites, 0, sizeof( version_suites ) );
  1211. /* Get the suites identifiers from their name */
  1212. for( i = 0; i < 4; i++ )
  1213. {
  1214. version_suites[i][0] = mbedtls_ssl_get_ciphersuite_id( name[i] );
  1215. if( version_suites[i][0] == 0 )
  1216. {
  1217. mbedtls_printf( "unknown ciphersuite: '%s'\n", name[i] );
  1218. ret = 2;
  1219. goto usage;
  1220. }
  1221. }
  1222. }
  1223. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  1224. /*
  1225. * Unhexify the pre-shared key and parse the list if any given
  1226. */
  1227. if( unhexify( psk, opt.psk, &psk_len ) != 0 )
  1228. {
  1229. mbedtls_printf( "pre-shared key not valid hex\n" );
  1230. goto exit;
  1231. }
  1232. if( opt.psk_list != NULL )
  1233. {
  1234. if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
  1235. {
  1236. mbedtls_printf( "psk_list invalid" );
  1237. goto exit;
  1238. }
  1239. }
  1240. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  1241. #if defined(MBEDTLS_SSL_ALPN)
  1242. if( opt.alpn_string != NULL )
  1243. {
  1244. p = (char *) opt.alpn_string;
  1245. i = 0;
  1246. /* Leave room for a final NULL in alpn_list */
  1247. while( i < (int) sizeof alpn_list - 1 && *p != '\0' )
  1248. {
  1249. alpn_list[i++] = p;
  1250. /* Terminate the current string and move on to next one */
  1251. while( *p != ',' && *p != '\0' )
  1252. p++;
  1253. if( *p == ',' )
  1254. *p++ = '\0';
  1255. }
  1256. }
  1257. #endif /* MBEDTLS_SSL_ALPN */
  1258. /*
  1259. * 0. Initialize the RNG and the session data
  1260. */
  1261. mbedtls_printf( "\n . Seeding the random number generator..." );
  1262. fflush( stdout );
  1263. mbedtls_entropy_init( &entropy );
  1264. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  1265. (const unsigned char *) pers,
  1266. strlen( pers ) ) ) != 0 )
  1267. {
  1268. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n", -ret );
  1269. goto exit;
  1270. }
  1271. mbedtls_printf( " ok\n" );
  1272. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1273. /*
  1274. * 1.1. Load the trusted CA
  1275. */
  1276. mbedtls_printf( " . Loading the CA root certificate ..." );
  1277. fflush( stdout );
  1278. #if defined(MBEDTLS_FS_IO)
  1279. if( strlen( opt.ca_path ) )
  1280. if( strcmp( opt.ca_path, "none" ) == 0 )
  1281. ret = 0;
  1282. else
  1283. ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
  1284. else if( strlen( opt.ca_file ) )
  1285. if( strcmp( opt.ca_file, "none" ) == 0 )
  1286. ret = 0;
  1287. else
  1288. ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
  1289. else
  1290. #endif
  1291. #if defined(MBEDTLS_CERTS_C)
  1292. for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
  1293. {
  1294. ret = mbedtls_x509_crt_parse( &cacert,
  1295. (const unsigned char *) mbedtls_test_cas[i],
  1296. mbedtls_test_cas_len[i] );
  1297. if( ret != 0 )
  1298. break;
  1299. }
  1300. #else
  1301. {
  1302. ret = 1;
  1303. mbedtls_printf("MBEDTLS_CERTS_C not defined.");
  1304. }
  1305. #endif
  1306. if( ret < 0 )
  1307. {
  1308. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
  1309. goto exit;
  1310. }
  1311. mbedtls_printf( " ok (%d skipped)\n", ret );
  1312. /*
  1313. * 1.2. Load own certificate and private key
  1314. */
  1315. mbedtls_printf( " . Loading the server cert. and key..." );
  1316. fflush( stdout );
  1317. #if defined(MBEDTLS_FS_IO)
  1318. if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
  1319. {
  1320. key_cert_init++;
  1321. if( ( ret = mbedtls_x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
  1322. {
  1323. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
  1324. -ret );
  1325. goto exit;
  1326. }
  1327. }
  1328. if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
  1329. {
  1330. key_cert_init++;
  1331. if( ( ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" ) ) != 0 )
  1332. {
  1333. mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n", -ret );
  1334. goto exit;
  1335. }
  1336. }
  1337. if( key_cert_init == 1 )
  1338. {
  1339. mbedtls_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
  1340. goto exit;
  1341. }
  1342. if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
  1343. {
  1344. key_cert_init2++;
  1345. if( ( ret = mbedtls_x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
  1346. {
  1347. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n",
  1348. -ret );
  1349. goto exit;
  1350. }
  1351. }
  1352. if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
  1353. {
  1354. key_cert_init2++;
  1355. if( ( ret = mbedtls_pk_parse_keyfile( &pkey2, opt.key_file2, "" ) ) != 0 )
  1356. {
  1357. mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n",
  1358. -ret );
  1359. goto exit;
  1360. }
  1361. }
  1362. if( key_cert_init2 == 1 )
  1363. {
  1364. mbedtls_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
  1365. goto exit;
  1366. }
  1367. #endif
  1368. if( key_cert_init == 0 &&
  1369. strcmp( opt.crt_file, "none" ) != 0 &&
  1370. strcmp( opt.key_file, "none" ) != 0 &&
  1371. key_cert_init2 == 0 &&
  1372. strcmp( opt.crt_file2, "none" ) != 0 &&
  1373. strcmp( opt.key_file2, "none" ) != 0 )
  1374. {
  1375. #if !defined(MBEDTLS_CERTS_C)
  1376. mbedtls_printf( "Not certificated or key provided, and \n"
  1377. "MBEDTLS_CERTS_C not defined!\n" );
  1378. goto exit;
  1379. #else
  1380. #if defined(MBEDTLS_RSA_C)
  1381. if( ( ret = mbedtls_x509_crt_parse( &srvcert,
  1382. (const unsigned char *) mbedtls_test_srv_crt_rsa,
  1383. mbedtls_test_srv_crt_rsa_len ) ) != 0 )
  1384. {
  1385. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
  1386. goto exit;
  1387. }
  1388. if( ( ret = mbedtls_pk_parse_key( &pkey,
  1389. (const unsigned char *) mbedtls_test_srv_key_rsa,
  1390. mbedtls_test_srv_key_rsa_len, NULL, 0 ) ) != 0 )
  1391. {
  1392. mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n", -ret );
  1393. goto exit;
  1394. }
  1395. key_cert_init = 2;
  1396. #endif /* MBEDTLS_RSA_C */
  1397. #if defined(MBEDTLS_ECDSA_C)
  1398. if( ( ret = mbedtls_x509_crt_parse( &srvcert2,
  1399. (const unsigned char *) mbedtls_test_srv_crt_ec,
  1400. mbedtls_test_srv_crt_ec_len ) ) != 0 )
  1401. {
  1402. mbedtls_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n", -ret );
  1403. goto exit;
  1404. }
  1405. if( ( ret = mbedtls_pk_parse_key( &pkey2,
  1406. (const unsigned char *) mbedtls_test_srv_key_ec,
  1407. mbedtls_test_srv_key_ec_len, NULL, 0 ) ) != 0 )
  1408. {
  1409. mbedtls_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n", -ret );
  1410. goto exit;
  1411. }
  1412. key_cert_init2 = 2;
  1413. #endif /* MBEDTLS_ECDSA_C */
  1414. #endif /* MBEDTLS_CERTS_C */
  1415. }
  1416. mbedtls_printf( " ok\n" );
  1417. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  1418. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
  1419. if( opt.dhm_file != NULL )
  1420. {
  1421. mbedtls_printf( " . Loading DHM parameters..." );
  1422. fflush( stdout );
  1423. if( ( ret = mbedtls_dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
  1424. {
  1425. mbedtls_printf( " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n",
  1426. -ret );
  1427. goto exit;
  1428. }
  1429. mbedtls_printf( " ok\n" );
  1430. }
  1431. #endif
  1432. #if defined(SNI_OPTION)
  1433. if( opt.sni != NULL )
  1434. {
  1435. mbedtls_printf( " . Setting up SNI information..." );
  1436. fflush( stdout );
  1437. if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
  1438. {
  1439. mbedtls_printf( " failed\n" );
  1440. goto exit;
  1441. }
  1442. mbedtls_printf( " ok\n" );
  1443. }
  1444. #endif /* SNI_OPTION */
  1445. /*
  1446. * 2. Setup the listening TCP socket
  1447. */
  1448. mbedtls_printf( " . Bind on %s://%s:%s/ ...",
  1449. opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
  1450. opt.server_addr ? opt.server_addr : "*",
  1451. opt.server_port );
  1452. fflush( stdout );
  1453. if( ( ret = mbedtls_net_bind( &listen_fd, opt.server_addr, opt.server_port,
  1454. opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
  1455. MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
  1456. {
  1457. mbedtls_printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret );
  1458. goto exit;
  1459. }
  1460. mbedtls_printf( " ok\n" );
  1461. /*
  1462. * 3. Setup stuff
  1463. */
  1464. mbedtls_printf( " . Setting up the SSL/TLS structure..." );
  1465. fflush( stdout );
  1466. if( ( ret = mbedtls_ssl_config_defaults( &conf,
  1467. MBEDTLS_SSL_IS_SERVER,
  1468. opt.transport,
  1469. MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
  1470. {
  1471. mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret );
  1472. goto exit;
  1473. }
  1474. if( opt.auth_mode != DFL_AUTH_MODE )
  1475. mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
  1476. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1477. if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
  1478. mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
  1479. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1480. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  1481. if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
  1482. {
  1483. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
  1484. goto exit;
  1485. };
  1486. #endif
  1487. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  1488. if( opt.trunc_hmac != DFL_TRUNC_HMAC )
  1489. mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
  1490. #endif
  1491. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  1492. if( opt.extended_ms != DFL_EXTENDED_MS )
  1493. mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
  1494. #endif
  1495. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  1496. if( opt.etm != DFL_ETM )
  1497. mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
  1498. #endif
  1499. #if defined(MBEDTLS_SSL_ALPN)
  1500. if( opt.alpn_string != NULL )
  1501. if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
  1502. {
  1503. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
  1504. goto exit;
  1505. }
  1506. #endif
  1507. mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
  1508. mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
  1509. #if defined(MBEDTLS_SSL_CACHE_C)
  1510. if( opt.cache_max != -1 )
  1511. mbedtls_ssl_cache_set_max_entries( &cache, opt.cache_max );
  1512. if( opt.cache_timeout != -1 )
  1513. mbedtls_ssl_cache_set_timeout( &cache, opt.cache_timeout );
  1514. mbedtls_ssl_conf_session_cache( &conf, &cache,
  1515. mbedtls_ssl_cache_get,
  1516. mbedtls_ssl_cache_set );
  1517. #endif
  1518. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1519. if( opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED )
  1520. {
  1521. if( ( ret = mbedtls_ssl_ticket_setup( &ticket_ctx,
  1522. mbedtls_ctr_drbg_random, &ctr_drbg,
  1523. MBEDTLS_CIPHER_AES_256_GCM,
  1524. opt.ticket_timeout ) ) != 0 )
  1525. {
  1526. mbedtls_printf( " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret );
  1527. goto exit;
  1528. }
  1529. mbedtls_ssl_conf_session_tickets_cb( &conf,
  1530. mbedtls_ssl_ticket_write,
  1531. mbedtls_ssl_ticket_parse,
  1532. &ticket_ctx );
  1533. }
  1534. #endif
  1535. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1536. if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  1537. {
  1538. #if defined(MBEDTLS_SSL_COOKIE_C)
  1539. if( opt.cookies > 0 )
  1540. {
  1541. if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
  1542. mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  1543. {
  1544. mbedtls_printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
  1545. goto exit;
  1546. }
  1547. mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
  1548. &cookie_ctx );
  1549. }
  1550. else
  1551. #endif /* MBEDTLS_SSL_COOKIE_C */
  1552. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  1553. if( opt.cookies == 0 )
  1554. {
  1555. mbedtls_ssl_conf_dtls_cookies( &conf, NULL, NULL, NULL );
  1556. }
  1557. else
  1558. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
  1559. {
  1560. ; /* Nothing to do */
  1561. }
  1562. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  1563. if( opt.anti_replay != DFL_ANTI_REPLAY )
  1564. mbedtls_ssl_conf_dtls_anti_replay( &conf, opt.anti_replay );
  1565. #endif
  1566. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  1567. if( opt.badmac_limit != DFL_BADMAC_LIMIT )
  1568. mbedtls_ssl_conf_dtls_badmac_limit( &conf, opt.badmac_limit );
  1569. #endif
  1570. }
  1571. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1572. if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
  1573. mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
  1574. #if defined(MBEDTLS_ARC4_C)
  1575. if( opt.arc4 != DFL_ARC4 )
  1576. mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
  1577. #endif
  1578. if( opt.version_suites != NULL )
  1579. {
  1580. mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[0],
  1581. MBEDTLS_SSL_MAJOR_VERSION_3,
  1582. MBEDTLS_SSL_MINOR_VERSION_0 );
  1583. mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[1],
  1584. MBEDTLS_SSL_MAJOR_VERSION_3,
  1585. MBEDTLS_SSL_MINOR_VERSION_1 );
  1586. mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[2],
  1587. MBEDTLS_SSL_MAJOR_VERSION_3,
  1588. MBEDTLS_SSL_MINOR_VERSION_2 );
  1589. mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[3],
  1590. MBEDTLS_SSL_MAJOR_VERSION_3,
  1591. MBEDTLS_SSL_MINOR_VERSION_3 );
  1592. }
  1593. if( opt.allow_legacy != DFL_ALLOW_LEGACY )
  1594. mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
  1595. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1596. mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
  1597. if( opt.renego_delay != DFL_RENEGO_DELAY )
  1598. mbedtls_ssl_conf_renegotiation_enforced( &conf, opt.renego_delay );
  1599. if( opt.renego_period != DFL_RENEGO_PERIOD )
  1600. {
  1601. PUT_UINT64_BE( renego_period, opt.renego_period, 0 );
  1602. mbedtls_ssl_conf_renegotiation_period( &conf, renego_period );
  1603. }
  1604. #endif
  1605. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1606. if( strcmp( opt.ca_path, "none" ) != 0 &&
  1607. strcmp( opt.ca_file, "none" ) != 0 )
  1608. {
  1609. mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
  1610. }
  1611. if( key_cert_init )
  1612. if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
  1613. {
  1614. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
  1615. goto exit;
  1616. }
  1617. if( key_cert_init2 )
  1618. if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, &pkey2 ) ) != 0 )
  1619. {
  1620. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
  1621. goto exit;
  1622. }
  1623. #endif
  1624. #if defined(SNI_OPTION)
  1625. if( opt.sni != NULL )
  1626. mbedtls_ssl_conf_sni( &conf, sni_callback, sni_info );
  1627. #endif
  1628. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  1629. if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
  1630. {
  1631. ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
  1632. (const unsigned char *) opt.psk_identity,
  1633. strlen( opt.psk_identity ) );
  1634. if( ret != 0 )
  1635. {
  1636. mbedtls_printf( " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", - ret );
  1637. goto exit;
  1638. }
  1639. }
  1640. if( opt.psk_list != NULL )
  1641. mbedtls_ssl_conf_psk_cb( &conf, psk_callback, psk_info );
  1642. #endif
  1643. #if defined(MBEDTLS_DHM_C)
  1644. /*
  1645. * Use different group than default DHM group
  1646. */
  1647. #if defined(MBEDTLS_FS_IO)
  1648. if( opt.dhm_file != NULL )
  1649. ret = mbedtls_ssl_conf_dh_param_ctx( &conf, &dhm );
  1650. #endif
  1651. if( ret != 0 )
  1652. {
  1653. mbedtls_printf( " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", - ret );
  1654. goto exit;
  1655. }
  1656. #endif
  1657. if( opt.min_version != DFL_MIN_VERSION )
  1658. mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
  1659. if( opt.max_version != DFL_MIN_VERSION )
  1660. mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
  1661. if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
  1662. {
  1663. mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret );
  1664. goto exit;
  1665. }
  1666. if( opt.nbio == 2 )
  1667. mbedtls_ssl_set_bio( &ssl, &client_fd, my_send, my_recv, NULL );
  1668. else
  1669. mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv,
  1670. opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
  1671. #if defined(MBEDTLS_TIMING_C)
  1672. mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
  1673. mbedtls_timing_get_delay );
  1674. #endif
  1675. mbedtls_printf( " ok\n" );
  1676. reset:
  1677. #if !defined(_WIN32)
  1678. if( received_sigterm )
  1679. {
  1680. mbedtls_printf( " interrupted by SIGTERM\n" );
  1681. ret = 0;
  1682. goto exit;
  1683. }
  1684. #endif
  1685. if( ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
  1686. {
  1687. mbedtls_printf( " ! Client initiated reconnection from same port\n" );
  1688. goto handshake;
  1689. }
  1690. #ifdef MBEDTLS_ERROR_C
  1691. if( ret != 0 )
  1692. {
  1693. char error_buf[100];
  1694. mbedtls_strerror( ret, error_buf, 100 );
  1695. mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
  1696. }
  1697. #endif
  1698. mbedtls_net_free( &client_fd );
  1699. mbedtls_ssl_session_reset( &ssl );
  1700. /*
  1701. * 3. Wait until a client connects
  1702. */
  1703. mbedtls_printf( " . Waiting for a remote connection ..." );
  1704. fflush( stdout );
  1705. if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
  1706. client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
  1707. {
  1708. #if !defined(_WIN32)
  1709. if( received_sigterm )
  1710. {
  1711. mbedtls_printf( " interrupted by signal\n" );
  1712. ret = 0;
  1713. goto exit;
  1714. }
  1715. #endif
  1716. mbedtls_printf( " failed\n ! mbedtls_net_accept returned -0x%x\n\n", -ret );
  1717. goto exit;
  1718. }
  1719. if( opt.nbio > 0 )
  1720. ret = mbedtls_net_set_nonblock( &client_fd );
  1721. else
  1722. ret = mbedtls_net_set_block( &client_fd );
  1723. if( ret != 0 )
  1724. {
  1725. mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
  1726. goto exit;
  1727. }
  1728. mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
  1729. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  1730. if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  1731. {
  1732. if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
  1733. client_ip, cliip_len ) ) != 0 )
  1734. {
  1735. mbedtls_printf( " failed\n ! "
  1736. "mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n", -ret );
  1737. goto exit;
  1738. }
  1739. }
  1740. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
  1741. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1742. if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
  1743. {
  1744. if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
  1745. (const unsigned char *) opt.ecjpake_pw,
  1746. strlen( opt.ecjpake_pw ) ) ) != 0 )
  1747. {
  1748. mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
  1749. goto exit;
  1750. }
  1751. }
  1752. #endif
  1753. mbedtls_printf( " ok\n" );
  1754. /*
  1755. * 4. Handshake
  1756. */
  1757. handshake:
  1758. mbedtls_printf( " . Performing the SSL/TLS handshake..." );
  1759. fflush( stdout );
  1760. do ret = mbedtls_ssl_handshake( &ssl );
  1761. while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
  1762. ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  1763. if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
  1764. {
  1765. mbedtls_printf( " hello verification requested\n" );
  1766. ret = 0;
  1767. goto reset;
  1768. }
  1769. else if( ret != 0 )
  1770. {
  1771. mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
  1772. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1773. if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
  1774. {
  1775. char vrfy_buf[512];
  1776. flags = mbedtls_ssl_get_verify_result( &ssl );
  1777. mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
  1778. mbedtls_printf( "%s\n", vrfy_buf );
  1779. }
  1780. #endif
  1781. goto reset;
  1782. }
  1783. else /* ret == 0 */
  1784. {
  1785. mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
  1786. mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
  1787. }
  1788. if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
  1789. mbedtls_printf( " [ Record expansion is %d ]\n", ret );
  1790. else
  1791. mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
  1792. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  1793. mbedtls_printf( " [ Maximum fragment length is %u ]\n",
  1794. (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
  1795. #endif
  1796. #if defined(MBEDTLS_SSL_ALPN)
  1797. if( opt.alpn_string != NULL )
  1798. {
  1799. const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
  1800. mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
  1801. alp ? alp : "(none)" );
  1802. }
  1803. #endif
  1804. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1805. /*
  1806. * 5. Verify the server certificate
  1807. */
  1808. mbedtls_printf( " . Verifying peer X.509 certificate..." );
  1809. if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
  1810. {
  1811. char vrfy_buf[512];
  1812. mbedtls_printf( " failed\n" );
  1813. mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
  1814. mbedtls_printf( "%s\n", vrfy_buf );
  1815. }
  1816. else
  1817. mbedtls_printf( " ok\n" );
  1818. if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
  1819. {
  1820. char crt_buf[512];
  1821. mbedtls_printf( " . Peer certificate information ...\n" );
  1822. mbedtls_x509_crt_info( crt_buf, sizeof( crt_buf ), " ",
  1823. mbedtls_ssl_get_peer_cert( &ssl ) );
  1824. mbedtls_printf( "%s\n", crt_buf );
  1825. }
  1826. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  1827. if( opt.exchanges == 0 )
  1828. goto close_notify;
  1829. exchanges_left = opt.exchanges;
  1830. data_exchange:
  1831. /*
  1832. * 6. Read the HTTP Request
  1833. */
  1834. mbedtls_printf( " < Read from client:" );
  1835. fflush( stdout );
  1836. /*
  1837. * TLS and DTLS need different reading styles (stream vs datagram)
  1838. */
  1839. if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
  1840. {
  1841. do
  1842. {
  1843. int terminated = 0;
  1844. len = sizeof( buf ) - 1;
  1845. memset( buf, 0, sizeof( buf ) );
  1846. ret = mbedtls_ssl_read( &ssl, buf, len );
  1847. if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
  1848. ret == MBEDTLS_ERR_SSL_WANT_WRITE )
  1849. continue;
  1850. if( ret <= 0 )
  1851. {
  1852. switch( ret )
  1853. {
  1854. case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
  1855. mbedtls_printf( " connection was closed gracefully\n" );
  1856. goto close_notify;
  1857. case 0:
  1858. case MBEDTLS_ERR_NET_CONN_RESET:
  1859. mbedtls_printf( " connection was reset by peer\n" );
  1860. ret = MBEDTLS_ERR_NET_CONN_RESET;
  1861. goto reset;
  1862. default:
  1863. mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
  1864. goto reset;
  1865. }
  1866. }
  1867. if( mbedtls_ssl_get_bytes_avail( &ssl ) == 0 )
  1868. {
  1869. len = ret;
  1870. buf[len] = '\0';
  1871. mbedtls_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
  1872. /* End of message should be detected according to the syntax of the
  1873. * application protocol (eg HTTP), just use a dummy test here. */
  1874. if( buf[len - 1] == '\n' )
  1875. terminated = 1;
  1876. }
  1877. else
  1878. {
  1879. int extra_len, ori_len;
  1880. unsigned char *larger_buf;
  1881. ori_len = ret;
  1882. extra_len = (int) mbedtls_ssl_get_bytes_avail( &ssl );
  1883. larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 );
  1884. if( larger_buf == NULL )
  1885. {
  1886. mbedtls_printf( " ! memory allocation failed\n" );
  1887. ret = 1;
  1888. goto reset;
  1889. }
  1890. memset( larger_buf, 0, ori_len + extra_len );
  1891. memcpy( larger_buf, buf, ori_len );
  1892. /* This read should never fail and get the whole cached data */
  1893. ret = mbedtls_ssl_read( &ssl, larger_buf + ori_len, extra_len );
  1894. if( ret != extra_len ||
  1895. mbedtls_ssl_get_bytes_avail( &ssl ) != 0 )
  1896. {
  1897. mbedtls_printf( " ! mbedtls_ssl_read failed on cached data\n" );
  1898. ret = 1;
  1899. goto reset;
  1900. }
  1901. larger_buf[ori_len + extra_len] = '\0';
  1902. mbedtls_printf( " %u bytes read (%u + %u)\n\n%s\n",
  1903. ori_len + extra_len, ori_len, extra_len,
  1904. (char *) larger_buf );
  1905. /* End of message should be detected according to the syntax of the
  1906. * application protocol (eg HTTP), just use a dummy test here. */
  1907. if( larger_buf[ori_len + extra_len - 1] == '\n' )
  1908. terminated = 1;
  1909. mbedtls_free( larger_buf );
  1910. }
  1911. if( terminated )
  1912. {
  1913. ret = 0;
  1914. break;
  1915. }
  1916. }
  1917. while( 1 );
  1918. }
  1919. else /* Not stream, so datagram */
  1920. {
  1921. len = sizeof( buf ) - 1;
  1922. memset( buf, 0, sizeof( buf ) );
  1923. do ret = mbedtls_ssl_read( &ssl, buf, len );
  1924. while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
  1925. ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  1926. if( ret <= 0 )
  1927. {
  1928. switch( ret )
  1929. {
  1930. case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
  1931. mbedtls_printf( " connection was closed gracefully\n" );
  1932. ret = 0;
  1933. goto close_notify;
  1934. default:
  1935. mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
  1936. goto reset;
  1937. }
  1938. }
  1939. len = ret;
  1940. buf[len] = '\0';
  1941. mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
  1942. ret = 0;
  1943. }
  1944. /*
  1945. * 7a. Request renegotiation while client is waiting for input from us.
  1946. * (only on the first exchange, to be able to test retransmission)
  1947. */
  1948. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1949. if( opt.renegotiate && exchanges_left == opt.exchanges )
  1950. {
  1951. mbedtls_printf( " . Requestion renegotiation..." );
  1952. fflush( stdout );
  1953. while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
  1954. {
  1955. if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
  1956. ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  1957. {
  1958. mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
  1959. goto reset;
  1960. }
  1961. }
  1962. mbedtls_printf( " ok\n" );
  1963. }
  1964. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1965. /*
  1966. * 7. Write the 200 Response
  1967. */
  1968. mbedtls_printf( " > Write to client:" );
  1969. fflush( stdout );
  1970. len = sprintf( (char *) buf, HTTP_RESPONSE,
  1971. mbedtls_ssl_get_ciphersuite( &ssl ) );
  1972. if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
  1973. {
  1974. for( written = 0, frags = 0; written < len; written += ret, frags++ )
  1975. {
  1976. while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
  1977. <= 0 )
  1978. {
  1979. if( ret == MBEDTLS_ERR_NET_CONN_RESET )
  1980. {
  1981. mbedtls_printf( " failed\n ! peer closed the connection\n\n" );
  1982. goto reset;
  1983. }
  1984. if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
  1985. ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  1986. {
  1987. mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
  1988. goto reset;
  1989. }
  1990. }
  1991. }
  1992. }
  1993. else /* Not stream, so datagram */
  1994. {
  1995. do ret = mbedtls_ssl_write( &ssl, buf, len );
  1996. while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
  1997. ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  1998. if( ret < 0 )
  1999. {
  2000. mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
  2001. goto reset;
  2002. }
  2003. frags = 1;
  2004. written = ret;
  2005. }
  2006. buf[written] = '\0';
  2007. mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
  2008. ret = 0;
  2009. /*
  2010. * 7b. Continue doing data exchanges?
  2011. */
  2012. if( --exchanges_left > 0 )
  2013. goto data_exchange;
  2014. /*
  2015. * 8. Done, cleanly close the connection
  2016. */
  2017. close_notify:
  2018. mbedtls_printf( " . Closing the connection..." );
  2019. /* No error checking, the connection might be closed already */
  2020. do ret = mbedtls_ssl_close_notify( &ssl );
  2021. while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  2022. ret = 0;
  2023. mbedtls_printf( " done\n" );
  2024. goto reset;
  2025. /*
  2026. * Cleanup and exit
  2027. */
  2028. exit:
  2029. #ifdef MBEDTLS_ERROR_C
  2030. if( ret != 0 )
  2031. {
  2032. char error_buf[100];
  2033. mbedtls_strerror( ret, error_buf, 100 );
  2034. mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
  2035. }
  2036. #endif
  2037. mbedtls_printf( " . Cleaning up..." );
  2038. fflush( stdout );
  2039. mbedtls_net_free( &client_fd );
  2040. mbedtls_net_free( &listen_fd );
  2041. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
  2042. mbedtls_dhm_free( &dhm );
  2043. #endif
  2044. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2045. mbedtls_x509_crt_free( &cacert );
  2046. mbedtls_x509_crt_free( &srvcert );
  2047. mbedtls_pk_free( &pkey );
  2048. mbedtls_x509_crt_free( &srvcert2 );
  2049. mbedtls_pk_free( &pkey2 );
  2050. #endif
  2051. #if defined(SNI_OPTION)
  2052. sni_free( sni_info );
  2053. #endif
  2054. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  2055. psk_free( psk_info );
  2056. #endif
  2057. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
  2058. mbedtls_dhm_free( &dhm );
  2059. #endif
  2060. mbedtls_ssl_free( &ssl );
  2061. mbedtls_ssl_config_free( &conf );
  2062. mbedtls_ctr_drbg_free( &ctr_drbg );
  2063. mbedtls_entropy_free( &entropy );
  2064. #if defined(MBEDTLS_SSL_CACHE_C)
  2065. mbedtls_ssl_cache_free( &cache );
  2066. #endif
  2067. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2068. mbedtls_ssl_ticket_free( &ticket_ctx );
  2069. #endif
  2070. #if defined(MBEDTLS_SSL_COOKIE_C)
  2071. mbedtls_ssl_cookie_free( &cookie_ctx );
  2072. #endif
  2073. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  2074. #if defined(MBEDTLS_MEMORY_DEBUG)
  2075. mbedtls_memory_buffer_alloc_status();
  2076. #endif
  2077. mbedtls_memory_buffer_alloc_free();
  2078. #endif
  2079. mbedtls_printf( " done.\n" );
  2080. #if defined(_WIN32)
  2081. mbedtls_printf( " + Press Enter to exit this program.\n" );
  2082. fflush( stdout ); getchar();
  2083. #endif
  2084. // Shell can not handle large exit numbers -> 1 for errors
  2085. if( ret < 0 )
  2086. ret = 1;
  2087. return( ret );
  2088. }
  2089. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
  2090. MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
  2091. MBEDTLS_CTR_DRBG_C */