statem_srvr.c 141 KB

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