ssl_srv.c 145 KB

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