statem_clnt.c 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841
  1. /*
  2. * Copyright 1995-2018 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 Apache License 2.0 (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_locl.h"
  15. #include "statem_locl.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 <openssl/trace.h>
  25. #include <internal/cryptlib.h>
  26. static MSG_PROCESS_RETURN tls_process_as_hello_retry_request(SSL *s, PACKET *pkt);
  27. static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL *s, PACKET *pkt);
  28. static ossl_inline int cert_req_allowed(SSL *s);
  29. static int key_exchange_expected(SSL *s);
  30. static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
  31. WPACKET *pkt);
  32. /*
  33. * Is a CertificateRequest message allowed at the moment or not?
  34. *
  35. * Return values are:
  36. * 1: Yes
  37. * 0: No
  38. */
  39. static ossl_inline int cert_req_allowed(SSL *s)
  40. {
  41. /* TLS does not like anon-DH with client cert */
  42. if ((s->version > SSL3_VERSION
  43. && (s->s3.tmp.new_cipher->algorithm_auth & SSL_aNULL))
  44. || (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK)))
  45. return 0;
  46. return 1;
  47. }
  48. /*
  49. * Should we expect the ServerKeyExchange message or not?
  50. *
  51. * Return values are:
  52. * 1: Yes
  53. * 0: No
  54. */
  55. static int key_exchange_expected(SSL *s)
  56. {
  57. long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
  58. /*
  59. * Can't skip server key exchange if this is an ephemeral
  60. * ciphersuite or for SRP
  61. */
  62. if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK
  63. | SSL_kSRP)) {
  64. return 1;
  65. }
  66. return 0;
  67. }
  68. /*
  69. * ossl_statem_client_read_transition() encapsulates the logic for the allowed
  70. * handshake state transitions when a TLS1.3 client is reading messages from the
  71. * server. The message type that the server has sent is provided in |mt|. The
  72. * current state is in |s->statem.hand_state|.
  73. *
  74. * Return values are 1 for success (transition allowed) and 0 on error
  75. * (transition not allowed)
  76. */
  77. static int ossl_statem_client13_read_transition(SSL *s, int mt)
  78. {
  79. OSSL_STATEM *st = &s->statem;
  80. /*
  81. * Note: There is no case for TLS_ST_CW_CLNT_HELLO, because we haven't
  82. * yet negotiated TLSv1.3 at that point so that is handled by
  83. * ossl_statem_client_read_transition()
  84. */
  85. switch (st->hand_state) {
  86. default:
  87. break;
  88. case TLS_ST_CW_CLNT_HELLO:
  89. /*
  90. * This must a ClientHello following a HelloRetryRequest, so the only
  91. * thing we can get now is a ServerHello.
  92. */
  93. if (mt == SSL3_MT_SERVER_HELLO) {
  94. st->hand_state = TLS_ST_CR_SRVR_HELLO;
  95. return 1;
  96. }
  97. break;
  98. case TLS_ST_CR_SRVR_HELLO:
  99. if (mt == SSL3_MT_ENCRYPTED_EXTENSIONS) {
  100. st->hand_state = TLS_ST_CR_ENCRYPTED_EXTENSIONS;
  101. return 1;
  102. }
  103. break;
  104. case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
  105. if (s->hit) {
  106. if (mt == SSL3_MT_FINISHED) {
  107. st->hand_state = TLS_ST_CR_FINISHED;
  108. return 1;
  109. }
  110. } else {
  111. if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
  112. st->hand_state = TLS_ST_CR_CERT_REQ;
  113. return 1;
  114. }
  115. if (mt == SSL3_MT_CERTIFICATE) {
  116. st->hand_state = TLS_ST_CR_CERT;
  117. return 1;
  118. }
  119. }
  120. break;
  121. case TLS_ST_CR_CERT_REQ:
  122. if (mt == SSL3_MT_CERTIFICATE) {
  123. st->hand_state = TLS_ST_CR_CERT;
  124. return 1;
  125. }
  126. break;
  127. case TLS_ST_CR_CERT:
  128. if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
  129. st->hand_state = TLS_ST_CR_CERT_VRFY;
  130. return 1;
  131. }
  132. break;
  133. case TLS_ST_CR_CERT_VRFY:
  134. if (mt == SSL3_MT_FINISHED) {
  135. st->hand_state = TLS_ST_CR_FINISHED;
  136. return 1;
  137. }
  138. break;
  139. case TLS_ST_OK:
  140. if (mt == SSL3_MT_NEWSESSION_TICKET) {
  141. st->hand_state = TLS_ST_CR_SESSION_TICKET;
  142. return 1;
  143. }
  144. if (mt == SSL3_MT_KEY_UPDATE) {
  145. st->hand_state = TLS_ST_CR_KEY_UPDATE;
  146. return 1;
  147. }
  148. if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
  149. #if DTLS_MAX_VERSION_INTERNAL != DTLS1_2_VERSION
  150. # error TODO(DTLS1.3): Restore digest for PHA before adding message.
  151. #endif
  152. if (!SSL_IS_DTLS(s) && s->post_handshake_auth == SSL_PHA_EXT_SENT) {
  153. s->post_handshake_auth = SSL_PHA_REQUESTED;
  154. /*
  155. * In TLS, this is called before the message is added to the
  156. * digest. In DTLS, this is expected to be called after adding
  157. * to the digest. Either move the digest restore, or add the
  158. * message here after the swap, or do it after the clientFinished?
  159. */
  160. if (!tls13_restore_handshake_digest_for_pha(s)) {
  161. /* SSLfatal() already called */
  162. return 0;
  163. }
  164. st->hand_state = TLS_ST_CR_CERT_REQ;
  165. return 1;
  166. }
  167. }
  168. break;
  169. }
  170. /* No valid transition found */
  171. return 0;
  172. }
  173. /*
  174. * ossl_statem_client_read_transition() encapsulates the logic for the allowed
  175. * handshake state transitions when the client is reading messages from the
  176. * server. The message type that the server has sent is provided in |mt|. The
  177. * current state is in |s->statem.hand_state|.
  178. *
  179. * Return values are 1 for success (transition allowed) and 0 on error
  180. * (transition not allowed)
  181. */
  182. int ossl_statem_client_read_transition(SSL *s, int mt)
  183. {
  184. OSSL_STATEM *st = &s->statem;
  185. int ske_expected;
  186. /*
  187. * Note that after writing the first ClientHello we don't know what version
  188. * we are going to negotiate yet, so we don't take this branch until later.
  189. */
  190. if (SSL_IS_TLS13(s)) {
  191. if (!ossl_statem_client13_read_transition(s, mt))
  192. goto err;
  193. return 1;
  194. }
  195. switch (st->hand_state) {
  196. default:
  197. break;
  198. case TLS_ST_CW_CLNT_HELLO:
  199. if (mt == SSL3_MT_SERVER_HELLO) {
  200. st->hand_state = TLS_ST_CR_SRVR_HELLO;
  201. return 1;
  202. }
  203. if (SSL_IS_DTLS(s)) {
  204. if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
  205. st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
  206. return 1;
  207. }
  208. }
  209. break;
  210. case TLS_ST_EARLY_DATA:
  211. /*
  212. * We've not actually selected TLSv1.3 yet, but we have sent early
  213. * data. The only thing allowed now is a ServerHello or a
  214. * HelloRetryRequest.
  215. */
  216. if (mt == SSL3_MT_SERVER_HELLO) {
  217. st->hand_state = TLS_ST_CR_SRVR_HELLO;
  218. return 1;
  219. }
  220. break;
  221. case TLS_ST_CR_SRVR_HELLO:
  222. if (s->hit) {
  223. if (s->ext.ticket_expected) {
  224. if (mt == SSL3_MT_NEWSESSION_TICKET) {
  225. st->hand_state = TLS_ST_CR_SESSION_TICKET;
  226. return 1;
  227. }
  228. } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  229. st->hand_state = TLS_ST_CR_CHANGE;
  230. return 1;
  231. }
  232. } else {
  233. if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
  234. st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
  235. return 1;
  236. } else if (s->version >= TLS1_VERSION
  237. && s->ext.session_secret_cb != NULL
  238. && s->session->ext.tick != NULL
  239. && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  240. /*
  241. * Normally, we can tell if the server is resuming the session
  242. * from the session ID. EAP-FAST (RFC 4851), however, relies on
  243. * the next server message after the ServerHello to determine if
  244. * the server is resuming.
  245. */
  246. s->hit = 1;
  247. st->hand_state = TLS_ST_CR_CHANGE;
  248. return 1;
  249. } else if (!(s->s3.tmp.new_cipher->algorithm_auth
  250. & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
  251. if (mt == SSL3_MT_CERTIFICATE) {
  252. st->hand_state = TLS_ST_CR_CERT;
  253. return 1;
  254. }
  255. } else {
  256. ske_expected = key_exchange_expected(s);
  257. /* SKE is optional for some PSK ciphersuites */
  258. if (ske_expected
  259. || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)
  260. && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
  261. if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
  262. st->hand_state = TLS_ST_CR_KEY_EXCH;
  263. return 1;
  264. }
  265. } else if (mt == SSL3_MT_CERTIFICATE_REQUEST
  266. && cert_req_allowed(s)) {
  267. st->hand_state = TLS_ST_CR_CERT_REQ;
  268. return 1;
  269. } else if (mt == SSL3_MT_SERVER_DONE) {
  270. st->hand_state = TLS_ST_CR_SRVR_DONE;
  271. return 1;
  272. }
  273. }
  274. }
  275. break;
  276. case TLS_ST_CR_CERT:
  277. /*
  278. * The CertificateStatus message is optional even if
  279. * |ext.status_expected| is set
  280. */
  281. if (s->ext.status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) {
  282. st->hand_state = TLS_ST_CR_CERT_STATUS;
  283. return 1;
  284. }
  285. /* Fall through */
  286. case TLS_ST_CR_CERT_STATUS:
  287. ske_expected = key_exchange_expected(s);
  288. /* SKE is optional for some PSK ciphersuites */
  289. if (ske_expected || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)
  290. && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
  291. if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
  292. st->hand_state = TLS_ST_CR_KEY_EXCH;
  293. return 1;
  294. }
  295. goto err;
  296. }
  297. /* Fall through */
  298. case TLS_ST_CR_KEY_EXCH:
  299. if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
  300. if (cert_req_allowed(s)) {
  301. st->hand_state = TLS_ST_CR_CERT_REQ;
  302. return 1;
  303. }
  304. goto err;
  305. }
  306. /* Fall through */
  307. case TLS_ST_CR_CERT_REQ:
  308. if (mt == SSL3_MT_SERVER_DONE) {
  309. st->hand_state = TLS_ST_CR_SRVR_DONE;
  310. return 1;
  311. }
  312. break;
  313. case TLS_ST_CW_FINISHED:
  314. if (s->ext.ticket_expected) {
  315. if (mt == SSL3_MT_NEWSESSION_TICKET) {
  316. st->hand_state = TLS_ST_CR_SESSION_TICKET;
  317. return 1;
  318. }
  319. } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  320. st->hand_state = TLS_ST_CR_CHANGE;
  321. return 1;
  322. }
  323. break;
  324. case TLS_ST_CR_SESSION_TICKET:
  325. if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  326. st->hand_state = TLS_ST_CR_CHANGE;
  327. return 1;
  328. }
  329. break;
  330. case TLS_ST_CR_CHANGE:
  331. if (mt == SSL3_MT_FINISHED) {
  332. st->hand_state = TLS_ST_CR_FINISHED;
  333. return 1;
  334. }
  335. break;
  336. case TLS_ST_OK:
  337. if (mt == SSL3_MT_HELLO_REQUEST) {
  338. st->hand_state = TLS_ST_CR_HELLO_REQ;
  339. return 1;
  340. }
  341. break;
  342. }
  343. err:
  344. /* No valid transition found */
  345. if (SSL_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  346. BIO *rbio;
  347. /*
  348. * CCS messages don't have a message sequence number so this is probably
  349. * because of an out-of-order CCS. We'll just drop it.
  350. */
  351. s->init_num = 0;
  352. s->rwstate = SSL_READING;
  353. rbio = SSL_get_rbio(s);
  354. BIO_clear_retry_flags(rbio);
  355. BIO_set_retry_read(rbio);
  356. return 0;
  357. }
  358. SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE,
  359. SSL_F_OSSL_STATEM_CLIENT_READ_TRANSITION,
  360. SSL_R_UNEXPECTED_MESSAGE);
  361. return 0;
  362. }
  363. /*
  364. * ossl_statem_client13_write_transition() works out what handshake state to
  365. * move to next when the TLSv1.3 client is writing messages to be sent to the
  366. * server.
  367. */
  368. static WRITE_TRAN ossl_statem_client13_write_transition(SSL *s)
  369. {
  370. OSSL_STATEM *st = &s->statem;
  371. /*
  372. * Note: There are no cases for TLS_ST_BEFORE because we haven't negotiated
  373. * TLSv1.3 yet at that point. They are handled by
  374. * ossl_statem_client_write_transition().
  375. */
  376. switch (st->hand_state) {
  377. default:
  378. /* Shouldn't happen */
  379. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  380. SSL_F_OSSL_STATEM_CLIENT13_WRITE_TRANSITION,
  381. ERR_R_INTERNAL_ERROR);
  382. return WRITE_TRAN_ERROR;
  383. case TLS_ST_CR_CERT_REQ:
  384. if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
  385. st->hand_state = TLS_ST_CW_CERT;
  386. return WRITE_TRAN_CONTINUE;
  387. }
  388. /*
  389. * We should only get here if we received a CertificateRequest after
  390. * we already sent close_notify
  391. */
  392. if (!ossl_assert((s->shutdown & SSL_SENT_SHUTDOWN) != 0)) {
  393. /* Shouldn't happen - same as default case */
  394. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  395. SSL_F_OSSL_STATEM_CLIENT13_WRITE_TRANSITION,
  396. ERR_R_INTERNAL_ERROR);
  397. return WRITE_TRAN_ERROR;
  398. }
  399. st->hand_state = TLS_ST_OK;
  400. return WRITE_TRAN_CONTINUE;
  401. case TLS_ST_CR_FINISHED:
  402. if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY
  403. || s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING)
  404. st->hand_state = TLS_ST_PENDING_EARLY_DATA_END;
  405. else if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
  406. && s->hello_retry_request == SSL_HRR_NONE)
  407. st->hand_state = TLS_ST_CW_CHANGE;
  408. else
  409. st->hand_state = (s->s3.tmp.cert_req != 0) ? TLS_ST_CW_CERT
  410. : TLS_ST_CW_FINISHED;
  411. return WRITE_TRAN_CONTINUE;
  412. case TLS_ST_PENDING_EARLY_DATA_END:
  413. if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
  414. st->hand_state = TLS_ST_CW_END_OF_EARLY_DATA;
  415. return WRITE_TRAN_CONTINUE;
  416. }
  417. /* Fall through */
  418. case TLS_ST_CW_END_OF_EARLY_DATA:
  419. case TLS_ST_CW_CHANGE:
  420. st->hand_state = (s->s3.tmp.cert_req != 0) ? TLS_ST_CW_CERT
  421. : TLS_ST_CW_FINISHED;
  422. return WRITE_TRAN_CONTINUE;
  423. case TLS_ST_CW_CERT:
  424. /* If a non-empty Certificate we also send CertificateVerify */
  425. st->hand_state = (s->s3.tmp.cert_req == 1) ? TLS_ST_CW_CERT_VRFY
  426. : TLS_ST_CW_FINISHED;
  427. return WRITE_TRAN_CONTINUE;
  428. case TLS_ST_CW_CERT_VRFY:
  429. st->hand_state = TLS_ST_CW_FINISHED;
  430. return WRITE_TRAN_CONTINUE;
  431. case TLS_ST_CR_KEY_UPDATE:
  432. case TLS_ST_CW_KEY_UPDATE:
  433. case TLS_ST_CR_SESSION_TICKET:
  434. case TLS_ST_CW_FINISHED:
  435. st->hand_state = TLS_ST_OK;
  436. return WRITE_TRAN_CONTINUE;
  437. case TLS_ST_OK:
  438. if (s->key_update != SSL_KEY_UPDATE_NONE) {
  439. st->hand_state = TLS_ST_CW_KEY_UPDATE;
  440. return WRITE_TRAN_CONTINUE;
  441. }
  442. /* Try to read from the server instead */
  443. return WRITE_TRAN_FINISHED;
  444. }
  445. }
  446. /*
  447. * ossl_statem_client_write_transition() works out what handshake state to
  448. * move to next when the client is writing messages to be sent to the server.
  449. */
  450. WRITE_TRAN ossl_statem_client_write_transition(SSL *s)
  451. {
  452. OSSL_STATEM *st = &s->statem;
  453. /*
  454. * Note that immediately before/after a ClientHello we don't know what
  455. * version we are going to negotiate yet, so we don't take this branch until
  456. * later
  457. */
  458. if (SSL_IS_TLS13(s))
  459. return ossl_statem_client13_write_transition(s);
  460. switch (st->hand_state) {
  461. default:
  462. /* Shouldn't happen */
  463. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  464. SSL_F_OSSL_STATEM_CLIENT_WRITE_TRANSITION,
  465. ERR_R_INTERNAL_ERROR);
  466. return WRITE_TRAN_ERROR;
  467. case TLS_ST_OK:
  468. if (!s->renegotiate) {
  469. /*
  470. * We haven't requested a renegotiation ourselves so we must have
  471. * received a message from the server. Better read it.
  472. */
  473. return WRITE_TRAN_FINISHED;
  474. }
  475. /* Renegotiation */
  476. /* fall thru */
  477. case TLS_ST_BEFORE:
  478. st->hand_state = TLS_ST_CW_CLNT_HELLO;
  479. return WRITE_TRAN_CONTINUE;
  480. case TLS_ST_CW_CLNT_HELLO:
  481. if (s->early_data_state == SSL_EARLY_DATA_CONNECTING) {
  482. /*
  483. * We are assuming this is a TLSv1.3 connection, although we haven't
  484. * actually selected a version yet.
  485. */
  486. if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0)
  487. st->hand_state = TLS_ST_CW_CHANGE;
  488. else
  489. st->hand_state = TLS_ST_EARLY_DATA;
  490. return WRITE_TRAN_CONTINUE;
  491. }
  492. /*
  493. * No transition at the end of writing because we don't know what
  494. * we will be sent
  495. */
  496. return WRITE_TRAN_FINISHED;
  497. case TLS_ST_CR_SRVR_HELLO:
  498. /*
  499. * We only get here in TLSv1.3. We just received an HRR, so issue a
  500. * CCS unless middlebox compat mode is off, or we already issued one
  501. * because we did early data.
  502. */
  503. if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
  504. && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING)
  505. st->hand_state = TLS_ST_CW_CHANGE;
  506. else
  507. st->hand_state = TLS_ST_CW_CLNT_HELLO;
  508. return WRITE_TRAN_CONTINUE;
  509. case TLS_ST_EARLY_DATA:
  510. return WRITE_TRAN_FINISHED;
  511. case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
  512. st->hand_state = TLS_ST_CW_CLNT_HELLO;
  513. return WRITE_TRAN_CONTINUE;
  514. case TLS_ST_CR_SRVR_DONE:
  515. if (s->s3.tmp.cert_req)
  516. st->hand_state = TLS_ST_CW_CERT;
  517. else
  518. st->hand_state = TLS_ST_CW_KEY_EXCH;
  519. return WRITE_TRAN_CONTINUE;
  520. case TLS_ST_CW_CERT:
  521. st->hand_state = TLS_ST_CW_KEY_EXCH;
  522. return WRITE_TRAN_CONTINUE;
  523. case TLS_ST_CW_KEY_EXCH:
  524. /*
  525. * For TLS, cert_req is set to 2, so a cert chain of nothing is
  526. * sent, but no verify packet is sent
  527. */
  528. /*
  529. * XXX: For now, we do not support client authentication in ECDH
  530. * cipher suites with ECDH (rather than ECDSA) certificates. We
  531. * need to skip the certificate verify message when client's
  532. * ECDH public key is sent inside the client certificate.
  533. */
  534. if (s->s3.tmp.cert_req == 1) {
  535. st->hand_state = TLS_ST_CW_CERT_VRFY;
  536. } else {
  537. st->hand_state = TLS_ST_CW_CHANGE;
  538. }
  539. if (s->s3.flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
  540. st->hand_state = TLS_ST_CW_CHANGE;
  541. }
  542. return WRITE_TRAN_CONTINUE;
  543. case TLS_ST_CW_CERT_VRFY:
  544. st->hand_state = TLS_ST_CW_CHANGE;
  545. return WRITE_TRAN_CONTINUE;
  546. case TLS_ST_CW_CHANGE:
  547. if (s->hello_retry_request == SSL_HRR_PENDING) {
  548. st->hand_state = TLS_ST_CW_CLNT_HELLO;
  549. } else if (s->early_data_state == SSL_EARLY_DATA_CONNECTING) {
  550. st->hand_state = TLS_ST_EARLY_DATA;
  551. } else {
  552. #if defined(OPENSSL_NO_NEXTPROTONEG)
  553. st->hand_state = TLS_ST_CW_FINISHED;
  554. #else
  555. if (!SSL_IS_DTLS(s) && s->s3.npn_seen)
  556. st->hand_state = TLS_ST_CW_NEXT_PROTO;
  557. else
  558. st->hand_state = TLS_ST_CW_FINISHED;
  559. #endif
  560. }
  561. return WRITE_TRAN_CONTINUE;
  562. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  563. case TLS_ST_CW_NEXT_PROTO:
  564. st->hand_state = TLS_ST_CW_FINISHED;
  565. return WRITE_TRAN_CONTINUE;
  566. #endif
  567. case TLS_ST_CW_FINISHED:
  568. if (s->hit) {
  569. st->hand_state = TLS_ST_OK;
  570. return WRITE_TRAN_CONTINUE;
  571. } else {
  572. return WRITE_TRAN_FINISHED;
  573. }
  574. case TLS_ST_CR_FINISHED:
  575. if (s->hit) {
  576. st->hand_state = TLS_ST_CW_CHANGE;
  577. return WRITE_TRAN_CONTINUE;
  578. } else {
  579. st->hand_state = TLS_ST_OK;
  580. return WRITE_TRAN_CONTINUE;
  581. }
  582. case TLS_ST_CR_HELLO_REQ:
  583. /*
  584. * If we can renegotiate now then do so, otherwise wait for a more
  585. * convenient time.
  586. */
  587. if (ssl3_renegotiate_check(s, 1)) {
  588. if (!tls_setup_handshake(s)) {
  589. /* SSLfatal() already called */
  590. return WRITE_TRAN_ERROR;
  591. }
  592. st->hand_state = TLS_ST_CW_CLNT_HELLO;
  593. return WRITE_TRAN_CONTINUE;
  594. }
  595. st->hand_state = TLS_ST_OK;
  596. return WRITE_TRAN_CONTINUE;
  597. }
  598. }
  599. /*
  600. * Perform any pre work that needs to be done prior to sending a message from
  601. * the client to the server.
  602. */
  603. WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst)
  604. {
  605. OSSL_STATEM *st = &s->statem;
  606. switch (st->hand_state) {
  607. default:
  608. /* No pre work to be done */
  609. break;
  610. case TLS_ST_CW_CLNT_HELLO:
  611. s->shutdown = 0;
  612. if (SSL_IS_DTLS(s)) {
  613. /* every DTLS ClientHello resets Finished MAC */
  614. if (!ssl3_init_finished_mac(s)) {
  615. /* SSLfatal() already called */
  616. return WORK_ERROR;
  617. }
  618. }
  619. break;
  620. case TLS_ST_CW_CHANGE:
  621. if (SSL_IS_DTLS(s)) {
  622. if (s->hit) {
  623. /*
  624. * We're into the last flight so we don't retransmit these
  625. * messages unless we need to.
  626. */
  627. st->use_timer = 0;
  628. }
  629. #ifndef OPENSSL_NO_SCTP
  630. if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
  631. /* Calls SSLfatal() as required */
  632. return dtls_wait_for_dry(s);
  633. }
  634. #endif
  635. }
  636. break;
  637. case TLS_ST_PENDING_EARLY_DATA_END:
  638. /*
  639. * If we've been called by SSL_do_handshake()/SSL_write(), or we did not
  640. * attempt to write early data before calling SSL_read() then we press
  641. * on with the handshake. Otherwise we pause here.
  642. */
  643. if (s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING
  644. || s->early_data_state == SSL_EARLY_DATA_NONE)
  645. return WORK_FINISHED_CONTINUE;
  646. /* Fall through */
  647. case TLS_ST_EARLY_DATA:
  648. return tls_finish_handshake(s, wst, 0, 1);
  649. case TLS_ST_OK:
  650. /* Calls SSLfatal() as required */
  651. return tls_finish_handshake(s, wst, 1, 1);
  652. }
  653. return WORK_FINISHED_CONTINUE;
  654. }
  655. /*
  656. * Perform any work that needs to be done after sending a message from the
  657. * client to the server.
  658. */
  659. WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst)
  660. {
  661. OSSL_STATEM *st = &s->statem;
  662. s->init_num = 0;
  663. switch (st->hand_state) {
  664. default:
  665. /* No post work to be done */
  666. break;
  667. case TLS_ST_CW_CLNT_HELLO:
  668. if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
  669. && s->max_early_data > 0) {
  670. /*
  671. * We haven't selected TLSv1.3 yet so we don't call the change
  672. * cipher state function associated with the SSL_METHOD. Instead
  673. * we call tls13_change_cipher_state() directly.
  674. */
  675. if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0) {
  676. if (!tls13_change_cipher_state(s,
  677. SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
  678. /* SSLfatal() already called */
  679. return WORK_ERROR;
  680. }
  681. }
  682. /* else we're in compat mode so we delay flushing until after CCS */
  683. } else if (!statem_flush(s)) {
  684. return WORK_MORE_A;
  685. }
  686. if (SSL_IS_DTLS(s)) {
  687. /* Treat the next message as the first packet */
  688. s->first_packet = 1;
  689. }
  690. break;
  691. case TLS_ST_CW_END_OF_EARLY_DATA:
  692. /*
  693. * We set the enc_write_ctx back to NULL because we may end up writing
  694. * in cleartext again if we get a HelloRetryRequest from the server.
  695. */
  696. EVP_CIPHER_CTX_free(s->enc_write_ctx);
  697. s->enc_write_ctx = NULL;
  698. break;
  699. case TLS_ST_CW_KEY_EXCH:
  700. if (tls_client_key_exchange_post_work(s) == 0) {
  701. /* SSLfatal() already called */
  702. return WORK_ERROR;
  703. }
  704. break;
  705. case TLS_ST_CW_CHANGE:
  706. if (SSL_IS_TLS13(s) || s->hello_retry_request == SSL_HRR_PENDING)
  707. break;
  708. if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
  709. && s->max_early_data > 0) {
  710. /*
  711. * We haven't selected TLSv1.3 yet so we don't call the change
  712. * cipher state function associated with the SSL_METHOD. Instead
  713. * we call tls13_change_cipher_state() directly.
  714. */
  715. if (!tls13_change_cipher_state(s,
  716. SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE))
  717. return WORK_ERROR;
  718. break;
  719. }
  720. s->session->cipher = s->s3.tmp.new_cipher;
  721. #ifdef OPENSSL_NO_COMP
  722. s->session->compress_meth = 0;
  723. #else
  724. if (s->s3.tmp.new_compression == NULL)
  725. s->session->compress_meth = 0;
  726. else
  727. s->session->compress_meth = s->s3.tmp.new_compression->id;
  728. #endif
  729. if (!s->method->ssl3_enc->setup_key_block(s)) {
  730. /* SSLfatal() already called */
  731. return WORK_ERROR;
  732. }
  733. if (!s->method->ssl3_enc->change_cipher_state(s,
  734. SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
  735. /* SSLfatal() already called */
  736. return WORK_ERROR;
  737. }
  738. if (SSL_IS_DTLS(s)) {
  739. #ifndef OPENSSL_NO_SCTP
  740. if (s->hit) {
  741. /*
  742. * Change to new shared key of SCTP-Auth, will be ignored if
  743. * no SCTP used.
  744. */
  745. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  746. 0, NULL);
  747. }
  748. #endif
  749. dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
  750. }
  751. break;
  752. case TLS_ST_CW_FINISHED:
  753. #ifndef OPENSSL_NO_SCTP
  754. if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) {
  755. /*
  756. * Change to new shared key of SCTP-Auth, will be ignored if
  757. * no SCTP used.
  758. */
  759. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  760. 0, NULL);
  761. }
  762. #endif
  763. if (statem_flush(s) != 1)
  764. return WORK_MORE_B;
  765. if (SSL_IS_TLS13(s)) {
  766. if (!tls13_save_handshake_digest_for_pha(s)) {
  767. /* SSLfatal() already called */
  768. return WORK_ERROR;
  769. }
  770. if (s->post_handshake_auth != SSL_PHA_REQUESTED) {
  771. if (!s->method->ssl3_enc->change_cipher_state(s,
  772. SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
  773. /* SSLfatal() already called */
  774. return WORK_ERROR;
  775. }
  776. }
  777. }
  778. break;
  779. case TLS_ST_CW_KEY_UPDATE:
  780. if (statem_flush(s) != 1)
  781. return WORK_MORE_A;
  782. if (!tls13_update_key(s, 1)) {
  783. /* SSLfatal() already called */
  784. return WORK_ERROR;
  785. }
  786. break;
  787. }
  788. return WORK_FINISHED_CONTINUE;
  789. }
  790. /*
  791. * Get the message construction function and message type for sending from the
  792. * client
  793. *
  794. * Valid return values are:
  795. * 1: Success
  796. * 0: Error
  797. */
  798. int ossl_statem_client_construct_message(SSL *s, WPACKET *pkt,
  799. confunc_f *confunc, int *mt)
  800. {
  801. OSSL_STATEM *st = &s->statem;
  802. switch (st->hand_state) {
  803. default:
  804. /* Shouldn't happen */
  805. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  806. SSL_F_OSSL_STATEM_CLIENT_CONSTRUCT_MESSAGE,
  807. SSL_R_BAD_HANDSHAKE_STATE);
  808. return 0;
  809. case TLS_ST_CW_CHANGE:
  810. if (SSL_IS_DTLS(s))
  811. *confunc = dtls_construct_change_cipher_spec;
  812. else
  813. *confunc = tls_construct_change_cipher_spec;
  814. *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
  815. break;
  816. case TLS_ST_CW_CLNT_HELLO:
  817. *confunc = tls_construct_client_hello;
  818. *mt = SSL3_MT_CLIENT_HELLO;
  819. break;
  820. case TLS_ST_CW_END_OF_EARLY_DATA:
  821. *confunc = tls_construct_end_of_early_data;
  822. *mt = SSL3_MT_END_OF_EARLY_DATA;
  823. break;
  824. case TLS_ST_PENDING_EARLY_DATA_END:
  825. *confunc = NULL;
  826. *mt = SSL3_MT_DUMMY;
  827. break;
  828. case TLS_ST_CW_CERT:
  829. *confunc = tls_construct_client_certificate;
  830. *mt = SSL3_MT_CERTIFICATE;
  831. break;
  832. case TLS_ST_CW_KEY_EXCH:
  833. *confunc = tls_construct_client_key_exchange;
  834. *mt = SSL3_MT_CLIENT_KEY_EXCHANGE;
  835. break;
  836. case TLS_ST_CW_CERT_VRFY:
  837. *confunc = tls_construct_cert_verify;
  838. *mt = SSL3_MT_CERTIFICATE_VERIFY;
  839. break;
  840. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  841. case TLS_ST_CW_NEXT_PROTO:
  842. *confunc = tls_construct_next_proto;
  843. *mt = SSL3_MT_NEXT_PROTO;
  844. break;
  845. #endif
  846. case TLS_ST_CW_FINISHED:
  847. *confunc = tls_construct_finished;
  848. *mt = SSL3_MT_FINISHED;
  849. break;
  850. case TLS_ST_CW_KEY_UPDATE:
  851. *confunc = tls_construct_key_update;
  852. *mt = SSL3_MT_KEY_UPDATE;
  853. break;
  854. }
  855. return 1;
  856. }
  857. /*
  858. * Returns the maximum allowed length for the current message that we are
  859. * reading. Excludes the message header.
  860. */
  861. size_t ossl_statem_client_max_message_size(SSL *s)
  862. {
  863. OSSL_STATEM *st = &s->statem;
  864. switch (st->hand_state) {
  865. default:
  866. /* Shouldn't happen */
  867. return 0;
  868. case TLS_ST_CR_SRVR_HELLO:
  869. return SERVER_HELLO_MAX_LENGTH;
  870. case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
  871. return HELLO_VERIFY_REQUEST_MAX_LENGTH;
  872. case TLS_ST_CR_CERT:
  873. return s->max_cert_list;
  874. case TLS_ST_CR_CERT_VRFY:
  875. return SSL3_RT_MAX_PLAIN_LENGTH;
  876. case TLS_ST_CR_CERT_STATUS:
  877. return SSL3_RT_MAX_PLAIN_LENGTH;
  878. case TLS_ST_CR_KEY_EXCH:
  879. return SERVER_KEY_EXCH_MAX_LENGTH;
  880. case TLS_ST_CR_CERT_REQ:
  881. /*
  882. * Set to s->max_cert_list for compatibility with previous releases. In
  883. * practice these messages can get quite long if servers are configured
  884. * to provide a long list of acceptable CAs
  885. */
  886. return s->max_cert_list;
  887. case TLS_ST_CR_SRVR_DONE:
  888. return SERVER_HELLO_DONE_MAX_LENGTH;
  889. case TLS_ST_CR_CHANGE:
  890. if (s->version == DTLS1_BAD_VER)
  891. return 3;
  892. return CCS_MAX_LENGTH;
  893. case TLS_ST_CR_SESSION_TICKET:
  894. return SSL3_RT_MAX_PLAIN_LENGTH;
  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. s->session->peer_type = certidx;
  1755. X509_free(s->session->peer);
  1756. X509_up_ref(x);
  1757. s->session->peer = x;
  1758. s->session->verify_result = s->verify_result;
  1759. x = NULL;
  1760. /* Save the current hash state for when we receive the CertificateVerify */
  1761. if (SSL_IS_TLS13(s)
  1762. && !ssl_handshake_hash(s, s->cert_verify_hash,
  1763. sizeof(s->cert_verify_hash),
  1764. &s->cert_verify_hash_len)) {
  1765. /* SSLfatal() already called */;
  1766. goto err;
  1767. }
  1768. ret = MSG_PROCESS_CONTINUE_READING;
  1769. err:
  1770. X509_free(x);
  1771. sk_X509_pop_free(sk, X509_free);
  1772. return ret;
  1773. }
  1774. static int tls_process_ske_psk_preamble(SSL *s, PACKET *pkt)
  1775. {
  1776. #ifndef OPENSSL_NO_PSK
  1777. PACKET psk_identity_hint;
  1778. /* PSK ciphersuites are preceded by an identity hint */
  1779. if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
  1780. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE,
  1781. SSL_R_LENGTH_MISMATCH);
  1782. return 0;
  1783. }
  1784. /*
  1785. * Store PSK identity hint for later use, hint is used in
  1786. * tls_construct_client_key_exchange. Assume that the maximum length of
  1787. * a PSK identity hint can be as long as the maximum length of a PSK
  1788. * identity.
  1789. */
  1790. if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1791. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1792. SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE,
  1793. SSL_R_DATA_LENGTH_TOO_LONG);
  1794. return 0;
  1795. }
  1796. if (PACKET_remaining(&psk_identity_hint) == 0) {
  1797. OPENSSL_free(s->session->psk_identity_hint);
  1798. s->session->psk_identity_hint = NULL;
  1799. } else if (!PACKET_strndup(&psk_identity_hint,
  1800. &s->session->psk_identity_hint)) {
  1801. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE,
  1802. ERR_R_INTERNAL_ERROR);
  1803. return 0;
  1804. }
  1805. return 1;
  1806. #else
  1807. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE,
  1808. ERR_R_INTERNAL_ERROR);
  1809. return 0;
  1810. #endif
  1811. }
  1812. static int tls_process_ske_srp(SSL *s, PACKET *pkt, EVP_PKEY **pkey)
  1813. {
  1814. #ifndef OPENSSL_NO_SRP
  1815. PACKET prime, generator, salt, server_pub;
  1816. if (!PACKET_get_length_prefixed_2(pkt, &prime)
  1817. || !PACKET_get_length_prefixed_2(pkt, &generator)
  1818. || !PACKET_get_length_prefixed_1(pkt, &salt)
  1819. || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
  1820. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_SKE_SRP,
  1821. SSL_R_LENGTH_MISMATCH);
  1822. return 0;
  1823. }
  1824. /* TODO(size_t): Convert BN_bin2bn() calls */
  1825. if ((s->srp_ctx.N =
  1826. BN_bin2bn(PACKET_data(&prime),
  1827. (int)PACKET_remaining(&prime), NULL)) == NULL
  1828. || (s->srp_ctx.g =
  1829. BN_bin2bn(PACKET_data(&generator),
  1830. (int)PACKET_remaining(&generator), NULL)) == NULL
  1831. || (s->srp_ctx.s =
  1832. BN_bin2bn(PACKET_data(&salt),
  1833. (int)PACKET_remaining(&salt), NULL)) == NULL
  1834. || (s->srp_ctx.B =
  1835. BN_bin2bn(PACKET_data(&server_pub),
  1836. (int)PACKET_remaining(&server_pub), NULL)) == NULL) {
  1837. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_SRP,
  1838. ERR_R_BN_LIB);
  1839. return 0;
  1840. }
  1841. if (!srp_verify_server_param(s)) {
  1842. /* SSLfatal() already called */
  1843. return 0;
  1844. }
  1845. /* We must check if there is a certificate */
  1846. if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
  1847. *pkey = X509_get0_pubkey(s->session->peer);
  1848. return 1;
  1849. #else
  1850. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_SRP,
  1851. ERR_R_INTERNAL_ERROR);
  1852. return 0;
  1853. #endif
  1854. }
  1855. static int tls_process_ske_dhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey)
  1856. {
  1857. #ifndef OPENSSL_NO_DH
  1858. PACKET prime, generator, pub_key;
  1859. EVP_PKEY *peer_tmp = NULL;
  1860. DH *dh = NULL;
  1861. BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL;
  1862. int check_bits = 0;
  1863. if (!PACKET_get_length_prefixed_2(pkt, &prime)
  1864. || !PACKET_get_length_prefixed_2(pkt, &generator)
  1865. || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
  1866. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_SKE_DHE,
  1867. SSL_R_LENGTH_MISMATCH);
  1868. return 0;
  1869. }
  1870. peer_tmp = EVP_PKEY_new();
  1871. dh = DH_new();
  1872. if (peer_tmp == NULL || dh == NULL) {
  1873. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_DHE,
  1874. ERR_R_MALLOC_FAILURE);
  1875. goto err;
  1876. }
  1877. /* TODO(size_t): Convert these calls */
  1878. p = BN_bin2bn(PACKET_data(&prime), (int)PACKET_remaining(&prime), NULL);
  1879. g = BN_bin2bn(PACKET_data(&generator), (int)PACKET_remaining(&generator),
  1880. NULL);
  1881. bnpub_key = BN_bin2bn(PACKET_data(&pub_key),
  1882. (int)PACKET_remaining(&pub_key), NULL);
  1883. if (p == NULL || g == NULL || bnpub_key == NULL) {
  1884. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_DHE,
  1885. ERR_R_BN_LIB);
  1886. goto err;
  1887. }
  1888. /* test non-zero pubkey */
  1889. if (BN_is_zero(bnpub_key)) {
  1890. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_SKE_DHE,
  1891. SSL_R_BAD_DH_VALUE);
  1892. goto err;
  1893. }
  1894. if (!DH_set0_pqg(dh, p, NULL, g)) {
  1895. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_DHE,
  1896. ERR_R_BN_LIB);
  1897. goto err;
  1898. }
  1899. p = g = NULL;
  1900. if (DH_check_params(dh, &check_bits) == 0 || check_bits != 0) {
  1901. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_SKE_DHE,
  1902. SSL_R_BAD_DH_VALUE);
  1903. goto err;
  1904. }
  1905. if (!DH_set0_key(dh, bnpub_key, NULL)) {
  1906. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_DHE,
  1907. ERR_R_BN_LIB);
  1908. goto err;
  1909. }
  1910. bnpub_key = NULL;
  1911. if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
  1912. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_PROCESS_SKE_DHE,
  1913. SSL_R_DH_KEY_TOO_SMALL);
  1914. goto err;
  1915. }
  1916. if (EVP_PKEY_assign_DH(peer_tmp, dh) == 0) {
  1917. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_DHE,
  1918. ERR_R_EVP_LIB);
  1919. goto err;
  1920. }
  1921. s->s3.peer_tmp = peer_tmp;
  1922. /*
  1923. * FIXME: This makes assumptions about which ciphersuites come with
  1924. * public keys. We should have a less ad-hoc way of doing this
  1925. */
  1926. if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
  1927. *pkey = X509_get0_pubkey(s->session->peer);
  1928. /* else anonymous DH, so no certificate or pkey. */
  1929. return 1;
  1930. err:
  1931. BN_free(p);
  1932. BN_free(g);
  1933. BN_free(bnpub_key);
  1934. DH_free(dh);
  1935. EVP_PKEY_free(peer_tmp);
  1936. return 0;
  1937. #else
  1938. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_DHE,
  1939. ERR_R_INTERNAL_ERROR);
  1940. return 0;
  1941. #endif
  1942. }
  1943. static int tls_process_ske_ecdhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey)
  1944. {
  1945. #ifndef OPENSSL_NO_EC
  1946. PACKET encoded_pt;
  1947. unsigned int curve_type, curve_id;
  1948. /*
  1949. * Extract elliptic curve parameters and the server's ephemeral ECDH
  1950. * public key. We only support named (not generic) curves and
  1951. * ECParameters in this case is just three bytes.
  1952. */
  1953. if (!PACKET_get_1(pkt, &curve_type) || !PACKET_get_net_2(pkt, &curve_id)) {
  1954. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_SKE_ECDHE,
  1955. SSL_R_LENGTH_TOO_SHORT);
  1956. return 0;
  1957. }
  1958. /*
  1959. * Check curve is named curve type and one of our preferences, if not
  1960. * server has sent an invalid curve.
  1961. */
  1962. if (curve_type != NAMED_CURVE_TYPE
  1963. || !tls1_check_group_id(s, curve_id, 1)) {
  1964. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_SKE_ECDHE,
  1965. SSL_R_WRONG_CURVE);
  1966. return 0;
  1967. }
  1968. if ((s->s3.peer_tmp = ssl_generate_param_group(curve_id)) == NULL) {
  1969. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_ECDHE,
  1970. SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
  1971. return 0;
  1972. }
  1973. if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
  1974. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_SKE_ECDHE,
  1975. SSL_R_LENGTH_MISMATCH);
  1976. return 0;
  1977. }
  1978. if (!EVP_PKEY_set1_tls_encodedpoint(s->s3.peer_tmp,
  1979. PACKET_data(&encoded_pt),
  1980. PACKET_remaining(&encoded_pt))) {
  1981. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_SKE_ECDHE,
  1982. SSL_R_BAD_ECPOINT);
  1983. return 0;
  1984. }
  1985. /*
  1986. * The ECC/TLS specification does not mention the use of DSA to sign
  1987. * ECParameters in the server key exchange message. We do support RSA
  1988. * and ECDSA.
  1989. */
  1990. if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aECDSA)
  1991. *pkey = X509_get0_pubkey(s->session->peer);
  1992. else if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aRSA)
  1993. *pkey = X509_get0_pubkey(s->session->peer);
  1994. /* else anonymous ECDH, so no certificate or pkey. */
  1995. return 1;
  1996. #else
  1997. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SKE_ECDHE,
  1998. ERR_R_INTERNAL_ERROR);
  1999. return 0;
  2000. #endif
  2001. }
  2002. MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt)
  2003. {
  2004. long alg_k;
  2005. EVP_PKEY *pkey = NULL;
  2006. EVP_MD_CTX *md_ctx = NULL;
  2007. EVP_PKEY_CTX *pctx = NULL;
  2008. PACKET save_param_start, signature;
  2009. alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
  2010. save_param_start = *pkt;
  2011. #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
  2012. EVP_PKEY_free(s->s3.peer_tmp);
  2013. s->s3.peer_tmp = NULL;
  2014. #endif
  2015. if (alg_k & SSL_PSK) {
  2016. if (!tls_process_ske_psk_preamble(s, pkt)) {
  2017. /* SSLfatal() already called */
  2018. goto err;
  2019. }
  2020. }
  2021. /* Nothing else to do for plain PSK or RSAPSK */
  2022. if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
  2023. } else if (alg_k & SSL_kSRP) {
  2024. if (!tls_process_ske_srp(s, pkt, &pkey)) {
  2025. /* SSLfatal() already called */
  2026. goto err;
  2027. }
  2028. } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
  2029. if (!tls_process_ske_dhe(s, pkt, &pkey)) {
  2030. /* SSLfatal() already called */
  2031. goto err;
  2032. }
  2033. } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
  2034. if (!tls_process_ske_ecdhe(s, pkt, &pkey)) {
  2035. /* SSLfatal() already called */
  2036. goto err;
  2037. }
  2038. } else if (alg_k) {
  2039. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2040. SSL_R_UNEXPECTED_MESSAGE);
  2041. goto err;
  2042. }
  2043. /* if it was signed, check the signature */
  2044. if (pkey != NULL) {
  2045. PACKET params;
  2046. int maxsig;
  2047. const EVP_MD *md = NULL;
  2048. unsigned char *tbs;
  2049. size_t tbslen;
  2050. int rv;
  2051. /*
  2052. * |pkt| now points to the beginning of the signature, so the difference
  2053. * equals the length of the parameters.
  2054. */
  2055. if (!PACKET_get_sub_packet(&save_param_start, &params,
  2056. PACKET_remaining(&save_param_start) -
  2057. PACKET_remaining(pkt))) {
  2058. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2059. ERR_R_INTERNAL_ERROR);
  2060. goto err;
  2061. }
  2062. if (SSL_USE_SIGALGS(s)) {
  2063. unsigned int sigalg;
  2064. if (!PACKET_get_net_2(pkt, &sigalg)) {
  2065. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2066. SSL_R_LENGTH_TOO_SHORT);
  2067. goto err;
  2068. }
  2069. if (tls12_check_peer_sigalg(s, sigalg, pkey) <=0) {
  2070. /* SSLfatal() already called */
  2071. goto err;
  2072. }
  2073. } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) {
  2074. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2075. ERR_R_INTERNAL_ERROR);
  2076. goto err;
  2077. }
  2078. if (!tls1_lookup_md(s->s3.tmp.peer_sigalg, &md)) {
  2079. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2080. ERR_R_INTERNAL_ERROR);
  2081. goto err;
  2082. }
  2083. if (SSL_USE_SIGALGS(s))
  2084. OSSL_TRACE1(TLS, "USING TLSv1.2 HASH %s\n",
  2085. md == NULL ? "n/a" : EVP_MD_name(md));
  2086. if (!PACKET_get_length_prefixed_2(pkt, &signature)
  2087. || PACKET_remaining(pkt) != 0) {
  2088. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2089. SSL_R_LENGTH_MISMATCH);
  2090. goto err;
  2091. }
  2092. maxsig = EVP_PKEY_size(pkey);
  2093. if (maxsig < 0) {
  2094. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2095. ERR_R_INTERNAL_ERROR);
  2096. goto err;
  2097. }
  2098. /*
  2099. * Check signature length
  2100. */
  2101. if (PACKET_remaining(&signature) > (size_t)maxsig) {
  2102. /* wrong packet length */
  2103. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2104. SSL_R_WRONG_SIGNATURE_LENGTH);
  2105. goto err;
  2106. }
  2107. md_ctx = EVP_MD_CTX_new();
  2108. if (md_ctx == NULL) {
  2109. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2110. ERR_R_MALLOC_FAILURE);
  2111. goto err;
  2112. }
  2113. if (EVP_DigestVerifyInit(md_ctx, &pctx, md, NULL, pkey) <= 0) {
  2114. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2115. ERR_R_EVP_LIB);
  2116. goto err;
  2117. }
  2118. if (SSL_USE_PSS(s)) {
  2119. if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
  2120. || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
  2121. RSA_PSS_SALTLEN_DIGEST) <= 0) {
  2122. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2123. SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
  2124. goto err;
  2125. }
  2126. }
  2127. tbslen = construct_key_exchange_tbs(s, &tbs, PACKET_data(&params),
  2128. PACKET_remaining(&params));
  2129. if (tbslen == 0) {
  2130. /* SSLfatal() already called */
  2131. goto err;
  2132. }
  2133. rv = EVP_DigestVerify(md_ctx, PACKET_data(&signature),
  2134. PACKET_remaining(&signature), tbs, tbslen);
  2135. OPENSSL_free(tbs);
  2136. if (rv <= 0) {
  2137. SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2138. SSL_R_BAD_SIGNATURE);
  2139. goto err;
  2140. }
  2141. EVP_MD_CTX_free(md_ctx);
  2142. md_ctx = NULL;
  2143. } else {
  2144. /* aNULL, aSRP or PSK do not need public keys */
  2145. if (!(s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
  2146. && !(alg_k & SSL_PSK)) {
  2147. /* Might be wrong key type, check it */
  2148. if (ssl3_check_cert_and_algorithm(s)) {
  2149. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2150. SSL_R_BAD_DATA);
  2151. }
  2152. /* else this shouldn't happen, SSLfatal() already called */
  2153. goto err;
  2154. }
  2155. /* still data left over */
  2156. if (PACKET_remaining(pkt) != 0) {
  2157. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  2158. SSL_R_EXTRA_DATA_IN_MESSAGE);
  2159. goto err;
  2160. }
  2161. }
  2162. return MSG_PROCESS_CONTINUE_READING;
  2163. err:
  2164. EVP_MD_CTX_free(md_ctx);
  2165. return MSG_PROCESS_ERROR;
  2166. }
  2167. MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
  2168. {
  2169. size_t i;
  2170. /* Clear certificate validity flags */
  2171. for (i = 0; i < SSL_PKEY_NUM; i++)
  2172. s->s3.tmp.valid_flags[i] = 0;
  2173. if (SSL_IS_TLS13(s)) {
  2174. PACKET reqctx, extensions;
  2175. RAW_EXTENSION *rawexts = NULL;
  2176. if ((s->shutdown & SSL_SENT_SHUTDOWN) != 0) {
  2177. /*
  2178. * We already sent close_notify. This can only happen in TLSv1.3
  2179. * post-handshake messages. We can't reasonably respond to this, so
  2180. * we just ignore it
  2181. */
  2182. return MSG_PROCESS_FINISHED_READING;
  2183. }
  2184. /* Free and zero certificate types: it is not present in TLS 1.3 */
  2185. OPENSSL_free(s->s3.tmp.ctype);
  2186. s->s3.tmp.ctype = NULL;
  2187. s->s3.tmp.ctype_len = 0;
  2188. OPENSSL_free(s->pha_context);
  2189. s->pha_context = NULL;
  2190. if (!PACKET_get_length_prefixed_1(pkt, &reqctx) ||
  2191. !PACKET_memdup(&reqctx, &s->pha_context, &s->pha_context_len)) {
  2192. SSLfatal(s, SSL_AD_DECODE_ERROR,
  2193. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2194. SSL_R_LENGTH_MISMATCH);
  2195. return MSG_PROCESS_ERROR;
  2196. }
  2197. if (!PACKET_get_length_prefixed_2(pkt, &extensions)) {
  2198. SSLfatal(s, SSL_AD_DECODE_ERROR,
  2199. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2200. SSL_R_BAD_LENGTH);
  2201. return MSG_PROCESS_ERROR;
  2202. }
  2203. if (!tls_collect_extensions(s, &extensions,
  2204. SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
  2205. &rawexts, NULL, 1)
  2206. || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
  2207. rawexts, NULL, 0, 1)) {
  2208. /* SSLfatal() already called */
  2209. OPENSSL_free(rawexts);
  2210. return MSG_PROCESS_ERROR;
  2211. }
  2212. OPENSSL_free(rawexts);
  2213. if (!tls1_process_sigalgs(s)) {
  2214. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2215. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2216. SSL_R_BAD_LENGTH);
  2217. return MSG_PROCESS_ERROR;
  2218. }
  2219. } else {
  2220. PACKET ctypes;
  2221. /* get the certificate types */
  2222. if (!PACKET_get_length_prefixed_1(pkt, &ctypes)) {
  2223. SSLfatal(s, SSL_AD_DECODE_ERROR,
  2224. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2225. SSL_R_LENGTH_MISMATCH);
  2226. return MSG_PROCESS_ERROR;
  2227. }
  2228. if (!PACKET_memdup(&ctypes, &s->s3.tmp.ctype, &s->s3.tmp.ctype_len)) {
  2229. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2230. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2231. ERR_R_INTERNAL_ERROR);
  2232. return MSG_PROCESS_ERROR;
  2233. }
  2234. if (SSL_USE_SIGALGS(s)) {
  2235. PACKET sigalgs;
  2236. if (!PACKET_get_length_prefixed_2(pkt, &sigalgs)) {
  2237. SSLfatal(s, SSL_AD_DECODE_ERROR,
  2238. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2239. SSL_R_LENGTH_MISMATCH);
  2240. return MSG_PROCESS_ERROR;
  2241. }
  2242. /*
  2243. * Despite this being for certificates, preserve compatibility
  2244. * with pre-TLS 1.3 and use the regular sigalgs field.
  2245. */
  2246. if (!tls1_save_sigalgs(s, &sigalgs, 0)) {
  2247. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2248. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2249. SSL_R_SIGNATURE_ALGORITHMS_ERROR);
  2250. return MSG_PROCESS_ERROR;
  2251. }
  2252. if (!tls1_process_sigalgs(s)) {
  2253. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2254. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2255. ERR_R_MALLOC_FAILURE);
  2256. return MSG_PROCESS_ERROR;
  2257. }
  2258. }
  2259. /* get the CA RDNs */
  2260. if (!parse_ca_names(s, pkt)) {
  2261. /* SSLfatal() already called */
  2262. return MSG_PROCESS_ERROR;
  2263. }
  2264. }
  2265. if (PACKET_remaining(pkt) != 0) {
  2266. SSLfatal(s, SSL_AD_DECODE_ERROR,
  2267. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2268. SSL_R_LENGTH_MISMATCH);
  2269. return MSG_PROCESS_ERROR;
  2270. }
  2271. /* we should setup a certificate to return.... */
  2272. s->s3.tmp.cert_req = 1;
  2273. /*
  2274. * In TLSv1.3 we don't prepare the client certificate yet. We wait until
  2275. * after the CertificateVerify message has been received. This is because
  2276. * in TLSv1.3 the CertificateRequest arrives before the Certificate message
  2277. * but in TLSv1.2 it is the other way around. We want to make sure that
  2278. * SSL_get_peer_certificate() returns something sensible in
  2279. * client_cert_cb.
  2280. */
  2281. if (SSL_IS_TLS13(s) && s->post_handshake_auth != SSL_PHA_REQUESTED)
  2282. return MSG_PROCESS_CONTINUE_READING;
  2283. return MSG_PROCESS_CONTINUE_PROCESSING;
  2284. }
  2285. MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
  2286. {
  2287. unsigned int ticklen;
  2288. unsigned long ticket_lifetime_hint, age_add = 0;
  2289. unsigned int sess_len;
  2290. RAW_EXTENSION *exts = NULL;
  2291. PACKET nonce;
  2292. PACKET_null_init(&nonce);
  2293. if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
  2294. || (SSL_IS_TLS13(s)
  2295. && (!PACKET_get_net_4(pkt, &age_add)
  2296. || !PACKET_get_length_prefixed_1(pkt, &nonce)))
  2297. || !PACKET_get_net_2(pkt, &ticklen)
  2298. || (SSL_IS_TLS13(s) ? (ticklen == 0 || PACKET_remaining(pkt) < ticklen)
  2299. : PACKET_remaining(pkt) != ticklen)) {
  2300. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_NEW_SESSION_TICKET,
  2301. SSL_R_LENGTH_MISMATCH);
  2302. goto err;
  2303. }
  2304. /*
  2305. * Server is allowed to change its mind (in <=TLSv1.2) and send an empty
  2306. * ticket. We already checked this TLSv1.3 case above, so it should never
  2307. * be 0 here in that instance
  2308. */
  2309. if (ticklen == 0)
  2310. return MSG_PROCESS_CONTINUE_READING;
  2311. /*
  2312. * Sessions must be immutable once they go into the session cache. Otherwise
  2313. * we can get multi-thread problems. Therefore we don't "update" sessions,
  2314. * we replace them with a duplicate. In TLSv1.3 we need to do this every
  2315. * time a NewSessionTicket arrives because those messages arrive
  2316. * post-handshake and the session may have already gone into the session
  2317. * cache.
  2318. */
  2319. if (SSL_IS_TLS13(s) || s->session->session_id_length > 0) {
  2320. SSL_SESSION *new_sess;
  2321. /*
  2322. * We reused an existing session, so we need to replace it with a new
  2323. * one
  2324. */
  2325. if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
  2326. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2327. SSL_F_TLS_PROCESS_NEW_SESSION_TICKET,
  2328. ERR_R_MALLOC_FAILURE);
  2329. goto err;
  2330. }
  2331. if ((s->session_ctx->session_cache_mode & SSL_SESS_CACHE_CLIENT) != 0
  2332. && !SSL_IS_TLS13(s)) {
  2333. /*
  2334. * In TLSv1.2 and below the arrival of a new tickets signals that
  2335. * any old ticket we were using is now out of date, so we remove the
  2336. * old session from the cache. We carry on if this fails
  2337. */
  2338. SSL_CTX_remove_session(s->session_ctx, s->session);
  2339. }
  2340. SSL_SESSION_free(s->session);
  2341. s->session = new_sess;
  2342. }
  2343. /*
  2344. * Technically the cast to long here is not guaranteed by the C standard -
  2345. * but we use it elsewhere, so this should be ok.
  2346. */
  2347. s->session->time = (long)time(NULL);
  2348. OPENSSL_free(s->session->ext.tick);
  2349. s->session->ext.tick = NULL;
  2350. s->session->ext.ticklen = 0;
  2351. s->session->ext.tick = OPENSSL_malloc(ticklen);
  2352. if (s->session->ext.tick == NULL) {
  2353. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_NEW_SESSION_TICKET,
  2354. ERR_R_MALLOC_FAILURE);
  2355. goto err;
  2356. }
  2357. if (!PACKET_copy_bytes(pkt, s->session->ext.tick, ticklen)) {
  2358. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_NEW_SESSION_TICKET,
  2359. SSL_R_LENGTH_MISMATCH);
  2360. goto err;
  2361. }
  2362. s->session->ext.tick_lifetime_hint = ticket_lifetime_hint;
  2363. s->session->ext.tick_age_add = age_add;
  2364. s->session->ext.ticklen = ticklen;
  2365. if (SSL_IS_TLS13(s)) {
  2366. PACKET extpkt;
  2367. if (!PACKET_as_length_prefixed_2(pkt, &extpkt)
  2368. || PACKET_remaining(pkt) != 0) {
  2369. SSLfatal(s, SSL_AD_DECODE_ERROR,
  2370. SSL_F_TLS_PROCESS_NEW_SESSION_TICKET,
  2371. SSL_R_LENGTH_MISMATCH);
  2372. goto err;
  2373. }
  2374. if (!tls_collect_extensions(s, &extpkt,
  2375. SSL_EXT_TLS1_3_NEW_SESSION_TICKET, &exts,
  2376. NULL, 1)
  2377. || !tls_parse_all_extensions(s,
  2378. SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
  2379. exts, NULL, 0, 1)) {
  2380. /* SSLfatal() already called */
  2381. goto err;
  2382. }
  2383. }
  2384. /*
  2385. * There are two ways to detect a resumed ticket session. One is to set
  2386. * an appropriate session ID and then the server must return a match in
  2387. * ServerHello. This allows the normal client session ID matching to work
  2388. * and we know much earlier that the ticket has been accepted. The
  2389. * other way is to set zero length session ID when the ticket is
  2390. * presented and rely on the handshake to determine session resumption.
  2391. * We choose the former approach because this fits in with assumptions
  2392. * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
  2393. * SHA256 is disabled) hash of the ticket.
  2394. */
  2395. /*
  2396. * TODO(size_t): we use sess_len here because EVP_Digest expects an int
  2397. * but s->session->session_id_length is a size_t
  2398. */
  2399. if (!EVP_Digest(s->session->ext.tick, ticklen,
  2400. s->session->session_id, &sess_len,
  2401. EVP_sha256(), NULL)) {
  2402. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_NEW_SESSION_TICKET,
  2403. ERR_R_EVP_LIB);
  2404. goto err;
  2405. }
  2406. s->session->session_id_length = sess_len;
  2407. s->session->not_resumable = 0;
  2408. /* This is a standalone message in TLSv1.3, so there is no more to read */
  2409. if (SSL_IS_TLS13(s)) {
  2410. const EVP_MD *md = ssl_handshake_md(s);
  2411. int hashleni = EVP_MD_size(md);
  2412. size_t hashlen;
  2413. static const unsigned char nonce_label[] = "resumption";
  2414. /* Ensure cast to size_t is safe */
  2415. if (!ossl_assert(hashleni >= 0)) {
  2416. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2417. SSL_F_TLS_PROCESS_NEW_SESSION_TICKET,
  2418. ERR_R_INTERNAL_ERROR);
  2419. goto err;
  2420. }
  2421. hashlen = (size_t)hashleni;
  2422. if (!tls13_hkdf_expand(s, md, s->resumption_master_secret,
  2423. nonce_label,
  2424. sizeof(nonce_label) - 1,
  2425. PACKET_data(&nonce),
  2426. PACKET_remaining(&nonce),
  2427. s->session->master_key,
  2428. hashlen, 1)) {
  2429. /* SSLfatal() already called */
  2430. goto err;
  2431. }
  2432. s->session->master_key_length = hashlen;
  2433. OPENSSL_free(exts);
  2434. ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
  2435. return MSG_PROCESS_FINISHED_READING;
  2436. }
  2437. return MSG_PROCESS_CONTINUE_READING;
  2438. err:
  2439. OPENSSL_free(exts);
  2440. return MSG_PROCESS_ERROR;
  2441. }
  2442. /*
  2443. * In TLSv1.3 this is called from the extensions code, otherwise it is used to
  2444. * parse a separate message. Returns 1 on success or 0 on failure
  2445. */
  2446. int tls_process_cert_status_body(SSL *s, PACKET *pkt)
  2447. {
  2448. size_t resplen;
  2449. unsigned int type;
  2450. if (!PACKET_get_1(pkt, &type)
  2451. || type != TLSEXT_STATUSTYPE_ocsp) {
  2452. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_STATUS_BODY,
  2453. SSL_R_UNSUPPORTED_STATUS_TYPE);
  2454. return 0;
  2455. }
  2456. if (!PACKET_get_net_3_len(pkt, &resplen)
  2457. || PACKET_remaining(pkt) != resplen) {
  2458. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_STATUS_BODY,
  2459. SSL_R_LENGTH_MISMATCH);
  2460. return 0;
  2461. }
  2462. s->ext.ocsp.resp = OPENSSL_malloc(resplen);
  2463. if (s->ext.ocsp.resp == NULL) {
  2464. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_STATUS_BODY,
  2465. ERR_R_MALLOC_FAILURE);
  2466. return 0;
  2467. }
  2468. if (!PACKET_copy_bytes(pkt, s->ext.ocsp.resp, resplen)) {
  2469. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_STATUS_BODY,
  2470. SSL_R_LENGTH_MISMATCH);
  2471. return 0;
  2472. }
  2473. s->ext.ocsp.resp_len = resplen;
  2474. return 1;
  2475. }
  2476. MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
  2477. {
  2478. if (!tls_process_cert_status_body(s, pkt)) {
  2479. /* SSLfatal() already called */
  2480. return MSG_PROCESS_ERROR;
  2481. }
  2482. return MSG_PROCESS_CONTINUE_READING;
  2483. }
  2484. /*
  2485. * Perform miscellaneous checks and processing after we have received the
  2486. * server's initial flight. In TLS1.3 this is after the Server Finished message.
  2487. * In <=TLS1.2 this is after the ServerDone message. Returns 1 on success or 0
  2488. * on failure.
  2489. */
  2490. int tls_process_initial_server_flight(SSL *s)
  2491. {
  2492. /*
  2493. * at this point we check that we have the required stuff from
  2494. * the server
  2495. */
  2496. if (!ssl3_check_cert_and_algorithm(s)) {
  2497. /* SSLfatal() already called */
  2498. return 0;
  2499. }
  2500. /*
  2501. * Call the ocsp status callback if needed. The |ext.ocsp.resp| and
  2502. * |ext.ocsp.resp_len| values will be set if we actually received a status
  2503. * message, or NULL and -1 otherwise
  2504. */
  2505. if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing
  2506. && s->ctx->ext.status_cb != NULL) {
  2507. int ret = s->ctx->ext.status_cb(s, s->ctx->ext.status_arg);
  2508. if (ret == 0) {
  2509. SSLfatal(s, SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE,
  2510. SSL_F_TLS_PROCESS_INITIAL_SERVER_FLIGHT,
  2511. SSL_R_INVALID_STATUS_RESPONSE);
  2512. return 0;
  2513. }
  2514. if (ret < 0) {
  2515. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2516. SSL_F_TLS_PROCESS_INITIAL_SERVER_FLIGHT,
  2517. ERR_R_MALLOC_FAILURE);
  2518. return 0;
  2519. }
  2520. }
  2521. #ifndef OPENSSL_NO_CT
  2522. if (s->ct_validation_callback != NULL) {
  2523. /* Note we validate the SCTs whether or not we abort on error */
  2524. if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) {
  2525. /* SSLfatal() already called */
  2526. return 0;
  2527. }
  2528. }
  2529. #endif
  2530. return 1;
  2531. }
  2532. MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
  2533. {
  2534. if (PACKET_remaining(pkt) > 0) {
  2535. /* should contain no data */
  2536. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_SERVER_DONE,
  2537. SSL_R_LENGTH_MISMATCH);
  2538. return MSG_PROCESS_ERROR;
  2539. }
  2540. #ifndef OPENSSL_NO_SRP
  2541. if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
  2542. if (SRP_Calc_A_param(s) <= 0) {
  2543. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SERVER_DONE,
  2544. SSL_R_SRP_A_CALC);
  2545. return MSG_PROCESS_ERROR;
  2546. }
  2547. }
  2548. #endif
  2549. if (!tls_process_initial_server_flight(s)) {
  2550. /* SSLfatal() already called */
  2551. return MSG_PROCESS_ERROR;
  2552. }
  2553. return MSG_PROCESS_FINISHED_READING;
  2554. }
  2555. static int tls_construct_cke_psk_preamble(SSL *s, WPACKET *pkt)
  2556. {
  2557. #ifndef OPENSSL_NO_PSK
  2558. int ret = 0;
  2559. /*
  2560. * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
  2561. * \0-terminated identity. The last byte is for us for simulating
  2562. * strnlen.
  2563. */
  2564. char identity[PSK_MAX_IDENTITY_LEN + 1];
  2565. size_t identitylen = 0;
  2566. unsigned char psk[PSK_MAX_PSK_LEN];
  2567. unsigned char *tmppsk = NULL;
  2568. char *tmpidentity = NULL;
  2569. size_t psklen = 0;
  2570. if (s->psk_client_callback == NULL) {
  2571. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
  2572. SSL_R_PSK_NO_CLIENT_CB);
  2573. goto err;
  2574. }
  2575. memset(identity, 0, sizeof(identity));
  2576. psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
  2577. identity, sizeof(identity) - 1,
  2578. psk, sizeof(psk));
  2579. if (psklen > PSK_MAX_PSK_LEN) {
  2580. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  2581. SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
  2582. goto err;
  2583. } else if (psklen == 0) {
  2584. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  2585. SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
  2586. SSL_R_PSK_IDENTITY_NOT_FOUND);
  2587. goto err;
  2588. }
  2589. identitylen = strlen(identity);
  2590. if (identitylen > PSK_MAX_IDENTITY_LEN) {
  2591. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
  2592. ERR_R_INTERNAL_ERROR);
  2593. goto err;
  2594. }
  2595. tmppsk = OPENSSL_memdup(psk, psklen);
  2596. tmpidentity = OPENSSL_strdup(identity);
  2597. if (tmppsk == NULL || tmpidentity == NULL) {
  2598. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
  2599. ERR_R_MALLOC_FAILURE);
  2600. goto err;
  2601. }
  2602. OPENSSL_free(s->s3.tmp.psk);
  2603. s->s3.tmp.psk = tmppsk;
  2604. s->s3.tmp.psklen = psklen;
  2605. tmppsk = NULL;
  2606. OPENSSL_free(s->session->psk_identity);
  2607. s->session->psk_identity = tmpidentity;
  2608. tmpidentity = NULL;
  2609. if (!WPACKET_sub_memcpy_u16(pkt, identity, identitylen)) {
  2610. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
  2611. ERR_R_INTERNAL_ERROR);
  2612. goto err;
  2613. }
  2614. ret = 1;
  2615. err:
  2616. OPENSSL_cleanse(psk, psklen);
  2617. OPENSSL_cleanse(identity, sizeof(identity));
  2618. OPENSSL_clear_free(tmppsk, psklen);
  2619. OPENSSL_clear_free(tmpidentity, identitylen);
  2620. return ret;
  2621. #else
  2622. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
  2623. ERR_R_INTERNAL_ERROR);
  2624. return 0;
  2625. #endif
  2626. }
  2627. static int tls_construct_cke_rsa(SSL *s, WPACKET *pkt)
  2628. {
  2629. #ifndef OPENSSL_NO_RSA
  2630. unsigned char *encdata = NULL;
  2631. EVP_PKEY *pkey = NULL;
  2632. EVP_PKEY_CTX *pctx = NULL;
  2633. size_t enclen;
  2634. unsigned char *pms = NULL;
  2635. size_t pmslen = 0;
  2636. if (s->session->peer == NULL) {
  2637. /*
  2638. * We should always have a server certificate with SSL_kRSA.
  2639. */
  2640. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2641. ERR_R_INTERNAL_ERROR);
  2642. return 0;
  2643. }
  2644. pkey = X509_get0_pubkey(s->session->peer);
  2645. if (EVP_PKEY_get0_RSA(pkey) == NULL) {
  2646. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2647. ERR_R_INTERNAL_ERROR);
  2648. return 0;
  2649. }
  2650. pmslen = SSL_MAX_MASTER_KEY_LENGTH;
  2651. pms = OPENSSL_malloc(pmslen);
  2652. if (pms == NULL) {
  2653. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2654. ERR_R_MALLOC_FAILURE);
  2655. return 0;
  2656. }
  2657. pms[0] = s->client_version >> 8;
  2658. pms[1] = s->client_version & 0xff;
  2659. /* TODO(size_t): Convert this function */
  2660. if (RAND_bytes(pms + 2, (int)(pmslen - 2)) <= 0) {
  2661. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2662. ERR_R_MALLOC_FAILURE);
  2663. goto err;
  2664. }
  2665. /* Fix buf for TLS and beyond */
  2666. if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) {
  2667. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2668. ERR_R_INTERNAL_ERROR);
  2669. goto err;
  2670. }
  2671. pctx = EVP_PKEY_CTX_new(pkey, NULL);
  2672. if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
  2673. || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
  2674. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2675. ERR_R_EVP_LIB);
  2676. goto err;
  2677. }
  2678. if (!WPACKET_allocate_bytes(pkt, enclen, &encdata)
  2679. || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) {
  2680. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2681. SSL_R_BAD_RSA_ENCRYPT);
  2682. goto err;
  2683. }
  2684. EVP_PKEY_CTX_free(pctx);
  2685. pctx = NULL;
  2686. /* Fix buf for TLS and beyond */
  2687. if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) {
  2688. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2689. ERR_R_INTERNAL_ERROR);
  2690. goto err;
  2691. }
  2692. /* Log the premaster secret, if logging is enabled. */
  2693. if (!ssl_log_rsa_client_key_exchange(s, encdata, enclen, pms, pmslen)) {
  2694. /* SSLfatal() already called */
  2695. goto err;
  2696. }
  2697. s->s3.tmp.pms = pms;
  2698. s->s3.tmp.pmslen = pmslen;
  2699. return 1;
  2700. err:
  2701. OPENSSL_clear_free(pms, pmslen);
  2702. EVP_PKEY_CTX_free(pctx);
  2703. return 0;
  2704. #else
  2705. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2706. ERR_R_INTERNAL_ERROR);
  2707. return 0;
  2708. #endif
  2709. }
  2710. static int tls_construct_cke_dhe(SSL *s, WPACKET *pkt)
  2711. {
  2712. #ifndef OPENSSL_NO_DH
  2713. DH *dh_clnt = NULL;
  2714. const BIGNUM *pub_key;
  2715. EVP_PKEY *ckey = NULL, *skey = NULL;
  2716. unsigned char *keybytes = NULL;
  2717. skey = s->s3.peer_tmp;
  2718. if (skey == NULL) {
  2719. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_DHE,
  2720. ERR_R_INTERNAL_ERROR);
  2721. goto err;
  2722. }
  2723. ckey = ssl_generate_pkey(skey);
  2724. if (ckey == NULL) {
  2725. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_DHE,
  2726. ERR_R_INTERNAL_ERROR);
  2727. goto err;
  2728. }
  2729. dh_clnt = EVP_PKEY_get0_DH(ckey);
  2730. if (dh_clnt == NULL) {
  2731. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_DHE,
  2732. ERR_R_INTERNAL_ERROR);
  2733. goto err;
  2734. }
  2735. if (ssl_derive(s, ckey, skey, 0) == 0) {
  2736. /* SSLfatal() already called */
  2737. goto err;
  2738. }
  2739. /* send off the data */
  2740. DH_get0_key(dh_clnt, &pub_key, NULL);
  2741. if (!WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(pub_key),
  2742. &keybytes)) {
  2743. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_DHE,
  2744. ERR_R_INTERNAL_ERROR);
  2745. goto err;
  2746. }
  2747. BN_bn2bin(pub_key, keybytes);
  2748. EVP_PKEY_free(ckey);
  2749. return 1;
  2750. err:
  2751. EVP_PKEY_free(ckey);
  2752. return 0;
  2753. #else
  2754. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_DHE,
  2755. ERR_R_INTERNAL_ERROR);
  2756. return 0;
  2757. #endif
  2758. }
  2759. static int tls_construct_cke_ecdhe(SSL *s, WPACKET *pkt)
  2760. {
  2761. #ifndef OPENSSL_NO_EC
  2762. unsigned char *encodedPoint = NULL;
  2763. size_t encoded_pt_len = 0;
  2764. EVP_PKEY *ckey = NULL, *skey = NULL;
  2765. int ret = 0;
  2766. skey = s->s3.peer_tmp;
  2767. if (skey == NULL) {
  2768. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_ECDHE,
  2769. ERR_R_INTERNAL_ERROR);
  2770. return 0;
  2771. }
  2772. ckey = ssl_generate_pkey(skey);
  2773. if (ckey == NULL) {
  2774. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_ECDHE,
  2775. ERR_R_MALLOC_FAILURE);
  2776. goto err;
  2777. }
  2778. if (ssl_derive(s, ckey, skey, 0) == 0) {
  2779. /* SSLfatal() already called */
  2780. goto err;
  2781. }
  2782. /* Generate encoding of client key */
  2783. encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(ckey, &encodedPoint);
  2784. if (encoded_pt_len == 0) {
  2785. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_ECDHE,
  2786. ERR_R_EC_LIB);
  2787. goto err;
  2788. }
  2789. if (!WPACKET_sub_memcpy_u8(pkt, encodedPoint, encoded_pt_len)) {
  2790. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_ECDHE,
  2791. ERR_R_INTERNAL_ERROR);
  2792. goto err;
  2793. }
  2794. ret = 1;
  2795. err:
  2796. OPENSSL_free(encodedPoint);
  2797. EVP_PKEY_free(ckey);
  2798. return ret;
  2799. #else
  2800. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_ECDHE,
  2801. ERR_R_INTERNAL_ERROR);
  2802. return 0;
  2803. #endif
  2804. }
  2805. static int tls_construct_cke_gost(SSL *s, WPACKET *pkt)
  2806. {
  2807. #ifndef OPENSSL_NO_GOST
  2808. /* GOST key exchange message creation */
  2809. EVP_PKEY_CTX *pkey_ctx = NULL;
  2810. X509 *peer_cert;
  2811. size_t msglen;
  2812. unsigned int md_len;
  2813. unsigned char shared_ukm[32], tmp[256];
  2814. EVP_MD_CTX *ukm_hash = NULL;
  2815. int dgst_nid = NID_id_GostR3411_94;
  2816. unsigned char *pms = NULL;
  2817. size_t pmslen = 0;
  2818. if ((s->s3.tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
  2819. dgst_nid = NID_id_GostR3411_2012_256;
  2820. /*
  2821. * Get server certificate PKEY and create ctx from it
  2822. */
  2823. peer_cert = s->session->peer;
  2824. if (!peer_cert) {
  2825. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2826. SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
  2827. return 0;
  2828. }
  2829. pkey_ctx = EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert), NULL);
  2830. if (pkey_ctx == NULL) {
  2831. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2832. ERR_R_MALLOC_FAILURE);
  2833. return 0;
  2834. }
  2835. /*
  2836. * If we have send a certificate, and certificate key
  2837. * parameters match those of server certificate, use
  2838. * certificate key for key exchange
  2839. */
  2840. /* Otherwise, generate ephemeral key pair */
  2841. pmslen = 32;
  2842. pms = OPENSSL_malloc(pmslen);
  2843. if (pms == NULL) {
  2844. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2845. ERR_R_MALLOC_FAILURE);
  2846. goto err;
  2847. }
  2848. if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0
  2849. /* Generate session key
  2850. * TODO(size_t): Convert this function
  2851. */
  2852. || RAND_bytes(pms, (int)pmslen) <= 0) {
  2853. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2854. ERR_R_INTERNAL_ERROR);
  2855. goto err;
  2856. };
  2857. /*
  2858. * Compute shared IV and store it in algorithm-specific context
  2859. * data
  2860. */
  2861. ukm_hash = EVP_MD_CTX_new();
  2862. if (ukm_hash == NULL
  2863. || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0
  2864. || EVP_DigestUpdate(ukm_hash, s->s3.client_random,
  2865. SSL3_RANDOM_SIZE) <= 0
  2866. || EVP_DigestUpdate(ukm_hash, s->s3.server_random,
  2867. SSL3_RANDOM_SIZE) <= 0
  2868. || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
  2869. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2870. ERR_R_INTERNAL_ERROR);
  2871. goto err;
  2872. }
  2873. EVP_MD_CTX_free(ukm_hash);
  2874. ukm_hash = NULL;
  2875. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
  2876. EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) {
  2877. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2878. SSL_R_LIBRARY_BUG);
  2879. goto err;
  2880. }
  2881. /* Make GOST keytransport blob message */
  2882. /*
  2883. * Encapsulate it into sequence
  2884. */
  2885. msglen = 255;
  2886. if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
  2887. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2888. SSL_R_LIBRARY_BUG);
  2889. goto err;
  2890. }
  2891. if (!WPACKET_put_bytes_u8(pkt, V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)
  2892. || (msglen >= 0x80 && !WPACKET_put_bytes_u8(pkt, 0x81))
  2893. || !WPACKET_sub_memcpy_u8(pkt, tmp, msglen)) {
  2894. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2895. ERR_R_INTERNAL_ERROR);
  2896. goto err;
  2897. }
  2898. EVP_PKEY_CTX_free(pkey_ctx);
  2899. s->s3.tmp.pms = pms;
  2900. s->s3.tmp.pmslen = pmslen;
  2901. return 1;
  2902. err:
  2903. EVP_PKEY_CTX_free(pkey_ctx);
  2904. OPENSSL_clear_free(pms, pmslen);
  2905. EVP_MD_CTX_free(ukm_hash);
  2906. return 0;
  2907. #else
  2908. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2909. ERR_R_INTERNAL_ERROR);
  2910. return 0;
  2911. #endif
  2912. }
  2913. static int tls_construct_cke_srp(SSL *s, WPACKET *pkt)
  2914. {
  2915. #ifndef OPENSSL_NO_SRP
  2916. unsigned char *abytes = NULL;
  2917. if (s->srp_ctx.A == NULL
  2918. || !WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(s->srp_ctx.A),
  2919. &abytes)) {
  2920. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_SRP,
  2921. ERR_R_INTERNAL_ERROR);
  2922. return 0;
  2923. }
  2924. BN_bn2bin(s->srp_ctx.A, abytes);
  2925. OPENSSL_free(s->session->srp_username);
  2926. s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
  2927. if (s->session->srp_username == NULL) {
  2928. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_SRP,
  2929. ERR_R_MALLOC_FAILURE);
  2930. return 0;
  2931. }
  2932. return 1;
  2933. #else
  2934. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_SRP,
  2935. ERR_R_INTERNAL_ERROR);
  2936. return 0;
  2937. #endif
  2938. }
  2939. int tls_construct_client_key_exchange(SSL *s, WPACKET *pkt)
  2940. {
  2941. unsigned long alg_k;
  2942. alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
  2943. /*
  2944. * All of the construct functions below call SSLfatal() if necessary so
  2945. * no need to do so here.
  2946. */
  2947. if ((alg_k & SSL_PSK)
  2948. && !tls_construct_cke_psk_preamble(s, pkt))
  2949. goto err;
  2950. if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
  2951. if (!tls_construct_cke_rsa(s, pkt))
  2952. goto err;
  2953. } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
  2954. if (!tls_construct_cke_dhe(s, pkt))
  2955. goto err;
  2956. } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
  2957. if (!tls_construct_cke_ecdhe(s, pkt))
  2958. goto err;
  2959. } else if (alg_k & SSL_kGOST) {
  2960. if (!tls_construct_cke_gost(s, pkt))
  2961. goto err;
  2962. } else if (alg_k & SSL_kSRP) {
  2963. if (!tls_construct_cke_srp(s, pkt))
  2964. goto err;
  2965. } else if (!(alg_k & SSL_kPSK)) {
  2966. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2967. SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  2968. goto err;
  2969. }
  2970. return 1;
  2971. err:
  2972. OPENSSL_clear_free(s->s3.tmp.pms, s->s3.tmp.pmslen);
  2973. s->s3.tmp.pms = NULL;
  2974. #ifndef OPENSSL_NO_PSK
  2975. OPENSSL_clear_free(s->s3.tmp.psk, s->s3.tmp.psklen);
  2976. s->s3.tmp.psk = NULL;
  2977. #endif
  2978. return 0;
  2979. }
  2980. int tls_client_key_exchange_post_work(SSL *s)
  2981. {
  2982. unsigned char *pms = NULL;
  2983. size_t pmslen = 0;
  2984. pms = s->s3.tmp.pms;
  2985. pmslen = s->s3.tmp.pmslen;
  2986. #ifndef OPENSSL_NO_SRP
  2987. /* Check for SRP */
  2988. if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
  2989. if (!srp_generate_client_master_secret(s)) {
  2990. /* SSLfatal() already called */
  2991. goto err;
  2992. }
  2993. return 1;
  2994. }
  2995. #endif
  2996. if (pms == NULL && !(s->s3.tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
  2997. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2998. SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
  2999. goto err;
  3000. }
  3001. if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
  3002. /* SSLfatal() already called */
  3003. /* ssl_generate_master_secret frees the pms even on error */
  3004. pms = NULL;
  3005. pmslen = 0;
  3006. goto err;
  3007. }
  3008. pms = NULL;
  3009. pmslen = 0;
  3010. #ifndef OPENSSL_NO_SCTP
  3011. if (SSL_IS_DTLS(s)) {
  3012. unsigned char sctpauthkey[64];
  3013. char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
  3014. size_t labellen;
  3015. /*
  3016. * Add new shared key for SCTP-Auth, will be ignored if no SCTP
  3017. * used.
  3018. */
  3019. memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
  3020. sizeof(DTLS1_SCTP_AUTH_LABEL));
  3021. /* Don't include the terminating zero. */
  3022. labellen = sizeof(labelbuffer) - 1;
  3023. if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
  3024. labellen += 1;
  3025. if (SSL_export_keying_material(s, sctpauthkey,
  3026. sizeof(sctpauthkey), labelbuffer,
  3027. labellen, NULL, 0, 0) <= 0) {
  3028. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3029. SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
  3030. ERR_R_INTERNAL_ERROR);
  3031. goto err;
  3032. }
  3033. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  3034. sizeof(sctpauthkey), sctpauthkey);
  3035. }
  3036. #endif
  3037. return 1;
  3038. err:
  3039. OPENSSL_clear_free(pms, pmslen);
  3040. s->s3.tmp.pms = NULL;
  3041. return 0;
  3042. }
  3043. /*
  3044. * Check a certificate can be used for client authentication. Currently check
  3045. * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
  3046. * certificates can be used and optionally checks suitability for Suite B.
  3047. */
  3048. static int ssl3_check_client_certificate(SSL *s)
  3049. {
  3050. /* If no suitable signature algorithm can't use certificate */
  3051. if (!tls_choose_sigalg(s, 0) || s->s3.tmp.sigalg == NULL)
  3052. return 0;
  3053. /*
  3054. * If strict mode check suitability of chain before using it. This also
  3055. * adjusts suite B digest if necessary.
  3056. */
  3057. if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
  3058. !tls1_check_chain(s, NULL, NULL, NULL, -2))
  3059. return 0;
  3060. return 1;
  3061. }
  3062. WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst)
  3063. {
  3064. X509 *x509 = NULL;
  3065. EVP_PKEY *pkey = NULL;
  3066. int i;
  3067. if (wst == WORK_MORE_A) {
  3068. /* Let cert callback update client certificates if required */
  3069. if (s->cert->cert_cb) {
  3070. i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
  3071. if (i < 0) {
  3072. s->rwstate = SSL_X509_LOOKUP;
  3073. return WORK_MORE_A;
  3074. }
  3075. if (i == 0) {
  3076. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3077. SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
  3078. SSL_R_CALLBACK_FAILED);
  3079. return WORK_ERROR;
  3080. }
  3081. s->rwstate = SSL_NOTHING;
  3082. }
  3083. if (ssl3_check_client_certificate(s)) {
  3084. if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
  3085. return WORK_FINISHED_STOP;
  3086. }
  3087. return WORK_FINISHED_CONTINUE;
  3088. }
  3089. /* Fall through to WORK_MORE_B */
  3090. wst = WORK_MORE_B;
  3091. }
  3092. /* We need to get a client cert */
  3093. if (wst == WORK_MORE_B) {
  3094. /*
  3095. * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
  3096. * return(-1); We then get retied later
  3097. */
  3098. i = ssl_do_client_cert_cb(s, &x509, &pkey);
  3099. if (i < 0) {
  3100. s->rwstate = SSL_X509_LOOKUP;
  3101. return WORK_MORE_B;
  3102. }
  3103. s->rwstate = SSL_NOTHING;
  3104. if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
  3105. if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
  3106. i = 0;
  3107. } else if (i == 1) {
  3108. i = 0;
  3109. SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
  3110. SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  3111. }
  3112. X509_free(x509);
  3113. EVP_PKEY_free(pkey);
  3114. if (i && !ssl3_check_client_certificate(s))
  3115. i = 0;
  3116. if (i == 0) {
  3117. if (s->version == SSL3_VERSION) {
  3118. s->s3.tmp.cert_req = 0;
  3119. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
  3120. return WORK_FINISHED_CONTINUE;
  3121. } else {
  3122. s->s3.tmp.cert_req = 2;
  3123. if (!ssl3_digest_cached_records(s, 0)) {
  3124. /* SSLfatal() already called */
  3125. return WORK_ERROR;
  3126. }
  3127. }
  3128. }
  3129. if (s->post_handshake_auth == SSL_PHA_REQUESTED)
  3130. return WORK_FINISHED_STOP;
  3131. return WORK_FINISHED_CONTINUE;
  3132. }
  3133. /* Shouldn't ever get here */
  3134. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
  3135. ERR_R_INTERNAL_ERROR);
  3136. return WORK_ERROR;
  3137. }
  3138. int tls_construct_client_certificate(SSL *s, WPACKET *pkt)
  3139. {
  3140. if (SSL_IS_TLS13(s)) {
  3141. if (s->pha_context == NULL) {
  3142. /* no context available, add 0-length context */
  3143. if (!WPACKET_put_bytes_u8(pkt, 0)) {
  3144. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3145. SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
  3146. return 0;
  3147. }
  3148. } else if (!WPACKET_sub_memcpy_u8(pkt, s->pha_context, s->pha_context_len)) {
  3149. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3150. SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
  3151. return 0;
  3152. }
  3153. }
  3154. if (!ssl3_output_cert_chain(s, pkt,
  3155. (s->s3.tmp.cert_req == 2) ? NULL
  3156. : s->cert->key)) {
  3157. /* SSLfatal() already called */
  3158. return 0;
  3159. }
  3160. if (SSL_IS_TLS13(s)
  3161. && SSL_IS_FIRST_HANDSHAKE(s)
  3162. && (!s->method->ssl3_enc->change_cipher_state(s,
  3163. SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) {
  3164. /*
  3165. * This is a fatal error, which leaves enc_write_ctx in an inconsistent
  3166. * state and thus ssl3_send_alert may crash.
  3167. */
  3168. SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE,
  3169. SSL_R_CANNOT_CHANGE_CIPHER);
  3170. return 0;
  3171. }
  3172. return 1;
  3173. }
  3174. int ssl3_check_cert_and_algorithm(SSL *s)
  3175. {
  3176. const SSL_CERT_LOOKUP *clu;
  3177. size_t idx;
  3178. long alg_k, alg_a;
  3179. alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
  3180. alg_a = s->s3.tmp.new_cipher->algorithm_auth;
  3181. /* we don't have a certificate */
  3182. if (!(alg_a & SSL_aCERT))
  3183. return 1;
  3184. /* This is the passed certificate */
  3185. clu = ssl_cert_lookup_by_pkey(X509_get0_pubkey(s->session->peer), &idx);
  3186. /* Check certificate is recognised and suitable for cipher */
  3187. if (clu == NULL || (alg_a & clu->amask) == 0) {
  3188. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3189. SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3190. SSL_R_MISSING_SIGNING_CERT);
  3191. return 0;
  3192. }
  3193. #ifndef OPENSSL_NO_EC
  3194. if (clu->amask & SSL_aECDSA) {
  3195. if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s))
  3196. return 1;
  3197. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3198. SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
  3199. return 0;
  3200. }
  3201. #endif
  3202. #ifndef OPENSSL_NO_RSA
  3203. if (alg_k & (SSL_kRSA | SSL_kRSAPSK) && idx != SSL_PKEY_RSA) {
  3204. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3205. SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3206. SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  3207. return 0;
  3208. }
  3209. #endif
  3210. #ifndef OPENSSL_NO_DH
  3211. if ((alg_k & SSL_kDHE) && (s->s3.peer_tmp == NULL)) {
  3212. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3213. ERR_R_INTERNAL_ERROR);
  3214. return 0;
  3215. }
  3216. #endif
  3217. return 1;
  3218. }
  3219. #ifndef OPENSSL_NO_NEXTPROTONEG
  3220. int tls_construct_next_proto(SSL *s, WPACKET *pkt)
  3221. {
  3222. size_t len, padding_len;
  3223. unsigned char *padding = NULL;
  3224. len = s->ext.npn_len;
  3225. padding_len = 32 - ((len + 2) % 32);
  3226. if (!WPACKET_sub_memcpy_u8(pkt, s->ext.npn, len)
  3227. || !WPACKET_sub_allocate_bytes_u8(pkt, padding_len, &padding)) {
  3228. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_NEXT_PROTO,
  3229. ERR_R_INTERNAL_ERROR);
  3230. return 0;
  3231. }
  3232. memset(padding, 0, padding_len);
  3233. return 1;
  3234. }
  3235. #endif
  3236. MSG_PROCESS_RETURN tls_process_hello_req(SSL *s, PACKET *pkt)
  3237. {
  3238. if (PACKET_remaining(pkt) > 0) {
  3239. /* should contain no data */
  3240. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_HELLO_REQ,
  3241. SSL_R_LENGTH_MISMATCH);
  3242. return MSG_PROCESS_ERROR;
  3243. }
  3244. if ((s->options & SSL_OP_NO_RENEGOTIATION)) {
  3245. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
  3246. return MSG_PROCESS_FINISHED_READING;
  3247. }
  3248. /*
  3249. * This is a historical discrepancy (not in the RFC) maintained for
  3250. * compatibility reasons. If a TLS client receives a HelloRequest it will
  3251. * attempt an abbreviated handshake. However if a DTLS client receives a
  3252. * HelloRequest it will do a full handshake. Either behaviour is reasonable
  3253. * but doing one for TLS and another for DTLS is odd.
  3254. */
  3255. if (SSL_IS_DTLS(s))
  3256. SSL_renegotiate(s);
  3257. else
  3258. SSL_renegotiate_abbreviated(s);
  3259. return MSG_PROCESS_FINISHED_READING;
  3260. }
  3261. static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL *s, PACKET *pkt)
  3262. {
  3263. PACKET extensions;
  3264. RAW_EXTENSION *rawexts = NULL;
  3265. if (!PACKET_as_length_prefixed_2(pkt, &extensions)
  3266. || PACKET_remaining(pkt) != 0) {
  3267. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_ENCRYPTED_EXTENSIONS,
  3268. SSL_R_LENGTH_MISMATCH);
  3269. goto err;
  3270. }
  3271. if (!tls_collect_extensions(s, &extensions,
  3272. SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, &rawexts,
  3273. NULL, 1)
  3274. || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
  3275. rawexts, NULL, 0, 1)) {
  3276. /* SSLfatal() already called */
  3277. goto err;
  3278. }
  3279. OPENSSL_free(rawexts);
  3280. return MSG_PROCESS_CONTINUE_READING;
  3281. err:
  3282. OPENSSL_free(rawexts);
  3283. return MSG_PROCESS_ERROR;
  3284. }
  3285. int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
  3286. {
  3287. int i = 0;
  3288. #ifndef OPENSSL_NO_ENGINE
  3289. if (s->ctx->client_cert_engine) {
  3290. i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
  3291. SSL_get_client_CA_list(s),
  3292. px509, ppkey, NULL, NULL, NULL);
  3293. if (i != 0)
  3294. return i;
  3295. }
  3296. #endif
  3297. if (s->ctx->client_cert_cb)
  3298. i = s->ctx->client_cert_cb(s, px509, ppkey);
  3299. return i;
  3300. }
  3301. int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, WPACKET *pkt)
  3302. {
  3303. int i;
  3304. size_t totlen = 0, len, maxlen, maxverok = 0;
  3305. int empty_reneg_info_scsv = !s->renegotiate;
  3306. /* Set disabled masks for this session */
  3307. if (!ssl_set_client_disabled(s)) {
  3308. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_CIPHER_LIST_TO_BYTES,
  3309. SSL_R_NO_PROTOCOLS_AVAILABLE);
  3310. return 0;
  3311. }
  3312. if (sk == NULL) {
  3313. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_CIPHER_LIST_TO_BYTES,
  3314. ERR_R_INTERNAL_ERROR);
  3315. return 0;
  3316. }
  3317. #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
  3318. # if OPENSSL_MAX_TLS1_2_CIPHER_LENGTH < 6
  3319. # error Max cipher length too short
  3320. # endif
  3321. /*
  3322. * Some servers hang if client hello > 256 bytes as hack workaround
  3323. * chop number of supported ciphers to keep it well below this if we
  3324. * use TLS v1.2
  3325. */
  3326. if (TLS1_get_version(s) >= TLS1_2_VERSION)
  3327. maxlen = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
  3328. else
  3329. #endif
  3330. /* Maximum length that can be stored in 2 bytes. Length must be even */
  3331. maxlen = 0xfffe;
  3332. if (empty_reneg_info_scsv)
  3333. maxlen -= 2;
  3334. if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV)
  3335. maxlen -= 2;
  3336. for (i = 0; i < sk_SSL_CIPHER_num(sk) && totlen < maxlen; i++) {
  3337. const SSL_CIPHER *c;
  3338. c = sk_SSL_CIPHER_value(sk, i);
  3339. /* Skip disabled ciphers */
  3340. if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0))
  3341. continue;
  3342. if (!s->method->put_cipher_by_char(c, pkt, &len)) {
  3343. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_CIPHER_LIST_TO_BYTES,
  3344. ERR_R_INTERNAL_ERROR);
  3345. return 0;
  3346. }
  3347. /* Sanity check that the maximum version we offer has ciphers enabled */
  3348. if (!maxverok) {
  3349. if (SSL_IS_DTLS(s)) {
  3350. if (DTLS_VERSION_GE(c->max_dtls, s->s3.tmp.max_ver)
  3351. && DTLS_VERSION_LE(c->min_dtls, s->s3.tmp.max_ver))
  3352. maxverok = 1;
  3353. } else {
  3354. if (c->max_tls >= s->s3.tmp.max_ver
  3355. && c->min_tls <= s->s3.tmp.max_ver)
  3356. maxverok = 1;
  3357. }
  3358. }
  3359. totlen += len;
  3360. }
  3361. if (totlen == 0 || !maxverok) {
  3362. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_CIPHER_LIST_TO_BYTES,
  3363. SSL_R_NO_CIPHERS_AVAILABLE);
  3364. if (!maxverok)
  3365. ERR_add_error_data(1, "No ciphers enabled for max supported "
  3366. "SSL/TLS version");
  3367. return 0;
  3368. }
  3369. if (totlen != 0) {
  3370. if (empty_reneg_info_scsv) {
  3371. static SSL_CIPHER scsv = {
  3372. 0, NULL, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
  3373. };
  3374. if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) {
  3375. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3376. SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR);
  3377. return 0;
  3378. }
  3379. }
  3380. if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
  3381. static SSL_CIPHER scsv = {
  3382. 0, NULL, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
  3383. };
  3384. if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) {
  3385. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3386. SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR);
  3387. return 0;
  3388. }
  3389. }
  3390. }
  3391. return 1;
  3392. }
  3393. int tls_construct_end_of_early_data(SSL *s, WPACKET *pkt)
  3394. {
  3395. if (s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY
  3396. && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING) {
  3397. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3398. SSL_F_TLS_CONSTRUCT_END_OF_EARLY_DATA,
  3399. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  3400. return 0;
  3401. }
  3402. s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;
  3403. return 1;
  3404. }