extensions_srvr.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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 <openssl/ocsp.h>
  10. #include "../ssl_locl.h"
  11. #include "statem_locl.h"
  12. /*
  13. * Parse the client's renegotiation binding and abort if it's not right
  14. */
  15. int tls_parse_ctos_renegotiate(SSL *s, PACKET *pkt, unsigned int context,
  16. X509 *x, size_t chainidx, int *al)
  17. {
  18. unsigned int ilen;
  19. const unsigned char *data;
  20. /* Parse the length byte */
  21. if (!PACKET_get_1(pkt, &ilen)
  22. || !PACKET_get_bytes(pkt, &data, ilen)) {
  23. SSLerr(SSL_F_TLS_PARSE_CTOS_RENEGOTIATE,
  24. SSL_R_RENEGOTIATION_ENCODING_ERR);
  25. *al = SSL_AD_ILLEGAL_PARAMETER;
  26. return 0;
  27. }
  28. /* Check that the extension matches */
  29. if (ilen != s->s3->previous_client_finished_len) {
  30. SSLerr(SSL_F_TLS_PARSE_CTOS_RENEGOTIATE,
  31. SSL_R_RENEGOTIATION_MISMATCH);
  32. *al = SSL_AD_HANDSHAKE_FAILURE;
  33. return 0;
  34. }
  35. if (memcmp(data, s->s3->previous_client_finished,
  36. s->s3->previous_client_finished_len)) {
  37. SSLerr(SSL_F_TLS_PARSE_CTOS_RENEGOTIATE,
  38. SSL_R_RENEGOTIATION_MISMATCH);
  39. *al = SSL_AD_HANDSHAKE_FAILURE;
  40. return 0;
  41. }
  42. s->s3->send_connection_binding = 1;
  43. return 1;
  44. }
  45. /*-
  46. * The servername extension is treated as follows:
  47. *
  48. * - Only the hostname type is supported with a maximum length of 255.
  49. * - The servername is rejected if too long or if it contains zeros,
  50. * in which case an fatal alert is generated.
  51. * - The servername field is maintained together with the session cache.
  52. * - When a session is resumed, the servername call back invoked in order
  53. * to allow the application to position itself to the right context.
  54. * - The servername is acknowledged if it is new for a session or when
  55. * it is identical to a previously used for the same session.
  56. * Applications can control the behaviour. They can at any time
  57. * set a 'desirable' servername for a new SSL object. This can be the
  58. * case for example with HTTPS when a Host: header field is received and
  59. * a renegotiation is requested. In this case, a possible servername
  60. * presented in the new client hello is only acknowledged if it matches
  61. * the value of the Host: field.
  62. * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
  63. * if they provide for changing an explicit servername context for the
  64. * session, i.e. when the session has been established with a servername
  65. * extension.
  66. * - On session reconnect, the servername extension may be absent.
  67. */
  68. int tls_parse_ctos_server_name(SSL *s, PACKET *pkt, unsigned int context,
  69. X509 *x, size_t chainidx, int *al)
  70. {
  71. unsigned int servname_type;
  72. PACKET sni, hostname;
  73. if (!PACKET_as_length_prefixed_2(pkt, &sni)
  74. /* ServerNameList must be at least 1 byte long. */
  75. || PACKET_remaining(&sni) == 0) {
  76. *al = SSL_AD_DECODE_ERROR;
  77. return 0;
  78. }
  79. /*
  80. * Although the server_name extension was intended to be
  81. * extensible to new name types, RFC 4366 defined the
  82. * syntax inextensibly and OpenSSL 1.0.x parses it as
  83. * such.
  84. * RFC 6066 corrected the mistake but adding new name types
  85. * is nevertheless no longer feasible, so act as if no other
  86. * SNI types can exist, to simplify parsing.
  87. *
  88. * Also note that the RFC permits only one SNI value per type,
  89. * i.e., we can only have a single hostname.
  90. */
  91. if (!PACKET_get_1(&sni, &servname_type)
  92. || servname_type != TLSEXT_NAMETYPE_host_name
  93. || !PACKET_as_length_prefixed_2(&sni, &hostname)) {
  94. *al = SSL_AD_DECODE_ERROR;
  95. return 0;
  96. }
  97. if (!s->hit) {
  98. if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) {
  99. *al = TLS1_AD_UNRECOGNIZED_NAME;
  100. return 0;
  101. }
  102. if (PACKET_contains_zero_byte(&hostname)) {
  103. *al = TLS1_AD_UNRECOGNIZED_NAME;
  104. return 0;
  105. }
  106. OPENSSL_free(s->session->ext.hostname);
  107. s->session->ext.hostname = NULL;
  108. if (!PACKET_strndup(&hostname, &s->session->ext.hostname)) {
  109. *al = TLS1_AD_INTERNAL_ERROR;
  110. return 0;
  111. }
  112. s->servername_done = 1;
  113. } else {
  114. /*
  115. * TODO(openssl-team): if the SNI doesn't match, we MUST
  116. * fall back to a full handshake.
  117. */
  118. s->servername_done = s->session->ext.hostname
  119. && PACKET_equal(&hostname, s->session->ext.hostname,
  120. strlen(s->session->ext.hostname));
  121. }
  122. return 1;
  123. }
  124. #ifndef OPENSSL_NO_SRP
  125. int tls_parse_ctos_srp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  126. size_t chainidx, int *al)
  127. {
  128. PACKET srp_I;
  129. if (!PACKET_as_length_prefixed_1(pkt, &srp_I)
  130. || PACKET_contains_zero_byte(&srp_I)) {
  131. *al = SSL_AD_DECODE_ERROR;
  132. return 0;
  133. }
  134. /*
  135. * TODO(openssl-team): currently, we re-authenticate the user
  136. * upon resumption. Instead, we MUST ignore the login.
  137. */
  138. if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) {
  139. *al = TLS1_AD_INTERNAL_ERROR;
  140. return 0;
  141. }
  142. return 1;
  143. }
  144. #endif
  145. #ifndef OPENSSL_NO_EC
  146. int tls_parse_ctos_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context,
  147. X509 *x, size_t chainidx, int *al)
  148. {
  149. PACKET ec_point_format_list;
  150. if (!PACKET_as_length_prefixed_1(pkt, &ec_point_format_list)
  151. || PACKET_remaining(&ec_point_format_list) == 0) {
  152. *al = SSL_AD_DECODE_ERROR;
  153. return 0;
  154. }
  155. if (!s->hit) {
  156. if (!PACKET_memdup(&ec_point_format_list,
  157. &s->session->ext.ecpointformats,
  158. &s->session->ext.ecpointformats_len)) {
  159. *al = TLS1_AD_INTERNAL_ERROR;
  160. return 0;
  161. }
  162. }
  163. return 1;
  164. }
  165. #endif /* OPENSSL_NO_EC */
  166. int tls_parse_ctos_session_ticket(SSL *s, PACKET *pkt, unsigned int context,
  167. X509 *x, size_t chainidx, int *al)
  168. {
  169. if (s->ext.session_ticket_cb &&
  170. !s->ext.session_ticket_cb(s, PACKET_data(pkt),
  171. PACKET_remaining(pkt),
  172. s->ext.session_ticket_cb_arg)) {
  173. *al = TLS1_AD_INTERNAL_ERROR;
  174. return 0;
  175. }
  176. return 1;
  177. }
  178. int tls_parse_ctos_sig_algs(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  179. size_t chainidx, int *al)
  180. {
  181. PACKET supported_sig_algs;
  182. if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)
  183. || PACKET_remaining(&supported_sig_algs) == 0) {
  184. *al = SSL_AD_DECODE_ERROR;
  185. return 0;
  186. }
  187. if (!s->hit && !tls1_save_sigalgs(s, &supported_sig_algs)) {
  188. *al = TLS1_AD_DECODE_ERROR;
  189. return 0;
  190. }
  191. return 1;
  192. }
  193. #ifndef OPENSSL_NO_OCSP
  194. int tls_parse_ctos_status_request(SSL *s, PACKET *pkt, unsigned int context,
  195. X509 *x, size_t chainidx, int *al)
  196. {
  197. PACKET responder_id_list, exts;
  198. /* Not defined if we get one of these in a client Certificate */
  199. if (x != NULL)
  200. return 1;
  201. if (!PACKET_get_1(pkt, (unsigned int *)&s->ext.status_type)) {
  202. *al = SSL_AD_DECODE_ERROR;
  203. return 0;
  204. }
  205. if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) {
  206. /*
  207. * We don't know what to do with any other type so ignore it.
  208. */
  209. s->ext.status_type = TLSEXT_STATUSTYPE_nothing;
  210. return 1;
  211. }
  212. if (!PACKET_get_length_prefixed_2 (pkt, &responder_id_list)) {
  213. *al = SSL_AD_DECODE_ERROR;
  214. return 0;
  215. }
  216. /*
  217. * We remove any OCSP_RESPIDs from a previous handshake
  218. * to prevent unbounded memory growth - CVE-2016-6304
  219. */
  220. sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);
  221. if (PACKET_remaining(&responder_id_list) > 0) {
  222. s->ext.ocsp.ids = sk_OCSP_RESPID_new_null();
  223. if (s->ext.ocsp.ids == NULL) {
  224. *al = SSL_AD_INTERNAL_ERROR;
  225. return 0;
  226. }
  227. } else {
  228. s->ext.ocsp.ids = NULL;
  229. }
  230. while (PACKET_remaining(&responder_id_list) > 0) {
  231. OCSP_RESPID *id;
  232. PACKET responder_id;
  233. const unsigned char *id_data;
  234. if (!PACKET_get_length_prefixed_2(&responder_id_list, &responder_id)
  235. || PACKET_remaining(&responder_id) == 0) {
  236. *al = SSL_AD_DECODE_ERROR;
  237. return 0;
  238. }
  239. id_data = PACKET_data(&responder_id);
  240. /* TODO(size_t): Convert d2i_* to size_t */
  241. id = d2i_OCSP_RESPID(NULL, &id_data,
  242. (int)PACKET_remaining(&responder_id));
  243. if (id == NULL) {
  244. *al = SSL_AD_DECODE_ERROR;
  245. return 0;
  246. }
  247. if (id_data != PACKET_end(&responder_id)) {
  248. OCSP_RESPID_free(id);
  249. *al = SSL_AD_DECODE_ERROR;
  250. return 0;
  251. }
  252. if (!sk_OCSP_RESPID_push(s->ext.ocsp.ids, id)) {
  253. OCSP_RESPID_free(id);
  254. *al = SSL_AD_INTERNAL_ERROR;
  255. return 0;
  256. }
  257. }
  258. /* Read in request_extensions */
  259. if (!PACKET_as_length_prefixed_2(pkt, &exts)) {
  260. *al = SSL_AD_DECODE_ERROR;
  261. return 0;
  262. }
  263. if (PACKET_remaining(&exts) > 0) {
  264. const unsigned char *ext_data = PACKET_data(&exts);
  265. sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts,
  266. X509_EXTENSION_free);
  267. s->ext.ocsp.exts =
  268. d2i_X509_EXTENSIONS(NULL, &ext_data, (int)PACKET_remaining(&exts));
  269. if (s->ext.ocsp.exts == NULL || ext_data != PACKET_end(&exts)) {
  270. *al = SSL_AD_DECODE_ERROR;
  271. return 0;
  272. }
  273. }
  274. return 1;
  275. }
  276. #endif
  277. #ifndef OPENSSL_NO_NEXTPROTONEG
  278. int tls_parse_ctos_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  279. size_t chainidx, int *al)
  280. {
  281. /*
  282. * We shouldn't accept this extension on a
  283. * renegotiation.
  284. */
  285. if (SSL_IS_FIRST_HANDSHAKE(s))
  286. s->s3->npn_seen = 1;
  287. return 1;
  288. }
  289. #endif
  290. /*
  291. * Save the ALPN extension in a ClientHello.|pkt| holds the contents of the ALPN
  292. * extension, not including type and length. |al| is a pointer to the alert
  293. * value to send in the event of a failure. Returns: 1 on success, 0 on error.
  294. */
  295. int tls_parse_ctos_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  296. size_t chainidx, int *al)
  297. {
  298. PACKET protocol_list, save_protocol_list, protocol;
  299. if (!SSL_IS_FIRST_HANDSHAKE(s))
  300. return 1;
  301. if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)
  302. || PACKET_remaining(&protocol_list) < 2) {
  303. *al = SSL_AD_DECODE_ERROR;
  304. return 0;
  305. }
  306. save_protocol_list = protocol_list;
  307. do {
  308. /* Protocol names can't be empty. */
  309. if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)
  310. || PACKET_remaining(&protocol) == 0) {
  311. *al = SSL_AD_DECODE_ERROR;
  312. return 0;
  313. }
  314. } while (PACKET_remaining(&protocol_list) != 0);
  315. OPENSSL_free(s->s3->alpn_proposed);
  316. s->s3->alpn_proposed = NULL;
  317. s->s3->alpn_proposed_len = 0;
  318. if (!PACKET_memdup(&save_protocol_list,
  319. &s->s3->alpn_proposed, &s->s3->alpn_proposed_len)) {
  320. *al = TLS1_AD_INTERNAL_ERROR;
  321. return 0;
  322. }
  323. return 1;
  324. }
  325. #ifndef OPENSSL_NO_SRTP
  326. int tls_parse_ctos_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  327. size_t chainidx, int *al)
  328. {
  329. STACK_OF(SRTP_PROTECTION_PROFILE) *srvr;
  330. unsigned int ct, mki_len, id;
  331. int i, srtp_pref;
  332. PACKET subpkt;
  333. /* Ignore this if we have no SRTP profiles */
  334. if (SSL_get_srtp_profiles(s) == NULL)
  335. return 1;
  336. /* Pull off the length of the cipher suite list and check it is even */
  337. if (!PACKET_get_net_2(pkt, &ct) || (ct & 1) != 0
  338. || !PACKET_get_sub_packet(pkt, &subpkt, ct)) {
  339. SSLerr(SSL_F_TLS_PARSE_CTOS_USE_SRTP,
  340. SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  341. *al = SSL_AD_DECODE_ERROR;
  342. return 0;
  343. }
  344. srvr = SSL_get_srtp_profiles(s);
  345. s->srtp_profile = NULL;
  346. /* Search all profiles for a match initially */
  347. srtp_pref = sk_SRTP_PROTECTION_PROFILE_num(srvr);
  348. while (PACKET_remaining(&subpkt)) {
  349. if (!PACKET_get_net_2(&subpkt, &id)) {
  350. SSLerr(SSL_F_TLS_PARSE_CTOS_USE_SRTP,
  351. SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  352. *al = SSL_AD_DECODE_ERROR;
  353. return 0;
  354. }
  355. /*
  356. * Only look for match in profiles of higher preference than
  357. * current match.
  358. * If no profiles have been have been configured then this
  359. * does nothing.
  360. */
  361. for (i = 0; i < srtp_pref; i++) {
  362. SRTP_PROTECTION_PROFILE *sprof =
  363. sk_SRTP_PROTECTION_PROFILE_value(srvr, i);
  364. if (sprof->id == id) {
  365. s->srtp_profile = sprof;
  366. srtp_pref = i;
  367. break;
  368. }
  369. }
  370. }
  371. /* Now extract the MKI value as a sanity check, but discard it for now */
  372. if (!PACKET_get_1(pkt, &mki_len)) {
  373. SSLerr(SSL_F_TLS_PARSE_CTOS_USE_SRTP,
  374. SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  375. *al = SSL_AD_DECODE_ERROR;
  376. return 0;
  377. }
  378. if (!PACKET_forward(pkt, mki_len)
  379. || PACKET_remaining(pkt)) {
  380. SSLerr(SSL_F_TLS_PARSE_CTOS_USE_SRTP, SSL_R_BAD_SRTP_MKI_VALUE);
  381. *al = SSL_AD_DECODE_ERROR;
  382. return 0;
  383. }
  384. return 1;
  385. }
  386. #endif
  387. int tls_parse_ctos_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  388. size_t chainidx, int *al)
  389. {
  390. if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC))
  391. s->ext.use_etm = 1;
  392. return 1;
  393. }
  394. /*
  395. * Process a psk_kex_modes extension received in the ClientHello. |pkt| contains
  396. * the raw PACKET data for the extension. Returns 1 on success or 0 on failure.
  397. * If a failure occurs then |*al| is set to an appropriate alert value.
  398. */
  399. int tls_parse_ctos_psk_kex_modes(SSL *s, PACKET *pkt, unsigned int context,
  400. X509 *x, size_t chainidx, int *al)
  401. {
  402. #ifndef OPENSSL_NO_TLS1_3
  403. PACKET psk_kex_modes;
  404. unsigned int mode;
  405. if (!PACKET_as_length_prefixed_1(pkt, &psk_kex_modes)
  406. || PACKET_remaining(&psk_kex_modes) == 0) {
  407. *al = SSL_AD_DECODE_ERROR;
  408. return 0;
  409. }
  410. while (PACKET_get_1(&psk_kex_modes, &mode)) {
  411. if (mode == TLSEXT_KEX_MODE_KE_DHE)
  412. s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE_DHE;
  413. else if (mode == TLSEXT_KEX_MODE_KE)
  414. s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE;
  415. }
  416. #endif
  417. return 1;
  418. }
  419. /*
  420. * Process a key_share extension received in the ClientHello. |pkt| contains
  421. * the raw PACKET data for the extension. Returns 1 on success or 0 on failure.
  422. * If a failure occurs then |*al| is set to an appropriate alert value.
  423. */
  424. int tls_parse_ctos_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  425. size_t chainidx, int *al)
  426. {
  427. #ifndef OPENSSL_NO_TLS1_3
  428. unsigned int group_id;
  429. PACKET key_share_list, encoded_pt;
  430. const unsigned char *clntcurves, *srvrcurves;
  431. size_t clnt_num_curves, srvr_num_curves;
  432. int group_nid, found = 0;
  433. unsigned int curve_flags;
  434. if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0)
  435. return 1;
  436. /* Sanity check */
  437. if (s->s3->peer_tmp != NULL) {
  438. *al = SSL_AD_INTERNAL_ERROR;
  439. SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  440. return 0;
  441. }
  442. if (!PACKET_as_length_prefixed_2(pkt, &key_share_list)) {
  443. *al = SSL_AD_HANDSHAKE_FAILURE;
  444. SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, SSL_R_LENGTH_MISMATCH);
  445. return 0;
  446. }
  447. /* Get our list of supported curves */
  448. if (!tls1_get_curvelist(s, 0, &srvrcurves, &srvr_num_curves)) {
  449. *al = SSL_AD_INTERNAL_ERROR;
  450. SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  451. return 0;
  452. }
  453. /*
  454. * Get the clients list of supported curves.
  455. * TODO(TLS1.3): We should validate that we actually received
  456. * supported_groups!
  457. */
  458. if (!tls1_get_curvelist(s, 1, &clntcurves, &clnt_num_curves)) {
  459. *al = SSL_AD_INTERNAL_ERROR;
  460. SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  461. return 0;
  462. }
  463. while (PACKET_remaining(&key_share_list) > 0) {
  464. if (!PACKET_get_net_2(&key_share_list, &group_id)
  465. || !PACKET_get_length_prefixed_2(&key_share_list, &encoded_pt)
  466. || PACKET_remaining(&encoded_pt) == 0) {
  467. *al = SSL_AD_HANDSHAKE_FAILURE;
  468. SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE,
  469. SSL_R_LENGTH_MISMATCH);
  470. return 0;
  471. }
  472. /*
  473. * If we already found a suitable key_share we loop through the
  474. * rest to verify the structure, but don't process them.
  475. */
  476. if (found)
  477. continue;
  478. /* Check if this share is in supported_groups sent from client */
  479. if (!check_in_list(s, group_id, clntcurves, clnt_num_curves, 0)) {
  480. *al = SSL_AD_HANDSHAKE_FAILURE;
  481. SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
  482. return 0;
  483. }
  484. /* Check if this share is for a group we can use */
  485. if (!check_in_list(s, group_id, srvrcurves, srvr_num_curves, 1)) {
  486. /* Share not suitable */
  487. continue;
  488. }
  489. group_nid = tls1_ec_curve_id2nid(group_id, &curve_flags);
  490. if (group_nid == 0) {
  491. *al = SSL_AD_INTERNAL_ERROR;
  492. SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE,
  493. SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
  494. return 0;
  495. }
  496. if ((curve_flags & TLS_CURVE_TYPE) == TLS_CURVE_CUSTOM) {
  497. /* Can happen for some curves, e.g. X25519 */
  498. EVP_PKEY *key = EVP_PKEY_new();
  499. if (key == NULL || !EVP_PKEY_set_type(key, group_nid)) {
  500. *al = SSL_AD_INTERNAL_ERROR;
  501. SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, ERR_R_EVP_LIB);
  502. EVP_PKEY_free(key);
  503. return 0;
  504. }
  505. s->s3->peer_tmp = key;
  506. } else {
  507. /* Set up EVP_PKEY with named curve as parameters */
  508. EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
  509. if (pctx == NULL
  510. || EVP_PKEY_paramgen_init(pctx) <= 0
  511. || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
  512. group_nid) <= 0
  513. || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) {
  514. *al = SSL_AD_INTERNAL_ERROR;
  515. SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, ERR_R_EVP_LIB);
  516. EVP_PKEY_CTX_free(pctx);
  517. return 0;
  518. }
  519. EVP_PKEY_CTX_free(pctx);
  520. pctx = NULL;
  521. }
  522. s->s3->group_id = group_id;
  523. if (!EVP_PKEY_set1_tls_encodedpoint(s->s3->peer_tmp,
  524. PACKET_data(&encoded_pt),
  525. PACKET_remaining(&encoded_pt))) {
  526. *al = SSL_AD_DECODE_ERROR;
  527. SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, SSL_R_BAD_ECPOINT);
  528. return 0;
  529. }
  530. found = 1;
  531. }
  532. #endif
  533. return 1;
  534. }
  535. #ifndef OPENSSL_NO_EC
  536. int tls_parse_ctos_supported_groups(SSL *s, PACKET *pkt, unsigned int context,
  537. X509 *x, size_t chainidx, int *al)
  538. {
  539. PACKET supported_groups_list;
  540. /* Each group is 2 bytes and we must have at least 1. */
  541. if (!PACKET_as_length_prefixed_2(pkt, &supported_groups_list)
  542. || PACKET_remaining(&supported_groups_list) == 0
  543. || (PACKET_remaining(&supported_groups_list) % 2) != 0) {
  544. *al = SSL_AD_DECODE_ERROR;
  545. return 0;
  546. }
  547. OPENSSL_free(s->session->ext.supportedgroups);
  548. s->session->ext.supportedgroups = NULL;
  549. s->session->ext.supportedgroups_len = 0;
  550. if (!PACKET_memdup(&supported_groups_list,
  551. &s->session->ext.supportedgroups,
  552. &s->session->ext.supportedgroups_len)) {
  553. *al = SSL_AD_DECODE_ERROR;
  554. return 0;
  555. }
  556. return 1;
  557. }
  558. #endif
  559. int tls_parse_ctos_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  560. size_t chainidx, int *al)
  561. {
  562. /* The extension must always be empty */
  563. if (PACKET_remaining(pkt) != 0) {
  564. *al = SSL_AD_DECODE_ERROR;
  565. return 0;
  566. }
  567. s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
  568. return 1;
  569. }
  570. int tls_parse_ctos_early_data(SSL *s, PACKET *pkt, unsigned int context,
  571. X509 *x, size_t chainidx, int *al)
  572. {
  573. if (PACKET_remaining(pkt) != 0) {
  574. *al = SSL_AD_DECODE_ERROR;
  575. return 0;
  576. }
  577. return 1;
  578. }
  579. int tls_parse_ctos_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  580. size_t chainidx, int *al)
  581. {
  582. PACKET identities, binders, binder;
  583. size_t binderoffset, hashsize;
  584. SSL_SESSION *sess = NULL;
  585. unsigned int id, i;
  586. const EVP_MD *md = NULL;
  587. uint32_t ticket_age = 0, now, agesec, agems;
  588. /*
  589. * If we have no PSK kex mode that we recognise then we can't resume so
  590. * ignore this extension
  591. */
  592. if ((s->ext.psk_kex_mode
  593. & (TLSEXT_KEX_MODE_FLAG_KE | TLSEXT_KEX_MODE_FLAG_KE_DHE)) == 0)
  594. return 1;
  595. if (!PACKET_get_length_prefixed_2(pkt, &identities)) {
  596. *al = SSL_AD_DECODE_ERROR;
  597. return 0;
  598. }
  599. for (id = 0; PACKET_remaining(&identities) != 0; id++) {
  600. PACKET identity;
  601. unsigned long ticket_agel;
  602. int ret;
  603. if (!PACKET_get_length_prefixed_2(&identities, &identity)
  604. || !PACKET_get_net_4(&identities, &ticket_agel)) {
  605. *al = SSL_AD_DECODE_ERROR;
  606. return 0;
  607. }
  608. ticket_age = (uint32_t)ticket_agel;
  609. ret = tls_decrypt_ticket(s, PACKET_data(&identity),
  610. PACKET_remaining(&identity), NULL, 0, &sess);
  611. if (ret == TICKET_FATAL_ERR_MALLOC || ret == TICKET_FATAL_ERR_OTHER) {
  612. *al = SSL_AD_INTERNAL_ERROR;
  613. return 0;
  614. }
  615. if (ret == TICKET_NO_DECRYPT)
  616. continue;
  617. md = ssl_md(sess->cipher->algorithm2);
  618. if (md == NULL) {
  619. /*
  620. * Don't recognise this cipher so we can't use the session.
  621. * Ignore it
  622. */
  623. SSL_SESSION_free(sess);
  624. sess = NULL;
  625. continue;
  626. }
  627. /*
  628. * TODO(TLS1.3): Somehow we need to handle the case of a ticket renewal.
  629. * Ignored for now
  630. */
  631. break;
  632. }
  633. if (sess == NULL)
  634. return 1;
  635. binderoffset = PACKET_data(pkt) - (const unsigned char *)s->init_buf->data;
  636. hashsize = EVP_MD_size(md);
  637. if (!PACKET_get_length_prefixed_2(pkt, &binders)) {
  638. *al = SSL_AD_DECODE_ERROR;
  639. goto err;
  640. }
  641. for (i = 0; i <= id; i++) {
  642. if (!PACKET_get_length_prefixed_1(&binders, &binder)) {
  643. *al = SSL_AD_DECODE_ERROR;
  644. goto err;
  645. }
  646. }
  647. if (PACKET_remaining(&binder) != hashsize
  648. || tls_psk_do_binder(s, md,
  649. (const unsigned char *)s->init_buf->data,
  650. binderoffset, PACKET_data(&binder), NULL,
  651. sess, 0) != 1) {
  652. *al = SSL_AD_DECODE_ERROR;
  653. SSLerr(SSL_F_TLS_PARSE_CTOS_PSK, ERR_R_INTERNAL_ERROR);
  654. goto err;
  655. }
  656. sess->ext.tick_identity = id;
  657. now = (uint32_t)time(NULL);
  658. agesec = now - (uint32_t)sess->time;
  659. agems = agesec * (uint32_t)1000;
  660. ticket_age -= sess->ext.tick_age_add;
  661. /*
  662. * For simplicity we do our age calculations in seconds. If the client does
  663. * it in ms then it could appear that their ticket age is longer than ours
  664. * (our ticket age calculation should always be slightly longer than the
  665. * client's due to the network latency). Therefore we add 1000ms to our age
  666. * calculation to adjust for rounding errors.
  667. */
  668. if (sess->timeout >= (long)agesec
  669. && agems / (uint32_t)1000 == agesec
  670. && ticket_age <= agems + 1000
  671. && ticket_age + TICKET_AGE_ALLOWANCE >= agems + 1000) {
  672. /*
  673. * Ticket age is within tolerance and not expired. We allow it for early
  674. * data
  675. */
  676. s->ext.early_data_ok = 1;
  677. }
  678. SSL_SESSION_free(s->session);
  679. s->session = sess;
  680. return 1;
  681. err:
  682. SSL_SESSION_free(sess);
  683. return 0;
  684. }
  685. /*
  686. * Add the server's renegotiation binding
  687. */
  688. int tls_construct_stoc_renegotiate(SSL *s, WPACKET *pkt, unsigned int context,
  689. X509 *x, size_t chainidx, int *al)
  690. {
  691. if (!s->s3->send_connection_binding)
  692. return 1;
  693. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
  694. || !WPACKET_start_sub_packet_u16(pkt)
  695. || !WPACKET_start_sub_packet_u8(pkt)
  696. || !WPACKET_memcpy(pkt, s->s3->previous_client_finished,
  697. s->s3->previous_client_finished_len)
  698. || !WPACKET_memcpy(pkt, s->s3->previous_server_finished,
  699. s->s3->previous_server_finished_len)
  700. || !WPACKET_close(pkt)
  701. || !WPACKET_close(pkt)) {
  702. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_RENEGOTIATE, ERR_R_INTERNAL_ERROR);
  703. return 0;
  704. }
  705. return 1;
  706. }
  707. int tls_construct_stoc_server_name(SSL *s, WPACKET *pkt, unsigned int context,
  708. X509 *x, size_t chainidx, int *al)
  709. {
  710. if (s->hit || s->servername_done != 1
  711. || s->session->ext.hostname == NULL)
  712. return 1;
  713. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
  714. || !WPACKET_put_bytes_u16(pkt, 0)) {
  715. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_SERVER_NAME, ERR_R_INTERNAL_ERROR);
  716. return 0;
  717. }
  718. return 1;
  719. }
  720. #ifndef OPENSSL_NO_EC
  721. int tls_construct_stoc_ec_pt_formats(SSL *s, WPACKET *pkt, unsigned int context,
  722. X509 *x, size_t chainidx, int *al)
  723. {
  724. unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  725. unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  726. int using_ecc = ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))
  727. && (s->session->ext.ecpointformats != NULL);
  728. const unsigned char *plist;
  729. size_t plistlen;
  730. if (!using_ecc)
  731. return 1;
  732. tls1_get_formatlist(s, &plist, &plistlen);
  733. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
  734. || !WPACKET_start_sub_packet_u16(pkt)
  735. || !WPACKET_sub_memcpy_u8(pkt, plist, plistlen)
  736. || !WPACKET_close(pkt)) {
  737. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
  738. return 0;
  739. }
  740. return 1;
  741. }
  742. #endif
  743. int tls_construct_stoc_session_ticket(SSL *s, WPACKET *pkt,
  744. unsigned int context, X509 *x,
  745. size_t chainidx, int *al)
  746. {
  747. if (!s->ext.ticket_expected || !tls_use_ticket(s)) {
  748. s->ext.ticket_expected = 0;
  749. return 1;
  750. }
  751. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
  752. || !WPACKET_put_bytes_u16(pkt, 0)) {
  753. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
  754. return 0;
  755. }
  756. return 1;
  757. }
  758. #ifndef OPENSSL_NO_OCSP
  759. int tls_construct_stoc_status_request(SSL *s, WPACKET *pkt,
  760. unsigned int context, X509 *x,
  761. size_t chainidx, int *al)
  762. {
  763. if (!s->ext.status_expected)
  764. return 1;
  765. if (SSL_IS_TLS13(s) && chainidx != 0)
  766. return 1;
  767. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
  768. || !WPACKET_start_sub_packet_u16(pkt)) {
  769. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
  770. return 0;
  771. }
  772. /*
  773. * In TLSv1.3 we include the certificate status itself. In <= TLSv1.2 we
  774. * send back an empty extension, with the certificate status appearing as a
  775. * separate message
  776. */
  777. if ((SSL_IS_TLS13(s) && !tls_construct_cert_status_body(s, pkt))
  778. || !WPACKET_close(pkt)) {
  779. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
  780. return 0;
  781. }
  782. return 1;
  783. }
  784. #endif
  785. #ifndef OPENSSL_NO_NEXTPROTONEG
  786. int tls_construct_stoc_next_proto_neg(SSL *s, WPACKET *pkt,
  787. unsigned int context, X509 *x,
  788. size_t chainidx, int *al)
  789. {
  790. const unsigned char *npa;
  791. unsigned int npalen;
  792. int ret;
  793. int npn_seen = s->s3->npn_seen;
  794. s->s3->npn_seen = 0;
  795. if (!npn_seen || s->ctx->ext.npn_advertised_cb == NULL)
  796. return 1;
  797. ret = s->ctx->ext.npn_advertised_cb(s, &npa, &npalen,
  798. s->ctx->ext.npn_advertised_cb_arg);
  799. if (ret == SSL_TLSEXT_ERR_OK) {
  800. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
  801. || !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) {
  802. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_NEXT_PROTO_NEG,
  803. ERR_R_INTERNAL_ERROR);
  804. return 0;
  805. }
  806. s->s3->npn_seen = 1;
  807. }
  808. return 1;
  809. }
  810. #endif
  811. int tls_construct_stoc_alpn(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  812. size_t chainidx, int *al)
  813. {
  814. if (s->s3->alpn_selected == NULL)
  815. return 1;
  816. if (!WPACKET_put_bytes_u16(pkt,
  817. TLSEXT_TYPE_application_layer_protocol_negotiation)
  818. || !WPACKET_start_sub_packet_u16(pkt)
  819. || !WPACKET_start_sub_packet_u16(pkt)
  820. || !WPACKET_sub_memcpy_u8(pkt, s->s3->alpn_selected,
  821. s->s3->alpn_selected_len)
  822. || !WPACKET_close(pkt)
  823. || !WPACKET_close(pkt)) {
  824. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_ALPN, ERR_R_INTERNAL_ERROR);
  825. return 0;
  826. }
  827. return 1;
  828. }
  829. #ifndef OPENSSL_NO_SRTP
  830. int tls_construct_stoc_use_srtp(SSL *s, WPACKET *pkt, unsigned int context,
  831. X509 *x, size_t chainidx, int *al)
  832. {
  833. if (s->srtp_profile == NULL)
  834. return 1;
  835. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
  836. || !WPACKET_start_sub_packet_u16(pkt)
  837. || !WPACKET_put_bytes_u16(pkt, 2)
  838. || !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id)
  839. || !WPACKET_put_bytes_u8(pkt, 0)
  840. || !WPACKET_close(pkt)) {
  841. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_USE_SRTP, ERR_R_INTERNAL_ERROR);
  842. return 0;
  843. }
  844. return 1;
  845. }
  846. #endif
  847. int tls_construct_stoc_etm(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  848. size_t chainidx, int *al)
  849. {
  850. if (!s->ext.use_etm)
  851. return 1;
  852. /*
  853. * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
  854. * for other cases too.
  855. */
  856. if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD
  857. || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4
  858. || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
  859. || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12) {
  860. s->ext.use_etm = 0;
  861. return 1;
  862. }
  863. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
  864. || !WPACKET_put_bytes_u16(pkt, 0)) {
  865. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_ETM, ERR_R_INTERNAL_ERROR);
  866. return 0;
  867. }
  868. return 1;
  869. }
  870. int tls_construct_stoc_ems(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  871. size_t chainidx, int *al)
  872. {
  873. if ((s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) == 0)
  874. return 1;
  875. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
  876. || !WPACKET_put_bytes_u16(pkt, 0)) {
  877. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_EMS, ERR_R_INTERNAL_ERROR);
  878. return 0;
  879. }
  880. return 1;
  881. }
  882. int tls_construct_stoc_key_share(SSL *s, WPACKET *pkt, unsigned int context,
  883. X509 *x, size_t chainidx, int *al)
  884. {
  885. #ifndef OPENSSL_NO_TLS1_3
  886. unsigned char *encodedPoint;
  887. size_t encoded_pt_len = 0;
  888. EVP_PKEY *ckey = s->s3->peer_tmp, *skey = NULL;
  889. if (ckey == NULL) {
  890. /* No key_share received from client */
  891. if (s->hello_retry_request) {
  892. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
  893. || !WPACKET_start_sub_packet_u16(pkt)
  894. || !WPACKET_put_bytes_u16(pkt, s->s3->group_id)
  895. || !WPACKET_close(pkt)) {
  896. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE,
  897. ERR_R_INTERNAL_ERROR);
  898. return 0;
  899. }
  900. return 1;
  901. }
  902. /* Must be resuming. */
  903. if (!s->hit || !tls13_generate_handshake_secret(s, NULL, 0)) {
  904. *al = SSL_AD_INTERNAL_ERROR;
  905. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  906. return 0;
  907. }
  908. return 1;
  909. }
  910. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
  911. || !WPACKET_start_sub_packet_u16(pkt)
  912. || !WPACKET_put_bytes_u16(pkt, s->s3->group_id)) {
  913. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  914. return 0;
  915. }
  916. skey = ssl_generate_pkey(ckey);
  917. if (skey == NULL) {
  918. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_MALLOC_FAILURE);
  919. return 0;
  920. }
  921. /* Generate encoding of server key */
  922. encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(skey, &encodedPoint);
  923. if (encoded_pt_len == 0) {
  924. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_EC_LIB);
  925. EVP_PKEY_free(skey);
  926. return 0;
  927. }
  928. if (!WPACKET_sub_memcpy_u16(pkt, encodedPoint, encoded_pt_len)
  929. || !WPACKET_close(pkt)) {
  930. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  931. EVP_PKEY_free(skey);
  932. OPENSSL_free(encodedPoint);
  933. return 0;
  934. }
  935. OPENSSL_free(encodedPoint);
  936. /* This causes the crypto state to be updated based on the derived keys */
  937. s->s3->tmp.pkey = skey;
  938. if (ssl_derive(s, skey, ckey, 1) == 0) {
  939. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  940. return 0;
  941. }
  942. #endif
  943. return 1;
  944. }
  945. int tls_construct_stoc_cryptopro_bug(SSL *s, WPACKET *pkt, unsigned int context,
  946. X509 *x, size_t chainidx, int *al)
  947. {
  948. const unsigned char cryptopro_ext[36] = {
  949. 0xfd, 0xe8, /* 65000 */
  950. 0x00, 0x20, /* 32 bytes length */
  951. 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
  952. 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
  953. 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
  954. 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
  955. };
  956. if (((s->s3->tmp.new_cipher->id & 0xFFFF) != 0x80
  957. && (s->s3->tmp.new_cipher->id & 0xFFFF) != 0x81)
  958. || (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG) == 0)
  959. return 1;
  960. if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) {
  961. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_CRYPTOPRO_BUG, ERR_R_INTERNAL_ERROR);
  962. return 0;
  963. }
  964. return 1;
  965. }
  966. int tls_construct_stoc_early_data(SSL *s, WPACKET *pkt, unsigned int context,
  967. X509 *x, size_t chainidx, int *al)
  968. {
  969. if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {
  970. if (s->max_early_data == 0)
  971. return 1;
  972. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
  973. || !WPACKET_start_sub_packet_u16(pkt)
  974. || !WPACKET_put_bytes_u32(pkt, s->max_early_data)
  975. || !WPACKET_close(pkt)) {
  976. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_EARLY_DATA, ERR_R_INTERNAL_ERROR);
  977. return 0;
  978. }
  979. return 1;
  980. }
  981. if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED)
  982. return 1;
  983. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
  984. || !WPACKET_start_sub_packet_u16(pkt)
  985. || !WPACKET_close(pkt)) {
  986. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_EARLY_DATA, ERR_R_INTERNAL_ERROR);
  987. return 0;
  988. }
  989. return 1;
  990. }
  991. int tls_construct_stoc_psk(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  992. size_t chainidx, int *al)
  993. {
  994. if (!s->hit)
  995. return 1;
  996. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)
  997. || !WPACKET_start_sub_packet_u16(pkt)
  998. || !WPACKET_put_bytes_u16(pkt, s->session->ext.tick_identity)
  999. || !WPACKET_close(pkt)) {
  1000. SSLerr(SSL_F_TLS_CONSTRUCT_STOC_PSK, ERR_R_INTERNAL_ERROR);
  1001. return 0;
  1002. }
  1003. return 1;
  1004. }