25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

tls13_client.cc 29 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>
7 yıl önce
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 yıl önce
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 yıl önce
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 yıl önce
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 yıl önce
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>
7 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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 <limits.h>
  17. #include <string.h>
  18. #include <utility>
  19. #include <openssl/bytestring.h>
  20. #include <openssl/digest.h>
  21. #include <openssl/err.h>
  22. #include <openssl/mem.h>
  23. #include <openssl/stack.h>
  24. #include "../crypto/internal.h"
  25. #include "internal.h"
  26. namespace bssl {
  27. enum client_hs_state_t {
  28. state_read_hello_retry_request = 0,
  29. state_send_second_client_hello,
  30. state_read_server_hello,
  31. state_read_encrypted_extensions,
  32. state_read_certificate_request,
  33. state_read_server_certificate,
  34. state_read_server_certificate_verify,
  35. state_read_server_finished,
  36. state_send_end_of_early_data,
  37. state_send_client_certificate,
  38. state_send_client_certificate_verify,
  39. state_complete_second_flight,
  40. state_done,
  41. };
  42. static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0};
  43. static enum ssl_hs_wait_t do_read_hello_retry_request(SSL_HANDSHAKE *hs) {
  44. SSL *const ssl = hs->ssl;
  45. assert(ssl->s3->have_version);
  46. SSLMessage msg;
  47. if (!ssl->method->get_message(ssl, &msg)) {
  48. return ssl_hs_read_message;
  49. }
  50. // Queue up a ChangeCipherSpec for whenever we next send something. This
  51. // will be before the second ClientHello. If we offered early data, this was
  52. // already done.
  53. if (!hs->early_data_offered &&
  54. !ssl->method->add_change_cipher_spec(ssl)) {
  55. return ssl_hs_error;
  56. }
  57. if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) {
  58. return ssl_hs_error;
  59. }
  60. CBS body = msg.body, extensions, server_random, session_id;
  61. uint16_t server_version, cipher_suite;
  62. uint8_t compression_method;
  63. if (!CBS_get_u16(&body, &server_version) ||
  64. !CBS_get_bytes(&body, &server_random, SSL3_RANDOM_SIZE) ||
  65. !CBS_get_u8_length_prefixed(&body, &session_id) ||
  66. !CBS_mem_equal(&session_id, hs->session_id, hs->session_id_len) ||
  67. !CBS_get_u16(&body, &cipher_suite) ||
  68. !CBS_get_u8(&body, &compression_method) ||
  69. compression_method != 0 ||
  70. !CBS_get_u16_length_prefixed(&body, &extensions) ||
  71. CBS_len(&extensions) == 0 ||
  72. CBS_len(&body) != 0) {
  73. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  74. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  75. return ssl_hs_error;
  76. }
  77. if (!CBS_mem_equal(&server_random, kHelloRetryRequest, SSL3_RANDOM_SIZE)) {
  78. hs->tls13_state = state_read_server_hello;
  79. return ssl_hs_ok;
  80. }
  81. const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite);
  82. // Check if the cipher is a TLS 1.3 cipher.
  83. if (cipher == NULL ||
  84. SSL_CIPHER_get_min_version(cipher) > ssl_protocol_version(ssl) ||
  85. SSL_CIPHER_get_max_version(cipher) < ssl_protocol_version(ssl)) {
  86. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
  87. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  88. return ssl_hs_error;
  89. }
  90. hs->new_cipher = cipher;
  91. if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
  92. !hs->transcript.UpdateForHelloRetryRequest()) {
  93. return ssl_hs_error;
  94. }
  95. bool have_cookie, have_key_share, have_supported_versions;
  96. CBS cookie, key_share, supported_versions;
  97. SSL_EXTENSION_TYPE ext_types[] = {
  98. {TLSEXT_TYPE_key_share, &have_key_share, &key_share},
  99. {TLSEXT_TYPE_cookie, &have_cookie, &cookie},
  100. {TLSEXT_TYPE_supported_versions, &have_supported_versions,
  101. &supported_versions},
  102. };
  103. uint8_t alert = SSL_AD_DECODE_ERROR;
  104. if (!ssl_parse_extensions(&extensions, &alert, ext_types,
  105. OPENSSL_ARRAY_SIZE(ext_types),
  106. 0 /* reject unknown */)) {
  107. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  108. return ssl_hs_error;
  109. }
  110. if (!have_cookie && !have_key_share) {
  111. OPENSSL_PUT_ERROR(SSL, SSL_R_EMPTY_HELLO_RETRY_REQUEST);
  112. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  113. return ssl_hs_error;
  114. }
  115. if (have_cookie) {
  116. CBS cookie_value;
  117. if (!CBS_get_u16_length_prefixed(&cookie, &cookie_value) ||
  118. CBS_len(&cookie_value) == 0 ||
  119. CBS_len(&cookie) != 0) {
  120. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  121. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  122. return ssl_hs_error;
  123. }
  124. if (!hs->cookie.CopyFrom(cookie_value)) {
  125. return ssl_hs_error;
  126. }
  127. }
  128. if (have_key_share) {
  129. uint16_t group_id;
  130. if (!CBS_get_u16(&key_share, &group_id) || CBS_len(&key_share) != 0) {
  131. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  132. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  133. return ssl_hs_error;
  134. }
  135. // The group must be supported.
  136. if (!tls1_check_group_id(ssl, group_id)) {
  137. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  138. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
  139. return ssl_hs_error;
  140. }
  141. // Check that the HelloRetryRequest does not request the key share that
  142. // was provided in the initial ClientHello.
  143. if (hs->key_share->GroupID() == group_id) {
  144. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  145. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
  146. return ssl_hs_error;
  147. }
  148. hs->key_share.reset();
  149. hs->retry_group = group_id;
  150. }
  151. if (!ssl_hash_message(hs, msg)) {
  152. return ssl_hs_error;
  153. }
  154. ssl->method->next_message(ssl);
  155. hs->received_hello_retry_request = true;
  156. hs->tls13_state = state_send_second_client_hello;
  157. // 0-RTT is rejected if we receive a HelloRetryRequest.
  158. if (hs->in_early_data) {
  159. return ssl_hs_early_data_rejected;
  160. }
  161. return ssl_hs_ok;
  162. }
  163. static enum ssl_hs_wait_t do_send_second_client_hello(SSL_HANDSHAKE *hs) {
  164. SSL *const ssl = hs->ssl;
  165. // Restore the null cipher. We may have switched due to 0-RTT.
  166. bssl::UniquePtr<SSLAEADContext> null_ctx =
  167. SSLAEADContext::CreateNullCipher(SSL_is_dtls(ssl));
  168. if (!null_ctx ||
  169. !ssl->method->set_write_state(ssl, std::move(null_ctx))) {
  170. return ssl_hs_error;
  171. }
  172. ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
  173. if (!ssl_write_client_hello(hs)) {
  174. return ssl_hs_error;
  175. }
  176. hs->tls13_state = state_read_server_hello;
  177. return ssl_hs_flush;
  178. }
  179. static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) {
  180. SSL *const ssl = hs->ssl;
  181. SSLMessage msg;
  182. if (!ssl->method->get_message(ssl, &msg)) {
  183. return ssl_hs_read_message;
  184. }
  185. if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) {
  186. return ssl_hs_error;
  187. }
  188. CBS body = msg.body, server_random, session_id, extensions;
  189. uint16_t server_version;
  190. uint16_t cipher_suite;
  191. uint8_t compression_method;
  192. if (!CBS_get_u16(&body, &server_version) ||
  193. !CBS_get_bytes(&body, &server_random, SSL3_RANDOM_SIZE) ||
  194. !CBS_get_u8_length_prefixed(&body, &session_id) ||
  195. !CBS_mem_equal(&session_id, hs->session_id, hs->session_id_len) ||
  196. !CBS_get_u16(&body, &cipher_suite) ||
  197. !CBS_get_u8(&body, &compression_method) ||
  198. compression_method != 0 ||
  199. !CBS_get_u16_length_prefixed(&body, &extensions) ||
  200. CBS_len(&body) != 0) {
  201. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  202. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  203. return ssl_hs_error;
  204. }
  205. if (server_version != TLS1_2_VERSION) {
  206. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  207. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER);
  208. return ssl_hs_error;
  209. }
  210. // Forbid a second HelloRetryRequest.
  211. if (CBS_mem_equal(&server_random, kHelloRetryRequest, SSL3_RANDOM_SIZE)) {
  212. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  213. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  214. return ssl_hs_error;
  215. }
  216. OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
  217. SSL3_RANDOM_SIZE);
  218. // Check if the cipher is a TLS 1.3 cipher.
  219. const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite);
  220. if (cipher == nullptr ||
  221. SSL_CIPHER_get_min_version(cipher) > ssl_protocol_version(ssl) ||
  222. SSL_CIPHER_get_max_version(cipher) < ssl_protocol_version(ssl)) {
  223. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
  224. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  225. return ssl_hs_error;
  226. }
  227. // Check that the cipher matches the one in the HelloRetryRequest.
  228. if (hs->received_hello_retry_request &&
  229. hs->new_cipher != cipher) {
  230. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
  231. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  232. return ssl_hs_error;
  233. }
  234. // Parse out the extensions.
  235. bool have_key_share = false, have_pre_shared_key = false,
  236. have_supported_versions = false;
  237. CBS key_share, pre_shared_key, supported_versions;
  238. SSL_EXTENSION_TYPE ext_types[] = {
  239. {TLSEXT_TYPE_key_share, &have_key_share, &key_share},
  240. {TLSEXT_TYPE_pre_shared_key, &have_pre_shared_key, &pre_shared_key},
  241. {TLSEXT_TYPE_supported_versions, &have_supported_versions,
  242. &supported_versions},
  243. };
  244. uint8_t alert = SSL_AD_DECODE_ERROR;
  245. if (!ssl_parse_extensions(&extensions, &alert, ext_types,
  246. OPENSSL_ARRAY_SIZE(ext_types),
  247. 0 /* reject unknown */)) {
  248. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  249. return ssl_hs_error;
  250. }
  251. alert = SSL_AD_DECODE_ERROR;
  252. if (have_pre_shared_key) {
  253. if (ssl->session == NULL) {
  254. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
  255. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
  256. return ssl_hs_error;
  257. }
  258. if (!ssl_ext_pre_shared_key_parse_serverhello(hs, &alert,
  259. &pre_shared_key)) {
  260. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  261. return ssl_hs_error;
  262. }
  263. if (ssl->session->ssl_version != ssl->version) {
  264. OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
  265. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  266. return ssl_hs_error;
  267. }
  268. if (ssl->session->cipher->algorithm_prf != cipher->algorithm_prf) {
  269. OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_PRF_HASH_MISMATCH);
  270. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  271. return ssl_hs_error;
  272. }
  273. if (!ssl_session_is_context_valid(ssl, ssl->session)) {
  274. // This is actually a client application bug.
  275. OPENSSL_PUT_ERROR(SSL,
  276. SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  277. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  278. return ssl_hs_error;
  279. }
  280. ssl->s3->session_reused = true;
  281. // Only authentication information carries over in TLS 1.3.
  282. hs->new_session = SSL_SESSION_dup(ssl->session, SSL_SESSION_DUP_AUTH_ONLY);
  283. if (!hs->new_session) {
  284. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  285. return ssl_hs_error;
  286. }
  287. ssl_set_session(ssl, NULL);
  288. // Resumption incorporates fresh key material, so refresh the timeout.
  289. ssl_session_renew_timeout(ssl, hs->new_session.get(),
  290. ssl->session_ctx->session_psk_dhe_timeout);
  291. } else if (!ssl_get_new_session(hs, 0)) {
  292. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  293. return ssl_hs_error;
  294. }
  295. hs->new_session->cipher = cipher;
  296. hs->new_cipher = cipher;
  297. size_t hash_len =
  298. EVP_MD_size(ssl_get_handshake_digest(ssl_protocol_version(ssl), cipher));
  299. // Set up the key schedule and incorporate the PSK into the running secret.
  300. if (ssl->s3->session_reused) {
  301. if (!tls13_init_key_schedule(hs, hs->new_session->master_key,
  302. hs->new_session->master_key_length)) {
  303. return ssl_hs_error;
  304. }
  305. } else if (!tls13_init_key_schedule(hs, kZeroes, hash_len)) {
  306. return ssl_hs_error;
  307. }
  308. if (!have_key_share) {
  309. // We do not support psk_ke and thus always require a key share.
  310. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE);
  311. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION);
  312. return ssl_hs_error;
  313. }
  314. // Resolve ECDHE and incorporate it into the secret.
  315. Array<uint8_t> dhe_secret;
  316. alert = SSL_AD_DECODE_ERROR;
  317. if (!ssl_ext_key_share_parse_serverhello(hs, &dhe_secret, &alert,
  318. &key_share)) {
  319. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  320. return ssl_hs_error;
  321. }
  322. if (!tls13_advance_key_schedule(hs, dhe_secret.data(), dhe_secret.size()) ||
  323. !ssl_hash_message(hs, msg) ||
  324. !tls13_derive_handshake_secrets(hs) ||
  325. !tls13_set_traffic_key(ssl, evp_aead_open, hs->server_handshake_secret,
  326. hs->hash_len)) {
  327. return ssl_hs_error;
  328. }
  329. if (!hs->early_data_offered) {
  330. // If not sending early data, set client traffic keys now so that alerts are
  331. // encrypted.
  332. if (!tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_handshake_secret,
  333. hs->hash_len)) {
  334. return ssl_hs_error;
  335. }
  336. }
  337. ssl->method->next_message(ssl);
  338. hs->tls13_state = state_read_encrypted_extensions;
  339. return ssl_hs_ok;
  340. }
  341. static enum ssl_hs_wait_t do_read_encrypted_extensions(SSL_HANDSHAKE *hs) {
  342. SSL *const ssl = hs->ssl;
  343. SSLMessage msg;
  344. if (!ssl->method->get_message(ssl, &msg)) {
  345. return ssl_hs_read_message;
  346. }
  347. if (!ssl_check_message_type(ssl, msg, SSL3_MT_ENCRYPTED_EXTENSIONS)) {
  348. return ssl_hs_error;
  349. }
  350. CBS body = msg.body;
  351. if (!ssl_parse_serverhello_tlsext(hs, &body)) {
  352. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  353. return ssl_hs_error;
  354. }
  355. if (CBS_len(&body) != 0) {
  356. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  357. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  358. return ssl_hs_error;
  359. }
  360. // Store the negotiated ALPN in the session.
  361. if (!ssl->s3->alpn_selected.empty()) {
  362. hs->new_session->early_alpn = (uint8_t *)BUF_memdup(
  363. ssl->s3->alpn_selected.data(), ssl->s3->alpn_selected.size());
  364. if (hs->new_session->early_alpn == NULL) {
  365. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  366. return ssl_hs_error;
  367. }
  368. hs->new_session->early_alpn_len = ssl->s3->alpn_selected.size();
  369. }
  370. if (ssl->s3->early_data_accepted) {
  371. if (hs->early_session->cipher != hs->new_session->cipher ||
  372. MakeConstSpan(hs->early_session->early_alpn,
  373. hs->early_session->early_alpn_len) !=
  374. ssl->s3->alpn_selected) {
  375. OPENSSL_PUT_ERROR(SSL, SSL_R_ALPN_MISMATCH_ON_EARLY_DATA);
  376. return ssl_hs_error;
  377. }
  378. if (ssl->s3->tlsext_channel_id_valid || hs->received_custom_extension ||
  379. ssl->token_binding_negotiated) {
  380. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION_ON_EARLY_DATA);
  381. return ssl_hs_error;
  382. }
  383. }
  384. if (!ssl_hash_message(hs, msg)) {
  385. return ssl_hs_error;
  386. }
  387. ssl->method->next_message(ssl);
  388. hs->tls13_state = state_read_certificate_request;
  389. if (hs->in_early_data && !ssl->s3->early_data_accepted) {
  390. return ssl_hs_early_data_rejected;
  391. }
  392. return ssl_hs_ok;
  393. }
  394. static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) {
  395. SSL *const ssl = hs->ssl;
  396. // CertificateRequest may only be sent in non-resumption handshakes.
  397. if (ssl->s3->session_reused) {
  398. hs->tls13_state = state_read_server_finished;
  399. return ssl_hs_ok;
  400. }
  401. SSLMessage msg;
  402. if (!ssl->method->get_message(ssl, &msg)) {
  403. return ssl_hs_read_message;
  404. }
  405. // CertificateRequest is optional.
  406. if (msg.type != SSL3_MT_CERTIFICATE_REQUEST) {
  407. hs->tls13_state = state_read_server_certificate;
  408. return ssl_hs_ok;
  409. }
  410. bool have_sigalgs = false, have_ca = false;
  411. CBS sigalgs, ca;
  412. const SSL_EXTENSION_TYPE ext_types[] = {
  413. {TLSEXT_TYPE_signature_algorithms, &have_sigalgs, &sigalgs},
  414. {TLSEXT_TYPE_certificate_authorities, &have_ca, &ca},
  415. };
  416. CBS body = msg.body, context, extensions, supported_signature_algorithms;
  417. uint8_t alert = SSL_AD_DECODE_ERROR;
  418. if (!CBS_get_u8_length_prefixed(&body, &context) ||
  419. // The request context is always empty during the handshake.
  420. CBS_len(&context) != 0 ||
  421. !CBS_get_u16_length_prefixed(&body, &extensions) ||
  422. CBS_len(&body) != 0 ||
  423. !ssl_parse_extensions(&extensions, &alert, ext_types,
  424. OPENSSL_ARRAY_SIZE(ext_types),
  425. 1 /* accept unknown */) ||
  426. (have_ca && CBS_len(&ca) == 0) ||
  427. !have_sigalgs ||
  428. !CBS_get_u16_length_prefixed(&sigalgs,
  429. &supported_signature_algorithms) ||
  430. CBS_len(&supported_signature_algorithms) == 0 ||
  431. !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
  432. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  433. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  434. return ssl_hs_error;
  435. }
  436. if (have_ca) {
  437. hs->ca_names = ssl_parse_client_CA_list(ssl, &alert, &ca);
  438. if (!hs->ca_names) {
  439. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  440. return ssl_hs_error;
  441. }
  442. } else {
  443. hs->ca_names.reset(sk_CRYPTO_BUFFER_new_null());
  444. if (!hs->ca_names) {
  445. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  446. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  447. return ssl_hs_error;
  448. }
  449. }
  450. hs->cert_request = true;
  451. ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
  452. if (!ssl_hash_message(hs, msg)) {
  453. return ssl_hs_error;
  454. }
  455. ssl->method->next_message(ssl);
  456. hs->tls13_state = state_read_server_certificate;
  457. return ssl_hs_ok;
  458. }
  459. static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) {
  460. SSL *const ssl = hs->ssl;
  461. SSLMessage msg;
  462. if (!ssl->method->get_message(ssl, &msg)) {
  463. return ssl_hs_read_message;
  464. }
  465. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
  466. !tls13_process_certificate(hs, msg, 0 /* certificate required */) ||
  467. !ssl_hash_message(hs, msg)) {
  468. return ssl_hs_error;
  469. }
  470. ssl->method->next_message(ssl);
  471. hs->tls13_state = state_read_server_certificate_verify;
  472. return ssl_hs_ok;
  473. }
  474. static enum ssl_hs_wait_t do_read_server_certificate_verify(
  475. SSL_HANDSHAKE *hs) {
  476. SSL *const ssl = hs->ssl;
  477. SSLMessage msg;
  478. if (!ssl->method->get_message(ssl, &msg)) {
  479. return ssl_hs_read_message;
  480. }
  481. switch (ssl_verify_peer_cert(hs)) {
  482. case ssl_verify_ok:
  483. break;
  484. case ssl_verify_invalid:
  485. return ssl_hs_error;
  486. case ssl_verify_retry:
  487. hs->tls13_state = state_read_server_certificate_verify;
  488. return ssl_hs_certificate_verify;
  489. }
  490. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY) ||
  491. !tls13_process_certificate_verify(hs, msg) ||
  492. !ssl_hash_message(hs, msg)) {
  493. return ssl_hs_error;
  494. }
  495. ssl->method->next_message(ssl);
  496. hs->tls13_state = state_read_server_finished;
  497. return ssl_hs_ok;
  498. }
  499. static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) {
  500. SSL *const ssl = hs->ssl;
  501. SSLMessage msg;
  502. if (!ssl->method->get_message(ssl, &msg)) {
  503. return ssl_hs_read_message;
  504. }
  505. if (!ssl_check_message_type(ssl, msg, SSL3_MT_FINISHED) ||
  506. !tls13_process_finished(hs, msg, 0 /* don't use saved value */) ||
  507. !ssl_hash_message(hs, msg) ||
  508. // Update the secret to the master secret and derive traffic keys.
  509. !tls13_advance_key_schedule(hs, kZeroes, hs->hash_len) ||
  510. !tls13_derive_application_secrets(hs)) {
  511. return ssl_hs_error;
  512. }
  513. ssl->method->next_message(ssl);
  514. hs->tls13_state = state_send_end_of_early_data;
  515. return ssl_hs_ok;
  516. }
  517. static enum ssl_hs_wait_t do_send_end_of_early_data(SSL_HANDSHAKE *hs) {
  518. SSL *const ssl = hs->ssl;
  519. if (ssl->s3->early_data_accepted) {
  520. hs->can_early_write = false;
  521. ScopedCBB cbb;
  522. CBB body;
  523. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  524. SSL3_MT_END_OF_EARLY_DATA) ||
  525. !ssl_add_message_cbb(ssl, cbb.get())) {
  526. return ssl_hs_error;
  527. }
  528. }
  529. if (hs->early_data_offered) {
  530. if (!tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_handshake_secret,
  531. hs->hash_len)) {
  532. return ssl_hs_error;
  533. }
  534. }
  535. hs->tls13_state = state_send_client_certificate;
  536. return ssl_hs_ok;
  537. }
  538. static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) {
  539. SSL *const ssl = hs->ssl;
  540. // The peer didn't request a certificate.
  541. if (!hs->cert_request) {
  542. hs->tls13_state = state_complete_second_flight;
  543. return ssl_hs_ok;
  544. }
  545. // Call cert_cb to update the certificate.
  546. if (ssl->cert->cert_cb != NULL) {
  547. int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
  548. if (rv == 0) {
  549. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  550. OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
  551. return ssl_hs_error;
  552. }
  553. if (rv < 0) {
  554. hs->tls13_state = state_send_client_certificate;
  555. return ssl_hs_x509_lookup;
  556. }
  557. }
  558. if (!ssl_on_certificate_selected(hs) ||
  559. !tls13_add_certificate(hs)) {
  560. return ssl_hs_error;
  561. }
  562. hs->tls13_state = state_send_client_certificate_verify;
  563. return ssl_hs_ok;
  564. }
  565. static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) {
  566. SSL *const ssl = hs->ssl;
  567. // Don't send CertificateVerify if there is no certificate.
  568. if (!ssl_has_certificate(ssl)) {
  569. hs->tls13_state = state_complete_second_flight;
  570. return ssl_hs_ok;
  571. }
  572. switch (tls13_add_certificate_verify(hs)) {
  573. case ssl_private_key_success:
  574. hs->tls13_state = state_complete_second_flight;
  575. return ssl_hs_ok;
  576. case ssl_private_key_retry:
  577. hs->tls13_state = state_send_client_certificate_verify;
  578. return ssl_hs_private_key_operation;
  579. case ssl_private_key_failure:
  580. return ssl_hs_error;
  581. }
  582. assert(0);
  583. return ssl_hs_error;
  584. }
  585. static enum ssl_hs_wait_t do_complete_second_flight(SSL_HANDSHAKE *hs) {
  586. SSL *const ssl = hs->ssl;
  587. // Send a Channel ID assertion if necessary.
  588. if (ssl->s3->tlsext_channel_id_valid) {
  589. if (!ssl_do_channel_id_callback(ssl)) {
  590. hs->tls13_state = state_complete_second_flight;
  591. return ssl_hs_error;
  592. }
  593. if (ssl->tlsext_channel_id_private == NULL) {
  594. return ssl_hs_channel_id_lookup;
  595. }
  596. ScopedCBB cbb;
  597. CBB body;
  598. if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) ||
  599. !tls1_write_channel_id(hs, &body) ||
  600. !ssl_add_message_cbb(ssl, cbb.get())) {
  601. return ssl_hs_error;
  602. }
  603. }
  604. // Send a Finished message.
  605. if (!tls13_add_finished(hs)) {
  606. return ssl_hs_error;
  607. }
  608. // Derive the final keys and enable them.
  609. if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->server_traffic_secret_0,
  610. hs->hash_len) ||
  611. !tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_traffic_secret_0,
  612. hs->hash_len) ||
  613. !tls13_derive_resumption_secret(hs)) {
  614. return ssl_hs_error;
  615. }
  616. hs->tls13_state = state_done;
  617. return ssl_hs_flush;
  618. }
  619. enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs) {
  620. while (hs->tls13_state != state_done) {
  621. enum ssl_hs_wait_t ret = ssl_hs_error;
  622. enum client_hs_state_t state =
  623. static_cast<enum client_hs_state_t>(hs->tls13_state);
  624. switch (state) {
  625. case state_read_hello_retry_request:
  626. ret = do_read_hello_retry_request(hs);
  627. break;
  628. case state_send_second_client_hello:
  629. ret = do_send_second_client_hello(hs);
  630. break;
  631. case state_read_server_hello:
  632. ret = do_read_server_hello(hs);
  633. break;
  634. case state_read_encrypted_extensions:
  635. ret = do_read_encrypted_extensions(hs);
  636. break;
  637. case state_read_certificate_request:
  638. ret = do_read_certificate_request(hs);
  639. break;
  640. case state_read_server_certificate:
  641. ret = do_read_server_certificate(hs);
  642. break;
  643. case state_read_server_certificate_verify:
  644. ret = do_read_server_certificate_verify(hs);
  645. break;
  646. case state_read_server_finished:
  647. ret = do_read_server_finished(hs);
  648. break;
  649. case state_send_end_of_early_data:
  650. ret = do_send_end_of_early_data(hs);
  651. break;
  652. case state_send_client_certificate:
  653. ret = do_send_client_certificate(hs);
  654. break;
  655. case state_send_client_certificate_verify:
  656. ret = do_send_client_certificate_verify(hs);
  657. break;
  658. case state_complete_second_flight:
  659. ret = do_complete_second_flight(hs);
  660. break;
  661. case state_done:
  662. ret = ssl_hs_ok;
  663. break;
  664. }
  665. if (hs->tls13_state != state) {
  666. ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1);
  667. }
  668. if (ret != ssl_hs_ok) {
  669. return ret;
  670. }
  671. }
  672. return ssl_hs_ok;
  673. }
  674. const char *tls13_client_handshake_state(SSL_HANDSHAKE *hs) {
  675. enum client_hs_state_t state =
  676. static_cast<enum client_hs_state_t>(hs->tls13_state);
  677. switch (state) {
  678. case state_read_hello_retry_request:
  679. return "TLS 1.3 client read_hello_retry_request";
  680. case state_send_second_client_hello:
  681. return "TLS 1.3 client send_second_client_hello";
  682. case state_read_server_hello:
  683. return "TLS 1.3 client read_server_hello";
  684. case state_read_encrypted_extensions:
  685. return "TLS 1.3 client read_encrypted_extensions";
  686. case state_read_certificate_request:
  687. return "TLS 1.3 client read_certificate_request";
  688. case state_read_server_certificate:
  689. return "TLS 1.3 client read_server_certificate";
  690. case state_read_server_certificate_verify:
  691. return "TLS 1.3 client read_server_certificate_verify";
  692. case state_read_server_finished:
  693. return "TLS 1.3 client read_server_finished";
  694. case state_send_end_of_early_data:
  695. return "TLS 1.3 client send_end_of_early_data";
  696. case state_send_client_certificate:
  697. return "TLS 1.3 client send_client_certificate";
  698. case state_send_client_certificate_verify:
  699. return "TLS 1.3 client send_client_certificate_verify";
  700. case state_complete_second_flight:
  701. return "TLS 1.3 client complete_second_flight";
  702. case state_done:
  703. return "TLS 1.3 client done";
  704. }
  705. return "TLS 1.3 client unknown";
  706. }
  707. int tls13_process_new_session_ticket(SSL *ssl, const SSLMessage &msg) {
  708. if (ssl->s3->write_shutdown != ssl_shutdown_none) {
  709. // Ignore tickets on shutdown. Callers tend to indiscriminately call
  710. // |SSL_shutdown| before destroying an |SSL|, at which point calling the new
  711. // session callback may be confusing.
  712. return 1;
  713. }
  714. UniquePtr<SSL_SESSION> session = SSL_SESSION_dup(
  715. ssl->s3->established_session.get(), SSL_SESSION_INCLUDE_NONAUTH);
  716. if (!session) {
  717. return 0;
  718. }
  719. ssl_session_rebase_time(ssl, session.get());
  720. uint32_t server_timeout;
  721. CBS body = msg.body, ticket_nonce, ticket, extensions;
  722. if (!CBS_get_u32(&body, &server_timeout) ||
  723. !CBS_get_u32(&body, &session->ticket_age_add) ||
  724. !CBS_get_u8_length_prefixed(&body, &ticket_nonce) ||
  725. !CBS_get_u16_length_prefixed(&body, &ticket) ||
  726. !CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen) ||
  727. !CBS_get_u16_length_prefixed(&body, &extensions) ||
  728. CBS_len(&body) != 0) {
  729. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  730. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  731. return 0;
  732. }
  733. // Cap the renewable lifetime by the server advertised value. This avoids
  734. // wasting bandwidth on 0-RTT when we know the server will reject it.
  735. if (session->timeout > server_timeout) {
  736. session->timeout = server_timeout;
  737. }
  738. if (!tls13_derive_session_psk(session.get(), ticket_nonce)) {
  739. return 0;
  740. }
  741. // Parse out the extensions.
  742. bool have_early_data_info = false;
  743. CBS early_data_info;
  744. const SSL_EXTENSION_TYPE ext_types[] = {
  745. {TLSEXT_TYPE_early_data, &have_early_data_info, &early_data_info},
  746. };
  747. uint8_t alert = SSL_AD_DECODE_ERROR;
  748. if (!ssl_parse_extensions(&extensions, &alert, ext_types,
  749. OPENSSL_ARRAY_SIZE(ext_types),
  750. 1 /* ignore unknown */)) {
  751. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  752. return 0;
  753. }
  754. if (have_early_data_info && ssl->cert->enable_early_data) {
  755. if (!CBS_get_u32(&early_data_info, &session->ticket_max_early_data) ||
  756. CBS_len(&early_data_info) != 0) {
  757. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  758. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  759. return 0;
  760. }
  761. }
  762. session->ticket_age_add_valid = 1;
  763. session->not_resumable = 0;
  764. if ((ssl->ctx->session_cache_mode & SSL_SESS_CACHE_CLIENT) &&
  765. ssl->ctx->new_session_cb != NULL &&
  766. ssl->ctx->new_session_cb(ssl, session.get())) {
  767. // |new_session_cb|'s return value signals that it took ownership.
  768. session.release();
  769. }
  770. return 1;
  771. }
  772. } // namespace bssl