2019-07-09 04:01:12 +02:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2019-07-09 04:01:12 +02:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/mtproto/ProxySecret.h"
|
|
|
|
|
|
|
|
#include "td/utils/base64.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace mtproto {
|
|
|
|
|
2020-01-25 23:22:07 +01:00
|
|
|
Result<ProxySecret> ProxySecret::from_link(Slice encoded_secret, bool truncate_if_needed) {
|
2019-07-09 04:01:12 +02:00
|
|
|
auto r_decoded = hex_decode(encoded_secret);
|
|
|
|
if (r_decoded.is_error()) {
|
|
|
|
r_decoded = base64url_decode(encoded_secret);
|
|
|
|
}
|
2024-01-29 13:48:57 +01:00
|
|
|
if (r_decoded.is_error()) {
|
|
|
|
r_decoded = base64_decode(encoded_secret);
|
|
|
|
}
|
2019-07-09 04:01:12 +02:00
|
|
|
if (r_decoded.is_error()) {
|
|
|
|
return Status::Error(400, "Wrong proxy secret");
|
|
|
|
}
|
2020-01-25 23:22:07 +01:00
|
|
|
return from_binary(r_decoded.ok(), truncate_if_needed);
|
2019-07-09 04:01:12 +02:00
|
|
|
}
|
|
|
|
|
2020-01-25 23:22:07 +01:00
|
|
|
Result<ProxySecret> ProxySecret::from_binary(Slice raw_unchecked_secret, bool truncate_if_needed) {
|
|
|
|
if (raw_unchecked_secret.size() > 17 + MAX_DOMAIN_LENGTH) {
|
|
|
|
if (truncate_if_needed) {
|
|
|
|
raw_unchecked_secret.truncate(17 + MAX_DOMAIN_LENGTH);
|
|
|
|
} else {
|
|
|
|
return Status::Error(400, "Too long secret");
|
|
|
|
}
|
2020-01-24 16:33:17 +01:00
|
|
|
}
|
2019-07-09 04:01:12 +02:00
|
|
|
if (raw_unchecked_secret.size() == 16 ||
|
|
|
|
(raw_unchecked_secret.size() == 17 && static_cast<unsigned char>(raw_unchecked_secret[0]) == 0xdd) ||
|
2019-07-11 22:42:59 +02:00
|
|
|
(raw_unchecked_secret.size() >= 18 && static_cast<unsigned char>(raw_unchecked_secret[0]) == 0xee)) {
|
2019-07-09 04:01:12 +02:00
|
|
|
return from_raw(raw_unchecked_secret);
|
|
|
|
}
|
|
|
|
if (raw_unchecked_secret.size() < 16) {
|
|
|
|
return Status::Error(400, "Wrong proxy secret");
|
|
|
|
}
|
|
|
|
return Status::Error(400, "Unsupported proxy secret");
|
|
|
|
}
|
|
|
|
|
|
|
|
string ProxySecret::get_encoded_secret() const {
|
|
|
|
if (emulate_tls()) {
|
|
|
|
return base64url_encode(secret_);
|
|
|
|
}
|
2019-09-02 15:50:19 +02:00
|
|
|
return hex_encode(secret_);
|
2019-07-09 04:01:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mtproto
|
|
|
|
} // namespace td
|