t1_lib.c 103 KB

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