statem_clnt.c 134 KB

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