Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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 using namespace std::placeholders;
16
17 Blocking_Client::Blocking_Client(read_fn reader,
18 write_fn writer,
19 Session_Manager& session_manager,
20 Credentials_Manager& creds,
21 const Policy& policy,
22 RandomNumberGenerator& rng,
23 const Server_Information& server_info,
24 const Protocol_Version& offer_version,
25 const std::vector<std::string>& next) :
26 m_read(reader),
27 m_callbacks(new TLS::Compat_Callbacks(
28 writer,
29 std::bind(&Blocking_Client::data_cb, this, _1, _2),
30 std::function<void (Alert)>(std::bind(&Blocking_Client::alert_cb, this, _1)),
31 std::bind(&Blocking_Client::handshake_cb, this, _1)
32 )),
33 m_channel(*m_callbacks.get(),
34 session_manager,
35 creds,
36 policy,
37 rng,
38 server_info,
39 offer_version,
40 next)
41 {
42 }
43
44 bool Blocking_Client::handshake_cb(const Session& session)
45 {
46 return this->handshake_complete(session);
47 }
48
49 void Blocking_Client::alert_cb(const Alert& alert)
50 {
51 this->alert_notification(alert);
52 }
53
54 void Blocking_Client::data_cb(const uint8_t data[], size_t data_len)
55 {
56 m_plaintext.insert(m_plaintext.end(), data, data + data_len);
57 }
58
59 void Blocking_Client::do_handshake()
60 {
61 std::vector<uint8_t> readbuf(4096);
62
63 while(!m_channel.is_closed() && !m_channel.is_active())
64 {
65 const size_t from_socket = m_read(readbuf.data(), readbuf.size());
66 m_channel.received_data(readbuf.data(), from_socket);
67 }
68 }
69
70 size_t Blocking_Client::read(uint8_t buf[], size_t buf_len)
71 {
72 std::vector<uint8_t> readbuf(4096);
73
74 while(m_plaintext.empty() && !m_channel.is_closed())
75 {
76 const size_t from_socket = m_read(readbuf.data(), readbuf.size());
77 m_channel.received_data(readbuf.data(), from_socket);
78 }
79
80 const size_t returned = std::min(buf_len, m_plaintext.size());
81
82 for(size_t i = 0; i != returned; ++i)
83 buf[i] = m_plaintext[i];
84 m_plaintext.erase(m_plaintext.begin(), m_plaintext.begin() + returned);
85
86 BOTAN_ASSERT_IMPLICATION(returned == 0, m_channel.is_closed(),
87 "Only return zero if channel is closed");
88
89 return returned;
90 }
91
92 }
93
94 }
placeholders
Botan::TLS::Session_Manager
Definition: tls_session_manager.h:29
Botan::TLS::Session
Definition: tls_session.h:27
Botan::TLS::Blocking_Client
Definition: tls_blocking.h:26
Botan::RandomNumberGenerator
Definition: rng.h:25
std
Definition: bigint.h:619
Botan::TLS::Blocking_Client::read_fn
std::function< size_t(uint8_t[], size_t)> read_fn
Definition: tls_blocking.h:33
Botan::TLS::Blocking_Client::alert_notification
virtual void alert_notification(const Alert &)
Definition: tls_blocking.h:87
Botan::TLS::Protocol_Version
Definition: tls_version.h:21
Botan::TLS::Channel::is_closed
bool is_closed() const
Definition: tls_channel.cpp:245
Botan::TLS::Blocking_Client::write_fn
std::function< void(const uint8_t[], size_t)> write_fn
Definition: tls_blocking.h:34
Botan::TLS::Server_Information
Definition: tls_server_info.h:21
Botan::TLS::Compat_Callbacks
Definition: tls_callbacks.h:202
Botan
Definition: alg_id.cpp:13
Botan::Credentials_Manager
Definition: credentials_manager.h:28
Botan::TLS::Blocking_Client::handshake_complete
virtual bool handshake_complete(const Session &)
Definition: tls_blocking.h:82
Botan::TLS::Channel::received_data
size_t received_data(const uint8_t buf[], size_t buf_size)
Definition: tls_channel.cpp:284
Botan::TLS::Policy
Definition: tls_policy.h:26
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:17
Botan::CT::min
T min(T a, T b)
Definition: ct_utils.h:180
BOTAN_ASSERT_IMPLICATION
#define BOTAN_ASSERT_IMPLICATION(expr1, expr2, msg)
Definition: assert.h:66
Botan::TLS::Blocking_Client::read
size_t read(uint8_t buf[], size_t buf_len)
Definition: tls_blocking.cpp:70
Botan::TLS::Alert
Definition: tls_alert.h:21
Botan::TLS::Blocking_Client::do_handshake
void do_handshake()
Definition: tls_blocking.cpp:59
Botan::TLS::Channel::is_active
bool is_active() const
Definition: tls_channel.cpp:240