statem_srvr.c 140 KB

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