extensions_srvr.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980
  1. /*
  2. * Copyright 2016-2022 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 1
  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, 8 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 + 8 + 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. uint64_t 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_8(&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 = 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, 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. agesec = (uint32_t)(time(NULL) - sess->time);
  1056. agems = agesec * (uint32_t)1000;
  1057. ticket_age -= sess->ext.tick_age_add;
  1058. /*
  1059. * For simplicity we do our age calculations in seconds. If the
  1060. * client does it in ms then it could appear that their ticket age
  1061. * is longer than ours (our ticket age calculation should always be
  1062. * slightly longer than the client's due to the network latency).
  1063. * Therefore we add 1000ms to our age calculation to adjust for
  1064. * rounding errors.
  1065. */
  1066. if (id == 0
  1067. && sess->timeout >= (long)agesec
  1068. && agems / (uint32_t)1000 == agesec
  1069. && ticket_age <= agems + 1000
  1070. && ticket_age + TICKET_AGE_ALLOWANCE >= agems + 1000) {
  1071. /*
  1072. * Ticket age is within tolerance and not expired. We allow it
  1073. * for early data
  1074. */
  1075. s->ext.early_data_ok = 1;
  1076. }
  1077. }
  1078. md = ssl_md(sess->cipher->algorithm2);
  1079. if (md != ssl_md(s->s3->tmp.new_cipher->algorithm2)) {
  1080. /* The ciphersuite is not compatible with this session. */
  1081. SSL_SESSION_free(sess);
  1082. sess = NULL;
  1083. s->ext.early_data_ok = 0;
  1084. s->ext.ticket_expected = 0;
  1085. continue;
  1086. }
  1087. break;
  1088. }
  1089. if (sess == NULL)
  1090. return 1;
  1091. binderoffset = PACKET_data(pkt) - (const unsigned char *)s->init_buf->data;
  1092. hashsize = EVP_MD_size(md);
  1093. if (!PACKET_get_length_prefixed_2(pkt, &binders)) {
  1094. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_PSK,
  1095. SSL_R_BAD_EXTENSION);
  1096. goto err;
  1097. }
  1098. for (i = 0; i <= id; i++) {
  1099. if (!PACKET_get_length_prefixed_1(&binders, &binder)) {
  1100. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_PSK,
  1101. SSL_R_BAD_EXTENSION);
  1102. goto err;
  1103. }
  1104. }
  1105. if (PACKET_remaining(&binder) != hashsize) {
  1106. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_PSK,
  1107. SSL_R_BAD_EXTENSION);
  1108. goto err;
  1109. }
  1110. if (tls_psk_do_binder(s, md, (const unsigned char *)s->init_buf->data,
  1111. binderoffset, PACKET_data(&binder), NULL, sess, 0,
  1112. ext) != 1) {
  1113. /* SSLfatal() already called */
  1114. goto err;
  1115. }
  1116. s->ext.tick_identity = id;
  1117. SSL_SESSION_free(s->session);
  1118. s->session = sess;
  1119. return 1;
  1120. err:
  1121. SSL_SESSION_free(sess);
  1122. return 0;
  1123. }
  1124. int tls_parse_ctos_post_handshake_auth(SSL *s, PACKET *pkt, unsigned int context,
  1125. X509 *x, size_t chainidx)
  1126. {
  1127. if (PACKET_remaining(pkt) != 0) {
  1128. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_POST_HANDSHAKE_AUTH,
  1129. SSL_R_POST_HANDSHAKE_AUTH_ENCODING_ERR);
  1130. return 0;
  1131. }
  1132. s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;
  1133. return 1;
  1134. }
  1135. /*
  1136. * Add the server's renegotiation binding
  1137. */
  1138. EXT_RETURN tls_construct_stoc_renegotiate(SSL *s, WPACKET *pkt,
  1139. unsigned int context, X509 *x,
  1140. size_t chainidx)
  1141. {
  1142. if (!s->s3->send_connection_binding)
  1143. return EXT_RETURN_NOT_SENT;
  1144. /* Still add this even if SSL_OP_NO_RENEGOTIATION is set */
  1145. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
  1146. || !WPACKET_start_sub_packet_u16(pkt)
  1147. || !WPACKET_start_sub_packet_u8(pkt)
  1148. || !WPACKET_memcpy(pkt, s->s3->previous_client_finished,
  1149. s->s3->previous_client_finished_len)
  1150. || !WPACKET_memcpy(pkt, s->s3->previous_server_finished,
  1151. s->s3->previous_server_finished_len)
  1152. || !WPACKET_close(pkt)
  1153. || !WPACKET_close(pkt)) {
  1154. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_RENEGOTIATE,
  1155. ERR_R_INTERNAL_ERROR);
  1156. return EXT_RETURN_FAIL;
  1157. }
  1158. return EXT_RETURN_SENT;
  1159. }
  1160. EXT_RETURN tls_construct_stoc_server_name(SSL *s, WPACKET *pkt,
  1161. unsigned int context, X509 *x,
  1162. size_t chainidx)
  1163. {
  1164. if (s->servername_done != 1)
  1165. return EXT_RETURN_NOT_SENT;
  1166. /*
  1167. * Prior to TLSv1.3 we ignore any SNI in the current handshake if resuming.
  1168. * We just use the servername from the initial handshake.
  1169. */
  1170. if (s->hit && !SSL_IS_TLS13(s))
  1171. return EXT_RETURN_NOT_SENT;
  1172. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
  1173. || !WPACKET_put_bytes_u16(pkt, 0)) {
  1174. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_SERVER_NAME,
  1175. ERR_R_INTERNAL_ERROR);
  1176. return EXT_RETURN_FAIL;
  1177. }
  1178. return EXT_RETURN_SENT;
  1179. }
  1180. /* Add/include the server's max fragment len extension into ServerHello */
  1181. EXT_RETURN tls_construct_stoc_maxfragmentlen(SSL *s, WPACKET *pkt,
  1182. unsigned int context, X509 *x,
  1183. size_t chainidx)
  1184. {
  1185. if (!USE_MAX_FRAGMENT_LENGTH_EXT(s->session))
  1186. return EXT_RETURN_NOT_SENT;
  1187. /*-
  1188. * 4 bytes for this extension type and extension length
  1189. * 1 byte for the Max Fragment Length code value.
  1190. */
  1191. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length)
  1192. || !WPACKET_start_sub_packet_u16(pkt)
  1193. || !WPACKET_put_bytes_u8(pkt, s->session->ext.max_fragment_len_mode)
  1194. || !WPACKET_close(pkt)) {
  1195. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1196. SSL_F_TLS_CONSTRUCT_STOC_MAXFRAGMENTLEN, ERR_R_INTERNAL_ERROR);
  1197. return EXT_RETURN_FAIL;
  1198. }
  1199. return EXT_RETURN_SENT;
  1200. }
  1201. #ifndef OPENSSL_NO_EC
  1202. EXT_RETURN tls_construct_stoc_ec_pt_formats(SSL *s, WPACKET *pkt,
  1203. unsigned int context, X509 *x,
  1204. size_t chainidx)
  1205. {
  1206. unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1207. unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1208. int using_ecc = ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))
  1209. && (s->ext.peer_ecpointformats != NULL);
  1210. const unsigned char *plist;
  1211. size_t plistlen;
  1212. if (!using_ecc)
  1213. return EXT_RETURN_NOT_SENT;
  1214. tls1_get_formatlist(s, &plist, &plistlen);
  1215. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
  1216. || !WPACKET_start_sub_packet_u16(pkt)
  1217. || !WPACKET_sub_memcpy_u8(pkt, plist, plistlen)
  1218. || !WPACKET_close(pkt)) {
  1219. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1220. SSL_F_TLS_CONSTRUCT_STOC_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
  1221. return EXT_RETURN_FAIL;
  1222. }
  1223. return EXT_RETURN_SENT;
  1224. }
  1225. #endif
  1226. #ifndef OPENSSL_NO_EC
  1227. EXT_RETURN tls_construct_stoc_supported_groups(SSL *s, WPACKET *pkt,
  1228. unsigned int context, X509 *x,
  1229. size_t chainidx)
  1230. {
  1231. const uint16_t *groups;
  1232. size_t numgroups, i, first = 1;
  1233. /* s->s3->group_id is non zero if we accepted a key_share */
  1234. if (s->s3->group_id == 0)
  1235. return EXT_RETURN_NOT_SENT;
  1236. /* Get our list of supported groups */
  1237. tls1_get_supported_groups(s, &groups, &numgroups);
  1238. if (numgroups == 0) {
  1239. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1240. SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_GROUPS, ERR_R_INTERNAL_ERROR);
  1241. return EXT_RETURN_FAIL;
  1242. }
  1243. /* Copy group ID if supported */
  1244. for (i = 0; i < numgroups; i++) {
  1245. uint16_t group = groups[i];
  1246. if (tls_curve_allowed(s, group, SSL_SECOP_CURVE_SUPPORTED)) {
  1247. if (first) {
  1248. /*
  1249. * Check if the client is already using our preferred group. If
  1250. * so we don't need to add this extension
  1251. */
  1252. if (s->s3->group_id == group)
  1253. return EXT_RETURN_NOT_SENT;
  1254. /* Add extension header */
  1255. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)
  1256. /* Sub-packet for supported_groups extension */
  1257. || !WPACKET_start_sub_packet_u16(pkt)
  1258. || !WPACKET_start_sub_packet_u16(pkt)) {
  1259. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1260. SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_GROUPS,
  1261. ERR_R_INTERNAL_ERROR);
  1262. return EXT_RETURN_FAIL;
  1263. }
  1264. first = 0;
  1265. }
  1266. if (!WPACKET_put_bytes_u16(pkt, group)) {
  1267. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1268. SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_GROUPS,
  1269. ERR_R_INTERNAL_ERROR);
  1270. return EXT_RETURN_FAIL;
  1271. }
  1272. }
  1273. }
  1274. if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
  1275. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1276. SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_GROUPS,
  1277. ERR_R_INTERNAL_ERROR);
  1278. return EXT_RETURN_FAIL;
  1279. }
  1280. return EXT_RETURN_SENT;
  1281. }
  1282. #endif
  1283. EXT_RETURN tls_construct_stoc_session_ticket(SSL *s, WPACKET *pkt,
  1284. unsigned int context, X509 *x,
  1285. size_t chainidx)
  1286. {
  1287. if (!s->ext.ticket_expected || !tls_use_ticket(s)) {
  1288. s->ext.ticket_expected = 0;
  1289. return EXT_RETURN_NOT_SENT;
  1290. }
  1291. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
  1292. || !WPACKET_put_bytes_u16(pkt, 0)) {
  1293. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1294. SSL_F_TLS_CONSTRUCT_STOC_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
  1295. return EXT_RETURN_FAIL;
  1296. }
  1297. return EXT_RETURN_SENT;
  1298. }
  1299. #ifndef OPENSSL_NO_OCSP
  1300. EXT_RETURN tls_construct_stoc_status_request(SSL *s, WPACKET *pkt,
  1301. unsigned int context, X509 *x,
  1302. size_t chainidx)
  1303. {
  1304. /* We don't currently support this extension inside a CertificateRequest */
  1305. if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST)
  1306. return EXT_RETURN_NOT_SENT;
  1307. if (!s->ext.status_expected)
  1308. return EXT_RETURN_NOT_SENT;
  1309. if (SSL_IS_TLS13(s) && chainidx != 0)
  1310. return EXT_RETURN_NOT_SENT;
  1311. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
  1312. || !WPACKET_start_sub_packet_u16(pkt)) {
  1313. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1314. SSL_F_TLS_CONSTRUCT_STOC_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
  1315. return EXT_RETURN_FAIL;
  1316. }
  1317. /*
  1318. * In TLSv1.3 we include the certificate status itself. In <= TLSv1.2 we
  1319. * send back an empty extension, with the certificate status appearing as a
  1320. * separate message
  1321. */
  1322. if (SSL_IS_TLS13(s) && !tls_construct_cert_status_body(s, pkt)) {
  1323. /* SSLfatal() already called */
  1324. return EXT_RETURN_FAIL;
  1325. }
  1326. if (!WPACKET_close(pkt)) {
  1327. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1328. SSL_F_TLS_CONSTRUCT_STOC_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
  1329. return EXT_RETURN_FAIL;
  1330. }
  1331. return EXT_RETURN_SENT;
  1332. }
  1333. #endif
  1334. #ifndef OPENSSL_NO_NEXTPROTONEG
  1335. EXT_RETURN tls_construct_stoc_next_proto_neg(SSL *s, WPACKET *pkt,
  1336. unsigned int context, X509 *x,
  1337. size_t chainidx)
  1338. {
  1339. const unsigned char *npa;
  1340. unsigned int npalen;
  1341. int ret;
  1342. int npn_seen = s->s3->npn_seen;
  1343. s->s3->npn_seen = 0;
  1344. if (!npn_seen || s->ctx->ext.npn_advertised_cb == NULL)
  1345. return EXT_RETURN_NOT_SENT;
  1346. ret = s->ctx->ext.npn_advertised_cb(s, &npa, &npalen,
  1347. s->ctx->ext.npn_advertised_cb_arg);
  1348. if (ret == SSL_TLSEXT_ERR_OK) {
  1349. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
  1350. || !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) {
  1351. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1352. SSL_F_TLS_CONSTRUCT_STOC_NEXT_PROTO_NEG,
  1353. ERR_R_INTERNAL_ERROR);
  1354. return EXT_RETURN_FAIL;
  1355. }
  1356. s->s3->npn_seen = 1;
  1357. }
  1358. return EXT_RETURN_SENT;
  1359. }
  1360. #endif
  1361. EXT_RETURN tls_construct_stoc_alpn(SSL *s, WPACKET *pkt, unsigned int context,
  1362. X509 *x, size_t chainidx)
  1363. {
  1364. if (s->s3->alpn_selected == NULL)
  1365. return EXT_RETURN_NOT_SENT;
  1366. if (!WPACKET_put_bytes_u16(pkt,
  1367. TLSEXT_TYPE_application_layer_protocol_negotiation)
  1368. || !WPACKET_start_sub_packet_u16(pkt)
  1369. || !WPACKET_start_sub_packet_u16(pkt)
  1370. || !WPACKET_sub_memcpy_u8(pkt, s->s3->alpn_selected,
  1371. s->s3->alpn_selected_len)
  1372. || !WPACKET_close(pkt)
  1373. || !WPACKET_close(pkt)) {
  1374. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1375. SSL_F_TLS_CONSTRUCT_STOC_ALPN, ERR_R_INTERNAL_ERROR);
  1376. return EXT_RETURN_FAIL;
  1377. }
  1378. return EXT_RETURN_SENT;
  1379. }
  1380. #ifndef OPENSSL_NO_SRTP
  1381. EXT_RETURN tls_construct_stoc_use_srtp(SSL *s, WPACKET *pkt,
  1382. unsigned int context, X509 *x,
  1383. size_t chainidx)
  1384. {
  1385. if (s->srtp_profile == NULL)
  1386. return EXT_RETURN_NOT_SENT;
  1387. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
  1388. || !WPACKET_start_sub_packet_u16(pkt)
  1389. || !WPACKET_put_bytes_u16(pkt, 2)
  1390. || !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id)
  1391. || !WPACKET_put_bytes_u8(pkt, 0)
  1392. || !WPACKET_close(pkt)) {
  1393. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_USE_SRTP,
  1394. ERR_R_INTERNAL_ERROR);
  1395. return EXT_RETURN_FAIL;
  1396. }
  1397. return EXT_RETURN_SENT;
  1398. }
  1399. #endif
  1400. EXT_RETURN tls_construct_stoc_etm(SSL *s, WPACKET *pkt, unsigned int context,
  1401. X509 *x, size_t chainidx)
  1402. {
  1403. if (!s->ext.use_etm)
  1404. return EXT_RETURN_NOT_SENT;
  1405. /*
  1406. * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
  1407. * for other cases too.
  1408. */
  1409. if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD
  1410. || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4
  1411. || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
  1412. || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12) {
  1413. s->ext.use_etm = 0;
  1414. return EXT_RETURN_NOT_SENT;
  1415. }
  1416. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
  1417. || !WPACKET_put_bytes_u16(pkt, 0)) {
  1418. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_ETM,
  1419. ERR_R_INTERNAL_ERROR);
  1420. return EXT_RETURN_FAIL;
  1421. }
  1422. return EXT_RETURN_SENT;
  1423. }
  1424. EXT_RETURN tls_construct_stoc_ems(SSL *s, WPACKET *pkt, unsigned int context,
  1425. X509 *x, size_t chainidx)
  1426. {
  1427. if ((s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) == 0)
  1428. return EXT_RETURN_NOT_SENT;
  1429. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
  1430. || !WPACKET_put_bytes_u16(pkt, 0)) {
  1431. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_EMS,
  1432. ERR_R_INTERNAL_ERROR);
  1433. return EXT_RETURN_FAIL;
  1434. }
  1435. return EXT_RETURN_SENT;
  1436. }
  1437. EXT_RETURN tls_construct_stoc_supported_versions(SSL *s, WPACKET *pkt,
  1438. unsigned int context, X509 *x,
  1439. size_t chainidx)
  1440. {
  1441. if (!ossl_assert(SSL_IS_TLS13(s))) {
  1442. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1443. SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_VERSIONS,
  1444. ERR_R_INTERNAL_ERROR);
  1445. return EXT_RETURN_FAIL;
  1446. }
  1447. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
  1448. || !WPACKET_start_sub_packet_u16(pkt)
  1449. || !WPACKET_put_bytes_u16(pkt, s->version)
  1450. || !WPACKET_close(pkt)) {
  1451. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1452. SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_VERSIONS,
  1453. ERR_R_INTERNAL_ERROR);
  1454. return EXT_RETURN_FAIL;
  1455. }
  1456. return EXT_RETURN_SENT;
  1457. }
  1458. EXT_RETURN tls_construct_stoc_key_share(SSL *s, WPACKET *pkt,
  1459. unsigned int context, X509 *x,
  1460. size_t chainidx)
  1461. {
  1462. #ifndef OPENSSL_NO_TLS1_3
  1463. unsigned char *encodedPoint;
  1464. size_t encoded_pt_len = 0;
  1465. EVP_PKEY *ckey = s->s3->peer_tmp, *skey = NULL;
  1466. if (s->hello_retry_request == SSL_HRR_PENDING) {
  1467. if (ckey != NULL) {
  1468. /* Original key_share was acceptable so don't ask for another one */
  1469. return EXT_RETURN_NOT_SENT;
  1470. }
  1471. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
  1472. || !WPACKET_start_sub_packet_u16(pkt)
  1473. || !WPACKET_put_bytes_u16(pkt, s->s3->group_id)
  1474. || !WPACKET_close(pkt)) {
  1475. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1476. SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE,
  1477. ERR_R_INTERNAL_ERROR);
  1478. return EXT_RETURN_FAIL;
  1479. }
  1480. return EXT_RETURN_SENT;
  1481. }
  1482. if (ckey == NULL) {
  1483. /* No key_share received from client - must be resuming */
  1484. if (!s->hit || !tls13_generate_handshake_secret(s, NULL, 0)) {
  1485. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1486. SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  1487. return EXT_RETURN_FAIL;
  1488. }
  1489. return EXT_RETURN_NOT_SENT;
  1490. }
  1491. if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0) {
  1492. /*
  1493. * PSK ('hit') and explicitly not doing DHE (if the client sent the
  1494. * DHE option we always take it); don't send key share.
  1495. */
  1496. return EXT_RETURN_NOT_SENT;
  1497. }
  1498. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
  1499. || !WPACKET_start_sub_packet_u16(pkt)
  1500. || !WPACKET_put_bytes_u16(pkt, s->s3->group_id)) {
  1501. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1502. SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  1503. return EXT_RETURN_FAIL;
  1504. }
  1505. skey = ssl_generate_pkey(ckey);
  1506. if (skey == NULL) {
  1507. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE,
  1508. ERR_R_MALLOC_FAILURE);
  1509. return EXT_RETURN_FAIL;
  1510. }
  1511. /* Generate encoding of server key */
  1512. encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(skey, &encodedPoint);
  1513. if (encoded_pt_len == 0) {
  1514. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE,
  1515. ERR_R_EC_LIB);
  1516. EVP_PKEY_free(skey);
  1517. return EXT_RETURN_FAIL;
  1518. }
  1519. if (!WPACKET_sub_memcpy_u16(pkt, encodedPoint, encoded_pt_len)
  1520. || !WPACKET_close(pkt)) {
  1521. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE,
  1522. ERR_R_INTERNAL_ERROR);
  1523. EVP_PKEY_free(skey);
  1524. OPENSSL_free(encodedPoint);
  1525. return EXT_RETURN_FAIL;
  1526. }
  1527. OPENSSL_free(encodedPoint);
  1528. /* This causes the crypto state to be updated based on the derived keys */
  1529. s->s3->tmp.pkey = skey;
  1530. if (ssl_derive(s, skey, ckey, 1) == 0) {
  1531. /* SSLfatal() already called */
  1532. return EXT_RETURN_FAIL;
  1533. }
  1534. return EXT_RETURN_SENT;
  1535. #else
  1536. return EXT_RETURN_FAIL;
  1537. #endif
  1538. }
  1539. EXT_RETURN tls_construct_stoc_cookie(SSL *s, WPACKET *pkt, unsigned int context,
  1540. X509 *x, size_t chainidx)
  1541. {
  1542. #ifndef OPENSSL_NO_TLS1_3
  1543. unsigned char *hashval1, *hashval2, *appcookie1, *appcookie2, *cookie;
  1544. unsigned char *hmac, *hmac2;
  1545. size_t startlen, ciphlen, totcookielen, hashlen, hmaclen, appcookielen;
  1546. EVP_MD_CTX *hctx;
  1547. EVP_PKEY *pkey;
  1548. int ret = EXT_RETURN_FAIL;
  1549. if ((s->s3->flags & TLS1_FLAGS_STATELESS) == 0)
  1550. return EXT_RETURN_NOT_SENT;
  1551. if (s->ctx->gen_stateless_cookie_cb == NULL) {
  1552. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1553. SSL_R_NO_COOKIE_CALLBACK_SET);
  1554. return EXT_RETURN_FAIL;
  1555. }
  1556. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie)
  1557. || !WPACKET_start_sub_packet_u16(pkt)
  1558. || !WPACKET_start_sub_packet_u16(pkt)
  1559. || !WPACKET_get_total_written(pkt, &startlen)
  1560. || !WPACKET_reserve_bytes(pkt, MAX_COOKIE_SIZE, &cookie)
  1561. || !WPACKET_put_bytes_u16(pkt, COOKIE_STATE_FORMAT_VERSION)
  1562. || !WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION)
  1563. || !WPACKET_put_bytes_u16(pkt, s->s3->group_id)
  1564. || !s->method->put_cipher_by_char(s->s3->tmp.new_cipher, pkt,
  1565. &ciphlen)
  1566. /* Is there a key_share extension present in this HRR? */
  1567. || !WPACKET_put_bytes_u8(pkt, s->s3->peer_tmp == NULL)
  1568. || !WPACKET_put_bytes_u64(pkt, time(NULL))
  1569. || !WPACKET_start_sub_packet_u16(pkt)
  1570. || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &hashval1)) {
  1571. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1572. ERR_R_INTERNAL_ERROR);
  1573. return EXT_RETURN_FAIL;
  1574. }
  1575. /*
  1576. * Get the hash of the initial ClientHello. ssl_handshake_hash() operates
  1577. * on raw buffers, so we first reserve sufficient bytes (above) and then
  1578. * subsequently allocate them (below)
  1579. */
  1580. if (!ssl3_digest_cached_records(s, 0)
  1581. || !ssl_handshake_hash(s, hashval1, EVP_MAX_MD_SIZE, &hashlen)) {
  1582. /* SSLfatal() already called */
  1583. return EXT_RETURN_FAIL;
  1584. }
  1585. if (!WPACKET_allocate_bytes(pkt, hashlen, &hashval2)
  1586. || !ossl_assert(hashval1 == hashval2)
  1587. || !WPACKET_close(pkt)
  1588. || !WPACKET_start_sub_packet_u8(pkt)
  1589. || !WPACKET_reserve_bytes(pkt, SSL_COOKIE_LENGTH, &appcookie1)) {
  1590. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1591. ERR_R_INTERNAL_ERROR);
  1592. return EXT_RETURN_FAIL;
  1593. }
  1594. /* Generate the application cookie */
  1595. if (s->ctx->gen_stateless_cookie_cb(s, appcookie1, &appcookielen) == 0) {
  1596. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1597. SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
  1598. return EXT_RETURN_FAIL;
  1599. }
  1600. if (!WPACKET_allocate_bytes(pkt, appcookielen, &appcookie2)
  1601. || !ossl_assert(appcookie1 == appcookie2)
  1602. || !WPACKET_close(pkt)
  1603. || !WPACKET_get_total_written(pkt, &totcookielen)
  1604. || !WPACKET_reserve_bytes(pkt, SHA256_DIGEST_LENGTH, &hmac)) {
  1605. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1606. ERR_R_INTERNAL_ERROR);
  1607. return EXT_RETURN_FAIL;
  1608. }
  1609. hmaclen = SHA256_DIGEST_LENGTH;
  1610. totcookielen -= startlen;
  1611. if (!ossl_assert(totcookielen <= MAX_COOKIE_SIZE - SHA256_DIGEST_LENGTH)) {
  1612. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1613. ERR_R_INTERNAL_ERROR);
  1614. return EXT_RETURN_FAIL;
  1615. }
  1616. /* HMAC the cookie */
  1617. hctx = EVP_MD_CTX_create();
  1618. pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
  1619. s->session_ctx->ext.cookie_hmac_key,
  1620. sizeof(s->session_ctx->ext
  1621. .cookie_hmac_key));
  1622. if (hctx == NULL || pkey == NULL) {
  1623. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1624. ERR_R_MALLOC_FAILURE);
  1625. goto err;
  1626. }
  1627. if (EVP_DigestSignInit(hctx, NULL, EVP_sha256(), NULL, pkey) <= 0
  1628. || EVP_DigestSign(hctx, hmac, &hmaclen, cookie,
  1629. totcookielen) <= 0) {
  1630. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1631. ERR_R_INTERNAL_ERROR);
  1632. goto err;
  1633. }
  1634. if (!ossl_assert(totcookielen + hmaclen <= MAX_COOKIE_SIZE)) {
  1635. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1636. ERR_R_INTERNAL_ERROR);
  1637. goto err;
  1638. }
  1639. if (!WPACKET_allocate_bytes(pkt, hmaclen, &hmac2)
  1640. || !ossl_assert(hmac == hmac2)
  1641. || !ossl_assert(cookie == hmac - totcookielen)
  1642. || !WPACKET_close(pkt)
  1643. || !WPACKET_close(pkt)) {
  1644. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1645. ERR_R_INTERNAL_ERROR);
  1646. goto err;
  1647. }
  1648. ret = EXT_RETURN_SENT;
  1649. err:
  1650. EVP_MD_CTX_free(hctx);
  1651. EVP_PKEY_free(pkey);
  1652. return ret;
  1653. #else
  1654. return EXT_RETURN_FAIL;
  1655. #endif
  1656. }
  1657. EXT_RETURN tls_construct_stoc_cryptopro_bug(SSL *s, WPACKET *pkt,
  1658. unsigned int context, X509 *x,
  1659. size_t chainidx)
  1660. {
  1661. const unsigned char cryptopro_ext[36] = {
  1662. 0xfd, 0xe8, /* 65000 */
  1663. 0x00, 0x20, /* 32 bytes length */
  1664. 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
  1665. 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
  1666. 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
  1667. 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
  1668. };
  1669. if (((s->s3->tmp.new_cipher->id & 0xFFFF) != 0x80
  1670. && (s->s3->tmp.new_cipher->id & 0xFFFF) != 0x81)
  1671. || (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG) == 0)
  1672. return EXT_RETURN_NOT_SENT;
  1673. if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) {
  1674. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1675. SSL_F_TLS_CONSTRUCT_STOC_CRYPTOPRO_BUG, ERR_R_INTERNAL_ERROR);
  1676. return EXT_RETURN_FAIL;
  1677. }
  1678. return EXT_RETURN_SENT;
  1679. }
  1680. EXT_RETURN tls_construct_stoc_early_data(SSL *s, WPACKET *pkt,
  1681. unsigned int context, X509 *x,
  1682. size_t chainidx)
  1683. {
  1684. if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {
  1685. if (s->max_early_data == 0)
  1686. return EXT_RETURN_NOT_SENT;
  1687. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
  1688. || !WPACKET_start_sub_packet_u16(pkt)
  1689. || !WPACKET_put_bytes_u32(pkt, s->max_early_data)
  1690. || !WPACKET_close(pkt)) {
  1691. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1692. SSL_F_TLS_CONSTRUCT_STOC_EARLY_DATA, ERR_R_INTERNAL_ERROR);
  1693. return EXT_RETURN_FAIL;
  1694. }
  1695. return EXT_RETURN_SENT;
  1696. }
  1697. if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED)
  1698. return EXT_RETURN_NOT_SENT;
  1699. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
  1700. || !WPACKET_start_sub_packet_u16(pkt)
  1701. || !WPACKET_close(pkt)) {
  1702. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_EARLY_DATA,
  1703. ERR_R_INTERNAL_ERROR);
  1704. return EXT_RETURN_FAIL;
  1705. }
  1706. return EXT_RETURN_SENT;
  1707. }
  1708. EXT_RETURN tls_construct_stoc_psk(SSL *s, WPACKET *pkt, unsigned int context,
  1709. X509 *x, size_t chainidx)
  1710. {
  1711. if (!s->hit)
  1712. return EXT_RETURN_NOT_SENT;
  1713. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)
  1714. || !WPACKET_start_sub_packet_u16(pkt)
  1715. || !WPACKET_put_bytes_u16(pkt, s->ext.tick_identity)
  1716. || !WPACKET_close(pkt)) {
  1717. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1718. SSL_F_TLS_CONSTRUCT_STOC_PSK, ERR_R_INTERNAL_ERROR);
  1719. return EXT_RETURN_FAIL;
  1720. }
  1721. return EXT_RETURN_SENT;
  1722. }