t1_lib.c 140 KB

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