t1_lib.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695
  1. /* ssl/t1_lib.c */
  2. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson ([email protected]).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young ([email protected])"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson ([email protected])"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * [email protected].
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * ([email protected]). This product includes software written by Tim
  108. * Hudson ([email protected]).
  109. *
  110. */
  111. #include <stdio.h>
  112. #include <openssl/objects.h>
  113. #include <openssl/evp.h>
  114. #include <openssl/hmac.h>
  115. #include <openssl/ocsp.h>
  116. #include <openssl/rand.h>
  117. #include "ssl_locl.h"
  118. const char tls1_version_str[]="TLSv1" OPENSSL_VERSION_PTEXT;
  119. #ifndef OPENSSL_NO_TLSEXT
  120. static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
  121. const unsigned char *sess_id, int sesslen,
  122. SSL_SESSION **psess);
  123. #endif
  124. SSL3_ENC_METHOD TLSv1_enc_data={
  125. tls1_enc,
  126. tls1_mac,
  127. tls1_setup_key_block,
  128. tls1_generate_master_secret,
  129. tls1_change_cipher_state,
  130. tls1_final_finish_mac,
  131. TLS1_FINISH_MAC_LENGTH,
  132. tls1_cert_verify_mac,
  133. TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
  134. TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
  135. tls1_alert_code,
  136. tls1_export_keying_material,
  137. };
  138. long tls1_default_timeout(void)
  139. {
  140. /* 2 hours, the 24 hours mentioned in the TLSv1 spec
  141. * is way too long for http, the cache would over fill */
  142. return(60*60*2);
  143. }
  144. int tls1_new(SSL *s)
  145. {
  146. if (!ssl3_new(s)) return(0);
  147. s->method->ssl_clear(s);
  148. return(1);
  149. }
  150. void tls1_free(SSL *s)
  151. {
  152. #ifndef OPENSSL_NO_TLSEXT
  153. if (s->tlsext_session_ticket)
  154. {
  155. OPENSSL_free(s->tlsext_session_ticket);
  156. }
  157. #endif /* OPENSSL_NO_TLSEXT */
  158. ssl3_free(s);
  159. }
  160. void tls1_clear(SSL *s)
  161. {
  162. ssl3_clear(s);
  163. s->version = s->method->version;
  164. }
  165. #ifndef OPENSSL_NO_EC
  166. static int nid_list[] =
  167. {
  168. NID_sect163k1, /* sect163k1 (1) */
  169. NID_sect163r1, /* sect163r1 (2) */
  170. NID_sect163r2, /* sect163r2 (3) */
  171. NID_sect193r1, /* sect193r1 (4) */
  172. NID_sect193r2, /* sect193r2 (5) */
  173. NID_sect233k1, /* sect233k1 (6) */
  174. NID_sect233r1, /* sect233r1 (7) */
  175. NID_sect239k1, /* sect239k1 (8) */
  176. NID_sect283k1, /* sect283k1 (9) */
  177. NID_sect283r1, /* sect283r1 (10) */
  178. NID_sect409k1, /* sect409k1 (11) */
  179. NID_sect409r1, /* sect409r1 (12) */
  180. NID_sect571k1, /* sect571k1 (13) */
  181. NID_sect571r1, /* sect571r1 (14) */
  182. NID_secp160k1, /* secp160k1 (15) */
  183. NID_secp160r1, /* secp160r1 (16) */
  184. NID_secp160r2, /* secp160r2 (17) */
  185. NID_secp192k1, /* secp192k1 (18) */
  186. NID_X9_62_prime192v1, /* secp192r1 (19) */
  187. NID_secp224k1, /* secp224k1 (20) */
  188. NID_secp224r1, /* secp224r1 (21) */
  189. NID_secp256k1, /* secp256k1 (22) */
  190. NID_X9_62_prime256v1, /* secp256r1 (23) */
  191. NID_secp384r1, /* secp384r1 (24) */
  192. NID_secp521r1 /* secp521r1 (25) */
  193. };
  194. static int pref_list[] =
  195. {
  196. NID_sect571r1, /* sect571r1 (14) */
  197. NID_sect571k1, /* sect571k1 (13) */
  198. NID_secp521r1, /* secp521r1 (25) */
  199. NID_sect409k1, /* sect409k1 (11) */
  200. NID_sect409r1, /* sect409r1 (12) */
  201. NID_secp384r1, /* secp384r1 (24) */
  202. NID_sect283k1, /* sect283k1 (9) */
  203. NID_sect283r1, /* sect283r1 (10) */
  204. NID_secp256k1, /* secp256k1 (22) */
  205. NID_X9_62_prime256v1, /* secp256r1 (23) */
  206. NID_sect239k1, /* sect239k1 (8) */
  207. NID_sect233k1, /* sect233k1 (6) */
  208. NID_sect233r1, /* sect233r1 (7) */
  209. NID_secp224k1, /* secp224k1 (20) */
  210. NID_secp224r1, /* secp224r1 (21) */
  211. NID_sect193r1, /* sect193r1 (4) */
  212. NID_sect193r2, /* sect193r2 (5) */
  213. NID_secp192k1, /* secp192k1 (18) */
  214. NID_X9_62_prime192v1, /* secp192r1 (19) */
  215. NID_sect163k1, /* sect163k1 (1) */
  216. NID_sect163r1, /* sect163r1 (2) */
  217. NID_sect163r2, /* sect163r2 (3) */
  218. NID_secp160k1, /* secp160k1 (15) */
  219. NID_secp160r1, /* secp160r1 (16) */
  220. NID_secp160r2, /* secp160r2 (17) */
  221. };
  222. int tls1_ec_curve_id2nid(int curve_id)
  223. {
  224. /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
  225. if ((curve_id < 1) || ((unsigned int)curve_id >
  226. sizeof(nid_list)/sizeof(nid_list[0])))
  227. return 0;
  228. return nid_list[curve_id-1];
  229. }
  230. int tls1_ec_nid2curve_id(int nid)
  231. {
  232. /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
  233. switch (nid)
  234. {
  235. case NID_sect163k1: /* sect163k1 (1) */
  236. return 1;
  237. case NID_sect163r1: /* sect163r1 (2) */
  238. return 2;
  239. case NID_sect163r2: /* sect163r2 (3) */
  240. return 3;
  241. case NID_sect193r1: /* sect193r1 (4) */
  242. return 4;
  243. case NID_sect193r2: /* sect193r2 (5) */
  244. return 5;
  245. case NID_sect233k1: /* sect233k1 (6) */
  246. return 6;
  247. case NID_sect233r1: /* sect233r1 (7) */
  248. return 7;
  249. case NID_sect239k1: /* sect239k1 (8) */
  250. return 8;
  251. case NID_sect283k1: /* sect283k1 (9) */
  252. return 9;
  253. case NID_sect283r1: /* sect283r1 (10) */
  254. return 10;
  255. case NID_sect409k1: /* sect409k1 (11) */
  256. return 11;
  257. case NID_sect409r1: /* sect409r1 (12) */
  258. return 12;
  259. case NID_sect571k1: /* sect571k1 (13) */
  260. return 13;
  261. case NID_sect571r1: /* sect571r1 (14) */
  262. return 14;
  263. case NID_secp160k1: /* secp160k1 (15) */
  264. return 15;
  265. case NID_secp160r1: /* secp160r1 (16) */
  266. return 16;
  267. case NID_secp160r2: /* secp160r2 (17) */
  268. return 17;
  269. case NID_secp192k1: /* secp192k1 (18) */
  270. return 18;
  271. case NID_X9_62_prime192v1: /* secp192r1 (19) */
  272. return 19;
  273. case NID_secp224k1: /* secp224k1 (20) */
  274. return 20;
  275. case NID_secp224r1: /* secp224r1 (21) */
  276. return 21;
  277. case NID_secp256k1: /* secp256k1 (22) */
  278. return 22;
  279. case NID_X9_62_prime256v1: /* secp256r1 (23) */
  280. return 23;
  281. case NID_secp384r1: /* secp384r1 (24) */
  282. return 24;
  283. case NID_secp521r1: /* secp521r1 (25) */
  284. return 25;
  285. default:
  286. return 0;
  287. }
  288. }
  289. #endif /* OPENSSL_NO_EC */
  290. #ifndef OPENSSL_NO_TLSEXT
  291. /* List of supported signature algorithms and hashes. Should make this
  292. * customisable at some point, for now include everything we support.
  293. */
  294. #ifdef OPENSSL_NO_RSA
  295. #define tlsext_sigalg_rsa(md) /* */
  296. #else
  297. #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
  298. #endif
  299. #ifdef OPENSSL_NO_DSA
  300. #define tlsext_sigalg_dsa(md) /* */
  301. #else
  302. #define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
  303. #endif
  304. #ifdef OPENSSL_NO_ECDSA
  305. #define tlsext_sigalg_ecdsa(md) /* */
  306. #else
  307. #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
  308. #endif
  309. #define tlsext_sigalg(md) \
  310. tlsext_sigalg_rsa(md) \
  311. tlsext_sigalg_dsa(md) \
  312. tlsext_sigalg_ecdsa(md)
  313. static unsigned char tls12_sigalgs[] = {
  314. #ifndef OPENSSL_NO_SHA512
  315. tlsext_sigalg(TLSEXT_hash_sha512)
  316. tlsext_sigalg(TLSEXT_hash_sha384)
  317. #endif
  318. #ifndef OPENSSL_NO_SHA256
  319. tlsext_sigalg(TLSEXT_hash_sha256)
  320. tlsext_sigalg(TLSEXT_hash_sha224)
  321. #endif
  322. #ifndef OPENSSL_NO_SHA
  323. tlsext_sigalg(TLSEXT_hash_sha1)
  324. #endif
  325. };
  326. int tls12_get_req_sig_algs(SSL *s, unsigned char *p)
  327. {
  328. size_t slen = sizeof(tls12_sigalgs);
  329. if (p)
  330. memcpy(p, tls12_sigalgs, slen);
  331. return (int)slen;
  332. }
  333. unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
  334. {
  335. int extdatalen=0;
  336. unsigned char *ret = p;
  337. /* don't add extensions for SSLv3 unless doing secure renegotiation */
  338. if (s->client_version == SSL3_VERSION
  339. && !s->s3->send_connection_binding)
  340. return p;
  341. ret+=2;
  342. if (ret>=limit) return NULL; /* this really never occurs, but ... */
  343. if (s->tlsext_hostname != NULL)
  344. {
  345. /* Add TLS extension servername to the Client Hello message */
  346. unsigned long size_str;
  347. long lenmax;
  348. /* check for enough space.
  349. 4 for the servername type and entension length
  350. 2 for servernamelist length
  351. 1 for the hostname type
  352. 2 for hostname length
  353. + hostname length
  354. */
  355. if ((lenmax = limit - ret - 9) < 0
  356. || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
  357. return NULL;
  358. /* extension type and length */
  359. s2n(TLSEXT_TYPE_server_name,ret);
  360. s2n(size_str+5,ret);
  361. /* length of servername list */
  362. s2n(size_str+3,ret);
  363. /* hostname type, length and hostname */
  364. *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
  365. s2n(size_str,ret);
  366. memcpy(ret, s->tlsext_hostname, size_str);
  367. ret+=size_str;
  368. }
  369. /* Add RI if renegotiating */
  370. if (s->renegotiate)
  371. {
  372. int el;
  373. if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
  374. {
  375. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  376. return NULL;
  377. }
  378. if((limit - p - 4 - el) < 0) return NULL;
  379. s2n(TLSEXT_TYPE_renegotiate,ret);
  380. s2n(el,ret);
  381. if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
  382. {
  383. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  384. return NULL;
  385. }
  386. ret += el;
  387. }
  388. #ifndef OPENSSL_NO_SRP
  389. /* Add SRP username if there is one */
  390. if (s->srp_ctx.login != NULL)
  391. { /* Add TLS extension SRP username to the Client Hello message */
  392. int login_len = strlen(s->srp_ctx.login);
  393. if (login_len > 255 || login_len == 0)
  394. {
  395. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  396. return NULL;
  397. }
  398. /* check for enough space.
  399. 4 for the srp type type and entension length
  400. 1 for the srp user identity
  401. + srp user identity length
  402. */
  403. if ((limit - ret - 5 - login_len) < 0) return NULL;
  404. /* fill in the extension */
  405. s2n(TLSEXT_TYPE_srp,ret);
  406. s2n(login_len+1,ret);
  407. (*ret++) = (unsigned char) login_len;
  408. memcpy(ret, s->srp_ctx.login, login_len);
  409. ret+=login_len;
  410. }
  411. #endif
  412. #ifndef OPENSSL_NO_EC
  413. if (s->tlsext_ecpointformatlist != NULL &&
  414. s->version != DTLS1_VERSION)
  415. {
  416. /* Add TLS extension ECPointFormats to the ClientHello message */
  417. long lenmax;
  418. if ((lenmax = limit - ret - 5) < 0) return NULL;
  419. if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
  420. if (s->tlsext_ecpointformatlist_length > 255)
  421. {
  422. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  423. return NULL;
  424. }
  425. s2n(TLSEXT_TYPE_ec_point_formats,ret);
  426. s2n(s->tlsext_ecpointformatlist_length + 1,ret);
  427. *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
  428. memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
  429. ret+=s->tlsext_ecpointformatlist_length;
  430. }
  431. if (s->tlsext_ellipticcurvelist != NULL &&
  432. s->version != DTLS1_VERSION)
  433. {
  434. /* Add TLS extension EllipticCurves to the ClientHello message */
  435. long lenmax;
  436. if ((lenmax = limit - ret - 6) < 0) return NULL;
  437. if (s->tlsext_ellipticcurvelist_length > (unsigned long)lenmax) return NULL;
  438. if (s->tlsext_ellipticcurvelist_length > 65532)
  439. {
  440. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  441. return NULL;
  442. }
  443. s2n(TLSEXT_TYPE_elliptic_curves,ret);
  444. s2n(s->tlsext_ellipticcurvelist_length + 2, ret);
  445. /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
  446. * elliptic_curve_list, but the examples use two bytes.
  447. * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
  448. * resolves this to two bytes.
  449. */
  450. s2n(s->tlsext_ellipticcurvelist_length, ret);
  451. memcpy(ret, s->tlsext_ellipticcurvelist, s->tlsext_ellipticcurvelist_length);
  452. ret+=s->tlsext_ellipticcurvelist_length;
  453. }
  454. #endif /* OPENSSL_NO_EC */
  455. if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
  456. {
  457. int ticklen;
  458. if (!s->new_session && s->session && s->session->tlsext_tick)
  459. ticklen = s->session->tlsext_ticklen;
  460. else if (s->session && s->tlsext_session_ticket &&
  461. s->tlsext_session_ticket->data)
  462. {
  463. ticklen = s->tlsext_session_ticket->length;
  464. s->session->tlsext_tick = OPENSSL_malloc(ticklen);
  465. if (!s->session->tlsext_tick)
  466. return NULL;
  467. memcpy(s->session->tlsext_tick,
  468. s->tlsext_session_ticket->data,
  469. ticklen);
  470. s->session->tlsext_ticklen = ticklen;
  471. }
  472. else
  473. ticklen = 0;
  474. if (ticklen == 0 && s->tlsext_session_ticket &&
  475. s->tlsext_session_ticket->data == NULL)
  476. goto skip_ext;
  477. /* Check for enough room 2 for extension type, 2 for len
  478. * rest for ticket
  479. */
  480. if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
  481. s2n(TLSEXT_TYPE_session_ticket,ret);
  482. s2n(ticklen,ret);
  483. if (ticklen)
  484. {
  485. memcpy(ret, s->session->tlsext_tick, ticklen);
  486. ret += ticklen;
  487. }
  488. }
  489. skip_ext:
  490. if (TLS1_get_client_version(s) >= TLS1_2_VERSION)
  491. {
  492. if ((size_t)(limit - ret) < sizeof(tls12_sigalgs) + 6)
  493. return NULL;
  494. s2n(TLSEXT_TYPE_signature_algorithms,ret);
  495. s2n(sizeof(tls12_sigalgs) + 2, ret);
  496. s2n(sizeof(tls12_sigalgs), ret);
  497. memcpy(ret, tls12_sigalgs, sizeof(tls12_sigalgs));
  498. ret += sizeof(tls12_sigalgs);
  499. }
  500. #ifdef TLSEXT_TYPE_opaque_prf_input
  501. if (s->s3->client_opaque_prf_input != NULL &&
  502. s->version != DTLS1_VERSION)
  503. {
  504. size_t col = s->s3->client_opaque_prf_input_len;
  505. if ((long)(limit - ret - 6 - col < 0))
  506. return NULL;
  507. if (col > 0xFFFD) /* can't happen */
  508. return NULL;
  509. s2n(TLSEXT_TYPE_opaque_prf_input, ret);
  510. s2n(col + 2, ret);
  511. s2n(col, ret);
  512. memcpy(ret, s->s3->client_opaque_prf_input, col);
  513. ret += col;
  514. }
  515. #endif
  516. if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp &&
  517. s->version != DTLS1_VERSION)
  518. {
  519. int i;
  520. long extlen, idlen, itmp;
  521. OCSP_RESPID *id;
  522. idlen = 0;
  523. for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
  524. {
  525. id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
  526. itmp = i2d_OCSP_RESPID(id, NULL);
  527. if (itmp <= 0)
  528. return NULL;
  529. idlen += itmp + 2;
  530. }
  531. if (s->tlsext_ocsp_exts)
  532. {
  533. extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
  534. if (extlen < 0)
  535. return NULL;
  536. }
  537. else
  538. extlen = 0;
  539. if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL;
  540. s2n(TLSEXT_TYPE_status_request, ret);
  541. if (extlen + idlen > 0xFFF0)
  542. return NULL;
  543. s2n(extlen + idlen + 5, ret);
  544. *(ret++) = TLSEXT_STATUSTYPE_ocsp;
  545. s2n(idlen, ret);
  546. for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
  547. {
  548. /* save position of id len */
  549. unsigned char *q = ret;
  550. id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
  551. /* skip over id len */
  552. ret += 2;
  553. itmp = i2d_OCSP_RESPID(id, &ret);
  554. /* write id len */
  555. s2n(itmp, q);
  556. }
  557. s2n(extlen, ret);
  558. if (extlen > 0)
  559. i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
  560. }
  561. #ifndef OPENSSL_NO_HEARTBEATS
  562. /* Add Heartbeat extension */
  563. s2n(TLSEXT_TYPE_heartbeat,ret);
  564. s2n(1,ret);
  565. /* Set mode:
  566. * 1: peer may send requests
  567. * 2: peer not allowed to send requests
  568. */
  569. if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
  570. *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
  571. else
  572. *(ret++) = SSL_TLSEXT_HB_ENABLED;
  573. #endif
  574. #ifndef OPENSSL_NO_NEXTPROTONEG
  575. if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
  576. {
  577. /* The client advertises an emtpy extension to indicate its
  578. * support for Next Protocol Negotiation */
  579. if (limit - ret - 4 < 0)
  580. return NULL;
  581. s2n(TLSEXT_TYPE_next_proto_neg,ret);
  582. s2n(0,ret);
  583. }
  584. #endif
  585. #ifndef OPENSSL_NO_SRTP
  586. if(SSL_get_srtp_profiles(s))
  587. {
  588. int el;
  589. ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
  590. if((limit - p - 4 - el) < 0) return NULL;
  591. s2n(TLSEXT_TYPE_use_srtp,ret);
  592. s2n(el,ret);
  593. if(ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
  594. {
  595. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  596. return NULL;
  597. }
  598. ret += el;
  599. }
  600. #endif
  601. if ((extdatalen = ret-p-2)== 0)
  602. return p;
  603. s2n(extdatalen,p);
  604. return ret;
  605. }
  606. unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
  607. {
  608. int extdatalen=0;
  609. unsigned char *ret = p;
  610. #ifndef OPENSSL_NO_NEXTPROTONEG
  611. int next_proto_neg_seen;
  612. #endif
  613. /* don't add extensions for SSLv3, unless doing secure renegotiation */
  614. if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
  615. return p;
  616. ret+=2;
  617. if (ret>=limit) return NULL; /* this really never occurs, but ... */
  618. if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
  619. {
  620. if ((long)(limit - ret - 4) < 0) return NULL;
  621. s2n(TLSEXT_TYPE_server_name,ret);
  622. s2n(0,ret);
  623. }
  624. if(s->s3->send_connection_binding)
  625. {
  626. int el;
  627. if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
  628. {
  629. SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  630. return NULL;
  631. }
  632. if((limit - p - 4 - el) < 0) return NULL;
  633. s2n(TLSEXT_TYPE_renegotiate,ret);
  634. s2n(el,ret);
  635. if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
  636. {
  637. SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  638. return NULL;
  639. }
  640. ret += el;
  641. }
  642. #ifndef OPENSSL_NO_EC
  643. if (s->tlsext_ecpointformatlist != NULL &&
  644. s->version != DTLS1_VERSION)
  645. {
  646. /* Add TLS extension ECPointFormats to the ServerHello message */
  647. long lenmax;
  648. if ((lenmax = limit - ret - 5) < 0) return NULL;
  649. if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
  650. if (s->tlsext_ecpointformatlist_length > 255)
  651. {
  652. SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  653. return NULL;
  654. }
  655. s2n(TLSEXT_TYPE_ec_point_formats,ret);
  656. s2n(s->tlsext_ecpointformatlist_length + 1,ret);
  657. *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
  658. memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
  659. ret+=s->tlsext_ecpointformatlist_length;
  660. }
  661. /* Currently the server should not respond with a SupportedCurves extension */
  662. #endif /* OPENSSL_NO_EC */
  663. if (s->tlsext_ticket_expected
  664. && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
  665. {
  666. if ((long)(limit - ret - 4) < 0) return NULL;
  667. s2n(TLSEXT_TYPE_session_ticket,ret);
  668. s2n(0,ret);
  669. }
  670. if (s->tlsext_status_expected)
  671. {
  672. if ((long)(limit - ret - 4) < 0) return NULL;
  673. s2n(TLSEXT_TYPE_status_request,ret);
  674. s2n(0,ret);
  675. }
  676. #ifdef TLSEXT_TYPE_opaque_prf_input
  677. if (s->s3->server_opaque_prf_input != NULL &&
  678. s->version != DTLS1_VERSION)
  679. {
  680. size_t sol = s->s3->server_opaque_prf_input_len;
  681. if ((long)(limit - ret - 6 - sol) < 0)
  682. return NULL;
  683. if (sol > 0xFFFD) /* can't happen */
  684. return NULL;
  685. s2n(TLSEXT_TYPE_opaque_prf_input, ret);
  686. s2n(sol + 2, ret);
  687. s2n(sol, ret);
  688. memcpy(ret, s->s3->server_opaque_prf_input, sol);
  689. ret += sol;
  690. }
  691. #endif
  692. #ifndef OPENSSL_NO_SRTP
  693. if(s->srtp_profile)
  694. {
  695. int el;
  696. ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
  697. if((limit - p - 4 - el) < 0) return NULL;
  698. s2n(TLSEXT_TYPE_use_srtp,ret);
  699. s2n(el,ret);
  700. if(ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
  701. {
  702. SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  703. return NULL;
  704. }
  705. ret+=el;
  706. }
  707. #endif
  708. if (((s->s3->tmp.new_cipher->id & 0xFFFF)==0x80 || (s->s3->tmp.new_cipher->id & 0xFFFF)==0x81)
  709. && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG))
  710. { const unsigned char cryptopro_ext[36] = {
  711. 0xfd, 0xe8, /*65000*/
  712. 0x00, 0x20, /*32 bytes length*/
  713. 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
  714. 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
  715. 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
  716. 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17};
  717. if (limit-ret<36) return NULL;
  718. memcpy(ret,cryptopro_ext,36);
  719. ret+=36;
  720. }
  721. #ifndef OPENSSL_NO_HEARTBEATS
  722. /* Add Heartbeat extension if we've received one */
  723. if (s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED)
  724. {
  725. s2n(TLSEXT_TYPE_heartbeat,ret);
  726. s2n(1,ret);
  727. /* Set mode:
  728. * 1: peer may send requests
  729. * 2: peer not allowed to send requests
  730. */
  731. if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
  732. *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
  733. else
  734. *(ret++) = SSL_TLSEXT_HB_ENABLED;
  735. }
  736. #endif
  737. #ifndef OPENSSL_NO_NEXTPROTONEG
  738. next_proto_neg_seen = s->s3->next_proto_neg_seen;
  739. s->s3->next_proto_neg_seen = 0;
  740. if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
  741. {
  742. const unsigned char *npa;
  743. unsigned int npalen;
  744. int r;
  745. r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
  746. if (r == SSL_TLSEXT_ERR_OK)
  747. {
  748. if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
  749. s2n(TLSEXT_TYPE_next_proto_neg,ret);
  750. s2n(npalen,ret);
  751. memcpy(ret, npa, npalen);
  752. ret += npalen;
  753. s->s3->next_proto_neg_seen = 1;
  754. }
  755. }
  756. #endif
  757. if ((extdatalen = ret-p-2)== 0)
  758. return p;
  759. s2n(extdatalen,p);
  760. return ret;
  761. }
  762. #ifndef OPENSSL_NO_EC
  763. /* ssl_check_for_safari attempts to fingerprint Safari using OS X
  764. * SecureTransport using the TLS extension block in |d|, of length |n|.
  765. * Safari, since 10.6, sends exactly these extensions, in this order:
  766. * SNI,
  767. * elliptic_curves
  768. * ec_point_formats
  769. *
  770. * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
  771. * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
  772. * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
  773. * 10.8..10.8.3 (which don't work).
  774. */
  775. static void ssl_check_for_safari(SSL *s, const unsigned char *data, const unsigned char *d, int n) {
  776. unsigned short type, size;
  777. static const unsigned char kSafariExtensionsBlock[] = {
  778. 0x00, 0x0a, /* elliptic_curves extension */
  779. 0x00, 0x08, /* 8 bytes */
  780. 0x00, 0x06, /* 6 bytes of curve ids */
  781. 0x00, 0x17, /* P-256 */
  782. 0x00, 0x18, /* P-384 */
  783. 0x00, 0x19, /* P-521 */
  784. 0x00, 0x0b, /* ec_point_formats */
  785. 0x00, 0x02, /* 2 bytes */
  786. 0x01, /* 1 point format */
  787. 0x00, /* uncompressed */
  788. };
  789. /* The following is only present in TLS 1.2 */
  790. static const unsigned char kSafariTLS12ExtensionsBlock[] = {
  791. 0x00, 0x0d, /* signature_algorithms */
  792. 0x00, 0x0c, /* 12 bytes */
  793. 0x00, 0x0a, /* 10 bytes */
  794. 0x05, 0x01, /* SHA-384/RSA */
  795. 0x04, 0x01, /* SHA-256/RSA */
  796. 0x02, 0x01, /* SHA-1/RSA */
  797. 0x04, 0x03, /* SHA-256/ECDSA */
  798. 0x02, 0x03, /* SHA-1/ECDSA */
  799. };
  800. if (data >= (d+n-2))
  801. return;
  802. data += 2;
  803. if (data > (d+n-4))
  804. return;
  805. n2s(data,type);
  806. n2s(data,size);
  807. if (type != TLSEXT_TYPE_server_name)
  808. return;
  809. if (data+size > d+n)
  810. return;
  811. data += size;
  812. if (TLS1_get_client_version(s) >= TLS1_2_VERSION)
  813. {
  814. const size_t len1 = sizeof(kSafariExtensionsBlock);
  815. const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock);
  816. if (data + len1 + len2 != d+n)
  817. return;
  818. if (memcmp(data, kSafariExtensionsBlock, len1) != 0)
  819. return;
  820. if (memcmp(data + len1, kSafariTLS12ExtensionsBlock, len2) != 0)
  821. return;
  822. }
  823. else
  824. {
  825. const size_t len = sizeof(kSafariExtensionsBlock);
  826. if (data + len != d+n)
  827. return;
  828. if (memcmp(data, kSafariExtensionsBlock, len) != 0)
  829. return;
  830. }
  831. s->s3->is_probably_safari = 1;
  832. }
  833. #endif /* !OPENSSL_NO_EC */
  834. int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
  835. {
  836. unsigned short type;
  837. unsigned short size;
  838. unsigned short len;
  839. unsigned char *data = *p;
  840. int renegotiate_seen = 0;
  841. int sigalg_seen = 0;
  842. s->servername_done = 0;
  843. s->tlsext_status_type = -1;
  844. #ifndef OPENSSL_NO_NEXTPROTONEG
  845. s->s3->next_proto_neg_seen = 0;
  846. #endif
  847. #ifndef OPENSSL_NO_HEARTBEATS
  848. s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
  849. SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
  850. #endif
  851. #ifndef OPENSSL_NO_EC
  852. if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
  853. ssl_check_for_safari(s, data, d, n);
  854. #endif /* !OPENSSL_NO_EC */
  855. if (data >= (d+n-2))
  856. goto ri_check;
  857. n2s(data,len);
  858. if (data > (d+n-len))
  859. goto ri_check;
  860. while (data <= (d+n-4))
  861. {
  862. n2s(data,type);
  863. n2s(data,size);
  864. if (data+size > (d+n))
  865. goto ri_check;
  866. #if 0
  867. fprintf(stderr,"Received extension type %d size %d\n",type,size);
  868. #endif
  869. if (s->tlsext_debug_cb)
  870. s->tlsext_debug_cb(s, 0, type, data, size,
  871. s->tlsext_debug_arg);
  872. /* The servername extension is treated as follows:
  873. - Only the hostname type is supported with a maximum length of 255.
  874. - The servername is rejected if too long or if it contains zeros,
  875. in which case an fatal alert is generated.
  876. - The servername field is maintained together with the session cache.
  877. - When a session is resumed, the servername call back invoked in order
  878. to allow the application to position itself to the right context.
  879. - The servername is acknowledged if it is new for a session or when
  880. it is identical to a previously used for the same session.
  881. Applications can control the behaviour. They can at any time
  882. set a 'desirable' servername for a new SSL object. This can be the
  883. case for example with HTTPS when a Host: header field is received and
  884. a renegotiation is requested. In this case, a possible servername
  885. presented in the new client hello is only acknowledged if it matches
  886. the value of the Host: field.
  887. - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
  888. if they provide for changing an explicit servername context for the session,
  889. i.e. when the session has been established with a servername extension.
  890. - On session reconnect, the servername extension may be absent.
  891. */
  892. if (type == TLSEXT_TYPE_server_name)
  893. {
  894. unsigned char *sdata;
  895. int servname_type;
  896. int dsize;
  897. if (size < 2)
  898. {
  899. *al = SSL_AD_DECODE_ERROR;
  900. return 0;
  901. }
  902. n2s(data,dsize);
  903. size -= 2;
  904. if (dsize > size )
  905. {
  906. *al = SSL_AD_DECODE_ERROR;
  907. return 0;
  908. }
  909. sdata = data;
  910. while (dsize > 3)
  911. {
  912. servname_type = *(sdata++);
  913. n2s(sdata,len);
  914. dsize -= 3;
  915. if (len > dsize)
  916. {
  917. *al = SSL_AD_DECODE_ERROR;
  918. return 0;
  919. }
  920. if (s->servername_done == 0)
  921. switch (servname_type)
  922. {
  923. case TLSEXT_NAMETYPE_host_name:
  924. if (!s->hit)
  925. {
  926. if(s->session->tlsext_hostname)
  927. {
  928. *al = SSL_AD_DECODE_ERROR;
  929. return 0;
  930. }
  931. if (len > TLSEXT_MAXLEN_host_name)
  932. {
  933. *al = TLS1_AD_UNRECOGNIZED_NAME;
  934. return 0;
  935. }
  936. if ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL)
  937. {
  938. *al = TLS1_AD_INTERNAL_ERROR;
  939. return 0;
  940. }
  941. memcpy(s->session->tlsext_hostname, sdata, len);
  942. s->session->tlsext_hostname[len]='\0';
  943. if (strlen(s->session->tlsext_hostname) != len) {
  944. OPENSSL_free(s->session->tlsext_hostname);
  945. s->session->tlsext_hostname = NULL;
  946. *al = TLS1_AD_UNRECOGNIZED_NAME;
  947. return 0;
  948. }
  949. s->servername_done = 1;
  950. }
  951. else
  952. s->servername_done = s->session->tlsext_hostname
  953. && strlen(s->session->tlsext_hostname) == len
  954. && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
  955. break;
  956. default:
  957. break;
  958. }
  959. dsize -= len;
  960. }
  961. if (dsize != 0)
  962. {
  963. *al = SSL_AD_DECODE_ERROR;
  964. return 0;
  965. }
  966. }
  967. #ifndef OPENSSL_NO_SRP
  968. else if (type == TLSEXT_TYPE_srp)
  969. {
  970. if (size <= 0 || ((len = data[0])) != (size -1))
  971. {
  972. *al = SSL_AD_DECODE_ERROR;
  973. return 0;
  974. }
  975. if (s->srp_ctx.login != NULL)
  976. {
  977. *al = SSL_AD_DECODE_ERROR;
  978. return 0;
  979. }
  980. if ((s->srp_ctx.login = OPENSSL_malloc(len+1)) == NULL)
  981. return -1;
  982. memcpy(s->srp_ctx.login, &data[1], len);
  983. s->srp_ctx.login[len]='\0';
  984. if (strlen(s->srp_ctx.login) != len)
  985. {
  986. *al = SSL_AD_DECODE_ERROR;
  987. return 0;
  988. }
  989. }
  990. #endif
  991. #ifndef OPENSSL_NO_EC
  992. else if (type == TLSEXT_TYPE_ec_point_formats &&
  993. s->version != DTLS1_VERSION)
  994. {
  995. unsigned char *sdata = data;
  996. int ecpointformatlist_length = *(sdata++);
  997. if (ecpointformatlist_length != size - 1)
  998. {
  999. *al = TLS1_AD_DECODE_ERROR;
  1000. return 0;
  1001. }
  1002. if (!s->hit)
  1003. {
  1004. if(s->session->tlsext_ecpointformatlist)
  1005. {
  1006. OPENSSL_free(s->session->tlsext_ecpointformatlist);
  1007. s->session->tlsext_ecpointformatlist = NULL;
  1008. }
  1009. s->session->tlsext_ecpointformatlist_length = 0;
  1010. if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
  1011. {
  1012. *al = TLS1_AD_INTERNAL_ERROR;
  1013. return 0;
  1014. }
  1015. s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
  1016. memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
  1017. }
  1018. #if 0
  1019. fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length);
  1020. sdata = s->session->tlsext_ecpointformatlist;
  1021. for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
  1022. fprintf(stderr,"%i ",*(sdata++));
  1023. fprintf(stderr,"\n");
  1024. #endif
  1025. }
  1026. else if (type == TLSEXT_TYPE_elliptic_curves &&
  1027. s->version != DTLS1_VERSION)
  1028. {
  1029. unsigned char *sdata = data;
  1030. int ellipticcurvelist_length = (*(sdata++) << 8);
  1031. ellipticcurvelist_length += (*(sdata++));
  1032. if (ellipticcurvelist_length != size - 2 ||
  1033. ellipticcurvelist_length < 1)
  1034. {
  1035. *al = TLS1_AD_DECODE_ERROR;
  1036. return 0;
  1037. }
  1038. if (!s->hit)
  1039. {
  1040. if(s->session->tlsext_ellipticcurvelist)
  1041. {
  1042. *al = TLS1_AD_DECODE_ERROR;
  1043. return 0;
  1044. }
  1045. s->session->tlsext_ellipticcurvelist_length = 0;
  1046. if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL)
  1047. {
  1048. *al = TLS1_AD_INTERNAL_ERROR;
  1049. return 0;
  1050. }
  1051. s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length;
  1052. memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length);
  1053. }
  1054. #if 0
  1055. fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length);
  1056. sdata = s->session->tlsext_ellipticcurvelist;
  1057. for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
  1058. fprintf(stderr,"%i ",*(sdata++));
  1059. fprintf(stderr,"\n");
  1060. #endif
  1061. }
  1062. #endif /* OPENSSL_NO_EC */
  1063. #ifdef TLSEXT_TYPE_opaque_prf_input
  1064. else if (type == TLSEXT_TYPE_opaque_prf_input &&
  1065. s->version != DTLS1_VERSION)
  1066. {
  1067. unsigned char *sdata = data;
  1068. if (size < 2)
  1069. {
  1070. *al = SSL_AD_DECODE_ERROR;
  1071. return 0;
  1072. }
  1073. n2s(sdata, s->s3->client_opaque_prf_input_len);
  1074. if (s->s3->client_opaque_prf_input_len != size - 2)
  1075. {
  1076. *al = SSL_AD_DECODE_ERROR;
  1077. return 0;
  1078. }
  1079. if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
  1080. OPENSSL_free(s->s3->client_opaque_prf_input);
  1081. if (s->s3->client_opaque_prf_input_len == 0)
  1082. s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
  1083. else
  1084. s->s3->client_opaque_prf_input = BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
  1085. if (s->s3->client_opaque_prf_input == NULL)
  1086. {
  1087. *al = TLS1_AD_INTERNAL_ERROR;
  1088. return 0;
  1089. }
  1090. }
  1091. #endif
  1092. else if (type == TLSEXT_TYPE_session_ticket)
  1093. {
  1094. if (s->tls_session_ticket_ext_cb &&
  1095. !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
  1096. {
  1097. *al = TLS1_AD_INTERNAL_ERROR;
  1098. return 0;
  1099. }
  1100. }
  1101. else if (type == TLSEXT_TYPE_renegotiate)
  1102. {
  1103. if(!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
  1104. return 0;
  1105. renegotiate_seen = 1;
  1106. }
  1107. else if (type == TLSEXT_TYPE_signature_algorithms)
  1108. {
  1109. int dsize;
  1110. if (sigalg_seen || size < 2)
  1111. {
  1112. *al = SSL_AD_DECODE_ERROR;
  1113. return 0;
  1114. }
  1115. sigalg_seen = 1;
  1116. n2s(data,dsize);
  1117. size -= 2;
  1118. if (dsize != size || dsize & 1)
  1119. {
  1120. *al = SSL_AD_DECODE_ERROR;
  1121. return 0;
  1122. }
  1123. if (!tls1_process_sigalgs(s, data, dsize))
  1124. {
  1125. *al = SSL_AD_DECODE_ERROR;
  1126. return 0;
  1127. }
  1128. }
  1129. else if (type == TLSEXT_TYPE_status_request &&
  1130. s->version != DTLS1_VERSION && s->ctx->tlsext_status_cb)
  1131. {
  1132. if (size < 5)
  1133. {
  1134. *al = SSL_AD_DECODE_ERROR;
  1135. return 0;
  1136. }
  1137. s->tlsext_status_type = *data++;
  1138. size--;
  1139. if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
  1140. {
  1141. const unsigned char *sdata;
  1142. int dsize;
  1143. /* Read in responder_id_list */
  1144. n2s(data,dsize);
  1145. size -= 2;
  1146. if (dsize > size )
  1147. {
  1148. *al = SSL_AD_DECODE_ERROR;
  1149. return 0;
  1150. }
  1151. while (dsize > 0)
  1152. {
  1153. OCSP_RESPID *id;
  1154. int idsize;
  1155. if (dsize < 4)
  1156. {
  1157. *al = SSL_AD_DECODE_ERROR;
  1158. return 0;
  1159. }
  1160. n2s(data, idsize);
  1161. dsize -= 2 + idsize;
  1162. size -= 2 + idsize;
  1163. if (dsize < 0)
  1164. {
  1165. *al = SSL_AD_DECODE_ERROR;
  1166. return 0;
  1167. }
  1168. sdata = data;
  1169. data += idsize;
  1170. id = d2i_OCSP_RESPID(NULL,
  1171. &sdata, idsize);
  1172. if (!id)
  1173. {
  1174. *al = SSL_AD_DECODE_ERROR;
  1175. return 0;
  1176. }
  1177. if (data != sdata)
  1178. {
  1179. OCSP_RESPID_free(id);
  1180. *al = SSL_AD_DECODE_ERROR;
  1181. return 0;
  1182. }
  1183. if (!s->tlsext_ocsp_ids
  1184. && !(s->tlsext_ocsp_ids =
  1185. sk_OCSP_RESPID_new_null()))
  1186. {
  1187. OCSP_RESPID_free(id);
  1188. *al = SSL_AD_INTERNAL_ERROR;
  1189. return 0;
  1190. }
  1191. if (!sk_OCSP_RESPID_push(
  1192. s->tlsext_ocsp_ids, id))
  1193. {
  1194. OCSP_RESPID_free(id);
  1195. *al = SSL_AD_INTERNAL_ERROR;
  1196. return 0;
  1197. }
  1198. }
  1199. /* Read in request_extensions */
  1200. if (size < 2)
  1201. {
  1202. *al = SSL_AD_DECODE_ERROR;
  1203. return 0;
  1204. }
  1205. n2s(data,dsize);
  1206. size -= 2;
  1207. if (dsize != size)
  1208. {
  1209. *al = SSL_AD_DECODE_ERROR;
  1210. return 0;
  1211. }
  1212. sdata = data;
  1213. if (dsize > 0)
  1214. {
  1215. if (s->tlsext_ocsp_exts)
  1216. {
  1217. sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
  1218. X509_EXTENSION_free);
  1219. }
  1220. s->tlsext_ocsp_exts =
  1221. d2i_X509_EXTENSIONS(NULL,
  1222. &sdata, dsize);
  1223. if (!s->tlsext_ocsp_exts
  1224. || (data + dsize != sdata))
  1225. {
  1226. *al = SSL_AD_DECODE_ERROR;
  1227. return 0;
  1228. }
  1229. }
  1230. }
  1231. /* We don't know what to do with any other type
  1232. * so ignore it.
  1233. */
  1234. else
  1235. s->tlsext_status_type = -1;
  1236. }
  1237. #ifndef OPENSSL_NO_HEARTBEATS
  1238. else if (type == TLSEXT_TYPE_heartbeat)
  1239. {
  1240. switch(data[0])
  1241. {
  1242. case 0x01: /* Client allows us to send HB requests */
  1243. s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
  1244. break;
  1245. case 0x02: /* Client doesn't accept HB requests */
  1246. s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
  1247. s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
  1248. break;
  1249. default: *al = SSL_AD_ILLEGAL_PARAMETER;
  1250. return 0;
  1251. }
  1252. }
  1253. #endif
  1254. #ifndef OPENSSL_NO_NEXTPROTONEG
  1255. else if (type == TLSEXT_TYPE_next_proto_neg &&
  1256. s->s3->tmp.finish_md_len == 0)
  1257. {
  1258. /* We shouldn't accept this extension on a
  1259. * renegotiation.
  1260. *
  1261. * s->new_session will be set on renegotiation, but we
  1262. * probably shouldn't rely that it couldn't be set on
  1263. * the initial renegotation too in certain cases (when
  1264. * there's some other reason to disallow resuming an
  1265. * earlier session -- the current code won't be doing
  1266. * anything like that, but this might change).
  1267. * A valid sign that there's been a previous handshake
  1268. * in this connection is if s->s3->tmp.finish_md_len >
  1269. * 0. (We are talking about a check that will happen
  1270. * in the Hello protocol round, well before a new
  1271. * Finished message could have been computed.) */
  1272. s->s3->next_proto_neg_seen = 1;
  1273. }
  1274. #endif
  1275. /* session ticket processed earlier */
  1276. #ifndef OPENSSL_NO_SRTP
  1277. else if (type == TLSEXT_TYPE_use_srtp)
  1278. {
  1279. if(ssl_parse_clienthello_use_srtp_ext(s, data, size,
  1280. al))
  1281. return 0;
  1282. }
  1283. #endif
  1284. data+=size;
  1285. }
  1286. *p = data;
  1287. ri_check:
  1288. /* Need RI if renegotiating */
  1289. if (!renegotiate_seen && s->renegotiate &&
  1290. !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
  1291. {
  1292. *al = SSL_AD_HANDSHAKE_FAILURE;
  1293. SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT,
  1294. SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  1295. return 0;
  1296. }
  1297. return 1;
  1298. }
  1299. #ifndef OPENSSL_NO_NEXTPROTONEG
  1300. /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
  1301. * elements of zero length are allowed and the set of elements must exactly fill
  1302. * the length of the block. */
  1303. static char ssl_next_proto_validate(unsigned char *d, unsigned len)
  1304. {
  1305. unsigned int off = 0;
  1306. while (off < len)
  1307. {
  1308. if (d[off] == 0)
  1309. return 0;
  1310. off += d[off];
  1311. off++;
  1312. }
  1313. return off == len;
  1314. }
  1315. #endif
  1316. int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
  1317. {
  1318. unsigned short length;
  1319. unsigned short type;
  1320. unsigned short size;
  1321. unsigned char *data = *p;
  1322. int tlsext_servername = 0;
  1323. int renegotiate_seen = 0;
  1324. #ifndef OPENSSL_NO_NEXTPROTONEG
  1325. s->s3->next_proto_neg_seen = 0;
  1326. #endif
  1327. #ifndef OPENSSL_NO_HEARTBEATS
  1328. s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
  1329. SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
  1330. #endif
  1331. if (data >= (d+n-2))
  1332. goto ri_check;
  1333. n2s(data,length);
  1334. if (data+length != d+n)
  1335. {
  1336. *al = SSL_AD_DECODE_ERROR;
  1337. return 0;
  1338. }
  1339. while(data <= (d+n-4))
  1340. {
  1341. n2s(data,type);
  1342. n2s(data,size);
  1343. if (data+size > (d+n))
  1344. goto ri_check;
  1345. if (s->tlsext_debug_cb)
  1346. s->tlsext_debug_cb(s, 1, type, data, size,
  1347. s->tlsext_debug_arg);
  1348. if (type == TLSEXT_TYPE_server_name)
  1349. {
  1350. if (s->tlsext_hostname == NULL || size > 0)
  1351. {
  1352. *al = TLS1_AD_UNRECOGNIZED_NAME;
  1353. return 0;
  1354. }
  1355. tlsext_servername = 1;
  1356. }
  1357. #ifndef OPENSSL_NO_EC
  1358. else if (type == TLSEXT_TYPE_ec_point_formats &&
  1359. s->version != DTLS1_VERSION)
  1360. {
  1361. unsigned char *sdata = data;
  1362. int ecpointformatlist_length = *(sdata++);
  1363. if (ecpointformatlist_length != size - 1 ||
  1364. ecpointformatlist_length < 1)
  1365. {
  1366. *al = TLS1_AD_DECODE_ERROR;
  1367. return 0;
  1368. }
  1369. s->session->tlsext_ecpointformatlist_length = 0;
  1370. if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
  1371. if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
  1372. {
  1373. *al = TLS1_AD_INTERNAL_ERROR;
  1374. return 0;
  1375. }
  1376. s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
  1377. memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
  1378. #if 0
  1379. fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
  1380. sdata = s->session->tlsext_ecpointformatlist;
  1381. for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
  1382. fprintf(stderr,"%i ",*(sdata++));
  1383. fprintf(stderr,"\n");
  1384. #endif
  1385. }
  1386. #endif /* OPENSSL_NO_EC */
  1387. else if (type == TLSEXT_TYPE_session_ticket)
  1388. {
  1389. if (s->tls_session_ticket_ext_cb &&
  1390. !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
  1391. {
  1392. *al = TLS1_AD_INTERNAL_ERROR;
  1393. return 0;
  1394. }
  1395. if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
  1396. || (size > 0))
  1397. {
  1398. *al = TLS1_AD_UNSUPPORTED_EXTENSION;
  1399. return 0;
  1400. }
  1401. s->tlsext_ticket_expected = 1;
  1402. }
  1403. #ifdef TLSEXT_TYPE_opaque_prf_input
  1404. else if (type == TLSEXT_TYPE_opaque_prf_input &&
  1405. s->version != DTLS1_VERSION)
  1406. {
  1407. unsigned char *sdata = data;
  1408. if (size < 2)
  1409. {
  1410. *al = SSL_AD_DECODE_ERROR;
  1411. return 0;
  1412. }
  1413. n2s(sdata, s->s3->server_opaque_prf_input_len);
  1414. if (s->s3->server_opaque_prf_input_len != size - 2)
  1415. {
  1416. *al = SSL_AD_DECODE_ERROR;
  1417. return 0;
  1418. }
  1419. if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
  1420. OPENSSL_free(s->s3->server_opaque_prf_input);
  1421. if (s->s3->server_opaque_prf_input_len == 0)
  1422. s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
  1423. else
  1424. s->s3->server_opaque_prf_input = BUF_memdup(sdata, s->s3->server_opaque_prf_input_len);
  1425. if (s->s3->server_opaque_prf_input == NULL)
  1426. {
  1427. *al = TLS1_AD_INTERNAL_ERROR;
  1428. return 0;
  1429. }
  1430. }
  1431. #endif
  1432. else if (type == TLSEXT_TYPE_status_request &&
  1433. s->version != DTLS1_VERSION)
  1434. {
  1435. /* MUST be empty and only sent if we've requested
  1436. * a status request message.
  1437. */
  1438. if ((s->tlsext_status_type == -1) || (size > 0))
  1439. {
  1440. *al = TLS1_AD_UNSUPPORTED_EXTENSION;
  1441. return 0;
  1442. }
  1443. /* Set flag to expect CertificateStatus message */
  1444. s->tlsext_status_expected = 1;
  1445. }
  1446. #ifndef OPENSSL_NO_NEXTPROTONEG
  1447. else if (type == TLSEXT_TYPE_next_proto_neg &&
  1448. s->s3->tmp.finish_md_len == 0)
  1449. {
  1450. unsigned char *selected;
  1451. unsigned char selected_len;
  1452. /* We must have requested it. */
  1453. if (s->ctx->next_proto_select_cb == NULL)
  1454. {
  1455. *al = TLS1_AD_UNSUPPORTED_EXTENSION;
  1456. return 0;
  1457. }
  1458. /* The data must be valid */
  1459. if (!ssl_next_proto_validate(data, size))
  1460. {
  1461. *al = TLS1_AD_DECODE_ERROR;
  1462. return 0;
  1463. }
  1464. if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data, size, s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
  1465. {
  1466. *al = TLS1_AD_INTERNAL_ERROR;
  1467. return 0;
  1468. }
  1469. s->next_proto_negotiated = OPENSSL_malloc(selected_len);
  1470. if (!s->next_proto_negotiated)
  1471. {
  1472. *al = TLS1_AD_INTERNAL_ERROR;
  1473. return 0;
  1474. }
  1475. memcpy(s->next_proto_negotiated, selected, selected_len);
  1476. s->next_proto_negotiated_len = selected_len;
  1477. s->s3->next_proto_neg_seen = 1;
  1478. }
  1479. #endif
  1480. else if (type == TLSEXT_TYPE_renegotiate)
  1481. {
  1482. if(!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
  1483. return 0;
  1484. renegotiate_seen = 1;
  1485. }
  1486. #ifndef OPENSSL_NO_HEARTBEATS
  1487. else if (type == TLSEXT_TYPE_heartbeat)
  1488. {
  1489. switch(data[0])
  1490. {
  1491. case 0x01: /* Server allows us to send HB requests */
  1492. s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
  1493. break;
  1494. case 0x02: /* Server doesn't accept HB requests */
  1495. s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
  1496. s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
  1497. break;
  1498. default: *al = SSL_AD_ILLEGAL_PARAMETER;
  1499. return 0;
  1500. }
  1501. }
  1502. #endif
  1503. #ifndef OPENSSL_NO_SRTP
  1504. else if (type == TLSEXT_TYPE_use_srtp)
  1505. {
  1506. if(ssl_parse_serverhello_use_srtp_ext(s, data, size,
  1507. al))
  1508. return 0;
  1509. }
  1510. #endif
  1511. data+=size;
  1512. }
  1513. if (data != d+n)
  1514. {
  1515. *al = SSL_AD_DECODE_ERROR;
  1516. return 0;
  1517. }
  1518. if (!s->hit && tlsext_servername == 1)
  1519. {
  1520. if (s->tlsext_hostname)
  1521. {
  1522. if (s->session->tlsext_hostname == NULL)
  1523. {
  1524. s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
  1525. if (!s->session->tlsext_hostname)
  1526. {
  1527. *al = SSL_AD_UNRECOGNIZED_NAME;
  1528. return 0;
  1529. }
  1530. }
  1531. else
  1532. {
  1533. *al = SSL_AD_DECODE_ERROR;
  1534. return 0;
  1535. }
  1536. }
  1537. }
  1538. *p = data;
  1539. ri_check:
  1540. /* Determine if we need to see RI. Strictly speaking if we want to
  1541. * avoid an attack we should *always* see RI even on initial server
  1542. * hello because the client doesn't see any renegotiation during an
  1543. * attack. However this would mean we could not connect to any server
  1544. * which doesn't support RI so for the immediate future tolerate RI
  1545. * absence on initial connect only.
  1546. */
  1547. if (!renegotiate_seen
  1548. && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
  1549. && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
  1550. {
  1551. *al = SSL_AD_HANDSHAKE_FAILURE;
  1552. SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT,
  1553. SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  1554. return 0;
  1555. }
  1556. return 1;
  1557. }
  1558. int ssl_prepare_clienthello_tlsext(SSL *s)
  1559. {
  1560. #ifndef OPENSSL_NO_EC
  1561. /* If we are client and using an elliptic curve cryptography cipher suite, send the point formats
  1562. * and elliptic curves we support.
  1563. */
  1564. int using_ecc = 0;
  1565. int i;
  1566. unsigned char *j;
  1567. unsigned long alg_k, alg_a;
  1568. STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
  1569. for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
  1570. {
  1571. SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
  1572. alg_k = c->algorithm_mkey;
  1573. alg_a = c->algorithm_auth;
  1574. if ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe) || (alg_a & SSL_aECDSA)))
  1575. {
  1576. using_ecc = 1;
  1577. break;
  1578. }
  1579. }
  1580. using_ecc = using_ecc && (s->version >= TLS1_VERSION);
  1581. if (using_ecc)
  1582. {
  1583. if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
  1584. if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
  1585. {
  1586. SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
  1587. return -1;
  1588. }
  1589. s->tlsext_ecpointformatlist_length = 3;
  1590. s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
  1591. s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
  1592. s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
  1593. /* we support all named elliptic curves in draft-ietf-tls-ecc-12 */
  1594. if (s->tlsext_ellipticcurvelist != NULL) OPENSSL_free(s->tlsext_ellipticcurvelist);
  1595. s->tlsext_ellipticcurvelist_length = sizeof(pref_list)/sizeof(pref_list[0]) * 2;
  1596. if ((s->tlsext_ellipticcurvelist = OPENSSL_malloc(s->tlsext_ellipticcurvelist_length)) == NULL)
  1597. {
  1598. s->tlsext_ellipticcurvelist_length = 0;
  1599. SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
  1600. return -1;
  1601. }
  1602. for (i = 0, j = s->tlsext_ellipticcurvelist; (unsigned int)i <
  1603. sizeof(pref_list)/sizeof(pref_list[0]); i++)
  1604. {
  1605. int id = tls1_ec_nid2curve_id(pref_list[i]);
  1606. s2n(id,j);
  1607. }
  1608. }
  1609. #endif /* OPENSSL_NO_EC */
  1610. #ifdef TLSEXT_TYPE_opaque_prf_input
  1611. {
  1612. int r = 1;
  1613. if (s->ctx->tlsext_opaque_prf_input_callback != 0)
  1614. {
  1615. r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
  1616. if (!r)
  1617. return -1;
  1618. }
  1619. if (s->tlsext_opaque_prf_input != NULL)
  1620. {
  1621. if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
  1622. OPENSSL_free(s->s3->client_opaque_prf_input);
  1623. if (s->tlsext_opaque_prf_input_len == 0)
  1624. s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
  1625. else
  1626. s->s3->client_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
  1627. if (s->s3->client_opaque_prf_input == NULL)
  1628. {
  1629. SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
  1630. return -1;
  1631. }
  1632. s->s3->client_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
  1633. }
  1634. if (r == 2)
  1635. /* at callback's request, insist on receiving an appropriate server opaque PRF input */
  1636. s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
  1637. }
  1638. #endif
  1639. return 1;
  1640. }
  1641. int ssl_prepare_serverhello_tlsext(SSL *s)
  1642. {
  1643. #ifndef OPENSSL_NO_EC
  1644. /* If we are server and using an ECC cipher suite, send the point formats we support
  1645. * if the client sent us an ECPointsFormat extension. Note that the server is not
  1646. * supposed to send an EllipticCurves extension.
  1647. */
  1648. unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1649. unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1650. int using_ecc = (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA);
  1651. using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
  1652. if (using_ecc)
  1653. {
  1654. if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
  1655. if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
  1656. {
  1657. SSLerr(SSL_F_SSL_PREPARE_SERVERHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
  1658. return -1;
  1659. }
  1660. s->tlsext_ecpointformatlist_length = 3;
  1661. s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
  1662. s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
  1663. s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
  1664. }
  1665. #endif /* OPENSSL_NO_EC */
  1666. return 1;
  1667. }
  1668. int ssl_check_clienthello_tlsext_early(SSL *s)
  1669. {
  1670. int ret=SSL_TLSEXT_ERR_NOACK;
  1671. int al = SSL_AD_UNRECOGNIZED_NAME;
  1672. #ifndef OPENSSL_NO_EC
  1673. /* The handling of the ECPointFormats extension is done elsewhere, namely in
  1674. * ssl3_choose_cipher in s3_lib.c.
  1675. */
  1676. /* The handling of the EllipticCurves extension is done elsewhere, namely in
  1677. * ssl3_choose_cipher in s3_lib.c.
  1678. */
  1679. #endif
  1680. if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
  1681. ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
  1682. else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
  1683. ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
  1684. #ifdef TLSEXT_TYPE_opaque_prf_input
  1685. {
  1686. /* This sort of belongs into ssl_prepare_serverhello_tlsext(),
  1687. * but we might be sending an alert in response to the client hello,
  1688. * so this has to happen here in
  1689. * ssl_check_clienthello_tlsext_early(). */
  1690. int r = 1;
  1691. if (s->ctx->tlsext_opaque_prf_input_callback != 0)
  1692. {
  1693. r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
  1694. if (!r)
  1695. {
  1696. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1697. al = SSL_AD_INTERNAL_ERROR;
  1698. goto err;
  1699. }
  1700. }
  1701. if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
  1702. OPENSSL_free(s->s3->server_opaque_prf_input);
  1703. s->s3->server_opaque_prf_input = NULL;
  1704. if (s->tlsext_opaque_prf_input != NULL)
  1705. {
  1706. if (s->s3->client_opaque_prf_input != NULL &&
  1707. s->s3->client_opaque_prf_input_len == s->tlsext_opaque_prf_input_len)
  1708. {
  1709. /* can only use this extension if we have a server opaque PRF input
  1710. * of the same length as the client opaque PRF input! */
  1711. if (s->tlsext_opaque_prf_input_len == 0)
  1712. s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
  1713. else
  1714. s->s3->server_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
  1715. if (s->s3->server_opaque_prf_input == NULL)
  1716. {
  1717. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1718. al = SSL_AD_INTERNAL_ERROR;
  1719. goto err;
  1720. }
  1721. s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
  1722. }
  1723. }
  1724. if (r == 2 && s->s3->server_opaque_prf_input == NULL)
  1725. {
  1726. /* The callback wants to enforce use of the extension,
  1727. * but we can't do that with the client opaque PRF input;
  1728. * abort the handshake.
  1729. */
  1730. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1731. al = SSL_AD_HANDSHAKE_FAILURE;
  1732. }
  1733. }
  1734. err:
  1735. #endif
  1736. switch (ret)
  1737. {
  1738. case SSL_TLSEXT_ERR_ALERT_FATAL:
  1739. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1740. return -1;
  1741. case SSL_TLSEXT_ERR_ALERT_WARNING:
  1742. ssl3_send_alert(s,SSL3_AL_WARNING,al);
  1743. return 1;
  1744. case SSL_TLSEXT_ERR_NOACK:
  1745. s->servername_done=0;
  1746. default:
  1747. return 1;
  1748. }
  1749. }
  1750. int ssl_check_clienthello_tlsext_late(SSL *s)
  1751. {
  1752. int ret = SSL_TLSEXT_ERR_OK;
  1753. int al;
  1754. /* If status request then ask callback what to do.
  1755. * Note: this must be called after servername callbacks in case
  1756. * the certificate has changed, and must be called after the cipher
  1757. * has been chosen because this may influence which certificate is sent
  1758. */
  1759. if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb)
  1760. {
  1761. int r;
  1762. CERT_PKEY *certpkey;
  1763. certpkey = ssl_get_server_send_pkey(s);
  1764. /* If no certificate can't return certificate status */
  1765. if (certpkey == NULL)
  1766. {
  1767. s->tlsext_status_expected = 0;
  1768. return 1;
  1769. }
  1770. /* Set current certificate to one we will use so
  1771. * SSL_get_certificate et al can pick it up.
  1772. */
  1773. s->cert->key = certpkey;
  1774. r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
  1775. switch (r)
  1776. {
  1777. /* We don't want to send a status request response */
  1778. case SSL_TLSEXT_ERR_NOACK:
  1779. s->tlsext_status_expected = 0;
  1780. break;
  1781. /* status request response should be sent */
  1782. case SSL_TLSEXT_ERR_OK:
  1783. if (s->tlsext_ocsp_resp)
  1784. s->tlsext_status_expected = 1;
  1785. else
  1786. s->tlsext_status_expected = 0;
  1787. break;
  1788. /* something bad happened */
  1789. case SSL_TLSEXT_ERR_ALERT_FATAL:
  1790. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1791. al = SSL_AD_INTERNAL_ERROR;
  1792. goto err;
  1793. }
  1794. }
  1795. else
  1796. s->tlsext_status_expected = 0;
  1797. err:
  1798. switch (ret)
  1799. {
  1800. case SSL_TLSEXT_ERR_ALERT_FATAL:
  1801. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1802. return -1;
  1803. case SSL_TLSEXT_ERR_ALERT_WARNING:
  1804. ssl3_send_alert(s,SSL3_AL_WARNING,al);
  1805. return 1;
  1806. default:
  1807. return 1;
  1808. }
  1809. }
  1810. int ssl_check_serverhello_tlsext(SSL *s)
  1811. {
  1812. int ret=SSL_TLSEXT_ERR_NOACK;
  1813. int al = SSL_AD_UNRECOGNIZED_NAME;
  1814. #ifndef OPENSSL_NO_EC
  1815. /* If we are client and using an elliptic curve cryptography cipher
  1816. * suite, then if server returns an EC point formats lists extension
  1817. * it must contain uncompressed.
  1818. */
  1819. unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1820. unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1821. if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
  1822. (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) &&
  1823. ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA)))
  1824. {
  1825. /* we are using an ECC cipher */
  1826. size_t i;
  1827. unsigned char *list;
  1828. int found_uncompressed = 0;
  1829. list = s->session->tlsext_ecpointformatlist;
  1830. for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
  1831. {
  1832. if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
  1833. {
  1834. found_uncompressed = 1;
  1835. break;
  1836. }
  1837. }
  1838. if (!found_uncompressed)
  1839. {
  1840. SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
  1841. return -1;
  1842. }
  1843. }
  1844. ret = SSL_TLSEXT_ERR_OK;
  1845. #endif /* OPENSSL_NO_EC */
  1846. if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
  1847. ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
  1848. else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
  1849. ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
  1850. #ifdef TLSEXT_TYPE_opaque_prf_input
  1851. if (s->s3->server_opaque_prf_input_len > 0)
  1852. {
  1853. /* This case may indicate that we, as a client, want to insist on using opaque PRF inputs.
  1854. * So first verify that we really have a value from the server too. */
  1855. if (s->s3->server_opaque_prf_input == NULL)
  1856. {
  1857. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1858. al = SSL_AD_HANDSHAKE_FAILURE;
  1859. }
  1860. /* Anytime the server *has* sent an opaque PRF input, we need to check
  1861. * that we have a client opaque PRF input of the same size. */
  1862. if (s->s3->client_opaque_prf_input == NULL ||
  1863. s->s3->client_opaque_prf_input_len != s->s3->server_opaque_prf_input_len)
  1864. {
  1865. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1866. al = SSL_AD_ILLEGAL_PARAMETER;
  1867. }
  1868. }
  1869. #endif
  1870. /* If we've requested certificate status and we wont get one
  1871. * tell the callback
  1872. */
  1873. if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
  1874. && s->ctx && s->ctx->tlsext_status_cb)
  1875. {
  1876. int r;
  1877. /* Set resp to NULL, resplen to -1 so callback knows
  1878. * there is no response.
  1879. */
  1880. if (s->tlsext_ocsp_resp)
  1881. {
  1882. OPENSSL_free(s->tlsext_ocsp_resp);
  1883. s->tlsext_ocsp_resp = NULL;
  1884. }
  1885. s->tlsext_ocsp_resplen = -1;
  1886. r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
  1887. if (r == 0)
  1888. {
  1889. al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
  1890. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1891. }
  1892. if (r < 0)
  1893. {
  1894. al = SSL_AD_INTERNAL_ERROR;
  1895. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1896. }
  1897. }
  1898. switch (ret)
  1899. {
  1900. case SSL_TLSEXT_ERR_ALERT_FATAL:
  1901. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1902. return -1;
  1903. case SSL_TLSEXT_ERR_ALERT_WARNING:
  1904. ssl3_send_alert(s,SSL3_AL_WARNING,al);
  1905. return 1;
  1906. case SSL_TLSEXT_ERR_NOACK:
  1907. s->servername_done=0;
  1908. default:
  1909. return 1;
  1910. }
  1911. }
  1912. /* Since the server cache lookup is done early on in the processing of the
  1913. * ClientHello, and other operations depend on the result, we need to handle
  1914. * any TLS session ticket extension at the same time.
  1915. *
  1916. * session_id: points at the session ID in the ClientHello. This code will
  1917. * read past the end of this in order to parse out the session ticket
  1918. * extension, if any.
  1919. * len: the length of the session ID.
  1920. * limit: a pointer to the first byte after the ClientHello.
  1921. * ret: (output) on return, if a ticket was decrypted, then this is set to
  1922. * point to the resulting session.
  1923. *
  1924. * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
  1925. * ciphersuite, in which case we have no use for session tickets and one will
  1926. * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
  1927. *
  1928. * Returns:
  1929. * -1: fatal error, either from parsing or decrypting the ticket.
  1930. * 0: no ticket was found (or was ignored, based on settings).
  1931. * 1: a zero length extension was found, indicating that the client supports
  1932. * session tickets but doesn't currently have one to offer.
  1933. * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
  1934. * couldn't be decrypted because of a non-fatal error.
  1935. * 3: a ticket was successfully decrypted and *ret was set.
  1936. *
  1937. * Side effects:
  1938. * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
  1939. * a new session ticket to the client because the client indicated support
  1940. * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
  1941. * a session ticket or we couldn't use the one it gave us, or if
  1942. * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
  1943. * Otherwise, s->tlsext_ticket_expected is set to 0.
  1944. */
  1945. int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
  1946. const unsigned char *limit, SSL_SESSION **ret)
  1947. {
  1948. /* Point after session ID in client hello */
  1949. const unsigned char *p = session_id + len;
  1950. unsigned short i;
  1951. *ret = NULL;
  1952. s->tlsext_ticket_expected = 0;
  1953. /* If tickets disabled behave as if no ticket present
  1954. * to permit stateful resumption.
  1955. */
  1956. if (SSL_get_options(s) & SSL_OP_NO_TICKET)
  1957. return 0;
  1958. if ((s->version <= SSL3_VERSION) || !limit)
  1959. return 0;
  1960. if (p >= limit)
  1961. return -1;
  1962. /* Skip past DTLS cookie */
  1963. if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER)
  1964. {
  1965. i = *(p++);
  1966. p+= i;
  1967. if (p >= limit)
  1968. return -1;
  1969. }
  1970. /* Skip past cipher list */
  1971. n2s(p, i);
  1972. p+= i;
  1973. if (p >= limit)
  1974. return -1;
  1975. /* Skip past compression algorithm list */
  1976. i = *(p++);
  1977. p += i;
  1978. if (p > limit)
  1979. return -1;
  1980. /* Now at start of extensions */
  1981. if ((p + 2) >= limit)
  1982. return 0;
  1983. n2s(p, i);
  1984. while ((p + 4) <= limit)
  1985. {
  1986. unsigned short type, size;
  1987. n2s(p, type);
  1988. n2s(p, size);
  1989. if (p + size > limit)
  1990. return 0;
  1991. if (type == TLSEXT_TYPE_session_ticket)
  1992. {
  1993. int r;
  1994. if (size == 0)
  1995. {
  1996. /* The client will accept a ticket but doesn't
  1997. * currently have one. */
  1998. s->tlsext_ticket_expected = 1;
  1999. return 1;
  2000. }
  2001. if (s->tls_session_secret_cb)
  2002. {
  2003. /* Indicate that the ticket couldn't be
  2004. * decrypted rather than generating the session
  2005. * from ticket now, trigger abbreviated
  2006. * handshake based on external mechanism to
  2007. * calculate the master secret later. */
  2008. return 2;
  2009. }
  2010. r = tls_decrypt_ticket(s, p, size, session_id, len, ret);
  2011. switch (r)
  2012. {
  2013. case 2: /* ticket couldn't be decrypted */
  2014. s->tlsext_ticket_expected = 1;
  2015. return 2;
  2016. case 3: /* ticket was decrypted */
  2017. return r;
  2018. case 4: /* ticket decrypted but need to renew */
  2019. s->tlsext_ticket_expected = 1;
  2020. return 3;
  2021. default: /* fatal error */
  2022. return -1;
  2023. }
  2024. }
  2025. p += size;
  2026. }
  2027. return 0;
  2028. }
  2029. /* tls_decrypt_ticket attempts to decrypt a session ticket.
  2030. *
  2031. * etick: points to the body of the session ticket extension.
  2032. * eticklen: the length of the session tickets extenion.
  2033. * sess_id: points at the session ID.
  2034. * sesslen: the length of the session ID.
  2035. * psess: (output) on return, if a ticket was decrypted, then this is set to
  2036. * point to the resulting session.
  2037. *
  2038. * Returns:
  2039. * -1: fatal error, either from parsing or decrypting the ticket.
  2040. * 2: the ticket couldn't be decrypted.
  2041. * 3: a ticket was successfully decrypted and *psess was set.
  2042. * 4: same as 3, but the ticket needs to be renewed.
  2043. */
  2044. static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
  2045. const unsigned char *sess_id, int sesslen,
  2046. SSL_SESSION **psess)
  2047. {
  2048. SSL_SESSION *sess;
  2049. unsigned char *sdec;
  2050. const unsigned char *p;
  2051. int slen, mlen, renew_ticket = 0;
  2052. unsigned char tick_hmac[EVP_MAX_MD_SIZE];
  2053. HMAC_CTX hctx;
  2054. EVP_CIPHER_CTX ctx;
  2055. SSL_CTX *tctx = s->initial_ctx;
  2056. /* Need at least keyname + iv + some encrypted data */
  2057. if (eticklen < 48)
  2058. return 2;
  2059. /* Initialize session ticket encryption and HMAC contexts */
  2060. HMAC_CTX_init(&hctx);
  2061. EVP_CIPHER_CTX_init(&ctx);
  2062. if (tctx->tlsext_ticket_key_cb)
  2063. {
  2064. unsigned char *nctick = (unsigned char *)etick;
  2065. int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
  2066. &ctx, &hctx, 0);
  2067. if (rv < 0)
  2068. return -1;
  2069. if (rv == 0)
  2070. return 2;
  2071. if (rv == 2)
  2072. renew_ticket = 1;
  2073. }
  2074. else
  2075. {
  2076. /* Check key name matches */
  2077. if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
  2078. return 2;
  2079. HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
  2080. tlsext_tick_md(), NULL);
  2081. EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
  2082. tctx->tlsext_tick_aes_key, etick + 16);
  2083. }
  2084. /* Attempt to process session ticket, first conduct sanity and
  2085. * integrity checks on ticket.
  2086. */
  2087. mlen = HMAC_size(&hctx);
  2088. if (mlen < 0)
  2089. {
  2090. EVP_CIPHER_CTX_cleanup(&ctx);
  2091. return -1;
  2092. }
  2093. eticklen -= mlen;
  2094. /* Check HMAC of encrypted ticket */
  2095. HMAC_Update(&hctx, etick, eticklen);
  2096. HMAC_Final(&hctx, tick_hmac, NULL);
  2097. HMAC_CTX_cleanup(&hctx);
  2098. if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen))
  2099. return 2;
  2100. /* Attempt to decrypt session data */
  2101. /* Move p after IV to start of encrypted ticket, update length */
  2102. p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
  2103. eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
  2104. sdec = OPENSSL_malloc(eticklen);
  2105. if (!sdec)
  2106. {
  2107. EVP_CIPHER_CTX_cleanup(&ctx);
  2108. return -1;
  2109. }
  2110. EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
  2111. if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0)
  2112. return 2;
  2113. slen += mlen;
  2114. EVP_CIPHER_CTX_cleanup(&ctx);
  2115. p = sdec;
  2116. sess = d2i_SSL_SESSION(NULL, &p, slen);
  2117. OPENSSL_free(sdec);
  2118. if (sess)
  2119. {
  2120. /* The session ID, if non-empty, is used by some clients to
  2121. * detect that the ticket has been accepted. So we copy it to
  2122. * the session structure. If it is empty set length to zero
  2123. * as required by standard.
  2124. */
  2125. if (sesslen)
  2126. memcpy(sess->session_id, sess_id, sesslen);
  2127. sess->session_id_length = sesslen;
  2128. *psess = sess;
  2129. if (renew_ticket)
  2130. return 4;
  2131. else
  2132. return 3;
  2133. }
  2134. ERR_clear_error();
  2135. /* For session parse failure, indicate that we need to send a new
  2136. * ticket. */
  2137. return 2;
  2138. }
  2139. /* Tables to translate from NIDs to TLS v1.2 ids */
  2140. typedef struct
  2141. {
  2142. int nid;
  2143. int id;
  2144. } tls12_lookup;
  2145. static tls12_lookup tls12_md[] = {
  2146. #ifndef OPENSSL_NO_MD5
  2147. {NID_md5, TLSEXT_hash_md5},
  2148. #endif
  2149. #ifndef OPENSSL_NO_SHA
  2150. {NID_sha1, TLSEXT_hash_sha1},
  2151. #endif
  2152. #ifndef OPENSSL_NO_SHA256
  2153. {NID_sha224, TLSEXT_hash_sha224},
  2154. {NID_sha256, TLSEXT_hash_sha256},
  2155. #endif
  2156. #ifndef OPENSSL_NO_SHA512
  2157. {NID_sha384, TLSEXT_hash_sha384},
  2158. {NID_sha512, TLSEXT_hash_sha512}
  2159. #endif
  2160. };
  2161. static tls12_lookup tls12_sig[] = {
  2162. #ifndef OPENSSL_NO_RSA
  2163. {EVP_PKEY_RSA, TLSEXT_signature_rsa},
  2164. #endif
  2165. #ifndef OPENSSL_NO_DSA
  2166. {EVP_PKEY_DSA, TLSEXT_signature_dsa},
  2167. #endif
  2168. #ifndef OPENSSL_NO_ECDSA
  2169. {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
  2170. #endif
  2171. };
  2172. static int tls12_find_id(int nid, tls12_lookup *table, size_t tlen)
  2173. {
  2174. size_t i;
  2175. for (i = 0; i < tlen; i++)
  2176. {
  2177. if (table[i].nid == nid)
  2178. return table[i].id;
  2179. }
  2180. return -1;
  2181. }
  2182. #if 0
  2183. static int tls12_find_nid(int id, tls12_lookup *table, size_t tlen)
  2184. {
  2185. size_t i;
  2186. for (i = 0; i < tlen; i++)
  2187. {
  2188. if (table[i].id == id)
  2189. return table[i].nid;
  2190. }
  2191. return -1;
  2192. }
  2193. #endif
  2194. int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
  2195. {
  2196. int sig_id, md_id;
  2197. if (!md)
  2198. return 0;
  2199. md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
  2200. sizeof(tls12_md)/sizeof(tls12_lookup));
  2201. if (md_id == -1)
  2202. return 0;
  2203. sig_id = tls12_get_sigid(pk);
  2204. if (sig_id == -1)
  2205. return 0;
  2206. p[0] = (unsigned char)md_id;
  2207. p[1] = (unsigned char)sig_id;
  2208. return 1;
  2209. }
  2210. int tls12_get_sigid(const EVP_PKEY *pk)
  2211. {
  2212. return tls12_find_id(pk->type, tls12_sig,
  2213. sizeof(tls12_sig)/sizeof(tls12_lookup));
  2214. }
  2215. const EVP_MD *tls12_get_hash(unsigned char hash_alg)
  2216. {
  2217. switch(hash_alg)
  2218. {
  2219. #ifndef OPENSSL_NO_SHA
  2220. case TLSEXT_hash_sha1:
  2221. return EVP_sha1();
  2222. #endif
  2223. #ifndef OPENSSL_NO_SHA256
  2224. case TLSEXT_hash_sha224:
  2225. return EVP_sha224();
  2226. case TLSEXT_hash_sha256:
  2227. return EVP_sha256();
  2228. #endif
  2229. #ifndef OPENSSL_NO_SHA512
  2230. case TLSEXT_hash_sha384:
  2231. return EVP_sha384();
  2232. case TLSEXT_hash_sha512:
  2233. return EVP_sha512();
  2234. #endif
  2235. default:
  2236. return NULL;
  2237. }
  2238. }
  2239. /* Set preferred digest for each key type */
  2240. int tls1_process_sigalgs(SSL *s, const unsigned char *data, int dsize)
  2241. {
  2242. int i, idx;
  2243. const EVP_MD *md;
  2244. CERT *c = s->cert;
  2245. /* Extension ignored for TLS versions below 1.2 */
  2246. if (TLS1_get_version(s) < TLS1_2_VERSION)
  2247. return 1;
  2248. /* Should never happen */
  2249. if (!c)
  2250. return 0;
  2251. c->pkeys[SSL_PKEY_DSA_SIGN].digest = NULL;
  2252. c->pkeys[SSL_PKEY_RSA_SIGN].digest = NULL;
  2253. c->pkeys[SSL_PKEY_RSA_ENC].digest = NULL;
  2254. c->pkeys[SSL_PKEY_ECC].digest = NULL;
  2255. for (i = 0; i < dsize; i += 2)
  2256. {
  2257. unsigned char hash_alg = data[i], sig_alg = data[i+1];
  2258. switch(sig_alg)
  2259. {
  2260. #ifndef OPENSSL_NO_RSA
  2261. case TLSEXT_signature_rsa:
  2262. idx = SSL_PKEY_RSA_SIGN;
  2263. break;
  2264. #endif
  2265. #ifndef OPENSSL_NO_DSA
  2266. case TLSEXT_signature_dsa:
  2267. idx = SSL_PKEY_DSA_SIGN;
  2268. break;
  2269. #endif
  2270. #ifndef OPENSSL_NO_ECDSA
  2271. case TLSEXT_signature_ecdsa:
  2272. idx = SSL_PKEY_ECC;
  2273. break;
  2274. #endif
  2275. default:
  2276. continue;
  2277. }
  2278. if (c->pkeys[idx].digest == NULL)
  2279. {
  2280. md = tls12_get_hash(hash_alg);
  2281. if (md)
  2282. {
  2283. c->pkeys[idx].digest = md;
  2284. if (idx == SSL_PKEY_RSA_SIGN)
  2285. c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
  2286. }
  2287. }
  2288. }
  2289. /* Set any remaining keys to default values. NOTE: if alg is not
  2290. * supported it stays as NULL.
  2291. */
  2292. #ifndef OPENSSL_NO_DSA
  2293. if (!c->pkeys[SSL_PKEY_DSA_SIGN].digest)
  2294. c->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_sha1();
  2295. #endif
  2296. #ifndef OPENSSL_NO_RSA
  2297. if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest)
  2298. {
  2299. c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
  2300. c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
  2301. }
  2302. #endif
  2303. #ifndef OPENSSL_NO_ECDSA
  2304. if (!c->pkeys[SSL_PKEY_ECC].digest)
  2305. c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
  2306. #endif
  2307. return 1;
  2308. }
  2309. #endif
  2310. #ifndef OPENSSL_NO_HEARTBEATS
  2311. int
  2312. tls1_process_heartbeat(SSL *s)
  2313. {
  2314. unsigned char *p = &s->s3->rrec.data[0], *pl;
  2315. unsigned short hbtype;
  2316. unsigned int payload;
  2317. unsigned int padding = 16; /* Use minimum padding */
  2318. /* Read type and payload length first */
  2319. hbtype = *p++;
  2320. n2s(p, payload);
  2321. pl = p;
  2322. if (s->msg_callback)
  2323. s->msg_callback(0, s->version, TLS1_RT_HEARTBEAT,
  2324. &s->s3->rrec.data[0], s->s3->rrec.length,
  2325. s, s->msg_callback_arg);
  2326. if (hbtype == TLS1_HB_REQUEST)
  2327. {
  2328. unsigned char *buffer, *bp;
  2329. int r;
  2330. /* Allocate memory for the response, size is 1 bytes
  2331. * message type, plus 2 bytes payload length, plus
  2332. * payload, plus padding
  2333. */
  2334. buffer = OPENSSL_malloc(1 + 2 + payload + padding);
  2335. bp = buffer;
  2336. /* Enter response type, length and copy payload */
  2337. *bp++ = TLS1_HB_RESPONSE;
  2338. s2n(payload, bp);
  2339. memcpy(bp, pl, payload);
  2340. bp += payload;
  2341. /* Random padding */
  2342. RAND_pseudo_bytes(bp, padding);
  2343. r = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buffer, 3 + payload + padding);
  2344. if (r >= 0 && s->msg_callback)
  2345. s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
  2346. buffer, 3 + payload + padding,
  2347. s, s->msg_callback_arg);
  2348. OPENSSL_free(buffer);
  2349. if (r < 0)
  2350. return r;
  2351. }
  2352. else if (hbtype == TLS1_HB_RESPONSE)
  2353. {
  2354. unsigned int seq;
  2355. /* We only send sequence numbers (2 bytes unsigned int),
  2356. * and 16 random bytes, so we just try to read the
  2357. * sequence number */
  2358. n2s(pl, seq);
  2359. if (payload == 18 && seq == s->tlsext_hb_seq)
  2360. {
  2361. s->tlsext_hb_seq++;
  2362. s->tlsext_hb_pending = 0;
  2363. }
  2364. }
  2365. return 0;
  2366. }
  2367. int
  2368. tls1_heartbeat(SSL *s)
  2369. {
  2370. unsigned char *buf, *p;
  2371. int ret;
  2372. unsigned int payload = 18; /* Sequence number + random bytes */
  2373. unsigned int padding = 16; /* Use minimum padding */
  2374. /* Only send if peer supports and accepts HB requests... */
  2375. if (!(s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) ||
  2376. s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_SEND_REQUESTS)
  2377. {
  2378. SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_TLS_HEARTBEAT_PEER_DOESNT_ACCEPT);
  2379. return -1;
  2380. }
  2381. /* ...and there is none in flight yet... */
  2382. if (s->tlsext_hb_pending)
  2383. {
  2384. SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_TLS_HEARTBEAT_PENDING);
  2385. return -1;
  2386. }
  2387. /* ...and no handshake in progress. */
  2388. if (SSL_in_init(s) || s->in_handshake)
  2389. {
  2390. SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_UNEXPECTED_MESSAGE);
  2391. return -1;
  2392. }
  2393. /* Check if padding is too long, payload and padding
  2394. * must not exceed 2^14 - 3 = 16381 bytes in total.
  2395. */
  2396. OPENSSL_assert(payload + padding <= 16381);
  2397. /* Create HeartBeat message, we just use a sequence number
  2398. * as payload to distuingish different messages and add
  2399. * some random stuff.
  2400. * - Message Type, 1 byte
  2401. * - Payload Length, 2 bytes (unsigned int)
  2402. * - Payload, the sequence number (2 bytes uint)
  2403. * - Payload, random bytes (16 bytes uint)
  2404. * - Padding
  2405. */
  2406. buf = OPENSSL_malloc(1 + 2 + payload + padding);
  2407. p = buf;
  2408. /* Message Type */
  2409. *p++ = TLS1_HB_REQUEST;
  2410. /* Payload length (18 bytes here) */
  2411. s2n(payload, p);
  2412. /* Sequence number */
  2413. s2n(s->tlsext_hb_seq, p);
  2414. /* 16 random bytes */
  2415. RAND_pseudo_bytes(p, 16);
  2416. p += 16;
  2417. /* Random padding */
  2418. RAND_pseudo_bytes(p, padding);
  2419. ret = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buf, 3 + payload + padding);
  2420. if (ret >= 0)
  2421. {
  2422. if (s->msg_callback)
  2423. s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
  2424. buf, 3 + payload + padding,
  2425. s, s->msg_callback_arg);
  2426. s->tlsext_hb_pending = 1;
  2427. }
  2428. OPENSSL_free(buf);
  2429. return ret;
  2430. }
  2431. #endif