statem_clnt.c 135 KB

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