t1_lib.c 74 KB

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