Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220
  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 <stdio.h>
  109. #include <stdlib.h>
  110. #include <assert.h>
  111. #include <openssl/bytestring.h>
  112. #include <openssl/evp.h>
  113. #include <openssl/hmac.h>
  114. #include <openssl/mem.h>
  115. #include <openssl/obj.h>
  116. #include <openssl/rand.h>
  117. #include "ssl_locl.h"
  118. static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
  119. const unsigned char *sess_id, int sesslen,
  120. SSL_SESSION **psess);
  121. static int ssl_check_clienthello_tlsext(SSL *s);
  122. static int ssl_check_serverhello_tlsext(SSL *s);
  123. SSL3_ENC_METHOD TLSv1_enc_data={
  124. tls1_enc,
  125. tls1_mac,
  126. tls1_setup_key_block,
  127. tls1_generate_master_secret,
  128. tls1_change_cipher_state,
  129. tls1_final_finish_mac,
  130. TLS1_FINISH_MAC_LENGTH,
  131. tls1_cert_verify_mac,
  132. TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
  133. TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
  134. tls1_alert_code,
  135. tls1_export_keying_material,
  136. 0,
  137. SSL3_HM_HEADER_LENGTH,
  138. ssl3_set_handshake_header,
  139. ssl3_handshake_write,
  140. ssl3_add_to_finished_hash,
  141. };
  142. SSL3_ENC_METHOD TLSv1_1_enc_data={
  143. tls1_enc,
  144. tls1_mac,
  145. tls1_setup_key_block,
  146. tls1_generate_master_secret,
  147. tls1_change_cipher_state,
  148. tls1_final_finish_mac,
  149. TLS1_FINISH_MAC_LENGTH,
  150. tls1_cert_verify_mac,
  151. TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
  152. TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
  153. tls1_alert_code,
  154. tls1_export_keying_material,
  155. SSL_ENC_FLAG_EXPLICIT_IV,
  156. SSL3_HM_HEADER_LENGTH,
  157. ssl3_set_handshake_header,
  158. ssl3_handshake_write,
  159. ssl3_add_to_finished_hash,
  160. };
  161. SSL3_ENC_METHOD TLSv1_2_enc_data={
  162. tls1_enc,
  163. tls1_mac,
  164. tls1_setup_key_block,
  165. tls1_generate_master_secret,
  166. tls1_change_cipher_state,
  167. tls1_final_finish_mac,
  168. TLS1_FINISH_MAC_LENGTH,
  169. tls1_cert_verify_mac,
  170. TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
  171. TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
  172. tls1_alert_code,
  173. tls1_export_keying_material,
  174. SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
  175. |SSL_ENC_FLAG_TLS1_2_CIPHERS,
  176. SSL3_HM_HEADER_LENGTH,
  177. ssl3_set_handshake_header,
  178. ssl3_handshake_write,
  179. ssl3_add_to_finished_hash,
  180. };
  181. static int compare_uint16_t(const void *p1, const void *p2)
  182. {
  183. uint16_t u1 = *((const uint16_t*)p1);
  184. uint16_t u2 = *((const uint16_t*)p2);
  185. if (u1 < u2)
  186. {
  187. return -1;
  188. }
  189. else if (u1 > u2)
  190. {
  191. return 1;
  192. }
  193. else
  194. {
  195. return 0;
  196. }
  197. }
  198. /* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be more
  199. * than one extension of the same type in a ClientHello or ServerHello. This
  200. * function does an initial scan over the extensions block to filter those
  201. * out. */
  202. static int tls1_check_duplicate_extensions(const CBS *cbs)
  203. {
  204. CBS extensions = *cbs;
  205. size_t num_extensions = 0, i = 0;
  206. uint16_t *extension_types = NULL;
  207. int ret = 0;
  208. /* First pass: count the extensions. */
  209. while (CBS_len(&extensions) > 0)
  210. {
  211. uint16_t type;
  212. CBS extension;
  213. if (!CBS_get_u16(&extensions, &type) ||
  214. !CBS_get_u16_length_prefixed(&extensions, &extension))
  215. {
  216. goto done;
  217. }
  218. num_extensions++;
  219. }
  220. if (num_extensions == 0)
  221. {
  222. return 1;
  223. }
  224. extension_types = (uint16_t*)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
  225. if (extension_types == NULL)
  226. {
  227. OPENSSL_PUT_ERROR(SSL, tls1_check_duplicate_extensions, ERR_R_MALLOC_FAILURE);
  228. goto done;
  229. }
  230. /* Second pass: gather the extension types. */
  231. extensions = *cbs;
  232. for (i = 0; i < num_extensions; i++)
  233. {
  234. CBS extension;
  235. if (!CBS_get_u16(&extensions, &extension_types[i]) ||
  236. !CBS_get_u16_length_prefixed(&extensions, &extension))
  237. {
  238. /* This should not happen. */
  239. goto done;
  240. }
  241. }
  242. assert(CBS_len(&extensions) == 0);
  243. /* Sort the extensions and make sure there are no duplicates. */
  244. qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
  245. for (i = 1; i < num_extensions; i++)
  246. {
  247. if (extension_types[i-1] == extension_types[i])
  248. {
  249. goto done;
  250. }
  251. }
  252. ret = 1;
  253. done:
  254. if (extension_types)
  255. OPENSSL_free(extension_types);
  256. return ret;
  257. }
  258. char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx)
  259. {
  260. CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
  261. CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
  262. /* Skip client version. */
  263. if (!CBS_skip(&client_hello, 2))
  264. return 0;
  265. /* Skip client nonce. */
  266. if (!CBS_skip(&client_hello, 32))
  267. return 0;
  268. /* Extract session_id. */
  269. if (!CBS_get_u8_length_prefixed(&client_hello, &session_id))
  270. return 0;
  271. ctx->session_id = CBS_data(&session_id);
  272. ctx->session_id_len = CBS_len(&session_id);
  273. /* Skip past DTLS cookie */
  274. if (SSL_IS_DTLS(ctx->ssl))
  275. {
  276. CBS cookie;
  277. if (!CBS_get_u8_length_prefixed(&client_hello, &cookie))
  278. return 0;
  279. }
  280. /* Extract cipher_suites. */
  281. if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
  282. CBS_len(&cipher_suites) < 2 ||
  283. (CBS_len(&cipher_suites) & 1) != 0)
  284. return 0;
  285. ctx->cipher_suites = CBS_data(&cipher_suites);
  286. ctx->cipher_suites_len = CBS_len(&cipher_suites);
  287. /* Extract compression_methods. */
  288. if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
  289. CBS_len(&compression_methods) < 1)
  290. return 0;
  291. ctx->compression_methods = CBS_data(&compression_methods);
  292. ctx->compression_methods_len = CBS_len(&compression_methods);
  293. /* If the ClientHello ends here then it's valid, but doesn't have any
  294. * extensions. (E.g. SSLv3.) */
  295. if (CBS_len(&client_hello) == 0)
  296. {
  297. ctx->extensions = NULL;
  298. ctx->extensions_len = 0;
  299. return 1;
  300. }
  301. /* Extract extensions and check it is valid. */
  302. if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
  303. !tls1_check_duplicate_extensions(&extensions) ||
  304. CBS_len(&client_hello) != 0)
  305. return 0;
  306. ctx->extensions = CBS_data(&extensions);
  307. ctx->extensions_len = CBS_len(&extensions);
  308. return 1;
  309. }
  310. char
  311. SSL_early_callback_ctx_extension_get(const struct ssl_early_callback_ctx *ctx,
  312. uint16_t extension_type,
  313. const unsigned char **out_data,
  314. size_t *out_len)
  315. {
  316. CBS extensions;
  317. CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
  318. while (CBS_len(&extensions) != 0)
  319. {
  320. uint16_t type;
  321. CBS extension;
  322. /* Decode the next extension. */
  323. if (!CBS_get_u16(&extensions, &type) ||
  324. !CBS_get_u16_length_prefixed(&extensions, &extension))
  325. return 0;
  326. if (type == extension_type)
  327. {
  328. *out_data = CBS_data(&extension);
  329. *out_len = CBS_len(&extension);
  330. return 1;
  331. }
  332. }
  333. return 0;
  334. }
  335. static const int nid_list[] =
  336. {
  337. NID_sect163k1, /* sect163k1 (1) */
  338. NID_sect163r1, /* sect163r1 (2) */
  339. NID_sect163r2, /* sect163r2 (3) */
  340. NID_sect193r1, /* sect193r1 (4) */
  341. NID_sect193r2, /* sect193r2 (5) */
  342. NID_sect233k1, /* sect233k1 (6) */
  343. NID_sect233r1, /* sect233r1 (7) */
  344. NID_sect239k1, /* sect239k1 (8) */
  345. NID_sect283k1, /* sect283k1 (9) */
  346. NID_sect283r1, /* sect283r1 (10) */
  347. NID_sect409k1, /* sect409k1 (11) */
  348. NID_sect409r1, /* sect409r1 (12) */
  349. NID_sect571k1, /* sect571k1 (13) */
  350. NID_sect571r1, /* sect571r1 (14) */
  351. NID_secp160k1, /* secp160k1 (15) */
  352. NID_secp160r1, /* secp160r1 (16) */
  353. NID_secp160r2, /* secp160r2 (17) */
  354. NID_secp192k1, /* secp192k1 (18) */
  355. NID_X9_62_prime192v1, /* secp192r1 (19) */
  356. NID_secp224k1, /* secp224k1 (20) */
  357. NID_secp224r1, /* secp224r1 (21) */
  358. NID_secp256k1, /* secp256k1 (22) */
  359. NID_X9_62_prime256v1, /* secp256r1 (23) */
  360. NID_secp384r1, /* secp384r1 (24) */
  361. NID_secp521r1, /* secp521r1 (25) */
  362. NID_brainpoolP256r1, /* brainpoolP256r1 (26) */
  363. NID_brainpoolP384r1, /* brainpoolP384r1 (27) */
  364. NID_brainpoolP512r1 /* brainpool512r1 (28) */
  365. };
  366. static const uint8_t ecformats_default[] =
  367. {
  368. TLSEXT_ECPOINTFORMAT_uncompressed,
  369. };
  370. static const uint16_t eccurves_default[] =
  371. {
  372. 23, /* secp256r1 (23) */
  373. 24, /* secp384r1 (24) */
  374. 25, /* secp521r1 (25) */
  375. };
  376. int tls1_ec_curve_id2nid(uint16_t curve_id)
  377. {
  378. /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
  379. if (curve_id < 1 || curve_id > sizeof(nid_list)/sizeof(nid_list[0]))
  380. return OBJ_undef;
  381. return nid_list[curve_id-1];
  382. }
  383. uint16_t tls1_ec_nid2curve_id(int nid)
  384. {
  385. size_t i;
  386. for (i = 0; i < sizeof(nid_list)/sizeof(nid_list[0]); i++)
  387. {
  388. /* nid_list[i] stores the NID corresponding to curve ID i+1. */
  389. if (nid == nid_list[i])
  390. return i + 1;
  391. }
  392. /* Use 0 for non-existent curve ID. Note: this assumes that curve ID 0
  393. * will never be allocated. */
  394. return 0;
  395. }
  396. /* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len|
  397. * to the list of allowed curve IDs. If |get_peer_curves| is non-zero,
  398. * return the peer's curve list. Otherwise, return the preferred
  399. * list. */
  400. static void tls1_get_curvelist(SSL *s, int get_peer_curves,
  401. const uint16_t **out_curve_ids, size_t *out_curve_ids_len)
  402. {
  403. if (get_peer_curves)
  404. {
  405. *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
  406. *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
  407. return;
  408. }
  409. *out_curve_ids = s->tlsext_ellipticcurvelist;
  410. *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
  411. if (!*out_curve_ids)
  412. {
  413. *out_curve_ids = eccurves_default;
  414. *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
  415. }
  416. }
  417. int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id)
  418. {
  419. uint8_t curve_type;
  420. uint16_t curve_id;
  421. const uint16_t *curves;
  422. size_t curves_len, i;
  423. /* Only support named curves. */
  424. if (!CBS_get_u8(cbs, &curve_type) ||
  425. curve_type != NAMED_CURVE_TYPE ||
  426. !CBS_get_u16(cbs, &curve_id))
  427. return 0;
  428. tls1_get_curvelist(s, 0, &curves, &curves_len);
  429. for (i = 0; i < curves_len; i++)
  430. {
  431. if (curve_id == curves[i])
  432. {
  433. *out_curve_id = curve_id;
  434. return 1;
  435. }
  436. }
  437. return 0;
  438. }
  439. int tls1_get_shared_curve(SSL *s)
  440. {
  441. const uint16_t *pref, *supp;
  442. size_t preflen, supplen, i, j;
  443. /* Can't do anything on client side */
  444. if (s->server == 0)
  445. return NID_undef;
  446. /* Return first preference shared curve */
  447. tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
  448. &supp, &supplen);
  449. tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
  450. &pref, &preflen);
  451. for (i = 0; i < preflen; i++)
  452. {
  453. for (j = 0; j < supplen; j++)
  454. {
  455. if (pref[i] == supp[j])
  456. return tls1_ec_curve_id2nid(pref[i]);
  457. }
  458. }
  459. return NID_undef;
  460. }
  461. /* NOTE: tls1_ec_curve_id2nid and tls1_set_curves assume that
  462. *
  463. * (a) 0 is not a valid curve ID.
  464. *
  465. * (b) The largest curve ID is 31.
  466. *
  467. * Those implementations must be revised before adding support for curve IDs
  468. * that break these assumptions. */
  469. OPENSSL_COMPILE_ASSERT(
  470. (sizeof(nid_list) / sizeof(nid_list[0])) < 32, small_curve_ids);
  471. int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
  472. const int *curves, size_t ncurves)
  473. {
  474. uint16_t *curve_ids;
  475. size_t i;
  476. /* Bitmap of curves included to detect duplicates: only works
  477. * while curve ids < 32
  478. */
  479. uint32_t dup_list = 0;
  480. curve_ids = (uint16_t*)OPENSSL_malloc(ncurves * sizeof(uint16_t));
  481. if (!curve_ids)
  482. return 0;
  483. for (i = 0; i < ncurves; i++)
  484. {
  485. uint32_t idmask;
  486. uint16_t id;
  487. id = tls1_ec_nid2curve_id(curves[i]);
  488. idmask = ((uint32_t)1) << id;
  489. if (!id || (dup_list & idmask))
  490. {
  491. OPENSSL_free(curve_ids);
  492. return 0;
  493. }
  494. dup_list |= idmask;
  495. curve_ids[i] = id;
  496. }
  497. if (*out_curve_ids)
  498. OPENSSL_free(*out_curve_ids);
  499. *out_curve_ids = curve_ids;
  500. *out_curve_ids_len = ncurves;
  501. return 1;
  502. }
  503. /* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
  504. * TLS curve ID and point format, respectively, for |ec|. It returns one on
  505. * success and zero on failure. */
  506. static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id, uint8_t *out_comp_id, EC_KEY *ec)
  507. {
  508. int nid;
  509. uint16_t id;
  510. const EC_GROUP *grp;
  511. if (!ec)
  512. return 0;
  513. grp = EC_KEY_get0_group(ec);
  514. if (!grp)
  515. return 0;
  516. /* Determine curve ID */
  517. nid = EC_GROUP_get_curve_name(grp);
  518. id = tls1_ec_nid2curve_id(nid);
  519. if (!id)
  520. return 0;
  521. /* Set the named curve ID. Arbitrary explicit curves are not
  522. * supported. */
  523. *out_curve_id = id;
  524. if (out_comp_id)
  525. {
  526. if (EC_KEY_get0_public_key(ec) == NULL)
  527. return 0;
  528. if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED)
  529. *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
  530. else
  531. *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
  532. }
  533. return 1;
  534. }
  535. /* tls1_check_point_format returns one if |comp_id| is consistent with the
  536. * peer's point format preferences. */
  537. static int tls1_check_point_format(SSL *s, uint8_t comp_id)
  538. {
  539. uint8_t *p = s->s3->tmp.peer_ecpointformatlist;
  540. size_t plen = s->s3->tmp.peer_ecpointformatlist_length;
  541. size_t i;
  542. /* If point formats extension present check it, otherwise everything
  543. * is supported (see RFC4492). */
  544. if (p == NULL)
  545. return 1;
  546. for (i = 0; i < plen; i++)
  547. {
  548. if (comp_id == p[i])
  549. return 1;
  550. }
  551. return 0;
  552. }
  553. /* tls1_check_curve_id returns one if |curve_id| is consistent with both our and
  554. * the peer's curve preferences. Note: if called as the client, only our
  555. * preferences are checked; the peer (the server) does not send preferences. */
  556. static int tls1_check_curve_id(SSL *s, uint16_t curve_id)
  557. {
  558. const uint16_t *curves;
  559. size_t curves_len, i, j;
  560. /* Check against our list, then the peer's list. */
  561. for (j = 0; j <= 1; j++)
  562. {
  563. tls1_get_curvelist(s, j, &curves, &curves_len);
  564. for (i = 0; i < curves_len; i++)
  565. {
  566. if (curves[i] == curve_id)
  567. break;
  568. }
  569. if (i == curves_len)
  570. return 0;
  571. /* Servers do not present a preference list so, if we are a
  572. * client, only check our list. */
  573. if (!s->server)
  574. return 1;
  575. }
  576. return 1;
  577. }
  578. static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
  579. size_t *pformatslen)
  580. {
  581. /* If we have a custom point format list use it otherwise
  582. * use default */
  583. if (s->tlsext_ecpointformatlist)
  584. {
  585. *pformats = s->tlsext_ecpointformatlist;
  586. *pformatslen = s->tlsext_ecpointformatlist_length;
  587. }
  588. else
  589. {
  590. *pformats = ecformats_default;
  591. *pformatslen = sizeof(ecformats_default);
  592. }
  593. }
  594. /* Check cert parameters compatible with extensions: currently just checks
  595. * EC certificates have compatible curves and compression.
  596. */
  597. static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
  598. {
  599. uint8_t comp_id;
  600. uint16_t curve_id;
  601. EVP_PKEY *pkey;
  602. int rv;
  603. pkey = X509_get_pubkey(x);
  604. if (!pkey)
  605. return 0;
  606. /* If not EC nothing to do */
  607. if (pkey->type != EVP_PKEY_EC)
  608. {
  609. EVP_PKEY_free(pkey);
  610. return 1;
  611. }
  612. rv = tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec);
  613. EVP_PKEY_free(pkey);
  614. if (!rv)
  615. return 0;
  616. /* Can't check curve_id for client certs as we don't have a
  617. * supported curves extension. */
  618. if (s->server && !tls1_check_curve_id(s, curve_id))
  619. return 0;
  620. return tls1_check_point_format(s, comp_id);
  621. }
  622. /* Check EC temporary key is compatible with client extensions */
  623. int tls1_check_ec_tmp_key(SSL *s)
  624. {
  625. uint16_t curve_id;
  626. EC_KEY *ec = s->cert->ecdh_tmp;
  627. if (s->cert->ecdh_tmp_auto)
  628. {
  629. /* Need a shared curve */
  630. return tls1_get_shared_curve(s) != NID_undef;
  631. }
  632. if (!ec)
  633. {
  634. if (s->cert->ecdh_tmp_cb)
  635. return 1;
  636. else
  637. return 0;
  638. }
  639. return tls1_curve_params_from_ec_key(&curve_id, NULL, ec) &&
  640. tls1_check_curve_id(s, curve_id);
  641. }
  642. /* List of supported signature algorithms and hashes. Should make this
  643. * customisable at some point, for now include everything we support.
  644. */
  645. #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
  646. #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
  647. #define tlsext_sigalg(md) \
  648. tlsext_sigalg_rsa(md) \
  649. tlsext_sigalg_ecdsa(md)
  650. static const uint8_t tls12_sigalgs[] = {
  651. tlsext_sigalg(TLSEXT_hash_sha512)
  652. tlsext_sigalg(TLSEXT_hash_sha384)
  653. tlsext_sigalg(TLSEXT_hash_sha256)
  654. tlsext_sigalg(TLSEXT_hash_sha224)
  655. tlsext_sigalg(TLSEXT_hash_sha1)
  656. };
  657. size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
  658. {
  659. /* If server use client authentication sigalgs if not NULL */
  660. if (s->server && s->cert->client_sigalgs)
  661. {
  662. *psigs = s->cert->client_sigalgs;
  663. return s->cert->client_sigalgslen;
  664. }
  665. else if (s->cert->conf_sigalgs)
  666. {
  667. *psigs = s->cert->conf_sigalgs;
  668. return s->cert->conf_sigalgslen;
  669. }
  670. else
  671. {
  672. *psigs = tls12_sigalgs;
  673. return sizeof(tls12_sigalgs);
  674. }
  675. }
  676. /* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of
  677. * |cbs|. It checks it is consistent with |s|'s sent supported
  678. * signature algorithms and, if so, writes the relevant digest into
  679. * |*out_md| and returns 1. Otherwise it returns 0 and writes an alert
  680. * into |*out_alert|.
  681. */
  682. int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert,
  683. SSL *s, CBS *cbs, EVP_PKEY *pkey)
  684. {
  685. const unsigned char *sent_sigs;
  686. size_t sent_sigslen, i;
  687. int sigalg = tls12_get_sigid(pkey);
  688. uint8_t hash, signature;
  689. /* Should never happen */
  690. if (sigalg == -1)
  691. {
  692. OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, ERR_R_INTERNAL_ERROR);
  693. *out_alert = SSL_AD_INTERNAL_ERROR;
  694. return 0;
  695. }
  696. if (!CBS_get_u8(cbs, &hash) ||
  697. !CBS_get_u8(cbs, &signature))
  698. {
  699. OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_DECODE_ERROR);
  700. *out_alert = SSL_AD_DECODE_ERROR;
  701. return 0;
  702. }
  703. /* Check key type is consistent with signature */
  704. if (sigalg != signature)
  705. {
  706. OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
  707. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  708. return 0;
  709. }
  710. if (pkey->type == EVP_PKEY_EC)
  711. {
  712. uint16_t curve_id;
  713. uint8_t comp_id;
  714. /* Check compression and curve matches extensions */
  715. if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec))
  716. {
  717. *out_alert = SSL_AD_INTERNAL_ERROR;
  718. return 0;
  719. }
  720. if (s->server)
  721. {
  722. if (!tls1_check_curve_id(s, curve_id) ||
  723. !tls1_check_point_format(s, comp_id))
  724. {
  725. OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE);
  726. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  727. return 0;
  728. }
  729. }
  730. }
  731. /* Check signature matches a type we sent */
  732. sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
  733. for (i = 0; i < sent_sigslen; i+=2, sent_sigs+=2)
  734. {
  735. if (hash == sent_sigs[0] && signature == sent_sigs[1])
  736. break;
  737. }
  738. /* Allow fallback to SHA1 if not strict mode */
  739. if (i == sent_sigslen && (hash != TLSEXT_hash_sha1 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
  740. {
  741. OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
  742. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  743. return 0;
  744. }
  745. *out_md = tls12_get_hash(hash);
  746. if (*out_md == NULL)
  747. {
  748. OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST);
  749. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  750. return 0;
  751. }
  752. /* Store the digest used so applications can retrieve it if they
  753. * wish.
  754. */
  755. if (s->session && s->session->sess_cert)
  756. s->session->sess_cert->peer_key->digest = *out_md;
  757. return 1;
  758. }
  759. /* Get a mask of disabled algorithms: an algorithm is disabled
  760. * if it isn't supported or doesn't appear in supported signature
  761. * algorithms. Unlike ssl_cipher_get_disabled this applies to a specific
  762. * session and not global settings.
  763. *
  764. */
  765. void ssl_set_client_disabled(SSL *s)
  766. {
  767. CERT *c = s->cert;
  768. const unsigned char *sigalgs;
  769. size_t i, sigalgslen;
  770. int have_rsa = 0, have_ecdsa = 0;
  771. c->mask_a = 0;
  772. c->mask_k = 0;
  773. /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
  774. if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
  775. c->mask_ssl = SSL_TLSV1_2;
  776. else
  777. c->mask_ssl = 0;
  778. /* Now go through all signature algorithms seeing if we support
  779. * any for RSA, DSA, ECDSA. Do this for all versions not just
  780. * TLS 1.2.
  781. */
  782. sigalgslen = tls12_get_psigalgs(s, &sigalgs);
  783. for (i = 0; i < sigalgslen; i += 2, sigalgs += 2)
  784. {
  785. switch(sigalgs[1])
  786. {
  787. case TLSEXT_signature_rsa:
  788. have_rsa = 1;
  789. break;
  790. case TLSEXT_signature_ecdsa:
  791. have_ecdsa = 1;
  792. break;
  793. }
  794. }
  795. /* Disable auth if we don't include any appropriate signature
  796. * algorithms.
  797. */
  798. if (!have_rsa)
  799. {
  800. c->mask_a |= SSL_aRSA;
  801. }
  802. if (!have_ecdsa)
  803. {
  804. c->mask_a |= SSL_aECDSA;
  805. }
  806. /* with PSK there must be client callback set */
  807. if (!s->psk_client_callback)
  808. {
  809. c->mask_a |= SSL_aPSK;
  810. c->mask_k |= SSL_kPSK;
  811. }
  812. c->valid = 1;
  813. }
  814. /* header_len is the length of the ClientHello header written so far, used to
  815. * compute padding. It does not include the record header. Pass 0 if no padding
  816. * is to be done. */
  817. unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit, size_t header_len)
  818. {
  819. int extdatalen=0;
  820. unsigned char *ret = buf;
  821. unsigned char *orig = buf;
  822. /* See if we support any ECC ciphersuites */
  823. int using_ecc = 0;
  824. if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s))
  825. {
  826. size_t i;
  827. unsigned long alg_k, alg_a;
  828. STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
  829. for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
  830. {
  831. const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
  832. alg_k = c->algorithm_mkey;
  833. alg_a = c->algorithm_auth;
  834. if ((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA))
  835. {
  836. using_ecc = 1;
  837. break;
  838. }
  839. }
  840. }
  841. /* don't add extensions for SSLv3 unless doing secure renegotiation */
  842. if (s->client_version == SSL3_VERSION
  843. && !s->s3->send_connection_binding)
  844. return orig;
  845. ret+=2;
  846. if (ret>=limit) return NULL; /* this really never occurs, but ... */
  847. if (s->tlsext_hostname != NULL)
  848. {
  849. /* Add TLS extension servername to the Client Hello message */
  850. unsigned long size_str;
  851. long lenmax;
  852. /* check for enough space.
  853. 4 for the servername type and entension length
  854. 2 for servernamelist length
  855. 1 for the hostname type
  856. 2 for hostname length
  857. + hostname length
  858. */
  859. if ((lenmax = limit - ret - 9) < 0
  860. || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
  861. return NULL;
  862. /* extension type and length */
  863. s2n(TLSEXT_TYPE_server_name,ret);
  864. s2n(size_str+5,ret);
  865. /* length of servername list */
  866. s2n(size_str+3,ret);
  867. /* hostname type, length and hostname */
  868. *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
  869. s2n(size_str,ret);
  870. memcpy(ret, s->tlsext_hostname, size_str);
  871. ret+=size_str;
  872. }
  873. /* Add RI if renegotiating */
  874. if (s->renegotiate)
  875. {
  876. int el;
  877. if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
  878. {
  879. OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
  880. return NULL;
  881. }
  882. if((limit - ret - 4 - el) < 0) return NULL;
  883. s2n(TLSEXT_TYPE_renegotiate,ret);
  884. s2n(el,ret);
  885. if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
  886. {
  887. OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
  888. return NULL;
  889. }
  890. ret += el;
  891. }
  892. /* Add extended master secret. */
  893. if (s->version != SSL3_VERSION)
  894. {
  895. if (limit - ret - 4 < 0)
  896. return NULL;
  897. s2n(TLSEXT_TYPE_extended_master_secret,ret);
  898. s2n(0,ret);
  899. }
  900. if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
  901. {
  902. int ticklen;
  903. if (!s->new_session && s->session && s->session->tlsext_tick)
  904. ticklen = s->session->tlsext_ticklen;
  905. else if (s->session && s->tlsext_session_ticket &&
  906. s->tlsext_session_ticket->data)
  907. {
  908. s->session->tlsext_tick = BUF_memdup(
  909. s->tlsext_session_ticket->data,
  910. s->tlsext_session_ticket->length);
  911. if (!s->session->tlsext_tick)
  912. return NULL;
  913. ticklen = s->tlsext_session_ticket->length;
  914. s->session->tlsext_ticklen = ticklen;
  915. }
  916. else
  917. ticklen = 0;
  918. if (ticklen == 0 && s->tlsext_session_ticket &&
  919. s->tlsext_session_ticket->data == NULL)
  920. goto skip_ext;
  921. /* Check for enough room 2 for extension type, 2 for len
  922. * rest for ticket
  923. */
  924. if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
  925. s2n(TLSEXT_TYPE_session_ticket,ret);
  926. s2n(ticklen,ret);
  927. if (ticklen)
  928. {
  929. memcpy(ret, s->session->tlsext_tick, ticklen);
  930. ret += ticklen;
  931. }
  932. }
  933. skip_ext:
  934. if (SSL_USE_SIGALGS(s))
  935. {
  936. size_t salglen;
  937. const unsigned char *salg;
  938. salglen = tls12_get_psigalgs(s, &salg);
  939. if ((size_t)(limit - ret) < salglen + 6)
  940. return NULL;
  941. s2n(TLSEXT_TYPE_signature_algorithms,ret);
  942. s2n(salglen + 2, ret);
  943. s2n(salglen, ret);
  944. memcpy(ret, salg, salglen);
  945. ret += salglen;
  946. }
  947. if (s->ocsp_stapling_enabled)
  948. {
  949. /* The status_request extension is excessively extensible at
  950. * every layer. On the client, only support requesting OCSP
  951. * responses with an empty responder_id_list and no
  952. * extensions. */
  953. if (limit - ret - 4 - 1 - 2 - 2 < 0) return NULL;
  954. s2n(TLSEXT_TYPE_status_request, ret);
  955. s2n(1 + 2 + 2, ret);
  956. /* status_type */
  957. *(ret++) = TLSEXT_STATUSTYPE_ocsp;
  958. /* responder_id_list - empty */
  959. s2n(0, ret);
  960. /* request_extensions - empty */
  961. s2n(0, ret);
  962. }
  963. if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
  964. {
  965. /* The client advertises an emtpy extension to indicate its
  966. * support for Next Protocol Negotiation */
  967. if (limit - ret - 4 < 0)
  968. return NULL;
  969. s2n(TLSEXT_TYPE_next_proto_neg,ret);
  970. s2n(0,ret);
  971. }
  972. if (s->signed_cert_timestamps_enabled && !s->s3->tmp.finish_md_len)
  973. {
  974. /* The client advertises an empty extension to indicate its support for
  975. * certificate timestamps. */
  976. if (limit - ret - 4 < 0)
  977. return NULL;
  978. s2n(TLSEXT_TYPE_certificate_timestamp,ret);
  979. s2n(0,ret);
  980. }
  981. if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len)
  982. {
  983. if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
  984. return NULL;
  985. s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
  986. s2n(2 + s->alpn_client_proto_list_len,ret);
  987. s2n(s->alpn_client_proto_list_len,ret);
  988. memcpy(ret, s->alpn_client_proto_list,
  989. s->alpn_client_proto_list_len);
  990. ret += s->alpn_client_proto_list_len;
  991. }
  992. if (s->tlsext_channel_id_enabled)
  993. {
  994. /* The client advertises an emtpy extension to indicate its
  995. * support for Channel ID. */
  996. if (limit - ret - 4 < 0)
  997. return NULL;
  998. if (s->ctx->tlsext_channel_id_enabled_new)
  999. s2n(TLSEXT_TYPE_channel_id_new,ret);
  1000. else
  1001. s2n(TLSEXT_TYPE_channel_id,ret);
  1002. s2n(0,ret);
  1003. }
  1004. if(SSL_get_srtp_profiles(s))
  1005. {
  1006. int el;
  1007. ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
  1008. if((limit - ret - 4 - el) < 0) return NULL;
  1009. s2n(TLSEXT_TYPE_use_srtp,ret);
  1010. s2n(el,ret);
  1011. if(!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
  1012. {
  1013. OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
  1014. return NULL;
  1015. }
  1016. ret += el;
  1017. }
  1018. if (using_ecc)
  1019. {
  1020. /* Add TLS extension ECPointFormats to the ClientHello message */
  1021. long lenmax;
  1022. const uint8_t *formats;
  1023. const uint16_t *curves;
  1024. size_t formats_len, curves_len, i;
  1025. tls1_get_formatlist(s, &formats, &formats_len);
  1026. if ((lenmax = limit - ret - 5) < 0) return NULL;
  1027. if (formats_len > (size_t)lenmax) return NULL;
  1028. if (formats_len > 255)
  1029. {
  1030. OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
  1031. return NULL;
  1032. }
  1033. s2n(TLSEXT_TYPE_ec_point_formats,ret);
  1034. s2n(formats_len + 1,ret);
  1035. *(ret++) = (unsigned char)formats_len;
  1036. memcpy(ret, formats, formats_len);
  1037. ret+=formats_len;
  1038. /* Add TLS extension EllipticCurves to the ClientHello message */
  1039. tls1_get_curvelist(s, 0, &curves, &curves_len);
  1040. if ((lenmax = limit - ret - 6) < 0) return NULL;
  1041. if ((curves_len * 2) > (size_t)lenmax) return NULL;
  1042. if ((curves_len * 2) > 65532)
  1043. {
  1044. OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
  1045. return NULL;
  1046. }
  1047. s2n(TLSEXT_TYPE_elliptic_curves,ret);
  1048. s2n((curves_len * 2) + 2, ret);
  1049. /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
  1050. * elliptic_curve_list, but the examples use two bytes.
  1051. * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
  1052. * resolves this to two bytes.
  1053. */
  1054. s2n(curves_len * 2, ret);
  1055. for (i = 0; i < curves_len; i++)
  1056. {
  1057. s2n(curves[i], ret);
  1058. }
  1059. }
  1060. #ifdef TLSEXT_TYPE_padding
  1061. /* Add padding to workaround bugs in F5 terminators.
  1062. * See https://tools.ietf.org/html/draft-agl-tls-padding-03
  1063. *
  1064. * NB: because this code works out the length of all existing
  1065. * extensions it MUST always appear last. */
  1066. if (header_len > 0)
  1067. {
  1068. header_len += ret - orig;
  1069. if (header_len > 0xff && header_len < 0x200)
  1070. {
  1071. size_t padding_len = 0x200 - header_len;
  1072. /* Extensions take at least four bytes to encode. Always
  1073. * include least one byte of data if including the
  1074. * extension. WebSphere Application Server 7.0 is
  1075. * intolerant to the last extension being zero-length. */
  1076. if (padding_len >= 4 + 1)
  1077. padding_len -= 4;
  1078. else
  1079. padding_len = 1;
  1080. if (limit - ret - 4 - (long)padding_len < 0)
  1081. return NULL;
  1082. s2n(TLSEXT_TYPE_padding, ret);
  1083. s2n(padding_len, ret);
  1084. memset(ret, 0, padding_len);
  1085. ret += padding_len;
  1086. }
  1087. }
  1088. #endif
  1089. if ((extdatalen = ret-orig-2)== 0)
  1090. return orig;
  1091. s2n(extdatalen, orig);
  1092. return ret;
  1093. }
  1094. unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit)
  1095. {
  1096. int extdatalen=0;
  1097. unsigned char *orig = buf;
  1098. unsigned char *ret = buf;
  1099. int next_proto_neg_seen;
  1100. unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1101. unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1102. int using_ecc = (alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA);
  1103. using_ecc = using_ecc && (s->s3->tmp.peer_ecpointformatlist != NULL);
  1104. /* don't add extensions for SSLv3, unless doing secure renegotiation */
  1105. if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
  1106. return orig;
  1107. ret+=2;
  1108. if (ret>=limit) return NULL; /* this really never occurs, but ... */
  1109. if (!s->hit && s->should_ack_sni && s->session->tlsext_hostname != NULL)
  1110. {
  1111. if ((long)(limit - ret - 4) < 0) return NULL;
  1112. s2n(TLSEXT_TYPE_server_name,ret);
  1113. s2n(0,ret);
  1114. }
  1115. if(s->s3->send_connection_binding)
  1116. {
  1117. int el;
  1118. if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
  1119. {
  1120. OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
  1121. return NULL;
  1122. }
  1123. if((limit - ret - 4 - el) < 0) return NULL;
  1124. s2n(TLSEXT_TYPE_renegotiate,ret);
  1125. s2n(el,ret);
  1126. if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
  1127. {
  1128. OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
  1129. return NULL;
  1130. }
  1131. ret += el;
  1132. }
  1133. if (s->s3->tmp.extended_master_secret)
  1134. {
  1135. if ((long)(limit - ret - 4) < 0) return NULL;
  1136. s2n(TLSEXT_TYPE_extended_master_secret,ret);
  1137. s2n(0,ret);
  1138. }
  1139. if (using_ecc)
  1140. {
  1141. const unsigned char *plist;
  1142. size_t plistlen;
  1143. /* Add TLS extension ECPointFormats to the ServerHello message */
  1144. long lenmax;
  1145. tls1_get_formatlist(s, &plist, &plistlen);
  1146. if ((lenmax = limit - ret - 5) < 0) return NULL;
  1147. if (plistlen > (size_t)lenmax) return NULL;
  1148. if (plistlen > 255)
  1149. {
  1150. OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
  1151. return NULL;
  1152. }
  1153. s2n(TLSEXT_TYPE_ec_point_formats,ret);
  1154. s2n(plistlen + 1,ret);
  1155. *(ret++) = (unsigned char) plistlen;
  1156. memcpy(ret, plist, plistlen);
  1157. ret+=plistlen;
  1158. }
  1159. /* Currently the server should not respond with a SupportedCurves extension */
  1160. if (s->tlsext_ticket_expected
  1161. && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
  1162. {
  1163. if ((long)(limit - ret - 4) < 0) return NULL;
  1164. s2n(TLSEXT_TYPE_session_ticket,ret);
  1165. s2n(0,ret);
  1166. }
  1167. if (s->s3->tmp.certificate_status_expected)
  1168. {
  1169. if ((long)(limit - ret - 4) < 0) return NULL;
  1170. s2n(TLSEXT_TYPE_status_request,ret);
  1171. s2n(0,ret);
  1172. }
  1173. if(s->srtp_profile)
  1174. {
  1175. int el;
  1176. ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
  1177. if((limit - ret - 4 - el) < 0) return NULL;
  1178. s2n(TLSEXT_TYPE_use_srtp,ret);
  1179. s2n(el,ret);
  1180. if(!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
  1181. {
  1182. OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
  1183. return NULL;
  1184. }
  1185. ret+=el;
  1186. }
  1187. next_proto_neg_seen = s->s3->next_proto_neg_seen;
  1188. s->s3->next_proto_neg_seen = 0;
  1189. if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
  1190. {
  1191. const unsigned char *npa;
  1192. unsigned int npalen;
  1193. int r;
  1194. r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
  1195. if (r == SSL_TLSEXT_ERR_OK)
  1196. {
  1197. if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
  1198. s2n(TLSEXT_TYPE_next_proto_neg,ret);
  1199. s2n(npalen,ret);
  1200. memcpy(ret, npa, npalen);
  1201. ret += npalen;
  1202. s->s3->next_proto_neg_seen = 1;
  1203. }
  1204. }
  1205. if (s->s3->alpn_selected)
  1206. {
  1207. const uint8_t *selected = s->s3->alpn_selected;
  1208. size_t len = s->s3->alpn_selected_len;
  1209. if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
  1210. return NULL;
  1211. s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
  1212. s2n(3 + len,ret);
  1213. s2n(1 + len,ret);
  1214. *ret++ = len;
  1215. memcpy(ret, selected, len);
  1216. ret += len;
  1217. }
  1218. /* If the client advertised support for Channel ID, and we have it
  1219. * enabled, then we want to echo it back. */
  1220. if (s->s3->tlsext_channel_id_valid)
  1221. {
  1222. if (limit - ret - 4 < 0)
  1223. return NULL;
  1224. if (s->s3->tlsext_channel_id_new)
  1225. s2n(TLSEXT_TYPE_channel_id_new,ret);
  1226. else
  1227. s2n(TLSEXT_TYPE_channel_id,ret);
  1228. s2n(0,ret);
  1229. }
  1230. if ((extdatalen = ret-orig-2) == 0)
  1231. return orig;
  1232. s2n(extdatalen, orig);
  1233. return ret;
  1234. }
  1235. /* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
  1236. * ClientHello.
  1237. * cbs: the contents of the extension, not including the type and length.
  1238. * out_alert: a pointer to the alert value to send in the event of a zero
  1239. * return.
  1240. *
  1241. * returns: 1 on success. */
  1242. static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert)
  1243. {
  1244. CBS protocol_name_list, protocol_name_list_copy;
  1245. const unsigned char *selected;
  1246. unsigned char selected_len;
  1247. int r;
  1248. if (s->ctx->alpn_select_cb == NULL)
  1249. return 1;
  1250. if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
  1251. CBS_len(cbs) != 0 ||
  1252. CBS_len(&protocol_name_list) < 2)
  1253. goto parse_error;
  1254. /* Validate the protocol list. */
  1255. protocol_name_list_copy = protocol_name_list;
  1256. while (CBS_len(&protocol_name_list_copy) > 0)
  1257. {
  1258. CBS protocol_name;
  1259. if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name))
  1260. goto parse_error;
  1261. }
  1262. r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
  1263. CBS_data(&protocol_name_list), CBS_len(&protocol_name_list),
  1264. s->ctx->alpn_select_cb_arg);
  1265. if (r == SSL_TLSEXT_ERR_OK) {
  1266. if (s->s3->alpn_selected)
  1267. OPENSSL_free(s->s3->alpn_selected);
  1268. s->s3->alpn_selected = BUF_memdup(selected, selected_len);
  1269. if (!s->s3->alpn_selected)
  1270. {
  1271. *out_alert = SSL_AD_INTERNAL_ERROR;
  1272. return 0;
  1273. }
  1274. s->s3->alpn_selected_len = selected_len;
  1275. }
  1276. return 1;
  1277. parse_error:
  1278. *out_alert = SSL_AD_DECODE_ERROR;
  1279. return 0;
  1280. }
  1281. static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert)
  1282. {
  1283. int renegotiate_seen = 0;
  1284. CBS extensions;
  1285. size_t i;
  1286. s->should_ack_sni = 0;
  1287. s->s3->next_proto_neg_seen = 0;
  1288. s->s3->tmp.certificate_status_expected = 0;
  1289. s->s3->tmp.extended_master_secret = 0;
  1290. if (s->s3->alpn_selected)
  1291. {
  1292. OPENSSL_free(s->s3->alpn_selected);
  1293. s->s3->alpn_selected = NULL;
  1294. }
  1295. /* Clear any signature algorithms extension received */
  1296. if (s->cert->peer_sigalgs)
  1297. {
  1298. OPENSSL_free(s->cert->peer_sigalgs);
  1299. s->cert->peer_sigalgs = NULL;
  1300. }
  1301. /* Clear any shared signature algorithms */
  1302. if (s->cert->shared_sigalgs)
  1303. {
  1304. OPENSSL_free(s->cert->shared_sigalgs);
  1305. s->cert->shared_sigalgs = NULL;
  1306. }
  1307. /* Clear certificate digests and validity flags */
  1308. for (i = 0; i < SSL_PKEY_NUM; i++)
  1309. {
  1310. s->cert->pkeys[i].digest = NULL;
  1311. s->cert->pkeys[i].valid_flags = 0;
  1312. }
  1313. /* Clear ECC extensions */
  1314. if (s->s3->tmp.peer_ecpointformatlist != 0)
  1315. {
  1316. OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
  1317. s->s3->tmp.peer_ecpointformatlist = NULL;
  1318. s->s3->tmp.peer_ecpointformatlist_length = 0;
  1319. }
  1320. if (s->s3->tmp.peer_ellipticcurvelist != 0)
  1321. {
  1322. OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
  1323. s->s3->tmp.peer_ellipticcurvelist = NULL;
  1324. s->s3->tmp.peer_ellipticcurvelist_length = 0;
  1325. }
  1326. /* There may be no extensions. */
  1327. if (CBS_len(cbs) == 0)
  1328. {
  1329. goto ri_check;
  1330. }
  1331. /* Decode the extensions block and check it is valid. */
  1332. if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
  1333. !tls1_check_duplicate_extensions(&extensions))
  1334. {
  1335. *out_alert = SSL_AD_DECODE_ERROR;
  1336. return 0;
  1337. }
  1338. while (CBS_len(&extensions) != 0)
  1339. {
  1340. uint16_t type;
  1341. CBS extension;
  1342. /* Decode the next extension. */
  1343. if (!CBS_get_u16(&extensions, &type) ||
  1344. !CBS_get_u16_length_prefixed(&extensions, &extension))
  1345. {
  1346. *out_alert = SSL_AD_DECODE_ERROR;
  1347. return 0;
  1348. }
  1349. if (s->tlsext_debug_cb)
  1350. {
  1351. s->tlsext_debug_cb(s, 0, type, (unsigned char*)CBS_data(&extension),
  1352. CBS_len(&extension), s->tlsext_debug_arg);
  1353. }
  1354. /* The servername extension is treated as follows:
  1355. - Only the hostname type is supported with a maximum length of 255.
  1356. - The servername is rejected if too long or if it contains zeros,
  1357. in which case an fatal alert is generated.
  1358. - The servername field is maintained together with the session cache.
  1359. - When a session is resumed, the servername call back invoked in order
  1360. to allow the application to position itself to the right context.
  1361. - The servername is acknowledged if it is new for a session or when
  1362. it is identical to a previously used for the same session.
  1363. Applications can control the behaviour. They can at any time
  1364. set a 'desirable' servername for a new SSL object. This can be the
  1365. case for example with HTTPS when a Host: header field is received and
  1366. a renegotiation is requested. In this case, a possible servername
  1367. presented in the new client hello is only acknowledged if it matches
  1368. the value of the Host: field.
  1369. - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
  1370. if they provide for changing an explicit servername context for the session,
  1371. i.e. when the session has been established with a servername extension.
  1372. - On session reconnect, the servername extension may be absent.
  1373. */
  1374. if (type == TLSEXT_TYPE_server_name)
  1375. {
  1376. CBS server_name_list;
  1377. char have_seen_host_name = 0;
  1378. if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
  1379. CBS_len(&server_name_list) < 1 ||
  1380. CBS_len(&extension) != 0)
  1381. {
  1382. *out_alert = SSL_AD_DECODE_ERROR;
  1383. return 0;
  1384. }
  1385. /* Decode each ServerName in the extension. */
  1386. while (CBS_len(&server_name_list) > 0)
  1387. {
  1388. uint8_t name_type;
  1389. CBS host_name;
  1390. /* Decode the NameType. */
  1391. if (!CBS_get_u8(&server_name_list, &name_type))
  1392. {
  1393. *out_alert = SSL_AD_DECODE_ERROR;
  1394. return 0;
  1395. }
  1396. /* Only host_name is supported. */
  1397. if (name_type != TLSEXT_NAMETYPE_host_name)
  1398. continue;
  1399. if (have_seen_host_name)
  1400. {
  1401. /* The ServerNameList MUST NOT contain
  1402. * more than one name of the same
  1403. * name_type. */
  1404. *out_alert = SSL_AD_DECODE_ERROR;
  1405. return 0;
  1406. }
  1407. have_seen_host_name = 1;
  1408. if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
  1409. CBS_len(&host_name) < 1)
  1410. {
  1411. *out_alert = SSL_AD_DECODE_ERROR;
  1412. return 0;
  1413. }
  1414. if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
  1415. CBS_contains_zero_byte(&host_name))
  1416. {
  1417. *out_alert = SSL_AD_UNRECOGNIZED_NAME;
  1418. return 0;
  1419. }
  1420. if (!s->hit)
  1421. {
  1422. assert(s->session->tlsext_hostname == NULL);
  1423. if (s->session->tlsext_hostname)
  1424. {
  1425. /* This should be impossible. */
  1426. *out_alert = SSL_AD_DECODE_ERROR;
  1427. return 0;
  1428. }
  1429. /* Copy the hostname as a string. */
  1430. if (!CBS_strdup(&host_name, &s->session->tlsext_hostname))
  1431. {
  1432. *out_alert = SSL_AD_INTERNAL_ERROR;
  1433. return 0;
  1434. }
  1435. s->should_ack_sni = 1;
  1436. }
  1437. }
  1438. }
  1439. else if (type == TLSEXT_TYPE_ec_point_formats)
  1440. {
  1441. CBS ec_point_format_list;
  1442. if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
  1443. CBS_len(&extension) != 0)
  1444. {
  1445. *out_alert = SSL_AD_DECODE_ERROR;
  1446. return 0;
  1447. }
  1448. if (!CBS_stow(&ec_point_format_list,
  1449. &s->s3->tmp.peer_ecpointformatlist,
  1450. &s->s3->tmp.peer_ecpointformatlist_length))
  1451. {
  1452. *out_alert = SSL_AD_INTERNAL_ERROR;
  1453. return 0;
  1454. }
  1455. }
  1456. else if (type == TLSEXT_TYPE_elliptic_curves)
  1457. {
  1458. CBS elliptic_curve_list;
  1459. size_t i, num_curves;
  1460. if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
  1461. CBS_len(&elliptic_curve_list) == 0 ||
  1462. (CBS_len(&elliptic_curve_list) & 1) != 0 ||
  1463. CBS_len(&extension) != 0)
  1464. {
  1465. *out_alert = SSL_AD_DECODE_ERROR;
  1466. return 0;
  1467. }
  1468. if (s->s3->tmp.peer_ellipticcurvelist)
  1469. {
  1470. OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
  1471. s->s3->tmp.peer_ellipticcurvelist_length = 0;
  1472. }
  1473. s->s3->tmp.peer_ellipticcurvelist =
  1474. (uint16_t*)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
  1475. if (s->s3->tmp.peer_ellipticcurvelist == NULL)
  1476. {
  1477. *out_alert = SSL_AD_INTERNAL_ERROR;
  1478. return 0;
  1479. }
  1480. num_curves = CBS_len(&elliptic_curve_list) / 2;
  1481. for (i = 0; i < num_curves; i++)
  1482. {
  1483. if (!CBS_get_u16(&elliptic_curve_list,
  1484. &s->s3->tmp.peer_ellipticcurvelist[i]))
  1485. {
  1486. *out_alert = SSL_AD_INTERNAL_ERROR;
  1487. return 0;
  1488. }
  1489. }
  1490. if (CBS_len(&elliptic_curve_list) != 0)
  1491. {
  1492. *out_alert = SSL_AD_INTERNAL_ERROR;
  1493. return 0;
  1494. }
  1495. s->s3->tmp.peer_ellipticcurvelist_length = num_curves;
  1496. }
  1497. else if (type == TLSEXT_TYPE_session_ticket)
  1498. {
  1499. if (s->tls_session_ticket_ext_cb &&
  1500. !s->tls_session_ticket_ext_cb(s, CBS_data(&extension), CBS_len(&extension), s->tls_session_ticket_ext_cb_arg))
  1501. {
  1502. *out_alert = SSL_AD_INTERNAL_ERROR;
  1503. return 0;
  1504. }
  1505. }
  1506. else if (type == TLSEXT_TYPE_renegotiate)
  1507. {
  1508. if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert))
  1509. return 0;
  1510. renegotiate_seen = 1;
  1511. }
  1512. else if (type == TLSEXT_TYPE_signature_algorithms)
  1513. {
  1514. CBS supported_signature_algorithms;
  1515. if (!CBS_get_u16_length_prefixed(&extension, &supported_signature_algorithms) ||
  1516. CBS_len(&extension) != 0)
  1517. {
  1518. *out_alert = SSL_AD_DECODE_ERROR;
  1519. return 0;
  1520. }
  1521. /* Ensure the signature algorithms are non-empty. It
  1522. * contains a list of SignatureAndHashAlgorithms
  1523. * which are two bytes each. */
  1524. if (CBS_len(&supported_signature_algorithms) == 0 ||
  1525. (CBS_len(&supported_signature_algorithms) % 2) != 0)
  1526. {
  1527. *out_alert = SSL_AD_DECODE_ERROR;
  1528. return 0;
  1529. }
  1530. if (!tls1_process_sigalgs(s, &supported_signature_algorithms))
  1531. {
  1532. *out_alert = SSL_AD_DECODE_ERROR;
  1533. return 0;
  1534. }
  1535. /* If sigalgs received and no shared algorithms fatal
  1536. * error.
  1537. */
  1538. if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs)
  1539. {
  1540. OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
  1541. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1542. return 0;
  1543. }
  1544. }
  1545. else if (type == TLSEXT_TYPE_next_proto_neg &&
  1546. s->s3->tmp.finish_md_len == 0 &&
  1547. s->s3->alpn_selected == NULL)
  1548. {
  1549. /* The extension must be empty. */
  1550. if (CBS_len(&extension) != 0)
  1551. {
  1552. *out_alert = SSL_AD_DECODE_ERROR;
  1553. return 0;
  1554. }
  1555. /* We shouldn't accept this extension on a
  1556. * renegotiation.
  1557. *
  1558. * s->new_session will be set on renegotiation, but we
  1559. * probably shouldn't rely that it couldn't be set on
  1560. * the initial renegotation too in certain cases (when
  1561. * there's some other reason to disallow resuming an
  1562. * earlier session -- the current code won't be doing
  1563. * anything like that, but this might change).
  1564. * A valid sign that there's been a previous handshake
  1565. * in this connection is if s->s3->tmp.finish_md_len >
  1566. * 0. (We are talking about a check that will happen
  1567. * in the Hello protocol round, well before a new
  1568. * Finished message could have been computed.) */
  1569. s->s3->next_proto_neg_seen = 1;
  1570. }
  1571. else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
  1572. s->ctx->alpn_select_cb &&
  1573. s->s3->tmp.finish_md_len == 0)
  1574. {
  1575. if (!tls1_alpn_handle_client_hello(s, &extension, out_alert))
  1576. return 0;
  1577. /* ALPN takes precedence over NPN. */
  1578. s->s3->next_proto_neg_seen = 0;
  1579. }
  1580. else if (type == TLSEXT_TYPE_channel_id &&
  1581. s->tlsext_channel_id_enabled)
  1582. {
  1583. /* The extension must be empty. */
  1584. if (CBS_len(&extension) != 0)
  1585. {
  1586. *out_alert = SSL_AD_DECODE_ERROR;
  1587. return 0;
  1588. }
  1589. s->s3->tlsext_channel_id_valid = 1;
  1590. }
  1591. else if (type == TLSEXT_TYPE_channel_id_new &&
  1592. s->tlsext_channel_id_enabled)
  1593. {
  1594. /* The extension must be empty. */
  1595. if (CBS_len(&extension) != 0)
  1596. {
  1597. *out_alert = SSL_AD_DECODE_ERROR;
  1598. return 0;
  1599. }
  1600. s->s3->tlsext_channel_id_valid = 1;
  1601. s->s3->tlsext_channel_id_new = 1;
  1602. }
  1603. /* session ticket processed earlier */
  1604. else if (type == TLSEXT_TYPE_use_srtp)
  1605. {
  1606. if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert))
  1607. return 0;
  1608. }
  1609. else if (type == TLSEXT_TYPE_extended_master_secret &&
  1610. s->version != SSL3_VERSION)
  1611. {
  1612. if (CBS_len(&extension) != 0)
  1613. {
  1614. *out_alert = SSL_AD_DECODE_ERROR;
  1615. return 0;
  1616. }
  1617. s->s3->tmp.extended_master_secret = 1;
  1618. }
  1619. }
  1620. ri_check:
  1621. /* Need RI if renegotiating */
  1622. if (!renegotiate_seen && s->renegotiate &&
  1623. !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
  1624. {
  1625. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  1626. OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  1627. return 0;
  1628. }
  1629. /* If no signature algorithms extension set default values */
  1630. if (!s->cert->peer_sigalgs)
  1631. ssl_cert_set_default_md(s->cert);
  1632. return 1;
  1633. }
  1634. int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs)
  1635. {
  1636. int alert = -1;
  1637. if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0)
  1638. {
  1639. ssl3_send_alert(s, SSL3_AL_FATAL, alert);
  1640. return 0;
  1641. }
  1642. if (ssl_check_clienthello_tlsext(s) <= 0)
  1643. {
  1644. OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext, SSL_R_CLIENTHELLO_TLSEXT);
  1645. return 0;
  1646. }
  1647. return 1;
  1648. }
  1649. /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
  1650. * elements of zero length are allowed and the set of elements must exactly fill
  1651. * the length of the block. */
  1652. static char ssl_next_proto_validate(const CBS *cbs)
  1653. {
  1654. CBS copy = *cbs;
  1655. while (CBS_len(&copy) != 0)
  1656. {
  1657. CBS proto;
  1658. if (!CBS_get_u8_length_prefixed(&copy, &proto) ||
  1659. CBS_len(&proto) == 0)
  1660. {
  1661. return 0;
  1662. }
  1663. }
  1664. return 1;
  1665. }
  1666. static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert)
  1667. {
  1668. int tlsext_servername = 0;
  1669. int renegotiate_seen = 0;
  1670. CBS extensions;
  1671. /* TODO(davidben): Move all of these to some per-handshake state that
  1672. * gets systematically reset on a new handshake; perhaps allocate it
  1673. * fresh each time so it's not even kept around post-handshake. */
  1674. s->s3->next_proto_neg_seen = 0;
  1675. s->tlsext_ticket_expected = 0;
  1676. s->s3->tmp.certificate_status_expected = 0;
  1677. s->s3->tmp.extended_master_secret = 0;
  1678. if (s->s3->alpn_selected)
  1679. {
  1680. OPENSSL_free(s->s3->alpn_selected);
  1681. s->s3->alpn_selected = NULL;
  1682. }
  1683. /* Clear ECC extensions */
  1684. if (s->s3->tmp.peer_ecpointformatlist != 0)
  1685. {
  1686. OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
  1687. s->s3->tmp.peer_ecpointformatlist = NULL;
  1688. s->s3->tmp.peer_ecpointformatlist_length = 0;
  1689. }
  1690. /* There may be no extensions. */
  1691. if (CBS_len(cbs) == 0)
  1692. {
  1693. goto ri_check;
  1694. }
  1695. /* Decode the extensions block and check it is valid. */
  1696. if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
  1697. !tls1_check_duplicate_extensions(&extensions))
  1698. {
  1699. *out_alert = SSL_AD_DECODE_ERROR;
  1700. return 0;
  1701. }
  1702. while (CBS_len(&extensions) != 0)
  1703. {
  1704. uint16_t type;
  1705. CBS extension;
  1706. /* Decode the next extension. */
  1707. if (!CBS_get_u16(&extensions, &type) ||
  1708. !CBS_get_u16_length_prefixed(&extensions, &extension))
  1709. {
  1710. *out_alert = SSL_AD_DECODE_ERROR;
  1711. return 0;
  1712. }
  1713. if (s->tlsext_debug_cb)
  1714. {
  1715. s->tlsext_debug_cb(s, 1, type, (unsigned char*)CBS_data(&extension),
  1716. CBS_len(&extension), s->tlsext_debug_arg);
  1717. }
  1718. if (type == TLSEXT_TYPE_server_name)
  1719. {
  1720. /* The extension must be empty. */
  1721. if (CBS_len(&extension) != 0)
  1722. {
  1723. *out_alert = SSL_AD_DECODE_ERROR;
  1724. return 0;
  1725. }
  1726. /* We must have sent it in ClientHello. */
  1727. if (s->tlsext_hostname == NULL)
  1728. {
  1729. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  1730. return 0;
  1731. }
  1732. tlsext_servername = 1;
  1733. }
  1734. else if (type == TLSEXT_TYPE_ec_point_formats)
  1735. {
  1736. CBS ec_point_format_list;
  1737. if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
  1738. CBS_len(&extension) != 0)
  1739. {
  1740. *out_alert = SSL_AD_DECODE_ERROR;
  1741. return 0;
  1742. }
  1743. if (!CBS_stow(&ec_point_format_list,
  1744. &s->s3->tmp.peer_ecpointformatlist,
  1745. &s->s3->tmp.peer_ecpointformatlist_length))
  1746. {
  1747. *out_alert = SSL_AD_INTERNAL_ERROR;
  1748. return 0;
  1749. }
  1750. }
  1751. else if (type == TLSEXT_TYPE_session_ticket)
  1752. {
  1753. if (s->tls_session_ticket_ext_cb &&
  1754. !s->tls_session_ticket_ext_cb(s, CBS_data(&extension), CBS_len(&extension),
  1755. s->tls_session_ticket_ext_cb_arg))
  1756. {
  1757. *out_alert = SSL_AD_INTERNAL_ERROR;
  1758. return 0;
  1759. }
  1760. if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0)
  1761. {
  1762. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  1763. return 0;
  1764. }
  1765. s->tlsext_ticket_expected = 1;
  1766. }
  1767. else if (type == TLSEXT_TYPE_status_request)
  1768. {
  1769. /* The extension MUST be empty and may only sent if
  1770. * we've requested a status request message. */
  1771. if (CBS_len(&extension) != 0)
  1772. {
  1773. *out_alert = SSL_AD_DECODE_ERROR;
  1774. return 0;
  1775. }
  1776. if (!s->ocsp_stapling_enabled)
  1777. {
  1778. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  1779. return 0;
  1780. }
  1781. /* Set a flag to expect a CertificateStatus message */
  1782. s->s3->tmp.certificate_status_expected = 1;
  1783. }
  1784. else if (type == TLSEXT_TYPE_next_proto_neg && s->s3->tmp.finish_md_len == 0) {
  1785. unsigned char *selected;
  1786. unsigned char selected_len;
  1787. /* We must have requested it. */
  1788. if (s->ctx->next_proto_select_cb == NULL)
  1789. {
  1790. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  1791. return 0;
  1792. }
  1793. /* The data must be valid. */
  1794. if (!ssl_next_proto_validate(&extension))
  1795. {
  1796. *out_alert = SSL_AD_DECODE_ERROR;
  1797. return 0;
  1798. }
  1799. if (s->ctx->next_proto_select_cb(s, &selected, &selected_len,
  1800. CBS_data(&extension), CBS_len(&extension),
  1801. s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
  1802. {
  1803. *out_alert = SSL_AD_INTERNAL_ERROR;
  1804. return 0;
  1805. }
  1806. s->next_proto_negotiated = BUF_memdup(selected, selected_len);
  1807. if (s->next_proto_negotiated == NULL)
  1808. {
  1809. *out_alert = SSL_AD_INTERNAL_ERROR;
  1810. return 0;
  1811. }
  1812. s->next_proto_negotiated_len = selected_len;
  1813. s->s3->next_proto_neg_seen = 1;
  1814. }
  1815. else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation)
  1816. {
  1817. CBS protocol_name_list, protocol_name;
  1818. /* We must have requested it. */
  1819. if (s->alpn_client_proto_list == NULL)
  1820. {
  1821. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  1822. return 0;
  1823. }
  1824. /* The extension data consists of a ProtocolNameList
  1825. * which must have exactly one ProtocolName. Each of
  1826. * these is length-prefixed. */
  1827. if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
  1828. CBS_len(&extension) != 0 ||
  1829. !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
  1830. CBS_len(&protocol_name_list) != 0)
  1831. {
  1832. *out_alert = SSL_AD_DECODE_ERROR;
  1833. return 0;
  1834. }
  1835. if (!CBS_stow(&protocol_name,
  1836. &s->s3->alpn_selected,
  1837. &s->s3->alpn_selected_len))
  1838. {
  1839. *out_alert = SSL_AD_INTERNAL_ERROR;
  1840. return 0;
  1841. }
  1842. }
  1843. else if (type == TLSEXT_TYPE_channel_id)
  1844. {
  1845. if (CBS_len(&extension) != 0)
  1846. {
  1847. *out_alert = SSL_AD_DECODE_ERROR;
  1848. return 0;
  1849. }
  1850. s->s3->tlsext_channel_id_valid = 1;
  1851. }
  1852. else if (type == TLSEXT_TYPE_channel_id_new)
  1853. {
  1854. if (CBS_len(&extension) != 0)
  1855. {
  1856. *out_alert = SSL_AD_DECODE_ERROR;
  1857. return 0;
  1858. }
  1859. s->s3->tlsext_channel_id_valid = 1;
  1860. s->s3->tlsext_channel_id_new = 1;
  1861. }
  1862. else if (type == TLSEXT_TYPE_certificate_timestamp)
  1863. {
  1864. if (CBS_len(&extension) == 0)
  1865. {
  1866. *out_alert = SSL_AD_DECODE_ERROR;
  1867. return 0;
  1868. }
  1869. /* Session resumption uses the original session information. */
  1870. if (!s->hit)
  1871. {
  1872. if (!CBS_stow(&extension,
  1873. &s->session->tlsext_signed_cert_timestamp_list,
  1874. &s->session->tlsext_signed_cert_timestamp_list_length))
  1875. {
  1876. *out_alert = SSL_AD_INTERNAL_ERROR;
  1877. return 0;
  1878. }
  1879. }
  1880. }
  1881. else if (type == TLSEXT_TYPE_renegotiate)
  1882. {
  1883. if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert))
  1884. return 0;
  1885. renegotiate_seen = 1;
  1886. }
  1887. else if (type == TLSEXT_TYPE_use_srtp)
  1888. {
  1889. if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert))
  1890. return 0;
  1891. }
  1892. else if (type == TLSEXT_TYPE_extended_master_secret)
  1893. {
  1894. if (/* It is invalid for the server to select EMS and
  1895. SSLv3. */
  1896. s->version == SSL3_VERSION ||
  1897. CBS_len(&extension) != 0)
  1898. {
  1899. *out_alert = SSL_AD_DECODE_ERROR;
  1900. return 0;
  1901. }
  1902. s->s3->tmp.extended_master_secret = 1;
  1903. }
  1904. }
  1905. if (!s->hit && tlsext_servername == 1)
  1906. {
  1907. if (s->tlsext_hostname)
  1908. {
  1909. if (s->session->tlsext_hostname == NULL)
  1910. {
  1911. s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
  1912. if (!s->session->tlsext_hostname)
  1913. {
  1914. *out_alert = SSL_AD_UNRECOGNIZED_NAME;
  1915. return 0;
  1916. }
  1917. }
  1918. else
  1919. {
  1920. *out_alert = SSL_AD_DECODE_ERROR;
  1921. return 0;
  1922. }
  1923. }
  1924. }
  1925. ri_check:
  1926. /* Determine if we need to see RI. Strictly speaking if we want to
  1927. * avoid an attack we should *always* see RI even on initial server
  1928. * hello because the client doesn't see any renegotiation during an
  1929. * attack. However this would mean we could not connect to any server
  1930. * which doesn't support RI so for the immediate future tolerate RI
  1931. * absence on initial connect only.
  1932. */
  1933. if (!renegotiate_seen
  1934. && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
  1935. && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
  1936. {
  1937. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  1938. OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  1939. return 0;
  1940. }
  1941. return 1;
  1942. }
  1943. int ssl_prepare_clienthello_tlsext(SSL *s)
  1944. {
  1945. return 1;
  1946. }
  1947. int ssl_prepare_serverhello_tlsext(SSL *s)
  1948. {
  1949. return 1;
  1950. }
  1951. static int ssl_check_clienthello_tlsext(SSL *s)
  1952. {
  1953. int ret=SSL_TLSEXT_ERR_NOACK;
  1954. int al = SSL_AD_UNRECOGNIZED_NAME;
  1955. /* The handling of the ECPointFormats extension is done elsewhere, namely in
  1956. * ssl3_choose_cipher in s3_lib.c.
  1957. */
  1958. /* The handling of the EllipticCurves extension is done elsewhere, namely in
  1959. * ssl3_choose_cipher in s3_lib.c.
  1960. */
  1961. if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
  1962. ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
  1963. else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
  1964. ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
  1965. switch (ret)
  1966. {
  1967. case SSL_TLSEXT_ERR_ALERT_FATAL:
  1968. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1969. return -1;
  1970. case SSL_TLSEXT_ERR_ALERT_WARNING:
  1971. ssl3_send_alert(s,SSL3_AL_WARNING,al);
  1972. return 1;
  1973. case SSL_TLSEXT_ERR_NOACK:
  1974. s->should_ack_sni = 0;
  1975. return 1;
  1976. default:
  1977. return 1;
  1978. }
  1979. }
  1980. static int ssl_check_serverhello_tlsext(SSL *s)
  1981. {
  1982. int ret=SSL_TLSEXT_ERR_NOACK;
  1983. int al = SSL_AD_UNRECOGNIZED_NAME;
  1984. /* If we are client and using an elliptic curve cryptography cipher
  1985. * suite, then if server returns an EC point formats lists extension
  1986. * it must contain uncompressed.
  1987. */
  1988. unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1989. unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1990. if (((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)) &&
  1991. !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed))
  1992. {
  1993. OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
  1994. return -1;
  1995. }
  1996. ret = SSL_TLSEXT_ERR_OK;
  1997. if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
  1998. ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
  1999. else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
  2000. ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
  2001. switch (ret)
  2002. {
  2003. case SSL_TLSEXT_ERR_ALERT_FATAL:
  2004. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  2005. return -1;
  2006. case SSL_TLSEXT_ERR_ALERT_WARNING:
  2007. ssl3_send_alert(s,SSL3_AL_WARNING,al);
  2008. return 1;
  2009. default:
  2010. return 1;
  2011. }
  2012. }
  2013. int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs)
  2014. {
  2015. int alert = -1;
  2016. if (s->version < SSL3_VERSION)
  2017. return 1;
  2018. if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0)
  2019. {
  2020. ssl3_send_alert(s, SSL3_AL_FATAL, alert);
  2021. return 0;
  2022. }
  2023. if (ssl_check_serverhello_tlsext(s) <= 0)
  2024. {
  2025. OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext, SSL_R_SERVERHELLO_TLSEXT);
  2026. return 0;
  2027. }
  2028. return 1;
  2029. }
  2030. /* Since the server cache lookup is done early on in the processing of the
  2031. * ClientHello, and other operations depend on the result, we need to handle
  2032. * any TLS session ticket extension at the same time.
  2033. *
  2034. * ctx: contains the early callback context, which is the result of a
  2035. * shallow parse of the ClientHello.
  2036. * ret: (output) on return, if a ticket was decrypted, then this is set to
  2037. * point to the resulting session.
  2038. *
  2039. * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
  2040. * ciphersuite, in which case we have no use for session tickets and one will
  2041. * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
  2042. *
  2043. * Returns:
  2044. * -1: fatal error, either from parsing or decrypting the ticket.
  2045. * 0: no ticket was found (or was ignored, based on settings).
  2046. * 1: a zero length extension was found, indicating that the client supports
  2047. * session tickets but doesn't currently have one to offer.
  2048. * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
  2049. * couldn't be decrypted because of a non-fatal error.
  2050. * 3: a ticket was successfully decrypted and *ret was set.
  2051. *
  2052. * Side effects:
  2053. * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
  2054. * a new session ticket to the client because the client indicated support
  2055. * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
  2056. * a session ticket or we couldn't use the one it gave us, or if
  2057. * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
  2058. * Otherwise, s->tlsext_ticket_expected is set to 0.
  2059. */
  2060. int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
  2061. SSL_SESSION **ret)
  2062. {
  2063. *ret = NULL;
  2064. s->tlsext_ticket_expected = 0;
  2065. const unsigned char *data;
  2066. size_t len;
  2067. int r;
  2068. /* If tickets disabled behave as if no ticket present
  2069. * to permit stateful resumption.
  2070. */
  2071. if (SSL_get_options(s) & SSL_OP_NO_TICKET)
  2072. return 0;
  2073. if ((s->version <= SSL3_VERSION) && !ctx->extensions)
  2074. return 0;
  2075. if (!SSL_early_callback_ctx_extension_get(
  2076. ctx, TLSEXT_TYPE_session_ticket, &data, &len))
  2077. {
  2078. return 0;
  2079. }
  2080. if (len == 0)
  2081. {
  2082. /* The client will accept a ticket but doesn't
  2083. * currently have one. */
  2084. s->tlsext_ticket_expected = 1;
  2085. return 1;
  2086. }
  2087. if (s->tls_session_secret_cb)
  2088. {
  2089. /* Indicate that the ticket couldn't be
  2090. * decrypted rather than generating the session
  2091. * from ticket now, trigger abbreviated
  2092. * handshake based on external mechanism to
  2093. * calculate the master secret later. */
  2094. return 2;
  2095. }
  2096. r = tls_decrypt_ticket(s, data, len, ctx->session_id,
  2097. ctx->session_id_len, ret);
  2098. switch (r)
  2099. {
  2100. case 2: /* ticket couldn't be decrypted */
  2101. s->tlsext_ticket_expected = 1;
  2102. return 2;
  2103. case 3: /* ticket was decrypted */
  2104. return r;
  2105. case 4: /* ticket decrypted but need to renew */
  2106. s->tlsext_ticket_expected = 1;
  2107. return 3;
  2108. default: /* fatal error */
  2109. return -1;
  2110. }
  2111. }
  2112. /* tls_decrypt_ticket attempts to decrypt a session ticket.
  2113. *
  2114. * etick: points to the body of the session ticket extension.
  2115. * eticklen: the length of the session tickets extenion.
  2116. * sess_id: points at the session ID.
  2117. * sesslen: the length of the session ID.
  2118. * psess: (output) on return, if a ticket was decrypted, then this is set to
  2119. * point to the resulting session.
  2120. *
  2121. * Returns:
  2122. * -1: fatal error, either from parsing or decrypting the ticket.
  2123. * 2: the ticket couldn't be decrypted.
  2124. * 3: a ticket was successfully decrypted and *psess was set.
  2125. * 4: same as 3, but the ticket needs to be renewed.
  2126. */
  2127. static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
  2128. const unsigned char *sess_id, int sesslen,
  2129. SSL_SESSION **psess)
  2130. {
  2131. SSL_SESSION *sess;
  2132. unsigned char *sdec;
  2133. const unsigned char *p;
  2134. int slen, mlen, renew_ticket = 0;
  2135. unsigned char tick_hmac[EVP_MAX_MD_SIZE];
  2136. HMAC_CTX hctx;
  2137. EVP_CIPHER_CTX ctx;
  2138. SSL_CTX *tctx = s->initial_ctx;
  2139. /* Need at least keyname + iv + some encrypted data */
  2140. if (eticklen < 48)
  2141. return 2;
  2142. /* Initialize session ticket encryption and HMAC contexts */
  2143. HMAC_CTX_init(&hctx);
  2144. EVP_CIPHER_CTX_init(&ctx);
  2145. if (tctx->tlsext_ticket_key_cb)
  2146. {
  2147. unsigned char *nctick = (unsigned char *)etick;
  2148. int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
  2149. &ctx, &hctx, 0);
  2150. if (rv < 0)
  2151. return -1;
  2152. if (rv == 0)
  2153. return 2;
  2154. if (rv == 2)
  2155. renew_ticket = 1;
  2156. }
  2157. else
  2158. {
  2159. /* Check key name matches */
  2160. if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
  2161. return 2;
  2162. HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
  2163. tlsext_tick_md(), NULL);
  2164. EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
  2165. tctx->tlsext_tick_aes_key, etick + 16);
  2166. }
  2167. /* Attempt to process session ticket, first conduct sanity and
  2168. * integrity checks on ticket.
  2169. */
  2170. mlen = HMAC_size(&hctx);
  2171. if (mlen < 0)
  2172. {
  2173. EVP_CIPHER_CTX_cleanup(&ctx);
  2174. return -1;
  2175. }
  2176. eticklen -= mlen;
  2177. /* Check HMAC of encrypted ticket */
  2178. HMAC_Update(&hctx, etick, eticklen);
  2179. HMAC_Final(&hctx, tick_hmac, NULL);
  2180. HMAC_CTX_cleanup(&hctx);
  2181. if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen))
  2182. {
  2183. EVP_CIPHER_CTX_cleanup(&ctx);
  2184. return 2;
  2185. }
  2186. /* Attempt to decrypt session data */
  2187. /* Move p after IV to start of encrypted ticket, update length */
  2188. p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
  2189. eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
  2190. sdec = OPENSSL_malloc(eticklen);
  2191. if (!sdec)
  2192. {
  2193. EVP_CIPHER_CTX_cleanup(&ctx);
  2194. return -1;
  2195. }
  2196. EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
  2197. if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0)
  2198. {
  2199. EVP_CIPHER_CTX_cleanup(&ctx);
  2200. OPENSSL_free(sdec);
  2201. return 2;
  2202. }
  2203. slen += mlen;
  2204. EVP_CIPHER_CTX_cleanup(&ctx);
  2205. p = sdec;
  2206. sess = d2i_SSL_SESSION(NULL, &p, slen);
  2207. OPENSSL_free(sdec);
  2208. if (sess)
  2209. {
  2210. /* The session ID, if non-empty, is used by some clients to
  2211. * detect that the ticket has been accepted. So we copy it to
  2212. * the session structure. If it is empty set length to zero
  2213. * as required by standard.
  2214. */
  2215. if (sesslen)
  2216. memcpy(sess->session_id, sess_id, sesslen);
  2217. sess->session_id_length = sesslen;
  2218. *psess = sess;
  2219. if (renew_ticket)
  2220. return 4;
  2221. else
  2222. return 3;
  2223. }
  2224. ERR_clear_error();
  2225. /* For session parse failure, indicate that we need to send a new
  2226. * ticket. */
  2227. return 2;
  2228. }
  2229. /* Tables to translate from NIDs to TLS v1.2 ids */
  2230. typedef struct
  2231. {
  2232. int nid;
  2233. int id;
  2234. } tls12_lookup;
  2235. static const tls12_lookup tls12_md[] = {
  2236. {NID_md5, TLSEXT_hash_md5},
  2237. {NID_sha1, TLSEXT_hash_sha1},
  2238. {NID_sha224, TLSEXT_hash_sha224},
  2239. {NID_sha256, TLSEXT_hash_sha256},
  2240. {NID_sha384, TLSEXT_hash_sha384},
  2241. {NID_sha512, TLSEXT_hash_sha512}
  2242. };
  2243. static const tls12_lookup tls12_sig[] = {
  2244. {EVP_PKEY_RSA, TLSEXT_signature_rsa},
  2245. {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
  2246. };
  2247. static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
  2248. {
  2249. size_t i;
  2250. for (i = 0; i < tlen; i++)
  2251. {
  2252. if (table[i].nid == nid)
  2253. return table[i].id;
  2254. }
  2255. return -1;
  2256. }
  2257. static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
  2258. {
  2259. size_t i;
  2260. for (i = 0; i < tlen; i++)
  2261. {
  2262. if ((table[i].id) == id)
  2263. return table[i].nid;
  2264. }
  2265. return NID_undef;
  2266. }
  2267. int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
  2268. {
  2269. int sig_id, md_id;
  2270. if (!md)
  2271. return 0;
  2272. md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
  2273. sizeof(tls12_md)/sizeof(tls12_lookup));
  2274. if (md_id == -1)
  2275. return 0;
  2276. sig_id = tls12_get_sigid(pk);
  2277. if (sig_id == -1)
  2278. return 0;
  2279. p[0] = (unsigned char)md_id;
  2280. p[1] = (unsigned char)sig_id;
  2281. return 1;
  2282. }
  2283. int tls12_get_sigid(const EVP_PKEY *pk)
  2284. {
  2285. return tls12_find_id(pk->type, tls12_sig,
  2286. sizeof(tls12_sig)/sizeof(tls12_lookup));
  2287. }
  2288. const EVP_MD *tls12_get_hash(unsigned char hash_alg)
  2289. {
  2290. switch(hash_alg)
  2291. {
  2292. case TLSEXT_hash_md5:
  2293. return EVP_md5();
  2294. case TLSEXT_hash_sha1:
  2295. return EVP_sha1();
  2296. case TLSEXT_hash_sha224:
  2297. return EVP_sha224();
  2298. case TLSEXT_hash_sha256:
  2299. return EVP_sha256();
  2300. case TLSEXT_hash_sha384:
  2301. return EVP_sha384();
  2302. case TLSEXT_hash_sha512:
  2303. return EVP_sha512();
  2304. default:
  2305. return NULL;
  2306. }
  2307. }
  2308. static int tls12_get_pkey_idx(unsigned char sig_alg)
  2309. {
  2310. switch(sig_alg)
  2311. {
  2312. case TLSEXT_signature_rsa:
  2313. return SSL_PKEY_RSA_SIGN;
  2314. case TLSEXT_signature_ecdsa:
  2315. return SSL_PKEY_ECC;
  2316. }
  2317. return -1;
  2318. }
  2319. /* Convert TLS 1.2 signature algorithm extension values into NIDs */
  2320. static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
  2321. int *psignhash_nid, const unsigned char *data)
  2322. {
  2323. int sign_nid = 0, hash_nid = 0;
  2324. if (!phash_nid && !psign_nid && !psignhash_nid)
  2325. return;
  2326. if (phash_nid || psignhash_nid)
  2327. {
  2328. hash_nid = tls12_find_nid(data[0], tls12_md,
  2329. sizeof(tls12_md)/sizeof(tls12_lookup));
  2330. if (phash_nid)
  2331. *phash_nid = hash_nid;
  2332. }
  2333. if (psign_nid || psignhash_nid)
  2334. {
  2335. sign_nid = tls12_find_nid(data[1], tls12_sig,
  2336. sizeof(tls12_sig)/sizeof(tls12_lookup));
  2337. if (psign_nid)
  2338. *psign_nid = sign_nid;
  2339. }
  2340. if (psignhash_nid)
  2341. {
  2342. if (sign_nid && hash_nid)
  2343. OBJ_find_sigid_by_algs(psignhash_nid,
  2344. hash_nid, sign_nid);
  2345. else
  2346. *psignhash_nid = NID_undef;
  2347. }
  2348. }
  2349. /* Given preference and allowed sigalgs set shared sigalgs */
  2350. static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig,
  2351. const unsigned char *pref, size_t preflen,
  2352. const unsigned char *allow, size_t allowlen)
  2353. {
  2354. const unsigned char *ptmp, *atmp;
  2355. size_t i, j, nmatch = 0;
  2356. for (i = 0, ptmp = pref; i < preflen; i+=2, ptmp+=2)
  2357. {
  2358. /* Skip disabled hashes or signature algorithms */
  2359. if (tls12_get_hash(ptmp[0]) == NULL)
  2360. continue;
  2361. if (tls12_get_pkey_idx(ptmp[1]) == -1)
  2362. continue;
  2363. for (j = 0, atmp = allow; j < allowlen; j+=2, atmp+=2)
  2364. {
  2365. if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1])
  2366. {
  2367. nmatch++;
  2368. if (shsig)
  2369. {
  2370. shsig->rhash = ptmp[0];
  2371. shsig->rsign = ptmp[1];
  2372. tls1_lookup_sigalg(&shsig->hash_nid,
  2373. &shsig->sign_nid,
  2374. &shsig->signandhash_nid,
  2375. ptmp);
  2376. shsig++;
  2377. }
  2378. break;
  2379. }
  2380. }
  2381. }
  2382. return nmatch;
  2383. }
  2384. /* Set shared signature algorithms for SSL structures */
  2385. static int tls1_set_shared_sigalgs(SSL *s)
  2386. {
  2387. const unsigned char *pref, *allow, *conf;
  2388. size_t preflen, allowlen, conflen;
  2389. size_t nmatch;
  2390. TLS_SIGALGS *salgs = NULL;
  2391. CERT *c = s->cert;
  2392. if (c->shared_sigalgs)
  2393. {
  2394. OPENSSL_free(c->shared_sigalgs);
  2395. c->shared_sigalgs = NULL;
  2396. }
  2397. /* If client use client signature algorithms if not NULL */
  2398. if (!s->server && c->client_sigalgs)
  2399. {
  2400. conf = c->client_sigalgs;
  2401. conflen = c->client_sigalgslen;
  2402. }
  2403. else if (c->conf_sigalgs)
  2404. {
  2405. conf = c->conf_sigalgs;
  2406. conflen = c->conf_sigalgslen;
  2407. }
  2408. else
  2409. conflen = tls12_get_psigalgs(s, &conf);
  2410. if(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
  2411. {
  2412. pref = conf;
  2413. preflen = conflen;
  2414. allow = c->peer_sigalgs;
  2415. allowlen = c->peer_sigalgslen;
  2416. }
  2417. else
  2418. {
  2419. allow = conf;
  2420. allowlen = conflen;
  2421. pref = c->peer_sigalgs;
  2422. preflen = c->peer_sigalgslen;
  2423. }
  2424. nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
  2425. if (!nmatch)
  2426. return 1;
  2427. salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
  2428. if (!salgs)
  2429. return 0;
  2430. nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
  2431. c->shared_sigalgs = salgs;
  2432. c->shared_sigalgslen = nmatch;
  2433. return 1;
  2434. }
  2435. /* Set preferred digest for each key type */
  2436. int tls1_process_sigalgs(SSL *s, const CBS *sigalgs)
  2437. {
  2438. int idx;
  2439. size_t i;
  2440. const EVP_MD *md;
  2441. CERT *c = s->cert;
  2442. TLS_SIGALGS *sigptr;
  2443. /* Extension ignored for inappropriate versions */
  2444. if (!SSL_USE_SIGALGS(s))
  2445. return 1;
  2446. /* Length must be even */
  2447. if (CBS_len(sigalgs) % 2 != 0)
  2448. return 0;
  2449. /* Should never happen */
  2450. if (!c)
  2451. return 0;
  2452. if (!CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen))
  2453. return 0;
  2454. tls1_set_shared_sigalgs(s);
  2455. for (i = 0, sigptr = c->shared_sigalgs;
  2456. i < c->shared_sigalgslen; i++, sigptr++)
  2457. {
  2458. idx = tls12_get_pkey_idx(sigptr->rsign);
  2459. if (idx > 0 && c->pkeys[idx].digest == NULL)
  2460. {
  2461. md = tls12_get_hash(sigptr->rhash);
  2462. c->pkeys[idx].digest = md;
  2463. c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
  2464. if (idx == SSL_PKEY_RSA_SIGN)
  2465. {
  2466. c->pkeys[SSL_PKEY_RSA_ENC].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
  2467. c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
  2468. }
  2469. }
  2470. }
  2471. /* In strict mode leave unset digests as NULL to indicate we can't
  2472. * use the certificate for signing.
  2473. */
  2474. if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
  2475. {
  2476. /* Set any remaining keys to default values. NOTE: if alg is
  2477. * not supported it stays as NULL.
  2478. */
  2479. if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest)
  2480. {
  2481. c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
  2482. c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
  2483. }
  2484. if (!c->pkeys[SSL_PKEY_ECC].digest)
  2485. c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
  2486. }
  2487. return 1;
  2488. }
  2489. int SSL_get_sigalgs(SSL *s, int idx,
  2490. int *psign, int *phash, int *psignhash,
  2491. unsigned char *rsig, unsigned char *rhash)
  2492. {
  2493. const unsigned char *psig = s->cert->peer_sigalgs;
  2494. if (psig == NULL)
  2495. return 0;
  2496. if (idx >= 0)
  2497. {
  2498. idx <<= 1;
  2499. if (idx >= (int)s->cert->peer_sigalgslen)
  2500. return 0;
  2501. psig += idx;
  2502. if (rhash)
  2503. *rhash = psig[0];
  2504. if (rsig)
  2505. *rsig = psig[1];
  2506. tls1_lookup_sigalg(phash, psign, psignhash, psig);
  2507. }
  2508. return s->cert->peer_sigalgslen / 2;
  2509. }
  2510. int SSL_get_shared_sigalgs(SSL *s, int idx,
  2511. int *psign, int *phash, int *psignhash,
  2512. unsigned char *rsig, unsigned char *rhash)
  2513. {
  2514. TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
  2515. if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
  2516. return 0;
  2517. shsigalgs += idx;
  2518. if (phash)
  2519. *phash = shsigalgs->hash_nid;
  2520. if (psign)
  2521. *psign = shsigalgs->sign_nid;
  2522. if (psignhash)
  2523. *psignhash = shsigalgs->signandhash_nid;
  2524. if (rsig)
  2525. *rsig = shsigalgs->rsign;
  2526. if (rhash)
  2527. *rhash = shsigalgs->rhash;
  2528. return s->cert->shared_sigalgslen;
  2529. }
  2530. /* tls1_channel_id_hash calculates the signed data for a Channel ID on the given
  2531. * SSL connection and writes it to |md|. */
  2532. int
  2533. tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s)
  2534. {
  2535. EVP_MD_CTX ctx;
  2536. unsigned char temp_digest[EVP_MAX_MD_SIZE];
  2537. unsigned temp_digest_len;
  2538. int i;
  2539. static const char kClientIDMagic[] = "TLS Channel ID signature";
  2540. if (s->s3->handshake_buffer)
  2541. if (!ssl3_digest_cached_records(s, free_handshake_buffer))
  2542. return 0;
  2543. EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
  2544. if (s->hit && s->s3->tlsext_channel_id_new)
  2545. {
  2546. static const char kResumptionMagic[] = "Resumption";
  2547. EVP_DigestUpdate(md, kResumptionMagic,
  2548. sizeof(kResumptionMagic));
  2549. if (s->session->original_handshake_hash_len == 0)
  2550. return 0;
  2551. EVP_DigestUpdate(md, s->session->original_handshake_hash,
  2552. s->session->original_handshake_hash_len);
  2553. }
  2554. EVP_MD_CTX_init(&ctx);
  2555. for (i = 0; i < SSL_MAX_DIGEST; i++)
  2556. {
  2557. if (s->s3->handshake_dgst[i] == NULL)
  2558. continue;
  2559. EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i]);
  2560. EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
  2561. EVP_DigestUpdate(md, temp_digest, temp_digest_len);
  2562. }
  2563. EVP_MD_CTX_cleanup(&ctx);
  2564. return 1;
  2565. }
  2566. /* tls1_record_handshake_hashes_for_channel_id records the current handshake
  2567. * hashes in |s->session| so that Channel ID resumptions can sign that data. */
  2568. int tls1_record_handshake_hashes_for_channel_id(SSL *s)
  2569. {
  2570. int digest_len;
  2571. /* This function should never be called for a resumed session because
  2572. * the handshake hashes that we wish to record are for the original,
  2573. * full handshake. */
  2574. if (s->hit)
  2575. return -1;
  2576. /* It only makes sense to call this function if Channel IDs have been
  2577. * negotiated. */
  2578. if (!s->s3->tlsext_channel_id_new)
  2579. return -1;
  2580. digest_len = tls1_handshake_digest(
  2581. s, s->session->original_handshake_hash,
  2582. sizeof(s->session->original_handshake_hash));
  2583. if (digest_len < 0)
  2584. return -1;
  2585. s->session->original_handshake_hash_len = digest_len;
  2586. return 1;
  2587. }
  2588. int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
  2589. {
  2590. unsigned char *sigalgs, *sptr;
  2591. int rhash, rsign;
  2592. size_t i;
  2593. if (salglen & 1)
  2594. return 0;
  2595. sigalgs = OPENSSL_malloc(salglen);
  2596. if (sigalgs == NULL)
  2597. return 0;
  2598. for (i = 0, sptr = sigalgs; i < salglen; i+=2)
  2599. {
  2600. rhash = tls12_find_id(*psig_nids++, tls12_md,
  2601. sizeof(tls12_md)/sizeof(tls12_lookup));
  2602. rsign = tls12_find_id(*psig_nids++, tls12_sig,
  2603. sizeof(tls12_sig)/sizeof(tls12_lookup));
  2604. if (rhash == -1 || rsign == -1)
  2605. goto err;
  2606. *sptr++ = rhash;
  2607. *sptr++ = rsign;
  2608. }
  2609. if (client)
  2610. {
  2611. if (c->client_sigalgs)
  2612. OPENSSL_free(c->client_sigalgs);
  2613. c->client_sigalgs = sigalgs;
  2614. c->client_sigalgslen = salglen;
  2615. }
  2616. else
  2617. {
  2618. if (c->conf_sigalgs)
  2619. OPENSSL_free(c->conf_sigalgs);
  2620. c->conf_sigalgs = sigalgs;
  2621. c->conf_sigalgslen = salglen;
  2622. }
  2623. return 1;
  2624. err:
  2625. OPENSSL_free(sigalgs);
  2626. return 0;
  2627. }
  2628. static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
  2629. {
  2630. int sig_nid;
  2631. size_t i;
  2632. if (default_nid == -1)
  2633. return 1;
  2634. sig_nid = X509_get_signature_nid(x);
  2635. if (default_nid)
  2636. return sig_nid == default_nid ? 1 : 0;
  2637. for (i = 0; i < c->shared_sigalgslen; i++)
  2638. if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
  2639. return 1;
  2640. return 0;
  2641. }
  2642. /* Check to see if a certificate issuer name matches list of CA names */
  2643. static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
  2644. {
  2645. X509_NAME *nm;
  2646. size_t i;
  2647. nm = X509_get_issuer_name(x);
  2648. for (i = 0; i < sk_X509_NAME_num(names); i++)
  2649. {
  2650. if(!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
  2651. return 1;
  2652. }
  2653. return 0;
  2654. }
  2655. /* Check certificate chain is consistent with TLS extensions and is
  2656. * usable by server. This servers two purposes: it allows users to
  2657. * check chains before passing them to the server and it allows the
  2658. * server to check chains before attempting to use them.
  2659. */
  2660. /* Flags which need to be set for a certificate when stict mode not set */
  2661. #define CERT_PKEY_VALID_FLAGS \
  2662. (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
  2663. /* Strict mode flags */
  2664. #define CERT_PKEY_STRICT_FLAGS \
  2665. (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
  2666. | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
  2667. int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
  2668. int idx)
  2669. {
  2670. size_t i;
  2671. int rv = 0;
  2672. int check_flags = 0, strict_mode;
  2673. CERT_PKEY *cpk = NULL;
  2674. CERT *c = s->cert;
  2675. /* idx == -1 means checking server chains */
  2676. if (idx != -1)
  2677. {
  2678. /* idx == -2 means checking client certificate chains */
  2679. if (idx == -2)
  2680. {
  2681. cpk = c->key;
  2682. idx = cpk - c->pkeys;
  2683. }
  2684. else
  2685. cpk = c->pkeys + idx;
  2686. x = cpk->x509;
  2687. pk = cpk->privatekey;
  2688. chain = cpk->chain;
  2689. strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
  2690. /* If no cert or key, forget it */
  2691. if (!x || !pk)
  2692. goto end;
  2693. }
  2694. else
  2695. {
  2696. if (!x || !pk)
  2697. goto end;
  2698. idx = ssl_cert_type(x, pk);
  2699. if (idx == -1)
  2700. goto end;
  2701. cpk = c->pkeys + idx;
  2702. if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
  2703. check_flags = CERT_PKEY_STRICT_FLAGS;
  2704. else
  2705. check_flags = CERT_PKEY_VALID_FLAGS;
  2706. strict_mode = 1;
  2707. }
  2708. /* Check all signature algorithms are consistent with
  2709. * signature algorithms extension if TLS 1.2 or later
  2710. * and strict mode.
  2711. */
  2712. if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode)
  2713. {
  2714. int default_nid;
  2715. unsigned char rsign = 0;
  2716. if (c->peer_sigalgs)
  2717. default_nid = 0;
  2718. /* If no sigalgs extension use defaults from RFC5246 */
  2719. else
  2720. {
  2721. switch(idx)
  2722. {
  2723. case SSL_PKEY_RSA_ENC:
  2724. case SSL_PKEY_RSA_SIGN:
  2725. rsign = TLSEXT_signature_rsa;
  2726. default_nid = NID_sha1WithRSAEncryption;
  2727. break;
  2728. case SSL_PKEY_ECC:
  2729. rsign = TLSEXT_signature_ecdsa;
  2730. default_nid = NID_ecdsa_with_SHA1;
  2731. break;
  2732. default:
  2733. default_nid = -1;
  2734. break;
  2735. }
  2736. }
  2737. /* If peer sent no signature algorithms extension and we
  2738. * have set preferred signature algorithms check we support
  2739. * sha1.
  2740. */
  2741. if (default_nid > 0 && c->conf_sigalgs)
  2742. {
  2743. size_t j;
  2744. const unsigned char *p = c->conf_sigalgs;
  2745. for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2)
  2746. {
  2747. if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
  2748. break;
  2749. }
  2750. if (j == c->conf_sigalgslen)
  2751. {
  2752. if (check_flags)
  2753. goto skip_sigs;
  2754. else
  2755. goto end;
  2756. }
  2757. }
  2758. /* Check signature algorithm of each cert in chain */
  2759. if (!tls1_check_sig_alg(c, x, default_nid))
  2760. {
  2761. if (!check_flags) goto end;
  2762. }
  2763. else
  2764. rv |= CERT_PKEY_EE_SIGNATURE;
  2765. rv |= CERT_PKEY_CA_SIGNATURE;
  2766. for (i = 0; i < sk_X509_num(chain); i++)
  2767. {
  2768. if (!tls1_check_sig_alg(c, sk_X509_value(chain, i),
  2769. default_nid))
  2770. {
  2771. if (check_flags)
  2772. {
  2773. rv &= ~CERT_PKEY_CA_SIGNATURE;
  2774. break;
  2775. }
  2776. else
  2777. goto end;
  2778. }
  2779. }
  2780. }
  2781. /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
  2782. else if(check_flags)
  2783. rv |= CERT_PKEY_EE_SIGNATURE|CERT_PKEY_CA_SIGNATURE;
  2784. skip_sigs:
  2785. /* Check cert parameters are consistent */
  2786. if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
  2787. rv |= CERT_PKEY_EE_PARAM;
  2788. else if (!check_flags)
  2789. goto end;
  2790. if (!s->server)
  2791. rv |= CERT_PKEY_CA_PARAM;
  2792. /* In strict mode check rest of chain too */
  2793. else if (strict_mode)
  2794. {
  2795. rv |= CERT_PKEY_CA_PARAM;
  2796. for (i = 0; i < sk_X509_num(chain); i++)
  2797. {
  2798. X509 *ca = sk_X509_value(chain, i);
  2799. if (!tls1_check_cert_param(s, ca, 0))
  2800. {
  2801. if (check_flags)
  2802. {
  2803. rv &= ~CERT_PKEY_CA_PARAM;
  2804. break;
  2805. }
  2806. else
  2807. goto end;
  2808. }
  2809. }
  2810. }
  2811. if (!s->server && strict_mode)
  2812. {
  2813. STACK_OF(X509_NAME) *ca_dn;
  2814. uint8_t check_type = 0;
  2815. switch (pk->type)
  2816. {
  2817. case EVP_PKEY_RSA:
  2818. check_type = TLS_CT_RSA_SIGN;
  2819. break;
  2820. case EVP_PKEY_EC:
  2821. check_type = TLS_CT_ECDSA_SIGN;
  2822. break;
  2823. }
  2824. if (check_type)
  2825. {
  2826. if (s->s3->tmp.certificate_types &&
  2827. memchr(s->s3->tmp.certificate_types, check_type, s->s3->tmp.num_certificate_types))
  2828. {
  2829. rv |= CERT_PKEY_CERT_TYPE;
  2830. }
  2831. if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
  2832. goto end;
  2833. }
  2834. else
  2835. rv |= CERT_PKEY_CERT_TYPE;
  2836. ca_dn = s->s3->tmp.ca_names;
  2837. if (!sk_X509_NAME_num(ca_dn))
  2838. rv |= CERT_PKEY_ISSUER_NAME;
  2839. if (!(rv & CERT_PKEY_ISSUER_NAME))
  2840. {
  2841. if (ssl_check_ca_name(ca_dn, x))
  2842. rv |= CERT_PKEY_ISSUER_NAME;
  2843. }
  2844. if (!(rv & CERT_PKEY_ISSUER_NAME))
  2845. {
  2846. for (i = 0; i < sk_X509_num(chain); i++)
  2847. {
  2848. X509 *xtmp = sk_X509_value(chain, i);
  2849. if (ssl_check_ca_name(ca_dn, xtmp))
  2850. {
  2851. rv |= CERT_PKEY_ISSUER_NAME;
  2852. break;
  2853. }
  2854. }
  2855. }
  2856. if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
  2857. goto end;
  2858. }
  2859. else
  2860. rv |= CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE;
  2861. if (!check_flags || (rv & check_flags) == check_flags)
  2862. rv |= CERT_PKEY_VALID;
  2863. end:
  2864. if (TLS1_get_version(s) >= TLS1_2_VERSION)
  2865. {
  2866. if (cpk->valid_flags & CERT_PKEY_EXPLICIT_SIGN)
  2867. rv |= CERT_PKEY_EXPLICIT_SIGN|CERT_PKEY_SIGN;
  2868. else if (cpk->digest)
  2869. rv |= CERT_PKEY_SIGN;
  2870. }
  2871. else
  2872. rv |= CERT_PKEY_SIGN|CERT_PKEY_EXPLICIT_SIGN;
  2873. /* When checking a CERT_PKEY structure all flags are irrelevant
  2874. * if the chain is invalid.
  2875. */
  2876. if (!check_flags)
  2877. {
  2878. if (rv & CERT_PKEY_VALID)
  2879. cpk->valid_flags = rv;
  2880. else
  2881. {
  2882. /* Preserve explicit sign flag, clear rest */
  2883. cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
  2884. return 0;
  2885. }
  2886. }
  2887. return rv;
  2888. }
  2889. /* Set validity of certificates in an SSL structure */
  2890. void tls1_set_cert_validity(SSL *s)
  2891. {
  2892. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
  2893. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
  2894. tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
  2895. }
  2896. /* User level utiity function to check a chain is suitable */
  2897. int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
  2898. {
  2899. return tls1_check_chain(s, x, pk, chain, -1);
  2900. }