extensions.c 60 KB

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