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,nlohmann::json jsonObj,std::string versionKey,std::string infoKey)32 static bool checkParams(GenerateInfoParams& params, nlohmann::json jsonObj, std::string versionKey, std::string infoKey)
33 {
34 if (jsonObj.find(versionKey) == jsonObj.end() || !jsonObj.at(versionKey).is_number_integer()) {
35 return false;
36 }
37 if (jsonObj.find(infoKey) != jsonObj.end() && jsonObj.at(infoKey).is_array() &&
38 !jsonObj.at(infoKey).empty() && jsonObj.at(infoKey).at(0).is_string()) {
39 return true;
40 }
41 return false;
42 }
43
GenerateDlpGeneralInfo(const GenerateInfoParams & params,std::string & generalInfo)44 int32_t GenerateDlpGeneralInfo(const GenerateInfoParams& params, std::string& generalInfo)
45 {
46 nlohmann::json dlp_general_info;
47
48 #ifdef DLP_FILE_VERSION_INNER
49 uint32_t version = params.version;
50 #else
51 uint32_t version = CURRENT_VERSION;
52 #endif
53
54 dlp_general_info[DLP_VERSION_LOW_CAMEL_CASE] = version;
55 dlp_general_info[DLP_OFFLINE_FLAG] = params.offlineAccessFlag;
56 if (params.contactAccount.empty()) {
57 return DLP_SERVICE_ERROR_VALUE_INVALID;
58 }
59 dlp_general_info[DLP_CONTACT_ACCOUNT] = params.contactAccount;
60 dlp_general_info[DLP_EXTRA_INFO_LOW_CAMEL_CASE] = params.extraInfo;
61 if (params.extraInfo.empty()) {
62 dlp_general_info[DLP_EXTRA_INFO_LOW_CAMEL_CASE] = {"kia_info", "cert_info", "enc_data"};
63 }
64 if (version >= HMAC_VERSION) {
65 dlp_general_info[DLP_HMAC_VALUE] = params.hmacVal;
66 }
67 generalInfo = dlp_general_info.dump();
68 return DLP_OK;
69 }
70
ParseDlpGeneralInfo(const std::string & generalInfo,GenerateInfoParams & params)71 int32_t ParseDlpGeneralInfo(const std::string& generalInfo, GenerateInfoParams& params)
72 {
73 if (generalInfo.empty()) {
74 return DLP_SERVICE_ERROR_VALUE_INVALID;
75 }
76 auto jsonObj = nlohmann::json::parse(generalInfo, nullptr, false);
77 if (jsonObj.is_discarded() || (!jsonObj.is_object())) {
78 return DLP_PARSE_ERROR_VALUE_INVALID;
79 }
80 if (checkParams(params, jsonObj, DLP_VERSION, DLP_EXTRA_INFO)) {
81 params.version = jsonObj.at(DLP_VERSION).get<uint32_t>();
82 params.extraInfo = jsonObj.at(DLP_EXTRA_INFO).get<std::vector<std::string>>();
83 } else if (checkParams(params, jsonObj, DLP_VERSION_LOW_CAMEL_CASE, DLP_EXTRA_INFO_LOW_CAMEL_CASE)) {
84 params.version = jsonObj.at(DLP_VERSION_LOW_CAMEL_CASE).get<uint32_t>();
85 params.extraInfo = jsonObj.at(DLP_EXTRA_INFO_LOW_CAMEL_CASE).get<std::vector<std::string>>();
86 } else {
87 return DLP_PARSE_ERROR_VALUE_INVALID;
88 }
89
90 if (jsonObj.find(DLP_OFFLINE_FLAG) != jsonObj.end() && jsonObj.at(DLP_OFFLINE_FLAG).is_boolean()) {
91 params.offlineAccessFlag = jsonObj.at(DLP_OFFLINE_FLAG).get<bool>();
92 } else {
93 return DLP_PARSE_ERROR_VALUE_INVALID;
94 }
95
96 if (jsonObj.find(DLP_CONTACT_ACCOUNT) != jsonObj.end() && jsonObj.at(DLP_CONTACT_ACCOUNT).is_string()) {
97 params.contactAccount = jsonObj.at(DLP_CONTACT_ACCOUNT).get<std::string>();
98 if (params.contactAccount == "") {
99 return DLP_PARSE_ERROR_VALUE_INVALID;
100 }
101 }
102
103 if (jsonObj.find(DLP_HMAC_VALUE) != jsonObj.end() && jsonObj.at(DLP_HMAC_VALUE).is_string()) {
104 params.hmacVal = jsonObj.at(DLP_HMAC_VALUE).get<std::string>();
105 } else if (params.version >= HMAC_VERSION) {
106 return DLP_PARSE_ERROR_VALUE_INVALID;
107 }
108 return DLP_OK;
109 }
110 } // namespace DlpPermission
111 } // namespace Security
112 } // namespace OHOS