statem_srvr.c 141 KB

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