extensions_clnt.c 68 KB

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