extensions.c 60 KB

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