extensions_clnt.c 71 KB

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