You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tls13_server.c 27 KiB

Don't use the buffer BIO in TLS. On the TLS side, we introduce a running buffer of ciphertext. Queuing up pending data consists of encrypting the record into the buffer. This effectively reimplements what the buffer BIO was doing previously, but this resizes to fit the whole flight. As part of this, rename all the functions to add to the pending flight to be more uniform. This CL proposes "add_foo" to add to the pending flight and "flush_flight" to drain it. We add an add_alert hook for alerts but, for now, only the SSL 3.0 warning alert (sent mid-handshake) uses this mechanism. Later work will push this down to the rest of the write path so closure alerts use it too, as in DTLS. The intended end state is that all the ssl_buffer.c and wpend_ret logic will only be used for application data and eventually optionally replaced by the in-place API, while all "incidental" data will be handled internally. For now, the two buffers are mutually exclusive. Moving closure alerts to "incidentals" will change this, but flushing application data early is tricky due to wpend_ret. (If we call ssl_write_buffer_flush, do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That too is all left alone in this change. To keep the diff down, write_message is retained for now and will be removed from the state machines in a follow-up change. BUG=72 Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d Reviewed-on: https://boringssl-review.googlesource.com/13224 Reviewed-by: Adam Langley <agl@google.com>
7 years ago
Don't use the buffer BIO in TLS. On the TLS side, we introduce a running buffer of ciphertext. Queuing up pending data consists of encrypting the record into the buffer. This effectively reimplements what the buffer BIO was doing previously, but this resizes to fit the whole flight. As part of this, rename all the functions to add to the pending flight to be more uniform. This CL proposes "add_foo" to add to the pending flight and "flush_flight" to drain it. We add an add_alert hook for alerts but, for now, only the SSL 3.0 warning alert (sent mid-handshake) uses this mechanism. Later work will push this down to the rest of the write path so closure alerts use it too, as in DTLS. The intended end state is that all the ssl_buffer.c and wpend_ret logic will only be used for application data and eventually optionally replaced by the in-place API, while all "incidental" data will be handled internally. For now, the two buffers are mutually exclusive. Moving closure alerts to "incidentals" will change this, but flushing application data early is tricky due to wpend_ret. (If we call ssl_write_buffer_flush, do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That too is all left alone in this change. To keep the diff down, write_message is retained for now and will be removed from the state machines in a follow-up change. BUG=72 Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d Reviewed-on: https://boringssl-review.googlesource.com/13224 Reviewed-by: Adam Langley <agl@google.com>
7 years ago
Simplify ssl_private_key_* state machine points. The original motivation behind the sign/complete split was to avoid needlessly hashing the input on each pass through the state machine, but we're payload-based now and, in all cases, the payload is either cheap to compute or readily available. (Even the hashing worry was probably unnecessary.) Tweak ssl_private_key_{sign,decrypt} to automatically call ssl_private_key_complete as needed and take advantage of this in the handshake state machines: - TLS 1.3 signing now computes the payload each pass. The payload is small and we're already allocating a comparable-sized buffer each iteration to hold the signature. This shouldn't be a big deal. - TLS 1.2 decryption code still needs two states due to reading the message (fixed in new state machine style), but otherwise it just performs cheap idempotent tasks again. The PSK code is reshuffled to guarantee the callback is not called twice (though this was impossible anyway because we don't support RSA_PSK). - TLS 1.2 CertificateVerify signing is easy as the transcript is readily available. The buffer is released very slightly later, but it shouldn't matter. - TLS 1.2 ServerKeyExchange signing required some reshuffling. Assembling the ServerKeyExchange parameters is moved to the previous state. The signing payload has some randoms prepended. This is cheap enough, but a nuisance in C. Pre-prepend the randoms in hs->server_params. With this change, we are *nearly* rid of the A/B => same function pattern. BUG=128 Change-Id: Iec4fe0be7cfc88a6de027ba2760fae70794ea810 Reviewed-on: https://boringssl-review.googlesource.com/17265 Commit-Queue: David Benjamin <davidben@google.com> Commit-Queue: Steven Valdez <svaldez@google.com> Reviewed-by: Steven Valdez <svaldez@google.com>
7 years ago
Simplify ssl_private_key_* state machine points. The original motivation behind the sign/complete split was to avoid needlessly hashing the input on each pass through the state machine, but we're payload-based now and, in all cases, the payload is either cheap to compute or readily available. (Even the hashing worry was probably unnecessary.) Tweak ssl_private_key_{sign,decrypt} to automatically call ssl_private_key_complete as needed and take advantage of this in the handshake state machines: - TLS 1.3 signing now computes the payload each pass. The payload is small and we're already allocating a comparable-sized buffer each iteration to hold the signature. This shouldn't be a big deal. - TLS 1.2 decryption code still needs two states due to reading the message (fixed in new state machine style), but otherwise it just performs cheap idempotent tasks again. The PSK code is reshuffled to guarantee the callback is not called twice (though this was impossible anyway because we don't support RSA_PSK). - TLS 1.2 CertificateVerify signing is easy as the transcript is readily available. The buffer is released very slightly later, but it shouldn't matter. - TLS 1.2 ServerKeyExchange signing required some reshuffling. Assembling the ServerKeyExchange parameters is moved to the previous state. The signing payload has some randoms prepended. This is cheap enough, but a nuisance in C. Pre-prepend the randoms in hs->server_params. With this change, we are *nearly* rid of the A/B => same function pattern. BUG=128 Change-Id: Iec4fe0be7cfc88a6de027ba2760fae70794ea810 Reviewed-on: https://boringssl-review.googlesource.com/17265 Commit-Queue: David Benjamin <davidben@google.com> Commit-Queue: Steven Valdez <svaldez@google.com> Reviewed-by: Steven Valdez <svaldez@google.com>
7 years ago
Simplify ssl_private_key_* state machine points. The original motivation behind the sign/complete split was to avoid needlessly hashing the input on each pass through the state machine, but we're payload-based now and, in all cases, the payload is either cheap to compute or readily available. (Even the hashing worry was probably unnecessary.) Tweak ssl_private_key_{sign,decrypt} to automatically call ssl_private_key_complete as needed and take advantage of this in the handshake state machines: - TLS 1.3 signing now computes the payload each pass. The payload is small and we're already allocating a comparable-sized buffer each iteration to hold the signature. This shouldn't be a big deal. - TLS 1.2 decryption code still needs two states due to reading the message (fixed in new state machine style), but otherwise it just performs cheap idempotent tasks again. The PSK code is reshuffled to guarantee the callback is not called twice (though this was impossible anyway because we don't support RSA_PSK). - TLS 1.2 CertificateVerify signing is easy as the transcript is readily available. The buffer is released very slightly later, but it shouldn't matter. - TLS 1.2 ServerKeyExchange signing required some reshuffling. Assembling the ServerKeyExchange parameters is moved to the previous state. The signing payload has some randoms prepended. This is cheap enough, but a nuisance in C. Pre-prepend the randoms in hs->server_params. With this change, we are *nearly* rid of the A/B => same function pattern. BUG=128 Change-Id: Iec4fe0be7cfc88a6de027ba2760fae70794ea810 Reviewed-on: https://boringssl-review.googlesource.com/17265 Commit-Queue: David Benjamin <davidben@google.com> Commit-Queue: Steven Valdez <svaldez@google.com> Reviewed-by: Steven Valdez <svaldez@google.com>
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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. enum server_hs_state_t {
  27. state_select_parameters = 0,
  28. state_select_session,
  29. state_send_hello_retry_request,
  30. state_process_second_client_hello,
  31. state_send_server_hello,
  32. state_send_server_certificate_verify,
  33. state_send_server_finished,
  34. state_read_second_client_flight,
  35. state_process_end_of_early_data,
  36. state_process_client_certificate,
  37. state_process_client_certificate_verify,
  38. state_process_channel_id,
  39. state_process_client_finished,
  40. state_send_new_session_ticket,
  41. state_done,
  42. };
  43. static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0};
  44. static int resolve_ecdhe_secret(SSL_HANDSHAKE *hs, int *out_need_retry,
  45. SSL_CLIENT_HELLO *client_hello) {
  46. SSL *const ssl = hs->ssl;
  47. *out_need_retry = 0;
  48. /* We only support connections that include an ECDHE key exchange. */
  49. CBS key_share;
  50. if (!ssl_client_hello_get_extension(client_hello, &key_share,
  51. TLSEXT_TYPE_key_share)) {
  52. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE);
  53. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION);
  54. return 0;
  55. }
  56. int found_key_share;
  57. uint8_t *dhe_secret;
  58. size_t dhe_secret_len;
  59. uint8_t alert = SSL_AD_DECODE_ERROR;
  60. if (!ssl_ext_key_share_parse_clienthello(hs, &found_key_share, &dhe_secret,
  61. &dhe_secret_len, &alert,
  62. &key_share)) {
  63. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  64. return 0;
  65. }
  66. if (!found_key_share) {
  67. *out_need_retry = 1;
  68. return 0;
  69. }
  70. int ok = tls13_advance_key_schedule(hs, dhe_secret, dhe_secret_len);
  71. OPENSSL_free(dhe_secret);
  72. return ok;
  73. }
  74. static const SSL_CIPHER *choose_tls13_cipher(
  75. const SSL *ssl, const SSL_CLIENT_HELLO *client_hello) {
  76. if (client_hello->cipher_suites_len % 2 != 0) {
  77. return NULL;
  78. }
  79. CBS cipher_suites;
  80. CBS_init(&cipher_suites, client_hello->cipher_suites,
  81. client_hello->cipher_suites_len);
  82. const int aes_is_fine = EVP_has_aes_hardware();
  83. const uint16_t version = ssl3_protocol_version(ssl);
  84. const SSL_CIPHER *best = NULL;
  85. while (CBS_len(&cipher_suites) > 0) {
  86. uint16_t cipher_suite;
  87. if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
  88. return NULL;
  89. }
  90. /* Limit to TLS 1.3 ciphers we know about. */
  91. const SSL_CIPHER *candidate = SSL_get_cipher_by_value(cipher_suite);
  92. if (candidate == NULL ||
  93. SSL_CIPHER_get_min_version(candidate) > version ||
  94. SSL_CIPHER_get_max_version(candidate) < version) {
  95. continue;
  96. }
  97. /* TLS 1.3 removes legacy ciphers, so honor the client order, but prefer
  98. * ChaCha20 if we do not have AES hardware. */
  99. if (aes_is_fine) {
  100. return candidate;
  101. }
  102. if (candidate->algorithm_enc == SSL_CHACHA20POLY1305) {
  103. return candidate;
  104. }
  105. if (best == NULL) {
  106. best = candidate;
  107. }
  108. }
  109. return best;
  110. }
  111. static int add_new_session_tickets(SSL_HANDSHAKE *hs) {
  112. SSL *const ssl = hs->ssl;
  113. /* TLS 1.3 recommends single-use tickets, so issue multiple tickets in case
  114. * the client makes several connections before getting a renewal. */
  115. static const int kNumTickets = 2;
  116. SSL_SESSION *session = hs->new_session;
  117. CBB cbb;
  118. CBB_zero(&cbb);
  119. /* Rebase the session timestamp so that it is measured from ticket
  120. * issuance. */
  121. ssl_session_rebase_time(ssl, session);
  122. for (int i = 0; i < kNumTickets; i++) {
  123. if (!RAND_bytes((uint8_t *)&session->ticket_age_add, 4)) {
  124. goto err;
  125. }
  126. session->ticket_age_add_valid = 1;
  127. CBB body, ticket, extensions;
  128. if (!ssl->method->init_message(ssl, &cbb, &body,
  129. SSL3_MT_NEW_SESSION_TICKET) ||
  130. !CBB_add_u32(&body, session->timeout) ||
  131. !CBB_add_u32(&body, session->ticket_age_add) ||
  132. !CBB_add_u16_length_prefixed(&body, &ticket) ||
  133. !ssl_encrypt_ticket(ssl, &ticket, session) ||
  134. !CBB_add_u16_length_prefixed(&body, &extensions)) {
  135. goto err;
  136. }
  137. if (ssl->cert->enable_early_data) {
  138. session->ticket_max_early_data = kMaxEarlyDataAccepted;
  139. CBB early_data_info;
  140. if (!CBB_add_u16(&extensions, TLSEXT_TYPE_ticket_early_data_info) ||
  141. !CBB_add_u16_length_prefixed(&extensions, &early_data_info) ||
  142. !CBB_add_u32(&early_data_info, session->ticket_max_early_data) ||
  143. !CBB_flush(&extensions)) {
  144. goto err;
  145. }
  146. }
  147. /* Add a fake extension. See draft-davidben-tls-grease-01. */
  148. if (!CBB_add_u16(&extensions,
  149. ssl_get_grease_value(ssl, ssl_grease_ticket_extension)) ||
  150. !CBB_add_u16(&extensions, 0 /* empty */)) {
  151. goto err;
  152. }
  153. if (!ssl_add_message_cbb(ssl, &cbb)) {
  154. goto err;
  155. }
  156. }
  157. return 1;
  158. err:
  159. CBB_cleanup(&cbb);
  160. return 0;
  161. }
  162. static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) {
  163. /* At this point, most ClientHello extensions have already been processed by
  164. * the common handshake logic. Resolve the remaining non-PSK parameters. */
  165. SSL *const ssl = hs->ssl;
  166. SSL_CLIENT_HELLO client_hello;
  167. if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
  168. ssl->init_num)) {
  169. OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
  170. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  171. return ssl_hs_error;
  172. }
  173. /* Negotiate the cipher suite. */
  174. hs->new_cipher = choose_tls13_cipher(ssl, &client_hello);
  175. if (hs->new_cipher == NULL) {
  176. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
  177. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  178. return ssl_hs_error;
  179. }
  180. /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
  181. * deferred. Complete it now. */
  182. uint8_t alert = SSL_AD_DECODE_ERROR;
  183. if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
  184. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  185. return ssl_hs_error;
  186. }
  187. /* The PRF hash is now known. Set up the key schedule and hash the
  188. * ClientHello. */
  189. if (!tls13_init_key_schedule(hs) ||
  190. !ssl_hash_current_message(hs)) {
  191. return ssl_hs_error;
  192. }
  193. hs->tls13_state = state_select_session;
  194. return ssl_hs_ok;
  195. }
  196. static enum ssl_ticket_aead_result_t select_session(
  197. SSL_HANDSHAKE *hs, uint8_t *out_alert, SSL_SESSION **out_session,
  198. int32_t *out_ticket_age_skew, const SSL_CLIENT_HELLO *client_hello) {
  199. SSL *const ssl = hs->ssl;
  200. *out_session = NULL;
  201. /* Decode the ticket if we agreed on a PSK key exchange mode. */
  202. CBS pre_shared_key;
  203. if (!hs->accept_psk_mode ||
  204. !ssl_client_hello_get_extension(client_hello, &pre_shared_key,
  205. TLSEXT_TYPE_pre_shared_key)) {
  206. return ssl_ticket_aead_ignore_ticket;
  207. }
  208. /* Verify that the pre_shared_key extension is the last extension in
  209. * ClientHello. */
  210. if (CBS_data(&pre_shared_key) + CBS_len(&pre_shared_key) !=
  211. client_hello->extensions + client_hello->extensions_len) {
  212. OPENSSL_PUT_ERROR(SSL, SSL_R_PRE_SHARED_KEY_MUST_BE_LAST);
  213. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  214. return ssl_ticket_aead_error;
  215. }
  216. CBS ticket, binders;
  217. uint32_t client_ticket_age;
  218. if (!ssl_ext_pre_shared_key_parse_clienthello(hs, &ticket, &binders,
  219. &client_ticket_age, out_alert,
  220. &pre_shared_key)) {
  221. return ssl_ticket_aead_error;
  222. }
  223. /* TLS 1.3 session tickets are renewed separately as part of the
  224. * NewSessionTicket. */
  225. int unused_renew;
  226. SSL_SESSION *session = NULL;
  227. enum ssl_ticket_aead_result_t ret =
  228. ssl_process_ticket(ssl, &session, &unused_renew, CBS_data(&ticket),
  229. CBS_len(&ticket), NULL, 0);
  230. switch (ret) {
  231. case ssl_ticket_aead_success:
  232. break;
  233. case ssl_ticket_aead_error:
  234. *out_alert = SSL_AD_INTERNAL_ERROR;
  235. return ret;
  236. default:
  237. return ret;
  238. }
  239. if (!ssl_session_is_resumable(hs, session) ||
  240. /* Historically, some TLS 1.3 tickets were missing ticket_age_add. */
  241. !session->ticket_age_add_valid) {
  242. SSL_SESSION_free(session);
  243. return ssl_ticket_aead_ignore_ticket;
  244. }
  245. /* Recover the client ticket age and convert to seconds. */
  246. client_ticket_age -= session->ticket_age_add;
  247. client_ticket_age /= 1000;
  248. struct OPENSSL_timeval now;
  249. ssl_get_current_time(ssl, &now);
  250. /* Compute the server ticket age in seconds. */
  251. assert(now.tv_sec >= session->time);
  252. uint64_t server_ticket_age = now.tv_sec - session->time;
  253. /* To avoid overflowing |hs->ticket_age_skew|, we will not resume
  254. * 68-year-old sessions. */
  255. if (server_ticket_age > INT32_MAX) {
  256. SSL_SESSION_free(session);
  257. return ssl_ticket_aead_ignore_ticket;
  258. }
  259. /* TODO(davidben,svaldez): Measure this value to decide on tolerance. For
  260. * now, accept all values. https://crbug.com/boringssl/113. */
  261. *out_ticket_age_skew =
  262. (int32_t)client_ticket_age - (int32_t)server_ticket_age;
  263. /* Check the PSK binder. */
  264. if (!tls13_verify_psk_binder(hs, session, &binders)) {
  265. SSL_SESSION_free(session);
  266. *out_alert = SSL_AD_DECRYPT_ERROR;
  267. return ssl_ticket_aead_error;
  268. }
  269. *out_session = session;
  270. return ssl_ticket_aead_success;
  271. }
  272. static enum ssl_hs_wait_t do_select_session(SSL_HANDSHAKE *hs) {
  273. SSL *const ssl = hs->ssl;
  274. SSL_CLIENT_HELLO client_hello;
  275. if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
  276. ssl->init_num)) {
  277. OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
  278. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  279. return ssl_hs_error;
  280. }
  281. uint8_t alert = SSL_AD_DECODE_ERROR;
  282. SSL_SESSION *session = NULL;
  283. switch (select_session(hs, &alert, &session, &ssl->s3->ticket_age_skew,
  284. &client_hello)) {
  285. case ssl_ticket_aead_ignore_ticket:
  286. assert(session == NULL);
  287. if (!ssl_get_new_session(hs, 1 /* server */)) {
  288. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  289. return ssl_hs_error;
  290. }
  291. break;
  292. case ssl_ticket_aead_success:
  293. /* Carry over authentication information from the previous handshake into
  294. * a fresh session. */
  295. hs->new_session = SSL_SESSION_dup(session, SSL_SESSION_DUP_AUTH_ONLY);
  296. if (/* Early data must be acceptable for this ticket. */
  297. ssl->cert->enable_early_data &&
  298. session->ticket_max_early_data != 0 &&
  299. /* The client must have offered early data. */
  300. hs->early_data_offered &&
  301. /* Channel ID is incompatible with 0-RTT. */
  302. !ssl->s3->tlsext_channel_id_valid &&
  303. /* The negotiated ALPN must match the one in the ticket. */
  304. ssl->s3->alpn_selected_len == session->early_alpn_len &&
  305. OPENSSL_memcmp(ssl->s3->alpn_selected, session->early_alpn,
  306. ssl->s3->alpn_selected_len) == 0) {
  307. ssl->early_data_accepted = 1;
  308. }
  309. SSL_SESSION_free(session);
  310. if (hs->new_session == NULL) {
  311. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  312. return ssl_hs_error;
  313. }
  314. ssl->s3->session_reused = 1;
  315. /* Resumption incorporates fresh key material, so refresh the timeout. */
  316. ssl_session_renew_timeout(ssl, hs->new_session,
  317. ssl->session_ctx->session_psk_dhe_timeout);
  318. break;
  319. case ssl_ticket_aead_error:
  320. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  321. return ssl_hs_error;
  322. case ssl_ticket_aead_retry:
  323. hs->tls13_state = state_select_session;
  324. return ssl_hs_pending_ticket;
  325. }
  326. /* Record connection properties in the new session. */
  327. hs->new_session->cipher = hs->new_cipher;
  328. if (hs->hostname != NULL) {
  329. OPENSSL_free(hs->new_session->tlsext_hostname);
  330. hs->new_session->tlsext_hostname = BUF_strdup(hs->hostname);
  331. if (hs->new_session->tlsext_hostname == NULL) {
  332. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  333. return ssl_hs_error;
  334. }
  335. }
  336. /* Store the initial negotiated ALPN in the session. */
  337. if (ssl->s3->alpn_selected != NULL) {
  338. hs->new_session->early_alpn =
  339. BUF_memdup(ssl->s3->alpn_selected, ssl->s3->alpn_selected_len);
  340. if (hs->new_session->early_alpn == NULL) {
  341. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  342. return ssl_hs_error;
  343. }
  344. hs->new_session->early_alpn_len = ssl->s3->alpn_selected_len;
  345. }
  346. if (ssl->ctx->dos_protection_cb != NULL &&
  347. ssl->ctx->dos_protection_cb(&client_hello) == 0) {
  348. /* Connection rejected for DOS reasons. */
  349. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
  350. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  351. return ssl_hs_error;
  352. }
  353. /* Incorporate the PSK into the running secret. */
  354. if (ssl->s3->session_reused) {
  355. if (!tls13_advance_key_schedule(hs, hs->new_session->master_key,
  356. hs->new_session->master_key_length)) {
  357. return ssl_hs_error;
  358. }
  359. } else if (!tls13_advance_key_schedule(hs, kZeroes, hs->hash_len)) {
  360. return ssl_hs_error;
  361. }
  362. if (ssl->early_data_accepted) {
  363. if (!tls13_derive_early_secrets(hs)) {
  364. return ssl_hs_error;
  365. }
  366. } else if (hs->early_data_offered) {
  367. ssl->s3->skip_early_data = 1;
  368. }
  369. ssl->method->received_flight(ssl);
  370. /* Resolve ECDHE and incorporate it into the secret. */
  371. int need_retry;
  372. if (!resolve_ecdhe_secret(hs, &need_retry, &client_hello)) {
  373. if (need_retry) {
  374. ssl->early_data_accepted = 0;
  375. ssl->s3->skip_early_data = 1;
  376. hs->tls13_state = state_send_hello_retry_request;
  377. return ssl_hs_ok;
  378. }
  379. return ssl_hs_error;
  380. }
  381. hs->tls13_state = state_send_server_hello;
  382. return ssl_hs_ok;
  383. }
  384. static enum ssl_hs_wait_t do_send_hello_retry_request(SSL_HANDSHAKE *hs) {
  385. SSL *const ssl = hs->ssl;
  386. CBB cbb, body, extensions;
  387. uint16_t group_id;
  388. if (!ssl->method->init_message(ssl, &cbb, &body,
  389. SSL3_MT_HELLO_RETRY_REQUEST) ||
  390. !CBB_add_u16(&body, ssl->version) ||
  391. !tls1_get_shared_group(hs, &group_id) ||
  392. !CBB_add_u16_length_prefixed(&body, &extensions) ||
  393. !CBB_add_u16(&extensions, TLSEXT_TYPE_key_share) ||
  394. !CBB_add_u16(&extensions, 2 /* length */) ||
  395. !CBB_add_u16(&extensions, group_id) ||
  396. !ssl_add_message_cbb(ssl, &cbb)) {
  397. CBB_cleanup(&cbb);
  398. return ssl_hs_error;
  399. }
  400. hs->tls13_state = state_process_second_client_hello;
  401. return ssl_hs_flush_and_read_message;
  402. }
  403. static enum ssl_hs_wait_t do_process_second_client_hello(SSL_HANDSHAKE *hs) {
  404. SSL *const ssl = hs->ssl;
  405. if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_HELLO)) {
  406. return ssl_hs_error;
  407. }
  408. SSL_CLIENT_HELLO client_hello;
  409. if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
  410. ssl->init_num)) {
  411. OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
  412. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  413. return ssl_hs_error;
  414. }
  415. int need_retry;
  416. if (!resolve_ecdhe_secret(hs, &need_retry, &client_hello)) {
  417. if (need_retry) {
  418. /* Only send one HelloRetryRequest. */
  419. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  420. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
  421. }
  422. return ssl_hs_error;
  423. }
  424. if (!ssl_hash_current_message(hs)) {
  425. return ssl_hs_error;
  426. }
  427. ssl->method->received_flight(ssl);
  428. hs->tls13_state = state_send_server_hello;
  429. return ssl_hs_ok;
  430. }
  431. static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
  432. SSL *const ssl = hs->ssl;
  433. /* Send a ServerHello. */
  434. CBB cbb, body, extensions;
  435. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) ||
  436. !CBB_add_u16(&body, ssl->version) ||
  437. !RAND_bytes(ssl->s3->server_random, sizeof(ssl->s3->server_random)) ||
  438. !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
  439. !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
  440. !CBB_add_u16_length_prefixed(&body, &extensions) ||
  441. !ssl_ext_pre_shared_key_add_serverhello(hs, &extensions) ||
  442. !ssl_ext_key_share_add_serverhello(hs, &extensions) ||
  443. !ssl_add_message_cbb(ssl, &cbb)) {
  444. goto err;
  445. }
  446. /* Derive and enable the handshake traffic secrets. */
  447. if (!tls13_derive_handshake_secrets(hs) ||
  448. !tls13_set_traffic_key(ssl, evp_aead_seal, hs->server_handshake_secret,
  449. hs->hash_len)) {
  450. goto err;
  451. }
  452. /* Send EncryptedExtensions. */
  453. if (!ssl->method->init_message(ssl, &cbb, &body,
  454. SSL3_MT_ENCRYPTED_EXTENSIONS) ||
  455. !ssl_add_serverhello_tlsext(hs, &body) ||
  456. !ssl_add_message_cbb(ssl, &cbb)) {
  457. goto err;
  458. }
  459. /* Determine whether to request a client certificate. */
  460. hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
  461. /* CertificateRequest may only be sent in non-resumption handshakes. */
  462. if (ssl->s3->session_reused) {
  463. hs->cert_request = 0;
  464. }
  465. /* Send a CertificateRequest, if necessary. */
  466. if (hs->cert_request) {
  467. CBB sigalgs_cbb;
  468. if (!ssl->method->init_message(ssl, &cbb, &body,
  469. SSL3_MT_CERTIFICATE_REQUEST) ||
  470. !CBB_add_u8(&body, 0 /* no certificate_request_context. */) ||
  471. !CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
  472. !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) ||
  473. !ssl_add_client_CA_list(ssl, &body) ||
  474. !CBB_add_u16(&body, 0 /* empty certificate_extensions. */) ||
  475. !ssl_add_message_cbb(ssl, &cbb)) {
  476. goto err;
  477. }
  478. }
  479. /* Send the server Certificate message, if necessary. */
  480. if (!ssl->s3->session_reused) {
  481. if (!ssl_has_certificate(ssl)) {
  482. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
  483. goto err;
  484. }
  485. if (!tls13_add_certificate(hs)) {
  486. goto err;
  487. }
  488. hs->tls13_state = state_send_server_certificate_verify;
  489. return ssl_hs_ok;
  490. }
  491. hs->tls13_state = state_send_server_finished;
  492. return ssl_hs_ok;
  493. err:
  494. CBB_cleanup(&cbb);
  495. return ssl_hs_error;
  496. }
  497. static enum ssl_hs_wait_t do_send_server_certificate_verify(SSL_HANDSHAKE *hs) {
  498. switch (tls13_add_certificate_verify(hs)) {
  499. case ssl_private_key_success:
  500. hs->tls13_state = state_send_server_finished;
  501. return ssl_hs_ok;
  502. case ssl_private_key_retry:
  503. hs->tls13_state = state_send_server_certificate_verify;
  504. return ssl_hs_private_key_operation;
  505. case ssl_private_key_failure:
  506. return ssl_hs_error;
  507. }
  508. assert(0);
  509. return ssl_hs_error;
  510. }
  511. static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
  512. SSL *const ssl = hs->ssl;
  513. if (!tls13_add_finished(hs) ||
  514. /* Update the secret to the master secret and derive traffic keys. */
  515. !tls13_advance_key_schedule(hs, kZeroes, hs->hash_len) ||
  516. !tls13_derive_application_secrets(hs) ||
  517. !tls13_set_traffic_key(ssl, evp_aead_seal, hs->server_traffic_secret_0,
  518. hs->hash_len)) {
  519. return ssl_hs_error;
  520. }
  521. if (ssl->early_data_accepted) {
  522. /* If accepting 0-RTT, we send tickets half-RTT. This gets the tickets on
  523. * the wire sooner and also avoids triggering a write on |SSL_read| when
  524. * processing the client Finished. This requires computing the client
  525. * Finished early. See draft-ietf-tls-tls13-18, section 4.5.1. */
  526. size_t finished_len;
  527. if (!tls13_finished_mac(hs, hs->expected_client_finished, &finished_len,
  528. 0 /* client */)) {
  529. return ssl_hs_error;
  530. }
  531. if (finished_len != hs->hash_len) {
  532. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  533. return ssl_hs_error;
  534. }
  535. /* Feed the predicted Finished into the transcript. This allows us to derive
  536. * the resumption secret early and send half-RTT tickets.
  537. *
  538. * TODO(davidben): This will need to be updated for DTLS 1.3. */
  539. assert(!SSL_is_dtls(hs->ssl));
  540. uint8_t header[4] = {SSL3_MT_FINISHED, 0, 0, hs->hash_len};
  541. if (!SSL_TRANSCRIPT_update(&hs->transcript, header, sizeof(header)) ||
  542. !SSL_TRANSCRIPT_update(&hs->transcript, hs->expected_client_finished,
  543. hs->hash_len) ||
  544. !tls13_derive_resumption_secret(hs) ||
  545. !add_new_session_tickets(hs)) {
  546. return ssl_hs_error;
  547. }
  548. }
  549. hs->tls13_state = state_read_second_client_flight;
  550. return ssl_hs_flush;
  551. }
  552. static enum ssl_hs_wait_t do_read_second_client_flight(SSL_HANDSHAKE *hs) {
  553. SSL *const ssl = hs->ssl;
  554. if (ssl->early_data_accepted) {
  555. if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->early_traffic_secret,
  556. hs->hash_len)) {
  557. return ssl_hs_error;
  558. }
  559. hs->can_early_write = 1;
  560. hs->can_early_read = 1;
  561. hs->in_early_data = 1;
  562. hs->tls13_state = state_process_end_of_early_data;
  563. return ssl_hs_read_end_of_early_data;
  564. }
  565. hs->tls13_state = state_process_end_of_early_data;
  566. return ssl_hs_ok;
  567. }
  568. static enum ssl_hs_wait_t do_process_end_of_early_data(SSL_HANDSHAKE *hs) {
  569. SSL *const ssl = hs->ssl;
  570. if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->client_handshake_secret,
  571. hs->hash_len)) {
  572. return ssl_hs_error;
  573. }
  574. hs->tls13_state = ssl->early_data_accepted ? state_process_client_finished
  575. : state_process_client_certificate;
  576. return ssl_hs_read_message;
  577. }
  578. static enum ssl_hs_wait_t do_process_client_certificate(SSL_HANDSHAKE *hs) {
  579. SSL *const ssl = hs->ssl;
  580. if (!hs->cert_request) {
  581. /* OpenSSL returns X509_V_OK when no certificates are requested. This is
  582. * classed by them as a bug, but it's assumed by at least NGINX. */
  583. hs->new_session->verify_result = X509_V_OK;
  584. /* Skip this state. */
  585. hs->tls13_state = state_process_channel_id;
  586. return ssl_hs_ok;
  587. }
  588. const int allow_anonymous =
  589. (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) == 0;
  590. if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE) ||
  591. !tls13_process_certificate(hs, allow_anonymous) ||
  592. !ssl_hash_current_message(hs)) {
  593. return ssl_hs_error;
  594. }
  595. hs->tls13_state = state_process_client_certificate_verify;
  596. return ssl_hs_read_message;
  597. }
  598. static enum ssl_hs_wait_t do_process_client_certificate_verify(
  599. SSL_HANDSHAKE *hs) {
  600. SSL *const ssl = hs->ssl;
  601. if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
  602. /* Skip this state. */
  603. hs->tls13_state = state_process_channel_id;
  604. return ssl_hs_ok;
  605. }
  606. if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY) ||
  607. !tls13_process_certificate_verify(hs) ||
  608. !ssl_hash_current_message(hs)) {
  609. return ssl_hs_error;
  610. }
  611. hs->tls13_state = state_process_channel_id;
  612. return ssl_hs_read_message;
  613. }
  614. static enum ssl_hs_wait_t do_process_channel_id(SSL_HANDSHAKE *hs) {
  615. if (!hs->ssl->s3->tlsext_channel_id_valid) {
  616. hs->tls13_state = state_process_client_finished;
  617. return ssl_hs_ok;
  618. }
  619. if (!ssl_check_message_type(hs->ssl, SSL3_MT_CHANNEL_ID) ||
  620. !tls1_verify_channel_id(hs) ||
  621. !ssl_hash_current_message(hs)) {
  622. return ssl_hs_error;
  623. }
  624. hs->tls13_state = state_process_client_finished;
  625. return ssl_hs_read_message;
  626. }
  627. static enum ssl_hs_wait_t do_process_client_finished(SSL_HANDSHAKE *hs) {
  628. SSL *const ssl = hs->ssl;
  629. if (!ssl_check_message_type(ssl, SSL3_MT_FINISHED) ||
  630. /* If early data was accepted, we've already computed the client Finished
  631. * and derived the resumption secret. */
  632. !tls13_process_finished(hs, ssl->early_data_accepted) ||
  633. /* evp_aead_seal keys have already been switched. */
  634. !tls13_set_traffic_key(ssl, evp_aead_open, hs->client_traffic_secret_0,
  635. hs->hash_len)) {
  636. return ssl_hs_error;
  637. }
  638. ssl->method->received_flight(ssl);
  639. if (!ssl->early_data_accepted) {
  640. if (!ssl_hash_current_message(hs) ||
  641. !tls13_derive_resumption_secret(hs)) {
  642. return ssl_hs_error;
  643. }
  644. /* We send post-handshake tickets as part of the handshake in 1-RTT. */
  645. hs->tls13_state = state_send_new_session_ticket;
  646. return ssl_hs_ok;
  647. }
  648. hs->tls13_state = state_done;
  649. return ssl_hs_ok;
  650. }
  651. static enum ssl_hs_wait_t do_send_new_session_ticket(SSL_HANDSHAKE *hs) {
  652. /* If the client doesn't accept resumption with PSK_DHE_KE, don't send a
  653. * session ticket. */
  654. if (!hs->accept_psk_mode) {
  655. hs->tls13_state = state_done;
  656. return ssl_hs_ok;
  657. }
  658. if (!add_new_session_tickets(hs)) {
  659. return ssl_hs_error;
  660. }
  661. hs->tls13_state = state_done;
  662. return ssl_hs_flush;
  663. }
  664. enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs) {
  665. while (hs->tls13_state != state_done) {
  666. enum ssl_hs_wait_t ret = ssl_hs_error;
  667. enum server_hs_state_t state = hs->tls13_state;
  668. switch (state) {
  669. case state_select_parameters:
  670. ret = do_select_parameters(hs);
  671. break;
  672. case state_select_session:
  673. ret = do_select_session(hs);
  674. break;
  675. case state_send_hello_retry_request:
  676. ret = do_send_hello_retry_request(hs);
  677. break;
  678. case state_process_second_client_hello:
  679. ret = do_process_second_client_hello(hs);
  680. break;
  681. case state_send_server_hello:
  682. ret = do_send_server_hello(hs);
  683. break;
  684. case state_send_server_certificate_verify:
  685. ret = do_send_server_certificate_verify(hs);
  686. break;
  687. case state_send_server_finished:
  688. ret = do_send_server_finished(hs);
  689. break;
  690. case state_read_second_client_flight:
  691. ret = do_read_second_client_flight(hs);
  692. break;
  693. case state_process_end_of_early_data:
  694. ret = do_process_end_of_early_data(hs);
  695. break;
  696. case state_process_client_certificate:
  697. ret = do_process_client_certificate(hs);
  698. break;
  699. case state_process_client_certificate_verify:
  700. ret = do_process_client_certificate_verify(hs);
  701. break;
  702. case state_process_channel_id:
  703. ret = do_process_channel_id(hs);
  704. break;
  705. case state_process_client_finished:
  706. ret = do_process_client_finished(hs);
  707. break;
  708. case state_send_new_session_ticket:
  709. ret = do_send_new_session_ticket(hs);
  710. break;
  711. case state_done:
  712. ret = ssl_hs_ok;
  713. break;
  714. }
  715. if (ret != ssl_hs_ok) {
  716. return ret;
  717. }
  718. }
  719. return ssl_hs_ok;
  720. }