!102 消除硬编码问题

Merge pull request !102 from 韩震/baiweiW
This commit is contained in:
openharmony_ci
2022-03-18 12:42:40 +00:00
committed by Gitee
8 changed files with 90 additions and 23 deletions
+3 -2
View File
@@ -30,6 +30,7 @@ ohos_shared_library("useriam_common_lib") {
"coauth/src/coauth_funcs.c",
"coauth/src/coauth_sign_centre.c",
"coauth/src/executor_message.c",
"coauth/src/pool.c",
"common/src/buffer.c",
"common/src/linked_list.c",
"common/src/tlv_base.c",
@@ -43,8 +44,8 @@ ohos_shared_library("useriam_common_lib") {
"hal_sdk/useridm_interface.cpp",
"idm/src/idm_session.c",
"idm/src/user_idm_funcs.c",
"key_mgr/src/token_key.c",
"lock/src/lock.c",
"pool/src/pool.c",
"user_auth/src/auth_level.c",
"user_auth/src/context_manager.c",
"user_auth/src/user_auth_funcs.c",
@@ -59,7 +60,7 @@ ohos_shared_library("useriam_common_lib") {
"common/inc",
"interface",
"idm/inc",
"pool/inc",
"key_mgr/inc",
"user_auth/inc",
"//third_party/openssl/include",
]
+3 -10
View File
@@ -20,17 +20,10 @@
#include "adaptor_algorithm.h"
#include "adaptor_log.h"
#include "adaptor_time.h"
#include "token_key.h"
#define TOKEN_VALIDITY_PERIOD (10 * 60 * 1000)
// Key used for coauth signature.
static uint8_t g_coAuthTokenKey[SHA256_KEY_LEN] = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
};
static bool IsTimeValid(const ScheduleTokenHal *coAuthToken)
{
uint64_t currentTime = GetSystemTime();
@@ -52,7 +45,7 @@ ResultCode CoAuthTokenSign(ScheduleTokenHal *coAuthToken)
coAuthToken->version = TOKEN_VERSION;
ResultCode ret = RESULT_SUCCESS;
Buffer *data = CreateBufferByData((uint8_t *)coAuthToken, COAUTH_TOKEN_DATA_LEN);
Buffer *key = CreateBufferByData(g_coAuthTokenKey, SHA256_KEY_LEN);
Buffer *key = GetTokenKey();
Buffer *sign = NULL;
if (data == NULL || key == NULL) {
LOG_ERROR("lack of member");
@@ -91,7 +84,7 @@ ResultCode CoAuthTokenVerify(const ScheduleTokenHal *coAuthToken)
}
ResultCode ret = RESULT_SUCCESS;
Buffer *data = CreateBufferByData((uint8_t *)coAuthToken, COAUTH_TOKEN_DATA_LEN);
Buffer *key = CreateBufferByData(g_coAuthTokenKey, SHA256_KEY_LEN);
Buffer *key = GetTokenKey();
Buffer *sign = CreateBufferByData(coAuthToken->sign, SHA256_SIGN_LEN);
Buffer *rightSign = NULL;
if (data == NULL || key == NULL || sign == NULL) {
+5
View File
@@ -25,6 +25,7 @@ extern "C" {
#include "context_manager.h"
#include "adaptor_log.h"
#include "lock.h"
#include "token_key.h"
}
namespace OHOS {
@@ -57,6 +58,10 @@ int32_t Init()
LOG_ERROR("init user auth failed");
goto FAIL;
}
if (InitTokenKey() != RESULT_SUCCESS) {
LOG_ERROR("init token key failed");
goto FAIL;
}
g_isInitUserIAM = true;
GlobalUnLock();
return RESULT_SUCCESS;
+25
View File
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef USER_IAM_TOKEN_KEY
#define USER_IAM_TOKEN_KEY
#include "buffer.h"
#include "defines.h"
Buffer *GetTokenKey(void);
ResultCode InitTokenKey(void);
#endif
+51
View File
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "token_key.h"
#include <stddef.h>
#include "adaptor_algorithm.h"
#include "adaptor_log.h"
#include "buffer.h"
#include "defines.h"
#define SHA256_KEY_LEN 32
// This is for example only. Should be implemented in trusted environment.
static Buffer *g_tokenKey = NULL;
Buffer *GetTokenKey(void)
{
return CopyBuffer(g_tokenKey);
}
ResultCode InitTokenKey(void)
{
if (g_tokenKey != NULL) {
return RESULT_SUCCESS;
}
g_tokenKey = CreateBuffer(SHA256_KEY_LEN);
if (g_tokenKey == NULL) {
LOG_ERROR("g_tokenKey: create buffer failed");
return RESULT_NO_MEMORY;
}
if (SecureRandom(g_tokenKey->buf, g_tokenKey->maxSize) != RESULT_SUCCESS) {
LOG_ERROR("get random failed");
return RESULT_GENERAL_ERROR;
}
g_tokenKey->contentSize = g_tokenKey->maxSize;
return RESULT_SUCCESS;
}
+3 -11
View File
@@ -20,18 +20,10 @@
#include "adaptor_algorithm.h"
#include "adaptor_log.h"
#include "adaptor_time.h"
#include "token_key.h"
#define TOKEN_VALIDITY_PERIOD (10 * 60 * 1000)
#define DEMO_KEY { \
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, \
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, \
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, \
0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, \
}
static uint8_t g_userAuthTokenKey[SHA256_KEY_LEN] = DEMO_KEY;
static bool IsTimeValid(const UserAuthTokenHal *userAuthToken)
{
uint64_t currentTime = GetSystemTime();
@@ -53,7 +45,7 @@ ResultCode UserAuthTokenSign(UserAuthTokenHal *userAuthToken)
userAuthToken->version = TOKEN_VERSION;
ResultCode ret = RESULT_SUCCESS;
Buffer *data = CreateBufferByData((uint8_t *)userAuthToken, AUTH_TOKEN_DATA_LEN);
Buffer *key = CreateBufferByData(g_userAuthTokenKey, SHA256_KEY_LEN);
Buffer *key = GetTokenKey();
Buffer *sign = NULL;
if (data == NULL || key == NULL) {
LOG_ERROR("lack of member");
@@ -92,7 +84,7 @@ ResultCode UserAuthTokenVerify(const UserAuthTokenHal *userAuthToken)
}
ResultCode ret = RESULT_SUCCESS;
Buffer *data = CreateBufferByData((uint8_t *)userAuthToken, AUTH_TOKEN_DATA_LEN);
Buffer *key = CreateBufferByData(g_userAuthTokenKey, SHA256_KEY_LEN);
Buffer *key = GetTokenKey();
Buffer *sign = CreateBufferByData((uint8_t *)userAuthToken->sign, SHA256_SIGN_LEN);
Buffer *rightSign = NULL;
if (data == NULL || key == NULL || sign == NULL) {