statem_srvr.c 141 KB

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