2
0

statem_clnt.c 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. * Copyright 2005 Nokia. All rights reserved.
  5. *
  6. * Licensed under the OpenSSL license (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. #include <stdio.h>
  12. #include <time.h>
  13. #include <assert.h>
  14. #include "../ssl_local.h"
  15. #include "statem_local.h"
  16. #include <openssl/buffer.h>
  17. #include <openssl/rand.h>
  18. #include <openssl/objects.h>
  19. #include <openssl/evp.h>
  20. #include <openssl/md5.h>
  21. #include <openssl/dh.h>
  22. #include <openssl/bn.h>
  23. #include <openssl/engine.h>
  24. #include <internal/cryptlib.h>
  25. static MSG_PROCESS_RETURN tls_process_as_hello_retry_request(SSL *s, PACKET *pkt);
  26. static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL *s, PACKET *pkt);
  27. static ossl_inline int cert_req_allowed(SSL *s);
  28. static int key_exchange_expected(SSL *s);
  29. static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
  30. WPACKET *pkt);
  31. /*
  32. * Is a CertificateRequest message allowed at the moment or not?
  33. *
  34. * Return values are:
  35. * 1: Yes
  36. * 0: No
  37. */
  38. static ossl_inline int cert_req_allowed(SSL *s)
  39. {
  40. /* TLS does not like anon-DH with client cert */
  41. if ((s->version > SSL3_VERSION
  42. && (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL))
  43. || (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK)))
  44. return 0;
  45. return 1;
  46. }
  47. /*
  48. * Should we expect the ServerKeyExchange message or not?
  49. *
  50. * Return values are:
  51. * 1: Yes
  52. * 0: No
  53. */
  54. static int key_exchange_expected(SSL *s)
  55. {
  56. long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  57. /*
  58. * Can't skip server key exchange if this is an ephemeral
  59. * ciphersuite or for SRP
  60. */
  61. if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK
  62. | SSL_kSRP)) {
  63. return 1;
  64. }
  65. return 0;
  66. }
  67. /*
  68. * ossl_statem_client_read_transition() encapsulates the logic for the allowed
  69. * handshake state transitions when a TLS1.3 client is reading messages from the
  70. * server. The message type that the server has sent is provided in |mt|. The
  71. * current state is in |s->statem.hand_state|.
  72. *
  73. * Return values are 1 for success (transition allowed) and 0 on error
  74. * (transition not allowed)
  75. */
  76. static int ossl_statem_client13_read_transition(SSL *s, int mt)
  77. {
  78. OSSL_STATEM *st = &s->statem;
  79. /*
  80. * Note: There is no case for TLS_ST_CW_CLNT_HELLO, because we haven't
  81. * yet negotiated TLSv1.3 at that point so that is handled by
  82. * ossl_statem_client_read_transition()
  83. */
  84. switch (st->hand_state) {
  85. default:
  86. break;
  87. case TLS_ST_CW_CLNT_HELLO:
  88. /*
  89. * This must a ClientHello following a HelloRetryRequest, so the only
  90. * thing we can get now is a ServerHello.
  91. */
  92. if (mt == SSL3_MT_SERVER_HELLO) {
  93. st->hand_state = TLS_ST_CR_SRVR_HELLO;
  94. return 1;
  95. }
  96. break;
  97. case TLS_ST_CR_SRVR_HELLO:
  98. if (mt == SSL3_MT_ENCRYPTED_EXTENSIONS) {
  99. st->hand_state = TLS_ST_CR_ENCRYPTED_EXTENSIONS;
  100. return 1;
  101. }
  102. break;
  103. case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
  104. if (s->hit) {
  105. if (mt == SSL3_MT_FINISHED) {
  106. st->hand_state = TLS_ST_CR_FINISHED;
  107. return 1;
  108. }
  109. } else {
  110. if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
  111. st->hand_state = TLS_ST_CR_CERT_REQ;
  112. return 1;
  113. }
  114. if (mt == SSL3_MT_CERTIFICATE) {
  115. st->hand_state = TLS_ST_CR_CERT;
  116. return 1;
  117. }
  118. }
  119. break;
  120. case TLS_ST_CR_CERT_REQ:
  121. if (mt == SSL3_MT_CERTIFICATE) {
  122. st->hand_state = TLS_ST_CR_CERT;
  123. return 1;
  124. }
  125. break;
  126. case TLS_ST_CR_CERT:
  127. if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
  128. st->hand_state = TLS_ST_CR_CERT_VRFY;
  129. return 1;
  130. }
  131. break;
  132. case TLS_ST_CR_CERT_VRFY:
  133. if (mt == SSL3_MT_FINISHED) {
  134. st->hand_state = TLS_ST_CR_FINISHED;
  135. return 1;
  136. }
  137. break;
  138. case TLS_ST_OK:
  139. if (mt == SSL3_MT_NEWSESSION_TICKET) {
  140. st->hand_state = TLS_ST_CR_SESSION_TICKET;
  141. return 1;
  142. }
  143. if (mt == SSL3_MT_KEY_UPDATE) {
  144. st->hand_state = TLS_ST_CR_KEY_UPDATE;
  145. return 1;
  146. }
  147. if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
  148. #if DTLS_MAX_VERSION != DTLS1_2_VERSION
  149. # error TODO(DTLS1.3): Restore digest for PHA before adding message.
  150. #endif
  151. if (!SSL_IS_DTLS(s) && s->post_handshake_auth == SSL_PHA_EXT_SENT) {
  152. s->post_handshake_auth = SSL_PHA_REQUESTED;
  153. /*
  154. * In TLS, this is called before the message is added to the
  155. * digest. In DTLS, this is expected to be called after adding
  156. * to the digest. Either move the digest restore, or add the
  157. * message here after the swap, or do it after the clientFinished?
  158. */
  159. if (!tls13_restore_handshake_digest_for_pha(s)) {
  160. /* SSLfatal() already called */
  161. return 0;
  162. }
  163. st->hand_state = TLS_ST_CR_CERT_REQ;
  164. return 1;
  165. }
  166. }
  167. break;
  168. }
  169. /* No valid transition found */
  170. return 0;
  171. }
  172. /*
  173. * ossl_statem_client_read_transition() encapsulates the logic for the allowed
  174. * handshake state transitions when the client is reading messages from the
  175. * server. The message type that the server has sent is provided in |mt|. The
  176. * current state is in |s->statem.hand_state|.
  177. *
  178. * Return values are 1 for success (transition allowed) and 0 on error
  179. * (transition not allowed)
  180. */
  181. int ossl_statem_client_read_transition(SSL *s, int mt)
  182. {
  183. OSSL_STATEM *st = &s->statem;
  184. int ske_expected;
  185. /*
  186. * Note that after writing the first ClientHello we don't know what version
  187. * we are going to negotiate yet, so we don't take this branch until later.
  188. */
  189. if (SSL_IS_TLS13(s)) {
  190. if (!ossl_statem_client13_read_transition(s, mt))
  191. goto err;
  192. return 1;
  193. }
  194. switch (st->hand_state) {
  195. default:
  196. break;
  197. case TLS_ST_CW_CLNT_HELLO:
  198. if (mt == SSL3_MT_SERVER_HELLO) {
  199. st->hand_state = TLS_ST_CR_SRVR_HELLO;
  200. return 1;
  201. }
  202. if (SSL_IS_DTLS(s)) {
  203. if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
  204. st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
  205. return 1;
  206. }
  207. }
  208. break;
  209. case TLS_ST_EARLY_DATA:
  210. /*
  211. * We've not actually selected TLSv1.3 yet, but we have sent early
  212. * data. The only thing allowed now is a ServerHello or a
  213. * HelloRetryRequest.
  214. */
  215. if (mt == SSL3_MT_SERVER_HELLO) {
  216. st->hand_state = TLS_ST_CR_SRVR_HELLO;
  217. return 1;
  218. }
  219. break;
  220. case TLS_ST_CR_SRVR_HELLO:
  221. if (s->hit) {
  222. if (s->ext.ticket_expected) {
  223. if (mt == SSL3_MT_NEWSESSION_TICKET) {
  224. st->hand_state = TLS_ST_CR_SESSION_TICKET;
  225. return 1;
  226. }
  227. } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  228. st->hand_state = TLS_ST_CR_CHANGE;
  229. return 1;
  230. }
  231. } else {
  232. if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
  233. st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
  234. return 1;
  235. } else if (s->version >= TLS1_VERSION
  236. && s->ext.session_secret_cb != NULL
  237. && s->session->ext.tick != NULL
  238. && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  239. /*
  240. * Normally, we can tell if the server is resuming the session
  241. * from the session ID. EAP-FAST (RFC 4851), however, relies on
  242. * the next server message after the ServerHello to determine if
  243. * the server is resuming.
  244. */
  245. s->hit = 1;
  246. st->hand_state = TLS_ST_CR_CHANGE;
  247. return 1;
  248. } else if (!(s->s3->tmp.new_cipher->algorithm_auth
  249. & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
  250. if (mt == SSL3_MT_CERTIFICATE) {
  251. st->hand_state = TLS_ST_CR_CERT;
  252. return 1;
  253. }
  254. } else {
  255. ske_expected = key_exchange_expected(s);
  256. /* SKE is optional for some PSK ciphersuites */
  257. if (ske_expected
  258. || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
  259. && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
  260. if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
  261. st->hand_state = TLS_ST_CR_KEY_EXCH;
  262. return 1;
  263. }
  264. } else if (mt == SSL3_MT_CERTIFICATE_REQUEST
  265. && cert_req_allowed(s)) {
  266. st->hand_state = TLS_ST_CR_CERT_REQ;
  267. return 1;
  268. } else if (mt == SSL3_MT_SERVER_DONE) {
  269. st->hand_state = TLS_ST_CR_SRVR_DONE;
  270. return 1;
  271. }
  272. }
  273. }
  274. break;
  275. case TLS_ST_CR_CERT:
  276. /*
  277. * The CertificateStatus message is optional even if
  278. * |ext.status_expected| is set
  279. */
  280. if (s->ext.status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) {
  281. st->hand_state = TLS_ST_CR_CERT_STATUS;
  282. return 1;
  283. }
  284. /* Fall through */
  285. case TLS_ST_CR_CERT_STATUS:
  286. ske_expected = key_exchange_expected(s);
  287. /* SKE is optional for some PSK ciphersuites */
  288. if (ske_expected || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
  289. && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
  290. if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
  291. st->hand_state = TLS_ST_CR_KEY_EXCH;
  292. return 1;
  293. }
  294. goto err;
  295. }
  296. /* Fall through */
  297. case TLS_ST_CR_KEY_EXCH:
  298. if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
  299. if (cert_req_allowed(s)) {
  300. st->hand_state = TLS_ST_CR_CERT_REQ;
  301. return 1;
  302. }
  303. goto err;
  304. }
  305. /* Fall through */
  306. case TLS_ST_CR_CERT_REQ:
  307. if (mt == SSL3_MT_SERVER_DONE) {
  308. st->hand_state = TLS_ST_CR_SRVR_DONE;
  309. return 1;
  310. }
  311. break;
  312. case TLS_ST_CW_FINISHED:
  313. if (s->ext.ticket_expected) {
  314. if (mt == SSL3_MT_NEWSESSION_TICKET) {
  315. st->hand_state = TLS_ST_CR_SESSION_TICKET;
  316. return 1;
  317. }
  318. } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  319. st->hand_state = TLS_ST_CR_CHANGE;
  320. return 1;
  321. }
  322. break;
  323. case TLS_ST_CR_SESSION_TICKET:
  324. if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  325. st->hand_state = TLS_ST_CR_CHANGE;
  326. return 1;
  327. }
  328. break;
  329. case TLS_ST_CR_CHANGE:
  330. if (mt == SSL3_MT_FINISHED) {
  331. st->hand_state = TLS_ST_CR_FINISHED;
  332. return 1;
  333. }
  334. break;
  335. case TLS_ST_OK:
  336. if (mt == SSL3_MT_HELLO_REQUEST) {
  337. st->hand_state = TLS_ST_CR_HELLO_REQ;
  338. return 1;
  339. }
  340. break;
  341. }
  342. err:
  343. /* No valid transition found */
  344. if (SSL_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  345. BIO *rbio;
  346. /*
  347. * CCS messages don't have a message sequence number so this is probably
  348. * because of an out-of-order CCS. We'll just drop it.
  349. */
  350. s->init_num = 0;
  351. s->rwstate = SSL_READING;
  352. rbio = SSL_get_rbio(s);
  353. BIO_clear_retry_flags(rbio);
  354. BIO_set_retry_read(rbio);
  355. return 0;
  356. }
  357. SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE,
  358. SSL_F_OSSL_STATEM_CLIENT_READ_TRANSITION,
  359. SSL_R_UNEXPECTED_MESSAGE);
  360. return 0;
  361. }
  362. /*
  363. * ossl_statem_client13_write_transition() works out what handshake state to
  364. * move to next when the TLSv1.3 client is writing messages to be sent to the
  365. * server.
  366. */
  367. static WRITE_TRAN ossl_statem_client13_write_transition(SSL *s)
  368. {
  369. OSSL_STATEM *st = &s->statem;
  370. /*
  371. * Note: There are no cases for TLS_ST_BEFORE because we haven't negotiated
  372. * TLSv1.3 yet at that point. They are handled by
  373. * ossl_statem_client_write_transition().
  374. */
  375. switch (st->hand_state) {
  376. default:
  377. /* Shouldn't happen */
  378. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  379. SSL_F_OSSL_STATEM_CLIENT13_WRITE_TRANSITION,
  380. ERR_R_INTERNAL_ERROR);
  381. return WRITE_TRAN_ERROR;
  382. case TLS_ST_CR_CERT_REQ:
  383. if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
  384. st->hand_state = TLS_ST_CW_CERT;
  385. return WRITE_TRAN_CONTINUE;
  386. }
  387. /*
  388. * We should only get here if we received a CertificateRequest after
  389. * we already sent close_notify
  390. */
  391. if (!ossl_assert((s->shutdown & SSL_SENT_SHUTDOWN) != 0)) {
  392. /* Shouldn't happen - same as default case */
  393. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  394. SSL_F_OSSL_STATEM_CLIENT13_WRITE_TRANSITION,
  395. ERR_R_INTERNAL_ERROR);
  396. return WRITE_TRAN_ERROR;
  397. }
  398. st->hand_state = TLS_ST_OK;
  399. return WRITE_TRAN_CONTINUE;
  400. case TLS_ST_CR_FINISHED:
  401. if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY
  402. || s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING)
  403. st->hand_state = TLS_ST_PENDING_EARLY_DATA_END;
  404. else if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
  405. && s->hello_retry_request == SSL_HRR_NONE)
  406. st->hand_state = TLS_ST_CW_CHANGE;
  407. else
  408. st->hand_state = (s->s3->tmp.cert_req != 0) ? TLS_ST_CW_CERT
  409. : TLS_ST_CW_FINISHED;
  410. return WRITE_TRAN_CONTINUE;
  411. case TLS_ST_PENDING_EARLY_DATA_END:
  412. if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
  413. st->hand_state = TLS_ST_CW_END_OF_EARLY_DATA;
  414. return WRITE_TRAN_CONTINUE;
  415. }
  416. /* Fall through */
  417. case TLS_ST_CW_END_OF_EARLY_DATA:
  418. case TLS_ST_CW_CHANGE:
  419. st->hand_state = (s->s3->tmp.cert_req != 0) ? TLS_ST_CW_CERT
  420. : TLS_ST_CW_FINISHED;
  421. return WRITE_TRAN_CONTINUE;
  422. case TLS_ST_CW_CERT:
  423. /* If a non-empty Certificate we also send CertificateVerify */
  424. st->hand_state = (s->s3->tmp.cert_req == 1) ? TLS_ST_CW_CERT_VRFY
  425. : TLS_ST_CW_FINISHED;
  426. return WRITE_TRAN_CONTINUE;
  427. case TLS_ST_CW_CERT_VRFY:
  428. st->hand_state = TLS_ST_CW_FINISHED;
  429. return WRITE_TRAN_CONTINUE;
  430. case TLS_ST_CR_KEY_UPDATE:
  431. case TLS_ST_CW_KEY_UPDATE:
  432. case TLS_ST_CR_SESSION_TICKET:
  433. case TLS_ST_CW_FINISHED:
  434. st->hand_state = TLS_ST_OK;
  435. return WRITE_TRAN_CONTINUE;
  436. case TLS_ST_OK:
  437. if (s->key_update != SSL_KEY_UPDATE_NONE) {
  438. st->hand_state = TLS_ST_CW_KEY_UPDATE;
  439. return WRITE_TRAN_CONTINUE;
  440. }
  441. /* Try to read from the server instead */
  442. return WRITE_TRAN_FINISHED;
  443. }
  444. }
  445. /*
  446. * ossl_statem_client_write_transition() works out what handshake state to
  447. * move to next when the client is writing messages to be sent to the server.
  448. */
  449. WRITE_TRAN ossl_statem_client_write_transition(SSL *s)
  450. {
  451. OSSL_STATEM *st = &s->statem;
  452. /*
  453. * Note that immediately before/after a ClientHello we don't know what
  454. * version we are going to negotiate yet, so we don't take this branch until
  455. * later
  456. */
  457. if (SSL_IS_TLS13(s))
  458. return ossl_statem_client13_write_transition(s);
  459. switch (st->hand_state) {
  460. default:
  461. /* Shouldn't happen */
  462. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  463. SSL_F_OSSL_STATEM_CLIENT_WRITE_TRANSITION,
  464. ERR_R_INTERNAL_ERROR);
  465. return WRITE_TRAN_ERROR;
  466. case TLS_ST_OK:
  467. if (!s->renegotiate) {
  468. /*
  469. * We haven't requested a renegotiation ourselves so we must have
  470. * received a message from the server. Better read it.
  471. */
  472. return WRITE_TRAN_FINISHED;
  473. }
  474. /* Renegotiation */
  475. /* fall thru */
  476. case TLS_ST_BEFORE:
  477. st->hand_state = TLS_ST_CW_CLNT_HELLO;
  478. return WRITE_TRAN_CONTINUE;
  479. case TLS_ST_CW_CLNT_HELLO:
  480. if (s->early_data_state == SSL_EARLY_DATA_CONNECTING) {
  481. /*
  482. * We are assuming this is a TLSv1.3 connection, although we haven't
  483. * actually selected a version yet.
  484. */
  485. if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0)
  486. st->hand_state = TLS_ST_CW_CHANGE;
  487. else
  488. st->hand_state = TLS_ST_EARLY_DATA;
  489. return WRITE_TRAN_CONTINUE;
  490. }
  491. /*
  492. * No transition at the end of writing because we don't know what
  493. * we will be sent
  494. */
  495. return WRITE_TRAN_FINISHED;
  496. case TLS_ST_CR_SRVR_HELLO:
  497. /*
  498. * We only get here in TLSv1.3. We just received an HRR, so issue a
  499. * CCS unless middlebox compat mode is off, or we already issued one
  500. * because we did early data.
  501. */
  502. if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
  503. && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING)
  504. st->hand_state = TLS_ST_CW_CHANGE;
  505. else
  506. st->hand_state = TLS_ST_CW_CLNT_HELLO;
  507. return WRITE_TRAN_CONTINUE;
  508. case TLS_ST_EARLY_DATA:
  509. return WRITE_TRAN_FINISHED;
  510. case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
  511. st->hand_state = TLS_ST_CW_CLNT_HELLO;
  512. return WRITE_TRAN_CONTINUE;
  513. case TLS_ST_CR_SRVR_DONE:
  514. if (s->s3->tmp.cert_req)
  515. st->hand_state = TLS_ST_CW_CERT;
  516. else
  517. st->hand_state = TLS_ST_CW_KEY_EXCH;
  518. return WRITE_TRAN_CONTINUE;
  519. case TLS_ST_CW_CERT:
  520. st->hand_state = TLS_ST_CW_KEY_EXCH;
  521. return WRITE_TRAN_CONTINUE;
  522. case TLS_ST_CW_KEY_EXCH:
  523. /*
  524. * For TLS, cert_req is set to 2, so a cert chain of nothing is
  525. * sent, but no verify packet is sent
  526. */
  527. /*
  528. * XXX: For now, we do not support client authentication in ECDH
  529. * cipher suites with ECDH (rather than ECDSA) certificates. We
  530. * need to skip the certificate verify message when client's
  531. * ECDH public key is sent inside the client certificate.
  532. */
  533. if (s->s3->tmp.cert_req == 1) {
  534. st->hand_state = TLS_ST_CW_CERT_VRFY;
  535. } else {
  536. st->hand_state = TLS_ST_CW_CHANGE;
  537. }
  538. if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
  539. st->hand_state = TLS_ST_CW_CHANGE;
  540. }
  541. return WRITE_TRAN_CONTINUE;
  542. case TLS_ST_CW_CERT_VRFY:
  543. st->hand_state = TLS_ST_CW_CHANGE;
  544. return WRITE_TRAN_CONTINUE;
  545. case TLS_ST_CW_CHANGE:
  546. if (s->hello_retry_request == SSL_HRR_PENDING) {
  547. st->hand_state = TLS_ST_CW_CLNT_HELLO;
  548. } else if (s->early_data_state == SSL_EARLY_DATA_CONNECTING) {
  549. st->hand_state = TLS_ST_EARLY_DATA;
  550. } else {
  551. #if defined(OPENSSL_NO_NEXTPROTONEG)
  552. st->hand_state = TLS_ST_CW_FINISHED;
  553. #else
  554. if (!SSL_IS_DTLS(s) && s->s3->npn_seen)
  555. st->hand_state = TLS_ST_CW_NEXT_PROTO;
  556. else
  557. st->hand_state = TLS_ST_CW_FINISHED;
  558. #endif
  559. }
  560. return WRITE_TRAN_CONTINUE;
  561. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  562. case TLS_ST_CW_NEXT_PROTO:
  563. st->hand_state = TLS_ST_CW_FINISHED;
  564. return WRITE_TRAN_CONTINUE;
  565. #endif
  566. case TLS_ST_CW_FINISHED:
  567. if (s->hit) {
  568. st->hand_state = TLS_ST_OK;
  569. return WRITE_TRAN_CONTINUE;
  570. } else {
  571. return WRITE_TRAN_FINISHED;
  572. }
  573. case TLS_ST_CR_FINISHED:
  574. if (s->hit) {
  575. st->hand_state = TLS_ST_CW_CHANGE;
  576. return WRITE_TRAN_CONTINUE;
  577. } else {
  578. st->hand_state = TLS_ST_OK;
  579. return WRITE_TRAN_CONTINUE;
  580. }
  581. case TLS_ST_CR_HELLO_REQ:
  582. /*
  583. * If we can renegotiate now then do so, otherwise wait for a more
  584. * convenient time.
  585. */
  586. if (ssl3_renegotiate_check(s, 1)) {
  587. if (!tls_setup_handshake(s)) {
  588. /* SSLfatal() already called */
  589. return WRITE_TRAN_ERROR;
  590. }
  591. st->hand_state = TLS_ST_CW_CLNT_HELLO;
  592. return WRITE_TRAN_CONTINUE;
  593. }
  594. st->hand_state = TLS_ST_OK;
  595. return WRITE_TRAN_CONTINUE;
  596. }
  597. }
  598. /*
  599. * Perform any pre work that needs to be done prior to sending a message from
  600. * the client to the server.
  601. */
  602. WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst)
  603. {
  604. OSSL_STATEM *st = &s->statem;
  605. switch (st->hand_state) {
  606. default:
  607. /* No pre work to be done */
  608. break;
  609. case TLS_ST_CW_CLNT_HELLO:
  610. s->shutdown = 0;
  611. if (SSL_IS_DTLS(s)) {
  612. /* every DTLS ClientHello resets Finished MAC */
  613. if (!ssl3_init_finished_mac(s)) {
  614. /* SSLfatal() already called */
  615. return WORK_ERROR;
  616. }
  617. }
  618. break;
  619. case TLS_ST_CW_CHANGE:
  620. if (SSL_IS_DTLS(s)) {
  621. if (s->hit) {
  622. /*
  623. * We're into the last flight so we don't retransmit these
  624. * messages unless we need to.
  625. */
  626. st->use_timer = 0;
  627. }
  628. #ifndef OPENSSL_NO_SCTP
  629. if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
  630. /* Calls SSLfatal() as required */
  631. return dtls_wait_for_dry(s);
  632. }
  633. #endif
  634. }
  635. break;
  636. case TLS_ST_PENDING_EARLY_DATA_END:
  637. /*
  638. * If we've been called by SSL_do_handshake()/SSL_write(), or we did not
  639. * attempt to write early data before calling SSL_read() then we press
  640. * on with the handshake. Otherwise we pause here.
  641. */
  642. if (s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING
  643. || s->early_data_state == SSL_EARLY_DATA_NONE)
  644. return WORK_FINISHED_CONTINUE;
  645. /* Fall through */
  646. case TLS_ST_EARLY_DATA:
  647. return tls_finish_handshake(s, wst, 0, 1);
  648. case TLS_ST_OK:
  649. /* Calls SSLfatal() as required */
  650. return tls_finish_handshake(s, wst, 1, 1);
  651. }
  652. return WORK_FINISHED_CONTINUE;
  653. }
  654. /*
  655. * Perform any work that needs to be done after sending a message from the
  656. * client to the server.
  657. */
  658. WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst)
  659. {
  660. OSSL_STATEM *st = &s->statem;
  661. s->init_num = 0;
  662. switch (st->hand_state) {
  663. default:
  664. /* No post work to be done */
  665. break;
  666. case TLS_ST_CW_CLNT_HELLO:
  667. if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
  668. && s->max_early_data > 0) {
  669. /*
  670. * We haven't selected TLSv1.3 yet so we don't call the change
  671. * cipher state function associated with the SSL_METHOD. Instead
  672. * we call tls13_change_cipher_state() directly.
  673. */
  674. if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0) {
  675. if (!tls13_change_cipher_state(s,
  676. SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
  677. /* SSLfatal() already called */
  678. return WORK_ERROR;
  679. }
  680. }
  681. /* else we're in compat mode so we delay flushing until after CCS */
  682. } else if (!statem_flush(s)) {
  683. return WORK_MORE_A;
  684. }
  685. if (SSL_IS_DTLS(s)) {
  686. /* Treat the next message as the first packet */
  687. s->first_packet = 1;
  688. }
  689. break;
  690. case TLS_ST_CW_END_OF_EARLY_DATA:
  691. /*
  692. * We set the enc_write_ctx back to NULL because we may end up writing
  693. * in cleartext again if we get a HelloRetryRequest from the server.
  694. */
  695. EVP_CIPHER_CTX_free(s->enc_write_ctx);
  696. s->enc_write_ctx = NULL;
  697. break;
  698. case TLS_ST_CW_KEY_EXCH:
  699. if (tls_client_key_exchange_post_work(s) == 0) {
  700. /* SSLfatal() already called */
  701. return WORK_ERROR;
  702. }
  703. break;
  704. case TLS_ST_CW_CHANGE:
  705. if (SSL_IS_TLS13(s) || s->hello_retry_request == SSL_HRR_PENDING)
  706. break;
  707. if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
  708. && s->max_early_data > 0) {
  709. /*
  710. * We haven't selected TLSv1.3 yet so we don't call the change
  711. * cipher state function associated with the SSL_METHOD. Instead
  712. * we call tls13_change_cipher_state() directly.
  713. */
  714. if (!tls13_change_cipher_state(s,
  715. SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE))
  716. return WORK_ERROR;
  717. break;
  718. }
  719. s->session->cipher = s->s3->tmp.new_cipher;
  720. #ifdef OPENSSL_NO_COMP
  721. s->session->compress_meth = 0;
  722. #else
  723. if (s->s3->tmp.new_compression == NULL)
  724. s->session->compress_meth = 0;
  725. else
  726. s->session->compress_meth = s->s3->tmp.new_compression->id;
  727. #endif
  728. if (!s->method->ssl3_enc->setup_key_block(s)) {
  729. /* SSLfatal() already called */
  730. return WORK_ERROR;
  731. }
  732. if (!s->method->ssl3_enc->change_cipher_state(s,
  733. SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
  734. /* SSLfatal() already called */
  735. return WORK_ERROR;
  736. }
  737. if (SSL_IS_DTLS(s)) {
  738. #ifndef OPENSSL_NO_SCTP
  739. if (s->hit) {
  740. /*
  741. * Change to new shared key of SCTP-Auth, will be ignored if
  742. * no SCTP used.
  743. */
  744. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  745. 0, NULL);
  746. }
  747. #endif
  748. dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
  749. }
  750. break;
  751. case TLS_ST_CW_FINISHED:
  752. #ifndef OPENSSL_NO_SCTP
  753. if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) {
  754. /*
  755. * Change to new shared key of SCTP-Auth, will be ignored if
  756. * no SCTP used.
  757. */
  758. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  759. 0, NULL);
  760. }
  761. #endif
  762. if (statem_flush(s) != 1)
  763. return WORK_MORE_B;
  764. if (SSL_IS_TLS13(s)) {
  765. if (!tls13_save_handshake_digest_for_pha(s)) {
  766. /* SSLfatal() already called */
  767. return WORK_ERROR;
  768. }
  769. if (s->post_handshake_auth != SSL_PHA_REQUESTED) {
  770. if (!s->method->ssl3_enc->change_cipher_state(s,
  771. SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
  772. /* SSLfatal() already called */
  773. return WORK_ERROR;
  774. }
  775. }
  776. }
  777. break;
  778. case TLS_ST_CW_KEY_UPDATE:
  779. if (statem_flush(s) != 1)
  780. return WORK_MORE_A;
  781. if (!tls13_update_key(s, 1)) {
  782. /* SSLfatal() already called */
  783. return WORK_ERROR;
  784. }
  785. break;
  786. }
  787. return WORK_FINISHED_CONTINUE;
  788. }
  789. /*
  790. * Get the message construction function and message type for sending from the
  791. * client
  792. *
  793. * Valid return values are:
  794. * 1: Success
  795. * 0: Error
  796. */
  797. int ossl_statem_client_construct_message(SSL *s, WPACKET *pkt,
  798. confunc_f *confunc, int *mt)
  799. {
  800. OSSL_STATEM *st = &s->statem;
  801. switch (st->hand_state) {
  802. default:
  803. /* Shouldn't happen */
  804. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  805. SSL_F_OSSL_STATEM_CLIENT_CONSTRUCT_MESSAGE,
  806. SSL_R_BAD_HANDSHAKE_STATE);
  807. return 0;
  808. case TLS_ST_CW_CHANGE:
  809. if (SSL_IS_DTLS(s))
  810. *confunc = dtls_construct_change_cipher_spec;
  811. else
  812. *confunc = tls_construct_change_cipher_spec;
  813. *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
  814. break;
  815. case TLS_ST_CW_CLNT_HELLO:
  816. *confunc = tls_construct_client_hello;
  817. *mt = SSL3_MT_CLIENT_HELLO;
  818. break;
  819. case TLS_ST_CW_END_OF_EARLY_DATA:
  820. *confunc = tls_construct_end_of_early_data;
  821. *mt = SSL3_MT_END_OF_EARLY_DATA;
  822. break;
  823. case TLS_ST_PENDING_EARLY_DATA_END:
  824. *confunc = NULL;
  825. *mt = SSL3_MT_DUMMY;
  826. break;
  827. case TLS_ST_CW_CERT:
  828. *confunc = tls_construct_client_certificate;
  829. *mt = SSL3_MT_CERTIFICATE;
  830. break;
  831. case TLS_ST_CW_KEY_EXCH:
  832. *confunc = tls_construct_client_key_exchange;
  833. *mt = SSL3_MT_CLIENT_KEY_EXCHANGE;
  834. break;
  835. case TLS_ST_CW_CERT_VRFY:
  836. *confunc = tls_construct_cert_verify;
  837. *mt = SSL3_MT_CERTIFICATE_VERIFY;
  838. break;
  839. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  840. case TLS_ST_CW_NEXT_PROTO:
  841. *confunc = tls_construct_next_proto;
  842. *mt = SSL3_MT_NEXT_PROTO;
  843. break;
  844. #endif
  845. case TLS_ST_CW_FINISHED:
  846. *confunc = tls_construct_finished;
  847. *mt = SSL3_MT_FINISHED;
  848. break;
  849. case TLS_ST_CW_KEY_UPDATE:
  850. *confunc = tls_construct_key_update;
  851. *mt = SSL3_MT_KEY_UPDATE;
  852. break;
  853. }
  854. return 1;
  855. }
  856. /*
  857. * Returns the maximum allowed length for the current message that we are
  858. * reading. Excludes the message header.
  859. */
  860. size_t ossl_statem_client_max_message_size(SSL *s)
  861. {
  862. OSSL_STATEM *st = &s->statem;
  863. switch (st->hand_state) {
  864. default:
  865. /* Shouldn't happen */
  866. return 0;
  867. case TLS_ST_CR_SRVR_HELLO:
  868. return SERVER_HELLO_MAX_LENGTH;
  869. case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
  870. return HELLO_VERIFY_REQUEST_MAX_LENGTH;
  871. case TLS_ST_CR_CERT:
  872. return s->max_cert_list;
  873. case TLS_ST_CR_CERT_VRFY:
  874. return SSL3_RT_MAX_PLAIN_LENGTH;
  875. case TLS_ST_CR_CERT_STATUS:
  876. return SSL3_RT_MAX_PLAIN_LENGTH;
  877. case TLS_ST_CR_KEY_EXCH:
  878. return SERVER_KEY_EXCH_MAX_LENGTH;
  879. case TLS_ST_CR_CERT_REQ:
  880. /*
  881. * Set to s->max_cert_list for compatibility with previous releases. In
  882. * practice these messages can get quite long if servers are configured
  883. * to provide a long list of acceptable CAs
  884. */
  885. return s->max_cert_list;
  886. case TLS_ST_CR_SRVR_DONE:
  887. return SERVER_HELLO_DONE_MAX_LENGTH;
  888. case TLS_ST_CR_CHANGE:
  889. if (s->version == DTLS1_BAD_VER)
  890. return 3;
  891. return CCS_MAX_LENGTH;
  892. case TLS_ST_CR_SESSION_TICKET:
  893. return (SSL_IS_TLS13(s)) ? SESSION_TICKET_MAX_LENGTH_TLS13
  894. : SESSION_TICKET_MAX_LENGTH_TLS12;
  895. case TLS_ST_CR_FINISHED:
  896. return FINISHED_MAX_LENGTH;
  897. case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
  898. return ENCRYPTED_EXTENSIONS_MAX_LENGTH;
  899. case TLS_ST_CR_KEY_UPDATE:
  900. return KEY_UPDATE_MAX_LENGTH;
  901. }
  902. }
  903. /*
  904. * Process a message that the client has been received from the server.
  905. */
  906. MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt)
  907. {
  908. OSSL_STATEM *st = &s->statem;
  909. switch (st->hand_state) {
  910. default:
  911. /* Shouldn't happen */
  912. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  913. SSL_F_OSSL_STATEM_CLIENT_PROCESS_MESSAGE,
  914. ERR_R_INTERNAL_ERROR);
  915. return MSG_PROCESS_ERROR;
  916. case TLS_ST_CR_SRVR_HELLO:
  917. return tls_process_server_hello(s, pkt);
  918. case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
  919. return dtls_process_hello_verify(s, pkt);
  920. case TLS_ST_CR_CERT:
  921. return tls_process_server_certificate(s, pkt);
  922. case TLS_ST_CR_CERT_VRFY:
  923. return tls_process_cert_verify(s, pkt);
  924. case TLS_ST_CR_CERT_STATUS:
  925. return tls_process_cert_status(s, pkt);
  926. case TLS_ST_CR_KEY_EXCH:
  927. return tls_process_key_exchange(s, pkt);
  928. case TLS_ST_CR_CERT_REQ:
  929. return tls_process_certificate_request(s, pkt);
  930. case TLS_ST_CR_SRVR_DONE:
  931. return tls_process_server_done(s, pkt);
  932. case TLS_ST_CR_CHANGE:
  933. return tls_process_change_cipher_spec(s, pkt);
  934. case TLS_ST_CR_SESSION_TICKET:
  935. return tls_process_new_session_ticket(s, pkt);
  936. case TLS_ST_CR_FINISHED:
  937. return tls_process_finished(s, pkt);
  938. case TLS_ST_CR_HELLO_REQ:
  939. return tls_process_hello_req(s, pkt);
  940. case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
  941. return tls_process_encrypted_extensions(s, pkt);
  942. case TLS_ST_CR_KEY_UPDATE:
  943. return tls_process_key_update(s, pkt);
  944. }
  945. }
  946. /*
  947. * Perform any further processing required following the receipt of a message
  948. * from the server
  949. */
  950. WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst)
  951. {
  952. OSSL_STATEM *st = &s->statem;
  953. switch (st->hand_state) {
  954. default:
  955. /* Shouldn't happen */
  956. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  957. SSL_F_OSSL_STATEM_CLIENT_POST_PROCESS_MESSAGE,
  958. ERR_R_INTERNAL_ERROR);
  959. return WORK_ERROR;
  960. case TLS_ST_CR_CERT_VRFY:
  961. case TLS_ST_CR_CERT_REQ:
  962. return tls_prepare_client_certificate(s, wst);
  963. }
  964. }
  965. int tls_construct_client_hello(SSL *s, WPACKET *pkt)
  966. {
  967. unsigned char *p;
  968. size_t sess_id_len;
  969. int i, protverr;
  970. #ifndef OPENSSL_NO_COMP
  971. SSL_COMP *comp;
  972. #endif
  973. SSL_SESSION *sess = s->session;
  974. unsigned char *session_id;
  975. /* Work out what SSL/TLS/DTLS version to use */
  976. protverr = ssl_set_client_hello_version(s);
  977. if (protverr != 0) {
  978. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CLIENT_HELLO,
  979. protverr);
  980. return 0;
  981. }
  982. if (sess == NULL
  983. || !ssl_version_supported(s, sess->ssl_version, NULL)
  984. || !SSL_SESSION_is_resumable(sess)) {
  985. if (s->hello_retry_request == SSL_HRR_NONE
  986. && !ssl_get_new_session(s, 0)) {
  987. /* SSLfatal() already called */
  988. return 0;
  989. }
  990. }
  991. /* else use the pre-loaded session */
  992. p = s->s3->client_random;
  993. /*
  994. * for DTLS if client_random is initialized, reuse it, we are
  995. * required to use same upon reply to HelloVerify
  996. */
  997. if (SSL_IS_DTLS(s)) {
  998. size_t idx;
  999. i = 1;
  1000. for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
  1001. if (p[idx]) {
  1002. i = 0;
  1003. break;
  1004. }
  1005. }
  1006. } else {
  1007. i = (s->hello_retry_request == SSL_HRR_NONE);
  1008. }
  1009. if (i && ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random),
  1010. DOWNGRADE_NONE) <= 0) {
  1011. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CLIENT_HELLO,
  1012. ERR_R_INTERNAL_ERROR);
  1013. return 0;
  1014. }
  1015. /*-
  1016. * version indicates the negotiated version: for example from
  1017. * an SSLv2/v3 compatible client hello). The client_version
  1018. * field is the maximum version we permit and it is also
  1019. * used in RSA encrypted premaster secrets. Some servers can
  1020. * choke if we initially report a higher version then
  1021. * renegotiate to a lower one in the premaster secret. This
  1022. * didn't happen with TLS 1.0 as most servers supported it
  1023. * but it can with TLS 1.1 or later if the server only supports
  1024. * 1.0.
  1025. *
  1026. * Possible scenario with previous logic:
  1027. * 1. Client hello indicates TLS 1.2
  1028. * 2. Server hello says TLS 1.0
  1029. * 3. RSA encrypted premaster secret uses 1.2.
  1030. * 4. Handshake proceeds using TLS 1.0.
  1031. * 5. Server sends hello request to renegotiate.
  1032. * 6. Client hello indicates TLS v1.0 as we now
  1033. * know that is maximum server supports.
  1034. * 7. Server chokes on RSA encrypted premaster secret
  1035. * containing version 1.0.
  1036. *
  1037. * For interoperability it should be OK to always use the
  1038. * maximum version we support in client hello and then rely
  1039. * on the checking of version to ensure the servers isn't
  1040. * being inconsistent: for example initially negotiating with
  1041. * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
  1042. * client_version in client hello and not resetting it to
  1043. * the negotiated version.
  1044. *
  1045. * For TLS 1.3 we always set the ClientHello version to 1.2 and rely on the
  1046. * supported_versions extension for the real supported versions.
  1047. */
  1048. if (!WPACKET_put_bytes_u16(pkt, s->client_version)
  1049. || !WPACKET_memcpy(pkt, s->s3->client_random, SSL3_RANDOM_SIZE)) {
  1050. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CLIENT_HELLO,
  1051. ERR_R_INTERNAL_ERROR);
  1052. return 0;
  1053. }
  1054. /* Session ID */
  1055. session_id = s->session->session_id;
  1056. if (s->new_session || s->session->ssl_version == TLS1_3_VERSION) {
  1057. if (s->version == TLS1_3_VERSION
  1058. && (s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0) {
  1059. sess_id_len = sizeof(s->tmp_session_id);
  1060. s->tmp_session_id_len = sess_id_len;
  1061. session_id = s->tmp_session_id;
  1062. if (s->hello_retry_request == SSL_HRR_NONE
  1063. && RAND_bytes(s->tmp_session_id, sess_id_len) <= 0) {
  1064. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1065. SSL_F_TLS_CONSTRUCT_CLIENT_HELLO,
  1066. ERR_R_INTERNAL_ERROR);
  1067. return 0;
  1068. }
  1069. } else {
  1070. sess_id_len = 0;
  1071. }
  1072. } else {
  1073. assert(s->session->session_id_length <= sizeof(s->session->session_id));
  1074. sess_id_len = s->session->session_id_length;
  1075. if (s->version == TLS1_3_VERSION) {
  1076. s->tmp_session_id_len = sess_id_len;
  1077. memcpy(s->tmp_session_id, s->session->session_id, sess_id_len);
  1078. }
  1079. }
  1080. if (!WPACKET_start_sub_packet_u8(pkt)
  1081. || (sess_id_len != 0 && !WPACKET_memcpy(pkt, session_id,
  1082. sess_id_len))
  1083. || !WPACKET_close(pkt)) {
  1084. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CLIENT_HELLO,
  1085. ERR_R_INTERNAL_ERROR);
  1086. return 0;
  1087. }
  1088. /* cookie stuff for DTLS */
  1089. if (SSL_IS_DTLS(s)) {
  1090. if (s->d1->cookie_len > sizeof(s->d1->cookie)
  1091. || !WPACKET_sub_memcpy_u8(pkt, s->d1->cookie,
  1092. s->d1->cookie_len)) {
  1093. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CLIENT_HELLO,
  1094. ERR_R_INTERNAL_ERROR);
  1095. return 0;
  1096. }
  1097. }
  1098. /* Ciphers supported */
  1099. if (!WPACKET_start_sub_packet_u16(pkt)) {
  1100. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CLIENT_HELLO,
  1101. ERR_R_INTERNAL_ERROR);
  1102. return 0;
  1103. }
  1104. if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), pkt)) {
  1105. /* SSLfatal() already called */
  1106. return 0;
  1107. }
  1108. if (!WPACKET_close(pkt)) {
  1109. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CLIENT_HELLO,
  1110. ERR_R_INTERNAL_ERROR);
  1111. return 0;
  1112. }
  1113. /* COMPRESSION */
  1114. if (!WPACKET_start_sub_packet_u8(pkt)) {
  1115. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CLIENT_HELLO,
  1116. ERR_R_INTERNAL_ERROR);
  1117. return 0;
  1118. }
  1119. #ifndef OPENSSL_NO_COMP
  1120. if (ssl_allow_compression(s)
  1121. && s->ctx->comp_methods
  1122. && (SSL_IS_DTLS(s) || s->s3->tmp.max_ver < TLS1_3_VERSION)) {
  1123. int compnum = sk_SSL_COMP_num(s->ctx->comp_methods);
  1124. for (i = 0; i < compnum; i++) {
  1125. comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
  1126. if (!WPACKET_put_bytes_u8(pkt, comp->id)) {
  1127. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1128. SSL_F_TLS_CONSTRUCT_CLIENT_HELLO,
  1129. ERR_R_INTERNAL_ERROR);
  1130. return 0;
  1131. }
  1132. }
  1133. }
  1134. #endif
  1135. /* Add the NULL method */
  1136. if (!WPACKET_put_bytes_u8(pkt, 0) || !WPACKET_close(pkt)) {
  1137. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CLIENT_HELLO,
  1138. ERR_R_INTERNAL_ERROR);
  1139. return 0;
  1140. }
  1141. /* TLS extensions */
  1142. if (!tls_construct_extensions(s, pkt, SSL_EXT_CLIENT_HELLO, NULL, 0)) {
  1143. /* SSLfatal() already called */
  1144. return 0;
  1145. }
  1146. return 1;
  1147. }
  1148. MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt)
  1149. {
  1150. size_t cookie_len;
  1151. PACKET cookiepkt;
  1152. if (!PACKET_forward(pkt, 2)
  1153. || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
  1154. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_DTLS_PROCESS_HELLO_VERIFY,
  1155. SSL_R_LENGTH_MISMATCH);
  1156. return MSG_PROCESS_ERROR;
  1157. }
  1158. cookie_len = PACKET_remaining(&cookiepkt);
  1159. if (cookie_len > sizeof(s->d1->cookie)) {
  1160. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_DTLS_PROCESS_HELLO_VERIFY,
  1161. SSL_R_LENGTH_TOO_LONG);
  1162. return MSG_PROCESS_ERROR;
  1163. }
  1164. if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
  1165. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_DTLS_PROCESS_HELLO_VERIFY,
  1166. SSL_R_LENGTH_MISMATCH);
  1167. return MSG_PROCESS_ERROR;
  1168. }
  1169. s->d1->cookie_len = cookie_len;
  1170. return MSG_PROCESS_FINISHED_READING;
  1171. }
  1172. static int set_client_ciphersuite(SSL *s, const unsigned char *cipherchars)
  1173. {
  1174. STACK_OF(SSL_CIPHER) *sk;
  1175. const SSL_CIPHER *c;
  1176. int i;
  1177. c = ssl_get_cipher_by_char(s, cipherchars, 0);
  1178. if (c == NULL) {
  1179. /* unknown cipher */
  1180. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_SET_CLIENT_CIPHERSUITE,
  1181. SSL_R_UNKNOWN_CIPHER_RETURNED);
  1182. return 0;
  1183. }
  1184. /*
  1185. * If it is a disabled cipher we either didn't send it in client hello,
  1186. * or it's not allowed for the selected protocol. So we return an error.
  1187. */
  1188. if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK, 1)) {
  1189. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_SET_CLIENT_CIPHERSUITE,
  1190. SSL_R_WRONG_CIPHER_RETURNED);
  1191. return 0;
  1192. }
  1193. sk = ssl_get_ciphers_by_id(s);
  1194. i = sk_SSL_CIPHER_find(sk, c);
  1195. if (i < 0) {
  1196. /* we did not say we would use this cipher */
  1197. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_SET_CLIENT_CIPHERSUITE,
  1198. SSL_R_WRONG_CIPHER_RETURNED);
  1199. return 0;
  1200. }
  1201. if (SSL_IS_TLS13(s) && s->s3->tmp.new_cipher != NULL
  1202. && s->s3->tmp.new_cipher->id != c->id) {
  1203. /* ServerHello selected a different ciphersuite to that in the HRR */
  1204. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_SET_CLIENT_CIPHERSUITE,
  1205. SSL_R_WRONG_CIPHER_RETURNED);
  1206. return 0;
  1207. }
  1208. /*
  1209. * Depending on the session caching (internal/external), the cipher
  1210. * and/or cipher_id values may not be set. Make sure that cipher_id is
  1211. * set and use it for comparison.
  1212. */
  1213. if (s->session->cipher != NULL)
  1214. s->session->cipher_id = s->session->cipher->id;
  1215. if (s->hit && (s->session->cipher_id != c->id)) {
  1216. if (SSL_IS_TLS13(s)) {
  1217. /*
  1218. * In TLSv1.3 it is valid for the server to select a different
  1219. * ciphersuite as long as the hash is the same.
  1220. */
  1221. if (ssl_md(c->algorithm2)
  1222. != ssl_md(s->session->cipher->algorithm2)) {
  1223. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1224. SSL_F_SET_CLIENT_CIPHERSUITE,
  1225. SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED);
  1226. return 0;
  1227. }
  1228. } else {
  1229. /*
  1230. * Prior to TLSv1.3 resuming a session always meant using the same
  1231. * ciphersuite.
  1232. */
  1233. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_SET_CLIENT_CIPHERSUITE,
  1234. SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  1235. return 0;
  1236. }
  1237. }
  1238. s->s3->tmp.new_cipher = c;
  1239. return 1;
  1240. }
  1241. MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)
  1242. {
  1243. PACKET session_id, extpkt;
  1244. size_t session_id_len;
  1245. const unsigned char *cipherchars;
  1246. int hrr = 0;
  1247. unsigned int compression;
  1248. unsigned int sversion;
  1249. unsigned int context;
  1250. RAW_EXTENSION *extensions = NULL;
  1251. #ifndef OPENSSL_NO_COMP
  1252. SSL_COMP *comp;
  1253. #endif
  1254. if (!PACKET_get_net_2(pkt, &sversion)) {
  1255. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_SERVER_HELLO,
  1256. SSL_R_LENGTH_MISMATCH);
  1257. goto err;
  1258. }
  1259. /* load the server random */
  1260. if (s->version == TLS1_3_VERSION
  1261. && sversion == TLS1_2_VERSION
  1262. && PACKET_remaining(pkt) >= SSL3_RANDOM_SIZE
  1263. && memcmp(hrrrandom, PACKET_data(pkt), SSL3_RANDOM_SIZE) == 0) {
  1264. s->hello_retry_request = SSL_HRR_PENDING;
  1265. hrr = 1;
  1266. if (!PACKET_forward(pkt, SSL3_RANDOM_SIZE)) {
  1267. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_SERVER_HELLO,
  1268. SSL_R_LENGTH_MISMATCH);
  1269. goto err;
  1270. }
  1271. } else {
  1272. if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
  1273. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_SERVER_HELLO,
  1274. SSL_R_LENGTH_MISMATCH);
  1275. goto err;
  1276. }
  1277. }
  1278. /* Get the session-id. */
  1279. if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
  1280. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_SERVER_HELLO,
  1281. SSL_R_LENGTH_MISMATCH);
  1282. goto err;
  1283. }
  1284. session_id_len = PACKET_remaining(&session_id);
  1285. if (session_id_len > sizeof(s->session->session_id)
  1286. || session_id_len > SSL3_SESSION_ID_SIZE) {
  1287. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_SERVER_HELLO,
  1288. SSL_R_SSL3_SESSION_ID_TOO_LONG);
  1289. goto err;
  1290. }
  1291. if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
  1292. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_SERVER_HELLO,
  1293. SSL_R_LENGTH_MISMATCH);
  1294. goto err;
  1295. }
  1296. if (!PACKET_get_1(pkt, &compression)) {
  1297. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_SERVER_HELLO,
  1298. SSL_R_LENGTH_MISMATCH);
  1299. goto err;
  1300. }
  1301. /* TLS extensions */
  1302. if (PACKET_remaining(pkt) == 0 && !hrr) {
  1303. PACKET_null_init(&extpkt);
  1304. } else if (!PACKET_as_length_prefixed_2(pkt, &extpkt)
  1305. || PACKET_remaining(pkt) != 0) {
  1306. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_SERVER_HELLO,
  1307. SSL_R_BAD_LENGTH);
  1308. goto err;
  1309. }
  1310. if (!hrr) {
  1311. if (!tls_collect_extensions(s, &extpkt,
  1312. SSL_EXT_TLS1_2_SERVER_HELLO
  1313. | SSL_EXT_TLS1_3_SERVER_HELLO,
  1314. &extensions, NULL, 1)) {
  1315. /* SSLfatal() already called */
  1316. goto err;
  1317. }
  1318. if (!ssl_choose_client_version(s, sversion, extensions)) {
  1319. /* SSLfatal() already called */
  1320. goto err;
  1321. }
  1322. }
  1323. if (SSL_IS_TLS13(s) || hrr) {
  1324. if (compression != 0) {
  1325. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1326. SSL_F_TLS_PROCESS_SERVER_HELLO,
  1327. SSL_R_INVALID_COMPRESSION_ALGORITHM);
  1328. goto err;
  1329. }
  1330. if (session_id_len != s->tmp_session_id_len
  1331. || memcmp(PACKET_data(&session_id), s->tmp_session_id,
  1332. session_id_len) != 0) {
  1333. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1334. SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INVALID_SESSION_ID);
  1335. goto err;
  1336. }
  1337. }
  1338. if (hrr) {
  1339. if (!set_client_ciphersuite(s, cipherchars)) {
  1340. /* SSLfatal() already called */
  1341. goto err;
  1342. }
  1343. return tls_process_as_hello_retry_request(s, &extpkt);
  1344. }
  1345. /*
  1346. * Now we have chosen the version we need to check again that the extensions
  1347. * are appropriate for this version.
  1348. */
  1349. context = SSL_IS_TLS13(s) ? SSL_EXT_TLS1_3_SERVER_HELLO
  1350. : SSL_EXT_TLS1_2_SERVER_HELLO;
  1351. if (!tls_validate_all_contexts(s, context, extensions)) {
  1352. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_SERVER_HELLO,
  1353. SSL_R_BAD_EXTENSION);
  1354. goto err;
  1355. }
  1356. s->hit = 0;
  1357. if (SSL_IS_TLS13(s)) {
  1358. /*
  1359. * In TLSv1.3 a ServerHello message signals a key change so the end of
  1360. * the message must be on a record boundary.
  1361. */
  1362. if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
  1363. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
  1364. SSL_F_TLS_PROCESS_SERVER_HELLO,
  1365. SSL_R_NOT_ON_RECORD_BOUNDARY);
  1366. goto err;
  1367. }
  1368. /* This will set s->hit if we are resuming */
  1369. if (!tls_parse_extension(s, TLSEXT_IDX_psk,
  1370. SSL_EXT_TLS1_3_SERVER_HELLO,
  1371. extensions, NULL, 0)) {
  1372. /* SSLfatal() already called */
  1373. goto err;
  1374. }
  1375. } else {
  1376. /*
  1377. * Check if we can resume the session based on external pre-shared
  1378. * secret. EAP-FAST (RFC 4851) supports two types of session resumption.
  1379. * Resumption based on server-side state works with session IDs.
  1380. * Resumption based on pre-shared Protected Access Credentials (PACs)
  1381. * works by overriding the SessionTicket extension at the application
  1382. * layer, and does not send a session ID. (We do not know whether
  1383. * EAP-FAST servers would honour the session ID.) Therefore, the session
  1384. * ID alone is not a reliable indicator of session resumption, so we
  1385. * first check if we can resume, and later peek at the next handshake
  1386. * message to see if the server wants to resume.
  1387. */
  1388. if (s->version >= TLS1_VERSION
  1389. && s->ext.session_secret_cb != NULL && s->session->ext.tick) {
  1390. const SSL_CIPHER *pref_cipher = NULL;
  1391. /*
  1392. * s->session->master_key_length is a size_t, but this is an int for
  1393. * backwards compat reasons
  1394. */
  1395. int master_key_length;
  1396. master_key_length = sizeof(s->session->master_key);
  1397. if (s->ext.session_secret_cb(s, s->session->master_key,
  1398. &master_key_length,
  1399. NULL, &pref_cipher,
  1400. s->ext.session_secret_cb_arg)
  1401. && master_key_length > 0) {
  1402. s->session->master_key_length = master_key_length;
  1403. s->session->cipher = pref_cipher ?
  1404. pref_cipher : ssl_get_cipher_by_char(s, cipherchars, 0);
  1405. } else {
  1406. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1407. SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
  1408. goto err;
  1409. }
  1410. }
  1411. if (session_id_len != 0
  1412. && session_id_len == s->session->session_id_length
  1413. && memcmp(PACKET_data(&session_id), s->session->session_id,
  1414. session_id_len) == 0)
  1415. s->hit = 1;
  1416. }
  1417. if (s->hit) {
  1418. if (s->sid_ctx_length != s->session->sid_ctx_length
  1419. || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
  1420. /* actually a client application bug */
  1421. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1422. SSL_F_TLS_PROCESS_SERVER_HELLO,
  1423. SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  1424. goto err;
  1425. }
  1426. } else {
  1427. /*
  1428. * If we were trying for session-id reuse but the server
  1429. * didn't resume, make a new SSL_SESSION.
  1430. * In the case of EAP-FAST and PAC, we do not send a session ID,
  1431. * so the PAC-based session secret is always preserved. It'll be
  1432. * overwritten if the server refuses resumption.
  1433. */
  1434. if (s->session->session_id_length > 0) {
  1435. tsan_counter(&s->session_ctx->stats.sess_miss);
  1436. if (!ssl_get_new_session(s, 0)) {
  1437. /* SSLfatal() already called */
  1438. goto err;
  1439. }
  1440. }
  1441. s->session->ssl_version = s->version;
  1442. /*
  1443. * In TLSv1.2 and below we save the session id we were sent so we can
  1444. * resume it later. In TLSv1.3 the session id we were sent is just an
  1445. * echo of what we originally sent in the ClientHello and should not be
  1446. * used for resumption.
  1447. */
  1448. if (!SSL_IS_TLS13(s)) {
  1449. s->session->session_id_length = session_id_len;
  1450. /* session_id_len could be 0 */
  1451. if (session_id_len > 0)
  1452. memcpy(s->session->session_id, PACKET_data(&session_id),
  1453. session_id_len);
  1454. }
  1455. }
  1456. /* Session version and negotiated protocol version should match */
  1457. if (s->version != s->session->ssl_version) {
  1458. SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_F_TLS_PROCESS_SERVER_HELLO,
  1459. SSL_R_SSL_SESSION_VERSION_MISMATCH);
  1460. goto err;
  1461. }
  1462. /*
  1463. * Now that we know the version, update the check to see if it's an allowed
  1464. * version.
  1465. */
  1466. s->s3->tmp.min_ver = s->version;
  1467. s->s3->tmp.max_ver = s->version;
  1468. if (!set_client_ciphersuite(s, cipherchars)) {
  1469. /* SSLfatal() already called */
  1470. goto err;
  1471. }
  1472. #ifdef OPENSSL_NO_COMP
  1473. if (compression != 0) {
  1474. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_SERVER_HELLO,
  1475. SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  1476. goto err;
  1477. }
  1478. /*
  1479. * If compression is disabled we'd better not try to resume a session
  1480. * using compression.
  1481. */
  1482. if (s->session->compress_meth != 0) {
  1483. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_PROCESS_SERVER_HELLO,
  1484. SSL_R_INCONSISTENT_COMPRESSION);
  1485. goto err;
  1486. }
  1487. #else
  1488. if (s->hit && compression != s->session->compress_meth) {
  1489. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_SERVER_HELLO,
  1490. SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
  1491. goto err;
  1492. }
  1493. if (compression == 0)
  1494. comp = NULL;
  1495. else if (!ssl_allow_compression(s)) {
  1496. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_SERVER_HELLO,
  1497. SSL_R_COMPRESSION_DISABLED);
  1498. goto err;
  1499. } else {
  1500. comp = ssl3_comp_find(s->ctx->comp_methods, compression);
  1501. }
  1502. if (compression != 0 && comp == NULL) {
  1503. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_SERVER_HELLO,
  1504. SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  1505. goto err;
  1506. } else {
  1507. s->s3->tmp.new_compression = comp;
  1508. }
  1509. #endif
  1510. if (!tls_parse_all_extensions(s, context, extensions, NULL, 0, 1)) {
  1511. /* SSLfatal() already called */
  1512. goto err;
  1513. }
  1514. #ifndef OPENSSL_NO_SCTP
  1515. if (SSL_IS_DTLS(s) && s->hit) {
  1516. unsigned char sctpauthkey[64];
  1517. char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
  1518. size_t labellen;
  1519. /*
  1520. * Add new shared key for SCTP-Auth, will be ignored if
  1521. * no SCTP used.
  1522. */
  1523. memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
  1524. sizeof(DTLS1_SCTP_AUTH_LABEL));
  1525. /* Don't include the terminating zero. */
  1526. labellen = sizeof(labelbuffer) - 1;
  1527. if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
  1528. labellen += 1;
  1529. if (SSL_export_keying_material(s, sctpauthkey,
  1530. sizeof(sctpauthkey),
  1531. labelbuffer,
  1532. labellen, NULL, 0, 0) <= 0) {
  1533. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SERVER_HELLO,
  1534. ERR_R_INTERNAL_ERROR);
  1535. goto err;
  1536. }
  1537. BIO_ctrl(SSL_get_wbio(s),
  1538. BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  1539. sizeof(sctpauthkey), sctpauthkey);
  1540. }
  1541. #endif
  1542. /*
  1543. * In TLSv1.3 we have some post-processing to change cipher state, otherwise
  1544. * we're done with this message
  1545. */
  1546. if (SSL_IS_TLS13(s)
  1547. && (!s->method->ssl3_enc->setup_key_block(s)
  1548. || !s->method->ssl3_enc->change_cipher_state(s,
  1549. SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_READ))) {
  1550. /* SSLfatal() already called */
  1551. goto err;
  1552. }
  1553. OPENSSL_free(extensions);
  1554. return MSG_PROCESS_CONTINUE_READING;
  1555. err:
  1556. OPENSSL_free(extensions);
  1557. return MSG_PROCESS_ERROR;
  1558. }
  1559. static MSG_PROCESS_RETURN tls_process_as_hello_retry_request(SSL *s,
  1560. PACKET *extpkt)
  1561. {
  1562. RAW_EXTENSION *extensions = NULL;
  1563. /*
  1564. * If we were sending early_data then the enc_write_ctx is now invalid and
  1565. * should not be used.
  1566. */
  1567. EVP_CIPHER_CTX_free(s->enc_write_ctx);
  1568. s->enc_write_ctx = NULL;
  1569. if (!tls_collect_extensions(s, extpkt, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST,
  1570. &extensions, NULL, 1)
  1571. || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST,
  1572. extensions, NULL, 0, 1)) {
  1573. /* SSLfatal() already called */
  1574. goto err;
  1575. }
  1576. OPENSSL_free(extensions);
  1577. extensions = NULL;
  1578. if (s->ext.tls13_cookie_len == 0
  1579. #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
  1580. && s->s3->tmp.pkey != NULL
  1581. #endif
  1582. ) {
  1583. /*
  1584. * We didn't receive a cookie or a new key_share so the next
  1585. * ClientHello will not change
  1586. */
  1587. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1588. SSL_F_TLS_PROCESS_AS_HELLO_RETRY_REQUEST,
  1589. SSL_R_NO_CHANGE_FOLLOWING_HRR);
  1590. goto err;
  1591. }
  1592. /*
  1593. * Re-initialise the Transcript Hash. We're going to prepopulate it with
  1594. * a synthetic message_hash in place of ClientHello1.
  1595. */
  1596. if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) {
  1597. /* SSLfatal() already called */
  1598. goto err;
  1599. }
  1600. /*
  1601. * Add this message to the Transcript Hash. Normally this is done
  1602. * automatically prior to the message processing stage. However due to the
  1603. * need to create the synthetic message hash, we defer that step until now
  1604. * for HRR messages.
  1605. */
  1606. if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
  1607. s->init_num + SSL3_HM_HEADER_LENGTH)) {
  1608. /* SSLfatal() already called */
  1609. goto err;
  1610. }
  1611. return MSG_PROCESS_FINISHED_READING;
  1612. err:
  1613. OPENSSL_free(extensions);
  1614. return MSG_PROCESS_ERROR;
  1615. }
  1616. MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt)
  1617. {
  1618. int i;
  1619. MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
  1620. unsigned long cert_list_len, cert_len;
  1621. X509 *x = NULL;
  1622. const unsigned char *certstart, *certbytes;
  1623. STACK_OF(X509) *sk = NULL;
  1624. EVP_PKEY *pkey = NULL;
  1625. size_t chainidx, certidx;
  1626. unsigned int context = 0;
  1627. const SSL_CERT_LOOKUP *clu;
  1628. if ((sk = sk_X509_new_null()) == NULL) {
  1629. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1630. ERR_R_MALLOC_FAILURE);
  1631. goto err;
  1632. }
  1633. if ((SSL_IS_TLS13(s) && !PACKET_get_1(pkt, &context))
  1634. || context != 0
  1635. || !PACKET_get_net_3(pkt, &cert_list_len)
  1636. || PACKET_remaining(pkt) != cert_list_len
  1637. || PACKET_remaining(pkt) == 0) {
  1638. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1639. SSL_R_LENGTH_MISMATCH);
  1640. goto err;
  1641. }
  1642. for (chainidx = 0; PACKET_remaining(pkt); chainidx++) {
  1643. if (!PACKET_get_net_3(pkt, &cert_len)
  1644. || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
  1645. SSLfatal(s, SSL_AD_DECODE_ERROR,
  1646. SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1647. SSL_R_CERT_LENGTH_MISMATCH);
  1648. goto err;
  1649. }
  1650. certstart = certbytes;
  1651. x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
  1652. if (x == NULL) {
  1653. SSLfatal(s, SSL_AD_BAD_CERTIFICATE,
  1654. SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
  1655. goto err;
  1656. }
  1657. if (certbytes != (certstart + cert_len)) {
  1658. SSLfatal(s, SSL_AD_DECODE_ERROR,
  1659. SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1660. SSL_R_CERT_LENGTH_MISMATCH);
  1661. goto err;
  1662. }
  1663. if (SSL_IS_TLS13(s)) {
  1664. RAW_EXTENSION *rawexts = NULL;
  1665. PACKET extensions;
  1666. if (!PACKET_get_length_prefixed_2(pkt, &extensions)) {
  1667. SSLfatal(s, SSL_AD_DECODE_ERROR,
  1668. SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1669. SSL_R_BAD_LENGTH);
  1670. goto err;
  1671. }
  1672. if (!tls_collect_extensions(s, &extensions,
  1673. SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
  1674. NULL, chainidx == 0)
  1675. || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
  1676. rawexts, x, chainidx,
  1677. PACKET_remaining(pkt) == 0)) {
  1678. OPENSSL_free(rawexts);
  1679. /* SSLfatal already called */
  1680. goto err;
  1681. }
  1682. OPENSSL_free(rawexts);
  1683. }
  1684. if (!sk_X509_push(sk, x)) {
  1685. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1686. SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1687. ERR_R_MALLOC_FAILURE);
  1688. goto err;
  1689. }
  1690. x = NULL;
  1691. }
  1692. i = ssl_verify_cert_chain(s, sk);
  1693. /*
  1694. * The documented interface is that SSL_VERIFY_PEER should be set in order
  1695. * for client side verification of the server certificate to take place.
  1696. * However, historically the code has only checked that *any* flag is set
  1697. * to cause server verification to take place. Use of the other flags makes
  1698. * no sense in client mode. An attempt to clean up the semantics was
  1699. * reverted because at least one application *only* set
  1700. * SSL_VERIFY_FAIL_IF_NO_PEER_CERT. Prior to the clean up this still caused
  1701. * server verification to take place, after the clean up it silently did
  1702. * nothing. SSL_CTX_set_verify()/SSL_set_verify() cannot validate the flags
  1703. * sent to them because they are void functions. Therefore, we now use the
  1704. * (less clean) historic behaviour of performing validation if any flag is
  1705. * set. The *documented* interface remains the same.
  1706. */
  1707. if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
  1708. SSLfatal(s, ssl_x509err2alert(s->verify_result),
  1709. SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1710. SSL_R_CERTIFICATE_VERIFY_FAILED);
  1711. goto err;
  1712. }
  1713. ERR_clear_error(); /* but we keep s->verify_result */
  1714. if (i > 1) {
  1715. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1716. SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i);
  1717. goto err;
  1718. }
  1719. s->session->peer_chain = sk;
  1720. /*
  1721. * Inconsistency alert: cert_chain does include the peer's certificate,
  1722. * which we don't include in statem_srvr.c
  1723. */
  1724. x = sk_X509_value(sk, 0);
  1725. sk = NULL;
  1726. pkey = X509_get0_pubkey(x);
  1727. if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
  1728. x = NULL;
  1729. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1730. SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
  1731. goto err;
  1732. }
  1733. if ((clu = ssl_cert_lookup_by_pkey(pkey, &certidx)) == NULL) {
  1734. x = NULL;
  1735. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1736. SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1737. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  1738. goto err;
  1739. }
  1740. /*
  1741. * Check certificate type is consistent with ciphersuite. For TLS 1.3
  1742. * skip check since TLS 1.3 ciphersuites can be used with any certificate
  1743. * type.
  1744. */
  1745. if (!SSL_IS_TLS13(s)) {
  1746. if ((clu->amask & s->s3->tmp.new_cipher->algorithm_auth) == 0) {
  1747. x = NULL;
  1748. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1749. SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1750. SSL_R_WRONG_CERTIFICATE_TYPE);
  1751. goto err;
  1752. }
  1753. }
  1754. X509_free(s->session->peer);
  1755. X509_up_ref(x);
  1756. s->session->peer = x;
  1757. s->session->verify_result = s->verify_result;
  1758. x = NULL;
  1759. /* Save the current hash state for when we receive the CertificateVerify */
  1760. if (SSL_IS_TLS13(s)
  1761. && !ssl_handshake_hash(s, s->cert_verify_hash,
  1762. sizeof(s->cert_verify_hash),
  1763. &s->cert_verify_hash_len)) {
  1764. /* SSLfatal() already called */;
  1765. goto err;
  1766. }
  1767. ret = MSG_PROCESS_CONTINUE_READING;
  1768. err:
  1769. X509_free(x);
  1770. sk_X509_pop_free(sk, X509_free);
  1771. return ret;
  1772. }
  1773. static int tls_process_ske_psk_preamble(SSL *s, PACKET *pkt)
  1774. {
  1775. #ifndef OPENSSL_NO_PSK
  1776. PACKET psk_identity_hint;
  1777. /* PSK ciphersuites are preceded by an identity hint */
  1778. if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
  1779. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE,
  1780. SSL_R_LENGTH_MISMATCH);
  1781. return 0;
  1782. }
  1783. /*
  1784. * Store PSK identity hint for later use, hint is used in
  1785. * tls_construct_client_key_exchange. Assume that the maximum length of
  1786. * a PSK identity hint can be as long as the maximum length of a PSK
  1787. * identity.
  1788. */
  1789. if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1790. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1791. SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE,
  1792. SSL_R_DATA_LENGTH_TOO_LONG);
  1793. return 0;
  1794. }
  1795. if (PACKET_remaining(&psk_identity_hint) == 0) {
  1796. OPENSSL_free(s->session->psk_identity_hint);
  1797. s->session->psk_identity_hint = NULL;
  1798. } else if (!PACKET_strndup(&psk_identity_hint,
  1799. &s->session->psk_identity_hint)) {
  1800. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE,
  1801. ERR_R_INTERNAL_ERROR);
  1802. return 0;
  1803. }
  1804. return 1;
  1805. #else
  1806. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE,
  1807. ERR_R_INTERNAL_ERROR);
  1808. return 0;
  1809. #endif
  1810. }
  1811. static int tls_process_ske_srp(SSL *s, PACKET *pkt, EVP_PKEY **pkey)
  1812. {
  1813. #ifndef OPENSSL_NO_SRP
  1814. PACKET prime, generator, salt, server_pub;
  1815. if (!PACKET_get_length_prefixed_2(pkt, &prime)
  1816. || !PACKET_get_length_prefixed_2(pkt, &generator)
  1817. || !PACKET_get_length_prefixed_1(pkt, &salt)
  1818. || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
  1819. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_SKE_SRP,
  1820. SSL_R_LENGTH_MISMATCH);
  1821. return 0;
  1822. }
  1823. /* TODO(size_t): Convert BN_bin2bn() calls */
  1824. if ((s->srp_ctx.N =
  1825. BN_bin2bn(PACKET_data(&prime),
  1826. (int)PACKET_remaining(&prime), NULL)) == NULL
  1827. || (s->srp_ctx.g =
  1828. BN_bin2bn(PACKET_data(&generator),
  1829. (int)PACKET_remaining(&generator), NULL)) == NULL
  1830. || (s->srp_ctx.s =
  1831. BN_bin2bn(PACKET_data(&salt),
  1832. (int)PACKET_remaining(&salt), NULL)) == NULL
  1833. || (s->srp_ctx.B =
  1834. BN_bin2bn(PACKET_data(&server_pub),
  1835. (int)PACKET_remaining(&server_pub), NULL)) == NULL) {
  1836. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_SRP,
  1837. ERR_R_BN_LIB);
  1838. return 0;
  1839. }
  1840. if (!srp_verify_server_param(s)) {
  1841. /* SSLfatal() already called */
  1842. return 0;
  1843. }
  1844. /* We must check if there is a certificate */
  1845. if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
  1846. *pkey = X509_get0_pubkey(s->session->peer);
  1847. return 1;
  1848. #else
  1849. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_SRP,
  1850. ERR_R_INTERNAL_ERROR);
  1851. return 0;
  1852. #endif
  1853. }
  1854. static int tls_process_ske_dhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey)
  1855. {
  1856. #ifndef OPENSSL_NO_DH
  1857. PACKET prime, generator, pub_key;
  1858. EVP_PKEY *peer_tmp = NULL;
  1859. DH *dh = NULL;
  1860. BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL;
  1861. int check_bits = 0;
  1862. if (!PACKET_get_length_prefixed_2(pkt, &prime)
  1863. || !PACKET_get_length_prefixed_2(pkt, &generator)
  1864. || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
  1865. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_SKE_DHE,
  1866. SSL_R_LENGTH_MISMATCH);
  1867. return 0;
  1868. }
  1869. peer_tmp = EVP_PKEY_new();
  1870. dh = DH_new();
  1871. if (peer_tmp == NULL || dh == NULL) {
  1872. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_DHE,
  1873. ERR_R_MALLOC_FAILURE);
  1874. goto err;
  1875. }
  1876. /* TODO(size_t): Convert these calls */
  1877. p = BN_bin2bn(PACKET_data(&prime), (int)PACKET_remaining(&prime), NULL);
  1878. g = BN_bin2bn(PACKET_data(&generator), (int)PACKET_remaining(&generator),
  1879. NULL);
  1880. bnpub_key = BN_bin2bn(PACKET_data(&pub_key),
  1881. (int)PACKET_remaining(&pub_key), NULL);
  1882. if (p == NULL || g == NULL || bnpub_key == NULL) {
  1883. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_DHE,
  1884. ERR_R_BN_LIB);
  1885. goto err;
  1886. }
  1887. /* test non-zero pubkey */
  1888. if (BN_is_zero(bnpub_key)) {
  1889. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_SKE_DHE,
  1890. SSL_R_BAD_DH_VALUE);
  1891. goto err;
  1892. }
  1893. if (!DH_set0_pqg(dh, p, NULL, g)) {
  1894. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_DHE,
  1895. ERR_R_BN_LIB);
  1896. goto err;
  1897. }
  1898. p = g = NULL;
  1899. if (DH_check_params(dh, &check_bits) == 0 || check_bits != 0) {
  1900. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_SKE_DHE,
  1901. SSL_R_BAD_DH_VALUE);
  1902. goto err;
  1903. }
  1904. if (!DH_set0_key(dh, bnpub_key, NULL)) {
  1905. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_DHE,
  1906. ERR_R_BN_LIB);
  1907. goto err;
  1908. }
  1909. bnpub_key = NULL;
  1910. if (EVP_PKEY_assign_DH(peer_tmp, dh) == 0) {
  1911. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_DHE,
  1912. ERR_R_EVP_LIB);
  1913. goto err;
  1914. }
  1915. dh = NULL;
  1916. if (!ssl_security(s, SSL_SECOP_TMP_DH, EVP_PKEY_security_bits(peer_tmp),
  1917. 0, peer_tmp)) {
  1918. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_PROCESS_SKE_DHE,
  1919. SSL_R_DH_KEY_TOO_SMALL);
  1920. goto err;
  1921. }
  1922. s->s3->peer_tmp = peer_tmp;
  1923. /*
  1924. * FIXME: This makes assumptions about which ciphersuites come with
  1925. * public keys. We should have a less ad-hoc way of doing this
  1926. */
  1927. if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
  1928. *pkey = X509_get0_pubkey(s->session->peer);
  1929. /* else anonymous DH, so no certificate or pkey. */
  1930. return 1;
  1931. err:
  1932. BN_free(p);
  1933. BN_free(g);
  1934. BN_free(bnpub_key);
  1935. DH_free(dh);
  1936. EVP_PKEY_free(peer_tmp);
  1937. return 0;
  1938. #else
  1939. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_DHE,
  1940. ERR_R_INTERNAL_ERROR);
  1941. return 0;
  1942. #endif
  1943. }
  1944. static int tls_process_ske_ecdhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey)
  1945. {
  1946. #ifndef OPENSSL_NO_EC
  1947. PACKET encoded_pt;
  1948. unsigned int curve_type, curve_id;
  1949. /*
  1950. * Extract elliptic curve parameters and the server's ephemeral ECDH
  1951. * public key. We only support named (not generic) curves and
  1952. * ECParameters in this case is just three bytes.
  1953. */
  1954. if (!PACKET_get_1(pkt, &curve_type) || !PACKET_get_net_2(pkt, &curve_id)) {
  1955. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_SKE_ECDHE,
  1956. SSL_R_LENGTH_TOO_SHORT);
  1957. return 0;
  1958. }
  1959. /*
  1960. * Check curve is named curve type and one of our preferences, if not
  1961. * server has sent an invalid curve.
  1962. */
  1963. if (curve_type != NAMED_CURVE_TYPE
  1964. || !tls1_check_group_id(s, curve_id, 1)) {
  1965. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_SKE_ECDHE,
  1966. SSL_R_WRONG_CURVE);
  1967. return 0;
  1968. }
  1969. if ((s->s3->peer_tmp = ssl_generate_param_group(curve_id)) == NULL) {
  1970. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_ECDHE,
  1971. SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
  1972. return 0;
  1973. }
  1974. if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
  1975. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_SKE_ECDHE,
  1976. SSL_R_LENGTH_MISMATCH);
  1977. return 0;
  1978. }
  1979. if (!EVP_PKEY_set1_tls_encodedpoint(s->s3->peer_tmp,
  1980. PACKET_data(&encoded_pt),
  1981. PACKET_remaining(&encoded_pt))) {
  1982. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_SKE_ECDHE,
  1983. SSL_R_BAD_ECPOINT);
  1984. return 0;
  1985. }
  1986. /*
  1987. * The ECC/TLS specification does not mention the use of DSA to sign
  1988. * ECParameters in the server key exchange message. We do support RSA
  1989. * and ECDSA.
  1990. */
  1991. if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aECDSA)
  1992. *pkey = X509_get0_pubkey(s->session->peer);
  1993. else if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aRSA)
  1994. *pkey = X509_get0_pubkey(s->session->peer);
  1995. /* else anonymous ECDH, so no certificate or pkey. */
  1996. return 1;
  1997. #else
  1998. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_ECDHE,
  1999. ERR_R_INTERNAL_ERROR);
  2000. return 0;
  2001. #endif
  2002. }
  2003. MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt)
  2004. {
  2005. long alg_k;
  2006. EVP_PKEY *pkey = NULL;
  2007. EVP_MD_CTX *md_ctx = NULL;
  2008. EVP_PKEY_CTX *pctx = NULL;
  2009. PACKET save_param_start, signature;
  2010. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  2011. save_param_start = *pkt;
  2012. #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
  2013. EVP_PKEY_free(s->s3->peer_tmp);
  2014. s->s3->peer_tmp = NULL;
  2015. #endif
  2016. if (alg_k & SSL_PSK) {
  2017. if (!tls_process_ske_psk_preamble(s, pkt)) {
  2018. /* SSLfatal() already called */
  2019. goto err;
  2020. }
  2021. }
  2022. /* Nothing else to do for plain PSK or RSAPSK */
  2023. if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
  2024. } else if (alg_k & SSL_kSRP) {
  2025. if (!tls_process_ske_srp(s, pkt, &pkey)) {
  2026. /* SSLfatal() already called */
  2027. goto err;
  2028. }
  2029. } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
  2030. if (!tls_process_ske_dhe(s, pkt, &pkey)) {
  2031. /* SSLfatal() already called */
  2032. goto err;
  2033. }
  2034. } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
  2035. if (!tls_process_ske_ecdhe(s, pkt, &pkey)) {
  2036. /* SSLfatal() already called */
  2037. goto err;
  2038. }
  2039. } else if (alg_k) {
  2040. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2041. SSL_R_UNEXPECTED_MESSAGE);
  2042. goto err;
  2043. }
  2044. /* if it was signed, check the signature */
  2045. if (pkey != NULL) {
  2046. PACKET params;
  2047. int maxsig;
  2048. const EVP_MD *md = NULL;
  2049. unsigned char *tbs;
  2050. size_t tbslen;
  2051. int rv;
  2052. /*
  2053. * |pkt| now points to the beginning of the signature, so the difference
  2054. * equals the length of the parameters.
  2055. */
  2056. if (!PACKET_get_sub_packet(&save_param_start, &params,
  2057. PACKET_remaining(&save_param_start) -
  2058. PACKET_remaining(pkt))) {
  2059. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2060. ERR_R_INTERNAL_ERROR);
  2061. goto err;
  2062. }
  2063. if (SSL_USE_SIGALGS(s)) {
  2064. unsigned int sigalg;
  2065. if (!PACKET_get_net_2(pkt, &sigalg)) {
  2066. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2067. SSL_R_LENGTH_TOO_SHORT);
  2068. goto err;
  2069. }
  2070. if (tls12_check_peer_sigalg(s, sigalg, pkey) <=0) {
  2071. /* SSLfatal() already called */
  2072. goto err;
  2073. }
  2074. } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) {
  2075. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2076. ERR_R_INTERNAL_ERROR);
  2077. goto err;
  2078. }
  2079. if (!tls1_lookup_md(s->s3->tmp.peer_sigalg, &md)) {
  2080. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2081. ERR_R_INTERNAL_ERROR);
  2082. goto err;
  2083. }
  2084. #ifdef SSL_DEBUG
  2085. if (SSL_USE_SIGALGS(s))
  2086. fprintf(stderr, "USING TLSv1.2 HASH %s\n",
  2087. md == NULL ? "n/a" : EVP_MD_name(md));
  2088. #endif
  2089. if (!PACKET_get_length_prefixed_2(pkt, &signature)
  2090. || PACKET_remaining(pkt) != 0) {
  2091. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2092. SSL_R_LENGTH_MISMATCH);
  2093. goto err;
  2094. }
  2095. maxsig = EVP_PKEY_size(pkey);
  2096. if (maxsig < 0) {
  2097. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2098. ERR_R_INTERNAL_ERROR);
  2099. goto err;
  2100. }
  2101. /*
  2102. * Check signature length
  2103. */
  2104. if (PACKET_remaining(&signature) > (size_t)maxsig) {
  2105. /* wrong packet length */
  2106. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2107. SSL_R_WRONG_SIGNATURE_LENGTH);
  2108. goto err;
  2109. }
  2110. md_ctx = EVP_MD_CTX_new();
  2111. if (md_ctx == NULL) {
  2112. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2113. ERR_R_MALLOC_FAILURE);
  2114. goto err;
  2115. }
  2116. if (EVP_DigestVerifyInit(md_ctx, &pctx, md, NULL, pkey) <= 0) {
  2117. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2118. ERR_R_EVP_LIB);
  2119. goto err;
  2120. }
  2121. if (SSL_USE_PSS(s)) {
  2122. if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
  2123. || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
  2124. RSA_PSS_SALTLEN_DIGEST) <= 0) {
  2125. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2126. SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
  2127. goto err;
  2128. }
  2129. }
  2130. tbslen = construct_key_exchange_tbs(s, &tbs, PACKET_data(&params),
  2131. PACKET_remaining(&params));
  2132. if (tbslen == 0) {
  2133. /* SSLfatal() already called */
  2134. goto err;
  2135. }
  2136. rv = EVP_DigestVerify(md_ctx, PACKET_data(&signature),
  2137. PACKET_remaining(&signature), tbs, tbslen);
  2138. OPENSSL_free(tbs);
  2139. if (rv <= 0) {
  2140. SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2141. SSL_R_BAD_SIGNATURE);
  2142. goto err;
  2143. }
  2144. EVP_MD_CTX_free(md_ctx);
  2145. md_ctx = NULL;
  2146. } else {
  2147. /* aNULL, aSRP or PSK do not need public keys */
  2148. if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
  2149. && !(alg_k & SSL_PSK)) {
  2150. /* Might be wrong key type, check it */
  2151. if (ssl3_check_cert_and_algorithm(s)) {
  2152. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2153. SSL_R_BAD_DATA);
  2154. }
  2155. /* else this shouldn't happen, SSLfatal() already called */
  2156. goto err;
  2157. }
  2158. /* still data left over */
  2159. if (PACKET_remaining(pkt) != 0) {
  2160. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2161. SSL_R_EXTRA_DATA_IN_MESSAGE);
  2162. goto err;
  2163. }
  2164. }
  2165. return MSG_PROCESS_CONTINUE_READING;
  2166. err:
  2167. EVP_MD_CTX_free(md_ctx);
  2168. return MSG_PROCESS_ERROR;
  2169. }
  2170. MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
  2171. {
  2172. size_t i;
  2173. /* Clear certificate validity flags */
  2174. for (i = 0; i < SSL_PKEY_NUM; i++)
  2175. s->s3->tmp.valid_flags[i] = 0;
  2176. if (SSL_IS_TLS13(s)) {
  2177. PACKET reqctx, extensions;
  2178. RAW_EXTENSION *rawexts = NULL;
  2179. if ((s->shutdown & SSL_SENT_SHUTDOWN) != 0) {
  2180. /*
  2181. * We already sent close_notify. This can only happen in TLSv1.3
  2182. * post-handshake messages. We can't reasonably respond to this, so
  2183. * we just ignore it
  2184. */
  2185. return MSG_PROCESS_FINISHED_READING;
  2186. }
  2187. /* Free and zero certificate types: it is not present in TLS 1.3 */
  2188. OPENSSL_free(s->s3->tmp.ctype);
  2189. s->s3->tmp.ctype = NULL;
  2190. s->s3->tmp.ctype_len = 0;
  2191. OPENSSL_free(s->pha_context);
  2192. s->pha_context = NULL;
  2193. s->pha_context_len = 0;
  2194. if (!PACKET_get_length_prefixed_1(pkt, &reqctx) ||
  2195. !PACKET_memdup(&reqctx, &s->pha_context, &s->pha_context_len)) {
  2196. SSLfatal(s, SSL_AD_DECODE_ERROR,
  2197. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2198. SSL_R_LENGTH_MISMATCH);
  2199. return MSG_PROCESS_ERROR;
  2200. }
  2201. if (!PACKET_get_length_prefixed_2(pkt, &extensions)) {
  2202. SSLfatal(s, SSL_AD_DECODE_ERROR,
  2203. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2204. SSL_R_BAD_LENGTH);
  2205. return MSG_PROCESS_ERROR;
  2206. }
  2207. if (!tls_collect_extensions(s, &extensions,
  2208. SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
  2209. &rawexts, NULL, 1)
  2210. || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
  2211. rawexts, NULL, 0, 1)) {
  2212. /* SSLfatal() already called */
  2213. OPENSSL_free(rawexts);
  2214. return MSG_PROCESS_ERROR;
  2215. }
  2216. OPENSSL_free(rawexts);
  2217. if (!tls1_process_sigalgs(s)) {
  2218. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2219. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2220. SSL_R_BAD_LENGTH);
  2221. return MSG_PROCESS_ERROR;
  2222. }
  2223. } else {
  2224. PACKET ctypes;
  2225. /* get the certificate types */
  2226. if (!PACKET_get_length_prefixed_1(pkt, &ctypes)) {
  2227. SSLfatal(s, SSL_AD_DECODE_ERROR,
  2228. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2229. SSL_R_LENGTH_MISMATCH);
  2230. return MSG_PROCESS_ERROR;
  2231. }
  2232. if (!PACKET_memdup(&ctypes, &s->s3->tmp.ctype, &s->s3->tmp.ctype_len)) {
  2233. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2234. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2235. ERR_R_INTERNAL_ERROR);
  2236. return MSG_PROCESS_ERROR;
  2237. }
  2238. if (SSL_USE_SIGALGS(s)) {
  2239. PACKET sigalgs;
  2240. if (!PACKET_get_length_prefixed_2(pkt, &sigalgs)) {
  2241. SSLfatal(s, SSL_AD_DECODE_ERROR,
  2242. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2243. SSL_R_LENGTH_MISMATCH);
  2244. return MSG_PROCESS_ERROR;
  2245. }
  2246. /*
  2247. * Despite this being for certificates, preserve compatibility
  2248. * with pre-TLS 1.3 and use the regular sigalgs field.
  2249. */
  2250. if (!tls1_save_sigalgs(s, &sigalgs, 0)) {
  2251. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2252. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2253. SSL_R_SIGNATURE_ALGORITHMS_ERROR);
  2254. return MSG_PROCESS_ERROR;
  2255. }
  2256. if (!tls1_process_sigalgs(s)) {
  2257. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2258. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2259. ERR_R_MALLOC_FAILURE);
  2260. return MSG_PROCESS_ERROR;
  2261. }
  2262. }
  2263. /* get the CA RDNs */
  2264. if (!parse_ca_names(s, pkt)) {
  2265. /* SSLfatal() already called */
  2266. return MSG_PROCESS_ERROR;
  2267. }
  2268. }
  2269. if (PACKET_remaining(pkt) != 0) {
  2270. SSLfatal(s, SSL_AD_DECODE_ERROR,
  2271. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2272. SSL_R_LENGTH_MISMATCH);
  2273. return MSG_PROCESS_ERROR;
  2274. }
  2275. /* we should setup a certificate to return.... */
  2276. s->s3->tmp.cert_req = 1;
  2277. /*
  2278. * In TLSv1.3 we don't prepare the client certificate yet. We wait until
  2279. * after the CertificateVerify message has been received. This is because
  2280. * in TLSv1.3 the CertificateRequest arrives before the Certificate message
  2281. * but in TLSv1.2 it is the other way around. We want to make sure that
  2282. * SSL_get_peer_certificate() returns something sensible in
  2283. * client_cert_cb.
  2284. */
  2285. if (SSL_IS_TLS13(s) && s->post_handshake_auth != SSL_PHA_REQUESTED)
  2286. return MSG_PROCESS_CONTINUE_READING;
  2287. return MSG_PROCESS_CONTINUE_PROCESSING;
  2288. }
  2289. MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
  2290. {
  2291. unsigned int ticklen;
  2292. unsigned long ticket_lifetime_hint, age_add = 0;
  2293. unsigned int sess_len;
  2294. RAW_EXTENSION *exts = NULL;
  2295. PACKET nonce;
  2296. PACKET_null_init(&nonce);
  2297. if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
  2298. || (SSL_IS_TLS13(s)
  2299. && (!PACKET_get_net_4(pkt, &age_add)
  2300. || !PACKET_get_length_prefixed_1(pkt, &nonce)))
  2301. || !PACKET_get_net_2(pkt, &ticklen)
  2302. || (SSL_IS_TLS13(s) ? (ticklen == 0 || PACKET_remaining(pkt) < ticklen)
  2303. : PACKET_remaining(pkt) != ticklen)) {
  2304. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_NEW_SESSION_TICKET,
  2305. SSL_R_LENGTH_MISMATCH);
  2306. goto err;
  2307. }
  2308. /*
  2309. * Server is allowed to change its mind (in <=TLSv1.2) and send an empty
  2310. * ticket. We already checked this TLSv1.3 case above, so it should never
  2311. * be 0 here in that instance
  2312. */
  2313. if (ticklen == 0)
  2314. return MSG_PROCESS_CONTINUE_READING;
  2315. /*
  2316. * Sessions must be immutable once they go into the session cache. Otherwise
  2317. * we can get multi-thread problems. Therefore we don't "update" sessions,
  2318. * we replace them with a duplicate. In TLSv1.3 we need to do this every
  2319. * time a NewSessionTicket arrives because those messages arrive
  2320. * post-handshake and the session may have already gone into the session
  2321. * cache.
  2322. */
  2323. if (SSL_IS_TLS13(s) || s->session->session_id_length > 0) {
  2324. SSL_SESSION *new_sess;
  2325. /*
  2326. * We reused an existing session, so we need to replace it with a new
  2327. * one
  2328. */
  2329. if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
  2330. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2331. SSL_F_TLS_PROCESS_NEW_SESSION_TICKET,
  2332. ERR_R_MALLOC_FAILURE);
  2333. goto err;
  2334. }
  2335. if ((s->session_ctx->session_cache_mode & SSL_SESS_CACHE_CLIENT) != 0
  2336. && !SSL_IS_TLS13(s)) {
  2337. /*
  2338. * In TLSv1.2 and below the arrival of a new tickets signals that
  2339. * any old ticket we were using is now out of date, so we remove the
  2340. * old session from the cache. We carry on if this fails
  2341. */
  2342. SSL_CTX_remove_session(s->session_ctx, s->session);
  2343. }
  2344. SSL_SESSION_free(s->session);
  2345. s->session = new_sess;
  2346. }
  2347. /*
  2348. * Technically the cast to long here is not guaranteed by the C standard -
  2349. * but we use it elsewhere, so this should be ok.
  2350. */
  2351. s->session->time = (long)time(NULL);
  2352. OPENSSL_free(s->session->ext.tick);
  2353. s->session->ext.tick = NULL;
  2354. s->session->ext.ticklen = 0;
  2355. s->session->ext.tick = OPENSSL_malloc(ticklen);
  2356. if (s->session->ext.tick == NULL) {
  2357. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_NEW_SESSION_TICKET,
  2358. ERR_R_MALLOC_FAILURE);
  2359. goto err;
  2360. }
  2361. if (!PACKET_copy_bytes(pkt, s->session->ext.tick, ticklen)) {
  2362. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_NEW_SESSION_TICKET,
  2363. SSL_R_LENGTH_MISMATCH);
  2364. goto err;
  2365. }
  2366. s->session->ext.tick_lifetime_hint = ticket_lifetime_hint;
  2367. s->session->ext.tick_age_add = age_add;
  2368. s->session->ext.ticklen = ticklen;
  2369. if (SSL_IS_TLS13(s)) {
  2370. PACKET extpkt;
  2371. if (!PACKET_as_length_prefixed_2(pkt, &extpkt)
  2372. || PACKET_remaining(pkt) != 0) {
  2373. SSLfatal(s, SSL_AD_DECODE_ERROR,
  2374. SSL_F_TLS_PROCESS_NEW_SESSION_TICKET,
  2375. SSL_R_LENGTH_MISMATCH);
  2376. goto err;
  2377. }
  2378. if (!tls_collect_extensions(s, &extpkt,
  2379. SSL_EXT_TLS1_3_NEW_SESSION_TICKET, &exts,
  2380. NULL, 1)
  2381. || !tls_parse_all_extensions(s,
  2382. SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
  2383. exts, NULL, 0, 1)) {
  2384. /* SSLfatal() already called */
  2385. goto err;
  2386. }
  2387. }
  2388. /*
  2389. * There are two ways to detect a resumed ticket session. One is to set
  2390. * an appropriate session ID and then the server must return a match in
  2391. * ServerHello. This allows the normal client session ID matching to work
  2392. * and we know much earlier that the ticket has been accepted. The
  2393. * other way is to set zero length session ID when the ticket is
  2394. * presented and rely on the handshake to determine session resumption.
  2395. * We choose the former approach because this fits in with assumptions
  2396. * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
  2397. * SHA256 is disabled) hash of the ticket.
  2398. */
  2399. /*
  2400. * TODO(size_t): we use sess_len here because EVP_Digest expects an int
  2401. * but s->session->session_id_length is a size_t
  2402. */
  2403. if (!EVP_Digest(s->session->ext.tick, ticklen,
  2404. s->session->session_id, &sess_len,
  2405. EVP_sha256(), NULL)) {
  2406. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_NEW_SESSION_TICKET,
  2407. ERR_R_EVP_LIB);
  2408. goto err;
  2409. }
  2410. s->session->session_id_length = sess_len;
  2411. s->session->not_resumable = 0;
  2412. /* This is a standalone message in TLSv1.3, so there is no more to read */
  2413. if (SSL_IS_TLS13(s)) {
  2414. const EVP_MD *md = ssl_handshake_md(s);
  2415. int hashleni = EVP_MD_size(md);
  2416. size_t hashlen;
  2417. static const unsigned char nonce_label[] = "resumption";
  2418. /* Ensure cast to size_t is safe */
  2419. if (!ossl_assert(hashleni >= 0)) {
  2420. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2421. SSL_F_TLS_PROCESS_NEW_SESSION_TICKET,
  2422. ERR_R_INTERNAL_ERROR);
  2423. goto err;
  2424. }
  2425. hashlen = (size_t)hashleni;
  2426. if (!tls13_hkdf_expand(s, md, s->resumption_master_secret,
  2427. nonce_label,
  2428. sizeof(nonce_label) - 1,
  2429. PACKET_data(&nonce),
  2430. PACKET_remaining(&nonce),
  2431. s->session->master_key,
  2432. hashlen, 1)) {
  2433. /* SSLfatal() already called */
  2434. goto err;
  2435. }
  2436. s->session->master_key_length = hashlen;
  2437. OPENSSL_free(exts);
  2438. ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
  2439. return MSG_PROCESS_FINISHED_READING;
  2440. }
  2441. return MSG_PROCESS_CONTINUE_READING;
  2442. err:
  2443. OPENSSL_free(exts);
  2444. return MSG_PROCESS_ERROR;
  2445. }
  2446. /*
  2447. * In TLSv1.3 this is called from the extensions code, otherwise it is used to
  2448. * parse a separate message. Returns 1 on success or 0 on failure
  2449. */
  2450. int tls_process_cert_status_body(SSL *s, PACKET *pkt)
  2451. {
  2452. size_t resplen;
  2453. unsigned int type;
  2454. if (!PACKET_get_1(pkt, &type)
  2455. || type != TLSEXT_STATUSTYPE_ocsp) {
  2456. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_STATUS_BODY,
  2457. SSL_R_UNSUPPORTED_STATUS_TYPE);
  2458. return 0;
  2459. }
  2460. if (!PACKET_get_net_3_len(pkt, &resplen)
  2461. || PACKET_remaining(pkt) != resplen) {
  2462. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_STATUS_BODY,
  2463. SSL_R_LENGTH_MISMATCH);
  2464. return 0;
  2465. }
  2466. s->ext.ocsp.resp = OPENSSL_malloc(resplen);
  2467. if (s->ext.ocsp.resp == NULL) {
  2468. s->ext.ocsp.resp_len = 0;
  2469. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_STATUS_BODY,
  2470. ERR_R_MALLOC_FAILURE);
  2471. return 0;
  2472. }
  2473. s->ext.ocsp.resp_len = resplen;
  2474. if (!PACKET_copy_bytes(pkt, s->ext.ocsp.resp, resplen)) {
  2475. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_STATUS_BODY,
  2476. SSL_R_LENGTH_MISMATCH);
  2477. return 0;
  2478. }
  2479. return 1;
  2480. }
  2481. MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
  2482. {
  2483. if (!tls_process_cert_status_body(s, pkt)) {
  2484. /* SSLfatal() already called */
  2485. return MSG_PROCESS_ERROR;
  2486. }
  2487. return MSG_PROCESS_CONTINUE_READING;
  2488. }
  2489. /*
  2490. * Perform miscellaneous checks and processing after we have received the
  2491. * server's initial flight. In TLS1.3 this is after the Server Finished message.
  2492. * In <=TLS1.2 this is after the ServerDone message. Returns 1 on success or 0
  2493. * on failure.
  2494. */
  2495. int tls_process_initial_server_flight(SSL *s)
  2496. {
  2497. /*
  2498. * at this point we check that we have the required stuff from
  2499. * the server
  2500. */
  2501. if (!ssl3_check_cert_and_algorithm(s)) {
  2502. /* SSLfatal() already called */
  2503. return 0;
  2504. }
  2505. /*
  2506. * Call the ocsp status callback if needed. The |ext.ocsp.resp| and
  2507. * |ext.ocsp.resp_len| values will be set if we actually received a status
  2508. * message, or NULL and -1 otherwise
  2509. */
  2510. if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing
  2511. && s->ctx->ext.status_cb != NULL) {
  2512. int ret = s->ctx->ext.status_cb(s, s->ctx->ext.status_arg);
  2513. if (ret == 0) {
  2514. SSLfatal(s, SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE,
  2515. SSL_F_TLS_PROCESS_INITIAL_SERVER_FLIGHT,
  2516. SSL_R_INVALID_STATUS_RESPONSE);
  2517. return 0;
  2518. }
  2519. if (ret < 0) {
  2520. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2521. SSL_F_TLS_PROCESS_INITIAL_SERVER_FLIGHT,
  2522. SSL_R_OCSP_CALLBACK_FAILURE);
  2523. return 0;
  2524. }
  2525. }
  2526. #ifndef OPENSSL_NO_CT
  2527. if (s->ct_validation_callback != NULL) {
  2528. /* Note we validate the SCTs whether or not we abort on error */
  2529. if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) {
  2530. /* SSLfatal() already called */
  2531. return 0;
  2532. }
  2533. }
  2534. #endif
  2535. return 1;
  2536. }
  2537. MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
  2538. {
  2539. if (PACKET_remaining(pkt) > 0) {
  2540. /* should contain no data */
  2541. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_SERVER_DONE,
  2542. SSL_R_LENGTH_MISMATCH);
  2543. return MSG_PROCESS_ERROR;
  2544. }
  2545. #ifndef OPENSSL_NO_SRP
  2546. if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
  2547. if (SRP_Calc_A_param(s) <= 0) {
  2548. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SERVER_DONE,
  2549. SSL_R_SRP_A_CALC);
  2550. return MSG_PROCESS_ERROR;
  2551. }
  2552. }
  2553. #endif
  2554. if (!tls_process_initial_server_flight(s)) {
  2555. /* SSLfatal() already called */
  2556. return MSG_PROCESS_ERROR;
  2557. }
  2558. return MSG_PROCESS_FINISHED_READING;
  2559. }
  2560. static int tls_construct_cke_psk_preamble(SSL *s, WPACKET *pkt)
  2561. {
  2562. #ifndef OPENSSL_NO_PSK
  2563. int ret = 0;
  2564. /*
  2565. * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
  2566. * \0-terminated identity. The last byte is for us for simulating
  2567. * strnlen.
  2568. */
  2569. char identity[PSK_MAX_IDENTITY_LEN + 1];
  2570. size_t identitylen = 0;
  2571. unsigned char psk[PSK_MAX_PSK_LEN];
  2572. unsigned char *tmppsk = NULL;
  2573. char *tmpidentity = NULL;
  2574. size_t psklen = 0;
  2575. if (s->psk_client_callback == NULL) {
  2576. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
  2577. SSL_R_PSK_NO_CLIENT_CB);
  2578. goto err;
  2579. }
  2580. memset(identity, 0, sizeof(identity));
  2581. psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
  2582. identity, sizeof(identity) - 1,
  2583. psk, sizeof(psk));
  2584. if (psklen > PSK_MAX_PSK_LEN) {
  2585. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  2586. SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
  2587. psklen = PSK_MAX_PSK_LEN; /* Avoid overrunning the array on cleanse */
  2588. goto err;
  2589. } else if (psklen == 0) {
  2590. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  2591. SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
  2592. SSL_R_PSK_IDENTITY_NOT_FOUND);
  2593. goto err;
  2594. }
  2595. identitylen = strlen(identity);
  2596. if (identitylen > PSK_MAX_IDENTITY_LEN) {
  2597. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
  2598. ERR_R_INTERNAL_ERROR);
  2599. goto err;
  2600. }
  2601. tmppsk = OPENSSL_memdup(psk, psklen);
  2602. tmpidentity = OPENSSL_strdup(identity);
  2603. if (tmppsk == NULL || tmpidentity == NULL) {
  2604. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
  2605. ERR_R_MALLOC_FAILURE);
  2606. goto err;
  2607. }
  2608. OPENSSL_free(s->s3->tmp.psk);
  2609. s->s3->tmp.psk = tmppsk;
  2610. s->s3->tmp.psklen = psklen;
  2611. tmppsk = NULL;
  2612. OPENSSL_free(s->session->psk_identity);
  2613. s->session->psk_identity = tmpidentity;
  2614. tmpidentity = NULL;
  2615. if (!WPACKET_sub_memcpy_u16(pkt, identity, identitylen)) {
  2616. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
  2617. ERR_R_INTERNAL_ERROR);
  2618. goto err;
  2619. }
  2620. ret = 1;
  2621. err:
  2622. OPENSSL_cleanse(psk, psklen);
  2623. OPENSSL_cleanse(identity, sizeof(identity));
  2624. OPENSSL_clear_free(tmppsk, psklen);
  2625. OPENSSL_clear_free(tmpidentity, identitylen);
  2626. return ret;
  2627. #else
  2628. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
  2629. ERR_R_INTERNAL_ERROR);
  2630. return 0;
  2631. #endif
  2632. }
  2633. static int tls_construct_cke_rsa(SSL *s, WPACKET *pkt)
  2634. {
  2635. #ifndef OPENSSL_NO_RSA
  2636. unsigned char *encdata = NULL;
  2637. EVP_PKEY *pkey = NULL;
  2638. EVP_PKEY_CTX *pctx = NULL;
  2639. size_t enclen;
  2640. unsigned char *pms = NULL;
  2641. size_t pmslen = 0;
  2642. if (s->session->peer == NULL) {
  2643. /*
  2644. * We should always have a server certificate with SSL_kRSA.
  2645. */
  2646. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2647. ERR_R_INTERNAL_ERROR);
  2648. return 0;
  2649. }
  2650. pkey = X509_get0_pubkey(s->session->peer);
  2651. if (EVP_PKEY_get0_RSA(pkey) == NULL) {
  2652. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2653. ERR_R_INTERNAL_ERROR);
  2654. return 0;
  2655. }
  2656. pmslen = SSL_MAX_MASTER_KEY_LENGTH;
  2657. pms = OPENSSL_malloc(pmslen);
  2658. if (pms == NULL) {
  2659. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2660. ERR_R_MALLOC_FAILURE);
  2661. return 0;
  2662. }
  2663. pms[0] = s->client_version >> 8;
  2664. pms[1] = s->client_version & 0xff;
  2665. /* TODO(size_t): Convert this function */
  2666. if (RAND_bytes(pms + 2, (int)(pmslen - 2)) <= 0) {
  2667. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2668. ERR_R_MALLOC_FAILURE);
  2669. goto err;
  2670. }
  2671. /* Fix buf for TLS and beyond */
  2672. if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) {
  2673. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2674. ERR_R_INTERNAL_ERROR);
  2675. goto err;
  2676. }
  2677. pctx = EVP_PKEY_CTX_new(pkey, NULL);
  2678. if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
  2679. || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
  2680. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2681. ERR_R_EVP_LIB);
  2682. goto err;
  2683. }
  2684. if (!WPACKET_allocate_bytes(pkt, enclen, &encdata)
  2685. || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) {
  2686. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2687. SSL_R_BAD_RSA_ENCRYPT);
  2688. goto err;
  2689. }
  2690. EVP_PKEY_CTX_free(pctx);
  2691. pctx = NULL;
  2692. /* Fix buf for TLS and beyond */
  2693. if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) {
  2694. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2695. ERR_R_INTERNAL_ERROR);
  2696. goto err;
  2697. }
  2698. /* Log the premaster secret, if logging is enabled. */
  2699. if (!ssl_log_rsa_client_key_exchange(s, encdata, enclen, pms, pmslen)) {
  2700. /* SSLfatal() already called */
  2701. goto err;
  2702. }
  2703. s->s3->tmp.pms = pms;
  2704. s->s3->tmp.pmslen = pmslen;
  2705. return 1;
  2706. err:
  2707. OPENSSL_clear_free(pms, pmslen);
  2708. EVP_PKEY_CTX_free(pctx);
  2709. return 0;
  2710. #else
  2711. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2712. ERR_R_INTERNAL_ERROR);
  2713. return 0;
  2714. #endif
  2715. }
  2716. static int tls_construct_cke_dhe(SSL *s, WPACKET *pkt)
  2717. {
  2718. #ifndef OPENSSL_NO_DH
  2719. DH *dh_clnt = NULL;
  2720. const BIGNUM *pub_key;
  2721. EVP_PKEY *ckey = NULL, *skey = NULL;
  2722. unsigned char *keybytes = NULL;
  2723. skey = s->s3->peer_tmp;
  2724. if (skey == NULL) {
  2725. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_DHE,
  2726. ERR_R_INTERNAL_ERROR);
  2727. goto err;
  2728. }
  2729. ckey = ssl_generate_pkey(skey);
  2730. if (ckey == NULL) {
  2731. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_DHE,
  2732. ERR_R_INTERNAL_ERROR);
  2733. goto err;
  2734. }
  2735. dh_clnt = EVP_PKEY_get0_DH(ckey);
  2736. if (dh_clnt == NULL) {
  2737. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_DHE,
  2738. ERR_R_INTERNAL_ERROR);
  2739. goto err;
  2740. }
  2741. if (ssl_derive(s, ckey, skey, 0) == 0) {
  2742. /* SSLfatal() already called */
  2743. goto err;
  2744. }
  2745. /* send off the data */
  2746. DH_get0_key(dh_clnt, &pub_key, NULL);
  2747. if (!WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(pub_key),
  2748. &keybytes)) {
  2749. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_DHE,
  2750. ERR_R_INTERNAL_ERROR);
  2751. goto err;
  2752. }
  2753. BN_bn2bin(pub_key, keybytes);
  2754. EVP_PKEY_free(ckey);
  2755. return 1;
  2756. err:
  2757. EVP_PKEY_free(ckey);
  2758. return 0;
  2759. #else
  2760. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_DHE,
  2761. ERR_R_INTERNAL_ERROR);
  2762. return 0;
  2763. #endif
  2764. }
  2765. static int tls_construct_cke_ecdhe(SSL *s, WPACKET *pkt)
  2766. {
  2767. #ifndef OPENSSL_NO_EC
  2768. unsigned char *encodedPoint = NULL;
  2769. size_t encoded_pt_len = 0;
  2770. EVP_PKEY *ckey = NULL, *skey = NULL;
  2771. int ret = 0;
  2772. skey = s->s3->peer_tmp;
  2773. if (skey == NULL) {
  2774. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_ECDHE,
  2775. ERR_R_INTERNAL_ERROR);
  2776. return 0;
  2777. }
  2778. ckey = ssl_generate_pkey(skey);
  2779. if (ckey == NULL) {
  2780. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_ECDHE,
  2781. ERR_R_MALLOC_FAILURE);
  2782. goto err;
  2783. }
  2784. if (ssl_derive(s, ckey, skey, 0) == 0) {
  2785. /* SSLfatal() already called */
  2786. goto err;
  2787. }
  2788. /* Generate encoding of client key */
  2789. encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(ckey, &encodedPoint);
  2790. if (encoded_pt_len == 0) {
  2791. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_ECDHE,
  2792. ERR_R_EC_LIB);
  2793. goto err;
  2794. }
  2795. if (!WPACKET_sub_memcpy_u8(pkt, encodedPoint, encoded_pt_len)) {
  2796. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_ECDHE,
  2797. ERR_R_INTERNAL_ERROR);
  2798. goto err;
  2799. }
  2800. ret = 1;
  2801. err:
  2802. OPENSSL_free(encodedPoint);
  2803. EVP_PKEY_free(ckey);
  2804. return ret;
  2805. #else
  2806. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_ECDHE,
  2807. ERR_R_INTERNAL_ERROR);
  2808. return 0;
  2809. #endif
  2810. }
  2811. static int tls_construct_cke_gost(SSL *s, WPACKET *pkt)
  2812. {
  2813. #ifndef OPENSSL_NO_GOST
  2814. /* GOST key exchange message creation */
  2815. EVP_PKEY_CTX *pkey_ctx = NULL;
  2816. X509 *peer_cert;
  2817. size_t msglen;
  2818. unsigned int md_len;
  2819. unsigned char shared_ukm[32], tmp[256];
  2820. EVP_MD_CTX *ukm_hash = NULL;
  2821. int dgst_nid = NID_id_GostR3411_94;
  2822. unsigned char *pms = NULL;
  2823. size_t pmslen = 0;
  2824. if ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
  2825. dgst_nid = NID_id_GostR3411_2012_256;
  2826. /*
  2827. * Get server certificate PKEY and create ctx from it
  2828. */
  2829. peer_cert = s->session->peer;
  2830. if (!peer_cert) {
  2831. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2832. SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
  2833. return 0;
  2834. }
  2835. pkey_ctx = EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert), NULL);
  2836. if (pkey_ctx == NULL) {
  2837. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2838. ERR_R_MALLOC_FAILURE);
  2839. return 0;
  2840. }
  2841. /*
  2842. * If we have send a certificate, and certificate key
  2843. * parameters match those of server certificate, use
  2844. * certificate key for key exchange
  2845. */
  2846. /* Otherwise, generate ephemeral key pair */
  2847. pmslen = 32;
  2848. pms = OPENSSL_malloc(pmslen);
  2849. if (pms == NULL) {
  2850. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2851. ERR_R_MALLOC_FAILURE);
  2852. goto err;
  2853. }
  2854. if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0
  2855. /* Generate session key
  2856. * TODO(size_t): Convert this function
  2857. */
  2858. || RAND_bytes(pms, (int)pmslen) <= 0) {
  2859. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2860. ERR_R_INTERNAL_ERROR);
  2861. goto err;
  2862. };
  2863. /*
  2864. * Compute shared IV and store it in algorithm-specific context
  2865. * data
  2866. */
  2867. ukm_hash = EVP_MD_CTX_new();
  2868. if (ukm_hash == NULL
  2869. || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0
  2870. || EVP_DigestUpdate(ukm_hash, s->s3->client_random,
  2871. SSL3_RANDOM_SIZE) <= 0
  2872. || EVP_DigestUpdate(ukm_hash, s->s3->server_random,
  2873. SSL3_RANDOM_SIZE) <= 0
  2874. || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
  2875. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2876. ERR_R_INTERNAL_ERROR);
  2877. goto err;
  2878. }
  2879. EVP_MD_CTX_free(ukm_hash);
  2880. ukm_hash = NULL;
  2881. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
  2882. EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) {
  2883. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2884. SSL_R_LIBRARY_BUG);
  2885. goto err;
  2886. }
  2887. /* Make GOST keytransport blob message */
  2888. /*
  2889. * Encapsulate it into sequence
  2890. */
  2891. msglen = 255;
  2892. if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
  2893. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2894. SSL_R_LIBRARY_BUG);
  2895. goto err;
  2896. }
  2897. if (!WPACKET_put_bytes_u8(pkt, V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)
  2898. || (msglen >= 0x80 && !WPACKET_put_bytes_u8(pkt, 0x81))
  2899. || !WPACKET_sub_memcpy_u8(pkt, tmp, msglen)) {
  2900. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2901. ERR_R_INTERNAL_ERROR);
  2902. goto err;
  2903. }
  2904. EVP_PKEY_CTX_free(pkey_ctx);
  2905. s->s3->tmp.pms = pms;
  2906. s->s3->tmp.pmslen = pmslen;
  2907. return 1;
  2908. err:
  2909. EVP_PKEY_CTX_free(pkey_ctx);
  2910. OPENSSL_clear_free(pms, pmslen);
  2911. EVP_MD_CTX_free(ukm_hash);
  2912. return 0;
  2913. #else
  2914. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2915. ERR_R_INTERNAL_ERROR);
  2916. return 0;
  2917. #endif
  2918. }
  2919. static int tls_construct_cke_srp(SSL *s, WPACKET *pkt)
  2920. {
  2921. #ifndef OPENSSL_NO_SRP
  2922. unsigned char *abytes = NULL;
  2923. if (s->srp_ctx.A == NULL
  2924. || !WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(s->srp_ctx.A),
  2925. &abytes)) {
  2926. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_SRP,
  2927. ERR_R_INTERNAL_ERROR);
  2928. return 0;
  2929. }
  2930. BN_bn2bin(s->srp_ctx.A, abytes);
  2931. OPENSSL_free(s->session->srp_username);
  2932. s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
  2933. if (s->session->srp_username == NULL) {
  2934. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_SRP,
  2935. ERR_R_MALLOC_FAILURE);
  2936. return 0;
  2937. }
  2938. return 1;
  2939. #else
  2940. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_SRP,
  2941. ERR_R_INTERNAL_ERROR);
  2942. return 0;
  2943. #endif
  2944. }
  2945. int tls_construct_client_key_exchange(SSL *s, WPACKET *pkt)
  2946. {
  2947. unsigned long alg_k;
  2948. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  2949. /*
  2950. * All of the construct functions below call SSLfatal() if necessary so
  2951. * no need to do so here.
  2952. */
  2953. if ((alg_k & SSL_PSK)
  2954. && !tls_construct_cke_psk_preamble(s, pkt))
  2955. goto err;
  2956. if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
  2957. if (!tls_construct_cke_rsa(s, pkt))
  2958. goto err;
  2959. } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
  2960. if (!tls_construct_cke_dhe(s, pkt))
  2961. goto err;
  2962. } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
  2963. if (!tls_construct_cke_ecdhe(s, pkt))
  2964. goto err;
  2965. } else if (alg_k & SSL_kGOST) {
  2966. if (!tls_construct_cke_gost(s, pkt))
  2967. goto err;
  2968. } else if (alg_k & SSL_kSRP) {
  2969. if (!tls_construct_cke_srp(s, pkt))
  2970. goto err;
  2971. } else if (!(alg_k & SSL_kPSK)) {
  2972. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2973. SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  2974. goto err;
  2975. }
  2976. return 1;
  2977. err:
  2978. OPENSSL_clear_free(s->s3->tmp.pms, s->s3->tmp.pmslen);
  2979. s->s3->tmp.pms = NULL;
  2980. s->s3->tmp.pmslen = 0;
  2981. #ifndef OPENSSL_NO_PSK
  2982. OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
  2983. s->s3->tmp.psk = NULL;
  2984. s->s3->tmp.psklen = 0;
  2985. #endif
  2986. return 0;
  2987. }
  2988. int tls_client_key_exchange_post_work(SSL *s)
  2989. {
  2990. unsigned char *pms = NULL;
  2991. size_t pmslen = 0;
  2992. pms = s->s3->tmp.pms;
  2993. pmslen = s->s3->tmp.pmslen;
  2994. #ifndef OPENSSL_NO_SRP
  2995. /* Check for SRP */
  2996. if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
  2997. if (!srp_generate_client_master_secret(s)) {
  2998. /* SSLfatal() already called */
  2999. goto err;
  3000. }
  3001. return 1;
  3002. }
  3003. #endif
  3004. if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
  3005. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3006. SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
  3007. goto err;
  3008. }
  3009. if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
  3010. /* SSLfatal() already called */
  3011. /* ssl_generate_master_secret frees the pms even on error */
  3012. pms = NULL;
  3013. pmslen = 0;
  3014. goto err;
  3015. }
  3016. pms = NULL;
  3017. pmslen = 0;
  3018. #ifndef OPENSSL_NO_SCTP
  3019. if (SSL_IS_DTLS(s)) {
  3020. unsigned char sctpauthkey[64];
  3021. char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
  3022. size_t labellen;
  3023. /*
  3024. * Add new shared key for SCTP-Auth, will be ignored if no SCTP
  3025. * used.
  3026. */
  3027. memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
  3028. sizeof(DTLS1_SCTP_AUTH_LABEL));
  3029. /* Don't include the terminating zero. */
  3030. labellen = sizeof(labelbuffer) - 1;
  3031. if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
  3032. labellen += 1;
  3033. if (SSL_export_keying_material(s, sctpauthkey,
  3034. sizeof(sctpauthkey), labelbuffer,
  3035. labellen, NULL, 0, 0) <= 0) {
  3036. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3037. SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
  3038. ERR_R_INTERNAL_ERROR);
  3039. goto err;
  3040. }
  3041. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  3042. sizeof(sctpauthkey), sctpauthkey);
  3043. }
  3044. #endif
  3045. return 1;
  3046. err:
  3047. OPENSSL_clear_free(pms, pmslen);
  3048. s->s3->tmp.pms = NULL;
  3049. s->s3->tmp.pmslen = 0;
  3050. return 0;
  3051. }
  3052. /*
  3053. * Check a certificate can be used for client authentication. Currently check
  3054. * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
  3055. * certificates can be used and optionally checks suitability for Suite B.
  3056. */
  3057. static int ssl3_check_client_certificate(SSL *s)
  3058. {
  3059. /* If no suitable signature algorithm can't use certificate */
  3060. if (!tls_choose_sigalg(s, 0) || s->s3->tmp.sigalg == NULL)
  3061. return 0;
  3062. /*
  3063. * If strict mode check suitability of chain before using it. This also
  3064. * adjusts suite B digest if necessary.
  3065. */
  3066. if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
  3067. !tls1_check_chain(s, NULL, NULL, NULL, -2))
  3068. return 0;
  3069. return 1;
  3070. }
  3071. WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst)
  3072. {
  3073. X509 *x509 = NULL;
  3074. EVP_PKEY *pkey = NULL;
  3075. int i;
  3076. if (wst == WORK_MORE_A) {
  3077. /* Let cert callback update client certificates if required */
  3078. if (s->cert->cert_cb) {
  3079. i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
  3080. if (i < 0) {
  3081. s->rwstate = SSL_X509_LOOKUP;
  3082. return WORK_MORE_A;
  3083. }
  3084. if (i == 0) {
  3085. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3086. SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
  3087. SSL_R_CALLBACK_FAILED);
  3088. return WORK_ERROR;
  3089. }
  3090. s->rwstate = SSL_NOTHING;
  3091. }
  3092. if (ssl3_check_client_certificate(s)) {
  3093. if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
  3094. return WORK_FINISHED_STOP;
  3095. }
  3096. return WORK_FINISHED_CONTINUE;
  3097. }
  3098. /* Fall through to WORK_MORE_B */
  3099. wst = WORK_MORE_B;
  3100. }
  3101. /* We need to get a client cert */
  3102. if (wst == WORK_MORE_B) {
  3103. /*
  3104. * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
  3105. * return(-1); We then get retied later
  3106. */
  3107. i = ssl_do_client_cert_cb(s, &x509, &pkey);
  3108. if (i < 0) {
  3109. s->rwstate = SSL_X509_LOOKUP;
  3110. return WORK_MORE_B;
  3111. }
  3112. s->rwstate = SSL_NOTHING;
  3113. if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
  3114. if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
  3115. i = 0;
  3116. } else if (i == 1) {
  3117. i = 0;
  3118. SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
  3119. SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  3120. }
  3121. X509_free(x509);
  3122. EVP_PKEY_free(pkey);
  3123. if (i && !ssl3_check_client_certificate(s))
  3124. i = 0;
  3125. if (i == 0) {
  3126. if (s->version == SSL3_VERSION) {
  3127. s->s3->tmp.cert_req = 0;
  3128. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
  3129. return WORK_FINISHED_CONTINUE;
  3130. } else {
  3131. s->s3->tmp.cert_req = 2;
  3132. if (!ssl3_digest_cached_records(s, 0)) {
  3133. /* SSLfatal() already called */
  3134. return WORK_ERROR;
  3135. }
  3136. }
  3137. }
  3138. if (s->post_handshake_auth == SSL_PHA_REQUESTED)
  3139. return WORK_FINISHED_STOP;
  3140. return WORK_FINISHED_CONTINUE;
  3141. }
  3142. /* Shouldn't ever get here */
  3143. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
  3144. ERR_R_INTERNAL_ERROR);
  3145. return WORK_ERROR;
  3146. }
  3147. int tls_construct_client_certificate(SSL *s, WPACKET *pkt)
  3148. {
  3149. if (SSL_IS_TLS13(s)) {
  3150. if (s->pha_context == NULL) {
  3151. /* no context available, add 0-length context */
  3152. if (!WPACKET_put_bytes_u8(pkt, 0)) {
  3153. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3154. SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
  3155. return 0;
  3156. }
  3157. } else if (!WPACKET_sub_memcpy_u8(pkt, s->pha_context, s->pha_context_len)) {
  3158. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3159. SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
  3160. return 0;
  3161. }
  3162. }
  3163. if (!ssl3_output_cert_chain(s, pkt,
  3164. (s->s3->tmp.cert_req == 2) ? NULL
  3165. : s->cert->key)) {
  3166. /* SSLfatal() already called */
  3167. return 0;
  3168. }
  3169. if (SSL_IS_TLS13(s)
  3170. && SSL_IS_FIRST_HANDSHAKE(s)
  3171. && (!s->method->ssl3_enc->change_cipher_state(s,
  3172. SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) {
  3173. /*
  3174. * This is a fatal error, which leaves enc_write_ctx in an inconsistent
  3175. * state and thus ssl3_send_alert may crash.
  3176. */
  3177. SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE,
  3178. SSL_R_CANNOT_CHANGE_CIPHER);
  3179. return 0;
  3180. }
  3181. return 1;
  3182. }
  3183. int ssl3_check_cert_and_algorithm(SSL *s)
  3184. {
  3185. const SSL_CERT_LOOKUP *clu;
  3186. size_t idx;
  3187. long alg_k, alg_a;
  3188. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  3189. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  3190. /* we don't have a certificate */
  3191. if (!(alg_a & SSL_aCERT))
  3192. return 1;
  3193. /* This is the passed certificate */
  3194. clu = ssl_cert_lookup_by_pkey(X509_get0_pubkey(s->session->peer), &idx);
  3195. /* Check certificate is recognised and suitable for cipher */
  3196. if (clu == NULL || (alg_a & clu->amask) == 0) {
  3197. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3198. SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3199. SSL_R_MISSING_SIGNING_CERT);
  3200. return 0;
  3201. }
  3202. #ifndef OPENSSL_NO_EC
  3203. if (clu->amask & SSL_aECDSA) {
  3204. if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s))
  3205. return 1;
  3206. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3207. SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
  3208. return 0;
  3209. }
  3210. #endif
  3211. #ifndef OPENSSL_NO_RSA
  3212. if (alg_k & (SSL_kRSA | SSL_kRSAPSK) && idx != SSL_PKEY_RSA) {
  3213. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3214. SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3215. SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  3216. return 0;
  3217. }
  3218. #endif
  3219. #ifndef OPENSSL_NO_DH
  3220. if ((alg_k & SSL_kDHE) && (s->s3->peer_tmp == NULL)) {
  3221. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3222. ERR_R_INTERNAL_ERROR);
  3223. return 0;
  3224. }
  3225. #endif
  3226. return 1;
  3227. }
  3228. #ifndef OPENSSL_NO_NEXTPROTONEG
  3229. int tls_construct_next_proto(SSL *s, WPACKET *pkt)
  3230. {
  3231. size_t len, padding_len;
  3232. unsigned char *padding = NULL;
  3233. len = s->ext.npn_len;
  3234. padding_len = 32 - ((len + 2) % 32);
  3235. if (!WPACKET_sub_memcpy_u8(pkt, s->ext.npn, len)
  3236. || !WPACKET_sub_allocate_bytes_u8(pkt, padding_len, &padding)) {
  3237. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_NEXT_PROTO,
  3238. ERR_R_INTERNAL_ERROR);
  3239. return 0;
  3240. }
  3241. memset(padding, 0, padding_len);
  3242. return 1;
  3243. }
  3244. #endif
  3245. MSG_PROCESS_RETURN tls_process_hello_req(SSL *s, PACKET *pkt)
  3246. {
  3247. if (PACKET_remaining(pkt) > 0) {
  3248. /* should contain no data */
  3249. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_HELLO_REQ,
  3250. SSL_R_LENGTH_MISMATCH);
  3251. return MSG_PROCESS_ERROR;
  3252. }
  3253. if ((s->options & SSL_OP_NO_RENEGOTIATION)) {
  3254. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
  3255. return MSG_PROCESS_FINISHED_READING;
  3256. }
  3257. /*
  3258. * This is a historical discrepancy (not in the RFC) maintained for
  3259. * compatibility reasons. If a TLS client receives a HelloRequest it will
  3260. * attempt an abbreviated handshake. However if a DTLS client receives a
  3261. * HelloRequest it will do a full handshake. Either behaviour is reasonable
  3262. * but doing one for TLS and another for DTLS is odd.
  3263. */
  3264. if (SSL_IS_DTLS(s))
  3265. SSL_renegotiate(s);
  3266. else
  3267. SSL_renegotiate_abbreviated(s);
  3268. return MSG_PROCESS_FINISHED_READING;
  3269. }
  3270. static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL *s, PACKET *pkt)
  3271. {
  3272. PACKET extensions;
  3273. RAW_EXTENSION *rawexts = NULL;
  3274. if (!PACKET_as_length_prefixed_2(pkt, &extensions)
  3275. || PACKET_remaining(pkt) != 0) {
  3276. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_ENCRYPTED_EXTENSIONS,
  3277. SSL_R_LENGTH_MISMATCH);
  3278. goto err;
  3279. }
  3280. if (!tls_collect_extensions(s, &extensions,
  3281. SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, &rawexts,
  3282. NULL, 1)
  3283. || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
  3284. rawexts, NULL, 0, 1)) {
  3285. /* SSLfatal() already called */
  3286. goto err;
  3287. }
  3288. OPENSSL_free(rawexts);
  3289. return MSG_PROCESS_CONTINUE_READING;
  3290. err:
  3291. OPENSSL_free(rawexts);
  3292. return MSG_PROCESS_ERROR;
  3293. }
  3294. int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
  3295. {
  3296. int i = 0;
  3297. #ifndef OPENSSL_NO_ENGINE
  3298. if (s->ctx->client_cert_engine) {
  3299. i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
  3300. SSL_get_client_CA_list(s),
  3301. px509, ppkey, NULL, NULL, NULL);
  3302. if (i != 0)
  3303. return i;
  3304. }
  3305. #endif
  3306. if (s->ctx->client_cert_cb)
  3307. i = s->ctx->client_cert_cb(s, px509, ppkey);
  3308. return i;
  3309. }
  3310. int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, WPACKET *pkt)
  3311. {
  3312. int i;
  3313. size_t totlen = 0, len, maxlen, maxverok = 0;
  3314. int empty_reneg_info_scsv = !s->renegotiate;
  3315. /* Set disabled masks for this session */
  3316. if (!ssl_set_client_disabled(s)) {
  3317. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_CIPHER_LIST_TO_BYTES,
  3318. SSL_R_NO_PROTOCOLS_AVAILABLE);
  3319. return 0;
  3320. }
  3321. if (sk == NULL) {
  3322. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_CIPHER_LIST_TO_BYTES,
  3323. ERR_R_INTERNAL_ERROR);
  3324. return 0;
  3325. }
  3326. #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
  3327. # if OPENSSL_MAX_TLS1_2_CIPHER_LENGTH < 6
  3328. # error Max cipher length too short
  3329. # endif
  3330. /*
  3331. * Some servers hang if client hello > 256 bytes as hack workaround
  3332. * chop number of supported ciphers to keep it well below this if we
  3333. * use TLS v1.2
  3334. */
  3335. if (TLS1_get_version(s) >= TLS1_2_VERSION)
  3336. maxlen = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
  3337. else
  3338. #endif
  3339. /* Maximum length that can be stored in 2 bytes. Length must be even */
  3340. maxlen = 0xfffe;
  3341. if (empty_reneg_info_scsv)
  3342. maxlen -= 2;
  3343. if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV)
  3344. maxlen -= 2;
  3345. for (i = 0; i < sk_SSL_CIPHER_num(sk) && totlen < maxlen; i++) {
  3346. const SSL_CIPHER *c;
  3347. c = sk_SSL_CIPHER_value(sk, i);
  3348. /* Skip disabled ciphers */
  3349. if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0))
  3350. continue;
  3351. if (!s->method->put_cipher_by_char(c, pkt, &len)) {
  3352. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_CIPHER_LIST_TO_BYTES,
  3353. ERR_R_INTERNAL_ERROR);
  3354. return 0;
  3355. }
  3356. /* Sanity check that the maximum version we offer has ciphers enabled */
  3357. if (!maxverok) {
  3358. if (SSL_IS_DTLS(s)) {
  3359. if (DTLS_VERSION_GE(c->max_dtls, s->s3->tmp.max_ver)
  3360. && DTLS_VERSION_LE(c->min_dtls, s->s3->tmp.max_ver))
  3361. maxverok = 1;
  3362. } else {
  3363. if (c->max_tls >= s->s3->tmp.max_ver
  3364. && c->min_tls <= s->s3->tmp.max_ver)
  3365. maxverok = 1;
  3366. }
  3367. }
  3368. totlen += len;
  3369. }
  3370. if (totlen == 0 || !maxverok) {
  3371. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_CIPHER_LIST_TO_BYTES,
  3372. SSL_R_NO_CIPHERS_AVAILABLE);
  3373. if (!maxverok)
  3374. ERR_add_error_data(1, "No ciphers enabled for max supported "
  3375. "SSL/TLS version");
  3376. return 0;
  3377. }
  3378. if (totlen != 0) {
  3379. if (empty_reneg_info_scsv) {
  3380. static SSL_CIPHER scsv = {
  3381. 0, NULL, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
  3382. };
  3383. if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) {
  3384. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3385. SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR);
  3386. return 0;
  3387. }
  3388. }
  3389. if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
  3390. static SSL_CIPHER scsv = {
  3391. 0, NULL, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
  3392. };
  3393. if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) {
  3394. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3395. SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR);
  3396. return 0;
  3397. }
  3398. }
  3399. }
  3400. return 1;
  3401. }
  3402. int tls_construct_end_of_early_data(SSL *s, WPACKET *pkt)
  3403. {
  3404. if (s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY
  3405. && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING) {
  3406. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3407. SSL_F_TLS_CONSTRUCT_END_OF_EARLY_DATA,
  3408. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  3409. return 0;
  3410. }
  3411. s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;
  3412. return 1;
  3413. }