statem_clnt.c 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843
  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. if (!PACKET_get_length_prefixed_1(pkt, &reqctx) ||
  2193. !PACKET_memdup(&reqctx, &s->pha_context, &s->pha_context_len)) {
  2194. SSLfatal(s, SSL_AD_DECODE_ERROR,
  2195. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2196. SSL_R_LENGTH_MISMATCH);
  2197. return MSG_PROCESS_ERROR;
  2198. }
  2199. if (!PACKET_get_length_prefixed_2(pkt, &extensions)) {
  2200. SSLfatal(s, SSL_AD_DECODE_ERROR,
  2201. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2202. SSL_R_BAD_LENGTH);
  2203. return MSG_PROCESS_ERROR;
  2204. }
  2205. if (!tls_collect_extensions(s, &extensions,
  2206. SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
  2207. &rawexts, NULL, 1)
  2208. || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
  2209. rawexts, NULL, 0, 1)) {
  2210. /* SSLfatal() already called */
  2211. OPENSSL_free(rawexts);
  2212. return MSG_PROCESS_ERROR;
  2213. }
  2214. OPENSSL_free(rawexts);
  2215. if (!tls1_process_sigalgs(s)) {
  2216. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2217. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2218. SSL_R_BAD_LENGTH);
  2219. return MSG_PROCESS_ERROR;
  2220. }
  2221. } else {
  2222. PACKET ctypes;
  2223. /* get the certificate types */
  2224. if (!PACKET_get_length_prefixed_1(pkt, &ctypes)) {
  2225. SSLfatal(s, SSL_AD_DECODE_ERROR,
  2226. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2227. SSL_R_LENGTH_MISMATCH);
  2228. return MSG_PROCESS_ERROR;
  2229. }
  2230. if (!PACKET_memdup(&ctypes, &s->s3->tmp.ctype, &s->s3->tmp.ctype_len)) {
  2231. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2232. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2233. ERR_R_INTERNAL_ERROR);
  2234. return MSG_PROCESS_ERROR;
  2235. }
  2236. if (SSL_USE_SIGALGS(s)) {
  2237. PACKET sigalgs;
  2238. if (!PACKET_get_length_prefixed_2(pkt, &sigalgs)) {
  2239. SSLfatal(s, SSL_AD_DECODE_ERROR,
  2240. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2241. SSL_R_LENGTH_MISMATCH);
  2242. return MSG_PROCESS_ERROR;
  2243. }
  2244. /*
  2245. * Despite this being for certificates, preserve compatibility
  2246. * with pre-TLS 1.3 and use the regular sigalgs field.
  2247. */
  2248. if (!tls1_save_sigalgs(s, &sigalgs, 0)) {
  2249. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2250. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2251. SSL_R_SIGNATURE_ALGORITHMS_ERROR);
  2252. return MSG_PROCESS_ERROR;
  2253. }
  2254. if (!tls1_process_sigalgs(s)) {
  2255. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2256. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2257. ERR_R_MALLOC_FAILURE);
  2258. return MSG_PROCESS_ERROR;
  2259. }
  2260. }
  2261. /* get the CA RDNs */
  2262. if (!parse_ca_names(s, pkt)) {
  2263. /* SSLfatal() already called */
  2264. return MSG_PROCESS_ERROR;
  2265. }
  2266. }
  2267. if (PACKET_remaining(pkt) != 0) {
  2268. SSLfatal(s, SSL_AD_DECODE_ERROR,
  2269. SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  2270. SSL_R_LENGTH_MISMATCH);
  2271. return MSG_PROCESS_ERROR;
  2272. }
  2273. /* we should setup a certificate to return.... */
  2274. s->s3->tmp.cert_req = 1;
  2275. /*
  2276. * In TLSv1.3 we don't prepare the client certificate yet. We wait until
  2277. * after the CertificateVerify message has been received. This is because
  2278. * in TLSv1.3 the CertificateRequest arrives before the Certificate message
  2279. * but in TLSv1.2 it is the other way around. We want to make sure that
  2280. * SSL_get_peer_certificate() returns something sensible in
  2281. * client_cert_cb.
  2282. */
  2283. if (SSL_IS_TLS13(s) && s->post_handshake_auth != SSL_PHA_REQUESTED)
  2284. return MSG_PROCESS_CONTINUE_READING;
  2285. return MSG_PROCESS_CONTINUE_PROCESSING;
  2286. }
  2287. MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
  2288. {
  2289. unsigned int ticklen;
  2290. unsigned long ticket_lifetime_hint, age_add = 0;
  2291. unsigned int sess_len;
  2292. RAW_EXTENSION *exts = NULL;
  2293. PACKET nonce;
  2294. PACKET_null_init(&nonce);
  2295. if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
  2296. || (SSL_IS_TLS13(s)
  2297. && (!PACKET_get_net_4(pkt, &age_add)
  2298. || !PACKET_get_length_prefixed_1(pkt, &nonce)))
  2299. || !PACKET_get_net_2(pkt, &ticklen)
  2300. || (SSL_IS_TLS13(s) ? (ticklen == 0 || PACKET_remaining(pkt) < ticklen)
  2301. : PACKET_remaining(pkt) != ticklen)) {
  2302. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_NEW_SESSION_TICKET,
  2303. SSL_R_LENGTH_MISMATCH);
  2304. goto err;
  2305. }
  2306. /*
  2307. * Server is allowed to change its mind (in <=TLSv1.2) and send an empty
  2308. * ticket. We already checked this TLSv1.3 case above, so it should never
  2309. * be 0 here in that instance
  2310. */
  2311. if (ticklen == 0)
  2312. return MSG_PROCESS_CONTINUE_READING;
  2313. /*
  2314. * Sessions must be immutable once they go into the session cache. Otherwise
  2315. * we can get multi-thread problems. Therefore we don't "update" sessions,
  2316. * we replace them with a duplicate. In TLSv1.3 we need to do this every
  2317. * time a NewSessionTicket arrives because those messages arrive
  2318. * post-handshake and the session may have already gone into the session
  2319. * cache.
  2320. */
  2321. if (SSL_IS_TLS13(s) || s->session->session_id_length > 0) {
  2322. SSL_SESSION *new_sess;
  2323. /*
  2324. * We reused an existing session, so we need to replace it with a new
  2325. * one
  2326. */
  2327. if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
  2328. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2329. SSL_F_TLS_PROCESS_NEW_SESSION_TICKET,
  2330. ERR_R_MALLOC_FAILURE);
  2331. goto err;
  2332. }
  2333. if ((s->session_ctx->session_cache_mode & SSL_SESS_CACHE_CLIENT) != 0
  2334. && !SSL_IS_TLS13(s)) {
  2335. /*
  2336. * In TLSv1.2 and below the arrival of a new tickets signals that
  2337. * any old ticket we were using is now out of date, so we remove the
  2338. * old session from the cache. We carry on if this fails
  2339. */
  2340. SSL_CTX_remove_session(s->session_ctx, s->session);
  2341. }
  2342. SSL_SESSION_free(s->session);
  2343. s->session = new_sess;
  2344. }
  2345. /*
  2346. * Technically the cast to long here is not guaranteed by the C standard -
  2347. * but we use it elsewhere, so this should be ok.
  2348. */
  2349. s->session->time = (long)time(NULL);
  2350. OPENSSL_free(s->session->ext.tick);
  2351. s->session->ext.tick = NULL;
  2352. s->session->ext.ticklen = 0;
  2353. s->session->ext.tick = OPENSSL_malloc(ticklen);
  2354. if (s->session->ext.tick == NULL) {
  2355. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_NEW_SESSION_TICKET,
  2356. ERR_R_MALLOC_FAILURE);
  2357. goto err;
  2358. }
  2359. if (!PACKET_copy_bytes(pkt, s->session->ext.tick, ticklen)) {
  2360. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_NEW_SESSION_TICKET,
  2361. SSL_R_LENGTH_MISMATCH);
  2362. goto err;
  2363. }
  2364. s->session->ext.tick_lifetime_hint = ticket_lifetime_hint;
  2365. s->session->ext.tick_age_add = age_add;
  2366. s->session->ext.ticklen = ticklen;
  2367. if (SSL_IS_TLS13(s)) {
  2368. PACKET extpkt;
  2369. if (!PACKET_as_length_prefixed_2(pkt, &extpkt)
  2370. || PACKET_remaining(pkt) != 0) {
  2371. SSLfatal(s, SSL_AD_DECODE_ERROR,
  2372. SSL_F_TLS_PROCESS_NEW_SESSION_TICKET,
  2373. SSL_R_LENGTH_MISMATCH);
  2374. goto err;
  2375. }
  2376. if (!tls_collect_extensions(s, &extpkt,
  2377. SSL_EXT_TLS1_3_NEW_SESSION_TICKET, &exts,
  2378. NULL, 1)
  2379. || !tls_parse_all_extensions(s,
  2380. SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
  2381. exts, NULL, 0, 1)) {
  2382. /* SSLfatal() already called */
  2383. goto err;
  2384. }
  2385. }
  2386. /*
  2387. * There are two ways to detect a resumed ticket session. One is to set
  2388. * an appropriate session ID and then the server must return a match in
  2389. * ServerHello. This allows the normal client session ID matching to work
  2390. * and we know much earlier that the ticket has been accepted. The
  2391. * other way is to set zero length session ID when the ticket is
  2392. * presented and rely on the handshake to determine session resumption.
  2393. * We choose the former approach because this fits in with assumptions
  2394. * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
  2395. * SHA256 is disabled) hash of the ticket.
  2396. */
  2397. /*
  2398. * TODO(size_t): we use sess_len here because EVP_Digest expects an int
  2399. * but s->session->session_id_length is a size_t
  2400. */
  2401. if (!EVP_Digest(s->session->ext.tick, ticklen,
  2402. s->session->session_id, &sess_len,
  2403. EVP_sha256(), NULL)) {
  2404. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_NEW_SESSION_TICKET,
  2405. ERR_R_EVP_LIB);
  2406. goto err;
  2407. }
  2408. s->session->session_id_length = sess_len;
  2409. s->session->not_resumable = 0;
  2410. /* This is a standalone message in TLSv1.3, so there is no more to read */
  2411. if (SSL_IS_TLS13(s)) {
  2412. const EVP_MD *md = ssl_handshake_md(s);
  2413. int hashleni = EVP_MD_size(md);
  2414. size_t hashlen;
  2415. static const unsigned char nonce_label[] = "resumption";
  2416. /* Ensure cast to size_t is safe */
  2417. if (!ossl_assert(hashleni >= 0)) {
  2418. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2419. SSL_F_TLS_PROCESS_NEW_SESSION_TICKET,
  2420. ERR_R_INTERNAL_ERROR);
  2421. goto err;
  2422. }
  2423. hashlen = (size_t)hashleni;
  2424. if (!tls13_hkdf_expand(s, md, s->resumption_master_secret,
  2425. nonce_label,
  2426. sizeof(nonce_label) - 1,
  2427. PACKET_data(&nonce),
  2428. PACKET_remaining(&nonce),
  2429. s->session->master_key,
  2430. hashlen, 1)) {
  2431. /* SSLfatal() already called */
  2432. goto err;
  2433. }
  2434. s->session->master_key_length = hashlen;
  2435. OPENSSL_free(exts);
  2436. ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
  2437. return MSG_PROCESS_FINISHED_READING;
  2438. }
  2439. return MSG_PROCESS_CONTINUE_READING;
  2440. err:
  2441. OPENSSL_free(exts);
  2442. return MSG_PROCESS_ERROR;
  2443. }
  2444. /*
  2445. * In TLSv1.3 this is called from the extensions code, otherwise it is used to
  2446. * parse a separate message. Returns 1 on success or 0 on failure
  2447. */
  2448. int tls_process_cert_status_body(SSL *s, PACKET *pkt)
  2449. {
  2450. size_t resplen;
  2451. unsigned int type;
  2452. if (!PACKET_get_1(pkt, &type)
  2453. || type != TLSEXT_STATUSTYPE_ocsp) {
  2454. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_STATUS_BODY,
  2455. SSL_R_UNSUPPORTED_STATUS_TYPE);
  2456. return 0;
  2457. }
  2458. if (!PACKET_get_net_3_len(pkt, &resplen)
  2459. || PACKET_remaining(pkt) != resplen) {
  2460. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_STATUS_BODY,
  2461. SSL_R_LENGTH_MISMATCH);
  2462. return 0;
  2463. }
  2464. s->ext.ocsp.resp = OPENSSL_malloc(resplen);
  2465. if (s->ext.ocsp.resp == NULL) {
  2466. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_STATUS_BODY,
  2467. ERR_R_MALLOC_FAILURE);
  2468. return 0;
  2469. }
  2470. if (!PACKET_copy_bytes(pkt, s->ext.ocsp.resp, resplen)) {
  2471. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_STATUS_BODY,
  2472. SSL_R_LENGTH_MISMATCH);
  2473. return 0;
  2474. }
  2475. s->ext.ocsp.resp_len = resplen;
  2476. return 1;
  2477. }
  2478. MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
  2479. {
  2480. if (!tls_process_cert_status_body(s, pkt)) {
  2481. /* SSLfatal() already called */
  2482. return MSG_PROCESS_ERROR;
  2483. }
  2484. return MSG_PROCESS_CONTINUE_READING;
  2485. }
  2486. /*
  2487. * Perform miscellaneous checks and processing after we have received the
  2488. * server's initial flight. In TLS1.3 this is after the Server Finished message.
  2489. * In <=TLS1.2 this is after the ServerDone message. Returns 1 on success or 0
  2490. * on failure.
  2491. */
  2492. int tls_process_initial_server_flight(SSL *s)
  2493. {
  2494. /*
  2495. * at this point we check that we have the required stuff from
  2496. * the server
  2497. */
  2498. if (!ssl3_check_cert_and_algorithm(s)) {
  2499. /* SSLfatal() already called */
  2500. return 0;
  2501. }
  2502. /*
  2503. * Call the ocsp status callback if needed. The |ext.ocsp.resp| and
  2504. * |ext.ocsp.resp_len| values will be set if we actually received a status
  2505. * message, or NULL and -1 otherwise
  2506. */
  2507. if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing
  2508. && s->ctx->ext.status_cb != NULL) {
  2509. int ret = s->ctx->ext.status_cb(s, s->ctx->ext.status_arg);
  2510. if (ret == 0) {
  2511. SSLfatal(s, SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE,
  2512. SSL_F_TLS_PROCESS_INITIAL_SERVER_FLIGHT,
  2513. SSL_R_INVALID_STATUS_RESPONSE);
  2514. return 0;
  2515. }
  2516. if (ret < 0) {
  2517. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2518. SSL_F_TLS_PROCESS_INITIAL_SERVER_FLIGHT,
  2519. ERR_R_MALLOC_FAILURE);
  2520. return 0;
  2521. }
  2522. }
  2523. #ifndef OPENSSL_NO_CT
  2524. if (s->ct_validation_callback != NULL) {
  2525. /* Note we validate the SCTs whether or not we abort on error */
  2526. if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) {
  2527. /* SSLfatal() already called */
  2528. return 0;
  2529. }
  2530. }
  2531. #endif
  2532. return 1;
  2533. }
  2534. MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
  2535. {
  2536. if (PACKET_remaining(pkt) > 0) {
  2537. /* should contain no data */
  2538. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_SERVER_DONE,
  2539. SSL_R_LENGTH_MISMATCH);
  2540. return MSG_PROCESS_ERROR;
  2541. }
  2542. #ifndef OPENSSL_NO_SRP
  2543. if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
  2544. if (SRP_Calc_A_param(s) <= 0) {
  2545. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_SERVER_DONE,
  2546. SSL_R_SRP_A_CALC);
  2547. return MSG_PROCESS_ERROR;
  2548. }
  2549. }
  2550. #endif
  2551. if (!tls_process_initial_server_flight(s)) {
  2552. /* SSLfatal() already called */
  2553. return MSG_PROCESS_ERROR;
  2554. }
  2555. return MSG_PROCESS_FINISHED_READING;
  2556. }
  2557. static int tls_construct_cke_psk_preamble(SSL *s, WPACKET *pkt)
  2558. {
  2559. #ifndef OPENSSL_NO_PSK
  2560. int ret = 0;
  2561. /*
  2562. * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
  2563. * \0-terminated identity. The last byte is for us for simulating
  2564. * strnlen.
  2565. */
  2566. char identity[PSK_MAX_IDENTITY_LEN + 1];
  2567. size_t identitylen = 0;
  2568. unsigned char psk[PSK_MAX_PSK_LEN];
  2569. unsigned char *tmppsk = NULL;
  2570. char *tmpidentity = NULL;
  2571. size_t psklen = 0;
  2572. if (s->psk_client_callback == NULL) {
  2573. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
  2574. SSL_R_PSK_NO_CLIENT_CB);
  2575. goto err;
  2576. }
  2577. memset(identity, 0, sizeof(identity));
  2578. psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
  2579. identity, sizeof(identity) - 1,
  2580. psk, sizeof(psk));
  2581. if (psklen > PSK_MAX_PSK_LEN) {
  2582. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  2583. SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
  2584. goto err;
  2585. } else if (psklen == 0) {
  2586. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  2587. SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
  2588. SSL_R_PSK_IDENTITY_NOT_FOUND);
  2589. goto err;
  2590. }
  2591. identitylen = strlen(identity);
  2592. if (identitylen > PSK_MAX_IDENTITY_LEN) {
  2593. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
  2594. ERR_R_INTERNAL_ERROR);
  2595. goto err;
  2596. }
  2597. tmppsk = OPENSSL_memdup(psk, psklen);
  2598. tmpidentity = OPENSSL_strdup(identity);
  2599. if (tmppsk == NULL || tmpidentity == NULL) {
  2600. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
  2601. ERR_R_MALLOC_FAILURE);
  2602. goto err;
  2603. }
  2604. OPENSSL_free(s->s3->tmp.psk);
  2605. s->s3->tmp.psk = tmppsk;
  2606. s->s3->tmp.psklen = psklen;
  2607. tmppsk = NULL;
  2608. OPENSSL_free(s->session->psk_identity);
  2609. s->session->psk_identity = tmpidentity;
  2610. tmpidentity = NULL;
  2611. if (!WPACKET_sub_memcpy_u16(pkt, identity, identitylen)) {
  2612. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
  2613. ERR_R_INTERNAL_ERROR);
  2614. goto err;
  2615. }
  2616. ret = 1;
  2617. err:
  2618. OPENSSL_cleanse(psk, psklen);
  2619. OPENSSL_cleanse(identity, sizeof(identity));
  2620. OPENSSL_clear_free(tmppsk, psklen);
  2621. OPENSSL_clear_free(tmpidentity, identitylen);
  2622. return ret;
  2623. #else
  2624. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
  2625. ERR_R_INTERNAL_ERROR);
  2626. return 0;
  2627. #endif
  2628. }
  2629. static int tls_construct_cke_rsa(SSL *s, WPACKET *pkt)
  2630. {
  2631. #ifndef OPENSSL_NO_RSA
  2632. unsigned char *encdata = NULL;
  2633. EVP_PKEY *pkey = NULL;
  2634. EVP_PKEY_CTX *pctx = NULL;
  2635. size_t enclen;
  2636. unsigned char *pms = NULL;
  2637. size_t pmslen = 0;
  2638. if (s->session->peer == NULL) {
  2639. /*
  2640. * We should always have a server certificate with SSL_kRSA.
  2641. */
  2642. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2643. ERR_R_INTERNAL_ERROR);
  2644. return 0;
  2645. }
  2646. pkey = X509_get0_pubkey(s->session->peer);
  2647. if (EVP_PKEY_get0_RSA(pkey) == NULL) {
  2648. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2649. ERR_R_INTERNAL_ERROR);
  2650. return 0;
  2651. }
  2652. pmslen = SSL_MAX_MASTER_KEY_LENGTH;
  2653. pms = OPENSSL_malloc(pmslen);
  2654. if (pms == NULL) {
  2655. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2656. ERR_R_MALLOC_FAILURE);
  2657. return 0;
  2658. }
  2659. pms[0] = s->client_version >> 8;
  2660. pms[1] = s->client_version & 0xff;
  2661. /* TODO(size_t): Convert this function */
  2662. if (RAND_bytes(pms + 2, (int)(pmslen - 2)) <= 0) {
  2663. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2664. ERR_R_MALLOC_FAILURE);
  2665. goto err;
  2666. }
  2667. /* Fix buf for TLS and beyond */
  2668. if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) {
  2669. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2670. ERR_R_INTERNAL_ERROR);
  2671. goto err;
  2672. }
  2673. pctx = EVP_PKEY_CTX_new(pkey, NULL);
  2674. if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
  2675. || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
  2676. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2677. ERR_R_EVP_LIB);
  2678. goto err;
  2679. }
  2680. if (!WPACKET_allocate_bytes(pkt, enclen, &encdata)
  2681. || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) {
  2682. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2683. SSL_R_BAD_RSA_ENCRYPT);
  2684. goto err;
  2685. }
  2686. EVP_PKEY_CTX_free(pctx);
  2687. pctx = NULL;
  2688. /* Fix buf for TLS and beyond */
  2689. if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) {
  2690. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2691. ERR_R_INTERNAL_ERROR);
  2692. goto err;
  2693. }
  2694. /* Log the premaster secret, if logging is enabled. */
  2695. if (!ssl_log_rsa_client_key_exchange(s, encdata, enclen, pms, pmslen)) {
  2696. /* SSLfatal() already called */
  2697. goto err;
  2698. }
  2699. s->s3->tmp.pms = pms;
  2700. s->s3->tmp.pmslen = pmslen;
  2701. return 1;
  2702. err:
  2703. OPENSSL_clear_free(pms, pmslen);
  2704. EVP_PKEY_CTX_free(pctx);
  2705. return 0;
  2706. #else
  2707. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
  2708. ERR_R_INTERNAL_ERROR);
  2709. return 0;
  2710. #endif
  2711. }
  2712. static int tls_construct_cke_dhe(SSL *s, WPACKET *pkt)
  2713. {
  2714. #ifndef OPENSSL_NO_DH
  2715. DH *dh_clnt = NULL;
  2716. const BIGNUM *pub_key;
  2717. EVP_PKEY *ckey = NULL, *skey = NULL;
  2718. unsigned char *keybytes = NULL;
  2719. skey = s->s3->peer_tmp;
  2720. if (skey == NULL) {
  2721. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_DHE,
  2722. ERR_R_INTERNAL_ERROR);
  2723. goto err;
  2724. }
  2725. ckey = ssl_generate_pkey(skey);
  2726. if (ckey == NULL) {
  2727. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_DHE,
  2728. ERR_R_INTERNAL_ERROR);
  2729. goto err;
  2730. }
  2731. dh_clnt = EVP_PKEY_get0_DH(ckey);
  2732. if (dh_clnt == NULL) {
  2733. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_DHE,
  2734. ERR_R_INTERNAL_ERROR);
  2735. goto err;
  2736. }
  2737. if (ssl_derive(s, ckey, skey, 0) == 0) {
  2738. /* SSLfatal() already called */
  2739. goto err;
  2740. }
  2741. /* send off the data */
  2742. DH_get0_key(dh_clnt, &pub_key, NULL);
  2743. if (!WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(pub_key),
  2744. &keybytes)) {
  2745. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_DHE,
  2746. ERR_R_INTERNAL_ERROR);
  2747. goto err;
  2748. }
  2749. BN_bn2bin(pub_key, keybytes);
  2750. EVP_PKEY_free(ckey);
  2751. return 1;
  2752. err:
  2753. EVP_PKEY_free(ckey);
  2754. return 0;
  2755. #else
  2756. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_DHE,
  2757. ERR_R_INTERNAL_ERROR);
  2758. return 0;
  2759. #endif
  2760. }
  2761. static int tls_construct_cke_ecdhe(SSL *s, WPACKET *pkt)
  2762. {
  2763. #ifndef OPENSSL_NO_EC
  2764. unsigned char *encodedPoint = NULL;
  2765. size_t encoded_pt_len = 0;
  2766. EVP_PKEY *ckey = NULL, *skey = NULL;
  2767. int ret = 0;
  2768. skey = s->s3->peer_tmp;
  2769. if (skey == NULL) {
  2770. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_ECDHE,
  2771. ERR_R_INTERNAL_ERROR);
  2772. return 0;
  2773. }
  2774. ckey = ssl_generate_pkey(skey);
  2775. if (ckey == NULL) {
  2776. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_ECDHE,
  2777. ERR_R_MALLOC_FAILURE);
  2778. goto err;
  2779. }
  2780. if (ssl_derive(s, ckey, skey, 0) == 0) {
  2781. /* SSLfatal() already called */
  2782. goto err;
  2783. }
  2784. /* Generate encoding of client key */
  2785. encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(ckey, &encodedPoint);
  2786. if (encoded_pt_len == 0) {
  2787. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_ECDHE,
  2788. ERR_R_EC_LIB);
  2789. goto err;
  2790. }
  2791. if (!WPACKET_sub_memcpy_u8(pkt, encodedPoint, encoded_pt_len)) {
  2792. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_ECDHE,
  2793. ERR_R_INTERNAL_ERROR);
  2794. goto err;
  2795. }
  2796. ret = 1;
  2797. err:
  2798. OPENSSL_free(encodedPoint);
  2799. EVP_PKEY_free(ckey);
  2800. return ret;
  2801. #else
  2802. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_ECDHE,
  2803. ERR_R_INTERNAL_ERROR);
  2804. return 0;
  2805. #endif
  2806. }
  2807. static int tls_construct_cke_gost(SSL *s, WPACKET *pkt)
  2808. {
  2809. #ifndef OPENSSL_NO_GOST
  2810. /* GOST key exchange message creation */
  2811. EVP_PKEY_CTX *pkey_ctx = NULL;
  2812. X509 *peer_cert;
  2813. size_t msglen;
  2814. unsigned int md_len;
  2815. unsigned char shared_ukm[32], tmp[256];
  2816. EVP_MD_CTX *ukm_hash = NULL;
  2817. int dgst_nid = NID_id_GostR3411_94;
  2818. unsigned char *pms = NULL;
  2819. size_t pmslen = 0;
  2820. if ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
  2821. dgst_nid = NID_id_GostR3411_2012_256;
  2822. /*
  2823. * Get server certificate PKEY and create ctx from it
  2824. */
  2825. peer_cert = s->session->peer;
  2826. if (!peer_cert) {
  2827. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2828. SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
  2829. return 0;
  2830. }
  2831. pkey_ctx = EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert), NULL);
  2832. if (pkey_ctx == NULL) {
  2833. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2834. ERR_R_MALLOC_FAILURE);
  2835. return 0;
  2836. }
  2837. /*
  2838. * If we have send a certificate, and certificate key
  2839. * parameters match those of server certificate, use
  2840. * certificate key for key exchange
  2841. */
  2842. /* Otherwise, generate ephemeral key pair */
  2843. pmslen = 32;
  2844. pms = OPENSSL_malloc(pmslen);
  2845. if (pms == NULL) {
  2846. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2847. ERR_R_MALLOC_FAILURE);
  2848. goto err;
  2849. }
  2850. if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0
  2851. /* Generate session key
  2852. * TODO(size_t): Convert this function
  2853. */
  2854. || RAND_bytes(pms, (int)pmslen) <= 0) {
  2855. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2856. ERR_R_INTERNAL_ERROR);
  2857. goto err;
  2858. };
  2859. /*
  2860. * Compute shared IV and store it in algorithm-specific context
  2861. * data
  2862. */
  2863. ukm_hash = EVP_MD_CTX_new();
  2864. if (ukm_hash == NULL
  2865. || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0
  2866. || EVP_DigestUpdate(ukm_hash, s->s3->client_random,
  2867. SSL3_RANDOM_SIZE) <= 0
  2868. || EVP_DigestUpdate(ukm_hash, s->s3->server_random,
  2869. SSL3_RANDOM_SIZE) <= 0
  2870. || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
  2871. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2872. ERR_R_INTERNAL_ERROR);
  2873. goto err;
  2874. }
  2875. EVP_MD_CTX_free(ukm_hash);
  2876. ukm_hash = NULL;
  2877. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
  2878. EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) {
  2879. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2880. SSL_R_LIBRARY_BUG);
  2881. goto err;
  2882. }
  2883. /* Make GOST keytransport blob message */
  2884. /*
  2885. * Encapsulate it into sequence
  2886. */
  2887. msglen = 255;
  2888. if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
  2889. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2890. SSL_R_LIBRARY_BUG);
  2891. goto err;
  2892. }
  2893. if (!WPACKET_put_bytes_u8(pkt, V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)
  2894. || (msglen >= 0x80 && !WPACKET_put_bytes_u8(pkt, 0x81))
  2895. || !WPACKET_sub_memcpy_u8(pkt, tmp, msglen)) {
  2896. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2897. ERR_R_INTERNAL_ERROR);
  2898. goto err;
  2899. }
  2900. EVP_PKEY_CTX_free(pkey_ctx);
  2901. s->s3->tmp.pms = pms;
  2902. s->s3->tmp.pmslen = pmslen;
  2903. return 1;
  2904. err:
  2905. EVP_PKEY_CTX_free(pkey_ctx);
  2906. OPENSSL_clear_free(pms, pmslen);
  2907. EVP_MD_CTX_free(ukm_hash);
  2908. return 0;
  2909. #else
  2910. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2911. ERR_R_INTERNAL_ERROR);
  2912. return 0;
  2913. #endif
  2914. }
  2915. static int tls_construct_cke_srp(SSL *s, WPACKET *pkt)
  2916. {
  2917. #ifndef OPENSSL_NO_SRP
  2918. unsigned char *abytes = NULL;
  2919. if (s->srp_ctx.A == NULL
  2920. || !WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(s->srp_ctx.A),
  2921. &abytes)) {
  2922. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_SRP,
  2923. ERR_R_INTERNAL_ERROR);
  2924. return 0;
  2925. }
  2926. BN_bn2bin(s->srp_ctx.A, abytes);
  2927. OPENSSL_free(s->session->srp_username);
  2928. s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
  2929. if (s->session->srp_username == NULL) {
  2930. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_SRP,
  2931. ERR_R_MALLOC_FAILURE);
  2932. return 0;
  2933. }
  2934. return 1;
  2935. #else
  2936. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_SRP,
  2937. ERR_R_INTERNAL_ERROR);
  2938. return 0;
  2939. #endif
  2940. }
  2941. int tls_construct_client_key_exchange(SSL *s, WPACKET *pkt)
  2942. {
  2943. unsigned long alg_k;
  2944. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  2945. /*
  2946. * All of the construct functions below call SSLfatal() if necessary so
  2947. * no need to do so here.
  2948. */
  2949. if ((alg_k & SSL_PSK)
  2950. && !tls_construct_cke_psk_preamble(s, pkt))
  2951. goto err;
  2952. if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
  2953. if (!tls_construct_cke_rsa(s, pkt))
  2954. goto err;
  2955. } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
  2956. if (!tls_construct_cke_dhe(s, pkt))
  2957. goto err;
  2958. } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
  2959. if (!tls_construct_cke_ecdhe(s, pkt))
  2960. goto err;
  2961. } else if (alg_k & SSL_kGOST) {
  2962. if (!tls_construct_cke_gost(s, pkt))
  2963. goto err;
  2964. } else if (alg_k & SSL_kSRP) {
  2965. if (!tls_construct_cke_srp(s, pkt))
  2966. goto err;
  2967. } else if (!(alg_k & SSL_kPSK)) {
  2968. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2969. SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  2970. goto err;
  2971. }
  2972. return 1;
  2973. err:
  2974. OPENSSL_clear_free(s->s3->tmp.pms, s->s3->tmp.pmslen);
  2975. s->s3->tmp.pms = NULL;
  2976. #ifndef OPENSSL_NO_PSK
  2977. OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
  2978. s->s3->tmp.psk = NULL;
  2979. #endif
  2980. return 0;
  2981. }
  2982. int tls_client_key_exchange_post_work(SSL *s)
  2983. {
  2984. unsigned char *pms = NULL;
  2985. size_t pmslen = 0;
  2986. pms = s->s3->tmp.pms;
  2987. pmslen = s->s3->tmp.pmslen;
  2988. #ifndef OPENSSL_NO_SRP
  2989. /* Check for SRP */
  2990. if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
  2991. if (!srp_generate_client_master_secret(s)) {
  2992. /* SSLfatal() already called */
  2993. goto err;
  2994. }
  2995. return 1;
  2996. }
  2997. #endif
  2998. if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
  2999. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3000. SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
  3001. goto err;
  3002. }
  3003. if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
  3004. /* SSLfatal() already called */
  3005. /* ssl_generate_master_secret frees the pms even on error */
  3006. pms = NULL;
  3007. pmslen = 0;
  3008. goto err;
  3009. }
  3010. pms = NULL;
  3011. pmslen = 0;
  3012. #ifndef OPENSSL_NO_SCTP
  3013. if (SSL_IS_DTLS(s)) {
  3014. unsigned char sctpauthkey[64];
  3015. char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
  3016. size_t labellen;
  3017. /*
  3018. * Add new shared key for SCTP-Auth, will be ignored if no SCTP
  3019. * used.
  3020. */
  3021. memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
  3022. sizeof(DTLS1_SCTP_AUTH_LABEL));
  3023. /* Don't include the terminating zero. */
  3024. labellen = sizeof(labelbuffer) - 1;
  3025. if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
  3026. labellen += 1;
  3027. if (SSL_export_keying_material(s, sctpauthkey,
  3028. sizeof(sctpauthkey), labelbuffer,
  3029. labellen, NULL, 0, 0) <= 0) {
  3030. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3031. SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
  3032. ERR_R_INTERNAL_ERROR);
  3033. goto err;
  3034. }
  3035. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  3036. sizeof(sctpauthkey), sctpauthkey);
  3037. }
  3038. #endif
  3039. return 1;
  3040. err:
  3041. OPENSSL_clear_free(pms, pmslen);
  3042. s->s3->tmp.pms = NULL;
  3043. return 0;
  3044. }
  3045. /*
  3046. * Check a certificate can be used for client authentication. Currently check
  3047. * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
  3048. * certificates can be used and optionally checks suitability for Suite B.
  3049. */
  3050. static int ssl3_check_client_certificate(SSL *s)
  3051. {
  3052. /* If no suitable signature algorithm can't use certificate */
  3053. if (!tls_choose_sigalg(s, 0) || s->s3->tmp.sigalg == NULL)
  3054. return 0;
  3055. /*
  3056. * If strict mode check suitability of chain before using it. This also
  3057. * adjusts suite B digest if necessary.
  3058. */
  3059. if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
  3060. !tls1_check_chain(s, NULL, NULL, NULL, -2))
  3061. return 0;
  3062. return 1;
  3063. }
  3064. WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst)
  3065. {
  3066. X509 *x509 = NULL;
  3067. EVP_PKEY *pkey = NULL;
  3068. int i;
  3069. if (wst == WORK_MORE_A) {
  3070. /* Let cert callback update client certificates if required */
  3071. if (s->cert->cert_cb) {
  3072. i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
  3073. if (i < 0) {
  3074. s->rwstate = SSL_X509_LOOKUP;
  3075. return WORK_MORE_A;
  3076. }
  3077. if (i == 0) {
  3078. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3079. SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
  3080. SSL_R_CALLBACK_FAILED);
  3081. return WORK_ERROR;
  3082. }
  3083. s->rwstate = SSL_NOTHING;
  3084. }
  3085. if (ssl3_check_client_certificate(s)) {
  3086. if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
  3087. return WORK_FINISHED_STOP;
  3088. }
  3089. return WORK_FINISHED_CONTINUE;
  3090. }
  3091. /* Fall through to WORK_MORE_B */
  3092. wst = WORK_MORE_B;
  3093. }
  3094. /* We need to get a client cert */
  3095. if (wst == WORK_MORE_B) {
  3096. /*
  3097. * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
  3098. * return(-1); We then get retied later
  3099. */
  3100. i = ssl_do_client_cert_cb(s, &x509, &pkey);
  3101. if (i < 0) {
  3102. s->rwstate = SSL_X509_LOOKUP;
  3103. return WORK_MORE_B;
  3104. }
  3105. s->rwstate = SSL_NOTHING;
  3106. if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
  3107. if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
  3108. i = 0;
  3109. } else if (i == 1) {
  3110. i = 0;
  3111. SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
  3112. SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  3113. }
  3114. X509_free(x509);
  3115. EVP_PKEY_free(pkey);
  3116. if (i && !ssl3_check_client_certificate(s))
  3117. i = 0;
  3118. if (i == 0) {
  3119. if (s->version == SSL3_VERSION) {
  3120. s->s3->tmp.cert_req = 0;
  3121. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
  3122. return WORK_FINISHED_CONTINUE;
  3123. } else {
  3124. s->s3->tmp.cert_req = 2;
  3125. if (!ssl3_digest_cached_records(s, 0)) {
  3126. /* SSLfatal() already called */
  3127. return WORK_ERROR;
  3128. }
  3129. }
  3130. }
  3131. if (s->post_handshake_auth == SSL_PHA_REQUESTED)
  3132. return WORK_FINISHED_STOP;
  3133. return WORK_FINISHED_CONTINUE;
  3134. }
  3135. /* Shouldn't ever get here */
  3136. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
  3137. ERR_R_INTERNAL_ERROR);
  3138. return WORK_ERROR;
  3139. }
  3140. int tls_construct_client_certificate(SSL *s, WPACKET *pkt)
  3141. {
  3142. if (SSL_IS_TLS13(s)) {
  3143. if (s->pha_context == NULL) {
  3144. /* no context available, add 0-length context */
  3145. if (!WPACKET_put_bytes_u8(pkt, 0)) {
  3146. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3147. SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
  3148. return 0;
  3149. }
  3150. } else if (!WPACKET_sub_memcpy_u8(pkt, s->pha_context, s->pha_context_len)) {
  3151. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3152. SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
  3153. return 0;
  3154. }
  3155. }
  3156. if (!ssl3_output_cert_chain(s, pkt,
  3157. (s->s3->tmp.cert_req == 2) ? NULL
  3158. : s->cert->key)) {
  3159. /* SSLfatal() already called */
  3160. return 0;
  3161. }
  3162. if (SSL_IS_TLS13(s)
  3163. && SSL_IS_FIRST_HANDSHAKE(s)
  3164. && (!s->method->ssl3_enc->change_cipher_state(s,
  3165. SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) {
  3166. /*
  3167. * This is a fatal error, which leaves enc_write_ctx in an inconsistent
  3168. * state and thus ssl3_send_alert may crash.
  3169. */
  3170. SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE,
  3171. SSL_R_CANNOT_CHANGE_CIPHER);
  3172. return 0;
  3173. }
  3174. return 1;
  3175. }
  3176. int ssl3_check_cert_and_algorithm(SSL *s)
  3177. {
  3178. const SSL_CERT_LOOKUP *clu;
  3179. size_t idx;
  3180. long alg_k, alg_a;
  3181. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  3182. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  3183. /* we don't have a certificate */
  3184. if (!(alg_a & SSL_aCERT))
  3185. return 1;
  3186. /* This is the passed certificate */
  3187. clu = ssl_cert_lookup_by_pkey(X509_get0_pubkey(s->session->peer), &idx);
  3188. /* Check certificate is recognised and suitable for cipher */
  3189. if (clu == NULL || (alg_a & clu->amask) == 0) {
  3190. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3191. SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3192. SSL_R_MISSING_SIGNING_CERT);
  3193. return 0;
  3194. }
  3195. #ifndef OPENSSL_NO_EC
  3196. if (clu->amask & SSL_aECDSA) {
  3197. if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s))
  3198. return 1;
  3199. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3200. SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
  3201. return 0;
  3202. }
  3203. #endif
  3204. #ifndef OPENSSL_NO_RSA
  3205. if (alg_k & (SSL_kRSA | SSL_kRSAPSK) && idx != SSL_PKEY_RSA) {
  3206. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3207. SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3208. SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  3209. return 0;
  3210. }
  3211. #endif
  3212. #ifndef OPENSSL_NO_DH
  3213. if ((alg_k & SSL_kDHE) && (s->s3->peer_tmp == NULL)) {
  3214. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3215. ERR_R_INTERNAL_ERROR);
  3216. return 0;
  3217. }
  3218. #endif
  3219. return 1;
  3220. }
  3221. #ifndef OPENSSL_NO_NEXTPROTONEG
  3222. int tls_construct_next_proto(SSL *s, WPACKET *pkt)
  3223. {
  3224. size_t len, padding_len;
  3225. unsigned char *padding = NULL;
  3226. len = s->ext.npn_len;
  3227. padding_len = 32 - ((len + 2) % 32);
  3228. if (!WPACKET_sub_memcpy_u8(pkt, s->ext.npn, len)
  3229. || !WPACKET_sub_allocate_bytes_u8(pkt, padding_len, &padding)) {
  3230. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_NEXT_PROTO,
  3231. ERR_R_INTERNAL_ERROR);
  3232. return 0;
  3233. }
  3234. memset(padding, 0, padding_len);
  3235. return 1;
  3236. }
  3237. #endif
  3238. MSG_PROCESS_RETURN tls_process_hello_req(SSL *s, PACKET *pkt)
  3239. {
  3240. if (PACKET_remaining(pkt) > 0) {
  3241. /* should contain no data */
  3242. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_HELLO_REQ,
  3243. SSL_R_LENGTH_MISMATCH);
  3244. return MSG_PROCESS_ERROR;
  3245. }
  3246. if ((s->options & SSL_OP_NO_RENEGOTIATION)) {
  3247. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
  3248. return MSG_PROCESS_FINISHED_READING;
  3249. }
  3250. /*
  3251. * This is a historical discrepancy (not in the RFC) maintained for
  3252. * compatibility reasons. If a TLS client receives a HelloRequest it will
  3253. * attempt an abbreviated handshake. However if a DTLS client receives a
  3254. * HelloRequest it will do a full handshake. Either behaviour is reasonable
  3255. * but doing one for TLS and another for DTLS is odd.
  3256. */
  3257. if (SSL_IS_DTLS(s))
  3258. SSL_renegotiate(s);
  3259. else
  3260. SSL_renegotiate_abbreviated(s);
  3261. return MSG_PROCESS_FINISHED_READING;
  3262. }
  3263. static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL *s, PACKET *pkt)
  3264. {
  3265. PACKET extensions;
  3266. RAW_EXTENSION *rawexts = NULL;
  3267. if (!PACKET_as_length_prefixed_2(pkt, &extensions)
  3268. || PACKET_remaining(pkt) != 0) {
  3269. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_ENCRYPTED_EXTENSIONS,
  3270. SSL_R_LENGTH_MISMATCH);
  3271. goto err;
  3272. }
  3273. if (!tls_collect_extensions(s, &extensions,
  3274. SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, &rawexts,
  3275. NULL, 1)
  3276. || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
  3277. rawexts, NULL, 0, 1)) {
  3278. /* SSLfatal() already called */
  3279. goto err;
  3280. }
  3281. OPENSSL_free(rawexts);
  3282. return MSG_PROCESS_CONTINUE_READING;
  3283. err:
  3284. OPENSSL_free(rawexts);
  3285. return MSG_PROCESS_ERROR;
  3286. }
  3287. int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
  3288. {
  3289. int i = 0;
  3290. #ifndef OPENSSL_NO_ENGINE
  3291. if (s->ctx->client_cert_engine) {
  3292. i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
  3293. SSL_get_client_CA_list(s),
  3294. px509, ppkey, NULL, NULL, NULL);
  3295. if (i != 0)
  3296. return i;
  3297. }
  3298. #endif
  3299. if (s->ctx->client_cert_cb)
  3300. i = s->ctx->client_cert_cb(s, px509, ppkey);
  3301. return i;
  3302. }
  3303. int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, WPACKET *pkt)
  3304. {
  3305. int i;
  3306. size_t totlen = 0, len, maxlen, maxverok = 0;
  3307. int empty_reneg_info_scsv = !s->renegotiate;
  3308. /* Set disabled masks for this session */
  3309. if (!ssl_set_client_disabled(s)) {
  3310. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_CIPHER_LIST_TO_BYTES,
  3311. SSL_R_NO_PROTOCOLS_AVAILABLE);
  3312. return 0;
  3313. }
  3314. if (sk == NULL) {
  3315. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_CIPHER_LIST_TO_BYTES,
  3316. ERR_R_INTERNAL_ERROR);
  3317. return 0;
  3318. }
  3319. #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
  3320. # if OPENSSL_MAX_TLS1_2_CIPHER_LENGTH < 6
  3321. # error Max cipher length too short
  3322. # endif
  3323. /*
  3324. * Some servers hang if client hello > 256 bytes as hack workaround
  3325. * chop number of supported ciphers to keep it well below this if we
  3326. * use TLS v1.2
  3327. */
  3328. if (TLS1_get_version(s) >= TLS1_2_VERSION)
  3329. maxlen = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
  3330. else
  3331. #endif
  3332. /* Maximum length that can be stored in 2 bytes. Length must be even */
  3333. maxlen = 0xfffe;
  3334. if (empty_reneg_info_scsv)
  3335. maxlen -= 2;
  3336. if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV)
  3337. maxlen -= 2;
  3338. for (i = 0; i < sk_SSL_CIPHER_num(sk) && totlen < maxlen; i++) {
  3339. const SSL_CIPHER *c;
  3340. c = sk_SSL_CIPHER_value(sk, i);
  3341. /* Skip disabled ciphers */
  3342. if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0))
  3343. continue;
  3344. if (!s->method->put_cipher_by_char(c, pkt, &len)) {
  3345. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_CIPHER_LIST_TO_BYTES,
  3346. ERR_R_INTERNAL_ERROR);
  3347. return 0;
  3348. }
  3349. /* Sanity check that the maximum version we offer has ciphers enabled */
  3350. if (!maxverok) {
  3351. if (SSL_IS_DTLS(s)) {
  3352. if (DTLS_VERSION_GE(c->max_dtls, s->s3->tmp.max_ver)
  3353. && DTLS_VERSION_LE(c->min_dtls, s->s3->tmp.max_ver))
  3354. maxverok = 1;
  3355. } else {
  3356. if (c->max_tls >= s->s3->tmp.max_ver
  3357. && c->min_tls <= s->s3->tmp.max_ver)
  3358. maxverok = 1;
  3359. }
  3360. }
  3361. totlen += len;
  3362. }
  3363. if (totlen == 0 || !maxverok) {
  3364. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_CIPHER_LIST_TO_BYTES,
  3365. SSL_R_NO_CIPHERS_AVAILABLE);
  3366. if (!maxverok)
  3367. ERR_add_error_data(1, "No ciphers enabled for max supported "
  3368. "SSL/TLS version");
  3369. return 0;
  3370. }
  3371. if (totlen != 0) {
  3372. if (empty_reneg_info_scsv) {
  3373. static SSL_CIPHER scsv = {
  3374. 0, NULL, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
  3375. };
  3376. if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) {
  3377. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3378. SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR);
  3379. return 0;
  3380. }
  3381. }
  3382. if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
  3383. static SSL_CIPHER scsv = {
  3384. 0, NULL, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
  3385. };
  3386. if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) {
  3387. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3388. SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR);
  3389. return 0;
  3390. }
  3391. }
  3392. }
  3393. return 1;
  3394. }
  3395. int tls_construct_end_of_early_data(SSL *s, WPACKET *pkt)
  3396. {
  3397. if (s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY
  3398. && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING) {
  3399. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  3400. SSL_F_TLS_CONSTRUCT_END_OF_EARLY_DATA,
  3401. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  3402. return 0;
  3403. }
  3404. s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;
  3405. return 1;
  3406. }