statem_clnt.c 123 KB

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