2
0

statem_srvr.c 145 KB

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