extensions.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937
  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. #if defined(__TANDEM) && defined(_SPT_MODEL_)
  10. # include <spthread.h>
  11. # include <spt_extensions.h> /* timeval */
  12. #endif
  13. #include <string.h>
  14. #include "internal/nelem.h"
  15. #include "internal/cryptlib.h"
  16. #include "../ssl_local.h"
  17. #include "statem_local.h"
  18. static int final_renegotiate(SSL_CONNECTION *s, unsigned int context, int sent);
  19. static int init_server_name(SSL_CONNECTION *s, unsigned int context);
  20. static int final_server_name(SSL_CONNECTION *s, unsigned int context, int sent);
  21. static int final_ec_pt_formats(SSL_CONNECTION *s, unsigned int context,
  22. int sent);
  23. static int init_session_ticket(SSL_CONNECTION *s, unsigned int context);
  24. #ifndef OPENSSL_NO_OCSP
  25. static int init_status_request(SSL_CONNECTION *s, unsigned int context);
  26. #endif
  27. #ifndef OPENSSL_NO_NEXTPROTONEG
  28. static int init_npn(SSL_CONNECTION *s, unsigned int context);
  29. #endif
  30. static int init_alpn(SSL_CONNECTION *s, unsigned int context);
  31. static int final_alpn(SSL_CONNECTION *s, unsigned int context, int sent);
  32. static int init_sig_algs_cert(SSL_CONNECTION *s, unsigned int context);
  33. static int init_sig_algs(SSL_CONNECTION *s, unsigned int context);
  34. static int init_server_cert_type(SSL_CONNECTION *sc, unsigned int context);
  35. static int init_client_cert_type(SSL_CONNECTION *sc, unsigned int context);
  36. static int init_certificate_authorities(SSL_CONNECTION *s,
  37. unsigned int context);
  38. static EXT_RETURN tls_construct_certificate_authorities(SSL_CONNECTION *s,
  39. WPACKET *pkt,
  40. unsigned int context,
  41. X509 *x,
  42. size_t chainidx);
  43. static int tls_parse_certificate_authorities(SSL_CONNECTION *s, PACKET *pkt,
  44. unsigned int context, X509 *x,
  45. size_t chainidx);
  46. #ifndef OPENSSL_NO_SRP
  47. static int init_srp(SSL_CONNECTION *s, unsigned int context);
  48. #endif
  49. static int init_ec_point_formats(SSL_CONNECTION *s, unsigned int context);
  50. static int init_etm(SSL_CONNECTION *s, unsigned int context);
  51. static int init_ems(SSL_CONNECTION *s, unsigned int context);
  52. static int final_ems(SSL_CONNECTION *s, unsigned int context, int sent);
  53. static int init_psk_kex_modes(SSL_CONNECTION *s, unsigned int context);
  54. static int final_key_share(SSL_CONNECTION *s, unsigned int context, int sent);
  55. #ifndef OPENSSL_NO_SRTP
  56. static int init_srtp(SSL_CONNECTION *s, unsigned int context);
  57. #endif
  58. static int final_sig_algs(SSL_CONNECTION *s, unsigned int context, int sent);
  59. static int final_early_data(SSL_CONNECTION *s, unsigned int context, int sent);
  60. static int final_maxfragmentlen(SSL_CONNECTION *s, unsigned int context,
  61. int sent);
  62. static int init_post_handshake_auth(SSL_CONNECTION *s, unsigned int context);
  63. static int final_psk(SSL_CONNECTION *s, unsigned int context, int sent);
  64. static int tls_init_compress_certificate(SSL_CONNECTION *sc, unsigned int context);
  65. static EXT_RETURN tls_construct_compress_certificate(SSL_CONNECTION *sc, WPACKET *pkt,
  66. unsigned int context,
  67. X509 *x, size_t chainidx);
  68. static int tls_parse_compress_certificate(SSL_CONNECTION *sc, PACKET *pkt,
  69. unsigned int context,
  70. X509 *x, size_t chainidx);
  71. /* Structure to define a built-in extension */
  72. typedef struct extensions_definition_st {
  73. /* The defined type for the extension */
  74. unsigned int type;
  75. /*
  76. * The context that this extension applies to, e.g. what messages and
  77. * protocol versions
  78. */
  79. unsigned int context;
  80. /*
  81. * Initialise extension before parsing. Always called for relevant contexts
  82. * even if extension not present
  83. */
  84. int (*init)(SSL_CONNECTION *s, unsigned int context);
  85. /* Parse extension sent from client to server */
  86. int (*parse_ctos)(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
  87. X509 *x, size_t chainidx);
  88. /* Parse extension send from server to client */
  89. int (*parse_stoc)(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
  90. X509 *x, size_t chainidx);
  91. /* Construct extension sent from server to client */
  92. EXT_RETURN (*construct_stoc)(SSL_CONNECTION *s, WPACKET *pkt,
  93. unsigned int context,
  94. X509 *x, size_t chainidx);
  95. /* Construct extension sent from client to server */
  96. EXT_RETURN (*construct_ctos)(SSL_CONNECTION *s, WPACKET *pkt,
  97. unsigned int context,
  98. X509 *x, size_t chainidx);
  99. /*
  100. * Finalise extension after parsing. Always called where an extensions was
  101. * initialised even if the extension was not present. |sent| is set to 1 if
  102. * the extension was seen, or 0 otherwise.
  103. */
  104. int (*final)(SSL_CONNECTION *s, unsigned int context, int sent);
  105. } EXTENSION_DEFINITION;
  106. /*
  107. * Definitions of all built-in extensions. NOTE: Changes in the number or order
  108. * of these extensions should be mirrored with equivalent changes to the
  109. * indexes ( TLSEXT_IDX_* ) defined in ssl_local.h.
  110. * Extensions should be added to test/ext_internal_test.c as well, as that
  111. * tests the ordering of the extensions.
  112. *
  113. * Each extension has an initialiser, a client and
  114. * server side parser and a finaliser. The initialiser is called (if the
  115. * extension is relevant to the given context) even if we did not see the
  116. * extension in the message that we received. The parser functions are only
  117. * called if we see the extension in the message. The finalisers are always
  118. * called if the initialiser was called.
  119. * There are also server and client side constructor functions which are always
  120. * called during message construction if the extension is relevant for the
  121. * given context.
  122. * The initialisation, parsing, finalisation and construction functions are
  123. * always called in the order defined in this list. Some extensions may depend
  124. * on others having been processed first, so the order of this list is
  125. * significant.
  126. * The extension context is defined by a series of flags which specify which
  127. * messages the extension is relevant to. These flags also specify whether the
  128. * extension is relevant to a particular protocol or protocol version.
  129. *
  130. * NOTE: WebSphere Application Server 7+ cannot handle empty extensions at
  131. * the end, keep these extensions before signature_algorithm.
  132. */
  133. #define INVALID_EXTENSION { TLSEXT_TYPE_invalid, 0, NULL, NULL, NULL, NULL, NULL, NULL }
  134. static const EXTENSION_DEFINITION ext_defs[] = {
  135. {
  136. TLSEXT_TYPE_renegotiate,
  137. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  138. | SSL_EXT_SSL3_ALLOWED | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  139. NULL, tls_parse_ctos_renegotiate, tls_parse_stoc_renegotiate,
  140. tls_construct_stoc_renegotiate, tls_construct_ctos_renegotiate,
  141. final_renegotiate
  142. },
  143. {
  144. TLSEXT_TYPE_server_name,
  145. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  146. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
  147. init_server_name,
  148. tls_parse_ctos_server_name, tls_parse_stoc_server_name,
  149. tls_construct_stoc_server_name, tls_construct_ctos_server_name,
  150. final_server_name
  151. },
  152. {
  153. TLSEXT_TYPE_max_fragment_length,
  154. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  155. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
  156. NULL, tls_parse_ctos_maxfragmentlen, tls_parse_stoc_maxfragmentlen,
  157. tls_construct_stoc_maxfragmentlen, tls_construct_ctos_maxfragmentlen,
  158. final_maxfragmentlen
  159. },
  160. #ifndef OPENSSL_NO_SRP
  161. {
  162. TLSEXT_TYPE_srp,
  163. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  164. init_srp, tls_parse_ctos_srp, NULL, NULL, tls_construct_ctos_srp, NULL
  165. },
  166. #else
  167. INVALID_EXTENSION,
  168. #endif
  169. {
  170. TLSEXT_TYPE_ec_point_formats,
  171. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  172. | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  173. init_ec_point_formats, tls_parse_ctos_ec_pt_formats, tls_parse_stoc_ec_pt_formats,
  174. tls_construct_stoc_ec_pt_formats, tls_construct_ctos_ec_pt_formats,
  175. final_ec_pt_formats
  176. },
  177. {
  178. /*
  179. * "supported_groups" is spread across several specifications.
  180. * It was originally specified as "elliptic_curves" in RFC 4492,
  181. * and broadened to include named FFDH groups by RFC 7919.
  182. * Both RFCs 4492 and 7919 do not include a provision for the server
  183. * to indicate to the client the complete list of groups supported
  184. * by the server, with the server instead just indicating the
  185. * selected group for this connection in the ServerKeyExchange
  186. * message. (D)TLS 1.3 adds a scheme for the server to indicate
  187. * to the client its list of supported groups in the
  188. * EncryptedExtensions message, but none of the relevant
  189. * specifications permit sending supported_groups in the ServerHello.
  190. * Nonetheless (possibly due to the close proximity to the
  191. * "ec_point_formats" extension, which is allowed in the ServerHello),
  192. * there are several servers that send this extension in the
  193. * ServerHello anyway. Up to and including the 1.1.0 release,
  194. * we did not check for the presence of nonpermitted extensions,
  195. * so to avoid a regression, we must permit this extension in the
  196. * (D)TLS 1.2 ServerHello as well.
  197. *
  198. * Note that there is no tls_parse_stoc_supported_groups function,
  199. * so we do not perform any additional parsing, validation, or
  200. * processing on the server's group list -- this is just a minimal
  201. * change to preserve compatibility with these misbehaving servers.
  202. */
  203. TLSEXT_TYPE_supported_groups,
  204. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
  205. | SSL_EXT_TLS1_2_SERVER_HELLO,
  206. NULL, tls_parse_ctos_supported_groups, NULL,
  207. tls_construct_stoc_supported_groups,
  208. tls_construct_ctos_supported_groups, NULL
  209. },
  210. {
  211. TLSEXT_TYPE_session_ticket,
  212. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  213. | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  214. init_session_ticket, tls_parse_ctos_session_ticket,
  215. tls_parse_stoc_session_ticket, tls_construct_stoc_session_ticket,
  216. tls_construct_ctos_session_ticket, NULL
  217. },
  218. #ifndef OPENSSL_NO_OCSP
  219. {
  220. TLSEXT_TYPE_status_request,
  221. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  222. | SSL_EXT_TLS1_3_CERTIFICATE | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
  223. init_status_request, tls_parse_ctos_status_request,
  224. tls_parse_stoc_status_request, tls_construct_stoc_status_request,
  225. tls_construct_ctos_status_request, NULL
  226. },
  227. #else
  228. INVALID_EXTENSION,
  229. #endif
  230. #ifndef OPENSSL_NO_NEXTPROTONEG
  231. {
  232. TLSEXT_TYPE_next_proto_neg,
  233. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  234. | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  235. init_npn, tls_parse_ctos_npn, tls_parse_stoc_npn,
  236. tls_construct_stoc_next_proto_neg, tls_construct_ctos_npn, NULL
  237. },
  238. #else
  239. INVALID_EXTENSION,
  240. #endif
  241. {
  242. /*
  243. * Must appear in this list after server_name so that finalisation
  244. * happens after server_name callbacks
  245. */
  246. TLSEXT_TYPE_application_layer_protocol_negotiation,
  247. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  248. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
  249. init_alpn, tls_parse_ctos_alpn, tls_parse_stoc_alpn,
  250. tls_construct_stoc_alpn, tls_construct_ctos_alpn, final_alpn
  251. },
  252. #ifndef OPENSSL_NO_SRTP
  253. {
  254. TLSEXT_TYPE_use_srtp,
  255. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  256. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS | SSL_EXT_DTLS_ONLY,
  257. init_srtp, tls_parse_ctos_use_srtp, tls_parse_stoc_use_srtp,
  258. tls_construct_stoc_use_srtp, tls_construct_ctos_use_srtp, NULL
  259. },
  260. #else
  261. INVALID_EXTENSION,
  262. #endif
  263. {
  264. TLSEXT_TYPE_encrypt_then_mac,
  265. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  266. | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  267. init_etm, tls_parse_ctos_etm, tls_parse_stoc_etm,
  268. tls_construct_stoc_etm, tls_construct_ctos_etm, NULL
  269. },
  270. #ifndef OPENSSL_NO_CT
  271. {
  272. TLSEXT_TYPE_signed_certificate_timestamp,
  273. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  274. | SSL_EXT_TLS1_3_CERTIFICATE | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
  275. NULL,
  276. /*
  277. * No server side support for this, but can be provided by a custom
  278. * extension. This is an exception to the rule that custom extensions
  279. * cannot override built in ones.
  280. */
  281. NULL, tls_parse_stoc_sct, NULL, tls_construct_ctos_sct, NULL
  282. },
  283. #else
  284. INVALID_EXTENSION,
  285. #endif
  286. {
  287. TLSEXT_TYPE_extended_master_secret,
  288. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  289. | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  290. init_ems, tls_parse_ctos_ems, tls_parse_stoc_ems,
  291. tls_construct_stoc_ems, tls_construct_ctos_ems, final_ems
  292. },
  293. {
  294. TLSEXT_TYPE_signature_algorithms_cert,
  295. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
  296. init_sig_algs_cert, tls_parse_ctos_sig_algs_cert,
  297. tls_parse_ctos_sig_algs_cert,
  298. /* We do not generate signature_algorithms_cert at present. */
  299. NULL, NULL, NULL
  300. },
  301. {
  302. TLSEXT_TYPE_post_handshake_auth,
  303. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ONLY,
  304. init_post_handshake_auth,
  305. tls_parse_ctos_post_handshake_auth, NULL,
  306. NULL, tls_construct_ctos_post_handshake_auth,
  307. NULL,
  308. },
  309. {
  310. TLSEXT_TYPE_client_cert_type,
  311. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
  312. | SSL_EXT_TLS1_2_SERVER_HELLO,
  313. init_client_cert_type,
  314. tls_parse_ctos_client_cert_type, tls_parse_stoc_client_cert_type,
  315. tls_construct_stoc_client_cert_type, tls_construct_ctos_client_cert_type,
  316. NULL
  317. },
  318. {
  319. TLSEXT_TYPE_server_cert_type,
  320. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
  321. | SSL_EXT_TLS1_2_SERVER_HELLO,
  322. init_server_cert_type,
  323. tls_parse_ctos_server_cert_type, tls_parse_stoc_server_cert_type,
  324. tls_construct_stoc_server_cert_type, tls_construct_ctos_server_cert_type,
  325. NULL
  326. },
  327. {
  328. TLSEXT_TYPE_signature_algorithms,
  329. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
  330. init_sig_algs, tls_parse_ctos_sig_algs,
  331. tls_parse_ctos_sig_algs, tls_construct_ctos_sig_algs,
  332. tls_construct_ctos_sig_algs, final_sig_algs
  333. },
  334. {
  335. TLSEXT_TYPE_supported_versions,
  336. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
  337. | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST,
  338. NULL,
  339. /* Processed inline as part of version selection */
  340. NULL, tls_parse_stoc_supported_versions,
  341. tls_construct_stoc_supported_versions,
  342. tls_construct_ctos_supported_versions, NULL
  343. },
  344. {
  345. TLSEXT_TYPE_psk_kex_modes,
  346. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ONLY,
  347. init_psk_kex_modes, tls_parse_ctos_psk_kex_modes, NULL, NULL,
  348. tls_construct_ctos_psk_kex_modes, NULL
  349. },
  350. {
  351. /*
  352. * Must be in this list after supported_groups. We need that to have
  353. * been parsed before we do this one.
  354. */
  355. TLSEXT_TYPE_key_share,
  356. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
  357. | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
  358. | SSL_EXT_TLS1_3_ONLY,
  359. NULL, tls_parse_ctos_key_share, tls_parse_stoc_key_share,
  360. tls_construct_stoc_key_share, tls_construct_ctos_key_share,
  361. final_key_share
  362. },
  363. {
  364. /* Must be after key_share */
  365. TLSEXT_TYPE_cookie,
  366. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
  367. | SSL_EXT_TLS1_3_ONLY,
  368. NULL, tls_parse_ctos_cookie, tls_parse_stoc_cookie,
  369. tls_construct_stoc_cookie, tls_construct_ctos_cookie, NULL
  370. },
  371. {
  372. /*
  373. * Special unsolicited ServerHello extension only used when
  374. * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set. We allow it in a ClientHello but
  375. * ignore it.
  376. */
  377. TLSEXT_TYPE_cryptopro_bug,
  378. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
  379. | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
  380. NULL, NULL, NULL, tls_construct_stoc_cryptopro_bug, NULL, NULL
  381. },
  382. {
  383. TLSEXT_TYPE_compress_certificate,
  384. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
  385. | SSL_EXT_TLS1_3_ONLY,
  386. tls_init_compress_certificate,
  387. tls_parse_compress_certificate, tls_parse_compress_certificate,
  388. tls_construct_compress_certificate, tls_construct_compress_certificate,
  389. NULL
  390. },
  391. {
  392. TLSEXT_TYPE_early_data,
  393. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
  394. | SSL_EXT_TLS1_3_NEW_SESSION_TICKET | SSL_EXT_TLS1_3_ONLY,
  395. NULL, tls_parse_ctos_early_data, tls_parse_stoc_early_data,
  396. tls_construct_stoc_early_data, tls_construct_ctos_early_data,
  397. final_early_data
  398. },
  399. {
  400. TLSEXT_TYPE_certificate_authorities,
  401. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
  402. | SSL_EXT_TLS1_3_ONLY,
  403. init_certificate_authorities,
  404. tls_parse_certificate_authorities, tls_parse_certificate_authorities,
  405. tls_construct_certificate_authorities,
  406. tls_construct_certificate_authorities, NULL,
  407. },
  408. {
  409. /* Must be immediately before pre_shared_key */
  410. TLSEXT_TYPE_padding,
  411. SSL_EXT_CLIENT_HELLO,
  412. NULL,
  413. /* We send this, but don't read it */
  414. NULL, NULL, NULL, tls_construct_ctos_padding, NULL
  415. },
  416. {
  417. /* Required by the (D)TLSv1.3 spec to always be the last extension */
  418. TLSEXT_TYPE_psk,
  419. SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
  420. | SSL_EXT_TLS1_3_ONLY,
  421. NULL, tls_parse_ctos_psk, tls_parse_stoc_psk, tls_construct_stoc_psk,
  422. tls_construct_ctos_psk, final_psk
  423. }
  424. };
  425. /* Returns a TLSEXT_TYPE for the given index */
  426. unsigned int ossl_get_extension_type(size_t idx)
  427. {
  428. size_t num_exts = OSSL_NELEM(ext_defs);
  429. if (idx >= num_exts)
  430. return TLSEXT_TYPE_out_of_range;
  431. return ext_defs[idx].type;
  432. }
  433. /* Check whether an extension's context matches the current context */
  434. static int validate_context(SSL_CONNECTION *s, unsigned int extctx,
  435. unsigned int thisctx)
  436. {
  437. /* Check we're allowed to use this extension in this context */
  438. if ((thisctx & extctx) == 0)
  439. return 0;
  440. if (SSL_CONNECTION_IS_DTLS(s)) {
  441. if ((extctx & SSL_EXT_TLS_ONLY) != 0)
  442. return 0;
  443. } else if ((extctx & SSL_EXT_DTLS_ONLY) != 0) {
  444. return 0;
  445. }
  446. return 1;
  447. }
  448. int tls_validate_all_contexts(SSL_CONNECTION *s, unsigned int thisctx,
  449. RAW_EXTENSION *exts)
  450. {
  451. size_t i, num_exts, builtin_num = OSSL_NELEM(ext_defs), offset;
  452. RAW_EXTENSION *thisext;
  453. unsigned int context;
  454. ENDPOINT role = ENDPOINT_BOTH;
  455. if ((thisctx & SSL_EXT_CLIENT_HELLO) != 0)
  456. role = ENDPOINT_SERVER;
  457. else if ((thisctx & SSL_EXT_TLS1_2_SERVER_HELLO) != 0)
  458. role = ENDPOINT_CLIENT;
  459. /* Calculate the number of extensions in the extensions list */
  460. num_exts = builtin_num + s->cert->custext.meths_count;
  461. for (thisext = exts, i = 0; i < num_exts; i++, thisext++) {
  462. if (!thisext->present)
  463. continue;
  464. if (i < builtin_num) {
  465. context = ext_defs[i].context;
  466. } else {
  467. custom_ext_method *meth = NULL;
  468. meth = custom_ext_find(&s->cert->custext, role, thisext->type,
  469. &offset);
  470. if (!ossl_assert(meth != NULL))
  471. return 0;
  472. context = meth->context;
  473. }
  474. if (!validate_context(s, context, thisctx))
  475. return 0;
  476. }
  477. return 1;
  478. }
  479. /*
  480. * Verify whether we are allowed to use the extension |type| in the current
  481. * |context|. Returns 1 to indicate the extension is allowed or unknown or 0 to
  482. * indicate the extension is not allowed. If returning 1 then |*found| is set to
  483. * the definition for the extension we found.
  484. */
  485. static int verify_extension(SSL_CONNECTION *s, unsigned int context,
  486. unsigned int type, custom_ext_methods *meths,
  487. RAW_EXTENSION *rawexlist, RAW_EXTENSION **found)
  488. {
  489. size_t i;
  490. size_t builtin_num = OSSL_NELEM(ext_defs);
  491. const EXTENSION_DEFINITION *thisext;
  492. for (i = 0, thisext = ext_defs; i < builtin_num; i++, thisext++) {
  493. if (type == thisext->type) {
  494. if (!validate_context(s, thisext->context, context))
  495. return 0;
  496. *found = &rawexlist[i];
  497. return 1;
  498. }
  499. }
  500. /* Check the custom extensions */
  501. if (meths != NULL) {
  502. size_t offset = 0;
  503. ENDPOINT role = ENDPOINT_BOTH;
  504. custom_ext_method *meth = NULL;
  505. if ((context & SSL_EXT_CLIENT_HELLO) != 0)
  506. role = ENDPOINT_SERVER;
  507. else if ((context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0)
  508. role = ENDPOINT_CLIENT;
  509. meth = custom_ext_find(meths, role, type, &offset);
  510. if (meth != NULL) {
  511. if (!validate_context(s, meth->context, context))
  512. return 0;
  513. *found = &rawexlist[offset + builtin_num];
  514. return 1;
  515. }
  516. }
  517. /* Unknown extension. We allow it */
  518. *found = NULL;
  519. return 1;
  520. }
  521. /*
  522. * Check whether the context defined for an extension |extctx| means whether
  523. * the extension is relevant for the current context |thisctx| or not. Returns
  524. * 1 if the extension is relevant for this context, and 0 otherwise
  525. */
  526. int extension_is_relevant(SSL_CONNECTION *s, unsigned int extctx,
  527. unsigned int thisctx)
  528. {
  529. int is_version13;
  530. /*
  531. * For HRR we haven't selected the version yet but we know it will be
  532. * (D)TLSv1.3
  533. */
  534. if ((thisctx & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0)
  535. is_version13 = 1;
  536. else
  537. is_version13 = SSL_CONNECTION_IS_VERSION13(s);
  538. if ((SSL_CONNECTION_IS_DTLS(s)
  539. && (extctx & SSL_EXT_TLS_IMPLEMENTATION_ONLY) != 0)
  540. || (s->version == SSL3_VERSION
  541. && (extctx & SSL_EXT_SSL3_ALLOWED) == 0)
  542. /*
  543. * Note that is_version13 means "(D)TLS 1.3 has been negotiated",
  544. * which is never true when generating the ClientHello.
  545. * However, version negotiation *has* occurred by the time the
  546. * ClientHello extensions are being parsed.
  547. * Be careful to allow (D)TLS 1.3-only extensions when generating
  548. * the ClientHello.
  549. */
  550. || (is_version13 && (extctx & SSL_EXT_TLS1_2_AND_BELOW_ONLY) != 0)
  551. || (!is_version13 && (extctx & SSL_EXT_TLS1_3_ONLY) != 0
  552. && (thisctx & SSL_EXT_CLIENT_HELLO) == 0)
  553. || (s->server && !is_version13 && (extctx & SSL_EXT_TLS1_3_ONLY) != 0)
  554. || (s->hit && (extctx & SSL_EXT_IGNORE_ON_RESUMPTION) != 0))
  555. return 0;
  556. return 1;
  557. }
  558. /*
  559. * Gather a list of all the extensions from the data in |packet]. |context|
  560. * tells us which message this extension is for. The raw extension data is
  561. * stored in |*res| on success. We don't actually process the content of the
  562. * extensions yet, except to check their types. This function also runs the
  563. * initialiser functions for all known extensions if |init| is nonzero (whether
  564. * we have collected them or not). If successful the caller is responsible for
  565. * freeing the contents of |*res|.
  566. *
  567. * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
  568. * more than one extension of the same type in a ClientHello or ServerHello.
  569. * This function returns 1 if all extensions are unique and we have parsed their
  570. * types, and 0 if the extensions contain duplicates, could not be successfully
  571. * found, or an internal error occurred. We only check duplicates for
  572. * extensions that we know about. We ignore others.
  573. */
  574. int tls_collect_extensions(SSL_CONNECTION *s, PACKET *packet,
  575. unsigned int context,
  576. RAW_EXTENSION **res, size_t *len, int init)
  577. {
  578. PACKET extensions = *packet;
  579. size_t i = 0;
  580. size_t num_exts;
  581. custom_ext_methods *exts = &s->cert->custext;
  582. RAW_EXTENSION *raw_extensions = NULL;
  583. const EXTENSION_DEFINITION *thisexd;
  584. *res = NULL;
  585. /*
  586. * Initialise server side custom extensions. Client side is done during
  587. * construction of extensions for the ClientHello.
  588. */
  589. if ((context & SSL_EXT_CLIENT_HELLO) != 0)
  590. custom_ext_init(&s->cert->custext);
  591. num_exts = OSSL_NELEM(ext_defs) + (exts != NULL ? exts->meths_count : 0);
  592. raw_extensions = OPENSSL_zalloc(num_exts * sizeof(*raw_extensions));
  593. if (raw_extensions == NULL) {
  594. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  595. return 0;
  596. }
  597. i = 0;
  598. while (PACKET_remaining(&extensions) > 0) {
  599. unsigned int type, idx;
  600. PACKET extension;
  601. RAW_EXTENSION *thisex;
  602. if (!PACKET_get_net_2(&extensions, &type) ||
  603. !PACKET_get_length_prefixed_2(&extensions, &extension)) {
  604. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
  605. goto err;
  606. }
  607. /*
  608. * Verify this extension is allowed. We only check duplicates for
  609. * extensions that we recognise. We also have a special case for the
  610. * PSK extension, which must be the last one in the ClientHello.
  611. */
  612. if (!verify_extension(s, context, type, exts, raw_extensions, &thisex)
  613. || (thisex != NULL && thisex->present == 1)
  614. || (type == TLSEXT_TYPE_psk
  615. && (context & SSL_EXT_CLIENT_HELLO) != 0
  616. && PACKET_remaining(&extensions) != 0)) {
  617. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
  618. goto err;
  619. }
  620. idx = thisex - raw_extensions;
  621. /*-
  622. * Check that we requested this extension (if appropriate). Requests can
  623. * be sent in the ClientHello and CertificateRequest. Unsolicited
  624. * extensions can be sent in the NewSessionTicket. We only do this for
  625. * the built-in extensions. Custom extensions have a different but
  626. * similar check elsewhere.
  627. * Special cases:
  628. * - The HRR cookie extension is unsolicited
  629. * - The renegotiate extension is unsolicited (the client signals
  630. * support via an SCSV)
  631. * - The signed_certificate_timestamp extension can be provided by a
  632. * custom extension or by the built-in version. We let the extension
  633. * itself handle unsolicited response checks.
  634. */
  635. if (idx < OSSL_NELEM(ext_defs)
  636. && (context & (SSL_EXT_CLIENT_HELLO
  637. | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
  638. | SSL_EXT_TLS1_3_NEW_SESSION_TICKET)) == 0
  639. && type != TLSEXT_TYPE_cookie
  640. && type != TLSEXT_TYPE_renegotiate
  641. && type != TLSEXT_TYPE_signed_certificate_timestamp
  642. && (s->ext.extflags[idx] & SSL_EXT_FLAG_SENT) == 0
  643. #ifndef OPENSSL_NO_GOST
  644. && !((context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0
  645. && type == TLSEXT_TYPE_cryptopro_bug)
  646. #endif
  647. ) {
  648. SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION,
  649. SSL_R_UNSOLICITED_EXTENSION);
  650. goto err;
  651. }
  652. if (thisex != NULL) {
  653. thisex->data = extension;
  654. thisex->present = 1;
  655. thisex->type = type;
  656. thisex->received_order = i++;
  657. if (s->ext.debug_cb)
  658. s->ext.debug_cb(SSL_CONNECTION_GET_SSL(s), !s->server,
  659. thisex->type, PACKET_data(&thisex->data),
  660. PACKET_remaining(&thisex->data),
  661. s->ext.debug_arg);
  662. }
  663. }
  664. if (init) {
  665. /*
  666. * Initialise all known extensions relevant to this context,
  667. * whether we have found them or not
  668. */
  669. for (thisexd = ext_defs, i = 0; i < OSSL_NELEM(ext_defs);
  670. i++, thisexd++) {
  671. if (thisexd->init != NULL && (thisexd->context & context) != 0
  672. && extension_is_relevant(s, thisexd->context, context)
  673. && !thisexd->init(s, context)) {
  674. /* SSLfatal() already called */
  675. goto err;
  676. }
  677. }
  678. }
  679. *res = raw_extensions;
  680. if (len != NULL)
  681. *len = num_exts;
  682. return 1;
  683. err:
  684. OPENSSL_free(raw_extensions);
  685. return 0;
  686. }
  687. /*
  688. * Runs the parser for a given extension with index |idx|. |exts| contains the
  689. * list of all parsed extensions previously collected by
  690. * tls_collect_extensions(). The parser is only run if it is applicable for the
  691. * given |context| and the parser has not already been run. If this is for a
  692. * Certificate message, then we also provide the parser with the relevant
  693. * Certificate |x| and its position in the |chainidx| with 0 being the first
  694. * Certificate. Returns 1 on success or 0 on failure. If an extension is not
  695. * present this counted as success.
  696. */
  697. int tls_parse_extension(SSL_CONNECTION *s, TLSEXT_INDEX idx, int context,
  698. RAW_EXTENSION *exts, X509 *x, size_t chainidx)
  699. {
  700. RAW_EXTENSION *currext = &exts[idx];
  701. int (*parser)(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, X509 *x,
  702. size_t chainidx) = NULL;
  703. /* Skip if the extension is not present */
  704. if (!currext->present)
  705. return 1;
  706. /* Skip if we've already parsed this extension */
  707. if (currext->parsed)
  708. return 1;
  709. currext->parsed = 1;
  710. if (idx < OSSL_NELEM(ext_defs)) {
  711. /* We are handling a built-in extension */
  712. const EXTENSION_DEFINITION *extdef = &ext_defs[idx];
  713. /* Check if extension is defined for our protocol. If not, skip */
  714. if (!extension_is_relevant(s, extdef->context, context))
  715. return 1;
  716. parser = s->server ? extdef->parse_ctos : extdef->parse_stoc;
  717. if (parser != NULL)
  718. return parser(s, &currext->data, context, x, chainidx);
  719. /*
  720. * If the parser is NULL we fall through to the custom extension
  721. * processing
  722. */
  723. }
  724. /* Parse custom extensions */
  725. return custom_ext_parse(s, context, currext->type,
  726. PACKET_data(&currext->data),
  727. PACKET_remaining(&currext->data),
  728. x, chainidx);
  729. }
  730. /*
  731. * Parse all remaining extensions that have not yet been parsed. Also calls the
  732. * finalisation for all extensions at the end if |fin| is nonzero, whether we
  733. * collected them or not. Returns 1 for success or 0 for failure. If we are
  734. * working on a Certificate message then we also pass the Certificate |x| and
  735. * its position in the |chainidx|, with 0 being the first certificate.
  736. */
  737. int tls_parse_all_extensions(SSL_CONNECTION *s, int context,
  738. RAW_EXTENSION *exts, X509 *x,
  739. size_t chainidx, int fin)
  740. {
  741. size_t i, numexts = OSSL_NELEM(ext_defs);
  742. const EXTENSION_DEFINITION *thisexd;
  743. /* Calculate the number of extensions in the extensions list */
  744. numexts += s->cert->custext.meths_count;
  745. /* Parse each extension in turn */
  746. for (i = 0; i < numexts; i++) {
  747. if (!tls_parse_extension(s, i, context, exts, x, chainidx)) {
  748. /* SSLfatal() already called */
  749. return 0;
  750. }
  751. }
  752. if (fin) {
  753. /*
  754. * Finalise all known extensions relevant to this context,
  755. * whether we have found them or not
  756. */
  757. for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs);
  758. i++, thisexd++) {
  759. if (thisexd->final != NULL && (thisexd->context & context) != 0
  760. && !thisexd->final(s, context, exts[i].present)) {
  761. /* SSLfatal() already called */
  762. return 0;
  763. }
  764. }
  765. }
  766. return 1;
  767. }
  768. int should_add_extension(SSL_CONNECTION *s, unsigned int extctx,
  769. unsigned int thisctx, int max_version)
  770. {
  771. /* Skip if not relevant for our context */
  772. if ((extctx & thisctx) == 0)
  773. return 0;
  774. /* Check if this extension is defined for our protocol. If not, skip */
  775. if (!extension_is_relevant(s, extctx, thisctx)
  776. || ((extctx & SSL_EXT_TLS1_3_ONLY) != 0
  777. && (thisctx & SSL_EXT_CLIENT_HELLO) != 0
  778. && (SSL_CONNECTION_IS_DTLS(s) ? DTLS_VERSION_LT(max_version, DTLS1_3_VERSION)
  779. : max_version < TLS1_3_VERSION)))
  780. return 0;
  781. return 1;
  782. }
  783. /*
  784. * Construct all the extensions relevant to the current |context| and write
  785. * them to |pkt|. If this is an extension for a Certificate in a Certificate
  786. * message, then |x| will be set to the Certificate we are handling, and
  787. * |chainidx| will indicate the position in the chainidx we are processing (with
  788. * 0 being the first in the chain). Returns 1 on success or 0 on failure. On a
  789. * failure construction stops at the first extension to fail to construct.
  790. */
  791. int tls_construct_extensions(SSL_CONNECTION *s, WPACKET *pkt,
  792. unsigned int context,
  793. X509 *x, size_t chainidx)
  794. {
  795. size_t i;
  796. int min_version, max_version = 0, reason;
  797. const EXTENSION_DEFINITION *thisexd;
  798. int for_comp = (context & SSL_EXT_TLS1_3_CERTIFICATE_COMPRESSION) != 0;
  799. if (!WPACKET_start_sub_packet_u16(pkt)
  800. /*
  801. * If extensions are of zero length then we don't even add the
  802. * extensions length bytes to a ClientHello/ServerHello
  803. * (for non-(D)TLSv1.3).
  804. */
  805. || ((context &
  806. (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO)) != 0
  807. && !WPACKET_set_flags(pkt,
  808. WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))) {
  809. if (!for_comp)
  810. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  811. return 0;
  812. }
  813. if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
  814. reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);
  815. if (reason != 0) {
  816. if (!for_comp)
  817. SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason);
  818. return 0;
  819. }
  820. }
  821. /* Add custom extensions first */
  822. if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
  823. /* On the server side with initialise during ClientHello parsing */
  824. custom_ext_init(&s->cert->custext);
  825. }
  826. if (!custom_ext_add(s, context, pkt, x, chainidx, max_version)) {
  827. /* SSLfatal() already called */
  828. return 0;
  829. }
  830. for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs); i++, thisexd++) {
  831. EXT_RETURN (*construct)(SSL_CONNECTION *s, WPACKET *pkt,
  832. unsigned int context,
  833. X509 *x, size_t chainidx);
  834. EXT_RETURN ret;
  835. /* Skip if not relevant for our context */
  836. if (!should_add_extension(s, thisexd->context, context, max_version))
  837. continue;
  838. construct = s->server ? thisexd->construct_stoc
  839. : thisexd->construct_ctos;
  840. if (construct == NULL)
  841. continue;
  842. ret = construct(s, pkt, context, x, chainidx);
  843. if (ret == EXT_RETURN_FAIL) {
  844. /* SSLfatal() already called */
  845. return 0;
  846. }
  847. if (ret == EXT_RETURN_SENT
  848. && (context & (SSL_EXT_CLIENT_HELLO
  849. | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
  850. | SSL_EXT_TLS1_3_NEW_SESSION_TICKET)) != 0)
  851. s->ext.extflags[i] |= SSL_EXT_FLAG_SENT;
  852. }
  853. if (!WPACKET_close(pkt)) {
  854. if (!for_comp)
  855. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  856. return 0;
  857. }
  858. return 1;
  859. }
  860. /*
  861. * Built in extension finalisation and initialisation functions. All initialise
  862. * or finalise the associated extension type for the given |context|. For
  863. * finalisers |sent| is set to 1 if we saw the extension during parsing, and 0
  864. * otherwise. These functions return 1 on success or 0 on failure.
  865. */
  866. static int final_renegotiate(SSL_CONNECTION *s, unsigned int context, int sent)
  867. {
  868. if (!s->server) {
  869. /*
  870. * Check if we can connect to a server that doesn't support safe
  871. * renegotiation
  872. */
  873. if (!(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
  874. && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
  875. && !sent) {
  876. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  877. SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  878. return 0;
  879. }
  880. return 1;
  881. }
  882. /* Need RI if renegotiating */
  883. if (s->renegotiate
  884. && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
  885. && !sent) {
  886. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
  887. SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  888. return 0;
  889. }
  890. return 1;
  891. }
  892. static ossl_inline void ssl_tsan_decr(const SSL_CTX *ctx,
  893. TSAN_QUALIFIER int *stat)
  894. {
  895. if (ssl_tsan_lock(ctx)) {
  896. tsan_decr(stat);
  897. ssl_tsan_unlock(ctx);
  898. }
  899. }
  900. static int init_server_name(SSL_CONNECTION *s, unsigned int context)
  901. {
  902. if (s->server) {
  903. s->servername_done = 0;
  904. OPENSSL_free(s->ext.hostname);
  905. s->ext.hostname = NULL;
  906. }
  907. return 1;
  908. }
  909. static int final_server_name(SSL_CONNECTION *s, unsigned int context, int sent)
  910. {
  911. int ret = SSL_TLSEXT_ERR_NOACK;
  912. int altmp = SSL_AD_UNRECOGNIZED_NAME;
  913. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  914. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  915. int was_ticket = (SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0;
  916. if (!ossl_assert(sctx != NULL) || !ossl_assert(s->session_ctx != NULL)) {
  917. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  918. return 0;
  919. }
  920. if (sctx->ext.servername_cb != NULL)
  921. ret = sctx->ext.servername_cb(ssl, &altmp,
  922. sctx->ext.servername_arg);
  923. else if (s->session_ctx->ext.servername_cb != NULL)
  924. ret = s->session_ctx->ext.servername_cb(ssl, &altmp,
  925. s->session_ctx->ext.servername_arg);
  926. /*
  927. * For servers, propagate the SNI hostname from the temporary
  928. * storage in the SSL to the persistent SSL_SESSION, now that we
  929. * know we accepted it.
  930. * Clients make this copy when parsing the server's response to
  931. * the extension, which is when they find out that the negotiation
  932. * was successful.
  933. */
  934. if (s->server) {
  935. if (sent && ret == SSL_TLSEXT_ERR_OK && !s->hit) {
  936. /* Only store the hostname in the session if we accepted it. */
  937. OPENSSL_free(s->session->ext.hostname);
  938. s->session->ext.hostname = OPENSSL_strdup(s->ext.hostname);
  939. if (s->session->ext.hostname == NULL && s->ext.hostname != NULL) {
  940. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  941. }
  942. }
  943. }
  944. /*
  945. * If we switched contexts (whether here or in the client_hello callback),
  946. * move the sess_accept increment from the session_ctx to the new
  947. * context, to avoid the confusing situation of having sess_accept_good
  948. * exceed sess_accept (zero) for the new context.
  949. */
  950. if (SSL_IS_FIRST_HANDSHAKE(s) && sctx != s->session_ctx
  951. && s->hello_retry_request == SSL_HRR_NONE) {
  952. ssl_tsan_counter(sctx, &sctx->stats.sess_accept);
  953. ssl_tsan_decr(s->session_ctx, &s->session_ctx->stats.sess_accept);
  954. }
  955. /*
  956. * If we're expecting to send a ticket, and tickets were previously enabled,
  957. * and now tickets are disabled, then turn off expected ticket.
  958. * Also, if this is not a resumption, create a new session ID
  959. */
  960. if (ret == SSL_TLSEXT_ERR_OK && s->ext.ticket_expected
  961. && was_ticket && (SSL_get_options(ssl) & SSL_OP_NO_TICKET) != 0) {
  962. s->ext.ticket_expected = 0;
  963. if (!s->hit) {
  964. SSL_SESSION* ss = SSL_get_session(ssl);
  965. if (ss != NULL) {
  966. OPENSSL_free(ss->ext.tick);
  967. ss->ext.tick = NULL;
  968. ss->ext.ticklen = 0;
  969. ss->ext.tick_lifetime_hint = 0;
  970. ss->ext.tick_age_add = 0;
  971. if (!ssl_generate_session_id(s, ss)) {
  972. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  973. return 0;
  974. }
  975. } else {
  976. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  977. return 0;
  978. }
  979. }
  980. }
  981. switch (ret) {
  982. case SSL_TLSEXT_ERR_ALERT_FATAL:
  983. SSLfatal(s, altmp, SSL_R_CALLBACK_FAILED);
  984. return 0;
  985. case SSL_TLSEXT_ERR_ALERT_WARNING:
  986. /* (D)TLSv1.3 doesn't have warning alerts so we suppress this */
  987. if (!SSL_CONNECTION_IS_VERSION13(s))
  988. ssl3_send_alert(s, SSL3_AL_WARNING, altmp);
  989. s->servername_done = 0;
  990. return 1;
  991. case SSL_TLSEXT_ERR_NOACK:
  992. s->servername_done = 0;
  993. return 1;
  994. default:
  995. return 1;
  996. }
  997. }
  998. static int final_ec_pt_formats(SSL_CONNECTION *s, unsigned int context,
  999. int sent)
  1000. {
  1001. unsigned long alg_k, alg_a;
  1002. if (s->server)
  1003. return 1;
  1004. alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
  1005. alg_a = s->s3.tmp.new_cipher->algorithm_auth;
  1006. /*
  1007. * If we are client and using an elliptic curve cryptography cipher
  1008. * suite, then if server returns an EC point formats lists extension it
  1009. * must contain uncompressed.
  1010. */
  1011. if (s->ext.ecpointformats != NULL
  1012. && s->ext.ecpointformats_len > 0
  1013. && s->ext.peer_ecpointformats != NULL
  1014. && s->ext.peer_ecpointformats_len > 0
  1015. && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
  1016. /* we are using an ECC cipher */
  1017. size_t i;
  1018. unsigned char *list = s->ext.peer_ecpointformats;
  1019. for (i = 0; i < s->ext.peer_ecpointformats_len; i++) {
  1020. if (*list++ == TLSEXT_ECPOINTFORMAT_uncompressed)
  1021. break;
  1022. }
  1023. if (i == s->ext.peer_ecpointformats_len) {
  1024. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1025. SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
  1026. return 0;
  1027. }
  1028. }
  1029. return 1;
  1030. }
  1031. static int init_session_ticket(SSL_CONNECTION *s, unsigned int context)
  1032. {
  1033. if (!s->server)
  1034. s->ext.ticket_expected = 0;
  1035. return 1;
  1036. }
  1037. #ifndef OPENSSL_NO_OCSP
  1038. static int init_status_request(SSL_CONNECTION *s, unsigned int context)
  1039. {
  1040. if (s->server) {
  1041. s->ext.status_type = TLSEXT_STATUSTYPE_nothing;
  1042. } else {
  1043. /*
  1044. * Ensure we get sensible values passed to tlsext_status_cb in the event
  1045. * that we don't receive a status message
  1046. */
  1047. OPENSSL_free(s->ext.ocsp.resp);
  1048. s->ext.ocsp.resp = NULL;
  1049. s->ext.ocsp.resp_len = 0;
  1050. }
  1051. return 1;
  1052. }
  1053. #endif
  1054. #ifndef OPENSSL_NO_NEXTPROTONEG
  1055. static int init_npn(SSL_CONNECTION *s, unsigned int context)
  1056. {
  1057. s->s3.npn_seen = 0;
  1058. return 1;
  1059. }
  1060. #endif
  1061. static int init_alpn(SSL_CONNECTION *s, unsigned int context)
  1062. {
  1063. OPENSSL_free(s->s3.alpn_selected);
  1064. s->s3.alpn_selected = NULL;
  1065. s->s3.alpn_selected_len = 0;
  1066. if (s->server) {
  1067. OPENSSL_free(s->s3.alpn_proposed);
  1068. s->s3.alpn_proposed = NULL;
  1069. s->s3.alpn_proposed_len = 0;
  1070. }
  1071. return 1;
  1072. }
  1073. static int final_alpn(SSL_CONNECTION *s, unsigned int context, int sent)
  1074. {
  1075. if (!s->server && !sent && s->session->ext.alpn_selected != NULL)
  1076. s->ext.early_data_ok = 0;
  1077. if (!s->server || !SSL_CONNECTION_IS_VERSION13(s))
  1078. return 1;
  1079. /*
  1080. * Call alpn_select callback if needed. Has to be done after SNI and
  1081. * cipher negotiation (HTTP/2 restricts permitted ciphers). In (D)TLSv1.3
  1082. * we also have to do this before we decide whether to accept early_data.
  1083. * In (D)TLSv1.3 we've already negotiated our cipher so we do this call now.
  1084. * For < (D)TLSv1.3 we defer it until after cipher negotiation.
  1085. *
  1086. * On failure SSLfatal() already called.
  1087. */
  1088. return tls_handle_alpn(s);
  1089. }
  1090. static int init_sig_algs(SSL_CONNECTION *s, unsigned int context)
  1091. {
  1092. /* Clear any signature algorithms extension received */
  1093. OPENSSL_free(s->s3.tmp.peer_sigalgs);
  1094. s->s3.tmp.peer_sigalgs = NULL;
  1095. s->s3.tmp.peer_sigalgslen = 0;
  1096. return 1;
  1097. }
  1098. static int init_sig_algs_cert(SSL_CONNECTION *s,
  1099. ossl_unused unsigned int context)
  1100. {
  1101. /* Clear any signature algorithms extension received */
  1102. OPENSSL_free(s->s3.tmp.peer_cert_sigalgs);
  1103. s->s3.tmp.peer_cert_sigalgs = NULL;
  1104. s->s3.tmp.peer_cert_sigalgslen = 0;
  1105. return 1;
  1106. }
  1107. #ifndef OPENSSL_NO_SRP
  1108. static int init_srp(SSL_CONNECTION *s, unsigned int context)
  1109. {
  1110. OPENSSL_free(s->srp_ctx.login);
  1111. s->srp_ctx.login = NULL;
  1112. return 1;
  1113. }
  1114. #endif
  1115. static int init_ec_point_formats(SSL_CONNECTION *s, unsigned int context)
  1116. {
  1117. OPENSSL_free(s->ext.peer_ecpointformats);
  1118. s->ext.peer_ecpointformats = NULL;
  1119. s->ext.peer_ecpointformats_len = 0;
  1120. return 1;
  1121. }
  1122. static int init_etm(SSL_CONNECTION *s, unsigned int context)
  1123. {
  1124. s->ext.use_etm = 0;
  1125. return 1;
  1126. }
  1127. static int init_ems(SSL_CONNECTION *s, unsigned int context)
  1128. {
  1129. if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) {
  1130. s->s3.flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
  1131. s->s3.flags |= TLS1_FLAGS_REQUIRED_EXTMS;
  1132. }
  1133. return 1;
  1134. }
  1135. static int final_ems(SSL_CONNECTION *s, unsigned int context, int sent)
  1136. {
  1137. /*
  1138. * Check extended master secret extension is not dropped on
  1139. * renegotiation.
  1140. */
  1141. if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)
  1142. && (s->s3.flags & TLS1_FLAGS_REQUIRED_EXTMS)) {
  1143. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_EXTMS);
  1144. return 0;
  1145. }
  1146. if (!s->server && s->hit) {
  1147. /*
  1148. * Check extended master secret extension is consistent with
  1149. * original session.
  1150. */
  1151. if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
  1152. !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
  1153. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_EXTMS);
  1154. return 0;
  1155. }
  1156. }
  1157. return 1;
  1158. }
  1159. static int init_certificate_authorities(SSL_CONNECTION *s, unsigned int context)
  1160. {
  1161. sk_X509_NAME_pop_free(s->s3.tmp.peer_ca_names, X509_NAME_free);
  1162. s->s3.tmp.peer_ca_names = NULL;
  1163. return 1;
  1164. }
  1165. static EXT_RETURN tls_construct_certificate_authorities(SSL_CONNECTION *s,
  1166. WPACKET *pkt,
  1167. unsigned int context,
  1168. X509 *x,
  1169. size_t chainidx)
  1170. {
  1171. const STACK_OF(X509_NAME) *ca_sk = get_ca_names(s);
  1172. if (ca_sk == NULL || sk_X509_NAME_num(ca_sk) == 0)
  1173. return EXT_RETURN_NOT_SENT;
  1174. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_certificate_authorities)
  1175. || !WPACKET_start_sub_packet_u16(pkt)) {
  1176. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1177. return EXT_RETURN_FAIL;
  1178. }
  1179. if (!construct_ca_names(s, ca_sk, pkt)) {
  1180. /* SSLfatal() already called */
  1181. return EXT_RETURN_FAIL;
  1182. }
  1183. if (!WPACKET_close(pkt)) {
  1184. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1185. return EXT_RETURN_FAIL;
  1186. }
  1187. return EXT_RETURN_SENT;
  1188. }
  1189. static int tls_parse_certificate_authorities(SSL_CONNECTION *s, PACKET *pkt,
  1190. unsigned int context, X509 *x,
  1191. size_t chainidx)
  1192. {
  1193. if (!parse_ca_names(s, pkt))
  1194. return 0;
  1195. if (PACKET_remaining(pkt) != 0) {
  1196. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
  1197. return 0;
  1198. }
  1199. return 1;
  1200. }
  1201. #ifndef OPENSSL_NO_SRTP
  1202. static int init_srtp(SSL_CONNECTION *s, unsigned int context)
  1203. {
  1204. if (s->server)
  1205. s->srtp_profile = NULL;
  1206. return 1;
  1207. }
  1208. #endif
  1209. static int final_sig_algs(SSL_CONNECTION *s, unsigned int context, int sent)
  1210. {
  1211. if (!sent && SSL_CONNECTION_IS_VERSION13(s) && !s->hit) {
  1212. SSLfatal(s, TLS13_AD_MISSING_EXTENSION,
  1213. SSL_R_MISSING_SIGALGS_EXTENSION);
  1214. return 0;
  1215. }
  1216. return 1;
  1217. }
  1218. static int final_key_share(SSL_CONNECTION *s, unsigned int context, int sent)
  1219. {
  1220. #if !(defined(OPENSSL_NO_TLS1_3) && defined(OPENSSL_NO_DTLS1_3))
  1221. if (!SSL_CONNECTION_IS_VERSION13(s))
  1222. return 1;
  1223. /* Nothing to do for key_share in an HRR */
  1224. if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0)
  1225. return 1;
  1226. /*
  1227. * If
  1228. * we are a client
  1229. * AND
  1230. * we have no key_share
  1231. * AND
  1232. * (we are not resuming
  1233. * OR the kex_mode doesn't allow non key_share resumes)
  1234. * THEN
  1235. * fail;
  1236. */
  1237. if (!s->server
  1238. && !sent
  1239. && (!s->hit
  1240. || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0)) {
  1241. /* Nothing left we can do - just fail */
  1242. SSLfatal(s, SSL_AD_MISSING_EXTENSION, SSL_R_NO_SUITABLE_KEY_SHARE);
  1243. return 0;
  1244. }
  1245. /*
  1246. * IF
  1247. * we are a server
  1248. * THEN
  1249. * IF
  1250. * we have a suitable key_share
  1251. * THEN
  1252. * IF
  1253. * we are stateless AND we have no cookie
  1254. * THEN
  1255. * send a HelloRetryRequest
  1256. * ELSE
  1257. * IF
  1258. * we didn't already send a HelloRetryRequest
  1259. * AND
  1260. * the client sent a key_share extension
  1261. * AND
  1262. * (we are not resuming
  1263. * OR the kex_mode allows key_share resumes)
  1264. * AND
  1265. * a shared group exists
  1266. * THEN
  1267. * send a HelloRetryRequest
  1268. * ELSE IF
  1269. * we are not resuming
  1270. * OR
  1271. * the kex_mode doesn't allow non key_share resumes
  1272. * THEN
  1273. * fail
  1274. * ELSE IF
  1275. * we are stateless AND we have no cookie
  1276. * THEN
  1277. * send a HelloRetryRequest
  1278. */
  1279. if (s->server) {
  1280. if (s->s3.peer_tmp != NULL) {
  1281. /* We have a suitable key_share */
  1282. if ((s->s3.flags & TLS1_FLAGS_STATELESS) != 0
  1283. && !s->ext.cookieok) {
  1284. if (!ossl_assert(s->hello_retry_request == SSL_HRR_NONE)) {
  1285. /*
  1286. * If we are stateless then we wouldn't know about any
  1287. * previously sent HRR - so how can this be anything other
  1288. * than 0?
  1289. */
  1290. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1291. return 0;
  1292. }
  1293. s->hello_retry_request = SSL_HRR_PENDING;
  1294. return 1;
  1295. }
  1296. } else {
  1297. /* No suitable key_share */
  1298. if (s->hello_retry_request == SSL_HRR_NONE && sent
  1299. && (!s->hit
  1300. || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE)
  1301. != 0)) {
  1302. const uint16_t *pgroups, *clntgroups;
  1303. size_t num_groups, clnt_num_groups, i;
  1304. unsigned int group_id = 0;
  1305. /* Check if a shared group exists */
  1306. /* Get the clients list of supported groups. */
  1307. tls1_get_peer_groups(s, &clntgroups, &clnt_num_groups);
  1308. tls1_get_supported_groups(s, &pgroups, &num_groups);
  1309. /*
  1310. * Find the first group we allow that is also in client's list
  1311. */
  1312. for (i = 0; i < num_groups; i++) {
  1313. int version;
  1314. group_id = pgroups[i];
  1315. version = SSL_CONNECTION_IS_DTLS(s) ?
  1316. DTLS1_3_VERSION : TLS1_3_VERSION;
  1317. if (check_in_list(s, group_id, clntgroups, clnt_num_groups,
  1318. 1)
  1319. && tls_group_allowed(s, group_id,
  1320. SSL_SECOP_CURVE_SUPPORTED)
  1321. && tls_valid_group(s, group_id, version,
  1322. version, 0, NULL))
  1323. break;
  1324. }
  1325. if (i < num_groups) {
  1326. /* A shared group exists so send a HelloRetryRequest */
  1327. s->s3.group_id = group_id;
  1328. s->hello_retry_request = SSL_HRR_PENDING;
  1329. return 1;
  1330. }
  1331. }
  1332. if (!s->hit
  1333. || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0) {
  1334. /* Nothing left we can do - just fail */
  1335. SSLfatal(s, sent ? SSL_AD_HANDSHAKE_FAILURE
  1336. : SSL_AD_MISSING_EXTENSION,
  1337. SSL_R_NO_SUITABLE_KEY_SHARE);
  1338. return 0;
  1339. }
  1340. if ((s->s3.flags & TLS1_FLAGS_STATELESS) != 0
  1341. && !s->ext.cookieok) {
  1342. if (!ossl_assert(s->hello_retry_request == SSL_HRR_NONE)) {
  1343. /*
  1344. * If we are stateless then we wouldn't know about any
  1345. * previously sent HRR - so how can this be anything other
  1346. * than 0?
  1347. */
  1348. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1349. return 0;
  1350. }
  1351. s->hello_retry_request = SSL_HRR_PENDING;
  1352. return 1;
  1353. }
  1354. }
  1355. /*
  1356. * We have a key_share so don't send any more HelloRetryRequest
  1357. * messages
  1358. */
  1359. if (s->hello_retry_request == SSL_HRR_PENDING)
  1360. s->hello_retry_request = SSL_HRR_COMPLETE;
  1361. } else {
  1362. /*
  1363. * For a client side resumption with no key_share we need to generate
  1364. * the handshake secret (otherwise this is done during key_share
  1365. * processing).
  1366. */
  1367. if (!sent && !tls13_generate_handshake_secret(s, NULL, 0)) {
  1368. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1369. return 0;
  1370. }
  1371. }
  1372. #endif /* !defined(OPENSSL_NO_TLS1_3) && !defined(OPENSSL_NO_DTLS1_3) */
  1373. return 1;
  1374. }
  1375. static int init_psk_kex_modes(SSL_CONNECTION *s, unsigned int context)
  1376. {
  1377. s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_NONE;
  1378. return 1;
  1379. }
  1380. int tls_psk_do_binder(SSL_CONNECTION *s, const EVP_MD *md,
  1381. const unsigned char *msgstart,
  1382. size_t binderoffset, const unsigned char *binderin,
  1383. unsigned char *binderout, SSL_SESSION *sess, int sign,
  1384. int external)
  1385. {
  1386. EVP_PKEY *mackey = NULL;
  1387. EVP_MD_CTX *mctx = NULL;
  1388. unsigned char hash[EVP_MAX_MD_SIZE], binderkey[EVP_MAX_MD_SIZE];
  1389. unsigned char finishedkey[EVP_MAX_MD_SIZE], tmpbinder[EVP_MAX_MD_SIZE];
  1390. unsigned char *early_secret;
  1391. /* ASCII: "res binder", in hex for EBCDIC compatibility */
  1392. static const unsigned char resumption_label[] = "\x72\x65\x73\x20\x62\x69\x6E\x64\x65\x72";
  1393. /* ASCII: "ext binder", in hex for EBCDIC compatibility */
  1394. static const unsigned char external_label[] = "\x65\x78\x74\x20\x62\x69\x6E\x64\x65\x72";
  1395. const unsigned char *label;
  1396. size_t bindersize, labelsize, hashsize;
  1397. int hashsizei = EVP_MD_get_size(md);
  1398. int ret = -1;
  1399. int usepskfored = 0;
  1400. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  1401. /* Ensure cast to size_t is safe */
  1402. if (!ossl_assert(hashsizei >= 0)) {
  1403. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1404. goto err;
  1405. }
  1406. hashsize = (size_t)hashsizei;
  1407. if (external
  1408. && s->early_data_state == SSL_EARLY_DATA_CONNECTING
  1409. && s->session->ext.max_early_data == 0
  1410. && sess->ext.max_early_data > 0)
  1411. usepskfored = 1;
  1412. if (external) {
  1413. label = external_label;
  1414. labelsize = sizeof(external_label) - 1;
  1415. } else {
  1416. label = resumption_label;
  1417. labelsize = sizeof(resumption_label) - 1;
  1418. }
  1419. /*
  1420. * Generate the early_secret. On the server side we've selected a PSK to
  1421. * resume with (internal or external) so we always do this. On the client
  1422. * side we do this for a non-external (i.e. resumption) PSK or external PSK
  1423. * that will be used for early_data so that it is in place for sending early
  1424. * data. For client side external PSK not being used for early_data we
  1425. * generate it but store it away for later use.
  1426. */
  1427. if (s->server || !external || usepskfored)
  1428. early_secret = (unsigned char *)s->early_secret;
  1429. else
  1430. early_secret = (unsigned char *)sess->early_secret;
  1431. if (!tls13_generate_secret(s, md, NULL, sess->master_key,
  1432. sess->master_key_length, early_secret)) {
  1433. /* SSLfatal() already called */
  1434. goto err;
  1435. }
  1436. /*
  1437. * Create the handshake hash for the binder key...the messages so far are
  1438. * empty!
  1439. */
  1440. mctx = EVP_MD_CTX_new();
  1441. if (mctx == NULL
  1442. || EVP_DigestInit_ex(mctx, md, NULL) <= 0
  1443. || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
  1444. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1445. goto err;
  1446. }
  1447. /* Generate the binder key */
  1448. if (!tls13_hkdf_expand(s, md, early_secret, label, labelsize, hash,
  1449. hashsize, binderkey, hashsize, 1)) {
  1450. /* SSLfatal() already called */
  1451. goto err;
  1452. }
  1453. /* Generate the finished key */
  1454. if (!tls13_derive_finishedkey(s, md, binderkey, finishedkey, hashsize)) {
  1455. /* SSLfatal() already called */
  1456. goto err;
  1457. }
  1458. if (EVP_DigestInit_ex(mctx, md, NULL) <= 0) {
  1459. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1460. goto err;
  1461. }
  1462. /*
  1463. * Get a hash of the ClientHello up to the start of the binders. If we are
  1464. * following a HelloRetryRequest then this includes the hash of the first
  1465. * ClientHello and the HelloRetryRequest itself.
  1466. */
  1467. if (s->hello_retry_request == SSL_HRR_PENDING) {
  1468. size_t hdatalen;
  1469. long hdatalen_l;
  1470. void *hdata;
  1471. hdatalen = hdatalen_l =
  1472. BIO_get_mem_data(s->s3.handshake_buffer, &hdata);
  1473. if (hdatalen_l <= 0) {
  1474. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_LENGTH);
  1475. goto err;
  1476. }
  1477. /*
  1478. * For servers the handshake buffer data will include the second
  1479. * ClientHello - which we don't want - so we need to take that bit off.
  1480. */
  1481. if (s->server) {
  1482. PACKET hashprefix, msg;
  1483. /* Find how many bytes are left after the first two messages */
  1484. if (!PACKET_buf_init(&hashprefix, hdata, hdatalen)
  1485. || !PACKET_forward(&hashprefix, 1)
  1486. || !PACKET_get_length_prefixed_3(&hashprefix, &msg)
  1487. || !PACKET_forward(&hashprefix, 1)
  1488. || !PACKET_get_length_prefixed_3(&hashprefix, &msg)) {
  1489. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1490. goto err;
  1491. }
  1492. hdatalen -= PACKET_remaining(&hashprefix);
  1493. }
  1494. if (EVP_DigestUpdate(mctx, hdata, hdatalen) <= 0) {
  1495. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1496. goto err;
  1497. }
  1498. }
  1499. if (EVP_DigestUpdate(mctx, msgstart, binderoffset) <= 0
  1500. || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
  1501. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1502. goto err;
  1503. }
  1504. mackey = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC",
  1505. sctx->propq, finishedkey,
  1506. hashsize);
  1507. if (mackey == NULL) {
  1508. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1509. goto err;
  1510. }
  1511. if (!sign)
  1512. binderout = tmpbinder;
  1513. bindersize = hashsize;
  1514. if (EVP_DigestSignInit_ex(mctx, NULL, EVP_MD_get0_name(md), sctx->libctx,
  1515. sctx->propq, mackey, NULL) <= 0
  1516. || EVP_DigestSignUpdate(mctx, hash, hashsize) <= 0
  1517. || EVP_DigestSignFinal(mctx, binderout, &bindersize) <= 0
  1518. || bindersize != hashsize) {
  1519. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1520. goto err;
  1521. }
  1522. if (sign) {
  1523. ret = 1;
  1524. } else {
  1525. /* HMAC keys can't do EVP_DigestVerify* - use CRYPTO_memcmp instead */
  1526. ret = (CRYPTO_memcmp(binderin, binderout, hashsize) == 0);
  1527. if (!ret)
  1528. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BINDER_DOES_NOT_VERIFY);
  1529. }
  1530. err:
  1531. OPENSSL_cleanse(binderkey, sizeof(binderkey));
  1532. OPENSSL_cleanse(finishedkey, sizeof(finishedkey));
  1533. EVP_PKEY_free(mackey);
  1534. EVP_MD_CTX_free(mctx);
  1535. return ret;
  1536. }
  1537. static int final_early_data(SSL_CONNECTION *s, unsigned int context, int sent)
  1538. {
  1539. if (!sent)
  1540. return 1;
  1541. if (!s->server) {
  1542. if (context == SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
  1543. && sent
  1544. && !s->ext.early_data_ok) {
  1545. /*
  1546. * If we get here then the server accepted our early_data but we
  1547. * later realised that it shouldn't have done (e.g. inconsistent
  1548. * ALPN)
  1549. */
  1550. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EARLY_DATA);
  1551. return 0;
  1552. }
  1553. return 1;
  1554. }
  1555. if (s->max_early_data == 0
  1556. || !s->hit
  1557. || s->early_data_state != SSL_EARLY_DATA_ACCEPTING
  1558. || !s->ext.early_data_ok
  1559. || s->hello_retry_request != SSL_HRR_NONE
  1560. || (s->allow_early_data_cb != NULL
  1561. && !s->allow_early_data_cb(SSL_CONNECTION_GET_SSL(s),
  1562. s->allow_early_data_cb_data))) {
  1563. s->ext.early_data = SSL_EARLY_DATA_REJECTED;
  1564. } else {
  1565. s->ext.early_data = SSL_EARLY_DATA_ACCEPTED;
  1566. if (!tls13_change_cipher_state(s,
  1567. SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_SERVER_READ)) {
  1568. /* SSLfatal() already called */
  1569. return 0;
  1570. }
  1571. }
  1572. return 1;
  1573. }
  1574. static int final_maxfragmentlen(SSL_CONNECTION *s, unsigned int context,
  1575. int sent)
  1576. {
  1577. /*
  1578. * Session resumption on server-side with MFL extension active
  1579. * BUT MFL extension packet was not resent (i.e. sent == 0)
  1580. */
  1581. if (s->server && s->hit && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)
  1582. && !sent ) {
  1583. SSLfatal(s, SSL_AD_MISSING_EXTENSION, SSL_R_BAD_EXTENSION);
  1584. return 0;
  1585. }
  1586. if (s->session && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)) {
  1587. s->rlayer.rrlmethod->set_max_frag_len(s->rlayer.rrl,
  1588. GET_MAX_FRAGMENT_LENGTH(s->session));
  1589. s->rlayer.wrlmethod->set_max_frag_len(s->rlayer.wrl,
  1590. ssl_get_max_send_fragment(s));
  1591. }
  1592. return 1;
  1593. }
  1594. static int init_post_handshake_auth(SSL_CONNECTION *s,
  1595. ossl_unused unsigned int context)
  1596. {
  1597. s->post_handshake_auth = SSL_PHA_NONE;
  1598. return 1;
  1599. }
  1600. /*
  1601. * If clients offer "pre_shared_key" without a "psk_key_exchange_modes"
  1602. * extension, servers MUST abort the handshake.
  1603. */
  1604. static int final_psk(SSL_CONNECTION *s, unsigned int context, int sent)
  1605. {
  1606. if (s->server && sent && s->clienthello != NULL
  1607. && !s->clienthello->pre_proc_exts[TLSEXT_IDX_psk_kex_modes].present) {
  1608. SSLfatal(s, TLS13_AD_MISSING_EXTENSION,
  1609. SSL_R_MISSING_PSK_KEX_MODES_EXTENSION);
  1610. return 0;
  1611. }
  1612. return 1;
  1613. }
  1614. static int tls_init_compress_certificate(SSL_CONNECTION *sc, unsigned int context)
  1615. {
  1616. memset(sc->ext.compress_certificate_from_peer, 0,
  1617. sizeof(sc->ext.compress_certificate_from_peer));
  1618. return 1;
  1619. }
  1620. /* The order these are put into the packet imply a preference order: [brotli, zlib, zstd] */
  1621. static EXT_RETURN tls_construct_compress_certificate(SSL_CONNECTION *sc, WPACKET *pkt,
  1622. unsigned int context,
  1623. X509 *x, size_t chainidx)
  1624. {
  1625. #ifndef OPENSSL_NO_COMP_ALG
  1626. int i;
  1627. if (!ossl_comp_has_alg(0))
  1628. return EXT_RETURN_NOT_SENT;
  1629. /* Server: Don't attempt to compress a non-X509 (i.e. an RPK) */
  1630. if (sc->server && sc->ext.server_cert_type != TLSEXT_cert_type_x509) {
  1631. sc->cert_comp_prefs[0] = TLSEXT_comp_cert_none;
  1632. return EXT_RETURN_NOT_SENT;
  1633. }
  1634. /* Client: If we sent a client cert-type extension, don't indicate compression */
  1635. if (!sc->server && sc->ext.client_cert_type_ctos) {
  1636. sc->cert_comp_prefs[0] = TLSEXT_comp_cert_none;
  1637. return EXT_RETURN_NOT_SENT;
  1638. }
  1639. /* Do not indicate we support receiving compressed certificates */
  1640. if ((sc->options & SSL_OP_NO_RX_CERTIFICATE_COMPRESSION) != 0)
  1641. return EXT_RETURN_NOT_SENT;
  1642. if (sc->cert_comp_prefs[0] == TLSEXT_comp_cert_none)
  1643. return EXT_RETURN_NOT_SENT;
  1644. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_compress_certificate)
  1645. || !WPACKET_start_sub_packet_u16(pkt)
  1646. || !WPACKET_start_sub_packet_u8(pkt))
  1647. goto err;
  1648. for (i = 0; sc->cert_comp_prefs[i] != TLSEXT_comp_cert_none; i++) {
  1649. if (!WPACKET_put_bytes_u16(pkt, sc->cert_comp_prefs[i]))
  1650. goto err;
  1651. }
  1652. if (!WPACKET_close(pkt) || !WPACKET_close(pkt))
  1653. goto err;
  1654. sc->ext.compress_certificate_sent = 1;
  1655. return EXT_RETURN_SENT;
  1656. err:
  1657. SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1658. return EXT_RETURN_FAIL;
  1659. #else
  1660. return EXT_RETURN_NOT_SENT;
  1661. #endif
  1662. }
  1663. #ifndef OPENSSL_NO_COMP_ALG
  1664. static int tls_comp_in_pref(SSL_CONNECTION *sc, int alg)
  1665. {
  1666. int i;
  1667. /* ossl_comp_has_alg() considers 0 as "any" */
  1668. if (alg == 0)
  1669. return 0;
  1670. /* Make sure algorithm is enabled */
  1671. if (!ossl_comp_has_alg(alg))
  1672. return 0;
  1673. /* If no preferences are set, it's ok */
  1674. if (sc->cert_comp_prefs[0] == TLSEXT_comp_cert_none)
  1675. return 1;
  1676. /* Find the algorithm */
  1677. for (i = 0; i < TLSEXT_comp_cert_limit; i++)
  1678. if (sc->cert_comp_prefs[i] == alg)
  1679. return 1;
  1680. return 0;
  1681. }
  1682. #endif
  1683. int tls_parse_compress_certificate(SSL_CONNECTION *sc, PACKET *pkt, unsigned int context,
  1684. X509 *x, size_t chainidx)
  1685. {
  1686. #ifndef OPENSSL_NO_COMP_ALG
  1687. PACKET supported_comp_algs;
  1688. unsigned int comp;
  1689. int already_set[TLSEXT_comp_cert_limit];
  1690. int j = 0;
  1691. /* If no algorithms are available, ignore the extension */
  1692. if (!ossl_comp_has_alg(0))
  1693. return 1;
  1694. /* Don't attempt to compress a non-X509 (i.e. an RPK) */
  1695. if (sc->server && sc->ext.server_cert_type != TLSEXT_cert_type_x509)
  1696. return 1;
  1697. if (!sc->server && sc->ext.client_cert_type != TLSEXT_cert_type_x509)
  1698. return 1;
  1699. /* Ignore the extension and don't send compressed certificates */
  1700. if ((sc->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0)
  1701. return 1;
  1702. if (!PACKET_as_length_prefixed_1(pkt, &supported_comp_algs)
  1703. || PACKET_remaining(&supported_comp_algs) == 0) {
  1704. SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
  1705. return 0;
  1706. }
  1707. memset(already_set, 0, sizeof(already_set));
  1708. /*
  1709. * The preference array has real values, so take a look at each
  1710. * value coming in, and make sure it's in our preference list
  1711. * The array is 0 (i.e. "none") terminated
  1712. * The preference list only contains supported algorithms
  1713. */
  1714. while (PACKET_get_net_2(&supported_comp_algs, &comp)) {
  1715. if (tls_comp_in_pref(sc, comp) && !already_set[comp]) {
  1716. sc->ext.compress_certificate_from_peer[j++] = comp;
  1717. already_set[comp] = 1;
  1718. }
  1719. }
  1720. #endif
  1721. return 1;
  1722. }
  1723. static int init_server_cert_type(SSL_CONNECTION *sc, unsigned int context)
  1724. {
  1725. /* Only reset when parsing client hello */
  1726. if (sc->server) {
  1727. sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
  1728. sc->ext.server_cert_type = TLSEXT_cert_type_x509;
  1729. }
  1730. return 1;
  1731. }
  1732. static int init_client_cert_type(SSL_CONNECTION *sc, unsigned int context)
  1733. {
  1734. /* Only reset when parsing client hello */
  1735. if (sc->server) {
  1736. sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
  1737. sc->ext.client_cert_type = TLSEXT_cert_type_x509;
  1738. }
  1739. return 1;
  1740. }