Ton

Форк
0
/
adnl-ext-connection.cpp 
192 строки · 5.1 Кб
1
/*
2
    This file is part of TON Blockchain Library.
3

4
    TON Blockchain Library is free software: you can redistribute it and/or modify
5
    it under the terms of the GNU Lesser General Public License as published by
6
    the Free Software Foundation, either version 2 of the License, or
7
    (at your option) any later version.
8

9
    TON Blockchain Library is distributed in the hope that it will be useful,
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
    GNU Lesser General Public License for more details.
13

14
    You should have received a copy of the GNU Lesser General Public License
15
    along with TON Blockchain Library.  If not, see <http://www.gnu.org/licenses/>.
16

17
    Copyright 2017-2020 Telegram Systems LLP
18
*/
19
#include "adnl-ext-connection.hpp"
20

21
namespace ton {
22

23
namespace adnl {
24

25
void AdnlExtConnection::send_uninit(td::BufferSlice data) {
26
  buffered_fd_.output_buffer().append(std::move(data));
27
  loop();
28
}
29

30
void AdnlExtConnection::send(td::BufferSlice data) {
31
  LOG(DEBUG) << "sending packet of size " << data.size();
32
  auto data_size = td::narrow_cast<td::uint32>(data.size()) + 32 + 32;
33
  if (data_size < 32 || data_size > (1 << 24)) {
34
    LOG(WARNING) << "bad packet size " << data_size;
35
    return;
36
  }
37

38
  td::BufferSlice d{data.size() + 4 + 32 + 32};
39
  auto S = d.as_slice();
40

41
  S.copy_from(td::Slice(reinterpret_cast<const td::uint8 *>(&data_size), 4));
42
  S.remove_prefix(4);
43
  auto Sc = S;
44
  td::Random::secure_bytes(S.copy().truncate(32));
45
  S.remove_prefix(32);
46
  S.copy_from(data.as_slice());
47
  S.remove_prefix(data.size());
48

49
  td::sha256(Sc.truncate(32 + data.size()), S);
50

51
  td::BufferSlice e{d.size()};
52

53
  out_ctr_.encrypt(d.as_slice(), e.as_slice());
54

55
  buffered_fd_.output_buffer().append(std::move(e));
56
  loop();
57
}
58

59
td::Status AdnlExtConnection::receive(td::ChainBufferReader &input, bool &exit_loop) {
60
  if (stop_read_) {
61
    exit_loop = true;
62
    return td::Status::OK();
63
  }
64
  if (input.size() > 0) {
65
    received_bytes_ = 1;
66
  }
67
  if (inited_) {
68
    if (!read_len_) {
69
      if (input.size() < 4) {
70
        exit_loop = true;
71
        return td::Status::OK();
72
      }
73

74
      char x[4];
75
      td::MutableSlice s{x, 4};
76
      input.advance(4, s);
77

78
      td::MutableSlice e{reinterpret_cast<td::uint8 *>(&len_), 4};
79
      in_ctr_.encrypt(s, e);
80
      LOG(DEBUG) << "len=" << len_;
81
      if (len_ > (1 << 24) || len_ < 32) {
82
        return td::Status::Error("Too big packet");
83
      }
84
      read_len_ = true;
85
    }
86
    if (input.size() < len_) {
87
      exit_loop = true;
88
      return td::Status::OK();
89
    }
90
    auto data = input.cut_head(len_).move_as_buffer_slice();
91
    update_timer();
92

93
    td::BufferSlice dec_data{data.size()};
94
    in_ctr_.encrypt(data.as_slice(), dec_data.as_slice());
95

96
    exit_loop = false;
97
    read_len_ = false;
98
    len_ = 0;
99
    return receive_packet(std::move(dec_data));
100
  } else {
101
    if (input.size() < 256) {
102
      exit_loop = true;
103
      return td::Status::OK();
104
    }
105

106
    auto data = input.cut_head(256).move_as_buffer_slice();
107
    update_timer();
108

109
    exit_loop = false;
110
    return process_init_packet(std::move(data));
111
  }
112
}
113

114
void AdnlExtConnection::loop() {
115
  auto status = [&] {
116
    TRY_STATUS(buffered_fd_.flush_read());
117
    auto &input = buffered_fd_.input_buffer();
118
    bool exit_loop = false;
119
    while (!exit_loop) {
120
      TRY_STATUS(receive(input, exit_loop));
121
    }
122
    TRY_STATUS(buffered_fd_.flush_write());
123
    if (td::can_close(buffered_fd_)) {
124
      stop();
125
    }
126
    return td::Status::OK();
127
  }();
128
  if (status.is_error()) {
129
    LOG(ERROR) << "Client got error " << status;
130
    stop();
131
  } else {
132
    send_ready();
133
  }
134
}
135

136
td::Status AdnlExtConnection::init_crypto(td::Slice S) {
137
  if (S.size() < 96) {
138
    return td::Status::Error(ErrorCode::protoviolation, "too small enc data");
139
  }
140
  CHECK(S.size() >= 96);
141
  td::SecureString s1(32), s2(32);
142
  td::SecureString v1(16), v2(16);
143
  s1.as_mutable_slice().copy_from(S.copy().truncate(32));
144
  S.remove_prefix(32);
145
  s2.as_mutable_slice().copy_from(S.copy().truncate(32));
146
  S.remove_prefix(32);
147
  v1.as_mutable_slice().copy_from(S.copy().truncate(16));
148
  S.remove_prefix(16);
149
  v2.as_mutable_slice().copy_from(S.copy().truncate(16));
150
  S.remove_prefix(16);
151
  if (is_client_) {
152
    in_ctr_.init(s1, v1);
153
    out_ctr_.init(s2, v2);
154
  } else {
155
    in_ctr_.init(s2, v2);
156
    out_ctr_.init(s1, v1);
157
  }
158
  inited_ = true;
159
  return td::Status::OK();
160
}
161

162
td::Status AdnlExtConnection::receive_packet(td::BufferSlice data) {
163
  LOG(DEBUG) << "received packet of size " << data.size();
164
  auto S = data.as_slice();
165
  S.truncate(data.size() - 32);
166
  auto D = data.as_slice();
167
  D.remove_prefix(data.size() - 32);
168

169
  if (td::sha256(S) != D) {
170
    return td::Status::Error(ErrorCode::protoviolation, "sha256 mismatch");
171
  }
172

173
  data.truncate(data.size() - 32);
174
  data.confirm_read(32);
175

176
  if (data.size() == 0) {
177
    // keepalive
178
    return td::Status::OK();
179
  }
180

181
  bool processed = false;
182
  TRY_STATUS(process_custom_packet(data, processed));
183
  if (processed) {
184
    return td::Status::OK();
185
  }
186

187
  return process_packet(std::move(data));
188
}
189

190
}  // namespace adnl
191

192
}  // namespace ton
193

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.