t1_lib.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  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 "ssl_locl.h"
  117. const char tls1_version_str[]="TLSv1" OPENSSL_VERSION_PTEXT;
  118. #ifndef OPENSSL_NO_TLSEXT
  119. static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
  120. const unsigned char *sess_id, int sesslen,
  121. SSL_SESSION **psess);
  122. #endif
  123. SSL3_ENC_METHOD TLSv1_enc_data={
  124. tls1_enc,
  125. tls1_mac,
  126. tls1_setup_key_block,
  127. tls1_generate_master_secret,
  128. tls1_change_cipher_state,
  129. tls1_final_finish_mac,
  130. TLS1_FINISH_MAC_LENGTH,
  131. tls1_cert_verify_mac,
  132. TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
  133. TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
  134. tls1_alert_code,
  135. };
  136. long tls1_default_timeout(void)
  137. {
  138. /* 2 hours, the 24 hours mentioned in the TLSv1 spec
  139. * is way too long for http, the cache would over fill */
  140. return(60*60*2);
  141. }
  142. int tls1_new(SSL *s)
  143. {
  144. if (!ssl3_new(s)) return(0);
  145. s->method->ssl_clear(s);
  146. return(1);
  147. }
  148. void tls1_free(SSL *s)
  149. {
  150. #ifndef OPENSSL_NO_TLSEXT
  151. if (s->tlsext_session_ticket)
  152. {
  153. OPENSSL_free(s->tlsext_session_ticket);
  154. }
  155. #endif /* OPENSSL_NO_TLSEXT */
  156. ssl3_free(s);
  157. }
  158. void tls1_clear(SSL *s)
  159. {
  160. ssl3_clear(s);
  161. s->version=TLS1_VERSION;
  162. }
  163. #ifndef OPENSSL_NO_EC
  164. static int nid_list[] =
  165. {
  166. NID_sect163k1, /* sect163k1 (1) */
  167. NID_sect163r1, /* sect163r1 (2) */
  168. NID_sect163r2, /* sect163r2 (3) */
  169. NID_sect193r1, /* sect193r1 (4) */
  170. NID_sect193r2, /* sect193r2 (5) */
  171. NID_sect233k1, /* sect233k1 (6) */
  172. NID_sect233r1, /* sect233r1 (7) */
  173. NID_sect239k1, /* sect239k1 (8) */
  174. NID_sect283k1, /* sect283k1 (9) */
  175. NID_sect283r1, /* sect283r1 (10) */
  176. NID_sect409k1, /* sect409k1 (11) */
  177. NID_sect409r1, /* sect409r1 (12) */
  178. NID_sect571k1, /* sect571k1 (13) */
  179. NID_sect571r1, /* sect571r1 (14) */
  180. NID_secp160k1, /* secp160k1 (15) */
  181. NID_secp160r1, /* secp160r1 (16) */
  182. NID_secp160r2, /* secp160r2 (17) */
  183. NID_secp192k1, /* secp192k1 (18) */
  184. NID_X9_62_prime192v1, /* secp192r1 (19) */
  185. NID_secp224k1, /* secp224k1 (20) */
  186. NID_secp224r1, /* secp224r1 (21) */
  187. NID_secp256k1, /* secp256k1 (22) */
  188. NID_X9_62_prime256v1, /* secp256r1 (23) */
  189. NID_secp384r1, /* secp384r1 (24) */
  190. NID_secp521r1 /* secp521r1 (25) */
  191. };
  192. int tls1_ec_curve_id2nid(int curve_id)
  193. {
  194. /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
  195. if ((curve_id < 1) || ((unsigned int)curve_id >
  196. sizeof(nid_list)/sizeof(nid_list[0])))
  197. return 0;
  198. return nid_list[curve_id-1];
  199. }
  200. int tls1_ec_nid2curve_id(int nid)
  201. {
  202. /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
  203. switch (nid)
  204. {
  205. case NID_sect163k1: /* sect163k1 (1) */
  206. return 1;
  207. case NID_sect163r1: /* sect163r1 (2) */
  208. return 2;
  209. case NID_sect163r2: /* sect163r2 (3) */
  210. return 3;
  211. case NID_sect193r1: /* sect193r1 (4) */
  212. return 4;
  213. case NID_sect193r2: /* sect193r2 (5) */
  214. return 5;
  215. case NID_sect233k1: /* sect233k1 (6) */
  216. return 6;
  217. case NID_sect233r1: /* sect233r1 (7) */
  218. return 7;
  219. case NID_sect239k1: /* sect239k1 (8) */
  220. return 8;
  221. case NID_sect283k1: /* sect283k1 (9) */
  222. return 9;
  223. case NID_sect283r1: /* sect283r1 (10) */
  224. return 10;
  225. case NID_sect409k1: /* sect409k1 (11) */
  226. return 11;
  227. case NID_sect409r1: /* sect409r1 (12) */
  228. return 12;
  229. case NID_sect571k1: /* sect571k1 (13) */
  230. return 13;
  231. case NID_sect571r1: /* sect571r1 (14) */
  232. return 14;
  233. case NID_secp160k1: /* secp160k1 (15) */
  234. return 15;
  235. case NID_secp160r1: /* secp160r1 (16) */
  236. return 16;
  237. case NID_secp160r2: /* secp160r2 (17) */
  238. return 17;
  239. case NID_secp192k1: /* secp192k1 (18) */
  240. return 18;
  241. case NID_X9_62_prime192v1: /* secp192r1 (19) */
  242. return 19;
  243. case NID_secp224k1: /* secp224k1 (20) */
  244. return 20;
  245. case NID_secp224r1: /* secp224r1 (21) */
  246. return 21;
  247. case NID_secp256k1: /* secp256k1 (22) */
  248. return 22;
  249. case NID_X9_62_prime256v1: /* secp256r1 (23) */
  250. return 23;
  251. case NID_secp384r1: /* secp384r1 (24) */
  252. return 24;
  253. case NID_secp521r1: /* secp521r1 (25) */
  254. return 25;
  255. default:
  256. return 0;
  257. }
  258. }
  259. #endif /* OPENSSL_NO_EC */
  260. #ifndef OPENSSL_NO_TLSEXT
  261. unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
  262. {
  263. int extdatalen=0;
  264. unsigned char *ret = p;
  265. /* don't add extensions for SSLv3 unless doing secure renegotiation */
  266. if (s->client_version == SSL3_VERSION
  267. && !s->s3->send_connection_binding)
  268. return p;
  269. ret+=2;
  270. if (ret>=limit) return NULL; /* this really never occurs, but ... */
  271. if (s->tlsext_hostname != NULL)
  272. {
  273. /* Add TLS extension servername to the Client Hello message */
  274. unsigned long size_str;
  275. long lenmax;
  276. /* check for enough space.
  277. 4 for the servername type and entension length
  278. 2 for servernamelist length
  279. 1 for the hostname type
  280. 2 for hostname length
  281. + hostname length
  282. */
  283. if ((lenmax = limit - ret - 9) < 0
  284. || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
  285. return NULL;
  286. /* extension type and length */
  287. s2n(TLSEXT_TYPE_server_name,ret);
  288. s2n(size_str+5,ret);
  289. /* length of servername list */
  290. s2n(size_str+3,ret);
  291. /* hostname type, length and hostname */
  292. *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
  293. s2n(size_str,ret);
  294. memcpy(ret, s->tlsext_hostname, size_str);
  295. ret+=size_str;
  296. }
  297. /* Add RI if renegotiating */
  298. if (s->new_session)
  299. {
  300. int el;
  301. if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
  302. {
  303. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  304. return NULL;
  305. }
  306. if((limit - p - 4 - el) < 0) return NULL;
  307. s2n(TLSEXT_TYPE_renegotiate,ret);
  308. s2n(el,ret);
  309. if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
  310. {
  311. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  312. return NULL;
  313. }
  314. ret += el;
  315. }
  316. #ifndef OPENSSL_NO_EC
  317. if (s->tlsext_ecpointformatlist != NULL &&
  318. s->version != DTLS1_VERSION)
  319. {
  320. /* Add TLS extension ECPointFormats to the ClientHello message */
  321. long lenmax;
  322. if ((lenmax = limit - ret - 5) < 0) return NULL;
  323. if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
  324. if (s->tlsext_ecpointformatlist_length > 255)
  325. {
  326. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  327. return NULL;
  328. }
  329. s2n(TLSEXT_TYPE_ec_point_formats,ret);
  330. s2n(s->tlsext_ecpointformatlist_length + 1,ret);
  331. *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
  332. memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
  333. ret+=s->tlsext_ecpointformatlist_length;
  334. }
  335. if (s->tlsext_ellipticcurvelist != NULL &&
  336. s->version != DTLS1_VERSION)
  337. {
  338. /* Add TLS extension EllipticCurves to the ClientHello message */
  339. long lenmax;
  340. if ((lenmax = limit - ret - 6) < 0) return NULL;
  341. if (s->tlsext_ellipticcurvelist_length > (unsigned long)lenmax) return NULL;
  342. if (s->tlsext_ellipticcurvelist_length > 65532)
  343. {
  344. SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  345. return NULL;
  346. }
  347. s2n(TLSEXT_TYPE_elliptic_curves,ret);
  348. s2n(s->tlsext_ellipticcurvelist_length + 2, ret);
  349. /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
  350. * elliptic_curve_list, but the examples use two bytes.
  351. * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
  352. * resolves this to two bytes.
  353. */
  354. s2n(s->tlsext_ellipticcurvelist_length, ret);
  355. memcpy(ret, s->tlsext_ellipticcurvelist, s->tlsext_ellipticcurvelist_length);
  356. ret+=s->tlsext_ellipticcurvelist_length;
  357. }
  358. #endif /* OPENSSL_NO_EC */
  359. if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
  360. {
  361. int ticklen;
  362. if (!s->new_session && s->session && s->session->tlsext_tick)
  363. ticklen = s->session->tlsext_ticklen;
  364. else if (s->session && s->tlsext_session_ticket &&
  365. s->tlsext_session_ticket->data)
  366. {
  367. ticklen = s->tlsext_session_ticket->length;
  368. s->session->tlsext_tick = OPENSSL_malloc(ticklen);
  369. if (!s->session->tlsext_tick)
  370. return NULL;
  371. memcpy(s->session->tlsext_tick,
  372. s->tlsext_session_ticket->data,
  373. ticklen);
  374. s->session->tlsext_ticklen = ticklen;
  375. }
  376. else
  377. ticklen = 0;
  378. if (ticklen == 0 && s->tlsext_session_ticket &&
  379. s->tlsext_session_ticket->data == NULL)
  380. goto skip_ext;
  381. /* Check for enough room 2 for extension type, 2 for len
  382. * rest for ticket
  383. */
  384. if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
  385. s2n(TLSEXT_TYPE_session_ticket,ret);
  386. s2n(ticklen,ret);
  387. if (ticklen)
  388. {
  389. memcpy(ret, s->session->tlsext_tick, ticklen);
  390. ret += ticklen;
  391. }
  392. }
  393. skip_ext:
  394. #ifdef TLSEXT_TYPE_opaque_prf_input
  395. if (s->s3->client_opaque_prf_input != NULL &&
  396. s->version != DTLS1_VERSION)
  397. {
  398. size_t col = s->s3->client_opaque_prf_input_len;
  399. if ((long)(limit - ret - 6 - col < 0))
  400. return NULL;
  401. if (col > 0xFFFD) /* can't happen */
  402. return NULL;
  403. s2n(TLSEXT_TYPE_opaque_prf_input, ret);
  404. s2n(col + 2, ret);
  405. s2n(col, ret);
  406. memcpy(ret, s->s3->client_opaque_prf_input, col);
  407. ret += col;
  408. }
  409. #endif
  410. if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp &&
  411. s->version != DTLS1_VERSION)
  412. {
  413. int i;
  414. long extlen, idlen, itmp;
  415. OCSP_RESPID *id;
  416. idlen = 0;
  417. for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
  418. {
  419. id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
  420. itmp = i2d_OCSP_RESPID(id, NULL);
  421. if (itmp <= 0)
  422. return NULL;
  423. idlen += itmp + 2;
  424. }
  425. if (s->tlsext_ocsp_exts)
  426. {
  427. extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
  428. if (extlen < 0)
  429. return NULL;
  430. }
  431. else
  432. extlen = 0;
  433. if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL;
  434. s2n(TLSEXT_TYPE_status_request, ret);
  435. if (extlen + idlen > 0xFFF0)
  436. return NULL;
  437. s2n(extlen + idlen + 5, ret);
  438. *(ret++) = TLSEXT_STATUSTYPE_ocsp;
  439. s2n(idlen, ret);
  440. for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
  441. {
  442. /* save position of id len */
  443. unsigned char *q = ret;
  444. id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
  445. /* skip over id len */
  446. ret += 2;
  447. itmp = i2d_OCSP_RESPID(id, &ret);
  448. /* write id len */
  449. s2n(itmp, q);
  450. }
  451. s2n(extlen, ret);
  452. if (extlen > 0)
  453. i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
  454. }
  455. if ((extdatalen = ret-p-2)== 0)
  456. return p;
  457. s2n(extdatalen,p);
  458. return ret;
  459. }
  460. unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
  461. {
  462. int extdatalen=0;
  463. unsigned char *ret = p;
  464. /* don't add extensions for SSLv3, unless doing secure renegotiation */
  465. if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
  466. return p;
  467. ret+=2;
  468. if (ret>=limit) return NULL; /* this really never occurs, but ... */
  469. if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
  470. {
  471. if ((long)(limit - ret - 4) < 0) return NULL;
  472. s2n(TLSEXT_TYPE_server_name,ret);
  473. s2n(0,ret);
  474. }
  475. if(s->s3->send_connection_binding)
  476. {
  477. int el;
  478. if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
  479. {
  480. SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  481. return NULL;
  482. }
  483. if((limit - p - 4 - el) < 0) return NULL;
  484. s2n(TLSEXT_TYPE_renegotiate,ret);
  485. s2n(el,ret);
  486. if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
  487. {
  488. SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  489. return NULL;
  490. }
  491. ret += el;
  492. }
  493. #ifndef OPENSSL_NO_EC
  494. if (s->tlsext_ecpointformatlist != NULL &&
  495. s->version != DTLS1_VERSION)
  496. {
  497. /* Add TLS extension ECPointFormats to the ServerHello message */
  498. long lenmax;
  499. if ((lenmax = limit - ret - 5) < 0) return NULL;
  500. if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
  501. if (s->tlsext_ecpointformatlist_length > 255)
  502. {
  503. SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
  504. return NULL;
  505. }
  506. s2n(TLSEXT_TYPE_ec_point_formats,ret);
  507. s2n(s->tlsext_ecpointformatlist_length + 1,ret);
  508. *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
  509. memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
  510. ret+=s->tlsext_ecpointformatlist_length;
  511. }
  512. /* Currently the server should not respond with a SupportedCurves extension */
  513. #endif /* OPENSSL_NO_EC */
  514. if (s->tlsext_ticket_expected
  515. && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
  516. {
  517. if ((long)(limit - ret - 4) < 0) return NULL;
  518. s2n(TLSEXT_TYPE_session_ticket,ret);
  519. s2n(0,ret);
  520. }
  521. if (s->tlsext_status_expected)
  522. {
  523. if ((long)(limit - ret - 4) < 0) return NULL;
  524. s2n(TLSEXT_TYPE_status_request,ret);
  525. s2n(0,ret);
  526. }
  527. #ifdef TLSEXT_TYPE_opaque_prf_input
  528. if (s->s3->server_opaque_prf_input != NULL &&
  529. s->version != DTLS1_VERSION)
  530. {
  531. size_t sol = s->s3->server_opaque_prf_input_len;
  532. if ((long)(limit - ret - 6 - sol) < 0)
  533. return NULL;
  534. if (sol > 0xFFFD) /* can't happen */
  535. return NULL;
  536. s2n(TLSEXT_TYPE_opaque_prf_input, ret);
  537. s2n(sol + 2, ret);
  538. s2n(sol, ret);
  539. memcpy(ret, s->s3->server_opaque_prf_input, sol);
  540. ret += sol;
  541. }
  542. #endif
  543. if (((s->s3->tmp.new_cipher->id & 0xFFFF)==0x80 || (s->s3->tmp.new_cipher->id & 0xFFFF)==0x81)
  544. && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG))
  545. { const unsigned char cryptopro_ext[36] = {
  546. 0xfd, 0xe8, /*65000*/
  547. 0x00, 0x20, /*32 bytes length*/
  548. 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
  549. 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
  550. 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
  551. 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17};
  552. if (limit-ret<36) return NULL;
  553. memcpy(ret,cryptopro_ext,36);
  554. ret+=36;
  555. }
  556. if ((extdatalen = ret-p-2)== 0)
  557. return p;
  558. s2n(extdatalen,p);
  559. return ret;
  560. }
  561. int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
  562. {
  563. unsigned short type;
  564. unsigned short size;
  565. unsigned short len;
  566. unsigned char *data = *p;
  567. int renegotiate_seen = 0;
  568. s->servername_done = 0;
  569. s->tlsext_status_type = -1;
  570. if (data >= (d+n-2))
  571. goto ri_check;
  572. n2s(data,len);
  573. if (data > (d+n-len))
  574. goto ri_check;
  575. while (data <= (d+n-4))
  576. {
  577. n2s(data,type);
  578. n2s(data,size);
  579. if (data+size > (d+n))
  580. goto ri_check;
  581. #if 0
  582. fprintf(stderr,"Received extension type %d size %d\n",type,size);
  583. #endif
  584. if (s->tlsext_debug_cb)
  585. s->tlsext_debug_cb(s, 0, type, data, size,
  586. s->tlsext_debug_arg);
  587. /* The servername extension is treated as follows:
  588. - Only the hostname type is supported with a maximum length of 255.
  589. - The servername is rejected if too long or if it contains zeros,
  590. in which case an fatal alert is generated.
  591. - The servername field is maintained together with the session cache.
  592. - When a session is resumed, the servername call back invoked in order
  593. to allow the application to position itself to the right context.
  594. - The servername is acknowledged if it is new for a session or when
  595. it is identical to a previously used for the same session.
  596. Applications can control the behaviour. They can at any time
  597. set a 'desirable' servername for a new SSL object. This can be the
  598. case for example with HTTPS when a Host: header field is received and
  599. a renegotiation is requested. In this case, a possible servername
  600. presented in the new client hello is only acknowledged if it matches
  601. the value of the Host: field.
  602. - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
  603. if they provide for changing an explicit servername context for the session,
  604. i.e. when the session has been established with a servername extension.
  605. - On session reconnect, the servername extension may be absent.
  606. */
  607. if (type == TLSEXT_TYPE_server_name)
  608. {
  609. unsigned char *sdata;
  610. int servname_type;
  611. int dsize;
  612. if (size < 2)
  613. {
  614. *al = SSL_AD_DECODE_ERROR;
  615. return 0;
  616. }
  617. n2s(data,dsize);
  618. size -= 2;
  619. if (dsize > size )
  620. {
  621. *al = SSL_AD_DECODE_ERROR;
  622. return 0;
  623. }
  624. sdata = data;
  625. while (dsize > 3)
  626. {
  627. servname_type = *(sdata++);
  628. n2s(sdata,len);
  629. dsize -= 3;
  630. if (len > dsize)
  631. {
  632. *al = SSL_AD_DECODE_ERROR;
  633. return 0;
  634. }
  635. if (s->servername_done == 0)
  636. switch (servname_type)
  637. {
  638. case TLSEXT_NAMETYPE_host_name:
  639. if (!s->hit)
  640. {
  641. if(s->session->tlsext_hostname)
  642. {
  643. *al = SSL_AD_DECODE_ERROR;
  644. return 0;
  645. }
  646. if (len > TLSEXT_MAXLEN_host_name)
  647. {
  648. *al = TLS1_AD_UNRECOGNIZED_NAME;
  649. return 0;
  650. }
  651. if ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL)
  652. {
  653. *al = TLS1_AD_INTERNAL_ERROR;
  654. return 0;
  655. }
  656. memcpy(s->session->tlsext_hostname, sdata, len);
  657. s->session->tlsext_hostname[len]='\0';
  658. if (strlen(s->session->tlsext_hostname) != len) {
  659. OPENSSL_free(s->session->tlsext_hostname);
  660. s->session->tlsext_hostname = NULL;
  661. *al = TLS1_AD_UNRECOGNIZED_NAME;
  662. return 0;
  663. }
  664. s->servername_done = 1;
  665. }
  666. else
  667. s->servername_done = s->session->tlsext_hostname
  668. && strlen(s->session->tlsext_hostname) == len
  669. && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
  670. break;
  671. default:
  672. break;
  673. }
  674. dsize -= len;
  675. }
  676. if (dsize != 0)
  677. {
  678. *al = SSL_AD_DECODE_ERROR;
  679. return 0;
  680. }
  681. }
  682. #ifndef OPENSSL_NO_EC
  683. else if (type == TLSEXT_TYPE_ec_point_formats &&
  684. s->version != DTLS1_VERSION)
  685. {
  686. unsigned char *sdata = data;
  687. int ecpointformatlist_length = *(sdata++);
  688. if (ecpointformatlist_length != size - 1)
  689. {
  690. *al = TLS1_AD_DECODE_ERROR;
  691. return 0;
  692. }
  693. if (!s->hit)
  694. {
  695. if(s->session->tlsext_ecpointformatlist)
  696. {
  697. OPENSSL_free(s->session->tlsext_ecpointformatlist);
  698. s->session->tlsext_ecpointformatlist = NULL;
  699. }
  700. s->session->tlsext_ecpointformatlist_length = 0;
  701. if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
  702. {
  703. *al = TLS1_AD_INTERNAL_ERROR;
  704. return 0;
  705. }
  706. s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
  707. memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
  708. }
  709. #if 0
  710. fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length);
  711. sdata = s->session->tlsext_ecpointformatlist;
  712. for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
  713. fprintf(stderr,"%i ",*(sdata++));
  714. fprintf(stderr,"\n");
  715. #endif
  716. }
  717. else if (type == TLSEXT_TYPE_elliptic_curves &&
  718. s->version != DTLS1_VERSION)
  719. {
  720. unsigned char *sdata = data;
  721. int ellipticcurvelist_length = (*(sdata++) << 8);
  722. ellipticcurvelist_length += (*(sdata++));
  723. if (ellipticcurvelist_length != size - 2)
  724. {
  725. *al = TLS1_AD_DECODE_ERROR;
  726. return 0;
  727. }
  728. if (!s->hit)
  729. {
  730. if(s->session->tlsext_ellipticcurvelist)
  731. {
  732. *al = TLS1_AD_DECODE_ERROR;
  733. return 0;
  734. }
  735. s->session->tlsext_ellipticcurvelist_length = 0;
  736. if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL)
  737. {
  738. *al = TLS1_AD_INTERNAL_ERROR;
  739. return 0;
  740. }
  741. s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length;
  742. memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length);
  743. }
  744. #if 0
  745. fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length);
  746. sdata = s->session->tlsext_ellipticcurvelist;
  747. for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
  748. fprintf(stderr,"%i ",*(sdata++));
  749. fprintf(stderr,"\n");
  750. #endif
  751. }
  752. #endif /* OPENSSL_NO_EC */
  753. #ifdef TLSEXT_TYPE_opaque_prf_input
  754. else if (type == TLSEXT_TYPE_opaque_prf_input &&
  755. s->version != DTLS1_VERSION)
  756. {
  757. unsigned char *sdata = data;
  758. if (size < 2)
  759. {
  760. *al = SSL_AD_DECODE_ERROR;
  761. return 0;
  762. }
  763. n2s(sdata, s->s3->client_opaque_prf_input_len);
  764. if (s->s3->client_opaque_prf_input_len != size - 2)
  765. {
  766. *al = SSL_AD_DECODE_ERROR;
  767. return 0;
  768. }
  769. if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
  770. OPENSSL_free(s->s3->client_opaque_prf_input);
  771. if (s->s3->client_opaque_prf_input_len == 0)
  772. s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
  773. else
  774. s->s3->client_opaque_prf_input = BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
  775. if (s->s3->client_opaque_prf_input == NULL)
  776. {
  777. *al = TLS1_AD_INTERNAL_ERROR;
  778. return 0;
  779. }
  780. }
  781. #endif
  782. else if (type == TLSEXT_TYPE_session_ticket)
  783. {
  784. if (s->tls_session_ticket_ext_cb &&
  785. !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
  786. {
  787. *al = TLS1_AD_INTERNAL_ERROR;
  788. return 0;
  789. }
  790. }
  791. else if (type == TLSEXT_TYPE_renegotiate)
  792. {
  793. if(!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
  794. return 0;
  795. renegotiate_seen = 1;
  796. }
  797. else if (type == TLSEXT_TYPE_status_request &&
  798. s->version != DTLS1_VERSION && s->ctx->tlsext_status_cb)
  799. {
  800. if (size < 5)
  801. {
  802. *al = SSL_AD_DECODE_ERROR;
  803. return 0;
  804. }
  805. s->tlsext_status_type = *data++;
  806. size--;
  807. if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
  808. {
  809. const unsigned char *sdata;
  810. int dsize;
  811. /* Read in responder_id_list */
  812. n2s(data,dsize);
  813. size -= 2;
  814. if (dsize > size )
  815. {
  816. *al = SSL_AD_DECODE_ERROR;
  817. return 0;
  818. }
  819. while (dsize > 0)
  820. {
  821. OCSP_RESPID *id;
  822. int idsize;
  823. if (dsize < 4)
  824. {
  825. *al = SSL_AD_DECODE_ERROR;
  826. return 0;
  827. }
  828. n2s(data, idsize);
  829. dsize -= 2 + idsize;
  830. size -= 2 + idsize;
  831. if (dsize < 0)
  832. {
  833. *al = SSL_AD_DECODE_ERROR;
  834. return 0;
  835. }
  836. sdata = data;
  837. data += idsize;
  838. id = d2i_OCSP_RESPID(NULL,
  839. &sdata, idsize);
  840. if (!id)
  841. {
  842. *al = SSL_AD_DECODE_ERROR;
  843. return 0;
  844. }
  845. if (data != sdata)
  846. {
  847. OCSP_RESPID_free(id);
  848. *al = SSL_AD_DECODE_ERROR;
  849. return 0;
  850. }
  851. if (!s->tlsext_ocsp_ids
  852. && !(s->tlsext_ocsp_ids =
  853. sk_OCSP_RESPID_new_null()))
  854. {
  855. OCSP_RESPID_free(id);
  856. *al = SSL_AD_INTERNAL_ERROR;
  857. return 0;
  858. }
  859. if (!sk_OCSP_RESPID_push(
  860. s->tlsext_ocsp_ids, id))
  861. {
  862. OCSP_RESPID_free(id);
  863. *al = SSL_AD_INTERNAL_ERROR;
  864. return 0;
  865. }
  866. }
  867. /* Read in request_extensions */
  868. if (size < 2)
  869. {
  870. *al = SSL_AD_DECODE_ERROR;
  871. return 0;
  872. }
  873. n2s(data,dsize);
  874. size -= 2;
  875. if (dsize != size)
  876. {
  877. *al = SSL_AD_DECODE_ERROR;
  878. return 0;
  879. }
  880. sdata = data;
  881. if (dsize > 0)
  882. {
  883. s->tlsext_ocsp_exts =
  884. d2i_X509_EXTENSIONS(NULL,
  885. &sdata, dsize);
  886. if (!s->tlsext_ocsp_exts
  887. || (data + dsize != sdata))
  888. {
  889. *al = SSL_AD_DECODE_ERROR;
  890. return 0;
  891. }
  892. }
  893. }
  894. /* We don't know what to do with any other type
  895. * so ignore it.
  896. */
  897. else
  898. s->tlsext_status_type = -1;
  899. }
  900. /* session ticket processed earlier */
  901. data+=size;
  902. }
  903. *p = data;
  904. ri_check:
  905. /* Need RI if renegotiating */
  906. if (!renegotiate_seen && s->new_session &&
  907. !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
  908. {
  909. *al = SSL_AD_HANDSHAKE_FAILURE;
  910. SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT,
  911. SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  912. return 0;
  913. }
  914. return 1;
  915. }
  916. int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
  917. {
  918. unsigned short length;
  919. unsigned short type;
  920. unsigned short size;
  921. unsigned char *data = *p;
  922. int tlsext_servername = 0;
  923. int renegotiate_seen = 0;
  924. if (data >= (d+n-2))
  925. goto ri_check;
  926. n2s(data,length);
  927. if (data+length != d+n)
  928. {
  929. *al = SSL_AD_DECODE_ERROR;
  930. return 0;
  931. }
  932. while(data <= (d+n-4))
  933. {
  934. n2s(data,type);
  935. n2s(data,size);
  936. if (data+size > (d+n))
  937. goto ri_check;
  938. if (s->tlsext_debug_cb)
  939. s->tlsext_debug_cb(s, 1, type, data, size,
  940. s->tlsext_debug_arg);
  941. if (type == TLSEXT_TYPE_server_name)
  942. {
  943. if (s->tlsext_hostname == NULL || size > 0)
  944. {
  945. *al = TLS1_AD_UNRECOGNIZED_NAME;
  946. return 0;
  947. }
  948. tlsext_servername = 1;
  949. }
  950. #ifndef OPENSSL_NO_EC
  951. else if (type == TLSEXT_TYPE_ec_point_formats &&
  952. s->version != DTLS1_VERSION)
  953. {
  954. unsigned char *sdata = data;
  955. int ecpointformatlist_length = *(sdata++);
  956. if (ecpointformatlist_length != size - 1)
  957. {
  958. *al = TLS1_AD_DECODE_ERROR;
  959. return 0;
  960. }
  961. s->session->tlsext_ecpointformatlist_length = 0;
  962. if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
  963. if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
  964. {
  965. *al = TLS1_AD_INTERNAL_ERROR;
  966. return 0;
  967. }
  968. s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
  969. memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
  970. #if 0
  971. fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
  972. sdata = s->session->tlsext_ecpointformatlist;
  973. for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
  974. fprintf(stderr,"%i ",*(sdata++));
  975. fprintf(stderr,"\n");
  976. #endif
  977. }
  978. #endif /* OPENSSL_NO_EC */
  979. else if (type == TLSEXT_TYPE_session_ticket)
  980. {
  981. if (s->tls_session_ticket_ext_cb &&
  982. !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
  983. {
  984. *al = TLS1_AD_INTERNAL_ERROR;
  985. return 0;
  986. }
  987. if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
  988. || (size > 0))
  989. {
  990. *al = TLS1_AD_UNSUPPORTED_EXTENSION;
  991. return 0;
  992. }
  993. s->tlsext_ticket_expected = 1;
  994. }
  995. #ifdef TLSEXT_TYPE_opaque_prf_input
  996. else if (type == TLSEXT_TYPE_opaque_prf_input &&
  997. s->version != DTLS1_VERSION)
  998. {
  999. unsigned char *sdata = data;
  1000. if (size < 2)
  1001. {
  1002. *al = SSL_AD_DECODE_ERROR;
  1003. return 0;
  1004. }
  1005. n2s(sdata, s->s3->server_opaque_prf_input_len);
  1006. if (s->s3->server_opaque_prf_input_len != size - 2)
  1007. {
  1008. *al = SSL_AD_DECODE_ERROR;
  1009. return 0;
  1010. }
  1011. if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
  1012. OPENSSL_free(s->s3->server_opaque_prf_input);
  1013. if (s->s3->server_opaque_prf_input_len == 0)
  1014. s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
  1015. else
  1016. s->s3->server_opaque_prf_input = BUF_memdup(sdata, s->s3->server_opaque_prf_input_len);
  1017. if (s->s3->server_opaque_prf_input == NULL)
  1018. {
  1019. *al = TLS1_AD_INTERNAL_ERROR;
  1020. return 0;
  1021. }
  1022. }
  1023. #endif
  1024. else if (type == TLSEXT_TYPE_status_request &&
  1025. s->version != DTLS1_VERSION)
  1026. {
  1027. /* MUST be empty and only sent if we've requested
  1028. * a status request message.
  1029. */
  1030. if ((s->tlsext_status_type == -1) || (size > 0))
  1031. {
  1032. *al = TLS1_AD_UNSUPPORTED_EXTENSION;
  1033. return 0;
  1034. }
  1035. /* Set flag to expect CertificateStatus message */
  1036. s->tlsext_status_expected = 1;
  1037. }
  1038. else if (type == TLSEXT_TYPE_renegotiate)
  1039. {
  1040. if(!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
  1041. return 0;
  1042. renegotiate_seen = 1;
  1043. }
  1044. data+=size;
  1045. }
  1046. if (data != d+n)
  1047. {
  1048. *al = SSL_AD_DECODE_ERROR;
  1049. return 0;
  1050. }
  1051. if (!s->hit && tlsext_servername == 1)
  1052. {
  1053. if (s->tlsext_hostname)
  1054. {
  1055. if (s->session->tlsext_hostname == NULL)
  1056. {
  1057. s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
  1058. if (!s->session->tlsext_hostname)
  1059. {
  1060. *al = SSL_AD_UNRECOGNIZED_NAME;
  1061. return 0;
  1062. }
  1063. }
  1064. else
  1065. {
  1066. *al = SSL_AD_DECODE_ERROR;
  1067. return 0;
  1068. }
  1069. }
  1070. }
  1071. *p = data;
  1072. ri_check:
  1073. /* Determine if we need to see RI. Strictly speaking if we want to
  1074. * avoid an attack we should *always* see RI even on initial server
  1075. * hello because the client doesn't see any renegotiation during an
  1076. * attack. However this would mean we could not connect to any server
  1077. * which doesn't support RI so for the immediate future tolerate RI
  1078. * absence on initial connect only.
  1079. */
  1080. if (!renegotiate_seen
  1081. && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
  1082. && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
  1083. {
  1084. *al = SSL_AD_HANDSHAKE_FAILURE;
  1085. SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT,
  1086. SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  1087. return 0;
  1088. }
  1089. return 1;
  1090. }
  1091. int ssl_prepare_clienthello_tlsext(SSL *s)
  1092. {
  1093. #ifndef OPENSSL_NO_EC
  1094. /* If we are client and using an elliptic curve cryptography cipher suite, send the point formats
  1095. * and elliptic curves we support.
  1096. */
  1097. int using_ecc = 0;
  1098. int i;
  1099. unsigned char *j;
  1100. unsigned long alg_k, alg_a;
  1101. STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
  1102. for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
  1103. {
  1104. SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
  1105. alg_k = c->algorithm_mkey;
  1106. alg_a = c->algorithm_auth;
  1107. if ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe) || (alg_a & SSL_aECDSA)))
  1108. {
  1109. using_ecc = 1;
  1110. break;
  1111. }
  1112. }
  1113. using_ecc = using_ecc && (s->version == TLS1_VERSION);
  1114. if (using_ecc)
  1115. {
  1116. if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
  1117. if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
  1118. {
  1119. SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
  1120. return -1;
  1121. }
  1122. s->tlsext_ecpointformatlist_length = 3;
  1123. s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
  1124. s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
  1125. s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
  1126. /* we support all named elliptic curves in draft-ietf-tls-ecc-12 */
  1127. if (s->tlsext_ellipticcurvelist != NULL) OPENSSL_free(s->tlsext_ellipticcurvelist);
  1128. s->tlsext_ellipticcurvelist_length = sizeof(nid_list)/sizeof(nid_list[0]) * 2;
  1129. if ((s->tlsext_ellipticcurvelist = OPENSSL_malloc(s->tlsext_ellipticcurvelist_length)) == NULL)
  1130. {
  1131. s->tlsext_ellipticcurvelist_length = 0;
  1132. SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
  1133. return -1;
  1134. }
  1135. for (i = 1, j = s->tlsext_ellipticcurvelist; (unsigned int)i <=
  1136. sizeof(nid_list)/sizeof(nid_list[0]); i++)
  1137. s2n(i,j);
  1138. }
  1139. #endif /* OPENSSL_NO_EC */
  1140. #ifdef TLSEXT_TYPE_opaque_prf_input
  1141. {
  1142. int r = 1;
  1143. if (s->ctx->tlsext_opaque_prf_input_callback != 0)
  1144. {
  1145. r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
  1146. if (!r)
  1147. return -1;
  1148. }
  1149. if (s->tlsext_opaque_prf_input != NULL)
  1150. {
  1151. if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
  1152. OPENSSL_free(s->s3->client_opaque_prf_input);
  1153. if (s->tlsext_opaque_prf_input_len == 0)
  1154. s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
  1155. else
  1156. s->s3->client_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
  1157. if (s->s3->client_opaque_prf_input == NULL)
  1158. {
  1159. SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
  1160. return -1;
  1161. }
  1162. s->s3->client_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
  1163. }
  1164. if (r == 2)
  1165. /* at callback's request, insist on receiving an appropriate server opaque PRF input */
  1166. s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
  1167. }
  1168. #endif
  1169. return 1;
  1170. }
  1171. int ssl_prepare_serverhello_tlsext(SSL *s)
  1172. {
  1173. #ifndef OPENSSL_NO_EC
  1174. /* If we are server and using an ECC cipher suite, send the point formats we support
  1175. * if the client sent us an ECPointsFormat extension. Note that the server is not
  1176. * supposed to send an EllipticCurves extension.
  1177. */
  1178. unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1179. unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1180. int using_ecc = (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA);
  1181. using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
  1182. if (using_ecc)
  1183. {
  1184. if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
  1185. if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
  1186. {
  1187. SSLerr(SSL_F_SSL_PREPARE_SERVERHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
  1188. return -1;
  1189. }
  1190. s->tlsext_ecpointformatlist_length = 3;
  1191. s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
  1192. s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
  1193. s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
  1194. }
  1195. #endif /* OPENSSL_NO_EC */
  1196. return 1;
  1197. }
  1198. int ssl_check_clienthello_tlsext(SSL *s)
  1199. {
  1200. int ret=SSL_TLSEXT_ERR_NOACK;
  1201. int al = SSL_AD_UNRECOGNIZED_NAME;
  1202. #ifndef OPENSSL_NO_EC
  1203. /* The handling of the ECPointFormats extension is done elsewhere, namely in
  1204. * ssl3_choose_cipher in s3_lib.c.
  1205. */
  1206. /* The handling of the EllipticCurves extension is done elsewhere, namely in
  1207. * ssl3_choose_cipher in s3_lib.c.
  1208. */
  1209. #endif
  1210. if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
  1211. ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
  1212. else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
  1213. ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
  1214. /* If status request then ask callback what to do.
  1215. * Note: this must be called after servername callbacks in case
  1216. * the certificate has changed.
  1217. */
  1218. if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb)
  1219. {
  1220. int r;
  1221. r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
  1222. switch (r)
  1223. {
  1224. /* We don't want to send a status request response */
  1225. case SSL_TLSEXT_ERR_NOACK:
  1226. s->tlsext_status_expected = 0;
  1227. break;
  1228. /* status request response should be sent */
  1229. case SSL_TLSEXT_ERR_OK:
  1230. if (s->tlsext_ocsp_resp)
  1231. s->tlsext_status_expected = 1;
  1232. else
  1233. s->tlsext_status_expected = 0;
  1234. break;
  1235. /* something bad happened */
  1236. case SSL_TLSEXT_ERR_ALERT_FATAL:
  1237. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1238. al = SSL_AD_INTERNAL_ERROR;
  1239. goto err;
  1240. }
  1241. }
  1242. else
  1243. s->tlsext_status_expected = 0;
  1244. #ifdef TLSEXT_TYPE_opaque_prf_input
  1245. {
  1246. /* This sort of belongs into ssl_prepare_serverhello_tlsext(),
  1247. * but we might be sending an alert in response to the client hello,
  1248. * so this has to happen here in ssl_check_clienthello_tlsext(). */
  1249. int r = 1;
  1250. if (s->ctx->tlsext_opaque_prf_input_callback != 0)
  1251. {
  1252. r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
  1253. if (!r)
  1254. {
  1255. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1256. al = SSL_AD_INTERNAL_ERROR;
  1257. goto err;
  1258. }
  1259. }
  1260. if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
  1261. OPENSSL_free(s->s3->server_opaque_prf_input);
  1262. s->s3->server_opaque_prf_input = NULL;
  1263. if (s->tlsext_opaque_prf_input != NULL)
  1264. {
  1265. if (s->s3->client_opaque_prf_input != NULL &&
  1266. s->s3->client_opaque_prf_input_len == s->tlsext_opaque_prf_input_len)
  1267. {
  1268. /* can only use this extension if we have a server opaque PRF input
  1269. * of the same length as the client opaque PRF input! */
  1270. if (s->tlsext_opaque_prf_input_len == 0)
  1271. s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
  1272. else
  1273. s->s3->server_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
  1274. if (s->s3->server_opaque_prf_input == NULL)
  1275. {
  1276. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1277. al = SSL_AD_INTERNAL_ERROR;
  1278. goto err;
  1279. }
  1280. s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
  1281. }
  1282. }
  1283. if (r == 2 && s->s3->server_opaque_prf_input == NULL)
  1284. {
  1285. /* The callback wants to enforce use of the extension,
  1286. * but we can't do that with the client opaque PRF input;
  1287. * abort the handshake.
  1288. */
  1289. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1290. al = SSL_AD_HANDSHAKE_FAILURE;
  1291. }
  1292. }
  1293. #endif
  1294. err:
  1295. switch (ret)
  1296. {
  1297. case SSL_TLSEXT_ERR_ALERT_FATAL:
  1298. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1299. return -1;
  1300. case SSL_TLSEXT_ERR_ALERT_WARNING:
  1301. ssl3_send_alert(s,SSL3_AL_WARNING,al);
  1302. return 1;
  1303. case SSL_TLSEXT_ERR_NOACK:
  1304. s->servername_done=0;
  1305. default:
  1306. return 1;
  1307. }
  1308. }
  1309. int ssl_check_serverhello_tlsext(SSL *s)
  1310. {
  1311. int ret=SSL_TLSEXT_ERR_NOACK;
  1312. int al = SSL_AD_UNRECOGNIZED_NAME;
  1313. #ifndef OPENSSL_NO_EC
  1314. /* If we are client and using an elliptic curve cryptography cipher
  1315. * suite, then if server returns an EC point formats lists extension
  1316. * it must contain uncompressed.
  1317. */
  1318. unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1319. unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1320. if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
  1321. (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) &&
  1322. ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA)))
  1323. {
  1324. /* we are using an ECC cipher */
  1325. size_t i;
  1326. unsigned char *list;
  1327. int found_uncompressed = 0;
  1328. list = s->session->tlsext_ecpointformatlist;
  1329. for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
  1330. {
  1331. if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
  1332. {
  1333. found_uncompressed = 1;
  1334. break;
  1335. }
  1336. }
  1337. if (!found_uncompressed)
  1338. {
  1339. SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
  1340. return -1;
  1341. }
  1342. }
  1343. ret = SSL_TLSEXT_ERR_OK;
  1344. #endif /* OPENSSL_NO_EC */
  1345. if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
  1346. ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
  1347. else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
  1348. ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
  1349. #ifdef TLSEXT_TYPE_opaque_prf_input
  1350. if (s->s3->server_opaque_prf_input_len > 0)
  1351. {
  1352. /* This case may indicate that we, as a client, want to insist on using opaque PRF inputs.
  1353. * So first verify that we really have a value from the server too. */
  1354. if (s->s3->server_opaque_prf_input == NULL)
  1355. {
  1356. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1357. al = SSL_AD_HANDSHAKE_FAILURE;
  1358. }
  1359. /* Anytime the server *has* sent an opaque PRF input, we need to check
  1360. * that we have a client opaque PRF input of the same size. */
  1361. if (s->s3->client_opaque_prf_input == NULL ||
  1362. s->s3->client_opaque_prf_input_len != s->s3->server_opaque_prf_input_len)
  1363. {
  1364. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1365. al = SSL_AD_ILLEGAL_PARAMETER;
  1366. }
  1367. }
  1368. #endif
  1369. /* If we've requested certificate status and we wont get one
  1370. * tell the callback
  1371. */
  1372. if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
  1373. && s->ctx && s->ctx->tlsext_status_cb)
  1374. {
  1375. int r;
  1376. /* Set resp to NULL, resplen to -1 so callback knows
  1377. * there is no response.
  1378. */
  1379. if (s->tlsext_ocsp_resp)
  1380. {
  1381. OPENSSL_free(s->tlsext_ocsp_resp);
  1382. s->tlsext_ocsp_resp = NULL;
  1383. }
  1384. s->tlsext_ocsp_resplen = -1;
  1385. r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
  1386. if (r == 0)
  1387. {
  1388. al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
  1389. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1390. }
  1391. if (r < 0)
  1392. {
  1393. al = SSL_AD_INTERNAL_ERROR;
  1394. ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  1395. }
  1396. }
  1397. switch (ret)
  1398. {
  1399. case SSL_TLSEXT_ERR_ALERT_FATAL:
  1400. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1401. return -1;
  1402. case SSL_TLSEXT_ERR_ALERT_WARNING:
  1403. ssl3_send_alert(s,SSL3_AL_WARNING,al);
  1404. return 1;
  1405. case SSL_TLSEXT_ERR_NOACK:
  1406. s->servername_done=0;
  1407. default:
  1408. return 1;
  1409. }
  1410. }
  1411. /* Since the server cache lookup is done early on in the processing of client
  1412. * hello and other operations depend on the result we need to handle any TLS
  1413. * session ticket extension at the same time.
  1414. */
  1415. int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
  1416. const unsigned char *limit, SSL_SESSION **ret)
  1417. {
  1418. /* Point after session ID in client hello */
  1419. const unsigned char *p = session_id + len;
  1420. unsigned short i;
  1421. /* If tickets disabled behave as if no ticket present
  1422. * to permit stateful resumption.
  1423. */
  1424. if (SSL_get_options(s) & SSL_OP_NO_TICKET)
  1425. return 1;
  1426. if ((s->version <= SSL3_VERSION) || !limit)
  1427. return 1;
  1428. if (p >= limit)
  1429. return -1;
  1430. /* Skip past DTLS cookie */
  1431. if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER)
  1432. {
  1433. i = *(p++);
  1434. p+= i;
  1435. if (p >= limit)
  1436. return -1;
  1437. }
  1438. /* Skip past cipher list */
  1439. n2s(p, i);
  1440. p+= i;
  1441. if (p >= limit)
  1442. return -1;
  1443. /* Skip past compression algorithm list */
  1444. i = *(p++);
  1445. p += i;
  1446. if (p > limit)
  1447. return -1;
  1448. /* Now at start of extensions */
  1449. if ((p + 2) >= limit)
  1450. return 1;
  1451. n2s(p, i);
  1452. while ((p + 4) <= limit)
  1453. {
  1454. unsigned short type, size;
  1455. n2s(p, type);
  1456. n2s(p, size);
  1457. if (p + size > limit)
  1458. return 1;
  1459. if (type == TLSEXT_TYPE_session_ticket)
  1460. {
  1461. /* If tickets disabled indicate cache miss which will
  1462. * trigger a full handshake
  1463. */
  1464. if (SSL_get_options(s) & SSL_OP_NO_TICKET)
  1465. return 1;
  1466. /* If zero length note client will accept a ticket
  1467. * and indicate cache miss to trigger full handshake
  1468. */
  1469. if (size == 0)
  1470. {
  1471. s->tlsext_ticket_expected = 1;
  1472. return 0; /* Cache miss */
  1473. }
  1474. if (s->tls_session_secret_cb)
  1475. {
  1476. /* Indicate cache miss here and instead of
  1477. * generating the session from ticket now,
  1478. * trigger abbreviated handshake based on
  1479. * external mechanism to calculate the master
  1480. * secret later. */
  1481. return 0;
  1482. }
  1483. return tls_decrypt_ticket(s, p, size, session_id, len,
  1484. ret);
  1485. }
  1486. p += size;
  1487. }
  1488. return 1;
  1489. }
  1490. static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
  1491. const unsigned char *sess_id, int sesslen,
  1492. SSL_SESSION **psess)
  1493. {
  1494. SSL_SESSION *sess;
  1495. unsigned char *sdec;
  1496. const unsigned char *p;
  1497. int slen, mlen, renew_ticket = 0;
  1498. unsigned char tick_hmac[EVP_MAX_MD_SIZE];
  1499. HMAC_CTX hctx;
  1500. EVP_CIPHER_CTX ctx;
  1501. SSL_CTX *tctx = s->initial_ctx;
  1502. /* Need at least keyname + iv + some encrypted data */
  1503. if (eticklen < 48)
  1504. goto tickerr;
  1505. /* Initialize session ticket encryption and HMAC contexts */
  1506. HMAC_CTX_init(&hctx);
  1507. EVP_CIPHER_CTX_init(&ctx);
  1508. if (tctx->tlsext_ticket_key_cb)
  1509. {
  1510. unsigned char *nctick = (unsigned char *)etick;
  1511. int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
  1512. &ctx, &hctx, 0);
  1513. if (rv < 0)
  1514. return -1;
  1515. if (rv == 0)
  1516. goto tickerr;
  1517. if (rv == 2)
  1518. renew_ticket = 1;
  1519. }
  1520. else
  1521. {
  1522. /* Check key name matches */
  1523. if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
  1524. goto tickerr;
  1525. HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
  1526. tlsext_tick_md(), NULL);
  1527. EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
  1528. tctx->tlsext_tick_aes_key, etick + 16);
  1529. }
  1530. /* Attempt to process session ticket, first conduct sanity and
  1531. * integrity checks on ticket.
  1532. */
  1533. mlen = HMAC_size(&hctx);
  1534. if (mlen < 0)
  1535. {
  1536. EVP_CIPHER_CTX_cleanup(&ctx);
  1537. return -1;
  1538. }
  1539. eticklen -= mlen;
  1540. /* Check HMAC of encrypted ticket */
  1541. HMAC_Update(&hctx, etick, eticklen);
  1542. HMAC_Final(&hctx, tick_hmac, NULL);
  1543. HMAC_CTX_cleanup(&hctx);
  1544. if (memcmp(tick_hmac, etick + eticklen, mlen))
  1545. goto tickerr;
  1546. /* Attempt to decrypt session data */
  1547. /* Move p after IV to start of encrypted ticket, update length */
  1548. p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
  1549. eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
  1550. sdec = OPENSSL_malloc(eticklen);
  1551. if (!sdec)
  1552. {
  1553. EVP_CIPHER_CTX_cleanup(&ctx);
  1554. return -1;
  1555. }
  1556. EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
  1557. if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0)
  1558. goto tickerr;
  1559. slen += mlen;
  1560. EVP_CIPHER_CTX_cleanup(&ctx);
  1561. p = sdec;
  1562. sess = d2i_SSL_SESSION(NULL, &p, slen);
  1563. OPENSSL_free(sdec);
  1564. if (sess)
  1565. {
  1566. /* The session ID if non-empty is used by some clients to
  1567. * detect that the ticket has been accepted. So we copy it to
  1568. * the session structure. If it is empty set length to zero
  1569. * as required by standard.
  1570. */
  1571. if (sesslen)
  1572. memcpy(sess->session_id, sess_id, sesslen);
  1573. sess->session_id_length = sesslen;
  1574. *psess = sess;
  1575. s->tlsext_ticket_expected = renew_ticket;
  1576. return 1;
  1577. }
  1578. /* If session decrypt failure indicate a cache miss and set state to
  1579. * send a new ticket
  1580. */
  1581. tickerr:
  1582. s->tlsext_ticket_expected = 1;
  1583. return 0;
  1584. }
  1585. #endif