ssl_srv.c 160 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631
  1. /*
  2. * SSLv3/TLSv1 server-side functions
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "common.h"
  20. #if defined(MBEDTLS_SSL_SRV_C)
  21. #include "mbedtls/platform.h"
  22. #include "mbedtls/ssl.h"
  23. #include "mbedtls/ssl_internal.h"
  24. #include "mbedtls/debug.h"
  25. #include "mbedtls/error.h"
  26. #include "mbedtls/platform_util.h"
  27. #include "constant_time_internal.h"
  28. #include "mbedtls/constant_time.h"
  29. #include <string.h>
  30. #if defined(MBEDTLS_ECP_C)
  31. #include "mbedtls/ecp.h"
  32. #endif
  33. #if defined(MBEDTLS_HAVE_TIME)
  34. #include "mbedtls/platform_time.h"
  35. #endif
  36. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  37. int mbedtls_ssl_set_client_transport_id(mbedtls_ssl_context *ssl,
  38. const unsigned char *info,
  39. size_t ilen)
  40. {
  41. if (ssl->conf->endpoint != MBEDTLS_SSL_IS_SERVER) {
  42. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  43. }
  44. mbedtls_free(ssl->cli_id);
  45. if ((ssl->cli_id = mbedtls_calloc(1, ilen)) == NULL) {
  46. return MBEDTLS_ERR_SSL_ALLOC_FAILED;
  47. }
  48. memcpy(ssl->cli_id, info, ilen);
  49. ssl->cli_id_len = ilen;
  50. return 0;
  51. }
  52. void mbedtls_ssl_conf_dtls_cookies(mbedtls_ssl_config *conf,
  53. mbedtls_ssl_cookie_write_t *f_cookie_write,
  54. mbedtls_ssl_cookie_check_t *f_cookie_check,
  55. void *p_cookie)
  56. {
  57. conf->f_cookie_write = f_cookie_write;
  58. conf->f_cookie_check = f_cookie_check;
  59. conf->p_cookie = p_cookie;
  60. }
  61. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
  62. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  63. MBEDTLS_CHECK_RETURN_CRITICAL
  64. static int ssl_parse_servername_ext(mbedtls_ssl_context *ssl,
  65. const unsigned char *buf,
  66. size_t len)
  67. {
  68. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  69. size_t servername_list_size, hostname_len;
  70. const unsigned char *p;
  71. MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
  72. if (len < 2) {
  73. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  74. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  75. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  76. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  77. }
  78. servername_list_size = ((buf[0] << 8) | (buf[1]));
  79. if (servername_list_size + 2 != len) {
  80. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  81. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  82. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  83. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  84. }
  85. p = buf + 2;
  86. while (servername_list_size > 2) {
  87. hostname_len = ((p[1] << 8) | p[2]);
  88. if (hostname_len + 3 > servername_list_size) {
  89. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  90. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  91. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  92. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  93. }
  94. if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
  95. ret = ssl->conf->f_sni(ssl->conf->p_sni,
  96. ssl, p + 3, hostname_len);
  97. if (ret != 0) {
  98. MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
  99. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  100. MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME);
  101. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  102. }
  103. return 0;
  104. }
  105. servername_list_size -= hostname_len + 3;
  106. p += hostname_len + 3;
  107. }
  108. if (servername_list_size != 0) {
  109. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  110. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  111. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  112. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  113. }
  114. return 0;
  115. }
  116. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  117. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  118. MBEDTLS_CHECK_RETURN_CRITICAL
  119. static int ssl_conf_has_psk_or_cb(mbedtls_ssl_config const *conf)
  120. {
  121. if (conf->f_psk != NULL) {
  122. return 1;
  123. }
  124. if (conf->psk_identity_len == 0 || conf->psk_identity == NULL) {
  125. return 0;
  126. }
  127. if (conf->psk != NULL && conf->psk_len != 0) {
  128. return 1;
  129. }
  130. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  131. if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
  132. return 1;
  133. }
  134. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  135. return 0;
  136. }
  137. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  138. MBEDTLS_CHECK_RETURN_CRITICAL
  139. static int ssl_use_opaque_psk(mbedtls_ssl_context const *ssl)
  140. {
  141. if (ssl->conf->f_psk != NULL) {
  142. /* If we've used a callback to select the PSK,
  143. * the static configuration is irrelevant. */
  144. if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
  145. return 1;
  146. }
  147. return 0;
  148. }
  149. if (!mbedtls_svc_key_id_is_null(ssl->conf->psk_opaque)) {
  150. return 1;
  151. }
  152. return 0;
  153. }
  154. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  155. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  156. MBEDTLS_CHECK_RETURN_CRITICAL
  157. static int ssl_parse_renegotiation_info(mbedtls_ssl_context *ssl,
  158. const unsigned char *buf,
  159. size_t len)
  160. {
  161. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  162. if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
  163. /* Check verify-data in constant-time. The length OTOH is no secret */
  164. if (len != 1 + ssl->verify_data_len ||
  165. buf[0] != ssl->verify_data_len ||
  166. mbedtls_ct_memcmp(buf + 1, ssl->peer_verify_data,
  167. ssl->verify_data_len) != 0) {
  168. MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info"));
  169. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  170. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  171. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  172. }
  173. } else
  174. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  175. {
  176. if (len != 1 || buf[0] != 0x0) {
  177. MBEDTLS_SSL_DEBUG_MSG(1, ("non-zero length renegotiation info"));
  178. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  179. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  180. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  181. }
  182. ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
  183. }
  184. return 0;
  185. }
  186. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  187. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  188. /*
  189. * Status of the implementation of signature-algorithms extension:
  190. *
  191. * Currently, we are only considering the signature-algorithm extension
  192. * to pick a ciphersuite which allows us to send the ServerKeyExchange
  193. * message with a signature-hash combination that the user allows.
  194. *
  195. * We do *not* check whether all certificates in our certificate
  196. * chain are signed with an allowed signature-hash pair.
  197. * This needs to be done at a later stage.
  198. *
  199. */
  200. MBEDTLS_CHECK_RETURN_CRITICAL
  201. static int ssl_parse_signature_algorithms_ext(mbedtls_ssl_context *ssl,
  202. const unsigned char *buf,
  203. size_t len)
  204. {
  205. size_t sig_alg_list_size;
  206. const unsigned char *p;
  207. const unsigned char *end = buf + len;
  208. mbedtls_md_type_t md_cur;
  209. mbedtls_pk_type_t sig_cur;
  210. if (len < 2) {
  211. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  212. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  213. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  214. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  215. }
  216. sig_alg_list_size = ((buf[0] << 8) | (buf[1]));
  217. if (sig_alg_list_size + 2 != len ||
  218. sig_alg_list_size % 2 != 0) {
  219. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  220. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  221. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  222. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  223. }
  224. /* Currently we only guarantee signing the ServerKeyExchange message according
  225. * to the constraints specified in this extension (see above), so it suffices
  226. * to remember only one suitable hash for each possible signature algorithm.
  227. *
  228. * This will change when we also consider certificate signatures,
  229. * in which case we will need to remember the whole signature-hash
  230. * pair list from the extension.
  231. */
  232. for (p = buf + 2; p < end; p += 2) {
  233. /* Silently ignore unknown signature or hash algorithms. */
  234. if ((sig_cur = mbedtls_ssl_pk_alg_from_sig(p[1])) == MBEDTLS_PK_NONE) {
  235. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext"
  236. " unknown sig alg encoding %d", p[1]));
  237. continue;
  238. }
  239. /* Check if we support the hash the user proposes */
  240. md_cur = mbedtls_ssl_md_alg_from_hash(p[0]);
  241. if (md_cur == MBEDTLS_MD_NONE) {
  242. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext:"
  243. " unknown hash alg encoding %d", p[0]));
  244. continue;
  245. }
  246. if (mbedtls_ssl_check_sig_hash(ssl, md_cur) == 0) {
  247. mbedtls_ssl_sig_hash_set_add(&ssl->handshake->hash_algs, sig_cur, md_cur);
  248. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext:"
  249. " match sig %u and hash %u",
  250. (unsigned) sig_cur, (unsigned) md_cur));
  251. } else {
  252. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext: "
  253. "hash alg %u not supported", (unsigned) md_cur));
  254. }
  255. }
  256. return 0;
  257. }
  258. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
  259. MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  260. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  261. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  262. MBEDTLS_CHECK_RETURN_CRITICAL
  263. static int ssl_parse_supported_elliptic_curves(mbedtls_ssl_context *ssl,
  264. const unsigned char *buf,
  265. size_t len)
  266. {
  267. size_t list_size, our_size;
  268. const unsigned char *p;
  269. const mbedtls_ecp_curve_info *curve_info, **curves;
  270. if (len < 2) {
  271. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  272. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  273. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  274. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  275. }
  276. list_size = ((buf[0] << 8) | (buf[1]));
  277. if (list_size + 2 != len ||
  278. list_size % 2 != 0) {
  279. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  280. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  281. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  282. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  283. }
  284. /* Should never happen unless client duplicates the extension */
  285. if (ssl->handshake->curves != NULL) {
  286. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  287. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  288. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  289. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  290. }
  291. /* Don't allow our peer to make us allocate too much memory,
  292. * and leave room for a final 0 */
  293. our_size = list_size / 2 + 1;
  294. if (our_size > MBEDTLS_ECP_DP_MAX) {
  295. our_size = MBEDTLS_ECP_DP_MAX;
  296. }
  297. if ((curves = mbedtls_calloc(our_size, sizeof(*curves))) == NULL) {
  298. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  299. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
  300. return MBEDTLS_ERR_SSL_ALLOC_FAILED;
  301. }
  302. ssl->handshake->curves = curves;
  303. p = buf + 2;
  304. while (list_size > 0 && our_size > 1) {
  305. curve_info = mbedtls_ecp_curve_info_from_tls_id((p[0] << 8) | p[1]);
  306. if (curve_info != NULL) {
  307. *curves++ = curve_info;
  308. our_size--;
  309. }
  310. list_size -= 2;
  311. p += 2;
  312. }
  313. return 0;
  314. }
  315. MBEDTLS_CHECK_RETURN_CRITICAL
  316. static int ssl_parse_supported_point_formats(mbedtls_ssl_context *ssl,
  317. const unsigned char *buf,
  318. size_t len)
  319. {
  320. size_t list_size;
  321. const unsigned char *p;
  322. if (len == 0 || (size_t) (buf[0] + 1) != len) {
  323. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  324. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  325. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  326. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  327. }
  328. list_size = buf[0];
  329. p = buf + 1;
  330. while (list_size > 0) {
  331. if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
  332. p[0] == MBEDTLS_ECP_PF_COMPRESSED) {
  333. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
  334. ssl->handshake->ecdh_ctx.point_format = p[0];
  335. #endif
  336. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  337. ssl->handshake->ecjpake_ctx.point_format = p[0];
  338. #endif
  339. MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
  340. return 0;
  341. }
  342. list_size--;
  343. p++;
  344. }
  345. return 0;
  346. }
  347. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
  348. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  349. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  350. MBEDTLS_CHECK_RETURN_CRITICAL
  351. static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
  352. const unsigned char *buf,
  353. size_t len)
  354. {
  355. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  356. if (mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) {
  357. MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
  358. return 0;
  359. }
  360. if ((ret = mbedtls_ecjpake_read_round_one(&ssl->handshake->ecjpake_ctx,
  361. buf, len)) != 0) {
  362. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_one", ret);
  363. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  364. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  365. return ret;
  366. }
  367. /* Only mark the extension as OK when we're sure it is */
  368. ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
  369. return 0;
  370. }
  371. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  372. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  373. MBEDTLS_CHECK_RETURN_CRITICAL
  374. static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context *ssl,
  375. const unsigned char *buf,
  376. size_t len)
  377. {
  378. if (len != 1 || buf[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID) {
  379. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  380. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  381. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  382. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  383. }
  384. ssl->session_negotiate->mfl_code = buf[0];
  385. return 0;
  386. }
  387. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  388. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  389. MBEDTLS_CHECK_RETURN_CRITICAL
  390. static int ssl_parse_cid_ext(mbedtls_ssl_context *ssl,
  391. const unsigned char *buf,
  392. size_t len)
  393. {
  394. size_t peer_cid_len;
  395. /* CID extension only makes sense in DTLS */
  396. if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  397. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  398. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  399. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  400. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  401. }
  402. /*
  403. * Quoting draft-ietf-tls-dtls-connection-id-05
  404. * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
  405. *
  406. * struct {
  407. * opaque cid<0..2^8-1>;
  408. * } ConnectionId;
  409. */
  410. if (len < 1) {
  411. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  412. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  413. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  414. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  415. }
  416. peer_cid_len = *buf++;
  417. len--;
  418. if (len != peer_cid_len) {
  419. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  420. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  421. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  422. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  423. }
  424. /* Ignore CID if the user has disabled its use. */
  425. if (ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
  426. /* Leave ssl->handshake->cid_in_use in its default
  427. * value of MBEDTLS_SSL_CID_DISABLED. */
  428. MBEDTLS_SSL_DEBUG_MSG(3, ("Client sent CID extension, but CID disabled"));
  429. return 0;
  430. }
  431. if (peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX) {
  432. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  433. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  434. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  435. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  436. }
  437. ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
  438. ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
  439. memcpy(ssl->handshake->peer_cid, buf, peer_cid_len);
  440. MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated"));
  441. MBEDTLS_SSL_DEBUG_BUF(3, "Client CID", buf, peer_cid_len);
  442. return 0;
  443. }
  444. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  445. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  446. MBEDTLS_CHECK_RETURN_CRITICAL
  447. static int ssl_parse_truncated_hmac_ext(mbedtls_ssl_context *ssl,
  448. const unsigned char *buf,
  449. size_t len)
  450. {
  451. if (len != 0) {
  452. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  453. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  454. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  455. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  456. }
  457. ((void) buf);
  458. if (ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED) {
  459. ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
  460. }
  461. return 0;
  462. }
  463. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  464. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  465. MBEDTLS_CHECK_RETURN_CRITICAL
  466. static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
  467. const unsigned char *buf,
  468. size_t len)
  469. {
  470. if (len != 0) {
  471. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  472. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  473. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  474. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  475. }
  476. ((void) buf);
  477. if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
  478. ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0) {
  479. ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
  480. }
  481. return 0;
  482. }
  483. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  484. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  485. MBEDTLS_CHECK_RETURN_CRITICAL
  486. static int ssl_parse_extended_ms_ext(mbedtls_ssl_context *ssl,
  487. const unsigned char *buf,
  488. size_t len)
  489. {
  490. if (len != 0) {
  491. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  492. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  493. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  494. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  495. }
  496. ((void) buf);
  497. if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED &&
  498. ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0) {
  499. ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
  500. }
  501. return 0;
  502. }
  503. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  504. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  505. MBEDTLS_CHECK_RETURN_CRITICAL
  506. static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
  507. unsigned char *buf,
  508. size_t len)
  509. {
  510. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  511. mbedtls_ssl_session session;
  512. mbedtls_ssl_session_init(&session);
  513. if (ssl->conf->f_ticket_parse == NULL ||
  514. ssl->conf->f_ticket_write == NULL) {
  515. return 0;
  516. }
  517. /* Remember the client asked us to send a new ticket */
  518. ssl->handshake->new_session_ticket = 1;
  519. MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, len));
  520. if (len == 0) {
  521. return 0;
  522. }
  523. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  524. if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
  525. MBEDTLS_SSL_DEBUG_MSG(3, ("ticket rejected: renegotiating"));
  526. return 0;
  527. }
  528. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  529. /*
  530. * Failures are ok: just ignore the ticket and proceed.
  531. */
  532. if ((ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket, &session,
  533. buf, len)) != 0) {
  534. mbedtls_ssl_session_free(&session);
  535. if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
  536. MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
  537. } else if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
  538. MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
  539. } else {
  540. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_ticket_parse", ret);
  541. }
  542. return 0;
  543. }
  544. /*
  545. * Keep the session ID sent by the client, since we MUST send it back to
  546. * inform them we're accepting the ticket (RFC 5077 section 3.4)
  547. */
  548. session.id_len = ssl->session_negotiate->id_len;
  549. memcpy(&session.id, ssl->session_negotiate->id, session.id_len);
  550. mbedtls_ssl_session_free(ssl->session_negotiate);
  551. memcpy(ssl->session_negotiate, &session, sizeof(mbedtls_ssl_session));
  552. /* Zeroize instead of free as we copied the content */
  553. mbedtls_platform_zeroize(&session, sizeof(mbedtls_ssl_session));
  554. MBEDTLS_SSL_DEBUG_MSG(3, ("session successfully restored from ticket"));
  555. ssl->handshake->resume = 1;
  556. /* Don't send a new ticket after all, this one is OK */
  557. ssl->handshake->new_session_ticket = 0;
  558. return 0;
  559. }
  560. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  561. #if defined(MBEDTLS_SSL_ALPN)
  562. MBEDTLS_CHECK_RETURN_CRITICAL
  563. static int ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
  564. const unsigned char *buf, size_t len)
  565. {
  566. size_t list_len, cur_len, ours_len;
  567. const unsigned char *theirs, *start, *end;
  568. const char **ours;
  569. /* If ALPN not configured, just ignore the extension */
  570. if (ssl->conf->alpn_list == NULL) {
  571. return 0;
  572. }
  573. /*
  574. * opaque ProtocolName<1..2^8-1>;
  575. *
  576. * struct {
  577. * ProtocolName protocol_name_list<2..2^16-1>
  578. * } ProtocolNameList;
  579. */
  580. /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
  581. if (len < 4) {
  582. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  583. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  584. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  585. }
  586. list_len = (buf[0] << 8) | buf[1];
  587. if (list_len != len - 2) {
  588. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  589. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  590. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  591. }
  592. /*
  593. * Validate peer's list (lengths)
  594. */
  595. start = buf + 2;
  596. end = buf + len;
  597. for (theirs = start; theirs != end; theirs += cur_len) {
  598. cur_len = *theirs++;
  599. /* Current identifier must fit in list */
  600. if (cur_len > (size_t) (end - theirs)) {
  601. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  602. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  603. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  604. }
  605. /* Empty strings MUST NOT be included */
  606. if (cur_len == 0) {
  607. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  608. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  609. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  610. }
  611. }
  612. /*
  613. * Use our order of preference
  614. */
  615. for (ours = ssl->conf->alpn_list; *ours != NULL; ours++) {
  616. ours_len = strlen(*ours);
  617. for (theirs = start; theirs != end; theirs += cur_len) {
  618. cur_len = *theirs++;
  619. if (cur_len == ours_len &&
  620. memcmp(theirs, *ours, cur_len) == 0) {
  621. ssl->alpn_chosen = *ours;
  622. return 0;
  623. }
  624. }
  625. }
  626. /* If we get there, no match was found */
  627. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  628. MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL);
  629. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  630. }
  631. #endif /* MBEDTLS_SSL_ALPN */
  632. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  633. MBEDTLS_CHECK_RETURN_CRITICAL
  634. static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
  635. const unsigned char *buf,
  636. size_t len)
  637. {
  638. mbedtls_ssl_srtp_profile client_protection = MBEDTLS_TLS_SRTP_UNSET;
  639. size_t i, j;
  640. size_t profile_length;
  641. uint16_t mki_length;
  642. /*! 2 bytes for profile length and 1 byte for mki len */
  643. const size_t size_of_lengths = 3;
  644. /* If use_srtp is not configured, just ignore the extension */
  645. if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
  646. (ssl->conf->dtls_srtp_profile_list == NULL) ||
  647. (ssl->conf->dtls_srtp_profile_list_len == 0)) {
  648. return 0;
  649. }
  650. /* RFC5764 section 4.1.1
  651. * uint8 SRTPProtectionProfile[2];
  652. *
  653. * struct {
  654. * SRTPProtectionProfiles SRTPProtectionProfiles;
  655. * opaque srtp_mki<0..255>;
  656. * } UseSRTPData;
  657. * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
  658. */
  659. /*
  660. * Min length is 5: at least one protection profile(2 bytes)
  661. * and length(2 bytes) + srtp_mki length(1 byte)
  662. * Check here that we have at least 2 bytes of protection profiles length
  663. * and one of srtp_mki length
  664. */
  665. if (len < size_of_lengths) {
  666. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  667. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  668. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  669. }
  670. ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
  671. /* first 2 bytes are protection profile length(in bytes) */
  672. profile_length = (buf[0] << 8) | buf[1];
  673. buf += 2;
  674. /* The profile length cannot be bigger than input buffer size - lengths fields */
  675. if (profile_length > len - size_of_lengths ||
  676. profile_length % 2 != 0) { /* profiles are 2 bytes long, so the length must be even */
  677. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  678. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  679. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  680. }
  681. /*
  682. * parse the extension list values are defined in
  683. * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
  684. */
  685. for (j = 0; j < profile_length; j += 2) {
  686. uint16_t protection_profile_value = buf[j] << 8 | buf[j + 1];
  687. client_protection = mbedtls_ssl_check_srtp_profile_value(protection_profile_value);
  688. if (client_protection != MBEDTLS_TLS_SRTP_UNSET) {
  689. MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
  690. mbedtls_ssl_get_srtp_profile_as_string(
  691. client_protection)));
  692. } else {
  693. continue;
  694. }
  695. /* check if suggested profile is in our list */
  696. for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
  697. if (client_protection == ssl->conf->dtls_srtp_profile_list[i]) {
  698. ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
  699. MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
  700. mbedtls_ssl_get_srtp_profile_as_string(
  701. client_protection)));
  702. break;
  703. }
  704. }
  705. if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_TLS_SRTP_UNSET) {
  706. break;
  707. }
  708. }
  709. buf += profile_length; /* buf points to the mki length */
  710. mki_length = *buf;
  711. buf++;
  712. if (mki_length > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ||
  713. mki_length + profile_length + size_of_lengths != len) {
  714. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  715. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  716. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  717. }
  718. /* Parse the mki only if present and mki is supported locally */
  719. if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
  720. mki_length > 0) {
  721. ssl->dtls_srtp_info.mki_len = mki_length;
  722. memcpy(ssl->dtls_srtp_info.mki_value, buf, mki_length);
  723. MBEDTLS_SSL_DEBUG_BUF(3, "using mki", ssl->dtls_srtp_info.mki_value,
  724. ssl->dtls_srtp_info.mki_len);
  725. }
  726. return 0;
  727. }
  728. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  729. /*
  730. * Auxiliary functions for ServerHello parsing and related actions
  731. */
  732. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  733. /*
  734. * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
  735. */
  736. #if defined(MBEDTLS_ECDSA_C)
  737. MBEDTLS_CHECK_RETURN_CRITICAL
  738. static int ssl_check_key_curve(mbedtls_pk_context *pk,
  739. const mbedtls_ecp_curve_info **curves)
  740. {
  741. const mbedtls_ecp_curve_info **crv = curves;
  742. mbedtls_ecp_group_id grp_id = mbedtls_pk_ec(*pk)->grp.id;
  743. while (*crv != NULL) {
  744. if ((*crv)->grp_id == grp_id) {
  745. return 0;
  746. }
  747. crv++;
  748. }
  749. return -1;
  750. }
  751. #endif /* MBEDTLS_ECDSA_C */
  752. /*
  753. * Try picking a certificate for this ciphersuite,
  754. * return 0 on success and -1 on failure.
  755. */
  756. MBEDTLS_CHECK_RETURN_CRITICAL
  757. static int ssl_pick_cert(mbedtls_ssl_context *ssl,
  758. const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
  759. {
  760. mbedtls_ssl_key_cert *cur, *list, *fallback = NULL;
  761. mbedtls_pk_type_t pk_alg =
  762. mbedtls_ssl_get_ciphersuite_sig_pk_alg(ciphersuite_info);
  763. uint32_t flags;
  764. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  765. if (ssl->handshake->sni_key_cert != NULL) {
  766. list = ssl->handshake->sni_key_cert;
  767. } else
  768. #endif
  769. list = ssl->conf->key_cert;
  770. if (pk_alg == MBEDTLS_PK_NONE) {
  771. return 0;
  772. }
  773. MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite requires certificate"));
  774. if (list == NULL) {
  775. MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
  776. return -1;
  777. }
  778. for (cur = list; cur != NULL; cur = cur->next) {
  779. flags = 0;
  780. MBEDTLS_SSL_DEBUG_CRT(3, "candidate certificate chain, certificate",
  781. cur->cert);
  782. if (!mbedtls_pk_can_do(&cur->cert->pk, pk_alg)) {
  783. MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: key type"));
  784. continue;
  785. }
  786. /*
  787. * This avoids sending the client a cert it'll reject based on
  788. * keyUsage or other extensions.
  789. *
  790. * It also allows the user to provision different certificates for
  791. * different uses based on keyUsage, eg if they want to avoid signing
  792. * and decrypting with the same RSA key.
  793. */
  794. if (mbedtls_ssl_check_cert_usage(cur->cert, ciphersuite_info,
  795. MBEDTLS_SSL_IS_SERVER, &flags) != 0) {
  796. MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
  797. "(extended) key usage extension"));
  798. continue;
  799. }
  800. #if defined(MBEDTLS_ECDSA_C)
  801. if (pk_alg == MBEDTLS_PK_ECDSA &&
  802. ssl_check_key_curve(&cur->cert->pk, ssl->handshake->curves) != 0) {
  803. MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: elliptic curve"));
  804. continue;
  805. }
  806. #endif
  807. /*
  808. * Try to select a SHA-1 certificate for pre-1.2 clients, but still
  809. * present them a SHA-higher cert rather than failing if it's the only
  810. * one we got that satisfies the other conditions.
  811. */
  812. if (ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 &&
  813. cur->cert->sig_md != MBEDTLS_MD_SHA1) {
  814. if (fallback == NULL) {
  815. fallback = cur;
  816. }
  817. {
  818. MBEDTLS_SSL_DEBUG_MSG(3, ("certificate not preferred: "
  819. "sha-2 with pre-TLS 1.2 client"));
  820. continue;
  821. }
  822. }
  823. /* If we get there, we got a winner */
  824. break;
  825. }
  826. if (cur == NULL) {
  827. cur = fallback;
  828. }
  829. /* Do not update ssl->handshake->key_cert unless there is a match */
  830. if (cur != NULL) {
  831. ssl->handshake->key_cert = cur;
  832. MBEDTLS_SSL_DEBUG_CRT(3, "selected certificate chain, certificate",
  833. ssl->handshake->key_cert->cert);
  834. return 0;
  835. }
  836. return -1;
  837. }
  838. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  839. /*
  840. * Check if a given ciphersuite is suitable for use with our config/keys/etc
  841. * Sets ciphersuite_info only if the suite matches.
  842. */
  843. MBEDTLS_CHECK_RETURN_CRITICAL
  844. static int ssl_ciphersuite_match(mbedtls_ssl_context *ssl, int suite_id,
  845. const mbedtls_ssl_ciphersuite_t **ciphersuite_info)
  846. {
  847. const mbedtls_ssl_ciphersuite_t *suite_info;
  848. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  849. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  850. mbedtls_pk_type_t sig_type;
  851. #endif
  852. suite_info = mbedtls_ssl_ciphersuite_from_id(suite_id);
  853. if (suite_info == NULL) {
  854. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  855. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  856. }
  857. MBEDTLS_SSL_DEBUG_MSG(3, ("trying ciphersuite: %#04x (%s)",
  858. (unsigned int) suite_id, suite_info->name));
  859. if (suite_info->min_minor_ver > ssl->minor_ver ||
  860. suite_info->max_minor_ver < ssl->minor_ver) {
  861. MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: version"));
  862. return 0;
  863. }
  864. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  865. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  866. (suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS)) {
  867. return 0;
  868. }
  869. #endif
  870. #if defined(MBEDTLS_ARC4_C)
  871. if (ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
  872. suite_info->cipher == MBEDTLS_CIPHER_ARC4_128) {
  873. MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: rc4"));
  874. return 0;
  875. }
  876. #endif
  877. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  878. if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
  879. (ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK) == 0) {
  880. MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: ecjpake "
  881. "not configured or ext missing"));
  882. return 0;
  883. }
  884. #endif
  885. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
  886. if (mbedtls_ssl_ciphersuite_uses_ec(suite_info) &&
  887. (ssl->handshake->curves == NULL ||
  888. ssl->handshake->curves[0] == NULL)) {
  889. MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: "
  890. "no common elliptic curve"));
  891. return 0;
  892. }
  893. #endif
  894. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  895. /* If the ciphersuite requires a pre-shared key and we don't
  896. * have one, skip it now rather than failing later */
  897. if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
  898. ssl_conf_has_psk_or_cb(ssl->conf) == 0) {
  899. MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: no pre-shared key"));
  900. return 0;
  901. }
  902. #endif
  903. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  904. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  905. /* If the ciphersuite requires signing, check whether
  906. * a suitable hash algorithm is present. */
  907. if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
  908. sig_type = mbedtls_ssl_get_ciphersuite_sig_alg(suite_info);
  909. if (sig_type != MBEDTLS_PK_NONE &&
  910. mbedtls_ssl_sig_hash_set_find(&ssl->handshake->hash_algs,
  911. sig_type) == MBEDTLS_MD_NONE) {
  912. MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: no suitable hash algorithm "
  913. "for signature algorithm %u", (unsigned) sig_type));
  914. return 0;
  915. }
  916. }
  917. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
  918. MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  919. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  920. /*
  921. * Final check: if ciphersuite requires us to have a
  922. * certificate/key of a particular type:
  923. * - select the appropriate certificate if we have one, or
  924. * - try the next ciphersuite if we don't
  925. * This must be done last since we modify the key_cert list.
  926. */
  927. if (ssl_pick_cert(ssl, suite_info) != 0) {
  928. MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: "
  929. "no suitable certificate"));
  930. return 0;
  931. }
  932. #endif
  933. *ciphersuite_info = suite_info;
  934. return 0;
  935. }
  936. #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
  937. MBEDTLS_CHECK_RETURN_CRITICAL
  938. static int ssl_parse_client_hello_v2(mbedtls_ssl_context *ssl)
  939. {
  940. int ret, got_common_suite;
  941. unsigned int i, j;
  942. size_t n;
  943. unsigned int ciph_len, sess_len, chal_len;
  944. unsigned char *buf, *p;
  945. const int *ciphersuites;
  946. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  947. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello v2"));
  948. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  949. if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
  950. MBEDTLS_SSL_DEBUG_MSG(1, ("client hello v2 illegal for renegotiation"));
  951. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  952. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  953. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  954. }
  955. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  956. buf = ssl->in_hdr;
  957. MBEDTLS_SSL_DEBUG_BUF(4, "record header", buf, 5);
  958. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v2, message type: %d",
  959. buf[2]));
  960. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v2, message len.: %d",
  961. ((buf[0] & 0x7F) << 8) | buf[1]));
  962. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v2, max. version: [%d:%d]",
  963. buf[3], buf[4]));
  964. /*
  965. * SSLv2 Client Hello
  966. *
  967. * Record layer:
  968. * 0 . 1 message length
  969. *
  970. * SSL layer:
  971. * 2 . 2 message type
  972. * 3 . 4 protocol version
  973. */
  974. if (buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO ||
  975. buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3) {
  976. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  977. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  978. }
  979. n = ((buf[0] << 8) | buf[1]) & 0x7FFF;
  980. if (n < 17 || n > 512) {
  981. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  982. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  983. }
  984. ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
  985. ssl->minor_ver = (buf[4] <= ssl->conf->max_minor_ver)
  986. ? buf[4] : ssl->conf->max_minor_ver;
  987. if (ssl->minor_ver < ssl->conf->min_minor_ver) {
  988. MBEDTLS_SSL_DEBUG_MSG(1, ("client only supports ssl smaller than minimum"
  989. " [%d:%d] < [%d:%d]",
  990. ssl->major_ver, ssl->minor_ver,
  991. ssl->conf->min_major_ver, ssl->conf->min_minor_ver));
  992. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  993. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
  994. return MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION;
  995. }
  996. ssl->handshake->max_major_ver = buf[3];
  997. ssl->handshake->max_minor_ver = buf[4];
  998. if ((ret = mbedtls_ssl_fetch_input(ssl, 2 + n)) != 0) {
  999. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
  1000. return ret;
  1001. }
  1002. ssl->handshake->update_checksum(ssl, buf + 2, n);
  1003. buf = ssl->in_msg;
  1004. n = ssl->in_left - 5;
  1005. /*
  1006. * 0 . 1 ciphersuitelist length
  1007. * 2 . 3 session id length
  1008. * 4 . 5 challenge length
  1009. * 6 . .. ciphersuitelist
  1010. * .. . .. session id
  1011. * .. . .. challenge
  1012. */
  1013. MBEDTLS_SSL_DEBUG_BUF(4, "record contents", buf, n);
  1014. ciph_len = (buf[0] << 8) | buf[1];
  1015. sess_len = (buf[2] << 8) | buf[3];
  1016. chal_len = (buf[4] << 8) | buf[5];
  1017. MBEDTLS_SSL_DEBUG_MSG(3, ("ciph_len: %u, sess_len: %u, chal_len: %u",
  1018. ciph_len, sess_len, chal_len));
  1019. /*
  1020. * Make sure each parameter length is valid
  1021. */
  1022. if (ciph_len < 3 || (ciph_len % 3) != 0) {
  1023. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1024. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1025. }
  1026. if (sess_len > 32) {
  1027. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1028. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1029. }
  1030. if (chal_len < 8 || chal_len > 32) {
  1031. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1032. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1033. }
  1034. if (n != 6 + ciph_len + sess_len + chal_len) {
  1035. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1036. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1037. }
  1038. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, ciphersuitelist",
  1039. buf + 6, ciph_len);
  1040. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
  1041. buf + 6 + ciph_len, sess_len);
  1042. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, challenge",
  1043. buf + 6 + ciph_len + sess_len, chal_len);
  1044. p = buf + 6 + ciph_len;
  1045. ssl->session_negotiate->id_len = sess_len;
  1046. memset(ssl->session_negotiate->id, 0,
  1047. sizeof(ssl->session_negotiate->id));
  1048. memcpy(ssl->session_negotiate->id, p, ssl->session_negotiate->id_len);
  1049. p += sess_len;
  1050. memset(ssl->handshake->randbytes, 0, 64);
  1051. memcpy(ssl->handshake->randbytes + 32 - chal_len, p, chal_len);
  1052. /*
  1053. * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
  1054. */
  1055. for (i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3) {
  1056. if (p[0] == 0 && p[1] == 0 && p[2] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO) {
  1057. MBEDTLS_SSL_DEBUG_MSG(3, ("received TLS_EMPTY_RENEGOTIATION_INFO "));
  1058. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1059. if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
  1060. MBEDTLS_SSL_DEBUG_MSG(1, ("received RENEGOTIATION SCSV "
  1061. "during renegotiation"));
  1062. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1063. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1064. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1065. }
  1066. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1067. ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
  1068. break;
  1069. }
  1070. }
  1071. #if defined(MBEDTLS_SSL_FALLBACK_SCSV)
  1072. for (i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3) {
  1073. if (p[0] == 0 &&
  1074. MBEDTLS_GET_UINT16_BE(p, 1) != MBEDTLS_SSL_FALLBACK_SCSV_VALUE) {
  1075. MBEDTLS_SSL_DEBUG_MSG(3, ("received FALLBACK_SCSV"));
  1076. if (ssl->minor_ver < ssl->conf->max_minor_ver) {
  1077. MBEDTLS_SSL_DEBUG_MSG(1, ("inapropriate fallback"));
  1078. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1079. MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK);
  1080. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1081. }
  1082. break;
  1083. }
  1084. }
  1085. #endif /* MBEDTLS_SSL_FALLBACK_SCSV */
  1086. got_common_suite = 0;
  1087. ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
  1088. ciphersuite_info = NULL;
  1089. #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
  1090. for (j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3) {
  1091. for (i = 0; ciphersuites[i] != 0; i++) {
  1092. if (p[0] != 0 ||
  1093. MBEDTLS_GET_UINT16_BE(p, 1) != ciphersuites[i]) {
  1094. continue;
  1095. }
  1096. got_common_suite = 1;
  1097. if ((ret = ssl_ciphersuite_match(ssl, ciphersuites[i],
  1098. &ciphersuite_info)) != 0) {
  1099. return ret;
  1100. }
  1101. if (ciphersuite_info != NULL) {
  1102. goto have_ciphersuite_v2;
  1103. }
  1104. }
  1105. }
  1106. #else
  1107. for (i = 0; ciphersuites[i] != 0; i++) {
  1108. for (j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3) {
  1109. if (p[0] != 0 ||
  1110. MBEDTLS_GET_UINT16_BE(p, 1) != ciphersuites[i]) {
  1111. continue;
  1112. }
  1113. got_common_suite = 1;
  1114. if ((ret = ssl_ciphersuite_match(ssl, ciphersuites[i],
  1115. &ciphersuite_info)) != 0) {
  1116. return ret;
  1117. }
  1118. if (ciphersuite_info != NULL) {
  1119. goto have_ciphersuite_v2;
  1120. }
  1121. }
  1122. }
  1123. #endif
  1124. if (got_common_suite) {
  1125. MBEDTLS_SSL_DEBUG_MSG(1, ("got ciphersuites in common, "
  1126. "but none of them usable"));
  1127. return MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE;
  1128. } else {
  1129. MBEDTLS_SSL_DEBUG_MSG(1, ("got no ciphersuites in common"));
  1130. return MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN;
  1131. }
  1132. have_ciphersuite_v2:
  1133. MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %s", ciphersuite_info->name));
  1134. ssl->session_negotiate->ciphersuite = ciphersuites[i];
  1135. ssl->handshake->ciphersuite_info = ciphersuite_info;
  1136. /*
  1137. * SSLv2 Client Hello relevant renegotiation security checks
  1138. */
  1139. if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  1140. ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
  1141. MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation, breaking off handshake"));
  1142. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1143. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1144. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1145. }
  1146. ssl->in_left = 0;
  1147. ssl->state++;
  1148. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello v2"));
  1149. return 0;
  1150. }
  1151. #endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
  1152. /* This function doesn't alert on errors that happen early during
  1153. ClientHello parsing because they might indicate that the client is
  1154. not talking SSL/TLS at all and would not understand our alert. */
  1155. MBEDTLS_CHECK_RETURN_CRITICAL
  1156. static int ssl_parse_client_hello(mbedtls_ssl_context *ssl)
  1157. {
  1158. int ret, got_common_suite;
  1159. size_t i, j;
  1160. size_t ciph_offset, comp_offset, ext_offset;
  1161. size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
  1162. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1163. size_t cookie_offset, cookie_len;
  1164. #endif
  1165. unsigned char *buf, *p, *ext;
  1166. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1167. int renegotiation_info_seen = 0;
  1168. #endif
  1169. int handshake_failure = 0;
  1170. const int *ciphersuites;
  1171. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  1172. int major, minor;
  1173. /* If there is no signature-algorithm extension present,
  1174. * we need to fall back to the default values for allowed
  1175. * signature-hash pairs. */
  1176. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  1177. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  1178. int sig_hash_alg_ext_present = 0;
  1179. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
  1180. MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  1181. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
  1182. int renegotiating = 0;
  1183. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  1184. read_record_header:
  1185. #endif
  1186. /*
  1187. * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
  1188. * otherwise read it ourselves manually in order to support SSLv2
  1189. * ClientHello, which doesn't use the same record layer format.
  1190. */
  1191. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1192. if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
  1193. renegotiating = 1;
  1194. }
  1195. #endif
  1196. if (!renegotiating) {
  1197. if ((ret = mbedtls_ssl_fetch_input(ssl, 5)) != 0) {
  1198. /* No alert on a read error. */
  1199. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
  1200. return ret;
  1201. }
  1202. }
  1203. buf = ssl->in_hdr;
  1204. #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
  1205. int is_dtls = 0;
  1206. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1207. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  1208. is_dtls = 1;
  1209. }
  1210. #endif
  1211. if (!is_dtls) {
  1212. if ((buf[0] & 0x80) != 0) {
  1213. return ssl_parse_client_hello_v2(ssl);
  1214. }
  1215. }
  1216. #endif
  1217. MBEDTLS_SSL_DEBUG_BUF(4, "record header", buf, mbedtls_ssl_in_hdr_len(ssl));
  1218. /*
  1219. * SSLv3/TLS Client Hello
  1220. *
  1221. * Record layer:
  1222. * 0 . 0 message type
  1223. * 1 . 2 protocol version
  1224. * 3 . 11 DTLS: epoch + record sequence number
  1225. * 3 . 4 message length
  1226. */
  1227. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, message type: %d",
  1228. buf[0]));
  1229. if (buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE) {
  1230. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1231. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1232. }
  1233. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, message len.: %d",
  1234. (ssl->in_len[0] << 8) | ssl->in_len[1]));
  1235. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, protocol version: [%d:%d]",
  1236. buf[1], buf[2]));
  1237. mbedtls_ssl_read_version(&major, &minor, ssl->conf->transport, buf + 1);
  1238. /* According to RFC 5246 Appendix E.1, the version here is typically
  1239. * "{03,00}, the lowest version number supported by the client, [or] the
  1240. * value of ClientHello.client_version", so the only meaningful check here
  1241. * is the major version shouldn't be less than 3 */
  1242. if (major < MBEDTLS_SSL_MAJOR_VERSION_3) {
  1243. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1244. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1245. }
  1246. /* For DTLS if this is the initial handshake, remember the client sequence
  1247. * number to use it in our next message (RFC 6347 4.2.1) */
  1248. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1249. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
  1250. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1251. && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
  1252. #endif
  1253. ) {
  1254. /* Epoch should be 0 for initial handshakes */
  1255. if (ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0) {
  1256. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1257. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1258. }
  1259. memcpy(ssl->cur_out_ctr + 2, ssl->in_ctr + 2, 6);
  1260. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  1261. if (mbedtls_ssl_dtls_replay_check(ssl) != 0) {
  1262. MBEDTLS_SSL_DEBUG_MSG(1, ("replayed record, discarding"));
  1263. ssl->next_record_offset = 0;
  1264. ssl->in_left = 0;
  1265. goto read_record_header;
  1266. }
  1267. /* No MAC to check yet, so we can update right now */
  1268. mbedtls_ssl_dtls_replay_update(ssl);
  1269. #endif
  1270. }
  1271. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1272. msg_len = (ssl->in_len[0] << 8) | ssl->in_len[1];
  1273. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1274. if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
  1275. /* Set by mbedtls_ssl_read_record() */
  1276. msg_len = ssl->in_hslen;
  1277. } else
  1278. #endif
  1279. {
  1280. if (msg_len > MBEDTLS_SSL_IN_CONTENT_LEN) {
  1281. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1282. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1283. }
  1284. if ((ret = mbedtls_ssl_fetch_input(ssl,
  1285. mbedtls_ssl_in_hdr_len(ssl) + msg_len)) != 0) {
  1286. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
  1287. return ret;
  1288. }
  1289. /* Done reading this record, get ready for the next one */
  1290. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1291. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  1292. ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len(ssl);
  1293. } else
  1294. #endif
  1295. ssl->in_left = 0;
  1296. }
  1297. buf = ssl->in_msg;
  1298. MBEDTLS_SSL_DEBUG_BUF(4, "record contents", buf, msg_len);
  1299. ssl->handshake->update_checksum(ssl, buf, msg_len);
  1300. /*
  1301. * Handshake layer:
  1302. * 0 . 0 handshake type
  1303. * 1 . 3 handshake length
  1304. * 4 . 5 DTLS only: message sequence number
  1305. * 6 . 8 DTLS only: fragment offset
  1306. * 9 . 11 DTLS only: fragment length
  1307. */
  1308. if (msg_len < mbedtls_ssl_hs_hdr_len(ssl)) {
  1309. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1310. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1311. }
  1312. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, handshake type: %d", buf[0]));
  1313. if (buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO) {
  1314. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1315. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1316. }
  1317. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, handshake len.: %d",
  1318. (buf[1] << 16) | (buf[2] << 8) | buf[3]));
  1319. if (buf[1] != 0) {
  1320. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message: %u != 0",
  1321. (unsigned) buf[1]));
  1322. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1323. }
  1324. /* We don't support fragmentation of ClientHello (yet?) */
  1325. if (msg_len != mbedtls_ssl_hs_hdr_len(ssl) + ((buf[2] << 8) | buf[3])) {
  1326. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message: %u != %u + %u",
  1327. (unsigned) msg_len,
  1328. (unsigned) mbedtls_ssl_hs_hdr_len(ssl),
  1329. (unsigned) (buf[2] << 8) | buf[3]));
  1330. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1331. }
  1332. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1333. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  1334. /*
  1335. * Copy the client's handshake message_seq on initial handshakes,
  1336. * check sequence number on renego.
  1337. */
  1338. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1339. if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
  1340. /* This couldn't be done in ssl_prepare_handshake_record() */
  1341. unsigned int cli_msg_seq = (ssl->in_msg[4] << 8) |
  1342. ssl->in_msg[5];
  1343. if (cli_msg_seq != ssl->handshake->in_msg_seq) {
  1344. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message_seq: "
  1345. "%u (expected %u)", cli_msg_seq,
  1346. ssl->handshake->in_msg_seq));
  1347. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1348. }
  1349. ssl->handshake->in_msg_seq++;
  1350. } else
  1351. #endif
  1352. {
  1353. unsigned int cli_msg_seq = (ssl->in_msg[4] << 8) |
  1354. ssl->in_msg[5];
  1355. ssl->handshake->out_msg_seq = cli_msg_seq;
  1356. ssl->handshake->in_msg_seq = cli_msg_seq + 1;
  1357. }
  1358. /*
  1359. * For now we don't support fragmentation, so make sure
  1360. * fragment_offset == 0 and fragment_length == length
  1361. */
  1362. MBEDTLS_SSL_DEBUG_MSG(
  1363. 4, ("fragment_offset=%u fragment_length=%u length=%u",
  1364. (unsigned) (ssl->in_msg[6] << 16 | ssl->in_msg[7] << 8 | ssl->in_msg[8]),
  1365. (unsigned) (ssl->in_msg[9] << 16 | ssl->in_msg[10] << 8 | ssl->in_msg[11]),
  1366. (unsigned) (ssl->in_msg[1] << 16 | ssl->in_msg[2] << 8 | ssl->in_msg[3])));
  1367. if (ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
  1368. memcmp(ssl->in_msg + 1, ssl->in_msg + 9, 3) != 0) {
  1369. MBEDTLS_SSL_DEBUG_MSG(1, ("ClientHello fragmentation not supported"));
  1370. return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  1371. }
  1372. }
  1373. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1374. buf += mbedtls_ssl_hs_hdr_len(ssl);
  1375. msg_len -= mbedtls_ssl_hs_hdr_len(ssl);
  1376. /*
  1377. * ClientHello layer:
  1378. * 0 . 1 protocol version
  1379. * 2 . 33 random bytes (starting with 4 bytes of Unix time)
  1380. * 34 . 35 session id length (1 byte)
  1381. * 35 . 34+x session id
  1382. * 35+x . 35+x DTLS only: cookie length (1 byte)
  1383. * 36+x . .. DTLS only: cookie
  1384. * .. . .. ciphersuite list length (2 bytes)
  1385. * .. . .. ciphersuite list
  1386. * .. . .. compression alg. list length (1 byte)
  1387. * .. . .. compression alg. list
  1388. * .. . .. extensions length (2 bytes, optional)
  1389. * .. . .. extensions (optional)
  1390. */
  1391. /*
  1392. * Minimal length (with everything empty and extensions omitted) is
  1393. * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
  1394. * read at least up to session id length without worrying.
  1395. */
  1396. if (msg_len < 38) {
  1397. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1398. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1399. }
  1400. /*
  1401. * Check and save the protocol version
  1402. */
  1403. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, version", buf, 2);
  1404. mbedtls_ssl_read_version(&ssl->major_ver, &ssl->minor_ver,
  1405. ssl->conf->transport, buf);
  1406. ssl->handshake->max_major_ver = ssl->major_ver;
  1407. ssl->handshake->max_minor_ver = ssl->minor_ver;
  1408. if (ssl->major_ver < ssl->conf->min_major_ver ||
  1409. ssl->minor_ver < ssl->conf->min_minor_ver) {
  1410. MBEDTLS_SSL_DEBUG_MSG(1, ("client only supports ssl smaller than minimum"
  1411. " [%d:%d] < [%d:%d]",
  1412. ssl->major_ver, ssl->minor_ver,
  1413. ssl->conf->min_major_ver, ssl->conf->min_minor_ver));
  1414. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1415. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
  1416. return MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION;
  1417. }
  1418. if (ssl->major_ver > ssl->conf->max_major_ver) {
  1419. ssl->major_ver = ssl->conf->max_major_ver;
  1420. ssl->minor_ver = ssl->conf->max_minor_ver;
  1421. } else if (ssl->minor_ver > ssl->conf->max_minor_ver) {
  1422. ssl->minor_ver = ssl->conf->max_minor_ver;
  1423. }
  1424. /*
  1425. * Save client random (inc. Unix time)
  1426. */
  1427. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes", buf + 2, 32);
  1428. memcpy(ssl->handshake->randbytes, buf + 2, 32);
  1429. /*
  1430. * Check the session ID length and save session ID
  1431. */
  1432. sess_len = buf[34];
  1433. if (sess_len > sizeof(ssl->session_negotiate->id) ||
  1434. sess_len + 34 + 2 > msg_len) { /* 2 for cipherlist length field */
  1435. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1436. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1437. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1438. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1439. }
  1440. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id", buf + 35, sess_len);
  1441. ssl->session_negotiate->id_len = sess_len;
  1442. memset(ssl->session_negotiate->id, 0,
  1443. sizeof(ssl->session_negotiate->id));
  1444. memcpy(ssl->session_negotiate->id, buf + 35,
  1445. ssl->session_negotiate->id_len);
  1446. /*
  1447. * Check the cookie length and content
  1448. */
  1449. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1450. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  1451. cookie_offset = 35 + sess_len;
  1452. cookie_len = buf[cookie_offset];
  1453. if (cookie_offset + 1 + cookie_len + 2 > msg_len) {
  1454. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1455. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1456. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
  1457. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1458. }
  1459. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, cookie",
  1460. buf + cookie_offset + 1, cookie_len);
  1461. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  1462. if (ssl->conf->f_cookie_check != NULL
  1463. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1464. && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
  1465. #endif
  1466. ) {
  1467. if (ssl->conf->f_cookie_check(ssl->conf->p_cookie,
  1468. buf + cookie_offset + 1, cookie_len,
  1469. ssl->cli_id, ssl->cli_id_len) != 0) {
  1470. MBEDTLS_SSL_DEBUG_MSG(2, ("cookie verification failed"));
  1471. ssl->handshake->verify_cookie_len = 1;
  1472. } else {
  1473. MBEDTLS_SSL_DEBUG_MSG(2, ("cookie verification passed"));
  1474. ssl->handshake->verify_cookie_len = 0;
  1475. }
  1476. } else
  1477. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
  1478. {
  1479. /* We know we didn't send a cookie, so it should be empty */
  1480. if (cookie_len != 0) {
  1481. /* This may be an attacker's probe, so don't send an alert */
  1482. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1483. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1484. }
  1485. MBEDTLS_SSL_DEBUG_MSG(2, ("cookie verification skipped"));
  1486. }
  1487. /*
  1488. * Check the ciphersuitelist length (will be parsed later)
  1489. */
  1490. ciph_offset = cookie_offset + 1 + cookie_len;
  1491. } else
  1492. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1493. ciph_offset = 35 + sess_len;
  1494. ciph_len = (buf[ciph_offset + 0] << 8)
  1495. | (buf[ciph_offset + 1]);
  1496. if (ciph_len < 2 ||
  1497. ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
  1498. (ciph_len % 2) != 0) {
  1499. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1500. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1501. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1502. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1503. }
  1504. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, ciphersuitelist",
  1505. buf + ciph_offset + 2, ciph_len);
  1506. /*
  1507. * Check the compression algorithms length and pick one
  1508. */
  1509. comp_offset = ciph_offset + 2 + ciph_len;
  1510. comp_len = buf[comp_offset];
  1511. if (comp_len < 1 ||
  1512. comp_len > 16 ||
  1513. comp_len + comp_offset + 1 > msg_len) {
  1514. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1515. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1516. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1517. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1518. }
  1519. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, compression",
  1520. buf + comp_offset + 1, comp_len);
  1521. ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
  1522. #if defined(MBEDTLS_ZLIB_SUPPORT)
  1523. for (i = 0; i < comp_len; ++i) {
  1524. if (buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE) {
  1525. ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE;
  1526. break;
  1527. }
  1528. }
  1529. #endif
  1530. /* See comments in ssl_write_client_hello() */
  1531. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1532. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  1533. ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
  1534. }
  1535. #endif
  1536. /* Do not parse the extensions if the protocol is SSLv3 */
  1537. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  1538. if ((ssl->major_ver != 3) || (ssl->minor_ver != 0)) {
  1539. #endif
  1540. /*
  1541. * Check the extension length
  1542. */
  1543. ext_offset = comp_offset + 1 + comp_len;
  1544. if (msg_len > ext_offset) {
  1545. if (msg_len < ext_offset + 2) {
  1546. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1547. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1548. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1549. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1550. }
  1551. ext_len = (buf[ext_offset + 0] << 8)
  1552. | (buf[ext_offset + 1]);
  1553. if (msg_len != ext_offset + 2 + ext_len) {
  1554. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1555. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1556. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1557. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1558. }
  1559. } else {
  1560. ext_len = 0;
  1561. }
  1562. ext = buf + ext_offset + 2;
  1563. MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", ext, ext_len);
  1564. while (ext_len != 0) {
  1565. unsigned int ext_id;
  1566. unsigned int ext_size;
  1567. if (ext_len < 4) {
  1568. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1569. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1570. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1571. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1572. }
  1573. ext_id = ((ext[0] << 8) | (ext[1]));
  1574. ext_size = ((ext[2] << 8) | (ext[3]));
  1575. if (ext_size + 4 > ext_len) {
  1576. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1577. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1578. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1579. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1580. }
  1581. switch (ext_id) {
  1582. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  1583. case MBEDTLS_TLS_EXT_SERVERNAME:
  1584. MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
  1585. if (ssl->conf->f_sni == NULL) {
  1586. break;
  1587. }
  1588. ret = ssl_parse_servername_ext(ssl, ext + 4, ext_size);
  1589. if (ret != 0) {
  1590. return ret;
  1591. }
  1592. break;
  1593. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  1594. case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
  1595. MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
  1596. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1597. renegotiation_info_seen = 1;
  1598. #endif
  1599. ret = ssl_parse_renegotiation_info(ssl, ext + 4, ext_size);
  1600. if (ret != 0) {
  1601. return ret;
  1602. }
  1603. break;
  1604. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  1605. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  1606. case MBEDTLS_TLS_EXT_SIG_ALG:
  1607. MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
  1608. ret = ssl_parse_signature_algorithms_ext(ssl, ext + 4, ext_size);
  1609. if (ret != 0) {
  1610. return ret;
  1611. }
  1612. sig_hash_alg_ext_present = 1;
  1613. break;
  1614. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
  1615. MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  1616. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  1617. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1618. case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
  1619. MBEDTLS_SSL_DEBUG_MSG(3, ("found supported elliptic curves extension"));
  1620. ret = ssl_parse_supported_elliptic_curves(ssl, ext + 4, ext_size);
  1621. if (ret != 0) {
  1622. return ret;
  1623. }
  1624. break;
  1625. case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
  1626. MBEDTLS_SSL_DEBUG_MSG(3, ("found supported point formats extension"));
  1627. ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
  1628. ret = ssl_parse_supported_point_formats(ssl, ext + 4, ext_size);
  1629. if (ret != 0) {
  1630. return ret;
  1631. }
  1632. break;
  1633. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
  1634. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1635. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1636. case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
  1637. MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake kkpp extension"));
  1638. ret = ssl_parse_ecjpake_kkpp(ssl, ext + 4, ext_size);
  1639. if (ret != 0) {
  1640. return ret;
  1641. }
  1642. break;
  1643. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1644. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  1645. case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
  1646. MBEDTLS_SSL_DEBUG_MSG(3, ("found max fragment length extension"));
  1647. ret = ssl_parse_max_fragment_length_ext(ssl, ext + 4, ext_size);
  1648. if (ret != 0) {
  1649. return ret;
  1650. }
  1651. break;
  1652. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  1653. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  1654. case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
  1655. MBEDTLS_SSL_DEBUG_MSG(3, ("found truncated hmac extension"));
  1656. ret = ssl_parse_truncated_hmac_ext(ssl, ext + 4, ext_size);
  1657. if (ret != 0) {
  1658. return ret;
  1659. }
  1660. break;
  1661. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  1662. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1663. case MBEDTLS_TLS_EXT_CID:
  1664. MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
  1665. ret = ssl_parse_cid_ext(ssl, ext + 4, ext_size);
  1666. if (ret != 0) {
  1667. return ret;
  1668. }
  1669. break;
  1670. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  1671. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  1672. case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
  1673. MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt then mac extension"));
  1674. ret = ssl_parse_encrypt_then_mac_ext(ssl, ext + 4, ext_size);
  1675. if (ret != 0) {
  1676. return ret;
  1677. }
  1678. break;
  1679. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  1680. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  1681. case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
  1682. MBEDTLS_SSL_DEBUG_MSG(3, ("found extended master secret extension"));
  1683. ret = ssl_parse_extended_ms_ext(ssl, ext + 4, ext_size);
  1684. if (ret != 0) {
  1685. return ret;
  1686. }
  1687. break;
  1688. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  1689. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1690. case MBEDTLS_TLS_EXT_SESSION_TICKET:
  1691. MBEDTLS_SSL_DEBUG_MSG(3, ("found session ticket extension"));
  1692. ret = ssl_parse_session_ticket_ext(ssl, ext + 4, ext_size);
  1693. if (ret != 0) {
  1694. return ret;
  1695. }
  1696. break;
  1697. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  1698. #if defined(MBEDTLS_SSL_ALPN)
  1699. case MBEDTLS_TLS_EXT_ALPN:
  1700. MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
  1701. ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size);
  1702. if (ret != 0) {
  1703. return ret;
  1704. }
  1705. break;
  1706. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  1707. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  1708. case MBEDTLS_TLS_EXT_USE_SRTP:
  1709. MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
  1710. ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size);
  1711. if (ret != 0) {
  1712. return ret;
  1713. }
  1714. break;
  1715. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  1716. default:
  1717. MBEDTLS_SSL_DEBUG_MSG(3, ("unknown extension found: %u (ignoring)",
  1718. ext_id));
  1719. }
  1720. ext_len -= 4 + ext_size;
  1721. ext += 4 + ext_size;
  1722. }
  1723. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  1724. }
  1725. #endif
  1726. #if defined(MBEDTLS_SSL_FALLBACK_SCSV)
  1727. for (i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2) {
  1728. if (MBEDTLS_GET_UINT16_BE(p, 0) == MBEDTLS_SSL_FALLBACK_SCSV_VALUE) {
  1729. MBEDTLS_SSL_DEBUG_MSG(2, ("received FALLBACK_SCSV"));
  1730. if (ssl->minor_ver < ssl->conf->max_minor_ver) {
  1731. MBEDTLS_SSL_DEBUG_MSG(1, ("inapropriate fallback"));
  1732. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1733. MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK);
  1734. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1735. }
  1736. break;
  1737. }
  1738. }
  1739. #endif /* MBEDTLS_SSL_FALLBACK_SCSV */
  1740. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  1741. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  1742. /*
  1743. * Try to fall back to default hash SHA1 if the client
  1744. * hasn't provided any preferred signature-hash combinations.
  1745. */
  1746. if (sig_hash_alg_ext_present == 0) {
  1747. mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1;
  1748. if (mbedtls_ssl_check_sig_hash(ssl, md_default) != 0) {
  1749. md_default = MBEDTLS_MD_NONE;
  1750. }
  1751. mbedtls_ssl_sig_hash_set_const_hash(&ssl->handshake->hash_algs, md_default);
  1752. }
  1753. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
  1754. MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  1755. /*
  1756. * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
  1757. */
  1758. for (i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2) {
  1759. if (p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO) {
  1760. MBEDTLS_SSL_DEBUG_MSG(3, ("received TLS_EMPTY_RENEGOTIATION_INFO "));
  1761. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1762. if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
  1763. MBEDTLS_SSL_DEBUG_MSG(1, ("received RENEGOTIATION SCSV "
  1764. "during renegotiation"));
  1765. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1766. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1767. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1768. }
  1769. #endif
  1770. ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
  1771. break;
  1772. }
  1773. }
  1774. /*
  1775. * Renegotiation security checks
  1776. */
  1777. if (ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
  1778. ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
  1779. MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation, breaking off handshake"));
  1780. handshake_failure = 1;
  1781. }
  1782. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1783. else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  1784. ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
  1785. renegotiation_info_seen == 0) {
  1786. MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation_info extension missing (secure)"));
  1787. handshake_failure = 1;
  1788. } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  1789. ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  1790. ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
  1791. MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
  1792. handshake_failure = 1;
  1793. } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  1794. ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  1795. renegotiation_info_seen == 1) {
  1796. MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation_info extension present (legacy)"));
  1797. handshake_failure = 1;
  1798. }
  1799. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1800. if (handshake_failure == 1) {
  1801. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1802. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1803. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
  1804. }
  1805. /*
  1806. * Search for a matching ciphersuite
  1807. * (At the end because we need information from the EC-based extensions
  1808. * and certificate from the SNI callback triggered by the SNI extension.)
  1809. */
  1810. got_common_suite = 0;
  1811. ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
  1812. ciphersuite_info = NULL;
  1813. #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
  1814. for (j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2) {
  1815. for (i = 0; ciphersuites[i] != 0; i++) {
  1816. if (MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i]) {
  1817. continue;
  1818. }
  1819. got_common_suite = 1;
  1820. if ((ret = ssl_ciphersuite_match(ssl, ciphersuites[i],
  1821. &ciphersuite_info)) != 0) {
  1822. return ret;
  1823. }
  1824. if (ciphersuite_info != NULL) {
  1825. goto have_ciphersuite;
  1826. }
  1827. }
  1828. }
  1829. #else
  1830. for (i = 0; ciphersuites[i] != 0; i++) {
  1831. for (j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2) {
  1832. if (MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i]) {
  1833. continue;
  1834. }
  1835. got_common_suite = 1;
  1836. if ((ret = ssl_ciphersuite_match(ssl, ciphersuites[i],
  1837. &ciphersuite_info)) != 0) {
  1838. return ret;
  1839. }
  1840. if (ciphersuite_info != NULL) {
  1841. goto have_ciphersuite;
  1842. }
  1843. }
  1844. }
  1845. #endif
  1846. if (got_common_suite) {
  1847. MBEDTLS_SSL_DEBUG_MSG(1, ("got ciphersuites in common, "
  1848. "but none of them usable"));
  1849. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1850. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1851. return MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE;
  1852. } else {
  1853. MBEDTLS_SSL_DEBUG_MSG(1, ("got no ciphersuites in common"));
  1854. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1855. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1856. return MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN;
  1857. }
  1858. have_ciphersuite:
  1859. MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %s", ciphersuite_info->name));
  1860. ssl->session_negotiate->ciphersuite = ciphersuites[i];
  1861. ssl->handshake->ciphersuite_info = ciphersuite_info;
  1862. ssl->state++;
  1863. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1864. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  1865. mbedtls_ssl_recv_flight_completed(ssl);
  1866. }
  1867. #endif
  1868. /* Debugging-only output for testsuite */
  1869. #if defined(MBEDTLS_DEBUG_C) && \
  1870. defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  1871. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  1872. if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
  1873. mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg(ciphersuite_info);
  1874. if (sig_alg != MBEDTLS_PK_NONE) {
  1875. mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find(&ssl->handshake->hash_algs,
  1876. sig_alg);
  1877. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext: %d",
  1878. mbedtls_ssl_hash_from_md_alg(md_alg)));
  1879. } else {
  1880. MBEDTLS_SSL_DEBUG_MSG(3, ("no hash algorithm for signature algorithm "
  1881. "%u - should not happen", (unsigned) sig_alg));
  1882. }
  1883. }
  1884. #endif
  1885. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
  1886. return 0;
  1887. }
  1888. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  1889. static void ssl_write_truncated_hmac_ext(mbedtls_ssl_context *ssl,
  1890. unsigned char *buf,
  1891. size_t *olen)
  1892. {
  1893. unsigned char *p = buf;
  1894. if (ssl->session_negotiate->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED) {
  1895. *olen = 0;
  1896. return;
  1897. }
  1898. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding truncated hmac extension"));
  1899. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_TRUNCATED_HMAC, p, 0);
  1900. p += 2;
  1901. *p++ = 0x00;
  1902. *p++ = 0x00;
  1903. *olen = 4;
  1904. }
  1905. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  1906. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1907. static void ssl_write_cid_ext(mbedtls_ssl_context *ssl,
  1908. unsigned char *buf,
  1909. size_t *olen)
  1910. {
  1911. unsigned char *p = buf;
  1912. size_t ext_len;
  1913. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
  1914. *olen = 0;
  1915. /* Skip writing the extension if we don't want to use it or if
  1916. * the client hasn't offered it. */
  1917. if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED) {
  1918. return;
  1919. }
  1920. /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
  1921. * which is at most 255, so the increment cannot overflow. */
  1922. if (end < p || (size_t) (end - p) < (unsigned) (ssl->own_cid_len + 5)) {
  1923. MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
  1924. return;
  1925. }
  1926. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding CID extension"));
  1927. /*
  1928. * Quoting draft-ietf-tls-dtls-connection-id-05
  1929. * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
  1930. *
  1931. * struct {
  1932. * opaque cid<0..2^8-1>;
  1933. * } ConnectionId;
  1934. */
  1935. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_CID, p, 0);
  1936. p += 2;
  1937. ext_len = (size_t) ssl->own_cid_len + 1;
  1938. MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
  1939. p += 2;
  1940. *p++ = (uint8_t) ssl->own_cid_len;
  1941. memcpy(p, ssl->own_cid, ssl->own_cid_len);
  1942. *olen = ssl->own_cid_len + 5;
  1943. }
  1944. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  1945. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  1946. static void ssl_write_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
  1947. unsigned char *buf,
  1948. size_t *olen)
  1949. {
  1950. unsigned char *p = buf;
  1951. const mbedtls_ssl_ciphersuite_t *suite = NULL;
  1952. const mbedtls_cipher_info_t *cipher = NULL;
  1953. if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
  1954. ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_DISABLED;
  1955. }
  1956. /*
  1957. * RFC 7366: "If a server receives an encrypt-then-MAC request extension
  1958. * from a client and then selects a stream or Authenticated Encryption
  1959. * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
  1960. * encrypt-then-MAC response extension back to the client."
  1961. */
  1962. if ((suite = mbedtls_ssl_ciphersuite_from_id(
  1963. ssl->session_negotiate->ciphersuite)) == NULL ||
  1964. (cipher = mbedtls_cipher_info_from_type(suite->cipher)) == NULL ||
  1965. cipher->mode != MBEDTLS_MODE_CBC) {
  1966. ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_DISABLED;
  1967. }
  1968. if (ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) {
  1969. *olen = 0;
  1970. return;
  1971. }
  1972. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding encrypt then mac extension"));
  1973. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0);
  1974. p += 2;
  1975. *p++ = 0x00;
  1976. *p++ = 0x00;
  1977. *olen = 4;
  1978. }
  1979. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  1980. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  1981. static void ssl_write_extended_ms_ext(mbedtls_ssl_context *ssl,
  1982. unsigned char *buf,
  1983. size_t *olen)
  1984. {
  1985. unsigned char *p = buf;
  1986. if (ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
  1987. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
  1988. *olen = 0;
  1989. return;
  1990. }
  1991. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding extended master secret "
  1992. "extension"));
  1993. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0);
  1994. p += 2;
  1995. *p++ = 0x00;
  1996. *p++ = 0x00;
  1997. *olen = 4;
  1998. }
  1999. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  2000. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2001. static void ssl_write_session_ticket_ext(mbedtls_ssl_context *ssl,
  2002. unsigned char *buf,
  2003. size_t *olen)
  2004. {
  2005. unsigned char *p = buf;
  2006. if (ssl->handshake->new_session_ticket == 0) {
  2007. *olen = 0;
  2008. return;
  2009. }
  2010. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding session ticket extension"));
  2011. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0);
  2012. p += 2;
  2013. *p++ = 0x00;
  2014. *p++ = 0x00;
  2015. *olen = 4;
  2016. }
  2017. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  2018. static void ssl_write_renegotiation_ext(mbedtls_ssl_context *ssl,
  2019. unsigned char *buf,
  2020. size_t *olen)
  2021. {
  2022. unsigned char *p = buf;
  2023. if (ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION) {
  2024. *olen = 0;
  2025. return;
  2026. }
  2027. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, secure renegotiation extension"));
  2028. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0);
  2029. p += 2;
  2030. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  2031. if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
  2032. *p++ = 0x00;
  2033. *p++ = (ssl->verify_data_len * 2 + 1) & 0xFF;
  2034. *p++ = ssl->verify_data_len * 2 & 0xFF;
  2035. memcpy(p, ssl->peer_verify_data, ssl->verify_data_len);
  2036. p += ssl->verify_data_len;
  2037. memcpy(p, ssl->own_verify_data, ssl->verify_data_len);
  2038. p += ssl->verify_data_len;
  2039. } else
  2040. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  2041. {
  2042. *p++ = 0x00;
  2043. *p++ = 0x01;
  2044. *p++ = 0x00;
  2045. }
  2046. *olen = p - buf;
  2047. }
  2048. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  2049. static void ssl_write_max_fragment_length_ext(mbedtls_ssl_context *ssl,
  2050. unsigned char *buf,
  2051. size_t *olen)
  2052. {
  2053. unsigned char *p = buf;
  2054. if (ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE) {
  2055. *olen = 0;
  2056. return;
  2057. }
  2058. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, max_fragment_length extension"));
  2059. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0);
  2060. p += 2;
  2061. *p++ = 0x00;
  2062. *p++ = 1;
  2063. *p++ = ssl->session_negotiate->mfl_code;
  2064. *olen = 5;
  2065. }
  2066. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  2067. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  2068. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2069. static void ssl_write_supported_point_formats_ext(mbedtls_ssl_context *ssl,
  2070. unsigned char *buf,
  2071. size_t *olen)
  2072. {
  2073. unsigned char *p = buf;
  2074. ((void) ssl);
  2075. if ((ssl->handshake->cli_exts &
  2076. MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT) == 0) {
  2077. *olen = 0;
  2078. return;
  2079. }
  2080. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, supported_point_formats extension"));
  2081. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0);
  2082. p += 2;
  2083. *p++ = 0x00;
  2084. *p++ = 2;
  2085. *p++ = 1;
  2086. *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
  2087. *olen = 6;
  2088. }
  2089. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  2090. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2091. static void ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
  2092. unsigned char *buf,
  2093. size_t *olen)
  2094. {
  2095. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2096. unsigned char *p = buf;
  2097. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
  2098. size_t kkpp_len;
  2099. *olen = 0;
  2100. /* Skip costly computation if not needed */
  2101. if (ssl->handshake->ciphersuite_info->key_exchange !=
  2102. MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
  2103. return;
  2104. }
  2105. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, ecjpake kkpp extension"));
  2106. if (end - p < 4) {
  2107. MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
  2108. return;
  2109. }
  2110. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0);
  2111. p += 2;
  2112. ret = mbedtls_ecjpake_write_round_one(&ssl->handshake->ecjpake_ctx,
  2113. p + 2, end - p - 2, &kkpp_len,
  2114. ssl->conf->f_rng, ssl->conf->p_rng);
  2115. if (ret != 0) {
  2116. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_one", ret);
  2117. return;
  2118. }
  2119. MBEDTLS_PUT_UINT16_BE(kkpp_len, p, 0);
  2120. p += 2;
  2121. *olen = kkpp_len + 4;
  2122. }
  2123. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  2124. #if defined(MBEDTLS_SSL_ALPN)
  2125. static void ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
  2126. unsigned char *buf, size_t *olen)
  2127. {
  2128. if (ssl->alpn_chosen == NULL) {
  2129. *olen = 0;
  2130. return;
  2131. }
  2132. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding alpn extension"));
  2133. /*
  2134. * 0 . 1 ext identifier
  2135. * 2 . 3 ext length
  2136. * 4 . 5 protocol list length
  2137. * 6 . 6 protocol name length
  2138. * 7 . 7+n protocol name
  2139. */
  2140. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, buf, 0);
  2141. *olen = 7 + strlen(ssl->alpn_chosen);
  2142. MBEDTLS_PUT_UINT16_BE(*olen - 4, buf, 2);
  2143. MBEDTLS_PUT_UINT16_BE(*olen - 6, buf, 4);
  2144. buf[6] = MBEDTLS_BYTE_0(*olen - 7);
  2145. memcpy(buf + 7, ssl->alpn_chosen, *olen - 7);
  2146. }
  2147. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
  2148. #if defined(MBEDTLS_SSL_DTLS_SRTP) && defined(MBEDTLS_SSL_PROTO_DTLS)
  2149. static void ssl_write_use_srtp_ext(mbedtls_ssl_context *ssl,
  2150. unsigned char *buf,
  2151. size_t *olen)
  2152. {
  2153. size_t mki_len = 0, ext_len = 0;
  2154. uint16_t profile_value = 0;
  2155. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
  2156. *olen = 0;
  2157. if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
  2158. (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET)) {
  2159. return;
  2160. }
  2161. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding use_srtp extension"));
  2162. if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
  2163. mki_len = ssl->dtls_srtp_info.mki_len;
  2164. }
  2165. /* The extension total size is 9 bytes :
  2166. * - 2 bytes for the extension tag
  2167. * - 2 bytes for the total size
  2168. * - 2 bytes for the protection profile length
  2169. * - 2 bytes for the protection profile
  2170. * - 1 byte for the mki length
  2171. * + the actual mki length
  2172. * Check we have enough room in the output buffer */
  2173. if ((size_t) (end - buf) < mki_len + 9) {
  2174. MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
  2175. return;
  2176. }
  2177. /* extension */
  2178. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, buf, 0);
  2179. /*
  2180. * total length 5 and mki value: only one profile(2 bytes)
  2181. * and length(2 bytes) and srtp_mki )
  2182. */
  2183. ext_len = 5 + mki_len;
  2184. MBEDTLS_PUT_UINT16_BE(ext_len, buf, 2);
  2185. /* protection profile length: 2 */
  2186. buf[4] = 0x00;
  2187. buf[5] = 0x02;
  2188. profile_value = mbedtls_ssl_check_srtp_profile_value(
  2189. ssl->dtls_srtp_info.chosen_dtls_srtp_profile);
  2190. if (profile_value != MBEDTLS_TLS_SRTP_UNSET) {
  2191. MBEDTLS_PUT_UINT16_BE(profile_value, buf, 6);
  2192. } else {
  2193. MBEDTLS_SSL_DEBUG_MSG(1, ("use_srtp extension invalid profile"));
  2194. return;
  2195. }
  2196. buf[8] = mki_len & 0xFF;
  2197. memcpy(&buf[9], ssl->dtls_srtp_info.mki_value, mki_len);
  2198. *olen = 9 + mki_len;
  2199. }
  2200. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  2201. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  2202. MBEDTLS_CHECK_RETURN_CRITICAL
  2203. static int ssl_write_hello_verify_request(mbedtls_ssl_context *ssl)
  2204. {
  2205. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2206. unsigned char *p = ssl->out_msg + 4;
  2207. unsigned char *cookie_len_byte;
  2208. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello verify request"));
  2209. /*
  2210. * struct {
  2211. * ProtocolVersion server_version;
  2212. * opaque cookie<0..2^8-1>;
  2213. * } HelloVerifyRequest;
  2214. */
  2215. /* The RFC is not clear on this point, but sending the actual negotiated
  2216. * version looks like the most interoperable thing to do. */
  2217. mbedtls_ssl_write_version(ssl->major_ver, ssl->minor_ver,
  2218. ssl->conf->transport, p);
  2219. MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
  2220. p += 2;
  2221. /* If we get here, f_cookie_check is not null */
  2222. if (ssl->conf->f_cookie_write == NULL) {
  2223. MBEDTLS_SSL_DEBUG_MSG(1, ("inconsistent cookie callbacks"));
  2224. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2225. }
  2226. /* Skip length byte until we know the length */
  2227. cookie_len_byte = p++;
  2228. if ((ret = ssl->conf->f_cookie_write(ssl->conf->p_cookie,
  2229. &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN,
  2230. ssl->cli_id, ssl->cli_id_len)) != 0) {
  2231. MBEDTLS_SSL_DEBUG_RET(1, "f_cookie_write", ret);
  2232. return ret;
  2233. }
  2234. *cookie_len_byte = (unsigned char) (p - (cookie_len_byte + 1));
  2235. MBEDTLS_SSL_DEBUG_BUF(3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte);
  2236. ssl->out_msglen = p - ssl->out_msg;
  2237. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  2238. ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
  2239. ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
  2240. if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
  2241. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
  2242. return ret;
  2243. }
  2244. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  2245. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  2246. (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
  2247. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
  2248. return ret;
  2249. }
  2250. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  2251. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello verify request"));
  2252. return 0;
  2253. }
  2254. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
  2255. static void ssl_handle_id_based_session_resumption(mbedtls_ssl_context *ssl)
  2256. {
  2257. int ret;
  2258. mbedtls_ssl_session session_tmp;
  2259. mbedtls_ssl_session * const session = ssl->session_negotiate;
  2260. /* Resume is 0 by default, see ssl_handshake_init().
  2261. * It may be already set to 1 by ssl_parse_session_ticket_ext(). */
  2262. if (ssl->handshake->resume == 1) {
  2263. return;
  2264. }
  2265. if (session->id_len == 0) {
  2266. return;
  2267. }
  2268. if (ssl->conf->f_get_cache == NULL) {
  2269. return;
  2270. }
  2271. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  2272. if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
  2273. return;
  2274. }
  2275. #endif
  2276. mbedtls_ssl_session_init(&session_tmp);
  2277. session_tmp.id_len = session->id_len;
  2278. memcpy(session_tmp.id, session->id, session->id_len);
  2279. ret = ssl->conf->f_get_cache(ssl->conf->p_cache,
  2280. &session_tmp);
  2281. if (ret != 0) {
  2282. goto exit;
  2283. }
  2284. if (session->ciphersuite != session_tmp.ciphersuite ||
  2285. session->compression != session_tmp.compression) {
  2286. /* Mismatch between cached and negotiated session */
  2287. goto exit;
  2288. }
  2289. /* Move semantics */
  2290. mbedtls_ssl_session_free(session);
  2291. *session = session_tmp;
  2292. memset(&session_tmp, 0, sizeof(session_tmp));
  2293. MBEDTLS_SSL_DEBUG_MSG(3, ("session successfully restored from cache"));
  2294. ssl->handshake->resume = 1;
  2295. exit:
  2296. mbedtls_ssl_session_free(&session_tmp);
  2297. }
  2298. MBEDTLS_CHECK_RETURN_CRITICAL
  2299. static int ssl_write_server_hello(mbedtls_ssl_context *ssl)
  2300. {
  2301. #if defined(MBEDTLS_HAVE_TIME)
  2302. mbedtls_time_t t;
  2303. #endif
  2304. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2305. size_t olen, ext_len = 0, n;
  2306. unsigned char *buf, *p;
  2307. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
  2308. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  2309. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  2310. ssl->handshake->verify_cookie_len != 0) {
  2311. MBEDTLS_SSL_DEBUG_MSG(2, ("client hello was not authenticated"));
  2312. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
  2313. return ssl_write_hello_verify_request(ssl);
  2314. }
  2315. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
  2316. if (ssl->conf->f_rng == NULL) {
  2317. MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
  2318. return MBEDTLS_ERR_SSL_NO_RNG;
  2319. }
  2320. /*
  2321. * 0 . 0 handshake type
  2322. * 1 . 3 handshake length
  2323. * 4 . 5 protocol version
  2324. * 6 . 9 UNIX time()
  2325. * 10 . 37 random bytes
  2326. */
  2327. buf = ssl->out_msg;
  2328. p = buf + 4;
  2329. mbedtls_ssl_write_version(ssl->major_ver, ssl->minor_ver,
  2330. ssl->conf->transport, p);
  2331. p += 2;
  2332. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen version: [%d:%d]",
  2333. buf[4], buf[5]));
  2334. #if defined(MBEDTLS_HAVE_TIME)
  2335. t = mbedtls_time(NULL);
  2336. MBEDTLS_PUT_UINT32_BE(t, p, 0);
  2337. p += 4;
  2338. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
  2339. (long long) t));
  2340. #else
  2341. if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p, 4)) != 0) {
  2342. return ret;
  2343. }
  2344. p += 4;
  2345. #endif /* MBEDTLS_HAVE_TIME */
  2346. if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p, 28)) != 0) {
  2347. return ret;
  2348. }
  2349. p += 28;
  2350. memcpy(ssl->handshake->randbytes + 32, buf + 6, 32);
  2351. MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 6, 32);
  2352. ssl_handle_id_based_session_resumption(ssl);
  2353. if (ssl->handshake->resume == 0) {
  2354. /*
  2355. * New session, create a new session id,
  2356. * unless we're about to issue a session ticket
  2357. */
  2358. ssl->state++;
  2359. #if defined(MBEDTLS_HAVE_TIME)
  2360. ssl->session_negotiate->start = mbedtls_time(NULL);
  2361. #endif
  2362. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2363. if (ssl->handshake->new_session_ticket != 0) {
  2364. ssl->session_negotiate->id_len = n = 0;
  2365. memset(ssl->session_negotiate->id, 0, 32);
  2366. } else
  2367. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  2368. {
  2369. ssl->session_negotiate->id_len = n = 32;
  2370. if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, ssl->session_negotiate->id,
  2371. n)) != 0) {
  2372. return ret;
  2373. }
  2374. }
  2375. } else {
  2376. /*
  2377. * Resuming a session
  2378. */
  2379. n = ssl->session_negotiate->id_len;
  2380. ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
  2381. if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
  2382. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
  2383. return ret;
  2384. }
  2385. }
  2386. /*
  2387. * 38 . 38 session id length
  2388. * 39 . 38+n session id
  2389. * 39+n . 40+n chosen ciphersuite
  2390. * 41+n . 41+n chosen compression alg.
  2391. * 42+n . 43+n extensions length
  2392. * 44+n . 43+n+m extensions
  2393. */
  2394. *p++ = (unsigned char) ssl->session_negotiate->id_len;
  2395. memcpy(p, ssl->session_negotiate->id, ssl->session_negotiate->id_len);
  2396. p += ssl->session_negotiate->id_len;
  2397. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
  2398. MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 39, n);
  2399. MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
  2400. ssl->handshake->resume ? "a" : "no"));
  2401. MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
  2402. p += 2;
  2403. *p++ = MBEDTLS_BYTE_0(ssl->session_negotiate->compression);
  2404. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %s",
  2405. mbedtls_ssl_get_ciphersuite_name(ssl->session_negotiate->ciphersuite)));
  2406. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: 0x%02X",
  2407. (unsigned int) ssl->session_negotiate->compression));
  2408. /* Do not write the extensions if the protocol is SSLv3 */
  2409. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  2410. if ((ssl->major_ver != 3) || (ssl->minor_ver != 0)) {
  2411. #endif
  2412. /*
  2413. * First write extensions, then the total length
  2414. */
  2415. ssl_write_renegotiation_ext(ssl, p + 2 + ext_len, &olen);
  2416. ext_len += olen;
  2417. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  2418. ssl_write_max_fragment_length_ext(ssl, p + 2 + ext_len, &olen);
  2419. ext_len += olen;
  2420. #endif
  2421. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  2422. ssl_write_truncated_hmac_ext(ssl, p + 2 + ext_len, &olen);
  2423. ext_len += olen;
  2424. #endif
  2425. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  2426. ssl_write_cid_ext(ssl, p + 2 + ext_len, &olen);
  2427. ext_len += olen;
  2428. #endif
  2429. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  2430. ssl_write_encrypt_then_mac_ext(ssl, p + 2 + ext_len, &olen);
  2431. ext_len += olen;
  2432. #endif
  2433. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  2434. ssl_write_extended_ms_ext(ssl, p + 2 + ext_len, &olen);
  2435. ext_len += olen;
  2436. #endif
  2437. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2438. ssl_write_session_ticket_ext(ssl, p + 2 + ext_len, &olen);
  2439. ext_len += olen;
  2440. #endif
  2441. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  2442. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2443. if (mbedtls_ssl_ciphersuite_uses_ec(
  2444. mbedtls_ssl_ciphersuite_from_id(ssl->session_negotiate->ciphersuite))) {
  2445. ssl_write_supported_point_formats_ext(ssl, p + 2 + ext_len, &olen);
  2446. ext_len += olen;
  2447. }
  2448. #endif
  2449. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2450. ssl_write_ecjpake_kkpp_ext(ssl, p + 2 + ext_len, &olen);
  2451. ext_len += olen;
  2452. #endif
  2453. #if defined(MBEDTLS_SSL_ALPN)
  2454. ssl_write_alpn_ext(ssl, p + 2 + ext_len, &olen);
  2455. ext_len += olen;
  2456. #endif
  2457. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  2458. ssl_write_use_srtp_ext(ssl, p + 2 + ext_len, &olen);
  2459. ext_len += olen;
  2460. #endif
  2461. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
  2462. ext_len));
  2463. if (ext_len > 0) {
  2464. MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
  2465. p += 2 + ext_len;
  2466. }
  2467. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  2468. }
  2469. #endif
  2470. ssl->out_msglen = p - buf;
  2471. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  2472. ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
  2473. ret = mbedtls_ssl_write_handshake_msg(ssl);
  2474. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
  2475. return ret;
  2476. }
  2477. #if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
  2478. MBEDTLS_CHECK_RETURN_CRITICAL
  2479. static int ssl_write_certificate_request(mbedtls_ssl_context *ssl)
  2480. {
  2481. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2482. ssl->handshake->ciphersuite_info;
  2483. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
  2484. if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
  2485. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
  2486. ssl->state++;
  2487. return 0;
  2488. }
  2489. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2490. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2491. }
  2492. #else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
  2493. MBEDTLS_CHECK_RETURN_CRITICAL
  2494. static int ssl_write_certificate_request(mbedtls_ssl_context *ssl)
  2495. {
  2496. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  2497. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2498. ssl->handshake->ciphersuite_info;
  2499. uint16_t dn_size, total_dn_size; /* excluding length bytes */
  2500. size_t ct_len, sa_len; /* including length bytes */
  2501. unsigned char *buf, *p;
  2502. const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
  2503. const mbedtls_x509_crt *crt;
  2504. int authmode;
  2505. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
  2506. ssl->state++;
  2507. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  2508. if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
  2509. authmode = ssl->handshake->sni_authmode;
  2510. } else
  2511. #endif
  2512. authmode = ssl->conf->authmode;
  2513. if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info) ||
  2514. authmode == MBEDTLS_SSL_VERIFY_NONE) {
  2515. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
  2516. return 0;
  2517. }
  2518. /*
  2519. * 0 . 0 handshake type
  2520. * 1 . 3 handshake length
  2521. * 4 . 4 cert type count
  2522. * 5 .. m-1 cert types
  2523. * m .. m+1 sig alg length (TLS 1.2 only)
  2524. * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
  2525. * n .. n+1 length of all DNs
  2526. * n+2 .. n+3 length of DN 1
  2527. * n+4 .. ... Distinguished Name #1
  2528. * ... .. ... length of DN 2, etc.
  2529. */
  2530. buf = ssl->out_msg;
  2531. p = buf + 4;
  2532. /*
  2533. * Supported certificate types
  2534. *
  2535. * ClientCertificateType certificate_types<1..2^8-1>;
  2536. * enum { (255) } ClientCertificateType;
  2537. */
  2538. ct_len = 0;
  2539. #if defined(MBEDTLS_RSA_C)
  2540. p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
  2541. #endif
  2542. #if defined(MBEDTLS_ECDSA_C)
  2543. p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
  2544. #endif
  2545. p[0] = (unsigned char) ct_len++;
  2546. p += ct_len;
  2547. sa_len = 0;
  2548. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2549. /*
  2550. * Add signature_algorithms for verify (TLS 1.2)
  2551. *
  2552. * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
  2553. *
  2554. * struct {
  2555. * HashAlgorithm hash;
  2556. * SignatureAlgorithm signature;
  2557. * } SignatureAndHashAlgorithm;
  2558. *
  2559. * enum { (255) } HashAlgorithm;
  2560. * enum { (255) } SignatureAlgorithm;
  2561. */
  2562. if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
  2563. const int *cur;
  2564. /*
  2565. * Supported signature algorithms
  2566. */
  2567. for (cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++) {
  2568. unsigned char hash = mbedtls_ssl_hash_from_md_alg(*cur);
  2569. if (MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md(ssl, hash)) {
  2570. continue;
  2571. }
  2572. #if defined(MBEDTLS_RSA_C)
  2573. p[2 + sa_len++] = hash;
  2574. p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA;
  2575. #endif
  2576. #if defined(MBEDTLS_ECDSA_C)
  2577. p[2 + sa_len++] = hash;
  2578. p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA;
  2579. #endif
  2580. }
  2581. MBEDTLS_PUT_UINT16_BE(sa_len, p, 0);
  2582. sa_len += 2;
  2583. p += sa_len;
  2584. }
  2585. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2586. /*
  2587. * DistinguishedName certificate_authorities<0..2^16-1>;
  2588. * opaque DistinguishedName<1..2^16-1>;
  2589. */
  2590. p += 2;
  2591. total_dn_size = 0;
  2592. if (ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED) {
  2593. /* NOTE: If trusted certificates are provisioned
  2594. * via a CA callback (configured through
  2595. * `mbedtls_ssl_conf_ca_cb()`, then the
  2596. * CertificateRequest is currently left empty. */
  2597. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  2598. if (ssl->handshake->sni_ca_chain != NULL) {
  2599. crt = ssl->handshake->sni_ca_chain;
  2600. } else
  2601. #endif
  2602. crt = ssl->conf->ca_chain;
  2603. while (crt != NULL && crt->version != 0) {
  2604. /* It follows from RFC 5280 A.1 that this length
  2605. * can be represented in at most 11 bits. */
  2606. dn_size = (uint16_t) crt->subject_raw.len;
  2607. if (end < p || (size_t) (end - p) < 2 + (size_t) dn_size) {
  2608. MBEDTLS_SSL_DEBUG_MSG(1, ("skipping CAs: buffer too short"));
  2609. break;
  2610. }
  2611. MBEDTLS_PUT_UINT16_BE(dn_size, p, 0);
  2612. p += 2;
  2613. memcpy(p, crt->subject_raw.p, dn_size);
  2614. p += dn_size;
  2615. MBEDTLS_SSL_DEBUG_BUF(3, "requested DN", p - dn_size, dn_size);
  2616. total_dn_size += 2 + dn_size;
  2617. crt = crt->next;
  2618. }
  2619. }
  2620. ssl->out_msglen = p - buf;
  2621. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  2622. ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
  2623. MBEDTLS_PUT_UINT16_BE(total_dn_size, ssl->out_msg, 4 + ct_len + sa_len);
  2624. ret = mbedtls_ssl_write_handshake_msg(ssl);
  2625. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
  2626. return ret;
  2627. }
  2628. #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
  2629. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  2630. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  2631. MBEDTLS_CHECK_RETURN_CRITICAL
  2632. static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
  2633. {
  2634. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2635. mbedtls_pk_context *own_key = mbedtls_ssl_own_key(ssl);
  2636. /* Check if the key is a transparent ECDH key.
  2637. * This also ensures that it is safe to call mbedtls_pk_ec(). */
  2638. if (mbedtls_pk_get_type(own_key) != MBEDTLS_PK_ECKEY &&
  2639. mbedtls_pk_get_type(own_key) != MBEDTLS_PK_ECKEY_DH) {
  2640. MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
  2641. return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
  2642. }
  2643. if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx,
  2644. mbedtls_pk_ec(*own_key),
  2645. MBEDTLS_ECDH_OURS)) != 0) {
  2646. MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
  2647. return ret;
  2648. }
  2649. return 0;
  2650. }
  2651. #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
  2652. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  2653. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
  2654. defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2655. MBEDTLS_CHECK_RETURN_CRITICAL
  2656. static int ssl_resume_server_key_exchange(mbedtls_ssl_context *ssl,
  2657. size_t *signature_len)
  2658. {
  2659. /* Append the signature to ssl->out_msg, leaving 2 bytes for the
  2660. * signature length which will be added in ssl_write_server_key_exchange
  2661. * after the call to ssl_prepare_server_key_exchange.
  2662. * ssl_write_server_key_exchange also takes care of incrementing
  2663. * ssl->out_msglen. */
  2664. unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
  2665. size_t sig_max_len = (ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
  2666. - sig_start);
  2667. int ret = ssl->conf->f_async_resume(ssl,
  2668. sig_start, signature_len, sig_max_len);
  2669. if (ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
  2670. ssl->handshake->async_in_progress = 0;
  2671. mbedtls_ssl_set_async_operation_data(ssl, NULL);
  2672. }
  2673. MBEDTLS_SSL_DEBUG_RET(2, "ssl_resume_server_key_exchange", ret);
  2674. return ret;
  2675. }
  2676. #endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
  2677. defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
  2678. /* Prepare the ServerKeyExchange message, up to and including
  2679. * calculating the signature if any, but excluding formatting the
  2680. * signature and sending the message. */
  2681. MBEDTLS_CHECK_RETURN_CRITICAL
  2682. static int ssl_prepare_server_key_exchange(mbedtls_ssl_context *ssl,
  2683. size_t *signature_len)
  2684. {
  2685. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2686. ssl->handshake->ciphersuite_info;
  2687. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
  2688. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
  2689. unsigned char *dig_signed = NULL;
  2690. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
  2691. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
  2692. (void) ciphersuite_info; /* unused in some configurations */
  2693. #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
  2694. (void) signature_len;
  2695. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
  2696. ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
  2697. /*
  2698. *
  2699. * Part 1: Provide key exchange parameters for chosen ciphersuite.
  2700. *
  2701. */
  2702. /*
  2703. * - ECJPAKE key exchanges
  2704. */
  2705. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2706. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
  2707. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2708. size_t len = 0;
  2709. ret = mbedtls_ecjpake_write_round_two(
  2710. &ssl->handshake->ecjpake_ctx,
  2711. ssl->out_msg + ssl->out_msglen,
  2712. MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
  2713. ssl->conf->f_rng, ssl->conf->p_rng);
  2714. if (ret != 0) {
  2715. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
  2716. return ret;
  2717. }
  2718. ssl->out_msglen += len;
  2719. }
  2720. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  2721. /*
  2722. * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
  2723. * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
  2724. * we use empty support identity hints here.
  2725. **/
  2726. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
  2727. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  2728. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  2729. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
  2730. ssl->out_msg[ssl->out_msglen++] = 0x00;
  2731. ssl->out_msg[ssl->out_msglen++] = 0x00;
  2732. }
  2733. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
  2734. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  2735. /*
  2736. * - DHE key exchanges
  2737. */
  2738. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
  2739. if (mbedtls_ssl_ciphersuite_uses_dhe(ciphersuite_info)) {
  2740. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2741. size_t len = 0;
  2742. if (ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL) {
  2743. MBEDTLS_SSL_DEBUG_MSG(1, ("no DH parameters set"));
  2744. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  2745. }
  2746. /*
  2747. * Ephemeral DH parameters:
  2748. *
  2749. * struct {
  2750. * opaque dh_p<1..2^16-1>;
  2751. * opaque dh_g<1..2^16-1>;
  2752. * opaque dh_Ys<1..2^16-1>;
  2753. * } ServerDHParams;
  2754. */
  2755. if ((ret = mbedtls_dhm_set_group(&ssl->handshake->dhm_ctx,
  2756. &ssl->conf->dhm_P,
  2757. &ssl->conf->dhm_G)) != 0) {
  2758. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_set_group", ret);
  2759. return ret;
  2760. }
  2761. if ((ret = mbedtls_dhm_make_params(
  2762. &ssl->handshake->dhm_ctx,
  2763. (int) mbedtls_mpi_size(&ssl->handshake->dhm_ctx.P),
  2764. ssl->out_msg + ssl->out_msglen, &len,
  2765. ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
  2766. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_params", ret);
  2767. return ret;
  2768. }
  2769. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
  2770. dig_signed = ssl->out_msg + ssl->out_msglen;
  2771. #endif
  2772. ssl->out_msglen += len;
  2773. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
  2774. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
  2775. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
  2776. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
  2777. }
  2778. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
  2779. /*
  2780. * - ECDHE key exchanges
  2781. */
  2782. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
  2783. if (mbedtls_ssl_ciphersuite_uses_ecdhe(ciphersuite_info)) {
  2784. /*
  2785. * Ephemeral ECDH parameters:
  2786. *
  2787. * struct {
  2788. * ECParameters curve_params;
  2789. * ECPoint public;
  2790. * } ServerECDHParams;
  2791. */
  2792. const mbedtls_ecp_curve_info **curve = NULL;
  2793. const mbedtls_ecp_group_id *gid;
  2794. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2795. size_t len = 0;
  2796. /* Match our preference list against the offered curves */
  2797. for (gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++) {
  2798. for (curve = ssl->handshake->curves; *curve != NULL; curve++) {
  2799. if ((*curve)->grp_id == *gid) {
  2800. goto curve_matching_done;
  2801. }
  2802. }
  2803. }
  2804. curve_matching_done:
  2805. if (curve == NULL || *curve == NULL) {
  2806. MBEDTLS_SSL_DEBUG_MSG(1, ("no matching curve for ECDHE"));
  2807. return MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN;
  2808. }
  2809. MBEDTLS_SSL_DEBUG_MSG(2, ("ECDHE curve: %s", (*curve)->name));
  2810. if ((ret = mbedtls_ecdh_setup(&ssl->handshake->ecdh_ctx,
  2811. (*curve)->grp_id)) != 0) {
  2812. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecp_group_load", ret);
  2813. return ret;
  2814. }
  2815. if ((ret = mbedtls_ecdh_make_params(
  2816. &ssl->handshake->ecdh_ctx, &len,
  2817. ssl->out_msg + ssl->out_msglen,
  2818. MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
  2819. ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
  2820. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_params", ret);
  2821. return ret;
  2822. }
  2823. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
  2824. dig_signed = ssl->out_msg + ssl->out_msglen;
  2825. #endif
  2826. ssl->out_msglen += len;
  2827. MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
  2828. MBEDTLS_DEBUG_ECDH_Q);
  2829. }
  2830. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
  2831. /*
  2832. *
  2833. * Part 2: For key exchanges involving the server signing the
  2834. * exchange parameters, compute and add the signature here.
  2835. *
  2836. */
  2837. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
  2838. if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
  2839. size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
  2840. size_t hashlen = 0;
  2841. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2842. unsigned char hash[PSA_HASH_MAX_SIZE];
  2843. #else
  2844. unsigned char hash[MBEDTLS_MD_MAX_SIZE];
  2845. #endif
  2846. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2847. /*
  2848. * 2.1: Choose hash algorithm:
  2849. * A: For TLS 1.2, obey signature-hash-algorithm extension
  2850. * to choose appropriate hash.
  2851. * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1
  2852. * (RFC 4492, Sec. 5.4)
  2853. * C: Otherwise, use MD5 + SHA1 (RFC 4346, Sec. 7.4.3)
  2854. */
  2855. mbedtls_md_type_t md_alg;
  2856. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2857. mbedtls_pk_type_t sig_alg =
  2858. mbedtls_ssl_get_ciphersuite_sig_pk_alg(ciphersuite_info);
  2859. if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
  2860. /* A: For TLS 1.2, obey signature-hash-algorithm extension
  2861. * (RFC 5246, Sec. 7.4.1.4.1). */
  2862. if (sig_alg == MBEDTLS_PK_NONE ||
  2863. (md_alg = mbedtls_ssl_sig_hash_set_find(&ssl->handshake->hash_algs,
  2864. sig_alg)) == MBEDTLS_MD_NONE) {
  2865. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2866. /* (... because we choose a cipher suite
  2867. * only if there is a matching hash.) */
  2868. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2869. }
  2870. } else
  2871. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2872. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  2873. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  2874. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
  2875. /* B: Default hash SHA1 */
  2876. md_alg = MBEDTLS_MD_SHA1;
  2877. } else
  2878. #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
  2879. MBEDTLS_SSL_PROTO_TLS1_1 */
  2880. {
  2881. /* C: MD5 + SHA1 */
  2882. md_alg = MBEDTLS_MD_NONE;
  2883. }
  2884. MBEDTLS_SSL_DEBUG_MSG(3, ("pick hash algorithm %u for signing", (unsigned) md_alg));
  2885. /*
  2886. * 2.2: Compute the hash to be signed
  2887. */
  2888. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  2889. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  2890. if (md_alg == MBEDTLS_MD_NONE) {
  2891. hashlen = 36;
  2892. ret = mbedtls_ssl_get_key_exchange_md_ssl_tls(ssl, hash,
  2893. dig_signed,
  2894. dig_signed_len);
  2895. if (ret != 0) {
  2896. return ret;
  2897. }
  2898. } else
  2899. #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
  2900. MBEDTLS_SSL_PROTO_TLS1_1 */
  2901. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  2902. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2903. if (md_alg != MBEDTLS_MD_NONE) {
  2904. ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
  2905. dig_signed,
  2906. dig_signed_len,
  2907. md_alg);
  2908. if (ret != 0) {
  2909. return ret;
  2910. }
  2911. } else
  2912. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
  2913. MBEDTLS_SSL_PROTO_TLS1_2 */
  2914. {
  2915. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2916. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2917. }
  2918. MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
  2919. /*
  2920. * 2.3: Compute and add the signature
  2921. */
  2922. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2923. if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
  2924. /*
  2925. * For TLS 1.2, we need to specify signature and hash algorithm
  2926. * explicitly through a prefix to the signature.
  2927. *
  2928. * struct {
  2929. * HashAlgorithm hash;
  2930. * SignatureAlgorithm signature;
  2931. * } SignatureAndHashAlgorithm;
  2932. *
  2933. * struct {
  2934. * SignatureAndHashAlgorithm algorithm;
  2935. * opaque signature<0..2^16-1>;
  2936. * } DigitallySigned;
  2937. *
  2938. */
  2939. ssl->out_msg[ssl->out_msglen++] =
  2940. mbedtls_ssl_hash_from_md_alg(md_alg);
  2941. ssl->out_msg[ssl->out_msglen++] =
  2942. mbedtls_ssl_sig_from_pk_alg(sig_alg);
  2943. }
  2944. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2945. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2946. if (ssl->conf->f_async_sign_start != NULL) {
  2947. ret = ssl->conf->f_async_sign_start(ssl,
  2948. mbedtls_ssl_own_cert(ssl),
  2949. md_alg, hash, hashlen);
  2950. switch (ret) {
  2951. case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
  2952. /* act as if f_async_sign was null */
  2953. break;
  2954. case 0:
  2955. ssl->handshake->async_in_progress = 1;
  2956. return ssl_resume_server_key_exchange(ssl, signature_len);
  2957. case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
  2958. ssl->handshake->async_in_progress = 1;
  2959. return MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS;
  2960. default:
  2961. MBEDTLS_SSL_DEBUG_RET(1, "f_async_sign_start", ret);
  2962. return ret;
  2963. }
  2964. }
  2965. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  2966. if (mbedtls_ssl_own_key(ssl) == NULL) {
  2967. MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key"));
  2968. return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
  2969. }
  2970. /* Append the signature to ssl->out_msg, leaving 2 bytes for the
  2971. * signature length which will be added in ssl_write_server_key_exchange
  2972. * after the call to ssl_prepare_server_key_exchange.
  2973. * ssl_write_server_key_exchange also takes care of incrementing
  2974. * ssl->out_msglen. */
  2975. if ((ret = mbedtls_pk_sign(mbedtls_ssl_own_key(ssl),
  2976. md_alg, hash, hashlen,
  2977. ssl->out_msg + ssl->out_msglen + 2,
  2978. signature_len,
  2979. ssl->conf->f_rng,
  2980. ssl->conf->p_rng)) != 0) {
  2981. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
  2982. return ret;
  2983. }
  2984. }
  2985. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
  2986. return 0;
  2987. }
  2988. /* Prepare the ServerKeyExchange message and send it. For ciphersuites
  2989. * that do not include a ServerKeyExchange message, do nothing. Either
  2990. * way, if successful, move on to the next step in the SSL state
  2991. * machine. */
  2992. MBEDTLS_CHECK_RETURN_CRITICAL
  2993. static int ssl_write_server_key_exchange(mbedtls_ssl_context *ssl)
  2994. {
  2995. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2996. size_t signature_len = 0;
  2997. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
  2998. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2999. ssl->handshake->ciphersuite_info;
  3000. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
  3001. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server key exchange"));
  3002. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
  3003. /* Extract static ECDH parameters and abort if ServerKeyExchange
  3004. * is not needed. */
  3005. if (mbedtls_ssl_ciphersuite_no_pfs(ciphersuite_info)) {
  3006. /* For suites involving ECDH, extract DH parameters
  3007. * from certificate at this point. */
  3008. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
  3009. if (mbedtls_ssl_ciphersuite_uses_ecdh(ciphersuite_info)) {
  3010. ret = ssl_get_ecdh_params_from_cert(ssl);
  3011. if (ret != 0) {
  3012. MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
  3013. return ret;
  3014. }
  3015. }
  3016. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
  3017. /* Key exchanges not involving ephemeral keys don't use
  3018. * ServerKeyExchange, so end here. */
  3019. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write server key exchange"));
  3020. ssl->state++;
  3021. return 0;
  3022. }
  3023. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
  3024. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
  3025. defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  3026. /* If we have already prepared the message and there is an ongoing
  3027. * signature operation, resume signing. */
  3028. if (ssl->handshake->async_in_progress != 0) {
  3029. MBEDTLS_SSL_DEBUG_MSG(2, ("resuming signature operation"));
  3030. ret = ssl_resume_server_key_exchange(ssl, &signature_len);
  3031. } else
  3032. #endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
  3033. defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
  3034. {
  3035. /* ServerKeyExchange is needed. Prepare the message. */
  3036. ret = ssl_prepare_server_key_exchange(ssl, &signature_len);
  3037. }
  3038. if (ret != 0) {
  3039. /* If we're starting to write a new message, set ssl->out_msglen
  3040. * to 0. But if we're resuming after an asynchronous message,
  3041. * out_msglen is the amount of data written so far and mst be
  3042. * preserved. */
  3043. if (ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
  3044. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server key exchange (pending)"));
  3045. } else {
  3046. ssl->out_msglen = 0;
  3047. }
  3048. return ret;
  3049. }
  3050. /* If there is a signature, write its length.
  3051. * ssl_prepare_server_key_exchange already wrote the signature
  3052. * itself at its proper place in the output buffer. */
  3053. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
  3054. if (signature_len != 0) {
  3055. ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_1(signature_len);
  3056. ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_0(signature_len);
  3057. MBEDTLS_SSL_DEBUG_BUF(3, "my signature",
  3058. ssl->out_msg + ssl->out_msglen,
  3059. signature_len);
  3060. /* Skip over the already-written signature */
  3061. ssl->out_msglen += signature_len;
  3062. }
  3063. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
  3064. /* Add header and send. */
  3065. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  3066. ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
  3067. ssl->state++;
  3068. if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
  3069. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
  3070. return ret;
  3071. }
  3072. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server key exchange"));
  3073. return 0;
  3074. }
  3075. MBEDTLS_CHECK_RETURN_CRITICAL
  3076. static int ssl_write_server_hello_done(mbedtls_ssl_context *ssl)
  3077. {
  3078. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3079. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello done"));
  3080. ssl->out_msglen = 4;
  3081. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  3082. ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
  3083. ssl->state++;
  3084. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  3085. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  3086. mbedtls_ssl_send_flight_completed(ssl);
  3087. }
  3088. #endif
  3089. if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
  3090. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
  3091. return ret;
  3092. }
  3093. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  3094. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  3095. (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
  3096. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
  3097. return ret;
  3098. }
  3099. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  3100. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello done"));
  3101. return 0;
  3102. }
  3103. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  3104. defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  3105. MBEDTLS_CHECK_RETURN_CRITICAL
  3106. static int ssl_parse_client_dh_public(mbedtls_ssl_context *ssl, unsigned char **p,
  3107. const unsigned char *end)
  3108. {
  3109. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  3110. size_t n;
  3111. /*
  3112. * Receive G^Y mod P, premaster = (G^Y)^X mod P
  3113. */
  3114. if (*p + 2 > end) {
  3115. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
  3116. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
  3117. }
  3118. n = ((*p)[0] << 8) | (*p)[1];
  3119. *p += 2;
  3120. if (*p + n > end) {
  3121. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
  3122. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
  3123. }
  3124. if ((ret = mbedtls_dhm_read_public(&ssl->handshake->dhm_ctx, *p, n)) != 0) {
  3125. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_read_public", ret);
  3126. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP;
  3127. }
  3128. *p += n;
  3129. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
  3130. return ret;
  3131. }
  3132. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  3133. MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  3134. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
  3135. defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  3136. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  3137. MBEDTLS_CHECK_RETURN_CRITICAL
  3138. static int ssl_resume_decrypt_pms(mbedtls_ssl_context *ssl,
  3139. unsigned char *peer_pms,
  3140. size_t *peer_pmslen,
  3141. size_t peer_pmssize)
  3142. {
  3143. int ret = ssl->conf->f_async_resume(ssl,
  3144. peer_pms, peer_pmslen, peer_pmssize);
  3145. if (ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
  3146. ssl->handshake->async_in_progress = 0;
  3147. mbedtls_ssl_set_async_operation_data(ssl, NULL);
  3148. }
  3149. MBEDTLS_SSL_DEBUG_RET(2, "ssl_decrypt_encrypted_pms", ret);
  3150. return ret;
  3151. }
  3152. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  3153. MBEDTLS_CHECK_RETURN_CRITICAL
  3154. static int ssl_decrypt_encrypted_pms(mbedtls_ssl_context *ssl,
  3155. const unsigned char *p,
  3156. const unsigned char *end,
  3157. unsigned char *peer_pms,
  3158. size_t *peer_pmslen,
  3159. size_t peer_pmssize)
  3160. {
  3161. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3162. mbedtls_x509_crt *own_cert = mbedtls_ssl_own_cert(ssl);
  3163. if (own_cert == NULL) {
  3164. MBEDTLS_SSL_DEBUG_MSG(1, ("got no local certificate"));
  3165. return MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
  3166. }
  3167. mbedtls_pk_context *public_key = &own_cert->pk;
  3168. mbedtls_pk_context *private_key = mbedtls_ssl_own_key(ssl);
  3169. size_t len = mbedtls_pk_get_len(public_key);
  3170. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  3171. /* If we have already started decoding the message and there is an ongoing
  3172. * decryption operation, resume signing. */
  3173. if (ssl->handshake->async_in_progress != 0) {
  3174. MBEDTLS_SSL_DEBUG_MSG(2, ("resuming decryption operation"));
  3175. return ssl_resume_decrypt_pms(ssl,
  3176. peer_pms, peer_pmslen, peer_pmssize);
  3177. }
  3178. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  3179. /*
  3180. * Prepare to decrypt the premaster using own private RSA key
  3181. */
  3182. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  3183. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  3184. if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0) {
  3185. if (p + 2 > end) {
  3186. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
  3187. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
  3188. }
  3189. if (*p++ != MBEDTLS_BYTE_1(len) ||
  3190. *p++ != MBEDTLS_BYTE_0(len)) {
  3191. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
  3192. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
  3193. }
  3194. }
  3195. #endif
  3196. if (p + len != end) {
  3197. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
  3198. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
  3199. }
  3200. /*
  3201. * Decrypt the premaster secret
  3202. */
  3203. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  3204. if (ssl->conf->f_async_decrypt_start != NULL) {
  3205. ret = ssl->conf->f_async_decrypt_start(ssl,
  3206. mbedtls_ssl_own_cert(ssl),
  3207. p, len);
  3208. switch (ret) {
  3209. case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
  3210. /* act as if f_async_decrypt_start was null */
  3211. break;
  3212. case 0:
  3213. ssl->handshake->async_in_progress = 1;
  3214. return ssl_resume_decrypt_pms(ssl,
  3215. peer_pms,
  3216. peer_pmslen,
  3217. peer_pmssize);
  3218. case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
  3219. ssl->handshake->async_in_progress = 1;
  3220. return MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS;
  3221. default:
  3222. MBEDTLS_SSL_DEBUG_RET(1, "f_async_decrypt_start", ret);
  3223. return ret;
  3224. }
  3225. }
  3226. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  3227. if (!mbedtls_pk_can_do(private_key, MBEDTLS_PK_RSA)) {
  3228. MBEDTLS_SSL_DEBUG_MSG(1, ("got no RSA private key"));
  3229. return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
  3230. }
  3231. ret = mbedtls_pk_decrypt(private_key, p, len,
  3232. peer_pms, peer_pmslen, peer_pmssize,
  3233. ssl->conf->f_rng, ssl->conf->p_rng);
  3234. return ret;
  3235. }
  3236. MBEDTLS_CHECK_RETURN_CRITICAL
  3237. static int ssl_parse_encrypted_pms(mbedtls_ssl_context *ssl,
  3238. const unsigned char *p,
  3239. const unsigned char *end,
  3240. size_t pms_offset)
  3241. {
  3242. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3243. unsigned char *pms = ssl->handshake->premaster + pms_offset;
  3244. unsigned char ver[2];
  3245. unsigned char fake_pms[48], peer_pms[48];
  3246. unsigned char mask;
  3247. size_t i, peer_pmslen;
  3248. unsigned int diff;
  3249. /* In case of a failure in decryption, the decryption may write less than
  3250. * 2 bytes of output, but we always read the first two bytes. It doesn't
  3251. * matter in the end because diff will be nonzero in that case due to
  3252. * ret being nonzero, and we only care whether diff is 0.
  3253. * But do initialize peer_pms and peer_pmslen for robustness anyway. This
  3254. * also makes memory analyzers happy (don't access uninitialized memory,
  3255. * even if it's an unsigned char). */
  3256. peer_pms[0] = peer_pms[1] = ~0;
  3257. peer_pmslen = 0;
  3258. ret = ssl_decrypt_encrypted_pms(ssl, p, end,
  3259. peer_pms,
  3260. &peer_pmslen,
  3261. sizeof(peer_pms));
  3262. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  3263. if (ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
  3264. return ret;
  3265. }
  3266. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  3267. mbedtls_ssl_write_version(ssl->handshake->max_major_ver,
  3268. ssl->handshake->max_minor_ver,
  3269. ssl->conf->transport, ver);
  3270. /* Avoid data-dependent branches while checking for invalid
  3271. * padding, to protect against timing-based Bleichenbacher-type
  3272. * attacks. */
  3273. diff = (unsigned int) ret;
  3274. diff |= peer_pmslen ^ 48;
  3275. diff |= peer_pms[0] ^ ver[0];
  3276. diff |= peer_pms[1] ^ ver[1];
  3277. /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
  3278. mask = mbedtls_ct_uint_mask(diff);
  3279. /*
  3280. * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
  3281. * must not cause the connection to end immediately; instead, send a
  3282. * bad_record_mac later in the handshake.
  3283. * To protect against timing-based variants of the attack, we must
  3284. * not have any branch that depends on whether the decryption was
  3285. * successful. In particular, always generate the fake premaster secret,
  3286. * regardless of whether it will ultimately influence the output or not.
  3287. */
  3288. ret = ssl->conf->f_rng(ssl->conf->p_rng, fake_pms, sizeof(fake_pms));
  3289. if (ret != 0) {
  3290. /* It's ok to abort on an RNG failure, since this does not reveal
  3291. * anything about the RSA decryption. */
  3292. return ret;
  3293. }
  3294. #if defined(MBEDTLS_SSL_DEBUG_ALL)
  3295. if (diff != 0) {
  3296. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
  3297. }
  3298. #endif
  3299. if (sizeof(ssl->handshake->premaster) < pms_offset ||
  3300. sizeof(ssl->handshake->premaster) - pms_offset < 48) {
  3301. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  3302. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  3303. }
  3304. ssl->handshake->pmslen = 48;
  3305. /* Set pms to either the true or the fake PMS, without
  3306. * data-dependent branches. */
  3307. for (i = 0; i < ssl->handshake->pmslen; i++) {
  3308. pms[i] = (mask & fake_pms[i]) | ((~mask) & peer_pms[i]);
  3309. }
  3310. return 0;
  3311. }
  3312. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
  3313. MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  3314. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  3315. MBEDTLS_CHECK_RETURN_CRITICAL
  3316. static int ssl_parse_client_psk_identity(mbedtls_ssl_context *ssl, unsigned char **p,
  3317. const unsigned char *end)
  3318. {
  3319. int ret = 0;
  3320. uint16_t n;
  3321. if (ssl_conf_has_psk_or_cb(ssl->conf) == 0) {
  3322. MBEDTLS_SSL_DEBUG_MSG(1, ("got no pre-shared key"));
  3323. return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
  3324. }
  3325. /*
  3326. * Receive client pre-shared key identity name
  3327. */
  3328. if (end - *p < 2) {
  3329. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
  3330. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
  3331. }
  3332. n = ((*p)[0] << 8) | (*p)[1];
  3333. *p += 2;
  3334. if (n == 0 || n > end - *p) {
  3335. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
  3336. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
  3337. }
  3338. if (ssl->conf->f_psk != NULL) {
  3339. if (ssl->conf->f_psk(ssl->conf->p_psk, ssl, *p, n) != 0) {
  3340. ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
  3341. }
  3342. } else {
  3343. /* Identity is not a big secret since clients send it in the clear,
  3344. * but treat it carefully anyway, just in case */
  3345. if (n != ssl->conf->psk_identity_len ||
  3346. mbedtls_ct_memcmp(ssl->conf->psk_identity, *p, n) != 0) {
  3347. ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
  3348. }
  3349. }
  3350. if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
  3351. MBEDTLS_SSL_DEBUG_BUF(3, "Unknown PSK identity", *p, n);
  3352. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  3353. MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY);
  3354. return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
  3355. }
  3356. *p += n;
  3357. return 0;
  3358. }
  3359. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  3360. MBEDTLS_CHECK_RETURN_CRITICAL
  3361. static int ssl_parse_client_key_exchange(mbedtls_ssl_context *ssl)
  3362. {
  3363. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3364. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  3365. unsigned char *p, *end;
  3366. ciphersuite_info = ssl->handshake->ciphersuite_info;
  3367. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client key exchange"));
  3368. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
  3369. (defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
  3370. defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED))
  3371. if ((ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  3372. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) &&
  3373. (ssl->handshake->async_in_progress != 0)) {
  3374. /* We've already read a record and there is an asynchronous
  3375. * operation in progress to decrypt it. So skip reading the
  3376. * record. */
  3377. MBEDTLS_SSL_DEBUG_MSG(3, ("will resume decryption of previously-read record"));
  3378. } else
  3379. #endif
  3380. if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
  3381. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
  3382. return ret;
  3383. }
  3384. p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
  3385. end = ssl->in_msg + ssl->in_hslen;
  3386. if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
  3387. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
  3388. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
  3389. }
  3390. if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE) {
  3391. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
  3392. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
  3393. }
  3394. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
  3395. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
  3396. if ((ret = ssl_parse_client_dh_public(ssl, &p, end)) != 0) {
  3397. MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_dh_public"), ret);
  3398. return ret;
  3399. }
  3400. if (p != end) {
  3401. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange"));
  3402. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
  3403. }
  3404. if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
  3405. ssl->handshake->premaster,
  3406. MBEDTLS_PREMASTER_SIZE,
  3407. &ssl->handshake->pmslen,
  3408. ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
  3409. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
  3410. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS;
  3411. }
  3412. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
  3413. } else
  3414. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
  3415. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  3416. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  3417. defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  3418. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  3419. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  3420. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
  3421. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
  3422. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
  3423. if ((ret = mbedtls_ecdh_read_public(&ssl->handshake->ecdh_ctx,
  3424. p, end - p)) != 0) {
  3425. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_read_public", ret);
  3426. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP;
  3427. }
  3428. MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
  3429. MBEDTLS_DEBUG_ECDH_QP);
  3430. if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
  3431. &ssl->handshake->pmslen,
  3432. ssl->handshake->premaster,
  3433. MBEDTLS_MPI_MAX_SIZE,
  3434. ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
  3435. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
  3436. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS;
  3437. }
  3438. MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
  3439. MBEDTLS_DEBUG_ECDH_Z);
  3440. } else
  3441. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  3442. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  3443. MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  3444. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  3445. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
  3446. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
  3447. if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
  3448. MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
  3449. return ret;
  3450. }
  3451. if (p != end) {
  3452. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange"));
  3453. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
  3454. }
  3455. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3456. /* For opaque PSKs, we perform the PSK-to-MS derivation automatically
  3457. * and skip the intermediate PMS. */
  3458. if (ssl_use_opaque_psk(ssl) == 1) {
  3459. MBEDTLS_SSL_DEBUG_MSG(1, ("skip PMS generation for opaque PSK"));
  3460. } else
  3461. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  3462. if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
  3463. ciphersuite_info->key_exchange)) != 0) {
  3464. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
  3465. return ret;
  3466. }
  3467. } else
  3468. #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
  3469. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  3470. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
  3471. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  3472. if (ssl->handshake->async_in_progress != 0) {
  3473. /* There is an asynchronous operation in progress to
  3474. * decrypt the encrypted premaster secret, so skip
  3475. * directly to resuming this operation. */
  3476. MBEDTLS_SSL_DEBUG_MSG(3, ("PSK identity already parsed"));
  3477. /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
  3478. * won't actually use it, but maintain p anyway for robustness. */
  3479. p += ssl->conf->psk_identity_len + 2;
  3480. } else
  3481. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  3482. if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
  3483. MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
  3484. return ret;
  3485. }
  3486. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3487. /* Opaque PSKs are currently only supported for PSK-only. */
  3488. if (ssl_use_opaque_psk(ssl) == 1) {
  3489. MBEDTLS_SSL_DEBUG_MSG(1, ("opaque PSK not supported with RSA-PSK"));
  3490. return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  3491. }
  3492. #endif
  3493. if ((ret = ssl_parse_encrypted_pms(ssl, p, end, 2)) != 0) {
  3494. MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_encrypted_pms"), ret);
  3495. return ret;
  3496. }
  3497. if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
  3498. ciphersuite_info->key_exchange)) != 0) {
  3499. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
  3500. return ret;
  3501. }
  3502. } else
  3503. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  3504. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  3505. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
  3506. if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
  3507. MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
  3508. return ret;
  3509. }
  3510. if ((ret = ssl_parse_client_dh_public(ssl, &p, end)) != 0) {
  3511. MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_dh_public"), ret);
  3512. return ret;
  3513. }
  3514. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3515. /* Opaque PSKs are currently only supported for PSK-only. */
  3516. if (ssl_use_opaque_psk(ssl) == 1) {
  3517. MBEDTLS_SSL_DEBUG_MSG(1, ("opaque PSK not supported with DHE-PSK"));
  3518. return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  3519. }
  3520. #endif
  3521. if (p != end) {
  3522. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange"));
  3523. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE;
  3524. }
  3525. if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
  3526. ciphersuite_info->key_exchange)) != 0) {
  3527. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
  3528. return ret;
  3529. }
  3530. } else
  3531. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  3532. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  3533. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
  3534. if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
  3535. MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
  3536. return ret;
  3537. }
  3538. if ((ret = mbedtls_ecdh_read_public(&ssl->handshake->ecdh_ctx,
  3539. p, end - p)) != 0) {
  3540. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_read_public", ret);
  3541. return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP;
  3542. }
  3543. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3544. /* Opaque PSKs are currently only supported for PSK-only. */
  3545. if (ssl_use_opaque_psk(ssl) == 1) {
  3546. MBEDTLS_SSL_DEBUG_MSG(1, ("opaque PSK not supported with ECDHE-PSK"));
  3547. return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  3548. }
  3549. #endif
  3550. MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
  3551. MBEDTLS_DEBUG_ECDH_QP);
  3552. if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
  3553. ciphersuite_info->key_exchange)) != 0) {
  3554. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
  3555. return ret;
  3556. }
  3557. } else
  3558. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  3559. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  3560. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
  3561. if ((ret = ssl_parse_encrypted_pms(ssl, p, end, 0)) != 0) {
  3562. MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_parse_encrypted_pms_secret"), ret);
  3563. return ret;
  3564. }
  3565. } else
  3566. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
  3567. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  3568. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
  3569. ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
  3570. p, end - p);
  3571. if (ret != 0) {
  3572. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
  3573. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  3574. }
  3575. ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
  3576. ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
  3577. ssl->conf->f_rng, ssl->conf->p_rng);
  3578. if (ret != 0) {
  3579. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
  3580. return ret;
  3581. }
  3582. } else
  3583. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  3584. {
  3585. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  3586. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  3587. }
  3588. if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
  3589. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
  3590. return ret;
  3591. }
  3592. ssl->state++;
  3593. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client key exchange"));
  3594. return 0;
  3595. }
  3596. #if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
  3597. MBEDTLS_CHECK_RETURN_CRITICAL
  3598. static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl)
  3599. {
  3600. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  3601. ssl->handshake->ciphersuite_info;
  3602. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
  3603. if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
  3604. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
  3605. ssl->state++;
  3606. return 0;
  3607. }
  3608. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  3609. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  3610. }
  3611. #else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
  3612. MBEDTLS_CHECK_RETURN_CRITICAL
  3613. static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl)
  3614. {
  3615. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  3616. size_t i, sig_len;
  3617. unsigned char hash[48];
  3618. unsigned char *hash_start = hash;
  3619. size_t hashlen;
  3620. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  3621. mbedtls_pk_type_t pk_alg;
  3622. #endif
  3623. mbedtls_md_type_t md_alg;
  3624. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  3625. ssl->handshake->ciphersuite_info;
  3626. mbedtls_pk_context *peer_pk;
  3627. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
  3628. if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
  3629. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
  3630. ssl->state++;
  3631. return 0;
  3632. }
  3633. #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  3634. if (ssl->session_negotiate->peer_cert == NULL) {
  3635. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
  3636. ssl->state++;
  3637. return 0;
  3638. }
  3639. #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  3640. if (ssl->session_negotiate->peer_cert_digest == NULL) {
  3641. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
  3642. ssl->state++;
  3643. return 0;
  3644. }
  3645. #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  3646. /* Read the message without adding it to the checksum */
  3647. ret = mbedtls_ssl_read_record(ssl, 0 /* no checksum update */);
  3648. if (0 != ret) {
  3649. MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_read_record"), ret);
  3650. return ret;
  3651. }
  3652. ssl->state++;
  3653. /* Process the message contents */
  3654. if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
  3655. ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY) {
  3656. MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
  3657. return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
  3658. }
  3659. i = mbedtls_ssl_hs_hdr_len(ssl);
  3660. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  3661. peer_pk = &ssl->handshake->peer_pubkey;
  3662. #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  3663. if (ssl->session_negotiate->peer_cert == NULL) {
  3664. /* Should never happen */
  3665. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  3666. }
  3667. peer_pk = &ssl->session_negotiate->peer_cert->pk;
  3668. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  3669. /*
  3670. * struct {
  3671. * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
  3672. * opaque signature<0..2^16-1>;
  3673. * } DigitallySigned;
  3674. */
  3675. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  3676. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  3677. if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
  3678. md_alg = MBEDTLS_MD_NONE;
  3679. hashlen = 36;
  3680. /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
  3681. if (mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECDSA)) {
  3682. hash_start += 16;
  3683. hashlen -= 16;
  3684. md_alg = MBEDTLS_MD_SHA1;
  3685. }
  3686. } else
  3687. #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 ||
  3688. MBEDTLS_SSL_PROTO_TLS1_1 */
  3689. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  3690. if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
  3691. if (i + 2 > ssl->in_hslen) {
  3692. MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
  3693. return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
  3694. }
  3695. /*
  3696. * Hash
  3697. */
  3698. md_alg = mbedtls_ssl_md_alg_from_hash(ssl->in_msg[i]);
  3699. if (md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md(ssl, ssl->in_msg[i])) {
  3700. MBEDTLS_SSL_DEBUG_MSG(1, ("peer not adhering to requested sig_alg"
  3701. " for verify message"));
  3702. return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
  3703. }
  3704. #if !defined(MBEDTLS_MD_SHA1)
  3705. if (MBEDTLS_MD_SHA1 == md_alg) {
  3706. hash_start += 16;
  3707. }
  3708. #endif
  3709. /* Info from md_alg will be used instead */
  3710. hashlen = 0;
  3711. i++;
  3712. /*
  3713. * Signature
  3714. */
  3715. if ((pk_alg = mbedtls_ssl_pk_alg_from_sig(ssl->in_msg[i]))
  3716. == MBEDTLS_PK_NONE) {
  3717. MBEDTLS_SSL_DEBUG_MSG(1, ("peer not adhering to requested sig_alg"
  3718. " for verify message"));
  3719. return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
  3720. }
  3721. /*
  3722. * Check the certificate's key type matches the signature alg
  3723. */
  3724. if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
  3725. MBEDTLS_SSL_DEBUG_MSG(1, ("sig_alg doesn't match cert key"));
  3726. return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
  3727. }
  3728. i++;
  3729. } else
  3730. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  3731. {
  3732. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  3733. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  3734. }
  3735. if (i + 2 > ssl->in_hslen) {
  3736. MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
  3737. return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
  3738. }
  3739. sig_len = (ssl->in_msg[i] << 8) | ssl->in_msg[i+1];
  3740. i += 2;
  3741. if (i + sig_len != ssl->in_hslen) {
  3742. MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
  3743. return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY;
  3744. }
  3745. /* Calculate hash and verify signature */
  3746. {
  3747. size_t dummy_hlen;
  3748. ssl->handshake->calc_verify(ssl, hash, &dummy_hlen);
  3749. }
  3750. if ((ret = mbedtls_pk_verify(peer_pk,
  3751. md_alg, hash_start, hashlen,
  3752. ssl->in_msg + i, sig_len)) != 0) {
  3753. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
  3754. return ret;
  3755. }
  3756. mbedtls_ssl_update_handshake_status(ssl);
  3757. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate verify"));
  3758. return ret;
  3759. }
  3760. #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
  3761. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  3762. MBEDTLS_CHECK_RETURN_CRITICAL
  3763. static int ssl_write_new_session_ticket(mbedtls_ssl_context *ssl)
  3764. {
  3765. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3766. size_t tlen;
  3767. uint32_t lifetime;
  3768. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write new session ticket"));
  3769. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  3770. ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
  3771. /*
  3772. * struct {
  3773. * uint32 ticket_lifetime_hint;
  3774. * opaque ticket<0..2^16-1>;
  3775. * } NewSessionTicket;
  3776. *
  3777. * 4 . 7 ticket_lifetime_hint (0 = unspecified)
  3778. * 8 . 9 ticket_len (n)
  3779. * 10 . 9+n ticket content
  3780. */
  3781. if ((ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
  3782. ssl->session_negotiate,
  3783. ssl->out_msg + 10,
  3784. ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
  3785. &tlen, &lifetime)) != 0) {
  3786. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_ticket_write", ret);
  3787. tlen = 0;
  3788. }
  3789. MBEDTLS_PUT_UINT32_BE(lifetime, ssl->out_msg, 4);
  3790. MBEDTLS_PUT_UINT16_BE(tlen, ssl->out_msg, 8);
  3791. ssl->out_msglen = 10 + tlen;
  3792. /*
  3793. * Morally equivalent to updating ssl->state, but NewSessionTicket and
  3794. * ChangeCipherSpec share the same state.
  3795. */
  3796. ssl->handshake->new_session_ticket = 0;
  3797. if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
  3798. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
  3799. return ret;
  3800. }
  3801. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
  3802. return 0;
  3803. }
  3804. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  3805. /*
  3806. * SSL handshake -- server side -- single step
  3807. */
  3808. int mbedtls_ssl_handshake_server_step(mbedtls_ssl_context *ssl)
  3809. {
  3810. int ret = 0;
  3811. if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
  3812. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  3813. }
  3814. MBEDTLS_SSL_DEBUG_MSG(2, ("server state: %d", ssl->state));
  3815. if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
  3816. return ret;
  3817. }
  3818. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  3819. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  3820. ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
  3821. if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
  3822. return ret;
  3823. }
  3824. }
  3825. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  3826. switch (ssl->state) {
  3827. case MBEDTLS_SSL_HELLO_REQUEST:
  3828. ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
  3829. break;
  3830. /*
  3831. * <== ClientHello
  3832. */
  3833. case MBEDTLS_SSL_CLIENT_HELLO:
  3834. ret = ssl_parse_client_hello(ssl);
  3835. break;
  3836. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  3837. case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
  3838. return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED;
  3839. #endif
  3840. /*
  3841. * ==> ServerHello
  3842. * Certificate
  3843. * ( ServerKeyExchange )
  3844. * ( CertificateRequest )
  3845. * ServerHelloDone
  3846. */
  3847. case MBEDTLS_SSL_SERVER_HELLO:
  3848. ret = ssl_write_server_hello(ssl);
  3849. break;
  3850. case MBEDTLS_SSL_SERVER_CERTIFICATE:
  3851. ret = mbedtls_ssl_write_certificate(ssl);
  3852. break;
  3853. case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
  3854. ret = ssl_write_server_key_exchange(ssl);
  3855. break;
  3856. case MBEDTLS_SSL_CERTIFICATE_REQUEST:
  3857. ret = ssl_write_certificate_request(ssl);
  3858. break;
  3859. case MBEDTLS_SSL_SERVER_HELLO_DONE:
  3860. ret = ssl_write_server_hello_done(ssl);
  3861. break;
  3862. /*
  3863. * <== ( Certificate/Alert )
  3864. * ClientKeyExchange
  3865. * ( CertificateVerify )
  3866. * ChangeCipherSpec
  3867. * Finished
  3868. */
  3869. case MBEDTLS_SSL_CLIENT_CERTIFICATE:
  3870. ret = mbedtls_ssl_parse_certificate(ssl);
  3871. break;
  3872. case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
  3873. ret = ssl_parse_client_key_exchange(ssl);
  3874. break;
  3875. case MBEDTLS_SSL_CERTIFICATE_VERIFY:
  3876. ret = ssl_parse_certificate_verify(ssl);
  3877. break;
  3878. case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
  3879. ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
  3880. break;
  3881. case MBEDTLS_SSL_CLIENT_FINISHED:
  3882. ret = mbedtls_ssl_parse_finished(ssl);
  3883. break;
  3884. /*
  3885. * ==> ( NewSessionTicket )
  3886. * ChangeCipherSpec
  3887. * Finished
  3888. */
  3889. case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
  3890. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  3891. if (ssl->handshake->new_session_ticket != 0) {
  3892. ret = ssl_write_new_session_ticket(ssl);
  3893. } else
  3894. #endif
  3895. ret = mbedtls_ssl_write_change_cipher_spec(ssl);
  3896. break;
  3897. case MBEDTLS_SSL_SERVER_FINISHED:
  3898. ret = mbedtls_ssl_write_finished(ssl);
  3899. break;
  3900. case MBEDTLS_SSL_FLUSH_BUFFERS:
  3901. MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
  3902. ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
  3903. break;
  3904. case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
  3905. mbedtls_ssl_handshake_wrapup(ssl);
  3906. break;
  3907. default:
  3908. MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
  3909. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  3910. }
  3911. return ret;
  3912. }
  3913. #endif /* MBEDTLS_SSL_SRV_C */