extensions_clnt.c 65 KB

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