extensions_srvr.c 66 KB

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