statem_clnt.c 134 KB

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