handshake_client.cc 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. *
  113. * Portions of the attached software ("Contribution") are developed by
  114. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  115. *
  116. * The Contribution is licensed pursuant to the OpenSSL open source
  117. * license provided above.
  118. *
  119. * ECC cipher suite support in OpenSSL originally written by
  120. * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
  121. *
  122. */
  123. /* ====================================================================
  124. * Copyright 2005 Nokia. All rights reserved.
  125. *
  126. * The portions of the attached software ("Contribution") is developed by
  127. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  128. * license.
  129. *
  130. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  131. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  132. * support (see RFC 4279) to OpenSSL.
  133. *
  134. * No patent licenses or other rights except those expressly stated in
  135. * the OpenSSL open source license shall be deemed granted or received
  136. * expressly, by implication, estoppel, or otherwise.
  137. *
  138. * No assurances are provided by Nokia that the Contribution does not
  139. * infringe the patent or other intellectual property rights of any third
  140. * party or that the license provides you with all the necessary rights
  141. * to make use of the Contribution.
  142. *
  143. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  144. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  145. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  146. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  147. * OTHERWISE.
  148. */
  149. #include <openssl/ssl.h>
  150. #include <assert.h>
  151. #include <limits.h>
  152. #include <string.h>
  153. #include <utility>
  154. #include <openssl/aead.h>
  155. #include <openssl/bn.h>
  156. #include <openssl/buf.h>
  157. #include <openssl/bytestring.h>
  158. #include <openssl/ec_key.h>
  159. #include <openssl/ecdsa.h>
  160. #include <openssl/err.h>
  161. #include <openssl/evp.h>
  162. #include <openssl/md5.h>
  163. #include <openssl/mem.h>
  164. #include <openssl/rand.h>
  165. #include <openssl/sha.h>
  166. #include "../crypto/internal.h"
  167. #include "internal.h"
  168. BSSL_NAMESPACE_BEGIN
  169. enum ssl_client_hs_state_t {
  170. state_start_connect = 0,
  171. state_enter_early_data,
  172. state_early_reverify_server_certificate,
  173. state_read_hello_verify_request,
  174. state_read_server_hello,
  175. state_tls13,
  176. state_read_server_certificate,
  177. state_read_certificate_status,
  178. state_verify_server_certificate,
  179. state_reverify_server_certificate,
  180. state_read_server_key_exchange,
  181. state_read_certificate_request,
  182. state_read_server_hello_done,
  183. state_send_client_certificate,
  184. state_send_client_key_exchange,
  185. state_send_client_certificate_verify,
  186. state_send_client_finished,
  187. state_finish_flight,
  188. state_read_session_ticket,
  189. state_process_change_cipher_spec,
  190. state_read_server_finished,
  191. state_finish_client_handshake,
  192. state_done,
  193. };
  194. // ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
  195. // disabled algorithms.
  196. static void ssl_get_client_disabled(SSL_HANDSHAKE *hs, uint32_t *out_mask_a,
  197. uint32_t *out_mask_k) {
  198. *out_mask_a = 0;
  199. *out_mask_k = 0;
  200. // PSK requires a client callback.
  201. if (hs->config->psk_client_callback == NULL) {
  202. *out_mask_a |= SSL_aPSK;
  203. *out_mask_k |= SSL_kPSK;
  204. }
  205. }
  206. static bool ssl_write_client_cipher_list(SSL_HANDSHAKE *hs, CBB *out) {
  207. SSL *const ssl = hs->ssl;
  208. uint32_t mask_a, mask_k;
  209. ssl_get_client_disabled(hs, &mask_a, &mask_k);
  210. CBB child;
  211. if (!CBB_add_u16_length_prefixed(out, &child)) {
  212. return false;
  213. }
  214. // Add a fake cipher suite. See draft-davidben-tls-grease-01.
  215. if (ssl->ctx->grease_enabled &&
  216. !CBB_add_u16(&child, ssl_get_grease_value(hs, ssl_grease_cipher))) {
  217. return false;
  218. }
  219. // Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
  220. // hardware support.
  221. if (hs->max_version >= TLS1_3_VERSION) {
  222. if (!EVP_has_aes_hardware() &&
  223. !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
  224. return false;
  225. }
  226. if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
  227. !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
  228. return false;
  229. }
  230. if (EVP_has_aes_hardware() &&
  231. !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
  232. return false;
  233. }
  234. }
  235. if (hs->min_version < TLS1_3_VERSION) {
  236. bool any_enabled = false;
  237. for (const SSL_CIPHER *cipher : SSL_get_ciphers(ssl)) {
  238. // Skip disabled ciphers
  239. if ((cipher->algorithm_mkey & mask_k) ||
  240. (cipher->algorithm_auth & mask_a)) {
  241. continue;
  242. }
  243. if (SSL_CIPHER_get_min_version(cipher) > hs->max_version ||
  244. SSL_CIPHER_get_max_version(cipher) < hs->min_version) {
  245. continue;
  246. }
  247. any_enabled = true;
  248. if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
  249. return false;
  250. }
  251. }
  252. // If all ciphers were disabled, return the error to the caller.
  253. if (!any_enabled && hs->max_version < TLS1_3_VERSION) {
  254. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
  255. return false;
  256. }
  257. }
  258. if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
  259. if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
  260. return false;
  261. }
  262. }
  263. return CBB_flush(out);
  264. }
  265. bool ssl_write_client_hello(SSL_HANDSHAKE *hs) {
  266. SSL *const ssl = hs->ssl;
  267. ScopedCBB cbb;
  268. CBB body;
  269. if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO)) {
  270. return false;
  271. }
  272. CBB child;
  273. if (!CBB_add_u16(&body, hs->client_version) ||
  274. !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  275. !CBB_add_u8_length_prefixed(&body, &child)) {
  276. return false;
  277. }
  278. // Do not send a session ID on renegotiation.
  279. if (!ssl->s3->initial_handshake_complete &&
  280. !CBB_add_bytes(&child, hs->session_id, hs->session_id_len)) {
  281. return false;
  282. }
  283. if (SSL_is_dtls(ssl)) {
  284. if (!CBB_add_u8_length_prefixed(&body, &child) ||
  285. !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
  286. return false;
  287. }
  288. }
  289. size_t header_len =
  290. SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
  291. if (!ssl_write_client_cipher_list(hs, &body) ||
  292. !CBB_add_u8(&body, 1 /* one compression method */) ||
  293. !CBB_add_u8(&body, 0 /* null compression */) ||
  294. !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) {
  295. return false;
  296. }
  297. Array<uint8_t> msg;
  298. if (!ssl->method->finish_message(ssl, cbb.get(), &msg)) {
  299. return false;
  300. }
  301. // Now that the length prefixes have been computed, fill in the placeholder
  302. // PSK binder.
  303. if (hs->needs_psk_binder &&
  304. !tls13_write_psk_binder(hs, MakeSpan(msg))) {
  305. return false;
  306. }
  307. return ssl->method->add_message(ssl, std::move(msg));
  308. }
  309. static bool parse_supported_versions(SSL_HANDSHAKE *hs, uint16_t *version,
  310. const CBS *in) {
  311. // If the outer version is not TLS 1.2, or there is no extensions block, use
  312. // the outer version.
  313. if (*version != TLS1_2_VERSION || CBS_len(in) == 0) {
  314. return true;
  315. }
  316. SSL *const ssl = hs->ssl;
  317. CBS copy = *in, extensions;
  318. if (!CBS_get_u16_length_prefixed(&copy, &extensions) ||
  319. CBS_len(&copy) != 0) {
  320. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  321. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  322. return false;
  323. }
  324. bool have_supported_versions;
  325. CBS supported_versions;
  326. const SSL_EXTENSION_TYPE ext_types[] = {
  327. {TLSEXT_TYPE_supported_versions, &have_supported_versions,
  328. &supported_versions},
  329. };
  330. uint8_t alert = SSL_AD_DECODE_ERROR;
  331. if (!ssl_parse_extensions(&extensions, &alert, ext_types,
  332. OPENSSL_ARRAY_SIZE(ext_types),
  333. 1 /* ignore unknown */)) {
  334. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  335. return false;
  336. }
  337. // Override the outer version with the extension, if present.
  338. if (have_supported_versions &&
  339. (!CBS_get_u16(&supported_versions, version) ||
  340. CBS_len(&supported_versions) != 0)) {
  341. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  342. return false;
  343. }
  344. return true;
  345. }
  346. static enum ssl_hs_wait_t do_start_connect(SSL_HANDSHAKE *hs) {
  347. SSL *const ssl = hs->ssl;
  348. ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
  349. // |session_reused| must be reset in case this is a renegotiation.
  350. ssl->s3->session_reused = false;
  351. // Freeze the version range.
  352. if (!ssl_get_version_range(hs, &hs->min_version, &hs->max_version)) {
  353. return ssl_hs_error;
  354. }
  355. // Always advertise the ClientHello version from the original maximum version,
  356. // even on renegotiation. The static RSA key exchange uses this field, and
  357. // some servers fail when it changes across handshakes.
  358. if (SSL_is_dtls(hs->ssl)) {
  359. hs->client_version =
  360. hs->max_version >= TLS1_2_VERSION ? DTLS1_2_VERSION : DTLS1_VERSION;
  361. } else {
  362. hs->client_version =
  363. hs->max_version >= TLS1_2_VERSION ? TLS1_2_VERSION : hs->max_version;
  364. }
  365. // If the configured session has expired or was created at a disabled
  366. // version, drop it.
  367. if (ssl->session != NULL) {
  368. if (ssl->session->is_server ||
  369. !ssl_supports_version(hs, ssl->session->ssl_version) ||
  370. (ssl->session->session_id_length == 0 &&
  371. ssl->session->ticket.empty()) ||
  372. ssl->session->not_resumable ||
  373. !ssl_session_is_time_valid(ssl, ssl->session.get())) {
  374. ssl_set_session(ssl, NULL);
  375. }
  376. }
  377. if (!RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
  378. return ssl_hs_error;
  379. }
  380. if (ssl->session != nullptr &&
  381. !ssl->s3->initial_handshake_complete &&
  382. ssl->session->session_id_length > 0) {
  383. hs->session_id_len = ssl->session->session_id_length;
  384. OPENSSL_memcpy(hs->session_id, ssl->session->session_id,
  385. hs->session_id_len);
  386. } else if (hs->max_version >= TLS1_3_VERSION) {
  387. // Initialize a random session ID.
  388. hs->session_id_len = sizeof(hs->session_id);
  389. if (!RAND_bytes(hs->session_id, hs->session_id_len)) {
  390. return ssl_hs_error;
  391. }
  392. }
  393. if (!ssl_write_client_hello(hs)) {
  394. return ssl_hs_error;
  395. }
  396. hs->state = state_enter_early_data;
  397. return ssl_hs_flush;
  398. }
  399. static enum ssl_hs_wait_t do_enter_early_data(SSL_HANDSHAKE *hs) {
  400. SSL *const ssl = hs->ssl;
  401. if (SSL_is_dtls(ssl)) {
  402. hs->state = state_read_hello_verify_request;
  403. return ssl_hs_ok;
  404. }
  405. if (!hs->early_data_offered) {
  406. hs->state = state_read_server_hello;
  407. return ssl_hs_ok;
  408. }
  409. ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->session->ssl_version);
  410. if (!ssl->method->add_change_cipher_spec(ssl)) {
  411. return ssl_hs_error;
  412. }
  413. if (!tls13_init_early_key_schedule(
  414. hs, MakeConstSpan(ssl->session->master_key,
  415. ssl->session->master_key_length)) ||
  416. !tls13_derive_early_secret(hs) ||
  417. !tls13_set_early_secret_for_quic(hs)) {
  418. return ssl_hs_error;
  419. }
  420. if (ssl->quic_method == nullptr &&
  421. !tls13_set_traffic_key(ssl, ssl_encryption_early_data, evp_aead_seal,
  422. hs->early_traffic_secret())) {
  423. return ssl_hs_error;
  424. }
  425. // Stash the early data session, so connection properties may be queried out
  426. // of it.
  427. hs->early_session = UpRef(ssl->session);
  428. hs->state = state_early_reverify_server_certificate;
  429. return ssl_hs_ok;
  430. }
  431. static enum ssl_hs_wait_t do_early_reverify_server_certificate(SSL_HANDSHAKE *hs) {
  432. if (hs->ssl->ctx->reverify_on_resume) {
  433. switch (ssl_reverify_peer_cert(hs)) {
  434. case ssl_verify_ok:
  435. break;
  436. case ssl_verify_invalid:
  437. return ssl_hs_error;
  438. case ssl_verify_retry:
  439. hs->state = state_early_reverify_server_certificate;
  440. return ssl_hs_certificate_verify;
  441. }
  442. }
  443. hs->in_early_data = true;
  444. hs->can_early_write = true;
  445. hs->state = state_read_server_hello;
  446. return ssl_hs_early_return;
  447. }
  448. static enum ssl_hs_wait_t do_read_hello_verify_request(SSL_HANDSHAKE *hs) {
  449. SSL *const ssl = hs->ssl;
  450. assert(SSL_is_dtls(ssl));
  451. SSLMessage msg;
  452. if (!ssl->method->get_message(ssl, &msg)) {
  453. return ssl_hs_read_message;
  454. }
  455. if (msg.type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
  456. hs->state = state_read_server_hello;
  457. return ssl_hs_ok;
  458. }
  459. CBS hello_verify_request = msg.body, cookie;
  460. uint16_t server_version;
  461. if (!CBS_get_u16(&hello_verify_request, &server_version) ||
  462. !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
  463. CBS_len(&cookie) > sizeof(ssl->d1->cookie) ||
  464. CBS_len(&hello_verify_request) != 0) {
  465. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  466. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  467. return ssl_hs_error;
  468. }
  469. OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
  470. ssl->d1->cookie_len = CBS_len(&cookie);
  471. ssl->method->next_message(ssl);
  472. // DTLS resets the handshake buffer after HelloVerifyRequest.
  473. if (!hs->transcript.Init()) {
  474. return ssl_hs_error;
  475. }
  476. if (!ssl_write_client_hello(hs)) {
  477. return ssl_hs_error;
  478. }
  479. hs->state = state_read_server_hello;
  480. return ssl_hs_flush;
  481. }
  482. static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) {
  483. SSL *const ssl = hs->ssl;
  484. SSLMessage msg;
  485. if (!ssl->method->get_message(ssl, &msg)) {
  486. return ssl_hs_read_server_hello;
  487. }
  488. if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) {
  489. return ssl_hs_error;
  490. }
  491. CBS server_hello = msg.body, server_random, session_id;
  492. uint16_t server_version, cipher_suite;
  493. uint8_t compression_method;
  494. if (!CBS_get_u16(&server_hello, &server_version) ||
  495. !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
  496. !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
  497. CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
  498. !CBS_get_u16(&server_hello, &cipher_suite) ||
  499. !CBS_get_u8(&server_hello, &compression_method)) {
  500. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  501. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  502. return ssl_hs_error;
  503. }
  504. // Use the supported_versions extension if applicable.
  505. if (!parse_supported_versions(hs, &server_version, &server_hello)) {
  506. return ssl_hs_error;
  507. }
  508. if (!ssl_supports_version(hs, server_version)) {
  509. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
  510. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
  511. return ssl_hs_error;
  512. }
  513. assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
  514. if (!ssl->s3->have_version) {
  515. ssl->version = server_version;
  516. // At this point, the connection's version is known and ssl->version is
  517. // fixed. Begin enforcing the record-layer version.
  518. ssl->s3->have_version = true;
  519. ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
  520. } else if (server_version != ssl->version) {
  521. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
  522. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
  523. return ssl_hs_error;
  524. }
  525. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  526. hs->state = state_tls13;
  527. return ssl_hs_ok;
  528. }
  529. // Clear some TLS 1.3 state that no longer needs to be retained.
  530. hs->key_shares[0].reset();
  531. hs->key_shares[1].reset();
  532. hs->key_share_bytes.Reset();
  533. // A TLS 1.2 server would not know to skip the early data we offered. Report
  534. // an error code sooner. The caller may use this error code to implement the
  535. // fallback described in RFC 8446 appendix D.3.
  536. if (hs->early_data_offered) {
  537. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA);
  538. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
  539. return ssl_hs_error;
  540. }
  541. // Copy over the server random.
  542. OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
  543. SSL3_RANDOM_SIZE);
  544. // Enforce the TLS 1.3 anti-downgrade feature.
  545. if (!ssl->s3->initial_handshake_complete &&
  546. ssl_supports_version(hs, TLS1_3_VERSION)) {
  547. static_assert(
  548. sizeof(kTLS12DowngradeRandom) == sizeof(kTLS13DowngradeRandom),
  549. "downgrade signals have different size");
  550. static_assert(
  551. sizeof(kJDK11DowngradeRandom) == sizeof(kTLS13DowngradeRandom),
  552. "downgrade signals have different size");
  553. auto suffix =
  554. MakeConstSpan(ssl->s3->server_random, sizeof(ssl->s3->server_random))
  555. .subspan(SSL3_RANDOM_SIZE - sizeof(kTLS13DowngradeRandom));
  556. if (suffix == kTLS12DowngradeRandom || suffix == kTLS13DowngradeRandom ||
  557. suffix == kJDK11DowngradeRandom) {
  558. ssl->s3->tls13_downgrade = true;
  559. if (!hs->config->ignore_tls13_downgrade) {
  560. OPENSSL_PUT_ERROR(SSL, SSL_R_TLS13_DOWNGRADE);
  561. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  562. return ssl_hs_error;
  563. }
  564. }
  565. }
  566. if (!ssl->s3->initial_handshake_complete && ssl->session != nullptr &&
  567. ssl->session->session_id_length != 0 &&
  568. CBS_mem_equal(&session_id, ssl->session->session_id,
  569. ssl->session->session_id_length)) {
  570. ssl->s3->session_reused = true;
  571. } else {
  572. // The server may also have echoed back the TLS 1.3 compatibility mode
  573. // session ID. As we know this is not a session the server knows about, any
  574. // server resuming it is in error. Reject the first connection
  575. // deterministicly, rather than installing an invalid session into the
  576. // session cache. https://crbug.com/796910
  577. if (hs->session_id_len != 0 &&
  578. CBS_mem_equal(&session_id, hs->session_id, hs->session_id_len)) {
  579. OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_ECHOED_INVALID_SESSION_ID);
  580. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  581. return ssl_hs_error;
  582. }
  583. // The session wasn't resumed. Create a fresh SSL_SESSION to
  584. // fill out.
  585. ssl_set_session(ssl, NULL);
  586. if (!ssl_get_new_session(hs, 0 /* client */)) {
  587. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  588. return ssl_hs_error;
  589. }
  590. // Note: session_id could be empty.
  591. hs->new_session->session_id_length = CBS_len(&session_id);
  592. OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id),
  593. CBS_len(&session_id));
  594. }
  595. const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite);
  596. if (cipher == NULL) {
  597. // unknown cipher
  598. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
  599. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  600. return ssl_hs_error;
  601. }
  602. // The cipher must be allowed in the selected version and enabled.
  603. uint32_t mask_a, mask_k;
  604. ssl_get_client_disabled(hs, &mask_a, &mask_k);
  605. if ((cipher->algorithm_mkey & mask_k) || (cipher->algorithm_auth & mask_a) ||
  606. SSL_CIPHER_get_min_version(cipher) > ssl_protocol_version(ssl) ||
  607. SSL_CIPHER_get_max_version(cipher) < ssl_protocol_version(ssl) ||
  608. !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, cipher)) {
  609. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
  610. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  611. return ssl_hs_error;
  612. }
  613. if (ssl->session != NULL) {
  614. if (ssl->session->ssl_version != ssl->version) {
  615. OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
  616. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  617. return ssl_hs_error;
  618. }
  619. if (ssl->session->cipher != cipher) {
  620. OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  621. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  622. return ssl_hs_error;
  623. }
  624. if (!ssl_session_is_context_valid(hs, ssl->session.get())) {
  625. // This is actually a client application bug.
  626. OPENSSL_PUT_ERROR(SSL,
  627. SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  628. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  629. return ssl_hs_error;
  630. }
  631. } else {
  632. hs->new_session->cipher = cipher;
  633. }
  634. hs->new_cipher = cipher;
  635. // Now that the cipher is known, initialize the handshake hash and hash the
  636. // ServerHello.
  637. if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
  638. !ssl_hash_message(hs, msg)) {
  639. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  640. return ssl_hs_error;
  641. }
  642. // If doing a full handshake, the server may request a client certificate
  643. // which requires hashing the handshake transcript. Otherwise, the handshake
  644. // buffer may be released.
  645. if (ssl->session != NULL ||
  646. !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  647. hs->transcript.FreeBuffer();
  648. }
  649. // Only the NULL compression algorithm is supported.
  650. if (compression_method != 0) {
  651. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  652. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  653. return ssl_hs_error;
  654. }
  655. // TLS extensions
  656. if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
  657. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  658. return ssl_hs_error;
  659. }
  660. // There should be nothing left over in the record.
  661. if (CBS_len(&server_hello) != 0) {
  662. // wrong packet length
  663. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  664. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  665. return ssl_hs_error;
  666. }
  667. if (ssl->session != NULL &&
  668. hs->extended_master_secret != ssl->session->extended_master_secret) {
  669. if (ssl->session->extended_master_secret) {
  670. OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
  671. } else {
  672. OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
  673. }
  674. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  675. return ssl_hs_error;
  676. }
  677. if (ssl->s3->token_binding_negotiated &&
  678. (!hs->extended_master_secret || !ssl->s3->send_connection_binding)) {
  679. OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_TB_WITHOUT_EMS_OR_RI);
  680. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
  681. return ssl_hs_error;
  682. }
  683. ssl->method->next_message(ssl);
  684. if (ssl->session != NULL) {
  685. if (ssl->ctx->reverify_on_resume &&
  686. ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  687. hs->state = state_reverify_server_certificate;
  688. } else {
  689. hs->state = state_read_session_ticket;
  690. }
  691. return ssl_hs_ok;
  692. }
  693. hs->state = state_read_server_certificate;
  694. return ssl_hs_ok;
  695. }
  696. static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
  697. enum ssl_hs_wait_t wait = tls13_client_handshake(hs);
  698. if (wait == ssl_hs_ok) {
  699. hs->state = state_finish_client_handshake;
  700. return ssl_hs_ok;
  701. }
  702. return wait;
  703. }
  704. static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) {
  705. SSL *const ssl = hs->ssl;
  706. if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  707. hs->state = state_read_certificate_status;
  708. return ssl_hs_ok;
  709. }
  710. SSLMessage msg;
  711. if (!ssl->method->get_message(ssl, &msg)) {
  712. return ssl_hs_read_message;
  713. }
  714. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
  715. !ssl_hash_message(hs, msg)) {
  716. return ssl_hs_error;
  717. }
  718. CBS body = msg.body;
  719. uint8_t alert = SSL_AD_DECODE_ERROR;
  720. if (!ssl_parse_cert_chain(&alert, &hs->new_session->certs, &hs->peer_pubkey,
  721. NULL, &body, ssl->ctx->pool)) {
  722. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  723. return ssl_hs_error;
  724. }
  725. if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0 ||
  726. CBS_len(&body) != 0 ||
  727. !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
  728. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  729. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  730. return ssl_hs_error;
  731. }
  732. if (!ssl_check_leaf_certificate(
  733. hs, hs->peer_pubkey.get(),
  734. sk_CRYPTO_BUFFER_value(hs->new_session->certs.get(), 0))) {
  735. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  736. return ssl_hs_error;
  737. }
  738. ssl->method->next_message(ssl);
  739. hs->state = state_read_certificate_status;
  740. return ssl_hs_ok;
  741. }
  742. static enum ssl_hs_wait_t do_read_certificate_status(SSL_HANDSHAKE *hs) {
  743. SSL *const ssl = hs->ssl;
  744. if (!hs->certificate_status_expected) {
  745. hs->state = state_verify_server_certificate;
  746. return ssl_hs_ok;
  747. }
  748. SSLMessage msg;
  749. if (!ssl->method->get_message(ssl, &msg)) {
  750. return ssl_hs_read_message;
  751. }
  752. if (msg.type != SSL3_MT_CERTIFICATE_STATUS) {
  753. // A server may send status_request in ServerHello and then change its mind
  754. // about sending CertificateStatus.
  755. hs->state = state_verify_server_certificate;
  756. return ssl_hs_ok;
  757. }
  758. if (!ssl_hash_message(hs, msg)) {
  759. return ssl_hs_error;
  760. }
  761. CBS certificate_status = msg.body, ocsp_response;
  762. uint8_t status_type;
  763. if (!CBS_get_u8(&certificate_status, &status_type) ||
  764. status_type != TLSEXT_STATUSTYPE_ocsp ||
  765. !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
  766. CBS_len(&ocsp_response) == 0 ||
  767. CBS_len(&certificate_status) != 0) {
  768. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  769. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  770. return ssl_hs_error;
  771. }
  772. hs->new_session->ocsp_response.reset(
  773. CRYPTO_BUFFER_new_from_CBS(&ocsp_response, ssl->ctx->pool));
  774. if (hs->new_session->ocsp_response == nullptr) {
  775. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  776. return ssl_hs_error;
  777. }
  778. ssl->method->next_message(ssl);
  779. hs->state = state_verify_server_certificate;
  780. return ssl_hs_ok;
  781. }
  782. static enum ssl_hs_wait_t do_verify_server_certificate(SSL_HANDSHAKE *hs) {
  783. if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  784. hs->state = state_read_server_key_exchange;
  785. return ssl_hs_ok;
  786. }
  787. switch (ssl_verify_peer_cert(hs)) {
  788. case ssl_verify_ok:
  789. break;
  790. case ssl_verify_invalid:
  791. return ssl_hs_error;
  792. case ssl_verify_retry:
  793. hs->state = state_verify_server_certificate;
  794. return ssl_hs_certificate_verify;
  795. }
  796. hs->state = state_read_server_key_exchange;
  797. return ssl_hs_ok;
  798. }
  799. static enum ssl_hs_wait_t do_reverify_server_certificate(SSL_HANDSHAKE *hs) {
  800. assert(hs->ssl->ctx->reverify_on_resume);
  801. switch (ssl_reverify_peer_cert(hs)) {
  802. case ssl_verify_ok:
  803. break;
  804. case ssl_verify_invalid:
  805. return ssl_hs_error;
  806. case ssl_verify_retry:
  807. hs->state = state_reverify_server_certificate;
  808. return ssl_hs_certificate_verify;
  809. }
  810. hs->state = state_read_session_ticket;
  811. return ssl_hs_ok;
  812. }
  813. static enum ssl_hs_wait_t do_read_server_key_exchange(SSL_HANDSHAKE *hs) {
  814. SSL *const ssl = hs->ssl;
  815. SSLMessage msg;
  816. if (!ssl->method->get_message(ssl, &msg)) {
  817. return ssl_hs_read_message;
  818. }
  819. if (msg.type != SSL3_MT_SERVER_KEY_EXCHANGE) {
  820. // Some ciphers (pure PSK) have an optional ServerKeyExchange message.
  821. if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) {
  822. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  823. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  824. return ssl_hs_error;
  825. }
  826. hs->state = state_read_certificate_request;
  827. return ssl_hs_ok;
  828. }
  829. if (!ssl_hash_message(hs, msg)) {
  830. return ssl_hs_error;
  831. }
  832. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  833. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  834. CBS server_key_exchange = msg.body;
  835. if (alg_a & SSL_aPSK) {
  836. CBS psk_identity_hint;
  837. // Each of the PSK key exchanges begins with a psk_identity_hint.
  838. if (!CBS_get_u16_length_prefixed(&server_key_exchange,
  839. &psk_identity_hint)) {
  840. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  841. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  842. return ssl_hs_error;
  843. }
  844. // Store the PSK identity hint for the ClientKeyExchange. Assume that the
  845. // maximum length of a PSK identity hint can be as long as the maximum
  846. // length of a PSK identity. Also do not allow NULL characters; identities
  847. // are saved as C strings.
  848. //
  849. // TODO(davidben): Should invalid hints be ignored? It's a hint rather than
  850. // a specific identity.
  851. if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
  852. CBS_contains_zero_byte(&psk_identity_hint)) {
  853. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  854. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  855. return ssl_hs_error;
  856. }
  857. // Save non-empty identity hints as a C string. Empty identity hints we
  858. // treat as missing. Plain PSK makes it possible to send either no hint
  859. // (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
  860. // empty hint. Having different capabilities is odd, so we interpret empty
  861. // and missing as identical.
  862. char *raw = nullptr;
  863. if (CBS_len(&psk_identity_hint) != 0 &&
  864. !CBS_strdup(&psk_identity_hint, &raw)) {
  865. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  866. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  867. return ssl_hs_error;
  868. }
  869. hs->peer_psk_identity_hint.reset(raw);
  870. }
  871. if (alg_k & SSL_kECDHE) {
  872. // Parse the server parameters.
  873. uint8_t group_type;
  874. uint16_t group_id;
  875. CBS point;
  876. if (!CBS_get_u8(&server_key_exchange, &group_type) ||
  877. group_type != NAMED_CURVE_TYPE ||
  878. !CBS_get_u16(&server_key_exchange, &group_id) ||
  879. !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
  880. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  881. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  882. return ssl_hs_error;
  883. }
  884. hs->new_session->group_id = group_id;
  885. // Ensure the group is consistent with preferences.
  886. if (!tls1_check_group_id(hs, group_id)) {
  887. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
  888. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  889. return ssl_hs_error;
  890. }
  891. // Initialize ECDH and save the peer public key for later.
  892. hs->key_shares[0] = SSLKeyShare::Create(group_id);
  893. if (!hs->key_shares[0] ||
  894. !hs->peer_key.CopyFrom(point)) {
  895. return ssl_hs_error;
  896. }
  897. } else if (!(alg_k & SSL_kPSK)) {
  898. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  899. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  900. return ssl_hs_error;
  901. }
  902. // At this point, |server_key_exchange| contains the signature, if any, while
  903. // |msg.body| contains the entire message. From that, derive a CBS containing
  904. // just the parameter.
  905. CBS parameter;
  906. CBS_init(&parameter, CBS_data(&msg.body),
  907. CBS_len(&msg.body) - CBS_len(&server_key_exchange));
  908. // ServerKeyExchange should be signed by the server's public key.
  909. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  910. uint16_t signature_algorithm = 0;
  911. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  912. if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
  913. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  914. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  915. return ssl_hs_error;
  916. }
  917. uint8_t alert = SSL_AD_DECODE_ERROR;
  918. if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
  919. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  920. return ssl_hs_error;
  921. }
  922. hs->new_session->peer_signature_algorithm = signature_algorithm;
  923. } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
  924. hs->peer_pubkey.get())) {
  925. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
  926. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
  927. return ssl_hs_error;
  928. }
  929. // The last field in |server_key_exchange| is the signature.
  930. CBS signature;
  931. if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
  932. CBS_len(&server_key_exchange) != 0) {
  933. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  934. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  935. return ssl_hs_error;
  936. }
  937. ScopedCBB transcript;
  938. Array<uint8_t> transcript_data;
  939. if (!CBB_init(transcript.get(),
  940. 2 * SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
  941. !CBB_add_bytes(transcript.get(), ssl->s3->client_random,
  942. SSL3_RANDOM_SIZE) ||
  943. !CBB_add_bytes(transcript.get(), ssl->s3->server_random,
  944. SSL3_RANDOM_SIZE) ||
  945. !CBB_add_bytes(transcript.get(), CBS_data(&parameter),
  946. CBS_len(&parameter)) ||
  947. !CBBFinishArray(transcript.get(), &transcript_data)) {
  948. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  949. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  950. return ssl_hs_error;
  951. }
  952. if (!ssl_public_key_verify(ssl, signature, signature_algorithm,
  953. hs->peer_pubkey.get(), transcript_data)) {
  954. // bad signature
  955. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
  956. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  957. return ssl_hs_error;
  958. }
  959. } else {
  960. // PSK ciphers are the only supported certificate-less ciphers.
  961. assert(alg_a == SSL_aPSK);
  962. if (CBS_len(&server_key_exchange) > 0) {
  963. OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
  964. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  965. return ssl_hs_error;
  966. }
  967. }
  968. ssl->method->next_message(ssl);
  969. hs->state = state_read_certificate_request;
  970. return ssl_hs_ok;
  971. }
  972. static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) {
  973. SSL *const ssl = hs->ssl;
  974. if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  975. hs->state = state_read_server_hello_done;
  976. return ssl_hs_ok;
  977. }
  978. SSLMessage msg;
  979. if (!ssl->method->get_message(ssl, &msg)) {
  980. return ssl_hs_read_message;
  981. }
  982. if (msg.type == SSL3_MT_SERVER_HELLO_DONE) {
  983. // If we get here we don't need the handshake buffer as we won't be doing
  984. // client auth.
  985. hs->transcript.FreeBuffer();
  986. hs->state = state_read_server_hello_done;
  987. return ssl_hs_ok;
  988. }
  989. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_REQUEST) ||
  990. !ssl_hash_message(hs, msg)) {
  991. return ssl_hs_error;
  992. }
  993. // Get the certificate types.
  994. CBS body = msg.body, certificate_types;
  995. if (!CBS_get_u8_length_prefixed(&body, &certificate_types)) {
  996. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  997. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  998. return ssl_hs_error;
  999. }
  1000. if (!hs->certificate_types.CopyFrom(certificate_types)) {
  1001. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1002. return ssl_hs_error;
  1003. }
  1004. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  1005. CBS supported_signature_algorithms;
  1006. if (!CBS_get_u16_length_prefixed(&body, &supported_signature_algorithms) ||
  1007. !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
  1008. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1009. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1010. return ssl_hs_error;
  1011. }
  1012. }
  1013. uint8_t alert = SSL_AD_DECODE_ERROR;
  1014. UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names =
  1015. ssl_parse_client_CA_list(ssl, &alert, &body);
  1016. if (!ca_names) {
  1017. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  1018. return ssl_hs_error;
  1019. }
  1020. if (CBS_len(&body) != 0) {
  1021. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1022. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1023. return ssl_hs_error;
  1024. }
  1025. hs->cert_request = true;
  1026. hs->ca_names = std::move(ca_names);
  1027. ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
  1028. ssl->method->next_message(ssl);
  1029. hs->state = state_read_server_hello_done;
  1030. return ssl_hs_ok;
  1031. }
  1032. static enum ssl_hs_wait_t do_read_server_hello_done(SSL_HANDSHAKE *hs) {
  1033. SSL *const ssl = hs->ssl;
  1034. SSLMessage msg;
  1035. if (!ssl->method->get_message(ssl, &msg)) {
  1036. return ssl_hs_read_message;
  1037. }
  1038. if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO_DONE) ||
  1039. !ssl_hash_message(hs, msg)) {
  1040. return ssl_hs_error;
  1041. }
  1042. // ServerHelloDone is empty.
  1043. if (CBS_len(&msg.body) != 0) {
  1044. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1045. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1046. return ssl_hs_error;
  1047. }
  1048. ssl->method->next_message(ssl);
  1049. hs->state = state_send_client_certificate;
  1050. return ssl_hs_ok;
  1051. }
  1052. static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) {
  1053. SSL *const ssl = hs->ssl;
  1054. // The peer didn't request a certificate.
  1055. if (!hs->cert_request) {
  1056. hs->state = state_send_client_key_exchange;
  1057. return ssl_hs_ok;
  1058. }
  1059. // Call cert_cb to update the certificate.
  1060. if (hs->config->cert->cert_cb != NULL) {
  1061. int rv = hs->config->cert->cert_cb(ssl, hs->config->cert->cert_cb_arg);
  1062. if (rv == 0) {
  1063. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1064. OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
  1065. return ssl_hs_error;
  1066. }
  1067. if (rv < 0) {
  1068. hs->state = state_send_client_certificate;
  1069. return ssl_hs_x509_lookup;
  1070. }
  1071. }
  1072. if (!ssl_has_certificate(hs)) {
  1073. // Without a client certificate, the handshake buffer may be released.
  1074. hs->transcript.FreeBuffer();
  1075. }
  1076. if (!ssl_on_certificate_selected(hs) ||
  1077. !ssl_output_cert_chain(hs)) {
  1078. return ssl_hs_error;
  1079. }
  1080. hs->state = state_send_client_key_exchange;
  1081. return ssl_hs_ok;
  1082. }
  1083. static_assert(sizeof(size_t) >= sizeof(unsigned),
  1084. "size_t is smaller than unsigned");
  1085. static enum ssl_hs_wait_t do_send_client_key_exchange(SSL_HANDSHAKE *hs) {
  1086. SSL *const ssl = hs->ssl;
  1087. ScopedCBB cbb;
  1088. CBB body;
  1089. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  1090. SSL3_MT_CLIENT_KEY_EXCHANGE)) {
  1091. return ssl_hs_error;
  1092. }
  1093. Array<uint8_t> pms;
  1094. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  1095. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  1096. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  1097. CRYPTO_BUFFER *leaf =
  1098. sk_CRYPTO_BUFFER_value(hs->new_session->certs.get(), 0);
  1099. CBS leaf_cbs;
  1100. CBS_init(&leaf_cbs, CRYPTO_BUFFER_data(leaf), CRYPTO_BUFFER_len(leaf));
  1101. // Check the key usage matches the cipher suite. We do this unconditionally
  1102. // for non-RSA certificates. In particular, it's needed to distinguish ECDH
  1103. // certificates, which we do not support, from ECDSA certificates.
  1104. // Historically, we have not checked RSA key usages, so it is controlled by
  1105. // a flag for now. See https://crbug.com/795089.
  1106. ssl_key_usage_t intended_use = (alg_k & SSL_kRSA)
  1107. ? key_usage_encipherment
  1108. : key_usage_digital_signature;
  1109. if (ssl->config->enforce_rsa_key_usage ||
  1110. EVP_PKEY_id(hs->peer_pubkey.get()) != EVP_PKEY_RSA) {
  1111. if (!ssl_cert_check_key_usage(&leaf_cbs, intended_use)) {
  1112. return ssl_hs_error;
  1113. }
  1114. }
  1115. }
  1116. // If using a PSK key exchange, prepare the pre-shared key.
  1117. unsigned psk_len = 0;
  1118. uint8_t psk[PSK_MAX_PSK_LEN];
  1119. if (alg_a & SSL_aPSK) {
  1120. if (hs->config->psk_client_callback == NULL) {
  1121. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
  1122. return ssl_hs_error;
  1123. }
  1124. char identity[PSK_MAX_IDENTITY_LEN + 1];
  1125. OPENSSL_memset(identity, 0, sizeof(identity));
  1126. psk_len = hs->config->psk_client_callback(
  1127. ssl, hs->peer_psk_identity_hint.get(), identity, sizeof(identity), psk,
  1128. sizeof(psk));
  1129. if (psk_len == 0) {
  1130. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
  1131. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1132. return ssl_hs_error;
  1133. }
  1134. assert(psk_len <= PSK_MAX_PSK_LEN);
  1135. hs->new_session->psk_identity.reset(BUF_strdup(identity));
  1136. if (hs->new_session->psk_identity == nullptr) {
  1137. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1138. return ssl_hs_error;
  1139. }
  1140. // Write out psk_identity.
  1141. CBB child;
  1142. if (!CBB_add_u16_length_prefixed(&body, &child) ||
  1143. !CBB_add_bytes(&child, (const uint8_t *)identity,
  1144. OPENSSL_strnlen(identity, sizeof(identity))) ||
  1145. !CBB_flush(&body)) {
  1146. return ssl_hs_error;
  1147. }
  1148. }
  1149. // Depending on the key exchange method, compute |pms|.
  1150. if (alg_k & SSL_kRSA) {
  1151. if (!pms.Init(SSL_MAX_MASTER_KEY_LENGTH)) {
  1152. return ssl_hs_error;
  1153. }
  1154. RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey.get());
  1155. if (rsa == NULL) {
  1156. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1157. return ssl_hs_error;
  1158. }
  1159. pms[0] = hs->client_version >> 8;
  1160. pms[1] = hs->client_version & 0xff;
  1161. if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
  1162. return ssl_hs_error;
  1163. }
  1164. CBB enc_pms;
  1165. uint8_t *ptr;
  1166. size_t enc_pms_len;
  1167. if (!CBB_add_u16_length_prefixed(&body, &enc_pms) ||
  1168. !CBB_reserve(&enc_pms, &ptr, RSA_size(rsa)) ||
  1169. !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms.data(),
  1170. pms.size(), RSA_PKCS1_PADDING) ||
  1171. !CBB_did_write(&enc_pms, enc_pms_len) ||
  1172. !CBB_flush(&body)) {
  1173. return ssl_hs_error;
  1174. }
  1175. } else if (alg_k & SSL_kECDHE) {
  1176. // Generate a keypair and serialize the public half.
  1177. CBB child;
  1178. if (!CBB_add_u8_length_prefixed(&body, &child)) {
  1179. return ssl_hs_error;
  1180. }
  1181. // Compute the premaster.
  1182. uint8_t alert = SSL_AD_DECODE_ERROR;
  1183. if (!hs->key_shares[0]->Accept(&child, &pms, &alert, hs->peer_key)) {
  1184. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  1185. return ssl_hs_error;
  1186. }
  1187. if (!CBB_flush(&body)) {
  1188. return ssl_hs_error;
  1189. }
  1190. // The key exchange state may now be discarded.
  1191. hs->key_shares[0].reset();
  1192. hs->key_shares[1].reset();
  1193. hs->peer_key.Reset();
  1194. } else if (alg_k & SSL_kPSK) {
  1195. // For plain PSK, other_secret is a block of 0s with the same length as
  1196. // the pre-shared key.
  1197. if (!pms.Init(psk_len)) {
  1198. return ssl_hs_error;
  1199. }
  1200. OPENSSL_memset(pms.data(), 0, pms.size());
  1201. } else {
  1202. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1203. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1204. return ssl_hs_error;
  1205. }
  1206. // For a PSK cipher suite, other_secret is combined with the pre-shared
  1207. // key.
  1208. if (alg_a & SSL_aPSK) {
  1209. ScopedCBB pms_cbb;
  1210. CBB child;
  1211. if (!CBB_init(pms_cbb.get(), 2 + psk_len + 2 + pms.size()) ||
  1212. !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
  1213. !CBB_add_bytes(&child, pms.data(), pms.size()) ||
  1214. !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
  1215. !CBB_add_bytes(&child, psk, psk_len) ||
  1216. !CBBFinishArray(pms_cbb.get(), &pms)) {
  1217. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1218. return ssl_hs_error;
  1219. }
  1220. }
  1221. // The message must be added to the finished hash before calculating the
  1222. // master secret.
  1223. if (!ssl_add_message_cbb(ssl, cbb.get())) {
  1224. return ssl_hs_error;
  1225. }
  1226. hs->new_session->master_key_length =
  1227. tls1_generate_master_secret(hs, hs->new_session->master_key, pms);
  1228. if (hs->new_session->master_key_length == 0) {
  1229. return ssl_hs_error;
  1230. }
  1231. hs->new_session->extended_master_secret = hs->extended_master_secret;
  1232. hs->state = state_send_client_certificate_verify;
  1233. return ssl_hs_ok;
  1234. }
  1235. static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) {
  1236. SSL *const ssl = hs->ssl;
  1237. if (!hs->cert_request || !ssl_has_certificate(hs)) {
  1238. hs->state = state_send_client_finished;
  1239. return ssl_hs_ok;
  1240. }
  1241. assert(ssl_has_private_key(hs));
  1242. ScopedCBB cbb;
  1243. CBB body, child;
  1244. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  1245. SSL3_MT_CERTIFICATE_VERIFY)) {
  1246. return ssl_hs_error;
  1247. }
  1248. uint16_t signature_algorithm;
  1249. if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
  1250. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1251. return ssl_hs_error;
  1252. }
  1253. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  1254. // Write out the digest type in TLS 1.2.
  1255. if (!CBB_add_u16(&body, signature_algorithm)) {
  1256. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1257. return ssl_hs_error;
  1258. }
  1259. }
  1260. // Set aside space for the signature.
  1261. const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
  1262. uint8_t *ptr;
  1263. if (!CBB_add_u16_length_prefixed(&body, &child) ||
  1264. !CBB_reserve(&child, &ptr, max_sig_len)) {
  1265. return ssl_hs_error;
  1266. }
  1267. size_t sig_len = max_sig_len;
  1268. switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
  1269. signature_algorithm,
  1270. hs->transcript.buffer())) {
  1271. case ssl_private_key_success:
  1272. break;
  1273. case ssl_private_key_failure:
  1274. return ssl_hs_error;
  1275. case ssl_private_key_retry:
  1276. hs->state = state_send_client_certificate_verify;
  1277. return ssl_hs_private_key_operation;
  1278. }
  1279. if (!CBB_did_write(&child, sig_len) ||
  1280. !ssl_add_message_cbb(ssl, cbb.get())) {
  1281. return ssl_hs_error;
  1282. }
  1283. // The handshake buffer is no longer necessary.
  1284. hs->transcript.FreeBuffer();
  1285. hs->state = state_send_client_finished;
  1286. return ssl_hs_ok;
  1287. }
  1288. static enum ssl_hs_wait_t do_send_client_finished(SSL_HANDSHAKE *hs) {
  1289. SSL *const ssl = hs->ssl;
  1290. // Resolve Channel ID first, before any non-idempotent operations.
  1291. if (ssl->s3->channel_id_valid) {
  1292. if (!ssl_do_channel_id_callback(hs)) {
  1293. return ssl_hs_error;
  1294. }
  1295. if (hs->config->channel_id_private == NULL) {
  1296. hs->state = state_send_client_finished;
  1297. return ssl_hs_channel_id_lookup;
  1298. }
  1299. }
  1300. if (!ssl->method->add_change_cipher_spec(ssl) ||
  1301. !tls1_change_cipher_state(hs, evp_aead_seal)) {
  1302. return ssl_hs_error;
  1303. }
  1304. if (hs->next_proto_neg_seen) {
  1305. static const uint8_t kZero[32] = {0};
  1306. size_t padding_len =
  1307. 32 - ((ssl->s3->next_proto_negotiated.size() + 2) % 32);
  1308. ScopedCBB cbb;
  1309. CBB body, child;
  1310. if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_NEXT_PROTO) ||
  1311. !CBB_add_u8_length_prefixed(&body, &child) ||
  1312. !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated.data(),
  1313. ssl->s3->next_proto_negotiated.size()) ||
  1314. !CBB_add_u8_length_prefixed(&body, &child) ||
  1315. !CBB_add_bytes(&child, kZero, padding_len) ||
  1316. !ssl_add_message_cbb(ssl, cbb.get())) {
  1317. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1318. return ssl_hs_error;
  1319. }
  1320. }
  1321. if (ssl->s3->channel_id_valid) {
  1322. ScopedCBB cbb;
  1323. CBB body;
  1324. if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) ||
  1325. !tls1_write_channel_id(hs, &body) ||
  1326. !ssl_add_message_cbb(ssl, cbb.get())) {
  1327. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1328. return ssl_hs_error;
  1329. }
  1330. }
  1331. if (!ssl_send_finished(hs)) {
  1332. return ssl_hs_error;
  1333. }
  1334. hs->state = state_finish_flight;
  1335. return ssl_hs_flush;
  1336. }
  1337. static bool can_false_start(const SSL_HANDSHAKE *hs) {
  1338. SSL *const ssl = hs->ssl;
  1339. // False Start bypasses the Finished check's downgrade protection. This can
  1340. // enable attacks where we send data under weaker settings than supported
  1341. // (e.g. the Logjam attack). Thus we require TLS 1.2 with an ECDHE+AEAD
  1342. // cipher, our strongest settings before TLS 1.3.
  1343. //
  1344. // Now that TLS 1.3 exists, we would like to avoid similar attacks between
  1345. // TLS 1.2 and TLS 1.3, but there are too many TLS 1.2 deployments to
  1346. // sacrifice False Start on them. TLS 1.3's downgrade signal fixes this, but
  1347. // |SSL_CTX_set_ignore_tls13_downgrade| can disable it due to compatibility
  1348. // issues.
  1349. //
  1350. // |SSL_CTX_set_ignore_tls13_downgrade| normally still retains Finished-based
  1351. // downgrade protection, but False Start bypasses that. Thus, we disable False
  1352. // Start based on the TLS 1.3 downgrade signal, even if otherwise unenforced.
  1353. if (SSL_is_dtls(ssl) ||
  1354. SSL_version(ssl) != TLS1_2_VERSION ||
  1355. hs->new_cipher->algorithm_mkey != SSL_kECDHE ||
  1356. hs->new_cipher->algorithm_mac != SSL_AEAD ||
  1357. ssl->s3->tls13_downgrade) {
  1358. return false;
  1359. }
  1360. // Additionally require ALPN or NPN by default.
  1361. //
  1362. // TODO(davidben): Can this constraint be relaxed globally now that cipher
  1363. // suite requirements have been tightened?
  1364. if (!ssl->ctx->false_start_allowed_without_alpn &&
  1365. ssl->s3->alpn_selected.empty() &&
  1366. ssl->s3->next_proto_negotiated.empty()) {
  1367. return false;
  1368. }
  1369. return true;
  1370. }
  1371. static enum ssl_hs_wait_t do_finish_flight(SSL_HANDSHAKE *hs) {
  1372. SSL *const ssl = hs->ssl;
  1373. if (ssl->session != NULL) {
  1374. hs->state = state_finish_client_handshake;
  1375. return ssl_hs_ok;
  1376. }
  1377. // This is a full handshake. If it involves ChannelID, then record the
  1378. // handshake hashes at this point in the session so that any resumption of
  1379. // this session with ChannelID can sign those hashes.
  1380. if (!tls1_record_handshake_hashes_for_channel_id(hs)) {
  1381. return ssl_hs_error;
  1382. }
  1383. hs->state = state_read_session_ticket;
  1384. if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
  1385. can_false_start(hs) &&
  1386. // No False Start on renegotiation (would complicate the state machine).
  1387. !ssl->s3->initial_handshake_complete) {
  1388. hs->in_false_start = true;
  1389. hs->can_early_write = true;
  1390. return ssl_hs_early_return;
  1391. }
  1392. return ssl_hs_ok;
  1393. }
  1394. static enum ssl_hs_wait_t do_read_session_ticket(SSL_HANDSHAKE *hs) {
  1395. SSL *const ssl = hs->ssl;
  1396. if (!hs->ticket_expected) {
  1397. hs->state = state_process_change_cipher_spec;
  1398. return ssl_hs_read_change_cipher_spec;
  1399. }
  1400. SSLMessage msg;
  1401. if (!ssl->method->get_message(ssl, &msg)) {
  1402. return ssl_hs_read_message;
  1403. }
  1404. if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEW_SESSION_TICKET) ||
  1405. !ssl_hash_message(hs, msg)) {
  1406. return ssl_hs_error;
  1407. }
  1408. CBS new_session_ticket = msg.body, ticket;
  1409. uint32_t ticket_lifetime_hint;
  1410. if (!CBS_get_u32(&new_session_ticket, &ticket_lifetime_hint) ||
  1411. !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
  1412. CBS_len(&new_session_ticket) != 0) {
  1413. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1414. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1415. return ssl_hs_error;
  1416. }
  1417. if (CBS_len(&ticket) == 0) {
  1418. // RFC 5077 allows a server to change its mind and send no ticket after
  1419. // negotiating the extension. The value of |ticket_expected| is checked in
  1420. // |ssl_update_cache| so is cleared here to avoid an unnecessary update.
  1421. hs->ticket_expected = false;
  1422. ssl->method->next_message(ssl);
  1423. hs->state = state_process_change_cipher_spec;
  1424. return ssl_hs_read_change_cipher_spec;
  1425. }
  1426. SSL_SESSION *session = hs->new_session.get();
  1427. UniquePtr<SSL_SESSION> renewed_session;
  1428. if (ssl->session != NULL) {
  1429. // The server is sending a new ticket for an existing session. Sessions are
  1430. // immutable once established, so duplicate all but the ticket of the
  1431. // existing session.
  1432. renewed_session =
  1433. SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_INCLUDE_NONAUTH);
  1434. if (!renewed_session) {
  1435. // This should never happen.
  1436. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1437. return ssl_hs_error;
  1438. }
  1439. session = renewed_session.get();
  1440. }
  1441. // |ticket_lifetime_hint| is measured from when the ticket was issued.
  1442. ssl_session_rebase_time(ssl, session);
  1443. if (!session->ticket.CopyFrom(ticket)) {
  1444. return ssl_hs_error;
  1445. }
  1446. session->ticket_lifetime_hint = ticket_lifetime_hint;
  1447. // Generate a session ID for this session. Some callers expect all sessions to
  1448. // have a session ID. Additionally, it acts as the session ID to signal
  1449. // resumption.
  1450. SHA256(CBS_data(&ticket), CBS_len(&ticket), session->session_id);
  1451. session->session_id_length = SHA256_DIGEST_LENGTH;
  1452. if (renewed_session) {
  1453. session->not_resumable = false;
  1454. ssl->session = std::move(renewed_session);
  1455. }
  1456. ssl->method->next_message(ssl);
  1457. hs->state = state_process_change_cipher_spec;
  1458. return ssl_hs_read_change_cipher_spec;
  1459. }
  1460. static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
  1461. if (!tls1_change_cipher_state(hs, evp_aead_open)) {
  1462. return ssl_hs_error;
  1463. }
  1464. hs->state = state_read_server_finished;
  1465. return ssl_hs_ok;
  1466. }
  1467. static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) {
  1468. SSL *const ssl = hs->ssl;
  1469. enum ssl_hs_wait_t wait = ssl_get_finished(hs);
  1470. if (wait != ssl_hs_ok) {
  1471. return wait;
  1472. }
  1473. if (ssl->session != NULL) {
  1474. hs->state = state_send_client_finished;
  1475. return ssl_hs_ok;
  1476. }
  1477. hs->state = state_finish_client_handshake;
  1478. return ssl_hs_ok;
  1479. }
  1480. static enum ssl_hs_wait_t do_finish_client_handshake(SSL_HANDSHAKE *hs) {
  1481. SSL *const ssl = hs->ssl;
  1482. ssl->method->on_handshake_complete(ssl);
  1483. if (ssl->session != NULL) {
  1484. ssl->s3->established_session = UpRef(ssl->session);
  1485. } else {
  1486. // We make a copy of the session in order to maintain the immutability
  1487. // of the new established_session due to False Start. The caller may
  1488. // have taken a reference to the temporary session.
  1489. ssl->s3->established_session =
  1490. SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_DUP_ALL);
  1491. if (!ssl->s3->established_session) {
  1492. return ssl_hs_error;
  1493. }
  1494. // Renegotiations do not participate in session resumption.
  1495. if (!ssl->s3->initial_handshake_complete) {
  1496. ssl->s3->established_session->not_resumable = false;
  1497. }
  1498. hs->new_session.reset();
  1499. }
  1500. hs->handshake_finalized = true;
  1501. ssl->s3->initial_handshake_complete = true;
  1502. ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
  1503. hs->state = state_done;
  1504. return ssl_hs_ok;
  1505. }
  1506. enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs) {
  1507. while (hs->state != state_done) {
  1508. enum ssl_hs_wait_t ret = ssl_hs_error;
  1509. enum ssl_client_hs_state_t state =
  1510. static_cast<enum ssl_client_hs_state_t>(hs->state);
  1511. switch (state) {
  1512. case state_start_connect:
  1513. ret = do_start_connect(hs);
  1514. break;
  1515. case state_enter_early_data:
  1516. ret = do_enter_early_data(hs);
  1517. break;
  1518. case state_early_reverify_server_certificate:
  1519. ret = do_early_reverify_server_certificate(hs);
  1520. break;
  1521. case state_read_hello_verify_request:
  1522. ret = do_read_hello_verify_request(hs);
  1523. break;
  1524. case state_read_server_hello:
  1525. ret = do_read_server_hello(hs);
  1526. break;
  1527. case state_tls13:
  1528. ret = do_tls13(hs);
  1529. break;
  1530. case state_read_server_certificate:
  1531. ret = do_read_server_certificate(hs);
  1532. break;
  1533. case state_read_certificate_status:
  1534. ret = do_read_certificate_status(hs);
  1535. break;
  1536. case state_verify_server_certificate:
  1537. ret = do_verify_server_certificate(hs);
  1538. break;
  1539. case state_reverify_server_certificate:
  1540. ret = do_reverify_server_certificate(hs);
  1541. break;
  1542. case state_read_server_key_exchange:
  1543. ret = do_read_server_key_exchange(hs);
  1544. break;
  1545. case state_read_certificate_request:
  1546. ret = do_read_certificate_request(hs);
  1547. break;
  1548. case state_read_server_hello_done:
  1549. ret = do_read_server_hello_done(hs);
  1550. break;
  1551. case state_send_client_certificate:
  1552. ret = do_send_client_certificate(hs);
  1553. break;
  1554. case state_send_client_key_exchange:
  1555. ret = do_send_client_key_exchange(hs);
  1556. break;
  1557. case state_send_client_certificate_verify:
  1558. ret = do_send_client_certificate_verify(hs);
  1559. break;
  1560. case state_send_client_finished:
  1561. ret = do_send_client_finished(hs);
  1562. break;
  1563. case state_finish_flight:
  1564. ret = do_finish_flight(hs);
  1565. break;
  1566. case state_read_session_ticket:
  1567. ret = do_read_session_ticket(hs);
  1568. break;
  1569. case state_process_change_cipher_spec:
  1570. ret = do_process_change_cipher_spec(hs);
  1571. break;
  1572. case state_read_server_finished:
  1573. ret = do_read_server_finished(hs);
  1574. break;
  1575. case state_finish_client_handshake:
  1576. ret = do_finish_client_handshake(hs);
  1577. break;
  1578. case state_done:
  1579. ret = ssl_hs_ok;
  1580. break;
  1581. }
  1582. if (hs->state != state) {
  1583. ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1);
  1584. }
  1585. if (ret != ssl_hs_ok) {
  1586. return ret;
  1587. }
  1588. }
  1589. ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
  1590. return ssl_hs_ok;
  1591. }
  1592. const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs) {
  1593. enum ssl_client_hs_state_t state =
  1594. static_cast<enum ssl_client_hs_state_t>(hs->state);
  1595. switch (state) {
  1596. case state_start_connect:
  1597. return "TLS client start_connect";
  1598. case state_enter_early_data:
  1599. return "TLS client enter_early_data";
  1600. case state_early_reverify_server_certificate:
  1601. return "TLS client early_reverify_server_certificate";
  1602. case state_read_hello_verify_request:
  1603. return "TLS client read_hello_verify_request";
  1604. case state_read_server_hello:
  1605. return "TLS client read_server_hello";
  1606. case state_tls13:
  1607. return tls13_client_handshake_state(hs);
  1608. case state_read_server_certificate:
  1609. return "TLS client read_server_certificate";
  1610. case state_read_certificate_status:
  1611. return "TLS client read_certificate_status";
  1612. case state_verify_server_certificate:
  1613. return "TLS client verify_server_certificate";
  1614. case state_reverify_server_certificate:
  1615. return "TLS client reverify_server_certificate";
  1616. case state_read_server_key_exchange:
  1617. return "TLS client read_server_key_exchange";
  1618. case state_read_certificate_request:
  1619. return "TLS client read_certificate_request";
  1620. case state_read_server_hello_done:
  1621. return "TLS client read_server_hello_done";
  1622. case state_send_client_certificate:
  1623. return "TLS client send_client_certificate";
  1624. case state_send_client_key_exchange:
  1625. return "TLS client send_client_key_exchange";
  1626. case state_send_client_certificate_verify:
  1627. return "TLS client send_client_certificate_verify";
  1628. case state_send_client_finished:
  1629. return "TLS client send_client_finished";
  1630. case state_finish_flight:
  1631. return "TLS client finish_flight";
  1632. case state_read_session_ticket:
  1633. return "TLS client read_session_ticket";
  1634. case state_process_change_cipher_spec:
  1635. return "TLS client process_change_cipher_spec";
  1636. case state_read_server_finished:
  1637. return "TLS client read_server_finished";
  1638. case state_finish_client_handshake:
  1639. return "TLS client finish_client_handshake";
  1640. case state_done:
  1641. return "TLS client done";
  1642. }
  1643. return "TLS client unknown";
  1644. }
  1645. BSSL_NAMESPACE_END