2
0

extensions_clnt.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024
  1. /*
  2. * Copyright 2016-2018 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_locl.h"
  11. #include "internal/cryptlib.h"
  12. #include "statem_locl.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;
  105. unsigned long alg_k, alg_a;
  106. STACK_OF(SSL_CIPHER) *cipher_stack = NULL;
  107. /* See if we support any ECC ciphersuites */
  108. if (s->version == SSL3_VERSION)
  109. return 0;
  110. cipher_stack = SSL_get_ciphers(s);
  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. return 1;
  120. }
  121. return 0;
  122. }
  123. EXT_RETURN tls_construct_ctos_ec_pt_formats(SSL *s, WPACKET *pkt,
  124. unsigned int context, X509 *x,
  125. size_t chainidx)
  126. {
  127. const unsigned char *pformats;
  128. size_t num_formats;
  129. if (!use_ecc(s))
  130. return EXT_RETURN_NOT_SENT;
  131. /* Add TLS extension ECPointFormats to the ClientHello message */
  132. tls1_get_formatlist(s, &pformats, &num_formats);
  133. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
  134. /* Sub-packet for formats extension */
  135. || !WPACKET_start_sub_packet_u16(pkt)
  136. || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats)
  137. || !WPACKET_close(pkt)) {
  138. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  139. SSL_F_TLS_CONSTRUCT_CTOS_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
  140. return EXT_RETURN_FAIL;
  141. }
  142. return EXT_RETURN_SENT;
  143. }
  144. EXT_RETURN tls_construct_ctos_supported_groups(SSL *s, WPACKET *pkt,
  145. unsigned int context, X509 *x,
  146. size_t chainidx)
  147. {
  148. const uint16_t *pgroups = NULL;
  149. size_t num_groups = 0, i;
  150. if (!use_ecc(s))
  151. return EXT_RETURN_NOT_SENT;
  152. /*
  153. * Add TLS extension supported_groups to the ClientHello message
  154. */
  155. /* TODO(TLS1.3): Add support for DHE groups */
  156. tls1_get_supported_groups(s, &pgroups, &num_groups);
  157. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)
  158. /* Sub-packet for supported_groups extension */
  159. || !WPACKET_start_sub_packet_u16(pkt)
  160. || !WPACKET_start_sub_packet_u16(pkt)) {
  161. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  162. SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,
  163. ERR_R_INTERNAL_ERROR);
  164. return EXT_RETURN_FAIL;
  165. }
  166. /* Copy curve ID if supported */
  167. for (i = 0; i < num_groups; i++) {
  168. uint16_t ctmp = pgroups[i];
  169. if (tls_curve_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED)) {
  170. if (!WPACKET_put_bytes_u16(pkt, ctmp)) {
  171. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  172. SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,
  173. ERR_R_INTERNAL_ERROR);
  174. return EXT_RETURN_FAIL;
  175. }
  176. }
  177. }
  178. if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
  179. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  180. SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,
  181. ERR_R_INTERNAL_ERROR);
  182. return EXT_RETURN_FAIL;
  183. }
  184. return EXT_RETURN_SENT;
  185. }
  186. #endif
  187. EXT_RETURN tls_construct_ctos_session_ticket(SSL *s, WPACKET *pkt,
  188. unsigned int context, X509 *x,
  189. size_t chainidx)
  190. {
  191. size_t ticklen;
  192. if (!tls_use_ticket(s))
  193. return EXT_RETURN_NOT_SENT;
  194. if (!s->new_session && s->session != NULL
  195. && s->session->ext.tick != NULL
  196. && s->session->ssl_version != TLS1_3_VERSION) {
  197. ticklen = s->session->ext.ticklen;
  198. } else if (s->session && s->ext.session_ticket != NULL
  199. && s->ext.session_ticket->data != NULL) {
  200. ticklen = s->ext.session_ticket->length;
  201. s->session->ext.tick = OPENSSL_malloc(ticklen);
  202. if (s->session->ext.tick == NULL) {
  203. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  204. SSL_F_TLS_CONSTRUCT_CTOS_SESSION_TICKET,
  205. ERR_R_INTERNAL_ERROR);
  206. return EXT_RETURN_FAIL;
  207. }
  208. memcpy(s->session->ext.tick,
  209. s->ext.session_ticket->data, ticklen);
  210. s->session->ext.ticklen = ticklen;
  211. } else {
  212. ticklen = 0;
  213. }
  214. if (ticklen == 0 && s->ext.session_ticket != NULL &&
  215. s->ext.session_ticket->data == NULL)
  216. return EXT_RETURN_NOT_SENT;
  217. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
  218. || !WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick, ticklen)) {
  219. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  220. SSL_F_TLS_CONSTRUCT_CTOS_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
  221. return EXT_RETURN_FAIL;
  222. }
  223. return EXT_RETURN_SENT;
  224. }
  225. EXT_RETURN tls_construct_ctos_sig_algs(SSL *s, WPACKET *pkt,
  226. unsigned int context, X509 *x,
  227. size_t chainidx)
  228. {
  229. size_t salglen;
  230. const uint16_t *salg;
  231. if (!SSL_CLIENT_USE_SIGALGS(s))
  232. return EXT_RETURN_NOT_SENT;
  233. salglen = tls12_get_psigalgs(s, 1, &salg);
  234. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms)
  235. /* Sub-packet for sig-algs extension */
  236. || !WPACKET_start_sub_packet_u16(pkt)
  237. /* Sub-packet for the actual list */
  238. || !WPACKET_start_sub_packet_u16(pkt)
  239. || !tls12_copy_sigalgs(s, pkt, salg, salglen)
  240. || !WPACKET_close(pkt)
  241. || !WPACKET_close(pkt)) {
  242. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_SIG_ALGS,
  243. ERR_R_INTERNAL_ERROR);
  244. return EXT_RETURN_FAIL;
  245. }
  246. return EXT_RETURN_SENT;
  247. }
  248. #ifndef OPENSSL_NO_OCSP
  249. EXT_RETURN tls_construct_ctos_status_request(SSL *s, WPACKET *pkt,
  250. unsigned int context, X509 *x,
  251. size_t chainidx)
  252. {
  253. int i;
  254. /* This extension isn't defined for client Certificates */
  255. if (x != NULL)
  256. return EXT_RETURN_NOT_SENT;
  257. if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp)
  258. return EXT_RETURN_NOT_SENT;
  259. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
  260. /* Sub-packet for status request extension */
  261. || !WPACKET_start_sub_packet_u16(pkt)
  262. || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp)
  263. /* Sub-packet for the ids */
  264. || !WPACKET_start_sub_packet_u16(pkt)) {
  265. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  266. SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
  267. return EXT_RETURN_FAIL;
  268. }
  269. for (i = 0; i < sk_OCSP_RESPID_num(s->ext.ocsp.ids); i++) {
  270. unsigned char *idbytes;
  271. OCSP_RESPID *id = sk_OCSP_RESPID_value(s->ext.ocsp.ids, i);
  272. int idlen = i2d_OCSP_RESPID(id, NULL);
  273. if (idlen <= 0
  274. /* Sub-packet for an individual id */
  275. || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes)
  276. || i2d_OCSP_RESPID(id, &idbytes) != idlen) {
  277. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  278. SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST,
  279. ERR_R_INTERNAL_ERROR);
  280. return EXT_RETURN_FAIL;
  281. }
  282. }
  283. if (!WPACKET_close(pkt)
  284. || !WPACKET_start_sub_packet_u16(pkt)) {
  285. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  286. SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
  287. return EXT_RETURN_FAIL;
  288. }
  289. if (s->ext.ocsp.exts) {
  290. unsigned char *extbytes;
  291. int extlen = i2d_X509_EXTENSIONS(s->ext.ocsp.exts, NULL);
  292. if (extlen < 0) {
  293. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  294. SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST,
  295. ERR_R_INTERNAL_ERROR);
  296. return EXT_RETURN_FAIL;
  297. }
  298. if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes)
  299. || i2d_X509_EXTENSIONS(s->ext.ocsp.exts, &extbytes)
  300. != extlen) {
  301. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  302. SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST,
  303. ERR_R_INTERNAL_ERROR);
  304. return EXT_RETURN_FAIL;
  305. }
  306. }
  307. if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
  308. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  309. SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
  310. return EXT_RETURN_FAIL;
  311. }
  312. return EXT_RETURN_SENT;
  313. }
  314. #endif
  315. #ifndef OPENSSL_NO_NEXTPROTONEG
  316. EXT_RETURN tls_construct_ctos_npn(SSL *s, WPACKET *pkt, unsigned int context,
  317. X509 *x, size_t chainidx)
  318. {
  319. if (s->ctx->ext.npn_select_cb == NULL || !SSL_IS_FIRST_HANDSHAKE(s))
  320. return EXT_RETURN_NOT_SENT;
  321. /*
  322. * The client advertises an empty extension to indicate its support
  323. * for Next Protocol Negotiation
  324. */
  325. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
  326. || !WPACKET_put_bytes_u16(pkt, 0)) {
  327. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_NPN,
  328. ERR_R_INTERNAL_ERROR);
  329. return EXT_RETURN_FAIL;
  330. }
  331. return EXT_RETURN_SENT;
  332. }
  333. #endif
  334. EXT_RETURN tls_construct_ctos_alpn(SSL *s, WPACKET *pkt, unsigned int context,
  335. X509 *x, size_t chainidx)
  336. {
  337. s->s3->alpn_sent = 0;
  338. if (s->ext.alpn == NULL || !SSL_IS_FIRST_HANDSHAKE(s))
  339. return EXT_RETURN_NOT_SENT;
  340. if (!WPACKET_put_bytes_u16(pkt,
  341. TLSEXT_TYPE_application_layer_protocol_negotiation)
  342. /* Sub-packet ALPN extension */
  343. || !WPACKET_start_sub_packet_u16(pkt)
  344. || !WPACKET_sub_memcpy_u16(pkt, s->ext.alpn, s->ext.alpn_len)
  345. || !WPACKET_close(pkt)) {
  346. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_ALPN,
  347. ERR_R_INTERNAL_ERROR);
  348. return EXT_RETURN_FAIL;
  349. }
  350. s->s3->alpn_sent = 1;
  351. return EXT_RETURN_SENT;
  352. }
  353. #ifndef OPENSSL_NO_SRTP
  354. EXT_RETURN tls_construct_ctos_use_srtp(SSL *s, WPACKET *pkt,
  355. unsigned int context, X509 *x,
  356. size_t chainidx)
  357. {
  358. STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = SSL_get_srtp_profiles(s);
  359. int i, end;
  360. if (clnt == NULL)
  361. return EXT_RETURN_NOT_SENT;
  362. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
  363. /* Sub-packet for SRTP extension */
  364. || !WPACKET_start_sub_packet_u16(pkt)
  365. /* Sub-packet for the protection profile list */
  366. || !WPACKET_start_sub_packet_u16(pkt)) {
  367. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_USE_SRTP,
  368. ERR_R_INTERNAL_ERROR);
  369. return EXT_RETURN_FAIL;
  370. }
  371. end = sk_SRTP_PROTECTION_PROFILE_num(clnt);
  372. for (i = 0; i < end; i++) {
  373. const SRTP_PROTECTION_PROFILE *prof =
  374. sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
  375. if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) {
  376. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  377. SSL_F_TLS_CONSTRUCT_CTOS_USE_SRTP, ERR_R_INTERNAL_ERROR);
  378. return EXT_RETURN_FAIL;
  379. }
  380. }
  381. if (!WPACKET_close(pkt)
  382. /* Add an empty use_mki value */
  383. || !WPACKET_put_bytes_u8(pkt, 0)
  384. || !WPACKET_close(pkt)) {
  385. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_USE_SRTP,
  386. ERR_R_INTERNAL_ERROR);
  387. return EXT_RETURN_FAIL;
  388. }
  389. return EXT_RETURN_SENT;
  390. }
  391. #endif
  392. EXT_RETURN tls_construct_ctos_etm(SSL *s, WPACKET *pkt, unsigned int context,
  393. X509 *x, size_t chainidx)
  394. {
  395. if (s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
  396. return EXT_RETURN_NOT_SENT;
  397. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
  398. || !WPACKET_put_bytes_u16(pkt, 0)) {
  399. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_ETM,
  400. ERR_R_INTERNAL_ERROR);
  401. return EXT_RETURN_FAIL;
  402. }
  403. return EXT_RETURN_SENT;
  404. }
  405. #ifndef OPENSSL_NO_CT
  406. EXT_RETURN tls_construct_ctos_sct(SSL *s, WPACKET *pkt, unsigned int context,
  407. X509 *x, size_t chainidx)
  408. {
  409. if (s->ct_validation_callback == NULL)
  410. return EXT_RETURN_NOT_SENT;
  411. /* Not defined for client Certificates */
  412. if (x != NULL)
  413. return EXT_RETURN_NOT_SENT;
  414. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp)
  415. || !WPACKET_put_bytes_u16(pkt, 0)) {
  416. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_SCT,
  417. ERR_R_INTERNAL_ERROR);
  418. return EXT_RETURN_FAIL;
  419. }
  420. return EXT_RETURN_SENT;
  421. }
  422. #endif
  423. EXT_RETURN tls_construct_ctos_ems(SSL *s, WPACKET *pkt, unsigned int context,
  424. X509 *x, size_t chainidx)
  425. {
  426. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
  427. || !WPACKET_put_bytes_u16(pkt, 0)) {
  428. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_EMS,
  429. ERR_R_INTERNAL_ERROR);
  430. return EXT_RETURN_FAIL;
  431. }
  432. return EXT_RETURN_SENT;
  433. }
  434. EXT_RETURN tls_construct_ctos_supported_versions(SSL *s, WPACKET *pkt,
  435. unsigned int context, X509 *x,
  436. size_t chainidx)
  437. {
  438. int currv, min_version, max_version, reason;
  439. reason = ssl_get_min_max_version(s, &min_version, &max_version);
  440. if (reason != 0) {
  441. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  442. SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS, reason);
  443. return EXT_RETURN_FAIL;
  444. }
  445. /*
  446. * Don't include this if we can't negotiate TLSv1.3. We can do a straight
  447. * comparison here because we will never be called in DTLS.
  448. */
  449. if (max_version < TLS1_3_VERSION)
  450. return EXT_RETURN_NOT_SENT;
  451. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
  452. || !WPACKET_start_sub_packet_u16(pkt)
  453. || !WPACKET_start_sub_packet_u8(pkt)) {
  454. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  455. SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
  456. ERR_R_INTERNAL_ERROR);
  457. return EXT_RETURN_FAIL;
  458. }
  459. /*
  460. * TODO(TLS1.3): There is some discussion on the TLS list as to whether
  461. * we should include versions <TLS1.2. For the moment we do. To be
  462. * reviewed later.
  463. */
  464. for (currv = max_version; currv >= min_version; currv--) {
  465. /* TODO(TLS1.3): Remove this first if clause prior to release!! */
  466. if (currv == TLS1_3_VERSION) {
  467. if (!WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION_DRAFT)
  468. || !WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION_DRAFT_27)
  469. || !WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION_DRAFT_26)) {
  470. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  471. SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
  472. ERR_R_INTERNAL_ERROR);
  473. return EXT_RETURN_FAIL;
  474. }
  475. } else if (!WPACKET_put_bytes_u16(pkt, currv)) {
  476. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  477. SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
  478. ERR_R_INTERNAL_ERROR);
  479. return EXT_RETURN_FAIL;
  480. }
  481. }
  482. if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
  483. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  484. SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
  485. ERR_R_INTERNAL_ERROR);
  486. return EXT_RETURN_FAIL;
  487. }
  488. return EXT_RETURN_SENT;
  489. }
  490. /*
  491. * Construct a psk_kex_modes extension.
  492. */
  493. EXT_RETURN tls_construct_ctos_psk_kex_modes(SSL *s, WPACKET *pkt,
  494. unsigned int context, X509 *x,
  495. size_t chainidx)
  496. {
  497. #ifndef OPENSSL_NO_TLS1_3
  498. int nodhe = s->options & SSL_OP_ALLOW_NO_DHE_KEX;
  499. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk_kex_modes)
  500. || !WPACKET_start_sub_packet_u16(pkt)
  501. || !WPACKET_start_sub_packet_u8(pkt)
  502. || !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE_DHE)
  503. || (nodhe && !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE))
  504. || !WPACKET_close(pkt)
  505. || !WPACKET_close(pkt)) {
  506. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  507. SSL_F_TLS_CONSTRUCT_CTOS_PSK_KEX_MODES, ERR_R_INTERNAL_ERROR);
  508. return EXT_RETURN_FAIL;
  509. }
  510. s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_KE_DHE;
  511. if (nodhe)
  512. s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE;
  513. #endif
  514. return EXT_RETURN_SENT;
  515. }
  516. #ifndef OPENSSL_NO_TLS1_3
  517. static int add_key_share(SSL *s, WPACKET *pkt, unsigned int curve_id)
  518. {
  519. unsigned char *encoded_point = NULL;
  520. EVP_PKEY *key_share_key = NULL;
  521. size_t encodedlen;
  522. if (s->s3->tmp.pkey != NULL) {
  523. if (!ossl_assert(s->hello_retry_request == SSL_HRR_PENDING)) {
  524. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_ADD_KEY_SHARE,
  525. 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_tls_encodedpoint(key_share_key,
  541. &encoded_point);
  542. if (encodedlen == 0) {
  543. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_ADD_KEY_SHARE, 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, SSL_F_ADD_KEY_SHARE,
  550. ERR_R_INTERNAL_ERROR);
  551. goto err;
  552. }
  553. /*
  554. * TODO(TLS1.3): When changing to send more than one key_share we're
  555. * going to need to be able to save more than one EVP_PKEY. For now
  556. * we reuse the existing tmp.pkey
  557. */
  558. s->s3->tmp.pkey = key_share_key;
  559. s->s3->group_id = curve_id;
  560. OPENSSL_free(encoded_point);
  561. return 1;
  562. err:
  563. if (s->s3->tmp.pkey == NULL)
  564. EVP_PKEY_free(key_share_key);
  565. OPENSSL_free(encoded_point);
  566. return 0;
  567. }
  568. #endif
  569. EXT_RETURN tls_construct_ctos_key_share(SSL *s, WPACKET *pkt,
  570. unsigned int context, X509 *x,
  571. size_t chainidx)
  572. {
  573. #ifndef OPENSSL_NO_TLS1_3
  574. size_t i, num_groups = 0;
  575. const uint16_t *pgroups = NULL;
  576. uint16_t curve_id = 0;
  577. /* key_share extension */
  578. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
  579. /* Extension data sub-packet */
  580. || !WPACKET_start_sub_packet_u16(pkt)
  581. /* KeyShare list sub-packet */
  582. || !WPACKET_start_sub_packet_u16(pkt)) {
  583. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE,
  584. ERR_R_INTERNAL_ERROR);
  585. return EXT_RETURN_FAIL;
  586. }
  587. tls1_get_supported_groups(s, &pgroups, &num_groups);
  588. /*
  589. * TODO(TLS1.3): Make the number of key_shares sent configurable. For
  590. * now, just send one
  591. */
  592. if (s->s3->group_id != 0) {
  593. curve_id = s->s3->group_id;
  594. } else {
  595. for (i = 0; i < num_groups; i++) {
  596. if (!tls_curve_allowed(s, pgroups[i], SSL_SECOP_CURVE_SUPPORTED))
  597. continue;
  598. curve_id = pgroups[i];
  599. break;
  600. }
  601. }
  602. if (curve_id == 0) {
  603. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE,
  604. 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, SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE,
  613. ERR_R_INTERNAL_ERROR);
  614. return EXT_RETURN_FAIL;
  615. }
  616. return EXT_RETURN_SENT;
  617. #else
  618. return EXT_RETURN_NOT_SENT;
  619. #endif
  620. }
  621. EXT_RETURN tls_construct_ctos_cookie(SSL *s, WPACKET *pkt, 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, SSL_F_TLS_CONSTRUCT_CTOS_COOKIE,
  635. ERR_R_INTERNAL_ERROR);
  636. goto end;
  637. }
  638. ret = EXT_RETURN_SENT;
  639. end:
  640. OPENSSL_free(s->ext.tls13_cookie);
  641. s->ext.tls13_cookie = NULL;
  642. s->ext.tls13_cookie_len = 0;
  643. return ret;
  644. }
  645. EXT_RETURN tls_construct_ctos_early_data(SSL *s, WPACKET *pkt,
  646. unsigned int context, X509 *x,
  647. size_t chainidx)
  648. {
  649. #ifndef OPENSSL_NO_PSK
  650. char identity[PSK_MAX_IDENTITY_LEN + 1];
  651. #endif /* OPENSSL_NO_PSK */
  652. const unsigned char *id = NULL;
  653. size_t idlen = 0;
  654. SSL_SESSION *psksess = NULL;
  655. SSL_SESSION *edsess = NULL;
  656. const EVP_MD *handmd = NULL;
  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(s, 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_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
  665. SSL_R_BAD_PSK);
  666. return EXT_RETURN_FAIL;
  667. }
  668. #ifndef OPENSSL_NO_PSK
  669. if (psksess == NULL && s->psk_client_callback != NULL) {
  670. unsigned char psk[PSK_MAX_PSK_LEN];
  671. size_t psklen = 0;
  672. memset(identity, 0, sizeof(identity));
  673. psklen = s->psk_client_callback(s, NULL, identity, sizeof(identity) - 1,
  674. psk, sizeof(psk));
  675. if (psklen > PSK_MAX_PSK_LEN) {
  676. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  677. SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA, ERR_R_INTERNAL_ERROR);
  678. return EXT_RETURN_FAIL;
  679. } else if (psklen > 0) {
  680. const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 };
  681. const SSL_CIPHER *cipher;
  682. idlen = strlen(identity);
  683. if (idlen > PSK_MAX_IDENTITY_LEN) {
  684. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  685. SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
  686. ERR_R_INTERNAL_ERROR);
  687. return EXT_RETURN_FAIL;
  688. }
  689. id = (unsigned char *)identity;
  690. /*
  691. * We found a PSK using an old style callback. We don't know
  692. * the digest so we default to SHA256 as per the TLSv1.3 spec
  693. */
  694. cipher = SSL_CIPHER_find(s, tls13_aes128gcmsha256_id);
  695. if (cipher == NULL) {
  696. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  697. SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
  698. ERR_R_INTERNAL_ERROR);
  699. return EXT_RETURN_FAIL;
  700. }
  701. psksess = SSL_SESSION_new();
  702. if (psksess == NULL
  703. || !SSL_SESSION_set1_master_key(psksess, psk, psklen)
  704. || !SSL_SESSION_set_cipher(psksess, cipher)
  705. || !SSL_SESSION_set_protocol_version(psksess, TLS1_3_VERSION)) {
  706. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  707. SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
  708. ERR_R_INTERNAL_ERROR);
  709. OPENSSL_cleanse(psk, psklen);
  710. return EXT_RETURN_FAIL;
  711. }
  712. OPENSSL_cleanse(psk, psklen);
  713. }
  714. }
  715. #endif /* OPENSSL_NO_PSK */
  716. SSL_SESSION_free(s->psksession);
  717. s->psksession = psksess;
  718. if (psksess != NULL) {
  719. OPENSSL_free(s->psksession_id);
  720. s->psksession_id = OPENSSL_memdup(id, idlen);
  721. if (s->psksession_id == NULL) {
  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 now, 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->session->ext.tick_identity = TLSEXT_PSK_BAD_IDENTITY;
  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. now = (uint32_t)time(NULL);
  924. agesec = now - (uint32_t)s->session->time;
  925. /*
  926. * We calculate the age in seconds but the server may work in ms. Due to
  927. * rounding errors we could overestimate the age by up to 1s. It is
  928. * better to underestimate it. Otherwise, if the RTT is very short, when
  929. * the server calculates the age reported by the client it could be
  930. * bigger than the age calculated on the server - which should never
  931. * happen.
  932. */
  933. if (agesec > 0)
  934. agesec--;
  935. if (s->session->ext.tick_lifetime_hint < agesec) {
  936. /* Ticket is too old. Ignore it. */
  937. goto dopsksess;
  938. }
  939. /*
  940. * Calculate age in ms. We're just doing it to nearest second. Should be
  941. * good enough.
  942. */
  943. agems = agesec * (uint32_t)1000;
  944. if (agesec != 0 && agems / (uint32_t)1000 != agesec) {
  945. /*
  946. * Overflow. Shouldn't happen unless this is a *really* old session.
  947. * If so we just ignore it.
  948. */
  949. goto dopsksess;
  950. }
  951. /*
  952. * Obfuscate the age. Overflow here is fine, this addition is supposed
  953. * to be mod 2^32.
  954. */
  955. agems += s->session->ext.tick_age_add;
  956. reshashsize = EVP_MD_size(mdres);
  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. }
  1010. if (!WPACKET_close(pkt)
  1011. || !WPACKET_get_total_written(pkt, &binderoffset)
  1012. || !WPACKET_start_sub_packet_u16(pkt)
  1013. || (dores
  1014. && !WPACKET_sub_allocate_bytes_u8(pkt, reshashsize, &resbinder))
  1015. || (s->psksession != NULL
  1016. && !WPACKET_sub_allocate_bytes_u8(pkt, pskhashsize, &pskbinder))
  1017. || !WPACKET_close(pkt)
  1018. || !WPACKET_close(pkt)
  1019. || !WPACKET_get_total_written(pkt, &msglen)
  1020. /*
  1021. * We need to fill in all the sub-packet lengths now so we can
  1022. * calculate the HMAC of the message up to the binders
  1023. */
  1024. || !WPACKET_fill_lengths(pkt)) {
  1025. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
  1026. ERR_R_INTERNAL_ERROR);
  1027. return EXT_RETURN_FAIL;
  1028. }
  1029. msgstart = WPACKET_get_curr(pkt) - msglen;
  1030. if (dores
  1031. && tls_psk_do_binder(s, mdres, msgstart, binderoffset, NULL,
  1032. resbinder, s->session, 1, 0) != 1) {
  1033. /* SSLfatal() already called */
  1034. return EXT_RETURN_FAIL;
  1035. }
  1036. if (s->psksession != NULL
  1037. && tls_psk_do_binder(s, mdpsk, msgstart, binderoffset, NULL,
  1038. pskbinder, s->psksession, 1, 1) != 1) {
  1039. /* SSLfatal() already called */
  1040. return EXT_RETURN_FAIL;
  1041. }
  1042. if (dores)
  1043. s->session->ext.tick_identity = 0;
  1044. if (s->psksession != NULL)
  1045. s->psksession->ext.tick_identity = (dores ? 1 : 0);
  1046. return EXT_RETURN_SENT;
  1047. #else
  1048. return EXT_RETURN_NOT_SENT;
  1049. #endif
  1050. }
  1051. EXT_RETURN tls_construct_ctos_post_handshake_auth(SSL *s, WPACKET *pkt,
  1052. unsigned int context,
  1053. X509 *x, size_t chainidx)
  1054. {
  1055. #ifndef OPENSSL_NO_TLS1_3
  1056. if (!s->pha_forced) {
  1057. int i, n = 0;
  1058. /* check for cert, if present, we can do post-handshake auth */
  1059. if (s->cert == NULL)
  1060. return EXT_RETURN_NOT_SENT;
  1061. for (i = 0; i < SSL_PKEY_NUM; i++) {
  1062. if (s->cert->pkeys[i].x509 != NULL
  1063. && s->cert->pkeys[i].privatekey != NULL)
  1064. n++;
  1065. }
  1066. /* no identity certificates, so no extension */
  1067. if (n == 0)
  1068. return EXT_RETURN_NOT_SENT;
  1069. }
  1070. /* construct extension - 0 length, no contents */
  1071. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_post_handshake_auth)
  1072. || !WPACKET_start_sub_packet_u16(pkt)
  1073. || !WPACKET_close(pkt)) {
  1074. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1075. SSL_F_TLS_CONSTRUCT_CTOS_POST_HANDSHAKE_AUTH,
  1076. ERR_R_INTERNAL_ERROR);
  1077. return EXT_RETURN_FAIL;
  1078. }
  1079. s->post_handshake_auth = SSL_PHA_EXT_SENT;
  1080. return EXT_RETURN_SENT;
  1081. #else
  1082. return EXT_RETURN_NOT_SENT;
  1083. #endif
  1084. }
  1085. /*
  1086. * Parse the server's renegotiation binding and abort if it's not right
  1087. */
  1088. int tls_parse_stoc_renegotiate(SSL *s, PACKET *pkt, unsigned int context,
  1089. X509 *x, size_t chainidx)
  1090. {
  1091. size_t expected_len = s->s3->previous_client_finished_len
  1092. + s->s3->previous_server_finished_len;
  1093. size_t ilen;
  1094. const unsigned char *data;
  1095. /* Check for logic errors */
  1096. if (!ossl_assert(expected_len == 0
  1097. || s->s3->previous_client_finished_len != 0)
  1098. || !ossl_assert(expected_len == 0
  1099. || s->s3->previous_server_finished_len != 0)) {
  1100. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
  1101. ERR_R_INTERNAL_ERROR);
  1102. return 0;
  1103. }
  1104. /* Parse the length byte */
  1105. if (!PACKET_get_1_len(pkt, &ilen)) {
  1106. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
  1107. SSL_R_RENEGOTIATION_ENCODING_ERR);
  1108. return 0;
  1109. }
  1110. /* Consistency check */
  1111. if (PACKET_remaining(pkt) != ilen) {
  1112. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
  1113. SSL_R_RENEGOTIATION_ENCODING_ERR);
  1114. return 0;
  1115. }
  1116. /* Check that the extension matches */
  1117. if (ilen != expected_len) {
  1118. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
  1119. SSL_R_RENEGOTIATION_MISMATCH);
  1120. return 0;
  1121. }
  1122. if (!PACKET_get_bytes(pkt, &data, s->s3->previous_client_finished_len)
  1123. || memcmp(data, s->s3->previous_client_finished,
  1124. s->s3->previous_client_finished_len) != 0) {
  1125. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
  1126. SSL_R_RENEGOTIATION_MISMATCH);
  1127. return 0;
  1128. }
  1129. if (!PACKET_get_bytes(pkt, &data, s->s3->previous_server_finished_len)
  1130. || memcmp(data, s->s3->previous_server_finished,
  1131. s->s3->previous_server_finished_len) != 0) {
  1132. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
  1133. SSL_R_RENEGOTIATION_MISMATCH);
  1134. return 0;
  1135. }
  1136. s->s3->send_connection_binding = 1;
  1137. return 1;
  1138. }
  1139. /* Parse the server's max fragment len extension packet */
  1140. int tls_parse_stoc_maxfragmentlen(SSL *s, PACKET *pkt, unsigned int context,
  1141. X509 *x, size_t chainidx)
  1142. {
  1143. unsigned int value;
  1144. if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) {
  1145. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_MAXFRAGMENTLEN,
  1146. SSL_R_BAD_EXTENSION);
  1147. return 0;
  1148. }
  1149. /* |value| should contains a valid max-fragment-length code. */
  1150. if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) {
  1151. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1152. SSL_F_TLS_PARSE_STOC_MAXFRAGMENTLEN,
  1153. SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
  1154. return 0;
  1155. }
  1156. /* Must be the same value as client-configured one who was sent to server */
  1157. /*-
  1158. * RFC 6066: if a client receives a maximum fragment length negotiation
  1159. * response that differs from the length it requested, ...
  1160. * It must abort with SSL_AD_ILLEGAL_PARAMETER alert
  1161. */
  1162. if (value != s->ext.max_fragment_len_mode) {
  1163. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1164. SSL_F_TLS_PARSE_STOC_MAXFRAGMENTLEN,
  1165. SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
  1166. return 0;
  1167. }
  1168. /*
  1169. * Maximum Fragment Length Negotiation succeeded.
  1170. * The negotiated Maximum Fragment Length is binding now.
  1171. */
  1172. s->session->ext.max_fragment_len_mode = value;
  1173. return 1;
  1174. }
  1175. int tls_parse_stoc_server_name(SSL *s, PACKET *pkt, unsigned int context,
  1176. X509 *x, size_t chainidx)
  1177. {
  1178. if (s->ext.hostname == NULL) {
  1179. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_SERVER_NAME,
  1180. ERR_R_INTERNAL_ERROR);
  1181. return 0;
  1182. }
  1183. if (PACKET_remaining(pkt) > 0) {
  1184. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_SERVER_NAME,
  1185. SSL_R_BAD_EXTENSION);
  1186. return 0;
  1187. }
  1188. if (!s->hit) {
  1189. if (s->session->ext.hostname != NULL) {
  1190. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_SERVER_NAME,
  1191. ERR_R_INTERNAL_ERROR);
  1192. return 0;
  1193. }
  1194. s->session->ext.hostname = OPENSSL_strdup(s->ext.hostname);
  1195. if (s->session->ext.hostname == NULL) {
  1196. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_SERVER_NAME,
  1197. ERR_R_INTERNAL_ERROR);
  1198. return 0;
  1199. }
  1200. }
  1201. return 1;
  1202. }
  1203. #ifndef OPENSSL_NO_EC
  1204. int tls_parse_stoc_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context,
  1205. X509 *x, size_t chainidx)
  1206. {
  1207. size_t ecpointformats_len;
  1208. PACKET ecptformatlist;
  1209. if (!PACKET_as_length_prefixed_1(pkt, &ecptformatlist)) {
  1210. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_EC_PT_FORMATS,
  1211. SSL_R_BAD_EXTENSION);
  1212. return 0;
  1213. }
  1214. if (!s->hit) {
  1215. ecpointformats_len = PACKET_remaining(&ecptformatlist);
  1216. if (ecpointformats_len == 0) {
  1217. SSLfatal(s, SSL_AD_DECODE_ERROR,
  1218. SSL_F_TLS_PARSE_STOC_EC_PT_FORMATS, SSL_R_BAD_LENGTH);
  1219. return 0;
  1220. }
  1221. s->session->ext.ecpointformats_len = 0;
  1222. OPENSSL_free(s->session->ext.ecpointformats);
  1223. s->session->ext.ecpointformats = OPENSSL_malloc(ecpointformats_len);
  1224. if (s->session->ext.ecpointformats == NULL) {
  1225. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1226. SSL_F_TLS_PARSE_STOC_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
  1227. return 0;
  1228. }
  1229. s->session->ext.ecpointformats_len = ecpointformats_len;
  1230. if (!PACKET_copy_bytes(&ecptformatlist,
  1231. s->session->ext.ecpointformats,
  1232. ecpointformats_len)) {
  1233. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1234. SSL_F_TLS_PARSE_STOC_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
  1235. return 0;
  1236. }
  1237. }
  1238. return 1;
  1239. }
  1240. #endif
  1241. int tls_parse_stoc_session_ticket(SSL *s, PACKET *pkt, unsigned int context,
  1242. X509 *x, size_t chainidx)
  1243. {
  1244. if (s->ext.session_ticket_cb != NULL &&
  1245. !s->ext.session_ticket_cb(s, PACKET_data(pkt),
  1246. PACKET_remaining(pkt),
  1247. s->ext.session_ticket_cb_arg)) {
  1248. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1249. SSL_F_TLS_PARSE_STOC_SESSION_TICKET, SSL_R_BAD_EXTENSION);
  1250. return 0;
  1251. }
  1252. if (!tls_use_ticket(s)) {
  1253. SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION,
  1254. SSL_F_TLS_PARSE_STOC_SESSION_TICKET, SSL_R_BAD_EXTENSION);
  1255. return 0;
  1256. }
  1257. if (PACKET_remaining(pkt) > 0) {
  1258. SSLfatal(s, SSL_AD_DECODE_ERROR,
  1259. SSL_F_TLS_PARSE_STOC_SESSION_TICKET, SSL_R_BAD_EXTENSION);
  1260. return 0;
  1261. }
  1262. s->ext.ticket_expected = 1;
  1263. return 1;
  1264. }
  1265. #ifndef OPENSSL_NO_OCSP
  1266. int tls_parse_stoc_status_request(SSL *s, PACKET *pkt, unsigned int context,
  1267. X509 *x, size_t chainidx)
  1268. {
  1269. if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) {
  1270. /* We ignore this if the server sends a CertificateRequest */
  1271. /* TODO(TLS1.3): Add support for this */
  1272. return 1;
  1273. }
  1274. /*
  1275. * MUST only be sent if we've requested a status
  1276. * request message. In TLS <= 1.2 it must also be empty.
  1277. */
  1278. if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) {
  1279. SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION,
  1280. SSL_F_TLS_PARSE_STOC_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
  1281. return 0;
  1282. }
  1283. if (!SSL_IS_TLS13(s) && PACKET_remaining(pkt) > 0) {
  1284. SSLfatal(s, SSL_AD_DECODE_ERROR,
  1285. SSL_F_TLS_PARSE_STOC_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
  1286. return 0;
  1287. }
  1288. if (SSL_IS_TLS13(s)) {
  1289. /* We only know how to handle this if it's for the first Certificate in
  1290. * the chain. We ignore any other responses.
  1291. */
  1292. if (chainidx != 0)
  1293. return 1;
  1294. /* SSLfatal() already called */
  1295. return tls_process_cert_status_body(s, pkt);
  1296. }
  1297. /* Set flag to expect CertificateStatus message */
  1298. s->ext.status_expected = 1;
  1299. return 1;
  1300. }
  1301. #endif
  1302. #ifndef OPENSSL_NO_CT
  1303. int tls_parse_stoc_sct(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1304. size_t chainidx)
  1305. {
  1306. if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) {
  1307. /* We ignore this if the server sends it in a CertificateRequest */
  1308. /* TODO(TLS1.3): Add support for this */
  1309. return 1;
  1310. }
  1311. /*
  1312. * Only take it if we asked for it - i.e if there is no CT validation
  1313. * callback set, then a custom extension MAY be processing it, so we
  1314. * need to let control continue to flow to that.
  1315. */
  1316. if (s->ct_validation_callback != NULL) {
  1317. size_t size = PACKET_remaining(pkt);
  1318. /* Simply copy it off for later processing */
  1319. OPENSSL_free(s->ext.scts);
  1320. s->ext.scts = NULL;
  1321. s->ext.scts_len = (uint16_t)size;
  1322. if (size > 0) {
  1323. s->ext.scts = OPENSSL_malloc(size);
  1324. if (s->ext.scts == NULL
  1325. || !PACKET_copy_bytes(pkt, s->ext.scts, size)) {
  1326. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_SCT,
  1327. ERR_R_INTERNAL_ERROR);
  1328. return 0;
  1329. }
  1330. }
  1331. } else {
  1332. ENDPOINT role = (context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0
  1333. ? ENDPOINT_CLIENT : ENDPOINT_BOTH;
  1334. /*
  1335. * If we didn't ask for it then there must be a custom extension,
  1336. * otherwise this is unsolicited.
  1337. */
  1338. if (custom_ext_find(&s->cert->custext, role,
  1339. TLSEXT_TYPE_signed_certificate_timestamp,
  1340. NULL) == NULL) {
  1341. SSLfatal(s, TLS1_AD_UNSUPPORTED_EXTENSION, SSL_F_TLS_PARSE_STOC_SCT,
  1342. SSL_R_BAD_EXTENSION);
  1343. return 0;
  1344. }
  1345. if (!custom_ext_parse(s, context,
  1346. TLSEXT_TYPE_signed_certificate_timestamp,
  1347. PACKET_data(pkt), PACKET_remaining(pkt),
  1348. x, chainidx)) {
  1349. /* SSLfatal already called */
  1350. return 0;
  1351. }
  1352. }
  1353. return 1;
  1354. }
  1355. #endif
  1356. #ifndef OPENSSL_NO_NEXTPROTONEG
  1357. /*
  1358. * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
  1359. * elements of zero length are allowed and the set of elements must exactly
  1360. * fill the length of the block. Returns 1 on success or 0 on failure.
  1361. */
  1362. static int ssl_next_proto_validate(SSL *s, PACKET *pkt)
  1363. {
  1364. PACKET tmp_protocol;
  1365. while (PACKET_remaining(pkt)) {
  1366. if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)
  1367. || PACKET_remaining(&tmp_protocol) == 0) {
  1368. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL_NEXT_PROTO_VALIDATE,
  1369. SSL_R_BAD_EXTENSION);
  1370. return 0;
  1371. }
  1372. }
  1373. return 1;
  1374. }
  1375. int tls_parse_stoc_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1376. size_t chainidx)
  1377. {
  1378. unsigned char *selected;
  1379. unsigned char selected_len;
  1380. PACKET tmppkt;
  1381. /* Check if we are in a renegotiation. If so ignore this extension */
  1382. if (!SSL_IS_FIRST_HANDSHAKE(s))
  1383. return 1;
  1384. /* We must have requested it. */
  1385. if (s->ctx->ext.npn_select_cb == NULL) {
  1386. SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_F_TLS_PARSE_STOC_NPN,
  1387. SSL_R_BAD_EXTENSION);
  1388. return 0;
  1389. }
  1390. /* The data must be valid */
  1391. tmppkt = *pkt;
  1392. if (!ssl_next_proto_validate(s, &tmppkt)) {
  1393. /* SSLfatal() already called */
  1394. return 0;
  1395. }
  1396. if (s->ctx->ext.npn_select_cb(s, &selected, &selected_len,
  1397. PACKET_data(pkt),
  1398. PACKET_remaining(pkt),
  1399. s->ctx->ext.npn_select_cb_arg) !=
  1400. SSL_TLSEXT_ERR_OK) {
  1401. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_PARSE_STOC_NPN,
  1402. SSL_R_BAD_EXTENSION);
  1403. return 0;
  1404. }
  1405. /*
  1406. * Could be non-NULL if server has sent multiple NPN extensions in
  1407. * a single Serverhello
  1408. */
  1409. OPENSSL_free(s->ext.npn);
  1410. s->ext.npn = OPENSSL_malloc(selected_len);
  1411. if (s->ext.npn == NULL) {
  1412. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_NPN,
  1413. ERR_R_INTERNAL_ERROR);
  1414. return 0;
  1415. }
  1416. memcpy(s->ext.npn, selected, selected_len);
  1417. s->ext.npn_len = selected_len;
  1418. s->s3->npn_seen = 1;
  1419. return 1;
  1420. }
  1421. #endif
  1422. int tls_parse_stoc_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1423. size_t chainidx)
  1424. {
  1425. size_t len;
  1426. /* We must have requested it. */
  1427. if (!s->s3->alpn_sent) {
  1428. SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_F_TLS_PARSE_STOC_ALPN,
  1429. SSL_R_BAD_EXTENSION);
  1430. return 0;
  1431. }
  1432. /*-
  1433. * The extension data consists of:
  1434. * uint16 list_length
  1435. * uint8 proto_length;
  1436. * uint8 proto[proto_length];
  1437. */
  1438. if (!PACKET_get_net_2_len(pkt, &len)
  1439. || PACKET_remaining(pkt) != len || !PACKET_get_1_len(pkt, &len)
  1440. || PACKET_remaining(pkt) != len) {
  1441. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
  1442. SSL_R_BAD_EXTENSION);
  1443. return 0;
  1444. }
  1445. OPENSSL_free(s->s3->alpn_selected);
  1446. s->s3->alpn_selected = OPENSSL_malloc(len);
  1447. if (s->s3->alpn_selected == NULL) {
  1448. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
  1449. ERR_R_INTERNAL_ERROR);
  1450. return 0;
  1451. }
  1452. if (!PACKET_copy_bytes(pkt, s->s3->alpn_selected, len)) {
  1453. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
  1454. SSL_R_BAD_EXTENSION);
  1455. return 0;
  1456. }
  1457. s->s3->alpn_selected_len = len;
  1458. if (s->session->ext.alpn_selected == NULL
  1459. || s->session->ext.alpn_selected_len != len
  1460. || memcmp(s->session->ext.alpn_selected, s->s3->alpn_selected, len)
  1461. != 0) {
  1462. /* ALPN not consistent with the old session so cannot use early_data */
  1463. s->ext.early_data_ok = 0;
  1464. }
  1465. if (!s->hit) {
  1466. /*
  1467. * This is a new session and so alpn_selected should have been
  1468. * initialised to NULL. We should update it with the selected ALPN.
  1469. */
  1470. if (!ossl_assert(s->session->ext.alpn_selected == NULL)) {
  1471. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
  1472. ERR_R_INTERNAL_ERROR);
  1473. return 0;
  1474. }
  1475. s->session->ext.alpn_selected =
  1476. OPENSSL_memdup(s->s3->alpn_selected, s->s3->alpn_selected_len);
  1477. if (s->session->ext.alpn_selected == NULL) {
  1478. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
  1479. ERR_R_INTERNAL_ERROR);
  1480. return 0;
  1481. }
  1482. s->session->ext.alpn_selected_len = s->s3->alpn_selected_len;
  1483. }
  1484. return 1;
  1485. }
  1486. #ifndef OPENSSL_NO_SRTP
  1487. int tls_parse_stoc_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1488. size_t chainidx)
  1489. {
  1490. unsigned int id, ct, mki;
  1491. int i;
  1492. STACK_OF(SRTP_PROTECTION_PROFILE) *clnt;
  1493. SRTP_PROTECTION_PROFILE *prof;
  1494. if (!PACKET_get_net_2(pkt, &ct) || ct != 2
  1495. || !PACKET_get_net_2(pkt, &id)
  1496. || !PACKET_get_1(pkt, &mki)
  1497. || PACKET_remaining(pkt) != 0) {
  1498. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_USE_SRTP,
  1499. SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1500. return 0;
  1501. }
  1502. if (mki != 0) {
  1503. /* Must be no MKI, since we never offer one */
  1504. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_USE_SRTP,
  1505. SSL_R_BAD_SRTP_MKI_VALUE);
  1506. return 0;
  1507. }
  1508. /* Throw an error if the server gave us an unsolicited extension */
  1509. clnt = SSL_get_srtp_profiles(s);
  1510. if (clnt == NULL) {
  1511. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_USE_SRTP,
  1512. SSL_R_NO_SRTP_PROFILES);
  1513. return 0;
  1514. }
  1515. /*
  1516. * Check to see if the server gave us something we support (and
  1517. * presumably offered)
  1518. */
  1519. for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) {
  1520. prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
  1521. if (prof->id == id) {
  1522. s->srtp_profile = prof;
  1523. return 1;
  1524. }
  1525. }
  1526. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_USE_SRTP,
  1527. SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1528. return 0;
  1529. }
  1530. #endif
  1531. int tls_parse_stoc_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1532. size_t chainidx)
  1533. {
  1534. /* Ignore if inappropriate ciphersuite */
  1535. if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
  1536. && s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
  1537. && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4)
  1538. s->ext.use_etm = 1;
  1539. return 1;
  1540. }
  1541. int tls_parse_stoc_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1542. size_t chainidx)
  1543. {
  1544. s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
  1545. if (!s->hit)
  1546. s->session->flags |= SSL_SESS_FLAG_EXTMS;
  1547. return 1;
  1548. }
  1549. int tls_parse_stoc_supported_versions(SSL *s, PACKET *pkt, unsigned int context,
  1550. X509 *x, size_t chainidx)
  1551. {
  1552. unsigned int version;
  1553. if (!PACKET_get_net_2(pkt, &version)
  1554. || PACKET_remaining(pkt) != 0) {
  1555. SSLfatal(s, SSL_AD_DECODE_ERROR,
  1556. SSL_F_TLS_PARSE_STOC_SUPPORTED_VERSIONS,
  1557. SSL_R_LENGTH_MISMATCH);
  1558. return 0;
  1559. }
  1560. /* TODO(TLS1.3): Remove this before release */
  1561. if (version == TLS1_3_VERSION_DRAFT
  1562. || version == TLS1_3_VERSION_DRAFT_27
  1563. || version == TLS1_3_VERSION_DRAFT_26)
  1564. version = TLS1_3_VERSION;
  1565. /*
  1566. * The only protocol version we support which is valid in this extension in
  1567. * a ServerHello is TLSv1.3 therefore we shouldn't be getting anything else.
  1568. */
  1569. if (version != TLS1_3_VERSION) {
  1570. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1571. SSL_F_TLS_PARSE_STOC_SUPPORTED_VERSIONS,
  1572. SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
  1573. return 0;
  1574. }
  1575. /* We ignore this extension for HRRs except to sanity check it */
  1576. if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST)
  1577. return 1;
  1578. /* We just set it here. We validate it in ssl_choose_client_version */
  1579. s->version = version;
  1580. return 1;
  1581. }
  1582. int tls_parse_stoc_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1583. size_t chainidx)
  1584. {
  1585. #ifndef OPENSSL_NO_TLS1_3
  1586. unsigned int group_id;
  1587. PACKET encoded_pt;
  1588. EVP_PKEY *ckey = s->s3->tmp.pkey, *skey = NULL;
  1589. /* Sanity check */
  1590. if (ckey == NULL || s->s3->peer_tmp != NULL) {
  1591. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
  1592. ERR_R_INTERNAL_ERROR);
  1593. return 0;
  1594. }
  1595. if (!PACKET_get_net_2(pkt, &group_id)) {
  1596. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
  1597. SSL_R_LENGTH_MISMATCH);
  1598. return 0;
  1599. }
  1600. if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0) {
  1601. const uint16_t *pgroups = NULL;
  1602. size_t i, num_groups;
  1603. if (PACKET_remaining(pkt) != 0) {
  1604. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
  1605. SSL_R_LENGTH_MISMATCH);
  1606. return 0;
  1607. }
  1608. /*
  1609. * It is an error if the HelloRetryRequest wants a key_share that we
  1610. * already sent in the first ClientHello
  1611. */
  1612. if (group_id == s->s3->group_id) {
  1613. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1614. SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
  1615. return 0;
  1616. }
  1617. /* Validate the selected group is one we support */
  1618. tls1_get_supported_groups(s, &pgroups, &num_groups);
  1619. for (i = 0; i < num_groups; i++) {
  1620. if (group_id == pgroups[i])
  1621. break;
  1622. }
  1623. if (i >= num_groups
  1624. || !tls_curve_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED)) {
  1625. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1626. SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
  1627. return 0;
  1628. }
  1629. s->s3->group_id = group_id;
  1630. EVP_PKEY_free(s->s3->tmp.pkey);
  1631. s->s3->tmp.pkey = NULL;
  1632. return 1;
  1633. }
  1634. if (group_id != s->s3->group_id) {
  1635. /*
  1636. * This isn't for the group that we sent in the original
  1637. * key_share!
  1638. */
  1639. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
  1640. SSL_R_BAD_KEY_SHARE);
  1641. return 0;
  1642. }
  1643. if (!PACKET_as_length_prefixed_2(pkt, &encoded_pt)
  1644. || PACKET_remaining(&encoded_pt) == 0) {
  1645. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
  1646. SSL_R_LENGTH_MISMATCH);
  1647. return 0;
  1648. }
  1649. skey = ssl_generate_pkey(ckey);
  1650. if (skey == NULL) {
  1651. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
  1652. ERR_R_MALLOC_FAILURE);
  1653. return 0;
  1654. }
  1655. if (!EVP_PKEY_set1_tls_encodedpoint(skey, PACKET_data(&encoded_pt),
  1656. PACKET_remaining(&encoded_pt))) {
  1657. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
  1658. SSL_R_BAD_ECPOINT);
  1659. EVP_PKEY_free(skey);
  1660. return 0;
  1661. }
  1662. if (ssl_derive(s, ckey, skey, 1) == 0) {
  1663. /* SSLfatal() already called */
  1664. EVP_PKEY_free(skey);
  1665. return 0;
  1666. }
  1667. s->s3->peer_tmp = skey;
  1668. #endif
  1669. return 1;
  1670. }
  1671. int tls_parse_stoc_cookie(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1672. size_t chainidx)
  1673. {
  1674. PACKET cookie;
  1675. if (!PACKET_as_length_prefixed_2(pkt, &cookie)
  1676. || !PACKET_memdup(&cookie, &s->ext.tls13_cookie,
  1677. &s->ext.tls13_cookie_len)) {
  1678. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_COOKIE,
  1679. SSL_R_LENGTH_MISMATCH);
  1680. return 0;
  1681. }
  1682. return 1;
  1683. }
  1684. int tls_parse_stoc_early_data(SSL *s, PACKET *pkt, unsigned int context,
  1685. X509 *x, size_t chainidx)
  1686. {
  1687. if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {
  1688. unsigned long max_early_data;
  1689. if (!PACKET_get_net_4(pkt, &max_early_data)
  1690. || PACKET_remaining(pkt) != 0) {
  1691. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_EARLY_DATA,
  1692. SSL_R_INVALID_MAX_EARLY_DATA);
  1693. return 0;
  1694. }
  1695. s->session->ext.max_early_data = max_early_data;
  1696. return 1;
  1697. }
  1698. if (PACKET_remaining(pkt) != 0) {
  1699. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_EARLY_DATA,
  1700. SSL_R_BAD_EXTENSION);
  1701. return 0;
  1702. }
  1703. if (!s->ext.early_data_ok
  1704. || !s->hit
  1705. || s->session->ext.tick_identity != 0) {
  1706. /*
  1707. * If we get here then we didn't send early data, or we didn't resume
  1708. * using the first identity, or the SNI/ALPN is not consistent so the
  1709. * server should not be accepting it.
  1710. */
  1711. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_EARLY_DATA,
  1712. SSL_R_BAD_EXTENSION);
  1713. return 0;
  1714. }
  1715. s->ext.early_data = SSL_EARLY_DATA_ACCEPTED;
  1716. return 1;
  1717. }
  1718. int tls_parse_stoc_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1719. size_t chainidx)
  1720. {
  1721. #ifndef OPENSSL_NO_TLS1_3
  1722. unsigned int identity;
  1723. if (!PACKET_get_net_2(pkt, &identity) || PACKET_remaining(pkt) != 0) {
  1724. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_PSK,
  1725. SSL_R_LENGTH_MISMATCH);
  1726. return 0;
  1727. }
  1728. if (s->session->ext.tick_identity == (int)identity) {
  1729. s->hit = 1;
  1730. SSL_SESSION_free(s->psksession);
  1731. s->psksession = NULL;
  1732. return 1;
  1733. }
  1734. if (s->psksession == NULL
  1735. || s->psksession->ext.tick_identity != (int)identity) {
  1736. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_PSK,
  1737. SSL_R_BAD_PSK_IDENTITY);
  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. #endif
  1755. return 1;
  1756. }