statem_clnt.c 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181
  1. /*
  2. * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. * Copyright 2005 Nokia. All rights reserved.
  5. *
  6. * Licensed under the Apache License 2.0 (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. #include <stdio.h>
  12. #include <time.h>
  13. #include <assert.h>
  14. #include "../ssl_local.h"
  15. #include "statem_local.h"
  16. #include <openssl/buffer.h>
  17. #include <openssl/rand.h>
  18. #include <openssl/objects.h>
  19. #include <openssl/evp.h>
  20. #include <openssl/md5.h>
  21. #include <openssl/dh.h>
  22. #include <openssl/rsa.h>
  23. #include <openssl/bn.h>
  24. #include <openssl/engine.h>
  25. #include <openssl/trace.h>
  26. #include <openssl/core_names.h>
  27. #include <openssl/param_build.h>
  28. #include "internal/cryptlib.h"
  29. static MSG_PROCESS_RETURN tls_process_as_hello_retry_request(SSL_CONNECTION *s,
  30. PACKET *pkt);
  31. static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL_CONNECTION *s,
  32. PACKET *pkt);
  33. static ossl_inline int cert_req_allowed(SSL_CONNECTION *s);
  34. static int key_exchange_expected(SSL_CONNECTION *s);
  35. static int ssl_cipher_list_to_bytes(SSL_CONNECTION *s, STACK_OF(SSL_CIPHER) *sk,
  36. WPACKET *pkt);
  37. static ossl_inline int received_server_cert(SSL_CONNECTION *sc)
  38. {
  39. return sc->session->peer_rpk != NULL || sc->session->peer != NULL;
  40. }
  41. /*
  42. * Is a CertificateRequest message allowed at the moment or not?
  43. *
  44. * Return values are:
  45. * 1: Yes
  46. * 0: No
  47. */
  48. static ossl_inline int cert_req_allowed(SSL_CONNECTION *s)
  49. {
  50. /* TLS does not like anon-DH with client cert */
  51. if ((s->version > SSL3_VERSION
  52. && (s->s3.tmp.new_cipher->algorithm_auth & SSL_aNULL))
  53. || (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK)))
  54. return 0;
  55. return 1;
  56. }
  57. /*
  58. * Should we expect the ServerKeyExchange message or not?
  59. *
  60. * Return values are:
  61. * 1: Yes
  62. * 0: No
  63. */
  64. static int key_exchange_expected(SSL_CONNECTION *s)
  65. {
  66. long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
  67. /*
  68. * Can't skip server key exchange if this is an ephemeral
  69. * ciphersuite or for SRP
  70. */
  71. if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK
  72. | SSL_kSRP)) {
  73. return 1;
  74. }
  75. return 0;
  76. }
  77. /*
  78. * ossl_statem_client_read_transition() encapsulates the logic for the allowed
  79. * handshake state transitions when a TLS1.3 client is reading messages from the
  80. * server. The message type that the server has sent is provided in |mt|. The
  81. * current state is in |s->statem.hand_state|.
  82. *
  83. * Return values are 1 for success (transition allowed) and 0 on error
  84. * (transition not allowed)
  85. */
  86. static int ossl_statem_client13_read_transition(SSL_CONNECTION *s, int mt)
  87. {
  88. OSSL_STATEM *st = &s->statem;
  89. /*
  90. * Note: There is no case for TLS_ST_CW_CLNT_HELLO, because we haven't
  91. * yet negotiated TLSv1.3 at that point so that is handled by
  92. * ossl_statem_client_read_transition()
  93. */
  94. switch (st->hand_state) {
  95. default:
  96. break;
  97. case TLS_ST_CW_CLNT_HELLO:
  98. /*
  99. * This must a ClientHello following a HelloRetryRequest, so the only
  100. * thing we can get now is a ServerHello.
  101. */
  102. if (mt == SSL3_MT_SERVER_HELLO) {
  103. st->hand_state = TLS_ST_CR_SRVR_HELLO;
  104. return 1;
  105. }
  106. break;
  107. case TLS_ST_CR_SRVR_HELLO:
  108. if (mt == SSL3_MT_ENCRYPTED_EXTENSIONS) {
  109. st->hand_state = TLS_ST_CR_ENCRYPTED_EXTENSIONS;
  110. return 1;
  111. }
  112. break;
  113. case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
  114. if (s->hit) {
  115. if (mt == SSL3_MT_FINISHED) {
  116. st->hand_state = TLS_ST_CR_FINISHED;
  117. return 1;
  118. }
  119. } else {
  120. if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
  121. st->hand_state = TLS_ST_CR_CERT_REQ;
  122. return 1;
  123. }
  124. if (mt == SSL3_MT_CERTIFICATE) {
  125. st->hand_state = TLS_ST_CR_CERT;
  126. return 1;
  127. }
  128. #ifndef OPENSSL_NO_COMP_ALG
  129. if (mt == SSL3_MT_COMPRESSED_CERTIFICATE
  130. && s->ext.compress_certificate_sent) {
  131. st->hand_state = TLS_ST_CR_COMP_CERT;
  132. return 1;
  133. }
  134. #endif
  135. }
  136. break;
  137. case TLS_ST_CR_CERT_REQ:
  138. if (mt == SSL3_MT_CERTIFICATE) {
  139. st->hand_state = TLS_ST_CR_CERT;
  140. return 1;
  141. }
  142. #ifndef OPENSSL_NO_COMP_ALG
  143. if (mt == SSL3_MT_COMPRESSED_CERTIFICATE
  144. && s->ext.compress_certificate_sent) {
  145. st->hand_state = TLS_ST_CR_COMP_CERT;
  146. return 1;
  147. }
  148. #endif
  149. break;
  150. case TLS_ST_CR_CERT:
  151. case TLS_ST_CR_COMP_CERT:
  152. if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
  153. st->hand_state = TLS_ST_CR_CERT_VRFY;
  154. return 1;
  155. }
  156. break;
  157. case TLS_ST_CR_CERT_VRFY:
  158. if (mt == SSL3_MT_FINISHED) {
  159. st->hand_state = TLS_ST_CR_FINISHED;
  160. return 1;
  161. }
  162. break;
  163. case TLS_ST_OK:
  164. if (mt == SSL3_MT_NEWSESSION_TICKET) {
  165. st->hand_state = TLS_ST_CR_SESSION_TICKET;
  166. return 1;
  167. }
  168. if (mt == SSL3_MT_KEY_UPDATE && !SSL_IS_QUIC_HANDSHAKE(s)) {
  169. st->hand_state = TLS_ST_CR_KEY_UPDATE;
  170. return 1;
  171. }
  172. if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
  173. #if DTLS_MAX_VERSION_INTERNAL != DTLS1_2_VERSION
  174. /* Restore digest for PHA before adding message.*/
  175. # error Internal DTLS version error
  176. #endif
  177. if (!SSL_CONNECTION_IS_DTLS(s)
  178. && s->post_handshake_auth == SSL_PHA_EXT_SENT) {
  179. s->post_handshake_auth = SSL_PHA_REQUESTED;
  180. /*
  181. * In TLS, this is called before the message is added to the
  182. * digest. In DTLS, this is expected to be called after adding
  183. * to the digest. Either move the digest restore, or add the
  184. * message here after the swap, or do it after the clientFinished?
  185. */
  186. if (!tls13_restore_handshake_digest_for_pha(s)) {
  187. /* SSLfatal() already called */
  188. return 0;
  189. }
  190. st->hand_state = TLS_ST_CR_CERT_REQ;
  191. return 1;
  192. }
  193. }
  194. break;
  195. }
  196. /* No valid transition found */
  197. return 0;
  198. }
  199. /*
  200. * ossl_statem_client_read_transition() encapsulates the logic for the allowed
  201. * handshake state transitions when the client is reading messages from the
  202. * server. The message type that the server has sent is provided in |mt|. The
  203. * current state is in |s->statem.hand_state|.
  204. *
  205. * Return values are 1 for success (transition allowed) and 0 on error
  206. * (transition not allowed)
  207. */
  208. int ossl_statem_client_read_transition(SSL_CONNECTION *s, int mt)
  209. {
  210. OSSL_STATEM *st = &s->statem;
  211. int ske_expected;
  212. /*
  213. * Note that after writing the first ClientHello we don't know what version
  214. * we are going to negotiate yet, so we don't take this branch until later.
  215. */
  216. if (SSL_CONNECTION_IS_TLS13(s)) {
  217. if (!ossl_statem_client13_read_transition(s, mt))
  218. goto err;
  219. return 1;
  220. }
  221. switch (st->hand_state) {
  222. default:
  223. break;
  224. case TLS_ST_CW_CLNT_HELLO:
  225. if (mt == SSL3_MT_SERVER_HELLO) {
  226. st->hand_state = TLS_ST_CR_SRVR_HELLO;
  227. return 1;
  228. }
  229. if (SSL_CONNECTION_IS_DTLS(s)) {
  230. if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
  231. st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
  232. return 1;
  233. }
  234. }
  235. break;
  236. case TLS_ST_EARLY_DATA:
  237. /*
  238. * We've not actually selected TLSv1.3 yet, but we have sent early
  239. * data. The only thing allowed now is a ServerHello or a
  240. * HelloRetryRequest.
  241. */
  242. if (mt == SSL3_MT_SERVER_HELLO) {
  243. st->hand_state = TLS_ST_CR_SRVR_HELLO;
  244. return 1;
  245. }
  246. break;
  247. case TLS_ST_CR_SRVR_HELLO:
  248. if (s->hit) {
  249. if (s->ext.ticket_expected) {
  250. if (mt == SSL3_MT_NEWSESSION_TICKET) {
  251. st->hand_state = TLS_ST_CR_SESSION_TICKET;
  252. return 1;
  253. }
  254. } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  255. st->hand_state = TLS_ST_CR_CHANGE;
  256. return 1;
  257. }
  258. } else {
  259. if (SSL_CONNECTION_IS_DTLS(s)
  260. && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
  261. st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
  262. return 1;
  263. } else if (s->version >= TLS1_VERSION
  264. && s->ext.session_secret_cb != NULL
  265. && s->session->ext.tick != NULL
  266. && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  267. /*
  268. * Normally, we can tell if the server is resuming the session
  269. * from the session ID. EAP-FAST (RFC 4851), however, relies on
  270. * the next server message after the ServerHello to determine if
  271. * the server is resuming.
  272. */
  273. s->hit = 1;
  274. st->hand_state = TLS_ST_CR_CHANGE;
  275. return 1;
  276. } else if (!(s->s3.tmp.new_cipher->algorithm_auth
  277. & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
  278. if (mt == SSL3_MT_CERTIFICATE) {
  279. st->hand_state = TLS_ST_CR_CERT;
  280. return 1;
  281. }
  282. } else {
  283. ske_expected = key_exchange_expected(s);
  284. /* SKE is optional for some PSK ciphersuites */
  285. if (ske_expected
  286. || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)
  287. && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
  288. if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
  289. st->hand_state = TLS_ST_CR_KEY_EXCH;
  290. return 1;
  291. }
  292. } else if (mt == SSL3_MT_CERTIFICATE_REQUEST
  293. && cert_req_allowed(s)) {
  294. st->hand_state = TLS_ST_CR_CERT_REQ;
  295. return 1;
  296. } else if (mt == SSL3_MT_SERVER_DONE) {
  297. st->hand_state = TLS_ST_CR_SRVR_DONE;
  298. return 1;
  299. }
  300. }
  301. }
  302. break;
  303. case TLS_ST_CR_CERT:
  304. case TLS_ST_CR_COMP_CERT:
  305. /*
  306. * The CertificateStatus message is optional even if
  307. * |ext.status_expected| is set
  308. */
  309. if (s->ext.status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) {
  310. st->hand_state = TLS_ST_CR_CERT_STATUS;
  311. return 1;
  312. }
  313. /* Fall through */
  314. case TLS_ST_CR_CERT_STATUS:
  315. ske_expected = key_exchange_expected(s);
  316. /* SKE is optional for some PSK ciphersuites */
  317. if (ske_expected || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)
  318. && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
  319. if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
  320. st->hand_state = TLS_ST_CR_KEY_EXCH;
  321. return 1;
  322. }
  323. goto err;
  324. }
  325. /* Fall through */
  326. case TLS_ST_CR_KEY_EXCH:
  327. if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
  328. if (cert_req_allowed(s)) {
  329. st->hand_state = TLS_ST_CR_CERT_REQ;
  330. return 1;
  331. }
  332. goto err;
  333. }
  334. /* Fall through */
  335. case TLS_ST_CR_CERT_REQ:
  336. if (mt == SSL3_MT_SERVER_DONE) {
  337. st->hand_state = TLS_ST_CR_SRVR_DONE;
  338. return 1;
  339. }
  340. break;
  341. case TLS_ST_CW_FINISHED:
  342. if (s->ext.ticket_expected) {
  343. if (mt == SSL3_MT_NEWSESSION_TICKET) {
  344. st->hand_state = TLS_ST_CR_SESSION_TICKET;
  345. return 1;
  346. }
  347. } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  348. st->hand_state = TLS_ST_CR_CHANGE;
  349. return 1;
  350. }
  351. break;
  352. case TLS_ST_CR_SESSION_TICKET:
  353. if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  354. st->hand_state = TLS_ST_CR_CHANGE;
  355. return 1;
  356. }
  357. break;
  358. case TLS_ST_CR_CHANGE:
  359. if (mt == SSL3_MT_FINISHED) {
  360. st->hand_state = TLS_ST_CR_FINISHED;
  361. return 1;
  362. }
  363. break;
  364. case TLS_ST_OK:
  365. if (mt == SSL3_MT_HELLO_REQUEST) {
  366. st->hand_state = TLS_ST_CR_HELLO_REQ;
  367. return 1;
  368. }
  369. break;
  370. }
  371. err:
  372. /* No valid transition found */
  373. if (SSL_CONNECTION_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  374. BIO *rbio;
  375. /*
  376. * CCS messages don't have a message sequence number so this is probably
  377. * because of an out-of-order CCS. We'll just drop it.
  378. */
  379. s->init_num = 0;
  380. s->rwstate = SSL_READING;
  381. rbio = SSL_get_rbio(SSL_CONNECTION_GET_SSL(s));
  382. BIO_clear_retry_flags(rbio);
  383. BIO_set_retry_read(rbio);
  384. return 0;
  385. }
  386. SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
  387. return 0;
  388. }
  389. static int do_compressed_cert(SSL_CONNECTION *sc)
  390. {
  391. /* If we negotiated RPK, we won't try to compress it */
  392. return sc->ext.client_cert_type == TLSEXT_cert_type_x509
  393. && sc->ext.compress_certificate_from_peer[0] != TLSEXT_comp_cert_none;
  394. }
  395. /*
  396. * ossl_statem_client13_write_transition() works out what handshake state to
  397. * move to next when the TLSv1.3 client is writing messages to be sent to the
  398. * server.
  399. */
  400. static WRITE_TRAN ossl_statem_client13_write_transition(SSL_CONNECTION *s)
  401. {
  402. OSSL_STATEM *st = &s->statem;
  403. /*
  404. * Note: There are no cases for TLS_ST_BEFORE because we haven't negotiated
  405. * TLSv1.3 yet at that point. They are handled by
  406. * ossl_statem_client_write_transition().
  407. */
  408. switch (st->hand_state) {
  409. default:
  410. /* Shouldn't happen */
  411. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  412. return WRITE_TRAN_ERROR;
  413. case TLS_ST_CR_CERT_REQ:
  414. if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
  415. if (do_compressed_cert(s))
  416. st->hand_state = TLS_ST_CW_COMP_CERT;
  417. else
  418. st->hand_state = TLS_ST_CW_CERT;
  419. return WRITE_TRAN_CONTINUE;
  420. }
  421. /*
  422. * We should only get here if we received a CertificateRequest after
  423. * we already sent close_notify
  424. */
  425. if (!ossl_assert((s->shutdown & SSL_SENT_SHUTDOWN) != 0)) {
  426. /* Shouldn't happen - same as default case */
  427. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  428. return WRITE_TRAN_ERROR;
  429. }
  430. st->hand_state = TLS_ST_OK;
  431. return WRITE_TRAN_CONTINUE;
  432. case TLS_ST_CR_FINISHED:
  433. if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY
  434. || s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING)
  435. st->hand_state = TLS_ST_PENDING_EARLY_DATA_END;
  436. else if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
  437. && s->hello_retry_request == SSL_HRR_NONE)
  438. st->hand_state = TLS_ST_CW_CHANGE;
  439. else if (s->s3.tmp.cert_req == 0)
  440. st->hand_state = TLS_ST_CW_FINISHED;
  441. else if (do_compressed_cert(s))
  442. st->hand_state = TLS_ST_CW_COMP_CERT;
  443. else
  444. st->hand_state = TLS_ST_CW_CERT;
  445. s->ts_msg_read = ossl_time_now();
  446. return WRITE_TRAN_CONTINUE;
  447. case TLS_ST_PENDING_EARLY_DATA_END:
  448. if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
  449. st->hand_state = TLS_ST_CW_END_OF_EARLY_DATA;
  450. return WRITE_TRAN_CONTINUE;
  451. }
  452. /* Fall through */
  453. case TLS_ST_CW_END_OF_EARLY_DATA:
  454. case TLS_ST_CW_CHANGE:
  455. if (s->s3.tmp.cert_req == 0)
  456. st->hand_state = TLS_ST_CW_FINISHED;
  457. else if (do_compressed_cert(s))
  458. st->hand_state = TLS_ST_CW_COMP_CERT;
  459. else
  460. st->hand_state = TLS_ST_CW_CERT;
  461. return WRITE_TRAN_CONTINUE;
  462. case TLS_ST_CW_COMP_CERT:
  463. case TLS_ST_CW_CERT:
  464. /* If a non-empty Certificate we also send CertificateVerify */
  465. st->hand_state = (s->s3.tmp.cert_req == 1) ? TLS_ST_CW_CERT_VRFY
  466. : TLS_ST_CW_FINISHED;
  467. return WRITE_TRAN_CONTINUE;
  468. case TLS_ST_CW_CERT_VRFY:
  469. st->hand_state = TLS_ST_CW_FINISHED;
  470. return WRITE_TRAN_CONTINUE;
  471. case TLS_ST_CR_KEY_UPDATE:
  472. case TLS_ST_CW_KEY_UPDATE:
  473. case TLS_ST_CR_SESSION_TICKET:
  474. case TLS_ST_CW_FINISHED:
  475. st->hand_state = TLS_ST_OK;
  476. return WRITE_TRAN_CONTINUE;
  477. case TLS_ST_OK:
  478. if (s->key_update != SSL_KEY_UPDATE_NONE) {
  479. st->hand_state = TLS_ST_CW_KEY_UPDATE;
  480. return WRITE_TRAN_CONTINUE;
  481. }
  482. /* Try to read from the server instead */
  483. return WRITE_TRAN_FINISHED;
  484. }
  485. }
  486. /*
  487. * ossl_statem_client_write_transition() works out what handshake state to
  488. * move to next when the client is writing messages to be sent to the server.
  489. */
  490. WRITE_TRAN ossl_statem_client_write_transition(SSL_CONNECTION *s)
  491. {
  492. OSSL_STATEM *st = &s->statem;
  493. /*
  494. * Note that immediately before/after a ClientHello we don't know what
  495. * version we are going to negotiate yet, so we don't take this branch until
  496. * later
  497. */
  498. if (SSL_CONNECTION_IS_TLS13(s))
  499. return ossl_statem_client13_write_transition(s);
  500. switch (st->hand_state) {
  501. default:
  502. /* Shouldn't happen */
  503. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  504. return WRITE_TRAN_ERROR;
  505. case TLS_ST_OK:
  506. if (!s->renegotiate) {
  507. /*
  508. * We haven't requested a renegotiation ourselves so we must have
  509. * received a message from the server. Better read it.
  510. */
  511. return WRITE_TRAN_FINISHED;
  512. }
  513. /* Renegotiation */
  514. /* fall thru */
  515. case TLS_ST_BEFORE:
  516. st->hand_state = TLS_ST_CW_CLNT_HELLO;
  517. return WRITE_TRAN_CONTINUE;
  518. case TLS_ST_CW_CLNT_HELLO:
  519. if (s->early_data_state == SSL_EARLY_DATA_CONNECTING) {
  520. /*
  521. * We are assuming this is a TLSv1.3 connection, although we haven't
  522. * actually selected a version yet.
  523. */
  524. if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0)
  525. st->hand_state = TLS_ST_CW_CHANGE;
  526. else
  527. st->hand_state = TLS_ST_EARLY_DATA;
  528. return WRITE_TRAN_CONTINUE;
  529. }
  530. /*
  531. * No transition at the end of writing because we don't know what
  532. * we will be sent
  533. */
  534. s->ts_msg_write = ossl_time_now();
  535. return WRITE_TRAN_FINISHED;
  536. case TLS_ST_CR_SRVR_HELLO:
  537. /*
  538. * We only get here in TLSv1.3. We just received an HRR, so issue a
  539. * CCS unless middlebox compat mode is off, or we already issued one
  540. * because we did early data.
  541. */
  542. if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
  543. && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING)
  544. st->hand_state = TLS_ST_CW_CHANGE;
  545. else
  546. st->hand_state = TLS_ST_CW_CLNT_HELLO;
  547. return WRITE_TRAN_CONTINUE;
  548. case TLS_ST_EARLY_DATA:
  549. s->ts_msg_write = ossl_time_now();
  550. return WRITE_TRAN_FINISHED;
  551. case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
  552. st->hand_state = TLS_ST_CW_CLNT_HELLO;
  553. return WRITE_TRAN_CONTINUE;
  554. case TLS_ST_CR_SRVR_DONE:
  555. s->ts_msg_read = ossl_time_now();
  556. if (s->s3.tmp.cert_req)
  557. st->hand_state = TLS_ST_CW_CERT;
  558. else
  559. st->hand_state = TLS_ST_CW_KEY_EXCH;
  560. return WRITE_TRAN_CONTINUE;
  561. case TLS_ST_CW_CERT:
  562. st->hand_state = TLS_ST_CW_KEY_EXCH;
  563. return WRITE_TRAN_CONTINUE;
  564. case TLS_ST_CW_KEY_EXCH:
  565. /*
  566. * For TLS, cert_req is set to 2, so a cert chain of nothing is
  567. * sent, but no verify packet is sent
  568. */
  569. /*
  570. * XXX: For now, we do not support client authentication in ECDH
  571. * cipher suites with ECDH (rather than ECDSA) certificates. We
  572. * need to skip the certificate verify message when client's
  573. * ECDH public key is sent inside the client certificate.
  574. */
  575. if (s->s3.tmp.cert_req == 1) {
  576. st->hand_state = TLS_ST_CW_CERT_VRFY;
  577. } else {
  578. st->hand_state = TLS_ST_CW_CHANGE;
  579. }
  580. if (s->s3.flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
  581. st->hand_state = TLS_ST_CW_CHANGE;
  582. }
  583. return WRITE_TRAN_CONTINUE;
  584. case TLS_ST_CW_CERT_VRFY:
  585. st->hand_state = TLS_ST_CW_CHANGE;
  586. return WRITE_TRAN_CONTINUE;
  587. case TLS_ST_CW_CHANGE:
  588. if (s->hello_retry_request == SSL_HRR_PENDING) {
  589. st->hand_state = TLS_ST_CW_CLNT_HELLO;
  590. } else if (s->early_data_state == SSL_EARLY_DATA_CONNECTING) {
  591. st->hand_state = TLS_ST_EARLY_DATA;
  592. } else {
  593. #if defined(OPENSSL_NO_NEXTPROTONEG)
  594. st->hand_state = TLS_ST_CW_FINISHED;
  595. #else
  596. if (!SSL_CONNECTION_IS_DTLS(s) && s->s3.npn_seen)
  597. st->hand_state = TLS_ST_CW_NEXT_PROTO;
  598. else
  599. st->hand_state = TLS_ST_CW_FINISHED;
  600. #endif
  601. }
  602. return WRITE_TRAN_CONTINUE;
  603. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  604. case TLS_ST_CW_NEXT_PROTO:
  605. st->hand_state = TLS_ST_CW_FINISHED;
  606. return WRITE_TRAN_CONTINUE;
  607. #endif
  608. case TLS_ST_CW_FINISHED:
  609. if (s->hit) {
  610. st->hand_state = TLS_ST_OK;
  611. return WRITE_TRAN_CONTINUE;
  612. } else {
  613. return WRITE_TRAN_FINISHED;
  614. }
  615. case TLS_ST_CR_FINISHED:
  616. if (s->hit) {
  617. st->hand_state = TLS_ST_CW_CHANGE;
  618. return WRITE_TRAN_CONTINUE;
  619. } else {
  620. st->hand_state = TLS_ST_OK;
  621. return WRITE_TRAN_CONTINUE;
  622. }
  623. case TLS_ST_CR_HELLO_REQ:
  624. /*
  625. * If we can renegotiate now then do so, otherwise wait for a more
  626. * convenient time.
  627. */
  628. if (ssl3_renegotiate_check(SSL_CONNECTION_GET_SSL(s), 1)) {
  629. if (!tls_setup_handshake(s)) {
  630. /* SSLfatal() already called */
  631. return WRITE_TRAN_ERROR;
  632. }
  633. st->hand_state = TLS_ST_CW_CLNT_HELLO;
  634. return WRITE_TRAN_CONTINUE;
  635. }
  636. st->hand_state = TLS_ST_OK;
  637. return WRITE_TRAN_CONTINUE;
  638. }
  639. }
  640. /*
  641. * Perform any pre work that needs to be done prior to sending a message from
  642. * the client to the server.
  643. */
  644. WORK_STATE ossl_statem_client_pre_work(SSL_CONNECTION *s, WORK_STATE wst)
  645. {
  646. OSSL_STATEM *st = &s->statem;
  647. switch (st->hand_state) {
  648. default:
  649. /* No pre work to be done */
  650. break;
  651. case TLS_ST_CW_CLNT_HELLO:
  652. s->shutdown = 0;
  653. if (SSL_CONNECTION_IS_DTLS(s)) {
  654. /* every DTLS ClientHello resets Finished MAC */
  655. if (!ssl3_init_finished_mac(s)) {
  656. /* SSLfatal() already called */
  657. return WORK_ERROR;
  658. }
  659. } else if (s->ext.early_data == SSL_EARLY_DATA_REJECTED) {
  660. /*
  661. * This must be a second ClientHello after an HRR following an
  662. * earlier rejected attempt to send early data. Since we were
  663. * previously encrypting the early data we now need to reset the
  664. * write record layer in order to write in plaintext again.
  665. */
  666. if (!ssl_set_new_record_layer(s,
  667. TLS_ANY_VERSION,
  668. OSSL_RECORD_DIRECTION_WRITE,
  669. OSSL_RECORD_PROTECTION_LEVEL_NONE,
  670. NULL, 0, NULL, 0, NULL, 0, NULL, 0,
  671. NULL, 0, NID_undef, NULL, NULL,
  672. NULL)) {
  673. /* SSLfatal already called */
  674. return WORK_ERROR;
  675. }
  676. }
  677. break;
  678. case TLS_ST_CW_CHANGE:
  679. if (SSL_CONNECTION_IS_DTLS(s)) {
  680. if (s->hit) {
  681. /*
  682. * We're into the last flight so we don't retransmit these
  683. * messages unless we need to.
  684. */
  685. st->use_timer = 0;
  686. }
  687. #ifndef OPENSSL_NO_SCTP
  688. if (BIO_dgram_is_sctp(SSL_get_wbio(SSL_CONNECTION_GET_SSL(s)))) {
  689. /* Calls SSLfatal() as required */
  690. return dtls_wait_for_dry(s);
  691. }
  692. #endif
  693. }
  694. break;
  695. case TLS_ST_PENDING_EARLY_DATA_END:
  696. /*
  697. * If we've been called by SSL_do_handshake()/SSL_write(), or we did not
  698. * attempt to write early data before calling SSL_read() then we press
  699. * on with the handshake. Otherwise we pause here.
  700. */
  701. if (s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING
  702. || s->early_data_state == SSL_EARLY_DATA_NONE)
  703. return WORK_FINISHED_CONTINUE;
  704. /* Fall through */
  705. case TLS_ST_EARLY_DATA:
  706. return tls_finish_handshake(s, wst, 0, 1);
  707. case TLS_ST_OK:
  708. /* Calls SSLfatal() as required */
  709. return tls_finish_handshake(s, wst, 1, 1);
  710. }
  711. return WORK_FINISHED_CONTINUE;
  712. }
  713. /*
  714. * Perform any work that needs to be done after sending a message from the
  715. * client to the server.
  716. */
  717. WORK_STATE ossl_statem_client_post_work(SSL_CONNECTION *s, WORK_STATE wst)
  718. {
  719. OSSL_STATEM *st = &s->statem;
  720. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  721. s->init_num = 0;
  722. switch (st->hand_state) {
  723. default:
  724. /* No post work to be done */
  725. break;
  726. case TLS_ST_CW_CLNT_HELLO:
  727. if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
  728. && s->max_early_data > 0) {
  729. /*
  730. * We haven't selected TLSv1.3 yet so we don't call the change
  731. * cipher state function associated with the SSL_METHOD. Instead
  732. * we call tls13_change_cipher_state() directly.
  733. */
  734. if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0) {
  735. if (!tls13_change_cipher_state(s,
  736. SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
  737. /* SSLfatal() already called */
  738. return WORK_ERROR;
  739. }
  740. }
  741. /* else we're in compat mode so we delay flushing until after CCS */
  742. } else if (!statem_flush(s)) {
  743. return WORK_MORE_A;
  744. }
  745. if (SSL_CONNECTION_IS_DTLS(s)) {
  746. /* Treat the next message as the first packet */
  747. s->first_packet = 1;
  748. }
  749. break;
  750. case TLS_ST_CW_KEY_EXCH:
  751. if (tls_client_key_exchange_post_work(s) == 0) {
  752. /* SSLfatal() already called */
  753. return WORK_ERROR;
  754. }
  755. break;
  756. case TLS_ST_CW_CHANGE:
  757. if (SSL_CONNECTION_IS_TLS13(s)
  758. || s->hello_retry_request == SSL_HRR_PENDING)
  759. break;
  760. if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
  761. && s->max_early_data > 0) {
  762. /*
  763. * We haven't selected TLSv1.3 yet so we don't call the change
  764. * cipher state function associated with the SSL_METHOD. Instead
  765. * we call tls13_change_cipher_state() directly.
  766. */
  767. if (!tls13_change_cipher_state(s,
  768. SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE))
  769. return WORK_ERROR;
  770. break;
  771. }
  772. s->session->cipher = s->s3.tmp.new_cipher;
  773. #ifdef OPENSSL_NO_COMP
  774. s->session->compress_meth = 0;
  775. #else
  776. if (s->s3.tmp.new_compression == NULL)
  777. s->session->compress_meth = 0;
  778. else
  779. s->session->compress_meth = s->s3.tmp.new_compression->id;
  780. #endif
  781. if (!ssl->method->ssl3_enc->setup_key_block(s)) {
  782. /* SSLfatal() already called */
  783. return WORK_ERROR;
  784. }
  785. if (!ssl->method->ssl3_enc->change_cipher_state(s,
  786. SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
  787. /* SSLfatal() already called */
  788. return WORK_ERROR;
  789. }
  790. if (SSL_CONNECTION_IS_DTLS(s)) {
  791. #ifndef OPENSSL_NO_SCTP
  792. if (s->hit) {
  793. /*
  794. * Change to new shared key of SCTP-Auth, will be ignored if
  795. * no SCTP used.
  796. */
  797. BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  798. 0, NULL);
  799. }
  800. #endif
  801. dtls1_increment_epoch(s, SSL3_CC_WRITE);
  802. }
  803. break;
  804. case TLS_ST_CW_FINISHED:
  805. #ifndef OPENSSL_NO_SCTP
  806. if (wst == WORK_MORE_A && SSL_CONNECTION_IS_DTLS(s) && s->hit == 0) {
  807. /*
  808. * Change to new shared key of SCTP-Auth, will be ignored if
  809. * no SCTP used.
  810. */
  811. BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  812. 0, NULL);
  813. }
  814. #endif
  815. if (statem_flush(s) != 1)
  816. return WORK_MORE_B;
  817. if (SSL_CONNECTION_IS_TLS13(s)) {
  818. if (!tls13_save_handshake_digest_for_pha(s)) {
  819. /* SSLfatal() already called */
  820. return WORK_ERROR;
  821. }
  822. if (s->post_handshake_auth != SSL_PHA_REQUESTED) {
  823. if (!ssl->method->ssl3_enc->change_cipher_state(s,
  824. SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
  825. /* SSLfatal() already called */
  826. return WORK_ERROR;
  827. }
  828. }
  829. }
  830. break;
  831. case TLS_ST_CW_KEY_UPDATE:
  832. if (statem_flush(s) != 1)
  833. return WORK_MORE_A;
  834. if (!tls13_update_key(s, 1)) {
  835. /* SSLfatal() already called */
  836. return WORK_ERROR;
  837. }
  838. break;
  839. }
  840. return WORK_FINISHED_CONTINUE;
  841. }
  842. /*
  843. * Get the message construction function and message type for sending from the
  844. * client
  845. *
  846. * Valid return values are:
  847. * 1: Success
  848. * 0: Error
  849. */
  850. int ossl_statem_client_construct_message(SSL_CONNECTION *s,
  851. confunc_f *confunc, int *mt)
  852. {
  853. OSSL_STATEM *st = &s->statem;
  854. switch (st->hand_state) {
  855. default:
  856. /* Shouldn't happen */
  857. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE);
  858. return 0;
  859. case TLS_ST_CW_CHANGE:
  860. if (SSL_CONNECTION_IS_DTLS(s))
  861. *confunc = dtls_construct_change_cipher_spec;
  862. else
  863. *confunc = tls_construct_change_cipher_spec;
  864. *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
  865. break;
  866. case TLS_ST_CW_CLNT_HELLO:
  867. *confunc = tls_construct_client_hello;
  868. *mt = SSL3_MT_CLIENT_HELLO;
  869. break;
  870. case TLS_ST_CW_END_OF_EARLY_DATA:
  871. *confunc = tls_construct_end_of_early_data;
  872. *mt = SSL3_MT_END_OF_EARLY_DATA;
  873. break;
  874. case TLS_ST_PENDING_EARLY_DATA_END:
  875. *confunc = NULL;
  876. *mt = SSL3_MT_DUMMY;
  877. break;
  878. case TLS_ST_CW_CERT:
  879. *confunc = tls_construct_client_certificate;
  880. *mt = SSL3_MT_CERTIFICATE;
  881. break;
  882. #ifndef OPENSSL_NO_COMP_ALG
  883. case TLS_ST_CW_COMP_CERT:
  884. *confunc = tls_construct_client_compressed_certificate;
  885. *mt = SSL3_MT_COMPRESSED_CERTIFICATE;
  886. break;
  887. #endif
  888. case TLS_ST_CW_KEY_EXCH:
  889. *confunc = tls_construct_client_key_exchange;
  890. *mt = SSL3_MT_CLIENT_KEY_EXCHANGE;
  891. break;
  892. case TLS_ST_CW_CERT_VRFY:
  893. *confunc = tls_construct_cert_verify;
  894. *mt = SSL3_MT_CERTIFICATE_VERIFY;
  895. break;
  896. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  897. case TLS_ST_CW_NEXT_PROTO:
  898. *confunc = tls_construct_next_proto;
  899. *mt = SSL3_MT_NEXT_PROTO;
  900. break;
  901. #endif
  902. case TLS_ST_CW_FINISHED:
  903. *confunc = tls_construct_finished;
  904. *mt = SSL3_MT_FINISHED;
  905. break;
  906. case TLS_ST_CW_KEY_UPDATE:
  907. *confunc = tls_construct_key_update;
  908. *mt = SSL3_MT_KEY_UPDATE;
  909. break;
  910. }
  911. return 1;
  912. }
  913. /*
  914. * Returns the maximum allowed length for the current message that we are
  915. * reading. Excludes the message header.
  916. */
  917. size_t ossl_statem_client_max_message_size(SSL_CONNECTION *s)
  918. {
  919. OSSL_STATEM *st = &s->statem;
  920. switch (st->hand_state) {
  921. default:
  922. /* Shouldn't happen */
  923. return 0;
  924. case TLS_ST_CR_SRVR_HELLO:
  925. return SERVER_HELLO_MAX_LENGTH;
  926. case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
  927. return HELLO_VERIFY_REQUEST_MAX_LENGTH;
  928. case TLS_ST_CR_COMP_CERT:
  929. case TLS_ST_CR_CERT:
  930. return s->max_cert_list;
  931. case TLS_ST_CR_CERT_VRFY:
  932. return CERTIFICATE_VERIFY_MAX_LENGTH;
  933. case TLS_ST_CR_CERT_STATUS:
  934. return SSL3_RT_MAX_PLAIN_LENGTH;
  935. case TLS_ST_CR_KEY_EXCH:
  936. return SERVER_KEY_EXCH_MAX_LENGTH;
  937. case TLS_ST_CR_CERT_REQ:
  938. /*
  939. * Set to s->max_cert_list for compatibility with previous releases. In
  940. * practice these messages can get quite long if servers are configured
  941. * to provide a long list of acceptable CAs
  942. */
  943. return s->max_cert_list;
  944. case TLS_ST_CR_SRVR_DONE:
  945. return SERVER_HELLO_DONE_MAX_LENGTH;
  946. case TLS_ST_CR_CHANGE:
  947. if (s->version == DTLS1_BAD_VER)
  948. return 3;
  949. return CCS_MAX_LENGTH;
  950. case TLS_ST_CR_SESSION_TICKET:
  951. return (SSL_CONNECTION_IS_TLS13(s)) ? SESSION_TICKET_MAX_LENGTH_TLS13
  952. : SESSION_TICKET_MAX_LENGTH_TLS12;
  953. case TLS_ST_CR_FINISHED:
  954. return FINISHED_MAX_LENGTH;
  955. case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
  956. return ENCRYPTED_EXTENSIONS_MAX_LENGTH;
  957. case TLS_ST_CR_KEY_UPDATE:
  958. return KEY_UPDATE_MAX_LENGTH;
  959. }
  960. }
  961. /*
  962. * Process a message that the client has received from the server.
  963. */
  964. MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL_CONNECTION *s,
  965. PACKET *pkt)
  966. {
  967. OSSL_STATEM *st = &s->statem;
  968. switch (st->hand_state) {
  969. default:
  970. /* Shouldn't happen */
  971. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  972. return MSG_PROCESS_ERROR;
  973. case TLS_ST_CR_SRVR_HELLO:
  974. return tls_process_server_hello(s, pkt);
  975. case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
  976. return dtls_process_hello_verify(s, pkt);
  977. case TLS_ST_CR_CERT:
  978. return tls_process_server_certificate(s, pkt);
  979. #ifndef OPENSSL_NO_COMP_ALG
  980. case TLS_ST_CR_COMP_CERT:
  981. return tls_process_server_compressed_certificate(s, pkt);
  982. #endif
  983. case TLS_ST_CR_CERT_VRFY:
  984. return tls_process_cert_verify(s, pkt);
  985. case TLS_ST_CR_CERT_STATUS:
  986. return tls_process_cert_status(s, pkt);
  987. case TLS_ST_CR_KEY_EXCH:
  988. return tls_process_key_exchange(s, pkt);
  989. case TLS_ST_CR_CERT_REQ:
  990. return tls_process_certificate_request(s, pkt);
  991. case TLS_ST_CR_SRVR_DONE:
  992. return tls_process_server_done(s, pkt);
  993. case TLS_ST_CR_CHANGE:
  994. return tls_process_change_cipher_spec(s, pkt);
  995. case TLS_ST_CR_SESSION_TICKET:
  996. return tls_process_new_session_ticket(s, pkt);
  997. case TLS_ST_CR_FINISHED:
  998. return tls_process_finished(s, pkt);
  999. case TLS_ST_CR_HELLO_REQ:
  1000. return tls_process_hello_req(s, pkt);
  1001. case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
  1002. return tls_process_encrypted_extensions(s, pkt);
  1003. case TLS_ST_CR_KEY_UPDATE:
  1004. return tls_process_key_update(s, pkt);
  1005. }
  1006. }
  1007. /*
  1008. * Perform any further processing required following the receipt of a message
  1009. * from the server
  1010. */
  1011. WORK_STATE ossl_statem_client_post_process_message(SSL_CONNECTION *s,
  1012. WORK_STATE wst)
  1013. {
  1014. OSSL_STATEM *st = &s->statem;
  1015. switch (st->hand_state) {
  1016. default:
  1017. /* Shouldn't happen */
  1018. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1019. return WORK_ERROR;
  1020. case TLS_ST_CR_CERT:
  1021. case TLS_ST_CR_COMP_CERT:
  1022. return tls_post_process_server_certificate(s, wst);
  1023. case TLS_ST_CR_CERT_VRFY:
  1024. case TLS_ST_CR_CERT_REQ:
  1025. return tls_prepare_client_certificate(s, wst);
  1026. }
  1027. }
  1028. CON_FUNC_RETURN tls_construct_client_hello(SSL_CONNECTION *s, WPACKET *pkt)
  1029. {
  1030. unsigned char *p;
  1031. size_t sess_id_len;
  1032. int i, protverr;
  1033. #ifndef OPENSSL_NO_COMP
  1034. SSL_COMP *comp;
  1035. #endif
  1036. SSL_SESSION *sess = s->session;
  1037. unsigned char *session_id;
  1038. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  1039. /* Work out what SSL/TLS/DTLS version to use */
  1040. protverr = ssl_set_client_hello_version(s);
  1041. if (protverr != 0) {
  1042. SSLfatal(s, SSL_AD_INTERNAL_ERROR, protverr);
  1043. return CON_FUNC_ERROR;
  1044. }
  1045. if (sess == NULL
  1046. || !ssl_version_supported(s, sess->ssl_version, NULL)
  1047. || !SSL_SESSION_is_resumable(sess)) {
  1048. if (s->hello_retry_request == SSL_HRR_NONE
  1049. && !ssl_get_new_session(s, 0)) {
  1050. /* SSLfatal() already called */
  1051. return CON_FUNC_ERROR;
  1052. }
  1053. }
  1054. /* else use the pre-loaded session */
  1055. p = s->s3.client_random;
  1056. /*
  1057. * for DTLS if client_random is initialized, reuse it, we are
  1058. * required to use same upon reply to HelloVerify
  1059. */
  1060. if (SSL_CONNECTION_IS_DTLS(s)) {
  1061. size_t idx;
  1062. i = 1;
  1063. for (idx = 0; idx < sizeof(s->s3.client_random); idx++) {
  1064. if (p[idx]) {
  1065. i = 0;
  1066. break;
  1067. }
  1068. }
  1069. } else {
  1070. i = (s->hello_retry_request == SSL_HRR_NONE);
  1071. }
  1072. if (i && ssl_fill_hello_random(s, 0, p, sizeof(s->s3.client_random),
  1073. DOWNGRADE_NONE) <= 0) {
  1074. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1075. return CON_FUNC_ERROR;
  1076. }
  1077. /*-
  1078. * version indicates the negotiated version: for example from
  1079. * an SSLv2/v3 compatible client hello). The client_version
  1080. * field is the maximum version we permit and it is also
  1081. * used in RSA encrypted premaster secrets. Some servers can
  1082. * choke if we initially report a higher version then
  1083. * renegotiate to a lower one in the premaster secret. This
  1084. * didn't happen with TLS 1.0 as most servers supported it
  1085. * but it can with TLS 1.1 or later if the server only supports
  1086. * 1.0.
  1087. *
  1088. * Possible scenario with previous logic:
  1089. * 1. Client hello indicates TLS 1.2
  1090. * 2. Server hello says TLS 1.0
  1091. * 3. RSA encrypted premaster secret uses 1.2.
  1092. * 4. Handshake proceeds using TLS 1.0.
  1093. * 5. Server sends hello request to renegotiate.
  1094. * 6. Client hello indicates TLS v1.0 as we now
  1095. * know that is maximum server supports.
  1096. * 7. Server chokes on RSA encrypted premaster secret
  1097. * containing version 1.0.
  1098. *
  1099. * For interoperability it should be OK to always use the
  1100. * maximum version we support in client hello and then rely
  1101. * on the checking of version to ensure the servers isn't
  1102. * being inconsistent: for example initially negotiating with
  1103. * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
  1104. * client_version in client hello and not resetting it to
  1105. * the negotiated version.
  1106. *
  1107. * For TLS 1.3 we always set the ClientHello version to 1.2 and rely on the
  1108. * supported_versions extension for the real supported versions.
  1109. */
  1110. if (!WPACKET_put_bytes_u16(pkt, s->client_version)
  1111. || !WPACKET_memcpy(pkt, s->s3.client_random, SSL3_RANDOM_SIZE)) {
  1112. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1113. return CON_FUNC_ERROR;
  1114. }
  1115. /* Session ID */
  1116. session_id = s->session->session_id;
  1117. if (s->new_session || s->session->ssl_version == TLS1_3_VERSION) {
  1118. if (s->version == TLS1_3_VERSION
  1119. && (s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0) {
  1120. sess_id_len = sizeof(s->tmp_session_id);
  1121. s->tmp_session_id_len = sess_id_len;
  1122. session_id = s->tmp_session_id;
  1123. if (s->hello_retry_request == SSL_HRR_NONE
  1124. && RAND_bytes_ex(sctx->libctx, s->tmp_session_id,
  1125. sess_id_len, 0) <= 0) {
  1126. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1127. return CON_FUNC_ERROR;
  1128. }
  1129. } else {
  1130. sess_id_len = 0;
  1131. }
  1132. } else {
  1133. assert(s->session->session_id_length <= sizeof(s->session->session_id));
  1134. sess_id_len = s->session->session_id_length;
  1135. if (s->version == TLS1_3_VERSION) {
  1136. s->tmp_session_id_len = sess_id_len;
  1137. memcpy(s->tmp_session_id, s->session->session_id, sess_id_len);
  1138. }
  1139. }
  1140. if (!WPACKET_start_sub_packet_u8(pkt)
  1141. || (sess_id_len != 0 && !WPACKET_memcpy(pkt, session_id,
  1142. sess_id_len))
  1143. || !WPACKET_close(pkt)) {
  1144. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1145. return CON_FUNC_ERROR;
  1146. }
  1147. /* cookie stuff for DTLS */
  1148. if (SSL_CONNECTION_IS_DTLS(s)) {
  1149. if (s->d1->cookie_len > sizeof(s->d1->cookie)
  1150. || !WPACKET_sub_memcpy_u8(pkt, s->d1->cookie,
  1151. s->d1->cookie_len)) {
  1152. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1153. return CON_FUNC_ERROR;
  1154. }
  1155. }
  1156. /* Ciphers supported */
  1157. if (!WPACKET_start_sub_packet_u16(pkt)) {
  1158. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1159. return CON_FUNC_ERROR;
  1160. }
  1161. if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(SSL_CONNECTION_GET_SSL(s)),
  1162. pkt)) {
  1163. /* SSLfatal() already called */
  1164. return CON_FUNC_ERROR;
  1165. }
  1166. if (!WPACKET_close(pkt)) {
  1167. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1168. return CON_FUNC_ERROR;
  1169. }
  1170. /* COMPRESSION */
  1171. if (!WPACKET_start_sub_packet_u8(pkt)) {
  1172. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1173. return CON_FUNC_ERROR;
  1174. }
  1175. #ifndef OPENSSL_NO_COMP
  1176. if (ssl_allow_compression(s)
  1177. && sctx->comp_methods
  1178. && (SSL_CONNECTION_IS_DTLS(s)
  1179. || s->s3.tmp.max_ver < TLS1_3_VERSION)) {
  1180. int compnum = sk_SSL_COMP_num(sctx->comp_methods);
  1181. for (i = 0; i < compnum; i++) {
  1182. comp = sk_SSL_COMP_value(sctx->comp_methods, i);
  1183. if (!WPACKET_put_bytes_u8(pkt, comp->id)) {
  1184. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1185. return CON_FUNC_ERROR;
  1186. }
  1187. }
  1188. }
  1189. #endif
  1190. /* Add the NULL method */
  1191. if (!WPACKET_put_bytes_u8(pkt, 0) || !WPACKET_close(pkt)) {
  1192. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1193. return CON_FUNC_ERROR;
  1194. }
  1195. /* TLS extensions */
  1196. if (!tls_construct_extensions(s, pkt, SSL_EXT_CLIENT_HELLO, NULL, 0)) {
  1197. /* SSLfatal() already called */
  1198. return CON_FUNC_ERROR;
  1199. }
  1200. return CON_FUNC_SUCCESS;
  1201. }
  1202. MSG_PROCESS_RETURN dtls_process_hello_verify(SSL_CONNECTION *s, PACKET *pkt)
  1203. {
  1204. size_t cookie_len;
  1205. PACKET cookiepkt;
  1206. if (!PACKET_forward(pkt, 2)
  1207. || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
  1208. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1209. return MSG_PROCESS_ERROR;
  1210. }
  1211. cookie_len = PACKET_remaining(&cookiepkt);
  1212. if (cookie_len > sizeof(s->d1->cookie)) {
  1213. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_LENGTH_TOO_LONG);
  1214. return MSG_PROCESS_ERROR;
  1215. }
  1216. if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
  1217. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1218. return MSG_PROCESS_ERROR;
  1219. }
  1220. s->d1->cookie_len = cookie_len;
  1221. return MSG_PROCESS_FINISHED_READING;
  1222. }
  1223. static int set_client_ciphersuite(SSL_CONNECTION *s,
  1224. const unsigned char *cipherchars)
  1225. {
  1226. STACK_OF(SSL_CIPHER) *sk;
  1227. const SSL_CIPHER *c;
  1228. int i;
  1229. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  1230. c = ssl_get_cipher_by_char(s, cipherchars, 0);
  1231. if (c == NULL) {
  1232. /* unknown cipher */
  1233. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CIPHER_RETURNED);
  1234. return 0;
  1235. }
  1236. /*
  1237. * If it is a disabled cipher we either didn't send it in client hello,
  1238. * or it's not allowed for the selected protocol. So we return an error.
  1239. */
  1240. if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK, 1)) {
  1241. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED);
  1242. return 0;
  1243. }
  1244. sk = ssl_get_ciphers_by_id(s);
  1245. i = sk_SSL_CIPHER_find(sk, c);
  1246. if (i < 0) {
  1247. /* we did not say we would use this cipher */
  1248. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED);
  1249. return 0;
  1250. }
  1251. if (SSL_CONNECTION_IS_TLS13(s) && s->s3.tmp.new_cipher != NULL
  1252. && s->s3.tmp.new_cipher->id != c->id) {
  1253. /* ServerHello selected a different ciphersuite to that in the HRR */
  1254. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED);
  1255. return 0;
  1256. }
  1257. /*
  1258. * Depending on the session caching (internal/external), the cipher
  1259. * and/or cipher_id values may not be set. Make sure that cipher_id is
  1260. * set and use it for comparison.
  1261. */
  1262. if (s->session->cipher != NULL)
  1263. s->session->cipher_id = s->session->cipher->id;
  1264. if (s->hit && (s->session->cipher_id != c->id)) {
  1265. if (SSL_CONNECTION_IS_TLS13(s)) {
  1266. const EVP_MD *md = ssl_md(sctx, c->algorithm2);
  1267. if (!ossl_assert(s->session->cipher != NULL)) {
  1268. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1269. return 0;
  1270. }
  1271. /*
  1272. * In TLSv1.3 it is valid for the server to select a different
  1273. * ciphersuite as long as the hash is the same.
  1274. */
  1275. if (md == NULL
  1276. || md != ssl_md(sctx, s->session->cipher->algorithm2)) {
  1277. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1278. SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED);
  1279. return 0;
  1280. }
  1281. } else {
  1282. /*
  1283. * Prior to TLSv1.3 resuming a session always meant using the same
  1284. * ciphersuite.
  1285. */
  1286. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1287. SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  1288. return 0;
  1289. }
  1290. }
  1291. s->s3.tmp.new_cipher = c;
  1292. return 1;
  1293. }
  1294. MSG_PROCESS_RETURN tls_process_server_hello(SSL_CONNECTION *s, PACKET *pkt)
  1295. {
  1296. PACKET session_id, extpkt;
  1297. size_t session_id_len;
  1298. const unsigned char *cipherchars;
  1299. int hrr = 0;
  1300. unsigned int compression;
  1301. unsigned int sversion;
  1302. unsigned int context;
  1303. RAW_EXTENSION *extensions = NULL;
  1304. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  1305. #ifndef OPENSSL_NO_COMP
  1306. SSL_COMP *comp;
  1307. #endif
  1308. if (!PACKET_get_net_2(pkt, &sversion)) {
  1309. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1310. goto err;
  1311. }
  1312. /* load the server random */
  1313. if (s->version == TLS1_3_VERSION
  1314. && sversion == TLS1_2_VERSION
  1315. && PACKET_remaining(pkt) >= SSL3_RANDOM_SIZE
  1316. && memcmp(hrrrandom, PACKET_data(pkt), SSL3_RANDOM_SIZE) == 0) {
  1317. if (s->hello_retry_request != SSL_HRR_NONE) {
  1318. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
  1319. goto err;
  1320. }
  1321. s->hello_retry_request = SSL_HRR_PENDING;
  1322. /* Tell the record layer that we know we're going to get TLSv1.3 */
  1323. if (!ssl_set_record_protocol_version(s, s->version)) {
  1324. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1325. goto err;
  1326. }
  1327. hrr = 1;
  1328. if (!PACKET_forward(pkt, SSL3_RANDOM_SIZE)) {
  1329. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1330. goto err;
  1331. }
  1332. } else {
  1333. if (!PACKET_copy_bytes(pkt, s->s3.server_random, SSL3_RANDOM_SIZE)) {
  1334. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1335. goto err;
  1336. }
  1337. }
  1338. /* Get the session-id. */
  1339. if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
  1340. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1341. goto err;
  1342. }
  1343. session_id_len = PACKET_remaining(&session_id);
  1344. if (session_id_len > sizeof(s->session->session_id)
  1345. || session_id_len > SSL3_SESSION_ID_SIZE) {
  1346. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_SSL3_SESSION_ID_TOO_LONG);
  1347. goto err;
  1348. }
  1349. if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
  1350. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1351. goto err;
  1352. }
  1353. if (!PACKET_get_1(pkt, &compression)) {
  1354. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1355. goto err;
  1356. }
  1357. /* TLS extensions */
  1358. if (PACKET_remaining(pkt) == 0 && !hrr) {
  1359. PACKET_null_init(&extpkt);
  1360. } else if (!PACKET_as_length_prefixed_2(pkt, &extpkt)
  1361. || PACKET_remaining(pkt) != 0) {
  1362. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
  1363. goto err;
  1364. }
  1365. if (!hrr) {
  1366. if (!tls_collect_extensions(s, &extpkt,
  1367. SSL_EXT_TLS1_2_SERVER_HELLO
  1368. | SSL_EXT_TLS1_3_SERVER_HELLO,
  1369. &extensions, NULL, 1)) {
  1370. /* SSLfatal() already called */
  1371. goto err;
  1372. }
  1373. if (!ssl_choose_client_version(s, sversion, extensions)) {
  1374. /* SSLfatal() already called */
  1375. goto err;
  1376. }
  1377. }
  1378. if (SSL_CONNECTION_IS_TLS13(s) || hrr) {
  1379. if (compression != 0) {
  1380. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1381. SSL_R_INVALID_COMPRESSION_ALGORITHM);
  1382. goto err;
  1383. }
  1384. if (session_id_len != s->tmp_session_id_len
  1385. || memcmp(PACKET_data(&session_id), s->tmp_session_id,
  1386. session_id_len) != 0) {
  1387. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INVALID_SESSION_ID);
  1388. goto err;
  1389. }
  1390. }
  1391. if (hrr) {
  1392. if (!set_client_ciphersuite(s, cipherchars)) {
  1393. /* SSLfatal() already called */
  1394. goto err;
  1395. }
  1396. return tls_process_as_hello_retry_request(s, &extpkt);
  1397. }
  1398. /*
  1399. * Now we have chosen the version we need to check again that the extensions
  1400. * are appropriate for this version.
  1401. */
  1402. context = SSL_CONNECTION_IS_TLS13(s) ? SSL_EXT_TLS1_3_SERVER_HELLO
  1403. : SSL_EXT_TLS1_2_SERVER_HELLO;
  1404. if (!tls_validate_all_contexts(s, context, extensions)) {
  1405. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
  1406. goto err;
  1407. }
  1408. s->hit = 0;
  1409. if (SSL_CONNECTION_IS_TLS13(s)) {
  1410. /*
  1411. * In TLSv1.3 a ServerHello message signals a key change so the end of
  1412. * the message must be on a record boundary.
  1413. */
  1414. if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
  1415. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
  1416. SSL_R_NOT_ON_RECORD_BOUNDARY);
  1417. goto err;
  1418. }
  1419. /* This will set s->hit if we are resuming */
  1420. if (!tls_parse_extension(s, TLSEXT_IDX_psk,
  1421. SSL_EXT_TLS1_3_SERVER_HELLO,
  1422. extensions, NULL, 0)) {
  1423. /* SSLfatal() already called */
  1424. goto err;
  1425. }
  1426. } else {
  1427. /*
  1428. * Check if we can resume the session based on external pre-shared
  1429. * secret. EAP-FAST (RFC 4851) supports two types of session resumption.
  1430. * Resumption based on server-side state works with session IDs.
  1431. * Resumption based on pre-shared Protected Access Credentials (PACs)
  1432. * works by overriding the SessionTicket extension at the application
  1433. * layer, and does not send a session ID. (We do not know whether
  1434. * EAP-FAST servers would honour the session ID.) Therefore, the session
  1435. * ID alone is not a reliable indicator of session resumption, so we
  1436. * first check if we can resume, and later peek at the next handshake
  1437. * message to see if the server wants to resume.
  1438. */
  1439. if (s->version >= TLS1_VERSION
  1440. && s->ext.session_secret_cb != NULL && s->session->ext.tick) {
  1441. const SSL_CIPHER *pref_cipher = NULL;
  1442. /*
  1443. * s->session->master_key_length is a size_t, but this is an int for
  1444. * backwards compat reasons
  1445. */
  1446. int master_key_length;
  1447. master_key_length = sizeof(s->session->master_key);
  1448. if (s->ext.session_secret_cb(ssl, s->session->master_key,
  1449. &master_key_length,
  1450. NULL, &pref_cipher,
  1451. s->ext.session_secret_cb_arg)
  1452. && master_key_length > 0) {
  1453. s->session->master_key_length = master_key_length;
  1454. s->session->cipher = pref_cipher ?
  1455. pref_cipher : ssl_get_cipher_by_char(s, cipherchars, 0);
  1456. } else {
  1457. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1458. goto err;
  1459. }
  1460. }
  1461. if (session_id_len != 0
  1462. && session_id_len == s->session->session_id_length
  1463. && memcmp(PACKET_data(&session_id), s->session->session_id,
  1464. session_id_len) == 0)
  1465. s->hit = 1;
  1466. }
  1467. if (s->hit) {
  1468. if (s->sid_ctx_length != s->session->sid_ctx_length
  1469. || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
  1470. /* actually a client application bug */
  1471. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1472. SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  1473. goto err;
  1474. }
  1475. } else {
  1476. /*
  1477. * If we were trying for session-id reuse but the server
  1478. * didn't resume, make a new SSL_SESSION.
  1479. * In the case of EAP-FAST and PAC, we do not send a session ID,
  1480. * so the PAC-based session secret is always preserved. It'll be
  1481. * overwritten if the server refuses resumption.
  1482. */
  1483. if (s->session->session_id_length > 0) {
  1484. ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_miss);
  1485. if (!ssl_get_new_session(s, 0)) {
  1486. /* SSLfatal() already called */
  1487. goto err;
  1488. }
  1489. }
  1490. s->session->ssl_version = s->version;
  1491. /*
  1492. * In TLSv1.2 and below we save the session id we were sent so we can
  1493. * resume it later. In TLSv1.3 the session id we were sent is just an
  1494. * echo of what we originally sent in the ClientHello and should not be
  1495. * used for resumption.
  1496. */
  1497. if (!SSL_CONNECTION_IS_TLS13(s)) {
  1498. s->session->session_id_length = session_id_len;
  1499. /* session_id_len could be 0 */
  1500. if (session_id_len > 0)
  1501. memcpy(s->session->session_id, PACKET_data(&session_id),
  1502. session_id_len);
  1503. }
  1504. }
  1505. /* Session version and negotiated protocol version should match */
  1506. if (s->version != s->session->ssl_version) {
  1507. SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
  1508. SSL_R_SSL_SESSION_VERSION_MISMATCH);
  1509. goto err;
  1510. }
  1511. /*
  1512. * Now that we know the version, update the check to see if it's an allowed
  1513. * version.
  1514. */
  1515. s->s3.tmp.min_ver = s->version;
  1516. s->s3.tmp.max_ver = s->version;
  1517. if (!set_client_ciphersuite(s, cipherchars)) {
  1518. /* SSLfatal() already called */
  1519. goto err;
  1520. }
  1521. #ifdef OPENSSL_NO_COMP
  1522. if (compression != 0) {
  1523. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1524. SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  1525. goto err;
  1526. }
  1527. /*
  1528. * If compression is disabled we'd better not try to resume a session
  1529. * using compression.
  1530. */
  1531. if (s->session->compress_meth != 0) {
  1532. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_COMPRESSION);
  1533. goto err;
  1534. }
  1535. #else
  1536. if (s->hit && compression != s->session->compress_meth) {
  1537. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1538. SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
  1539. goto err;
  1540. }
  1541. if (compression == 0)
  1542. comp = NULL;
  1543. else if (!ssl_allow_compression(s)) {
  1544. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COMPRESSION_DISABLED);
  1545. goto err;
  1546. } else {
  1547. comp = ssl3_comp_find(SSL_CONNECTION_GET_CTX(s)->comp_methods,
  1548. compression);
  1549. }
  1550. if (compression != 0 && comp == NULL) {
  1551. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1552. SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  1553. goto err;
  1554. } else {
  1555. s->s3.tmp.new_compression = comp;
  1556. }
  1557. #endif
  1558. if (!tls_parse_all_extensions(s, context, extensions, NULL, 0, 1)) {
  1559. /* SSLfatal() already called */
  1560. goto err;
  1561. }
  1562. #ifndef OPENSSL_NO_SCTP
  1563. if (SSL_CONNECTION_IS_DTLS(s) && s->hit) {
  1564. unsigned char sctpauthkey[64];
  1565. char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
  1566. size_t labellen;
  1567. /*
  1568. * Add new shared key for SCTP-Auth, will be ignored if
  1569. * no SCTP used.
  1570. */
  1571. memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
  1572. sizeof(DTLS1_SCTP_AUTH_LABEL));
  1573. /* Don't include the terminating zero. */
  1574. labellen = sizeof(labelbuffer) - 1;
  1575. if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
  1576. labellen += 1;
  1577. if (SSL_export_keying_material(ssl, sctpauthkey,
  1578. sizeof(sctpauthkey),
  1579. labelbuffer,
  1580. labellen, NULL, 0, 0) <= 0) {
  1581. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1582. goto err;
  1583. }
  1584. BIO_ctrl(SSL_get_wbio(ssl),
  1585. BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  1586. sizeof(sctpauthkey), sctpauthkey);
  1587. }
  1588. #endif
  1589. /*
  1590. * In TLSv1.3 we have some post-processing to change cipher state, otherwise
  1591. * we're done with this message
  1592. */
  1593. if (SSL_CONNECTION_IS_TLS13(s)) {
  1594. if (!ssl->method->ssl3_enc->setup_key_block(s)
  1595. || !ssl->method->ssl3_enc->change_cipher_state(s,
  1596. SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_READ)) {
  1597. /* SSLfatal() already called */
  1598. goto err;
  1599. }
  1600. /*
  1601. * If we're not doing early-data and we're not going to send a dummy CCS
  1602. * (i.e. no middlebox compat mode) then we can change the write keys
  1603. * immediately. Otherwise we have to defer this until after all possible
  1604. * early data is written. We could just always defer until the last
  1605. * moment except QUIC needs it done at the same time as the read keys
  1606. * are changed. Since QUIC doesn't do TLS early data or need middlebox
  1607. * compat this doesn't cause a problem.
  1608. */
  1609. if (s->early_data_state == SSL_EARLY_DATA_NONE
  1610. && (s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0
  1611. && !ssl->method->ssl3_enc->change_cipher_state(s,
  1612. SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
  1613. /* SSLfatal() already called */
  1614. goto err;
  1615. }
  1616. }
  1617. OPENSSL_free(extensions);
  1618. return MSG_PROCESS_CONTINUE_READING;
  1619. err:
  1620. OPENSSL_free(extensions);
  1621. return MSG_PROCESS_ERROR;
  1622. }
  1623. static MSG_PROCESS_RETURN tls_process_as_hello_retry_request(SSL_CONNECTION *s,
  1624. PACKET *extpkt)
  1625. {
  1626. RAW_EXTENSION *extensions = NULL;
  1627. /*
  1628. * If we were sending early_data then any alerts should not be sent using
  1629. * the old wrlmethod.
  1630. */
  1631. if (s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING
  1632. && !ssl_set_new_record_layer(s,
  1633. TLS_ANY_VERSION,
  1634. OSSL_RECORD_DIRECTION_WRITE,
  1635. OSSL_RECORD_PROTECTION_LEVEL_NONE,
  1636. NULL, 0, NULL, 0, NULL, 0, NULL, 0,
  1637. NULL, 0, NID_undef, NULL, NULL, NULL)) {
  1638. /* SSLfatal already called */
  1639. goto err;
  1640. }
  1641. /* We are definitely going to be using TLSv1.3 */
  1642. s->rlayer.wrlmethod->set_protocol_version(s->rlayer.wrl, TLS1_3_VERSION);
  1643. if (!tls_collect_extensions(s, extpkt, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST,
  1644. &extensions, NULL, 1)
  1645. || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST,
  1646. extensions, NULL, 0, 1)) {
  1647. /* SSLfatal() already called */
  1648. goto err;
  1649. }
  1650. OPENSSL_free(extensions);
  1651. extensions = NULL;
  1652. if (s->ext.tls13_cookie_len == 0 && s->s3.tmp.pkey != NULL) {
  1653. /*
  1654. * We didn't receive a cookie or a new key_share so the next
  1655. * ClientHello will not change
  1656. */
  1657. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CHANGE_FOLLOWING_HRR);
  1658. goto err;
  1659. }
  1660. /*
  1661. * Re-initialise the Transcript Hash. We're going to prepopulate it with
  1662. * a synthetic message_hash in place of ClientHello1.
  1663. */
  1664. if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) {
  1665. /* SSLfatal() already called */
  1666. goto err;
  1667. }
  1668. /*
  1669. * Add this message to the Transcript Hash. Normally this is done
  1670. * automatically prior to the message processing stage. However due to the
  1671. * need to create the synthetic message hash, we defer that step until now
  1672. * for HRR messages.
  1673. */
  1674. if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
  1675. s->init_num + SSL3_HM_HEADER_LENGTH)) {
  1676. /* SSLfatal() already called */
  1677. goto err;
  1678. }
  1679. return MSG_PROCESS_FINISHED_READING;
  1680. err:
  1681. OPENSSL_free(extensions);
  1682. return MSG_PROCESS_ERROR;
  1683. }
  1684. MSG_PROCESS_RETURN tls_process_server_rpk(SSL_CONNECTION *sc, PACKET *pkt)
  1685. {
  1686. EVP_PKEY *peer_rpk;
  1687. if (!tls_process_rpk(sc, pkt, &peer_rpk)) {
  1688. /* SSLfatal() already called */
  1689. return MSG_PROCESS_ERROR;
  1690. }
  1691. if (peer_rpk == NULL) {
  1692. SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_CERTIFICATE);
  1693. return MSG_PROCESS_ERROR;
  1694. }
  1695. EVP_PKEY_free(sc->session->peer_rpk);
  1696. sc->session->peer_rpk = peer_rpk;
  1697. return MSG_PROCESS_CONTINUE_PROCESSING;
  1698. }
  1699. static WORK_STATE tls_post_process_server_rpk(SSL_CONNECTION *sc,
  1700. WORK_STATE wst)
  1701. {
  1702. size_t certidx;
  1703. const SSL_CERT_LOOKUP *clu;
  1704. if (sc->session->peer_rpk == NULL) {
  1705. SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER,
  1706. SSL_R_INVALID_RAW_PUBLIC_KEY);
  1707. return WORK_ERROR;
  1708. }
  1709. if (sc->rwstate == SSL_RETRY_VERIFY)
  1710. sc->rwstate = SSL_NOTHING;
  1711. if (ssl_verify_rpk(sc, sc->session->peer_rpk) > 0
  1712. && sc->rwstate == SSL_RETRY_VERIFY)
  1713. return WORK_MORE_A;
  1714. if ((clu = ssl_cert_lookup_by_pkey(sc->session->peer_rpk, &certidx,
  1715. SSL_CONNECTION_GET_CTX(sc))) == NULL) {
  1716. SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  1717. return WORK_ERROR;
  1718. }
  1719. /*
  1720. * Check certificate type is consistent with ciphersuite. For TLS 1.3
  1721. * skip check since TLS 1.3 ciphersuites can be used with any certificate
  1722. * type.
  1723. */
  1724. if (!SSL_CONNECTION_IS_TLS13(sc)) {
  1725. if ((clu->amask & sc->s3.tmp.new_cipher->algorithm_auth) == 0) {
  1726. SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_RPK_TYPE);
  1727. return WORK_ERROR;
  1728. }
  1729. }
  1730. /* Ensure there is no peer/peer_chain */
  1731. X509_free(sc->session->peer);
  1732. sc->session->peer = NULL;
  1733. sk_X509_pop_free(sc->session->peer_chain, X509_free);
  1734. sc->session->peer_chain = NULL;
  1735. sc->session->verify_result = sc->verify_result;
  1736. /* Save the current hash state for when we receive the CertificateVerify */
  1737. if (SSL_CONNECTION_IS_TLS13(sc)
  1738. && !ssl_handshake_hash(sc, sc->cert_verify_hash,
  1739. sizeof(sc->cert_verify_hash),
  1740. &sc->cert_verify_hash_len)) {
  1741. /* SSLfatal() already called */
  1742. return WORK_ERROR;
  1743. }
  1744. return WORK_FINISHED_CONTINUE;
  1745. }
  1746. /* prepare server cert verification by setting s->session->peer_chain from pkt */
  1747. MSG_PROCESS_RETURN tls_process_server_certificate(SSL_CONNECTION *s,
  1748. PACKET *pkt)
  1749. {
  1750. unsigned long cert_list_len, cert_len;
  1751. X509 *x = NULL;
  1752. const unsigned char *certstart, *certbytes;
  1753. size_t chainidx;
  1754. unsigned int context = 0;
  1755. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  1756. if (s->ext.server_cert_type == TLSEXT_cert_type_rpk)
  1757. return tls_process_server_rpk(s, pkt);
  1758. if (s->ext.server_cert_type != TLSEXT_cert_type_x509) {
  1759. SSLfatal(s, SSL_AD_UNSUPPORTED_CERTIFICATE,
  1760. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  1761. goto err;
  1762. }
  1763. if ((s->session->peer_chain = sk_X509_new_null()) == NULL) {
  1764. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  1765. goto err;
  1766. }
  1767. if ((SSL_CONNECTION_IS_TLS13(s) && !PACKET_get_1(pkt, &context))
  1768. || context != 0
  1769. || !PACKET_get_net_3(pkt, &cert_list_len)
  1770. || PACKET_remaining(pkt) != cert_list_len
  1771. || PACKET_remaining(pkt) == 0) {
  1772. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1773. goto err;
  1774. }
  1775. for (chainidx = 0; PACKET_remaining(pkt); chainidx++) {
  1776. if (!PACKET_get_net_3(pkt, &cert_len)
  1777. || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
  1778. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
  1779. goto err;
  1780. }
  1781. certstart = certbytes;
  1782. x = X509_new_ex(sctx->libctx, sctx->propq);
  1783. if (x == NULL) {
  1784. SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_ASN1_LIB);
  1785. goto err;
  1786. }
  1787. if (d2i_X509(&x, (const unsigned char **)&certbytes,
  1788. cert_len) == NULL) {
  1789. SSLfatal(s, SSL_AD_BAD_CERTIFICATE, ERR_R_ASN1_LIB);
  1790. goto err;
  1791. }
  1792. if (certbytes != (certstart + cert_len)) {
  1793. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
  1794. goto err;
  1795. }
  1796. if (SSL_CONNECTION_IS_TLS13(s)) {
  1797. RAW_EXTENSION *rawexts = NULL;
  1798. PACKET extensions;
  1799. if (!PACKET_get_length_prefixed_2(pkt, &extensions)) {
  1800. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
  1801. goto err;
  1802. }
  1803. if (!tls_collect_extensions(s, &extensions,
  1804. SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
  1805. NULL, chainidx == 0)
  1806. || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
  1807. rawexts, x, chainidx,
  1808. PACKET_remaining(pkt) == 0)) {
  1809. OPENSSL_free(rawexts);
  1810. /* SSLfatal already called */
  1811. goto err;
  1812. }
  1813. OPENSSL_free(rawexts);
  1814. }
  1815. if (!sk_X509_push(s->session->peer_chain, x)) {
  1816. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  1817. goto err;
  1818. }
  1819. x = NULL;
  1820. }
  1821. return MSG_PROCESS_CONTINUE_PROCESSING;
  1822. err:
  1823. X509_free(x);
  1824. OSSL_STACK_OF_X509_free(s->session->peer_chain);
  1825. s->session->peer_chain = NULL;
  1826. return MSG_PROCESS_ERROR;
  1827. }
  1828. /*
  1829. * Verify the s->session->peer_chain and check server cert type.
  1830. * On success set s->session->peer and s->session->verify_result.
  1831. * Else the peer certificate verification callback may request retry.
  1832. */
  1833. WORK_STATE tls_post_process_server_certificate(SSL_CONNECTION *s,
  1834. WORK_STATE wst)
  1835. {
  1836. X509 *x;
  1837. EVP_PKEY *pkey = NULL;
  1838. const SSL_CERT_LOOKUP *clu;
  1839. size_t certidx;
  1840. int i;
  1841. if (s->ext.server_cert_type == TLSEXT_cert_type_rpk)
  1842. return tls_post_process_server_rpk(s, wst);
  1843. if (s->rwstate == SSL_RETRY_VERIFY)
  1844. s->rwstate = SSL_NOTHING;
  1845. i = ssl_verify_cert_chain(s, s->session->peer_chain);
  1846. if (i > 0 && s->rwstate == SSL_RETRY_VERIFY) {
  1847. return WORK_MORE_A;
  1848. }
  1849. /*
  1850. * The documented interface is that SSL_VERIFY_PEER should be set in order
  1851. * for client side verification of the server certificate to take place.
  1852. * However, historically the code has only checked that *any* flag is set
  1853. * to cause server verification to take place. Use of the other flags makes
  1854. * no sense in client mode. An attempt to clean up the semantics was
  1855. * reverted because at least one application *only* set
  1856. * SSL_VERIFY_FAIL_IF_NO_PEER_CERT. Prior to the clean up this still caused
  1857. * server verification to take place, after the clean up it silently did
  1858. * nothing. SSL_CTX_set_verify()/SSL_set_verify() cannot validate the flags
  1859. * sent to them because they are void functions. Therefore, we now use the
  1860. * (less clean) historic behaviour of performing validation if any flag is
  1861. * set. The *documented* interface remains the same.
  1862. */
  1863. if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
  1864. SSLfatal(s, ssl_x509err2alert(s->verify_result),
  1865. SSL_R_CERTIFICATE_VERIFY_FAILED);
  1866. return WORK_ERROR;
  1867. }
  1868. ERR_clear_error(); /* but we keep s->verify_result */
  1869. /*
  1870. * Inconsistency alert: cert_chain does include the peer's certificate,
  1871. * which we don't include in statem_srvr.c
  1872. */
  1873. x = sk_X509_value(s->session->peer_chain, 0);
  1874. pkey = X509_get0_pubkey(x);
  1875. if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
  1876. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1877. SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
  1878. return WORK_ERROR;
  1879. }
  1880. if ((clu = ssl_cert_lookup_by_pkey(pkey, &certidx,
  1881. SSL_CONNECTION_GET_CTX(s))) == NULL) {
  1882. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  1883. return WORK_ERROR;
  1884. }
  1885. /*
  1886. * Check certificate type is consistent with ciphersuite. For TLS 1.3
  1887. * skip check since TLS 1.3 ciphersuites can be used with any certificate
  1888. * type.
  1889. */
  1890. if (!SSL_CONNECTION_IS_TLS13(s)) {
  1891. if ((clu->amask & s->s3.tmp.new_cipher->algorithm_auth) == 0) {
  1892. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CERTIFICATE_TYPE);
  1893. return WORK_ERROR;
  1894. }
  1895. }
  1896. X509_free(s->session->peer);
  1897. X509_up_ref(x);
  1898. s->session->peer = x;
  1899. s->session->verify_result = s->verify_result;
  1900. /* Ensure there is no RPK */
  1901. EVP_PKEY_free(s->session->peer_rpk);
  1902. s->session->peer_rpk = NULL;
  1903. /* Save the current hash state for when we receive the CertificateVerify */
  1904. if (SSL_CONNECTION_IS_TLS13(s)
  1905. && !ssl_handshake_hash(s, s->cert_verify_hash,
  1906. sizeof(s->cert_verify_hash),
  1907. &s->cert_verify_hash_len)) {
  1908. /* SSLfatal() already called */;
  1909. return WORK_ERROR;
  1910. }
  1911. return WORK_FINISHED_CONTINUE;
  1912. }
  1913. #ifndef OPENSSL_NO_COMP_ALG
  1914. MSG_PROCESS_RETURN tls_process_server_compressed_certificate(SSL_CONNECTION *sc, PACKET *pkt)
  1915. {
  1916. MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
  1917. PACKET tmppkt;
  1918. BUF_MEM *buf = BUF_MEM_new();
  1919. if (tls13_process_compressed_certificate(sc, pkt, &tmppkt, buf) != MSG_PROCESS_ERROR)
  1920. ret = tls_process_server_certificate(sc, &tmppkt);
  1921. BUF_MEM_free(buf);
  1922. return ret;
  1923. }
  1924. #endif
  1925. static int tls_process_ske_psk_preamble(SSL_CONNECTION *s, PACKET *pkt)
  1926. {
  1927. #ifndef OPENSSL_NO_PSK
  1928. PACKET psk_identity_hint;
  1929. /* PSK ciphersuites are preceded by an identity hint */
  1930. if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
  1931. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1932. return 0;
  1933. }
  1934. /*
  1935. * Store PSK identity hint for later use, hint is used in
  1936. * tls_construct_client_key_exchange. Assume that the maximum length of
  1937. * a PSK identity hint can be as long as the maximum length of a PSK
  1938. * identity.
  1939. */
  1940. if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1941. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DATA_LENGTH_TOO_LONG);
  1942. return 0;
  1943. }
  1944. if (PACKET_remaining(&psk_identity_hint) == 0) {
  1945. OPENSSL_free(s->session->psk_identity_hint);
  1946. s->session->psk_identity_hint = NULL;
  1947. } else if (!PACKET_strndup(&psk_identity_hint,
  1948. &s->session->psk_identity_hint)) {
  1949. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1950. return 0;
  1951. }
  1952. return 1;
  1953. #else
  1954. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1955. return 0;
  1956. #endif
  1957. }
  1958. static int tls_process_ske_srp(SSL_CONNECTION *s, PACKET *pkt, EVP_PKEY **pkey)
  1959. {
  1960. #ifndef OPENSSL_NO_SRP
  1961. PACKET prime, generator, salt, server_pub;
  1962. if (!PACKET_get_length_prefixed_2(pkt, &prime)
  1963. || !PACKET_get_length_prefixed_2(pkt, &generator)
  1964. || !PACKET_get_length_prefixed_1(pkt, &salt)
  1965. || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
  1966. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1967. return 0;
  1968. }
  1969. if ((s->srp_ctx.N =
  1970. BN_bin2bn(PACKET_data(&prime),
  1971. (int)PACKET_remaining(&prime), NULL)) == NULL
  1972. || (s->srp_ctx.g =
  1973. BN_bin2bn(PACKET_data(&generator),
  1974. (int)PACKET_remaining(&generator), NULL)) == NULL
  1975. || (s->srp_ctx.s =
  1976. BN_bin2bn(PACKET_data(&salt),
  1977. (int)PACKET_remaining(&salt), NULL)) == NULL
  1978. || (s->srp_ctx.B =
  1979. BN_bin2bn(PACKET_data(&server_pub),
  1980. (int)PACKET_remaining(&server_pub), NULL)) == NULL) {
  1981. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB);
  1982. return 0;
  1983. }
  1984. if (!srp_verify_server_param(s)) {
  1985. /* SSLfatal() already called */
  1986. return 0;
  1987. }
  1988. /* We must check if there is a certificate */
  1989. if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
  1990. *pkey = tls_get_peer_pkey(s);
  1991. return 1;
  1992. #else
  1993. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1994. return 0;
  1995. #endif
  1996. }
  1997. static int tls_process_ske_dhe(SSL_CONNECTION *s, PACKET *pkt, EVP_PKEY **pkey)
  1998. {
  1999. PACKET prime, generator, pub_key;
  2000. EVP_PKEY *peer_tmp = NULL;
  2001. BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL;
  2002. EVP_PKEY_CTX *pctx = NULL;
  2003. OSSL_PARAM *params = NULL;
  2004. OSSL_PARAM_BLD *tmpl = NULL;
  2005. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  2006. int ret = 0;
  2007. if (!PACKET_get_length_prefixed_2(pkt, &prime)
  2008. || !PACKET_get_length_prefixed_2(pkt, &generator)
  2009. || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
  2010. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2011. return 0;
  2012. }
  2013. p = BN_bin2bn(PACKET_data(&prime), (int)PACKET_remaining(&prime), NULL);
  2014. g = BN_bin2bn(PACKET_data(&generator), (int)PACKET_remaining(&generator),
  2015. NULL);
  2016. bnpub_key = BN_bin2bn(PACKET_data(&pub_key),
  2017. (int)PACKET_remaining(&pub_key), NULL);
  2018. if (p == NULL || g == NULL || bnpub_key == NULL) {
  2019. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB);
  2020. goto err;
  2021. }
  2022. tmpl = OSSL_PARAM_BLD_new();
  2023. if (tmpl == NULL
  2024. || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p)
  2025. || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g)
  2026. || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY,
  2027. bnpub_key)
  2028. || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
  2029. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2030. goto err;
  2031. }
  2032. pctx = EVP_PKEY_CTX_new_from_name(sctx->libctx, "DH", sctx->propq);
  2033. if (pctx == NULL) {
  2034. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2035. goto err;
  2036. }
  2037. if (EVP_PKEY_fromdata_init(pctx) <= 0
  2038. || EVP_PKEY_fromdata(pctx, &peer_tmp, EVP_PKEY_KEYPAIR, params) <= 0) {
  2039. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_DH_VALUE);
  2040. goto err;
  2041. }
  2042. EVP_PKEY_CTX_free(pctx);
  2043. pctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, peer_tmp, sctx->propq);
  2044. if (pctx == NULL
  2045. /*
  2046. * EVP_PKEY_param_check() will verify that the DH params are using
  2047. * a safe prime. In this context, because we're using ephemeral DH,
  2048. * we're ok with it not being a safe prime.
  2049. * EVP_PKEY_param_check_quick() skips the safe prime check.
  2050. */
  2051. || EVP_PKEY_param_check_quick(pctx) != 1
  2052. || EVP_PKEY_public_check(pctx) != 1) {
  2053. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_DH_VALUE);
  2054. goto err;
  2055. }
  2056. if (!ssl_security(s, SSL_SECOP_TMP_DH,
  2057. EVP_PKEY_get_security_bits(peer_tmp),
  2058. 0, peer_tmp)) {
  2059. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DH_KEY_TOO_SMALL);
  2060. goto err;
  2061. }
  2062. s->s3.peer_tmp = peer_tmp;
  2063. peer_tmp = NULL;
  2064. /*
  2065. * FIXME: This makes assumptions about which ciphersuites come with
  2066. * public keys. We should have a less ad-hoc way of doing this
  2067. */
  2068. if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
  2069. *pkey = tls_get_peer_pkey(s);
  2070. /* else anonymous DH, so no certificate or pkey. */
  2071. ret = 1;
  2072. err:
  2073. OSSL_PARAM_BLD_free(tmpl);
  2074. OSSL_PARAM_free(params);
  2075. EVP_PKEY_free(peer_tmp);
  2076. EVP_PKEY_CTX_free(pctx);
  2077. BN_free(p);
  2078. BN_free(g);
  2079. BN_free(bnpub_key);
  2080. return ret;
  2081. }
  2082. static int tls_process_ske_ecdhe(SSL_CONNECTION *s, PACKET *pkt, EVP_PKEY **pkey)
  2083. {
  2084. PACKET encoded_pt;
  2085. unsigned int curve_type, curve_id;
  2086. /*
  2087. * Extract elliptic curve parameters and the server's ephemeral ECDH
  2088. * public key. We only support named (not generic) curves and
  2089. * ECParameters in this case is just three bytes.
  2090. */
  2091. if (!PACKET_get_1(pkt, &curve_type) || !PACKET_get_net_2(pkt, &curve_id)) {
  2092. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
  2093. return 0;
  2094. }
  2095. /*
  2096. * Check curve is named curve type and one of our preferences, if not
  2097. * server has sent an invalid curve.
  2098. */
  2099. if (curve_type != NAMED_CURVE_TYPE
  2100. || !tls1_check_group_id(s, curve_id, 1)) {
  2101. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CURVE);
  2102. return 0;
  2103. }
  2104. if ((s->s3.peer_tmp = ssl_generate_param_group(s, curve_id)) == NULL) {
  2105. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2106. SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
  2107. return 0;
  2108. }
  2109. if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
  2110. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2111. return 0;
  2112. }
  2113. if (EVP_PKEY_set1_encoded_public_key(s->s3.peer_tmp,
  2114. PACKET_data(&encoded_pt),
  2115. PACKET_remaining(&encoded_pt)) <= 0) {
  2116. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_ECPOINT);
  2117. return 0;
  2118. }
  2119. /*
  2120. * The ECC/TLS specification does not mention the use of DSA to sign
  2121. * ECParameters in the server key exchange message. We do support RSA
  2122. * and ECDSA.
  2123. */
  2124. if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aECDSA)
  2125. *pkey = tls_get_peer_pkey(s);
  2126. else if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aRSA)
  2127. *pkey = tls_get_peer_pkey(s);
  2128. /* else anonymous ECDH, so no certificate or pkey. */
  2129. /* Cache the agreed upon group in the SSL_SESSION */
  2130. s->session->kex_group = curve_id;
  2131. return 1;
  2132. }
  2133. MSG_PROCESS_RETURN tls_process_key_exchange(SSL_CONNECTION *s, PACKET *pkt)
  2134. {
  2135. long alg_k;
  2136. EVP_PKEY *pkey = NULL;
  2137. EVP_MD_CTX *md_ctx = NULL;
  2138. EVP_PKEY_CTX *pctx = NULL;
  2139. PACKET save_param_start, signature;
  2140. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  2141. alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
  2142. save_param_start = *pkt;
  2143. EVP_PKEY_free(s->s3.peer_tmp);
  2144. s->s3.peer_tmp = NULL;
  2145. if (alg_k & SSL_PSK) {
  2146. if (!tls_process_ske_psk_preamble(s, pkt)) {
  2147. /* SSLfatal() already called */
  2148. goto err;
  2149. }
  2150. }
  2151. /* Nothing else to do for plain PSK or RSAPSK */
  2152. if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
  2153. } else if (alg_k & SSL_kSRP) {
  2154. if (!tls_process_ske_srp(s, pkt, &pkey)) {
  2155. /* SSLfatal() already called */
  2156. goto err;
  2157. }
  2158. } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
  2159. if (!tls_process_ske_dhe(s, pkt, &pkey)) {
  2160. /* SSLfatal() already called */
  2161. goto err;
  2162. }
  2163. } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
  2164. if (!tls_process_ske_ecdhe(s, pkt, &pkey)) {
  2165. /* SSLfatal() already called */
  2166. goto err;
  2167. }
  2168. } else if (alg_k) {
  2169. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
  2170. goto err;
  2171. }
  2172. /* if it was signed, check the signature */
  2173. if (pkey != NULL) {
  2174. PACKET params;
  2175. const EVP_MD *md = NULL;
  2176. unsigned char *tbs;
  2177. size_t tbslen;
  2178. int rv;
  2179. /*
  2180. * |pkt| now points to the beginning of the signature, so the difference
  2181. * equals the length of the parameters.
  2182. */
  2183. if (!PACKET_get_sub_packet(&save_param_start, &params,
  2184. PACKET_remaining(&save_param_start) -
  2185. PACKET_remaining(pkt))) {
  2186. SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
  2187. goto err;
  2188. }
  2189. if (SSL_USE_SIGALGS(s)) {
  2190. unsigned int sigalg;
  2191. if (!PACKET_get_net_2(pkt, &sigalg)) {
  2192. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
  2193. goto err;
  2194. }
  2195. if (tls12_check_peer_sigalg(s, sigalg, pkey) <=0) {
  2196. /* SSLfatal() already called */
  2197. goto err;
  2198. }
  2199. } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) {
  2200. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2201. SSL_R_LEGACY_SIGALG_DISALLOWED_OR_UNSUPPORTED);
  2202. goto err;
  2203. }
  2204. if (!tls1_lookup_md(sctx, s->s3.tmp.peer_sigalg, &md)) {
  2205. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2206. SSL_R_NO_SUITABLE_DIGEST_ALGORITHM);
  2207. goto err;
  2208. }
  2209. if (SSL_USE_SIGALGS(s))
  2210. OSSL_TRACE1(TLS, "USING TLSv1.2 HASH %s\n",
  2211. md == NULL ? "n/a" : EVP_MD_get0_name(md));
  2212. if (!PACKET_get_length_prefixed_2(pkt, &signature)
  2213. || PACKET_remaining(pkt) != 0) {
  2214. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2215. goto err;
  2216. }
  2217. md_ctx = EVP_MD_CTX_new();
  2218. if (md_ctx == NULL) {
  2219. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  2220. goto err;
  2221. }
  2222. if (EVP_DigestVerifyInit_ex(md_ctx, &pctx,
  2223. md == NULL ? NULL : EVP_MD_get0_name(md),
  2224. sctx->libctx, sctx->propq, pkey,
  2225. NULL) <= 0) {
  2226. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  2227. goto err;
  2228. }
  2229. if (SSL_USE_PSS(s)) {
  2230. if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
  2231. || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
  2232. RSA_PSS_SALTLEN_DIGEST) <= 0) {
  2233. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  2234. goto err;
  2235. }
  2236. }
  2237. tbslen = construct_key_exchange_tbs(s, &tbs, PACKET_data(&params),
  2238. PACKET_remaining(&params));
  2239. if (tbslen == 0) {
  2240. /* SSLfatal() already called */
  2241. goto err;
  2242. }
  2243. rv = EVP_DigestVerify(md_ctx, PACKET_data(&signature),
  2244. PACKET_remaining(&signature), tbs, tbslen);
  2245. OPENSSL_free(tbs);
  2246. if (rv <= 0) {
  2247. SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_BAD_SIGNATURE);
  2248. goto err;
  2249. }
  2250. EVP_MD_CTX_free(md_ctx);
  2251. md_ctx = NULL;
  2252. } else {
  2253. /* aNULL, aSRP or PSK do not need public keys */
  2254. if (!(s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
  2255. && !(alg_k & SSL_PSK)) {
  2256. /* Might be wrong key type, check it */
  2257. if (ssl3_check_cert_and_algorithm(s)) {
  2258. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_DATA);
  2259. }
  2260. /* else this shouldn't happen, SSLfatal() already called */
  2261. goto err;
  2262. }
  2263. /* still data left over */
  2264. if (PACKET_remaining(pkt) != 0) {
  2265. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_EXTRA_DATA_IN_MESSAGE);
  2266. goto err;
  2267. }
  2268. }
  2269. return MSG_PROCESS_CONTINUE_READING;
  2270. err:
  2271. EVP_MD_CTX_free(md_ctx);
  2272. return MSG_PROCESS_ERROR;
  2273. }
  2274. MSG_PROCESS_RETURN tls_process_certificate_request(SSL_CONNECTION *s,
  2275. PACKET *pkt)
  2276. {
  2277. /* Clear certificate validity flags */
  2278. if (s->s3.tmp.valid_flags != NULL)
  2279. memset(s->s3.tmp.valid_flags, 0, s->ssl_pkey_num * sizeof(uint32_t));
  2280. else
  2281. s->s3.tmp.valid_flags = OPENSSL_zalloc(s->ssl_pkey_num * sizeof(uint32_t));
  2282. /* Give up for good if allocation didn't work */
  2283. if (s->s3.tmp.valid_flags == NULL)
  2284. return 0;
  2285. if (SSL_CONNECTION_IS_TLS13(s)) {
  2286. PACKET reqctx, extensions;
  2287. RAW_EXTENSION *rawexts = NULL;
  2288. if ((s->shutdown & SSL_SENT_SHUTDOWN) != 0) {
  2289. /*
  2290. * We already sent close_notify. This can only happen in TLSv1.3
  2291. * post-handshake messages. We can't reasonably respond to this, so
  2292. * we just ignore it
  2293. */
  2294. return MSG_PROCESS_FINISHED_READING;
  2295. }
  2296. /* Free and zero certificate types: it is not present in TLS 1.3 */
  2297. OPENSSL_free(s->s3.tmp.ctype);
  2298. s->s3.tmp.ctype = NULL;
  2299. s->s3.tmp.ctype_len = 0;
  2300. OPENSSL_free(s->pha_context);
  2301. s->pha_context = NULL;
  2302. s->pha_context_len = 0;
  2303. if (!PACKET_get_length_prefixed_1(pkt, &reqctx) ||
  2304. !PACKET_memdup(&reqctx, &s->pha_context, &s->pha_context_len)) {
  2305. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2306. return MSG_PROCESS_ERROR;
  2307. }
  2308. if (!PACKET_get_length_prefixed_2(pkt, &extensions)) {
  2309. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
  2310. return MSG_PROCESS_ERROR;
  2311. }
  2312. if (!tls_collect_extensions(s, &extensions,
  2313. SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
  2314. &rawexts, NULL, 1)
  2315. || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
  2316. rawexts, NULL, 0, 1)) {
  2317. /* SSLfatal() already called */
  2318. OPENSSL_free(rawexts);
  2319. return MSG_PROCESS_ERROR;
  2320. }
  2321. OPENSSL_free(rawexts);
  2322. if (!tls1_process_sigalgs(s)) {
  2323. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_LENGTH);
  2324. return MSG_PROCESS_ERROR;
  2325. }
  2326. } else {
  2327. PACKET ctypes;
  2328. /* get the certificate types */
  2329. if (!PACKET_get_length_prefixed_1(pkt, &ctypes)) {
  2330. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2331. return MSG_PROCESS_ERROR;
  2332. }
  2333. if (!PACKET_memdup(&ctypes, &s->s3.tmp.ctype, &s->s3.tmp.ctype_len)) {
  2334. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2335. return MSG_PROCESS_ERROR;
  2336. }
  2337. if (SSL_USE_SIGALGS(s)) {
  2338. PACKET sigalgs;
  2339. if (!PACKET_get_length_prefixed_2(pkt, &sigalgs)) {
  2340. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2341. return MSG_PROCESS_ERROR;
  2342. }
  2343. /*
  2344. * Despite this being for certificates, preserve compatibility
  2345. * with pre-TLS 1.3 and use the regular sigalgs field.
  2346. */
  2347. if (!tls1_save_sigalgs(s, &sigalgs, 0)) {
  2348. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2349. SSL_R_SIGNATURE_ALGORITHMS_ERROR);
  2350. return MSG_PROCESS_ERROR;
  2351. }
  2352. if (!tls1_process_sigalgs(s)) {
  2353. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
  2354. return MSG_PROCESS_ERROR;
  2355. }
  2356. }
  2357. /* get the CA RDNs */
  2358. if (!parse_ca_names(s, pkt)) {
  2359. /* SSLfatal() already called */
  2360. return MSG_PROCESS_ERROR;
  2361. }
  2362. }
  2363. if (PACKET_remaining(pkt) != 0) {
  2364. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2365. return MSG_PROCESS_ERROR;
  2366. }
  2367. /* we should setup a certificate to return.... */
  2368. s->s3.tmp.cert_req = 1;
  2369. /*
  2370. * In TLSv1.3 we don't prepare the client certificate yet. We wait until
  2371. * after the CertificateVerify message has been received. This is because
  2372. * in TLSv1.3 the CertificateRequest arrives before the Certificate message
  2373. * but in TLSv1.2 it is the other way around. We want to make sure that
  2374. * SSL_get1_peer_certificate() returns something sensible in
  2375. * client_cert_cb.
  2376. */
  2377. if (SSL_CONNECTION_IS_TLS13(s)
  2378. && s->post_handshake_auth != SSL_PHA_REQUESTED)
  2379. return MSG_PROCESS_CONTINUE_READING;
  2380. return MSG_PROCESS_CONTINUE_PROCESSING;
  2381. }
  2382. MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL_CONNECTION *s,
  2383. PACKET *pkt)
  2384. {
  2385. unsigned int ticklen;
  2386. unsigned long ticket_lifetime_hint, age_add = 0;
  2387. unsigned int sess_len;
  2388. RAW_EXTENSION *exts = NULL;
  2389. PACKET nonce;
  2390. EVP_MD *sha256 = NULL;
  2391. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  2392. PACKET_null_init(&nonce);
  2393. if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
  2394. || (SSL_CONNECTION_IS_TLS13(s)
  2395. && (!PACKET_get_net_4(pkt, &age_add)
  2396. || !PACKET_get_length_prefixed_1(pkt, &nonce)))
  2397. || !PACKET_get_net_2(pkt, &ticklen)
  2398. || (SSL_CONNECTION_IS_TLS13(s) ? (ticklen == 0
  2399. || PACKET_remaining(pkt) < ticklen)
  2400. : PACKET_remaining(pkt) != ticklen)) {
  2401. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2402. goto err;
  2403. }
  2404. /*
  2405. * Server is allowed to change its mind (in <=TLSv1.2) and send an empty
  2406. * ticket. We already checked this TLSv1.3 case above, so it should never
  2407. * be 0 here in that instance
  2408. */
  2409. if (ticklen == 0)
  2410. return MSG_PROCESS_CONTINUE_READING;
  2411. /*
  2412. * Sessions must be immutable once they go into the session cache. Otherwise
  2413. * we can get multi-thread problems. Therefore we don't "update" sessions,
  2414. * we replace them with a duplicate. In TLSv1.3 we need to do this every
  2415. * time a NewSessionTicket arrives because those messages arrive
  2416. * post-handshake and the session may have already gone into the session
  2417. * cache.
  2418. */
  2419. if (SSL_CONNECTION_IS_TLS13(s) || s->session->session_id_length > 0) {
  2420. SSL_SESSION *new_sess;
  2421. /*
  2422. * We reused an existing session, so we need to replace it with a new
  2423. * one
  2424. */
  2425. if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
  2426. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
  2427. goto err;
  2428. }
  2429. if ((s->session_ctx->session_cache_mode & SSL_SESS_CACHE_CLIENT) != 0
  2430. && !SSL_CONNECTION_IS_TLS13(s)) {
  2431. /*
  2432. * In TLSv1.2 and below the arrival of a new tickets signals that
  2433. * any old ticket we were using is now out of date, so we remove the
  2434. * old session from the cache. We carry on if this fails
  2435. */
  2436. SSL_CTX_remove_session(s->session_ctx, s->session);
  2437. }
  2438. SSL_SESSION_free(s->session);
  2439. s->session = new_sess;
  2440. }
  2441. s->session->time = ossl_time_now();
  2442. ssl_session_calculate_timeout(s->session);
  2443. OPENSSL_free(s->session->ext.tick);
  2444. s->session->ext.tick = NULL;
  2445. s->session->ext.ticklen = 0;
  2446. s->session->ext.tick = OPENSSL_malloc(ticklen);
  2447. if (s->session->ext.tick == NULL) {
  2448. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  2449. goto err;
  2450. }
  2451. if (!PACKET_copy_bytes(pkt, s->session->ext.tick, ticklen)) {
  2452. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2453. goto err;
  2454. }
  2455. s->session->ext.tick_lifetime_hint = ticket_lifetime_hint;
  2456. s->session->ext.tick_age_add = age_add;
  2457. s->session->ext.ticklen = ticklen;
  2458. if (SSL_CONNECTION_IS_TLS13(s)) {
  2459. PACKET extpkt;
  2460. if (!PACKET_as_length_prefixed_2(pkt, &extpkt)
  2461. || PACKET_remaining(pkt) != 0) {
  2462. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2463. goto err;
  2464. }
  2465. if (!tls_collect_extensions(s, &extpkt,
  2466. SSL_EXT_TLS1_3_NEW_SESSION_TICKET, &exts,
  2467. NULL, 1)
  2468. || !tls_parse_all_extensions(s,
  2469. SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
  2470. exts, NULL, 0, 1)) {
  2471. /* SSLfatal() already called */
  2472. goto err;
  2473. }
  2474. }
  2475. /*
  2476. * There are two ways to detect a resumed ticket session. One is to set
  2477. * an appropriate session ID and then the server must return a match in
  2478. * ServerHello. This allows the normal client session ID matching to work
  2479. * and we know much earlier that the ticket has been accepted. The
  2480. * other way is to set zero length session ID when the ticket is
  2481. * presented and rely on the handshake to determine session resumption.
  2482. * We choose the former approach because this fits in with assumptions
  2483. * elsewhere in OpenSSL. The session ID is set to the SHA256 hash of the
  2484. * ticket.
  2485. */
  2486. sha256 = EVP_MD_fetch(sctx->libctx, "SHA2-256", sctx->propq);
  2487. if (sha256 == NULL) {
  2488. /* Error is already recorded */
  2489. SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
  2490. goto err;
  2491. }
  2492. /*
  2493. * We use sess_len here because EVP_Digest expects an int
  2494. * but s->session->session_id_length is a size_t
  2495. */
  2496. if (!EVP_Digest(s->session->ext.tick, ticklen,
  2497. s->session->session_id, &sess_len,
  2498. sha256, NULL)) {
  2499. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  2500. goto err;
  2501. }
  2502. EVP_MD_free(sha256);
  2503. sha256 = NULL;
  2504. s->session->session_id_length = sess_len;
  2505. s->session->not_resumable = 0;
  2506. /* This is a standalone message in TLSv1.3, so there is no more to read */
  2507. if (SSL_CONNECTION_IS_TLS13(s)) {
  2508. const EVP_MD *md = ssl_handshake_md(s);
  2509. int hashleni = EVP_MD_get_size(md);
  2510. size_t hashlen;
  2511. static const unsigned char nonce_label[] = "resumption";
  2512. /* Ensure cast to size_t is safe */
  2513. if (!ossl_assert(hashleni >= 0)) {
  2514. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2515. goto err;
  2516. }
  2517. hashlen = (size_t)hashleni;
  2518. if (!tls13_hkdf_expand(s, md, s->resumption_master_secret,
  2519. nonce_label,
  2520. sizeof(nonce_label) - 1,
  2521. PACKET_data(&nonce),
  2522. PACKET_remaining(&nonce),
  2523. s->session->master_key,
  2524. hashlen, 1)) {
  2525. /* SSLfatal() already called */
  2526. goto err;
  2527. }
  2528. s->session->master_key_length = hashlen;
  2529. OPENSSL_free(exts);
  2530. ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
  2531. return MSG_PROCESS_FINISHED_READING;
  2532. }
  2533. return MSG_PROCESS_CONTINUE_READING;
  2534. err:
  2535. EVP_MD_free(sha256);
  2536. OPENSSL_free(exts);
  2537. return MSG_PROCESS_ERROR;
  2538. }
  2539. /*
  2540. * In TLSv1.3 this is called from the extensions code, otherwise it is used to
  2541. * parse a separate message. Returns 1 on success or 0 on failure
  2542. */
  2543. int tls_process_cert_status_body(SSL_CONNECTION *s, PACKET *pkt)
  2544. {
  2545. size_t resplen;
  2546. unsigned int type;
  2547. if (!PACKET_get_1(pkt, &type)
  2548. || type != TLSEXT_STATUSTYPE_ocsp) {
  2549. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_UNSUPPORTED_STATUS_TYPE);
  2550. return 0;
  2551. }
  2552. if (!PACKET_get_net_3_len(pkt, &resplen)
  2553. || PACKET_remaining(pkt) != resplen) {
  2554. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2555. return 0;
  2556. }
  2557. s->ext.ocsp.resp = OPENSSL_malloc(resplen);
  2558. if (s->ext.ocsp.resp == NULL) {
  2559. s->ext.ocsp.resp_len = 0;
  2560. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  2561. return 0;
  2562. }
  2563. s->ext.ocsp.resp_len = resplen;
  2564. if (!PACKET_copy_bytes(pkt, s->ext.ocsp.resp, resplen)) {
  2565. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2566. return 0;
  2567. }
  2568. return 1;
  2569. }
  2570. MSG_PROCESS_RETURN tls_process_cert_status(SSL_CONNECTION *s, PACKET *pkt)
  2571. {
  2572. if (!tls_process_cert_status_body(s, pkt)) {
  2573. /* SSLfatal() already called */
  2574. return MSG_PROCESS_ERROR;
  2575. }
  2576. return MSG_PROCESS_CONTINUE_READING;
  2577. }
  2578. /*
  2579. * Perform miscellaneous checks and processing after we have received the
  2580. * server's initial flight. In TLS1.3 this is after the Server Finished message.
  2581. * In <=TLS1.2 this is after the ServerDone message. Returns 1 on success or 0
  2582. * on failure.
  2583. */
  2584. int tls_process_initial_server_flight(SSL_CONNECTION *s)
  2585. {
  2586. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  2587. /*
  2588. * at this point we check that we have the required stuff from
  2589. * the server
  2590. */
  2591. if (!ssl3_check_cert_and_algorithm(s)) {
  2592. /* SSLfatal() already called */
  2593. return 0;
  2594. }
  2595. /*
  2596. * Call the ocsp status callback if needed. The |ext.ocsp.resp| and
  2597. * |ext.ocsp.resp_len| values will be set if we actually received a status
  2598. * message, or NULL and -1 otherwise
  2599. */
  2600. if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing
  2601. && sctx->ext.status_cb != NULL) {
  2602. int ret = sctx->ext.status_cb(SSL_CONNECTION_GET_SSL(s),
  2603. sctx->ext.status_arg);
  2604. if (ret == 0) {
  2605. SSLfatal(s, SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE,
  2606. SSL_R_INVALID_STATUS_RESPONSE);
  2607. return 0;
  2608. }
  2609. if (ret < 0) {
  2610. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2611. SSL_R_OCSP_CALLBACK_FAILURE);
  2612. return 0;
  2613. }
  2614. }
  2615. #ifndef OPENSSL_NO_CT
  2616. if (s->ct_validation_callback != NULL) {
  2617. /* Note we validate the SCTs whether or not we abort on error */
  2618. if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) {
  2619. /* SSLfatal() already called */
  2620. return 0;
  2621. }
  2622. }
  2623. #endif
  2624. return 1;
  2625. }
  2626. MSG_PROCESS_RETURN tls_process_server_done(SSL_CONNECTION *s, PACKET *pkt)
  2627. {
  2628. if (PACKET_remaining(pkt) > 0) {
  2629. /* should contain no data */
  2630. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2631. return MSG_PROCESS_ERROR;
  2632. }
  2633. #ifndef OPENSSL_NO_SRP
  2634. if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
  2635. if (ssl_srp_calc_a_param_intern(s) <= 0) {
  2636. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_SRP_A_CALC);
  2637. return MSG_PROCESS_ERROR;
  2638. }
  2639. }
  2640. #endif
  2641. if (!tls_process_initial_server_flight(s)) {
  2642. /* SSLfatal() already called */
  2643. return MSG_PROCESS_ERROR;
  2644. }
  2645. return MSG_PROCESS_FINISHED_READING;
  2646. }
  2647. static int tls_construct_cke_psk_preamble(SSL_CONNECTION *s, WPACKET *pkt)
  2648. {
  2649. #ifndef OPENSSL_NO_PSK
  2650. int ret = 0;
  2651. /*
  2652. * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
  2653. * \0-terminated identity. The last byte is for us for simulating
  2654. * strnlen.
  2655. */
  2656. char identity[PSK_MAX_IDENTITY_LEN + 1];
  2657. size_t identitylen = 0;
  2658. unsigned char psk[PSK_MAX_PSK_LEN];
  2659. unsigned char *tmppsk = NULL;
  2660. char *tmpidentity = NULL;
  2661. size_t psklen = 0;
  2662. if (s->psk_client_callback == NULL) {
  2663. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PSK_NO_CLIENT_CB);
  2664. goto err;
  2665. }
  2666. memset(identity, 0, sizeof(identity));
  2667. psklen = s->psk_client_callback(SSL_CONNECTION_GET_SSL(s),
  2668. s->session->psk_identity_hint,
  2669. identity, sizeof(identity) - 1,
  2670. psk, sizeof(psk));
  2671. if (psklen > PSK_MAX_PSK_LEN) {
  2672. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, ERR_R_INTERNAL_ERROR);
  2673. psklen = PSK_MAX_PSK_LEN; /* Avoid overrunning the array on cleanse */
  2674. goto err;
  2675. } else if (psklen == 0) {
  2676. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_PSK_IDENTITY_NOT_FOUND);
  2677. goto err;
  2678. }
  2679. identitylen = strlen(identity);
  2680. if (identitylen > PSK_MAX_IDENTITY_LEN) {
  2681. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2682. goto err;
  2683. }
  2684. tmppsk = OPENSSL_memdup(psk, psklen);
  2685. tmpidentity = OPENSSL_strdup(identity);
  2686. if (tmppsk == NULL || tmpidentity == NULL) {
  2687. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  2688. goto err;
  2689. }
  2690. OPENSSL_free(s->s3.tmp.psk);
  2691. s->s3.tmp.psk = tmppsk;
  2692. s->s3.tmp.psklen = psklen;
  2693. tmppsk = NULL;
  2694. OPENSSL_free(s->session->psk_identity);
  2695. s->session->psk_identity = tmpidentity;
  2696. tmpidentity = NULL;
  2697. if (!WPACKET_sub_memcpy_u16(pkt, identity, identitylen)) {
  2698. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2699. goto err;
  2700. }
  2701. ret = 1;
  2702. err:
  2703. OPENSSL_cleanse(psk, psklen);
  2704. OPENSSL_cleanse(identity, sizeof(identity));
  2705. OPENSSL_clear_free(tmppsk, psklen);
  2706. OPENSSL_clear_free(tmpidentity, identitylen);
  2707. return ret;
  2708. #else
  2709. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2710. return 0;
  2711. #endif
  2712. }
  2713. static int tls_construct_cke_rsa(SSL_CONNECTION *s, WPACKET *pkt)
  2714. {
  2715. unsigned char *encdata = NULL;
  2716. EVP_PKEY *pkey = NULL;
  2717. EVP_PKEY_CTX *pctx = NULL;
  2718. size_t enclen;
  2719. unsigned char *pms = NULL;
  2720. size_t pmslen = 0;
  2721. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  2722. if (!received_server_cert(s)) {
  2723. /*
  2724. * We should always have a server certificate with SSL_kRSA.
  2725. */
  2726. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2727. return 0;
  2728. }
  2729. if ((pkey = tls_get_peer_pkey(s)) == NULL) {
  2730. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2731. return 0;
  2732. }
  2733. if (!EVP_PKEY_is_a(pkey, "RSA")) {
  2734. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2735. return 0;
  2736. }
  2737. pmslen = SSL_MAX_MASTER_KEY_LENGTH;
  2738. pms = OPENSSL_malloc(pmslen);
  2739. if (pms == NULL) {
  2740. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  2741. return 0;
  2742. }
  2743. pms[0] = s->client_version >> 8;
  2744. pms[1] = s->client_version & 0xff;
  2745. if (RAND_bytes_ex(sctx->libctx, pms + 2, pmslen - 2, 0) <= 0) {
  2746. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_RAND_LIB);
  2747. goto err;
  2748. }
  2749. /* Fix buf for TLS and beyond */
  2750. if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) {
  2751. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2752. goto err;
  2753. }
  2754. pctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pkey, sctx->propq);
  2755. if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
  2756. || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
  2757. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  2758. goto err;
  2759. }
  2760. if (!WPACKET_allocate_bytes(pkt, enclen, &encdata)
  2761. || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) {
  2762. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_RSA_ENCRYPT);
  2763. goto err;
  2764. }
  2765. EVP_PKEY_CTX_free(pctx);
  2766. pctx = NULL;
  2767. /* Fix buf for TLS and beyond */
  2768. if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) {
  2769. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2770. goto err;
  2771. }
  2772. /* Log the premaster secret, if logging is enabled. */
  2773. if (!ssl_log_rsa_client_key_exchange(s, encdata, enclen, pms, pmslen)) {
  2774. /* SSLfatal() already called */
  2775. goto err;
  2776. }
  2777. s->s3.tmp.pms = pms;
  2778. s->s3.tmp.pmslen = pmslen;
  2779. return 1;
  2780. err:
  2781. OPENSSL_clear_free(pms, pmslen);
  2782. EVP_PKEY_CTX_free(pctx);
  2783. return 0;
  2784. }
  2785. static int tls_construct_cke_dhe(SSL_CONNECTION *s, WPACKET *pkt)
  2786. {
  2787. EVP_PKEY *ckey = NULL, *skey = NULL;
  2788. unsigned char *keybytes = NULL;
  2789. int prime_len;
  2790. unsigned char *encoded_pub = NULL;
  2791. size_t encoded_pub_len, pad_len;
  2792. int ret = 0;
  2793. skey = s->s3.peer_tmp;
  2794. if (skey == NULL) {
  2795. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2796. goto err;
  2797. }
  2798. ckey = ssl_generate_pkey(s, skey);
  2799. if (ckey == NULL) {
  2800. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2801. goto err;
  2802. }
  2803. if (ssl_derive(s, ckey, skey, 0) == 0) {
  2804. /* SSLfatal() already called */
  2805. goto err;
  2806. }
  2807. /* send off the data */
  2808. /* Generate encoding of server key */
  2809. encoded_pub_len = EVP_PKEY_get1_encoded_public_key(ckey, &encoded_pub);
  2810. if (encoded_pub_len == 0) {
  2811. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2812. EVP_PKEY_free(ckey);
  2813. return EXT_RETURN_FAIL;
  2814. }
  2815. /*
  2816. * For interoperability with some versions of the Microsoft TLS
  2817. * stack, we need to zero pad the DHE pub key to the same length
  2818. * as the prime.
  2819. */
  2820. prime_len = EVP_PKEY_get_size(ckey);
  2821. pad_len = prime_len - encoded_pub_len;
  2822. if (pad_len > 0) {
  2823. if (!WPACKET_sub_allocate_bytes_u16(pkt, pad_len, &keybytes)) {
  2824. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2825. goto err;
  2826. }
  2827. memset(keybytes, 0, pad_len);
  2828. }
  2829. if (!WPACKET_sub_memcpy_u16(pkt, encoded_pub, encoded_pub_len)) {
  2830. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2831. goto err;
  2832. }
  2833. ret = 1;
  2834. err:
  2835. OPENSSL_free(encoded_pub);
  2836. EVP_PKEY_free(ckey);
  2837. return ret;
  2838. }
  2839. static int tls_construct_cke_ecdhe(SSL_CONNECTION *s, WPACKET *pkt)
  2840. {
  2841. unsigned char *encodedPoint = NULL;
  2842. size_t encoded_pt_len = 0;
  2843. EVP_PKEY *ckey = NULL, *skey = NULL;
  2844. int ret = 0;
  2845. skey = s->s3.peer_tmp;
  2846. if (skey == NULL) {
  2847. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2848. return 0;
  2849. }
  2850. ckey = ssl_generate_pkey(s, skey);
  2851. if (ckey == NULL) {
  2852. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
  2853. goto err;
  2854. }
  2855. if (ssl_derive(s, ckey, skey, 0) == 0) {
  2856. /* SSLfatal() already called */
  2857. goto err;
  2858. }
  2859. /* Generate encoding of client key */
  2860. encoded_pt_len = EVP_PKEY_get1_encoded_public_key(ckey, &encodedPoint);
  2861. if (encoded_pt_len == 0) {
  2862. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
  2863. goto err;
  2864. }
  2865. if (!WPACKET_sub_memcpy_u8(pkt, encodedPoint, encoded_pt_len)) {
  2866. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2867. goto err;
  2868. }
  2869. ret = 1;
  2870. err:
  2871. OPENSSL_free(encodedPoint);
  2872. EVP_PKEY_free(ckey);
  2873. return ret;
  2874. }
  2875. static int tls_construct_cke_gost(SSL_CONNECTION *s, WPACKET *pkt)
  2876. {
  2877. #ifndef OPENSSL_NO_GOST
  2878. /* GOST key exchange message creation */
  2879. EVP_PKEY_CTX *pkey_ctx = NULL;
  2880. EVP_PKEY *pkey = NULL;
  2881. size_t msglen;
  2882. unsigned int md_len;
  2883. unsigned char shared_ukm[32], tmp[256];
  2884. EVP_MD_CTX *ukm_hash = NULL;
  2885. int dgst_nid = NID_id_GostR3411_94;
  2886. unsigned char *pms = NULL;
  2887. size_t pmslen = 0;
  2888. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  2889. if ((s->s3.tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
  2890. dgst_nid = NID_id_GostR3411_2012_256;
  2891. /*
  2892. * Get server certificate PKEY and create ctx from it
  2893. */
  2894. if ((pkey = tls_get_peer_pkey(s)) == NULL) {
  2895. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  2896. SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
  2897. return 0;
  2898. }
  2899. pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx,
  2900. pkey,
  2901. sctx->propq);
  2902. if (pkey_ctx == NULL) {
  2903. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  2904. return 0;
  2905. }
  2906. /*
  2907. * If we have send a certificate, and certificate key
  2908. * parameters match those of server certificate, use
  2909. * certificate key for key exchange
  2910. */
  2911. /* Otherwise, generate ephemeral key pair */
  2912. pmslen = 32;
  2913. pms = OPENSSL_malloc(pmslen);
  2914. if (pms == NULL) {
  2915. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  2916. goto err;
  2917. }
  2918. if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0
  2919. /* Generate session key
  2920. */
  2921. || RAND_bytes_ex(sctx->libctx, pms, pmslen, 0) <= 0) {
  2922. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2923. goto err;
  2924. };
  2925. /*
  2926. * Compute shared IV and store it in algorithm-specific context
  2927. * data
  2928. */
  2929. ukm_hash = EVP_MD_CTX_new();
  2930. if (ukm_hash == NULL
  2931. || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0
  2932. || EVP_DigestUpdate(ukm_hash, s->s3.client_random,
  2933. SSL3_RANDOM_SIZE) <= 0
  2934. || EVP_DigestUpdate(ukm_hash, s->s3.server_random,
  2935. SSL3_RANDOM_SIZE) <= 0
  2936. || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
  2937. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2938. goto err;
  2939. }
  2940. EVP_MD_CTX_free(ukm_hash);
  2941. ukm_hash = NULL;
  2942. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
  2943. EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) <= 0) {
  2944. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
  2945. goto err;
  2946. }
  2947. /* Make GOST keytransport blob message */
  2948. /*
  2949. * Encapsulate it into sequence
  2950. */
  2951. msglen = 255;
  2952. if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
  2953. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
  2954. goto err;
  2955. }
  2956. if (!WPACKET_put_bytes_u8(pkt, V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)
  2957. || (msglen >= 0x80 && !WPACKET_put_bytes_u8(pkt, 0x81))
  2958. || !WPACKET_sub_memcpy_u8(pkt, tmp, msglen)) {
  2959. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2960. goto err;
  2961. }
  2962. EVP_PKEY_CTX_free(pkey_ctx);
  2963. s->s3.tmp.pms = pms;
  2964. s->s3.tmp.pmslen = pmslen;
  2965. return 1;
  2966. err:
  2967. EVP_PKEY_CTX_free(pkey_ctx);
  2968. OPENSSL_clear_free(pms, pmslen);
  2969. EVP_MD_CTX_free(ukm_hash);
  2970. return 0;
  2971. #else
  2972. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2973. return 0;
  2974. #endif
  2975. }
  2976. #ifndef OPENSSL_NO_GOST
  2977. int ossl_gost18_cke_cipher_nid(const SSL_CONNECTION *s)
  2978. {
  2979. if ((s->s3.tmp.new_cipher->algorithm_enc & SSL_MAGMA) != 0)
  2980. return NID_magma_ctr;
  2981. else if ((s->s3.tmp.new_cipher->algorithm_enc & SSL_KUZNYECHIK) != 0)
  2982. return NID_kuznyechik_ctr;
  2983. return NID_undef;
  2984. }
  2985. int ossl_gost_ukm(const SSL_CONNECTION *s, unsigned char *dgst_buf)
  2986. {
  2987. EVP_MD_CTX *hash = NULL;
  2988. unsigned int md_len;
  2989. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  2990. const EVP_MD *md = ssl_evp_md_fetch(sctx->libctx, NID_id_GostR3411_2012_256,
  2991. sctx->propq);
  2992. if (md == NULL)
  2993. return 0;
  2994. if ((hash = EVP_MD_CTX_new()) == NULL
  2995. || EVP_DigestInit(hash, md) <= 0
  2996. || EVP_DigestUpdate(hash, s->s3.client_random, SSL3_RANDOM_SIZE) <= 0
  2997. || EVP_DigestUpdate(hash, s->s3.server_random, SSL3_RANDOM_SIZE) <= 0
  2998. || EVP_DigestFinal_ex(hash, dgst_buf, &md_len) <= 0) {
  2999. EVP_MD_CTX_free(hash);
  3000. ssl_evp_md_free(md);
  3001. return 0;
  3002. }
  3003. EVP_MD_CTX_free(hash);
  3004. ssl_evp_md_free(md);
  3005. return 1;
  3006. }
  3007. #endif
  3008. static int tls_construct_cke_gost18(SSL_CONNECTION *s, WPACKET *pkt)
  3009. {
  3010. #ifndef OPENSSL_NO_GOST
  3011. /* GOST 2018 key exchange message creation */
  3012. unsigned char rnd_dgst[32];
  3013. unsigned char *encdata = NULL;
  3014. EVP_PKEY_CTX *pkey_ctx = NULL;
  3015. EVP_PKEY *pkey;
  3016. unsigned char *pms = NULL;
  3017. size_t pmslen = 0;
  3018. size_t msglen;
  3019. int cipher_nid = ossl_gost18_cke_cipher_nid(s);
  3020. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  3021. if (cipher_nid == NID_undef) {
  3022. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3023. return 0;
  3024. }
  3025. if (ossl_gost_ukm(s, rnd_dgst) <= 0) {
  3026. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3027. goto err;
  3028. }
  3029. /* Pre-master secret - random bytes */
  3030. pmslen = 32;
  3031. pms = OPENSSL_malloc(pmslen);
  3032. if (pms == NULL) {
  3033. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  3034. goto err;
  3035. }
  3036. if (RAND_bytes_ex(sctx->libctx, pms, pmslen, 0) <= 0) {
  3037. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3038. goto err;
  3039. }
  3040. /* Get server certificate PKEY and create ctx from it */
  3041. if ((pkey = tls_get_peer_pkey(s)) == NULL) {
  3042. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3043. SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
  3044. goto err;
  3045. }
  3046. pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx,
  3047. pkey,
  3048. sctx->propq);
  3049. if (pkey_ctx == NULL) {
  3050. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  3051. goto err;
  3052. }
  3053. if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0) {
  3054. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3055. goto err;
  3056. };
  3057. /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code */
  3058. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
  3059. EVP_PKEY_CTRL_SET_IV, 32, rnd_dgst) <= 0) {
  3060. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
  3061. goto err;
  3062. }
  3063. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
  3064. EVP_PKEY_CTRL_CIPHER, cipher_nid, NULL) <= 0) {
  3065. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
  3066. goto err;
  3067. }
  3068. if (EVP_PKEY_encrypt(pkey_ctx, NULL, &msglen, pms, pmslen) <= 0) {
  3069. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  3070. goto err;
  3071. }
  3072. if (!WPACKET_allocate_bytes(pkt, msglen, &encdata)
  3073. || EVP_PKEY_encrypt(pkey_ctx, encdata, &msglen, pms, pmslen) <= 0) {
  3074. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  3075. goto err;
  3076. }
  3077. EVP_PKEY_CTX_free(pkey_ctx);
  3078. pkey_ctx = NULL;
  3079. s->s3.tmp.pms = pms;
  3080. s->s3.tmp.pmslen = pmslen;
  3081. return 1;
  3082. err:
  3083. EVP_PKEY_CTX_free(pkey_ctx);
  3084. OPENSSL_clear_free(pms, pmslen);
  3085. return 0;
  3086. #else
  3087. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3088. return 0;
  3089. #endif
  3090. }
  3091. static int tls_construct_cke_srp(SSL_CONNECTION *s, WPACKET *pkt)
  3092. {
  3093. #ifndef OPENSSL_NO_SRP
  3094. unsigned char *abytes = NULL;
  3095. if (s->srp_ctx.A == NULL
  3096. || !WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(s->srp_ctx.A),
  3097. &abytes)) {
  3098. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3099. return 0;
  3100. }
  3101. BN_bn2bin(s->srp_ctx.A, abytes);
  3102. OPENSSL_free(s->session->srp_username);
  3103. s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
  3104. if (s->session->srp_username == NULL) {
  3105. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  3106. return 0;
  3107. }
  3108. return 1;
  3109. #else
  3110. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3111. return 0;
  3112. #endif
  3113. }
  3114. CON_FUNC_RETURN tls_construct_client_key_exchange(SSL_CONNECTION *s,
  3115. WPACKET *pkt)
  3116. {
  3117. unsigned long alg_k;
  3118. alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
  3119. /*
  3120. * All of the construct functions below call SSLfatal() if necessary so
  3121. * no need to do so here.
  3122. */
  3123. if ((alg_k & SSL_PSK)
  3124. && !tls_construct_cke_psk_preamble(s, pkt))
  3125. goto err;
  3126. if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
  3127. if (!tls_construct_cke_rsa(s, pkt))
  3128. goto err;
  3129. } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
  3130. if (!tls_construct_cke_dhe(s, pkt))
  3131. goto err;
  3132. } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
  3133. if (!tls_construct_cke_ecdhe(s, pkt))
  3134. goto err;
  3135. } else if (alg_k & SSL_kGOST) {
  3136. if (!tls_construct_cke_gost(s, pkt))
  3137. goto err;
  3138. } else if (alg_k & SSL_kGOST18) {
  3139. if (!tls_construct_cke_gost18(s, pkt))
  3140. goto err;
  3141. } else if (alg_k & SSL_kSRP) {
  3142. if (!tls_construct_cke_srp(s, pkt))
  3143. goto err;
  3144. } else if (!(alg_k & SSL_kPSK)) {
  3145. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3146. goto err;
  3147. }
  3148. return CON_FUNC_SUCCESS;
  3149. err:
  3150. OPENSSL_clear_free(s->s3.tmp.pms, s->s3.tmp.pmslen);
  3151. s->s3.tmp.pms = NULL;
  3152. s->s3.tmp.pmslen = 0;
  3153. #ifndef OPENSSL_NO_PSK
  3154. OPENSSL_clear_free(s->s3.tmp.psk, s->s3.tmp.psklen);
  3155. s->s3.tmp.psk = NULL;
  3156. s->s3.tmp.psklen = 0;
  3157. #endif
  3158. return CON_FUNC_ERROR;
  3159. }
  3160. int tls_client_key_exchange_post_work(SSL_CONNECTION *s)
  3161. {
  3162. unsigned char *pms = NULL;
  3163. size_t pmslen = 0;
  3164. pms = s->s3.tmp.pms;
  3165. pmslen = s->s3.tmp.pmslen;
  3166. #ifndef OPENSSL_NO_SRP
  3167. /* Check for SRP */
  3168. if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
  3169. if (!srp_generate_client_master_secret(s)) {
  3170. /* SSLfatal() already called */
  3171. goto err;
  3172. }
  3173. return 1;
  3174. }
  3175. #endif
  3176. if (pms == NULL && !(s->s3.tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
  3177. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_PASSED_INVALID_ARGUMENT);
  3178. goto err;
  3179. }
  3180. if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
  3181. /* SSLfatal() already called */
  3182. /* ssl_generate_master_secret frees the pms even on error */
  3183. pms = NULL;
  3184. pmslen = 0;
  3185. goto err;
  3186. }
  3187. pms = NULL;
  3188. pmslen = 0;
  3189. #ifndef OPENSSL_NO_SCTP
  3190. if (SSL_CONNECTION_IS_DTLS(s)) {
  3191. unsigned char sctpauthkey[64];
  3192. char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
  3193. size_t labellen;
  3194. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  3195. /*
  3196. * Add new shared key for SCTP-Auth, will be ignored if no SCTP
  3197. * used.
  3198. */
  3199. memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
  3200. sizeof(DTLS1_SCTP_AUTH_LABEL));
  3201. /* Don't include the terminating zero. */
  3202. labellen = sizeof(labelbuffer) - 1;
  3203. if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
  3204. labellen += 1;
  3205. if (SSL_export_keying_material(ssl, sctpauthkey,
  3206. sizeof(sctpauthkey), labelbuffer,
  3207. labellen, NULL, 0, 0) <= 0) {
  3208. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3209. goto err;
  3210. }
  3211. BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  3212. sizeof(sctpauthkey), sctpauthkey);
  3213. }
  3214. #endif
  3215. return 1;
  3216. err:
  3217. OPENSSL_clear_free(pms, pmslen);
  3218. s->s3.tmp.pms = NULL;
  3219. s->s3.tmp.pmslen = 0;
  3220. return 0;
  3221. }
  3222. /*
  3223. * Check a certificate can be used for client authentication. Currently check
  3224. * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
  3225. * certificates can be used and optionally checks suitability for Suite B.
  3226. */
  3227. static int ssl3_check_client_certificate(SSL_CONNECTION *s)
  3228. {
  3229. /* If no suitable signature algorithm can't use certificate */
  3230. if (!tls_choose_sigalg(s, 0) || s->s3.tmp.sigalg == NULL)
  3231. return 0;
  3232. /*
  3233. * If strict mode check suitability of chain before using it. This also
  3234. * adjusts suite B digest if necessary.
  3235. */
  3236. if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
  3237. !tls1_check_chain(s, NULL, NULL, NULL, -2))
  3238. return 0;
  3239. return 1;
  3240. }
  3241. WORK_STATE tls_prepare_client_certificate(SSL_CONNECTION *s, WORK_STATE wst)
  3242. {
  3243. X509 *x509 = NULL;
  3244. EVP_PKEY *pkey = NULL;
  3245. int i;
  3246. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  3247. if (wst == WORK_MORE_A) {
  3248. /* Let cert callback update client certificates if required */
  3249. if (s->cert->cert_cb) {
  3250. i = s->cert->cert_cb(ssl, s->cert->cert_cb_arg);
  3251. if (i < 0) {
  3252. s->rwstate = SSL_X509_LOOKUP;
  3253. return WORK_MORE_A;
  3254. }
  3255. if (i == 0) {
  3256. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED);
  3257. return WORK_ERROR;
  3258. }
  3259. s->rwstate = SSL_NOTHING;
  3260. }
  3261. if (ssl3_check_client_certificate(s)) {
  3262. if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
  3263. return WORK_FINISHED_STOP;
  3264. }
  3265. return WORK_FINISHED_CONTINUE;
  3266. }
  3267. /* Fall through to WORK_MORE_B */
  3268. wst = WORK_MORE_B;
  3269. }
  3270. /* We need to get a client cert */
  3271. if (wst == WORK_MORE_B) {
  3272. /*
  3273. * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
  3274. * return(-1); We then get retied later
  3275. */
  3276. i = ssl_do_client_cert_cb(s, &x509, &pkey);
  3277. if (i < 0) {
  3278. s->rwstate = SSL_X509_LOOKUP;
  3279. return WORK_MORE_B;
  3280. }
  3281. s->rwstate = SSL_NOTHING;
  3282. if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
  3283. if (!SSL_use_certificate(ssl, x509)
  3284. || !SSL_use_PrivateKey(ssl, pkey))
  3285. i = 0;
  3286. } else if (i == 1) {
  3287. i = 0;
  3288. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  3289. }
  3290. X509_free(x509);
  3291. EVP_PKEY_free(pkey);
  3292. if (i && !ssl3_check_client_certificate(s))
  3293. i = 0;
  3294. if (i == 0) {
  3295. if (s->version == SSL3_VERSION) {
  3296. s->s3.tmp.cert_req = 0;
  3297. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
  3298. return WORK_FINISHED_CONTINUE;
  3299. } else {
  3300. s->s3.tmp.cert_req = 2;
  3301. s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none;
  3302. if (!ssl3_digest_cached_records(s, 0)) {
  3303. /* SSLfatal() already called */
  3304. return WORK_ERROR;
  3305. }
  3306. }
  3307. }
  3308. if (!SSL_CONNECTION_IS_TLS13(s)
  3309. || (s->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0)
  3310. s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none;
  3311. if (s->post_handshake_auth == SSL_PHA_REQUESTED)
  3312. return WORK_FINISHED_STOP;
  3313. return WORK_FINISHED_CONTINUE;
  3314. }
  3315. /* Shouldn't ever get here */
  3316. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3317. return WORK_ERROR;
  3318. }
  3319. CON_FUNC_RETURN tls_construct_client_certificate(SSL_CONNECTION *s,
  3320. WPACKET *pkt)
  3321. {
  3322. CERT_PKEY *cpk = NULL;
  3323. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  3324. if (SSL_CONNECTION_IS_TLS13(s)) {
  3325. if (s->pha_context == NULL) {
  3326. /* no context available, add 0-length context */
  3327. if (!WPACKET_put_bytes_u8(pkt, 0)) {
  3328. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3329. return CON_FUNC_ERROR;
  3330. }
  3331. } else if (!WPACKET_sub_memcpy_u8(pkt, s->pha_context, s->pha_context_len)) {
  3332. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3333. return CON_FUNC_ERROR;
  3334. }
  3335. }
  3336. if (s->s3.tmp.cert_req != 2)
  3337. cpk = s->cert->key;
  3338. switch (s->ext.client_cert_type) {
  3339. case TLSEXT_cert_type_rpk:
  3340. if (!tls_output_rpk(s, pkt, cpk)) {
  3341. /* SSLfatal() already called */
  3342. return CON_FUNC_ERROR;
  3343. }
  3344. break;
  3345. case TLSEXT_cert_type_x509:
  3346. if (!ssl3_output_cert_chain(s, pkt, cpk, 0)) {
  3347. /* SSLfatal() already called */
  3348. return CON_FUNC_ERROR;
  3349. }
  3350. break;
  3351. default:
  3352. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3353. return CON_FUNC_ERROR;
  3354. }
  3355. /*
  3356. * If we attempted to write early data or we're in middlebox compat mode
  3357. * then we deferred changing the handshake write keys to the last possible
  3358. * moment. We need to do it now.
  3359. */
  3360. if (SSL_CONNECTION_IS_TLS13(s)
  3361. && SSL_IS_FIRST_HANDSHAKE(s)
  3362. && (s->early_data_state != SSL_EARLY_DATA_NONE
  3363. || (s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0)
  3364. && (!ssl->method->ssl3_enc->change_cipher_state(s,
  3365. SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) {
  3366. /*
  3367. * This is a fatal error, which leaves enc_write_ctx in an inconsistent
  3368. * state and thus ssl3_send_alert may crash.
  3369. */
  3370. SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_CANNOT_CHANGE_CIPHER);
  3371. return CON_FUNC_ERROR;
  3372. }
  3373. return CON_FUNC_SUCCESS;
  3374. }
  3375. #ifndef OPENSSL_NO_COMP_ALG
  3376. CON_FUNC_RETURN tls_construct_client_compressed_certificate(SSL_CONNECTION *sc,
  3377. WPACKET *pkt)
  3378. {
  3379. SSL *ssl = SSL_CONNECTION_GET_SSL(sc);
  3380. WPACKET tmppkt;
  3381. BUF_MEM *buf = NULL;
  3382. size_t length;
  3383. size_t max_length;
  3384. COMP_METHOD *method;
  3385. COMP_CTX *comp = NULL;
  3386. int comp_len;
  3387. int ret = 0;
  3388. int alg = sc->ext.compress_certificate_from_peer[0];
  3389. /* Note that sc->s3.tmp.cert_req == 2 is checked in write transition */
  3390. if ((buf = BUF_MEM_new()) == NULL || !WPACKET_init(&tmppkt, buf))
  3391. goto err;
  3392. /* Use the |tmppkt| for the to-be-compressed data */
  3393. if (sc->pha_context == NULL) {
  3394. /* no context available, add 0-length context */
  3395. if (!WPACKET_put_bytes_u8(&tmppkt, 0))
  3396. goto err;
  3397. } else if (!WPACKET_sub_memcpy_u8(&tmppkt, sc->pha_context, sc->pha_context_len))
  3398. goto err;
  3399. if (!ssl3_output_cert_chain(sc, &tmppkt, sc->cert->key, 0)) {
  3400. /* SSLfatal() already called */
  3401. goto out;
  3402. }
  3403. /* continue with the real |pkt| */
  3404. if (!WPACKET_put_bytes_u16(pkt, alg)
  3405. || !WPACKET_get_total_written(&tmppkt, &length)
  3406. || !WPACKET_put_bytes_u24(pkt, length))
  3407. goto err;
  3408. switch (alg) {
  3409. case TLSEXT_comp_cert_zlib:
  3410. method = COMP_zlib_oneshot();
  3411. break;
  3412. case TLSEXT_comp_cert_brotli:
  3413. method = COMP_brotli_oneshot();
  3414. break;
  3415. case TLSEXT_comp_cert_zstd:
  3416. method = COMP_zstd_oneshot();
  3417. break;
  3418. default:
  3419. goto err;
  3420. }
  3421. max_length = ossl_calculate_comp_expansion(alg, length);
  3422. if ((comp = COMP_CTX_new(method)) == NULL
  3423. || !WPACKET_start_sub_packet_u24(pkt)
  3424. || !WPACKET_reserve_bytes(pkt, max_length, NULL))
  3425. goto err;
  3426. comp_len = COMP_compress_block(comp, WPACKET_get_curr(pkt), max_length,
  3427. (unsigned char *)buf->data, length);
  3428. if (comp_len <= 0)
  3429. goto err;
  3430. if (!WPACKET_allocate_bytes(pkt, comp_len, NULL)
  3431. || !WPACKET_close(pkt))
  3432. goto err;
  3433. /*
  3434. * If we attempted to write early data or we're in middlebox compat mode
  3435. * then we deferred changing the handshake write keys to the last possible
  3436. * moment. We need to do it now.
  3437. */
  3438. if (SSL_IS_FIRST_HANDSHAKE(sc)
  3439. && (sc->early_data_state != SSL_EARLY_DATA_NONE
  3440. || (sc->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0)
  3441. && (!ssl->method->ssl3_enc->change_cipher_state(sc,
  3442. SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) {
  3443. /*
  3444. * This is a fatal error, which leaves sc->enc_write_ctx in an
  3445. * inconsistent state and thus ssl3_send_alert may crash.
  3446. */
  3447. SSLfatal(sc, SSL_AD_NO_ALERT, SSL_R_CANNOT_CHANGE_CIPHER);
  3448. goto out;
  3449. }
  3450. ret = 1;
  3451. goto out;
  3452. err:
  3453. SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3454. out:
  3455. if (buf != NULL) {
  3456. /* If |buf| is NULL, then |tmppkt| could not have been initialized */
  3457. WPACKET_cleanup(&tmppkt);
  3458. }
  3459. BUF_MEM_free(buf);
  3460. COMP_CTX_free(comp);
  3461. return ret;
  3462. }
  3463. #endif
  3464. int ssl3_check_cert_and_algorithm(SSL_CONNECTION *s)
  3465. {
  3466. const SSL_CERT_LOOKUP *clu;
  3467. size_t idx;
  3468. long alg_k, alg_a;
  3469. EVP_PKEY *pkey;
  3470. alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
  3471. alg_a = s->s3.tmp.new_cipher->algorithm_auth;
  3472. /* we don't have a certificate */
  3473. if (!(alg_a & SSL_aCERT))
  3474. return 1;
  3475. /* This is the passed certificate */
  3476. pkey = tls_get_peer_pkey(s);
  3477. clu = ssl_cert_lookup_by_pkey(pkey, &idx, SSL_CONNECTION_GET_CTX(s));
  3478. /* Check certificate is recognised and suitable for cipher */
  3479. if (clu == NULL || (alg_a & clu->amask) == 0) {
  3480. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_MISSING_SIGNING_CERT);
  3481. return 0;
  3482. }
  3483. if (alg_k & (SSL_kRSA | SSL_kRSAPSK) && idx != SSL_PKEY_RSA) {
  3484. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3485. SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  3486. return 0;
  3487. }
  3488. if ((alg_k & SSL_kDHE) && (s->s3.peer_tmp == NULL)) {
  3489. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3490. return 0;
  3491. }
  3492. /* Early out to skip the checks below */
  3493. if (s->session->peer_rpk != NULL)
  3494. return 1;
  3495. if (clu->amask & SSL_aECDSA) {
  3496. if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s))
  3497. return 1;
  3498. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_ECC_CERT);
  3499. return 0;
  3500. }
  3501. return 1;
  3502. }
  3503. #ifndef OPENSSL_NO_NEXTPROTONEG
  3504. CON_FUNC_RETURN tls_construct_next_proto(SSL_CONNECTION *s, WPACKET *pkt)
  3505. {
  3506. size_t len, padding_len;
  3507. unsigned char *padding = NULL;
  3508. len = s->ext.npn_len;
  3509. padding_len = 32 - ((len + 2) % 32);
  3510. if (!WPACKET_sub_memcpy_u8(pkt, s->ext.npn, len)
  3511. || !WPACKET_sub_allocate_bytes_u8(pkt, padding_len, &padding)) {
  3512. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3513. return CON_FUNC_ERROR;
  3514. }
  3515. memset(padding, 0, padding_len);
  3516. return CON_FUNC_SUCCESS;
  3517. }
  3518. #endif
  3519. MSG_PROCESS_RETURN tls_process_hello_req(SSL_CONNECTION *s, PACKET *pkt)
  3520. {
  3521. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  3522. if (PACKET_remaining(pkt) > 0) {
  3523. /* should contain no data */
  3524. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  3525. return MSG_PROCESS_ERROR;
  3526. }
  3527. if ((s->options & SSL_OP_NO_RENEGOTIATION)) {
  3528. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
  3529. return MSG_PROCESS_FINISHED_READING;
  3530. }
  3531. /*
  3532. * This is a historical discrepancy (not in the RFC) maintained for
  3533. * compatibility reasons. If a TLS client receives a HelloRequest it will
  3534. * attempt an abbreviated handshake. However if a DTLS client receives a
  3535. * HelloRequest it will do a full handshake. Either behaviour is reasonable
  3536. * but doing one for TLS and another for DTLS is odd.
  3537. */
  3538. if (SSL_CONNECTION_IS_DTLS(s))
  3539. SSL_renegotiate(ssl);
  3540. else
  3541. SSL_renegotiate_abbreviated(ssl);
  3542. return MSG_PROCESS_FINISHED_READING;
  3543. }
  3544. static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL_CONNECTION *s,
  3545. PACKET *pkt)
  3546. {
  3547. PACKET extensions;
  3548. RAW_EXTENSION *rawexts = NULL;
  3549. if (!PACKET_as_length_prefixed_2(pkt, &extensions)
  3550. || PACKET_remaining(pkt) != 0) {
  3551. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  3552. goto err;
  3553. }
  3554. if (!tls_collect_extensions(s, &extensions,
  3555. SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, &rawexts,
  3556. NULL, 1)
  3557. || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
  3558. rawexts, NULL, 0, 1)) {
  3559. /* SSLfatal() already called */
  3560. goto err;
  3561. }
  3562. OPENSSL_free(rawexts);
  3563. return MSG_PROCESS_CONTINUE_READING;
  3564. err:
  3565. OPENSSL_free(rawexts);
  3566. return MSG_PROCESS_ERROR;
  3567. }
  3568. int ssl_do_client_cert_cb(SSL_CONNECTION *s, X509 **px509, EVP_PKEY **ppkey)
  3569. {
  3570. int i = 0;
  3571. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  3572. #ifndef OPENSSL_NO_ENGINE
  3573. if (sctx->client_cert_engine) {
  3574. i = tls_engine_load_ssl_client_cert(s, px509, ppkey);
  3575. if (i != 0)
  3576. return i;
  3577. }
  3578. #endif
  3579. if (sctx->client_cert_cb)
  3580. i = sctx->client_cert_cb(SSL_CONNECTION_GET_SSL(s), px509, ppkey);
  3581. return i;
  3582. }
  3583. int ssl_cipher_list_to_bytes(SSL_CONNECTION *s, STACK_OF(SSL_CIPHER) *sk,
  3584. WPACKET *pkt)
  3585. {
  3586. int i;
  3587. size_t totlen = 0, len, maxlen, maxverok = 0;
  3588. int empty_reneg_info_scsv = !s->renegotiate
  3589. && (SSL_CONNECTION_IS_DTLS(s)
  3590. || s->min_proto_version < TLS1_3_VERSION);
  3591. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  3592. /* Set disabled masks for this session */
  3593. if (!ssl_set_client_disabled(s)) {
  3594. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_PROTOCOLS_AVAILABLE);
  3595. return 0;
  3596. }
  3597. if (sk == NULL) {
  3598. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3599. return 0;
  3600. }
  3601. #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
  3602. # if OPENSSL_MAX_TLS1_2_CIPHER_LENGTH < 6
  3603. # error Max cipher length too short
  3604. # endif
  3605. /*
  3606. * Some servers hang if client hello > 256 bytes as hack workaround
  3607. * chop number of supported ciphers to keep it well below this if we
  3608. * use TLS v1.2
  3609. */
  3610. if (TLS1_get_version(ssl) >= TLS1_2_VERSION)
  3611. maxlen = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
  3612. else
  3613. #endif
  3614. /* Maximum length that can be stored in 2 bytes. Length must be even */
  3615. maxlen = 0xfffe;
  3616. if (empty_reneg_info_scsv)
  3617. maxlen -= 2;
  3618. if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV)
  3619. maxlen -= 2;
  3620. for (i = 0; i < sk_SSL_CIPHER_num(sk) && totlen < maxlen; i++) {
  3621. const SSL_CIPHER *c;
  3622. c = sk_SSL_CIPHER_value(sk, i);
  3623. /* Skip disabled ciphers */
  3624. if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0))
  3625. continue;
  3626. if (!ssl->method->put_cipher_by_char(c, pkt, &len)) {
  3627. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3628. return 0;
  3629. }
  3630. /* Sanity check that the maximum version we offer has ciphers enabled */
  3631. if (!maxverok) {
  3632. if (SSL_CONNECTION_IS_DTLS(s)) {
  3633. if (DTLS_VERSION_GE(c->max_dtls, s->s3.tmp.max_ver)
  3634. && DTLS_VERSION_LE(c->min_dtls, s->s3.tmp.max_ver))
  3635. maxverok = 1;
  3636. } else {
  3637. if (c->max_tls >= s->s3.tmp.max_ver
  3638. && c->min_tls <= s->s3.tmp.max_ver)
  3639. maxverok = 1;
  3640. }
  3641. }
  3642. totlen += len;
  3643. }
  3644. if (totlen == 0 || !maxverok) {
  3645. const char *maxvertext =
  3646. !maxverok
  3647. ? "No ciphers enabled for max supported SSL/TLS version"
  3648. : NULL;
  3649. SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_CIPHERS_AVAILABLE,
  3650. maxvertext);
  3651. return 0;
  3652. }
  3653. if (totlen != 0) {
  3654. if (empty_reneg_info_scsv) {
  3655. static SSL_CIPHER scsv = {
  3656. 0, NULL, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
  3657. };
  3658. if (!ssl->method->put_cipher_by_char(&scsv, pkt, &len)) {
  3659. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3660. return 0;
  3661. }
  3662. }
  3663. if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
  3664. static SSL_CIPHER scsv = {
  3665. 0, NULL, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
  3666. };
  3667. if (!ssl->method->put_cipher_by_char(&scsv, pkt, &len)) {
  3668. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3669. return 0;
  3670. }
  3671. }
  3672. }
  3673. return 1;
  3674. }
  3675. CON_FUNC_RETURN tls_construct_end_of_early_data(SSL_CONNECTION *s, WPACKET *pkt)
  3676. {
  3677. if (s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY
  3678. && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING) {
  3679. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  3680. return CON_FUNC_ERROR;
  3681. }
  3682. s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;
  3683. return CON_FUNC_SUCCESS;
  3684. }