upgrade key alias 7

Signed-off-by: Cai Xincheng <caixincheng@huawei.com>
Change-Id: I76d39e2ce8e008f2886bf0ffd15c3d4dd73d1ce1
This commit is contained in:
Cai Xincheng 2024-09-20 09:56:53 +08:00
parent 36689a1b45
commit 356167385b
2 changed files with 8 additions and 9 deletions

View File

@ -92,17 +92,17 @@ fn get_existing_key_id(
require_password_set: bool,
) -> Option<KeyId> {
let new_alias = calculate_key_alias(calling_info, auth_type, access_type, require_password_set, true);
let new_alias_blob = HksBlob { size: new_alias.len() as u32, data: new_alias.as_ptr() };
if let Ok(true) = check_key_exists(calling_info.user_id(), auth_type, access_type, require_password_set, &new_alias)
{
let new_alias_blob = HksBlob { size: new_alias.len() as u32, data: new_alias.as_ptr() };
let key_id = KeyId::new(calling_info.user_id(), new_alias_blob, access_type);
return Some(key_id);
}
let old_alias = calculate_key_alias(calling_info, auth_type, access_type, require_password_set, false);
let old_alias_blob = HksBlob { size: old_alias.len() as u32, data: old_alias.as_ptr() };
if let Ok(true) = check_key_exists(calling_info.user_id(), auth_type, access_type, require_password_set, &old_alias)
{
let old_alias_blob = HksBlob { size: old_alias.len() as u32, data: old_alias.as_ptr() };
let key_id = KeyId::new(calling_info.user_id(), old_alias_blob, access_type);
return Some(key_id);
}
@ -119,7 +119,6 @@ pub fn rename_key_alias(
) -> Result<bool> {
let new_alias = calculate_key_alias(calling_info, auth_type, access_type, require_password_set, true);
let prefixed_new_alias = [ALIAS_PREFIX.to_vec(), new_alias.clone()].concat();
let prefixed_new_alias_blob = HksBlob { size: prefixed_new_alias.len() as u32, data: prefixed_new_alias.as_ptr() };
if check_key_exists(
calling_info.user_id(),
@ -132,14 +131,14 @@ pub fn rename_key_alias(
}
if let Some(key_id) = get_existing_key_id(calling_info, auth_type, access_type, require_password_set) {
let prefixed_new_alias_blob = HksBlob { size: prefixed_new_alias.len() as u32, data: prefixed_new_alias.as_ptr() };
let ret = unsafe { RenameKeyAlias(&key_id as *const KeyId, &prefixed_new_alias_blob as *const HksBlob) };
match ret {
SUCCESS => Ok(true),
_ => {
loge!(
"[FATAL]Rename key alias failed, err code is {}, err msg is {}.",
transfer_error_code(ErrCode::try_from(ret as u32)?).code,
transfer_error_code(ErrCode::try_from(ret as u32)?).msg
"[FATAL]Rename key alias failed, err is {}.",
transfer_error_code(ErrCode::try_from(ret as u32)?)
);
Ok(false)
},
@ -169,7 +168,6 @@ impl SecretKey {
access_type: Accessibility,
require_password_set: bool,
) -> Result<Self> {
let old_alias = calculate_key_alias(calling_info, auth_type, access_type, require_password_set, false);
let new_alias = calculate_key_alias(calling_info, auth_type, access_type, require_password_set, true);
let prefixed_new_alias = [ALIAS_PREFIX.to_vec(), new_alias.clone()].concat();
@ -195,6 +193,7 @@ impl SecretKey {
}
// Check whether key with old alias exists.
let old_alias = calculate_key_alias(calling_info, auth_type, access_type, require_password_set, false);
let key =
Self { user_id: calling_info.user_id(), auth_type, access_type, require_password_set, alias: old_alias };
if key.exists()? {

View File

@ -361,8 +361,8 @@ impl Database {
let auth_type = result.get_enum_attr(&column::AUTH_TYPE)?;
let access_type = result.get_enum_attr(&column::ACCESSIBILITY)?;
let require_password_set = result.get_bool_attr(&column::REQUIRE_PASSWORD_SET)?;
// Return false as long as a call for renaming key alias returned false.
upgrade_result |= rename_key_alias(&calling_info, auth_type, access_type, require_password_set)?;
// upgrade_result is set to false as long as any call in the loop for renaming key alias returned false.
upgrade_result &= rename_key_alias(&calling_info, auth_type, access_type, require_password_set)?;
}
Ok(upgrade_result)