Botan 3.11.0
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
364
365 ch.retry(hrr, m_transcript_hash, callbacks(), rng());
366
368
369 send_handshake_message(std::reference_wrapper(ch));
370
371 // RFC 8446 4.1.4
372 // If a client receives a second HelloRetryRequest in the same connection [...],
373 // it MUST abort the handshake with an "unexpected_message" alert.
374 m_transitions.set_expected_next(Handshake_Type::ServerHello);
375}
376
377void Client_Impl_13::handle(const Encrypted_Extensions& encrypted_extensions_msg) {
378 const auto& exts = encrypted_extensions_msg.extensions();
379
380 // RFC 8446 4.2
381 // Implementations MUST NOT send extension responses if the remote
382 // endpoint did not send the corresponding extension requests, [...]. Upon
383 // receiving such an extension, an endpoint MUST abort the handshake
384 // with an "unsupported_extension" alert.
385 const auto& requested_exts = m_handshake_state.client_hello().extensions().extension_types();
386 if(exts.contains_other_than(requested_exts)) {
387 throw TLS_Exception(Alert::UnsupportedExtension,
388 "Encrypted Extensions contained an extension that was not offered");
389 }
390
391 // Note: As per RFC 6066 3. we can check for an empty SNI extensions to
392 // determine if the server used the SNI we sent here.
393
394 if(exts.has<Record_Size_Limit>() && m_handshake_state.client_hello().extensions().has<Record_Size_Limit>()) {
395 // RFC 8449 4.
396 // The record size limit only applies to records sent toward the
397 // endpoint that advertises the limit. An endpoint can send records
398 // that are larger than the limit it advertises as its own limit.
399 //
400 // Hence, the "outgoing" limit is what the server requested and the
401 // "incoming" limit is what we requested in the Client Hello.
402 auto* const outgoing_limit = exts.get<Record_Size_Limit>();
403 auto* const incoming_limit = m_handshake_state.client_hello().extensions().get<Record_Size_Limit>();
404 set_record_size_limits(outgoing_limit->limit(), incoming_limit->limit());
405 }
406
407 if(exts.has<Server_Certificate_Type>() &&
408 m_handshake_state.client_hello().extensions().has<Server_Certificate_Type>()) {
409 const auto* server_cert_type = exts.get<Server_Certificate_Type>();
410 const auto* our_server_cert_types = m_handshake_state.client_hello().extensions().get<Server_Certificate_Type>();
411 our_server_cert_types->validate_selection(*server_cert_type);
412
413 // RFC 7250 4.2
414 // With the server_certificate_type extension in the server hello, the
415 // TLS server indicates the certificate type carried in the Certificate
416 // payload.
417 //
418 // Note: TLS 1.3 carries this extension in the Encrypted Extensions
419 // message instead of the Server Hello.
420 set_selected_certificate_type(server_cert_type->selected_certificate_type());
421 }
422
424
425 if(m_handshake_state.server_hello().extensions().has<PSK>()) {
426 // RFC 8446 2.2
427 // As the server is authenticating via a PSK, it does not send a
428 // Certificate or a CertificateVerify message.
429 m_transitions.set_expected_next(Handshake_Type::Finished);
430 } else {
431 m_transitions.set_expected_next({Handshake_Type::Certificate, Handshake_Type::CertificateRequest});
432 }
433}
434
435void Client_Impl_13::handle(const Certificate_Request_13& certificate_request_msg) {
436 // RFC 8446 4.3.2
437 // [The 'context' field] SHALL be zero length unless used for the
438 // post-handshake authentication exchanges described in Section 4.6.2.
439 if(!is_handshake_complete() && !certificate_request_msg.context().empty()) {
440 throw TLS_Exception(Alert::DecodeError, "Certificate_Request context must be empty in the main handshake");
441 }
442
444 certificate_request_msg.extensions(), Connection_Side::Server, Handshake_Type::CertificateRequest);
445 m_transitions.set_expected_next(Handshake_Type::Certificate);
446}
447
448void Client_Impl_13::handle(const Certificate_13& certificate_msg) {
449 // RFC 8446 4.4.2
450 // certificate_request_context: [...] In the case of server authentication,
451 // this field SHALL be zero length.
452 if(!certificate_msg.request_context().empty()) {
453 throw TLS_Exception(Alert::DecodeError, "Received a server certificate message with non-empty request context");
454 }
455
456 // RFC 8446 4.4.2
457 // Extensions in the Certificate message from the server MUST correspond
458 // to ones from the ClientHello message.
459 certificate_msg.validate_extensions(m_handshake_state.client_hello().extensions().extension_types(), callbacks());
460 certificate_msg.verify(callbacks(),
461 policy(),
463 m_info.hostname(),
464 m_handshake_state.client_hello().extensions().has<Certificate_Status_Request>());
465
466 m_transitions.set_expected_next(Handshake_Type::CertificateVerify);
467}
468
469void Client_Impl_13::handle(const Certificate_Verify_13& certificate_verify_msg) {
470 // RFC 8446 4.4.3
471 // If the CertificateVerify message is sent by a server, the signature
472 // algorithm MUST be one offered in the client's "signature_algorithms"
473 // extension unless no valid certificate chain can be produced without
474 // unsupported algorithms.
475 //
476 // Note: if the server failed to produce a certificate chain without using
477 // an unsupported signature scheme, we opt to abort the handshake.
478 const auto offered = m_handshake_state.client_hello().signature_schemes();
479 if(!value_exists(offered, certificate_verify_msg.signature_scheme())) {
480 throw TLS_Exception(Alert::IllegalParameter,
481 "We did not offer the usage of " + certificate_verify_msg.signature_scheme().to_string() +
482 " as a signature scheme");
483 }
484
485 const bool sig_valid = certificate_verify_msg.verify(
486 *m_handshake_state.server_certificate().public_key(), callbacks(), m_transcript_hash.previous());
487
488 if(!sig_valid) {
489 throw TLS_Exception(Alert::DecryptError, "Server certificate verification failed");
490 }
491
492 m_transitions.set_expected_next(Handshake_Type::Finished);
493}
494
495void Client_Impl_13::send_client_authentication(Channel_Impl_13::AggregatedHandshakeMessages& flight) {
496 BOTAN_ASSERT_NOMSG(m_handshake_state.has_certificate_request());
497 const auto& cert_request = m_handshake_state.certificate_request();
498
499 const auto cert_type = [&] {
500 const auto& exts = m_handshake_state.encrypted_extensions().extensions();
501 const auto& chexts = m_handshake_state.client_hello().extensions();
502 if(exts.has<Client_Certificate_Type>() && chexts.has<Client_Certificate_Type>()) {
503 const auto* client_cert_type = exts.get<Client_Certificate_Type>();
504 chexts.get<Client_Certificate_Type>()->validate_selection(*client_cert_type);
505
506 // RFC 7250 4.2
507 // This client_certificate_type extension in the server hello then
508 // indicates the type of certificates the client is requested to
509 // provide in a subsequent certificate payload.
510 //
511 // Note: TLS 1.3 carries this extension in the Encrypted Extensions
512 // message instead of the Server Hello.
513 return client_cert_type->selected_certificate_type();
514 } else {
515 // RFC 8446 4.4.2
516 // If the corresponding certificate type extension [...] was not
517 // negotiated in EncryptedExtensions, [...] then each
518 // CertificateEntry contains a DER-encoded X.509 certificate.
520 }
521 }();
522
523 // RFC 8446 4.4.2
524 // certificate_request_context: If this message is in response to a
525 // CertificateRequest, the value of certificate_request_context in
526 // that message.
527 flight.add(m_handshake_state.sending(
528 Certificate_13(cert_request, m_info.hostname(), credentials_manager(), callbacks(), cert_type)));
529
530 // RFC 8446 4.4.2
531 // If the server requests client authentication but no suitable certificate
532 // is available, the client MUST send a Certificate message containing no
533 // certificates.
534 //
535 // In that case, no Certificate Verify message will be sent.
536 if(!m_handshake_state.client_certificate().empty()) {
537 flight.add(m_handshake_state.sending(Certificate_Verify_13(m_handshake_state.client_certificate(),
538 cert_request.signature_schemes(),
539 m_info.hostname(),
540 m_transcript_hash.current(),
543 policy(),
544 callbacks(),
545 rng())));
546 }
547}
548
549void Client_Impl_13::handle(const Finished_13& finished_msg) {
550 // RFC 8446 4.4.4
551 // Recipients of Finished messages MUST verify that the contents are
552 // correct and if incorrect MUST terminate the connection with a
553 // "decrypt_error" alert.
554 if(!finished_msg.verify(m_cipher_state.get(), m_transcript_hash.previous())) {
555 throw TLS_Exception(Alert::DecryptError, "Finished message didn't verify");
556 }
557
558 // Give the application a chance for a final veto before fully
559 // establishing the connection.
560 callbacks().tls_session_established(Session_Summary(m_handshake_state.server_hello(),
565 m_resumed_session.has_value(),
566 m_info,
567 callbacks().tls_current_timestamp()));
568
569 // Derives the secrets for receiving application data but defers
570 // the derivation of sending application data.
571 m_cipher_state->advance_with_server_finished(m_transcript_hash.current(), *this);
572
573 auto flight = aggregate_handshake_messages();
574
575 // RFC 8446 4.4.2
576 // The client MUST send a Certificate message if and only if the server
577 // has requested client authentication via a CertificateRequest message.
578 if(m_handshake_state.has_certificate_request()) {
579 send_client_authentication(flight);
580 }
581
582 // send client finished handshake message (still using handshake traffic secrets)
583 flight.add(m_handshake_state.sending(Finished_13(m_cipher_state.get(), m_transcript_hash.current())));
584
585 flight.send();
586
587 // derives the sending application traffic secrets
588 m_cipher_state->advance_with_client_finished(m_transcript_hash.current());
589
590 // TODO: Create a dummy session object and invoke tls_session_established.
591 // Alternatively, consider changing the expectations described in the
592 // callback's doc string.
593
594 // no more handshake messages expected
595 m_transitions.set_expected_next({});
596
598}
599
600void TLS::Client_Impl_13::handle(const New_Session_Ticket_13& new_session_ticket) {
601 callbacks().tls_examine_extensions(
602 new_session_ticket.extensions(), Connection_Side::Server, Handshake_Type::NewSessionTicket);
603
604 const Session session(m_cipher_state->psk(new_session_ticket.nonce()),
605 new_session_ticket.early_data_byte_limit(),
606 new_session_ticket.ticket_age_add(),
607 new_session_ticket.lifetime_hint(),
608 m_handshake_state.server_hello().selected_version(),
609 m_handshake_state.server_hello().ciphersuite(),
611 peer_cert_chain(),
612 peer_raw_public_key(),
613 m_info,
614 callbacks().tls_current_timestamp());
615
616 if(callbacks().tls_should_persist_resumption_information(session)) {
617 session_manager().store(session, Session_Handle(new_session_ticket.handle()));
618 }
619}
620
621std::vector<X509_Certificate> Client_Impl_13::peer_cert_chain() const {
622 if(m_handshake_state.has_server_certificate_msg() &&
623 m_handshake_state.server_certificate().has_certificate_chain()) {
624 return m_handshake_state.server_certificate().cert_chain();
625 }
626
627 if(m_resumed_session.has_value()) {
628 return m_resumed_session->session.peer_certs();
629 }
630
631 return {};
632}
633
634std::shared_ptr<const Public_Key> Client_Impl_13::peer_raw_public_key() const {
635 if(m_handshake_state.has_server_certificate_msg() && m_handshake_state.server_certificate().is_raw_public_key()) {
636 return m_handshake_state.server_certificate().public_key();
637 }
638
639 if(m_resumed_session.has_value()) {
640 return m_resumed_session->session.peer_raw_public_key();
641 }
642
643 return nullptr;
644}
645
646std::optional<std::string> Client_Impl_13::external_psk_identity() const {
647 return m_psk_identity;
648}
649
651 return std::exchange(m_should_send_ccs, false); // test-and-set
652}
653
654void Client_Impl_13::maybe_log_secret(std::string_view label, std::span<const uint8_t> secret) const {
655 if(policy().allow_ssl_key_log_file()) {
656 callbacks().tls_ssl_key_log_data(label, m_handshake_state.client_hello().random(), secret);
657 }
658}
659
662 const auto& eee = m_handshake_state.encrypted_extensions().extensions();
664 return eee.get<Application_Layer_Protocol_Notification>()->single_protocol();
665 }
666 }
667
668 return "";
669}
670
671} // 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