Minor fixes.

GitOrigin-RevId: 619b0fa38647168dd44d5c2f99729ecb61d4f11f
This commit is contained in:
levlam 2018-04-19 16:43:09 +03:00
parent d80148afae
commit 2b19d388b9
4 changed files with 35 additions and 39 deletions

View File

@ -78,7 +78,7 @@ void GetSecureValue::loop() {
} }
auto *file_manager = G()->td().get_actor_unsafe()->file_manager_.get(); auto *file_manager = G()->td().get_actor_unsafe()->file_manager_.get();
auto r_secure_value = decrypt_encrypted_secure_value(file_manager, *secret_, *encrypted_secure_value_); auto r_secure_value = decrypt_secure_value(file_manager, *secret_, *encrypted_secure_value_);
if (r_secure_value.is_error()) { if (r_secure_value.is_error()) {
return on_error(r_secure_value.move_as_error()); return on_error(r_secure_value.move_as_error());
} }
@ -144,7 +144,7 @@ void GetAllSecureValues::loop() {
} }
auto *file_manager = G()->td().get_actor_unsafe()->file_manager_.get(); auto *file_manager = G()->td().get_actor_unsafe()->file_manager_.get();
auto r_secure_values = decrypt_encrypted_secure_values(file_manager, *secret_, *encrypted_secure_values_); auto r_secure_values = decrypt_secure_values(file_manager, *secret_, *encrypted_secure_values_);
if (r_secure_values.is_error()) { if (r_secure_values.is_error()) {
return on_error(r_secure_values.move_as_error()); return on_error(r_secure_values.move_as_error());
} }
@ -329,7 +329,7 @@ void SetSecureValue::on_result(NetQueryPtr query) {
if (secure_value_.selfie.is_valid()) { if (secure_value_.selfie.is_valid()) {
merge(file_manager, secure_value_.selfie, encrypted_secure_value.selfie); merge(file_manager, secure_value_.selfie, encrypted_secure_value.selfie);
} }
auto r_secure_value = decrypt_encrypted_secure_value(file_manager, *secret_, encrypted_secure_value); auto r_secure_value = decrypt_secure_value(file_manager, *secret_, encrypted_secure_value);
if (r_secure_value.is_error()) { if (r_secure_value.is_error()) {
return on_error(r_secure_value.move_as_error()); return on_error(r_secure_value.move_as_error());
} }
@ -431,8 +431,8 @@ class GetPassportAuthorizationForm : public NetQueryCallback {
continue; continue;
} }
auto r_secure_value = decrypt_encrypted_secure_value( auto r_secure_value =
file_manager, *secret_, get_encrypted_secure_value(file_manager, std::move(value))); decrypt_secure_value(file_manager, *secret_, get_encrypted_secure_value(file_manager, std::move(value)));
value = nullptr; value = nullptr;
if (r_secure_value.is_error()) { if (r_secure_value.is_error()) {
LOG(ERROR) << "Failed to decrypt secure value: " << r_secure_value.error(); LOG(ERROR) << "Failed to decrypt secure value: " << r_secure_value.error();

View File

@ -247,7 +247,8 @@ string get_secure_value_data_field_name(SecureValueType type, string field_name)
} }
bool operator==(const EncryptedSecureFile &lhs, const EncryptedSecureFile &rhs) { bool operator==(const EncryptedSecureFile &lhs, const EncryptedSecureFile &rhs) {
return lhs.file_id == rhs.file_id && lhs.file_hash == rhs.file_hash && lhs.encrypted_secret == rhs.encrypted_secret; return lhs.file_id == rhs.file_id && lhs.date == rhs.date && lhs.file_hash == rhs.file_hash &&
lhs.encrypted_secret == rhs.encrypted_secret;
} }
bool operator!=(const EncryptedSecureFile &lhs, const EncryptedSecureFile &rhs) { bool operator!=(const EncryptedSecureFile &lhs, const EncryptedSecureFile &rhs) {
@ -271,6 +272,11 @@ EncryptedSecureFile get_encrypted_secure_file(FileManager *file_manager,
result.file_id = file_manager->register_remote( result.file_id = file_manager->register_remote(
FullRemoteFileLocation(FileType::Secure, secure_file->id_, secure_file->access_hash_, DcId::internal(dc_id)), FullRemoteFileLocation(FileType::Secure, secure_file->id_, secure_file->access_hash_, DcId::internal(dc_id)),
FileLocationSource::FromServer, {}, 0, 0, ""); FileLocationSource::FromServer, {}, 0, 0, "");
result.date = secure_file->date_;
if (result.date < 0) {
LOG(ERROR) << "Receive wrong date " << result.date;
result.date = 0;
}
result.encrypted_secret = secure_file->secret_.as_slice().str(); result.encrypted_secret = secure_file->secret_.as_slice().str();
result.file_hash = secure_file->file_hash_.as_slice().str(); result.file_hash = secure_file->file_hash_.as_slice().str();
break; break;
@ -865,7 +871,7 @@ td_api::object_ptr<td_api::allPassportData> get_all_passport_data_object(FileMan
return td_api::make_object<td_api::allPassportData>(std::move(result)); return td_api::make_object<td_api::allPassportData>(std::move(result));
} }
Result<std::pair<FileId, SecureFileCredentials>> decrypt_secure_file(FileManager *file_manager, static Result<std::pair<FileId, SecureFileCredentials>> decrypt_secure_file(FileManager *file_manager,
const secure_storage::Secret &master_secret, const secure_storage::Secret &master_secret,
const EncryptedSecureFile &secure_file) { const EncryptedSecureFile &secure_file) {
if (!secure_file.file_id.is_valid()) { if (!secure_file.file_id.is_valid()) {
@ -880,7 +886,7 @@ Result<std::pair<FileId, SecureFileCredentials>> decrypt_secure_file(FileManager
return std::make_pair(secure_file.file_id, SecureFileCredentials{secret.as_slice().str(), hash.as_slice().str()}); return std::make_pair(secure_file.file_id, SecureFileCredentials{secret.as_slice().str(), hash.as_slice().str()});
} }
Result<std::pair<vector<FileId>, vector<SecureFileCredentials>>> decrypt_secure_files( static Result<std::pair<vector<FileId>, vector<SecureFileCredentials>>> decrypt_secure_files(
FileManager *file_manager, const secure_storage::Secret &secret, const vector<EncryptedSecureFile> &secure_files) { FileManager *file_manager, const secure_storage::Secret &secret, const vector<EncryptedSecureFile> &secure_files) {
vector<FileId> res; vector<FileId> res;
vector<SecureFileCredentials> credentials; vector<SecureFileCredentials> credentials;
@ -894,7 +900,7 @@ Result<std::pair<vector<FileId>, vector<SecureFileCredentials>>> decrypt_secure_
return std::make_pair(std::move(res), std::move(credentials)); return std::make_pair(std::move(res), std::move(credentials));
} }
Result<std::pair<string, SecureDataCredentials>> decrypt_secure_data(const secure_storage::Secret &master_secret, static Result<std::pair<string, SecureDataCredentials>> decrypt_secure_data(const secure_storage::Secret &master_secret,
const EncryptedSecureData &secure_data) { const EncryptedSecureData &secure_data) {
TRY_RESULT(hash, secure_storage::ValueHash::create(secure_data.hash)); TRY_RESULT(hash, secure_storage::ValueHash::create(secure_data.hash));
TRY_RESULT(encrypted_secret, secure_storage::EncryptedSecret::create(secure_data.encrypted_secret)); TRY_RESULT(encrypted_secret, secure_storage::EncryptedSecret::create(secure_data.encrypted_secret));
@ -903,8 +909,7 @@ Result<std::pair<string, SecureDataCredentials>> decrypt_secure_data(const secur
return std::make_pair(value.as_slice().str(), SecureDataCredentials{secret.as_slice().str(), hash.as_slice().str()}); return std::make_pair(value.as_slice().str(), SecureDataCredentials{secret.as_slice().str(), hash.as_slice().str()});
} }
Result<SecureValueWithCredentials> decrypt_encrypted_secure_value(FileManager *file_manager, Result<SecureValueWithCredentials> decrypt_secure_value(FileManager *file_manager, const secure_storage::Secret &secret,
const secure_storage::Secret &secret,
const EncryptedSecureValue &encrypted_secure_value) { const EncryptedSecureValue &encrypted_secure_value) {
SecureValue res; SecureValue res;
SecureValueCredentials res_credentials; SecureValueCredentials res_credentials;
@ -936,13 +941,13 @@ Result<SecureValueWithCredentials> decrypt_encrypted_secure_value(FileManager *f
return SecureValueWithCredentials{std::move(res), std::move(res_credentials)}; return SecureValueWithCredentials{std::move(res), std::move(res_credentials)};
} }
Result<vector<SecureValueWithCredentials>> decrypt_encrypted_secure_values( Result<vector<SecureValueWithCredentials>> decrypt_secure_values(
FileManager *file_manager, const secure_storage::Secret &secret, FileManager *file_manager, const secure_storage::Secret &secret,
const vector<EncryptedSecureValue> &encrypted_secure_values) { const vector<EncryptedSecureValue> &encrypted_secure_values) {
vector<SecureValueWithCredentials> result; vector<SecureValueWithCredentials> result;
result.reserve(encrypted_secure_values.size()); result.reserve(encrypted_secure_values.size());
for (auto &encrypted_secure_value : encrypted_secure_values) { for (auto &encrypted_secure_value : encrypted_secure_values) {
auto r_secure_value_with_credentials = decrypt_encrypted_secure_value(file_manager, secret, encrypted_secure_value); auto r_secure_value_with_credentials = decrypt_secure_value(file_manager, secret, encrypted_secure_value);
if (r_secure_value_with_credentials.is_ok()) { if (r_secure_value_with_credentials.is_ok()) {
result.push_back(r_secure_value_with_credentials.move_as_ok()); result.push_back(r_secure_value_with_credentials.move_as_ok());
} else { } else {
@ -952,7 +957,7 @@ Result<vector<SecureValueWithCredentials>> decrypt_encrypted_secure_values(
return std::move(result); return std::move(result);
} }
EncryptedSecureFile encrypt_secure_file(FileManager *file_manager, const secure_storage::Secret &master_secret, static EncryptedSecureFile encrypt_secure_file(FileManager *file_manager, const secure_storage::Secret &master_secret,
FileId file, string &to_hash) { FileId file, string &to_hash) {
auto file_view = file_manager->get_file_view(file); auto file_view = file_manager->get_file_view(file);
if (file_view.empty()) { if (file_view.empty()) {
@ -978,14 +983,15 @@ EncryptedSecureFile encrypt_secure_file(FileManager *file_manager, const secure_
return res; return res;
} }
vector<EncryptedSecureFile> encrypt_secure_files(FileManager *file_manager, const secure_storage::Secret &master_secret, static vector<EncryptedSecureFile> encrypt_secure_files(FileManager *file_manager,
const secure_storage::Secret &master_secret,
vector<FileId> files, string &to_hash) { vector<FileId> files, string &to_hash) {
return transform(files, return transform(files,
[&](auto file_id) { return encrypt_secure_file(file_manager, master_secret, file_id, to_hash); }); [&](auto file_id) { return encrypt_secure_file(file_manager, master_secret, file_id, to_hash); });
} }
EncryptedSecureData encrypt_secure_data(const secure_storage::Secret &master_secret, Slice data, string &to_hash) { static EncryptedSecureData encrypt_secure_data(const secure_storage::Secret &master_secret, Slice data,
namespace ss = secure_storage; string &to_hash) {
auto secret = secure_storage::Secret::create_new(); auto secret = secure_storage::Secret::create_new();
auto encrypted = encrypt_value(secret, data).move_as_ok(); auto encrypted = encrypt_value(secret, data).move_as_ok();
EncryptedSecureData res; EncryptedSecureData res;

View File

@ -57,6 +57,7 @@ string get_secure_value_data_field_name(SecureValueType type, string field_name)
struct EncryptedSecureFile { struct EncryptedSecureFile {
FileId file_id; FileId file_id;
int32 date = 0;
string file_hash; string file_hash;
string encrypted_secret; string encrypted_secret;
}; };
@ -184,25 +185,12 @@ Result<td_api::object_ptr<td_api::PassportData>> get_passport_data_object(FileMa
td_api::object_ptr<td_api::allPassportData> get_all_passport_data_object(FileManager *file_manager, td_api::object_ptr<td_api::allPassportData> get_all_passport_data_object(FileManager *file_manager,
const vector<SecureValue> &values); const vector<SecureValue> &values);
Result<std::pair<FileId, SecureFileCredentials>> decrypt_secure_file(FileManager *file_manager, Result<SecureValueWithCredentials> decrypt_secure_value(FileManager *file_manager, const secure_storage::Secret &secret,
const secure_storage::Secret &secret,
const EncryptedSecureFile &secure_file);
Result<std::pair<vector<FileId>, vector<SecureFileCredentials>>> decrypt_secure_files(
FileManager *file_manager, const secure_storage::Secret &secret, const vector<EncryptedSecureFile> &secure_file);
Result<std::pair<string, SecureDataCredentials>> decrypt_secure_data(const secure_storage::Secret &secret,
const EncryptedSecureData &secure_data);
Result<SecureValueWithCredentials> decrypt_encrypted_secure_value(FileManager *file_manager,
const secure_storage::Secret &secret,
const EncryptedSecureValue &encrypted_secure_value); const EncryptedSecureValue &encrypted_secure_value);
Result<vector<SecureValueWithCredentials>> decrypt_encrypted_secure_values( Result<vector<SecureValueWithCredentials>> decrypt_secure_values(
FileManager *file_manager, const secure_storage::Secret &secret, FileManager *file_manager, const secure_storage::Secret &secret,
const vector<EncryptedSecureValue> &encrypted_secure_values); const vector<EncryptedSecureValue> &encrypted_secure_values);
EncryptedSecureFile encrypt_secure_file(FileManager *file_manager, const secure_storage::Secret &master_secret,
FileId file, string &to_hash);
vector<EncryptedSecureFile> encrypt_secure_files(FileManager *file_manager, const secure_storage::Secret &master_secret,
vector<FileId> files, string &to_hash);
EncryptedSecureData encrypt_secure_data(const secure_storage::Secret &master_secret, Slice data, string &to_hash);
EncryptedSecureValue encrypt_secure_value(FileManager *file_manager, const secure_storage::Secret &master_secret, EncryptedSecureValue encrypt_secure_value(FileManager *file_manager, const secure_storage::Secret &master_secret,
const SecureValue &secure_value); const SecureValue &secure_value);

View File

@ -17,6 +17,7 @@ namespace td {
template <class StorerT> template <class StorerT>
void store(EncryptedSecureFile file, StorerT &storer) { void store(EncryptedSecureFile file, StorerT &storer) {
store(file.file_id, storer); store(file.file_id, storer);
store(file.date, storer);
store(file.file_hash, storer); store(file.file_hash, storer);
store(file.encrypted_secret, storer); store(file.encrypted_secret, storer);
} }
@ -24,6 +25,7 @@ void store(EncryptedSecureFile file, StorerT &storer) {
template <class ParserT> template <class ParserT>
void parse(EncryptedSecureFile &file, ParserT &parser) { void parse(EncryptedSecureFile &file, ParserT &parser) {
parse(file.file_id, parser); parse(file.file_id, parser);
parse(file.date, parser);
parse(file.file_hash, parser); parse(file.file_hash, parser);
parse(file.encrypted_secret, parser); parse(file.encrypted_secret, parser);
} }