t1_lib.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578
  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. #ifndef OPENSSL_NO_MD5
  326. tlsext_sigalg_rsa(TLSEXT_hash_md5)
  327. #endif
  328. };
  329. int tls12_get_req_sig_algs(SSL *s, unsigned char *p)
  330. {
  331. size_t slen = sizeof(tls12_sigalgs);
  332. #ifdef OPENSSL_FIPS
  333. /* If FIPS mode don't include MD5 which is last */
  334. if (FIPS_mode())
  335. slen -= 2;
  336. #endif
  337. if (p)
  338. memcpy(p, tls12_sigalgs, slen);
  339. return (int)slen;
  340. }
  341. unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
  342. {
  343. int extdatalen=0;
  344. unsigned char *ret = p;
  345. /* don't add extensions for SSLv3 unless doing secure renegotiation */
  346. if (s->client_version == SSL3_VERSION
  347. && !s->s3->send_connection_binding)
  348. return p;
  349. ret+=2;
  350. if (ret>=limit) return NULL; /* this really never occurs, but ... */
  351. if (s->tlsext_hostname != NULL)
  352. {
  353. /* Add TLS extension servername to the Client Hello message */
  354. unsigned long size_str;
  355. long lenmax;
  356. /* check for enough space.
  357. 4 for the servername type and entension length
  358. 2 for servernamelist length
  359. 1 for the hostname type
  360. 2 for hostname length
  361. + hostname length
  362. */
  363. if ((lenmax = limit - ret - 9) < 0
  364. || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
  365. return NULL;
  366. /* extension type and length */
  367. s2n(TLSEXT_TYPE_server_name,ret);
  368. s2n(size_str+5,ret);
  369. /* length of servername list */
  370. s2n(size_str+3,ret);
  371. /* hostname type, length and hostname */
  372. *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
  373. s2n(size_str,ret);
  374. memcpy(ret, s->tlsext_hostname, size_str);
  375. ret+=size_str;
  376. }
  377. /* Add RI if renegotiating */
  378. if (s->renegotiate)
  379. {
  380. int el;
  381. if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
  382. {
  383. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  384. return NULL;
  385. }
  386. if((limit - p - 4 - el) < 0) return NULL;
  387. s2n(TLSEXT_TYPE_renegotiate,ret);
  388. s2n(el,ret);
  389. if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
  390. {
  391. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  392. return NULL;
  393. }
  394. ret += el;
  395. }
  396. #ifndef OPENSSL_NO_SRP
  397. /* Add SRP username if there is one */
  398. if (s->srp_ctx.login != NULL)
  399. { /* Add TLS extension SRP username to the Client Hello message */
  400. int login_len = strlen(s->srp_ctx.login);
  401. if (login_len > 255 || login_len == 0)
  402. {
  403. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  404. return NULL;
  405. }
  406. /* check for enough space.
  407. 4 for the srp type type and entension length
  408. 1 for the srp user identity
  409. + srp user identity length
  410. */
  411. if ((limit - ret - 5 - login_len) < 0) return NULL;
  412. /* fill in the extension */
  413. s2n(TLSEXT_TYPE_srp,ret);
  414. s2n(login_len+1,ret);
  415. (*ret++) = (unsigned char) login_len;
  416. memcpy(ret, s->srp_ctx.login, login_len);
  417. ret+=login_len;
  418. }
  419. #endif
  420. #ifndef OPENSSL_NO_EC
  421. if (s->tlsext_ecpointformatlist != NULL &&
  422. s->version != DTLS1_VERSION)
  423. {
  424. /* Add TLS extension ECPointFormats to the ClientHello message */
  425. long lenmax;
  426. if ((lenmax = limit - ret - 5) < 0) return NULL;
  427. if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
  428. if (s->tlsext_ecpointformatlist_length > 255)
  429. {
  430. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  431. return NULL;
  432. }
  433. s2n(TLSEXT_TYPE_ec_point_formats,ret);
  434. s2n(s->tlsext_ecpointformatlist_length + 1,ret);
  435. *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
  436. memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
  437. ret+=s->tlsext_ecpointformatlist_length;
  438. }
  439. if (s->tlsext_ellipticcurvelist != NULL &&
  440. s->version != DTLS1_VERSION)
  441. {
  442. /* Add TLS extension EllipticCurves to the ClientHello message */
  443. long lenmax;
  444. if ((lenmax = limit - ret - 6) < 0) return NULL;
  445. if (s->tlsext_ellipticcurvelist_length > (unsigned long)lenmax) return NULL;
  446. if (s->tlsext_ellipticcurvelist_length > 65532)
  447. {
  448. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  449. return NULL;
  450. }
  451. s2n(TLSEXT_TYPE_elliptic_curves,ret);
  452. s2n(s->tlsext_ellipticcurvelist_length + 2, ret);
  453. /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
  454. * elliptic_curve_list, but the examples use two bytes.
  455. * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
  456. * resolves this to two bytes.
  457. */
  458. s2n(s->tlsext_ellipticcurvelist_length, ret);
  459. memcpy(ret, s->tlsext_ellipticcurvelist, s->tlsext_ellipticcurvelist_length);
  460. ret+=s->tlsext_ellipticcurvelist_length;
  461. }
  462. #endif /* OPENSSL_NO_EC */
  463. if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
  464. {
  465. int ticklen;
  466. if (!s->new_session && s->session && s->session->tlsext_tick)
  467. ticklen = s->session->tlsext_ticklen;
  468. else if (s->session && s->tlsext_session_ticket &&
  469. s->tlsext_session_ticket->data)
  470. {
  471. ticklen = s->tlsext_session_ticket->length;
  472. s->session->tlsext_tick = OPENSSL_malloc(ticklen);
  473. if (!s->session->tlsext_tick)
  474. return NULL;
  475. memcpy(s->session->tlsext_tick,
  476. s->tlsext_session_ticket->data,
  477. ticklen);
  478. s->session->tlsext_ticklen = ticklen;
  479. }
  480. else
  481. ticklen = 0;
  482. if (ticklen == 0 && s->tlsext_session_ticket &&
  483. s->tlsext_session_ticket->data == NULL)
  484. goto skip_ext;
  485. /* Check for enough room 2 for extension type, 2 for len
  486. * rest for ticket
  487. */
  488. if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
  489. s2n(TLSEXT_TYPE_session_ticket,ret);
  490. s2n(ticklen,ret);
  491. if (ticklen)
  492. {
  493. memcpy(ret, s->session->tlsext_tick, ticklen);
  494. ret += ticklen;
  495. }
  496. }
  497. skip_ext:
  498. if (TLS1_get_client_version(s) >= TLS1_2_VERSION)
  499. {
  500. if ((size_t)(limit - ret) < sizeof(tls12_sigalgs) + 6)
  501. return NULL;
  502. s2n(TLSEXT_TYPE_signature_algorithms,ret);
  503. s2n(sizeof(tls12_sigalgs) + 2, ret);
  504. s2n(sizeof(tls12_sigalgs), ret);
  505. memcpy(ret, tls12_sigalgs, sizeof(tls12_sigalgs));
  506. ret += sizeof(tls12_sigalgs);
  507. }
  508. #ifdef TLSEXT_TYPE_opaque_prf_input
  509. if (s->s3->client_opaque_prf_input != NULL &&
  510. s->version != DTLS1_VERSION)
  511. {
  512. size_t col = s->s3->client_opaque_prf_input_len;
  513. if ((long)(limit - ret - 6 - col < 0))
  514. return NULL;
  515. if (col > 0xFFFD) /* can't happen */
  516. return NULL;
  517. s2n(TLSEXT_TYPE_opaque_prf_input, ret);
  518. s2n(col + 2, ret);
  519. s2n(col, ret);
  520. memcpy(ret, s->s3->client_opaque_prf_input, col);
  521. ret += col;
  522. }
  523. #endif
  524. if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp &&
  525. s->version != DTLS1_VERSION)
  526. {
  527. int i;
  528. long extlen, idlen, itmp;
  529. OCSP_RESPID *id;
  530. idlen = 0;
  531. for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
  532. {
  533. id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
  534. itmp = i2d_OCSP_RESPID(id, NULL);
  535. if (itmp <= 0)
  536. return NULL;
  537. idlen += itmp + 2;
  538. }
  539. if (s->tlsext_ocsp_exts)
  540. {
  541. extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
  542. if (extlen < 0)
  543. return NULL;
  544. }
  545. else
  546. extlen = 0;
  547. if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL;
  548. s2n(TLSEXT_TYPE_status_request, ret);
  549. if (extlen + idlen > 0xFFF0)
  550. return NULL;
  551. s2n(extlen + idlen + 5, ret);
  552. *(ret++) = TLSEXT_STATUSTYPE_ocsp;
  553. s2n(idlen, ret);
  554. for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
  555. {
  556. /* save position of id len */
  557. unsigned char *q = ret;
  558. id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
  559. /* skip over id len */
  560. ret += 2;
  561. itmp = i2d_OCSP_RESPID(id, &ret);
  562. /* write id len */
  563. s2n(itmp, q);
  564. }
  565. s2n(extlen, ret);
  566. if (extlen > 0)
  567. i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
  568. }
  569. #ifndef OPENSSL_NO_HEARTBEATS
  570. /* Add Heartbeat extension */
  571. s2n(TLSEXT_TYPE_heartbeat,ret);
  572. s2n(1,ret);
  573. /* Set mode:
  574. * 1: peer may send requests
  575. * 2: peer not allowed to send requests
  576. */
  577. if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
  578. *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
  579. else
  580. *(ret++) = SSL_TLSEXT_HB_ENABLED;
  581. #endif
  582. #ifndef OPENSSL_NO_NEXTPROTONEG
  583. if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
  584. {
  585. /* The client advertises an emtpy extension to indicate its
  586. * support for Next Protocol Negotiation */
  587. if (limit - ret - 4 < 0)
  588. return NULL;
  589. s2n(TLSEXT_TYPE_next_proto_neg,ret);
  590. s2n(0,ret);
  591. }
  592. #endif
  593. if(SSL_get_srtp_profiles(s))
  594. {
  595. int el;
  596. ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
  597. if((limit - p - 4 - el) < 0) return NULL;
  598. s2n(TLSEXT_TYPE_use_srtp,ret);
  599. s2n(el,ret);
  600. if(ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
  601. {
  602. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  603. return NULL;
  604. }
  605. ret += el;
  606. }
  607. if ((extdatalen = ret-p-2)== 0)
  608. return p;
  609. s2n(extdatalen,p);
  610. return ret;
  611. }
  612. unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
  613. {
  614. int extdatalen=0;
  615. unsigned char *ret = p;
  616. #ifndef OPENSSL_NO_NEXTPROTONEG
  617. int next_proto_neg_seen;
  618. #endif
  619. /* don't add extensions for SSLv3, unless doing secure renegotiation */
  620. if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
  621. return p;
  622. ret+=2;
  623. if (ret>=limit) return NULL; /* this really never occurs, but ... */
  624. if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
  625. {
  626. if ((long)(limit - ret - 4) < 0) return NULL;
  627. s2n(TLSEXT_TYPE_server_name,ret);
  628. s2n(0,ret);
  629. }
  630. if(s->s3->send_connection_binding)
  631. {
  632. int el;
  633. if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
  634. {
  635. SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  636. return NULL;
  637. }
  638. if((limit - p - 4 - el) < 0) return NULL;
  639. s2n(TLSEXT_TYPE_renegotiate,ret);
  640. s2n(el,ret);
  641. if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
  642. {
  643. SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  644. return NULL;
  645. }
  646. ret += el;
  647. }
  648. #ifndef OPENSSL_NO_EC
  649. if (s->tlsext_ecpointformatlist != NULL &&
  650. s->version != DTLS1_VERSION)
  651. {
  652. /* Add TLS extension ECPointFormats to the ServerHello message */
  653. long lenmax;
  654. if ((lenmax = limit - ret - 5) < 0) return NULL;
  655. if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
  656. if (s->tlsext_ecpointformatlist_length > 255)
  657. {
  658. SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  659. return NULL;
  660. }
  661. s2n(TLSEXT_TYPE_ec_point_formats,ret);
  662. s2n(s->tlsext_ecpointformatlist_length + 1,ret);
  663. *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
  664. memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
  665. ret+=s->tlsext_ecpointformatlist_length;
  666. }
  667. /* Currently the server should not respond with a SupportedCurves extension */
  668. #endif /* OPENSSL_NO_EC */
  669. if (s->tlsext_ticket_expected
  670. && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
  671. {
  672. if ((long)(limit - ret - 4) < 0) return NULL;
  673. s2n(TLSEXT_TYPE_session_ticket,ret);
  674. s2n(0,ret);
  675. }
  676. if (s->tlsext_status_expected)
  677. {
  678. if ((long)(limit - ret - 4) < 0) return NULL;
  679. s2n(TLSEXT_TYPE_status_request,ret);
  680. s2n(0,ret);
  681. }
  682. #ifdef TLSEXT_TYPE_opaque_prf_input
  683. if (s->s3->server_opaque_prf_input != NULL &&
  684. s->version != DTLS1_VERSION)
  685. {
  686. size_t sol = s->s3->server_opaque_prf_input_len;
  687. if ((long)(limit - ret - 6 - sol) < 0)
  688. return NULL;
  689. if (sol > 0xFFFD) /* can't happen */
  690. return NULL;
  691. s2n(TLSEXT_TYPE_opaque_prf_input, ret);
  692. s2n(sol + 2, ret);
  693. s2n(sol, ret);
  694. memcpy(ret, s->s3->server_opaque_prf_input, sol);
  695. ret += sol;
  696. }
  697. #endif
  698. if(s->srtp_profile)
  699. {
  700. int el;
  701. ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
  702. if((limit - p - 4 - el) < 0) return NULL;
  703. s2n(TLSEXT_TYPE_use_srtp,ret);
  704. s2n(el,ret);
  705. if(ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
  706. {
  707. SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  708. return NULL;
  709. }
  710. ret+=el;
  711. }
  712. if (((s->s3->tmp.new_cipher->id & 0xFFFF)==0x80 || (s->s3->tmp.new_cipher->id & 0xFFFF)==0x81)
  713. && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG))
  714. { const unsigned char cryptopro_ext[36] = {
  715. 0xfd, 0xe8, /*65000*/
  716. 0x00, 0x20, /*32 bytes length*/
  717. 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
  718. 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
  719. 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
  720. 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17};
  721. if (limit-ret<36) return NULL;
  722. memcpy(ret,cryptopro_ext,36);
  723. ret+=36;
  724. }
  725. #ifndef OPENSSL_NO_HEARTBEATS
  726. /* Add Heartbeat extension if we've received one */
  727. if (s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED)
  728. {
  729. s2n(TLSEXT_TYPE_heartbeat,ret);
  730. s2n(1,ret);
  731. /* Set mode:
  732. * 1: peer may send requests
  733. * 2: peer not allowed to send requests
  734. */
  735. if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
  736. *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
  737. else
  738. *(ret++) = SSL_TLSEXT_HB_ENABLED;
  739. }
  740. #endif
  741. #ifndef OPENSSL_NO_NEXTPROTONEG
  742. next_proto_neg_seen = s->s3->next_proto_neg_seen;
  743. s->s3->next_proto_neg_seen = 0;
  744. if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
  745. {
  746. const unsigned char *npa;
  747. unsigned int npalen;
  748. int r;
  749. r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
  750. if (r == SSL_TLSEXT_ERR_OK)
  751. {
  752. if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
  753. s2n(TLSEXT_TYPE_next_proto_neg,ret);
  754. s2n(npalen,ret);
  755. memcpy(ret, npa, npalen);
  756. ret += npalen;
  757. s->s3->next_proto_neg_seen = 1;
  758. }
  759. }
  760. #endif
  761. if ((extdatalen = ret-p-2)== 0)
  762. return p;
  763. s2n(extdatalen,p);
  764. return ret;
  765. }
  766. int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
  767. {
  768. unsigned short type;
  769. unsigned short size;
  770. unsigned short len;
  771. unsigned char *data = *p;
  772. int renegotiate_seen = 0;
  773. int sigalg_seen = 0;
  774. s->servername_done = 0;
  775. s->tlsext_status_type = -1;
  776. #ifndef OPENSSL_NO_NEXTPROTONEG
  777. s->s3->next_proto_neg_seen = 0;
  778. #endif
  779. #ifndef OPENSSL_NO_HEARTBEATS
  780. s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
  781. SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
  782. #endif
  783. if (data >= (d+n-2))
  784. goto ri_check;
  785. n2s(data,len);
  786. if (data > (d+n-len))
  787. goto ri_check;
  788. while (data <= (d+n-4))
  789. {
  790. n2s(data,type);
  791. n2s(data,size);
  792. if (data+size > (d+n))
  793. goto ri_check;
  794. #if 0
  795. fprintf(stderr,"Received extension type %d size %d\n",type,size);
  796. #endif
  797. if (s->tlsext_debug_cb)
  798. s->tlsext_debug_cb(s, 0, type, data, size,
  799. s->tlsext_debug_arg);
  800. /* The servername extension is treated as follows:
  801. - Only the hostname type is supported with a maximum length of 255.
  802. - The servername is rejected if too long or if it contains zeros,
  803. in which case an fatal alert is generated.
  804. - The servername field is maintained together with the session cache.
  805. - When a session is resumed, the servername call back invoked in order
  806. to allow the application to position itself to the right context.
  807. - The servername is acknowledged if it is new for a session or when
  808. it is identical to a previously used for the same session.
  809. Applications can control the behaviour. They can at any time
  810. set a 'desirable' servername for a new SSL object. This can be the
  811. case for example with HTTPS when a Host: header field is received and
  812. a renegotiation is requested. In this case, a possible servername
  813. presented in the new client hello is only acknowledged if it matches
  814. the value of the Host: field.
  815. - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
  816. if they provide for changing an explicit servername context for the session,
  817. i.e. when the session has been established with a servername extension.
  818. - On session reconnect, the servername extension may be absent.
  819. */
  820. if (type == TLSEXT_TYPE_server_name)
  821. {
  822. unsigned char *sdata;
  823. int servname_type;
  824. int dsize;
  825. if (size < 2)
  826. {
  827. *al = SSL_AD_DECODE_ERROR;
  828. return 0;
  829. }
  830. n2s(data,dsize);
  831. size -= 2;
  832. if (dsize > size )
  833. {
  834. *al = SSL_AD_DECODE_ERROR;
  835. return 0;
  836. }
  837. sdata = data;
  838. while (dsize > 3)
  839. {
  840. servname_type = *(sdata++);
  841. n2s(sdata,len);
  842. dsize -= 3;
  843. if (len > dsize)
  844. {
  845. *al = SSL_AD_DECODE_ERROR;
  846. return 0;
  847. }
  848. if (s->servername_done == 0)
  849. switch (servname_type)
  850. {
  851. case TLSEXT_NAMETYPE_host_name:
  852. if (!s->hit)
  853. {
  854. if(s->session->tlsext_hostname)
  855. {
  856. *al = SSL_AD_DECODE_ERROR;
  857. return 0;
  858. }
  859. if (len > TLSEXT_MAXLEN_host_name)
  860. {
  861. *al = TLS1_AD_UNRECOGNIZED_NAME;
  862. return 0;
  863. }
  864. if ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL)
  865. {
  866. *al = TLS1_AD_INTERNAL_ERROR;
  867. return 0;
  868. }
  869. memcpy(s->session->tlsext_hostname, sdata, len);
  870. s->session->tlsext_hostname[len]='\0';
  871. if (strlen(s->session->tlsext_hostname) != len) {
  872. OPENSSL_free(s->session->tlsext_hostname);
  873. s->session->tlsext_hostname = NULL;
  874. *al = TLS1_AD_UNRECOGNIZED_NAME;
  875. return 0;
  876. }
  877. s->servername_done = 1;
  878. }
  879. else
  880. s->servername_done = s->session->tlsext_hostname
  881. && strlen(s->session->tlsext_hostname) == len
  882. && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
  883. break;
  884. default:
  885. break;
  886. }
  887. dsize -= len;
  888. }
  889. if (dsize != 0)
  890. {
  891. *al = SSL_AD_DECODE_ERROR;
  892. return 0;
  893. }
  894. }
  895. #ifndef OPENSSL_NO_SRP
  896. else if (type == TLSEXT_TYPE_srp)
  897. {
  898. if (size <= 0 || ((len = data[0])) != (size -1))
  899. {
  900. *al = SSL_AD_DECODE_ERROR;
  901. return 0;
  902. }
  903. if (s->srp_ctx.login != NULL)
  904. {
  905. *al = SSL_AD_DECODE_ERROR;
  906. return 0;
  907. }
  908. if ((s->srp_ctx.login = OPENSSL_malloc(len+1)) == NULL)
  909. return -1;
  910. memcpy(s->srp_ctx.login, &data[1], len);
  911. s->srp_ctx.login[len]='\0';
  912. if (strlen(s->srp_ctx.login) != len)
  913. {
  914. *al = SSL_AD_DECODE_ERROR;
  915. return 0;
  916. }
  917. }
  918. #endif
  919. #ifndef OPENSSL_NO_EC
  920. else if (type == TLSEXT_TYPE_ec_point_formats &&
  921. s->version != DTLS1_VERSION)
  922. {
  923. unsigned char *sdata = data;
  924. int ecpointformatlist_length = *(sdata++);
  925. if (ecpointformatlist_length != size - 1)
  926. {
  927. *al = TLS1_AD_DECODE_ERROR;
  928. return 0;
  929. }
  930. if (!s->hit)
  931. {
  932. if(s->session->tlsext_ecpointformatlist)
  933. {
  934. OPENSSL_free(s->session->tlsext_ecpointformatlist);
  935. s->session->tlsext_ecpointformatlist = NULL;
  936. }
  937. s->session->tlsext_ecpointformatlist_length = 0;
  938. if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
  939. {
  940. *al = TLS1_AD_INTERNAL_ERROR;
  941. return 0;
  942. }
  943. s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
  944. memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
  945. }
  946. #if 0
  947. fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length);
  948. sdata = s->session->tlsext_ecpointformatlist;
  949. for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
  950. fprintf(stderr,"%i ",*(sdata++));
  951. fprintf(stderr,"\n");
  952. #endif
  953. }
  954. else if (type == TLSEXT_TYPE_elliptic_curves &&
  955. s->version != DTLS1_VERSION)
  956. {
  957. unsigned char *sdata = data;
  958. int ellipticcurvelist_length = (*(sdata++) << 8);
  959. ellipticcurvelist_length += (*(sdata++));
  960. if (ellipticcurvelist_length != size - 2)
  961. {
  962. *al = TLS1_AD_DECODE_ERROR;
  963. return 0;
  964. }
  965. if (!s->hit)
  966. {
  967. if(s->session->tlsext_ellipticcurvelist)
  968. {
  969. *al = TLS1_AD_DECODE_ERROR;
  970. return 0;
  971. }
  972. s->session->tlsext_ellipticcurvelist_length = 0;
  973. if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL)
  974. {
  975. *al = TLS1_AD_INTERNAL_ERROR;
  976. return 0;
  977. }
  978. s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length;
  979. memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length);
  980. }
  981. #if 0
  982. fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length);
  983. sdata = s->session->tlsext_ellipticcurvelist;
  984. for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
  985. fprintf(stderr,"%i ",*(sdata++));
  986. fprintf(stderr,"\n");
  987. #endif
  988. }
  989. #endif /* OPENSSL_NO_EC */
  990. #ifdef TLSEXT_TYPE_opaque_prf_input
  991. else if (type == TLSEXT_TYPE_opaque_prf_input &&
  992. s->version != DTLS1_VERSION)
  993. {
  994. unsigned char *sdata = data;
  995. if (size < 2)
  996. {
  997. *al = SSL_AD_DECODE_ERROR;
  998. return 0;
  999. }
  1000. n2s(sdata, s->s3->client_opaque_prf_input_len);
  1001. if (s->s3->client_opaque_prf_input_len != size - 2)
  1002. {
  1003. *al = SSL_AD_DECODE_ERROR;
  1004. return 0;
  1005. }
  1006. if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
  1007. OPENSSL_free(s->s3->client_opaque_prf_input);
  1008. if (s->s3->client_opaque_prf_input_len == 0)
  1009. s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
  1010. else
  1011. s->s3->client_opaque_prf_input = BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
  1012. if (s->s3->client_opaque_prf_input == NULL)
  1013. {
  1014. *al = TLS1_AD_INTERNAL_ERROR;
  1015. return 0;
  1016. }
  1017. }
  1018. #endif
  1019. else if (type == TLSEXT_TYPE_session_ticket)
  1020. {
  1021. if (s->tls_session_ticket_ext_cb &&
  1022. !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
  1023. {
  1024. *al = TLS1_AD_INTERNAL_ERROR;
  1025. return 0;
  1026. }
  1027. }
  1028. else if (type == TLSEXT_TYPE_renegotiate)
  1029. {
  1030. if(!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
  1031. return 0;
  1032. renegotiate_seen = 1;
  1033. }
  1034. else if (type == TLSEXT_TYPE_signature_algorithms)
  1035. {
  1036. int dsize;
  1037. if (sigalg_seen || size < 2)
  1038. {
  1039. *al = SSL_AD_DECODE_ERROR;
  1040. return 0;
  1041. }
  1042. sigalg_seen = 1;
  1043. n2s(data,dsize);
  1044. size -= 2;
  1045. if (dsize != size || dsize & 1)
  1046. {
  1047. *al = SSL_AD_DECODE_ERROR;
  1048. return 0;
  1049. }
  1050. if (!tls1_process_sigalgs(s, data, dsize))
  1051. {
  1052. *al = SSL_AD_DECODE_ERROR;
  1053. return 0;
  1054. }
  1055. }
  1056. else if (type == TLSEXT_TYPE_status_request &&
  1057. s->version != DTLS1_VERSION && s->ctx->tlsext_status_cb)
  1058. {
  1059. if (size < 5)
  1060. {
  1061. *al = SSL_AD_DECODE_ERROR;
  1062. return 0;
  1063. }
  1064. s->tlsext_status_type = *data++;
  1065. size--;
  1066. if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
  1067. {
  1068. const unsigned char *sdata;
  1069. int dsize;
  1070. /* Read in responder_id_list */
  1071. n2s(data,dsize);
  1072. size -= 2;
  1073. if (dsize > size )
  1074. {
  1075. *al = SSL_AD_DECODE_ERROR;
  1076. return 0;
  1077. }
  1078. while (dsize > 0)
  1079. {
  1080. OCSP_RESPID *id;
  1081. int idsize;
  1082. if (dsize < 4)
  1083. {
  1084. *al = SSL_AD_DECODE_ERROR;
  1085. return 0;
  1086. }
  1087. n2s(data, idsize);
  1088. dsize -= 2 + idsize;
  1089. size -= 2 + idsize;
  1090. if (dsize < 0)
  1091. {
  1092. *al = SSL_AD_DECODE_ERROR;
  1093. return 0;
  1094. }
  1095. sdata = data;
  1096. data += idsize;
  1097. id = d2i_OCSP_RESPID(NULL,
  1098. &sdata, idsize);
  1099. if (!id)
  1100. {
  1101. *al = SSL_AD_DECODE_ERROR;
  1102. return 0;
  1103. }
  1104. if (data != sdata)
  1105. {
  1106. OCSP_RESPID_free(id);
  1107. *al = SSL_AD_DECODE_ERROR;
  1108. return 0;
  1109. }
  1110. if (!s->tlsext_ocsp_ids
  1111. && !(s->tlsext_ocsp_ids =
  1112. sk_OCSP_RESPID_new_null()))
  1113. {
  1114. OCSP_RESPID_free(id);
  1115. *al = SSL_AD_INTERNAL_ERROR;
  1116. return 0;
  1117. }
  1118. if (!sk_OCSP_RESPID_push(
  1119. s->tlsext_ocsp_ids, id))
  1120. {
  1121. OCSP_RESPID_free(id);
  1122. *al = SSL_AD_INTERNAL_ERROR;
  1123. return 0;
  1124. }
  1125. }
  1126. /* Read in request_extensions */
  1127. if (size < 2)
  1128. {
  1129. *al = SSL_AD_DECODE_ERROR;
  1130. return 0;
  1131. }
  1132. n2s(data,dsize);
  1133. size -= 2;
  1134. if (dsize != size)
  1135. {
  1136. *al = SSL_AD_DECODE_ERROR;
  1137. return 0;
  1138. }
  1139. sdata = data;
  1140. if (dsize > 0)
  1141. {
  1142. if (s->tlsext_ocsp_exts)
  1143. {
  1144. sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
  1145. X509_EXTENSION_free);
  1146. }
  1147. s->tlsext_ocsp_exts =
  1148. d2i_X509_EXTENSIONS(NULL,
  1149. &sdata, dsize);
  1150. if (!s->tlsext_ocsp_exts
  1151. || (data + dsize != sdata))
  1152. {
  1153. *al = SSL_AD_DECODE_ERROR;
  1154. return 0;
  1155. }
  1156. }
  1157. }
  1158. /* We don't know what to do with any other type
  1159. * so ignore it.
  1160. */
  1161. else
  1162. s->tlsext_status_type = -1;
  1163. }
  1164. #ifndef OPENSSL_NO_HEARTBEATS
  1165. else if (type == TLSEXT_TYPE_heartbeat)
  1166. {
  1167. switch(data[0])
  1168. {
  1169. case 0x01: /* Client allows us to send HB requests */
  1170. s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
  1171. break;
  1172. case 0x02: /* Client doesn't accept HB requests */
  1173. s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
  1174. s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
  1175. break;
  1176. default: *al = SSL_AD_ILLEGAL_PARAMETER;
  1177. return 0;
  1178. }
  1179. }
  1180. #endif
  1181. #ifndef OPENSSL_NO_NEXTPROTONEG
  1182. else if (type == TLSEXT_TYPE_next_proto_neg &&
  1183. s->s3->tmp.finish_md_len == 0)
  1184. {
  1185. /* We shouldn't accept this extension on a
  1186. * renegotiation.
  1187. *
  1188. * s->new_session will be set on renegotiation, but we
  1189. * probably shouldn't rely that it couldn't be set on
  1190. * the initial renegotation too in certain cases (when
  1191. * there's some other reason to disallow resuming an
  1192. * earlier session -- the current code won't be doing
  1193. * anything like that, but this might change).
  1194. * A valid sign that there's been a previous handshake
  1195. * in this connection is if s->s3->tmp.finish_md_len >
  1196. * 0. (We are talking about a check that will happen
  1197. * in the Hello protocol round, well before a new
  1198. * Finished message could have been computed.) */
  1199. s->s3->next_proto_neg_seen = 1;
  1200. }
  1201. #endif
  1202. /* session ticket processed earlier */
  1203. else if (type == TLSEXT_TYPE_use_srtp)
  1204. {
  1205. if(ssl_parse_clienthello_use_srtp_ext(s, data, size,
  1206. al))
  1207. return 0;
  1208. }
  1209. data+=size;
  1210. }
  1211. *p = data;
  1212. ri_check:
  1213. /* Need RI if renegotiating */
  1214. if (!renegotiate_seen && s->renegotiate &&
  1215. !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
  1216. {
  1217. *al = SSL_AD_HANDSHAKE_FAILURE;
  1218. SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT,
  1219. SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  1220. return 0;
  1221. }
  1222. return 1;
  1223. }
  1224. #ifndef OPENSSL_NO_NEXTPROTONEG
  1225. /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
  1226. * elements of zero length are allowed and the set of elements must exactly fill
  1227. * the length of the block. */
  1228. static char ssl_next_proto_validate(unsigned char *d, unsigned len)
  1229. {
  1230. unsigned int off = 0;
  1231. while (off < len)
  1232. {
  1233. if (d[off] == 0)
  1234. return 0;
  1235. off += d[off];
  1236. off++;
  1237. }
  1238. return off == len;
  1239. }
  1240. #endif
  1241. int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
  1242. {
  1243. unsigned short length;
  1244. unsigned short type;
  1245. unsigned short size;
  1246. unsigned char *data = *p;
  1247. int tlsext_servername = 0;
  1248. int renegotiate_seen = 0;
  1249. #ifndef OPENSSL_NO_NEXTPROTONEG
  1250. s->s3->next_proto_neg_seen = 0;
  1251. #endif
  1252. #ifndef OPENSSL_NO_HEARTBEATS
  1253. s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
  1254. SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
  1255. #endif
  1256. if (data >= (d+n-2))
  1257. goto ri_check;
  1258. n2s(data,length);
  1259. if (data+length != d+n)
  1260. {
  1261. *al = SSL_AD_DECODE_ERROR;
  1262. return 0;
  1263. }
  1264. while(data <= (d+n-4))
  1265. {
  1266. n2s(data,type);
  1267. n2s(data,size);
  1268. if (data+size > (d+n))
  1269. goto ri_check;
  1270. if (s->tlsext_debug_cb)
  1271. s->tlsext_debug_cb(s, 1, type, data, size,
  1272. s->tlsext_debug_arg);
  1273. if (type == TLSEXT_TYPE_server_name)
  1274. {
  1275. if (s->tlsext_hostname == NULL || size > 0)
  1276. {
  1277. *al = TLS1_AD_UNRECOGNIZED_NAME;
  1278. return 0;
  1279. }
  1280. tlsext_servername = 1;
  1281. }
  1282. #ifndef OPENSSL_NO_EC
  1283. else if (type == TLSEXT_TYPE_ec_point_formats &&
  1284. s->version != DTLS1_VERSION)
  1285. {
  1286. unsigned char *sdata = data;
  1287. int ecpointformatlist_length = *(sdata++);
  1288. if (ecpointformatlist_length != size - 1)
  1289. {
  1290. *al = TLS1_AD_DECODE_ERROR;
  1291. return 0;
  1292. }
  1293. s->session->tlsext_ecpointformatlist_length = 0;
  1294. if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
  1295. if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
  1296. {
  1297. *al = TLS1_AD_INTERNAL_ERROR;
  1298. return 0;
  1299. }
  1300. s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
  1301. memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
  1302. #if 0
  1303. fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
  1304. sdata = s->session->tlsext_ecpointformatlist;
  1305. for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
  1306. fprintf(stderr,"%i ",*(sdata++));
  1307. fprintf(stderr,"\n");
  1308. #endif
  1309. }
  1310. #endif /* OPENSSL_NO_EC */
  1311. else if (type == TLSEXT_TYPE_session_ticket)
  1312. {
  1313. if (s->tls_session_ticket_ext_cb &&
  1314. !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
  1315. {
  1316. *al = TLS1_AD_INTERNAL_ERROR;
  1317. return 0;
  1318. }
  1319. if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
  1320. || (size > 0))
  1321. {
  1322. *al = TLS1_AD_UNSUPPORTED_EXTENSION;
  1323. return 0;
  1324. }
  1325. s->tlsext_ticket_expected = 1;
  1326. }
  1327. #ifdef TLSEXT_TYPE_opaque_prf_input
  1328. else if (type == TLSEXT_TYPE_opaque_prf_input &&
  1329. s->version != DTLS1_VERSION)
  1330. {
  1331. unsigned char *sdata = data;
  1332. if (size < 2)
  1333. {
  1334. *al = SSL_AD_DECODE_ERROR;
  1335. return 0;
  1336. }
  1337. n2s(sdata, s->s3->server_opaque_prf_input_len);
  1338. if (s->s3->server_opaque_prf_input_len != size - 2)
  1339. {
  1340. *al = SSL_AD_DECODE_ERROR;
  1341. return 0;
  1342. }
  1343. if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
  1344. OPENSSL_free(s->s3->server_opaque_prf_input);
  1345. if (s->s3->server_opaque_prf_input_len == 0)
  1346. s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
  1347. else
  1348. s->s3->server_opaque_prf_input = BUF_memdup(sdata, s->s3->server_opaque_prf_input_len);
  1349. if (s->s3->server_opaque_prf_input == NULL)
  1350. {
  1351. *al = TLS1_AD_INTERNAL_ERROR;
  1352. return 0;
  1353. }
  1354. }
  1355. #endif
  1356. else if (type == TLSEXT_TYPE_status_request &&
  1357. s->version != DTLS1_VERSION)
  1358. {
  1359. /* MUST be empty and only sent if we've requested
  1360. * a status request message.
  1361. */
  1362. if ((s->tlsext_status_type == -1) || (size > 0))
  1363. {
  1364. *al = TLS1_AD_UNSUPPORTED_EXTENSION;
  1365. return 0;
  1366. }
  1367. /* Set flag to expect CertificateStatus message */
  1368. s->tlsext_status_expected = 1;
  1369. }
  1370. #ifndef OPENSSL_NO_NEXTPROTONEG
  1371. else if (type == TLSEXT_TYPE_next_proto_neg &&
  1372. s->s3->tmp.finish_md_len == 0)
  1373. {
  1374. unsigned char *selected;
  1375. unsigned char selected_len;
  1376. /* We must have requested it. */
  1377. if ((s->ctx->next_proto_select_cb == NULL))
  1378. {
  1379. *al = TLS1_AD_UNSUPPORTED_EXTENSION;
  1380. return 0;
  1381. }
  1382. /* The data must be valid */
  1383. if (!ssl_next_proto_validate(data, size))
  1384. {
  1385. *al = TLS1_AD_DECODE_ERROR;
  1386. return 0;
  1387. }
  1388. if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data, size, s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
  1389. {
  1390. *al = TLS1_AD_INTERNAL_ERROR;
  1391. return 0;
  1392. }
  1393. s->next_proto_negotiated = OPENSSL_malloc(selected_len);
  1394. if (!s->next_proto_negotiated)
  1395. {
  1396. *al = TLS1_AD_INTERNAL_ERROR;
  1397. return 0;
  1398. }
  1399. memcpy(s->next_proto_negotiated, selected, selected_len);
  1400. s->next_proto_negotiated_len = selected_len;
  1401. s->s3->next_proto_neg_seen = 1;
  1402. }
  1403. #endif
  1404. else if (type == TLSEXT_TYPE_renegotiate)
  1405. {
  1406. if(!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
  1407. return 0;
  1408. renegotiate_seen = 1;
  1409. }
  1410. #ifndef OPENSSL_NO_HEARTBEATS
  1411. else if (type == TLSEXT_TYPE_heartbeat)
  1412. {
  1413. switch(data[0])
  1414. {
  1415. case 0x01: /* Server allows us to send HB requests */
  1416. s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
  1417. break;
  1418. case 0x02: /* Server doesn't accept HB requests */
  1419. s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
  1420. s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
  1421. break;
  1422. default: *al = SSL_AD_ILLEGAL_PARAMETER;
  1423. return 0;
  1424. }
  1425. }
  1426. #endif
  1427. else if (type == TLSEXT_TYPE_use_srtp)
  1428. {
  1429. if(ssl_parse_serverhello_use_srtp_ext(s, data, size,
  1430. al))
  1431. return 0;
  1432. }
  1433. data+=size;
  1434. }
  1435. if (data != d+n)
  1436. {
  1437. *al = SSL_AD_DECODE_ERROR;
  1438. return 0;
  1439. }
  1440. if (!s->hit && tlsext_servername == 1)
  1441. {
  1442. if (s->tlsext_hostname)
  1443. {
  1444. if (s->session->tlsext_hostname == NULL)
  1445. {
  1446. s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
  1447. if (!s->session->tlsext_hostname)
  1448. {
  1449. *al = SSL_AD_UNRECOGNIZED_NAME;
  1450. return 0;
  1451. }
  1452. }
  1453. else
  1454. {
  1455. *al = SSL_AD_DECODE_ERROR;
  1456. return 0;
  1457. }
  1458. }
  1459. }
  1460. *p = data;
  1461. ri_check:
  1462. /* Determine if we need to see RI. Strictly speaking if we want to
  1463. * avoid an attack we should *always* see RI even on initial server
  1464. * hello because the client doesn't see any renegotiation during an
  1465. * attack. However this would mean we could not connect to any server
  1466. * which doesn't support RI so for the immediate future tolerate RI
  1467. * absence on initial connect only.
  1468. */
  1469. if (!renegotiate_seen
  1470. && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
  1471. && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
  1472. {
  1473. *al = SSL_AD_HANDSHAKE_FAILURE;
  1474. SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT,
  1475. SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  1476. return 0;
  1477. }
  1478. return 1;
  1479. }
  1480. int ssl_prepare_clienthello_tlsext(SSL *s)
  1481. {
  1482. #ifndef OPENSSL_NO_EC
  1483. /* If we are client and using an elliptic curve cryptography cipher suite, send the point formats
  1484. * and elliptic curves we support.
  1485. */
  1486. int using_ecc = 0;
  1487. int i;
  1488. unsigned char *j;
  1489. unsigned long alg_k, alg_a;
  1490. STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
  1491. for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
  1492. {
  1493. SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
  1494. alg_k = c->algorithm_mkey;
  1495. alg_a = c->algorithm_auth;
  1496. if ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe) || (alg_a & SSL_aECDSA)))
  1497. {
  1498. using_ecc = 1;
  1499. break;
  1500. }
  1501. }
  1502. using_ecc = using_ecc && (s->version >= TLS1_VERSION);
  1503. if (using_ecc)
  1504. {
  1505. if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
  1506. if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
  1507. {
  1508. SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
  1509. return -1;
  1510. }
  1511. s->tlsext_ecpointformatlist_length = 3;
  1512. s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
  1513. s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
  1514. s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
  1515. /* we support all named elliptic curves in draft-ietf-tls-ecc-12 */
  1516. if (s->tlsext_ellipticcurvelist != NULL) OPENSSL_free(s->tlsext_ellipticcurvelist);
  1517. s->tlsext_ellipticcurvelist_length = sizeof(pref_list)/sizeof(pref_list[0]) * 2;
  1518. if ((s->tlsext_ellipticcurvelist = OPENSSL_malloc(s->tlsext_ellipticcurvelist_length)) == NULL)
  1519. {
  1520. s->tlsext_ellipticcurvelist_length = 0;
  1521. SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
  1522. return -1;
  1523. }
  1524. for (i = 0, j = s->tlsext_ellipticcurvelist; (unsigned int)i <
  1525. sizeof(pref_list)/sizeof(pref_list[0]); i++)
  1526. {
  1527. int id = tls1_ec_nid2curve_id(pref_list[i]);
  1528. s2n(id,j);
  1529. }
  1530. }
  1531. #endif /* OPENSSL_NO_EC */
  1532. #ifdef TLSEXT_TYPE_opaque_prf_input
  1533. {
  1534. int r = 1;
  1535. if (s->ctx->tlsext_opaque_prf_input_callback != 0)
  1536. {
  1537. r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
  1538. if (!r)
  1539. return -1;
  1540. }
  1541. if (s->tlsext_opaque_prf_input != NULL)
  1542. {
  1543. if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
  1544. OPENSSL_free(s->s3->client_opaque_prf_input);
  1545. if (s->tlsext_opaque_prf_input_len == 0)
  1546. s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
  1547. else
  1548. s->s3->client_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
  1549. if (s->s3->client_opaque_prf_input == NULL)
  1550. {
  1551. SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
  1552. return -1;
  1553. }
  1554. s->s3->client_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
  1555. }
  1556. if (r == 2)
  1557. /* at callback's request, insist on receiving an appropriate server opaque PRF input */
  1558. s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
  1559. }
  1560. #endif
  1561. return 1;
  1562. }
  1563. int ssl_prepare_serverhello_tlsext(SSL *s)
  1564. {
  1565. #ifndef OPENSSL_NO_EC
  1566. /* If we are server and using an ECC cipher suite, send the point formats we support
  1567. * if the client sent us an ECPointsFormat extension. Note that the server is not
  1568. * supposed to send an EllipticCurves extension.
  1569. */
  1570. unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1571. unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1572. int using_ecc = (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA);
  1573. using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
  1574. if (using_ecc)
  1575. {
  1576. if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
  1577. if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
  1578. {
  1579. SSLerr(SSL_F_SSL_PREPARE_SERVERHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
  1580. return -1;
  1581. }
  1582. s->tlsext_ecpointformatlist_length = 3;
  1583. s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
  1584. s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
  1585. s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
  1586. }
  1587. #endif /* OPENSSL_NO_EC */
  1588. return 1;
  1589. }
  1590. int ssl_check_clienthello_tlsext(SSL *s)
  1591. {
  1592. int ret=SSL_TLSEXT_ERR_NOACK;
  1593. int al = SSL_AD_UNRECOGNIZED_NAME;
  1594. #ifndef OPENSSL_NO_EC
  1595. /* The handling of the ECPointFormats extension is done elsewhere, namely in
  1596. * ssl3_choose_cipher in s3_lib.c.
  1597. */
  1598. /* The handling of the EllipticCurves extension is done elsewhere, namely in
  1599. * ssl3_choose_cipher in s3_lib.c.
  1600. */
  1601. #endif
  1602. if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
  1603. ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
  1604. else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
  1605. ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
  1606. /* If status request then ask callback what to do.
  1607. * Note: this must be called after servername callbacks in case
  1608. * the certificate has changed.
  1609. */
  1610. if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb)
  1611. {
  1612. int r;
  1613. r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
  1614. switch (r)
  1615. {
  1616. /* We don't want to send a status request response */
  1617. case SSL_TLSEXT_ERR_NOACK:
  1618. s->tlsext_status_expected = 0;
  1619. break;
  1620. /* status request response should be sent */
  1621. case SSL_TLSEXT_ERR_OK:
  1622. if (s->tlsext_ocsp_resp)
  1623. s->tlsext_status_expected = 1;
  1624. else
  1625. s->tlsext_status_expected = 0;
  1626. break;
  1627. /* something bad happened */
  1628. case SSL_TLSEXT_ERR_ALERT_FATAL:
  1629. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1630. al = SSL_AD_INTERNAL_ERROR;
  1631. goto err;
  1632. }
  1633. }
  1634. else
  1635. s->tlsext_status_expected = 0;
  1636. #ifdef TLSEXT_TYPE_opaque_prf_input
  1637. {
  1638. /* This sort of belongs into ssl_prepare_serverhello_tlsext(),
  1639. * but we might be sending an alert in response to the client hello,
  1640. * so this has to happen here in ssl_check_clienthello_tlsext(). */
  1641. int r = 1;
  1642. if (s->ctx->tlsext_opaque_prf_input_callback != 0)
  1643. {
  1644. r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
  1645. if (!r)
  1646. {
  1647. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1648. al = SSL_AD_INTERNAL_ERROR;
  1649. goto err;
  1650. }
  1651. }
  1652. if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
  1653. OPENSSL_free(s->s3->server_opaque_prf_input);
  1654. s->s3->server_opaque_prf_input = NULL;
  1655. if (s->tlsext_opaque_prf_input != NULL)
  1656. {
  1657. if (s->s3->client_opaque_prf_input != NULL &&
  1658. s->s3->client_opaque_prf_input_len == s->tlsext_opaque_prf_input_len)
  1659. {
  1660. /* can only use this extension if we have a server opaque PRF input
  1661. * of the same length as the client opaque PRF input! */
  1662. if (s->tlsext_opaque_prf_input_len == 0)
  1663. s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
  1664. else
  1665. s->s3->server_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
  1666. if (s->s3->server_opaque_prf_input == NULL)
  1667. {
  1668. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1669. al = SSL_AD_INTERNAL_ERROR;
  1670. goto err;
  1671. }
  1672. s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
  1673. }
  1674. }
  1675. if (r == 2 && s->s3->server_opaque_prf_input == NULL)
  1676. {
  1677. /* The callback wants to enforce use of the extension,
  1678. * but we can't do that with the client opaque PRF input;
  1679. * abort the handshake.
  1680. */
  1681. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1682. al = SSL_AD_HANDSHAKE_FAILURE;
  1683. }
  1684. }
  1685. #endif
  1686. err:
  1687. switch (ret)
  1688. {
  1689. case SSL_TLSEXT_ERR_ALERT_FATAL:
  1690. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1691. return -1;
  1692. case SSL_TLSEXT_ERR_ALERT_WARNING:
  1693. ssl3_send_alert(s,SSL3_AL_WARNING,al);
  1694. return 1;
  1695. case SSL_TLSEXT_ERR_NOACK:
  1696. s->servername_done=0;
  1697. default:
  1698. return 1;
  1699. }
  1700. }
  1701. int ssl_check_serverhello_tlsext(SSL *s)
  1702. {
  1703. int ret=SSL_TLSEXT_ERR_NOACK;
  1704. int al = SSL_AD_UNRECOGNIZED_NAME;
  1705. #ifndef OPENSSL_NO_EC
  1706. /* If we are client and using an elliptic curve cryptography cipher
  1707. * suite, then if server returns an EC point formats lists extension
  1708. * it must contain uncompressed.
  1709. */
  1710. unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1711. unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1712. if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
  1713. (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) &&
  1714. ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA)))
  1715. {
  1716. /* we are using an ECC cipher */
  1717. size_t i;
  1718. unsigned char *list;
  1719. int found_uncompressed = 0;
  1720. list = s->session->tlsext_ecpointformatlist;
  1721. for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
  1722. {
  1723. if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
  1724. {
  1725. found_uncompressed = 1;
  1726. break;
  1727. }
  1728. }
  1729. if (!found_uncompressed)
  1730. {
  1731. SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
  1732. return -1;
  1733. }
  1734. }
  1735. ret = SSL_TLSEXT_ERR_OK;
  1736. #endif /* OPENSSL_NO_EC */
  1737. if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
  1738. ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
  1739. else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
  1740. ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
  1741. #ifdef TLSEXT_TYPE_opaque_prf_input
  1742. if (s->s3->server_opaque_prf_input_len > 0)
  1743. {
  1744. /* This case may indicate that we, as a client, want to insist on using opaque PRF inputs.
  1745. * So first verify that we really have a value from the server too. */
  1746. if (s->s3->server_opaque_prf_input == NULL)
  1747. {
  1748. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1749. al = SSL_AD_HANDSHAKE_FAILURE;
  1750. }
  1751. /* Anytime the server *has* sent an opaque PRF input, we need to check
  1752. * that we have a client opaque PRF input of the same size. */
  1753. if (s->s3->client_opaque_prf_input == NULL ||
  1754. s->s3->client_opaque_prf_input_len != s->s3->server_opaque_prf_input_len)
  1755. {
  1756. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1757. al = SSL_AD_ILLEGAL_PARAMETER;
  1758. }
  1759. }
  1760. #endif
  1761. /* If we've requested certificate status and we wont get one
  1762. * tell the callback
  1763. */
  1764. if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
  1765. && s->ctx && s->ctx->tlsext_status_cb)
  1766. {
  1767. int r;
  1768. /* Set resp to NULL, resplen to -1 so callback knows
  1769. * there is no response.
  1770. */
  1771. if (s->tlsext_ocsp_resp)
  1772. {
  1773. OPENSSL_free(s->tlsext_ocsp_resp);
  1774. s->tlsext_ocsp_resp = NULL;
  1775. }
  1776. s->tlsext_ocsp_resplen = -1;
  1777. r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
  1778. if (r == 0)
  1779. {
  1780. al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
  1781. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1782. }
  1783. if (r < 0)
  1784. {
  1785. al = SSL_AD_INTERNAL_ERROR;
  1786. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1787. }
  1788. }
  1789. switch (ret)
  1790. {
  1791. case SSL_TLSEXT_ERR_ALERT_FATAL:
  1792. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1793. return -1;
  1794. case SSL_TLSEXT_ERR_ALERT_WARNING:
  1795. ssl3_send_alert(s,SSL3_AL_WARNING,al);
  1796. return 1;
  1797. case SSL_TLSEXT_ERR_NOACK:
  1798. s->servername_done=0;
  1799. default:
  1800. return 1;
  1801. }
  1802. }
  1803. /* Since the server cache lookup is done early on in the processing of the
  1804. * ClientHello, and other operations depend on the result, we need to handle
  1805. * any TLS session ticket extension at the same time.
  1806. *
  1807. * session_id: points at the session ID in the ClientHello. This code will
  1808. * read past the end of this in order to parse out the session ticket
  1809. * extension, if any.
  1810. * len: the length of the session ID.
  1811. * limit: a pointer to the first byte after the ClientHello.
  1812. * ret: (output) on return, if a ticket was decrypted, then this is set to
  1813. * point to the resulting session.
  1814. *
  1815. * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
  1816. * ciphersuite, in which case we have no use for session tickets and one will
  1817. * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
  1818. *
  1819. * Returns:
  1820. * -1: fatal error, either from parsing or decrypting the ticket.
  1821. * 0: no ticket was found (or was ignored, based on settings).
  1822. * 1: a zero length extension was found, indicating that the client supports
  1823. * session tickets but doesn't currently have one to offer.
  1824. * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
  1825. * couldn't be decrypted because of a non-fatal error.
  1826. * 3: a ticket was successfully decrypted and *ret was set.
  1827. *
  1828. * Side effects:
  1829. * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
  1830. * a new session ticket to the client because the client indicated support
  1831. * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
  1832. * a session ticket or we couldn't use the one it gave us, or if
  1833. * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
  1834. * Otherwise, s->tlsext_ticket_expected is set to 0.
  1835. */
  1836. int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
  1837. const unsigned char *limit, SSL_SESSION **ret)
  1838. {
  1839. /* Point after session ID in client hello */
  1840. const unsigned char *p = session_id + len;
  1841. unsigned short i;
  1842. *ret = NULL;
  1843. s->tlsext_ticket_expected = 0;
  1844. /* If tickets disabled behave as if no ticket present
  1845. * to permit stateful resumption.
  1846. */
  1847. if (SSL_get_options(s) & SSL_OP_NO_TICKET)
  1848. return 0;
  1849. if ((s->version <= SSL3_VERSION) || !limit)
  1850. return 0;
  1851. if (p >= limit)
  1852. return -1;
  1853. /* Skip past DTLS cookie */
  1854. if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER)
  1855. {
  1856. i = *(p++);
  1857. p+= i;
  1858. if (p >= limit)
  1859. return -1;
  1860. }
  1861. /* Skip past cipher list */
  1862. n2s(p, i);
  1863. p+= i;
  1864. if (p >= limit)
  1865. return -1;
  1866. /* Skip past compression algorithm list */
  1867. i = *(p++);
  1868. p += i;
  1869. if (p > limit)
  1870. return -1;
  1871. /* Now at start of extensions */
  1872. if ((p + 2) >= limit)
  1873. return 0;
  1874. n2s(p, i);
  1875. while ((p + 4) <= limit)
  1876. {
  1877. unsigned short type, size;
  1878. n2s(p, type);
  1879. n2s(p, size);
  1880. if (p + size > limit)
  1881. return 0;
  1882. if (type == TLSEXT_TYPE_session_ticket)
  1883. {
  1884. int r;
  1885. if (size == 0)
  1886. {
  1887. /* The client will accept a ticket but doesn't
  1888. * currently have one. */
  1889. s->tlsext_ticket_expected = 1;
  1890. return 1;
  1891. }
  1892. if (s->tls_session_secret_cb)
  1893. {
  1894. /* Indicate that the ticket couldn't be
  1895. * decrypted rather than generating the session
  1896. * from ticket now, trigger abbreviated
  1897. * handshake based on external mechanism to
  1898. * calculate the master secret later. */
  1899. return 2;
  1900. }
  1901. r = tls_decrypt_ticket(s, p, size, session_id, len, ret);
  1902. switch (r)
  1903. {
  1904. case 2: /* ticket couldn't be decrypted */
  1905. s->tlsext_ticket_expected = 1;
  1906. return 2;
  1907. case 3: /* ticket was decrypted */
  1908. return r;
  1909. case 4: /* ticket decrypted but need to renew */
  1910. s->tlsext_ticket_expected = 1;
  1911. return 3;
  1912. default: /* fatal error */
  1913. return -1;
  1914. }
  1915. }
  1916. p += size;
  1917. }
  1918. return 0;
  1919. }
  1920. /* tls_decrypt_ticket attempts to decrypt a session ticket.
  1921. *
  1922. * etick: points to the body of the session ticket extension.
  1923. * eticklen: the length of the session tickets extenion.
  1924. * sess_id: points at the session ID.
  1925. * sesslen: the length of the session ID.
  1926. * psess: (output) on return, if a ticket was decrypted, then this is set to
  1927. * point to the resulting session.
  1928. *
  1929. * Returns:
  1930. * -1: fatal error, either from parsing or decrypting the ticket.
  1931. * 2: the ticket couldn't be decrypted.
  1932. * 3: a ticket was successfully decrypted and *psess was set.
  1933. * 4: same as 3, but the ticket needs to be renewed.
  1934. */
  1935. static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
  1936. const unsigned char *sess_id, int sesslen,
  1937. SSL_SESSION **psess)
  1938. {
  1939. SSL_SESSION *sess;
  1940. unsigned char *sdec;
  1941. const unsigned char *p;
  1942. int slen, mlen, renew_ticket = 0;
  1943. unsigned char tick_hmac[EVP_MAX_MD_SIZE];
  1944. HMAC_CTX hctx;
  1945. EVP_CIPHER_CTX ctx;
  1946. SSL_CTX *tctx = s->initial_ctx;
  1947. /* Need at least keyname + iv + some encrypted data */
  1948. if (eticklen < 48)
  1949. return 2;
  1950. /* Initialize session ticket encryption and HMAC contexts */
  1951. HMAC_CTX_init(&hctx);
  1952. EVP_CIPHER_CTX_init(&ctx);
  1953. if (tctx->tlsext_ticket_key_cb)
  1954. {
  1955. unsigned char *nctick = (unsigned char *)etick;
  1956. int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
  1957. &ctx, &hctx, 0);
  1958. if (rv < 0)
  1959. return -1;
  1960. if (rv == 0)
  1961. return 2;
  1962. if (rv == 2)
  1963. renew_ticket = 1;
  1964. }
  1965. else
  1966. {
  1967. /* Check key name matches */
  1968. if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
  1969. return 2;
  1970. HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
  1971. tlsext_tick_md(), NULL);
  1972. EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
  1973. tctx->tlsext_tick_aes_key, etick + 16);
  1974. }
  1975. /* Attempt to process session ticket, first conduct sanity and
  1976. * integrity checks on ticket.
  1977. */
  1978. mlen = HMAC_size(&hctx);
  1979. if (mlen < 0)
  1980. {
  1981. EVP_CIPHER_CTX_cleanup(&ctx);
  1982. return -1;
  1983. }
  1984. eticklen -= mlen;
  1985. /* Check HMAC of encrypted ticket */
  1986. HMAC_Update(&hctx, etick, eticklen);
  1987. HMAC_Final(&hctx, tick_hmac, NULL);
  1988. HMAC_CTX_cleanup(&hctx);
  1989. if (memcmp(tick_hmac, etick + eticklen, mlen))
  1990. return 2;
  1991. /* Attempt to decrypt session data */
  1992. /* Move p after IV to start of encrypted ticket, update length */
  1993. p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
  1994. eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
  1995. sdec = OPENSSL_malloc(eticklen);
  1996. if (!sdec)
  1997. {
  1998. EVP_CIPHER_CTX_cleanup(&ctx);
  1999. return -1;
  2000. }
  2001. EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
  2002. if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0)
  2003. return 2;
  2004. slen += mlen;
  2005. EVP_CIPHER_CTX_cleanup(&ctx);
  2006. p = sdec;
  2007. sess = d2i_SSL_SESSION(NULL, &p, slen);
  2008. OPENSSL_free(sdec);
  2009. if (sess)
  2010. {
  2011. /* The session ID, if non-empty, is used by some clients to
  2012. * detect that the ticket has been accepted. So we copy it to
  2013. * the session structure. If it is empty set length to zero
  2014. * as required by standard.
  2015. */
  2016. if (sesslen)
  2017. memcpy(sess->session_id, sess_id, sesslen);
  2018. sess->session_id_length = sesslen;
  2019. *psess = sess;
  2020. if (renew_ticket)
  2021. return 4;
  2022. else
  2023. return 3;
  2024. }
  2025. ERR_clear_error();
  2026. /* For session parse failure, indicate that we need to send a new
  2027. * ticket. */
  2028. return 2;
  2029. }
  2030. /* Tables to translate from NIDs to TLS v1.2 ids */
  2031. typedef struct
  2032. {
  2033. int nid;
  2034. int id;
  2035. } tls12_lookup;
  2036. static tls12_lookup tls12_md[] = {
  2037. #ifndef OPENSSL_NO_MD5
  2038. {NID_md5, TLSEXT_hash_md5},
  2039. #endif
  2040. #ifndef OPENSSL_NO_SHA
  2041. {NID_sha1, TLSEXT_hash_sha1},
  2042. #endif
  2043. #ifndef OPENSSL_NO_SHA256
  2044. {NID_sha224, TLSEXT_hash_sha224},
  2045. {NID_sha256, TLSEXT_hash_sha256},
  2046. #endif
  2047. #ifndef OPENSSL_NO_SHA512
  2048. {NID_sha384, TLSEXT_hash_sha384},
  2049. {NID_sha512, TLSEXT_hash_sha512}
  2050. #endif
  2051. };
  2052. static tls12_lookup tls12_sig[] = {
  2053. #ifndef OPENSSL_NO_RSA
  2054. {EVP_PKEY_RSA, TLSEXT_signature_rsa},
  2055. #endif
  2056. #ifndef OPENSSL_NO_DSA
  2057. {EVP_PKEY_DSA, TLSEXT_signature_dsa},
  2058. #endif
  2059. #ifndef OPENSSL_NO_ECDSA
  2060. {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
  2061. #endif
  2062. };
  2063. static int tls12_find_id(int nid, tls12_lookup *table, size_t tlen)
  2064. {
  2065. size_t i;
  2066. for (i = 0; i < tlen; i++)
  2067. {
  2068. if (table[i].nid == nid)
  2069. return table[i].id;
  2070. }
  2071. return -1;
  2072. }
  2073. #if 0
  2074. static int tls12_find_nid(int id, tls12_lookup *table, size_t tlen)
  2075. {
  2076. size_t i;
  2077. for (i = 0; i < tlen; i++)
  2078. {
  2079. if (table[i].id == id)
  2080. return table[i].nid;
  2081. }
  2082. return -1;
  2083. }
  2084. #endif
  2085. int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
  2086. {
  2087. int sig_id, md_id;
  2088. if (!md)
  2089. return 0;
  2090. md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
  2091. sizeof(tls12_md)/sizeof(tls12_lookup));
  2092. if (md_id == -1)
  2093. return 0;
  2094. sig_id = tls12_get_sigid(pk);
  2095. if (sig_id == -1)
  2096. return 0;
  2097. p[0] = (unsigned char)md_id;
  2098. p[1] = (unsigned char)sig_id;
  2099. return 1;
  2100. }
  2101. int tls12_get_sigid(const EVP_PKEY *pk)
  2102. {
  2103. return tls12_find_id(pk->type, tls12_sig,
  2104. sizeof(tls12_sig)/sizeof(tls12_lookup));
  2105. }
  2106. const EVP_MD *tls12_get_hash(unsigned char hash_alg)
  2107. {
  2108. switch(hash_alg)
  2109. {
  2110. #ifndef OPENSSL_NO_MD5
  2111. case TLSEXT_hash_md5:
  2112. #ifdef OPENSSL_FIPS
  2113. if (FIPS_mode())
  2114. return NULL;
  2115. #endif
  2116. return EVP_md5();
  2117. #endif
  2118. #ifndef OPENSSL_NO_SHA
  2119. case TLSEXT_hash_sha1:
  2120. return EVP_sha1();
  2121. #endif
  2122. #ifndef OPENSSL_NO_SHA256
  2123. case TLSEXT_hash_sha224:
  2124. return EVP_sha224();
  2125. case TLSEXT_hash_sha256:
  2126. return EVP_sha256();
  2127. #endif
  2128. #ifndef OPENSSL_NO_SHA512
  2129. case TLSEXT_hash_sha384:
  2130. return EVP_sha384();
  2131. case TLSEXT_hash_sha512:
  2132. return EVP_sha512();
  2133. #endif
  2134. default:
  2135. return NULL;
  2136. }
  2137. }
  2138. /* Set preferred digest for each key type */
  2139. int tls1_process_sigalgs(SSL *s, const unsigned char *data, int dsize)
  2140. {
  2141. int i, idx;
  2142. const EVP_MD *md;
  2143. CERT *c = s->cert;
  2144. /* Extension ignored for TLS versions below 1.2 */
  2145. if (TLS1_get_version(s) < TLS1_2_VERSION)
  2146. return 1;
  2147. /* Should never happen */
  2148. if (!c)
  2149. return 0;
  2150. c->pkeys[SSL_PKEY_DSA_SIGN].digest = NULL;
  2151. c->pkeys[SSL_PKEY_RSA_SIGN].digest = NULL;
  2152. c->pkeys[SSL_PKEY_RSA_ENC].digest = NULL;
  2153. c->pkeys[SSL_PKEY_ECC].digest = NULL;
  2154. for (i = 0; i < dsize; i += 2)
  2155. {
  2156. unsigned char hash_alg = data[i], sig_alg = data[i+1];
  2157. switch(sig_alg)
  2158. {
  2159. #ifndef OPENSSL_NO_RSA
  2160. case TLSEXT_signature_rsa:
  2161. idx = SSL_PKEY_RSA_SIGN;
  2162. break;
  2163. #endif
  2164. #ifndef OPENSSL_NO_DSA
  2165. case TLSEXT_signature_dsa:
  2166. idx = SSL_PKEY_DSA_SIGN;
  2167. break;
  2168. #endif
  2169. #ifndef OPENSSL_NO_ECDSA
  2170. case TLSEXT_signature_ecdsa:
  2171. idx = SSL_PKEY_ECC;
  2172. break;
  2173. #endif
  2174. default:
  2175. continue;
  2176. }
  2177. if (c->pkeys[idx].digest == NULL)
  2178. {
  2179. md = tls12_get_hash(hash_alg);
  2180. if (md)
  2181. {
  2182. c->pkeys[idx].digest = md;
  2183. if (idx == SSL_PKEY_RSA_SIGN)
  2184. c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
  2185. }
  2186. }
  2187. }
  2188. /* Set any remaining keys to default values. NOTE: if alg is not
  2189. * supported it stays as NULL.
  2190. */
  2191. #ifndef OPENSSL_NO_DSA
  2192. if (!c->pkeys[SSL_PKEY_DSA_SIGN].digest)
  2193. c->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_dss1();
  2194. #endif
  2195. #ifndef OPENSSL_NO_RSA
  2196. if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest)
  2197. {
  2198. c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
  2199. c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
  2200. }
  2201. #endif
  2202. #ifndef OPENSSL_NO_ECDSA
  2203. if (!c->pkeys[SSL_PKEY_ECC].digest)
  2204. c->pkeys[SSL_PKEY_ECC].digest = EVP_ecdsa();
  2205. #endif
  2206. return 1;
  2207. }
  2208. #endif
  2209. #ifndef OPENSSL_NO_HEARTBEATS
  2210. int
  2211. tls1_process_heartbeat(SSL *s)
  2212. {
  2213. unsigned char *p = &s->s3->rrec.data[0], *pl;
  2214. unsigned short hbtype;
  2215. unsigned int payload;
  2216. unsigned int padding = 16; /* Use minimum padding */
  2217. /* Read type and payload length first */
  2218. hbtype = *p++;
  2219. n2s(p, payload);
  2220. pl = p;
  2221. if (s->msg_callback)
  2222. s->msg_callback(0, s->version, TLS1_RT_HEARTBEAT,
  2223. &s->s3->rrec.data[0], s->s3->rrec.length,
  2224. s, s->msg_callback_arg);
  2225. if (hbtype == TLS1_HB_REQUEST)
  2226. {
  2227. unsigned char *buffer, *bp;
  2228. int r;
  2229. /* Allocate memory for the response, size is 1 bytes
  2230. * message type, plus 2 bytes payload length, plus
  2231. * payload, plus padding
  2232. */
  2233. buffer = OPENSSL_malloc(1 + 2 + payload + padding);
  2234. bp = buffer;
  2235. /* Enter response type, length and copy payload */
  2236. *bp++ = TLS1_HB_RESPONSE;
  2237. s2n(payload, bp);
  2238. memcpy(bp, pl, payload);
  2239. bp += payload;
  2240. /* Random padding */
  2241. RAND_pseudo_bytes(bp, padding);
  2242. r = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buffer, 3 + payload + padding);
  2243. if (r >= 0 && s->msg_callback)
  2244. s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
  2245. buffer, 3 + payload + padding,
  2246. s, s->msg_callback_arg);
  2247. OPENSSL_free(buffer);
  2248. if (r < 0)
  2249. return r;
  2250. }
  2251. else if (hbtype == TLS1_HB_RESPONSE)
  2252. {
  2253. unsigned int seq;
  2254. /* We only send sequence numbers (2 bytes unsigned int),
  2255. * and 16 random bytes, so we just try to read the
  2256. * sequence number */
  2257. n2s(pl, seq);
  2258. if (payload == 18 && seq == s->tlsext_hb_seq)
  2259. {
  2260. s->tlsext_hb_seq++;
  2261. s->tlsext_hb_pending = 0;
  2262. }
  2263. }
  2264. return 0;
  2265. }
  2266. int
  2267. tls1_heartbeat(SSL *s)
  2268. {
  2269. unsigned char *buf, *p;
  2270. int ret;
  2271. unsigned int payload = 18; /* Sequence number + random bytes */
  2272. unsigned int padding = 16; /* Use minimum padding */
  2273. /* Only send if peer supports and accepts HB requests... */
  2274. if (!(s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) ||
  2275. s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_SEND_REQUESTS)
  2276. {
  2277. SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_TLS_HEARTBEAT_PEER_DOESNT_ACCEPT);
  2278. return -1;
  2279. }
  2280. /* ...and there is none in flight yet... */
  2281. if (s->tlsext_hb_pending)
  2282. {
  2283. SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_TLS_HEARTBEAT_PENDING);
  2284. return -1;
  2285. }
  2286. /* ...and no handshake in progress. */
  2287. if (SSL_in_init(s) || s->in_handshake)
  2288. {
  2289. SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_UNEXPECTED_MESSAGE);
  2290. return -1;
  2291. }
  2292. /* Check if padding is too long, payload and padding
  2293. * must not exceed 2^14 - 3 = 16381 bytes in total.
  2294. */
  2295. OPENSSL_assert(payload + padding <= 16381);
  2296. /* Create HeartBeat message, we just use a sequence number
  2297. * as payload to distuingish different messages and add
  2298. * some random stuff.
  2299. * - Message Type, 1 byte
  2300. * - Payload Length, 2 bytes (unsigned int)
  2301. * - Payload, the sequence number (2 bytes uint)
  2302. * - Payload, random bytes (16 bytes uint)
  2303. * - Padding
  2304. */
  2305. buf = OPENSSL_malloc(1 + 2 + payload + padding);
  2306. p = buf;
  2307. /* Message Type */
  2308. *p++ = TLS1_HB_REQUEST;
  2309. /* Payload length (18 bytes here) */
  2310. s2n(payload, p);
  2311. /* Sequence number */
  2312. s2n(s->tlsext_hb_seq, p);
  2313. /* 16 random bytes */
  2314. RAND_pseudo_bytes(p, 16);
  2315. p += 16;
  2316. /* Random padding */
  2317. RAND_pseudo_bytes(p, padding);
  2318. ret = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buf, 3 + payload + padding);
  2319. if (ret >= 0)
  2320. {
  2321. if (s->msg_callback)
  2322. s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
  2323. buf, 3 + payload + padding,
  2324. s, s->msg_callback_arg);
  2325. s->tlsext_hb_pending = 1;
  2326. }
  2327. OPENSSL_free(buf);
  2328. return ret;
  2329. }
  2330. #endif