1
0

statem_clnt.c 134 KB

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