extensions.c 59 KB

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