extensions_srvr.c 74 KB

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