Botan 2.19.5 Crypto and TLS for C&

tls_blocking.cpp
Go to the documentation of this file.
1/*
2* TLS Blocking API
3* (C) 2013 Jack Lloyd
4* 2016 Matthias Gierlings
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/tls_blocking.h>
10
11 namespace Botan {
12
13 namespace TLS {
14
15 Blocking_Client::Blocking_Client(read_fn reader,
16 write_fn writer,
17 Session_Manager& session_manager,
18 Credentials_Manager& creds,
19 const Policy& policy,
20 RandomNumberGenerator& rng,
21 const Server_Information& server_info,
22 const Protocol_Version& offer_version,
23 const std::vector<std::string>& next) :
24 m_read(reader),
25 m_callbacks(new TLS::Compat_Callbacks(
26 /*
27 we are ok using deprecated features here because the whole Blocking_Client class
28 is also deprecated, so just silence the warning.
29 */
30 TLS::Compat_Callbacks::SILENCE_DEPRECATION_WARNING::PLEASE,
31 writer,
32 std::bind(&Blocking_Client::data_cb, this, std::placeholders::_1, std::placeholders::_2),
33 std::function<void (Alert)>(std::bind(&Blocking_Client::alert_cb, this, std::placeholders::_1)),
34 std::bind(&Blocking_Client::handshake_cb, this, std::placeholders::_1)
35 )),
36 m_channel(*m_callbacks.get(),
37 session_manager,
38 creds,
39 policy,
40 rng,
41 server_info,
42 offer_version,
43 next)
44 {
45 }
46
47 bool Blocking_Client::handshake_cb(const Session& session)
48 {
49 return this->handshake_complete(session);
50 }
51
52 void Blocking_Client::alert_cb(const Alert& alert)
53 {
54 this->alert_notification(alert);
55 }
56
57 void Blocking_Client::data_cb(const uint8_t data[], size_t data_len)
58 {
59 m_plaintext.insert(m_plaintext.end(), data, data + data_len);
60 }
61
62 void Blocking_Client::do_handshake()
63 {
64 std::vector<uint8_t> readbuf(4096);
65
66 while(!m_channel.is_closed() && !m_channel.is_active())
67 {
68 const size_t from_socket = m_read(readbuf.data(), readbuf.size());
69 m_channel.received_data(readbuf.data(), from_socket);
70 }
71 }
72
73 size_t Blocking_Client::read(uint8_t buf[], size_t buf_len)
74 {
75 std::vector<uint8_t> readbuf(4096);
76
77 while(m_plaintext.empty() && !m_channel.is_closed())
78 {
79 const size_t from_socket = m_read(readbuf.data(), readbuf.size());
80 m_channel.received_data(readbuf.data(), from_socket);
81 }
82
83 const size_t returned = std::min(buf_len, m_plaintext.size());
84
85 for(size_t i = 0; i != returned; ++i)
86 buf[i] = m_plaintext[i];
87 m_plaintext.erase(m_plaintext.begin(), m_plaintext.begin() + returned);
88
89 BOTAN_ASSERT_IMPLICATION(returned == 0, m_channel.is_closed(),
90 "Only return zero if channel is closed");
91
92 return returned;
93 }
94
95}
96
97}
BOTAN_ASSERT_IMPLICATION
#define BOTAN_ASSERT_IMPLICATION(expr1, expr2, msg)
Definition: assert.h:94
Botan::Credentials_Manager
Definition: credentials_manager.h:31
Botan::RandomNumberGenerator
Definition: rng.h:26
Botan::TLS::Alert
Definition: tls_alert.h:22
Botan::TLS::Blocking_Client
Definition: tls_blocking.h:23
Botan::TLS::Blocking_Client::read_fn
std::function< size_t(uint8_t[], size_t)> read_fn
Definition: tls_blocking.h:29
Botan::TLS::Blocking_Client::write_fn
std::function< void(const uint8_t[], size_t)> write_fn
Definition: tls_blocking.h:30
Botan::TLS::Blocking_Client::Blocking_Client
Blocking_Client(read_fn reader, write_fn writer, Session_Manager &session_manager, Credentials_Manager &creds, const Policy &policy, RandomNumberGenerator &rng, const Server_Information &server_info=Server_Information(), const Protocol_Version &offer_version=Protocol_Version::latest_tls_version(), const std::vector< std::string > &next_protos={})
Definition: tls_blocking.cpp:15
Botan::TLS::Blocking_Client::read
size_t read(uint8_t buf[], size_t buf_len)
Definition: tls_blocking.cpp:73
Botan::TLS::Blocking_Client::do_handshake
void do_handshake()
Definition: tls_blocking.cpp:62
Botan::TLS::Blocking_Client::handshake_complete
virtual bool handshake_complete(const Session &)
Definition: tls_blocking.h:78
Botan::TLS::Blocking_Client::alert_notification
virtual void alert_notification(const Alert &)
Definition: tls_blocking.h:83
Botan::TLS::Channel::is_active
bool is_active() const
Definition: tls_channel.cpp:279
Botan::TLS::Channel::is_closed
bool is_closed() const
Definition: tls_channel.cpp:286
Botan::TLS::Channel::received_data
size_t received_data(const uint8_t buf[], size_t buf_size)
Definition: tls_channel.cpp:316
Botan::TLS::Compat_Callbacks
Definition: tls_callbacks.h:367
Botan::TLS::Policy
Definition: tls_policy.h:29
Botan::TLS::Protocol_Version
Definition: tls_version.h:22
Botan::TLS::Server_Information
Definition: tls_server_info.h:22
Botan::TLS::Session_Manager
Definition: tls_session_manager.h:30
Botan::TLS::Session
Definition: tls_session.h:28
Botan
Definition: alg_id.cpp:13
std
Definition: bigint.h:1155