extensions_clnt.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020
  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. 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. } else if (!WPACKET_put_bytes_u16(pkt, currv)) {
  474. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  475. SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
  476. ERR_R_INTERNAL_ERROR);
  477. return EXT_RETURN_FAIL;
  478. }
  479. }
  480. if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
  481. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  482. SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
  483. ERR_R_INTERNAL_ERROR);
  484. return EXT_RETURN_FAIL;
  485. }
  486. return EXT_RETURN_SENT;
  487. }
  488. /*
  489. * Construct a psk_kex_modes extension.
  490. */
  491. EXT_RETURN tls_construct_ctos_psk_kex_modes(SSL *s, WPACKET *pkt,
  492. unsigned int context, X509 *x,
  493. size_t chainidx)
  494. {
  495. #ifndef OPENSSL_NO_TLS1_3
  496. int nodhe = s->options & SSL_OP_ALLOW_NO_DHE_KEX;
  497. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk_kex_modes)
  498. || !WPACKET_start_sub_packet_u16(pkt)
  499. || !WPACKET_start_sub_packet_u8(pkt)
  500. || !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE_DHE)
  501. || (nodhe && !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE))
  502. || !WPACKET_close(pkt)
  503. || !WPACKET_close(pkt)) {
  504. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  505. SSL_F_TLS_CONSTRUCT_CTOS_PSK_KEX_MODES, ERR_R_INTERNAL_ERROR);
  506. return EXT_RETURN_FAIL;
  507. }
  508. s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_KE_DHE;
  509. if (nodhe)
  510. s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE;
  511. #endif
  512. return EXT_RETURN_SENT;
  513. }
  514. #ifndef OPENSSL_NO_TLS1_3
  515. static int add_key_share(SSL *s, WPACKET *pkt, unsigned int curve_id)
  516. {
  517. unsigned char *encoded_point = NULL;
  518. EVP_PKEY *key_share_key = NULL;
  519. size_t encodedlen;
  520. if (s->s3->tmp.pkey != NULL) {
  521. if (!ossl_assert(s->hello_retry_request == SSL_HRR_PENDING)) {
  522. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_ADD_KEY_SHARE,
  523. ERR_R_INTERNAL_ERROR);
  524. return 0;
  525. }
  526. /*
  527. * Could happen if we got an HRR that wasn't requesting a new key_share
  528. */
  529. key_share_key = s->s3->tmp.pkey;
  530. } else {
  531. key_share_key = ssl_generate_pkey_group(s, curve_id);
  532. if (key_share_key == NULL) {
  533. /* SSLfatal() already called */
  534. return 0;
  535. }
  536. }
  537. /* Encode the public key. */
  538. encodedlen = EVP_PKEY_get1_tls_encodedpoint(key_share_key,
  539. &encoded_point);
  540. if (encodedlen == 0) {
  541. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_ADD_KEY_SHARE, ERR_R_EC_LIB);
  542. goto err;
  543. }
  544. /* Create KeyShareEntry */
  545. if (!WPACKET_put_bytes_u16(pkt, curve_id)
  546. || !WPACKET_sub_memcpy_u16(pkt, encoded_point, encodedlen)) {
  547. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_ADD_KEY_SHARE,
  548. ERR_R_INTERNAL_ERROR);
  549. goto err;
  550. }
  551. /*
  552. * TODO(TLS1.3): When changing to send more than one key_share we're
  553. * going to need to be able to save more than one EVP_PKEY. For now
  554. * we reuse the existing tmp.pkey
  555. */
  556. s->s3->tmp.pkey = key_share_key;
  557. s->s3->group_id = curve_id;
  558. OPENSSL_free(encoded_point);
  559. return 1;
  560. err:
  561. if (s->s3->tmp.pkey == NULL)
  562. EVP_PKEY_free(key_share_key);
  563. OPENSSL_free(encoded_point);
  564. return 0;
  565. }
  566. #endif
  567. EXT_RETURN tls_construct_ctos_key_share(SSL *s, WPACKET *pkt,
  568. unsigned int context, X509 *x,
  569. size_t chainidx)
  570. {
  571. #ifndef OPENSSL_NO_TLS1_3
  572. size_t i, num_groups = 0;
  573. const uint16_t *pgroups = NULL;
  574. uint16_t curve_id = 0;
  575. /* key_share extension */
  576. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
  577. /* Extension data sub-packet */
  578. || !WPACKET_start_sub_packet_u16(pkt)
  579. /* KeyShare list sub-packet */
  580. || !WPACKET_start_sub_packet_u16(pkt)) {
  581. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE,
  582. ERR_R_INTERNAL_ERROR);
  583. return EXT_RETURN_FAIL;
  584. }
  585. tls1_get_supported_groups(s, &pgroups, &num_groups);
  586. /*
  587. * TODO(TLS1.3): Make the number of key_shares sent configurable. For
  588. * now, just send one
  589. */
  590. if (s->s3->group_id != 0) {
  591. curve_id = s->s3->group_id;
  592. } else {
  593. for (i = 0; i < num_groups; i++) {
  594. if (!tls_curve_allowed(s, pgroups[i], SSL_SECOP_CURVE_SUPPORTED))
  595. continue;
  596. curve_id = pgroups[i];
  597. break;
  598. }
  599. }
  600. if (curve_id == 0) {
  601. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE,
  602. SSL_R_NO_SUITABLE_KEY_SHARE);
  603. return EXT_RETURN_FAIL;
  604. }
  605. if (!add_key_share(s, pkt, curve_id)) {
  606. /* SSLfatal() already called */
  607. return EXT_RETURN_FAIL;
  608. }
  609. if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
  610. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE,
  611. ERR_R_INTERNAL_ERROR);
  612. return EXT_RETURN_FAIL;
  613. }
  614. return EXT_RETURN_SENT;
  615. #else
  616. return EXT_RETURN_NOT_SENT;
  617. #endif
  618. }
  619. EXT_RETURN tls_construct_ctos_cookie(SSL *s, WPACKET *pkt, unsigned int context,
  620. X509 *x, size_t chainidx)
  621. {
  622. EXT_RETURN ret = EXT_RETURN_FAIL;
  623. /* Should only be set if we've had an HRR */
  624. if (s->ext.tls13_cookie_len == 0)
  625. return EXT_RETURN_NOT_SENT;
  626. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie)
  627. /* Extension data sub-packet */
  628. || !WPACKET_start_sub_packet_u16(pkt)
  629. || !WPACKET_sub_memcpy_u16(pkt, s->ext.tls13_cookie,
  630. s->ext.tls13_cookie_len)
  631. || !WPACKET_close(pkt)) {
  632. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_COOKIE,
  633. ERR_R_INTERNAL_ERROR);
  634. goto end;
  635. }
  636. ret = EXT_RETURN_SENT;
  637. end:
  638. OPENSSL_free(s->ext.tls13_cookie);
  639. s->ext.tls13_cookie = NULL;
  640. s->ext.tls13_cookie_len = 0;
  641. return ret;
  642. }
  643. EXT_RETURN tls_construct_ctos_early_data(SSL *s, WPACKET *pkt,
  644. unsigned int context, X509 *x,
  645. size_t chainidx)
  646. {
  647. #ifndef OPENSSL_NO_PSK
  648. char identity[PSK_MAX_IDENTITY_LEN + 1];
  649. #endif /* OPENSSL_NO_PSK */
  650. const unsigned char *id = NULL;
  651. size_t idlen = 0;
  652. SSL_SESSION *psksess = NULL;
  653. SSL_SESSION *edsess = NULL;
  654. const EVP_MD *handmd = NULL;
  655. if (s->hello_retry_request == SSL_HRR_PENDING)
  656. handmd = ssl_handshake_md(s);
  657. if (s->psk_use_session_cb != NULL
  658. && (!s->psk_use_session_cb(s, handmd, &id, &idlen, &psksess)
  659. || (psksess != NULL
  660. && psksess->ssl_version != TLS1_3_VERSION))) {
  661. SSL_SESSION_free(psksess);
  662. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
  663. SSL_R_BAD_PSK);
  664. return EXT_RETURN_FAIL;
  665. }
  666. #ifndef OPENSSL_NO_PSK
  667. if (psksess == NULL && s->psk_client_callback != NULL) {
  668. unsigned char psk[PSK_MAX_PSK_LEN];
  669. size_t psklen = 0;
  670. memset(identity, 0, sizeof(identity));
  671. psklen = s->psk_client_callback(s, NULL, identity, sizeof(identity) - 1,
  672. psk, sizeof(psk));
  673. if (psklen > PSK_MAX_PSK_LEN) {
  674. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  675. SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA, ERR_R_INTERNAL_ERROR);
  676. return EXT_RETURN_FAIL;
  677. } else if (psklen > 0) {
  678. const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 };
  679. const SSL_CIPHER *cipher;
  680. idlen = strlen(identity);
  681. if (idlen > PSK_MAX_IDENTITY_LEN) {
  682. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  683. SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
  684. ERR_R_INTERNAL_ERROR);
  685. return EXT_RETURN_FAIL;
  686. }
  687. id = (unsigned char *)identity;
  688. /*
  689. * We found a PSK using an old style callback. We don't know
  690. * the digest so we default to SHA256 as per the TLSv1.3 spec
  691. */
  692. cipher = SSL_CIPHER_find(s, tls13_aes128gcmsha256_id);
  693. if (cipher == NULL) {
  694. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  695. SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
  696. ERR_R_INTERNAL_ERROR);
  697. return EXT_RETURN_FAIL;
  698. }
  699. psksess = SSL_SESSION_new();
  700. if (psksess == NULL
  701. || !SSL_SESSION_set1_master_key(psksess, psk, psklen)
  702. || !SSL_SESSION_set_cipher(psksess, cipher)
  703. || !SSL_SESSION_set_protocol_version(psksess, TLS1_3_VERSION)) {
  704. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  705. SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
  706. ERR_R_INTERNAL_ERROR);
  707. OPENSSL_cleanse(psk, psklen);
  708. return EXT_RETURN_FAIL;
  709. }
  710. OPENSSL_cleanse(psk, psklen);
  711. }
  712. }
  713. #endif /* OPENSSL_NO_PSK */
  714. SSL_SESSION_free(s->psksession);
  715. s->psksession = psksess;
  716. if (psksess != NULL) {
  717. OPENSSL_free(s->psksession_id);
  718. s->psksession_id = OPENSSL_memdup(id, idlen);
  719. if (s->psksession_id == NULL) {
  720. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  721. SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA, ERR_R_INTERNAL_ERROR);
  722. return EXT_RETURN_FAIL;
  723. }
  724. s->psksession_id_len = idlen;
  725. }
  726. if (s->early_data_state != SSL_EARLY_DATA_CONNECTING
  727. || (s->session->ext.max_early_data == 0
  728. && (psksess == NULL || psksess->ext.max_early_data == 0))) {
  729. s->max_early_data = 0;
  730. return EXT_RETURN_NOT_SENT;
  731. }
  732. edsess = s->session->ext.max_early_data != 0 ? s->session : psksess;
  733. s->max_early_data = edsess->ext.max_early_data;
  734. if (edsess->ext.hostname != NULL) {
  735. if (s->ext.hostname == NULL
  736. || (s->ext.hostname != NULL
  737. && strcmp(s->ext.hostname, edsess->ext.hostname) != 0)) {
  738. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  739. SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
  740. SSL_R_INCONSISTENT_EARLY_DATA_SNI);
  741. return EXT_RETURN_FAIL;
  742. }
  743. }
  744. if ((s->ext.alpn == NULL && edsess->ext.alpn_selected != NULL)) {
  745. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
  746. SSL_R_INCONSISTENT_EARLY_DATA_ALPN);
  747. return EXT_RETURN_FAIL;
  748. }
  749. /*
  750. * Verify that we are offering an ALPN protocol consistent with the early
  751. * data.
  752. */
  753. if (edsess->ext.alpn_selected != NULL) {
  754. PACKET prots, alpnpkt;
  755. int found = 0;
  756. if (!PACKET_buf_init(&prots, s->ext.alpn, s->ext.alpn_len)) {
  757. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  758. SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA, ERR_R_INTERNAL_ERROR);
  759. return EXT_RETURN_FAIL;
  760. }
  761. while (PACKET_get_length_prefixed_1(&prots, &alpnpkt)) {
  762. if (PACKET_equal(&alpnpkt, edsess->ext.alpn_selected,
  763. edsess->ext.alpn_selected_len)) {
  764. found = 1;
  765. break;
  766. }
  767. }
  768. if (!found) {
  769. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  770. SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
  771. SSL_R_INCONSISTENT_EARLY_DATA_ALPN);
  772. return EXT_RETURN_FAIL;
  773. }
  774. }
  775. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
  776. || !WPACKET_start_sub_packet_u16(pkt)
  777. || !WPACKET_close(pkt)) {
  778. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
  779. ERR_R_INTERNAL_ERROR);
  780. return EXT_RETURN_FAIL;
  781. }
  782. /*
  783. * We set this to rejected here. Later, if the server acknowledges the
  784. * extension, we set it to accepted.
  785. */
  786. s->ext.early_data = SSL_EARLY_DATA_REJECTED;
  787. s->ext.early_data_ok = 1;
  788. return EXT_RETURN_SENT;
  789. }
  790. #define F5_WORKAROUND_MIN_MSG_LEN 0xff
  791. #define F5_WORKAROUND_MAX_MSG_LEN 0x200
  792. /*
  793. * PSK pre binder overhead =
  794. * 2 bytes for TLSEXT_TYPE_psk
  795. * 2 bytes for extension length
  796. * 2 bytes for identities list length
  797. * 2 bytes for identity length
  798. * 4 bytes for obfuscated_ticket_age
  799. * 2 bytes for binder list length
  800. * 1 byte for binder length
  801. * The above excludes the number of bytes for the identity itself and the
  802. * subsequent binder bytes
  803. */
  804. #define PSK_PRE_BINDER_OVERHEAD (2 + 2 + 2 + 2 + 4 + 2 + 1)
  805. EXT_RETURN tls_construct_ctos_padding(SSL *s, WPACKET *pkt,
  806. unsigned int context, X509 *x,
  807. size_t chainidx)
  808. {
  809. unsigned char *padbytes;
  810. size_t hlen;
  811. if ((s->options & SSL_OP_TLSEXT_PADDING) == 0)
  812. return EXT_RETURN_NOT_SENT;
  813. /*
  814. * Add padding to workaround bugs in F5 terminators. See RFC7685.
  815. * This code calculates the length of all extensions added so far but
  816. * excludes the PSK extension (because that MUST be written last). Therefore
  817. * this extension MUST always appear second to last.
  818. */
  819. if (!WPACKET_get_total_written(pkt, &hlen)) {
  820. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PADDING,
  821. ERR_R_INTERNAL_ERROR);
  822. return EXT_RETURN_FAIL;
  823. }
  824. /*
  825. * If we're going to send a PSK then that will be written out after this
  826. * extension, so we need to calculate how long it is going to be.
  827. */
  828. if (s->session->ssl_version == TLS1_3_VERSION
  829. && s->session->ext.ticklen != 0
  830. && s->session->cipher != NULL) {
  831. const EVP_MD *md = ssl_md(s->session->cipher->algorithm2);
  832. if (md != NULL) {
  833. /*
  834. * Add the fixed PSK overhead, the identity length and the binder
  835. * length.
  836. */
  837. hlen += PSK_PRE_BINDER_OVERHEAD + s->session->ext.ticklen
  838. + EVP_MD_size(md);
  839. }
  840. }
  841. if (hlen > F5_WORKAROUND_MIN_MSG_LEN && hlen < F5_WORKAROUND_MAX_MSG_LEN) {
  842. /* Calculate the amount of padding we need to add */
  843. hlen = F5_WORKAROUND_MAX_MSG_LEN - hlen;
  844. /*
  845. * Take off the size of extension header itself (2 bytes for type and
  846. * 2 bytes for length bytes), but ensure that the extension is at least
  847. * 1 byte long so as not to have an empty extension last (WebSphere 7.x,
  848. * 8.x are intolerant of that condition)
  849. */
  850. if (hlen > 4)
  851. hlen -= 4;
  852. else
  853. hlen = 1;
  854. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding)
  855. || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) {
  856. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PADDING,
  857. ERR_R_INTERNAL_ERROR);
  858. return EXT_RETURN_FAIL;
  859. }
  860. memset(padbytes, 0, hlen);
  861. }
  862. return EXT_RETURN_SENT;
  863. }
  864. /*
  865. * Construct the pre_shared_key extension
  866. */
  867. EXT_RETURN tls_construct_ctos_psk(SSL *s, WPACKET *pkt, unsigned int context,
  868. X509 *x, size_t chainidx)
  869. {
  870. #ifndef OPENSSL_NO_TLS1_3
  871. uint32_t now, agesec, agems = 0;
  872. size_t reshashsize = 0, pskhashsize = 0, binderoffset, msglen;
  873. unsigned char *resbinder = NULL, *pskbinder = NULL, *msgstart = NULL;
  874. const EVP_MD *handmd = NULL, *mdres = NULL, *mdpsk = NULL;
  875. int dores = 0;
  876. s->session->ext.tick_identity = TLSEXT_PSK_BAD_IDENTITY;
  877. /*
  878. * Note: At this stage of the code we only support adding a single
  879. * resumption PSK. If we add support for multiple PSKs then the length
  880. * calculations in the padding extension will need to be adjusted.
  881. */
  882. /*
  883. * If this is an incompatible or new session then we have nothing to resume
  884. * so don't add this extension.
  885. */
  886. if (s->session->ssl_version != TLS1_3_VERSION
  887. || (s->session->ext.ticklen == 0 && s->psksession == NULL))
  888. return EXT_RETURN_NOT_SENT;
  889. if (s->hello_retry_request == SSL_HRR_PENDING)
  890. handmd = ssl_handshake_md(s);
  891. if (s->session->ext.ticklen != 0) {
  892. /* Get the digest associated with the ciphersuite in the session */
  893. if (s->session->cipher == NULL) {
  894. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
  895. ERR_R_INTERNAL_ERROR);
  896. return EXT_RETURN_FAIL;
  897. }
  898. mdres = ssl_md(s->session->cipher->algorithm2);
  899. if (mdres == NULL) {
  900. /*
  901. * Don't recognize this cipher so we can't use the session.
  902. * Ignore it
  903. */
  904. goto dopsksess;
  905. }
  906. if (s->hello_retry_request == SSL_HRR_PENDING && mdres != handmd) {
  907. /*
  908. * Selected ciphersuite hash does not match the hash for the session
  909. * so we can't use it.
  910. */
  911. goto dopsksess;
  912. }
  913. /*
  914. * Technically the C standard just says time() returns a time_t and says
  915. * nothing about the encoding of that type. In practice most
  916. * implementations follow POSIX which holds it as an integral type in
  917. * seconds since epoch. We've already made the assumption that we can do
  918. * this in multiple places in the code, so portability shouldn't be an
  919. * issue.
  920. */
  921. now = (uint32_t)time(NULL);
  922. agesec = now - (uint32_t)s->session->time;
  923. /*
  924. * We calculate the age in seconds but the server may work in ms. Due to
  925. * rounding errors we could overestimate the age by up to 1s. It is
  926. * better to underestimate it. Otherwise, if the RTT is very short, when
  927. * the server calculates the age reported by the client it could be
  928. * bigger than the age calculated on the server - which should never
  929. * happen.
  930. */
  931. if (agesec > 0)
  932. agesec--;
  933. if (s->session->ext.tick_lifetime_hint < agesec) {
  934. /* Ticket is too old. Ignore it. */
  935. goto dopsksess;
  936. }
  937. /*
  938. * Calculate age in ms. We're just doing it to nearest second. Should be
  939. * good enough.
  940. */
  941. agems = agesec * (uint32_t)1000;
  942. if (agesec != 0 && agems / (uint32_t)1000 != agesec) {
  943. /*
  944. * Overflow. Shouldn't happen unless this is a *really* old session.
  945. * If so we just ignore it.
  946. */
  947. goto dopsksess;
  948. }
  949. /*
  950. * Obfuscate the age. Overflow here is fine, this addition is supposed
  951. * to be mod 2^32.
  952. */
  953. agems += s->session->ext.tick_age_add;
  954. reshashsize = EVP_MD_size(mdres);
  955. dores = 1;
  956. }
  957. dopsksess:
  958. if (!dores && s->psksession == NULL)
  959. return EXT_RETURN_NOT_SENT;
  960. if (s->psksession != NULL) {
  961. mdpsk = ssl_md(s->psksession->cipher->algorithm2);
  962. if (mdpsk == NULL) {
  963. /*
  964. * Don't recognize this cipher so we can't use the session.
  965. * If this happens it's an application bug.
  966. */
  967. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
  968. SSL_R_BAD_PSK);
  969. return EXT_RETURN_FAIL;
  970. }
  971. if (s->hello_retry_request == SSL_HRR_PENDING && mdpsk != handmd) {
  972. /*
  973. * Selected ciphersuite hash does not match the hash for the PSK
  974. * session. This is an application bug.
  975. */
  976. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
  977. SSL_R_BAD_PSK);
  978. return EXT_RETURN_FAIL;
  979. }
  980. pskhashsize = EVP_MD_size(mdpsk);
  981. }
  982. /* Create the extension, but skip over the binder for now */
  983. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)
  984. || !WPACKET_start_sub_packet_u16(pkt)
  985. || !WPACKET_start_sub_packet_u16(pkt)) {
  986. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
  987. ERR_R_INTERNAL_ERROR);
  988. return EXT_RETURN_FAIL;
  989. }
  990. if (dores) {
  991. if (!WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick,
  992. s->session->ext.ticklen)
  993. || !WPACKET_put_bytes_u32(pkt, agems)) {
  994. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
  995. ERR_R_INTERNAL_ERROR);
  996. return EXT_RETURN_FAIL;
  997. }
  998. }
  999. if (s->psksession != NULL) {
  1000. if (!WPACKET_sub_memcpy_u16(pkt, s->psksession_id,
  1001. s->psksession_id_len)
  1002. || !WPACKET_put_bytes_u32(pkt, 0)) {
  1003. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
  1004. ERR_R_INTERNAL_ERROR);
  1005. return EXT_RETURN_FAIL;
  1006. }
  1007. }
  1008. if (!WPACKET_close(pkt)
  1009. || !WPACKET_get_total_written(pkt, &binderoffset)
  1010. || !WPACKET_start_sub_packet_u16(pkt)
  1011. || (dores
  1012. && !WPACKET_sub_allocate_bytes_u8(pkt, reshashsize, &resbinder))
  1013. || (s->psksession != NULL
  1014. && !WPACKET_sub_allocate_bytes_u8(pkt, pskhashsize, &pskbinder))
  1015. || !WPACKET_close(pkt)
  1016. || !WPACKET_close(pkt)
  1017. || !WPACKET_get_total_written(pkt, &msglen)
  1018. /*
  1019. * We need to fill in all the sub-packet lengths now so we can
  1020. * calculate the HMAC of the message up to the binders
  1021. */
  1022. || !WPACKET_fill_lengths(pkt)) {
  1023. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
  1024. ERR_R_INTERNAL_ERROR);
  1025. return EXT_RETURN_FAIL;
  1026. }
  1027. msgstart = WPACKET_get_curr(pkt) - msglen;
  1028. if (dores
  1029. && tls_psk_do_binder(s, mdres, msgstart, binderoffset, NULL,
  1030. resbinder, s->session, 1, 0) != 1) {
  1031. /* SSLfatal() already called */
  1032. return EXT_RETURN_FAIL;
  1033. }
  1034. if (s->psksession != NULL
  1035. && tls_psk_do_binder(s, mdpsk, msgstart, binderoffset, NULL,
  1036. pskbinder, s->psksession, 1, 1) != 1) {
  1037. /* SSLfatal() already called */
  1038. return EXT_RETURN_FAIL;
  1039. }
  1040. if (dores)
  1041. s->session->ext.tick_identity = 0;
  1042. if (s->psksession != NULL)
  1043. s->psksession->ext.tick_identity = (dores ? 1 : 0);
  1044. return EXT_RETURN_SENT;
  1045. #else
  1046. return EXT_RETURN_NOT_SENT;
  1047. #endif
  1048. }
  1049. EXT_RETURN tls_construct_ctos_post_handshake_auth(SSL *s, WPACKET *pkt,
  1050. unsigned int context,
  1051. X509 *x, size_t chainidx)
  1052. {
  1053. #ifndef OPENSSL_NO_TLS1_3
  1054. if (!s->pha_forced) {
  1055. int i, n = 0;
  1056. /* check for cert, if present, we can do post-handshake auth */
  1057. if (s->cert == NULL)
  1058. return EXT_RETURN_NOT_SENT;
  1059. for (i = 0; i < SSL_PKEY_NUM; i++) {
  1060. if (s->cert->pkeys[i].x509 != NULL
  1061. && s->cert->pkeys[i].privatekey != NULL)
  1062. n++;
  1063. }
  1064. /* no identity certificates, so no extension */
  1065. if (n == 0)
  1066. return EXT_RETURN_NOT_SENT;
  1067. }
  1068. /* construct extension - 0 length, no contents */
  1069. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_post_handshake_auth)
  1070. || !WPACKET_start_sub_packet_u16(pkt)
  1071. || !WPACKET_close(pkt)) {
  1072. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1073. SSL_F_TLS_CONSTRUCT_CTOS_POST_HANDSHAKE_AUTH,
  1074. ERR_R_INTERNAL_ERROR);
  1075. return EXT_RETURN_FAIL;
  1076. }
  1077. s->post_handshake_auth = SSL_PHA_EXT_SENT;
  1078. return EXT_RETURN_SENT;
  1079. #else
  1080. return EXT_RETURN_NOT_SENT;
  1081. #endif
  1082. }
  1083. /*
  1084. * Parse the server's renegotiation binding and abort if it's not right
  1085. */
  1086. int tls_parse_stoc_renegotiate(SSL *s, PACKET *pkt, unsigned int context,
  1087. X509 *x, size_t chainidx)
  1088. {
  1089. size_t expected_len = s->s3->previous_client_finished_len
  1090. + s->s3->previous_server_finished_len;
  1091. size_t ilen;
  1092. const unsigned char *data;
  1093. /* Check for logic errors */
  1094. if (!ossl_assert(expected_len == 0
  1095. || s->s3->previous_client_finished_len != 0)
  1096. || !ossl_assert(expected_len == 0
  1097. || s->s3->previous_server_finished_len != 0)) {
  1098. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
  1099. ERR_R_INTERNAL_ERROR);
  1100. return 0;
  1101. }
  1102. /* Parse the length byte */
  1103. if (!PACKET_get_1_len(pkt, &ilen)) {
  1104. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
  1105. SSL_R_RENEGOTIATION_ENCODING_ERR);
  1106. return 0;
  1107. }
  1108. /* Consistency check */
  1109. if (PACKET_remaining(pkt) != ilen) {
  1110. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
  1111. SSL_R_RENEGOTIATION_ENCODING_ERR);
  1112. return 0;
  1113. }
  1114. /* Check that the extension matches */
  1115. if (ilen != expected_len) {
  1116. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
  1117. SSL_R_RENEGOTIATION_MISMATCH);
  1118. return 0;
  1119. }
  1120. if (!PACKET_get_bytes(pkt, &data, s->s3->previous_client_finished_len)
  1121. || memcmp(data, s->s3->previous_client_finished,
  1122. s->s3->previous_client_finished_len) != 0) {
  1123. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
  1124. SSL_R_RENEGOTIATION_MISMATCH);
  1125. return 0;
  1126. }
  1127. if (!PACKET_get_bytes(pkt, &data, s->s3->previous_server_finished_len)
  1128. || memcmp(data, s->s3->previous_server_finished,
  1129. s->s3->previous_server_finished_len) != 0) {
  1130. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
  1131. SSL_R_RENEGOTIATION_MISMATCH);
  1132. return 0;
  1133. }
  1134. s->s3->send_connection_binding = 1;
  1135. return 1;
  1136. }
  1137. /* Parse the server's max fragment len extension packet */
  1138. int tls_parse_stoc_maxfragmentlen(SSL *s, PACKET *pkt, unsigned int context,
  1139. X509 *x, size_t chainidx)
  1140. {
  1141. unsigned int value;
  1142. if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) {
  1143. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_MAXFRAGMENTLEN,
  1144. SSL_R_BAD_EXTENSION);
  1145. return 0;
  1146. }
  1147. /* |value| should contains a valid max-fragment-length code. */
  1148. if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) {
  1149. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1150. SSL_F_TLS_PARSE_STOC_MAXFRAGMENTLEN,
  1151. SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
  1152. return 0;
  1153. }
  1154. /* Must be the same value as client-configured one who was sent to server */
  1155. /*-
  1156. * RFC 6066: if a client receives a maximum fragment length negotiation
  1157. * response that differs from the length it requested, ...
  1158. * It must abort with SSL_AD_ILLEGAL_PARAMETER alert
  1159. */
  1160. if (value != s->ext.max_fragment_len_mode) {
  1161. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1162. SSL_F_TLS_PARSE_STOC_MAXFRAGMENTLEN,
  1163. SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
  1164. return 0;
  1165. }
  1166. /*
  1167. * Maximum Fragment Length Negotiation succeeded.
  1168. * The negotiated Maximum Fragment Length is binding now.
  1169. */
  1170. s->session->ext.max_fragment_len_mode = value;
  1171. return 1;
  1172. }
  1173. int tls_parse_stoc_server_name(SSL *s, PACKET *pkt, unsigned int context,
  1174. X509 *x, size_t chainidx)
  1175. {
  1176. if (s->ext.hostname == NULL) {
  1177. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_SERVER_NAME,
  1178. ERR_R_INTERNAL_ERROR);
  1179. return 0;
  1180. }
  1181. if (PACKET_remaining(pkt) > 0) {
  1182. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_SERVER_NAME,
  1183. SSL_R_BAD_EXTENSION);
  1184. return 0;
  1185. }
  1186. if (!s->hit) {
  1187. if (s->session->ext.hostname != NULL) {
  1188. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_SERVER_NAME,
  1189. ERR_R_INTERNAL_ERROR);
  1190. return 0;
  1191. }
  1192. s->session->ext.hostname = OPENSSL_strdup(s->ext.hostname);
  1193. if (s->session->ext.hostname == NULL) {
  1194. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_SERVER_NAME,
  1195. ERR_R_INTERNAL_ERROR);
  1196. return 0;
  1197. }
  1198. }
  1199. return 1;
  1200. }
  1201. #ifndef OPENSSL_NO_EC
  1202. int tls_parse_stoc_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context,
  1203. X509 *x, size_t chainidx)
  1204. {
  1205. size_t ecpointformats_len;
  1206. PACKET ecptformatlist;
  1207. if (!PACKET_as_length_prefixed_1(pkt, &ecptformatlist)) {
  1208. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_EC_PT_FORMATS,
  1209. SSL_R_BAD_EXTENSION);
  1210. return 0;
  1211. }
  1212. if (!s->hit) {
  1213. ecpointformats_len = PACKET_remaining(&ecptformatlist);
  1214. if (ecpointformats_len == 0) {
  1215. SSLfatal(s, SSL_AD_DECODE_ERROR,
  1216. SSL_F_TLS_PARSE_STOC_EC_PT_FORMATS, SSL_R_BAD_LENGTH);
  1217. return 0;
  1218. }
  1219. s->session->ext.ecpointformats_len = 0;
  1220. OPENSSL_free(s->session->ext.ecpointformats);
  1221. s->session->ext.ecpointformats = OPENSSL_malloc(ecpointformats_len);
  1222. if (s->session->ext.ecpointformats == NULL) {
  1223. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1224. SSL_F_TLS_PARSE_STOC_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
  1225. return 0;
  1226. }
  1227. s->session->ext.ecpointformats_len = ecpointformats_len;
  1228. if (!PACKET_copy_bytes(&ecptformatlist,
  1229. s->session->ext.ecpointformats,
  1230. ecpointformats_len)) {
  1231. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1232. SSL_F_TLS_PARSE_STOC_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
  1233. return 0;
  1234. }
  1235. }
  1236. return 1;
  1237. }
  1238. #endif
  1239. int tls_parse_stoc_session_ticket(SSL *s, PACKET *pkt, unsigned int context,
  1240. X509 *x, size_t chainidx)
  1241. {
  1242. if (s->ext.session_ticket_cb != NULL &&
  1243. !s->ext.session_ticket_cb(s, PACKET_data(pkt),
  1244. PACKET_remaining(pkt),
  1245. s->ext.session_ticket_cb_arg)) {
  1246. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  1247. SSL_F_TLS_PARSE_STOC_SESSION_TICKET, SSL_R_BAD_EXTENSION);
  1248. return 0;
  1249. }
  1250. if (!tls_use_ticket(s)) {
  1251. SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION,
  1252. SSL_F_TLS_PARSE_STOC_SESSION_TICKET, SSL_R_BAD_EXTENSION);
  1253. return 0;
  1254. }
  1255. if (PACKET_remaining(pkt) > 0) {
  1256. SSLfatal(s, SSL_AD_DECODE_ERROR,
  1257. SSL_F_TLS_PARSE_STOC_SESSION_TICKET, SSL_R_BAD_EXTENSION);
  1258. return 0;
  1259. }
  1260. s->ext.ticket_expected = 1;
  1261. return 1;
  1262. }
  1263. #ifndef OPENSSL_NO_OCSP
  1264. int tls_parse_stoc_status_request(SSL *s, PACKET *pkt, unsigned int context,
  1265. X509 *x, size_t chainidx)
  1266. {
  1267. if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) {
  1268. /* We ignore this if the server sends a CertificateRequest */
  1269. /* TODO(TLS1.3): Add support for this */
  1270. return 1;
  1271. }
  1272. /*
  1273. * MUST only be sent if we've requested a status
  1274. * request message. In TLS <= 1.2 it must also be empty.
  1275. */
  1276. if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) {
  1277. SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION,
  1278. SSL_F_TLS_PARSE_STOC_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
  1279. return 0;
  1280. }
  1281. if (!SSL_IS_TLS13(s) && PACKET_remaining(pkt) > 0) {
  1282. SSLfatal(s, SSL_AD_DECODE_ERROR,
  1283. SSL_F_TLS_PARSE_STOC_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
  1284. return 0;
  1285. }
  1286. if (SSL_IS_TLS13(s)) {
  1287. /* We only know how to handle this if it's for the first Certificate in
  1288. * the chain. We ignore any other responses.
  1289. */
  1290. if (chainidx != 0)
  1291. return 1;
  1292. /* SSLfatal() already called */
  1293. return tls_process_cert_status_body(s, pkt);
  1294. }
  1295. /* Set flag to expect CertificateStatus message */
  1296. s->ext.status_expected = 1;
  1297. return 1;
  1298. }
  1299. #endif
  1300. #ifndef OPENSSL_NO_CT
  1301. int tls_parse_stoc_sct(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1302. size_t chainidx)
  1303. {
  1304. if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) {
  1305. /* We ignore this if the server sends it in a CertificateRequest */
  1306. /* TODO(TLS1.3): Add support for this */
  1307. return 1;
  1308. }
  1309. /*
  1310. * Only take it if we asked for it - i.e if there is no CT validation
  1311. * callback set, then a custom extension MAY be processing it, so we
  1312. * need to let control continue to flow to that.
  1313. */
  1314. if (s->ct_validation_callback != NULL) {
  1315. size_t size = PACKET_remaining(pkt);
  1316. /* Simply copy it off for later processing */
  1317. OPENSSL_free(s->ext.scts);
  1318. s->ext.scts = NULL;
  1319. s->ext.scts_len = (uint16_t)size;
  1320. if (size > 0) {
  1321. s->ext.scts = OPENSSL_malloc(size);
  1322. if (s->ext.scts == NULL
  1323. || !PACKET_copy_bytes(pkt, s->ext.scts, size)) {
  1324. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_SCT,
  1325. ERR_R_INTERNAL_ERROR);
  1326. return 0;
  1327. }
  1328. }
  1329. } else {
  1330. ENDPOINT role = (context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0
  1331. ? ENDPOINT_CLIENT : ENDPOINT_BOTH;
  1332. /*
  1333. * If we didn't ask for it then there must be a custom extension,
  1334. * otherwise this is unsolicited.
  1335. */
  1336. if (custom_ext_find(&s->cert->custext, role,
  1337. TLSEXT_TYPE_signed_certificate_timestamp,
  1338. NULL) == NULL) {
  1339. SSLfatal(s, TLS1_AD_UNSUPPORTED_EXTENSION, SSL_F_TLS_PARSE_STOC_SCT,
  1340. SSL_R_BAD_EXTENSION);
  1341. return 0;
  1342. }
  1343. if (!custom_ext_parse(s, context,
  1344. TLSEXT_TYPE_signed_certificate_timestamp,
  1345. PACKET_data(pkt), PACKET_remaining(pkt),
  1346. x, chainidx)) {
  1347. /* SSLfatal already called */
  1348. return 0;
  1349. }
  1350. }
  1351. return 1;
  1352. }
  1353. #endif
  1354. #ifndef OPENSSL_NO_NEXTPROTONEG
  1355. /*
  1356. * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
  1357. * elements of zero length are allowed and the set of elements must exactly
  1358. * fill the length of the block. Returns 1 on success or 0 on failure.
  1359. */
  1360. static int ssl_next_proto_validate(SSL *s, PACKET *pkt)
  1361. {
  1362. PACKET tmp_protocol;
  1363. while (PACKET_remaining(pkt)) {
  1364. if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)
  1365. || PACKET_remaining(&tmp_protocol) == 0) {
  1366. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL_NEXT_PROTO_VALIDATE,
  1367. SSL_R_BAD_EXTENSION);
  1368. return 0;
  1369. }
  1370. }
  1371. return 1;
  1372. }
  1373. int tls_parse_stoc_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1374. size_t chainidx)
  1375. {
  1376. unsigned char *selected;
  1377. unsigned char selected_len;
  1378. PACKET tmppkt;
  1379. /* Check if we are in a renegotiation. If so ignore this extension */
  1380. if (!SSL_IS_FIRST_HANDSHAKE(s))
  1381. return 1;
  1382. /* We must have requested it. */
  1383. if (s->ctx->ext.npn_select_cb == NULL) {
  1384. SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_F_TLS_PARSE_STOC_NPN,
  1385. SSL_R_BAD_EXTENSION);
  1386. return 0;
  1387. }
  1388. /* The data must be valid */
  1389. tmppkt = *pkt;
  1390. if (!ssl_next_proto_validate(s, &tmppkt)) {
  1391. /* SSLfatal() already called */
  1392. return 0;
  1393. }
  1394. if (s->ctx->ext.npn_select_cb(s, &selected, &selected_len,
  1395. PACKET_data(pkt),
  1396. PACKET_remaining(pkt),
  1397. s->ctx->ext.npn_select_cb_arg) !=
  1398. SSL_TLSEXT_ERR_OK) {
  1399. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_PARSE_STOC_NPN,
  1400. SSL_R_BAD_EXTENSION);
  1401. return 0;
  1402. }
  1403. /*
  1404. * Could be non-NULL if server has sent multiple NPN extensions in
  1405. * a single Serverhello
  1406. */
  1407. OPENSSL_free(s->ext.npn);
  1408. s->ext.npn = OPENSSL_malloc(selected_len);
  1409. if (s->ext.npn == NULL) {
  1410. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_NPN,
  1411. ERR_R_INTERNAL_ERROR);
  1412. return 0;
  1413. }
  1414. memcpy(s->ext.npn, selected, selected_len);
  1415. s->ext.npn_len = selected_len;
  1416. s->s3->npn_seen = 1;
  1417. return 1;
  1418. }
  1419. #endif
  1420. int tls_parse_stoc_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1421. size_t chainidx)
  1422. {
  1423. size_t len;
  1424. /* We must have requested it. */
  1425. if (!s->s3->alpn_sent) {
  1426. SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_F_TLS_PARSE_STOC_ALPN,
  1427. SSL_R_BAD_EXTENSION);
  1428. return 0;
  1429. }
  1430. /*-
  1431. * The extension data consists of:
  1432. * uint16 list_length
  1433. * uint8 proto_length;
  1434. * uint8 proto[proto_length];
  1435. */
  1436. if (!PACKET_get_net_2_len(pkt, &len)
  1437. || PACKET_remaining(pkt) != len || !PACKET_get_1_len(pkt, &len)
  1438. || PACKET_remaining(pkt) != len) {
  1439. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
  1440. SSL_R_BAD_EXTENSION);
  1441. return 0;
  1442. }
  1443. OPENSSL_free(s->s3->alpn_selected);
  1444. s->s3->alpn_selected = OPENSSL_malloc(len);
  1445. if (s->s3->alpn_selected == NULL) {
  1446. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
  1447. ERR_R_INTERNAL_ERROR);
  1448. return 0;
  1449. }
  1450. if (!PACKET_copy_bytes(pkt, s->s3->alpn_selected, len)) {
  1451. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
  1452. SSL_R_BAD_EXTENSION);
  1453. return 0;
  1454. }
  1455. s->s3->alpn_selected_len = len;
  1456. if (s->session->ext.alpn_selected == NULL
  1457. || s->session->ext.alpn_selected_len != len
  1458. || memcmp(s->session->ext.alpn_selected, s->s3->alpn_selected, len)
  1459. != 0) {
  1460. /* ALPN not consistent with the old session so cannot use early_data */
  1461. s->ext.early_data_ok = 0;
  1462. }
  1463. if (!s->hit) {
  1464. /*
  1465. * This is a new session and so alpn_selected should have been
  1466. * initialised to NULL. We should update it with the selected ALPN.
  1467. */
  1468. if (!ossl_assert(s->session->ext.alpn_selected == NULL)) {
  1469. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
  1470. ERR_R_INTERNAL_ERROR);
  1471. return 0;
  1472. }
  1473. s->session->ext.alpn_selected =
  1474. OPENSSL_memdup(s->s3->alpn_selected, s->s3->alpn_selected_len);
  1475. if (s->session->ext.alpn_selected == NULL) {
  1476. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
  1477. ERR_R_INTERNAL_ERROR);
  1478. return 0;
  1479. }
  1480. s->session->ext.alpn_selected_len = s->s3->alpn_selected_len;
  1481. }
  1482. return 1;
  1483. }
  1484. #ifndef OPENSSL_NO_SRTP
  1485. int tls_parse_stoc_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1486. size_t chainidx)
  1487. {
  1488. unsigned int id, ct, mki;
  1489. int i;
  1490. STACK_OF(SRTP_PROTECTION_PROFILE) *clnt;
  1491. SRTP_PROTECTION_PROFILE *prof;
  1492. if (!PACKET_get_net_2(pkt, &ct) || ct != 2
  1493. || !PACKET_get_net_2(pkt, &id)
  1494. || !PACKET_get_1(pkt, &mki)
  1495. || PACKET_remaining(pkt) != 0) {
  1496. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_USE_SRTP,
  1497. SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1498. return 0;
  1499. }
  1500. if (mki != 0) {
  1501. /* Must be no MKI, since we never offer one */
  1502. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_USE_SRTP,
  1503. SSL_R_BAD_SRTP_MKI_VALUE);
  1504. return 0;
  1505. }
  1506. /* Throw an error if the server gave us an unsolicited extension */
  1507. clnt = SSL_get_srtp_profiles(s);
  1508. if (clnt == NULL) {
  1509. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_USE_SRTP,
  1510. SSL_R_NO_SRTP_PROFILES);
  1511. return 0;
  1512. }
  1513. /*
  1514. * Check to see if the server gave us something we support (and
  1515. * presumably offered)
  1516. */
  1517. for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) {
  1518. prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
  1519. if (prof->id == id) {
  1520. s->srtp_profile = prof;
  1521. return 1;
  1522. }
  1523. }
  1524. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_USE_SRTP,
  1525. SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1526. return 0;
  1527. }
  1528. #endif
  1529. int tls_parse_stoc_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1530. size_t chainidx)
  1531. {
  1532. /* Ignore if inappropriate ciphersuite */
  1533. if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
  1534. && s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
  1535. && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4)
  1536. s->ext.use_etm = 1;
  1537. return 1;
  1538. }
  1539. int tls_parse_stoc_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1540. size_t chainidx)
  1541. {
  1542. s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
  1543. if (!s->hit)
  1544. s->session->flags |= SSL_SESS_FLAG_EXTMS;
  1545. return 1;
  1546. }
  1547. int tls_parse_stoc_supported_versions(SSL *s, PACKET *pkt, unsigned int context,
  1548. X509 *x, size_t chainidx)
  1549. {
  1550. unsigned int version;
  1551. if (!PACKET_get_net_2(pkt, &version)
  1552. || PACKET_remaining(pkt) != 0) {
  1553. SSLfatal(s, SSL_AD_DECODE_ERROR,
  1554. SSL_F_TLS_PARSE_STOC_SUPPORTED_VERSIONS,
  1555. SSL_R_LENGTH_MISMATCH);
  1556. return 0;
  1557. }
  1558. /* TODO(TLS1.3): Remove this before release */
  1559. if (version == TLS1_3_VERSION_DRAFT)
  1560. version = TLS1_3_VERSION;
  1561. /*
  1562. * The only protocol version we support which is valid in this extension in
  1563. * a ServerHello is TLSv1.3 therefore we shouldn't be getting anything else.
  1564. */
  1565. if (version != TLS1_3_VERSION) {
  1566. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1567. SSL_F_TLS_PARSE_STOC_SUPPORTED_VERSIONS,
  1568. SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
  1569. return 0;
  1570. }
  1571. /* We ignore this extension for HRRs except to sanity check it */
  1572. if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST)
  1573. return 1;
  1574. /* We just set it here. We validate it in ssl_choose_client_version */
  1575. s->version = version;
  1576. return 1;
  1577. }
  1578. int tls_parse_stoc_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1579. size_t chainidx)
  1580. {
  1581. #ifndef OPENSSL_NO_TLS1_3
  1582. unsigned int group_id;
  1583. PACKET encoded_pt;
  1584. EVP_PKEY *ckey = s->s3->tmp.pkey, *skey = NULL;
  1585. /* Sanity check */
  1586. if (ckey == NULL || s->s3->peer_tmp != NULL) {
  1587. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
  1588. ERR_R_INTERNAL_ERROR);
  1589. return 0;
  1590. }
  1591. if (!PACKET_get_net_2(pkt, &group_id)) {
  1592. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
  1593. SSL_R_LENGTH_MISMATCH);
  1594. return 0;
  1595. }
  1596. if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0) {
  1597. const uint16_t *pgroups = NULL;
  1598. size_t i, num_groups;
  1599. if (PACKET_remaining(pkt) != 0) {
  1600. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
  1601. SSL_R_LENGTH_MISMATCH);
  1602. return 0;
  1603. }
  1604. /*
  1605. * It is an error if the HelloRetryRequest wants a key_share that we
  1606. * already sent in the first ClientHello
  1607. */
  1608. if (group_id == s->s3->group_id) {
  1609. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1610. SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
  1611. return 0;
  1612. }
  1613. /* Validate the selected group is one we support */
  1614. tls1_get_supported_groups(s, &pgroups, &num_groups);
  1615. for (i = 0; i < num_groups; i++) {
  1616. if (group_id == pgroups[i])
  1617. break;
  1618. }
  1619. if (i >= num_groups
  1620. || !tls_curve_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED)) {
  1621. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1622. SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
  1623. return 0;
  1624. }
  1625. s->s3->group_id = group_id;
  1626. EVP_PKEY_free(s->s3->tmp.pkey);
  1627. s->s3->tmp.pkey = NULL;
  1628. return 1;
  1629. }
  1630. if (group_id != s->s3->group_id) {
  1631. /*
  1632. * This isn't for the group that we sent in the original
  1633. * key_share!
  1634. */
  1635. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
  1636. SSL_R_BAD_KEY_SHARE);
  1637. return 0;
  1638. }
  1639. if (!PACKET_as_length_prefixed_2(pkt, &encoded_pt)
  1640. || PACKET_remaining(&encoded_pt) == 0) {
  1641. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
  1642. SSL_R_LENGTH_MISMATCH);
  1643. return 0;
  1644. }
  1645. skey = ssl_generate_pkey(ckey);
  1646. if (skey == NULL) {
  1647. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
  1648. ERR_R_MALLOC_FAILURE);
  1649. return 0;
  1650. }
  1651. if (!EVP_PKEY_set1_tls_encodedpoint(skey, PACKET_data(&encoded_pt),
  1652. PACKET_remaining(&encoded_pt))) {
  1653. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
  1654. SSL_R_BAD_ECPOINT);
  1655. EVP_PKEY_free(skey);
  1656. return 0;
  1657. }
  1658. if (ssl_derive(s, ckey, skey, 1) == 0) {
  1659. /* SSLfatal() already called */
  1660. EVP_PKEY_free(skey);
  1661. return 0;
  1662. }
  1663. s->s3->peer_tmp = skey;
  1664. #endif
  1665. return 1;
  1666. }
  1667. int tls_parse_stoc_cookie(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1668. size_t chainidx)
  1669. {
  1670. PACKET cookie;
  1671. if (!PACKET_as_length_prefixed_2(pkt, &cookie)
  1672. || !PACKET_memdup(&cookie, &s->ext.tls13_cookie,
  1673. &s->ext.tls13_cookie_len)) {
  1674. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_COOKIE,
  1675. SSL_R_LENGTH_MISMATCH);
  1676. return 0;
  1677. }
  1678. return 1;
  1679. }
  1680. int tls_parse_stoc_early_data(SSL *s, PACKET *pkt, unsigned int context,
  1681. X509 *x, size_t chainidx)
  1682. {
  1683. if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {
  1684. unsigned long max_early_data;
  1685. if (!PACKET_get_net_4(pkt, &max_early_data)
  1686. || PACKET_remaining(pkt) != 0) {
  1687. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_EARLY_DATA,
  1688. SSL_R_INVALID_MAX_EARLY_DATA);
  1689. return 0;
  1690. }
  1691. s->session->ext.max_early_data = max_early_data;
  1692. return 1;
  1693. }
  1694. if (PACKET_remaining(pkt) != 0) {
  1695. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_EARLY_DATA,
  1696. SSL_R_BAD_EXTENSION);
  1697. return 0;
  1698. }
  1699. if (!s->ext.early_data_ok
  1700. || !s->hit
  1701. || s->session->ext.tick_identity != 0) {
  1702. /*
  1703. * If we get here then we didn't send early data, or we didn't resume
  1704. * using the first identity, or the SNI/ALPN is not consistent so the
  1705. * server should not be accepting it.
  1706. */
  1707. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_EARLY_DATA,
  1708. SSL_R_BAD_EXTENSION);
  1709. return 0;
  1710. }
  1711. s->ext.early_data = SSL_EARLY_DATA_ACCEPTED;
  1712. return 1;
  1713. }
  1714. int tls_parse_stoc_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1715. size_t chainidx)
  1716. {
  1717. #ifndef OPENSSL_NO_TLS1_3
  1718. unsigned int identity;
  1719. if (!PACKET_get_net_2(pkt, &identity) || PACKET_remaining(pkt) != 0) {
  1720. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_PSK,
  1721. SSL_R_LENGTH_MISMATCH);
  1722. return 0;
  1723. }
  1724. if (s->session->ext.tick_identity == (int)identity) {
  1725. s->hit = 1;
  1726. SSL_SESSION_free(s->psksession);
  1727. s->psksession = NULL;
  1728. return 1;
  1729. }
  1730. if (s->psksession == NULL
  1731. || s->psksession->ext.tick_identity != (int)identity) {
  1732. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_PSK,
  1733. SSL_R_BAD_PSK_IDENTITY);
  1734. return 0;
  1735. }
  1736. /*
  1737. * If we used the external PSK for sending early_data then s->early_secret
  1738. * is already set up, so don't overwrite it. Otherwise we copy the
  1739. * early_secret across that we generated earlier.
  1740. */
  1741. if ((s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY
  1742. && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING)
  1743. || s->session->ext.max_early_data > 0
  1744. || s->psksession->ext.max_early_data == 0)
  1745. memcpy(s->early_secret, s->psksession->early_secret, EVP_MAX_MD_SIZE);
  1746. SSL_SESSION_free(s->session);
  1747. s->session = s->psksession;
  1748. s->psksession = NULL;
  1749. s->hit = 1;
  1750. #endif
  1751. return 1;
  1752. }