ssl_server2.c 74 KB

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