t1_lib.c 142 KB

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