extensions_clnt.c 73 KB

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