t1_lib.cc 112 KB

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