extensions.c 60 KB

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