ssl_srv.c 138 KB

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