t1_lib.c 89 KB

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