ssl_server2.c 140 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070
  1. /*
  2. * SSL client with options
  3. *
  4. * Copyright The Mbed TLS Contributors
  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. #include "ssl_test_lib.h"
  20. #if defined(MBEDTLS_SSL_TEST_IMPOSSIBLE)
  21. int main( void )
  22. {
  23. mbedtls_printf( MBEDTLS_SSL_TEST_IMPOSSIBLE );
  24. mbedtls_exit( 0 );
  25. }
  26. #elif !defined(MBEDTLS_SSL_SRV_C)
  27. int main( void )
  28. {
  29. mbedtls_printf( "MBEDTLS_SSL_SRV_C not defined.\n" );
  30. mbedtls_exit( 0 );
  31. }
  32. #else /* !MBEDTLS_SSL_TEST_IMPOSSIBLE && MBEDTLS_SSL_SRV_C */
  33. #include <stdint.h>
  34. #if !defined(_MSC_VER)
  35. #include <inttypes.h>
  36. #endif
  37. #if !defined(_WIN32)
  38. #include <signal.h>
  39. #endif
  40. #if defined(MBEDTLS_SSL_CACHE_C)
  41. #include "mbedtls/ssl_cache.h"
  42. #endif
  43. #if defined(MBEDTLS_SSL_TICKET_C)
  44. #include "mbedtls/ssl_ticket.h"
  45. #endif
  46. #if defined(MBEDTLS_SSL_COOKIE_C)
  47. #include "mbedtls/ssl_cookie.h"
  48. #endif
  49. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_FS_IO)
  50. #define SNI_OPTION
  51. #endif
  52. #if defined(_WIN32)
  53. #include <windows.h>
  54. #endif
  55. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  56. #include "test/psa_crypto_helpers.h"
  57. #endif
  58. /* Size of memory to be allocated for the heap, when using the library's memory
  59. * management and MBEDTLS_MEMORY_BUFFER_ALLOC_C is enabled. */
  60. #define MEMORY_HEAP_SIZE 120000
  61. #define DFL_SERVER_ADDR NULL
  62. #define DFL_SERVER_PORT "4433"
  63. #define DFL_RESPONSE_SIZE -1
  64. #define DFL_DEBUG_LEVEL 0
  65. #define DFL_NBIO 0
  66. #define DFL_EVENT 0
  67. #define DFL_READ_TIMEOUT 0
  68. #define DFL_CA_FILE ""
  69. #define DFL_CA_PATH ""
  70. #define DFL_CRT_FILE ""
  71. #define DFL_KEY_FILE ""
  72. #define DFL_KEY_PWD ""
  73. #define DFL_CRT_FILE2 ""
  74. #define DFL_KEY_FILE2 ""
  75. #define DFL_KEY_PWD2 ""
  76. #define DFL_ASYNC_OPERATIONS "-"
  77. #define DFL_ASYNC_PRIVATE_DELAY1 ( -1 )
  78. #define DFL_ASYNC_PRIVATE_DELAY2 ( -1 )
  79. #define DFL_ASYNC_PRIVATE_ERROR ( 0 )
  80. #define DFL_PSK ""
  81. #define DFL_PSK_OPAQUE 0
  82. #define DFL_PSK_LIST_OPAQUE 0
  83. #define DFL_PSK_IDENTITY "Client_identity"
  84. #define DFL_ECJPAKE_PW NULL
  85. #define DFL_PSK_LIST NULL
  86. #define DFL_FORCE_CIPHER 0
  87. #define DFL_VERSION_SUITES NULL
  88. #define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
  89. #define DFL_ALLOW_LEGACY -2
  90. #define DFL_RENEGOTIATE 0
  91. #define DFL_RENEGO_DELAY -2
  92. #define DFL_RENEGO_PERIOD ( (uint64_t)-1 )
  93. #define DFL_EXCHANGES 1
  94. #define DFL_MIN_VERSION -1
  95. #define DFL_MAX_VERSION -1
  96. #define DFL_ARC4 -1
  97. #define DFL_SHA1 -1
  98. #define DFL_CID_ENABLED 0
  99. #define DFL_CID_VALUE ""
  100. #define DFL_CID_ENABLED_RENEGO -1
  101. #define DFL_CID_VALUE_RENEGO NULL
  102. #define DFL_AUTH_MODE -1
  103. #define DFL_CERT_REQ_CA_LIST MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED
  104. #define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
  105. #define DFL_TRUNC_HMAC -1
  106. #define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
  107. #define DFL_TICKET_TIMEOUT 86400
  108. #define DFL_CACHE_MAX -1
  109. #define DFL_CACHE_TIMEOUT -1
  110. #define DFL_SNI NULL
  111. #define DFL_ALPN_STRING NULL
  112. #define DFL_CURVES NULL
  113. #define DFL_DHM_FILE NULL
  114. #define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
  115. #define DFL_COOKIES 1
  116. #define DFL_ANTI_REPLAY -1
  117. #define DFL_HS_TO_MIN 0
  118. #define DFL_HS_TO_MAX 0
  119. #define DFL_DTLS_MTU -1
  120. #define DFL_BADMAC_LIMIT -1
  121. #define DFL_DGRAM_PACKING 1
  122. #define DFL_EXTENDED_MS -1
  123. #define DFL_ETM -1
  124. #define DFL_SERIALIZE 0
  125. #define DFL_CONTEXT_FILE ""
  126. #define DFL_EXTENDED_MS_ENFORCE -1
  127. #define DFL_CA_CALLBACK 0
  128. #define DFL_EAP_TLS 0
  129. #define DFL_REPRODUCIBLE 0
  130. #define DFL_NSS_KEYLOG 0
  131. #define DFL_NSS_KEYLOG_FILE NULL
  132. #define DFL_QUERY_CONFIG_MODE 0
  133. #define DFL_USE_SRTP 0
  134. #define DFL_SRTP_FORCE_PROFILE 0
  135. #define DFL_SRTP_SUPPORT_MKI 0
  136. #define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  137. "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  138. "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  139. "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  140. "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  141. "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  142. "07-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah</p>\r\n"
  143. /* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
  144. * packets (for fragmentation purposes) */
  145. #define HTTP_RESPONSE \
  146. "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
  147. "<h2>mbed TLS Test Server</h2>\r\n" \
  148. "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
  149. /*
  150. * Size of the basic I/O buffer. Able to hold our default response.
  151. *
  152. * You will need to adapt the mbedtls_ssl_get_bytes_avail() test in ssl-opt.sh
  153. * if you change this value to something outside the range <= 100 or > 500
  154. */
  155. #define DFL_IO_BUF_LEN 200
  156. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  157. #if defined(MBEDTLS_FS_IO)
  158. #define USAGE_IO \
  159. " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
  160. " default: \"\" (pre-loaded)\n" \
  161. " use \"none\" to skip loading any top-level CAs.\n" \
  162. " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
  163. " default: \"\" (pre-loaded) (overrides ca_file)\n" \
  164. " use \"none\" to skip loading any top-level CAs.\n" \
  165. " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
  166. " default: see note after key_file2\n" \
  167. " key_file=%%s default: see note after key_file2\n" \
  168. " key_pwd=%%s Password for key specified by key_file argument\n"\
  169. " default: none\n" \
  170. " crt_file2=%%s Your second cert and chain (in bottom to top order, top may be omitted)\n" \
  171. " default: see note after key_file2\n" \
  172. " key_file2=%%s default: see note below\n" \
  173. " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
  174. " preloaded certificate(s) and key(s) are used if available\n" \
  175. " key_pwd2=%%s Password for key specified by key_file2 argument\n"\
  176. " default: none\n" \
  177. " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
  178. " default: preloaded parameters\n"
  179. #else
  180. #define USAGE_IO \
  181. "\n" \
  182. " No file operations available (MBEDTLS_FS_IO not defined)\n" \
  183. "\n"
  184. #endif /* MBEDTLS_FS_IO */
  185. #else
  186. #define USAGE_IO ""
  187. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  188. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  189. #define USAGE_SSL_ASYNC \
  190. " async_operations=%%c... d=decrypt, s=sign (default: -=off)\n" \
  191. " async_private_delay1=%%d Asynchronous delay for key_file or preloaded key\n" \
  192. " async_private_delay2=%%d Asynchronous delay for key_file2 and sni\n" \
  193. " default: -1 (not asynchronous)\n" \
  194. " async_private_error=%%d Async callback error injection (default=0=none,\n" \
  195. " 1=start, 2=cancel, 3=resume, negative=first time only)"
  196. #else
  197. #define USAGE_SSL_ASYNC ""
  198. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  199. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  200. #define USAGE_CID \
  201. " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
  202. " default: 0 (disabled)\n" \
  203. " cid_renego=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension during renegotiation.\n" \
  204. " default: same as 'cid' parameter\n" \
  205. " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
  206. " default: \"\"\n" \
  207. " cid_val_renego=%%s The CID to use for incoming messages (in hex, without 0x) after renegotiation.\n" \
  208. " default: same as 'cid_val' parameter\n"
  209. #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  210. #define USAGE_CID ""
  211. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  212. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  213. #define USAGE_PSK_RAW \
  214. " psk=%%s default: \"\" (disabled)\n" \
  215. " The PSK values are in hex, without 0x.\n" \
  216. " psk_list=%%s default: \"\"\n" \
  217. " A list of (PSK identity, PSK value) pairs.\n" \
  218. " The PSK values are in hex, without 0x.\n" \
  219. " id1,psk1[,id2,psk2[,...]]\n" \
  220. " psk_identity=%%s default: \"Client_identity\"\n"
  221. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  222. #define USAGE_PSK_SLOT \
  223. " psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \
  224. " Enable this to store the PSK configured through command line\n" \
  225. " parameter `psk` in a PSA-based key slot.\n" \
  226. " Note: Currently only supported in conjunction with\n" \
  227. " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
  228. " to force a particular PSK-only ciphersuite.\n" \
  229. " Note: This is to test integration of PSA-based opaque PSKs with\n" \
  230. " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
  231. " with prepopulated key slots instead of importing raw key material.\n" \
  232. " psk_list_opaque=%%d default: 0 (don't use opaque dynamic PSKs)\n" \
  233. " Enable this to store the list of dynamically chosen PSKs configured\n" \
  234. " through the command line parameter `psk_list` in PSA-based key slots.\n" \
  235. " Note: Currently only supported in conjunction with\n" \
  236. " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
  237. " to force a particular PSK-only ciphersuite.\n" \
  238. " Note: This is to test integration of PSA-based opaque PSKs with\n" \
  239. " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
  240. " with prepopulated key slots instead of importing raw key material.\n"
  241. #else
  242. #define USAGE_PSK_SLOT ""
  243. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  244. #define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT
  245. #else
  246. #define USAGE_PSK ""
  247. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  248. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  249. #define USAGE_CA_CALLBACK \
  250. " ca_callback=%%d default: 0 (disabled)\n" \
  251. " Enable this to use the trusted certificate callback function\n"
  252. #else
  253. #define USAGE_CA_CALLBACK ""
  254. #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
  255. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  256. #define USAGE_TICKETS \
  257. " tickets=%%d default: 1 (enabled)\n" \
  258. " ticket_timeout=%%d default: 86400 (one day)\n"
  259. #else
  260. #define USAGE_TICKETS ""
  261. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  262. #if defined(MBEDTLS_SSL_EXPORT_KEYS)
  263. #define USAGE_EAP_TLS \
  264. " eap_tls=%%d default: 0 (disabled)\n"
  265. #define USAGE_NSS_KEYLOG \
  266. " nss_keylog=%%d default: 0 (disabled)\n" \
  267. " This cannot be used with eap_tls=1\n"
  268. #define USAGE_NSS_KEYLOG_FILE \
  269. " nss_keylog_file=%%s\n"
  270. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  271. #define USAGE_SRTP \
  272. " use_srtp=%%d default: 0 (disabled)\n" \
  273. " srtp_force_profile=%%d default: 0 (all enabled)\n" \
  274. " available profiles:\n" \
  275. " 1 - SRTP_AES128_CM_HMAC_SHA1_80\n" \
  276. " 2 - SRTP_AES128_CM_HMAC_SHA1_32\n" \
  277. " 3 - SRTP_NULL_HMAC_SHA1_80\n" \
  278. " 4 - SRTP_NULL_HMAC_SHA1_32\n" \
  279. " support_mki=%%d default: 0 (not supported)\n"
  280. #else /* MBEDTLS_SSL_DTLS_SRTP */
  281. #define USAGE_SRTP ""
  282. #endif
  283. #else /* MBEDTLS_SSL_EXPORT_KEYS */
  284. #define USAGE_EAP_TLS ""
  285. #define USAGE_NSS_KEYLOG ""
  286. #define USAGE_NSS_KEYLOG_FILE ""
  287. #define USAGE_SRTP ""
  288. #endif /* MBEDTLS_SSL_EXPORT_KEYS */
  289. #if defined(MBEDTLS_SSL_CACHE_C)
  290. #define USAGE_CACHE \
  291. " cache_max=%%d default: cache default (50)\n" \
  292. " cache_timeout=%%d default: cache default (1d)\n"
  293. #else
  294. #define USAGE_CACHE ""
  295. #endif /* MBEDTLS_SSL_CACHE_C */
  296. #if defined(SNI_OPTION)
  297. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  298. #define SNI_CRL ",crl"
  299. #else
  300. #define SNI_CRL ""
  301. #endif
  302. #define USAGE_SNI \
  303. " sni=%%s name1,cert1,key1,ca1"SNI_CRL",auth1[,...]\n" \
  304. " default: disabled\n"
  305. #else
  306. #define USAGE_SNI ""
  307. #endif /* SNI_OPTION */
  308. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  309. #define USAGE_MAX_FRAG_LEN \
  310. " max_frag_len=%%d default: 16384 (tls default)\n" \
  311. " options: 512, 1024, 2048, 4096\n"
  312. #else
  313. #define USAGE_MAX_FRAG_LEN ""
  314. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  315. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  316. #define USAGE_TRUNC_HMAC \
  317. " trunc_hmac=%%d default: library default\n"
  318. #else
  319. #define USAGE_TRUNC_HMAC ""
  320. #endif
  321. #if defined(MBEDTLS_SSL_ALPN)
  322. #define USAGE_ALPN \
  323. " alpn=%%s default: \"\" (disabled)\n" \
  324. " example: spdy/1,http/1.1\n"
  325. #else
  326. #define USAGE_ALPN ""
  327. #endif /* MBEDTLS_SSL_ALPN */
  328. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  329. #define USAGE_COOKIES \
  330. " cookies=0/1/-1 default: 1 (enabled)\n" \
  331. " 0: disabled, -1: library default (broken)\n"
  332. #else
  333. #define USAGE_COOKIES ""
  334. #endif
  335. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  336. #define USAGE_ANTI_REPLAY \
  337. " anti_replay=0/1 default: (library default: enabled)\n"
  338. #else
  339. #define USAGE_ANTI_REPLAY ""
  340. #endif
  341. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  342. #define USAGE_BADMAC_LIMIT \
  343. " badmac_limit=%%d default: (library default: disabled)\n"
  344. #else
  345. #define USAGE_BADMAC_LIMIT ""
  346. #endif
  347. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  348. #define USAGE_DTLS \
  349. " dtls=%%d default: 0 (TLS)\n" \
  350. " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
  351. " range of DTLS handshake timeouts in millisecs\n" \
  352. " mtu=%%d default: (library default: unlimited)\n" \
  353. " dgram_packing=%%d default: 1 (allowed)\n" \
  354. " allow or forbid packing of multiple\n" \
  355. " records within a single datgram.\n"
  356. #else
  357. #define USAGE_DTLS ""
  358. #endif
  359. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  360. #define USAGE_EMS \
  361. " extended_ms=0/1 default: (library default: on)\n"
  362. #else
  363. #define USAGE_EMS ""
  364. #endif
  365. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  366. #define USAGE_ETM \
  367. " etm=0/1 default: (library default: on)\n"
  368. #else
  369. #define USAGE_ETM ""
  370. #endif
  371. #define USAGE_REPRODUCIBLE \
  372. " reproducible=0/1 default: 0 (disabled)\n"
  373. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  374. #define USAGE_RENEGO \
  375. " renegotiation=%%d default: 0 (disabled)\n" \
  376. " renegotiate=%%d default: 0 (disabled)\n" \
  377. " renego_delay=%%d default: -2 (library default)\n" \
  378. " renego_period=%%d default: (2^64 - 1 for TLS, 2^48 - 1 for DTLS)\n"
  379. #else
  380. #define USAGE_RENEGO ""
  381. #endif
  382. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  383. #define USAGE_ECJPAKE \
  384. " ecjpake_pw=%%s default: none (disabled)\n"
  385. #else
  386. #define USAGE_ECJPAKE ""
  387. #endif
  388. #if defined(MBEDTLS_ECP_C)
  389. #define USAGE_CURVES \
  390. " curves=a,b,c,d default: \"default\" (library default)\n" \
  391. " example: \"secp521r1,brainpoolP512r1\"\n" \
  392. " - use \"none\" for empty list\n" \
  393. " - see mbedtls_ecp_curve_list()\n" \
  394. " for acceptable curve names\n"
  395. #else
  396. #define USAGE_CURVES ""
  397. #endif
  398. #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
  399. #define USAGE_SERIALIZATION \
  400. " serialize=%%d default: 0 (do not serialize/deserialize)\n" \
  401. " options: 1 (serialize)\n" \
  402. " 2 (serialize with re-initialization)\n" \
  403. " context_file=%%s The file path to write a serialized connection\n"\
  404. " in the form of base64 code (serialize option\n" \
  405. " must be set)\n" \
  406. " default: \"\" (do nothing)\n" \
  407. " option: a file path\n"
  408. #else
  409. #define USAGE_SERIALIZATION ""
  410. #endif
  411. /* USAGE is arbitrarily split to stay under the portable string literal
  412. * length limit: 4095 bytes in C99. */
  413. #define USAGE1 \
  414. "\n usage: ssl_server2 param=<>...\n" \
  415. "\n acceptable parameters:\n" \
  416. " server_addr=%%s default: (all interfaces)\n" \
  417. " server_port=%%d default: 4433\n" \
  418. " debug_level=%%d default: 0 (disabled)\n" \
  419. " buffer_size=%%d default: 200 \n" \
  420. " (minimum: 1, max: 16385)\n" \
  421. " response_size=%%d default: about 152 (basic response)\n" \
  422. " (minimum: 0, max: 16384)\n" \
  423. " increases buffer_size if bigger\n"\
  424. " nbio=%%d default: 0 (blocking I/O)\n" \
  425. " options: 1 (non-blocking), 2 (added delays)\n" \
  426. " event=%%d default: 0 (loop)\n" \
  427. " options: 1 (level-triggered, implies nbio=1),\n" \
  428. " read_timeout=%%d default: 0 ms (no timeout)\n" \
  429. "\n" \
  430. USAGE_DTLS \
  431. USAGE_SRTP \
  432. USAGE_COOKIES \
  433. USAGE_ANTI_REPLAY \
  434. USAGE_BADMAC_LIMIT \
  435. "\n"
  436. #define USAGE2 \
  437. " auth_mode=%%s default: (library default: none)\n" \
  438. " options: none, optional, required\n" \
  439. " cert_req_ca_list=%%d default: 1 (send ca list)\n" \
  440. " options: 1 (send ca list), 0 (don't send)\n" \
  441. USAGE_IO \
  442. "\n" \
  443. USAGE_PSK \
  444. USAGE_CA_CALLBACK \
  445. USAGE_ECJPAKE \
  446. "\n"
  447. #define USAGE3 \
  448. " allow_legacy=%%d default: (library default: no)\n" \
  449. USAGE_RENEGO \
  450. " exchanges=%%d default: 1\n" \
  451. "\n" \
  452. USAGE_TICKETS \
  453. USAGE_EAP_TLS \
  454. USAGE_REPRODUCIBLE \
  455. USAGE_NSS_KEYLOG \
  456. USAGE_NSS_KEYLOG_FILE \
  457. USAGE_CACHE \
  458. USAGE_MAX_FRAG_LEN \
  459. USAGE_TRUNC_HMAC \
  460. USAGE_ALPN \
  461. USAGE_EMS \
  462. USAGE_ETM \
  463. USAGE_CURVES \
  464. "\n"
  465. #define USAGE4 \
  466. USAGE_SSL_ASYNC \
  467. USAGE_SNI \
  468. " arc4=%%d default: (library default: 0)\n" \
  469. " allow_sha1=%%d default: 0\n" \
  470. " min_version=%%s default: (library default: tls1)\n" \
  471. " max_version=%%s default: (library default: tls12)\n" \
  472. " force_version=%%s default: \"\" (none)\n" \
  473. " options: ssl3, tls1, tls1_1, tls12, dtls1, dtls12\n" \
  474. "\n" \
  475. " version_suites=a,b,c,d per-version ciphersuites\n" \
  476. " in order from ssl3 to tls12\n" \
  477. " default: all enabled\n" \
  478. " force_ciphersuite=<name> default: all enabled\n" \
  479. " query_config=<name> return 0 if the specified\n" \
  480. " configuration macro is defined and 1\n" \
  481. " otherwise. The expansion of the macro\n" \
  482. " is printed if it is defined\n" \
  483. USAGE_SERIALIZATION \
  484. " acceptable ciphersuite names:\n"
  485. #define ALPN_LIST_SIZE 10
  486. #define CURVE_LIST_SIZE 20
  487. #define PUT_UINT64_BE(out_be,in_le,i) \
  488. { \
  489. (out_be)[(i) + 0] = (unsigned char)( ( (in_le) >> 56 ) & 0xFF ); \
  490. (out_be)[(i) + 1] = (unsigned char)( ( (in_le) >> 48 ) & 0xFF ); \
  491. (out_be)[(i) + 2] = (unsigned char)( ( (in_le) >> 40 ) & 0xFF ); \
  492. (out_be)[(i) + 3] = (unsigned char)( ( (in_le) >> 32 ) & 0xFF ); \
  493. (out_be)[(i) + 4] = (unsigned char)( ( (in_le) >> 24 ) & 0xFF ); \
  494. (out_be)[(i) + 5] = (unsigned char)( ( (in_le) >> 16 ) & 0xFF ); \
  495. (out_be)[(i) + 6] = (unsigned char)( ( (in_le) >> 8 ) & 0xFF ); \
  496. (out_be)[(i) + 7] = (unsigned char)( ( (in_le) >> 0 ) & 0xFF ); \
  497. }
  498. /*
  499. * global options
  500. */
  501. struct options
  502. {
  503. const char *server_addr; /* address on which the ssl service runs */
  504. const char *server_port; /* port on which the ssl service runs */
  505. int debug_level; /* level of debugging */
  506. int nbio; /* should I/O be blocking? */
  507. int event; /* loop or event-driven IO? level or edge triggered? */
  508. uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
  509. int response_size; /* pad response with header to requested size */
  510. uint16_t buffer_size; /* IO buffer size */
  511. const char *ca_file; /* the file with the CA certificate(s) */
  512. const char *ca_path; /* the path with the CA certificate(s) reside */
  513. const char *crt_file; /* the file with the server certificate */
  514. const char *key_file; /* the file with the server key */
  515. const char *key_pwd; /* the password for the server key */
  516. const char *crt_file2; /* the file with the 2nd server certificate */
  517. const char *key_file2; /* the file with the 2nd server key */
  518. const char *key_pwd2; /* the password for the 2nd server key */
  519. const char *async_operations; /* supported SSL asynchronous operations */
  520. int async_private_delay1; /* number of times f_async_resume needs to be called for key 1, or -1 for no async */
  521. int async_private_delay2; /* number of times f_async_resume needs to be called for key 2, or -1 for no async */
  522. int async_private_error; /* inject error in async private callback */
  523. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  524. int psk_opaque;
  525. int psk_list_opaque;
  526. #endif
  527. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  528. int ca_callback; /* Use callback for trusted certificate list */
  529. #endif
  530. const char *psk; /* the pre-shared key */
  531. const char *psk_identity; /* the pre-shared key identity */
  532. char *psk_list; /* list of PSK id/key pairs for callback */
  533. const char *ecjpake_pw; /* the EC J-PAKE password */
  534. int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
  535. const char *version_suites; /* per-version ciphersuites */
  536. int renegotiation; /* enable / disable renegotiation */
  537. int allow_legacy; /* allow legacy renegotiation */
  538. int renegotiate; /* attempt renegotiation? */
  539. int renego_delay; /* delay before enforcing renegotiation */
  540. uint64_t renego_period; /* period for automatic renegotiation */
  541. int exchanges; /* number of data exchanges */
  542. int min_version; /* minimum protocol version accepted */
  543. int max_version; /* maximum protocol version accepted */
  544. int arc4; /* flag for arc4 suites support */
  545. int allow_sha1; /* flag for SHA-1 support */
  546. int auth_mode; /* verify mode for connection */
  547. int cert_req_ca_list; /* should we send the CA list? */
  548. unsigned char mfl_code; /* code for maximum fragment length */
  549. int trunc_hmac; /* accept truncated hmac? */
  550. int tickets; /* enable / disable session tickets */
  551. int ticket_timeout; /* session ticket lifetime */
  552. int cache_max; /* max number of session cache entries */
  553. int cache_timeout; /* expiration delay of session cache entries */
  554. char *sni; /* string describing sni information */
  555. const char *curves; /* list of supported elliptic curves */
  556. const char *alpn_string; /* ALPN supported protocols */
  557. const char *dhm_file; /* the file with the DH parameters */
  558. int extended_ms; /* allow negotiation of extended MS? */
  559. int etm; /* allow negotiation of encrypt-then-MAC? */
  560. int transport; /* TLS or DTLS? */
  561. int cookies; /* Use cookies for DTLS? -1 to break them */
  562. int anti_replay; /* Use anti-replay for DTLS? -1 for default */
  563. uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
  564. uint32_t hs_to_max; /* Max value of DTLS handshake timer */
  565. int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
  566. int dgram_packing; /* allow/forbid datagram packing */
  567. int badmac_limit; /* Limit of records with bad MAC */
  568. int eap_tls; /* derive EAP-TLS keying material? */
  569. int nss_keylog; /* export NSS key log material */
  570. const char *nss_keylog_file; /* NSS key log file */
  571. int cid_enabled; /* whether to use the CID extension or not */
  572. int cid_enabled_renego; /* whether to use the CID extension or not
  573. * during renegotiation */
  574. const char *cid_val; /* the CID to use for incoming messages */
  575. int serialize; /* serialize/deserialize connection */
  576. const char *context_file; /* the file to write a serialized connection
  577. * in the form of base64 code (serialize
  578. * option must be set) */
  579. const char *cid_val_renego; /* the CID to use for incoming messages
  580. * after renegotiation */
  581. int reproducible; /* make communication reproducible */
  582. int query_config_mode; /* whether to read config */
  583. int use_srtp; /* Support SRTP */
  584. int force_srtp_profile; /* SRTP protection profile to use or all */
  585. int support_mki; /* The dtls mki mki support */
  586. } opt;
  587. #include "ssl_test_common_source.c"
  588. /*
  589. * Return authmode from string, or -1 on error
  590. */
  591. static int get_auth_mode( const char *s )
  592. {
  593. if( strcmp( s, "none" ) == 0 )
  594. return( MBEDTLS_SSL_VERIFY_NONE );
  595. if( strcmp( s, "optional" ) == 0 )
  596. return( MBEDTLS_SSL_VERIFY_OPTIONAL );
  597. if( strcmp( s, "required" ) == 0 )
  598. return( MBEDTLS_SSL_VERIFY_REQUIRED );
  599. return( -1 );
  600. }
  601. /*
  602. * Used by sni_parse and psk_parse to handle coma-separated lists
  603. */
  604. #define GET_ITEM( dst ) \
  605. do \
  606. { \
  607. (dst) = p; \
  608. while( *p != ',' ) \
  609. if( ++p > end ) \
  610. goto error; \
  611. *p++ = '\0'; \
  612. } while( 0 )
  613. #if defined(SNI_OPTION)
  614. typedef struct _sni_entry sni_entry;
  615. struct _sni_entry {
  616. const char *name;
  617. mbedtls_x509_crt *cert;
  618. mbedtls_pk_context *key;
  619. mbedtls_x509_crt* ca;
  620. mbedtls_x509_crl* crl;
  621. int authmode;
  622. sni_entry *next;
  623. };
  624. void sni_free( sni_entry *head )
  625. {
  626. sni_entry *cur = head, *next;
  627. while( cur != NULL )
  628. {
  629. mbedtls_x509_crt_free( cur->cert );
  630. mbedtls_free( cur->cert );
  631. mbedtls_pk_free( cur->key );
  632. mbedtls_free( cur->key );
  633. mbedtls_x509_crt_free( cur->ca );
  634. mbedtls_free( cur->ca );
  635. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  636. mbedtls_x509_crl_free( cur->crl );
  637. mbedtls_free( cur->crl );
  638. #endif
  639. next = cur->next;
  640. mbedtls_free( cur );
  641. cur = next;
  642. }
  643. }
  644. /*
  645. * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...]
  646. * into a usable sni_entry list. For ca1, crl1, auth1, the special value
  647. * '-' means unset. If ca1 is unset, then crl1 is ignored too.
  648. *
  649. * Modifies the input string! This is not production quality!
  650. */
  651. sni_entry *sni_parse( char *sni_string )
  652. {
  653. sni_entry *cur = NULL, *new = NULL;
  654. char *p = sni_string;
  655. char *end = p;
  656. char *crt_file, *key_file, *ca_file, *auth_str;
  657. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  658. char *crl_file;
  659. #endif
  660. while( *end != '\0' )
  661. ++end;
  662. *end = ',';
  663. while( p <= end )
  664. {
  665. if( ( new = mbedtls_calloc( 1, sizeof( sni_entry ) ) ) == NULL )
  666. {
  667. sni_free( cur );
  668. return( NULL );
  669. }
  670. GET_ITEM( new->name );
  671. GET_ITEM( crt_file );
  672. GET_ITEM( key_file );
  673. GET_ITEM( ca_file );
  674. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  675. GET_ITEM( crl_file );
  676. #endif
  677. GET_ITEM( auth_str );
  678. if( ( new->cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL ||
  679. ( new->key = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) ) ) == NULL )
  680. goto error;
  681. mbedtls_x509_crt_init( new->cert );
  682. mbedtls_pk_init( new->key );
  683. if( mbedtls_x509_crt_parse_file( new->cert, crt_file ) != 0 ||
  684. mbedtls_pk_parse_keyfile( new->key, key_file, "" ) != 0 )
  685. goto error;
  686. if( strcmp( ca_file, "-" ) != 0 )
  687. {
  688. if( ( new->ca = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
  689. goto error;
  690. mbedtls_x509_crt_init( new->ca );
  691. if( mbedtls_x509_crt_parse_file( new->ca, ca_file ) != 0 )
  692. goto error;
  693. }
  694. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  695. if( strcmp( crl_file, "-" ) != 0 )
  696. {
  697. if( ( new->crl = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) ) ) == NULL )
  698. goto error;
  699. mbedtls_x509_crl_init( new->crl );
  700. if( mbedtls_x509_crl_parse_file( new->crl, crl_file ) != 0 )
  701. goto error;
  702. }
  703. #endif
  704. if( strcmp( auth_str, "-" ) != 0 )
  705. {
  706. if( ( new->authmode = get_auth_mode( auth_str ) ) < 0 )
  707. goto error;
  708. }
  709. else
  710. new->authmode = DFL_AUTH_MODE;
  711. new->next = cur;
  712. cur = new;
  713. }
  714. return( cur );
  715. error:
  716. sni_free( new );
  717. sni_free( cur );
  718. return( NULL );
  719. }
  720. /*
  721. * SNI callback.
  722. */
  723. int sni_callback( void *p_info, mbedtls_ssl_context *ssl,
  724. const unsigned char *name, size_t name_len )
  725. {
  726. const sni_entry *cur = (const sni_entry *) p_info;
  727. while( cur != NULL )
  728. {
  729. if( name_len == strlen( cur->name ) &&
  730. memcmp( name, cur->name, name_len ) == 0 )
  731. {
  732. if( cur->ca != NULL )
  733. mbedtls_ssl_set_hs_ca_chain( ssl, cur->ca, cur->crl );
  734. if( cur->authmode != DFL_AUTH_MODE )
  735. mbedtls_ssl_set_hs_authmode( ssl, cur->authmode );
  736. return( mbedtls_ssl_set_hs_own_cert( ssl, cur->cert, cur->key ) );
  737. }
  738. cur = cur->next;
  739. }
  740. return( -1 );
  741. }
  742. #endif /* SNI_OPTION */
  743. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  744. typedef struct _psk_entry psk_entry;
  745. struct _psk_entry
  746. {
  747. const char *name;
  748. size_t key_len;
  749. unsigned char key[MBEDTLS_PSK_MAX_LEN];
  750. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  751. psa_key_id_t slot;
  752. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  753. psk_entry *next;
  754. };
  755. /*
  756. * Free a list of psk_entry's
  757. */
  758. int psk_free( psk_entry *head )
  759. {
  760. psk_entry *next;
  761. while( head != NULL )
  762. {
  763. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  764. psa_status_t status;
  765. psa_key_id_t const slot = head->slot;
  766. if( slot != 0 )
  767. {
  768. status = psa_destroy_key( slot );
  769. if( status != PSA_SUCCESS )
  770. return( status );
  771. }
  772. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  773. next = head->next;
  774. mbedtls_free( head );
  775. head = next;
  776. }
  777. return( 0 );
  778. }
  779. /*
  780. * Parse a string of pairs name1,key1[,name2,key2[,...]]
  781. * into a usable psk_entry list.
  782. *
  783. * Modifies the input string! This is not production quality!
  784. */
  785. psk_entry *psk_parse( char *psk_string )
  786. {
  787. psk_entry *cur = NULL, *new = NULL;
  788. char *p = psk_string;
  789. char *end = p;
  790. char *key_hex;
  791. while( *end != '\0' )
  792. ++end;
  793. *end = ',';
  794. while( p <= end )
  795. {
  796. if( ( new = mbedtls_calloc( 1, sizeof( psk_entry ) ) ) == NULL )
  797. goto error;
  798. memset( new, 0, sizeof( psk_entry ) );
  799. GET_ITEM( new->name );
  800. GET_ITEM( key_hex );
  801. if( mbedtls_test_unhexify( new->key, MBEDTLS_PSK_MAX_LEN,
  802. key_hex, &new->key_len ) != 0 )
  803. goto error;
  804. new->next = cur;
  805. cur = new;
  806. }
  807. return( cur );
  808. error:
  809. psk_free( new );
  810. psk_free( cur );
  811. return( 0 );
  812. }
  813. /*
  814. * PSK callback
  815. */
  816. int psk_callback( void *p_info, mbedtls_ssl_context *ssl,
  817. const unsigned char *name, size_t name_len )
  818. {
  819. psk_entry *cur = (psk_entry *) p_info;
  820. while( cur != NULL )
  821. {
  822. if( name_len == strlen( cur->name ) &&
  823. memcmp( name, cur->name, name_len ) == 0 )
  824. {
  825. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  826. if( cur->slot != 0 )
  827. return( mbedtls_ssl_set_hs_psk_opaque( ssl, cur->slot ) );
  828. else
  829. #endif
  830. return( mbedtls_ssl_set_hs_psk( ssl, cur->key, cur->key_len ) );
  831. }
  832. cur = cur->next;
  833. }
  834. return( -1 );
  835. }
  836. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  837. static mbedtls_net_context listen_fd, client_fd;
  838. /* Interruption handler to ensure clean exit (for valgrind testing) */
  839. #if !defined(_WIN32)
  840. static int received_sigterm = 0;
  841. void term_handler( int sig )
  842. {
  843. ((void) sig);
  844. received_sigterm = 1;
  845. mbedtls_net_free( &listen_fd ); /* causes mbedtls_net_accept() to abort */
  846. mbedtls_net_free( &client_fd ); /* causes net_read() to abort */
  847. }
  848. #endif
  849. /** Return true if \p ret is a status code indicating that there is an
  850. * operation in progress on an SSL connection, and false if it indicates
  851. * success or a fatal error.
  852. *
  853. * The possible operations in progress are:
  854. *
  855. * - A read, when the SSL input buffer does not contain a full message.
  856. * - A write, when the SSL output buffer contains some data that has not
  857. * been sent over the network yet.
  858. * - An asynchronous callback that has not completed yet. */
  859. static int mbedtls_status_is_ssl_in_progress( int ret )
  860. {
  861. return( ret == MBEDTLS_ERR_SSL_WANT_READ ||
  862. ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
  863. ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
  864. }
  865. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  866. typedef struct
  867. {
  868. mbedtls_x509_crt *cert; /*!< Certificate corresponding to the key */
  869. mbedtls_pk_context *pk; /*!< Private key */
  870. unsigned delay; /*!< Number of resume steps to go through */
  871. unsigned pk_owned : 1; /*!< Whether to free the pk object on exit */
  872. } ssl_async_key_slot_t;
  873. typedef enum {
  874. SSL_ASYNC_INJECT_ERROR_NONE = 0, /*!< Let the callbacks succeed */
  875. SSL_ASYNC_INJECT_ERROR_START, /*!< Inject error during start */
  876. SSL_ASYNC_INJECT_ERROR_CANCEL, /*!< Close the connection after async start */
  877. SSL_ASYNC_INJECT_ERROR_RESUME, /*!< Inject error during resume */
  878. #define SSL_ASYNC_INJECT_ERROR_MAX SSL_ASYNC_INJECT_ERROR_RESUME
  879. } ssl_async_inject_error_t;
  880. typedef struct
  881. {
  882. ssl_async_key_slot_t slots[4]; /* key, key2, sni1, sni2 */
  883. size_t slots_used;
  884. ssl_async_inject_error_t inject_error;
  885. int (*f_rng)(void *, unsigned char *, size_t);
  886. void *p_rng;
  887. } ssl_async_key_context_t;
  888. int ssl_async_set_key( ssl_async_key_context_t *ctx,
  889. mbedtls_x509_crt *cert,
  890. mbedtls_pk_context *pk,
  891. int pk_take_ownership,
  892. unsigned delay )
  893. {
  894. if( ctx->slots_used >= sizeof( ctx->slots ) / sizeof( *ctx->slots ) )
  895. return( -1 );
  896. ctx->slots[ctx->slots_used].cert = cert;
  897. ctx->slots[ctx->slots_used].pk = pk;
  898. ctx->slots[ctx->slots_used].delay = delay;
  899. ctx->slots[ctx->slots_used].pk_owned = pk_take_ownership;
  900. ++ctx->slots_used;
  901. return( 0 );
  902. }
  903. #define SSL_ASYNC_INPUT_MAX_SIZE 512
  904. typedef enum
  905. {
  906. ASYNC_OP_SIGN,
  907. ASYNC_OP_DECRYPT,
  908. } ssl_async_operation_type_t;
  909. /* Note that the enum above and the array below need to be kept in sync!
  910. * `ssl_async_operation_names[op]` is the name of op for each value `op`
  911. * of type `ssl_async_operation_type_t`. */
  912. static const char *const ssl_async_operation_names[] =
  913. {
  914. "sign",
  915. "decrypt",
  916. };
  917. typedef struct
  918. {
  919. unsigned slot;
  920. ssl_async_operation_type_t operation_type;
  921. mbedtls_md_type_t md_alg;
  922. unsigned char input[SSL_ASYNC_INPUT_MAX_SIZE];
  923. size_t input_len;
  924. unsigned remaining_delay;
  925. } ssl_async_operation_context_t;
  926. static int ssl_async_start( mbedtls_ssl_context *ssl,
  927. mbedtls_x509_crt *cert,
  928. ssl_async_operation_type_t op_type,
  929. mbedtls_md_type_t md_alg,
  930. const unsigned char *input,
  931. size_t input_len )
  932. {
  933. ssl_async_key_context_t *config_data =
  934. mbedtls_ssl_conf_get_async_config_data( ssl->conf );
  935. unsigned slot;
  936. ssl_async_operation_context_t *ctx = NULL;
  937. const char *op_name = ssl_async_operation_names[op_type];
  938. {
  939. char dn[100];
  940. if( mbedtls_x509_dn_gets( dn, sizeof( dn ), &cert->subject ) > 0 )
  941. mbedtls_printf( "Async %s callback: looking for DN=%s\n",
  942. op_name, dn );
  943. }
  944. /* Look for a private key that matches the public key in cert.
  945. * Since this test code has the private key inside Mbed TLS,
  946. * we call mbedtls_pk_check_pair to match a private key with the
  947. * public key. */
  948. for( slot = 0; slot < config_data->slots_used; slot++ )
  949. {
  950. if( mbedtls_pk_check_pair( &cert->pk,
  951. config_data->slots[slot].pk ) == 0 )
  952. break;
  953. }
  954. if( slot == config_data->slots_used )
  955. {
  956. mbedtls_printf( "Async %s callback: no key matches this certificate.\n",
  957. op_name );
  958. return( MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH );
  959. }
  960. mbedtls_printf( "Async %s callback: using key slot %u, delay=%u.\n",
  961. op_name, slot, config_data->slots[slot].delay );
  962. if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_START )
  963. {
  964. mbedtls_printf( "Async %s callback: injected error\n", op_name );
  965. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  966. }
  967. if( input_len > SSL_ASYNC_INPUT_MAX_SIZE )
  968. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  969. ctx = mbedtls_calloc( 1, sizeof( *ctx ) );
  970. if( ctx == NULL )
  971. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  972. ctx->slot = slot;
  973. ctx->operation_type = op_type;
  974. ctx->md_alg = md_alg;
  975. memcpy( ctx->input, input, input_len );
  976. ctx->input_len = input_len;
  977. ctx->remaining_delay = config_data->slots[slot].delay;
  978. mbedtls_ssl_set_async_operation_data( ssl, ctx );
  979. if( ctx->remaining_delay == 0 )
  980. return( 0 );
  981. else
  982. return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
  983. }
  984. static int ssl_async_sign( mbedtls_ssl_context *ssl,
  985. mbedtls_x509_crt *cert,
  986. mbedtls_md_type_t md_alg,
  987. const unsigned char *hash,
  988. size_t hash_len )
  989. {
  990. return( ssl_async_start( ssl, cert,
  991. ASYNC_OP_SIGN, md_alg,
  992. hash, hash_len ) );
  993. }
  994. static int ssl_async_decrypt( mbedtls_ssl_context *ssl,
  995. mbedtls_x509_crt *cert,
  996. const unsigned char *input,
  997. size_t input_len )
  998. {
  999. return( ssl_async_start( ssl, cert,
  1000. ASYNC_OP_DECRYPT, MBEDTLS_MD_NONE,
  1001. input, input_len ) );
  1002. }
  1003. static int ssl_async_resume( mbedtls_ssl_context *ssl,
  1004. unsigned char *output,
  1005. size_t *output_len,
  1006. size_t output_size )
  1007. {
  1008. ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
  1009. ssl_async_key_context_t *config_data =
  1010. mbedtls_ssl_conf_get_async_config_data( ssl->conf );
  1011. ssl_async_key_slot_t *key_slot = &config_data->slots[ctx->slot];
  1012. int ret;
  1013. const char *op_name;
  1014. if( ctx->remaining_delay > 0 )
  1015. {
  1016. --ctx->remaining_delay;
  1017. mbedtls_printf( "Async resume (slot %u): call %u more times.\n",
  1018. ctx->slot, ctx->remaining_delay );
  1019. return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
  1020. }
  1021. switch( ctx->operation_type )
  1022. {
  1023. case ASYNC_OP_DECRYPT:
  1024. ret = mbedtls_pk_decrypt( key_slot->pk,
  1025. ctx->input, ctx->input_len,
  1026. output, output_len, output_size,
  1027. config_data->f_rng, config_data->p_rng );
  1028. break;
  1029. case ASYNC_OP_SIGN:
  1030. ret = mbedtls_pk_sign( key_slot->pk,
  1031. ctx->md_alg,
  1032. ctx->input, ctx->input_len,
  1033. output, output_len,
  1034. config_data->f_rng, config_data->p_rng );
  1035. break;
  1036. default:
  1037. mbedtls_printf( "Async resume (slot %u): unknown operation type %ld. This shouldn't happen.\n",
  1038. ctx->slot, (long) ctx->operation_type );
  1039. mbedtls_free( ctx );
  1040. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  1041. break;
  1042. }
  1043. op_name = ssl_async_operation_names[ctx->operation_type];
  1044. if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_RESUME )
  1045. {
  1046. mbedtls_printf( "Async resume callback: %s done but injected error\n",
  1047. op_name );
  1048. mbedtls_free( ctx );
  1049. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  1050. }
  1051. mbedtls_printf( "Async resume (slot %u): %s done, status=%d.\n",
  1052. ctx->slot, op_name, ret );
  1053. mbedtls_free( ctx );
  1054. return( ret );
  1055. }
  1056. static void ssl_async_cancel( mbedtls_ssl_context *ssl )
  1057. {
  1058. ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
  1059. mbedtls_printf( "Async cancel callback.\n" );
  1060. mbedtls_free( ctx );
  1061. }
  1062. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  1063. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1064. static psa_status_t psa_setup_psk_key_slot( psa_key_id_t *slot,
  1065. psa_algorithm_t alg,
  1066. unsigned char *psk,
  1067. size_t psk_len )
  1068. {
  1069. psa_status_t status;
  1070. psa_key_attributes_t key_attributes;
  1071. key_attributes = psa_key_attributes_init();
  1072. psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
  1073. psa_set_key_algorithm( &key_attributes, alg );
  1074. psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
  1075. status = psa_import_key( &key_attributes, psk, psk_len, slot );
  1076. if( status != PSA_SUCCESS )
  1077. {
  1078. fprintf( stderr, "IMPORT\n" );
  1079. return( status );
  1080. }
  1081. return( PSA_SUCCESS );
  1082. }
  1083. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1084. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1085. int report_cid_usage( mbedtls_ssl_context *ssl,
  1086. const char *additional_description )
  1087. {
  1088. int ret;
  1089. unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
  1090. size_t peer_cid_len;
  1091. int cid_negotiated;
  1092. if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  1093. return( 0 );
  1094. /* Check if the use of a CID has been negotiated */
  1095. ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
  1096. peer_cid, &peer_cid_len );
  1097. if( ret != 0 )
  1098. {
  1099. mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
  1100. (unsigned int) -ret );
  1101. return( ret );
  1102. }
  1103. if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
  1104. {
  1105. if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
  1106. {
  1107. mbedtls_printf( "(%s) Use of Connection ID was not offered by client.\n",
  1108. additional_description );
  1109. }
  1110. }
  1111. else
  1112. {
  1113. size_t idx=0;
  1114. mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
  1115. additional_description );
  1116. mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
  1117. additional_description,
  1118. (unsigned) peer_cid_len );
  1119. while( idx < peer_cid_len )
  1120. {
  1121. mbedtls_printf( "%02x ", peer_cid[ idx ] );
  1122. idx++;
  1123. }
  1124. mbedtls_printf( "\n" );
  1125. }
  1126. return( 0 );
  1127. }
  1128. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  1129. int main( int argc, char *argv[] )
  1130. {
  1131. int ret = 0, len, written, frags, exchanges_left;
  1132. int query_config_ret = 0;
  1133. int version_suites[4][2];
  1134. io_ctx_t io_ctx;
  1135. unsigned char* buf = 0;
  1136. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  1137. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1138. psa_algorithm_t alg = 0;
  1139. psa_key_id_t psk_slot = 0;
  1140. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1141. unsigned char psk[MBEDTLS_PSK_MAX_LEN];
  1142. size_t psk_len = 0;
  1143. psk_entry *psk_info = NULL;
  1144. #endif
  1145. const char *pers = "ssl_server2";
  1146. unsigned char client_ip[16] = { 0 };
  1147. size_t cliip_len;
  1148. #if defined(MBEDTLS_SSL_COOKIE_C)
  1149. mbedtls_ssl_cookie_ctx cookie_ctx;
  1150. #endif
  1151. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1152. mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
  1153. #endif
  1154. rng_context_t rng;
  1155. mbedtls_ssl_context ssl;
  1156. mbedtls_ssl_config conf;
  1157. #if defined(MBEDTLS_TIMING_C)
  1158. mbedtls_timing_delay_context timer;
  1159. #endif
  1160. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1161. unsigned char renego_period[8] = { 0 };
  1162. #endif
  1163. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1164. uint32_t flags;
  1165. mbedtls_x509_crt cacert;
  1166. mbedtls_x509_crt srvcert;
  1167. mbedtls_pk_context pkey;
  1168. mbedtls_x509_crt srvcert2;
  1169. mbedtls_pk_context pkey2;
  1170. int key_cert_init = 0, key_cert_init2 = 0;
  1171. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  1172. ssl_async_key_context_t ssl_async_keys;
  1173. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  1174. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  1175. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
  1176. mbedtls_dhm_context dhm;
  1177. #endif
  1178. #if defined(MBEDTLS_SSL_CACHE_C)
  1179. mbedtls_ssl_cache_context cache;
  1180. #endif
  1181. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1182. mbedtls_ssl_ticket_context ticket_ctx;
  1183. #endif
  1184. #if defined(SNI_OPTION)
  1185. sni_entry *sni_info = NULL;
  1186. #endif
  1187. #if defined(MBEDTLS_ECP_C)
  1188. mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
  1189. const mbedtls_ecp_curve_info * curve_cur;
  1190. #endif
  1191. #if defined(MBEDTLS_SSL_ALPN)
  1192. const char *alpn_list[ALPN_LIST_SIZE];
  1193. #endif
  1194. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  1195. unsigned char alloc_buf[MEMORY_HEAP_SIZE];
  1196. #endif
  1197. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1198. unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
  1199. unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
  1200. size_t cid_len = 0;
  1201. size_t cid_renego_len = 0;
  1202. #endif
  1203. #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
  1204. unsigned char *context_buf = NULL;
  1205. size_t context_buf_len = 0;
  1206. #endif
  1207. int i;
  1208. char *p, *q;
  1209. const int *list;
  1210. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1211. psa_status_t status;
  1212. #endif
  1213. #if defined(MBEDTLS_SSL_EXPORT_KEYS)
  1214. unsigned char eap_tls_keymaterial[16];
  1215. unsigned char eap_tls_iv[8];
  1216. const char* eap_tls_label = "client EAP encryption";
  1217. eap_tls_keys eap_tls_keying;
  1218. #if defined( MBEDTLS_SSL_DTLS_SRTP )
  1219. /*! master keys and master salt for SRTP generated during handshake */
  1220. unsigned char dtls_srtp_key_material[MBEDTLS_TLS_SRTP_MAX_KEY_MATERIAL_LENGTH];
  1221. const char* dtls_srtp_label = "EXTRACTOR-dtls_srtp";
  1222. dtls_srtp_keys dtls_srtp_keying;
  1223. const mbedtls_ssl_srtp_profile default_profiles[] = {
  1224. MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80,
  1225. MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_32,
  1226. MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_80,
  1227. MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_32,
  1228. MBEDTLS_TLS_SRTP_UNSET
  1229. };
  1230. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  1231. #endif /* MBEDTLS_SSL_EXPORT_KEYS */
  1232. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  1233. mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
  1234. #if defined(MBEDTLS_MEMORY_DEBUG)
  1235. size_t current_heap_memory, peak_heap_memory, heap_blocks;
  1236. #endif /* MBEDTLS_MEMORY_DEBUG */
  1237. #endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */
  1238. #if defined(MBEDTLS_TEST_HOOKS)
  1239. test_hooks_init( );
  1240. #endif /* MBEDTLS_TEST_HOOKS */
  1241. /*
  1242. * Make sure memory references are valid in case we exit early.
  1243. */
  1244. mbedtls_net_init( &client_fd );
  1245. mbedtls_net_init( &listen_fd );
  1246. mbedtls_ssl_init( &ssl );
  1247. mbedtls_ssl_config_init( &conf );
  1248. rng_init( &rng );
  1249. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1250. mbedtls_x509_crt_init( &cacert );
  1251. mbedtls_x509_crt_init( &srvcert );
  1252. mbedtls_pk_init( &pkey );
  1253. mbedtls_x509_crt_init( &srvcert2 );
  1254. mbedtls_pk_init( &pkey2 );
  1255. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  1256. memset( &ssl_async_keys, 0, sizeof( ssl_async_keys ) );
  1257. #endif
  1258. #endif
  1259. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
  1260. mbedtls_dhm_init( &dhm );
  1261. #endif
  1262. #if defined(MBEDTLS_SSL_CACHE_C)
  1263. mbedtls_ssl_cache_init( &cache );
  1264. #endif
  1265. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1266. mbedtls_ssl_ticket_init( &ticket_ctx );
  1267. #endif
  1268. #if defined(MBEDTLS_SSL_ALPN)
  1269. memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
  1270. #endif
  1271. #if defined(MBEDTLS_SSL_COOKIE_C)
  1272. mbedtls_ssl_cookie_init( &cookie_ctx );
  1273. #endif
  1274. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1275. status = psa_crypto_init();
  1276. if( status != PSA_SUCCESS )
  1277. {
  1278. mbedtls_fprintf( stderr, "Failed to initialize PSA Crypto implementation: %d\n",
  1279. (int) status );
  1280. ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
  1281. goto exit;
  1282. }
  1283. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1284. #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
  1285. mbedtls_test_enable_insecure_external_rng( );
  1286. #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  1287. #if !defined(_WIN32)
  1288. /* Abort cleanly on SIGTERM and SIGINT */
  1289. signal( SIGTERM, term_handler );
  1290. signal( SIGINT, term_handler );
  1291. #endif
  1292. if( argc == 0 )
  1293. {
  1294. usage:
  1295. if( ret == 0 )
  1296. ret = 1;
  1297. mbedtls_printf( USAGE1 );
  1298. mbedtls_printf( USAGE2 );
  1299. mbedtls_printf( USAGE3 );
  1300. mbedtls_printf( USAGE4 );
  1301. list = mbedtls_ssl_list_ciphersuites();
  1302. while( *list )
  1303. {
  1304. mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
  1305. list++;
  1306. if( !*list )
  1307. break;
  1308. mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
  1309. list++;
  1310. }
  1311. mbedtls_printf("\n");
  1312. goto exit;
  1313. }
  1314. opt.buffer_size = DFL_IO_BUF_LEN;
  1315. opt.server_addr = DFL_SERVER_ADDR;
  1316. opt.server_port = DFL_SERVER_PORT;
  1317. opt.debug_level = DFL_DEBUG_LEVEL;
  1318. opt.event = DFL_EVENT;
  1319. opt.response_size = DFL_RESPONSE_SIZE;
  1320. opt.nbio = DFL_NBIO;
  1321. opt.cid_enabled = DFL_CID_ENABLED;
  1322. opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
  1323. opt.cid_val = DFL_CID_VALUE;
  1324. opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
  1325. opt.read_timeout = DFL_READ_TIMEOUT;
  1326. opt.ca_file = DFL_CA_FILE;
  1327. opt.ca_path = DFL_CA_PATH;
  1328. opt.crt_file = DFL_CRT_FILE;
  1329. opt.key_file = DFL_KEY_FILE;
  1330. opt.key_pwd = DFL_KEY_PWD;
  1331. opt.crt_file2 = DFL_CRT_FILE2;
  1332. opt.key_file2 = DFL_KEY_FILE2;
  1333. opt.key_pwd2 = DFL_KEY_PWD2;
  1334. opt.async_operations = DFL_ASYNC_OPERATIONS;
  1335. opt.async_private_delay1 = DFL_ASYNC_PRIVATE_DELAY1;
  1336. opt.async_private_delay2 = DFL_ASYNC_PRIVATE_DELAY2;
  1337. opt.async_private_error = DFL_ASYNC_PRIVATE_ERROR;
  1338. opt.psk = DFL_PSK;
  1339. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1340. opt.psk_opaque = DFL_PSK_OPAQUE;
  1341. opt.psk_list_opaque = DFL_PSK_LIST_OPAQUE;
  1342. #endif
  1343. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  1344. opt.ca_callback = DFL_CA_CALLBACK;
  1345. #endif
  1346. opt.psk_identity = DFL_PSK_IDENTITY;
  1347. opt.psk_list = DFL_PSK_LIST;
  1348. opt.ecjpake_pw = DFL_ECJPAKE_PW;
  1349. opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
  1350. opt.version_suites = DFL_VERSION_SUITES;
  1351. opt.renegotiation = DFL_RENEGOTIATION;
  1352. opt.allow_legacy = DFL_ALLOW_LEGACY;
  1353. opt.renegotiate = DFL_RENEGOTIATE;
  1354. opt.renego_delay = DFL_RENEGO_DELAY;
  1355. opt.renego_period = DFL_RENEGO_PERIOD;
  1356. opt.exchanges = DFL_EXCHANGES;
  1357. opt.min_version = DFL_MIN_VERSION;
  1358. opt.max_version = DFL_MAX_VERSION;
  1359. opt.arc4 = DFL_ARC4;
  1360. opt.allow_sha1 = DFL_SHA1;
  1361. opt.auth_mode = DFL_AUTH_MODE;
  1362. opt.cert_req_ca_list = DFL_CERT_REQ_CA_LIST;
  1363. opt.mfl_code = DFL_MFL_CODE;
  1364. opt.trunc_hmac = DFL_TRUNC_HMAC;
  1365. opt.tickets = DFL_TICKETS;
  1366. opt.ticket_timeout = DFL_TICKET_TIMEOUT;
  1367. opt.cache_max = DFL_CACHE_MAX;
  1368. opt.cache_timeout = DFL_CACHE_TIMEOUT;
  1369. opt.sni = DFL_SNI;
  1370. opt.alpn_string = DFL_ALPN_STRING;
  1371. opt.curves = DFL_CURVES;
  1372. opt.dhm_file = DFL_DHM_FILE;
  1373. opt.transport = DFL_TRANSPORT;
  1374. opt.cookies = DFL_COOKIES;
  1375. opt.anti_replay = DFL_ANTI_REPLAY;
  1376. opt.hs_to_min = DFL_HS_TO_MIN;
  1377. opt.hs_to_max = DFL_HS_TO_MAX;
  1378. opt.dtls_mtu = DFL_DTLS_MTU;
  1379. opt.dgram_packing = DFL_DGRAM_PACKING;
  1380. opt.badmac_limit = DFL_BADMAC_LIMIT;
  1381. opt.extended_ms = DFL_EXTENDED_MS;
  1382. opt.etm = DFL_ETM;
  1383. opt.serialize = DFL_SERIALIZE;
  1384. opt.context_file = DFL_CONTEXT_FILE;
  1385. opt.eap_tls = DFL_EAP_TLS;
  1386. opt.reproducible = DFL_REPRODUCIBLE;
  1387. opt.nss_keylog = DFL_NSS_KEYLOG;
  1388. opt.nss_keylog_file = DFL_NSS_KEYLOG_FILE;
  1389. opt.query_config_mode = DFL_QUERY_CONFIG_MODE;
  1390. opt.use_srtp = DFL_USE_SRTP;
  1391. opt.force_srtp_profile = DFL_SRTP_FORCE_PROFILE;
  1392. opt.support_mki = DFL_SRTP_SUPPORT_MKI;
  1393. for( i = 1; i < argc; i++ )
  1394. {
  1395. p = argv[i];
  1396. if( ( q = strchr( p, '=' ) ) == NULL )
  1397. goto usage;
  1398. *q++ = '\0';
  1399. if( strcmp( p, "server_port" ) == 0 )
  1400. opt.server_port = q;
  1401. else if( strcmp( p, "server_addr" ) == 0 )
  1402. opt.server_addr = q;
  1403. else if( strcmp( p, "dtls" ) == 0 )
  1404. {
  1405. int t = atoi( q );
  1406. if( t == 0 )
  1407. opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
  1408. else if( t == 1 )
  1409. opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
  1410. else
  1411. goto usage;
  1412. }
  1413. else if( strcmp( p, "debug_level" ) == 0 )
  1414. {
  1415. opt.debug_level = atoi( q );
  1416. if( opt.debug_level < 0 || opt.debug_level > 65535 )
  1417. goto usage;
  1418. }
  1419. else if( strcmp( p, "nbio" ) == 0 )
  1420. {
  1421. opt.nbio = atoi( q );
  1422. if( opt.nbio < 0 || opt.nbio > 2 )
  1423. goto usage;
  1424. }
  1425. else if( strcmp( p, "event" ) == 0 )
  1426. {
  1427. opt.event = atoi( q );
  1428. if( opt.event < 0 || opt.event > 2 )
  1429. goto usage;
  1430. }
  1431. else if( strcmp( p, "read_timeout" ) == 0 )
  1432. opt.read_timeout = atoi( q );
  1433. else if( strcmp( p, "buffer_size" ) == 0 )
  1434. {
  1435. opt.buffer_size = atoi( q );
  1436. if( opt.buffer_size < 1 || opt.buffer_size > MBEDTLS_SSL_MAX_CONTENT_LEN + 1 )
  1437. goto usage;
  1438. }
  1439. else if( strcmp( p, "response_size" ) == 0 )
  1440. {
  1441. opt.response_size = atoi( q );
  1442. if( opt.response_size < 0 || opt.response_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
  1443. goto usage;
  1444. if( opt.buffer_size < opt.response_size )
  1445. opt.buffer_size = opt.response_size;
  1446. }
  1447. else if( strcmp( p, "ca_file" ) == 0 )
  1448. opt.ca_file = q;
  1449. else if( strcmp( p, "ca_path" ) == 0 )
  1450. opt.ca_path = q;
  1451. else if( strcmp( p, "crt_file" ) == 0 )
  1452. opt.crt_file = q;
  1453. else if( strcmp( p, "key_file" ) == 0 )
  1454. opt.key_file = q;
  1455. else if( strcmp( p, "key_pwd" ) == 0 )
  1456. opt.key_pwd = q;
  1457. else if( strcmp( p, "crt_file2" ) == 0 )
  1458. opt.crt_file2 = q;
  1459. else if( strcmp( p, "key_file2" ) == 0 )
  1460. opt.key_file2 = q;
  1461. else if( strcmp( p, "key_pwd2" ) == 0 )
  1462. opt.key_pwd2 = q;
  1463. else if( strcmp( p, "dhm_file" ) == 0 )
  1464. opt.dhm_file = q;
  1465. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  1466. else if( strcmp( p, "async_operations" ) == 0 )
  1467. opt.async_operations = q;
  1468. else if( strcmp( p, "async_private_delay1" ) == 0 )
  1469. opt.async_private_delay1 = atoi( q );
  1470. else if( strcmp( p, "async_private_delay2" ) == 0 )
  1471. opt.async_private_delay2 = atoi( q );
  1472. else if( strcmp( p, "async_private_error" ) == 0 )
  1473. {
  1474. int n = atoi( q );
  1475. if( n < -SSL_ASYNC_INJECT_ERROR_MAX ||
  1476. n > SSL_ASYNC_INJECT_ERROR_MAX )
  1477. {
  1478. ret = 2;
  1479. goto usage;
  1480. }
  1481. opt.async_private_error = n;
  1482. }
  1483. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  1484. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1485. else if( strcmp( p, "cid" ) == 0 )
  1486. {
  1487. opt.cid_enabled = atoi( q );
  1488. if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
  1489. goto usage;
  1490. }
  1491. else if( strcmp( p, "cid_renego" ) == 0 )
  1492. {
  1493. opt.cid_enabled_renego = atoi( q );
  1494. if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
  1495. goto usage;
  1496. }
  1497. else if( strcmp( p, "cid_val" ) == 0 )
  1498. {
  1499. opt.cid_val = q;
  1500. }
  1501. else if( strcmp( p, "cid_val_renego" ) == 0 )
  1502. {
  1503. opt.cid_val_renego = q;
  1504. }
  1505. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  1506. else if( strcmp( p, "psk" ) == 0 )
  1507. opt.psk = q;
  1508. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1509. else if( strcmp( p, "psk_opaque" ) == 0 )
  1510. opt.psk_opaque = atoi( q );
  1511. else if( strcmp( p, "psk_list_opaque" ) == 0 )
  1512. opt.psk_list_opaque = atoi( q );
  1513. #endif
  1514. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  1515. else if( strcmp( p, "ca_callback" ) == 0)
  1516. opt.ca_callback = atoi( q );
  1517. #endif
  1518. else if( strcmp( p, "psk_identity" ) == 0 )
  1519. opt.psk_identity = q;
  1520. else if( strcmp( p, "psk_list" ) == 0 )
  1521. opt.psk_list = q;
  1522. else if( strcmp( p, "ecjpake_pw" ) == 0 )
  1523. opt.ecjpake_pw = q;
  1524. else if( strcmp( p, "force_ciphersuite" ) == 0 )
  1525. {
  1526. opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
  1527. if( opt.force_ciphersuite[0] == 0 )
  1528. {
  1529. ret = 2;
  1530. goto usage;
  1531. }
  1532. opt.force_ciphersuite[1] = 0;
  1533. }
  1534. else if( strcmp( p, "curves" ) == 0 )
  1535. opt.curves = q;
  1536. else if( strcmp( p, "version_suites" ) == 0 )
  1537. opt.version_suites = q;
  1538. else if( strcmp( p, "renegotiation" ) == 0 )
  1539. {
  1540. opt.renegotiation = (atoi( q )) ?
  1541. MBEDTLS_SSL_RENEGOTIATION_ENABLED :
  1542. MBEDTLS_SSL_RENEGOTIATION_DISABLED;
  1543. }
  1544. else if( strcmp( p, "allow_legacy" ) == 0 )
  1545. {
  1546. switch( atoi( q ) )
  1547. {
  1548. case -1:
  1549. opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
  1550. break;
  1551. case 0:
  1552. opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
  1553. break;
  1554. case 1:
  1555. opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
  1556. break;
  1557. default: goto usage;
  1558. }
  1559. }
  1560. else if( strcmp( p, "renegotiate" ) == 0 )
  1561. {
  1562. opt.renegotiate = atoi( q );
  1563. if( opt.renegotiate < 0 || opt.renegotiate > 1 )
  1564. goto usage;
  1565. }
  1566. else if( strcmp( p, "renego_delay" ) == 0 )
  1567. {
  1568. opt.renego_delay = atoi( q );
  1569. }
  1570. else if( strcmp( p, "renego_period" ) == 0 )
  1571. {
  1572. #if defined(_MSC_VER)
  1573. opt.renego_period = _strtoui64( q, NULL, 10 );
  1574. #else
  1575. if( sscanf( q, "%" SCNu64, &opt.renego_period ) != 1 )
  1576. goto usage;
  1577. #endif /* _MSC_VER */
  1578. if( opt.renego_period < 2 )
  1579. goto usage;
  1580. }
  1581. else if( strcmp( p, "exchanges" ) == 0 )
  1582. {
  1583. opt.exchanges = atoi( q );
  1584. if( opt.exchanges < 0 )
  1585. goto usage;
  1586. }
  1587. else if( strcmp( p, "min_version" ) == 0 )
  1588. {
  1589. if( strcmp( q, "ssl3" ) == 0 )
  1590. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
  1591. else if( strcmp( q, "tls1" ) == 0 )
  1592. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
  1593. else if( strcmp( q, "tls1_1" ) == 0 ||
  1594. strcmp( q, "dtls1" ) == 0 )
  1595. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
  1596. else if( strcmp( q, "tls12" ) == 0 ||
  1597. strcmp( q, "dtls12" ) == 0 )
  1598. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1599. else
  1600. goto usage;
  1601. }
  1602. else if( strcmp( p, "max_version" ) == 0 )
  1603. {
  1604. if( strcmp( q, "ssl3" ) == 0 )
  1605. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
  1606. else if( strcmp( q, "tls1" ) == 0 )
  1607. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
  1608. else if( strcmp( q, "tls1_1" ) == 0 ||
  1609. strcmp( q, "dtls1" ) == 0 )
  1610. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
  1611. else if( strcmp( q, "tls12" ) == 0 ||
  1612. strcmp( q, "dtls12" ) == 0 )
  1613. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1614. else
  1615. goto usage;
  1616. }
  1617. else if( strcmp( p, "arc4" ) == 0 )
  1618. {
  1619. switch( atoi( q ) )
  1620. {
  1621. case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
  1622. case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
  1623. default: goto usage;
  1624. }
  1625. }
  1626. else if( strcmp( p, "allow_sha1" ) == 0 )
  1627. {
  1628. switch( atoi( q ) )
  1629. {
  1630. case 0: opt.allow_sha1 = 0; break;
  1631. case 1: opt.allow_sha1 = 1; break;
  1632. default: goto usage;
  1633. }
  1634. }
  1635. else if( strcmp( p, "force_version" ) == 0 )
  1636. {
  1637. if( strcmp( q, "ssl3" ) == 0 )
  1638. {
  1639. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
  1640. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
  1641. }
  1642. else if( strcmp( q, "tls1" ) == 0 )
  1643. {
  1644. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
  1645. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
  1646. }
  1647. else if( strcmp( q, "tls1_1" ) == 0 )
  1648. {
  1649. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
  1650. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
  1651. }
  1652. else if( strcmp( q, "tls12" ) == 0 )
  1653. {
  1654. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1655. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1656. }
  1657. else if( strcmp( q, "dtls1" ) == 0 )
  1658. {
  1659. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
  1660. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
  1661. opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
  1662. }
  1663. else if( strcmp( q, "dtls12" ) == 0 )
  1664. {
  1665. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1666. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1667. opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
  1668. }
  1669. else
  1670. goto usage;
  1671. }
  1672. else if( strcmp( p, "auth_mode" ) == 0 )
  1673. {
  1674. if( ( opt.auth_mode = get_auth_mode( q ) ) < 0 )
  1675. goto usage;
  1676. }
  1677. else if( strcmp( p, "cert_req_ca_list" ) == 0 )
  1678. {
  1679. opt.cert_req_ca_list = atoi( q );
  1680. if( opt.cert_req_ca_list < 0 || opt.cert_req_ca_list > 1 )
  1681. goto usage;
  1682. }
  1683. else if( strcmp( p, "max_frag_len" ) == 0 )
  1684. {
  1685. if( strcmp( q, "512" ) == 0 )
  1686. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
  1687. else if( strcmp( q, "1024" ) == 0 )
  1688. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
  1689. else if( strcmp( q, "2048" ) == 0 )
  1690. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
  1691. else if( strcmp( q, "4096" ) == 0 )
  1692. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
  1693. else
  1694. goto usage;
  1695. }
  1696. else if( strcmp( p, "alpn" ) == 0 )
  1697. {
  1698. opt.alpn_string = q;
  1699. }
  1700. else if( strcmp( p, "trunc_hmac" ) == 0 )
  1701. {
  1702. switch( atoi( q ) )
  1703. {
  1704. case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
  1705. case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
  1706. default: goto usage;
  1707. }
  1708. }
  1709. else if( strcmp( p, "extended_ms" ) == 0 )
  1710. {
  1711. switch( atoi( q ) )
  1712. {
  1713. case 0:
  1714. opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
  1715. break;
  1716. case 1:
  1717. opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
  1718. break;
  1719. default: goto usage;
  1720. }
  1721. }
  1722. else if( strcmp( p, "etm" ) == 0 )
  1723. {
  1724. switch( atoi( q ) )
  1725. {
  1726. case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
  1727. case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
  1728. default: goto usage;
  1729. }
  1730. }
  1731. else if( strcmp( p, "tickets" ) == 0 )
  1732. {
  1733. opt.tickets = atoi( q );
  1734. if( opt.tickets < 0 || opt.tickets > 1 )
  1735. goto usage;
  1736. }
  1737. else if( strcmp( p, "ticket_timeout" ) == 0 )
  1738. {
  1739. opt.ticket_timeout = atoi( q );
  1740. if( opt.ticket_timeout < 0 )
  1741. goto usage;
  1742. }
  1743. else if( strcmp( p, "cache_max" ) == 0 )
  1744. {
  1745. opt.cache_max = atoi( q );
  1746. if( opt.cache_max < 0 )
  1747. goto usage;
  1748. }
  1749. else if( strcmp( p, "cache_timeout" ) == 0 )
  1750. {
  1751. opt.cache_timeout = atoi( q );
  1752. if( opt.cache_timeout < 0 )
  1753. goto usage;
  1754. }
  1755. else if( strcmp( p, "cookies" ) == 0 )
  1756. {
  1757. opt.cookies = atoi( q );
  1758. if( opt.cookies < -1 || opt.cookies > 1)
  1759. goto usage;
  1760. }
  1761. else if( strcmp( p, "anti_replay" ) == 0 )
  1762. {
  1763. opt.anti_replay = atoi( q );
  1764. if( opt.anti_replay < 0 || opt.anti_replay > 1)
  1765. goto usage;
  1766. }
  1767. else if( strcmp( p, "badmac_limit" ) == 0 )
  1768. {
  1769. opt.badmac_limit = atoi( q );
  1770. if( opt.badmac_limit < 0 )
  1771. goto usage;
  1772. }
  1773. else if( strcmp( p, "hs_timeout" ) == 0 )
  1774. {
  1775. if( ( p = strchr( q, '-' ) ) == NULL )
  1776. goto usage;
  1777. *p++ = '\0';
  1778. opt.hs_to_min = atoi( q );
  1779. opt.hs_to_max = atoi( p );
  1780. if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
  1781. goto usage;
  1782. }
  1783. else if( strcmp( p, "mtu" ) == 0 )
  1784. {
  1785. opt.dtls_mtu = atoi( q );
  1786. if( opt.dtls_mtu < 0 )
  1787. goto usage;
  1788. }
  1789. else if( strcmp( p, "dgram_packing" ) == 0 )
  1790. {
  1791. opt.dgram_packing = atoi( q );
  1792. if( opt.dgram_packing != 0 &&
  1793. opt.dgram_packing != 1 )
  1794. {
  1795. goto usage;
  1796. }
  1797. }
  1798. else if( strcmp( p, "sni" ) == 0 )
  1799. {
  1800. opt.sni = q;
  1801. }
  1802. else if( strcmp( p, "query_config" ) == 0 )
  1803. {
  1804. opt.query_config_mode = 1;
  1805. query_config_ret = query_config( q );
  1806. goto exit;
  1807. }
  1808. else if( strcmp( p, "serialize") == 0 )
  1809. {
  1810. opt.serialize = atoi( q );
  1811. if( opt.serialize < 0 || opt.serialize > 2)
  1812. goto usage;
  1813. }
  1814. else if( strcmp( p, "context_file") == 0 )
  1815. {
  1816. opt.context_file = q;
  1817. }
  1818. else if( strcmp( p, "eap_tls" ) == 0 )
  1819. {
  1820. opt.eap_tls = atoi( q );
  1821. if( opt.eap_tls < 0 || opt.eap_tls > 1 )
  1822. goto usage;
  1823. }
  1824. else if( strcmp( p, "reproducible" ) == 0 )
  1825. {
  1826. opt.reproducible = 1;
  1827. }
  1828. else if( strcmp( p, "nss_keylog" ) == 0 )
  1829. {
  1830. opt.nss_keylog = atoi( q );
  1831. if( opt.nss_keylog < 0 || opt.nss_keylog > 1 )
  1832. goto usage;
  1833. }
  1834. else if( strcmp( p, "nss_keylog_file" ) == 0 )
  1835. {
  1836. opt.nss_keylog_file = q;
  1837. }
  1838. else if( strcmp( p, "use_srtp" ) == 0 )
  1839. {
  1840. opt.use_srtp = atoi ( q );
  1841. }
  1842. else if( strcmp( p, "srtp_force_profile" ) == 0 )
  1843. {
  1844. opt.force_srtp_profile = atoi( q );
  1845. }
  1846. else if( strcmp( p, "support_mki" ) == 0 )
  1847. {
  1848. opt.support_mki = atoi( q );
  1849. }
  1850. else
  1851. goto usage;
  1852. }
  1853. if( opt.nss_keylog != 0 && opt.eap_tls != 0 )
  1854. {
  1855. mbedtls_printf( "Error: eap_tls and nss_keylog options cannot be used together.\n" );
  1856. goto usage;
  1857. }
  1858. /* Event-driven IO is incompatible with the above custom
  1859. * receive and send functions, as the polling builds on
  1860. * refers to the underlying net_context. */
  1861. if( opt.event == 1 && opt.nbio != 1 )
  1862. {
  1863. mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
  1864. opt.nbio = 1;
  1865. }
  1866. #if defined(MBEDTLS_DEBUG_C)
  1867. mbedtls_debug_set_threshold( opt.debug_level );
  1868. #endif
  1869. buf = mbedtls_calloc( 1, opt.buffer_size + 1 );
  1870. if( buf == NULL )
  1871. {
  1872. mbedtls_printf( "Could not allocate %u bytes\n", opt.buffer_size );
  1873. ret = 3;
  1874. goto exit;
  1875. }
  1876. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1877. if( opt.psk_opaque != 0 )
  1878. {
  1879. if( strlen( opt.psk ) == 0 )
  1880. {
  1881. mbedtls_printf( "psk_opaque set but no psk to be imported specified.\n" );
  1882. ret = 2;
  1883. goto usage;
  1884. }
  1885. if( opt.force_ciphersuite[0] <= 0 )
  1886. {
  1887. mbedtls_printf( "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n" );
  1888. ret = 2;
  1889. goto usage;
  1890. }
  1891. }
  1892. if( opt.psk_list_opaque != 0 )
  1893. {
  1894. if( opt.psk_list == NULL )
  1895. {
  1896. mbedtls_printf( "psk_slot set but no psk to be imported specified.\n" );
  1897. ret = 2;
  1898. goto usage;
  1899. }
  1900. if( opt.force_ciphersuite[0] <= 0 )
  1901. {
  1902. mbedtls_printf( "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n" );
  1903. ret = 2;
  1904. goto usage;
  1905. }
  1906. }
  1907. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1908. if( opt.force_ciphersuite[0] > 0 )
  1909. {
  1910. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  1911. ciphersuite_info =
  1912. mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
  1913. if( opt.max_version != -1 &&
  1914. ciphersuite_info->min_minor_ver > opt.max_version )
  1915. {
  1916. mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
  1917. ret = 2;
  1918. goto usage;
  1919. }
  1920. if( opt.min_version != -1 &&
  1921. ciphersuite_info->max_minor_ver < opt.min_version )
  1922. {
  1923. mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
  1924. ret = 2;
  1925. goto usage;
  1926. }
  1927. /* If we select a version that's not supported by
  1928. * this suite, then there will be no common ciphersuite... */
  1929. if( opt.max_version == -1 ||
  1930. opt.max_version > ciphersuite_info->max_minor_ver )
  1931. {
  1932. opt.max_version = ciphersuite_info->max_minor_ver;
  1933. }
  1934. if( opt.min_version < ciphersuite_info->min_minor_ver )
  1935. {
  1936. opt.min_version = ciphersuite_info->min_minor_ver;
  1937. /* DTLS starts with TLS 1.1 */
  1938. if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  1939. opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
  1940. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
  1941. }
  1942. /* Enable RC4 if needed and not explicitly disabled */
  1943. if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
  1944. {
  1945. if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
  1946. {
  1947. mbedtls_printf("forced RC4 ciphersuite with RC4 disabled\n");
  1948. ret = 2;
  1949. goto usage;
  1950. }
  1951. opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
  1952. }
  1953. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1954. if( opt.psk_opaque != 0 || opt.psk_list_opaque != 0 )
  1955. {
  1956. /* Ensure that the chosen ciphersuite is PSK-only; we must know
  1957. * the ciphersuite in advance to set the correct policy for the
  1958. * PSK key slot. This limitation might go away in the future. */
  1959. if( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK ||
  1960. opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 )
  1961. {
  1962. mbedtls_printf( "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n" );
  1963. ret = 2;
  1964. goto usage;
  1965. }
  1966. /* Determine KDF algorithm the opaque PSK will be used in. */
  1967. #if defined(MBEDTLS_SHA512_C)
  1968. if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
  1969. alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
  1970. else
  1971. #endif /* MBEDTLS_SHA512_C */
  1972. alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
  1973. }
  1974. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1975. }
  1976. if( opt.version_suites != NULL )
  1977. {
  1978. const char *name[4] = { 0 };
  1979. /* Parse 4-element coma-separated list */
  1980. for( i = 0, p = (char *) opt.version_suites;
  1981. i < 4 && *p != '\0';
  1982. i++ )
  1983. {
  1984. name[i] = p;
  1985. /* Terminate the current string and move on to next one */
  1986. while( *p != ',' && *p != '\0' )
  1987. p++;
  1988. if( *p == ',' )
  1989. *p++ = '\0';
  1990. }
  1991. if( i != 4 )
  1992. {
  1993. mbedtls_printf( "too few values for version_suites\n" );
  1994. ret = 1;
  1995. goto exit;
  1996. }
  1997. memset( version_suites, 0, sizeof( version_suites ) );
  1998. /* Get the suites identifiers from their name */
  1999. for( i = 0; i < 4; i++ )
  2000. {
  2001. version_suites[i][0] = mbedtls_ssl_get_ciphersuite_id( name[i] );
  2002. if( version_suites[i][0] == 0 )
  2003. {
  2004. mbedtls_printf( "unknown ciphersuite: '%s'\n", name[i] );
  2005. ret = 2;
  2006. goto usage;
  2007. }
  2008. }
  2009. }
  2010. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  2011. if( mbedtls_test_unhexify( cid, sizeof( cid ),
  2012. opt.cid_val, &cid_len ) != 0 )
  2013. {
  2014. mbedtls_printf( "CID not valid hex\n" );
  2015. goto exit;
  2016. }
  2017. /* Keep CID settings for renegotiation unless
  2018. * specified otherwise. */
  2019. if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
  2020. opt.cid_enabled_renego = opt.cid_enabled;
  2021. if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
  2022. opt.cid_val_renego = opt.cid_val;
  2023. if( mbedtls_test_unhexify( cid_renego, sizeof( cid_renego ),
  2024. opt.cid_val_renego, &cid_renego_len ) != 0 )
  2025. {
  2026. mbedtls_printf( "CID not valid hex\n" );
  2027. goto exit;
  2028. }
  2029. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  2030. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  2031. /*
  2032. * Unhexify the pre-shared key and parse the list if any given
  2033. */
  2034. if( mbedtls_test_unhexify( psk, sizeof( psk ),
  2035. opt.psk, &psk_len ) != 0 )
  2036. {
  2037. mbedtls_printf( "pre-shared key not valid hex\n" );
  2038. goto exit;
  2039. }
  2040. if( opt.psk_list != NULL )
  2041. {
  2042. if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
  2043. {
  2044. mbedtls_printf( "psk_list invalid" );
  2045. goto exit;
  2046. }
  2047. }
  2048. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  2049. #if defined(MBEDTLS_ECP_C)
  2050. if( opt.curves != NULL )
  2051. {
  2052. p = (char *) opt.curves;
  2053. i = 0;
  2054. if( strcmp( p, "none" ) == 0 )
  2055. {
  2056. curve_list[0] = MBEDTLS_ECP_DP_NONE;
  2057. }
  2058. else if( strcmp( p, "default" ) != 0 )
  2059. {
  2060. /* Leave room for a final NULL in curve list */
  2061. while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
  2062. {
  2063. q = p;
  2064. /* Terminate the current string */
  2065. while( *p != ',' && *p != '\0' )
  2066. p++;
  2067. if( *p == ',' )
  2068. *p++ = '\0';
  2069. if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
  2070. {
  2071. curve_list[i++] = curve_cur->grp_id;
  2072. }
  2073. else
  2074. {
  2075. mbedtls_printf( "unknown curve %s\n", q );
  2076. mbedtls_printf( "supported curves: " );
  2077. for( curve_cur = mbedtls_ecp_curve_list();
  2078. curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
  2079. curve_cur++ )
  2080. {
  2081. mbedtls_printf( "%s ", curve_cur->name );
  2082. }
  2083. mbedtls_printf( "\n" );
  2084. goto exit;
  2085. }
  2086. }
  2087. mbedtls_printf("Number of curves: %d\n", i );
  2088. if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
  2089. {
  2090. mbedtls_printf( "curves list too long, maximum %d",
  2091. CURVE_LIST_SIZE - 1 );
  2092. goto exit;
  2093. }
  2094. curve_list[i] = MBEDTLS_ECP_DP_NONE;
  2095. }
  2096. }
  2097. #endif /* MBEDTLS_ECP_C */
  2098. #if defined(MBEDTLS_SSL_ALPN)
  2099. if( opt.alpn_string != NULL )
  2100. {
  2101. p = (char *) opt.alpn_string;
  2102. i = 0;
  2103. /* Leave room for a final NULL in alpn_list */
  2104. while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
  2105. {
  2106. alpn_list[i++] = p;
  2107. /* Terminate the current string and move on to next one */
  2108. while( *p != ',' && *p != '\0' )
  2109. p++;
  2110. if( *p == ',' )
  2111. *p++ = '\0';
  2112. }
  2113. }
  2114. #endif /* MBEDTLS_SSL_ALPN */
  2115. /*
  2116. * 0. Initialize the RNG and the session data
  2117. */
  2118. mbedtls_printf( "\n . Seeding the random number generator..." );
  2119. fflush( stdout );
  2120. ret = rng_seed( &rng, opt.reproducible, pers );
  2121. if( ret != 0 )
  2122. goto exit;
  2123. mbedtls_printf( " ok\n" );
  2124. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2125. /*
  2126. * 1.1. Load the trusted CA
  2127. */
  2128. mbedtls_printf( " . Loading the CA root certificate ..." );
  2129. fflush( stdout );
  2130. if( strcmp( opt.ca_path, "none" ) == 0 ||
  2131. strcmp( opt.ca_file, "none" ) == 0 )
  2132. {
  2133. ret = 0;
  2134. }
  2135. else
  2136. #if defined(MBEDTLS_FS_IO)
  2137. if( strlen( opt.ca_path ) )
  2138. ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
  2139. else if( strlen( opt.ca_file ) )
  2140. ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
  2141. else
  2142. #endif
  2143. #if defined(MBEDTLS_CERTS_C)
  2144. {
  2145. #if defined(MBEDTLS_PEM_PARSE_C)
  2146. for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
  2147. {
  2148. ret = mbedtls_x509_crt_parse( &cacert,
  2149. (const unsigned char *) mbedtls_test_cas[i],
  2150. mbedtls_test_cas_len[i] );
  2151. if( ret != 0 )
  2152. break;
  2153. }
  2154. if( ret == 0 )
  2155. #endif /* MBEDTLS_PEM_PARSE_C */
  2156. for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
  2157. {
  2158. ret = mbedtls_x509_crt_parse_der( &cacert,
  2159. (const unsigned char *) mbedtls_test_cas_der[i],
  2160. mbedtls_test_cas_der_len[i] );
  2161. if( ret != 0 )
  2162. break;
  2163. }
  2164. }
  2165. #else
  2166. {
  2167. ret = 1;
  2168. mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
  2169. }
  2170. #endif /* MBEDTLS_CERTS_C */
  2171. if( ret < 0 )
  2172. {
  2173. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", (unsigned int) -ret );
  2174. goto exit;
  2175. }
  2176. mbedtls_printf( " ok (%d skipped)\n", ret );
  2177. /*
  2178. * 1.2. Load own certificate and private key
  2179. */
  2180. mbedtls_printf( " . Loading the server cert. and key..." );
  2181. fflush( stdout );
  2182. #if defined(MBEDTLS_FS_IO)
  2183. if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
  2184. {
  2185. key_cert_init++;
  2186. if( ( ret = mbedtls_x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
  2187. {
  2188. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
  2189. (unsigned int) -ret );
  2190. goto exit;
  2191. }
  2192. }
  2193. if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
  2194. {
  2195. key_cert_init++;
  2196. if( ( ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file,
  2197. opt.key_pwd ) ) != 0 )
  2198. {
  2199. mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n", (unsigned int) -ret );
  2200. goto exit;
  2201. }
  2202. }
  2203. if( key_cert_init == 1 )
  2204. {
  2205. mbedtls_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
  2206. goto exit;
  2207. }
  2208. if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
  2209. {
  2210. key_cert_init2++;
  2211. if( ( ret = mbedtls_x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
  2212. {
  2213. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n",
  2214. (unsigned int) -ret );
  2215. goto exit;
  2216. }
  2217. }
  2218. if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
  2219. {
  2220. key_cert_init2++;
  2221. if( ( ret = mbedtls_pk_parse_keyfile( &pkey2, opt.key_file2,
  2222. opt.key_pwd2 ) ) != 0 )
  2223. {
  2224. mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n",
  2225. (unsigned int) -ret );
  2226. goto exit;
  2227. }
  2228. }
  2229. if( key_cert_init2 == 1 )
  2230. {
  2231. mbedtls_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
  2232. goto exit;
  2233. }
  2234. #endif
  2235. if( key_cert_init == 0 &&
  2236. strcmp( opt.crt_file, "none" ) != 0 &&
  2237. strcmp( opt.key_file, "none" ) != 0 &&
  2238. key_cert_init2 == 0 &&
  2239. strcmp( opt.crt_file2, "none" ) != 0 &&
  2240. strcmp( opt.key_file2, "none" ) != 0 )
  2241. {
  2242. #if !defined(MBEDTLS_CERTS_C)
  2243. mbedtls_printf( "Not certificated or key provided, and \nMBEDTLS_CERTS_C not defined!\n" );
  2244. goto exit;
  2245. #else
  2246. #if defined(MBEDTLS_RSA_C)
  2247. if( ( ret = mbedtls_x509_crt_parse( &srvcert,
  2248. (const unsigned char *) mbedtls_test_srv_crt_rsa,
  2249. mbedtls_test_srv_crt_rsa_len ) ) != 0 )
  2250. {
  2251. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
  2252. (unsigned int) -ret );
  2253. goto exit;
  2254. }
  2255. if( ( ret = mbedtls_pk_parse_key( &pkey,
  2256. (const unsigned char *) mbedtls_test_srv_key_rsa,
  2257. mbedtls_test_srv_key_rsa_len, NULL, 0 ) ) != 0 )
  2258. {
  2259. mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
  2260. (unsigned int) -ret );
  2261. goto exit;
  2262. }
  2263. key_cert_init = 2;
  2264. #endif /* MBEDTLS_RSA_C */
  2265. #if defined(MBEDTLS_ECDSA_C)
  2266. if( ( ret = mbedtls_x509_crt_parse( &srvcert2,
  2267. (const unsigned char *) mbedtls_test_srv_crt_ec,
  2268. mbedtls_test_srv_crt_ec_len ) ) != 0 )
  2269. {
  2270. mbedtls_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n",
  2271. (unsigned int) -ret );
  2272. goto exit;
  2273. }
  2274. if( ( ret = mbedtls_pk_parse_key( &pkey2,
  2275. (const unsigned char *) mbedtls_test_srv_key_ec,
  2276. mbedtls_test_srv_key_ec_len, NULL, 0 ) ) != 0 )
  2277. {
  2278. mbedtls_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n",
  2279. (unsigned int) -ret );
  2280. goto exit;
  2281. }
  2282. key_cert_init2 = 2;
  2283. #endif /* MBEDTLS_ECDSA_C */
  2284. #endif /* MBEDTLS_CERTS_C */
  2285. }
  2286. mbedtls_printf( " ok\n" );
  2287. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2288. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
  2289. if( opt.dhm_file != NULL )
  2290. {
  2291. mbedtls_printf( " . Loading DHM parameters..." );
  2292. fflush( stdout );
  2293. if( ( ret = mbedtls_dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
  2294. {
  2295. mbedtls_printf( " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n",
  2296. (unsigned int) -ret );
  2297. goto exit;
  2298. }
  2299. mbedtls_printf( " ok\n" );
  2300. }
  2301. #endif
  2302. #if defined(SNI_OPTION)
  2303. if( opt.sni != NULL )
  2304. {
  2305. mbedtls_printf( " . Setting up SNI information..." );
  2306. fflush( stdout );
  2307. if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
  2308. {
  2309. mbedtls_printf( " failed\n" );
  2310. goto exit;
  2311. }
  2312. mbedtls_printf( " ok\n" );
  2313. }
  2314. #endif /* SNI_OPTION */
  2315. /*
  2316. * 2. Setup the listening TCP socket
  2317. */
  2318. mbedtls_printf( " . Bind on %s://%s:%s/ ...",
  2319. opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
  2320. opt.server_addr ? opt.server_addr : "*",
  2321. opt.server_port );
  2322. fflush( stdout );
  2323. if( ( ret = mbedtls_net_bind( &listen_fd, opt.server_addr, opt.server_port,
  2324. opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
  2325. MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
  2326. {
  2327. mbedtls_printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", (unsigned int) -ret );
  2328. goto exit;
  2329. }
  2330. mbedtls_printf( " ok\n" );
  2331. /*
  2332. * 3. Setup stuff
  2333. */
  2334. mbedtls_printf( " . Setting up the SSL/TLS structure..." );
  2335. fflush( stdout );
  2336. if( ( ret = mbedtls_ssl_config_defaults( &conf,
  2337. MBEDTLS_SSL_IS_SERVER,
  2338. opt.transport,
  2339. MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
  2340. {
  2341. mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", (unsigned int) -ret );
  2342. goto exit;
  2343. }
  2344. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2345. /* The default algorithms profile disables SHA-1, but our tests still
  2346. rely on it heavily. Hence we allow it here. A real-world server
  2347. should use the default profile unless there is a good reason not to. */
  2348. if( opt.allow_sha1 > 0 )
  2349. {
  2350. crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
  2351. mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
  2352. mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
  2353. }
  2354. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2355. if( opt.auth_mode != DFL_AUTH_MODE )
  2356. mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
  2357. if( opt.cert_req_ca_list != DFL_CERT_REQ_CA_LIST )
  2358. mbedtls_ssl_conf_cert_req_ca_list( &conf, opt.cert_req_ca_list );
  2359. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  2360. if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
  2361. mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
  2362. if( opt.dgram_packing != DFL_DGRAM_PACKING )
  2363. mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
  2364. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  2365. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  2366. if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
  2367. {
  2368. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
  2369. goto exit;
  2370. }
  2371. #endif
  2372. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  2373. if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
  2374. {
  2375. if( opt.cid_enabled == 1 &&
  2376. opt.cid_enabled_renego == 1 &&
  2377. cid_len != cid_renego_len )
  2378. {
  2379. mbedtls_printf( "CID length must not change during renegotiation\n" );
  2380. goto usage;
  2381. }
  2382. if( opt.cid_enabled == 1 )
  2383. ret = mbedtls_ssl_conf_cid( &conf, cid_len,
  2384. MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
  2385. else
  2386. ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len,
  2387. MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
  2388. if( ret != 0 )
  2389. {
  2390. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
  2391. (unsigned int) -ret );
  2392. goto exit;
  2393. }
  2394. }
  2395. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  2396. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  2397. const mbedtls_ssl_srtp_profile forced_profile[] = { opt.force_srtp_profile, MBEDTLS_TLS_SRTP_UNSET };
  2398. if( opt.use_srtp == 1 )
  2399. {
  2400. if( opt.force_srtp_profile != 0 )
  2401. {
  2402. ret = mbedtls_ssl_conf_dtls_srtp_protection_profiles( &conf, forced_profile );
  2403. }
  2404. else
  2405. {
  2406. ret = mbedtls_ssl_conf_dtls_srtp_protection_profiles( &conf, default_profiles );
  2407. }
  2408. if( ret != 0 )
  2409. {
  2410. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_dtls_srtp_protection_profiles returned %d\n\n", ret );
  2411. goto exit;
  2412. }
  2413. mbedtls_ssl_conf_srtp_mki_value_supported( &conf,
  2414. opt.support_mki ?
  2415. MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED :
  2416. MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED );
  2417. }
  2418. else if( opt.force_srtp_profile != 0 )
  2419. {
  2420. mbedtls_printf( " failed\n ! must enable use_srtp to force srtp profile\n\n" );
  2421. goto exit;
  2422. }
  2423. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  2424. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  2425. if( opt.trunc_hmac != DFL_TRUNC_HMAC )
  2426. mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
  2427. #endif
  2428. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  2429. if( opt.extended_ms != DFL_EXTENDED_MS )
  2430. mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
  2431. #endif
  2432. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  2433. if( opt.etm != DFL_ETM )
  2434. mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
  2435. #endif
  2436. #if defined(MBEDTLS_SSL_EXPORT_KEYS)
  2437. if( opt.eap_tls != 0 )
  2438. {
  2439. mbedtls_ssl_conf_export_keys_ext_cb( &conf, eap_tls_key_derivation,
  2440. &eap_tls_keying );
  2441. }
  2442. else if( opt.nss_keylog != 0 )
  2443. {
  2444. mbedtls_ssl_conf_export_keys_ext_cb( &conf,
  2445. nss_keylog_export,
  2446. NULL );
  2447. }
  2448. #if defined( MBEDTLS_SSL_DTLS_SRTP )
  2449. else if( opt.use_srtp != 0 )
  2450. {
  2451. mbedtls_ssl_conf_export_keys_ext_cb( &conf, dtls_srtp_key_derivation,
  2452. &dtls_srtp_keying );
  2453. }
  2454. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  2455. #endif /* MBEDTLS_SSL_EXPORT_KEYS */
  2456. #if defined(MBEDTLS_SSL_ALPN)
  2457. if( opt.alpn_string != NULL )
  2458. if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
  2459. {
  2460. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
  2461. goto exit;
  2462. }
  2463. #endif
  2464. if (opt.reproducible)
  2465. {
  2466. #if defined(MBEDTLS_HAVE_TIME)
  2467. #if defined(MBEDTLS_PLATFORM_TIME_ALT)
  2468. mbedtls_platform_set_time( dummy_constant_time );
  2469. #else
  2470. fprintf( stderr, "Warning: reproducible option used without constant time\n" );
  2471. #endif
  2472. #endif /* MBEDTLS_HAVE_TIME */
  2473. }
  2474. mbedtls_ssl_conf_rng( &conf, rng_get, &rng );
  2475. mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
  2476. #if defined(MBEDTLS_SSL_CACHE_C)
  2477. if( opt.cache_max != -1 )
  2478. mbedtls_ssl_cache_set_max_entries( &cache, opt.cache_max );
  2479. if( opt.cache_timeout != -1 )
  2480. mbedtls_ssl_cache_set_timeout( &cache, opt.cache_timeout );
  2481. mbedtls_ssl_conf_session_cache( &conf, &cache,
  2482. mbedtls_ssl_cache_get,
  2483. mbedtls_ssl_cache_set );
  2484. #endif
  2485. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2486. if( opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED )
  2487. {
  2488. if( ( ret = mbedtls_ssl_ticket_setup( &ticket_ctx,
  2489. rng_get, &rng,
  2490. MBEDTLS_CIPHER_AES_256_GCM,
  2491. opt.ticket_timeout ) ) != 0 )
  2492. {
  2493. mbedtls_printf( " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret );
  2494. goto exit;
  2495. }
  2496. mbedtls_ssl_conf_session_tickets_cb( &conf,
  2497. mbedtls_ssl_ticket_write,
  2498. mbedtls_ssl_ticket_parse,
  2499. &ticket_ctx );
  2500. }
  2501. #endif
  2502. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  2503. if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  2504. {
  2505. #if defined(MBEDTLS_SSL_COOKIE_C)
  2506. if( opt.cookies > 0 )
  2507. {
  2508. if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
  2509. rng_get, &rng ) ) != 0 )
  2510. {
  2511. mbedtls_printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
  2512. goto exit;
  2513. }
  2514. mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
  2515. &cookie_ctx );
  2516. }
  2517. else
  2518. #endif /* MBEDTLS_SSL_COOKIE_C */
  2519. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  2520. if( opt.cookies == 0 )
  2521. {
  2522. mbedtls_ssl_conf_dtls_cookies( &conf, NULL, NULL, NULL );
  2523. }
  2524. else
  2525. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
  2526. {
  2527. ; /* Nothing to do */
  2528. }
  2529. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  2530. if( opt.anti_replay != DFL_ANTI_REPLAY )
  2531. mbedtls_ssl_conf_dtls_anti_replay( &conf, opt.anti_replay );
  2532. #endif
  2533. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  2534. if( opt.badmac_limit != DFL_BADMAC_LIMIT )
  2535. mbedtls_ssl_conf_dtls_badmac_limit( &conf, opt.badmac_limit );
  2536. #endif
  2537. }
  2538. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  2539. if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
  2540. mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
  2541. #if defined(MBEDTLS_ARC4_C)
  2542. if( opt.arc4 != DFL_ARC4 )
  2543. mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
  2544. #endif
  2545. if( opt.version_suites != NULL )
  2546. {
  2547. mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[0],
  2548. MBEDTLS_SSL_MAJOR_VERSION_3,
  2549. MBEDTLS_SSL_MINOR_VERSION_0 );
  2550. mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[1],
  2551. MBEDTLS_SSL_MAJOR_VERSION_3,
  2552. MBEDTLS_SSL_MINOR_VERSION_1 );
  2553. mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[2],
  2554. MBEDTLS_SSL_MAJOR_VERSION_3,
  2555. MBEDTLS_SSL_MINOR_VERSION_2 );
  2556. mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[3],
  2557. MBEDTLS_SSL_MAJOR_VERSION_3,
  2558. MBEDTLS_SSL_MINOR_VERSION_3 );
  2559. }
  2560. if( opt.allow_legacy != DFL_ALLOW_LEGACY )
  2561. mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
  2562. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  2563. mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
  2564. if( opt.renego_delay != DFL_RENEGO_DELAY )
  2565. mbedtls_ssl_conf_renegotiation_enforced( &conf, opt.renego_delay );
  2566. if( opt.renego_period != DFL_RENEGO_PERIOD )
  2567. {
  2568. PUT_UINT64_BE( renego_period, opt.renego_period, 0 );
  2569. mbedtls_ssl_conf_renegotiation_period( &conf, renego_period );
  2570. }
  2571. #endif
  2572. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2573. if( strcmp( opt.ca_path, "none" ) != 0 &&
  2574. strcmp( opt.ca_file, "none" ) != 0 )
  2575. {
  2576. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  2577. if( opt.ca_callback != 0 )
  2578. mbedtls_ssl_conf_ca_cb( &conf, ca_callback, &cacert);
  2579. else
  2580. #endif
  2581. mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
  2582. }
  2583. if( key_cert_init )
  2584. {
  2585. mbedtls_pk_context *pk = &pkey;
  2586. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2587. if( opt.async_private_delay1 >= 0 )
  2588. {
  2589. ret = ssl_async_set_key( &ssl_async_keys, &srvcert, pk, 0,
  2590. opt.async_private_delay1 );
  2591. if( ret < 0 )
  2592. {
  2593. mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
  2594. ret );
  2595. goto exit;
  2596. }
  2597. pk = NULL;
  2598. }
  2599. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  2600. if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, pk ) ) != 0 )
  2601. {
  2602. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
  2603. goto exit;
  2604. }
  2605. }
  2606. if( key_cert_init2 )
  2607. {
  2608. mbedtls_pk_context *pk = &pkey2;
  2609. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2610. if( opt.async_private_delay2 >= 0 )
  2611. {
  2612. ret = ssl_async_set_key( &ssl_async_keys, &srvcert2, pk, 0,
  2613. opt.async_private_delay2 );
  2614. if( ret < 0 )
  2615. {
  2616. mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
  2617. ret );
  2618. goto exit;
  2619. }
  2620. pk = NULL;
  2621. }
  2622. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  2623. if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, pk ) ) != 0 )
  2624. {
  2625. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
  2626. goto exit;
  2627. }
  2628. }
  2629. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2630. if( opt.async_operations[0] != '-' )
  2631. {
  2632. mbedtls_ssl_async_sign_t *sign = NULL;
  2633. mbedtls_ssl_async_decrypt_t *decrypt = NULL;
  2634. const char *r;
  2635. for( r = opt.async_operations; *r; r++ )
  2636. {
  2637. switch( *r )
  2638. {
  2639. case 'd':
  2640. decrypt = ssl_async_decrypt;
  2641. break;
  2642. case 's':
  2643. sign = ssl_async_sign;
  2644. break;
  2645. }
  2646. }
  2647. ssl_async_keys.inject_error = ( opt.async_private_error < 0 ?
  2648. - opt.async_private_error :
  2649. opt.async_private_error );
  2650. ssl_async_keys.f_rng = rng_get;
  2651. ssl_async_keys.p_rng = &rng;
  2652. mbedtls_ssl_conf_async_private_cb( &conf,
  2653. sign,
  2654. decrypt,
  2655. ssl_async_resume,
  2656. ssl_async_cancel,
  2657. &ssl_async_keys );
  2658. }
  2659. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  2660. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2661. #if defined(SNI_OPTION)
  2662. if( opt.sni != NULL )
  2663. {
  2664. mbedtls_ssl_conf_sni( &conf, sni_callback, sni_info );
  2665. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2666. if( opt.async_private_delay2 >= 0 )
  2667. {
  2668. sni_entry *cur;
  2669. for( cur = sni_info; cur != NULL; cur = cur->next )
  2670. {
  2671. ret = ssl_async_set_key( &ssl_async_keys,
  2672. cur->cert, cur->key, 1,
  2673. opt.async_private_delay2 );
  2674. if( ret < 0 )
  2675. {
  2676. mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
  2677. ret );
  2678. goto exit;
  2679. }
  2680. cur->key = NULL;
  2681. }
  2682. }
  2683. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  2684. }
  2685. #endif
  2686. #if defined(MBEDTLS_ECP_C)
  2687. if( opt.curves != NULL &&
  2688. strcmp( opt.curves, "default" ) != 0 )
  2689. {
  2690. mbedtls_ssl_conf_curves( &conf, curve_list );
  2691. }
  2692. #endif
  2693. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  2694. if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
  2695. {
  2696. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2697. if( opt.psk_opaque != 0 )
  2698. {
  2699. /* The algorithm has already been determined earlier. */
  2700. status = psa_setup_psk_key_slot( &psk_slot, alg, psk, psk_len );
  2701. if( status != PSA_SUCCESS )
  2702. {
  2703. fprintf( stderr, "SETUP FAIL\n" );
  2704. ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
  2705. goto exit;
  2706. }
  2707. if( ( ret = mbedtls_ssl_conf_psk_opaque( &conf, psk_slot,
  2708. (const unsigned char *) opt.psk_identity,
  2709. strlen( opt.psk_identity ) ) ) != 0 )
  2710. {
  2711. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
  2712. ret );
  2713. goto exit;
  2714. }
  2715. }
  2716. else
  2717. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  2718. if( psk_len > 0 )
  2719. {
  2720. ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
  2721. (const unsigned char *) opt.psk_identity,
  2722. strlen( opt.psk_identity ) );
  2723. if( ret != 0 )
  2724. {
  2725. mbedtls_printf( " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", (unsigned int) -ret );
  2726. goto exit;
  2727. }
  2728. }
  2729. }
  2730. if( opt.psk_list != NULL )
  2731. {
  2732. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2733. if( opt.psk_list_opaque != 0 )
  2734. {
  2735. psk_entry *cur_psk;
  2736. for( cur_psk = psk_info; cur_psk != NULL; cur_psk = cur_psk->next )
  2737. {
  2738. status = psa_setup_psk_key_slot( &cur_psk->slot, alg,
  2739. cur_psk->key,
  2740. cur_psk->key_len );
  2741. if( status != PSA_SUCCESS )
  2742. {
  2743. ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
  2744. goto exit;
  2745. }
  2746. }
  2747. }
  2748. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  2749. mbedtls_ssl_conf_psk_cb( &conf, psk_callback, psk_info );
  2750. }
  2751. #endif
  2752. #if defined(MBEDTLS_DHM_C)
  2753. /*
  2754. * Use different group than default DHM group
  2755. */
  2756. #if defined(MBEDTLS_FS_IO)
  2757. if( opt.dhm_file != NULL )
  2758. ret = mbedtls_ssl_conf_dh_param_ctx( &conf, &dhm );
  2759. #endif
  2760. if( ret != 0 )
  2761. {
  2762. mbedtls_printf( " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", (unsigned int) -ret );
  2763. goto exit;
  2764. }
  2765. #endif
  2766. if( opt.min_version != DFL_MIN_VERSION )
  2767. mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
  2768. if( opt.max_version != DFL_MIN_VERSION )
  2769. mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
  2770. if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
  2771. {
  2772. mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", (unsigned int) -ret );
  2773. goto exit;
  2774. }
  2775. io_ctx.ssl = &ssl;
  2776. io_ctx.net = &client_fd;
  2777. mbedtls_ssl_set_bio( &ssl, &io_ctx, send_cb, recv_cb,
  2778. opt.nbio == 0 ? recv_timeout_cb : NULL );
  2779. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  2780. if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  2781. {
  2782. if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled,
  2783. cid, cid_len ) ) != 0 )
  2784. {
  2785. mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
  2786. ret );
  2787. goto exit;
  2788. }
  2789. }
  2790. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  2791. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  2792. if( opt.dtls_mtu != DFL_DTLS_MTU )
  2793. mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
  2794. #endif
  2795. #if defined(MBEDTLS_TIMING_C)
  2796. mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
  2797. mbedtls_timing_get_delay );
  2798. #endif
  2799. mbedtls_printf( " ok\n" );
  2800. reset:
  2801. #if !defined(_WIN32)
  2802. if( received_sigterm )
  2803. {
  2804. mbedtls_printf( " interrupted by SIGTERM (not in net_accept())\n" );
  2805. if( ret == MBEDTLS_ERR_NET_INVALID_CONTEXT )
  2806. ret = 0;
  2807. goto exit;
  2808. }
  2809. #endif
  2810. if( ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
  2811. {
  2812. mbedtls_printf( " ! Client initiated reconnection from same port\n" );
  2813. goto handshake;
  2814. }
  2815. #ifdef MBEDTLS_ERROR_C
  2816. if( ret != 0 )
  2817. {
  2818. char error_buf[100];
  2819. mbedtls_strerror( ret, error_buf, 100 );
  2820. mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
  2821. }
  2822. #endif
  2823. mbedtls_net_free( &client_fd );
  2824. mbedtls_ssl_session_reset( &ssl );
  2825. /*
  2826. * 3. Wait until a client connects
  2827. */
  2828. mbedtls_printf( " . Waiting for a remote connection ..." );
  2829. fflush( stdout );
  2830. if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
  2831. client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
  2832. {
  2833. #if !defined(_WIN32)
  2834. if( received_sigterm )
  2835. {
  2836. mbedtls_printf( " interrupted by SIGTERM (in net_accept())\n" );
  2837. if( ret == MBEDTLS_ERR_NET_ACCEPT_FAILED )
  2838. ret = 0;
  2839. goto exit;
  2840. }
  2841. #endif
  2842. mbedtls_printf( " failed\n ! mbedtls_net_accept returned -0x%x\n\n", (unsigned int) -ret );
  2843. goto exit;
  2844. }
  2845. if( opt.nbio > 0 )
  2846. ret = mbedtls_net_set_nonblock( &client_fd );
  2847. else
  2848. ret = mbedtls_net_set_block( &client_fd );
  2849. if( ret != 0 )
  2850. {
  2851. mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", (unsigned int) -ret );
  2852. goto exit;
  2853. }
  2854. mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
  2855. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  2856. if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  2857. {
  2858. if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
  2859. client_ip, cliip_len ) ) != 0 )
  2860. {
  2861. mbedtls_printf( " failed\n ! mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n",
  2862. (unsigned int) -ret );
  2863. goto exit;
  2864. }
  2865. }
  2866. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
  2867. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2868. if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
  2869. {
  2870. if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
  2871. (const unsigned char *) opt.ecjpake_pw,
  2872. strlen( opt.ecjpake_pw ) ) ) != 0 )
  2873. {
  2874. mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
  2875. goto exit;
  2876. }
  2877. }
  2878. #endif
  2879. mbedtls_printf( " ok\n" );
  2880. /*
  2881. * 4. Handshake
  2882. */
  2883. handshake:
  2884. mbedtls_printf( " . Performing the SSL/TLS handshake..." );
  2885. fflush( stdout );
  2886. while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
  2887. {
  2888. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2889. if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS &&
  2890. ssl_async_keys.inject_error == SSL_ASYNC_INJECT_ERROR_CANCEL )
  2891. {
  2892. mbedtls_printf( " cancelling on injected error\n" );
  2893. break;
  2894. }
  2895. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  2896. if( ! mbedtls_status_is_ssl_in_progress( ret ) )
  2897. break;
  2898. /* For event-driven IO, wait for socket to become available */
  2899. if( opt.event == 1 /* level triggered IO */ )
  2900. {
  2901. #if defined(MBEDTLS_TIMING_C)
  2902. ret = idle( &client_fd, &timer, ret );
  2903. #else
  2904. ret = idle( &client_fd, ret );
  2905. #endif
  2906. if( ret != 0 )
  2907. goto reset;
  2908. }
  2909. }
  2910. if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
  2911. {
  2912. mbedtls_printf( " hello verification requested\n" );
  2913. ret = 0;
  2914. goto reset;
  2915. }
  2916. else if( ret != 0 )
  2917. {
  2918. mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", (unsigned int) -ret );
  2919. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2920. if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
  2921. {
  2922. char vrfy_buf[512];
  2923. flags = mbedtls_ssl_get_verify_result( &ssl );
  2924. mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
  2925. mbedtls_printf( "%s\n", vrfy_buf );
  2926. }
  2927. #endif
  2928. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2929. if( opt.async_private_error < 0 )
  2930. /* Injected error only the first time round, to test reset */
  2931. ssl_async_keys.inject_error = SSL_ASYNC_INJECT_ERROR_NONE;
  2932. #endif
  2933. goto reset;
  2934. }
  2935. else /* ret == 0 */
  2936. {
  2937. mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
  2938. mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
  2939. }
  2940. if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
  2941. mbedtls_printf( " [ Record expansion is %d ]\n", ret );
  2942. else
  2943. mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
  2944. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  2945. mbedtls_printf( " [ Maximum input fragment length is %u ]\n",
  2946. (unsigned int) mbedtls_ssl_get_input_max_frag_len( &ssl ) );
  2947. mbedtls_printf( " [ Maximum output fragment length is %u ]\n",
  2948. (unsigned int) mbedtls_ssl_get_output_max_frag_len( &ssl ) );
  2949. #endif
  2950. #if defined(MBEDTLS_SSL_ALPN)
  2951. if( opt.alpn_string != NULL )
  2952. {
  2953. const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
  2954. mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
  2955. alp ? alp : "(none)" );
  2956. }
  2957. #endif
  2958. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2959. /*
  2960. * 5. Verify the client certificate
  2961. */
  2962. mbedtls_printf( " . Verifying peer X.509 certificate..." );
  2963. if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
  2964. {
  2965. char vrfy_buf[512];
  2966. mbedtls_printf( " failed\n" );
  2967. mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
  2968. mbedtls_printf( "%s\n", vrfy_buf );
  2969. }
  2970. else
  2971. mbedtls_printf( " ok\n" );
  2972. if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
  2973. {
  2974. char crt_buf[512];
  2975. mbedtls_printf( " . Peer certificate information ...\n" );
  2976. mbedtls_x509_crt_info( crt_buf, sizeof( crt_buf ), " ",
  2977. mbedtls_ssl_get_peer_cert( &ssl ) );
  2978. mbedtls_printf( "%s\n", crt_buf );
  2979. }
  2980. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2981. #if defined(MBEDTLS_SSL_EXPORT_KEYS)
  2982. if( opt.eap_tls != 0 )
  2983. {
  2984. size_t j = 0;
  2985. if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type,
  2986. eap_tls_keying.master_secret,
  2987. sizeof( eap_tls_keying.master_secret ),
  2988. eap_tls_label,
  2989. eap_tls_keying.randbytes,
  2990. sizeof( eap_tls_keying.randbytes ),
  2991. eap_tls_keymaterial,
  2992. sizeof( eap_tls_keymaterial ) ) )
  2993. != 0 )
  2994. {
  2995. mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
  2996. (unsigned int) -ret );
  2997. goto reset;
  2998. }
  2999. mbedtls_printf( " EAP-TLS key material is:" );
  3000. for( j = 0; j < sizeof( eap_tls_keymaterial ); j++ )
  3001. {
  3002. if( j % 8 == 0 )
  3003. mbedtls_printf("\n ");
  3004. mbedtls_printf("%02x ", eap_tls_keymaterial[j] );
  3005. }
  3006. mbedtls_printf("\n");
  3007. if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type, NULL, 0,
  3008. eap_tls_label,
  3009. eap_tls_keying.randbytes,
  3010. sizeof( eap_tls_keying.randbytes ),
  3011. eap_tls_iv,
  3012. sizeof( eap_tls_iv ) ) ) != 0 )
  3013. {
  3014. mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
  3015. (unsigned int) -ret );
  3016. goto reset;
  3017. }
  3018. mbedtls_printf( " EAP-TLS IV is:" );
  3019. for( j = 0; j < sizeof( eap_tls_iv ); j++ )
  3020. {
  3021. if( j % 8 == 0 )
  3022. mbedtls_printf("\n ");
  3023. mbedtls_printf("%02x ", eap_tls_iv[j] );
  3024. }
  3025. mbedtls_printf("\n");
  3026. }
  3027. #if defined( MBEDTLS_SSL_DTLS_SRTP )
  3028. else if( opt.use_srtp != 0 )
  3029. {
  3030. size_t j = 0;
  3031. mbedtls_dtls_srtp_info dtls_srtp_negotiation_result;
  3032. mbedtls_ssl_get_dtls_srtp_negotiation_result( &ssl, &dtls_srtp_negotiation_result );
  3033. if( dtls_srtp_negotiation_result.chosen_dtls_srtp_profile
  3034. == MBEDTLS_TLS_SRTP_UNSET )
  3035. {
  3036. mbedtls_printf( " Unable to negotiate "
  3037. "the use of DTLS-SRTP\n" );
  3038. }
  3039. else
  3040. {
  3041. if( ( ret = mbedtls_ssl_tls_prf( dtls_srtp_keying.tls_prf_type,
  3042. dtls_srtp_keying.master_secret,
  3043. sizeof( dtls_srtp_keying.master_secret ),
  3044. dtls_srtp_label,
  3045. dtls_srtp_keying.randbytes,
  3046. sizeof( dtls_srtp_keying.randbytes ),
  3047. dtls_srtp_key_material,
  3048. sizeof( dtls_srtp_key_material ) ) )
  3049. != 0 )
  3050. {
  3051. mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
  3052. (unsigned int) -ret );
  3053. goto exit;
  3054. }
  3055. mbedtls_printf( " DTLS-SRTP key material is:" );
  3056. for( j = 0; j < sizeof( dtls_srtp_key_material ); j++ )
  3057. {
  3058. if( j % 8 == 0 )
  3059. mbedtls_printf( "\n " );
  3060. mbedtls_printf( "%02x ", dtls_srtp_key_material[j] );
  3061. }
  3062. mbedtls_printf( "\n" );
  3063. /* produce a less readable output used to perform automatic checks
  3064. * - compare client and server output
  3065. * - interop test with openssl which client produces this kind of output
  3066. */
  3067. mbedtls_printf( " Keying material: " );
  3068. for( j = 0; j < sizeof( dtls_srtp_key_material ); j++ )
  3069. {
  3070. mbedtls_printf( "%02X", dtls_srtp_key_material[j] );
  3071. }
  3072. mbedtls_printf( "\n" );
  3073. if ( dtls_srtp_negotiation_result.mki_len > 0 )
  3074. {
  3075. mbedtls_printf( " DTLS-SRTP mki value: " );
  3076. for( j = 0; j < dtls_srtp_negotiation_result.mki_len; j++ )
  3077. {
  3078. mbedtls_printf( "%02X", dtls_srtp_negotiation_result.mki_value[j] );
  3079. }
  3080. }
  3081. else
  3082. {
  3083. mbedtls_printf( " DTLS-SRTP no mki value negotiated" );
  3084. }
  3085. mbedtls_printf( "\n" );
  3086. }
  3087. }
  3088. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  3089. #endif /* MBEDTLS_SSL_EXPORT_KEYS */
  3090. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  3091. ret = report_cid_usage( &ssl, "initial handshake" );
  3092. if( ret != 0 )
  3093. goto exit;
  3094. if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  3095. {
  3096. if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
  3097. cid_renego, cid_renego_len ) ) != 0 )
  3098. {
  3099. mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
  3100. ret );
  3101. goto exit;
  3102. }
  3103. }
  3104. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  3105. #if defined(MBEDTLS_MEMORY_DEBUG)
  3106. mbedtls_memory_buffer_alloc_cur_get( &current_heap_memory, &heap_blocks );
  3107. mbedtls_memory_buffer_alloc_max_get( &peak_heap_memory, &heap_blocks );
  3108. mbedtls_printf( "Heap memory usage after handshake: %lu bytes. Peak memory usage was %lu\n",
  3109. (unsigned long) current_heap_memory, (unsigned long) peak_heap_memory );
  3110. #endif /* MBEDTLS_MEMORY_DEBUG */
  3111. if( opt.exchanges == 0 )
  3112. goto close_notify;
  3113. exchanges_left = opt.exchanges;
  3114. data_exchange:
  3115. /*
  3116. * 6. Read the HTTP Request
  3117. */
  3118. mbedtls_printf( " < Read from client:" );
  3119. fflush( stdout );
  3120. /*
  3121. * TLS and DTLS need different reading styles (stream vs datagram)
  3122. */
  3123. if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
  3124. {
  3125. do
  3126. {
  3127. int terminated = 0;
  3128. len = opt.buffer_size - 1;
  3129. memset( buf, 0, opt.buffer_size );
  3130. ret = mbedtls_ssl_read( &ssl, buf, len );
  3131. if( mbedtls_status_is_ssl_in_progress( ret ) )
  3132. {
  3133. if( opt.event == 1 /* level triggered IO */ )
  3134. {
  3135. #if defined(MBEDTLS_TIMING_C)
  3136. idle( &client_fd, &timer, ret );
  3137. #else
  3138. idle( &client_fd, ret );
  3139. #endif
  3140. }
  3141. continue;
  3142. }
  3143. if( ret <= 0 )
  3144. {
  3145. switch( ret )
  3146. {
  3147. case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
  3148. mbedtls_printf( " connection was closed gracefully\n" );
  3149. goto close_notify;
  3150. case 0:
  3151. case MBEDTLS_ERR_NET_CONN_RESET:
  3152. mbedtls_printf( " connection was reset by peer\n" );
  3153. ret = MBEDTLS_ERR_NET_CONN_RESET;
  3154. goto reset;
  3155. default:
  3156. mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", (unsigned int) -ret );
  3157. goto reset;
  3158. }
  3159. }
  3160. if( mbedtls_ssl_get_bytes_avail( &ssl ) == 0 )
  3161. {
  3162. len = ret;
  3163. buf[len] = '\0';
  3164. mbedtls_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
  3165. /* End of message should be detected according to the syntax of the
  3166. * application protocol (eg HTTP), just use a dummy test here. */
  3167. if( buf[len - 1] == '\n' )
  3168. terminated = 1;
  3169. }
  3170. else
  3171. {
  3172. int extra_len, ori_len;
  3173. unsigned char *larger_buf;
  3174. ori_len = ret;
  3175. extra_len = (int) mbedtls_ssl_get_bytes_avail( &ssl );
  3176. larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 );
  3177. if( larger_buf == NULL )
  3178. {
  3179. mbedtls_printf( " ! memory allocation failed\n" );
  3180. ret = 1;
  3181. goto reset;
  3182. }
  3183. memset( larger_buf, 0, ori_len + extra_len );
  3184. memcpy( larger_buf, buf, ori_len );
  3185. /* This read should never fail and get the whole cached data */
  3186. ret = mbedtls_ssl_read( &ssl, larger_buf + ori_len, extra_len );
  3187. if( ret != extra_len ||
  3188. mbedtls_ssl_get_bytes_avail( &ssl ) != 0 )
  3189. {
  3190. mbedtls_printf( " ! mbedtls_ssl_read failed on cached data\n" );
  3191. ret = 1;
  3192. goto reset;
  3193. }
  3194. larger_buf[ori_len + extra_len] = '\0';
  3195. mbedtls_printf( " %d bytes read (%d + %d)\n\n%s\n",
  3196. ori_len + extra_len, ori_len, extra_len,
  3197. (char *) larger_buf );
  3198. /* End of message should be detected according to the syntax of the
  3199. * application protocol (eg HTTP), just use a dummy test here. */
  3200. if( larger_buf[ori_len + extra_len - 1] == '\n' )
  3201. terminated = 1;
  3202. mbedtls_free( larger_buf );
  3203. }
  3204. if( terminated )
  3205. {
  3206. ret = 0;
  3207. break;
  3208. }
  3209. }
  3210. while( 1 );
  3211. }
  3212. else /* Not stream, so datagram */
  3213. {
  3214. len = opt.buffer_size - 1;
  3215. memset( buf, 0, opt.buffer_size );
  3216. do
  3217. {
  3218. /* Without the call to `mbedtls_ssl_check_pending`, it might
  3219. * happen that the client sends application data in the same
  3220. * datagram as the Finished message concluding the handshake.
  3221. * In this case, the application data would be ready to be
  3222. * processed while the underlying transport wouldn't signal
  3223. * any further incoming data.
  3224. *
  3225. * See the test 'Event-driven I/O: session-id resume, UDP packing'
  3226. * in tests/ssl-opt.sh.
  3227. */
  3228. /* For event-driven IO, wait for socket to become available */
  3229. if( mbedtls_ssl_check_pending( &ssl ) == 0 &&
  3230. opt.event == 1 /* level triggered IO */ )
  3231. {
  3232. #if defined(MBEDTLS_TIMING_C)
  3233. idle( &client_fd, &timer, MBEDTLS_ERR_SSL_WANT_READ );
  3234. #else
  3235. idle( &client_fd, MBEDTLS_ERR_SSL_WANT_READ );
  3236. #endif
  3237. }
  3238. ret = mbedtls_ssl_read( &ssl, buf, len );
  3239. /* Note that even if `mbedtls_ssl_check_pending` returns true,
  3240. * it can happen that the subsequent call to `mbedtls_ssl_read`
  3241. * returns `MBEDTLS_ERR_SSL_WANT_READ`, because the pending messages
  3242. * might be discarded (e.g. because they are retransmissions). */
  3243. }
  3244. while( mbedtls_status_is_ssl_in_progress( ret ) );
  3245. if( ret <= 0 )
  3246. {
  3247. switch( ret )
  3248. {
  3249. case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
  3250. mbedtls_printf( " connection was closed gracefully\n" );
  3251. ret = 0;
  3252. goto close_notify;
  3253. default:
  3254. mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", (unsigned int) -ret );
  3255. goto reset;
  3256. }
  3257. }
  3258. len = ret;
  3259. buf[len] = '\0';
  3260. mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
  3261. ret = 0;
  3262. }
  3263. /*
  3264. * 7a. Request renegotiation while client is waiting for input from us.
  3265. * (only on the first exchange, to be able to test retransmission)
  3266. */
  3267. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  3268. if( opt.renegotiate && exchanges_left == opt.exchanges )
  3269. {
  3270. mbedtls_printf( " . Requestion renegotiation..." );
  3271. fflush( stdout );
  3272. while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
  3273. {
  3274. if( ! mbedtls_status_is_ssl_in_progress( ret ) )
  3275. {
  3276. mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
  3277. goto reset;
  3278. }
  3279. /* For event-driven IO, wait for socket to become available */
  3280. if( opt.event == 1 /* level triggered IO */ )
  3281. {
  3282. #if defined(MBEDTLS_TIMING_C)
  3283. idle( &client_fd, &timer, ret );
  3284. #else
  3285. idle( &client_fd, ret );
  3286. #endif
  3287. }
  3288. }
  3289. mbedtls_printf( " ok\n" );
  3290. }
  3291. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  3292. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  3293. ret = report_cid_usage( &ssl, "after renegotiation" );
  3294. if( ret != 0 )
  3295. goto exit;
  3296. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  3297. /*
  3298. * 7. Write the 200 Response
  3299. */
  3300. mbedtls_printf( " > Write to client:" );
  3301. fflush( stdout );
  3302. len = sprintf( (char *) buf, HTTP_RESPONSE,
  3303. mbedtls_ssl_get_ciphersuite( &ssl ) );
  3304. /* Add padding to the response to reach opt.response_size in length */
  3305. if( opt.response_size != DFL_RESPONSE_SIZE &&
  3306. len < opt.response_size )
  3307. {
  3308. memset( buf + len, 'B', opt.response_size - len );
  3309. len += opt.response_size - len;
  3310. }
  3311. /* Truncate if response size is smaller than the "natural" size */
  3312. if( opt.response_size != DFL_RESPONSE_SIZE &&
  3313. len > opt.response_size )
  3314. {
  3315. len = opt.response_size;
  3316. /* Still end with \r\n unless that's really not possible */
  3317. if( len >= 2 ) buf[len - 2] = '\r';
  3318. if( len >= 1 ) buf[len - 1] = '\n';
  3319. }
  3320. if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
  3321. {
  3322. for( written = 0, frags = 0; written < len; written += ret, frags++ )
  3323. {
  3324. while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
  3325. <= 0 )
  3326. {
  3327. if( ret == MBEDTLS_ERR_NET_CONN_RESET )
  3328. {
  3329. mbedtls_printf( " failed\n ! peer closed the connection\n\n" );
  3330. goto reset;
  3331. }
  3332. if( ! mbedtls_status_is_ssl_in_progress( ret ) )
  3333. {
  3334. mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
  3335. goto reset;
  3336. }
  3337. /* For event-driven IO, wait for socket to become available */
  3338. if( opt.event == 1 /* level triggered IO */ )
  3339. {
  3340. #if defined(MBEDTLS_TIMING_C)
  3341. idle( &client_fd, &timer, ret );
  3342. #else
  3343. idle( &client_fd, ret );
  3344. #endif
  3345. }
  3346. }
  3347. }
  3348. }
  3349. else /* Not stream, so datagram */
  3350. {
  3351. while( 1 )
  3352. {
  3353. ret = mbedtls_ssl_write( &ssl, buf, len );
  3354. if( ! mbedtls_status_is_ssl_in_progress( ret ) )
  3355. break;
  3356. /* For event-driven IO, wait for socket to become available */
  3357. if( opt.event == 1 /* level triggered IO */ )
  3358. {
  3359. #if defined(MBEDTLS_TIMING_C)
  3360. idle( &client_fd, &timer, ret );
  3361. #else
  3362. idle( &client_fd, ret );
  3363. #endif
  3364. }
  3365. }
  3366. if( ret < 0 )
  3367. {
  3368. mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
  3369. goto reset;
  3370. }
  3371. frags = 1;
  3372. written = ret;
  3373. }
  3374. buf[written] = '\0';
  3375. mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
  3376. ret = 0;
  3377. /*
  3378. * 7b. Simulate serialize/deserialize and go back to data exchange
  3379. */
  3380. #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
  3381. if( opt.serialize != 0 )
  3382. {
  3383. size_t buf_len;
  3384. mbedtls_printf( " . Serializing live connection..." );
  3385. ret = mbedtls_ssl_context_save( &ssl, NULL, 0, &buf_len );
  3386. if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
  3387. {
  3388. mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
  3389. "-0x%x\n\n", (unsigned int) -ret );
  3390. goto exit;
  3391. }
  3392. if( ( context_buf = mbedtls_calloc( 1, buf_len ) ) == NULL )
  3393. {
  3394. mbedtls_printf( " failed\n ! Couldn't allocate buffer for "
  3395. "serialized context" );
  3396. goto exit;
  3397. }
  3398. context_buf_len = buf_len;
  3399. if( ( ret = mbedtls_ssl_context_save( &ssl, context_buf,
  3400. buf_len, &buf_len ) ) != 0 )
  3401. {
  3402. mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
  3403. "-0x%x\n\n", (unsigned int) -ret );
  3404. goto exit;
  3405. }
  3406. mbedtls_printf( " ok\n" );
  3407. /* Save serialized context to the 'opt.context_file' as a base64 code */
  3408. if( 0 < strlen( opt.context_file ) )
  3409. {
  3410. FILE *b64_file;
  3411. uint8_t *b64_buf;
  3412. size_t b64_len;
  3413. mbedtls_printf( " . Save serialized context to a file... " );
  3414. mbedtls_base64_encode( NULL, 0, &b64_len, context_buf, buf_len );
  3415. if( ( b64_buf = mbedtls_calloc( 1, b64_len ) ) == NULL )
  3416. {
  3417. mbedtls_printf( "failed\n ! Couldn't allocate buffer for "
  3418. "the base64 code\n" );
  3419. goto exit;
  3420. }
  3421. if( ( ret = mbedtls_base64_encode( b64_buf, b64_len, &b64_len,
  3422. context_buf, buf_len ) ) != 0 )
  3423. {
  3424. mbedtls_printf( "failed\n ! mbedtls_base64_encode returned "
  3425. "-0x%x\n", (unsigned int) -ret );
  3426. mbedtls_free( b64_buf );
  3427. goto exit;
  3428. }
  3429. if( ( b64_file = fopen( opt.context_file, "w" ) ) == NULL )
  3430. {
  3431. mbedtls_printf( "failed\n ! Cannot open '%s' for writing.\n",
  3432. opt.context_file );
  3433. mbedtls_free( b64_buf );
  3434. goto exit;
  3435. }
  3436. if( b64_len != fwrite( b64_buf, 1, b64_len, b64_file ) )
  3437. {
  3438. mbedtls_printf( "failed\n ! fwrite(%ld bytes) failed\n",
  3439. (long) b64_len );
  3440. mbedtls_free( b64_buf );
  3441. fclose( b64_file );
  3442. goto exit;
  3443. }
  3444. mbedtls_free( b64_buf );
  3445. fclose( b64_file );
  3446. mbedtls_printf( "ok\n" );
  3447. }
  3448. /*
  3449. * This simulates a workflow where you have a long-lived server
  3450. * instance, potentially with a pool of ssl_context objects, and you
  3451. * just want to re-use one while the connection is inactive: in that
  3452. * case you can just reset() it, and then it's ready to receive
  3453. * serialized data from another connection (or the same here).
  3454. */
  3455. if( opt.serialize == 1 )
  3456. {
  3457. /* nothing to do here, done by context_save() already */
  3458. mbedtls_printf( " . Context has been reset... ok\n" );
  3459. }
  3460. /*
  3461. * This simulates a workflow where you have one server instance per
  3462. * connection, and want to release it entire when the connection is
  3463. * inactive, and spawn it again when needed again - this would happen
  3464. * between ssl_free() and ssl_init() below, together with any other
  3465. * teardown/startup code needed - for example, preparing the
  3466. * ssl_config again (see section 3 "setup stuff" in this file).
  3467. */
  3468. if( opt.serialize == 2 )
  3469. {
  3470. mbedtls_printf( " . Freeing and reinitializing context..." );
  3471. mbedtls_ssl_free( &ssl );
  3472. mbedtls_ssl_init( &ssl );
  3473. if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
  3474. {
  3475. mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned "
  3476. "-0x%x\n\n", (unsigned int) -ret );
  3477. goto exit;
  3478. }
  3479. /*
  3480. * This illustrates the minimum amount of things you need to set
  3481. * up, however you could set up much more if desired, for example
  3482. * if you want to share your set up code between the case of
  3483. * establishing a new connection and this case.
  3484. */
  3485. if( opt.nbio == 2 )
  3486. mbedtls_ssl_set_bio( &ssl, &client_fd, delayed_send,
  3487. delayed_recv, NULL );
  3488. else
  3489. mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send,
  3490. mbedtls_net_recv,
  3491. opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
  3492. #if defined(MBEDTLS_TIMING_C)
  3493. mbedtls_ssl_set_timer_cb( &ssl, &timer,
  3494. mbedtls_timing_set_delay,
  3495. mbedtls_timing_get_delay );
  3496. #endif /* MBEDTLS_TIMING_C */
  3497. mbedtls_printf( " ok\n" );
  3498. }
  3499. mbedtls_printf( " . Deserializing connection..." );
  3500. if( ( ret = mbedtls_ssl_context_load( &ssl, context_buf,
  3501. buf_len ) ) != 0 )
  3502. {
  3503. mbedtls_printf( "failed\n ! mbedtls_ssl_context_load returned "
  3504. "-0x%x\n\n", (unsigned int) -ret );
  3505. goto exit;
  3506. }
  3507. mbedtls_free( context_buf );
  3508. context_buf = NULL;
  3509. context_buf_len = 0;
  3510. mbedtls_printf( " ok\n" );
  3511. }
  3512. #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
  3513. /*
  3514. * 7c. Continue doing data exchanges?
  3515. */
  3516. if( --exchanges_left > 0 )
  3517. goto data_exchange;
  3518. /*
  3519. * 8. Done, cleanly close the connection
  3520. */
  3521. close_notify:
  3522. mbedtls_printf( " . Closing the connection..." );
  3523. /* No error checking, the connection might be closed already */
  3524. do ret = mbedtls_ssl_close_notify( &ssl );
  3525. while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  3526. ret = 0;
  3527. mbedtls_printf( " done\n" );
  3528. goto reset;
  3529. /*
  3530. * Cleanup and exit
  3531. */
  3532. exit:
  3533. #ifdef MBEDTLS_ERROR_C
  3534. if( ret != 0 )
  3535. {
  3536. char error_buf[100];
  3537. mbedtls_strerror( ret, error_buf, 100 );
  3538. mbedtls_printf("Last error was: -0x%X - %s\n\n", (unsigned int) -ret, error_buf );
  3539. }
  3540. #endif
  3541. if( opt.query_config_mode == DFL_QUERY_CONFIG_MODE )
  3542. {
  3543. mbedtls_printf( " . Cleaning up..." );
  3544. fflush( stdout );
  3545. }
  3546. mbedtls_net_free( &client_fd );
  3547. mbedtls_net_free( &listen_fd );
  3548. mbedtls_ssl_free( &ssl );
  3549. mbedtls_ssl_config_free( &conf );
  3550. #if defined(MBEDTLS_SSL_CACHE_C)
  3551. mbedtls_ssl_cache_free( &cache );
  3552. #endif
  3553. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  3554. mbedtls_ssl_ticket_free( &ticket_ctx );
  3555. #endif
  3556. #if defined(MBEDTLS_SSL_COOKIE_C)
  3557. mbedtls_ssl_cookie_free( &cookie_ctx );
  3558. #endif
  3559. #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
  3560. if( context_buf != NULL )
  3561. mbedtls_platform_zeroize( context_buf, context_buf_len );
  3562. mbedtls_free( context_buf );
  3563. #endif
  3564. #if defined(SNI_OPTION)
  3565. sni_free( sni_info );
  3566. #endif
  3567. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  3568. ret = psk_free( psk_info );
  3569. if( ( ret != 0 ) && ( opt.query_config_mode == DFL_QUERY_CONFIG_MODE ) )
  3570. mbedtls_printf( "Failed to list of opaque PSKs - error was %d\n", ret );
  3571. #endif
  3572. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  3573. mbedtls_x509_crt_free( &cacert );
  3574. mbedtls_x509_crt_free( &srvcert );
  3575. mbedtls_pk_free( &pkey );
  3576. mbedtls_x509_crt_free( &srvcert2 );
  3577. mbedtls_pk_free( &pkey2 );
  3578. #endif
  3579. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
  3580. mbedtls_dhm_free( &dhm );
  3581. #endif
  3582. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  3583. for( i = 0; (size_t) i < ssl_async_keys.slots_used; i++ )
  3584. {
  3585. if( ssl_async_keys.slots[i].pk_owned )
  3586. {
  3587. mbedtls_pk_free( ssl_async_keys.slots[i].pk );
  3588. mbedtls_free( ssl_async_keys.slots[i].pk );
  3589. ssl_async_keys.slots[i].pk = NULL;
  3590. }
  3591. }
  3592. #endif
  3593. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) && \
  3594. defined(MBEDTLS_USE_PSA_CRYPTO)
  3595. if( opt.psk_opaque != 0 )
  3596. {
  3597. /* This is ok even if the slot hasn't been
  3598. * initialized (we might have jumed here
  3599. * immediately because of bad cmd line params,
  3600. * for example). */
  3601. status = psa_destroy_key( psk_slot );
  3602. if( ( status != PSA_SUCCESS ) &&
  3603. ( opt.query_config_mode == DFL_QUERY_CONFIG_MODE ) )
  3604. {
  3605. mbedtls_printf( "Failed to destroy key slot %u - error was %d",
  3606. (unsigned) psk_slot, (int) status );
  3607. }
  3608. }
  3609. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED &&
  3610. MBEDTLS_USE_PSA_CRYPTO */
  3611. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3612. const char* message = mbedtls_test_helper_is_psa_leaking();
  3613. if( message )
  3614. {
  3615. if( ret == 0 )
  3616. ret = 1;
  3617. mbedtls_printf( "PSA memory leak detected: %s\n", message);
  3618. }
  3619. #endif
  3620. /* For builds with MBEDTLS_TEST_USE_PSA_CRYPTO_RNG psa crypto
  3621. * resources are freed by rng_free(). */
  3622. #if defined(MBEDTLS_USE_PSA_CRYPTO) && \
  3623. !defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
  3624. mbedtls_psa_crypto_free( );
  3625. #endif
  3626. rng_free( &rng );
  3627. mbedtls_free( buf );
  3628. #if defined(MBEDTLS_TEST_HOOKS)
  3629. /* Let test hooks detect errors such as resource leaks.
  3630. * Don't do it in query_config mode, because some test code prints
  3631. * information to stdout and this gets mixed with the regular output. */
  3632. if( opt.query_config_mode == DFL_QUERY_CONFIG_MODE )
  3633. {
  3634. if( test_hooks_failure_detected( ) )
  3635. {
  3636. if( ret == 0 )
  3637. ret = 1;
  3638. mbedtls_printf( "Test hooks detected errors.\n" );
  3639. }
  3640. }
  3641. test_hooks_free( );
  3642. #endif /* MBEDTLS_TEST_HOOKS */
  3643. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  3644. #if defined(MBEDTLS_MEMORY_DEBUG)
  3645. mbedtls_memory_buffer_alloc_status();
  3646. #endif
  3647. mbedtls_memory_buffer_alloc_free();
  3648. #endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */
  3649. if( opt.query_config_mode == DFL_QUERY_CONFIG_MODE )
  3650. {
  3651. mbedtls_printf( " done.\n" );
  3652. #if defined(_WIN32)
  3653. mbedtls_printf( " + Press Enter to exit this program.\n" );
  3654. fflush( stdout ); getchar();
  3655. #endif
  3656. }
  3657. // Shell can not handle large exit numbers -> 1 for errors
  3658. if( ret < 0 )
  3659. ret = 1;
  3660. if( opt.query_config_mode == DFL_QUERY_CONFIG_MODE )
  3661. mbedtls_exit( ret );
  3662. else
  3663. mbedtls_exit( query_config_ret );
  3664. }
  3665. #endif /* !MBEDTLS_SSL_TEST_IMPOSSIBLE && MBEDTLS_SSL_SRV_C */