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