ssl_srv.c 163 KB

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