extensions_clnt.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164
  1. /*
  2. * Copyright 2016-2023 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 "internal/cryptlib.h"
  12. #include "statem_local.h"
  13. EXT_RETURN tls_construct_ctos_renegotiate(SSL_CONNECTION *s, WPACKET *pkt,
  14. unsigned int context, X509 *x,
  15. size_t chainidx)
  16. {
  17. if (!s->renegotiate) {
  18. /* If not renegotiating, send an empty RI extension to indicate support */
  19. #if DTLS_MAX_VERSION_INTERNAL != DTLS1_2_VERSION
  20. # error Internal DTLS version error
  21. #endif
  22. if (!SSL_CONNECTION_IS_DTLS(s)
  23. && (s->min_proto_version >= TLS1_3_VERSION
  24. || (ssl_security(s, SSL_SECOP_VERSION, 0, TLS1_VERSION, NULL)
  25. && s->min_proto_version <= TLS1_VERSION))) {
  26. /*
  27. * For TLS <= 1.0 SCSV is used instead, and for TLS 1.3 this
  28. * extension isn't used at all.
  29. */
  30. return EXT_RETURN_NOT_SENT;
  31. }
  32. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
  33. || !WPACKET_start_sub_packet_u16(pkt)
  34. || !WPACKET_put_bytes_u8(pkt, 0)
  35. || !WPACKET_close(pkt)) {
  36. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  37. return EXT_RETURN_FAIL;
  38. }
  39. return EXT_RETURN_SENT;
  40. }
  41. /* Add a complete RI extension if renegotiating */
  42. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
  43. || !WPACKET_start_sub_packet_u16(pkt)
  44. || !WPACKET_sub_memcpy_u8(pkt, s->s3.previous_client_finished,
  45. s->s3.previous_client_finished_len)
  46. || !WPACKET_close(pkt)) {
  47. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  48. return EXT_RETURN_FAIL;
  49. }
  50. return EXT_RETURN_SENT;
  51. }
  52. EXT_RETURN tls_construct_ctos_server_name(SSL_CONNECTION *s, WPACKET *pkt,
  53. unsigned int context, X509 *x,
  54. size_t chainidx)
  55. {
  56. if (s->ext.hostname == NULL)
  57. return EXT_RETURN_NOT_SENT;
  58. /* Add TLS extension servername to the Client Hello message */
  59. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
  60. /* Sub-packet for server_name extension */
  61. || !WPACKET_start_sub_packet_u16(pkt)
  62. /* Sub-packet for servername list (always 1 hostname)*/
  63. || !WPACKET_start_sub_packet_u16(pkt)
  64. || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name)
  65. || !WPACKET_sub_memcpy_u16(pkt, s->ext.hostname,
  66. strlen(s->ext.hostname))
  67. || !WPACKET_close(pkt)
  68. || !WPACKET_close(pkt)) {
  69. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  70. return EXT_RETURN_FAIL;
  71. }
  72. return EXT_RETURN_SENT;
  73. }
  74. /* Push a Max Fragment Len extension into ClientHello */
  75. EXT_RETURN tls_construct_ctos_maxfragmentlen(SSL_CONNECTION *s, WPACKET *pkt,
  76. unsigned int context, X509 *x,
  77. size_t chainidx)
  78. {
  79. if (s->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_DISABLED)
  80. return EXT_RETURN_NOT_SENT;
  81. /* Add Max Fragment Length extension if client enabled it. */
  82. /*-
  83. * 4 bytes for this extension type and extension length
  84. * 1 byte for the Max Fragment Length code value.
  85. */
  86. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length)
  87. /* Sub-packet for Max Fragment Length extension (1 byte) */
  88. || !WPACKET_start_sub_packet_u16(pkt)
  89. || !WPACKET_put_bytes_u8(pkt, s->ext.max_fragment_len_mode)
  90. || !WPACKET_close(pkt)) {
  91. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  92. return EXT_RETURN_FAIL;
  93. }
  94. return EXT_RETURN_SENT;
  95. }
  96. #ifndef OPENSSL_NO_SRP
  97. EXT_RETURN tls_construct_ctos_srp(SSL_CONNECTION *s, WPACKET *pkt,
  98. unsigned int context,
  99. X509 *x, size_t chainidx)
  100. {
  101. /* Add SRP username if there is one */
  102. if (s->srp_ctx.login == NULL)
  103. return EXT_RETURN_NOT_SENT;
  104. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp)
  105. /* Sub-packet for SRP extension */
  106. || !WPACKET_start_sub_packet_u16(pkt)
  107. || !WPACKET_start_sub_packet_u8(pkt)
  108. /* login must not be zero...internal error if so */
  109. || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
  110. || !WPACKET_memcpy(pkt, s->srp_ctx.login,
  111. strlen(s->srp_ctx.login))
  112. || !WPACKET_close(pkt)
  113. || !WPACKET_close(pkt)) {
  114. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  115. return EXT_RETURN_FAIL;
  116. }
  117. return EXT_RETURN_SENT;
  118. }
  119. #endif
  120. static int use_ecc(SSL_CONNECTION *s, int min_version, int max_version)
  121. {
  122. int i, end, ret = 0;
  123. unsigned long alg_k, alg_a;
  124. STACK_OF(SSL_CIPHER) *cipher_stack = NULL;
  125. const uint16_t *pgroups = NULL;
  126. size_t num_groups, j;
  127. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  128. /* See if we support any ECC ciphersuites */
  129. if (s->version == SSL3_VERSION)
  130. return 0;
  131. cipher_stack = SSL_get1_supported_ciphers(ssl);
  132. end = sk_SSL_CIPHER_num(cipher_stack);
  133. for (i = 0; i < end; i++) {
  134. const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
  135. alg_k = c->algorithm_mkey;
  136. alg_a = c->algorithm_auth;
  137. if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))
  138. || (alg_a & SSL_aECDSA)
  139. || c->min_tls >= TLS1_3_VERSION) {
  140. ret = 1;
  141. break;
  142. }
  143. }
  144. sk_SSL_CIPHER_free(cipher_stack);
  145. if (!ret)
  146. return 0;
  147. /* Check we have at least one EC supported group */
  148. tls1_get_supported_groups(s, &pgroups, &num_groups);
  149. for (j = 0; j < num_groups; j++) {
  150. uint16_t ctmp = pgroups[j];
  151. if (tls_valid_group(s, ctmp, min_version, max_version, 1, NULL)
  152. && tls_group_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED))
  153. return 1;
  154. }
  155. return 0;
  156. }
  157. EXT_RETURN tls_construct_ctos_ec_pt_formats(SSL_CONNECTION *s, WPACKET *pkt,
  158. unsigned int context, X509 *x,
  159. size_t chainidx)
  160. {
  161. const unsigned char *pformats;
  162. size_t num_formats;
  163. int reason, min_version, max_version;
  164. reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);
  165. if (reason != 0) {
  166. SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason);
  167. return EXT_RETURN_FAIL;
  168. }
  169. if (!use_ecc(s, min_version, max_version))
  170. return EXT_RETURN_NOT_SENT;
  171. /* Add TLS extension ECPointFormats to the ClientHello message */
  172. tls1_get_formatlist(s, &pformats, &num_formats);
  173. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
  174. /* Sub-packet for formats extension */
  175. || !WPACKET_start_sub_packet_u16(pkt)
  176. || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats)
  177. || !WPACKET_close(pkt)) {
  178. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  179. return EXT_RETURN_FAIL;
  180. }
  181. return EXT_RETURN_SENT;
  182. }
  183. EXT_RETURN tls_construct_ctos_supported_groups(SSL_CONNECTION *s, WPACKET *pkt,
  184. unsigned int context, X509 *x,
  185. size_t chainidx)
  186. {
  187. const uint16_t *pgroups = NULL;
  188. size_t num_groups = 0, i, tls13added = 0, added = 0;
  189. int min_version, max_version, reason;
  190. reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);
  191. if (reason != 0) {
  192. SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason);
  193. return EXT_RETURN_FAIL;
  194. }
  195. /*
  196. * We only support EC groups in TLSv1.2 or below, and in DTLS. Therefore
  197. * if we don't have EC support then we don't send this extension.
  198. */
  199. if (!use_ecc(s, min_version, max_version)
  200. && (SSL_CONNECTION_IS_DTLS(s) || max_version < TLS1_3_VERSION))
  201. return EXT_RETURN_NOT_SENT;
  202. /*
  203. * Add TLS extension supported_groups to the ClientHello message
  204. */
  205. tls1_get_supported_groups(s, &pgroups, &num_groups);
  206. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)
  207. /* Sub-packet for supported_groups extension */
  208. || !WPACKET_start_sub_packet_u16(pkt)
  209. || !WPACKET_start_sub_packet_u16(pkt)
  210. || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)) {
  211. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  212. return EXT_RETURN_FAIL;
  213. }
  214. /* Copy group ID if supported */
  215. for (i = 0; i < num_groups; i++) {
  216. uint16_t ctmp = pgroups[i];
  217. int okfortls13;
  218. if (tls_valid_group(s, ctmp, min_version, max_version, 0, &okfortls13)
  219. && tls_group_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED)) {
  220. if (!WPACKET_put_bytes_u16(pkt, ctmp)) {
  221. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  222. return EXT_RETURN_FAIL;
  223. }
  224. if (okfortls13 && max_version == TLS1_3_VERSION)
  225. tls13added++;
  226. added++;
  227. }
  228. }
  229. if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
  230. if (added == 0)
  231. SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_GROUPS,
  232. "No groups enabled for max supported SSL/TLS version");
  233. else
  234. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  235. return EXT_RETURN_FAIL;
  236. }
  237. if (tls13added == 0 && max_version == TLS1_3_VERSION) {
  238. SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_GROUPS,
  239. "No groups enabled for max supported SSL/TLS version");
  240. return EXT_RETURN_FAIL;
  241. }
  242. return EXT_RETURN_SENT;
  243. }
  244. EXT_RETURN tls_construct_ctos_session_ticket(SSL_CONNECTION *s, WPACKET *pkt,
  245. unsigned int context, X509 *x,
  246. size_t chainidx)
  247. {
  248. size_t ticklen;
  249. if (!tls_use_ticket(s))
  250. return EXT_RETURN_NOT_SENT;
  251. if (!s->new_session && s->session != NULL
  252. && s->session->ext.tick != NULL
  253. && s->session->ssl_version != TLS1_3_VERSION) {
  254. ticklen = s->session->ext.ticklen;
  255. } else if (s->session && s->ext.session_ticket != NULL
  256. && s->ext.session_ticket->data != NULL) {
  257. ticklen = s->ext.session_ticket->length;
  258. s->session->ext.tick = OPENSSL_malloc(ticklen);
  259. if (s->session->ext.tick == NULL) {
  260. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  261. return EXT_RETURN_FAIL;
  262. }
  263. memcpy(s->session->ext.tick,
  264. s->ext.session_ticket->data, ticklen);
  265. s->session->ext.ticklen = ticklen;
  266. } else {
  267. ticklen = 0;
  268. }
  269. if (ticklen == 0 && s->ext.session_ticket != NULL &&
  270. s->ext.session_ticket->data == NULL)
  271. return EXT_RETURN_NOT_SENT;
  272. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
  273. || !WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick, ticklen)) {
  274. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  275. return EXT_RETURN_FAIL;
  276. }
  277. return EXT_RETURN_SENT;
  278. }
  279. EXT_RETURN tls_construct_ctos_sig_algs(SSL_CONNECTION *s, WPACKET *pkt,
  280. unsigned int context, X509 *x,
  281. size_t chainidx)
  282. {
  283. size_t salglen;
  284. const uint16_t *salg;
  285. if (!SSL_CLIENT_USE_SIGALGS(s))
  286. return EXT_RETURN_NOT_SENT;
  287. salglen = tls12_get_psigalgs(s, 1, &salg);
  288. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms)
  289. /* Sub-packet for sig-algs extension */
  290. || !WPACKET_start_sub_packet_u16(pkt)
  291. /* Sub-packet for the actual list */
  292. || !WPACKET_start_sub_packet_u16(pkt)
  293. || !tls12_copy_sigalgs(s, pkt, salg, salglen)
  294. || !WPACKET_close(pkt)
  295. || !WPACKET_close(pkt)) {
  296. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  297. return EXT_RETURN_FAIL;
  298. }
  299. return EXT_RETURN_SENT;
  300. }
  301. #ifndef OPENSSL_NO_OCSP
  302. EXT_RETURN tls_construct_ctos_status_request(SSL_CONNECTION *s, WPACKET *pkt,
  303. unsigned int context, X509 *x,
  304. size_t chainidx)
  305. {
  306. int i;
  307. /* This extension isn't defined for client Certificates */
  308. if (x != NULL)
  309. return EXT_RETURN_NOT_SENT;
  310. if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp)
  311. return EXT_RETURN_NOT_SENT;
  312. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
  313. /* Sub-packet for status request extension */
  314. || !WPACKET_start_sub_packet_u16(pkt)
  315. || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp)
  316. /* Sub-packet for the ids */
  317. || !WPACKET_start_sub_packet_u16(pkt)) {
  318. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  319. return EXT_RETURN_FAIL;
  320. }
  321. for (i = 0; i < sk_OCSP_RESPID_num(s->ext.ocsp.ids); i++) {
  322. unsigned char *idbytes;
  323. OCSP_RESPID *id = sk_OCSP_RESPID_value(s->ext.ocsp.ids, i);
  324. int idlen = i2d_OCSP_RESPID(id, NULL);
  325. if (idlen <= 0
  326. /* Sub-packet for an individual id */
  327. || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes)
  328. || i2d_OCSP_RESPID(id, &idbytes) != idlen) {
  329. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  330. return EXT_RETURN_FAIL;
  331. }
  332. }
  333. if (!WPACKET_close(pkt)
  334. || !WPACKET_start_sub_packet_u16(pkt)) {
  335. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  336. return EXT_RETURN_FAIL;
  337. }
  338. if (s->ext.ocsp.exts) {
  339. unsigned char *extbytes;
  340. int extlen = i2d_X509_EXTENSIONS(s->ext.ocsp.exts, NULL);
  341. if (extlen < 0) {
  342. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  343. return EXT_RETURN_FAIL;
  344. }
  345. if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes)
  346. || i2d_X509_EXTENSIONS(s->ext.ocsp.exts, &extbytes)
  347. != extlen) {
  348. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  349. return EXT_RETURN_FAIL;
  350. }
  351. }
  352. if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
  353. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  354. return EXT_RETURN_FAIL;
  355. }
  356. return EXT_RETURN_SENT;
  357. }
  358. #endif
  359. #ifndef OPENSSL_NO_NEXTPROTONEG
  360. EXT_RETURN tls_construct_ctos_npn(SSL_CONNECTION *s, WPACKET *pkt,
  361. unsigned int context,
  362. X509 *x, size_t chainidx)
  363. {
  364. if (SSL_CONNECTION_GET_CTX(s)->ext.npn_select_cb == NULL
  365. || !SSL_IS_FIRST_HANDSHAKE(s))
  366. return EXT_RETURN_NOT_SENT;
  367. /*
  368. * The client advertises an empty extension to indicate its support
  369. * for Next Protocol Negotiation
  370. */
  371. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
  372. || !WPACKET_put_bytes_u16(pkt, 0)) {
  373. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  374. return EXT_RETURN_FAIL;
  375. }
  376. return EXT_RETURN_SENT;
  377. }
  378. #endif
  379. EXT_RETURN tls_construct_ctos_alpn(SSL_CONNECTION *s, WPACKET *pkt,
  380. unsigned int context,
  381. X509 *x, size_t chainidx)
  382. {
  383. s->s3.alpn_sent = 0;
  384. if (s->ext.alpn == NULL || !SSL_IS_FIRST_HANDSHAKE(s))
  385. return EXT_RETURN_NOT_SENT;
  386. if (!WPACKET_put_bytes_u16(pkt,
  387. TLSEXT_TYPE_application_layer_protocol_negotiation)
  388. /* Sub-packet ALPN extension */
  389. || !WPACKET_start_sub_packet_u16(pkt)
  390. || !WPACKET_sub_memcpy_u16(pkt, s->ext.alpn, s->ext.alpn_len)
  391. || !WPACKET_close(pkt)) {
  392. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  393. return EXT_RETURN_FAIL;
  394. }
  395. s->s3.alpn_sent = 1;
  396. return EXT_RETURN_SENT;
  397. }
  398. #ifndef OPENSSL_NO_SRTP
  399. EXT_RETURN tls_construct_ctos_use_srtp(SSL_CONNECTION *s, WPACKET *pkt,
  400. unsigned int context, X509 *x,
  401. size_t chainidx)
  402. {
  403. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  404. STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = SSL_get_srtp_profiles(ssl);
  405. int i, end;
  406. if (clnt == NULL)
  407. return EXT_RETURN_NOT_SENT;
  408. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
  409. /* Sub-packet for SRTP extension */
  410. || !WPACKET_start_sub_packet_u16(pkt)
  411. /* Sub-packet for the protection profile list */
  412. || !WPACKET_start_sub_packet_u16(pkt)) {
  413. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  414. return EXT_RETURN_FAIL;
  415. }
  416. end = sk_SRTP_PROTECTION_PROFILE_num(clnt);
  417. for (i = 0; i < end; i++) {
  418. const SRTP_PROTECTION_PROFILE *prof =
  419. sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
  420. if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) {
  421. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  422. return EXT_RETURN_FAIL;
  423. }
  424. }
  425. if (!WPACKET_close(pkt)
  426. /* Add an empty use_mki value */
  427. || !WPACKET_put_bytes_u8(pkt, 0)
  428. || !WPACKET_close(pkt)) {
  429. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  430. return EXT_RETURN_FAIL;
  431. }
  432. return EXT_RETURN_SENT;
  433. }
  434. #endif
  435. EXT_RETURN tls_construct_ctos_etm(SSL_CONNECTION *s, WPACKET *pkt,
  436. unsigned int context,
  437. X509 *x, size_t chainidx)
  438. {
  439. if (s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
  440. return EXT_RETURN_NOT_SENT;
  441. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
  442. || !WPACKET_put_bytes_u16(pkt, 0)) {
  443. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  444. return EXT_RETURN_FAIL;
  445. }
  446. return EXT_RETURN_SENT;
  447. }
  448. #ifndef OPENSSL_NO_CT
  449. EXT_RETURN tls_construct_ctos_sct(SSL_CONNECTION *s, WPACKET *pkt,
  450. unsigned int context,
  451. X509 *x, size_t chainidx)
  452. {
  453. if (s->ct_validation_callback == NULL)
  454. return EXT_RETURN_NOT_SENT;
  455. /* Not defined for client Certificates */
  456. if (x != NULL)
  457. return EXT_RETURN_NOT_SENT;
  458. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp)
  459. || !WPACKET_put_bytes_u16(pkt, 0)) {
  460. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  461. return EXT_RETURN_FAIL;
  462. }
  463. return EXT_RETURN_SENT;
  464. }
  465. #endif
  466. EXT_RETURN tls_construct_ctos_ems(SSL_CONNECTION *s, WPACKET *pkt,
  467. unsigned int context,
  468. X509 *x, size_t chainidx)
  469. {
  470. if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET)
  471. return EXT_RETURN_NOT_SENT;
  472. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
  473. || !WPACKET_put_bytes_u16(pkt, 0)) {
  474. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  475. return EXT_RETURN_FAIL;
  476. }
  477. return EXT_RETURN_SENT;
  478. }
  479. EXT_RETURN tls_construct_ctos_supported_versions(SSL_CONNECTION *s, WPACKET *pkt,
  480. unsigned int context, X509 *x,
  481. size_t chainidx)
  482. {
  483. int currv, min_version, max_version, reason;
  484. reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);
  485. if (reason != 0) {
  486. SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason);
  487. return EXT_RETURN_FAIL;
  488. }
  489. /*
  490. * Don't include this if we can't negotiate TLSv1.3. We can do a straight
  491. * comparison here because we will never be called in DTLS.
  492. */
  493. if (max_version < TLS1_3_VERSION)
  494. return EXT_RETURN_NOT_SENT;
  495. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
  496. || !WPACKET_start_sub_packet_u16(pkt)
  497. || !WPACKET_start_sub_packet_u8(pkt)) {
  498. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  499. return EXT_RETURN_FAIL;
  500. }
  501. for (currv = max_version; currv >= min_version; currv--) {
  502. if (!WPACKET_put_bytes_u16(pkt, currv)) {
  503. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  504. return EXT_RETURN_FAIL;
  505. }
  506. }
  507. if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
  508. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  509. return EXT_RETURN_FAIL;
  510. }
  511. return EXT_RETURN_SENT;
  512. }
  513. /*
  514. * Construct a psk_kex_modes extension.
  515. */
  516. EXT_RETURN tls_construct_ctos_psk_kex_modes(SSL_CONNECTION *s, WPACKET *pkt,
  517. unsigned int context, X509 *x,
  518. size_t chainidx)
  519. {
  520. #ifndef OPENSSL_NO_TLS1_3
  521. int nodhe = s->options & SSL_OP_ALLOW_NO_DHE_KEX;
  522. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk_kex_modes)
  523. || !WPACKET_start_sub_packet_u16(pkt)
  524. || !WPACKET_start_sub_packet_u8(pkt)
  525. || !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE_DHE)
  526. || (nodhe && !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE))
  527. || !WPACKET_close(pkt)
  528. || !WPACKET_close(pkt)) {
  529. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  530. return EXT_RETURN_FAIL;
  531. }
  532. s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_KE_DHE;
  533. if (nodhe)
  534. s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE;
  535. #endif
  536. return EXT_RETURN_SENT;
  537. }
  538. #ifndef OPENSSL_NO_TLS1_3
  539. static int add_key_share(SSL_CONNECTION *s, WPACKET *pkt, unsigned int curve_id)
  540. {
  541. unsigned char *encoded_point = NULL;
  542. EVP_PKEY *key_share_key = NULL;
  543. size_t encodedlen;
  544. if (s->s3.tmp.pkey != NULL) {
  545. if (!ossl_assert(s->hello_retry_request == SSL_HRR_PENDING)) {
  546. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  547. return 0;
  548. }
  549. /*
  550. * Could happen if we got an HRR that wasn't requesting a new key_share
  551. */
  552. key_share_key = s->s3.tmp.pkey;
  553. } else {
  554. key_share_key = ssl_generate_pkey_group(s, curve_id);
  555. if (key_share_key == NULL) {
  556. /* SSLfatal() already called */
  557. return 0;
  558. }
  559. }
  560. /* Encode the public key. */
  561. encodedlen = EVP_PKEY_get1_encoded_public_key(key_share_key,
  562. &encoded_point);
  563. if (encodedlen == 0) {
  564. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
  565. goto err;
  566. }
  567. /* Create KeyShareEntry */
  568. if (!WPACKET_put_bytes_u16(pkt, curve_id)
  569. || !WPACKET_sub_memcpy_u16(pkt, encoded_point, encodedlen)) {
  570. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  571. goto err;
  572. }
  573. /*
  574. * When changing to send more than one key_share we're
  575. * going to need to be able to save more than one EVP_PKEY. For now
  576. * we reuse the existing tmp.pkey
  577. */
  578. s->s3.tmp.pkey = key_share_key;
  579. s->s3.group_id = curve_id;
  580. OPENSSL_free(encoded_point);
  581. return 1;
  582. err:
  583. if (s->s3.tmp.pkey == NULL)
  584. EVP_PKEY_free(key_share_key);
  585. OPENSSL_free(encoded_point);
  586. return 0;
  587. }
  588. #endif
  589. EXT_RETURN tls_construct_ctos_key_share(SSL_CONNECTION *s, WPACKET *pkt,
  590. unsigned int context, X509 *x,
  591. size_t chainidx)
  592. {
  593. #ifndef OPENSSL_NO_TLS1_3
  594. size_t i, num_groups = 0;
  595. const uint16_t *pgroups = NULL;
  596. uint16_t curve_id = 0;
  597. /* key_share extension */
  598. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
  599. /* Extension data sub-packet */
  600. || !WPACKET_start_sub_packet_u16(pkt)
  601. /* KeyShare list sub-packet */
  602. || !WPACKET_start_sub_packet_u16(pkt)) {
  603. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  604. return EXT_RETURN_FAIL;
  605. }
  606. tls1_get_supported_groups(s, &pgroups, &num_groups);
  607. /*
  608. * Make the number of key_shares sent configurable. For
  609. * now, we just send one
  610. */
  611. if (s->s3.group_id != 0) {
  612. curve_id = s->s3.group_id;
  613. } else {
  614. for (i = 0; i < num_groups; i++) {
  615. if (!tls_group_allowed(s, pgroups[i], SSL_SECOP_CURVE_SUPPORTED))
  616. continue;
  617. if (!tls_valid_group(s, pgroups[i], TLS1_3_VERSION, TLS1_3_VERSION,
  618. 0, NULL))
  619. continue;
  620. curve_id = pgroups[i];
  621. break;
  622. }
  623. }
  624. if (curve_id == 0) {
  625. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_KEY_SHARE);
  626. return EXT_RETURN_FAIL;
  627. }
  628. if (!add_key_share(s, pkt, curve_id)) {
  629. /* SSLfatal() already called */
  630. return EXT_RETURN_FAIL;
  631. }
  632. if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
  633. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  634. return EXT_RETURN_FAIL;
  635. }
  636. return EXT_RETURN_SENT;
  637. #else
  638. return EXT_RETURN_NOT_SENT;
  639. #endif
  640. }
  641. EXT_RETURN tls_construct_ctos_cookie(SSL_CONNECTION *s, WPACKET *pkt,
  642. unsigned int context,
  643. X509 *x, size_t chainidx)
  644. {
  645. EXT_RETURN ret = EXT_RETURN_FAIL;
  646. /* Should only be set if we've had an HRR */
  647. if (s->ext.tls13_cookie_len == 0)
  648. return EXT_RETURN_NOT_SENT;
  649. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie)
  650. /* Extension data sub-packet */
  651. || !WPACKET_start_sub_packet_u16(pkt)
  652. || !WPACKET_sub_memcpy_u16(pkt, s->ext.tls13_cookie,
  653. s->ext.tls13_cookie_len)
  654. || !WPACKET_close(pkt)) {
  655. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  656. goto end;
  657. }
  658. ret = EXT_RETURN_SENT;
  659. end:
  660. OPENSSL_free(s->ext.tls13_cookie);
  661. s->ext.tls13_cookie = NULL;
  662. s->ext.tls13_cookie_len = 0;
  663. return ret;
  664. }
  665. EXT_RETURN tls_construct_ctos_early_data(SSL_CONNECTION *s, WPACKET *pkt,
  666. unsigned int context, X509 *x,
  667. size_t chainidx)
  668. {
  669. #ifndef OPENSSL_NO_PSK
  670. char identity[PSK_MAX_IDENTITY_LEN + 1];
  671. #endif /* OPENSSL_NO_PSK */
  672. const unsigned char *id = NULL;
  673. size_t idlen = 0;
  674. SSL_SESSION *psksess = NULL;
  675. SSL_SESSION *edsess = NULL;
  676. const EVP_MD *handmd = NULL;
  677. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  678. if (s->hello_retry_request == SSL_HRR_PENDING)
  679. handmd = ssl_handshake_md(s);
  680. if (s->psk_use_session_cb != NULL
  681. && (!s->psk_use_session_cb(ssl, handmd, &id, &idlen, &psksess)
  682. || (psksess != NULL
  683. && psksess->ssl_version != TLS1_3_VERSION))) {
  684. SSL_SESSION_free(psksess);
  685. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
  686. return EXT_RETURN_FAIL;
  687. }
  688. #ifndef OPENSSL_NO_PSK
  689. if (psksess == NULL && s->psk_client_callback != NULL) {
  690. unsigned char psk[PSK_MAX_PSK_LEN];
  691. size_t psklen = 0;
  692. memset(identity, 0, sizeof(identity));
  693. psklen = s->psk_client_callback(ssl, NULL,
  694. identity, sizeof(identity) - 1,
  695. psk, sizeof(psk));
  696. if (psklen > PSK_MAX_PSK_LEN) {
  697. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, ERR_R_INTERNAL_ERROR);
  698. return EXT_RETURN_FAIL;
  699. } else if (psklen > 0) {
  700. const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 };
  701. const SSL_CIPHER *cipher;
  702. idlen = strlen(identity);
  703. if (idlen > PSK_MAX_IDENTITY_LEN) {
  704. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  705. return EXT_RETURN_FAIL;
  706. }
  707. id = (unsigned char *)identity;
  708. /*
  709. * We found a PSK using an old style callback. We don't know
  710. * the digest so we default to SHA256 as per the TLSv1.3 spec
  711. */
  712. cipher = SSL_CIPHER_find(ssl, tls13_aes128gcmsha256_id);
  713. if (cipher == NULL) {
  714. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  715. return EXT_RETURN_FAIL;
  716. }
  717. psksess = SSL_SESSION_new();
  718. if (psksess == NULL
  719. || !SSL_SESSION_set1_master_key(psksess, psk, psklen)
  720. || !SSL_SESSION_set_cipher(psksess, cipher)
  721. || !SSL_SESSION_set_protocol_version(psksess, TLS1_3_VERSION)) {
  722. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  723. OPENSSL_cleanse(psk, psklen);
  724. return EXT_RETURN_FAIL;
  725. }
  726. OPENSSL_cleanse(psk, psklen);
  727. }
  728. }
  729. #endif /* OPENSSL_NO_PSK */
  730. SSL_SESSION_free(s->psksession);
  731. s->psksession = psksess;
  732. if (psksess != NULL) {
  733. OPENSSL_free(s->psksession_id);
  734. s->psksession_id = OPENSSL_memdup(id, idlen);
  735. if (s->psksession_id == NULL) {
  736. s->psksession_id_len = 0;
  737. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  738. return EXT_RETURN_FAIL;
  739. }
  740. s->psksession_id_len = idlen;
  741. }
  742. if (s->early_data_state != SSL_EARLY_DATA_CONNECTING
  743. || (s->session->ext.max_early_data == 0
  744. && (psksess == NULL || psksess->ext.max_early_data == 0))) {
  745. s->max_early_data = 0;
  746. return EXT_RETURN_NOT_SENT;
  747. }
  748. edsess = s->session->ext.max_early_data != 0 ? s->session : psksess;
  749. s->max_early_data = edsess->ext.max_early_data;
  750. if (edsess->ext.hostname != NULL) {
  751. if (s->ext.hostname == NULL
  752. || (s->ext.hostname != NULL
  753. && strcmp(s->ext.hostname, edsess->ext.hostname) != 0)) {
  754. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  755. SSL_R_INCONSISTENT_EARLY_DATA_SNI);
  756. return EXT_RETURN_FAIL;
  757. }
  758. }
  759. if ((s->ext.alpn == NULL && edsess->ext.alpn_selected != NULL)) {
  760. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_INCONSISTENT_EARLY_DATA_ALPN);
  761. return EXT_RETURN_FAIL;
  762. }
  763. /*
  764. * Verify that we are offering an ALPN protocol consistent with the early
  765. * data.
  766. */
  767. if (edsess->ext.alpn_selected != NULL) {
  768. PACKET prots, alpnpkt;
  769. int found = 0;
  770. if (!PACKET_buf_init(&prots, s->ext.alpn, s->ext.alpn_len)) {
  771. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  772. return EXT_RETURN_FAIL;
  773. }
  774. while (PACKET_get_length_prefixed_1(&prots, &alpnpkt)) {
  775. if (PACKET_equal(&alpnpkt, edsess->ext.alpn_selected,
  776. edsess->ext.alpn_selected_len)) {
  777. found = 1;
  778. break;
  779. }
  780. }
  781. if (!found) {
  782. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  783. SSL_R_INCONSISTENT_EARLY_DATA_ALPN);
  784. return EXT_RETURN_FAIL;
  785. }
  786. }
  787. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
  788. || !WPACKET_start_sub_packet_u16(pkt)
  789. || !WPACKET_close(pkt)) {
  790. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  791. return EXT_RETURN_FAIL;
  792. }
  793. /*
  794. * We set this to rejected here. Later, if the server acknowledges the
  795. * extension, we set it to accepted.
  796. */
  797. s->ext.early_data = SSL_EARLY_DATA_REJECTED;
  798. s->ext.early_data_ok = 1;
  799. return EXT_RETURN_SENT;
  800. }
  801. #define F5_WORKAROUND_MIN_MSG_LEN 0xff
  802. #define F5_WORKAROUND_MAX_MSG_LEN 0x200
  803. /*
  804. * PSK pre binder overhead =
  805. * 2 bytes for TLSEXT_TYPE_psk
  806. * 2 bytes for extension length
  807. * 2 bytes for identities list length
  808. * 2 bytes for identity length
  809. * 4 bytes for obfuscated_ticket_age
  810. * 2 bytes for binder list length
  811. * 1 byte for binder length
  812. * The above excludes the number of bytes for the identity itself and the
  813. * subsequent binder bytes
  814. */
  815. #define PSK_PRE_BINDER_OVERHEAD (2 + 2 + 2 + 2 + 4 + 2 + 1)
  816. EXT_RETURN tls_construct_ctos_padding(SSL_CONNECTION *s, WPACKET *pkt,
  817. unsigned int context, X509 *x,
  818. size_t chainidx)
  819. {
  820. unsigned char *padbytes;
  821. size_t hlen;
  822. if ((s->options & SSL_OP_TLSEXT_PADDING) == 0)
  823. return EXT_RETURN_NOT_SENT;
  824. /*
  825. * Add padding to workaround bugs in F5 terminators. See RFC7685.
  826. * This code calculates the length of all extensions added so far but
  827. * excludes the PSK extension (because that MUST be written last). Therefore
  828. * this extension MUST always appear second to last.
  829. */
  830. if (!WPACKET_get_total_written(pkt, &hlen)) {
  831. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  832. return EXT_RETURN_FAIL;
  833. }
  834. /*
  835. * If we're going to send a PSK then that will be written out after this
  836. * extension, so we need to calculate how long it is going to be.
  837. */
  838. if (s->session->ssl_version == TLS1_3_VERSION
  839. && s->session->ext.ticklen != 0
  840. && s->session->cipher != NULL) {
  841. const EVP_MD *md = ssl_md(SSL_CONNECTION_GET_CTX(s),
  842. s->session->cipher->algorithm2);
  843. if (md != NULL) {
  844. /*
  845. * Add the fixed PSK overhead, the identity length and the binder
  846. * length.
  847. */
  848. hlen += PSK_PRE_BINDER_OVERHEAD + s->session->ext.ticklen
  849. + EVP_MD_get_size(md);
  850. }
  851. }
  852. if (hlen > F5_WORKAROUND_MIN_MSG_LEN && hlen < F5_WORKAROUND_MAX_MSG_LEN) {
  853. /* Calculate the amount of padding we need to add */
  854. hlen = F5_WORKAROUND_MAX_MSG_LEN - hlen;
  855. /*
  856. * Take off the size of extension header itself (2 bytes for type and
  857. * 2 bytes for length bytes), but ensure that the extension is at least
  858. * 1 byte long so as not to have an empty extension last (WebSphere 7.x,
  859. * 8.x are intolerant of that condition)
  860. */
  861. if (hlen > 4)
  862. hlen -= 4;
  863. else
  864. hlen = 1;
  865. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding)
  866. || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) {
  867. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  868. return EXT_RETURN_FAIL;
  869. }
  870. memset(padbytes, 0, hlen);
  871. }
  872. return EXT_RETURN_SENT;
  873. }
  874. /*
  875. * Construct the pre_shared_key extension
  876. */
  877. EXT_RETURN tls_construct_ctos_psk(SSL_CONNECTION *s, WPACKET *pkt,
  878. unsigned int context,
  879. X509 *x, size_t chainidx)
  880. {
  881. #ifndef OPENSSL_NO_TLS1_3
  882. uint32_t agesec, agems = 0;
  883. size_t reshashsize = 0, pskhashsize = 0, binderoffset, msglen;
  884. unsigned char *resbinder = NULL, *pskbinder = NULL, *msgstart = NULL;
  885. const EVP_MD *handmd = NULL, *mdres = NULL, *mdpsk = NULL;
  886. int dores = 0;
  887. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  888. OSSL_TIME t;
  889. s->ext.tick_identity = 0;
  890. /*
  891. * Note: At this stage of the code we only support adding a single
  892. * resumption PSK. If we add support for multiple PSKs then the length
  893. * calculations in the padding extension will need to be adjusted.
  894. */
  895. /*
  896. * If this is an incompatible or new session then we have nothing to resume
  897. * so don't add this extension.
  898. */
  899. if (s->session->ssl_version != TLS1_3_VERSION
  900. || (s->session->ext.ticklen == 0 && s->psksession == NULL))
  901. return EXT_RETURN_NOT_SENT;
  902. if (s->hello_retry_request == SSL_HRR_PENDING)
  903. handmd = ssl_handshake_md(s);
  904. if (s->session->ext.ticklen != 0) {
  905. /* Get the digest associated with the ciphersuite in the session */
  906. if (s->session->cipher == NULL) {
  907. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  908. return EXT_RETURN_FAIL;
  909. }
  910. mdres = ssl_md(sctx, s->session->cipher->algorithm2);
  911. if (mdres == NULL) {
  912. /*
  913. * Don't recognize this cipher so we can't use the session.
  914. * Ignore it
  915. */
  916. goto dopsksess;
  917. }
  918. if (s->hello_retry_request == SSL_HRR_PENDING && mdres != handmd) {
  919. /*
  920. * Selected ciphersuite hash does not match the hash for the session
  921. * so we can't use it.
  922. */
  923. goto dopsksess;
  924. }
  925. /*
  926. * Technically the C standard just says time() returns a time_t and says
  927. * nothing about the encoding of that type. In practice most
  928. * implementations follow POSIX which holds it as an integral type in
  929. * seconds since epoch. We've already made the assumption that we can do
  930. * this in multiple places in the code, so portability shouldn't be an
  931. * issue.
  932. */
  933. t = ossl_time_subtract(ossl_time_now(), s->session->time);
  934. agesec = (uint32_t)ossl_time2seconds(t);
  935. /*
  936. * We calculate the age in seconds but the server may work in ms. Due to
  937. * rounding errors we could overestimate the age by up to 1s. It is
  938. * better to underestimate it. Otherwise, if the RTT is very short, when
  939. * the server calculates the age reported by the client it could be
  940. * bigger than the age calculated on the server - which should never
  941. * happen.
  942. */
  943. if (agesec > 0)
  944. agesec--;
  945. if (s->session->ext.tick_lifetime_hint < agesec) {
  946. /* Ticket is too old. Ignore it. */
  947. goto dopsksess;
  948. }
  949. /*
  950. * Calculate age in ms. We're just doing it to nearest second. Should be
  951. * good enough.
  952. */
  953. agems = agesec * (uint32_t)1000;
  954. if (agesec != 0 && agems / (uint32_t)1000 != agesec) {
  955. /*
  956. * Overflow. Shouldn't happen unless this is a *really* old session.
  957. * If so we just ignore it.
  958. */
  959. goto dopsksess;
  960. }
  961. /*
  962. * Obfuscate the age. Overflow here is fine, this addition is supposed
  963. * to be mod 2^32.
  964. */
  965. agems += s->session->ext.tick_age_add;
  966. reshashsize = EVP_MD_get_size(mdres);
  967. s->ext.tick_identity++;
  968. dores = 1;
  969. }
  970. dopsksess:
  971. if (!dores && s->psksession == NULL)
  972. return EXT_RETURN_NOT_SENT;
  973. if (s->psksession != NULL) {
  974. mdpsk = ssl_md(sctx, s->psksession->cipher->algorithm2);
  975. if (mdpsk == NULL) {
  976. /*
  977. * Don't recognize this cipher so we can't use the session.
  978. * If this happens it's an application bug.
  979. */
  980. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
  981. return EXT_RETURN_FAIL;
  982. }
  983. if (s->hello_retry_request == SSL_HRR_PENDING && mdpsk != handmd) {
  984. /*
  985. * Selected ciphersuite hash does not match the hash for the PSK
  986. * session. This is an application bug.
  987. */
  988. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
  989. return EXT_RETURN_FAIL;
  990. }
  991. pskhashsize = EVP_MD_get_size(mdpsk);
  992. }
  993. /* Create the extension, but skip over the binder for now */
  994. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)
  995. || !WPACKET_start_sub_packet_u16(pkt)
  996. || !WPACKET_start_sub_packet_u16(pkt)) {
  997. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  998. return EXT_RETURN_FAIL;
  999. }
  1000. if (dores) {
  1001. if (!WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick,
  1002. s->session->ext.ticklen)
  1003. || !WPACKET_put_bytes_u32(pkt, agems)) {
  1004. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1005. return EXT_RETURN_FAIL;
  1006. }
  1007. }
  1008. if (s->psksession != NULL) {
  1009. if (!WPACKET_sub_memcpy_u16(pkt, s->psksession_id,
  1010. s->psksession_id_len)
  1011. || !WPACKET_put_bytes_u32(pkt, 0)) {
  1012. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1013. return EXT_RETURN_FAIL;
  1014. }
  1015. s->ext.tick_identity++;
  1016. }
  1017. if (!WPACKET_close(pkt)
  1018. || !WPACKET_get_total_written(pkt, &binderoffset)
  1019. || !WPACKET_start_sub_packet_u16(pkt)
  1020. || (dores
  1021. && !WPACKET_sub_allocate_bytes_u8(pkt, reshashsize, &resbinder))
  1022. || (s->psksession != NULL
  1023. && !WPACKET_sub_allocate_bytes_u8(pkt, pskhashsize, &pskbinder))
  1024. || !WPACKET_close(pkt)
  1025. || !WPACKET_close(pkt)
  1026. || !WPACKET_get_total_written(pkt, &msglen)
  1027. /*
  1028. * We need to fill in all the sub-packet lengths now so we can
  1029. * calculate the HMAC of the message up to the binders
  1030. */
  1031. || !WPACKET_fill_lengths(pkt)) {
  1032. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1033. return EXT_RETURN_FAIL;
  1034. }
  1035. msgstart = WPACKET_get_curr(pkt) - msglen;
  1036. if (dores
  1037. && tls_psk_do_binder(s, mdres, msgstart, binderoffset, NULL,
  1038. resbinder, s->session, 1, 0) != 1) {
  1039. /* SSLfatal() already called */
  1040. return EXT_RETURN_FAIL;
  1041. }
  1042. if (s->psksession != NULL
  1043. && tls_psk_do_binder(s, mdpsk, msgstart, binderoffset, NULL,
  1044. pskbinder, s->psksession, 1, 1) != 1) {
  1045. /* SSLfatal() already called */
  1046. return EXT_RETURN_FAIL;
  1047. }
  1048. return EXT_RETURN_SENT;
  1049. #else
  1050. return EXT_RETURN_NOT_SENT;
  1051. #endif
  1052. }
  1053. EXT_RETURN tls_construct_ctos_post_handshake_auth(SSL_CONNECTION *s, WPACKET *pkt,
  1054. ossl_unused unsigned int context,
  1055. ossl_unused X509 *x,
  1056. ossl_unused size_t chainidx)
  1057. {
  1058. #ifndef OPENSSL_NO_TLS1_3
  1059. if (!s->pha_enabled)
  1060. return EXT_RETURN_NOT_SENT;
  1061. /* construct extension - 0 length, no contents */
  1062. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_post_handshake_auth)
  1063. || !WPACKET_start_sub_packet_u16(pkt)
  1064. || !WPACKET_close(pkt)) {
  1065. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1066. return EXT_RETURN_FAIL;
  1067. }
  1068. s->post_handshake_auth = SSL_PHA_EXT_SENT;
  1069. return EXT_RETURN_SENT;
  1070. #else
  1071. return EXT_RETURN_NOT_SENT;
  1072. #endif
  1073. }
  1074. /*
  1075. * Parse the server's renegotiation binding and abort if it's not right
  1076. */
  1077. int tls_parse_stoc_renegotiate(SSL_CONNECTION *s, PACKET *pkt,
  1078. unsigned int context,
  1079. X509 *x, size_t chainidx)
  1080. {
  1081. size_t expected_len = s->s3.previous_client_finished_len
  1082. + s->s3.previous_server_finished_len;
  1083. size_t ilen;
  1084. const unsigned char *data;
  1085. /* Check for logic errors */
  1086. if (!ossl_assert(expected_len == 0
  1087. || s->s3.previous_client_finished_len != 0)
  1088. || !ossl_assert(expected_len == 0
  1089. || s->s3.previous_server_finished_len != 0)) {
  1090. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1091. return 0;
  1092. }
  1093. /* Parse the length byte */
  1094. if (!PACKET_get_1_len(pkt, &ilen)) {
  1095. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR);
  1096. return 0;
  1097. }
  1098. /* Consistency check */
  1099. if (PACKET_remaining(pkt) != ilen) {
  1100. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR);
  1101. return 0;
  1102. }
  1103. /* Check that the extension matches */
  1104. if (ilen != expected_len) {
  1105. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH);
  1106. return 0;
  1107. }
  1108. if (!PACKET_get_bytes(pkt, &data, s->s3.previous_client_finished_len)
  1109. || memcmp(data, s->s3.previous_client_finished,
  1110. s->s3.previous_client_finished_len) != 0) {
  1111. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH);
  1112. return 0;
  1113. }
  1114. if (!PACKET_get_bytes(pkt, &data, s->s3.previous_server_finished_len)
  1115. || memcmp(data, s->s3.previous_server_finished,
  1116. s->s3.previous_server_finished_len) != 0) {
  1117. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH);
  1118. return 0;
  1119. }
  1120. s->s3.send_connection_binding = 1;
  1121. return 1;
  1122. }
  1123. /* Parse the server's max fragment len extension packet */
  1124. int tls_parse_stoc_maxfragmentlen(SSL_CONNECTION *s, PACKET *pkt,
  1125. unsigned int context,
  1126. X509 *x, size_t chainidx)
  1127. {
  1128. unsigned int value;
  1129. if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) {
  1130. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
  1131. return 0;
  1132. }
  1133. /* |value| should contains a valid max-fragment-length code. */
  1134. if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) {
  1135. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1136. SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
  1137. return 0;
  1138. }
  1139. /* Must be the same value as client-configured one who was sent to server */
  1140. /*-
  1141. * RFC 6066: if a client receives a maximum fragment length negotiation
  1142. * response that differs from the length it requested, ...
  1143. * It must abort with SSL_AD_ILLEGAL_PARAMETER alert
  1144. */
  1145. if (value != s->ext.max_fragment_len_mode) {
  1146. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1147. SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
  1148. return 0;
  1149. }
  1150. /*
  1151. * Maximum Fragment Length Negotiation succeeded.
  1152. * The negotiated Maximum Fragment Length is binding now.
  1153. */
  1154. s->session->ext.max_fragment_len_mode = value;
  1155. return 1;
  1156. }
  1157. int tls_parse_stoc_server_name(SSL_CONNECTION *s, PACKET *pkt,
  1158. unsigned int context,
  1159. X509 *x, size_t chainidx)
  1160. {
  1161. if (s->ext.hostname == NULL) {
  1162. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1163. return 0;
  1164. }
  1165. if (PACKET_remaining(pkt) > 0) {
  1166. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
  1167. return 0;
  1168. }
  1169. if (!s->hit) {
  1170. if (s->session->ext.hostname != NULL) {
  1171. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1172. return 0;
  1173. }
  1174. s->session->ext.hostname = OPENSSL_strdup(s->ext.hostname);
  1175. if (s->session->ext.hostname == NULL) {
  1176. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1177. return 0;
  1178. }
  1179. }
  1180. return 1;
  1181. }
  1182. int tls_parse_stoc_ec_pt_formats(SSL_CONNECTION *s, PACKET *pkt,
  1183. unsigned int context,
  1184. X509 *x, size_t chainidx)
  1185. {
  1186. size_t ecpointformats_len;
  1187. PACKET ecptformatlist;
  1188. if (!PACKET_as_length_prefixed_1(pkt, &ecptformatlist)) {
  1189. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
  1190. return 0;
  1191. }
  1192. if (!s->hit) {
  1193. ecpointformats_len = PACKET_remaining(&ecptformatlist);
  1194. if (ecpointformats_len == 0) {
  1195. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
  1196. return 0;
  1197. }
  1198. s->ext.peer_ecpointformats_len = 0;
  1199. OPENSSL_free(s->ext.peer_ecpointformats);
  1200. s->ext.peer_ecpointformats = OPENSSL_malloc(ecpointformats_len);
  1201. if (s->ext.peer_ecpointformats == NULL) {
  1202. s->ext.peer_ecpointformats_len = 0;
  1203. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1204. return 0;
  1205. }
  1206. s->ext.peer_ecpointformats_len = ecpointformats_len;
  1207. if (!PACKET_copy_bytes(&ecptformatlist,
  1208. s->ext.peer_ecpointformats,
  1209. ecpointformats_len)) {
  1210. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1211. return 0;
  1212. }
  1213. }
  1214. return 1;
  1215. }
  1216. int tls_parse_stoc_session_ticket(SSL_CONNECTION *s, PACKET *pkt,
  1217. unsigned int context,
  1218. X509 *x, size_t chainidx)
  1219. {
  1220. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  1221. if (s->ext.session_ticket_cb != NULL &&
  1222. !s->ext.session_ticket_cb(ssl, PACKET_data(pkt),
  1223. PACKET_remaining(pkt),
  1224. s->ext.session_ticket_cb_arg)) {
  1225. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_EXTENSION);
  1226. return 0;
  1227. }
  1228. if (!tls_use_ticket(s)) {
  1229. SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
  1230. return 0;
  1231. }
  1232. if (PACKET_remaining(pkt) > 0) {
  1233. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
  1234. return 0;
  1235. }
  1236. s->ext.ticket_expected = 1;
  1237. return 1;
  1238. }
  1239. #ifndef OPENSSL_NO_OCSP
  1240. int tls_parse_stoc_status_request(SSL_CONNECTION *s, PACKET *pkt,
  1241. unsigned int context,
  1242. X509 *x, size_t chainidx)
  1243. {
  1244. if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) {
  1245. /* We ignore this if the server sends a CertificateRequest */
  1246. return 1;
  1247. }
  1248. /*
  1249. * MUST only be sent if we've requested a status
  1250. * request message. In TLS <= 1.2 it must also be empty.
  1251. */
  1252. if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) {
  1253. SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
  1254. return 0;
  1255. }
  1256. if (!SSL_CONNECTION_IS_TLS13(s) && PACKET_remaining(pkt) > 0) {
  1257. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
  1258. return 0;
  1259. }
  1260. if (SSL_CONNECTION_IS_TLS13(s)) {
  1261. /* We only know how to handle this if it's for the first Certificate in
  1262. * the chain. We ignore any other responses.
  1263. */
  1264. if (chainidx != 0)
  1265. return 1;
  1266. /* SSLfatal() already called */
  1267. return tls_process_cert_status_body(s, pkt);
  1268. }
  1269. /* Set flag to expect CertificateStatus message */
  1270. s->ext.status_expected = 1;
  1271. return 1;
  1272. }
  1273. #endif
  1274. #ifndef OPENSSL_NO_CT
  1275. int tls_parse_stoc_sct(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
  1276. X509 *x, size_t chainidx)
  1277. {
  1278. if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) {
  1279. /* We ignore this if the server sends it in a CertificateRequest */
  1280. return 1;
  1281. }
  1282. /*
  1283. * Only take it if we asked for it - i.e if there is no CT validation
  1284. * callback set, then a custom extension MAY be processing it, so we
  1285. * need to let control continue to flow to that.
  1286. */
  1287. if (s->ct_validation_callback != NULL) {
  1288. size_t size = PACKET_remaining(pkt);
  1289. /* Simply copy it off for later processing */
  1290. OPENSSL_free(s->ext.scts);
  1291. s->ext.scts = NULL;
  1292. s->ext.scts_len = (uint16_t)size;
  1293. if (size > 0) {
  1294. s->ext.scts = OPENSSL_malloc(size);
  1295. if (s->ext.scts == NULL) {
  1296. s->ext.scts_len = 0;
  1297. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  1298. return 0;
  1299. }
  1300. if (!PACKET_copy_bytes(pkt, s->ext.scts, size)) {
  1301. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1302. return 0;
  1303. }
  1304. }
  1305. } else {
  1306. ENDPOINT role = (context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0
  1307. ? ENDPOINT_CLIENT : ENDPOINT_BOTH;
  1308. /*
  1309. * If we didn't ask for it then there must be a custom extension,
  1310. * otherwise this is unsolicited.
  1311. */
  1312. if (custom_ext_find(&s->cert->custext, role,
  1313. TLSEXT_TYPE_signed_certificate_timestamp,
  1314. NULL) == NULL) {
  1315. SSLfatal(s, TLS1_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
  1316. return 0;
  1317. }
  1318. if (!custom_ext_parse(s, context,
  1319. TLSEXT_TYPE_signed_certificate_timestamp,
  1320. PACKET_data(pkt), PACKET_remaining(pkt),
  1321. x, chainidx)) {
  1322. /* SSLfatal already called */
  1323. return 0;
  1324. }
  1325. }
  1326. return 1;
  1327. }
  1328. #endif
  1329. #ifndef OPENSSL_NO_NEXTPROTONEG
  1330. /*
  1331. * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
  1332. * elements of zero length are allowed and the set of elements must exactly
  1333. * fill the length of the block. Returns 1 on success or 0 on failure.
  1334. */
  1335. static int ssl_next_proto_validate(SSL_CONNECTION *s, PACKET *pkt)
  1336. {
  1337. PACKET tmp_protocol;
  1338. while (PACKET_remaining(pkt)) {
  1339. if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)
  1340. || PACKET_remaining(&tmp_protocol) == 0) {
  1341. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
  1342. return 0;
  1343. }
  1344. }
  1345. return 1;
  1346. }
  1347. int tls_parse_stoc_npn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
  1348. X509 *x, size_t chainidx)
  1349. {
  1350. unsigned char *selected;
  1351. unsigned char selected_len;
  1352. PACKET tmppkt;
  1353. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  1354. /* Check if we are in a renegotiation. If so ignore this extension */
  1355. if (!SSL_IS_FIRST_HANDSHAKE(s))
  1356. return 1;
  1357. /* We must have requested it. */
  1358. if (sctx->ext.npn_select_cb == NULL) {
  1359. SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
  1360. return 0;
  1361. }
  1362. /* The data must be valid */
  1363. tmppkt = *pkt;
  1364. if (!ssl_next_proto_validate(s, &tmppkt)) {
  1365. /* SSLfatal() already called */
  1366. return 0;
  1367. }
  1368. if (sctx->ext.npn_select_cb(SSL_CONNECTION_GET_SSL(s),
  1369. &selected, &selected_len,
  1370. PACKET_data(pkt), PACKET_remaining(pkt),
  1371. sctx->ext.npn_select_cb_arg) !=
  1372. SSL_TLSEXT_ERR_OK) {
  1373. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_EXTENSION);
  1374. return 0;
  1375. }
  1376. /*
  1377. * Could be non-NULL if server has sent multiple NPN extensions in
  1378. * a single Serverhello
  1379. */
  1380. OPENSSL_free(s->ext.npn);
  1381. s->ext.npn = OPENSSL_malloc(selected_len);
  1382. if (s->ext.npn == NULL) {
  1383. s->ext.npn_len = 0;
  1384. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1385. return 0;
  1386. }
  1387. memcpy(s->ext.npn, selected, selected_len);
  1388. s->ext.npn_len = selected_len;
  1389. s->s3.npn_seen = 1;
  1390. return 1;
  1391. }
  1392. #endif
  1393. int tls_parse_stoc_alpn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
  1394. X509 *x, size_t chainidx)
  1395. {
  1396. size_t len;
  1397. /* We must have requested it. */
  1398. if (!s->s3.alpn_sent) {
  1399. SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
  1400. return 0;
  1401. }
  1402. /*-
  1403. * The extension data consists of:
  1404. * uint16 list_length
  1405. * uint8 proto_length;
  1406. * uint8 proto[proto_length];
  1407. */
  1408. if (!PACKET_get_net_2_len(pkt, &len)
  1409. || PACKET_remaining(pkt) != len || !PACKET_get_1_len(pkt, &len)
  1410. || PACKET_remaining(pkt) != len) {
  1411. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
  1412. return 0;
  1413. }
  1414. OPENSSL_free(s->s3.alpn_selected);
  1415. s->s3.alpn_selected = OPENSSL_malloc(len);
  1416. if (s->s3.alpn_selected == NULL) {
  1417. s->s3.alpn_selected_len = 0;
  1418. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1419. return 0;
  1420. }
  1421. if (!PACKET_copy_bytes(pkt, s->s3.alpn_selected, len)) {
  1422. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
  1423. return 0;
  1424. }
  1425. s->s3.alpn_selected_len = len;
  1426. if (s->session->ext.alpn_selected == NULL
  1427. || s->session->ext.alpn_selected_len != len
  1428. || memcmp(s->session->ext.alpn_selected, s->s3.alpn_selected, len)
  1429. != 0) {
  1430. /* ALPN not consistent with the old session so cannot use early_data */
  1431. s->ext.early_data_ok = 0;
  1432. }
  1433. if (!s->hit) {
  1434. /*
  1435. * This is a new session and so alpn_selected should have been
  1436. * initialised to NULL. We should update it with the selected ALPN.
  1437. */
  1438. if (!ossl_assert(s->session->ext.alpn_selected == NULL)) {
  1439. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1440. return 0;
  1441. }
  1442. s->session->ext.alpn_selected =
  1443. OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len);
  1444. if (s->session->ext.alpn_selected == NULL) {
  1445. s->session->ext.alpn_selected_len = 0;
  1446. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1447. return 0;
  1448. }
  1449. s->session->ext.alpn_selected_len = s->s3.alpn_selected_len;
  1450. }
  1451. return 1;
  1452. }
  1453. #ifndef OPENSSL_NO_SRTP
  1454. int tls_parse_stoc_use_srtp(SSL_CONNECTION *s, PACKET *pkt,
  1455. unsigned int context, X509 *x, size_t chainidx)
  1456. {
  1457. unsigned int id, ct, mki;
  1458. int i;
  1459. STACK_OF(SRTP_PROTECTION_PROFILE) *clnt;
  1460. SRTP_PROTECTION_PROFILE *prof;
  1461. if (!PACKET_get_net_2(pkt, &ct) || ct != 2
  1462. || !PACKET_get_net_2(pkt, &id)
  1463. || !PACKET_get_1(pkt, &mki)
  1464. || PACKET_remaining(pkt) != 0) {
  1465. SSLfatal(s, SSL_AD_DECODE_ERROR,
  1466. SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1467. return 0;
  1468. }
  1469. if (mki != 0) {
  1470. /* Must be no MKI, since we never offer one */
  1471. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_SRTP_MKI_VALUE);
  1472. return 0;
  1473. }
  1474. /* Throw an error if the server gave us an unsolicited extension */
  1475. clnt = SSL_get_srtp_profiles(SSL_CONNECTION_GET_SSL(s));
  1476. if (clnt == NULL) {
  1477. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_NO_SRTP_PROFILES);
  1478. return 0;
  1479. }
  1480. /*
  1481. * Check to see if the server gave us something we support (and
  1482. * presumably offered)
  1483. */
  1484. for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) {
  1485. prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
  1486. if (prof->id == id) {
  1487. s->srtp_profile = prof;
  1488. return 1;
  1489. }
  1490. }
  1491. SSLfatal(s, SSL_AD_DECODE_ERROR,
  1492. SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1493. return 0;
  1494. }
  1495. #endif
  1496. int tls_parse_stoc_etm(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
  1497. X509 *x, size_t chainidx)
  1498. {
  1499. /* Ignore if inappropriate ciphersuite */
  1500. if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
  1501. && s->s3.tmp.new_cipher->algorithm_mac != SSL_AEAD
  1502. && s->s3.tmp.new_cipher->algorithm_enc != SSL_RC4
  1503. && s->s3.tmp.new_cipher->algorithm_enc != SSL_eGOST2814789CNT
  1504. && s->s3.tmp.new_cipher->algorithm_enc != SSL_eGOST2814789CNT12
  1505. && s->s3.tmp.new_cipher->algorithm_enc != SSL_MAGMA
  1506. && s->s3.tmp.new_cipher->algorithm_enc != SSL_KUZNYECHIK)
  1507. s->ext.use_etm = 1;
  1508. return 1;
  1509. }
  1510. int tls_parse_stoc_ems(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
  1511. X509 *x, size_t chainidx)
  1512. {
  1513. if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET)
  1514. return 1;
  1515. s->s3.flags |= TLS1_FLAGS_RECEIVED_EXTMS;
  1516. if (!s->hit)
  1517. s->session->flags |= SSL_SESS_FLAG_EXTMS;
  1518. return 1;
  1519. }
  1520. int tls_parse_stoc_supported_versions(SSL_CONNECTION *s, PACKET *pkt,
  1521. unsigned int context,
  1522. X509 *x, size_t chainidx)
  1523. {
  1524. unsigned int version;
  1525. if (!PACKET_get_net_2(pkt, &version)
  1526. || PACKET_remaining(pkt) != 0) {
  1527. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1528. return 0;
  1529. }
  1530. /*
  1531. * The only protocol version we support which is valid in this extension in
  1532. * a ServerHello is TLSv1.3 therefore we shouldn't be getting anything else.
  1533. */
  1534. if (version != TLS1_3_VERSION) {
  1535. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1536. SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
  1537. return 0;
  1538. }
  1539. /* We ignore this extension for HRRs except to sanity check it */
  1540. if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST)
  1541. return 1;
  1542. /* We just set it here. We validate it in ssl_choose_client_version */
  1543. s->version = version;
  1544. if (!ssl_set_record_protocol_version(s, version)) {
  1545. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1546. return 0;
  1547. }
  1548. return 1;
  1549. }
  1550. int tls_parse_stoc_key_share(SSL_CONNECTION *s, PACKET *pkt,
  1551. unsigned int context, X509 *x,
  1552. size_t chainidx)
  1553. {
  1554. #ifndef OPENSSL_NO_TLS1_3
  1555. unsigned int group_id;
  1556. PACKET encoded_pt;
  1557. EVP_PKEY *ckey = s->s3.tmp.pkey, *skey = NULL;
  1558. const TLS_GROUP_INFO *ginf = NULL;
  1559. /* Sanity check */
  1560. if (ckey == NULL || s->s3.peer_tmp != NULL) {
  1561. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1562. return 0;
  1563. }
  1564. if (!PACKET_get_net_2(pkt, &group_id)) {
  1565. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1566. return 0;
  1567. }
  1568. if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0) {
  1569. const uint16_t *pgroups = NULL;
  1570. size_t i, num_groups;
  1571. if (PACKET_remaining(pkt) != 0) {
  1572. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1573. return 0;
  1574. }
  1575. /*
  1576. * It is an error if the HelloRetryRequest wants a key_share that we
  1577. * already sent in the first ClientHello
  1578. */
  1579. if (group_id == s->s3.group_id) {
  1580. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
  1581. return 0;
  1582. }
  1583. /* Validate the selected group is one we support */
  1584. tls1_get_supported_groups(s, &pgroups, &num_groups);
  1585. for (i = 0; i < num_groups; i++) {
  1586. if (group_id == pgroups[i])
  1587. break;
  1588. }
  1589. if (i >= num_groups
  1590. || !tls_group_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED)
  1591. || !tls_valid_group(s, group_id, TLS1_3_VERSION, TLS1_3_VERSION,
  1592. 0, NULL)) {
  1593. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
  1594. return 0;
  1595. }
  1596. s->s3.group_id = group_id;
  1597. EVP_PKEY_free(s->s3.tmp.pkey);
  1598. s->s3.tmp.pkey = NULL;
  1599. return 1;
  1600. }
  1601. if (group_id != s->s3.group_id) {
  1602. /*
  1603. * This isn't for the group that we sent in the original
  1604. * key_share!
  1605. */
  1606. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
  1607. return 0;
  1608. }
  1609. /* Retain this group in the SSL_SESSION */
  1610. if (!s->hit) {
  1611. s->session->kex_group = group_id;
  1612. } else if (group_id != s->session->kex_group) {
  1613. /*
  1614. * If this is a resumption but changed what group was used, we need
  1615. * to record the new group in the session, but the session is not
  1616. * a new session and could be in use by other threads. So, make
  1617. * a copy of the session to record the new information so that it's
  1618. * useful for any sessions resumed from tickets issued on this
  1619. * connection.
  1620. */
  1621. SSL_SESSION *new_sess;
  1622. if ((new_sess = ssl_session_dup(s->session, 0)) == NULL) {
  1623. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
  1624. return 0;
  1625. }
  1626. SSL_SESSION_free(s->session);
  1627. s->session = new_sess;
  1628. s->session->kex_group = group_id;
  1629. }
  1630. if ((ginf = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s),
  1631. group_id)) == NULL) {
  1632. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
  1633. return 0;
  1634. }
  1635. if (!PACKET_as_length_prefixed_2(pkt, &encoded_pt)
  1636. || PACKET_remaining(&encoded_pt) == 0) {
  1637. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1638. return 0;
  1639. }
  1640. if (!ginf->is_kem) {
  1641. /* Regular KEX */
  1642. skey = EVP_PKEY_new();
  1643. if (skey == NULL || EVP_PKEY_copy_parameters(skey, ckey) <= 0) {
  1644. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);
  1645. EVP_PKEY_free(skey);
  1646. return 0;
  1647. }
  1648. if (tls13_set_encoded_pub_key(skey, PACKET_data(&encoded_pt),
  1649. PACKET_remaining(&encoded_pt)) <= 0) {
  1650. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_ECPOINT);
  1651. EVP_PKEY_free(skey);
  1652. return 0;
  1653. }
  1654. if (ssl_derive(s, ckey, skey, 1) == 0) {
  1655. /* SSLfatal() already called */
  1656. EVP_PKEY_free(skey);
  1657. return 0;
  1658. }
  1659. s->s3.peer_tmp = skey;
  1660. } else {
  1661. /* KEM Mode */
  1662. const unsigned char *ct = PACKET_data(&encoded_pt);
  1663. size_t ctlen = PACKET_remaining(&encoded_pt);
  1664. if (ssl_decapsulate(s, ckey, ct, ctlen, 1) == 0) {
  1665. /* SSLfatal() already called */
  1666. return 0;
  1667. }
  1668. }
  1669. s->s3.did_kex = 1;
  1670. #endif
  1671. return 1;
  1672. }
  1673. int tls_parse_stoc_cookie(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
  1674. X509 *x, size_t chainidx)
  1675. {
  1676. PACKET cookie;
  1677. if (!PACKET_as_length_prefixed_2(pkt, &cookie)
  1678. || !PACKET_memdup(&cookie, &s->ext.tls13_cookie,
  1679. &s->ext.tls13_cookie_len)) {
  1680. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1681. return 0;
  1682. }
  1683. return 1;
  1684. }
  1685. int tls_parse_stoc_early_data(SSL_CONNECTION *s, PACKET *pkt,
  1686. unsigned int context,
  1687. X509 *x, size_t chainidx)
  1688. {
  1689. if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {
  1690. unsigned long max_early_data;
  1691. if (!PACKET_get_net_4(pkt, &max_early_data)
  1692. || PACKET_remaining(pkt) != 0) {
  1693. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_INVALID_MAX_EARLY_DATA);
  1694. return 0;
  1695. }
  1696. s->session->ext.max_early_data = max_early_data;
  1697. if (SSL_IS_QUIC_HANDSHAKE(s) && max_early_data != 0xffffffff) {
  1698. /*
  1699. * QUIC allows missing max_early_data, or a max_early_data value
  1700. * of 0xffffffff. Missing max_early_data is stored in the session
  1701. * as 0. This is indistinguishable in OpenSSL from a present
  1702. * max_early_data value that was 0. In order that later checks for
  1703. * invalid max_early_data correctly treat as an error the case where
  1704. * max_early_data is present and it is 0, we store any invalid
  1705. * value in the same (non-zero) way. Otherwise we would have to
  1706. * introduce a new flag just for this.
  1707. */
  1708. s->session->ext.max_early_data = 1;
  1709. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INVALID_MAX_EARLY_DATA);
  1710. return 0;
  1711. }
  1712. return 1;
  1713. }
  1714. if (PACKET_remaining(pkt) != 0) {
  1715. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
  1716. return 0;
  1717. }
  1718. if (!s->ext.early_data_ok
  1719. || !s->hit) {
  1720. /*
  1721. * If we get here then we didn't send early data, or we didn't resume
  1722. * using the first identity, or the SNI/ALPN is not consistent so the
  1723. * server should not be accepting it.
  1724. */
  1725. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
  1726. return 0;
  1727. }
  1728. s->ext.early_data = SSL_EARLY_DATA_ACCEPTED;
  1729. return 1;
  1730. }
  1731. int tls_parse_stoc_psk(SSL_CONNECTION *s, PACKET *pkt,
  1732. unsigned int context, X509 *x,
  1733. size_t chainidx)
  1734. {
  1735. #ifndef OPENSSL_NO_TLS1_3
  1736. unsigned int identity;
  1737. if (!PACKET_get_net_2(pkt, &identity) || PACKET_remaining(pkt) != 0) {
  1738. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
  1739. return 0;
  1740. }
  1741. if (identity >= (unsigned int)s->ext.tick_identity) {
  1742. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_PSK_IDENTITY);
  1743. return 0;
  1744. }
  1745. /*
  1746. * Session resumption tickets are always sent before PSK tickets. If the
  1747. * ticket index is 0 then it must be for a session resumption ticket if we
  1748. * sent two tickets, or if we didn't send a PSK ticket.
  1749. */
  1750. if (identity == 0 && (s->psksession == NULL || s->ext.tick_identity == 2)) {
  1751. s->hit = 1;
  1752. SSL_SESSION_free(s->psksession);
  1753. s->psksession = NULL;
  1754. return 1;
  1755. }
  1756. if (s->psksession == NULL) {
  1757. /* Should never happen */
  1758. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1759. return 0;
  1760. }
  1761. /*
  1762. * If we used the external PSK for sending early_data then s->early_secret
  1763. * is already set up, so don't overwrite it. Otherwise we copy the
  1764. * early_secret across that we generated earlier.
  1765. */
  1766. if ((s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY
  1767. && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING)
  1768. || s->session->ext.max_early_data > 0
  1769. || s->psksession->ext.max_early_data == 0)
  1770. memcpy(s->early_secret, s->psksession->early_secret, EVP_MAX_MD_SIZE);
  1771. SSL_SESSION_free(s->session);
  1772. s->session = s->psksession;
  1773. s->psksession = NULL;
  1774. s->hit = 1;
  1775. /* Early data is only allowed if we used the first ticket */
  1776. if (identity != 0)
  1777. s->ext.early_data_ok = 0;
  1778. #endif
  1779. return 1;
  1780. }
  1781. EXT_RETURN tls_construct_ctos_client_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,
  1782. unsigned int context,
  1783. X509 *x, size_t chainidx)
  1784. {
  1785. sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
  1786. if (sc->client_cert_type == NULL)
  1787. return EXT_RETURN_NOT_SENT;
  1788. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_client_cert_type)
  1789. || !WPACKET_start_sub_packet_u16(pkt)
  1790. || !WPACKET_sub_memcpy_u8(pkt, sc->client_cert_type, sc->client_cert_type_len)
  1791. || !WPACKET_close(pkt)) {
  1792. SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1793. return EXT_RETURN_FAIL;
  1794. }
  1795. sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_GOOD;
  1796. return EXT_RETURN_SENT;
  1797. }
  1798. int tls_parse_stoc_client_cert_type(SSL_CONNECTION *sc, PACKET *pkt,
  1799. unsigned int context,
  1800. X509 *x, size_t chainidx)
  1801. {
  1802. unsigned int type;
  1803. if (PACKET_remaining(pkt) != 1) {
  1804. SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
  1805. return 0;
  1806. }
  1807. if (!PACKET_get_1(pkt, &type)) {
  1808. SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
  1809. return 0;
  1810. }
  1811. /* We did not send/ask for this */
  1812. if (!ossl_assert(sc->ext.client_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)) {
  1813. SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
  1814. return 0;
  1815. }
  1816. /* We don't have this enabled */
  1817. if (sc->client_cert_type == NULL) {
  1818. SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
  1819. return 0;
  1820. }
  1821. /* Given back a value we didn't configure */
  1822. if (memchr(sc->client_cert_type, type, sc->client_cert_type_len) == NULL) {
  1823. SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_VALUE);
  1824. return 0;
  1825. }
  1826. sc->ext.client_cert_type = type;
  1827. return 1;
  1828. }
  1829. EXT_RETURN tls_construct_ctos_server_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,
  1830. unsigned int context,
  1831. X509 *x, size_t chainidx)
  1832. {
  1833. sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
  1834. if (sc->server_cert_type == NULL)
  1835. return EXT_RETURN_NOT_SENT;
  1836. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_cert_type)
  1837. || !WPACKET_start_sub_packet_u16(pkt)
  1838. || !WPACKET_sub_memcpy_u8(pkt, sc->server_cert_type, sc->server_cert_type_len)
  1839. || !WPACKET_close(pkt)) {
  1840. SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1841. return EXT_RETURN_FAIL;
  1842. }
  1843. sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_GOOD;
  1844. return EXT_RETURN_SENT;
  1845. }
  1846. int tls_parse_stoc_server_cert_type(SSL_CONNECTION *sc, PACKET *pkt,
  1847. unsigned int context,
  1848. X509 *x, size_t chainidx)
  1849. {
  1850. unsigned int type;
  1851. if (PACKET_remaining(pkt) != 1) {
  1852. SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
  1853. return 0;
  1854. }
  1855. if (!PACKET_get_1(pkt, &type)) {
  1856. SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
  1857. return 0;
  1858. }
  1859. /* We did not send/ask for this */
  1860. if (!ossl_assert(sc->ext.server_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)) {
  1861. SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
  1862. return 0;
  1863. }
  1864. /* We don't have this enabled */
  1865. if (sc->server_cert_type == NULL) {
  1866. SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
  1867. return 0;
  1868. }
  1869. /* Given back a value we didn't configure */
  1870. if (memchr(sc->server_cert_type, type, sc->server_cert_type_len) == NULL) {
  1871. SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_VALUE);
  1872. return 0;
  1873. }
  1874. sc->ext.server_cert_type = type;
  1875. return 1;
  1876. }