forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadnl-ext-connection.cpp
192 lines (161 loc) · 5.06 KB
/
adnl-ext-connection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2017-2019 Telegram Systems LLP
*/
#include "adnl-ext-connection.hpp"
namespace ton {
namespace adnl {
void AdnlExtConnection::send_uninit(td::BufferSlice data) {
buffered_fd_.output_buffer().append(std::move(data));
loop();
}
void AdnlExtConnection::send(td::BufferSlice data) {
LOG(DEBUG) << "sending packet of size " << data.size();
auto data_size = td::narrow_cast<td::uint32>(data.size()) + 32 + 32;
if (data_size < 32 || data_size > (1 << 24)) {
LOG(WARNING) << "bad packet size " << data_size;
return;
}
td::BufferSlice d{data.size() + 4 + 32 + 32};
auto S = d.as_slice();
S.copy_from(td::Slice(reinterpret_cast<const td::uint8 *>(&data_size), 4));
S.remove_prefix(4);
auto Sc = S;
td::Random::secure_bytes(S.copy().truncate(32));
S.remove_prefix(32);
S.copy_from(data.as_slice());
S.remove_prefix(data.size());
td::sha256(Sc.truncate(32 + data.size()), S);
td::BufferSlice e{d.size()};
out_ctr_.encrypt(d.as_slice(), e.as_slice());
buffered_fd_.output_buffer().append(std::move(e));
loop();
}
td::Status AdnlExtConnection::receive(td::ChainBufferReader &input, bool &exit_loop) {
if (stop_read_) {
exit_loop = true;
return td::Status::OK();
}
if (input.size() > 0) {
received_bytes_ = 1;
}
if (inited_) {
if (!read_len_) {
if (input.size() < 4) {
exit_loop = true;
return td::Status::OK();
}
char x[4];
td::MutableSlice s{x, 4};
input.advance(4, s);
td::MutableSlice e{reinterpret_cast<td::uint8 *>(&len_), 4};
in_ctr_.encrypt(s, e);
LOG(DEBUG) << "len=" << len_;
if (len_ > (1 << 24) || len_ < 32) {
return td::Status::Error("Too big packet");
}
read_len_ = true;
}
if (input.size() < len_) {
exit_loop = true;
return td::Status::OK();
}
auto data = input.cut_head(len_).move_as_buffer_slice();
update_timer();
td::BufferSlice dec_data{data.size()};
in_ctr_.encrypt(data.as_slice(), dec_data.as_slice());
exit_loop = false;
read_len_ = false;
len_ = 0;
return receive_packet(std::move(dec_data));
} else {
if (input.size() < 256) {
exit_loop = true;
return td::Status::OK();
}
auto data = input.cut_head(256).move_as_buffer_slice();
update_timer();
exit_loop = false;
return process_init_packet(std::move(data));
}
}
void AdnlExtConnection::loop() {
auto status = [&] {
TRY_STATUS(buffered_fd_.flush_read());
auto &input = buffered_fd_.input_buffer();
bool exit_loop = false;
while (!exit_loop) {
TRY_STATUS(receive(input, exit_loop));
}
TRY_STATUS(buffered_fd_.flush_write());
if (td::can_close(buffered_fd_)) {
stop();
}
return td::Status::OK();
}();
if (status.is_error()) {
LOG(ERROR) << "Client got error " << status;
stop();
} else {
send_ready();
}
}
td::Status AdnlExtConnection::init_crypto(td::Slice S) {
if (S.size() < 96) {
return td::Status::Error(ErrorCode::protoviolation, "too small enc data");
}
CHECK(S.size() >= 96);
td::SecureString s1(32), s2(32);
td::SecureString v1(16), v2(16);
s1.as_mutable_slice().copy_from(S.copy().truncate(32));
S.remove_prefix(32);
s2.as_mutable_slice().copy_from(S.copy().truncate(32));
S.remove_prefix(32);
v1.as_mutable_slice().copy_from(S.copy().truncate(16));
S.remove_prefix(16);
v2.as_mutable_slice().copy_from(S.copy().truncate(16));
S.remove_prefix(16);
if (is_client_) {
in_ctr_.init(s1, v1);
out_ctr_.init(s2, v2);
} else {
in_ctr_.init(s2, v2);
out_ctr_.init(s1, v1);
}
inited_ = true;
return td::Status::OK();
}
td::Status AdnlExtConnection::receive_packet(td::BufferSlice data) {
LOG(DEBUG) << "received packet of size " << data.size();
auto S = data.as_slice();
S.truncate(data.size() - 32);
auto D = data.as_slice();
D.remove_prefix(data.size() - 32);
if (td::sha256(S) != D) {
return td::Status::Error(ErrorCode::protoviolation, "sha256 mismatch");
}
data.truncate(data.size() - 32);
data.confirm_read(32);
if (data.size() == 0) {
// keepalive
return td::Status::OK();
}
bool processed = false;
TRY_STATUS(process_custom_packet(data, processed));
if (processed) {
return td::Status::OK();
}
return process_packet(std::move(data));
}
} // namespace adnl
} // namespace ton