ssl_server2.c 105 KB

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