statem_srvr.c 143 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339
  1. /*
  2. * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. * Copyright 2005 Nokia. All rights reserved.
  5. *
  6. * Licensed under the OpenSSL license (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. #include <stdio.h>
  12. #include "../ssl_local.h"
  13. #include "statem_local.h"
  14. #include "internal/constant_time.h"
  15. #include "internal/cryptlib.h"
  16. #include <openssl/buffer.h>
  17. #include <openssl/rand.h>
  18. #include <openssl/objects.h>
  19. #include <openssl/evp.h>
  20. #include <openssl/hmac.h>
  21. #include <openssl/x509.h>
  22. #include <openssl/dh.h>
  23. #include <openssl/bn.h>
  24. #include <openssl/md5.h>
  25. #include <openssl/asn1t.h>
  26. #define TICKET_NONCE_SIZE 8
  27. typedef struct {
  28. ASN1_TYPE *kxBlob;
  29. ASN1_TYPE *opaqueBlob;
  30. } GOST_KX_MESSAGE;
  31. DECLARE_ASN1_FUNCTIONS(GOST_KX_MESSAGE)
  32. ASN1_SEQUENCE(GOST_KX_MESSAGE) = {
  33. ASN1_SIMPLE(GOST_KX_MESSAGE, kxBlob, ASN1_ANY),
  34. ASN1_OPT(GOST_KX_MESSAGE, opaqueBlob, ASN1_ANY),
  35. } ASN1_SEQUENCE_END(GOST_KX_MESSAGE)
  36. IMPLEMENT_ASN1_FUNCTIONS(GOST_KX_MESSAGE)
  37. static int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt);
  38. /*
  39. * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
  40. * handshake state transitions when a TLSv1.3 server is reading messages from
  41. * the client. The message type that the client has sent is provided in |mt|.
  42. * The current state is in |s->statem.hand_state|.
  43. *
  44. * Return values are 1 for success (transition allowed) and 0 on error
  45. * (transition not allowed)
  46. */
  47. static int ossl_statem_server13_read_transition(SSL *s, int mt)
  48. {
  49. OSSL_STATEM *st = &s->statem;
  50. /*
  51. * Note: There is no case for TLS_ST_BEFORE because at that stage we have
  52. * not negotiated TLSv1.3 yet, so that case is handled by
  53. * ossl_statem_server_read_transition()
  54. */
  55. switch (st->hand_state) {
  56. default:
  57. break;
  58. case TLS_ST_EARLY_DATA:
  59. if (s->hello_retry_request == SSL_HRR_PENDING) {
  60. if (mt == SSL3_MT_CLIENT_HELLO) {
  61. st->hand_state = TLS_ST_SR_CLNT_HELLO;
  62. return 1;
  63. }
  64. break;
  65. } else if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED
  66. && !SSL_IS_QUIC(s)) {
  67. if (mt == SSL3_MT_END_OF_EARLY_DATA) {
  68. st->hand_state = TLS_ST_SR_END_OF_EARLY_DATA;
  69. return 1;
  70. }
  71. break;
  72. }
  73. /* Fall through */
  74. case TLS_ST_SR_END_OF_EARLY_DATA:
  75. case TLS_ST_SW_FINISHED:
  76. if (s->s3->tmp.cert_request) {
  77. if (mt == SSL3_MT_CERTIFICATE) {
  78. st->hand_state = TLS_ST_SR_CERT;
  79. return 1;
  80. }
  81. } else {
  82. if (mt == SSL3_MT_FINISHED) {
  83. st->hand_state = TLS_ST_SR_FINISHED;
  84. return 1;
  85. }
  86. }
  87. break;
  88. case TLS_ST_SR_CERT:
  89. if (s->session->peer == NULL) {
  90. if (mt == SSL3_MT_FINISHED) {
  91. st->hand_state = TLS_ST_SR_FINISHED;
  92. return 1;
  93. }
  94. } else {
  95. if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
  96. st->hand_state = TLS_ST_SR_CERT_VRFY;
  97. return 1;
  98. }
  99. }
  100. break;
  101. case TLS_ST_SR_CERT_VRFY:
  102. if (mt == SSL3_MT_FINISHED) {
  103. st->hand_state = TLS_ST_SR_FINISHED;
  104. return 1;
  105. }
  106. break;
  107. case TLS_ST_OK:
  108. /*
  109. * Its never ok to start processing handshake messages in the middle of
  110. * early data (i.e. before we've received the end of early data alert)
  111. */
  112. if (s->early_data_state == SSL_EARLY_DATA_READING)
  113. break;
  114. if (mt == SSL3_MT_CERTIFICATE
  115. && s->post_handshake_auth == SSL_PHA_REQUESTED) {
  116. st->hand_state = TLS_ST_SR_CERT;
  117. return 1;
  118. }
  119. if (mt == SSL3_MT_KEY_UPDATE) {
  120. st->hand_state = TLS_ST_SR_KEY_UPDATE;
  121. return 1;
  122. }
  123. break;
  124. }
  125. /* No valid transition found */
  126. return 0;
  127. }
  128. /*
  129. * ossl_statem_server_read_transition() encapsulates the logic for the allowed
  130. * handshake state transitions when the server is reading messages from the
  131. * client. The message type that the client has sent is provided in |mt|. The
  132. * current state is in |s->statem.hand_state|.
  133. *
  134. * Return values are 1 for success (transition allowed) and 0 on error
  135. * (transition not allowed)
  136. */
  137. int ossl_statem_server_read_transition(SSL *s, int mt)
  138. {
  139. OSSL_STATEM *st = &s->statem;
  140. if (SSL_IS_TLS13(s)) {
  141. if (!ossl_statem_server13_read_transition(s, mt))
  142. goto err;
  143. return 1;
  144. }
  145. switch (st->hand_state) {
  146. default:
  147. break;
  148. case TLS_ST_BEFORE:
  149. case TLS_ST_OK:
  150. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  151. if (mt == SSL3_MT_CLIENT_HELLO) {
  152. st->hand_state = TLS_ST_SR_CLNT_HELLO;
  153. return 1;
  154. }
  155. break;
  156. case TLS_ST_SW_SRVR_DONE:
  157. /*
  158. * If we get a CKE message after a ServerDone then either
  159. * 1) We didn't request a Certificate
  160. * OR
  161. * 2) If we did request one then
  162. * a) We allow no Certificate to be returned
  163. * AND
  164. * b) We are running SSL3 (in TLS1.0+ the client must return a 0
  165. * list if we requested a certificate)
  166. */
  167. if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
  168. if (s->s3->tmp.cert_request) {
  169. if (s->version == SSL3_VERSION) {
  170. if ((s->verify_mode & SSL_VERIFY_PEER)
  171. && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
  172. /*
  173. * This isn't an unexpected message as such - we're just
  174. * not going to accept it because we require a client
  175. * cert.
  176. */
  177. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  178. SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION,
  179. SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  180. return 0;
  181. }
  182. st->hand_state = TLS_ST_SR_KEY_EXCH;
  183. return 1;
  184. }
  185. } else {
  186. st->hand_state = TLS_ST_SR_KEY_EXCH;
  187. return 1;
  188. }
  189. } else if (s->s3->tmp.cert_request) {
  190. if (mt == SSL3_MT_CERTIFICATE) {
  191. st->hand_state = TLS_ST_SR_CERT;
  192. return 1;
  193. }
  194. }
  195. break;
  196. case TLS_ST_SR_CERT:
  197. if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
  198. st->hand_state = TLS_ST_SR_KEY_EXCH;
  199. return 1;
  200. }
  201. break;
  202. case TLS_ST_SR_KEY_EXCH:
  203. /*
  204. * We should only process a CertificateVerify message if we have
  205. * received a Certificate from the client. If so then |s->session->peer|
  206. * will be non NULL. In some instances a CertificateVerify message is
  207. * not required even if the peer has sent a Certificate (e.g. such as in
  208. * the case of static DH). In that case |st->no_cert_verify| should be
  209. * set.
  210. */
  211. if (s->session->peer == NULL || st->no_cert_verify) {
  212. if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  213. /*
  214. * For the ECDH ciphersuites when the client sends its ECDH
  215. * pub key in a certificate, the CertificateVerify message is
  216. * not sent. Also for GOST ciphersuites when the client uses
  217. * its key from the certificate for key exchange.
  218. */
  219. st->hand_state = TLS_ST_SR_CHANGE;
  220. return 1;
  221. }
  222. } else {
  223. if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
  224. st->hand_state = TLS_ST_SR_CERT_VRFY;
  225. return 1;
  226. }
  227. }
  228. break;
  229. case TLS_ST_SR_CERT_VRFY:
  230. if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  231. st->hand_state = TLS_ST_SR_CHANGE;
  232. return 1;
  233. }
  234. break;
  235. case TLS_ST_SR_CHANGE:
  236. #ifndef OPENSSL_NO_NEXTPROTONEG
  237. if (s->s3->npn_seen) {
  238. if (mt == SSL3_MT_NEXT_PROTO) {
  239. st->hand_state = TLS_ST_SR_NEXT_PROTO;
  240. return 1;
  241. }
  242. } else {
  243. #endif
  244. if (mt == SSL3_MT_FINISHED) {
  245. st->hand_state = TLS_ST_SR_FINISHED;
  246. return 1;
  247. }
  248. #ifndef OPENSSL_NO_NEXTPROTONEG
  249. }
  250. #endif
  251. break;
  252. #ifndef OPENSSL_NO_NEXTPROTONEG
  253. case TLS_ST_SR_NEXT_PROTO:
  254. if (mt == SSL3_MT_FINISHED) {
  255. st->hand_state = TLS_ST_SR_FINISHED;
  256. return 1;
  257. }
  258. break;
  259. #endif
  260. case TLS_ST_SW_FINISHED:
  261. if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  262. st->hand_state = TLS_ST_SR_CHANGE;
  263. return 1;
  264. }
  265. break;
  266. }
  267. err:
  268. /* No valid transition found */
  269. if (SSL_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  270. BIO *rbio;
  271. /*
  272. * CCS messages don't have a message sequence number so this is probably
  273. * because of an out-of-order CCS. We'll just drop it.
  274. */
  275. s->init_num = 0;
  276. s->rwstate = SSL_READING;
  277. rbio = SSL_get_rbio(s);
  278. BIO_clear_retry_flags(rbio);
  279. BIO_set_retry_read(rbio);
  280. return 0;
  281. }
  282. SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE,
  283. SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION,
  284. SSL_R_UNEXPECTED_MESSAGE);
  285. return 0;
  286. }
  287. /*
  288. * Should we send a ServerKeyExchange message?
  289. *
  290. * Valid return values are:
  291. * 1: Yes
  292. * 0: No
  293. */
  294. static int send_server_key_exchange(SSL *s)
  295. {
  296. unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  297. /*
  298. * only send a ServerKeyExchange if DH or fortezza but we have a
  299. * sign only certificate PSK: may send PSK identity hints For
  300. * ECC ciphersuites, we send a serverKeyExchange message only if
  301. * the cipher suite is either ECDH-anon or ECDHE. In other cases,
  302. * the server certificate contains the server's public key for
  303. * key exchange.
  304. */
  305. if (alg_k & (SSL_kDHE | SSL_kECDHE)
  306. /*
  307. * PSK: send ServerKeyExchange if PSK identity hint if
  308. * provided
  309. */
  310. #ifndef OPENSSL_NO_PSK
  311. /* Only send SKE if we have identity hint for plain PSK */
  312. || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
  313. && s->cert->psk_identity_hint)
  314. /* For other PSK always send SKE */
  315. || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
  316. #endif
  317. #ifndef OPENSSL_NO_SRP
  318. /* SRP: send ServerKeyExchange */
  319. || (alg_k & SSL_kSRP)
  320. #endif
  321. ) {
  322. return 1;
  323. }
  324. return 0;
  325. }
  326. /*
  327. * Should we send a CertificateRequest message?
  328. *
  329. * Valid return values are:
  330. * 1: Yes
  331. * 0: No
  332. */
  333. int send_certificate_request(SSL *s)
  334. {
  335. if (
  336. /* don't request cert unless asked for it: */
  337. s->verify_mode & SSL_VERIFY_PEER
  338. /*
  339. * don't request if post-handshake-only unless doing
  340. * post-handshake in TLSv1.3:
  341. */
  342. && (!SSL_IS_TLS13(s) || !(s->verify_mode & SSL_VERIFY_POST_HANDSHAKE)
  343. || s->post_handshake_auth == SSL_PHA_REQUEST_PENDING)
  344. /*
  345. * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
  346. * a second time:
  347. */
  348. && (s->certreqs_sent < 1 ||
  349. !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))
  350. /*
  351. * never request cert in anonymous ciphersuites (see
  352. * section "Certificate request" in SSL 3 drafts and in
  353. * RFC 2246):
  354. */
  355. && (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
  356. /*
  357. * ... except when the application insists on
  358. * verification (against the specs, but statem_clnt.c accepts
  359. * this for SSL 3)
  360. */
  361. || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  362. /* don't request certificate for SRP auth */
  363. && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
  364. /*
  365. * With normal PSK Certificates and Certificate Requests
  366. * are omitted
  367. */
  368. && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)) {
  369. return 1;
  370. }
  371. return 0;
  372. }
  373. /*
  374. * ossl_statem_server13_write_transition() works out what handshake state to
  375. * move to next when a TLSv1.3 server is writing messages to be sent to the
  376. * client.
  377. */
  378. static WRITE_TRAN ossl_statem_server13_write_transition(SSL *s)
  379. {
  380. OSSL_STATEM *st = &s->statem;
  381. /*
  382. * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
  383. * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
  384. */
  385. switch (st->hand_state) {
  386. default:
  387. /* Shouldn't happen */
  388. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  389. SSL_F_OSSL_STATEM_SERVER13_WRITE_TRANSITION,
  390. ERR_R_INTERNAL_ERROR);
  391. return WRITE_TRAN_ERROR;
  392. case TLS_ST_OK:
  393. if (s->key_update != SSL_KEY_UPDATE_NONE) {
  394. st->hand_state = TLS_ST_SW_KEY_UPDATE;
  395. return WRITE_TRAN_CONTINUE;
  396. }
  397. if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
  398. st->hand_state = TLS_ST_SW_CERT_REQ;
  399. return WRITE_TRAN_CONTINUE;
  400. }
  401. if (s->ext.extra_tickets_expected > 0) {
  402. st->hand_state = TLS_ST_SW_SESSION_TICKET;
  403. return WRITE_TRAN_CONTINUE;
  404. }
  405. /* Try to read from the client instead */
  406. return WRITE_TRAN_FINISHED;
  407. case TLS_ST_SR_CLNT_HELLO:
  408. st->hand_state = TLS_ST_SW_SRVR_HELLO;
  409. return WRITE_TRAN_CONTINUE;
  410. case TLS_ST_SW_SRVR_HELLO:
  411. if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
  412. && s->hello_retry_request != SSL_HRR_COMPLETE)
  413. st->hand_state = TLS_ST_SW_CHANGE;
  414. else if (s->hello_retry_request == SSL_HRR_PENDING)
  415. st->hand_state = TLS_ST_EARLY_DATA;
  416. else
  417. st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
  418. return WRITE_TRAN_CONTINUE;
  419. case TLS_ST_SW_CHANGE:
  420. if (s->hello_retry_request == SSL_HRR_PENDING)
  421. st->hand_state = TLS_ST_EARLY_DATA;
  422. else
  423. st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
  424. return WRITE_TRAN_CONTINUE;
  425. case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
  426. if (s->hit)
  427. st->hand_state = TLS_ST_SW_FINISHED;
  428. else if (send_certificate_request(s))
  429. st->hand_state = TLS_ST_SW_CERT_REQ;
  430. else
  431. st->hand_state = TLS_ST_SW_CERT;
  432. return WRITE_TRAN_CONTINUE;
  433. case TLS_ST_SW_CERT_REQ:
  434. if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
  435. s->post_handshake_auth = SSL_PHA_REQUESTED;
  436. st->hand_state = TLS_ST_OK;
  437. } else {
  438. st->hand_state = TLS_ST_SW_CERT;
  439. }
  440. return WRITE_TRAN_CONTINUE;
  441. case TLS_ST_SW_CERT:
  442. st->hand_state = TLS_ST_SW_CERT_VRFY;
  443. return WRITE_TRAN_CONTINUE;
  444. case TLS_ST_SW_CERT_VRFY:
  445. st->hand_state = TLS_ST_SW_FINISHED;
  446. return WRITE_TRAN_CONTINUE;
  447. case TLS_ST_SW_FINISHED:
  448. st->hand_state = TLS_ST_EARLY_DATA;
  449. return WRITE_TRAN_CONTINUE;
  450. case TLS_ST_EARLY_DATA:
  451. return WRITE_TRAN_FINISHED;
  452. case TLS_ST_SR_FINISHED:
  453. /*
  454. * Technically we have finished the handshake at this point, but we're
  455. * going to remain "in_init" for now and write out any session tickets
  456. * immediately.
  457. */
  458. if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
  459. s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;
  460. } else if (!s->ext.ticket_expected) {
  461. /*
  462. * If we're not going to renew the ticket then we just finish the
  463. * handshake at this point.
  464. */
  465. st->hand_state = TLS_ST_OK;
  466. return WRITE_TRAN_CONTINUE;
  467. }
  468. if (s->num_tickets > s->sent_tickets)
  469. st->hand_state = TLS_ST_SW_SESSION_TICKET;
  470. else
  471. st->hand_state = TLS_ST_OK;
  472. return WRITE_TRAN_CONTINUE;
  473. case TLS_ST_SR_KEY_UPDATE:
  474. case TLS_ST_SW_KEY_UPDATE:
  475. st->hand_state = TLS_ST_OK;
  476. return WRITE_TRAN_CONTINUE;
  477. case TLS_ST_SW_SESSION_TICKET:
  478. /* In a resumption we only ever send a maximum of one new ticket.
  479. * Following an initial handshake we send the number of tickets we have
  480. * been configured for.
  481. */
  482. if (!SSL_IS_FIRST_HANDSHAKE(s) && s->ext.extra_tickets_expected > 0) {
  483. return WRITE_TRAN_CONTINUE;
  484. } else if (s->hit || s->num_tickets <= s->sent_tickets) {
  485. /* We've written enough tickets out. */
  486. st->hand_state = TLS_ST_OK;
  487. }
  488. return WRITE_TRAN_CONTINUE;
  489. }
  490. }
  491. /*
  492. * ossl_statem_server_write_transition() works out what handshake state to move
  493. * to next when the server is writing messages to be sent to the client.
  494. */
  495. WRITE_TRAN ossl_statem_server_write_transition(SSL *s)
  496. {
  497. OSSL_STATEM *st = &s->statem;
  498. /*
  499. * Note that before the ClientHello we don't know what version we are going
  500. * to negotiate yet, so we don't take this branch until later
  501. */
  502. if (SSL_IS_TLS13(s))
  503. return ossl_statem_server13_write_transition(s);
  504. switch (st->hand_state) {
  505. default:
  506. /* Shouldn't happen */
  507. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  508. SSL_F_OSSL_STATEM_SERVER_WRITE_TRANSITION,
  509. ERR_R_INTERNAL_ERROR);
  510. return WRITE_TRAN_ERROR;
  511. case TLS_ST_OK:
  512. if (st->request_state == TLS_ST_SW_HELLO_REQ) {
  513. /* We must be trying to renegotiate */
  514. st->hand_state = TLS_ST_SW_HELLO_REQ;
  515. st->request_state = TLS_ST_BEFORE;
  516. return WRITE_TRAN_CONTINUE;
  517. }
  518. /* Must be an incoming ClientHello */
  519. if (!tls_setup_handshake(s)) {
  520. /* SSLfatal() already called */
  521. return WRITE_TRAN_ERROR;
  522. }
  523. /* Fall through */
  524. case TLS_ST_BEFORE:
  525. /* Just go straight to trying to read from the client */
  526. return WRITE_TRAN_FINISHED;
  527. case TLS_ST_SW_HELLO_REQ:
  528. st->hand_state = TLS_ST_OK;
  529. return WRITE_TRAN_CONTINUE;
  530. case TLS_ST_SR_CLNT_HELLO:
  531. if (SSL_IS_DTLS(s) && !s->d1->cookie_verified
  532. && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) {
  533. st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;
  534. } else if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
  535. /* We must have rejected the renegotiation */
  536. st->hand_state = TLS_ST_OK;
  537. return WRITE_TRAN_CONTINUE;
  538. } else {
  539. st->hand_state = TLS_ST_SW_SRVR_HELLO;
  540. }
  541. return WRITE_TRAN_CONTINUE;
  542. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  543. return WRITE_TRAN_FINISHED;
  544. case TLS_ST_SW_SRVR_HELLO:
  545. if (s->hit) {
  546. if (s->ext.ticket_expected)
  547. st->hand_state = TLS_ST_SW_SESSION_TICKET;
  548. else
  549. st->hand_state = TLS_ST_SW_CHANGE;
  550. } else {
  551. /* Check if it is anon DH or anon ECDH, */
  552. /* normal PSK or SRP */
  553. if (!(s->s3->tmp.new_cipher->algorithm_auth &
  554. (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
  555. st->hand_state = TLS_ST_SW_CERT;
  556. } else if (send_server_key_exchange(s)) {
  557. st->hand_state = TLS_ST_SW_KEY_EXCH;
  558. } else if (send_certificate_request(s)) {
  559. st->hand_state = TLS_ST_SW_CERT_REQ;
  560. } else {
  561. st->hand_state = TLS_ST_SW_SRVR_DONE;
  562. }
  563. }
  564. return WRITE_TRAN_CONTINUE;
  565. case TLS_ST_SW_CERT:
  566. if (s->ext.status_expected) {
  567. st->hand_state = TLS_ST_SW_CERT_STATUS;
  568. return WRITE_TRAN_CONTINUE;
  569. }
  570. /* Fall through */
  571. case TLS_ST_SW_CERT_STATUS:
  572. if (send_server_key_exchange(s)) {
  573. st->hand_state = TLS_ST_SW_KEY_EXCH;
  574. return WRITE_TRAN_CONTINUE;
  575. }
  576. /* Fall through */
  577. case TLS_ST_SW_KEY_EXCH:
  578. if (send_certificate_request(s)) {
  579. st->hand_state = TLS_ST_SW_CERT_REQ;
  580. return WRITE_TRAN_CONTINUE;
  581. }
  582. /* Fall through */
  583. case TLS_ST_SW_CERT_REQ:
  584. st->hand_state = TLS_ST_SW_SRVR_DONE;
  585. return WRITE_TRAN_CONTINUE;
  586. case TLS_ST_SW_SRVR_DONE:
  587. return WRITE_TRAN_FINISHED;
  588. case TLS_ST_SR_FINISHED:
  589. if (s->hit) {
  590. st->hand_state = TLS_ST_OK;
  591. return WRITE_TRAN_CONTINUE;
  592. } else if (s->ext.ticket_expected) {
  593. st->hand_state = TLS_ST_SW_SESSION_TICKET;
  594. } else {
  595. st->hand_state = TLS_ST_SW_CHANGE;
  596. }
  597. return WRITE_TRAN_CONTINUE;
  598. case TLS_ST_SW_SESSION_TICKET:
  599. st->hand_state = TLS_ST_SW_CHANGE;
  600. return WRITE_TRAN_CONTINUE;
  601. case TLS_ST_SW_CHANGE:
  602. st->hand_state = TLS_ST_SW_FINISHED;
  603. return WRITE_TRAN_CONTINUE;
  604. case TLS_ST_SW_FINISHED:
  605. if (s->hit) {
  606. return WRITE_TRAN_FINISHED;
  607. }
  608. st->hand_state = TLS_ST_OK;
  609. return WRITE_TRAN_CONTINUE;
  610. }
  611. }
  612. /*
  613. * Perform any pre work that needs to be done prior to sending a message from
  614. * the server to the client.
  615. */
  616. WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst)
  617. {
  618. OSSL_STATEM *st = &s->statem;
  619. switch (st->hand_state) {
  620. default:
  621. /* No pre work to be done */
  622. break;
  623. case TLS_ST_SW_HELLO_REQ:
  624. s->shutdown = 0;
  625. if (SSL_IS_DTLS(s))
  626. dtls1_clear_sent_buffer(s);
  627. break;
  628. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  629. s->shutdown = 0;
  630. if (SSL_IS_DTLS(s)) {
  631. dtls1_clear_sent_buffer(s);
  632. /* We don't buffer this message so don't use the timer */
  633. st->use_timer = 0;
  634. }
  635. break;
  636. case TLS_ST_SW_SRVR_HELLO:
  637. if (SSL_IS_DTLS(s)) {
  638. /*
  639. * Messages we write from now on should be buffered and
  640. * retransmitted if necessary, so we need to use the timer now
  641. */
  642. st->use_timer = 1;
  643. }
  644. break;
  645. case TLS_ST_SW_SRVR_DONE:
  646. #ifndef OPENSSL_NO_SCTP
  647. if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
  648. /* Calls SSLfatal() as required */
  649. return dtls_wait_for_dry(s);
  650. }
  651. #endif
  652. return WORK_FINISHED_CONTINUE;
  653. case TLS_ST_SW_SESSION_TICKET:
  654. if (SSL_IS_TLS13(s) && s->sent_tickets == 0
  655. && s->ext.extra_tickets_expected == 0) {
  656. /*
  657. * Actually this is the end of the handshake, but we're going
  658. * straight into writing the session ticket out. So we finish off
  659. * the handshake, but keep the various buffers active.
  660. *
  661. * Calls SSLfatal as required.
  662. */
  663. return tls_finish_handshake(s, wst, 0, 0);
  664. } if (SSL_IS_DTLS(s)) {
  665. /*
  666. * We're into the last flight. We don't retransmit the last flight
  667. * unless we need to, so we don't use the timer
  668. */
  669. st->use_timer = 0;
  670. }
  671. break;
  672. case TLS_ST_SW_CHANGE:
  673. if (SSL_IS_TLS13(s))
  674. break;
  675. /* Writes to s->session are only safe for initial handshakes */
  676. if (s->session->cipher == NULL) {
  677. s->session->cipher = s->s3->tmp.new_cipher;
  678. } else if (s->session->cipher != s->s3->tmp.new_cipher) {
  679. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  680. SSL_F_OSSL_STATEM_SERVER_PRE_WORK,
  681. ERR_R_INTERNAL_ERROR);
  682. return WORK_ERROR;
  683. }
  684. if (!s->method->ssl3_enc->setup_key_block(s)) {
  685. /* SSLfatal() already called */
  686. return WORK_ERROR;
  687. }
  688. if (SSL_IS_DTLS(s)) {
  689. /*
  690. * We're into the last flight. We don't retransmit the last flight
  691. * unless we need to, so we don't use the timer. This might have
  692. * already been set to 0 if we sent a NewSessionTicket message,
  693. * but we'll set it again here in case we didn't.
  694. */
  695. st->use_timer = 0;
  696. }
  697. return WORK_FINISHED_CONTINUE;
  698. case TLS_ST_EARLY_DATA:
  699. if (s->early_data_state != SSL_EARLY_DATA_ACCEPTING
  700. && (s->s3->flags & TLS1_FLAGS_STATELESS) == 0)
  701. return WORK_FINISHED_CONTINUE;
  702. /* Fall through */
  703. case TLS_ST_OK:
  704. /* Calls SSLfatal() as required */
  705. return tls_finish_handshake(s, wst, 1, 1);
  706. }
  707. return WORK_FINISHED_CONTINUE;
  708. }
  709. static ossl_inline int conn_is_closed(void)
  710. {
  711. switch (get_last_sys_error()) {
  712. #if defined(EPIPE)
  713. case EPIPE:
  714. return 1;
  715. #endif
  716. #if defined(ECONNRESET)
  717. case ECONNRESET:
  718. return 1;
  719. #endif
  720. #if defined(WSAECONNRESET)
  721. case WSAECONNRESET:
  722. return 1;
  723. #endif
  724. default:
  725. return 0;
  726. }
  727. }
  728. /*
  729. * Perform any work that needs to be done after sending a message from the
  730. * server to the client.
  731. */
  732. WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst)
  733. {
  734. OSSL_STATEM *st = &s->statem;
  735. s->init_num = 0;
  736. switch (st->hand_state) {
  737. default:
  738. /* No post work to be done */
  739. break;
  740. case TLS_ST_SW_HELLO_REQ:
  741. if (statem_flush(s) != 1)
  742. return WORK_MORE_A;
  743. if (!ssl3_init_finished_mac(s)) {
  744. /* SSLfatal() already called */
  745. return WORK_ERROR;
  746. }
  747. break;
  748. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  749. if (statem_flush(s) != 1)
  750. return WORK_MORE_A;
  751. /* HelloVerifyRequest resets Finished MAC */
  752. if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) {
  753. /* SSLfatal() already called */
  754. return WORK_ERROR;
  755. }
  756. /*
  757. * The next message should be another ClientHello which we need to
  758. * treat like it was the first packet
  759. */
  760. s->first_packet = 1;
  761. break;
  762. case TLS_ST_SW_SRVR_HELLO:
  763. if (SSL_IS_TLS13(s) && s->hello_retry_request == SSL_HRR_PENDING) {
  764. if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0
  765. && statem_flush(s) != 1)
  766. return WORK_MORE_A;
  767. break;
  768. }
  769. #ifndef OPENSSL_NO_SCTP
  770. if (SSL_IS_DTLS(s) && s->hit) {
  771. unsigned char sctpauthkey[64];
  772. char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
  773. size_t labellen;
  774. /*
  775. * Add new shared key for SCTP-Auth, will be ignored if no
  776. * SCTP used.
  777. */
  778. memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
  779. sizeof(DTLS1_SCTP_AUTH_LABEL));
  780. /* Don't include the terminating zero. */
  781. labellen = sizeof(labelbuffer) - 1;
  782. if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
  783. labellen += 1;
  784. if (SSL_export_keying_material(s, sctpauthkey,
  785. sizeof(sctpauthkey), labelbuffer,
  786. labellen, NULL, 0,
  787. 0) <= 0) {
  788. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  789. SSL_F_OSSL_STATEM_SERVER_POST_WORK,
  790. ERR_R_INTERNAL_ERROR);
  791. return WORK_ERROR;
  792. }
  793. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  794. sizeof(sctpauthkey), sctpauthkey);
  795. }
  796. #endif
  797. if (!SSL_IS_TLS13(s)
  798. || ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
  799. && s->hello_retry_request != SSL_HRR_COMPLETE))
  800. break;
  801. /* Fall through */
  802. case TLS_ST_SW_CHANGE:
  803. if (s->hello_retry_request == SSL_HRR_PENDING) {
  804. if (!statem_flush(s))
  805. return WORK_MORE_A;
  806. break;
  807. }
  808. if (SSL_IS_TLS13(s)) {
  809. if (!s->method->ssl3_enc->setup_key_block(s)
  810. || !s->method->ssl3_enc->change_cipher_state(s,
  811. SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
  812. /* SSLfatal() already called */
  813. return WORK_ERROR;
  814. }
  815. if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED
  816. && !s->method->ssl3_enc->change_cipher_state(s,
  817. SSL3_CC_HANDSHAKE |SSL3_CHANGE_CIPHER_SERVER_READ)) {
  818. /* SSLfatal() already called */
  819. return WORK_ERROR;
  820. }
  821. /*
  822. * We don't yet know whether the next record we are going to receive
  823. * is an unencrypted alert, an encrypted alert, or an encrypted
  824. * handshake message. We temporarily tolerate unencrypted alerts.
  825. */
  826. s->statem.enc_read_state = ENC_READ_STATE_ALLOW_PLAIN_ALERTS;
  827. break;
  828. }
  829. #ifndef OPENSSL_NO_SCTP
  830. if (SSL_IS_DTLS(s) && !s->hit) {
  831. /*
  832. * Change to new shared key of SCTP-Auth, will be ignored if
  833. * no SCTP used.
  834. */
  835. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  836. 0, NULL);
  837. }
  838. #endif
  839. if (!s->method->ssl3_enc->change_cipher_state(s,
  840. SSL3_CHANGE_CIPHER_SERVER_WRITE))
  841. {
  842. /* SSLfatal() already called */
  843. return WORK_ERROR;
  844. }
  845. if (SSL_IS_DTLS(s))
  846. dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
  847. break;
  848. case TLS_ST_SW_SRVR_DONE:
  849. if (statem_flush(s) != 1)
  850. return WORK_MORE_A;
  851. break;
  852. case TLS_ST_SW_FINISHED:
  853. if (statem_flush(s) != 1)
  854. return WORK_MORE_A;
  855. #ifndef OPENSSL_NO_SCTP
  856. if (SSL_IS_DTLS(s) && s->hit) {
  857. /*
  858. * Change to new shared key of SCTP-Auth, will be ignored if
  859. * no SCTP used.
  860. */
  861. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  862. 0, NULL);
  863. }
  864. #endif
  865. if (SSL_IS_TLS13(s)) {
  866. /* TLS 1.3 gets the secret size from the handshake md */
  867. size_t dummy;
  868. if (!s->method->ssl3_enc->generate_master_secret(s,
  869. s->master_secret, s->handshake_secret, 0,
  870. &dummy)
  871. || !s->method->ssl3_enc->change_cipher_state(s,
  872. SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE))
  873. /* SSLfatal() already called */
  874. return WORK_ERROR;
  875. #ifndef OPENSSL_NO_QUIC
  876. if (SSL_IS_QUIC(s) && s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
  877. s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
  878. if (!s->method->ssl3_enc->change_cipher_state(
  879. s, SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ))
  880. /* SSLfatal() already called */
  881. return WORK_ERROR;
  882. }
  883. #endif
  884. }
  885. break;
  886. case TLS_ST_SW_CERT_REQ:
  887. if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
  888. if (statem_flush(s) != 1)
  889. return WORK_MORE_A;
  890. }
  891. break;
  892. case TLS_ST_SW_KEY_UPDATE:
  893. if (statem_flush(s) != 1)
  894. return WORK_MORE_A;
  895. if (!tls13_update_key(s, 1)) {
  896. /* SSLfatal() already called */
  897. return WORK_ERROR;
  898. }
  899. break;
  900. case TLS_ST_SW_SESSION_TICKET:
  901. clear_sys_error();
  902. if (SSL_IS_TLS13(s) && statem_flush(s) != 1) {
  903. if (SSL_get_error(s, 0) == SSL_ERROR_SYSCALL
  904. && conn_is_closed()) {
  905. /*
  906. * We ignore connection closed errors in TLSv1.3 when sending a
  907. * NewSessionTicket and behave as if we were successful. This is
  908. * so that we are still able to read data sent to us by a client
  909. * that closes soon after the end of the handshake without
  910. * waiting to read our post-handshake NewSessionTickets.
  911. */
  912. s->rwstate = SSL_NOTHING;
  913. break;
  914. }
  915. return WORK_MORE_A;
  916. }
  917. break;
  918. }
  919. return WORK_FINISHED_CONTINUE;
  920. }
  921. /*
  922. * Get the message construction function and message type for sending from the
  923. * server
  924. *
  925. * Valid return values are:
  926. * 1: Success
  927. * 0: Error
  928. */
  929. int ossl_statem_server_construct_message(SSL *s, WPACKET *pkt,
  930. confunc_f *confunc, int *mt)
  931. {
  932. OSSL_STATEM *st = &s->statem;
  933. switch (st->hand_state) {
  934. default:
  935. /* Shouldn't happen */
  936. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  937. SSL_F_OSSL_STATEM_SERVER_CONSTRUCT_MESSAGE,
  938. SSL_R_BAD_HANDSHAKE_STATE);
  939. return 0;
  940. case TLS_ST_SW_CHANGE:
  941. if (SSL_IS_DTLS(s))
  942. *confunc = dtls_construct_change_cipher_spec;
  943. else
  944. *confunc = tls_construct_change_cipher_spec;
  945. *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
  946. break;
  947. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  948. *confunc = dtls_construct_hello_verify_request;
  949. *mt = DTLS1_MT_HELLO_VERIFY_REQUEST;
  950. break;
  951. case TLS_ST_SW_HELLO_REQ:
  952. /* No construction function needed */
  953. *confunc = NULL;
  954. *mt = SSL3_MT_HELLO_REQUEST;
  955. break;
  956. case TLS_ST_SW_SRVR_HELLO:
  957. *confunc = tls_construct_server_hello;
  958. *mt = SSL3_MT_SERVER_HELLO;
  959. break;
  960. case TLS_ST_SW_CERT:
  961. *confunc = tls_construct_server_certificate;
  962. *mt = SSL3_MT_CERTIFICATE;
  963. break;
  964. case TLS_ST_SW_CERT_VRFY:
  965. *confunc = tls_construct_cert_verify;
  966. *mt = SSL3_MT_CERTIFICATE_VERIFY;
  967. break;
  968. case TLS_ST_SW_KEY_EXCH:
  969. *confunc = tls_construct_server_key_exchange;
  970. *mt = SSL3_MT_SERVER_KEY_EXCHANGE;
  971. break;
  972. case TLS_ST_SW_CERT_REQ:
  973. *confunc = tls_construct_certificate_request;
  974. *mt = SSL3_MT_CERTIFICATE_REQUEST;
  975. break;
  976. case TLS_ST_SW_SRVR_DONE:
  977. *confunc = tls_construct_server_done;
  978. *mt = SSL3_MT_SERVER_DONE;
  979. break;
  980. case TLS_ST_SW_SESSION_TICKET:
  981. *confunc = tls_construct_new_session_ticket;
  982. *mt = SSL3_MT_NEWSESSION_TICKET;
  983. break;
  984. case TLS_ST_SW_CERT_STATUS:
  985. *confunc = tls_construct_cert_status;
  986. *mt = SSL3_MT_CERTIFICATE_STATUS;
  987. break;
  988. case TLS_ST_SW_FINISHED:
  989. *confunc = tls_construct_finished;
  990. *mt = SSL3_MT_FINISHED;
  991. break;
  992. case TLS_ST_EARLY_DATA:
  993. *confunc = NULL;
  994. *mt = SSL3_MT_DUMMY;
  995. break;
  996. case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
  997. *confunc = tls_construct_encrypted_extensions;
  998. *mt = SSL3_MT_ENCRYPTED_EXTENSIONS;
  999. break;
  1000. case TLS_ST_SW_KEY_UPDATE:
  1001. *confunc = tls_construct_key_update;
  1002. *mt = SSL3_MT_KEY_UPDATE;
  1003. break;
  1004. }
  1005. return 1;
  1006. }
  1007. /*
  1008. * Maximum size (excluding the Handshake header) of a ClientHello message,
  1009. * calculated as follows:
  1010. *
  1011. * 2 + # client_version
  1012. * 32 + # only valid length for random
  1013. * 1 + # length of session_id
  1014. * 32 + # maximum size for session_id
  1015. * 2 + # length of cipher suites
  1016. * 2^16-2 + # maximum length of cipher suites array
  1017. * 1 + # length of compression_methods
  1018. * 2^8-1 + # maximum length of compression methods
  1019. * 2 + # length of extensions
  1020. * 2^16-1 # maximum length of extensions
  1021. */
  1022. #define CLIENT_HELLO_MAX_LENGTH 131396
  1023. #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
  1024. #define NEXT_PROTO_MAX_LENGTH 514
  1025. /*
  1026. * Returns the maximum allowed length for the current message that we are
  1027. * reading. Excludes the message header.
  1028. */
  1029. size_t ossl_statem_server_max_message_size(SSL *s)
  1030. {
  1031. OSSL_STATEM *st = &s->statem;
  1032. switch (st->hand_state) {
  1033. default:
  1034. /* Shouldn't happen */
  1035. return 0;
  1036. case TLS_ST_SR_CLNT_HELLO:
  1037. return CLIENT_HELLO_MAX_LENGTH;
  1038. case TLS_ST_SR_END_OF_EARLY_DATA:
  1039. return END_OF_EARLY_DATA_MAX_LENGTH;
  1040. case TLS_ST_SR_CERT:
  1041. return s->max_cert_list;
  1042. case TLS_ST_SR_KEY_EXCH:
  1043. return CLIENT_KEY_EXCH_MAX_LENGTH;
  1044. case TLS_ST_SR_CERT_VRFY:
  1045. return SSL3_RT_MAX_PLAIN_LENGTH;
  1046. #ifndef OPENSSL_NO_NEXTPROTONEG
  1047. case TLS_ST_SR_NEXT_PROTO:
  1048. return NEXT_PROTO_MAX_LENGTH;
  1049. #endif
  1050. case TLS_ST_SR_CHANGE:
  1051. return CCS_MAX_LENGTH;
  1052. case TLS_ST_SR_FINISHED:
  1053. return FINISHED_MAX_LENGTH;
  1054. case TLS_ST_SR_KEY_UPDATE:
  1055. return KEY_UPDATE_MAX_LENGTH;
  1056. }
  1057. }
  1058. /*
  1059. * Process a message that the server has received from the client.
  1060. */
  1061. MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt)
  1062. {
  1063. OSSL_STATEM *st = &s->statem;
  1064. switch (st->hand_state) {
  1065. default:
  1066. /* Shouldn't happen */
  1067. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1068. SSL_F_OSSL_STATEM_SERVER_PROCESS_MESSAGE,
  1069. ERR_R_INTERNAL_ERROR);
  1070. return MSG_PROCESS_ERROR;
  1071. case TLS_ST_SR_CLNT_HELLO:
  1072. return tls_process_client_hello(s, pkt);
  1073. case TLS_ST_SR_END_OF_EARLY_DATA:
  1074. return tls_process_end_of_early_data(s, pkt);
  1075. case TLS_ST_SR_CERT:
  1076. return tls_process_client_certificate(s, pkt);
  1077. case TLS_ST_SR_KEY_EXCH:
  1078. return tls_process_client_key_exchange(s, pkt);
  1079. case TLS_ST_SR_CERT_VRFY:
  1080. return tls_process_cert_verify(s, pkt);
  1081. #ifndef OPENSSL_NO_NEXTPROTONEG
  1082. case TLS_ST_SR_NEXT_PROTO:
  1083. return tls_process_next_proto(s, pkt);
  1084. #endif
  1085. case TLS_ST_SR_CHANGE:
  1086. return tls_process_change_cipher_spec(s, pkt);
  1087. case TLS_ST_SR_FINISHED:
  1088. return tls_process_finished(s, pkt);
  1089. case TLS_ST_SR_KEY_UPDATE:
  1090. return tls_process_key_update(s, pkt);
  1091. }
  1092. }
  1093. /*
  1094. * Perform any further processing required following the receipt of a message
  1095. * from the client
  1096. */
  1097. WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst)
  1098. {
  1099. OSSL_STATEM *st = &s->statem;
  1100. switch (st->hand_state) {
  1101. default:
  1102. /* Shouldn't happen */
  1103. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1104. SSL_F_OSSL_STATEM_SERVER_POST_PROCESS_MESSAGE,
  1105. ERR_R_INTERNAL_ERROR);
  1106. return WORK_ERROR;
  1107. case TLS_ST_SR_CLNT_HELLO:
  1108. return tls_post_process_client_hello(s, wst);
  1109. case TLS_ST_SR_KEY_EXCH:
  1110. return tls_post_process_client_key_exchange(s, wst);
  1111. }
  1112. }
  1113. #ifndef OPENSSL_NO_SRP
  1114. /* Returns 1 on success, 0 for retryable error, -1 for fatal error */
  1115. static int ssl_check_srp_ext_ClientHello(SSL *s)
  1116. {
  1117. int ret;
  1118. int al = SSL_AD_UNRECOGNIZED_NAME;
  1119. if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
  1120. (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
  1121. if (s->srp_ctx.login == NULL) {
  1122. /*
  1123. * RFC 5054 says SHOULD reject, we do so if There is no srp
  1124. * login name
  1125. */
  1126. SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY,
  1127. SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO,
  1128. SSL_R_PSK_IDENTITY_NOT_FOUND);
  1129. return -1;
  1130. } else {
  1131. ret = SSL_srp_server_param_with_username(s, &al);
  1132. if (ret < 0)
  1133. return 0;
  1134. if (ret == SSL3_AL_FATAL) {
  1135. SSLfatal(s, al, SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO,
  1136. al == SSL_AD_UNKNOWN_PSK_IDENTITY
  1137. ? SSL_R_PSK_IDENTITY_NOT_FOUND
  1138. : SSL_R_CLIENTHELLO_TLSEXT);
  1139. return -1;
  1140. }
  1141. }
  1142. }
  1143. return 1;
  1144. }
  1145. #endif
  1146. int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie,
  1147. size_t cookie_len)
  1148. {
  1149. /* Always use DTLS 1.0 version: see RFC 6347 */
  1150. if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION)
  1151. || !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len))
  1152. return 0;
  1153. return 1;
  1154. }
  1155. int dtls_construct_hello_verify_request(SSL *s, WPACKET *pkt)
  1156. {
  1157. unsigned int cookie_leni;
  1158. if (s->ctx->app_gen_cookie_cb == NULL ||
  1159. s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
  1160. &cookie_leni) == 0 ||
  1161. cookie_leni > 255) {
  1162. SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
  1163. SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
  1164. return 0;
  1165. }
  1166. s->d1->cookie_len = cookie_leni;
  1167. if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie,
  1168. s->d1->cookie_len)) {
  1169. SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
  1170. ERR_R_INTERNAL_ERROR);
  1171. return 0;
  1172. }
  1173. return 1;
  1174. }
  1175. #ifndef OPENSSL_NO_EC
  1176. /*-
  1177. * ssl_check_for_safari attempts to fingerprint Safari using OS X
  1178. * SecureTransport using the TLS extension block in |hello|.
  1179. * Safari, since 10.6, sends exactly these extensions, in this order:
  1180. * SNI,
  1181. * elliptic_curves
  1182. * ec_point_formats
  1183. * signature_algorithms (for TLSv1.2 only)
  1184. *
  1185. * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
  1186. * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
  1187. * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
  1188. * 10.8..10.8.3 (which don't work).
  1189. */
  1190. static void ssl_check_for_safari(SSL *s, const CLIENTHELLO_MSG *hello)
  1191. {
  1192. static const unsigned char kSafariExtensionsBlock[] = {
  1193. 0x00, 0x0a, /* elliptic_curves extension */
  1194. 0x00, 0x08, /* 8 bytes */
  1195. 0x00, 0x06, /* 6 bytes of curve ids */
  1196. 0x00, 0x17, /* P-256 */
  1197. 0x00, 0x18, /* P-384 */
  1198. 0x00, 0x19, /* P-521 */
  1199. 0x00, 0x0b, /* ec_point_formats */
  1200. 0x00, 0x02, /* 2 bytes */
  1201. 0x01, /* 1 point format */
  1202. 0x00, /* uncompressed */
  1203. /* The following is only present in TLS 1.2 */
  1204. 0x00, 0x0d, /* signature_algorithms */
  1205. 0x00, 0x0c, /* 12 bytes */
  1206. 0x00, 0x0a, /* 10 bytes */
  1207. 0x05, 0x01, /* SHA-384/RSA */
  1208. 0x04, 0x01, /* SHA-256/RSA */
  1209. 0x02, 0x01, /* SHA-1/RSA */
  1210. 0x04, 0x03, /* SHA-256/ECDSA */
  1211. 0x02, 0x03, /* SHA-1/ECDSA */
  1212. };
  1213. /* Length of the common prefix (first two extensions). */
  1214. static const size_t kSafariCommonExtensionsLength = 18;
  1215. unsigned int type;
  1216. PACKET sni, tmppkt;
  1217. size_t ext_len;
  1218. tmppkt = hello->extensions;
  1219. if (!PACKET_forward(&tmppkt, 2)
  1220. || !PACKET_get_net_2(&tmppkt, &type)
  1221. || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
  1222. return;
  1223. }
  1224. if (type != TLSEXT_TYPE_server_name)
  1225. return;
  1226. ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
  1227. sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
  1228. s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
  1229. ext_len);
  1230. }
  1231. #endif /* !OPENSSL_NO_EC */
  1232. MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)
  1233. {
  1234. /* |cookie| will only be initialized for DTLS. */
  1235. PACKET session_id, compression, extensions, cookie;
  1236. static const unsigned char null_compression = 0;
  1237. CLIENTHELLO_MSG *clienthello = NULL;
  1238. /* Check if this is actually an unexpected renegotiation ClientHello */
  1239. if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
  1240. if (!ossl_assert(!SSL_IS_TLS13(s))) {
  1241. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
  1242. ERR_R_INTERNAL_ERROR);
  1243. goto err;
  1244. }
  1245. if ((s->options & SSL_OP_NO_RENEGOTIATION) != 0
  1246. || (!s->s3->send_connection_binding
  1247. && (s->options
  1248. & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION) == 0)) {
  1249. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
  1250. return MSG_PROCESS_FINISHED_READING;
  1251. }
  1252. s->renegotiate = 1;
  1253. s->new_session = 1;
  1254. }
  1255. clienthello = OPENSSL_zalloc(sizeof(*clienthello));
  1256. if (clienthello == NULL) {
  1257. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
  1258. ERR_R_INTERNAL_ERROR);
  1259. goto err;
  1260. }
  1261. /*
  1262. * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
  1263. */
  1264. clienthello->isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer);
  1265. PACKET_null_init(&cookie);
  1266. if (clienthello->isv2) {
  1267. unsigned int mt;
  1268. if (!SSL_IS_FIRST_HANDSHAKE(s)
  1269. || s->hello_retry_request != SSL_HRR_NONE) {
  1270. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
  1271. SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNEXPECTED_MESSAGE);
  1272. goto err;
  1273. }
  1274. /*-
  1275. * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
  1276. * header is sent directly on the wire, not wrapped as a TLS
  1277. * record. Our record layer just processes the message length and passes
  1278. * the rest right through. Its format is:
  1279. * Byte Content
  1280. * 0-1 msg_length - decoded by the record layer
  1281. * 2 msg_type - s->init_msg points here
  1282. * 3-4 version
  1283. * 5-6 cipher_spec_length
  1284. * 7-8 session_id_length
  1285. * 9-10 challenge_length
  1286. * ... ...
  1287. */
  1288. if (!PACKET_get_1(pkt, &mt)
  1289. || mt != SSL2_MT_CLIENT_HELLO) {
  1290. /*
  1291. * Should never happen. We should have tested this in the record
  1292. * layer in order to have determined that this is a SSLv2 record
  1293. * in the first place
  1294. */
  1295. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
  1296. ERR_R_INTERNAL_ERROR);
  1297. goto err;
  1298. }
  1299. }
  1300. if (!PACKET_get_net_2(pkt, &clienthello->legacy_version)) {
  1301. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
  1302. SSL_R_LENGTH_TOO_SHORT);
  1303. goto err;
  1304. }
  1305. /* Parse the message and load client random. */
  1306. if (clienthello->isv2) {
  1307. /*
  1308. * Handle an SSLv2 backwards compatible ClientHello
  1309. * Note, this is only for SSLv3+ using the backward compatible format.
  1310. * Real SSLv2 is not supported, and is rejected below.
  1311. */
  1312. unsigned int ciphersuite_len, session_id_len, challenge_len;
  1313. PACKET challenge;
  1314. if (!PACKET_get_net_2(pkt, &ciphersuite_len)
  1315. || !PACKET_get_net_2(pkt, &session_id_len)
  1316. || !PACKET_get_net_2(pkt, &challenge_len)) {
  1317. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
  1318. SSL_R_RECORD_LENGTH_MISMATCH);
  1319. goto err;
  1320. }
  1321. if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  1322. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1323. SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
  1324. goto err;
  1325. }
  1326. if (!PACKET_get_sub_packet(pkt, &clienthello->ciphersuites,
  1327. ciphersuite_len)
  1328. || !PACKET_copy_bytes(pkt, clienthello->session_id, session_id_len)
  1329. || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
  1330. /* No extensions. */
  1331. || PACKET_remaining(pkt) != 0) {
  1332. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
  1333. SSL_R_RECORD_LENGTH_MISMATCH);
  1334. goto err;
  1335. }
  1336. clienthello->session_id_len = session_id_len;
  1337. /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
  1338. * here rather than sizeof(clienthello->random) because that is the limit
  1339. * for SSLv3 and it is fixed. It won't change even if
  1340. * sizeof(clienthello->random) does.
  1341. */
  1342. challenge_len = challenge_len > SSL3_RANDOM_SIZE
  1343. ? SSL3_RANDOM_SIZE : challenge_len;
  1344. memset(clienthello->random, 0, SSL3_RANDOM_SIZE);
  1345. if (!PACKET_copy_bytes(&challenge,
  1346. clienthello->random + SSL3_RANDOM_SIZE -
  1347. challenge_len, challenge_len)
  1348. /* Advertise only null compression. */
  1349. || !PACKET_buf_init(&compression, &null_compression, 1)) {
  1350. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
  1351. ERR_R_INTERNAL_ERROR);
  1352. goto err;
  1353. }
  1354. PACKET_null_init(&clienthello->extensions);
  1355. } else {
  1356. /* Regular ClientHello. */
  1357. if (!PACKET_copy_bytes(pkt, clienthello->random, SSL3_RANDOM_SIZE)
  1358. || !PACKET_get_length_prefixed_1(pkt, &session_id)
  1359. || !PACKET_copy_all(&session_id, clienthello->session_id,
  1360. SSL_MAX_SSL_SESSION_ID_LENGTH,
  1361. &clienthello->session_id_len)) {
  1362. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
  1363. SSL_R_LENGTH_MISMATCH);
  1364. goto err;
  1365. }
  1366. if (SSL_IS_DTLS(s)) {
  1367. if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
  1368. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
  1369. SSL_R_LENGTH_MISMATCH);
  1370. goto err;
  1371. }
  1372. if (!PACKET_copy_all(&cookie, clienthello->dtls_cookie,
  1373. DTLS1_COOKIE_LENGTH,
  1374. &clienthello->dtls_cookie_len)) {
  1375. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1376. SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  1377. goto err;
  1378. }
  1379. /*
  1380. * If we require cookies and this ClientHello doesn't contain one,
  1381. * just return since we do not want to allocate any memory yet.
  1382. * So check cookie length...
  1383. */
  1384. if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
  1385. if (clienthello->dtls_cookie_len == 0) {
  1386. OPENSSL_free(clienthello);
  1387. return MSG_PROCESS_FINISHED_READING;
  1388. }
  1389. }
  1390. }
  1391. if (!PACKET_get_length_prefixed_2(pkt, &clienthello->ciphersuites)) {
  1392. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
  1393. SSL_R_LENGTH_MISMATCH);
  1394. goto err;
  1395. }
  1396. if (!PACKET_get_length_prefixed_1(pkt, &compression)) {
  1397. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
  1398. SSL_R_LENGTH_MISMATCH);
  1399. goto err;
  1400. }
  1401. /* Could be empty. */
  1402. if (PACKET_remaining(pkt) == 0) {
  1403. PACKET_null_init(&clienthello->extensions);
  1404. } else {
  1405. if (!PACKET_get_length_prefixed_2(pkt, &clienthello->extensions)
  1406. || PACKET_remaining(pkt) != 0) {
  1407. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
  1408. SSL_R_LENGTH_MISMATCH);
  1409. goto err;
  1410. }
  1411. }
  1412. #ifndef OPENSSL_NO_QUIC
  1413. if (SSL_IS_QUIC(s)) {
  1414. /* Any other QUIC checks on ClientHello here */
  1415. if (clienthello->session_id_len > 0) {
  1416. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_CLIENT_HELLO,
  1417. SSL_R_LENGTH_MISMATCH);
  1418. goto err;
  1419. }
  1420. }
  1421. #endif
  1422. }
  1423. if (!PACKET_copy_all(&compression, clienthello->compressions,
  1424. MAX_COMPRESSIONS_SIZE,
  1425. &clienthello->compressions_len)) {
  1426. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
  1427. ERR_R_INTERNAL_ERROR);
  1428. goto err;
  1429. }
  1430. /* Preserve the raw extensions PACKET for later use */
  1431. extensions = clienthello->extensions;
  1432. if (!tls_collect_extensions(s, &extensions, SSL_EXT_CLIENT_HELLO,
  1433. &clienthello->pre_proc_exts,
  1434. &clienthello->pre_proc_exts_len, 1)) {
  1435. /* SSLfatal already been called */
  1436. goto err;
  1437. }
  1438. s->clienthello = clienthello;
  1439. return MSG_PROCESS_CONTINUE_PROCESSING;
  1440. err:
  1441. if (clienthello != NULL)
  1442. OPENSSL_free(clienthello->pre_proc_exts);
  1443. OPENSSL_free(clienthello);
  1444. return MSG_PROCESS_ERROR;
  1445. }
  1446. static int tls_early_post_process_client_hello(SSL *s)
  1447. {
  1448. unsigned int j;
  1449. int i, al = SSL_AD_INTERNAL_ERROR;
  1450. int protverr;
  1451. size_t loop;
  1452. unsigned long id;
  1453. #ifndef OPENSSL_NO_COMP
  1454. SSL_COMP *comp = NULL;
  1455. #endif
  1456. const SSL_CIPHER *c;
  1457. STACK_OF(SSL_CIPHER) *ciphers = NULL;
  1458. STACK_OF(SSL_CIPHER) *scsvs = NULL;
  1459. CLIENTHELLO_MSG *clienthello = s->clienthello;
  1460. DOWNGRADE dgrd = DOWNGRADE_NONE;
  1461. /* Finished parsing the ClientHello, now we can start processing it */
  1462. /* Give the ClientHello callback a crack at things */
  1463. if (s->ctx->client_hello_cb != NULL) {
  1464. /* A failure in the ClientHello callback terminates the connection. */
  1465. switch (s->ctx->client_hello_cb(s, &al, s->ctx->client_hello_cb_arg)) {
  1466. case SSL_CLIENT_HELLO_SUCCESS:
  1467. break;
  1468. case SSL_CLIENT_HELLO_RETRY:
  1469. s->rwstate = SSL_CLIENT_HELLO_CB;
  1470. return -1;
  1471. case SSL_CLIENT_HELLO_ERROR:
  1472. default:
  1473. SSLfatal(s, al,
  1474. SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
  1475. SSL_R_CALLBACK_FAILED);
  1476. goto err;
  1477. }
  1478. }
  1479. /* Set up the client_random */
  1480. memcpy(s->s3->client_random, clienthello->random, SSL3_RANDOM_SIZE);
  1481. /* Choose the version */
  1482. if (clienthello->isv2) {
  1483. if (clienthello->legacy_version == SSL2_VERSION
  1484. || (clienthello->legacy_version & 0xff00)
  1485. != (SSL3_VERSION_MAJOR << 8)) {
  1486. /*
  1487. * This is real SSLv2 or something completely unknown. We don't
  1488. * support it.
  1489. */
  1490. SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
  1491. SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
  1492. SSL_R_UNKNOWN_PROTOCOL);
  1493. goto err;
  1494. }
  1495. /* SSLv3/TLS */
  1496. s->client_version = clienthello->legacy_version;
  1497. }
  1498. /*
  1499. * Do SSL/TLS version negotiation if applicable. For DTLS we just check
  1500. * versions are potentially compatible. Version negotiation comes later.
  1501. */
  1502. if (!SSL_IS_DTLS(s)) {
  1503. protverr = ssl_choose_server_version(s, clienthello, &dgrd);
  1504. } else if (s->method->version != DTLS_ANY_VERSION &&
  1505. DTLS_VERSION_LT((int)clienthello->legacy_version, s->version)) {
  1506. protverr = SSL_R_VERSION_TOO_LOW;
  1507. } else {
  1508. protverr = 0;
  1509. }
  1510. if (protverr) {
  1511. if (SSL_IS_FIRST_HANDSHAKE(s)) {
  1512. /* like ssl3_get_record, send alert using remote version number */
  1513. s->version = s->client_version = clienthello->legacy_version;
  1514. }
  1515. SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
  1516. SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, protverr);
  1517. goto err;
  1518. }
  1519. /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
  1520. if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) {
  1521. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
  1522. SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
  1523. SSL_R_NOT_ON_RECORD_BOUNDARY);
  1524. goto err;
  1525. }
  1526. if (SSL_IS_DTLS(s)) {
  1527. /* Empty cookie was already handled above by returning early. */
  1528. if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
  1529. if (s->ctx->app_verify_cookie_cb != NULL) {
  1530. if (s->ctx->app_verify_cookie_cb(s, clienthello->dtls_cookie,
  1531. clienthello->dtls_cookie_len) == 0) {
  1532. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1533. SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
  1534. SSL_R_COOKIE_MISMATCH);
  1535. goto err;
  1536. /* else cookie verification succeeded */
  1537. }
  1538. /* default verification */
  1539. } else if (s->d1->cookie_len != clienthello->dtls_cookie_len
  1540. || memcmp(clienthello->dtls_cookie, s->d1->cookie,
  1541. s->d1->cookie_len) != 0) {
  1542. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1543. SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
  1544. SSL_R_COOKIE_MISMATCH);
  1545. goto err;
  1546. }
  1547. s->d1->cookie_verified = 1;
  1548. }
  1549. if (s->method->version == DTLS_ANY_VERSION) {
  1550. protverr = ssl_choose_server_version(s, clienthello, &dgrd);
  1551. if (protverr != 0) {
  1552. s->version = s->client_version;
  1553. SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
  1554. SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, protverr);
  1555. goto err;
  1556. }
  1557. }
  1558. }
  1559. s->hit = 0;
  1560. if (!ssl_cache_cipherlist(s, &clienthello->ciphersuites,
  1561. clienthello->isv2) ||
  1562. !bytes_to_cipher_list(s, &clienthello->ciphersuites, &ciphers, &scsvs,
  1563. clienthello->isv2, 1)) {
  1564. /* SSLfatal() already called */
  1565. goto err;
  1566. }
  1567. s->s3->send_connection_binding = 0;
  1568. /* Check what signalling cipher-suite values were received. */
  1569. if (scsvs != NULL) {
  1570. for(i = 0; i < sk_SSL_CIPHER_num(scsvs); i++) {
  1571. c = sk_SSL_CIPHER_value(scsvs, i);
  1572. if (SSL_CIPHER_get_id(c) == SSL3_CK_SCSV) {
  1573. if (s->renegotiate) {
  1574. /* SCSV is fatal if renegotiating */
  1575. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1576. SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
  1577. SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
  1578. goto err;
  1579. }
  1580. s->s3->send_connection_binding = 1;
  1581. } else if (SSL_CIPHER_get_id(c) == SSL3_CK_FALLBACK_SCSV &&
  1582. !ssl_check_version_downgrade(s)) {
  1583. /*
  1584. * This SCSV indicates that the client previously tried
  1585. * a higher version. We should fail if the current version
  1586. * is an unexpected downgrade, as that indicates that the first
  1587. * connection may have been tampered with in order to trigger
  1588. * an insecure downgrade.
  1589. */
  1590. SSLfatal(s, SSL_AD_INAPPROPRIATE_FALLBACK,
  1591. SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
  1592. SSL_R_INAPPROPRIATE_FALLBACK);
  1593. goto err;
  1594. }
  1595. }
  1596. }
  1597. /* For TLSv1.3 we must select the ciphersuite *before* session resumption */
  1598. if (SSL_IS_TLS13(s)) {
  1599. const SSL_CIPHER *cipher =
  1600. ssl3_choose_cipher(s, ciphers, SSL_get_ciphers(s));
  1601. if (cipher == NULL) {
  1602. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1603. SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
  1604. SSL_R_NO_SHARED_CIPHER);
  1605. goto err;
  1606. }
  1607. if (s->hello_retry_request == SSL_HRR_PENDING
  1608. && (s->s3->tmp.new_cipher == NULL
  1609. || s->s3->tmp.new_cipher->id != cipher->id)) {
  1610. /*
  1611. * A previous HRR picked a different ciphersuite to the one we
  1612. * just selected. Something must have changed.
  1613. */
  1614. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1615. SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
  1616. SSL_R_BAD_CIPHER);
  1617. goto err;
  1618. }
  1619. s->s3->tmp.new_cipher = cipher;
  1620. }
  1621. /* We need to do this before getting the session */
  1622. if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret,
  1623. SSL_EXT_CLIENT_HELLO,
  1624. clienthello->pre_proc_exts, NULL, 0)) {
  1625. /* SSLfatal() already called */
  1626. goto err;
  1627. }
  1628. /*
  1629. * We don't allow resumption in a backwards compatible ClientHello.
  1630. * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
  1631. *
  1632. * Versions before 0.9.7 always allow clients to resume sessions in
  1633. * renegotiation. 0.9.7 and later allow this by default, but optionally
  1634. * ignore resumption requests with flag
  1635. * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
  1636. * than a change to default behavior so that applications relying on
  1637. * this for security won't even compile against older library versions).
  1638. * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
  1639. * request renegotiation but not a new session (s->new_session remains
  1640. * unset): for servers, this essentially just means that the
  1641. * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
  1642. * ignored.
  1643. */
  1644. if (clienthello->isv2 ||
  1645. (s->new_session &&
  1646. (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
  1647. if (!ssl_get_new_session(s, 1)) {
  1648. /* SSLfatal() already called */
  1649. goto err;
  1650. }
  1651. } else {
  1652. i = ssl_get_prev_session(s, clienthello);
  1653. if (i == 1) {
  1654. /* previous session */
  1655. s->hit = 1;
  1656. } else if (i == -1) {
  1657. /* SSLfatal() already called */
  1658. goto err;
  1659. } else {
  1660. /* i == 0 */
  1661. if (!ssl_get_new_session(s, 1)) {
  1662. /* SSLfatal() already called */
  1663. goto err;
  1664. }
  1665. }
  1666. }
  1667. if (SSL_IS_TLS13(s)) {
  1668. memcpy(s->tmp_session_id, s->clienthello->session_id,
  1669. s->clienthello->session_id_len);
  1670. s->tmp_session_id_len = s->clienthello->session_id_len;
  1671. }
  1672. /*
  1673. * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
  1674. * ciphersuite compatibility with the session as part of resumption.
  1675. */
  1676. if (!SSL_IS_TLS13(s) && s->hit) {
  1677. j = 0;
  1678. id = s->session->cipher->id;
  1679. #ifdef CIPHER_DEBUG
  1680. fprintf(stderr, "client sent %d ciphers\n", sk_SSL_CIPHER_num(ciphers));
  1681. #endif
  1682. for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
  1683. c = sk_SSL_CIPHER_value(ciphers, i);
  1684. #ifdef CIPHER_DEBUG
  1685. fprintf(stderr, "client [%2d of %2d]:%s\n",
  1686. i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
  1687. #endif
  1688. if (c->id == id) {
  1689. j = 1;
  1690. break;
  1691. }
  1692. }
  1693. if (j == 0) {
  1694. /*
  1695. * we need to have the cipher in the cipher list if we are asked
  1696. * to reuse it
  1697. */
  1698. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1699. SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
  1700. SSL_R_REQUIRED_CIPHER_MISSING);
  1701. goto err;
  1702. }
  1703. }
  1704. for (loop = 0; loop < clienthello->compressions_len; loop++) {
  1705. if (clienthello->compressions[loop] == 0)
  1706. break;
  1707. }
  1708. if (loop >= clienthello->compressions_len) {
  1709. /* no compress */
  1710. SSLfatal(s, SSL_AD_DECODE_ERROR,
  1711. SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
  1712. SSL_R_NO_COMPRESSION_SPECIFIED);
  1713. goto err;
  1714. }
  1715. #ifndef OPENSSL_NO_EC
  1716. if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
  1717. ssl_check_for_safari(s, clienthello);
  1718. #endif /* !OPENSSL_NO_EC */
  1719. /* TLS extensions */
  1720. if (!tls_parse_all_extensions(s, SSL_EXT_CLIENT_HELLO,
  1721. clienthello->pre_proc_exts, NULL, 0, 1)) {
  1722. /* SSLfatal() already called */
  1723. goto err;
  1724. }
  1725. /*
  1726. * Check if we want to use external pre-shared secret for this handshake
  1727. * for not reused session only. We need to generate server_random before
  1728. * calling tls_session_secret_cb in order to allow SessionTicket
  1729. * processing to use it in key derivation.
  1730. */
  1731. {
  1732. unsigned char *pos;
  1733. pos = s->s3->server_random;
  1734. if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE, dgrd) <= 0) {
  1735. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1736. SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
  1737. ERR_R_INTERNAL_ERROR);
  1738. goto err;
  1739. }
  1740. }
  1741. if (!s->hit
  1742. && s->version >= TLS1_VERSION
  1743. && !SSL_IS_TLS13(s)
  1744. && !SSL_IS_DTLS(s)
  1745. && s->ext.session_secret_cb) {
  1746. const SSL_CIPHER *pref_cipher = NULL;
  1747. /*
  1748. * s->session->master_key_length is a size_t, but this is an int for
  1749. * backwards compat reasons
  1750. */
  1751. int master_key_length;
  1752. master_key_length = sizeof(s->session->master_key);
  1753. if (s->ext.session_secret_cb(s, s->session->master_key,
  1754. &master_key_length, ciphers,
  1755. &pref_cipher,
  1756. s->ext.session_secret_cb_arg)
  1757. && master_key_length > 0) {
  1758. s->session->master_key_length = master_key_length;
  1759. s->hit = 1;
  1760. s->peer_ciphers = ciphers;
  1761. s->session->verify_result = X509_V_OK;
  1762. ciphers = NULL;
  1763. /* check if some cipher was preferred by call back */
  1764. if (pref_cipher == NULL)
  1765. pref_cipher = ssl3_choose_cipher(s, s->peer_ciphers,
  1766. SSL_get_ciphers(s));
  1767. if (pref_cipher == NULL) {
  1768. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1769. SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
  1770. SSL_R_NO_SHARED_CIPHER);
  1771. goto err;
  1772. }
  1773. s->session->cipher = pref_cipher;
  1774. sk_SSL_CIPHER_free(s->cipher_list);
  1775. s->cipher_list = sk_SSL_CIPHER_dup(s->peer_ciphers);
  1776. sk_SSL_CIPHER_free(s->cipher_list_by_id);
  1777. s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->peer_ciphers);
  1778. }
  1779. }
  1780. /*
  1781. * Worst case, we will use the NULL compression, but if we have other
  1782. * options, we will now look for them. We have complen-1 compression
  1783. * algorithms from the client, starting at q.
  1784. */
  1785. s->s3->tmp.new_compression = NULL;
  1786. if (SSL_IS_TLS13(s)) {
  1787. /*
  1788. * We already checked above that the NULL compression method appears in
  1789. * the list. Now we check there aren't any others (which is illegal in
  1790. * a TLSv1.3 ClientHello.
  1791. */
  1792. if (clienthello->compressions_len != 1) {
  1793. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1794. SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
  1795. SSL_R_INVALID_COMPRESSION_ALGORITHM);
  1796. goto err;
  1797. }
  1798. }
  1799. #ifndef OPENSSL_NO_COMP
  1800. /* This only happens if we have a cache hit */
  1801. else if (s->session->compress_meth != 0) {
  1802. int m, comp_id = s->session->compress_meth;
  1803. unsigned int k;
  1804. /* Perform sanity checks on resumed compression algorithm */
  1805. /* Can't disable compression */
  1806. if (!ssl_allow_compression(s)) {
  1807. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1808. SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
  1809. SSL_R_INCONSISTENT_COMPRESSION);
  1810. goto err;
  1811. }
  1812. /* Look for resumed compression method */
  1813. for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
  1814. comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
  1815. if (comp_id == comp->id) {
  1816. s->s3->tmp.new_compression = comp;
  1817. break;
  1818. }
  1819. }
  1820. if (s->s3->tmp.new_compression == NULL) {
  1821. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1822. SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
  1823. SSL_R_INVALID_COMPRESSION_ALGORITHM);
  1824. goto err;
  1825. }
  1826. /* Look for resumed method in compression list */
  1827. for (k = 0; k < clienthello->compressions_len; k++) {
  1828. if (clienthello->compressions[k] == comp_id)
  1829. break;
  1830. }
  1831. if (k >= clienthello->compressions_len) {
  1832. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1833. SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
  1834. SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
  1835. goto err;
  1836. }
  1837. } else if (s->hit) {
  1838. comp = NULL;
  1839. } else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
  1840. /* See if we have a match */
  1841. int m, nn, v, done = 0;
  1842. unsigned int o;
  1843. nn = sk_SSL_COMP_num(s->ctx->comp_methods);
  1844. for (m = 0; m < nn; m++) {
  1845. comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
  1846. v = comp->id;
  1847. for (o = 0; o < clienthello->compressions_len; o++) {
  1848. if (v == clienthello->compressions[o]) {
  1849. done = 1;
  1850. break;
  1851. }
  1852. }
  1853. if (done)
  1854. break;
  1855. }
  1856. if (done)
  1857. s->s3->tmp.new_compression = comp;
  1858. else
  1859. comp = NULL;
  1860. }
  1861. #else
  1862. /*
  1863. * If compression is disabled we'd better not try to resume a session
  1864. * using compression.
  1865. */
  1866. if (s->session->compress_meth != 0) {
  1867. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1868. SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
  1869. SSL_R_INCONSISTENT_COMPRESSION);
  1870. goto err;
  1871. }
  1872. #endif
  1873. /*
  1874. * Given s->peer_ciphers and SSL_get_ciphers, we must pick a cipher
  1875. */
  1876. if (!s->hit || SSL_IS_TLS13(s)) {
  1877. sk_SSL_CIPHER_free(s->peer_ciphers);
  1878. s->peer_ciphers = ciphers;
  1879. if (ciphers == NULL) {
  1880. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1881. SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
  1882. ERR_R_INTERNAL_ERROR);
  1883. goto err;
  1884. }
  1885. ciphers = NULL;
  1886. }
  1887. if (!s->hit) {
  1888. #ifdef OPENSSL_NO_COMP
  1889. s->session->compress_meth = 0;
  1890. #else
  1891. s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
  1892. #endif
  1893. if (!tls1_set_server_sigalgs(s)) {
  1894. /* SSLfatal() already called */
  1895. goto err;
  1896. }
  1897. }
  1898. sk_SSL_CIPHER_free(ciphers);
  1899. sk_SSL_CIPHER_free(scsvs);
  1900. OPENSSL_free(clienthello->pre_proc_exts);
  1901. OPENSSL_free(s->clienthello);
  1902. s->clienthello = NULL;
  1903. return 1;
  1904. err:
  1905. sk_SSL_CIPHER_free(ciphers);
  1906. sk_SSL_CIPHER_free(scsvs);
  1907. OPENSSL_free(clienthello->pre_proc_exts);
  1908. OPENSSL_free(s->clienthello);
  1909. s->clienthello = NULL;
  1910. return 0;
  1911. }
  1912. /*
  1913. * Call the status request callback if needed. Upon success, returns 1.
  1914. * Upon failure, returns 0.
  1915. */
  1916. static int tls_handle_status_request(SSL *s)
  1917. {
  1918. s->ext.status_expected = 0;
  1919. /*
  1920. * If status request then ask callback what to do. Note: this must be
  1921. * called after servername callbacks in case the certificate has changed,
  1922. * and must be called after the cipher has been chosen because this may
  1923. * influence which certificate is sent
  1924. */
  1925. if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing && s->ctx != NULL
  1926. && s->ctx->ext.status_cb != NULL) {
  1927. int ret;
  1928. /* If no certificate can't return certificate status */
  1929. if (s->s3->tmp.cert != NULL) {
  1930. /*
  1931. * Set current certificate to one we will use so SSL_get_certificate
  1932. * et al can pick it up.
  1933. */
  1934. s->cert->key = s->s3->tmp.cert;
  1935. ret = s->ctx->ext.status_cb(s, s->ctx->ext.status_arg);
  1936. switch (ret) {
  1937. /* We don't want to send a status request response */
  1938. case SSL_TLSEXT_ERR_NOACK:
  1939. s->ext.status_expected = 0;
  1940. break;
  1941. /* status request response should be sent */
  1942. case SSL_TLSEXT_ERR_OK:
  1943. if (s->ext.ocsp.resp)
  1944. s->ext.status_expected = 1;
  1945. break;
  1946. /* something bad happened */
  1947. case SSL_TLSEXT_ERR_ALERT_FATAL:
  1948. default:
  1949. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1950. SSL_F_TLS_HANDLE_STATUS_REQUEST,
  1951. SSL_R_CLIENTHELLO_TLSEXT);
  1952. return 0;
  1953. }
  1954. }
  1955. }
  1956. return 1;
  1957. }
  1958. /*
  1959. * Call the alpn_select callback if needed. Upon success, returns 1.
  1960. * Upon failure, returns 0.
  1961. */
  1962. int tls_handle_alpn(SSL *s)
  1963. {
  1964. const unsigned char *selected = NULL;
  1965. unsigned char selected_len = 0;
  1966. if (s->ctx->ext.alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) {
  1967. int r = s->ctx->ext.alpn_select_cb(s, &selected, &selected_len,
  1968. s->s3->alpn_proposed,
  1969. (unsigned int)s->s3->alpn_proposed_len,
  1970. s->ctx->ext.alpn_select_cb_arg);
  1971. if (r == SSL_TLSEXT_ERR_OK) {
  1972. OPENSSL_free(s->s3->alpn_selected);
  1973. s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len);
  1974. if (s->s3->alpn_selected == NULL) {
  1975. s->s3->alpn_selected_len = 0;
  1976. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_HANDLE_ALPN,
  1977. ERR_R_INTERNAL_ERROR);
  1978. return 0;
  1979. }
  1980. s->s3->alpn_selected_len = selected_len;
  1981. #ifndef OPENSSL_NO_NEXTPROTONEG
  1982. /* ALPN takes precedence over NPN. */
  1983. s->s3->npn_seen = 0;
  1984. #endif
  1985. /* Check ALPN is consistent with session */
  1986. if (s->session->ext.alpn_selected == NULL
  1987. || selected_len != s->session->ext.alpn_selected_len
  1988. || memcmp(selected, s->session->ext.alpn_selected,
  1989. selected_len) != 0) {
  1990. /* Not consistent so can't be used for early_data */
  1991. s->ext.early_data_ok = 0;
  1992. if (!s->hit) {
  1993. /*
  1994. * This is a new session and so alpn_selected should have
  1995. * been initialised to NULL. We should update it with the
  1996. * selected ALPN.
  1997. */
  1998. if (!ossl_assert(s->session->ext.alpn_selected == NULL)) {
  1999. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2000. SSL_F_TLS_HANDLE_ALPN,
  2001. ERR_R_INTERNAL_ERROR);
  2002. return 0;
  2003. }
  2004. s->session->ext.alpn_selected = OPENSSL_memdup(selected,
  2005. selected_len);
  2006. if (s->session->ext.alpn_selected == NULL) {
  2007. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2008. SSL_F_TLS_HANDLE_ALPN,
  2009. ERR_R_INTERNAL_ERROR);
  2010. return 0;
  2011. }
  2012. s->session->ext.alpn_selected_len = selected_len;
  2013. }
  2014. }
  2015. return 1;
  2016. } else if (r != SSL_TLSEXT_ERR_NOACK) {
  2017. SSLfatal(s, SSL_AD_NO_APPLICATION_PROTOCOL, SSL_F_TLS_HANDLE_ALPN,
  2018. SSL_R_NO_APPLICATION_PROTOCOL);
  2019. return 0;
  2020. }
  2021. /*
  2022. * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was
  2023. * present.
  2024. */
  2025. }
  2026. /* Check ALPN is consistent with session */
  2027. if (s->session->ext.alpn_selected != NULL) {
  2028. /* Not consistent so can't be used for early_data */
  2029. s->ext.early_data_ok = 0;
  2030. }
  2031. return 1;
  2032. }
  2033. WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst)
  2034. {
  2035. const SSL_CIPHER *cipher;
  2036. if (wst == WORK_MORE_A) {
  2037. int rv = tls_early_post_process_client_hello(s);
  2038. if (rv == 0) {
  2039. /* SSLfatal() was already called */
  2040. goto err;
  2041. }
  2042. if (rv < 0)
  2043. return WORK_MORE_A;
  2044. wst = WORK_MORE_B;
  2045. }
  2046. if (wst == WORK_MORE_B) {
  2047. if (!s->hit || SSL_IS_TLS13(s)) {
  2048. /* Let cert callback update server certificates if required */
  2049. if (!s->hit && s->cert->cert_cb != NULL) {
  2050. int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
  2051. if (rv == 0) {
  2052. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2053. SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
  2054. SSL_R_CERT_CB_ERROR);
  2055. goto err;
  2056. }
  2057. if (rv < 0) {
  2058. s->rwstate = SSL_X509_LOOKUP;
  2059. return WORK_MORE_B;
  2060. }
  2061. s->rwstate = SSL_NOTHING;
  2062. }
  2063. /* In TLSv1.3 we selected the ciphersuite before resumption */
  2064. if (!SSL_IS_TLS13(s)) {
  2065. cipher =
  2066. ssl3_choose_cipher(s, s->peer_ciphers, SSL_get_ciphers(s));
  2067. if (cipher == NULL) {
  2068. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  2069. SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
  2070. SSL_R_NO_SHARED_CIPHER);
  2071. goto err;
  2072. }
  2073. s->s3->tmp.new_cipher = cipher;
  2074. }
  2075. if (!s->hit) {
  2076. if (!tls_choose_sigalg(s, 1)) {
  2077. /* SSLfatal already called */
  2078. goto err;
  2079. }
  2080. /* check whether we should disable session resumption */
  2081. if (s->not_resumable_session_cb != NULL)
  2082. s->session->not_resumable =
  2083. s->not_resumable_session_cb(s,
  2084. ((s->s3->tmp.new_cipher->algorithm_mkey
  2085. & (SSL_kDHE | SSL_kECDHE)) != 0));
  2086. if (s->session->not_resumable)
  2087. /* do not send a session ticket */
  2088. s->ext.ticket_expected = 0;
  2089. }
  2090. } else {
  2091. /* Session-id reuse */
  2092. s->s3->tmp.new_cipher = s->session->cipher;
  2093. }
  2094. /*-
  2095. * we now have the following setup.
  2096. * client_random
  2097. * cipher_list - our preferred list of ciphers
  2098. * ciphers - the clients preferred list of ciphers
  2099. * compression - basically ignored right now
  2100. * ssl version is set - sslv3
  2101. * s->session - The ssl session has been setup.
  2102. * s->hit - session reuse flag
  2103. * s->s3->tmp.new_cipher- the new cipher to use.
  2104. */
  2105. /*
  2106. * Call status_request callback if needed. Has to be done after the
  2107. * certificate callbacks etc above.
  2108. */
  2109. if (!tls_handle_status_request(s)) {
  2110. /* SSLfatal() already called */
  2111. goto err;
  2112. }
  2113. /*
  2114. * Call alpn_select callback if needed. Has to be done after SNI and
  2115. * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
  2116. * we already did this because cipher negotiation happens earlier, and
  2117. * we must handle ALPN before we decide whether to accept early_data.
  2118. */
  2119. if (!SSL_IS_TLS13(s) && !tls_handle_alpn(s)) {
  2120. /* SSLfatal() already called */
  2121. goto err;
  2122. }
  2123. wst = WORK_MORE_C;
  2124. }
  2125. #ifndef OPENSSL_NO_SRP
  2126. if (wst == WORK_MORE_C) {
  2127. int ret;
  2128. if ((ret = ssl_check_srp_ext_ClientHello(s)) == 0) {
  2129. /*
  2130. * callback indicates further work to be done
  2131. */
  2132. s->rwstate = SSL_X509_LOOKUP;
  2133. return WORK_MORE_C;
  2134. }
  2135. if (ret < 0) {
  2136. /* SSLfatal() already called */
  2137. goto err;
  2138. }
  2139. }
  2140. #endif
  2141. return WORK_FINISHED_STOP;
  2142. err:
  2143. return WORK_ERROR;
  2144. }
  2145. int tls_construct_server_hello(SSL *s, WPACKET *pkt)
  2146. {
  2147. int compm;
  2148. size_t sl, len;
  2149. int version;
  2150. unsigned char *session_id;
  2151. int usetls13 = SSL_IS_TLS13(s) || s->hello_retry_request == SSL_HRR_PENDING;
  2152. version = usetls13 ? TLS1_2_VERSION : s->version;
  2153. if (!WPACKET_put_bytes_u16(pkt, version)
  2154. /*
  2155. * Random stuff. Filling of the server_random takes place in
  2156. * tls_process_client_hello()
  2157. */
  2158. || !WPACKET_memcpy(pkt,
  2159. s->hello_retry_request == SSL_HRR_PENDING
  2160. ? hrrrandom : s->s3->server_random,
  2161. SSL3_RANDOM_SIZE)) {
  2162. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_SERVER_HELLO,
  2163. ERR_R_INTERNAL_ERROR);
  2164. return 0;
  2165. }
  2166. /*-
  2167. * There are several cases for the session ID to send
  2168. * back in the server hello:
  2169. * - For session reuse from the session cache,
  2170. * we send back the old session ID.
  2171. * - If stateless session reuse (using a session ticket)
  2172. * is successful, we send back the client's "session ID"
  2173. * (which doesn't actually identify the session).
  2174. * - If it is a new session, we send back the new
  2175. * session ID.
  2176. * - However, if we want the new session to be single-use,
  2177. * we send back a 0-length session ID.
  2178. * - In TLSv1.3 we echo back the session id sent to us by the client
  2179. * regardless
  2180. * s->hit is non-zero in either case of session reuse,
  2181. * so the following won't overwrite an ID that we're supposed
  2182. * to send back.
  2183. */
  2184. if (s->session->not_resumable ||
  2185. (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
  2186. && !s->hit))
  2187. s->session->session_id_length = 0;
  2188. if (usetls13) {
  2189. sl = s->tmp_session_id_len;
  2190. session_id = s->tmp_session_id;
  2191. } else {
  2192. sl = s->session->session_id_length;
  2193. session_id = s->session->session_id;
  2194. }
  2195. if (sl > sizeof(s->session->session_id)) {
  2196. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_SERVER_HELLO,
  2197. ERR_R_INTERNAL_ERROR);
  2198. return 0;
  2199. }
  2200. /* set up the compression method */
  2201. #ifdef OPENSSL_NO_COMP
  2202. compm = 0;
  2203. #else
  2204. if (usetls13 || s->s3->tmp.new_compression == NULL)
  2205. compm = 0;
  2206. else
  2207. compm = s->s3->tmp.new_compression->id;
  2208. #endif
  2209. if (!WPACKET_sub_memcpy_u8(pkt, session_id, sl)
  2210. || !s->method->put_cipher_by_char(s->s3->tmp.new_cipher, pkt, &len)
  2211. || !WPACKET_put_bytes_u8(pkt, compm)) {
  2212. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_SERVER_HELLO,
  2213. ERR_R_INTERNAL_ERROR);
  2214. return 0;
  2215. }
  2216. if (!tls_construct_extensions(s, pkt,
  2217. s->hello_retry_request == SSL_HRR_PENDING
  2218. ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
  2219. : (SSL_IS_TLS13(s)
  2220. ? SSL_EXT_TLS1_3_SERVER_HELLO
  2221. : SSL_EXT_TLS1_2_SERVER_HELLO),
  2222. NULL, 0)) {
  2223. /* SSLfatal() already called */
  2224. return 0;
  2225. }
  2226. if (s->hello_retry_request == SSL_HRR_PENDING) {
  2227. /* Ditch the session. We'll create a new one next time around */
  2228. SSL_SESSION_free(s->session);
  2229. s->session = NULL;
  2230. s->hit = 0;
  2231. /*
  2232. * Re-initialise the Transcript Hash. We're going to prepopulate it with
  2233. * a synthetic message_hash in place of ClientHello1.
  2234. */
  2235. if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) {
  2236. /* SSLfatal() already called */
  2237. return 0;
  2238. }
  2239. } else if (!(s->verify_mode & SSL_VERIFY_PEER)
  2240. && !ssl3_digest_cached_records(s, 0)) {
  2241. /* SSLfatal() already called */;
  2242. return 0;
  2243. }
  2244. return 1;
  2245. }
  2246. int tls_construct_server_done(SSL *s, WPACKET *pkt)
  2247. {
  2248. if (!s->s3->tmp.cert_request) {
  2249. if (!ssl3_digest_cached_records(s, 0)) {
  2250. /* SSLfatal() already called */
  2251. return 0;
  2252. }
  2253. }
  2254. return 1;
  2255. }
  2256. int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt)
  2257. {
  2258. #ifndef OPENSSL_NO_DH
  2259. EVP_PKEY *pkdh = NULL;
  2260. #endif
  2261. #ifndef OPENSSL_NO_EC
  2262. unsigned char *encodedPoint = NULL;
  2263. size_t encodedlen = 0;
  2264. int curve_id = 0;
  2265. #endif
  2266. const SIGALG_LOOKUP *lu = s->s3->tmp.sigalg;
  2267. int i;
  2268. unsigned long type;
  2269. const BIGNUM *r[4];
  2270. EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
  2271. EVP_PKEY_CTX *pctx = NULL;
  2272. size_t paramlen, paramoffset;
  2273. if (!WPACKET_get_total_written(pkt, &paramoffset)) {
  2274. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2275. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  2276. goto err;
  2277. }
  2278. if (md_ctx == NULL) {
  2279. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2280. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  2281. goto err;
  2282. }
  2283. type = s->s3->tmp.new_cipher->algorithm_mkey;
  2284. r[0] = r[1] = r[2] = r[3] = NULL;
  2285. #ifndef OPENSSL_NO_PSK
  2286. /* Plain PSK or RSAPSK nothing to do */
  2287. if (type & (SSL_kPSK | SSL_kRSAPSK)) {
  2288. } else
  2289. #endif /* !OPENSSL_NO_PSK */
  2290. #ifndef OPENSSL_NO_DH
  2291. if (type & (SSL_kDHE | SSL_kDHEPSK)) {
  2292. CERT *cert = s->cert;
  2293. EVP_PKEY *pkdhp = NULL;
  2294. DH *dh;
  2295. if (s->cert->dh_tmp_auto) {
  2296. DH *dhp = ssl_get_auto_dh(s);
  2297. pkdh = EVP_PKEY_new();
  2298. if (pkdh == NULL || dhp == NULL) {
  2299. DH_free(dhp);
  2300. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2301. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  2302. ERR_R_INTERNAL_ERROR);
  2303. goto err;
  2304. }
  2305. EVP_PKEY_assign_DH(pkdh, dhp);
  2306. pkdhp = pkdh;
  2307. } else {
  2308. pkdhp = cert->dh_tmp;
  2309. }
  2310. if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {
  2311. DH *dhp = s->cert->dh_tmp_cb(s, 0, 1024);
  2312. pkdh = ssl_dh_to_pkey(dhp);
  2313. if (pkdh == NULL) {
  2314. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2315. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  2316. ERR_R_INTERNAL_ERROR);
  2317. goto err;
  2318. }
  2319. pkdhp = pkdh;
  2320. }
  2321. if (pkdhp == NULL) {
  2322. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2323. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  2324. SSL_R_MISSING_TMP_DH_KEY);
  2325. goto err;
  2326. }
  2327. if (!ssl_security(s, SSL_SECOP_TMP_DH,
  2328. EVP_PKEY_security_bits(pkdhp), 0, pkdhp)) {
  2329. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  2330. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  2331. SSL_R_DH_KEY_TOO_SMALL);
  2332. goto err;
  2333. }
  2334. if (s->s3->tmp.pkey != NULL) {
  2335. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2336. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  2337. ERR_R_INTERNAL_ERROR);
  2338. goto err;
  2339. }
  2340. s->s3->tmp.pkey = ssl_generate_pkey(pkdhp);
  2341. if (s->s3->tmp.pkey == NULL) {
  2342. SSLfatal(s, SSL_AD_INTERNAL_ERROR, 0, ERR_R_INTERNAL_ERROR);
  2343. goto err;
  2344. }
  2345. dh = EVP_PKEY_get0_DH(s->s3->tmp.pkey);
  2346. if (dh == NULL) {
  2347. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2348. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  2349. ERR_R_INTERNAL_ERROR);
  2350. goto err;
  2351. }
  2352. EVP_PKEY_free(pkdh);
  2353. pkdh = NULL;
  2354. DH_get0_pqg(dh, &r[0], NULL, &r[1]);
  2355. DH_get0_key(dh, &r[2], NULL);
  2356. } else
  2357. #endif
  2358. #ifndef OPENSSL_NO_EC
  2359. if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
  2360. if (s->s3->tmp.pkey != NULL) {
  2361. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2362. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  2363. ERR_R_INTERNAL_ERROR);
  2364. goto err;
  2365. }
  2366. /* Get NID of appropriate shared curve */
  2367. curve_id = tls1_shared_group(s, -2);
  2368. if (curve_id == 0) {
  2369. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  2370. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  2371. SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
  2372. goto err;
  2373. }
  2374. s->s3->tmp.pkey = ssl_generate_pkey_group(s, curve_id);
  2375. /* Generate a new key for this curve */
  2376. if (s->s3->tmp.pkey == NULL) {
  2377. /* SSLfatal() already called */
  2378. goto err;
  2379. }
  2380. /* Encode the public key. */
  2381. encodedlen = EVP_PKEY_get1_tls_encodedpoint(s->s3->tmp.pkey,
  2382. &encodedPoint);
  2383. if (encodedlen == 0) {
  2384. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2385. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EC_LIB);
  2386. goto err;
  2387. }
  2388. /*
  2389. * We'll generate the serverKeyExchange message explicitly so we
  2390. * can set these to NULLs
  2391. */
  2392. r[0] = NULL;
  2393. r[1] = NULL;
  2394. r[2] = NULL;
  2395. r[3] = NULL;
  2396. } else
  2397. #endif /* !OPENSSL_NO_EC */
  2398. #ifndef OPENSSL_NO_SRP
  2399. if (type & SSL_kSRP) {
  2400. if ((s->srp_ctx.N == NULL) ||
  2401. (s->srp_ctx.g == NULL) ||
  2402. (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
  2403. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2404. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  2405. SSL_R_MISSING_SRP_PARAM);
  2406. goto err;
  2407. }
  2408. r[0] = s->srp_ctx.N;
  2409. r[1] = s->srp_ctx.g;
  2410. r[2] = s->srp_ctx.s;
  2411. r[3] = s->srp_ctx.B;
  2412. } else
  2413. #endif
  2414. {
  2415. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2416. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  2417. SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  2418. goto err;
  2419. }
  2420. if (((s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0)
  2421. || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) {
  2422. lu = NULL;
  2423. } else if (lu == NULL) {
  2424. SSLfatal(s, SSL_AD_DECODE_ERROR,
  2425. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  2426. goto err;
  2427. }
  2428. #ifndef OPENSSL_NO_PSK
  2429. if (type & SSL_PSK) {
  2430. size_t len = (s->cert->psk_identity_hint == NULL)
  2431. ? 0 : strlen(s->cert->psk_identity_hint);
  2432. /*
  2433. * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
  2434. * checked this when we set the identity hint - but just in case
  2435. */
  2436. if (len > PSK_MAX_IDENTITY_LEN
  2437. || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,
  2438. len)) {
  2439. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2440. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  2441. ERR_R_INTERNAL_ERROR);
  2442. goto err;
  2443. }
  2444. }
  2445. #endif
  2446. for (i = 0; i < 4 && r[i] != NULL; i++) {
  2447. unsigned char *binval;
  2448. int res;
  2449. #ifndef OPENSSL_NO_SRP
  2450. if ((i == 2) && (type & SSL_kSRP)) {
  2451. res = WPACKET_start_sub_packet_u8(pkt);
  2452. } else
  2453. #endif
  2454. res = WPACKET_start_sub_packet_u16(pkt);
  2455. if (!res) {
  2456. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2457. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  2458. ERR_R_INTERNAL_ERROR);
  2459. goto err;
  2460. }
  2461. #ifndef OPENSSL_NO_DH
  2462. /*-
  2463. * for interoperability with some versions of the Microsoft TLS
  2464. * stack, we need to zero pad the DHE pub key to the same length
  2465. * as the prime
  2466. */
  2467. if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) {
  2468. size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]);
  2469. if (len > 0) {
  2470. if (!WPACKET_allocate_bytes(pkt, len, &binval)) {
  2471. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2472. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  2473. ERR_R_INTERNAL_ERROR);
  2474. goto err;
  2475. }
  2476. memset(binval, 0, len);
  2477. }
  2478. }
  2479. #endif
  2480. if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval)
  2481. || !WPACKET_close(pkt)) {
  2482. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2483. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  2484. ERR_R_INTERNAL_ERROR);
  2485. goto err;
  2486. }
  2487. BN_bn2bin(r[i], binval);
  2488. }
  2489. #ifndef OPENSSL_NO_EC
  2490. if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
  2491. /*
  2492. * We only support named (not generic) curves. In this situation, the
  2493. * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
  2494. * [1 byte length of encoded point], followed by the actual encoded
  2495. * point itself
  2496. */
  2497. if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE)
  2498. || !WPACKET_put_bytes_u8(pkt, 0)
  2499. || !WPACKET_put_bytes_u8(pkt, curve_id)
  2500. || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) {
  2501. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2502. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  2503. ERR_R_INTERNAL_ERROR);
  2504. goto err;
  2505. }
  2506. OPENSSL_free(encodedPoint);
  2507. encodedPoint = NULL;
  2508. }
  2509. #endif
  2510. /* not anonymous */
  2511. if (lu != NULL) {
  2512. EVP_PKEY *pkey = s->s3->tmp.cert->privatekey;
  2513. const EVP_MD *md;
  2514. unsigned char *sigbytes1, *sigbytes2, *tbs;
  2515. size_t siglen, tbslen;
  2516. int rv;
  2517. if (pkey == NULL || !tls1_lookup_md(lu, &md)) {
  2518. /* Should never happen */
  2519. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2520. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  2521. ERR_R_INTERNAL_ERROR);
  2522. goto err;
  2523. }
  2524. /* Get length of the parameters we have written above */
  2525. if (!WPACKET_get_length(pkt, &paramlen)) {
  2526. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2527. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  2528. ERR_R_INTERNAL_ERROR);
  2529. goto err;
  2530. }
  2531. /* send signature algorithm */
  2532. if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {
  2533. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2534. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  2535. ERR_R_INTERNAL_ERROR);
  2536. goto err;
  2537. }
  2538. /*
  2539. * Create the signature. We don't know the actual length of the sig
  2540. * until after we've created it, so we reserve enough bytes for it
  2541. * up front, and then properly allocate them in the WPACKET
  2542. * afterwards.
  2543. */
  2544. siglen = EVP_PKEY_size(pkey);
  2545. if (!WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1)
  2546. || EVP_DigestSignInit(md_ctx, &pctx, md, NULL, pkey) <= 0) {
  2547. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2548. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  2549. ERR_R_INTERNAL_ERROR);
  2550. goto err;
  2551. }
  2552. if (lu->sig == EVP_PKEY_RSA_PSS) {
  2553. if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
  2554. || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) {
  2555. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2556. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  2557. ERR_R_EVP_LIB);
  2558. goto err;
  2559. }
  2560. }
  2561. tbslen = construct_key_exchange_tbs(s, &tbs,
  2562. s->init_buf->data + paramoffset,
  2563. paramlen);
  2564. if (tbslen == 0) {
  2565. /* SSLfatal() already called */
  2566. goto err;
  2567. }
  2568. rv = EVP_DigestSign(md_ctx, sigbytes1, &siglen, tbs, tbslen);
  2569. OPENSSL_free(tbs);
  2570. if (rv <= 0 || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2)
  2571. || sigbytes1 != sigbytes2) {
  2572. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2573. SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  2574. ERR_R_INTERNAL_ERROR);
  2575. goto err;
  2576. }
  2577. }
  2578. EVP_MD_CTX_free(md_ctx);
  2579. return 1;
  2580. err:
  2581. #ifndef OPENSSL_NO_DH
  2582. EVP_PKEY_free(pkdh);
  2583. #endif
  2584. #ifndef OPENSSL_NO_EC
  2585. OPENSSL_free(encodedPoint);
  2586. #endif
  2587. EVP_MD_CTX_free(md_ctx);
  2588. return 0;
  2589. }
  2590. int tls_construct_certificate_request(SSL *s, WPACKET *pkt)
  2591. {
  2592. if (SSL_IS_TLS13(s)) {
  2593. /* Send random context when doing post-handshake auth */
  2594. if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
  2595. OPENSSL_free(s->pha_context);
  2596. s->pha_context_len = 32;
  2597. if ((s->pha_context = OPENSSL_malloc(s->pha_context_len)) == NULL) {
  2598. s->pha_context_len = 0;
  2599. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2600. SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
  2601. ERR_R_INTERNAL_ERROR);
  2602. return 0;
  2603. }
  2604. if (RAND_bytes(s->pha_context, s->pha_context_len) <= 0
  2605. || !WPACKET_sub_memcpy_u8(pkt, s->pha_context,
  2606. s->pha_context_len)) {
  2607. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2608. SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
  2609. ERR_R_INTERNAL_ERROR);
  2610. return 0;
  2611. }
  2612. /* reset the handshake hash back to just after the ClientFinished */
  2613. if (!tls13_restore_handshake_digest_for_pha(s)) {
  2614. /* SSLfatal() already called */
  2615. return 0;
  2616. }
  2617. } else {
  2618. if (!WPACKET_put_bytes_u8(pkt, 0)) {
  2619. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2620. SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
  2621. ERR_R_INTERNAL_ERROR);
  2622. return 0;
  2623. }
  2624. }
  2625. if (!tls_construct_extensions(s, pkt,
  2626. SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, NULL,
  2627. 0)) {
  2628. /* SSLfatal() already called */
  2629. return 0;
  2630. }
  2631. goto done;
  2632. }
  2633. /* get the list of acceptable cert types */
  2634. if (!WPACKET_start_sub_packet_u8(pkt)
  2635. || !ssl3_get_req_cert_type(s, pkt) || !WPACKET_close(pkt)) {
  2636. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2637. SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
  2638. return 0;
  2639. }
  2640. if (SSL_USE_SIGALGS(s)) {
  2641. const uint16_t *psigs;
  2642. size_t nl = tls12_get_psigalgs(s, 1, &psigs);
  2643. if (!WPACKET_start_sub_packet_u16(pkt)
  2644. || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
  2645. || !tls12_copy_sigalgs(s, pkt, psigs, nl)
  2646. || !WPACKET_close(pkt)) {
  2647. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2648. SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
  2649. ERR_R_INTERNAL_ERROR);
  2650. return 0;
  2651. }
  2652. }
  2653. if (!construct_ca_names(s, get_ca_names(s), pkt)) {
  2654. /* SSLfatal() already called */
  2655. return 0;
  2656. }
  2657. done:
  2658. s->certreqs_sent++;
  2659. s->s3->tmp.cert_request = 1;
  2660. return 1;
  2661. }
  2662. static int tls_process_cke_psk_preamble(SSL *s, PACKET *pkt)
  2663. {
  2664. #ifndef OPENSSL_NO_PSK
  2665. unsigned char psk[PSK_MAX_PSK_LEN];
  2666. size_t psklen;
  2667. PACKET psk_identity;
  2668. if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
  2669. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
  2670. SSL_R_LENGTH_MISMATCH);
  2671. return 0;
  2672. }
  2673. if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
  2674. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
  2675. SSL_R_DATA_LENGTH_TOO_LONG);
  2676. return 0;
  2677. }
  2678. if (s->psk_server_callback == NULL) {
  2679. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
  2680. SSL_R_PSK_NO_SERVER_CB);
  2681. return 0;
  2682. }
  2683. if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
  2684. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
  2685. ERR_R_INTERNAL_ERROR);
  2686. return 0;
  2687. }
  2688. psklen = s->psk_server_callback(s, s->session->psk_identity,
  2689. psk, sizeof(psk));
  2690. if (psklen > PSK_MAX_PSK_LEN) {
  2691. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
  2692. ERR_R_INTERNAL_ERROR);
  2693. return 0;
  2694. } else if (psklen == 0) {
  2695. /*
  2696. * PSK related to the given identity not found
  2697. */
  2698. SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY,
  2699. SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
  2700. SSL_R_PSK_IDENTITY_NOT_FOUND);
  2701. return 0;
  2702. }
  2703. OPENSSL_free(s->s3->tmp.psk);
  2704. s->s3->tmp.psk = OPENSSL_memdup(psk, psklen);
  2705. OPENSSL_cleanse(psk, psklen);
  2706. if (s->s3->tmp.psk == NULL) {
  2707. s->s3->tmp.psklen = 0;
  2708. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2709. SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE);
  2710. return 0;
  2711. }
  2712. s->s3->tmp.psklen = psklen;
  2713. return 1;
  2714. #else
  2715. /* Should never happen */
  2716. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
  2717. ERR_R_INTERNAL_ERROR);
  2718. return 0;
  2719. #endif
  2720. }
  2721. static int tls_process_cke_rsa(SSL *s, PACKET *pkt)
  2722. {
  2723. #ifndef OPENSSL_NO_RSA
  2724. unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
  2725. int decrypt_len;
  2726. unsigned char decrypt_good, version_good;
  2727. size_t j, padding_len;
  2728. PACKET enc_premaster;
  2729. RSA *rsa = NULL;
  2730. unsigned char *rsa_decrypt = NULL;
  2731. int ret = 0;
  2732. rsa = EVP_PKEY_get0_RSA(s->cert->pkeys[SSL_PKEY_RSA].privatekey);
  2733. if (rsa == NULL) {
  2734. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
  2735. SSL_R_MISSING_RSA_CERTIFICATE);
  2736. return 0;
  2737. }
  2738. /* SSLv3 and pre-standard DTLS omit the length bytes. */
  2739. if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
  2740. enc_premaster = *pkt;
  2741. } else {
  2742. if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
  2743. || PACKET_remaining(pkt) != 0) {
  2744. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
  2745. SSL_R_LENGTH_MISMATCH);
  2746. return 0;
  2747. }
  2748. }
  2749. /*
  2750. * We want to be sure that the plaintext buffer size makes it safe to
  2751. * iterate over the entire size of a premaster secret
  2752. * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
  2753. * their ciphertext cannot accommodate a premaster secret anyway.
  2754. */
  2755. if (RSA_size(rsa) < SSL_MAX_MASTER_KEY_LENGTH) {
  2756. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
  2757. RSA_R_KEY_SIZE_TOO_SMALL);
  2758. return 0;
  2759. }
  2760. rsa_decrypt = OPENSSL_malloc(RSA_size(rsa));
  2761. if (rsa_decrypt == NULL) {
  2762. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
  2763. ERR_R_MALLOC_FAILURE);
  2764. return 0;
  2765. }
  2766. /*
  2767. * We must not leak whether a decryption failure occurs because of
  2768. * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
  2769. * section 7.4.7.1). The code follows that advice of the TLS RFC and
  2770. * generates a random premaster secret for the case that the decrypt
  2771. * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
  2772. */
  2773. if (RAND_priv_bytes(rand_premaster_secret,
  2774. sizeof(rand_premaster_secret)) <= 0) {
  2775. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
  2776. ERR_R_INTERNAL_ERROR);
  2777. goto err;
  2778. }
  2779. /*
  2780. * Decrypt with no padding. PKCS#1 padding will be removed as part of
  2781. * the timing-sensitive code below.
  2782. */
  2783. /* TODO(size_t): Convert this function */
  2784. decrypt_len = (int)RSA_private_decrypt((int)PACKET_remaining(&enc_premaster),
  2785. PACKET_data(&enc_premaster),
  2786. rsa_decrypt, rsa, RSA_NO_PADDING);
  2787. if (decrypt_len < 0) {
  2788. SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
  2789. ERR_R_INTERNAL_ERROR);
  2790. goto err;
  2791. }
  2792. /* Check the padding. See RFC 3447, section 7.2.2. */
  2793. /*
  2794. * The smallest padded premaster is 11 bytes of overhead. Small keys
  2795. * are publicly invalid, so this may return immediately. This ensures
  2796. * PS is at least 8 bytes.
  2797. */
  2798. if (decrypt_len < 11 + SSL_MAX_MASTER_KEY_LENGTH) {
  2799. SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
  2800. SSL_R_DECRYPTION_FAILED);
  2801. goto err;
  2802. }
  2803. padding_len = decrypt_len - SSL_MAX_MASTER_KEY_LENGTH;
  2804. decrypt_good = constant_time_eq_int_8(rsa_decrypt[0], 0) &
  2805. constant_time_eq_int_8(rsa_decrypt[1], 2);
  2806. for (j = 2; j < padding_len - 1; j++) {
  2807. decrypt_good &= ~constant_time_is_zero_8(rsa_decrypt[j]);
  2808. }
  2809. decrypt_good &= constant_time_is_zero_8(rsa_decrypt[padding_len - 1]);
  2810. /*
  2811. * If the version in the decrypted pre-master secret is correct then
  2812. * version_good will be 0xff, otherwise it'll be zero. The
  2813. * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
  2814. * (http://eprint.iacr.org/2003/052/) exploits the version number
  2815. * check as a "bad version oracle". Thus version checks are done in
  2816. * constant time and are treated like any other decryption error.
  2817. */
  2818. version_good =
  2819. constant_time_eq_8(rsa_decrypt[padding_len],
  2820. (unsigned)(s->client_version >> 8));
  2821. version_good &=
  2822. constant_time_eq_8(rsa_decrypt[padding_len + 1],
  2823. (unsigned)(s->client_version & 0xff));
  2824. /*
  2825. * The premaster secret must contain the same version number as the
  2826. * ClientHello to detect version rollback attacks (strangely, the
  2827. * protocol does not offer such protection for DH ciphersuites).
  2828. * However, buggy clients exist that send the negotiated protocol
  2829. * version instead if the server does not support the requested
  2830. * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
  2831. * clients.
  2832. */
  2833. if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
  2834. unsigned char workaround_good;
  2835. workaround_good = constant_time_eq_8(rsa_decrypt[padding_len],
  2836. (unsigned)(s->version >> 8));
  2837. workaround_good &=
  2838. constant_time_eq_8(rsa_decrypt[padding_len + 1],
  2839. (unsigned)(s->version & 0xff));
  2840. version_good |= workaround_good;
  2841. }
  2842. /*
  2843. * Both decryption and version must be good for decrypt_good to
  2844. * remain non-zero (0xff).
  2845. */
  2846. decrypt_good &= version_good;
  2847. /*
  2848. * Now copy rand_premaster_secret over from p using
  2849. * decrypt_good_mask. If decryption failed, then p does not
  2850. * contain valid plaintext, however, a check above guarantees
  2851. * it is still sufficiently large to read from.
  2852. */
  2853. for (j = 0; j < sizeof(rand_premaster_secret); j++) {
  2854. rsa_decrypt[padding_len + j] =
  2855. constant_time_select_8(decrypt_good,
  2856. rsa_decrypt[padding_len + j],
  2857. rand_premaster_secret[j]);
  2858. }
  2859. if (!ssl_generate_master_secret(s, rsa_decrypt + padding_len,
  2860. sizeof(rand_premaster_secret), 0)) {
  2861. /* SSLfatal() already called */
  2862. goto err;
  2863. }
  2864. ret = 1;
  2865. err:
  2866. OPENSSL_free(rsa_decrypt);
  2867. return ret;
  2868. #else
  2869. /* Should never happen */
  2870. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
  2871. ERR_R_INTERNAL_ERROR);
  2872. return 0;
  2873. #endif
  2874. }
  2875. static int tls_process_cke_dhe(SSL *s, PACKET *pkt)
  2876. {
  2877. #ifndef OPENSSL_NO_DH
  2878. EVP_PKEY *skey = NULL;
  2879. DH *cdh;
  2880. unsigned int i;
  2881. BIGNUM *pub_key;
  2882. const unsigned char *data;
  2883. EVP_PKEY *ckey = NULL;
  2884. int ret = 0;
  2885. if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) {
  2886. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
  2887. SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
  2888. goto err;
  2889. }
  2890. skey = s->s3->tmp.pkey;
  2891. if (skey == NULL) {
  2892. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
  2893. SSL_R_MISSING_TMP_DH_KEY);
  2894. goto err;
  2895. }
  2896. if (PACKET_remaining(pkt) == 0L) {
  2897. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
  2898. SSL_R_MISSING_TMP_DH_KEY);
  2899. goto err;
  2900. }
  2901. if (!PACKET_get_bytes(pkt, &data, i)) {
  2902. /* We already checked we have enough data */
  2903. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
  2904. ERR_R_INTERNAL_ERROR);
  2905. goto err;
  2906. }
  2907. ckey = EVP_PKEY_new();
  2908. if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {
  2909. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
  2910. SSL_R_BN_LIB);
  2911. goto err;
  2912. }
  2913. cdh = EVP_PKEY_get0_DH(ckey);
  2914. pub_key = BN_bin2bn(data, i, NULL);
  2915. if (pub_key == NULL || cdh == NULL || !DH_set0_key(cdh, pub_key, NULL)) {
  2916. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
  2917. ERR_R_INTERNAL_ERROR);
  2918. BN_free(pub_key);
  2919. goto err;
  2920. }
  2921. if (ssl_derive(s, skey, ckey, 1) == 0) {
  2922. /* SSLfatal() already called */
  2923. goto err;
  2924. }
  2925. ret = 1;
  2926. EVP_PKEY_free(s->s3->tmp.pkey);
  2927. s->s3->tmp.pkey = NULL;
  2928. err:
  2929. EVP_PKEY_free(ckey);
  2930. return ret;
  2931. #else
  2932. /* Should never happen */
  2933. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
  2934. ERR_R_INTERNAL_ERROR);
  2935. return 0;
  2936. #endif
  2937. }
  2938. static int tls_process_cke_ecdhe(SSL *s, PACKET *pkt)
  2939. {
  2940. #ifndef OPENSSL_NO_EC
  2941. EVP_PKEY *skey = s->s3->tmp.pkey;
  2942. EVP_PKEY *ckey = NULL;
  2943. int ret = 0;
  2944. if (PACKET_remaining(pkt) == 0L) {
  2945. /* We don't support ECDH client auth */
  2946. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_PROCESS_CKE_ECDHE,
  2947. SSL_R_MISSING_TMP_ECDH_KEY);
  2948. goto err;
  2949. } else {
  2950. unsigned int i;
  2951. const unsigned char *data;
  2952. /*
  2953. * Get client's public key from encoded point in the
  2954. * ClientKeyExchange message.
  2955. */
  2956. /* Get encoded point length */
  2957. if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i)
  2958. || PACKET_remaining(pkt) != 0) {
  2959. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
  2960. SSL_R_LENGTH_MISMATCH);
  2961. goto err;
  2962. }
  2963. if (skey == NULL) {
  2964. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
  2965. SSL_R_MISSING_TMP_ECDH_KEY);
  2966. goto err;
  2967. }
  2968. ckey = EVP_PKEY_new();
  2969. if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {
  2970. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
  2971. ERR_R_EVP_LIB);
  2972. goto err;
  2973. }
  2974. if (EVP_PKEY_set1_tls_encodedpoint(ckey, data, i) == 0) {
  2975. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
  2976. ERR_R_EC_LIB);
  2977. goto err;
  2978. }
  2979. }
  2980. if (ssl_derive(s, skey, ckey, 1) == 0) {
  2981. /* SSLfatal() already called */
  2982. goto err;
  2983. }
  2984. ret = 1;
  2985. EVP_PKEY_free(s->s3->tmp.pkey);
  2986. s->s3->tmp.pkey = NULL;
  2987. err:
  2988. EVP_PKEY_free(ckey);
  2989. return ret;
  2990. #else
  2991. /* Should never happen */
  2992. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
  2993. ERR_R_INTERNAL_ERROR);
  2994. return 0;
  2995. #endif
  2996. }
  2997. static int tls_process_cke_srp(SSL *s, PACKET *pkt)
  2998. {
  2999. #ifndef OPENSSL_NO_SRP
  3000. unsigned int i;
  3001. const unsigned char *data;
  3002. if (!PACKET_get_net_2(pkt, &i)
  3003. || !PACKET_get_bytes(pkt, &data, i)) {
  3004. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
  3005. SSL_R_BAD_SRP_A_LENGTH);
  3006. return 0;
  3007. }
  3008. if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
  3009. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
  3010. ERR_R_BN_LIB);
  3011. return 0;
  3012. }
  3013. if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) {
  3014. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_CKE_SRP,
  3015. SSL_R_BAD_SRP_PARAMETERS);
  3016. return 0;
  3017. }
  3018. OPENSSL_free(s->session->srp_username);
  3019. s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
  3020. if (s->session->srp_username == NULL) {
  3021. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
  3022. ERR_R_MALLOC_FAILURE);
  3023. return 0;
  3024. }
  3025. if (!srp_generate_server_master_secret(s)) {
  3026. /* SSLfatal() already called */
  3027. return 0;
  3028. }
  3029. return 1;
  3030. #else
  3031. /* Should never happen */
  3032. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
  3033. ERR_R_INTERNAL_ERROR);
  3034. return 0;
  3035. #endif
  3036. }
  3037. static int tls_process_cke_gost(SSL *s, PACKET *pkt)
  3038. {
  3039. #ifndef OPENSSL_NO_GOST
  3040. EVP_PKEY_CTX *pkey_ctx;
  3041. EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
  3042. unsigned char premaster_secret[32];
  3043. const unsigned char *start;
  3044. size_t outlen = 32, inlen;
  3045. unsigned long alg_a;
  3046. GOST_KX_MESSAGE *pKX = NULL;
  3047. const unsigned char *ptr;
  3048. int ret = 0;
  3049. /* Get our certificate private key */
  3050. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  3051. if (alg_a & SSL_aGOST12) {
  3052. /*
  3053. * New GOST ciphersuites have SSL_aGOST01 bit too
  3054. */
  3055. pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
  3056. if (pk == NULL) {
  3057. pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
  3058. }
  3059. if (pk == NULL) {
  3060. pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
  3061. }
  3062. } else if (alg_a & SSL_aGOST01) {
  3063. pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
  3064. }
  3065. pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
  3066. if (pkey_ctx == NULL) {
  3067. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
  3068. ERR_R_MALLOC_FAILURE);
  3069. return 0;
  3070. }
  3071. if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
  3072. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
  3073. ERR_R_INTERNAL_ERROR);
  3074. return 0;
  3075. }
  3076. /*
  3077. * If client certificate is present and is of the same type, maybe
  3078. * use it for key exchange. Don't mind errors from
  3079. * EVP_PKEY_derive_set_peer, because it is completely valid to use a
  3080. * client certificate for authorization only.
  3081. */
  3082. client_pub_pkey = X509_get0_pubkey(s->session->peer);
  3083. if (client_pub_pkey) {
  3084. if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
  3085. ERR_clear_error();
  3086. }
  3087. ptr = PACKET_data(pkt);
  3088. /* Some implementations provide extra data in the opaqueBlob
  3089. * We have nothing to do with this blob so we just skip it */
  3090. pKX = d2i_GOST_KX_MESSAGE(NULL, &ptr, PACKET_remaining(pkt));
  3091. if (pKX == NULL
  3092. || pKX->kxBlob == NULL
  3093. || ASN1_TYPE_get(pKX->kxBlob) != V_ASN1_SEQUENCE) {
  3094. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
  3095. SSL_R_DECRYPTION_FAILED);
  3096. goto err;
  3097. }
  3098. if (!PACKET_forward(pkt, ptr - PACKET_data(pkt))) {
  3099. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
  3100. SSL_R_DECRYPTION_FAILED);
  3101. goto err;
  3102. }
  3103. if (PACKET_remaining(pkt) != 0) {
  3104. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
  3105. SSL_R_DECRYPTION_FAILED);
  3106. goto err;
  3107. }
  3108. inlen = pKX->kxBlob->value.sequence->length;
  3109. start = pKX->kxBlob->value.sequence->data;
  3110. if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start,
  3111. inlen) <= 0) {
  3112. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
  3113. SSL_R_DECRYPTION_FAILED);
  3114. goto err;
  3115. }
  3116. /* Generate master secret */
  3117. if (!ssl_generate_master_secret(s, premaster_secret,
  3118. sizeof(premaster_secret), 0)) {
  3119. /* SSLfatal() already called */
  3120. goto err;
  3121. }
  3122. /* Check if pubkey from client certificate was used */
  3123. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2,
  3124. NULL) > 0)
  3125. s->statem.no_cert_verify = 1;
  3126. ret = 1;
  3127. err:
  3128. EVP_PKEY_CTX_free(pkey_ctx);
  3129. GOST_KX_MESSAGE_free(pKX);
  3130. return ret;
  3131. #else
  3132. /* Should never happen */
  3133. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
  3134. ERR_R_INTERNAL_ERROR);
  3135. return 0;
  3136. #endif
  3137. }
  3138. MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt)
  3139. {
  3140. unsigned long alg_k;
  3141. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  3142. /* For PSK parse and retrieve identity, obtain PSK key */
  3143. if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt)) {
  3144. /* SSLfatal() already called */
  3145. goto err;
  3146. }
  3147. if (alg_k & SSL_kPSK) {
  3148. /* Identity extracted earlier: should be nothing left */
  3149. if (PACKET_remaining(pkt) != 0) {
  3150. SSLfatal(s, SSL_AD_DECODE_ERROR,
  3151. SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
  3152. SSL_R_LENGTH_MISMATCH);
  3153. goto err;
  3154. }
  3155. /* PSK handled by ssl_generate_master_secret */
  3156. if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
  3157. /* SSLfatal() already called */
  3158. goto err;
  3159. }
  3160. } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
  3161. if (!tls_process_cke_rsa(s, pkt)) {
  3162. /* SSLfatal() already called */
  3163. goto err;
  3164. }
  3165. } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
  3166. if (!tls_process_cke_dhe(s, pkt)) {
  3167. /* SSLfatal() already called */
  3168. goto err;
  3169. }
  3170. } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
  3171. if (!tls_process_cke_ecdhe(s, pkt)) {
  3172. /* SSLfatal() already called */
  3173. goto err;
  3174. }
  3175. } else if (alg_k & SSL_kSRP) {
  3176. if (!tls_process_cke_srp(s, pkt)) {
  3177. /* SSLfatal() already called */
  3178. goto err;
  3179. }
  3180. } else if (alg_k & SSL_kGOST) {
  3181. if (!tls_process_cke_gost(s, pkt)) {
  3182. /* SSLfatal() already called */
  3183. goto err;
  3184. }
  3185. } else {
  3186. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3187. SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
  3188. SSL_R_UNKNOWN_CIPHER_TYPE);
  3189. goto err;
  3190. }
  3191. return MSG_PROCESS_CONTINUE_PROCESSING;
  3192. err:
  3193. #ifndef OPENSSL_NO_PSK
  3194. OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
  3195. s->s3->tmp.psk = NULL;
  3196. s->s3->tmp.psklen = 0;
  3197. #endif
  3198. return MSG_PROCESS_ERROR;
  3199. }
  3200. WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst)
  3201. {
  3202. #ifndef OPENSSL_NO_SCTP
  3203. if (wst == WORK_MORE_A) {
  3204. if (SSL_IS_DTLS(s)) {
  3205. unsigned char sctpauthkey[64];
  3206. char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
  3207. size_t labellen;
  3208. /*
  3209. * Add new shared key for SCTP-Auth, will be ignored if no SCTP
  3210. * used.
  3211. */
  3212. memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
  3213. sizeof(DTLS1_SCTP_AUTH_LABEL));
  3214. /* Don't include the terminating zero. */
  3215. labellen = sizeof(labelbuffer) - 1;
  3216. if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
  3217. labellen += 1;
  3218. if (SSL_export_keying_material(s, sctpauthkey,
  3219. sizeof(sctpauthkey), labelbuffer,
  3220. labellen, NULL, 0,
  3221. 0) <= 0) {
  3222. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3223. SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
  3224. ERR_R_INTERNAL_ERROR);
  3225. return WORK_ERROR;
  3226. }
  3227. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  3228. sizeof(sctpauthkey), sctpauthkey);
  3229. }
  3230. }
  3231. #endif
  3232. if (s->statem.no_cert_verify || !s->session->peer) {
  3233. /*
  3234. * No certificate verify or no peer certificate so we no longer need
  3235. * the handshake_buffer
  3236. */
  3237. if (!ssl3_digest_cached_records(s, 0)) {
  3238. /* SSLfatal() already called */
  3239. return WORK_ERROR;
  3240. }
  3241. return WORK_FINISHED_CONTINUE;
  3242. } else {
  3243. if (!s->s3->handshake_buffer) {
  3244. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3245. SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
  3246. ERR_R_INTERNAL_ERROR);
  3247. return WORK_ERROR;
  3248. }
  3249. /*
  3250. * For sigalgs freeze the handshake buffer. If we support
  3251. * extms we've done this already so this is a no-op
  3252. */
  3253. if (!ssl3_digest_cached_records(s, 1)) {
  3254. /* SSLfatal() already called */
  3255. return WORK_ERROR;
  3256. }
  3257. }
  3258. return WORK_FINISHED_CONTINUE;
  3259. }
  3260. MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt)
  3261. {
  3262. int i;
  3263. MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
  3264. X509 *x = NULL;
  3265. unsigned long l;
  3266. const unsigned char *certstart, *certbytes;
  3267. STACK_OF(X509) *sk = NULL;
  3268. PACKET spkt, context;
  3269. size_t chainidx;
  3270. SSL_SESSION *new_sess = NULL;
  3271. /*
  3272. * To get this far we must have read encrypted data from the client. We no
  3273. * longer tolerate unencrypted alerts. This value is ignored if less than
  3274. * TLSv1.3
  3275. */
  3276. s->statem.enc_read_state = ENC_READ_STATE_VALID;
  3277. if ((sk = sk_X509_new_null()) == NULL) {
  3278. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
  3279. ERR_R_MALLOC_FAILURE);
  3280. goto err;
  3281. }
  3282. if (SSL_IS_TLS13(s) && (!PACKET_get_length_prefixed_1(pkt, &context)
  3283. || (s->pha_context == NULL && PACKET_remaining(&context) != 0)
  3284. || (s->pha_context != NULL &&
  3285. !PACKET_equal(&context, s->pha_context, s->pha_context_len)))) {
  3286. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
  3287. SSL_R_INVALID_CONTEXT);
  3288. goto err;
  3289. }
  3290. if (!PACKET_get_length_prefixed_3(pkt, &spkt)
  3291. || PACKET_remaining(pkt) != 0) {
  3292. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
  3293. SSL_R_LENGTH_MISMATCH);
  3294. goto err;
  3295. }
  3296. for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) {
  3297. if (!PACKET_get_net_3(&spkt, &l)
  3298. || !PACKET_get_bytes(&spkt, &certbytes, l)) {
  3299. SSLfatal(s, SSL_AD_DECODE_ERROR,
  3300. SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
  3301. SSL_R_CERT_LENGTH_MISMATCH);
  3302. goto err;
  3303. }
  3304. certstart = certbytes;
  3305. x = d2i_X509(NULL, (const unsigned char **)&certbytes, l);
  3306. if (x == NULL) {
  3307. SSLfatal(s, SSL_AD_DECODE_ERROR,
  3308. SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
  3309. goto err;
  3310. }
  3311. if (certbytes != (certstart + l)) {
  3312. SSLfatal(s, SSL_AD_DECODE_ERROR,
  3313. SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
  3314. SSL_R_CERT_LENGTH_MISMATCH);
  3315. goto err;
  3316. }
  3317. if (SSL_IS_TLS13(s)) {
  3318. RAW_EXTENSION *rawexts = NULL;
  3319. PACKET extensions;
  3320. if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) {
  3321. SSLfatal(s, SSL_AD_DECODE_ERROR,
  3322. SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
  3323. SSL_R_BAD_LENGTH);
  3324. goto err;
  3325. }
  3326. if (!tls_collect_extensions(s, &extensions,
  3327. SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
  3328. NULL, chainidx == 0)
  3329. || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
  3330. rawexts, x, chainidx,
  3331. PACKET_remaining(&spkt) == 0)) {
  3332. OPENSSL_free(rawexts);
  3333. goto err;
  3334. }
  3335. OPENSSL_free(rawexts);
  3336. }
  3337. if (!sk_X509_push(sk, x)) {
  3338. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3339. SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
  3340. ERR_R_MALLOC_FAILURE);
  3341. goto err;
  3342. }
  3343. x = NULL;
  3344. }
  3345. if (sk_X509_num(sk) <= 0) {
  3346. /* TLS does not mind 0 certs returned */
  3347. if (s->version == SSL3_VERSION) {
  3348. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3349. SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
  3350. SSL_R_NO_CERTIFICATES_RETURNED);
  3351. goto err;
  3352. }
  3353. /* Fail for TLS only if we required a certificate */
  3354. else if ((s->verify_mode & SSL_VERIFY_PEER) &&
  3355. (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
  3356. SSLfatal(s, SSL_AD_CERTIFICATE_REQUIRED,
  3357. SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
  3358. SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  3359. goto err;
  3360. }
  3361. /* No client certificate so digest cached records */
  3362. if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
  3363. /* SSLfatal() already called */
  3364. goto err;
  3365. }
  3366. } else {
  3367. EVP_PKEY *pkey;
  3368. i = ssl_verify_cert_chain(s, sk);
  3369. if (i <= 0) {
  3370. SSLfatal(s, ssl_x509err2alert(s->verify_result),
  3371. SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
  3372. SSL_R_CERTIFICATE_VERIFY_FAILED);
  3373. goto err;
  3374. }
  3375. if (i > 1) {
  3376. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3377. SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, i);
  3378. goto err;
  3379. }
  3380. pkey = X509_get0_pubkey(sk_X509_value(sk, 0));
  3381. if (pkey == NULL) {
  3382. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3383. SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
  3384. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  3385. goto err;
  3386. }
  3387. }
  3388. /*
  3389. * Sessions must be immutable once they go into the session cache. Otherwise
  3390. * we can get multi-thread problems. Therefore we don't "update" sessions,
  3391. * we replace them with a duplicate. Here, we need to do this every time
  3392. * a new certificate is received via post-handshake authentication, as the
  3393. * session may have already gone into the session cache.
  3394. */
  3395. if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
  3396. if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
  3397. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3398. SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
  3399. ERR_R_MALLOC_FAILURE);
  3400. goto err;
  3401. }
  3402. SSL_SESSION_free(s->session);
  3403. s->session = new_sess;
  3404. }
  3405. X509_free(s->session->peer);
  3406. s->session->peer = sk_X509_shift(sk);
  3407. s->session->verify_result = s->verify_result;
  3408. sk_X509_pop_free(s->session->peer_chain, X509_free);
  3409. s->session->peer_chain = sk;
  3410. sk = NULL;
  3411. /*
  3412. * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
  3413. * message
  3414. */
  3415. if (SSL_IS_TLS13(s) && !ssl3_digest_cached_records(s, 1)) {
  3416. /* SSLfatal() already called */
  3417. goto err;
  3418. }
  3419. /*
  3420. * Inconsistency alert: cert_chain does *not* include the peer's own
  3421. * certificate, while we do include it in statem_clnt.c
  3422. */
  3423. /* Save the current hash state for when we receive the CertificateVerify */
  3424. if (SSL_IS_TLS13(s)) {
  3425. if (!ssl_handshake_hash(s, s->cert_verify_hash,
  3426. sizeof(s->cert_verify_hash),
  3427. &s->cert_verify_hash_len)) {
  3428. /* SSLfatal() already called */
  3429. goto err;
  3430. }
  3431. /* Resend session tickets */
  3432. s->sent_tickets = 0;
  3433. }
  3434. ret = MSG_PROCESS_CONTINUE_READING;
  3435. err:
  3436. X509_free(x);
  3437. sk_X509_pop_free(sk, X509_free);
  3438. return ret;
  3439. }
  3440. int tls_construct_server_certificate(SSL *s, WPACKET *pkt)
  3441. {
  3442. CERT_PKEY *cpk = s->s3->tmp.cert;
  3443. if (cpk == NULL) {
  3444. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3445. SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
  3446. return 0;
  3447. }
  3448. /*
  3449. * In TLSv1.3 the certificate chain is always preceded by a 0 length context
  3450. * for the server Certificate message
  3451. */
  3452. if (SSL_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0)) {
  3453. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3454. SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
  3455. return 0;
  3456. }
  3457. if (!ssl3_output_cert_chain(s, pkt, cpk)) {
  3458. /* SSLfatal() already called */
  3459. return 0;
  3460. }
  3461. return 1;
  3462. }
  3463. static int create_ticket_prequel(SSL *s, WPACKET *pkt, uint32_t age_add,
  3464. unsigned char *tick_nonce)
  3465. {
  3466. uint32_t timeout = (uint32_t)s->session->timeout;
  3467. /*
  3468. * Ticket lifetime hint:
  3469. * In TLSv1.3 we reset the "time" field above, and always specify the
  3470. * timeout, limited to a 1 week period per RFC8446.
  3471. * For TLSv1.2 this is advisory only and we leave this unspecified for
  3472. * resumed session (for simplicity).
  3473. */
  3474. #define ONE_WEEK_SEC (7 * 24 * 60 * 60)
  3475. if (SSL_IS_TLS13(s)) {
  3476. if (s->session->timeout > ONE_WEEK_SEC)
  3477. timeout = ONE_WEEK_SEC;
  3478. } else if (s->hit)
  3479. timeout = 0;
  3480. if (!WPACKET_put_bytes_u32(pkt, timeout)) {
  3481. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CREATE_TICKET_PREQUEL,
  3482. ERR_R_INTERNAL_ERROR);
  3483. return 0;
  3484. }
  3485. if (SSL_IS_TLS13(s)) {
  3486. if (!WPACKET_put_bytes_u32(pkt, age_add)
  3487. || !WPACKET_sub_memcpy_u8(pkt, tick_nonce, TICKET_NONCE_SIZE)) {
  3488. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CREATE_TICKET_PREQUEL,
  3489. ERR_R_INTERNAL_ERROR);
  3490. return 0;
  3491. }
  3492. }
  3493. /* Start the sub-packet for the actual ticket data */
  3494. if (!WPACKET_start_sub_packet_u16(pkt)) {
  3495. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CREATE_TICKET_PREQUEL,
  3496. ERR_R_INTERNAL_ERROR);
  3497. return 0;
  3498. }
  3499. return 1;
  3500. }
  3501. static int construct_stateless_ticket(SSL *s, WPACKET *pkt, uint32_t age_add,
  3502. unsigned char *tick_nonce)
  3503. {
  3504. unsigned char *senc = NULL;
  3505. EVP_CIPHER_CTX *ctx = NULL;
  3506. HMAC_CTX *hctx = NULL;
  3507. unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2;
  3508. const unsigned char *const_p;
  3509. int len, slen_full, slen, lenfinal;
  3510. SSL_SESSION *sess;
  3511. unsigned int hlen;
  3512. SSL_CTX *tctx = s->session_ctx;
  3513. unsigned char iv[EVP_MAX_IV_LENGTH];
  3514. unsigned char key_name[TLSEXT_KEYNAME_LENGTH];
  3515. int iv_len, ok = 0;
  3516. size_t macoffset, macendoffset;
  3517. /* get session encoding length */
  3518. slen_full = i2d_SSL_SESSION(s->session, NULL);
  3519. /*
  3520. * Some length values are 16 bits, so forget it if session is too
  3521. * long
  3522. */
  3523. if (slen_full == 0 || slen_full > 0xFF00) {
  3524. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
  3525. ERR_R_INTERNAL_ERROR);
  3526. goto err;
  3527. }
  3528. senc = OPENSSL_malloc(slen_full);
  3529. if (senc == NULL) {
  3530. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3531. SSL_F_CONSTRUCT_STATELESS_TICKET, ERR_R_MALLOC_FAILURE);
  3532. goto err;
  3533. }
  3534. ctx = EVP_CIPHER_CTX_new();
  3535. hctx = HMAC_CTX_new();
  3536. if (ctx == NULL || hctx == NULL) {
  3537. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
  3538. ERR_R_MALLOC_FAILURE);
  3539. goto err;
  3540. }
  3541. p = senc;
  3542. if (!i2d_SSL_SESSION(s->session, &p)) {
  3543. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
  3544. ERR_R_INTERNAL_ERROR);
  3545. goto err;
  3546. }
  3547. /*
  3548. * create a fresh copy (not shared with other threads) to clean up
  3549. */
  3550. const_p = senc;
  3551. sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
  3552. if (sess == NULL) {
  3553. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
  3554. ERR_R_INTERNAL_ERROR);
  3555. goto err;
  3556. }
  3557. slen = i2d_SSL_SESSION(sess, NULL);
  3558. if (slen == 0 || slen > slen_full) {
  3559. /* shouldn't ever happen */
  3560. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
  3561. ERR_R_INTERNAL_ERROR);
  3562. SSL_SESSION_free(sess);
  3563. goto err;
  3564. }
  3565. p = senc;
  3566. if (!i2d_SSL_SESSION(sess, &p)) {
  3567. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
  3568. ERR_R_INTERNAL_ERROR);
  3569. SSL_SESSION_free(sess);
  3570. goto err;
  3571. }
  3572. SSL_SESSION_free(sess);
  3573. /*
  3574. * Initialize HMAC and cipher contexts. If callback present it does
  3575. * all the work otherwise use generated values from parent ctx.
  3576. */
  3577. if (tctx->ext.ticket_key_cb) {
  3578. /* if 0 is returned, write an empty ticket */
  3579. int ret = tctx->ext.ticket_key_cb(s, key_name, iv, ctx,
  3580. hctx, 1);
  3581. if (ret == 0) {
  3582. /* Put timeout and length */
  3583. if (!WPACKET_put_bytes_u32(pkt, 0)
  3584. || !WPACKET_put_bytes_u16(pkt, 0)) {
  3585. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3586. SSL_F_CONSTRUCT_STATELESS_TICKET,
  3587. ERR_R_INTERNAL_ERROR);
  3588. goto err;
  3589. }
  3590. OPENSSL_free(senc);
  3591. EVP_CIPHER_CTX_free(ctx);
  3592. HMAC_CTX_free(hctx);
  3593. return 1;
  3594. }
  3595. if (ret < 0) {
  3596. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
  3597. SSL_R_CALLBACK_FAILED);
  3598. goto err;
  3599. }
  3600. iv_len = EVP_CIPHER_CTX_iv_length(ctx);
  3601. } else {
  3602. const EVP_CIPHER *cipher = EVP_aes_256_cbc();
  3603. iv_len = EVP_CIPHER_iv_length(cipher);
  3604. if (RAND_bytes(iv, iv_len) <= 0
  3605. || !EVP_EncryptInit_ex(ctx, cipher, NULL,
  3606. tctx->ext.secure->tick_aes_key, iv)
  3607. || !HMAC_Init_ex(hctx, tctx->ext.secure->tick_hmac_key,
  3608. sizeof(tctx->ext.secure->tick_hmac_key),
  3609. EVP_sha256(), NULL)) {
  3610. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
  3611. ERR_R_INTERNAL_ERROR);
  3612. goto err;
  3613. }
  3614. memcpy(key_name, tctx->ext.tick_key_name,
  3615. sizeof(tctx->ext.tick_key_name));
  3616. }
  3617. if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
  3618. /* SSLfatal() already called */
  3619. goto err;
  3620. }
  3621. if (!WPACKET_get_total_written(pkt, &macoffset)
  3622. /* Output key name */
  3623. || !WPACKET_memcpy(pkt, key_name, sizeof(key_name))
  3624. /* output IV */
  3625. || !WPACKET_memcpy(pkt, iv, iv_len)
  3626. || !WPACKET_reserve_bytes(pkt, slen + EVP_MAX_BLOCK_LENGTH,
  3627. &encdata1)
  3628. /* Encrypt session data */
  3629. || !EVP_EncryptUpdate(ctx, encdata1, &len, senc, slen)
  3630. || !WPACKET_allocate_bytes(pkt, len, &encdata2)
  3631. || encdata1 != encdata2
  3632. || !EVP_EncryptFinal(ctx, encdata1 + len, &lenfinal)
  3633. || !WPACKET_allocate_bytes(pkt, lenfinal, &encdata2)
  3634. || encdata1 + len != encdata2
  3635. || len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH
  3636. || !WPACKET_get_total_written(pkt, &macendoffset)
  3637. || !HMAC_Update(hctx,
  3638. (unsigned char *)s->init_buf->data + macoffset,
  3639. macendoffset - macoffset)
  3640. || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1)
  3641. || !HMAC_Final(hctx, macdata1, &hlen)
  3642. || hlen > EVP_MAX_MD_SIZE
  3643. || !WPACKET_allocate_bytes(pkt, hlen, &macdata2)
  3644. || macdata1 != macdata2) {
  3645. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3646. SSL_F_CONSTRUCT_STATELESS_TICKET, ERR_R_INTERNAL_ERROR);
  3647. goto err;
  3648. }
  3649. /* Close the sub-packet created by create_ticket_prequel() */
  3650. if (!WPACKET_close(pkt)) {
  3651. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
  3652. ERR_R_INTERNAL_ERROR);
  3653. goto err;
  3654. }
  3655. ok = 1;
  3656. err:
  3657. OPENSSL_free(senc);
  3658. EVP_CIPHER_CTX_free(ctx);
  3659. HMAC_CTX_free(hctx);
  3660. return ok;
  3661. }
  3662. static int construct_stateful_ticket(SSL *s, WPACKET *pkt, uint32_t age_add,
  3663. unsigned char *tick_nonce)
  3664. {
  3665. if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
  3666. /* SSLfatal() already called */
  3667. return 0;
  3668. }
  3669. if (!WPACKET_memcpy(pkt, s->session->session_id,
  3670. s->session->session_id_length)
  3671. || !WPACKET_close(pkt)) {
  3672. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATEFUL_TICKET,
  3673. ERR_R_INTERNAL_ERROR);
  3674. return 0;
  3675. }
  3676. return 1;
  3677. }
  3678. int tls_construct_new_session_ticket(SSL *s, WPACKET *pkt)
  3679. {
  3680. SSL_CTX *tctx = s->session_ctx;
  3681. unsigned char tick_nonce[TICKET_NONCE_SIZE];
  3682. union {
  3683. unsigned char age_add_c[sizeof(uint32_t)];
  3684. uint32_t age_add;
  3685. } age_add_u;
  3686. age_add_u.age_add = 0;
  3687. if (SSL_IS_TLS13(s)) {
  3688. size_t i, hashlen;
  3689. uint64_t nonce;
  3690. static const unsigned char nonce_label[] = "resumption";
  3691. const EVP_MD *md = ssl_handshake_md(s);
  3692. int hashleni = EVP_MD_size(md);
  3693. /* Ensure cast to size_t is safe */
  3694. if (!ossl_assert(hashleni >= 0)) {
  3695. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3696. SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
  3697. ERR_R_INTERNAL_ERROR);
  3698. goto err;
  3699. }
  3700. hashlen = (size_t)hashleni;
  3701. /*
  3702. * If we already sent one NewSessionTicket, or we resumed then
  3703. * s->session may already be in a cache and so we must not modify it.
  3704. * Instead we need to take a copy of it and modify that.
  3705. */
  3706. if (s->sent_tickets != 0 || s->hit) {
  3707. SSL_SESSION *new_sess = ssl_session_dup(s->session, 0);
  3708. if (new_sess == NULL) {
  3709. /* SSLfatal already called */
  3710. goto err;
  3711. }
  3712. SSL_SESSION_free(s->session);
  3713. s->session = new_sess;
  3714. }
  3715. if (!ssl_generate_session_id(s, s->session)) {
  3716. /* SSLfatal() already called */
  3717. goto err;
  3718. }
  3719. if (RAND_bytes(age_add_u.age_add_c, sizeof(age_add_u)) <= 0) {
  3720. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3721. SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
  3722. ERR_R_INTERNAL_ERROR);
  3723. goto err;
  3724. }
  3725. s->session->ext.tick_age_add = age_add_u.age_add;
  3726. nonce = s->next_ticket_nonce;
  3727. for (i = TICKET_NONCE_SIZE; i > 0; i--) {
  3728. tick_nonce[i - 1] = (unsigned char)(nonce & 0xff);
  3729. nonce >>= 8;
  3730. }
  3731. if (!tls13_hkdf_expand(s, md, s->resumption_master_secret,
  3732. nonce_label,
  3733. sizeof(nonce_label) - 1,
  3734. tick_nonce,
  3735. TICKET_NONCE_SIZE,
  3736. s->session->master_key,
  3737. hashlen, 1)) {
  3738. /* SSLfatal() already called */
  3739. goto err;
  3740. }
  3741. s->session->master_key_length = hashlen;
  3742. s->session->time = (long)time(NULL);
  3743. if (s->s3->alpn_selected != NULL) {
  3744. OPENSSL_free(s->session->ext.alpn_selected);
  3745. s->session->ext.alpn_selected =
  3746. OPENSSL_memdup(s->s3->alpn_selected, s->s3->alpn_selected_len);
  3747. if (s->session->ext.alpn_selected == NULL) {
  3748. s->session->ext.alpn_selected_len = 0;
  3749. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3750. SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
  3751. ERR_R_MALLOC_FAILURE);
  3752. goto err;
  3753. }
  3754. s->session->ext.alpn_selected_len = s->s3->alpn_selected_len;
  3755. }
  3756. s->session->ext.max_early_data = s->max_early_data;
  3757. }
  3758. if (tctx->generate_ticket_cb != NULL &&
  3759. tctx->generate_ticket_cb(s, tctx->ticket_cb_data) == 0) {
  3760. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3761. SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
  3762. ERR_R_INTERNAL_ERROR);
  3763. goto err;
  3764. }
  3765. /*
  3766. * If we are using anti-replay protection then we behave as if
  3767. * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
  3768. * is no point in using full stateless tickets.
  3769. */
  3770. if (SSL_IS_TLS13(s)
  3771. && ((s->options & SSL_OP_NO_TICKET) != 0
  3772. || (s->max_early_data > 0
  3773. && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))) {
  3774. if (!construct_stateful_ticket(s, pkt, age_add_u.age_add, tick_nonce)) {
  3775. /* SSLfatal() already called */
  3776. goto err;
  3777. }
  3778. } else if (!construct_stateless_ticket(s, pkt, age_add_u.age_add,
  3779. tick_nonce)) {
  3780. /* SSLfatal() already called */
  3781. goto err;
  3782. }
  3783. if (SSL_IS_TLS13(s)) {
  3784. if (!tls_construct_extensions(s, pkt,
  3785. SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
  3786. NULL, 0)) {
  3787. /* SSLfatal() already called */
  3788. goto err;
  3789. }
  3790. /*
  3791. * Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets|
  3792. * gets reset to 0 if we send more tickets following a post-handshake
  3793. * auth, but |next_ticket_nonce| does not. If we're sending extra
  3794. * tickets, decrement the count of pending extra tickets.
  3795. */
  3796. s->sent_tickets++;
  3797. s->next_ticket_nonce++;
  3798. if (s->ext.extra_tickets_expected > 0)
  3799. s->ext.extra_tickets_expected--;
  3800. ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
  3801. }
  3802. return 1;
  3803. err:
  3804. return 0;
  3805. }
  3806. /*
  3807. * In TLSv1.3 this is called from the extensions code, otherwise it is used to
  3808. * create a separate message. Returns 1 on success or 0 on failure.
  3809. */
  3810. int tls_construct_cert_status_body(SSL *s, WPACKET *pkt)
  3811. {
  3812. if (!WPACKET_put_bytes_u8(pkt, s->ext.status_type)
  3813. || !WPACKET_sub_memcpy_u24(pkt, s->ext.ocsp.resp,
  3814. s->ext.ocsp.resp_len)) {
  3815. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_STATUS_BODY,
  3816. ERR_R_INTERNAL_ERROR);
  3817. return 0;
  3818. }
  3819. return 1;
  3820. }
  3821. int tls_construct_cert_status(SSL *s, WPACKET *pkt)
  3822. {
  3823. if (!tls_construct_cert_status_body(s, pkt)) {
  3824. /* SSLfatal() already called */
  3825. return 0;
  3826. }
  3827. return 1;
  3828. }
  3829. #ifndef OPENSSL_NO_NEXTPROTONEG
  3830. /*
  3831. * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
  3832. * It sets the next_proto member in s if found
  3833. */
  3834. MSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt)
  3835. {
  3836. PACKET next_proto, padding;
  3837. size_t next_proto_len;
  3838. /*-
  3839. * The payload looks like:
  3840. * uint8 proto_len;
  3841. * uint8 proto[proto_len];
  3842. * uint8 padding_len;
  3843. * uint8 padding[padding_len];
  3844. */
  3845. if (!PACKET_get_length_prefixed_1(pkt, &next_proto)
  3846. || !PACKET_get_length_prefixed_1(pkt, &padding)
  3847. || PACKET_remaining(pkt) > 0) {
  3848. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_NEXT_PROTO,
  3849. SSL_R_LENGTH_MISMATCH);
  3850. return MSG_PROCESS_ERROR;
  3851. }
  3852. if (!PACKET_memdup(&next_proto, &s->ext.npn, &next_proto_len)) {
  3853. s->ext.npn_len = 0;
  3854. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_NEXT_PROTO,
  3855. ERR_R_INTERNAL_ERROR);
  3856. return MSG_PROCESS_ERROR;
  3857. }
  3858. s->ext.npn_len = (unsigned char)next_proto_len;
  3859. return MSG_PROCESS_CONTINUE_READING;
  3860. }
  3861. #endif
  3862. static int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt)
  3863. {
  3864. if (!tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
  3865. NULL, 0)) {
  3866. /* SSLfatal() already called */
  3867. return 0;
  3868. }
  3869. return 1;
  3870. }
  3871. MSG_PROCESS_RETURN tls_process_end_of_early_data(SSL *s, PACKET *pkt)
  3872. {
  3873. if (PACKET_remaining(pkt) != 0) {
  3874. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_END_OF_EARLY_DATA,
  3875. SSL_R_LENGTH_MISMATCH);
  3876. return MSG_PROCESS_ERROR;
  3877. }
  3878. if (s->early_data_state != SSL_EARLY_DATA_READING
  3879. && s->early_data_state != SSL_EARLY_DATA_READ_RETRY) {
  3880. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_END_OF_EARLY_DATA,
  3881. ERR_R_INTERNAL_ERROR);
  3882. return MSG_PROCESS_ERROR;
  3883. }
  3884. /*
  3885. * EndOfEarlyData signals a key change so the end of the message must be on
  3886. * a record boundary.
  3887. */
  3888. if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
  3889. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
  3890. SSL_F_TLS_PROCESS_END_OF_EARLY_DATA,
  3891. SSL_R_NOT_ON_RECORD_BOUNDARY);
  3892. return MSG_PROCESS_ERROR;
  3893. }
  3894. s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
  3895. if (!s->method->ssl3_enc->change_cipher_state(s,
  3896. SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) {
  3897. /* SSLfatal() already called */
  3898. return MSG_PROCESS_ERROR;
  3899. }
  3900. return MSG_PROCESS_CONTINUE_READING;
  3901. }