t1_lib.cc 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com). */
  108. #include <openssl/ssl.h>
  109. #include <assert.h>
  110. #include <limits.h>
  111. #include <stdlib.h>
  112. #include <string.h>
  113. #include <utility>
  114. #include <openssl/bytestring.h>
  115. #include <openssl/digest.h>
  116. #include <openssl/err.h>
  117. #include <openssl/evp.h>
  118. #include <openssl/hmac.h>
  119. #include <openssl/mem.h>
  120. #include <openssl/nid.h>
  121. #include <openssl/rand.h>
  122. #include "internal.h"
  123. #include "../crypto/internal.h"
  124. namespace bssl {
  125. static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs);
  126. static int compare_uint16_t(const void *p1, const void *p2) {
  127. uint16_t u1 = *((const uint16_t *)p1);
  128. uint16_t u2 = *((const uint16_t *)p2);
  129. if (u1 < u2) {
  130. return -1;
  131. } else if (u1 > u2) {
  132. return 1;
  133. } else {
  134. return 0;
  135. }
  136. }
  137. // Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
  138. // more than one extension of the same type in a ClientHello or ServerHello.
  139. // This function does an initial scan over the extensions block to filter those
  140. // out.
  141. static int tls1_check_duplicate_extensions(const CBS *cbs) {
  142. // First pass: count the extensions.
  143. size_t num_extensions = 0;
  144. CBS extensions = *cbs;
  145. while (CBS_len(&extensions) > 0) {
  146. uint16_t type;
  147. CBS extension;
  148. if (!CBS_get_u16(&extensions, &type) ||
  149. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  150. return 0;
  151. }
  152. num_extensions++;
  153. }
  154. if (num_extensions == 0) {
  155. return 1;
  156. }
  157. Array<uint16_t> extension_types;
  158. if (!extension_types.Init(num_extensions)) {
  159. return 0;
  160. }
  161. // Second pass: gather the extension types.
  162. extensions = *cbs;
  163. for (size_t i = 0; i < extension_types.size(); i++) {
  164. CBS extension;
  165. if (!CBS_get_u16(&extensions, &extension_types[i]) ||
  166. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  167. // This should not happen.
  168. return 0;
  169. }
  170. }
  171. assert(CBS_len(&extensions) == 0);
  172. // Sort the extensions and make sure there are no duplicates.
  173. qsort(extension_types.data(), extension_types.size(), sizeof(uint16_t),
  174. compare_uint16_t);
  175. for (size_t i = 1; i < num_extensions; i++) {
  176. if (extension_types[i - 1] == extension_types[i]) {
  177. return 0;
  178. }
  179. }
  180. return 1;
  181. }
  182. int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out,
  183. const SSLMessage &msg) {
  184. OPENSSL_memset(out, 0, sizeof(*out));
  185. out->ssl = ssl;
  186. out->client_hello = CBS_data(&msg.body);
  187. out->client_hello_len = CBS_len(&msg.body);
  188. CBS client_hello, random, session_id;
  189. CBS_init(&client_hello, out->client_hello, out->client_hello_len);
  190. if (!CBS_get_u16(&client_hello, &out->version) ||
  191. !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
  192. !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
  193. CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  194. return 0;
  195. }
  196. out->random = CBS_data(&random);
  197. out->random_len = CBS_len(&random);
  198. out->session_id = CBS_data(&session_id);
  199. out->session_id_len = CBS_len(&session_id);
  200. // Skip past DTLS cookie
  201. if (SSL_is_dtls(out->ssl)) {
  202. CBS cookie;
  203. if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
  204. CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
  205. return 0;
  206. }
  207. }
  208. CBS cipher_suites, compression_methods;
  209. if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
  210. CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
  211. !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
  212. CBS_len(&compression_methods) < 1) {
  213. return 0;
  214. }
  215. out->cipher_suites = CBS_data(&cipher_suites);
  216. out->cipher_suites_len = CBS_len(&cipher_suites);
  217. out->compression_methods = CBS_data(&compression_methods);
  218. out->compression_methods_len = CBS_len(&compression_methods);
  219. // If the ClientHello ends here then it's valid, but doesn't have any
  220. // extensions. (E.g. SSLv3.)
  221. if (CBS_len(&client_hello) == 0) {
  222. out->extensions = NULL;
  223. out->extensions_len = 0;
  224. return 1;
  225. }
  226. // Extract extensions and check it is valid.
  227. CBS extensions;
  228. if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
  229. !tls1_check_duplicate_extensions(&extensions) ||
  230. CBS_len(&client_hello) != 0) {
  231. return 0;
  232. }
  233. out->extensions = CBS_data(&extensions);
  234. out->extensions_len = CBS_len(&extensions);
  235. return 1;
  236. }
  237. int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
  238. CBS *out, uint16_t extension_type) {
  239. CBS extensions;
  240. CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
  241. while (CBS_len(&extensions) != 0) {
  242. // Decode the next extension.
  243. uint16_t type;
  244. CBS extension;
  245. if (!CBS_get_u16(&extensions, &type) ||
  246. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  247. return 0;
  248. }
  249. if (type == extension_type) {
  250. *out = extension;
  251. return 1;
  252. }
  253. }
  254. return 0;
  255. }
  256. static const uint16_t kDefaultGroups[] = {
  257. SSL_CURVE_X25519,
  258. SSL_CURVE_SECP256R1,
  259. SSL_CURVE_SECP384R1,
  260. };
  261. Span<const uint16_t> tls1_get_grouplist(const SSL *ssl) {
  262. if (ssl->supported_group_list != nullptr) {
  263. return MakeConstSpan(ssl->supported_group_list,
  264. ssl->supported_group_list_len);
  265. }
  266. return Span<const uint16_t>(kDefaultGroups);
  267. }
  268. int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
  269. SSL *const ssl = hs->ssl;
  270. assert(ssl->server);
  271. // Clients are not required to send a supported_groups extension. In this
  272. // case, the server is free to pick any group it likes. See RFC 4492,
  273. // section 4, paragraph 3.
  274. //
  275. // However, in the interests of compatibility, we will skip ECDH if the
  276. // client didn't send an extension because we can't be sure that they'll
  277. // support our favoured group. Thus we do not special-case an emtpy
  278. // |peer_supported_group_list|.
  279. Span<const uint16_t> groups = tls1_get_grouplist(ssl);
  280. Span<const uint16_t> pref, supp;
  281. if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
  282. pref = groups;
  283. supp = hs->peer_supported_group_list;
  284. } else {
  285. pref = hs->peer_supported_group_list;
  286. supp = groups;
  287. }
  288. for (uint16_t pref_group : pref) {
  289. for (uint16_t supp_group : supp) {
  290. if (pref_group == supp_group) {
  291. *out_group_id = pref_group;
  292. return 1;
  293. }
  294. }
  295. }
  296. return 0;
  297. }
  298. int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
  299. const int *curves, size_t ncurves) {
  300. uint16_t *group_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
  301. if (group_ids == NULL) {
  302. return 0;
  303. }
  304. for (size_t i = 0; i < ncurves; i++) {
  305. if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
  306. OPENSSL_free(group_ids);
  307. return 0;
  308. }
  309. }
  310. OPENSSL_free(*out_group_ids);
  311. *out_group_ids = group_ids;
  312. *out_group_ids_len = ncurves;
  313. return 1;
  314. }
  315. int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
  316. const char *curves) {
  317. uint16_t *group_ids = NULL;
  318. size_t ncurves = 0;
  319. const char *col;
  320. const char *ptr = curves;
  321. do {
  322. col = strchr(ptr, ':');
  323. uint16_t group_id;
  324. if (!ssl_name_to_group_id(&group_id, ptr,
  325. col ? (size_t)(col - ptr) : strlen(ptr))) {
  326. goto err;
  327. }
  328. uint16_t *new_group_ids = (uint16_t *)OPENSSL_realloc(
  329. group_ids, (ncurves + 1) * sizeof(uint16_t));
  330. if (new_group_ids == NULL) {
  331. goto err;
  332. }
  333. group_ids = new_group_ids;
  334. group_ids[ncurves] = group_id;
  335. ncurves++;
  336. if (col) {
  337. ptr = col + 1;
  338. }
  339. } while (col);
  340. OPENSSL_free(*out_group_ids);
  341. *out_group_ids = group_ids;
  342. *out_group_ids_len = ncurves;
  343. return 1;
  344. err:
  345. OPENSSL_free(group_ids);
  346. return 0;
  347. }
  348. int tls1_check_group_id(const SSL *ssl, uint16_t group_id) {
  349. for (uint16_t supported : tls1_get_grouplist(ssl)) {
  350. if (supported == group_id) {
  351. return 1;
  352. }
  353. }
  354. return 0;
  355. }
  356. // kVerifySignatureAlgorithms is the default list of accepted signature
  357. // algorithms for verifying.
  358. //
  359. // For now, RSA-PSS signature algorithms are not enabled on Android's system
  360. // BoringSSL. Once the change in Chrome has stuck and the values are finalized,
  361. // restore them.
  362. static const uint16_t kVerifySignatureAlgorithms[] = {
  363. // List our preferred algorithms first.
  364. SSL_SIGN_ED25519,
  365. SSL_SIGN_ECDSA_SECP256R1_SHA256,
  366. SSL_SIGN_RSA_PSS_SHA256,
  367. SSL_SIGN_RSA_PKCS1_SHA256,
  368. // Larger hashes are acceptable.
  369. SSL_SIGN_ECDSA_SECP384R1_SHA384,
  370. SSL_SIGN_RSA_PSS_SHA384,
  371. SSL_SIGN_RSA_PKCS1_SHA384,
  372. SSL_SIGN_RSA_PSS_SHA512,
  373. SSL_SIGN_RSA_PKCS1_SHA512,
  374. // For now, SHA-1 is still accepted but least preferable.
  375. SSL_SIGN_RSA_PKCS1_SHA1,
  376. };
  377. // kSignSignatureAlgorithms is the default list of supported signature
  378. // algorithms for signing.
  379. //
  380. // For now, RSA-PSS signature algorithms are not enabled on Android's system
  381. // BoringSSL. Once the change in Chrome has stuck and the values are finalized,
  382. // restore them.
  383. static const uint16_t kSignSignatureAlgorithms[] = {
  384. // List our preferred algorithms first.
  385. SSL_SIGN_ED25519,
  386. SSL_SIGN_ECDSA_SECP256R1_SHA256,
  387. SSL_SIGN_RSA_PSS_SHA256,
  388. SSL_SIGN_RSA_PKCS1_SHA256,
  389. // If needed, sign larger hashes.
  390. //
  391. // TODO(davidben): Determine which of these may be pruned.
  392. SSL_SIGN_ECDSA_SECP384R1_SHA384,
  393. SSL_SIGN_RSA_PSS_SHA384,
  394. SSL_SIGN_RSA_PKCS1_SHA384,
  395. SSL_SIGN_ECDSA_SECP521R1_SHA512,
  396. SSL_SIGN_RSA_PSS_SHA512,
  397. SSL_SIGN_RSA_PKCS1_SHA512,
  398. // If the peer supports nothing else, sign with SHA-1.
  399. SSL_SIGN_ECDSA_SHA1,
  400. SSL_SIGN_RSA_PKCS1_SHA1,
  401. };
  402. int tls12_add_verify_sigalgs(const SSL *ssl, CBB *out) {
  403. const uint16_t *sigalgs = kVerifySignatureAlgorithms;
  404. size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
  405. if (ssl->ctx->num_verify_sigalgs != 0) {
  406. sigalgs = ssl->ctx->verify_sigalgs;
  407. num_sigalgs = ssl->ctx->num_verify_sigalgs;
  408. }
  409. for (size_t i = 0; i < num_sigalgs; i++) {
  410. if (sigalgs == kVerifySignatureAlgorithms &&
  411. sigalgs[i] == SSL_SIGN_ED25519 &&
  412. !ssl->ctx->ed25519_enabled) {
  413. continue;
  414. }
  415. if (!CBB_add_u16(out, sigalgs[i])) {
  416. return 0;
  417. }
  418. }
  419. return 1;
  420. }
  421. int tls12_check_peer_sigalg(SSL *ssl, uint8_t *out_alert, uint16_t sigalg) {
  422. const uint16_t *sigalgs = kVerifySignatureAlgorithms;
  423. size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
  424. if (ssl->ctx->num_verify_sigalgs != 0) {
  425. sigalgs = ssl->ctx->verify_sigalgs;
  426. num_sigalgs = ssl->ctx->num_verify_sigalgs;
  427. }
  428. for (size_t i = 0; i < num_sigalgs; i++) {
  429. if (sigalgs == kVerifySignatureAlgorithms &&
  430. sigalgs[i] == SSL_SIGN_ED25519 &&
  431. !ssl->ctx->ed25519_enabled) {
  432. continue;
  433. }
  434. if (sigalg == sigalgs[i]) {
  435. return 1;
  436. }
  437. }
  438. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
  439. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  440. return 0;
  441. }
  442. // tls_extension represents a TLS extension that is handled internally. The
  443. // |init| function is called for each handshake, before any other functions of
  444. // the extension. Then the add and parse callbacks are called as needed.
  445. //
  446. // The parse callbacks receive a |CBS| that contains the contents of the
  447. // extension (i.e. not including the type and length bytes). If an extension is
  448. // not received then the parse callbacks will be called with a NULL CBS so that
  449. // they can do any processing needed to handle the absence of an extension.
  450. //
  451. // The add callbacks receive a |CBB| to which the extension can be appended but
  452. // the function is responsible for appending the type and length bytes too.
  453. //
  454. // All callbacks return true for success and false for error. If a parse
  455. // function returns zero then a fatal alert with value |*out_alert| will be
  456. // sent. If |*out_alert| isn't set, then a |decode_error| alert will be sent.
  457. struct tls_extension {
  458. uint16_t value;
  459. void (*init)(SSL_HANDSHAKE *hs);
  460. bool (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
  461. bool (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  462. CBS *contents);
  463. bool (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  464. CBS *contents);
  465. bool (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
  466. };
  467. static bool forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  468. CBS *contents) {
  469. if (contents != NULL) {
  470. // Servers MUST NOT send this extension.
  471. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  472. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
  473. return false;
  474. }
  475. return true;
  476. }
  477. static bool ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  478. CBS *contents) {
  479. // This extension from the client is handled elsewhere.
  480. return true;
  481. }
  482. static bool dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  483. return true;
  484. }
  485. // Server name indication (SNI).
  486. //
  487. // https://tools.ietf.org/html/rfc6066#section-3.
  488. static bool ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  489. SSL *const ssl = hs->ssl;
  490. if (ssl->tlsext_hostname == NULL) {
  491. return true;
  492. }
  493. CBB contents, server_name_list, name;
  494. if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
  495. !CBB_add_u16_length_prefixed(out, &contents) ||
  496. !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
  497. !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
  498. !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
  499. !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
  500. strlen(ssl->tlsext_hostname)) ||
  501. !CBB_flush(out)) {
  502. return false;
  503. }
  504. return true;
  505. }
  506. static bool ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  507. CBS *contents) {
  508. // The server may acknowledge SNI with an empty extension. We check the syntax
  509. // but otherwise ignore this signal.
  510. return contents == NULL || CBS_len(contents) == 0;
  511. }
  512. static bool ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  513. CBS *contents) {
  514. SSL *const ssl = hs->ssl;
  515. if (contents == NULL) {
  516. return true;
  517. }
  518. CBS server_name_list, host_name;
  519. uint8_t name_type;
  520. if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
  521. !CBS_get_u8(&server_name_list, &name_type) ||
  522. // Although the server_name extension was intended to be extensible to
  523. // new name types and multiple names, OpenSSL 1.0.x had a bug which meant
  524. // different name types will cause an error. Further, RFC 4366 originally
  525. // defined syntax inextensibly. RFC 6066 corrected this mistake, but
  526. // adding new name types is no longer feasible.
  527. //
  528. // Act as if the extensibility does not exist to simplify parsing.
  529. !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
  530. CBS_len(&server_name_list) != 0 ||
  531. CBS_len(contents) != 0) {
  532. return false;
  533. }
  534. if (name_type != TLSEXT_NAMETYPE_host_name ||
  535. CBS_len(&host_name) == 0 ||
  536. CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
  537. CBS_contains_zero_byte(&host_name)) {
  538. *out_alert = SSL_AD_UNRECOGNIZED_NAME;
  539. return false;
  540. }
  541. // Copy the hostname as a string.
  542. char *raw = nullptr;
  543. if (!CBS_strdup(&host_name, &raw)) {
  544. *out_alert = SSL_AD_INTERNAL_ERROR;
  545. return false;
  546. }
  547. ssl->s3->hostname.reset(raw);
  548. hs->should_ack_sni = true;
  549. return true;
  550. }
  551. static bool ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  552. if (hs->ssl->s3->session_reused ||
  553. !hs->should_ack_sni) {
  554. return true;
  555. }
  556. if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
  557. !CBB_add_u16(out, 0 /* length */)) {
  558. return false;
  559. }
  560. return true;
  561. }
  562. // Renegotiation indication.
  563. //
  564. // https://tools.ietf.org/html/rfc5746
  565. static bool ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  566. SSL *const ssl = hs->ssl;
  567. // Renegotiation indication is not necessary in TLS 1.3.
  568. if (hs->min_version >= TLS1_3_VERSION) {
  569. return true;
  570. }
  571. assert(ssl->s3->initial_handshake_complete ==
  572. (ssl->s3->previous_client_finished_len != 0));
  573. CBB contents, prev_finished;
  574. if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
  575. !CBB_add_u16_length_prefixed(out, &contents) ||
  576. !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
  577. !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
  578. ssl->s3->previous_client_finished_len) ||
  579. !CBB_flush(out)) {
  580. return false;
  581. }
  582. return true;
  583. }
  584. static bool ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  585. CBS *contents) {
  586. SSL *const ssl = hs->ssl;
  587. if (contents != NULL && ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  588. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  589. return false;
  590. }
  591. // Servers may not switch between omitting the extension and supporting it.
  592. // See RFC 5746, sections 3.5 and 4.2.
  593. if (ssl->s3->initial_handshake_complete &&
  594. (contents != NULL) != ssl->s3->send_connection_binding) {
  595. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  596. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  597. return false;
  598. }
  599. if (contents == NULL) {
  600. // Strictly speaking, if we want to avoid an attack we should *always* see
  601. // RI even on initial ServerHello because the client doesn't see any
  602. // renegotiation during an attack. However this would mean we could not
  603. // connect to any server which doesn't support RI.
  604. //
  605. // OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
  606. // practical terms every client sets it so it's just assumed here.
  607. return true;
  608. }
  609. const size_t expected_len = ssl->s3->previous_client_finished_len +
  610. ssl->s3->previous_server_finished_len;
  611. // Check for logic errors
  612. assert(!expected_len || ssl->s3->previous_client_finished_len);
  613. assert(!expected_len || ssl->s3->previous_server_finished_len);
  614. assert(ssl->s3->initial_handshake_complete ==
  615. (ssl->s3->previous_client_finished_len != 0));
  616. assert(ssl->s3->initial_handshake_complete ==
  617. (ssl->s3->previous_server_finished_len != 0));
  618. // Parse out the extension contents.
  619. CBS renegotiated_connection;
  620. if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
  621. CBS_len(contents) != 0) {
  622. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
  623. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  624. return false;
  625. }
  626. // Check that the extension matches.
  627. if (CBS_len(&renegotiated_connection) != expected_len) {
  628. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  629. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  630. return false;
  631. }
  632. const uint8_t *d = CBS_data(&renegotiated_connection);
  633. bool ok = CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
  634. ssl->s3->previous_client_finished_len) == 0;
  635. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  636. ok = true;
  637. #endif
  638. if (!ok) {
  639. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  640. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  641. return false;
  642. }
  643. d += ssl->s3->previous_client_finished_len;
  644. ok = CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
  645. ssl->s3->previous_server_finished_len) == 0;
  646. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  647. ok = true;
  648. #endif
  649. if (!ok) {
  650. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  651. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  652. return false;
  653. }
  654. ssl->s3->send_connection_binding = true;
  655. return true;
  656. }
  657. static bool ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  658. CBS *contents) {
  659. SSL *const ssl = hs->ssl;
  660. // Renegotiation isn't supported as a server so this function should never be
  661. // called after the initial handshake.
  662. assert(!ssl->s3->initial_handshake_complete);
  663. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  664. return true;
  665. }
  666. if (contents == NULL) {
  667. return true;
  668. }
  669. CBS renegotiated_connection;
  670. if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
  671. CBS_len(contents) != 0) {
  672. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
  673. return false;
  674. }
  675. // Check that the extension matches. We do not support renegotiation as a
  676. // server, so this must be empty.
  677. if (CBS_len(&renegotiated_connection) != 0) {
  678. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  679. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  680. return false;
  681. }
  682. ssl->s3->send_connection_binding = true;
  683. return true;
  684. }
  685. static bool ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  686. SSL *const ssl = hs->ssl;
  687. // Renegotiation isn't supported as a server so this function should never be
  688. // called after the initial handshake.
  689. assert(!ssl->s3->initial_handshake_complete);
  690. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  691. return true;
  692. }
  693. if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
  694. !CBB_add_u16(out, 1 /* length */) ||
  695. !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
  696. return false;
  697. }
  698. return true;
  699. }
  700. // Extended Master Secret.
  701. //
  702. // https://tools.ietf.org/html/rfc7627
  703. static bool ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  704. // Extended master secret is not necessary in TLS 1.3.
  705. if (hs->min_version >= TLS1_3_VERSION || hs->max_version <= SSL3_VERSION) {
  706. return true;
  707. }
  708. if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
  709. !CBB_add_u16(out, 0 /* length */)) {
  710. return false;
  711. }
  712. return true;
  713. }
  714. static bool ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  715. CBS *contents) {
  716. SSL *const ssl = hs->ssl;
  717. if (contents != NULL) {
  718. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
  719. ssl->version == SSL3_VERSION ||
  720. CBS_len(contents) != 0) {
  721. return false;
  722. }
  723. hs->extended_master_secret = true;
  724. }
  725. // Whether EMS is negotiated may not change on renegotiation.
  726. if (ssl->s3->established_session != nullptr &&
  727. hs->extended_master_secret !=
  728. !!ssl->s3->established_session->extended_master_secret) {
  729. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
  730. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  731. return false;
  732. }
  733. return true;
  734. }
  735. static bool ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  736. CBS *contents) {
  737. uint16_t version = ssl_protocol_version(hs->ssl);
  738. if (version >= TLS1_3_VERSION ||
  739. version == SSL3_VERSION) {
  740. return true;
  741. }
  742. if (contents == NULL) {
  743. return true;
  744. }
  745. if (CBS_len(contents) != 0) {
  746. return false;
  747. }
  748. hs->extended_master_secret = true;
  749. return true;
  750. }
  751. static bool ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  752. if (!hs->extended_master_secret) {
  753. return true;
  754. }
  755. if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
  756. !CBB_add_u16(out, 0 /* length */)) {
  757. return false;
  758. }
  759. return true;
  760. }
  761. // Session tickets.
  762. //
  763. // https://tools.ietf.org/html/rfc5077
  764. static bool ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  765. SSL *const ssl = hs->ssl;
  766. // TLS 1.3 uses a different ticket extension.
  767. if (hs->min_version >= TLS1_3_VERSION ||
  768. SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
  769. return true;
  770. }
  771. const uint8_t *ticket_data = NULL;
  772. int ticket_len = 0;
  773. // Renegotiation does not participate in session resumption. However, still
  774. // advertise the extension to avoid potentially breaking servers which carry
  775. // over the state from the previous handshake, such as OpenSSL servers
  776. // without upstream's 3c3f0259238594d77264a78944d409f2127642c4.
  777. if (!ssl->s3->initial_handshake_complete &&
  778. ssl->session != NULL &&
  779. ssl->session->tlsext_tick != NULL &&
  780. // Don't send TLS 1.3 session tickets in the ticket extension.
  781. ssl_session_protocol_version(ssl->session) < TLS1_3_VERSION) {
  782. ticket_data = ssl->session->tlsext_tick;
  783. ticket_len = ssl->session->tlsext_ticklen;
  784. }
  785. CBB ticket;
  786. if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
  787. !CBB_add_u16_length_prefixed(out, &ticket) ||
  788. !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
  789. !CBB_flush(out)) {
  790. return false;
  791. }
  792. return true;
  793. }
  794. static bool ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  795. CBS *contents) {
  796. SSL *const ssl = hs->ssl;
  797. if (contents == NULL) {
  798. return true;
  799. }
  800. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  801. return false;
  802. }
  803. // If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
  804. // this function should never be called, even if the server tries to send the
  805. // extension.
  806. assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
  807. if (CBS_len(contents) != 0) {
  808. return false;
  809. }
  810. hs->ticket_expected = true;
  811. return true;
  812. }
  813. static bool ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  814. if (!hs->ticket_expected) {
  815. return true;
  816. }
  817. // If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true.
  818. assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
  819. if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
  820. !CBB_add_u16(out, 0 /* length */)) {
  821. return false;
  822. }
  823. return true;
  824. }
  825. // Signature Algorithms.
  826. //
  827. // https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
  828. static bool ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  829. SSL *const ssl = hs->ssl;
  830. if (hs->max_version < TLS1_2_VERSION) {
  831. return true;
  832. }
  833. CBB contents, sigalgs_cbb;
  834. if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
  835. !CBB_add_u16_length_prefixed(out, &contents) ||
  836. !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
  837. !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) ||
  838. !CBB_flush(out)) {
  839. return false;
  840. }
  841. return true;
  842. }
  843. static bool ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  844. CBS *contents) {
  845. hs->peer_sigalgs.Reset();
  846. if (contents == NULL) {
  847. return true;
  848. }
  849. CBS supported_signature_algorithms;
  850. if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
  851. CBS_len(contents) != 0 ||
  852. CBS_len(&supported_signature_algorithms) == 0 ||
  853. !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
  854. return false;
  855. }
  856. return true;
  857. }
  858. // OCSP Stapling.
  859. //
  860. // https://tools.ietf.org/html/rfc6066#section-8
  861. static bool ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  862. SSL *const ssl = hs->ssl;
  863. if (!ssl->ocsp_stapling_enabled) {
  864. return true;
  865. }
  866. CBB contents;
  867. if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
  868. !CBB_add_u16_length_prefixed(out, &contents) ||
  869. !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
  870. !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
  871. !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
  872. !CBB_flush(out)) {
  873. return false;
  874. }
  875. return true;
  876. }
  877. static bool ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  878. CBS *contents) {
  879. SSL *const ssl = hs->ssl;
  880. if (contents == NULL) {
  881. return true;
  882. }
  883. // TLS 1.3 OCSP responses are included in the Certificate extensions.
  884. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  885. return false;
  886. }
  887. // OCSP stapling is forbidden on non-certificate ciphers.
  888. if (CBS_len(contents) != 0 ||
  889. !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  890. return false;
  891. }
  892. // Note this does not check for resumption in TLS 1.2. Sending
  893. // status_request here does not make sense, but OpenSSL does so and the
  894. // specification does not say anything. Tolerate it but ignore it.
  895. hs->certificate_status_expected = true;
  896. return true;
  897. }
  898. static bool ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  899. CBS *contents) {
  900. if (contents == NULL) {
  901. return true;
  902. }
  903. uint8_t status_type;
  904. if (!CBS_get_u8(contents, &status_type)) {
  905. return false;
  906. }
  907. // We cannot decide whether OCSP stapling will occur yet because the correct
  908. // SSL_CTX might not have been selected.
  909. hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
  910. return true;
  911. }
  912. static bool ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  913. SSL *const ssl = hs->ssl;
  914. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
  915. !hs->ocsp_stapling_requested ||
  916. ssl->cert->ocsp_response == NULL ||
  917. ssl->s3->session_reused ||
  918. !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  919. return true;
  920. }
  921. hs->certificate_status_expected = true;
  922. return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
  923. CBB_add_u16(out, 0 /* length */);
  924. }
  925. // Next protocol negotiation.
  926. //
  927. // https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html
  928. static bool ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  929. SSL *const ssl = hs->ssl;
  930. if (ssl->s3->initial_handshake_complete ||
  931. ssl->ctx->next_proto_select_cb == NULL ||
  932. SSL_is_dtls(ssl)) {
  933. return true;
  934. }
  935. if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
  936. !CBB_add_u16(out, 0 /* length */)) {
  937. return false;
  938. }
  939. return true;
  940. }
  941. static bool ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  942. CBS *contents) {
  943. SSL *const ssl = hs->ssl;
  944. if (contents == NULL) {
  945. return true;
  946. }
  947. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  948. return false;
  949. }
  950. // If any of these are false then we should never have sent the NPN
  951. // extension in the ClientHello and thus this function should never have been
  952. // called.
  953. assert(!ssl->s3->initial_handshake_complete);
  954. assert(!SSL_is_dtls(ssl));
  955. assert(ssl->ctx->next_proto_select_cb != NULL);
  956. if (!ssl->s3->alpn_selected.empty()) {
  957. // NPN and ALPN may not be negotiated in the same connection.
  958. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  959. OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
  960. return false;
  961. }
  962. const uint8_t *const orig_contents = CBS_data(contents);
  963. const size_t orig_len = CBS_len(contents);
  964. while (CBS_len(contents) != 0) {
  965. CBS proto;
  966. if (!CBS_get_u8_length_prefixed(contents, &proto) ||
  967. CBS_len(&proto) == 0) {
  968. return false;
  969. }
  970. }
  971. uint8_t *selected;
  972. uint8_t selected_len;
  973. if (ssl->ctx->next_proto_select_cb(
  974. ssl, &selected, &selected_len, orig_contents, orig_len,
  975. ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK ||
  976. !ssl->s3->next_proto_negotiated.CopyFrom(
  977. MakeConstSpan(selected, selected_len))) {
  978. *out_alert = SSL_AD_INTERNAL_ERROR;
  979. return false;
  980. }
  981. hs->next_proto_neg_seen = true;
  982. return true;
  983. }
  984. static bool ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  985. CBS *contents) {
  986. SSL *const ssl = hs->ssl;
  987. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  988. return true;
  989. }
  990. if (contents != NULL && CBS_len(contents) != 0) {
  991. return false;
  992. }
  993. if (contents == NULL ||
  994. ssl->s3->initial_handshake_complete ||
  995. ssl->ctx->next_protos_advertised_cb == NULL ||
  996. SSL_is_dtls(ssl)) {
  997. return true;
  998. }
  999. hs->next_proto_neg_seen = true;
  1000. return true;
  1001. }
  1002. static bool ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1003. SSL *const ssl = hs->ssl;
  1004. // |next_proto_neg_seen| might have been cleared when an ALPN extension was
  1005. // parsed.
  1006. if (!hs->next_proto_neg_seen) {
  1007. return true;
  1008. }
  1009. const uint8_t *npa;
  1010. unsigned npa_len;
  1011. if (ssl->ctx->next_protos_advertised_cb(
  1012. ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
  1013. SSL_TLSEXT_ERR_OK) {
  1014. hs->next_proto_neg_seen = false;
  1015. return true;
  1016. }
  1017. CBB contents;
  1018. if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
  1019. !CBB_add_u16_length_prefixed(out, &contents) ||
  1020. !CBB_add_bytes(&contents, npa, npa_len) ||
  1021. !CBB_flush(out)) {
  1022. return false;
  1023. }
  1024. return true;
  1025. }
  1026. // Signed certificate timestamps.
  1027. //
  1028. // https://tools.ietf.org/html/rfc6962#section-3.3.1
  1029. static bool ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1030. SSL *const ssl = hs->ssl;
  1031. if (!ssl->signed_cert_timestamps_enabled) {
  1032. return true;
  1033. }
  1034. if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
  1035. !CBB_add_u16(out, 0 /* length */)) {
  1036. return false;
  1037. }
  1038. return true;
  1039. }
  1040. static bool ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1041. CBS *contents) {
  1042. SSL *const ssl = hs->ssl;
  1043. if (contents == NULL) {
  1044. return true;
  1045. }
  1046. // TLS 1.3 SCTs are included in the Certificate extensions.
  1047. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  1048. *out_alert = SSL_AD_DECODE_ERROR;
  1049. return false;
  1050. }
  1051. // If this is false then we should never have sent the SCT extension in the
  1052. // ClientHello and thus this function should never have been called.
  1053. assert(ssl->signed_cert_timestamps_enabled);
  1054. if (!ssl_is_sct_list_valid(contents)) {
  1055. *out_alert = SSL_AD_DECODE_ERROR;
  1056. return false;
  1057. }
  1058. // Session resumption uses the original session information. The extension
  1059. // should not be sent on resumption, but RFC 6962 did not make it a
  1060. // requirement, so tolerate this.
  1061. //
  1062. // TODO(davidben): Enforce this anyway.
  1063. if (!ssl->s3->session_reused) {
  1064. CRYPTO_BUFFER_free(hs->new_session->signed_cert_timestamp_list);
  1065. hs->new_session->signed_cert_timestamp_list =
  1066. CRYPTO_BUFFER_new_from_CBS(contents, ssl->ctx->pool);
  1067. if (hs->new_session->signed_cert_timestamp_list == nullptr) {
  1068. *out_alert = SSL_AD_INTERNAL_ERROR;
  1069. return false;
  1070. }
  1071. }
  1072. return true;
  1073. }
  1074. static bool ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1075. CBS *contents) {
  1076. if (contents == NULL) {
  1077. return true;
  1078. }
  1079. if (CBS_len(contents) != 0) {
  1080. return false;
  1081. }
  1082. hs->scts_requested = true;
  1083. return true;
  1084. }
  1085. static bool ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1086. SSL *const ssl = hs->ssl;
  1087. // The extension shouldn't be sent when resuming sessions.
  1088. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
  1089. ssl->s3->session_reused ||
  1090. ssl->cert->signed_cert_timestamp_list == NULL) {
  1091. return true;
  1092. }
  1093. CBB contents;
  1094. return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
  1095. CBB_add_u16_length_prefixed(out, &contents) &&
  1096. CBB_add_bytes(
  1097. &contents,
  1098. CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
  1099. CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
  1100. CBB_flush(out);
  1101. }
  1102. // Application-level Protocol Negotiation.
  1103. //
  1104. // https://tools.ietf.org/html/rfc7301
  1105. static bool ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1106. SSL *const ssl = hs->ssl;
  1107. if (ssl->alpn_client_proto_list == NULL ||
  1108. ssl->s3->initial_handshake_complete) {
  1109. return true;
  1110. }
  1111. CBB contents, proto_list;
  1112. if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
  1113. !CBB_add_u16_length_prefixed(out, &contents) ||
  1114. !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
  1115. !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
  1116. ssl->alpn_client_proto_list_len) ||
  1117. !CBB_flush(out)) {
  1118. return false;
  1119. }
  1120. return true;
  1121. }
  1122. static bool ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1123. CBS *contents) {
  1124. SSL *const ssl = hs->ssl;
  1125. if (contents == NULL) {
  1126. return true;
  1127. }
  1128. assert(!ssl->s3->initial_handshake_complete);
  1129. assert(ssl->alpn_client_proto_list != NULL);
  1130. if (hs->next_proto_neg_seen) {
  1131. // NPN and ALPN may not be negotiated in the same connection.
  1132. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1133. OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
  1134. return false;
  1135. }
  1136. // The extension data consists of a ProtocolNameList which must have
  1137. // exactly one ProtocolName. Each of these is length-prefixed.
  1138. CBS protocol_name_list, protocol_name;
  1139. if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
  1140. CBS_len(contents) != 0 ||
  1141. !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
  1142. // Empty protocol names are forbidden.
  1143. CBS_len(&protocol_name) == 0 ||
  1144. CBS_len(&protocol_name_list) != 0) {
  1145. return false;
  1146. }
  1147. if (!ssl_is_alpn_protocol_allowed(ssl, protocol_name)) {
  1148. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
  1149. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1150. return false;
  1151. }
  1152. if (!ssl->s3->alpn_selected.CopyFrom(protocol_name)) {
  1153. *out_alert = SSL_AD_INTERNAL_ERROR;
  1154. return false;
  1155. }
  1156. return true;
  1157. }
  1158. bool ssl_is_alpn_protocol_allowed(const SSL *ssl,
  1159. Span<const uint8_t> protocol) {
  1160. if (ssl->alpn_client_proto_list == nullptr) {
  1161. return false;
  1162. }
  1163. if (ssl->ctx->allow_unknown_alpn_protos) {
  1164. return true;
  1165. }
  1166. // Check that the protocol name is one of the ones we advertised.
  1167. CBS client_protocol_name_list, client_protocol_name;
  1168. CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
  1169. ssl->alpn_client_proto_list_len);
  1170. while (CBS_len(&client_protocol_name_list) > 0) {
  1171. if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
  1172. &client_protocol_name)) {
  1173. return false;
  1174. }
  1175. if (client_protocol_name == protocol) {
  1176. return true;
  1177. }
  1178. }
  1179. return false;
  1180. }
  1181. bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1182. const SSL_CLIENT_HELLO *client_hello) {
  1183. SSL *const ssl = hs->ssl;
  1184. CBS contents;
  1185. if (ssl->ctx->alpn_select_cb == NULL ||
  1186. !ssl_client_hello_get_extension(
  1187. client_hello, &contents,
  1188. TLSEXT_TYPE_application_layer_protocol_negotiation)) {
  1189. // Ignore ALPN if not configured or no extension was supplied.
  1190. return true;
  1191. }
  1192. // ALPN takes precedence over NPN.
  1193. hs->next_proto_neg_seen = false;
  1194. CBS protocol_name_list;
  1195. if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
  1196. CBS_len(&contents) != 0 ||
  1197. CBS_len(&protocol_name_list) < 2) {
  1198. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  1199. *out_alert = SSL_AD_DECODE_ERROR;
  1200. return false;
  1201. }
  1202. // Validate the protocol list.
  1203. CBS protocol_name_list_copy = protocol_name_list;
  1204. while (CBS_len(&protocol_name_list_copy) > 0) {
  1205. CBS protocol_name;
  1206. if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
  1207. // Empty protocol names are forbidden.
  1208. CBS_len(&protocol_name) == 0) {
  1209. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  1210. *out_alert = SSL_AD_DECODE_ERROR;
  1211. return false;
  1212. }
  1213. }
  1214. const uint8_t *selected;
  1215. uint8_t selected_len;
  1216. if (ssl->ctx->alpn_select_cb(
  1217. ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
  1218. CBS_len(&protocol_name_list),
  1219. ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
  1220. if (!ssl->s3->alpn_selected.CopyFrom(
  1221. MakeConstSpan(selected, selected_len))) {
  1222. *out_alert = SSL_AD_INTERNAL_ERROR;
  1223. return false;
  1224. }
  1225. }
  1226. return true;
  1227. }
  1228. static bool ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1229. SSL *const ssl = hs->ssl;
  1230. if (ssl->s3->alpn_selected.empty()) {
  1231. return true;
  1232. }
  1233. CBB contents, proto_list, proto;
  1234. if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
  1235. !CBB_add_u16_length_prefixed(out, &contents) ||
  1236. !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
  1237. !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
  1238. !CBB_add_bytes(&proto, ssl->s3->alpn_selected.data(),
  1239. ssl->s3->alpn_selected.size()) ||
  1240. !CBB_flush(out)) {
  1241. return false;
  1242. }
  1243. return true;
  1244. }
  1245. // Channel ID.
  1246. //
  1247. // https://tools.ietf.org/html/draft-balfanz-tls-channelid-01
  1248. static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
  1249. hs->ssl->s3->tlsext_channel_id_valid = false;
  1250. }
  1251. static bool ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1252. SSL *const ssl = hs->ssl;
  1253. if (!ssl->tlsext_channel_id_enabled ||
  1254. SSL_is_dtls(ssl)) {
  1255. return true;
  1256. }
  1257. if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
  1258. !CBB_add_u16(out, 0 /* length */)) {
  1259. return false;
  1260. }
  1261. return true;
  1262. }
  1263. static bool ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
  1264. uint8_t *out_alert,
  1265. CBS *contents) {
  1266. SSL *const ssl = hs->ssl;
  1267. if (contents == NULL) {
  1268. return true;
  1269. }
  1270. assert(!SSL_is_dtls(ssl));
  1271. assert(ssl->tlsext_channel_id_enabled);
  1272. if (CBS_len(contents) != 0) {
  1273. return false;
  1274. }
  1275. ssl->s3->tlsext_channel_id_valid = true;
  1276. return true;
  1277. }
  1278. static bool ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
  1279. uint8_t *out_alert,
  1280. CBS *contents) {
  1281. SSL *const ssl = hs->ssl;
  1282. if (contents == NULL ||
  1283. !ssl->tlsext_channel_id_enabled ||
  1284. SSL_is_dtls(ssl)) {
  1285. return true;
  1286. }
  1287. if (CBS_len(contents) != 0) {
  1288. return false;
  1289. }
  1290. ssl->s3->tlsext_channel_id_valid = true;
  1291. return true;
  1292. }
  1293. static bool ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1294. SSL *const ssl = hs->ssl;
  1295. if (!ssl->s3->tlsext_channel_id_valid) {
  1296. return true;
  1297. }
  1298. if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
  1299. !CBB_add_u16(out, 0 /* length */)) {
  1300. return false;
  1301. }
  1302. return true;
  1303. }
  1304. // Secure Real-time Transport Protocol (SRTP) extension.
  1305. //
  1306. // https://tools.ietf.org/html/rfc5764
  1307. static void ext_srtp_init(SSL_HANDSHAKE *hs) {
  1308. hs->ssl->srtp_profile = NULL;
  1309. }
  1310. static bool ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1311. SSL *const ssl = hs->ssl;
  1312. STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
  1313. if (profiles == NULL ||
  1314. sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0) {
  1315. return true;
  1316. }
  1317. CBB contents, profile_ids;
  1318. if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
  1319. !CBB_add_u16_length_prefixed(out, &contents) ||
  1320. !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
  1321. return false;
  1322. }
  1323. for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
  1324. if (!CBB_add_u16(&profile_ids, profile->id)) {
  1325. return false;
  1326. }
  1327. }
  1328. if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
  1329. !CBB_flush(out)) {
  1330. return false;
  1331. }
  1332. return true;
  1333. }
  1334. static bool ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1335. CBS *contents) {
  1336. SSL *const ssl = hs->ssl;
  1337. if (contents == NULL) {
  1338. return true;
  1339. }
  1340. // The extension consists of a u16-prefixed profile ID list containing a
  1341. // single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
  1342. //
  1343. // See https://tools.ietf.org/html/rfc5764#section-4.1.1
  1344. CBS profile_ids, srtp_mki;
  1345. uint16_t profile_id;
  1346. if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
  1347. !CBS_get_u16(&profile_ids, &profile_id) ||
  1348. CBS_len(&profile_ids) != 0 ||
  1349. !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
  1350. CBS_len(contents) != 0) {
  1351. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1352. return false;
  1353. }
  1354. if (CBS_len(&srtp_mki) != 0) {
  1355. // Must be no MKI, since we never offer one.
  1356. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
  1357. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1358. return false;
  1359. }
  1360. STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
  1361. // Check to see if the server gave us something we support (and presumably
  1362. // offered).
  1363. for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
  1364. if (profile->id == profile_id) {
  1365. ssl->srtp_profile = profile;
  1366. return true;
  1367. }
  1368. }
  1369. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1370. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1371. return false;
  1372. }
  1373. static bool ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1374. CBS *contents) {
  1375. SSL *const ssl = hs->ssl;
  1376. if (contents == NULL) {
  1377. return true;
  1378. }
  1379. CBS profile_ids, srtp_mki;
  1380. if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
  1381. CBS_len(&profile_ids) < 2 ||
  1382. !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
  1383. CBS_len(contents) != 0) {
  1384. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1385. return false;
  1386. }
  1387. // Discard the MKI value for now.
  1388. const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
  1389. SSL_get_srtp_profiles(ssl);
  1390. // Pick the server's most preferred profile.
  1391. for (const SRTP_PROTECTION_PROFILE *server_profile : server_profiles) {
  1392. CBS profile_ids_tmp;
  1393. CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
  1394. while (CBS_len(&profile_ids_tmp) > 0) {
  1395. uint16_t profile_id;
  1396. if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
  1397. return false;
  1398. }
  1399. if (server_profile->id == profile_id) {
  1400. ssl->srtp_profile = server_profile;
  1401. return true;
  1402. }
  1403. }
  1404. }
  1405. return true;
  1406. }
  1407. static bool ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1408. SSL *const ssl = hs->ssl;
  1409. if (ssl->srtp_profile == NULL) {
  1410. return true;
  1411. }
  1412. CBB contents, profile_ids;
  1413. if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
  1414. !CBB_add_u16_length_prefixed(out, &contents) ||
  1415. !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
  1416. !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
  1417. !CBB_add_u8(&contents, 0 /* empty MKI */) ||
  1418. !CBB_flush(out)) {
  1419. return false;
  1420. }
  1421. return true;
  1422. }
  1423. // EC point formats.
  1424. //
  1425. // https://tools.ietf.org/html/rfc4492#section-5.1.2
  1426. static bool ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
  1427. CBB contents, formats;
  1428. if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
  1429. !CBB_add_u16_length_prefixed(out, &contents) ||
  1430. !CBB_add_u8_length_prefixed(&contents, &formats) ||
  1431. !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
  1432. !CBB_flush(out)) {
  1433. return false;
  1434. }
  1435. return true;
  1436. }
  1437. static bool ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1438. // The point format extension is unneccessary in TLS 1.3.
  1439. if (hs->min_version >= TLS1_3_VERSION) {
  1440. return true;
  1441. }
  1442. return ext_ec_point_add_extension(hs, out);
  1443. }
  1444. static bool ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1445. CBS *contents) {
  1446. if (contents == NULL) {
  1447. return true;
  1448. }
  1449. if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
  1450. return false;
  1451. }
  1452. CBS ec_point_format_list;
  1453. if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
  1454. CBS_len(contents) != 0) {
  1455. return false;
  1456. }
  1457. // Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
  1458. // point format.
  1459. if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
  1460. TLSEXT_ECPOINTFORMAT_uncompressed,
  1461. CBS_len(&ec_point_format_list)) == NULL) {
  1462. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1463. return false;
  1464. }
  1465. return true;
  1466. }
  1467. static bool ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1468. CBS *contents) {
  1469. if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
  1470. return true;
  1471. }
  1472. return ext_ec_point_parse_serverhello(hs, out_alert, contents);
  1473. }
  1474. static bool ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1475. SSL *const ssl = hs->ssl;
  1476. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  1477. return true;
  1478. }
  1479. const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  1480. const uint32_t alg_a = hs->new_cipher->algorithm_auth;
  1481. const bool using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
  1482. if (!using_ecc) {
  1483. return true;
  1484. }
  1485. return ext_ec_point_add_extension(hs, out);
  1486. }
  1487. // Pre Shared Key
  1488. //
  1489. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6
  1490. static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
  1491. SSL *const ssl = hs->ssl;
  1492. if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
  1493. ssl_session_protocol_version(ssl->session) < TLS1_3_VERSION) {
  1494. return 0;
  1495. }
  1496. size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session));
  1497. return 15 + ssl->session->tlsext_ticklen + binder_len;
  1498. }
  1499. static bool ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1500. SSL *const ssl = hs->ssl;
  1501. hs->needs_psk_binder = false;
  1502. if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
  1503. ssl_session_protocol_version(ssl->session) < TLS1_3_VERSION) {
  1504. return true;
  1505. }
  1506. // Per draft-ietf-tls-tls13-21 section 4.1.4, skip offering the session if the
  1507. // selected cipher in HelloRetryRequest does not match. This avoids performing
  1508. // the transcript hash transformation for multiple hashes.
  1509. if (hs->received_hello_retry_request &&
  1510. ssl_is_draft21(ssl->version) &&
  1511. ssl->session->cipher->algorithm_prf != hs->new_cipher->algorithm_prf) {
  1512. return true;
  1513. }
  1514. struct OPENSSL_timeval now;
  1515. ssl_get_current_time(ssl, &now);
  1516. uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
  1517. uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
  1518. // Fill in a placeholder zero binder of the appropriate length. It will be
  1519. // computed and filled in later after length prefixes are computed.
  1520. uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
  1521. size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session));
  1522. CBB contents, identity, ticket, binders, binder;
  1523. if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
  1524. !CBB_add_u16_length_prefixed(out, &contents) ||
  1525. !CBB_add_u16_length_prefixed(&contents, &identity) ||
  1526. !CBB_add_u16_length_prefixed(&identity, &ticket) ||
  1527. !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
  1528. ssl->session->tlsext_ticklen) ||
  1529. !CBB_add_u32(&identity, obfuscated_ticket_age) ||
  1530. !CBB_add_u16_length_prefixed(&contents, &binders) ||
  1531. !CBB_add_u8_length_prefixed(&binders, &binder) ||
  1532. !CBB_add_bytes(&binder, zero_binder, binder_len)) {
  1533. return false;
  1534. }
  1535. hs->needs_psk_binder = true;
  1536. return CBB_flush(out);
  1537. }
  1538. bool ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
  1539. uint8_t *out_alert,
  1540. CBS *contents) {
  1541. uint16_t psk_id;
  1542. if (!CBS_get_u16(contents, &psk_id) ||
  1543. CBS_len(contents) != 0) {
  1544. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1545. *out_alert = SSL_AD_DECODE_ERROR;
  1546. return false;
  1547. }
  1548. // We only advertise one PSK identity, so the only legal index is zero.
  1549. if (psk_id != 0) {
  1550. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
  1551. *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
  1552. return false;
  1553. }
  1554. return true;
  1555. }
  1556. bool ssl_ext_pre_shared_key_parse_clienthello(
  1557. SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
  1558. uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
  1559. // We only process the first PSK identity since we don't support pure PSK.
  1560. CBS identities, binders;
  1561. if (!CBS_get_u16_length_prefixed(contents, &identities) ||
  1562. !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
  1563. !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
  1564. !CBS_get_u16_length_prefixed(contents, &binders) ||
  1565. CBS_len(&binders) == 0 ||
  1566. CBS_len(contents) != 0) {
  1567. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1568. *out_alert = SSL_AD_DECODE_ERROR;
  1569. return false;
  1570. }
  1571. *out_binders = binders;
  1572. // Check the syntax of the remaining identities, but do not process them.
  1573. size_t num_identities = 1;
  1574. while (CBS_len(&identities) != 0) {
  1575. CBS unused_ticket;
  1576. uint32_t unused_obfuscated_ticket_age;
  1577. if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
  1578. !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
  1579. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1580. *out_alert = SSL_AD_DECODE_ERROR;
  1581. return false;
  1582. }
  1583. num_identities++;
  1584. }
  1585. // Check the syntax of the binders. The value will be checked later if
  1586. // resuming.
  1587. size_t num_binders = 0;
  1588. while (CBS_len(&binders) != 0) {
  1589. CBS binder;
  1590. if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
  1591. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1592. *out_alert = SSL_AD_DECODE_ERROR;
  1593. return false;
  1594. }
  1595. num_binders++;
  1596. }
  1597. if (num_identities != num_binders) {
  1598. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
  1599. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1600. return false;
  1601. }
  1602. return true;
  1603. }
  1604. bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1605. if (!hs->ssl->s3->session_reused) {
  1606. return true;
  1607. }
  1608. CBB contents;
  1609. if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
  1610. !CBB_add_u16_length_prefixed(out, &contents) ||
  1611. // We only consider the first identity for resumption
  1612. !CBB_add_u16(&contents, 0) ||
  1613. !CBB_flush(out)) {
  1614. return false;
  1615. }
  1616. return true;
  1617. }
  1618. // Pre-Shared Key Exchange Modes
  1619. //
  1620. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7
  1621. static bool ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
  1622. CBB *out) {
  1623. if (hs->max_version < TLS1_3_VERSION) {
  1624. return true;
  1625. }
  1626. CBB contents, ke_modes;
  1627. if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
  1628. !CBB_add_u16_length_prefixed(out, &contents) ||
  1629. !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
  1630. !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
  1631. return false;
  1632. }
  1633. return CBB_flush(out);
  1634. }
  1635. static bool ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
  1636. uint8_t *out_alert,
  1637. CBS *contents) {
  1638. if (contents == NULL) {
  1639. return true;
  1640. }
  1641. CBS ke_modes;
  1642. if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
  1643. CBS_len(&ke_modes) == 0 ||
  1644. CBS_len(contents) != 0) {
  1645. *out_alert = SSL_AD_DECODE_ERROR;
  1646. return false;
  1647. }
  1648. // We only support tickets with PSK_DHE_KE.
  1649. hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
  1650. CBS_len(&ke_modes)) != NULL;
  1651. return true;
  1652. }
  1653. // Early Data Indication
  1654. //
  1655. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8
  1656. static bool ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1657. SSL *const ssl = hs->ssl;
  1658. if (!ssl->cert->enable_early_data ||
  1659. // Session must be 0-RTT capable.
  1660. ssl->session == NULL ||
  1661. ssl_session_protocol_version(ssl->session) < TLS1_3_VERSION ||
  1662. ssl->session->ticket_max_early_data == 0 ||
  1663. // The second ClientHello never offers early data.
  1664. hs->received_hello_retry_request ||
  1665. // In case ALPN preferences changed since this session was established,
  1666. // avoid reporting a confusing value in |SSL_get0_alpn_selected|.
  1667. (ssl->session->early_alpn_len != 0 &&
  1668. !ssl_is_alpn_protocol_allowed(
  1669. ssl, MakeConstSpan(ssl->session->early_alpn,
  1670. ssl->session->early_alpn_len)))) {
  1671. return true;
  1672. }
  1673. hs->early_data_offered = true;
  1674. if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
  1675. !CBB_add_u16(out, 0) ||
  1676. !CBB_flush(out)) {
  1677. return false;
  1678. }
  1679. return true;
  1680. }
  1681. static bool ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
  1682. uint8_t *out_alert, CBS *contents) {
  1683. SSL *const ssl = hs->ssl;
  1684. if (contents == NULL) {
  1685. return true;
  1686. }
  1687. if (CBS_len(contents) != 0) {
  1688. *out_alert = SSL_AD_DECODE_ERROR;
  1689. return false;
  1690. }
  1691. if (!ssl->s3->session_reused) {
  1692. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  1693. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
  1694. return false;
  1695. }
  1696. ssl->early_data_accepted = true;
  1697. return true;
  1698. }
  1699. static bool ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
  1700. uint8_t *out_alert, CBS *contents) {
  1701. SSL *const ssl = hs->ssl;
  1702. if (contents == NULL ||
  1703. ssl_protocol_version(ssl) < TLS1_3_VERSION) {
  1704. return true;
  1705. }
  1706. if (CBS_len(contents) != 0) {
  1707. *out_alert = SSL_AD_DECODE_ERROR;
  1708. return false;
  1709. }
  1710. hs->early_data_offered = true;
  1711. return true;
  1712. }
  1713. static bool ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1714. if (!hs->ssl->early_data_accepted) {
  1715. return true;
  1716. }
  1717. if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
  1718. !CBB_add_u16(out, 0) ||
  1719. !CBB_flush(out)) {
  1720. return false;
  1721. }
  1722. return true;
  1723. }
  1724. // Key Share
  1725. //
  1726. // https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5
  1727. static bool ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1728. SSL *const ssl = hs->ssl;
  1729. if (hs->max_version < TLS1_3_VERSION) {
  1730. return true;
  1731. }
  1732. CBB contents, kse_bytes;
  1733. if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
  1734. !CBB_add_u16_length_prefixed(out, &contents) ||
  1735. !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
  1736. return false;
  1737. }
  1738. uint16_t group_id = hs->retry_group;
  1739. if (hs->received_hello_retry_request) {
  1740. // We received a HelloRetryRequest without a new curve, so there is no new
  1741. // share to append. Leave |hs->key_share| as-is.
  1742. if (group_id == 0 &&
  1743. !CBB_add_bytes(&kse_bytes, hs->key_share_bytes.data(),
  1744. hs->key_share_bytes.size())) {
  1745. return false;
  1746. }
  1747. hs->key_share_bytes.Reset();
  1748. if (group_id == 0) {
  1749. return CBB_flush(out);
  1750. }
  1751. } else {
  1752. // Add a fake group. See draft-davidben-tls-grease-01.
  1753. if (ssl->ctx->grease_enabled &&
  1754. (!CBB_add_u16(&kse_bytes,
  1755. ssl_get_grease_value(ssl, ssl_grease_group)) ||
  1756. !CBB_add_u16(&kse_bytes, 1 /* length */) ||
  1757. !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
  1758. return false;
  1759. }
  1760. // Predict the most preferred group.
  1761. Span<const uint16_t> groups = tls1_get_grouplist(ssl);
  1762. if (groups.empty()) {
  1763. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
  1764. return false;
  1765. }
  1766. group_id = groups[0];
  1767. }
  1768. hs->key_share = SSLKeyShare::Create(group_id);
  1769. CBB key_exchange;
  1770. if (!hs->key_share ||
  1771. !CBB_add_u16(&kse_bytes, group_id) ||
  1772. !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
  1773. !hs->key_share->Offer(&key_exchange) ||
  1774. !CBB_flush(&kse_bytes)) {
  1775. return false;
  1776. }
  1777. // Save the contents of the extension to repeat it in the second ClientHello.
  1778. if (!hs->received_hello_retry_request &&
  1779. !hs->key_share_bytes.CopyFrom(
  1780. MakeConstSpan(CBB_data(&kse_bytes), CBB_len(&kse_bytes)))) {
  1781. return false;
  1782. }
  1783. return CBB_flush(out);
  1784. }
  1785. bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
  1786. Array<uint8_t> *out_secret,
  1787. uint8_t *out_alert, CBS *contents) {
  1788. CBS peer_key;
  1789. uint16_t group_id;
  1790. if (!CBS_get_u16(contents, &group_id) ||
  1791. !CBS_get_u16_length_prefixed(contents, &peer_key) ||
  1792. CBS_len(contents) != 0) {
  1793. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1794. *out_alert = SSL_AD_DECODE_ERROR;
  1795. return false;
  1796. }
  1797. if (hs->key_share->GroupID() != group_id) {
  1798. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1799. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
  1800. return false;
  1801. }
  1802. if (!hs->key_share->Finish(out_secret, out_alert, peer_key)) {
  1803. *out_alert = SSL_AD_INTERNAL_ERROR;
  1804. return false;
  1805. }
  1806. hs->new_session->group_id = group_id;
  1807. hs->key_share.reset();
  1808. return true;
  1809. }
  1810. bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
  1811. Array<uint8_t> *out_secret,
  1812. uint8_t *out_alert, CBS *contents) {
  1813. uint16_t group_id;
  1814. CBS key_shares;
  1815. if (!tls1_get_shared_group(hs, &group_id)) {
  1816. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
  1817. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  1818. return false;
  1819. }
  1820. if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
  1821. CBS_len(contents) != 0) {
  1822. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1823. return false;
  1824. }
  1825. // Find the corresponding key share.
  1826. CBS peer_key;
  1827. CBS_init(&peer_key, NULL, 0);
  1828. while (CBS_len(&key_shares) > 0) {
  1829. uint16_t id;
  1830. CBS peer_key_tmp;
  1831. if (!CBS_get_u16(&key_shares, &id) ||
  1832. !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp) ||
  1833. CBS_len(&peer_key_tmp) == 0) {
  1834. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1835. return false;
  1836. }
  1837. if (id == group_id) {
  1838. if (CBS_len(&peer_key) != 0) {
  1839. OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
  1840. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1841. return false;
  1842. }
  1843. peer_key = peer_key_tmp;
  1844. // Continue parsing the structure to keep peers honest.
  1845. }
  1846. }
  1847. if (CBS_len(&peer_key) == 0) {
  1848. *out_found = false;
  1849. out_secret->Reset();
  1850. return true;
  1851. }
  1852. // Compute the DH secret.
  1853. Array<uint8_t> secret;
  1854. ScopedCBB public_key;
  1855. UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id);
  1856. if (!key_share ||
  1857. !CBB_init(public_key.get(), 32) ||
  1858. !key_share->Accept(public_key.get(), &secret, out_alert, peer_key) ||
  1859. !CBBFinishArray(public_key.get(), &hs->ecdh_public_key)) {
  1860. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1861. return false;
  1862. }
  1863. *out_secret = std::move(secret);
  1864. *out_found = true;
  1865. return true;
  1866. }
  1867. bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1868. uint16_t group_id;
  1869. CBB kse_bytes, public_key;
  1870. if (!tls1_get_shared_group(hs, &group_id) ||
  1871. !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
  1872. !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
  1873. !CBB_add_u16(&kse_bytes, group_id) ||
  1874. !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
  1875. !CBB_add_bytes(&public_key, hs->ecdh_public_key.data(),
  1876. hs->ecdh_public_key.size()) ||
  1877. !CBB_flush(out)) {
  1878. return false;
  1879. }
  1880. hs->ecdh_public_key.Reset();
  1881. hs->new_session->group_id = group_id;
  1882. return true;
  1883. }
  1884. // Supported Versions
  1885. //
  1886. // https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1
  1887. static bool ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1888. SSL *const ssl = hs->ssl;
  1889. if (hs->max_version <= TLS1_2_VERSION) {
  1890. return true;
  1891. }
  1892. CBB contents, versions;
  1893. if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
  1894. !CBB_add_u16_length_prefixed(out, &contents) ||
  1895. !CBB_add_u8_length_prefixed(&contents, &versions)) {
  1896. return false;
  1897. }
  1898. // Add a fake version. See draft-davidben-tls-grease-01.
  1899. if (ssl->ctx->grease_enabled &&
  1900. !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
  1901. return false;
  1902. }
  1903. if (!ssl_add_supported_versions(hs, &versions) ||
  1904. !CBB_flush(out)) {
  1905. return false;
  1906. }
  1907. return true;
  1908. }
  1909. // Cookie
  1910. //
  1911. // https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2
  1912. static bool ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1913. if (hs->cookie.empty()) {
  1914. return true;
  1915. }
  1916. CBB contents, cookie;
  1917. if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
  1918. !CBB_add_u16_length_prefixed(out, &contents) ||
  1919. !CBB_add_u16_length_prefixed(&contents, &cookie) ||
  1920. !CBB_add_bytes(&cookie, hs->cookie.data(), hs->cookie.size()) ||
  1921. !CBB_flush(out)) {
  1922. return false;
  1923. }
  1924. // The cookie is no longer needed in memory.
  1925. hs->cookie.Reset();
  1926. return true;
  1927. }
  1928. // Negotiated Groups
  1929. //
  1930. // https://tools.ietf.org/html/rfc4492#section-5.1.2
  1931. // https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4
  1932. static bool ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1933. SSL *const ssl = hs->ssl;
  1934. CBB contents, groups_bytes;
  1935. if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
  1936. !CBB_add_u16_length_prefixed(out, &contents) ||
  1937. !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
  1938. return false;
  1939. }
  1940. // Add a fake group. See draft-davidben-tls-grease-01.
  1941. if (ssl->ctx->grease_enabled &&
  1942. !CBB_add_u16(&groups_bytes,
  1943. ssl_get_grease_value(ssl, ssl_grease_group))) {
  1944. return false;
  1945. }
  1946. for (uint16_t group : tls1_get_grouplist(ssl)) {
  1947. if (!CBB_add_u16(&groups_bytes, group)) {
  1948. return false;
  1949. }
  1950. }
  1951. return CBB_flush(out);
  1952. }
  1953. static bool ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
  1954. uint8_t *out_alert,
  1955. CBS *contents) {
  1956. // This extension is not expected to be echoed by servers in TLS 1.2, but some
  1957. // BigIP servers send it nonetheless, so do not enforce this.
  1958. return true;
  1959. }
  1960. static bool parse_u16_array(const CBS *cbs, Array<uint16_t> *out) {
  1961. CBS copy = *cbs;
  1962. if ((CBS_len(&copy) & 1) != 0) {
  1963. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1964. return false;
  1965. }
  1966. Array<uint16_t> ret;
  1967. if (!ret.Init(CBS_len(&copy) / 2)) {
  1968. return false;
  1969. }
  1970. for (size_t i = 0; i < ret.size(); i++) {
  1971. if (!CBS_get_u16(&copy, &ret[i])) {
  1972. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1973. return false;
  1974. }
  1975. }
  1976. assert(CBS_len(&copy) == 0);
  1977. *out = std::move(ret);
  1978. return 1;
  1979. }
  1980. static bool ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
  1981. uint8_t *out_alert,
  1982. CBS *contents) {
  1983. if (contents == NULL) {
  1984. return true;
  1985. }
  1986. CBS supported_group_list;
  1987. if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
  1988. CBS_len(&supported_group_list) == 0 ||
  1989. CBS_len(contents) != 0 ||
  1990. !parse_u16_array(&supported_group_list, &hs->peer_supported_group_list)) {
  1991. return false;
  1992. }
  1993. return true;
  1994. }
  1995. // kExtensions contains all the supported extensions.
  1996. static const struct tls_extension kExtensions[] = {
  1997. {
  1998. TLSEXT_TYPE_renegotiate,
  1999. NULL,
  2000. ext_ri_add_clienthello,
  2001. ext_ri_parse_serverhello,
  2002. ext_ri_parse_clienthello,
  2003. ext_ri_add_serverhello,
  2004. },
  2005. {
  2006. TLSEXT_TYPE_server_name,
  2007. NULL,
  2008. ext_sni_add_clienthello,
  2009. ext_sni_parse_serverhello,
  2010. ext_sni_parse_clienthello,
  2011. ext_sni_add_serverhello,
  2012. },
  2013. {
  2014. TLSEXT_TYPE_extended_master_secret,
  2015. NULL,
  2016. ext_ems_add_clienthello,
  2017. ext_ems_parse_serverhello,
  2018. ext_ems_parse_clienthello,
  2019. ext_ems_add_serverhello,
  2020. },
  2021. {
  2022. TLSEXT_TYPE_session_ticket,
  2023. NULL,
  2024. ext_ticket_add_clienthello,
  2025. ext_ticket_parse_serverhello,
  2026. // Ticket extension client parsing is handled in ssl_session.c
  2027. ignore_parse_clienthello,
  2028. ext_ticket_add_serverhello,
  2029. },
  2030. {
  2031. TLSEXT_TYPE_signature_algorithms,
  2032. NULL,
  2033. ext_sigalgs_add_clienthello,
  2034. forbid_parse_serverhello,
  2035. ext_sigalgs_parse_clienthello,
  2036. dont_add_serverhello,
  2037. },
  2038. {
  2039. TLSEXT_TYPE_status_request,
  2040. NULL,
  2041. ext_ocsp_add_clienthello,
  2042. ext_ocsp_parse_serverhello,
  2043. ext_ocsp_parse_clienthello,
  2044. ext_ocsp_add_serverhello,
  2045. },
  2046. {
  2047. TLSEXT_TYPE_next_proto_neg,
  2048. NULL,
  2049. ext_npn_add_clienthello,
  2050. ext_npn_parse_serverhello,
  2051. ext_npn_parse_clienthello,
  2052. ext_npn_add_serverhello,
  2053. },
  2054. {
  2055. TLSEXT_TYPE_certificate_timestamp,
  2056. NULL,
  2057. ext_sct_add_clienthello,
  2058. ext_sct_parse_serverhello,
  2059. ext_sct_parse_clienthello,
  2060. ext_sct_add_serverhello,
  2061. },
  2062. {
  2063. TLSEXT_TYPE_application_layer_protocol_negotiation,
  2064. NULL,
  2065. ext_alpn_add_clienthello,
  2066. ext_alpn_parse_serverhello,
  2067. // ALPN is negotiated late in |ssl_negotiate_alpn|.
  2068. ignore_parse_clienthello,
  2069. ext_alpn_add_serverhello,
  2070. },
  2071. {
  2072. TLSEXT_TYPE_channel_id,
  2073. ext_channel_id_init,
  2074. ext_channel_id_add_clienthello,
  2075. ext_channel_id_parse_serverhello,
  2076. ext_channel_id_parse_clienthello,
  2077. ext_channel_id_add_serverhello,
  2078. },
  2079. {
  2080. TLSEXT_TYPE_srtp,
  2081. ext_srtp_init,
  2082. ext_srtp_add_clienthello,
  2083. ext_srtp_parse_serverhello,
  2084. ext_srtp_parse_clienthello,
  2085. ext_srtp_add_serverhello,
  2086. },
  2087. {
  2088. TLSEXT_TYPE_ec_point_formats,
  2089. NULL,
  2090. ext_ec_point_add_clienthello,
  2091. ext_ec_point_parse_serverhello,
  2092. ext_ec_point_parse_clienthello,
  2093. ext_ec_point_add_serverhello,
  2094. },
  2095. {
  2096. TLSEXT_TYPE_key_share,
  2097. NULL,
  2098. ext_key_share_add_clienthello,
  2099. forbid_parse_serverhello,
  2100. ignore_parse_clienthello,
  2101. dont_add_serverhello,
  2102. },
  2103. {
  2104. TLSEXT_TYPE_psk_key_exchange_modes,
  2105. NULL,
  2106. ext_psk_key_exchange_modes_add_clienthello,
  2107. forbid_parse_serverhello,
  2108. ext_psk_key_exchange_modes_parse_clienthello,
  2109. dont_add_serverhello,
  2110. },
  2111. {
  2112. TLSEXT_TYPE_early_data,
  2113. NULL,
  2114. ext_early_data_add_clienthello,
  2115. ext_early_data_parse_serverhello,
  2116. ext_early_data_parse_clienthello,
  2117. ext_early_data_add_serverhello,
  2118. },
  2119. {
  2120. TLSEXT_TYPE_supported_versions,
  2121. NULL,
  2122. ext_supported_versions_add_clienthello,
  2123. forbid_parse_serverhello,
  2124. ignore_parse_clienthello,
  2125. dont_add_serverhello,
  2126. },
  2127. {
  2128. TLSEXT_TYPE_cookie,
  2129. NULL,
  2130. ext_cookie_add_clienthello,
  2131. forbid_parse_serverhello,
  2132. ignore_parse_clienthello,
  2133. dont_add_serverhello,
  2134. },
  2135. // The final extension must be non-empty. WebSphere Application Server 7.0 is
  2136. // intolerant to the last extension being zero-length. See
  2137. // https://crbug.com/363583.
  2138. {
  2139. TLSEXT_TYPE_supported_groups,
  2140. NULL,
  2141. ext_supported_groups_add_clienthello,
  2142. ext_supported_groups_parse_serverhello,
  2143. ext_supported_groups_parse_clienthello,
  2144. dont_add_serverhello,
  2145. },
  2146. };
  2147. #define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
  2148. static_assert(kNumExtensions <=
  2149. sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
  2150. "too many extensions for sent bitset");
  2151. static_assert(kNumExtensions <=
  2152. sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
  2153. "too many extensions for received bitset");
  2154. static const struct tls_extension *tls_extension_find(uint32_t *out_index,
  2155. uint16_t value) {
  2156. unsigned i;
  2157. for (i = 0; i < kNumExtensions; i++) {
  2158. if (kExtensions[i].value == value) {
  2159. *out_index = i;
  2160. return &kExtensions[i];
  2161. }
  2162. }
  2163. return NULL;
  2164. }
  2165. int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
  2166. SSL *const ssl = hs->ssl;
  2167. // Don't add extensions for SSLv3 unless doing secure renegotiation.
  2168. if (hs->client_version == SSL3_VERSION &&
  2169. !ssl->s3->send_connection_binding) {
  2170. return 1;
  2171. }
  2172. CBB extensions;
  2173. if (!CBB_add_u16_length_prefixed(out, &extensions)) {
  2174. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2175. return 0;
  2176. }
  2177. hs->extensions.sent = 0;
  2178. hs->custom_extensions.sent = 0;
  2179. for (size_t i = 0; i < kNumExtensions; i++) {
  2180. if (kExtensions[i].init != NULL) {
  2181. kExtensions[i].init(hs);
  2182. }
  2183. }
  2184. uint16_t grease_ext1 = 0;
  2185. if (ssl->ctx->grease_enabled) {
  2186. // Add a fake empty extension. See draft-davidben-tls-grease-01.
  2187. grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
  2188. if (!CBB_add_u16(&extensions, grease_ext1) ||
  2189. !CBB_add_u16(&extensions, 0 /* zero length */)) {
  2190. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2191. return 0;
  2192. }
  2193. }
  2194. for (size_t i = 0; i < kNumExtensions; i++) {
  2195. const size_t len_before = CBB_len(&extensions);
  2196. if (!kExtensions[i].add_clienthello(hs, &extensions)) {
  2197. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
  2198. ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
  2199. return 0;
  2200. }
  2201. if (CBB_len(&extensions) != len_before) {
  2202. hs->extensions.sent |= (1u << i);
  2203. }
  2204. }
  2205. if (!custom_ext_add_clienthello(hs, &extensions)) {
  2206. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2207. return 0;
  2208. }
  2209. if (ssl->ctx->grease_enabled) {
  2210. // Add a fake non-empty extension. See draft-davidben-tls-grease-01.
  2211. uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
  2212. // The two fake extensions must not have the same value. GREASE values are
  2213. // of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
  2214. // one.
  2215. if (grease_ext1 == grease_ext2) {
  2216. grease_ext2 ^= 0x1010;
  2217. }
  2218. if (!CBB_add_u16(&extensions, grease_ext2) ||
  2219. !CBB_add_u16(&extensions, 1 /* one byte length */) ||
  2220. !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
  2221. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2222. return 0;
  2223. }
  2224. }
  2225. if (!SSL_is_dtls(ssl)) {
  2226. size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
  2227. header_len += 2 + CBB_len(&extensions) + psk_extension_len;
  2228. if (header_len > 0xff && header_len < 0x200) {
  2229. // Add padding to workaround bugs in F5 terminators. See RFC 7685.
  2230. //
  2231. // NB: because this code works out the length of all existing extensions
  2232. // it MUST always appear last.
  2233. size_t padding_len = 0x200 - header_len;
  2234. // Extensions take at least four bytes to encode. Always include at least
  2235. // one byte of data if including the extension. WebSphere Application
  2236. // Server 7.0 is intolerant to the last extension being zero-length. See
  2237. // https://crbug.com/363583.
  2238. if (padding_len >= 4 + 1) {
  2239. padding_len -= 4;
  2240. } else {
  2241. padding_len = 1;
  2242. }
  2243. uint8_t *padding_bytes;
  2244. if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
  2245. !CBB_add_u16(&extensions, padding_len) ||
  2246. !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
  2247. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2248. return 0;
  2249. }
  2250. OPENSSL_memset(padding_bytes, 0, padding_len);
  2251. }
  2252. }
  2253. // The PSK extension must be last, including after the padding.
  2254. if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
  2255. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2256. return 0;
  2257. }
  2258. // Discard empty extensions blocks.
  2259. if (CBB_len(&extensions) == 0) {
  2260. CBB_discard_child(out);
  2261. }
  2262. return CBB_flush(out);
  2263. }
  2264. int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
  2265. SSL *const ssl = hs->ssl;
  2266. CBB extensions;
  2267. if (!CBB_add_u16_length_prefixed(out, &extensions)) {
  2268. goto err;
  2269. }
  2270. for (unsigned i = 0; i < kNumExtensions; i++) {
  2271. if (!(hs->extensions.received & (1u << i))) {
  2272. // Don't send extensions that were not received.
  2273. continue;
  2274. }
  2275. if (!kExtensions[i].add_serverhello(hs, &extensions)) {
  2276. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
  2277. ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
  2278. goto err;
  2279. }
  2280. }
  2281. if (!custom_ext_add_serverhello(hs, &extensions)) {
  2282. goto err;
  2283. }
  2284. // Discard empty extensions blocks before TLS 1.3.
  2285. if (ssl_protocol_version(ssl) < TLS1_3_VERSION &&
  2286. CBB_len(&extensions) == 0) {
  2287. CBB_discard_child(out);
  2288. }
  2289. return CBB_flush(out);
  2290. err:
  2291. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2292. return 0;
  2293. }
  2294. static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
  2295. const SSL_CLIENT_HELLO *client_hello,
  2296. int *out_alert) {
  2297. SSL *const ssl = hs->ssl;
  2298. for (size_t i = 0; i < kNumExtensions; i++) {
  2299. if (kExtensions[i].init != NULL) {
  2300. kExtensions[i].init(hs);
  2301. }
  2302. }
  2303. hs->extensions.received = 0;
  2304. hs->custom_extensions.received = 0;
  2305. CBS extensions;
  2306. CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
  2307. while (CBS_len(&extensions) != 0) {
  2308. uint16_t type;
  2309. CBS extension;
  2310. // Decode the next extension.
  2311. if (!CBS_get_u16(&extensions, &type) ||
  2312. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  2313. *out_alert = SSL_AD_DECODE_ERROR;
  2314. return 0;
  2315. }
  2316. // RFC 5746 made the existence of extensions in SSL 3.0 somewhat
  2317. // ambiguous. Ignore all but the renegotiation_info extension.
  2318. if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
  2319. continue;
  2320. }
  2321. unsigned ext_index;
  2322. const struct tls_extension *const ext =
  2323. tls_extension_find(&ext_index, type);
  2324. if (ext == NULL) {
  2325. if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
  2326. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
  2327. return 0;
  2328. }
  2329. continue;
  2330. }
  2331. hs->extensions.received |= (1u << ext_index);
  2332. uint8_t alert = SSL_AD_DECODE_ERROR;
  2333. if (!ext->parse_clienthello(hs, &alert, &extension)) {
  2334. *out_alert = alert;
  2335. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
  2336. ERR_add_error_dataf("extension %u", (unsigned)type);
  2337. return 0;
  2338. }
  2339. }
  2340. for (size_t i = 0; i < kNumExtensions; i++) {
  2341. if (hs->extensions.received & (1u << i)) {
  2342. continue;
  2343. }
  2344. CBS *contents = NULL, fake_contents;
  2345. static const uint8_t kFakeRenegotiateExtension[] = {0};
  2346. if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
  2347. ssl_client_cipher_list_contains_cipher(client_hello,
  2348. SSL3_CK_SCSV & 0xffff)) {
  2349. // The renegotiation SCSV was received so pretend that we received a
  2350. // renegotiation extension.
  2351. CBS_init(&fake_contents, kFakeRenegotiateExtension,
  2352. sizeof(kFakeRenegotiateExtension));
  2353. contents = &fake_contents;
  2354. hs->extensions.received |= (1u << i);
  2355. }
  2356. // Extension wasn't observed so call the callback with a NULL
  2357. // parameter.
  2358. uint8_t alert = SSL_AD_DECODE_ERROR;
  2359. if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
  2360. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
  2361. ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
  2362. *out_alert = alert;
  2363. return 0;
  2364. }
  2365. }
  2366. return 1;
  2367. }
  2368. int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
  2369. const SSL_CLIENT_HELLO *client_hello) {
  2370. SSL *const ssl = hs->ssl;
  2371. int alert = SSL_AD_DECODE_ERROR;
  2372. if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
  2373. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  2374. return 0;
  2375. }
  2376. if (ssl_check_clienthello_tlsext(hs) <= 0) {
  2377. OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
  2378. return 0;
  2379. }
  2380. return 1;
  2381. }
  2382. static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
  2383. int *out_alert) {
  2384. SSL *const ssl = hs->ssl;
  2385. // Before TLS 1.3, ServerHello extensions blocks may be omitted if empty.
  2386. if (CBS_len(cbs) == 0 && ssl_protocol_version(ssl) < TLS1_3_VERSION) {
  2387. return 1;
  2388. }
  2389. // Decode the extensions block and check it is valid.
  2390. CBS extensions;
  2391. if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
  2392. !tls1_check_duplicate_extensions(&extensions)) {
  2393. *out_alert = SSL_AD_DECODE_ERROR;
  2394. return 0;
  2395. }
  2396. uint32_t received = 0;
  2397. while (CBS_len(&extensions) != 0) {
  2398. uint16_t type;
  2399. CBS extension;
  2400. // Decode the next extension.
  2401. if (!CBS_get_u16(&extensions, &type) ||
  2402. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  2403. *out_alert = SSL_AD_DECODE_ERROR;
  2404. return 0;
  2405. }
  2406. unsigned ext_index;
  2407. const struct tls_extension *const ext =
  2408. tls_extension_find(&ext_index, type);
  2409. if (ext == NULL) {
  2410. hs->received_custom_extension = true;
  2411. if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
  2412. return 0;
  2413. }
  2414. continue;
  2415. }
  2416. static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
  2417. "too many bits");
  2418. if (!(hs->extensions.sent & (1u << ext_index)) &&
  2419. type != TLSEXT_TYPE_renegotiate) {
  2420. // If the extension was never sent then it is illegal, except for the
  2421. // renegotiation extension which, in SSL 3.0, is signaled via SCSV.
  2422. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
  2423. ERR_add_error_dataf("extension :%u", (unsigned)type);
  2424. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  2425. return 0;
  2426. }
  2427. received |= (1u << ext_index);
  2428. uint8_t alert = SSL_AD_DECODE_ERROR;
  2429. if (!ext->parse_serverhello(hs, &alert, &extension)) {
  2430. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
  2431. ERR_add_error_dataf("extension %u", (unsigned)type);
  2432. *out_alert = alert;
  2433. return 0;
  2434. }
  2435. }
  2436. for (size_t i = 0; i < kNumExtensions; i++) {
  2437. if (!(received & (1u << i))) {
  2438. // Extension wasn't observed so call the callback with a NULL
  2439. // parameter.
  2440. uint8_t alert = SSL_AD_DECODE_ERROR;
  2441. if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
  2442. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
  2443. ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
  2444. *out_alert = alert;
  2445. return 0;
  2446. }
  2447. }
  2448. }
  2449. return 1;
  2450. }
  2451. static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
  2452. SSL *const ssl = hs->ssl;
  2453. int ret = SSL_TLSEXT_ERR_NOACK;
  2454. int al = SSL_AD_UNRECOGNIZED_NAME;
  2455. if (ssl->ctx->tlsext_servername_callback != 0) {
  2456. ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
  2457. ssl->ctx->tlsext_servername_arg);
  2458. } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
  2459. ret = ssl->session_ctx->tlsext_servername_callback(
  2460. ssl, &al, ssl->session_ctx->tlsext_servername_arg);
  2461. }
  2462. switch (ret) {
  2463. case SSL_TLSEXT_ERR_ALERT_FATAL:
  2464. ssl_send_alert(ssl, SSL3_AL_FATAL, al);
  2465. return -1;
  2466. case SSL_TLSEXT_ERR_NOACK:
  2467. hs->should_ack_sni = false;
  2468. return 1;
  2469. default:
  2470. return 1;
  2471. }
  2472. }
  2473. int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
  2474. SSL *const ssl = hs->ssl;
  2475. int alert = SSL_AD_DECODE_ERROR;
  2476. if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
  2477. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  2478. return 0;
  2479. }
  2480. return 1;
  2481. }
  2482. static enum ssl_ticket_aead_result_t decrypt_ticket_with_cipher_ctx(
  2483. uint8_t **out, size_t *out_len, EVP_CIPHER_CTX *cipher_ctx,
  2484. HMAC_CTX *hmac_ctx, const uint8_t *ticket, size_t ticket_len) {
  2485. size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx);
  2486. // Check the MAC at the end of the ticket.
  2487. uint8_t mac[EVP_MAX_MD_SIZE];
  2488. size_t mac_len = HMAC_size(hmac_ctx);
  2489. if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
  2490. // The ticket must be large enough for key name, IV, data, and MAC.
  2491. return ssl_ticket_aead_ignore_ticket;
  2492. }
  2493. HMAC_Update(hmac_ctx, ticket, ticket_len - mac_len);
  2494. HMAC_Final(hmac_ctx, mac, NULL);
  2495. int mac_ok =
  2496. CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
  2497. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  2498. mac_ok = 1;
  2499. #endif
  2500. if (!mac_ok) {
  2501. return ssl_ticket_aead_ignore_ticket;
  2502. }
  2503. // Decrypt the session data.
  2504. const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
  2505. size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
  2506. mac_len;
  2507. UniquePtr<uint8_t> plaintext((uint8_t *)OPENSSL_malloc(ciphertext_len));
  2508. if (!plaintext) {
  2509. return ssl_ticket_aead_error;
  2510. }
  2511. size_t plaintext_len;
  2512. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  2513. OPENSSL_memcpy(plaintext.get(), ciphertext, ciphertext_len);
  2514. plaintext_len = ciphertext_len;
  2515. #else
  2516. if (ciphertext_len >= INT_MAX) {
  2517. return ssl_ticket_aead_ignore_ticket;
  2518. }
  2519. int len1, len2;
  2520. if (!EVP_DecryptUpdate(cipher_ctx, plaintext.get(), &len1, ciphertext,
  2521. (int)ciphertext_len) ||
  2522. !EVP_DecryptFinal_ex(cipher_ctx, plaintext.get() + len1, &len2)) {
  2523. ERR_clear_error();
  2524. return ssl_ticket_aead_ignore_ticket;
  2525. }
  2526. plaintext_len = (size_t)(len1) + len2;
  2527. #endif
  2528. *out = plaintext.release();
  2529. *out_len = plaintext_len;
  2530. return ssl_ticket_aead_success;
  2531. }
  2532. static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_cb(
  2533. SSL *ssl, uint8_t **out, size_t *out_len, bool *out_renew_ticket,
  2534. const uint8_t *ticket, size_t ticket_len) {
  2535. assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
  2536. ScopedEVP_CIPHER_CTX cipher_ctx;
  2537. ScopedHMAC_CTX hmac_ctx;
  2538. const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
  2539. int cb_ret = ssl->session_ctx->tlsext_ticket_key_cb(
  2540. ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, cipher_ctx.get(),
  2541. hmac_ctx.get(), 0 /* decrypt */);
  2542. if (cb_ret < 0) {
  2543. return ssl_ticket_aead_error;
  2544. } else if (cb_ret == 0) {
  2545. return ssl_ticket_aead_ignore_ticket;
  2546. } else if (cb_ret == 2) {
  2547. *out_renew_ticket = true;
  2548. } else {
  2549. assert(cb_ret == 1);
  2550. }
  2551. return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
  2552. hmac_ctx.get(), ticket, ticket_len);
  2553. }
  2554. static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_ticket_keys(
  2555. SSL *ssl, uint8_t **out, size_t *out_len, const uint8_t *ticket,
  2556. size_t ticket_len) {
  2557. assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
  2558. SSL_CTX *ctx = ssl->session_ctx;
  2559. // Rotate the ticket key if necessary.
  2560. if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
  2561. return ssl_ticket_aead_error;
  2562. }
  2563. // Pick the matching ticket key and decrypt.
  2564. ScopedEVP_CIPHER_CTX cipher_ctx;
  2565. ScopedHMAC_CTX hmac_ctx;
  2566. {
  2567. MutexReadLock lock(&ctx->lock);
  2568. const tlsext_ticket_key *key;
  2569. if (ctx->tlsext_ticket_key_current &&
  2570. !OPENSSL_memcmp(ctx->tlsext_ticket_key_current->name, ticket,
  2571. SSL_TICKET_KEY_NAME_LEN)) {
  2572. key = ctx->tlsext_ticket_key_current;
  2573. } else if (ctx->tlsext_ticket_key_prev &&
  2574. !OPENSSL_memcmp(ctx->tlsext_ticket_key_prev->name, ticket,
  2575. SSL_TICKET_KEY_NAME_LEN)) {
  2576. key = ctx->tlsext_ticket_key_prev;
  2577. } else {
  2578. return ssl_ticket_aead_ignore_ticket;
  2579. }
  2580. const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
  2581. if (!HMAC_Init_ex(hmac_ctx.get(), key->hmac_key, sizeof(key->hmac_key),
  2582. tlsext_tick_md(), NULL) ||
  2583. !EVP_DecryptInit_ex(cipher_ctx.get(), EVP_aes_128_cbc(), NULL,
  2584. key->aes_key, iv)) {
  2585. return ssl_ticket_aead_error;
  2586. }
  2587. }
  2588. return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
  2589. hmac_ctx.get(), ticket, ticket_len);
  2590. }
  2591. static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
  2592. SSL *ssl, uint8_t **out, size_t *out_len, bool *out_renew_ticket,
  2593. const uint8_t *ticket, size_t ticket_len) {
  2594. uint8_t *plaintext = (uint8_t *)OPENSSL_malloc(ticket_len);
  2595. if (plaintext == NULL) {
  2596. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  2597. return ssl_ticket_aead_error;
  2598. }
  2599. size_t plaintext_len;
  2600. const enum ssl_ticket_aead_result_t result =
  2601. ssl->session_ctx->ticket_aead_method->open(
  2602. ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
  2603. if (result == ssl_ticket_aead_success) {
  2604. *out = plaintext;
  2605. plaintext = NULL;
  2606. *out_len = plaintext_len;
  2607. }
  2608. OPENSSL_free(plaintext);
  2609. return result;
  2610. }
  2611. enum ssl_ticket_aead_result_t ssl_process_ticket(
  2612. SSL *ssl, UniquePtr<SSL_SESSION> *out_session, bool *out_renew_ticket,
  2613. const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
  2614. size_t session_id_len) {
  2615. *out_renew_ticket = false;
  2616. out_session->reset();
  2617. if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
  2618. session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  2619. return ssl_ticket_aead_ignore_ticket;
  2620. }
  2621. uint8_t *plaintext = NULL;
  2622. size_t plaintext_len;
  2623. enum ssl_ticket_aead_result_t result;
  2624. if (ssl->session_ctx->ticket_aead_method != NULL) {
  2625. result = ssl_decrypt_ticket_with_method(
  2626. ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
  2627. } else {
  2628. // Ensure there is room for the key name and the largest IV
  2629. // |tlsext_ticket_key_cb| may try to consume. The real limit may be lower,
  2630. // but the maximum IV length should be well under the minimum size for the
  2631. // session material and HMAC.
  2632. if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
  2633. return ssl_ticket_aead_ignore_ticket;
  2634. }
  2635. if (ssl->session_ctx->tlsext_ticket_key_cb != NULL) {
  2636. result = ssl_decrypt_ticket_with_cb(ssl, &plaintext, &plaintext_len,
  2637. out_renew_ticket, ticket, ticket_len);
  2638. } else {
  2639. result = ssl_decrypt_ticket_with_ticket_keys(
  2640. ssl, &plaintext, &plaintext_len, ticket, ticket_len);
  2641. }
  2642. }
  2643. if (result != ssl_ticket_aead_success) {
  2644. return result;
  2645. }
  2646. // Decode the session.
  2647. UniquePtr<SSL_SESSION> session(
  2648. SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx));
  2649. OPENSSL_free(plaintext);
  2650. if (!session) {
  2651. ERR_clear_error(); // Don't leave an error on the queue.
  2652. return ssl_ticket_aead_ignore_ticket;
  2653. }
  2654. // Copy the client's session ID into the new session, to denote the ticket has
  2655. // been accepted.
  2656. OPENSSL_memcpy(session->session_id, session_id, session_id_len);
  2657. session->session_id_length = session_id_len;
  2658. *out_session = std::move(session);
  2659. return ssl_ticket_aead_success;
  2660. }
  2661. int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
  2662. // Extension ignored for inappropriate versions
  2663. if (ssl_protocol_version(hs->ssl) < TLS1_2_VERSION) {
  2664. return 1;
  2665. }
  2666. return parse_u16_array(in_sigalgs, &hs->peer_sigalgs);
  2667. }
  2668. int tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
  2669. switch (EVP_PKEY_id(pkey)) {
  2670. case EVP_PKEY_RSA:
  2671. *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
  2672. return 1;
  2673. case EVP_PKEY_EC:
  2674. *out = SSL_SIGN_ECDSA_SHA1;
  2675. return 1;
  2676. default:
  2677. return 0;
  2678. }
  2679. }
  2680. int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
  2681. SSL *const ssl = hs->ssl;
  2682. CERT *cert = ssl->cert;
  2683. // Before TLS 1.2, the signature algorithm isn't negotiated as part of the
  2684. // handshake.
  2685. if (ssl_protocol_version(ssl) < TLS1_2_VERSION) {
  2686. if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey.get())) {
  2687. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
  2688. return 0;
  2689. }
  2690. return 1;
  2691. }
  2692. Span<const uint16_t> sigalgs = kSignSignatureAlgorithms;
  2693. if (cert->sigalgs != nullptr) {
  2694. sigalgs = MakeConstSpan(cert->sigalgs, cert->num_sigalgs);
  2695. }
  2696. Span<const uint16_t> peer_sigalgs = hs->peer_sigalgs;
  2697. if (peer_sigalgs.empty() && ssl_protocol_version(ssl) < TLS1_3_VERSION) {
  2698. // If the client didn't specify any signature_algorithms extension then
  2699. // we can assume that it supports SHA1. See
  2700. // http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
  2701. static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
  2702. SSL_SIGN_ECDSA_SHA1};
  2703. peer_sigalgs = kDefaultPeerAlgorithms;
  2704. }
  2705. for (uint16_t sigalg : sigalgs) {
  2706. // SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
  2707. // negotiated.
  2708. if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
  2709. !ssl_private_key_supports_signature_algorithm(hs, sigalg)) {
  2710. continue;
  2711. }
  2712. for (uint16_t peer_sigalg : peer_sigalgs) {
  2713. if (sigalg == peer_sigalg) {
  2714. *out = sigalg;
  2715. return 1;
  2716. }
  2717. }
  2718. }
  2719. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
  2720. return 0;
  2721. }
  2722. int tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) {
  2723. SSL *const ssl = hs->ssl;
  2724. // A Channel ID handshake message is structured to contain multiple
  2725. // extensions, but the only one that can be present is Channel ID.
  2726. uint16_t extension_type;
  2727. CBS channel_id = msg.body, extension;
  2728. if (!CBS_get_u16(&channel_id, &extension_type) ||
  2729. !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
  2730. CBS_len(&channel_id) != 0 ||
  2731. extension_type != TLSEXT_TYPE_channel_id ||
  2732. CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
  2733. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  2734. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  2735. return 0;
  2736. }
  2737. UniquePtr<EC_GROUP> p256(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  2738. if (!p256) {
  2739. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
  2740. return 0;
  2741. }
  2742. UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
  2743. UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
  2744. if (!sig || !x || !y) {
  2745. return 0;
  2746. }
  2747. const uint8_t *p = CBS_data(&extension);
  2748. if (BN_bin2bn(p + 0, 32, x.get()) == NULL ||
  2749. BN_bin2bn(p + 32, 32, y.get()) == NULL ||
  2750. BN_bin2bn(p + 64, 32, sig->r) == NULL ||
  2751. BN_bin2bn(p + 96, 32, sig->s) == NULL) {
  2752. return 0;
  2753. }
  2754. UniquePtr<EC_KEY> key(EC_KEY_new());
  2755. UniquePtr<EC_POINT> point(EC_POINT_new(p256.get()));
  2756. if (!key || !point ||
  2757. !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(),
  2758. y.get(), nullptr) ||
  2759. !EC_KEY_set_group(key.get(), p256.get()) ||
  2760. !EC_KEY_set_public_key(key.get(), point.get())) {
  2761. return 0;
  2762. }
  2763. uint8_t digest[EVP_MAX_MD_SIZE];
  2764. size_t digest_len;
  2765. if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
  2766. return 0;
  2767. }
  2768. int sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get());
  2769. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  2770. sig_ok = 1;
  2771. #endif
  2772. if (!sig_ok) {
  2773. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
  2774. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  2775. ssl->s3->tlsext_channel_id_valid = false;
  2776. return 0;
  2777. }
  2778. OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
  2779. return 1;
  2780. }
  2781. bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
  2782. SSL *const ssl = hs->ssl;
  2783. uint8_t digest[EVP_MAX_MD_SIZE];
  2784. size_t digest_len;
  2785. if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
  2786. return false;
  2787. }
  2788. EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
  2789. if (ec_key == nullptr) {
  2790. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2791. return false;
  2792. }
  2793. UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
  2794. if (!x || !y ||
  2795. !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
  2796. EC_KEY_get0_public_key(ec_key),
  2797. x.get(), y.get(), nullptr)) {
  2798. return false;
  2799. }
  2800. UniquePtr<ECDSA_SIG> sig(ECDSA_do_sign(digest, digest_len, ec_key));
  2801. if (!sig) {
  2802. return false;
  2803. }
  2804. CBB child;
  2805. if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
  2806. !CBB_add_u16_length_prefixed(cbb, &child) ||
  2807. !BN_bn2cbb_padded(&child, 32, x.get()) ||
  2808. !BN_bn2cbb_padded(&child, 32, y.get()) ||
  2809. !BN_bn2cbb_padded(&child, 32, sig->r) ||
  2810. !BN_bn2cbb_padded(&child, 32, sig->s) ||
  2811. !CBB_flush(cbb)) {
  2812. return false;
  2813. }
  2814. return true;
  2815. }
  2816. int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
  2817. SSL *const ssl = hs->ssl;
  2818. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  2819. Array<uint8_t> msg;
  2820. if (!tls13_get_cert_verify_signature_input(hs, &msg,
  2821. ssl_cert_verify_channel_id)) {
  2822. return 0;
  2823. }
  2824. SHA256(msg.data(), msg.size(), out);
  2825. *out_len = SHA256_DIGEST_LENGTH;
  2826. return 1;
  2827. }
  2828. SHA256_CTX ctx;
  2829. SHA256_Init(&ctx);
  2830. static const char kClientIDMagic[] = "TLS Channel ID signature";
  2831. SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
  2832. if (ssl->session != NULL) {
  2833. static const char kResumptionMagic[] = "Resumption";
  2834. SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
  2835. if (ssl->session->original_handshake_hash_len == 0) {
  2836. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2837. return 0;
  2838. }
  2839. SHA256_Update(&ctx, ssl->session->original_handshake_hash,
  2840. ssl->session->original_handshake_hash_len);
  2841. }
  2842. uint8_t hs_hash[EVP_MAX_MD_SIZE];
  2843. size_t hs_hash_len;
  2844. if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) {
  2845. return 0;
  2846. }
  2847. SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
  2848. SHA256_Final(out, &ctx);
  2849. *out_len = SHA256_DIGEST_LENGTH;
  2850. return 1;
  2851. }
  2852. // tls1_record_handshake_hashes_for_channel_id records the current handshake
  2853. // hashes in |hs->new_session| so that Channel ID resumptions can sign that
  2854. // data.
  2855. int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
  2856. SSL *const ssl = hs->ssl;
  2857. // This function should never be called for a resumed session because the
  2858. // handshake hashes that we wish to record are for the original, full
  2859. // handshake.
  2860. if (ssl->session != NULL) {
  2861. return 0;
  2862. }
  2863. static_assert(
  2864. sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
  2865. "original_handshake_hash is too small");
  2866. size_t digest_len;
  2867. if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash,
  2868. &digest_len)) {
  2869. return 0;
  2870. }
  2871. static_assert(EVP_MAX_MD_SIZE <= 0xff,
  2872. "EVP_MAX_MD_SIZE does not fit in uint8_t");
  2873. hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
  2874. return 1;
  2875. }
  2876. int ssl_do_channel_id_callback(SSL *ssl) {
  2877. if (ssl->tlsext_channel_id_private != NULL ||
  2878. ssl->ctx->channel_id_cb == NULL) {
  2879. return 1;
  2880. }
  2881. EVP_PKEY *key = NULL;
  2882. ssl->ctx->channel_id_cb(ssl, &key);
  2883. if (key == NULL) {
  2884. // The caller should try again later.
  2885. return 1;
  2886. }
  2887. int ret = SSL_set1_tls_channel_id(ssl, key);
  2888. EVP_PKEY_free(key);
  2889. return ret;
  2890. }
  2891. int ssl_is_sct_list_valid(const CBS *contents) {
  2892. // Shallow parse the SCT list for sanity. By the RFC
  2893. // (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
  2894. // of the SCTs may be empty.
  2895. CBS copy = *contents;
  2896. CBS sct_list;
  2897. if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
  2898. CBS_len(&copy) != 0 ||
  2899. CBS_len(&sct_list) == 0) {
  2900. return 0;
  2901. }
  2902. while (CBS_len(&sct_list) > 0) {
  2903. CBS sct;
  2904. if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
  2905. CBS_len(&sct) == 0) {
  2906. return 0;
  2907. }
  2908. }
  2909. return 1;
  2910. }
  2911. } // namespace bssl
  2912. using namespace bssl;
  2913. int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
  2914. uint16_t extension_type,
  2915. const uint8_t **out_data,
  2916. size_t *out_len) {
  2917. CBS cbs;
  2918. if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
  2919. return 0;
  2920. }
  2921. *out_data = CBS_data(&cbs);
  2922. *out_len = CBS_len(&cbs);
  2923. return 1;
  2924. }
  2925. void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
  2926. ctx->ed25519_enabled = !!enabled;
  2927. }
  2928. int SSL_extension_supported(unsigned extension_value) {
  2929. uint32_t index;
  2930. return extension_value == TLSEXT_TYPE_padding ||
  2931. tls_extension_find(&index, extension_value) != NULL;
  2932. }