extensions_srvr.c 68 KB

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