t1_lib.c 89 KB

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