extensions_srvr.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899
  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 "statem_locl.h"
  12. #include "internal/cryptlib.h"
  13. #define COOKIE_STATE_FORMAT_VERSION 0
  14. /*
  15. * 2 bytes for packet length, 2 bytes for format version, 2 bytes for
  16. * protocol version, 2 bytes for group id, 2 bytes for cipher id, 1 byte for
  17. * key_share present flag, 4 bytes for timestamp, 2 bytes for the hashlen,
  18. * EVP_MAX_MD_SIZE for transcript hash, 1 byte for app cookie length, app cookie
  19. * length bytes, SHA256_DIGEST_LENGTH bytes for the HMAC of the whole thing.
  20. */
  21. #define MAX_COOKIE_SIZE (2 + 2 + 2 + 2 + 2 + 1 + 4 + 2 + EVP_MAX_MD_SIZE + 1 \
  22. + SSL_COOKIE_LENGTH + SHA256_DIGEST_LENGTH)
  23. /*
  24. * Message header + 2 bytes for protocol version + number of random bytes +
  25. * + 1 byte for legacy session id length + number of bytes in legacy session id
  26. * + 2 bytes for ciphersuite + 1 byte for legacy compression
  27. * + 2 bytes for extension block length + 6 bytes for key_share extension
  28. * + 4 bytes for cookie extension header + the number of bytes in the cookie
  29. */
  30. #define MAX_HRR_SIZE (SSL3_HM_HEADER_LENGTH + 2 + SSL3_RANDOM_SIZE + 1 \
  31. + SSL_MAX_SSL_SESSION_ID_LENGTH + 2 + 1 + 2 + 6 + 4 \
  32. + MAX_COOKIE_SIZE)
  33. /*
  34. * Parse the client's renegotiation binding and abort if it's not right
  35. */
  36. int tls_parse_ctos_renegotiate(SSL *s, PACKET *pkt, unsigned int context,
  37. X509 *x, size_t chainidx)
  38. {
  39. unsigned int ilen;
  40. const unsigned char *data;
  41. /* Parse the length byte */
  42. if (!PACKET_get_1(pkt, &ilen)
  43. || !PACKET_get_bytes(pkt, &data, ilen)) {
  44. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_RENEGOTIATE,
  45. SSL_R_RENEGOTIATION_ENCODING_ERR);
  46. return 0;
  47. }
  48. /* Check that the extension matches */
  49. if (ilen != s->s3->previous_client_finished_len) {
  50. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_PARSE_CTOS_RENEGOTIATE,
  51. SSL_R_RENEGOTIATION_MISMATCH);
  52. return 0;
  53. }
  54. if (memcmp(data, s->s3->previous_client_finished,
  55. s->s3->previous_client_finished_len)) {
  56. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_PARSE_CTOS_RENEGOTIATE,
  57. SSL_R_RENEGOTIATION_MISMATCH);
  58. return 0;
  59. }
  60. s->s3->send_connection_binding = 1;
  61. return 1;
  62. }
  63. /*-
  64. * The servername extension is treated as follows:
  65. *
  66. * - Only the hostname type is supported with a maximum length of 255.
  67. * - The servername is rejected if too long or if it contains zeros,
  68. * in which case an fatal alert is generated.
  69. * - The servername field is maintained together with the session cache.
  70. * - When a session is resumed, the servername call back invoked in order
  71. * to allow the application to position itself to the right context.
  72. * - The servername is acknowledged if it is new for a session or when
  73. * it is identical to a previously used for the same session.
  74. * Applications can control the behaviour. They can at any time
  75. * set a 'desirable' servername for a new SSL object. This can be the
  76. * case for example with HTTPS when a Host: header field is received and
  77. * a renegotiation is requested. In this case, a possible servername
  78. * presented in the new client hello is only acknowledged if it matches
  79. * the value of the Host: field.
  80. * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
  81. * if they provide for changing an explicit servername context for the
  82. * session, i.e. when the session has been established with a servername
  83. * extension.
  84. * - On session reconnect, the servername extension may be absent.
  85. */
  86. int tls_parse_ctos_server_name(SSL *s, PACKET *pkt, unsigned int context,
  87. X509 *x, size_t chainidx)
  88. {
  89. unsigned int servname_type;
  90. PACKET sni, hostname;
  91. if (!PACKET_as_length_prefixed_2(pkt, &sni)
  92. /* ServerNameList must be at least 1 byte long. */
  93. || PACKET_remaining(&sni) == 0) {
  94. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_SERVER_NAME,
  95. SSL_R_BAD_EXTENSION);
  96. return 0;
  97. }
  98. /*
  99. * Although the intent was for server_name to be extensible, RFC 4366
  100. * was not clear about it; and so OpenSSL among other implementations,
  101. * always and only allows a 'host_name' name types.
  102. * RFC 6066 corrected the mistake but adding new name types
  103. * is nevertheless no longer feasible, so act as if no other
  104. * SNI types can exist, to simplify parsing.
  105. *
  106. * Also note that the RFC permits only one SNI value per type,
  107. * i.e., we can only have a single hostname.
  108. */
  109. if (!PACKET_get_1(&sni, &servname_type)
  110. || servname_type != TLSEXT_NAMETYPE_host_name
  111. || !PACKET_as_length_prefixed_2(&sni, &hostname)) {
  112. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_SERVER_NAME,
  113. SSL_R_BAD_EXTENSION);
  114. return 0;
  115. }
  116. if (!s->hit) {
  117. if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) {
  118. SSLfatal(s, SSL_AD_UNRECOGNIZED_NAME,
  119. SSL_F_TLS_PARSE_CTOS_SERVER_NAME,
  120. SSL_R_BAD_EXTENSION);
  121. return 0;
  122. }
  123. if (PACKET_contains_zero_byte(&hostname)) {
  124. SSLfatal(s, SSL_AD_UNRECOGNIZED_NAME,
  125. SSL_F_TLS_PARSE_CTOS_SERVER_NAME,
  126. SSL_R_BAD_EXTENSION);
  127. return 0;
  128. }
  129. OPENSSL_free(s->session->ext.hostname);
  130. s->session->ext.hostname = NULL;
  131. if (!PACKET_strndup(&hostname, &s->session->ext.hostname)) {
  132. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_SERVER_NAME,
  133. ERR_R_INTERNAL_ERROR);
  134. return 0;
  135. }
  136. s->servername_done = 1;
  137. } else {
  138. /*
  139. * TODO(openssl-team): if the SNI doesn't match, we MUST
  140. * fall back to a full handshake.
  141. */
  142. s->servername_done = s->session->ext.hostname
  143. && PACKET_equal(&hostname, s->session->ext.hostname,
  144. strlen(s->session->ext.hostname));
  145. if (!s->servername_done && s->session->ext.hostname != NULL)
  146. s->ext.early_data_ok = 0;
  147. }
  148. return 1;
  149. }
  150. int tls_parse_ctos_maxfragmentlen(SSL *s, PACKET *pkt, unsigned int context,
  151. X509 *x, size_t chainidx)
  152. {
  153. unsigned int value;
  154. if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) {
  155. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_MAXFRAGMENTLEN,
  156. SSL_R_BAD_EXTENSION);
  157. return 0;
  158. }
  159. /* Received |value| should be a valid max-fragment-length code. */
  160. if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) {
  161. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  162. SSL_F_TLS_PARSE_CTOS_MAXFRAGMENTLEN,
  163. SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
  164. return 0;
  165. }
  166. /*
  167. * RFC 6066: The negotiated length applies for the duration of the session
  168. * including session resumptions.
  169. * We should receive the same code as in resumed session !
  170. */
  171. if (s->hit && s->session->ext.max_fragment_len_mode != value) {
  172. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  173. SSL_F_TLS_PARSE_CTOS_MAXFRAGMENTLEN,
  174. SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
  175. return 0;
  176. }
  177. /*
  178. * Store it in session, so it'll become binding for us
  179. * and we'll include it in a next Server Hello.
  180. */
  181. s->session->ext.max_fragment_len_mode = value;
  182. return 1;
  183. }
  184. #ifndef OPENSSL_NO_SRP
  185. int tls_parse_ctos_srp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  186. size_t chainidx)
  187. {
  188. PACKET srp_I;
  189. if (!PACKET_as_length_prefixed_1(pkt, &srp_I)
  190. || PACKET_contains_zero_byte(&srp_I)) {
  191. SSLfatal(s, SSL_AD_DECODE_ERROR,
  192. SSL_F_TLS_PARSE_CTOS_SRP,
  193. SSL_R_BAD_EXTENSION);
  194. return 0;
  195. }
  196. /*
  197. * TODO(openssl-team): currently, we re-authenticate the user
  198. * upon resumption. Instead, we MUST ignore the login.
  199. */
  200. if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) {
  201. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_SRP,
  202. ERR_R_INTERNAL_ERROR);
  203. return 0;
  204. }
  205. return 1;
  206. }
  207. #endif
  208. #ifndef OPENSSL_NO_EC
  209. int tls_parse_ctos_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context,
  210. X509 *x, size_t chainidx)
  211. {
  212. PACKET ec_point_format_list;
  213. if (!PACKET_as_length_prefixed_1(pkt, &ec_point_format_list)
  214. || PACKET_remaining(&ec_point_format_list) == 0) {
  215. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_EC_PT_FORMATS,
  216. SSL_R_BAD_EXTENSION);
  217. return 0;
  218. }
  219. if (!s->hit) {
  220. if (!PACKET_memdup(&ec_point_format_list,
  221. &s->session->ext.ecpointformats,
  222. &s->session->ext.ecpointformats_len)) {
  223. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  224. SSL_F_TLS_PARSE_CTOS_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
  225. return 0;
  226. }
  227. }
  228. return 1;
  229. }
  230. #endif /* OPENSSL_NO_EC */
  231. int tls_parse_ctos_session_ticket(SSL *s, PACKET *pkt, unsigned int context,
  232. X509 *x, size_t chainidx)
  233. {
  234. if (s->ext.session_ticket_cb &&
  235. !s->ext.session_ticket_cb(s, PACKET_data(pkt),
  236. PACKET_remaining(pkt),
  237. s->ext.session_ticket_cb_arg)) {
  238. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  239. SSL_F_TLS_PARSE_CTOS_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
  240. return 0;
  241. }
  242. return 1;
  243. }
  244. int tls_parse_ctos_sig_algs_cert(SSL *s, PACKET *pkt, unsigned int context,
  245. X509 *x, size_t chainidx)
  246. {
  247. PACKET supported_sig_algs;
  248. if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)
  249. || PACKET_remaining(&supported_sig_algs) == 0) {
  250. SSLfatal(s, SSL_AD_DECODE_ERROR,
  251. SSL_F_TLS_PARSE_CTOS_SIG_ALGS_CERT, SSL_R_BAD_EXTENSION);
  252. return 0;
  253. }
  254. if (!s->hit && !tls1_save_sigalgs(s, &supported_sig_algs, 1)) {
  255. SSLfatal(s, SSL_AD_DECODE_ERROR,
  256. SSL_F_TLS_PARSE_CTOS_SIG_ALGS_CERT, SSL_R_BAD_EXTENSION);
  257. return 0;
  258. }
  259. return 1;
  260. }
  261. int tls_parse_ctos_sig_algs(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  262. size_t chainidx)
  263. {
  264. PACKET supported_sig_algs;
  265. if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)
  266. || PACKET_remaining(&supported_sig_algs) == 0) {
  267. SSLfatal(s, SSL_AD_DECODE_ERROR,
  268. SSL_F_TLS_PARSE_CTOS_SIG_ALGS, SSL_R_BAD_EXTENSION);
  269. return 0;
  270. }
  271. if (!s->hit && !tls1_save_sigalgs(s, &supported_sig_algs, 0)) {
  272. SSLfatal(s, SSL_AD_DECODE_ERROR,
  273. SSL_F_TLS_PARSE_CTOS_SIG_ALGS, SSL_R_BAD_EXTENSION);
  274. return 0;
  275. }
  276. return 1;
  277. }
  278. #ifndef OPENSSL_NO_OCSP
  279. int tls_parse_ctos_status_request(SSL *s, PACKET *pkt, unsigned int context,
  280. X509 *x, size_t chainidx)
  281. {
  282. PACKET responder_id_list, exts;
  283. /* We ignore this in a resumption handshake */
  284. if (s->hit)
  285. return 1;
  286. /* Not defined if we get one of these in a client Certificate */
  287. if (x != NULL)
  288. return 1;
  289. if (!PACKET_get_1(pkt, (unsigned int *)&s->ext.status_type)) {
  290. SSLfatal(s, SSL_AD_DECODE_ERROR,
  291. SSL_F_TLS_PARSE_CTOS_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
  292. return 0;
  293. }
  294. if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) {
  295. /*
  296. * We don't know what to do with any other type so ignore it.
  297. */
  298. s->ext.status_type = TLSEXT_STATUSTYPE_nothing;
  299. return 1;
  300. }
  301. if (!PACKET_get_length_prefixed_2 (pkt, &responder_id_list)) {
  302. SSLfatal(s, SSL_AD_DECODE_ERROR,
  303. SSL_F_TLS_PARSE_CTOS_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
  304. return 0;
  305. }
  306. /*
  307. * We remove any OCSP_RESPIDs from a previous handshake
  308. * to prevent unbounded memory growth - CVE-2016-6304
  309. */
  310. sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);
  311. if (PACKET_remaining(&responder_id_list) > 0) {
  312. s->ext.ocsp.ids = sk_OCSP_RESPID_new_null();
  313. if (s->ext.ocsp.ids == NULL) {
  314. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  315. SSL_F_TLS_PARSE_CTOS_STATUS_REQUEST, ERR_R_MALLOC_FAILURE);
  316. return 0;
  317. }
  318. } else {
  319. s->ext.ocsp.ids = NULL;
  320. }
  321. while (PACKET_remaining(&responder_id_list) > 0) {
  322. OCSP_RESPID *id;
  323. PACKET responder_id;
  324. const unsigned char *id_data;
  325. if (!PACKET_get_length_prefixed_2(&responder_id_list, &responder_id)
  326. || PACKET_remaining(&responder_id) == 0) {
  327. SSLfatal(s, SSL_AD_DECODE_ERROR,
  328. SSL_F_TLS_PARSE_CTOS_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
  329. return 0;
  330. }
  331. id_data = PACKET_data(&responder_id);
  332. /* TODO(size_t): Convert d2i_* to size_t */
  333. id = d2i_OCSP_RESPID(NULL, &id_data,
  334. (int)PACKET_remaining(&responder_id));
  335. if (id == NULL) {
  336. SSLfatal(s, SSL_AD_DECODE_ERROR,
  337. SSL_F_TLS_PARSE_CTOS_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
  338. return 0;
  339. }
  340. if (id_data != PACKET_end(&responder_id)) {
  341. OCSP_RESPID_free(id);
  342. SSLfatal(s, SSL_AD_DECODE_ERROR,
  343. SSL_F_TLS_PARSE_CTOS_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
  344. return 0;
  345. }
  346. if (!sk_OCSP_RESPID_push(s->ext.ocsp.ids, id)) {
  347. OCSP_RESPID_free(id);
  348. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  349. SSL_F_TLS_PARSE_CTOS_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
  350. return 0;
  351. }
  352. }
  353. /* Read in request_extensions */
  354. if (!PACKET_as_length_prefixed_2(pkt, &exts)) {
  355. SSLfatal(s, SSL_AD_DECODE_ERROR,
  356. SSL_F_TLS_PARSE_CTOS_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
  357. return 0;
  358. }
  359. if (PACKET_remaining(&exts) > 0) {
  360. const unsigned char *ext_data = PACKET_data(&exts);
  361. sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts,
  362. X509_EXTENSION_free);
  363. s->ext.ocsp.exts =
  364. d2i_X509_EXTENSIONS(NULL, &ext_data, (int)PACKET_remaining(&exts));
  365. if (s->ext.ocsp.exts == NULL || ext_data != PACKET_end(&exts)) {
  366. SSLfatal(s, SSL_AD_DECODE_ERROR,
  367. SSL_F_TLS_PARSE_CTOS_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
  368. return 0;
  369. }
  370. }
  371. return 1;
  372. }
  373. #endif
  374. #ifndef OPENSSL_NO_NEXTPROTONEG
  375. int tls_parse_ctos_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  376. size_t chainidx)
  377. {
  378. /*
  379. * We shouldn't accept this extension on a
  380. * renegotiation.
  381. */
  382. if (SSL_IS_FIRST_HANDSHAKE(s))
  383. s->s3->npn_seen = 1;
  384. return 1;
  385. }
  386. #endif
  387. /*
  388. * Save the ALPN extension in a ClientHello.|pkt| holds the contents of the ALPN
  389. * extension, not including type and length. Returns: 1 on success, 0 on error.
  390. */
  391. int tls_parse_ctos_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  392. size_t chainidx)
  393. {
  394. PACKET protocol_list, save_protocol_list, protocol;
  395. if (!SSL_IS_FIRST_HANDSHAKE(s))
  396. return 1;
  397. if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)
  398. || PACKET_remaining(&protocol_list) < 2) {
  399. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_ALPN,
  400. SSL_R_BAD_EXTENSION);
  401. return 0;
  402. }
  403. save_protocol_list = protocol_list;
  404. do {
  405. /* Protocol names can't be empty. */
  406. if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)
  407. || PACKET_remaining(&protocol) == 0) {
  408. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_ALPN,
  409. SSL_R_BAD_EXTENSION);
  410. return 0;
  411. }
  412. } while (PACKET_remaining(&protocol_list) != 0);
  413. OPENSSL_free(s->s3->alpn_proposed);
  414. s->s3->alpn_proposed = NULL;
  415. s->s3->alpn_proposed_len = 0;
  416. if (!PACKET_memdup(&save_protocol_list,
  417. &s->s3->alpn_proposed, &s->s3->alpn_proposed_len)) {
  418. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_ALPN,
  419. ERR_R_INTERNAL_ERROR);
  420. return 0;
  421. }
  422. return 1;
  423. }
  424. #ifndef OPENSSL_NO_SRTP
  425. int tls_parse_ctos_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  426. size_t chainidx)
  427. {
  428. STACK_OF(SRTP_PROTECTION_PROFILE) *srvr;
  429. unsigned int ct, mki_len, id;
  430. int i, srtp_pref;
  431. PACKET subpkt;
  432. /* Ignore this if we have no SRTP profiles */
  433. if (SSL_get_srtp_profiles(s) == NULL)
  434. return 1;
  435. /* Pull off the length of the cipher suite list and check it is even */
  436. if (!PACKET_get_net_2(pkt, &ct) || (ct & 1) != 0
  437. || !PACKET_get_sub_packet(pkt, &subpkt, ct)) {
  438. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_USE_SRTP,
  439. SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  440. return 0;
  441. }
  442. srvr = SSL_get_srtp_profiles(s);
  443. s->srtp_profile = NULL;
  444. /* Search all profiles for a match initially */
  445. srtp_pref = sk_SRTP_PROTECTION_PROFILE_num(srvr);
  446. while (PACKET_remaining(&subpkt)) {
  447. if (!PACKET_get_net_2(&subpkt, &id)) {
  448. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_USE_SRTP,
  449. SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  450. return 0;
  451. }
  452. /*
  453. * Only look for match in profiles of higher preference than
  454. * current match.
  455. * If no profiles have been have been configured then this
  456. * does nothing.
  457. */
  458. for (i = 0; i < srtp_pref; i++) {
  459. SRTP_PROTECTION_PROFILE *sprof =
  460. sk_SRTP_PROTECTION_PROFILE_value(srvr, i);
  461. if (sprof->id == id) {
  462. s->srtp_profile = sprof;
  463. srtp_pref = i;
  464. break;
  465. }
  466. }
  467. }
  468. /* Now extract the MKI value as a sanity check, but discard it for now */
  469. if (!PACKET_get_1(pkt, &mki_len)) {
  470. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_USE_SRTP,
  471. SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  472. return 0;
  473. }
  474. if (!PACKET_forward(pkt, mki_len)
  475. || PACKET_remaining(pkt)) {
  476. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_USE_SRTP,
  477. SSL_R_BAD_SRTP_MKI_VALUE);
  478. return 0;
  479. }
  480. return 1;
  481. }
  482. #endif
  483. int tls_parse_ctos_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  484. size_t chainidx)
  485. {
  486. if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC))
  487. s->ext.use_etm = 1;
  488. return 1;
  489. }
  490. /*
  491. * Process a psk_kex_modes extension received in the ClientHello. |pkt| contains
  492. * the raw PACKET data for the extension. Returns 1 on success or 0 on failure.
  493. */
  494. int tls_parse_ctos_psk_kex_modes(SSL *s, PACKET *pkt, unsigned int context,
  495. X509 *x, size_t chainidx)
  496. {
  497. #ifndef OPENSSL_NO_TLS1_3
  498. PACKET psk_kex_modes;
  499. unsigned int mode;
  500. if (!PACKET_as_length_prefixed_1(pkt, &psk_kex_modes)
  501. || PACKET_remaining(&psk_kex_modes) == 0) {
  502. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_PSK_KEX_MODES,
  503. SSL_R_BAD_EXTENSION);
  504. return 0;
  505. }
  506. while (PACKET_get_1(&psk_kex_modes, &mode)) {
  507. if (mode == TLSEXT_KEX_MODE_KE_DHE)
  508. s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE_DHE;
  509. else if (mode == TLSEXT_KEX_MODE_KE
  510. && (s->options & SSL_OP_ALLOW_NO_DHE_KEX) != 0)
  511. s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE;
  512. }
  513. #endif
  514. return 1;
  515. }
  516. /*
  517. * Process a key_share extension received in the ClientHello. |pkt| contains
  518. * the raw PACKET data for the extension. Returns 1 on success or 0 on failure.
  519. */
  520. int tls_parse_ctos_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  521. size_t chainidx)
  522. {
  523. #ifndef OPENSSL_NO_TLS1_3
  524. unsigned int group_id;
  525. PACKET key_share_list, encoded_pt;
  526. const uint16_t *clntgroups, *srvrgroups;
  527. size_t clnt_num_groups, srvr_num_groups;
  528. int found = 0;
  529. if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0)
  530. return 1;
  531. /* Sanity check */
  532. if (s->s3->peer_tmp != NULL) {
  533. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_KEY_SHARE,
  534. ERR_R_INTERNAL_ERROR);
  535. return 0;
  536. }
  537. if (!PACKET_as_length_prefixed_2(pkt, &key_share_list)) {
  538. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_KEY_SHARE,
  539. SSL_R_LENGTH_MISMATCH);
  540. return 0;
  541. }
  542. /* Get our list of supported groups */
  543. tls1_get_supported_groups(s, &srvrgroups, &srvr_num_groups);
  544. /* Get the clients list of supported groups. */
  545. tls1_get_peer_groups(s, &clntgroups, &clnt_num_groups);
  546. if (clnt_num_groups == 0) {
  547. /*
  548. * This can only happen if the supported_groups extension was not sent,
  549. * because we verify that the length is non-zero when we process that
  550. * extension.
  551. */
  552. SSLfatal(s, SSL_AD_MISSING_EXTENSION, SSL_F_TLS_PARSE_CTOS_KEY_SHARE,
  553. SSL_R_MISSING_SUPPORTED_GROUPS_EXTENSION);
  554. return 0;
  555. }
  556. if (s->s3->group_id != 0 && PACKET_remaining(&key_share_list) == 0) {
  557. /*
  558. * If we set a group_id already, then we must have sent an HRR
  559. * requesting a new key_share. If we haven't got one then that is an
  560. * error
  561. */
  562. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_CTOS_KEY_SHARE,
  563. SSL_R_BAD_KEY_SHARE);
  564. return 0;
  565. }
  566. while (PACKET_remaining(&key_share_list) > 0) {
  567. if (!PACKET_get_net_2(&key_share_list, &group_id)
  568. || !PACKET_get_length_prefixed_2(&key_share_list, &encoded_pt)
  569. || PACKET_remaining(&encoded_pt) == 0) {
  570. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_KEY_SHARE,
  571. SSL_R_LENGTH_MISMATCH);
  572. return 0;
  573. }
  574. /*
  575. * If we already found a suitable key_share we loop through the
  576. * rest to verify the structure, but don't process them.
  577. */
  578. if (found)
  579. continue;
  580. /*
  581. * If we sent an HRR then the key_share sent back MUST be for the group
  582. * we requested, and must be the only key_share sent.
  583. */
  584. if (s->s3->group_id != 0
  585. && (group_id != s->s3->group_id
  586. || PACKET_remaining(&key_share_list) != 0)) {
  587. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  588. SSL_F_TLS_PARSE_CTOS_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
  589. return 0;
  590. }
  591. /* Check if this share is in supported_groups sent from client */
  592. if (!check_in_list(s, group_id, clntgroups, clnt_num_groups, 0)) {
  593. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  594. SSL_F_TLS_PARSE_CTOS_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
  595. return 0;
  596. }
  597. /* Check if this share is for a group we can use */
  598. if (!check_in_list(s, group_id, srvrgroups, srvr_num_groups, 1)) {
  599. /* Share not suitable */
  600. continue;
  601. }
  602. if ((s->s3->peer_tmp = ssl_generate_param_group(group_id)) == NULL) {
  603. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_KEY_SHARE,
  604. SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
  605. return 0;
  606. }
  607. s->s3->group_id = group_id;
  608. if (!EVP_PKEY_set1_tls_encodedpoint(s->s3->peer_tmp,
  609. PACKET_data(&encoded_pt),
  610. PACKET_remaining(&encoded_pt))) {
  611. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  612. SSL_F_TLS_PARSE_CTOS_KEY_SHARE, SSL_R_BAD_ECPOINT);
  613. return 0;
  614. }
  615. found = 1;
  616. }
  617. #endif
  618. return 1;
  619. }
  620. int tls_parse_ctos_cookie(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  621. size_t chainidx)
  622. {
  623. unsigned int format, version, key_share, group_id;
  624. EVP_MD_CTX *hctx;
  625. EVP_PKEY *pkey;
  626. PACKET cookie, raw, chhash, appcookie;
  627. WPACKET hrrpkt;
  628. const unsigned char *data, *mdin, *ciphdata;
  629. unsigned char hmac[SHA256_DIGEST_LENGTH];
  630. unsigned char hrr[MAX_HRR_SIZE];
  631. size_t rawlen, hmaclen, hrrlen, ciphlen;
  632. unsigned long tm, now;
  633. /* Ignore any cookie if we're not set up to verify it */
  634. if (s->ctx->verify_stateless_cookie_cb == NULL
  635. || (s->s3->flags & TLS1_FLAGS_STATELESS) == 0)
  636. return 1;
  637. if (!PACKET_as_length_prefixed_2(pkt, &cookie)) {
  638. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  639. SSL_R_LENGTH_MISMATCH);
  640. return 0;
  641. }
  642. raw = cookie;
  643. data = PACKET_data(&raw);
  644. rawlen = PACKET_remaining(&raw);
  645. if (rawlen < SHA256_DIGEST_LENGTH
  646. || !PACKET_forward(&raw, rawlen - SHA256_DIGEST_LENGTH)) {
  647. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  648. SSL_R_LENGTH_MISMATCH);
  649. return 0;
  650. }
  651. mdin = PACKET_data(&raw);
  652. /* Verify the HMAC of the cookie */
  653. hctx = EVP_MD_CTX_create();
  654. pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
  655. s->session_ctx->ext.cookie_hmac_key,
  656. sizeof(s->session_ctx->ext
  657. .cookie_hmac_key));
  658. if (hctx == NULL || pkey == NULL) {
  659. EVP_MD_CTX_free(hctx);
  660. EVP_PKEY_free(pkey);
  661. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  662. ERR_R_MALLOC_FAILURE);
  663. return 0;
  664. }
  665. hmaclen = SHA256_DIGEST_LENGTH;
  666. if (EVP_DigestSignInit(hctx, NULL, EVP_sha256(), NULL, pkey) <= 0
  667. || EVP_DigestSign(hctx, hmac, &hmaclen, data,
  668. rawlen - SHA256_DIGEST_LENGTH) <= 0
  669. || hmaclen != SHA256_DIGEST_LENGTH) {
  670. EVP_MD_CTX_free(hctx);
  671. EVP_PKEY_free(pkey);
  672. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  673. ERR_R_INTERNAL_ERROR);
  674. return 0;
  675. }
  676. EVP_MD_CTX_free(hctx);
  677. EVP_PKEY_free(pkey);
  678. if (CRYPTO_memcmp(hmac, mdin, SHA256_DIGEST_LENGTH) != 0) {
  679. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_CTOS_COOKIE,
  680. SSL_R_COOKIE_MISMATCH);
  681. return 0;
  682. }
  683. if (!PACKET_get_net_2(&cookie, &format)) {
  684. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  685. SSL_R_LENGTH_MISMATCH);
  686. return 0;
  687. }
  688. /* Check the cookie format is something we recognise. Ignore it if not */
  689. if (format != COOKIE_STATE_FORMAT_VERSION)
  690. return 1;
  691. /*
  692. * The rest of these checks really shouldn't fail since we have verified the
  693. * HMAC above.
  694. */
  695. /* Check the version number is sane */
  696. if (!PACKET_get_net_2(&cookie, &version)) {
  697. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  698. SSL_R_LENGTH_MISMATCH);
  699. return 0;
  700. }
  701. if (version != TLS1_3_VERSION) {
  702. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_CTOS_COOKIE,
  703. SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
  704. return 0;
  705. }
  706. if (!PACKET_get_net_2(&cookie, &group_id)) {
  707. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  708. SSL_R_LENGTH_MISMATCH);
  709. return 0;
  710. }
  711. ciphdata = PACKET_data(&cookie);
  712. if (!PACKET_forward(&cookie, 2)) {
  713. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  714. SSL_R_LENGTH_MISMATCH);
  715. return 0;
  716. }
  717. if (group_id != s->s3->group_id
  718. || s->s3->tmp.new_cipher
  719. != ssl_get_cipher_by_char(s, ciphdata, 0)) {
  720. /*
  721. * We chose a different cipher or group id this time around to what is
  722. * in the cookie. Something must have changed.
  723. */
  724. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_CTOS_COOKIE,
  725. SSL_R_BAD_CIPHER);
  726. return 0;
  727. }
  728. if (!PACKET_get_1(&cookie, &key_share)
  729. || !PACKET_get_net_4(&cookie, &tm)
  730. || !PACKET_get_length_prefixed_2(&cookie, &chhash)
  731. || !PACKET_get_length_prefixed_1(&cookie, &appcookie)
  732. || PACKET_remaining(&cookie) != SHA256_DIGEST_LENGTH) {
  733. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  734. SSL_R_LENGTH_MISMATCH);
  735. return 0;
  736. }
  737. /* We tolerate a cookie age of up to 10 minutes (= 60 * 10 seconds) */
  738. now = (unsigned long)time(NULL);
  739. if (tm > now || (now - tm) > 600) {
  740. /* Cookie is stale. Ignore it */
  741. return 1;
  742. }
  743. /* Verify the app cookie */
  744. if (s->ctx->verify_stateless_cookie_cb(s, PACKET_data(&appcookie),
  745. PACKET_remaining(&appcookie)) == 0) {
  746. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_CTOS_COOKIE,
  747. SSL_R_COOKIE_MISMATCH);
  748. return 0;
  749. }
  750. /*
  751. * Reconstruct the HRR that we would have sent in response to the original
  752. * ClientHello so we can add it to the transcript hash.
  753. * Note: This won't work with custom HRR extensions
  754. */
  755. if (!WPACKET_init_static_len(&hrrpkt, hrr, sizeof(hrr), 0)) {
  756. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  757. ERR_R_INTERNAL_ERROR);
  758. return 0;
  759. }
  760. if (!WPACKET_put_bytes_u8(&hrrpkt, SSL3_MT_SERVER_HELLO)
  761. || !WPACKET_start_sub_packet_u24(&hrrpkt)
  762. || !WPACKET_put_bytes_u16(&hrrpkt, TLS1_2_VERSION)
  763. || !WPACKET_memcpy(&hrrpkt, hrrrandom, SSL3_RANDOM_SIZE)
  764. || !WPACKET_sub_memcpy_u8(&hrrpkt, s->tmp_session_id,
  765. s->tmp_session_id_len)
  766. || !s->method->put_cipher_by_char(s->s3->tmp.new_cipher, &hrrpkt,
  767. &ciphlen)
  768. || !WPACKET_put_bytes_u8(&hrrpkt, 0)
  769. || !WPACKET_start_sub_packet_u16(&hrrpkt)) {
  770. WPACKET_cleanup(&hrrpkt);
  771. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  772. ERR_R_INTERNAL_ERROR);
  773. return 0;
  774. }
  775. if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_supported_versions)
  776. || !WPACKET_start_sub_packet_u16(&hrrpkt)
  777. /* TODO(TLS1.3): Fix this before release */
  778. || !WPACKET_put_bytes_u16(&hrrpkt, TLS1_3_VERSION_DRAFT)
  779. || !WPACKET_close(&hrrpkt)) {
  780. WPACKET_cleanup(&hrrpkt);
  781. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  782. ERR_R_INTERNAL_ERROR);
  783. return 0;
  784. }
  785. if (key_share) {
  786. if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_key_share)
  787. || !WPACKET_start_sub_packet_u16(&hrrpkt)
  788. || !WPACKET_put_bytes_u16(&hrrpkt, s->s3->group_id)
  789. || !WPACKET_close(&hrrpkt)) {
  790. WPACKET_cleanup(&hrrpkt);
  791. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  792. ERR_R_INTERNAL_ERROR);
  793. return 0;
  794. }
  795. }
  796. if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_cookie)
  797. || !WPACKET_start_sub_packet_u16(&hrrpkt)
  798. || !WPACKET_sub_memcpy_u16(&hrrpkt, data, rawlen)
  799. || !WPACKET_close(&hrrpkt) /* cookie extension */
  800. || !WPACKET_close(&hrrpkt) /* extension block */
  801. || !WPACKET_close(&hrrpkt) /* message */
  802. || !WPACKET_get_total_written(&hrrpkt, &hrrlen)
  803. || !WPACKET_finish(&hrrpkt)) {
  804. WPACKET_cleanup(&hrrpkt);
  805. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_COOKIE,
  806. ERR_R_INTERNAL_ERROR);
  807. return 0;
  808. }
  809. /* Reconstruct the transcript hash */
  810. if (!create_synthetic_message_hash(s, PACKET_data(&chhash),
  811. PACKET_remaining(&chhash), hrr,
  812. hrrlen)) {
  813. /* SSLfatal() already called */
  814. return 0;
  815. }
  816. /* Act as if this ClientHello came after a HelloRetryRequest */
  817. s->hello_retry_request = 1;
  818. s->ext.cookieok = 1;
  819. return 1;
  820. }
  821. #ifndef OPENSSL_NO_EC
  822. int tls_parse_ctos_supported_groups(SSL *s, PACKET *pkt, unsigned int context,
  823. X509 *x, size_t chainidx)
  824. {
  825. PACKET supported_groups_list;
  826. /* Each group is 2 bytes and we must have at least 1. */
  827. if (!PACKET_as_length_prefixed_2(pkt, &supported_groups_list)
  828. || PACKET_remaining(&supported_groups_list) == 0
  829. || (PACKET_remaining(&supported_groups_list) % 2) != 0) {
  830. SSLfatal(s, SSL_AD_DECODE_ERROR,
  831. SSL_F_TLS_PARSE_CTOS_SUPPORTED_GROUPS, SSL_R_BAD_EXTENSION);
  832. return 0;
  833. }
  834. if (!s->hit || SSL_IS_TLS13(s)) {
  835. OPENSSL_free(s->session->ext.supportedgroups);
  836. s->session->ext.supportedgroups = NULL;
  837. s->session->ext.supportedgroups_len = 0;
  838. if (!tls1_save_u16(&supported_groups_list,
  839. &s->session->ext.supportedgroups,
  840. &s->session->ext.supportedgroups_len)) {
  841. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  842. SSL_F_TLS_PARSE_CTOS_SUPPORTED_GROUPS,
  843. ERR_R_INTERNAL_ERROR);
  844. return 0;
  845. }
  846. }
  847. return 1;
  848. }
  849. #endif
  850. int tls_parse_ctos_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  851. size_t chainidx)
  852. {
  853. /* The extension must always be empty */
  854. if (PACKET_remaining(pkt) != 0) {
  855. SSLfatal(s, SSL_AD_DECODE_ERROR,
  856. SSL_F_TLS_PARSE_CTOS_EMS, SSL_R_BAD_EXTENSION);
  857. return 0;
  858. }
  859. s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
  860. return 1;
  861. }
  862. int tls_parse_ctos_early_data(SSL *s, PACKET *pkt, unsigned int context,
  863. X509 *x, size_t chainidx)
  864. {
  865. if (PACKET_remaining(pkt) != 0) {
  866. SSLfatal(s, SSL_AD_DECODE_ERROR,
  867. SSL_F_TLS_PARSE_CTOS_EARLY_DATA, SSL_R_BAD_EXTENSION);
  868. return 0;
  869. }
  870. if (s->hello_retry_request != SSL_HRR_NONE) {
  871. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  872. SSL_F_TLS_PARSE_CTOS_EARLY_DATA, SSL_R_BAD_EXTENSION);
  873. return 0;
  874. }
  875. return 1;
  876. }
  877. int tls_parse_ctos_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  878. size_t chainidx)
  879. {
  880. PACKET identities, binders, binder;
  881. size_t binderoffset, hashsize;
  882. SSL_SESSION *sess = NULL;
  883. unsigned int id, i, ext = 0;
  884. const EVP_MD *md = NULL;
  885. /*
  886. * If we have no PSK kex mode that we recognise then we can't resume so
  887. * ignore this extension
  888. */
  889. if ((s->ext.psk_kex_mode
  890. & (TLSEXT_KEX_MODE_FLAG_KE | TLSEXT_KEX_MODE_FLAG_KE_DHE)) == 0)
  891. return 1;
  892. if (!PACKET_get_length_prefixed_2(pkt, &identities)) {
  893. SSLfatal(s, SSL_AD_DECODE_ERROR,
  894. SSL_F_TLS_PARSE_CTOS_PSK, SSL_R_BAD_EXTENSION);
  895. return 0;
  896. }
  897. for (id = 0; PACKET_remaining(&identities) != 0; id++) {
  898. PACKET identity;
  899. unsigned long ticket_agel;
  900. size_t idlen;
  901. if (!PACKET_get_length_prefixed_2(&identities, &identity)
  902. || !PACKET_get_net_4(&identities, &ticket_agel)) {
  903. SSLfatal(s, SSL_AD_DECODE_ERROR,
  904. SSL_F_TLS_PARSE_CTOS_PSK, SSL_R_BAD_EXTENSION);
  905. return 0;
  906. }
  907. idlen = PACKET_remaining(&identity);
  908. if (s->psk_find_session_cb != NULL
  909. && !s->psk_find_session_cb(s, PACKET_data(&identity), idlen,
  910. &sess)) {
  911. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  912. SSL_F_TLS_PARSE_CTOS_PSK, SSL_R_BAD_EXTENSION);
  913. return 0;
  914. }
  915. #ifndef OPENSSL_NO_PSK
  916. if(sess == NULL
  917. && s->psk_server_callback != NULL
  918. && idlen <= PSK_MAX_IDENTITY_LEN) {
  919. char *pskid = NULL;
  920. unsigned char pskdata[PSK_MAX_PSK_LEN];
  921. unsigned int pskdatalen;
  922. if (!PACKET_strndup(&identity, &pskid)) {
  923. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_PSK,
  924. ERR_R_INTERNAL_ERROR);
  925. return 0;
  926. }
  927. pskdatalen = s->psk_server_callback(s, pskid, pskdata,
  928. sizeof(pskdata));
  929. OPENSSL_free(pskid);
  930. if (pskdatalen > PSK_MAX_PSK_LEN) {
  931. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_PSK,
  932. ERR_R_INTERNAL_ERROR);
  933. return 0;
  934. } else if (pskdatalen > 0) {
  935. const SSL_CIPHER *cipher;
  936. const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 };
  937. /*
  938. * We found a PSK using an old style callback. We don't know
  939. * the digest so we default to SHA256 as per the TLSv1.3 spec
  940. */
  941. cipher = SSL_CIPHER_find(s, tls13_aes128gcmsha256_id);
  942. if (cipher == NULL) {
  943. OPENSSL_cleanse(pskdata, pskdatalen);
  944. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_PSK,
  945. ERR_R_INTERNAL_ERROR);
  946. return 0;
  947. }
  948. sess = SSL_SESSION_new();
  949. if (sess == NULL
  950. || !SSL_SESSION_set1_master_key(sess, pskdata,
  951. pskdatalen)
  952. || !SSL_SESSION_set_cipher(sess, cipher)
  953. || !SSL_SESSION_set_protocol_version(sess,
  954. TLS1_3_VERSION)) {
  955. OPENSSL_cleanse(pskdata, pskdatalen);
  956. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_CTOS_PSK,
  957. ERR_R_INTERNAL_ERROR);
  958. goto err;
  959. }
  960. OPENSSL_cleanse(pskdata, pskdatalen);
  961. }
  962. }
  963. #endif /* OPENSSL_NO_PSK */
  964. if (sess != NULL) {
  965. /* We found a PSK */
  966. SSL_SESSION *sesstmp = ssl_session_dup(sess, 0);
  967. if (sesstmp == NULL) {
  968. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  969. SSL_F_TLS_PARSE_CTOS_PSK, ERR_R_INTERNAL_ERROR);
  970. return 0;
  971. }
  972. SSL_SESSION_free(sess);
  973. sess = sesstmp;
  974. /*
  975. * We've just been told to use this session for this context so
  976. * make sure the sid_ctx matches up.
  977. */
  978. memcpy(sess->sid_ctx, s->sid_ctx, s->sid_ctx_length);
  979. sess->sid_ctx_length = s->sid_ctx_length;
  980. ext = 1;
  981. if (id == 0)
  982. s->ext.early_data_ok = 1;
  983. } else {
  984. uint32_t ticket_age = 0, now, agesec, agems;
  985. int ret = tls_decrypt_ticket(s, PACKET_data(&identity),
  986. PACKET_remaining(&identity), NULL, 0,
  987. &sess);
  988. if (ret == SSL_TICKET_FATAL_ERR_MALLOC
  989. || ret == SSL_TICKET_FATAL_ERR_OTHER) {
  990. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  991. SSL_F_TLS_PARSE_CTOS_PSK, ERR_R_INTERNAL_ERROR);
  992. return 0;
  993. }
  994. if (ret == SSL_TICKET_NO_DECRYPT)
  995. continue;
  996. /* Check for replay */
  997. if (s->max_early_data > 0
  998. && !SSL_CTX_remove_session(s->session_ctx, sess)) {
  999. SSL_SESSION_free(sess);
  1000. sess = NULL;
  1001. continue;
  1002. }
  1003. ticket_age = (uint32_t)ticket_agel;
  1004. now = (uint32_t)time(NULL);
  1005. agesec = now - (uint32_t)sess->time;
  1006. agems = agesec * (uint32_t)1000;
  1007. ticket_age -= sess->ext.tick_age_add;
  1008. /*
  1009. * For simplicity we do our age calculations in seconds. If the
  1010. * client does it in ms then it could appear that their ticket age
  1011. * is longer than ours (our ticket age calculation should always be
  1012. * slightly longer than the client's due to the network latency).
  1013. * Therefore we add 1000ms to our age calculation to adjust for
  1014. * rounding errors.
  1015. */
  1016. if (id == 0
  1017. && sess->timeout >= (long)agesec
  1018. && agems / (uint32_t)1000 == agesec
  1019. && ticket_age <= agems + 1000
  1020. && ticket_age + TICKET_AGE_ALLOWANCE >= agems + 1000) {
  1021. /*
  1022. * Ticket age is within tolerance and not expired. We allow it
  1023. * for early data
  1024. */
  1025. s->ext.early_data_ok = 1;
  1026. }
  1027. }
  1028. md = ssl_md(sess->cipher->algorithm2);
  1029. if (md != ssl_md(s->s3->tmp.new_cipher->algorithm2)) {
  1030. /* The ciphersuite is not compatible with this session. */
  1031. SSL_SESSION_free(sess);
  1032. sess = NULL;
  1033. s->ext.early_data_ok = 0;
  1034. continue;
  1035. }
  1036. break;
  1037. }
  1038. if (sess == NULL)
  1039. return 1;
  1040. binderoffset = PACKET_data(pkt) - (const unsigned char *)s->init_buf->data;
  1041. hashsize = EVP_MD_size(md);
  1042. if (!PACKET_get_length_prefixed_2(pkt, &binders)) {
  1043. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_PSK,
  1044. SSL_R_BAD_EXTENSION);
  1045. goto err;
  1046. }
  1047. for (i = 0; i <= id; i++) {
  1048. if (!PACKET_get_length_prefixed_1(&binders, &binder)) {
  1049. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_PSK,
  1050. SSL_R_BAD_EXTENSION);
  1051. goto err;
  1052. }
  1053. }
  1054. if (PACKET_remaining(&binder) != hashsize) {
  1055. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_PSK,
  1056. SSL_R_BAD_EXTENSION);
  1057. goto err;
  1058. }
  1059. if (tls_psk_do_binder(s, md, (const unsigned char *)s->init_buf->data,
  1060. binderoffset, PACKET_data(&binder), NULL, sess, 0,
  1061. ext) != 1) {
  1062. /* SSLfatal() already called */
  1063. goto err;
  1064. }
  1065. sess->ext.tick_identity = id;
  1066. SSL_SESSION_free(s->session);
  1067. s->session = sess;
  1068. return 1;
  1069. err:
  1070. SSL_SESSION_free(sess);
  1071. return 0;
  1072. }
  1073. int tls_parse_ctos_post_handshake_auth(SSL *s, PACKET *pkt, unsigned int context,
  1074. X509 *x, size_t chainidx)
  1075. {
  1076. if (PACKET_remaining(pkt) != 0) {
  1077. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_CTOS_POST_HANDSHAKE_AUTH,
  1078. SSL_R_POST_HANDSHAKE_AUTH_ENCODING_ERR);
  1079. return 0;
  1080. }
  1081. s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;
  1082. return 1;
  1083. }
  1084. /*
  1085. * Add the server's renegotiation binding
  1086. */
  1087. EXT_RETURN tls_construct_stoc_renegotiate(SSL *s, WPACKET *pkt,
  1088. unsigned int context, X509 *x,
  1089. size_t chainidx)
  1090. {
  1091. if (!s->s3->send_connection_binding)
  1092. return EXT_RETURN_NOT_SENT;
  1093. /* Still add this even if SSL_OP_NO_RENEGOTIATION is set */
  1094. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
  1095. || !WPACKET_start_sub_packet_u16(pkt)
  1096. || !WPACKET_start_sub_packet_u8(pkt)
  1097. || !WPACKET_memcpy(pkt, s->s3->previous_client_finished,
  1098. s->s3->previous_client_finished_len)
  1099. || !WPACKET_memcpy(pkt, s->s3->previous_server_finished,
  1100. s->s3->previous_server_finished_len)
  1101. || !WPACKET_close(pkt)
  1102. || !WPACKET_close(pkt)) {
  1103. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_RENEGOTIATE,
  1104. ERR_R_INTERNAL_ERROR);
  1105. return EXT_RETURN_FAIL;
  1106. }
  1107. return EXT_RETURN_SENT;
  1108. }
  1109. EXT_RETURN tls_construct_stoc_server_name(SSL *s, WPACKET *pkt,
  1110. unsigned int context, X509 *x,
  1111. size_t chainidx)
  1112. {
  1113. if (s->hit || s->servername_done != 1
  1114. || s->session->ext.hostname == NULL)
  1115. return EXT_RETURN_NOT_SENT;
  1116. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
  1117. || !WPACKET_put_bytes_u16(pkt, 0)) {
  1118. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_SERVER_NAME,
  1119. ERR_R_INTERNAL_ERROR);
  1120. return EXT_RETURN_FAIL;
  1121. }
  1122. return EXT_RETURN_SENT;
  1123. }
  1124. /* Add/include the server's max fragment len extension into ServerHello */
  1125. EXT_RETURN tls_construct_stoc_maxfragmentlen(SSL *s, WPACKET *pkt,
  1126. unsigned int context, X509 *x,
  1127. size_t chainidx)
  1128. {
  1129. if (!USE_MAX_FRAGMENT_LENGTH_EXT(s->session))
  1130. return EXT_RETURN_NOT_SENT;
  1131. /*-
  1132. * 4 bytes for this extension type and extension length
  1133. * 1 byte for the Max Fragment Length code value.
  1134. */
  1135. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length)
  1136. || !WPACKET_start_sub_packet_u16(pkt)
  1137. || !WPACKET_put_bytes_u8(pkt, s->session->ext.max_fragment_len_mode)
  1138. || !WPACKET_close(pkt)) {
  1139. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1140. SSL_F_TLS_CONSTRUCT_STOC_MAXFRAGMENTLEN, ERR_R_INTERNAL_ERROR);
  1141. return EXT_RETURN_FAIL;
  1142. }
  1143. return EXT_RETURN_SENT;
  1144. }
  1145. #ifndef OPENSSL_NO_EC
  1146. EXT_RETURN tls_construct_stoc_ec_pt_formats(SSL *s, WPACKET *pkt,
  1147. unsigned int context, X509 *x,
  1148. size_t chainidx)
  1149. {
  1150. unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1151. unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1152. int using_ecc = ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))
  1153. && (s->session->ext.ecpointformats != NULL);
  1154. const unsigned char *plist;
  1155. size_t plistlen;
  1156. if (!using_ecc)
  1157. return EXT_RETURN_NOT_SENT;
  1158. tls1_get_formatlist(s, &plist, &plistlen);
  1159. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
  1160. || !WPACKET_start_sub_packet_u16(pkt)
  1161. || !WPACKET_sub_memcpy_u8(pkt, plist, plistlen)
  1162. || !WPACKET_close(pkt)) {
  1163. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1164. SSL_F_TLS_CONSTRUCT_STOC_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
  1165. return EXT_RETURN_FAIL;
  1166. }
  1167. return EXT_RETURN_SENT;
  1168. }
  1169. #endif
  1170. #ifndef OPENSSL_NO_EC
  1171. EXT_RETURN tls_construct_stoc_supported_groups(SSL *s, WPACKET *pkt,
  1172. unsigned int context, X509 *x,
  1173. size_t chainidx)
  1174. {
  1175. const uint16_t *groups;
  1176. size_t numgroups, i, first = 1;
  1177. /* s->s3->group_id is non zero if we accepted a key_share */
  1178. if (s->s3->group_id == 0)
  1179. return EXT_RETURN_NOT_SENT;
  1180. /* Get our list of supported groups */
  1181. tls1_get_supported_groups(s, &groups, &numgroups);
  1182. if (numgroups == 0) {
  1183. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1184. SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_GROUPS, ERR_R_INTERNAL_ERROR);
  1185. return EXT_RETURN_FAIL;
  1186. }
  1187. /* Copy group ID if supported */
  1188. for (i = 0; i < numgroups; i++) {
  1189. uint16_t group = groups[i];
  1190. if (tls_curve_allowed(s, group, SSL_SECOP_CURVE_SUPPORTED)) {
  1191. if (first) {
  1192. /*
  1193. * Check if the client is already using our preferred group. If
  1194. * so we don't need to add this extension
  1195. */
  1196. if (s->s3->group_id == group)
  1197. return EXT_RETURN_NOT_SENT;
  1198. /* Add extension header */
  1199. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)
  1200. /* Sub-packet for supported_groups extension */
  1201. || !WPACKET_start_sub_packet_u16(pkt)
  1202. || !WPACKET_start_sub_packet_u16(pkt)) {
  1203. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1204. SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_GROUPS,
  1205. ERR_R_INTERNAL_ERROR);
  1206. return EXT_RETURN_FAIL;
  1207. }
  1208. first = 0;
  1209. }
  1210. if (!WPACKET_put_bytes_u16(pkt, group)) {
  1211. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1212. SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_GROUPS,
  1213. ERR_R_INTERNAL_ERROR);
  1214. return EXT_RETURN_FAIL;
  1215. }
  1216. }
  1217. }
  1218. if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
  1219. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1220. SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_GROUPS,
  1221. ERR_R_INTERNAL_ERROR);
  1222. return EXT_RETURN_FAIL;
  1223. }
  1224. return EXT_RETURN_SENT;
  1225. }
  1226. #endif
  1227. EXT_RETURN tls_construct_stoc_session_ticket(SSL *s, WPACKET *pkt,
  1228. unsigned int context, X509 *x,
  1229. size_t chainidx)
  1230. {
  1231. if (!s->ext.ticket_expected || !tls_use_ticket(s)) {
  1232. s->ext.ticket_expected = 0;
  1233. return EXT_RETURN_NOT_SENT;
  1234. }
  1235. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
  1236. || !WPACKET_put_bytes_u16(pkt, 0)) {
  1237. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1238. SSL_F_TLS_CONSTRUCT_STOC_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
  1239. return EXT_RETURN_FAIL;
  1240. }
  1241. return EXT_RETURN_SENT;
  1242. }
  1243. #ifndef OPENSSL_NO_OCSP
  1244. EXT_RETURN tls_construct_stoc_status_request(SSL *s, WPACKET *pkt,
  1245. unsigned int context, X509 *x,
  1246. size_t chainidx)
  1247. {
  1248. if (!s->ext.status_expected)
  1249. return EXT_RETURN_NOT_SENT;
  1250. if (SSL_IS_TLS13(s) && chainidx != 0)
  1251. return EXT_RETURN_NOT_SENT;
  1252. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
  1253. || !WPACKET_start_sub_packet_u16(pkt)) {
  1254. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1255. SSL_F_TLS_CONSTRUCT_STOC_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
  1256. return EXT_RETURN_FAIL;
  1257. }
  1258. /*
  1259. * In TLSv1.3 we include the certificate status itself. In <= TLSv1.2 we
  1260. * send back an empty extension, with the certificate status appearing as a
  1261. * separate message
  1262. */
  1263. if (SSL_IS_TLS13(s) && !tls_construct_cert_status_body(s, pkt)) {
  1264. /* SSLfatal() already called */
  1265. return EXT_RETURN_FAIL;
  1266. }
  1267. if (!WPACKET_close(pkt)) {
  1268. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1269. SSL_F_TLS_CONSTRUCT_STOC_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
  1270. return EXT_RETURN_FAIL;
  1271. }
  1272. return EXT_RETURN_SENT;
  1273. }
  1274. #endif
  1275. #ifndef OPENSSL_NO_NEXTPROTONEG
  1276. EXT_RETURN tls_construct_stoc_next_proto_neg(SSL *s, WPACKET *pkt,
  1277. unsigned int context, X509 *x,
  1278. size_t chainidx)
  1279. {
  1280. const unsigned char *npa;
  1281. unsigned int npalen;
  1282. int ret;
  1283. int npn_seen = s->s3->npn_seen;
  1284. s->s3->npn_seen = 0;
  1285. if (!npn_seen || s->ctx->ext.npn_advertised_cb == NULL)
  1286. return EXT_RETURN_NOT_SENT;
  1287. ret = s->ctx->ext.npn_advertised_cb(s, &npa, &npalen,
  1288. s->ctx->ext.npn_advertised_cb_arg);
  1289. if (ret == SSL_TLSEXT_ERR_OK) {
  1290. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
  1291. || !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) {
  1292. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1293. SSL_F_TLS_CONSTRUCT_STOC_NEXT_PROTO_NEG,
  1294. ERR_R_INTERNAL_ERROR);
  1295. return EXT_RETURN_FAIL;
  1296. }
  1297. s->s3->npn_seen = 1;
  1298. }
  1299. return EXT_RETURN_SENT;
  1300. }
  1301. #endif
  1302. EXT_RETURN tls_construct_stoc_alpn(SSL *s, WPACKET *pkt, unsigned int context,
  1303. X509 *x, size_t chainidx)
  1304. {
  1305. if (s->s3->alpn_selected == NULL)
  1306. return EXT_RETURN_NOT_SENT;
  1307. if (!WPACKET_put_bytes_u16(pkt,
  1308. TLSEXT_TYPE_application_layer_protocol_negotiation)
  1309. || !WPACKET_start_sub_packet_u16(pkt)
  1310. || !WPACKET_start_sub_packet_u16(pkt)
  1311. || !WPACKET_sub_memcpy_u8(pkt, s->s3->alpn_selected,
  1312. s->s3->alpn_selected_len)
  1313. || !WPACKET_close(pkt)
  1314. || !WPACKET_close(pkt)) {
  1315. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1316. SSL_F_TLS_CONSTRUCT_STOC_ALPN, ERR_R_INTERNAL_ERROR);
  1317. return EXT_RETURN_FAIL;
  1318. }
  1319. return EXT_RETURN_SENT;
  1320. }
  1321. #ifndef OPENSSL_NO_SRTP
  1322. EXT_RETURN tls_construct_stoc_use_srtp(SSL *s, WPACKET *pkt,
  1323. unsigned int context, X509 *x,
  1324. size_t chainidx)
  1325. {
  1326. if (s->srtp_profile == NULL)
  1327. return EXT_RETURN_NOT_SENT;
  1328. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
  1329. || !WPACKET_start_sub_packet_u16(pkt)
  1330. || !WPACKET_put_bytes_u16(pkt, 2)
  1331. || !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id)
  1332. || !WPACKET_put_bytes_u8(pkt, 0)
  1333. || !WPACKET_close(pkt)) {
  1334. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_USE_SRTP,
  1335. ERR_R_INTERNAL_ERROR);
  1336. return EXT_RETURN_FAIL;
  1337. }
  1338. return EXT_RETURN_SENT;
  1339. }
  1340. #endif
  1341. EXT_RETURN tls_construct_stoc_etm(SSL *s, WPACKET *pkt, unsigned int context,
  1342. X509 *x, size_t chainidx)
  1343. {
  1344. if (!s->ext.use_etm)
  1345. return EXT_RETURN_NOT_SENT;
  1346. /*
  1347. * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
  1348. * for other cases too.
  1349. */
  1350. if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD
  1351. || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4
  1352. || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
  1353. || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12) {
  1354. s->ext.use_etm = 0;
  1355. return EXT_RETURN_NOT_SENT;
  1356. }
  1357. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
  1358. || !WPACKET_put_bytes_u16(pkt, 0)) {
  1359. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_ETM,
  1360. ERR_R_INTERNAL_ERROR);
  1361. return EXT_RETURN_FAIL;
  1362. }
  1363. return EXT_RETURN_SENT;
  1364. }
  1365. EXT_RETURN tls_construct_stoc_ems(SSL *s, WPACKET *pkt, unsigned int context,
  1366. X509 *x, size_t chainidx)
  1367. {
  1368. if ((s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) == 0)
  1369. return EXT_RETURN_NOT_SENT;
  1370. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
  1371. || !WPACKET_put_bytes_u16(pkt, 0)) {
  1372. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_EMS,
  1373. ERR_R_INTERNAL_ERROR);
  1374. return EXT_RETURN_FAIL;
  1375. }
  1376. return EXT_RETURN_SENT;
  1377. }
  1378. EXT_RETURN tls_construct_stoc_supported_versions(SSL *s, WPACKET *pkt,
  1379. unsigned int context, X509 *x,
  1380. size_t chainidx)
  1381. {
  1382. if (!ossl_assert(SSL_IS_TLS13(s))) {
  1383. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1384. SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_VERSIONS,
  1385. ERR_R_INTERNAL_ERROR);
  1386. return EXT_RETURN_FAIL;
  1387. }
  1388. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
  1389. || !WPACKET_start_sub_packet_u16(pkt)
  1390. /* TODO(TLS1.3): Update to remove the TLSv1.3 draft indicator */
  1391. || !WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION_DRAFT)
  1392. || !WPACKET_close(pkt)) {
  1393. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1394. SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_VERSIONS,
  1395. ERR_R_INTERNAL_ERROR);
  1396. return EXT_RETURN_FAIL;
  1397. }
  1398. return EXT_RETURN_SENT;
  1399. }
  1400. EXT_RETURN tls_construct_stoc_key_share(SSL *s, WPACKET *pkt,
  1401. unsigned int context, X509 *x,
  1402. size_t chainidx)
  1403. {
  1404. #ifndef OPENSSL_NO_TLS1_3
  1405. unsigned char *encodedPoint;
  1406. size_t encoded_pt_len = 0;
  1407. EVP_PKEY *ckey = s->s3->peer_tmp, *skey = NULL;
  1408. if (s->hello_retry_request == SSL_HRR_PENDING) {
  1409. if (ckey != NULL) {
  1410. /* Original key_share was acceptable so don't ask for another one */
  1411. return EXT_RETURN_NOT_SENT;
  1412. }
  1413. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
  1414. || !WPACKET_start_sub_packet_u16(pkt)
  1415. || !WPACKET_put_bytes_u16(pkt, s->s3->group_id)
  1416. || !WPACKET_close(pkt)) {
  1417. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1418. SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE,
  1419. ERR_R_INTERNAL_ERROR);
  1420. return EXT_RETURN_FAIL;
  1421. }
  1422. return EXT_RETURN_SENT;
  1423. }
  1424. if (ckey == NULL) {
  1425. /* No key_share received from client - must be resuming */
  1426. if (!s->hit || !tls13_generate_handshake_secret(s, NULL, 0)) {
  1427. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1428. SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  1429. return EXT_RETURN_FAIL;
  1430. }
  1431. return EXT_RETURN_NOT_SENT;
  1432. }
  1433. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
  1434. || !WPACKET_start_sub_packet_u16(pkt)
  1435. || !WPACKET_put_bytes_u16(pkt, s->s3->group_id)) {
  1436. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1437. SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  1438. return EXT_RETURN_FAIL;
  1439. }
  1440. skey = ssl_generate_pkey(ckey);
  1441. if (skey == NULL) {
  1442. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE,
  1443. ERR_R_MALLOC_FAILURE);
  1444. return EXT_RETURN_FAIL;
  1445. }
  1446. /* Generate encoding of server key */
  1447. encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(skey, &encodedPoint);
  1448. if (encoded_pt_len == 0) {
  1449. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE,
  1450. ERR_R_EC_LIB);
  1451. EVP_PKEY_free(skey);
  1452. return EXT_RETURN_FAIL;
  1453. }
  1454. if (!WPACKET_sub_memcpy_u16(pkt, encodedPoint, encoded_pt_len)
  1455. || !WPACKET_close(pkt)) {
  1456. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE,
  1457. ERR_R_INTERNAL_ERROR);
  1458. EVP_PKEY_free(skey);
  1459. OPENSSL_free(encodedPoint);
  1460. return EXT_RETURN_FAIL;
  1461. }
  1462. OPENSSL_free(encodedPoint);
  1463. /* This causes the crypto state to be updated based on the derived keys */
  1464. s->s3->tmp.pkey = skey;
  1465. if (ssl_derive(s, skey, ckey, 1) == 0) {
  1466. /* SSLfatal() already called */
  1467. return EXT_RETURN_FAIL;
  1468. }
  1469. #endif
  1470. return EXT_RETURN_SENT;
  1471. }
  1472. EXT_RETURN tls_construct_stoc_cookie(SSL *s, WPACKET *pkt, unsigned int context,
  1473. X509 *x, size_t chainidx)
  1474. {
  1475. unsigned char *hashval1, *hashval2, *appcookie1, *appcookie2, *cookie;
  1476. unsigned char *hmac, *hmac2;
  1477. size_t startlen, ciphlen, totcookielen, hashlen, hmaclen, appcookielen;
  1478. EVP_MD_CTX *hctx;
  1479. EVP_PKEY *pkey;
  1480. int ret = EXT_RETURN_FAIL;
  1481. if ((s->s3->flags & TLS1_FLAGS_STATELESS) == 0)
  1482. return EXT_RETURN_NOT_SENT;
  1483. if (s->ctx->gen_stateless_cookie_cb == NULL) {
  1484. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1485. SSL_R_NO_COOKIE_CALLBACK_SET);
  1486. return EXT_RETURN_FAIL;
  1487. }
  1488. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie)
  1489. || !WPACKET_start_sub_packet_u16(pkt)
  1490. || !WPACKET_start_sub_packet_u16(pkt)
  1491. || !WPACKET_get_total_written(pkt, &startlen)
  1492. || !WPACKET_reserve_bytes(pkt, MAX_COOKIE_SIZE, &cookie)
  1493. || !WPACKET_put_bytes_u16(pkt, COOKIE_STATE_FORMAT_VERSION)
  1494. || !WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION)
  1495. || !WPACKET_put_bytes_u16(pkt, s->s3->group_id)
  1496. || !s->method->put_cipher_by_char(s->s3->tmp.new_cipher, pkt,
  1497. &ciphlen)
  1498. /* Is there a key_share extension present in this HRR? */
  1499. || !WPACKET_put_bytes_u8(pkt, s->s3->peer_tmp == NULL)
  1500. || !WPACKET_put_bytes_u32(pkt, (unsigned int)time(NULL))
  1501. || !WPACKET_start_sub_packet_u16(pkt)
  1502. || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &hashval1)) {
  1503. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1504. ERR_R_INTERNAL_ERROR);
  1505. return EXT_RETURN_FAIL;
  1506. }
  1507. /*
  1508. * Get the hash of the initial ClientHello. ssl_handshake_hash() operates
  1509. * on raw buffers, so we first reserve sufficient bytes (above) and then
  1510. * subsequently allocate them (below)
  1511. */
  1512. if (!ssl3_digest_cached_records(s, 0)
  1513. || !ssl_handshake_hash(s, hashval1, EVP_MAX_MD_SIZE, &hashlen)) {
  1514. /* SSLfatal() already called */
  1515. return EXT_RETURN_FAIL;
  1516. }
  1517. if (!WPACKET_allocate_bytes(pkt, hashlen, &hashval2)
  1518. || !ossl_assert(hashval1 == hashval2)
  1519. || !WPACKET_close(pkt)
  1520. || !WPACKET_start_sub_packet_u8(pkt)
  1521. || !WPACKET_reserve_bytes(pkt, SSL_COOKIE_LENGTH, &appcookie1)) {
  1522. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1523. ERR_R_INTERNAL_ERROR);
  1524. return EXT_RETURN_FAIL;
  1525. }
  1526. /* Generate the application cookie */
  1527. if (s->ctx->gen_stateless_cookie_cb(s, appcookie1, &appcookielen) == 0) {
  1528. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1529. SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
  1530. return EXT_RETURN_FAIL;
  1531. }
  1532. if (!WPACKET_allocate_bytes(pkt, appcookielen, &appcookie2)
  1533. || !ossl_assert(appcookie1 == appcookie2)
  1534. || !WPACKET_close(pkt)
  1535. || !WPACKET_get_total_written(pkt, &totcookielen)
  1536. || !WPACKET_reserve_bytes(pkt, SHA256_DIGEST_LENGTH, &hmac)) {
  1537. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1538. ERR_R_INTERNAL_ERROR);
  1539. return EXT_RETURN_FAIL;
  1540. }
  1541. hmaclen = SHA256_DIGEST_LENGTH;
  1542. totcookielen -= startlen;
  1543. if (!ossl_assert(totcookielen <= MAX_COOKIE_SIZE - SHA256_DIGEST_LENGTH)) {
  1544. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1545. ERR_R_INTERNAL_ERROR);
  1546. return EXT_RETURN_FAIL;
  1547. }
  1548. /* HMAC the cookie */
  1549. hctx = EVP_MD_CTX_create();
  1550. pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
  1551. s->session_ctx->ext.cookie_hmac_key,
  1552. sizeof(s->session_ctx->ext
  1553. .cookie_hmac_key));
  1554. if (hctx == NULL || pkey == NULL) {
  1555. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1556. ERR_R_MALLOC_FAILURE);
  1557. goto err;
  1558. }
  1559. if (EVP_DigestSignInit(hctx, NULL, EVP_sha256(), NULL, pkey) <= 0
  1560. || EVP_DigestSign(hctx, hmac, &hmaclen, cookie,
  1561. totcookielen) <= 0) {
  1562. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1563. ERR_R_INTERNAL_ERROR);
  1564. goto err;
  1565. }
  1566. if (!ossl_assert(totcookielen + hmaclen <= MAX_COOKIE_SIZE)) {
  1567. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1568. ERR_R_INTERNAL_ERROR);
  1569. goto err;
  1570. }
  1571. if (!WPACKET_allocate_bytes(pkt, hmaclen, &hmac2)
  1572. || !ossl_assert(hmac == hmac2)
  1573. || !ossl_assert(cookie == hmac - totcookielen)
  1574. || !WPACKET_close(pkt)
  1575. || !WPACKET_close(pkt)) {
  1576. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_COOKIE,
  1577. ERR_R_INTERNAL_ERROR);
  1578. goto err;
  1579. }
  1580. ret = EXT_RETURN_SENT;
  1581. err:
  1582. EVP_MD_CTX_free(hctx);
  1583. EVP_PKEY_free(pkey);
  1584. return ret;
  1585. }
  1586. EXT_RETURN tls_construct_stoc_cryptopro_bug(SSL *s, WPACKET *pkt,
  1587. unsigned int context, X509 *x,
  1588. size_t chainidx)
  1589. {
  1590. const unsigned char cryptopro_ext[36] = {
  1591. 0xfd, 0xe8, /* 65000 */
  1592. 0x00, 0x20, /* 32 bytes length */
  1593. 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
  1594. 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
  1595. 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
  1596. 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
  1597. };
  1598. if (((s->s3->tmp.new_cipher->id & 0xFFFF) != 0x80
  1599. && (s->s3->tmp.new_cipher->id & 0xFFFF) != 0x81)
  1600. || (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG) == 0)
  1601. return EXT_RETURN_NOT_SENT;
  1602. if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) {
  1603. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1604. SSL_F_TLS_CONSTRUCT_STOC_CRYPTOPRO_BUG, ERR_R_INTERNAL_ERROR);
  1605. return EXT_RETURN_FAIL;
  1606. }
  1607. return EXT_RETURN_SENT;
  1608. }
  1609. EXT_RETURN tls_construct_stoc_early_data(SSL *s, WPACKET *pkt,
  1610. unsigned int context, X509 *x,
  1611. size_t chainidx)
  1612. {
  1613. if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {
  1614. if (s->max_early_data == 0)
  1615. return EXT_RETURN_NOT_SENT;
  1616. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
  1617. || !WPACKET_start_sub_packet_u16(pkt)
  1618. || !WPACKET_put_bytes_u32(pkt, s->max_early_data)
  1619. || !WPACKET_close(pkt)) {
  1620. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1621. SSL_F_TLS_CONSTRUCT_STOC_EARLY_DATA, ERR_R_INTERNAL_ERROR);
  1622. return EXT_RETURN_FAIL;
  1623. }
  1624. return EXT_RETURN_SENT;
  1625. }
  1626. if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED)
  1627. return EXT_RETURN_NOT_SENT;
  1628. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
  1629. || !WPACKET_start_sub_packet_u16(pkt)
  1630. || !WPACKET_close(pkt)) {
  1631. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_EARLY_DATA,
  1632. ERR_R_INTERNAL_ERROR);
  1633. return EXT_RETURN_FAIL;
  1634. }
  1635. return EXT_RETURN_SENT;
  1636. }
  1637. EXT_RETURN tls_construct_stoc_psk(SSL *s, WPACKET *pkt, unsigned int context,
  1638. X509 *x, size_t chainidx)
  1639. {
  1640. if (!s->hit)
  1641. return EXT_RETURN_NOT_SENT;
  1642. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)
  1643. || !WPACKET_start_sub_packet_u16(pkt)
  1644. || !WPACKET_put_bytes_u16(pkt, s->session->ext.tick_identity)
  1645. || !WPACKET_close(pkt)) {
  1646. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1647. SSL_F_TLS_CONSTRUCT_STOC_PSK, ERR_R_INTERNAL_ERROR);
  1648. return EXT_RETURN_FAIL;
  1649. }
  1650. return EXT_RETURN_SENT;
  1651. }