Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

tls13_server.cc 34 KiB

Move libssl's internals into the bssl namespace. This is horrible, but everything else I tried was worse. The goal with this CL is to take the extern "C" out of ssl/internal.h and move most symbols to namespace bssl, so we can start using C++ helpers and destructors without worry. Complications: - Public API functions must be extern "C" and match their declaration in ssl.h, which is unnamespaced. C++ really does not want you to interleave namespaced and unnamespaced things. One can actually write a namespaced extern "C" function, but this means, from C++'s perspective, the function is namespaced. Trying to namespace the public header would worked but ended up too deep a rabbithole. - Our STACK_OF macros do not work right in namespaces. - The typedefs for our exposed but opaque types are visible in the header files and copied into consuming projects as forward declarations. We ultimately want to give SSL a destructor, but clobbering an unnamespaced ssl_st::~ssl_st seems bad manners. - MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL. This CL opts for: - ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This informs the public headers to create forward declarations which are compatible with our namespaces. - For now, C++-defined type FOO ends up at bssl::FOO with a typedef outside. Later I imagine we'll rename many of them. - Internal functions get namespace bssl, so we stop worrying about stomping the tls1_prf symbol. Exported C functions are stuck as they are. Rather than try anything weird, bite the bullet and reorder files which have a mix of public and private functions. I expect that over time, the public functions will become fairly small as we move logic to more idiomatic C++. Files without any public C functions can just be written normally. - To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle in advance of them being made idiomatic C++. Bug: 132 Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581 Reviewed-on: https://boringssl-review.googlesource.com/18124 Reviewed-by: David Benjamin <davidben@google.com>
před 7 roky
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>
před 7 roky
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>
před 7 roky
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>
před 7 roky
Move libssl's internals into the bssl namespace. This is horrible, but everything else I tried was worse. The goal with this CL is to take the extern "C" out of ssl/internal.h and move most symbols to namespace bssl, so we can start using C++ helpers and destructors without worry. Complications: - Public API functions must be extern "C" and match their declaration in ssl.h, which is unnamespaced. C++ really does not want you to interleave namespaced and unnamespaced things. One can actually write a namespaced extern "C" function, but this means, from C++'s perspective, the function is namespaced. Trying to namespace the public header would worked but ended up too deep a rabbithole. - Our STACK_OF macros do not work right in namespaces. - The typedefs for our exposed but opaque types are visible in the header files and copied into consuming projects as forward declarations. We ultimately want to give SSL a destructor, but clobbering an unnamespaced ssl_st::~ssl_st seems bad manners. - MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL. This CL opts for: - ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This informs the public headers to create forward declarations which are compatible with our namespaces. - For now, C++-defined type FOO ends up at bssl::FOO with a typedef outside. Later I imagine we'll rename many of them. - Internal functions get namespace bssl, so we stop worrying about stomping the tls1_prf symbol. Exported C functions are stuck as they are. Rather than try anything weird, bite the bullet and reorder files which have a mix of public and private functions. I expect that over time, the public functions will become fairly small as we move logic to more idiomatic C++. Files without any public C functions can just be written normally. - To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle in advance of them being made idiomatic C++. Bug: 132 Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581 Reviewed-on: https://boringssl-review.googlesource.com/18124 Reviewed-by: David Benjamin <davidben@google.com>
před 7 roky
Move libssl's internals into the bssl namespace. This is horrible, but everything else I tried was worse. The goal with this CL is to take the extern "C" out of ssl/internal.h and move most symbols to namespace bssl, so we can start using C++ helpers and destructors without worry. Complications: - Public API functions must be extern "C" and match their declaration in ssl.h, which is unnamespaced. C++ really does not want you to interleave namespaced and unnamespaced things. One can actually write a namespaced extern "C" function, but this means, from C++'s perspective, the function is namespaced. Trying to namespace the public header would worked but ended up too deep a rabbithole. - Our STACK_OF macros do not work right in namespaces. - The typedefs for our exposed but opaque types are visible in the header files and copied into consuming projects as forward declarations. We ultimately want to give SSL a destructor, but clobbering an unnamespaced ssl_st::~ssl_st seems bad manners. - MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL. This CL opts for: - ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This informs the public headers to create forward declarations which are compatible with our namespaces. - For now, C++-defined type FOO ends up at bssl::FOO with a typedef outside. Later I imagine we'll rename many of them. - Internal functions get namespace bssl, so we stop worrying about stomping the tls1_prf symbol. Exported C functions are stuck as they are. Rather than try anything weird, bite the bullet and reorder files which have a mix of public and private functions. I expect that over time, the public functions will become fairly small as we move logic to more idiomatic C++. Files without any public C functions can just be written normally. - To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle in advance of them being made idiomatic C++. Bug: 132 Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581 Reviewed-on: https://boringssl-review.googlesource.com/18124 Reviewed-by: David Benjamin <davidben@google.com>
před 7 roky
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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. namespace bssl {
  33. enum server_hs_state_t {
  34. state_select_parameters = 0,
  35. state_select_session,
  36. state_send_hello_retry_request,
  37. state_read_second_client_hello,
  38. state_send_server_hello,
  39. state_send_server_certificate_verify,
  40. state_send_server_finished,
  41. state_read_second_client_flight,
  42. state_process_end_of_early_data,
  43. state_read_client_certificate,
  44. state_read_client_certificate_verify,
  45. state_read_channel_id,
  46. state_read_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, bool *out_need_retry,
  52. SSL_CLIENT_HELLO *client_hello) {
  53. SSL *const ssl = hs->ssl;
  54. *out_need_retry = false;
  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. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION);
  61. return 0;
  62. }
  63. bool found_key_share;
  64. Array<uint8_t> dhe_secret;
  65. uint8_t alert = SSL_AD_DECODE_ERROR;
  66. if (!ssl_ext_key_share_parse_clienthello(hs, &found_key_share, &dhe_secret,
  67. &alert, &key_share)) {
  68. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  69. return 0;
  70. }
  71. if (!found_key_share) {
  72. *out_need_retry = true;
  73. return 0;
  74. }
  75. return tls13_advance_key_schedule(hs, dhe_secret.data(), dhe_secret.size());
  76. }
  77. static int ssl_ext_supported_versions_add_serverhello(SSL_HANDSHAKE *hs,
  78. CBB *out) {
  79. CBB contents;
  80. if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
  81. !CBB_add_u16_length_prefixed(out, &contents) ||
  82. !CBB_add_u16(&contents, hs->ssl->version) ||
  83. !CBB_flush(out)) {
  84. return 0;
  85. }
  86. return 1;
  87. }
  88. static const SSL_CIPHER *choose_tls13_cipher(
  89. const SSL *ssl, const SSL_CLIENT_HELLO *client_hello) {
  90. if (client_hello->cipher_suites_len % 2 != 0) {
  91. return NULL;
  92. }
  93. CBS cipher_suites;
  94. CBS_init(&cipher_suites, client_hello->cipher_suites,
  95. client_hello->cipher_suites_len);
  96. const int aes_is_fine = EVP_has_aes_hardware();
  97. const uint16_t version = ssl_protocol_version(ssl);
  98. const SSL_CIPHER *best = NULL;
  99. while (CBS_len(&cipher_suites) > 0) {
  100. uint16_t cipher_suite;
  101. if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
  102. return NULL;
  103. }
  104. // Limit to TLS 1.3 ciphers we know about.
  105. const SSL_CIPHER *candidate = SSL_get_cipher_by_value(cipher_suite);
  106. if (candidate == NULL ||
  107. SSL_CIPHER_get_min_version(candidate) > version ||
  108. SSL_CIPHER_get_max_version(candidate) < version) {
  109. continue;
  110. }
  111. // TLS 1.3 removes legacy ciphers, so honor the client order, but prefer
  112. // ChaCha20 if we do not have AES hardware.
  113. if (aes_is_fine) {
  114. return candidate;
  115. }
  116. if (candidate->algorithm_enc == SSL_CHACHA20POLY1305) {
  117. return candidate;
  118. }
  119. if (best == NULL) {
  120. best = candidate;
  121. }
  122. }
  123. return best;
  124. }
  125. static int add_new_session_tickets(SSL_HANDSHAKE *hs) {
  126. SSL *const ssl = hs->ssl;
  127. // TLS 1.3 recommends single-use tickets, so issue multiple tickets in case
  128. // the client makes several connections before getting a renewal.
  129. static const int kNumTickets = 2;
  130. // Rebase the session timestamp so that it is measured from ticket
  131. // issuance.
  132. ssl_session_rebase_time(ssl, hs->new_session.get());
  133. for (int i = 0; i < kNumTickets; i++) {
  134. UniquePtr<SSL_SESSION> session(
  135. SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_INCLUDE_NONAUTH));
  136. if (!session) {
  137. return 0;
  138. }
  139. if (!RAND_bytes((uint8_t *)&session->ticket_age_add, 4)) {
  140. return 0;
  141. }
  142. session->ticket_age_add_valid = 1;
  143. if (ssl->cert->enable_early_data) {
  144. session->ticket_max_early_data = kMaxEarlyDataAccepted;
  145. }
  146. static_assert(kNumTickets < 256, "Too many tickets");
  147. uint8_t nonce[] = {static_cast<uint8_t>(i)};
  148. ScopedCBB cbb;
  149. CBB body, nonce_cbb, ticket, extensions;
  150. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  151. SSL3_MT_NEW_SESSION_TICKET) ||
  152. !CBB_add_u32(&body, session->timeout) ||
  153. !CBB_add_u32(&body, session->ticket_age_add) ||
  154. !CBB_add_u8_length_prefixed(&body, &nonce_cbb) ||
  155. !CBB_add_bytes(&nonce_cbb, nonce, sizeof(nonce)) ||
  156. !CBB_add_u16_length_prefixed(&body, &ticket) ||
  157. !tls13_derive_session_psk(session.get(), nonce) ||
  158. !ssl_encrypt_ticket(ssl, &ticket, session.get()) ||
  159. !CBB_add_u16_length_prefixed(&body, &extensions)) {
  160. return 0;
  161. }
  162. if (ssl->cert->enable_early_data) {
  163. CBB early_data_info;
  164. if (!CBB_add_u16(&extensions, TLSEXT_TYPE_early_data) ||
  165. !CBB_add_u16_length_prefixed(&extensions, &early_data_info) ||
  166. !CBB_add_u32(&early_data_info, session->ticket_max_early_data) ||
  167. !CBB_flush(&extensions)) {
  168. return 0;
  169. }
  170. }
  171. // Add a fake extension. See draft-davidben-tls-grease-01.
  172. if (!CBB_add_u16(&extensions,
  173. ssl_get_grease_value(hs, ssl_grease_ticket_extension)) ||
  174. !CBB_add_u16(&extensions, 0 /* empty */)) {
  175. return 0;
  176. }
  177. if (!ssl_add_message_cbb(ssl, cbb.get())) {
  178. return 0;
  179. }
  180. }
  181. return 1;
  182. }
  183. static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) {
  184. // At this point, most ClientHello extensions have already been processed by
  185. // the common handshake logic. Resolve the remaining non-PSK parameters.
  186. SSL *const ssl = hs->ssl;
  187. SSLMessage msg;
  188. if (!ssl->method->get_message(ssl, &msg)) {
  189. return ssl_hs_read_message;
  190. }
  191. SSL_CLIENT_HELLO client_hello;
  192. if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
  193. OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
  194. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  195. return ssl_hs_error;
  196. }
  197. OPENSSL_memcpy(hs->session_id, client_hello.session_id,
  198. client_hello.session_id_len);
  199. hs->session_id_len = client_hello.session_id_len;
  200. // Negotiate the cipher suite.
  201. hs->new_cipher = choose_tls13_cipher(ssl, &client_hello);
  202. if (hs->new_cipher == NULL) {
  203. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
  204. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  205. return ssl_hs_error;
  206. }
  207. // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
  208. // deferred. Complete it now.
  209. uint8_t alert = SSL_AD_DECODE_ERROR;
  210. if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
  211. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  212. return ssl_hs_error;
  213. }
  214. // The PRF hash is now known. Set up the key schedule and hash the
  215. // ClientHello.
  216. if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher)) {
  217. return ssl_hs_error;
  218. }
  219. if (!ssl_hash_message(hs, msg)) {
  220. return ssl_hs_error;
  221. }
  222. hs->tls13_state = state_select_session;
  223. return ssl_hs_ok;
  224. }
  225. static enum ssl_ticket_aead_result_t select_session(
  226. SSL_HANDSHAKE *hs, uint8_t *out_alert, UniquePtr<SSL_SESSION> *out_session,
  227. int32_t *out_ticket_age_skew, const SSLMessage &msg,
  228. const SSL_CLIENT_HELLO *client_hello) {
  229. SSL *const ssl = hs->ssl;
  230. *out_session = NULL;
  231. // Decode the ticket if we agreed on a PSK key exchange mode.
  232. CBS pre_shared_key;
  233. if (!hs->accept_psk_mode ||
  234. !ssl_client_hello_get_extension(client_hello, &pre_shared_key,
  235. TLSEXT_TYPE_pre_shared_key)) {
  236. return ssl_ticket_aead_ignore_ticket;
  237. }
  238. // Verify that the pre_shared_key extension is the last extension in
  239. // ClientHello.
  240. if (CBS_data(&pre_shared_key) + CBS_len(&pre_shared_key) !=
  241. client_hello->extensions + client_hello->extensions_len) {
  242. OPENSSL_PUT_ERROR(SSL, SSL_R_PRE_SHARED_KEY_MUST_BE_LAST);
  243. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  244. return ssl_ticket_aead_error;
  245. }
  246. CBS ticket, binders;
  247. uint32_t client_ticket_age;
  248. if (!ssl_ext_pre_shared_key_parse_clienthello(hs, &ticket, &binders,
  249. &client_ticket_age, out_alert,
  250. &pre_shared_key)) {
  251. return ssl_ticket_aead_error;
  252. }
  253. // TLS 1.3 session tickets are renewed separately as part of the
  254. // NewSessionTicket.
  255. bool unused_renew;
  256. UniquePtr<SSL_SESSION> session;
  257. enum ssl_ticket_aead_result_t ret =
  258. ssl_process_ticket(ssl, &session, &unused_renew, CBS_data(&ticket),
  259. CBS_len(&ticket), NULL, 0);
  260. switch (ret) {
  261. case ssl_ticket_aead_success:
  262. break;
  263. case ssl_ticket_aead_error:
  264. *out_alert = SSL_AD_INTERNAL_ERROR;
  265. return ret;
  266. default:
  267. return ret;
  268. }
  269. if (!ssl_session_is_resumable(hs, session.get()) ||
  270. // Historically, some TLS 1.3 tickets were missing ticket_age_add.
  271. !session->ticket_age_add_valid) {
  272. return ssl_ticket_aead_ignore_ticket;
  273. }
  274. // Recover the client ticket age and convert to seconds.
  275. client_ticket_age -= session->ticket_age_add;
  276. client_ticket_age /= 1000;
  277. struct OPENSSL_timeval now;
  278. ssl_get_current_time(ssl, &now);
  279. // Compute the server ticket age in seconds.
  280. assert(now.tv_sec >= session->time);
  281. uint64_t server_ticket_age = now.tv_sec - session->time;
  282. // To avoid overflowing |hs->ticket_age_skew|, we will not resume
  283. // 68-year-old sessions.
  284. if (server_ticket_age > INT32_MAX) {
  285. return ssl_ticket_aead_ignore_ticket;
  286. }
  287. // TODO(davidben,svaldez): Measure this value to decide on tolerance. For
  288. // now, accept all values. https://crbug.com/boringssl/113.
  289. *out_ticket_age_skew =
  290. (int32_t)client_ticket_age - (int32_t)server_ticket_age;
  291. // Check the PSK binder.
  292. if (!tls13_verify_psk_binder(hs, session.get(), msg, &binders)) {
  293. *out_alert = SSL_AD_DECRYPT_ERROR;
  294. return ssl_ticket_aead_error;
  295. }
  296. *out_session = std::move(session);
  297. return ssl_ticket_aead_success;
  298. }
  299. static enum ssl_hs_wait_t do_select_session(SSL_HANDSHAKE *hs) {
  300. SSL *const ssl = hs->ssl;
  301. SSLMessage msg;
  302. if (!ssl->method->get_message(ssl, &msg)) {
  303. return ssl_hs_read_message;
  304. }
  305. SSL_CLIENT_HELLO client_hello;
  306. if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
  307. OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
  308. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  309. return ssl_hs_error;
  310. }
  311. uint8_t alert = SSL_AD_DECODE_ERROR;
  312. UniquePtr<SSL_SESSION> session;
  313. switch (select_session(hs, &alert, &session, &ssl->s3->ticket_age_skew, msg,
  314. &client_hello)) {
  315. case ssl_ticket_aead_ignore_ticket:
  316. assert(!session);
  317. if (!ssl_get_new_session(hs, 1 /* server */)) {
  318. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  319. return ssl_hs_error;
  320. }
  321. break;
  322. case ssl_ticket_aead_success:
  323. // Carry over authentication information from the previous handshake into
  324. // a fresh session.
  325. hs->new_session =
  326. SSL_SESSION_dup(session.get(), SSL_SESSION_DUP_AUTH_ONLY);
  327. if (ssl->cert->enable_early_data &&
  328. // Early data must be acceptable for this ticket.
  329. session->ticket_max_early_data != 0 &&
  330. // The client must have offered early data.
  331. hs->early_data_offered &&
  332. // Channel ID is incompatible with 0-RTT.
  333. !ssl->s3->tlsext_channel_id_valid &&
  334. // If Token Binding is negotiated, reject 0-RTT.
  335. !ssl->s3->token_binding_negotiated &&
  336. // Custom extensions is incompatible with 0-RTT.
  337. hs->custom_extensions.received == 0 &&
  338. // The negotiated ALPN must match the one in the ticket.
  339. ssl->s3->alpn_selected ==
  340. MakeConstSpan(session->early_alpn, session->early_alpn_len)) {
  341. ssl->s3->early_data_accepted = true;
  342. }
  343. if (hs->new_session == NULL) {
  344. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  345. return ssl_hs_error;
  346. }
  347. ssl->s3->session_reused = true;
  348. // Resumption incorporates fresh key material, so refresh the timeout.
  349. ssl_session_renew_timeout(ssl, hs->new_session.get(),
  350. ssl->session_ctx->session_psk_dhe_timeout);
  351. break;
  352. case ssl_ticket_aead_error:
  353. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  354. return ssl_hs_error;
  355. case ssl_ticket_aead_retry:
  356. hs->tls13_state = state_select_session;
  357. return ssl_hs_pending_ticket;
  358. }
  359. // Record connection properties in the new session.
  360. hs->new_session->cipher = hs->new_cipher;
  361. // Store the initial negotiated ALPN in the session.
  362. if (!ssl->s3->alpn_selected.empty()) {
  363. hs->new_session->early_alpn = (uint8_t *)BUF_memdup(
  364. ssl->s3->alpn_selected.data(), ssl->s3->alpn_selected.size());
  365. if (hs->new_session->early_alpn == NULL) {
  366. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  367. return ssl_hs_error;
  368. }
  369. hs->new_session->early_alpn_len = ssl->s3->alpn_selected.size();
  370. }
  371. if (ssl->ctx->dos_protection_cb != NULL &&
  372. ssl->ctx->dos_protection_cb(&client_hello) == 0) {
  373. // Connection rejected for DOS reasons.
  374. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
  375. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  376. return ssl_hs_error;
  377. }
  378. size_t hash_len = EVP_MD_size(
  379. ssl_get_handshake_digest(ssl_protocol_version(ssl), hs->new_cipher));
  380. // Set up the key schedule and incorporate the PSK into the running secret.
  381. if (ssl->s3->session_reused) {
  382. if (!tls13_init_key_schedule(hs, hs->new_session->master_key,
  383. hs->new_session->master_key_length)) {
  384. return ssl_hs_error;
  385. }
  386. } else if (!tls13_init_key_schedule(hs, kZeroes, hash_len)) {
  387. return ssl_hs_error;
  388. }
  389. if (ssl->s3->early_data_accepted) {
  390. if (!tls13_derive_early_secrets(hs)) {
  391. return ssl_hs_error;
  392. }
  393. } else if (hs->early_data_offered) {
  394. ssl->s3->skip_early_data = true;
  395. }
  396. // Resolve ECDHE and incorporate it into the secret.
  397. bool need_retry;
  398. if (!resolve_ecdhe_secret(hs, &need_retry, &client_hello)) {
  399. if (need_retry) {
  400. ssl->s3->early_data_accepted = false;
  401. ssl->s3->skip_early_data = true;
  402. ssl->method->next_message(ssl);
  403. if (!hs->transcript.UpdateForHelloRetryRequest()) {
  404. return ssl_hs_error;
  405. }
  406. hs->tls13_state = state_send_hello_retry_request;
  407. return ssl_hs_ok;
  408. }
  409. return ssl_hs_error;
  410. }
  411. ssl->method->next_message(ssl);
  412. hs->tls13_state = state_send_server_hello;
  413. return ssl_hs_ok;
  414. }
  415. static enum ssl_hs_wait_t do_send_hello_retry_request(SSL_HANDSHAKE *hs) {
  416. SSL *const ssl = hs->ssl;
  417. ScopedCBB cbb;
  418. CBB body, session_id, extensions;
  419. uint16_t group_id;
  420. if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
  421. !CBB_add_u16(&body, TLS1_2_VERSION) ||
  422. !CBB_add_bytes(&body, kHelloRetryRequest, SSL3_RANDOM_SIZE) ||
  423. !CBB_add_u8_length_prefixed(&body, &session_id) ||
  424. !CBB_add_bytes(&session_id, hs->session_id, hs->session_id_len) ||
  425. !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
  426. !CBB_add_u8(&body, 0 /* no compression */) ||
  427. !tls1_get_shared_group(hs, &group_id) ||
  428. !CBB_add_u16_length_prefixed(&body, &extensions) ||
  429. !CBB_add_u16(&extensions, TLSEXT_TYPE_supported_versions) ||
  430. !CBB_add_u16(&extensions, 2 /* length */) ||
  431. !CBB_add_u16(&extensions, ssl->version) ||
  432. !CBB_add_u16(&extensions, TLSEXT_TYPE_key_share) ||
  433. !CBB_add_u16(&extensions, 2 /* length */) ||
  434. !CBB_add_u16(&extensions, group_id) ||
  435. !ssl_add_message_cbb(ssl, cbb.get())) {
  436. return ssl_hs_error;
  437. }
  438. if (!ssl->method->add_change_cipher_spec(ssl)) {
  439. return ssl_hs_error;
  440. }
  441. hs->sent_hello_retry_request = true;
  442. hs->tls13_state = state_read_second_client_hello;
  443. return ssl_hs_flush;
  444. }
  445. static enum ssl_hs_wait_t do_read_second_client_hello(SSL_HANDSHAKE *hs) {
  446. SSL *const ssl = hs->ssl;
  447. SSLMessage msg;
  448. if (!ssl->method->get_message(ssl, &msg)) {
  449. return ssl_hs_read_message;
  450. }
  451. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) {
  452. return ssl_hs_error;
  453. }
  454. SSL_CLIENT_HELLO client_hello;
  455. if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
  456. OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
  457. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  458. return ssl_hs_error;
  459. }
  460. bool need_retry;
  461. if (!resolve_ecdhe_secret(hs, &need_retry, &client_hello)) {
  462. if (need_retry) {
  463. // Only send one HelloRetryRequest.
  464. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  465. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
  466. }
  467. return ssl_hs_error;
  468. }
  469. if (!ssl_hash_message(hs, msg)) {
  470. return ssl_hs_error;
  471. }
  472. ssl->method->next_message(ssl);
  473. hs->tls13_state = state_send_server_hello;
  474. return ssl_hs_ok;
  475. }
  476. static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
  477. SSL *const ssl = hs->ssl;
  478. // Send a ServerHello.
  479. ScopedCBB cbb;
  480. CBB body, extensions, session_id;
  481. if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
  482. !CBB_add_u16(&body, TLS1_2_VERSION) ||
  483. !RAND_bytes(ssl->s3->server_random, sizeof(ssl->s3->server_random)) ||
  484. !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
  485. !CBB_add_u8_length_prefixed(&body, &session_id) ||
  486. !CBB_add_bytes(&session_id, hs->session_id, hs->session_id_len) ||
  487. !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
  488. !CBB_add_u8(&body, 0) ||
  489. !CBB_add_u16_length_prefixed(&body, &extensions) ||
  490. !ssl_ext_pre_shared_key_add_serverhello(hs, &extensions) ||
  491. !ssl_ext_key_share_add_serverhello(hs, &extensions) ||
  492. !ssl_ext_supported_versions_add_serverhello(hs, &extensions) ||
  493. !ssl_add_message_cbb(ssl, cbb.get())) {
  494. return ssl_hs_error;
  495. }
  496. if (!hs->sent_hello_retry_request &&
  497. !ssl->method->add_change_cipher_spec(ssl)) {
  498. return ssl_hs_error;
  499. }
  500. // Derive and enable the handshake traffic secrets.
  501. if (!tls13_derive_handshake_secrets(hs) ||
  502. !tls13_set_traffic_key(ssl, evp_aead_seal, hs->server_handshake_secret,
  503. hs->hash_len)) {
  504. return ssl_hs_error;
  505. }
  506. // Send EncryptedExtensions.
  507. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  508. SSL3_MT_ENCRYPTED_EXTENSIONS) ||
  509. !ssl_add_serverhello_tlsext(hs, &body) ||
  510. !ssl_add_message_cbb(ssl, cbb.get())) {
  511. return ssl_hs_error;
  512. }
  513. if (!ssl->s3->session_reused) {
  514. // Determine whether to request a client certificate.
  515. hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
  516. // Only request a certificate if Channel ID isn't negotiated.
  517. if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
  518. ssl->s3->tlsext_channel_id_valid) {
  519. hs->cert_request = false;
  520. }
  521. }
  522. // Send a CertificateRequest, if necessary.
  523. if (hs->cert_request) {
  524. CBB cert_request_extensions, sigalg_contents, sigalgs_cbb;
  525. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  526. SSL3_MT_CERTIFICATE_REQUEST) ||
  527. !CBB_add_u8(&body, 0 /* no certificate_request_context. */) ||
  528. !CBB_add_u16_length_prefixed(&body, &cert_request_extensions) ||
  529. !CBB_add_u16(&cert_request_extensions,
  530. TLSEXT_TYPE_signature_algorithms) ||
  531. !CBB_add_u16_length_prefixed(&cert_request_extensions,
  532. &sigalg_contents) ||
  533. !CBB_add_u16_length_prefixed(&sigalg_contents, &sigalgs_cbb) ||
  534. !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb,
  535. false /* online signature */)) {
  536. return ssl_hs_error;
  537. }
  538. if (tls12_has_different_verify_sigalgs_for_certs(ssl)) {
  539. if (!CBB_add_u16(&cert_request_extensions,
  540. TLSEXT_TYPE_signature_algorithms_cert) ||
  541. !CBB_add_u16_length_prefixed(&cert_request_extensions,
  542. &sigalg_contents) ||
  543. !CBB_add_u16_length_prefixed(&sigalg_contents, &sigalgs_cbb) ||
  544. !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb, true /* certs */)) {
  545. return ssl_hs_error;
  546. }
  547. }
  548. if (ssl_has_client_CAs(ssl)) {
  549. CBB ca_contents;
  550. if (!CBB_add_u16(&cert_request_extensions,
  551. TLSEXT_TYPE_certificate_authorities) ||
  552. !CBB_add_u16_length_prefixed(&cert_request_extensions,
  553. &ca_contents) ||
  554. !ssl_add_client_CA_list(ssl, &ca_contents) ||
  555. !CBB_flush(&cert_request_extensions)) {
  556. return ssl_hs_error;
  557. }
  558. }
  559. if (!ssl_add_message_cbb(ssl, cbb.get())) {
  560. return ssl_hs_error;
  561. }
  562. }
  563. // Send the server Certificate message, if necessary.
  564. if (!ssl->s3->session_reused) {
  565. if (!ssl_has_certificate(ssl)) {
  566. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
  567. return ssl_hs_error;
  568. }
  569. if (!tls13_add_certificate(hs)) {
  570. return ssl_hs_error;
  571. }
  572. hs->tls13_state = state_send_server_certificate_verify;
  573. return ssl_hs_ok;
  574. }
  575. hs->tls13_state = state_send_server_finished;
  576. return ssl_hs_ok;
  577. }
  578. static enum ssl_hs_wait_t do_send_server_certificate_verify(SSL_HANDSHAKE *hs) {
  579. switch (tls13_add_certificate_verify(hs)) {
  580. case ssl_private_key_success:
  581. hs->tls13_state = state_send_server_finished;
  582. return ssl_hs_ok;
  583. case ssl_private_key_retry:
  584. hs->tls13_state = state_send_server_certificate_verify;
  585. return ssl_hs_private_key_operation;
  586. case ssl_private_key_failure:
  587. return ssl_hs_error;
  588. }
  589. assert(0);
  590. return ssl_hs_error;
  591. }
  592. static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
  593. SSL *const ssl = hs->ssl;
  594. if (!tls13_add_finished(hs) ||
  595. // Update the secret to the master secret and derive traffic keys.
  596. !tls13_advance_key_schedule(hs, kZeroes, hs->hash_len) ||
  597. !tls13_derive_application_secrets(hs) ||
  598. !tls13_set_traffic_key(ssl, evp_aead_seal, hs->server_traffic_secret_0,
  599. hs->hash_len)) {
  600. return ssl_hs_error;
  601. }
  602. if (ssl->s3->early_data_accepted) {
  603. // If accepting 0-RTT, we send tickets half-RTT. This gets the tickets on
  604. // the wire sooner and also avoids triggering a write on |SSL_read| when
  605. // processing the client Finished. This requires computing the client
  606. // Finished early. See draft-ietf-tls-tls13-18, section 4.5.1.
  607. static const uint8_t kEndOfEarlyData[4] = {SSL3_MT_END_OF_EARLY_DATA, 0,
  608. 0, 0};
  609. if (!hs->transcript.Update(kEndOfEarlyData)) {
  610. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  611. return ssl_hs_error;
  612. }
  613. size_t finished_len;
  614. if (!tls13_finished_mac(hs, hs->expected_client_finished, &finished_len,
  615. 0 /* client */)) {
  616. return ssl_hs_error;
  617. }
  618. if (finished_len != hs->hash_len) {
  619. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  620. return ssl_hs_error;
  621. }
  622. // Feed the predicted Finished into the transcript. This allows us to derive
  623. // the resumption secret early and send half-RTT tickets.
  624. //
  625. // TODO(davidben): This will need to be updated for DTLS 1.3.
  626. assert(!SSL_is_dtls(hs->ssl));
  627. assert(hs->hash_len <= 0xff);
  628. uint8_t header[4] = {SSL3_MT_FINISHED, 0, 0,
  629. static_cast<uint8_t>(hs->hash_len)};
  630. if (!hs->transcript.Update(header) ||
  631. !hs->transcript.Update(
  632. MakeConstSpan(hs->expected_client_finished, hs->hash_len)) ||
  633. !tls13_derive_resumption_secret(hs) ||
  634. !add_new_session_tickets(hs)) {
  635. return ssl_hs_error;
  636. }
  637. }
  638. hs->tls13_state = state_read_second_client_flight;
  639. return ssl_hs_flush;
  640. }
  641. static enum ssl_hs_wait_t do_read_second_client_flight(SSL_HANDSHAKE *hs) {
  642. SSL *const ssl = hs->ssl;
  643. if (ssl->s3->early_data_accepted) {
  644. if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->early_traffic_secret,
  645. hs->hash_len)) {
  646. return ssl_hs_error;
  647. }
  648. hs->can_early_write = true;
  649. hs->can_early_read = true;
  650. hs->in_early_data = true;
  651. }
  652. hs->tls13_state = state_process_end_of_early_data;
  653. return ssl->s3->early_data_accepted ? ssl_hs_read_end_of_early_data
  654. : ssl_hs_ok;
  655. }
  656. static enum ssl_hs_wait_t do_process_end_of_early_data(SSL_HANDSHAKE *hs) {
  657. SSL *const ssl = hs->ssl;
  658. if (hs->early_data_offered) {
  659. // If early data was not accepted, the EndOfEarlyData and ChangeCipherSpec
  660. // message will be in the discarded early data.
  661. if (hs->ssl->s3->early_data_accepted) {
  662. SSLMessage msg;
  663. if (!ssl->method->get_message(ssl, &msg)) {
  664. return ssl_hs_read_message;
  665. }
  666. if (!ssl_check_message_type(ssl, msg, SSL3_MT_END_OF_EARLY_DATA)) {
  667. return ssl_hs_error;
  668. }
  669. if (CBS_len(&msg.body) != 0) {
  670. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  671. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  672. return ssl_hs_error;
  673. }
  674. ssl->method->next_message(ssl);
  675. }
  676. }
  677. if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->client_handshake_secret,
  678. hs->hash_len)) {
  679. return ssl_hs_error;
  680. }
  681. hs->tls13_state = ssl->s3->early_data_accepted
  682. ? state_read_client_finished
  683. : state_read_client_certificate;
  684. return ssl_hs_ok;
  685. }
  686. static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
  687. SSL *const ssl = hs->ssl;
  688. if (!hs->cert_request) {
  689. // OpenSSL returns X509_V_OK when no certificates are requested. This is
  690. // classed by them as a bug, but it's assumed by at least NGINX.
  691. hs->new_session->verify_result = X509_V_OK;
  692. // Skip this state.
  693. hs->tls13_state = state_read_channel_id;
  694. return ssl_hs_ok;
  695. }
  696. const int allow_anonymous =
  697. (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) == 0;
  698. SSLMessage msg;
  699. if (!ssl->method->get_message(ssl, &msg)) {
  700. return ssl_hs_read_message;
  701. }
  702. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
  703. !tls13_process_certificate(hs, msg, allow_anonymous) ||
  704. !ssl_hash_message(hs, msg)) {
  705. return ssl_hs_error;
  706. }
  707. ssl->method->next_message(ssl);
  708. hs->tls13_state = state_read_client_certificate_verify;
  709. return ssl_hs_ok;
  710. }
  711. static enum ssl_hs_wait_t do_read_client_certificate_verify(
  712. SSL_HANDSHAKE *hs) {
  713. SSL *const ssl = hs->ssl;
  714. if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
  715. // Skip this state.
  716. hs->tls13_state = state_read_channel_id;
  717. return ssl_hs_ok;
  718. }
  719. SSLMessage msg;
  720. if (!ssl->method->get_message(ssl, &msg)) {
  721. return ssl_hs_read_message;
  722. }
  723. switch (ssl_verify_peer_cert(hs)) {
  724. case ssl_verify_ok:
  725. break;
  726. case ssl_verify_invalid:
  727. return ssl_hs_error;
  728. case ssl_verify_retry:
  729. hs->tls13_state = state_read_client_certificate_verify;
  730. return ssl_hs_certificate_verify;
  731. }
  732. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY) ||
  733. !tls13_process_certificate_verify(hs, msg) ||
  734. !ssl_hash_message(hs, msg)) {
  735. return ssl_hs_error;
  736. }
  737. ssl->method->next_message(ssl);
  738. hs->tls13_state = state_read_channel_id;
  739. return ssl_hs_ok;
  740. }
  741. static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
  742. SSL *const ssl = hs->ssl;
  743. if (!ssl->s3->tlsext_channel_id_valid) {
  744. hs->tls13_state = state_read_client_finished;
  745. return ssl_hs_ok;
  746. }
  747. SSLMessage msg;
  748. if (!ssl->method->get_message(ssl, &msg)) {
  749. return ssl_hs_read_message;
  750. }
  751. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
  752. !tls1_verify_channel_id(hs, msg) ||
  753. !ssl_hash_message(hs, msg)) {
  754. return ssl_hs_error;
  755. }
  756. ssl->method->next_message(ssl);
  757. hs->tls13_state = state_read_client_finished;
  758. return ssl_hs_ok;
  759. }
  760. static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
  761. SSL *const ssl = hs->ssl;
  762. SSLMessage msg;
  763. if (!ssl->method->get_message(ssl, &msg)) {
  764. return ssl_hs_read_message;
  765. }
  766. if (!ssl_check_message_type(ssl, msg, SSL3_MT_FINISHED) ||
  767. // If early data was accepted, we've already computed the client Finished
  768. // and derived the resumption secret.
  769. !tls13_process_finished(hs, msg, ssl->s3->early_data_accepted) ||
  770. // evp_aead_seal keys have already been switched.
  771. !tls13_set_traffic_key(ssl, evp_aead_open, hs->client_traffic_secret_0,
  772. hs->hash_len)) {
  773. return ssl_hs_error;
  774. }
  775. if (!ssl->s3->early_data_accepted) {
  776. if (!ssl_hash_message(hs, msg) ||
  777. !tls13_derive_resumption_secret(hs)) {
  778. return ssl_hs_error;
  779. }
  780. // We send post-handshake tickets as part of the handshake in 1-RTT.
  781. hs->tls13_state = state_send_new_session_ticket;
  782. } else {
  783. // We already sent half-RTT tickets.
  784. hs->tls13_state = state_done;
  785. }
  786. ssl->method->next_message(ssl);
  787. return ssl_hs_ok;
  788. }
  789. static enum ssl_hs_wait_t do_send_new_session_ticket(SSL_HANDSHAKE *hs) {
  790. // If the client doesn't accept resumption with PSK_DHE_KE, don't send a
  791. // session ticket.
  792. if (!hs->accept_psk_mode) {
  793. hs->tls13_state = state_done;
  794. return ssl_hs_ok;
  795. }
  796. if (!add_new_session_tickets(hs)) {
  797. return ssl_hs_error;
  798. }
  799. hs->tls13_state = state_done;
  800. return ssl_hs_flush;
  801. }
  802. enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs) {
  803. while (hs->tls13_state != state_done) {
  804. enum ssl_hs_wait_t ret = ssl_hs_error;
  805. enum server_hs_state_t state =
  806. static_cast<enum server_hs_state_t>(hs->tls13_state);
  807. switch (state) {
  808. case state_select_parameters:
  809. ret = do_select_parameters(hs);
  810. break;
  811. case state_select_session:
  812. ret = do_select_session(hs);
  813. break;
  814. case state_send_hello_retry_request:
  815. ret = do_send_hello_retry_request(hs);
  816. break;
  817. case state_read_second_client_hello:
  818. ret = do_read_second_client_hello(hs);
  819. break;
  820. case state_send_server_hello:
  821. ret = do_send_server_hello(hs);
  822. break;
  823. case state_send_server_certificate_verify:
  824. ret = do_send_server_certificate_verify(hs);
  825. break;
  826. case state_send_server_finished:
  827. ret = do_send_server_finished(hs);
  828. break;
  829. case state_read_second_client_flight:
  830. ret = do_read_second_client_flight(hs);
  831. break;
  832. case state_process_end_of_early_data:
  833. ret = do_process_end_of_early_data(hs);
  834. break;
  835. case state_read_client_certificate:
  836. ret = do_read_client_certificate(hs);
  837. break;
  838. case state_read_client_certificate_verify:
  839. ret = do_read_client_certificate_verify(hs);
  840. break;
  841. case state_read_channel_id:
  842. ret = do_read_channel_id(hs);
  843. break;
  844. case state_read_client_finished:
  845. ret = do_read_client_finished(hs);
  846. break;
  847. case state_send_new_session_ticket:
  848. ret = do_send_new_session_ticket(hs);
  849. break;
  850. case state_done:
  851. ret = ssl_hs_ok;
  852. break;
  853. }
  854. if (hs->tls13_state != state) {
  855. ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
  856. }
  857. if (ret != ssl_hs_ok) {
  858. return ret;
  859. }
  860. }
  861. return ssl_hs_ok;
  862. }
  863. const char *tls13_server_handshake_state(SSL_HANDSHAKE *hs) {
  864. enum server_hs_state_t state =
  865. static_cast<enum server_hs_state_t>(hs->tls13_state);
  866. switch (state) {
  867. case state_select_parameters:
  868. return "TLS 1.3 server select_parameters";
  869. case state_select_session:
  870. return "TLS 1.3 server select_session";
  871. case state_send_hello_retry_request:
  872. return "TLS 1.3 server send_hello_retry_request";
  873. case state_read_second_client_hello:
  874. return "TLS 1.3 server read_second_client_hello";
  875. case state_send_server_hello:
  876. return "TLS 1.3 server send_server_hello";
  877. case state_send_server_certificate_verify:
  878. return "TLS 1.3 server send_server_certificate_verify";
  879. case state_send_server_finished:
  880. return "TLS 1.3 server send_server_finished";
  881. case state_read_second_client_flight:
  882. return "TLS 1.3 server read_second_client_flight";
  883. case state_process_end_of_early_data:
  884. return "TLS 1.3 server process_end_of_early_data";
  885. case state_read_client_certificate:
  886. return "TLS 1.3 server read_client_certificate";
  887. case state_read_client_certificate_verify:
  888. return "TLS 1.3 server read_client_certificate_verify";
  889. case state_read_channel_id:
  890. return "TLS 1.3 server read_channel_id";
  891. case state_read_client_finished:
  892. return "TLS 1.3 server read_client_finished";
  893. case state_send_new_session_ticket:
  894. return "TLS 1.3 server send_new_session_ticket";
  895. case state_done:
  896. return "TLS 1.3 server done";
  897. }
  898. return "TLS 1.3 server unknown";
  899. }
  900. } // namespace bssl