statem_clnt.c 135 KB

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