extensions_clnt.c 67 KB

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