ssl_cli.c 149 KB

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