1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "code_sign_utils_in_c.h"
17
18 #include <string>
19
20 #include "code_sign_utils.h"
21 #include "errcode.h"
22 #include "log.h"
23
24 using EntryMap = std::unordered_map<std::string, std::string>;
25
EnforceCodeSignForApp(const char * hapPath,const struct EntryMapEntryData * entryMapEntryData,enum FileType type)26 extern "C" int EnforceCodeSignForApp(const char *hapPath, const struct EntryMapEntryData *entryMapEntryData,
27 enum FileType type)
28 {
29 if (hapPath == nullptr || entryMapEntryData == nullptr) {
30 return CS_ERR_PARAM_INVALID;
31 }
32 std::string path(hapPath);
33 EntryMap entryPathMap;
34 int entryCount = entryMapEntryData->count;
35 for (int i = 0; i < entryCount; ++i) {
36 EntryMapEntry entry = entryMapEntryData->entries[i];
37 if (entry.key == nullptr || entry.value == nullptr) {
38 return CS_ERR_PARAM_INVALID;
39 }
40 std::string strKey(entry.key);
41 std::string strValue(entry.value);
42 if (entryPathMap.find(strKey) != entryPathMap.end()) {
43 return CS_ERR_PARAM_INVALID;
44 }
45 entryPathMap[strKey] = strValue;
46 }
47 OHOS::Security::CodeSign::CodeSignUtils utils;
48 return utils.EnforceCodeSignForApp(path, entryPathMap, static_cast<OHOS::Security::CodeSign::FileType>(type));
49 }