extensions.c 58 KB

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