statem_srvr.c 144 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401
  1. /*
  2. * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. * Copyright 2005 Nokia. All rights reserved.
  5. *
  6. * Licensed under the Apache License 2.0 (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. #include <stdio.h>
  12. #include "../ssl_local.h"
  13. #include "statem_local.h"
  14. #include "internal/constant_time.h"
  15. #include "internal/cryptlib.h"
  16. #include <openssl/buffer.h>
  17. #include <openssl/rand.h>
  18. #include <openssl/objects.h>
  19. #include <openssl/evp.h>
  20. #include <openssl/x509.h>
  21. #include <openssl/dh.h>
  22. #include <openssl/rsa.h>
  23. #include <openssl/bn.h>
  24. #include <openssl/md5.h>
  25. #include <openssl/trace.h>
  26. #include <openssl/core_names.h>
  27. #include <openssl/asn1t.h>
  28. #include <openssl/comp.h>
  29. #define TICKET_NONCE_SIZE 8
  30. typedef struct {
  31. ASN1_TYPE *kxBlob;
  32. ASN1_TYPE *opaqueBlob;
  33. } GOST_KX_MESSAGE;
  34. DECLARE_ASN1_FUNCTIONS(GOST_KX_MESSAGE)
  35. ASN1_SEQUENCE(GOST_KX_MESSAGE) = {
  36. ASN1_SIMPLE(GOST_KX_MESSAGE, kxBlob, ASN1_ANY),
  37. ASN1_OPT(GOST_KX_MESSAGE, opaqueBlob, ASN1_ANY),
  38. } ASN1_SEQUENCE_END(GOST_KX_MESSAGE)
  39. IMPLEMENT_ASN1_FUNCTIONS(GOST_KX_MESSAGE)
  40. static CON_FUNC_RETURN tls_construct_encrypted_extensions(SSL_CONNECTION *s,
  41. WPACKET *pkt);
  42. static ossl_inline int received_client_cert(const SSL_CONNECTION *sc)
  43. {
  44. return sc->session->peer_rpk != NULL || sc->session->peer != NULL;
  45. }
  46. /*
  47. * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
  48. * handshake state transitions when a TLSv1.3 server is reading messages from
  49. * the client. The message type that the client has sent is provided in |mt|.
  50. * The current state is in |s->statem.hand_state|.
  51. *
  52. * Return values are 1 for success (transition allowed) and 0 on error
  53. * (transition not allowed)
  54. */
  55. static int ossl_statem_server13_read_transition(SSL_CONNECTION *s, int mt)
  56. {
  57. OSSL_STATEM *st = &s->statem;
  58. /*
  59. * Note: There is no case for TLS_ST_BEFORE because at that stage we have
  60. * not negotiated TLSv1.3 yet, so that case is handled by
  61. * ossl_statem_server_read_transition()
  62. */
  63. switch (st->hand_state) {
  64. default:
  65. break;
  66. case TLS_ST_EARLY_DATA:
  67. if (s->hello_retry_request == SSL_HRR_PENDING) {
  68. if (mt == SSL3_MT_CLIENT_HELLO) {
  69. st->hand_state = TLS_ST_SR_CLNT_HELLO;
  70. return 1;
  71. }
  72. break;
  73. } else if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
  74. if (mt == SSL3_MT_END_OF_EARLY_DATA) {
  75. st->hand_state = TLS_ST_SR_END_OF_EARLY_DATA;
  76. return 1;
  77. }
  78. break;
  79. }
  80. /* Fall through */
  81. case TLS_ST_SR_END_OF_EARLY_DATA:
  82. case TLS_ST_SW_FINISHED:
  83. if (s->s3.tmp.cert_request) {
  84. if (mt == SSL3_MT_CERTIFICATE) {
  85. st->hand_state = TLS_ST_SR_CERT;
  86. return 1;
  87. }
  88. #ifndef OPENSSL_NO_COMP_ALG
  89. if (mt == SSL3_MT_COMPRESSED_CERTIFICATE
  90. && s->ext.compress_certificate_sent) {
  91. st->hand_state = TLS_ST_SR_COMP_CERT;
  92. return 1;
  93. }
  94. #endif
  95. } else {
  96. if (mt == SSL3_MT_FINISHED) {
  97. st->hand_state = TLS_ST_SR_FINISHED;
  98. return 1;
  99. }
  100. }
  101. break;
  102. case TLS_ST_SR_COMP_CERT:
  103. case TLS_ST_SR_CERT:
  104. if (!received_client_cert(s)) {
  105. if (mt == SSL3_MT_FINISHED) {
  106. st->hand_state = TLS_ST_SR_FINISHED;
  107. return 1;
  108. }
  109. } else {
  110. if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
  111. st->hand_state = TLS_ST_SR_CERT_VRFY;
  112. return 1;
  113. }
  114. }
  115. break;
  116. case TLS_ST_SR_CERT_VRFY:
  117. if (mt == SSL3_MT_FINISHED) {
  118. st->hand_state = TLS_ST_SR_FINISHED;
  119. return 1;
  120. }
  121. break;
  122. case TLS_ST_OK:
  123. /*
  124. * Its never ok to start processing handshake messages in the middle of
  125. * early data (i.e. before we've received the end of early data alert)
  126. */
  127. if (s->early_data_state == SSL_EARLY_DATA_READING)
  128. break;
  129. if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
  130. if (mt == SSL3_MT_CERTIFICATE) {
  131. st->hand_state = TLS_ST_SR_CERT;
  132. return 1;
  133. }
  134. #ifndef OPENSSL_NO_COMP_ALG
  135. if (mt == SSL3_MT_COMPRESSED_CERTIFICATE
  136. && s->ext.compress_certificate_sent) {
  137. st->hand_state = TLS_ST_SR_COMP_CERT;
  138. return 1;
  139. }
  140. #endif
  141. }
  142. if (mt == SSL3_MT_KEY_UPDATE && !SSL_IS_QUIC_HANDSHAKE(s)) {
  143. st->hand_state = TLS_ST_SR_KEY_UPDATE;
  144. return 1;
  145. }
  146. break;
  147. }
  148. /* No valid transition found */
  149. return 0;
  150. }
  151. /*
  152. * ossl_statem_server_read_transition() encapsulates the logic for the allowed
  153. * handshake state transitions when the server is reading messages from the
  154. * client. The message type that the client has sent is provided in |mt|. The
  155. * current state is in |s->statem.hand_state|.
  156. *
  157. * Return values are 1 for success (transition allowed) and 0 on error
  158. * (transition not allowed)
  159. */
  160. int ossl_statem_server_read_transition(SSL_CONNECTION *s, int mt)
  161. {
  162. OSSL_STATEM *st = &s->statem;
  163. if (SSL_CONNECTION_IS_TLS13(s)) {
  164. if (!ossl_statem_server13_read_transition(s, mt))
  165. goto err;
  166. return 1;
  167. }
  168. switch (st->hand_state) {
  169. default:
  170. break;
  171. case TLS_ST_BEFORE:
  172. case TLS_ST_OK:
  173. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  174. if (mt == SSL3_MT_CLIENT_HELLO) {
  175. st->hand_state = TLS_ST_SR_CLNT_HELLO;
  176. return 1;
  177. }
  178. break;
  179. case TLS_ST_SW_SRVR_DONE:
  180. /*
  181. * If we get a CKE message after a ServerDone then either
  182. * 1) We didn't request a Certificate
  183. * OR
  184. * 2) If we did request one then
  185. * a) We allow no Certificate to be returned
  186. * AND
  187. * b) We are running SSL3 (in TLS1.0+ the client must return a 0
  188. * list if we requested a certificate)
  189. */
  190. if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
  191. if (s->s3.tmp.cert_request) {
  192. if (s->version == SSL3_VERSION) {
  193. if ((s->verify_mode & SSL_VERIFY_PEER)
  194. && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
  195. /*
  196. * This isn't an unexpected message as such - we're just
  197. * not going to accept it because we require a client
  198. * cert.
  199. */
  200. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  201. SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  202. return 0;
  203. }
  204. st->hand_state = TLS_ST_SR_KEY_EXCH;
  205. return 1;
  206. }
  207. } else {
  208. st->hand_state = TLS_ST_SR_KEY_EXCH;
  209. return 1;
  210. }
  211. } else if (s->s3.tmp.cert_request) {
  212. if (mt == SSL3_MT_CERTIFICATE) {
  213. st->hand_state = TLS_ST_SR_CERT;
  214. return 1;
  215. }
  216. }
  217. break;
  218. case TLS_ST_SR_CERT:
  219. if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
  220. st->hand_state = TLS_ST_SR_KEY_EXCH;
  221. return 1;
  222. }
  223. break;
  224. case TLS_ST_SR_KEY_EXCH:
  225. /*
  226. * We should only process a CertificateVerify message if we have
  227. * received a Certificate from the client. If so then |s->session->peer|
  228. * will be non NULL. In some instances a CertificateVerify message is
  229. * not required even if the peer has sent a Certificate (e.g. such as in
  230. * the case of static DH). In that case |st->no_cert_verify| should be
  231. * set.
  232. */
  233. if (!received_client_cert(s) || st->no_cert_verify) {
  234. if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  235. /*
  236. * For the ECDH ciphersuites when the client sends its ECDH
  237. * pub key in a certificate, the CertificateVerify message is
  238. * not sent. Also for GOST ciphersuites when the client uses
  239. * its key from the certificate for key exchange.
  240. */
  241. st->hand_state = TLS_ST_SR_CHANGE;
  242. return 1;
  243. }
  244. } else {
  245. if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
  246. st->hand_state = TLS_ST_SR_CERT_VRFY;
  247. return 1;
  248. }
  249. }
  250. break;
  251. case TLS_ST_SR_CERT_VRFY:
  252. if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  253. st->hand_state = TLS_ST_SR_CHANGE;
  254. return 1;
  255. }
  256. break;
  257. case TLS_ST_SR_CHANGE:
  258. #ifndef OPENSSL_NO_NEXTPROTONEG
  259. if (s->s3.npn_seen) {
  260. if (mt == SSL3_MT_NEXT_PROTO) {
  261. st->hand_state = TLS_ST_SR_NEXT_PROTO;
  262. return 1;
  263. }
  264. } else {
  265. #endif
  266. if (mt == SSL3_MT_FINISHED) {
  267. st->hand_state = TLS_ST_SR_FINISHED;
  268. return 1;
  269. }
  270. #ifndef OPENSSL_NO_NEXTPROTONEG
  271. }
  272. #endif
  273. break;
  274. #ifndef OPENSSL_NO_NEXTPROTONEG
  275. case TLS_ST_SR_NEXT_PROTO:
  276. if (mt == SSL3_MT_FINISHED) {
  277. st->hand_state = TLS_ST_SR_FINISHED;
  278. return 1;
  279. }
  280. break;
  281. #endif
  282. case TLS_ST_SW_FINISHED:
  283. if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  284. st->hand_state = TLS_ST_SR_CHANGE;
  285. return 1;
  286. }
  287. break;
  288. }
  289. err:
  290. /* No valid transition found */
  291. if (SSL_CONNECTION_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  292. BIO *rbio;
  293. /*
  294. * CCS messages don't have a message sequence number so this is probably
  295. * because of an out-of-order CCS. We'll just drop it.
  296. */
  297. s->init_num = 0;
  298. s->rwstate = SSL_READING;
  299. rbio = SSL_get_rbio(SSL_CONNECTION_GET_SSL(s));
  300. BIO_clear_retry_flags(rbio);
  301. BIO_set_retry_read(rbio);
  302. return 0;
  303. }
  304. SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
  305. return 0;
  306. }
  307. /*
  308. * Should we send a ServerKeyExchange message?
  309. *
  310. * Valid return values are:
  311. * 1: Yes
  312. * 0: No
  313. */
  314. static int send_server_key_exchange(SSL_CONNECTION *s)
  315. {
  316. unsigned long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
  317. /*
  318. * only send a ServerKeyExchange if DH or fortezza but we have a
  319. * sign only certificate PSK: may send PSK identity hints For
  320. * ECC ciphersuites, we send a serverKeyExchange message only if
  321. * the cipher suite is either ECDH-anon or ECDHE. In other cases,
  322. * the server certificate contains the server's public key for
  323. * key exchange.
  324. */
  325. if (alg_k & (SSL_kDHE | SSL_kECDHE)
  326. /*
  327. * PSK: send ServerKeyExchange if PSK identity hint if
  328. * provided
  329. */
  330. #ifndef OPENSSL_NO_PSK
  331. /* Only send SKE if we have identity hint for plain PSK */
  332. || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
  333. && s->cert->psk_identity_hint)
  334. /* For other PSK always send SKE */
  335. || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
  336. #endif
  337. #ifndef OPENSSL_NO_SRP
  338. /* SRP: send ServerKeyExchange */
  339. || (alg_k & SSL_kSRP)
  340. #endif
  341. ) {
  342. return 1;
  343. }
  344. return 0;
  345. }
  346. /*
  347. * Used to determine if we should send a CompressedCertificate message
  348. *
  349. * Returns the algorithm to use, TLSEXT_comp_cert_none means no compression
  350. */
  351. static int get_compressed_certificate_alg(SSL_CONNECTION *sc)
  352. {
  353. #ifndef OPENSSL_NO_COMP_ALG
  354. int *alg = sc->ext.compress_certificate_from_peer;
  355. if (sc->s3.tmp.cert == NULL)
  356. return TLSEXT_comp_cert_none;
  357. for (; *alg != TLSEXT_comp_cert_none; alg++) {
  358. if (sc->s3.tmp.cert->comp_cert[*alg] != NULL)
  359. return *alg;
  360. }
  361. #endif
  362. return TLSEXT_comp_cert_none;
  363. }
  364. /*
  365. * Should we send a CertificateRequest message?
  366. *
  367. * Valid return values are:
  368. * 1: Yes
  369. * 0: No
  370. */
  371. int send_certificate_request(SSL_CONNECTION *s)
  372. {
  373. if (
  374. /* don't request cert unless asked for it: */
  375. s->verify_mode & SSL_VERIFY_PEER
  376. /*
  377. * don't request if post-handshake-only unless doing
  378. * post-handshake in TLSv1.3:
  379. */
  380. && (!SSL_CONNECTION_IS_TLS13(s)
  381. || !(s->verify_mode & SSL_VERIFY_POST_HANDSHAKE)
  382. || s->post_handshake_auth == SSL_PHA_REQUEST_PENDING)
  383. /*
  384. * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
  385. * a second time:
  386. */
  387. && (s->certreqs_sent < 1 ||
  388. !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))
  389. /*
  390. * never request cert in anonymous ciphersuites (see
  391. * section "Certificate request" in SSL 3 drafts and in
  392. * RFC 2246):
  393. */
  394. && (!(s->s3.tmp.new_cipher->algorithm_auth & SSL_aNULL)
  395. /*
  396. * ... except when the application insists on
  397. * verification (against the specs, but statem_clnt.c accepts
  398. * this for SSL 3)
  399. */
  400. || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  401. /* don't request certificate for SRP auth */
  402. && !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aSRP)
  403. /*
  404. * With normal PSK Certificates and Certificate Requests
  405. * are omitted
  406. */
  407. && !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aPSK)) {
  408. return 1;
  409. }
  410. return 0;
  411. }
  412. static int do_compressed_cert(SSL_CONNECTION *sc)
  413. {
  414. /* If we negotiated RPK, we won't attempt to compress it */
  415. return sc->ext.server_cert_type == TLSEXT_cert_type_x509
  416. && get_compressed_certificate_alg(sc) != TLSEXT_comp_cert_none;
  417. }
  418. /*
  419. * ossl_statem_server13_write_transition() works out what handshake state to
  420. * move to next when a TLSv1.3 server is writing messages to be sent to the
  421. * client.
  422. */
  423. static WRITE_TRAN ossl_statem_server13_write_transition(SSL_CONNECTION *s)
  424. {
  425. OSSL_STATEM *st = &s->statem;
  426. /*
  427. * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
  428. * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
  429. */
  430. switch (st->hand_state) {
  431. default:
  432. /* Shouldn't happen */
  433. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  434. return WRITE_TRAN_ERROR;
  435. case TLS_ST_OK:
  436. if (s->key_update != SSL_KEY_UPDATE_NONE) {
  437. st->hand_state = TLS_ST_SW_KEY_UPDATE;
  438. return WRITE_TRAN_CONTINUE;
  439. }
  440. if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
  441. st->hand_state = TLS_ST_SW_CERT_REQ;
  442. return WRITE_TRAN_CONTINUE;
  443. }
  444. if (s->ext.extra_tickets_expected > 0) {
  445. st->hand_state = TLS_ST_SW_SESSION_TICKET;
  446. return WRITE_TRAN_CONTINUE;
  447. }
  448. /* Try to read from the client instead */
  449. return WRITE_TRAN_FINISHED;
  450. case TLS_ST_SR_CLNT_HELLO:
  451. st->hand_state = TLS_ST_SW_SRVR_HELLO;
  452. return WRITE_TRAN_CONTINUE;
  453. case TLS_ST_SW_SRVR_HELLO:
  454. if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
  455. && s->hello_retry_request != SSL_HRR_COMPLETE)
  456. st->hand_state = TLS_ST_SW_CHANGE;
  457. else if (s->hello_retry_request == SSL_HRR_PENDING)
  458. st->hand_state = TLS_ST_EARLY_DATA;
  459. else
  460. st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
  461. return WRITE_TRAN_CONTINUE;
  462. case TLS_ST_SW_CHANGE:
  463. if (s->hello_retry_request == SSL_HRR_PENDING)
  464. st->hand_state = TLS_ST_EARLY_DATA;
  465. else
  466. st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
  467. return WRITE_TRAN_CONTINUE;
  468. case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
  469. if (s->hit)
  470. st->hand_state = TLS_ST_SW_FINISHED;
  471. else if (send_certificate_request(s))
  472. st->hand_state = TLS_ST_SW_CERT_REQ;
  473. else if (do_compressed_cert(s))
  474. st->hand_state = TLS_ST_SW_COMP_CERT;
  475. else
  476. st->hand_state = TLS_ST_SW_CERT;
  477. return WRITE_TRAN_CONTINUE;
  478. case TLS_ST_SW_CERT_REQ:
  479. if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
  480. s->post_handshake_auth = SSL_PHA_REQUESTED;
  481. st->hand_state = TLS_ST_OK;
  482. } else if (do_compressed_cert(s)) {
  483. st->hand_state = TLS_ST_SW_COMP_CERT;
  484. } else {
  485. st->hand_state = TLS_ST_SW_CERT;
  486. }
  487. return WRITE_TRAN_CONTINUE;
  488. case TLS_ST_SW_COMP_CERT:
  489. case TLS_ST_SW_CERT:
  490. st->hand_state = TLS_ST_SW_CERT_VRFY;
  491. return WRITE_TRAN_CONTINUE;
  492. case TLS_ST_SW_CERT_VRFY:
  493. st->hand_state = TLS_ST_SW_FINISHED;
  494. return WRITE_TRAN_CONTINUE;
  495. case TLS_ST_SW_FINISHED:
  496. st->hand_state = TLS_ST_EARLY_DATA;
  497. s->ts_msg_write = ossl_time_now();
  498. return WRITE_TRAN_CONTINUE;
  499. case TLS_ST_EARLY_DATA:
  500. return WRITE_TRAN_FINISHED;
  501. case TLS_ST_SR_FINISHED:
  502. s->ts_msg_read = ossl_time_now();
  503. /*
  504. * Technically we have finished the handshake at this point, but we're
  505. * going to remain "in_init" for now and write out any session tickets
  506. * immediately.
  507. */
  508. if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
  509. s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;
  510. } else if (!s->ext.ticket_expected) {
  511. /*
  512. * If we're not going to renew the ticket then we just finish the
  513. * handshake at this point.
  514. */
  515. st->hand_state = TLS_ST_OK;
  516. return WRITE_TRAN_CONTINUE;
  517. }
  518. if (s->num_tickets > s->sent_tickets)
  519. st->hand_state = TLS_ST_SW_SESSION_TICKET;
  520. else
  521. st->hand_state = TLS_ST_OK;
  522. return WRITE_TRAN_CONTINUE;
  523. case TLS_ST_SR_KEY_UPDATE:
  524. case TLS_ST_SW_KEY_UPDATE:
  525. st->hand_state = TLS_ST_OK;
  526. return WRITE_TRAN_CONTINUE;
  527. case TLS_ST_SW_SESSION_TICKET:
  528. /* In a resumption we only ever send a maximum of one new ticket.
  529. * Following an initial handshake we send the number of tickets we have
  530. * been configured for.
  531. */
  532. if (!SSL_IS_FIRST_HANDSHAKE(s) && s->ext.extra_tickets_expected > 0) {
  533. return WRITE_TRAN_CONTINUE;
  534. } else if (s->hit || s->num_tickets <= s->sent_tickets) {
  535. /* We've written enough tickets out. */
  536. st->hand_state = TLS_ST_OK;
  537. }
  538. return WRITE_TRAN_CONTINUE;
  539. }
  540. }
  541. /*
  542. * ossl_statem_server_write_transition() works out what handshake state to move
  543. * to next when the server is writing messages to be sent to the client.
  544. */
  545. WRITE_TRAN ossl_statem_server_write_transition(SSL_CONNECTION *s)
  546. {
  547. OSSL_STATEM *st = &s->statem;
  548. /*
  549. * Note that before the ClientHello we don't know what version we are going
  550. * to negotiate yet, so we don't take this branch until later
  551. */
  552. if (SSL_CONNECTION_IS_TLS13(s))
  553. return ossl_statem_server13_write_transition(s);
  554. switch (st->hand_state) {
  555. default:
  556. /* Shouldn't happen */
  557. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  558. return WRITE_TRAN_ERROR;
  559. case TLS_ST_OK:
  560. if (st->request_state == TLS_ST_SW_HELLO_REQ) {
  561. /* We must be trying to renegotiate */
  562. st->hand_state = TLS_ST_SW_HELLO_REQ;
  563. st->request_state = TLS_ST_BEFORE;
  564. return WRITE_TRAN_CONTINUE;
  565. }
  566. /* Must be an incoming ClientHello */
  567. if (!tls_setup_handshake(s)) {
  568. /* SSLfatal() already called */
  569. return WRITE_TRAN_ERROR;
  570. }
  571. /* Fall through */
  572. case TLS_ST_BEFORE:
  573. /* Just go straight to trying to read from the client */
  574. return WRITE_TRAN_FINISHED;
  575. case TLS_ST_SW_HELLO_REQ:
  576. st->hand_state = TLS_ST_OK;
  577. return WRITE_TRAN_CONTINUE;
  578. case TLS_ST_SR_CLNT_HELLO:
  579. if (SSL_CONNECTION_IS_DTLS(s) && !s->d1->cookie_verified
  580. && (SSL_get_options(SSL_CONNECTION_GET_SSL(s)) & SSL_OP_COOKIE_EXCHANGE)) {
  581. st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;
  582. } else if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
  583. /* We must have rejected the renegotiation */
  584. st->hand_state = TLS_ST_OK;
  585. return WRITE_TRAN_CONTINUE;
  586. } else {
  587. st->hand_state = TLS_ST_SW_SRVR_HELLO;
  588. }
  589. return WRITE_TRAN_CONTINUE;
  590. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  591. return WRITE_TRAN_FINISHED;
  592. case TLS_ST_SW_SRVR_HELLO:
  593. if (s->hit) {
  594. if (s->ext.ticket_expected)
  595. st->hand_state = TLS_ST_SW_SESSION_TICKET;
  596. else
  597. st->hand_state = TLS_ST_SW_CHANGE;
  598. } else {
  599. /* Check if it is anon DH or anon ECDH, */
  600. /* normal PSK or SRP */
  601. if (!(s->s3.tmp.new_cipher->algorithm_auth &
  602. (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
  603. st->hand_state = TLS_ST_SW_CERT;
  604. } else if (send_server_key_exchange(s)) {
  605. st->hand_state = TLS_ST_SW_KEY_EXCH;
  606. } else if (send_certificate_request(s)) {
  607. st->hand_state = TLS_ST_SW_CERT_REQ;
  608. } else {
  609. st->hand_state = TLS_ST_SW_SRVR_DONE;
  610. }
  611. }
  612. return WRITE_TRAN_CONTINUE;
  613. case TLS_ST_SW_CERT:
  614. if (s->ext.status_expected) {
  615. st->hand_state = TLS_ST_SW_CERT_STATUS;
  616. return WRITE_TRAN_CONTINUE;
  617. }
  618. /* Fall through */
  619. case TLS_ST_SW_CERT_STATUS:
  620. if (send_server_key_exchange(s)) {
  621. st->hand_state = TLS_ST_SW_KEY_EXCH;
  622. return WRITE_TRAN_CONTINUE;
  623. }
  624. /* Fall through */
  625. case TLS_ST_SW_KEY_EXCH:
  626. if (send_certificate_request(s)) {
  627. st->hand_state = TLS_ST_SW_CERT_REQ;
  628. return WRITE_TRAN_CONTINUE;
  629. }
  630. /* Fall through */
  631. case TLS_ST_SW_CERT_REQ:
  632. st->hand_state = TLS_ST_SW_SRVR_DONE;
  633. return WRITE_TRAN_CONTINUE;
  634. case TLS_ST_SW_SRVR_DONE:
  635. s->ts_msg_write = ossl_time_now();
  636. return WRITE_TRAN_FINISHED;
  637. case TLS_ST_SR_FINISHED:
  638. s->ts_msg_read = ossl_time_now();
  639. if (s->hit) {
  640. st->hand_state = TLS_ST_OK;
  641. return WRITE_TRAN_CONTINUE;
  642. } else if (s->ext.ticket_expected) {
  643. st->hand_state = TLS_ST_SW_SESSION_TICKET;
  644. } else {
  645. st->hand_state = TLS_ST_SW_CHANGE;
  646. }
  647. return WRITE_TRAN_CONTINUE;
  648. case TLS_ST_SW_SESSION_TICKET:
  649. st->hand_state = TLS_ST_SW_CHANGE;
  650. return WRITE_TRAN_CONTINUE;
  651. case TLS_ST_SW_CHANGE:
  652. st->hand_state = TLS_ST_SW_FINISHED;
  653. return WRITE_TRAN_CONTINUE;
  654. case TLS_ST_SW_FINISHED:
  655. if (s->hit) {
  656. return WRITE_TRAN_FINISHED;
  657. }
  658. st->hand_state = TLS_ST_OK;
  659. return WRITE_TRAN_CONTINUE;
  660. }
  661. }
  662. /*
  663. * Perform any pre work that needs to be done prior to sending a message from
  664. * the server to the client.
  665. */
  666. WORK_STATE ossl_statem_server_pre_work(SSL_CONNECTION *s, WORK_STATE wst)
  667. {
  668. OSSL_STATEM *st = &s->statem;
  669. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  670. switch (st->hand_state) {
  671. default:
  672. /* No pre work to be done */
  673. break;
  674. case TLS_ST_SW_HELLO_REQ:
  675. s->shutdown = 0;
  676. if (SSL_CONNECTION_IS_DTLS(s))
  677. dtls1_clear_sent_buffer(s);
  678. break;
  679. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  680. s->shutdown = 0;
  681. if (SSL_CONNECTION_IS_DTLS(s)) {
  682. dtls1_clear_sent_buffer(s);
  683. /* We don't buffer this message so don't use the timer */
  684. st->use_timer = 0;
  685. }
  686. break;
  687. case TLS_ST_SW_SRVR_HELLO:
  688. if (SSL_CONNECTION_IS_DTLS(s)) {
  689. /*
  690. * Messages we write from now on should be buffered and
  691. * retransmitted if necessary, so we need to use the timer now
  692. */
  693. st->use_timer = 1;
  694. }
  695. break;
  696. case TLS_ST_SW_SRVR_DONE:
  697. #ifndef OPENSSL_NO_SCTP
  698. if (SSL_CONNECTION_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(ssl))) {
  699. /* Calls SSLfatal() as required */
  700. return dtls_wait_for_dry(s);
  701. }
  702. #endif
  703. return WORK_FINISHED_CONTINUE;
  704. case TLS_ST_SW_SESSION_TICKET:
  705. if (SSL_CONNECTION_IS_TLS13(s) && s->sent_tickets == 0
  706. && s->ext.extra_tickets_expected == 0) {
  707. /*
  708. * Actually this is the end of the handshake, but we're going
  709. * straight into writing the session ticket out. So we finish off
  710. * the handshake, but keep the various buffers active.
  711. *
  712. * Calls SSLfatal as required.
  713. */
  714. return tls_finish_handshake(s, wst, 0, 0);
  715. }
  716. if (SSL_CONNECTION_IS_DTLS(s)) {
  717. /*
  718. * We're into the last flight. We don't retransmit the last flight
  719. * unless we need to, so we don't use the timer
  720. */
  721. st->use_timer = 0;
  722. }
  723. break;
  724. case TLS_ST_SW_CHANGE:
  725. if (SSL_CONNECTION_IS_TLS13(s))
  726. break;
  727. /* Writes to s->session are only safe for initial handshakes */
  728. if (s->session->cipher == NULL) {
  729. s->session->cipher = s->s3.tmp.new_cipher;
  730. } else if (s->session->cipher != s->s3.tmp.new_cipher) {
  731. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  732. return WORK_ERROR;
  733. }
  734. if (!ssl->method->ssl3_enc->setup_key_block(s)) {
  735. /* SSLfatal() already called */
  736. return WORK_ERROR;
  737. }
  738. if (SSL_CONNECTION_IS_DTLS(s)) {
  739. /*
  740. * We're into the last flight. We don't retransmit the last flight
  741. * unless we need to, so we don't use the timer. This might have
  742. * already been set to 0 if we sent a NewSessionTicket message,
  743. * but we'll set it again here in case we didn't.
  744. */
  745. st->use_timer = 0;
  746. }
  747. return WORK_FINISHED_CONTINUE;
  748. case TLS_ST_EARLY_DATA:
  749. if (s->early_data_state != SSL_EARLY_DATA_ACCEPTING
  750. && (s->s3.flags & TLS1_FLAGS_STATELESS) == 0)
  751. return WORK_FINISHED_CONTINUE;
  752. /* Fall through */
  753. case TLS_ST_OK:
  754. /* Calls SSLfatal() as required */
  755. return tls_finish_handshake(s, wst, 1, 1);
  756. }
  757. return WORK_FINISHED_CONTINUE;
  758. }
  759. static ossl_inline int conn_is_closed(void)
  760. {
  761. switch (get_last_sys_error()) {
  762. #if defined(EPIPE)
  763. case EPIPE:
  764. return 1;
  765. #endif
  766. #if defined(ECONNRESET)
  767. case ECONNRESET:
  768. return 1;
  769. #endif
  770. #if defined(WSAECONNRESET)
  771. case WSAECONNRESET:
  772. return 1;
  773. #endif
  774. default:
  775. return 0;
  776. }
  777. }
  778. /*
  779. * Perform any work that needs to be done after sending a message from the
  780. * server to the client.
  781. */
  782. WORK_STATE ossl_statem_server_post_work(SSL_CONNECTION *s, WORK_STATE wst)
  783. {
  784. OSSL_STATEM *st = &s->statem;
  785. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  786. s->init_num = 0;
  787. switch (st->hand_state) {
  788. default:
  789. /* No post work to be done */
  790. break;
  791. case TLS_ST_SW_HELLO_REQ:
  792. if (statem_flush(s) != 1)
  793. return WORK_MORE_A;
  794. if (!ssl3_init_finished_mac(s)) {
  795. /* SSLfatal() already called */
  796. return WORK_ERROR;
  797. }
  798. break;
  799. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  800. if (statem_flush(s) != 1)
  801. return WORK_MORE_A;
  802. /* HelloVerifyRequest resets Finished MAC */
  803. if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) {
  804. /* SSLfatal() already called */
  805. return WORK_ERROR;
  806. }
  807. /*
  808. * The next message should be another ClientHello which we need to
  809. * treat like it was the first packet
  810. */
  811. s->first_packet = 1;
  812. break;
  813. case TLS_ST_SW_SRVR_HELLO:
  814. if (SSL_CONNECTION_IS_TLS13(s)
  815. && s->hello_retry_request == SSL_HRR_PENDING) {
  816. if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0
  817. && statem_flush(s) != 1)
  818. return WORK_MORE_A;
  819. break;
  820. }
  821. #ifndef OPENSSL_NO_SCTP
  822. if (SSL_CONNECTION_IS_DTLS(s) && s->hit) {
  823. unsigned char sctpauthkey[64];
  824. char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
  825. size_t labellen;
  826. /*
  827. * Add new shared key for SCTP-Auth, will be ignored if no
  828. * SCTP used.
  829. */
  830. memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
  831. sizeof(DTLS1_SCTP_AUTH_LABEL));
  832. /* Don't include the terminating zero. */
  833. labellen = sizeof(labelbuffer) - 1;
  834. if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
  835. labellen += 1;
  836. if (SSL_export_keying_material(ssl, sctpauthkey,
  837. sizeof(sctpauthkey), labelbuffer,
  838. labellen, NULL, 0,
  839. 0) <= 0) {
  840. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  841. return WORK_ERROR;
  842. }
  843. BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  844. sizeof(sctpauthkey), sctpauthkey);
  845. }
  846. #endif
  847. if (!SSL_CONNECTION_IS_TLS13(s)
  848. || ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
  849. && s->hello_retry_request != SSL_HRR_COMPLETE))
  850. break;
  851. /* Fall through */
  852. case TLS_ST_SW_CHANGE:
  853. if (s->hello_retry_request == SSL_HRR_PENDING) {
  854. if (!statem_flush(s))
  855. return WORK_MORE_A;
  856. break;
  857. }
  858. if (SSL_CONNECTION_IS_TLS13(s)) {
  859. if (!ssl->method->ssl3_enc->setup_key_block(s)
  860. || !ssl->method->ssl3_enc->change_cipher_state(s,
  861. SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
  862. /* SSLfatal() already called */
  863. return WORK_ERROR;
  864. }
  865. if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED
  866. && !ssl->method->ssl3_enc->change_cipher_state(s,
  867. SSL3_CC_HANDSHAKE |SSL3_CHANGE_CIPHER_SERVER_READ)) {
  868. /* SSLfatal() already called */
  869. return WORK_ERROR;
  870. }
  871. /*
  872. * We don't yet know whether the next record we are going to receive
  873. * is an unencrypted alert, an encrypted alert, or an encrypted
  874. * handshake message. We temporarily tolerate unencrypted alerts.
  875. */
  876. if (s->rlayer.rrlmethod->set_plain_alerts != NULL)
  877. s->rlayer.rrlmethod->set_plain_alerts(s->rlayer.rrl, 1);
  878. break;
  879. }
  880. #ifndef OPENSSL_NO_SCTP
  881. if (SSL_CONNECTION_IS_DTLS(s) && !s->hit) {
  882. /*
  883. * Change to new shared key of SCTP-Auth, will be ignored if
  884. * no SCTP used.
  885. */
  886. BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  887. 0, NULL);
  888. }
  889. #endif
  890. if (!ssl->method->ssl3_enc->change_cipher_state(s,
  891. SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
  892. /* SSLfatal() already called */
  893. return WORK_ERROR;
  894. }
  895. break;
  896. case TLS_ST_SW_SRVR_DONE:
  897. if (statem_flush(s) != 1)
  898. return WORK_MORE_A;
  899. break;
  900. case TLS_ST_SW_FINISHED:
  901. if (statem_flush(s) != 1)
  902. return WORK_MORE_A;
  903. #ifndef OPENSSL_NO_SCTP
  904. if (SSL_CONNECTION_IS_DTLS(s) && s->hit) {
  905. /*
  906. * Change to new shared key of SCTP-Auth, will be ignored if
  907. * no SCTP used.
  908. */
  909. BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  910. 0, NULL);
  911. }
  912. #endif
  913. if (SSL_CONNECTION_IS_TLS13(s)) {
  914. /* TLS 1.3 gets the secret size from the handshake md */
  915. size_t dummy;
  916. if (!ssl->method->ssl3_enc->generate_master_secret(s,
  917. s->master_secret, s->handshake_secret, 0,
  918. &dummy)
  919. || !ssl->method->ssl3_enc->change_cipher_state(s,
  920. SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE))
  921. /* SSLfatal() already called */
  922. return WORK_ERROR;
  923. }
  924. break;
  925. case TLS_ST_SW_CERT_REQ:
  926. if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
  927. if (statem_flush(s) != 1)
  928. return WORK_MORE_A;
  929. } else {
  930. if (!SSL_CONNECTION_IS_TLS13(s)
  931. || (s->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0)
  932. s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none;
  933. }
  934. break;
  935. case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
  936. if (!s->hit && !send_certificate_request(s)) {
  937. if (!SSL_CONNECTION_IS_TLS13(s)
  938. || (s->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0)
  939. s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none;
  940. }
  941. break;
  942. case TLS_ST_SW_KEY_UPDATE:
  943. if (statem_flush(s) != 1)
  944. return WORK_MORE_A;
  945. if (!tls13_update_key(s, 1)) {
  946. /* SSLfatal() already called */
  947. return WORK_ERROR;
  948. }
  949. break;
  950. case TLS_ST_SW_SESSION_TICKET:
  951. clear_sys_error();
  952. if (SSL_CONNECTION_IS_TLS13(s) && statem_flush(s) != 1) {
  953. if (SSL_get_error(ssl, 0) == SSL_ERROR_SYSCALL
  954. && conn_is_closed()) {
  955. /*
  956. * We ignore connection closed errors in TLSv1.3 when sending a
  957. * NewSessionTicket and behave as if we were successful. This is
  958. * so that we are still able to read data sent to us by a client
  959. * that closes soon after the end of the handshake without
  960. * waiting to read our post-handshake NewSessionTickets.
  961. */
  962. s->rwstate = SSL_NOTHING;
  963. break;
  964. }
  965. return WORK_MORE_A;
  966. }
  967. break;
  968. }
  969. return WORK_FINISHED_CONTINUE;
  970. }
  971. /*
  972. * Get the message construction function and message type for sending from the
  973. * server
  974. *
  975. * Valid return values are:
  976. * 1: Success
  977. * 0: Error
  978. */
  979. int ossl_statem_server_construct_message(SSL_CONNECTION *s,
  980. confunc_f *confunc, int *mt)
  981. {
  982. OSSL_STATEM *st = &s->statem;
  983. switch (st->hand_state) {
  984. default:
  985. /* Shouldn't happen */
  986. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE);
  987. return 0;
  988. case TLS_ST_SW_CHANGE:
  989. if (SSL_CONNECTION_IS_DTLS(s))
  990. *confunc = dtls_construct_change_cipher_spec;
  991. else
  992. *confunc = tls_construct_change_cipher_spec;
  993. *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
  994. break;
  995. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  996. *confunc = dtls_construct_hello_verify_request;
  997. *mt = DTLS1_MT_HELLO_VERIFY_REQUEST;
  998. break;
  999. case TLS_ST_SW_HELLO_REQ:
  1000. /* No construction function needed */
  1001. *confunc = NULL;
  1002. *mt = SSL3_MT_HELLO_REQUEST;
  1003. break;
  1004. case TLS_ST_SW_SRVR_HELLO:
  1005. *confunc = tls_construct_server_hello;
  1006. *mt = SSL3_MT_SERVER_HELLO;
  1007. break;
  1008. case TLS_ST_SW_CERT:
  1009. *confunc = tls_construct_server_certificate;
  1010. *mt = SSL3_MT_CERTIFICATE;
  1011. break;
  1012. #ifndef OPENSSL_NO_COMP_ALG
  1013. case TLS_ST_SW_COMP_CERT:
  1014. *confunc = tls_construct_server_compressed_certificate;
  1015. *mt = SSL3_MT_COMPRESSED_CERTIFICATE;
  1016. break;
  1017. #endif
  1018. case TLS_ST_SW_CERT_VRFY:
  1019. *confunc = tls_construct_cert_verify;
  1020. *mt = SSL3_MT_CERTIFICATE_VERIFY;
  1021. break;
  1022. case TLS_ST_SW_KEY_EXCH:
  1023. *confunc = tls_construct_server_key_exchange;
  1024. *mt = SSL3_MT_SERVER_KEY_EXCHANGE;
  1025. break;
  1026. case TLS_ST_SW_CERT_REQ:
  1027. *confunc = tls_construct_certificate_request;
  1028. *mt = SSL3_MT_CERTIFICATE_REQUEST;
  1029. break;
  1030. case TLS_ST_SW_SRVR_DONE:
  1031. *confunc = tls_construct_server_done;
  1032. *mt = SSL3_MT_SERVER_DONE;
  1033. break;
  1034. case TLS_ST_SW_SESSION_TICKET:
  1035. *confunc = tls_construct_new_session_ticket;
  1036. *mt = SSL3_MT_NEWSESSION_TICKET;
  1037. break;
  1038. case TLS_ST_SW_CERT_STATUS:
  1039. *confunc = tls_construct_cert_status;
  1040. *mt = SSL3_MT_CERTIFICATE_STATUS;
  1041. break;
  1042. case TLS_ST_SW_FINISHED:
  1043. *confunc = tls_construct_finished;
  1044. *mt = SSL3_MT_FINISHED;
  1045. break;
  1046. case TLS_ST_EARLY_DATA:
  1047. *confunc = NULL;
  1048. *mt = SSL3_MT_DUMMY;
  1049. break;
  1050. case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
  1051. *confunc = tls_construct_encrypted_extensions;
  1052. *mt = SSL3_MT_ENCRYPTED_EXTENSIONS;
  1053. break;
  1054. case TLS_ST_SW_KEY_UPDATE:
  1055. *confunc = tls_construct_key_update;
  1056. *mt = SSL3_MT_KEY_UPDATE;
  1057. break;
  1058. }
  1059. return 1;
  1060. }
  1061. /*
  1062. * Maximum size (excluding the Handshake header) of a ClientHello message,
  1063. * calculated as follows:
  1064. *
  1065. * 2 + # client_version
  1066. * 32 + # only valid length for random
  1067. * 1 + # length of session_id
  1068. * 32 + # maximum size for session_id
  1069. * 2 + # length of cipher suites
  1070. * 2^16-2 + # maximum length of cipher suites array
  1071. * 1 + # length of compression_methods
  1072. * 2^8-1 + # maximum length of compression methods
  1073. * 2 + # length of extensions
  1074. * 2^16-1 # maximum length of extensions
  1075. */
  1076. #define CLIENT_HELLO_MAX_LENGTH 131396
  1077. #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
  1078. #define NEXT_PROTO_MAX_LENGTH 514
  1079. /*
  1080. * Returns the maximum allowed length for the current message that we are
  1081. * reading. Excludes the message header.
  1082. */
  1083. size_t ossl_statem_server_max_message_size(SSL_CONNECTION *s)
  1084. {
  1085. OSSL_STATEM *st = &s->statem;
  1086. switch (st->hand_state) {
  1087. default:
  1088. /* Shouldn't happen */
  1089. return 0;
  1090. case TLS_ST_SR_CLNT_HELLO:
  1091. return CLIENT_HELLO_MAX_LENGTH;
  1092. case TLS_ST_SR_END_OF_EARLY_DATA:
  1093. return END_OF_EARLY_DATA_MAX_LENGTH;
  1094. case TLS_ST_SR_COMP_CERT:
  1095. case TLS_ST_SR_CERT:
  1096. return s->max_cert_list;
  1097. case TLS_ST_SR_KEY_EXCH:
  1098. return CLIENT_KEY_EXCH_MAX_LENGTH;
  1099. case TLS_ST_SR_CERT_VRFY:
  1100. return CERTIFICATE_VERIFY_MAX_LENGTH;
  1101. #ifndef OPENSSL_NO_NEXTPROTONEG
  1102. case TLS_ST_SR_NEXT_PROTO:
  1103. return NEXT_PROTO_MAX_LENGTH;
  1104. #endif
  1105. case TLS_ST_SR_CHANGE:
  1106. return CCS_MAX_LENGTH;
  1107. case TLS_ST_SR_FINISHED:
  1108. return FINISHED_MAX_LENGTH;
  1109. case TLS_ST_SR_KEY_UPDATE:
  1110. return KEY_UPDATE_MAX_LENGTH;
  1111. }
  1112. }
  1113. /*
  1114. * Process a message that the server has received from the client.
  1115. */
  1116. MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL_CONNECTION *s,
  1117. PACKET *pkt)
  1118. {
  1119. OSSL_STATEM *st = &s->statem;
  1120. switch (st->hand_state) {
  1121. default:
  1122. /* Shouldn't happen */
  1123. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1124. return MSG_PROCESS_ERROR;
  1125. case TLS_ST_SR_CLNT_HELLO:
  1126. return tls_process_client_hello(s, pkt);
  1127. case TLS_ST_SR_END_OF_EARLY_DATA:
  1128. return tls_process_end_of_early_data(s, pkt);
  1129. case TLS_ST_SR_CERT:
  1130. return tls_process_client_certificate(s, pkt);
  1131. #ifndef OPENSSL_NO_COMP_ALG
  1132. case TLS_ST_SR_COMP_CERT:
  1133. return tls_process_client_compressed_certificate(s, pkt);
  1134. #endif
  1135. case TLS_ST_SR_KEY_EXCH:
  1136. return tls_process_client_key_exchange(s, pkt);
  1137. case TLS_ST_SR_CERT_VRFY:
  1138. return tls_process_cert_verify(s, pkt);
  1139. #ifndef OPENSSL_NO_NEXTPROTONEG
  1140. case TLS_ST_SR_NEXT_PROTO:
  1141. return tls_process_next_proto(s, pkt);
  1142. #endif
  1143. case TLS_ST_SR_CHANGE:
  1144. return tls_process_change_cipher_spec(s, pkt);
  1145. case TLS_ST_SR_FINISHED:
  1146. return tls_process_finished(s, pkt);
  1147. case TLS_ST_SR_KEY_UPDATE:
  1148. return tls_process_key_update(s, pkt);
  1149. }
  1150. }
  1151. /*
  1152. * Perform any further processing required following the receipt of a message
  1153. * from the client
  1154. */
  1155. WORK_STATE ossl_statem_server_post_process_message(SSL_CONNECTION *s,
  1156. WORK_STATE wst)
  1157. {
  1158. OSSL_STATEM *st = &s->statem;
  1159. switch (st->hand_state) {
  1160. default:
  1161. /* Shouldn't happen */
  1162. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1163. return WORK_ERROR;
  1164. case TLS_ST_SR_CLNT_HELLO:
  1165. return tls_post_process_client_hello(s, wst);
  1166. case TLS_ST_SR_KEY_EXCH:
  1167. return tls_post_process_client_key_exchange(s, wst);
  1168. }
  1169. }
  1170. #ifndef OPENSSL_NO_SRP
  1171. /* Returns 1 on success, 0 for retryable error, -1 for fatal error */
  1172. static int ssl_check_srp_ext_ClientHello(SSL_CONNECTION *s)
  1173. {
  1174. int ret;
  1175. int al = SSL_AD_UNRECOGNIZED_NAME;
  1176. if ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
  1177. (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
  1178. if (s->srp_ctx.login == NULL) {
  1179. /*
  1180. * RFC 5054 says SHOULD reject, we do so if There is no srp
  1181. * login name
  1182. */
  1183. SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY,
  1184. SSL_R_PSK_IDENTITY_NOT_FOUND);
  1185. return -1;
  1186. } else {
  1187. ret = ssl_srp_server_param_with_username_intern(s, &al);
  1188. if (ret < 0)
  1189. return 0;
  1190. if (ret == SSL3_AL_FATAL) {
  1191. SSLfatal(s, al,
  1192. al == SSL_AD_UNKNOWN_PSK_IDENTITY
  1193. ? SSL_R_PSK_IDENTITY_NOT_FOUND
  1194. : SSL_R_CLIENTHELLO_TLSEXT);
  1195. return -1;
  1196. }
  1197. }
  1198. }
  1199. return 1;
  1200. }
  1201. #endif
  1202. int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie,
  1203. size_t cookie_len)
  1204. {
  1205. /* Always use DTLS 1.0 version: see RFC 6347 */
  1206. if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION)
  1207. || !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len))
  1208. return 0;
  1209. return 1;
  1210. }
  1211. CON_FUNC_RETURN dtls_construct_hello_verify_request(SSL_CONNECTION *s,
  1212. WPACKET *pkt)
  1213. {
  1214. unsigned int cookie_leni;
  1215. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  1216. if (sctx->app_gen_cookie_cb == NULL
  1217. || sctx->app_gen_cookie_cb(SSL_CONNECTION_GET_SSL(s), s->d1->cookie,
  1218. &cookie_leni) == 0
  1219. || cookie_leni > DTLS1_COOKIE_LENGTH) {
  1220. SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
  1221. return CON_FUNC_ERROR;
  1222. }
  1223. s->d1->cookie_len = cookie_leni;
  1224. if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie,
  1225. s->d1->cookie_len)) {
  1226. SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR);
  1227. return CON_FUNC_ERROR;
  1228. }
  1229. return CON_FUNC_SUCCESS;
  1230. }
  1231. /*-
  1232. * ssl_check_for_safari attempts to fingerprint Safari using OS X
  1233. * SecureTransport using the TLS extension block in |hello|.
  1234. * Safari, since 10.6, sends exactly these extensions, in this order:
  1235. * SNI,
  1236. * elliptic_curves
  1237. * ec_point_formats
  1238. * signature_algorithms (for TLSv1.2 only)
  1239. *
  1240. * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
  1241. * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
  1242. * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
  1243. * 10.8..10.8.3 (which don't work).
  1244. */
  1245. static void ssl_check_for_safari(SSL_CONNECTION *s,
  1246. const CLIENTHELLO_MSG *hello)
  1247. {
  1248. static const unsigned char kSafariExtensionsBlock[] = {
  1249. 0x00, 0x0a, /* elliptic_curves extension */
  1250. 0x00, 0x08, /* 8 bytes */
  1251. 0x00, 0x06, /* 6 bytes of curve ids */
  1252. 0x00, 0x17, /* P-256 */
  1253. 0x00, 0x18, /* P-384 */
  1254. 0x00, 0x19, /* P-521 */
  1255. 0x00, 0x0b, /* ec_point_formats */
  1256. 0x00, 0x02, /* 2 bytes */
  1257. 0x01, /* 1 point format */
  1258. 0x00, /* uncompressed */
  1259. /* The following is only present in TLS 1.2 */
  1260. 0x00, 0x0d, /* signature_algorithms */
  1261. 0x00, 0x0c, /* 12 bytes */
  1262. 0x00, 0x0a, /* 10 bytes */
  1263. 0x05, 0x01, /* SHA-384/RSA */
  1264. 0x04, 0x01, /* SHA-256/RSA */
  1265. 0x02, 0x01, /* SHA-1/RSA */
  1266. 0x04, 0x03, /* SHA-256/ECDSA */
  1267. 0x02, 0x03, /* SHA-1/ECDSA */
  1268. };
  1269. /* Length of the common prefix (first two extensions). */
  1270. static const size_t kSafariCommonExtensionsLength = 18;
  1271. unsigned int type;
  1272. PACKET sni, tmppkt;
  1273. size_t ext_len;
  1274. tmppkt = hello->extensions;
  1275. if (!PACKET_forward(&tmppkt, 2)
  1276. || !PACKET_get_net_2(&tmppkt, &type)
  1277. || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
  1278. return;
  1279. }
  1280. if (type != TLSEXT_TYPE_server_name)
  1281. return;
  1282. ext_len = TLS1_get_client_version(
  1283. SSL_CONNECTION_GET_SSL(s)) >= TLS1_2_VERSION ?
  1284. sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
  1285. s->s3.is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
  1286. ext_len);
  1287. }
  1288. #define RENEG_OPTIONS_OK(options) \
  1289. ((options & SSL_OP_NO_RENEGOTIATION) == 0 \
  1290. && (options & SSL_OP_ALLOW_CLIENT_RENEGOTIATION) != 0)
  1291. MSG_PROCESS_RETURN tls_process_client_hello(SSL_CONNECTION *s, PACKET *pkt)
  1292. {
  1293. /* |cookie| will only be initialized for DTLS. */
  1294. PACKET session_id, compression, extensions, cookie;
  1295. static const unsigned char null_compression = 0;
  1296. CLIENTHELLO_MSG *clienthello = NULL;
  1297. /* Check if this is actually an unexpected renegotiation ClientHello */
  1298. if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
  1299. if (!ossl_assert(!SSL_CONNECTION_IS_TLS13(s))) {
  1300. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1301. goto err;
  1302. }
  1303. if (!RENEG_OPTIONS_OK(s->options)
  1304. || (!s->s3.send_connection_binding
  1305. && (s->options
  1306. & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION) == 0)) {
  1307. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
  1308. return MSG_PROCESS_FINISHED_READING;
  1309. }
  1310. s->renegotiate = 1;
  1311. s->new_session = 1;
  1312. }
  1313. clienthello = OPENSSL_zalloc(sizeof(*clienthello));
  1314. if (clienthello == NULL) {
  1315. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1316. goto err;
  1317. }
  1318. /*
  1319. * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
  1320. */
  1321. clienthello->isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer);
  1322. PACKET_null_init(&cookie);
  1323. if (clienthello->isv2) {
  1324. unsigned int mt;
  1325. if (!SSL_IS_FIRST_HANDSHAKE(s)
  1326. || s->hello_retry_request != SSL_HRR_NONE) {
  1327. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
  1328. goto err;
  1329. }
  1330. /*-
  1331. * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
  1332. * header is sent directly on the wire, not wrapped as a TLS
  1333. * record. Our record layer just processes the message length and passes
  1334. * the rest right through. Its format is:
  1335. * Byte Content
  1336. * 0-1 msg_length - decoded by the record layer
  1337. * 2 msg_type - s->init_msg points here
  1338. * 3-4 version
  1339. * 5-6 cipher_spec_length
  1340. * 7-8 session_id_length
  1341. * 9-10 challenge_length
  1342. * ... ...
  1343. */
  1344. if (!PACKET_get_1(pkt, &mt)
  1345. || mt != SSL2_MT_CLIENT_HELLO) {
  1346. /*
  1347. * Should never happen. We should have tested this in the record
  1348. * layer in order to have determined that this is a SSLv2 record
  1349. * in the first place
  1350. */
  1351. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1352. goto err;
  1353. }
  1354. }
  1355. if (!PACKET_get_net_2(pkt, &clienthello->legacy_version)) {
  1356. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
  1357. goto err;
  1358. }
  1359. /* Parse the message and load client random. */
  1360. if (clienthello->isv2) {
  1361. /*
  1362. * Handle an SSLv2 backwards compatible ClientHello
  1363. * Note, this is only for SSLv3+ using the backward compatible format.
  1364. * Real SSLv2 is not supported, and is rejected below.
  1365. */
  1366. unsigned int ciphersuite_len, session_id_len, challenge_len;
  1367. PACKET challenge;
  1368. if (!PACKET_get_net_2(pkt, &ciphersuite_len)
  1369. || !PACKET_get_net_2(pkt, &session_id_len)
  1370. || !PACKET_get_net_2(pkt, &challenge_len)) {
  1371. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RECORD_LENGTH_MISMATCH);
  1372. goto err;
  1373. }
  1374. if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  1375. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_LENGTH_MISMATCH);
  1376. goto err;
  1377. }
  1378. if (!PACKET_get_sub_packet(pkt, &clienthello->ciphersuites,
  1379. ciphersuite_len)
  1380. || !PACKET_copy_bytes(pkt, clienthello->session_id, session_id_len)
  1381. || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
  1382. /* No extensions. */
  1383. || PACKET_remaining(pkt) != 0) {
  1384. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RECORD_LENGTH_MISMATCH);
  1385. goto err;
  1386. }
  1387. clienthello->session_id_len = session_id_len;
  1388. /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
  1389. * here rather than sizeof(clienthello->random) because that is the limit
  1390. * for SSLv3 and it is fixed. It won't change even if
  1391. * sizeof(clienthello->random) does.
  1392. */
  1393. challenge_len = challenge_len > SSL3_RANDOM_SIZE
  1394. ? SSL3_RANDOM_SIZE : challenge_len;
  1395. memset(clienthello->random, 0, SSL3_RANDOM_SIZE);
  1396. if (!PACKET_copy_bytes(&challenge,
  1397. clienthello->random + SSL3_RANDOM_SIZE -
  1398. challenge_len, challenge_len)
  1399. /* Advertise only null compression. */
  1400. || !PACKET_buf_init(&compression, &null_compression, 1)) {
  1401. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1402. goto err;
  1403. }
  1404. PACKET_null_init(&clienthello->extensions);
  1405. } else {
  1406. /* Regular ClientHello. */
  1407. if (!PACKET_copy_bytes(pkt, clienthello->random, SSL3_RANDOM_SIZE)
  1408. || !PACKET_get_length_prefixed_1(pkt, &session_id)
  1409. || !PACKET_copy_all(&session_id, clienthello->session_id,
  1410. SSL_MAX_SSL_SESSION_ID_LENGTH,
  1411. &clienthello->session_id_len)) {
  1412. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1413. goto err;
  1414. }
  1415. if (SSL_CONNECTION_IS_DTLS(s)) {
  1416. if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
  1417. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1418. goto err;
  1419. }
  1420. if (!PACKET_copy_all(&cookie, clienthello->dtls_cookie,
  1421. DTLS1_COOKIE_LENGTH,
  1422. &clienthello->dtls_cookie_len)) {
  1423. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1424. goto err;
  1425. }
  1426. /*
  1427. * If we require cookies and this ClientHello doesn't contain one,
  1428. * just return since we do not want to allocate any memory yet.
  1429. * So check cookie length...
  1430. */
  1431. if (SSL_get_options(SSL_CONNECTION_GET_SSL(s)) & SSL_OP_COOKIE_EXCHANGE) {
  1432. if (clienthello->dtls_cookie_len == 0) {
  1433. OPENSSL_free(clienthello);
  1434. return MSG_PROCESS_FINISHED_READING;
  1435. }
  1436. }
  1437. }
  1438. if (!PACKET_get_length_prefixed_2(pkt, &clienthello->ciphersuites)) {
  1439. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1440. goto err;
  1441. }
  1442. if (!PACKET_get_length_prefixed_1(pkt, &compression)) {
  1443. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1444. goto err;
  1445. }
  1446. /* Could be empty. */
  1447. if (PACKET_remaining(pkt) == 0) {
  1448. PACKET_null_init(&clienthello->extensions);
  1449. } else {
  1450. if (!PACKET_get_length_prefixed_2(pkt, &clienthello->extensions)
  1451. || PACKET_remaining(pkt) != 0) {
  1452. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1453. goto err;
  1454. }
  1455. }
  1456. }
  1457. if (!PACKET_copy_all(&compression, clienthello->compressions,
  1458. MAX_COMPRESSIONS_SIZE,
  1459. &clienthello->compressions_len)) {
  1460. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1461. goto err;
  1462. }
  1463. /* Preserve the raw extensions PACKET for later use */
  1464. extensions = clienthello->extensions;
  1465. if (!tls_collect_extensions(s, &extensions, SSL_EXT_CLIENT_HELLO,
  1466. &clienthello->pre_proc_exts,
  1467. &clienthello->pre_proc_exts_len, 1)) {
  1468. /* SSLfatal already been called */
  1469. goto err;
  1470. }
  1471. s->clienthello = clienthello;
  1472. return MSG_PROCESS_CONTINUE_PROCESSING;
  1473. err:
  1474. if (clienthello != NULL)
  1475. OPENSSL_free(clienthello->pre_proc_exts);
  1476. OPENSSL_free(clienthello);
  1477. return MSG_PROCESS_ERROR;
  1478. }
  1479. static int tls_early_post_process_client_hello(SSL_CONNECTION *s)
  1480. {
  1481. unsigned int j;
  1482. int i, al = SSL_AD_INTERNAL_ERROR;
  1483. int protverr;
  1484. size_t loop;
  1485. unsigned long id;
  1486. #ifndef OPENSSL_NO_COMP
  1487. SSL_COMP *comp = NULL;
  1488. #endif
  1489. const SSL_CIPHER *c;
  1490. STACK_OF(SSL_CIPHER) *ciphers = NULL;
  1491. STACK_OF(SSL_CIPHER) *scsvs = NULL;
  1492. CLIENTHELLO_MSG *clienthello = s->clienthello;
  1493. DOWNGRADE dgrd = DOWNGRADE_NONE;
  1494. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  1495. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  1496. /* Finished parsing the ClientHello, now we can start processing it */
  1497. /* Give the ClientHello callback a crack at things */
  1498. if (sctx->client_hello_cb != NULL) {
  1499. /* A failure in the ClientHello callback terminates the connection. */
  1500. switch (sctx->client_hello_cb(ssl, &al, sctx->client_hello_cb_arg)) {
  1501. case SSL_CLIENT_HELLO_SUCCESS:
  1502. break;
  1503. case SSL_CLIENT_HELLO_RETRY:
  1504. s->rwstate = SSL_CLIENT_HELLO_CB;
  1505. return -1;
  1506. case SSL_CLIENT_HELLO_ERROR:
  1507. default:
  1508. SSLfatal(s, al, SSL_R_CALLBACK_FAILED);
  1509. goto err;
  1510. }
  1511. }
  1512. /* Set up the client_random */
  1513. memcpy(s->s3.client_random, clienthello->random, SSL3_RANDOM_SIZE);
  1514. /* Choose the version */
  1515. if (clienthello->isv2) {
  1516. if (clienthello->legacy_version == SSL2_VERSION
  1517. || (clienthello->legacy_version & 0xff00)
  1518. != (SSL3_VERSION_MAJOR << 8)) {
  1519. /*
  1520. * This is real SSLv2 or something completely unknown. We don't
  1521. * support it.
  1522. */
  1523. SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_UNKNOWN_PROTOCOL);
  1524. goto err;
  1525. }
  1526. /* SSLv3/TLS */
  1527. s->client_version = clienthello->legacy_version;
  1528. }
  1529. /* Choose the server SSL/TLS/DTLS version. */
  1530. protverr = ssl_choose_server_version(s, clienthello, &dgrd);
  1531. if (protverr) {
  1532. if (SSL_IS_FIRST_HANDSHAKE(s)) {
  1533. /* like ssl3_get_record, send alert using remote version number */
  1534. s->version = s->client_version = clienthello->legacy_version;
  1535. }
  1536. SSLfatal(s, SSL_AD_PROTOCOL_VERSION, protverr);
  1537. goto err;
  1538. }
  1539. /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
  1540. if (SSL_CONNECTION_IS_TLS13(s)
  1541. && RECORD_LAYER_processed_read_pending(&s->rlayer)) {
  1542. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY);
  1543. goto err;
  1544. }
  1545. if (SSL_CONNECTION_IS_DTLS(s)) {
  1546. /* Empty cookie was already handled above by returning early. */
  1547. if (SSL_get_options(ssl) & SSL_OP_COOKIE_EXCHANGE) {
  1548. if (sctx->app_verify_cookie_cb != NULL) {
  1549. if (sctx->app_verify_cookie_cb(ssl, clienthello->dtls_cookie,
  1550. clienthello->dtls_cookie_len) == 0) {
  1551. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1552. SSL_R_COOKIE_MISMATCH);
  1553. goto err;
  1554. /* else cookie verification succeeded */
  1555. }
  1556. /* default verification */
  1557. } else if (s->d1->cookie_len != clienthello->dtls_cookie_len
  1558. || memcmp(clienthello->dtls_cookie, s->d1->cookie,
  1559. s->d1->cookie_len) != 0) {
  1560. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_COOKIE_MISMATCH);
  1561. goto err;
  1562. }
  1563. s->d1->cookie_verified = 1;
  1564. }
  1565. }
  1566. s->hit = 0;
  1567. if (!ssl_cache_cipherlist(s, &clienthello->ciphersuites,
  1568. clienthello->isv2) ||
  1569. !ossl_bytes_to_cipher_list(s, &clienthello->ciphersuites, &ciphers,
  1570. &scsvs, clienthello->isv2, 1)) {
  1571. /* SSLfatal() already called */
  1572. goto err;
  1573. }
  1574. s->s3.send_connection_binding = 0;
  1575. /* Check what signalling cipher-suite values were received. */
  1576. if (scsvs != NULL) {
  1577. for (i = 0; i < sk_SSL_CIPHER_num(scsvs); i++) {
  1578. c = sk_SSL_CIPHER_value(scsvs, i);
  1579. if (SSL_CIPHER_get_id(c) == SSL3_CK_SCSV) {
  1580. if (s->renegotiate) {
  1581. /* SCSV is fatal if renegotiating */
  1582. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1583. SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
  1584. goto err;
  1585. }
  1586. s->s3.send_connection_binding = 1;
  1587. } else if (SSL_CIPHER_get_id(c) == SSL3_CK_FALLBACK_SCSV &&
  1588. !ssl_check_version_downgrade(s)) {
  1589. /*
  1590. * This SCSV indicates that the client previously tried
  1591. * a higher version. We should fail if the current version
  1592. * is an unexpected downgrade, as that indicates that the first
  1593. * connection may have been tampered with in order to trigger
  1594. * an insecure downgrade.
  1595. */
  1596. SSLfatal(s, SSL_AD_INAPPROPRIATE_FALLBACK,
  1597. SSL_R_INAPPROPRIATE_FALLBACK);
  1598. goto err;
  1599. }
  1600. }
  1601. }
  1602. /* For TLSv1.3 we must select the ciphersuite *before* session resumption */
  1603. if (SSL_CONNECTION_IS_TLS13(s)) {
  1604. const SSL_CIPHER *cipher =
  1605. ssl3_choose_cipher(s, ciphers, SSL_get_ciphers(ssl));
  1606. if (cipher == NULL) {
  1607. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_SHARED_CIPHER);
  1608. goto err;
  1609. }
  1610. if (s->hello_retry_request == SSL_HRR_PENDING
  1611. && (s->s3.tmp.new_cipher == NULL
  1612. || s->s3.tmp.new_cipher->id != cipher->id)) {
  1613. /*
  1614. * A previous HRR picked a different ciphersuite to the one we
  1615. * just selected. Something must have changed.
  1616. */
  1617. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_CIPHER);
  1618. goto err;
  1619. }
  1620. s->s3.tmp.new_cipher = cipher;
  1621. }
  1622. /* We need to do this before getting the session */
  1623. if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret,
  1624. SSL_EXT_CLIENT_HELLO,
  1625. clienthello->pre_proc_exts, NULL, 0)) {
  1626. /* SSLfatal() already called */
  1627. goto err;
  1628. }
  1629. /*
  1630. * We don't allow resumption in a backwards compatible ClientHello.
  1631. * In TLS1.1+, session_id MUST be empty.
  1632. *
  1633. * Versions before 0.9.7 always allow clients to resume sessions in
  1634. * renegotiation. 0.9.7 and later allow this by default, but optionally
  1635. * ignore resumption requests with flag
  1636. * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
  1637. * than a change to default behavior so that applications relying on
  1638. * this for security won't even compile against older library versions).
  1639. * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
  1640. * request renegotiation but not a new session (s->new_session remains
  1641. * unset): for servers, this essentially just means that the
  1642. * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
  1643. * ignored.
  1644. */
  1645. if (clienthello->isv2 ||
  1646. (s->new_session &&
  1647. (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
  1648. if (!ssl_get_new_session(s, 1)) {
  1649. /* SSLfatal() already called */
  1650. goto err;
  1651. }
  1652. } else {
  1653. i = ssl_get_prev_session(s, clienthello);
  1654. if (i == 1) {
  1655. /* previous session */
  1656. s->hit = 1;
  1657. } else if (i == -1) {
  1658. /* SSLfatal() already called */
  1659. goto err;
  1660. } else {
  1661. /* i == 0 */
  1662. if (!ssl_get_new_session(s, 1)) {
  1663. /* SSLfatal() already called */
  1664. goto err;
  1665. }
  1666. }
  1667. }
  1668. if (SSL_CONNECTION_IS_TLS13(s)) {
  1669. memcpy(s->tmp_session_id, s->clienthello->session_id,
  1670. s->clienthello->session_id_len);
  1671. s->tmp_session_id_len = s->clienthello->session_id_len;
  1672. }
  1673. /*
  1674. * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
  1675. * ciphersuite compatibility with the session as part of resumption.
  1676. */
  1677. if (!SSL_CONNECTION_IS_TLS13(s) && s->hit) {
  1678. j = 0;
  1679. id = s->session->cipher->id;
  1680. OSSL_TRACE_BEGIN(TLS_CIPHER) {
  1681. BIO_printf(trc_out, "client sent %d ciphers\n",
  1682. sk_SSL_CIPHER_num(ciphers));
  1683. }
  1684. for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
  1685. c = sk_SSL_CIPHER_value(ciphers, i);
  1686. if (trc_out != NULL)
  1687. BIO_printf(trc_out, "client [%2d of %2d]:%s\n", i,
  1688. sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
  1689. if (c->id == id) {
  1690. j = 1;
  1691. break;
  1692. }
  1693. }
  1694. if (j == 0) {
  1695. /*
  1696. * we need to have the cipher in the cipher list if we are asked
  1697. * to reuse it
  1698. */
  1699. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1700. SSL_R_REQUIRED_CIPHER_MISSING);
  1701. OSSL_TRACE_CANCEL(TLS_CIPHER);
  1702. goto err;
  1703. }
  1704. OSSL_TRACE_END(TLS_CIPHER);
  1705. }
  1706. for (loop = 0; loop < clienthello->compressions_len; loop++) {
  1707. if (clienthello->compressions[loop] == 0)
  1708. break;
  1709. }
  1710. if (loop >= clienthello->compressions_len) {
  1711. /* no compress */
  1712. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_NO_COMPRESSION_SPECIFIED);
  1713. goto err;
  1714. }
  1715. if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
  1716. ssl_check_for_safari(s, clienthello);
  1717. /* TLS extensions */
  1718. if (!tls_parse_all_extensions(s, SSL_EXT_CLIENT_HELLO,
  1719. clienthello->pre_proc_exts, NULL, 0, 1)) {
  1720. /* SSLfatal() already called */
  1721. goto err;
  1722. }
  1723. /*
  1724. * Check if we want to use external pre-shared secret for this handshake
  1725. * for not reused session only. We need to generate server_random before
  1726. * calling tls_session_secret_cb in order to allow SessionTicket
  1727. * processing to use it in key derivation.
  1728. */
  1729. {
  1730. unsigned char *pos;
  1731. pos = s->s3.server_random;
  1732. if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE, dgrd) <= 0) {
  1733. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1734. goto err;
  1735. }
  1736. }
  1737. if (!s->hit && !tls1_set_server_sigalgs(s)) {
  1738. /* SSLfatal() already called */
  1739. goto err;
  1740. }
  1741. if (!s->hit
  1742. && s->version >= TLS1_VERSION
  1743. && !SSL_CONNECTION_IS_TLS13(s)
  1744. && !SSL_CONNECTION_IS_DTLS(s)
  1745. && s->ext.session_secret_cb != NULL) {
  1746. const SSL_CIPHER *pref_cipher = NULL;
  1747. /*
  1748. * s->session->master_key_length is a size_t, but this is an int for
  1749. * backwards compat reasons
  1750. */
  1751. int master_key_length;
  1752. master_key_length = sizeof(s->session->master_key);
  1753. if (s->ext.session_secret_cb(ssl, s->session->master_key,
  1754. &master_key_length, ciphers,
  1755. &pref_cipher,
  1756. s->ext.session_secret_cb_arg)
  1757. && master_key_length > 0) {
  1758. s->session->master_key_length = master_key_length;
  1759. s->hit = 1;
  1760. s->peer_ciphers = ciphers;
  1761. s->session->verify_result = X509_V_OK;
  1762. ciphers = NULL;
  1763. /* check if some cipher was preferred by call back */
  1764. if (pref_cipher == NULL)
  1765. pref_cipher = ssl3_choose_cipher(s, s->peer_ciphers,
  1766. SSL_get_ciphers(ssl));
  1767. if (pref_cipher == NULL) {
  1768. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_SHARED_CIPHER);
  1769. goto err;
  1770. }
  1771. s->session->cipher = pref_cipher;
  1772. sk_SSL_CIPHER_free(s->cipher_list);
  1773. s->cipher_list = sk_SSL_CIPHER_dup(s->peer_ciphers);
  1774. sk_SSL_CIPHER_free(s->cipher_list_by_id);
  1775. s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->peer_ciphers);
  1776. }
  1777. }
  1778. /*
  1779. * Worst case, we will use the NULL compression, but if we have other
  1780. * options, we will now look for them. We have complen-1 compression
  1781. * algorithms from the client, starting at q.
  1782. */
  1783. s->s3.tmp.new_compression = NULL;
  1784. if (SSL_CONNECTION_IS_TLS13(s)) {
  1785. /*
  1786. * We already checked above that the NULL compression method appears in
  1787. * the list. Now we check there aren't any others (which is illegal in
  1788. * a TLSv1.3 ClientHello.
  1789. */
  1790. if (clienthello->compressions_len != 1) {
  1791. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1792. SSL_R_INVALID_COMPRESSION_ALGORITHM);
  1793. goto err;
  1794. }
  1795. }
  1796. #ifndef OPENSSL_NO_COMP
  1797. /* This only happens if we have a cache hit */
  1798. else if (s->session->compress_meth != 0) {
  1799. int m, comp_id = s->session->compress_meth;
  1800. unsigned int k;
  1801. /* Perform sanity checks on resumed compression algorithm */
  1802. /* Can't disable compression */
  1803. if (!ssl_allow_compression(s)) {
  1804. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1805. SSL_R_INCONSISTENT_COMPRESSION);
  1806. goto err;
  1807. }
  1808. /* Look for resumed compression method */
  1809. for (m = 0; m < sk_SSL_COMP_num(sctx->comp_methods); m++) {
  1810. comp = sk_SSL_COMP_value(sctx->comp_methods, m);
  1811. if (comp_id == comp->id) {
  1812. s->s3.tmp.new_compression = comp;
  1813. break;
  1814. }
  1815. }
  1816. if (s->s3.tmp.new_compression == NULL) {
  1817. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1818. SSL_R_INVALID_COMPRESSION_ALGORITHM);
  1819. goto err;
  1820. }
  1821. /* Look for resumed method in compression list */
  1822. for (k = 0; k < clienthello->compressions_len; k++) {
  1823. if (clienthello->compressions[k] == comp_id)
  1824. break;
  1825. }
  1826. if (k >= clienthello->compressions_len) {
  1827. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1828. SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
  1829. goto err;
  1830. }
  1831. } else if (s->hit) {
  1832. comp = NULL;
  1833. } else if (ssl_allow_compression(s) && sctx->comp_methods) {
  1834. /* See if we have a match */
  1835. int m, nn, v, done = 0;
  1836. unsigned int o;
  1837. nn = sk_SSL_COMP_num(sctx->comp_methods);
  1838. for (m = 0; m < nn; m++) {
  1839. comp = sk_SSL_COMP_value(sctx->comp_methods, m);
  1840. v = comp->id;
  1841. for (o = 0; o < clienthello->compressions_len; o++) {
  1842. if (v == clienthello->compressions[o]) {
  1843. done = 1;
  1844. break;
  1845. }
  1846. }
  1847. if (done)
  1848. break;
  1849. }
  1850. if (done)
  1851. s->s3.tmp.new_compression = comp;
  1852. else
  1853. comp = NULL;
  1854. }
  1855. #else
  1856. /*
  1857. * If compression is disabled we'd better not try to resume a session
  1858. * using compression.
  1859. */
  1860. if (s->session->compress_meth != 0) {
  1861. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_COMPRESSION);
  1862. goto err;
  1863. }
  1864. #endif
  1865. /*
  1866. * Given s->peer_ciphers and SSL_get_ciphers, we must pick a cipher
  1867. */
  1868. if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) {
  1869. sk_SSL_CIPHER_free(s->peer_ciphers);
  1870. s->peer_ciphers = ciphers;
  1871. if (ciphers == NULL) {
  1872. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1873. goto err;
  1874. }
  1875. ciphers = NULL;
  1876. }
  1877. if (!s->hit) {
  1878. #ifdef OPENSSL_NO_COMP
  1879. s->session->compress_meth = 0;
  1880. #else
  1881. s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
  1882. #endif
  1883. }
  1884. sk_SSL_CIPHER_free(ciphers);
  1885. sk_SSL_CIPHER_free(scsvs);
  1886. OPENSSL_free(clienthello->pre_proc_exts);
  1887. OPENSSL_free(s->clienthello);
  1888. s->clienthello = NULL;
  1889. return 1;
  1890. err:
  1891. sk_SSL_CIPHER_free(ciphers);
  1892. sk_SSL_CIPHER_free(scsvs);
  1893. OPENSSL_free(clienthello->pre_proc_exts);
  1894. OPENSSL_free(s->clienthello);
  1895. s->clienthello = NULL;
  1896. return 0;
  1897. }
  1898. /*
  1899. * Call the status request callback if needed. Upon success, returns 1.
  1900. * Upon failure, returns 0.
  1901. */
  1902. static int tls_handle_status_request(SSL_CONNECTION *s)
  1903. {
  1904. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  1905. s->ext.status_expected = 0;
  1906. /*
  1907. * If status request then ask callback what to do. Note: this must be
  1908. * called after servername callbacks in case the certificate has changed,
  1909. * and must be called after the cipher has been chosen because this may
  1910. * influence which certificate is sent
  1911. */
  1912. if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing && sctx != NULL
  1913. && sctx->ext.status_cb != NULL) {
  1914. int ret;
  1915. /* If no certificate can't return certificate status */
  1916. if (s->s3.tmp.cert != NULL) {
  1917. /*
  1918. * Set current certificate to one we will use so SSL_get_certificate
  1919. * et al can pick it up.
  1920. */
  1921. s->cert->key = s->s3.tmp.cert;
  1922. ret = sctx->ext.status_cb(SSL_CONNECTION_GET_SSL(s),
  1923. sctx->ext.status_arg);
  1924. switch (ret) {
  1925. /* We don't want to send a status request response */
  1926. case SSL_TLSEXT_ERR_NOACK:
  1927. s->ext.status_expected = 0;
  1928. break;
  1929. /* status request response should be sent */
  1930. case SSL_TLSEXT_ERR_OK:
  1931. if (s->ext.ocsp.resp)
  1932. s->ext.status_expected = 1;
  1933. break;
  1934. /* something bad happened */
  1935. case SSL_TLSEXT_ERR_ALERT_FATAL:
  1936. default:
  1937. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CLIENTHELLO_TLSEXT);
  1938. return 0;
  1939. }
  1940. }
  1941. }
  1942. return 1;
  1943. }
  1944. /*
  1945. * Call the alpn_select callback if needed. Upon success, returns 1.
  1946. * Upon failure, returns 0.
  1947. */
  1948. int tls_handle_alpn(SSL_CONNECTION *s)
  1949. {
  1950. const unsigned char *selected = NULL;
  1951. unsigned char selected_len = 0;
  1952. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  1953. if (sctx->ext.alpn_select_cb != NULL && s->s3.alpn_proposed != NULL) {
  1954. int r = sctx->ext.alpn_select_cb(SSL_CONNECTION_GET_SSL(s),
  1955. &selected, &selected_len,
  1956. s->s3.alpn_proposed,
  1957. (unsigned int)s->s3.alpn_proposed_len,
  1958. sctx->ext.alpn_select_cb_arg);
  1959. if (r == SSL_TLSEXT_ERR_OK) {
  1960. OPENSSL_free(s->s3.alpn_selected);
  1961. s->s3.alpn_selected = OPENSSL_memdup(selected, selected_len);
  1962. if (s->s3.alpn_selected == NULL) {
  1963. s->s3.alpn_selected_len = 0;
  1964. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1965. return 0;
  1966. }
  1967. s->s3.alpn_selected_len = selected_len;
  1968. #ifndef OPENSSL_NO_NEXTPROTONEG
  1969. /* ALPN takes precedence over NPN. */
  1970. s->s3.npn_seen = 0;
  1971. #endif
  1972. /* Check ALPN is consistent with session */
  1973. if (s->session->ext.alpn_selected == NULL
  1974. || selected_len != s->session->ext.alpn_selected_len
  1975. || memcmp(selected, s->session->ext.alpn_selected,
  1976. selected_len) != 0) {
  1977. /* Not consistent so can't be used for early_data */
  1978. s->ext.early_data_ok = 0;
  1979. if (!s->hit) {
  1980. /*
  1981. * This is a new session and so alpn_selected should have
  1982. * been initialised to NULL. We should update it with the
  1983. * selected ALPN.
  1984. */
  1985. if (!ossl_assert(s->session->ext.alpn_selected == NULL)) {
  1986. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1987. ERR_R_INTERNAL_ERROR);
  1988. return 0;
  1989. }
  1990. s->session->ext.alpn_selected = OPENSSL_memdup(selected,
  1991. selected_len);
  1992. if (s->session->ext.alpn_selected == NULL) {
  1993. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1994. ERR_R_INTERNAL_ERROR);
  1995. return 0;
  1996. }
  1997. s->session->ext.alpn_selected_len = selected_len;
  1998. }
  1999. }
  2000. return 1;
  2001. } else if (r != SSL_TLSEXT_ERR_NOACK) {
  2002. SSLfatal(s, SSL_AD_NO_APPLICATION_PROTOCOL,
  2003. SSL_R_NO_APPLICATION_PROTOCOL);
  2004. return 0;
  2005. }
  2006. /*
  2007. * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was
  2008. * present.
  2009. */
  2010. }
  2011. /* Check ALPN is consistent with session */
  2012. if (s->session->ext.alpn_selected != NULL) {
  2013. /* Not consistent so can't be used for early_data */
  2014. s->ext.early_data_ok = 0;
  2015. }
  2016. return 1;
  2017. }
  2018. WORK_STATE tls_post_process_client_hello(SSL_CONNECTION *s, WORK_STATE wst)
  2019. {
  2020. const SSL_CIPHER *cipher;
  2021. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  2022. if (wst == WORK_MORE_A) {
  2023. int rv = tls_early_post_process_client_hello(s);
  2024. if (rv == 0) {
  2025. /* SSLfatal() was already called */
  2026. goto err;
  2027. }
  2028. if (rv < 0)
  2029. return WORK_MORE_A;
  2030. wst = WORK_MORE_B;
  2031. }
  2032. if (wst == WORK_MORE_B) {
  2033. if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) {
  2034. /* Let cert callback update server certificates if required */
  2035. if (!s->hit && s->cert->cert_cb != NULL) {
  2036. int rv = s->cert->cert_cb(ssl, s->cert->cert_cb_arg);
  2037. if (rv == 0) {
  2038. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CERT_CB_ERROR);
  2039. goto err;
  2040. }
  2041. if (rv < 0) {
  2042. s->rwstate = SSL_X509_LOOKUP;
  2043. return WORK_MORE_B;
  2044. }
  2045. s->rwstate = SSL_NOTHING;
  2046. }
  2047. /* In TLSv1.3 we selected the ciphersuite before resumption */
  2048. if (!SSL_CONNECTION_IS_TLS13(s)) {
  2049. cipher =
  2050. ssl3_choose_cipher(s, s->peer_ciphers,
  2051. SSL_get_ciphers(ssl));
  2052. if (cipher == NULL) {
  2053. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  2054. SSL_R_NO_SHARED_CIPHER);
  2055. goto err;
  2056. }
  2057. s->s3.tmp.new_cipher = cipher;
  2058. }
  2059. if (!s->hit) {
  2060. if (!tls_choose_sigalg(s, 1)) {
  2061. /* SSLfatal already called */
  2062. goto err;
  2063. }
  2064. /* check whether we should disable session resumption */
  2065. if (s->not_resumable_session_cb != NULL)
  2066. s->session->not_resumable =
  2067. s->not_resumable_session_cb(ssl,
  2068. ((s->s3.tmp.new_cipher->algorithm_mkey
  2069. & (SSL_kDHE | SSL_kECDHE)) != 0));
  2070. if (s->session->not_resumable)
  2071. /* do not send a session ticket */
  2072. s->ext.ticket_expected = 0;
  2073. }
  2074. } else {
  2075. /* Session-id reuse */
  2076. s->s3.tmp.new_cipher = s->session->cipher;
  2077. }
  2078. /*-
  2079. * we now have the following setup.
  2080. * client_random
  2081. * cipher_list - our preferred list of ciphers
  2082. * ciphers - the client's preferred list of ciphers
  2083. * compression - basically ignored right now
  2084. * ssl version is set - sslv3
  2085. * s->session - The ssl session has been setup.
  2086. * s->hit - session reuse flag
  2087. * s->s3.tmp.new_cipher - the new cipher to use.
  2088. */
  2089. /*
  2090. * Call status_request callback if needed. Has to be done after the
  2091. * certificate callbacks etc above.
  2092. */
  2093. if (!tls_handle_status_request(s)) {
  2094. /* SSLfatal() already called */
  2095. goto err;
  2096. }
  2097. /*
  2098. * Call alpn_select callback if needed. Has to be done after SNI and
  2099. * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
  2100. * we already did this because cipher negotiation happens earlier, and
  2101. * we must handle ALPN before we decide whether to accept early_data.
  2102. */
  2103. if (!SSL_CONNECTION_IS_TLS13(s) && !tls_handle_alpn(s)) {
  2104. /* SSLfatal() already called */
  2105. goto err;
  2106. }
  2107. wst = WORK_MORE_C;
  2108. }
  2109. #ifndef OPENSSL_NO_SRP
  2110. if (wst == WORK_MORE_C) {
  2111. int ret;
  2112. if ((ret = ssl_check_srp_ext_ClientHello(s)) == 0) {
  2113. /*
  2114. * callback indicates further work to be done
  2115. */
  2116. s->rwstate = SSL_X509_LOOKUP;
  2117. return WORK_MORE_C;
  2118. }
  2119. if (ret < 0) {
  2120. /* SSLfatal() already called */
  2121. goto err;
  2122. }
  2123. }
  2124. #endif
  2125. return WORK_FINISHED_STOP;
  2126. err:
  2127. return WORK_ERROR;
  2128. }
  2129. CON_FUNC_RETURN tls_construct_server_hello(SSL_CONNECTION *s, WPACKET *pkt)
  2130. {
  2131. int compm;
  2132. size_t sl, len;
  2133. int version;
  2134. unsigned char *session_id;
  2135. int usetls13 = SSL_CONNECTION_IS_TLS13(s)
  2136. || s->hello_retry_request == SSL_HRR_PENDING;
  2137. version = usetls13 ? TLS1_2_VERSION : s->version;
  2138. if (!WPACKET_put_bytes_u16(pkt, version)
  2139. /*
  2140. * Random stuff. Filling of the server_random takes place in
  2141. * tls_process_client_hello()
  2142. */
  2143. || !WPACKET_memcpy(pkt,
  2144. s->hello_retry_request == SSL_HRR_PENDING
  2145. ? hrrrandom : s->s3.server_random,
  2146. SSL3_RANDOM_SIZE)) {
  2147. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2148. return CON_FUNC_ERROR;
  2149. }
  2150. /*-
  2151. * There are several cases for the session ID to send
  2152. * back in the server hello:
  2153. * - For session reuse from the session cache,
  2154. * we send back the old session ID.
  2155. * - If stateless session reuse (using a session ticket)
  2156. * is successful, we send back the client's "session ID"
  2157. * (which doesn't actually identify the session).
  2158. * - If it is a new session, we send back the new
  2159. * session ID.
  2160. * - However, if we want the new session to be single-use,
  2161. * we send back a 0-length session ID.
  2162. * - In TLSv1.3 we echo back the session id sent to us by the client
  2163. * regardless
  2164. * s->hit is non-zero in either case of session reuse,
  2165. * so the following won't overwrite an ID that we're supposed
  2166. * to send back.
  2167. */
  2168. if (!(SSL_CONNECTION_GET_CTX(s)->session_cache_mode & SSL_SESS_CACHE_SERVER)
  2169. && !s->hit)
  2170. s->session->session_id_length = 0;
  2171. if (usetls13) {
  2172. sl = s->tmp_session_id_len;
  2173. session_id = s->tmp_session_id;
  2174. } else {
  2175. sl = s->session->session_id_length;
  2176. session_id = s->session->session_id;
  2177. }
  2178. if (sl > sizeof(s->session->session_id)) {
  2179. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2180. return CON_FUNC_ERROR;
  2181. }
  2182. /* set up the compression method */
  2183. #ifdef OPENSSL_NO_COMP
  2184. compm = 0;
  2185. #else
  2186. if (usetls13 || s->s3.tmp.new_compression == NULL)
  2187. compm = 0;
  2188. else
  2189. compm = s->s3.tmp.new_compression->id;
  2190. #endif
  2191. if (!WPACKET_sub_memcpy_u8(pkt, session_id, sl)
  2192. || !SSL_CONNECTION_GET_SSL(s)->method->put_cipher_by_char(s->s3.tmp.new_cipher,
  2193. pkt, &len)
  2194. || !WPACKET_put_bytes_u8(pkt, compm)) {
  2195. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2196. return CON_FUNC_ERROR;
  2197. }
  2198. if (!tls_construct_extensions(s, pkt,
  2199. s->hello_retry_request == SSL_HRR_PENDING
  2200. ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
  2201. : (SSL_CONNECTION_IS_TLS13(s)
  2202. ? SSL_EXT_TLS1_3_SERVER_HELLO
  2203. : SSL_EXT_TLS1_2_SERVER_HELLO),
  2204. NULL, 0)) {
  2205. /* SSLfatal() already called */
  2206. return CON_FUNC_ERROR;
  2207. }
  2208. if (s->hello_retry_request == SSL_HRR_PENDING) {
  2209. /* Ditch the session. We'll create a new one next time around */
  2210. SSL_SESSION_free(s->session);
  2211. s->session = NULL;
  2212. s->hit = 0;
  2213. /*
  2214. * Re-initialise the Transcript Hash. We're going to prepopulate it with
  2215. * a synthetic message_hash in place of ClientHello1.
  2216. */
  2217. if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) {
  2218. /* SSLfatal() already called */
  2219. return CON_FUNC_ERROR;
  2220. }
  2221. } else if (!(s->verify_mode & SSL_VERIFY_PEER)
  2222. && !ssl3_digest_cached_records(s, 0)) {
  2223. /* SSLfatal() already called */;
  2224. return CON_FUNC_ERROR;
  2225. }
  2226. return CON_FUNC_SUCCESS;
  2227. }
  2228. CON_FUNC_RETURN tls_construct_server_done(SSL_CONNECTION *s, WPACKET *pkt)
  2229. {
  2230. if (!s->s3.tmp.cert_request) {
  2231. if (!ssl3_digest_cached_records(s, 0)) {
  2232. /* SSLfatal() already called */
  2233. return CON_FUNC_ERROR;
  2234. }
  2235. }
  2236. return CON_FUNC_SUCCESS;
  2237. }
  2238. CON_FUNC_RETURN tls_construct_server_key_exchange(SSL_CONNECTION *s,
  2239. WPACKET *pkt)
  2240. {
  2241. EVP_PKEY *pkdh = NULL;
  2242. unsigned char *encodedPoint = NULL;
  2243. size_t encodedlen = 0;
  2244. int curve_id = 0;
  2245. const SIGALG_LOOKUP *lu = s->s3.tmp.sigalg;
  2246. int i;
  2247. unsigned long type;
  2248. BIGNUM *r[4];
  2249. EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
  2250. EVP_PKEY_CTX *pctx = NULL;
  2251. size_t paramlen, paramoffset;
  2252. int freer = 0;
  2253. CON_FUNC_RETURN ret = CON_FUNC_ERROR;
  2254. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  2255. if (!WPACKET_get_total_written(pkt, &paramoffset)) {
  2256. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2257. goto err;
  2258. }
  2259. if (md_ctx == NULL) {
  2260. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  2261. goto err;
  2262. }
  2263. type = s->s3.tmp.new_cipher->algorithm_mkey;
  2264. r[0] = r[1] = r[2] = r[3] = NULL;
  2265. #ifndef OPENSSL_NO_PSK
  2266. /* Plain PSK or RSAPSK nothing to do */
  2267. if (type & (SSL_kPSK | SSL_kRSAPSK)) {
  2268. } else
  2269. #endif /* !OPENSSL_NO_PSK */
  2270. if (type & (SSL_kDHE | SSL_kDHEPSK)) {
  2271. CERT *cert = s->cert;
  2272. EVP_PKEY *pkdhp = NULL;
  2273. if (s->cert->dh_tmp_auto) {
  2274. pkdh = ssl_get_auto_dh(s);
  2275. if (pkdh == NULL) {
  2276. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2277. goto err;
  2278. }
  2279. pkdhp = pkdh;
  2280. } else {
  2281. pkdhp = cert->dh_tmp;
  2282. }
  2283. #if !defined(OPENSSL_NO_DEPRECATED_3_0)
  2284. if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {
  2285. pkdh = ssl_dh_to_pkey(s->cert->dh_tmp_cb(SSL_CONNECTION_GET_SSL(s),
  2286. 0, 1024));
  2287. if (pkdh == NULL) {
  2288. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2289. goto err;
  2290. }
  2291. pkdhp = pkdh;
  2292. }
  2293. #endif
  2294. if (pkdhp == NULL) {
  2295. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY);
  2296. goto err;
  2297. }
  2298. if (!ssl_security(s, SSL_SECOP_TMP_DH,
  2299. EVP_PKEY_get_security_bits(pkdhp), 0, pkdhp)) {
  2300. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DH_KEY_TOO_SMALL);
  2301. goto err;
  2302. }
  2303. if (s->s3.tmp.pkey != NULL) {
  2304. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2305. goto err;
  2306. }
  2307. s->s3.tmp.pkey = ssl_generate_pkey(s, pkdhp);
  2308. if (s->s3.tmp.pkey == NULL) {
  2309. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2310. goto err;
  2311. }
  2312. EVP_PKEY_free(pkdh);
  2313. pkdh = NULL;
  2314. /* These BIGNUMs need to be freed when we're finished */
  2315. freer = 1;
  2316. if (!EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_P,
  2317. &r[0])
  2318. || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_G,
  2319. &r[1])
  2320. || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey,
  2321. OSSL_PKEY_PARAM_PUB_KEY, &r[2])) {
  2322. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2323. goto err;
  2324. }
  2325. } else if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
  2326. if (s->s3.tmp.pkey != NULL) {
  2327. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2328. goto err;
  2329. }
  2330. /* Get NID of appropriate shared curve */
  2331. curve_id = tls1_shared_group(s, -2);
  2332. if (curve_id == 0) {
  2333. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  2334. SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
  2335. goto err;
  2336. }
  2337. /* Cache the group used in the SSL_SESSION */
  2338. s->session->kex_group = curve_id;
  2339. /* Generate a new key for this curve */
  2340. s->s3.tmp.pkey = ssl_generate_pkey_group(s, curve_id);
  2341. if (s->s3.tmp.pkey == NULL) {
  2342. /* SSLfatal() already called */
  2343. goto err;
  2344. }
  2345. /* Encode the public key. */
  2346. encodedlen = EVP_PKEY_get1_encoded_public_key(s->s3.tmp.pkey,
  2347. &encodedPoint);
  2348. if (encodedlen == 0) {
  2349. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
  2350. goto err;
  2351. }
  2352. /*
  2353. * We'll generate the serverKeyExchange message explicitly so we
  2354. * can set these to NULLs
  2355. */
  2356. r[0] = NULL;
  2357. r[1] = NULL;
  2358. r[2] = NULL;
  2359. r[3] = NULL;
  2360. } else
  2361. #ifndef OPENSSL_NO_SRP
  2362. if (type & SSL_kSRP) {
  2363. if ((s->srp_ctx.N == NULL) ||
  2364. (s->srp_ctx.g == NULL) ||
  2365. (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
  2366. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_SRP_PARAM);
  2367. goto err;
  2368. }
  2369. r[0] = s->srp_ctx.N;
  2370. r[1] = s->srp_ctx.g;
  2371. r[2] = s->srp_ctx.s;
  2372. r[3] = s->srp_ctx.B;
  2373. } else
  2374. #endif
  2375. {
  2376. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  2377. goto err;
  2378. }
  2379. if (((s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0)
  2380. || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) {
  2381. lu = NULL;
  2382. } else if (lu == NULL) {
  2383. SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
  2384. goto err;
  2385. }
  2386. #ifndef OPENSSL_NO_PSK
  2387. if (type & SSL_PSK) {
  2388. size_t len = (s->cert->psk_identity_hint == NULL)
  2389. ? 0 : strlen(s->cert->psk_identity_hint);
  2390. /*
  2391. * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
  2392. * checked this when we set the identity hint - but just in case
  2393. */
  2394. if (len > PSK_MAX_IDENTITY_LEN
  2395. || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,
  2396. len)) {
  2397. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2398. goto err;
  2399. }
  2400. }
  2401. #endif
  2402. for (i = 0; i < 4 && r[i] != NULL; i++) {
  2403. unsigned char *binval;
  2404. int res;
  2405. #ifndef OPENSSL_NO_SRP
  2406. if ((i == 2) && (type & SSL_kSRP)) {
  2407. res = WPACKET_start_sub_packet_u8(pkt);
  2408. } else
  2409. #endif
  2410. res = WPACKET_start_sub_packet_u16(pkt);
  2411. if (!res) {
  2412. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2413. goto err;
  2414. }
  2415. /*-
  2416. * for interoperability with some versions of the Microsoft TLS
  2417. * stack, we need to zero pad the DHE pub key to the same length
  2418. * as the prime
  2419. */
  2420. if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) {
  2421. size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]);
  2422. if (len > 0) {
  2423. if (!WPACKET_allocate_bytes(pkt, len, &binval)) {
  2424. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2425. goto err;
  2426. }
  2427. memset(binval, 0, len);
  2428. }
  2429. }
  2430. if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval)
  2431. || !WPACKET_close(pkt)) {
  2432. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2433. goto err;
  2434. }
  2435. BN_bn2bin(r[i], binval);
  2436. }
  2437. if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
  2438. /*
  2439. * We only support named (not generic) curves. In this situation, the
  2440. * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
  2441. * [1 byte length of encoded point], followed by the actual encoded
  2442. * point itself
  2443. */
  2444. if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE)
  2445. || !WPACKET_put_bytes_u8(pkt, 0)
  2446. || !WPACKET_put_bytes_u8(pkt, curve_id)
  2447. || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) {
  2448. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2449. goto err;
  2450. }
  2451. OPENSSL_free(encodedPoint);
  2452. encodedPoint = NULL;
  2453. }
  2454. /* not anonymous */
  2455. if (lu != NULL) {
  2456. EVP_PKEY *pkey = s->s3.tmp.cert->privatekey;
  2457. const EVP_MD *md;
  2458. unsigned char *sigbytes1, *sigbytes2, *tbs;
  2459. size_t siglen = 0, tbslen;
  2460. if (pkey == NULL || !tls1_lookup_md(sctx, lu, &md)) {
  2461. /* Should never happen */
  2462. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2463. goto err;
  2464. }
  2465. /* Get length of the parameters we have written above */
  2466. if (!WPACKET_get_length(pkt, &paramlen)) {
  2467. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2468. goto err;
  2469. }
  2470. /* send signature algorithm */
  2471. if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {
  2472. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2473. goto err;
  2474. }
  2475. if (EVP_DigestSignInit_ex(md_ctx, &pctx,
  2476. md == NULL ? NULL : EVP_MD_get0_name(md),
  2477. sctx->libctx, sctx->propq, pkey,
  2478. NULL) <= 0) {
  2479. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2480. goto err;
  2481. }
  2482. if (lu->sig == EVP_PKEY_RSA_PSS) {
  2483. if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
  2484. || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) {
  2485. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  2486. goto err;
  2487. }
  2488. }
  2489. tbslen = construct_key_exchange_tbs(s, &tbs,
  2490. s->init_buf->data + paramoffset,
  2491. paramlen);
  2492. if (tbslen == 0) {
  2493. /* SSLfatal() already called */
  2494. goto err;
  2495. }
  2496. if (EVP_DigestSign(md_ctx, NULL, &siglen, tbs, tbslen) <=0
  2497. || !WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1)
  2498. || EVP_DigestSign(md_ctx, sigbytes1, &siglen, tbs, tbslen) <= 0
  2499. || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2)
  2500. || sigbytes1 != sigbytes2) {
  2501. OPENSSL_free(tbs);
  2502. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2503. goto err;
  2504. }
  2505. OPENSSL_free(tbs);
  2506. }
  2507. ret = CON_FUNC_SUCCESS;
  2508. err:
  2509. EVP_PKEY_free(pkdh);
  2510. OPENSSL_free(encodedPoint);
  2511. EVP_MD_CTX_free(md_ctx);
  2512. if (freer) {
  2513. BN_free(r[0]);
  2514. BN_free(r[1]);
  2515. BN_free(r[2]);
  2516. BN_free(r[3]);
  2517. }
  2518. return ret;
  2519. }
  2520. CON_FUNC_RETURN tls_construct_certificate_request(SSL_CONNECTION *s,
  2521. WPACKET *pkt)
  2522. {
  2523. if (SSL_CONNECTION_IS_TLS13(s)) {
  2524. /* Send random context when doing post-handshake auth */
  2525. if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
  2526. OPENSSL_free(s->pha_context);
  2527. s->pha_context_len = 32;
  2528. if ((s->pha_context = OPENSSL_malloc(s->pha_context_len)) == NULL) {
  2529. s->pha_context_len = 0;
  2530. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2531. return CON_FUNC_ERROR;
  2532. }
  2533. if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s)->libctx,
  2534. s->pha_context, s->pha_context_len, 0) <= 0
  2535. || !WPACKET_sub_memcpy_u8(pkt, s->pha_context,
  2536. s->pha_context_len)) {
  2537. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2538. return CON_FUNC_ERROR;
  2539. }
  2540. /* reset the handshake hash back to just after the ClientFinished */
  2541. if (!tls13_restore_handshake_digest_for_pha(s)) {
  2542. /* SSLfatal() already called */
  2543. return CON_FUNC_ERROR;
  2544. }
  2545. } else {
  2546. if (!WPACKET_put_bytes_u8(pkt, 0)) {
  2547. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2548. return CON_FUNC_ERROR;
  2549. }
  2550. }
  2551. if (!tls_construct_extensions(s, pkt,
  2552. SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, NULL,
  2553. 0)) {
  2554. /* SSLfatal() already called */
  2555. return CON_FUNC_ERROR;
  2556. }
  2557. goto done;
  2558. }
  2559. /* get the list of acceptable cert types */
  2560. if (!WPACKET_start_sub_packet_u8(pkt)
  2561. || !ssl3_get_req_cert_type(s, pkt) || !WPACKET_close(pkt)) {
  2562. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2563. return CON_FUNC_ERROR;
  2564. }
  2565. if (SSL_USE_SIGALGS(s)) {
  2566. const uint16_t *psigs;
  2567. size_t nl = tls12_get_psigalgs(s, 1, &psigs);
  2568. if (!WPACKET_start_sub_packet_u16(pkt)
  2569. || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
  2570. || !tls12_copy_sigalgs(s, pkt, psigs, nl)
  2571. || !WPACKET_close(pkt)) {
  2572. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2573. return CON_FUNC_ERROR;
  2574. }
  2575. }
  2576. if (!construct_ca_names(s, get_ca_names(s), pkt)) {
  2577. /* SSLfatal() already called */
  2578. return CON_FUNC_ERROR;
  2579. }
  2580. done:
  2581. s->certreqs_sent++;
  2582. s->s3.tmp.cert_request = 1;
  2583. return CON_FUNC_SUCCESS;
  2584. }
  2585. static int tls_process_cke_psk_preamble(SSL_CONNECTION *s, PACKET *pkt)
  2586. {
  2587. #ifndef OPENSSL_NO_PSK
  2588. unsigned char psk[PSK_MAX_PSK_LEN];
  2589. size_t psklen;
  2590. PACKET psk_identity;
  2591. if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
  2592. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2593. return 0;
  2594. }
  2595. if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
  2596. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DATA_LENGTH_TOO_LONG);
  2597. return 0;
  2598. }
  2599. if (s->psk_server_callback == NULL) {
  2600. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PSK_NO_SERVER_CB);
  2601. return 0;
  2602. }
  2603. if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
  2604. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2605. return 0;
  2606. }
  2607. psklen = s->psk_server_callback(SSL_CONNECTION_GET_SSL(s),
  2608. s->session->psk_identity,
  2609. psk, sizeof(psk));
  2610. if (psklen > PSK_MAX_PSK_LEN) {
  2611. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2612. return 0;
  2613. } else if (psklen == 0) {
  2614. /*
  2615. * PSK related to the given identity not found
  2616. */
  2617. SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY, SSL_R_PSK_IDENTITY_NOT_FOUND);
  2618. return 0;
  2619. }
  2620. OPENSSL_free(s->s3.tmp.psk);
  2621. s->s3.tmp.psk = OPENSSL_memdup(psk, psklen);
  2622. OPENSSL_cleanse(psk, psklen);
  2623. if (s->s3.tmp.psk == NULL) {
  2624. s->s3.tmp.psklen = 0;
  2625. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  2626. return 0;
  2627. }
  2628. s->s3.tmp.psklen = psklen;
  2629. return 1;
  2630. #else
  2631. /* Should never happen */
  2632. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2633. return 0;
  2634. #endif
  2635. }
  2636. static int tls_process_cke_rsa(SSL_CONNECTION *s, PACKET *pkt)
  2637. {
  2638. size_t outlen;
  2639. PACKET enc_premaster;
  2640. EVP_PKEY *rsa = NULL;
  2641. unsigned char *rsa_decrypt = NULL;
  2642. int ret = 0;
  2643. EVP_PKEY_CTX *ctx = NULL;
  2644. OSSL_PARAM params[3], *p = params;
  2645. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  2646. rsa = s->cert->pkeys[SSL_PKEY_RSA].privatekey;
  2647. if (rsa == NULL) {
  2648. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_RSA_CERTIFICATE);
  2649. return 0;
  2650. }
  2651. /* SSLv3 and pre-standard DTLS omit the length bytes. */
  2652. if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
  2653. enc_premaster = *pkt;
  2654. } else {
  2655. if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
  2656. || PACKET_remaining(pkt) != 0) {
  2657. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2658. return 0;
  2659. }
  2660. }
  2661. outlen = SSL_MAX_MASTER_KEY_LENGTH;
  2662. rsa_decrypt = OPENSSL_malloc(outlen);
  2663. if (rsa_decrypt == NULL) {
  2664. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  2665. return 0;
  2666. }
  2667. ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, rsa, sctx->propq);
  2668. if (ctx == NULL) {
  2669. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  2670. goto err;
  2671. }
  2672. /*
  2673. * We must not leak whether a decryption failure occurs because of
  2674. * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
  2675. * section 7.4.7.1). We use the special padding type
  2676. * RSA_PKCS1_WITH_TLS_PADDING to do that. It will automatically decrypt the
  2677. * RSA, check the padding and check that the client version is as expected
  2678. * in the premaster secret. If any of that fails then the function appears
  2679. * to return successfully but with a random result. The call below could
  2680. * still fail if the input is publicly invalid.
  2681. * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
  2682. */
  2683. if (EVP_PKEY_decrypt_init(ctx) <= 0
  2684. || EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_WITH_TLS_PADDING) <= 0) {
  2685. SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);
  2686. goto err;
  2687. }
  2688. *p++ = OSSL_PARAM_construct_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION,
  2689. (unsigned int *)&s->client_version);
  2690. if ((s->options & SSL_OP_TLS_ROLLBACK_BUG) != 0)
  2691. *p++ = OSSL_PARAM_construct_uint(
  2692. OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION,
  2693. (unsigned int *)&s->version);
  2694. *p++ = OSSL_PARAM_construct_end();
  2695. if (!EVP_PKEY_CTX_set_params(ctx, params)
  2696. || EVP_PKEY_decrypt(ctx, rsa_decrypt, &outlen,
  2697. PACKET_data(&enc_premaster),
  2698. PACKET_remaining(&enc_premaster)) <= 0) {
  2699. SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);
  2700. goto err;
  2701. }
  2702. /*
  2703. * This test should never fail (otherwise we should have failed above) but
  2704. * we double check anyway.
  2705. */
  2706. if (outlen != SSL_MAX_MASTER_KEY_LENGTH) {
  2707. OPENSSL_cleanse(rsa_decrypt, SSL_MAX_MASTER_KEY_LENGTH);
  2708. SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);
  2709. goto err;
  2710. }
  2711. /* Also cleanses rsa_decrypt (on success or failure) */
  2712. if (!ssl_generate_master_secret(s, rsa_decrypt, outlen, 0)) {
  2713. /* SSLfatal() already called */
  2714. goto err;
  2715. }
  2716. ret = 1;
  2717. err:
  2718. OPENSSL_free(rsa_decrypt);
  2719. EVP_PKEY_CTX_free(ctx);
  2720. return ret;
  2721. }
  2722. static int tls_process_cke_dhe(SSL_CONNECTION *s, PACKET *pkt)
  2723. {
  2724. EVP_PKEY *skey = NULL;
  2725. unsigned int i;
  2726. const unsigned char *data;
  2727. EVP_PKEY *ckey = NULL;
  2728. int ret = 0;
  2729. if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) {
  2730. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
  2731. goto err;
  2732. }
  2733. skey = s->s3.tmp.pkey;
  2734. if (skey == NULL) {
  2735. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY);
  2736. goto err;
  2737. }
  2738. if (PACKET_remaining(pkt) == 0L) {
  2739. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_MISSING_TMP_DH_KEY);
  2740. goto err;
  2741. }
  2742. if (!PACKET_get_bytes(pkt, &data, i)) {
  2743. /* We already checked we have enough data */
  2744. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2745. goto err;
  2746. }
  2747. ckey = EVP_PKEY_new();
  2748. if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {
  2749. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);
  2750. goto err;
  2751. }
  2752. if (!EVP_PKEY_set1_encoded_public_key(ckey, data, i)) {
  2753. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2754. goto err;
  2755. }
  2756. if (ssl_derive(s, skey, ckey, 1) == 0) {
  2757. /* SSLfatal() already called */
  2758. goto err;
  2759. }
  2760. ret = 1;
  2761. EVP_PKEY_free(s->s3.tmp.pkey);
  2762. s->s3.tmp.pkey = NULL;
  2763. err:
  2764. EVP_PKEY_free(ckey);
  2765. return ret;
  2766. }
  2767. static int tls_process_cke_ecdhe(SSL_CONNECTION *s, PACKET *pkt)
  2768. {
  2769. EVP_PKEY *skey = s->s3.tmp.pkey;
  2770. EVP_PKEY *ckey = NULL;
  2771. int ret = 0;
  2772. if (PACKET_remaining(pkt) == 0L) {
  2773. /* We don't support ECDH client auth */
  2774. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_MISSING_TMP_ECDH_KEY);
  2775. goto err;
  2776. } else {
  2777. unsigned int i;
  2778. const unsigned char *data;
  2779. /*
  2780. * Get client's public key from encoded point in the
  2781. * ClientKeyExchange message.
  2782. */
  2783. /* Get encoded point length */
  2784. if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i)
  2785. || PACKET_remaining(pkt) != 0) {
  2786. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2787. goto err;
  2788. }
  2789. if (skey == NULL) {
  2790. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_ECDH_KEY);
  2791. goto err;
  2792. }
  2793. ckey = EVP_PKEY_new();
  2794. if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {
  2795. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);
  2796. goto err;
  2797. }
  2798. if (EVP_PKEY_set1_encoded_public_key(ckey, data, i) <= 0) {
  2799. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
  2800. goto err;
  2801. }
  2802. }
  2803. if (ssl_derive(s, skey, ckey, 1) == 0) {
  2804. /* SSLfatal() already called */
  2805. goto err;
  2806. }
  2807. ret = 1;
  2808. EVP_PKEY_free(s->s3.tmp.pkey);
  2809. s->s3.tmp.pkey = NULL;
  2810. err:
  2811. EVP_PKEY_free(ckey);
  2812. return ret;
  2813. }
  2814. static int tls_process_cke_srp(SSL_CONNECTION *s, PACKET *pkt)
  2815. {
  2816. #ifndef OPENSSL_NO_SRP
  2817. unsigned int i;
  2818. const unsigned char *data;
  2819. if (!PACKET_get_net_2(pkt, &i)
  2820. || !PACKET_get_bytes(pkt, &data, i)) {
  2821. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_SRP_A_LENGTH);
  2822. return 0;
  2823. }
  2824. if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
  2825. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB);
  2826. return 0;
  2827. }
  2828. if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) {
  2829. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_SRP_PARAMETERS);
  2830. return 0;
  2831. }
  2832. OPENSSL_free(s->session->srp_username);
  2833. s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
  2834. if (s->session->srp_username == NULL) {
  2835. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  2836. return 0;
  2837. }
  2838. if (!srp_generate_server_master_secret(s)) {
  2839. /* SSLfatal() already called */
  2840. return 0;
  2841. }
  2842. return 1;
  2843. #else
  2844. /* Should never happen */
  2845. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2846. return 0;
  2847. #endif
  2848. }
  2849. static int tls_process_cke_gost(SSL_CONNECTION *s, PACKET *pkt)
  2850. {
  2851. #ifndef OPENSSL_NO_GOST
  2852. EVP_PKEY_CTX *pkey_ctx;
  2853. EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
  2854. unsigned char premaster_secret[32];
  2855. const unsigned char *start;
  2856. size_t outlen = sizeof(premaster_secret), inlen;
  2857. unsigned long alg_a;
  2858. GOST_KX_MESSAGE *pKX = NULL;
  2859. const unsigned char *ptr;
  2860. int ret = 0;
  2861. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  2862. /* Get our certificate private key */
  2863. alg_a = s->s3.tmp.new_cipher->algorithm_auth;
  2864. if (alg_a & SSL_aGOST12) {
  2865. /*
  2866. * New GOST ciphersuites have SSL_aGOST01 bit too
  2867. */
  2868. pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
  2869. if (pk == NULL) {
  2870. pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
  2871. }
  2872. if (pk == NULL) {
  2873. pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
  2874. }
  2875. } else if (alg_a & SSL_aGOST01) {
  2876. pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
  2877. }
  2878. pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pk, sctx->propq);
  2879. if (pkey_ctx == NULL) {
  2880. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  2881. return 0;
  2882. }
  2883. if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
  2884. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2885. return 0;
  2886. }
  2887. /*
  2888. * If client certificate is present and is of the same type, maybe
  2889. * use it for key exchange. Don't mind errors from
  2890. * EVP_PKEY_derive_set_peer, because it is completely valid to use a
  2891. * client certificate for authorization only.
  2892. */
  2893. client_pub_pkey = tls_get_peer_pkey(s);
  2894. if (client_pub_pkey) {
  2895. if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
  2896. ERR_clear_error();
  2897. }
  2898. ptr = PACKET_data(pkt);
  2899. /* Some implementations provide extra data in the opaqueBlob
  2900. * We have nothing to do with this blob so we just skip it */
  2901. pKX = d2i_GOST_KX_MESSAGE(NULL, &ptr, PACKET_remaining(pkt));
  2902. if (pKX == NULL
  2903. || pKX->kxBlob == NULL
  2904. || ASN1_TYPE_get(pKX->kxBlob) != V_ASN1_SEQUENCE) {
  2905. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);
  2906. goto err;
  2907. }
  2908. if (!PACKET_forward(pkt, ptr - PACKET_data(pkt))) {
  2909. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED);
  2910. goto err;
  2911. }
  2912. if (PACKET_remaining(pkt) != 0) {
  2913. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED);
  2914. goto err;
  2915. }
  2916. inlen = pKX->kxBlob->value.sequence->length;
  2917. start = pKX->kxBlob->value.sequence->data;
  2918. if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start,
  2919. inlen) <= 0) {
  2920. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);
  2921. goto err;
  2922. }
  2923. /* Generate master secret */
  2924. if (!ssl_generate_master_secret(s, premaster_secret, outlen, 0)) {
  2925. /* SSLfatal() already called */
  2926. goto err;
  2927. }
  2928. /* Check if pubkey from client certificate was used */
  2929. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2,
  2930. NULL) > 0)
  2931. s->statem.no_cert_verify = 1;
  2932. ret = 1;
  2933. err:
  2934. EVP_PKEY_CTX_free(pkey_ctx);
  2935. GOST_KX_MESSAGE_free(pKX);
  2936. return ret;
  2937. #else
  2938. /* Should never happen */
  2939. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2940. return 0;
  2941. #endif
  2942. }
  2943. static int tls_process_cke_gost18(SSL_CONNECTION *s, PACKET *pkt)
  2944. {
  2945. #ifndef OPENSSL_NO_GOST
  2946. unsigned char rnd_dgst[32];
  2947. EVP_PKEY_CTX *pkey_ctx = NULL;
  2948. EVP_PKEY *pk = NULL;
  2949. unsigned char premaster_secret[32];
  2950. const unsigned char *start = NULL;
  2951. size_t outlen = sizeof(premaster_secret), inlen = 0;
  2952. int ret = 0;
  2953. int cipher_nid = ossl_gost18_cke_cipher_nid(s);
  2954. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  2955. if (cipher_nid == NID_undef) {
  2956. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2957. return 0;
  2958. }
  2959. if (ossl_gost_ukm(s, rnd_dgst) <= 0) {
  2960. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2961. goto err;
  2962. }
  2963. /* Get our certificate private key */
  2964. pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey != NULL ?
  2965. s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey :
  2966. s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
  2967. if (pk == NULL) {
  2968. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE);
  2969. goto err;
  2970. }
  2971. pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pk, sctx->propq);
  2972. if (pkey_ctx == NULL) {
  2973. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  2974. goto err;
  2975. }
  2976. if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
  2977. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2978. goto err;
  2979. }
  2980. /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code depending on size */
  2981. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT,
  2982. EVP_PKEY_CTRL_SET_IV, 32, rnd_dgst) <= 0) {
  2983. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
  2984. goto err;
  2985. }
  2986. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT,
  2987. EVP_PKEY_CTRL_CIPHER, cipher_nid, NULL) <= 0) {
  2988. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
  2989. goto err;
  2990. }
  2991. inlen = PACKET_remaining(pkt);
  2992. start = PACKET_data(pkt);
  2993. if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
  2994. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);
  2995. goto err;
  2996. }
  2997. /* Generate master secret */
  2998. if (!ssl_generate_master_secret(s, premaster_secret, outlen, 0)) {
  2999. /* SSLfatal() already called */
  3000. goto err;
  3001. }
  3002. ret = 1;
  3003. err:
  3004. EVP_PKEY_CTX_free(pkey_ctx);
  3005. return ret;
  3006. #else
  3007. /* Should never happen */
  3008. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3009. return 0;
  3010. #endif
  3011. }
  3012. MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL_CONNECTION *s,
  3013. PACKET *pkt)
  3014. {
  3015. unsigned long alg_k;
  3016. alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
  3017. /* For PSK parse and retrieve identity, obtain PSK key */
  3018. if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt)) {
  3019. /* SSLfatal() already called */
  3020. goto err;
  3021. }
  3022. if (alg_k & SSL_kPSK) {
  3023. /* Identity extracted earlier: should be nothing left */
  3024. if (PACKET_remaining(pkt) != 0) {
  3025. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  3026. goto err;
  3027. }
  3028. /* PSK handled by ssl_generate_master_secret */
  3029. if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
  3030. /* SSLfatal() already called */
  3031. goto err;
  3032. }
  3033. } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
  3034. if (!tls_process_cke_rsa(s, pkt)) {
  3035. /* SSLfatal() already called */
  3036. goto err;
  3037. }
  3038. } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
  3039. if (!tls_process_cke_dhe(s, pkt)) {
  3040. /* SSLfatal() already called */
  3041. goto err;
  3042. }
  3043. } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
  3044. if (!tls_process_cke_ecdhe(s, pkt)) {
  3045. /* SSLfatal() already called */
  3046. goto err;
  3047. }
  3048. } else if (alg_k & SSL_kSRP) {
  3049. if (!tls_process_cke_srp(s, pkt)) {
  3050. /* SSLfatal() already called */
  3051. goto err;
  3052. }
  3053. } else if (alg_k & SSL_kGOST) {
  3054. if (!tls_process_cke_gost(s, pkt)) {
  3055. /* SSLfatal() already called */
  3056. goto err;
  3057. }
  3058. } else if (alg_k & SSL_kGOST18) {
  3059. if (!tls_process_cke_gost18(s, pkt)) {
  3060. /* SSLfatal() already called */
  3061. goto err;
  3062. }
  3063. } else {
  3064. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_CIPHER_TYPE);
  3065. goto err;
  3066. }
  3067. return MSG_PROCESS_CONTINUE_PROCESSING;
  3068. err:
  3069. #ifndef OPENSSL_NO_PSK
  3070. OPENSSL_clear_free(s->s3.tmp.psk, s->s3.tmp.psklen);
  3071. s->s3.tmp.psk = NULL;
  3072. s->s3.tmp.psklen = 0;
  3073. #endif
  3074. return MSG_PROCESS_ERROR;
  3075. }
  3076. WORK_STATE tls_post_process_client_key_exchange(SSL_CONNECTION *s,
  3077. WORK_STATE wst)
  3078. {
  3079. #ifndef OPENSSL_NO_SCTP
  3080. if (wst == WORK_MORE_A) {
  3081. if (SSL_CONNECTION_IS_DTLS(s)) {
  3082. unsigned char sctpauthkey[64];
  3083. char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
  3084. size_t labellen;
  3085. /*
  3086. * Add new shared key for SCTP-Auth, will be ignored if no SCTP
  3087. * used.
  3088. */
  3089. memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
  3090. sizeof(DTLS1_SCTP_AUTH_LABEL));
  3091. /* Don't include the terminating zero. */
  3092. labellen = sizeof(labelbuffer) - 1;
  3093. if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
  3094. labellen += 1;
  3095. if (SSL_export_keying_material(SSL_CONNECTION_GET_SSL(s),
  3096. sctpauthkey,
  3097. sizeof(sctpauthkey), labelbuffer,
  3098. labellen, NULL, 0,
  3099. 0) <= 0) {
  3100. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3101. return WORK_ERROR;
  3102. }
  3103. BIO_ctrl(s->wbio, BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  3104. sizeof(sctpauthkey), sctpauthkey);
  3105. }
  3106. }
  3107. #endif
  3108. if (s->statem.no_cert_verify || !received_client_cert(s)) {
  3109. /*
  3110. * No certificate verify or no peer certificate so we no longer need
  3111. * the handshake_buffer
  3112. */
  3113. if (!ssl3_digest_cached_records(s, 0)) {
  3114. /* SSLfatal() already called */
  3115. return WORK_ERROR;
  3116. }
  3117. return WORK_FINISHED_CONTINUE;
  3118. } else {
  3119. if (!s->s3.handshake_buffer) {
  3120. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3121. return WORK_ERROR;
  3122. }
  3123. /*
  3124. * For sigalgs freeze the handshake buffer. If we support
  3125. * extms we've done this already so this is a no-op
  3126. */
  3127. if (!ssl3_digest_cached_records(s, 1)) {
  3128. /* SSLfatal() already called */
  3129. return WORK_ERROR;
  3130. }
  3131. }
  3132. return WORK_FINISHED_CONTINUE;
  3133. }
  3134. MSG_PROCESS_RETURN tls_process_client_rpk(SSL_CONNECTION *sc, PACKET *pkt)
  3135. {
  3136. MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
  3137. SSL_SESSION *new_sess = NULL;
  3138. EVP_PKEY *peer_rpk = NULL;
  3139. if (!tls_process_rpk(sc, pkt, &peer_rpk)) {
  3140. /* SSLfatal already called */
  3141. goto err;
  3142. }
  3143. if (peer_rpk == NULL) {
  3144. if ((sc->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
  3145. && (sc->verify_mode & SSL_VERIFY_PEER)) {
  3146. SSLfatal(sc, SSL_AD_CERTIFICATE_REQUIRED,
  3147. SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  3148. goto err;
  3149. }
  3150. } else {
  3151. if (ssl_verify_rpk(sc, peer_rpk) <= 0) {
  3152. SSLfatal(sc, ssl_x509err2alert(sc->verify_result),
  3153. SSL_R_CERTIFICATE_VERIFY_FAILED);
  3154. goto err;
  3155. }
  3156. }
  3157. /*
  3158. * Sessions must be immutable once they go into the session cache. Otherwise
  3159. * we can get multi-thread problems. Therefore we don't "update" sessions,
  3160. * we replace them with a duplicate. Here, we need to do this every time
  3161. * a new RPK (or certificate) is received via post-handshake authentication,
  3162. * as the session may have already gone into the session cache.
  3163. */
  3164. if (sc->post_handshake_auth == SSL_PHA_REQUESTED) {
  3165. if ((new_sess = ssl_session_dup(sc->session, 0)) == NULL) {
  3166. SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  3167. goto err;
  3168. }
  3169. SSL_SESSION_free(sc->session);
  3170. sc->session = new_sess;
  3171. }
  3172. /* Ensure there is no peer/peer_chain */
  3173. X509_free(sc->session->peer);
  3174. sc->session->peer = NULL;
  3175. sk_X509_pop_free(sc->session->peer_chain, X509_free);
  3176. sc->session->peer_chain = NULL;
  3177. /* Save RPK */
  3178. EVP_PKEY_free(sc->session->peer_rpk);
  3179. sc->session->peer_rpk = peer_rpk;
  3180. peer_rpk = NULL;
  3181. sc->session->verify_result = sc->verify_result;
  3182. /*
  3183. * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
  3184. * message
  3185. */
  3186. if (SSL_CONNECTION_IS_TLS13(sc)) {
  3187. if (!ssl3_digest_cached_records(sc, 1)) {
  3188. /* SSLfatal() already called */
  3189. goto err;
  3190. }
  3191. /* Save the current hash state for when we receive the CertificateVerify */
  3192. if (!ssl_handshake_hash(sc, sc->cert_verify_hash,
  3193. sizeof(sc->cert_verify_hash),
  3194. &sc->cert_verify_hash_len)) {
  3195. /* SSLfatal() already called */;
  3196. goto err;
  3197. }
  3198. /* resend session tickets */
  3199. sc->sent_tickets = 0;
  3200. }
  3201. ret = MSG_PROCESS_CONTINUE_READING;
  3202. err:
  3203. EVP_PKEY_free(peer_rpk);
  3204. return ret;
  3205. }
  3206. MSG_PROCESS_RETURN tls_process_client_certificate(SSL_CONNECTION *s,
  3207. PACKET *pkt)
  3208. {
  3209. int i;
  3210. MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
  3211. X509 *x = NULL;
  3212. unsigned long l;
  3213. const unsigned char *certstart, *certbytes;
  3214. STACK_OF(X509) *sk = NULL;
  3215. PACKET spkt, context;
  3216. size_t chainidx;
  3217. SSL_SESSION *new_sess = NULL;
  3218. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  3219. /*
  3220. * To get this far we must have read encrypted data from the client. We no
  3221. * longer tolerate unencrypted alerts. This is ignored if less than TLSv1.3
  3222. */
  3223. if (s->rlayer.rrlmethod->set_plain_alerts != NULL)
  3224. s->rlayer.rrlmethod->set_plain_alerts(s->rlayer.rrl, 0);
  3225. if (s->ext.client_cert_type == TLSEXT_cert_type_rpk)
  3226. return tls_process_client_rpk(s, pkt);
  3227. if (s->ext.client_cert_type != TLSEXT_cert_type_x509) {
  3228. SSLfatal(s, SSL_AD_UNSUPPORTED_CERTIFICATE,
  3229. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  3230. goto err;
  3231. }
  3232. if ((sk = sk_X509_new_null()) == NULL) {
  3233. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  3234. goto err;
  3235. }
  3236. if (SSL_CONNECTION_IS_TLS13(s)
  3237. && (!PACKET_get_length_prefixed_1(pkt, &context)
  3238. || (s->pha_context == NULL && PACKET_remaining(&context) != 0)
  3239. || (s->pha_context != NULL
  3240. && !PACKET_equal(&context, s->pha_context,
  3241. s->pha_context_len)))) {
  3242. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_INVALID_CONTEXT);
  3243. goto err;
  3244. }
  3245. if (!PACKET_get_length_prefixed_3(pkt, &spkt)
  3246. || PACKET_remaining(pkt) != 0) {
  3247. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  3248. goto err;
  3249. }
  3250. for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) {
  3251. if (!PACKET_get_net_3(&spkt, &l)
  3252. || !PACKET_get_bytes(&spkt, &certbytes, l)) {
  3253. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
  3254. goto err;
  3255. }
  3256. certstart = certbytes;
  3257. x = X509_new_ex(sctx->libctx, sctx->propq);
  3258. if (x == NULL) {
  3259. SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_X509_LIB);
  3260. goto err;
  3261. }
  3262. if (d2i_X509(&x, (const unsigned char **)&certbytes, l) == NULL) {
  3263. SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_ASN1_LIB);
  3264. goto err;
  3265. }
  3266. if (certbytes != (certstart + l)) {
  3267. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
  3268. goto err;
  3269. }
  3270. if (SSL_CONNECTION_IS_TLS13(s)) {
  3271. RAW_EXTENSION *rawexts = NULL;
  3272. PACKET extensions;
  3273. if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) {
  3274. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
  3275. goto err;
  3276. }
  3277. if (!tls_collect_extensions(s, &extensions,
  3278. SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
  3279. NULL, chainidx == 0)
  3280. || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
  3281. rawexts, x, chainidx,
  3282. PACKET_remaining(&spkt) == 0)) {
  3283. OPENSSL_free(rawexts);
  3284. goto err;
  3285. }
  3286. OPENSSL_free(rawexts);
  3287. }
  3288. if (!sk_X509_push(sk, x)) {
  3289. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  3290. goto err;
  3291. }
  3292. x = NULL;
  3293. }
  3294. if (sk_X509_num(sk) <= 0) {
  3295. /* TLS does not mind 0 certs returned */
  3296. if (s->version == SSL3_VERSION) {
  3297. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3298. SSL_R_NO_CERTIFICATES_RETURNED);
  3299. goto err;
  3300. }
  3301. /* Fail for TLS only if we required a certificate */
  3302. else if ((s->verify_mode & SSL_VERIFY_PEER) &&
  3303. (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
  3304. SSLfatal(s, SSL_AD_CERTIFICATE_REQUIRED,
  3305. SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  3306. goto err;
  3307. }
  3308. /* No client certificate so digest cached records */
  3309. if (s->s3.handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
  3310. /* SSLfatal() already called */
  3311. goto err;
  3312. }
  3313. } else {
  3314. EVP_PKEY *pkey;
  3315. i = ssl_verify_cert_chain(s, sk);
  3316. if (i <= 0) {
  3317. SSLfatal(s, ssl_x509err2alert(s->verify_result),
  3318. SSL_R_CERTIFICATE_VERIFY_FAILED);
  3319. goto err;
  3320. }
  3321. pkey = X509_get0_pubkey(sk_X509_value(sk, 0));
  3322. if (pkey == NULL) {
  3323. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3324. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  3325. goto err;
  3326. }
  3327. }
  3328. /*
  3329. * Sessions must be immutable once they go into the session cache. Otherwise
  3330. * we can get multi-thread problems. Therefore we don't "update" sessions,
  3331. * we replace them with a duplicate. Here, we need to do this every time
  3332. * a new certificate is received via post-handshake authentication, as the
  3333. * session may have already gone into the session cache.
  3334. */
  3335. if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
  3336. if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
  3337. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
  3338. goto err;
  3339. }
  3340. SSL_SESSION_free(s->session);
  3341. s->session = new_sess;
  3342. }
  3343. X509_free(s->session->peer);
  3344. s->session->peer = sk_X509_shift(sk);
  3345. s->session->verify_result = s->verify_result;
  3346. OSSL_STACK_OF_X509_free(s->session->peer_chain);
  3347. s->session->peer_chain = sk;
  3348. sk = NULL;
  3349. /* Ensure there is no RPK */
  3350. EVP_PKEY_free(s->session->peer_rpk);
  3351. s->session->peer_rpk = NULL;
  3352. /*
  3353. * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
  3354. * message
  3355. */
  3356. if (SSL_CONNECTION_IS_TLS13(s) && !ssl3_digest_cached_records(s, 1)) {
  3357. /* SSLfatal() already called */
  3358. goto err;
  3359. }
  3360. /*
  3361. * Inconsistency alert: cert_chain does *not* include the peer's own
  3362. * certificate, while we do include it in statem_clnt.c
  3363. */
  3364. /* Save the current hash state for when we receive the CertificateVerify */
  3365. if (SSL_CONNECTION_IS_TLS13(s)) {
  3366. if (!ssl_handshake_hash(s, s->cert_verify_hash,
  3367. sizeof(s->cert_verify_hash),
  3368. &s->cert_verify_hash_len)) {
  3369. /* SSLfatal() already called */
  3370. goto err;
  3371. }
  3372. /* Resend session tickets */
  3373. s->sent_tickets = 0;
  3374. }
  3375. ret = MSG_PROCESS_CONTINUE_READING;
  3376. err:
  3377. X509_free(x);
  3378. OSSL_STACK_OF_X509_free(sk);
  3379. return ret;
  3380. }
  3381. #ifndef OPENSSL_NO_COMP_ALG
  3382. MSG_PROCESS_RETURN tls_process_client_compressed_certificate(SSL_CONNECTION *sc, PACKET *pkt)
  3383. {
  3384. MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
  3385. PACKET tmppkt;
  3386. BUF_MEM *buf = BUF_MEM_new();
  3387. if (tls13_process_compressed_certificate(sc, pkt, &tmppkt, buf) != MSG_PROCESS_ERROR)
  3388. ret = tls_process_client_certificate(sc, &tmppkt);
  3389. BUF_MEM_free(buf);
  3390. return ret;
  3391. }
  3392. #endif
  3393. CON_FUNC_RETURN tls_construct_server_certificate(SSL_CONNECTION *s, WPACKET *pkt)
  3394. {
  3395. CERT_PKEY *cpk = s->s3.tmp.cert;
  3396. if (cpk == NULL) {
  3397. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3398. return CON_FUNC_ERROR;
  3399. }
  3400. /*
  3401. * In TLSv1.3 the certificate chain is always preceded by a 0 length context
  3402. * for the server Certificate message
  3403. */
  3404. if (SSL_CONNECTION_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0)) {
  3405. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3406. return CON_FUNC_ERROR;
  3407. }
  3408. switch (s->ext.server_cert_type) {
  3409. case TLSEXT_cert_type_rpk:
  3410. if (!tls_output_rpk(s, pkt, cpk)) {
  3411. /* SSLfatal() already called */
  3412. return 0;
  3413. }
  3414. break;
  3415. case TLSEXT_cert_type_x509:
  3416. if (!ssl3_output_cert_chain(s, pkt, cpk, 0)) {
  3417. /* SSLfatal() already called */
  3418. return 0;
  3419. }
  3420. break;
  3421. default:
  3422. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3423. return 0;
  3424. }
  3425. return CON_FUNC_SUCCESS;
  3426. }
  3427. #ifndef OPENSSL_NO_COMP_ALG
  3428. CON_FUNC_RETURN tls_construct_server_compressed_certificate(SSL_CONNECTION *sc, WPACKET *pkt)
  3429. {
  3430. int alg = get_compressed_certificate_alg(sc);
  3431. OSSL_COMP_CERT *cc = sc->s3.tmp.cert->comp_cert[alg];
  3432. if (!ossl_assert(cc != NULL)) {
  3433. SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3434. return 0;
  3435. }
  3436. /*
  3437. * Server can't compress on-demand
  3438. * Use pre-compressed certificate
  3439. */
  3440. if (!WPACKET_put_bytes_u16(pkt, alg)
  3441. || !WPACKET_put_bytes_u24(pkt, cc->orig_len)
  3442. || !WPACKET_start_sub_packet_u24(pkt)
  3443. || !WPACKET_memcpy(pkt, cc->data, cc->len)
  3444. || !WPACKET_close(pkt))
  3445. return 0;
  3446. sc->s3.tmp.cert->cert_comp_used++;
  3447. return 1;
  3448. }
  3449. #endif
  3450. static int create_ticket_prequel(SSL_CONNECTION *s, WPACKET *pkt,
  3451. uint32_t age_add, unsigned char *tick_nonce)
  3452. {
  3453. uint32_t timeout = (uint32_t)ossl_time2seconds(s->session->timeout);
  3454. /*
  3455. * Ticket lifetime hint:
  3456. * In TLSv1.3 we reset the "time" field above, and always specify the
  3457. * timeout, limited to a 1 week period per RFC8446.
  3458. * For TLSv1.2 this is advisory only and we leave this unspecified for
  3459. * resumed session (for simplicity).
  3460. */
  3461. #define ONE_WEEK_SEC (7 * 24 * 60 * 60)
  3462. if (SSL_CONNECTION_IS_TLS13(s)) {
  3463. if (ossl_time_compare(s->session->timeout,
  3464. ossl_seconds2time(ONE_WEEK_SEC)) > 0)
  3465. timeout = ONE_WEEK_SEC;
  3466. } else if (s->hit)
  3467. timeout = 0;
  3468. if (!WPACKET_put_bytes_u32(pkt, timeout)) {
  3469. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3470. return 0;
  3471. }
  3472. if (SSL_CONNECTION_IS_TLS13(s)) {
  3473. if (!WPACKET_put_bytes_u32(pkt, age_add)
  3474. || !WPACKET_sub_memcpy_u8(pkt, tick_nonce, TICKET_NONCE_SIZE)) {
  3475. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3476. return 0;
  3477. }
  3478. }
  3479. /* Start the sub-packet for the actual ticket data */
  3480. if (!WPACKET_start_sub_packet_u16(pkt)) {
  3481. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3482. return 0;
  3483. }
  3484. return 1;
  3485. }
  3486. static CON_FUNC_RETURN construct_stateless_ticket(SSL_CONNECTION *s,
  3487. WPACKET *pkt,
  3488. uint32_t age_add,
  3489. unsigned char *tick_nonce)
  3490. {
  3491. unsigned char *senc = NULL;
  3492. EVP_CIPHER_CTX *ctx = NULL;
  3493. SSL_HMAC *hctx = NULL;
  3494. unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2;
  3495. const unsigned char *const_p;
  3496. int len, slen_full, slen, lenfinal;
  3497. SSL_SESSION *sess;
  3498. size_t hlen;
  3499. SSL_CTX *tctx = s->session_ctx;
  3500. unsigned char iv[EVP_MAX_IV_LENGTH];
  3501. unsigned char key_name[TLSEXT_KEYNAME_LENGTH];
  3502. int iv_len;
  3503. CON_FUNC_RETURN ok = CON_FUNC_ERROR;
  3504. size_t macoffset, macendoffset;
  3505. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  3506. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  3507. /* get session encoding length */
  3508. slen_full = i2d_SSL_SESSION(s->session, NULL);
  3509. /*
  3510. * Some length values are 16 bits, so forget it if session is too
  3511. * long
  3512. */
  3513. if (slen_full == 0 || slen_full > 0xFF00) {
  3514. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3515. goto err;
  3516. }
  3517. senc = OPENSSL_malloc(slen_full);
  3518. if (senc == NULL) {
  3519. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  3520. goto err;
  3521. }
  3522. ctx = EVP_CIPHER_CTX_new();
  3523. if (ctx == NULL) {
  3524. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  3525. goto err;
  3526. }
  3527. hctx = ssl_hmac_new(tctx);
  3528. if (hctx == NULL) {
  3529. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
  3530. goto err;
  3531. }
  3532. p = senc;
  3533. if (!i2d_SSL_SESSION(s->session, &p)) {
  3534. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3535. goto err;
  3536. }
  3537. /*
  3538. * create a fresh copy (not shared with other threads) to clean up
  3539. */
  3540. const_p = senc;
  3541. sess = d2i_SSL_SESSION_ex(NULL, &const_p, slen_full, sctx->libctx,
  3542. sctx->propq);
  3543. if (sess == NULL) {
  3544. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3545. goto err;
  3546. }
  3547. slen = i2d_SSL_SESSION(sess, NULL);
  3548. if (slen == 0 || slen > slen_full) {
  3549. /* shouldn't ever happen */
  3550. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3551. SSL_SESSION_free(sess);
  3552. goto err;
  3553. }
  3554. p = senc;
  3555. if (!i2d_SSL_SESSION(sess, &p)) {
  3556. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3557. SSL_SESSION_free(sess);
  3558. goto err;
  3559. }
  3560. SSL_SESSION_free(sess);
  3561. /*
  3562. * Initialize HMAC and cipher contexts. If callback present it does
  3563. * all the work otherwise use generated values from parent ctx.
  3564. */
  3565. #ifndef OPENSSL_NO_DEPRECATED_3_0
  3566. if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL)
  3567. #else
  3568. if (tctx->ext.ticket_key_evp_cb != NULL)
  3569. #endif
  3570. {
  3571. int ret = 0;
  3572. if (tctx->ext.ticket_key_evp_cb != NULL)
  3573. ret = tctx->ext.ticket_key_evp_cb(ssl, key_name, iv, ctx,
  3574. ssl_hmac_get0_EVP_MAC_CTX(hctx),
  3575. 1);
  3576. #ifndef OPENSSL_NO_DEPRECATED_3_0
  3577. else if (tctx->ext.ticket_key_cb != NULL)
  3578. /* if 0 is returned, write an empty ticket */
  3579. ret = tctx->ext.ticket_key_cb(ssl, key_name, iv, ctx,
  3580. ssl_hmac_get0_HMAC_CTX(hctx), 1);
  3581. #endif
  3582. if (ret == 0) {
  3583. /*
  3584. * In TLSv1.2 we construct a 0 length ticket. In TLSv1.3 a 0
  3585. * length ticket is not allowed so we abort construction of the
  3586. * ticket
  3587. */
  3588. if (SSL_CONNECTION_IS_TLS13(s)) {
  3589. ok = CON_FUNC_DONT_SEND;
  3590. goto err;
  3591. }
  3592. /* Put timeout and length */
  3593. if (!WPACKET_put_bytes_u32(pkt, 0)
  3594. || !WPACKET_put_bytes_u16(pkt, 0)) {
  3595. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3596. goto err;
  3597. }
  3598. OPENSSL_free(senc);
  3599. EVP_CIPHER_CTX_free(ctx);
  3600. ssl_hmac_free(hctx);
  3601. return CON_FUNC_SUCCESS;
  3602. }
  3603. if (ret < 0) {
  3604. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED);
  3605. goto err;
  3606. }
  3607. iv_len = EVP_CIPHER_CTX_get_iv_length(ctx);
  3608. if (iv_len < 0) {
  3609. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3610. goto err;
  3611. }
  3612. } else {
  3613. EVP_CIPHER *cipher = EVP_CIPHER_fetch(sctx->libctx, "AES-256-CBC",
  3614. sctx->propq);
  3615. if (cipher == NULL) {
  3616. /* Error is already recorded */
  3617. SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
  3618. goto err;
  3619. }
  3620. iv_len = EVP_CIPHER_get_iv_length(cipher);
  3621. if (iv_len < 0
  3622. || RAND_bytes_ex(sctx->libctx, iv, iv_len, 0) <= 0
  3623. || !EVP_EncryptInit_ex(ctx, cipher, NULL,
  3624. tctx->ext.secure->tick_aes_key, iv)
  3625. || !ssl_hmac_init(hctx, tctx->ext.secure->tick_hmac_key,
  3626. sizeof(tctx->ext.secure->tick_hmac_key),
  3627. "SHA256")) {
  3628. EVP_CIPHER_free(cipher);
  3629. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3630. goto err;
  3631. }
  3632. EVP_CIPHER_free(cipher);
  3633. memcpy(key_name, tctx->ext.tick_key_name,
  3634. sizeof(tctx->ext.tick_key_name));
  3635. }
  3636. if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
  3637. /* SSLfatal() already called */
  3638. goto err;
  3639. }
  3640. if (!WPACKET_get_total_written(pkt, &macoffset)
  3641. /* Output key name */
  3642. || !WPACKET_memcpy(pkt, key_name, sizeof(key_name))
  3643. /* output IV */
  3644. || !WPACKET_memcpy(pkt, iv, iv_len)
  3645. || !WPACKET_reserve_bytes(pkt, slen + EVP_MAX_BLOCK_LENGTH,
  3646. &encdata1)
  3647. /* Encrypt session data */
  3648. || !EVP_EncryptUpdate(ctx, encdata1, &len, senc, slen)
  3649. || !WPACKET_allocate_bytes(pkt, len, &encdata2)
  3650. || encdata1 != encdata2
  3651. || !EVP_EncryptFinal(ctx, encdata1 + len, &lenfinal)
  3652. || !WPACKET_allocate_bytes(pkt, lenfinal, &encdata2)
  3653. || encdata1 + len != encdata2
  3654. || len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH
  3655. || !WPACKET_get_total_written(pkt, &macendoffset)
  3656. || !ssl_hmac_update(hctx,
  3657. (unsigned char *)s->init_buf->data + macoffset,
  3658. macendoffset - macoffset)
  3659. || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1)
  3660. || !ssl_hmac_final(hctx, macdata1, &hlen, EVP_MAX_MD_SIZE)
  3661. || hlen > EVP_MAX_MD_SIZE
  3662. || !WPACKET_allocate_bytes(pkt, hlen, &macdata2)
  3663. || macdata1 != macdata2) {
  3664. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3665. goto err;
  3666. }
  3667. /* Close the sub-packet created by create_ticket_prequel() */
  3668. if (!WPACKET_close(pkt)) {
  3669. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3670. goto err;
  3671. }
  3672. ok = CON_FUNC_SUCCESS;
  3673. err:
  3674. OPENSSL_free(senc);
  3675. EVP_CIPHER_CTX_free(ctx);
  3676. ssl_hmac_free(hctx);
  3677. return ok;
  3678. }
  3679. static int construct_stateful_ticket(SSL_CONNECTION *s, WPACKET *pkt,
  3680. uint32_t age_add,
  3681. unsigned char *tick_nonce)
  3682. {
  3683. if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
  3684. /* SSLfatal() already called */
  3685. return 0;
  3686. }
  3687. if (!WPACKET_memcpy(pkt, s->session->session_id,
  3688. s->session->session_id_length)
  3689. || !WPACKET_close(pkt)) {
  3690. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3691. return 0;
  3692. }
  3693. return 1;
  3694. }
  3695. static void tls_update_ticket_counts(SSL_CONNECTION *s)
  3696. {
  3697. /*
  3698. * Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets|
  3699. * gets reset to 0 if we send more tickets following a post-handshake
  3700. * auth, but |next_ticket_nonce| does not. If we're sending extra
  3701. * tickets, decrement the count of pending extra tickets.
  3702. */
  3703. s->sent_tickets++;
  3704. s->next_ticket_nonce++;
  3705. if (s->ext.extra_tickets_expected > 0)
  3706. s->ext.extra_tickets_expected--;
  3707. }
  3708. CON_FUNC_RETURN tls_construct_new_session_ticket(SSL_CONNECTION *s, WPACKET *pkt)
  3709. {
  3710. SSL_CTX *tctx = s->session_ctx;
  3711. unsigned char tick_nonce[TICKET_NONCE_SIZE];
  3712. union {
  3713. unsigned char age_add_c[sizeof(uint32_t)];
  3714. uint32_t age_add;
  3715. } age_add_u;
  3716. CON_FUNC_RETURN ret = CON_FUNC_ERROR;
  3717. age_add_u.age_add = 0;
  3718. if (SSL_CONNECTION_IS_TLS13(s)) {
  3719. size_t i, hashlen;
  3720. uint64_t nonce;
  3721. static const unsigned char nonce_label[] = "resumption";
  3722. const EVP_MD *md = ssl_handshake_md(s);
  3723. int hashleni = EVP_MD_get_size(md);
  3724. /* Ensure cast to size_t is safe */
  3725. if (!ossl_assert(hashleni >= 0)) {
  3726. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3727. goto err;
  3728. }
  3729. hashlen = (size_t)hashleni;
  3730. /*
  3731. * If we already sent one NewSessionTicket, or we resumed then
  3732. * s->session may already be in a cache and so we must not modify it.
  3733. * Instead we need to take a copy of it and modify that.
  3734. */
  3735. if (s->sent_tickets != 0 || s->hit) {
  3736. SSL_SESSION *new_sess = ssl_session_dup(s->session, 0);
  3737. if (new_sess == NULL) {
  3738. /* SSLfatal already called */
  3739. goto err;
  3740. }
  3741. SSL_SESSION_free(s->session);
  3742. s->session = new_sess;
  3743. }
  3744. if (!ssl_generate_session_id(s, s->session)) {
  3745. /* SSLfatal() already called */
  3746. goto err;
  3747. }
  3748. if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s)->libctx,
  3749. age_add_u.age_add_c, sizeof(age_add_u), 0) <= 0) {
  3750. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3751. goto err;
  3752. }
  3753. s->session->ext.tick_age_add = age_add_u.age_add;
  3754. nonce = s->next_ticket_nonce;
  3755. for (i = TICKET_NONCE_SIZE; i > 0; i--) {
  3756. tick_nonce[i - 1] = (unsigned char)(nonce & 0xff);
  3757. nonce >>= 8;
  3758. }
  3759. if (!tls13_hkdf_expand(s, md, s->resumption_master_secret,
  3760. nonce_label,
  3761. sizeof(nonce_label) - 1,
  3762. tick_nonce,
  3763. TICKET_NONCE_SIZE,
  3764. s->session->master_key,
  3765. hashlen, 1)) {
  3766. /* SSLfatal() already called */
  3767. goto err;
  3768. }
  3769. s->session->master_key_length = hashlen;
  3770. s->session->time = ossl_time_now();
  3771. ssl_session_calculate_timeout(s->session);
  3772. if (s->s3.alpn_selected != NULL) {
  3773. OPENSSL_free(s->session->ext.alpn_selected);
  3774. s->session->ext.alpn_selected =
  3775. OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len);
  3776. if (s->session->ext.alpn_selected == NULL) {
  3777. s->session->ext.alpn_selected_len = 0;
  3778. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  3779. goto err;
  3780. }
  3781. s->session->ext.alpn_selected_len = s->s3.alpn_selected_len;
  3782. }
  3783. s->session->ext.max_early_data = s->max_early_data;
  3784. }
  3785. if (tctx->generate_ticket_cb != NULL &&
  3786. tctx->generate_ticket_cb(SSL_CONNECTION_GET_SSL(s),
  3787. tctx->ticket_cb_data) == 0) {
  3788. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3789. goto err;
  3790. }
  3791. /*
  3792. * If we are using anti-replay protection then we behave as if
  3793. * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
  3794. * is no point in using full stateless tickets.
  3795. */
  3796. if (SSL_CONNECTION_IS_TLS13(s)
  3797. && ((s->options & SSL_OP_NO_TICKET) != 0
  3798. || (s->max_early_data > 0
  3799. && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))) {
  3800. if (!construct_stateful_ticket(s, pkt, age_add_u.age_add, tick_nonce)) {
  3801. /* SSLfatal() already called */
  3802. goto err;
  3803. }
  3804. } else {
  3805. CON_FUNC_RETURN tmpret;
  3806. tmpret = construct_stateless_ticket(s, pkt, age_add_u.age_add,
  3807. tick_nonce);
  3808. if (tmpret != CON_FUNC_SUCCESS) {
  3809. if (tmpret == CON_FUNC_DONT_SEND) {
  3810. /* Non-fatal. Abort construction but continue */
  3811. ret = CON_FUNC_DONT_SEND;
  3812. /* We count this as a success so update the counts anwyay */
  3813. tls_update_ticket_counts(s);
  3814. }
  3815. /* else SSLfatal() already called */
  3816. goto err;
  3817. }
  3818. }
  3819. if (SSL_CONNECTION_IS_TLS13(s)) {
  3820. if (!tls_construct_extensions(s, pkt,
  3821. SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
  3822. NULL, 0)) {
  3823. /* SSLfatal() already called */
  3824. goto err;
  3825. }
  3826. tls_update_ticket_counts(s);
  3827. ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
  3828. }
  3829. ret = CON_FUNC_SUCCESS;
  3830. err:
  3831. return ret;
  3832. }
  3833. /*
  3834. * In TLSv1.3 this is called from the extensions code, otherwise it is used to
  3835. * create a separate message. Returns 1 on success or 0 on failure.
  3836. */
  3837. int tls_construct_cert_status_body(SSL_CONNECTION *s, WPACKET *pkt)
  3838. {
  3839. if (!WPACKET_put_bytes_u8(pkt, s->ext.status_type)
  3840. || !WPACKET_sub_memcpy_u24(pkt, s->ext.ocsp.resp,
  3841. s->ext.ocsp.resp_len)) {
  3842. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3843. return 0;
  3844. }
  3845. return 1;
  3846. }
  3847. CON_FUNC_RETURN tls_construct_cert_status(SSL_CONNECTION *s, WPACKET *pkt)
  3848. {
  3849. if (!tls_construct_cert_status_body(s, pkt)) {
  3850. /* SSLfatal() already called */
  3851. return CON_FUNC_ERROR;
  3852. }
  3853. return CON_FUNC_SUCCESS;
  3854. }
  3855. #ifndef OPENSSL_NO_NEXTPROTONEG
  3856. /*
  3857. * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
  3858. * It sets the next_proto member in s if found
  3859. */
  3860. MSG_PROCESS_RETURN tls_process_next_proto(SSL_CONNECTION *s, PACKET *pkt)
  3861. {
  3862. PACKET next_proto, padding;
  3863. size_t next_proto_len;
  3864. /*-
  3865. * The payload looks like:
  3866. * uint8 proto_len;
  3867. * uint8 proto[proto_len];
  3868. * uint8 padding_len;
  3869. * uint8 padding[padding_len];
  3870. */
  3871. if (!PACKET_get_length_prefixed_1(pkt, &next_proto)
  3872. || !PACKET_get_length_prefixed_1(pkt, &padding)
  3873. || PACKET_remaining(pkt) > 0) {
  3874. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  3875. return MSG_PROCESS_ERROR;
  3876. }
  3877. if (!PACKET_memdup(&next_proto, &s->ext.npn, &next_proto_len)) {
  3878. s->ext.npn_len = 0;
  3879. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3880. return MSG_PROCESS_ERROR;
  3881. }
  3882. s->ext.npn_len = (unsigned char)next_proto_len;
  3883. return MSG_PROCESS_CONTINUE_READING;
  3884. }
  3885. #endif
  3886. static CON_FUNC_RETURN tls_construct_encrypted_extensions(SSL_CONNECTION *s,
  3887. WPACKET *pkt)
  3888. {
  3889. if (!tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
  3890. NULL, 0)) {
  3891. /* SSLfatal() already called */
  3892. return CON_FUNC_ERROR;
  3893. }
  3894. return CON_FUNC_SUCCESS;
  3895. }
  3896. MSG_PROCESS_RETURN tls_process_end_of_early_data(SSL_CONNECTION *s, PACKET *pkt)
  3897. {
  3898. if (PACKET_remaining(pkt) != 0) {
  3899. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  3900. return MSG_PROCESS_ERROR;
  3901. }
  3902. if (s->early_data_state != SSL_EARLY_DATA_READING
  3903. && s->early_data_state != SSL_EARLY_DATA_READ_RETRY) {
  3904. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3905. return MSG_PROCESS_ERROR;
  3906. }
  3907. /*
  3908. * EndOfEarlyData signals a key change so the end of the message must be on
  3909. * a record boundary.
  3910. */
  3911. if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
  3912. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY);
  3913. return MSG_PROCESS_ERROR;
  3914. }
  3915. s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
  3916. if (!SSL_CONNECTION_GET_SSL(s)->method->ssl3_enc->change_cipher_state(s,
  3917. SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) {
  3918. /* SSLfatal() already called */
  3919. return MSG_PROCESS_ERROR;
  3920. }
  3921. return MSG_PROCESS_CONTINUE_READING;
  3922. }