Various fixes.

GitOrigin-RevId: e3a710c085e465c44c850db0b41fae19061c2dc5
This commit is contained in:
levlam 2018-04-06 16:26:32 +03:00
parent f7826ec41a
commit 93db75293d
4 changed files with 30 additions and 31 deletions

View File

@ -196,29 +196,27 @@ void PasswordManager::get_full_state(string password, Promise<PasswordFullState>
void PasswordManager::do_get_full_state(string password, PasswordState state, Promise<PasswordFullState> promise) {
auto current_salt = state.current_salt;
send_with_promise(G()->net_query_creator().create(create_storer(
telegram_api::account_getPasswordSettings(calc_password_hash(password, current_salt)))),
PromiseCreator::lambda([promise = std::move(promise), state = std::move(state),
password](Result<NetQueryPtr> r_query) mutable {
promise.set_result([&]() -> Result<PasswordFullState> {
TRY_RESULT(query, std::move(r_query));
TRY_RESULT(result, fetch_result<telegram_api::account_getPasswordSettings>(std::move(query)));
PasswordPrivateState private_state;
private_state.email = result->email_;
send_with_promise(
G()->net_query_creator().create(
create_storer(telegram_api::account_getPasswordSettings(calc_password_hash(password, current_salt)))),
PromiseCreator::lambda([promise = std::move(promise), state = std::move(state),
password](Result<NetQueryPtr> r_query) mutable {
promise.set_result([&]() -> Result<PasswordFullState> {
TRY_RESULT(query, std::move(r_query));
TRY_RESULT(result, fetch_result<telegram_api::account_getPasswordSettings>(std::move(query)));
PasswordPrivateState private_state;
private_state.email = result->email_;
namespace ss = secure_storage;
auto r_secret = [&]() -> Result<ss::Secret> {
TRY_RESULT(encrypted_secret, ss::EncryptedSecret::create(result->secure_secret_.as_slice()));
return encrypted_secret.decrypt(PSLICE() << result->secure_salt_.as_slice() << password
<< result->secure_salt_.as_slice());
}();
auto r_secret = [&]() -> Result<secure_storage::Secret> {
TRY_RESULT(encrypted_secret, secure_storage::EncryptedSecret::create(result->secure_secret_.as_slice()));
return encrypted_secret.decrypt(PSLICE() << result->secure_salt_.as_slice() << password
<< result->secure_salt_.as_slice());
}();
LOG_IF(ERROR, r_secret.is_error()) << r_secret.error();
LOG_IF(ERROR, r_secret.is_ok()) << "HAS SECRET";
private_state.secret = std::move(r_secret);
return PasswordFullState{std::move(state), std::move(private_state)};
}());
}));
private_state.secret = std::move(r_secret);
return PasswordFullState{std::move(state), std::move(private_state)};
}());
}));
}
void PasswordManager::get_recovery_email_address(string password,

View File

@ -35,7 +35,7 @@ AesCbcState calc_aes_cbc_state(Slice seed) {
}
template <class F>
Status data_view_for_each(DataView &data, F &&f) {
static Status data_view_for_each(DataView &data, F &&f) {
const int64 step = 128 << 10;
for (int64 i = 0, size = data.size(); i < size; i += step) {
TRY_RESULT(bytes, data.pread(i, min(step, size - i)));
@ -159,7 +159,7 @@ Result<Secret> Secret::create(Slice secret) {
}
uint32 checksum = secret_checksum(secret);
if (checksum != 0) {
return Status::Error(PSLICE() << "Wrong cheksum " << checksum);
return Status::Error(PSLICE() << "Wrong checksum " << checksum);
}
UInt256 res;
td::as_slice(res).copy_from(secret);
@ -206,7 +206,7 @@ Secret::Secret(UInt256 secret, int64 hash) : secret_(secret), hash_(hash) {
//EncryptedSecret
Result<EncryptedSecret> EncryptedSecret::create(Slice encrypted_secret) {
if (encrypted_secret.size() != 32) {
return Status::Error("Wrong encrypte secret size");
return Status::Error("Wrong encrypted secret size");
}
UInt256 res;
td::as_slice(res).copy_from(encrypted_secret);
@ -242,6 +242,9 @@ Result<BufferSlice> Decryptor::append(BufferSlice data) {
if (!skipped_prefix_) {
to_skip_ = data.as_slice().ubegin()[0];
size_t to_skip = min(to_skip_, data.size());
if (to_skip_ > data.size()) {
to_skip_ = 0; // to fail final to_skip check
}
skipped_prefix_ = true;
data = data.from_slice(data.as_slice().remove_prefix(to_skip));
}

View File

@ -488,9 +488,8 @@ vector<SecureFile> encrypt_secure_files(FileManager *file_manager, const secure_
}
SecureData encrypt_secure_data(const secure_storage::Secret &master_secret, Slice data, string &to_hash) {
namespace ss = secure_storage;
auto secret = ss::Secret::create_new();
auto encrypted = ss::encrypt_value(secret, data).move_as_ok();
auto secret = secure_storage::Secret::create_new();
auto encrypted = encrypt_value(secret, data).move_as_ok();
SecureData res;
res.encrypted_secret =
secret.encrypt(PSLICE() << master_secret.as_slice() << encrypted.hash.as_slice()).as_slice().str();
@ -503,21 +502,20 @@ SecureData encrypt_secure_data(const secure_storage::Secret &master_secret, Slic
EncryptedSecureValue encrypt_secure_value(FileManager *file_manager, const secure_storage::Secret &master_secret,
const SecureValue &secure_value) {
namespace ss = secure_storage;
EncryptedSecureValue res;
res.type = secure_value.type;
switch (res.type) {
case SecureValueType::EmailAddress:
case SecureValueType::PhoneNumber:
res.data = SecureData{secure_value.data, "", ""};
res.hash = ss::calc_value_hash(secure_value.data).as_slice().str();
res.hash = secure_storage::calc_value_hash(secure_value.data).as_slice().str();
break;
default: {
string to_hash;
res.data = encrypt_secure_data(master_secret, secure_value.data, to_hash);
res.files = encrypt_secure_files(file_manager, master_secret, secure_value.files, to_hash);
res.selfie = encrypt_secure_file(file_manager, master_secret, secure_value.selfie, to_hash);
res.hash = ss::calc_value_hash(to_hash).as_slice().str();
res.hash = secure_storage::calc_value_hash(to_hash).as_slice().str();
break;
}
}

View File

@ -130,7 +130,7 @@ td_api::object_ptr<td_api::encryptedCredentials> get_encrypted_credentials_objec
class SecureValue {
public:
SecureValueType type;
SecureValueType type = SecureValueType::None
string data;
vector<FileId> files;
FileId selfie;