statem_clnt.c 134 KB

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