extensions.c 68 KB

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