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