ssl_tls12_server.c 157 KB

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