1 /*
2 * Copyright (c) 2023 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 "dlp_permission_kit.h"
17 #include <string>
18 #include <thread>
19 #include <vector>
20 #include "dlp_permission.h"
21 #include "dlp_permission_log.h"
22 #include "dlp_permission_serializer.h"
23 #include "permission_policy.h"
24
25 namespace OHOS {
26 namespace Security {
27 namespace DlpPermission {
28 namespace {
29 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION, "DlpPermissionKit"};
30 } // namespace
31
GenerateDlpCertificate(const PermissionPolicy & policy,std::vector<uint8_t> & cert)32 int32_t DlpPermissionKit::GenerateDlpCertificate(const PermissionPolicy& policy, std::vector<uint8_t>& cert)
33 {
34 unordered_json jsonObj;
35 int32_t res = DlpPermissionSerializer::GetInstance().SerializeDlpPermission(policy, jsonObj);
36 if (res != DLP_OK) {
37 return res;
38 }
39
40 std::string certStr = jsonObj.dump();
41 cert = std::vector<uint8_t>(certStr.begin(), certStr.end());
42 return DLP_OK;
43 }
44
ParseDlpCertificate(const std::vector<uint8_t> & onlineCert,std::vector<uint8_t> & offlineCert,uint32_t & offlineFlag,PermissionPolicy & policy)45 int32_t DlpPermissionKit::ParseDlpCertificate(const std::vector<uint8_t>& onlineCert, std::vector<uint8_t>& offlineCert,
46 uint32_t& offlineFlag, PermissionPolicy& policy)
47 {
48 std::string encJsonStr(onlineCert.begin(), onlineCert.end());
49 auto jsonObj = nlohmann::json::parse(encJsonStr, nullptr, false);
50 if (jsonObj.is_discarded() || (!jsonObj.is_object())) {
51 DLP_LOG_ERROR(LABEL, "JsonObj is discarded");
52 return DLP_SERVICE_ERROR_JSON_OPERATE_FAIL;
53 }
54 return DlpPermissionSerializer::GetInstance().DeserializeDlpPermission(jsonObj, policy);
55 }
56 } // namespace DlpPermission
57 } // namespace Security
58 } // namespace OHOS
59