t1_lib.c 146 KB

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