extensions_srvr.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981
  1. /*
  2. * Copyright 2016-2021 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_local.h"
  11. #include "statem_local.h"
  12. #include "internal/cryptlib.h"
  13. #define COOKIE_STATE_FORMAT_VERSION 0
  14. /*
  15. * 2 bytes for packet length, 2 bytes for format version, 2 bytes for
  16. * protocol version, 2 bytes for group id, 2 bytes for cipher id, 1 byte for
  17. * key_share present flag, 4 bytes for timestamp, 2 bytes for the hashlen,
  18. * EVP_MAX_MD_SIZE for transcript hash, 1 byte for app cookie length, app cookie
  19. * length bytes, SHA256_DIGEST_LENGTH bytes for the HMAC of the whole thing.
  20. */
  21. #define MAX_COOKIE_SIZE (2 + 2 + 2 + 2 + 2 + 1 + 4 + 2 + EVP_MAX_MD_SIZE + 1 \
  22. + SSL_COOKIE_LENGTH + SHA256_DIGEST_LENGTH)
  23. /*
  24. * Message header + 2 bytes for protocol version + number of random bytes +
  25. * + 1 byte for legacy session id length + number of bytes in legacy session id
  26. * + 2 bytes for ciphersuite + 1 byte for legacy compression
  27. * + 2 bytes for extension block length + 6 bytes for key_share extension
  28. * + 4 bytes for cookie extension header + the number of bytes in the cookie
  29. */
  30. #define MAX_HRR_SIZE (SSL3_HM_HEADER_LENGTH + 2 + SSL3_RANDOM_SIZE + 1 \
  31. + SSL_MAX_SSL_SESSION_ID_LENGTH + 2 + 1 + 2 + 6 + 4 \
  32. + MAX_COOKIE_SIZE)
  33. /*
  34. * Parse the client's renegotiation binding and abort if it's not right
  35. */
  36. int tls_parse_ctos_renegotiate(SSL *s, PACKET *pkt, unsigned int context,
  37. X509 *x, size_t chainidx)
  38. {
  39. unsigned int ilen;
  40. const unsigned char *data;
  41. /* Parse the length byte */
  42. if (!PACKET_get_1(pkt, &ilen)
  43. || !PACKET_get_bytes(pkt, &data, ilen)) {
  44. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_RENEGOTIATE,
  45. SSL_R_RENEGOTIATION_ENCODING_ERR);
  46. return 0;
  47. }
  48. /* Check that the extension matches */
  49. if (ilen != s->s3->previous_client_finished_len) {
  50. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_PARSE_CTOS_RENEGOTIATE,
  51. SSL_R_RENEGOTIATION_MISMATCH);
  52. return 0;
  53. }
  54. if (memcmp(data, s->s3->previous_client_finished,
  55. s->s3->previous_client_finished_len)) {
  56. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_PARSE_CTOS_RENEGOTIATE,
  57. SSL_R_RENEGOTIATION_MISMATCH);
  58. return 0;
  59. }
  60. s->s3->send_connection_binding = 1;
  61. return 1;
  62. }
  63. /*-
  64. * The servername extension is treated as follows:
  65. *
  66. * - Only the hostname type is supported with a maximum length of 255.
  67. * - The servername is rejected if too long or if it contains zeros,
  68. * in which case an fatal alert is generated.
  69. * - The servername field is maintained together with the session cache.
  70. * - When a session is resumed, the servername call back invoked in order
  71. * to allow the application to position itself to the right context.
  72. * - The servername is acknowledged if it is new for a session or when
  73. * it is identical to a previously used for the same session.
  74. * Applications can control the behaviour. They can at any time
  75. * set a 'desirable' servername for a new SSL object. This can be the
  76. * case for example with HTTPS when a Host: header field is received and
  77. * a renegotiation is requested. In this case, a possible servername
  78. * presented in the new client hello is only acknowledged if it matches
  79. * the value of the Host: field.
  80. * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
  81. * if they provide for changing an explicit servername context for the
  82. * session, i.e. when the session has been established with a servername
  83. * extension.
  84. * - On session reconnect, the servername extension may be absent.
  85. */
  86. int tls_parse_ctos_server_name(SSL *s, PACKET *pkt, unsigned int context,
  87. X509 *x, size_t chainidx)
  88. {
  89. unsigned int servname_type;
  90. PACKET sni, hostname;
  91. if (!PACKET_as_length_prefixed_2(pkt, &sni)
  92. /* ServerNameList must be at least 1 byte long. */
  93. || PACKET_remaining(&sni) == 0) {
  94. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_SERVER_NAME,
  95. SSL_R_BAD_EXTENSION);
  96. return 0;
  97. }
  98. /*
  99. * Although the intent was for server_name to be extensible, RFC 4366
  100. * was not clear about it; and so OpenSSL among other implementations,
  101. * always and only allows a 'host_name' name types.
  102. * RFC 6066 corrected the mistake but adding new name types
  103. * is nevertheless no longer feasible, so act as if no other
  104. * SNI types can exist, to simplify parsing.
  105. *
  106. * Also note that the RFC permits only one SNI value per type,
  107. * i.e., we can only have a single hostname.
  108. */
  109. if (!PACKET_get_1(&sni, &servname_type)
  110. || servname_type != TLSEXT_NAMETYPE_host_name
  111. || !PACKET_as_length_prefixed_2(&sni, &hostname)) {
  112. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_SERVER_NAME,
  113. SSL_R_BAD_EXTENSION);
  114. return 0;
  115. }
  116. /*
  117. * In TLSv1.2 and below the SNI is associated with the session. In TLSv1.3
  118. * we always use the SNI value from the handshake.
  119. */
  120. if (!s->hit || SSL_IS_TLS13(s)) {
  121. if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) {
  122. SSLfatal(s, SSL_AD_UNRECOGNIZED_NAME,
  123. SSL_F_TLS_PARSE_CTOS_SERVER_NAME,
  124. SSL_R_BAD_EXTENSION);
  125. return 0;
  126. }
  127. if (PACKET_contains_zero_byte(&hostname)) {
  128. SSLfatal(s, SSL_AD_UNRECOGNIZED_NAME,
  129. SSL_F_TLS_PARSE_CTOS_SERVER_NAME,
  130. SSL_R_BAD_EXTENSION);
  131. return 0;
  132. }
  133. /*
  134. * Store the requested SNI in the SSL as temporary storage.
  135. * If we accept it, it will get stored in the SSL_SESSION as well.
  136. */
  137. OPENSSL_free(s->ext.hostname);
  138. s->ext.hostname = NULL;
  139. if (!PACKET_strndup(&hostname, &s->ext.hostname)) {
  140. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_SERVER_NAME,
  141. ERR_R_INTERNAL_ERROR);
  142. return 0;
  143. }
  144. s->servername_done = 1;
  145. } else {
  146. /*
  147. * In TLSv1.2 and below we should check if the SNI is consistent between
  148. * the initial handshake and the resumption. In TLSv1.3 SNI is not
  149. * associated with the session.
  150. */
  151. /*
  152. * TODO(openssl-team): if the SNI doesn't match, we MUST
  153. * fall back to a full handshake.
  154. */
  155. s->servername_done = (s->session->ext.hostname != NULL)
  156. && PACKET_equal(&hostname, s->session->ext.hostname,
  157. strlen(s->session->ext.hostname));
  158. }
  159. return 1;
  160. }
  161. int tls_parse_ctos_maxfragmentlen(SSL *s, PACKET *pkt, unsigned int context,
  162. X509 *x, size_t chainidx)
  163. {
  164. unsigned int value;
  165. if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) {
  166. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_MAXFRAGMENTLEN,
  167. SSL_R_BAD_EXTENSION);
  168. return 0;
  169. }
  170. /* Received |value| should be a valid max-fragment-length code. */
  171. if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) {
  172. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  173. SSL_F_TLS_PARSE_CTOS_MAXFRAGMENTLEN,
  174. SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
  175. return 0;
  176. }
  177. /*
  178. * RFC 6066: The negotiated length applies for the duration of the session
  179. * including session resumptions.
  180. * We should receive the same code as in resumed session !
  181. */
  182. if (s->hit && s->session->ext.max_fragment_len_mode != value) {
  183. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  184. SSL_F_TLS_PARSE_CTOS_MAXFRAGMENTLEN,
  185. SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
  186. return 0;
  187. }
  188. /*
  189. * Store it in session, so it'll become binding for us
  190. * and we'll include it in a next Server Hello.
  191. */
  192. s->session->ext.max_fragment_len_mode = value;
  193. return 1;
  194. }
  195. #ifndef OPENSSL_NO_SRP
  196. int tls_parse_ctos_srp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  197. size_t chainidx)
  198. {
  199. PACKET srp_I;
  200. if (!PACKET_as_length_prefixed_1(pkt, &srp_I)
  201. || PACKET_contains_zero_byte(&srp_I)) {
  202. SSLfatal(s, SSL_AD_DECODE_ERROR,
  203. SSL_F_TLS_PARSE_CTOS_SRP,
  204. SSL_R_BAD_EXTENSION);
  205. return 0;
  206. }
  207. /*
  208. * TODO(openssl-team): currently, we re-authenticate the user
  209. * upon resumption. Instead, we MUST ignore the login.
  210. */
  211. if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) {
  212. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_SRP,
  213. ERR_R_INTERNAL_ERROR);
  214. return 0;
  215. }
  216. return 1;
  217. }
  218. #endif
  219. #ifndef OPENSSL_NO_EC
  220. int tls_parse_ctos_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context,
  221. X509 *x, size_t chainidx)
  222. {
  223. PACKET ec_point_format_list;
  224. if (!PACKET_as_length_prefixed_1(pkt, &ec_point_format_list)
  225. || PACKET_remaining(&ec_point_format_list) == 0) {
  226. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_EC_PT_FORMATS,
  227. SSL_R_BAD_EXTENSION);
  228. return 0;
  229. }
  230. if (!s->hit) {
  231. if (!PACKET_memdup(&ec_point_format_list,
  232. &s->ext.peer_ecpointformats,
  233. &s->ext.peer_ecpointformats_len)) {
  234. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  235. SSL_F_TLS_PARSE_CTOS_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
  236. return 0;
  237. }
  238. }
  239. return 1;
  240. }
  241. #endif /* OPENSSL_NO_EC */
  242. int tls_parse_ctos_session_ticket(SSL *s, PACKET *pkt, unsigned int context,
  243. X509 *x, size_t chainidx)
  244. {
  245. if (s->ext.session_ticket_cb &&
  246. !s->ext.session_ticket_cb(s, PACKET_data(pkt),
  247. PACKET_remaining(pkt),
  248. s->ext.session_ticket_cb_arg)) {
  249. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  250. SSL_F_TLS_PARSE_CTOS_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
  251. return 0;
  252. }
  253. return 1;
  254. }
  255. int tls_parse_ctos_sig_algs_cert(SSL *s, PACKET *pkt, unsigned int context,
  256. X509 *x, size_t chainidx)
  257. {
  258. PACKET supported_sig_algs;
  259. if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)
  260. || PACKET_remaining(&supported_sig_algs) == 0) {
  261. SSLfatal(s, SSL_AD_DECODE_ERROR,
  262. SSL_F_TLS_PARSE_CTOS_SIG_ALGS_CERT, SSL_R_BAD_EXTENSION);
  263. return 0;
  264. }
  265. if (!s->hit && !tls1_save_sigalgs(s, &supported_sig_algs, 1)) {
  266. SSLfatal(s, SSL_AD_DECODE_ERROR,
  267. SSL_F_TLS_PARSE_CTOS_SIG_ALGS_CERT, SSL_R_BAD_EXTENSION);
  268. return 0;
  269. }
  270. return 1;
  271. }
  272. int tls_parse_ctos_sig_algs(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  273. size_t chainidx)
  274. {
  275. PACKET supported_sig_algs;
  276. if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)
  277. || PACKET_remaining(&supported_sig_algs) == 0) {
  278. SSLfatal(s, SSL_AD_DECODE_ERROR,
  279. SSL_F_TLS_PARSE_CTOS_SIG_ALGS, SSL_R_BAD_EXTENSION);
  280. return 0;
  281. }
  282. if (!s->hit && !tls1_save_sigalgs(s, &supported_sig_algs, 0)) {
  283. SSLfatal(s, SSL_AD_DECODE_ERROR,
  284. SSL_F_TLS_PARSE_CTOS_SIG_ALGS, SSL_R_BAD_EXTENSION);
  285. return 0;
  286. }
  287. return 1;
  288. }
  289. #ifndef OPENSSL_NO_OCSP
  290. int tls_parse_ctos_status_request(SSL *s, PACKET *pkt, unsigned int context,
  291. X509 *x, size_t chainidx)
  292. {
  293. PACKET responder_id_list, exts;
  294. /* We ignore this in a resumption handshake */
  295. if (s->hit)
  296. return 1;
  297. /* Not defined if we get one of these in a client Certificate */
  298. if (x != NULL)
  299. return 1;
  300. if (!PACKET_get_1(pkt, (unsigned int *)&s->ext.status_type)) {
  301. SSLfatal(s, SSL_AD_DECODE_ERROR,
  302. SSL_F_TLS_PARSE_CTOS_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
  303. return 0;
  304. }
  305. if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) {
  306. /*
  307. * We don't know what to do with any other type so ignore it.
  308. */
  309. s->ext.status_type = TLSEXT_STATUSTYPE_nothing;
  310. return 1;
  311. }
  312. if (!PACKET_get_length_prefixed_2 (pkt, &responder_id_list)) {
  313. SSLfatal(s, SSL_AD_DECODE_ERROR,
  314. SSL_F_TLS_PARSE_CTOS_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
  315. return 0;
  316. }
  317. /*
  318. * We remove any OCSP_RESPIDs from a previous handshake
  319. * to prevent unbounded memory growth - CVE-2016-6304
  320. */
  321. sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);
  322. if (PACKET_remaining(&responder_id_list) > 0) {
  323. s->ext.ocsp.ids = sk_OCSP_RESPID_new_null();
  324. if (s->ext.ocsp.ids == NULL) {
  325. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  326. SSL_F_TLS_PARSE_CTOS_STATUS_REQUEST, ERR_R_MALLOC_FAILURE);
  327. return 0;
  328. }
  329. } else {
  330. s->ext.ocsp.ids = NULL;
  331. }
  332. while (PACKET_remaining(&responder_id_list) > 0) {
  333. OCSP_RESPID *id;
  334. PACKET responder_id;
  335. const unsigned char *id_data;
  336. if (!PACKET_get_length_prefixed_2(&responder_id_list, &responder_id)
  337. || PACKET_remaining(&responder_id) == 0) {
  338. SSLfatal(s, SSL_AD_DECODE_ERROR,
  339. SSL_F_TLS_PARSE_CTOS_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
  340. return 0;
  341. }
  342. id_data = PACKET_data(&responder_id);
  343. /* TODO(size_t): Convert d2i_* to size_t */
  344. id = d2i_OCSP_RESPID(NULL, &id_data,
  345. (int)PACKET_remaining(&responder_id));
  346. if (id == NULL) {
  347. SSLfatal(s, SSL_AD_DECODE_ERROR,
  348. SSL_F_TLS_PARSE_CTOS_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
  349. return 0;
  350. }
  351. if (id_data != PACKET_end(&responder_id)) {
  352. OCSP_RESPID_free(id);
  353. SSLfatal(s, SSL_AD_DECODE_ERROR,
  354. SSL_F_TLS_PARSE_CTOS_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
  355. return 0;
  356. }
  357. if (!sk_OCSP_RESPID_push(s->ext.ocsp.ids, id)) {
  358. OCSP_RESPID_free(id);
  359. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  360. SSL_F_TLS_PARSE_CTOS_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
  361. return 0;
  362. }
  363. }
  364. /* Read in request_extensions */
  365. if (!PACKET_as_length_prefixed_2(pkt, &exts)) {
  366. SSLfatal(s, SSL_AD_DECODE_ERROR,
  367. SSL_F_TLS_PARSE_CTOS_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
  368. return 0;
  369. }
  370. if (PACKET_remaining(&exts) > 0) {
  371. const unsigned char *ext_data = PACKET_data(&exts);
  372. sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts,
  373. X509_EXTENSION_free);
  374. s->ext.ocsp.exts =
  375. d2i_X509_EXTENSIONS(NULL, &ext_data, (int)PACKET_remaining(&exts));
  376. if (s->ext.ocsp.exts == NULL || ext_data != PACKET_end(&exts)) {
  377. SSLfatal(s, SSL_AD_DECODE_ERROR,
  378. SSL_F_TLS_PARSE_CTOS_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
  379. return 0;
  380. }
  381. }
  382. return 1;
  383. }
  384. #endif
  385. #ifndef OPENSSL_NO_NEXTPROTONEG
  386. int tls_parse_ctos_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  387. size_t chainidx)
  388. {
  389. /*
  390. * We shouldn't accept this extension on a
  391. * renegotiation.
  392. */
  393. if (SSL_IS_FIRST_HANDSHAKE(s))
  394. s->s3->npn_seen = 1;
  395. return 1;
  396. }
  397. #endif
  398. /*
  399. * Save the ALPN extension in a ClientHello.|pkt| holds the contents of the ALPN
  400. * extension, not including type and length. Returns: 1 on success, 0 on error.
  401. */
  402. int tls_parse_ctos_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  403. size_t chainidx)
  404. {
  405. PACKET protocol_list, save_protocol_list, protocol;
  406. if (!SSL_IS_FIRST_HANDSHAKE(s))
  407. return 1;
  408. if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)
  409. || PACKET_remaining(&protocol_list) < 2) {
  410. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_ALPN,
  411. SSL_R_BAD_EXTENSION);
  412. return 0;
  413. }
  414. save_protocol_list = protocol_list;
  415. do {
  416. /* Protocol names can't be empty. */
  417. if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)
  418. || PACKET_remaining(&protocol) == 0) {
  419. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_ALPN,
  420. SSL_R_BAD_EXTENSION);
  421. return 0;
  422. }
  423. } while (PACKET_remaining(&protocol_list) != 0);
  424. OPENSSL_free(s->s3->alpn_proposed);
  425. s->s3->alpn_proposed = NULL;
  426. s->s3->alpn_proposed_len = 0;
  427. if (!PACKET_memdup(&save_protocol_list,
  428. &s->s3->alpn_proposed, &s->s3->alpn_proposed_len)) {
  429. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_ALPN,
  430. ERR_R_INTERNAL_ERROR);
  431. return 0;
  432. }
  433. return 1;
  434. }
  435. #ifndef OPENSSL_NO_SRTP
  436. int tls_parse_ctos_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  437. size_t chainidx)
  438. {
  439. STACK_OF(SRTP_PROTECTION_PROFILE) *srvr;
  440. unsigned int ct, mki_len, id;
  441. int i, srtp_pref;
  442. PACKET subpkt;
  443. /* Ignore this if we have no SRTP profiles */
  444. if (SSL_get_srtp_profiles(s) == NULL)
  445. return 1;
  446. /* Pull off the length of the cipher suite list and check it is even */
  447. if (!PACKET_get_net_2(pkt, &ct) || (ct & 1) != 0
  448. || !PACKET_get_sub_packet(pkt, &subpkt, ct)) {
  449. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_USE_SRTP,
  450. SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  451. return 0;
  452. }
  453. srvr = SSL_get_srtp_profiles(s);
  454. s->srtp_profile = NULL;
  455. /* Search all profiles for a match initially */
  456. srtp_pref = sk_SRTP_PROTECTION_PROFILE_num(srvr);
  457. while (PACKET_remaining(&subpkt)) {
  458. if (!PACKET_get_net_2(&subpkt, &id)) {
  459. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_USE_SRTP,
  460. SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  461. return 0;
  462. }
  463. /*
  464. * Only look for match in profiles of higher preference than
  465. * current match.
  466. * If no profiles have been have been configured then this
  467. * does nothing.
  468. */
  469. for (i = 0; i < srtp_pref; i++) {
  470. SRTP_PROTECTION_PROFILE *sprof =
  471. sk_SRTP_PROTECTION_PROFILE_value(srvr, i);
  472. if (sprof->id == id) {
  473. s->srtp_profile = sprof;
  474. srtp_pref = i;
  475. break;
  476. }
  477. }
  478. }
  479. /* Now extract the MKI value as a sanity check, but discard it for now */
  480. if (!PACKET_get_1(pkt, &mki_len)) {
  481. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_USE_SRTP,
  482. SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  483. return 0;
  484. }
  485. if (!PACKET_forward(pkt, mki_len)
  486. || PACKET_remaining(pkt)) {
  487. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_USE_SRTP,
  488. SSL_R_BAD_SRTP_MKI_VALUE);
  489. return 0;
  490. }
  491. return 1;
  492. }
  493. #endif
  494. int tls_parse_ctos_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  495. size_t chainidx)
  496. {
  497. if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC))
  498. s->ext.use_etm = 1;
  499. return 1;
  500. }
  501. /*
  502. * Process a psk_kex_modes extension received in the ClientHello. |pkt| contains
  503. * the raw PACKET data for the extension. Returns 1 on success or 0 on failure.
  504. */
  505. int tls_parse_ctos_psk_kex_modes(SSL *s, PACKET *pkt, unsigned int context,
  506. X509 *x, size_t chainidx)
  507. {
  508. #ifndef OPENSSL_NO_TLS1_3
  509. PACKET psk_kex_modes;
  510. unsigned int mode;
  511. if (!PACKET_as_length_prefixed_1(pkt, &psk_kex_modes)
  512. || PACKET_remaining(&psk_kex_modes) == 0) {
  513. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_PSK_KEX_MODES,
  514. SSL_R_BAD_EXTENSION);
  515. return 0;
  516. }
  517. while (PACKET_get_1(&psk_kex_modes, &mode)) {
  518. if (mode == TLSEXT_KEX_MODE_KE_DHE)
  519. s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE_DHE;
  520. else if (mode == TLSEXT_KEX_MODE_KE
  521. && (s->options & SSL_OP_ALLOW_NO_DHE_KEX) != 0)
  522. s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE;
  523. }
  524. #endif
  525. return 1;
  526. }
  527. /*
  528. * Process a key_share extension received in the ClientHello. |pkt| contains
  529. * the raw PACKET data for the extension. Returns 1 on success or 0 on failure.
  530. */
  531. int tls_parse_ctos_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  532. size_t chainidx)
  533. {
  534. #ifndef OPENSSL_NO_TLS1_3
  535. unsigned int group_id;
  536. PACKET key_share_list, encoded_pt;
  537. const uint16_t *clntgroups, *srvrgroups;
  538. size_t clnt_num_groups, srvr_num_groups;
  539. int found = 0;
  540. if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0)
  541. return 1;
  542. /* Sanity check */
  543. if (s->s3->peer_tmp != NULL) {
  544. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_KEY_SHARE,
  545. ERR_R_INTERNAL_ERROR);
  546. return 0;
  547. }
  548. if (!PACKET_as_length_prefixed_2(pkt, &key_share_list)) {
  549. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_KEY_SHARE,
  550. SSL_R_LENGTH_MISMATCH);
  551. return 0;
  552. }
  553. /* Get our list of supported groups */
  554. tls1_get_supported_groups(s, &srvrgroups, &srvr_num_groups);
  555. /* Get the clients list of supported groups. */
  556. tls1_get_peer_groups(s, &clntgroups, &clnt_num_groups);
  557. if (clnt_num_groups == 0) {
  558. /*
  559. * This can only happen if the supported_groups extension was not sent,
  560. * because we verify that the length is non-zero when we process that
  561. * extension.
  562. */
  563. SSLfatal(s, SSL_AD_MISSING_EXTENSION, SSL_F_TLS_PARSE_CTOS_KEY_SHARE,
  564. SSL_R_MISSING_SUPPORTED_GROUPS_EXTENSION);
  565. return 0;
  566. }
  567. if (s->s3->group_id != 0 && PACKET_remaining(&key_share_list) == 0) {
  568. /*
  569. * If we set a group_id already, then we must have sent an HRR
  570. * requesting a new key_share. If we haven't got one then that is an
  571. * error
  572. */
  573. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_CTOS_KEY_SHARE,
  574. SSL_R_BAD_KEY_SHARE);
  575. return 0;
  576. }
  577. while (PACKET_remaining(&key_share_list) > 0) {
  578. if (!PACKET_get_net_2(&key_share_list, &group_id)
  579. || !PACKET_get_length_prefixed_2(&key_share_list, &encoded_pt)
  580. || PACKET_remaining(&encoded_pt) == 0) {
  581. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_KEY_SHARE,
  582. SSL_R_LENGTH_MISMATCH);
  583. return 0;
  584. }
  585. /*
  586. * If we already found a suitable key_share we loop through the
  587. * rest to verify the structure, but don't process them.
  588. */
  589. if (found)
  590. continue;
  591. /*
  592. * If we sent an HRR then the key_share sent back MUST be for the group
  593. * we requested, and must be the only key_share sent.
  594. */
  595. if (s->s3->group_id != 0
  596. && (group_id != s->s3->group_id
  597. || PACKET_remaining(&key_share_list) != 0)) {
  598. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  599. SSL_F_TLS_PARSE_CTOS_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
  600. return 0;
  601. }
  602. /* Check if this share is in supported_groups sent from client */
  603. if (!check_in_list(s, group_id, clntgroups, clnt_num_groups, 0)) {
  604. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  605. SSL_F_TLS_PARSE_CTOS_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
  606. return 0;
  607. }
  608. /* Check if this share is for a group we can use */
  609. if (!check_in_list(s, group_id, srvrgroups, srvr_num_groups, 1)) {
  610. /* Share not suitable */
  611. continue;
  612. }
  613. if ((s->s3->peer_tmp = ssl_generate_param_group(group_id)) == NULL) {
  614. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_KEY_SHARE,
  615. SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
  616. return 0;
  617. }
  618. s->s3->group_id = group_id;
  619. if (!EVP_PKEY_set1_tls_encodedpoint(s->s3->peer_tmp,
  620. PACKET_data(&encoded_pt),
  621. PACKET_remaining(&encoded_pt))) {
  622. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  623. SSL_F_TLS_PARSE_CTOS_KEY_SHARE, SSL_R_BAD_ECPOINT);
  624. return 0;
  625. }
  626. found = 1;
  627. }
  628. #endif
  629. return 1;
  630. }
  631. int tls_parse_ctos_cookie(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  632. size_t chainidx)
  633. {
  634. #ifndef OPENSSL_NO_TLS1_3
  635. unsigned int format, version, key_share, group_id;
  636. EVP_MD_CTX *hctx;
  637. EVP_PKEY *pkey;
  638. PACKET cookie, raw, chhash, appcookie;
  639. WPACKET hrrpkt;
  640. const unsigned char *data, *mdin, *ciphdata;
  641. unsigned char hmac[SHA256_DIGEST_LENGTH];
  642. unsigned char hrr[MAX_HRR_SIZE];
  643. size_t rawlen, hmaclen, hrrlen, ciphlen;
  644. unsigned long tm, now;
  645. /* Ignore any cookie if we're not set up to verify it */
  646. if (s->ctx->verify_stateless_cookie_cb == NULL
  647. || (s->s3->flags & TLS1_FLAGS_STATELESS) == 0)
  648. return 1;
  649. if (!PACKET_as_length_prefixed_2(pkt, &cookie)) {
  650. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  651. SSL_R_LENGTH_MISMATCH);
  652. return 0;
  653. }
  654. raw = cookie;
  655. data = PACKET_data(&raw);
  656. rawlen = PACKET_remaining(&raw);
  657. if (rawlen < SHA256_DIGEST_LENGTH
  658. || !PACKET_forward(&raw, rawlen - SHA256_DIGEST_LENGTH)) {
  659. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  660. SSL_R_LENGTH_MISMATCH);
  661. return 0;
  662. }
  663. mdin = PACKET_data(&raw);
  664. /* Verify the HMAC of the cookie */
  665. hctx = EVP_MD_CTX_create();
  666. pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
  667. s->session_ctx->ext.cookie_hmac_key,
  668. sizeof(s->session_ctx->ext
  669. .cookie_hmac_key));
  670. if (hctx == NULL || pkey == NULL) {
  671. EVP_MD_CTX_free(hctx);
  672. EVP_PKEY_free(pkey);
  673. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  674. ERR_R_MALLOC_FAILURE);
  675. return 0;
  676. }
  677. hmaclen = SHA256_DIGEST_LENGTH;
  678. if (EVP_DigestSignInit(hctx, NULL, EVP_sha256(), NULL, pkey) <= 0
  679. || EVP_DigestSign(hctx, hmac, &hmaclen, data,
  680. rawlen - SHA256_DIGEST_LENGTH) <= 0
  681. || hmaclen != SHA256_DIGEST_LENGTH) {
  682. EVP_MD_CTX_free(hctx);
  683. EVP_PKEY_free(pkey);
  684. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  685. ERR_R_INTERNAL_ERROR);
  686. return 0;
  687. }
  688. EVP_MD_CTX_free(hctx);
  689. EVP_PKEY_free(pkey);
  690. if (CRYPTO_memcmp(hmac, mdin, SHA256_DIGEST_LENGTH) != 0) {
  691. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_CTOS_COOKIE,
  692. SSL_R_COOKIE_MISMATCH);
  693. return 0;
  694. }
  695. if (!PACKET_get_net_2(&cookie, &format)) {
  696. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  697. SSL_R_LENGTH_MISMATCH);
  698. return 0;
  699. }
  700. /* Check the cookie format is something we recognise. Ignore it if not */
  701. if (format != COOKIE_STATE_FORMAT_VERSION)
  702. return 1;
  703. /*
  704. * The rest of these checks really shouldn't fail since we have verified the
  705. * HMAC above.
  706. */
  707. /* Check the version number is sane */
  708. if (!PACKET_get_net_2(&cookie, &version)) {
  709. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  710. SSL_R_LENGTH_MISMATCH);
  711. return 0;
  712. }
  713. if (version != TLS1_3_VERSION) {
  714. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_CTOS_COOKIE,
  715. SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
  716. return 0;
  717. }
  718. if (!PACKET_get_net_2(&cookie, &group_id)) {
  719. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  720. SSL_R_LENGTH_MISMATCH);
  721. return 0;
  722. }
  723. ciphdata = PACKET_data(&cookie);
  724. if (!PACKET_forward(&cookie, 2)) {
  725. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  726. SSL_R_LENGTH_MISMATCH);
  727. return 0;
  728. }
  729. if (group_id != s->s3->group_id
  730. || s->s3->tmp.new_cipher
  731. != ssl_get_cipher_by_char(s, ciphdata, 0)) {
  732. /*
  733. * We chose a different cipher or group id this time around to what is
  734. * in the cookie. Something must have changed.
  735. */
  736. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_CTOS_COOKIE,
  737. SSL_R_BAD_CIPHER);
  738. return 0;
  739. }
  740. if (!PACKET_get_1(&cookie, &key_share)
  741. || !PACKET_get_net_4(&cookie, &tm)
  742. || !PACKET_get_length_prefixed_2(&cookie, &chhash)
  743. || !PACKET_get_length_prefixed_1(&cookie, &appcookie)
  744. || PACKET_remaining(&cookie) != SHA256_DIGEST_LENGTH) {
  745. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  746. SSL_R_LENGTH_MISMATCH);
  747. return 0;
  748. }
  749. /* We tolerate a cookie age of up to 10 minutes (= 60 * 10 seconds) */
  750. now = (unsigned long)time(NULL);
  751. if (tm > now || (now - tm) > 600) {
  752. /* Cookie is stale. Ignore it */
  753. return 1;
  754. }
  755. /* Verify the app cookie */
  756. if (s->ctx->verify_stateless_cookie_cb(s, PACKET_data(&appcookie),
  757. PACKET_remaining(&appcookie)) == 0) {
  758. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_CTOS_COOKIE,
  759. SSL_R_COOKIE_MISMATCH);
  760. return 0;
  761. }
  762. /*
  763. * Reconstruct the HRR that we would have sent in response to the original
  764. * ClientHello so we can add it to the transcript hash.
  765. * Note: This won't work with custom HRR extensions
  766. */
  767. if (!WPACKET_init_static_len(&hrrpkt, hrr, sizeof(hrr), 0)) {
  768. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  769. ERR_R_INTERNAL_ERROR);
  770. return 0;
  771. }
  772. if (!WPACKET_put_bytes_u8(&hrrpkt, SSL3_MT_SERVER_HELLO)
  773. || !WPACKET_start_sub_packet_u24(&hrrpkt)
  774. || !WPACKET_put_bytes_u16(&hrrpkt, TLS1_2_VERSION)
  775. || !WPACKET_memcpy(&hrrpkt, hrrrandom, SSL3_RANDOM_SIZE)
  776. || !WPACKET_sub_memcpy_u8(&hrrpkt, s->tmp_session_id,
  777. s->tmp_session_id_len)
  778. || !s->method->put_cipher_by_char(s->s3->tmp.new_cipher, &hrrpkt,
  779. &ciphlen)
  780. || !WPACKET_put_bytes_u8(&hrrpkt, 0)
  781. || !WPACKET_start_sub_packet_u16(&hrrpkt)) {
  782. WPACKET_cleanup(&hrrpkt);
  783. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  784. ERR_R_INTERNAL_ERROR);
  785. return 0;
  786. }
  787. if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_supported_versions)
  788. || !WPACKET_start_sub_packet_u16(&hrrpkt)
  789. || !WPACKET_put_bytes_u16(&hrrpkt, s->version)
  790. || !WPACKET_close(&hrrpkt)) {
  791. WPACKET_cleanup(&hrrpkt);
  792. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  793. ERR_R_INTERNAL_ERROR);
  794. return 0;
  795. }
  796. if (key_share) {
  797. if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_key_share)
  798. || !WPACKET_start_sub_packet_u16(&hrrpkt)
  799. || !WPACKET_put_bytes_u16(&hrrpkt, s->s3->group_id)
  800. || !WPACKET_close(&hrrpkt)) {
  801. WPACKET_cleanup(&hrrpkt);
  802. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  803. ERR_R_INTERNAL_ERROR);
  804. return 0;
  805. }
  806. }
  807. if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_cookie)
  808. || !WPACKET_start_sub_packet_u16(&hrrpkt)
  809. || !WPACKET_sub_memcpy_u16(&hrrpkt, data, rawlen)
  810. || !WPACKET_close(&hrrpkt) /* cookie extension */
  811. || !WPACKET_close(&hrrpkt) /* extension block */
  812. || !WPACKET_close(&hrrpkt) /* message */
  813. || !WPACKET_get_total_written(&hrrpkt, &hrrlen)
  814. || !WPACKET_finish(&hrrpkt)) {
  815. WPACKET_cleanup(&hrrpkt);
  816. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  817. ERR_R_INTERNAL_ERROR);
  818. return 0;
  819. }
  820. /* Reconstruct the transcript hash */
  821. if (!create_synthetic_message_hash(s, PACKET_data(&chhash),
  822. PACKET_remaining(&chhash), hrr,
  823. hrrlen)) {
  824. /* SSLfatal() already called */
  825. return 0;
  826. }
  827. /* Act as if this ClientHello came after a HelloRetryRequest */
  828. s->hello_retry_request = 1;
  829. s->ext.cookieok = 1;
  830. #endif
  831. return 1;
  832. }
  833. #ifndef OPENSSL_NO_EC
  834. int tls_parse_ctos_supported_groups(SSL *s, PACKET *pkt, unsigned int context,
  835. X509 *x, size_t chainidx)
  836. {
  837. PACKET supported_groups_list;
  838. /* Each group is 2 bytes and we must have at least 1. */
  839. if (!PACKET_as_length_prefixed_2(pkt, &supported_groups_list)
  840. || PACKET_remaining(&supported_groups_list) == 0
  841. || (PACKET_remaining(&supported_groups_list) % 2) != 0) {
  842. SSLfatal(s, SSL_AD_DECODE_ERROR,
  843. SSL_F_TLS_PARSE_CTOS_SUPPORTED_GROUPS, SSL_R_BAD_EXTENSION);
  844. return 0;
  845. }
  846. if (!s->hit || SSL_IS_TLS13(s)) {
  847. OPENSSL_free(s->ext.peer_supportedgroups);
  848. s->ext.peer_supportedgroups = NULL;
  849. s->ext.peer_supportedgroups_len = 0;
  850. if (!tls1_save_u16(&supported_groups_list,
  851. &s->ext.peer_supportedgroups,
  852. &s->ext.peer_supportedgroups_len)) {
  853. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  854. SSL_F_TLS_PARSE_CTOS_SUPPORTED_GROUPS,
  855. ERR_R_INTERNAL_ERROR);
  856. return 0;
  857. }
  858. }
  859. return 1;
  860. }
  861. #endif
  862. int tls_parse_ctos_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  863. size_t chainidx)
  864. {
  865. /* The extension must always be empty */
  866. if (PACKET_remaining(pkt) != 0) {
  867. SSLfatal(s, SSL_AD_DECODE_ERROR,
  868. SSL_F_TLS_PARSE_CTOS_EMS, SSL_R_BAD_EXTENSION);
  869. return 0;
  870. }
  871. s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
  872. return 1;
  873. }
  874. int tls_parse_ctos_early_data(SSL *s, PACKET *pkt, unsigned int context,
  875. X509 *x, size_t chainidx)
  876. {
  877. if (PACKET_remaining(pkt) != 0) {
  878. SSLfatal(s, SSL_AD_DECODE_ERROR,
  879. SSL_F_TLS_PARSE_CTOS_EARLY_DATA, SSL_R_BAD_EXTENSION);
  880. return 0;
  881. }
  882. if (s->hello_retry_request != SSL_HRR_NONE) {
  883. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  884. SSL_F_TLS_PARSE_CTOS_EARLY_DATA, SSL_R_BAD_EXTENSION);
  885. return 0;
  886. }
  887. return 1;
  888. }
  889. static SSL_TICKET_STATUS tls_get_stateful_ticket(SSL *s, PACKET *tick,
  890. SSL_SESSION **sess)
  891. {
  892. SSL_SESSION *tmpsess = NULL;
  893. s->ext.ticket_expected = 1;
  894. switch (PACKET_remaining(tick)) {
  895. case 0:
  896. return SSL_TICKET_EMPTY;
  897. case SSL_MAX_SSL_SESSION_ID_LENGTH:
  898. break;
  899. default:
  900. return SSL_TICKET_NO_DECRYPT;
  901. }
  902. tmpsess = lookup_sess_in_cache(s, PACKET_data(tick),
  903. SSL_MAX_SSL_SESSION_ID_LENGTH);
  904. if (tmpsess == NULL)
  905. return SSL_TICKET_NO_DECRYPT;
  906. *sess = tmpsess;
  907. return SSL_TICKET_SUCCESS;
  908. }
  909. int tls_parse_ctos_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  910. size_t chainidx)
  911. {
  912. PACKET identities, binders, binder;
  913. size_t binderoffset, hashsize;
  914. SSL_SESSION *sess = NULL;
  915. unsigned int id, i, ext = 0;
  916. const EVP_MD *md = NULL;
  917. /*
  918. * If we have no PSK kex mode that we recognise then we can't resume so
  919. * ignore this extension
  920. */
  921. if ((s->ext.psk_kex_mode
  922. & (TLSEXT_KEX_MODE_FLAG_KE | TLSEXT_KEX_MODE_FLAG_KE_DHE)) == 0)
  923. return 1;
  924. if (!PACKET_get_length_prefixed_2(pkt, &identities)) {
  925. SSLfatal(s, SSL_AD_DECODE_ERROR,
  926. SSL_F_TLS_PARSE_CTOS_PSK, SSL_R_BAD_EXTENSION);
  927. return 0;
  928. }
  929. s->ext.ticket_expected = 0;
  930. for (id = 0; PACKET_remaining(&identities) != 0; id++) {
  931. PACKET identity;
  932. unsigned long ticket_agel;
  933. size_t idlen;
  934. if (!PACKET_get_length_prefixed_2(&identities, &identity)
  935. || !PACKET_get_net_4(&identities, &ticket_agel)) {
  936. SSLfatal(s, SSL_AD_DECODE_ERROR,
  937. SSL_F_TLS_PARSE_CTOS_PSK, SSL_R_BAD_EXTENSION);
  938. return 0;
  939. }
  940. idlen = PACKET_remaining(&identity);
  941. if (s->psk_find_session_cb != NULL
  942. && !s->psk_find_session_cb(s, PACKET_data(&identity), idlen,
  943. &sess)) {
  944. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  945. SSL_F_TLS_PARSE_CTOS_PSK, SSL_R_BAD_EXTENSION);
  946. return 0;
  947. }
  948. #ifndef OPENSSL_NO_PSK
  949. if(sess == NULL
  950. && s->psk_server_callback != NULL
  951. && idlen <= PSK_MAX_IDENTITY_LEN) {
  952. char *pskid = NULL;
  953. unsigned char pskdata[PSK_MAX_PSK_LEN];
  954. unsigned int pskdatalen;
  955. if (!PACKET_strndup(&identity, &pskid)) {
  956. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_PSK,
  957. ERR_R_INTERNAL_ERROR);
  958. return 0;
  959. }
  960. pskdatalen = s->psk_server_callback(s, pskid, pskdata,
  961. sizeof(pskdata));
  962. OPENSSL_free(pskid);
  963. if (pskdatalen > PSK_MAX_PSK_LEN) {
  964. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_PSK,
  965. ERR_R_INTERNAL_ERROR);
  966. return 0;
  967. } else if (pskdatalen > 0) {
  968. const SSL_CIPHER *cipher;
  969. const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 };
  970. /*
  971. * We found a PSK using an old style callback. We don't know
  972. * the digest so we default to SHA256 as per the TLSv1.3 spec
  973. */
  974. cipher = SSL_CIPHER_find(s, tls13_aes128gcmsha256_id);
  975. if (cipher == NULL) {
  976. OPENSSL_cleanse(pskdata, pskdatalen);
  977. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_PSK,
  978. ERR_R_INTERNAL_ERROR);
  979. return 0;
  980. }
  981. sess = SSL_SESSION_new();
  982. if (sess == NULL
  983. || !SSL_SESSION_set1_master_key(sess, pskdata,
  984. pskdatalen)
  985. || !SSL_SESSION_set_cipher(sess, cipher)
  986. || !SSL_SESSION_set_protocol_version(sess,
  987. TLS1_3_VERSION)) {
  988. OPENSSL_cleanse(pskdata, pskdatalen);
  989. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_PSK,
  990. ERR_R_INTERNAL_ERROR);
  991. goto err;
  992. }
  993. OPENSSL_cleanse(pskdata, pskdatalen);
  994. }
  995. }
  996. #endif /* OPENSSL_NO_PSK */
  997. if (sess != NULL) {
  998. /* We found a PSK */
  999. SSL_SESSION *sesstmp = ssl_session_dup(sess, 0);
  1000. if (sesstmp == NULL) {
  1001. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1002. SSL_F_TLS_PARSE_CTOS_PSK, ERR_R_INTERNAL_ERROR);
  1003. goto err;
  1004. }
  1005. SSL_SESSION_free(sess);
  1006. sess = sesstmp;
  1007. /*
  1008. * We've just been told to use this session for this context so
  1009. * make sure the sid_ctx matches up.
  1010. */
  1011. memcpy(sess->sid_ctx, s->sid_ctx, s->sid_ctx_length);
  1012. sess->sid_ctx_length = s->sid_ctx_length;
  1013. ext = 1;
  1014. if (id == 0)
  1015. s->ext.early_data_ok = 1;
  1016. s->ext.ticket_expected = 1;
  1017. } else {
  1018. uint32_t ticket_age = 0, now, agesec, agems;
  1019. int ret;
  1020. /*
  1021. * If we are using anti-replay protection then we behave as if
  1022. * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
  1023. * is no point in using full stateless tickets.
  1024. */
  1025. if ((s->options & SSL_OP_NO_TICKET) != 0
  1026. || (s->max_early_data > 0
  1027. && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))
  1028. ret = tls_get_stateful_ticket(s, &identity, &sess);
  1029. else
  1030. ret = tls_decrypt_ticket(s, PACKET_data(&identity),
  1031. PACKET_remaining(&identity), NULL, 0,
  1032. &sess);
  1033. if (ret == SSL_TICKET_EMPTY) {
  1034. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_PSK,
  1035. SSL_R_BAD_EXTENSION);
  1036. return 0;
  1037. }
  1038. if (ret == SSL_TICKET_FATAL_ERR_MALLOC
  1039. || ret == SSL_TICKET_FATAL_ERR_OTHER) {
  1040. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1041. SSL_F_TLS_PARSE_CTOS_PSK, ERR_R_INTERNAL_ERROR);
  1042. return 0;
  1043. }
  1044. if (ret == SSL_TICKET_NONE || ret == SSL_TICKET_NO_DECRYPT)
  1045. continue;
  1046. /* Check for replay */
  1047. if (s->max_early_data > 0
  1048. && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0
  1049. && !SSL_CTX_remove_session(s->session_ctx, sess)) {
  1050. SSL_SESSION_free(sess);
  1051. sess = NULL;
  1052. continue;
  1053. }
  1054. ticket_age = (uint32_t)ticket_agel;
  1055. now = (uint32_t)time(NULL);
  1056. agesec = now - (uint32_t)sess->time;
  1057. agems = agesec * (uint32_t)1000;
  1058. ticket_age -= sess->ext.tick_age_add;
  1059. /*
  1060. * For simplicity we do our age calculations in seconds. If the
  1061. * client does it in ms then it could appear that their ticket age
  1062. * is longer than ours (our ticket age calculation should always be
  1063. * slightly longer than the client's due to the network latency).
  1064. * Therefore we add 1000ms to our age calculation to adjust for
  1065. * rounding errors.
  1066. */
  1067. if (id == 0
  1068. && sess->timeout >= (long)agesec
  1069. && agems / (uint32_t)1000 == agesec
  1070. && ticket_age <= agems + 1000
  1071. && ticket_age + TICKET_AGE_ALLOWANCE >= agems + 1000) {
  1072. /*
  1073. * Ticket age is within tolerance and not expired. We allow it
  1074. * for early data
  1075. */
  1076. s->ext.early_data_ok = 1;
  1077. }
  1078. }
  1079. md = ssl_md(sess->cipher->algorithm2);
  1080. if (md != ssl_md(s->s3->tmp.new_cipher->algorithm2)) {
  1081. /* The ciphersuite is not compatible with this session. */
  1082. SSL_SESSION_free(sess);
  1083. sess = NULL;
  1084. s->ext.early_data_ok = 0;
  1085. s->ext.ticket_expected = 0;
  1086. continue;
  1087. }
  1088. break;
  1089. }
  1090. if (sess == NULL)
  1091. return 1;
  1092. binderoffset = PACKET_data(pkt) - (const unsigned char *)s->init_buf->data;
  1093. hashsize = EVP_MD_size(md);
  1094. if (!PACKET_get_length_prefixed_2(pkt, &binders)) {
  1095. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_PSK,
  1096. SSL_R_BAD_EXTENSION);
  1097. goto err;
  1098. }
  1099. for (i = 0; i <= id; i++) {
  1100. if (!PACKET_get_length_prefixed_1(&binders, &binder)) {
  1101. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_PSK,
  1102. SSL_R_BAD_EXTENSION);
  1103. goto err;
  1104. }
  1105. }
  1106. if (PACKET_remaining(&binder) != hashsize) {
  1107. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_PSK,
  1108. SSL_R_BAD_EXTENSION);
  1109. goto err;
  1110. }
  1111. if (tls_psk_do_binder(s, md, (const unsigned char *)s->init_buf->data,
  1112. binderoffset, PACKET_data(&binder), NULL, sess, 0,
  1113. ext) != 1) {
  1114. /* SSLfatal() already called */
  1115. goto err;
  1116. }
  1117. s->ext.tick_identity = id;
  1118. SSL_SESSION_free(s->session);
  1119. s->session = sess;
  1120. return 1;
  1121. err:
  1122. SSL_SESSION_free(sess);
  1123. return 0;
  1124. }
  1125. int tls_parse_ctos_post_handshake_auth(SSL *s, PACKET *pkt, unsigned int context,
  1126. X509 *x, size_t chainidx)
  1127. {
  1128. if (PACKET_remaining(pkt) != 0) {
  1129. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_POST_HANDSHAKE_AUTH,
  1130. SSL_R_POST_HANDSHAKE_AUTH_ENCODING_ERR);
  1131. return 0;
  1132. }
  1133. s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;
  1134. return 1;
  1135. }
  1136. /*
  1137. * Add the server's renegotiation binding
  1138. */
  1139. EXT_RETURN tls_construct_stoc_renegotiate(SSL *s, WPACKET *pkt,
  1140. unsigned int context, X509 *x,
  1141. size_t chainidx)
  1142. {
  1143. if (!s->s3->send_connection_binding)
  1144. return EXT_RETURN_NOT_SENT;
  1145. /* Still add this even if SSL_OP_NO_RENEGOTIATION is set */
  1146. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
  1147. || !WPACKET_start_sub_packet_u16(pkt)
  1148. || !WPACKET_start_sub_packet_u8(pkt)
  1149. || !WPACKET_memcpy(pkt, s->s3->previous_client_finished,
  1150. s->s3->previous_client_finished_len)
  1151. || !WPACKET_memcpy(pkt, s->s3->previous_server_finished,
  1152. s->s3->previous_server_finished_len)
  1153. || !WPACKET_close(pkt)
  1154. || !WPACKET_close(pkt)) {
  1155. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_RENEGOTIATE,
  1156. ERR_R_INTERNAL_ERROR);
  1157. return EXT_RETURN_FAIL;
  1158. }
  1159. return EXT_RETURN_SENT;
  1160. }
  1161. EXT_RETURN tls_construct_stoc_server_name(SSL *s, WPACKET *pkt,
  1162. unsigned int context, X509 *x,
  1163. size_t chainidx)
  1164. {
  1165. if (s->servername_done != 1)
  1166. return EXT_RETURN_NOT_SENT;
  1167. /*
  1168. * Prior to TLSv1.3 we ignore any SNI in the current handshake if resuming.
  1169. * We just use the servername from the initial handshake.
  1170. */
  1171. if (s->hit && !SSL_IS_TLS13(s))
  1172. return EXT_RETURN_NOT_SENT;
  1173. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
  1174. || !WPACKET_put_bytes_u16(pkt, 0)) {
  1175. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_SERVER_NAME,
  1176. ERR_R_INTERNAL_ERROR);
  1177. return EXT_RETURN_FAIL;
  1178. }
  1179. return EXT_RETURN_SENT;
  1180. }
  1181. /* Add/include the server's max fragment len extension into ServerHello */
  1182. EXT_RETURN tls_construct_stoc_maxfragmentlen(SSL *s, WPACKET *pkt,
  1183. unsigned int context, X509 *x,
  1184. size_t chainidx)
  1185. {
  1186. if (!USE_MAX_FRAGMENT_LENGTH_EXT(s->session))
  1187. return EXT_RETURN_NOT_SENT;
  1188. /*-
  1189. * 4 bytes for this extension type and extension length
  1190. * 1 byte for the Max Fragment Length code value.
  1191. */
  1192. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length)
  1193. || !WPACKET_start_sub_packet_u16(pkt)
  1194. || !WPACKET_put_bytes_u8(pkt, s->session->ext.max_fragment_len_mode)
  1195. || !WPACKET_close(pkt)) {
  1196. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1197. SSL_F_TLS_CONSTRUCT_STOC_MAXFRAGMENTLEN, ERR_R_INTERNAL_ERROR);
  1198. return EXT_RETURN_FAIL;
  1199. }
  1200. return EXT_RETURN_SENT;
  1201. }
  1202. #ifndef OPENSSL_NO_EC
  1203. EXT_RETURN tls_construct_stoc_ec_pt_formats(SSL *s, WPACKET *pkt,
  1204. unsigned int context, X509 *x,
  1205. size_t chainidx)
  1206. {
  1207. unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1208. unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1209. int using_ecc = ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))
  1210. && (s->ext.peer_ecpointformats != NULL);
  1211. const unsigned char *plist;
  1212. size_t plistlen;
  1213. if (!using_ecc)
  1214. return EXT_RETURN_NOT_SENT;
  1215. tls1_get_formatlist(s, &plist, &plistlen);
  1216. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
  1217. || !WPACKET_start_sub_packet_u16(pkt)
  1218. || !WPACKET_sub_memcpy_u8(pkt, plist, plistlen)
  1219. || !WPACKET_close(pkt)) {
  1220. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1221. SSL_F_TLS_CONSTRUCT_STOC_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
  1222. return EXT_RETURN_FAIL;
  1223. }
  1224. return EXT_RETURN_SENT;
  1225. }
  1226. #endif
  1227. #ifndef OPENSSL_NO_EC
  1228. EXT_RETURN tls_construct_stoc_supported_groups(SSL *s, WPACKET *pkt,
  1229. unsigned int context, X509 *x,
  1230. size_t chainidx)
  1231. {
  1232. const uint16_t *groups;
  1233. size_t numgroups, i, first = 1;
  1234. /* s->s3->group_id is non zero if we accepted a key_share */
  1235. if (s->s3->group_id == 0)
  1236. return EXT_RETURN_NOT_SENT;
  1237. /* Get our list of supported groups */
  1238. tls1_get_supported_groups(s, &groups, &numgroups);
  1239. if (numgroups == 0) {
  1240. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1241. SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_GROUPS, ERR_R_INTERNAL_ERROR);
  1242. return EXT_RETURN_FAIL;
  1243. }
  1244. /* Copy group ID if supported */
  1245. for (i = 0; i < numgroups; i++) {
  1246. uint16_t group = groups[i];
  1247. if (tls_curve_allowed(s, group, SSL_SECOP_CURVE_SUPPORTED)) {
  1248. if (first) {
  1249. /*
  1250. * Check if the client is already using our preferred group. If
  1251. * so we don't need to add this extension
  1252. */
  1253. if (s->s3->group_id == group)
  1254. return EXT_RETURN_NOT_SENT;
  1255. /* Add extension header */
  1256. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)
  1257. /* Sub-packet for supported_groups extension */
  1258. || !WPACKET_start_sub_packet_u16(pkt)
  1259. || !WPACKET_start_sub_packet_u16(pkt)) {
  1260. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1261. SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_GROUPS,
  1262. ERR_R_INTERNAL_ERROR);
  1263. return EXT_RETURN_FAIL;
  1264. }
  1265. first = 0;
  1266. }
  1267. if (!WPACKET_put_bytes_u16(pkt, group)) {
  1268. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1269. SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_GROUPS,
  1270. ERR_R_INTERNAL_ERROR);
  1271. return EXT_RETURN_FAIL;
  1272. }
  1273. }
  1274. }
  1275. if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
  1276. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1277. SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_GROUPS,
  1278. ERR_R_INTERNAL_ERROR);
  1279. return EXT_RETURN_FAIL;
  1280. }
  1281. return EXT_RETURN_SENT;
  1282. }
  1283. #endif
  1284. EXT_RETURN tls_construct_stoc_session_ticket(SSL *s, WPACKET *pkt,
  1285. unsigned int context, X509 *x,
  1286. size_t chainidx)
  1287. {
  1288. if (!s->ext.ticket_expected || !tls_use_ticket(s)) {
  1289. s->ext.ticket_expected = 0;
  1290. return EXT_RETURN_NOT_SENT;
  1291. }
  1292. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
  1293. || !WPACKET_put_bytes_u16(pkt, 0)) {
  1294. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1295. SSL_F_TLS_CONSTRUCT_STOC_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
  1296. return EXT_RETURN_FAIL;
  1297. }
  1298. return EXT_RETURN_SENT;
  1299. }
  1300. #ifndef OPENSSL_NO_OCSP
  1301. EXT_RETURN tls_construct_stoc_status_request(SSL *s, WPACKET *pkt,
  1302. unsigned int context, X509 *x,
  1303. size_t chainidx)
  1304. {
  1305. /* We don't currently support this extension inside a CertificateRequest */
  1306. if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST)
  1307. return EXT_RETURN_NOT_SENT;
  1308. if (!s->ext.status_expected)
  1309. return EXT_RETURN_NOT_SENT;
  1310. if (SSL_IS_TLS13(s) && chainidx != 0)
  1311. return EXT_RETURN_NOT_SENT;
  1312. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
  1313. || !WPACKET_start_sub_packet_u16(pkt)) {
  1314. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1315. SSL_F_TLS_CONSTRUCT_STOC_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
  1316. return EXT_RETURN_FAIL;
  1317. }
  1318. /*
  1319. * In TLSv1.3 we include the certificate status itself. In <= TLSv1.2 we
  1320. * send back an empty extension, with the certificate status appearing as a
  1321. * separate message
  1322. */
  1323. if (SSL_IS_TLS13(s) && !tls_construct_cert_status_body(s, pkt)) {
  1324. /* SSLfatal() already called */
  1325. return EXT_RETURN_FAIL;
  1326. }
  1327. if (!WPACKET_close(pkt)) {
  1328. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1329. SSL_F_TLS_CONSTRUCT_STOC_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
  1330. return EXT_RETURN_FAIL;
  1331. }
  1332. return EXT_RETURN_SENT;
  1333. }
  1334. #endif
  1335. #ifndef OPENSSL_NO_NEXTPROTONEG
  1336. EXT_RETURN tls_construct_stoc_next_proto_neg(SSL *s, WPACKET *pkt,
  1337. unsigned int context, X509 *x,
  1338. size_t chainidx)
  1339. {
  1340. const unsigned char *npa;
  1341. unsigned int npalen;
  1342. int ret;
  1343. int npn_seen = s->s3->npn_seen;
  1344. s->s3->npn_seen = 0;
  1345. if (!npn_seen || s->ctx->ext.npn_advertised_cb == NULL)
  1346. return EXT_RETURN_NOT_SENT;
  1347. ret = s->ctx->ext.npn_advertised_cb(s, &npa, &npalen,
  1348. s->ctx->ext.npn_advertised_cb_arg);
  1349. if (ret == SSL_TLSEXT_ERR_OK) {
  1350. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
  1351. || !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) {
  1352. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1353. SSL_F_TLS_CONSTRUCT_STOC_NEXT_PROTO_NEG,
  1354. ERR_R_INTERNAL_ERROR);
  1355. return EXT_RETURN_FAIL;
  1356. }
  1357. s->s3->npn_seen = 1;
  1358. }
  1359. return EXT_RETURN_SENT;
  1360. }
  1361. #endif
  1362. EXT_RETURN tls_construct_stoc_alpn(SSL *s, WPACKET *pkt, unsigned int context,
  1363. X509 *x, size_t chainidx)
  1364. {
  1365. if (s->s3->alpn_selected == NULL)
  1366. return EXT_RETURN_NOT_SENT;
  1367. if (!WPACKET_put_bytes_u16(pkt,
  1368. TLSEXT_TYPE_application_layer_protocol_negotiation)
  1369. || !WPACKET_start_sub_packet_u16(pkt)
  1370. || !WPACKET_start_sub_packet_u16(pkt)
  1371. || !WPACKET_sub_memcpy_u8(pkt, s->s3->alpn_selected,
  1372. s->s3->alpn_selected_len)
  1373. || !WPACKET_close(pkt)
  1374. || !WPACKET_close(pkt)) {
  1375. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1376. SSL_F_TLS_CONSTRUCT_STOC_ALPN, ERR_R_INTERNAL_ERROR);
  1377. return EXT_RETURN_FAIL;
  1378. }
  1379. return EXT_RETURN_SENT;
  1380. }
  1381. #ifndef OPENSSL_NO_SRTP
  1382. EXT_RETURN tls_construct_stoc_use_srtp(SSL *s, WPACKET *pkt,
  1383. unsigned int context, X509 *x,
  1384. size_t chainidx)
  1385. {
  1386. if (s->srtp_profile == NULL)
  1387. return EXT_RETURN_NOT_SENT;
  1388. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
  1389. || !WPACKET_start_sub_packet_u16(pkt)
  1390. || !WPACKET_put_bytes_u16(pkt, 2)
  1391. || !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id)
  1392. || !WPACKET_put_bytes_u8(pkt, 0)
  1393. || !WPACKET_close(pkt)) {
  1394. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_USE_SRTP,
  1395. ERR_R_INTERNAL_ERROR);
  1396. return EXT_RETURN_FAIL;
  1397. }
  1398. return EXT_RETURN_SENT;
  1399. }
  1400. #endif
  1401. EXT_RETURN tls_construct_stoc_etm(SSL *s, WPACKET *pkt, unsigned int context,
  1402. X509 *x, size_t chainidx)
  1403. {
  1404. if (!s->ext.use_etm)
  1405. return EXT_RETURN_NOT_SENT;
  1406. /*
  1407. * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
  1408. * for other cases too.
  1409. */
  1410. if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD
  1411. || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4
  1412. || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
  1413. || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12) {
  1414. s->ext.use_etm = 0;
  1415. return EXT_RETURN_NOT_SENT;
  1416. }
  1417. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
  1418. || !WPACKET_put_bytes_u16(pkt, 0)) {
  1419. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_ETM,
  1420. ERR_R_INTERNAL_ERROR);
  1421. return EXT_RETURN_FAIL;
  1422. }
  1423. return EXT_RETURN_SENT;
  1424. }
  1425. EXT_RETURN tls_construct_stoc_ems(SSL *s, WPACKET *pkt, unsigned int context,
  1426. X509 *x, size_t chainidx)
  1427. {
  1428. if ((s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) == 0)
  1429. return EXT_RETURN_NOT_SENT;
  1430. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
  1431. || !WPACKET_put_bytes_u16(pkt, 0)) {
  1432. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_EMS,
  1433. ERR_R_INTERNAL_ERROR);
  1434. return EXT_RETURN_FAIL;
  1435. }
  1436. return EXT_RETURN_SENT;
  1437. }
  1438. EXT_RETURN tls_construct_stoc_supported_versions(SSL *s, WPACKET *pkt,
  1439. unsigned int context, X509 *x,
  1440. size_t chainidx)
  1441. {
  1442. if (!ossl_assert(SSL_IS_TLS13(s))) {
  1443. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1444. SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_VERSIONS,
  1445. ERR_R_INTERNAL_ERROR);
  1446. return EXT_RETURN_FAIL;
  1447. }
  1448. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
  1449. || !WPACKET_start_sub_packet_u16(pkt)
  1450. || !WPACKET_put_bytes_u16(pkt, s->version)
  1451. || !WPACKET_close(pkt)) {
  1452. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1453. SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_VERSIONS,
  1454. ERR_R_INTERNAL_ERROR);
  1455. return EXT_RETURN_FAIL;
  1456. }
  1457. return EXT_RETURN_SENT;
  1458. }
  1459. EXT_RETURN tls_construct_stoc_key_share(SSL *s, WPACKET *pkt,
  1460. unsigned int context, X509 *x,
  1461. size_t chainidx)
  1462. {
  1463. #ifndef OPENSSL_NO_TLS1_3
  1464. unsigned char *encodedPoint;
  1465. size_t encoded_pt_len = 0;
  1466. EVP_PKEY *ckey = s->s3->peer_tmp, *skey = NULL;
  1467. if (s->hello_retry_request == SSL_HRR_PENDING) {
  1468. if (ckey != NULL) {
  1469. /* Original key_share was acceptable so don't ask for another one */
  1470. return EXT_RETURN_NOT_SENT;
  1471. }
  1472. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
  1473. || !WPACKET_start_sub_packet_u16(pkt)
  1474. || !WPACKET_put_bytes_u16(pkt, s->s3->group_id)
  1475. || !WPACKET_close(pkt)) {
  1476. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1477. SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE,
  1478. ERR_R_INTERNAL_ERROR);
  1479. return EXT_RETURN_FAIL;
  1480. }
  1481. return EXT_RETURN_SENT;
  1482. }
  1483. if (ckey == NULL) {
  1484. /* No key_share received from client - must be resuming */
  1485. if (!s->hit || !tls13_generate_handshake_secret(s, NULL, 0)) {
  1486. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1487. SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  1488. return EXT_RETURN_FAIL;
  1489. }
  1490. return EXT_RETURN_NOT_SENT;
  1491. }
  1492. if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0) {
  1493. /*
  1494. * PSK ('hit') and explicitly not doing DHE (if the client sent the
  1495. * DHE option we always take it); don't send key share.
  1496. */
  1497. return EXT_RETURN_NOT_SENT;
  1498. }
  1499. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
  1500. || !WPACKET_start_sub_packet_u16(pkt)
  1501. || !WPACKET_put_bytes_u16(pkt, s->s3->group_id)) {
  1502. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1503. SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  1504. return EXT_RETURN_FAIL;
  1505. }
  1506. skey = ssl_generate_pkey(ckey);
  1507. if (skey == NULL) {
  1508. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE,
  1509. ERR_R_MALLOC_FAILURE);
  1510. return EXT_RETURN_FAIL;
  1511. }
  1512. /* Generate encoding of server key */
  1513. encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(skey, &encodedPoint);
  1514. if (encoded_pt_len == 0) {
  1515. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE,
  1516. ERR_R_EC_LIB);
  1517. EVP_PKEY_free(skey);
  1518. return EXT_RETURN_FAIL;
  1519. }
  1520. if (!WPACKET_sub_memcpy_u16(pkt, encodedPoint, encoded_pt_len)
  1521. || !WPACKET_close(pkt)) {
  1522. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE,
  1523. ERR_R_INTERNAL_ERROR);
  1524. EVP_PKEY_free(skey);
  1525. OPENSSL_free(encodedPoint);
  1526. return EXT_RETURN_FAIL;
  1527. }
  1528. OPENSSL_free(encodedPoint);
  1529. /* This causes the crypto state to be updated based on the derived keys */
  1530. s->s3->tmp.pkey = skey;
  1531. if (ssl_derive(s, skey, ckey, 1) == 0) {
  1532. /* SSLfatal() already called */
  1533. return EXT_RETURN_FAIL;
  1534. }
  1535. return EXT_RETURN_SENT;
  1536. #else
  1537. return EXT_RETURN_FAIL;
  1538. #endif
  1539. }
  1540. EXT_RETURN tls_construct_stoc_cookie(SSL *s, WPACKET *pkt, unsigned int context,
  1541. X509 *x, size_t chainidx)
  1542. {
  1543. #ifndef OPENSSL_NO_TLS1_3
  1544. unsigned char *hashval1, *hashval2, *appcookie1, *appcookie2, *cookie;
  1545. unsigned char *hmac, *hmac2;
  1546. size_t startlen, ciphlen, totcookielen, hashlen, hmaclen, appcookielen;
  1547. EVP_MD_CTX *hctx;
  1548. EVP_PKEY *pkey;
  1549. int ret = EXT_RETURN_FAIL;
  1550. if ((s->s3->flags & TLS1_FLAGS_STATELESS) == 0)
  1551. return EXT_RETURN_NOT_SENT;
  1552. if (s->ctx->gen_stateless_cookie_cb == NULL) {
  1553. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1554. SSL_R_NO_COOKIE_CALLBACK_SET);
  1555. return EXT_RETURN_FAIL;
  1556. }
  1557. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie)
  1558. || !WPACKET_start_sub_packet_u16(pkt)
  1559. || !WPACKET_start_sub_packet_u16(pkt)
  1560. || !WPACKET_get_total_written(pkt, &startlen)
  1561. || !WPACKET_reserve_bytes(pkt, MAX_COOKIE_SIZE, &cookie)
  1562. || !WPACKET_put_bytes_u16(pkt, COOKIE_STATE_FORMAT_VERSION)
  1563. || !WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION)
  1564. || !WPACKET_put_bytes_u16(pkt, s->s3->group_id)
  1565. || !s->method->put_cipher_by_char(s->s3->tmp.new_cipher, pkt,
  1566. &ciphlen)
  1567. /* Is there a key_share extension present in this HRR? */
  1568. || !WPACKET_put_bytes_u8(pkt, s->s3->peer_tmp == NULL)
  1569. || !WPACKET_put_bytes_u32(pkt, (unsigned int)time(NULL))
  1570. || !WPACKET_start_sub_packet_u16(pkt)
  1571. || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &hashval1)) {
  1572. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1573. ERR_R_INTERNAL_ERROR);
  1574. return EXT_RETURN_FAIL;
  1575. }
  1576. /*
  1577. * Get the hash of the initial ClientHello. ssl_handshake_hash() operates
  1578. * on raw buffers, so we first reserve sufficient bytes (above) and then
  1579. * subsequently allocate them (below)
  1580. */
  1581. if (!ssl3_digest_cached_records(s, 0)
  1582. || !ssl_handshake_hash(s, hashval1, EVP_MAX_MD_SIZE, &hashlen)) {
  1583. /* SSLfatal() already called */
  1584. return EXT_RETURN_FAIL;
  1585. }
  1586. if (!WPACKET_allocate_bytes(pkt, hashlen, &hashval2)
  1587. || !ossl_assert(hashval1 == hashval2)
  1588. || !WPACKET_close(pkt)
  1589. || !WPACKET_start_sub_packet_u8(pkt)
  1590. || !WPACKET_reserve_bytes(pkt, SSL_COOKIE_LENGTH, &appcookie1)) {
  1591. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1592. ERR_R_INTERNAL_ERROR);
  1593. return EXT_RETURN_FAIL;
  1594. }
  1595. /* Generate the application cookie */
  1596. if (s->ctx->gen_stateless_cookie_cb(s, appcookie1, &appcookielen) == 0) {
  1597. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1598. SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
  1599. return EXT_RETURN_FAIL;
  1600. }
  1601. if (!WPACKET_allocate_bytes(pkt, appcookielen, &appcookie2)
  1602. || !ossl_assert(appcookie1 == appcookie2)
  1603. || !WPACKET_close(pkt)
  1604. || !WPACKET_get_total_written(pkt, &totcookielen)
  1605. || !WPACKET_reserve_bytes(pkt, SHA256_DIGEST_LENGTH, &hmac)) {
  1606. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1607. ERR_R_INTERNAL_ERROR);
  1608. return EXT_RETURN_FAIL;
  1609. }
  1610. hmaclen = SHA256_DIGEST_LENGTH;
  1611. totcookielen -= startlen;
  1612. if (!ossl_assert(totcookielen <= MAX_COOKIE_SIZE - SHA256_DIGEST_LENGTH)) {
  1613. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1614. ERR_R_INTERNAL_ERROR);
  1615. return EXT_RETURN_FAIL;
  1616. }
  1617. /* HMAC the cookie */
  1618. hctx = EVP_MD_CTX_create();
  1619. pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
  1620. s->session_ctx->ext.cookie_hmac_key,
  1621. sizeof(s->session_ctx->ext
  1622. .cookie_hmac_key));
  1623. if (hctx == NULL || pkey == NULL) {
  1624. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1625. ERR_R_MALLOC_FAILURE);
  1626. goto err;
  1627. }
  1628. if (EVP_DigestSignInit(hctx, NULL, EVP_sha256(), NULL, pkey) <= 0
  1629. || EVP_DigestSign(hctx, hmac, &hmaclen, cookie,
  1630. totcookielen) <= 0) {
  1631. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1632. ERR_R_INTERNAL_ERROR);
  1633. goto err;
  1634. }
  1635. if (!ossl_assert(totcookielen + hmaclen <= MAX_COOKIE_SIZE)) {
  1636. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1637. ERR_R_INTERNAL_ERROR);
  1638. goto err;
  1639. }
  1640. if (!WPACKET_allocate_bytes(pkt, hmaclen, &hmac2)
  1641. || !ossl_assert(hmac == hmac2)
  1642. || !ossl_assert(cookie == hmac - totcookielen)
  1643. || !WPACKET_close(pkt)
  1644. || !WPACKET_close(pkt)) {
  1645. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1646. ERR_R_INTERNAL_ERROR);
  1647. goto err;
  1648. }
  1649. ret = EXT_RETURN_SENT;
  1650. err:
  1651. EVP_MD_CTX_free(hctx);
  1652. EVP_PKEY_free(pkey);
  1653. return ret;
  1654. #else
  1655. return EXT_RETURN_FAIL;
  1656. #endif
  1657. }
  1658. EXT_RETURN tls_construct_stoc_cryptopro_bug(SSL *s, WPACKET *pkt,
  1659. unsigned int context, X509 *x,
  1660. size_t chainidx)
  1661. {
  1662. const unsigned char cryptopro_ext[36] = {
  1663. 0xfd, 0xe8, /* 65000 */
  1664. 0x00, 0x20, /* 32 bytes length */
  1665. 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
  1666. 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
  1667. 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
  1668. 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
  1669. };
  1670. if (((s->s3->tmp.new_cipher->id & 0xFFFF) != 0x80
  1671. && (s->s3->tmp.new_cipher->id & 0xFFFF) != 0x81)
  1672. || (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG) == 0)
  1673. return EXT_RETURN_NOT_SENT;
  1674. if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) {
  1675. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1676. SSL_F_TLS_CONSTRUCT_STOC_CRYPTOPRO_BUG, ERR_R_INTERNAL_ERROR);
  1677. return EXT_RETURN_FAIL;
  1678. }
  1679. return EXT_RETURN_SENT;
  1680. }
  1681. EXT_RETURN tls_construct_stoc_early_data(SSL *s, WPACKET *pkt,
  1682. unsigned int context, X509 *x,
  1683. size_t chainidx)
  1684. {
  1685. if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {
  1686. if (s->max_early_data == 0)
  1687. return EXT_RETURN_NOT_SENT;
  1688. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
  1689. || !WPACKET_start_sub_packet_u16(pkt)
  1690. || !WPACKET_put_bytes_u32(pkt, s->max_early_data)
  1691. || !WPACKET_close(pkt)) {
  1692. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1693. SSL_F_TLS_CONSTRUCT_STOC_EARLY_DATA, ERR_R_INTERNAL_ERROR);
  1694. return EXT_RETURN_FAIL;
  1695. }
  1696. return EXT_RETURN_SENT;
  1697. }
  1698. if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED)
  1699. return EXT_RETURN_NOT_SENT;
  1700. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
  1701. || !WPACKET_start_sub_packet_u16(pkt)
  1702. || !WPACKET_close(pkt)) {
  1703. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_EARLY_DATA,
  1704. ERR_R_INTERNAL_ERROR);
  1705. return EXT_RETURN_FAIL;
  1706. }
  1707. return EXT_RETURN_SENT;
  1708. }
  1709. EXT_RETURN tls_construct_stoc_psk(SSL *s, WPACKET *pkt, unsigned int context,
  1710. X509 *x, size_t chainidx)
  1711. {
  1712. if (!s->hit)
  1713. return EXT_RETURN_NOT_SENT;
  1714. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)
  1715. || !WPACKET_start_sub_packet_u16(pkt)
  1716. || !WPACKET_put_bytes_u16(pkt, s->ext.tick_identity)
  1717. || !WPACKET_close(pkt)) {
  1718. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1719. SSL_F_TLS_CONSTRUCT_STOC_PSK, ERR_R_INTERNAL_ERROR);
  1720. return EXT_RETURN_FAIL;
  1721. }
  1722. return EXT_RETURN_SENT;
  1723. }