!1687 LoadSecretKeyFromDisk函数 bug 修复

Merge pull request !1687 from zhaojinghui/changjiaxing
This commit is contained in:
openharmony_ci 2024-09-07 14:05:56 +00:00 committed by Gitee
commit f41f60126b
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 35 additions and 0 deletions

View File

@ -481,11 +481,21 @@ bool RdbSecurityManager::LoadSecretKeyFromDisk(const std::string &keyPath, RdbSe
}
}
auto size = content.size();
auto offset = 0;
auto iter = content.begin();
if (offset + 1 >= size) {
return false;
}
keyData.distributed = *iter;
iter++;
offset++;
std::vector<uint8_t> createTime;
if (offset + sizeof(time_t) / sizeof(uint8_t) >= size) {
return false;
}
offset += sizeof(time_t) / sizeof(uint8_t);
for (int i = 0; i < static_cast<int>(sizeof(time_t) / sizeof(uint8_t)); i++) {
createTime.push_back(*iter);
iter++;
@ -495,6 +505,10 @@ bool RdbSecurityManager::LoadSecretKeyFromDisk(const std::string &keyPath, RdbSe
keyData.timeValue = *reinterpret_cast<time_t *>(&createTime[0]);
}
if (offset + AEAD_LEN >= size) {
return false;
}
offset = size;
keyData.secretKey.insert(keyData.secretKey.end(), iter, content.end());
return true;

View File

@ -21,6 +21,7 @@
#include "common.h"
#include "rdb_errno.h"
#include "file_ex.h"
using namespace testing::ext;
using namespace OHOS::NativeRdb;
@ -99,4 +100,24 @@ HWTEST_F(RdbSecurityManagerTest, LockUnlock, TestSize.Level1)
ASSERT_TRUE(afterUnlock);
thread.join();
}
/**
* @tc.name: LoadSecretKeyFromDiskTest
* @tc.desc: test load secret key from disk test
* @tc.type: FUNC
*/
HWTEST_F(RdbSecurityManagerTest, LoadSecretKeyFromDiskTest, TestSize.Level1)
{
std::string name = "secret_key_load_test";
auto keyPath = RDB_TEST_PATH + "key/" + name + ".pub_key";
RdbSecurityManager::KeyFiles keyFile(keyPath);
const std::string file = keyFile.GetKeyFile(RdbSecurityManager::KeyFileType::PUB_KEY_FILE);
std::vector<char> content = { 'a' };
bool ret = OHOS::SaveBufferToFile(file, content);
ASSERT_TRUE(ret);
RdbPassword pwd =
RdbSecurityManager::GetInstance().GetRdbPassword(keyPath, RdbSecurityManager::KeyFileType::PUB_KEY_FILE);
ASSERT_EQ(pwd.GetSize(), 0);
}
}