2018-03-27 15:11:15 +02:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
|
|
|
|
|
|
|
#include "td/telegram/files/FileId.h"
|
|
|
|
#include "td/telegram/SecureStorage.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2018-04-05 15:06:03 +02:00
|
|
|
#include "td/utils/optional.h"
|
2018-04-07 01:38:28 +02:00
|
|
|
#include "td/utils/Slice.h"
|
2018-03-27 15:11:15 +02:00
|
|
|
#include "td/utils/Status.h"
|
2018-04-28 10:56:10 +02:00
|
|
|
#include "td/utils/StringBuilder.h"
|
2018-03-27 15:11:15 +02:00
|
|
|
|
2018-04-07 01:38:28 +02:00
|
|
|
#include <utility>
|
|
|
|
|
2018-03-27 15:11:15 +02:00
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class FileManager;
|
|
|
|
|
2018-04-18 16:28:48 +02:00
|
|
|
enum class SecureValueType : int32 {
|
2018-03-27 15:11:15 +02:00
|
|
|
None,
|
|
|
|
PersonalDetails,
|
|
|
|
Passport,
|
|
|
|
DriverLicense,
|
|
|
|
IdentityCard,
|
2018-04-27 14:38:02 +02:00
|
|
|
InternalPassport,
|
2018-03-27 15:11:15 +02:00
|
|
|
Address,
|
|
|
|
UtilityBill,
|
|
|
|
BankStatement,
|
|
|
|
RentalAgreement,
|
2018-04-27 14:38:02 +02:00
|
|
|
PassportRegistration,
|
|
|
|
TemporaryRegistration,
|
2018-03-27 15:11:15 +02:00
|
|
|
PhoneNumber,
|
|
|
|
EmailAddress
|
|
|
|
};
|
|
|
|
|
2018-04-18 16:28:48 +02:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const SecureValueType &type);
|
|
|
|
|
2018-08-12 11:51:24 +02:00
|
|
|
vector<SecureValueType> unique_secure_value_types(vector<SecureValueType> types);
|
|
|
|
|
2018-04-11 19:42:06 +02:00
|
|
|
SecureValueType get_secure_value_type(const tl_object_ptr<telegram_api::SecureValueType> &secure_value_type);
|
2018-08-01 15:52:07 +02:00
|
|
|
SecureValueType get_secure_value_type_td_api(const tl_object_ptr<td_api::PassportElementType> &passport_element_type);
|
2018-03-27 15:11:15 +02:00
|
|
|
|
|
|
|
vector<SecureValueType> get_secure_value_types(
|
2018-04-11 19:42:06 +02:00
|
|
|
const vector<tl_object_ptr<telegram_api::SecureValueType>> &secure_value_types);
|
2018-04-06 21:37:30 +02:00
|
|
|
vector<SecureValueType> get_secure_value_types_td_api(
|
2018-08-01 15:52:07 +02:00
|
|
|
const vector<tl_object_ptr<td_api::PassportElementType>> &secure_value_types);
|
2018-03-27 15:11:15 +02:00
|
|
|
|
2018-08-01 15:52:07 +02:00
|
|
|
td_api::object_ptr<td_api::PassportElementType> get_passport_element_type_object(SecureValueType type);
|
2018-04-19 16:31:25 +02:00
|
|
|
td_api::object_ptr<telegram_api::SecureValueType> get_input_secure_value_type(SecureValueType type);
|
2018-03-27 15:11:15 +02:00
|
|
|
|
2018-08-01 15:52:07 +02:00
|
|
|
vector<td_api::object_ptr<td_api::PassportElementType>> get_passport_element_types_object(
|
2018-03-27 15:11:15 +02:00
|
|
|
const vector<SecureValueType> &types);
|
|
|
|
|
2018-08-12 11:51:24 +02:00
|
|
|
struct SuitableSecureValue {
|
|
|
|
SecureValueType type;
|
|
|
|
bool is_selfie_required;
|
|
|
|
bool is_translation_required;
|
|
|
|
bool is_native_name_required;
|
|
|
|
};
|
|
|
|
|
|
|
|
SuitableSecureValue get_suitable_secure_value(
|
|
|
|
const tl_object_ptr<telegram_api::secureRequiredType> &secure_required_type);
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::passportSuitableElement> get_passport_suitable_element_object(
|
|
|
|
const SuitableSecureValue &required_element);
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::passportRequiredElement> get_passport_required_element_object(
|
|
|
|
const vector<SuitableSecureValue> &required_element);
|
|
|
|
|
|
|
|
vector<td_api::object_ptr<td_api::passportRequiredElement>> get_passport_required_elements_object(
|
|
|
|
const vector<vector<SuitableSecureValue>> &required_elements);
|
|
|
|
|
2018-04-18 16:28:48 +02:00
|
|
|
string get_secure_value_data_field_name(SecureValueType type, string field_name);
|
|
|
|
|
2018-04-20 16:48:17 +02:00
|
|
|
struct DatedFile {
|
2018-03-27 15:11:15 +02:00
|
|
|
FileId file_id;
|
2018-04-19 15:43:09 +02:00
|
|
|
int32 date = 0;
|
2018-04-20 16:48:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
bool operator==(const DatedFile &lhs, const DatedFile &rhs);
|
|
|
|
bool operator!=(const DatedFile &lhs, const DatedFile &rhs);
|
|
|
|
|
|
|
|
struct EncryptedSecureFile {
|
|
|
|
DatedFile file;
|
2018-03-27 15:11:15 +02:00
|
|
|
string file_hash;
|
|
|
|
string encrypted_secret;
|
|
|
|
};
|
|
|
|
|
2018-04-06 15:41:38 +02:00
|
|
|
bool operator==(const EncryptedSecureFile &lhs, const EncryptedSecureFile &rhs);
|
|
|
|
bool operator!=(const EncryptedSecureFile &lhs, const EncryptedSecureFile &rhs);
|
2018-03-27 15:11:15 +02:00
|
|
|
|
2018-04-07 00:29:36 +02:00
|
|
|
EncryptedSecureFile get_encrypted_secure_file(FileManager *file_manager,
|
|
|
|
tl_object_ptr<telegram_api::SecureFile> &&secure_file_ptr);
|
2018-03-27 15:11:15 +02:00
|
|
|
|
2018-04-07 00:29:36 +02:00
|
|
|
vector<EncryptedSecureFile> get_encrypted_secure_files(FileManager *file_manager,
|
|
|
|
vector<tl_object_ptr<telegram_api::SecureFile>> &&secure_files);
|
2018-03-27 15:11:15 +02:00
|
|
|
|
2018-04-03 19:49:07 +02:00
|
|
|
struct SecureInputFile {
|
|
|
|
FileId file_id;
|
|
|
|
tl_object_ptr<telegram_api::InputSecureFile> input_file;
|
|
|
|
};
|
2018-03-27 15:11:15 +02:00
|
|
|
telegram_api::object_ptr<telegram_api::InputSecureFile> get_input_secure_file_object(FileManager *file_manager,
|
2018-04-06 15:41:38 +02:00
|
|
|
const EncryptedSecureFile &file,
|
2018-04-03 19:49:07 +02:00
|
|
|
SecureInputFile &input_file);
|
2018-03-27 15:11:15 +02:00
|
|
|
|
|
|
|
vector<telegram_api::object_ptr<telegram_api::InputSecureFile>> get_input_secure_files_object(
|
2018-04-06 15:41:38 +02:00
|
|
|
FileManager *file_manager, const vector<EncryptedSecureFile> &file, vector<SecureInputFile> &input_files);
|
2018-03-27 15:11:15 +02:00
|
|
|
|
2018-04-06 15:41:38 +02:00
|
|
|
struct EncryptedSecureData {
|
2018-03-27 15:11:15 +02:00
|
|
|
string data;
|
|
|
|
string hash;
|
|
|
|
string encrypted_secret;
|
|
|
|
};
|
|
|
|
|
2018-04-06 15:41:38 +02:00
|
|
|
bool operator==(const EncryptedSecureData &lhs, const EncryptedSecureData &rhs);
|
|
|
|
bool operator!=(const EncryptedSecureData &lhs, const EncryptedSecureData &rhs);
|
2018-03-27 15:11:15 +02:00
|
|
|
|
2018-04-07 00:29:36 +02:00
|
|
|
EncryptedSecureData get_encrypted_secure_data(tl_object_ptr<telegram_api::secureData> &&secure_data);
|
2018-03-27 15:11:15 +02:00
|
|
|
|
2018-04-06 15:41:38 +02:00
|
|
|
telegram_api::object_ptr<telegram_api::secureData> get_secure_data_object(const EncryptedSecureData &data);
|
2018-03-27 15:11:15 +02:00
|
|
|
|
|
|
|
struct EncryptedSecureValue {
|
|
|
|
SecureValueType type = SecureValueType::None;
|
2018-04-06 15:41:38 +02:00
|
|
|
EncryptedSecureData data;
|
|
|
|
vector<EncryptedSecureFile> files;
|
2018-04-27 14:38:02 +02:00
|
|
|
EncryptedSecureFile front_side;
|
|
|
|
EncryptedSecureFile reverse_side;
|
2018-04-06 15:41:38 +02:00
|
|
|
EncryptedSecureFile selfie;
|
2018-08-10 18:39:48 +02:00
|
|
|
vector<EncryptedSecureFile> translations;
|
|
|
|
string hash;
|
2018-03-27 15:11:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
bool operator==(const EncryptedSecureValue &lhs, const EncryptedSecureValue &rhs);
|
|
|
|
bool operator!=(const EncryptedSecureValue &lhs, const EncryptedSecureValue &rhs);
|
|
|
|
|
|
|
|
EncryptedSecureValue get_encrypted_secure_value(FileManager *file_manager,
|
|
|
|
tl_object_ptr<telegram_api::secureValue> &&secure_value);
|
|
|
|
|
|
|
|
vector<EncryptedSecureValue> get_encrypted_secure_values(
|
|
|
|
FileManager *file_manager, vector<tl_object_ptr<telegram_api::secureValue>> &&secure_values);
|
|
|
|
|
2018-08-01 15:52:07 +02:00
|
|
|
td_api::object_ptr<td_api::encryptedPassportElement> get_encrypted_passport_element_object(
|
|
|
|
FileManager *file_manager, const EncryptedSecureValue &value);
|
2018-03-27 15:11:15 +02:00
|
|
|
telegram_api::object_ptr<telegram_api::inputSecureValue> get_input_secure_value_object(
|
2018-08-10 18:39:48 +02:00
|
|
|
FileManager *file_manager, const EncryptedSecureValue &value, vector<SecureInputFile> &files,
|
|
|
|
optional<SecureInputFile> &front_side, optional<SecureInputFile> &reverse_side, optional<SecureInputFile> &selfie,
|
|
|
|
vector<SecureInputFile> &translations);
|
2018-03-27 15:11:15 +02:00
|
|
|
|
2018-08-01 15:52:07 +02:00
|
|
|
vector<td_api::object_ptr<td_api::encryptedPassportElement>> get_encrypted_passport_element_object(
|
2018-03-27 15:11:15 +02:00
|
|
|
FileManager *file_manager, const vector<EncryptedSecureValue> &values);
|
|
|
|
|
2018-04-06 21:37:30 +02:00
|
|
|
struct EncryptedSecureCredentials {
|
2018-03-27 15:11:15 +02:00
|
|
|
string data;
|
|
|
|
string hash;
|
|
|
|
string encrypted_secret;
|
|
|
|
};
|
|
|
|
|
2018-04-06 21:37:30 +02:00
|
|
|
bool operator==(const EncryptedSecureCredentials &lhs, const EncryptedSecureCredentials &rhs);
|
|
|
|
bool operator!=(const EncryptedSecureCredentials &lhs, const EncryptedSecureCredentials &rhs);
|
2018-03-27 15:11:15 +02:00
|
|
|
|
2018-04-07 00:29:36 +02:00
|
|
|
EncryptedSecureCredentials get_encrypted_secure_credentials(
|
2018-04-06 21:37:30 +02:00
|
|
|
tl_object_ptr<telegram_api::secureCredentialsEncrypted> &&credentials);
|
2018-03-27 15:11:15 +02:00
|
|
|
|
2018-04-06 21:37:30 +02:00
|
|
|
telegram_api::object_ptr<telegram_api::secureCredentialsEncrypted> get_secure_credentials_encrypted_object(
|
|
|
|
const EncryptedSecureCredentials &credentials);
|
|
|
|
td_api::object_ptr<td_api::encryptedCredentials> get_encrypted_credentials_object(
|
|
|
|
const EncryptedSecureCredentials &credentials);
|
|
|
|
|
|
|
|
struct SecureDataCredentials {
|
|
|
|
string secret;
|
2018-04-07 11:49:45 +02:00
|
|
|
string hash;
|
2018-04-06 21:37:30 +02:00
|
|
|
};
|
|
|
|
struct SecureFileCredentials {
|
|
|
|
string secret;
|
2018-04-07 11:49:45 +02:00
|
|
|
string hash;
|
2018-04-06 21:37:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SecureValueCredentials {
|
|
|
|
SecureValueType type;
|
|
|
|
string hash;
|
|
|
|
optional<SecureDataCredentials> data;
|
|
|
|
std::vector<SecureFileCredentials> files;
|
2018-04-27 14:38:02 +02:00
|
|
|
optional<SecureFileCredentials> front_side;
|
|
|
|
optional<SecureFileCredentials> reverse_side;
|
2018-04-06 21:37:30 +02:00
|
|
|
optional<SecureFileCredentials> selfie;
|
2018-08-10 18:39:48 +02:00
|
|
|
std::vector<SecureFileCredentials> translations;
|
2018-04-06 21:37:30 +02:00
|
|
|
};
|
|
|
|
|
2018-04-28 00:54:45 +02:00
|
|
|
Result<EncryptedSecureCredentials> get_encrypted_credentials(const std::vector<SecureValueCredentials> &credentials,
|
2018-08-12 11:51:24 +02:00
|
|
|
Slice payload, Slice public_key);
|
2018-03-27 15:11:15 +02:00
|
|
|
|
|
|
|
class SecureValue {
|
|
|
|
public:
|
2018-04-06 18:30:36 +02:00
|
|
|
SecureValueType type = SecureValueType::None;
|
2018-03-27 15:11:15 +02:00
|
|
|
string data;
|
2018-04-20 16:48:17 +02:00
|
|
|
vector<DatedFile> files;
|
2018-04-27 14:38:02 +02:00
|
|
|
DatedFile front_side;
|
|
|
|
DatedFile reverse_side;
|
2018-04-20 16:48:17 +02:00
|
|
|
DatedFile selfie;
|
2018-08-10 18:39:48 +02:00
|
|
|
vector<DatedFile> translations;
|
2018-03-27 15:11:15 +02:00
|
|
|
};
|
|
|
|
|
2018-04-06 21:37:30 +02:00
|
|
|
struct SecureValueWithCredentials {
|
|
|
|
SecureValue value;
|
|
|
|
SecureValueCredentials credentials;
|
|
|
|
};
|
|
|
|
|
2018-04-03 19:49:07 +02:00
|
|
|
Result<SecureValue> get_secure_value(FileManager *file_manager,
|
2018-08-01 15:52:07 +02:00
|
|
|
td_api::object_ptr<td_api::InputPassportElement> &&input_passport_element);
|
2018-04-03 19:49:07 +02:00
|
|
|
|
2018-08-01 15:52:07 +02:00
|
|
|
Result<td_api::object_ptr<td_api::PassportElement>> get_passport_element_object(FileManager *file_manager,
|
|
|
|
const SecureValue &value);
|
2018-03-27 15:11:15 +02:00
|
|
|
|
2018-08-01 15:52:07 +02:00
|
|
|
td_api::object_ptr<td_api::passportElements> get_passport_elements_object(FileManager *file_manager,
|
|
|
|
const vector<SecureValue> &values);
|
2018-04-07 00:29:36 +02:00
|
|
|
|
2018-04-19 15:43:09 +02:00
|
|
|
Result<SecureValueWithCredentials> decrypt_secure_value(FileManager *file_manager, const secure_storage::Secret &secret,
|
|
|
|
const EncryptedSecureValue &encrypted_secure_value);
|
|
|
|
Result<vector<SecureValueWithCredentials>> decrypt_secure_values(
|
2018-04-07 00:29:36 +02:00
|
|
|
FileManager *file_manager, const secure_storage::Secret &secret,
|
|
|
|
const vector<EncryptedSecureValue> &encrypted_secure_values);
|
2018-03-27 15:11:15 +02:00
|
|
|
|
|
|
|
EncryptedSecureValue encrypt_secure_value(FileManager *file_manager, const secure_storage::Secret &master_secret,
|
|
|
|
const SecureValue &secure_value);
|
|
|
|
|
|
|
|
} // namespace td
|