ssl_server2.c 159 KB

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