Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

903 rindas
29 KiB

  1. /* Copyright (c) 2016, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. /* Per C99, various stdint.h macros are unavailable in C++ unless some macros
  15. * are defined. C++11 overruled this decision, but older Android NDKs still
  16. * require it. */
  17. #if !defined(__STDC_LIMIT_MACROS)
  18. #define __STDC_LIMIT_MACROS
  19. #endif
  20. #include <openssl/ssl.h>
  21. #include <assert.h>
  22. #include <string.h>
  23. #include <openssl/aead.h>
  24. #include <openssl/bytestring.h>
  25. #include <openssl/digest.h>
  26. #include <openssl/err.h>
  27. #include <openssl/mem.h>
  28. #include <openssl/rand.h>
  29. #include <openssl/stack.h>
  30. #include "../crypto/internal.h"
  31. #include "internal.h"
  32. enum server_hs_state_t {
  33. state_select_parameters = 0,
  34. state_select_session,
  35. state_send_hello_retry_request,
  36. state_process_second_client_hello,
  37. state_send_server_hello,
  38. state_send_server_certificate_verify,
  39. state_send_server_finished,
  40. state_read_second_client_flight,
  41. state_process_change_cipher_spec,
  42. state_process_end_of_early_data,
  43. state_process_client_certificate,
  44. state_process_client_certificate_verify,
  45. state_process_channel_id,
  46. state_process_client_finished,
  47. state_send_new_session_ticket,
  48. state_done,
  49. };
  50. static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0};
  51. static int resolve_ecdhe_secret(SSL_HANDSHAKE *hs, int *out_need_retry,
  52. SSL_CLIENT_HELLO *client_hello) {
  53. SSL *const ssl = hs->ssl;
  54. *out_need_retry = 0;
  55. /* We only support connections that include an ECDHE key exchange. */
  56. CBS key_share;
  57. if (!ssl_client_hello_get_extension(client_hello, &key_share,
  58. TLSEXT_TYPE_key_share)) {
  59. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE);
  60. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION);
  61. return 0;
  62. }
  63. int found_key_share;
  64. uint8_t *dhe_secret;
  65. size_t dhe_secret_len;
  66. uint8_t alert = SSL_AD_DECODE_ERROR;
  67. if (!ssl_ext_key_share_parse_clienthello(hs, &found_key_share, &dhe_secret,
  68. &dhe_secret_len, &alert,
  69. &key_share)) {
  70. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  71. return 0;
  72. }
  73. if (!found_key_share) {
  74. *out_need_retry = 1;
  75. return 0;
  76. }
  77. int ok = tls13_advance_key_schedule(hs, dhe_secret, dhe_secret_len);
  78. OPENSSL_free(dhe_secret);
  79. return ok;
  80. }
  81. static int ssl_ext_supported_versions_add_serverhello(SSL_HANDSHAKE *hs,
  82. CBB *out) {
  83. CBB contents;
  84. if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
  85. !CBB_add_u16_length_prefixed(out, &contents) ||
  86. !CBB_add_u16(&contents, hs->ssl->version) ||
  87. !CBB_flush(out)) {
  88. return 0;
  89. }
  90. return 1;
  91. }
  92. static const SSL_CIPHER *choose_tls13_cipher(
  93. const SSL *ssl, const SSL_CLIENT_HELLO *client_hello) {
  94. if (client_hello->cipher_suites_len % 2 != 0) {
  95. return NULL;
  96. }
  97. CBS cipher_suites;
  98. CBS_init(&cipher_suites, client_hello->cipher_suites,
  99. client_hello->cipher_suites_len);
  100. const int aes_is_fine = EVP_has_aes_hardware();
  101. const uint16_t version = ssl3_protocol_version(ssl);
  102. const SSL_CIPHER *best = NULL;
  103. while (CBS_len(&cipher_suites) > 0) {
  104. uint16_t cipher_suite;
  105. if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
  106. return NULL;
  107. }
  108. /* Limit to TLS 1.3 ciphers we know about. */
  109. const SSL_CIPHER *candidate = SSL_get_cipher_by_value(cipher_suite);
  110. if (candidate == NULL ||
  111. SSL_CIPHER_get_min_version(candidate) > version ||
  112. SSL_CIPHER_get_max_version(candidate) < version) {
  113. continue;
  114. }
  115. /* TLS 1.3 removes legacy ciphers, so honor the client order, but prefer
  116. * ChaCha20 if we do not have AES hardware. */
  117. if (aes_is_fine) {
  118. return candidate;
  119. }
  120. if (candidate->algorithm_enc == SSL_CHACHA20POLY1305) {
  121. return candidate;
  122. }
  123. if (best == NULL) {
  124. best = candidate;
  125. }
  126. }
  127. return best;
  128. }
  129. static int add_new_session_tickets(SSL_HANDSHAKE *hs) {
  130. SSL *const ssl = hs->ssl;
  131. /* TLS 1.3 recommends single-use tickets, so issue multiple tickets in case
  132. * the client makes several connections before getting a renewal. */
  133. static const int kNumTickets = 2;
  134. SSL_SESSION *session = hs->new_session;
  135. CBB cbb;
  136. CBB_zero(&cbb);
  137. /* Rebase the session timestamp so that it is measured from ticket
  138. * issuance. */
  139. ssl_session_rebase_time(ssl, session);
  140. for (int i = 0; i < kNumTickets; i++) {
  141. if (!RAND_bytes((uint8_t *)&session->ticket_age_add, 4)) {
  142. goto err;
  143. }
  144. session->ticket_age_add_valid = 1;
  145. CBB body, ticket, extensions;
  146. if (!ssl->method->init_message(ssl, &cbb, &body,
  147. SSL3_MT_NEW_SESSION_TICKET) ||
  148. !CBB_add_u32(&body, session->timeout) ||
  149. !CBB_add_u32(&body, session->ticket_age_add) ||
  150. !CBB_add_u16_length_prefixed(&body, &ticket) ||
  151. !ssl_encrypt_ticket(ssl, &ticket, session) ||
  152. !CBB_add_u16_length_prefixed(&body, &extensions)) {
  153. goto err;
  154. }
  155. if (ssl->cert->enable_early_data) {
  156. session->ticket_max_early_data = kMaxEarlyDataAccepted;
  157. CBB early_data_info;
  158. if (!CBB_add_u16(&extensions, TLSEXT_TYPE_ticket_early_data_info) ||
  159. !CBB_add_u16_length_prefixed(&extensions, &early_data_info) ||
  160. !CBB_add_u32(&early_data_info, session->ticket_max_early_data) ||
  161. !CBB_flush(&extensions)) {
  162. goto err;
  163. }
  164. }
  165. /* Add a fake extension. See draft-davidben-tls-grease-01. */
  166. if (!CBB_add_u16(&extensions,
  167. ssl_get_grease_value(ssl, ssl_grease_ticket_extension)) ||
  168. !CBB_add_u16(&extensions, 0 /* empty */)) {
  169. goto err;
  170. }
  171. if (!ssl_add_message_cbb(ssl, &cbb)) {
  172. goto err;
  173. }
  174. }
  175. return 1;
  176. err:
  177. CBB_cleanup(&cbb);
  178. return 0;
  179. }
  180. static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) {
  181. /* At this point, most ClientHello extensions have already been processed by
  182. * the common handshake logic. Resolve the remaining non-PSK parameters. */
  183. SSL *const ssl = hs->ssl;
  184. SSL_CLIENT_HELLO client_hello;
  185. if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
  186. ssl->init_num) ||
  187. client_hello.session_id_len > sizeof(hs->session_id)) {
  188. OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
  189. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  190. return ssl_hs_error;
  191. }
  192. OPENSSL_memcpy(hs->session_id, client_hello.session_id,
  193. client_hello.session_id_len);
  194. hs->session_id_len = client_hello.session_id_len;
  195. /* Negotiate the cipher suite. */
  196. hs->new_cipher = choose_tls13_cipher(ssl, &client_hello);
  197. if (hs->new_cipher == NULL) {
  198. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
  199. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  200. return ssl_hs_error;
  201. }
  202. /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
  203. * deferred. Complete it now. */
  204. uint8_t alert = SSL_AD_DECODE_ERROR;
  205. if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
  206. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  207. return ssl_hs_error;
  208. }
  209. /* The PRF hash is now known. Set up the key schedule and hash the
  210. * ClientHello. */
  211. if (!tls13_init_key_schedule(hs) ||
  212. !ssl_hash_current_message(hs)) {
  213. return ssl_hs_error;
  214. }
  215. hs->tls13_state = state_select_session;
  216. return ssl_hs_ok;
  217. }
  218. static enum ssl_ticket_aead_result_t select_session(
  219. SSL_HANDSHAKE *hs, uint8_t *out_alert, SSL_SESSION **out_session,
  220. int32_t *out_ticket_age_skew, const SSL_CLIENT_HELLO *client_hello) {
  221. SSL *const ssl = hs->ssl;
  222. *out_session = NULL;
  223. /* Decode the ticket if we agreed on a PSK key exchange mode. */
  224. CBS pre_shared_key;
  225. if (!hs->accept_psk_mode ||
  226. !ssl_client_hello_get_extension(client_hello, &pre_shared_key,
  227. TLSEXT_TYPE_pre_shared_key)) {
  228. return ssl_ticket_aead_ignore_ticket;
  229. }
  230. /* Verify that the pre_shared_key extension is the last extension in
  231. * ClientHello. */
  232. if (CBS_data(&pre_shared_key) + CBS_len(&pre_shared_key) !=
  233. client_hello->extensions + client_hello->extensions_len) {
  234. OPENSSL_PUT_ERROR(SSL, SSL_R_PRE_SHARED_KEY_MUST_BE_LAST);
  235. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  236. return ssl_ticket_aead_error;
  237. }
  238. CBS ticket, binders;
  239. uint32_t client_ticket_age;
  240. if (!ssl_ext_pre_shared_key_parse_clienthello(hs, &ticket, &binders,
  241. &client_ticket_age, out_alert,
  242. &pre_shared_key)) {
  243. return ssl_ticket_aead_error;
  244. }
  245. /* TLS 1.3 session tickets are renewed separately as part of the
  246. * NewSessionTicket. */
  247. int unused_renew;
  248. SSL_SESSION *session = NULL;
  249. enum ssl_ticket_aead_result_t ret =
  250. ssl_process_ticket(ssl, &session, &unused_renew, CBS_data(&ticket),
  251. CBS_len(&ticket), NULL, 0);
  252. switch (ret) {
  253. case ssl_ticket_aead_success:
  254. break;
  255. case ssl_ticket_aead_error:
  256. *out_alert = SSL_AD_INTERNAL_ERROR;
  257. return ret;
  258. default:
  259. return ret;
  260. }
  261. if (!ssl_session_is_resumable(hs, session) ||
  262. /* Historically, some TLS 1.3 tickets were missing ticket_age_add. */
  263. !session->ticket_age_add_valid) {
  264. SSL_SESSION_free(session);
  265. return ssl_ticket_aead_ignore_ticket;
  266. }
  267. /* Recover the client ticket age and convert to seconds. */
  268. client_ticket_age -= session->ticket_age_add;
  269. client_ticket_age /= 1000;
  270. struct OPENSSL_timeval now;
  271. ssl_get_current_time(ssl, &now);
  272. /* Compute the server ticket age in seconds. */
  273. assert(now.tv_sec >= session->time);
  274. uint64_t server_ticket_age = now.tv_sec - session->time;
  275. /* To avoid overflowing |hs->ticket_age_skew|, we will not resume
  276. * 68-year-old sessions. */
  277. if (server_ticket_age > INT32_MAX) {
  278. SSL_SESSION_free(session);
  279. return ssl_ticket_aead_ignore_ticket;
  280. }
  281. /* TODO(davidben,svaldez): Measure this value to decide on tolerance. For
  282. * now, accept all values. https://crbug.com/boringssl/113. */
  283. *out_ticket_age_skew =
  284. (int32_t)client_ticket_age - (int32_t)server_ticket_age;
  285. /* Check the PSK binder. */
  286. if (!tls13_verify_psk_binder(hs, session, &binders)) {
  287. SSL_SESSION_free(session);
  288. *out_alert = SSL_AD_DECRYPT_ERROR;
  289. return ssl_ticket_aead_error;
  290. }
  291. *out_session = session;
  292. return ssl_ticket_aead_success;
  293. }
  294. static enum ssl_hs_wait_t do_select_session(SSL_HANDSHAKE *hs) {
  295. SSL *const ssl = hs->ssl;
  296. SSL_CLIENT_HELLO client_hello;
  297. if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
  298. ssl->init_num)) {
  299. OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
  300. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  301. return ssl_hs_error;
  302. }
  303. uint8_t alert = SSL_AD_DECODE_ERROR;
  304. SSL_SESSION *session = NULL;
  305. switch (select_session(hs, &alert, &session, &ssl->s3->ticket_age_skew,
  306. &client_hello)) {
  307. case ssl_ticket_aead_ignore_ticket:
  308. assert(session == NULL);
  309. if (!ssl_get_new_session(hs, 1 /* server */)) {
  310. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  311. return ssl_hs_error;
  312. }
  313. break;
  314. case ssl_ticket_aead_success:
  315. /* Carry over authentication information from the previous handshake into
  316. * a fresh session. */
  317. hs->new_session = SSL_SESSION_dup(session, SSL_SESSION_DUP_AUTH_ONLY);
  318. if (/* Early data must be acceptable for this ticket. */
  319. ssl->cert->enable_early_data &&
  320. session->ticket_max_early_data != 0 &&
  321. /* The client must have offered early data. */
  322. hs->early_data_offered &&
  323. /* Channel ID is incompatible with 0-RTT. */
  324. !ssl->s3->tlsext_channel_id_valid &&
  325. /* The negotiated ALPN must match the one in the ticket. */
  326. ssl->s3->alpn_selected_len == session->early_alpn_len &&
  327. OPENSSL_memcmp(ssl->s3->alpn_selected, session->early_alpn,
  328. ssl->s3->alpn_selected_len) == 0) {
  329. ssl->early_data_accepted = 1;
  330. }
  331. SSL_SESSION_free(session);
  332. if (hs->new_session == NULL) {
  333. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  334. return ssl_hs_error;
  335. }
  336. ssl->s3->session_reused = 1;
  337. /* Resumption incorporates fresh key material, so refresh the timeout. */
  338. ssl_session_renew_timeout(ssl, hs->new_session,
  339. ssl->session_ctx->session_psk_dhe_timeout);
  340. break;
  341. case ssl_ticket_aead_error:
  342. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  343. return ssl_hs_error;
  344. case ssl_ticket_aead_retry:
  345. hs->tls13_state = state_select_session;
  346. return ssl_hs_pending_ticket;
  347. }
  348. /* Record connection properties in the new session. */
  349. hs->new_session->cipher = hs->new_cipher;
  350. if (hs->hostname != NULL) {
  351. OPENSSL_free(hs->new_session->tlsext_hostname);
  352. hs->new_session->tlsext_hostname = BUF_strdup(hs->hostname);
  353. if (hs->new_session->tlsext_hostname == NULL) {
  354. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  355. return ssl_hs_error;
  356. }
  357. }
  358. /* Store the initial negotiated ALPN in the session. */
  359. if (ssl->s3->alpn_selected != NULL) {
  360. hs->new_session->early_alpn = (uint8_t *)BUF_memdup(
  361. ssl->s3->alpn_selected, ssl->s3->alpn_selected_len);
  362. if (hs->new_session->early_alpn == NULL) {
  363. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  364. return ssl_hs_error;
  365. }
  366. hs->new_session->early_alpn_len = ssl->s3->alpn_selected_len;
  367. }
  368. if (ssl->ctx->dos_protection_cb != NULL &&
  369. ssl->ctx->dos_protection_cb(&client_hello) == 0) {
  370. /* Connection rejected for DOS reasons. */
  371. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
  372. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  373. return ssl_hs_error;
  374. }
  375. /* Incorporate the PSK into the running secret. */
  376. if (ssl->s3->session_reused) {
  377. if (!tls13_advance_key_schedule(hs, hs->new_session->master_key,
  378. hs->new_session->master_key_length)) {
  379. return ssl_hs_error;
  380. }
  381. } else if (!tls13_advance_key_schedule(hs, kZeroes, hs->hash_len)) {
  382. return ssl_hs_error;
  383. }
  384. if (ssl->early_data_accepted) {
  385. if (!tls13_derive_early_secrets(hs)) {
  386. return ssl_hs_error;
  387. }
  388. } else if (hs->early_data_offered) {
  389. ssl->s3->skip_early_data = 1;
  390. }
  391. ssl->method->received_flight(ssl);
  392. /* Resolve ECDHE and incorporate it into the secret. */
  393. int need_retry;
  394. if (!resolve_ecdhe_secret(hs, &need_retry, &client_hello)) {
  395. if (need_retry) {
  396. ssl->early_data_accepted = 0;
  397. ssl->s3->skip_early_data = 1;
  398. hs->tls13_state = state_send_hello_retry_request;
  399. return ssl_hs_ok;
  400. }
  401. return ssl_hs_error;
  402. }
  403. hs->tls13_state = state_send_server_hello;
  404. return ssl_hs_ok;
  405. }
  406. static enum ssl_hs_wait_t do_send_hello_retry_request(SSL_HANDSHAKE *hs) {
  407. SSL *const ssl = hs->ssl;
  408. CBB cbb, body, extensions;
  409. uint16_t group_id;
  410. if (!ssl->method->init_message(ssl, &cbb, &body,
  411. SSL3_MT_HELLO_RETRY_REQUEST) ||
  412. !CBB_add_u16(&body, ssl->version) ||
  413. !tls1_get_shared_group(hs, &group_id) ||
  414. !CBB_add_u16_length_prefixed(&body, &extensions) ||
  415. !CBB_add_u16(&extensions, TLSEXT_TYPE_key_share) ||
  416. !CBB_add_u16(&extensions, 2 /* length */) ||
  417. !CBB_add_u16(&extensions, group_id) ||
  418. !ssl_add_message_cbb(ssl, &cbb)) {
  419. CBB_cleanup(&cbb);
  420. return ssl_hs_error;
  421. }
  422. hs->tls13_state = state_process_second_client_hello;
  423. return ssl_hs_flush_and_read_message;
  424. }
  425. static enum ssl_hs_wait_t do_process_second_client_hello(SSL_HANDSHAKE *hs) {
  426. SSL *const ssl = hs->ssl;
  427. if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_HELLO)) {
  428. return ssl_hs_error;
  429. }
  430. SSL_CLIENT_HELLO client_hello;
  431. if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
  432. ssl->init_num)) {
  433. OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
  434. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  435. return ssl_hs_error;
  436. }
  437. int need_retry;
  438. if (!resolve_ecdhe_secret(hs, &need_retry, &client_hello)) {
  439. if (need_retry) {
  440. /* Only send one HelloRetryRequest. */
  441. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  442. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
  443. }
  444. return ssl_hs_error;
  445. }
  446. if (!ssl_hash_current_message(hs)) {
  447. return ssl_hs_error;
  448. }
  449. ssl->method->received_flight(ssl);
  450. hs->tls13_state = state_send_server_hello;
  451. return ssl_hs_ok;
  452. }
  453. static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
  454. SSL *const ssl = hs->ssl;
  455. uint16_t version = ssl->version;
  456. if (ssl->version == TLS1_3_EXPERIMENT_VERSION) {
  457. version = TLS1_2_VERSION;
  458. }
  459. /* Send a ServerHello. */
  460. CBB cbb, body, extensions, session_id;
  461. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) ||
  462. !CBB_add_u16(&body, version) ||
  463. !RAND_bytes(ssl->s3->server_random, sizeof(ssl->s3->server_random)) ||
  464. !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
  465. (ssl->version == TLS1_3_EXPERIMENT_VERSION &&
  466. (!CBB_add_u8_length_prefixed(&body, &session_id) ||
  467. !CBB_add_bytes(&session_id, hs->session_id, hs->session_id_len))) ||
  468. !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
  469. (ssl->version == TLS1_3_EXPERIMENT_VERSION && !CBB_add_u8(&body, 0)) ||
  470. !CBB_add_u16_length_prefixed(&body, &extensions) ||
  471. !ssl_ext_pre_shared_key_add_serverhello(hs, &extensions) ||
  472. !ssl_ext_key_share_add_serverhello(hs, &extensions) ||
  473. (ssl->version == TLS1_3_EXPERIMENT_VERSION &&
  474. !ssl_ext_supported_versions_add_serverhello(hs, &extensions)) ||
  475. !ssl_add_message_cbb(ssl, &cbb)) {
  476. goto err;
  477. }
  478. if (ssl->version == TLS1_3_EXPERIMENT_VERSION &&
  479. !ssl3_add_change_cipher_spec(ssl)) {
  480. goto err;
  481. }
  482. /* Derive and enable the handshake traffic secrets. */
  483. if (!tls13_derive_handshake_secrets(hs) ||
  484. !tls13_set_traffic_key(ssl, evp_aead_seal, hs->server_handshake_secret,
  485. hs->hash_len)) {
  486. goto err;
  487. }
  488. /* Send EncryptedExtensions. */
  489. if (!ssl->method->init_message(ssl, &cbb, &body,
  490. SSL3_MT_ENCRYPTED_EXTENSIONS) ||
  491. !ssl_add_serverhello_tlsext(hs, &body) ||
  492. !ssl_add_message_cbb(ssl, &cbb)) {
  493. goto err;
  494. }
  495. if (!ssl->s3->session_reused) {
  496. /* Determine whether to request a client certificate. */
  497. hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
  498. /* Only request a certificate if Channel ID isn't negotiated. */
  499. if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
  500. ssl->s3->tlsext_channel_id_valid) {
  501. hs->cert_request = 0;
  502. }
  503. }
  504. /* Send a CertificateRequest, if necessary. */
  505. if (hs->cert_request) {
  506. CBB sigalgs_cbb;
  507. if (!ssl->method->init_message(ssl, &cbb, &body,
  508. SSL3_MT_CERTIFICATE_REQUEST) ||
  509. !CBB_add_u8(&body, 0 /* no certificate_request_context. */) ||
  510. !CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
  511. !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) ||
  512. !ssl_add_client_CA_list(ssl, &body) ||
  513. !CBB_add_u16(&body, 0 /* empty certificate_extensions. */) ||
  514. !ssl_add_message_cbb(ssl, &cbb)) {
  515. goto err;
  516. }
  517. }
  518. /* Send the server Certificate message, if necessary. */
  519. if (!ssl->s3->session_reused) {
  520. if (!ssl_has_certificate(ssl)) {
  521. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
  522. goto err;
  523. }
  524. if (!tls13_add_certificate(hs)) {
  525. goto err;
  526. }
  527. hs->tls13_state = state_send_server_certificate_verify;
  528. return ssl_hs_ok;
  529. }
  530. hs->tls13_state = state_send_server_finished;
  531. return ssl_hs_ok;
  532. err:
  533. CBB_cleanup(&cbb);
  534. return ssl_hs_error;
  535. }
  536. static enum ssl_hs_wait_t do_send_server_certificate_verify(SSL_HANDSHAKE *hs) {
  537. switch (tls13_add_certificate_verify(hs)) {
  538. case ssl_private_key_success:
  539. hs->tls13_state = state_send_server_finished;
  540. return ssl_hs_ok;
  541. case ssl_private_key_retry:
  542. hs->tls13_state = state_send_server_certificate_verify;
  543. return ssl_hs_private_key_operation;
  544. case ssl_private_key_failure:
  545. return ssl_hs_error;
  546. }
  547. assert(0);
  548. return ssl_hs_error;
  549. }
  550. static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
  551. SSL *const ssl = hs->ssl;
  552. if (!tls13_add_finished(hs) ||
  553. /* Update the secret to the master secret and derive traffic keys. */
  554. !tls13_advance_key_schedule(hs, kZeroes, hs->hash_len) ||
  555. !tls13_derive_application_secrets(hs) ||
  556. !tls13_set_traffic_key(ssl, evp_aead_seal, hs->server_traffic_secret_0,
  557. hs->hash_len)) {
  558. return ssl_hs_error;
  559. }
  560. if (ssl->early_data_accepted) {
  561. /* If accepting 0-RTT, we send tickets half-RTT. This gets the tickets on
  562. * the wire sooner and also avoids triggering a write on |SSL_read| when
  563. * processing the client Finished. This requires computing the client
  564. * Finished early. See draft-ietf-tls-tls13-18, section 4.5.1. */
  565. size_t finished_len;
  566. if (!tls13_finished_mac(hs, hs->expected_client_finished, &finished_len,
  567. 0 /* client */)) {
  568. return ssl_hs_error;
  569. }
  570. if (finished_len != hs->hash_len) {
  571. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  572. return ssl_hs_error;
  573. }
  574. /* Feed the predicted Finished into the transcript. This allows us to derive
  575. * the resumption secret early and send half-RTT tickets.
  576. *
  577. * TODO(davidben): This will need to be updated for DTLS 1.3. */
  578. assert(!SSL_is_dtls(hs->ssl));
  579. assert(hs->hash_len <= 0xff);
  580. uint8_t header[4] = {SSL3_MT_FINISHED, 0, 0, static_cast<uint8_t>(hs->hash_len)};
  581. if (!SSL_TRANSCRIPT_update(&hs->transcript, header, sizeof(header)) ||
  582. !SSL_TRANSCRIPT_update(&hs->transcript, hs->expected_client_finished,
  583. hs->hash_len) ||
  584. !tls13_derive_resumption_secret(hs) ||
  585. !add_new_session_tickets(hs)) {
  586. return ssl_hs_error;
  587. }
  588. }
  589. hs->tls13_state = state_read_second_client_flight;
  590. return ssl_hs_flush;
  591. }
  592. static enum ssl_hs_wait_t do_read_second_client_flight(SSL_HANDSHAKE *hs) {
  593. SSL *const ssl = hs->ssl;
  594. if (ssl->early_data_accepted) {
  595. if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->early_traffic_secret,
  596. hs->hash_len)) {
  597. return ssl_hs_error;
  598. }
  599. hs->can_early_write = 1;
  600. hs->can_early_read = 1;
  601. hs->in_early_data = 1;
  602. hs->tls13_state = state_process_end_of_early_data;
  603. return ssl_hs_read_end_of_early_data;
  604. }
  605. hs->tls13_state = state_process_end_of_early_data;
  606. return ssl_hs_ok;
  607. }
  608. static enum ssl_hs_wait_t do_process_end_of_early_data(SSL_HANDSHAKE *hs) {
  609. hs->tls13_state = state_process_change_cipher_spec;
  610. /* If early data was accepted, the ChangeCipherSpec message will be in the
  611. * discarded early data. */
  612. if (hs->early_data_offered && !hs->ssl->early_data_accepted) {
  613. return ssl_hs_ok;
  614. }
  615. return hs->ssl->version == TLS1_3_EXPERIMENT_VERSION
  616. ? ssl_hs_read_change_cipher_spec
  617. : ssl_hs_ok;
  618. }
  619. static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
  620. SSL *const ssl = hs->ssl;
  621. if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->client_handshake_secret,
  622. hs->hash_len)) {
  623. return ssl_hs_error;
  624. }
  625. hs->tls13_state = ssl->early_data_accepted ? state_process_client_finished
  626. : state_process_client_certificate;
  627. return ssl_hs_read_message;
  628. }
  629. static enum ssl_hs_wait_t do_process_client_certificate(SSL_HANDSHAKE *hs) {
  630. SSL *const ssl = hs->ssl;
  631. if (!hs->cert_request) {
  632. /* OpenSSL returns X509_V_OK when no certificates are requested. This is
  633. * classed by them as a bug, but it's assumed by at least NGINX. */
  634. hs->new_session->verify_result = X509_V_OK;
  635. /* Skip this state. */
  636. hs->tls13_state = state_process_channel_id;
  637. return ssl_hs_ok;
  638. }
  639. const int allow_anonymous =
  640. (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) == 0;
  641. if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE) ||
  642. !tls13_process_certificate(hs, allow_anonymous) ||
  643. !ssl_hash_current_message(hs)) {
  644. return ssl_hs_error;
  645. }
  646. hs->tls13_state = state_process_client_certificate_verify;
  647. return ssl_hs_read_message;
  648. }
  649. static enum ssl_hs_wait_t do_process_client_certificate_verify(
  650. SSL_HANDSHAKE *hs) {
  651. SSL *const ssl = hs->ssl;
  652. if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
  653. /* Skip this state. */
  654. hs->tls13_state = state_process_channel_id;
  655. return ssl_hs_ok;
  656. }
  657. if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY) ||
  658. !tls13_process_certificate_verify(hs) ||
  659. !ssl_hash_current_message(hs)) {
  660. return ssl_hs_error;
  661. }
  662. hs->tls13_state = state_process_channel_id;
  663. return ssl_hs_read_message;
  664. }
  665. static enum ssl_hs_wait_t do_process_channel_id(SSL_HANDSHAKE *hs) {
  666. if (!hs->ssl->s3->tlsext_channel_id_valid) {
  667. hs->tls13_state = state_process_client_finished;
  668. return ssl_hs_ok;
  669. }
  670. if (!ssl_check_message_type(hs->ssl, SSL3_MT_CHANNEL_ID) ||
  671. !tls1_verify_channel_id(hs) ||
  672. !ssl_hash_current_message(hs)) {
  673. return ssl_hs_error;
  674. }
  675. hs->tls13_state = state_process_client_finished;
  676. return ssl_hs_read_message;
  677. }
  678. static enum ssl_hs_wait_t do_process_client_finished(SSL_HANDSHAKE *hs) {
  679. SSL *const ssl = hs->ssl;
  680. if (!ssl_check_message_type(ssl, SSL3_MT_FINISHED) ||
  681. /* If early data was accepted, we've already computed the client Finished
  682. * and derived the resumption secret. */
  683. !tls13_process_finished(hs, ssl->early_data_accepted) ||
  684. /* evp_aead_seal keys have already been switched. */
  685. !tls13_set_traffic_key(ssl, evp_aead_open, hs->client_traffic_secret_0,
  686. hs->hash_len)) {
  687. return ssl_hs_error;
  688. }
  689. ssl->method->received_flight(ssl);
  690. if (!ssl->early_data_accepted) {
  691. if (!ssl_hash_current_message(hs) ||
  692. !tls13_derive_resumption_secret(hs)) {
  693. return ssl_hs_error;
  694. }
  695. /* We send post-handshake tickets as part of the handshake in 1-RTT. */
  696. hs->tls13_state = state_send_new_session_ticket;
  697. return ssl_hs_ok;
  698. }
  699. hs->tls13_state = state_done;
  700. return ssl_hs_ok;
  701. }
  702. static enum ssl_hs_wait_t do_send_new_session_ticket(SSL_HANDSHAKE *hs) {
  703. /* If the client doesn't accept resumption with PSK_DHE_KE, don't send a
  704. * session ticket. */
  705. if (!hs->accept_psk_mode) {
  706. hs->tls13_state = state_done;
  707. return ssl_hs_ok;
  708. }
  709. if (!add_new_session_tickets(hs)) {
  710. return ssl_hs_error;
  711. }
  712. hs->tls13_state = state_done;
  713. return ssl_hs_flush;
  714. }
  715. enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs) {
  716. while (hs->tls13_state != state_done) {
  717. enum ssl_hs_wait_t ret = ssl_hs_error;
  718. enum server_hs_state_t state =
  719. static_cast<enum server_hs_state_t>(hs->tls13_state);
  720. switch (state) {
  721. case state_select_parameters:
  722. ret = do_select_parameters(hs);
  723. break;
  724. case state_select_session:
  725. ret = do_select_session(hs);
  726. break;
  727. case state_send_hello_retry_request:
  728. ret = do_send_hello_retry_request(hs);
  729. break;
  730. case state_process_second_client_hello:
  731. ret = do_process_second_client_hello(hs);
  732. break;
  733. case state_send_server_hello:
  734. ret = do_send_server_hello(hs);
  735. break;
  736. case state_send_server_certificate_verify:
  737. ret = do_send_server_certificate_verify(hs);
  738. break;
  739. case state_send_server_finished:
  740. ret = do_send_server_finished(hs);
  741. break;
  742. case state_read_second_client_flight:
  743. ret = do_read_second_client_flight(hs);
  744. break;
  745. case state_process_end_of_early_data:
  746. ret = do_process_end_of_early_data(hs);
  747. break;
  748. case state_process_change_cipher_spec:
  749. ret = do_process_change_cipher_spec(hs);
  750. break;
  751. case state_process_client_certificate:
  752. ret = do_process_client_certificate(hs);
  753. break;
  754. case state_process_client_certificate_verify:
  755. ret = do_process_client_certificate_verify(hs);
  756. break;
  757. case state_process_channel_id:
  758. ret = do_process_channel_id(hs);
  759. break;
  760. case state_process_client_finished:
  761. ret = do_process_client_finished(hs);
  762. break;
  763. case state_send_new_session_ticket:
  764. ret = do_send_new_session_ticket(hs);
  765. break;
  766. case state_done:
  767. ret = ssl_hs_ok;
  768. break;
  769. }
  770. if (ret != ssl_hs_ok) {
  771. return ret;
  772. }
  773. }
  774. return ssl_hs_ok;
  775. }