statem_srvr.c 144 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401
  1. /*
  2. * Copyright 1995-2023 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
  1738. && s->version >= TLS1_VERSION
  1739. && !SSL_CONNECTION_IS_TLS13(s)
  1740. && !SSL_CONNECTION_IS_DTLS(s)
  1741. && s->ext.session_secret_cb != NULL) {
  1742. const SSL_CIPHER *pref_cipher = NULL;
  1743. /*
  1744. * s->session->master_key_length is a size_t, but this is an int for
  1745. * backwards compat reasons
  1746. */
  1747. int master_key_length;
  1748. master_key_length = sizeof(s->session->master_key);
  1749. if (s->ext.session_secret_cb(ssl, s->session->master_key,
  1750. &master_key_length, ciphers,
  1751. &pref_cipher,
  1752. s->ext.session_secret_cb_arg)
  1753. && master_key_length > 0) {
  1754. s->session->master_key_length = master_key_length;
  1755. s->hit = 1;
  1756. s->peer_ciphers = ciphers;
  1757. s->session->verify_result = X509_V_OK;
  1758. ciphers = NULL;
  1759. /* check if some cipher was preferred by call back */
  1760. if (pref_cipher == NULL)
  1761. pref_cipher = ssl3_choose_cipher(s, s->peer_ciphers,
  1762. SSL_get_ciphers(ssl));
  1763. if (pref_cipher == NULL) {
  1764. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_SHARED_CIPHER);
  1765. goto err;
  1766. }
  1767. s->session->cipher = pref_cipher;
  1768. sk_SSL_CIPHER_free(s->cipher_list);
  1769. s->cipher_list = sk_SSL_CIPHER_dup(s->peer_ciphers);
  1770. sk_SSL_CIPHER_free(s->cipher_list_by_id);
  1771. s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->peer_ciphers);
  1772. }
  1773. }
  1774. /*
  1775. * Worst case, we will use the NULL compression, but if we have other
  1776. * options, we will now look for them. We have complen-1 compression
  1777. * algorithms from the client, starting at q.
  1778. */
  1779. s->s3.tmp.new_compression = NULL;
  1780. if (SSL_CONNECTION_IS_TLS13(s)) {
  1781. /*
  1782. * We already checked above that the NULL compression method appears in
  1783. * the list. Now we check there aren't any others (which is illegal in
  1784. * a TLSv1.3 ClientHello.
  1785. */
  1786. if (clienthello->compressions_len != 1) {
  1787. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1788. SSL_R_INVALID_COMPRESSION_ALGORITHM);
  1789. goto err;
  1790. }
  1791. }
  1792. #ifndef OPENSSL_NO_COMP
  1793. /* This only happens if we have a cache hit */
  1794. else if (s->session->compress_meth != 0) {
  1795. int m, comp_id = s->session->compress_meth;
  1796. unsigned int k;
  1797. /* Perform sanity checks on resumed compression algorithm */
  1798. /* Can't disable compression */
  1799. if (!ssl_allow_compression(s)) {
  1800. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1801. SSL_R_INCONSISTENT_COMPRESSION);
  1802. goto err;
  1803. }
  1804. /* Look for resumed compression method */
  1805. for (m = 0; m < sk_SSL_COMP_num(sctx->comp_methods); m++) {
  1806. comp = sk_SSL_COMP_value(sctx->comp_methods, m);
  1807. if (comp_id == comp->id) {
  1808. s->s3.tmp.new_compression = comp;
  1809. break;
  1810. }
  1811. }
  1812. if (s->s3.tmp.new_compression == NULL) {
  1813. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1814. SSL_R_INVALID_COMPRESSION_ALGORITHM);
  1815. goto err;
  1816. }
  1817. /* Look for resumed method in compression list */
  1818. for (k = 0; k < clienthello->compressions_len; k++) {
  1819. if (clienthello->compressions[k] == comp_id)
  1820. break;
  1821. }
  1822. if (k >= clienthello->compressions_len) {
  1823. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1824. SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
  1825. goto err;
  1826. }
  1827. } else if (s->hit) {
  1828. comp = NULL;
  1829. } else if (ssl_allow_compression(s) && sctx->comp_methods) {
  1830. /* See if we have a match */
  1831. int m, nn, v, done = 0;
  1832. unsigned int o;
  1833. nn = sk_SSL_COMP_num(sctx->comp_methods);
  1834. for (m = 0; m < nn; m++) {
  1835. comp = sk_SSL_COMP_value(sctx->comp_methods, m);
  1836. v = comp->id;
  1837. for (o = 0; o < clienthello->compressions_len; o++) {
  1838. if (v == clienthello->compressions[o]) {
  1839. done = 1;
  1840. break;
  1841. }
  1842. }
  1843. if (done)
  1844. break;
  1845. }
  1846. if (done)
  1847. s->s3.tmp.new_compression = comp;
  1848. else
  1849. comp = NULL;
  1850. }
  1851. #else
  1852. /*
  1853. * If compression is disabled we'd better not try to resume a session
  1854. * using compression.
  1855. */
  1856. if (s->session->compress_meth != 0) {
  1857. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_COMPRESSION);
  1858. goto err;
  1859. }
  1860. #endif
  1861. /*
  1862. * Given s->peer_ciphers and SSL_get_ciphers, we must pick a cipher
  1863. */
  1864. if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) {
  1865. sk_SSL_CIPHER_free(s->peer_ciphers);
  1866. s->peer_ciphers = ciphers;
  1867. if (ciphers == NULL) {
  1868. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1869. goto err;
  1870. }
  1871. ciphers = NULL;
  1872. }
  1873. if (!s->hit) {
  1874. #ifdef OPENSSL_NO_COMP
  1875. s->session->compress_meth = 0;
  1876. #else
  1877. s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
  1878. #endif
  1879. if (!tls1_set_server_sigalgs(s)) {
  1880. /* SSLfatal() already called */
  1881. goto err;
  1882. }
  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 (s->session->not_resumable ||
  2169. (!(SSL_CONNECTION_GET_CTX(s)->session_cache_mode & SSL_SESS_CACHE_SERVER)
  2170. && !s->hit))
  2171. s->session->session_id_length = 0;
  2172. if (usetls13) {
  2173. sl = s->tmp_session_id_len;
  2174. session_id = s->tmp_session_id;
  2175. } else {
  2176. sl = s->session->session_id_length;
  2177. session_id = s->session->session_id;
  2178. }
  2179. if (sl > sizeof(s->session->session_id)) {
  2180. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2181. return CON_FUNC_ERROR;
  2182. }
  2183. /* set up the compression method */
  2184. #ifdef OPENSSL_NO_COMP
  2185. compm = 0;
  2186. #else
  2187. if (usetls13 || s->s3.tmp.new_compression == NULL)
  2188. compm = 0;
  2189. else
  2190. compm = s->s3.tmp.new_compression->id;
  2191. #endif
  2192. if (!WPACKET_sub_memcpy_u8(pkt, session_id, sl)
  2193. || !SSL_CONNECTION_GET_SSL(s)->method->put_cipher_by_char(s->s3.tmp.new_cipher,
  2194. pkt, &len)
  2195. || !WPACKET_put_bytes_u8(pkt, compm)) {
  2196. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2197. return CON_FUNC_ERROR;
  2198. }
  2199. if (!tls_construct_extensions(s, pkt,
  2200. s->hello_retry_request == SSL_HRR_PENDING
  2201. ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
  2202. : (SSL_CONNECTION_IS_TLS13(s)
  2203. ? SSL_EXT_TLS1_3_SERVER_HELLO
  2204. : SSL_EXT_TLS1_2_SERVER_HELLO),
  2205. NULL, 0)) {
  2206. /* SSLfatal() already called */
  2207. return CON_FUNC_ERROR;
  2208. }
  2209. if (s->hello_retry_request == SSL_HRR_PENDING) {
  2210. /* Ditch the session. We'll create a new one next time around */
  2211. SSL_SESSION_free(s->session);
  2212. s->session = NULL;
  2213. s->hit = 0;
  2214. /*
  2215. * Re-initialise the Transcript Hash. We're going to prepopulate it with
  2216. * a synthetic message_hash in place of ClientHello1.
  2217. */
  2218. if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) {
  2219. /* SSLfatal() already called */
  2220. return CON_FUNC_ERROR;
  2221. }
  2222. } else if (!(s->verify_mode & SSL_VERIFY_PEER)
  2223. && !ssl3_digest_cached_records(s, 0)) {
  2224. /* SSLfatal() already called */;
  2225. return CON_FUNC_ERROR;
  2226. }
  2227. return CON_FUNC_SUCCESS;
  2228. }
  2229. CON_FUNC_RETURN tls_construct_server_done(SSL_CONNECTION *s, WPACKET *pkt)
  2230. {
  2231. if (!s->s3.tmp.cert_request) {
  2232. if (!ssl3_digest_cached_records(s, 0)) {
  2233. /* SSLfatal() already called */
  2234. return CON_FUNC_ERROR;
  2235. }
  2236. }
  2237. return CON_FUNC_SUCCESS;
  2238. }
  2239. CON_FUNC_RETURN tls_construct_server_key_exchange(SSL_CONNECTION *s,
  2240. WPACKET *pkt)
  2241. {
  2242. EVP_PKEY *pkdh = NULL;
  2243. unsigned char *encodedPoint = NULL;
  2244. size_t encodedlen = 0;
  2245. int curve_id = 0;
  2246. const SIGALG_LOOKUP *lu = s->s3.tmp.sigalg;
  2247. int i;
  2248. unsigned long type;
  2249. BIGNUM *r[4];
  2250. EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
  2251. EVP_PKEY_CTX *pctx = NULL;
  2252. size_t paramlen, paramoffset;
  2253. int freer = 0;
  2254. CON_FUNC_RETURN ret = CON_FUNC_ERROR;
  2255. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  2256. if (!WPACKET_get_total_written(pkt, &paramoffset)) {
  2257. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2258. goto err;
  2259. }
  2260. if (md_ctx == NULL) {
  2261. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  2262. goto err;
  2263. }
  2264. type = s->s3.tmp.new_cipher->algorithm_mkey;
  2265. r[0] = r[1] = r[2] = r[3] = NULL;
  2266. #ifndef OPENSSL_NO_PSK
  2267. /* Plain PSK or RSAPSK nothing to do */
  2268. if (type & (SSL_kPSK | SSL_kRSAPSK)) {
  2269. } else
  2270. #endif /* !OPENSSL_NO_PSK */
  2271. if (type & (SSL_kDHE | SSL_kDHEPSK)) {
  2272. CERT *cert = s->cert;
  2273. EVP_PKEY *pkdhp = NULL;
  2274. if (s->cert->dh_tmp_auto) {
  2275. pkdh = ssl_get_auto_dh(s);
  2276. if (pkdh == NULL) {
  2277. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2278. goto err;
  2279. }
  2280. pkdhp = pkdh;
  2281. } else {
  2282. pkdhp = cert->dh_tmp;
  2283. }
  2284. #if !defined(OPENSSL_NO_DEPRECATED_3_0)
  2285. if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {
  2286. pkdh = ssl_dh_to_pkey(s->cert->dh_tmp_cb(SSL_CONNECTION_GET_SSL(s),
  2287. 0, 1024));
  2288. if (pkdh == NULL) {
  2289. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2290. goto err;
  2291. }
  2292. pkdhp = pkdh;
  2293. }
  2294. #endif
  2295. if (pkdhp == NULL) {
  2296. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY);
  2297. goto err;
  2298. }
  2299. if (!ssl_security(s, SSL_SECOP_TMP_DH,
  2300. EVP_PKEY_get_security_bits(pkdhp), 0, pkdhp)) {
  2301. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DH_KEY_TOO_SMALL);
  2302. goto err;
  2303. }
  2304. if (s->s3.tmp.pkey != NULL) {
  2305. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2306. goto err;
  2307. }
  2308. s->s3.tmp.pkey = ssl_generate_pkey(s, pkdhp);
  2309. if (s->s3.tmp.pkey == NULL) {
  2310. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2311. goto err;
  2312. }
  2313. EVP_PKEY_free(pkdh);
  2314. pkdh = NULL;
  2315. /* These BIGNUMs need to be freed when we're finished */
  2316. freer = 1;
  2317. if (!EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_P,
  2318. &r[0])
  2319. || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_G,
  2320. &r[1])
  2321. || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey,
  2322. OSSL_PKEY_PARAM_PUB_KEY, &r[2])) {
  2323. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2324. goto err;
  2325. }
  2326. } else if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
  2327. if (s->s3.tmp.pkey != NULL) {
  2328. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2329. goto err;
  2330. }
  2331. /* Get NID of appropriate shared curve */
  2332. curve_id = tls1_shared_group(s, -2);
  2333. if (curve_id == 0) {
  2334. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  2335. SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
  2336. goto err;
  2337. }
  2338. /* Cache the group used in the SSL_SESSION */
  2339. s->session->kex_group = curve_id;
  2340. /* Generate a new key for this curve */
  2341. s->s3.tmp.pkey = ssl_generate_pkey_group(s, curve_id);
  2342. if (s->s3.tmp.pkey == NULL) {
  2343. /* SSLfatal() already called */
  2344. goto err;
  2345. }
  2346. /* Encode the public key. */
  2347. encodedlen = EVP_PKEY_get1_encoded_public_key(s->s3.tmp.pkey,
  2348. &encodedPoint);
  2349. if (encodedlen == 0) {
  2350. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
  2351. goto err;
  2352. }
  2353. /*
  2354. * We'll generate the serverKeyExchange message explicitly so we
  2355. * can set these to NULLs
  2356. */
  2357. r[0] = NULL;
  2358. r[1] = NULL;
  2359. r[2] = NULL;
  2360. r[3] = NULL;
  2361. } else
  2362. #ifndef OPENSSL_NO_SRP
  2363. if (type & SSL_kSRP) {
  2364. if ((s->srp_ctx.N == NULL) ||
  2365. (s->srp_ctx.g == NULL) ||
  2366. (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
  2367. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_SRP_PARAM);
  2368. goto err;
  2369. }
  2370. r[0] = s->srp_ctx.N;
  2371. r[1] = s->srp_ctx.g;
  2372. r[2] = s->srp_ctx.s;
  2373. r[3] = s->srp_ctx.B;
  2374. } else
  2375. #endif
  2376. {
  2377. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  2378. goto err;
  2379. }
  2380. if (((s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0)
  2381. || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) {
  2382. lu = NULL;
  2383. } else if (lu == NULL) {
  2384. SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
  2385. goto err;
  2386. }
  2387. #ifndef OPENSSL_NO_PSK
  2388. if (type & SSL_PSK) {
  2389. size_t len = (s->cert->psk_identity_hint == NULL)
  2390. ? 0 : strlen(s->cert->psk_identity_hint);
  2391. /*
  2392. * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
  2393. * checked this when we set the identity hint - but just in case
  2394. */
  2395. if (len > PSK_MAX_IDENTITY_LEN
  2396. || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,
  2397. len)) {
  2398. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2399. goto err;
  2400. }
  2401. }
  2402. #endif
  2403. for (i = 0; i < 4 && r[i] != NULL; i++) {
  2404. unsigned char *binval;
  2405. int res;
  2406. #ifndef OPENSSL_NO_SRP
  2407. if ((i == 2) && (type & SSL_kSRP)) {
  2408. res = WPACKET_start_sub_packet_u8(pkt);
  2409. } else
  2410. #endif
  2411. res = WPACKET_start_sub_packet_u16(pkt);
  2412. if (!res) {
  2413. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2414. goto err;
  2415. }
  2416. /*-
  2417. * for interoperability with some versions of the Microsoft TLS
  2418. * stack, we need to zero pad the DHE pub key to the same length
  2419. * as the prime
  2420. */
  2421. if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) {
  2422. size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]);
  2423. if (len > 0) {
  2424. if (!WPACKET_allocate_bytes(pkt, len, &binval)) {
  2425. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2426. goto err;
  2427. }
  2428. memset(binval, 0, len);
  2429. }
  2430. }
  2431. if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval)
  2432. || !WPACKET_close(pkt)) {
  2433. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2434. goto err;
  2435. }
  2436. BN_bn2bin(r[i], binval);
  2437. }
  2438. if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
  2439. /*
  2440. * We only support named (not generic) curves. In this situation, the
  2441. * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
  2442. * [1 byte length of encoded point], followed by the actual encoded
  2443. * point itself
  2444. */
  2445. if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE)
  2446. || !WPACKET_put_bytes_u8(pkt, 0)
  2447. || !WPACKET_put_bytes_u8(pkt, curve_id)
  2448. || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) {
  2449. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2450. goto err;
  2451. }
  2452. OPENSSL_free(encodedPoint);
  2453. encodedPoint = NULL;
  2454. }
  2455. /* not anonymous */
  2456. if (lu != NULL) {
  2457. EVP_PKEY *pkey = s->s3.tmp.cert->privatekey;
  2458. const EVP_MD *md;
  2459. unsigned char *sigbytes1, *sigbytes2, *tbs;
  2460. size_t siglen = 0, tbslen;
  2461. if (pkey == NULL || !tls1_lookup_md(sctx, lu, &md)) {
  2462. /* Should never happen */
  2463. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2464. goto err;
  2465. }
  2466. /* Get length of the parameters we have written above */
  2467. if (!WPACKET_get_length(pkt, &paramlen)) {
  2468. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2469. goto err;
  2470. }
  2471. /* send signature algorithm */
  2472. if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {
  2473. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2474. goto err;
  2475. }
  2476. if (EVP_DigestSignInit_ex(md_ctx, &pctx,
  2477. md == NULL ? NULL : EVP_MD_get0_name(md),
  2478. sctx->libctx, sctx->propq, pkey,
  2479. NULL) <= 0) {
  2480. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2481. goto err;
  2482. }
  2483. if (lu->sig == EVP_PKEY_RSA_PSS) {
  2484. if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
  2485. || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) {
  2486. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  2487. goto err;
  2488. }
  2489. }
  2490. tbslen = construct_key_exchange_tbs(s, &tbs,
  2491. s->init_buf->data + paramoffset,
  2492. paramlen);
  2493. if (tbslen == 0) {
  2494. /* SSLfatal() already called */
  2495. goto err;
  2496. }
  2497. if (EVP_DigestSign(md_ctx, NULL, &siglen, tbs, tbslen) <=0
  2498. || !WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1)
  2499. || EVP_DigestSign(md_ctx, sigbytes1, &siglen, tbs, tbslen) <= 0
  2500. || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2)
  2501. || sigbytes1 != sigbytes2) {
  2502. OPENSSL_free(tbs);
  2503. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2504. goto err;
  2505. }
  2506. OPENSSL_free(tbs);
  2507. }
  2508. ret = CON_FUNC_SUCCESS;
  2509. err:
  2510. EVP_PKEY_free(pkdh);
  2511. OPENSSL_free(encodedPoint);
  2512. EVP_MD_CTX_free(md_ctx);
  2513. if (freer) {
  2514. BN_free(r[0]);
  2515. BN_free(r[1]);
  2516. BN_free(r[2]);
  2517. BN_free(r[3]);
  2518. }
  2519. return ret;
  2520. }
  2521. CON_FUNC_RETURN tls_construct_certificate_request(SSL_CONNECTION *s,
  2522. WPACKET *pkt)
  2523. {
  2524. if (SSL_CONNECTION_IS_TLS13(s)) {
  2525. /* Send random context when doing post-handshake auth */
  2526. if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
  2527. OPENSSL_free(s->pha_context);
  2528. s->pha_context_len = 32;
  2529. if ((s->pha_context = OPENSSL_malloc(s->pha_context_len)) == NULL) {
  2530. s->pha_context_len = 0;
  2531. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2532. return CON_FUNC_ERROR;
  2533. }
  2534. if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s)->libctx,
  2535. s->pha_context, s->pha_context_len, 0) <= 0
  2536. || !WPACKET_sub_memcpy_u8(pkt, s->pha_context,
  2537. s->pha_context_len)) {
  2538. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2539. return CON_FUNC_ERROR;
  2540. }
  2541. /* reset the handshake hash back to just after the ClientFinished */
  2542. if (!tls13_restore_handshake_digest_for_pha(s)) {
  2543. /* SSLfatal() already called */
  2544. return CON_FUNC_ERROR;
  2545. }
  2546. } else {
  2547. if (!WPACKET_put_bytes_u8(pkt, 0)) {
  2548. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2549. return CON_FUNC_ERROR;
  2550. }
  2551. }
  2552. if (!tls_construct_extensions(s, pkt,
  2553. SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, NULL,
  2554. 0)) {
  2555. /* SSLfatal() already called */
  2556. return CON_FUNC_ERROR;
  2557. }
  2558. goto done;
  2559. }
  2560. /* get the list of acceptable cert types */
  2561. if (!WPACKET_start_sub_packet_u8(pkt)
  2562. || !ssl3_get_req_cert_type(s, pkt) || !WPACKET_close(pkt)) {
  2563. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2564. return CON_FUNC_ERROR;
  2565. }
  2566. if (SSL_USE_SIGALGS(s)) {
  2567. const uint16_t *psigs;
  2568. size_t nl = tls12_get_psigalgs(s, 1, &psigs);
  2569. if (!WPACKET_start_sub_packet_u16(pkt)
  2570. || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
  2571. || !tls12_copy_sigalgs(s, pkt, psigs, nl)
  2572. || !WPACKET_close(pkt)) {
  2573. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2574. return CON_FUNC_ERROR;
  2575. }
  2576. }
  2577. if (!construct_ca_names(s, get_ca_names(s), pkt)) {
  2578. /* SSLfatal() already called */
  2579. return CON_FUNC_ERROR;
  2580. }
  2581. done:
  2582. s->certreqs_sent++;
  2583. s->s3.tmp.cert_request = 1;
  2584. return CON_FUNC_SUCCESS;
  2585. }
  2586. static int tls_process_cke_psk_preamble(SSL_CONNECTION *s, PACKET *pkt)
  2587. {
  2588. #ifndef OPENSSL_NO_PSK
  2589. unsigned char psk[PSK_MAX_PSK_LEN];
  2590. size_t psklen;
  2591. PACKET psk_identity;
  2592. if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
  2593. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2594. return 0;
  2595. }
  2596. if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
  2597. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DATA_LENGTH_TOO_LONG);
  2598. return 0;
  2599. }
  2600. if (s->psk_server_callback == NULL) {
  2601. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PSK_NO_SERVER_CB);
  2602. return 0;
  2603. }
  2604. if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
  2605. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2606. return 0;
  2607. }
  2608. psklen = s->psk_server_callback(SSL_CONNECTION_GET_SSL(s),
  2609. s->session->psk_identity,
  2610. psk, sizeof(psk));
  2611. if (psklen > PSK_MAX_PSK_LEN) {
  2612. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2613. return 0;
  2614. } else if (psklen == 0) {
  2615. /*
  2616. * PSK related to the given identity not found
  2617. */
  2618. SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY, SSL_R_PSK_IDENTITY_NOT_FOUND);
  2619. return 0;
  2620. }
  2621. OPENSSL_free(s->s3.tmp.psk);
  2622. s->s3.tmp.psk = OPENSSL_memdup(psk, psklen);
  2623. OPENSSL_cleanse(psk, psklen);
  2624. if (s->s3.tmp.psk == NULL) {
  2625. s->s3.tmp.psklen = 0;
  2626. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  2627. return 0;
  2628. }
  2629. s->s3.tmp.psklen = psklen;
  2630. return 1;
  2631. #else
  2632. /* Should never happen */
  2633. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2634. return 0;
  2635. #endif
  2636. }
  2637. static int tls_process_cke_rsa(SSL_CONNECTION *s, PACKET *pkt)
  2638. {
  2639. size_t outlen;
  2640. PACKET enc_premaster;
  2641. EVP_PKEY *rsa = NULL;
  2642. unsigned char *rsa_decrypt = NULL;
  2643. int ret = 0;
  2644. EVP_PKEY_CTX *ctx = NULL;
  2645. OSSL_PARAM params[3], *p = params;
  2646. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  2647. rsa = s->cert->pkeys[SSL_PKEY_RSA].privatekey;
  2648. if (rsa == NULL) {
  2649. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_RSA_CERTIFICATE);
  2650. return 0;
  2651. }
  2652. /* SSLv3 and pre-standard DTLS omit the length bytes. */
  2653. if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
  2654. enc_premaster = *pkt;
  2655. } else {
  2656. if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
  2657. || PACKET_remaining(pkt) != 0) {
  2658. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2659. return 0;
  2660. }
  2661. }
  2662. outlen = SSL_MAX_MASTER_KEY_LENGTH;
  2663. rsa_decrypt = OPENSSL_malloc(outlen);
  2664. if (rsa_decrypt == NULL) {
  2665. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  2666. return 0;
  2667. }
  2668. ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, rsa, sctx->propq);
  2669. if (ctx == NULL) {
  2670. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  2671. goto err;
  2672. }
  2673. /*
  2674. * We must not leak whether a decryption failure occurs because of
  2675. * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
  2676. * section 7.4.7.1). We use the special padding type
  2677. * RSA_PKCS1_WITH_TLS_PADDING to do that. It will automatically decrypt the
  2678. * RSA, check the padding and check that the client version is as expected
  2679. * in the premaster secret. If any of that fails then the function appears
  2680. * to return successfully but with a random result. The call below could
  2681. * still fail if the input is publicly invalid.
  2682. * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
  2683. */
  2684. if (EVP_PKEY_decrypt_init(ctx) <= 0
  2685. || EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_WITH_TLS_PADDING) <= 0) {
  2686. SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);
  2687. goto err;
  2688. }
  2689. *p++ = OSSL_PARAM_construct_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION,
  2690. (unsigned int *)&s->client_version);
  2691. if ((s->options & SSL_OP_TLS_ROLLBACK_BUG) != 0)
  2692. *p++ = OSSL_PARAM_construct_uint(
  2693. OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION,
  2694. (unsigned int *)&s->version);
  2695. *p++ = OSSL_PARAM_construct_end();
  2696. if (!EVP_PKEY_CTX_set_params(ctx, params)
  2697. || EVP_PKEY_decrypt(ctx, rsa_decrypt, &outlen,
  2698. PACKET_data(&enc_premaster),
  2699. PACKET_remaining(&enc_premaster)) <= 0) {
  2700. SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);
  2701. goto err;
  2702. }
  2703. /*
  2704. * This test should never fail (otherwise we should have failed above) but
  2705. * we double check anyway.
  2706. */
  2707. if (outlen != SSL_MAX_MASTER_KEY_LENGTH) {
  2708. OPENSSL_cleanse(rsa_decrypt, SSL_MAX_MASTER_KEY_LENGTH);
  2709. SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);
  2710. goto err;
  2711. }
  2712. /* Also cleanses rsa_decrypt (on success or failure) */
  2713. if (!ssl_generate_master_secret(s, rsa_decrypt, outlen, 0)) {
  2714. /* SSLfatal() already called */
  2715. goto err;
  2716. }
  2717. ret = 1;
  2718. err:
  2719. OPENSSL_free(rsa_decrypt);
  2720. EVP_PKEY_CTX_free(ctx);
  2721. return ret;
  2722. }
  2723. static int tls_process_cke_dhe(SSL_CONNECTION *s, PACKET *pkt)
  2724. {
  2725. EVP_PKEY *skey = NULL;
  2726. unsigned int i;
  2727. const unsigned char *data;
  2728. EVP_PKEY *ckey = NULL;
  2729. int ret = 0;
  2730. if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) {
  2731. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
  2732. goto err;
  2733. }
  2734. skey = s->s3.tmp.pkey;
  2735. if (skey == NULL) {
  2736. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY);
  2737. goto err;
  2738. }
  2739. if (PACKET_remaining(pkt) == 0L) {
  2740. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_MISSING_TMP_DH_KEY);
  2741. goto err;
  2742. }
  2743. if (!PACKET_get_bytes(pkt, &data, i)) {
  2744. /* We already checked we have enough data */
  2745. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2746. goto err;
  2747. }
  2748. ckey = EVP_PKEY_new();
  2749. if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {
  2750. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);
  2751. goto err;
  2752. }
  2753. if (!EVP_PKEY_set1_encoded_public_key(ckey, data, i)) {
  2754. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2755. goto err;
  2756. }
  2757. if (ssl_derive(s, skey, ckey, 1) == 0) {
  2758. /* SSLfatal() already called */
  2759. goto err;
  2760. }
  2761. ret = 1;
  2762. EVP_PKEY_free(s->s3.tmp.pkey);
  2763. s->s3.tmp.pkey = NULL;
  2764. err:
  2765. EVP_PKEY_free(ckey);
  2766. return ret;
  2767. }
  2768. static int tls_process_cke_ecdhe(SSL_CONNECTION *s, PACKET *pkt)
  2769. {
  2770. EVP_PKEY *skey = s->s3.tmp.pkey;
  2771. EVP_PKEY *ckey = NULL;
  2772. int ret = 0;
  2773. if (PACKET_remaining(pkt) == 0L) {
  2774. /* We don't support ECDH client auth */
  2775. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_MISSING_TMP_ECDH_KEY);
  2776. goto err;
  2777. } else {
  2778. unsigned int i;
  2779. const unsigned char *data;
  2780. /*
  2781. * Get client's public key from encoded point in the
  2782. * ClientKeyExchange message.
  2783. */
  2784. /* Get encoded point length */
  2785. if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i)
  2786. || PACKET_remaining(pkt) != 0) {
  2787. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2788. goto err;
  2789. }
  2790. if (skey == NULL) {
  2791. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_ECDH_KEY);
  2792. goto err;
  2793. }
  2794. ckey = EVP_PKEY_new();
  2795. if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {
  2796. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);
  2797. goto err;
  2798. }
  2799. if (EVP_PKEY_set1_encoded_public_key(ckey, data, i) <= 0) {
  2800. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
  2801. goto err;
  2802. }
  2803. }
  2804. if (ssl_derive(s, skey, ckey, 1) == 0) {
  2805. /* SSLfatal() already called */
  2806. goto err;
  2807. }
  2808. ret = 1;
  2809. EVP_PKEY_free(s->s3.tmp.pkey);
  2810. s->s3.tmp.pkey = NULL;
  2811. err:
  2812. EVP_PKEY_free(ckey);
  2813. return ret;
  2814. }
  2815. static int tls_process_cke_srp(SSL_CONNECTION *s, PACKET *pkt)
  2816. {
  2817. #ifndef OPENSSL_NO_SRP
  2818. unsigned int i;
  2819. const unsigned char *data;
  2820. if (!PACKET_get_net_2(pkt, &i)
  2821. || !PACKET_get_bytes(pkt, &data, i)) {
  2822. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_SRP_A_LENGTH);
  2823. return 0;
  2824. }
  2825. if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
  2826. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB);
  2827. return 0;
  2828. }
  2829. if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) {
  2830. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_SRP_PARAMETERS);
  2831. return 0;
  2832. }
  2833. OPENSSL_free(s->session->srp_username);
  2834. s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
  2835. if (s->session->srp_username == NULL) {
  2836. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  2837. return 0;
  2838. }
  2839. if (!srp_generate_server_master_secret(s)) {
  2840. /* SSLfatal() already called */
  2841. return 0;
  2842. }
  2843. return 1;
  2844. #else
  2845. /* Should never happen */
  2846. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2847. return 0;
  2848. #endif
  2849. }
  2850. static int tls_process_cke_gost(SSL_CONNECTION *s, PACKET *pkt)
  2851. {
  2852. #ifndef OPENSSL_NO_GOST
  2853. EVP_PKEY_CTX *pkey_ctx;
  2854. EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
  2855. unsigned char premaster_secret[32];
  2856. const unsigned char *start;
  2857. size_t outlen = sizeof(premaster_secret), inlen;
  2858. unsigned long alg_a;
  2859. GOST_KX_MESSAGE *pKX = NULL;
  2860. const unsigned char *ptr;
  2861. int ret = 0;
  2862. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  2863. /* Get our certificate private key */
  2864. alg_a = s->s3.tmp.new_cipher->algorithm_auth;
  2865. if (alg_a & SSL_aGOST12) {
  2866. /*
  2867. * New GOST ciphersuites have SSL_aGOST01 bit too
  2868. */
  2869. pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
  2870. if (pk == NULL) {
  2871. pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
  2872. }
  2873. if (pk == NULL) {
  2874. pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
  2875. }
  2876. } else if (alg_a & SSL_aGOST01) {
  2877. pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
  2878. }
  2879. pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pk, sctx->propq);
  2880. if (pkey_ctx == NULL) {
  2881. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  2882. return 0;
  2883. }
  2884. if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
  2885. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2886. return 0;
  2887. }
  2888. /*
  2889. * If client certificate is present and is of the same type, maybe
  2890. * use it for key exchange. Don't mind errors from
  2891. * EVP_PKEY_derive_set_peer, because it is completely valid to use a
  2892. * client certificate for authorization only.
  2893. */
  2894. client_pub_pkey = tls_get_peer_pkey(s);
  2895. if (client_pub_pkey) {
  2896. if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
  2897. ERR_clear_error();
  2898. }
  2899. ptr = PACKET_data(pkt);
  2900. /* Some implementations provide extra data in the opaqueBlob
  2901. * We have nothing to do with this blob so we just skip it */
  2902. pKX = d2i_GOST_KX_MESSAGE(NULL, &ptr, PACKET_remaining(pkt));
  2903. if (pKX == NULL
  2904. || pKX->kxBlob == NULL
  2905. || ASN1_TYPE_get(pKX->kxBlob) != V_ASN1_SEQUENCE) {
  2906. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);
  2907. goto err;
  2908. }
  2909. if (!PACKET_forward(pkt, ptr - PACKET_data(pkt))) {
  2910. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED);
  2911. goto err;
  2912. }
  2913. if (PACKET_remaining(pkt) != 0) {
  2914. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED);
  2915. goto err;
  2916. }
  2917. inlen = pKX->kxBlob->value.sequence->length;
  2918. start = pKX->kxBlob->value.sequence->data;
  2919. if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start,
  2920. inlen) <= 0) {
  2921. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);
  2922. goto err;
  2923. }
  2924. /* Generate master secret */
  2925. if (!ssl_generate_master_secret(s, premaster_secret, outlen, 0)) {
  2926. /* SSLfatal() already called */
  2927. goto err;
  2928. }
  2929. /* Check if pubkey from client certificate was used */
  2930. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2,
  2931. NULL) > 0)
  2932. s->statem.no_cert_verify = 1;
  2933. ret = 1;
  2934. err:
  2935. EVP_PKEY_CTX_free(pkey_ctx);
  2936. GOST_KX_MESSAGE_free(pKX);
  2937. return ret;
  2938. #else
  2939. /* Should never happen */
  2940. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2941. return 0;
  2942. #endif
  2943. }
  2944. static int tls_process_cke_gost18(SSL_CONNECTION *s, PACKET *pkt)
  2945. {
  2946. #ifndef OPENSSL_NO_GOST
  2947. unsigned char rnd_dgst[32];
  2948. EVP_PKEY_CTX *pkey_ctx = NULL;
  2949. EVP_PKEY *pk = NULL;
  2950. unsigned char premaster_secret[32];
  2951. const unsigned char *start = NULL;
  2952. size_t outlen = sizeof(premaster_secret), inlen = 0;
  2953. int ret = 0;
  2954. int cipher_nid = ossl_gost18_cke_cipher_nid(s);
  2955. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  2956. if (cipher_nid == NID_undef) {
  2957. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2958. return 0;
  2959. }
  2960. if (ossl_gost_ukm(s, rnd_dgst) <= 0) {
  2961. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2962. goto err;
  2963. }
  2964. /* Get our certificate private key */
  2965. pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey != NULL ?
  2966. s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey :
  2967. s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
  2968. if (pk == NULL) {
  2969. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE);
  2970. goto err;
  2971. }
  2972. pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pk, sctx->propq);
  2973. if (pkey_ctx == NULL) {
  2974. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  2975. goto err;
  2976. }
  2977. if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
  2978. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2979. goto err;
  2980. }
  2981. /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code depending on size */
  2982. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT,
  2983. EVP_PKEY_CTRL_SET_IV, 32, rnd_dgst) <= 0) {
  2984. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
  2985. goto err;
  2986. }
  2987. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT,
  2988. EVP_PKEY_CTRL_CIPHER, cipher_nid, NULL) <= 0) {
  2989. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
  2990. goto err;
  2991. }
  2992. inlen = PACKET_remaining(pkt);
  2993. start = PACKET_data(pkt);
  2994. if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
  2995. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);
  2996. goto err;
  2997. }
  2998. /* Generate master secret */
  2999. if (!ssl_generate_master_secret(s, premaster_secret, outlen, 0)) {
  3000. /* SSLfatal() already called */
  3001. goto err;
  3002. }
  3003. ret = 1;
  3004. err:
  3005. EVP_PKEY_CTX_free(pkey_ctx);
  3006. return ret;
  3007. #else
  3008. /* Should never happen */
  3009. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3010. return 0;
  3011. #endif
  3012. }
  3013. MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL_CONNECTION *s,
  3014. PACKET *pkt)
  3015. {
  3016. unsigned long alg_k;
  3017. alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
  3018. /* For PSK parse and retrieve identity, obtain PSK key */
  3019. if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt)) {
  3020. /* SSLfatal() already called */
  3021. goto err;
  3022. }
  3023. if (alg_k & SSL_kPSK) {
  3024. /* Identity extracted earlier: should be nothing left */
  3025. if (PACKET_remaining(pkt) != 0) {
  3026. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  3027. goto err;
  3028. }
  3029. /* PSK handled by ssl_generate_master_secret */
  3030. if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
  3031. /* SSLfatal() already called */
  3032. goto err;
  3033. }
  3034. } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
  3035. if (!tls_process_cke_rsa(s, pkt)) {
  3036. /* SSLfatal() already called */
  3037. goto err;
  3038. }
  3039. } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
  3040. if (!tls_process_cke_dhe(s, pkt)) {
  3041. /* SSLfatal() already called */
  3042. goto err;
  3043. }
  3044. } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
  3045. if (!tls_process_cke_ecdhe(s, pkt)) {
  3046. /* SSLfatal() already called */
  3047. goto err;
  3048. }
  3049. } else if (alg_k & SSL_kSRP) {
  3050. if (!tls_process_cke_srp(s, pkt)) {
  3051. /* SSLfatal() already called */
  3052. goto err;
  3053. }
  3054. } else if (alg_k & SSL_kGOST) {
  3055. if (!tls_process_cke_gost(s, pkt)) {
  3056. /* SSLfatal() already called */
  3057. goto err;
  3058. }
  3059. } else if (alg_k & SSL_kGOST18) {
  3060. if (!tls_process_cke_gost18(s, pkt)) {
  3061. /* SSLfatal() already called */
  3062. goto err;
  3063. }
  3064. } else {
  3065. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_CIPHER_TYPE);
  3066. goto err;
  3067. }
  3068. return MSG_PROCESS_CONTINUE_PROCESSING;
  3069. err:
  3070. #ifndef OPENSSL_NO_PSK
  3071. OPENSSL_clear_free(s->s3.tmp.psk, s->s3.tmp.psklen);
  3072. s->s3.tmp.psk = NULL;
  3073. s->s3.tmp.psklen = 0;
  3074. #endif
  3075. return MSG_PROCESS_ERROR;
  3076. }
  3077. WORK_STATE tls_post_process_client_key_exchange(SSL_CONNECTION *s,
  3078. WORK_STATE wst)
  3079. {
  3080. #ifndef OPENSSL_NO_SCTP
  3081. if (wst == WORK_MORE_A) {
  3082. if (SSL_CONNECTION_IS_DTLS(s)) {
  3083. unsigned char sctpauthkey[64];
  3084. char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
  3085. size_t labellen;
  3086. /*
  3087. * Add new shared key for SCTP-Auth, will be ignored if no SCTP
  3088. * used.
  3089. */
  3090. memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
  3091. sizeof(DTLS1_SCTP_AUTH_LABEL));
  3092. /* Don't include the terminating zero. */
  3093. labellen = sizeof(labelbuffer) - 1;
  3094. if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
  3095. labellen += 1;
  3096. if (SSL_export_keying_material(SSL_CONNECTION_GET_SSL(s),
  3097. sctpauthkey,
  3098. sizeof(sctpauthkey), labelbuffer,
  3099. labellen, NULL, 0,
  3100. 0) <= 0) {
  3101. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3102. return WORK_ERROR;
  3103. }
  3104. BIO_ctrl(s->wbio, BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  3105. sizeof(sctpauthkey), sctpauthkey);
  3106. }
  3107. }
  3108. #endif
  3109. if (s->statem.no_cert_verify || !received_client_cert(s)) {
  3110. /*
  3111. * No certificate verify or no peer certificate so we no longer need
  3112. * the handshake_buffer
  3113. */
  3114. if (!ssl3_digest_cached_records(s, 0)) {
  3115. /* SSLfatal() already called */
  3116. return WORK_ERROR;
  3117. }
  3118. return WORK_FINISHED_CONTINUE;
  3119. } else {
  3120. if (!s->s3.handshake_buffer) {
  3121. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3122. return WORK_ERROR;
  3123. }
  3124. /*
  3125. * For sigalgs freeze the handshake buffer. If we support
  3126. * extms we've done this already so this is a no-op
  3127. */
  3128. if (!ssl3_digest_cached_records(s, 1)) {
  3129. /* SSLfatal() already called */
  3130. return WORK_ERROR;
  3131. }
  3132. }
  3133. return WORK_FINISHED_CONTINUE;
  3134. }
  3135. MSG_PROCESS_RETURN tls_process_client_rpk(SSL_CONNECTION *sc, PACKET *pkt)
  3136. {
  3137. MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
  3138. SSL_SESSION *new_sess = NULL;
  3139. EVP_PKEY *peer_rpk = NULL;
  3140. if (!tls_process_rpk(sc, pkt, &peer_rpk)) {
  3141. /* SSLfatal already called */
  3142. goto err;
  3143. }
  3144. if (peer_rpk == NULL) {
  3145. if ((sc->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
  3146. && (sc->verify_mode & SSL_VERIFY_PEER)) {
  3147. SSLfatal(sc, SSL_AD_CERTIFICATE_REQUIRED,
  3148. SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  3149. goto err;
  3150. }
  3151. } else {
  3152. if (ssl_verify_rpk(sc, peer_rpk) <= 0) {
  3153. SSLfatal(sc, ssl_x509err2alert(sc->verify_result),
  3154. SSL_R_CERTIFICATE_VERIFY_FAILED);
  3155. goto err;
  3156. }
  3157. }
  3158. /*
  3159. * Sessions must be immutable once they go into the session cache. Otherwise
  3160. * we can get multi-thread problems. Therefore we don't "update" sessions,
  3161. * we replace them with a duplicate. Here, we need to do this every time
  3162. * a new RPK (or certificate) is received via post-handshake authentication,
  3163. * as the session may have already gone into the session cache.
  3164. */
  3165. if (sc->post_handshake_auth == SSL_PHA_REQUESTED) {
  3166. if ((new_sess = ssl_session_dup(sc->session, 0)) == NULL) {
  3167. SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  3168. goto err;
  3169. }
  3170. SSL_SESSION_free(sc->session);
  3171. sc->session = new_sess;
  3172. }
  3173. /* Ensure there is no peer/peer_chain */
  3174. X509_free(sc->session->peer);
  3175. sc->session->peer = NULL;
  3176. sk_X509_pop_free(sc->session->peer_chain, X509_free);
  3177. sc->session->peer_chain = NULL;
  3178. /* Save RPK */
  3179. EVP_PKEY_free(sc->session->peer_rpk);
  3180. sc->session->peer_rpk = peer_rpk;
  3181. peer_rpk = NULL;
  3182. sc->session->verify_result = sc->verify_result;
  3183. /*
  3184. * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
  3185. * message
  3186. */
  3187. if (SSL_CONNECTION_IS_TLS13(sc)) {
  3188. if (!ssl3_digest_cached_records(sc, 1)) {
  3189. /* SSLfatal() already called */
  3190. goto err;
  3191. }
  3192. /* Save the current hash state for when we receive the CertificateVerify */
  3193. if (!ssl_handshake_hash(sc, sc->cert_verify_hash,
  3194. sizeof(sc->cert_verify_hash),
  3195. &sc->cert_verify_hash_len)) {
  3196. /* SSLfatal() already called */;
  3197. goto err;
  3198. }
  3199. /* resend session tickets */
  3200. sc->sent_tickets = 0;
  3201. }
  3202. ret = MSG_PROCESS_CONTINUE_READING;
  3203. err:
  3204. EVP_PKEY_free(peer_rpk);
  3205. return ret;
  3206. }
  3207. MSG_PROCESS_RETURN tls_process_client_certificate(SSL_CONNECTION *s,
  3208. PACKET *pkt)
  3209. {
  3210. int i;
  3211. MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
  3212. X509 *x = NULL;
  3213. unsigned long l;
  3214. const unsigned char *certstart, *certbytes;
  3215. STACK_OF(X509) *sk = NULL;
  3216. PACKET spkt, context;
  3217. size_t chainidx;
  3218. SSL_SESSION *new_sess = NULL;
  3219. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  3220. /*
  3221. * To get this far we must have read encrypted data from the client. We no
  3222. * longer tolerate unencrypted alerts. This is ignored if less than TLSv1.3
  3223. */
  3224. if (s->rlayer.rrlmethod->set_plain_alerts != NULL)
  3225. s->rlayer.rrlmethod->set_plain_alerts(s->rlayer.rrl, 0);
  3226. if (s->ext.client_cert_type == TLSEXT_cert_type_rpk)
  3227. return tls_process_client_rpk(s, pkt);
  3228. if (s->ext.client_cert_type != TLSEXT_cert_type_x509) {
  3229. SSLfatal(s, SSL_AD_UNSUPPORTED_CERTIFICATE,
  3230. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  3231. goto err;
  3232. }
  3233. if ((sk = sk_X509_new_null()) == NULL) {
  3234. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  3235. goto err;
  3236. }
  3237. if (SSL_CONNECTION_IS_TLS13(s)
  3238. && (!PACKET_get_length_prefixed_1(pkt, &context)
  3239. || (s->pha_context == NULL && PACKET_remaining(&context) != 0)
  3240. || (s->pha_context != NULL
  3241. && !PACKET_equal(&context, s->pha_context,
  3242. s->pha_context_len)))) {
  3243. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_INVALID_CONTEXT);
  3244. goto err;
  3245. }
  3246. if (!PACKET_get_length_prefixed_3(pkt, &spkt)
  3247. || PACKET_remaining(pkt) != 0) {
  3248. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  3249. goto err;
  3250. }
  3251. for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) {
  3252. if (!PACKET_get_net_3(&spkt, &l)
  3253. || !PACKET_get_bytes(&spkt, &certbytes, l)) {
  3254. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
  3255. goto err;
  3256. }
  3257. certstart = certbytes;
  3258. x = X509_new_ex(sctx->libctx, sctx->propq);
  3259. if (x == NULL) {
  3260. SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_X509_LIB);
  3261. goto err;
  3262. }
  3263. if (d2i_X509(&x, (const unsigned char **)&certbytes, l) == NULL) {
  3264. SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_ASN1_LIB);
  3265. goto err;
  3266. }
  3267. if (certbytes != (certstart + l)) {
  3268. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
  3269. goto err;
  3270. }
  3271. if (SSL_CONNECTION_IS_TLS13(s)) {
  3272. RAW_EXTENSION *rawexts = NULL;
  3273. PACKET extensions;
  3274. if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) {
  3275. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
  3276. goto err;
  3277. }
  3278. if (!tls_collect_extensions(s, &extensions,
  3279. SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
  3280. NULL, chainidx == 0)
  3281. || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
  3282. rawexts, x, chainidx,
  3283. PACKET_remaining(&spkt) == 0)) {
  3284. OPENSSL_free(rawexts);
  3285. goto err;
  3286. }
  3287. OPENSSL_free(rawexts);
  3288. }
  3289. if (!sk_X509_push(sk, x)) {
  3290. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  3291. goto err;
  3292. }
  3293. x = NULL;
  3294. }
  3295. if (sk_X509_num(sk) <= 0) {
  3296. /* TLS does not mind 0 certs returned */
  3297. if (s->version == SSL3_VERSION) {
  3298. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3299. SSL_R_NO_CERTIFICATES_RETURNED);
  3300. goto err;
  3301. }
  3302. /* Fail for TLS only if we required a certificate */
  3303. else if ((s->verify_mode & SSL_VERIFY_PEER) &&
  3304. (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
  3305. SSLfatal(s, SSL_AD_CERTIFICATE_REQUIRED,
  3306. SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  3307. goto err;
  3308. }
  3309. /* No client certificate so digest cached records */
  3310. if (s->s3.handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
  3311. /* SSLfatal() already called */
  3312. goto err;
  3313. }
  3314. } else {
  3315. EVP_PKEY *pkey;
  3316. i = ssl_verify_cert_chain(s, sk);
  3317. if (i <= 0) {
  3318. SSLfatal(s, ssl_x509err2alert(s->verify_result),
  3319. SSL_R_CERTIFICATE_VERIFY_FAILED);
  3320. goto err;
  3321. }
  3322. pkey = X509_get0_pubkey(sk_X509_value(sk, 0));
  3323. if (pkey == NULL) {
  3324. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3325. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  3326. goto err;
  3327. }
  3328. }
  3329. /*
  3330. * Sessions must be immutable once they go into the session cache. Otherwise
  3331. * we can get multi-thread problems. Therefore we don't "update" sessions,
  3332. * we replace them with a duplicate. Here, we need to do this every time
  3333. * a new certificate is received via post-handshake authentication, as the
  3334. * session may have already gone into the session cache.
  3335. */
  3336. if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
  3337. if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
  3338. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
  3339. goto err;
  3340. }
  3341. SSL_SESSION_free(s->session);
  3342. s->session = new_sess;
  3343. }
  3344. X509_free(s->session->peer);
  3345. s->session->peer = sk_X509_shift(sk);
  3346. s->session->verify_result = s->verify_result;
  3347. OSSL_STACK_OF_X509_free(s->session->peer_chain);
  3348. s->session->peer_chain = sk;
  3349. sk = NULL;
  3350. /* Ensure there is no RPK */
  3351. EVP_PKEY_free(s->session->peer_rpk);
  3352. s->session->peer_rpk = NULL;
  3353. /*
  3354. * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
  3355. * message
  3356. */
  3357. if (SSL_CONNECTION_IS_TLS13(s) && !ssl3_digest_cached_records(s, 1)) {
  3358. /* SSLfatal() already called */
  3359. goto err;
  3360. }
  3361. /*
  3362. * Inconsistency alert: cert_chain does *not* include the peer's own
  3363. * certificate, while we do include it in statem_clnt.c
  3364. */
  3365. /* Save the current hash state for when we receive the CertificateVerify */
  3366. if (SSL_CONNECTION_IS_TLS13(s)) {
  3367. if (!ssl_handshake_hash(s, s->cert_verify_hash,
  3368. sizeof(s->cert_verify_hash),
  3369. &s->cert_verify_hash_len)) {
  3370. /* SSLfatal() already called */
  3371. goto err;
  3372. }
  3373. /* Resend session tickets */
  3374. s->sent_tickets = 0;
  3375. }
  3376. ret = MSG_PROCESS_CONTINUE_READING;
  3377. err:
  3378. X509_free(x);
  3379. OSSL_STACK_OF_X509_free(sk);
  3380. return ret;
  3381. }
  3382. #ifndef OPENSSL_NO_COMP_ALG
  3383. MSG_PROCESS_RETURN tls_process_client_compressed_certificate(SSL_CONNECTION *sc, PACKET *pkt)
  3384. {
  3385. MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
  3386. PACKET tmppkt;
  3387. BUF_MEM *buf = BUF_MEM_new();
  3388. if (tls13_process_compressed_certificate(sc, pkt, &tmppkt, buf) != MSG_PROCESS_ERROR)
  3389. ret = tls_process_client_certificate(sc, &tmppkt);
  3390. BUF_MEM_free(buf);
  3391. return ret;
  3392. }
  3393. #endif
  3394. CON_FUNC_RETURN tls_construct_server_certificate(SSL_CONNECTION *s, WPACKET *pkt)
  3395. {
  3396. CERT_PKEY *cpk = s->s3.tmp.cert;
  3397. if (cpk == NULL) {
  3398. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3399. return CON_FUNC_ERROR;
  3400. }
  3401. /*
  3402. * In TLSv1.3 the certificate chain is always preceded by a 0 length context
  3403. * for the server Certificate message
  3404. */
  3405. if (SSL_CONNECTION_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0)) {
  3406. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3407. return CON_FUNC_ERROR;
  3408. }
  3409. switch (s->ext.server_cert_type) {
  3410. case TLSEXT_cert_type_rpk:
  3411. if (!tls_output_rpk(s, pkt, cpk)) {
  3412. /* SSLfatal() already called */
  3413. return 0;
  3414. }
  3415. break;
  3416. case TLSEXT_cert_type_x509:
  3417. if (!ssl3_output_cert_chain(s, pkt, cpk, 0)) {
  3418. /* SSLfatal() already called */
  3419. return 0;
  3420. }
  3421. break;
  3422. default:
  3423. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3424. return 0;
  3425. }
  3426. return CON_FUNC_SUCCESS;
  3427. }
  3428. #ifndef OPENSSL_NO_COMP_ALG
  3429. CON_FUNC_RETURN tls_construct_server_compressed_certificate(SSL_CONNECTION *sc, WPACKET *pkt)
  3430. {
  3431. int alg = get_compressed_certificate_alg(sc);
  3432. OSSL_COMP_CERT *cc = sc->s3.tmp.cert->comp_cert[alg];
  3433. if (!ossl_assert(cc != NULL)) {
  3434. SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3435. return 0;
  3436. }
  3437. /*
  3438. * Server can't compress on-demand
  3439. * Use pre-compressed certificate
  3440. */
  3441. if (!WPACKET_put_bytes_u16(pkt, alg)
  3442. || !WPACKET_put_bytes_u24(pkt, cc->orig_len)
  3443. || !WPACKET_start_sub_packet_u24(pkt)
  3444. || !WPACKET_memcpy(pkt, cc->data, cc->len)
  3445. || !WPACKET_close(pkt))
  3446. return 0;
  3447. sc->s3.tmp.cert->cert_comp_used++;
  3448. return 1;
  3449. }
  3450. #endif
  3451. static int create_ticket_prequel(SSL_CONNECTION *s, WPACKET *pkt,
  3452. uint32_t age_add, unsigned char *tick_nonce)
  3453. {
  3454. uint32_t timeout = (uint32_t)ossl_time2seconds(s->session->timeout);
  3455. /*
  3456. * Ticket lifetime hint:
  3457. * In TLSv1.3 we reset the "time" field above, and always specify the
  3458. * timeout, limited to a 1 week period per RFC8446.
  3459. * For TLSv1.2 this is advisory only and we leave this unspecified for
  3460. * resumed session (for simplicity).
  3461. */
  3462. #define ONE_WEEK_SEC (7 * 24 * 60 * 60)
  3463. if (SSL_CONNECTION_IS_TLS13(s)) {
  3464. if (ossl_time_compare(s->session->timeout,
  3465. ossl_seconds2time(ONE_WEEK_SEC)) > 0)
  3466. timeout = ONE_WEEK_SEC;
  3467. } else if (s->hit)
  3468. timeout = 0;
  3469. if (!WPACKET_put_bytes_u32(pkt, timeout)) {
  3470. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3471. return 0;
  3472. }
  3473. if (SSL_CONNECTION_IS_TLS13(s)) {
  3474. if (!WPACKET_put_bytes_u32(pkt, age_add)
  3475. || !WPACKET_sub_memcpy_u8(pkt, tick_nonce, TICKET_NONCE_SIZE)) {
  3476. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3477. return 0;
  3478. }
  3479. }
  3480. /* Start the sub-packet for the actual ticket data */
  3481. if (!WPACKET_start_sub_packet_u16(pkt)) {
  3482. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3483. return 0;
  3484. }
  3485. return 1;
  3486. }
  3487. static CON_FUNC_RETURN construct_stateless_ticket(SSL_CONNECTION *s,
  3488. WPACKET *pkt,
  3489. uint32_t age_add,
  3490. unsigned char *tick_nonce)
  3491. {
  3492. unsigned char *senc = NULL;
  3493. EVP_CIPHER_CTX *ctx = NULL;
  3494. SSL_HMAC *hctx = NULL;
  3495. unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2;
  3496. const unsigned char *const_p;
  3497. int len, slen_full, slen, lenfinal;
  3498. SSL_SESSION *sess;
  3499. size_t hlen;
  3500. SSL_CTX *tctx = s->session_ctx;
  3501. unsigned char iv[EVP_MAX_IV_LENGTH];
  3502. unsigned char key_name[TLSEXT_KEYNAME_LENGTH];
  3503. int iv_len;
  3504. CON_FUNC_RETURN ok = CON_FUNC_ERROR;
  3505. size_t macoffset, macendoffset;
  3506. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  3507. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  3508. /* get session encoding length */
  3509. slen_full = i2d_SSL_SESSION(s->session, NULL);
  3510. /*
  3511. * Some length values are 16 bits, so forget it if session is too
  3512. * long
  3513. */
  3514. if (slen_full == 0 || slen_full > 0xFF00) {
  3515. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3516. goto err;
  3517. }
  3518. senc = OPENSSL_malloc(slen_full);
  3519. if (senc == NULL) {
  3520. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  3521. goto err;
  3522. }
  3523. ctx = EVP_CIPHER_CTX_new();
  3524. if (ctx == NULL) {
  3525. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  3526. goto err;
  3527. }
  3528. hctx = ssl_hmac_new(tctx);
  3529. if (hctx == NULL) {
  3530. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
  3531. goto err;
  3532. }
  3533. p = senc;
  3534. if (!i2d_SSL_SESSION(s->session, &p)) {
  3535. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3536. goto err;
  3537. }
  3538. /*
  3539. * create a fresh copy (not shared with other threads) to clean up
  3540. */
  3541. const_p = senc;
  3542. sess = d2i_SSL_SESSION_ex(NULL, &const_p, slen_full, sctx->libctx,
  3543. sctx->propq);
  3544. if (sess == NULL) {
  3545. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3546. goto err;
  3547. }
  3548. slen = i2d_SSL_SESSION(sess, NULL);
  3549. if (slen == 0 || slen > slen_full) {
  3550. /* shouldn't ever happen */
  3551. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3552. SSL_SESSION_free(sess);
  3553. goto err;
  3554. }
  3555. p = senc;
  3556. if (!i2d_SSL_SESSION(sess, &p)) {
  3557. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3558. SSL_SESSION_free(sess);
  3559. goto err;
  3560. }
  3561. SSL_SESSION_free(sess);
  3562. /*
  3563. * Initialize HMAC and cipher contexts. If callback present it does
  3564. * all the work otherwise use generated values from parent ctx.
  3565. */
  3566. #ifndef OPENSSL_NO_DEPRECATED_3_0
  3567. if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL)
  3568. #else
  3569. if (tctx->ext.ticket_key_evp_cb != NULL)
  3570. #endif
  3571. {
  3572. int ret = 0;
  3573. if (tctx->ext.ticket_key_evp_cb != NULL)
  3574. ret = tctx->ext.ticket_key_evp_cb(ssl, key_name, iv, ctx,
  3575. ssl_hmac_get0_EVP_MAC_CTX(hctx),
  3576. 1);
  3577. #ifndef OPENSSL_NO_DEPRECATED_3_0
  3578. else if (tctx->ext.ticket_key_cb != NULL)
  3579. /* if 0 is returned, write an empty ticket */
  3580. ret = tctx->ext.ticket_key_cb(ssl, key_name, iv, ctx,
  3581. ssl_hmac_get0_HMAC_CTX(hctx), 1);
  3582. #endif
  3583. if (ret == 0) {
  3584. /*
  3585. * In TLSv1.2 we construct a 0 length ticket. In TLSv1.3 a 0
  3586. * length ticket is not allowed so we abort construction of the
  3587. * ticket
  3588. */
  3589. if (SSL_CONNECTION_IS_TLS13(s)) {
  3590. ok = CON_FUNC_DONT_SEND;
  3591. goto err;
  3592. }
  3593. /* Put timeout and length */
  3594. if (!WPACKET_put_bytes_u32(pkt, 0)
  3595. || !WPACKET_put_bytes_u16(pkt, 0)) {
  3596. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3597. goto err;
  3598. }
  3599. OPENSSL_free(senc);
  3600. EVP_CIPHER_CTX_free(ctx);
  3601. ssl_hmac_free(hctx);
  3602. return CON_FUNC_SUCCESS;
  3603. }
  3604. if (ret < 0) {
  3605. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED);
  3606. goto err;
  3607. }
  3608. iv_len = EVP_CIPHER_CTX_get_iv_length(ctx);
  3609. if (iv_len < 0) {
  3610. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3611. goto err;
  3612. }
  3613. } else {
  3614. EVP_CIPHER *cipher = EVP_CIPHER_fetch(sctx->libctx, "AES-256-CBC",
  3615. sctx->propq);
  3616. if (cipher == NULL) {
  3617. /* Error is already recorded */
  3618. SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
  3619. goto err;
  3620. }
  3621. iv_len = EVP_CIPHER_get_iv_length(cipher);
  3622. if (iv_len < 0
  3623. || RAND_bytes_ex(sctx->libctx, iv, iv_len, 0) <= 0
  3624. || !EVP_EncryptInit_ex(ctx, cipher, NULL,
  3625. tctx->ext.secure->tick_aes_key, iv)
  3626. || !ssl_hmac_init(hctx, tctx->ext.secure->tick_hmac_key,
  3627. sizeof(tctx->ext.secure->tick_hmac_key),
  3628. "SHA256")) {
  3629. EVP_CIPHER_free(cipher);
  3630. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3631. goto err;
  3632. }
  3633. EVP_CIPHER_free(cipher);
  3634. memcpy(key_name, tctx->ext.tick_key_name,
  3635. sizeof(tctx->ext.tick_key_name));
  3636. }
  3637. if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
  3638. /* SSLfatal() already called */
  3639. goto err;
  3640. }
  3641. if (!WPACKET_get_total_written(pkt, &macoffset)
  3642. /* Output key name */
  3643. || !WPACKET_memcpy(pkt, key_name, sizeof(key_name))
  3644. /* output IV */
  3645. || !WPACKET_memcpy(pkt, iv, iv_len)
  3646. || !WPACKET_reserve_bytes(pkt, slen + EVP_MAX_BLOCK_LENGTH,
  3647. &encdata1)
  3648. /* Encrypt session data */
  3649. || !EVP_EncryptUpdate(ctx, encdata1, &len, senc, slen)
  3650. || !WPACKET_allocate_bytes(pkt, len, &encdata2)
  3651. || encdata1 != encdata2
  3652. || !EVP_EncryptFinal(ctx, encdata1 + len, &lenfinal)
  3653. || !WPACKET_allocate_bytes(pkt, lenfinal, &encdata2)
  3654. || encdata1 + len != encdata2
  3655. || len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH
  3656. || !WPACKET_get_total_written(pkt, &macendoffset)
  3657. || !ssl_hmac_update(hctx,
  3658. (unsigned char *)s->init_buf->data + macoffset,
  3659. macendoffset - macoffset)
  3660. || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1)
  3661. || !ssl_hmac_final(hctx, macdata1, &hlen, EVP_MAX_MD_SIZE)
  3662. || hlen > EVP_MAX_MD_SIZE
  3663. || !WPACKET_allocate_bytes(pkt, hlen, &macdata2)
  3664. || macdata1 != macdata2) {
  3665. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3666. goto err;
  3667. }
  3668. /* Close the sub-packet created by create_ticket_prequel() */
  3669. if (!WPACKET_close(pkt)) {
  3670. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3671. goto err;
  3672. }
  3673. ok = CON_FUNC_SUCCESS;
  3674. err:
  3675. OPENSSL_free(senc);
  3676. EVP_CIPHER_CTX_free(ctx);
  3677. ssl_hmac_free(hctx);
  3678. return ok;
  3679. }
  3680. static int construct_stateful_ticket(SSL_CONNECTION *s, WPACKET *pkt,
  3681. uint32_t age_add,
  3682. unsigned char *tick_nonce)
  3683. {
  3684. if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
  3685. /* SSLfatal() already called */
  3686. return 0;
  3687. }
  3688. if (!WPACKET_memcpy(pkt, s->session->session_id,
  3689. s->session->session_id_length)
  3690. || !WPACKET_close(pkt)) {
  3691. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3692. return 0;
  3693. }
  3694. return 1;
  3695. }
  3696. static void tls_update_ticket_counts(SSL_CONNECTION *s)
  3697. {
  3698. /*
  3699. * Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets|
  3700. * gets reset to 0 if we send more tickets following a post-handshake
  3701. * auth, but |next_ticket_nonce| does not. If we're sending extra
  3702. * tickets, decrement the count of pending extra tickets.
  3703. */
  3704. s->sent_tickets++;
  3705. s->next_ticket_nonce++;
  3706. if (s->ext.extra_tickets_expected > 0)
  3707. s->ext.extra_tickets_expected--;
  3708. }
  3709. CON_FUNC_RETURN tls_construct_new_session_ticket(SSL_CONNECTION *s, WPACKET *pkt)
  3710. {
  3711. SSL_CTX *tctx = s->session_ctx;
  3712. unsigned char tick_nonce[TICKET_NONCE_SIZE];
  3713. union {
  3714. unsigned char age_add_c[sizeof(uint32_t)];
  3715. uint32_t age_add;
  3716. } age_add_u;
  3717. CON_FUNC_RETURN ret = CON_FUNC_ERROR;
  3718. age_add_u.age_add = 0;
  3719. if (SSL_CONNECTION_IS_TLS13(s)) {
  3720. size_t i, hashlen;
  3721. uint64_t nonce;
  3722. static const unsigned char nonce_label[] = "resumption";
  3723. const EVP_MD *md = ssl_handshake_md(s);
  3724. int hashleni = EVP_MD_get_size(md);
  3725. /* Ensure cast to size_t is safe */
  3726. if (!ossl_assert(hashleni >= 0)) {
  3727. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3728. goto err;
  3729. }
  3730. hashlen = (size_t)hashleni;
  3731. /*
  3732. * If we already sent one NewSessionTicket, or we resumed then
  3733. * s->session may already be in a cache and so we must not modify it.
  3734. * Instead we need to take a copy of it and modify that.
  3735. */
  3736. if (s->sent_tickets != 0 || s->hit) {
  3737. SSL_SESSION *new_sess = ssl_session_dup(s->session, 0);
  3738. if (new_sess == NULL) {
  3739. /* SSLfatal already called */
  3740. goto err;
  3741. }
  3742. SSL_SESSION_free(s->session);
  3743. s->session = new_sess;
  3744. }
  3745. if (!ssl_generate_session_id(s, s->session)) {
  3746. /* SSLfatal() already called */
  3747. goto err;
  3748. }
  3749. if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s)->libctx,
  3750. age_add_u.age_add_c, sizeof(age_add_u), 0) <= 0) {
  3751. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3752. goto err;
  3753. }
  3754. s->session->ext.tick_age_add = age_add_u.age_add;
  3755. nonce = s->next_ticket_nonce;
  3756. for (i = TICKET_NONCE_SIZE; i > 0; i--) {
  3757. tick_nonce[i - 1] = (unsigned char)(nonce & 0xff);
  3758. nonce >>= 8;
  3759. }
  3760. if (!tls13_hkdf_expand(s, md, s->resumption_master_secret,
  3761. nonce_label,
  3762. sizeof(nonce_label) - 1,
  3763. tick_nonce,
  3764. TICKET_NONCE_SIZE,
  3765. s->session->master_key,
  3766. hashlen, 1)) {
  3767. /* SSLfatal() already called */
  3768. goto err;
  3769. }
  3770. s->session->master_key_length = hashlen;
  3771. s->session->time = ossl_time_now();
  3772. ssl_session_calculate_timeout(s->session);
  3773. if (s->s3.alpn_selected != NULL) {
  3774. OPENSSL_free(s->session->ext.alpn_selected);
  3775. s->session->ext.alpn_selected =
  3776. OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len);
  3777. if (s->session->ext.alpn_selected == NULL) {
  3778. s->session->ext.alpn_selected_len = 0;
  3779. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  3780. goto err;
  3781. }
  3782. s->session->ext.alpn_selected_len = s->s3.alpn_selected_len;
  3783. }
  3784. s->session->ext.max_early_data = s->max_early_data;
  3785. }
  3786. if (tctx->generate_ticket_cb != NULL &&
  3787. tctx->generate_ticket_cb(SSL_CONNECTION_GET_SSL(s),
  3788. tctx->ticket_cb_data) == 0) {
  3789. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3790. goto err;
  3791. }
  3792. /*
  3793. * If we are using anti-replay protection then we behave as if
  3794. * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
  3795. * is no point in using full stateless tickets.
  3796. */
  3797. if (SSL_CONNECTION_IS_TLS13(s)
  3798. && ((s->options & SSL_OP_NO_TICKET) != 0
  3799. || (s->max_early_data > 0
  3800. && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))) {
  3801. if (!construct_stateful_ticket(s, pkt, age_add_u.age_add, tick_nonce)) {
  3802. /* SSLfatal() already called */
  3803. goto err;
  3804. }
  3805. } else {
  3806. CON_FUNC_RETURN tmpret;
  3807. tmpret = construct_stateless_ticket(s, pkt, age_add_u.age_add,
  3808. tick_nonce);
  3809. if (tmpret != CON_FUNC_SUCCESS) {
  3810. if (tmpret == CON_FUNC_DONT_SEND) {
  3811. /* Non-fatal. Abort construction but continue */
  3812. ret = CON_FUNC_DONT_SEND;
  3813. /* We count this as a success so update the counts anwyay */
  3814. tls_update_ticket_counts(s);
  3815. }
  3816. /* else SSLfatal() already called */
  3817. goto err;
  3818. }
  3819. }
  3820. if (SSL_CONNECTION_IS_TLS13(s)) {
  3821. if (!tls_construct_extensions(s, pkt,
  3822. SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
  3823. NULL, 0)) {
  3824. /* SSLfatal() already called */
  3825. goto err;
  3826. }
  3827. tls_update_ticket_counts(s);
  3828. ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
  3829. }
  3830. ret = CON_FUNC_SUCCESS;
  3831. err:
  3832. return ret;
  3833. }
  3834. /*
  3835. * In TLSv1.3 this is called from the extensions code, otherwise it is used to
  3836. * create a separate message. Returns 1 on success or 0 on failure.
  3837. */
  3838. int tls_construct_cert_status_body(SSL_CONNECTION *s, WPACKET *pkt)
  3839. {
  3840. if (!WPACKET_put_bytes_u8(pkt, s->ext.status_type)
  3841. || !WPACKET_sub_memcpy_u24(pkt, s->ext.ocsp.resp,
  3842. s->ext.ocsp.resp_len)) {
  3843. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3844. return 0;
  3845. }
  3846. return 1;
  3847. }
  3848. CON_FUNC_RETURN tls_construct_cert_status(SSL_CONNECTION *s, WPACKET *pkt)
  3849. {
  3850. if (!tls_construct_cert_status_body(s, pkt)) {
  3851. /* SSLfatal() already called */
  3852. return CON_FUNC_ERROR;
  3853. }
  3854. return CON_FUNC_SUCCESS;
  3855. }
  3856. #ifndef OPENSSL_NO_NEXTPROTONEG
  3857. /*
  3858. * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
  3859. * It sets the next_proto member in s if found
  3860. */
  3861. MSG_PROCESS_RETURN tls_process_next_proto(SSL_CONNECTION *s, PACKET *pkt)
  3862. {
  3863. PACKET next_proto, padding;
  3864. size_t next_proto_len;
  3865. /*-
  3866. * The payload looks like:
  3867. * uint8 proto_len;
  3868. * uint8 proto[proto_len];
  3869. * uint8 padding_len;
  3870. * uint8 padding[padding_len];
  3871. */
  3872. if (!PACKET_get_length_prefixed_1(pkt, &next_proto)
  3873. || !PACKET_get_length_prefixed_1(pkt, &padding)
  3874. || PACKET_remaining(pkt) > 0) {
  3875. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  3876. return MSG_PROCESS_ERROR;
  3877. }
  3878. if (!PACKET_memdup(&next_proto, &s->ext.npn, &next_proto_len)) {
  3879. s->ext.npn_len = 0;
  3880. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3881. return MSG_PROCESS_ERROR;
  3882. }
  3883. s->ext.npn_len = (unsigned char)next_proto_len;
  3884. return MSG_PROCESS_CONTINUE_READING;
  3885. }
  3886. #endif
  3887. static CON_FUNC_RETURN tls_construct_encrypted_extensions(SSL_CONNECTION *s,
  3888. WPACKET *pkt)
  3889. {
  3890. if (!tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
  3891. NULL, 0)) {
  3892. /* SSLfatal() already called */
  3893. return CON_FUNC_ERROR;
  3894. }
  3895. return CON_FUNC_SUCCESS;
  3896. }
  3897. MSG_PROCESS_RETURN tls_process_end_of_early_data(SSL_CONNECTION *s, PACKET *pkt)
  3898. {
  3899. if (PACKET_remaining(pkt) != 0) {
  3900. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  3901. return MSG_PROCESS_ERROR;
  3902. }
  3903. if (s->early_data_state != SSL_EARLY_DATA_READING
  3904. && s->early_data_state != SSL_EARLY_DATA_READ_RETRY) {
  3905. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3906. return MSG_PROCESS_ERROR;
  3907. }
  3908. /*
  3909. * EndOfEarlyData signals a key change so the end of the message must be on
  3910. * a record boundary.
  3911. */
  3912. if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
  3913. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY);
  3914. return MSG_PROCESS_ERROR;
  3915. }
  3916. s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
  3917. if (!SSL_CONNECTION_GET_SSL(s)->method->ssl3_enc->change_cipher_state(s,
  3918. SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) {
  3919. /* SSLfatal() already called */
  3920. return MSG_PROCESS_ERROR;
  3921. }
  3922. return MSG_PROCESS_CONTINUE_READING;
  3923. }