t1_lib.c 147 KB

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