t1_lib.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781
  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 <stdio.h>
  112. #include <stdlib.h>
  113. #include <string.h>
  114. #include <openssl/bytestring.h>
  115. #include <openssl/digest.h>
  116. #include <openssl/err.h>
  117. #include <openssl/evp.h>
  118. #include <openssl/hmac.h>
  119. #include <openssl/mem.h>
  120. #include <openssl/obj.h>
  121. #include <openssl/rand.h>
  122. #include <openssl/type_check.h>
  123. #include "internal.h"
  124. static int ssl_check_clienthello_tlsext(SSL *ssl);
  125. static int ssl_check_serverhello_tlsext(SSL *ssl);
  126. static int compare_uint16_t(const void *p1, const void *p2) {
  127. uint16_t u1 = *((const uint16_t *)p1);
  128. uint16_t u2 = *((const uint16_t *)p2);
  129. if (u1 < u2) {
  130. return -1;
  131. } else if (u1 > u2) {
  132. return 1;
  133. } else {
  134. return 0;
  135. }
  136. }
  137. /* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
  138. * more than one extension of the same type in a ClientHello or ServerHello.
  139. * This function does an initial scan over the extensions block to filter those
  140. * out. */
  141. static int tls1_check_duplicate_extensions(const CBS *cbs) {
  142. CBS extensions = *cbs;
  143. size_t num_extensions = 0, i = 0;
  144. uint16_t *extension_types = NULL;
  145. int ret = 0;
  146. /* First pass: count the extensions. */
  147. while (CBS_len(&extensions) > 0) {
  148. uint16_t type;
  149. CBS extension;
  150. if (!CBS_get_u16(&extensions, &type) ||
  151. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  152. goto done;
  153. }
  154. num_extensions++;
  155. }
  156. if (num_extensions == 0) {
  157. return 1;
  158. }
  159. extension_types = OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
  160. if (extension_types == NULL) {
  161. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  162. goto done;
  163. }
  164. /* Second pass: gather the extension types. */
  165. extensions = *cbs;
  166. for (i = 0; i < num_extensions; i++) {
  167. CBS extension;
  168. if (!CBS_get_u16(&extensions, &extension_types[i]) ||
  169. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  170. /* This should not happen. */
  171. goto done;
  172. }
  173. }
  174. assert(CBS_len(&extensions) == 0);
  175. /* Sort the extensions and make sure there are no duplicates. */
  176. qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
  177. for (i = 1; i < num_extensions; i++) {
  178. if (extension_types[i - 1] == extension_types[i]) {
  179. goto done;
  180. }
  181. }
  182. ret = 1;
  183. done:
  184. OPENSSL_free(extension_types);
  185. return ret;
  186. }
  187. char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
  188. CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
  189. CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
  190. if (/* Skip client version. */
  191. !CBS_skip(&client_hello, 2) ||
  192. /* Skip client nonce. */
  193. !CBS_skip(&client_hello, 32) ||
  194. /* Extract session_id. */
  195. !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
  196. return 0;
  197. }
  198. ctx->session_id = CBS_data(&session_id);
  199. ctx->session_id_len = CBS_len(&session_id);
  200. /* Skip past DTLS cookie */
  201. if (SSL_IS_DTLS(ctx->ssl)) {
  202. CBS cookie;
  203. if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
  204. return 0;
  205. }
  206. }
  207. /* Extract cipher_suites. */
  208. if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
  209. CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
  210. return 0;
  211. }
  212. ctx->cipher_suites = CBS_data(&cipher_suites);
  213. ctx->cipher_suites_len = CBS_len(&cipher_suites);
  214. /* Extract compression_methods. */
  215. if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
  216. CBS_len(&compression_methods) < 1) {
  217. return 0;
  218. }
  219. ctx->compression_methods = CBS_data(&compression_methods);
  220. ctx->compression_methods_len = CBS_len(&compression_methods);
  221. /* If the ClientHello ends here then it's valid, but doesn't have any
  222. * extensions. (E.g. SSLv3.) */
  223. if (CBS_len(&client_hello) == 0) {
  224. ctx->extensions = NULL;
  225. ctx->extensions_len = 0;
  226. return 1;
  227. }
  228. /* Extract extensions and check it is valid. */
  229. if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
  230. !tls1_check_duplicate_extensions(&extensions) ||
  231. CBS_len(&client_hello) != 0) {
  232. return 0;
  233. }
  234. ctx->extensions = CBS_data(&extensions);
  235. ctx->extensions_len = CBS_len(&extensions);
  236. return 1;
  237. }
  238. int SSL_early_callback_ctx_extension_get(
  239. const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
  240. const uint8_t **out_data, size_t *out_len) {
  241. CBS extensions;
  242. CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
  243. while (CBS_len(&extensions) != 0) {
  244. uint16_t type;
  245. CBS extension;
  246. /* Decode the next extension. */
  247. if (!CBS_get_u16(&extensions, &type) ||
  248. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  249. return 0;
  250. }
  251. if (type == extension_type) {
  252. *out_data = CBS_data(&extension);
  253. *out_len = CBS_len(&extension);
  254. return 1;
  255. }
  256. }
  257. return 0;
  258. }
  259. static const uint16_t eccurves_default[] = {
  260. SSL_CURVE_X25519,
  261. SSL_CURVE_SECP256R1,
  262. SSL_CURVE_SECP384R1,
  263. #if defined(BORINGSSL_ANDROID_SYSTEM)
  264. SSL_CURVE_SECP521R1,
  265. #endif
  266. };
  267. /* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
  268. * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
  269. * peer's curve list. Otherwise, return the preferred list. */
  270. static void tls1_get_curvelist(SSL *ssl, int get_peer_curves,
  271. const uint16_t **out_curve_ids,
  272. size_t *out_curve_ids_len) {
  273. if (get_peer_curves) {
  274. /* Only clients send a curve list, so this function is only called
  275. * on the server. */
  276. assert(ssl->server);
  277. *out_curve_ids = ssl->s3->tmp.peer_ellipticcurvelist;
  278. *out_curve_ids_len = ssl->s3->tmp.peer_ellipticcurvelist_length;
  279. return;
  280. }
  281. *out_curve_ids = ssl->tlsext_ellipticcurvelist;
  282. *out_curve_ids_len = ssl->tlsext_ellipticcurvelist_length;
  283. if (!*out_curve_ids) {
  284. *out_curve_ids = eccurves_default;
  285. *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
  286. }
  287. }
  288. int tls1_get_shared_curve(SSL *ssl, uint16_t *out_curve_id) {
  289. const uint16_t *curves, *peer_curves, *pref, *supp;
  290. size_t curves_len, peer_curves_len, pref_len, supp_len, i, j;
  291. /* Can't do anything on client side */
  292. if (ssl->server == 0) {
  293. return 0;
  294. }
  295. tls1_get_curvelist(ssl, 0 /* local curves */, &curves, &curves_len);
  296. tls1_get_curvelist(ssl, 1 /* peer curves */, &peer_curves, &peer_curves_len);
  297. if (peer_curves_len == 0) {
  298. /* Clients are not required to send a supported_curves extension. In this
  299. * case, the server is free to pick any curve it likes. See RFC 4492,
  300. * section 4, paragraph 3.
  301. *
  302. * However, in the interests of compatibility, we will skip ECDH if the
  303. * client didn't send an extension because we can't be sure that they'll
  304. * support our favoured curve. */
  305. return 0;
  306. }
  307. if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
  308. pref = curves;
  309. pref_len = curves_len;
  310. supp = peer_curves;
  311. supp_len = peer_curves_len;
  312. } else {
  313. pref = peer_curves;
  314. pref_len = peer_curves_len;
  315. supp = curves;
  316. supp_len = curves_len;
  317. }
  318. for (i = 0; i < pref_len; i++) {
  319. for (j = 0; j < supp_len; j++) {
  320. if (pref[i] == supp[j]) {
  321. *out_curve_id = pref[i];
  322. return 1;
  323. }
  324. }
  325. }
  326. return 0;
  327. }
  328. int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
  329. const int *curves, size_t ncurves) {
  330. uint16_t *curve_ids;
  331. size_t i;
  332. curve_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
  333. if (curve_ids == NULL) {
  334. return 0;
  335. }
  336. for (i = 0; i < ncurves; i++) {
  337. if (!ssl_nid_to_curve_id(&curve_ids[i], curves[i])) {
  338. OPENSSL_free(curve_ids);
  339. return 0;
  340. }
  341. }
  342. OPENSSL_free(*out_curve_ids);
  343. *out_curve_ids = curve_ids;
  344. *out_curve_ids_len = ncurves;
  345. return 1;
  346. }
  347. /* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
  348. * TLS curve ID and point format, respectively, for |ec|. It returns one on
  349. * success and zero on failure. */
  350. static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
  351. uint8_t *out_comp_id, EC_KEY *ec) {
  352. int nid;
  353. uint16_t id;
  354. const EC_GROUP *grp;
  355. if (ec == NULL) {
  356. return 0;
  357. }
  358. grp = EC_KEY_get0_group(ec);
  359. if (grp == NULL) {
  360. return 0;
  361. }
  362. /* Determine curve ID */
  363. nid = EC_GROUP_get_curve_name(grp);
  364. if (!ssl_nid_to_curve_id(&id, nid)) {
  365. return 0;
  366. }
  367. /* Set the named curve ID. Arbitrary explicit curves are not supported. */
  368. *out_curve_id = id;
  369. if (out_comp_id) {
  370. if (EC_KEY_get0_public_key(ec) == NULL) {
  371. return 0;
  372. }
  373. if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
  374. *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
  375. } else {
  376. *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
  377. }
  378. }
  379. return 1;
  380. }
  381. /* tls1_check_curve_id returns one if |curve_id| is consistent with both our
  382. * and the peer's curve preferences. Note: if called as the client, only our
  383. * preferences are checked; the peer (the server) does not send preferences. */
  384. int tls1_check_curve_id(SSL *ssl, uint16_t curve_id) {
  385. const uint16_t *curves;
  386. size_t curves_len, i, get_peer_curves;
  387. /* Check against our list, then the peer's list. */
  388. for (get_peer_curves = 0; get_peer_curves <= 1; get_peer_curves++) {
  389. if (get_peer_curves && !ssl->server) {
  390. /* Servers do not present a preference list so, if we are a client, only
  391. * check our list. */
  392. continue;
  393. }
  394. tls1_get_curvelist(ssl, get_peer_curves, &curves, &curves_len);
  395. if (get_peer_curves && curves_len == 0) {
  396. /* Clients are not required to send a supported_curves extension. In this
  397. * case, the server is free to pick any curve it likes. See RFC 4492,
  398. * section 4, paragraph 3. */
  399. continue;
  400. }
  401. for (i = 0; i < curves_len; i++) {
  402. if (curves[i] == curve_id) {
  403. break;
  404. }
  405. }
  406. if (i == curves_len) {
  407. return 0;
  408. }
  409. }
  410. return 1;
  411. }
  412. int tls1_check_ec_cert(SSL *ssl, X509 *x) {
  413. int ret = 0;
  414. EVP_PKEY *pkey = X509_get_pubkey(x);
  415. uint16_t curve_id;
  416. uint8_t comp_id;
  417. if (!pkey) {
  418. goto done;
  419. }
  420. EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
  421. if (ec_key == NULL ||
  422. !tls1_curve_params_from_ec_key(&curve_id, &comp_id, ec_key) ||
  423. !tls1_check_curve_id(ssl, curve_id) ||
  424. comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
  425. goto done;
  426. }
  427. ret = 1;
  428. done:
  429. EVP_PKEY_free(pkey);
  430. return ret;
  431. }
  432. /* List of supported signature algorithms and hashes. Should make this
  433. * customisable at some point, for now include everything we support. */
  434. #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
  435. #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
  436. #define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
  437. static const uint8_t tls12_sigalgs[] = {
  438. tlsext_sigalg(TLSEXT_hash_sha512)
  439. tlsext_sigalg(TLSEXT_hash_sha384)
  440. tlsext_sigalg(TLSEXT_hash_sha256)
  441. tlsext_sigalg(TLSEXT_hash_sha1)
  442. };
  443. size_t tls12_get_psigalgs(SSL *ssl, const uint8_t **psigs) {
  444. *psigs = tls12_sigalgs;
  445. return sizeof(tls12_sigalgs);
  446. }
  447. int tls12_check_peer_sigalg(SSL *ssl, const EVP_MD **out_md, int *out_alert,
  448. uint8_t hash, uint8_t signature, EVP_PKEY *pkey) {
  449. const uint8_t *sent_sigs;
  450. size_t sent_sigslen, i;
  451. int sigalg = tls12_get_sigid(pkey->type);
  452. /* Should never happen */
  453. if (sigalg == -1) {
  454. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  455. *out_alert = SSL_AD_INTERNAL_ERROR;
  456. return 0;
  457. }
  458. /* Check key type is consistent with signature */
  459. if (sigalg != signature) {
  460. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
  461. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  462. return 0;
  463. }
  464. /* Check signature matches a type we sent */
  465. sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
  466. for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
  467. if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
  468. break;
  469. }
  470. }
  471. if (i == sent_sigslen) {
  472. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
  473. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  474. return 0;
  475. }
  476. *out_md = tls12_get_hash(hash);
  477. if (*out_md == NULL) {
  478. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST);
  479. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  480. return 0;
  481. }
  482. return 1;
  483. }
  484. /* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
  485. * supported or doesn't appear in supported signature algorithms. Unlike
  486. * ssl_cipher_get_disabled this applies to a specific session and not global
  487. * settings. */
  488. void ssl_set_client_disabled(SSL *ssl) {
  489. CERT *c = ssl->cert;
  490. const uint8_t *sigalgs;
  491. size_t i, sigalgslen;
  492. int have_rsa = 0, have_ecdsa = 0;
  493. c->mask_a = 0;
  494. c->mask_k = 0;
  495. /* Now go through all signature algorithms seeing if we support any for RSA,
  496. * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
  497. sigalgslen = tls12_get_psigalgs(ssl, &sigalgs);
  498. for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
  499. switch (sigalgs[1]) {
  500. case TLSEXT_signature_rsa:
  501. have_rsa = 1;
  502. break;
  503. case TLSEXT_signature_ecdsa:
  504. have_ecdsa = 1;
  505. break;
  506. }
  507. }
  508. /* Disable auth if we don't include any appropriate signature algorithms. */
  509. if (!have_rsa) {
  510. c->mask_a |= SSL_aRSA;
  511. }
  512. if (!have_ecdsa) {
  513. c->mask_a |= SSL_aECDSA;
  514. }
  515. /* with PSK there must be client callback set */
  516. if (!ssl->psk_client_callback) {
  517. c->mask_a |= SSL_aPSK;
  518. c->mask_k |= SSL_kPSK;
  519. }
  520. }
  521. /* tls_extension represents a TLS extension that is handled internally. The
  522. * |init| function is called for each handshake, before any other functions of
  523. * the extension. Then the add and parse callbacks are called as needed.
  524. *
  525. * The parse callbacks receive a |CBS| that contains the contents of the
  526. * extension (i.e. not including the type and length bytes). If an extension is
  527. * not received then the parse callbacks will be called with a NULL CBS so that
  528. * they can do any processing needed to handle the absence of an extension.
  529. *
  530. * The add callbacks receive a |CBB| to which the extension can be appended but
  531. * the function is responsible for appending the type and length bytes too.
  532. *
  533. * All callbacks return one for success and zero for error. If a parse function
  534. * returns zero then a fatal alert with value |*out_alert| will be sent. If
  535. * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
  536. struct tls_extension {
  537. uint16_t value;
  538. void (*init)(SSL *ssl);
  539. int (*add_clienthello)(SSL *ssl, CBB *out);
  540. int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
  541. int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
  542. int (*add_serverhello)(SSL *ssl, CBB *out);
  543. };
  544. /* Server name indication (SNI).
  545. *
  546. * https://tools.ietf.org/html/rfc6066#section-3. */
  547. static void ext_sni_init(SSL *ssl) {
  548. ssl->s3->tmp.should_ack_sni = 0;
  549. }
  550. static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
  551. if (ssl->tlsext_hostname == NULL) {
  552. return 1;
  553. }
  554. CBB contents, server_name_list, name;
  555. if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
  556. !CBB_add_u16_length_prefixed(out, &contents) ||
  557. !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
  558. !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
  559. !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
  560. !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
  561. strlen(ssl->tlsext_hostname)) ||
  562. !CBB_flush(out)) {
  563. return 0;
  564. }
  565. return 1;
  566. }
  567. static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  568. CBS *contents) {
  569. if (contents == NULL) {
  570. return 1;
  571. }
  572. if (CBS_len(contents) != 0) {
  573. return 0;
  574. }
  575. assert(ssl->tlsext_hostname != NULL);
  576. if (!ssl->hit) {
  577. assert(ssl->session->tlsext_hostname == NULL);
  578. ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
  579. if (!ssl->session->tlsext_hostname) {
  580. *out_alert = SSL_AD_INTERNAL_ERROR;
  581. return 0;
  582. }
  583. }
  584. return 1;
  585. }
  586. static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  587. CBS *contents) {
  588. if (contents == NULL) {
  589. return 1;
  590. }
  591. /* The servername extension is treated as follows:
  592. *
  593. * - Only the hostname type is supported with a maximum length of 255.
  594. * - The servername is rejected if too long or if it contains zeros, in
  595. * which case an fatal alert is generated.
  596. * - The servername field is maintained together with the session cache.
  597. * - When a session is resumed, the servername callback is invoked in order
  598. * to allow the application to position itself to the right context.
  599. * - The servername is acknowledged if it is new for a session or when
  600. * it is identical to a previously used for the same session.
  601. * Applications can control the behaviour. They can at any time
  602. * set a 'desirable' servername for a new SSL object. This can be the
  603. * case for example with HTTPS when a Host: header field is received and
  604. * a renegotiation is requested. In this case, a possible servername
  605. * presented in the new client hello is only acknowledged if it matches
  606. * the value of the Host: field.
  607. * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
  608. * if they provide for changing an explicit servername context for the
  609. * session,
  610. * i.e. when the session has been established with a servername extension.
  611. */
  612. CBS server_name_list;
  613. char have_seen_host_name = 0;
  614. if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
  615. CBS_len(&server_name_list) == 0 ||
  616. CBS_len(contents) != 0) {
  617. return 0;
  618. }
  619. /* Decode each ServerName in the extension. */
  620. while (CBS_len(&server_name_list) > 0) {
  621. uint8_t name_type;
  622. CBS host_name;
  623. if (!CBS_get_u8(&server_name_list, &name_type) ||
  624. !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) {
  625. return 0;
  626. }
  627. /* Only host_name is supported. */
  628. if (name_type != TLSEXT_NAMETYPE_host_name) {
  629. continue;
  630. }
  631. if (have_seen_host_name) {
  632. /* The ServerNameList MUST NOT contain more than one name of the same
  633. * name_type. */
  634. return 0;
  635. }
  636. have_seen_host_name = 1;
  637. if (CBS_len(&host_name) == 0 ||
  638. CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
  639. CBS_contains_zero_byte(&host_name)) {
  640. *out_alert = SSL_AD_UNRECOGNIZED_NAME;
  641. return 0;
  642. }
  643. if (!ssl->hit) {
  644. assert(ssl->session->tlsext_hostname == NULL);
  645. if (ssl->session->tlsext_hostname) {
  646. /* This should be impossible. */
  647. return 0;
  648. }
  649. /* Copy the hostname as a string. */
  650. if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
  651. *out_alert = SSL_AD_INTERNAL_ERROR;
  652. return 0;
  653. }
  654. ssl->s3->tmp.should_ack_sni = 1;
  655. }
  656. }
  657. return 1;
  658. }
  659. static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
  660. if (ssl->hit ||
  661. !ssl->s3->tmp.should_ack_sni ||
  662. ssl->session->tlsext_hostname == NULL) {
  663. return 1;
  664. }
  665. if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
  666. !CBB_add_u16(out, 0 /* length */)) {
  667. return 0;
  668. }
  669. return 1;
  670. }
  671. /* Renegotiation indication.
  672. *
  673. * https://tools.ietf.org/html/rfc5746 */
  674. static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
  675. CBB contents, prev_finished;
  676. if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
  677. !CBB_add_u16_length_prefixed(out, &contents) ||
  678. !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
  679. !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
  680. ssl->s3->previous_client_finished_len) ||
  681. !CBB_flush(out)) {
  682. return 0;
  683. }
  684. return 1;
  685. }
  686. static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  687. CBS *contents) {
  688. /* Servers may not switch between omitting the extension and supporting it.
  689. * See RFC 5746, sections 3.5 and 4.2. */
  690. if (ssl->s3->initial_handshake_complete &&
  691. (contents != NULL) != ssl->s3->send_connection_binding) {
  692. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  693. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  694. return 0;
  695. }
  696. if (contents == NULL) {
  697. /* Strictly speaking, if we want to avoid an attack we should *always* see
  698. * RI even on initial ServerHello because the client doesn't see any
  699. * renegotiation during an attack. However this would mean we could not
  700. * connect to any server which doesn't support RI.
  701. *
  702. * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
  703. * practical terms every client sets it so it's just assumed here. */
  704. return 1;
  705. }
  706. const size_t expected_len = ssl->s3->previous_client_finished_len +
  707. ssl->s3->previous_server_finished_len;
  708. /* Check for logic errors */
  709. assert(!expected_len || ssl->s3->previous_client_finished_len);
  710. assert(!expected_len || ssl->s3->previous_server_finished_len);
  711. /* Parse out the extension contents. */
  712. CBS renegotiated_connection;
  713. if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
  714. CBS_len(contents) != 0) {
  715. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
  716. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  717. return 0;
  718. }
  719. /* Check that the extension matches. */
  720. if (CBS_len(&renegotiated_connection) != expected_len) {
  721. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  722. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  723. return 0;
  724. }
  725. const uint8_t *d = CBS_data(&renegotiated_connection);
  726. if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
  727. ssl->s3->previous_client_finished_len)) {
  728. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  729. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  730. return 0;
  731. }
  732. d += ssl->s3->previous_client_finished_len;
  733. if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
  734. ssl->s3->previous_server_finished_len)) {
  735. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  736. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  737. return 0;
  738. }
  739. ssl->s3->send_connection_binding = 1;
  740. return 1;
  741. }
  742. static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  743. CBS *contents) {
  744. /* Renegotiation isn't supported as a server so this function should never be
  745. * called after the initial handshake. */
  746. assert(!ssl->s3->initial_handshake_complete);
  747. CBS fake_contents;
  748. static const uint8_t kFakeExtension[] = {0};
  749. if (contents == NULL) {
  750. if (ssl->s3->send_connection_binding) {
  751. /* The renegotiation SCSV was received so pretend that we received a
  752. * renegotiation extension. */
  753. CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
  754. contents = &fake_contents;
  755. /* We require that the renegotiation extension is at index zero of
  756. * kExtensions. */
  757. ssl->s3->tmp.extensions.received |= (1u << 0);
  758. } else {
  759. return 1;
  760. }
  761. }
  762. CBS renegotiated_connection;
  763. if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
  764. CBS_len(contents) != 0) {
  765. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
  766. return 0;
  767. }
  768. /* Check that the extension matches */
  769. if (!CBS_mem_equal(&renegotiated_connection,
  770. ssl->s3->previous_client_finished,
  771. ssl->s3->previous_client_finished_len)) {
  772. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  773. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  774. return 0;
  775. }
  776. ssl->s3->send_connection_binding = 1;
  777. return 1;
  778. }
  779. static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
  780. CBB contents, prev_finished;
  781. if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
  782. !CBB_add_u16_length_prefixed(out, &contents) ||
  783. !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
  784. !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
  785. ssl->s3->previous_client_finished_len) ||
  786. !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
  787. ssl->s3->previous_server_finished_len) ||
  788. !CBB_flush(out)) {
  789. return 0;
  790. }
  791. return 1;
  792. }
  793. /* Extended Master Secret.
  794. *
  795. * https://tools.ietf.org/html/rfc7627 */
  796. static void ext_ems_init(SSL *ssl) {
  797. ssl->s3->tmp.extended_master_secret = 0;
  798. }
  799. static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
  800. if (ssl->version == SSL3_VERSION) {
  801. return 1;
  802. }
  803. if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
  804. !CBB_add_u16(out, 0 /* length */)) {
  805. return 0;
  806. }
  807. return 1;
  808. }
  809. static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  810. CBS *contents) {
  811. if (contents == NULL) {
  812. return 1;
  813. }
  814. if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
  815. return 0;
  816. }
  817. ssl->s3->tmp.extended_master_secret = 1;
  818. return 1;
  819. }
  820. static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  821. CBS *contents) {
  822. if (ssl->version == SSL3_VERSION || contents == NULL) {
  823. return 1;
  824. }
  825. if (CBS_len(contents) != 0) {
  826. return 0;
  827. }
  828. ssl->s3->tmp.extended_master_secret = 1;
  829. return 1;
  830. }
  831. static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
  832. if (!ssl->s3->tmp.extended_master_secret) {
  833. return 1;
  834. }
  835. if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
  836. !CBB_add_u16(out, 0 /* length */)) {
  837. return 0;
  838. }
  839. return 1;
  840. }
  841. /* Session tickets.
  842. *
  843. * https://tools.ietf.org/html/rfc5077 */
  844. static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
  845. if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
  846. return 1;
  847. }
  848. const uint8_t *ticket_data = NULL;
  849. int ticket_len = 0;
  850. /* Renegotiation does not participate in session resumption. However, still
  851. * advertise the extension to avoid potentially breaking servers which carry
  852. * over the state from the previous handshake, such as OpenSSL servers
  853. * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
  854. if (!ssl->s3->initial_handshake_complete &&
  855. ssl->session != NULL &&
  856. ssl->session->tlsext_tick != NULL) {
  857. ticket_data = ssl->session->tlsext_tick;
  858. ticket_len = ssl->session->tlsext_ticklen;
  859. }
  860. CBB ticket;
  861. if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
  862. !CBB_add_u16_length_prefixed(out, &ticket) ||
  863. !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
  864. !CBB_flush(out)) {
  865. return 0;
  866. }
  867. return 1;
  868. }
  869. static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  870. CBS *contents) {
  871. ssl->tlsext_ticket_expected = 0;
  872. if (contents == NULL) {
  873. return 1;
  874. }
  875. /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
  876. * this function should never be called, even if the server tries to send the
  877. * extension. */
  878. assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
  879. if (CBS_len(contents) != 0) {
  880. return 0;
  881. }
  882. ssl->tlsext_ticket_expected = 1;
  883. return 1;
  884. }
  885. static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  886. CBS *contents) {
  887. /* This function isn't used because the ticket extension from the client is
  888. * handled in ssl_session.c. */
  889. return 1;
  890. }
  891. static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
  892. if (!ssl->tlsext_ticket_expected) {
  893. return 1;
  894. }
  895. /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
  896. * true. */
  897. assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
  898. if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
  899. !CBB_add_u16(out, 0 /* length */)) {
  900. return 0;
  901. }
  902. return 1;
  903. }
  904. /* Signature Algorithms.
  905. *
  906. * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
  907. static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
  908. if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) {
  909. return 1;
  910. }
  911. const uint8_t *sigalgs_data;
  912. const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
  913. CBB contents, sigalgs;
  914. if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
  915. !CBB_add_u16_length_prefixed(out, &contents) ||
  916. !CBB_add_u16_length_prefixed(&contents, &sigalgs) ||
  917. !CBB_add_bytes(&sigalgs, sigalgs_data, sigalgs_len) ||
  918. !CBB_flush(out)) {
  919. return 0;
  920. }
  921. return 1;
  922. }
  923. static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  924. CBS *contents) {
  925. if (contents != NULL) {
  926. /* Servers MUST NOT send this extension. */
  927. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  928. OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER);
  929. return 0;
  930. }
  931. return 1;
  932. }
  933. static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  934. CBS *contents) {
  935. OPENSSL_free(ssl->cert->peer_sigalgs);
  936. ssl->cert->peer_sigalgs = NULL;
  937. ssl->cert->peer_sigalgslen = 0;
  938. if (contents == NULL) {
  939. return 1;
  940. }
  941. CBS supported_signature_algorithms;
  942. if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
  943. CBS_len(contents) != 0 ||
  944. CBS_len(&supported_signature_algorithms) == 0 ||
  945. !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
  946. return 0;
  947. }
  948. return 1;
  949. }
  950. static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) {
  951. /* Servers MUST NOT send this extension. */
  952. return 1;
  953. }
  954. /* OCSP Stapling.
  955. *
  956. * https://tools.ietf.org/html/rfc6066#section-8 */
  957. static void ext_ocsp_init(SSL *ssl) {
  958. ssl->s3->tmp.certificate_status_expected = 0;
  959. ssl->tlsext_status_type = -1;
  960. }
  961. static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
  962. if (!ssl->ocsp_stapling_enabled) {
  963. return 1;
  964. }
  965. CBB contents;
  966. if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
  967. !CBB_add_u16_length_prefixed(out, &contents) ||
  968. !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
  969. !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
  970. !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
  971. !CBB_flush(out)) {
  972. return 0;
  973. }
  974. ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
  975. return 1;
  976. }
  977. static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  978. CBS *contents) {
  979. if (contents == NULL) {
  980. return 1;
  981. }
  982. if (CBS_len(contents) != 0) {
  983. return 0;
  984. }
  985. ssl->s3->tmp.certificate_status_expected = 1;
  986. return 1;
  987. }
  988. static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  989. CBS *contents) {
  990. if (contents == NULL) {
  991. return 1;
  992. }
  993. uint8_t status_type;
  994. if (!CBS_get_u8(contents, &status_type)) {
  995. return 0;
  996. }
  997. /* We cannot decide whether OCSP stapling will occur yet because the correct
  998. * SSL_CTX might not have been selected. */
  999. ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
  1000. return 1;
  1001. }
  1002. static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
  1003. /* The extension shouldn't be sent when resuming sessions. */
  1004. if (ssl->hit ||
  1005. !ssl->s3->tmp.ocsp_stapling_requested ||
  1006. ssl->ctx->ocsp_response_length == 0) {
  1007. return 1;
  1008. }
  1009. ssl->s3->tmp.certificate_status_expected = 1;
  1010. return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
  1011. CBB_add_u16(out, 0 /* length */);
  1012. }
  1013. /* Next protocol negotiation.
  1014. *
  1015. * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
  1016. static void ext_npn_init(SSL *ssl) {
  1017. ssl->s3->next_proto_neg_seen = 0;
  1018. }
  1019. static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
  1020. if (ssl->s3->initial_handshake_complete ||
  1021. ssl->ctx->next_proto_select_cb == NULL ||
  1022. (ssl->options & SSL_OP_DISABLE_NPN) ||
  1023. SSL_IS_DTLS(ssl)) {
  1024. return 1;
  1025. }
  1026. if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
  1027. !CBB_add_u16(out, 0 /* length */)) {
  1028. return 0;
  1029. }
  1030. return 1;
  1031. }
  1032. static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  1033. CBS *contents) {
  1034. if (contents == NULL) {
  1035. return 1;
  1036. }
  1037. /* If any of these are false then we should never have sent the NPN
  1038. * extension in the ClientHello and thus this function should never have been
  1039. * called. */
  1040. assert(!ssl->s3->initial_handshake_complete);
  1041. assert(!SSL_IS_DTLS(ssl));
  1042. assert(ssl->ctx->next_proto_select_cb != NULL);
  1043. assert(!(ssl->options & SSL_OP_DISABLE_NPN));
  1044. if (ssl->s3->alpn_selected != NULL) {
  1045. /* NPN and ALPN may not be negotiated in the same connection. */
  1046. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1047. OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
  1048. return 0;
  1049. }
  1050. const uint8_t *const orig_contents = CBS_data(contents);
  1051. const size_t orig_len = CBS_len(contents);
  1052. while (CBS_len(contents) != 0) {
  1053. CBS proto;
  1054. if (!CBS_get_u8_length_prefixed(contents, &proto) ||
  1055. CBS_len(&proto) == 0) {
  1056. return 0;
  1057. }
  1058. }
  1059. uint8_t *selected;
  1060. uint8_t selected_len;
  1061. if (ssl->ctx->next_proto_select_cb(
  1062. ssl, &selected, &selected_len, orig_contents, orig_len,
  1063. ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
  1064. *out_alert = SSL_AD_INTERNAL_ERROR;
  1065. return 0;
  1066. }
  1067. OPENSSL_free(ssl->s3->next_proto_negotiated);
  1068. ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
  1069. if (ssl->s3->next_proto_negotiated == NULL) {
  1070. *out_alert = SSL_AD_INTERNAL_ERROR;
  1071. return 0;
  1072. }
  1073. ssl->s3->next_proto_negotiated_len = selected_len;
  1074. ssl->s3->next_proto_neg_seen = 1;
  1075. return 1;
  1076. }
  1077. static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  1078. CBS *contents) {
  1079. if (contents != NULL && CBS_len(contents) != 0) {
  1080. return 0;
  1081. }
  1082. if (contents == NULL ||
  1083. ssl->s3->initial_handshake_complete ||
  1084. /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
  1085. * afterwards, parsing the ALPN extension will clear
  1086. * |next_proto_neg_seen|. */
  1087. ssl->s3->alpn_selected != NULL ||
  1088. ssl->ctx->next_protos_advertised_cb == NULL ||
  1089. SSL_IS_DTLS(ssl)) {
  1090. return 1;
  1091. }
  1092. ssl->s3->next_proto_neg_seen = 1;
  1093. return 1;
  1094. }
  1095. static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
  1096. /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
  1097. * parsed. */
  1098. if (!ssl->s3->next_proto_neg_seen) {
  1099. return 1;
  1100. }
  1101. const uint8_t *npa;
  1102. unsigned npa_len;
  1103. if (ssl->ctx->next_protos_advertised_cb(
  1104. ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
  1105. SSL_TLSEXT_ERR_OK) {
  1106. ssl->s3->next_proto_neg_seen = 0;
  1107. return 1;
  1108. }
  1109. CBB contents;
  1110. if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
  1111. !CBB_add_u16_length_prefixed(out, &contents) ||
  1112. !CBB_add_bytes(&contents, npa, npa_len) ||
  1113. !CBB_flush(out)) {
  1114. return 0;
  1115. }
  1116. return 1;
  1117. }
  1118. /* Signed certificate timestamps.
  1119. *
  1120. * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
  1121. static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
  1122. if (!ssl->signed_cert_timestamps_enabled) {
  1123. return 1;
  1124. }
  1125. if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
  1126. !CBB_add_u16(out, 0 /* length */)) {
  1127. return 0;
  1128. }
  1129. return 1;
  1130. }
  1131. static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  1132. CBS *contents) {
  1133. if (contents == NULL) {
  1134. return 1;
  1135. }
  1136. /* If this is false then we should never have sent the SCT extension in the
  1137. * ClientHello and thus this function should never have been called. */
  1138. assert(ssl->signed_cert_timestamps_enabled);
  1139. if (CBS_len(contents) == 0) {
  1140. *out_alert = SSL_AD_DECODE_ERROR;
  1141. return 0;
  1142. }
  1143. /* Session resumption uses the original session information. */
  1144. if (!ssl->hit &&
  1145. !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
  1146. &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
  1147. *out_alert = SSL_AD_INTERNAL_ERROR;
  1148. return 0;
  1149. }
  1150. return 1;
  1151. }
  1152. static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  1153. CBS *contents) {
  1154. return contents == NULL || CBS_len(contents) == 0;
  1155. }
  1156. static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
  1157. /* The extension shouldn't be sent when resuming sessions. */
  1158. if (ssl->hit ||
  1159. ssl->ctx->signed_cert_timestamp_list_length == 0) {
  1160. return 1;
  1161. }
  1162. CBB contents;
  1163. return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
  1164. CBB_add_u16_length_prefixed(out, &contents) &&
  1165. CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
  1166. ssl->ctx->signed_cert_timestamp_list_length) &&
  1167. CBB_flush(out);
  1168. }
  1169. /* Application-level Protocol Negotiation.
  1170. *
  1171. * https://tools.ietf.org/html/rfc7301 */
  1172. static void ext_alpn_init(SSL *ssl) {
  1173. OPENSSL_free(ssl->s3->alpn_selected);
  1174. ssl->s3->alpn_selected = NULL;
  1175. }
  1176. static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
  1177. if (ssl->alpn_client_proto_list == NULL ||
  1178. ssl->s3->initial_handshake_complete) {
  1179. return 1;
  1180. }
  1181. CBB contents, proto_list;
  1182. if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
  1183. !CBB_add_u16_length_prefixed(out, &contents) ||
  1184. !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
  1185. !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
  1186. ssl->alpn_client_proto_list_len) ||
  1187. !CBB_flush(out)) {
  1188. return 0;
  1189. }
  1190. return 1;
  1191. }
  1192. static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  1193. CBS *contents) {
  1194. if (contents == NULL) {
  1195. return 1;
  1196. }
  1197. assert(!ssl->s3->initial_handshake_complete);
  1198. assert(ssl->alpn_client_proto_list != NULL);
  1199. if (ssl->s3->next_proto_neg_seen) {
  1200. /* NPN and ALPN may not be negotiated in the same connection. */
  1201. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1202. OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
  1203. return 0;
  1204. }
  1205. /* The extension data consists of a ProtocolNameList which must have
  1206. * exactly one ProtocolName. Each of these is length-prefixed. */
  1207. CBS protocol_name_list, protocol_name;
  1208. if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
  1209. CBS_len(contents) != 0 ||
  1210. !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
  1211. /* Empty protocol names are forbidden. */
  1212. CBS_len(&protocol_name) == 0 ||
  1213. CBS_len(&protocol_name_list) != 0) {
  1214. return 0;
  1215. }
  1216. if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
  1217. &ssl->s3->alpn_selected_len)) {
  1218. *out_alert = SSL_AD_INTERNAL_ERROR;
  1219. return 0;
  1220. }
  1221. return 1;
  1222. }
  1223. static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  1224. CBS *contents) {
  1225. if (contents == NULL) {
  1226. return 1;
  1227. }
  1228. if (ssl->ctx->alpn_select_cb == NULL ||
  1229. ssl->s3->initial_handshake_complete) {
  1230. return 1;
  1231. }
  1232. /* ALPN takes precedence over NPN. */
  1233. ssl->s3->next_proto_neg_seen = 0;
  1234. CBS protocol_name_list;
  1235. if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
  1236. CBS_len(contents) != 0 ||
  1237. CBS_len(&protocol_name_list) < 2) {
  1238. return 0;
  1239. }
  1240. /* Validate the protocol list. */
  1241. CBS protocol_name_list_copy = protocol_name_list;
  1242. while (CBS_len(&protocol_name_list_copy) > 0) {
  1243. CBS protocol_name;
  1244. if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
  1245. /* Empty protocol names are forbidden. */
  1246. CBS_len(&protocol_name) == 0) {
  1247. return 0;
  1248. }
  1249. }
  1250. const uint8_t *selected;
  1251. uint8_t selected_len;
  1252. if (ssl->ctx->alpn_select_cb(
  1253. ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
  1254. CBS_len(&protocol_name_list),
  1255. ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
  1256. OPENSSL_free(ssl->s3->alpn_selected);
  1257. ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
  1258. if (ssl->s3->alpn_selected == NULL) {
  1259. *out_alert = SSL_AD_INTERNAL_ERROR;
  1260. return 0;
  1261. }
  1262. ssl->s3->alpn_selected_len = selected_len;
  1263. }
  1264. return 1;
  1265. }
  1266. static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
  1267. if (ssl->s3->alpn_selected == NULL) {
  1268. return 1;
  1269. }
  1270. CBB contents, proto_list, proto;
  1271. if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
  1272. !CBB_add_u16_length_prefixed(out, &contents) ||
  1273. !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
  1274. !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
  1275. !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
  1276. ssl->s3->alpn_selected_len) ||
  1277. !CBB_flush(out)) {
  1278. return 0;
  1279. }
  1280. return 1;
  1281. }
  1282. /* Channel ID.
  1283. *
  1284. * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
  1285. static void ext_channel_id_init(SSL *ssl) {
  1286. ssl->s3->tlsext_channel_id_valid = 0;
  1287. }
  1288. static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
  1289. if (!ssl->tlsext_channel_id_enabled ||
  1290. SSL_IS_DTLS(ssl)) {
  1291. return 1;
  1292. }
  1293. if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
  1294. !CBB_add_u16(out, 0 /* length */)) {
  1295. return 0;
  1296. }
  1297. return 1;
  1298. }
  1299. static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  1300. CBS *contents) {
  1301. if (contents == NULL) {
  1302. return 1;
  1303. }
  1304. assert(!SSL_IS_DTLS(ssl));
  1305. assert(ssl->tlsext_channel_id_enabled);
  1306. if (CBS_len(contents) != 0) {
  1307. return 0;
  1308. }
  1309. ssl->s3->tlsext_channel_id_valid = 1;
  1310. return 1;
  1311. }
  1312. static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  1313. CBS *contents) {
  1314. if (contents == NULL ||
  1315. !ssl->tlsext_channel_id_enabled ||
  1316. SSL_IS_DTLS(ssl)) {
  1317. return 1;
  1318. }
  1319. if (CBS_len(contents) != 0) {
  1320. return 0;
  1321. }
  1322. ssl->s3->tlsext_channel_id_valid = 1;
  1323. return 1;
  1324. }
  1325. static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
  1326. if (!ssl->s3->tlsext_channel_id_valid) {
  1327. return 1;
  1328. }
  1329. if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
  1330. !CBB_add_u16(out, 0 /* length */)) {
  1331. return 0;
  1332. }
  1333. return 1;
  1334. }
  1335. /* Secure Real-time Transport Protocol (SRTP) extension.
  1336. *
  1337. * https://tools.ietf.org/html/rfc5764 */
  1338. static void ext_srtp_init(SSL *ssl) {
  1339. ssl->srtp_profile = NULL;
  1340. }
  1341. static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
  1342. STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
  1343. if (profiles == NULL) {
  1344. return 1;
  1345. }
  1346. const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
  1347. if (num_profiles == 0) {
  1348. return 1;
  1349. }
  1350. CBB contents, profile_ids;
  1351. if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
  1352. !CBB_add_u16_length_prefixed(out, &contents) ||
  1353. !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
  1354. return 0;
  1355. }
  1356. size_t i;
  1357. for (i = 0; i < num_profiles; i++) {
  1358. if (!CBB_add_u16(&profile_ids,
  1359. sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
  1360. return 0;
  1361. }
  1362. }
  1363. if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
  1364. !CBB_flush(out)) {
  1365. return 0;
  1366. }
  1367. return 1;
  1368. }
  1369. static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  1370. CBS *contents) {
  1371. if (contents == NULL) {
  1372. return 1;
  1373. }
  1374. /* The extension consists of a u16-prefixed profile ID list containing a
  1375. * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
  1376. *
  1377. * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
  1378. CBS profile_ids, srtp_mki;
  1379. uint16_t profile_id;
  1380. if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
  1381. !CBS_get_u16(&profile_ids, &profile_id) ||
  1382. CBS_len(&profile_ids) != 0 ||
  1383. !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
  1384. CBS_len(contents) != 0) {
  1385. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1386. return 0;
  1387. }
  1388. if (CBS_len(&srtp_mki) != 0) {
  1389. /* Must be no MKI, since we never offer one. */
  1390. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
  1391. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1392. return 0;
  1393. }
  1394. STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
  1395. /* Check to see if the server gave us something we support (and presumably
  1396. * offered). */
  1397. size_t i;
  1398. for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
  1399. const SRTP_PROTECTION_PROFILE *profile =
  1400. sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
  1401. if (profile->id == profile_id) {
  1402. ssl->srtp_profile = profile;
  1403. return 1;
  1404. }
  1405. }
  1406. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1407. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1408. return 0;
  1409. }
  1410. static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  1411. CBS *contents) {
  1412. if (contents == NULL) {
  1413. return 1;
  1414. }
  1415. CBS profile_ids, srtp_mki;
  1416. if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
  1417. CBS_len(&profile_ids) < 2 ||
  1418. !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
  1419. CBS_len(contents) != 0) {
  1420. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1421. return 0;
  1422. }
  1423. /* Discard the MKI value for now. */
  1424. const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
  1425. SSL_get_srtp_profiles(ssl);
  1426. /* Pick the server's most preferred profile. */
  1427. size_t i;
  1428. for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
  1429. const SRTP_PROTECTION_PROFILE *server_profile =
  1430. sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
  1431. CBS profile_ids_tmp;
  1432. CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
  1433. while (CBS_len(&profile_ids_tmp) > 0) {
  1434. uint16_t profile_id;
  1435. if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
  1436. return 0;
  1437. }
  1438. if (server_profile->id == profile_id) {
  1439. ssl->srtp_profile = server_profile;
  1440. return 1;
  1441. }
  1442. }
  1443. }
  1444. return 1;
  1445. }
  1446. static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
  1447. if (ssl->srtp_profile == NULL) {
  1448. return 1;
  1449. }
  1450. CBB contents, profile_ids;
  1451. if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
  1452. !CBB_add_u16_length_prefixed(out, &contents) ||
  1453. !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
  1454. !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
  1455. !CBB_add_u8(&contents, 0 /* empty MKI */) ||
  1456. !CBB_flush(out)) {
  1457. return 0;
  1458. }
  1459. return 1;
  1460. }
  1461. /* EC point formats.
  1462. *
  1463. * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
  1464. static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
  1465. if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
  1466. return 0;
  1467. }
  1468. const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
  1469. size_t i;
  1470. for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
  1471. const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
  1472. const uint32_t alg_k = cipher->algorithm_mkey;
  1473. const uint32_t alg_a = cipher->algorithm_auth;
  1474. if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
  1475. return 1;
  1476. }
  1477. }
  1478. return 0;
  1479. }
  1480. static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
  1481. CBB contents, formats;
  1482. if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
  1483. !CBB_add_u16_length_prefixed(out, &contents) ||
  1484. !CBB_add_u8_length_prefixed(&contents, &formats) ||
  1485. !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
  1486. !CBB_flush(out)) {
  1487. return 0;
  1488. }
  1489. return 1;
  1490. }
  1491. static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
  1492. if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
  1493. return 1;
  1494. }
  1495. return ext_ec_point_add_extension(ssl, out);
  1496. }
  1497. static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  1498. CBS *contents) {
  1499. if (contents == NULL) {
  1500. return 1;
  1501. }
  1502. CBS ec_point_format_list;
  1503. if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
  1504. CBS_len(contents) != 0) {
  1505. return 0;
  1506. }
  1507. /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
  1508. * point format. */
  1509. if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
  1510. CBS_len(&ec_point_format_list)) == NULL) {
  1511. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1512. return 0;
  1513. }
  1514. return 1;
  1515. }
  1516. static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  1517. CBS *contents) {
  1518. return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
  1519. }
  1520. static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
  1521. const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
  1522. const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
  1523. const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
  1524. if (!using_ecc) {
  1525. return 1;
  1526. }
  1527. return ext_ec_point_add_extension(ssl, out);
  1528. }
  1529. /* EC supported curves.
  1530. *
  1531. * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
  1532. static void ext_ec_curves_init(SSL *ssl) {
  1533. OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
  1534. ssl->s3->tmp.peer_ellipticcurvelist = NULL;
  1535. ssl->s3->tmp.peer_ellipticcurvelist_length = 0;
  1536. }
  1537. static int ext_ec_curves_add_clienthello(SSL *ssl, CBB *out) {
  1538. if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
  1539. return 1;
  1540. }
  1541. CBB contents, curves_bytes;
  1542. if (!CBB_add_u16(out, TLSEXT_TYPE_elliptic_curves) ||
  1543. !CBB_add_u16_length_prefixed(out, &contents) ||
  1544. !CBB_add_u16_length_prefixed(&contents, &curves_bytes)) {
  1545. return 0;
  1546. }
  1547. const uint16_t *curves;
  1548. size_t curves_len;
  1549. tls1_get_curvelist(ssl, 0, &curves, &curves_len);
  1550. size_t i;
  1551. for (i = 0; i < curves_len; i++) {
  1552. if (!CBB_add_u16(&curves_bytes, curves[i])) {
  1553. return 0;
  1554. }
  1555. }
  1556. return CBB_flush(out);
  1557. }
  1558. static int ext_ec_curves_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  1559. CBS *contents) {
  1560. /* This extension is not expected to be echoed by servers and is ignored. */
  1561. return 1;
  1562. }
  1563. static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  1564. CBS *contents) {
  1565. if (contents == NULL) {
  1566. return 1;
  1567. }
  1568. CBS elliptic_curve_list;
  1569. if (!CBS_get_u16_length_prefixed(contents, &elliptic_curve_list) ||
  1570. CBS_len(&elliptic_curve_list) == 0 ||
  1571. (CBS_len(&elliptic_curve_list) & 1) != 0 ||
  1572. CBS_len(contents) != 0) {
  1573. return 0;
  1574. }
  1575. ssl->s3->tmp.peer_ellipticcurvelist = OPENSSL_malloc(CBS_len(&elliptic_curve_list));
  1576. if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) {
  1577. *out_alert = SSL_AD_INTERNAL_ERROR;
  1578. return 0;
  1579. }
  1580. const size_t num_curves = CBS_len(&elliptic_curve_list) / 2;
  1581. size_t i;
  1582. for (i = 0; i < num_curves; i++) {
  1583. if (!CBS_get_u16(&elliptic_curve_list,
  1584. &ssl->s3->tmp.peer_ellipticcurvelist[i])) {
  1585. goto err;
  1586. }
  1587. }
  1588. assert(CBS_len(&elliptic_curve_list) == 0);
  1589. ssl->s3->tmp.peer_ellipticcurvelist_length = num_curves;
  1590. return 1;
  1591. err:
  1592. OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
  1593. ssl->s3->tmp.peer_ellipticcurvelist = NULL;
  1594. *out_alert = SSL_AD_INTERNAL_ERROR;
  1595. return 0;
  1596. }
  1597. static int ext_ec_curves_add_serverhello(SSL *ssl, CBB *out) {
  1598. /* Servers don't echo this extension. */
  1599. return 1;
  1600. }
  1601. /* kExtensions contains all the supported extensions. */
  1602. static const struct tls_extension kExtensions[] = {
  1603. {
  1604. /* The renegotiation extension must always be at index zero because the
  1605. * |received| and |sent| bitsets need to be tweaked when the "extension" is
  1606. * sent as an SCSV. */
  1607. TLSEXT_TYPE_renegotiate,
  1608. NULL,
  1609. ext_ri_add_clienthello,
  1610. ext_ri_parse_serverhello,
  1611. ext_ri_parse_clienthello,
  1612. ext_ri_add_serverhello,
  1613. },
  1614. {
  1615. TLSEXT_TYPE_server_name,
  1616. ext_sni_init,
  1617. ext_sni_add_clienthello,
  1618. ext_sni_parse_serverhello,
  1619. ext_sni_parse_clienthello,
  1620. ext_sni_add_serverhello,
  1621. },
  1622. {
  1623. TLSEXT_TYPE_extended_master_secret,
  1624. ext_ems_init,
  1625. ext_ems_add_clienthello,
  1626. ext_ems_parse_serverhello,
  1627. ext_ems_parse_clienthello,
  1628. ext_ems_add_serverhello,
  1629. },
  1630. {
  1631. TLSEXT_TYPE_session_ticket,
  1632. NULL,
  1633. ext_ticket_add_clienthello,
  1634. ext_ticket_parse_serverhello,
  1635. ext_ticket_parse_clienthello,
  1636. ext_ticket_add_serverhello,
  1637. },
  1638. {
  1639. TLSEXT_TYPE_signature_algorithms,
  1640. NULL,
  1641. ext_sigalgs_add_clienthello,
  1642. ext_sigalgs_parse_serverhello,
  1643. ext_sigalgs_parse_clienthello,
  1644. ext_sigalgs_add_serverhello,
  1645. },
  1646. {
  1647. TLSEXT_TYPE_status_request,
  1648. ext_ocsp_init,
  1649. ext_ocsp_add_clienthello,
  1650. ext_ocsp_parse_serverhello,
  1651. ext_ocsp_parse_clienthello,
  1652. ext_ocsp_add_serverhello,
  1653. },
  1654. {
  1655. TLSEXT_TYPE_next_proto_neg,
  1656. ext_npn_init,
  1657. ext_npn_add_clienthello,
  1658. ext_npn_parse_serverhello,
  1659. ext_npn_parse_clienthello,
  1660. ext_npn_add_serverhello,
  1661. },
  1662. {
  1663. TLSEXT_TYPE_certificate_timestamp,
  1664. NULL,
  1665. ext_sct_add_clienthello,
  1666. ext_sct_parse_serverhello,
  1667. ext_sct_parse_clienthello,
  1668. ext_sct_add_serverhello,
  1669. },
  1670. {
  1671. TLSEXT_TYPE_application_layer_protocol_negotiation,
  1672. ext_alpn_init,
  1673. ext_alpn_add_clienthello,
  1674. ext_alpn_parse_serverhello,
  1675. ext_alpn_parse_clienthello,
  1676. ext_alpn_add_serverhello,
  1677. },
  1678. {
  1679. TLSEXT_TYPE_channel_id,
  1680. ext_channel_id_init,
  1681. ext_channel_id_add_clienthello,
  1682. ext_channel_id_parse_serverhello,
  1683. ext_channel_id_parse_clienthello,
  1684. ext_channel_id_add_serverhello,
  1685. },
  1686. {
  1687. TLSEXT_TYPE_srtp,
  1688. ext_srtp_init,
  1689. ext_srtp_add_clienthello,
  1690. ext_srtp_parse_serverhello,
  1691. ext_srtp_parse_clienthello,
  1692. ext_srtp_add_serverhello,
  1693. },
  1694. {
  1695. TLSEXT_TYPE_ec_point_formats,
  1696. NULL,
  1697. ext_ec_point_add_clienthello,
  1698. ext_ec_point_parse_serverhello,
  1699. ext_ec_point_parse_clienthello,
  1700. ext_ec_point_add_serverhello,
  1701. },
  1702. {
  1703. TLSEXT_TYPE_elliptic_curves,
  1704. ext_ec_curves_init,
  1705. ext_ec_curves_add_clienthello,
  1706. ext_ec_curves_parse_serverhello,
  1707. ext_ec_curves_parse_clienthello,
  1708. ext_ec_curves_add_serverhello,
  1709. },
  1710. };
  1711. #define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
  1712. OPENSSL_COMPILE_ASSERT(kNumExtensions <=
  1713. sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
  1714. too_many_extensions_for_sent_bitset);
  1715. OPENSSL_COMPILE_ASSERT(kNumExtensions <=
  1716. sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
  1717. 8,
  1718. too_many_extensions_for_received_bitset);
  1719. static const struct tls_extension *tls_extension_find(uint32_t *out_index,
  1720. uint16_t value) {
  1721. unsigned i;
  1722. for (i = 0; i < kNumExtensions; i++) {
  1723. if (kExtensions[i].value == value) {
  1724. *out_index = i;
  1725. return &kExtensions[i];
  1726. }
  1727. }
  1728. return NULL;
  1729. }
  1730. int SSL_extension_supported(unsigned extension_value) {
  1731. uint32_t index;
  1732. return extension_value == TLSEXT_TYPE_padding ||
  1733. tls_extension_find(&index, extension_value) != NULL;
  1734. }
  1735. int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
  1736. /* don't add extensions for SSLv3 unless doing secure renegotiation */
  1737. if (ssl->client_version == SSL3_VERSION &&
  1738. !ssl->s3->send_connection_binding) {
  1739. return 1;
  1740. }
  1741. CBB extensions;
  1742. if (!CBB_add_u16_length_prefixed(out, &extensions)) {
  1743. goto err;
  1744. }
  1745. ssl->s3->tmp.extensions.sent = 0;
  1746. ssl->s3->tmp.custom_extensions.sent = 0;
  1747. size_t i;
  1748. for (i = 0; i < kNumExtensions; i++) {
  1749. if (kExtensions[i].init != NULL) {
  1750. kExtensions[i].init(ssl);
  1751. }
  1752. }
  1753. for (i = 0; i < kNumExtensions; i++) {
  1754. const size_t len_before = CBB_len(&extensions);
  1755. if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
  1756. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
  1757. ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
  1758. goto err;
  1759. }
  1760. if (CBB_len(&extensions) != len_before) {
  1761. ssl->s3->tmp.extensions.sent |= (1u << i);
  1762. }
  1763. }
  1764. if (!custom_ext_add_clienthello(ssl, &extensions)) {
  1765. goto err;
  1766. }
  1767. if (!SSL_IS_DTLS(ssl)) {
  1768. header_len += 2 + CBB_len(&extensions);
  1769. if (header_len > 0xff && header_len < 0x200) {
  1770. /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
  1771. *
  1772. * NB: because this code works out the length of all existing extensions
  1773. * it MUST always appear last. */
  1774. size_t padding_len = 0x200 - header_len;
  1775. /* Extensions take at least four bytes to encode. Always include least
  1776. * one byte of data if including the extension. WebSphere Application
  1777. * Server 7.0 is intolerant to the last extension being zero-length. */
  1778. if (padding_len >= 4 + 1) {
  1779. padding_len -= 4;
  1780. } else {
  1781. padding_len = 1;
  1782. }
  1783. uint8_t *padding_bytes;
  1784. if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
  1785. !CBB_add_u16(&extensions, padding_len) ||
  1786. !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
  1787. goto err;
  1788. }
  1789. memset(padding_bytes, 0, padding_len);
  1790. }
  1791. }
  1792. /* Discard empty extensions blocks. */
  1793. if (CBB_len(&extensions) == 0) {
  1794. CBB_discard_child(out);
  1795. }
  1796. return CBB_flush(out);
  1797. err:
  1798. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1799. return 0;
  1800. }
  1801. int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
  1802. CBB extensions;
  1803. if (!CBB_add_u16_length_prefixed(out, &extensions)) {
  1804. goto err;
  1805. }
  1806. unsigned i;
  1807. for (i = 0; i < kNumExtensions; i++) {
  1808. if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
  1809. /* Don't send extensions that were not received. */
  1810. continue;
  1811. }
  1812. if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
  1813. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
  1814. ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
  1815. goto err;
  1816. }
  1817. }
  1818. if (!custom_ext_add_serverhello(ssl, &extensions)) {
  1819. goto err;
  1820. }
  1821. /* Discard empty extensions blocks. */
  1822. if (CBB_len(&extensions) == 0) {
  1823. CBB_discard_child(out);
  1824. }
  1825. return CBB_flush(out);
  1826. err:
  1827. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1828. return 0;
  1829. }
  1830. static int ssl_scan_clienthello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
  1831. size_t i;
  1832. for (i = 0; i < kNumExtensions; i++) {
  1833. if (kExtensions[i].init != NULL) {
  1834. kExtensions[i].init(ssl);
  1835. }
  1836. }
  1837. ssl->s3->tmp.extensions.received = 0;
  1838. ssl->s3->tmp.custom_extensions.received = 0;
  1839. /* The renegotiation extension must always be at index zero because the
  1840. * |received| and |sent| bitsets need to be tweaked when the "extension" is
  1841. * sent as an SCSV. */
  1842. assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
  1843. /* There may be no extensions. */
  1844. if (CBS_len(cbs) != 0) {
  1845. /* Decode the extensions block and check it is valid. */
  1846. CBS extensions;
  1847. if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
  1848. !tls1_check_duplicate_extensions(&extensions)) {
  1849. *out_alert = SSL_AD_DECODE_ERROR;
  1850. return 0;
  1851. }
  1852. while (CBS_len(&extensions) != 0) {
  1853. uint16_t type;
  1854. CBS extension;
  1855. /* Decode the next extension. */
  1856. if (!CBS_get_u16(&extensions, &type) ||
  1857. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  1858. *out_alert = SSL_AD_DECODE_ERROR;
  1859. return 0;
  1860. }
  1861. /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
  1862. * ambiguous. Ignore all but the renegotiation_info extension. */
  1863. if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
  1864. continue;
  1865. }
  1866. unsigned ext_index;
  1867. const struct tls_extension *const ext =
  1868. tls_extension_find(&ext_index, type);
  1869. if (ext == NULL) {
  1870. if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
  1871. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
  1872. return 0;
  1873. }
  1874. continue;
  1875. }
  1876. ssl->s3->tmp.extensions.received |= (1u << ext_index);
  1877. uint8_t alert = SSL_AD_DECODE_ERROR;
  1878. if (!ext->parse_clienthello(ssl, &alert, &extension)) {
  1879. *out_alert = alert;
  1880. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
  1881. ERR_add_error_dataf("extension: %u", (unsigned)type);
  1882. return 0;
  1883. }
  1884. }
  1885. }
  1886. for (i = 0; i < kNumExtensions; i++) {
  1887. if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
  1888. /* Extension wasn't observed so call the callback with a NULL
  1889. * parameter. */
  1890. uint8_t alert = SSL_AD_DECODE_ERROR;
  1891. if (!kExtensions[i].parse_clienthello(ssl, &alert, NULL)) {
  1892. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
  1893. ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
  1894. *out_alert = alert;
  1895. return 0;
  1896. }
  1897. }
  1898. }
  1899. return 1;
  1900. }
  1901. int ssl_parse_clienthello_tlsext(SSL *ssl, CBS *cbs) {
  1902. int alert = -1;
  1903. if (ssl_scan_clienthello_tlsext(ssl, cbs, &alert) <= 0) {
  1904. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  1905. return 0;
  1906. }
  1907. if (ssl_check_clienthello_tlsext(ssl) <= 0) {
  1908. OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
  1909. return 0;
  1910. }
  1911. return 1;
  1912. }
  1913. OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
  1914. static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
  1915. uint32_t received = 0;
  1916. if (CBS_len(cbs) != 0) {
  1917. /* Decode the extensions block and check it is valid. */
  1918. CBS extensions;
  1919. if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
  1920. !tls1_check_duplicate_extensions(&extensions)) {
  1921. *out_alert = SSL_AD_DECODE_ERROR;
  1922. return 0;
  1923. }
  1924. while (CBS_len(&extensions) != 0) {
  1925. uint16_t type;
  1926. CBS extension;
  1927. /* Decode the next extension. */
  1928. if (!CBS_get_u16(&extensions, &type) ||
  1929. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  1930. *out_alert = SSL_AD_DECODE_ERROR;
  1931. return 0;
  1932. }
  1933. unsigned ext_index;
  1934. const struct tls_extension *const ext =
  1935. tls_extension_find(&ext_index, type);
  1936. if (ext == NULL) {
  1937. if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
  1938. return 0;
  1939. }
  1940. continue;
  1941. }
  1942. if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index))) {
  1943. /* If the extension was never sent then it is illegal. */
  1944. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
  1945. ERR_add_error_dataf("extension :%u", (unsigned)type);
  1946. *out_alert = SSL_AD_DECODE_ERROR;
  1947. return 0;
  1948. }
  1949. received |= (1u << ext_index);
  1950. uint8_t alert = SSL_AD_DECODE_ERROR;
  1951. if (!ext->parse_serverhello(ssl, &alert, &extension)) {
  1952. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
  1953. ERR_add_error_dataf("extension: %u", (unsigned)type);
  1954. *out_alert = alert;
  1955. return 0;
  1956. }
  1957. }
  1958. }
  1959. size_t i;
  1960. for (i = 0; i < kNumExtensions; i++) {
  1961. if (!(received & (1u << i))) {
  1962. /* Extension wasn't observed so call the callback with a NULL
  1963. * parameter. */
  1964. uint8_t alert = SSL_AD_DECODE_ERROR;
  1965. if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
  1966. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
  1967. ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
  1968. *out_alert = alert;
  1969. return 0;
  1970. }
  1971. }
  1972. }
  1973. return 1;
  1974. }
  1975. static int ssl_check_clienthello_tlsext(SSL *ssl) {
  1976. int ret = SSL_TLSEXT_ERR_NOACK;
  1977. int al = SSL_AD_UNRECOGNIZED_NAME;
  1978. /* The handling of the ECPointFormats extension is done elsewhere, namely in
  1979. * ssl3_choose_cipher in s3_lib.c. */
  1980. if (ssl->ctx != NULL && ssl->ctx->tlsext_servername_callback != 0) {
  1981. ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
  1982. ssl->ctx->tlsext_servername_arg);
  1983. } else if (ssl->initial_ctx != NULL &&
  1984. ssl->initial_ctx->tlsext_servername_callback != 0) {
  1985. ret = ssl->initial_ctx->tlsext_servername_callback(
  1986. ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
  1987. }
  1988. switch (ret) {
  1989. case SSL_TLSEXT_ERR_ALERT_FATAL:
  1990. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  1991. return -1;
  1992. case SSL_TLSEXT_ERR_ALERT_WARNING:
  1993. ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
  1994. return 1;
  1995. case SSL_TLSEXT_ERR_NOACK:
  1996. ssl->s3->tmp.should_ack_sni = 0;
  1997. return 1;
  1998. default:
  1999. return 1;
  2000. }
  2001. }
  2002. static int ssl_check_serverhello_tlsext(SSL *ssl) {
  2003. int ret = SSL_TLSEXT_ERR_OK;
  2004. int al = SSL_AD_UNRECOGNIZED_NAME;
  2005. if (ssl->ctx != NULL && ssl->ctx->tlsext_servername_callback != 0) {
  2006. ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
  2007. ssl->ctx->tlsext_servername_arg);
  2008. } else if (ssl->initial_ctx != NULL &&
  2009. ssl->initial_ctx->tlsext_servername_callback != 0) {
  2010. ret = ssl->initial_ctx->tlsext_servername_callback(
  2011. ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
  2012. }
  2013. switch (ret) {
  2014. case SSL_TLSEXT_ERR_ALERT_FATAL:
  2015. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  2016. return -1;
  2017. case SSL_TLSEXT_ERR_ALERT_WARNING:
  2018. ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
  2019. return 1;
  2020. default:
  2021. return 1;
  2022. }
  2023. }
  2024. int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
  2025. int alert = -1;
  2026. if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
  2027. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  2028. return 0;
  2029. }
  2030. if (ssl_check_serverhello_tlsext(ssl) <= 0) {
  2031. OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
  2032. return 0;
  2033. }
  2034. return 1;
  2035. }
  2036. int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
  2037. int *out_renew_ticket, const uint8_t *ticket,
  2038. size_t ticket_len, const uint8_t *session_id,
  2039. size_t session_id_len) {
  2040. int ret = 1; /* Most errors are non-fatal. */
  2041. SSL_CTX *ssl_ctx = ssl->initial_ctx;
  2042. uint8_t *plaintext = NULL;
  2043. HMAC_CTX hmac_ctx;
  2044. HMAC_CTX_init(&hmac_ctx);
  2045. EVP_CIPHER_CTX cipher_ctx;
  2046. EVP_CIPHER_CTX_init(&cipher_ctx);
  2047. *out_renew_ticket = 0;
  2048. *out_session = NULL;
  2049. if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  2050. goto done;
  2051. }
  2052. /* Ensure there is room for the key name and the largest IV
  2053. * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
  2054. * the maximum IV length should be well under the minimum size for the
  2055. * session material and HMAC. */
  2056. if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
  2057. goto done;
  2058. }
  2059. const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
  2060. if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
  2061. int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
  2062. ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
  2063. &hmac_ctx, 0 /* decrypt */);
  2064. if (cb_ret < 0) {
  2065. ret = 0;
  2066. goto done;
  2067. }
  2068. if (cb_ret == 0) {
  2069. goto done;
  2070. }
  2071. if (cb_ret == 2) {
  2072. *out_renew_ticket = 1;
  2073. }
  2074. } else {
  2075. /* Check the key name matches. */
  2076. if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
  2077. SSL_TICKET_KEY_NAME_LEN) != 0) {
  2078. goto done;
  2079. }
  2080. if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
  2081. sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
  2082. NULL) ||
  2083. !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
  2084. ssl_ctx->tlsext_tick_aes_key, iv)) {
  2085. ret = 0;
  2086. goto done;
  2087. }
  2088. }
  2089. size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
  2090. /* Check the MAC at the end of the ticket. */
  2091. uint8_t mac[EVP_MAX_MD_SIZE];
  2092. size_t mac_len = HMAC_size(&hmac_ctx);
  2093. if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
  2094. /* The ticket must be large enough for key name, IV, data, and MAC. */
  2095. goto done;
  2096. }
  2097. HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
  2098. HMAC_Final(&hmac_ctx, mac, NULL);
  2099. if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
  2100. goto done;
  2101. }
  2102. /* Decrypt the session data. */
  2103. const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
  2104. size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
  2105. mac_len;
  2106. plaintext = OPENSSL_malloc(ciphertext_len);
  2107. if (plaintext == NULL) {
  2108. ret = 0;
  2109. goto done;
  2110. }
  2111. if (ciphertext_len >= INT_MAX) {
  2112. goto done;
  2113. }
  2114. int len1, len2;
  2115. if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
  2116. (int)ciphertext_len) ||
  2117. !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
  2118. ERR_clear_error(); /* Don't leave an error on the queue. */
  2119. goto done;
  2120. }
  2121. /* Decode the session. */
  2122. SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
  2123. if (session == NULL) {
  2124. ERR_clear_error(); /* Don't leave an error on the queue. */
  2125. goto done;
  2126. }
  2127. /* Copy the client's session ID into the new session, to denote the ticket has
  2128. * been accepted. */
  2129. memcpy(session->session_id, session_id, session_id_len);
  2130. session->session_id_length = session_id_len;
  2131. *out_session = session;
  2132. done:
  2133. OPENSSL_free(plaintext);
  2134. HMAC_CTX_cleanup(&hmac_ctx);
  2135. EVP_CIPHER_CTX_cleanup(&cipher_ctx);
  2136. return ret;
  2137. }
  2138. /* Tables to translate from NIDs to TLS v1.2 ids */
  2139. typedef struct {
  2140. int nid;
  2141. int id;
  2142. } tls12_lookup;
  2143. static const tls12_lookup tls12_md[] = {
  2144. {NID_sha1, TLSEXT_hash_sha1},
  2145. {NID_sha256, TLSEXT_hash_sha256},
  2146. {NID_sha384, TLSEXT_hash_sha384},
  2147. {NID_sha512, TLSEXT_hash_sha512},
  2148. };
  2149. static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
  2150. {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
  2151. static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
  2152. size_t i;
  2153. for (i = 0; i < tlen; i++) {
  2154. if (table[i].nid == nid) {
  2155. return table[i].id;
  2156. }
  2157. }
  2158. return -1;
  2159. }
  2160. int tls12_get_sigid(int pkey_type) {
  2161. return tls12_find_id(pkey_type, tls12_sig,
  2162. sizeof(tls12_sig) / sizeof(tls12_lookup));
  2163. }
  2164. int tls12_add_sigandhash(SSL *ssl, CBB *out, const EVP_MD *md) {
  2165. int md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
  2166. sizeof(tls12_md) / sizeof(tls12_lookup));
  2167. int sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
  2168. return md_id != -1 &&
  2169. sig_id != -1 &&
  2170. CBB_add_u8(out, (uint8_t)md_id) &&
  2171. CBB_add_u8(out, (uint8_t)sig_id);
  2172. }
  2173. const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
  2174. switch (hash_alg) {
  2175. case TLSEXT_hash_sha1:
  2176. return EVP_sha1();
  2177. case TLSEXT_hash_sha256:
  2178. return EVP_sha256();
  2179. case TLSEXT_hash_sha384:
  2180. return EVP_sha384();
  2181. case TLSEXT_hash_sha512:
  2182. return EVP_sha512();
  2183. default:
  2184. return NULL;
  2185. }
  2186. }
  2187. /* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
  2188. * algorithm |sig_alg|. It returns -1 if the type is unknown. */
  2189. static int tls12_get_pkey_type(uint8_t sig_alg) {
  2190. switch (sig_alg) {
  2191. case TLSEXT_signature_rsa:
  2192. return EVP_PKEY_RSA;
  2193. case TLSEXT_signature_ecdsa:
  2194. return EVP_PKEY_EC;
  2195. default:
  2196. return -1;
  2197. }
  2198. }
  2199. OPENSSL_COMPILE_ASSERT(sizeof(TLS_SIGALGS) == 2,
  2200. sizeof_tls_sigalgs_is_not_two);
  2201. int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
  2202. /* Extension ignored for inappropriate versions */
  2203. if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
  2204. return 1;
  2205. }
  2206. CERT *const cert = ssl->cert;
  2207. OPENSSL_free(cert->peer_sigalgs);
  2208. cert->peer_sigalgs = NULL;
  2209. cert->peer_sigalgslen = 0;
  2210. size_t num_sigalgs = CBS_len(in_sigalgs);
  2211. if (num_sigalgs % 2 != 0) {
  2212. return 0;
  2213. }
  2214. num_sigalgs /= 2;
  2215. /* supported_signature_algorithms in the certificate request is
  2216. * allowed to be empty. */
  2217. if (num_sigalgs == 0) {
  2218. return 1;
  2219. }
  2220. /* This multiplication doesn't overflow because sizeof(TLS_SIGALGS) is two
  2221. * (statically asserted above) and we just divided |num_sigalgs| by two. */
  2222. cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(TLS_SIGALGS));
  2223. if (cert->peer_sigalgs == NULL) {
  2224. return 0;
  2225. }
  2226. cert->peer_sigalgslen = num_sigalgs;
  2227. CBS sigalgs;
  2228. CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
  2229. size_t i;
  2230. for (i = 0; i < num_sigalgs; i++) {
  2231. TLS_SIGALGS *const sigalg = &cert->peer_sigalgs[i];
  2232. if (!CBS_get_u8(&sigalgs, &sigalg->rhash) ||
  2233. !CBS_get_u8(&sigalgs, &sigalg->rsign)) {
  2234. return 0;
  2235. }
  2236. }
  2237. return 1;
  2238. }
  2239. const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
  2240. CERT *cert = ssl->cert;
  2241. int type = ssl_private_key_type(ssl);
  2242. size_t i, j;
  2243. static const int kDefaultDigestList[] = {NID_sha256, NID_sha384, NID_sha512,
  2244. NID_sha1};
  2245. const int *digest_nids = kDefaultDigestList;
  2246. size_t num_digest_nids =
  2247. sizeof(kDefaultDigestList) / sizeof(kDefaultDigestList[0]);
  2248. if (cert->digest_nids != NULL) {
  2249. digest_nids = cert->digest_nids;
  2250. num_digest_nids = cert->num_digest_nids;
  2251. }
  2252. for (i = 0; i < num_digest_nids; i++) {
  2253. const int digest_nid = digest_nids[i];
  2254. for (j = 0; j < cert->peer_sigalgslen; j++) {
  2255. const EVP_MD *md = tls12_get_hash(cert->peer_sigalgs[j].rhash);
  2256. if (md == NULL ||
  2257. digest_nid != EVP_MD_type(md) ||
  2258. tls12_get_pkey_type(cert->peer_sigalgs[j].rsign) != type) {
  2259. continue;
  2260. }
  2261. return md;
  2262. }
  2263. }
  2264. /* If no suitable digest may be found, default to SHA-1. */
  2265. return EVP_sha1();
  2266. }
  2267. int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
  2268. int ret = 0;
  2269. EVP_MD_CTX ctx;
  2270. EVP_MD_CTX_init(&ctx);
  2271. if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
  2272. goto err;
  2273. }
  2274. static const char kClientIDMagic[] = "TLS Channel ID signature";
  2275. EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
  2276. if (ssl->hit) {
  2277. static const char kResumptionMagic[] = "Resumption";
  2278. EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
  2279. if (ssl->session->original_handshake_hash_len == 0) {
  2280. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2281. goto err;
  2282. }
  2283. EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
  2284. ssl->session->original_handshake_hash_len);
  2285. }
  2286. uint8_t handshake_hash[EVP_MAX_MD_SIZE];
  2287. int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
  2288. sizeof(handshake_hash));
  2289. if (handshake_hash_len < 0) {
  2290. goto err;
  2291. }
  2292. EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
  2293. unsigned len_u;
  2294. EVP_DigestFinal_ex(&ctx, out, &len_u);
  2295. *out_len = len_u;
  2296. ret = 1;
  2297. err:
  2298. EVP_MD_CTX_cleanup(&ctx);
  2299. return ret;
  2300. }
  2301. /* tls1_record_handshake_hashes_for_channel_id records the current handshake
  2302. * hashes in |ssl->session| so that Channel ID resumptions can sign that
  2303. * data. */
  2304. int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
  2305. int digest_len;
  2306. /* This function should never be called for a resumed session because the
  2307. * handshake hashes that we wish to record are for the original, full
  2308. * handshake. */
  2309. if (ssl->hit) {
  2310. return -1;
  2311. }
  2312. digest_len =
  2313. tls1_handshake_digest(ssl, ssl->session->original_handshake_hash,
  2314. sizeof(ssl->session->original_handshake_hash));
  2315. if (digest_len < 0) {
  2316. return -1;
  2317. }
  2318. ssl->session->original_handshake_hash_len = digest_len;
  2319. return 1;
  2320. }