tls13_server.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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. #include <openssl/ssl.h>
  15. #include <assert.h>
  16. #include <string.h>
  17. #include <openssl/aead.h>
  18. #include <openssl/bytestring.h>
  19. #include <openssl/digest.h>
  20. #include <openssl/err.h>
  21. #include <openssl/mem.h>
  22. #include <openssl/rand.h>
  23. #include <openssl/stack.h>
  24. #include "../crypto/internal.h"
  25. #include "internal.h"
  26. /* kMaxEarlyDataAccepted is the advertised number of plaintext bytes of early
  27. * data that will be accepted. This value should be slightly below
  28. * kMaxEarlyDataSkipped in tls_record.c, which is measured in ciphertext. */
  29. static const size_t kMaxEarlyDataAccepted = 14336;
  30. enum server_hs_state_t {
  31. state_select_parameters = 0,
  32. state_send_hello_retry_request,
  33. state_process_second_client_hello,
  34. state_send_server_hello,
  35. state_send_server_certificate_verify,
  36. state_complete_server_certificate_verify,
  37. state_send_server_finished,
  38. state_process_client_certificate,
  39. state_process_client_certificate_verify,
  40. state_process_channel_id,
  41. state_process_client_finished,
  42. state_send_new_session_ticket,
  43. state_done,
  44. };
  45. static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0};
  46. static int resolve_ecdhe_secret(SSL_HANDSHAKE *hs, int *out_need_retry,
  47. SSL_CLIENT_HELLO *client_hello) {
  48. SSL *const ssl = hs->ssl;
  49. *out_need_retry = 0;
  50. /* We only support connections that include an ECDHE key exchange. */
  51. CBS key_share;
  52. if (!ssl_client_hello_get_extension(client_hello, &key_share,
  53. TLSEXT_TYPE_key_share)) {
  54. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE);
  55. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION);
  56. return 0;
  57. }
  58. int found_key_share;
  59. uint8_t *dhe_secret;
  60. size_t dhe_secret_len;
  61. uint8_t alert = SSL_AD_DECODE_ERROR;
  62. if (!ssl_ext_key_share_parse_clienthello(hs, &found_key_share, &dhe_secret,
  63. &dhe_secret_len, &alert,
  64. &key_share)) {
  65. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  66. return 0;
  67. }
  68. if (!found_key_share) {
  69. *out_need_retry = 1;
  70. return 0;
  71. }
  72. int ok = tls13_advance_key_schedule(hs, dhe_secret, dhe_secret_len);
  73. OPENSSL_free(dhe_secret);
  74. return ok;
  75. }
  76. static const SSL_CIPHER *choose_tls13_cipher(
  77. const SSL *ssl, const SSL_CLIENT_HELLO *client_hello) {
  78. if (client_hello->cipher_suites_len % 2 != 0) {
  79. return NULL;
  80. }
  81. CBS cipher_suites;
  82. CBS_init(&cipher_suites, client_hello->cipher_suites,
  83. client_hello->cipher_suites_len);
  84. const int aes_is_fine = EVP_has_aes_hardware();
  85. const uint16_t version = ssl3_protocol_version(ssl);
  86. const SSL_CIPHER *best = NULL;
  87. while (CBS_len(&cipher_suites) > 0) {
  88. uint16_t cipher_suite;
  89. if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
  90. return NULL;
  91. }
  92. /* Limit to TLS 1.3 ciphers we know about. */
  93. const SSL_CIPHER *candidate = SSL_get_cipher_by_value(cipher_suite);
  94. if (candidate == NULL ||
  95. SSL_CIPHER_get_min_version(candidate) > version ||
  96. SSL_CIPHER_get_max_version(candidate) < version) {
  97. continue;
  98. }
  99. /* TLS 1.3 removes legacy ciphers, so honor the client order, but prefer
  100. * ChaCha20 if we do not have AES hardware. */
  101. if (aes_is_fine) {
  102. return candidate;
  103. }
  104. if (candidate->algorithm_enc == SSL_CHACHA20POLY1305) {
  105. return candidate;
  106. }
  107. if (best == NULL) {
  108. best = candidate;
  109. }
  110. }
  111. return best;
  112. }
  113. static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) {
  114. SSL *const ssl = hs->ssl;
  115. /* The short record header extension is incompatible with early data. */
  116. if (ssl->s3->skip_early_data && ssl->s3->short_header) {
  117. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
  118. return ssl_hs_error;
  119. }
  120. SSL_CLIENT_HELLO client_hello;
  121. if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
  122. ssl->init_num)) {
  123. OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
  124. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  125. return ssl_hs_error;
  126. }
  127. /* Negotiate the cipher suite. */
  128. hs->new_cipher = choose_tls13_cipher(ssl, &client_hello);
  129. if (hs->new_cipher == NULL) {
  130. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
  131. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  132. return ssl_hs_error;
  133. }
  134. /* The PRF hash is now known. Set up the key schedule and hash the
  135. * ClientHello. */
  136. if (!tls13_init_key_schedule(hs) ||
  137. !ssl_hash_current_message(hs)) {
  138. return ssl_hs_error;
  139. }
  140. /* Decode the ticket if we agree on a PSK key exchange mode. */
  141. uint8_t alert = SSL_AD_DECODE_ERROR;
  142. SSL_SESSION *session = NULL;
  143. CBS pre_shared_key, binders;
  144. if (hs->accept_psk_mode &&
  145. ssl_client_hello_get_extension(&client_hello, &pre_shared_key,
  146. TLSEXT_TYPE_pre_shared_key)) {
  147. /* Verify that the pre_shared_key extension is the last extension in
  148. * ClientHello. */
  149. if (CBS_data(&pre_shared_key) + CBS_len(&pre_shared_key) !=
  150. client_hello.extensions + client_hello.extensions_len) {
  151. OPENSSL_PUT_ERROR(SSL, SSL_R_PRE_SHARED_KEY_MUST_BE_LAST);
  152. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  153. return ssl_hs_error;
  154. }
  155. if (!ssl_ext_pre_shared_key_parse_clienthello(hs, &session, &binders,
  156. &alert, &pre_shared_key)) {
  157. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  158. return ssl_hs_error;
  159. }
  160. }
  161. if (session != NULL &&
  162. !ssl_session_is_resumable(hs, session)) {
  163. SSL_SESSION_free(session);
  164. session = NULL;
  165. }
  166. /* Set up the new session, either using the original one as a template or
  167. * creating a fresh one. */
  168. if (session == NULL) {
  169. if (!ssl_get_new_session(hs, 1 /* server */)) {
  170. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  171. return ssl_hs_error;
  172. }
  173. hs->new_session->cipher = hs->new_cipher;
  174. /* On new sessions, stash the SNI value in the session. */
  175. if (hs->hostname != NULL) {
  176. OPENSSL_free(hs->new_session->tlsext_hostname);
  177. hs->new_session->tlsext_hostname = BUF_strdup(hs->hostname);
  178. if (hs->new_session->tlsext_hostname == NULL) {
  179. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  180. return ssl_hs_error;
  181. }
  182. }
  183. } else {
  184. /* Check the PSK binder. */
  185. if (!tls13_verify_psk_binder(hs, session, &binders)) {
  186. SSL_SESSION_free(session);
  187. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  188. return ssl_hs_error;
  189. }
  190. /* Only authentication information carries over in TLS 1.3. */
  191. hs->new_session = SSL_SESSION_dup(session, SSL_SESSION_DUP_AUTH_ONLY);
  192. if (hs->new_session == NULL) {
  193. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  194. return ssl_hs_error;
  195. }
  196. ssl->s3->session_reused = 1;
  197. SSL_SESSION_free(session);
  198. /* Resumption incorporates fresh key material, so refresh the timeout. */
  199. ssl_session_renew_timeout(ssl, hs->new_session,
  200. ssl->initial_ctx->session_psk_dhe_timeout);
  201. }
  202. if (ssl->ctx->dos_protection_cb != NULL &&
  203. ssl->ctx->dos_protection_cb(&client_hello) == 0) {
  204. /* Connection rejected for DOS reasons. */
  205. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
  206. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  207. return ssl_hs_error;
  208. }
  209. /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
  210. * deferred. Complete it now. */
  211. alert = SSL_AD_DECODE_ERROR;
  212. if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
  213. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  214. return ssl_hs_error;
  215. }
  216. /* Store the initial negotiated ALPN in the session. */
  217. if (ssl->s3->alpn_selected != NULL) {
  218. hs->new_session->early_alpn =
  219. BUF_memdup(ssl->s3->alpn_selected, ssl->s3->alpn_selected_len);
  220. if (hs->new_session->early_alpn == NULL) {
  221. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  222. return ssl_hs_error;
  223. }
  224. hs->new_session->early_alpn_len = ssl->s3->alpn_selected_len;
  225. }
  226. /* Incorporate the PSK into the running secret. */
  227. if (ssl->s3->session_reused) {
  228. if (!tls13_advance_key_schedule(hs, hs->new_session->master_key,
  229. hs->new_session->master_key_length)) {
  230. return ssl_hs_error;
  231. }
  232. } else if (!tls13_advance_key_schedule(hs, kZeroes, hs->hash_len)) {
  233. return ssl_hs_error;
  234. }
  235. ssl->method->received_flight(ssl);
  236. /* Resolve ECDHE and incorporate it into the secret. */
  237. int need_retry;
  238. if (!resolve_ecdhe_secret(hs, &need_retry, &client_hello)) {
  239. if (need_retry) {
  240. hs->tls13_state = state_send_hello_retry_request;
  241. return ssl_hs_ok;
  242. }
  243. return ssl_hs_error;
  244. }
  245. hs->tls13_state = state_send_server_hello;
  246. return ssl_hs_ok;
  247. }
  248. static enum ssl_hs_wait_t do_send_hello_retry_request(SSL_HANDSHAKE *hs) {
  249. SSL *const ssl = hs->ssl;
  250. CBB cbb, body, extensions;
  251. uint16_t group_id;
  252. if (!ssl->method->init_message(ssl, &cbb, &body,
  253. SSL3_MT_HELLO_RETRY_REQUEST) ||
  254. !CBB_add_u16(&body, ssl->version) ||
  255. !tls1_get_shared_group(hs, &group_id) ||
  256. !CBB_add_u16_length_prefixed(&body, &extensions) ||
  257. !CBB_add_u16(&extensions, TLSEXT_TYPE_key_share) ||
  258. !CBB_add_u16(&extensions, 2 /* length */) ||
  259. !CBB_add_u16(&extensions, group_id) ||
  260. !ssl_add_message_cbb(ssl, &cbb)) {
  261. CBB_cleanup(&cbb);
  262. return ssl_hs_error;
  263. }
  264. hs->tls13_state = state_process_second_client_hello;
  265. return ssl_hs_flush_and_read_message;
  266. }
  267. static enum ssl_hs_wait_t do_process_second_client_hello(SSL_HANDSHAKE *hs) {
  268. SSL *const ssl = hs->ssl;
  269. if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_HELLO)) {
  270. return ssl_hs_error;
  271. }
  272. SSL_CLIENT_HELLO client_hello;
  273. if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
  274. ssl->init_num)) {
  275. OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
  276. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  277. return ssl_hs_error;
  278. }
  279. int need_retry;
  280. if (!resolve_ecdhe_secret(hs, &need_retry, &client_hello)) {
  281. if (need_retry) {
  282. /* Only send one HelloRetryRequest. */
  283. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  284. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
  285. }
  286. return ssl_hs_error;
  287. }
  288. if (!ssl_hash_current_message(hs)) {
  289. return ssl_hs_error;
  290. }
  291. ssl->method->received_flight(ssl);
  292. hs->tls13_state = state_send_server_hello;
  293. return ssl_hs_ok;
  294. }
  295. static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
  296. SSL *const ssl = hs->ssl;
  297. /* Send a ServerHello. */
  298. CBB cbb, body, extensions;
  299. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) ||
  300. !CBB_add_u16(&body, ssl->version) ||
  301. !RAND_bytes(ssl->s3->server_random, sizeof(ssl->s3->server_random)) ||
  302. !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
  303. !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
  304. !CBB_add_u16_length_prefixed(&body, &extensions) ||
  305. !ssl_ext_pre_shared_key_add_serverhello(hs, &extensions) ||
  306. !ssl_ext_key_share_add_serverhello(hs, &extensions)) {
  307. goto err;
  308. }
  309. if (ssl->s3->short_header) {
  310. if (!CBB_add_u16(&extensions, TLSEXT_TYPE_short_header) ||
  311. !CBB_add_u16(&extensions, 0 /* empty extension */)) {
  312. goto err;
  313. }
  314. }
  315. if (!ssl_add_message_cbb(ssl, &cbb)) {
  316. goto err;
  317. }
  318. /* Derive and enable the handshake traffic secrets. */
  319. if (!tls13_derive_handshake_secrets(hs) ||
  320. !tls13_set_traffic_key(ssl, evp_aead_open, hs->client_handshake_secret,
  321. hs->hash_len) ||
  322. !tls13_set_traffic_key(ssl, evp_aead_seal, hs->server_handshake_secret,
  323. hs->hash_len)) {
  324. goto err;
  325. }
  326. /* Send EncryptedExtensions. */
  327. if (!ssl->method->init_message(ssl, &cbb, &body,
  328. SSL3_MT_ENCRYPTED_EXTENSIONS) ||
  329. !ssl_add_serverhello_tlsext(hs, &body) ||
  330. !ssl_add_message_cbb(ssl, &cbb)) {
  331. goto err;
  332. }
  333. /* Determine whether to request a client certificate. */
  334. hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
  335. /* CertificateRequest may only be sent in non-resumption handshakes. */
  336. if (ssl->s3->session_reused) {
  337. hs->cert_request = 0;
  338. }
  339. /* Send a CertificateRequest, if necessary. */
  340. if (hs->cert_request) {
  341. CBB sigalgs_cbb;
  342. if (!ssl->method->init_message(ssl, &cbb, &body,
  343. SSL3_MT_CERTIFICATE_REQUEST) ||
  344. !CBB_add_u8(&body, 0 /* no certificate_request_context. */)) {
  345. goto err;
  346. }
  347. const uint16_t *sigalgs;
  348. size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
  349. if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb)) {
  350. goto err;
  351. }
  352. for (size_t i = 0; i < num_sigalgs; i++) {
  353. if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
  354. goto err;
  355. }
  356. }
  357. if (!ssl_add_client_CA_list(ssl, &body) ||
  358. !CBB_add_u16(&body, 0 /* empty certificate_extensions. */) ||
  359. !ssl_add_message_cbb(ssl, &cbb)) {
  360. goto err;
  361. }
  362. }
  363. /* Send the server Certificate message, if necessary. */
  364. if (!ssl->s3->session_reused) {
  365. if (!ssl_has_certificate(ssl)) {
  366. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
  367. goto err;
  368. }
  369. if (!tls13_add_certificate(hs)) {
  370. goto err;
  371. }
  372. hs->tls13_state = state_send_server_certificate_verify;
  373. return ssl_hs_ok;
  374. }
  375. hs->tls13_state = state_send_server_finished;
  376. return ssl_hs_ok;
  377. err:
  378. CBB_cleanup(&cbb);
  379. return ssl_hs_error;
  380. }
  381. static enum ssl_hs_wait_t do_send_server_certificate_verify(SSL_HANDSHAKE *hs,
  382. int is_first_run) {
  383. switch (tls13_add_certificate_verify(hs, is_first_run)) {
  384. case ssl_private_key_success:
  385. hs->tls13_state = state_send_server_finished;
  386. return ssl_hs_ok;
  387. case ssl_private_key_retry:
  388. hs->tls13_state = state_complete_server_certificate_verify;
  389. return ssl_hs_private_key_operation;
  390. case ssl_private_key_failure:
  391. return ssl_hs_error;
  392. }
  393. assert(0);
  394. return ssl_hs_error;
  395. }
  396. static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
  397. SSL *const ssl = hs->ssl;
  398. if (!tls13_add_finished(hs) ||
  399. /* Update the secret to the master secret and derive traffic keys. */
  400. !tls13_advance_key_schedule(hs, kZeroes, hs->hash_len) ||
  401. !tls13_derive_application_secrets(hs) ||
  402. !tls13_set_traffic_key(ssl, evp_aead_seal, hs->server_traffic_secret_0,
  403. hs->hash_len)) {
  404. return ssl_hs_error;
  405. }
  406. hs->tls13_state = state_process_client_certificate;
  407. return ssl_hs_flush_and_read_message;
  408. }
  409. static enum ssl_hs_wait_t do_process_client_certificate(SSL_HANDSHAKE *hs) {
  410. SSL *const ssl = hs->ssl;
  411. if (!hs->cert_request) {
  412. /* OpenSSL returns X509_V_OK when no certificates are requested. This is
  413. * classed by them as a bug, but it's assumed by at least NGINX. */
  414. hs->new_session->verify_result = X509_V_OK;
  415. /* Skip this state. */
  416. hs->tls13_state = state_process_channel_id;
  417. return ssl_hs_ok;
  418. }
  419. const int allow_anonymous =
  420. (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) == 0;
  421. if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE) ||
  422. !tls13_process_certificate(hs, allow_anonymous) ||
  423. !ssl_hash_current_message(hs)) {
  424. return ssl_hs_error;
  425. }
  426. hs->tls13_state = state_process_client_certificate_verify;
  427. return ssl_hs_read_message;
  428. }
  429. static enum ssl_hs_wait_t do_process_client_certificate_verify(
  430. SSL_HANDSHAKE *hs) {
  431. SSL *const ssl = hs->ssl;
  432. if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
  433. /* Skip this state. */
  434. hs->tls13_state = state_process_channel_id;
  435. return ssl_hs_ok;
  436. }
  437. if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY) ||
  438. !tls13_process_certificate_verify(hs) ||
  439. !ssl_hash_current_message(hs)) {
  440. return ssl_hs_error;
  441. }
  442. hs->tls13_state = state_process_channel_id;
  443. return ssl_hs_read_message;
  444. }
  445. static enum ssl_hs_wait_t do_process_channel_id(SSL_HANDSHAKE *hs) {
  446. if (!hs->ssl->s3->tlsext_channel_id_valid) {
  447. hs->tls13_state = state_process_client_finished;
  448. return ssl_hs_ok;
  449. }
  450. if (!ssl_check_message_type(hs->ssl, SSL3_MT_CHANNEL_ID) ||
  451. !tls1_verify_channel_id(hs) ||
  452. !ssl_hash_current_message(hs)) {
  453. return ssl_hs_error;
  454. }
  455. hs->tls13_state = state_process_client_finished;
  456. return ssl_hs_read_message;
  457. }
  458. static enum ssl_hs_wait_t do_process_client_finished(SSL_HANDSHAKE *hs) {
  459. SSL *const ssl = hs->ssl;
  460. if (!ssl_check_message_type(ssl, SSL3_MT_FINISHED) ||
  461. !tls13_process_finished(hs) ||
  462. !ssl_hash_current_message(hs) ||
  463. /* evp_aead_seal keys have already been switched. */
  464. !tls13_set_traffic_key(ssl, evp_aead_open, hs->client_traffic_secret_0,
  465. hs->hash_len) ||
  466. !tls13_derive_resumption_secret(hs)) {
  467. return ssl_hs_error;
  468. }
  469. ssl->method->received_flight(ssl);
  470. /* Rebase the session timestamp so that it is measured from ticket
  471. * issuance. */
  472. ssl_session_rebase_time(ssl, hs->new_session);
  473. hs->tls13_state = state_send_new_session_ticket;
  474. return ssl_hs_ok;
  475. }
  476. static enum ssl_hs_wait_t do_send_new_session_ticket(SSL_HANDSHAKE *hs) {
  477. /* TLS 1.3 recommends single-use tickets, so issue multiple tickets in case the
  478. * client makes several connections before getting a renewal. */
  479. static const int kNumTickets = 2;
  480. SSL *const ssl = hs->ssl;
  481. /* If the client doesn't accept resumption with PSK_DHE_KE, don't send a
  482. * session ticket. */
  483. if (!hs->accept_psk_mode) {
  484. hs->tls13_state = state_done;
  485. return ssl_hs_ok;
  486. }
  487. SSL_SESSION *session = hs->new_session;
  488. CBB cbb;
  489. CBB_zero(&cbb);
  490. for (int i = 0; i < kNumTickets; i++) {
  491. if (!RAND_bytes((uint8_t *)&session->ticket_age_add, 4)) {
  492. goto err;
  493. }
  494. CBB body, ticket, extensions;
  495. if (!ssl->method->init_message(ssl, &cbb, &body,
  496. SSL3_MT_NEW_SESSION_TICKET) ||
  497. !CBB_add_u32(&body, session->timeout) ||
  498. !CBB_add_u32(&body, session->ticket_age_add) ||
  499. !CBB_add_u16_length_prefixed(&body, &ticket) ||
  500. !ssl_encrypt_ticket(ssl, &ticket, session) ||
  501. !CBB_add_u16_length_prefixed(&body, &extensions)) {
  502. goto err;
  503. }
  504. if (ssl->ctx->enable_early_data) {
  505. session->ticket_max_early_data = kMaxEarlyDataAccepted;
  506. CBB early_data_info;
  507. if (!CBB_add_u16(&extensions, TLSEXT_TYPE_ticket_early_data_info) ||
  508. !CBB_add_u16_length_prefixed(&extensions, &early_data_info) ||
  509. !CBB_add_u32(&early_data_info, session->ticket_max_early_data) ||
  510. !CBB_flush(&extensions)) {
  511. goto err;
  512. }
  513. }
  514. /* Add a fake extension. See draft-davidben-tls-grease-01. */
  515. if (!CBB_add_u16(&extensions,
  516. ssl_get_grease_value(ssl, ssl_grease_ticket_extension)) ||
  517. !CBB_add_u16(&extensions, 0 /* empty */)) {
  518. goto err;
  519. }
  520. if (!ssl_add_message_cbb(ssl, &cbb)) {
  521. goto err;
  522. }
  523. }
  524. hs->session_tickets_sent++;
  525. hs->tls13_state = state_done;
  526. return ssl_hs_flush;
  527. err:
  528. CBB_cleanup(&cbb);
  529. return ssl_hs_error;
  530. }
  531. enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs) {
  532. while (hs->tls13_state != state_done) {
  533. enum ssl_hs_wait_t ret = ssl_hs_error;
  534. enum server_hs_state_t state = hs->tls13_state;
  535. switch (state) {
  536. case state_select_parameters:
  537. ret = do_select_parameters(hs);
  538. break;
  539. case state_send_hello_retry_request:
  540. ret = do_send_hello_retry_request(hs);
  541. break;
  542. case state_process_second_client_hello:
  543. ret = do_process_second_client_hello(hs);
  544. break;
  545. case state_send_server_hello:
  546. ret = do_send_server_hello(hs);
  547. break;
  548. case state_send_server_certificate_verify:
  549. ret = do_send_server_certificate_verify(hs, 1 /* first run */);
  550. break;
  551. case state_complete_server_certificate_verify:
  552. ret = do_send_server_certificate_verify(hs, 0 /* complete */);
  553. break;
  554. case state_send_server_finished:
  555. ret = do_send_server_finished(hs);
  556. break;
  557. case state_process_client_certificate:
  558. ret = do_process_client_certificate(hs);
  559. break;
  560. case state_process_client_certificate_verify:
  561. ret = do_process_client_certificate_verify(hs);
  562. break;
  563. case state_process_channel_id:
  564. ret = do_process_channel_id(hs);
  565. break;
  566. case state_process_client_finished:
  567. ret = do_process_client_finished(hs);
  568. break;
  569. case state_send_new_session_ticket:
  570. ret = do_send_new_session_ticket(hs);
  571. break;
  572. case state_done:
  573. ret = ssl_hs_ok;
  574. break;
  575. }
  576. if (ret != ssl_hs_ok) {
  577. return ret;
  578. }
  579. }
  580. return ssl_hs_ok;
  581. }