extensions.c 60 KB

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