t1_lib.cc 121 KB

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