statem_clnt.c 123 KB

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