statem_clnt.c 122 KB

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