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     std::string certStr = jsonObj.dump();
40     cert = std::vector<uint8_t>(certStr.begin(), certStr.end());
41     return DLP_OK;
42 }
43 
ParseDlpCertificate(sptr<CertParcel> & certParcel,PermissionPolicy & policy,const std::string & appId,const bool & offlineAccess)44 int32_t DlpPermissionKit::ParseDlpCertificate(sptr<CertParcel>& certParcel, PermissionPolicy& policy,
45     const std::string& appId, const bool& offlineAccess)
46 {
47     std::string encJsonStr(certParcel->cert.begin(), certParcel->cert.end());
48     auto jsonObj = nlohmann::json::parse(encJsonStr, nullptr, false);
49     if (jsonObj.is_discarded() || (!jsonObj.is_object())) {
50         DLP_LOG_ERROR(LABEL, "JsonObj is discarded");
51         return DLP_SERVICE_ERROR_JSON_OPERATE_FAIL;
52     }
53     certParcel->offlineCert = certParcel->cert;
54     return DlpPermissionSerializer::GetInstance().DeserializeDlpPermission(jsonObj, policy);
55 }
56 
SetReadFlag(uint32_t uid)57 int32_t DlpPermissionKit::SetReadFlag(uint32_t uid)
58 {
59     return true;
60 }
61 }  // namespace DlpPermission
62 }  // namespace Security
63 }  // namespace OHOS
64