extensions.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  1. /*
  2. * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include "internal/nelem.h"
  11. #include "internal/cryptlib.h"
  12. #include "../ssl_local.h"
  13. #include "statem_local.h"
  14. #include "internal/cryptlib.h"
  15. static int final_renegotiate(SSL *s, unsigned int context, int sent);
  16. static int init_server_name(SSL *s, unsigned int context);
  17. static int final_server_name(SSL *s, unsigned int context, int sent);
  18. #ifndef OPENSSL_NO_EC
  19. static int init_ec_point_formats(SSL *s, unsigned int context);
  20. static int final_ec_pt_formats(SSL *s, unsigned int context, int sent);
  21. #endif
  22. static int init_session_ticket(SSL *s, unsigned int context);
  23. #ifndef OPENSSL_NO_OCSP
  24. static int init_status_request(SSL *s, unsigned int context);
  25. #endif
  26. #ifndef OPENSSL_NO_NEXTPROTONEG
  27. static int init_npn(SSL *s, unsigned int context);
  28. #endif
  29. static int init_alpn(SSL *s, unsigned int context);
  30. static int final_alpn(SSL *s, unsigned int context, int sent);
  31. static int init_sig_algs_cert(SSL *s, unsigned int context);
  32. static int init_sig_algs(SSL *s, unsigned int context);
  33. static int init_certificate_authorities(SSL *s, unsigned int context);
  34. static EXT_RETURN tls_construct_certificate_authorities(SSL *s, WPACKET *pkt,
  35. unsigned int context,
  36. X509 *x,
  37. size_t chainidx);
  38. static int tls_parse_certificate_authorities(SSL *s, PACKET *pkt,
  39. unsigned int context, X509 *x,
  40. size_t chainidx);
  41. #ifndef OPENSSL_NO_SRP
  42. static int init_srp(SSL *s, unsigned int context);
  43. #endif
  44. static int init_etm(SSL *s, unsigned int context);
  45. static int init_ems(SSL *s, unsigned int context);
  46. static int final_ems(SSL *s, unsigned int context, int sent);
  47. static int init_psk_kex_modes(SSL *s, unsigned int context);
  48. #ifndef OPENSSL_NO_EC
  49. static int final_key_share(SSL *s, unsigned int context, int sent);
  50. #endif
  51. #ifndef OPENSSL_NO_SRTP
  52. static int init_srtp(SSL *s, unsigned int context);
  53. #endif
  54. static int final_sig_algs(SSL *s, unsigned int context, int sent);
  55. static int final_early_data(SSL *s, unsigned int context, int sent);
  56. static int final_maxfragmentlen(SSL *s, unsigned int context, int sent);
  57. static int init_post_handshake_auth(SSL *s, unsigned int context);
  58. static int final_psk(SSL *s, unsigned int context, int sent);
  59. #ifndef OPENSSL_NO_QUIC
  60. static int init_quic_transport_params(SSL *s, unsigned int context);
  61. static int final_quic_transport_params_draft(SSL *s, unsigned int context, int sent);
  62. static int final_quic_transport_params(SSL *s, unsigned int context, int sent);
  63. #endif
  64. /* Structure to define a built-in extension */
  65. typedef struct extensions_definition_st {
  66. /* The defined type for the extension */
  67. unsigned int type;
  68. /*
  69. * The context that this extension applies to, e.g. what messages and
  70. * protocol versions
  71. */
  72. unsigned int context;
  73. /*
  74. * Initialise extension before parsing. Always called for relevant contexts
  75. * even if extension not present
  76. */
  77. int (*init)(SSL *s, unsigned int context);
  78. /* Parse extension sent from client to server */
  79. int (*parse_ctos)(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  80. size_t chainidx);
  81. /* Parse extension send from server to client */
  82. int (*parse_stoc)(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  83. size_t chainidx);
  84. /* Construct extension sent from server to client */
  85. EXT_RETURN (*construct_stoc)(SSL *s, WPACKET *pkt, unsigned int context,
  86. X509 *x, size_t chainidx);
  87. /* Construct extension sent from client to server */
  88. EXT_RETURN (*construct_ctos)(SSL *s, WPACKET *pkt, unsigned int context,
  89. X509 *x, size_t chainidx);
  90. /*
  91. * Finalise extension after parsing. Always called where an extensions was
  92. * initialised even if the extension was not present. |sent| is set to 1 if
  93. * the extension was seen, or 0 otherwise.
  94. */
  95. int (*final)(SSL *s, unsigned int context, int sent);
  96. } EXTENSION_DEFINITION;
  97. /*
  98. * Definitions of all built-in extensions. NOTE: Changes in the number or order
  99. * of these extensions should be mirrored with equivalent changes to the
  100. * indexes ( TLSEXT_IDX_* ) defined in ssl_local.h.
  101. * Each extension has an initialiser, a client and
  102. * server side parser and a finaliser. The initialiser is called (if the
  103. * extension is relevant to the given context) even if we did not see the
  104. * extension in the message that we received. The parser functions are only
  105. * called if we see the extension in the message. The finalisers are always
  106. * called if the initialiser was called.
  107. * There are also server and client side constructor functions which are always
  108. * called during message construction if the extension is relevant for the
  109. * given context.
  110. * The initialisation, parsing, finalisation and construction functions are
  111. * always called in the order defined in this list. Some extensions may depend
  112. * on others having been processed first, so the order of this list is
  113. * significant.
  114. * The extension context is defined by a series of flags which specify which
  115. * messages the extension is relevant to. These flags also specify whether the
  116. * extension is relevant to a particular protocol or protocol version.
  117. *
  118. * TODO(TLS1.3): Make sure we have a test to check the consistency of these
  119. *
  120. * NOTE: WebSphere Application Server 7+ cannot handle empty extensions at
  121. * the end, keep these extensions before signature_algorithm.
  122. */
  123. #define INVALID_EXTENSION { 0x10000, 0, NULL, NULL, NULL, NULL, NULL, NULL }
  124. static const EXTENSION_DEFINITION ext_defs[] = {
  125. {
  126. TLSEXT_TYPE_renegotiate,
  127. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  128. | SSL_EXT_SSL3_ALLOWED | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  129. NULL, tls_parse_ctos_renegotiate, tls_parse_stoc_renegotiate,
  130. tls_construct_stoc_renegotiate, tls_construct_ctos_renegotiate,
  131. final_renegotiate
  132. },
  133. {
  134. TLSEXT_TYPE_server_name,
  135. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  136. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
  137. init_server_name,
  138. tls_parse_ctos_server_name, tls_parse_stoc_server_name,
  139. tls_construct_stoc_server_name, tls_construct_ctos_server_name,
  140. final_server_name
  141. },
  142. {
  143. TLSEXT_TYPE_max_fragment_length,
  144. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  145. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
  146. NULL, tls_parse_ctos_maxfragmentlen, tls_parse_stoc_maxfragmentlen,
  147. tls_construct_stoc_maxfragmentlen, tls_construct_ctos_maxfragmentlen,
  148. final_maxfragmentlen
  149. },
  150. #ifndef OPENSSL_NO_SRP
  151. {
  152. TLSEXT_TYPE_srp,
  153. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  154. init_srp, tls_parse_ctos_srp, NULL, NULL, tls_construct_ctos_srp, NULL
  155. },
  156. #else
  157. INVALID_EXTENSION,
  158. #endif
  159. #ifndef OPENSSL_NO_EC
  160. {
  161. TLSEXT_TYPE_ec_point_formats,
  162. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  163. | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  164. init_ec_point_formats, tls_parse_ctos_ec_pt_formats, tls_parse_stoc_ec_pt_formats,
  165. tls_construct_stoc_ec_pt_formats, tls_construct_ctos_ec_pt_formats,
  166. final_ec_pt_formats
  167. },
  168. {
  169. /*
  170. * "supported_groups" is spread across several specifications.
  171. * It was originally specified as "elliptic_curves" in RFC 4492,
  172. * and broadened to include named FFDH groups by RFC 7919.
  173. * Both RFCs 4492 and 7919 do not include a provision for the server
  174. * to indicate to the client the complete list of groups supported
  175. * by the server, with the server instead just indicating the
  176. * selected group for this connection in the ServerKeyExchange
  177. * message. TLS 1.3 adds a scheme for the server to indicate
  178. * to the client its list of supported groups in the
  179. * EncryptedExtensions message, but none of the relevant
  180. * specifications permit sending supported_groups in the ServerHello.
  181. * Nonetheless (possibly due to the close proximity to the
  182. * "ec_point_formats" extension, which is allowed in the ServerHello),
  183. * there are several servers that send this extension in the
  184. * ServerHello anyway. Up to and including the 1.1.0 release,
  185. * we did not check for the presence of nonpermitted extensions,
  186. * so to avoid a regression, we must permit this extension in the
  187. * TLS 1.2 ServerHello as well.
  188. *
  189. * Note that there is no tls_parse_stoc_supported_groups function,
  190. * so we do not perform any additional parsing, validation, or
  191. * processing on the server's group list -- this is just a minimal
  192. * change to preserve compatibility with these misbehaving servers.
  193. */
  194. TLSEXT_TYPE_supported_groups,
  195. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
  196. | SSL_EXT_TLS1_2_SERVER_HELLO,
  197. NULL, tls_parse_ctos_supported_groups, NULL,
  198. tls_construct_stoc_supported_groups,
  199. tls_construct_ctos_supported_groups, NULL
  200. },
  201. #else
  202. INVALID_EXTENSION,
  203. INVALID_EXTENSION,
  204. #endif
  205. {
  206. TLSEXT_TYPE_session_ticket,
  207. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  208. | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  209. init_session_ticket, tls_parse_ctos_session_ticket,
  210. tls_parse_stoc_session_ticket, tls_construct_stoc_session_ticket,
  211. tls_construct_ctos_session_ticket, NULL
  212. },
  213. #ifndef OPENSSL_NO_OCSP
  214. {
  215. TLSEXT_TYPE_status_request,
  216. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  217. | SSL_EXT_TLS1_3_CERTIFICATE | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
  218. init_status_request, tls_parse_ctos_status_request,
  219. tls_parse_stoc_status_request, tls_construct_stoc_status_request,
  220. tls_construct_ctos_status_request, NULL
  221. },
  222. #else
  223. INVALID_EXTENSION,
  224. #endif
  225. #ifndef OPENSSL_NO_NEXTPROTONEG
  226. {
  227. TLSEXT_TYPE_next_proto_neg,
  228. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  229. | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  230. init_npn, tls_parse_ctos_npn, tls_parse_stoc_npn,
  231. tls_construct_stoc_next_proto_neg, tls_construct_ctos_npn, NULL
  232. },
  233. #else
  234. INVALID_EXTENSION,
  235. #endif
  236. {
  237. /*
  238. * Must appear in this list after server_name so that finalisation
  239. * happens after server_name callbacks
  240. */
  241. TLSEXT_TYPE_application_layer_protocol_negotiation,
  242. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  243. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
  244. init_alpn, tls_parse_ctos_alpn, tls_parse_stoc_alpn,
  245. tls_construct_stoc_alpn, tls_construct_ctos_alpn, final_alpn
  246. },
  247. #ifndef OPENSSL_NO_SRTP
  248. {
  249. TLSEXT_TYPE_use_srtp,
  250. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  251. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS | SSL_EXT_DTLS_ONLY,
  252. init_srtp, tls_parse_ctos_use_srtp, tls_parse_stoc_use_srtp,
  253. tls_construct_stoc_use_srtp, tls_construct_ctos_use_srtp, NULL
  254. },
  255. #else
  256. INVALID_EXTENSION,
  257. #endif
  258. {
  259. TLSEXT_TYPE_encrypt_then_mac,
  260. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  261. | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  262. init_etm, tls_parse_ctos_etm, tls_parse_stoc_etm,
  263. tls_construct_stoc_etm, tls_construct_ctos_etm, NULL
  264. },
  265. #ifndef OPENSSL_NO_CT
  266. {
  267. TLSEXT_TYPE_signed_certificate_timestamp,
  268. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  269. | SSL_EXT_TLS1_3_CERTIFICATE | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
  270. NULL,
  271. /*
  272. * No server side support for this, but can be provided by a custom
  273. * extension. This is an exception to the rule that custom extensions
  274. * cannot override built in ones.
  275. */
  276. NULL, tls_parse_stoc_sct, NULL, tls_construct_ctos_sct, NULL
  277. },
  278. #else
  279. INVALID_EXTENSION,
  280. #endif
  281. {
  282. TLSEXT_TYPE_extended_master_secret,
  283. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  284. | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  285. init_ems, tls_parse_ctos_ems, tls_parse_stoc_ems,
  286. tls_construct_stoc_ems, tls_construct_ctos_ems, final_ems
  287. },
  288. {
  289. TLSEXT_TYPE_signature_algorithms_cert,
  290. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
  291. init_sig_algs_cert, tls_parse_ctos_sig_algs_cert,
  292. tls_parse_ctos_sig_algs_cert,
  293. /* We do not generate signature_algorithms_cert at present. */
  294. NULL, NULL, NULL
  295. },
  296. {
  297. TLSEXT_TYPE_post_handshake_auth,
  298. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ONLY,
  299. init_post_handshake_auth,
  300. tls_parse_ctos_post_handshake_auth, NULL,
  301. NULL, tls_construct_ctos_post_handshake_auth,
  302. NULL,
  303. },
  304. {
  305. TLSEXT_TYPE_signature_algorithms,
  306. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
  307. init_sig_algs, tls_parse_ctos_sig_algs,
  308. tls_parse_ctos_sig_algs, tls_construct_ctos_sig_algs,
  309. tls_construct_ctos_sig_algs, final_sig_algs
  310. },
  311. {
  312. TLSEXT_TYPE_supported_versions,
  313. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
  314. | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST | SSL_EXT_TLS_IMPLEMENTATION_ONLY,
  315. NULL,
  316. /* Processed inline as part of version selection */
  317. NULL, tls_parse_stoc_supported_versions,
  318. tls_construct_stoc_supported_versions,
  319. tls_construct_ctos_supported_versions, NULL
  320. },
  321. {
  322. TLSEXT_TYPE_psk_kex_modes,
  323. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS_IMPLEMENTATION_ONLY
  324. | SSL_EXT_TLS1_3_ONLY,
  325. init_psk_kex_modes, tls_parse_ctos_psk_kex_modes, NULL, NULL,
  326. tls_construct_ctos_psk_kex_modes, NULL
  327. },
  328. #ifndef OPENSSL_NO_EC
  329. {
  330. /*
  331. * Must be in this list after supported_groups. We need that to have
  332. * been parsed before we do this one.
  333. */
  334. TLSEXT_TYPE_key_share,
  335. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
  336. | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST | SSL_EXT_TLS_IMPLEMENTATION_ONLY
  337. | SSL_EXT_TLS1_3_ONLY,
  338. NULL, tls_parse_ctos_key_share, tls_parse_stoc_key_share,
  339. tls_construct_stoc_key_share, tls_construct_ctos_key_share,
  340. final_key_share
  341. },
  342. #else
  343. INVALID_EXTENSION,
  344. #endif
  345. {
  346. /* Must be after key_share */
  347. TLSEXT_TYPE_cookie,
  348. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
  349. | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY,
  350. NULL, tls_parse_ctos_cookie, tls_parse_stoc_cookie,
  351. tls_construct_stoc_cookie, tls_construct_ctos_cookie, NULL
  352. },
  353. {
  354. /*
  355. * Special unsolicited ServerHello extension only used when
  356. * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set. We allow it in a ClientHello but
  357. * ignore it.
  358. */
  359. TLSEXT_TYPE_cryptopro_bug,
  360. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  361. | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  362. NULL, NULL, NULL, tls_construct_stoc_cryptopro_bug, NULL, NULL
  363. },
  364. {
  365. TLSEXT_TYPE_early_data,
  366. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
  367. | SSL_EXT_TLS1_3_NEW_SESSION_TICKET | SSL_EXT_TLS1_3_ONLY,
  368. NULL, tls_parse_ctos_early_data, tls_parse_stoc_early_data,
  369. tls_construct_stoc_early_data, tls_construct_ctos_early_data,
  370. final_early_data
  371. },
  372. {
  373. TLSEXT_TYPE_certificate_authorities,
  374. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
  375. | SSL_EXT_TLS1_3_ONLY,
  376. init_certificate_authorities,
  377. tls_parse_certificate_authorities, tls_parse_certificate_authorities,
  378. tls_construct_certificate_authorities,
  379. tls_construct_certificate_authorities, NULL,
  380. },
  381. #ifndef OPENSSL_NO_QUIC
  382. {
  383. TLSEXT_TYPE_quic_transport_parameters_draft,
  384. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
  385. | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY,
  386. init_quic_transport_params,
  387. tls_parse_ctos_quic_transport_params_draft, tls_parse_stoc_quic_transport_params_draft,
  388. tls_construct_stoc_quic_transport_params_draft, tls_construct_ctos_quic_transport_params_draft,
  389. final_quic_transport_params_draft,
  390. },
  391. {
  392. TLSEXT_TYPE_quic_transport_parameters,
  393. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
  394. | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY,
  395. init_quic_transport_params,
  396. tls_parse_ctos_quic_transport_params, tls_parse_stoc_quic_transport_params,
  397. tls_construct_stoc_quic_transport_params, tls_construct_ctos_quic_transport_params,
  398. final_quic_transport_params,
  399. },
  400. #else
  401. INVALID_EXTENSION,
  402. INVALID_EXTENSION,
  403. #endif
  404. {
  405. /* Must be immediately before pre_shared_key */
  406. TLSEXT_TYPE_padding,
  407. SSL_EXT_CLIENT_HELLO,
  408. NULL,
  409. /* We send this, but don't read it */
  410. NULL, NULL, NULL, tls_construct_ctos_padding, NULL
  411. },
  412. {
  413. /* Required by the TLSv1.3 spec to always be the last extension */
  414. TLSEXT_TYPE_psk,
  415. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
  416. | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY,
  417. NULL, tls_parse_ctos_psk, tls_parse_stoc_psk, tls_construct_stoc_psk,
  418. tls_construct_ctos_psk, final_psk
  419. }
  420. };
  421. /* Check whether an extension's context matches the current context */
  422. static int validate_context(SSL *s, unsigned int extctx, unsigned int thisctx)
  423. {
  424. /* Check we're allowed to use this extension in this context */
  425. if ((thisctx & extctx) == 0)
  426. return 0;
  427. if (SSL_IS_DTLS(s)) {
  428. if ((extctx & SSL_EXT_TLS_ONLY) != 0)
  429. return 0;
  430. } else if ((extctx & SSL_EXT_DTLS_ONLY) != 0) {
  431. return 0;
  432. }
  433. return 1;
  434. }
  435. int tls_validate_all_contexts(SSL *s, unsigned int thisctx, RAW_EXTENSION *exts)
  436. {
  437. size_t i, num_exts, builtin_num = OSSL_NELEM(ext_defs), offset;
  438. RAW_EXTENSION *thisext;
  439. unsigned int context;
  440. ENDPOINT role = ENDPOINT_BOTH;
  441. if ((thisctx & SSL_EXT_CLIENT_HELLO) != 0)
  442. role = ENDPOINT_SERVER;
  443. else if ((thisctx & SSL_EXT_TLS1_2_SERVER_HELLO) != 0)
  444. role = ENDPOINT_CLIENT;
  445. /* Calculate the number of extensions in the extensions list */
  446. num_exts = builtin_num + s->cert->custext.meths_count;
  447. for (thisext = exts, i = 0; i < num_exts; i++, thisext++) {
  448. if (!thisext->present)
  449. continue;
  450. if (i < builtin_num) {
  451. context = ext_defs[i].context;
  452. } else {
  453. custom_ext_method *meth = NULL;
  454. meth = custom_ext_find(&s->cert->custext, role, thisext->type,
  455. &offset);
  456. if (!ossl_assert(meth != NULL))
  457. return 0;
  458. context = meth->context;
  459. }
  460. if (!validate_context(s, context, thisctx))
  461. return 0;
  462. }
  463. return 1;
  464. }
  465. /*
  466. * Verify whether we are allowed to use the extension |type| in the current
  467. * |context|. Returns 1 to indicate the extension is allowed or unknown or 0 to
  468. * indicate the extension is not allowed. If returning 1 then |*found| is set to
  469. * the definition for the extension we found.
  470. */
  471. static int verify_extension(SSL *s, unsigned int context, unsigned int type,
  472. custom_ext_methods *meths, RAW_EXTENSION *rawexlist,
  473. RAW_EXTENSION **found)
  474. {
  475. size_t i;
  476. size_t builtin_num = OSSL_NELEM(ext_defs);
  477. const EXTENSION_DEFINITION *thisext;
  478. for (i = 0, thisext = ext_defs; i < builtin_num; i++, thisext++) {
  479. if (type == thisext->type) {
  480. if (!validate_context(s, thisext->context, context))
  481. return 0;
  482. *found = &rawexlist[i];
  483. return 1;
  484. }
  485. }
  486. /* Check the custom extensions */
  487. if (meths != NULL) {
  488. size_t offset = 0;
  489. ENDPOINT role = ENDPOINT_BOTH;
  490. custom_ext_method *meth = NULL;
  491. if ((context & SSL_EXT_CLIENT_HELLO) != 0)
  492. role = ENDPOINT_SERVER;
  493. else if ((context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0)
  494. role = ENDPOINT_CLIENT;
  495. meth = custom_ext_find(meths, role, type, &offset);
  496. if (meth != NULL) {
  497. if (!validate_context(s, meth->context, context))
  498. return 0;
  499. *found = &rawexlist[offset + builtin_num];
  500. return 1;
  501. }
  502. }
  503. /* Unknown extension. We allow it */
  504. *found = NULL;
  505. return 1;
  506. }
  507. /*
  508. * Check whether the context defined for an extension |extctx| means whether
  509. * the extension is relevant for the current context |thisctx| or not. Returns
  510. * 1 if the extension is relevant for this context, and 0 otherwise
  511. */
  512. int extension_is_relevant(SSL *s, unsigned int extctx, unsigned int thisctx)
  513. {
  514. int is_tls13;
  515. /*
  516. * For HRR we haven't selected the version yet but we know it will be
  517. * TLSv1.3
  518. */
  519. if ((thisctx & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0)
  520. is_tls13 = 1;
  521. else
  522. is_tls13 = SSL_IS_TLS13(s);
  523. if ((SSL_IS_DTLS(s)
  524. && (extctx & SSL_EXT_TLS_IMPLEMENTATION_ONLY) != 0)
  525. || (s->version == SSL3_VERSION
  526. && (extctx & SSL_EXT_SSL3_ALLOWED) == 0)
  527. /*
  528. * Note that SSL_IS_TLS13() means "TLS 1.3 has been negotiated",
  529. * which is never true when generating the ClientHello.
  530. * However, version negotiation *has* occurred by the time the
  531. * ClientHello extensions are being parsed.
  532. * Be careful to allow TLS 1.3-only extensions when generating
  533. * the ClientHello.
  534. */
  535. || (is_tls13 && (extctx & SSL_EXT_TLS1_2_AND_BELOW_ONLY) != 0)
  536. || (!is_tls13 && (extctx & SSL_EXT_TLS1_3_ONLY) != 0
  537. && (thisctx & SSL_EXT_CLIENT_HELLO) == 0)
  538. || (s->server && !is_tls13 && (extctx & SSL_EXT_TLS1_3_ONLY) != 0)
  539. || (s->hit && (extctx & SSL_EXT_IGNORE_ON_RESUMPTION) != 0))
  540. return 0;
  541. return 1;
  542. }
  543. /*
  544. * Gather a list of all the extensions from the data in |packet]. |context|
  545. * tells us which message this extension is for. The raw extension data is
  546. * stored in |*res| on success. We don't actually process the content of the
  547. * extensions yet, except to check their types. This function also runs the
  548. * initialiser functions for all known extensions if |init| is nonzero (whether
  549. * we have collected them or not). If successful the caller is responsible for
  550. * freeing the contents of |*res|.
  551. *
  552. * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
  553. * more than one extension of the same type in a ClientHello or ServerHello.
  554. * This function returns 1 if all extensions are unique and we have parsed their
  555. * types, and 0 if the extensions contain duplicates, could not be successfully
  556. * found, or an internal error occurred. We only check duplicates for
  557. * extensions that we know about. We ignore others.
  558. */
  559. int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context,
  560. RAW_EXTENSION **res, size_t *len, int init)
  561. {
  562. PACKET extensions = *packet;
  563. size_t i = 0;
  564. size_t num_exts;
  565. custom_ext_methods *exts = &s->cert->custext;
  566. RAW_EXTENSION *raw_extensions = NULL;
  567. const EXTENSION_DEFINITION *thisexd;
  568. *res = NULL;
  569. /*
  570. * Initialise server side custom extensions. Client side is done during
  571. * construction of extensions for the ClientHello.
  572. */
  573. if ((context & SSL_EXT_CLIENT_HELLO) != 0)
  574. custom_ext_init(&s->cert->custext);
  575. num_exts = OSSL_NELEM(ext_defs) + (exts != NULL ? exts->meths_count : 0);
  576. raw_extensions = OPENSSL_zalloc(num_exts * sizeof(*raw_extensions));
  577. if (raw_extensions == NULL) {
  578. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_COLLECT_EXTENSIONS,
  579. ERR_R_MALLOC_FAILURE);
  580. return 0;
  581. }
  582. i = 0;
  583. while (PACKET_remaining(&extensions) > 0) {
  584. unsigned int type, idx;
  585. PACKET extension;
  586. RAW_EXTENSION *thisex;
  587. if (!PACKET_get_net_2(&extensions, &type) ||
  588. !PACKET_get_length_prefixed_2(&extensions, &extension)) {
  589. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_COLLECT_EXTENSIONS,
  590. SSL_R_BAD_EXTENSION);
  591. goto err;
  592. }
  593. /*
  594. * Verify this extension is allowed. We only check duplicates for
  595. * extensions that we recognise. We also have a special case for the
  596. * PSK extension, which must be the last one in the ClientHello.
  597. */
  598. if (!verify_extension(s, context, type, exts, raw_extensions, &thisex)
  599. || (thisex != NULL && thisex->present == 1)
  600. || (type == TLSEXT_TYPE_psk
  601. && (context & SSL_EXT_CLIENT_HELLO) != 0
  602. && PACKET_remaining(&extensions) != 0)) {
  603. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_COLLECT_EXTENSIONS,
  604. SSL_R_BAD_EXTENSION);
  605. goto err;
  606. }
  607. idx = thisex - raw_extensions;
  608. /*-
  609. * Check that we requested this extension (if appropriate). Requests can
  610. * be sent in the ClientHello and CertificateRequest. Unsolicited
  611. * extensions can be sent in the NewSessionTicket. We only do this for
  612. * the built-in extensions. Custom extensions have a different but
  613. * similar check elsewhere.
  614. * Special cases:
  615. * - The HRR cookie extension is unsolicited
  616. * - The renegotiate extension is unsolicited (the client signals
  617. * support via an SCSV)
  618. * - The signed_certificate_timestamp extension can be provided by a
  619. * custom extension or by the built-in version. We let the extension
  620. * itself handle unsolicited response checks.
  621. */
  622. if (idx < OSSL_NELEM(ext_defs)
  623. && (context & (SSL_EXT_CLIENT_HELLO
  624. | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
  625. | SSL_EXT_TLS1_3_NEW_SESSION_TICKET)) == 0
  626. && type != TLSEXT_TYPE_cookie
  627. && type != TLSEXT_TYPE_renegotiate
  628. && type != TLSEXT_TYPE_signed_certificate_timestamp
  629. && (s->ext.extflags[idx] & SSL_EXT_FLAG_SENT) == 0
  630. #ifndef OPENSSL_NO_GOST
  631. && !((context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0
  632. && type == TLSEXT_TYPE_cryptopro_bug)
  633. #endif
  634. ) {
  635. SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION,
  636. SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_UNSOLICITED_EXTENSION);
  637. goto err;
  638. }
  639. if (thisex != NULL) {
  640. thisex->data = extension;
  641. thisex->present = 1;
  642. thisex->type = type;
  643. thisex->received_order = i++;
  644. if (s->ext.debug_cb)
  645. s->ext.debug_cb(s, !s->server, thisex->type,
  646. PACKET_data(&thisex->data),
  647. PACKET_remaining(&thisex->data),
  648. s->ext.debug_arg);
  649. }
  650. }
  651. if (init) {
  652. /*
  653. * Initialise all known extensions relevant to this context,
  654. * whether we have found them or not
  655. */
  656. for (thisexd = ext_defs, i = 0; i < OSSL_NELEM(ext_defs);
  657. i++, thisexd++) {
  658. if (thisexd->init != NULL && (thisexd->context & context) != 0
  659. && extension_is_relevant(s, thisexd->context, context)
  660. && !thisexd->init(s, context)) {
  661. /* SSLfatal() already called */
  662. goto err;
  663. }
  664. }
  665. }
  666. *res = raw_extensions;
  667. if (len != NULL)
  668. *len = num_exts;
  669. return 1;
  670. err:
  671. OPENSSL_free(raw_extensions);
  672. return 0;
  673. }
  674. /*
  675. * Runs the parser for a given extension with index |idx|. |exts| contains the
  676. * list of all parsed extensions previously collected by
  677. * tls_collect_extensions(). The parser is only run if it is applicable for the
  678. * given |context| and the parser has not already been run. If this is for a
  679. * Certificate message, then we also provide the parser with the relevant
  680. * Certificate |x| and its position in the |chainidx| with 0 being the first
  681. * Certificate. Returns 1 on success or 0 on failure. If an extension is not
  682. * present this counted as success.
  683. */
  684. int tls_parse_extension(SSL *s, TLSEXT_INDEX idx, int context,
  685. RAW_EXTENSION *exts, X509 *x, size_t chainidx)
  686. {
  687. RAW_EXTENSION *currext = &exts[idx];
  688. int (*parser)(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  689. size_t chainidx) = NULL;
  690. /* Skip if the extension is not present */
  691. if (!currext->present)
  692. return 1;
  693. /* Skip if we've already parsed this extension */
  694. if (currext->parsed)
  695. return 1;
  696. currext->parsed = 1;
  697. if (idx < OSSL_NELEM(ext_defs)) {
  698. /* We are handling a built-in extension */
  699. const EXTENSION_DEFINITION *extdef = &ext_defs[idx];
  700. /* Check if extension is defined for our protocol. If not, skip */
  701. if (!extension_is_relevant(s, extdef->context, context))
  702. return 1;
  703. parser = s->server ? extdef->parse_ctos : extdef->parse_stoc;
  704. if (parser != NULL)
  705. return parser(s, &currext->data, context, x, chainidx);
  706. /*
  707. * If the parser is NULL we fall through to the custom extension
  708. * processing
  709. */
  710. }
  711. /* Parse custom extensions */
  712. return custom_ext_parse(s, context, currext->type,
  713. PACKET_data(&currext->data),
  714. PACKET_remaining(&currext->data),
  715. x, chainidx);
  716. }
  717. /*
  718. * Parse all remaining extensions that have not yet been parsed. Also calls the
  719. * finalisation for all extensions at the end if |fin| is nonzero, whether we
  720. * collected them or not. Returns 1 for success or 0 for failure. If we are
  721. * working on a Certificate message then we also pass the Certificate |x| and
  722. * its position in the |chainidx|, with 0 being the first certificate.
  723. */
  724. int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts, X509 *x,
  725. size_t chainidx, int fin)
  726. {
  727. size_t i, numexts = OSSL_NELEM(ext_defs);
  728. const EXTENSION_DEFINITION *thisexd;
  729. /* Calculate the number of extensions in the extensions list */
  730. numexts += s->cert->custext.meths_count;
  731. /* Parse each extension in turn */
  732. for (i = 0; i < numexts; i++) {
  733. if (!tls_parse_extension(s, i, context, exts, x, chainidx)) {
  734. /* SSLfatal() already called */
  735. return 0;
  736. }
  737. }
  738. if (fin) {
  739. /*
  740. * Finalise all known extensions relevant to this context,
  741. * whether we have found them or not
  742. */
  743. for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs);
  744. i++, thisexd++) {
  745. if (thisexd->final != NULL && (thisexd->context & context) != 0
  746. && !thisexd->final(s, context, exts[i].present)) {
  747. /* SSLfatal() already called */
  748. return 0;
  749. }
  750. }
  751. }
  752. return 1;
  753. }
  754. int should_add_extension(SSL *s, unsigned int extctx, unsigned int thisctx,
  755. int max_version)
  756. {
  757. /* Skip if not relevant for our context */
  758. if ((extctx & thisctx) == 0)
  759. return 0;
  760. /* Check if this extension is defined for our protocol. If not, skip */
  761. if (!extension_is_relevant(s, extctx, thisctx)
  762. || ((extctx & SSL_EXT_TLS1_3_ONLY) != 0
  763. && (thisctx & SSL_EXT_CLIENT_HELLO) != 0
  764. && (SSL_IS_DTLS(s) || max_version < TLS1_3_VERSION)))
  765. return 0;
  766. return 1;
  767. }
  768. /*
  769. * Construct all the extensions relevant to the current |context| and write
  770. * them to |pkt|. If this is an extension for a Certificate in a Certificate
  771. * message, then |x| will be set to the Certificate we are handling, and
  772. * |chainidx| will indicate the position in the chainidx we are processing (with
  773. * 0 being the first in the chain). Returns 1 on success or 0 on failure. On a
  774. * failure construction stops at the first extension to fail to construct.
  775. */
  776. int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context,
  777. X509 *x, size_t chainidx)
  778. {
  779. size_t i;
  780. int min_version, max_version = 0, reason;
  781. const EXTENSION_DEFINITION *thisexd;
  782. if (!WPACKET_start_sub_packet_u16(pkt)
  783. /*
  784. * If extensions are of zero length then we don't even add the
  785. * extensions length bytes to a ClientHello/ServerHello
  786. * (for non-TLSv1.3).
  787. */
  788. || ((context &
  789. (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO)) != 0
  790. && !WPACKET_set_flags(pkt,
  791. WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))) {
  792. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_EXTENSIONS,
  793. ERR_R_INTERNAL_ERROR);
  794. return 0;
  795. }
  796. if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
  797. reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);
  798. if (reason != 0) {
  799. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_EXTENSIONS,
  800. reason);
  801. return 0;
  802. }
  803. }
  804. /* Add custom extensions first */
  805. if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
  806. /* On the server side with initialise during ClientHello parsing */
  807. custom_ext_init(&s->cert->custext);
  808. }
  809. if (!custom_ext_add(s, context, pkt, x, chainidx, max_version)) {
  810. /* SSLfatal() already called */
  811. return 0;
  812. }
  813. for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs); i++, thisexd++) {
  814. EXT_RETURN (*construct)(SSL *s, WPACKET *pkt, unsigned int context,
  815. X509 *x, size_t chainidx);
  816. EXT_RETURN ret;
  817. /* Skip if not relevant for our context */
  818. if (!should_add_extension(s, thisexd->context, context, max_version))
  819. continue;
  820. construct = s->server ? thisexd->construct_stoc
  821. : thisexd->construct_ctos;
  822. if (construct == NULL)
  823. continue;
  824. ret = construct(s, pkt, context, x, chainidx);
  825. if (ret == EXT_RETURN_FAIL) {
  826. /* SSLfatal() already called */
  827. return 0;
  828. }
  829. if (ret == EXT_RETURN_SENT
  830. && (context & (SSL_EXT_CLIENT_HELLO
  831. | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
  832. | SSL_EXT_TLS1_3_NEW_SESSION_TICKET)) != 0)
  833. s->ext.extflags[i] |= SSL_EXT_FLAG_SENT;
  834. }
  835. if (!WPACKET_close(pkt)) {
  836. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_EXTENSIONS,
  837. ERR_R_INTERNAL_ERROR);
  838. return 0;
  839. }
  840. return 1;
  841. }
  842. /*
  843. * Built in extension finalisation and initialisation functions. All initialise
  844. * or finalise the associated extension type for the given |context|. For
  845. * finalisers |sent| is set to 1 if we saw the extension during parsing, and 0
  846. * otherwise. These functions return 1 on success or 0 on failure.
  847. */
  848. static int final_renegotiate(SSL *s, unsigned int context, int sent)
  849. {
  850. if (!s->server) {
  851. /*
  852. * Check if we can connect to a server that doesn't support safe
  853. * renegotiation
  854. */
  855. if (!(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
  856. && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
  857. && !sent) {
  858. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_FINAL_RENEGOTIATE,
  859. SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  860. return 0;
  861. }
  862. return 1;
  863. }
  864. /* Need RI if renegotiating */
  865. if (s->renegotiate
  866. && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
  867. && !sent) {
  868. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_FINAL_RENEGOTIATE,
  869. SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  870. return 0;
  871. }
  872. return 1;
  873. }
  874. static int init_server_name(SSL *s, unsigned int context)
  875. {
  876. if (s->server) {
  877. s->servername_done = 0;
  878. OPENSSL_free(s->ext.hostname);
  879. s->ext.hostname = NULL;
  880. }
  881. return 1;
  882. }
  883. static int final_server_name(SSL *s, unsigned int context, int sent)
  884. {
  885. int ret = SSL_TLSEXT_ERR_NOACK;
  886. int altmp = SSL_AD_UNRECOGNIZED_NAME;
  887. int was_ticket = (SSL_get_options(s) & SSL_OP_NO_TICKET) == 0;
  888. if (!ossl_assert(s->ctx != NULL) || !ossl_assert(s->session_ctx != NULL)) {
  889. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_FINAL_SERVER_NAME,
  890. ERR_R_INTERNAL_ERROR);
  891. return 0;
  892. }
  893. if (s->ctx->ext.servername_cb != NULL)
  894. ret = s->ctx->ext.servername_cb(s, &altmp,
  895. s->ctx->ext.servername_arg);
  896. else if (s->session_ctx->ext.servername_cb != NULL)
  897. ret = s->session_ctx->ext.servername_cb(s, &altmp,
  898. s->session_ctx->ext.servername_arg);
  899. /*
  900. * For servers, propagate the SNI hostname from the temporary
  901. * storage in the SSL to the persistent SSL_SESSION, now that we
  902. * know we accepted it.
  903. * Clients make this copy when parsing the server's response to
  904. * the extension, which is when they find out that the negotiation
  905. * was successful.
  906. */
  907. if (s->server) {
  908. if (sent && ret == SSL_TLSEXT_ERR_OK && !s->hit) {
  909. /* Only store the hostname in the session if we accepted it. */
  910. OPENSSL_free(s->session->ext.hostname);
  911. s->session->ext.hostname = OPENSSL_strdup(s->ext.hostname);
  912. if (s->session->ext.hostname == NULL && s->ext.hostname != NULL) {
  913. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_FINAL_SERVER_NAME,
  914. ERR_R_INTERNAL_ERROR);
  915. }
  916. }
  917. }
  918. /*
  919. * If we switched contexts (whether here or in the client_hello callback),
  920. * move the sess_accept increment from the session_ctx to the new
  921. * context, to avoid the confusing situation of having sess_accept_good
  922. * exceed sess_accept (zero) for the new context.
  923. */
  924. if (SSL_IS_FIRST_HANDSHAKE(s) && s->ctx != s->session_ctx
  925. && s->hello_retry_request == SSL_HRR_NONE) {
  926. tsan_counter(&s->ctx->stats.sess_accept);
  927. tsan_decr(&s->session_ctx->stats.sess_accept);
  928. }
  929. /*
  930. * If we're expecting to send a ticket, and tickets were previously enabled,
  931. * and now tickets are disabled, then turn off expected ticket.
  932. * Also, if this is not a resumption, create a new session ID
  933. */
  934. if (ret == SSL_TLSEXT_ERR_OK && s->ext.ticket_expected
  935. && was_ticket && (SSL_get_options(s) & SSL_OP_NO_TICKET) != 0) {
  936. s->ext.ticket_expected = 0;
  937. if (!s->hit) {
  938. SSL_SESSION* ss = SSL_get_session(s);
  939. if (ss != NULL) {
  940. OPENSSL_free(ss->ext.tick);
  941. ss->ext.tick = NULL;
  942. ss->ext.ticklen = 0;
  943. ss->ext.tick_lifetime_hint = 0;
  944. ss->ext.tick_age_add = 0;
  945. if (!ssl_generate_session_id(s, ss)) {
  946. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_FINAL_SERVER_NAME,
  947. ERR_R_INTERNAL_ERROR);
  948. return 0;
  949. }
  950. } else {
  951. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_FINAL_SERVER_NAME,
  952. ERR_R_INTERNAL_ERROR);
  953. return 0;
  954. }
  955. }
  956. }
  957. switch (ret) {
  958. case SSL_TLSEXT_ERR_ALERT_FATAL:
  959. SSLfatal(s, altmp, SSL_F_FINAL_SERVER_NAME, SSL_R_CALLBACK_FAILED);
  960. return 0;
  961. case SSL_TLSEXT_ERR_ALERT_WARNING:
  962. /* TLSv1.3 doesn't have warning alerts so we suppress this */
  963. if (!SSL_IS_TLS13(s))
  964. ssl3_send_alert(s, SSL3_AL_WARNING, altmp);
  965. s->servername_done = 0;
  966. return 1;
  967. case SSL_TLSEXT_ERR_NOACK:
  968. s->servername_done = 0;
  969. return 1;
  970. default:
  971. return 1;
  972. }
  973. }
  974. #ifndef OPENSSL_NO_EC
  975. static int init_ec_point_formats(SSL *s, unsigned int context)
  976. {
  977. OPENSSL_free(s->ext.peer_ecpointformats);
  978. s->ext.peer_ecpointformats = NULL;
  979. s->ext.peer_ecpointformats_len = 0;
  980. return 1;
  981. }
  982. static int final_ec_pt_formats(SSL *s, unsigned int context, int sent)
  983. {
  984. unsigned long alg_k, alg_a;
  985. if (s->server)
  986. return 1;
  987. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  988. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  989. /*
  990. * If we are client and using an elliptic curve cryptography cipher
  991. * suite, then if server returns an EC point formats lists extension it
  992. * must contain uncompressed.
  993. */
  994. if (s->ext.ecpointformats != NULL
  995. && s->ext.ecpointformats_len > 0
  996. && s->ext.peer_ecpointformats != NULL
  997. && s->ext.peer_ecpointformats_len > 0
  998. && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
  999. /* we are using an ECC cipher */
  1000. size_t i;
  1001. unsigned char *list = s->ext.peer_ecpointformats;
  1002. for (i = 0; i < s->ext.peer_ecpointformats_len; i++) {
  1003. if (*list++ == TLSEXT_ECPOINTFORMAT_uncompressed)
  1004. break;
  1005. }
  1006. if (i == s->ext.peer_ecpointformats_len) {
  1007. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_FINAL_EC_PT_FORMATS,
  1008. SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
  1009. return 0;
  1010. }
  1011. }
  1012. return 1;
  1013. }
  1014. #endif
  1015. static int init_session_ticket(SSL *s, unsigned int context)
  1016. {
  1017. if (!s->server)
  1018. s->ext.ticket_expected = 0;
  1019. return 1;
  1020. }
  1021. #ifndef OPENSSL_NO_OCSP
  1022. static int init_status_request(SSL *s, unsigned int context)
  1023. {
  1024. if (s->server) {
  1025. s->ext.status_type = TLSEXT_STATUSTYPE_nothing;
  1026. } else {
  1027. /*
  1028. * Ensure we get sensible values passed to tlsext_status_cb in the event
  1029. * that we don't receive a status message
  1030. */
  1031. OPENSSL_free(s->ext.ocsp.resp);
  1032. s->ext.ocsp.resp = NULL;
  1033. s->ext.ocsp.resp_len = 0;
  1034. }
  1035. return 1;
  1036. }
  1037. #endif
  1038. #ifndef OPENSSL_NO_NEXTPROTONEG
  1039. static int init_npn(SSL *s, unsigned int context)
  1040. {
  1041. s->s3->npn_seen = 0;
  1042. return 1;
  1043. }
  1044. #endif
  1045. static int init_alpn(SSL *s, unsigned int context)
  1046. {
  1047. OPENSSL_free(s->s3->alpn_selected);
  1048. s->s3->alpn_selected = NULL;
  1049. s->s3->alpn_selected_len = 0;
  1050. if (s->server) {
  1051. OPENSSL_free(s->s3->alpn_proposed);
  1052. s->s3->alpn_proposed = NULL;
  1053. s->s3->alpn_proposed_len = 0;
  1054. }
  1055. return 1;
  1056. }
  1057. static int final_alpn(SSL *s, unsigned int context, int sent)
  1058. {
  1059. if (!s->server && !sent && s->session->ext.alpn_selected != NULL)
  1060. s->ext.early_data_ok = 0;
  1061. if (!s->server || !SSL_IS_TLS13(s))
  1062. return 1;
  1063. /*
  1064. * Call alpn_select callback if needed. Has to be done after SNI and
  1065. * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
  1066. * we also have to do this before we decide whether to accept early_data.
  1067. * In TLSv1.3 we've already negotiated our cipher so we do this call now.
  1068. * For < TLSv1.3 we defer it until after cipher negotiation.
  1069. *
  1070. * On failure SSLfatal() already called.
  1071. */
  1072. return tls_handle_alpn(s);
  1073. }
  1074. static int init_sig_algs(SSL *s, unsigned int context)
  1075. {
  1076. /* Clear any signature algorithms extension received */
  1077. OPENSSL_free(s->s3->tmp.peer_sigalgs);
  1078. s->s3->tmp.peer_sigalgs = NULL;
  1079. s->s3->tmp.peer_sigalgslen = 0;
  1080. return 1;
  1081. }
  1082. static int init_sig_algs_cert(SSL *s, unsigned int context)
  1083. {
  1084. /* Clear any signature algorithms extension received */
  1085. OPENSSL_free(s->s3->tmp.peer_cert_sigalgs);
  1086. s->s3->tmp.peer_cert_sigalgs = NULL;
  1087. s->s3->tmp.peer_cert_sigalgslen = 0;
  1088. return 1;
  1089. }
  1090. #ifndef OPENSSL_NO_SRP
  1091. static int init_srp(SSL *s, unsigned int context)
  1092. {
  1093. OPENSSL_free(s->srp_ctx.login);
  1094. s->srp_ctx.login = NULL;
  1095. return 1;
  1096. }
  1097. #endif
  1098. static int init_etm(SSL *s, unsigned int context)
  1099. {
  1100. s->ext.use_etm = 0;
  1101. return 1;
  1102. }
  1103. static int init_ems(SSL *s, unsigned int context)
  1104. {
  1105. if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) {
  1106. s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
  1107. s->s3->flags |= TLS1_FLAGS_REQUIRED_EXTMS;
  1108. }
  1109. return 1;
  1110. }
  1111. static int final_ems(SSL *s, unsigned int context, int sent)
  1112. {
  1113. /*
  1114. * Check extended master secret extension is not dropped on
  1115. * renegotiation.
  1116. */
  1117. if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS)
  1118. && (s->s3->flags & TLS1_FLAGS_REQUIRED_EXTMS)) {
  1119. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_FINAL_EMS,
  1120. SSL_R_INCONSISTENT_EXTMS);
  1121. return 0;
  1122. }
  1123. if (!s->server && s->hit) {
  1124. /*
  1125. * Check extended master secret extension is consistent with
  1126. * original session.
  1127. */
  1128. if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
  1129. !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
  1130. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_FINAL_EMS,
  1131. SSL_R_INCONSISTENT_EXTMS);
  1132. return 0;
  1133. }
  1134. }
  1135. return 1;
  1136. }
  1137. static int init_certificate_authorities(SSL *s, unsigned int context)
  1138. {
  1139. sk_X509_NAME_pop_free(s->s3->tmp.peer_ca_names, X509_NAME_free);
  1140. s->s3->tmp.peer_ca_names = NULL;
  1141. return 1;
  1142. }
  1143. static EXT_RETURN tls_construct_certificate_authorities(SSL *s, WPACKET *pkt,
  1144. unsigned int context,
  1145. X509 *x,
  1146. size_t chainidx)
  1147. {
  1148. const STACK_OF(X509_NAME) *ca_sk = get_ca_names(s);
  1149. if (ca_sk == NULL || sk_X509_NAME_num(ca_sk) == 0)
  1150. return EXT_RETURN_NOT_SENT;
  1151. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_certificate_authorities)
  1152. || !WPACKET_start_sub_packet_u16(pkt)) {
  1153. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1154. SSL_F_TLS_CONSTRUCT_CERTIFICATE_AUTHORITIES,
  1155. ERR_R_INTERNAL_ERROR);
  1156. return EXT_RETURN_FAIL;
  1157. }
  1158. if (!construct_ca_names(s, ca_sk, pkt)) {
  1159. /* SSLfatal() already called */
  1160. return EXT_RETURN_FAIL;
  1161. }
  1162. if (!WPACKET_close(pkt)) {
  1163. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1164. SSL_F_TLS_CONSTRUCT_CERTIFICATE_AUTHORITIES,
  1165. ERR_R_INTERNAL_ERROR);
  1166. return EXT_RETURN_FAIL;
  1167. }
  1168. return EXT_RETURN_SENT;
  1169. }
  1170. static int tls_parse_certificate_authorities(SSL *s, PACKET *pkt,
  1171. unsigned int context, X509 *x,
  1172. size_t chainidx)
  1173. {
  1174. if (!parse_ca_names(s, pkt))
  1175. return 0;
  1176. if (PACKET_remaining(pkt) != 0) {
  1177. SSLfatal(s, SSL_AD_DECODE_ERROR,
  1178. SSL_F_TLS_PARSE_CERTIFICATE_AUTHORITIES, SSL_R_BAD_EXTENSION);
  1179. return 0;
  1180. }
  1181. return 1;
  1182. }
  1183. #ifndef OPENSSL_NO_SRTP
  1184. static int init_srtp(SSL *s, unsigned int context)
  1185. {
  1186. if (s->server)
  1187. s->srtp_profile = NULL;
  1188. return 1;
  1189. }
  1190. #endif
  1191. static int final_sig_algs(SSL *s, unsigned int context, int sent)
  1192. {
  1193. if (!sent && SSL_IS_TLS13(s) && !s->hit) {
  1194. SSLfatal(s, TLS13_AD_MISSING_EXTENSION, SSL_F_FINAL_SIG_ALGS,
  1195. SSL_R_MISSING_SIGALGS_EXTENSION);
  1196. return 0;
  1197. }
  1198. return 1;
  1199. }
  1200. #ifndef OPENSSL_NO_EC
  1201. static int final_key_share(SSL *s, unsigned int context, int sent)
  1202. {
  1203. if (!SSL_IS_TLS13(s))
  1204. return 1;
  1205. /* Nothing to do for key_share in an HRR */
  1206. if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0)
  1207. return 1;
  1208. /*
  1209. * If
  1210. * we are a client
  1211. * AND
  1212. * we have no key_share
  1213. * AND
  1214. * (we are not resuming
  1215. * OR the kex_mode doesn't allow non key_share resumes)
  1216. * THEN
  1217. * fail;
  1218. */
  1219. if (!s->server
  1220. && !sent
  1221. && (!s->hit
  1222. || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0)) {
  1223. /* Nothing left we can do - just fail */
  1224. SSLfatal(s, SSL_AD_MISSING_EXTENSION, SSL_F_FINAL_KEY_SHARE,
  1225. SSL_R_NO_SUITABLE_KEY_SHARE);
  1226. return 0;
  1227. }
  1228. /*
  1229. * IF
  1230. * we are a server
  1231. * THEN
  1232. * IF
  1233. * we have a suitable key_share
  1234. * THEN
  1235. * IF
  1236. * we are stateless AND we have no cookie
  1237. * THEN
  1238. * send a HelloRetryRequest
  1239. * ELSE
  1240. * IF
  1241. * we didn't already send a HelloRetryRequest
  1242. * AND
  1243. * the client sent a key_share extension
  1244. * AND
  1245. * (we are not resuming
  1246. * OR the kex_mode allows key_share resumes)
  1247. * AND
  1248. * a shared group exists
  1249. * THEN
  1250. * send a HelloRetryRequest
  1251. * ELSE IF
  1252. * we are not resuming
  1253. * OR
  1254. * the kex_mode doesn't allow non key_share resumes
  1255. * THEN
  1256. * fail
  1257. * ELSE IF
  1258. * we are stateless AND we have no cookie
  1259. * THEN
  1260. * send a HelloRetryRequest
  1261. */
  1262. if (s->server) {
  1263. if (s->s3->peer_tmp != NULL) {
  1264. /* We have a suitable key_share */
  1265. if ((s->s3->flags & TLS1_FLAGS_STATELESS) != 0
  1266. && !s->ext.cookieok) {
  1267. if (!ossl_assert(s->hello_retry_request == SSL_HRR_NONE)) {
  1268. /*
  1269. * If we are stateless then we wouldn't know about any
  1270. * previously sent HRR - so how can this be anything other
  1271. * than 0?
  1272. */
  1273. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_FINAL_KEY_SHARE,
  1274. ERR_R_INTERNAL_ERROR);
  1275. return 0;
  1276. }
  1277. s->hello_retry_request = SSL_HRR_PENDING;
  1278. return 1;
  1279. }
  1280. } else {
  1281. /* No suitable key_share */
  1282. if (s->hello_retry_request == SSL_HRR_NONE && sent
  1283. && (!s->hit
  1284. || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE)
  1285. != 0)) {
  1286. const uint16_t *pgroups, *clntgroups;
  1287. size_t num_groups, clnt_num_groups, i;
  1288. unsigned int group_id = 0;
  1289. /* Check if a shared group exists */
  1290. /* Get the clients list of supported groups. */
  1291. tls1_get_peer_groups(s, &clntgroups, &clnt_num_groups);
  1292. tls1_get_supported_groups(s, &pgroups, &num_groups);
  1293. /*
  1294. * Find the first group we allow that is also in client's list
  1295. */
  1296. for (i = 0; i < num_groups; i++) {
  1297. group_id = pgroups[i];
  1298. if (check_in_list(s, group_id, clntgroups, clnt_num_groups,
  1299. 1))
  1300. break;
  1301. }
  1302. if (i < num_groups) {
  1303. /* A shared group exists so send a HelloRetryRequest */
  1304. s->s3->group_id = group_id;
  1305. s->hello_retry_request = SSL_HRR_PENDING;
  1306. return 1;
  1307. }
  1308. }
  1309. if (!s->hit
  1310. || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0) {
  1311. /* Nothing left we can do - just fail */
  1312. SSLfatal(s, sent ? SSL_AD_HANDSHAKE_FAILURE
  1313. : SSL_AD_MISSING_EXTENSION,
  1314. SSL_F_FINAL_KEY_SHARE, SSL_R_NO_SUITABLE_KEY_SHARE);
  1315. return 0;
  1316. }
  1317. if ((s->s3->flags & TLS1_FLAGS_STATELESS) != 0
  1318. && !s->ext.cookieok) {
  1319. if (!ossl_assert(s->hello_retry_request == SSL_HRR_NONE)) {
  1320. /*
  1321. * If we are stateless then we wouldn't know about any
  1322. * previously sent HRR - so how can this be anything other
  1323. * than 0?
  1324. */
  1325. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_FINAL_KEY_SHARE,
  1326. ERR_R_INTERNAL_ERROR);
  1327. return 0;
  1328. }
  1329. s->hello_retry_request = SSL_HRR_PENDING;
  1330. return 1;
  1331. }
  1332. }
  1333. /*
  1334. * We have a key_share so don't send any more HelloRetryRequest
  1335. * messages
  1336. */
  1337. if (s->hello_retry_request == SSL_HRR_PENDING)
  1338. s->hello_retry_request = SSL_HRR_COMPLETE;
  1339. } else {
  1340. /*
  1341. * For a client side resumption with no key_share we need to generate
  1342. * the handshake secret (otherwise this is done during key_share
  1343. * processing).
  1344. */
  1345. if (!sent && !tls13_generate_handshake_secret(s, NULL, 0)) {
  1346. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_FINAL_KEY_SHARE,
  1347. ERR_R_INTERNAL_ERROR);
  1348. return 0;
  1349. }
  1350. }
  1351. return 1;
  1352. }
  1353. #endif
  1354. static int init_psk_kex_modes(SSL *s, unsigned int context)
  1355. {
  1356. s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_NONE;
  1357. return 1;
  1358. }
  1359. int tls_psk_do_binder(SSL *s, const EVP_MD *md, const unsigned char *msgstart,
  1360. size_t binderoffset, const unsigned char *binderin,
  1361. unsigned char *binderout, SSL_SESSION *sess, int sign,
  1362. int external)
  1363. {
  1364. EVP_PKEY *mackey = NULL;
  1365. EVP_MD_CTX *mctx = NULL;
  1366. unsigned char hash[EVP_MAX_MD_SIZE], binderkey[EVP_MAX_MD_SIZE];
  1367. unsigned char finishedkey[EVP_MAX_MD_SIZE], tmpbinder[EVP_MAX_MD_SIZE];
  1368. unsigned char *early_secret;
  1369. #ifdef CHARSET_EBCDIC
  1370. static const unsigned char resumption_label[] = { 0x72, 0x65, 0x73, 0x20, 0x62, 0x69, 0x6E, 0x64, 0x65, 0x72, 0x00 };
  1371. static const unsigned char external_label[] = { 0x65, 0x78, 0x74, 0x20, 0x62, 0x69, 0x6E, 0x64, 0x65, 0x72, 0x00 };
  1372. #else
  1373. static const unsigned char resumption_label[] = "res binder";
  1374. static const unsigned char external_label[] = "ext binder";
  1375. #endif
  1376. const unsigned char *label;
  1377. size_t bindersize, labelsize, hashsize;
  1378. int hashsizei = EVP_MD_size(md);
  1379. int ret = -1;
  1380. int usepskfored = 0;
  1381. /* Ensure cast to size_t is safe */
  1382. if (!ossl_assert(hashsizei >= 0)) {
  1383. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PSK_DO_BINDER,
  1384. ERR_R_INTERNAL_ERROR);
  1385. goto err;
  1386. }
  1387. hashsize = (size_t)hashsizei;
  1388. if (external
  1389. && s->early_data_state == SSL_EARLY_DATA_CONNECTING
  1390. && s->session->ext.max_early_data == 0
  1391. && sess->ext.max_early_data > 0)
  1392. usepskfored = 1;
  1393. if (external) {
  1394. label = external_label;
  1395. labelsize = sizeof(external_label) - 1;
  1396. } else {
  1397. label = resumption_label;
  1398. labelsize = sizeof(resumption_label) - 1;
  1399. }
  1400. /*
  1401. * Generate the early_secret. On the server side we've selected a PSK to
  1402. * resume with (internal or external) so we always do this. On the client
  1403. * side we do this for a non-external (i.e. resumption) PSK or external PSK
  1404. * that will be used for early_data so that it is in place for sending early
  1405. * data. For client side external PSK not being used for early_data we
  1406. * generate it but store it away for later use.
  1407. */
  1408. if (s->server || !external || usepskfored)
  1409. early_secret = (unsigned char *)s->early_secret;
  1410. else
  1411. early_secret = (unsigned char *)sess->early_secret;
  1412. if (!tls13_generate_secret(s, md, NULL, sess->master_key,
  1413. sess->master_key_length, early_secret)) {
  1414. /* SSLfatal() already called */
  1415. goto err;
  1416. }
  1417. /*
  1418. * Create the handshake hash for the binder key...the messages so far are
  1419. * empty!
  1420. */
  1421. mctx = EVP_MD_CTX_new();
  1422. if (mctx == NULL
  1423. || EVP_DigestInit_ex(mctx, md, NULL) <= 0
  1424. || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
  1425. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PSK_DO_BINDER,
  1426. ERR_R_INTERNAL_ERROR);
  1427. goto err;
  1428. }
  1429. /* Generate the binder key */
  1430. if (!tls13_hkdf_expand(s, md, early_secret, label, labelsize, hash,
  1431. hashsize, binderkey, hashsize, 1)) {
  1432. /* SSLfatal() already called */
  1433. goto err;
  1434. }
  1435. /* Generate the finished key */
  1436. if (!tls13_derive_finishedkey(s, md, binderkey, finishedkey, hashsize)) {
  1437. /* SSLfatal() already called */
  1438. goto err;
  1439. }
  1440. if (EVP_DigestInit_ex(mctx, md, NULL) <= 0) {
  1441. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PSK_DO_BINDER,
  1442. ERR_R_INTERNAL_ERROR);
  1443. goto err;
  1444. }
  1445. /*
  1446. * Get a hash of the ClientHello up to the start of the binders. If we are
  1447. * following a HelloRetryRequest then this includes the hash of the first
  1448. * ClientHello and the HelloRetryRequest itself.
  1449. */
  1450. if (s->hello_retry_request == SSL_HRR_PENDING) {
  1451. size_t hdatalen;
  1452. long hdatalen_l;
  1453. void *hdata;
  1454. hdatalen = hdatalen_l =
  1455. BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
  1456. if (hdatalen_l <= 0) {
  1457. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PSK_DO_BINDER,
  1458. SSL_R_BAD_HANDSHAKE_LENGTH);
  1459. goto err;
  1460. }
  1461. /*
  1462. * For servers the handshake buffer data will include the second
  1463. * ClientHello - which we don't want - so we need to take that bit off.
  1464. */
  1465. if (s->server) {
  1466. PACKET hashprefix, msg;
  1467. /* Find how many bytes are left after the first two messages */
  1468. if (!PACKET_buf_init(&hashprefix, hdata, hdatalen)
  1469. || !PACKET_forward(&hashprefix, 1)
  1470. || !PACKET_get_length_prefixed_3(&hashprefix, &msg)
  1471. || !PACKET_forward(&hashprefix, 1)
  1472. || !PACKET_get_length_prefixed_3(&hashprefix, &msg)) {
  1473. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PSK_DO_BINDER,
  1474. ERR_R_INTERNAL_ERROR);
  1475. goto err;
  1476. }
  1477. hdatalen -= PACKET_remaining(&hashprefix);
  1478. }
  1479. if (EVP_DigestUpdate(mctx, hdata, hdatalen) <= 0) {
  1480. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PSK_DO_BINDER,
  1481. ERR_R_INTERNAL_ERROR);
  1482. goto err;
  1483. }
  1484. }
  1485. if (EVP_DigestUpdate(mctx, msgstart, binderoffset) <= 0
  1486. || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
  1487. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PSK_DO_BINDER,
  1488. ERR_R_INTERNAL_ERROR);
  1489. goto err;
  1490. }
  1491. mackey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL, finishedkey,
  1492. hashsize);
  1493. if (mackey == NULL) {
  1494. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PSK_DO_BINDER,
  1495. ERR_R_INTERNAL_ERROR);
  1496. goto err;
  1497. }
  1498. if (!sign)
  1499. binderout = tmpbinder;
  1500. bindersize = hashsize;
  1501. if (EVP_DigestSignInit(mctx, NULL, md, NULL, mackey) <= 0
  1502. || EVP_DigestSignUpdate(mctx, hash, hashsize) <= 0
  1503. || EVP_DigestSignFinal(mctx, binderout, &bindersize) <= 0
  1504. || bindersize != hashsize) {
  1505. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PSK_DO_BINDER,
  1506. ERR_R_INTERNAL_ERROR);
  1507. goto err;
  1508. }
  1509. if (sign) {
  1510. ret = 1;
  1511. } else {
  1512. /* HMAC keys can't do EVP_DigestVerify* - use CRYPTO_memcmp instead */
  1513. ret = (CRYPTO_memcmp(binderin, binderout, hashsize) == 0);
  1514. if (!ret)
  1515. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PSK_DO_BINDER,
  1516. SSL_R_BINDER_DOES_NOT_VERIFY);
  1517. }
  1518. err:
  1519. OPENSSL_cleanse(binderkey, sizeof(binderkey));
  1520. OPENSSL_cleanse(finishedkey, sizeof(finishedkey));
  1521. EVP_PKEY_free(mackey);
  1522. EVP_MD_CTX_free(mctx);
  1523. return ret;
  1524. }
  1525. static int final_early_data(SSL *s, unsigned int context, int sent)
  1526. {
  1527. if (!sent)
  1528. return 1;
  1529. if (!s->server) {
  1530. if (context == SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
  1531. && sent
  1532. && !s->ext.early_data_ok) {
  1533. /*
  1534. * If we get here then the server accepted our early_data but we
  1535. * later realised that it shouldn't have done (e.g. inconsistent
  1536. * ALPN)
  1537. */
  1538. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_FINAL_EARLY_DATA,
  1539. SSL_R_BAD_EARLY_DATA);
  1540. return 0;
  1541. }
  1542. return 1;
  1543. }
  1544. if (s->max_early_data == 0
  1545. || !s->hit
  1546. || s->early_data_state != SSL_EARLY_DATA_ACCEPTING
  1547. || !s->ext.early_data_ok
  1548. || s->hello_retry_request != SSL_HRR_NONE
  1549. || (s->allow_early_data_cb != NULL
  1550. && !s->allow_early_data_cb(s,
  1551. s->allow_early_data_cb_data))) {
  1552. s->ext.early_data = SSL_EARLY_DATA_REJECTED;
  1553. } else {
  1554. s->ext.early_data = SSL_EARLY_DATA_ACCEPTED;
  1555. if (!tls13_change_cipher_state(s,
  1556. SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_SERVER_READ)) {
  1557. /* SSLfatal() already called */
  1558. return 0;
  1559. }
  1560. }
  1561. return 1;
  1562. }
  1563. static int final_maxfragmentlen(SSL *s, unsigned int context, int sent)
  1564. {
  1565. /*
  1566. * Session resumption on server-side with MFL extension active
  1567. * BUT MFL extension packet was not resent (i.e. sent == 0)
  1568. */
  1569. if (s->server && s->hit && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)
  1570. && !sent ) {
  1571. SSLfatal(s, SSL_AD_MISSING_EXTENSION, SSL_F_FINAL_MAXFRAGMENTLEN,
  1572. SSL_R_BAD_EXTENSION);
  1573. return 0;
  1574. }
  1575. /* Current SSL buffer is lower than requested MFL */
  1576. if (s->session && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)
  1577. && s->max_send_fragment < GET_MAX_FRAGMENT_LENGTH(s->session))
  1578. /* trigger a larger buffer reallocation */
  1579. if (!ssl3_setup_buffers(s)) {
  1580. /* SSLfatal() already called */
  1581. return 0;
  1582. }
  1583. return 1;
  1584. }
  1585. static int init_post_handshake_auth(SSL *s, unsigned int context)
  1586. {
  1587. s->post_handshake_auth = SSL_PHA_NONE;
  1588. return 1;
  1589. }
  1590. /*
  1591. * If clients offer "pre_shared_key" without a "psk_key_exchange_modes"
  1592. * extension, servers MUST abort the handshake.
  1593. */
  1594. static int final_psk(SSL *s, unsigned int context, int sent)
  1595. {
  1596. if (s->server && sent && s->clienthello != NULL
  1597. && !s->clienthello->pre_proc_exts[TLSEXT_IDX_psk_kex_modes].present) {
  1598. SSLfatal(s, TLS13_AD_MISSING_EXTENSION, SSL_F_FINAL_PSK,
  1599. SSL_R_MISSING_PSK_KEX_MODES_EXTENSION);
  1600. return 0;
  1601. }
  1602. return 1;
  1603. }
  1604. #ifndef OPENSSL_NO_QUIC
  1605. static int init_quic_transport_params(SSL *s, unsigned int context)
  1606. {
  1607. return 1;
  1608. }
  1609. static int final_quic_transport_params_draft(SSL *s, unsigned int context,
  1610. int sent)
  1611. {
  1612. return 1;
  1613. }
  1614. static int final_quic_transport_params(SSL *s, unsigned int context, int sent)
  1615. {
  1616. /* called after final_quic_transport_params_draft */
  1617. if (SSL_IS_QUIC(s)) {
  1618. if (s->ext.peer_quic_transport_params_len == 0
  1619. && s->ext.peer_quic_transport_params_draft_len == 0) {
  1620. SSLfatal(s, SSL_AD_MISSING_EXTENSION,
  1621. SSL_F_FINAL_QUIC_TRANSPORT_PARAMS,
  1622. SSL_R_MISSING_QUIC_TRANSPORT_PARAMETERS_EXTENSION);
  1623. return 0;
  1624. }
  1625. /* if we got both, discard the one we can't use */
  1626. if (s->ext.peer_quic_transport_params_len != 0
  1627. && s->ext.peer_quic_transport_params_draft_len != 0) {
  1628. if (s->quic_transport_version == TLSEXT_TYPE_quic_transport_parameters_draft) {
  1629. OPENSSL_free(s->ext.peer_quic_transport_params);
  1630. s->ext.peer_quic_transport_params = NULL;
  1631. s->ext.peer_quic_transport_params_len = 0;
  1632. } else {
  1633. OPENSSL_free(s->ext.peer_quic_transport_params_draft);
  1634. s->ext.peer_quic_transport_params_draft = NULL;
  1635. s->ext.peer_quic_transport_params_draft_len = 0;
  1636. }
  1637. }
  1638. }
  1639. return 1;
  1640. }
  1641. #endif