extensions.c 58 KB

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