1
0

extensions_srvr.c 74 KB

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