t1_lib.c 74 KB

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