extensions.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. /*
  2. * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include "../ssl_locl.h"
  11. #include "statem_locl.h"
  12. static int final_renegotiate(SSL *s, unsigned int context, int sent,
  13. int *al);
  14. static int init_server_name(SSL *s, unsigned int context);
  15. static int final_server_name(SSL *s, unsigned int context, int sent,
  16. int *al);
  17. #ifndef OPENSSL_NO_EC
  18. static int final_ec_pt_formats(SSL *s, unsigned int context, int sent,
  19. int *al);
  20. #endif
  21. static int init_session_ticket(SSL *s, unsigned int context);
  22. #ifndef OPENSSL_NO_OCSP
  23. static int init_status_request(SSL *s, unsigned int context);
  24. #endif
  25. #ifndef OPENSSL_NO_NEXTPROTONEG
  26. static int init_npn(SSL *s, unsigned int context);
  27. #endif
  28. static int init_alpn(SSL *s, unsigned int context);
  29. static int final_alpn(SSL *s, unsigned int context, int sent, int *al);
  30. static int init_sig_algs(SSL *s, unsigned int context);
  31. static int init_certificate_authorities(SSL *s, unsigned int context);
  32. static int tls_construct_certificate_authorities(SSL *s, WPACKET *pkt,
  33. unsigned int context, X509 *x,
  34. size_t chainidx, int *al);
  35. static int tls_parse_certificate_authorities(SSL *s, PACKET *pkt,
  36. unsigned int context, X509 *x,
  37. size_t chainidx, int *al);
  38. #ifndef OPENSSL_NO_SRP
  39. static int init_srp(SSL *s, unsigned int context);
  40. #endif
  41. static int init_etm(SSL *s, unsigned int context);
  42. static int init_ems(SSL *s, unsigned int context);
  43. static int final_ems(SSL *s, unsigned int context, int sent, int *al);
  44. static int init_psk_kex_modes(SSL *s, unsigned int context);
  45. #ifndef OPENSSL_NO_EC
  46. static int final_key_share(SSL *s, unsigned int context, int sent, int *al);
  47. #endif
  48. #ifndef OPENSSL_NO_SRTP
  49. static int init_srtp(SSL *s, unsigned int context);
  50. #endif
  51. static int final_sig_algs(SSL *s, unsigned int context, int sent, int *al);
  52. static int final_early_data(SSL *s, unsigned int context, int sent, int *al);
  53. /* Structure to define a built-in extension */
  54. typedef struct extensions_definition_st {
  55. /* The defined type for the extension */
  56. unsigned int type;
  57. /*
  58. * The context that this extension applies to, e.g. what messages and
  59. * protocol versions
  60. */
  61. unsigned int context;
  62. /*
  63. * Initialise extension before parsing. Always called for relevant contexts
  64. * even if extension not present
  65. */
  66. int (*init)(SSL *s, unsigned int context);
  67. /* Parse extension sent from client to server */
  68. int (*parse_ctos)(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  69. size_t chainidx, int *al);
  70. /* Parse extension send from server to client */
  71. int (*parse_stoc)(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  72. size_t chainidx, int *al);
  73. /* Construct extension sent from server to client */
  74. int (*construct_stoc)(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  75. size_t chainidx, int *al);
  76. /* Construct extension sent from client to server */
  77. int (*construct_ctos)(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  78. size_t chainidx, int *al);
  79. /*
  80. * Finalise extension after parsing. Always called where an extensions was
  81. * initialised even if the extension was not present. |sent| is set to 1 if
  82. * the extension was seen, or 0 otherwise.
  83. */
  84. int (*final)(SSL *s, unsigned int context, int sent, int *al);
  85. } EXTENSION_DEFINITION;
  86. /*
  87. * Definitions of all built-in extensions. NOTE: Changes in the number or order
  88. * of these extensions should be mirrored with equivalent changes to the
  89. * indexes ( TLSEXT_IDX_* ) defined in ssl_locl.h.
  90. * Each extension has an initialiser, a client and
  91. * server side parser and a finaliser. The initialiser is called (if the
  92. * extension is relevant to the given context) even if we did not see the
  93. * extension in the message that we received. The parser functions are only
  94. * called if we see the extension in the message. The finalisers are always
  95. * called if the initialiser was called.
  96. * There are also server and client side constructor functions which are always
  97. * called during message construction if the extension is relevant for the
  98. * given context.
  99. * The initialisation, parsing, finalisation and construction functions are
  100. * always called in the order defined in this list. Some extensions may depend
  101. * on others having been processed first, so the order of this list is
  102. * significant.
  103. * The extension context is defined by a series of flags which specify which
  104. * messages the extension is relevant to. These flags also specify whether the
  105. * extension is relevant to a particular protocol or protocol version.
  106. *
  107. * TODO(TLS1.3): Make sure we have a test to check the consistency of these
  108. */
  109. #define INVALID_EXTENSION { 0x10000, 0, NULL, NULL, NULL, NULL, NULL, NULL }
  110. static const EXTENSION_DEFINITION ext_defs[] = {
  111. {
  112. TLSEXT_TYPE_renegotiate,
  113. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  114. | SSL_EXT_SSL3_ALLOWED | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  115. NULL, tls_parse_ctos_renegotiate, tls_parse_stoc_renegotiate,
  116. tls_construct_stoc_renegotiate, tls_construct_ctos_renegotiate,
  117. final_renegotiate
  118. },
  119. {
  120. TLSEXT_TYPE_server_name,
  121. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  122. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
  123. init_server_name,
  124. tls_parse_ctos_server_name, tls_parse_stoc_server_name,
  125. tls_construct_stoc_server_name, tls_construct_ctos_server_name,
  126. final_server_name
  127. },
  128. #ifndef OPENSSL_NO_SRP
  129. {
  130. TLSEXT_TYPE_srp,
  131. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  132. init_srp, tls_parse_ctos_srp, NULL, NULL, tls_construct_ctos_srp, NULL
  133. },
  134. #else
  135. INVALID_EXTENSION,
  136. #endif
  137. #ifndef OPENSSL_NO_EC
  138. {
  139. TLSEXT_TYPE_ec_point_formats,
  140. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  141. | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  142. NULL, tls_parse_ctos_ec_pt_formats, tls_parse_stoc_ec_pt_formats,
  143. tls_construct_stoc_ec_pt_formats, tls_construct_ctos_ec_pt_formats,
  144. final_ec_pt_formats
  145. },
  146. {
  147. TLSEXT_TYPE_supported_groups,
  148. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
  149. NULL, tls_parse_ctos_supported_groups, NULL,
  150. NULL /* TODO(TLS1.3): Need to add this */,
  151. tls_construct_ctos_supported_groups, NULL
  152. },
  153. #else
  154. INVALID_EXTENSION,
  155. INVALID_EXTENSION,
  156. #endif
  157. {
  158. TLSEXT_TYPE_session_ticket,
  159. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  160. | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  161. init_session_ticket, tls_parse_ctos_session_ticket,
  162. tls_parse_stoc_session_ticket, tls_construct_stoc_session_ticket,
  163. tls_construct_ctos_session_ticket, NULL
  164. },
  165. {
  166. TLSEXT_TYPE_signature_algorithms,
  167. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
  168. init_sig_algs, tls_parse_ctos_sig_algs,
  169. tls_parse_ctos_sig_algs, tls_construct_ctos_sig_algs,
  170. tls_construct_ctos_sig_algs, final_sig_algs
  171. },
  172. #ifndef OPENSSL_NO_OCSP
  173. {
  174. TLSEXT_TYPE_status_request,
  175. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  176. | SSL_EXT_TLS1_3_CERTIFICATE,
  177. init_status_request, tls_parse_ctos_status_request,
  178. tls_parse_stoc_status_request, tls_construct_stoc_status_request,
  179. tls_construct_ctos_status_request, NULL
  180. },
  181. #else
  182. INVALID_EXTENSION,
  183. #endif
  184. #ifndef OPENSSL_NO_NEXTPROTONEG
  185. {
  186. TLSEXT_TYPE_next_proto_neg,
  187. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  188. | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  189. init_npn, tls_parse_ctos_npn, tls_parse_stoc_npn,
  190. tls_construct_stoc_next_proto_neg, tls_construct_ctos_npn, NULL
  191. },
  192. #else
  193. INVALID_EXTENSION,
  194. #endif
  195. {
  196. /*
  197. * Must appear in this list after server_name so that finalisation
  198. * happens after server_name callbacks
  199. */
  200. TLSEXT_TYPE_application_layer_protocol_negotiation,
  201. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  202. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
  203. init_alpn, tls_parse_ctos_alpn, tls_parse_stoc_alpn,
  204. tls_construct_stoc_alpn, tls_construct_ctos_alpn, final_alpn
  205. },
  206. #ifndef OPENSSL_NO_SRTP
  207. {
  208. TLSEXT_TYPE_use_srtp,
  209. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  210. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS | SSL_EXT_DTLS_ONLY,
  211. init_srtp, tls_parse_ctos_use_srtp, tls_parse_stoc_use_srtp,
  212. tls_construct_stoc_use_srtp, tls_construct_ctos_use_srtp, NULL
  213. },
  214. #else
  215. INVALID_EXTENSION,
  216. #endif
  217. {
  218. TLSEXT_TYPE_encrypt_then_mac,
  219. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  220. | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  221. init_etm, tls_parse_ctos_etm, tls_parse_stoc_etm,
  222. tls_construct_stoc_etm, tls_construct_ctos_etm, NULL
  223. },
  224. #ifndef OPENSSL_NO_CT
  225. {
  226. TLSEXT_TYPE_signed_certificate_timestamp,
  227. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  228. | SSL_EXT_TLS1_3_CERTIFICATE,
  229. NULL,
  230. /*
  231. * No server side support for this, but can be provided by a custom
  232. * extension. This is an exception to the rule that custom extensions
  233. * cannot override built in ones.
  234. */
  235. NULL, tls_parse_stoc_sct, NULL, tls_construct_ctos_sct, NULL
  236. },
  237. #else
  238. INVALID_EXTENSION,
  239. #endif
  240. {
  241. TLSEXT_TYPE_extended_master_secret,
  242. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  243. | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  244. init_ems, tls_parse_ctos_ems, tls_parse_stoc_ems,
  245. tls_construct_stoc_ems, tls_construct_ctos_ems, final_ems
  246. },
  247. {
  248. TLSEXT_TYPE_supported_versions,
  249. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS_IMPLEMENTATION_ONLY
  250. | SSL_EXT_TLS1_3_ONLY,
  251. NULL,
  252. /* Processed inline as part of version selection */
  253. NULL, NULL, NULL, tls_construct_ctos_supported_versions, NULL
  254. },
  255. {
  256. TLSEXT_TYPE_psk_kex_modes,
  257. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS_IMPLEMENTATION_ONLY
  258. | SSL_EXT_TLS1_3_ONLY,
  259. init_psk_kex_modes, tls_parse_ctos_psk_kex_modes, NULL, NULL,
  260. tls_construct_ctos_psk_kex_modes, NULL
  261. },
  262. #ifndef OPENSSL_NO_EC
  263. {
  264. /*
  265. * Must be in this list after supported_groups. We need that to have
  266. * been parsed before we do this one.
  267. */
  268. TLSEXT_TYPE_key_share,
  269. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
  270. | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST | SSL_EXT_TLS_IMPLEMENTATION_ONLY
  271. | SSL_EXT_TLS1_3_ONLY,
  272. NULL, tls_parse_ctos_key_share, tls_parse_stoc_key_share,
  273. tls_construct_stoc_key_share, tls_construct_ctos_key_share,
  274. final_key_share
  275. },
  276. #endif
  277. {
  278. TLSEXT_TYPE_cookie,
  279. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
  280. | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY,
  281. NULL, NULL, tls_parse_stoc_cookie, NULL, tls_construct_ctos_cookie,
  282. NULL
  283. },
  284. {
  285. /*
  286. * Special unsolicited ServerHello extension only used when
  287. * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set
  288. */
  289. TLSEXT_TYPE_cryptopro_bug,
  290. SSL_EXT_TLS1_2_SERVER_HELLO | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  291. NULL, NULL, NULL, tls_construct_stoc_cryptopro_bug, NULL, NULL
  292. },
  293. {
  294. TLSEXT_TYPE_early_data,
  295. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
  296. | SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
  297. NULL, tls_parse_ctos_early_data, tls_parse_stoc_early_data,
  298. tls_construct_stoc_early_data, tls_construct_ctos_early_data,
  299. final_early_data
  300. },
  301. {
  302. TLSEXT_TYPE_certificate_authorities,
  303. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
  304. | SSL_EXT_TLS1_3_ONLY,
  305. init_certificate_authorities,
  306. tls_parse_certificate_authorities, tls_parse_certificate_authorities,
  307. tls_construct_certificate_authorities,
  308. tls_construct_certificate_authorities, NULL,
  309. },
  310. {
  311. /* Must be immediately before pre_shared_key */
  312. TLSEXT_TYPE_padding,
  313. SSL_EXT_CLIENT_HELLO,
  314. NULL,
  315. /* We send this, but don't read it */
  316. NULL, NULL, NULL, tls_construct_ctos_padding, NULL
  317. },
  318. {
  319. /* Required by the TLSv1.3 spec to always be the last extension */
  320. TLSEXT_TYPE_psk,
  321. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
  322. | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY,
  323. NULL, tls_parse_ctos_psk, tls_parse_stoc_psk, tls_construct_stoc_psk,
  324. tls_construct_ctos_psk, NULL
  325. }
  326. };
  327. /* Check whether an extension's context matches the current context */
  328. static int validate_context(SSL *s, unsigned int extctx, unsigned int thisctx)
  329. {
  330. /* Check we're allowed to use this extension in this context */
  331. if ((thisctx & extctx) == 0)
  332. return 0;
  333. if (SSL_IS_DTLS(s)) {
  334. if ((extctx & SSL_EXT_TLS_ONLY) != 0)
  335. return 0;
  336. } else if ((extctx & SSL_EXT_DTLS_ONLY) != 0) {
  337. return 0;
  338. }
  339. return 1;
  340. }
  341. /*
  342. * Verify whether we are allowed to use the extension |type| in the current
  343. * |context|. Returns 1 to indicate the extension is allowed or unknown or 0 to
  344. * indicate the extension is not allowed. If returning 1 then |*found| is set to
  345. * the definition for the extension we found.
  346. */
  347. static int verify_extension(SSL *s, unsigned int context, unsigned int type,
  348. custom_ext_methods *meths, RAW_EXTENSION *rawexlist,
  349. RAW_EXTENSION **found)
  350. {
  351. size_t i;
  352. size_t builtin_num = OSSL_NELEM(ext_defs);
  353. const EXTENSION_DEFINITION *thisext;
  354. for (i = 0, thisext = ext_defs; i < builtin_num; i++, thisext++) {
  355. if (type == thisext->type) {
  356. if (!validate_context(s, thisext->context, context))
  357. return 0;
  358. *found = &rawexlist[i];
  359. return 1;
  360. }
  361. }
  362. /* Check the custom extensions */
  363. if (meths != NULL) {
  364. size_t offset = 0;
  365. ENDPOINT role = ENDPOINT_BOTH;
  366. custom_ext_method *meth = NULL;
  367. if ((context & SSL_EXT_CLIENT_HELLO) != 0)
  368. role = ENDPOINT_SERVER;
  369. else if ((context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0)
  370. role = ENDPOINT_CLIENT;
  371. meth = custom_ext_find(meths, role, type, &offset);
  372. if (meth != NULL) {
  373. if (!validate_context(s, meth->context, context))
  374. return 0;
  375. *found = &rawexlist[offset + builtin_num];
  376. return 1;
  377. }
  378. }
  379. /* Unknown extension. We allow it */
  380. *found = NULL;
  381. return 1;
  382. }
  383. /*
  384. * Check whether the context defined for an extension |extctx| means whether
  385. * the extension is relevant for the current context |thisctx| or not. Returns
  386. * 1 if the extension is relevant for this context, and 0 otherwise
  387. */
  388. int extension_is_relevant(SSL *s, unsigned int extctx, unsigned int thisctx)
  389. {
  390. if ((SSL_IS_DTLS(s)
  391. && (extctx & SSL_EXT_TLS_IMPLEMENTATION_ONLY) != 0)
  392. || (s->version == SSL3_VERSION
  393. && (extctx & SSL_EXT_SSL3_ALLOWED) == 0)
  394. || (SSL_IS_TLS13(s)
  395. && (extctx & SSL_EXT_TLS1_2_AND_BELOW_ONLY) != 0)
  396. || (!SSL_IS_TLS13(s) && (extctx & SSL_EXT_TLS1_3_ONLY) != 0)
  397. || (s->hit && (extctx & SSL_EXT_IGNORE_ON_RESUMPTION) != 0))
  398. return 0;
  399. return 1;
  400. }
  401. /*
  402. * Gather a list of all the extensions from the data in |packet]. |context|
  403. * tells us which message this extension is for. The raw extension data is
  404. * stored in |*res| on success. In the event of an error the alert type to use
  405. * is stored in |*al|. We don't actually process the content of the extensions
  406. * yet, except to check their types. This function also runs the initialiser
  407. * functions for all known extensions if |init| is nonzero (whether we have
  408. * collected them or not). If successful the caller is responsible for freeing
  409. * the contents of |*res|.
  410. *
  411. * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
  412. * more than one extension of the same type in a ClientHello or ServerHello.
  413. * This function returns 1 if all extensions are unique and we have parsed their
  414. * types, and 0 if the extensions contain duplicates, could not be successfully
  415. * found, or an internal error occurred. We only check duplicates for
  416. * extensions that we know about. We ignore others.
  417. */
  418. int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context,
  419. RAW_EXTENSION **res, int *al, size_t *len,
  420. int init)
  421. {
  422. PACKET extensions = *packet;
  423. size_t i = 0;
  424. size_t num_exts;
  425. custom_ext_methods *exts = &s->cert->custext;
  426. RAW_EXTENSION *raw_extensions = NULL;
  427. const EXTENSION_DEFINITION *thisexd;
  428. *res = NULL;
  429. /*
  430. * Initialise server side custom extensions. Client side is done during
  431. * construction of extensions for the ClientHello.
  432. */
  433. if ((context & SSL_EXT_CLIENT_HELLO) != 0)
  434. custom_ext_init(&s->cert->custext);
  435. num_exts = OSSL_NELEM(ext_defs) + (exts != NULL ? exts->meths_count : 0);
  436. raw_extensions = OPENSSL_zalloc(num_exts * sizeof(*raw_extensions));
  437. if (raw_extensions == NULL) {
  438. *al = SSL_AD_INTERNAL_ERROR;
  439. SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, ERR_R_MALLOC_FAILURE);
  440. return 0;
  441. }
  442. while (PACKET_remaining(&extensions) > 0) {
  443. unsigned int type;
  444. PACKET extension;
  445. RAW_EXTENSION *thisex;
  446. if (!PACKET_get_net_2(&extensions, &type) ||
  447. !PACKET_get_length_prefixed_2(&extensions, &extension)) {
  448. SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_BAD_EXTENSION);
  449. *al = SSL_AD_DECODE_ERROR;
  450. goto err;
  451. }
  452. /*
  453. * Verify this extension is allowed. We only check duplicates for
  454. * extensions that we recognise. We also have a special case for the
  455. * PSK extension, which must be the last one in the ClientHello.
  456. */
  457. if (!verify_extension(s, context, type, exts, raw_extensions, &thisex)
  458. || (thisex != NULL && thisex->present == 1)
  459. || (type == TLSEXT_TYPE_psk
  460. && (context & SSL_EXT_CLIENT_HELLO) != 0
  461. && PACKET_remaining(&extensions) != 0)) {
  462. SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_BAD_EXTENSION);
  463. *al = SSL_AD_ILLEGAL_PARAMETER;
  464. goto err;
  465. }
  466. if (thisex != NULL) {
  467. thisex->data = extension;
  468. thisex->present = 1;
  469. thisex->type = type;
  470. }
  471. }
  472. if (init) {
  473. /*
  474. * Initialise all known extensions relevant to this context,
  475. * whether we have found them or not
  476. */
  477. for (thisexd = ext_defs, i = 0; i < OSSL_NELEM(ext_defs);
  478. i++, thisexd++) {
  479. if (thisexd->init != NULL && (thisexd->context & context) != 0
  480. && extension_is_relevant(s, thisexd->context, context)
  481. && !thisexd->init(s, context)) {
  482. *al = SSL_AD_INTERNAL_ERROR;
  483. goto err;
  484. }
  485. }
  486. }
  487. *res = raw_extensions;
  488. if (len != NULL)
  489. *len = num_exts;
  490. return 1;
  491. err:
  492. OPENSSL_free(raw_extensions);
  493. return 0;
  494. }
  495. /*
  496. * Runs the parser for a given extension with index |idx|. |exts| contains the
  497. * list of all parsed extensions previously collected by
  498. * tls_collect_extensions(). The parser is only run if it is applicable for the
  499. * given |context| and the parser has not already been run. If this is for a
  500. * Certificate message, then we also provide the parser with the relevant
  501. * Certificate |x| and its position in the |chainidx| with 0 being the first
  502. * Certificate. Returns 1 on success or 0 on failure. In the event of a failure
  503. * |*al| is populated with a suitable alert code. If an extension is not present
  504. * this counted as success.
  505. */
  506. int tls_parse_extension(SSL *s, TLSEXT_INDEX idx, int context,
  507. RAW_EXTENSION *exts, X509 *x, size_t chainidx, int *al)
  508. {
  509. RAW_EXTENSION *currext = &exts[idx];
  510. int (*parser)(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  511. size_t chainidx, int *al) = NULL;
  512. /* Skip if the extension is not present */
  513. if (!currext->present)
  514. return 1;
  515. if (s->ext.debug_cb)
  516. s->ext.debug_cb(s, !s->server, currext->type,
  517. PACKET_data(&currext->data),
  518. PACKET_remaining(&currext->data),
  519. s->ext.debug_arg);
  520. /* Skip if we've already parsed this extension */
  521. if (currext->parsed)
  522. return 1;
  523. currext->parsed = 1;
  524. if (idx < OSSL_NELEM(ext_defs)) {
  525. /* We are handling a built-in extension */
  526. const EXTENSION_DEFINITION *extdef = &ext_defs[idx];
  527. /* Check if extension is defined for our protocol. If not, skip */
  528. if (!extension_is_relevant(s, extdef->context, context))
  529. return 1;
  530. parser = s->server ? extdef->parse_ctos : extdef->parse_stoc;
  531. if (parser != NULL)
  532. return parser(s, &currext->data, context, x, chainidx, al);
  533. /*
  534. * If the parser is NULL we fall through to the custom extension
  535. * processing
  536. */
  537. }
  538. /* Parse custom extensions */
  539. if (custom_ext_parse(s, context, currext->type,
  540. PACKET_data(&currext->data),
  541. PACKET_remaining(&currext->data),
  542. x, chainidx, al) <= 0)
  543. return 0;
  544. return 1;
  545. }
  546. /*
  547. * Parse all remaining extensions that have not yet been parsed. Also calls the
  548. * finalisation for all extensions at the end if |fin| is nonzero, whether we
  549. * collected them or not. Returns 1 for success or 0 for failure. If we are
  550. * working on a Certificate message then we also pass the Certificate |x| and
  551. * its position in the |chainidx|, with 0 being the first certificate. On
  552. * failure, |*al| is populated with a suitable alert code.
  553. */
  554. int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts, X509 *x,
  555. size_t chainidx, int *al, int fin)
  556. {
  557. size_t i, numexts = OSSL_NELEM(ext_defs);
  558. const EXTENSION_DEFINITION *thisexd;
  559. /* Calculate the number of extensions in the extensions list */
  560. numexts += s->cert->custext.meths_count;
  561. /* Parse each extension in turn */
  562. for (i = 0; i < numexts; i++) {
  563. if (!tls_parse_extension(s, i, context, exts, x, chainidx, al))
  564. return 0;
  565. }
  566. if (fin) {
  567. /*
  568. * Finalise all known extensions relevant to this context,
  569. * whether we have found them or not
  570. */
  571. for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs);
  572. i++, thisexd++) {
  573. if (thisexd->final != NULL && (thisexd->context & context) != 0
  574. && !thisexd->final(s, context, exts[i].present, al))
  575. return 0;
  576. }
  577. }
  578. return 1;
  579. }
  580. int should_add_extension(SSL *s, unsigned int extctx, unsigned int thisctx,
  581. int max_version)
  582. {
  583. /* Skip if not relevant for our context */
  584. if ((extctx & thisctx) == 0)
  585. return 0;
  586. /* Check if this extension is defined for our protocol. If not, skip */
  587. if ((SSL_IS_DTLS(s) && (extctx & SSL_EXT_TLS_IMPLEMENTATION_ONLY) != 0)
  588. || (s->version == SSL3_VERSION
  589. && (extctx & SSL_EXT_SSL3_ALLOWED) == 0)
  590. || (SSL_IS_TLS13(s)
  591. && (extctx & SSL_EXT_TLS1_2_AND_BELOW_ONLY) != 0)
  592. || (!SSL_IS_TLS13(s)
  593. && (extctx & SSL_EXT_TLS1_3_ONLY) != 0
  594. && (thisctx & SSL_EXT_CLIENT_HELLO) == 0)
  595. || ((extctx & SSL_EXT_TLS1_3_ONLY) != 0
  596. && (thisctx & SSL_EXT_CLIENT_HELLO) != 0
  597. && (SSL_IS_DTLS(s) || max_version < TLS1_3_VERSION)))
  598. return 0;
  599. return 1;
  600. }
  601. /*
  602. * Construct all the extensions relevant to the current |context| and write
  603. * them to |pkt|. If this is an extension for a Certificate in a Certificate
  604. * message, then |x| will be set to the Certificate we are handling, and
  605. * |chainidx| will indicate the position in the chainidx we are processing (with
  606. * 0 being the first in the chain). Returns 1 on success or 0 on failure. If a
  607. * failure occurs then |al| is populated with a suitable alert code. On a
  608. * failure construction stops at the first extension to fail to construct.
  609. */
  610. int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context,
  611. X509 *x, size_t chainidx, int *al)
  612. {
  613. size_t i;
  614. int min_version, max_version = 0, reason, tmpal;
  615. const EXTENSION_DEFINITION *thisexd;
  616. /*
  617. * Normally if something goes wrong during construction it's an internal
  618. * error. We can always override this later.
  619. */
  620. tmpal = SSL_AD_INTERNAL_ERROR;
  621. if (!WPACKET_start_sub_packet_u16(pkt)
  622. /*
  623. * If extensions are of zero length then we don't even add the
  624. * extensions length bytes to a ClientHello/ServerHello in SSLv3
  625. */
  626. || ((context &
  627. (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO)) != 0
  628. && s->version == SSL3_VERSION
  629. && !WPACKET_set_flags(pkt,
  630. WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))) {
  631. SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
  632. goto err;
  633. }
  634. if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
  635. reason = ssl_get_min_max_version(s, &min_version, &max_version);
  636. if (reason != 0) {
  637. SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, reason);
  638. goto err;
  639. }
  640. }
  641. /* Add custom extensions first */
  642. if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
  643. /* On the server side with initiase during ClientHello parsing */
  644. custom_ext_init(&s->cert->custext);
  645. }
  646. if (!custom_ext_add(s, context, pkt, x, chainidx, max_version, &tmpal)) {
  647. SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
  648. goto err;
  649. }
  650. for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs); i++, thisexd++) {
  651. int (*construct)(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  652. size_t chainidx, int *al);
  653. /* Skip if not relevant for our context */
  654. if (!should_add_extension(s, thisexd->context, context, max_version))
  655. continue;
  656. construct = s->server ? thisexd->construct_stoc
  657. : thisexd->construct_ctos;
  658. if (construct == NULL)
  659. continue;
  660. if (!construct(s, pkt, context, x, chainidx, &tmpal))
  661. goto err;
  662. }
  663. if (!WPACKET_close(pkt)) {
  664. SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
  665. goto err;
  666. }
  667. return 1;
  668. err:
  669. *al = tmpal;
  670. return 0;
  671. }
  672. /*
  673. * Built in extension finalisation and initialisation functions. All initialise
  674. * or finalise the associated extension type for the given |context|. For
  675. * finalisers |sent| is set to 1 if we saw the extension during parsing, and 0
  676. * otherwise. These functions return 1 on success or 0 on failure. In the event
  677. * of a failure then |*al| is populated with a suitable error code.
  678. */
  679. static int final_renegotiate(SSL *s, unsigned int context, int sent,
  680. int *al)
  681. {
  682. if (!s->server) {
  683. /*
  684. * Check if we can connect to a server that doesn't support safe
  685. * renegotiation
  686. */
  687. if (!(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
  688. && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
  689. && !sent) {
  690. *al = SSL_AD_HANDSHAKE_FAILURE;
  691. SSLerr(SSL_F_FINAL_RENEGOTIATE,
  692. SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  693. return 0;
  694. }
  695. return 1;
  696. }
  697. /* Need RI if renegotiating */
  698. if (s->renegotiate
  699. && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
  700. && !sent) {
  701. *al = SSL_AD_HANDSHAKE_FAILURE;
  702. SSLerr(SSL_F_FINAL_RENEGOTIATE,
  703. SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  704. return 0;
  705. }
  706. return 1;
  707. }
  708. static int init_server_name(SSL *s, unsigned int context)
  709. {
  710. if (s->server)
  711. s->servername_done = 0;
  712. return 1;
  713. }
  714. static int final_server_name(SSL *s, unsigned int context, int sent,
  715. int *al)
  716. {
  717. int ret = SSL_TLSEXT_ERR_NOACK;
  718. int altmp = SSL_AD_UNRECOGNIZED_NAME;
  719. if (s->ctx != NULL && s->ctx->ext.servername_cb != 0)
  720. ret = s->ctx->ext.servername_cb(s, &altmp,
  721. s->ctx->ext.servername_arg);
  722. else if (s->session_ctx != NULL
  723. && s->session_ctx->ext.servername_cb != 0)
  724. ret = s->session_ctx->ext.servername_cb(s, &altmp,
  725. s->session_ctx->ext.servername_arg);
  726. switch (ret) {
  727. case SSL_TLSEXT_ERR_ALERT_FATAL:
  728. *al = altmp;
  729. return 0;
  730. case SSL_TLSEXT_ERR_ALERT_WARNING:
  731. *al = altmp;
  732. return 1;
  733. case SSL_TLSEXT_ERR_NOACK:
  734. s->servername_done = 0;
  735. return 1;
  736. default:
  737. return 1;
  738. }
  739. }
  740. #ifndef OPENSSL_NO_EC
  741. static int final_ec_pt_formats(SSL *s, unsigned int context, int sent,
  742. int *al)
  743. {
  744. unsigned long alg_k, alg_a;
  745. if (s->server)
  746. return 1;
  747. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  748. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  749. /*
  750. * If we are client and using an elliptic curve cryptography cipher
  751. * suite, then if server returns an EC point formats lists extension it
  752. * must contain uncompressed.
  753. */
  754. if (s->ext.ecpointformats != NULL
  755. && s->ext.ecpointformats_len > 0
  756. && s->session->ext.ecpointformats != NULL
  757. && s->session->ext.ecpointformats_len > 0
  758. && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
  759. /* we are using an ECC cipher */
  760. size_t i;
  761. unsigned char *list = s->session->ext.ecpointformats;
  762. for (i = 0; i < s->session->ext.ecpointformats_len; i++) {
  763. if (*list++ == TLSEXT_ECPOINTFORMAT_uncompressed)
  764. break;
  765. }
  766. if (i == s->session->ext.ecpointformats_len) {
  767. SSLerr(SSL_F_FINAL_EC_PT_FORMATS,
  768. SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
  769. return 0;
  770. }
  771. }
  772. return 1;
  773. }
  774. #endif
  775. static int init_session_ticket(SSL *s, unsigned int context)
  776. {
  777. if (!s->server)
  778. s->ext.ticket_expected = 0;
  779. return 1;
  780. }
  781. #ifndef OPENSSL_NO_OCSP
  782. static int init_status_request(SSL *s, unsigned int context)
  783. {
  784. if (s->server) {
  785. s->ext.status_type = TLSEXT_STATUSTYPE_nothing;
  786. } else {
  787. /*
  788. * Ensure we get sensible values passed to tlsext_status_cb in the event
  789. * that we don't receive a status message
  790. */
  791. OPENSSL_free(s->ext.ocsp.resp);
  792. s->ext.ocsp.resp = NULL;
  793. s->ext.ocsp.resp_len = 0;
  794. }
  795. return 1;
  796. }
  797. #endif
  798. #ifndef OPENSSL_NO_NEXTPROTONEG
  799. static int init_npn(SSL *s, unsigned int context)
  800. {
  801. s->s3->npn_seen = 0;
  802. return 1;
  803. }
  804. #endif
  805. static int init_alpn(SSL *s, unsigned int context)
  806. {
  807. OPENSSL_free(s->s3->alpn_selected);
  808. s->s3->alpn_selected = NULL;
  809. s->s3->alpn_selected_len = 0;
  810. if (s->server) {
  811. OPENSSL_free(s->s3->alpn_proposed);
  812. s->s3->alpn_proposed = NULL;
  813. s->s3->alpn_proposed_len = 0;
  814. }
  815. return 1;
  816. }
  817. static int final_alpn(SSL *s, unsigned int context, int sent, int *al)
  818. {
  819. const unsigned char *selected = NULL;
  820. unsigned char selected_len = 0;
  821. if (!s->server)
  822. return 1;
  823. if (s->ctx->ext.alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) {
  824. int r = s->ctx->ext.alpn_select_cb(s, &selected, &selected_len,
  825. s->s3->alpn_proposed,
  826. (unsigned int)s->s3->alpn_proposed_len,
  827. s->ctx->ext.alpn_select_cb_arg);
  828. if (r == SSL_TLSEXT_ERR_OK) {
  829. OPENSSL_free(s->s3->alpn_selected);
  830. s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len);
  831. if (s->s3->alpn_selected == NULL) {
  832. *al = SSL_AD_INTERNAL_ERROR;
  833. return 0;
  834. }
  835. s->s3->alpn_selected_len = selected_len;
  836. #ifndef OPENSSL_NO_NEXTPROTONEG
  837. /* ALPN takes precedence over NPN. */
  838. s->s3->npn_seen = 0;
  839. #endif
  840. } else if (r == SSL_TLSEXT_ERR_NOACK) {
  841. /* Behave as if no callback was present. */
  842. return 1;
  843. } else {
  844. *al = SSL_AD_NO_APPLICATION_PROTOCOL;
  845. return 0;
  846. }
  847. }
  848. return 1;
  849. }
  850. static int init_sig_algs(SSL *s, unsigned int context)
  851. {
  852. /* Clear any signature algorithms extension received */
  853. OPENSSL_free(s->s3->tmp.peer_sigalgs);
  854. s->s3->tmp.peer_sigalgs = NULL;
  855. return 1;
  856. }
  857. #ifndef OPENSSL_NO_SRP
  858. static int init_srp(SSL *s, unsigned int context)
  859. {
  860. OPENSSL_free(s->srp_ctx.login);
  861. s->srp_ctx.login = NULL;
  862. return 1;
  863. }
  864. #endif
  865. static int init_etm(SSL *s, unsigned int context)
  866. {
  867. s->ext.use_etm = 0;
  868. return 1;
  869. }
  870. static int init_ems(SSL *s, unsigned int context)
  871. {
  872. if (!s->server)
  873. s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
  874. return 1;
  875. }
  876. static int final_ems(SSL *s, unsigned int context, int sent, int *al)
  877. {
  878. if (!s->server && s->hit) {
  879. /*
  880. * Check extended master secret extension is consistent with
  881. * original session.
  882. */
  883. if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
  884. !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
  885. *al = SSL_AD_HANDSHAKE_FAILURE;
  886. SSLerr(SSL_F_FINAL_EMS, SSL_R_INCONSISTENT_EXTMS);
  887. return 0;
  888. }
  889. }
  890. return 1;
  891. }
  892. static int init_certificate_authorities(SSL *s, unsigned int context)
  893. {
  894. sk_X509_NAME_pop_free(s->s3->tmp.peer_ca_names, X509_NAME_free);
  895. s->s3->tmp.peer_ca_names = NULL;
  896. return 1;
  897. }
  898. static int tls_construct_certificate_authorities(SSL *s, WPACKET *pkt,
  899. unsigned int context, X509 *x,
  900. size_t chainidx, int *al)
  901. {
  902. const STACK_OF(X509_NAME) *ca_sk = SSL_get0_CA_list(s);
  903. if (ca_sk == NULL || sk_X509_NAME_num(ca_sk) == 0)
  904. return 1;
  905. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_certificate_authorities)
  906. || !WPACKET_start_sub_packet_u16(pkt)
  907. || !construct_ca_names(s, pkt)
  908. || !WPACKET_close(pkt)) {
  909. SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_AUTHORITIES,
  910. ERR_R_INTERNAL_ERROR);
  911. return 0;
  912. }
  913. return 1;
  914. }
  915. static int tls_parse_certificate_authorities(SSL *s, PACKET *pkt,
  916. unsigned int context, X509 *x,
  917. size_t chainidx, int *al)
  918. {
  919. if (!parse_ca_names(s, pkt, al))
  920. return 0;
  921. if (PACKET_remaining(pkt) != 0) {
  922. *al = SSL_AD_DECODE_ERROR;
  923. return 0;
  924. }
  925. return 1;
  926. }
  927. #ifndef OPENSSL_NO_SRTP
  928. static int init_srtp(SSL *s, unsigned int context)
  929. {
  930. if (s->server)
  931. s->srtp_profile = NULL;
  932. return 1;
  933. }
  934. #endif
  935. static int final_sig_algs(SSL *s, unsigned int context, int sent, int *al)
  936. {
  937. if (!sent && SSL_IS_TLS13(s)) {
  938. *al = TLS13_AD_MISSING_EXTENSION;
  939. SSLerr(SSL_F_FINAL_SIG_ALGS, SSL_R_MISSING_SIGALGS_EXTENSION);
  940. return 0;
  941. }
  942. return 1;
  943. }
  944. #ifndef OPENSSL_NO_EC
  945. static int final_key_share(SSL *s, unsigned int context, int sent, int *al)
  946. {
  947. if (!SSL_IS_TLS13(s))
  948. return 1;
  949. /*
  950. * If
  951. * we are a client
  952. * AND
  953. * we have no key_share
  954. * AND
  955. * (we are not resuming
  956. * OR the kex_mode doesn't allow non key_share resumes)
  957. * THEN
  958. * fail;
  959. */
  960. if (!s->server
  961. && !sent
  962. && (!s->hit
  963. || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0)) {
  964. /* Nothing left we can do - just fail */
  965. *al = SSL_AD_HANDSHAKE_FAILURE;
  966. SSLerr(SSL_F_FINAL_KEY_SHARE, SSL_R_NO_SUITABLE_KEY_SHARE);
  967. return 0;
  968. }
  969. /*
  970. * If
  971. * we are a server
  972. * AND
  973. * we have no key_share
  974. * THEN
  975. * If
  976. * we didn't already send a HelloRetryRequest
  977. * AND
  978. * the client sent a key_share extension
  979. * AND
  980. * (we are not resuming
  981. * OR the kex_mode allows key_share resumes)
  982. * AND
  983. * a shared group exists
  984. * THEN
  985. * send a HelloRetryRequest
  986. * ELSE If
  987. * we are not resuming
  988. * OR
  989. * the kex_mode doesn't allow non key_share resumes
  990. * THEN
  991. * fail;
  992. */
  993. if (s->server && s->s3->peer_tmp == NULL) {
  994. /* No suitable share */
  995. if (s->hello_retry_request == 0 && sent
  996. && (!s->hit
  997. || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE)
  998. != 0)) {
  999. const unsigned char *pcurves, *pcurvestmp, *clntcurves;
  1000. size_t num_curves, clnt_num_curves, i;
  1001. unsigned int group_id = 0;
  1002. /* Check if a shared group exists */
  1003. /* Get the clients list of supported groups. */
  1004. if (!tls1_get_curvelist(s, 1, &clntcurves, &clnt_num_curves)) {
  1005. *al = SSL_AD_INTERNAL_ERROR;
  1006. SSLerr(SSL_F_FINAL_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  1007. return 0;
  1008. }
  1009. /* Get our list of available groups */
  1010. if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) {
  1011. *al = SSL_AD_INTERNAL_ERROR;
  1012. SSLerr(SSL_F_FINAL_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  1013. return 0;
  1014. }
  1015. /* Find the first group we allow that is also in client's list */
  1016. for (i = 0, pcurvestmp = pcurves; i < num_curves;
  1017. i++, pcurvestmp += 2) {
  1018. group_id = bytestogroup(pcurvestmp);
  1019. if (check_in_list(s, group_id, clntcurves, clnt_num_curves, 1))
  1020. break;
  1021. }
  1022. if (i < num_curves) {
  1023. /* A shared group exists so send a HelloRetryRequest */
  1024. s->s3->group_id = group_id;
  1025. s->hello_retry_request = 1;
  1026. return 1;
  1027. }
  1028. }
  1029. if (!s->hit
  1030. || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0) {
  1031. /* Nothing left we can do - just fail */
  1032. *al = SSL_AD_HANDSHAKE_FAILURE;
  1033. SSLerr(SSL_F_FINAL_KEY_SHARE, SSL_R_NO_SUITABLE_KEY_SHARE);
  1034. return 0;
  1035. }
  1036. }
  1037. /* We have a key_share so don't send any more HelloRetryRequest messages */
  1038. if (s->server)
  1039. s->hello_retry_request = 0;
  1040. /*
  1041. * For a client side resumption with no key_share we need to generate
  1042. * the handshake secret (otherwise this is done during key_share
  1043. * processing).
  1044. */
  1045. if (!sent && !s->server && !tls13_generate_handshake_secret(s, NULL, 0)) {
  1046. *al = SSL_AD_INTERNAL_ERROR;
  1047. SSLerr(SSL_F_FINAL_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  1048. return 0;
  1049. }
  1050. return 1;
  1051. }
  1052. #endif
  1053. static int init_psk_kex_modes(SSL *s, unsigned int context)
  1054. {
  1055. s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_NONE;
  1056. return 1;
  1057. }
  1058. int tls_psk_do_binder(SSL *s, const EVP_MD *md, const unsigned char *msgstart,
  1059. size_t binderoffset, const unsigned char *binderin,
  1060. unsigned char *binderout,
  1061. SSL_SESSION *sess, int sign)
  1062. {
  1063. EVP_PKEY *mackey = NULL;
  1064. EVP_MD_CTX *mctx = NULL;
  1065. unsigned char hash[EVP_MAX_MD_SIZE], binderkey[EVP_MAX_MD_SIZE];
  1066. unsigned char finishedkey[EVP_MAX_MD_SIZE], tmpbinder[EVP_MAX_MD_SIZE];
  1067. const char resumption_label[] = "resumption psk binder key";
  1068. size_t bindersize, hashsize = EVP_MD_size(md);
  1069. int ret = -1;
  1070. /* Generate the early_secret */
  1071. if (!tls13_generate_secret(s, md, NULL, sess->master_key,
  1072. sess->master_key_length,
  1073. (unsigned char *)&s->early_secret)) {
  1074. SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
  1075. goto err;
  1076. }
  1077. /*
  1078. * Create the handshake hash for the binder key...the messages so far are
  1079. * empty!
  1080. */
  1081. mctx = EVP_MD_CTX_new();
  1082. if (mctx == NULL
  1083. || EVP_DigestInit_ex(mctx, md, NULL) <= 0
  1084. || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
  1085. SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
  1086. goto err;
  1087. }
  1088. /* Generate the binder key */
  1089. if (!tls13_hkdf_expand(s, md, s->early_secret,
  1090. (unsigned char *)resumption_label,
  1091. sizeof(resumption_label) - 1, hash, binderkey,
  1092. hashsize)) {
  1093. SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
  1094. goto err;
  1095. }
  1096. /* Generate the finished key */
  1097. if (!tls13_derive_finishedkey(s, md, binderkey, finishedkey, hashsize)) {
  1098. SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
  1099. goto err;
  1100. }
  1101. if (EVP_DigestInit_ex(mctx, md, NULL) <= 0) {
  1102. SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
  1103. goto err;
  1104. }
  1105. /*
  1106. * Get a hash of the ClientHello up to the start of the binders. If we are
  1107. * following a HelloRetryRequest then this includes the hash of the first
  1108. * ClientHello and the HelloRetryRequest itself.
  1109. */
  1110. if (s->hello_retry_request) {
  1111. size_t hdatalen;
  1112. void *hdata;
  1113. hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
  1114. if (hdatalen <= 0) {
  1115. SSLerr(SSL_F_TLS_PSK_DO_BINDER, SSL_R_BAD_HANDSHAKE_LENGTH);
  1116. goto err;
  1117. }
  1118. /*
  1119. * For servers the handshake buffer data will include the second
  1120. * ClientHello - which we don't want - so we need to take that bit off.
  1121. */
  1122. if (s->server) {
  1123. PACKET hashprefix, msg;
  1124. /* Find how many bytes are left after the first two messages */
  1125. if (!PACKET_buf_init(&hashprefix, hdata, hdatalen)
  1126. || !PACKET_forward(&hashprefix, 1)
  1127. || !PACKET_get_length_prefixed_3(&hashprefix, &msg)
  1128. || !PACKET_forward(&hashprefix, 1)
  1129. || !PACKET_get_length_prefixed_3(&hashprefix, &msg)) {
  1130. SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
  1131. goto err;
  1132. }
  1133. hdatalen -= PACKET_remaining(&hashprefix);
  1134. }
  1135. if (EVP_DigestUpdate(mctx, hdata, hdatalen) <= 0) {
  1136. SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
  1137. goto err;
  1138. }
  1139. }
  1140. if (EVP_DigestUpdate(mctx, msgstart, binderoffset) <= 0
  1141. || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
  1142. SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
  1143. goto err;
  1144. }
  1145. mackey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, finishedkey, hashsize);
  1146. if (mackey == NULL) {
  1147. SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
  1148. goto err;
  1149. }
  1150. if (!sign)
  1151. binderout = tmpbinder;
  1152. bindersize = hashsize;
  1153. if (EVP_DigestSignInit(mctx, NULL, md, NULL, mackey) <= 0
  1154. || EVP_DigestSignUpdate(mctx, hash, hashsize) <= 0
  1155. || EVP_DigestSignFinal(mctx, binderout, &bindersize) <= 0
  1156. || bindersize != hashsize) {
  1157. SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
  1158. goto err;
  1159. }
  1160. if (sign) {
  1161. ret = 1;
  1162. } else {
  1163. /* HMAC keys can't do EVP_DigestVerify* - use CRYPTO_memcmp instead */
  1164. ret = (CRYPTO_memcmp(binderin, binderout, hashsize) == 0);
  1165. }
  1166. err:
  1167. OPENSSL_cleanse(binderkey, sizeof(binderkey));
  1168. OPENSSL_cleanse(finishedkey, sizeof(finishedkey));
  1169. EVP_PKEY_free(mackey);
  1170. EVP_MD_CTX_free(mctx);
  1171. return ret;
  1172. }
  1173. static int final_early_data(SSL *s, unsigned int context, int sent, int *al)
  1174. {
  1175. if (!s->server || !sent)
  1176. return 1;
  1177. if (s->max_early_data == 0
  1178. || !s->hit
  1179. || s->session->ext.tick_identity != 0
  1180. || s->early_data_state != SSL_EARLY_DATA_ACCEPTING
  1181. || !s->ext.early_data_ok
  1182. || s->hello_retry_request
  1183. || s->s3->alpn_selected_len != s->session->ext.alpn_selected_len
  1184. || (s->s3->alpn_selected_len > 0
  1185. && memcmp(s->s3->alpn_selected, s->session->ext.alpn_selected,
  1186. s->s3->alpn_selected_len) != 0)) {
  1187. s->ext.early_data = SSL_EARLY_DATA_REJECTED;
  1188. } else {
  1189. s->ext.early_data = SSL_EARLY_DATA_ACCEPTED;
  1190. if (!tls13_change_cipher_state(s,
  1191. SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_SERVER_READ)) {
  1192. *al = SSL_AD_INTERNAL_ERROR;
  1193. return 0;
  1194. }
  1195. }
  1196. return 1;
  1197. }