Botan 3.9.0
Crypto and TLS for C&
tls_server_impl_12.cpp
Go to the documentation of this file.
1/*
2* TLS Server
3* (C) 2004-2011,2012,2016 Jack Lloyd
4* 2016 Matthias Gierlings
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/internal/tls_server_impl_12.h>
10
11#include <botan/ocsp.h>
12#include <botan/tls_magic.h>
13#include <botan/tls_messages.h>
14#include <botan/tls_server.h>
15#include <botan/tls_version.h>
16#include <botan/internal/stl_util.h>
17#include <botan/internal/tls_handshake_state.h>
18
19namespace Botan::TLS {
20
21class Server_Handshake_State final : public Handshake_State {
22 public:
23 Server_Handshake_State(std::unique_ptr<Handshake_IO> io, Callbacks& cb) : Handshake_State(std::move(io), cb) {}
24
25 Private_Key* server_rsa_kex_key() { return m_server_rsa_kex_key.get(); }
26
27 void set_server_rsa_kex_key(std::shared_ptr<Private_Key> key) { m_server_rsa_kex_key = std::move(key); }
28
29 bool allow_session_resumption() const { return m_allow_session_resumption; }
30
31 void set_allow_session_resumption(bool allow_session_resumption) {
32 m_allow_session_resumption = allow_session_resumption;
33 }
34
35 const std::vector<X509_Certificate>& resume_peer_certs() const { return m_resume_peer_certs; }
36
37 void set_resume_certs(const std::vector<X509_Certificate>& certs) { m_resume_peer_certs = certs; }
38
39 void mark_as_resumption() { m_is_a_resumption = true; }
40
41 bool is_a_resumption() const { return m_is_a_resumption; }
42
43 private:
44 // Used by the server only, in case of RSA key exchange.
45 std::shared_ptr<Private_Key> m_server_rsa_kex_key;
46
47 /*
48 * Used by the server to know if resumption should be allowed on
49 * a server-initiated renegotiation
50 */
51 bool m_allow_session_resumption = true;
52
53 bool m_is_a_resumption = false;
54
55 std::vector<X509_Certificate> m_resume_peer_certs;
56};
57
58namespace {
59
60std::optional<Session> check_for_resume(const Session_Handle& handle_to_resume,
61 Session_Manager& session_manager,
62 Callbacks& cb,
63 const Policy& policy,
64 const Client_Hello_12* client_hello) {
65 auto session = session_manager.retrieve(handle_to_resume, cb, policy);
66 if(!session.has_value()) {
67 return std::nullopt;
68 }
69
70 // wrong version
71 if(client_hello->legacy_version() != session->version()) {
72 return std::nullopt;
73 }
74
75 // client didn't send original ciphersuite
76 if(!value_exists(client_hello->ciphersuites(), session->ciphersuite_code())) {
77 return std::nullopt;
78 }
79
80 // client sent a different SNI hostname
81 if(!client_hello->sni_hostname().empty() && client_hello->sni_hostname() != session->server_info().hostname()) {
82 return std::nullopt;
83 }
84
85 // Checking extended_master_secret on resume (RFC 7627 section 5.3)
86 if(client_hello->supports_extended_master_secret() != session->supports_extended_master_secret()) {
87 if(!session->supports_extended_master_secret()) {
88 return std::nullopt; // force new handshake with extended master secret
89 } else {
90 /*
91 Client previously negotiated session with extended master secret,
92 but has now attempted to resume without the extension: abort
93 */
94 throw TLS_Exception(Alert::HandshakeFailure, "Client resumed extended ms session without sending extension");
95 }
96 }
97
98 // Checking encrypt_then_mac on resume (RFC 7366 section 3.1)
99 if(!client_hello->supports_encrypt_then_mac() && session->supports_encrypt_then_mac()) {
100 /*
101 Client previously negotiated session with Encrypt-then-MAC,
102 but has now attempted to resume without the extension: abort
103 */
104 throw TLS_Exception(Alert::HandshakeFailure, "Client resumed Encrypt-then-MAC session without sending extension");
105 }
106
107 return session;
108}
109
110/*
111* Choose which ciphersuite to use
112*/
113uint16_t choose_ciphersuite(const Policy& policy,
114 Protocol_Version version,
115 const std::map<std::string, std::vector<X509_Certificate>>& cert_chains,
116 const Client_Hello_12& client_hello) {
117 const bool our_choice = policy.server_uses_own_ciphersuite_preferences();
118 const std::vector<uint16_t>& client_suites = client_hello.ciphersuites();
119 const std::vector<uint16_t> server_suites = policy.ciphersuite_list(version);
120
121 if(server_suites.empty()) {
122 throw TLS_Exception(Alert::HandshakeFailure, "Policy forbids us from negotiating any ciphersuite");
123 }
124
125 const bool have_shared_ecc_curve =
126 (policy.choose_key_exchange_group(client_hello.supported_ecc_curves(), {}) != Group_Params::NONE);
127
128 const bool client_supports_ffdhe_groups = !client_hello.supported_dh_groups().empty();
129
130 const bool have_shared_dh_group =
131 (policy.choose_key_exchange_group(client_hello.supported_dh_groups(), {}) != Group_Params::NONE);
132
133 /*
134 Walk down one list in preference order
135 */
136 std::vector<uint16_t> pref_list = server_suites;
137 std::vector<uint16_t> other_list = client_suites;
138
139 if(!our_choice) {
140 std::swap(pref_list, other_list);
141 }
142
143 for(auto suite_id : pref_list) {
144 if(!value_exists(other_list, suite_id)) {
145 continue;
146 }
147
148 const auto suite = Ciphersuite::by_id(suite_id);
149
150 if(!suite.has_value() || !suite->valid()) {
151 continue;
152 }
153
154 if(have_shared_ecc_curve == false && suite->ecc_ciphersuite()) {
155 continue;
156 }
157
158 if(suite->kex_method() == Kex_Algo::DH && client_supports_ffdhe_groups && !have_shared_dh_group) {
159 continue;
160 }
161
162 // For non-anon ciphersuites
163 if(suite->signature_used()) {
164 const std::string sig_algo = suite->sig_algo();
165
166 // Do we have any certificates for this sig?
167 if(!cert_chains.contains(sig_algo)) {
168 continue;
169 }
170
171 const std::vector<Signature_Scheme> allowed = policy.allowed_signature_schemes();
172
173 std::vector<Signature_Scheme> client_sig_methods = client_hello.signature_schemes();
174
175 /*
176 Contrary to the wording of draft-ietf-tls-md5-sha1-deprecate we do
177 not enforce that clients do not offer support SHA-1 or MD5
178 signatures; we just ignore it.
179 */
180 bool we_support_some_hash_by_client = false;
181
182 for(Signature_Scheme scheme : client_sig_methods) {
183 if(!scheme.is_available()) {
184 continue;
185 }
186
187 if(scheme.algorithm_name() == suite->sig_algo() &&
188 policy.allowed_signature_hash(scheme.hash_function_name())) {
189 we_support_some_hash_by_client = true;
190 }
191 }
192
193 if(we_support_some_hash_by_client == false) {
194 throw TLS_Exception(Alert::HandshakeFailure,
195 "Policy does not accept any hash function supported by client");
196 }
197 }
198
199 return suite_id;
200 }
201
202 // RFC 7919 Section 4.
203 // If the [Supported Groups] extension is present
204 // with FFDHE groups, none of the client’s offered groups are acceptable
205 // by the server, and none of the client’s proposed non-FFDHE cipher
206 // suites are acceptable to the server, the server MUST end the
207 // connection with a fatal TLS alert of type insufficient_security(71).
208 if(client_supports_ffdhe_groups && !have_shared_dh_group) {
209 throw TLS_Exception(Alert::InsufficientSecurity, "Can't agree on a sufficiently strong ciphersuite with client");
210 }
211
212 throw TLS_Exception(Alert::HandshakeFailure, "Can't agree on a ciphersuite with client");
213}
214
215std::map<std::string, std::vector<X509_Certificate>> get_server_certs(
216 std::string_view hostname, const std::vector<Signature_Scheme>& cert_sig_schemes, Credentials_Manager& creds) {
217 const char* cert_types[] = {"RSA", "ECDSA", "DSA", nullptr};
218
219 std::map<std::string, std::vector<X509_Certificate>> cert_chains;
220
221 for(size_t i = 0; cert_types[i] != nullptr; ++i) {
222 const std::vector<X509_Certificate> certs = creds.cert_chain_single_type(
223 cert_types[i], to_algorithm_identifiers(cert_sig_schemes), "tls-server", std::string(hostname));
224
225 if(!certs.empty()) {
226 cert_chains[cert_types[i]] = certs;
227 }
228 }
229
230 return cert_chains;
231}
232
233} // namespace
234
235Server_Impl_12::Server_Impl_12(const std::shared_ptr<Callbacks>& callbacks,
236 const std::shared_ptr<Session_Manager>& session_manager,
237 const std::shared_ptr<Credentials_Manager>& creds,
238 const std::shared_ptr<const Policy>& policy,
239 const std::shared_ptr<RandomNumberGenerator>& rng,
240 bool is_datagram,
241 size_t io_buf_sz) :
242 Channel_Impl_12(callbacks, session_manager, rng, policy, true, is_datagram, io_buf_sz), m_creds(creds) {
243 BOTAN_ASSERT_NONNULL(m_creds);
244}
245
247 Channel_Impl_12(downgrade_info.callbacks,
248 downgrade_info.session_manager,
249 downgrade_info.rng,
250 downgrade_info.policy,
251 true /* is_server*/,
252 false /* TLS 1.3 does not support DTLS yet */,
253 downgrade_info.io_buffer_size),
254 m_creds(downgrade_info.creds) {}
255
256std::unique_ptr<Handshake_State> Server_Impl_12::new_handshake_state(std::unique_ptr<Handshake_IO> io) {
257 auto state = std::make_unique<Server_Handshake_State>(std::move(io), callbacks());
258 state->set_expected_next(Handshake_Type::ClientHello);
259 return state;
260}
261
262std::vector<X509_Certificate> Server_Impl_12::get_peer_cert_chain(const Handshake_State& state_base) const {
263 const Server_Handshake_State& state = dynamic_cast<const Server_Handshake_State&>(state_base);
264 if(!state.resume_peer_certs().empty()) {
265 return state.resume_peer_certs();
266 }
267
268 if(state.client_certs() != nullptr) {
269 return state.client_certs()->cert_chain();
270 }
271 return std::vector<X509_Certificate>();
272}
273
274/*
275* Send a hello request to the client
276*/
277void Server_Impl_12::initiate_handshake(Handshake_State& state, bool force_full_renegotiation) {
278 dynamic_cast<Server_Handshake_State&>(state).set_allow_session_resumption(!force_full_renegotiation);
279
280 Hello_Request hello_req(state.handshake_io());
281}
282
283namespace {
284
285Protocol_Version select_version(const TLS::Policy& policy,
286 Protocol_Version client_offer,
287 Protocol_Version active_version,
288 const std::vector<Protocol_Version>& supported_versions) {
289 const bool is_datagram = client_offer.is_datagram_protocol();
290 const bool initial_handshake = (active_version.valid() == false);
291
292 if(!supported_versions.empty()) {
293 if(is_datagram) {
294 if(policy.allow_dtls12() && value_exists(supported_versions, Protocol_Version(Protocol_Version::DTLS_V12))) {
295 return Protocol_Version::DTLS_V12;
296 }
297 throw TLS_Exception(Alert::ProtocolVersion, "No shared DTLS version");
298 } else {
299 if(policy.allow_tls12() && value_exists(supported_versions, Protocol_Version(Protocol_Version::TLS_V12))) {
300 return Protocol_Version::TLS_V12;
301 }
302 throw TLS_Exception(Alert::ProtocolVersion, "No shared TLS version");
303 }
304 }
305
306 if(!initial_handshake) {
307 /*
308 * If this is a renegotiation, and the client has offered a
309 * later version than what it initially negotiated, negotiate
310 * the old version. This matches OpenSSL's behavior. If the
311 * client is offering a version earlier than what it initially
312 * negotiated, reject as a probable attack.
313 */
314 if(active_version > client_offer) {
315 throw TLS_Exception(
316 Alert::ProtocolVersion,
317 "Client negotiated " + active_version.to_string() + " then renegotiated with " + client_offer.to_string());
318 } else {
319 return active_version;
320 }
321 }
322
323 if(is_datagram) {
324 if(policy.allow_dtls12() && client_offer >= Protocol_Version::DTLS_V12) {
325 return Protocol_Version::DTLS_V12;
326 }
327 } else {
328 if(policy.allow_tls12() && client_offer >= Protocol_Version::TLS_V12) {
329 return Protocol_Version::TLS_V12;
330 }
331 }
332
333 throw TLS_Exception(Alert::ProtocolVersion,
334 "Client version " + client_offer.to_string() + " is unacceptable by policy");
335}
336} // namespace
337
338/*
339* Process a Client Hello Message
340*/
341void Server_Impl_12::process_client_hello_msg(const Handshake_State* active_state,
342 Server_Handshake_State& pending_state,
343 const std::vector<uint8_t>& contents,
344 bool epoch0_restart) {
345 BOTAN_ASSERT_IMPLICATION(epoch0_restart, active_state != nullptr, "Can't restart with a dead connection");
346
347 const bool initial_handshake = epoch0_restart || active_state == nullptr;
348
349 if(initial_handshake == false && policy().allow_client_initiated_renegotiation() == false) {
350 if(policy().abort_connection_on_undesired_renegotiation()) {
351 throw TLS_Exception(Alert::NoRenegotiation, "Server policy prohibits renegotiation");
352 } else {
353 send_warning_alert(Alert::NoRenegotiation);
354 }
355 return;
356 }
357
358 if(!policy().allow_insecure_renegotiation() && !(initial_handshake || secure_renegotiation_supported())) {
359 send_warning_alert(Alert::NoRenegotiation);
360 return;
361 }
362
363 if(pending_state.handshake_io().have_more_data()) {
364 throw TLS_Exception(Alert::UnexpectedMessage, "Have data remaining in buffer after ClientHello");
365 }
366
367 pending_state.client_hello(std::make_unique<Client_Hello_12>(contents));
368 const Protocol_Version client_offer = pending_state.client_hello()->legacy_version();
369 const bool datagram = client_offer.is_datagram_protocol();
370
371 if(datagram) {
372 if(client_offer.major_version() == 0xFF) {
373 throw TLS_Exception(Alert::ProtocolVersion, "Client offered DTLS version with major version 0xFF");
374 }
375 } else {
376 if(client_offer.major_version() < 3) {
377 throw TLS_Exception(Alert::ProtocolVersion, "Client offered TLS version with major version under 3");
378 }
379 if(client_offer.major_version() == 3 && client_offer.minor_version() == 0) {
380 throw TLS_Exception(Alert::ProtocolVersion, "Client offered SSLv3 which is not supported");
381 }
382 }
383
384 /*
385 * BoGo test suite expects that we will send the hello verify with a record
386 * version matching the version that is eventually negotiated. This is wrong
387 * but harmless, so go with it. Also doing the version negotiation step first
388 * allows to immediately close the connection with an alert if the client has
389 * offered a version that we are not going to negotiate anyway, instead of
390 * making them first do the cookie exchange and then telling them no.
391 *
392 * There is no issue with amplification here, since the alert is just 2 bytes.
393 */
394 const Protocol_Version negotiated_version =
395 select_version(policy(),
396 client_offer,
397 active_state != nullptr ? active_state->version() : Protocol_Version(),
398 pending_state.client_hello()->supported_versions());
399
400 pending_state.set_version(negotiated_version);
401
402 const auto compression_methods = pending_state.client_hello()->compression_methods();
403 if(!value_exists(compression_methods, uint8_t(0))) {
404 throw TLS_Exception(Alert::IllegalParameter, "Client did not offer NULL compression");
405 }
406
407 if(initial_handshake && datagram) {
408 SymmetricKey cookie_secret;
409
410 try {
411 cookie_secret = m_creds->psk("tls-server", "dtls-cookie-secret", "");
412 } catch(...) {}
413
414 if(!cookie_secret.empty()) {
415 const std::string client_identity = callbacks().tls_peer_network_identity();
416 Hello_Verify_Request verify(pending_state.client_hello()->cookie_input_data(), client_identity, cookie_secret);
417
418 if(pending_state.client_hello()->cookie() != verify.cookie()) {
419 if(epoch0_restart) {
420 pending_state.handshake_io().send_under_epoch(verify, 0);
421 } else {
422 pending_state.handshake_io().send(verify);
423 }
424
425 pending_state.client_hello(nullptr);
426 pending_state.set_expected_next(Handshake_Type::ClientHello);
427 return;
428 }
429 } else if(epoch0_restart) {
430 throw TLS_Exception(Alert::HandshakeFailure, "Reuse of DTLS association requires DTLS cookie secret be set");
431 }
432 }
433
434 if(epoch0_restart) {
435 // If we reached here then we were able to verify the cookie
437 }
438
439 secure_renegotiation_check(pending_state.client_hello());
440
442 pending_state.client_hello()->extensions(), Connection_Side::Client, Handshake_Type::ClientHello);
443
444 const auto session_handle = pending_state.client_hello()->session_handle();
445
446 std::optional<Session> session_info;
447 if(pending_state.allow_session_resumption() && session_handle.has_value()) {
448 session_info = check_for_resume(
449 session_handle.value(), session_manager(), callbacks(), policy(), pending_state.client_hello());
450 }
451
452 m_next_protocol = "";
453 if(pending_state.client_hello()->supports_alpn()) {
454 m_next_protocol = callbacks().tls_server_choose_app_protocol(pending_state.client_hello()->next_protocols());
455 }
456
457 if(session_info.has_value()) {
458 this->session_resume(pending_state, {session_info.value(), session_handle.value()});
459 } else {
460 // new session
461 this->session_create(pending_state);
462 }
463}
464
465void Server_Impl_12::process_certificate_msg(Server_Handshake_State& pending_state,
466 const std::vector<uint8_t>& contents) {
467 pending_state.client_certs(std::make_unique<Certificate_12>(contents, policy()));
468
469 // CERTIFICATE_REQUIRED would make more sense but BoGo expects handshake failure alert
470 if(pending_state.client_certs()->empty() && policy().require_client_certificate_authentication()) {
471 throw TLS_Exception(Alert::HandshakeFailure, "Policy requires client send a certificate, but it did not");
472 }
473
474 pending_state.set_expected_next(Handshake_Type::ClientKeyExchange);
475}
476
477void Server_Impl_12::process_client_key_exchange_msg(Server_Handshake_State& pending_state,
478 const std::vector<uint8_t>& contents) {
479 if(pending_state.received_handshake_msg(Handshake_Type::Certificate) && !pending_state.client_certs()->empty()) {
480 pending_state.set_expected_next(Handshake_Type::CertificateVerify);
481 } else {
482 pending_state.set_expected_next(Handshake_Type::HandshakeCCS);
483 }
484
485 pending_state.client_kex(std::make_unique<Client_Key_Exchange>(
486 contents, pending_state, pending_state.server_rsa_kex_key(), *m_creds, policy(), rng()));
487
488 pending_state.compute_session_keys();
489 if(policy().allow_ssl_key_log_file()) {
490 // draft-thomson-tls-keylogfile-00 Section 3.2
491 // An implementation of TLS 1.2 (and also earlier versions) use
492 // the label "CLIENT_RANDOM" to identify the "master" secret for
493 // the connection.
495 "CLIENT_RANDOM", pending_state.client_hello()->random(), pending_state.session_keys().master_secret());
496 }
497}
498
499void Server_Impl_12::process_change_cipher_spec_msg(Server_Handshake_State& pending_state) {
500 pending_state.set_expected_next(Handshake_Type::Finished);
502}
503
504void Server_Impl_12::process_certificate_verify_msg(Server_Handshake_State& pending_state,
505 Handshake_Type type,
506 const std::vector<uint8_t>& contents) {
507 pending_state.client_verify(std::make_unique<Certificate_Verify_12>(contents));
508
509 const std::vector<X509_Certificate>& client_certs = pending_state.client_certs()->cert_chain();
510
511 if(client_certs.empty()) {
512 throw TLS_Exception(Alert::DecodeError, "No client certificate sent");
513 }
514
515 if(!client_certs[0].allowed_usage(Key_Constraints::DigitalSignature)) {
516 throw TLS_Exception(Alert::BadCertificate, "Client certificate does not support signing");
517 }
518
519 const bool sig_valid = pending_state.client_verify()->verify(client_certs[0], pending_state, policy());
520
521 pending_state.hash().update(pending_state.handshake_io().format(contents, type));
522
523 /*
524 * Using DECRYPT_ERROR looks weird here, but per RFC 4346 is for
525 * "A handshake cryptographic operation failed, including being
526 * unable to correctly verify a signature, ..."
527 */
528 if(!sig_valid) {
529 throw TLS_Exception(Alert::DecryptError, "Client cert verify failed");
530 }
531
532 try {
533 const std::string sni_hostname = pending_state.client_hello()->sni_hostname();
534 auto trusted_CAs = m_creds->trusted_certificate_authorities("tls-server", sni_hostname);
535
536 callbacks().tls_verify_cert_chain(client_certs,
537 {}, // ocsp
538 trusted_CAs,
540 sni_hostname,
541 policy());
542 } catch(std::exception& e) {
543 throw TLS_Exception(Alert::BadCertificate, e.what());
544 }
545
546 pending_state.set_expected_next(Handshake_Type::HandshakeCCS);
547}
548
549void Server_Impl_12::process_finished_msg(Server_Handshake_State& pending_state,
550 Handshake_Type type,
551 const std::vector<uint8_t>& contents) {
552 pending_state.set_expected_next(Handshake_Type::None);
553
554 if(pending_state.handshake_io().have_more_data()) {
555 throw TLS_Exception(Alert::UnexpectedMessage, "Have data remaining in buffer after Finished");
556 }
557
558 pending_state.client_finished(std::make_unique<Finished_12>(contents));
559
560 if(!pending_state.client_finished()->verify(pending_state, Connection_Side::Client)) {
561 throw TLS_Exception(Alert::DecryptError, "Finished message didn't verify");
562 }
563
564 if(pending_state.server_finished() == nullptr) {
565 // already sent finished if resuming, so this is a new session
566
567 pending_state.hash().update(pending_state.handshake_io().format(contents, type));
568
569 Session session_info(pending_state.session_keys().master_secret(),
570 pending_state.server_hello()->legacy_version(),
571 pending_state.server_hello()->ciphersuite(),
573 pending_state.server_hello()->supports_extended_master_secret(),
574 pending_state.server_hello()->supports_encrypt_then_mac(),
575 get_peer_cert_chain(pending_state),
576 Server_Information(pending_state.client_hello()->sni_hostname()),
577 pending_state.server_hello()->srtp_profile(),
578 callbacks().tls_current_timestamp());
579
580 // Give the application a chance for a final veto before fully
581 // establishing the connection.
583 Session_Summary summary(session_info, pending_state.is_a_resumption(), external_psk_identity());
584 summary.set_session_id(pending_state.server_hello()->session_id());
585 return summary;
586 }());
587
588 if(callbacks().tls_should_persist_resumption_information(session_info)) {
589 auto handle = session_manager().establish(session_info,
590 pending_state.server_hello()->session_id(),
591 !pending_state.server_hello()->supports_session_ticket());
592
593 if(pending_state.server_hello()->supports_session_ticket() && handle.has_value() && handle->is_ticket()) {
594 pending_state.new_session_ticket(
595 std::make_unique<New_Session_Ticket_12>(pending_state.handshake_io(),
596 pending_state.hash(),
597 handle->ticket().value(),
598 policy().session_ticket_lifetime()));
599 }
600 }
601
602 if(pending_state.new_session_ticket() == nullptr && pending_state.server_hello()->supports_session_ticket()) {
603 pending_state.new_session_ticket(
604 std::make_unique<New_Session_Ticket_12>(pending_state.handshake_io(), pending_state.hash()));
605 }
606
607 pending_state.handshake_io().send(Change_Cipher_Spec());
608
610
611 pending_state.server_finished(
612 std::make_unique<Finished_12>(pending_state.handshake_io(), pending_state, Connection_Side::Server));
613 }
614
616}
617
618/*
619* Process a handshake message
620*/
621void Server_Impl_12::process_handshake_msg(const Handshake_State* active_state,
622 Handshake_State& state_base,
623 Handshake_Type type,
624 const std::vector<uint8_t>& contents,
625 bool epoch0_restart) {
626 Server_Handshake_State& state = dynamic_cast<Server_Handshake_State&>(state_base);
627 state.confirm_transition_to(type);
628
629 /*
630 * The change cipher spec message isn't technically a handshake
631 * message so it's not included in the hash. The finished and
632 * certificate verify messages are verified based on the current
633 * state of the hash *before* this message so we delay adding them
634 * to the hash computation until we've processed them below.
635 */
638 state.hash().update(state.handshake_io().format(contents, type));
639 }
640
641 switch(type) {
643 return this->process_client_hello_msg(active_state, state, contents, epoch0_restart);
644
646 return this->process_certificate_msg(state, contents);
647
649 return this->process_client_key_exchange_msg(state, contents);
650
652 return this->process_certificate_verify_msg(state, type, contents);
653
655 return this->process_change_cipher_spec_msg(state);
656
658 return this->process_finished_msg(state, type, contents);
659
660 default:
661 throw Unexpected_Message("Unknown handshake message received");
662 }
663}
664
665void Server_Impl_12::session_resume(Server_Handshake_State& pending_state, const Session_with_Handle& session) {
666 // Only offer a resuming client a new ticket if they didn't send one this time,
667 // ie, resumed via server-side resumption. TODO: also send one if expiring soon?
668
669 const bool offer_new_session_ticket = pending_state.client_hello()->supports_session_ticket() &&
670 pending_state.client_hello()->session_ticket().empty() &&
672
673 pending_state.server_hello(std::make_unique<Server_Hello_12>(pending_state.handshake_io(),
674 pending_state.hash(),
675 policy(),
676 callbacks(),
677 rng(),
679 *pending_state.client_hello(),
680 session.session,
681 offer_new_session_ticket,
682 m_next_protocol));
683
684 secure_renegotiation_check(pending_state.server_hello());
685
686 pending_state.mark_as_resumption();
687 pending_state.compute_session_keys(session.session.master_secret());
688 if(policy().allow_ssl_key_log_file()) {
689 // draft-thomson-tls-keylogfile-00 Section 3.2
690 // An implementation of TLS 1.2 (and also earlier versions) use
691 // the label "CLIENT_RANDOM" to identify the "master" secret for
692 // the connection.
694 "CLIENT_RANDOM", pending_state.client_hello()->random(), pending_state.session_keys().master_secret());
695 }
696 pending_state.set_resume_certs(session.session.peer_certs());
697
698 // Give the application a chance for a final veto before fully
699 // establishing the connection.
701 Session_Summary summary(session.session, pending_state.is_a_resumption(), external_psk_identity());
702 summary.set_session_id(pending_state.server_hello()->session_id());
703 if(auto ticket = session.handle.ticket()) {
704 summary.set_session_ticket(std::move(ticket.value()));
705 }
706 return summary;
707 }());
708
709 auto new_handle = [&, this]() -> std::optional<Session_Handle> {
710 if(!callbacks().tls_should_persist_resumption_information(session.session)) {
711 session_manager().remove(session.handle);
712 return std::nullopt;
713 } else {
714 return session_manager().establish(session.session, session.handle.id());
715 }
716 }();
717
718 if(pending_state.server_hello()->supports_session_ticket()) {
719 if(new_handle.has_value() && new_handle->is_ticket()) {
720 pending_state.new_session_ticket(std::make_unique<New_Session_Ticket_12>(pending_state.handshake_io(),
721 pending_state.hash(),
722 new_handle->ticket().value(),
723 policy().session_ticket_lifetime()));
724 } else {
725 pending_state.new_session_ticket(
726 std::make_unique<New_Session_Ticket_12>(pending_state.handshake_io(), pending_state.hash()));
727 }
728 }
729
730 pending_state.handshake_io().send(Change_Cipher_Spec());
731
733
734 pending_state.server_finished(
735 std::make_unique<Finished_12>(pending_state.handshake_io(), pending_state, Connection_Side::Server));
736 pending_state.set_expected_next(Handshake_Type::HandshakeCCS);
737}
738
739void Server_Impl_12::session_create(Server_Handshake_State& pending_state) {
740 std::map<std::string, std::vector<X509_Certificate>> cert_chains;
741
742 const std::string sni_hostname = pending_state.client_hello()->sni_hostname();
743
744 // RFC 8446 1.3
745 // The "signature_algorithms_cert" extension allows a client to indicate
746 // which signature algorithms it can validate in X.509 certificates.
747 //
748 // RFC 8446 4.2.3
749 // TLS 1.2 implementations SHOULD also process this extension.
750 const auto cert_signature_schemes = pending_state.client_hello()->certificate_signature_schemes();
751 cert_chains = get_server_certs(sni_hostname, cert_signature_schemes, *m_creds);
752
753 if(!sni_hostname.empty() && cert_chains.empty()) {
754 cert_chains = get_server_certs("", cert_signature_schemes, *m_creds);
755
756 /*
757 * Only send the unrecognized_name alert if we couldn't
758 * find any certs for the requested name but did find at
759 * least one cert to use in general. That avoids sending an
760 * unrecognized_name when a server is configured for purely
761 * anonymous/PSK operation.
762 */
763 if(!cert_chains.empty()) {
764 send_warning_alert(Alert::UnrecognizedName);
765 }
766 }
767
768 const uint16_t ciphersuite =
769 choose_ciphersuite(policy(), pending_state.version(), cert_chains, *pending_state.client_hello());
770
771 Server_Hello_12::Settings srv_settings(Session_ID(make_hello_random(rng(), callbacks(), policy())),
772 pending_state.version(),
773 ciphersuite,
774 session_manager().emits_session_tickets());
775
776 pending_state.server_hello(std::make_unique<Server_Hello_12>(pending_state.handshake_io(),
777 pending_state.hash(),
778 policy(),
779 callbacks(),
780 rng(),
782 *pending_state.client_hello(),
783 srv_settings,
784 m_next_protocol));
785
786 secure_renegotiation_check(pending_state.server_hello());
787
788 const Ciphersuite& pending_suite = pending_state.ciphersuite();
789
790 std::shared_ptr<Private_Key> private_key;
791
792 if(pending_suite.signature_used() || pending_suite.kex_method() == Kex_Algo::STATIC_RSA) {
793 const std::string algo_used = pending_suite.signature_used() ? pending_suite.sig_algo() : "RSA";
794
795 BOTAN_ASSERT(!cert_chains[algo_used].empty(), "Attempting to send empty certificate chain");
796
797 pending_state.server_certs(
798 std::make_unique<Certificate_12>(pending_state.handshake_io(), pending_state.hash(), cert_chains[algo_used]));
799
800 if(pending_state.client_hello()->supports_cert_status_message() && pending_state.is_a_resumption() == false) {
801 auto* csr = pending_state.client_hello()->extensions().get<Certificate_Status_Request>();
802 // csr is non-null if client_hello()->supports_cert_status_message()
803 BOTAN_ASSERT_NOMSG(csr != nullptr);
804 const auto resp_bytes = callbacks().tls_provide_cert_status(cert_chains[algo_used], *csr);
805 if(!resp_bytes.empty()) {
806 pending_state.server_cert_status(
807 std::make_unique<Certificate_Status>(pending_state.handshake_io(), pending_state.hash(), resp_bytes));
808 }
809 }
810
811 private_key = m_creds->private_key_for(pending_state.server_certs()->cert_chain()[0], "tls-server", sni_hostname);
812
813 if(!private_key) {
814 throw Internal_Error("No private key located for associated server cert");
815 }
816 }
817
818 if(pending_suite.kex_method() == Kex_Algo::STATIC_RSA) {
819 pending_state.set_server_rsa_kex_key(private_key);
820 } else {
821 pending_state.server_kex(std::make_unique<Server_Key_Exchange>(
822 pending_state.handshake_io(), pending_state, policy(), *m_creds, rng(), private_key.get()));
823 }
824
825 auto trusted_CAs = m_creds->trusted_certificate_authorities("tls-server", sni_hostname);
826
827 std::vector<X509_DN> client_auth_CAs;
828
829 for(auto* store : trusted_CAs) {
830 auto subjects = store->all_subjects();
831 client_auth_CAs.insert(client_auth_CAs.end(), subjects.begin(), subjects.end());
832 }
833
834 const bool request_cert = (client_auth_CAs.empty() == false) || policy().request_client_certificate_authentication();
835
836 if(request_cert && pending_state.ciphersuite().signature_used()) {
837 pending_state.cert_req(std::make_unique<Certificate_Request_12>(
838 pending_state.handshake_io(), pending_state.hash(), policy(), client_auth_CAs));
839
840 /*
841 SSLv3 allowed clients to skip the Certificate message entirely
842 if they wanted. In TLS v1.0 and later clients must send a
843 (possibly empty) Certificate message
844 */
845 pending_state.set_expected_next(Handshake_Type::Certificate);
846 } else {
847 pending_state.set_expected_next(Handshake_Type::ClientKeyExchange);
848 }
849
850 pending_state.server_hello_done(
851 std::make_unique<Server_Hello_Done>(pending_state.handshake_io(), pending_state.hash()));
852}
853} // namespace Botan::TLS
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:114
#define BOTAN_ASSERT_IMPLICATION(expr1, expr2, msg)
Definition assert.h:101
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:62
virtual std::vector< uint8_t > tls_provide_cert_status(const std::vector< X509_Certificate > &chain, const Certificate_Status_Request &csr)
virtual std::string tls_peer_network_identity()
virtual std::string tls_server_choose_app_protocol(const std::vector< std::string > &client_protos)
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_verify_cert_chain(const std::vector< X509_Certificate > &cert_chain, const std::vector< std::optional< OCSP::Response > > &ocsp_responses, const std::vector< Certificate_Store * > &trusted_roots, Usage_Type usage, std::string_view hostname, const TLS::Policy &policy)
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
RandomNumberGenerator & rng()
void change_cipher_spec_reader(Connection_Side side)
std::vector< uint8_t > secure_renegotiation_data_for_server_hello() const
void secure_renegotiation_check(const Client_Hello_12 *client_hello)
Session_Manager & session_manager()
const Policy & policy() const
void change_cipher_spec_writer(Connection_Side side)
Channel_Impl_12(const std::shared_ptr< Callbacks > &callbacks, const std::shared_ptr< Session_Manager > &session_manager, const std::shared_ptr< RandomNumberGenerator > &rng, const std::shared_ptr< const Policy > &policy, bool is_server, bool is_datagram, size_t io_buf_sz=TLS::Channel::IO_BUF_DEFAULT_SIZE)
std::optional< std::string > external_psk_identity() const override
bool secure_renegotiation_supported() const override
void send_warning_alert(Alert::Type type)
static std::optional< Ciphersuite > by_id(uint16_t suite)
Handshake_State(std::unique_ptr< Handshake_IO > io, Callbacks &callbacks)
virtual bool request_client_certificate_authentication() const
Server_Impl_12(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, bool is_datagram=false, size_t reserved_io_buffer_size=TLS::Channel::IO_BUF_DEFAULT_SIZE)
Helper class to embody a session handle in all protocol versions.
Definition tls_session.h:63
virtual size_t remove(const Session_Handle &handle)=0
virtual std::optional< Session_Handle > establish(const Session &session, const std::optional< Session_ID > &id=std::nullopt, bool tls12_no_ticket=false)
Save a new Session and assign a Session_Handle (TLS Server)
std::vector< AlgorithmIdentifier > to_algorithm_identifiers(const std::vector< Signature_Scheme > &schemes)
std::vector< uint8_t > make_hello_random(RandomNumberGenerator &rng, Callbacks &cb, const Policy &policy)
Strong< std::vector< uint8_t >, struct Session_ID_ > Session_ID
holds a TLS 1.2 session ID for stateful resumption
Definition tls_session.h:31
OctetString SymmetricKey
Definition symkey.h:140
bool value_exists(const std::vector< T > &vec, const OT &val)
Definition stl_util.h:52