t1_lib.c 145 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550
  1. /* ssl/t1_lib.c */
  2. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson ([email protected]).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young ([email protected])"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson ([email protected])"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * [email protected].
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * ([email protected]). This product includes software written by Tim
  108. * Hudson ([email protected]).
  109. *
  110. */
  111. #include <stdio.h>
  112. #include <openssl/objects.h>
  113. #include <openssl/evp.h>
  114. #include <openssl/hmac.h>
  115. #ifndef OPENSSL_NO_EC
  116. #ifdef OPENSSL_NO_EC2M
  117. # include <openssl/ec.h>
  118. #endif
  119. #endif
  120. #include <openssl/ocsp.h>
  121. #include <openssl/rand.h>
  122. #include "ssl_locl.h"
  123. const char tls1_version_str[] = "TLSv1" OPENSSL_VERSION_PTEXT;
  124. #ifndef OPENSSL_NO_TLSEXT
  125. static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
  126. const unsigned char *sess_id, int sesslen,
  127. SSL_SESSION **psess);
  128. static int ssl_check_clienthello_tlsext_early(SSL *s);
  129. int ssl_check_serverhello_tlsext(SSL *s);
  130. #endif
  131. #define CHECKLEN(curr, val, limit) \
  132. (((curr) >= (limit)) || (size_t)((limit) - (curr)) < (size_t)(val))
  133. SSL3_ENC_METHOD TLSv1_enc_data = {
  134. tls1_enc,
  135. tls1_mac,
  136. tls1_setup_key_block,
  137. tls1_generate_master_secret,
  138. tls1_change_cipher_state,
  139. tls1_final_finish_mac,
  140. TLS1_FINISH_MAC_LENGTH,
  141. tls1_cert_verify_mac,
  142. TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
  143. TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
  144. tls1_alert_code,
  145. tls1_export_keying_material,
  146. 0,
  147. SSL3_HM_HEADER_LENGTH,
  148. ssl3_set_handshake_header,
  149. ssl3_handshake_write
  150. };
  151. SSL3_ENC_METHOD TLSv1_1_enc_data = {
  152. tls1_enc,
  153. tls1_mac,
  154. tls1_setup_key_block,
  155. tls1_generate_master_secret,
  156. tls1_change_cipher_state,
  157. tls1_final_finish_mac,
  158. TLS1_FINISH_MAC_LENGTH,
  159. tls1_cert_verify_mac,
  160. TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
  161. TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
  162. tls1_alert_code,
  163. tls1_export_keying_material,
  164. SSL_ENC_FLAG_EXPLICIT_IV,
  165. SSL3_HM_HEADER_LENGTH,
  166. ssl3_set_handshake_header,
  167. ssl3_handshake_write
  168. };
  169. SSL3_ENC_METHOD TLSv1_2_enc_data = {
  170. tls1_enc,
  171. tls1_mac,
  172. tls1_setup_key_block,
  173. tls1_generate_master_secret,
  174. tls1_change_cipher_state,
  175. tls1_final_finish_mac,
  176. TLS1_FINISH_MAC_LENGTH,
  177. tls1_cert_verify_mac,
  178. TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
  179. TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
  180. tls1_alert_code,
  181. tls1_export_keying_material,
  182. SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
  183. | SSL_ENC_FLAG_TLS1_2_CIPHERS,
  184. SSL3_HM_HEADER_LENGTH,
  185. ssl3_set_handshake_header,
  186. ssl3_handshake_write
  187. };
  188. long tls1_default_timeout(void)
  189. {
  190. /*
  191. * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
  192. * http, the cache would over fill
  193. */
  194. return (60 * 60 * 2);
  195. }
  196. int tls1_new(SSL *s)
  197. {
  198. if (!ssl3_new(s))
  199. return (0);
  200. s->method->ssl_clear(s);
  201. return (1);
  202. }
  203. void tls1_free(SSL *s)
  204. {
  205. #ifndef OPENSSL_NO_TLSEXT
  206. if (s->tlsext_session_ticket) {
  207. OPENSSL_free(s->tlsext_session_ticket);
  208. }
  209. #endif /* OPENSSL_NO_TLSEXT */
  210. ssl3_free(s);
  211. }
  212. void tls1_clear(SSL *s)
  213. {
  214. ssl3_clear(s);
  215. s->version = s->method->version;
  216. }
  217. #ifndef OPENSSL_NO_EC
  218. static int nid_list[] = {
  219. NID_sect163k1, /* sect163k1 (1) */
  220. NID_sect163r1, /* sect163r1 (2) */
  221. NID_sect163r2, /* sect163r2 (3) */
  222. NID_sect193r1, /* sect193r1 (4) */
  223. NID_sect193r2, /* sect193r2 (5) */
  224. NID_sect233k1, /* sect233k1 (6) */
  225. NID_sect233r1, /* sect233r1 (7) */
  226. NID_sect239k1, /* sect239k1 (8) */
  227. NID_sect283k1, /* sect283k1 (9) */
  228. NID_sect283r1, /* sect283r1 (10) */
  229. NID_sect409k1, /* sect409k1 (11) */
  230. NID_sect409r1, /* sect409r1 (12) */
  231. NID_sect571k1, /* sect571k1 (13) */
  232. NID_sect571r1, /* sect571r1 (14) */
  233. NID_secp160k1, /* secp160k1 (15) */
  234. NID_secp160r1, /* secp160r1 (16) */
  235. NID_secp160r2, /* secp160r2 (17) */
  236. NID_secp192k1, /* secp192k1 (18) */
  237. NID_X9_62_prime192v1, /* secp192r1 (19) */
  238. NID_secp224k1, /* secp224k1 (20) */
  239. NID_secp224r1, /* secp224r1 (21) */
  240. NID_secp256k1, /* secp256k1 (22) */
  241. NID_X9_62_prime256v1, /* secp256r1 (23) */
  242. NID_secp384r1, /* secp384r1 (24) */
  243. NID_secp521r1, /* secp521r1 (25) */
  244. NID_brainpoolP256r1, /* brainpoolP256r1 (26) */
  245. NID_brainpoolP384r1, /* brainpoolP384r1 (27) */
  246. NID_brainpoolP512r1 /* brainpool512r1 (28) */
  247. };
  248. static const unsigned char ecformats_default[] = {
  249. TLSEXT_ECPOINTFORMAT_uncompressed,
  250. TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
  251. TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
  252. };
  253. /* The client's default curves / the server's 'auto' curves. */
  254. static const unsigned char eccurves_auto[] = {
  255. /* Prefer P-256 which has the fastest and most secure implementations. */
  256. 0, 23, /* secp256r1 (23) */
  257. /* Other >= 256-bit prime curves. */
  258. 0, 25, /* secp521r1 (25) */
  259. 0, 28, /* brainpool512r1 (28) */
  260. 0, 27, /* brainpoolP384r1 (27) */
  261. 0, 24, /* secp384r1 (24) */
  262. 0, 26, /* brainpoolP256r1 (26) */
  263. 0, 22, /* secp256k1 (22) */
  264. # ifndef OPENSSL_NO_EC2M
  265. /* >= 256-bit binary curves. */
  266. 0, 14, /* sect571r1 (14) */
  267. 0, 13, /* sect571k1 (13) */
  268. 0, 11, /* sect409k1 (11) */
  269. 0, 12, /* sect409r1 (12) */
  270. 0, 9, /* sect283k1 (9) */
  271. 0, 10, /* sect283r1 (10) */
  272. # endif
  273. };
  274. static const unsigned char eccurves_all[] = {
  275. /* Prefer P-256 which has the fastest and most secure implementations. */
  276. 0, 23, /* secp256r1 (23) */
  277. /* Other >= 256-bit prime curves. */
  278. 0, 25, /* secp521r1 (25) */
  279. 0, 28, /* brainpool512r1 (28) */
  280. 0, 27, /* brainpoolP384r1 (27) */
  281. 0, 24, /* secp384r1 (24) */
  282. 0, 26, /* brainpoolP256r1 (26) */
  283. 0, 22, /* secp256k1 (22) */
  284. # ifndef OPENSSL_NO_EC2M
  285. /* >= 256-bit binary curves. */
  286. 0, 14, /* sect571r1 (14) */
  287. 0, 13, /* sect571k1 (13) */
  288. 0, 11, /* sect409k1 (11) */
  289. 0, 12, /* sect409r1 (12) */
  290. 0, 9, /* sect283k1 (9) */
  291. 0, 10, /* sect283r1 (10) */
  292. # endif
  293. /*
  294. * Remaining curves disabled by default but still permitted if set
  295. * via an explicit callback or parameters.
  296. */
  297. 0, 20, /* secp224k1 (20) */
  298. 0, 21, /* secp224r1 (21) */
  299. 0, 18, /* secp192k1 (18) */
  300. 0, 19, /* secp192r1 (19) */
  301. 0, 15, /* secp160k1 (15) */
  302. 0, 16, /* secp160r1 (16) */
  303. 0, 17, /* secp160r2 (17) */
  304. # ifndef OPENSSL_NO_EC2M
  305. 0, 8, /* sect239k1 (8) */
  306. 0, 6, /* sect233k1 (6) */
  307. 0, 7, /* sect233r1 (7) */
  308. 0, 4, /* sect193r1 (4) */
  309. 0, 5, /* sect193r2 (5) */
  310. 0, 1, /* sect163k1 (1) */
  311. 0, 2, /* sect163r1 (2) */
  312. 0, 3, /* sect163r2 (3) */
  313. # endif
  314. };
  315. static const unsigned char suiteb_curves[] = {
  316. 0, TLSEXT_curve_P_256,
  317. 0, TLSEXT_curve_P_384
  318. };
  319. # ifdef OPENSSL_FIPS
  320. /* Brainpool not allowed in FIPS mode */
  321. static const unsigned char fips_curves_default[] = {
  322. # ifndef OPENSSL_NO_EC2M
  323. 0, 14, /* sect571r1 (14) */
  324. 0, 13, /* sect571k1 (13) */
  325. # endif
  326. 0, 25, /* secp521r1 (25) */
  327. # ifndef OPENSSL_NO_EC2M
  328. 0, 11, /* sect409k1 (11) */
  329. 0, 12, /* sect409r1 (12) */
  330. # endif
  331. 0, 24, /* secp384r1 (24) */
  332. # ifndef OPENSSL_NO_EC2M
  333. 0, 9, /* sect283k1 (9) */
  334. 0, 10, /* sect283r1 (10) */
  335. # endif
  336. 0, 22, /* secp256k1 (22) */
  337. 0, 23, /* secp256r1 (23) */
  338. # ifndef OPENSSL_NO_EC2M
  339. 0, 8, /* sect239k1 (8) */
  340. 0, 6, /* sect233k1 (6) */
  341. 0, 7, /* sect233r1 (7) */
  342. # endif
  343. 0, 20, /* secp224k1 (20) */
  344. 0, 21, /* secp224r1 (21) */
  345. # ifndef OPENSSL_NO_EC2M
  346. 0, 4, /* sect193r1 (4) */
  347. 0, 5, /* sect193r2 (5) */
  348. # endif
  349. 0, 18, /* secp192k1 (18) */
  350. 0, 19, /* secp192r1 (19) */
  351. # ifndef OPENSSL_NO_EC2M
  352. 0, 1, /* sect163k1 (1) */
  353. 0, 2, /* sect163r1 (2) */
  354. 0, 3, /* sect163r2 (3) */
  355. # endif
  356. 0, 15, /* secp160k1 (15) */
  357. 0, 16, /* secp160r1 (16) */
  358. 0, 17, /* secp160r2 (17) */
  359. };
  360. # endif
  361. int tls1_ec_curve_id2nid(int curve_id)
  362. {
  363. /* ECC curves from RFC 4492 and RFC 7027 */
  364. if ((curve_id < 1) || ((unsigned int)curve_id >
  365. sizeof(nid_list) / sizeof(nid_list[0])))
  366. return 0;
  367. return nid_list[curve_id - 1];
  368. }
  369. int tls1_ec_nid2curve_id(int nid)
  370. {
  371. /* ECC curves from RFC 4492 and RFC 7027 */
  372. switch (nid) {
  373. case NID_sect163k1: /* sect163k1 (1) */
  374. return 1;
  375. case NID_sect163r1: /* sect163r1 (2) */
  376. return 2;
  377. case NID_sect163r2: /* sect163r2 (3) */
  378. return 3;
  379. case NID_sect193r1: /* sect193r1 (4) */
  380. return 4;
  381. case NID_sect193r2: /* sect193r2 (5) */
  382. return 5;
  383. case NID_sect233k1: /* sect233k1 (6) */
  384. return 6;
  385. case NID_sect233r1: /* sect233r1 (7) */
  386. return 7;
  387. case NID_sect239k1: /* sect239k1 (8) */
  388. return 8;
  389. case NID_sect283k1: /* sect283k1 (9) */
  390. return 9;
  391. case NID_sect283r1: /* sect283r1 (10) */
  392. return 10;
  393. case NID_sect409k1: /* sect409k1 (11) */
  394. return 11;
  395. case NID_sect409r1: /* sect409r1 (12) */
  396. return 12;
  397. case NID_sect571k1: /* sect571k1 (13) */
  398. return 13;
  399. case NID_sect571r1: /* sect571r1 (14) */
  400. return 14;
  401. case NID_secp160k1: /* secp160k1 (15) */
  402. return 15;
  403. case NID_secp160r1: /* secp160r1 (16) */
  404. return 16;
  405. case NID_secp160r2: /* secp160r2 (17) */
  406. return 17;
  407. case NID_secp192k1: /* secp192k1 (18) */
  408. return 18;
  409. case NID_X9_62_prime192v1: /* secp192r1 (19) */
  410. return 19;
  411. case NID_secp224k1: /* secp224k1 (20) */
  412. return 20;
  413. case NID_secp224r1: /* secp224r1 (21) */
  414. return 21;
  415. case NID_secp256k1: /* secp256k1 (22) */
  416. return 22;
  417. case NID_X9_62_prime256v1: /* secp256r1 (23) */
  418. return 23;
  419. case NID_secp384r1: /* secp384r1 (24) */
  420. return 24;
  421. case NID_secp521r1: /* secp521r1 (25) */
  422. return 25;
  423. case NID_brainpoolP256r1: /* brainpoolP256r1 (26) */
  424. return 26;
  425. case NID_brainpoolP384r1: /* brainpoolP384r1 (27) */
  426. return 27;
  427. case NID_brainpoolP512r1: /* brainpool512r1 (28) */
  428. return 28;
  429. default:
  430. return 0;
  431. }
  432. }
  433. /*
  434. * Get curves list, if "sess" is set return client curves otherwise
  435. * preferred list.
  436. * Sets |num_curves| to the number of curves in the list, i.e.,
  437. * the length of |pcurves| is 2 * num_curves.
  438. * Returns 1 on success and 0 if the client curves list has invalid format.
  439. * The latter indicates an internal error: we should not be accepting such
  440. * lists in the first place.
  441. * TODO(emilia): we should really be storing the curves list in explicitly
  442. * parsed form instead. (However, this would affect binary compatibility
  443. * so cannot happen in the 1.0.x series.)
  444. */
  445. static int tls1_get_curvelist(SSL *s, int sess,
  446. const unsigned char **pcurves,
  447. size_t *num_curves)
  448. {
  449. size_t pcurveslen = 0;
  450. if (sess) {
  451. *pcurves = s->session->tlsext_ellipticcurvelist;
  452. pcurveslen = s->session->tlsext_ellipticcurvelist_length;
  453. } else {
  454. /* For Suite B mode only include P-256, P-384 */
  455. switch (tls1_suiteb(s)) {
  456. case SSL_CERT_FLAG_SUITEB_128_LOS:
  457. *pcurves = suiteb_curves;
  458. pcurveslen = sizeof(suiteb_curves);
  459. break;
  460. case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
  461. *pcurves = suiteb_curves;
  462. pcurveslen = 2;
  463. break;
  464. case SSL_CERT_FLAG_SUITEB_192_LOS:
  465. *pcurves = suiteb_curves + 2;
  466. pcurveslen = 2;
  467. break;
  468. default:
  469. *pcurves = s->tlsext_ellipticcurvelist;
  470. pcurveslen = s->tlsext_ellipticcurvelist_length;
  471. }
  472. if (!*pcurves) {
  473. # ifdef OPENSSL_FIPS
  474. if (FIPS_mode()) {
  475. *pcurves = fips_curves_default;
  476. pcurveslen = sizeof(fips_curves_default);
  477. } else
  478. # endif
  479. {
  480. if (!s->server || s->cert->ecdh_tmp_auto) {
  481. *pcurves = eccurves_auto;
  482. pcurveslen = sizeof(eccurves_auto);
  483. } else {
  484. *pcurves = eccurves_all;
  485. pcurveslen = sizeof(eccurves_all);
  486. }
  487. }
  488. }
  489. }
  490. /* We do not allow odd length arrays to enter the system. */
  491. if (pcurveslen & 1) {
  492. SSLerr(SSL_F_TLS1_GET_CURVELIST, ERR_R_INTERNAL_ERROR);
  493. *num_curves = 0;
  494. return 0;
  495. } else {
  496. *num_curves = pcurveslen / 2;
  497. return 1;
  498. }
  499. }
  500. /* Check a curve is one of our preferences */
  501. int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
  502. {
  503. const unsigned char *curves;
  504. size_t num_curves, i;
  505. unsigned int suiteb_flags = tls1_suiteb(s);
  506. if (len != 3 || p[0] != NAMED_CURVE_TYPE)
  507. return 0;
  508. /* Check curve matches Suite B preferences */
  509. if (suiteb_flags) {
  510. unsigned long cid = s->s3->tmp.new_cipher->id;
  511. if (p[1])
  512. return 0;
  513. if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
  514. if (p[2] != TLSEXT_curve_P_256)
  515. return 0;
  516. } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
  517. if (p[2] != TLSEXT_curve_P_384)
  518. return 0;
  519. } else /* Should never happen */
  520. return 0;
  521. }
  522. if (!tls1_get_curvelist(s, 0, &curves, &num_curves))
  523. return 0;
  524. for (i = 0; i < num_curves; i++, curves += 2) {
  525. if (p[1] == curves[0] && p[2] == curves[1])
  526. return 1;
  527. }
  528. return 0;
  529. }
  530. /*-
  531. * Return |nmatch|th shared curve or NID_undef if there is no match.
  532. * For nmatch == -1, return number of matches
  533. * For nmatch == -2, return the NID of the curve to use for
  534. * an EC tmp key, or NID_undef if there is no match.
  535. */
  536. int tls1_shared_curve(SSL *s, int nmatch)
  537. {
  538. const unsigned char *pref, *supp;
  539. size_t num_pref, num_supp, i, j;
  540. int k;
  541. /* Can't do anything on client side */
  542. if (s->server == 0)
  543. return -1;
  544. if (nmatch == -2) {
  545. if (tls1_suiteb(s)) {
  546. /*
  547. * For Suite B ciphersuite determines curve: we already know
  548. * these are acceptable due to previous checks.
  549. */
  550. unsigned long cid = s->s3->tmp.new_cipher->id;
  551. if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
  552. return NID_X9_62_prime256v1; /* P-256 */
  553. if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
  554. return NID_secp384r1; /* P-384 */
  555. /* Should never happen */
  556. return NID_undef;
  557. }
  558. /* If not Suite B just return first preference shared curve */
  559. nmatch = 0;
  560. }
  561. /*
  562. * Avoid truncation. tls1_get_curvelist takes an int
  563. * but s->options is a long...
  564. */
  565. if (!tls1_get_curvelist
  566. (s, (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0, &supp,
  567. &num_supp))
  568. /* In practice, NID_undef == 0 but let's be precise. */
  569. return nmatch == -1 ? 0 : NID_undef;
  570. if (!tls1_get_curvelist
  571. (s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref,
  572. &num_pref))
  573. return nmatch == -1 ? 0 : NID_undef;
  574. /*
  575. * If the client didn't send the elliptic_curves extension all of them
  576. * are allowed.
  577. */
  578. if (num_supp == 0 && (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0) {
  579. supp = eccurves_all;
  580. num_supp = sizeof(eccurves_all) / 2;
  581. } else if (num_pref == 0 &&
  582. (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) == 0) {
  583. pref = eccurves_all;
  584. num_pref = sizeof(eccurves_all) / 2;
  585. }
  586. k = 0;
  587. for (i = 0; i < num_pref; i++, pref += 2) {
  588. const unsigned char *tsupp = supp;
  589. for (j = 0; j < num_supp; j++, tsupp += 2) {
  590. if (pref[0] == tsupp[0] && pref[1] == tsupp[1]) {
  591. if (nmatch == k) {
  592. int id = (pref[0] << 8) | pref[1];
  593. return tls1_ec_curve_id2nid(id);
  594. }
  595. k++;
  596. }
  597. }
  598. }
  599. if (nmatch == -1)
  600. return k;
  601. /* Out of range (nmatch > k). */
  602. return NID_undef;
  603. }
  604. int tls1_set_curves(unsigned char **pext, size_t *pextlen,
  605. int *curves, size_t ncurves)
  606. {
  607. unsigned char *clist, *p;
  608. size_t i;
  609. /*
  610. * Bitmap of curves included to detect duplicates: only works while curve
  611. * ids < 32
  612. */
  613. unsigned long dup_list = 0;
  614. # ifdef OPENSSL_NO_EC2M
  615. EC_GROUP *curve;
  616. # endif
  617. clist = OPENSSL_malloc(ncurves * 2);
  618. if (!clist)
  619. return 0;
  620. for (i = 0, p = clist; i < ncurves; i++) {
  621. unsigned long idmask;
  622. int id;
  623. id = tls1_ec_nid2curve_id(curves[i]);
  624. # ifdef OPENSSL_FIPS
  625. /* NB: 25 is last curve ID supported by FIPS module */
  626. if (FIPS_mode() && id > 25) {
  627. OPENSSL_free(clist);
  628. return 0;
  629. }
  630. # endif
  631. # ifdef OPENSSL_NO_EC2M
  632. curve = EC_GROUP_new_by_curve_name(curves[i]);
  633. if (!curve || EC_METHOD_get_field_type(EC_GROUP_method_of(curve))
  634. == NID_X9_62_characteristic_two_field) {
  635. if (curve)
  636. EC_GROUP_free(curve);
  637. OPENSSL_free(clist);
  638. return 0;
  639. } else
  640. EC_GROUP_free(curve);
  641. # endif
  642. idmask = 1L << id;
  643. if (!id || (dup_list & idmask)) {
  644. OPENSSL_free(clist);
  645. return 0;
  646. }
  647. dup_list |= idmask;
  648. s2n(id, p);
  649. }
  650. if (*pext)
  651. OPENSSL_free(*pext);
  652. *pext = clist;
  653. *pextlen = ncurves * 2;
  654. return 1;
  655. }
  656. # define MAX_CURVELIST 28
  657. typedef struct {
  658. size_t nidcnt;
  659. int nid_arr[MAX_CURVELIST];
  660. } nid_cb_st;
  661. static int nid_cb(const char *elem, int len, void *arg)
  662. {
  663. nid_cb_st *narg = arg;
  664. size_t i;
  665. int nid;
  666. char etmp[20];
  667. if (elem == NULL)
  668. return 0;
  669. if (narg->nidcnt == MAX_CURVELIST)
  670. return 0;
  671. if (len > (int)(sizeof(etmp) - 1))
  672. return 0;
  673. memcpy(etmp, elem, len);
  674. etmp[len] = 0;
  675. nid = EC_curve_nist2nid(etmp);
  676. if (nid == NID_undef)
  677. nid = OBJ_sn2nid(etmp);
  678. if (nid == NID_undef)
  679. nid = OBJ_ln2nid(etmp);
  680. if (nid == NID_undef)
  681. return 0;
  682. for (i = 0; i < narg->nidcnt; i++)
  683. if (narg->nid_arr[i] == nid)
  684. return 0;
  685. narg->nid_arr[narg->nidcnt++] = nid;
  686. return 1;
  687. }
  688. /* Set curves based on a colon separate list */
  689. int tls1_set_curves_list(unsigned char **pext, size_t *pextlen,
  690. const char *str)
  691. {
  692. nid_cb_st ncb;
  693. ncb.nidcnt = 0;
  694. if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
  695. return 0;
  696. if (pext == NULL)
  697. return 1;
  698. return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
  699. }
  700. /* For an EC key set TLS id and required compression based on parameters */
  701. static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
  702. EC_KEY *ec)
  703. {
  704. int is_prime, id;
  705. const EC_GROUP *grp;
  706. const EC_METHOD *meth;
  707. if (!ec)
  708. return 0;
  709. /* Determine if it is a prime field */
  710. grp = EC_KEY_get0_group(ec);
  711. if (!grp)
  712. return 0;
  713. meth = EC_GROUP_method_of(grp);
  714. if (!meth)
  715. return 0;
  716. if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field)
  717. is_prime = 1;
  718. else
  719. is_prime = 0;
  720. /* Determine curve ID */
  721. id = EC_GROUP_get_curve_name(grp);
  722. id = tls1_ec_nid2curve_id(id);
  723. /* If we have an ID set it, otherwise set arbitrary explicit curve */
  724. if (id) {
  725. curve_id[0] = 0;
  726. curve_id[1] = (unsigned char)id;
  727. } else {
  728. curve_id[0] = 0xff;
  729. if (is_prime)
  730. curve_id[1] = 0x01;
  731. else
  732. curve_id[1] = 0x02;
  733. }
  734. if (comp_id) {
  735. if (EC_KEY_get0_public_key(ec) == NULL)
  736. return 0;
  737. if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
  738. if (is_prime)
  739. *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
  740. else
  741. *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
  742. } else
  743. *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
  744. }
  745. return 1;
  746. }
  747. /* Check an EC key is compatible with extensions */
  748. static int tls1_check_ec_key(SSL *s,
  749. unsigned char *curve_id, unsigned char *comp_id)
  750. {
  751. const unsigned char *pformats, *pcurves;
  752. size_t num_formats, num_curves, i;
  753. int j;
  754. /*
  755. * If point formats extension present check it, otherwise everything is
  756. * supported (see RFC4492).
  757. */
  758. if (comp_id && s->session->tlsext_ecpointformatlist) {
  759. pformats = s->session->tlsext_ecpointformatlist;
  760. num_formats = s->session->tlsext_ecpointformatlist_length;
  761. for (i = 0; i < num_formats; i++, pformats++) {
  762. if (*comp_id == *pformats)
  763. break;
  764. }
  765. if (i == num_formats)
  766. return 0;
  767. }
  768. if (!curve_id)
  769. return 1;
  770. /* Check curve is consistent with client and server preferences */
  771. for (j = 0; j <= 1; j++) {
  772. if (!tls1_get_curvelist(s, j, &pcurves, &num_curves))
  773. return 0;
  774. if (j == 1 && num_curves == 0) {
  775. /*
  776. * If we've not received any curves then skip this check.
  777. * RFC 4492 does not require the supported elliptic curves extension
  778. * so if it is not sent we can just choose any curve.
  779. * It is invalid to send an empty list in the elliptic curves
  780. * extension, so num_curves == 0 always means no extension.
  781. */
  782. break;
  783. }
  784. for (i = 0; i < num_curves; i++, pcurves += 2) {
  785. if (pcurves[0] == curve_id[0] && pcurves[1] == curve_id[1])
  786. break;
  787. }
  788. if (i == num_curves)
  789. return 0;
  790. /* For clients can only check sent curve list */
  791. if (!s->server)
  792. return 1;
  793. }
  794. return 1;
  795. }
  796. static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
  797. size_t *num_formats)
  798. {
  799. /*
  800. * If we have a custom point format list use it otherwise use default
  801. */
  802. if (s->tlsext_ecpointformatlist) {
  803. *pformats = s->tlsext_ecpointformatlist;
  804. *num_formats = s->tlsext_ecpointformatlist_length;
  805. } else {
  806. *pformats = ecformats_default;
  807. /* For Suite B we don't support char2 fields */
  808. if (tls1_suiteb(s))
  809. *num_formats = sizeof(ecformats_default) - 1;
  810. else
  811. *num_formats = sizeof(ecformats_default);
  812. }
  813. }
  814. /*
  815. * Check cert parameters compatible with extensions: currently just checks EC
  816. * certificates have compatible curves and compression.
  817. */
  818. static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
  819. {
  820. unsigned char comp_id, curve_id[2];
  821. EVP_PKEY *pkey;
  822. int rv;
  823. pkey = X509_get_pubkey(x);
  824. if (!pkey)
  825. return 0;
  826. /* If not EC nothing to do */
  827. if (pkey->type != EVP_PKEY_EC) {
  828. EVP_PKEY_free(pkey);
  829. return 1;
  830. }
  831. rv = tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec);
  832. EVP_PKEY_free(pkey);
  833. if (!rv)
  834. return 0;
  835. /*
  836. * Can't check curve_id for client certs as we don't have a supported
  837. * curves extension.
  838. */
  839. rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
  840. if (!rv)
  841. return 0;
  842. /*
  843. * Special case for suite B. We *MUST* sign using SHA256+P-256 or
  844. * SHA384+P-384, adjust digest if necessary.
  845. */
  846. if (set_ee_md && tls1_suiteb(s)) {
  847. int check_md;
  848. size_t i;
  849. CERT *c = s->cert;
  850. if (curve_id[0])
  851. return 0;
  852. /* Check to see we have necessary signing algorithm */
  853. if (curve_id[1] == TLSEXT_curve_P_256)
  854. check_md = NID_ecdsa_with_SHA256;
  855. else if (curve_id[1] == TLSEXT_curve_P_384)
  856. check_md = NID_ecdsa_with_SHA384;
  857. else
  858. return 0; /* Should never happen */
  859. for (i = 0; i < c->shared_sigalgslen; i++)
  860. if (check_md == c->shared_sigalgs[i].signandhash_nid)
  861. break;
  862. if (i == c->shared_sigalgslen)
  863. return 0;
  864. if (set_ee_md == 2) {
  865. if (check_md == NID_ecdsa_with_SHA256)
  866. c->pkeys[SSL_PKEY_ECC].digest = EVP_sha256();
  867. else
  868. c->pkeys[SSL_PKEY_ECC].digest = EVP_sha384();
  869. }
  870. }
  871. return rv;
  872. }
  873. # ifndef OPENSSL_NO_ECDH
  874. /* Check EC temporary key is compatible with client extensions */
  875. int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
  876. {
  877. unsigned char curve_id[2];
  878. EC_KEY *ec = s->cert->ecdh_tmp;
  879. # ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
  880. /* Allow any curve: not just those peer supports */
  881. if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
  882. return 1;
  883. # endif
  884. /*
  885. * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
  886. * curves permitted.
  887. */
  888. if (tls1_suiteb(s)) {
  889. /* Curve to check determined by ciphersuite */
  890. if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
  891. curve_id[1] = TLSEXT_curve_P_256;
  892. else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
  893. curve_id[1] = TLSEXT_curve_P_384;
  894. else
  895. return 0;
  896. curve_id[0] = 0;
  897. /* Check this curve is acceptable */
  898. if (!tls1_check_ec_key(s, curve_id, NULL))
  899. return 0;
  900. /* If auto or setting curve from callback assume OK */
  901. if (s->cert->ecdh_tmp_auto || s->cert->ecdh_tmp_cb)
  902. return 1;
  903. /* Otherwise check curve is acceptable */
  904. else {
  905. unsigned char curve_tmp[2];
  906. if (!ec)
  907. return 0;
  908. if (!tls1_set_ec_id(curve_tmp, NULL, ec))
  909. return 0;
  910. if (!curve_tmp[0] || curve_tmp[1] == curve_id[1])
  911. return 1;
  912. return 0;
  913. }
  914. }
  915. if (s->cert->ecdh_tmp_auto) {
  916. /* Need a shared curve */
  917. if (tls1_shared_curve(s, 0))
  918. return 1;
  919. else
  920. return 0;
  921. }
  922. if (!ec) {
  923. if (s->cert->ecdh_tmp_cb)
  924. return 1;
  925. else
  926. return 0;
  927. }
  928. if (!tls1_set_ec_id(curve_id, NULL, ec))
  929. return 0;
  930. /* Set this to allow use of invalid curves for testing */
  931. # if 0
  932. return 1;
  933. # else
  934. return tls1_check_ec_key(s, curve_id, NULL);
  935. # endif
  936. }
  937. # endif /* OPENSSL_NO_ECDH */
  938. #else
  939. static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
  940. {
  941. return 1;
  942. }
  943. #endif /* OPENSSL_NO_EC */
  944. #ifndef OPENSSL_NO_TLSEXT
  945. /*
  946. * List of supported signature algorithms and hashes. Should make this
  947. * customisable at some point, for now include everything we support.
  948. */
  949. # ifdef OPENSSL_NO_RSA
  950. # define tlsext_sigalg_rsa(md) /* */
  951. # else
  952. # define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
  953. # endif
  954. # ifdef OPENSSL_NO_DSA
  955. # define tlsext_sigalg_dsa(md) /* */
  956. # else
  957. # define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
  958. # endif
  959. # ifdef OPENSSL_NO_ECDSA
  960. # define tlsext_sigalg_ecdsa(md)
  961. /* */
  962. # else
  963. # define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
  964. # endif
  965. # define tlsext_sigalg(md) \
  966. tlsext_sigalg_rsa(md) \
  967. tlsext_sigalg_dsa(md) \
  968. tlsext_sigalg_ecdsa(md)
  969. static unsigned char tls12_sigalgs[] = {
  970. # ifndef OPENSSL_NO_SHA512
  971. tlsext_sigalg(TLSEXT_hash_sha512)
  972. tlsext_sigalg(TLSEXT_hash_sha384)
  973. # endif
  974. # ifndef OPENSSL_NO_SHA256
  975. tlsext_sigalg(TLSEXT_hash_sha256)
  976. tlsext_sigalg(TLSEXT_hash_sha224)
  977. # endif
  978. # ifndef OPENSSL_NO_SHA
  979. tlsext_sigalg(TLSEXT_hash_sha1)
  980. # endif
  981. };
  982. # ifndef OPENSSL_NO_ECDSA
  983. static unsigned char suiteb_sigalgs[] = {
  984. tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
  985. tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
  986. };
  987. # endif
  988. size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
  989. {
  990. /*
  991. * If Suite B mode use Suite B sigalgs only, ignore any other
  992. * preferences.
  993. */
  994. # ifndef OPENSSL_NO_EC
  995. switch (tls1_suiteb(s)) {
  996. case SSL_CERT_FLAG_SUITEB_128_LOS:
  997. *psigs = suiteb_sigalgs;
  998. return sizeof(suiteb_sigalgs);
  999. case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
  1000. *psigs = suiteb_sigalgs;
  1001. return 2;
  1002. case SSL_CERT_FLAG_SUITEB_192_LOS:
  1003. *psigs = suiteb_sigalgs + 2;
  1004. return 2;
  1005. }
  1006. # endif
  1007. /* If server use client authentication sigalgs if not NULL */
  1008. if (s->server && s->cert->client_sigalgs) {
  1009. *psigs = s->cert->client_sigalgs;
  1010. return s->cert->client_sigalgslen;
  1011. } else if (s->cert->conf_sigalgs) {
  1012. *psigs = s->cert->conf_sigalgs;
  1013. return s->cert->conf_sigalgslen;
  1014. } else {
  1015. *psigs = tls12_sigalgs;
  1016. return sizeof(tls12_sigalgs);
  1017. }
  1018. }
  1019. /*
  1020. * Check signature algorithm is consistent with sent supported signature
  1021. * algorithms and if so return relevant digest.
  1022. */
  1023. int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
  1024. const unsigned char *sig, EVP_PKEY *pkey)
  1025. {
  1026. const unsigned char *sent_sigs;
  1027. size_t sent_sigslen, i;
  1028. int sigalg = tls12_get_sigid(pkey);
  1029. /* Should never happen */
  1030. if (sigalg == -1)
  1031. return -1;
  1032. /* Check key type is consistent with signature */
  1033. if (sigalg != (int)sig[1]) {
  1034. SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
  1035. return 0;
  1036. }
  1037. # ifndef OPENSSL_NO_EC
  1038. if (pkey->type == EVP_PKEY_EC) {
  1039. unsigned char curve_id[2], comp_id;
  1040. /* Check compression and curve matches extensions */
  1041. if (!tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec))
  1042. return 0;
  1043. if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id)) {
  1044. SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
  1045. return 0;
  1046. }
  1047. /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
  1048. if (tls1_suiteb(s)) {
  1049. if (curve_id[0])
  1050. return 0;
  1051. if (curve_id[1] == TLSEXT_curve_P_256) {
  1052. if (sig[0] != TLSEXT_hash_sha256) {
  1053. SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
  1054. SSL_R_ILLEGAL_SUITEB_DIGEST);
  1055. return 0;
  1056. }
  1057. } else if (curve_id[1] == TLSEXT_curve_P_384) {
  1058. if (sig[0] != TLSEXT_hash_sha384) {
  1059. SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
  1060. SSL_R_ILLEGAL_SUITEB_DIGEST);
  1061. return 0;
  1062. }
  1063. } else
  1064. return 0;
  1065. }
  1066. } else if (tls1_suiteb(s))
  1067. return 0;
  1068. # endif
  1069. /* Check signature matches a type we sent */
  1070. sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
  1071. for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
  1072. if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
  1073. break;
  1074. }
  1075. /* Allow fallback to SHA1 if not strict mode */
  1076. if (i == sent_sigslen
  1077. && (sig[0] != TLSEXT_hash_sha1
  1078. || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
  1079. SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
  1080. return 0;
  1081. }
  1082. *pmd = tls12_get_hash(sig[0]);
  1083. if (*pmd == NULL) {
  1084. SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST);
  1085. return 0;
  1086. }
  1087. /*
  1088. * Store the digest used so applications can retrieve it if they wish.
  1089. */
  1090. if (s->session && s->session->sess_cert)
  1091. s->session->sess_cert->peer_key->digest = *pmd;
  1092. return 1;
  1093. }
  1094. /*
  1095. * Get a mask of disabled algorithms: an algorithm is disabled if it isn't
  1096. * supported or doesn't appear in supported signature algorithms. Unlike
  1097. * ssl_cipher_get_disabled this applies to a specific session and not global
  1098. * settings.
  1099. */
  1100. void ssl_set_client_disabled(SSL *s)
  1101. {
  1102. CERT *c = s->cert;
  1103. const unsigned char *sigalgs;
  1104. size_t i, sigalgslen;
  1105. int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
  1106. c->mask_a = 0;
  1107. c->mask_k = 0;
  1108. /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
  1109. if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
  1110. c->mask_ssl = SSL_TLSV1_2;
  1111. else
  1112. c->mask_ssl = 0;
  1113. /*
  1114. * Now go through all signature algorithms seeing if we support any for
  1115. * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2.
  1116. */
  1117. sigalgslen = tls12_get_psigalgs(s, &sigalgs);
  1118. for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
  1119. switch (sigalgs[1]) {
  1120. # ifndef OPENSSL_NO_RSA
  1121. case TLSEXT_signature_rsa:
  1122. have_rsa = 1;
  1123. break;
  1124. # endif
  1125. # ifndef OPENSSL_NO_DSA
  1126. case TLSEXT_signature_dsa:
  1127. have_dsa = 1;
  1128. break;
  1129. # endif
  1130. # ifndef OPENSSL_NO_ECDSA
  1131. case TLSEXT_signature_ecdsa:
  1132. have_ecdsa = 1;
  1133. break;
  1134. # endif
  1135. }
  1136. }
  1137. /*
  1138. * Disable auth and static DH if we don't include any appropriate
  1139. * signature algorithms.
  1140. */
  1141. if (!have_rsa) {
  1142. c->mask_a |= SSL_aRSA;
  1143. c->mask_k |= SSL_kDHr | SSL_kECDHr;
  1144. }
  1145. if (!have_dsa) {
  1146. c->mask_a |= SSL_aDSS;
  1147. c->mask_k |= SSL_kDHd;
  1148. }
  1149. if (!have_ecdsa) {
  1150. c->mask_a |= SSL_aECDSA;
  1151. c->mask_k |= SSL_kECDHe;
  1152. }
  1153. # ifndef OPENSSL_NO_KRB5
  1154. if (!kssl_tgt_is_available(s->kssl_ctx)) {
  1155. c->mask_a |= SSL_aKRB5;
  1156. c->mask_k |= SSL_kKRB5;
  1157. }
  1158. # endif
  1159. # ifndef OPENSSL_NO_PSK
  1160. /* with PSK there must be client callback set */
  1161. if (!s->psk_client_callback) {
  1162. c->mask_a |= SSL_aPSK;
  1163. c->mask_k |= SSL_kPSK;
  1164. }
  1165. # endif /* OPENSSL_NO_PSK */
  1166. # ifndef OPENSSL_NO_SRP
  1167. if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
  1168. c->mask_a |= SSL_aSRP;
  1169. c->mask_k |= SSL_kSRP;
  1170. }
  1171. # endif
  1172. c->valid = 1;
  1173. }
  1174. unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf,
  1175. unsigned char *limit, int *al)
  1176. {
  1177. int extdatalen = 0;
  1178. unsigned char *orig = buf;
  1179. unsigned char *ret = buf;
  1180. # ifndef OPENSSL_NO_EC
  1181. /* See if we support any ECC ciphersuites */
  1182. int using_ecc = 0;
  1183. if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
  1184. int i;
  1185. unsigned long alg_k, alg_a;
  1186. STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
  1187. for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
  1188. SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
  1189. alg_k = c->algorithm_mkey;
  1190. alg_a = c->algorithm_auth;
  1191. if ((alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)
  1192. || (alg_a & SSL_aECDSA))) {
  1193. using_ecc = 1;
  1194. break;
  1195. }
  1196. }
  1197. }
  1198. # endif
  1199. /* don't add extensions for SSLv3 unless doing secure renegotiation */
  1200. if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding)
  1201. return orig;
  1202. ret += 2;
  1203. if (ret >= limit)
  1204. return NULL; /* this really never occurs, but ... */
  1205. if (s->tlsext_hostname != NULL) {
  1206. /* Add TLS extension servername to the Client Hello message */
  1207. size_t size_str;
  1208. /*-
  1209. * check for enough space.
  1210. * 4 for the servername type and entension length
  1211. * 2 for servernamelist length
  1212. * 1 for the hostname type
  1213. * 2 for hostname length
  1214. * + hostname length
  1215. */
  1216. size_str = strlen(s->tlsext_hostname);
  1217. if (CHECKLEN(ret, 9 + size_str, limit))
  1218. return NULL;
  1219. /* extension type and length */
  1220. s2n(TLSEXT_TYPE_server_name, ret);
  1221. s2n(size_str + 5, ret);
  1222. /* length of servername list */
  1223. s2n(size_str + 3, ret);
  1224. /* hostname type, length and hostname */
  1225. *(ret++) = (unsigned char)TLSEXT_NAMETYPE_host_name;
  1226. s2n(size_str, ret);
  1227. memcpy(ret, s->tlsext_hostname, size_str);
  1228. ret += size_str;
  1229. }
  1230. /* Add RI if renegotiating */
  1231. if (s->renegotiate) {
  1232. int el;
  1233. if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
  1234. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  1235. return NULL;
  1236. }
  1237. if ((limit - ret - 4 - el) < 0)
  1238. return NULL;
  1239. s2n(TLSEXT_TYPE_renegotiate, ret);
  1240. s2n(el, ret);
  1241. if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
  1242. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  1243. return NULL;
  1244. }
  1245. ret += el;
  1246. }
  1247. # ifndef OPENSSL_NO_SRP
  1248. /* Add SRP username if there is one */
  1249. if (s->srp_ctx.login != NULL) { /* Add TLS extension SRP username to the
  1250. * Client Hello message */
  1251. size_t login_len = strlen(s->srp_ctx.login);
  1252. if (login_len > 255 || login_len == 0) {
  1253. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  1254. return NULL;
  1255. }
  1256. /*-
  1257. * check for enough space.
  1258. * 4 for the srp type type and entension length
  1259. * 1 for the srp user identity
  1260. * + srp user identity length
  1261. */
  1262. if (CHECKLEN(ret, 5 + login_len, limit))
  1263. return NULL;
  1264. /* fill in the extension */
  1265. s2n(TLSEXT_TYPE_srp, ret);
  1266. s2n(login_len + 1, ret);
  1267. (*ret++) = (unsigned char)login_len;
  1268. memcpy(ret, s->srp_ctx.login, login_len);
  1269. ret += login_len;
  1270. }
  1271. # endif
  1272. # ifndef OPENSSL_NO_EC
  1273. if (using_ecc) {
  1274. /*
  1275. * Add TLS extension ECPointFormats to the ClientHello message
  1276. */
  1277. const unsigned char *pcurves, *pformats;
  1278. size_t num_curves, num_formats, curves_list_len;
  1279. tls1_get_formatlist(s, &pformats, &num_formats);
  1280. if (num_formats > 255) {
  1281. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  1282. return NULL;
  1283. }
  1284. /*-
  1285. * check for enough space.
  1286. * 4 bytes for the ec point formats type and extension length
  1287. * 1 byte for the length of the formats
  1288. * + formats length
  1289. */
  1290. if (CHECKLEN(ret, 5 + num_formats, limit))
  1291. return NULL;
  1292. s2n(TLSEXT_TYPE_ec_point_formats, ret);
  1293. /* The point format list has 1-byte length. */
  1294. s2n(num_formats + 1, ret);
  1295. *(ret++) = (unsigned char)num_formats;
  1296. memcpy(ret, pformats, num_formats);
  1297. ret += num_formats;
  1298. /*
  1299. * Add TLS extension EllipticCurves to the ClientHello message
  1300. */
  1301. pcurves = s->tlsext_ellipticcurvelist;
  1302. if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves))
  1303. return NULL;
  1304. if (num_curves > 65532 / 2) {
  1305. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  1306. return NULL;
  1307. }
  1308. curves_list_len = 2 * num_curves;
  1309. /*-
  1310. * check for enough space.
  1311. * 4 bytes for the ec curves type and extension length
  1312. * 2 bytes for the curve list length
  1313. * + curve list length
  1314. */
  1315. if (CHECKLEN(ret, 6 + curves_list_len, limit))
  1316. return NULL;
  1317. s2n(TLSEXT_TYPE_elliptic_curves, ret);
  1318. s2n(curves_list_len + 2, ret);
  1319. s2n(curves_list_len, ret);
  1320. memcpy(ret, pcurves, curves_list_len);
  1321. ret += curves_list_len;
  1322. }
  1323. # endif /* OPENSSL_NO_EC */
  1324. if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
  1325. size_t ticklen;
  1326. if (!s->new_session && s->session && s->session->tlsext_tick)
  1327. ticklen = s->session->tlsext_ticklen;
  1328. else if (s->session && s->tlsext_session_ticket &&
  1329. s->tlsext_session_ticket->data) {
  1330. ticklen = s->tlsext_session_ticket->length;
  1331. s->session->tlsext_tick = OPENSSL_malloc(ticklen);
  1332. if (!s->session->tlsext_tick)
  1333. return NULL;
  1334. memcpy(s->session->tlsext_tick,
  1335. s->tlsext_session_ticket->data, ticklen);
  1336. s->session->tlsext_ticklen = ticklen;
  1337. } else
  1338. ticklen = 0;
  1339. if (ticklen == 0 && s->tlsext_session_ticket &&
  1340. s->tlsext_session_ticket->data == NULL)
  1341. goto skip_ext;
  1342. /*
  1343. * Check for enough room 2 for extension type, 2 for len rest for
  1344. * ticket
  1345. */
  1346. if (CHECKLEN(ret, 4 + ticklen, limit))
  1347. return NULL;
  1348. s2n(TLSEXT_TYPE_session_ticket, ret);
  1349. s2n(ticklen, ret);
  1350. if (ticklen > 0) {
  1351. memcpy(ret, s->session->tlsext_tick, ticklen);
  1352. ret += ticklen;
  1353. }
  1354. }
  1355. skip_ext:
  1356. if (SSL_CLIENT_USE_SIGALGS(s)) {
  1357. size_t salglen;
  1358. const unsigned char *salg;
  1359. salglen = tls12_get_psigalgs(s, &salg);
  1360. /*-
  1361. * check for enough space.
  1362. * 4 bytes for the sigalgs type and extension length
  1363. * 2 bytes for the sigalg list length
  1364. * + sigalg list length
  1365. */
  1366. if (CHECKLEN(ret, salglen + 6, limit))
  1367. return NULL;
  1368. s2n(TLSEXT_TYPE_signature_algorithms, ret);
  1369. s2n(salglen + 2, ret);
  1370. s2n(salglen, ret);
  1371. memcpy(ret, salg, salglen);
  1372. ret += salglen;
  1373. }
  1374. # ifdef TLSEXT_TYPE_opaque_prf_input
  1375. if (s->s3->client_opaque_prf_input != NULL) {
  1376. size_t col = s->s3->client_opaque_prf_input_len;
  1377. if ((long)(limit - ret - 6 - col < 0))
  1378. return NULL;
  1379. if (col > 0xFFFD) /* can't happen */
  1380. return NULL;
  1381. s2n(TLSEXT_TYPE_opaque_prf_input, ret);
  1382. s2n(col + 2, ret);
  1383. s2n(col, ret);
  1384. memcpy(ret, s->s3->client_opaque_prf_input, col);
  1385. ret += col;
  1386. }
  1387. # endif
  1388. if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
  1389. int i;
  1390. size_t extlen, idlen;
  1391. int lentmp;
  1392. OCSP_RESPID *id;
  1393. idlen = 0;
  1394. for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
  1395. id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
  1396. lentmp = i2d_OCSP_RESPID(id, NULL);
  1397. if (lentmp <= 0)
  1398. return NULL;
  1399. idlen += (size_t)lentmp + 2;
  1400. }
  1401. if (s->tlsext_ocsp_exts) {
  1402. lentmp = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
  1403. if (lentmp < 0)
  1404. return NULL;
  1405. extlen = (size_t)lentmp;
  1406. } else
  1407. extlen = 0;
  1408. if (extlen + idlen > 0xFFF0)
  1409. return NULL;
  1410. /*
  1411. * 2 bytes for status request type
  1412. * 2 bytes for status request len
  1413. * 1 byte for OCSP request type
  1414. * 2 bytes for length of ids
  1415. * 2 bytes for length of extensions
  1416. * + length of ids
  1417. * + length of extensions
  1418. */
  1419. if (CHECKLEN(ret, 9 + idlen + extlen, limit))
  1420. return NULL;
  1421. s2n(TLSEXT_TYPE_status_request, ret);
  1422. s2n(extlen + idlen + 5, ret);
  1423. *(ret++) = TLSEXT_STATUSTYPE_ocsp;
  1424. s2n(idlen, ret);
  1425. for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
  1426. /* save position of id len */
  1427. unsigned char *q = ret;
  1428. id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
  1429. /* skip over id len */
  1430. ret += 2;
  1431. lentmp = i2d_OCSP_RESPID(id, &ret);
  1432. /* write id len */
  1433. s2n(lentmp, q);
  1434. }
  1435. s2n(extlen, ret);
  1436. if (extlen > 0)
  1437. i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
  1438. }
  1439. # ifndef OPENSSL_NO_HEARTBEATS
  1440. /* Add Heartbeat extension */
  1441. /*-
  1442. * check for enough space.
  1443. * 4 bytes for the heartbeat ext type and extension length
  1444. * 1 byte for the mode
  1445. */
  1446. if (CHECKLEN(ret, 5, limit))
  1447. return NULL;
  1448. s2n(TLSEXT_TYPE_heartbeat, ret);
  1449. s2n(1, ret);
  1450. /*-
  1451. * Set mode:
  1452. * 1: peer may send requests
  1453. * 2: peer not allowed to send requests
  1454. */
  1455. if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
  1456. *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
  1457. else
  1458. *(ret++) = SSL_TLSEXT_HB_ENABLED;
  1459. # endif
  1460. # ifndef OPENSSL_NO_NEXTPROTONEG
  1461. if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {
  1462. /*
  1463. * The client advertises an emtpy extension to indicate its support
  1464. * for Next Protocol Negotiation
  1465. */
  1466. /*-
  1467. * check for enough space.
  1468. * 4 bytes for the NPN ext type and extension length
  1469. */
  1470. if (CHECKLEN(ret, 4, limit))
  1471. return NULL;
  1472. s2n(TLSEXT_TYPE_next_proto_neg, ret);
  1473. s2n(0, ret);
  1474. }
  1475. # endif
  1476. if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
  1477. /*-
  1478. * check for enough space.
  1479. * 4 bytes for the ALPN type and extension length
  1480. * 2 bytes for the ALPN protocol list length
  1481. * + ALPN protocol list length
  1482. */
  1483. if (CHECKLEN(ret, 6 + s->alpn_client_proto_list_len, limit))
  1484. return NULL;
  1485. s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
  1486. s2n(2 + s->alpn_client_proto_list_len, ret);
  1487. s2n(s->alpn_client_proto_list_len, ret);
  1488. memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
  1489. ret += s->alpn_client_proto_list_len;
  1490. s->cert->alpn_sent = 1;
  1491. }
  1492. # ifndef OPENSSL_NO_SRTP
  1493. if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) {
  1494. int el;
  1495. ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
  1496. /*-
  1497. * check for enough space.
  1498. * 4 bytes for the SRTP type and extension length
  1499. * + SRTP profiles length
  1500. */
  1501. if (CHECKLEN(ret, 4 + el, limit))
  1502. return NULL;
  1503. s2n(TLSEXT_TYPE_use_srtp, ret);
  1504. s2n(el, ret);
  1505. if (ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
  1506. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  1507. return NULL;
  1508. }
  1509. ret += el;
  1510. }
  1511. # endif
  1512. custom_ext_init(&s->cert->cli_ext);
  1513. /* Add custom TLS Extensions to ClientHello */
  1514. if (!custom_ext_add(s, 0, &ret, limit, al))
  1515. return NULL;
  1516. /*
  1517. * Add padding to workaround bugs in F5 terminators. See
  1518. * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this
  1519. * code works out the length of all existing extensions it MUST always
  1520. * appear last.
  1521. */
  1522. if (s->options & SSL_OP_TLSEXT_PADDING) {
  1523. int hlen = ret - (unsigned char *)s->init_buf->data;
  1524. /*
  1525. * The code in s23_clnt.c to build ClientHello messages includes the
  1526. * 5-byte record header in the buffer, while the code in s3_clnt.c
  1527. * does not.
  1528. */
  1529. if (s->state == SSL23_ST_CW_CLNT_HELLO_A)
  1530. hlen -= 5;
  1531. if (hlen > 0xff && hlen < 0x200) {
  1532. hlen = 0x200 - hlen;
  1533. if (hlen >= 4)
  1534. hlen -= 4;
  1535. else
  1536. hlen = 0;
  1537. /*-
  1538. * check for enough space. Strictly speaking we know we've already
  1539. * got enough space because to get here the message size is < 0x200,
  1540. * but we know that we've allocated far more than that in the buffer
  1541. * - but for consistency and robustness we're going to check anyway.
  1542. *
  1543. * 4 bytes for the padding type and extension length
  1544. * + padding length
  1545. */
  1546. if (CHECKLEN(ret, 4 + hlen, limit))
  1547. return NULL;
  1548. s2n(TLSEXT_TYPE_padding, ret);
  1549. s2n(hlen, ret);
  1550. memset(ret, 0, hlen);
  1551. ret += hlen;
  1552. }
  1553. }
  1554. if ((extdatalen = ret - orig - 2) == 0)
  1555. return orig;
  1556. s2n(extdatalen, orig);
  1557. return ret;
  1558. }
  1559. unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf,
  1560. unsigned char *limit, int *al)
  1561. {
  1562. int extdatalen = 0;
  1563. unsigned char *orig = buf;
  1564. unsigned char *ret = buf;
  1565. # ifndef OPENSSL_NO_NEXTPROTONEG
  1566. int next_proto_neg_seen;
  1567. # endif
  1568. # ifndef OPENSSL_NO_EC
  1569. unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1570. unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1571. int using_ecc = (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe))
  1572. || (alg_a & SSL_aECDSA);
  1573. using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
  1574. # endif
  1575. /*
  1576. * don't add extensions for SSLv3, unless doing secure renegotiation
  1577. */
  1578. if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
  1579. return orig;
  1580. ret += 2;
  1581. if (ret >= limit)
  1582. return NULL; /* this really never occurs, but ... */
  1583. if (!s->hit && s->servername_done == 1
  1584. && s->session->tlsext_hostname != NULL) {
  1585. if ((long)(limit - ret - 4) < 0)
  1586. return NULL;
  1587. s2n(TLSEXT_TYPE_server_name, ret);
  1588. s2n(0, ret);
  1589. }
  1590. if (s->s3->send_connection_binding) {
  1591. int el;
  1592. if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
  1593. SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  1594. return NULL;
  1595. }
  1596. /*-
  1597. * check for enough space.
  1598. * 4 bytes for the reneg type and extension length
  1599. * + reneg data length
  1600. */
  1601. if (CHECKLEN(ret, 4 + el, limit))
  1602. return NULL;
  1603. s2n(TLSEXT_TYPE_renegotiate, ret);
  1604. s2n(el, ret);
  1605. if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
  1606. SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  1607. return NULL;
  1608. }
  1609. ret += el;
  1610. }
  1611. # ifndef OPENSSL_NO_EC
  1612. if (using_ecc) {
  1613. const unsigned char *plist;
  1614. size_t plistlen;
  1615. /*
  1616. * Add TLS extension ECPointFormats to the ServerHello message
  1617. */
  1618. tls1_get_formatlist(s, &plist, &plistlen);
  1619. if (plistlen > 255) {
  1620. SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  1621. return NULL;
  1622. }
  1623. /*-
  1624. * check for enough space.
  1625. * 4 bytes for the ec points format type and extension length
  1626. * 1 byte for the points format list length
  1627. * + length of points format list
  1628. */
  1629. if (CHECKLEN(ret, 5 + plistlen, limit))
  1630. return NULL;
  1631. s2n(TLSEXT_TYPE_ec_point_formats, ret);
  1632. s2n(plistlen + 1, ret);
  1633. *(ret++) = (unsigned char)plistlen;
  1634. memcpy(ret, plist, plistlen);
  1635. ret += plistlen;
  1636. }
  1637. /*
  1638. * Currently the server should not respond with a SupportedCurves
  1639. * extension
  1640. */
  1641. # endif /* OPENSSL_NO_EC */
  1642. if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
  1643. /*-
  1644. * check for enough space.
  1645. * 4 bytes for the Ticket type and extension length
  1646. */
  1647. if (CHECKLEN(ret, 4, limit))
  1648. return NULL;
  1649. s2n(TLSEXT_TYPE_session_ticket, ret);
  1650. s2n(0, ret);
  1651. }
  1652. if (s->tlsext_status_expected) {
  1653. /*-
  1654. * check for enough space.
  1655. * 4 bytes for the Status request type and extension length
  1656. */
  1657. if (CHECKLEN(ret, 4, limit))
  1658. return NULL;
  1659. s2n(TLSEXT_TYPE_status_request, ret);
  1660. s2n(0, ret);
  1661. }
  1662. # ifdef TLSEXT_TYPE_opaque_prf_input
  1663. if (s->s3->server_opaque_prf_input != NULL) {
  1664. size_t sol = s->s3->server_opaque_prf_input_len;
  1665. if ((long)(limit - ret - 6 - sol) < 0)
  1666. return NULL;
  1667. if (sol > 0xFFFD) /* can't happen */
  1668. return NULL;
  1669. s2n(TLSEXT_TYPE_opaque_prf_input, ret);
  1670. s2n(sol + 2, ret);
  1671. s2n(sol, ret);
  1672. memcpy(ret, s->s3->server_opaque_prf_input, sol);
  1673. ret += sol;
  1674. }
  1675. # endif
  1676. # ifndef OPENSSL_NO_SRTP
  1677. if (SSL_IS_DTLS(s) && s->srtp_profile) {
  1678. int el;
  1679. ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
  1680. /*-
  1681. * check for enough space.
  1682. * 4 bytes for the SRTP profiles type and extension length
  1683. * + length of the SRTP profiles list
  1684. */
  1685. if (CHECKLEN(ret, 4 + el, limit))
  1686. return NULL;
  1687. s2n(TLSEXT_TYPE_use_srtp, ret);
  1688. s2n(el, ret);
  1689. if (ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
  1690. SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  1691. return NULL;
  1692. }
  1693. ret += el;
  1694. }
  1695. # endif
  1696. if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80
  1697. || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81)
  1698. && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) {
  1699. const unsigned char cryptopro_ext[36] = {
  1700. 0xfd, 0xe8, /* 65000 */
  1701. 0x00, 0x20, /* 32 bytes length */
  1702. 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
  1703. 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
  1704. 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
  1705. 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
  1706. };
  1707. /* check for enough space. */
  1708. if (CHECKLEN(ret, sizeof(cryptopro_ext), limit))
  1709. return NULL;
  1710. memcpy(ret, cryptopro_ext, sizeof(cryptopro_ext));
  1711. ret += sizeof(cryptopro_ext);
  1712. }
  1713. # ifndef OPENSSL_NO_HEARTBEATS
  1714. /* Add Heartbeat extension if we've received one */
  1715. if (s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) {
  1716. /*-
  1717. * check for enough space.
  1718. * 4 bytes for the Heartbeat type and extension length
  1719. * 1 byte for the mode
  1720. */
  1721. if (CHECKLEN(ret, 5, limit))
  1722. return NULL;
  1723. s2n(TLSEXT_TYPE_heartbeat, ret);
  1724. s2n(1, ret);
  1725. /*-
  1726. * Set mode:
  1727. * 1: peer may send requests
  1728. * 2: peer not allowed to send requests
  1729. */
  1730. if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
  1731. *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
  1732. else
  1733. *(ret++) = SSL_TLSEXT_HB_ENABLED;
  1734. }
  1735. # endif
  1736. # ifndef OPENSSL_NO_NEXTPROTONEG
  1737. next_proto_neg_seen = s->s3->next_proto_neg_seen;
  1738. s->s3->next_proto_neg_seen = 0;
  1739. if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
  1740. const unsigned char *npa;
  1741. unsigned int npalen;
  1742. int r;
  1743. r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen,
  1744. s->
  1745. ctx->next_protos_advertised_cb_arg);
  1746. if (r == SSL_TLSEXT_ERR_OK) {
  1747. /*-
  1748. * check for enough space.
  1749. * 4 bytes for the NPN type and extension length
  1750. * + length of protocols list
  1751. */
  1752. if (CHECKLEN(ret, 4 + npalen, limit))
  1753. return NULL;
  1754. s2n(TLSEXT_TYPE_next_proto_neg, ret);
  1755. s2n(npalen, ret);
  1756. memcpy(ret, npa, npalen);
  1757. ret += npalen;
  1758. s->s3->next_proto_neg_seen = 1;
  1759. }
  1760. }
  1761. # endif
  1762. if (!custom_ext_add(s, 1, &ret, limit, al))
  1763. return NULL;
  1764. if (s->s3->alpn_selected) {
  1765. const unsigned char *selected = s->s3->alpn_selected;
  1766. size_t len = s->s3->alpn_selected_len;
  1767. /*-
  1768. * check for enough space.
  1769. * 4 bytes for the ALPN type and extension length
  1770. * 2 bytes for ALPN data length
  1771. * 1 byte for selected protocol length
  1772. * + length of the selected protocol
  1773. */
  1774. if (CHECKLEN(ret, 7 + len, limit))
  1775. return NULL;
  1776. s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
  1777. s2n(3 + len, ret);
  1778. s2n(1 + len, ret);
  1779. *ret++ = len;
  1780. memcpy(ret, selected, len);
  1781. ret += len;
  1782. }
  1783. if ((extdatalen = ret - orig - 2) == 0)
  1784. return orig;
  1785. s2n(extdatalen, orig);
  1786. return ret;
  1787. }
  1788. # ifndef OPENSSL_NO_EC
  1789. /*-
  1790. * ssl_check_for_safari attempts to fingerprint Safari using OS X
  1791. * SecureTransport using the TLS extension block in |d|, of length |n|.
  1792. * Safari, since 10.6, sends exactly these extensions, in this order:
  1793. * SNI,
  1794. * elliptic_curves
  1795. * ec_point_formats
  1796. *
  1797. * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
  1798. * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
  1799. * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
  1800. * 10.8..10.8.3 (which don't work).
  1801. */
  1802. static void ssl_check_for_safari(SSL *s, const unsigned char *data,
  1803. const unsigned char *limit)
  1804. {
  1805. unsigned short type, size;
  1806. static const unsigned char kSafariExtensionsBlock[] = {
  1807. 0x00, 0x0a, /* elliptic_curves extension */
  1808. 0x00, 0x08, /* 8 bytes */
  1809. 0x00, 0x06, /* 6 bytes of curve ids */
  1810. 0x00, 0x17, /* P-256 */
  1811. 0x00, 0x18, /* P-384 */
  1812. 0x00, 0x19, /* P-521 */
  1813. 0x00, 0x0b, /* ec_point_formats */
  1814. 0x00, 0x02, /* 2 bytes */
  1815. 0x01, /* 1 point format */
  1816. 0x00, /* uncompressed */
  1817. };
  1818. /* The following is only present in TLS 1.2 */
  1819. static const unsigned char kSafariTLS12ExtensionsBlock[] = {
  1820. 0x00, 0x0d, /* signature_algorithms */
  1821. 0x00, 0x0c, /* 12 bytes */
  1822. 0x00, 0x0a, /* 10 bytes */
  1823. 0x05, 0x01, /* SHA-384/RSA */
  1824. 0x04, 0x01, /* SHA-256/RSA */
  1825. 0x02, 0x01, /* SHA-1/RSA */
  1826. 0x04, 0x03, /* SHA-256/ECDSA */
  1827. 0x02, 0x03, /* SHA-1/ECDSA */
  1828. };
  1829. if (limit - data <= 2)
  1830. return;
  1831. data += 2;
  1832. if (limit - data < 4)
  1833. return;
  1834. n2s(data, type);
  1835. n2s(data, size);
  1836. if (type != TLSEXT_TYPE_server_name)
  1837. return;
  1838. if (limit - data < size)
  1839. return;
  1840. data += size;
  1841. if (TLS1_get_client_version(s) >= TLS1_2_VERSION) {
  1842. const size_t len1 = sizeof(kSafariExtensionsBlock);
  1843. const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock);
  1844. if (limit - data != (int)(len1 + len2))
  1845. return;
  1846. if (memcmp(data, kSafariExtensionsBlock, len1) != 0)
  1847. return;
  1848. if (memcmp(data + len1, kSafariTLS12ExtensionsBlock, len2) != 0)
  1849. return;
  1850. } else {
  1851. const size_t len = sizeof(kSafariExtensionsBlock);
  1852. if (limit - data != (int)(len))
  1853. return;
  1854. if (memcmp(data, kSafariExtensionsBlock, len) != 0)
  1855. return;
  1856. }
  1857. s->s3->is_probably_safari = 1;
  1858. }
  1859. # endif /* !OPENSSL_NO_EC */
  1860. /*
  1861. * tls1_alpn_handle_client_hello is called to save the ALPN extension in a
  1862. * ClientHello. data: the contents of the extension, not including the type
  1863. * and length. data_len: the number of bytes in |data| al: a pointer to the
  1864. * alert value to send in the event of a non-zero return. returns: 0 on
  1865. * success.
  1866. */
  1867. static int tls1_alpn_handle_client_hello(SSL *s, const unsigned char *data,
  1868. unsigned data_len, int *al)
  1869. {
  1870. unsigned i;
  1871. unsigned proto_len;
  1872. if (data_len < 2)
  1873. goto parse_error;
  1874. /*
  1875. * data should contain a uint16 length followed by a series of 8-bit,
  1876. * length-prefixed strings.
  1877. */
  1878. i = ((unsigned)data[0]) << 8 | ((unsigned)data[1]);
  1879. data_len -= 2;
  1880. data += 2;
  1881. if (data_len != i)
  1882. goto parse_error;
  1883. if (data_len < 2)
  1884. goto parse_error;
  1885. for (i = 0; i < data_len;) {
  1886. proto_len = data[i];
  1887. i++;
  1888. if (proto_len == 0)
  1889. goto parse_error;
  1890. if (i + proto_len < i || i + proto_len > data_len)
  1891. goto parse_error;
  1892. i += proto_len;
  1893. }
  1894. if (s->cert->alpn_proposed != NULL)
  1895. OPENSSL_free(s->cert->alpn_proposed);
  1896. s->cert->alpn_proposed = OPENSSL_malloc(data_len);
  1897. if (s->cert->alpn_proposed == NULL) {
  1898. *al = SSL_AD_INTERNAL_ERROR;
  1899. return -1;
  1900. }
  1901. memcpy(s->cert->alpn_proposed, data, data_len);
  1902. s->cert->alpn_proposed_len = data_len;
  1903. return 0;
  1904. parse_error:
  1905. *al = SSL_AD_DECODE_ERROR;
  1906. return -1;
  1907. }
  1908. /*
  1909. * Process the ALPN extension in a ClientHello.
  1910. * al: a pointer to the alert value to send in the event of a failure.
  1911. * returns 1 on success, 0 on failure: al set only on failure
  1912. */
  1913. static int tls1_alpn_handle_client_hello_late(SSL *s, int *al)
  1914. {
  1915. const unsigned char *selected = NULL;
  1916. unsigned char selected_len = 0;
  1917. if (s->ctx->alpn_select_cb != NULL && s->cert->alpn_proposed != NULL) {
  1918. int r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
  1919. s->cert->alpn_proposed,
  1920. s->cert->alpn_proposed_len,
  1921. s->ctx->alpn_select_cb_arg);
  1922. if (r == SSL_TLSEXT_ERR_OK) {
  1923. OPENSSL_free(s->s3->alpn_selected);
  1924. s->s3->alpn_selected = OPENSSL_malloc(selected_len);
  1925. if (s->s3->alpn_selected == NULL) {
  1926. *al = SSL_AD_INTERNAL_ERROR;
  1927. return 0;
  1928. }
  1929. memcpy(s->s3->alpn_selected, selected, selected_len);
  1930. s->s3->alpn_selected_len = selected_len;
  1931. # ifndef OPENSSL_NO_NEXTPROTONEG
  1932. /* ALPN takes precedence over NPN. */
  1933. s->s3->next_proto_neg_seen = 0;
  1934. # endif
  1935. }
  1936. }
  1937. return 1;
  1938. }
  1939. static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p,
  1940. unsigned char *limit, int *al)
  1941. {
  1942. unsigned short type;
  1943. unsigned short size;
  1944. unsigned short len;
  1945. unsigned char *data = *p;
  1946. int renegotiate_seen = 0;
  1947. s->servername_done = 0;
  1948. s->tlsext_status_type = -1;
  1949. # ifndef OPENSSL_NO_NEXTPROTONEG
  1950. s->s3->next_proto_neg_seen = 0;
  1951. # endif
  1952. if (s->s3->alpn_selected) {
  1953. OPENSSL_free(s->s3->alpn_selected);
  1954. s->s3->alpn_selected = NULL;
  1955. }
  1956. s->s3->alpn_selected_len = 0;
  1957. if (s->cert->alpn_proposed) {
  1958. OPENSSL_free(s->cert->alpn_proposed);
  1959. s->cert->alpn_proposed = NULL;
  1960. }
  1961. s->cert->alpn_proposed_len = 0;
  1962. # ifndef OPENSSL_NO_HEARTBEATS
  1963. s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
  1964. SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
  1965. # endif
  1966. # ifndef OPENSSL_NO_EC
  1967. if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
  1968. ssl_check_for_safari(s, data, limit);
  1969. # endif /* !OPENSSL_NO_EC */
  1970. /* Clear any signature algorithms extension received */
  1971. if (s->cert->peer_sigalgs) {
  1972. OPENSSL_free(s->cert->peer_sigalgs);
  1973. s->cert->peer_sigalgs = NULL;
  1974. }
  1975. # ifndef OPENSSL_NO_SRP
  1976. if (s->srp_ctx.login != NULL) {
  1977. OPENSSL_free(s->srp_ctx.login);
  1978. s->srp_ctx.login = NULL;
  1979. }
  1980. # endif
  1981. s->srtp_profile = NULL;
  1982. if (data == limit)
  1983. goto ri_check;
  1984. if (limit - data < 2)
  1985. goto err;
  1986. n2s(data, len);
  1987. if (limit - data != len)
  1988. goto err;
  1989. while (limit - data >= 4) {
  1990. n2s(data, type);
  1991. n2s(data, size);
  1992. if (limit - data < size)
  1993. goto err;
  1994. # if 0
  1995. fprintf(stderr, "Received extension type %d size %d\n", type, size);
  1996. # endif
  1997. if (s->tlsext_debug_cb)
  1998. s->tlsext_debug_cb(s, 0, type, data, size, s->tlsext_debug_arg);
  1999. /*-
  2000. * The servername extension is treated as follows:
  2001. *
  2002. * - Only the hostname type is supported with a maximum length of 255.
  2003. * - The servername is rejected if too long or if it contains zeros,
  2004. * in which case an fatal alert is generated.
  2005. * - The servername field is maintained together with the session cache.
  2006. * - When a session is resumed, the servername call back invoked in order
  2007. * to allow the application to position itself to the right context.
  2008. * - The servername is acknowledged if it is new for a session or when
  2009. * it is identical to a previously used for the same session.
  2010. * Applications can control the behaviour. They can at any time
  2011. * set a 'desirable' servername for a new SSL object. This can be the
  2012. * case for example with HTTPS when a Host: header field is received and
  2013. * a renegotiation is requested. In this case, a possible servername
  2014. * presented in the new client hello is only acknowledged if it matches
  2015. * the value of the Host: field.
  2016. * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
  2017. * if they provide for changing an explicit servername context for the
  2018. * session, i.e. when the session has been established with a servername
  2019. * extension.
  2020. * - On session reconnect, the servername extension may be absent.
  2021. *
  2022. */
  2023. if (type == TLSEXT_TYPE_server_name) {
  2024. unsigned char *sdata;
  2025. int servname_type;
  2026. int dsize;
  2027. if (size < 2)
  2028. goto err;
  2029. n2s(data, dsize);
  2030. size -= 2;
  2031. if (dsize > size)
  2032. goto err;
  2033. sdata = data;
  2034. while (dsize > 3) {
  2035. servname_type = *(sdata++);
  2036. n2s(sdata, len);
  2037. dsize -= 3;
  2038. if (len > dsize)
  2039. goto err;
  2040. if (s->servername_done == 0)
  2041. switch (servname_type) {
  2042. case TLSEXT_NAMETYPE_host_name:
  2043. if (!s->hit) {
  2044. if (s->session->tlsext_hostname)
  2045. goto err;
  2046. if (len > TLSEXT_MAXLEN_host_name) {
  2047. *al = TLS1_AD_UNRECOGNIZED_NAME;
  2048. return 0;
  2049. }
  2050. if ((s->session->tlsext_hostname =
  2051. OPENSSL_malloc(len + 1)) == NULL) {
  2052. *al = TLS1_AD_INTERNAL_ERROR;
  2053. return 0;
  2054. }
  2055. memcpy(s->session->tlsext_hostname, sdata, len);
  2056. s->session->tlsext_hostname[len] = '\0';
  2057. if (strlen(s->session->tlsext_hostname) != len) {
  2058. OPENSSL_free(s->session->tlsext_hostname);
  2059. s->session->tlsext_hostname = NULL;
  2060. *al = TLS1_AD_UNRECOGNIZED_NAME;
  2061. return 0;
  2062. }
  2063. s->servername_done = 1;
  2064. } else
  2065. s->servername_done = s->session->tlsext_hostname
  2066. && strlen(s->session->tlsext_hostname) == len
  2067. && strncmp(s->session->tlsext_hostname,
  2068. (char *)sdata, len) == 0;
  2069. break;
  2070. default:
  2071. break;
  2072. }
  2073. dsize -= len;
  2074. }
  2075. if (dsize != 0)
  2076. goto err;
  2077. }
  2078. # ifndef OPENSSL_NO_SRP
  2079. else if (type == TLSEXT_TYPE_srp) {
  2080. if (size == 0 || ((len = data[0])) != (size - 1))
  2081. goto err;
  2082. if (s->srp_ctx.login != NULL)
  2083. goto err;
  2084. if ((s->srp_ctx.login = OPENSSL_malloc(len + 1)) == NULL)
  2085. return -1;
  2086. memcpy(s->srp_ctx.login, &data[1], len);
  2087. s->srp_ctx.login[len] = '\0';
  2088. if (strlen(s->srp_ctx.login) != len)
  2089. goto err;
  2090. }
  2091. # endif
  2092. # ifndef OPENSSL_NO_EC
  2093. else if (type == TLSEXT_TYPE_ec_point_formats) {
  2094. unsigned char *sdata = data;
  2095. int ecpointformatlist_length = *(sdata++);
  2096. if (ecpointformatlist_length != size - 1 ||
  2097. ecpointformatlist_length < 1)
  2098. goto err;
  2099. if (!s->hit) {
  2100. if (s->session->tlsext_ecpointformatlist) {
  2101. OPENSSL_free(s->session->tlsext_ecpointformatlist);
  2102. s->session->tlsext_ecpointformatlist = NULL;
  2103. }
  2104. s->session->tlsext_ecpointformatlist_length = 0;
  2105. if ((s->session->tlsext_ecpointformatlist =
  2106. OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
  2107. *al = TLS1_AD_INTERNAL_ERROR;
  2108. return 0;
  2109. }
  2110. s->session->tlsext_ecpointformatlist_length =
  2111. ecpointformatlist_length;
  2112. memcpy(s->session->tlsext_ecpointformatlist, sdata,
  2113. ecpointformatlist_length);
  2114. }
  2115. # if 0
  2116. fprintf(stderr,
  2117. "ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ",
  2118. s->session->tlsext_ecpointformatlist_length);
  2119. sdata = s->session->tlsext_ecpointformatlist;
  2120. for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
  2121. fprintf(stderr, "%i ", *(sdata++));
  2122. fprintf(stderr, "\n");
  2123. # endif
  2124. } else if (type == TLSEXT_TYPE_elliptic_curves) {
  2125. unsigned char *sdata = data;
  2126. int ellipticcurvelist_length = (*(sdata++) << 8);
  2127. ellipticcurvelist_length += (*(sdata++));
  2128. if (ellipticcurvelist_length != size - 2 ||
  2129. ellipticcurvelist_length < 1 ||
  2130. /* Each NamedCurve is 2 bytes. */
  2131. ellipticcurvelist_length & 1)
  2132. goto err;
  2133. if (!s->hit) {
  2134. if (s->session->tlsext_ellipticcurvelist)
  2135. goto err;
  2136. s->session->tlsext_ellipticcurvelist_length = 0;
  2137. if ((s->session->tlsext_ellipticcurvelist =
  2138. OPENSSL_malloc(ellipticcurvelist_length)) == NULL) {
  2139. *al = TLS1_AD_INTERNAL_ERROR;
  2140. return 0;
  2141. }
  2142. s->session->tlsext_ellipticcurvelist_length =
  2143. ellipticcurvelist_length;
  2144. memcpy(s->session->tlsext_ellipticcurvelist, sdata,
  2145. ellipticcurvelist_length);
  2146. }
  2147. # if 0
  2148. fprintf(stderr,
  2149. "ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ",
  2150. s->session->tlsext_ellipticcurvelist_length);
  2151. sdata = s->session->tlsext_ellipticcurvelist;
  2152. for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
  2153. fprintf(stderr, "%i ", *(sdata++));
  2154. fprintf(stderr, "\n");
  2155. # endif
  2156. }
  2157. # endif /* OPENSSL_NO_EC */
  2158. # ifdef TLSEXT_TYPE_opaque_prf_input
  2159. else if (type == TLSEXT_TYPE_opaque_prf_input) {
  2160. unsigned char *sdata = data;
  2161. if (size < 2) {
  2162. *al = SSL_AD_DECODE_ERROR;
  2163. return 0;
  2164. }
  2165. n2s(sdata, s->s3->client_opaque_prf_input_len);
  2166. if (s->s3->client_opaque_prf_input_len != size - 2) {
  2167. *al = SSL_AD_DECODE_ERROR;
  2168. return 0;
  2169. }
  2170. if (s->s3->client_opaque_prf_input != NULL) {
  2171. /* shouldn't really happen */
  2172. OPENSSL_free(s->s3->client_opaque_prf_input);
  2173. }
  2174. /* dummy byte just to get non-NULL */
  2175. if (s->s3->client_opaque_prf_input_len == 0)
  2176. s->s3->client_opaque_prf_input = OPENSSL_malloc(1);
  2177. else
  2178. s->s3->client_opaque_prf_input =
  2179. BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
  2180. if (s->s3->client_opaque_prf_input == NULL) {
  2181. *al = TLS1_AD_INTERNAL_ERROR;
  2182. return 0;
  2183. }
  2184. }
  2185. # endif
  2186. else if (type == TLSEXT_TYPE_session_ticket) {
  2187. if (s->tls_session_ticket_ext_cb &&
  2188. !s->tls_session_ticket_ext_cb(s, data, size,
  2189. s->tls_session_ticket_ext_cb_arg))
  2190. {
  2191. *al = TLS1_AD_INTERNAL_ERROR;
  2192. return 0;
  2193. }
  2194. } else if (type == TLSEXT_TYPE_renegotiate) {
  2195. if (!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
  2196. return 0;
  2197. renegotiate_seen = 1;
  2198. } else if (type == TLSEXT_TYPE_signature_algorithms) {
  2199. int dsize;
  2200. if (s->cert->peer_sigalgs || size < 2)
  2201. goto err;
  2202. n2s(data, dsize);
  2203. size -= 2;
  2204. if (dsize != size || dsize & 1 || !dsize)
  2205. goto err;
  2206. if (!tls1_save_sigalgs(s, data, dsize))
  2207. goto err;
  2208. } else if (type == TLSEXT_TYPE_status_request) {
  2209. if (size < 5)
  2210. goto err;
  2211. s->tlsext_status_type = *data++;
  2212. size--;
  2213. if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
  2214. const unsigned char *sdata;
  2215. int dsize;
  2216. /* Read in responder_id_list */
  2217. n2s(data, dsize);
  2218. size -= 2;
  2219. if (dsize > size)
  2220. goto err;
  2221. /*
  2222. * We remove any OCSP_RESPIDs from a previous handshake
  2223. * to prevent unbounded memory growth - CVE-2016-6304
  2224. */
  2225. sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids,
  2226. OCSP_RESPID_free);
  2227. if (dsize > 0) {
  2228. s->tlsext_ocsp_ids = sk_OCSP_RESPID_new_null();
  2229. if (s->tlsext_ocsp_ids == NULL) {
  2230. *al = SSL_AD_INTERNAL_ERROR;
  2231. return 0;
  2232. }
  2233. } else {
  2234. s->tlsext_ocsp_ids = NULL;
  2235. }
  2236. while (dsize > 0) {
  2237. OCSP_RESPID *id;
  2238. int idsize;
  2239. if (dsize < 4)
  2240. goto err;
  2241. n2s(data, idsize);
  2242. dsize -= 2 + idsize;
  2243. size -= 2 + idsize;
  2244. if (dsize < 0)
  2245. goto err;
  2246. sdata = data;
  2247. data += idsize;
  2248. id = d2i_OCSP_RESPID(NULL, &sdata, idsize);
  2249. if (!id)
  2250. goto err;
  2251. if (data != sdata) {
  2252. OCSP_RESPID_free(id);
  2253. goto err;
  2254. }
  2255. if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) {
  2256. OCSP_RESPID_free(id);
  2257. *al = SSL_AD_INTERNAL_ERROR;
  2258. return 0;
  2259. }
  2260. }
  2261. /* Read in request_extensions */
  2262. if (size < 2)
  2263. goto err;
  2264. n2s(data, dsize);
  2265. size -= 2;
  2266. if (dsize != size)
  2267. goto err;
  2268. sdata = data;
  2269. if (dsize > 0) {
  2270. if (s->tlsext_ocsp_exts) {
  2271. sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
  2272. X509_EXTENSION_free);
  2273. }
  2274. s->tlsext_ocsp_exts =
  2275. d2i_X509_EXTENSIONS(NULL, &sdata, dsize);
  2276. if (!s->tlsext_ocsp_exts || (data + dsize != sdata))
  2277. goto err;
  2278. }
  2279. }
  2280. /*
  2281. * We don't know what to do with any other type * so ignore it.
  2282. */
  2283. else
  2284. s->tlsext_status_type = -1;
  2285. }
  2286. # ifndef OPENSSL_NO_HEARTBEATS
  2287. else if (type == TLSEXT_TYPE_heartbeat) {
  2288. switch (data[0]) {
  2289. case 0x01: /* Client allows us to send HB requests */
  2290. s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
  2291. break;
  2292. case 0x02: /* Client doesn't accept HB requests */
  2293. s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
  2294. s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
  2295. break;
  2296. default:
  2297. *al = SSL_AD_ILLEGAL_PARAMETER;
  2298. return 0;
  2299. }
  2300. }
  2301. # endif
  2302. # ifndef OPENSSL_NO_NEXTPROTONEG
  2303. else if (type == TLSEXT_TYPE_next_proto_neg &&
  2304. s->s3->tmp.finish_md_len == 0) {
  2305. /*-
  2306. * We shouldn't accept this extension on a
  2307. * renegotiation.
  2308. *
  2309. * s->new_session will be set on renegotiation, but we
  2310. * probably shouldn't rely that it couldn't be set on
  2311. * the initial renegotation too in certain cases (when
  2312. * there's some other reason to disallow resuming an
  2313. * earlier session -- the current code won't be doing
  2314. * anything like that, but this might change).
  2315. *
  2316. * A valid sign that there's been a previous handshake
  2317. * in this connection is if s->s3->tmp.finish_md_len >
  2318. * 0. (We are talking about a check that will happen
  2319. * in the Hello protocol round, well before a new
  2320. * Finished message could have been computed.)
  2321. */
  2322. s->s3->next_proto_neg_seen = 1;
  2323. }
  2324. # endif
  2325. else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
  2326. s->s3->tmp.finish_md_len == 0) {
  2327. if (tls1_alpn_handle_client_hello(s, data, size, al) != 0)
  2328. return 0;
  2329. }
  2330. /* session ticket processed earlier */
  2331. # ifndef OPENSSL_NO_SRTP
  2332. else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)
  2333. && type == TLSEXT_TYPE_use_srtp) {
  2334. if (ssl_parse_clienthello_use_srtp_ext(s, data, size, al))
  2335. return 0;
  2336. }
  2337. # endif
  2338. data += size;
  2339. }
  2340. /* Spurious data on the end */
  2341. if (data != limit)
  2342. goto err;
  2343. *p = data;
  2344. ri_check:
  2345. /* Need RI if renegotiating */
  2346. if (!renegotiate_seen && s->renegotiate &&
  2347. !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
  2348. *al = SSL_AD_HANDSHAKE_FAILURE;
  2349. SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
  2350. SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  2351. return 0;
  2352. }
  2353. return 1;
  2354. err:
  2355. *al = SSL_AD_DECODE_ERROR;
  2356. return 0;
  2357. }
  2358. /*
  2359. * Parse any custom extensions found. "data" is the start of the extension data
  2360. * and "limit" is the end of the record. TODO: add strict syntax checking.
  2361. */
  2362. static int ssl_scan_clienthello_custom_tlsext(SSL *s,
  2363. const unsigned char *data,
  2364. const unsigned char *limit,
  2365. int *al)
  2366. {
  2367. unsigned short type, size, len;
  2368. /* If resumed session or no custom extensions nothing to do */
  2369. if (s->hit || s->cert->srv_ext.meths_count == 0)
  2370. return 1;
  2371. if (limit - data <= 2)
  2372. return 1;
  2373. n2s(data, len);
  2374. if (limit - data < len)
  2375. return 1;
  2376. while (limit - data >= 4) {
  2377. n2s(data, type);
  2378. n2s(data, size);
  2379. if (limit - data < size)
  2380. return 1;
  2381. if (custom_ext_parse(s, 1 /* server */ , type, data, size, al) <= 0)
  2382. return 0;
  2383. data += size;
  2384. }
  2385. return 1;
  2386. }
  2387. int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p,
  2388. unsigned char *limit)
  2389. {
  2390. int al = -1;
  2391. unsigned char *ptmp = *p;
  2392. /*
  2393. * Internally supported extensions are parsed first so SNI can be handled
  2394. * before custom extensions. An application processing SNI will typically
  2395. * switch the parent context using SSL_set_SSL_CTX and custom extensions
  2396. * need to be handled by the new SSL_CTX structure.
  2397. */
  2398. if (ssl_scan_clienthello_tlsext(s, p, limit, &al) <= 0) {
  2399. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  2400. return 0;
  2401. }
  2402. if (ssl_check_clienthello_tlsext_early(s) <= 0) {
  2403. SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, SSL_R_CLIENTHELLO_TLSEXT);
  2404. return 0;
  2405. }
  2406. custom_ext_init(&s->cert->srv_ext);
  2407. if (ssl_scan_clienthello_custom_tlsext(s, ptmp, limit, &al) <= 0) {
  2408. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  2409. return 0;
  2410. }
  2411. return 1;
  2412. }
  2413. # ifndef OPENSSL_NO_NEXTPROTONEG
  2414. /*
  2415. * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
  2416. * elements of zero length are allowed and the set of elements must exactly
  2417. * fill the length of the block.
  2418. */
  2419. static char ssl_next_proto_validate(unsigned char *d, unsigned len)
  2420. {
  2421. unsigned int off = 0;
  2422. while (off < len) {
  2423. if (d[off] == 0)
  2424. return 0;
  2425. off += d[off];
  2426. off++;
  2427. }
  2428. return off == len;
  2429. }
  2430. # endif
  2431. static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p,
  2432. unsigned char *d, int n, int *al)
  2433. {
  2434. unsigned short length;
  2435. unsigned short type;
  2436. unsigned short size;
  2437. unsigned char *data = *p;
  2438. int tlsext_servername = 0;
  2439. int renegotiate_seen = 0;
  2440. # ifndef OPENSSL_NO_NEXTPROTONEG
  2441. s->s3->next_proto_neg_seen = 0;
  2442. # endif
  2443. s->tlsext_ticket_expected = 0;
  2444. if (s->s3->alpn_selected) {
  2445. OPENSSL_free(s->s3->alpn_selected);
  2446. s->s3->alpn_selected = NULL;
  2447. }
  2448. # ifndef OPENSSL_NO_HEARTBEATS
  2449. s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
  2450. SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
  2451. # endif
  2452. if ((d + n) - data <= 2)
  2453. goto ri_check;
  2454. n2s(data, length);
  2455. if ((d + n) - data != length) {
  2456. *al = SSL_AD_DECODE_ERROR;
  2457. return 0;
  2458. }
  2459. while ((d + n) - data >= 4) {
  2460. n2s(data, type);
  2461. n2s(data, size);
  2462. if ((d + n) - data < size)
  2463. goto ri_check;
  2464. if (s->tlsext_debug_cb)
  2465. s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg);
  2466. if (type == TLSEXT_TYPE_server_name) {
  2467. if (s->tlsext_hostname == NULL || size > 0) {
  2468. *al = TLS1_AD_UNRECOGNIZED_NAME;
  2469. return 0;
  2470. }
  2471. tlsext_servername = 1;
  2472. }
  2473. # ifndef OPENSSL_NO_EC
  2474. else if (type == TLSEXT_TYPE_ec_point_formats) {
  2475. unsigned char *sdata = data;
  2476. int ecpointformatlist_length = *(sdata++);
  2477. if (ecpointformatlist_length != size - 1) {
  2478. *al = TLS1_AD_DECODE_ERROR;
  2479. return 0;
  2480. }
  2481. if (!s->hit) {
  2482. s->session->tlsext_ecpointformatlist_length = 0;
  2483. if (s->session->tlsext_ecpointformatlist != NULL)
  2484. OPENSSL_free(s->session->tlsext_ecpointformatlist);
  2485. if ((s->session->tlsext_ecpointformatlist =
  2486. OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
  2487. *al = TLS1_AD_INTERNAL_ERROR;
  2488. return 0;
  2489. }
  2490. s->session->tlsext_ecpointformatlist_length =
  2491. ecpointformatlist_length;
  2492. memcpy(s->session->tlsext_ecpointformatlist, sdata,
  2493. ecpointformatlist_length);
  2494. }
  2495. # if 0
  2496. fprintf(stderr,
  2497. "ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
  2498. sdata = s->session->tlsext_ecpointformatlist;
  2499. for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
  2500. fprintf(stderr, "%i ", *(sdata++));
  2501. fprintf(stderr, "\n");
  2502. # endif
  2503. }
  2504. # endif /* OPENSSL_NO_EC */
  2505. else if (type == TLSEXT_TYPE_session_ticket) {
  2506. if (s->tls_session_ticket_ext_cb &&
  2507. !s->tls_session_ticket_ext_cb(s, data, size,
  2508. s->tls_session_ticket_ext_cb_arg))
  2509. {
  2510. *al = TLS1_AD_INTERNAL_ERROR;
  2511. return 0;
  2512. }
  2513. if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
  2514. || (size > 0)) {
  2515. *al = TLS1_AD_UNSUPPORTED_EXTENSION;
  2516. return 0;
  2517. }
  2518. s->tlsext_ticket_expected = 1;
  2519. }
  2520. # ifdef TLSEXT_TYPE_opaque_prf_input
  2521. else if (type == TLSEXT_TYPE_opaque_prf_input) {
  2522. unsigned char *sdata = data;
  2523. if (size < 2) {
  2524. *al = SSL_AD_DECODE_ERROR;
  2525. return 0;
  2526. }
  2527. n2s(sdata, s->s3->server_opaque_prf_input_len);
  2528. if (s->s3->server_opaque_prf_input_len != size - 2) {
  2529. *al = SSL_AD_DECODE_ERROR;
  2530. return 0;
  2531. }
  2532. if (s->s3->server_opaque_prf_input != NULL) {
  2533. /* shouldn't really happen */
  2534. OPENSSL_free(s->s3->server_opaque_prf_input);
  2535. }
  2536. if (s->s3->server_opaque_prf_input_len == 0) {
  2537. /* dummy byte just to get non-NULL */
  2538. s->s3->server_opaque_prf_input = OPENSSL_malloc(1);
  2539. } else {
  2540. s->s3->server_opaque_prf_input =
  2541. BUF_memdup(sdata, s->s3->server_opaque_prf_input_len);
  2542. }
  2543. if (s->s3->server_opaque_prf_input == NULL) {
  2544. *al = TLS1_AD_INTERNAL_ERROR;
  2545. return 0;
  2546. }
  2547. }
  2548. # endif
  2549. else if (type == TLSEXT_TYPE_status_request) {
  2550. /*
  2551. * MUST be empty and only sent if we've requested a status
  2552. * request message.
  2553. */
  2554. if ((s->tlsext_status_type == -1) || (size > 0)) {
  2555. *al = TLS1_AD_UNSUPPORTED_EXTENSION;
  2556. return 0;
  2557. }
  2558. /* Set flag to expect CertificateStatus message */
  2559. s->tlsext_status_expected = 1;
  2560. }
  2561. # ifndef OPENSSL_NO_NEXTPROTONEG
  2562. else if (type == TLSEXT_TYPE_next_proto_neg &&
  2563. s->s3->tmp.finish_md_len == 0) {
  2564. unsigned char *selected;
  2565. unsigned char selected_len;
  2566. /* We must have requested it. */
  2567. if (s->ctx->next_proto_select_cb == NULL) {
  2568. *al = TLS1_AD_UNSUPPORTED_EXTENSION;
  2569. return 0;
  2570. }
  2571. /* The data must be valid */
  2572. if (!ssl_next_proto_validate(data, size)) {
  2573. *al = TLS1_AD_DECODE_ERROR;
  2574. return 0;
  2575. }
  2576. if (s->
  2577. ctx->next_proto_select_cb(s, &selected, &selected_len, data,
  2578. size,
  2579. s->ctx->next_proto_select_cb_arg) !=
  2580. SSL_TLSEXT_ERR_OK) {
  2581. *al = TLS1_AD_INTERNAL_ERROR;
  2582. return 0;
  2583. }
  2584. /*
  2585. * Could be non-NULL if server has sent multiple NPN extensions in
  2586. * a single Serverhello
  2587. */
  2588. OPENSSL_free(s->next_proto_negotiated);
  2589. s->next_proto_negotiated = OPENSSL_malloc(selected_len);
  2590. if (!s->next_proto_negotiated) {
  2591. *al = TLS1_AD_INTERNAL_ERROR;
  2592. return 0;
  2593. }
  2594. memcpy(s->next_proto_negotiated, selected, selected_len);
  2595. s->next_proto_negotiated_len = selected_len;
  2596. s->s3->next_proto_neg_seen = 1;
  2597. }
  2598. # endif
  2599. else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
  2600. unsigned len;
  2601. /* We must have requested it. */
  2602. if (!s->cert->alpn_sent) {
  2603. *al = TLS1_AD_UNSUPPORTED_EXTENSION;
  2604. return 0;
  2605. }
  2606. if (size < 4) {
  2607. *al = TLS1_AD_DECODE_ERROR;
  2608. return 0;
  2609. }
  2610. /*-
  2611. * The extension data consists of:
  2612. * uint16 list_length
  2613. * uint8 proto_length;
  2614. * uint8 proto[proto_length];
  2615. */
  2616. len = data[0];
  2617. len <<= 8;
  2618. len |= data[1];
  2619. if (len != (unsigned)size - 2) {
  2620. *al = TLS1_AD_DECODE_ERROR;
  2621. return 0;
  2622. }
  2623. len = data[2];
  2624. if (len != (unsigned)size - 3) {
  2625. *al = TLS1_AD_DECODE_ERROR;
  2626. return 0;
  2627. }
  2628. if (s->s3->alpn_selected)
  2629. OPENSSL_free(s->s3->alpn_selected);
  2630. s->s3->alpn_selected = OPENSSL_malloc(len);
  2631. if (!s->s3->alpn_selected) {
  2632. *al = TLS1_AD_INTERNAL_ERROR;
  2633. return 0;
  2634. }
  2635. memcpy(s->s3->alpn_selected, data + 3, len);
  2636. s->s3->alpn_selected_len = len;
  2637. }
  2638. else if (type == TLSEXT_TYPE_renegotiate) {
  2639. if (!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
  2640. return 0;
  2641. renegotiate_seen = 1;
  2642. }
  2643. # ifndef OPENSSL_NO_HEARTBEATS
  2644. else if (type == TLSEXT_TYPE_heartbeat) {
  2645. switch (data[0]) {
  2646. case 0x01: /* Server allows us to send HB requests */
  2647. s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
  2648. break;
  2649. case 0x02: /* Server doesn't accept HB requests */
  2650. s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
  2651. s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
  2652. break;
  2653. default:
  2654. *al = SSL_AD_ILLEGAL_PARAMETER;
  2655. return 0;
  2656. }
  2657. }
  2658. # endif
  2659. # ifndef OPENSSL_NO_SRTP
  2660. else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) {
  2661. if (ssl_parse_serverhello_use_srtp_ext(s, data, size, al))
  2662. return 0;
  2663. }
  2664. # endif
  2665. /*
  2666. * If this extension type was not otherwise handled, but matches a
  2667. * custom_cli_ext_record, then send it to the c callback
  2668. */
  2669. else if (custom_ext_parse(s, 0, type, data, size, al) <= 0)
  2670. return 0;
  2671. data += size;
  2672. }
  2673. if (data != d + n) {
  2674. *al = SSL_AD_DECODE_ERROR;
  2675. return 0;
  2676. }
  2677. if (!s->hit && tlsext_servername == 1) {
  2678. if (s->tlsext_hostname) {
  2679. if (s->session->tlsext_hostname == NULL) {
  2680. s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
  2681. if (!s->session->tlsext_hostname) {
  2682. *al = SSL_AD_UNRECOGNIZED_NAME;
  2683. return 0;
  2684. }
  2685. } else {
  2686. *al = SSL_AD_DECODE_ERROR;
  2687. return 0;
  2688. }
  2689. }
  2690. }
  2691. *p = data;
  2692. ri_check:
  2693. /*
  2694. * Determine if we need to see RI. Strictly speaking if we want to avoid
  2695. * an attack we should *always* see RI even on initial server hello
  2696. * because the client doesn't see any renegotiation during an attack.
  2697. * However this would mean we could not connect to any server which
  2698. * doesn't support RI so for the immediate future tolerate RI absence on
  2699. * initial connect only.
  2700. */
  2701. if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
  2702. && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
  2703. *al = SSL_AD_HANDSHAKE_FAILURE;
  2704. SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
  2705. SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  2706. return 0;
  2707. }
  2708. return 1;
  2709. }
  2710. int ssl_prepare_clienthello_tlsext(SSL *s)
  2711. {
  2712. # ifdef TLSEXT_TYPE_opaque_prf_input
  2713. {
  2714. int r = 1;
  2715. if (s->ctx->tlsext_opaque_prf_input_callback != 0) {
  2716. r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0,
  2717. s->
  2718. ctx->tlsext_opaque_prf_input_callback_arg);
  2719. if (!r)
  2720. return -1;
  2721. }
  2722. if (s->tlsext_opaque_prf_input != NULL) {
  2723. if (s->s3->client_opaque_prf_input != NULL) {
  2724. /* shouldn't really happen */
  2725. OPENSSL_free(s->s3->client_opaque_prf_input);
  2726. }
  2727. if (s->tlsext_opaque_prf_input_len == 0) {
  2728. /* dummy byte just to get non-NULL */
  2729. s->s3->client_opaque_prf_input = OPENSSL_malloc(1);
  2730. } else {
  2731. s->s3->client_opaque_prf_input =
  2732. BUF_memdup(s->tlsext_opaque_prf_input,
  2733. s->tlsext_opaque_prf_input_len);
  2734. }
  2735. if (s->s3->client_opaque_prf_input == NULL) {
  2736. SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,
  2737. ERR_R_MALLOC_FAILURE);
  2738. return -1;
  2739. }
  2740. s->s3->client_opaque_prf_input_len =
  2741. s->tlsext_opaque_prf_input_len;
  2742. }
  2743. if (r == 2)
  2744. /*
  2745. * at callback's request, insist on receiving an appropriate
  2746. * server opaque PRF input
  2747. */
  2748. s->s3->server_opaque_prf_input_len =
  2749. s->tlsext_opaque_prf_input_len;
  2750. }
  2751. # endif
  2752. s->cert->alpn_sent = 0;
  2753. return 1;
  2754. }
  2755. int ssl_prepare_serverhello_tlsext(SSL *s)
  2756. {
  2757. return 1;
  2758. }
  2759. static int ssl_check_clienthello_tlsext_early(SSL *s)
  2760. {
  2761. int ret = SSL_TLSEXT_ERR_NOACK;
  2762. int al = SSL_AD_UNRECOGNIZED_NAME;
  2763. # ifndef OPENSSL_NO_EC
  2764. /*
  2765. * The handling of the ECPointFormats extension is done elsewhere, namely
  2766. * in ssl3_choose_cipher in s3_lib.c.
  2767. */
  2768. /*
  2769. * The handling of the EllipticCurves extension is done elsewhere, namely
  2770. * in ssl3_choose_cipher in s3_lib.c.
  2771. */
  2772. # endif
  2773. if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
  2774. ret =
  2775. s->ctx->tlsext_servername_callback(s, &al,
  2776. s->ctx->tlsext_servername_arg);
  2777. else if (s->initial_ctx != NULL
  2778. && s->initial_ctx->tlsext_servername_callback != 0)
  2779. ret =
  2780. s->initial_ctx->tlsext_servername_callback(s, &al,
  2781. s->
  2782. initial_ctx->tlsext_servername_arg);
  2783. # ifdef TLSEXT_TYPE_opaque_prf_input
  2784. {
  2785. /*
  2786. * This sort of belongs into ssl_prepare_serverhello_tlsext(), but we
  2787. * might be sending an alert in response to the client hello, so this
  2788. * has to happen here in ssl_check_clienthello_tlsext_early().
  2789. */
  2790. int r = 1;
  2791. if (s->ctx->tlsext_opaque_prf_input_callback != 0) {
  2792. r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0,
  2793. s->
  2794. ctx->tlsext_opaque_prf_input_callback_arg);
  2795. if (!r) {
  2796. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  2797. al = SSL_AD_INTERNAL_ERROR;
  2798. goto err;
  2799. }
  2800. }
  2801. if (s->s3->server_opaque_prf_input != NULL) {
  2802. /* shouldn't really happen */
  2803. OPENSSL_free(s->s3->server_opaque_prf_input);
  2804. }
  2805. s->s3->server_opaque_prf_input = NULL;
  2806. if (s->tlsext_opaque_prf_input != NULL) {
  2807. if (s->s3->client_opaque_prf_input != NULL &&
  2808. s->s3->client_opaque_prf_input_len ==
  2809. s->tlsext_opaque_prf_input_len) {
  2810. /*
  2811. * can only use this extension if we have a server opaque PRF
  2812. * input of the same length as the client opaque PRF input!
  2813. */
  2814. if (s->tlsext_opaque_prf_input_len == 0) {
  2815. /* dummy byte just to get non-NULL */
  2816. s->s3->server_opaque_prf_input = OPENSSL_malloc(1);
  2817. } else {
  2818. s->s3->server_opaque_prf_input =
  2819. BUF_memdup(s->tlsext_opaque_prf_input,
  2820. s->tlsext_opaque_prf_input_len);
  2821. }
  2822. if (s->s3->server_opaque_prf_input == NULL) {
  2823. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  2824. al = SSL_AD_INTERNAL_ERROR;
  2825. goto err;
  2826. }
  2827. s->s3->server_opaque_prf_input_len =
  2828. s->tlsext_opaque_prf_input_len;
  2829. }
  2830. }
  2831. if (r == 2 && s->s3->server_opaque_prf_input == NULL) {
  2832. /*
  2833. * The callback wants to enforce use of the extension, but we
  2834. * can't do that with the client opaque PRF input; abort the
  2835. * handshake.
  2836. */
  2837. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  2838. al = SSL_AD_HANDSHAKE_FAILURE;
  2839. }
  2840. }
  2841. err:
  2842. # endif
  2843. switch (ret) {
  2844. case SSL_TLSEXT_ERR_ALERT_FATAL:
  2845. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  2846. return -1;
  2847. case SSL_TLSEXT_ERR_ALERT_WARNING:
  2848. ssl3_send_alert(s, SSL3_AL_WARNING, al);
  2849. return 1;
  2850. case SSL_TLSEXT_ERR_NOACK:
  2851. s->servername_done = 0;
  2852. default:
  2853. return 1;
  2854. }
  2855. }
  2856. int tls1_set_server_sigalgs(SSL *s)
  2857. {
  2858. int al;
  2859. size_t i;
  2860. /* Clear any shared sigtnature algorithms */
  2861. if (s->cert->shared_sigalgs) {
  2862. OPENSSL_free(s->cert->shared_sigalgs);
  2863. s->cert->shared_sigalgs = NULL;
  2864. s->cert->shared_sigalgslen = 0;
  2865. }
  2866. /* Clear certificate digests and validity flags */
  2867. for (i = 0; i < SSL_PKEY_NUM; i++) {
  2868. s->cert->pkeys[i].digest = NULL;
  2869. s->cert->pkeys[i].valid_flags = 0;
  2870. }
  2871. /* If sigalgs received process it. */
  2872. if (s->cert->peer_sigalgs) {
  2873. if (!tls1_process_sigalgs(s)) {
  2874. SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE);
  2875. al = SSL_AD_INTERNAL_ERROR;
  2876. goto err;
  2877. }
  2878. /* Fatal error is no shared signature algorithms */
  2879. if (!s->cert->shared_sigalgs) {
  2880. SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS,
  2881. SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
  2882. al = SSL_AD_ILLEGAL_PARAMETER;
  2883. goto err;
  2884. }
  2885. } else
  2886. ssl_cert_set_default_md(s->cert);
  2887. return 1;
  2888. err:
  2889. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  2890. return 0;
  2891. }
  2892. /*
  2893. * Upon success, returns 1.
  2894. * Upon failure, returns 0 and sets |al| to the appropriate fatal alert.
  2895. */
  2896. int ssl_check_clienthello_tlsext_late(SSL *s, int *al)
  2897. {
  2898. /*
  2899. * If status request then ask callback what to do. Note: this must be
  2900. * called after servername callbacks in case the certificate has changed,
  2901. * and must be called after the cipher has been chosen because this may
  2902. * influence which certificate is sent
  2903. */
  2904. if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) {
  2905. int ret;
  2906. CERT_PKEY *certpkey;
  2907. certpkey = ssl_get_server_send_pkey(s);
  2908. /* If no certificate can't return certificate status */
  2909. if (certpkey != NULL) {
  2910. /*
  2911. * Set current certificate to one we will use so SSL_get_certificate
  2912. * et al can pick it up.
  2913. */
  2914. s->cert->key = certpkey;
  2915. ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
  2916. switch (ret) {
  2917. /* We don't want to send a status request response */
  2918. case SSL_TLSEXT_ERR_NOACK:
  2919. s->tlsext_status_expected = 0;
  2920. break;
  2921. /* status request response should be sent */
  2922. case SSL_TLSEXT_ERR_OK:
  2923. if (s->tlsext_ocsp_resp)
  2924. s->tlsext_status_expected = 1;
  2925. break;
  2926. /* something bad happened */
  2927. case SSL_TLSEXT_ERR_ALERT_FATAL:
  2928. default:
  2929. *al = SSL_AD_INTERNAL_ERROR;
  2930. return 0;
  2931. }
  2932. }
  2933. }
  2934. if (!tls1_alpn_handle_client_hello_late(s, al)) {
  2935. return 0;
  2936. }
  2937. return 1;
  2938. }
  2939. int ssl_check_serverhello_tlsext(SSL *s)
  2940. {
  2941. int ret = SSL_TLSEXT_ERR_NOACK;
  2942. int al = SSL_AD_UNRECOGNIZED_NAME;
  2943. # ifndef OPENSSL_NO_EC
  2944. /*
  2945. * If we are client and using an elliptic curve cryptography cipher
  2946. * suite, then if server returns an EC point formats lists extension it
  2947. * must contain uncompressed.
  2948. */
  2949. unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  2950. unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  2951. if ((s->tlsext_ecpointformatlist != NULL)
  2952. && (s->tlsext_ecpointformatlist_length > 0)
  2953. && (s->session->tlsext_ecpointformatlist != NULL)
  2954. && (s->session->tlsext_ecpointformatlist_length > 0)
  2955. && ((alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe))
  2956. || (alg_a & SSL_aECDSA))) {
  2957. /* we are using an ECC cipher */
  2958. size_t i;
  2959. unsigned char *list;
  2960. int found_uncompressed = 0;
  2961. list = s->session->tlsext_ecpointformatlist;
  2962. for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) {
  2963. if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) {
  2964. found_uncompressed = 1;
  2965. break;
  2966. }
  2967. }
  2968. if (!found_uncompressed) {
  2969. SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,
  2970. SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
  2971. return -1;
  2972. }
  2973. }
  2974. ret = SSL_TLSEXT_ERR_OK;
  2975. # endif /* OPENSSL_NO_EC */
  2976. if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
  2977. ret =
  2978. s->ctx->tlsext_servername_callback(s, &al,
  2979. s->ctx->tlsext_servername_arg);
  2980. else if (s->initial_ctx != NULL
  2981. && s->initial_ctx->tlsext_servername_callback != 0)
  2982. ret =
  2983. s->initial_ctx->tlsext_servername_callback(s, &al,
  2984. s->
  2985. initial_ctx->tlsext_servername_arg);
  2986. # ifdef TLSEXT_TYPE_opaque_prf_input
  2987. if (s->s3->server_opaque_prf_input_len > 0) {
  2988. /*
  2989. * This case may indicate that we, as a client, want to insist on
  2990. * using opaque PRF inputs. So first verify that we really have a
  2991. * value from the server too.
  2992. */
  2993. if (s->s3->server_opaque_prf_input == NULL) {
  2994. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  2995. al = SSL_AD_HANDSHAKE_FAILURE;
  2996. }
  2997. /*
  2998. * Anytime the server *has* sent an opaque PRF input, we need to
  2999. * check that we have a client opaque PRF input of the same size.
  3000. */
  3001. if (s->s3->client_opaque_prf_input == NULL ||
  3002. s->s3->client_opaque_prf_input_len !=
  3003. s->s3->server_opaque_prf_input_len) {
  3004. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  3005. al = SSL_AD_ILLEGAL_PARAMETER;
  3006. }
  3007. }
  3008. # endif
  3009. OPENSSL_free(s->tlsext_ocsp_resp);
  3010. s->tlsext_ocsp_resp = NULL;
  3011. s->tlsext_ocsp_resplen = -1;
  3012. /*
  3013. * If we've requested certificate status and we wont get one tell the
  3014. * callback
  3015. */
  3016. if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
  3017. && !(s->hit) && s->ctx && s->ctx->tlsext_status_cb) {
  3018. int r;
  3019. /*
  3020. * Call callback with resp == NULL and resplen == -1 so callback
  3021. * knows there is no response
  3022. */
  3023. r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
  3024. if (r == 0) {
  3025. al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
  3026. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  3027. }
  3028. if (r < 0) {
  3029. al = SSL_AD_INTERNAL_ERROR;
  3030. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  3031. }
  3032. }
  3033. switch (ret) {
  3034. case SSL_TLSEXT_ERR_ALERT_FATAL:
  3035. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  3036. return -1;
  3037. case SSL_TLSEXT_ERR_ALERT_WARNING:
  3038. ssl3_send_alert(s, SSL3_AL_WARNING, al);
  3039. return 1;
  3040. case SSL_TLSEXT_ERR_NOACK:
  3041. s->servername_done = 0;
  3042. default:
  3043. return 1;
  3044. }
  3045. }
  3046. int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d,
  3047. int n)
  3048. {
  3049. int al = -1;
  3050. if (s->version < SSL3_VERSION)
  3051. return 1;
  3052. if (ssl_scan_serverhello_tlsext(s, p, d, n, &al) <= 0) {
  3053. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  3054. return 0;
  3055. }
  3056. if (ssl_check_serverhello_tlsext(s) <= 0) {
  3057. SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, SSL_R_SERVERHELLO_TLSEXT);
  3058. return 0;
  3059. }
  3060. return 1;
  3061. }
  3062. /*-
  3063. * Since the server cache lookup is done early on in the processing of the
  3064. * ClientHello, and other operations depend on the result, we need to handle
  3065. * any TLS session ticket extension at the same time.
  3066. *
  3067. * session_id: points at the session ID in the ClientHello. This code will
  3068. * read past the end of this in order to parse out the session ticket
  3069. * extension, if any.
  3070. * len: the length of the session ID.
  3071. * limit: a pointer to the first byte after the ClientHello.
  3072. * ret: (output) on return, if a ticket was decrypted, then this is set to
  3073. * point to the resulting session.
  3074. *
  3075. * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
  3076. * ciphersuite, in which case we have no use for session tickets and one will
  3077. * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
  3078. *
  3079. * Returns:
  3080. * -1: fatal error, either from parsing or decrypting the ticket.
  3081. * 0: no ticket was found (or was ignored, based on settings).
  3082. * 1: a zero length extension was found, indicating that the client supports
  3083. * session tickets but doesn't currently have one to offer.
  3084. * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
  3085. * couldn't be decrypted because of a non-fatal error.
  3086. * 3: a ticket was successfully decrypted and *ret was set.
  3087. *
  3088. * Side effects:
  3089. * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
  3090. * a new session ticket to the client because the client indicated support
  3091. * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
  3092. * a session ticket or we couldn't use the one it gave us, or if
  3093. * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
  3094. * Otherwise, s->tlsext_ticket_expected is set to 0.
  3095. */
  3096. int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
  3097. const unsigned char *limit, SSL_SESSION **ret)
  3098. {
  3099. /* Point after session ID in client hello */
  3100. const unsigned char *p = session_id + len;
  3101. unsigned short i;
  3102. *ret = NULL;
  3103. s->tlsext_ticket_expected = 0;
  3104. /*
  3105. * If tickets disabled behave as if no ticket present to permit stateful
  3106. * resumption.
  3107. */
  3108. if (SSL_get_options(s) & SSL_OP_NO_TICKET)
  3109. return 0;
  3110. if ((s->version <= SSL3_VERSION) || !limit)
  3111. return 0;
  3112. if (p >= limit)
  3113. return -1;
  3114. /* Skip past DTLS cookie */
  3115. if (SSL_IS_DTLS(s)) {
  3116. i = *(p++);
  3117. if (limit - p <= i)
  3118. return -1;
  3119. p += i;
  3120. }
  3121. /* Skip past cipher list */
  3122. n2s(p, i);
  3123. if (limit - p <= i)
  3124. return -1;
  3125. p += i;
  3126. /* Skip past compression algorithm list */
  3127. i = *(p++);
  3128. if (limit - p < i)
  3129. return -1;
  3130. p += i;
  3131. /* Now at start of extensions */
  3132. if (limit - p <= 2)
  3133. return 0;
  3134. n2s(p, i);
  3135. while (limit - p >= 4) {
  3136. unsigned short type, size;
  3137. n2s(p, type);
  3138. n2s(p, size);
  3139. if (limit - p < size)
  3140. return 0;
  3141. if (type == TLSEXT_TYPE_session_ticket) {
  3142. int r;
  3143. if (size == 0) {
  3144. /*
  3145. * The client will accept a ticket but doesn't currently have
  3146. * one.
  3147. */
  3148. s->tlsext_ticket_expected = 1;
  3149. return 1;
  3150. }
  3151. if (s->tls_session_secret_cb) {
  3152. /*
  3153. * Indicate that the ticket couldn't be decrypted rather than
  3154. * generating the session from ticket now, trigger
  3155. * abbreviated handshake based on external mechanism to
  3156. * calculate the master secret later.
  3157. */
  3158. return 2;
  3159. }
  3160. r = tls_decrypt_ticket(s, p, size, session_id, len, ret);
  3161. switch (r) {
  3162. case 2: /* ticket couldn't be decrypted */
  3163. s->tlsext_ticket_expected = 1;
  3164. return 2;
  3165. case 3: /* ticket was decrypted */
  3166. return r;
  3167. case 4: /* ticket decrypted but need to renew */
  3168. s->tlsext_ticket_expected = 1;
  3169. return 3;
  3170. default: /* fatal error */
  3171. return -1;
  3172. }
  3173. }
  3174. p += size;
  3175. }
  3176. return 0;
  3177. }
  3178. /*-
  3179. * tls_decrypt_ticket attempts to decrypt a session ticket.
  3180. *
  3181. * etick: points to the body of the session ticket extension.
  3182. * eticklen: the length of the session tickets extenion.
  3183. * sess_id: points at the session ID.
  3184. * sesslen: the length of the session ID.
  3185. * psess: (output) on return, if a ticket was decrypted, then this is set to
  3186. * point to the resulting session.
  3187. *
  3188. * Returns:
  3189. * -1: fatal error, either from parsing or decrypting the ticket.
  3190. * 2: the ticket couldn't be decrypted.
  3191. * 3: a ticket was successfully decrypted and *psess was set.
  3192. * 4: same as 3, but the ticket needs to be renewed.
  3193. */
  3194. static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
  3195. int eticklen, const unsigned char *sess_id,
  3196. int sesslen, SSL_SESSION **psess)
  3197. {
  3198. SSL_SESSION *sess;
  3199. unsigned char *sdec;
  3200. const unsigned char *p;
  3201. int slen, mlen, renew_ticket = 0;
  3202. unsigned char tick_hmac[EVP_MAX_MD_SIZE];
  3203. HMAC_CTX hctx;
  3204. EVP_CIPHER_CTX ctx;
  3205. SSL_CTX *tctx = s->initial_ctx;
  3206. /* Initialize session ticket encryption and HMAC contexts */
  3207. HMAC_CTX_init(&hctx);
  3208. EVP_CIPHER_CTX_init(&ctx);
  3209. if (tctx->tlsext_ticket_key_cb) {
  3210. unsigned char *nctick = (unsigned char *)etick;
  3211. int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
  3212. &ctx, &hctx, 0);
  3213. if (rv < 0)
  3214. return -1;
  3215. if (rv == 0)
  3216. return 2;
  3217. if (rv == 2)
  3218. renew_ticket = 1;
  3219. } else {
  3220. /* Check key name matches */
  3221. if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
  3222. return 2;
  3223. if (HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
  3224. tlsext_tick_md(), NULL) <= 0
  3225. || EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
  3226. tctx->tlsext_tick_aes_key,
  3227. etick + 16) <= 0) {
  3228. goto err;
  3229. }
  3230. }
  3231. /*
  3232. * Attempt to process session ticket, first conduct sanity and integrity
  3233. * checks on ticket.
  3234. */
  3235. mlen = HMAC_size(&hctx);
  3236. if (mlen < 0) {
  3237. goto err;
  3238. }
  3239. /* Sanity check ticket length: must exceed keyname + IV + HMAC */
  3240. if (eticklen <= 16 + EVP_CIPHER_CTX_iv_length(&ctx) + mlen) {
  3241. HMAC_CTX_cleanup(&hctx);
  3242. EVP_CIPHER_CTX_cleanup(&ctx);
  3243. return 2;
  3244. }
  3245. eticklen -= mlen;
  3246. /* Check HMAC of encrypted ticket */
  3247. if (HMAC_Update(&hctx, etick, eticklen) <= 0
  3248. || HMAC_Final(&hctx, tick_hmac, NULL) <= 0) {
  3249. goto err;
  3250. }
  3251. HMAC_CTX_cleanup(&hctx);
  3252. if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
  3253. EVP_CIPHER_CTX_cleanup(&ctx);
  3254. return 2;
  3255. }
  3256. /* Attempt to decrypt session data */
  3257. /* Move p after IV to start of encrypted ticket, update length */
  3258. p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
  3259. eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
  3260. sdec = OPENSSL_malloc(eticklen);
  3261. if (sdec == NULL
  3262. || EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen) <= 0) {
  3263. EVP_CIPHER_CTX_cleanup(&ctx);
  3264. OPENSSL_free(sdec);
  3265. return -1;
  3266. }
  3267. if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0) {
  3268. EVP_CIPHER_CTX_cleanup(&ctx);
  3269. OPENSSL_free(sdec);
  3270. return 2;
  3271. }
  3272. slen += mlen;
  3273. EVP_CIPHER_CTX_cleanup(&ctx);
  3274. p = sdec;
  3275. sess = d2i_SSL_SESSION(NULL, &p, slen);
  3276. OPENSSL_free(sdec);
  3277. if (sess) {
  3278. /*
  3279. * The session ID, if non-empty, is used by some clients to detect
  3280. * that the ticket has been accepted. So we copy it to the session
  3281. * structure. If it is empty set length to zero as required by
  3282. * standard.
  3283. */
  3284. if (sesslen)
  3285. memcpy(sess->session_id, sess_id, sesslen);
  3286. sess->session_id_length = sesslen;
  3287. *psess = sess;
  3288. if (renew_ticket)
  3289. return 4;
  3290. else
  3291. return 3;
  3292. }
  3293. ERR_clear_error();
  3294. /*
  3295. * For session parse failure, indicate that we need to send a new ticket.
  3296. */
  3297. return 2;
  3298. err:
  3299. EVP_CIPHER_CTX_cleanup(&ctx);
  3300. HMAC_CTX_cleanup(&hctx);
  3301. return -1;
  3302. }
  3303. /* Tables to translate from NIDs to TLS v1.2 ids */
  3304. typedef struct {
  3305. int nid;
  3306. int id;
  3307. } tls12_lookup;
  3308. static tls12_lookup tls12_md[] = {
  3309. {NID_md5, TLSEXT_hash_md5},
  3310. {NID_sha1, TLSEXT_hash_sha1},
  3311. {NID_sha224, TLSEXT_hash_sha224},
  3312. {NID_sha256, TLSEXT_hash_sha256},
  3313. {NID_sha384, TLSEXT_hash_sha384},
  3314. {NID_sha512, TLSEXT_hash_sha512}
  3315. };
  3316. static tls12_lookup tls12_sig[] = {
  3317. {EVP_PKEY_RSA, TLSEXT_signature_rsa},
  3318. {EVP_PKEY_DSA, TLSEXT_signature_dsa},
  3319. {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
  3320. };
  3321. static int tls12_find_id(int nid, tls12_lookup *table, size_t tlen)
  3322. {
  3323. size_t i;
  3324. for (i = 0; i < tlen; i++) {
  3325. if (table[i].nid == nid)
  3326. return table[i].id;
  3327. }
  3328. return -1;
  3329. }
  3330. static int tls12_find_nid(int id, tls12_lookup *table, size_t tlen)
  3331. {
  3332. size_t i;
  3333. for (i = 0; i < tlen; i++) {
  3334. if ((table[i].id) == id)
  3335. return table[i].nid;
  3336. }
  3337. return NID_undef;
  3338. }
  3339. int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk,
  3340. const EVP_MD *md)
  3341. {
  3342. int sig_id, md_id;
  3343. if (!md)
  3344. return 0;
  3345. md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
  3346. sizeof(tls12_md) / sizeof(tls12_lookup));
  3347. if (md_id == -1)
  3348. return 0;
  3349. sig_id = tls12_get_sigid(pk);
  3350. if (sig_id == -1)
  3351. return 0;
  3352. p[0] = (unsigned char)md_id;
  3353. p[1] = (unsigned char)sig_id;
  3354. return 1;
  3355. }
  3356. int tls12_get_sigid(const EVP_PKEY *pk)
  3357. {
  3358. return tls12_find_id(pk->type, tls12_sig,
  3359. sizeof(tls12_sig) / sizeof(tls12_lookup));
  3360. }
  3361. const EVP_MD *tls12_get_hash(unsigned char hash_alg)
  3362. {
  3363. switch (hash_alg) {
  3364. # ifndef OPENSSL_NO_MD5
  3365. case TLSEXT_hash_md5:
  3366. # ifdef OPENSSL_FIPS
  3367. if (FIPS_mode())
  3368. return NULL;
  3369. # endif
  3370. return EVP_md5();
  3371. # endif
  3372. # ifndef OPENSSL_NO_SHA
  3373. case TLSEXT_hash_sha1:
  3374. return EVP_sha1();
  3375. # endif
  3376. # ifndef OPENSSL_NO_SHA256
  3377. case TLSEXT_hash_sha224:
  3378. return EVP_sha224();
  3379. case TLSEXT_hash_sha256:
  3380. return EVP_sha256();
  3381. # endif
  3382. # ifndef OPENSSL_NO_SHA512
  3383. case TLSEXT_hash_sha384:
  3384. return EVP_sha384();
  3385. case TLSEXT_hash_sha512:
  3386. return EVP_sha512();
  3387. # endif
  3388. default:
  3389. return NULL;
  3390. }
  3391. }
  3392. static int tls12_get_pkey_idx(unsigned char sig_alg)
  3393. {
  3394. switch (sig_alg) {
  3395. # ifndef OPENSSL_NO_RSA
  3396. case TLSEXT_signature_rsa:
  3397. return SSL_PKEY_RSA_SIGN;
  3398. # endif
  3399. # ifndef OPENSSL_NO_DSA
  3400. case TLSEXT_signature_dsa:
  3401. return SSL_PKEY_DSA_SIGN;
  3402. # endif
  3403. # ifndef OPENSSL_NO_ECDSA
  3404. case TLSEXT_signature_ecdsa:
  3405. return SSL_PKEY_ECC;
  3406. # endif
  3407. }
  3408. return -1;
  3409. }
  3410. /* Convert TLS 1.2 signature algorithm extension values into NIDs */
  3411. static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
  3412. int *psignhash_nid, const unsigned char *data)
  3413. {
  3414. int sign_nid = NID_undef, hash_nid = NID_undef;
  3415. if (!phash_nid && !psign_nid && !psignhash_nid)
  3416. return;
  3417. if (phash_nid || psignhash_nid) {
  3418. hash_nid = tls12_find_nid(data[0], tls12_md,
  3419. sizeof(tls12_md) / sizeof(tls12_lookup));
  3420. if (phash_nid)
  3421. *phash_nid = hash_nid;
  3422. }
  3423. if (psign_nid || psignhash_nid) {
  3424. sign_nid = tls12_find_nid(data[1], tls12_sig,
  3425. sizeof(tls12_sig) / sizeof(tls12_lookup));
  3426. if (psign_nid)
  3427. *psign_nid = sign_nid;
  3428. }
  3429. if (psignhash_nid) {
  3430. if (sign_nid == NID_undef || hash_nid == NID_undef
  3431. || OBJ_find_sigid_by_algs(psignhash_nid, hash_nid,
  3432. sign_nid) <= 0)
  3433. *psignhash_nid = NID_undef;
  3434. }
  3435. }
  3436. /* Given preference and allowed sigalgs set shared sigalgs */
  3437. static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig,
  3438. const unsigned char *pref, size_t preflen,
  3439. const unsigned char *allow,
  3440. size_t allowlen)
  3441. {
  3442. const unsigned char *ptmp, *atmp;
  3443. size_t i, j, nmatch = 0;
  3444. for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
  3445. /* Skip disabled hashes or signature algorithms */
  3446. if (tls12_get_hash(ptmp[0]) == NULL)
  3447. continue;
  3448. if (tls12_get_pkey_idx(ptmp[1]) == -1)
  3449. continue;
  3450. for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
  3451. if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
  3452. nmatch++;
  3453. if (shsig) {
  3454. shsig->rhash = ptmp[0];
  3455. shsig->rsign = ptmp[1];
  3456. tls1_lookup_sigalg(&shsig->hash_nid,
  3457. &shsig->sign_nid,
  3458. &shsig->signandhash_nid, ptmp);
  3459. shsig++;
  3460. }
  3461. break;
  3462. }
  3463. }
  3464. }
  3465. return nmatch;
  3466. }
  3467. /* Set shared signature algorithms for SSL structures */
  3468. static int tls1_set_shared_sigalgs(SSL *s)
  3469. {
  3470. const unsigned char *pref, *allow, *conf;
  3471. size_t preflen, allowlen, conflen;
  3472. size_t nmatch;
  3473. TLS_SIGALGS *salgs = NULL;
  3474. CERT *c = s->cert;
  3475. unsigned int is_suiteb = tls1_suiteb(s);
  3476. if (c->shared_sigalgs) {
  3477. OPENSSL_free(c->shared_sigalgs);
  3478. c->shared_sigalgs = NULL;
  3479. c->shared_sigalgslen = 0;
  3480. }
  3481. /* If client use client signature algorithms if not NULL */
  3482. if (!s->server && c->client_sigalgs && !is_suiteb) {
  3483. conf = c->client_sigalgs;
  3484. conflen = c->client_sigalgslen;
  3485. } else if (c->conf_sigalgs && !is_suiteb) {
  3486. conf = c->conf_sigalgs;
  3487. conflen = c->conf_sigalgslen;
  3488. } else
  3489. conflen = tls12_get_psigalgs(s, &conf);
  3490. if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
  3491. pref = conf;
  3492. preflen = conflen;
  3493. allow = c->peer_sigalgs;
  3494. allowlen = c->peer_sigalgslen;
  3495. } else {
  3496. allow = conf;
  3497. allowlen = conflen;
  3498. pref = c->peer_sigalgs;
  3499. preflen = c->peer_sigalgslen;
  3500. }
  3501. nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
  3502. if (nmatch) {
  3503. salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
  3504. if (!salgs)
  3505. return 0;
  3506. nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
  3507. } else {
  3508. salgs = NULL;
  3509. }
  3510. c->shared_sigalgs = salgs;
  3511. c->shared_sigalgslen = nmatch;
  3512. return 1;
  3513. }
  3514. /* Set preferred digest for each key type */
  3515. int tls1_save_sigalgs(SSL *s, const unsigned char *data, int dsize)
  3516. {
  3517. CERT *c = s->cert;
  3518. /* Extension ignored for inappropriate versions */
  3519. if (!SSL_USE_SIGALGS(s))
  3520. return 1;
  3521. /* Should never happen */
  3522. if (!c)
  3523. return 0;
  3524. if (c->peer_sigalgs)
  3525. OPENSSL_free(c->peer_sigalgs);
  3526. c->peer_sigalgs = OPENSSL_malloc(dsize);
  3527. if (!c->peer_sigalgs)
  3528. return 0;
  3529. c->peer_sigalgslen = dsize;
  3530. memcpy(c->peer_sigalgs, data, dsize);
  3531. return 1;
  3532. }
  3533. int tls1_process_sigalgs(SSL *s)
  3534. {
  3535. int idx;
  3536. size_t i;
  3537. const EVP_MD *md;
  3538. CERT *c = s->cert;
  3539. TLS_SIGALGS *sigptr;
  3540. if (!tls1_set_shared_sigalgs(s))
  3541. return 0;
  3542. # ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
  3543. if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL) {
  3544. /*
  3545. * Use first set signature preference to force message digest,
  3546. * ignoring any peer preferences.
  3547. */
  3548. const unsigned char *sigs = NULL;
  3549. if (s->server)
  3550. sigs = c->conf_sigalgs;
  3551. else
  3552. sigs = c->client_sigalgs;
  3553. if (sigs) {
  3554. idx = tls12_get_pkey_idx(sigs[1]);
  3555. md = tls12_get_hash(sigs[0]);
  3556. c->pkeys[idx].digest = md;
  3557. c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
  3558. if (idx == SSL_PKEY_RSA_SIGN) {
  3559. c->pkeys[SSL_PKEY_RSA_ENC].valid_flags =
  3560. CERT_PKEY_EXPLICIT_SIGN;
  3561. c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
  3562. }
  3563. }
  3564. }
  3565. # endif
  3566. for (i = 0, sigptr = c->shared_sigalgs;
  3567. i < c->shared_sigalgslen; i++, sigptr++) {
  3568. idx = tls12_get_pkey_idx(sigptr->rsign);
  3569. if (idx > 0 && c->pkeys[idx].digest == NULL) {
  3570. md = tls12_get_hash(sigptr->rhash);
  3571. c->pkeys[idx].digest = md;
  3572. c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
  3573. if (idx == SSL_PKEY_RSA_SIGN) {
  3574. c->pkeys[SSL_PKEY_RSA_ENC].valid_flags =
  3575. CERT_PKEY_EXPLICIT_SIGN;
  3576. c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
  3577. }
  3578. }
  3579. }
  3580. /*
  3581. * In strict mode leave unset digests as NULL to indicate we can't use
  3582. * the certificate for signing.
  3583. */
  3584. if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
  3585. /*
  3586. * Set any remaining keys to default values. NOTE: if alg is not
  3587. * supported it stays as NULL.
  3588. */
  3589. # ifndef OPENSSL_NO_DSA
  3590. if (!c->pkeys[SSL_PKEY_DSA_SIGN].digest)
  3591. c->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_sha1();
  3592. # endif
  3593. # ifndef OPENSSL_NO_RSA
  3594. if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest) {
  3595. c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
  3596. c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
  3597. }
  3598. # endif
  3599. # ifndef OPENSSL_NO_ECDSA
  3600. if (!c->pkeys[SSL_PKEY_ECC].digest)
  3601. c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
  3602. # endif
  3603. }
  3604. return 1;
  3605. }
  3606. int SSL_get_sigalgs(SSL *s, int idx,
  3607. int *psign, int *phash, int *psignhash,
  3608. unsigned char *rsig, unsigned char *rhash)
  3609. {
  3610. const unsigned char *psig = s->cert->peer_sigalgs;
  3611. if (psig == NULL)
  3612. return 0;
  3613. if (idx >= 0) {
  3614. idx <<= 1;
  3615. if (idx >= (int)s->cert->peer_sigalgslen)
  3616. return 0;
  3617. psig += idx;
  3618. if (rhash)
  3619. *rhash = psig[0];
  3620. if (rsig)
  3621. *rsig = psig[1];
  3622. tls1_lookup_sigalg(phash, psign, psignhash, psig);
  3623. }
  3624. return s->cert->peer_sigalgslen / 2;
  3625. }
  3626. int SSL_get_shared_sigalgs(SSL *s, int idx,
  3627. int *psign, int *phash, int *psignhash,
  3628. unsigned char *rsig, unsigned char *rhash)
  3629. {
  3630. TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
  3631. if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
  3632. return 0;
  3633. shsigalgs += idx;
  3634. if (phash)
  3635. *phash = shsigalgs->hash_nid;
  3636. if (psign)
  3637. *psign = shsigalgs->sign_nid;
  3638. if (psignhash)
  3639. *psignhash = shsigalgs->signandhash_nid;
  3640. if (rsig)
  3641. *rsig = shsigalgs->rsign;
  3642. if (rhash)
  3643. *rhash = shsigalgs->rhash;
  3644. return s->cert->shared_sigalgslen;
  3645. }
  3646. # ifndef OPENSSL_NO_HEARTBEATS
  3647. int tls1_process_heartbeat(SSL *s)
  3648. {
  3649. unsigned char *p = &s->s3->rrec.data[0], *pl;
  3650. unsigned short hbtype;
  3651. unsigned int payload;
  3652. unsigned int padding = 16; /* Use minimum padding */
  3653. if (s->msg_callback)
  3654. s->msg_callback(0, s->version, TLS1_RT_HEARTBEAT,
  3655. &s->s3->rrec.data[0], s->s3->rrec.length,
  3656. s, s->msg_callback_arg);
  3657. /* Read type and payload length first */
  3658. if (1 + 2 + 16 > s->s3->rrec.length)
  3659. return 0; /* silently discard */
  3660. hbtype = *p++;
  3661. n2s(p, payload);
  3662. if (1 + 2 + payload + 16 > s->s3->rrec.length)
  3663. return 0; /* silently discard per RFC 6520 sec. 4 */
  3664. pl = p;
  3665. if (hbtype == TLS1_HB_REQUEST) {
  3666. unsigned char *buffer, *bp;
  3667. int r;
  3668. /*
  3669. * Allocate memory for the response, size is 1 bytes message type,
  3670. * plus 2 bytes payload length, plus payload, plus padding
  3671. */
  3672. buffer = OPENSSL_malloc(1 + 2 + payload + padding);
  3673. if (buffer == NULL)
  3674. return -1;
  3675. bp = buffer;
  3676. /* Enter response type, length and copy payload */
  3677. *bp++ = TLS1_HB_RESPONSE;
  3678. s2n(payload, bp);
  3679. memcpy(bp, pl, payload);
  3680. bp += payload;
  3681. /* Random padding */
  3682. if (RAND_bytes(bp, padding) <= 0) {
  3683. OPENSSL_free(buffer);
  3684. return -1;
  3685. }
  3686. r = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buffer,
  3687. 3 + payload + padding);
  3688. if (r >= 0 && s->msg_callback)
  3689. s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
  3690. buffer, 3 + payload + padding,
  3691. s, s->msg_callback_arg);
  3692. OPENSSL_free(buffer);
  3693. if (r < 0)
  3694. return r;
  3695. } else if (hbtype == TLS1_HB_RESPONSE) {
  3696. unsigned int seq;
  3697. /*
  3698. * We only send sequence numbers (2 bytes unsigned int), and 16
  3699. * random bytes, so we just try to read the sequence number
  3700. */
  3701. n2s(pl, seq);
  3702. if (payload == 18 && seq == s->tlsext_hb_seq) {
  3703. s->tlsext_hb_seq++;
  3704. s->tlsext_hb_pending = 0;
  3705. }
  3706. }
  3707. return 0;
  3708. }
  3709. int tls1_heartbeat(SSL *s)
  3710. {
  3711. unsigned char *buf, *p;
  3712. int ret = -1;
  3713. unsigned int payload = 18; /* Sequence number + random bytes */
  3714. unsigned int padding = 16; /* Use minimum padding */
  3715. /* Only send if peer supports and accepts HB requests... */
  3716. if (!(s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) ||
  3717. s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_SEND_REQUESTS) {
  3718. SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_TLS_HEARTBEAT_PEER_DOESNT_ACCEPT);
  3719. return -1;
  3720. }
  3721. /* ...and there is none in flight yet... */
  3722. if (s->tlsext_hb_pending) {
  3723. SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_TLS_HEARTBEAT_PENDING);
  3724. return -1;
  3725. }
  3726. /* ...and no handshake in progress. */
  3727. if (SSL_in_init(s) || s->in_handshake) {
  3728. SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_UNEXPECTED_MESSAGE);
  3729. return -1;
  3730. }
  3731. /*
  3732. * Check if padding is too long, payload and padding must not exceed 2^14
  3733. * - 3 = 16381 bytes in total.
  3734. */
  3735. OPENSSL_assert(payload + padding <= 16381);
  3736. /*-
  3737. * Create HeartBeat message, we just use a sequence number
  3738. * as payload to distuingish different messages and add
  3739. * some random stuff.
  3740. * - Message Type, 1 byte
  3741. * - Payload Length, 2 bytes (unsigned int)
  3742. * - Payload, the sequence number (2 bytes uint)
  3743. * - Payload, random bytes (16 bytes uint)
  3744. * - Padding
  3745. */
  3746. buf = OPENSSL_malloc(1 + 2 + payload + padding);
  3747. if (buf == NULL)
  3748. return -1;
  3749. p = buf;
  3750. /* Message Type */
  3751. *p++ = TLS1_HB_REQUEST;
  3752. /* Payload length (18 bytes here) */
  3753. s2n(payload, p);
  3754. /* Sequence number */
  3755. s2n(s->tlsext_hb_seq, p);
  3756. /* 16 random bytes */
  3757. if (RAND_bytes(p, 16) <= 0) {
  3758. SSLerr(SSL_F_TLS1_HEARTBEAT, ERR_R_INTERNAL_ERROR);
  3759. goto err;
  3760. }
  3761. p += 16;
  3762. /* Random padding */
  3763. if (RAND_bytes(p, padding) <= 0) {
  3764. SSLerr(SSL_F_TLS1_HEARTBEAT, ERR_R_INTERNAL_ERROR);
  3765. goto err;
  3766. }
  3767. ret = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buf, 3 + payload + padding);
  3768. if (ret >= 0) {
  3769. if (s->msg_callback)
  3770. s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
  3771. buf, 3 + payload + padding,
  3772. s, s->msg_callback_arg);
  3773. s->tlsext_hb_pending = 1;
  3774. }
  3775. err:
  3776. OPENSSL_free(buf);
  3777. return ret;
  3778. }
  3779. # endif
  3780. # define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2)
  3781. typedef struct {
  3782. size_t sigalgcnt;
  3783. int sigalgs[MAX_SIGALGLEN];
  3784. } sig_cb_st;
  3785. static int sig_cb(const char *elem, int len, void *arg)
  3786. {
  3787. sig_cb_st *sarg = arg;
  3788. size_t i;
  3789. char etmp[20], *p;
  3790. int sig_alg, hash_alg;
  3791. if (elem == NULL)
  3792. return 0;
  3793. if (sarg->sigalgcnt == MAX_SIGALGLEN)
  3794. return 0;
  3795. if (len > (int)(sizeof(etmp) - 1))
  3796. return 0;
  3797. memcpy(etmp, elem, len);
  3798. etmp[len] = 0;
  3799. p = strchr(etmp, '+');
  3800. if (!p)
  3801. return 0;
  3802. *p = 0;
  3803. p++;
  3804. if (!*p)
  3805. return 0;
  3806. if (!strcmp(etmp, "RSA"))
  3807. sig_alg = EVP_PKEY_RSA;
  3808. else if (!strcmp(etmp, "DSA"))
  3809. sig_alg = EVP_PKEY_DSA;
  3810. else if (!strcmp(etmp, "ECDSA"))
  3811. sig_alg = EVP_PKEY_EC;
  3812. else
  3813. return 0;
  3814. hash_alg = OBJ_sn2nid(p);
  3815. if (hash_alg == NID_undef)
  3816. hash_alg = OBJ_ln2nid(p);
  3817. if (hash_alg == NID_undef)
  3818. return 0;
  3819. for (i = 0; i < sarg->sigalgcnt; i += 2) {
  3820. if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg)
  3821. return 0;
  3822. }
  3823. sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
  3824. sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
  3825. return 1;
  3826. }
  3827. /*
  3828. * Set suppored signature algorithms based on a colon separated list of the
  3829. * form sig+hash e.g. RSA+SHA512:DSA+SHA512
  3830. */
  3831. int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
  3832. {
  3833. sig_cb_st sig;
  3834. sig.sigalgcnt = 0;
  3835. if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
  3836. return 0;
  3837. if (c == NULL)
  3838. return 1;
  3839. return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
  3840. }
  3841. int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
  3842. int client)
  3843. {
  3844. unsigned char *sigalgs, *sptr;
  3845. int rhash, rsign;
  3846. size_t i;
  3847. if (salglen & 1)
  3848. return 0;
  3849. sigalgs = OPENSSL_malloc(salglen);
  3850. if (sigalgs == NULL)
  3851. return 0;
  3852. for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
  3853. rhash = tls12_find_id(*psig_nids++, tls12_md,
  3854. sizeof(tls12_md) / sizeof(tls12_lookup));
  3855. rsign = tls12_find_id(*psig_nids++, tls12_sig,
  3856. sizeof(tls12_sig) / sizeof(tls12_lookup));
  3857. if (rhash == -1 || rsign == -1)
  3858. goto err;
  3859. *sptr++ = rhash;
  3860. *sptr++ = rsign;
  3861. }
  3862. if (client) {
  3863. if (c->client_sigalgs)
  3864. OPENSSL_free(c->client_sigalgs);
  3865. c->client_sigalgs = sigalgs;
  3866. c->client_sigalgslen = salglen;
  3867. } else {
  3868. if (c->conf_sigalgs)
  3869. OPENSSL_free(c->conf_sigalgs);
  3870. c->conf_sigalgs = sigalgs;
  3871. c->conf_sigalgslen = salglen;
  3872. }
  3873. return 1;
  3874. err:
  3875. OPENSSL_free(sigalgs);
  3876. return 0;
  3877. }
  3878. static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
  3879. {
  3880. int sig_nid;
  3881. size_t i;
  3882. if (default_nid == -1)
  3883. return 1;
  3884. sig_nid = X509_get_signature_nid(x);
  3885. if (default_nid)
  3886. return sig_nid == default_nid ? 1 : 0;
  3887. for (i = 0; i < c->shared_sigalgslen; i++)
  3888. if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
  3889. return 1;
  3890. return 0;
  3891. }
  3892. /* Check to see if a certificate issuer name matches list of CA names */
  3893. static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
  3894. {
  3895. X509_NAME *nm;
  3896. int i;
  3897. nm = X509_get_issuer_name(x);
  3898. for (i = 0; i < sk_X509_NAME_num(names); i++) {
  3899. if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
  3900. return 1;
  3901. }
  3902. return 0;
  3903. }
  3904. /*
  3905. * Check certificate chain is consistent with TLS extensions and is usable by
  3906. * server. This servers two purposes: it allows users to check chains before
  3907. * passing them to the server and it allows the server to check chains before
  3908. * attempting to use them.
  3909. */
  3910. /* Flags which need to be set for a certificate when stict mode not set */
  3911. # define CERT_PKEY_VALID_FLAGS \
  3912. (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
  3913. /* Strict mode flags */
  3914. # define CERT_PKEY_STRICT_FLAGS \
  3915. (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
  3916. | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
  3917. int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
  3918. int idx)
  3919. {
  3920. int i;
  3921. int rv = 0;
  3922. int check_flags = 0, strict_mode;
  3923. CERT_PKEY *cpk = NULL;
  3924. CERT *c = s->cert;
  3925. unsigned int suiteb_flags = tls1_suiteb(s);
  3926. /* idx == -1 means checking server chains */
  3927. if (idx != -1) {
  3928. /* idx == -2 means checking client certificate chains */
  3929. if (idx == -2) {
  3930. cpk = c->key;
  3931. idx = cpk - c->pkeys;
  3932. } else
  3933. cpk = c->pkeys + idx;
  3934. x = cpk->x509;
  3935. pk = cpk->privatekey;
  3936. chain = cpk->chain;
  3937. strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
  3938. /* If no cert or key, forget it */
  3939. if (!x || !pk)
  3940. goto end;
  3941. # ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
  3942. /* Allow any certificate to pass test */
  3943. if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL) {
  3944. rv = CERT_PKEY_STRICT_FLAGS | CERT_PKEY_EXPLICIT_SIGN |
  3945. CERT_PKEY_VALID | CERT_PKEY_SIGN;
  3946. cpk->valid_flags = rv;
  3947. return rv;
  3948. }
  3949. # endif
  3950. } else {
  3951. if (!x || !pk)
  3952. return 0;
  3953. idx = ssl_cert_type(x, pk);
  3954. if (idx == -1)
  3955. return 0;
  3956. cpk = c->pkeys + idx;
  3957. if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
  3958. check_flags = CERT_PKEY_STRICT_FLAGS;
  3959. else
  3960. check_flags = CERT_PKEY_VALID_FLAGS;
  3961. strict_mode = 1;
  3962. }
  3963. if (suiteb_flags) {
  3964. int ok;
  3965. if (check_flags)
  3966. check_flags |= CERT_PKEY_SUITEB;
  3967. ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
  3968. if (ok == X509_V_OK)
  3969. rv |= CERT_PKEY_SUITEB;
  3970. else if (!check_flags)
  3971. goto end;
  3972. }
  3973. /*
  3974. * Check all signature algorithms are consistent with signature
  3975. * algorithms extension if TLS 1.2 or later and strict mode.
  3976. */
  3977. if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
  3978. int default_nid;
  3979. unsigned char rsign = 0;
  3980. if (c->peer_sigalgs)
  3981. default_nid = 0;
  3982. /* If no sigalgs extension use defaults from RFC5246 */
  3983. else {
  3984. switch (idx) {
  3985. case SSL_PKEY_RSA_ENC:
  3986. case SSL_PKEY_RSA_SIGN:
  3987. case SSL_PKEY_DH_RSA:
  3988. rsign = TLSEXT_signature_rsa;
  3989. default_nid = NID_sha1WithRSAEncryption;
  3990. break;
  3991. case SSL_PKEY_DSA_SIGN:
  3992. case SSL_PKEY_DH_DSA:
  3993. rsign = TLSEXT_signature_dsa;
  3994. default_nid = NID_dsaWithSHA1;
  3995. break;
  3996. case SSL_PKEY_ECC:
  3997. rsign = TLSEXT_signature_ecdsa;
  3998. default_nid = NID_ecdsa_with_SHA1;
  3999. break;
  4000. default:
  4001. default_nid = -1;
  4002. break;
  4003. }
  4004. }
  4005. /*
  4006. * If peer sent no signature algorithms extension and we have set
  4007. * preferred signature algorithms check we support sha1.
  4008. */
  4009. if (default_nid > 0 && c->conf_sigalgs) {
  4010. size_t j;
  4011. const unsigned char *p = c->conf_sigalgs;
  4012. for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2) {
  4013. if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
  4014. break;
  4015. }
  4016. if (j == c->conf_sigalgslen) {
  4017. if (check_flags)
  4018. goto skip_sigs;
  4019. else
  4020. goto end;
  4021. }
  4022. }
  4023. /* Check signature algorithm of each cert in chain */
  4024. if (!tls1_check_sig_alg(c, x, default_nid)) {
  4025. if (!check_flags)
  4026. goto end;
  4027. } else
  4028. rv |= CERT_PKEY_EE_SIGNATURE;
  4029. rv |= CERT_PKEY_CA_SIGNATURE;
  4030. for (i = 0; i < sk_X509_num(chain); i++) {
  4031. if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) {
  4032. if (check_flags) {
  4033. rv &= ~CERT_PKEY_CA_SIGNATURE;
  4034. break;
  4035. } else
  4036. goto end;
  4037. }
  4038. }
  4039. }
  4040. /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
  4041. else if (check_flags)
  4042. rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
  4043. skip_sigs:
  4044. /* Check cert parameters are consistent */
  4045. if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
  4046. rv |= CERT_PKEY_EE_PARAM;
  4047. else if (!check_flags)
  4048. goto end;
  4049. if (!s->server)
  4050. rv |= CERT_PKEY_CA_PARAM;
  4051. /* In strict mode check rest of chain too */
  4052. else if (strict_mode) {
  4053. rv |= CERT_PKEY_CA_PARAM;
  4054. for (i = 0; i < sk_X509_num(chain); i++) {
  4055. X509 *ca = sk_X509_value(chain, i);
  4056. if (!tls1_check_cert_param(s, ca, 0)) {
  4057. if (check_flags) {
  4058. rv &= ~CERT_PKEY_CA_PARAM;
  4059. break;
  4060. } else
  4061. goto end;
  4062. }
  4063. }
  4064. }
  4065. if (!s->server && strict_mode) {
  4066. STACK_OF(X509_NAME) *ca_dn;
  4067. int check_type = 0;
  4068. switch (pk->type) {
  4069. case EVP_PKEY_RSA:
  4070. check_type = TLS_CT_RSA_SIGN;
  4071. break;
  4072. case EVP_PKEY_DSA:
  4073. check_type = TLS_CT_DSS_SIGN;
  4074. break;
  4075. case EVP_PKEY_EC:
  4076. check_type = TLS_CT_ECDSA_SIGN;
  4077. break;
  4078. case EVP_PKEY_DH:
  4079. case EVP_PKEY_DHX:
  4080. {
  4081. int cert_type = X509_certificate_type(x, pk);
  4082. if (cert_type & EVP_PKS_RSA)
  4083. check_type = TLS_CT_RSA_FIXED_DH;
  4084. if (cert_type & EVP_PKS_DSA)
  4085. check_type = TLS_CT_DSS_FIXED_DH;
  4086. }
  4087. }
  4088. if (check_type) {
  4089. const unsigned char *ctypes;
  4090. int ctypelen;
  4091. if (c->ctypes) {
  4092. ctypes = c->ctypes;
  4093. ctypelen = (int)c->ctype_num;
  4094. } else {
  4095. ctypes = (unsigned char *)s->s3->tmp.ctype;
  4096. ctypelen = s->s3->tmp.ctype_num;
  4097. }
  4098. for (i = 0; i < ctypelen; i++) {
  4099. if (ctypes[i] == check_type) {
  4100. rv |= CERT_PKEY_CERT_TYPE;
  4101. break;
  4102. }
  4103. }
  4104. if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
  4105. goto end;
  4106. } else
  4107. rv |= CERT_PKEY_CERT_TYPE;
  4108. ca_dn = s->s3->tmp.ca_names;
  4109. if (!sk_X509_NAME_num(ca_dn))
  4110. rv |= CERT_PKEY_ISSUER_NAME;
  4111. if (!(rv & CERT_PKEY_ISSUER_NAME)) {
  4112. if (ssl_check_ca_name(ca_dn, x))
  4113. rv |= CERT_PKEY_ISSUER_NAME;
  4114. }
  4115. if (!(rv & CERT_PKEY_ISSUER_NAME)) {
  4116. for (i = 0; i < sk_X509_num(chain); i++) {
  4117. X509 *xtmp = sk_X509_value(chain, i);
  4118. if (ssl_check_ca_name(ca_dn, xtmp)) {
  4119. rv |= CERT_PKEY_ISSUER_NAME;
  4120. break;
  4121. }
  4122. }
  4123. }
  4124. if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
  4125. goto end;
  4126. } else
  4127. rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
  4128. if (!check_flags || (rv & check_flags) == check_flags)
  4129. rv |= CERT_PKEY_VALID;
  4130. end:
  4131. if (TLS1_get_version(s) >= TLS1_2_VERSION) {
  4132. if (cpk->valid_flags & CERT_PKEY_EXPLICIT_SIGN)
  4133. rv |= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
  4134. else if (cpk->digest)
  4135. rv |= CERT_PKEY_SIGN;
  4136. } else
  4137. rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
  4138. /*
  4139. * When checking a CERT_PKEY structure all flags are irrelevant if the
  4140. * chain is invalid.
  4141. */
  4142. if (!check_flags) {
  4143. if (rv & CERT_PKEY_VALID)
  4144. cpk->valid_flags = rv;
  4145. else {
  4146. /* Preserve explicit sign flag, clear rest */
  4147. cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
  4148. return 0;
  4149. }
  4150. }
  4151. return rv;
  4152. }
  4153. /* Set validity of certificates in an SSL structure */
  4154. void tls1_set_cert_validity(SSL *s)
  4155. {
  4156. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
  4157. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
  4158. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
  4159. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DH_RSA);
  4160. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DH_DSA);
  4161. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
  4162. }
  4163. /* User level utiity function to check a chain is suitable */
  4164. int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
  4165. {
  4166. return tls1_check_chain(s, x, pk, chain, -1);
  4167. }
  4168. #endif