t1_lib.c 74 KB

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