• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 
17 #include "dlp_permission_public_interface.h"
18 #include "dlp_permission.h"
19 #include "nlohmann/json.hpp"
20 
21 namespace OHOS {
22 namespace Security {
23 namespace DlpPermission {
24 using Json = nlohmann::json;
25 const std::string DLP_CONTACT_ACCOUNT = "contactAccount";
26 const std::string DLP_VERSION = "dlp_version";
27 const std::string DLP_VERSION_LOW_CAMEL_CASE = "dlpVersion";
28 const std::string DLP_OFFLINE_FLAG = "offlineAccess";
29 const std::string DLP_EXTRA_INFO = "extra_info";
30 const std::string DLP_EXTRA_INFO_LOW_CAMEL_CASE = "extraInfo";
31 const std::string DLP_HMAC_VALUE = "hmacValue";
checkParams(GenerateInfoParams & params,const nlohmann::json & jsonObj,const std::string & versionKey,const std::string & infoKey)32 static bool checkParams(GenerateInfoParams& params, const nlohmann::json& jsonObj,
33                         const std::string& versionKey, const std::string& infoKey)
34 {
35     auto iter = jsonObj.find(versionKey);
36     if (iter == jsonObj.end() || !iter->is_number_integer()) {
37         return false;
38     }
39     iter = jsonObj.find(infoKey);
40     if (iter != jsonObj.end() && iter->is_array() &&
41         !iter->empty() && iter->at(0).is_string()) {
42         return true;
43     }
44     return false;
45 }
46 
GenerateDlpGeneralInfo(const GenerateInfoParams & params,std::string & generalInfo)47 int32_t GenerateDlpGeneralInfo(const GenerateInfoParams& params, std::string& generalInfo)
48 {
49     nlohmann::json dlp_general_info;
50 
51 #ifdef DLP_FILE_VERSION_INNER
52     uint32_t version = params.version;
53 #else
54     uint32_t version = CURRENT_VERSION;
55 #endif
56 
57     dlp_general_info[DLP_VERSION_LOW_CAMEL_CASE] = version;
58     dlp_general_info[DLP_OFFLINE_FLAG] = params.offlineAccessFlag;
59     if (params.contactAccount.empty()) {
60         return DLP_SERVICE_ERROR_VALUE_INVALID;
61     }
62     dlp_general_info[DLP_CONTACT_ACCOUNT] = params.contactAccount;
63     dlp_general_info[DLP_EXTRA_INFO_LOW_CAMEL_CASE] = params.extraInfo;
64     if (params.extraInfo.empty()) {
65         dlp_general_info[DLP_EXTRA_INFO_LOW_CAMEL_CASE] = {"kia_info", "cert_info", "enc_data"};
66     }
67     if (version >= HMAC_VERSION) {
68         dlp_general_info[DLP_HMAC_VALUE] = params.hmacVal;
69     }
70     generalInfo = dlp_general_info.dump();
71     return DLP_OK;
72 }
73 
ParseDlpGeneralInfo(const std::string & generalInfo,GenerateInfoParams & params)74 int32_t ParseDlpGeneralInfo(const std::string& generalInfo, GenerateInfoParams& params)
75 {
76     if (generalInfo.empty()) {
77         return DLP_SERVICE_ERROR_VALUE_INVALID;
78     }
79     auto jsonObj = nlohmann::json::parse(generalInfo, nullptr, false);
80     if (jsonObj.is_discarded() || (!jsonObj.is_object())) {
81         return DLP_PARSE_ERROR_VALUE_INVALID;
82     }
83     if (checkParams(params, jsonObj, DLP_VERSION, DLP_EXTRA_INFO)) {
84         params.version = jsonObj.at(DLP_VERSION).get<uint32_t>();
85         params.extraInfo = jsonObj.at(DLP_EXTRA_INFO).get<std::vector<std::string>>();
86     } else if (checkParams(params, jsonObj, DLP_VERSION_LOW_CAMEL_CASE, DLP_EXTRA_INFO_LOW_CAMEL_CASE)) {
87         params.version = jsonObj.at(DLP_VERSION_LOW_CAMEL_CASE).get<uint32_t>();
88         params.extraInfo = jsonObj.at(DLP_EXTRA_INFO_LOW_CAMEL_CASE).get<std::vector<std::string>>();
89     } else {
90         return DLP_PARSE_ERROR_VALUE_INVALID;
91     }
92     auto iter = jsonObj.find(DLP_OFFLINE_FLAG);
93     if (iter != jsonObj.end() && iter->is_boolean()) {
94         params.offlineAccessFlag = iter->get<bool>();
95     } else {
96         return DLP_PARSE_ERROR_VALUE_INVALID;
97     }
98     iter = jsonObj.find(DLP_CONTACT_ACCOUNT);
99     if (iter != jsonObj.end() && iter->is_string()) {
100         params.contactAccount = iter->get<std::string>();
101         if (params.contactAccount == "") {
102             return DLP_PARSE_ERROR_VALUE_INVALID;
103         }
104     }
105     iter = jsonObj.find(DLP_HMAC_VALUE);
106     if (iter != jsonObj.end() && iter->is_string()) {
107         params.hmacVal = iter->get<std::string>();
108     } else if (params.version >= HMAC_VERSION) {
109         return DLP_PARSE_ERROR_VALUE_INVALID;
110     }
111     return DLP_OK;
112 }
113 }  // namespace DlpPermission
114 }  // namespace Security
115 }  // namespace OHOS