extensions_clnt.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  1. /*
  2. * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <assert.h>
  10. #include <openssl/ocsp.h>
  11. #include "../ssl_locl.h"
  12. #include "statem_locl.h"
  13. int tls_construct_ctos_renegotiate(SSL *s, WPACKET *pkt, unsigned int context,
  14. X509 *x, size_t chainidx, int *al)
  15. {
  16. /* Add RI if renegotiating */
  17. if (!s->renegotiate)
  18. return 1;
  19. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
  20. || !WPACKET_start_sub_packet_u16(pkt)
  21. || !WPACKET_sub_memcpy_u8(pkt, s->s3->previous_client_finished,
  22. s->s3->previous_client_finished_len)
  23. || !WPACKET_close(pkt)) {
  24. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_RENEGOTIATE, ERR_R_INTERNAL_ERROR);
  25. return 0;
  26. }
  27. return 1;
  28. }
  29. int tls_construct_ctos_server_name(SSL *s, WPACKET *pkt, unsigned int context,
  30. X509 *x, size_t chainidx, int *al)
  31. {
  32. if (s->ext.hostname == NULL)
  33. return 1;
  34. /* Add TLS extension servername to the Client Hello message */
  35. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
  36. /* Sub-packet for server_name extension */
  37. || !WPACKET_start_sub_packet_u16(pkt)
  38. /* Sub-packet for servername list (always 1 hostname)*/
  39. || !WPACKET_start_sub_packet_u16(pkt)
  40. || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name)
  41. || !WPACKET_sub_memcpy_u16(pkt, s->ext.hostname,
  42. strlen(s->ext.hostname))
  43. || !WPACKET_close(pkt)
  44. || !WPACKET_close(pkt)) {
  45. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SERVER_NAME, ERR_R_INTERNAL_ERROR);
  46. return 0;
  47. }
  48. return 1;
  49. }
  50. #ifndef OPENSSL_NO_SRP
  51. int tls_construct_ctos_srp(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  52. size_t chainidx, int *al)
  53. {
  54. /* Add SRP username if there is one */
  55. if (s->srp_ctx.login == NULL)
  56. return 1;
  57. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp)
  58. /* Sub-packet for SRP extension */
  59. || !WPACKET_start_sub_packet_u16(pkt)
  60. || !WPACKET_start_sub_packet_u8(pkt)
  61. /* login must not be zero...internal error if so */
  62. || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
  63. || !WPACKET_memcpy(pkt, s->srp_ctx.login,
  64. strlen(s->srp_ctx.login))
  65. || !WPACKET_close(pkt)
  66. || !WPACKET_close(pkt)) {
  67. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SRP, ERR_R_INTERNAL_ERROR);
  68. return 0;
  69. }
  70. return 1;
  71. }
  72. #endif
  73. #ifndef OPENSSL_NO_EC
  74. static int use_ecc(SSL *s)
  75. {
  76. int i, end;
  77. unsigned long alg_k, alg_a;
  78. STACK_OF(SSL_CIPHER) *cipher_stack = NULL;
  79. /* See if we support any ECC ciphersuites */
  80. if (s->version == SSL3_VERSION)
  81. return 0;
  82. cipher_stack = SSL_get_ciphers(s);
  83. end = sk_SSL_CIPHER_num(cipher_stack);
  84. for (i = 0; i < end; i++) {
  85. const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
  86. alg_k = c->algorithm_mkey;
  87. alg_a = c->algorithm_auth;
  88. if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))
  89. || (alg_a & SSL_aECDSA)
  90. || c->min_tls >= TLS1_3_VERSION)
  91. return 1;
  92. }
  93. return 0;
  94. }
  95. int tls_construct_ctos_ec_pt_formats(SSL *s, WPACKET *pkt, unsigned int context,
  96. X509 *x, size_t chainidx, int *al)
  97. {
  98. const unsigned char *pformats;
  99. size_t num_formats;
  100. if (!use_ecc(s))
  101. return 1;
  102. /* Add TLS extension ECPointFormats to the ClientHello message */
  103. tls1_get_formatlist(s, &pformats, &num_formats);
  104. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
  105. /* Sub-packet for formats extension */
  106. || !WPACKET_start_sub_packet_u16(pkt)
  107. || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats)
  108. || !WPACKET_close(pkt)) {
  109. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
  110. return 0;
  111. }
  112. return 1;
  113. }
  114. int tls_construct_ctos_supported_groups(SSL *s, WPACKET *pkt,
  115. unsigned int context, X509 *x,
  116. size_t chainidx, int *al)
  117. {
  118. const unsigned char *pcurves = NULL, *pcurvestmp;
  119. size_t num_curves = 0, i;
  120. if (!use_ecc(s))
  121. return 1;
  122. /*
  123. * Add TLS extension supported_groups to the ClientHello message
  124. */
  125. /* TODO(TLS1.3): Add support for DHE groups */
  126. if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) {
  127. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,
  128. ERR_R_INTERNAL_ERROR);
  129. return 0;
  130. }
  131. pcurvestmp = pcurves;
  132. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)
  133. /* Sub-packet for supported_groups extension */
  134. || !WPACKET_start_sub_packet_u16(pkt)
  135. || !WPACKET_start_sub_packet_u16(pkt)) {
  136. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,
  137. ERR_R_INTERNAL_ERROR);
  138. return 0;
  139. }
  140. /* Copy curve ID if supported */
  141. for (i = 0; i < num_curves; i++, pcurvestmp += 2) {
  142. if (tls_curve_allowed(s, pcurvestmp, SSL_SECOP_CURVE_SUPPORTED)) {
  143. if (!WPACKET_put_bytes_u8(pkt, pcurvestmp[0])
  144. || !WPACKET_put_bytes_u8(pkt, pcurvestmp[1])) {
  145. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,
  146. ERR_R_INTERNAL_ERROR);
  147. return 0;
  148. }
  149. }
  150. }
  151. if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
  152. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,
  153. ERR_R_INTERNAL_ERROR);
  154. return 0;
  155. }
  156. return 1;
  157. }
  158. #endif
  159. int tls_construct_ctos_session_ticket(SSL *s, WPACKET *pkt,
  160. unsigned int context, X509 *x,
  161. size_t chainidx, int *al)
  162. {
  163. size_t ticklen;
  164. if (!tls_use_ticket(s))
  165. return 1;
  166. if (!s->new_session && s->session != NULL
  167. && s->session->ext.tick != NULL
  168. && s->session->ssl_version != TLS1_3_VERSION) {
  169. ticklen = s->session->ext.ticklen;
  170. } else if (s->session && s->ext.session_ticket != NULL
  171. && s->ext.session_ticket->data != NULL) {
  172. ticklen = s->ext.session_ticket->length;
  173. s->session->ext.tick = OPENSSL_malloc(ticklen);
  174. if (s->session->ext.tick == NULL) {
  175. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SESSION_TICKET,
  176. ERR_R_INTERNAL_ERROR);
  177. return 0;
  178. }
  179. memcpy(s->session->ext.tick,
  180. s->ext.session_ticket->data, ticklen);
  181. s->session->ext.ticklen = ticklen;
  182. } else {
  183. ticklen = 0;
  184. }
  185. if (ticklen == 0 && s->ext.session_ticket != NULL &&
  186. s->ext.session_ticket->data == NULL)
  187. return 1;
  188. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
  189. || !WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick, ticklen)) {
  190. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
  191. return 0;
  192. }
  193. return 1;
  194. }
  195. int tls_construct_ctos_sig_algs(SSL *s, WPACKET *pkt, unsigned int context,
  196. X509 *x, size_t chainidx, int *al)
  197. {
  198. size_t salglen;
  199. const uint16_t *salg;
  200. if (!SSL_CLIENT_USE_SIGALGS(s))
  201. return 1;
  202. salglen = tls12_get_psigalgs(s, 1, &salg);
  203. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms)
  204. /* Sub-packet for sig-algs extension */
  205. || !WPACKET_start_sub_packet_u16(pkt)
  206. /* Sub-packet for the actual list */
  207. || !WPACKET_start_sub_packet_u16(pkt)
  208. || !tls12_copy_sigalgs(s, pkt, salg, salglen)
  209. || !WPACKET_close(pkt)
  210. || !WPACKET_close(pkt)) {
  211. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SIG_ALGS, ERR_R_INTERNAL_ERROR);
  212. return 0;
  213. }
  214. return 1;
  215. }
  216. #ifndef OPENSSL_NO_OCSP
  217. int tls_construct_ctos_status_request(SSL *s, WPACKET *pkt,
  218. unsigned int context, X509 *x,
  219. size_t chainidx, int *al)
  220. {
  221. int i;
  222. /* This extension isn't defined for client Certificates */
  223. if (x != NULL)
  224. return 1;
  225. if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp)
  226. return 1;
  227. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
  228. /* Sub-packet for status request extension */
  229. || !WPACKET_start_sub_packet_u16(pkt)
  230. || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp)
  231. /* Sub-packet for the ids */
  232. || !WPACKET_start_sub_packet_u16(pkt)) {
  233. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
  234. return 0;
  235. }
  236. for (i = 0; i < sk_OCSP_RESPID_num(s->ext.ocsp.ids); i++) {
  237. unsigned char *idbytes;
  238. OCSP_RESPID *id = sk_OCSP_RESPID_value(s->ext.ocsp.ids, i);
  239. int idlen = i2d_OCSP_RESPID(id, NULL);
  240. if (idlen <= 0
  241. /* Sub-packet for an individual id */
  242. || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes)
  243. || i2d_OCSP_RESPID(id, &idbytes) != idlen) {
  244. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST,
  245. ERR_R_INTERNAL_ERROR);
  246. return 0;
  247. }
  248. }
  249. if (!WPACKET_close(pkt)
  250. || !WPACKET_start_sub_packet_u16(pkt)) {
  251. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
  252. return 0;
  253. }
  254. if (s->ext.ocsp.exts) {
  255. unsigned char *extbytes;
  256. int extlen = i2d_X509_EXTENSIONS(s->ext.ocsp.exts, NULL);
  257. if (extlen < 0) {
  258. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST,
  259. ERR_R_INTERNAL_ERROR);
  260. return 0;
  261. }
  262. if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes)
  263. || i2d_X509_EXTENSIONS(s->ext.ocsp.exts, &extbytes)
  264. != extlen) {
  265. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST,
  266. ERR_R_INTERNAL_ERROR);
  267. return 0;
  268. }
  269. }
  270. if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
  271. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
  272. return 0;
  273. }
  274. return 1;
  275. }
  276. #endif
  277. #ifndef OPENSSL_NO_NEXTPROTONEG
  278. int tls_construct_ctos_npn(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  279. size_t chainidx, int *al)
  280. {
  281. if (s->ctx->ext.npn_select_cb == NULL || !SSL_IS_FIRST_HANDSHAKE(s))
  282. return 1;
  283. /*
  284. * The client advertises an empty extension to indicate its support
  285. * for Next Protocol Negotiation
  286. */
  287. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
  288. || !WPACKET_put_bytes_u16(pkt, 0)) {
  289. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_NPN, ERR_R_INTERNAL_ERROR);
  290. return 0;
  291. }
  292. return 1;
  293. }
  294. #endif
  295. int tls_construct_ctos_alpn(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  296. size_t chainidx, int *al)
  297. {
  298. s->s3->alpn_sent = 0;
  299. if (s->ext.alpn == NULL || !SSL_IS_FIRST_HANDSHAKE(s))
  300. return 1;
  301. if (!WPACKET_put_bytes_u16(pkt,
  302. TLSEXT_TYPE_application_layer_protocol_negotiation)
  303. /* Sub-packet ALPN extension */
  304. || !WPACKET_start_sub_packet_u16(pkt)
  305. || !WPACKET_sub_memcpy_u16(pkt, s->ext.alpn, s->ext.alpn_len)
  306. || !WPACKET_close(pkt)) {
  307. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_ALPN, ERR_R_INTERNAL_ERROR);
  308. return 0;
  309. }
  310. s->s3->alpn_sent = 1;
  311. return 1;
  312. }
  313. #ifndef OPENSSL_NO_SRTP
  314. int tls_construct_ctos_use_srtp(SSL *s, WPACKET *pkt, unsigned int context,
  315. X509 *x, size_t chainidx, int *al)
  316. {
  317. STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = SSL_get_srtp_profiles(s);
  318. int i, end;
  319. if (clnt == NULL)
  320. return 1;
  321. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
  322. /* Sub-packet for SRTP extension */
  323. || !WPACKET_start_sub_packet_u16(pkt)
  324. /* Sub-packet for the protection profile list */
  325. || !WPACKET_start_sub_packet_u16(pkt)) {
  326. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_USE_SRTP, ERR_R_INTERNAL_ERROR);
  327. return 0;
  328. }
  329. end = sk_SRTP_PROTECTION_PROFILE_num(clnt);
  330. for (i = 0; i < end; i++) {
  331. const SRTP_PROTECTION_PROFILE *prof =
  332. sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
  333. if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) {
  334. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_USE_SRTP, ERR_R_INTERNAL_ERROR);
  335. return 0;
  336. }
  337. }
  338. if (!WPACKET_close(pkt)
  339. /* Add an empty use_mki value */
  340. || !WPACKET_put_bytes_u8(pkt, 0)
  341. || !WPACKET_close(pkt)) {
  342. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_USE_SRTP, ERR_R_INTERNAL_ERROR);
  343. return 0;
  344. }
  345. return 1;
  346. }
  347. #endif
  348. int tls_construct_ctos_etm(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  349. size_t chainidx, int *al)
  350. {
  351. if (s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
  352. return 1;
  353. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
  354. || !WPACKET_put_bytes_u16(pkt, 0)) {
  355. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_ETM, ERR_R_INTERNAL_ERROR);
  356. return 0;
  357. }
  358. return 1;
  359. }
  360. #ifndef OPENSSL_NO_CT
  361. int tls_construct_ctos_sct(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  362. size_t chainidx, int *al)
  363. {
  364. if (s->ct_validation_callback == NULL)
  365. return 1;
  366. /* Not defined for client Certificates */
  367. if (x != NULL)
  368. return 1;
  369. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp)
  370. || !WPACKET_put_bytes_u16(pkt, 0)) {
  371. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SCT, ERR_R_INTERNAL_ERROR);
  372. return 0;
  373. }
  374. return 1;
  375. }
  376. #endif
  377. int tls_construct_ctos_ems(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  378. size_t chainidx, int *al)
  379. {
  380. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
  381. || !WPACKET_put_bytes_u16(pkt, 0)) {
  382. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_EMS, ERR_R_INTERNAL_ERROR);
  383. return 0;
  384. }
  385. return 1;
  386. }
  387. int tls_construct_ctos_supported_versions(SSL *s, WPACKET *pkt,
  388. unsigned int context, X509 *x,
  389. size_t chainidx, int *al)
  390. {
  391. int currv, min_version, max_version, reason;
  392. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
  393. || !WPACKET_start_sub_packet_u16(pkt)
  394. || !WPACKET_start_sub_packet_u8(pkt)) {
  395. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
  396. ERR_R_INTERNAL_ERROR);
  397. return 0;
  398. }
  399. reason = ssl_get_min_max_version(s, &min_version, &max_version);
  400. if (reason != 0) {
  401. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS, reason);
  402. return 0;
  403. }
  404. /*
  405. * TODO(TLS1.3): There is some discussion on the TLS list as to whether
  406. * we should include versions <TLS1.2. For the moment we do. To be
  407. * reviewed later.
  408. */
  409. for (currv = max_version; currv >= min_version; currv--) {
  410. /* TODO(TLS1.3): Remove this first if clause prior to release!! */
  411. if (currv == TLS1_3_VERSION) {
  412. if (!WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION_DRAFT)) {
  413. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
  414. ERR_R_INTERNAL_ERROR);
  415. return 0;
  416. }
  417. } else if (!WPACKET_put_bytes_u16(pkt, currv)) {
  418. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
  419. ERR_R_INTERNAL_ERROR);
  420. return 0;
  421. }
  422. }
  423. if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
  424. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
  425. ERR_R_INTERNAL_ERROR);
  426. return 0;
  427. }
  428. return 1;
  429. }
  430. /*
  431. * Construct a psk_kex_modes extension. We only have two modes we know about
  432. * at this stage, so we send both.
  433. */
  434. int tls_construct_ctos_psk_kex_modes(SSL *s, WPACKET *pkt, unsigned int context,
  435. X509 *x, size_t chainidx, int *al)
  436. {
  437. #ifndef OPENSSL_NO_TLS1_3
  438. /*
  439. * TODO(TLS1.3): Do we want this list to be configurable? For now we always
  440. * just send both supported modes
  441. */
  442. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk_kex_modes)
  443. || !WPACKET_start_sub_packet_u16(pkt)
  444. || !WPACKET_start_sub_packet_u8(pkt)
  445. || !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE_DHE)
  446. || !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE)
  447. || !WPACKET_close(pkt)
  448. || !WPACKET_close(pkt)) {
  449. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_PSK_KEX_MODES, ERR_R_INTERNAL_ERROR);
  450. return 0;
  451. }
  452. s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_KE | TLSEXT_KEX_MODE_FLAG_KE_DHE;
  453. #endif
  454. return 1;
  455. }
  456. #ifndef OPENSSL_NO_TLS1_3
  457. static int add_key_share(SSL *s, WPACKET *pkt, unsigned int curve_id)
  458. {
  459. unsigned char *encoded_point;
  460. EVP_PKEY *key_share_key;
  461. size_t encodedlen;
  462. key_share_key = ssl_generate_pkey_curve(curve_id);
  463. if (key_share_key == NULL) {
  464. SSLerr(SSL_F_ADD_KEY_SHARE, ERR_R_EVP_LIB);
  465. return 0;
  466. }
  467. /* Encode the public key. */
  468. encodedlen = EVP_PKEY_get1_tls_encodedpoint(key_share_key,
  469. &encoded_point);
  470. if (encodedlen == 0) {
  471. SSLerr(SSL_F_ADD_KEY_SHARE, ERR_R_EC_LIB);
  472. EVP_PKEY_free(key_share_key);
  473. return 0;
  474. }
  475. /* Create KeyShareEntry */
  476. if (!WPACKET_put_bytes_u16(pkt, curve_id)
  477. || !WPACKET_sub_memcpy_u16(pkt, encoded_point, encodedlen)) {
  478. SSLerr(SSL_F_ADD_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  479. EVP_PKEY_free(key_share_key);
  480. OPENSSL_free(encoded_point);
  481. return 0;
  482. }
  483. /*
  484. * TODO(TLS1.3): When changing to send more than one key_share we're
  485. * going to need to be able to save more than one EVP_PKEY. For now
  486. * we reuse the existing tmp.pkey
  487. */
  488. s->s3->tmp.pkey = key_share_key;
  489. s->s3->group_id = curve_id;
  490. OPENSSL_free(encoded_point);
  491. return 1;
  492. }
  493. #endif
  494. int tls_construct_ctos_key_share(SSL *s, WPACKET *pkt, unsigned int context,
  495. X509 *x, size_t chainidx, int *al)
  496. {
  497. #ifndef OPENSSL_NO_TLS1_3
  498. size_t i, num_curves = 0;
  499. const unsigned char *pcurves = NULL;
  500. unsigned int curve_id = 0;
  501. /* key_share extension */
  502. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
  503. /* Extension data sub-packet */
  504. || !WPACKET_start_sub_packet_u16(pkt)
  505. /* KeyShare list sub-packet */
  506. || !WPACKET_start_sub_packet_u16(pkt)) {
  507. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  508. return 0;
  509. }
  510. if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) {
  511. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  512. return 0;
  513. }
  514. if (s->s3->tmp.pkey != NULL) {
  515. /* Shouldn't happen! */
  516. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  517. return 0;
  518. }
  519. /*
  520. * TODO(TLS1.3): Make the number of key_shares sent configurable. For
  521. * now, just send one
  522. */
  523. if (s->s3->group_id != 0) {
  524. curve_id = s->s3->group_id;
  525. } else {
  526. for (i = 0; i < num_curves; i++, pcurves += 2) {
  527. if (!tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED))
  528. continue;
  529. curve_id = bytestogroup(pcurves);
  530. break;
  531. }
  532. }
  533. if (curve_id == 0) {
  534. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, SSL_R_NO_SUITABLE_KEY_SHARE);
  535. return 0;
  536. }
  537. if (!add_key_share(s, pkt, curve_id))
  538. return 0;
  539. if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
  540. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  541. return 0;
  542. }
  543. #endif
  544. return 1;
  545. }
  546. int tls_construct_ctos_cookie(SSL *s, WPACKET *pkt, unsigned int context,
  547. X509 *x, size_t chainidx, int *al)
  548. {
  549. int ret = 0;
  550. /* Should only be set if we've had an HRR */
  551. if (s->ext.tls13_cookie_len == 0)
  552. return 1;
  553. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie)
  554. /* Extension data sub-packet */
  555. || !WPACKET_start_sub_packet_u16(pkt)
  556. || !WPACKET_sub_memcpy_u16(pkt, s->ext.tls13_cookie,
  557. s->ext.tls13_cookie_len)
  558. || !WPACKET_close(pkt)) {
  559. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_COOKIE, ERR_R_INTERNAL_ERROR);
  560. goto end;
  561. }
  562. ret = 1;
  563. end:
  564. OPENSSL_free(s->ext.tls13_cookie);
  565. s->ext.tls13_cookie = NULL;
  566. s->ext.tls13_cookie_len = 0;
  567. return ret;
  568. }
  569. int tls_construct_ctos_early_data(SSL *s, WPACKET *pkt, unsigned int context,
  570. X509 *x, size_t chainidx, int *al)
  571. {
  572. if (s->early_data_state != SSL_EARLY_DATA_CONNECTING
  573. || s->session->ext.max_early_data == 0) {
  574. s->max_early_data = 0;
  575. return 1;
  576. }
  577. s->max_early_data = s->session->ext.max_early_data;
  578. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
  579. || !WPACKET_start_sub_packet_u16(pkt)
  580. || !WPACKET_close(pkt)) {
  581. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA, ERR_R_INTERNAL_ERROR);
  582. return 0;
  583. }
  584. /*
  585. * We set this to rejected here. Later, if the server acknowledges the
  586. * extension, we set it to accepted.
  587. */
  588. s->ext.early_data = SSL_EARLY_DATA_REJECTED;
  589. return 1;
  590. }
  591. #define F5_WORKAROUND_MIN_MSG_LEN 0xff
  592. #define F5_WORKAROUND_MAX_MSG_LEN 0x200
  593. /*
  594. * PSK pre binder overhead =
  595. * 2 bytes for TLSEXT_TYPE_psk
  596. * 2 bytes for extension length
  597. * 2 bytes for identities list length
  598. * 2 bytes for identity length
  599. * 4 bytes for obfuscated_ticket_age
  600. * 2 bytes for binder list length
  601. * 1 byte for binder length
  602. * The above excludes the number of bytes for the identity itself and the
  603. * subsequent binder bytes
  604. */
  605. #define PSK_PRE_BINDER_OVERHEAD (2 + 2 + 2 + 2 + 4 + 2 + 1)
  606. int tls_construct_ctos_padding(SSL *s, WPACKET *pkt, unsigned int context,
  607. X509 *x, size_t chainidx, int *al)
  608. {
  609. unsigned char *padbytes;
  610. size_t hlen;
  611. if ((s->options & SSL_OP_TLSEXT_PADDING) == 0)
  612. return 1;
  613. /*
  614. * Add padding to workaround bugs in F5 terminators. See RFC7685.
  615. * This code calculates the length of all extensions added so far but
  616. * excludes the PSK extension (because that MUST be written last). Therefore
  617. * this extension MUST always appear second to last.
  618. */
  619. if (!WPACKET_get_total_written(pkt, &hlen)) {
  620. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_PADDING, ERR_R_INTERNAL_ERROR);
  621. return 0;
  622. }
  623. /*
  624. * If we're going to send a PSK then that will be written out after this
  625. * extension, so we need to calculate how long it is going to be.
  626. */
  627. if (s->session->ssl_version == TLS1_3_VERSION
  628. && s->session->ext.ticklen != 0
  629. && s->session->cipher != NULL) {
  630. const EVP_MD *md = ssl_md(s->session->cipher->algorithm2);
  631. if (md != NULL) {
  632. /*
  633. * Add the fixed PSK overhead, the identity length and the binder
  634. * length.
  635. */
  636. hlen += PSK_PRE_BINDER_OVERHEAD + s->session->ext.ticklen
  637. + EVP_MD_size(md);
  638. }
  639. }
  640. if (hlen > F5_WORKAROUND_MIN_MSG_LEN && hlen < F5_WORKAROUND_MAX_MSG_LEN) {
  641. /* Calculate the amount of padding we need to add */
  642. hlen = F5_WORKAROUND_MAX_MSG_LEN - hlen;
  643. /*
  644. * Take off the size of extension header itself (2 bytes for type and
  645. * 2 bytes for length bytes)
  646. */
  647. if (hlen >= 4)
  648. hlen -= 4;
  649. else
  650. hlen = 0;
  651. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding)
  652. || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) {
  653. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_PADDING, ERR_R_INTERNAL_ERROR);
  654. return 0;
  655. }
  656. memset(padbytes, 0, hlen);
  657. }
  658. return 1;
  659. }
  660. /*
  661. * Construct the pre_shared_key extension
  662. */
  663. int tls_construct_ctos_psk(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
  664. size_t chainidx, int *al)
  665. {
  666. #ifndef OPENSSL_NO_TLS1_3
  667. uint32_t now, agesec, agems;
  668. size_t hashsize, binderoffset, msglen;
  669. unsigned char *binder = NULL, *msgstart = NULL;
  670. const EVP_MD *md;
  671. int ret = 0;
  672. s->session->ext.tick_identity = TLSEXT_PSK_BAD_IDENTITY;
  673. /*
  674. * Note: At this stage of the code we only support adding a single
  675. * resumption PSK. If we add support for multiple PSKs then the length
  676. * calculations in the padding extension will need to be adjusted.
  677. */
  678. /*
  679. * If this is an incompatible or new session then we have nothing to resume
  680. * so don't add this extension.
  681. */
  682. if (s->session->ssl_version != TLS1_3_VERSION
  683. || s->session->ext.ticklen == 0)
  684. return 1;
  685. if (s->session->cipher == NULL) {
  686. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_PSK, ERR_R_INTERNAL_ERROR);
  687. goto err;
  688. }
  689. md = ssl_md(s->session->cipher->algorithm2);
  690. if (md == NULL) {
  691. /* Don't recognize this cipher so we can't use the session. Ignore it */
  692. return 1;
  693. }
  694. if (s->hello_retry_request && md != ssl_handshake_md(s)) {
  695. /*
  696. * Selected ciphersuite hash does not match the hash for the session so
  697. * we can't use it.
  698. */
  699. return 1;
  700. }
  701. /*
  702. * Technically the C standard just says time() returns a time_t and says
  703. * nothing about the encoding of that type. In practice most implementations
  704. * follow POSIX which holds it as an integral type in seconds since epoch.
  705. * We've already made the assumption that we can do this in multiple places
  706. * in the code, so portability shouldn't be an issue.
  707. */
  708. now = (uint32_t)time(NULL);
  709. agesec = now - (uint32_t)s->session->time;
  710. if (s->session->ext.tick_lifetime_hint < agesec) {
  711. /* Ticket is too old. Ignore it. */
  712. return 1;
  713. }
  714. /*
  715. * Calculate age in ms. We're just doing it to nearest second. Should be
  716. * good enough.
  717. */
  718. agems = agesec * (uint32_t)1000;
  719. if (agesec != 0 && agems / (uint32_t)1000 != agesec) {
  720. /*
  721. * Overflow. Shouldn't happen unless this is a *really* old session. If
  722. * so we just ignore it.
  723. */
  724. return 1;
  725. }
  726. /*
  727. * Obfuscate the age. Overflow here is fine, this addition is supposed to
  728. * be mod 2^32.
  729. */
  730. agems += s->session->ext.tick_age_add;
  731. hashsize = EVP_MD_size(md);
  732. /* Create the extension, but skip over the binder for now */
  733. if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)
  734. || !WPACKET_start_sub_packet_u16(pkt)
  735. || !WPACKET_start_sub_packet_u16(pkt)
  736. || !WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick,
  737. s->session->ext.ticklen)
  738. || !WPACKET_put_bytes_u32(pkt, agems)
  739. || !WPACKET_close(pkt)
  740. || !WPACKET_get_total_written(pkt, &binderoffset)
  741. || !WPACKET_start_sub_packet_u16(pkt)
  742. || !WPACKET_sub_allocate_bytes_u8(pkt, hashsize, &binder)
  743. || !WPACKET_close(pkt)
  744. || !WPACKET_close(pkt)
  745. || !WPACKET_get_total_written(pkt, &msglen)
  746. /*
  747. * We need to fill in all the sub-packet lengths now so we can
  748. * calculate the HMAC of the message up to the binders
  749. */
  750. || !WPACKET_fill_lengths(pkt)) {
  751. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_PSK, ERR_R_INTERNAL_ERROR);
  752. goto err;
  753. }
  754. msgstart = WPACKET_get_curr(pkt) - msglen;
  755. if (tls_psk_do_binder(s, md, msgstart, binderoffset, NULL, binder,
  756. s->session, 1) != 1) {
  757. SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_PSK, ERR_R_INTERNAL_ERROR);
  758. goto err;
  759. }
  760. s->session->ext.tick_identity = 0;
  761. ret = 1;
  762. err:
  763. return ret;
  764. #else
  765. return 1;
  766. #endif
  767. }
  768. /*
  769. * Parse the server's renegotiation binding and abort if it's not right
  770. */
  771. int tls_parse_stoc_renegotiate(SSL *s, PACKET *pkt, unsigned int context,
  772. X509 *x, size_t chainidx, int *al)
  773. {
  774. size_t expected_len = s->s3->previous_client_finished_len
  775. + s->s3->previous_server_finished_len;
  776. size_t ilen;
  777. const unsigned char *data;
  778. /* Check for logic errors */
  779. assert(expected_len == 0 || s->s3->previous_client_finished_len != 0);
  780. assert(expected_len == 0 || s->s3->previous_server_finished_len != 0);
  781. /* Parse the length byte */
  782. if (!PACKET_get_1_len(pkt, &ilen)) {
  783. SSLerr(SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
  784. SSL_R_RENEGOTIATION_ENCODING_ERR);
  785. *al = SSL_AD_ILLEGAL_PARAMETER;
  786. return 0;
  787. }
  788. /* Consistency check */
  789. if (PACKET_remaining(pkt) != ilen) {
  790. SSLerr(SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
  791. SSL_R_RENEGOTIATION_ENCODING_ERR);
  792. *al = SSL_AD_ILLEGAL_PARAMETER;
  793. return 0;
  794. }
  795. /* Check that the extension matches */
  796. if (ilen != expected_len) {
  797. SSLerr(SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
  798. SSL_R_RENEGOTIATION_MISMATCH);
  799. *al = SSL_AD_HANDSHAKE_FAILURE;
  800. return 0;
  801. }
  802. if (!PACKET_get_bytes(pkt, &data, s->s3->previous_client_finished_len)
  803. || memcmp(data, s->s3->previous_client_finished,
  804. s->s3->previous_client_finished_len) != 0) {
  805. SSLerr(SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
  806. SSL_R_RENEGOTIATION_MISMATCH);
  807. *al = SSL_AD_HANDSHAKE_FAILURE;
  808. return 0;
  809. }
  810. if (!PACKET_get_bytes(pkt, &data, s->s3->previous_server_finished_len)
  811. || memcmp(data, s->s3->previous_server_finished,
  812. s->s3->previous_server_finished_len) != 0) {
  813. SSLerr(SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
  814. SSL_R_RENEGOTIATION_MISMATCH);
  815. *al = SSL_AD_ILLEGAL_PARAMETER;
  816. return 0;
  817. }
  818. s->s3->send_connection_binding = 1;
  819. return 1;
  820. }
  821. int tls_parse_stoc_server_name(SSL *s, PACKET *pkt, unsigned int context,
  822. X509 *x, size_t chainidx, int *al)
  823. {
  824. if (s->ext.hostname == NULL || PACKET_remaining(pkt) > 0) {
  825. *al = SSL_AD_UNRECOGNIZED_NAME;
  826. return 0;
  827. }
  828. if (!s->hit) {
  829. if (s->session->ext.hostname != NULL) {
  830. *al = SSL_AD_INTERNAL_ERROR;
  831. return 0;
  832. }
  833. s->session->ext.hostname = OPENSSL_strdup(s->ext.hostname);
  834. if (s->session->ext.hostname == NULL) {
  835. *al = SSL_AD_INTERNAL_ERROR;
  836. return 0;
  837. }
  838. }
  839. return 1;
  840. }
  841. #ifndef OPENSSL_NO_EC
  842. int tls_parse_stoc_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context,
  843. X509 *x, size_t chainidx, int *al)
  844. {
  845. unsigned int ecpointformats_len;
  846. PACKET ecptformatlist;
  847. if (!PACKET_as_length_prefixed_1(pkt, &ecptformatlist)) {
  848. *al = SSL_AD_DECODE_ERROR;
  849. return 0;
  850. }
  851. if (!s->hit) {
  852. ecpointformats_len = PACKET_remaining(&ecptformatlist);
  853. s->session->ext.ecpointformats_len = 0;
  854. OPENSSL_free(s->session->ext.ecpointformats);
  855. s->session->ext.ecpointformats = OPENSSL_malloc(ecpointformats_len);
  856. if (s->session->ext.ecpointformats == NULL) {
  857. *al = SSL_AD_INTERNAL_ERROR;
  858. return 0;
  859. }
  860. s->session->ext.ecpointformats_len = ecpointformats_len;
  861. if (!PACKET_copy_bytes(&ecptformatlist,
  862. s->session->ext.ecpointformats,
  863. ecpointformats_len)) {
  864. *al = SSL_AD_INTERNAL_ERROR;
  865. return 0;
  866. }
  867. }
  868. return 1;
  869. }
  870. #endif
  871. int tls_parse_stoc_session_ticket(SSL *s, PACKET *pkt, unsigned int context,
  872. X509 *x, size_t chainidx, int *al)
  873. {
  874. if (s->ext.session_ticket_cb != NULL &&
  875. !s->ext.session_ticket_cb(s, PACKET_data(pkt),
  876. PACKET_remaining(pkt),
  877. s->ext.session_ticket_cb_arg)) {
  878. *al = SSL_AD_INTERNAL_ERROR;
  879. return 0;
  880. }
  881. if (!tls_use_ticket(s) || PACKET_remaining(pkt) > 0) {
  882. *al = SSL_AD_UNSUPPORTED_EXTENSION;
  883. return 0;
  884. }
  885. s->ext.ticket_expected = 1;
  886. return 1;
  887. }
  888. #ifndef OPENSSL_NO_OCSP
  889. int tls_parse_stoc_status_request(SSL *s, PACKET *pkt, unsigned int context,
  890. X509 *x, size_t chainidx, int *al)
  891. {
  892. /*
  893. * MUST only be sent if we've requested a status
  894. * request message. In TLS <= 1.2 it must also be empty.
  895. */
  896. if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp
  897. || (!SSL_IS_TLS13(s) && PACKET_remaining(pkt) > 0)) {
  898. *al = SSL_AD_UNSUPPORTED_EXTENSION;
  899. return 0;
  900. }
  901. if (SSL_IS_TLS13(s)) {
  902. /* We only know how to handle this if it's for the first Certificate in
  903. * the chain. We ignore any other responses.
  904. */
  905. if (chainidx != 0)
  906. return 1;
  907. return tls_process_cert_status_body(s, pkt, al);
  908. }
  909. /* Set flag to expect CertificateStatus message */
  910. s->ext.status_expected = 1;
  911. return 1;
  912. }
  913. #endif
  914. #ifndef OPENSSL_NO_CT
  915. int tls_parse_stoc_sct(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  916. size_t chainidx, int *al)
  917. {
  918. /*
  919. * Only take it if we asked for it - i.e if there is no CT validation
  920. * callback set, then a custom extension MAY be processing it, so we
  921. * need to let control continue to flow to that.
  922. */
  923. if (s->ct_validation_callback != NULL) {
  924. size_t size = PACKET_remaining(pkt);
  925. /* Simply copy it off for later processing */
  926. OPENSSL_free(s->ext.scts);
  927. s->ext.scts = NULL;
  928. s->ext.scts_len = size;
  929. if (size > 0) {
  930. s->ext.scts = OPENSSL_malloc(size);
  931. if (s->ext.scts == NULL
  932. || !PACKET_copy_bytes(pkt, s->ext.scts, size)) {
  933. *al = SSL_AD_INTERNAL_ERROR;
  934. return 0;
  935. }
  936. }
  937. } else {
  938. if (custom_ext_parse(s, context,
  939. TLSEXT_TYPE_signed_certificate_timestamp,
  940. PACKET_data(pkt), PACKET_remaining(pkt),
  941. x, chainidx, al) <= 0)
  942. return 0;
  943. }
  944. return 1;
  945. }
  946. #endif
  947. #ifndef OPENSSL_NO_NEXTPROTONEG
  948. /*
  949. * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
  950. * elements of zero length are allowed and the set of elements must exactly
  951. * fill the length of the block. Returns 1 on success or 0 on failure.
  952. */
  953. static int ssl_next_proto_validate(PACKET *pkt)
  954. {
  955. PACKET tmp_protocol;
  956. while (PACKET_remaining(pkt)) {
  957. if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)
  958. || PACKET_remaining(&tmp_protocol) == 0)
  959. return 0;
  960. }
  961. return 1;
  962. }
  963. int tls_parse_stoc_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  964. size_t chainidx, int *al)
  965. {
  966. unsigned char *selected;
  967. unsigned char selected_len;
  968. PACKET tmppkt;
  969. /* Check if we are in a renegotiation. If so ignore this extension */
  970. if (!SSL_IS_FIRST_HANDSHAKE(s))
  971. return 1;
  972. /* We must have requested it. */
  973. if (s->ctx->ext.npn_select_cb == NULL) {
  974. *al = SSL_AD_UNSUPPORTED_EXTENSION;
  975. return 0;
  976. }
  977. /* The data must be valid */
  978. tmppkt = *pkt;
  979. if (!ssl_next_proto_validate(&tmppkt)) {
  980. *al = SSL_AD_DECODE_ERROR;
  981. return 0;
  982. }
  983. if (s->ctx->ext.npn_select_cb(s, &selected, &selected_len,
  984. PACKET_data(pkt),
  985. PACKET_remaining(pkt),
  986. s->ctx->ext.npn_select_cb_arg) !=
  987. SSL_TLSEXT_ERR_OK) {
  988. *al = SSL_AD_INTERNAL_ERROR;
  989. return 0;
  990. }
  991. /*
  992. * Could be non-NULL if server has sent multiple NPN extensions in
  993. * a single Serverhello
  994. */
  995. OPENSSL_free(s->ext.npn);
  996. s->ext.npn = OPENSSL_malloc(selected_len);
  997. if (s->ext.npn == NULL) {
  998. *al = SSL_AD_INTERNAL_ERROR;
  999. return 0;
  1000. }
  1001. memcpy(s->ext.npn, selected, selected_len);
  1002. s->ext.npn_len = selected_len;
  1003. s->s3->npn_seen = 1;
  1004. return 1;
  1005. }
  1006. #endif
  1007. int tls_parse_stoc_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1008. size_t chainidx, int *al)
  1009. {
  1010. size_t len;
  1011. /* We must have requested it. */
  1012. if (!s->s3->alpn_sent) {
  1013. *al = SSL_AD_UNSUPPORTED_EXTENSION;
  1014. return 0;
  1015. }
  1016. /*-
  1017. * The extension data consists of:
  1018. * uint16 list_length
  1019. * uint8 proto_length;
  1020. * uint8 proto[proto_length];
  1021. */
  1022. if (!PACKET_get_net_2_len(pkt, &len)
  1023. || PACKET_remaining(pkt) != len || !PACKET_get_1_len(pkt, &len)
  1024. || PACKET_remaining(pkt) != len) {
  1025. *al = SSL_AD_DECODE_ERROR;
  1026. return 0;
  1027. }
  1028. OPENSSL_free(s->s3->alpn_selected);
  1029. s->s3->alpn_selected = OPENSSL_malloc(len);
  1030. if (s->s3->alpn_selected == NULL) {
  1031. *al = SSL_AD_INTERNAL_ERROR;
  1032. return 0;
  1033. }
  1034. if (!PACKET_copy_bytes(pkt, s->s3->alpn_selected, len)) {
  1035. *al = SSL_AD_DECODE_ERROR;
  1036. return 0;
  1037. }
  1038. s->s3->alpn_selected_len = len;
  1039. return 1;
  1040. }
  1041. #ifndef OPENSSL_NO_SRTP
  1042. int tls_parse_stoc_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1043. size_t chainidx, int *al)
  1044. {
  1045. unsigned int id, ct, mki;
  1046. int i;
  1047. STACK_OF(SRTP_PROTECTION_PROFILE) *clnt;
  1048. SRTP_PROTECTION_PROFILE *prof;
  1049. if (!PACKET_get_net_2(pkt, &ct) || ct != 2
  1050. || !PACKET_get_net_2(pkt, &id)
  1051. || !PACKET_get_1(pkt, &mki)
  1052. || PACKET_remaining(pkt) != 0) {
  1053. SSLerr(SSL_F_TLS_PARSE_STOC_USE_SRTP,
  1054. SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1055. *al = SSL_AD_DECODE_ERROR;
  1056. return 0;
  1057. }
  1058. if (mki != 0) {
  1059. /* Must be no MKI, since we never offer one */
  1060. SSLerr(SSL_F_TLS_PARSE_STOC_USE_SRTP, SSL_R_BAD_SRTP_MKI_VALUE);
  1061. *al = SSL_AD_ILLEGAL_PARAMETER;
  1062. return 0;
  1063. }
  1064. /* Throw an error if the server gave us an unsolicited extension */
  1065. clnt = SSL_get_srtp_profiles(s);
  1066. if (clnt == NULL) {
  1067. SSLerr(SSL_F_TLS_PARSE_STOC_USE_SRTP, SSL_R_NO_SRTP_PROFILES);
  1068. *al = SSL_AD_DECODE_ERROR;
  1069. return 0;
  1070. }
  1071. /*
  1072. * Check to see if the server gave us something we support (and
  1073. * presumably offered)
  1074. */
  1075. for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) {
  1076. prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
  1077. if (prof->id == id) {
  1078. s->srtp_profile = prof;
  1079. *al = 0;
  1080. return 1;
  1081. }
  1082. }
  1083. SSLerr(SSL_F_TLS_PARSE_STOC_USE_SRTP,
  1084. SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1085. *al = SSL_AD_DECODE_ERROR;
  1086. return 0;
  1087. }
  1088. #endif
  1089. int tls_parse_stoc_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1090. size_t chainidx, int *al)
  1091. {
  1092. /* Ignore if inappropriate ciphersuite */
  1093. if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
  1094. && s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
  1095. && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4)
  1096. s->ext.use_etm = 1;
  1097. return 1;
  1098. }
  1099. int tls_parse_stoc_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1100. size_t chainidx, int *al)
  1101. {
  1102. s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
  1103. if (!s->hit)
  1104. s->session->flags |= SSL_SESS_FLAG_EXTMS;
  1105. return 1;
  1106. }
  1107. int tls_parse_stoc_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1108. size_t chainidx, int *al)
  1109. {
  1110. #ifndef OPENSSL_NO_TLS1_3
  1111. unsigned int group_id;
  1112. PACKET encoded_pt;
  1113. EVP_PKEY *ckey = s->s3->tmp.pkey, *skey = NULL;
  1114. /* Sanity check */
  1115. if (ckey == NULL || s->s3->peer_tmp != NULL) {
  1116. *al = SSL_AD_INTERNAL_ERROR;
  1117. SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  1118. return 0;
  1119. }
  1120. if (!PACKET_get_net_2(pkt, &group_id)) {
  1121. *al = SSL_AD_HANDSHAKE_FAILURE;
  1122. SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_LENGTH_MISMATCH);
  1123. return 0;
  1124. }
  1125. if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0) {
  1126. unsigned const char *pcurves = NULL;
  1127. size_t i, num_curves;
  1128. if (PACKET_remaining(pkt) != 0) {
  1129. *al = SSL_AD_HANDSHAKE_FAILURE;
  1130. SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_LENGTH_MISMATCH);
  1131. return 0;
  1132. }
  1133. /*
  1134. * It is an error if the HelloRetryRequest wants a key_share that we
  1135. * already sent in the first ClientHello
  1136. */
  1137. if (group_id == s->s3->group_id) {
  1138. *al = SSL_AD_ILLEGAL_PARAMETER;
  1139. SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
  1140. return 0;
  1141. }
  1142. /* Validate the selected group is one we support */
  1143. if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) {
  1144. SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  1145. return 0;
  1146. }
  1147. for (i = 0; i < num_curves; i++, pcurves += 2) {
  1148. if (group_id == bytestogroup(pcurves))
  1149. break;
  1150. }
  1151. if (i >= num_curves
  1152. || !tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) {
  1153. *al = SSL_AD_ILLEGAL_PARAMETER;
  1154. SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
  1155. return 0;
  1156. }
  1157. s->s3->group_id = group_id;
  1158. EVP_PKEY_free(s->s3->tmp.pkey);
  1159. s->s3->tmp.pkey = NULL;
  1160. return 1;
  1161. }
  1162. if (group_id != s->s3->group_id) {
  1163. /*
  1164. * This isn't for the group that we sent in the original
  1165. * key_share!
  1166. */
  1167. *al = SSL_AD_HANDSHAKE_FAILURE;
  1168. SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
  1169. return 0;
  1170. }
  1171. if (!PACKET_as_length_prefixed_2(pkt, &encoded_pt)
  1172. || PACKET_remaining(&encoded_pt) == 0) {
  1173. *al = SSL_AD_DECODE_ERROR;
  1174. SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_LENGTH_MISMATCH);
  1175. return 0;
  1176. }
  1177. skey = ssl_generate_pkey(ckey);
  1178. if (skey == NULL) {
  1179. *al = SSL_AD_INTERNAL_ERROR;
  1180. SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, ERR_R_MALLOC_FAILURE);
  1181. return 0;
  1182. }
  1183. if (!EVP_PKEY_set1_tls_encodedpoint(skey, PACKET_data(&encoded_pt),
  1184. PACKET_remaining(&encoded_pt))) {
  1185. *al = SSL_AD_DECODE_ERROR;
  1186. SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_BAD_ECPOINT);
  1187. EVP_PKEY_free(skey);
  1188. return 0;
  1189. }
  1190. if (ssl_derive(s, ckey, skey, 1) == 0) {
  1191. *al = SSL_AD_INTERNAL_ERROR;
  1192. SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
  1193. EVP_PKEY_free(skey);
  1194. return 0;
  1195. }
  1196. s->s3->peer_tmp = skey;
  1197. #endif
  1198. return 1;
  1199. }
  1200. int tls_parse_stoc_cookie(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1201. size_t chainidx, int *al)
  1202. {
  1203. PACKET cookie;
  1204. if (!PACKET_as_length_prefixed_2(pkt, &cookie)
  1205. || !PACKET_memdup(&cookie, &s->ext.tls13_cookie,
  1206. &s->ext.tls13_cookie_len)) {
  1207. *al = SSL_AD_DECODE_ERROR;
  1208. SSLerr(SSL_F_TLS_PARSE_STOC_COOKIE, SSL_R_LENGTH_MISMATCH);
  1209. return 0;
  1210. }
  1211. return 1;
  1212. }
  1213. int tls_parse_stoc_early_data(SSL *s, PACKET *pkt, unsigned int context,
  1214. X509 *x, size_t chainidx, int *al)
  1215. {
  1216. if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {
  1217. unsigned long max_early_data;
  1218. if (!PACKET_get_net_4(pkt, &max_early_data)
  1219. || PACKET_remaining(pkt) != 0) {
  1220. SSLerr(SSL_F_TLS_PARSE_STOC_EARLY_DATA,
  1221. SSL_R_INVALID_MAX_EARLY_DATA);
  1222. *al = SSL_AD_DECODE_ERROR;
  1223. return 0;
  1224. }
  1225. s->session->ext.max_early_data = max_early_data;
  1226. return 1;
  1227. }
  1228. if (PACKET_remaining(pkt) != 0) {
  1229. *al = SSL_AD_DECODE_ERROR;
  1230. return 0;
  1231. }
  1232. if (s->ext.early_data != SSL_EARLY_DATA_REJECTED
  1233. || !s->hit
  1234. || s->session->ext.tick_identity != 0) {
  1235. /*
  1236. * If we get here then we didn't send early data, or we didn't resume
  1237. * using the first identity so the server should not be accepting it.
  1238. */
  1239. *al = SSL_AD_ILLEGAL_PARAMETER;
  1240. return 0;
  1241. }
  1242. s->ext.early_data = SSL_EARLY_DATA_ACCEPTED;
  1243. return 1;
  1244. }
  1245. int tls_parse_stoc_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
  1246. size_t chainidx, int *al)
  1247. {
  1248. #ifndef OPENSSL_NO_TLS1_3
  1249. unsigned int identity;
  1250. if (!PACKET_get_net_2(pkt, &identity) || PACKET_remaining(pkt) != 0) {
  1251. *al = SSL_AD_HANDSHAKE_FAILURE;
  1252. SSLerr(SSL_F_TLS_PARSE_STOC_PSK, SSL_R_LENGTH_MISMATCH);
  1253. return 0;
  1254. }
  1255. if (s->session->ext.tick_identity != (int)identity) {
  1256. *al = SSL_AD_HANDSHAKE_FAILURE;
  1257. SSLerr(SSL_F_TLS_PARSE_STOC_PSK, SSL_R_BAD_PSK_IDENTITY);
  1258. return 0;
  1259. }
  1260. s->hit = 1;
  1261. #endif
  1262. return 1;
  1263. }