Botan 3.11.1
Crypto and TLS for C&
tls_client_impl_13.cpp
Go to the documentation of this file.
1/*
2* TLS Client - implementation for TLS 1.3
3* (C) 2022 Jack Lloyd
4* 2021 Elektrobit Automotive GmbH
5* 2022 Hannes Rantzsch, René Meusel - neXenio GmbH
6*
7* Botan is released under the Simplified BSD License (see license.txt)
8*/
9
10#include <botan/internal/tls_client_impl_13.h>
11
12#include <botan/credentials_manager.h>
13#include <botan/tls_callbacks.h>
14#include <botan/tls_extensions_13.h>
15#include <botan/tls_messages_13.h>
16#include <botan/tls_policy.h>
17#include <botan/x509cert.h>
18#include <botan/internal/stl_util.h>
19#include <botan/internal/tls_channel_impl_13.h>
20#include <botan/internal/tls_cipher_state.h>
21
22#include <utility>
23
24namespace Botan::TLS {
25
26Client_Impl_13::Client_Impl_13(const std::shared_ptr<Callbacks>& callbacks,
27 const std::shared_ptr<Session_Manager>& session_manager,
28 const std::shared_ptr<Credentials_Manager>& creds,
29 const std::shared_ptr<const Policy>& policy,
30 const std::shared_ptr<RandomNumberGenerator>& rng,
32 const std::vector<std::string>& next_protocols) :
33 Channel_Impl_13(callbacks, session_manager, creds, rng, policy, false /* is_server */),
34 m_info(std::move(info)),
35 m_should_send_ccs(false) {
36#if defined(BOTAN_HAS_TLS_12)
37 if(policy->allow_tls12()) {
38 expect_downgrade(m_info, next_protocols);
39 }
40#endif
41
42 if(auto session = find_session_for_resumption()) {
43 if(!session->session.version().is_pre_tls_13()) {
44 m_resumed_session = std::move(session);
45 } else if(expects_downgrade()) {
46 // If we found a session that was created with TLS 1.2, we downgrade
47 // the implementation right away, before even issuing a Client Hello.
48 request_downgrade_for_resumption(std::move(session.value()));
49 return;
50 }
51 }
52
53 auto msg = send_handshake_message(m_handshake_state.sending(
55 *callbacks,
56 *rng,
57 m_info.hostname(),
58 next_protocols,
59 m_resumed_session,
60 creds->find_preshared_keys(m_info.hostname(), Connection_Side::Client))));
61
62 if(expects_downgrade()) {
64 }
65
66 // RFC 8446 Appendix D.4
67 // If not offering early data, the client sends a dummy change_cipher_spec
68 // record [...] immediately before its second flight. This may either be before
69 // its second ClientHello or before its encrypted handshake flight.
70 //
71 // TODO: don't schedule ccs here when early data is used
72 if(policy->tls_13_middlebox_compatibility_mode()) {
73 m_should_send_ccs = true;
74 }
75
76 m_transitions.set_expected_next({Handshake_Type::ServerHello, Handshake_Type::HelloRetryRequest});
77}
78
79void Client_Impl_13::process_handshake_msg(Handshake_Message_13 message) {
80 std::visit(
81 [&](auto msg) {
82 // first verify that the message was expected by the state machine...
83 m_transitions.confirm_transition_to(msg.get().type());
84
85 // ... then allow the library user to abort on their discretion
87
88 // ... finally handle the message
89 handle(msg.get());
90 },
91 m_handshake_state.received(std::move(message)));
92}
93
94void Client_Impl_13::process_post_handshake_msg(Post_Handshake_Message_13 message) {
96
97 std::visit([&](auto msg) { handle(msg); }, m_handshake_state.received(std::move(message)));
98}
99
101 // RFC 8446 5.
102 // If an implementation detects a change_cipher_spec record received before
103 // the first ClientHello message or after the peer's Finished message, it MUST be
104 // treated as an unexpected record type [("unexpected_message" alert)].
105 if(!m_handshake_state.has_client_hello() || m_handshake_state.has_server_finished()) {
106 throw TLS_Exception(Alert::UnexpectedMessage, "Received an unexpected dummy Change Cipher Spec");
107 }
108
109 // RFC 8446 5.
110 // An implementation may receive an unencrypted record of type change_cipher_spec [...]
111 // at any time after the first ClientHello message has been sent or received
112 // and before the peer's Finished message has been received [...]
113 // and MUST simply drop it without further processing.
114 //
115 // ... no further processing.
116}
117
119 return m_handshake_state.handshake_finished();
120}
121
122std::optional<Session_with_Handle> Client_Impl_13::find_session_for_resumption() {
123 // RFC 8446 4.6.1
124 // Clients MUST only resume if the new SNI value is valid for the
125 // server certificate presented in the original session and SHOULD only
126 // resume if the SNI value matches the one used in the original session.
127 //
128 // Below we search sessions based on their SNI value. Assuming that a
129 // 3rd party session manager does not lie to the implementation, we don't
130 // explicitly re-check that the SNI values match.
131 //
132 // Also, the default implementation did verify that very same SNI information
133 // against the server's certificate (via Callbacks::tls_verify_cert_chain())
134 // before storing it in the session.
135 //
136 // We therefore assume that the session returned by the `Session_Manager` is
137 // suitable for resumption in this context.
138 auto sessions = session_manager().find(m_info, callbacks(), policy());
139 if(sessions.empty()) {
140 return std::nullopt;
141 }
142
143 // TODO: TLS 1.3 allows sending more than one ticket (for resumption) in a
144 // Client Hello. Currently, we do not support that. The Session_Manager
145 // does imply otherwise with its API, though.
146 auto& session_to_resume = sessions.front();
147
148 return std::move(session_to_resume);
149}
150
151void Client_Impl_13::handle(const Server_Hello_12_Shim& server_hello_msg) {
152 if(m_handshake_state.has_hello_retry_request()) {
153 throw TLS_Exception(Alert::UnexpectedMessage, "Version downgrade received after Hello Retry");
154 }
155
156 // RFC 8446 Appendix D.1
157 // If the version chosen by the server is not supported by the client
158 // (or is not acceptable), the client MUST abort the handshake with a
159 // "protocol_version" alert.
160 if(!expects_downgrade()) {
161 throw TLS_Exception(Alert::ProtocolVersion, "Received an unexpected legacy Server Hello");
162 }
163
164 // RFC 8446 4.1.3
165 // TLS 1.3 has a downgrade protection mechanism embedded in the server's
166 // random value. TLS 1.3 servers which negotiate TLS 1.2 or below in
167 // response to a ClientHello MUST set the last 8 bytes of their Random
168 // value specially in their ServerHello.
169 //
170 // TLS 1.3 clients receiving a ServerHello indicating TLS 1.2 or below
171 // MUST check that the [downgrade indication is not set]. [...] If a match
172 // is found, the client MUST abort the handshake with an
173 // "illegal_parameter" alert.
174 if(server_hello_msg.random_signals_downgrade().has_value()) {
175 throw TLS_Exception(Alert::IllegalParameter, "Downgrade attack detected");
176 }
177
178 // RFC 8446 4.2.1
179 // A server which negotiates a version of TLS prior to TLS 1.3 [...]
180 // MUST NOT send the "supported_versions" extension.
181 //
182 // Note that this condition should never happen, as the Server_Hello parsing
183 // code decides to create a Server_Hello_12 based on the absence of this extension.
184 if(server_hello_msg.extensions().has<Supported_Versions>()) {
185 throw TLS_Exception(Alert::IllegalParameter, "Unexpected extension received");
186 }
187
188 // RFC 8446 Appendix D.1
189 // If the version chosen by the server is not supported by the client
190 // (or is not acceptable), the client MUST abort the handshake with a
191 // "protocol_version" alert.
192 const auto& client_hello_exts = m_handshake_state.client_hello().extensions();
193 BOTAN_ASSERT_NOMSG(client_hello_exts.has<Supported_Versions>());
194 if(!client_hello_exts.get<Supported_Versions>()->supports(server_hello_msg.selected_version())) {
195 throw TLS_Exception(Alert::ProtocolVersion, "Protocol version was not offered");
196 }
197
198 if(policy().tls_13_middlebox_compatibility_mode() &&
199 m_handshake_state.client_hello().session_id() == server_hello_msg.session_id()) {
200 // In compatibility mode, the server will reflect the session ID we sent in the client hello.
201 // However, a TLS 1.2 server that wants to downgrade cannot have found the random session ID
202 // we sent. Therefore, we have to consider this as an attack.
203 // (Thanks BoGo test EchoTLS13CompatibilitySessionID!)
204 throw TLS_Exception(Alert::IllegalParameter, "Unexpected session ID during downgrade");
205 }
206
208
209 // After this, no further messages are expected here because this instance will be replaced
210 // by a Client_Impl_12.
211 m_transitions.set_expected_next({});
212}
213
214namespace {
215// validate Server_Hello_13 and Hello_Retry_Request
216void validate_server_hello_ish(const Client_Hello_13& ch, const Server_Hello_13& sh) {
217 // RFC 8446 4.1.3
218 // A client which receives a legacy_session_id_echo field that does not match what
219 // it sent in the ClientHello MUST abort the handshake with an "illegal_parameter" alert.
220 if(ch.session_id() != sh.session_id()) {
221 throw TLS_Exception(Alert::IllegalParameter, "echoed session id did not match");
222 }
223
224 // RFC 8446 4.1.3
225 // A client which receives a cipher suite that was not offered MUST abort the handshake
226 // with an "illegal_parameter" alert.
227 if(!ch.offered_suite(sh.ciphersuite())) {
228 throw TLS_Exception(Alert::IllegalParameter, "Server replied with ciphersuite we didn't send");
229 }
230
231 // RFC 8446 4.2.1
232 // If the "supported_versions" extension in the ServerHello contains a
233 // version not offered by the client or contains a version prior to
234 // TLS 1.3, the client MUST abort the handshake with an "illegal_parameter" alert.
235 //
236 // Note: Server_Hello_13 parsing checks that its selected version is TLS 1.3
237 BOTAN_ASSERT_NOMSG(ch.extensions().has<Supported_Versions>());
238 if(!ch.extensions().get<Supported_Versions>()->supports(sh.selected_version())) {
239 throw TLS_Exception(Alert::IllegalParameter, "Protocol version was not offered");
240 }
241}
242} // namespace
243
244void Client_Impl_13::handle(const Server_Hello_13& sh) {
245 // Note: Basic checks (that do not require contextual information) were already
246 // performed during the construction of the Server_Hello_13 object.
247
248 const auto& ch = m_handshake_state.client_hello();
249
250 validate_server_hello_ish(ch, sh);
251
252 // RFC 8446 4.2
253 // Implementations MUST NOT send extension responses if the remote
254 // endpoint did not send the corresponding extension requests, [...]. Upon
255 // receiving such an extension, an endpoint MUST abort the handshake
256 // with an "unsupported_extension" alert.
257 if(sh.extensions().contains_other_than(ch.extensions().extension_types())) {
258 throw TLS_Exception(Alert::UnsupportedExtension, "Unsupported extension found in Server Hello");
259 }
260
261 if(m_handshake_state.has_hello_retry_request()) {
262 const auto& hrr = m_handshake_state.hello_retry_request();
263
264 // RFC 8446 4.1.4
265 // Upon receiving the ServerHello, clients MUST check that the cipher suite
266 // supplied in the ServerHello is the same as that in the HelloRetryRequest
267 // and otherwise abort the handshake with an "illegal_parameter" alert.
268 if(hrr.ciphersuite() != sh.ciphersuite()) {
269 throw TLS_Exception(Alert::IllegalParameter, "server changed its chosen ciphersuite");
270 }
271
272 // RFC 8446 4.1.4
273 // The value of selected_version in the HelloRetryRequest "supported_versions"
274 // extension MUST be retained in the ServerHello, and a client MUST abort the
275 // handshake with an "illegal_parameter" alert if the value changes.
276 if(hrr.selected_version() != sh.selected_version()) {
277 throw TLS_Exception(Alert::IllegalParameter, "server changed its chosen protocol version");
278 }
279 }
280
281 auto cipher = Ciphersuite::by_id(sh.ciphersuite());
282 BOTAN_ASSERT_NOMSG(cipher.has_value()); // should work, since we offered this suite
283
284 // RFC 8446 Appendix B.4
285 // Although TLS 1.3 uses the same cipher suite space as previous versions
286 // of TLS [...] cipher suites for TLS 1.2 and lower cannot be used with
287 // TLS 1.3.
288 if(!cipher->usable_in_version(Protocol_Version::TLS_V13)) {
289 throw TLS_Exception(Alert::IllegalParameter,
290 "Server replied using a ciphersuite not allowed in version it offered");
291 }
292
293 // RFC 8446 4.2.11
294 // Clients MUST verify that [...] a server "key_share" extension is present
295 // if required by the ClientHello "psk_key_exchange_modes" extension. If
296 // these values are not consistent, the client MUST abort the handshake
297 // with an "illegal_parameter" alert.
298 //
299 // Currently, we don't support PSK-only mode, hence a key share extension is
300 // considered mandatory.
301 //
302 // TODO: Implement PSK-only mode.
303 if(!sh.extensions().has<Key_Share>()) {
304 throw TLS_Exception(Alert::IllegalParameter, "Server Hello did not contain a key share extension");
305 }
306
307 auto* my_keyshare = ch.extensions().get<Key_Share>();
308 auto shared_secret = my_keyshare->decapsulate(*sh.extensions().get<Key_Share>(), policy(), callbacks(), rng());
309
310 m_transcript_hash.set_algorithm(cipher.value().prf_algo());
311
312 if(sh.extensions().has<PSK>()) {
313 std::tie(m_psk_identity, m_cipher_state) =
314 ch.extensions().get<PSK>()->take_selected_psk_info(*sh.extensions().get<PSK>(), cipher.value());
315
316 // If we offered a session for resumption *and* an externally provided PSK
317 // and the latter was chosen by the server over the offered resumption, we
318 // want to invalidate the now-outdated session in m_resumed_session.
319 if(m_psk_identity.has_value() && m_resumed_session.has_value()) {
320 m_resumed_session.reset();
321 }
322
323 // TODO: When implementing early data, `advance_with_client_hello` must
324 // happen _before_ encrypting any early application data.
325 // Same when we want to support early key export.
326 m_cipher_state->advance_with_client_hello(m_transcript_hash.previous(), *this);
327 m_cipher_state->advance_with_server_hello(
328 cipher.value(), std::move(shared_secret), m_transcript_hash.current(), *this);
329 } else {
330 m_resumed_session.reset(); // might have been set if we attempted a resumption
332 m_side, std::move(shared_secret), cipher.value(), m_transcript_hash.current(), *this);
333 }
334
336
337 m_transitions.set_expected_next(Handshake_Type::EncryptedExtensions);
338}
339
340void Client_Impl_13::handle(const Hello_Retry_Request& hrr) {
341 // Note: Basic checks (that do not require contextual information) were already
342 // performed during the construction of the Hello_Retry_Request object as
343 // a subclass of Server_Hello_13.
344
345 auto& ch = m_handshake_state.client_hello();
346
347 validate_server_hello_ish(ch, hrr);
348
349 // RFC 8446 4.1.4.
350 // A HelloRetryRequest MUST NOT contain any
351 // extensions that were not first offered by the client in its
352 // ClientHello, with the exception of optionally the "cookie".
353 auto allowed_exts = ch.extensions().extension_types();
354 allowed_exts.insert(Extension_Code::Cookie);
355 if(hrr.extensions().contains_other_than(allowed_exts)) {
356 throw TLS_Exception(Alert::UnsupportedExtension, "Unsupported extension found in Hello Retry Request");
357 }
358
359 auto cipher = Ciphersuite::by_id(hrr.ciphersuite());
360 BOTAN_ASSERT_NOMSG(cipher.has_value()); // should work, since we offered this suite
361
362 // RFC 8446 4.1.4 / Appendix B.4
363 // Similarly, cipher suites for TLS 1.2 and lower cannot be used with
364 // TLS 1.3.
365 if(!cipher->usable_in_version(Protocol_Version::TLS_V13)) {
366 throw TLS_Exception(Alert::IllegalParameter, "HelloRetryRequest selected a cipher suite not usable in TLS 1.3");
367 }
368
371
372 ch.retry(hrr, m_transcript_hash, callbacks(), rng());
373
375
376 send_handshake_message(std::reference_wrapper(ch));
377
378 // RFC 8446 4.1.4
379 // If a client receives a second HelloRetryRequest in the same connection [...],
380 // it MUST abort the handshake with an "unexpected_message" alert.
381 m_transitions.set_expected_next(Handshake_Type::ServerHello);
382}
383
384void Client_Impl_13::handle(const Encrypted_Extensions& encrypted_extensions_msg) {
385 const auto& exts = encrypted_extensions_msg.extensions();
386
387 // RFC 8446 4.2
388 // Implementations MUST NOT send extension responses if the remote
389 // endpoint did not send the corresponding extension requests, [...]. Upon
390 // receiving such an extension, an endpoint MUST abort the handshake
391 // with an "unsupported_extension" alert.
392 const auto& requested_exts = m_handshake_state.client_hello().extensions().extension_types();
393 if(exts.contains_other_than(requested_exts)) {
394 throw TLS_Exception(Alert::UnsupportedExtension,
395 "Encrypted Extensions contained an extension that was not offered");
396 }
397
398 // Note: As per RFC 6066 3. we can check for an empty SNI extensions to
399 // determine if the server used the SNI we sent here.
400
401 if(exts.has<Record_Size_Limit>() && m_handshake_state.client_hello().extensions().has<Record_Size_Limit>()) {
402 // RFC 8449 4.
403 // The record size limit only applies to records sent toward the
404 // endpoint that advertises the limit. An endpoint can send records
405 // that are larger than the limit it advertises as its own limit.
406 //
407 // Hence, the "outgoing" limit is what the server requested and the
408 // "incoming" limit is what we requested in the Client Hello.
409 auto* const outgoing_limit = exts.get<Record_Size_Limit>();
410 auto* const incoming_limit = m_handshake_state.client_hello().extensions().get<Record_Size_Limit>();
411 set_record_size_limits(outgoing_limit->limit(), incoming_limit->limit());
412 }
413
414 if(exts.has<Server_Certificate_Type>() &&
415 m_handshake_state.client_hello().extensions().has<Server_Certificate_Type>()) {
416 const auto* server_cert_type = exts.get<Server_Certificate_Type>();
417 const auto* our_server_cert_types = m_handshake_state.client_hello().extensions().get<Server_Certificate_Type>();
418 our_server_cert_types->validate_selection(*server_cert_type);
419
420 // RFC 7250 4.2
421 // With the server_certificate_type extension in the server hello, the
422 // TLS server indicates the certificate type carried in the Certificate
423 // payload.
424 //
425 // Note: TLS 1.3 carries this extension in the Encrypted Extensions
426 // message instead of the Server Hello.
427 set_selected_certificate_type(server_cert_type->selected_certificate_type());
428 }
429
431
432 if(m_handshake_state.server_hello().extensions().has<PSK>()) {
433 // RFC 8446 2.2
434 // As the server is authenticating via a PSK, it does not send a
435 // Certificate or a CertificateVerify message.
436 m_transitions.set_expected_next(Handshake_Type::Finished);
437 } else {
438 m_transitions.set_expected_next({Handshake_Type::Certificate, Handshake_Type::CertificateRequest});
439 }
440}
441
442void Client_Impl_13::handle(const Certificate_Request_13& certificate_request_msg) {
443 // RFC 8446 4.3.2
444 // [The 'context' field] SHALL be zero length unless used for the
445 // post-handshake authentication exchanges described in Section 4.6.2.
446 if(!is_handshake_complete() && !certificate_request_msg.context().empty()) {
447 throw TLS_Exception(Alert::DecodeError, "Certificate_Request context must be empty in the main handshake");
448 }
449
451 certificate_request_msg.extensions(), Connection_Side::Server, Handshake_Type::CertificateRequest);
452 m_transitions.set_expected_next(Handshake_Type::Certificate);
453}
454
455void Client_Impl_13::handle(const Certificate_13& certificate_msg) {
456 // RFC 8446 4.4.2
457 // certificate_request_context: [...] In the case of server authentication,
458 // this field SHALL be zero length.
459 if(!certificate_msg.request_context().empty()) {
460 throw TLS_Exception(Alert::DecodeError, "Received a server certificate message with non-empty request context");
461 }
462
463 // RFC 8446 4.4.2
464 // Extensions in the Certificate message from the server MUST correspond
465 // to ones from the ClientHello message.
466 certificate_msg.validate_extensions(m_handshake_state.client_hello().extensions().extension_types(), callbacks());
467 certificate_msg.verify(callbacks(),
468 policy(),
470 m_info.hostname(),
471 m_handshake_state.client_hello().extensions().has<Certificate_Status_Request>());
472
473 m_transitions.set_expected_next(Handshake_Type::CertificateVerify);
474}
475
476void Client_Impl_13::handle(const Certificate_Verify_13& certificate_verify_msg) {
477 // RFC 8446 4.4.3
478 // If the CertificateVerify message is sent by a server, the signature
479 // algorithm MUST be one offered in the client's "signature_algorithms"
480 // extension unless no valid certificate chain can be produced without
481 // unsupported algorithms.
482 //
483 // Note: if the server failed to produce a certificate chain without using
484 // an unsupported signature scheme, we opt to abort the handshake.
485 const auto offered = m_handshake_state.client_hello().signature_schemes();
486 if(!value_exists(offered, certificate_verify_msg.signature_scheme())) {
487 throw TLS_Exception(Alert::IllegalParameter,
488 "We did not offer the usage of " + certificate_verify_msg.signature_scheme().to_string() +
489 " as a signature scheme");
490 }
491
492 const bool sig_valid = certificate_verify_msg.verify(
493 *m_handshake_state.server_certificate().public_key(), callbacks(), m_transcript_hash.previous());
494
495 if(!sig_valid) {
496 throw TLS_Exception(Alert::DecryptError, "Server certificate verification failed");
497 }
498
499 m_transitions.set_expected_next(Handshake_Type::Finished);
500}
501
502void Client_Impl_13::send_client_authentication(Channel_Impl_13::AggregatedHandshakeMessages& flight) {
503 BOTAN_ASSERT_NOMSG(m_handshake_state.has_certificate_request());
504 const auto& cert_request = m_handshake_state.certificate_request();
505
506 const auto cert_type = [&] {
507 const auto& exts = m_handshake_state.encrypted_extensions().extensions();
508 const auto& chexts = m_handshake_state.client_hello().extensions();
509 if(exts.has<Client_Certificate_Type>() && chexts.has<Client_Certificate_Type>()) {
510 const auto* client_cert_type = exts.get<Client_Certificate_Type>();
511 chexts.get<Client_Certificate_Type>()->validate_selection(*client_cert_type);
512
513 // RFC 7250 4.2
514 // This client_certificate_type extension in the server hello then
515 // indicates the type of certificates the client is requested to
516 // provide in a subsequent certificate payload.
517 //
518 // Note: TLS 1.3 carries this extension in the Encrypted Extensions
519 // message instead of the Server Hello.
520 return client_cert_type->selected_certificate_type();
521 } else {
522 // RFC 8446 4.4.2
523 // If the corresponding certificate type extension [...] was not
524 // negotiated in EncryptedExtensions, [...] then each
525 // CertificateEntry contains a DER-encoded X.509 certificate.
527 }
528 }();
529
530 // RFC 8446 4.4.2
531 // certificate_request_context: If this message is in response to a
532 // CertificateRequest, the value of certificate_request_context in
533 // that message.
534 flight.add(m_handshake_state.sending(
535 Certificate_13(cert_request, m_info.hostname(), credentials_manager(), callbacks(), cert_type)));
536
537 // RFC 8446 4.4.2
538 // If the server requests client authentication but no suitable certificate
539 // is available, the client MUST send a Certificate message containing no
540 // certificates.
541 //
542 // In that case, no Certificate Verify message will be sent.
543 if(!m_handshake_state.client_certificate().empty()) {
544 flight.add(m_handshake_state.sending(Certificate_Verify_13(m_handshake_state.client_certificate(),
545 cert_request.signature_schemes(),
546 m_info.hostname(),
547 m_transcript_hash.current(),
550 policy(),
551 callbacks(),
552 rng())));
553 }
554}
555
556void Client_Impl_13::handle(const Finished_13& finished_msg) {
557 // RFC 8446 4.4.4
558 // Recipients of Finished messages MUST verify that the contents are
559 // correct and if incorrect MUST terminate the connection with a
560 // "decrypt_error" alert.
561 if(!finished_msg.verify(m_cipher_state.get(), m_transcript_hash.previous())) {
562 throw TLS_Exception(Alert::DecryptError, "Finished message didn't verify");
563 }
564
565 m_handshake_state.confirm_peer_finished_verified();
566
567 // Give the application a chance for a final veto before fully
568 // establishing the connection.
569 callbacks().tls_session_established(Session_Summary(m_handshake_state.server_hello(),
574 m_resumed_session.has_value(),
575 m_info,
576 callbacks().tls_current_timestamp()));
577
578 // Derives the secrets for receiving application data but defers
579 // the derivation of sending application data.
580 m_cipher_state->advance_with_server_finished(m_transcript_hash.current(), *this);
581
582 auto flight = aggregate_handshake_messages();
583
584 // RFC 8446 4.4.2
585 // The client MUST send a Certificate message if and only if the server
586 // has requested client authentication via a CertificateRequest message.
587 if(m_handshake_state.has_certificate_request()) {
588 send_client_authentication(flight);
589 }
590
591 // send client finished handshake message (still using handshake traffic secrets)
592 flight.add(m_handshake_state.sending(Finished_13(m_cipher_state.get(), m_transcript_hash.current())));
593
594 flight.send();
595
596 // derives the sending application traffic secrets
597 m_cipher_state->advance_with_client_finished(m_transcript_hash.current());
598
599 // TODO: Create a dummy session object and invoke tls_session_established.
600 // Alternatively, consider changing the expectations described in the
601 // callback's doc string.
602
603 // no more handshake messages expected
604 m_transitions.set_expected_next({});
605
607}
608
609void TLS::Client_Impl_13::handle(const New_Session_Ticket_13& new_session_ticket) {
610 callbacks().tls_examine_extensions(
611 new_session_ticket.extensions(), Connection_Side::Server, Handshake_Type::NewSessionTicket);
612
613 const Session session(m_cipher_state->psk(new_session_ticket.nonce()),
614 new_session_ticket.early_data_byte_limit(),
615 new_session_ticket.ticket_age_add(),
616 new_session_ticket.lifetime_hint(),
617 m_handshake_state.server_hello().selected_version(),
618 m_handshake_state.server_hello().ciphersuite(),
620 peer_cert_chain(),
621 peer_raw_public_key(),
622 m_info,
623 callbacks().tls_current_timestamp());
624
625 if(callbacks().tls_should_persist_resumption_information(session)) {
626 session_manager().store(session, Session_Handle(new_session_ticket.handle()));
627 }
628}
629
630std::vector<X509_Certificate> Client_Impl_13::peer_cert_chain() const {
631 if(m_handshake_state.has_server_certificate_msg() &&
632 m_handshake_state.server_certificate().has_certificate_chain()) {
633 return m_handshake_state.server_certificate().cert_chain();
634 }
635
636 if(m_resumed_session.has_value()) {
637 return m_resumed_session->session.peer_certs();
638 }
639
640 return {};
641}
642
643std::shared_ptr<const Public_Key> Client_Impl_13::peer_raw_public_key() const {
644 if(m_handshake_state.has_server_certificate_msg() && m_handshake_state.server_certificate().is_raw_public_key()) {
645 return m_handshake_state.server_certificate().public_key();
646 }
647
648 if(m_resumed_session.has_value()) {
649 return m_resumed_session->session.peer_raw_public_key();
650 }
651
652 return nullptr;
653}
654
655std::optional<std::string> Client_Impl_13::external_psk_identity() const {
656 return m_psk_identity;
657}
658
660 return std::exchange(m_should_send_ccs, false); // test-and-set
661}
662
663void Client_Impl_13::maybe_log_secret(std::string_view label, std::span<const uint8_t> secret) const {
664 if(policy().allow_ssl_key_log_file()) {
665 callbacks().tls_ssl_key_log_data(label, m_handshake_state.client_hello().random(), secret);
666 }
667}
668
671 const auto& eee = m_handshake_state.encrypted_extensions().extensions();
673 return eee.get<Application_Layer_Protocol_Notification>()->single_protocol();
674 }
675 }
676
677 return "";
678}
679
680} // namespace Botan::TLS
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
virtual void tls_session_activated()
virtual void tls_examine_extensions(const Extensions &extn, Connection_Side which_side, Handshake_Type which_message)
virtual void tls_session_established(const Session_Summary &session)
virtual void tls_ssl_key_log_data(std::string_view label, std::span< const uint8_t > client_random, std::span< const uint8_t > secret) const
virtual void tls_inspect_handshake_msg(const Handshake_Message &message)
const Policy & policy() const
AggregatedHandshakeMessages aggregate_handshake_messages()
Credentials_Manager & credentials_manager()
RandomNumberGenerator & rng()
std::vector< uint8_t > send_handshake_message(const std::variant< MsgTs... > &message)
Transcript_Hash_State m_transcript_hash
Channel_Impl_13(const std::shared_ptr< Callbacks > &callbacks, const std::shared_ptr< Session_Manager > &session_manager, const std::shared_ptr< Credentials_Manager > &credentials_manager, const std::shared_ptr< RandomNumberGenerator > &rng, const std::shared_ptr< const Policy > &policy, bool is_server)
virtual void process_dummy_change_cipher_spec()=0
Session_Manager & session_manager()
std::unique_ptr< Cipher_State > m_cipher_state
void set_selected_certificate_type(Certificate_Type cert_type)
void set_record_size_limits(uint16_t outgoing_limit, uint16_t incoming_limit)
void request_downgrade_for_resumption(Session_with_Handle session)
void preserve_client_hello(std::span< const uint8_t > msg)
static std::unique_ptr< Cipher_State > init_with_server_hello(Connection_Side side, secure_vector< uint8_t > &&shared_secret, const Ciphersuite &cipher, const Transcript_Hash &transcript_hash, const Secret_Logger &channel)
static std::optional< Ciphersuite > by_id(uint16_t suite)
const std::vector< uint8_t > & random() const
Client_Impl_13(const std::shared_ptr< Callbacks > &callbacks, const std::shared_ptr< Session_Manager > &session_manager, const std::shared_ptr< Credentials_Manager > &creds, const std::shared_ptr< const Policy > &policy, const std::shared_ptr< RandomNumberGenerator > &rng, Server_Information server_info=Server_Information(), const std::vector< std::string > &next_protocols={})
std::optional< std::string > external_psk_identity() const override
std::string application_protocol() const override
std::shared_ptr< const Public_Key > peer_raw_public_key() const override
bool is_handshake_complete() const override
std::vector< X509_Certificate > peer_cert_chain() const override
decltype(auto) received(Handshake_Message_13 message)
void confirm_transition_to(Handshake_Type msg_type)
Helper class to embody a session handle in all protocol versions.
virtual std::vector< Session_with_Handle > find(const Server_Information &info, Callbacks &callbacks, const Policy &policy)
Find all sessions that match a given server info.
bool supports(Protocol_Version version) const
static Transcript_Hash_State recreate_after_hello_retry_request(std::string_view algo_spec, const Transcript_Hash_State &prev_transcript_hash_state)
std::variant< Client_Hello_13, Client_Hello_12_Shim, Server_Hello_13, Server_Hello_12_Shim, Hello_Retry_Request, Encrypted_Extensions, Certificate_13, Certificate_Request_13, Certificate_Verify_13, Finished_13 > Handshake_Message_13
std::variant< New_Session_Ticket_13, Key_Update > Post_Handshake_Message_13
bool value_exists(const std::vector< T > &vec, const V &val)
Definition stl_util.h:43