ssl_cli.c 150 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397
  1. /*
  2. * SSLv3/TLSv1 client-side functions
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "common.h"
  20. #if defined(MBEDTLS_SSL_CLI_C)
  21. #include "mbedtls/platform.h"
  22. #include "mbedtls/ssl.h"
  23. #include "mbedtls/ssl_internal.h"
  24. #include "mbedtls/debug.h"
  25. #include "mbedtls/error.h"
  26. #include "mbedtls/constant_time.h"
  27. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  28. #include "mbedtls/psa_util.h"
  29. #include "psa/crypto.h"
  30. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  31. #include <string.h>
  32. #include <stdint.h>
  33. #if defined(MBEDTLS_HAVE_TIME)
  34. #include "mbedtls/platform_time.h"
  35. #endif
  36. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  37. #include "mbedtls/platform_util.h"
  38. #endif
  39. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  40. MBEDTLS_CHECK_RETURN_CRITICAL
  41. static int ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
  42. {
  43. if (conf->psk_identity == NULL ||
  44. conf->psk_identity_len == 0) {
  45. return 0;
  46. }
  47. if (conf->psk != NULL && conf->psk_len != 0) {
  48. return 1;
  49. }
  50. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  51. if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
  52. return 1;
  53. }
  54. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  55. return 0;
  56. }
  57. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  58. MBEDTLS_CHECK_RETURN_CRITICAL
  59. static int ssl_conf_has_static_raw_psk(mbedtls_ssl_config const *conf)
  60. {
  61. if (conf->psk_identity == NULL ||
  62. conf->psk_identity_len == 0) {
  63. return 0;
  64. }
  65. if (conf->psk != NULL && conf->psk_len != 0) {
  66. return 1;
  67. }
  68. return 0;
  69. }
  70. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  71. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  72. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  73. MBEDTLS_CHECK_RETURN_CRITICAL
  74. static int ssl_write_hostname_ext(mbedtls_ssl_context *ssl,
  75. unsigned char *buf,
  76. const unsigned char *end,
  77. size_t *olen)
  78. {
  79. unsigned char *p = buf;
  80. size_t hostname_len;
  81. *olen = 0;
  82. if (ssl->hostname == NULL) {
  83. return 0;
  84. }
  85. MBEDTLS_SSL_DEBUG_MSG(3,
  86. ("client hello, adding server name extension: %s",
  87. ssl->hostname));
  88. hostname_len = strlen(ssl->hostname);
  89. MBEDTLS_SSL_CHK_BUF_PTR(p, end, hostname_len + 9);
  90. /*
  91. * Sect. 3, RFC 6066 (TLS Extensions Definitions)
  92. *
  93. * In order to provide any of the server names, clients MAY include an
  94. * extension of type "server_name" in the (extended) client hello. The
  95. * "extension_data" field of this extension SHALL contain
  96. * "ServerNameList" where:
  97. *
  98. * struct {
  99. * NameType name_type;
  100. * select (name_type) {
  101. * case host_name: HostName;
  102. * } name;
  103. * } ServerName;
  104. *
  105. * enum {
  106. * host_name(0), (255)
  107. * } NameType;
  108. *
  109. * opaque HostName<1..2^16-1>;
  110. *
  111. * struct {
  112. * ServerName server_name_list<1..2^16-1>
  113. * } ServerNameList;
  114. *
  115. */
  116. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SERVERNAME, p, 0);
  117. p += 2;
  118. MBEDTLS_PUT_UINT16_BE(hostname_len + 5, p, 0);
  119. p += 2;
  120. MBEDTLS_PUT_UINT16_BE(hostname_len + 3, p, 0);
  121. p += 2;
  122. *p++ = MBEDTLS_BYTE_0(MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME);
  123. MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
  124. p += 2;
  125. memcpy(p, ssl->hostname, hostname_len);
  126. *olen = hostname_len + 9;
  127. return 0;
  128. }
  129. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  130. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  131. MBEDTLS_CHECK_RETURN_CRITICAL
  132. static int ssl_write_renegotiation_ext(mbedtls_ssl_context *ssl,
  133. unsigned char *buf,
  134. const unsigned char *end,
  135. size_t *olen)
  136. {
  137. unsigned char *p = buf;
  138. *olen = 0;
  139. /* We're always including a TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
  140. * initial ClientHello, in which case also adding the renegotiation
  141. * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
  142. if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
  143. return 0;
  144. }
  145. MBEDTLS_SSL_DEBUG_MSG(3,
  146. ("client hello, adding renegotiation extension"));
  147. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5 + ssl->verify_data_len);
  148. /*
  149. * Secure renegotiation
  150. */
  151. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0);
  152. p += 2;
  153. *p++ = 0x00;
  154. *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len + 1);
  155. *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len);
  156. memcpy(p, ssl->own_verify_data, ssl->verify_data_len);
  157. *olen = 5 + ssl->verify_data_len;
  158. return 0;
  159. }
  160. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  161. /*
  162. * Only if we handle at least one key exchange that needs signatures.
  163. */
  164. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  165. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  166. MBEDTLS_CHECK_RETURN_CRITICAL
  167. static int ssl_write_signature_algorithms_ext(mbedtls_ssl_context *ssl,
  168. unsigned char *buf,
  169. const unsigned char *end,
  170. size_t *olen)
  171. {
  172. unsigned char *p = buf;
  173. size_t sig_alg_len = 0;
  174. const int *md;
  175. #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C)
  176. unsigned char *sig_alg_list = buf + 6;
  177. #endif
  178. *olen = 0;
  179. if (ssl->conf->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
  180. return 0;
  181. }
  182. MBEDTLS_SSL_DEBUG_MSG(3,
  183. ("client hello, adding signature_algorithms extension"));
  184. if (ssl->conf->sig_hashes == NULL) {
  185. return MBEDTLS_ERR_SSL_BAD_CONFIG;
  186. }
  187. for (md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
  188. #if defined(MBEDTLS_ECDSA_C)
  189. sig_alg_len += 2;
  190. #endif
  191. #if defined(MBEDTLS_RSA_C)
  192. sig_alg_len += 2;
  193. #endif
  194. if (sig_alg_len > MBEDTLS_SSL_MAX_SIG_HASH_ALG_LIST_LEN) {
  195. MBEDTLS_SSL_DEBUG_MSG(3,
  196. ("length in bytes of sig-hash-alg extension too big"));
  197. return MBEDTLS_ERR_SSL_BAD_CONFIG;
  198. }
  199. }
  200. /* Empty signature algorithms list, this is a configuration error. */
  201. if (sig_alg_len == 0) {
  202. return MBEDTLS_ERR_SSL_BAD_CONFIG;
  203. }
  204. MBEDTLS_SSL_CHK_BUF_PTR(p, end, sig_alg_len + 6);
  205. /*
  206. * Prepare signature_algorithms extension (TLS 1.2)
  207. */
  208. sig_alg_len = 0;
  209. for (md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
  210. #if defined(MBEDTLS_ECDSA_C)
  211. sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg(*md);
  212. sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_ECDSA;
  213. #endif
  214. #if defined(MBEDTLS_RSA_C)
  215. sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg(*md);
  216. sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_RSA;
  217. #endif
  218. }
  219. /*
  220. * enum {
  221. * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
  222. * sha512(6), (255)
  223. * } HashAlgorithm;
  224. *
  225. * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
  226. * SignatureAlgorithm;
  227. *
  228. * struct {
  229. * HashAlgorithm hash;
  230. * SignatureAlgorithm signature;
  231. * } SignatureAndHashAlgorithm;
  232. *
  233. * SignatureAndHashAlgorithm
  234. * supported_signature_algorithms<2..2^16-2>;
  235. */
  236. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, p, 0);
  237. p += 2;
  238. MBEDTLS_PUT_UINT16_BE(sig_alg_len + 2, p, 0);
  239. p += 2;
  240. MBEDTLS_PUT_UINT16_BE(sig_alg_len, p, 0);
  241. p += 2;
  242. *olen = 6 + sig_alg_len;
  243. return 0;
  244. }
  245. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
  246. MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  247. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  248. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  249. MBEDTLS_CHECK_RETURN_CRITICAL
  250. static int ssl_write_supported_elliptic_curves_ext(mbedtls_ssl_context *ssl,
  251. unsigned char *buf,
  252. const unsigned char *end,
  253. size_t *olen)
  254. {
  255. unsigned char *p = buf;
  256. unsigned char *elliptic_curve_list = p + 6;
  257. size_t elliptic_curve_len = 0;
  258. const mbedtls_ecp_curve_info *info;
  259. const mbedtls_ecp_group_id *grp_id;
  260. *olen = 0;
  261. MBEDTLS_SSL_DEBUG_MSG(3,
  262. ("client hello, adding supported_elliptic_curves extension"));
  263. if (ssl->conf->curve_list == NULL) {
  264. return MBEDTLS_ERR_SSL_BAD_CONFIG;
  265. }
  266. for (grp_id = ssl->conf->curve_list;
  267. *grp_id != MBEDTLS_ECP_DP_NONE;
  268. grp_id++) {
  269. info = mbedtls_ecp_curve_info_from_grp_id(*grp_id);
  270. if (info == NULL) {
  271. MBEDTLS_SSL_DEBUG_MSG(1,
  272. ("invalid curve in ssl configuration"));
  273. return MBEDTLS_ERR_SSL_BAD_CONFIG;
  274. }
  275. elliptic_curve_len += 2;
  276. if (elliptic_curve_len > MBEDTLS_SSL_MAX_CURVE_LIST_LEN) {
  277. MBEDTLS_SSL_DEBUG_MSG(3,
  278. ("malformed supported_elliptic_curves extension in config"));
  279. return MBEDTLS_ERR_SSL_BAD_CONFIG;
  280. }
  281. }
  282. /* Empty elliptic curve list, this is a configuration error. */
  283. if (elliptic_curve_len == 0) {
  284. return MBEDTLS_ERR_SSL_BAD_CONFIG;
  285. }
  286. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6 + elliptic_curve_len);
  287. elliptic_curve_len = 0;
  288. for (grp_id = ssl->conf->curve_list;
  289. *grp_id != MBEDTLS_ECP_DP_NONE;
  290. grp_id++) {
  291. info = mbedtls_ecp_curve_info_from_grp_id(*grp_id);
  292. elliptic_curve_list[elliptic_curve_len++] = MBEDTLS_BYTE_1(info->tls_id);
  293. elliptic_curve_list[elliptic_curve_len++] = MBEDTLS_BYTE_0(info->tls_id);
  294. }
  295. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES, p, 0);
  296. p += 2;
  297. MBEDTLS_PUT_UINT16_BE(elliptic_curve_len + 2, p, 0);
  298. p += 2;
  299. MBEDTLS_PUT_UINT16_BE(elliptic_curve_len, p, 0);
  300. p += 2;
  301. *olen = 6 + elliptic_curve_len;
  302. return 0;
  303. }
  304. MBEDTLS_CHECK_RETURN_CRITICAL
  305. static int ssl_write_supported_point_formats_ext(mbedtls_ssl_context *ssl,
  306. unsigned char *buf,
  307. const unsigned char *end,
  308. size_t *olen)
  309. {
  310. unsigned char *p = buf;
  311. (void) ssl; /* ssl used for debugging only */
  312. *olen = 0;
  313. MBEDTLS_SSL_DEBUG_MSG(3,
  314. ("client hello, adding supported_point_formats extension"));
  315. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
  316. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0);
  317. p += 2;
  318. *p++ = 0x00;
  319. *p++ = 2;
  320. *p++ = 1;
  321. *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
  322. *olen = 6;
  323. return 0;
  324. }
  325. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
  326. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  327. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  328. MBEDTLS_CHECK_RETURN_CRITICAL
  329. static int ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
  330. unsigned char *buf,
  331. const unsigned char *end,
  332. size_t *olen)
  333. {
  334. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  335. unsigned char *p = buf;
  336. size_t kkpp_len;
  337. *olen = 0;
  338. /* Skip costly extension if we can't use EC J-PAKE anyway */
  339. if (mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) {
  340. return 0;
  341. }
  342. MBEDTLS_SSL_DEBUG_MSG(3,
  343. ("client hello, adding ecjpake_kkpp extension"));
  344. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
  345. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0);
  346. p += 2;
  347. /*
  348. * We may need to send ClientHello multiple times for Hello verification.
  349. * We don't want to compute fresh values every time (both for performance
  350. * and consistency reasons), so cache the extension content.
  351. */
  352. if (ssl->handshake->ecjpake_cache == NULL ||
  353. ssl->handshake->ecjpake_cache_len == 0) {
  354. MBEDTLS_SSL_DEBUG_MSG(3, ("generating new ecjpake parameters"));
  355. ret = mbedtls_ecjpake_write_round_one(&ssl->handshake->ecjpake_ctx,
  356. p + 2, end - p - 2, &kkpp_len,
  357. ssl->conf->f_rng, ssl->conf->p_rng);
  358. if (ret != 0) {
  359. MBEDTLS_SSL_DEBUG_RET(1,
  360. "mbedtls_ecjpake_write_round_one", ret);
  361. return ret;
  362. }
  363. ssl->handshake->ecjpake_cache = mbedtls_calloc(1, kkpp_len);
  364. if (ssl->handshake->ecjpake_cache == NULL) {
  365. MBEDTLS_SSL_DEBUG_MSG(1, ("allocation failed"));
  366. return MBEDTLS_ERR_SSL_ALLOC_FAILED;
  367. }
  368. memcpy(ssl->handshake->ecjpake_cache, p + 2, kkpp_len);
  369. ssl->handshake->ecjpake_cache_len = kkpp_len;
  370. } else {
  371. MBEDTLS_SSL_DEBUG_MSG(3, ("re-using cached ecjpake parameters"));
  372. kkpp_len = ssl->handshake->ecjpake_cache_len;
  373. MBEDTLS_SSL_CHK_BUF_PTR(p + 2, end, kkpp_len);
  374. memcpy(p + 2, ssl->handshake->ecjpake_cache, kkpp_len);
  375. }
  376. MBEDTLS_PUT_UINT16_BE(kkpp_len, p, 0);
  377. p += 2;
  378. *olen = kkpp_len + 4;
  379. return 0;
  380. }
  381. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  382. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  383. MBEDTLS_CHECK_RETURN_CRITICAL
  384. static int ssl_write_cid_ext(mbedtls_ssl_context *ssl,
  385. unsigned char *buf,
  386. const unsigned char *end,
  387. size_t *olen)
  388. {
  389. unsigned char *p = buf;
  390. size_t ext_len;
  391. /*
  392. * Quoting draft-ietf-tls-dtls-connection-id-05
  393. * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
  394. *
  395. * struct {
  396. * opaque cid<0..2^8-1>;
  397. * } ConnectionId;
  398. */
  399. *olen = 0;
  400. if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
  401. ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
  402. return 0;
  403. }
  404. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding CID extension"));
  405. /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
  406. * which is at most 255, so the increment cannot overflow. */
  407. MBEDTLS_SSL_CHK_BUF_PTR(p, end, (unsigned) (ssl->own_cid_len + 5));
  408. /* Add extension ID + size */
  409. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_CID, p, 0);
  410. p += 2;
  411. ext_len = (size_t) ssl->own_cid_len + 1;
  412. MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
  413. p += 2;
  414. *p++ = (uint8_t) ssl->own_cid_len;
  415. memcpy(p, ssl->own_cid, ssl->own_cid_len);
  416. *olen = ssl->own_cid_len + 5;
  417. return 0;
  418. }
  419. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  420. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  421. MBEDTLS_CHECK_RETURN_CRITICAL
  422. static int ssl_write_max_fragment_length_ext(mbedtls_ssl_context *ssl,
  423. unsigned char *buf,
  424. const unsigned char *end,
  425. size_t *olen)
  426. {
  427. unsigned char *p = buf;
  428. *olen = 0;
  429. if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE) {
  430. return 0;
  431. }
  432. MBEDTLS_SSL_DEBUG_MSG(3,
  433. ("client hello, adding max_fragment_length extension"));
  434. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5);
  435. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0);
  436. p += 2;
  437. *p++ = 0x00;
  438. *p++ = 1;
  439. *p++ = ssl->conf->mfl_code;
  440. *olen = 5;
  441. return 0;
  442. }
  443. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  444. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  445. MBEDTLS_CHECK_RETURN_CRITICAL
  446. static int ssl_write_truncated_hmac_ext(mbedtls_ssl_context *ssl,
  447. unsigned char *buf,
  448. const unsigned char *end,
  449. size_t *olen)
  450. {
  451. unsigned char *p = buf;
  452. *olen = 0;
  453. if (ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED) {
  454. return 0;
  455. }
  456. MBEDTLS_SSL_DEBUG_MSG(3,
  457. ("client hello, adding truncated_hmac extension"));
  458. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
  459. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_TRUNCATED_HMAC, p, 0);
  460. p += 2;
  461. *p++ = 0x00;
  462. *p++ = 0x00;
  463. *olen = 4;
  464. return 0;
  465. }
  466. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  467. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  468. MBEDTLS_CHECK_RETURN_CRITICAL
  469. static int ssl_write_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
  470. unsigned char *buf,
  471. const unsigned char *end,
  472. size_t *olen)
  473. {
  474. unsigned char *p = buf;
  475. *olen = 0;
  476. if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
  477. ssl->conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
  478. return 0;
  479. }
  480. MBEDTLS_SSL_DEBUG_MSG(3,
  481. ("client hello, adding encrypt_then_mac extension"));
  482. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
  483. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0);
  484. p += 2;
  485. *p++ = 0x00;
  486. *p++ = 0x00;
  487. *olen = 4;
  488. return 0;
  489. }
  490. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  491. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  492. MBEDTLS_CHECK_RETURN_CRITICAL
  493. static int ssl_write_extended_ms_ext(mbedtls_ssl_context *ssl,
  494. unsigned char *buf,
  495. const unsigned char *end,
  496. size_t *olen)
  497. {
  498. unsigned char *p = buf;
  499. *olen = 0;
  500. if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
  501. ssl->conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
  502. return 0;
  503. }
  504. MBEDTLS_SSL_DEBUG_MSG(3,
  505. ("client hello, adding extended_master_secret extension"));
  506. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
  507. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0);
  508. p += 2;
  509. *p++ = 0x00;
  510. *p++ = 0x00;
  511. *olen = 4;
  512. return 0;
  513. }
  514. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  515. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  516. MBEDTLS_CHECK_RETURN_CRITICAL
  517. static int ssl_write_session_ticket_ext(mbedtls_ssl_context *ssl,
  518. unsigned char *buf,
  519. const unsigned char *end,
  520. size_t *olen)
  521. {
  522. unsigned char *p = buf;
  523. size_t tlen = ssl->session_negotiate->ticket_len;
  524. *olen = 0;
  525. if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED) {
  526. return 0;
  527. }
  528. MBEDTLS_SSL_DEBUG_MSG(3,
  529. ("client hello, adding session ticket extension"));
  530. /* The addition is safe here since the ticket length is 16 bit. */
  531. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + tlen);
  532. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0);
  533. p += 2;
  534. MBEDTLS_PUT_UINT16_BE(tlen, p, 0);
  535. p += 2;
  536. *olen = 4;
  537. if (ssl->session_negotiate->ticket == NULL || tlen == 0) {
  538. return 0;
  539. }
  540. MBEDTLS_SSL_DEBUG_MSG(3,
  541. ("sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen));
  542. memcpy(p, ssl->session_negotiate->ticket, tlen);
  543. *olen += tlen;
  544. return 0;
  545. }
  546. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  547. #if defined(MBEDTLS_SSL_ALPN)
  548. MBEDTLS_CHECK_RETURN_CRITICAL
  549. static int ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
  550. unsigned char *buf,
  551. const unsigned char *end,
  552. size_t *olen)
  553. {
  554. unsigned char *p = buf;
  555. size_t alpnlen = 0;
  556. const char **cur;
  557. *olen = 0;
  558. if (ssl->conf->alpn_list == NULL) {
  559. return 0;
  560. }
  561. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding alpn extension"));
  562. for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
  563. alpnlen += strlen(*cur) + 1;
  564. }
  565. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6 + alpnlen);
  566. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
  567. p += 2;
  568. /*
  569. * opaque ProtocolName<1..2^8-1>;
  570. *
  571. * struct {
  572. * ProtocolName protocol_name_list<2..2^16-1>
  573. * } ProtocolNameList;
  574. */
  575. /* Skip writing extension and list length for now */
  576. p += 4;
  577. for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
  578. /*
  579. * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
  580. * protocol names is less than 255.
  581. */
  582. *p = (unsigned char) strlen(*cur);
  583. memcpy(p + 1, *cur, *p);
  584. p += 1 + *p;
  585. }
  586. *olen = p - buf;
  587. /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
  588. MBEDTLS_PUT_UINT16_BE(*olen - 6, buf, 4);
  589. /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
  590. MBEDTLS_PUT_UINT16_BE(*olen - 4, buf, 2);
  591. return 0;
  592. }
  593. #endif /* MBEDTLS_SSL_ALPN */
  594. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  595. MBEDTLS_CHECK_RETURN_CRITICAL
  596. static int ssl_write_use_srtp_ext(mbedtls_ssl_context *ssl,
  597. unsigned char *buf,
  598. const unsigned char *end,
  599. size_t *olen)
  600. {
  601. unsigned char *p = buf;
  602. size_t protection_profiles_index = 0, ext_len = 0;
  603. uint16_t mki_len = 0, profile_value = 0;
  604. *olen = 0;
  605. if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
  606. (ssl->conf->dtls_srtp_profile_list == NULL) ||
  607. (ssl->conf->dtls_srtp_profile_list_len == 0)) {
  608. return 0;
  609. }
  610. /* RFC 5764 section 4.1.1
  611. * uint8 SRTPProtectionProfile[2];
  612. *
  613. * struct {
  614. * SRTPProtectionProfiles SRTPProtectionProfiles;
  615. * opaque srtp_mki<0..255>;
  616. * } UseSRTPData;
  617. * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
  618. */
  619. if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
  620. mki_len = ssl->dtls_srtp_info.mki_len;
  621. }
  622. /* Extension length = 2 bytes for profiles length,
  623. * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
  624. * 1 byte for srtp_mki vector length and the mki_len value
  625. */
  626. ext_len = 2 + 2 * (ssl->conf->dtls_srtp_profile_list_len) + 1 + mki_len;
  627. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding use_srtp extension"));
  628. /* Check there is room in the buffer for the extension + 4 bytes
  629. * - the extension tag (2 bytes)
  630. * - the extension length (2 bytes)
  631. */
  632. MBEDTLS_SSL_CHK_BUF_PTR(p, end, ext_len + 4);
  633. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, p, 0);
  634. p += 2;
  635. MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
  636. p += 2;
  637. /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
  638. /* micro-optimization:
  639. * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
  640. * which is lower than 127, so the upper byte of the length is always 0
  641. * For the documentation, the more generic code is left in comments
  642. * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
  643. * >> 8 ) & 0xFF );
  644. */
  645. *p++ = 0;
  646. *p++ = MBEDTLS_BYTE_0(2 * ssl->conf->dtls_srtp_profile_list_len);
  647. for (protection_profiles_index = 0;
  648. protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
  649. protection_profiles_index++) {
  650. profile_value = mbedtls_ssl_check_srtp_profile_value
  651. (ssl->conf->dtls_srtp_profile_list[protection_profiles_index]);
  652. if (profile_value != MBEDTLS_TLS_SRTP_UNSET) {
  653. MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_write_use_srtp_ext, add profile: %04x",
  654. profile_value));
  655. MBEDTLS_PUT_UINT16_BE(profile_value, p, 0);
  656. p += 2;
  657. } else {
  658. /*
  659. * Note: we shall never arrive here as protection profiles
  660. * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
  661. */
  662. MBEDTLS_SSL_DEBUG_MSG(3,
  663. ("client hello, "
  664. "illegal DTLS-SRTP protection profile %d",
  665. ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
  666. ));
  667. return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  668. }
  669. }
  670. *p++ = mki_len & 0xFF;
  671. if (mki_len != 0) {
  672. memcpy(p, ssl->dtls_srtp_info.mki_value, mki_len);
  673. /*
  674. * Increment p to point to the current position.
  675. */
  676. p += mki_len;
  677. MBEDTLS_SSL_DEBUG_BUF(3, "sending mki", ssl->dtls_srtp_info.mki_value,
  678. ssl->dtls_srtp_info.mki_len);
  679. }
  680. /*
  681. * total extension length: extension type (2 bytes)
  682. * + extension length (2 bytes)
  683. * + protection profile length (2 bytes)
  684. * + 2 * number of protection profiles
  685. * + srtp_mki vector length(1 byte)
  686. * + mki value
  687. */
  688. *olen = p - buf;
  689. return 0;
  690. }
  691. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  692. /*
  693. * Generate random bytes for ClientHello
  694. */
  695. MBEDTLS_CHECK_RETURN_CRITICAL
  696. static int ssl_generate_random(mbedtls_ssl_context *ssl)
  697. {
  698. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  699. unsigned char *p = ssl->handshake->randbytes;
  700. #if defined(MBEDTLS_HAVE_TIME)
  701. mbedtls_time_t t;
  702. #endif
  703. /*
  704. * When responding to a verify request, MUST reuse random (RFC 6347 4.2.1)
  705. */
  706. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  707. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  708. ssl->handshake->verify_cookie != NULL) {
  709. return 0;
  710. }
  711. #endif
  712. #if defined(MBEDTLS_HAVE_TIME)
  713. t = mbedtls_time(NULL);
  714. MBEDTLS_PUT_UINT32_BE(t, p, 0);
  715. p += 4;
  716. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
  717. (long long) t));
  718. #else
  719. if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p, 4)) != 0) {
  720. return ret;
  721. }
  722. p += 4;
  723. #endif /* MBEDTLS_HAVE_TIME */
  724. if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p, 28)) != 0) {
  725. return ret;
  726. }
  727. return 0;
  728. }
  729. /**
  730. * \brief Validate cipher suite against config in SSL context.
  731. *
  732. * \param suite_info cipher suite to validate
  733. * \param ssl SSL context
  734. * \param min_minor_ver Minimal minor version to accept a cipher suite
  735. * \param max_minor_ver Maximal minor version to accept a cipher suite
  736. *
  737. * \return 0 if valid, else 1
  738. */
  739. MBEDTLS_CHECK_RETURN_CRITICAL
  740. static int ssl_validate_ciphersuite(
  741. const mbedtls_ssl_ciphersuite_t *suite_info,
  742. const mbedtls_ssl_context *ssl,
  743. int min_minor_ver, int max_minor_ver)
  744. {
  745. (void) ssl;
  746. if (suite_info == NULL) {
  747. return 1;
  748. }
  749. if (suite_info->min_minor_ver > max_minor_ver ||
  750. suite_info->max_minor_ver < min_minor_ver) {
  751. return 1;
  752. }
  753. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  754. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  755. (suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS)) {
  756. return 1;
  757. }
  758. #endif
  759. #if defined(MBEDTLS_ARC4_C)
  760. if (ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
  761. suite_info->cipher == MBEDTLS_CIPHER_ARC4_128) {
  762. return 1;
  763. }
  764. #endif
  765. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  766. if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
  767. mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) {
  768. return 1;
  769. }
  770. #endif
  771. /* Don't suggest PSK-based ciphersuite if no PSK is available. */
  772. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  773. if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
  774. ssl_conf_has_static_psk(ssl->conf) == 0) {
  775. return 1;
  776. }
  777. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  778. return 0;
  779. }
  780. MBEDTLS_CHECK_RETURN_CRITICAL
  781. static int ssl_write_client_hello(mbedtls_ssl_context *ssl)
  782. {
  783. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  784. size_t i, n, olen, ext_len = 0;
  785. unsigned char *buf;
  786. unsigned char *p, *q;
  787. const unsigned char *end;
  788. unsigned char offer_compress;
  789. const int *ciphersuites;
  790. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  791. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  792. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  793. int uses_ec = 0;
  794. #endif
  795. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client hello"));
  796. if (ssl->conf->f_rng == NULL) {
  797. MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
  798. return MBEDTLS_ERR_SSL_NO_RNG;
  799. }
  800. int renegotiating = 0;
  801. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  802. if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
  803. renegotiating = 1;
  804. }
  805. #endif
  806. if (!renegotiating) {
  807. ssl->major_ver = ssl->conf->min_major_ver;
  808. ssl->minor_ver = ssl->conf->min_minor_ver;
  809. }
  810. if (ssl->conf->max_major_ver == 0) {
  811. MBEDTLS_SSL_DEBUG_MSG(1,
  812. (
  813. "configured max major version is invalid, consider using mbedtls_ssl_config_defaults()"));
  814. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  815. }
  816. buf = ssl->out_msg;
  817. end = buf + MBEDTLS_SSL_OUT_CONTENT_LEN;
  818. /*
  819. * Check if there's enough space for the first part of the ClientHello
  820. * consisting of the 38 bytes described below, the session identifier (at
  821. * most 32 bytes) and its length (1 byte).
  822. *
  823. * Use static upper bounds instead of the actual values
  824. * to allow the compiler to optimize this away.
  825. */
  826. MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 38 + 1 + 32);
  827. /*
  828. * The 38 first bytes of the ClientHello:
  829. * 0 . 0 handshake type (written later)
  830. * 1 . 3 handshake length (written later)
  831. * 4 . 5 highest version supported
  832. * 6 . 9 current UNIX time
  833. * 10 . 37 random bytes
  834. *
  835. * The current UNIX time (4 bytes) and following 28 random bytes are written
  836. * by ssl_generate_random() into ssl->handshake->randbytes buffer and then
  837. * copied from there into the output buffer.
  838. */
  839. p = buf + 4;
  840. mbedtls_ssl_write_version(ssl->conf->max_major_ver,
  841. ssl->conf->max_minor_ver,
  842. ssl->conf->transport, p);
  843. p += 2;
  844. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, max version: [%d:%d]",
  845. buf[4], buf[5]));
  846. if ((ret = ssl_generate_random(ssl)) != 0) {
  847. MBEDTLS_SSL_DEBUG_RET(1, "ssl_generate_random", ret);
  848. return ret;
  849. }
  850. memcpy(p, ssl->handshake->randbytes, 32);
  851. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes", p, 32);
  852. p += 32;
  853. /*
  854. * 38 . 38 session id length
  855. * 39 . 39+n session id
  856. * 39+n . 39+n DTLS only: cookie length (1 byte)
  857. * 40+n . .. DTLS only: cookie
  858. * .. . .. ciphersuitelist length (2 bytes)
  859. * .. . .. ciphersuitelist
  860. * .. . .. compression methods length (1 byte)
  861. * .. . .. compression methods
  862. * .. . .. extensions length (2 bytes)
  863. * .. . .. extensions
  864. */
  865. n = ssl->session_negotiate->id_len;
  866. if (n < 16 || n > 32 ||
  867. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  868. ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
  869. #endif
  870. ssl->handshake->resume == 0) {
  871. n = 0;
  872. }
  873. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  874. /*
  875. * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
  876. * generate and include a Session ID in the TLS ClientHello."
  877. */
  878. if (!renegotiating) {
  879. if (ssl->session_negotiate->ticket != NULL &&
  880. ssl->session_negotiate->ticket_len != 0) {
  881. ret = ssl->conf->f_rng(ssl->conf->p_rng,
  882. ssl->session_negotiate->id, 32);
  883. if (ret != 0) {
  884. return ret;
  885. }
  886. ssl->session_negotiate->id_len = n = 32;
  887. }
  888. }
  889. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  890. /*
  891. * The first check of the output buffer size above (
  892. * MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 38 + 1 + 32 );)
  893. * has checked that there is enough space in the output buffer for the
  894. * session identifier length byte and the session identifier (n <= 32).
  895. */
  896. *p++ = (unsigned char) n;
  897. for (i = 0; i < n; i++) {
  898. *p++ = ssl->session_negotiate->id[i];
  899. }
  900. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
  901. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id", buf + 39, n);
  902. /*
  903. * With 'n' being the length of the session identifier
  904. *
  905. * 39+n . 39+n DTLS only: cookie length (1 byte)
  906. * 40+n . .. DTLS only: cookie
  907. * .. . .. ciphersuitelist length (2 bytes)
  908. * .. . .. ciphersuitelist
  909. * .. . .. compression methods length (1 byte)
  910. * .. . .. compression methods
  911. * .. . .. extensions length (2 bytes)
  912. * .. . .. extensions
  913. */
  914. /*
  915. * DTLS cookie
  916. */
  917. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  918. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  919. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
  920. if (ssl->handshake->verify_cookie == NULL) {
  921. MBEDTLS_SSL_DEBUG_MSG(3, ("no verify cookie to send"));
  922. *p++ = 0;
  923. } else {
  924. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, cookie",
  925. ssl->handshake->verify_cookie,
  926. ssl->handshake->verify_cookie_len);
  927. *p++ = ssl->handshake->verify_cookie_len;
  928. MBEDTLS_SSL_CHK_BUF_PTR(p, end,
  929. ssl->handshake->verify_cookie_len);
  930. memcpy(p, ssl->handshake->verify_cookie,
  931. ssl->handshake->verify_cookie_len);
  932. p += ssl->handshake->verify_cookie_len;
  933. }
  934. }
  935. #endif
  936. /*
  937. * Ciphersuite list
  938. */
  939. ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
  940. /* Skip writing ciphersuite length for now */
  941. n = 0;
  942. q = p;
  943. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  944. p += 2;
  945. for (i = 0; ciphersuites[i] != 0; i++) {
  946. ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuites[i]);
  947. if (ssl_validate_ciphersuite(ciphersuite_info, ssl,
  948. ssl->conf->min_minor_ver,
  949. ssl->conf->max_minor_ver) != 0) {
  950. continue;
  951. }
  952. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, add ciphersuite: %#04x (%s)",
  953. (unsigned int) ciphersuites[i], ciphersuite_info->name));
  954. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  955. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  956. uses_ec |= mbedtls_ssl_ciphersuite_uses_ec(ciphersuite_info);
  957. #endif
  958. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  959. n++;
  960. MBEDTLS_PUT_UINT16_BE(ciphersuites[i], p, 0);
  961. p += 2;
  962. }
  963. MBEDTLS_SSL_DEBUG_MSG(3,
  964. ("client hello, got %" MBEDTLS_PRINTF_SIZET
  965. " ciphersuites (excluding SCSVs)", n));
  966. /*
  967. * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
  968. */
  969. if (!renegotiating) {
  970. MBEDTLS_SSL_DEBUG_MSG(3, ("adding EMPTY_RENEGOTIATION_INFO_SCSV"));
  971. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  972. MBEDTLS_PUT_UINT16_BE(MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0);
  973. p += 2;
  974. n++;
  975. }
  976. /* Some versions of OpenSSL don't handle it correctly if not at end */
  977. #if defined(MBEDTLS_SSL_FALLBACK_SCSV)
  978. if (ssl->conf->fallback == MBEDTLS_SSL_IS_FALLBACK) {
  979. MBEDTLS_SSL_DEBUG_MSG(3, ("adding FALLBACK_SCSV"));
  980. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  981. MBEDTLS_PUT_UINT16_BE(MBEDTLS_SSL_FALLBACK_SCSV_VALUE, p, 0);
  982. p += 2;
  983. n++;
  984. }
  985. #endif
  986. *q++ = (unsigned char) (n >> 7);
  987. *q++ = (unsigned char) (n << 1);
  988. #if defined(MBEDTLS_ZLIB_SUPPORT)
  989. offer_compress = 1;
  990. #else
  991. offer_compress = 0;
  992. #endif
  993. /*
  994. * We don't support compression with DTLS right now: if many records come
  995. * in the same datagram, uncompressing one could overwrite the next one.
  996. * We don't want to add complexity for handling that case unless there is
  997. * an actual need for it.
  998. */
  999. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1000. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  1001. offer_compress = 0;
  1002. }
  1003. #endif
  1004. if (offer_compress) {
  1005. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, compress len.: %d", 2));
  1006. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, compress alg.: %d %d",
  1007. MBEDTLS_SSL_COMPRESS_DEFLATE,
  1008. MBEDTLS_SSL_COMPRESS_NULL));
  1009. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
  1010. *p++ = 2;
  1011. *p++ = MBEDTLS_SSL_COMPRESS_DEFLATE;
  1012. *p++ = MBEDTLS_SSL_COMPRESS_NULL;
  1013. } else {
  1014. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, compress len.: %d", 1));
  1015. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, compress alg.: %d",
  1016. MBEDTLS_SSL_COMPRESS_NULL));
  1017. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  1018. *p++ = 1;
  1019. *p++ = MBEDTLS_SSL_COMPRESS_NULL;
  1020. }
  1021. /* First write extensions, then the total length */
  1022. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  1023. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  1024. if ((ret = ssl_write_hostname_ext(ssl, p + 2 + ext_len,
  1025. end, &olen)) != 0) {
  1026. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_hostname_ext", ret);
  1027. return ret;
  1028. }
  1029. ext_len += olen;
  1030. #endif
  1031. /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
  1032. * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
  1033. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1034. if ((ret = ssl_write_renegotiation_ext(ssl, p + 2 + ext_len,
  1035. end, &olen)) != 0) {
  1036. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_renegotiation_ext", ret);
  1037. return ret;
  1038. }
  1039. ext_len += olen;
  1040. #endif
  1041. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  1042. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  1043. if ((ret = ssl_write_signature_algorithms_ext(ssl, p + 2 + ext_len,
  1044. end, &olen)) != 0) {
  1045. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_signature_algorithms_ext", ret);
  1046. return ret;
  1047. }
  1048. ext_len += olen;
  1049. #endif
  1050. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  1051. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1052. if (uses_ec) {
  1053. if ((ret = ssl_write_supported_elliptic_curves_ext(ssl, p + 2 + ext_len,
  1054. end, &olen)) != 0) {
  1055. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_supported_elliptic_curves_ext", ret);
  1056. return ret;
  1057. }
  1058. ext_len += olen;
  1059. if ((ret = ssl_write_supported_point_formats_ext(ssl, p + 2 + ext_len,
  1060. end, &olen)) != 0) {
  1061. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_supported_point_formats_ext", ret);
  1062. return ret;
  1063. }
  1064. ext_len += olen;
  1065. }
  1066. #endif
  1067. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1068. if ((ret = ssl_write_ecjpake_kkpp_ext(ssl, p + 2 + ext_len,
  1069. end, &olen)) != 0) {
  1070. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_ecjpake_kkpp_ext", ret);
  1071. return ret;
  1072. }
  1073. ext_len += olen;
  1074. #endif
  1075. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1076. if ((ret = ssl_write_cid_ext(ssl, p + 2 + ext_len, end, &olen)) != 0) {
  1077. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_cid_ext", ret);
  1078. return ret;
  1079. }
  1080. ext_len += olen;
  1081. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  1082. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  1083. if ((ret = ssl_write_max_fragment_length_ext(ssl, p + 2 + ext_len,
  1084. end, &olen)) != 0) {
  1085. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_max_fragment_length_ext", ret);
  1086. return ret;
  1087. }
  1088. ext_len += olen;
  1089. #endif
  1090. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  1091. if ((ret = ssl_write_truncated_hmac_ext(ssl, p + 2 + ext_len,
  1092. end, &olen)) != 0) {
  1093. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_truncated_hmac_ext", ret);
  1094. return ret;
  1095. }
  1096. ext_len += olen;
  1097. #endif
  1098. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  1099. if ((ret = ssl_write_encrypt_then_mac_ext(ssl, p + 2 + ext_len,
  1100. end, &olen)) != 0) {
  1101. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_encrypt_then_mac_ext", ret);
  1102. return ret;
  1103. }
  1104. ext_len += olen;
  1105. #endif
  1106. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  1107. if ((ret = ssl_write_extended_ms_ext(ssl, p + 2 + ext_len,
  1108. end, &olen)) != 0) {
  1109. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_extended_ms_ext", ret);
  1110. return ret;
  1111. }
  1112. ext_len += olen;
  1113. #endif
  1114. #if defined(MBEDTLS_SSL_ALPN)
  1115. if ((ret = ssl_write_alpn_ext(ssl, p + 2 + ext_len,
  1116. end, &olen)) != 0) {
  1117. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_alpn_ext", ret);
  1118. return ret;
  1119. }
  1120. ext_len += olen;
  1121. #endif
  1122. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  1123. if ((ret = ssl_write_use_srtp_ext(ssl, p + 2 + ext_len,
  1124. end, &olen)) != 0) {
  1125. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_use_srtp_ext", ret);
  1126. return ret;
  1127. }
  1128. ext_len += olen;
  1129. #endif
  1130. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1131. if ((ret = ssl_write_session_ticket_ext(ssl, p + 2 + ext_len,
  1132. end, &olen)) != 0) {
  1133. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_session_ticket_ext", ret);
  1134. return ret;
  1135. }
  1136. ext_len += olen;
  1137. #endif
  1138. /* olen unused if all extensions are disabled */
  1139. ((void) olen);
  1140. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
  1141. ext_len));
  1142. if (ext_len > 0) {
  1143. /* No need to check for space here, because the extension
  1144. * writing functions already took care of that. */
  1145. MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
  1146. p += 2 + ext_len;
  1147. }
  1148. ssl->out_msglen = p - buf;
  1149. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  1150. ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_HELLO;
  1151. ssl->state++;
  1152. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1153. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  1154. mbedtls_ssl_send_flight_completed(ssl);
  1155. }
  1156. #endif
  1157. if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
  1158. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
  1159. return ret;
  1160. }
  1161. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1162. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  1163. (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
  1164. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
  1165. return ret;
  1166. }
  1167. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1168. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client hello"));
  1169. return 0;
  1170. }
  1171. MBEDTLS_CHECK_RETURN_CRITICAL
  1172. static int ssl_parse_renegotiation_info(mbedtls_ssl_context *ssl,
  1173. const unsigned char *buf,
  1174. size_t len)
  1175. {
  1176. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1177. if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
  1178. /* Check verify-data in constant-time. The length OTOH is no secret */
  1179. if (len != 1 + ssl->verify_data_len * 2 ||
  1180. buf[0] != ssl->verify_data_len * 2 ||
  1181. mbedtls_ct_memcmp(buf + 1,
  1182. ssl->own_verify_data, ssl->verify_data_len) != 0 ||
  1183. mbedtls_ct_memcmp(buf + 1 + ssl->verify_data_len,
  1184. ssl->peer_verify_data, ssl->verify_data_len) != 0) {
  1185. MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info"));
  1186. mbedtls_ssl_send_alert_message(
  1187. ssl,
  1188. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1189. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1190. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1191. }
  1192. } else
  1193. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1194. {
  1195. if (len != 1 || buf[0] != 0x00) {
  1196. MBEDTLS_SSL_DEBUG_MSG(1,
  1197. ("non-zero length renegotiation info"));
  1198. mbedtls_ssl_send_alert_message(
  1199. ssl,
  1200. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1201. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1202. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1203. }
  1204. ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
  1205. }
  1206. return 0;
  1207. }
  1208. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  1209. MBEDTLS_CHECK_RETURN_CRITICAL
  1210. static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context *ssl,
  1211. const unsigned char *buf,
  1212. size_t len)
  1213. {
  1214. /*
  1215. * server should use the extension only if we did,
  1216. * and if so the server's value should match ours (and len is always 1)
  1217. */
  1218. if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
  1219. len != 1 ||
  1220. buf[0] != ssl->conf->mfl_code) {
  1221. MBEDTLS_SSL_DEBUG_MSG(1,
  1222. ("non-matching max fragment length extension"));
  1223. mbedtls_ssl_send_alert_message(
  1224. ssl,
  1225. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1226. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  1227. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1228. }
  1229. return 0;
  1230. }
  1231. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  1232. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  1233. MBEDTLS_CHECK_RETURN_CRITICAL
  1234. static int ssl_parse_truncated_hmac_ext(mbedtls_ssl_context *ssl,
  1235. const unsigned char *buf,
  1236. size_t len)
  1237. {
  1238. if (ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED ||
  1239. len != 0) {
  1240. MBEDTLS_SSL_DEBUG_MSG(1,
  1241. ("non-matching truncated HMAC extension"));
  1242. mbedtls_ssl_send_alert_message(
  1243. ssl,
  1244. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1245. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1246. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1247. }
  1248. ((void) buf);
  1249. ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
  1250. return 0;
  1251. }
  1252. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  1253. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1254. MBEDTLS_CHECK_RETURN_CRITICAL
  1255. static int ssl_parse_cid_ext(mbedtls_ssl_context *ssl,
  1256. const unsigned char *buf,
  1257. size_t len)
  1258. {
  1259. size_t peer_cid_len;
  1260. if ( /* CID extension only makes sense in DTLS */
  1261. ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
  1262. /* The server must only send the CID extension if we have offered it. */
  1263. ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
  1264. MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension unexpected"));
  1265. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1266. MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
  1267. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1268. }
  1269. if (len == 0) {
  1270. MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
  1271. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1272. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1273. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1274. }
  1275. peer_cid_len = *buf++;
  1276. len--;
  1277. if (peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX) {
  1278. MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
  1279. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1280. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1281. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1282. }
  1283. if (len != peer_cid_len) {
  1284. MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
  1285. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1286. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  1287. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1288. }
  1289. ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
  1290. ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
  1291. memcpy(ssl->handshake->peer_cid, buf, peer_cid_len);
  1292. MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated"));
  1293. MBEDTLS_SSL_DEBUG_BUF(3, "Server CID", buf, peer_cid_len);
  1294. return 0;
  1295. }
  1296. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  1297. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  1298. MBEDTLS_CHECK_RETURN_CRITICAL
  1299. static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
  1300. const unsigned char *buf,
  1301. size_t len)
  1302. {
  1303. if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
  1304. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
  1305. len != 0) {
  1306. MBEDTLS_SSL_DEBUG_MSG(1,
  1307. ("non-matching encrypt-then-MAC extension"));
  1308. mbedtls_ssl_send_alert_message(
  1309. ssl,
  1310. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1311. MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
  1312. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1313. }
  1314. ((void) buf);
  1315. ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
  1316. return 0;
  1317. }
  1318. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  1319. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  1320. MBEDTLS_CHECK_RETURN_CRITICAL
  1321. static int ssl_parse_extended_ms_ext(mbedtls_ssl_context *ssl,
  1322. const unsigned char *buf,
  1323. size_t len)
  1324. {
  1325. if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
  1326. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
  1327. len != 0) {
  1328. MBEDTLS_SSL_DEBUG_MSG(1,
  1329. ("non-matching extended master secret extension"));
  1330. mbedtls_ssl_send_alert_message(
  1331. ssl,
  1332. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1333. MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
  1334. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1335. }
  1336. ((void) buf);
  1337. ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
  1338. return 0;
  1339. }
  1340. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  1341. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1342. MBEDTLS_CHECK_RETURN_CRITICAL
  1343. static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
  1344. const unsigned char *buf,
  1345. size_t len)
  1346. {
  1347. if (ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
  1348. len != 0) {
  1349. MBEDTLS_SSL_DEBUG_MSG(1,
  1350. ("non-matching session ticket extension"));
  1351. mbedtls_ssl_send_alert_message(
  1352. ssl,
  1353. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1354. MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
  1355. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1356. }
  1357. ((void) buf);
  1358. ssl->handshake->new_session_ticket = 1;
  1359. return 0;
  1360. }
  1361. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  1362. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  1363. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1364. MBEDTLS_CHECK_RETURN_CRITICAL
  1365. static int ssl_parse_supported_point_formats_ext(mbedtls_ssl_context *ssl,
  1366. const unsigned char *buf,
  1367. size_t len)
  1368. {
  1369. size_t list_size;
  1370. const unsigned char *p;
  1371. if (len == 0 || (size_t) (buf[0] + 1) != len) {
  1372. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  1373. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1374. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1375. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1376. }
  1377. list_size = buf[0];
  1378. p = buf + 1;
  1379. while (list_size > 0) {
  1380. if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
  1381. p[0] == MBEDTLS_ECP_PF_COMPRESSED) {
  1382. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
  1383. ssl->handshake->ecdh_ctx.point_format = p[0];
  1384. #endif
  1385. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1386. ssl->handshake->ecjpake_ctx.point_format = p[0];
  1387. #endif
  1388. MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
  1389. return 0;
  1390. }
  1391. list_size--;
  1392. p++;
  1393. }
  1394. MBEDTLS_SSL_DEBUG_MSG(1, ("no point format in common"));
  1395. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1396. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1397. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1398. }
  1399. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
  1400. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1401. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1402. MBEDTLS_CHECK_RETURN_CRITICAL
  1403. static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
  1404. const unsigned char *buf,
  1405. size_t len)
  1406. {
  1407. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1408. if (ssl->handshake->ciphersuite_info->key_exchange !=
  1409. MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
  1410. MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
  1411. return 0;
  1412. }
  1413. /* If we got here, we no longer need our cached extension */
  1414. mbedtls_free(ssl->handshake->ecjpake_cache);
  1415. ssl->handshake->ecjpake_cache = NULL;
  1416. ssl->handshake->ecjpake_cache_len = 0;
  1417. if ((ret = mbedtls_ecjpake_read_round_one(&ssl->handshake->ecjpake_ctx,
  1418. buf, len)) != 0) {
  1419. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_one", ret);
  1420. mbedtls_ssl_send_alert_message(
  1421. ssl,
  1422. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1423. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1424. return ret;
  1425. }
  1426. return 0;
  1427. }
  1428. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1429. #if defined(MBEDTLS_SSL_ALPN)
  1430. MBEDTLS_CHECK_RETURN_CRITICAL
  1431. static int ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
  1432. const unsigned char *buf, size_t len)
  1433. {
  1434. size_t list_len, name_len;
  1435. const char **p;
  1436. /* If we didn't send it, the server shouldn't send it */
  1437. if (ssl->conf->alpn_list == NULL) {
  1438. MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching ALPN extension"));
  1439. mbedtls_ssl_send_alert_message(
  1440. ssl,
  1441. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1442. MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
  1443. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1444. }
  1445. /*
  1446. * opaque ProtocolName<1..2^8-1>;
  1447. *
  1448. * struct {
  1449. * ProtocolName protocol_name_list<2..2^16-1>
  1450. * } ProtocolNameList;
  1451. *
  1452. * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
  1453. */
  1454. /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
  1455. if (len < 4) {
  1456. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1457. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1458. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1459. }
  1460. list_len = (buf[0] << 8) | buf[1];
  1461. if (list_len != len - 2) {
  1462. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1463. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1464. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1465. }
  1466. name_len = buf[2];
  1467. if (name_len != list_len - 1) {
  1468. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1469. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1470. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1471. }
  1472. /* Check that the server chosen protocol was in our list and save it */
  1473. for (p = ssl->conf->alpn_list; *p != NULL; p++) {
  1474. if (name_len == strlen(*p) &&
  1475. memcmp(buf + 3, *p, name_len) == 0) {
  1476. ssl->alpn_chosen = *p;
  1477. return 0;
  1478. }
  1479. }
  1480. MBEDTLS_SSL_DEBUG_MSG(1, ("ALPN extension: no matching protocol"));
  1481. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1482. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1483. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1484. }
  1485. #endif /* MBEDTLS_SSL_ALPN */
  1486. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  1487. MBEDTLS_CHECK_RETURN_CRITICAL
  1488. static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
  1489. const unsigned char *buf,
  1490. size_t len)
  1491. {
  1492. mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
  1493. size_t i, mki_len = 0;
  1494. uint16_t server_protection_profile_value = 0;
  1495. /* If use_srtp is not configured, just ignore the extension */
  1496. if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
  1497. (ssl->conf->dtls_srtp_profile_list == NULL) ||
  1498. (ssl->conf->dtls_srtp_profile_list_len == 0)) {
  1499. return 0;
  1500. }
  1501. /* RFC 5764 section 4.1.1
  1502. * uint8 SRTPProtectionProfile[2];
  1503. *
  1504. * struct {
  1505. * SRTPProtectionProfiles SRTPProtectionProfiles;
  1506. * opaque srtp_mki<0..255>;
  1507. * } UseSRTPData;
  1508. * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
  1509. *
  1510. */
  1511. if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
  1512. mki_len = ssl->dtls_srtp_info.mki_len;
  1513. }
  1514. /*
  1515. * Length is 5 + optional mki_value : one protection profile length (2 bytes)
  1516. * + protection profile (2 bytes)
  1517. * + mki_len(1 byte)
  1518. * and optional srtp_mki
  1519. */
  1520. if ((len < 5) || (len != (buf[4] + 5u))) {
  1521. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1522. }
  1523. /*
  1524. * get the server protection profile
  1525. */
  1526. /*
  1527. * protection profile length must be 0x0002 as we must have only
  1528. * one protection profile in server Hello
  1529. */
  1530. if ((buf[0] != 0) || (buf[1] != 2)) {
  1531. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1532. }
  1533. server_protection_profile_value = (buf[2] << 8) | buf[3];
  1534. server_protection = mbedtls_ssl_check_srtp_profile_value(
  1535. server_protection_profile_value);
  1536. if (server_protection != MBEDTLS_TLS_SRTP_UNSET) {
  1537. MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
  1538. mbedtls_ssl_get_srtp_profile_as_string(
  1539. server_protection)));
  1540. }
  1541. ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
  1542. /*
  1543. * Check we have the server profile in our list
  1544. */
  1545. for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
  1546. if (server_protection == ssl->conf->dtls_srtp_profile_list[i]) {
  1547. ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
  1548. MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
  1549. mbedtls_ssl_get_srtp_profile_as_string(
  1550. server_protection)));
  1551. break;
  1552. }
  1553. }
  1554. /* If no match was found : server problem, it shall never answer with incompatible profile */
  1555. if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
  1556. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1557. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1558. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1559. }
  1560. /* If server does not use mki in its reply, make sure the client won't keep
  1561. * one as negotiated */
  1562. if (len == 5) {
  1563. ssl->dtls_srtp_info.mki_len = 0;
  1564. }
  1565. /*
  1566. * RFC5764:
  1567. * If the client detects a nonzero-length MKI in the server's response
  1568. * that is different than the one the client offered, then the client
  1569. * MUST abort the handshake and SHOULD send an invalid_parameter alert.
  1570. */
  1571. if (len > 5 && (buf[4] != mki_len ||
  1572. (memcmp(ssl->dtls_srtp_info.mki_value, &buf[5], mki_len)))) {
  1573. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1574. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  1575. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1576. }
  1577. #if defined(MBEDTLS_DEBUG_C)
  1578. if (len > 5) {
  1579. MBEDTLS_SSL_DEBUG_BUF(3, "received mki", ssl->dtls_srtp_info.mki_value,
  1580. ssl->dtls_srtp_info.mki_len);
  1581. }
  1582. #endif
  1583. return 0;
  1584. }
  1585. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  1586. /*
  1587. * Parse HelloVerifyRequest. Only called after verifying the HS type.
  1588. */
  1589. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1590. MBEDTLS_CHECK_RETURN_CRITICAL
  1591. static int ssl_parse_hello_verify_request(mbedtls_ssl_context *ssl)
  1592. {
  1593. const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
  1594. int major_ver, minor_ver;
  1595. unsigned char cookie_len;
  1596. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse hello verify request"));
  1597. /* Check that there is enough room for:
  1598. * - 2 bytes of version
  1599. * - 1 byte of cookie_len
  1600. */
  1601. if (mbedtls_ssl_hs_hdr_len(ssl) + 3 > ssl->in_msglen) {
  1602. MBEDTLS_SSL_DEBUG_MSG(1,
  1603. ("incoming HelloVerifyRequest message is too short"));
  1604. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1605. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1606. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1607. }
  1608. /*
  1609. * struct {
  1610. * ProtocolVersion server_version;
  1611. * opaque cookie<0..2^8-1>;
  1612. * } HelloVerifyRequest;
  1613. */
  1614. MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
  1615. mbedtls_ssl_read_version(&major_ver, &minor_ver, ssl->conf->transport, p);
  1616. p += 2;
  1617. /*
  1618. * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
  1619. * even is lower than our min version.
  1620. */
  1621. if (major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
  1622. minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ||
  1623. major_ver > ssl->conf->max_major_ver ||
  1624. minor_ver > ssl->conf->max_minor_ver) {
  1625. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server version"));
  1626. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1627. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
  1628. return MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION;
  1629. }
  1630. cookie_len = *p++;
  1631. if ((ssl->in_msg + ssl->in_msglen) - p < cookie_len) {
  1632. MBEDTLS_SSL_DEBUG_MSG(1,
  1633. ("cookie length does not match incoming message size"));
  1634. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1635. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1636. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1637. }
  1638. MBEDTLS_SSL_DEBUG_BUF(3, "cookie", p, cookie_len);
  1639. mbedtls_free(ssl->handshake->verify_cookie);
  1640. ssl->handshake->verify_cookie = mbedtls_calloc(1, cookie_len);
  1641. if (ssl->handshake->verify_cookie == NULL) {
  1642. MBEDTLS_SSL_DEBUG_MSG(1, ("alloc failed (%d bytes)", cookie_len));
  1643. return MBEDTLS_ERR_SSL_ALLOC_FAILED;
  1644. }
  1645. memcpy(ssl->handshake->verify_cookie, p, cookie_len);
  1646. ssl->handshake->verify_cookie_len = cookie_len;
  1647. /* Start over at ClientHello */
  1648. ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
  1649. mbedtls_ssl_reset_checksum(ssl);
  1650. mbedtls_ssl_recv_flight_completed(ssl);
  1651. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse hello verify request"));
  1652. return 0;
  1653. }
  1654. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1655. static int is_compression_bad(mbedtls_ssl_context *ssl, unsigned char comp)
  1656. {
  1657. int bad_comp = 0;
  1658. /* Suppress warnings in some configurations */
  1659. (void) ssl;
  1660. #if defined(MBEDTLS_ZLIB_SUPPORT)
  1661. /* See comments in ssl_write_client_hello() */
  1662. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1663. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  1664. comp != MBEDTLS_SSL_COMPRESS_NULL) {
  1665. bad_comp = 1;
  1666. }
  1667. #endif
  1668. if (comp != MBEDTLS_SSL_COMPRESS_NULL &&
  1669. comp != MBEDTLS_SSL_COMPRESS_DEFLATE) {
  1670. bad_comp = 1;
  1671. }
  1672. #else /* MBEDTLS_ZLIB_SUPPORT */
  1673. if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
  1674. bad_comp = 1;
  1675. }
  1676. #endif /* MBEDTLS_ZLIB_SUPPORT */
  1677. return bad_comp;
  1678. }
  1679. MBEDTLS_CHECK_RETURN_CRITICAL
  1680. static int ssl_parse_server_hello(mbedtls_ssl_context *ssl)
  1681. {
  1682. int ret, i;
  1683. size_t n;
  1684. size_t ext_len;
  1685. unsigned char *buf, *ext;
  1686. unsigned char comp;
  1687. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1688. int renegotiation_info_seen = 0;
  1689. #endif
  1690. int handshake_failure = 0;
  1691. const mbedtls_ssl_ciphersuite_t *suite_info;
  1692. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello"));
  1693. if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
  1694. /* No alert on a read error. */
  1695. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
  1696. return ret;
  1697. }
  1698. buf = ssl->in_msg;
  1699. if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
  1700. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1701. if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
  1702. ssl->renego_records_seen++;
  1703. if (ssl->conf->renego_max_records >= 0 &&
  1704. ssl->renego_records_seen > ssl->conf->renego_max_records) {
  1705. MBEDTLS_SSL_DEBUG_MSG(1,
  1706. ("renegotiation requested, but not honored by server"));
  1707. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  1708. }
  1709. MBEDTLS_SSL_DEBUG_MSG(1,
  1710. ("non-handshake message during renegotiation"));
  1711. ssl->keep_current_message = 1;
  1712. return MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO;
  1713. }
  1714. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1715. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  1716. mbedtls_ssl_send_alert_message(
  1717. ssl,
  1718. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1719. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
  1720. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  1721. }
  1722. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1723. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  1724. if (buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
  1725. MBEDTLS_SSL_DEBUG_MSG(2, ("received hello verify request"));
  1726. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
  1727. return ssl_parse_hello_verify_request(ssl);
  1728. } else {
  1729. /* We made it through the verification process */
  1730. mbedtls_free(ssl->handshake->verify_cookie);
  1731. ssl->handshake->verify_cookie = NULL;
  1732. ssl->handshake->verify_cookie_len = 0;
  1733. }
  1734. }
  1735. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1736. if (ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len(ssl) ||
  1737. buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO) {
  1738. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  1739. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1740. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1741. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1742. }
  1743. /*
  1744. * 0 . 1 server_version
  1745. * 2 . 33 random (maybe including 4 bytes of Unix time)
  1746. * 34 . 34 session_id length = n
  1747. * 35 . 34+n session_id
  1748. * 35+n . 36+n cipher_suite
  1749. * 37+n . 37+n compression_method
  1750. *
  1751. * 38+n . 39+n extensions length (optional)
  1752. * 40+n . .. extensions
  1753. */
  1754. buf += mbedtls_ssl_hs_hdr_len(ssl);
  1755. MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", buf + 0, 2);
  1756. mbedtls_ssl_read_version(&ssl->major_ver, &ssl->minor_ver,
  1757. ssl->conf->transport, buf + 0);
  1758. if (ssl->major_ver < ssl->conf->min_major_ver ||
  1759. ssl->minor_ver < ssl->conf->min_minor_ver ||
  1760. ssl->major_ver > ssl->conf->max_major_ver ||
  1761. ssl->minor_ver > ssl->conf->max_minor_ver) {
  1762. MBEDTLS_SSL_DEBUG_MSG(1,
  1763. (
  1764. "server version out of bounds - min: [%d:%d], server: [%d:%d], max: [%d:%d]",
  1765. ssl->conf->min_major_ver,
  1766. ssl->conf->min_minor_ver,
  1767. ssl->major_ver, ssl->minor_ver,
  1768. ssl->conf->max_major_ver,
  1769. ssl->conf->max_minor_ver));
  1770. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1771. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
  1772. return MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION;
  1773. }
  1774. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %lu",
  1775. ((unsigned long) buf[2] << 24) |
  1776. ((unsigned long) buf[3] << 16) |
  1777. ((unsigned long) buf[4] << 8) |
  1778. ((unsigned long) buf[5])));
  1779. memcpy(ssl->handshake->randbytes + 32, buf + 2, 32);
  1780. n = buf[34];
  1781. MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 2, 32);
  1782. if (n > 32) {
  1783. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  1784. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1785. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1786. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1787. }
  1788. if (ssl->in_hslen > mbedtls_ssl_hs_hdr_len(ssl) + 39 + n) {
  1789. ext_len = ((buf[38 + n] << 8)
  1790. | (buf[39 + n]));
  1791. if ((ext_len > 0 && ext_len < 4) ||
  1792. ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 40 + n + ext_len) {
  1793. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  1794. mbedtls_ssl_send_alert_message(
  1795. ssl,
  1796. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1797. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1798. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1799. }
  1800. } else if (ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl) + 38 + n) {
  1801. ext_len = 0;
  1802. } else {
  1803. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  1804. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1805. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1806. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1807. }
  1808. /* ciphersuite (used later) */
  1809. i = (buf[35 + n] << 8) | buf[36 + n];
  1810. /*
  1811. * Read and check compression
  1812. */
  1813. comp = buf[37 + n];
  1814. if (is_compression_bad(ssl, comp)) {
  1815. MBEDTLS_SSL_DEBUG_MSG(1,
  1816. ("server hello, bad compression: %d", comp));
  1817. mbedtls_ssl_send_alert_message(
  1818. ssl,
  1819. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1820. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  1821. return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  1822. }
  1823. /*
  1824. * Initialize update checksum functions
  1825. */
  1826. ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(i);
  1827. if (ssl->handshake->ciphersuite_info == NULL) {
  1828. MBEDTLS_SSL_DEBUG_MSG(1,
  1829. ("ciphersuite info for %04x not found", (unsigned int) i));
  1830. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1831. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
  1832. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  1833. }
  1834. mbedtls_ssl_optimize_checksum(ssl, ssl->handshake->ciphersuite_info);
  1835. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
  1836. MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 35, n);
  1837. /*
  1838. * Check if the session can be resumed
  1839. */
  1840. if (ssl->handshake->resume == 0 || n == 0 ||
  1841. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1842. ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
  1843. #endif
  1844. ssl->session_negotiate->ciphersuite != i ||
  1845. ssl->session_negotiate->compression != comp ||
  1846. ssl->session_negotiate->id_len != n ||
  1847. memcmp(ssl->session_negotiate->id, buf + 35, n) != 0) {
  1848. ssl->state++;
  1849. ssl->handshake->resume = 0;
  1850. #if defined(MBEDTLS_HAVE_TIME)
  1851. ssl->session_negotiate->start = mbedtls_time(NULL);
  1852. #endif
  1853. ssl->session_negotiate->ciphersuite = i;
  1854. ssl->session_negotiate->compression = comp;
  1855. ssl->session_negotiate->id_len = n;
  1856. memcpy(ssl->session_negotiate->id, buf + 35, n);
  1857. } else {
  1858. ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
  1859. }
  1860. MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
  1861. ssl->handshake->resume ? "a" : "no"));
  1862. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %04x", (unsigned) i));
  1863. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: %d",
  1864. buf[37 + n]));
  1865. /*
  1866. * Perform cipher suite validation in same way as in ssl_write_client_hello.
  1867. */
  1868. i = 0;
  1869. while (1) {
  1870. if (ssl->conf->ciphersuite_list[ssl->minor_ver][i] == 0) {
  1871. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  1872. mbedtls_ssl_send_alert_message(
  1873. ssl,
  1874. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1875. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  1876. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1877. }
  1878. if (ssl->conf->ciphersuite_list[ssl->minor_ver][i++] ==
  1879. ssl->session_negotiate->ciphersuite) {
  1880. break;
  1881. }
  1882. }
  1883. suite_info = mbedtls_ssl_ciphersuite_from_id(
  1884. ssl->session_negotiate->ciphersuite);
  1885. if (ssl_validate_ciphersuite(suite_info, ssl, ssl->minor_ver,
  1886. ssl->minor_ver) != 0) {
  1887. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  1888. mbedtls_ssl_send_alert_message(
  1889. ssl,
  1890. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1891. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  1892. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1893. }
  1894. MBEDTLS_SSL_DEBUG_MSG(3,
  1895. ("server hello, chosen ciphersuite: %s", suite_info->name));
  1896. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  1897. if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
  1898. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
  1899. ssl->handshake->ecrs_enabled = 1;
  1900. }
  1901. #endif
  1902. if (comp != MBEDTLS_SSL_COMPRESS_NULL
  1903. #if defined(MBEDTLS_ZLIB_SUPPORT)
  1904. && comp != MBEDTLS_SSL_COMPRESS_DEFLATE
  1905. #endif
  1906. ) {
  1907. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  1908. mbedtls_ssl_send_alert_message(
  1909. ssl,
  1910. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1911. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  1912. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1913. }
  1914. ssl->session_negotiate->compression = comp;
  1915. ext = buf + 40 + n;
  1916. MBEDTLS_SSL_DEBUG_MSG(2,
  1917. ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
  1918. ext_len));
  1919. while (ext_len) {
  1920. unsigned int ext_id = ((ext[0] << 8)
  1921. | (ext[1]));
  1922. unsigned int ext_size = ((ext[2] << 8)
  1923. | (ext[3]));
  1924. if (ext_size + 4 > ext_len) {
  1925. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  1926. mbedtls_ssl_send_alert_message(
  1927. ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1928. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1929. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  1930. }
  1931. switch (ext_id) {
  1932. case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
  1933. MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
  1934. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1935. renegotiation_info_seen = 1;
  1936. #endif
  1937. if ((ret = ssl_parse_renegotiation_info(ssl, ext + 4,
  1938. ext_size)) != 0) {
  1939. return ret;
  1940. }
  1941. break;
  1942. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  1943. case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
  1944. MBEDTLS_SSL_DEBUG_MSG(3,
  1945. ("found max_fragment_length extension"));
  1946. if ((ret = ssl_parse_max_fragment_length_ext(ssl,
  1947. ext + 4, ext_size)) != 0) {
  1948. return ret;
  1949. }
  1950. break;
  1951. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  1952. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  1953. case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
  1954. MBEDTLS_SSL_DEBUG_MSG(3, ("found truncated_hmac extension"));
  1955. if ((ret = ssl_parse_truncated_hmac_ext(ssl,
  1956. ext + 4, ext_size)) != 0) {
  1957. return ret;
  1958. }
  1959. break;
  1960. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  1961. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1962. case MBEDTLS_TLS_EXT_CID:
  1963. MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
  1964. if ((ret = ssl_parse_cid_ext(ssl,
  1965. ext + 4,
  1966. ext_size)) != 0) {
  1967. return ret;
  1968. }
  1969. break;
  1970. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  1971. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  1972. case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
  1973. MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt_then_mac extension"));
  1974. if ((ret = ssl_parse_encrypt_then_mac_ext(ssl,
  1975. ext + 4, ext_size)) != 0) {
  1976. return ret;
  1977. }
  1978. break;
  1979. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  1980. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  1981. case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
  1982. MBEDTLS_SSL_DEBUG_MSG(3,
  1983. ("found extended_master_secret extension"));
  1984. if ((ret = ssl_parse_extended_ms_ext(ssl,
  1985. ext + 4, ext_size)) != 0) {
  1986. return ret;
  1987. }
  1988. break;
  1989. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  1990. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1991. case MBEDTLS_TLS_EXT_SESSION_TICKET:
  1992. MBEDTLS_SSL_DEBUG_MSG(3, ("found session_ticket extension"));
  1993. if ((ret = ssl_parse_session_ticket_ext(ssl,
  1994. ext + 4, ext_size)) != 0) {
  1995. return ret;
  1996. }
  1997. break;
  1998. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  1999. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  2000. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2001. case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
  2002. MBEDTLS_SSL_DEBUG_MSG(3,
  2003. ("found supported_point_formats extension"));
  2004. if ((ret = ssl_parse_supported_point_formats_ext(ssl,
  2005. ext + 4, ext_size)) != 0) {
  2006. return ret;
  2007. }
  2008. break;
  2009. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
  2010. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  2011. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2012. case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
  2013. MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake_kkpp extension"));
  2014. if ((ret = ssl_parse_ecjpake_kkpp(ssl,
  2015. ext + 4, ext_size)) != 0) {
  2016. return ret;
  2017. }
  2018. break;
  2019. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  2020. #if defined(MBEDTLS_SSL_ALPN)
  2021. case MBEDTLS_TLS_EXT_ALPN:
  2022. MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
  2023. if ((ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size)) != 0) {
  2024. return ret;
  2025. }
  2026. break;
  2027. #endif /* MBEDTLS_SSL_ALPN */
  2028. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  2029. case MBEDTLS_TLS_EXT_USE_SRTP:
  2030. MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
  2031. if ((ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size)) != 0) {
  2032. return ret;
  2033. }
  2034. break;
  2035. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  2036. default:
  2037. MBEDTLS_SSL_DEBUG_MSG(3,
  2038. ("unknown extension found: %u (ignoring)", ext_id));
  2039. }
  2040. ext_len -= 4 + ext_size;
  2041. ext += 4 + ext_size;
  2042. if (ext_len > 0 && ext_len < 4) {
  2043. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  2044. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  2045. }
  2046. }
  2047. /*
  2048. * mbedtls_ssl_derive_keys() has to be called after the parsing of the
  2049. * extensions. It sets the transform data for the resumed session which in
  2050. * case of DTLS includes the server CID extracted from the CID extension.
  2051. */
  2052. if (ssl->handshake->resume) {
  2053. if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
  2054. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
  2055. mbedtls_ssl_send_alert_message(
  2056. ssl,
  2057. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2058. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
  2059. return ret;
  2060. }
  2061. }
  2062. /*
  2063. * Renegotiation security checks
  2064. */
  2065. if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  2066. ssl->conf->allow_legacy_renegotiation ==
  2067. MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
  2068. MBEDTLS_SSL_DEBUG_MSG(1,
  2069. ("legacy renegotiation, breaking off handshake"));
  2070. handshake_failure = 1;
  2071. }
  2072. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  2073. else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  2074. ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
  2075. renegotiation_info_seen == 0) {
  2076. MBEDTLS_SSL_DEBUG_MSG(1,
  2077. ("renegotiation_info extension missing (secure)"));
  2078. handshake_failure = 1;
  2079. } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  2080. ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  2081. ssl->conf->allow_legacy_renegotiation ==
  2082. MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
  2083. MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
  2084. handshake_failure = 1;
  2085. } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  2086. ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  2087. renegotiation_info_seen == 1) {
  2088. MBEDTLS_SSL_DEBUG_MSG(1,
  2089. ("renegotiation_info extension present (legacy)"));
  2090. handshake_failure = 1;
  2091. }
  2092. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  2093. if (handshake_failure == 1) {
  2094. mbedtls_ssl_send_alert_message(
  2095. ssl,
  2096. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2097. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  2098. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO;
  2099. }
  2100. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
  2101. return 0;
  2102. }
  2103. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  2104. defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  2105. MBEDTLS_CHECK_RETURN_CRITICAL
  2106. static int ssl_parse_server_dh_params(mbedtls_ssl_context *ssl,
  2107. unsigned char **p,
  2108. unsigned char *end)
  2109. {
  2110. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  2111. size_t dhm_actual_bitlen;
  2112. /*
  2113. * Ephemeral DH parameters:
  2114. *
  2115. * struct {
  2116. * opaque dh_p<1..2^16-1>;
  2117. * opaque dh_g<1..2^16-1>;
  2118. * opaque dh_Ys<1..2^16-1>;
  2119. * } ServerDHParams;
  2120. */
  2121. if ((ret = mbedtls_dhm_read_params(&ssl->handshake->dhm_ctx,
  2122. p, end)) != 0) {
  2123. MBEDTLS_SSL_DEBUG_RET(2, ("mbedtls_dhm_read_params"), ret);
  2124. return ret;
  2125. }
  2126. dhm_actual_bitlen = mbedtls_mpi_bitlen(&ssl->handshake->dhm_ctx.P);
  2127. if (dhm_actual_bitlen < ssl->conf->dhm_min_bitlen) {
  2128. MBEDTLS_SSL_DEBUG_MSG(1, ("DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
  2129. dhm_actual_bitlen,
  2130. ssl->conf->dhm_min_bitlen));
  2131. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2132. }
  2133. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
  2134. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
  2135. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
  2136. return ret;
  2137. }
  2138. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  2139. MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  2140. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  2141. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  2142. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
  2143. defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  2144. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  2145. MBEDTLS_CHECK_RETURN_CRITICAL
  2146. static int ssl_check_server_ecdh_params(const mbedtls_ssl_context *ssl)
  2147. {
  2148. const mbedtls_ecp_curve_info *curve_info;
  2149. mbedtls_ecp_group_id grp_id;
  2150. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  2151. grp_id = ssl->handshake->ecdh_ctx.grp.id;
  2152. #else
  2153. grp_id = ssl->handshake->ecdh_ctx.grp_id;
  2154. #endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
  2155. curve_info = mbedtls_ecp_curve_info_from_grp_id(grp_id);
  2156. if (curve_info == NULL) {
  2157. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2158. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2159. }
  2160. MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH curve: %s", curve_info->name));
  2161. #if defined(MBEDTLS_ECP_C)
  2162. if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
  2163. return -1;
  2164. }
  2165. #else
  2166. if (ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
  2167. ssl->handshake->ecdh_ctx.grp.nbits > 521) {
  2168. return -1;
  2169. }
  2170. #endif /* MBEDTLS_ECP_C */
  2171. MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
  2172. MBEDTLS_DEBUG_ECDH_QP);
  2173. return 0;
  2174. }
  2175. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  2176. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  2177. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
  2178. MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  2179. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  2180. #if defined(MBEDTLS_USE_PSA_CRYPTO) && \
  2181. (defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  2182. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED))
  2183. MBEDTLS_CHECK_RETURN_CRITICAL
  2184. static int ssl_parse_server_ecdh_params_psa(mbedtls_ssl_context *ssl,
  2185. unsigned char **p,
  2186. unsigned char *end)
  2187. {
  2188. uint16_t tls_id;
  2189. size_t ecdh_bits = 0;
  2190. uint8_t ecpoint_len;
  2191. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  2192. /*
  2193. * Parse ECC group
  2194. */
  2195. if (end - *p < 4) {
  2196. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2197. }
  2198. /* First byte is curve_type; only named_curve is handled */
  2199. if (*(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
  2200. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2201. }
  2202. /* Next two bytes are the namedcurve value */
  2203. tls_id = *(*p)++;
  2204. tls_id <<= 8;
  2205. tls_id |= *(*p)++;
  2206. /* Check it's a curve we offered */
  2207. if (mbedtls_ssl_check_curve_tls_id(ssl, tls_id) != 0) {
  2208. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2209. }
  2210. /* Convert EC group to PSA key type. */
  2211. if ((handshake->ecdh_psa_type =
  2212. mbedtls_psa_parse_tls_ecc_group(tls_id, &ecdh_bits)) == 0) {
  2213. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2214. }
  2215. if (ecdh_bits > 0xffff) {
  2216. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2217. }
  2218. handshake->ecdh_bits = (uint16_t) ecdh_bits;
  2219. /*
  2220. * Put peer's ECDH public key in the format understood by PSA.
  2221. */
  2222. ecpoint_len = *(*p)++;
  2223. if ((size_t) (end - *p) < ecpoint_len) {
  2224. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2225. }
  2226. if (mbedtls_psa_tls_ecpoint_to_psa_ec(
  2227. *p, ecpoint_len,
  2228. handshake->ecdh_psa_peerkey,
  2229. sizeof(handshake->ecdh_psa_peerkey),
  2230. &handshake->ecdh_psa_peerkey_len) != 0) {
  2231. return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
  2232. }
  2233. *p += ecpoint_len;
  2234. return 0;
  2235. }
  2236. #endif /* MBEDTLS_USE_PSA_CRYPTO &&
  2237. ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  2238. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
  2239. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  2240. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  2241. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  2242. MBEDTLS_CHECK_RETURN_CRITICAL
  2243. static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
  2244. unsigned char **p,
  2245. unsigned char *end)
  2246. {
  2247. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  2248. /*
  2249. * Ephemeral ECDH parameters:
  2250. *
  2251. * struct {
  2252. * ECParameters curve_params;
  2253. * ECPoint public;
  2254. * } ServerECDHParams;
  2255. */
  2256. if ((ret = mbedtls_ecdh_read_params(&ssl->handshake->ecdh_ctx,
  2257. (const unsigned char **) p, end)) != 0) {
  2258. MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_read_params"), ret);
  2259. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2260. if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
  2261. ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
  2262. }
  2263. #endif
  2264. return ret;
  2265. }
  2266. if (ssl_check_server_ecdh_params(ssl) != 0) {
  2267. MBEDTLS_SSL_DEBUG_MSG(1,
  2268. ("bad server key exchange message (ECDHE curve)"));
  2269. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2270. }
  2271. return ret;
  2272. }
  2273. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  2274. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  2275. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  2276. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  2277. MBEDTLS_CHECK_RETURN_CRITICAL
  2278. static int ssl_parse_server_psk_hint(mbedtls_ssl_context *ssl,
  2279. unsigned char **p,
  2280. unsigned char *end)
  2281. {
  2282. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  2283. uint16_t len;
  2284. ((void) ssl);
  2285. /*
  2286. * PSK parameters:
  2287. *
  2288. * opaque psk_identity_hint<0..2^16-1>;
  2289. */
  2290. if (end - (*p) < 2) {
  2291. MBEDTLS_SSL_DEBUG_MSG(1,
  2292. ("bad server key exchange message (psk_identity_hint length)"));
  2293. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2294. }
  2295. len = (*p)[0] << 8 | (*p)[1];
  2296. *p += 2;
  2297. if (end - (*p) < len) {
  2298. MBEDTLS_SSL_DEBUG_MSG(1,
  2299. ("bad server key exchange message (psk_identity_hint length)"));
  2300. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2301. }
  2302. /*
  2303. * Note: we currently ignore the PSK identity hint, as we only allow one
  2304. * PSK to be provisioned on the client. This could be changed later if
  2305. * someone needs that feature.
  2306. */
  2307. *p += len;
  2308. ret = 0;
  2309. return ret;
  2310. }
  2311. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  2312. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
  2313. defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  2314. /*
  2315. * Generate a pre-master secret and encrypt it with the server's RSA key
  2316. */
  2317. MBEDTLS_CHECK_RETURN_CRITICAL
  2318. static int ssl_write_encrypted_pms(mbedtls_ssl_context *ssl,
  2319. size_t offset, size_t *olen,
  2320. size_t pms_offset)
  2321. {
  2322. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2323. size_t len_bytes = ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ? 0 : 2;
  2324. unsigned char *p = ssl->handshake->premaster + pms_offset;
  2325. mbedtls_pk_context *peer_pk;
  2326. if (offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN) {
  2327. MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small for encrypted pms"));
  2328. return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
  2329. }
  2330. /*
  2331. * Generate (part of) the pre-master as
  2332. * struct {
  2333. * ProtocolVersion client_version;
  2334. * opaque random[46];
  2335. * } PreMasterSecret;
  2336. */
  2337. mbedtls_ssl_write_version(ssl->conf->max_major_ver,
  2338. ssl->conf->max_minor_ver,
  2339. ssl->conf->transport, p);
  2340. if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p + 2, 46)) != 0) {
  2341. MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
  2342. return ret;
  2343. }
  2344. ssl->handshake->pmslen = 48;
  2345. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  2346. peer_pk = &ssl->handshake->peer_pubkey;
  2347. #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2348. if (ssl->session_negotiate->peer_cert == NULL) {
  2349. /* Should never happen */
  2350. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2351. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2352. }
  2353. peer_pk = &ssl->session_negotiate->peer_cert->pk;
  2354. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2355. /*
  2356. * Now write it out, encrypted
  2357. */
  2358. if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_RSA)) {
  2359. MBEDTLS_SSL_DEBUG_MSG(1, ("certificate key type mismatch"));
  2360. return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
  2361. }
  2362. if ((ret = mbedtls_pk_encrypt(peer_pk,
  2363. p, ssl->handshake->pmslen,
  2364. ssl->out_msg + offset + len_bytes, olen,
  2365. MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
  2366. ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
  2367. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_rsa_pkcs1_encrypt", ret);
  2368. return ret;
  2369. }
  2370. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  2371. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2372. if (len_bytes == 2) {
  2373. MBEDTLS_PUT_UINT16_BE(*olen, ssl->out_msg, offset);
  2374. *olen += 2;
  2375. }
  2376. #endif
  2377. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  2378. /* We don't need the peer's public key anymore. Free it. */
  2379. mbedtls_pk_free(peer_pk);
  2380. #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2381. return 0;
  2382. }
  2383. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
  2384. MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  2385. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2386. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  2387. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  2388. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  2389. MBEDTLS_CHECK_RETURN_CRITICAL
  2390. static int ssl_parse_signature_algorithm(mbedtls_ssl_context *ssl,
  2391. unsigned char **p,
  2392. unsigned char *end,
  2393. mbedtls_md_type_t *md_alg,
  2394. mbedtls_pk_type_t *pk_alg)
  2395. {
  2396. ((void) ssl);
  2397. *md_alg = MBEDTLS_MD_NONE;
  2398. *pk_alg = MBEDTLS_PK_NONE;
  2399. /* Only in TLS 1.2 */
  2400. if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
  2401. return 0;
  2402. }
  2403. if ((*p) + 2 > end) {
  2404. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2405. }
  2406. /*
  2407. * Get hash algorithm
  2408. */
  2409. if ((*md_alg = mbedtls_ssl_md_alg_from_hash((*p)[0]))
  2410. == MBEDTLS_MD_NONE) {
  2411. MBEDTLS_SSL_DEBUG_MSG(1,
  2412. ("Server used unsupported HashAlgorithm %d", *(p)[0]));
  2413. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2414. }
  2415. /*
  2416. * Get signature algorithm
  2417. */
  2418. if ((*pk_alg = mbedtls_ssl_pk_alg_from_sig((*p)[1]))
  2419. == MBEDTLS_PK_NONE) {
  2420. MBEDTLS_SSL_DEBUG_MSG(1,
  2421. ("server used unsupported SignatureAlgorithm %d", (*p)[1]));
  2422. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2423. }
  2424. /*
  2425. * Check if the hash is acceptable
  2426. */
  2427. if (mbedtls_ssl_check_sig_hash(ssl, *md_alg) != 0) {
  2428. MBEDTLS_SSL_DEBUG_MSG(1,
  2429. ("server used HashAlgorithm %d that was not offered", *(p)[0]));
  2430. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2431. }
  2432. MBEDTLS_SSL_DEBUG_MSG(2, ("Server used SignatureAlgorithm %d",
  2433. (*p)[1]));
  2434. MBEDTLS_SSL_DEBUG_MSG(2, ("Server used HashAlgorithm %d",
  2435. (*p)[0]));
  2436. *p += 2;
  2437. return 0;
  2438. }
  2439. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  2440. MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  2441. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  2442. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2443. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  2444. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  2445. MBEDTLS_CHECK_RETURN_CRITICAL
  2446. static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
  2447. {
  2448. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2449. const mbedtls_ecp_keypair *peer_key;
  2450. mbedtls_pk_context *peer_pk;
  2451. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  2452. peer_pk = &ssl->handshake->peer_pubkey;
  2453. #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2454. if (ssl->session_negotiate->peer_cert == NULL) {
  2455. /* Should never happen */
  2456. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2457. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2458. }
  2459. peer_pk = &ssl->session_negotiate->peer_cert->pk;
  2460. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2461. /* This is a public key, so it can't be opaque, so can_do() is a good
  2462. * enough check to ensure pk_ec() is safe to use below. */
  2463. if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECKEY)) {
  2464. MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
  2465. return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
  2466. }
  2467. peer_key = mbedtls_pk_ec(*peer_pk);
  2468. if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx, peer_key,
  2469. MBEDTLS_ECDH_THEIRS)) != 0) {
  2470. MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
  2471. return ret;
  2472. }
  2473. if (ssl_check_server_ecdh_params(ssl) != 0) {
  2474. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
  2475. return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
  2476. }
  2477. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  2478. /* We don't need the peer's public key anymore. Free it,
  2479. * so that more RAM is available for upcoming expensive
  2480. * operations like ECDHE. */
  2481. mbedtls_pk_free(peer_pk);
  2482. #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2483. return ret;
  2484. }
  2485. #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
  2486. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  2487. MBEDTLS_CHECK_RETURN_CRITICAL
  2488. static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl)
  2489. {
  2490. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2491. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2492. ssl->handshake->ciphersuite_info;
  2493. unsigned char *p = NULL, *end = NULL;
  2494. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server key exchange"));
  2495. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  2496. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
  2497. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
  2498. ssl->state++;
  2499. return 0;
  2500. }
  2501. ((void) p);
  2502. ((void) end);
  2503. #endif
  2504. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  2505. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  2506. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
  2507. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
  2508. if ((ret = ssl_get_ecdh_params_from_cert(ssl)) != 0) {
  2509. MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
  2510. mbedtls_ssl_send_alert_message(
  2511. ssl,
  2512. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2513. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  2514. return ret;
  2515. }
  2516. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
  2517. ssl->state++;
  2518. return 0;
  2519. }
  2520. ((void) p);
  2521. ((void) end);
  2522. #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  2523. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  2524. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2525. if (ssl->handshake->ecrs_enabled &&
  2526. ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing) {
  2527. goto start_processing;
  2528. }
  2529. #endif
  2530. if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
  2531. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
  2532. return ret;
  2533. }
  2534. if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
  2535. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
  2536. mbedtls_ssl_send_alert_message(
  2537. ssl,
  2538. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2539. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
  2540. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  2541. }
  2542. /*
  2543. * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
  2544. * doesn't use a psk_identity_hint
  2545. */
  2546. if (ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE) {
  2547. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  2548. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
  2549. /* Current message is probably either
  2550. * CertificateRequest or ServerHelloDone */
  2551. ssl->keep_current_message = 1;
  2552. goto exit;
  2553. }
  2554. MBEDTLS_SSL_DEBUG_MSG(1,
  2555. ("server key exchange message must not be skipped"));
  2556. mbedtls_ssl_send_alert_message(
  2557. ssl,
  2558. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2559. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
  2560. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  2561. }
  2562. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2563. if (ssl->handshake->ecrs_enabled) {
  2564. ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
  2565. }
  2566. start_processing:
  2567. #endif
  2568. p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
  2569. end = ssl->in_msg + ssl->in_hslen;
  2570. MBEDTLS_SSL_DEBUG_BUF(3, "server key exchange", p, end - p);
  2571. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  2572. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  2573. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  2574. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  2575. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
  2576. if (ssl_parse_server_psk_hint(ssl, &p, end) != 0) {
  2577. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
  2578. mbedtls_ssl_send_alert_message(
  2579. ssl,
  2580. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2581. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  2582. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2583. }
  2584. } /* FALLTHROUGH */
  2585. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  2586. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
  2587. defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  2588. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  2589. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
  2590. ; /* nothing more to do */
  2591. } else
  2592. #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
  2593. MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  2594. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  2595. defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  2596. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
  2597. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
  2598. if (ssl_parse_server_dh_params(ssl, &p, end) != 0) {
  2599. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
  2600. mbedtls_ssl_send_alert_message(
  2601. ssl,
  2602. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2603. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  2604. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2605. }
  2606. } else
  2607. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  2608. MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  2609. #if defined(MBEDTLS_USE_PSA_CRYPTO) && \
  2610. (defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  2611. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED))
  2612. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  2613. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
  2614. if (ssl_parse_server_ecdh_params_psa(ssl, &p, end) != 0) {
  2615. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
  2616. mbedtls_ssl_send_alert_message(
  2617. ssl,
  2618. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2619. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  2620. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2621. }
  2622. } else
  2623. #endif /* MBEDTLS_USE_PSA_CRYPTO &&
  2624. ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  2625. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
  2626. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  2627. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
  2628. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  2629. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  2630. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  2631. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
  2632. if (ssl_parse_server_ecdh_params(ssl, &p, end) != 0) {
  2633. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
  2634. mbedtls_ssl_send_alert_message(
  2635. ssl,
  2636. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2637. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  2638. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2639. }
  2640. } else
  2641. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  2642. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
  2643. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  2644. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2645. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
  2646. ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
  2647. p, end - p);
  2648. if (ret != 0) {
  2649. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
  2650. mbedtls_ssl_send_alert_message(
  2651. ssl,
  2652. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2653. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  2654. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2655. }
  2656. } else
  2657. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  2658. {
  2659. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2660. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2661. }
  2662. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
  2663. if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
  2664. size_t sig_len, hashlen;
  2665. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2666. unsigned char hash[PSA_HASH_MAX_SIZE];
  2667. #else
  2668. unsigned char hash[MBEDTLS_MD_MAX_SIZE];
  2669. #endif
  2670. mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
  2671. mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
  2672. unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
  2673. size_t params_len = p - params;
  2674. void *rs_ctx = NULL;
  2675. mbedtls_pk_context *peer_pk;
  2676. /*
  2677. * Handle the digitally-signed structure
  2678. */
  2679. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2680. if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
  2681. if (ssl_parse_signature_algorithm(ssl, &p, end,
  2682. &md_alg, &pk_alg) != 0) {
  2683. MBEDTLS_SSL_DEBUG_MSG(1,
  2684. ("bad server key exchange message"));
  2685. mbedtls_ssl_send_alert_message(
  2686. ssl,
  2687. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2688. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  2689. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2690. }
  2691. if (pk_alg !=
  2692. mbedtls_ssl_get_ciphersuite_sig_pk_alg(ciphersuite_info)) {
  2693. MBEDTLS_SSL_DEBUG_MSG(1,
  2694. ("bad server key exchange message"));
  2695. mbedtls_ssl_send_alert_message(
  2696. ssl,
  2697. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2698. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  2699. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2700. }
  2701. } else
  2702. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2703. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  2704. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  2705. if (ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
  2706. pk_alg = mbedtls_ssl_get_ciphersuite_sig_pk_alg(ciphersuite_info);
  2707. /* Default hash for ECDSA is SHA-1 */
  2708. if (pk_alg == MBEDTLS_PK_ECDSA && md_alg == MBEDTLS_MD_NONE) {
  2709. md_alg = MBEDTLS_MD_SHA1;
  2710. }
  2711. } else
  2712. #endif
  2713. {
  2714. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2715. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2716. }
  2717. /*
  2718. * Read signature
  2719. */
  2720. if (p > end - 2) {
  2721. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
  2722. mbedtls_ssl_send_alert_message(
  2723. ssl,
  2724. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2725. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  2726. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2727. }
  2728. sig_len = (p[0] << 8) | p[1];
  2729. p += 2;
  2730. if (p != end - sig_len) {
  2731. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
  2732. mbedtls_ssl_send_alert_message(
  2733. ssl,
  2734. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2735. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  2736. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE;
  2737. }
  2738. MBEDTLS_SSL_DEBUG_BUF(3, "signature", p, sig_len);
  2739. /*
  2740. * Compute the hash that has been signed
  2741. */
  2742. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  2743. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  2744. if (md_alg == MBEDTLS_MD_NONE) {
  2745. hashlen = 36;
  2746. ret = mbedtls_ssl_get_key_exchange_md_ssl_tls(ssl, hash, params,
  2747. params_len);
  2748. if (ret != 0) {
  2749. return ret;
  2750. }
  2751. } else
  2752. #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
  2753. MBEDTLS_SSL_PROTO_TLS1_1 */
  2754. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  2755. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2756. if (md_alg != MBEDTLS_MD_NONE) {
  2757. ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
  2758. params, params_len,
  2759. md_alg);
  2760. if (ret != 0) {
  2761. return ret;
  2762. }
  2763. } else
  2764. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
  2765. MBEDTLS_SSL_PROTO_TLS1_2 */
  2766. {
  2767. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2768. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2769. }
  2770. MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
  2771. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  2772. peer_pk = &ssl->handshake->peer_pubkey;
  2773. #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2774. if (ssl->session_negotiate->peer_cert == NULL) {
  2775. /* Should never happen */
  2776. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2777. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2778. }
  2779. peer_pk = &ssl->session_negotiate->peer_cert->pk;
  2780. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2781. /*
  2782. * Verify signature
  2783. */
  2784. if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
  2785. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
  2786. mbedtls_ssl_send_alert_message(
  2787. ssl,
  2788. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2789. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  2790. return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
  2791. }
  2792. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2793. if (ssl->handshake->ecrs_enabled) {
  2794. rs_ctx = &ssl->handshake->ecrs_ctx.pk;
  2795. }
  2796. #endif /* MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED */
  2797. if ((ret = mbedtls_pk_verify_restartable(peer_pk,
  2798. md_alg, hash, hashlen, p, sig_len, rs_ctx)) != 0) {
  2799. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2800. if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
  2801. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
  2802. return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
  2803. }
  2804. #endif /* MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED */
  2805. mbedtls_ssl_send_alert_message(
  2806. ssl,
  2807. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2808. MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
  2809. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
  2810. return ret;
  2811. }
  2812. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  2813. /* We don't need the peer's public key anymore. Free it,
  2814. * so that more RAM is available for upcoming expensive
  2815. * operations like ECDHE. */
  2816. mbedtls_pk_free(peer_pk);
  2817. #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2818. }
  2819. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
  2820. exit:
  2821. ssl->state++;
  2822. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange"));
  2823. return 0;
  2824. }
  2825. #if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
  2826. MBEDTLS_CHECK_RETURN_CRITICAL
  2827. static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
  2828. {
  2829. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2830. ssl->handshake->ciphersuite_info;
  2831. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
  2832. if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
  2833. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
  2834. ssl->state++;
  2835. return 0;
  2836. }
  2837. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2838. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2839. }
  2840. #else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
  2841. MBEDTLS_CHECK_RETURN_CRITICAL
  2842. static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
  2843. {
  2844. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2845. unsigned char *buf;
  2846. size_t n = 0;
  2847. size_t cert_type_len = 0, dn_len = 0;
  2848. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2849. ssl->handshake->ciphersuite_info;
  2850. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
  2851. if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
  2852. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
  2853. ssl->state++;
  2854. return 0;
  2855. }
  2856. if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
  2857. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
  2858. return ret;
  2859. }
  2860. if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
  2861. MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
  2862. mbedtls_ssl_send_alert_message(
  2863. ssl,
  2864. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2865. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
  2866. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  2867. }
  2868. ssl->state++;
  2869. ssl->client_auth = (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST);
  2870. MBEDTLS_SSL_DEBUG_MSG(3, ("got %s certificate request",
  2871. ssl->client_auth ? "a" : "no"));
  2872. if (ssl->client_auth == 0) {
  2873. /* Current message is probably the ServerHelloDone */
  2874. ssl->keep_current_message = 1;
  2875. goto exit;
  2876. }
  2877. /*
  2878. * struct {
  2879. * ClientCertificateType certificate_types<1..2^8-1>;
  2880. * SignatureAndHashAlgorithm
  2881. * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
  2882. * DistinguishedName certificate_authorities<0..2^16-1>;
  2883. * } CertificateRequest;
  2884. *
  2885. * Since we only support a single certificate on clients, let's just
  2886. * ignore all the information that's supposed to help us pick a
  2887. * certificate.
  2888. *
  2889. * We could check that our certificate matches the request, and bail out
  2890. * if it doesn't, but it's simpler to just send the certificate anyway,
  2891. * and give the server the opportunity to decide if it should terminate
  2892. * the connection when it doesn't like our certificate.
  2893. *
  2894. * Same goes for the hash in TLS 1.2's signature_algorithms: at this
  2895. * point we only have one hash available (see comments in
  2896. * write_certificate_verify), so let's just use what we have.
  2897. *
  2898. * However, we still minimally parse the message to check it is at least
  2899. * superficially sane.
  2900. */
  2901. buf = ssl->in_msg;
  2902. /* certificate_types */
  2903. if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)) {
  2904. MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
  2905. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2906. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  2907. return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST;
  2908. }
  2909. cert_type_len = buf[mbedtls_ssl_hs_hdr_len(ssl)];
  2910. n = cert_type_len;
  2911. /*
  2912. * In the subsequent code there are two paths that read from buf:
  2913. * * the length of the signature algorithms field (if minor version of
  2914. * SSL is 3),
  2915. * * distinguished name length otherwise.
  2916. * Both reach at most the index:
  2917. * ...hdr_len + 2 + n,
  2918. * therefore the buffer length at this point must be greater than that
  2919. * regardless of the actual code path.
  2920. */
  2921. if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 2 + n) {
  2922. MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
  2923. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2924. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  2925. return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST;
  2926. }
  2927. /* supported_signature_algorithms */
  2928. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2929. if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
  2930. size_t sig_alg_len =
  2931. ((buf[mbedtls_ssl_hs_hdr_len(ssl) + 1 + n] << 8)
  2932. | (buf[mbedtls_ssl_hs_hdr_len(ssl) + 2 + n]));
  2933. #if defined(MBEDTLS_DEBUG_C)
  2934. unsigned char *sig_alg;
  2935. size_t i;
  2936. #endif
  2937. /*
  2938. * The furthest access in buf is in the loop few lines below:
  2939. * sig_alg[i + 1],
  2940. * where:
  2941. * sig_alg = buf + ...hdr_len + 3 + n,
  2942. * max(i) = sig_alg_len - 1.
  2943. * Therefore the furthest access is:
  2944. * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
  2945. * which reduces to:
  2946. * buf[...hdr_len + 3 + n + sig_alg_len],
  2947. * which is one less than we need the buf to be.
  2948. */
  2949. if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)
  2950. + 3 + n + sig_alg_len) {
  2951. MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
  2952. mbedtls_ssl_send_alert_message(
  2953. ssl,
  2954. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2955. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  2956. return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST;
  2957. }
  2958. #if defined(MBEDTLS_DEBUG_C)
  2959. sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n;
  2960. for (i = 0; i < sig_alg_len; i += 2) {
  2961. MBEDTLS_SSL_DEBUG_MSG(3,
  2962. ("Supported Signature Algorithm found: %d,%d",
  2963. sig_alg[i], sig_alg[i + 1]));
  2964. }
  2965. #endif
  2966. n += 2 + sig_alg_len;
  2967. }
  2968. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2969. /* certificate_authorities */
  2970. dn_len = ((buf[mbedtls_ssl_hs_hdr_len(ssl) + 1 + n] << 8)
  2971. | (buf[mbedtls_ssl_hs_hdr_len(ssl) + 2 + n]));
  2972. n += dn_len;
  2973. if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) {
  2974. MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
  2975. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2976. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  2977. return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST;
  2978. }
  2979. exit:
  2980. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
  2981. return 0;
  2982. }
  2983. #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
  2984. MBEDTLS_CHECK_RETURN_CRITICAL
  2985. static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl)
  2986. {
  2987. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2988. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done"));
  2989. if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
  2990. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
  2991. return ret;
  2992. }
  2993. if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
  2994. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
  2995. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  2996. }
  2997. if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) ||
  2998. ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) {
  2999. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
  3000. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  3001. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  3002. return MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE;
  3003. }
  3004. ssl->state++;
  3005. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  3006. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  3007. mbedtls_ssl_recv_flight_completed(ssl);
  3008. }
  3009. #endif
  3010. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done"));
  3011. return 0;
  3012. }
  3013. MBEDTLS_CHECK_RETURN_CRITICAL
  3014. static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
  3015. {
  3016. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3017. size_t header_len;
  3018. size_t content_len;
  3019. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  3020. ssl->handshake->ciphersuite_info;
  3021. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange"));
  3022. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
  3023. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
  3024. /*
  3025. * DHM key exchange -- send G^X mod P
  3026. */
  3027. content_len = ssl->handshake->dhm_ctx.len;
  3028. MBEDTLS_PUT_UINT16_BE(content_len, ssl->out_msg, 4);
  3029. header_len = 6;
  3030. ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
  3031. (int) mbedtls_mpi_size(&ssl->handshake->dhm_ctx.P),
  3032. &ssl->out_msg[header_len], content_len,
  3033. ssl->conf->f_rng, ssl->conf->p_rng);
  3034. if (ret != 0) {
  3035. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
  3036. return ret;
  3037. }
  3038. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
  3039. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
  3040. if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
  3041. ssl->handshake->premaster,
  3042. MBEDTLS_PREMASTER_SIZE,
  3043. &ssl->handshake->pmslen,
  3044. ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
  3045. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
  3046. return ret;
  3047. }
  3048. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
  3049. } else
  3050. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
  3051. #if defined(MBEDTLS_USE_PSA_CRYPTO) && \
  3052. (defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  3053. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED))
  3054. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  3055. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
  3056. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  3057. psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
  3058. psa_key_attributes_t key_attributes;
  3059. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  3060. unsigned char own_pubkey[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
  3061. size_t own_pubkey_len;
  3062. unsigned char *own_pubkey_ecpoint;
  3063. size_t own_pubkey_ecpoint_len;
  3064. header_len = 4;
  3065. MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
  3066. /*
  3067. * Generate EC private key for ECDHE exchange.
  3068. */
  3069. /* The master secret is obtained from the shared ECDH secret by
  3070. * applying the TLS 1.2 PRF with a specific salt and label. While
  3071. * the PSA Crypto API encourages combining key agreement schemes
  3072. * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
  3073. * yet support the provisioning of salt + label to the KDF.
  3074. * For the time being, we therefore need to split the computation
  3075. * of the ECDH secret and the application of the TLS 1.2 PRF. */
  3076. key_attributes = psa_key_attributes_init();
  3077. psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
  3078. psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
  3079. psa_set_key_type(&key_attributes, handshake->ecdh_psa_type);
  3080. psa_set_key_bits(&key_attributes, handshake->ecdh_bits);
  3081. /* Generate ECDH private key. */
  3082. status = psa_generate_key(&key_attributes,
  3083. &handshake->ecdh_psa_privkey);
  3084. if (status != PSA_SUCCESS) {
  3085. return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
  3086. }
  3087. /* Export the public part of the ECDH private key from PSA
  3088. * and convert it to ECPoint format used in ClientKeyExchange. */
  3089. status = psa_export_public_key(handshake->ecdh_psa_privkey,
  3090. own_pubkey, sizeof(own_pubkey),
  3091. &own_pubkey_len);
  3092. if (status != PSA_SUCCESS) {
  3093. psa_destroy_key(handshake->ecdh_psa_privkey);
  3094. handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
  3095. return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
  3096. }
  3097. if (mbedtls_psa_tls_psa_ec_to_ecpoint(own_pubkey,
  3098. own_pubkey_len,
  3099. &own_pubkey_ecpoint,
  3100. &own_pubkey_ecpoint_len) != 0) {
  3101. psa_destroy_key(handshake->ecdh_psa_privkey);
  3102. handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
  3103. return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
  3104. }
  3105. /* Copy ECPoint structure to outgoing message buffer. */
  3106. ssl->out_msg[header_len] = (unsigned char) own_pubkey_ecpoint_len;
  3107. memcpy(ssl->out_msg + header_len + 1,
  3108. own_pubkey_ecpoint, own_pubkey_ecpoint_len);
  3109. content_len = own_pubkey_ecpoint_len + 1;
  3110. /* The ECDH secret is the premaster secret used for key derivation. */
  3111. /* Compute ECDH shared secret. */
  3112. status = psa_raw_key_agreement(PSA_ALG_ECDH,
  3113. handshake->ecdh_psa_privkey,
  3114. handshake->ecdh_psa_peerkey,
  3115. handshake->ecdh_psa_peerkey_len,
  3116. ssl->handshake->premaster,
  3117. sizeof(ssl->handshake->premaster),
  3118. &ssl->handshake->pmslen);
  3119. destruction_status = psa_destroy_key(handshake->ecdh_psa_privkey);
  3120. handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
  3121. if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
  3122. return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
  3123. }
  3124. } else
  3125. #endif /* MBEDTLS_USE_PSA_CRYPTO &&
  3126. ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  3127. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
  3128. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  3129. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  3130. defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  3131. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  3132. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  3133. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
  3134. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
  3135. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
  3136. /*
  3137. * ECDH key exchange -- send client public value
  3138. */
  3139. header_len = 4;
  3140. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  3141. if (ssl->handshake->ecrs_enabled) {
  3142. if (ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret) {
  3143. goto ecdh_calc_secret;
  3144. }
  3145. mbedtls_ecdh_enable_restart(&ssl->handshake->ecdh_ctx);
  3146. }
  3147. #endif
  3148. ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
  3149. &content_len,
  3150. &ssl->out_msg[header_len], 1000,
  3151. ssl->conf->f_rng, ssl->conf->p_rng);
  3152. if (ret != 0) {
  3153. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
  3154. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  3155. if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
  3156. ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
  3157. }
  3158. #endif
  3159. return ret;
  3160. }
  3161. MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
  3162. MBEDTLS_DEBUG_ECDH_Q);
  3163. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  3164. if (ssl->handshake->ecrs_enabled) {
  3165. ssl->handshake->ecrs_n = content_len;
  3166. ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
  3167. }
  3168. ecdh_calc_secret:
  3169. if (ssl->handshake->ecrs_enabled) {
  3170. content_len = ssl->handshake->ecrs_n;
  3171. }
  3172. #endif
  3173. if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
  3174. &ssl->handshake->pmslen,
  3175. ssl->handshake->premaster,
  3176. MBEDTLS_MPI_MAX_SIZE,
  3177. ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
  3178. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
  3179. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  3180. if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
  3181. ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
  3182. }
  3183. #endif
  3184. return ret;
  3185. }
  3186. MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
  3187. MBEDTLS_DEBUG_ECDH_Z);
  3188. } else
  3189. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  3190. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  3191. MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  3192. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  3193. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  3194. if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
  3195. /*
  3196. * opaque psk_identity<0..2^16-1>;
  3197. */
  3198. if (ssl_conf_has_static_psk(ssl->conf) == 0) {
  3199. /* We don't offer PSK suites if we don't have a PSK,
  3200. * and we check that the server's choice is among the
  3201. * ciphersuites we offered, so this should never happen. */
  3202. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  3203. }
  3204. header_len = 4;
  3205. content_len = ssl->conf->psk_identity_len;
  3206. if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
  3207. MBEDTLS_SSL_DEBUG_MSG(1,
  3208. ("psk identity too long or SSL buffer too short"));
  3209. return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
  3210. }
  3211. ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
  3212. ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
  3213. memcpy(ssl->out_msg + header_len,
  3214. ssl->conf->psk_identity,
  3215. ssl->conf->psk_identity_len);
  3216. header_len += ssl->conf->psk_identity_len;
  3217. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
  3218. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
  3219. content_len = 0;
  3220. } else
  3221. #endif
  3222. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  3223. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
  3224. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3225. /* Opaque PSKs are currently only supported for PSK-only suites. */
  3226. if (ssl_conf_has_static_raw_psk(ssl->conf) == 0) {
  3227. MBEDTLS_SSL_DEBUG_MSG(1, ("opaque PSK not supported with RSA-PSK"));
  3228. return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  3229. }
  3230. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  3231. if ((ret = ssl_write_encrypted_pms(ssl, header_len,
  3232. &content_len, 2)) != 0) {
  3233. return ret;
  3234. }
  3235. } else
  3236. #endif
  3237. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  3238. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
  3239. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3240. /* Opaque PSKs are currently only supported for PSK-only suites. */
  3241. if (ssl_conf_has_static_raw_psk(ssl->conf) == 0) {
  3242. MBEDTLS_SSL_DEBUG_MSG(1, ("opaque PSK not supported with DHE-PSK"));
  3243. return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  3244. }
  3245. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  3246. /*
  3247. * ClientDiffieHellmanPublic public (DHM send G^X mod P)
  3248. */
  3249. content_len = ssl->handshake->dhm_ctx.len;
  3250. if (header_len + 2 + content_len >
  3251. MBEDTLS_SSL_OUT_CONTENT_LEN) {
  3252. MBEDTLS_SSL_DEBUG_MSG(1,
  3253. ("psk identity or DHM size too long or SSL buffer too short"));
  3254. return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
  3255. }
  3256. ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
  3257. ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
  3258. ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
  3259. (int) mbedtls_mpi_size(&ssl->handshake->dhm_ctx.P),
  3260. &ssl->out_msg[header_len], content_len,
  3261. ssl->conf->f_rng, ssl->conf->p_rng);
  3262. if (ret != 0) {
  3263. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
  3264. return ret;
  3265. }
  3266. } else
  3267. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  3268. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  3269. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
  3270. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3271. /* Opaque PSKs are currently only supported for PSK-only suites. */
  3272. if (ssl_conf_has_static_raw_psk(ssl->conf) == 0) {
  3273. MBEDTLS_SSL_DEBUG_MSG(1, ("opaque PSK not supported with ECDHE-PSK"));
  3274. return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  3275. }
  3276. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  3277. /*
  3278. * ClientECDiffieHellmanPublic public;
  3279. */
  3280. ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
  3281. &content_len,
  3282. &ssl->out_msg[header_len],
  3283. MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
  3284. ssl->conf->f_rng, ssl->conf->p_rng);
  3285. if (ret != 0) {
  3286. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
  3287. return ret;
  3288. }
  3289. MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
  3290. MBEDTLS_DEBUG_ECDH_Q);
  3291. } else
  3292. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  3293. {
  3294. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  3295. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  3296. }
  3297. #if defined(MBEDTLS_USE_PSA_CRYPTO) && \
  3298. defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
  3299. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
  3300. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
  3301. ssl_conf_has_static_raw_psk(ssl->conf) == 0) {
  3302. MBEDTLS_SSL_DEBUG_MSG(1,
  3303. ("skip PMS generation for opaque PSK"));
  3304. } else
  3305. #endif /* MBEDTLS_USE_PSA_CRYPTO &&
  3306. MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
  3307. if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
  3308. ciphersuite_info->key_exchange)) != 0) {
  3309. MBEDTLS_SSL_DEBUG_RET(1,
  3310. "mbedtls_ssl_psk_derive_premaster", ret);
  3311. return ret;
  3312. }
  3313. } else
  3314. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  3315. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  3316. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
  3317. header_len = 4;
  3318. if ((ret = ssl_write_encrypted_pms(ssl, header_len,
  3319. &content_len, 0)) != 0) {
  3320. return ret;
  3321. }
  3322. } else
  3323. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
  3324. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  3325. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
  3326. header_len = 4;
  3327. ret = mbedtls_ecjpake_write_round_two(&ssl->handshake->ecjpake_ctx,
  3328. ssl->out_msg + header_len,
  3329. MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
  3330. &content_len,
  3331. ssl->conf->f_rng, ssl->conf->p_rng);
  3332. if (ret != 0) {
  3333. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
  3334. return ret;
  3335. }
  3336. ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
  3337. ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
  3338. ssl->conf->f_rng, ssl->conf->p_rng);
  3339. if (ret != 0) {
  3340. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
  3341. return ret;
  3342. }
  3343. } else
  3344. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
  3345. {
  3346. ((void) ciphersuite_info);
  3347. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  3348. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  3349. }
  3350. ssl->out_msglen = header_len + content_len;
  3351. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  3352. ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
  3353. ssl->state++;
  3354. if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
  3355. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
  3356. return ret;
  3357. }
  3358. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange"));
  3359. return 0;
  3360. }
  3361. #if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
  3362. MBEDTLS_CHECK_RETURN_CRITICAL
  3363. static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
  3364. {
  3365. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  3366. ssl->handshake->ciphersuite_info;
  3367. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3368. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
  3369. if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
  3370. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
  3371. return ret;
  3372. }
  3373. if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
  3374. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
  3375. ssl->state++;
  3376. return 0;
  3377. }
  3378. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  3379. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  3380. }
  3381. #else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
  3382. MBEDTLS_CHECK_RETURN_CRITICAL
  3383. static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
  3384. {
  3385. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  3386. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  3387. ssl->handshake->ciphersuite_info;
  3388. size_t n = 0, offset = 0;
  3389. unsigned char hash[48];
  3390. unsigned char *hash_start = hash;
  3391. mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
  3392. size_t hashlen;
  3393. void *rs_ctx = NULL;
  3394. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
  3395. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  3396. if (ssl->handshake->ecrs_enabled &&
  3397. ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) {
  3398. goto sign;
  3399. }
  3400. #endif
  3401. if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
  3402. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
  3403. return ret;
  3404. }
  3405. if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
  3406. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
  3407. ssl->state++;
  3408. return 0;
  3409. }
  3410. if (ssl->client_auth == 0 || mbedtls_ssl_own_cert(ssl) == NULL) {
  3411. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
  3412. ssl->state++;
  3413. return 0;
  3414. }
  3415. if (mbedtls_ssl_own_key(ssl) == NULL) {
  3416. MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate"));
  3417. return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
  3418. }
  3419. /*
  3420. * Make a signature of the handshake digests
  3421. */
  3422. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  3423. if (ssl->handshake->ecrs_enabled) {
  3424. ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
  3425. }
  3426. sign:
  3427. #endif
  3428. ssl->handshake->calc_verify(ssl, hash, &hashlen);
  3429. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  3430. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  3431. if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
  3432. /*
  3433. * digitally-signed struct {
  3434. * opaque md5_hash[16];
  3435. * opaque sha_hash[20];
  3436. * };
  3437. *
  3438. * md5_hash
  3439. * MD5(handshake_messages);
  3440. *
  3441. * sha_hash
  3442. * SHA(handshake_messages);
  3443. */
  3444. md_alg = MBEDTLS_MD_NONE;
  3445. /*
  3446. * For ECDSA, default hash is SHA-1 only
  3447. */
  3448. if (mbedtls_pk_can_do(mbedtls_ssl_own_key(ssl), MBEDTLS_PK_ECDSA)) {
  3449. hash_start += 16;
  3450. hashlen -= 16;
  3451. md_alg = MBEDTLS_MD_SHA1;
  3452. }
  3453. } else
  3454. #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
  3455. MBEDTLS_SSL_PROTO_TLS1_1 */
  3456. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  3457. if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
  3458. /*
  3459. * digitally-signed struct {
  3460. * opaque handshake_messages[handshake_messages_length];
  3461. * };
  3462. *
  3463. * Taking shortcut here. We assume that the server always allows the
  3464. * PRF Hash function and has sent it in the allowed signature
  3465. * algorithms list received in the Certificate Request message.
  3466. *
  3467. * Until we encounter a server that does not, we will take this
  3468. * shortcut.
  3469. *
  3470. * Reason: Otherwise we should have running hashes for SHA512 and
  3471. * SHA224 in order to satisfy 'weird' needs from the server
  3472. * side.
  3473. */
  3474. if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
  3475. md_alg = MBEDTLS_MD_SHA384;
  3476. ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
  3477. } else {
  3478. md_alg = MBEDTLS_MD_SHA256;
  3479. ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
  3480. }
  3481. ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl));
  3482. /* Info from md_alg will be used instead */
  3483. hashlen = 0;
  3484. offset = 2;
  3485. } else
  3486. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  3487. {
  3488. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  3489. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  3490. }
  3491. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  3492. if (ssl->handshake->ecrs_enabled) {
  3493. rs_ctx = &ssl->handshake->ecrs_ctx.pk;
  3494. }
  3495. #endif
  3496. if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl),
  3497. md_alg, hash_start, hashlen,
  3498. ssl->out_msg + 6 + offset, &n,
  3499. ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx)) != 0) {
  3500. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
  3501. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  3502. if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
  3503. ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
  3504. }
  3505. #endif
  3506. return ret;
  3507. }
  3508. MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4);
  3509. ssl->out_msglen = 6 + n + offset;
  3510. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  3511. ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
  3512. ssl->state++;
  3513. if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
  3514. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
  3515. return ret;
  3516. }
  3517. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
  3518. return ret;
  3519. }
  3520. #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
  3521. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  3522. MBEDTLS_CHECK_RETURN_CRITICAL
  3523. static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl)
  3524. {
  3525. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3526. uint32_t lifetime;
  3527. size_t ticket_len;
  3528. unsigned char *ticket;
  3529. const unsigned char *msg;
  3530. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
  3531. if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
  3532. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
  3533. return ret;
  3534. }
  3535. if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
  3536. MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
  3537. mbedtls_ssl_send_alert_message(
  3538. ssl,
  3539. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  3540. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
  3541. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  3542. }
  3543. /*
  3544. * struct {
  3545. * uint32 ticket_lifetime_hint;
  3546. * opaque ticket<0..2^16-1>;
  3547. * } NewSessionTicket;
  3548. *
  3549. * 0 . 3 ticket_lifetime_hint
  3550. * 4 . 5 ticket_len (n)
  3551. * 6 . 5+n ticket content
  3552. */
  3553. if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
  3554. ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) {
  3555. MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
  3556. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  3557. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  3558. return MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET;
  3559. }
  3560. msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
  3561. lifetime = (((uint32_t) msg[0]) << 24) | (msg[1] << 16) |
  3562. (msg[2] << 8) | (msg[3]);
  3563. ticket_len = (msg[4] << 8) | (msg[5]);
  3564. if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) {
  3565. MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
  3566. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  3567. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  3568. return MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET;
  3569. }
  3570. MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len));
  3571. /* We're not waiting for a NewSessionTicket message any more */
  3572. ssl->handshake->new_session_ticket = 0;
  3573. ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
  3574. /*
  3575. * Zero-length ticket means the server changed his mind and doesn't want
  3576. * to send a ticket after all, so just forget it
  3577. */
  3578. if (ticket_len == 0) {
  3579. return 0;
  3580. }
  3581. if (ssl->session != NULL && ssl->session->ticket != NULL) {
  3582. mbedtls_platform_zeroize(ssl->session->ticket,
  3583. ssl->session->ticket_len);
  3584. mbedtls_free(ssl->session->ticket);
  3585. ssl->session->ticket = NULL;
  3586. ssl->session->ticket_len = 0;
  3587. }
  3588. mbedtls_platform_zeroize(ssl->session_negotiate->ticket,
  3589. ssl->session_negotiate->ticket_len);
  3590. mbedtls_free(ssl->session_negotiate->ticket);
  3591. ssl->session_negotiate->ticket = NULL;
  3592. ssl->session_negotiate->ticket_len = 0;
  3593. if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
  3594. MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
  3595. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  3596. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
  3597. return MBEDTLS_ERR_SSL_ALLOC_FAILED;
  3598. }
  3599. memcpy(ticket, msg + 6, ticket_len);
  3600. ssl->session_negotiate->ticket = ticket;
  3601. ssl->session_negotiate->ticket_len = ticket_len;
  3602. ssl->session_negotiate->ticket_lifetime = lifetime;
  3603. /*
  3604. * RFC 5077 section 3.4:
  3605. * "If the client receives a session ticket from the server, then it
  3606. * discards any Session ID that was sent in the ServerHello."
  3607. */
  3608. MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id"));
  3609. ssl->session_negotiate->id_len = 0;
  3610. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
  3611. return 0;
  3612. }
  3613. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  3614. /*
  3615. * SSL handshake -- client side -- single step
  3616. */
  3617. int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
  3618. {
  3619. int ret = 0;
  3620. if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
  3621. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  3622. }
  3623. MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %d", ssl->state));
  3624. if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
  3625. return ret;
  3626. }
  3627. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  3628. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  3629. ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
  3630. if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
  3631. return ret;
  3632. }
  3633. }
  3634. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  3635. /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
  3636. * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
  3637. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  3638. if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
  3639. ssl->handshake->new_session_ticket != 0) {
  3640. ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
  3641. }
  3642. #endif
  3643. switch (ssl->state) {
  3644. case MBEDTLS_SSL_HELLO_REQUEST:
  3645. ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
  3646. break;
  3647. /*
  3648. * ==> ClientHello
  3649. */
  3650. case MBEDTLS_SSL_CLIENT_HELLO:
  3651. ret = ssl_write_client_hello(ssl);
  3652. break;
  3653. /*
  3654. * <== ServerHello
  3655. * Certificate
  3656. * ( ServerKeyExchange )
  3657. * ( CertificateRequest )
  3658. * ServerHelloDone
  3659. */
  3660. case MBEDTLS_SSL_SERVER_HELLO:
  3661. ret = ssl_parse_server_hello(ssl);
  3662. break;
  3663. case MBEDTLS_SSL_SERVER_CERTIFICATE:
  3664. ret = mbedtls_ssl_parse_certificate(ssl);
  3665. break;
  3666. case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
  3667. ret = ssl_parse_server_key_exchange(ssl);
  3668. break;
  3669. case MBEDTLS_SSL_CERTIFICATE_REQUEST:
  3670. ret = ssl_parse_certificate_request(ssl);
  3671. break;
  3672. case MBEDTLS_SSL_SERVER_HELLO_DONE:
  3673. ret = ssl_parse_server_hello_done(ssl);
  3674. break;
  3675. /*
  3676. * ==> ( Certificate/Alert )
  3677. * ClientKeyExchange
  3678. * ( CertificateVerify )
  3679. * ChangeCipherSpec
  3680. * Finished
  3681. */
  3682. case MBEDTLS_SSL_CLIENT_CERTIFICATE:
  3683. ret = mbedtls_ssl_write_certificate(ssl);
  3684. break;
  3685. case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
  3686. ret = ssl_write_client_key_exchange(ssl);
  3687. break;
  3688. case MBEDTLS_SSL_CERTIFICATE_VERIFY:
  3689. ret = ssl_write_certificate_verify(ssl);
  3690. break;
  3691. case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
  3692. ret = mbedtls_ssl_write_change_cipher_spec(ssl);
  3693. break;
  3694. case MBEDTLS_SSL_CLIENT_FINISHED:
  3695. ret = mbedtls_ssl_write_finished(ssl);
  3696. break;
  3697. /*
  3698. * <== ( NewSessionTicket )
  3699. * ChangeCipherSpec
  3700. * Finished
  3701. */
  3702. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  3703. case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
  3704. ret = ssl_parse_new_session_ticket(ssl);
  3705. break;
  3706. #endif
  3707. case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
  3708. ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
  3709. break;
  3710. case MBEDTLS_SSL_SERVER_FINISHED:
  3711. ret = mbedtls_ssl_parse_finished(ssl);
  3712. break;
  3713. case MBEDTLS_SSL_FLUSH_BUFFERS:
  3714. MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
  3715. ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
  3716. break;
  3717. case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
  3718. mbedtls_ssl_handshake_wrapup(ssl);
  3719. break;
  3720. default:
  3721. MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
  3722. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  3723. }
  3724. return ret;
  3725. }
  3726. #endif /* MBEDTLS_SSL_CLI_C */