ssl_cli.c 149 KB

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