statem_srvr.c 132 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106
  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 "../ssl_local.h"
  13. #include "statem_local.h"
  14. #include "internal/constant_time.h"
  15. #include "internal/cryptlib.h"
  16. #include <openssl/buffer.h>
  17. #include <openssl/rand.h>
  18. #include <openssl/objects.h>
  19. #include <openssl/evp.h>
  20. #include <openssl/x509.h>
  21. #include <openssl/dh.h>
  22. #include <openssl/rsa.h>
  23. #include <openssl/bn.h>
  24. #include <openssl/md5.h>
  25. #include <openssl/trace.h>
  26. #include <openssl/core_names.h>
  27. #include <openssl/asn1t.h>
  28. #define TICKET_NONCE_SIZE 8
  29. typedef struct {
  30. ASN1_TYPE *kxBlob;
  31. ASN1_TYPE *opaqueBlob;
  32. } GOST_KX_MESSAGE;
  33. DECLARE_ASN1_FUNCTIONS(GOST_KX_MESSAGE)
  34. ASN1_SEQUENCE(GOST_KX_MESSAGE) = {
  35. ASN1_SIMPLE(GOST_KX_MESSAGE, kxBlob, ASN1_ANY),
  36. ASN1_OPT(GOST_KX_MESSAGE, opaqueBlob, ASN1_ANY),
  37. } ASN1_SEQUENCE_END(GOST_KX_MESSAGE)
  38. IMPLEMENT_ASN1_FUNCTIONS(GOST_KX_MESSAGE)
  39. static int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt);
  40. /*
  41. * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
  42. * handshake state transitions when a TLSv1.3 server is reading messages from
  43. * the client. The message type that the client has sent is provided in |mt|.
  44. * The current state is in |s->statem.hand_state|.
  45. *
  46. * Return values are 1 for success (transition allowed) and 0 on error
  47. * (transition not allowed)
  48. */
  49. static int ossl_statem_server13_read_transition(SSL *s, int mt)
  50. {
  51. OSSL_STATEM *st = &s->statem;
  52. /*
  53. * Note: There is no case for TLS_ST_BEFORE because at that stage we have
  54. * not negotiated TLSv1.3 yet, so that case is handled by
  55. * ossl_statem_server_read_transition()
  56. */
  57. switch (st->hand_state) {
  58. default:
  59. break;
  60. case TLS_ST_EARLY_DATA:
  61. if (s->hello_retry_request == SSL_HRR_PENDING) {
  62. if (mt == SSL3_MT_CLIENT_HELLO) {
  63. st->hand_state = TLS_ST_SR_CLNT_HELLO;
  64. return 1;
  65. }
  66. break;
  67. } else if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
  68. if (mt == SSL3_MT_END_OF_EARLY_DATA) {
  69. st->hand_state = TLS_ST_SR_END_OF_EARLY_DATA;
  70. return 1;
  71. }
  72. break;
  73. }
  74. /* Fall through */
  75. case TLS_ST_SR_END_OF_EARLY_DATA:
  76. case TLS_ST_SW_FINISHED:
  77. if (s->s3.tmp.cert_request) {
  78. if (mt == SSL3_MT_CERTIFICATE) {
  79. st->hand_state = TLS_ST_SR_CERT;
  80. return 1;
  81. }
  82. } else {
  83. if (mt == SSL3_MT_FINISHED) {
  84. st->hand_state = TLS_ST_SR_FINISHED;
  85. return 1;
  86. }
  87. }
  88. break;
  89. case TLS_ST_SR_CERT:
  90. if (s->session->peer == NULL) {
  91. if (mt == SSL3_MT_FINISHED) {
  92. st->hand_state = TLS_ST_SR_FINISHED;
  93. return 1;
  94. }
  95. } else {
  96. if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
  97. st->hand_state = TLS_ST_SR_CERT_VRFY;
  98. return 1;
  99. }
  100. }
  101. break;
  102. case TLS_ST_SR_CERT_VRFY:
  103. if (mt == SSL3_MT_FINISHED) {
  104. st->hand_state = TLS_ST_SR_FINISHED;
  105. return 1;
  106. }
  107. break;
  108. case TLS_ST_OK:
  109. /*
  110. * Its never ok to start processing handshake messages in the middle of
  111. * early data (i.e. before we've received the end of early data alert)
  112. */
  113. if (s->early_data_state == SSL_EARLY_DATA_READING)
  114. break;
  115. if (mt == SSL3_MT_CERTIFICATE
  116. && s->post_handshake_auth == SSL_PHA_REQUESTED) {
  117. st->hand_state = TLS_ST_SR_CERT;
  118. return 1;
  119. }
  120. if (mt == SSL3_MT_KEY_UPDATE) {
  121. st->hand_state = TLS_ST_SR_KEY_UPDATE;
  122. return 1;
  123. }
  124. break;
  125. }
  126. /* No valid transition found */
  127. return 0;
  128. }
  129. /*
  130. * ossl_statem_server_read_transition() encapsulates the logic for the allowed
  131. * handshake state transitions when the server is reading messages from the
  132. * client. The message type that the client has sent is provided in |mt|. The
  133. * current state is in |s->statem.hand_state|.
  134. *
  135. * Return values are 1 for success (transition allowed) and 0 on error
  136. * (transition not allowed)
  137. */
  138. int ossl_statem_server_read_transition(SSL *s, int mt)
  139. {
  140. OSSL_STATEM *st = &s->statem;
  141. if (SSL_IS_TLS13(s)) {
  142. if (!ossl_statem_server13_read_transition(s, mt))
  143. goto err;
  144. return 1;
  145. }
  146. switch (st->hand_state) {
  147. default:
  148. break;
  149. case TLS_ST_BEFORE:
  150. case TLS_ST_OK:
  151. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  152. if (mt == SSL3_MT_CLIENT_HELLO) {
  153. st->hand_state = TLS_ST_SR_CLNT_HELLO;
  154. return 1;
  155. }
  156. break;
  157. case TLS_ST_SW_SRVR_DONE:
  158. /*
  159. * If we get a CKE message after a ServerDone then either
  160. * 1) We didn't request a Certificate
  161. * OR
  162. * 2) If we did request one then
  163. * a) We allow no Certificate to be returned
  164. * AND
  165. * b) We are running SSL3 (in TLS1.0+ the client must return a 0
  166. * list if we requested a certificate)
  167. */
  168. if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
  169. if (s->s3.tmp.cert_request) {
  170. if (s->version == SSL3_VERSION) {
  171. if ((s->verify_mode & SSL_VERIFY_PEER)
  172. && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
  173. /*
  174. * This isn't an unexpected message as such - we're just
  175. * not going to accept it because we require a client
  176. * cert.
  177. */
  178. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  179. SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  180. return 0;
  181. }
  182. st->hand_state = TLS_ST_SR_KEY_EXCH;
  183. return 1;
  184. }
  185. } else {
  186. st->hand_state = TLS_ST_SR_KEY_EXCH;
  187. return 1;
  188. }
  189. } else if (s->s3.tmp.cert_request) {
  190. if (mt == SSL3_MT_CERTIFICATE) {
  191. st->hand_state = TLS_ST_SR_CERT;
  192. return 1;
  193. }
  194. }
  195. break;
  196. case TLS_ST_SR_CERT:
  197. if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
  198. st->hand_state = TLS_ST_SR_KEY_EXCH;
  199. return 1;
  200. }
  201. break;
  202. case TLS_ST_SR_KEY_EXCH:
  203. /*
  204. * We should only process a CertificateVerify message if we have
  205. * received a Certificate from the client. If so then |s->session->peer|
  206. * will be non NULL. In some instances a CertificateVerify message is
  207. * not required even if the peer has sent a Certificate (e.g. such as in
  208. * the case of static DH). In that case |st->no_cert_verify| should be
  209. * set.
  210. */
  211. if (s->session->peer == NULL || st->no_cert_verify) {
  212. if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  213. /*
  214. * For the ECDH ciphersuites when the client sends its ECDH
  215. * pub key in a certificate, the CertificateVerify message is
  216. * not sent. Also for GOST ciphersuites when the client uses
  217. * its key from the certificate for key exchange.
  218. */
  219. st->hand_state = TLS_ST_SR_CHANGE;
  220. return 1;
  221. }
  222. } else {
  223. if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
  224. st->hand_state = TLS_ST_SR_CERT_VRFY;
  225. return 1;
  226. }
  227. }
  228. break;
  229. case TLS_ST_SR_CERT_VRFY:
  230. if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  231. st->hand_state = TLS_ST_SR_CHANGE;
  232. return 1;
  233. }
  234. break;
  235. case TLS_ST_SR_CHANGE:
  236. #ifndef OPENSSL_NO_NEXTPROTONEG
  237. if (s->s3.npn_seen) {
  238. if (mt == SSL3_MT_NEXT_PROTO) {
  239. st->hand_state = TLS_ST_SR_NEXT_PROTO;
  240. return 1;
  241. }
  242. } else {
  243. #endif
  244. if (mt == SSL3_MT_FINISHED) {
  245. st->hand_state = TLS_ST_SR_FINISHED;
  246. return 1;
  247. }
  248. #ifndef OPENSSL_NO_NEXTPROTONEG
  249. }
  250. #endif
  251. break;
  252. #ifndef OPENSSL_NO_NEXTPROTONEG
  253. case TLS_ST_SR_NEXT_PROTO:
  254. if (mt == SSL3_MT_FINISHED) {
  255. st->hand_state = TLS_ST_SR_FINISHED;
  256. return 1;
  257. }
  258. break;
  259. #endif
  260. case TLS_ST_SW_FINISHED:
  261. if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  262. st->hand_state = TLS_ST_SR_CHANGE;
  263. return 1;
  264. }
  265. break;
  266. }
  267. err:
  268. /* No valid transition found */
  269. if (SSL_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  270. BIO *rbio;
  271. /*
  272. * CCS messages don't have a message sequence number so this is probably
  273. * because of an out-of-order CCS. We'll just drop it.
  274. */
  275. s->init_num = 0;
  276. s->rwstate = SSL_READING;
  277. rbio = SSL_get_rbio(s);
  278. BIO_clear_retry_flags(rbio);
  279. BIO_set_retry_read(rbio);
  280. return 0;
  281. }
  282. SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
  283. return 0;
  284. }
  285. /*
  286. * Should we send a ServerKeyExchange message?
  287. *
  288. * Valid return values are:
  289. * 1: Yes
  290. * 0: No
  291. */
  292. static int send_server_key_exchange(SSL *s)
  293. {
  294. unsigned long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
  295. /*
  296. * only send a ServerKeyExchange if DH or fortezza but we have a
  297. * sign only certificate PSK: may send PSK identity hints For
  298. * ECC ciphersuites, we send a serverKeyExchange message only if
  299. * the cipher suite is either ECDH-anon or ECDHE. In other cases,
  300. * the server certificate contains the server's public key for
  301. * key exchange.
  302. */
  303. if (alg_k & (SSL_kDHE | SSL_kECDHE)
  304. /*
  305. * PSK: send ServerKeyExchange if PSK identity hint if
  306. * provided
  307. */
  308. #ifndef OPENSSL_NO_PSK
  309. /* Only send SKE if we have identity hint for plain PSK */
  310. || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
  311. && s->cert->psk_identity_hint)
  312. /* For other PSK always send SKE */
  313. || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
  314. #endif
  315. #ifndef OPENSSL_NO_SRP
  316. /* SRP: send ServerKeyExchange */
  317. || (alg_k & SSL_kSRP)
  318. #endif
  319. ) {
  320. return 1;
  321. }
  322. return 0;
  323. }
  324. /*
  325. * Should we send a CertificateRequest message?
  326. *
  327. * Valid return values are:
  328. * 1: Yes
  329. * 0: No
  330. */
  331. int send_certificate_request(SSL *s)
  332. {
  333. if (
  334. /* don't request cert unless asked for it: */
  335. s->verify_mode & SSL_VERIFY_PEER
  336. /*
  337. * don't request if post-handshake-only unless doing
  338. * post-handshake in TLSv1.3:
  339. */
  340. && (!SSL_IS_TLS13(s) || !(s->verify_mode & SSL_VERIFY_POST_HANDSHAKE)
  341. || s->post_handshake_auth == SSL_PHA_REQUEST_PENDING)
  342. /*
  343. * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
  344. * a second time:
  345. */
  346. && (s->certreqs_sent < 1 ||
  347. !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))
  348. /*
  349. * never request cert in anonymous ciphersuites (see
  350. * section "Certificate request" in SSL 3 drafts and in
  351. * RFC 2246):
  352. */
  353. && (!(s->s3.tmp.new_cipher->algorithm_auth & SSL_aNULL)
  354. /*
  355. * ... except when the application insists on
  356. * verification (against the specs, but statem_clnt.c accepts
  357. * this for SSL 3)
  358. */
  359. || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  360. /* don't request certificate for SRP auth */
  361. && !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aSRP)
  362. /*
  363. * With normal PSK Certificates and Certificate Requests
  364. * are omitted
  365. */
  366. && !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aPSK)) {
  367. return 1;
  368. }
  369. return 0;
  370. }
  371. /*
  372. * ossl_statem_server13_write_transition() works out what handshake state to
  373. * move to next when a TLSv1.3 server is writing messages to be sent to the
  374. * client.
  375. */
  376. static WRITE_TRAN ossl_statem_server13_write_transition(SSL *s)
  377. {
  378. OSSL_STATEM *st = &s->statem;
  379. /*
  380. * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
  381. * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
  382. */
  383. switch (st->hand_state) {
  384. default:
  385. /* Shouldn't happen */
  386. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  387. return WRITE_TRAN_ERROR;
  388. case TLS_ST_OK:
  389. if (s->key_update != SSL_KEY_UPDATE_NONE) {
  390. st->hand_state = TLS_ST_SW_KEY_UPDATE;
  391. return WRITE_TRAN_CONTINUE;
  392. }
  393. if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
  394. st->hand_state = TLS_ST_SW_CERT_REQ;
  395. return WRITE_TRAN_CONTINUE;
  396. }
  397. if (s->ext.extra_tickets_expected > 0) {
  398. st->hand_state = TLS_ST_SW_SESSION_TICKET;
  399. return WRITE_TRAN_CONTINUE;
  400. }
  401. /* Try to read from the client instead */
  402. return WRITE_TRAN_FINISHED;
  403. case TLS_ST_SR_CLNT_HELLO:
  404. st->hand_state = TLS_ST_SW_SRVR_HELLO;
  405. return WRITE_TRAN_CONTINUE;
  406. case TLS_ST_SW_SRVR_HELLO:
  407. if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
  408. && s->hello_retry_request != SSL_HRR_COMPLETE)
  409. st->hand_state = TLS_ST_SW_CHANGE;
  410. else if (s->hello_retry_request == SSL_HRR_PENDING)
  411. st->hand_state = TLS_ST_EARLY_DATA;
  412. else
  413. st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
  414. return WRITE_TRAN_CONTINUE;
  415. case TLS_ST_SW_CHANGE:
  416. if (s->hello_retry_request == SSL_HRR_PENDING)
  417. st->hand_state = TLS_ST_EARLY_DATA;
  418. else
  419. st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
  420. return WRITE_TRAN_CONTINUE;
  421. case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
  422. if (s->hit)
  423. st->hand_state = TLS_ST_SW_FINISHED;
  424. else if (send_certificate_request(s))
  425. st->hand_state = TLS_ST_SW_CERT_REQ;
  426. else
  427. st->hand_state = TLS_ST_SW_CERT;
  428. return WRITE_TRAN_CONTINUE;
  429. case TLS_ST_SW_CERT_REQ:
  430. if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
  431. s->post_handshake_auth = SSL_PHA_REQUESTED;
  432. st->hand_state = TLS_ST_OK;
  433. } else {
  434. st->hand_state = TLS_ST_SW_CERT;
  435. }
  436. return WRITE_TRAN_CONTINUE;
  437. case TLS_ST_SW_CERT:
  438. st->hand_state = TLS_ST_SW_CERT_VRFY;
  439. return WRITE_TRAN_CONTINUE;
  440. case TLS_ST_SW_CERT_VRFY:
  441. st->hand_state = TLS_ST_SW_FINISHED;
  442. return WRITE_TRAN_CONTINUE;
  443. case TLS_ST_SW_FINISHED:
  444. st->hand_state = TLS_ST_EARLY_DATA;
  445. return WRITE_TRAN_CONTINUE;
  446. case TLS_ST_EARLY_DATA:
  447. return WRITE_TRAN_FINISHED;
  448. case TLS_ST_SR_FINISHED:
  449. /*
  450. * Technically we have finished the handshake at this point, but we're
  451. * going to remain "in_init" for now and write out any session tickets
  452. * immediately.
  453. */
  454. if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
  455. s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;
  456. } else if (!s->ext.ticket_expected) {
  457. /*
  458. * If we're not going to renew the ticket then we just finish the
  459. * handshake at this point.
  460. */
  461. st->hand_state = TLS_ST_OK;
  462. return WRITE_TRAN_CONTINUE;
  463. }
  464. if (s->num_tickets > s->sent_tickets)
  465. st->hand_state = TLS_ST_SW_SESSION_TICKET;
  466. else
  467. st->hand_state = TLS_ST_OK;
  468. return WRITE_TRAN_CONTINUE;
  469. case TLS_ST_SR_KEY_UPDATE:
  470. case TLS_ST_SW_KEY_UPDATE:
  471. st->hand_state = TLS_ST_OK;
  472. return WRITE_TRAN_CONTINUE;
  473. case TLS_ST_SW_SESSION_TICKET:
  474. /* In a resumption we only ever send a maximum of one new ticket.
  475. * Following an initial handshake we send the number of tickets we have
  476. * been configured for.
  477. */
  478. if (!SSL_IS_FIRST_HANDSHAKE(s) && s->ext.extra_tickets_expected > 0) {
  479. return WRITE_TRAN_CONTINUE;
  480. } else if (s->hit || s->num_tickets <= s->sent_tickets) {
  481. /* We've written enough tickets out. */
  482. st->hand_state = TLS_ST_OK;
  483. }
  484. return WRITE_TRAN_CONTINUE;
  485. }
  486. }
  487. /*
  488. * ossl_statem_server_write_transition() works out what handshake state to move
  489. * to next when the server is writing messages to be sent to the client.
  490. */
  491. WRITE_TRAN ossl_statem_server_write_transition(SSL *s)
  492. {
  493. OSSL_STATEM *st = &s->statem;
  494. /*
  495. * Note that before the ClientHello we don't know what version we are going
  496. * to negotiate yet, so we don't take this branch until later
  497. */
  498. if (SSL_IS_TLS13(s))
  499. return ossl_statem_server13_write_transition(s);
  500. switch (st->hand_state) {
  501. default:
  502. /* Shouldn't happen */
  503. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  504. return WRITE_TRAN_ERROR;
  505. case TLS_ST_OK:
  506. if (st->request_state == TLS_ST_SW_HELLO_REQ) {
  507. /* We must be trying to renegotiate */
  508. st->hand_state = TLS_ST_SW_HELLO_REQ;
  509. st->request_state = TLS_ST_BEFORE;
  510. return WRITE_TRAN_CONTINUE;
  511. }
  512. /* Must be an incoming ClientHello */
  513. if (!tls_setup_handshake(s)) {
  514. /* SSLfatal() already called */
  515. return WRITE_TRAN_ERROR;
  516. }
  517. /* Fall through */
  518. case TLS_ST_BEFORE:
  519. /* Just go straight to trying to read from the client */
  520. return WRITE_TRAN_FINISHED;
  521. case TLS_ST_SW_HELLO_REQ:
  522. st->hand_state = TLS_ST_OK;
  523. return WRITE_TRAN_CONTINUE;
  524. case TLS_ST_SR_CLNT_HELLO:
  525. if (SSL_IS_DTLS(s) && !s->d1->cookie_verified
  526. && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) {
  527. st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;
  528. } else if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
  529. /* We must have rejected the renegotiation */
  530. st->hand_state = TLS_ST_OK;
  531. return WRITE_TRAN_CONTINUE;
  532. } else {
  533. st->hand_state = TLS_ST_SW_SRVR_HELLO;
  534. }
  535. return WRITE_TRAN_CONTINUE;
  536. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  537. return WRITE_TRAN_FINISHED;
  538. case TLS_ST_SW_SRVR_HELLO:
  539. if (s->hit) {
  540. if (s->ext.ticket_expected)
  541. st->hand_state = TLS_ST_SW_SESSION_TICKET;
  542. else
  543. st->hand_state = TLS_ST_SW_CHANGE;
  544. } else {
  545. /* Check if it is anon DH or anon ECDH, */
  546. /* normal PSK or SRP */
  547. if (!(s->s3.tmp.new_cipher->algorithm_auth &
  548. (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
  549. st->hand_state = TLS_ST_SW_CERT;
  550. } else if (send_server_key_exchange(s)) {
  551. st->hand_state = TLS_ST_SW_KEY_EXCH;
  552. } else if (send_certificate_request(s)) {
  553. st->hand_state = TLS_ST_SW_CERT_REQ;
  554. } else {
  555. st->hand_state = TLS_ST_SW_SRVR_DONE;
  556. }
  557. }
  558. return WRITE_TRAN_CONTINUE;
  559. case TLS_ST_SW_CERT:
  560. if (s->ext.status_expected) {
  561. st->hand_state = TLS_ST_SW_CERT_STATUS;
  562. return WRITE_TRAN_CONTINUE;
  563. }
  564. /* Fall through */
  565. case TLS_ST_SW_CERT_STATUS:
  566. if (send_server_key_exchange(s)) {
  567. st->hand_state = TLS_ST_SW_KEY_EXCH;
  568. return WRITE_TRAN_CONTINUE;
  569. }
  570. /* Fall through */
  571. case TLS_ST_SW_KEY_EXCH:
  572. if (send_certificate_request(s)) {
  573. st->hand_state = TLS_ST_SW_CERT_REQ;
  574. return WRITE_TRAN_CONTINUE;
  575. }
  576. /* Fall through */
  577. case TLS_ST_SW_CERT_REQ:
  578. st->hand_state = TLS_ST_SW_SRVR_DONE;
  579. return WRITE_TRAN_CONTINUE;
  580. case TLS_ST_SW_SRVR_DONE:
  581. return WRITE_TRAN_FINISHED;
  582. case TLS_ST_SR_FINISHED:
  583. if (s->hit) {
  584. st->hand_state = TLS_ST_OK;
  585. return WRITE_TRAN_CONTINUE;
  586. } else if (s->ext.ticket_expected) {
  587. st->hand_state = TLS_ST_SW_SESSION_TICKET;
  588. } else {
  589. st->hand_state = TLS_ST_SW_CHANGE;
  590. }
  591. return WRITE_TRAN_CONTINUE;
  592. case TLS_ST_SW_SESSION_TICKET:
  593. st->hand_state = TLS_ST_SW_CHANGE;
  594. return WRITE_TRAN_CONTINUE;
  595. case TLS_ST_SW_CHANGE:
  596. st->hand_state = TLS_ST_SW_FINISHED;
  597. return WRITE_TRAN_CONTINUE;
  598. case TLS_ST_SW_FINISHED:
  599. if (s->hit) {
  600. return WRITE_TRAN_FINISHED;
  601. }
  602. st->hand_state = TLS_ST_OK;
  603. return WRITE_TRAN_CONTINUE;
  604. }
  605. }
  606. /*
  607. * Perform any pre work that needs to be done prior to sending a message from
  608. * the server to the client.
  609. */
  610. WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst)
  611. {
  612. OSSL_STATEM *st = &s->statem;
  613. switch (st->hand_state) {
  614. default:
  615. /* No pre work to be done */
  616. break;
  617. case TLS_ST_SW_HELLO_REQ:
  618. s->shutdown = 0;
  619. if (SSL_IS_DTLS(s))
  620. dtls1_clear_sent_buffer(s);
  621. break;
  622. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  623. s->shutdown = 0;
  624. if (SSL_IS_DTLS(s)) {
  625. dtls1_clear_sent_buffer(s);
  626. /* We don't buffer this message so don't use the timer */
  627. st->use_timer = 0;
  628. }
  629. break;
  630. case TLS_ST_SW_SRVR_HELLO:
  631. if (SSL_IS_DTLS(s)) {
  632. /*
  633. * Messages we write from now on should be buffered and
  634. * retransmitted if necessary, so we need to use the timer now
  635. */
  636. st->use_timer = 1;
  637. }
  638. break;
  639. case TLS_ST_SW_SRVR_DONE:
  640. #ifndef OPENSSL_NO_SCTP
  641. if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
  642. /* Calls SSLfatal() as required */
  643. return dtls_wait_for_dry(s);
  644. }
  645. #endif
  646. return WORK_FINISHED_CONTINUE;
  647. case TLS_ST_SW_SESSION_TICKET:
  648. if (SSL_IS_TLS13(s) && s->sent_tickets == 0
  649. && s->ext.extra_tickets_expected == 0) {
  650. /*
  651. * Actually this is the end of the handshake, but we're going
  652. * straight into writing the session ticket out. So we finish off
  653. * the handshake, but keep the various buffers active.
  654. *
  655. * Calls SSLfatal as required.
  656. */
  657. return tls_finish_handshake(s, wst, 0, 0);
  658. }
  659. if (SSL_IS_DTLS(s)) {
  660. /*
  661. * We're into the last flight. We don't retransmit the last flight
  662. * unless we need to, so we don't use the timer
  663. */
  664. st->use_timer = 0;
  665. }
  666. break;
  667. case TLS_ST_SW_CHANGE:
  668. if (SSL_IS_TLS13(s))
  669. break;
  670. /* Writes to s->session are only safe for initial handshakes */
  671. if (s->session->cipher == NULL) {
  672. s->session->cipher = s->s3.tmp.new_cipher;
  673. } else if (s->session->cipher != s->s3.tmp.new_cipher) {
  674. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  675. return WORK_ERROR;
  676. }
  677. if (!s->method->ssl3_enc->setup_key_block(s)) {
  678. /* SSLfatal() already called */
  679. return WORK_ERROR;
  680. }
  681. if (SSL_IS_DTLS(s)) {
  682. /*
  683. * We're into the last flight. We don't retransmit the last flight
  684. * unless we need to, so we don't use the timer. This might have
  685. * already been set to 0 if we sent a NewSessionTicket message,
  686. * but we'll set it again here in case we didn't.
  687. */
  688. st->use_timer = 0;
  689. }
  690. return WORK_FINISHED_CONTINUE;
  691. case TLS_ST_EARLY_DATA:
  692. if (s->early_data_state != SSL_EARLY_DATA_ACCEPTING
  693. && (s->s3.flags & TLS1_FLAGS_STATELESS) == 0)
  694. return WORK_FINISHED_CONTINUE;
  695. /* Fall through */
  696. case TLS_ST_OK:
  697. /* Calls SSLfatal() as required */
  698. return tls_finish_handshake(s, wst, 1, 1);
  699. }
  700. return WORK_FINISHED_CONTINUE;
  701. }
  702. static ossl_inline int conn_is_closed(void)
  703. {
  704. switch (get_last_sys_error()) {
  705. #if defined(EPIPE)
  706. case EPIPE:
  707. return 1;
  708. #endif
  709. #if defined(ECONNRESET)
  710. case ECONNRESET:
  711. return 1;
  712. #endif
  713. #if defined(WSAECONNRESET)
  714. case WSAECONNRESET:
  715. return 1;
  716. #endif
  717. default:
  718. return 0;
  719. }
  720. }
  721. /*
  722. * Perform any work that needs to be done after sending a message from the
  723. * server to the client.
  724. */
  725. WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst)
  726. {
  727. OSSL_STATEM *st = &s->statem;
  728. s->init_num = 0;
  729. switch (st->hand_state) {
  730. default:
  731. /* No post work to be done */
  732. break;
  733. case TLS_ST_SW_HELLO_REQ:
  734. if (statem_flush(s) != 1)
  735. return WORK_MORE_A;
  736. if (!ssl3_init_finished_mac(s)) {
  737. /* SSLfatal() already called */
  738. return WORK_ERROR;
  739. }
  740. break;
  741. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  742. if (statem_flush(s) != 1)
  743. return WORK_MORE_A;
  744. /* HelloVerifyRequest resets Finished MAC */
  745. if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) {
  746. /* SSLfatal() already called */
  747. return WORK_ERROR;
  748. }
  749. /*
  750. * The next message should be another ClientHello which we need to
  751. * treat like it was the first packet
  752. */
  753. s->first_packet = 1;
  754. break;
  755. case TLS_ST_SW_SRVR_HELLO:
  756. if (SSL_IS_TLS13(s) && s->hello_retry_request == SSL_HRR_PENDING) {
  757. if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0
  758. && statem_flush(s) != 1)
  759. return WORK_MORE_A;
  760. break;
  761. }
  762. #ifndef OPENSSL_NO_SCTP
  763. if (SSL_IS_DTLS(s) && s->hit) {
  764. unsigned char sctpauthkey[64];
  765. char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
  766. size_t labellen;
  767. /*
  768. * Add new shared key for SCTP-Auth, will be ignored if no
  769. * SCTP used.
  770. */
  771. memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
  772. sizeof(DTLS1_SCTP_AUTH_LABEL));
  773. /* Don't include the terminating zero. */
  774. labellen = sizeof(labelbuffer) - 1;
  775. if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
  776. labellen += 1;
  777. if (SSL_export_keying_material(s, sctpauthkey,
  778. sizeof(sctpauthkey), labelbuffer,
  779. labellen, NULL, 0,
  780. 0) <= 0) {
  781. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  782. return WORK_ERROR;
  783. }
  784. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  785. sizeof(sctpauthkey), sctpauthkey);
  786. }
  787. #endif
  788. if (!SSL_IS_TLS13(s)
  789. || ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
  790. && s->hello_retry_request != SSL_HRR_COMPLETE))
  791. break;
  792. /* Fall through */
  793. case TLS_ST_SW_CHANGE:
  794. if (s->hello_retry_request == SSL_HRR_PENDING) {
  795. if (!statem_flush(s))
  796. return WORK_MORE_A;
  797. break;
  798. }
  799. if (SSL_IS_TLS13(s)) {
  800. if (!s->method->ssl3_enc->setup_key_block(s)
  801. || !s->method->ssl3_enc->change_cipher_state(s,
  802. SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
  803. /* SSLfatal() already called */
  804. return WORK_ERROR;
  805. }
  806. if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED
  807. && !s->method->ssl3_enc->change_cipher_state(s,
  808. SSL3_CC_HANDSHAKE |SSL3_CHANGE_CIPHER_SERVER_READ)) {
  809. /* SSLfatal() already called */
  810. return WORK_ERROR;
  811. }
  812. /*
  813. * We don't yet know whether the next record we are going to receive
  814. * is an unencrypted alert, an encrypted alert, or an encrypted
  815. * handshake message. We temporarily tolerate unencrypted alerts.
  816. */
  817. s->statem.enc_read_state = ENC_READ_STATE_ALLOW_PLAIN_ALERTS;
  818. break;
  819. }
  820. #ifndef OPENSSL_NO_SCTP
  821. if (SSL_IS_DTLS(s) && !s->hit) {
  822. /*
  823. * Change to new shared key of SCTP-Auth, will be ignored if
  824. * no SCTP used.
  825. */
  826. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  827. 0, NULL);
  828. }
  829. #endif
  830. if (!s->method->ssl3_enc->change_cipher_state(s,
  831. SSL3_CHANGE_CIPHER_SERVER_WRITE))
  832. {
  833. /* SSLfatal() already called */
  834. return WORK_ERROR;
  835. }
  836. if (SSL_IS_DTLS(s))
  837. dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
  838. break;
  839. case TLS_ST_SW_SRVR_DONE:
  840. if (statem_flush(s) != 1)
  841. return WORK_MORE_A;
  842. break;
  843. case TLS_ST_SW_FINISHED:
  844. if (statem_flush(s) != 1)
  845. return WORK_MORE_A;
  846. #ifndef OPENSSL_NO_SCTP
  847. if (SSL_IS_DTLS(s) && s->hit) {
  848. /*
  849. * Change to new shared key of SCTP-Auth, will be ignored if
  850. * no SCTP used.
  851. */
  852. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  853. 0, NULL);
  854. }
  855. #endif
  856. if (SSL_IS_TLS13(s)) {
  857. /* TLS 1.3 gets the secret size from the handshake md */
  858. size_t dummy;
  859. if (!s->method->ssl3_enc->generate_master_secret(s,
  860. s->master_secret, s->handshake_secret, 0,
  861. &dummy)
  862. || !s->method->ssl3_enc->change_cipher_state(s,
  863. SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE))
  864. /* SSLfatal() already called */
  865. return WORK_ERROR;
  866. }
  867. break;
  868. case TLS_ST_SW_CERT_REQ:
  869. if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
  870. if (statem_flush(s) != 1)
  871. return WORK_MORE_A;
  872. }
  873. break;
  874. case TLS_ST_SW_KEY_UPDATE:
  875. if (statem_flush(s) != 1)
  876. return WORK_MORE_A;
  877. if (!tls13_update_key(s, 1)) {
  878. /* SSLfatal() already called */
  879. return WORK_ERROR;
  880. }
  881. break;
  882. case TLS_ST_SW_SESSION_TICKET:
  883. clear_sys_error();
  884. if (SSL_IS_TLS13(s) && statem_flush(s) != 1) {
  885. if (SSL_get_error(s, 0) == SSL_ERROR_SYSCALL
  886. && conn_is_closed()) {
  887. /*
  888. * We ignore connection closed errors in TLSv1.3 when sending a
  889. * NewSessionTicket and behave as if we were successful. This is
  890. * so that we are still able to read data sent to us by a client
  891. * that closes soon after the end of the handshake without
  892. * waiting to read our post-handshake NewSessionTickets.
  893. */
  894. s->rwstate = SSL_NOTHING;
  895. break;
  896. }
  897. return WORK_MORE_A;
  898. }
  899. break;
  900. }
  901. return WORK_FINISHED_CONTINUE;
  902. }
  903. /*
  904. * Get the message construction function and message type for sending from the
  905. * server
  906. *
  907. * Valid return values are:
  908. * 1: Success
  909. * 0: Error
  910. */
  911. int ossl_statem_server_construct_message(SSL *s,
  912. confunc_f *confunc, int *mt)
  913. {
  914. OSSL_STATEM *st = &s->statem;
  915. switch (st->hand_state) {
  916. default:
  917. /* Shouldn't happen */
  918. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE);
  919. return 0;
  920. case TLS_ST_SW_CHANGE:
  921. if (SSL_IS_DTLS(s))
  922. *confunc = dtls_construct_change_cipher_spec;
  923. else
  924. *confunc = tls_construct_change_cipher_spec;
  925. *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
  926. break;
  927. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  928. *confunc = dtls_construct_hello_verify_request;
  929. *mt = DTLS1_MT_HELLO_VERIFY_REQUEST;
  930. break;
  931. case TLS_ST_SW_HELLO_REQ:
  932. /* No construction function needed */
  933. *confunc = NULL;
  934. *mt = SSL3_MT_HELLO_REQUEST;
  935. break;
  936. case TLS_ST_SW_SRVR_HELLO:
  937. *confunc = tls_construct_server_hello;
  938. *mt = SSL3_MT_SERVER_HELLO;
  939. break;
  940. case TLS_ST_SW_CERT:
  941. *confunc = tls_construct_server_certificate;
  942. *mt = SSL3_MT_CERTIFICATE;
  943. break;
  944. case TLS_ST_SW_CERT_VRFY:
  945. *confunc = tls_construct_cert_verify;
  946. *mt = SSL3_MT_CERTIFICATE_VERIFY;
  947. break;
  948. case TLS_ST_SW_KEY_EXCH:
  949. *confunc = tls_construct_server_key_exchange;
  950. *mt = SSL3_MT_SERVER_KEY_EXCHANGE;
  951. break;
  952. case TLS_ST_SW_CERT_REQ:
  953. *confunc = tls_construct_certificate_request;
  954. *mt = SSL3_MT_CERTIFICATE_REQUEST;
  955. break;
  956. case TLS_ST_SW_SRVR_DONE:
  957. *confunc = tls_construct_server_done;
  958. *mt = SSL3_MT_SERVER_DONE;
  959. break;
  960. case TLS_ST_SW_SESSION_TICKET:
  961. *confunc = tls_construct_new_session_ticket;
  962. *mt = SSL3_MT_NEWSESSION_TICKET;
  963. break;
  964. case TLS_ST_SW_CERT_STATUS:
  965. *confunc = tls_construct_cert_status;
  966. *mt = SSL3_MT_CERTIFICATE_STATUS;
  967. break;
  968. case TLS_ST_SW_FINISHED:
  969. *confunc = tls_construct_finished;
  970. *mt = SSL3_MT_FINISHED;
  971. break;
  972. case TLS_ST_EARLY_DATA:
  973. *confunc = NULL;
  974. *mt = SSL3_MT_DUMMY;
  975. break;
  976. case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
  977. *confunc = tls_construct_encrypted_extensions;
  978. *mt = SSL3_MT_ENCRYPTED_EXTENSIONS;
  979. break;
  980. case TLS_ST_SW_KEY_UPDATE:
  981. *confunc = tls_construct_key_update;
  982. *mt = SSL3_MT_KEY_UPDATE;
  983. break;
  984. }
  985. return 1;
  986. }
  987. /*
  988. * Maximum size (excluding the Handshake header) of a ClientHello message,
  989. * calculated as follows:
  990. *
  991. * 2 + # client_version
  992. * 32 + # only valid length for random
  993. * 1 + # length of session_id
  994. * 32 + # maximum size for session_id
  995. * 2 + # length of cipher suites
  996. * 2^16-2 + # maximum length of cipher suites array
  997. * 1 + # length of compression_methods
  998. * 2^8-1 + # maximum length of compression methods
  999. * 2 + # length of extensions
  1000. * 2^16-1 # maximum length of extensions
  1001. */
  1002. #define CLIENT_HELLO_MAX_LENGTH 131396
  1003. #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
  1004. #define NEXT_PROTO_MAX_LENGTH 514
  1005. /*
  1006. * Returns the maximum allowed length for the current message that we are
  1007. * reading. Excludes the message header.
  1008. */
  1009. size_t ossl_statem_server_max_message_size(SSL *s)
  1010. {
  1011. OSSL_STATEM *st = &s->statem;
  1012. switch (st->hand_state) {
  1013. default:
  1014. /* Shouldn't happen */
  1015. return 0;
  1016. case TLS_ST_SR_CLNT_HELLO:
  1017. return CLIENT_HELLO_MAX_LENGTH;
  1018. case TLS_ST_SR_END_OF_EARLY_DATA:
  1019. return END_OF_EARLY_DATA_MAX_LENGTH;
  1020. case TLS_ST_SR_CERT:
  1021. return s->max_cert_list;
  1022. case TLS_ST_SR_KEY_EXCH:
  1023. return CLIENT_KEY_EXCH_MAX_LENGTH;
  1024. case TLS_ST_SR_CERT_VRFY:
  1025. return SSL3_RT_MAX_PLAIN_LENGTH;
  1026. #ifndef OPENSSL_NO_NEXTPROTONEG
  1027. case TLS_ST_SR_NEXT_PROTO:
  1028. return NEXT_PROTO_MAX_LENGTH;
  1029. #endif
  1030. case TLS_ST_SR_CHANGE:
  1031. return CCS_MAX_LENGTH;
  1032. case TLS_ST_SR_FINISHED:
  1033. return FINISHED_MAX_LENGTH;
  1034. case TLS_ST_SR_KEY_UPDATE:
  1035. return KEY_UPDATE_MAX_LENGTH;
  1036. }
  1037. }
  1038. /*
  1039. * Process a message that the server has received from the client.
  1040. */
  1041. MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt)
  1042. {
  1043. OSSL_STATEM *st = &s->statem;
  1044. switch (st->hand_state) {
  1045. default:
  1046. /* Shouldn't happen */
  1047. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1048. return MSG_PROCESS_ERROR;
  1049. case TLS_ST_SR_CLNT_HELLO:
  1050. return tls_process_client_hello(s, pkt);
  1051. case TLS_ST_SR_END_OF_EARLY_DATA:
  1052. return tls_process_end_of_early_data(s, pkt);
  1053. case TLS_ST_SR_CERT:
  1054. return tls_process_client_certificate(s, pkt);
  1055. case TLS_ST_SR_KEY_EXCH:
  1056. return tls_process_client_key_exchange(s, pkt);
  1057. case TLS_ST_SR_CERT_VRFY:
  1058. return tls_process_cert_verify(s, pkt);
  1059. #ifndef OPENSSL_NO_NEXTPROTONEG
  1060. case TLS_ST_SR_NEXT_PROTO:
  1061. return tls_process_next_proto(s, pkt);
  1062. #endif
  1063. case TLS_ST_SR_CHANGE:
  1064. return tls_process_change_cipher_spec(s, pkt);
  1065. case TLS_ST_SR_FINISHED:
  1066. return tls_process_finished(s, pkt);
  1067. case TLS_ST_SR_KEY_UPDATE:
  1068. return tls_process_key_update(s, pkt);
  1069. }
  1070. }
  1071. /*
  1072. * Perform any further processing required following the receipt of a message
  1073. * from the client
  1074. */
  1075. WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst)
  1076. {
  1077. OSSL_STATEM *st = &s->statem;
  1078. switch (st->hand_state) {
  1079. default:
  1080. /* Shouldn't happen */
  1081. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1082. return WORK_ERROR;
  1083. case TLS_ST_SR_CLNT_HELLO:
  1084. return tls_post_process_client_hello(s, wst);
  1085. case TLS_ST_SR_KEY_EXCH:
  1086. return tls_post_process_client_key_exchange(s, wst);
  1087. }
  1088. }
  1089. #ifndef OPENSSL_NO_SRP
  1090. /* Returns 1 on success, 0 for retryable error, -1 for fatal error */
  1091. static int ssl_check_srp_ext_ClientHello(SSL *s)
  1092. {
  1093. int ret;
  1094. int al = SSL_AD_UNRECOGNIZED_NAME;
  1095. if ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
  1096. (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
  1097. if (s->srp_ctx.login == NULL) {
  1098. /*
  1099. * RFC 5054 says SHOULD reject, we do so if There is no srp
  1100. * login name
  1101. */
  1102. SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY,
  1103. SSL_R_PSK_IDENTITY_NOT_FOUND);
  1104. return -1;
  1105. } else {
  1106. ret = ssl_srp_server_param_with_username_intern(s, &al);
  1107. if (ret < 0)
  1108. return 0;
  1109. if (ret == SSL3_AL_FATAL) {
  1110. SSLfatal(s, al,
  1111. al == SSL_AD_UNKNOWN_PSK_IDENTITY
  1112. ? SSL_R_PSK_IDENTITY_NOT_FOUND
  1113. : SSL_R_CLIENTHELLO_TLSEXT);
  1114. return -1;
  1115. }
  1116. }
  1117. }
  1118. return 1;
  1119. }
  1120. #endif
  1121. int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie,
  1122. size_t cookie_len)
  1123. {
  1124. /* Always use DTLS 1.0 version: see RFC 6347 */
  1125. if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION)
  1126. || !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len))
  1127. return 0;
  1128. return 1;
  1129. }
  1130. int dtls_construct_hello_verify_request(SSL *s, WPACKET *pkt)
  1131. {
  1132. unsigned int cookie_leni;
  1133. if (s->ctx->app_gen_cookie_cb == NULL ||
  1134. s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
  1135. &cookie_leni) == 0 ||
  1136. cookie_leni > DTLS1_COOKIE_LENGTH) {
  1137. SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
  1138. return 0;
  1139. }
  1140. s->d1->cookie_len = cookie_leni;
  1141. if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie,
  1142. s->d1->cookie_len)) {
  1143. SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR);
  1144. return 0;
  1145. }
  1146. return 1;
  1147. }
  1148. /*-
  1149. * ssl_check_for_safari attempts to fingerprint Safari using OS X
  1150. * SecureTransport using the TLS extension block in |hello|.
  1151. * Safari, since 10.6, sends exactly these extensions, in this order:
  1152. * SNI,
  1153. * elliptic_curves
  1154. * ec_point_formats
  1155. * signature_algorithms (for TLSv1.2 only)
  1156. *
  1157. * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
  1158. * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
  1159. * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
  1160. * 10.8..10.8.3 (which don't work).
  1161. */
  1162. static void ssl_check_for_safari(SSL *s, const CLIENTHELLO_MSG *hello)
  1163. {
  1164. static const unsigned char kSafariExtensionsBlock[] = {
  1165. 0x00, 0x0a, /* elliptic_curves extension */
  1166. 0x00, 0x08, /* 8 bytes */
  1167. 0x00, 0x06, /* 6 bytes of curve ids */
  1168. 0x00, 0x17, /* P-256 */
  1169. 0x00, 0x18, /* P-384 */
  1170. 0x00, 0x19, /* P-521 */
  1171. 0x00, 0x0b, /* ec_point_formats */
  1172. 0x00, 0x02, /* 2 bytes */
  1173. 0x01, /* 1 point format */
  1174. 0x00, /* uncompressed */
  1175. /* The following is only present in TLS 1.2 */
  1176. 0x00, 0x0d, /* signature_algorithms */
  1177. 0x00, 0x0c, /* 12 bytes */
  1178. 0x00, 0x0a, /* 10 bytes */
  1179. 0x05, 0x01, /* SHA-384/RSA */
  1180. 0x04, 0x01, /* SHA-256/RSA */
  1181. 0x02, 0x01, /* SHA-1/RSA */
  1182. 0x04, 0x03, /* SHA-256/ECDSA */
  1183. 0x02, 0x03, /* SHA-1/ECDSA */
  1184. };
  1185. /* Length of the common prefix (first two extensions). */
  1186. static const size_t kSafariCommonExtensionsLength = 18;
  1187. unsigned int type;
  1188. PACKET sni, tmppkt;
  1189. size_t ext_len;
  1190. tmppkt = hello->extensions;
  1191. if (!PACKET_forward(&tmppkt, 2)
  1192. || !PACKET_get_net_2(&tmppkt, &type)
  1193. || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
  1194. return;
  1195. }
  1196. if (type != TLSEXT_TYPE_server_name)
  1197. return;
  1198. ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
  1199. sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
  1200. s->s3.is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
  1201. ext_len);
  1202. }
  1203. #define RENEG_OPTIONS_OK(options) \
  1204. ((options & SSL_OP_NO_RENEGOTIATION) == 0 \
  1205. && (options & SSL_OP_ALLOW_CLIENT_RENEGOTIATION) != 0)
  1206. MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)
  1207. {
  1208. /* |cookie| will only be initialized for DTLS. */
  1209. PACKET session_id, compression, extensions, cookie;
  1210. static const unsigned char null_compression = 0;
  1211. CLIENTHELLO_MSG *clienthello = NULL;
  1212. /* Check if this is actually an unexpected renegotiation ClientHello */
  1213. if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
  1214. if (!ossl_assert(!SSL_IS_TLS13(s))) {
  1215. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1216. goto err;
  1217. }
  1218. if (!RENEG_OPTIONS_OK(s->options)
  1219. || (!s->s3.send_connection_binding
  1220. && (s->options
  1221. & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION) == 0)) {
  1222. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
  1223. return MSG_PROCESS_FINISHED_READING;
  1224. }
  1225. s->renegotiate = 1;
  1226. s->new_session = 1;
  1227. }
  1228. clienthello = OPENSSL_zalloc(sizeof(*clienthello));
  1229. if (clienthello == NULL) {
  1230. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1231. goto err;
  1232. }
  1233. /*
  1234. * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
  1235. */
  1236. clienthello->isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer);
  1237. PACKET_null_init(&cookie);
  1238. if (clienthello->isv2) {
  1239. unsigned int mt;
  1240. if (!SSL_IS_FIRST_HANDSHAKE(s)
  1241. || s->hello_retry_request != SSL_HRR_NONE) {
  1242. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
  1243. goto err;
  1244. }
  1245. /*-
  1246. * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
  1247. * header is sent directly on the wire, not wrapped as a TLS
  1248. * record. Our record layer just processes the message length and passes
  1249. * the rest right through. Its format is:
  1250. * Byte Content
  1251. * 0-1 msg_length - decoded by the record layer
  1252. * 2 msg_type - s->init_msg points here
  1253. * 3-4 version
  1254. * 5-6 cipher_spec_length
  1255. * 7-8 session_id_length
  1256. * 9-10 challenge_length
  1257. * ... ...
  1258. */
  1259. if (!PACKET_get_1(pkt, &mt)
  1260. || mt != SSL2_MT_CLIENT_HELLO) {
  1261. /*
  1262. * Should never happen. We should have tested this in the record
  1263. * layer in order to have determined that this is a SSLv2 record
  1264. * in the first place
  1265. */
  1266. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1267. goto err;
  1268. }
  1269. }
  1270. if (!PACKET_get_net_2(pkt, &clienthello->legacy_version)) {
  1271. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
  1272. goto err;
  1273. }
  1274. /* Parse the message and load client random. */
  1275. if (clienthello->isv2) {
  1276. /*
  1277. * Handle an SSLv2 backwards compatible ClientHello
  1278. * Note, this is only for SSLv3+ using the backward compatible format.
  1279. * Real SSLv2 is not supported, and is rejected below.
  1280. */
  1281. unsigned int ciphersuite_len, session_id_len, challenge_len;
  1282. PACKET challenge;
  1283. if (!PACKET_get_net_2(pkt, &ciphersuite_len)
  1284. || !PACKET_get_net_2(pkt, &session_id_len)
  1285. || !PACKET_get_net_2(pkt, &challenge_len)) {
  1286. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RECORD_LENGTH_MISMATCH);
  1287. goto err;
  1288. }
  1289. if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  1290. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_LENGTH_MISMATCH);
  1291. goto err;
  1292. }
  1293. if (!PACKET_get_sub_packet(pkt, &clienthello->ciphersuites,
  1294. ciphersuite_len)
  1295. || !PACKET_copy_bytes(pkt, clienthello->session_id, session_id_len)
  1296. || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
  1297. /* No extensions. */
  1298. || PACKET_remaining(pkt) != 0) {
  1299. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RECORD_LENGTH_MISMATCH);
  1300. goto err;
  1301. }
  1302. clienthello->session_id_len = session_id_len;
  1303. /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
  1304. * here rather than sizeof(clienthello->random) because that is the limit
  1305. * for SSLv3 and it is fixed. It won't change even if
  1306. * sizeof(clienthello->random) does.
  1307. */
  1308. challenge_len = challenge_len > SSL3_RANDOM_SIZE
  1309. ? SSL3_RANDOM_SIZE : challenge_len;
  1310. memset(clienthello->random, 0, SSL3_RANDOM_SIZE);
  1311. if (!PACKET_copy_bytes(&challenge,
  1312. clienthello->random + SSL3_RANDOM_SIZE -
  1313. challenge_len, challenge_len)
  1314. /* Advertise only null compression. */
  1315. || !PACKET_buf_init(&compression, &null_compression, 1)) {
  1316. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1317. goto err;
  1318. }
  1319. PACKET_null_init(&clienthello->extensions);
  1320. } else {
  1321. /* Regular ClientHello. */
  1322. if (!PACKET_copy_bytes(pkt, clienthello->random, SSL3_RANDOM_SIZE)
  1323. || !PACKET_get_length_prefixed_1(pkt, &session_id)
  1324. || !PACKET_copy_all(&session_id, clienthello->session_id,
  1325. SSL_MAX_SSL_SESSION_ID_LENGTH,
  1326. &clienthello->session_id_len)) {
  1327. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1328. goto err;
  1329. }
  1330. if (SSL_IS_DTLS(s)) {
  1331. if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
  1332. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1333. goto err;
  1334. }
  1335. if (!PACKET_copy_all(&cookie, clienthello->dtls_cookie,
  1336. DTLS1_COOKIE_LENGTH,
  1337. &clienthello->dtls_cookie_len)) {
  1338. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1339. goto err;
  1340. }
  1341. /*
  1342. * If we require cookies and this ClientHello doesn't contain one,
  1343. * just return since we do not want to allocate any memory yet.
  1344. * So check cookie length...
  1345. */
  1346. if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
  1347. if (clienthello->dtls_cookie_len == 0) {
  1348. OPENSSL_free(clienthello);
  1349. return MSG_PROCESS_FINISHED_READING;
  1350. }
  1351. }
  1352. }
  1353. if (!PACKET_get_length_prefixed_2(pkt, &clienthello->ciphersuites)) {
  1354. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1355. goto err;
  1356. }
  1357. if (!PACKET_get_length_prefixed_1(pkt, &compression)) {
  1358. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1359. goto err;
  1360. }
  1361. /* Could be empty. */
  1362. if (PACKET_remaining(pkt) == 0) {
  1363. PACKET_null_init(&clienthello->extensions);
  1364. } else {
  1365. if (!PACKET_get_length_prefixed_2(pkt, &clienthello->extensions)
  1366. || PACKET_remaining(pkt) != 0) {
  1367. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1368. goto err;
  1369. }
  1370. }
  1371. }
  1372. if (!PACKET_copy_all(&compression, clienthello->compressions,
  1373. MAX_COMPRESSIONS_SIZE,
  1374. &clienthello->compressions_len)) {
  1375. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1376. goto err;
  1377. }
  1378. /* Preserve the raw extensions PACKET for later use */
  1379. extensions = clienthello->extensions;
  1380. if (!tls_collect_extensions(s, &extensions, SSL_EXT_CLIENT_HELLO,
  1381. &clienthello->pre_proc_exts,
  1382. &clienthello->pre_proc_exts_len, 1)) {
  1383. /* SSLfatal already been called */
  1384. goto err;
  1385. }
  1386. s->clienthello = clienthello;
  1387. return MSG_PROCESS_CONTINUE_PROCESSING;
  1388. err:
  1389. if (clienthello != NULL)
  1390. OPENSSL_free(clienthello->pre_proc_exts);
  1391. OPENSSL_free(clienthello);
  1392. return MSG_PROCESS_ERROR;
  1393. }
  1394. static int tls_early_post_process_client_hello(SSL *s)
  1395. {
  1396. unsigned int j;
  1397. int i, al = SSL_AD_INTERNAL_ERROR;
  1398. int protverr;
  1399. size_t loop;
  1400. unsigned long id;
  1401. #ifndef OPENSSL_NO_COMP
  1402. SSL_COMP *comp = NULL;
  1403. #endif
  1404. const SSL_CIPHER *c;
  1405. STACK_OF(SSL_CIPHER) *ciphers = NULL;
  1406. STACK_OF(SSL_CIPHER) *scsvs = NULL;
  1407. CLIENTHELLO_MSG *clienthello = s->clienthello;
  1408. DOWNGRADE dgrd = DOWNGRADE_NONE;
  1409. /* Finished parsing the ClientHello, now we can start processing it */
  1410. /* Give the ClientHello callback a crack at things */
  1411. if (s->ctx->client_hello_cb != NULL) {
  1412. /* A failure in the ClientHello callback terminates the connection. */
  1413. switch (s->ctx->client_hello_cb(s, &al, s->ctx->client_hello_cb_arg)) {
  1414. case SSL_CLIENT_HELLO_SUCCESS:
  1415. break;
  1416. case SSL_CLIENT_HELLO_RETRY:
  1417. s->rwstate = SSL_CLIENT_HELLO_CB;
  1418. return -1;
  1419. case SSL_CLIENT_HELLO_ERROR:
  1420. default:
  1421. SSLfatal(s, al, SSL_R_CALLBACK_FAILED);
  1422. goto err;
  1423. }
  1424. }
  1425. /* Set up the client_random */
  1426. memcpy(s->s3.client_random, clienthello->random, SSL3_RANDOM_SIZE);
  1427. /* Choose the version */
  1428. if (clienthello->isv2) {
  1429. if (clienthello->legacy_version == SSL2_VERSION
  1430. || (clienthello->legacy_version & 0xff00)
  1431. != (SSL3_VERSION_MAJOR << 8)) {
  1432. /*
  1433. * This is real SSLv2 or something completely unknown. We don't
  1434. * support it.
  1435. */
  1436. SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_UNKNOWN_PROTOCOL);
  1437. goto err;
  1438. }
  1439. /* SSLv3/TLS */
  1440. s->client_version = clienthello->legacy_version;
  1441. }
  1442. /*
  1443. * Do SSL/TLS version negotiation if applicable. For DTLS we just check
  1444. * versions are potentially compatible. Version negotiation comes later.
  1445. */
  1446. if (!SSL_IS_DTLS(s)) {
  1447. protverr = ssl_choose_server_version(s, clienthello, &dgrd);
  1448. } else if (s->method->version != DTLS_ANY_VERSION &&
  1449. DTLS_VERSION_LT((int)clienthello->legacy_version, s->version)) {
  1450. protverr = SSL_R_VERSION_TOO_LOW;
  1451. } else {
  1452. protverr = 0;
  1453. }
  1454. if (protverr) {
  1455. if (SSL_IS_FIRST_HANDSHAKE(s)) {
  1456. /* like ssl3_get_record, send alert using remote version number */
  1457. s->version = s->client_version = clienthello->legacy_version;
  1458. }
  1459. SSLfatal(s, SSL_AD_PROTOCOL_VERSION, protverr);
  1460. goto err;
  1461. }
  1462. /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
  1463. if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) {
  1464. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY);
  1465. goto err;
  1466. }
  1467. if (SSL_IS_DTLS(s)) {
  1468. /* Empty cookie was already handled above by returning early. */
  1469. if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
  1470. if (s->ctx->app_verify_cookie_cb != NULL) {
  1471. if (s->ctx->app_verify_cookie_cb(s, clienthello->dtls_cookie,
  1472. clienthello->dtls_cookie_len) == 0) {
  1473. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1474. SSL_R_COOKIE_MISMATCH);
  1475. goto err;
  1476. /* else cookie verification succeeded */
  1477. }
  1478. /* default verification */
  1479. } else if (s->d1->cookie_len != clienthello->dtls_cookie_len
  1480. || memcmp(clienthello->dtls_cookie, s->d1->cookie,
  1481. s->d1->cookie_len) != 0) {
  1482. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_COOKIE_MISMATCH);
  1483. goto err;
  1484. }
  1485. s->d1->cookie_verified = 1;
  1486. }
  1487. if (s->method->version == DTLS_ANY_VERSION) {
  1488. protverr = ssl_choose_server_version(s, clienthello, &dgrd);
  1489. if (protverr != 0) {
  1490. s->version = s->client_version;
  1491. SSLfatal(s, SSL_AD_PROTOCOL_VERSION, protverr);
  1492. goto err;
  1493. }
  1494. }
  1495. }
  1496. s->hit = 0;
  1497. if (!ssl_cache_cipherlist(s, &clienthello->ciphersuites,
  1498. clienthello->isv2) ||
  1499. !bytes_to_cipher_list(s, &clienthello->ciphersuites, &ciphers, &scsvs,
  1500. clienthello->isv2, 1)) {
  1501. /* SSLfatal() already called */
  1502. goto err;
  1503. }
  1504. s->s3.send_connection_binding = 0;
  1505. /* Check what signalling cipher-suite values were received. */
  1506. if (scsvs != NULL) {
  1507. for (i = 0; i < sk_SSL_CIPHER_num(scsvs); i++) {
  1508. c = sk_SSL_CIPHER_value(scsvs, i);
  1509. if (SSL_CIPHER_get_id(c) == SSL3_CK_SCSV) {
  1510. if (s->renegotiate) {
  1511. /* SCSV is fatal if renegotiating */
  1512. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1513. SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
  1514. goto err;
  1515. }
  1516. s->s3.send_connection_binding = 1;
  1517. } else if (SSL_CIPHER_get_id(c) == SSL3_CK_FALLBACK_SCSV &&
  1518. !ssl_check_version_downgrade(s)) {
  1519. /*
  1520. * This SCSV indicates that the client previously tried
  1521. * a higher version. We should fail if the current version
  1522. * is an unexpected downgrade, as that indicates that the first
  1523. * connection may have been tampered with in order to trigger
  1524. * an insecure downgrade.
  1525. */
  1526. SSLfatal(s, SSL_AD_INAPPROPRIATE_FALLBACK,
  1527. SSL_R_INAPPROPRIATE_FALLBACK);
  1528. goto err;
  1529. }
  1530. }
  1531. }
  1532. /* For TLSv1.3 we must select the ciphersuite *before* session resumption */
  1533. if (SSL_IS_TLS13(s)) {
  1534. const SSL_CIPHER *cipher =
  1535. ssl3_choose_cipher(s, ciphers, SSL_get_ciphers(s));
  1536. if (cipher == NULL) {
  1537. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_SHARED_CIPHER);
  1538. goto err;
  1539. }
  1540. if (s->hello_retry_request == SSL_HRR_PENDING
  1541. && (s->s3.tmp.new_cipher == NULL
  1542. || s->s3.tmp.new_cipher->id != cipher->id)) {
  1543. /*
  1544. * A previous HRR picked a different ciphersuite to the one we
  1545. * just selected. Something must have changed.
  1546. */
  1547. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_CIPHER);
  1548. goto err;
  1549. }
  1550. s->s3.tmp.new_cipher = cipher;
  1551. }
  1552. /* We need to do this before getting the session */
  1553. if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret,
  1554. SSL_EXT_CLIENT_HELLO,
  1555. clienthello->pre_proc_exts, NULL, 0)) {
  1556. /* SSLfatal() already called */
  1557. goto err;
  1558. }
  1559. /*
  1560. * We don't allow resumption in a backwards compatible ClientHello.
  1561. * In TLS1.1+, session_id MUST be empty.
  1562. *
  1563. * Versions before 0.9.7 always allow clients to resume sessions in
  1564. * renegotiation. 0.9.7 and later allow this by default, but optionally
  1565. * ignore resumption requests with flag
  1566. * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
  1567. * than a change to default behavior so that applications relying on
  1568. * this for security won't even compile against older library versions).
  1569. * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
  1570. * request renegotiation but not a new session (s->new_session remains
  1571. * unset): for servers, this essentially just means that the
  1572. * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
  1573. * ignored.
  1574. */
  1575. if (clienthello->isv2 ||
  1576. (s->new_session &&
  1577. (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
  1578. if (!ssl_get_new_session(s, 1)) {
  1579. /* SSLfatal() already called */
  1580. goto err;
  1581. }
  1582. } else {
  1583. i = ssl_get_prev_session(s, clienthello);
  1584. if (i == 1) {
  1585. /* previous session */
  1586. s->hit = 1;
  1587. } else if (i == -1) {
  1588. /* SSLfatal() already called */
  1589. goto err;
  1590. } else {
  1591. /* i == 0 */
  1592. if (!ssl_get_new_session(s, 1)) {
  1593. /* SSLfatal() already called */
  1594. goto err;
  1595. }
  1596. }
  1597. }
  1598. if (SSL_IS_TLS13(s)) {
  1599. memcpy(s->tmp_session_id, s->clienthello->session_id,
  1600. s->clienthello->session_id_len);
  1601. s->tmp_session_id_len = s->clienthello->session_id_len;
  1602. }
  1603. /*
  1604. * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
  1605. * ciphersuite compatibility with the session as part of resumption.
  1606. */
  1607. if (!SSL_IS_TLS13(s) && s->hit) {
  1608. j = 0;
  1609. id = s->session->cipher->id;
  1610. OSSL_TRACE_BEGIN(TLS_CIPHER) {
  1611. BIO_printf(trc_out, "client sent %d ciphers\n",
  1612. sk_SSL_CIPHER_num(ciphers));
  1613. }
  1614. for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
  1615. c = sk_SSL_CIPHER_value(ciphers, i);
  1616. if (trc_out != NULL)
  1617. BIO_printf(trc_out, "client [%2d of %2d]:%s\n", i,
  1618. sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
  1619. if (c->id == id) {
  1620. j = 1;
  1621. break;
  1622. }
  1623. }
  1624. if (j == 0) {
  1625. /*
  1626. * we need to have the cipher in the cipher list if we are asked
  1627. * to reuse it
  1628. */
  1629. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1630. SSL_R_REQUIRED_CIPHER_MISSING);
  1631. OSSL_TRACE_CANCEL(TLS_CIPHER);
  1632. goto err;
  1633. }
  1634. OSSL_TRACE_END(TLS_CIPHER);
  1635. }
  1636. for (loop = 0; loop < clienthello->compressions_len; loop++) {
  1637. if (clienthello->compressions[loop] == 0)
  1638. break;
  1639. }
  1640. if (loop >= clienthello->compressions_len) {
  1641. /* no compress */
  1642. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_NO_COMPRESSION_SPECIFIED);
  1643. goto err;
  1644. }
  1645. if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
  1646. ssl_check_for_safari(s, clienthello);
  1647. /* TLS extensions */
  1648. if (!tls_parse_all_extensions(s, SSL_EXT_CLIENT_HELLO,
  1649. clienthello->pre_proc_exts, NULL, 0, 1)) {
  1650. /* SSLfatal() already called */
  1651. goto err;
  1652. }
  1653. /*
  1654. * Check if we want to use external pre-shared secret for this handshake
  1655. * for not reused session only. We need to generate server_random before
  1656. * calling tls_session_secret_cb in order to allow SessionTicket
  1657. * processing to use it in key derivation.
  1658. */
  1659. {
  1660. unsigned char *pos;
  1661. pos = s->s3.server_random;
  1662. if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE, dgrd) <= 0) {
  1663. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1664. goto err;
  1665. }
  1666. }
  1667. if (!s->hit
  1668. && s->version >= TLS1_VERSION
  1669. && !SSL_IS_TLS13(s)
  1670. && !SSL_IS_DTLS(s)
  1671. && s->ext.session_secret_cb) {
  1672. const SSL_CIPHER *pref_cipher = NULL;
  1673. /*
  1674. * s->session->master_key_length is a size_t, but this is an int for
  1675. * backwards compat reasons
  1676. */
  1677. int master_key_length;
  1678. master_key_length = sizeof(s->session->master_key);
  1679. if (s->ext.session_secret_cb(s, s->session->master_key,
  1680. &master_key_length, ciphers,
  1681. &pref_cipher,
  1682. s->ext.session_secret_cb_arg)
  1683. && master_key_length > 0) {
  1684. s->session->master_key_length = master_key_length;
  1685. s->hit = 1;
  1686. s->peer_ciphers = ciphers;
  1687. s->session->verify_result = X509_V_OK;
  1688. ciphers = NULL;
  1689. /* check if some cipher was preferred by call back */
  1690. if (pref_cipher == NULL)
  1691. pref_cipher = ssl3_choose_cipher(s, s->peer_ciphers,
  1692. SSL_get_ciphers(s));
  1693. if (pref_cipher == NULL) {
  1694. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_SHARED_CIPHER);
  1695. goto err;
  1696. }
  1697. s->session->cipher = pref_cipher;
  1698. sk_SSL_CIPHER_free(s->cipher_list);
  1699. s->cipher_list = sk_SSL_CIPHER_dup(s->peer_ciphers);
  1700. sk_SSL_CIPHER_free(s->cipher_list_by_id);
  1701. s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->peer_ciphers);
  1702. }
  1703. }
  1704. /*
  1705. * Worst case, we will use the NULL compression, but if we have other
  1706. * options, we will now look for them. We have complen-1 compression
  1707. * algorithms from the client, starting at q.
  1708. */
  1709. s->s3.tmp.new_compression = NULL;
  1710. if (SSL_IS_TLS13(s)) {
  1711. /*
  1712. * We already checked above that the NULL compression method appears in
  1713. * the list. Now we check there aren't any others (which is illegal in
  1714. * a TLSv1.3 ClientHello.
  1715. */
  1716. if (clienthello->compressions_len != 1) {
  1717. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1718. SSL_R_INVALID_COMPRESSION_ALGORITHM);
  1719. goto err;
  1720. }
  1721. }
  1722. #ifndef OPENSSL_NO_COMP
  1723. /* This only happens if we have a cache hit */
  1724. else if (s->session->compress_meth != 0) {
  1725. int m, comp_id = s->session->compress_meth;
  1726. unsigned int k;
  1727. /* Perform sanity checks on resumed compression algorithm */
  1728. /* Can't disable compression */
  1729. if (!ssl_allow_compression(s)) {
  1730. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1731. SSL_R_INCONSISTENT_COMPRESSION);
  1732. goto err;
  1733. }
  1734. /* Look for resumed compression method */
  1735. for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
  1736. comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
  1737. if (comp_id == comp->id) {
  1738. s->s3.tmp.new_compression = comp;
  1739. break;
  1740. }
  1741. }
  1742. if (s->s3.tmp.new_compression == NULL) {
  1743. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1744. SSL_R_INVALID_COMPRESSION_ALGORITHM);
  1745. goto err;
  1746. }
  1747. /* Look for resumed method in compression list */
  1748. for (k = 0; k < clienthello->compressions_len; k++) {
  1749. if (clienthello->compressions[k] == comp_id)
  1750. break;
  1751. }
  1752. if (k >= clienthello->compressions_len) {
  1753. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1754. SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
  1755. goto err;
  1756. }
  1757. } else if (s->hit) {
  1758. comp = NULL;
  1759. } else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
  1760. /* See if we have a match */
  1761. int m, nn, v, done = 0;
  1762. unsigned int o;
  1763. nn = sk_SSL_COMP_num(s->ctx->comp_methods);
  1764. for (m = 0; m < nn; m++) {
  1765. comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
  1766. v = comp->id;
  1767. for (o = 0; o < clienthello->compressions_len; o++) {
  1768. if (v == clienthello->compressions[o]) {
  1769. done = 1;
  1770. break;
  1771. }
  1772. }
  1773. if (done)
  1774. break;
  1775. }
  1776. if (done)
  1777. s->s3.tmp.new_compression = comp;
  1778. else
  1779. comp = NULL;
  1780. }
  1781. #else
  1782. /*
  1783. * If compression is disabled we'd better not try to resume a session
  1784. * using compression.
  1785. */
  1786. if (s->session->compress_meth != 0) {
  1787. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_COMPRESSION);
  1788. goto err;
  1789. }
  1790. #endif
  1791. /*
  1792. * Given s->peer_ciphers and SSL_get_ciphers, we must pick a cipher
  1793. */
  1794. if (!s->hit || SSL_IS_TLS13(s)) {
  1795. sk_SSL_CIPHER_free(s->peer_ciphers);
  1796. s->peer_ciphers = ciphers;
  1797. if (ciphers == NULL) {
  1798. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1799. goto err;
  1800. }
  1801. ciphers = NULL;
  1802. }
  1803. if (!s->hit) {
  1804. #ifdef OPENSSL_NO_COMP
  1805. s->session->compress_meth = 0;
  1806. #else
  1807. s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
  1808. #endif
  1809. if (!tls1_set_server_sigalgs(s)) {
  1810. /* SSLfatal() already called */
  1811. goto err;
  1812. }
  1813. }
  1814. sk_SSL_CIPHER_free(ciphers);
  1815. sk_SSL_CIPHER_free(scsvs);
  1816. OPENSSL_free(clienthello->pre_proc_exts);
  1817. OPENSSL_free(s->clienthello);
  1818. s->clienthello = NULL;
  1819. return 1;
  1820. err:
  1821. sk_SSL_CIPHER_free(ciphers);
  1822. sk_SSL_CIPHER_free(scsvs);
  1823. OPENSSL_free(clienthello->pre_proc_exts);
  1824. OPENSSL_free(s->clienthello);
  1825. s->clienthello = NULL;
  1826. return 0;
  1827. }
  1828. /*
  1829. * Call the status request callback if needed. Upon success, returns 1.
  1830. * Upon failure, returns 0.
  1831. */
  1832. static int tls_handle_status_request(SSL *s)
  1833. {
  1834. s->ext.status_expected = 0;
  1835. /*
  1836. * If status request then ask callback what to do. Note: this must be
  1837. * called after servername callbacks in case the certificate has changed,
  1838. * and must be called after the cipher has been chosen because this may
  1839. * influence which certificate is sent
  1840. */
  1841. if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing && s->ctx != NULL
  1842. && s->ctx->ext.status_cb != NULL) {
  1843. int ret;
  1844. /* If no certificate can't return certificate status */
  1845. if (s->s3.tmp.cert != NULL) {
  1846. /*
  1847. * Set current certificate to one we will use so SSL_get_certificate
  1848. * et al can pick it up.
  1849. */
  1850. s->cert->key = s->s3.tmp.cert;
  1851. ret = s->ctx->ext.status_cb(s, s->ctx->ext.status_arg);
  1852. switch (ret) {
  1853. /* We don't want to send a status request response */
  1854. case SSL_TLSEXT_ERR_NOACK:
  1855. s->ext.status_expected = 0;
  1856. break;
  1857. /* status request response should be sent */
  1858. case SSL_TLSEXT_ERR_OK:
  1859. if (s->ext.ocsp.resp)
  1860. s->ext.status_expected = 1;
  1861. break;
  1862. /* something bad happened */
  1863. case SSL_TLSEXT_ERR_ALERT_FATAL:
  1864. default:
  1865. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CLIENTHELLO_TLSEXT);
  1866. return 0;
  1867. }
  1868. }
  1869. }
  1870. return 1;
  1871. }
  1872. /*
  1873. * Call the alpn_select callback if needed. Upon success, returns 1.
  1874. * Upon failure, returns 0.
  1875. */
  1876. int tls_handle_alpn(SSL *s)
  1877. {
  1878. const unsigned char *selected = NULL;
  1879. unsigned char selected_len = 0;
  1880. if (s->ctx->ext.alpn_select_cb != NULL && s->s3.alpn_proposed != NULL) {
  1881. int r = s->ctx->ext.alpn_select_cb(s, &selected, &selected_len,
  1882. s->s3.alpn_proposed,
  1883. (unsigned int)s->s3.alpn_proposed_len,
  1884. s->ctx->ext.alpn_select_cb_arg);
  1885. if (r == SSL_TLSEXT_ERR_OK) {
  1886. OPENSSL_free(s->s3.alpn_selected);
  1887. s->s3.alpn_selected = OPENSSL_memdup(selected, selected_len);
  1888. if (s->s3.alpn_selected == NULL) {
  1889. s->s3.alpn_selected_len = 0;
  1890. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1891. return 0;
  1892. }
  1893. s->s3.alpn_selected_len = selected_len;
  1894. #ifndef OPENSSL_NO_NEXTPROTONEG
  1895. /* ALPN takes precedence over NPN. */
  1896. s->s3.npn_seen = 0;
  1897. #endif
  1898. /* Check ALPN is consistent with session */
  1899. if (s->session->ext.alpn_selected == NULL
  1900. || selected_len != s->session->ext.alpn_selected_len
  1901. || memcmp(selected, s->session->ext.alpn_selected,
  1902. selected_len) != 0) {
  1903. /* Not consistent so can't be used for early_data */
  1904. s->ext.early_data_ok = 0;
  1905. if (!s->hit) {
  1906. /*
  1907. * This is a new session and so alpn_selected should have
  1908. * been initialised to NULL. We should update it with the
  1909. * selected ALPN.
  1910. */
  1911. if (!ossl_assert(s->session->ext.alpn_selected == NULL)) {
  1912. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1913. ERR_R_INTERNAL_ERROR);
  1914. return 0;
  1915. }
  1916. s->session->ext.alpn_selected = OPENSSL_memdup(selected,
  1917. selected_len);
  1918. if (s->session->ext.alpn_selected == NULL) {
  1919. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1920. ERR_R_INTERNAL_ERROR);
  1921. return 0;
  1922. }
  1923. s->session->ext.alpn_selected_len = selected_len;
  1924. }
  1925. }
  1926. return 1;
  1927. } else if (r != SSL_TLSEXT_ERR_NOACK) {
  1928. SSLfatal(s, SSL_AD_NO_APPLICATION_PROTOCOL,
  1929. SSL_R_NO_APPLICATION_PROTOCOL);
  1930. return 0;
  1931. }
  1932. /*
  1933. * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was
  1934. * present.
  1935. */
  1936. }
  1937. /* Check ALPN is consistent with session */
  1938. if (s->session->ext.alpn_selected != NULL) {
  1939. /* Not consistent so can't be used for early_data */
  1940. s->ext.early_data_ok = 0;
  1941. }
  1942. return 1;
  1943. }
  1944. WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst)
  1945. {
  1946. const SSL_CIPHER *cipher;
  1947. if (wst == WORK_MORE_A) {
  1948. int rv = tls_early_post_process_client_hello(s);
  1949. if (rv == 0) {
  1950. /* SSLfatal() was already called */
  1951. goto err;
  1952. }
  1953. if (rv < 0)
  1954. return WORK_MORE_A;
  1955. wst = WORK_MORE_B;
  1956. }
  1957. if (wst == WORK_MORE_B) {
  1958. if (!s->hit || SSL_IS_TLS13(s)) {
  1959. /* Let cert callback update server certificates if required */
  1960. if (!s->hit && s->cert->cert_cb != NULL) {
  1961. int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
  1962. if (rv == 0) {
  1963. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CERT_CB_ERROR);
  1964. goto err;
  1965. }
  1966. if (rv < 0) {
  1967. s->rwstate = SSL_X509_LOOKUP;
  1968. return WORK_MORE_B;
  1969. }
  1970. s->rwstate = SSL_NOTHING;
  1971. }
  1972. /* In TLSv1.3 we selected the ciphersuite before resumption */
  1973. if (!SSL_IS_TLS13(s)) {
  1974. cipher =
  1975. ssl3_choose_cipher(s, s->peer_ciphers, SSL_get_ciphers(s));
  1976. if (cipher == NULL) {
  1977. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1978. SSL_R_NO_SHARED_CIPHER);
  1979. goto err;
  1980. }
  1981. s->s3.tmp.new_cipher = cipher;
  1982. }
  1983. if (!s->hit) {
  1984. if (!tls_choose_sigalg(s, 1)) {
  1985. /* SSLfatal already called */
  1986. goto err;
  1987. }
  1988. /* check whether we should disable session resumption */
  1989. if (s->not_resumable_session_cb != NULL)
  1990. s->session->not_resumable =
  1991. s->not_resumable_session_cb(s,
  1992. ((s->s3.tmp.new_cipher->algorithm_mkey
  1993. & (SSL_kDHE | SSL_kECDHE)) != 0));
  1994. if (s->session->not_resumable)
  1995. /* do not send a session ticket */
  1996. s->ext.ticket_expected = 0;
  1997. }
  1998. } else {
  1999. /* Session-id reuse */
  2000. s->s3.tmp.new_cipher = s->session->cipher;
  2001. }
  2002. /*-
  2003. * we now have the following setup.
  2004. * client_random
  2005. * cipher_list - our preferred list of ciphers
  2006. * ciphers - the clients preferred list of ciphers
  2007. * compression - basically ignored right now
  2008. * ssl version is set - sslv3
  2009. * s->session - The ssl session has been setup.
  2010. * s->hit - session reuse flag
  2011. * s->s3.tmp.new_cipher - the new cipher to use.
  2012. */
  2013. /*
  2014. * Call status_request callback if needed. Has to be done after the
  2015. * certificate callbacks etc above.
  2016. */
  2017. if (!tls_handle_status_request(s)) {
  2018. /* SSLfatal() already called */
  2019. goto err;
  2020. }
  2021. /*
  2022. * Call alpn_select callback if needed. Has to be done after SNI and
  2023. * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
  2024. * we already did this because cipher negotiation happens earlier, and
  2025. * we must handle ALPN before we decide whether to accept early_data.
  2026. */
  2027. if (!SSL_IS_TLS13(s) && !tls_handle_alpn(s)) {
  2028. /* SSLfatal() already called */
  2029. goto err;
  2030. }
  2031. wst = WORK_MORE_C;
  2032. }
  2033. #ifndef OPENSSL_NO_SRP
  2034. if (wst == WORK_MORE_C) {
  2035. int ret;
  2036. if ((ret = ssl_check_srp_ext_ClientHello(s)) == 0) {
  2037. /*
  2038. * callback indicates further work to be done
  2039. */
  2040. s->rwstate = SSL_X509_LOOKUP;
  2041. return WORK_MORE_C;
  2042. }
  2043. if (ret < 0) {
  2044. /* SSLfatal() already called */
  2045. goto err;
  2046. }
  2047. }
  2048. #endif
  2049. return WORK_FINISHED_STOP;
  2050. err:
  2051. return WORK_ERROR;
  2052. }
  2053. int tls_construct_server_hello(SSL *s, WPACKET *pkt)
  2054. {
  2055. int compm;
  2056. size_t sl, len;
  2057. int version;
  2058. unsigned char *session_id;
  2059. int usetls13 = SSL_IS_TLS13(s) || s->hello_retry_request == SSL_HRR_PENDING;
  2060. version = usetls13 ? TLS1_2_VERSION : s->version;
  2061. if (!WPACKET_put_bytes_u16(pkt, version)
  2062. /*
  2063. * Random stuff. Filling of the server_random takes place in
  2064. * tls_process_client_hello()
  2065. */
  2066. || !WPACKET_memcpy(pkt,
  2067. s->hello_retry_request == SSL_HRR_PENDING
  2068. ? hrrrandom : s->s3.server_random,
  2069. SSL3_RANDOM_SIZE)) {
  2070. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2071. return 0;
  2072. }
  2073. /*-
  2074. * There are several cases for the session ID to send
  2075. * back in the server hello:
  2076. * - For session reuse from the session cache,
  2077. * we send back the old session ID.
  2078. * - If stateless session reuse (using a session ticket)
  2079. * is successful, we send back the client's "session ID"
  2080. * (which doesn't actually identify the session).
  2081. * - If it is a new session, we send back the new
  2082. * session ID.
  2083. * - However, if we want the new session to be single-use,
  2084. * we send back a 0-length session ID.
  2085. * - In TLSv1.3 we echo back the session id sent to us by the client
  2086. * regardless
  2087. * s->hit is non-zero in either case of session reuse,
  2088. * so the following won't overwrite an ID that we're supposed
  2089. * to send back.
  2090. */
  2091. if (s->session->not_resumable ||
  2092. (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
  2093. && !s->hit))
  2094. s->session->session_id_length = 0;
  2095. if (usetls13) {
  2096. sl = s->tmp_session_id_len;
  2097. session_id = s->tmp_session_id;
  2098. } else {
  2099. sl = s->session->session_id_length;
  2100. session_id = s->session->session_id;
  2101. }
  2102. if (sl > sizeof(s->session->session_id)) {
  2103. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2104. return 0;
  2105. }
  2106. /* set up the compression method */
  2107. #ifdef OPENSSL_NO_COMP
  2108. compm = 0;
  2109. #else
  2110. if (usetls13 || s->s3.tmp.new_compression == NULL)
  2111. compm = 0;
  2112. else
  2113. compm = s->s3.tmp.new_compression->id;
  2114. #endif
  2115. if (!WPACKET_sub_memcpy_u8(pkt, session_id, sl)
  2116. || !s->method->put_cipher_by_char(s->s3.tmp.new_cipher, pkt, &len)
  2117. || !WPACKET_put_bytes_u8(pkt, compm)) {
  2118. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2119. return 0;
  2120. }
  2121. if (!tls_construct_extensions(s, pkt,
  2122. s->hello_retry_request == SSL_HRR_PENDING
  2123. ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
  2124. : (SSL_IS_TLS13(s)
  2125. ? SSL_EXT_TLS1_3_SERVER_HELLO
  2126. : SSL_EXT_TLS1_2_SERVER_HELLO),
  2127. NULL, 0)) {
  2128. /* SSLfatal() already called */
  2129. return 0;
  2130. }
  2131. if (s->hello_retry_request == SSL_HRR_PENDING) {
  2132. /* Ditch the session. We'll create a new one next time around */
  2133. SSL_SESSION_free(s->session);
  2134. s->session = NULL;
  2135. s->hit = 0;
  2136. /*
  2137. * Re-initialise the Transcript Hash. We're going to prepopulate it with
  2138. * a synthetic message_hash in place of ClientHello1.
  2139. */
  2140. if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) {
  2141. /* SSLfatal() already called */
  2142. return 0;
  2143. }
  2144. } else if (!(s->verify_mode & SSL_VERIFY_PEER)
  2145. && !ssl3_digest_cached_records(s, 0)) {
  2146. /* SSLfatal() already called */;
  2147. return 0;
  2148. }
  2149. return 1;
  2150. }
  2151. int tls_construct_server_done(SSL *s, WPACKET *pkt)
  2152. {
  2153. if (!s->s3.tmp.cert_request) {
  2154. if (!ssl3_digest_cached_records(s, 0)) {
  2155. /* SSLfatal() already called */
  2156. return 0;
  2157. }
  2158. }
  2159. return 1;
  2160. }
  2161. int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt)
  2162. {
  2163. EVP_PKEY *pkdh = NULL;
  2164. unsigned char *encodedPoint = NULL;
  2165. size_t encodedlen = 0;
  2166. int curve_id = 0;
  2167. const SIGALG_LOOKUP *lu = s->s3.tmp.sigalg;
  2168. int i;
  2169. unsigned long type;
  2170. BIGNUM *r[4];
  2171. EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
  2172. EVP_PKEY_CTX *pctx = NULL;
  2173. size_t paramlen, paramoffset;
  2174. int freer = 0, ret = 0;
  2175. if (!WPACKET_get_total_written(pkt, &paramoffset)) {
  2176. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2177. goto err;
  2178. }
  2179. if (md_ctx == NULL) {
  2180. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  2181. goto err;
  2182. }
  2183. type = s->s3.tmp.new_cipher->algorithm_mkey;
  2184. r[0] = r[1] = r[2] = r[3] = NULL;
  2185. #ifndef OPENSSL_NO_PSK
  2186. /* Plain PSK or RSAPSK nothing to do */
  2187. if (type & (SSL_kPSK | SSL_kRSAPSK)) {
  2188. } else
  2189. #endif /* !OPENSSL_NO_PSK */
  2190. if (type & (SSL_kDHE | SSL_kDHEPSK)) {
  2191. CERT *cert = s->cert;
  2192. EVP_PKEY *pkdhp = NULL;
  2193. if (s->cert->dh_tmp_auto) {
  2194. pkdh = ssl_get_auto_dh(s);
  2195. if (pkdh == NULL) {
  2196. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2197. goto err;
  2198. }
  2199. pkdhp = pkdh;
  2200. } else {
  2201. pkdhp = cert->dh_tmp;
  2202. }
  2203. #if !defined(OPENSSL_NO_DEPRECATED_3_0)
  2204. if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {
  2205. pkdh = ssl_dh_to_pkey(s->cert->dh_tmp_cb(s, 0, 1024));
  2206. if (pkdh == NULL) {
  2207. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2208. goto err;
  2209. }
  2210. pkdhp = pkdh;
  2211. }
  2212. #endif
  2213. if (pkdhp == NULL) {
  2214. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY);
  2215. goto err;
  2216. }
  2217. if (!ssl_security(s, SSL_SECOP_TMP_DH,
  2218. EVP_PKEY_get_security_bits(pkdhp), 0, pkdhp)) {
  2219. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DH_KEY_TOO_SMALL);
  2220. goto err;
  2221. }
  2222. if (s->s3.tmp.pkey != NULL) {
  2223. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2224. goto err;
  2225. }
  2226. s->s3.tmp.pkey = ssl_generate_pkey(s, pkdhp);
  2227. if (s->s3.tmp.pkey == NULL) {
  2228. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2229. goto err;
  2230. }
  2231. EVP_PKEY_free(pkdh);
  2232. pkdh = NULL;
  2233. /* These BIGNUMs need to be freed when we're finished */
  2234. freer = 1;
  2235. if (!EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_P,
  2236. &r[0])
  2237. || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_G,
  2238. &r[1])
  2239. || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey,
  2240. OSSL_PKEY_PARAM_PUB_KEY, &r[2])) {
  2241. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2242. goto err;
  2243. }
  2244. } else if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
  2245. if (s->s3.tmp.pkey != NULL) {
  2246. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2247. goto err;
  2248. }
  2249. /* Get NID of appropriate shared curve */
  2250. curve_id = tls1_shared_group(s, -2);
  2251. if (curve_id == 0) {
  2252. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  2253. SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
  2254. goto err;
  2255. }
  2256. /* Cache the group used in the SSL_SESSION */
  2257. s->session->kex_group = curve_id;
  2258. /* Generate a new key for this curve */
  2259. s->s3.tmp.pkey = ssl_generate_pkey_group(s, curve_id);
  2260. if (s->s3.tmp.pkey == NULL) {
  2261. /* SSLfatal() already called */
  2262. goto err;
  2263. }
  2264. /* Encode the public key. */
  2265. encodedlen = EVP_PKEY_get1_encoded_public_key(s->s3.tmp.pkey,
  2266. &encodedPoint);
  2267. if (encodedlen == 0) {
  2268. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
  2269. goto err;
  2270. }
  2271. /*
  2272. * We'll generate the serverKeyExchange message explicitly so we
  2273. * can set these to NULLs
  2274. */
  2275. r[0] = NULL;
  2276. r[1] = NULL;
  2277. r[2] = NULL;
  2278. r[3] = NULL;
  2279. } else
  2280. #ifndef OPENSSL_NO_SRP
  2281. if (type & SSL_kSRP) {
  2282. if ((s->srp_ctx.N == NULL) ||
  2283. (s->srp_ctx.g == NULL) ||
  2284. (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
  2285. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_SRP_PARAM);
  2286. goto err;
  2287. }
  2288. r[0] = s->srp_ctx.N;
  2289. r[1] = s->srp_ctx.g;
  2290. r[2] = s->srp_ctx.s;
  2291. r[3] = s->srp_ctx.B;
  2292. } else
  2293. #endif
  2294. {
  2295. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  2296. goto err;
  2297. }
  2298. if (((s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0)
  2299. || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) {
  2300. lu = NULL;
  2301. } else if (lu == NULL) {
  2302. SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
  2303. goto err;
  2304. }
  2305. #ifndef OPENSSL_NO_PSK
  2306. if (type & SSL_PSK) {
  2307. size_t len = (s->cert->psk_identity_hint == NULL)
  2308. ? 0 : strlen(s->cert->psk_identity_hint);
  2309. /*
  2310. * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
  2311. * checked this when we set the identity hint - but just in case
  2312. */
  2313. if (len > PSK_MAX_IDENTITY_LEN
  2314. || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,
  2315. len)) {
  2316. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2317. goto err;
  2318. }
  2319. }
  2320. #endif
  2321. for (i = 0; i < 4 && r[i] != NULL; i++) {
  2322. unsigned char *binval;
  2323. int res;
  2324. #ifndef OPENSSL_NO_SRP
  2325. if ((i == 2) && (type & SSL_kSRP)) {
  2326. res = WPACKET_start_sub_packet_u8(pkt);
  2327. } else
  2328. #endif
  2329. res = WPACKET_start_sub_packet_u16(pkt);
  2330. if (!res) {
  2331. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2332. goto err;
  2333. }
  2334. /*-
  2335. * for interoperability with some versions of the Microsoft TLS
  2336. * stack, we need to zero pad the DHE pub key to the same length
  2337. * as the prime
  2338. */
  2339. if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) {
  2340. size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]);
  2341. if (len > 0) {
  2342. if (!WPACKET_allocate_bytes(pkt, len, &binval)) {
  2343. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2344. goto err;
  2345. }
  2346. memset(binval, 0, len);
  2347. }
  2348. }
  2349. if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval)
  2350. || !WPACKET_close(pkt)) {
  2351. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2352. goto err;
  2353. }
  2354. BN_bn2bin(r[i], binval);
  2355. }
  2356. if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
  2357. /*
  2358. * We only support named (not generic) curves. In this situation, the
  2359. * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
  2360. * [1 byte length of encoded point], followed by the actual encoded
  2361. * point itself
  2362. */
  2363. if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE)
  2364. || !WPACKET_put_bytes_u8(pkt, 0)
  2365. || !WPACKET_put_bytes_u8(pkt, curve_id)
  2366. || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) {
  2367. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2368. goto err;
  2369. }
  2370. OPENSSL_free(encodedPoint);
  2371. encodedPoint = NULL;
  2372. }
  2373. /* not anonymous */
  2374. if (lu != NULL) {
  2375. EVP_PKEY *pkey = s->s3.tmp.cert->privatekey;
  2376. const EVP_MD *md;
  2377. unsigned char *sigbytes1, *sigbytes2, *tbs;
  2378. size_t siglen = 0, tbslen;
  2379. if (pkey == NULL || !tls1_lookup_md(s->ctx, lu, &md)) {
  2380. /* Should never happen */
  2381. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2382. goto err;
  2383. }
  2384. /* Get length of the parameters we have written above */
  2385. if (!WPACKET_get_length(pkt, &paramlen)) {
  2386. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2387. goto err;
  2388. }
  2389. /* send signature algorithm */
  2390. if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {
  2391. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2392. goto err;
  2393. }
  2394. if (EVP_DigestSignInit_ex(md_ctx, &pctx,
  2395. md == NULL ? NULL : EVP_MD_get0_name(md),
  2396. s->ctx->libctx, s->ctx->propq, pkey,
  2397. NULL) <= 0) {
  2398. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2399. goto err;
  2400. }
  2401. if (lu->sig == EVP_PKEY_RSA_PSS) {
  2402. if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
  2403. || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) {
  2404. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  2405. goto err;
  2406. }
  2407. }
  2408. tbslen = construct_key_exchange_tbs(s, &tbs,
  2409. s->init_buf->data + paramoffset,
  2410. paramlen);
  2411. if (tbslen == 0) {
  2412. /* SSLfatal() already called */
  2413. goto err;
  2414. }
  2415. if (EVP_DigestSign(md_ctx, NULL, &siglen, tbs, tbslen) <=0
  2416. || !WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1)
  2417. || EVP_DigestSign(md_ctx, sigbytes1, &siglen, tbs, tbslen) <= 0
  2418. || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2)
  2419. || sigbytes1 != sigbytes2) {
  2420. OPENSSL_free(tbs);
  2421. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2422. goto err;
  2423. }
  2424. OPENSSL_free(tbs);
  2425. }
  2426. ret = 1;
  2427. err:
  2428. EVP_PKEY_free(pkdh);
  2429. OPENSSL_free(encodedPoint);
  2430. EVP_MD_CTX_free(md_ctx);
  2431. if (freer) {
  2432. BN_free(r[0]);
  2433. BN_free(r[1]);
  2434. BN_free(r[2]);
  2435. BN_free(r[3]);
  2436. }
  2437. return ret;
  2438. }
  2439. int tls_construct_certificate_request(SSL *s, WPACKET *pkt)
  2440. {
  2441. if (SSL_IS_TLS13(s)) {
  2442. /* Send random context when doing post-handshake auth */
  2443. if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
  2444. OPENSSL_free(s->pha_context);
  2445. s->pha_context_len = 32;
  2446. if ((s->pha_context = OPENSSL_malloc(s->pha_context_len)) == NULL) {
  2447. s->pha_context_len = 0;
  2448. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2449. return 0;
  2450. }
  2451. if (RAND_bytes_ex(s->ctx->libctx, s->pha_context,
  2452. s->pha_context_len, 0) <= 0
  2453. || !WPACKET_sub_memcpy_u8(pkt, s->pha_context,
  2454. s->pha_context_len)) {
  2455. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2456. return 0;
  2457. }
  2458. /* reset the handshake hash back to just after the ClientFinished */
  2459. if (!tls13_restore_handshake_digest_for_pha(s)) {
  2460. /* SSLfatal() already called */
  2461. return 0;
  2462. }
  2463. } else {
  2464. if (!WPACKET_put_bytes_u8(pkt, 0)) {
  2465. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2466. return 0;
  2467. }
  2468. }
  2469. if (!tls_construct_extensions(s, pkt,
  2470. SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, NULL,
  2471. 0)) {
  2472. /* SSLfatal() already called */
  2473. return 0;
  2474. }
  2475. goto done;
  2476. }
  2477. /* get the list of acceptable cert types */
  2478. if (!WPACKET_start_sub_packet_u8(pkt)
  2479. || !ssl3_get_req_cert_type(s, pkt) || !WPACKET_close(pkt)) {
  2480. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2481. return 0;
  2482. }
  2483. if (SSL_USE_SIGALGS(s)) {
  2484. const uint16_t *psigs;
  2485. size_t nl = tls12_get_psigalgs(s, 1, &psigs);
  2486. if (!WPACKET_start_sub_packet_u16(pkt)
  2487. || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
  2488. || !tls12_copy_sigalgs(s, pkt, psigs, nl)
  2489. || !WPACKET_close(pkt)) {
  2490. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2491. return 0;
  2492. }
  2493. }
  2494. if (!construct_ca_names(s, get_ca_names(s), pkt)) {
  2495. /* SSLfatal() already called */
  2496. return 0;
  2497. }
  2498. done:
  2499. s->certreqs_sent++;
  2500. s->s3.tmp.cert_request = 1;
  2501. return 1;
  2502. }
  2503. static int tls_process_cke_psk_preamble(SSL *s, PACKET *pkt)
  2504. {
  2505. #ifndef OPENSSL_NO_PSK
  2506. unsigned char psk[PSK_MAX_PSK_LEN];
  2507. size_t psklen;
  2508. PACKET psk_identity;
  2509. if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
  2510. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2511. return 0;
  2512. }
  2513. if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
  2514. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DATA_LENGTH_TOO_LONG);
  2515. return 0;
  2516. }
  2517. if (s->psk_server_callback == NULL) {
  2518. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PSK_NO_SERVER_CB);
  2519. return 0;
  2520. }
  2521. if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
  2522. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2523. return 0;
  2524. }
  2525. psklen = s->psk_server_callback(s, s->session->psk_identity,
  2526. psk, sizeof(psk));
  2527. if (psklen > PSK_MAX_PSK_LEN) {
  2528. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2529. return 0;
  2530. } else if (psklen == 0) {
  2531. /*
  2532. * PSK related to the given identity not found
  2533. */
  2534. SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY, SSL_R_PSK_IDENTITY_NOT_FOUND);
  2535. return 0;
  2536. }
  2537. OPENSSL_free(s->s3.tmp.psk);
  2538. s->s3.tmp.psk = OPENSSL_memdup(psk, psklen);
  2539. OPENSSL_cleanse(psk, psklen);
  2540. if (s->s3.tmp.psk == NULL) {
  2541. s->s3.tmp.psklen = 0;
  2542. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  2543. return 0;
  2544. }
  2545. s->s3.tmp.psklen = psklen;
  2546. return 1;
  2547. #else
  2548. /* Should never happen */
  2549. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2550. return 0;
  2551. #endif
  2552. }
  2553. static int tls_process_cke_rsa(SSL *s, PACKET *pkt)
  2554. {
  2555. size_t outlen;
  2556. PACKET enc_premaster;
  2557. EVP_PKEY *rsa = NULL;
  2558. unsigned char *rsa_decrypt = NULL;
  2559. int ret = 0;
  2560. EVP_PKEY_CTX *ctx = NULL;
  2561. OSSL_PARAM params[3], *p = params;
  2562. rsa = s->cert->pkeys[SSL_PKEY_RSA].privatekey;
  2563. if (rsa == NULL) {
  2564. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_RSA_CERTIFICATE);
  2565. return 0;
  2566. }
  2567. /* SSLv3 and pre-standard DTLS omit the length bytes. */
  2568. if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
  2569. enc_premaster = *pkt;
  2570. } else {
  2571. if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
  2572. || PACKET_remaining(pkt) != 0) {
  2573. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2574. return 0;
  2575. }
  2576. }
  2577. outlen = SSL_MAX_MASTER_KEY_LENGTH;
  2578. rsa_decrypt = OPENSSL_malloc(outlen);
  2579. if (rsa_decrypt == NULL) {
  2580. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  2581. return 0;
  2582. }
  2583. ctx = EVP_PKEY_CTX_new_from_pkey(s->ctx->libctx, rsa, s->ctx->propq);
  2584. if (ctx == NULL) {
  2585. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  2586. goto err;
  2587. }
  2588. /*
  2589. * We must not leak whether a decryption failure occurs because of
  2590. * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
  2591. * section 7.4.7.1). We use the special padding type
  2592. * RSA_PKCS1_WITH_TLS_PADDING to do that. It will automatically decrypt the
  2593. * RSA, check the padding and check that the client version is as expected
  2594. * in the premaster secret. If any of that fails then the function appears
  2595. * to return successfully but with a random result. The call below could
  2596. * still fail if the input is publicly invalid.
  2597. * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
  2598. */
  2599. if (EVP_PKEY_decrypt_init(ctx) <= 0
  2600. || EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_WITH_TLS_PADDING) <= 0) {
  2601. SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);
  2602. goto err;
  2603. }
  2604. *p++ = OSSL_PARAM_construct_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION,
  2605. (unsigned int *)&s->client_version);
  2606. if ((s->options & SSL_OP_TLS_ROLLBACK_BUG) != 0)
  2607. *p++ = OSSL_PARAM_construct_uint(
  2608. OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION,
  2609. (unsigned int *)&s->version);
  2610. *p++ = OSSL_PARAM_construct_end();
  2611. if (!EVP_PKEY_CTX_set_params(ctx, params)
  2612. || EVP_PKEY_decrypt(ctx, rsa_decrypt, &outlen,
  2613. PACKET_data(&enc_premaster),
  2614. PACKET_remaining(&enc_premaster)) <= 0) {
  2615. SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);
  2616. goto err;
  2617. }
  2618. /*
  2619. * This test should never fail (otherwise we should have failed above) but
  2620. * we double check anyway.
  2621. */
  2622. if (outlen != SSL_MAX_MASTER_KEY_LENGTH) {
  2623. OPENSSL_cleanse(rsa_decrypt, SSL_MAX_MASTER_KEY_LENGTH);
  2624. SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);
  2625. goto err;
  2626. }
  2627. /* Also cleanses rsa_decrypt (on success or failure) */
  2628. if (!ssl_generate_master_secret(s, rsa_decrypt,
  2629. SSL_MAX_MASTER_KEY_LENGTH, 0)) {
  2630. /* SSLfatal() already called */
  2631. goto err;
  2632. }
  2633. ret = 1;
  2634. err:
  2635. OPENSSL_free(rsa_decrypt);
  2636. EVP_PKEY_CTX_free(ctx);
  2637. return ret;
  2638. }
  2639. static int tls_process_cke_dhe(SSL *s, PACKET *pkt)
  2640. {
  2641. EVP_PKEY *skey = NULL;
  2642. unsigned int i;
  2643. const unsigned char *data;
  2644. EVP_PKEY *ckey = NULL;
  2645. int ret = 0;
  2646. if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) {
  2647. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
  2648. goto err;
  2649. }
  2650. skey = s->s3.tmp.pkey;
  2651. if (skey == NULL) {
  2652. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY);
  2653. goto err;
  2654. }
  2655. if (PACKET_remaining(pkt) == 0L) {
  2656. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_MISSING_TMP_DH_KEY);
  2657. goto err;
  2658. }
  2659. if (!PACKET_get_bytes(pkt, &data, i)) {
  2660. /* We already checked we have enough data */
  2661. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2662. goto err;
  2663. }
  2664. ckey = EVP_PKEY_new();
  2665. if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {
  2666. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);
  2667. goto err;
  2668. }
  2669. if (!EVP_PKEY_set1_encoded_public_key(ckey, data, i)) {
  2670. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2671. goto err;
  2672. }
  2673. if (ssl_derive(s, skey, ckey, 1) == 0) {
  2674. /* SSLfatal() already called */
  2675. goto err;
  2676. }
  2677. ret = 1;
  2678. EVP_PKEY_free(s->s3.tmp.pkey);
  2679. s->s3.tmp.pkey = NULL;
  2680. err:
  2681. EVP_PKEY_free(ckey);
  2682. return ret;
  2683. }
  2684. static int tls_process_cke_ecdhe(SSL *s, PACKET *pkt)
  2685. {
  2686. EVP_PKEY *skey = s->s3.tmp.pkey;
  2687. EVP_PKEY *ckey = NULL;
  2688. int ret = 0;
  2689. if (PACKET_remaining(pkt) == 0L) {
  2690. /* We don't support ECDH client auth */
  2691. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_MISSING_TMP_ECDH_KEY);
  2692. goto err;
  2693. } else {
  2694. unsigned int i;
  2695. const unsigned char *data;
  2696. /*
  2697. * Get client's public key from encoded point in the
  2698. * ClientKeyExchange message.
  2699. */
  2700. /* Get encoded point length */
  2701. if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i)
  2702. || PACKET_remaining(pkt) != 0) {
  2703. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2704. goto err;
  2705. }
  2706. if (skey == NULL) {
  2707. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_ECDH_KEY);
  2708. goto err;
  2709. }
  2710. ckey = EVP_PKEY_new();
  2711. if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {
  2712. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);
  2713. goto err;
  2714. }
  2715. if (EVP_PKEY_set1_encoded_public_key(ckey, data, i) <= 0) {
  2716. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
  2717. goto err;
  2718. }
  2719. }
  2720. if (ssl_derive(s, skey, ckey, 1) == 0) {
  2721. /* SSLfatal() already called */
  2722. goto err;
  2723. }
  2724. ret = 1;
  2725. EVP_PKEY_free(s->s3.tmp.pkey);
  2726. s->s3.tmp.pkey = NULL;
  2727. err:
  2728. EVP_PKEY_free(ckey);
  2729. return ret;
  2730. }
  2731. static int tls_process_cke_srp(SSL *s, PACKET *pkt)
  2732. {
  2733. #ifndef OPENSSL_NO_SRP
  2734. unsigned int i;
  2735. const unsigned char *data;
  2736. if (!PACKET_get_net_2(pkt, &i)
  2737. || !PACKET_get_bytes(pkt, &data, i)) {
  2738. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_SRP_A_LENGTH);
  2739. return 0;
  2740. }
  2741. if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
  2742. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB);
  2743. return 0;
  2744. }
  2745. if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) {
  2746. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_SRP_PARAMETERS);
  2747. return 0;
  2748. }
  2749. OPENSSL_free(s->session->srp_username);
  2750. s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
  2751. if (s->session->srp_username == NULL) {
  2752. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  2753. return 0;
  2754. }
  2755. if (!srp_generate_server_master_secret(s)) {
  2756. /* SSLfatal() already called */
  2757. return 0;
  2758. }
  2759. return 1;
  2760. #else
  2761. /* Should never happen */
  2762. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2763. return 0;
  2764. #endif
  2765. }
  2766. static int tls_process_cke_gost(SSL *s, PACKET *pkt)
  2767. {
  2768. #ifndef OPENSSL_NO_GOST
  2769. EVP_PKEY_CTX *pkey_ctx;
  2770. EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
  2771. unsigned char premaster_secret[32];
  2772. const unsigned char *start;
  2773. size_t outlen = 32, inlen;
  2774. unsigned long alg_a;
  2775. GOST_KX_MESSAGE *pKX = NULL;
  2776. const unsigned char *ptr;
  2777. int ret = 0;
  2778. /* Get our certificate private key */
  2779. alg_a = s->s3.tmp.new_cipher->algorithm_auth;
  2780. if (alg_a & SSL_aGOST12) {
  2781. /*
  2782. * New GOST ciphersuites have SSL_aGOST01 bit too
  2783. */
  2784. pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
  2785. if (pk == NULL) {
  2786. pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
  2787. }
  2788. if (pk == NULL) {
  2789. pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
  2790. }
  2791. } else if (alg_a & SSL_aGOST01) {
  2792. pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
  2793. }
  2794. pkey_ctx = EVP_PKEY_CTX_new_from_pkey(s->ctx->libctx, pk, s->ctx->propq);
  2795. if (pkey_ctx == NULL) {
  2796. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  2797. return 0;
  2798. }
  2799. if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
  2800. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2801. return 0;
  2802. }
  2803. /*
  2804. * If client certificate is present and is of the same type, maybe
  2805. * use it for key exchange. Don't mind errors from
  2806. * EVP_PKEY_derive_set_peer, because it is completely valid to use a
  2807. * client certificate for authorization only.
  2808. */
  2809. client_pub_pkey = X509_get0_pubkey(s->session->peer);
  2810. if (client_pub_pkey) {
  2811. if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
  2812. ERR_clear_error();
  2813. }
  2814. ptr = PACKET_data(pkt);
  2815. /* Some implementations provide extra data in the opaqueBlob
  2816. * We have nothing to do with this blob so we just skip it */
  2817. pKX = d2i_GOST_KX_MESSAGE(NULL, &ptr, PACKET_remaining(pkt));
  2818. if (pKX == NULL
  2819. || pKX->kxBlob == NULL
  2820. || ASN1_TYPE_get(pKX->kxBlob) != V_ASN1_SEQUENCE) {
  2821. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);
  2822. goto err;
  2823. }
  2824. if (!PACKET_forward(pkt, ptr - PACKET_data(pkt))) {
  2825. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED);
  2826. goto err;
  2827. }
  2828. if (PACKET_remaining(pkt) != 0) {
  2829. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED);
  2830. goto err;
  2831. }
  2832. inlen = pKX->kxBlob->value.sequence->length;
  2833. start = pKX->kxBlob->value.sequence->data;
  2834. if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start,
  2835. inlen) <= 0) {
  2836. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);
  2837. goto err;
  2838. }
  2839. /* Generate master secret */
  2840. if (!ssl_generate_master_secret(s, premaster_secret,
  2841. sizeof(premaster_secret), 0)) {
  2842. /* SSLfatal() already called */
  2843. goto err;
  2844. }
  2845. /* Check if pubkey from client certificate was used */
  2846. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2,
  2847. NULL) > 0)
  2848. s->statem.no_cert_verify = 1;
  2849. ret = 1;
  2850. err:
  2851. EVP_PKEY_CTX_free(pkey_ctx);
  2852. GOST_KX_MESSAGE_free(pKX);
  2853. return ret;
  2854. #else
  2855. /* Should never happen */
  2856. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2857. return 0;
  2858. #endif
  2859. }
  2860. static int tls_process_cke_gost18(SSL *s, PACKET *pkt)
  2861. {
  2862. #ifndef OPENSSL_NO_GOST
  2863. unsigned char rnd_dgst[32];
  2864. EVP_PKEY_CTX *pkey_ctx = NULL;
  2865. EVP_PKEY *pk = NULL;
  2866. unsigned char premaster_secret[32];
  2867. const unsigned char *start = NULL;
  2868. size_t outlen = 32, inlen = 0;
  2869. int ret = 0;
  2870. int cipher_nid = ossl_gost18_cke_cipher_nid(s);
  2871. if (cipher_nid == NID_undef) {
  2872. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2873. return 0;
  2874. }
  2875. if (ossl_gost_ukm(s, rnd_dgst) <= 0) {
  2876. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2877. goto err;
  2878. }
  2879. /* Get our certificate private key */
  2880. pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey != NULL ?
  2881. s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey :
  2882. s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
  2883. if (pk == NULL) {
  2884. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE);
  2885. goto err;
  2886. }
  2887. pkey_ctx = EVP_PKEY_CTX_new_from_pkey(s->ctx->libctx, pk, s->ctx->propq);
  2888. if (pkey_ctx == NULL) {
  2889. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  2890. goto err;
  2891. }
  2892. if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
  2893. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2894. goto err;
  2895. }
  2896. /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code depending on size */
  2897. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT,
  2898. EVP_PKEY_CTRL_SET_IV, 32, rnd_dgst) <= 0) {
  2899. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
  2900. goto err;
  2901. }
  2902. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT,
  2903. EVP_PKEY_CTRL_CIPHER, cipher_nid, NULL) <= 0) {
  2904. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
  2905. goto err;
  2906. }
  2907. inlen = PACKET_remaining(pkt);
  2908. start = PACKET_data(pkt);
  2909. if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
  2910. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);
  2911. goto err;
  2912. }
  2913. /* Generate master secret */
  2914. if (!ssl_generate_master_secret(s, premaster_secret,
  2915. sizeof(premaster_secret), 0)) {
  2916. /* SSLfatal() already called */
  2917. goto err;
  2918. }
  2919. ret = 1;
  2920. err:
  2921. EVP_PKEY_CTX_free(pkey_ctx);
  2922. return ret;
  2923. #else
  2924. /* Should never happen */
  2925. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  2926. return 0;
  2927. #endif
  2928. }
  2929. MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt)
  2930. {
  2931. unsigned long alg_k;
  2932. alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
  2933. /* For PSK parse and retrieve identity, obtain PSK key */
  2934. if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt)) {
  2935. /* SSLfatal() already called */
  2936. goto err;
  2937. }
  2938. if (alg_k & SSL_kPSK) {
  2939. /* Identity extracted earlier: should be nothing left */
  2940. if (PACKET_remaining(pkt) != 0) {
  2941. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  2942. goto err;
  2943. }
  2944. /* PSK handled by ssl_generate_master_secret */
  2945. if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
  2946. /* SSLfatal() already called */
  2947. goto err;
  2948. }
  2949. } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
  2950. if (!tls_process_cke_rsa(s, pkt)) {
  2951. /* SSLfatal() already called */
  2952. goto err;
  2953. }
  2954. } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
  2955. if (!tls_process_cke_dhe(s, pkt)) {
  2956. /* SSLfatal() already called */
  2957. goto err;
  2958. }
  2959. } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
  2960. if (!tls_process_cke_ecdhe(s, pkt)) {
  2961. /* SSLfatal() already called */
  2962. goto err;
  2963. }
  2964. } else if (alg_k & SSL_kSRP) {
  2965. if (!tls_process_cke_srp(s, pkt)) {
  2966. /* SSLfatal() already called */
  2967. goto err;
  2968. }
  2969. } else if (alg_k & SSL_kGOST) {
  2970. if (!tls_process_cke_gost(s, pkt)) {
  2971. /* SSLfatal() already called */
  2972. goto err;
  2973. }
  2974. } else if (alg_k & SSL_kGOST18) {
  2975. if (!tls_process_cke_gost18(s, pkt)) {
  2976. /* SSLfatal() already called */
  2977. goto err;
  2978. }
  2979. } else {
  2980. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_CIPHER_TYPE);
  2981. goto err;
  2982. }
  2983. return MSG_PROCESS_CONTINUE_PROCESSING;
  2984. err:
  2985. #ifndef OPENSSL_NO_PSK
  2986. OPENSSL_clear_free(s->s3.tmp.psk, s->s3.tmp.psklen);
  2987. s->s3.tmp.psk = NULL;
  2988. s->s3.tmp.psklen = 0;
  2989. #endif
  2990. return MSG_PROCESS_ERROR;
  2991. }
  2992. WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst)
  2993. {
  2994. #ifndef OPENSSL_NO_SCTP
  2995. if (wst == WORK_MORE_A) {
  2996. if (SSL_IS_DTLS(s)) {
  2997. unsigned char sctpauthkey[64];
  2998. char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
  2999. size_t labellen;
  3000. /*
  3001. * Add new shared key for SCTP-Auth, will be ignored if no SCTP
  3002. * used.
  3003. */
  3004. memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
  3005. sizeof(DTLS1_SCTP_AUTH_LABEL));
  3006. /* Don't include the terminating zero. */
  3007. labellen = sizeof(labelbuffer) - 1;
  3008. if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
  3009. labellen += 1;
  3010. if (SSL_export_keying_material(s, sctpauthkey,
  3011. sizeof(sctpauthkey), labelbuffer,
  3012. labellen, NULL, 0,
  3013. 0) <= 0) {
  3014. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3015. return WORK_ERROR;
  3016. }
  3017. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  3018. sizeof(sctpauthkey), sctpauthkey);
  3019. }
  3020. }
  3021. #endif
  3022. if (s->statem.no_cert_verify || !s->session->peer) {
  3023. /*
  3024. * No certificate verify or no peer certificate so we no longer need
  3025. * the handshake_buffer
  3026. */
  3027. if (!ssl3_digest_cached_records(s, 0)) {
  3028. /* SSLfatal() already called */
  3029. return WORK_ERROR;
  3030. }
  3031. return WORK_FINISHED_CONTINUE;
  3032. } else {
  3033. if (!s->s3.handshake_buffer) {
  3034. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3035. return WORK_ERROR;
  3036. }
  3037. /*
  3038. * For sigalgs freeze the handshake buffer. If we support
  3039. * extms we've done this already so this is a no-op
  3040. */
  3041. if (!ssl3_digest_cached_records(s, 1)) {
  3042. /* SSLfatal() already called */
  3043. return WORK_ERROR;
  3044. }
  3045. }
  3046. return WORK_FINISHED_CONTINUE;
  3047. }
  3048. MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt)
  3049. {
  3050. int i;
  3051. MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
  3052. X509 *x = NULL;
  3053. unsigned long l;
  3054. const unsigned char *certstart, *certbytes;
  3055. STACK_OF(X509) *sk = NULL;
  3056. PACKET spkt, context;
  3057. size_t chainidx;
  3058. SSL_SESSION *new_sess = NULL;
  3059. /*
  3060. * To get this far we must have read encrypted data from the client. We no
  3061. * longer tolerate unencrypted alerts. This value is ignored if less than
  3062. * TLSv1.3
  3063. */
  3064. s->statem.enc_read_state = ENC_READ_STATE_VALID;
  3065. if ((sk = sk_X509_new_null()) == NULL) {
  3066. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  3067. goto err;
  3068. }
  3069. if (SSL_IS_TLS13(s) && (!PACKET_get_length_prefixed_1(pkt, &context)
  3070. || (s->pha_context == NULL && PACKET_remaining(&context) != 0)
  3071. || (s->pha_context != NULL &&
  3072. !PACKET_equal(&context, s->pha_context, s->pha_context_len)))) {
  3073. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_INVALID_CONTEXT);
  3074. goto err;
  3075. }
  3076. if (!PACKET_get_length_prefixed_3(pkt, &spkt)
  3077. || PACKET_remaining(pkt) != 0) {
  3078. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  3079. goto err;
  3080. }
  3081. for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) {
  3082. if (!PACKET_get_net_3(&spkt, &l)
  3083. || !PACKET_get_bytes(&spkt, &certbytes, l)) {
  3084. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
  3085. goto err;
  3086. }
  3087. certstart = certbytes;
  3088. x = X509_new_ex(s->ctx->libctx, s->ctx->propq);
  3089. if (x == NULL) {
  3090. SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_MALLOC_FAILURE);
  3091. goto err;
  3092. }
  3093. if (d2i_X509(&x, (const unsigned char **)&certbytes, l) == NULL) {
  3094. SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_ASN1_LIB);
  3095. goto err;
  3096. }
  3097. if (certbytes != (certstart + l)) {
  3098. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
  3099. goto err;
  3100. }
  3101. if (SSL_IS_TLS13(s)) {
  3102. RAW_EXTENSION *rawexts = NULL;
  3103. PACKET extensions;
  3104. if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) {
  3105. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
  3106. goto err;
  3107. }
  3108. if (!tls_collect_extensions(s, &extensions,
  3109. SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
  3110. NULL, chainidx == 0)
  3111. || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
  3112. rawexts, x, chainidx,
  3113. PACKET_remaining(&spkt) == 0)) {
  3114. OPENSSL_free(rawexts);
  3115. goto err;
  3116. }
  3117. OPENSSL_free(rawexts);
  3118. }
  3119. if (!sk_X509_push(sk, x)) {
  3120. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  3121. goto err;
  3122. }
  3123. x = NULL;
  3124. }
  3125. if (sk_X509_num(sk) <= 0) {
  3126. /* TLS does not mind 0 certs returned */
  3127. if (s->version == SSL3_VERSION) {
  3128. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3129. SSL_R_NO_CERTIFICATES_RETURNED);
  3130. goto err;
  3131. }
  3132. /* Fail for TLS only if we required a certificate */
  3133. else if ((s->verify_mode & SSL_VERIFY_PEER) &&
  3134. (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
  3135. SSLfatal(s, SSL_AD_CERTIFICATE_REQUIRED,
  3136. SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  3137. goto err;
  3138. }
  3139. /* No client certificate so digest cached records */
  3140. if (s->s3.handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
  3141. /* SSLfatal() already called */
  3142. goto err;
  3143. }
  3144. } else {
  3145. EVP_PKEY *pkey;
  3146. i = ssl_verify_cert_chain(s, sk);
  3147. if (i <= 0) {
  3148. SSLfatal(s, ssl_x509err2alert(s->verify_result),
  3149. SSL_R_CERTIFICATE_VERIFY_FAILED);
  3150. goto err;
  3151. }
  3152. pkey = X509_get0_pubkey(sk_X509_value(sk, 0));
  3153. if (pkey == NULL) {
  3154. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  3155. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  3156. goto err;
  3157. }
  3158. }
  3159. /*
  3160. * Sessions must be immutable once they go into the session cache. Otherwise
  3161. * we can get multi-thread problems. Therefore we don't "update" sessions,
  3162. * we replace them with a duplicate. Here, we need to do this every time
  3163. * a new certificate is received via post-handshake authentication, as the
  3164. * session may have already gone into the session cache.
  3165. */
  3166. if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
  3167. if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
  3168. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  3169. goto err;
  3170. }
  3171. SSL_SESSION_free(s->session);
  3172. s->session = new_sess;
  3173. }
  3174. X509_free(s->session->peer);
  3175. s->session->peer = sk_X509_shift(sk);
  3176. s->session->verify_result = s->verify_result;
  3177. OSSL_STACK_OF_X509_free(s->session->peer_chain);
  3178. s->session->peer_chain = sk;
  3179. sk = NULL;
  3180. /*
  3181. * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
  3182. * message
  3183. */
  3184. if (SSL_IS_TLS13(s) && !ssl3_digest_cached_records(s, 1)) {
  3185. /* SSLfatal() already called */
  3186. goto err;
  3187. }
  3188. /*
  3189. * Inconsistency alert: cert_chain does *not* include the peer's own
  3190. * certificate, while we do include it in statem_clnt.c
  3191. */
  3192. /* Save the current hash state for when we receive the CertificateVerify */
  3193. if (SSL_IS_TLS13(s)) {
  3194. if (!ssl_handshake_hash(s, s->cert_verify_hash,
  3195. sizeof(s->cert_verify_hash),
  3196. &s->cert_verify_hash_len)) {
  3197. /* SSLfatal() already called */
  3198. goto err;
  3199. }
  3200. /* Resend session tickets */
  3201. s->sent_tickets = 0;
  3202. }
  3203. ret = MSG_PROCESS_CONTINUE_READING;
  3204. err:
  3205. X509_free(x);
  3206. OSSL_STACK_OF_X509_free(sk);
  3207. return ret;
  3208. }
  3209. int tls_construct_server_certificate(SSL *s, WPACKET *pkt)
  3210. {
  3211. CERT_PKEY *cpk = s->s3.tmp.cert;
  3212. if (cpk == NULL) {
  3213. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3214. return 0;
  3215. }
  3216. /*
  3217. * In TLSv1.3 the certificate chain is always preceded by a 0 length context
  3218. * for the server Certificate message
  3219. */
  3220. if (SSL_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0)) {
  3221. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3222. return 0;
  3223. }
  3224. if (!ssl3_output_cert_chain(s, pkt, cpk)) {
  3225. /* SSLfatal() already called */
  3226. return 0;
  3227. }
  3228. return 1;
  3229. }
  3230. static int create_ticket_prequel(SSL *s, WPACKET *pkt, uint32_t age_add,
  3231. unsigned char *tick_nonce)
  3232. {
  3233. uint32_t timeout = (uint32_t)s->session->timeout;
  3234. /*
  3235. * Ticket lifetime hint:
  3236. * In TLSv1.3 we reset the "time" field above, and always specify the
  3237. * timeout, limited to a 1 week period per RFC8446.
  3238. * For TLSv1.2 this is advisory only and we leave this unspecified for
  3239. * resumed session (for simplicity).
  3240. */
  3241. #define ONE_WEEK_SEC (7 * 24 * 60 * 60)
  3242. if (SSL_IS_TLS13(s)) {
  3243. if (s->session->timeout > ONE_WEEK_SEC)
  3244. timeout = ONE_WEEK_SEC;
  3245. } else if (s->hit)
  3246. timeout = 0;
  3247. if (!WPACKET_put_bytes_u32(pkt, timeout)) {
  3248. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3249. return 0;
  3250. }
  3251. if (SSL_IS_TLS13(s)) {
  3252. if (!WPACKET_put_bytes_u32(pkt, age_add)
  3253. || !WPACKET_sub_memcpy_u8(pkt, tick_nonce, TICKET_NONCE_SIZE)) {
  3254. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3255. return 0;
  3256. }
  3257. }
  3258. /* Start the sub-packet for the actual ticket data */
  3259. if (!WPACKET_start_sub_packet_u16(pkt)) {
  3260. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3261. return 0;
  3262. }
  3263. return 1;
  3264. }
  3265. static int construct_stateless_ticket(SSL *s, WPACKET *pkt, uint32_t age_add,
  3266. unsigned char *tick_nonce)
  3267. {
  3268. unsigned char *senc = NULL;
  3269. EVP_CIPHER_CTX *ctx = NULL;
  3270. SSL_HMAC *hctx = NULL;
  3271. unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2;
  3272. const unsigned char *const_p;
  3273. int len, slen_full, slen, lenfinal;
  3274. SSL_SESSION *sess;
  3275. size_t hlen;
  3276. SSL_CTX *tctx = s->session_ctx;
  3277. unsigned char iv[EVP_MAX_IV_LENGTH];
  3278. unsigned char key_name[TLSEXT_KEYNAME_LENGTH];
  3279. int iv_len, ok = 0;
  3280. size_t macoffset, macendoffset;
  3281. /* get session encoding length */
  3282. slen_full = i2d_SSL_SESSION(s->session, NULL);
  3283. /*
  3284. * Some length values are 16 bits, so forget it if session is too
  3285. * long
  3286. */
  3287. if (slen_full == 0 || slen_full > 0xFF00) {
  3288. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3289. goto err;
  3290. }
  3291. senc = OPENSSL_malloc(slen_full);
  3292. if (senc == NULL) {
  3293. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  3294. goto err;
  3295. }
  3296. ctx = EVP_CIPHER_CTX_new();
  3297. hctx = ssl_hmac_new(tctx);
  3298. if (ctx == NULL || hctx == NULL) {
  3299. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  3300. goto err;
  3301. }
  3302. p = senc;
  3303. if (!i2d_SSL_SESSION(s->session, &p)) {
  3304. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3305. goto err;
  3306. }
  3307. /*
  3308. * create a fresh copy (not shared with other threads) to clean up
  3309. */
  3310. const_p = senc;
  3311. sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
  3312. if (sess == NULL) {
  3313. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3314. goto err;
  3315. }
  3316. slen = i2d_SSL_SESSION(sess, NULL);
  3317. if (slen == 0 || slen > slen_full) {
  3318. /* shouldn't ever happen */
  3319. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3320. SSL_SESSION_free(sess);
  3321. goto err;
  3322. }
  3323. p = senc;
  3324. if (!i2d_SSL_SESSION(sess, &p)) {
  3325. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3326. SSL_SESSION_free(sess);
  3327. goto err;
  3328. }
  3329. SSL_SESSION_free(sess);
  3330. /*
  3331. * Initialize HMAC and cipher contexts. If callback present it does
  3332. * all the work otherwise use generated values from parent ctx.
  3333. */
  3334. #ifndef OPENSSL_NO_DEPRECATED_3_0
  3335. if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL)
  3336. #else
  3337. if (tctx->ext.ticket_key_evp_cb != NULL)
  3338. #endif
  3339. {
  3340. int ret = 0;
  3341. if (tctx->ext.ticket_key_evp_cb != NULL)
  3342. ret = tctx->ext.ticket_key_evp_cb(s, key_name, iv, ctx,
  3343. ssl_hmac_get0_EVP_MAC_CTX(hctx),
  3344. 1);
  3345. #ifndef OPENSSL_NO_DEPRECATED_3_0
  3346. else if (tctx->ext.ticket_key_cb != NULL)
  3347. /* if 0 is returned, write an empty ticket */
  3348. ret = tctx->ext.ticket_key_cb(s, key_name, iv, ctx,
  3349. ssl_hmac_get0_HMAC_CTX(hctx), 1);
  3350. #endif
  3351. if (ret == 0) {
  3352. /* Put timeout and length */
  3353. if (!WPACKET_put_bytes_u32(pkt, 0)
  3354. || !WPACKET_put_bytes_u16(pkt, 0)) {
  3355. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3356. goto err;
  3357. }
  3358. OPENSSL_free(senc);
  3359. EVP_CIPHER_CTX_free(ctx);
  3360. ssl_hmac_free(hctx);
  3361. return 1;
  3362. }
  3363. if (ret < 0) {
  3364. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED);
  3365. goto err;
  3366. }
  3367. iv_len = EVP_CIPHER_CTX_get_iv_length(ctx);
  3368. } else {
  3369. EVP_CIPHER *cipher = EVP_CIPHER_fetch(s->ctx->libctx, "AES-256-CBC",
  3370. s->ctx->propq);
  3371. if (cipher == NULL) {
  3372. /* Error is already recorded */
  3373. SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
  3374. goto err;
  3375. }
  3376. iv_len = EVP_CIPHER_get_iv_length(cipher);
  3377. if (iv_len < 0
  3378. || RAND_bytes_ex(s->ctx->libctx, iv, iv_len, 0) <= 0
  3379. || !EVP_EncryptInit_ex(ctx, cipher, NULL,
  3380. tctx->ext.secure->tick_aes_key, iv)
  3381. || !ssl_hmac_init(hctx, tctx->ext.secure->tick_hmac_key,
  3382. sizeof(tctx->ext.secure->tick_hmac_key),
  3383. "SHA256")) {
  3384. EVP_CIPHER_free(cipher);
  3385. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3386. goto err;
  3387. }
  3388. EVP_CIPHER_free(cipher);
  3389. memcpy(key_name, tctx->ext.tick_key_name,
  3390. sizeof(tctx->ext.tick_key_name));
  3391. }
  3392. if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
  3393. /* SSLfatal() already called */
  3394. goto err;
  3395. }
  3396. if (!WPACKET_get_total_written(pkt, &macoffset)
  3397. /* Output key name */
  3398. || !WPACKET_memcpy(pkt, key_name, sizeof(key_name))
  3399. /* output IV */
  3400. || !WPACKET_memcpy(pkt, iv, iv_len)
  3401. || !WPACKET_reserve_bytes(pkt, slen + EVP_MAX_BLOCK_LENGTH,
  3402. &encdata1)
  3403. /* Encrypt session data */
  3404. || !EVP_EncryptUpdate(ctx, encdata1, &len, senc, slen)
  3405. || !WPACKET_allocate_bytes(pkt, len, &encdata2)
  3406. || encdata1 != encdata2
  3407. || !EVP_EncryptFinal(ctx, encdata1 + len, &lenfinal)
  3408. || !WPACKET_allocate_bytes(pkt, lenfinal, &encdata2)
  3409. || encdata1 + len != encdata2
  3410. || len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH
  3411. || !WPACKET_get_total_written(pkt, &macendoffset)
  3412. || !ssl_hmac_update(hctx,
  3413. (unsigned char *)s->init_buf->data + macoffset,
  3414. macendoffset - macoffset)
  3415. || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1)
  3416. || !ssl_hmac_final(hctx, macdata1, &hlen, EVP_MAX_MD_SIZE)
  3417. || hlen > EVP_MAX_MD_SIZE
  3418. || !WPACKET_allocate_bytes(pkt, hlen, &macdata2)
  3419. || macdata1 != macdata2) {
  3420. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3421. goto err;
  3422. }
  3423. /* Close the sub-packet created by create_ticket_prequel() */
  3424. if (!WPACKET_close(pkt)) {
  3425. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3426. goto err;
  3427. }
  3428. ok = 1;
  3429. err:
  3430. OPENSSL_free(senc);
  3431. EVP_CIPHER_CTX_free(ctx);
  3432. ssl_hmac_free(hctx);
  3433. return ok;
  3434. }
  3435. static int construct_stateful_ticket(SSL *s, WPACKET *pkt, uint32_t age_add,
  3436. unsigned char *tick_nonce)
  3437. {
  3438. if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
  3439. /* SSLfatal() already called */
  3440. return 0;
  3441. }
  3442. if (!WPACKET_memcpy(pkt, s->session->session_id,
  3443. s->session->session_id_length)
  3444. || !WPACKET_close(pkt)) {
  3445. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3446. return 0;
  3447. }
  3448. return 1;
  3449. }
  3450. int tls_construct_new_session_ticket(SSL *s, WPACKET *pkt)
  3451. {
  3452. SSL_CTX *tctx = s->session_ctx;
  3453. unsigned char tick_nonce[TICKET_NONCE_SIZE];
  3454. union {
  3455. unsigned char age_add_c[sizeof(uint32_t)];
  3456. uint32_t age_add;
  3457. } age_add_u;
  3458. age_add_u.age_add = 0;
  3459. if (SSL_IS_TLS13(s)) {
  3460. size_t i, hashlen;
  3461. uint64_t nonce;
  3462. static const unsigned char nonce_label[] = "resumption";
  3463. const EVP_MD *md = ssl_handshake_md(s);
  3464. int hashleni = EVP_MD_get_size(md);
  3465. /* Ensure cast to size_t is safe */
  3466. if (!ossl_assert(hashleni >= 0)) {
  3467. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3468. goto err;
  3469. }
  3470. hashlen = (size_t)hashleni;
  3471. /*
  3472. * If we already sent one NewSessionTicket, or we resumed then
  3473. * s->session may already be in a cache and so we must not modify it.
  3474. * Instead we need to take a copy of it and modify that.
  3475. */
  3476. if (s->sent_tickets != 0 || s->hit) {
  3477. SSL_SESSION *new_sess = ssl_session_dup(s->session, 0);
  3478. if (new_sess == NULL) {
  3479. /* SSLfatal already called */
  3480. goto err;
  3481. }
  3482. SSL_SESSION_free(s->session);
  3483. s->session = new_sess;
  3484. }
  3485. if (!ssl_generate_session_id(s, s->session)) {
  3486. /* SSLfatal() already called */
  3487. goto err;
  3488. }
  3489. if (RAND_bytes_ex(s->ctx->libctx, age_add_u.age_add_c,
  3490. sizeof(age_add_u), 0) <= 0) {
  3491. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3492. goto err;
  3493. }
  3494. s->session->ext.tick_age_add = age_add_u.age_add;
  3495. nonce = s->next_ticket_nonce;
  3496. for (i = TICKET_NONCE_SIZE; i > 0; i--) {
  3497. tick_nonce[i - 1] = (unsigned char)(nonce & 0xff);
  3498. nonce >>= 8;
  3499. }
  3500. if (!tls13_hkdf_expand(s, md, s->resumption_master_secret,
  3501. nonce_label,
  3502. sizeof(nonce_label) - 1,
  3503. tick_nonce,
  3504. TICKET_NONCE_SIZE,
  3505. s->session->master_key,
  3506. hashlen, 1)) {
  3507. /* SSLfatal() already called */
  3508. goto err;
  3509. }
  3510. s->session->master_key_length = hashlen;
  3511. s->session->time = time(NULL);
  3512. ssl_session_calculate_timeout(s->session);
  3513. if (s->s3.alpn_selected != NULL) {
  3514. OPENSSL_free(s->session->ext.alpn_selected);
  3515. s->session->ext.alpn_selected =
  3516. OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len);
  3517. if (s->session->ext.alpn_selected == NULL) {
  3518. s->session->ext.alpn_selected_len = 0;
  3519. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  3520. goto err;
  3521. }
  3522. s->session->ext.alpn_selected_len = s->s3.alpn_selected_len;
  3523. }
  3524. s->session->ext.max_early_data = s->max_early_data;
  3525. }
  3526. if (tctx->generate_ticket_cb != NULL &&
  3527. tctx->generate_ticket_cb(s, tctx->ticket_cb_data) == 0) {
  3528. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3529. goto err;
  3530. }
  3531. /*
  3532. * If we are using anti-replay protection then we behave as if
  3533. * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
  3534. * is no point in using full stateless tickets.
  3535. */
  3536. if (SSL_IS_TLS13(s)
  3537. && ((s->options & SSL_OP_NO_TICKET) != 0
  3538. || (s->max_early_data > 0
  3539. && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))) {
  3540. if (!construct_stateful_ticket(s, pkt, age_add_u.age_add, tick_nonce)) {
  3541. /* SSLfatal() already called */
  3542. goto err;
  3543. }
  3544. } else if (!construct_stateless_ticket(s, pkt, age_add_u.age_add,
  3545. tick_nonce)) {
  3546. /* SSLfatal() already called */
  3547. goto err;
  3548. }
  3549. if (SSL_IS_TLS13(s)) {
  3550. if (!tls_construct_extensions(s, pkt,
  3551. SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
  3552. NULL, 0)) {
  3553. /* SSLfatal() already called */
  3554. goto err;
  3555. }
  3556. /*
  3557. * Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets|
  3558. * gets reset to 0 if we send more tickets following a post-handshake
  3559. * auth, but |next_ticket_nonce| does not. If we're sending extra
  3560. * tickets, decrement the count of pending extra tickets.
  3561. */
  3562. s->sent_tickets++;
  3563. s->next_ticket_nonce++;
  3564. if (s->ext.extra_tickets_expected > 0)
  3565. s->ext.extra_tickets_expected--;
  3566. ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
  3567. }
  3568. return 1;
  3569. err:
  3570. return 0;
  3571. }
  3572. /*
  3573. * In TLSv1.3 this is called from the extensions code, otherwise it is used to
  3574. * create a separate message. Returns 1 on success or 0 on failure.
  3575. */
  3576. int tls_construct_cert_status_body(SSL *s, WPACKET *pkt)
  3577. {
  3578. if (!WPACKET_put_bytes_u8(pkt, s->ext.status_type)
  3579. || !WPACKET_sub_memcpy_u24(pkt, s->ext.ocsp.resp,
  3580. s->ext.ocsp.resp_len)) {
  3581. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3582. return 0;
  3583. }
  3584. return 1;
  3585. }
  3586. int tls_construct_cert_status(SSL *s, WPACKET *pkt)
  3587. {
  3588. if (!tls_construct_cert_status_body(s, pkt)) {
  3589. /* SSLfatal() already called */
  3590. return 0;
  3591. }
  3592. return 1;
  3593. }
  3594. #ifndef OPENSSL_NO_NEXTPROTONEG
  3595. /*
  3596. * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
  3597. * It sets the next_proto member in s if found
  3598. */
  3599. MSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt)
  3600. {
  3601. PACKET next_proto, padding;
  3602. size_t next_proto_len;
  3603. /*-
  3604. * The payload looks like:
  3605. * uint8 proto_len;
  3606. * uint8 proto[proto_len];
  3607. * uint8 padding_len;
  3608. * uint8 padding[padding_len];
  3609. */
  3610. if (!PACKET_get_length_prefixed_1(pkt, &next_proto)
  3611. || !PACKET_get_length_prefixed_1(pkt, &padding)
  3612. || PACKET_remaining(pkt) > 0) {
  3613. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  3614. return MSG_PROCESS_ERROR;
  3615. }
  3616. if (!PACKET_memdup(&next_proto, &s->ext.npn, &next_proto_len)) {
  3617. s->ext.npn_len = 0;
  3618. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3619. return MSG_PROCESS_ERROR;
  3620. }
  3621. s->ext.npn_len = (unsigned char)next_proto_len;
  3622. return MSG_PROCESS_CONTINUE_READING;
  3623. }
  3624. #endif
  3625. static int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt)
  3626. {
  3627. if (!tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
  3628. NULL, 0)) {
  3629. /* SSLfatal() already called */
  3630. return 0;
  3631. }
  3632. return 1;
  3633. }
  3634. MSG_PROCESS_RETURN tls_process_end_of_early_data(SSL *s, PACKET *pkt)
  3635. {
  3636. if (PACKET_remaining(pkt) != 0) {
  3637. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  3638. return MSG_PROCESS_ERROR;
  3639. }
  3640. if (s->early_data_state != SSL_EARLY_DATA_READING
  3641. && s->early_data_state != SSL_EARLY_DATA_READ_RETRY) {
  3642. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  3643. return MSG_PROCESS_ERROR;
  3644. }
  3645. /*
  3646. * EndOfEarlyData signals a key change so the end of the message must be on
  3647. * a record boundary.
  3648. */
  3649. if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
  3650. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY);
  3651. return MSG_PROCESS_ERROR;
  3652. }
  3653. s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
  3654. if (!s->method->ssl3_enc->change_cipher_state(s,
  3655. SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) {
  3656. /* SSLfatal() already called */
  3657. return MSG_PROCESS_ERROR;
  3658. }
  3659. return MSG_PROCESS_CONTINUE_READING;
  3660. }