2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-12-31 20:04:05 +01: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/telegram/net/PublicRsaKeyShared.h"
|
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2019-04-21 01:24:51 +02:00
|
|
|
#include "td/utils/format.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
2018-02-03 13:58:18 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
2018-02-03 13:58:18 +01:00
|
|
|
|
2019-01-02 00:43:37 +01:00
|
|
|
PublicRsaKeyShared::PublicRsaKeyShared(DcId dc_id, bool is_test) : dc_id_(dc_id) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!dc_id_.is_empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto add_pem = [this](CSlice pem) {
|
2021-07-05 20:04:23 +02:00
|
|
|
auto r_rsa = mtproto::RSA::from_pem_public_key(pem);
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(r_rsa.is_ok()) << r_rsa.error() << " " << pem;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (r_rsa.is_ok()) {
|
2021-07-05 21:09:45 +02:00
|
|
|
add_rsa(r_rsa.move_as_ok());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
2019-01-02 00:43:37 +01:00
|
|
|
|
|
|
|
if (is_test) {
|
|
|
|
add_pem(
|
|
|
|
"-----BEGIN RSA PUBLIC KEY-----\n"
|
2021-07-08 15:39:34 +02:00
|
|
|
"MIIBCgKCAQEAyMEdY1aR+sCR3ZSJrtztKTKqigvO/vBfqACJLZtS7QMgCGXJ6XIR\n"
|
|
|
|
"yy7mx66W0/sOFa7/1mAZtEoIokDP3ShoqF4fVNb6XeqgQfaUHd8wJpDWHcR2OFwv\n"
|
|
|
|
"plUUI1PLTktZ9uW2WE23b+ixNwJjJGwBDJPQEQFBE+vfmH0JP503wr5INS1poWg/\n"
|
|
|
|
"j25sIWeYPHYeOrFp/eXaqhISP6G+q2IeTaWTXpwZj4LzXq5YOpk4bYEQ6mvRq7D1\n"
|
|
|
|
"aHWfYmlEGepfaYR8Q0YqvvhYtMte3ITnuSJs171+GDqpdKcSwHnd6FudwGO4pcCO\n"
|
|
|
|
"j4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB\n"
|
2019-01-02 00:43:37 +01:00
|
|
|
"-----END RSA PUBLIC KEY-----");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
add_pem(
|
|
|
|
"-----BEGIN RSA PUBLIC KEY-----\n"
|
2021-07-08 15:39:34 +02:00
|
|
|
"MIIBCgKCAQEA6LszBcC1LGzyr992NzE0ieY+BSaOW622Aa9Bd4ZHLl+TuFQ4lo4g\n"
|
|
|
|
"5nKaMBwK/BIb9xUfg0Q29/2mgIR6Zr9krM7HjuIcCzFvDtr+L0GQjae9H0pRB2OO\n"
|
|
|
|
"62cECs5HKhT5DZ98K33vmWiLowc621dQuwKWSQKjWf50XYFw42h21P2KXUGyp2y/\n"
|
|
|
|
"+aEyZ+uVgLLQbRA1dEjSDZ2iGRy12Mk5gpYc397aYp438fsJoHIgJ2lgMv5h7WY9\n"
|
|
|
|
"t6N/byY9Nw9p21Og3AoXSL2q/2IJ1WRUhebgAdGVMlV1fkuOQoEzR7EdpqtQD9Cs\n"
|
|
|
|
"5+bfo3Nhmcyvk5ftB0WkJ9z6bNZ7yxrP8wIDAQAB\n"
|
2018-12-31 20:04:05 +01:00
|
|
|
"-----END RSA PUBLIC KEY-----");
|
|
|
|
}
|
|
|
|
|
2021-07-05 20:04:23 +02:00
|
|
|
void PublicRsaKeyShared::add_rsa(mtproto::RSA rsa) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto lock = rw_mutex_.lock_write();
|
|
|
|
auto fingerprint = rsa.get_fingerprint();
|
2021-07-05 21:19:59 +02:00
|
|
|
if (get_rsa_key_unsafe(fingerprint) != nullptr) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-07-05 21:19:59 +02:00
|
|
|
keys_.push_back(RsaKey{std::move(rsa), fingerprint});
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-05 21:09:45 +02:00
|
|
|
Result<mtproto::PublicRsaKeyInterface::RsaKey> PublicRsaKeyShared::get_rsa_key(const vector<int64> &fingerprints) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto lock = rw_mutex_.lock_read();
|
|
|
|
for (auto fingerprint : fingerprints) {
|
2021-07-05 21:19:59 +02:00
|
|
|
auto *rsa_key = get_rsa_key_unsafe(fingerprint);
|
|
|
|
if (rsa_key != nullptr) {
|
|
|
|
return RsaKey{rsa_key->rsa.clone(), fingerprint};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-21 01:24:51 +02:00
|
|
|
return Status::Error(PSLICE() << "Unknown fingerprints " << format::as_array(fingerprints));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PublicRsaKeyShared::drop_keys() {
|
|
|
|
if (dc_id_.is_empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto lock = rw_mutex_.lock_write();
|
2021-07-05 21:19:59 +02:00
|
|
|
keys_.clear();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PublicRsaKeyShared::has_keys() {
|
|
|
|
auto lock = rw_mutex_.lock_read();
|
2021-07-05 21:19:59 +02:00
|
|
|
return !keys_.empty();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
void PublicRsaKeyShared::add_listener(unique_ptr<Listener> listener) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (listener->notify()) {
|
|
|
|
auto lock = rw_mutex_.lock_write();
|
|
|
|
listeners_.push_back(std::move(listener));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-05 21:19:59 +02:00
|
|
|
mtproto::PublicRsaKeyInterface::RsaKey *PublicRsaKeyShared::get_rsa_key_unsafe(int64 fingerprint) {
|
|
|
|
auto it = std::find_if(keys_.begin(), keys_.end(),
|
|
|
|
[fingerprint](const auto &value) { return value.fingerprint == fingerprint; });
|
|
|
|
if (it == keys_.end()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
2021-07-05 21:19:59 +02:00
|
|
|
return &*it;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PublicRsaKeyShared::notify() {
|
|
|
|
auto lock = rw_mutex_.lock_read();
|
2019-10-21 15:25:56 +02:00
|
|
|
td::remove_if(listeners_, [&](auto &listener) { return !listener->notify(); });
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-02-03 13:58:18 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|