1f2d4f7b0Sopenharmony_ci/*
2f2d4f7b0Sopenharmony_ci * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
3f2d4f7b0Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f2d4f7b0Sopenharmony_ci * you may not use this file except in compliance with the License.
5f2d4f7b0Sopenharmony_ci * You may obtain a copy of the License at
6f2d4f7b0Sopenharmony_ci *
7f2d4f7b0Sopenharmony_ci *    http://www.apache.org/licenses/LICENSE-2.0
8f2d4f7b0Sopenharmony_ci *
9f2d4f7b0Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f2d4f7b0Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f2d4f7b0Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f2d4f7b0Sopenharmony_ci * See the License for the specific language governing permissions and
13f2d4f7b0Sopenharmony_ci * limitations under the License.
14f2d4f7b0Sopenharmony_ci */
15f2d4f7b0Sopenharmony_ci
16f2d4f7b0Sopenharmony_ci#include "app_provision.h"
17f2d4f7b0Sopenharmony_ci#include <stdbool.h>
18f2d4f7b0Sopenharmony_ci#include <string.h>
19f2d4f7b0Sopenharmony_ci#include "app_common.h"
20f2d4f7b0Sopenharmony_ci#include "app_verify_hal.h"
21f2d4f7b0Sopenharmony_ci#include "cJSON.h"
22f2d4f7b0Sopenharmony_ci#include "securec.h"
23f2d4f7b0Sopenharmony_ci
24f2d4f7b0Sopenharmony_ciconst char APP_GALLERY[] = "app_gallery";
25f2d4f7b0Sopenharmony_ciconst char ENTERPRISE[] = "enterprise";
26f2d4f7b0Sopenharmony_ciconst char ENTERPRISE_NORMAL[] = "enterprise_normal";
27f2d4f7b0Sopenharmony_ciconst char ENTERPRISE_MDM[] = "enterprise_mdm";
28f2d4f7b0Sopenharmony_ciconst char OS_INTEGRATION[] = "os_integration";
29f2d4f7b0Sopenharmony_ciconst char INTERNALTESTING[] = "internaltesting";
30f2d4f7b0Sopenharmony_ci
31f2d4f7b0Sopenharmony_cistatic void ProfInit(ProfileProf *pf)
32f2d4f7b0Sopenharmony_ci{
33f2d4f7b0Sopenharmony_ci    errno_t ret = memset_s(pf, sizeof(ProfileProf), 0, sizeof(ProfileProf));
34f2d4f7b0Sopenharmony_ci    if (ret != EOK) {
35f2d4f7b0Sopenharmony_ci        LOG_ERROR("memset failed");
36f2d4f7b0Sopenharmony_ci        return;
37f2d4f7b0Sopenharmony_ci    }
38f2d4f7b0Sopenharmony_ci    return;
39f2d4f7b0Sopenharmony_ci}
40f2d4f7b0Sopenharmony_ci
41f2d4f7b0Sopenharmony_cistatic char *GetStringTag(const cJSON *root, const char *tag)
42f2d4f7b0Sopenharmony_ci{
43f2d4f7b0Sopenharmony_ci    cJSON *jsonObj = cJSON_GetObjectItem(root, tag);
44f2d4f7b0Sopenharmony_ci    if ((jsonObj == NULL) || (jsonObj->valuestring == NULL)) {
45f2d4f7b0Sopenharmony_ci        LOG_PRINT_STR("failed to get %s", tag);
46f2d4f7b0Sopenharmony_ci        return NULL;
47f2d4f7b0Sopenharmony_ci    }
48f2d4f7b0Sopenharmony_ci    int32_t objLen = strlen(jsonObj->valuestring);
49f2d4f7b0Sopenharmony_ci    if (objLen < 0) {
50f2d4f7b0Sopenharmony_ci        LOG_PRINT_STR("len error");
51f2d4f7b0Sopenharmony_ci        return NULL;
52f2d4f7b0Sopenharmony_ci    }
53f2d4f7b0Sopenharmony_ci    char *value = APPV_MALLOC(objLen + 1);
54f2d4f7b0Sopenharmony_ci    if (value == NULL) {
55f2d4f7b0Sopenharmony_ci        LOG_ERROR("malloc error: %d", objLen + 1);
56f2d4f7b0Sopenharmony_ci        return NULL;
57f2d4f7b0Sopenharmony_ci    }
58f2d4f7b0Sopenharmony_ci    errno_t ret = strcpy_s(value, objLen + 1, jsonObj->valuestring);
59f2d4f7b0Sopenharmony_ci    if (ret != EOK) {
60f2d4f7b0Sopenharmony_ci        APPV_FREE(value);
61f2d4f7b0Sopenharmony_ci        LOG_ERROR("strcpy error: %d", ret);
62f2d4f7b0Sopenharmony_ci        return NULL;
63f2d4f7b0Sopenharmony_ci    }
64f2d4f7b0Sopenharmony_ci    return value;
65f2d4f7b0Sopenharmony_ci}
66f2d4f7b0Sopenharmony_ci
67f2d4f7b0Sopenharmony_cistatic void FreeStringAttay(char **array, int32_t num)
68f2d4f7b0Sopenharmony_ci{
69f2d4f7b0Sopenharmony_ci    if (array == NULL) {
70f2d4f7b0Sopenharmony_ci        return;
71f2d4f7b0Sopenharmony_ci    }
72f2d4f7b0Sopenharmony_ci    for (int32_t i = 0; i < num; i++) {
73f2d4f7b0Sopenharmony_ci        if (array[i] != NULL) {
74f2d4f7b0Sopenharmony_ci            APPV_FREE(array[i]);
75f2d4f7b0Sopenharmony_ci        }
76f2d4f7b0Sopenharmony_ci    }
77f2d4f7b0Sopenharmony_ci    APPV_FREE(array);
78f2d4f7b0Sopenharmony_ci    return;
79f2d4f7b0Sopenharmony_ci}
80f2d4f7b0Sopenharmony_ci
81f2d4f7b0Sopenharmony_cistatic char **GetStringArrayTag(const cJSON *root, const char *tag, int32_t *numReturn)
82f2d4f7b0Sopenharmony_ci{
83f2d4f7b0Sopenharmony_ci    cJSON *jsonObj = cJSON_GetObjectItem(root, tag);
84f2d4f7b0Sopenharmony_ci    if (jsonObj == NULL) {
85f2d4f7b0Sopenharmony_ci        LOG_PRINT_STR("failed to get %s", tag);
86f2d4f7b0Sopenharmony_ci        return NULL;
87f2d4f7b0Sopenharmony_ci    }
88f2d4f7b0Sopenharmony_ci    int32_t num = cJSON_GetArraySize(jsonObj);
89f2d4f7b0Sopenharmony_ci    if (num == 0) {
90f2d4f7b0Sopenharmony_ci        LOG_ERROR("array num 0");
91f2d4f7b0Sopenharmony_ci        *numReturn = 0;
92f2d4f7b0Sopenharmony_ci        return NULL;
93f2d4f7b0Sopenharmony_ci    }
94f2d4f7b0Sopenharmony_ci    char **value = APPV_MALLOC(sizeof(char *) * num);
95f2d4f7b0Sopenharmony_ci    if (value == NULL) {
96f2d4f7b0Sopenharmony_ci        LOG_ERROR("value is null");
97f2d4f7b0Sopenharmony_ci        *numReturn = 0;
98f2d4f7b0Sopenharmony_ci        return NULL;
99f2d4f7b0Sopenharmony_ci    }
100f2d4f7b0Sopenharmony_ci    (void)memset_s(value, sizeof(char *) * num, 0, sizeof(char *) * num);
101f2d4f7b0Sopenharmony_ci
102f2d4f7b0Sopenharmony_ci    for (int32_t i = 0; i < num; i++) {
103f2d4f7b0Sopenharmony_ci        cJSON *item = cJSON_GetArrayItem(jsonObj, i);
104f2d4f7b0Sopenharmony_ci        P_NULL_GOTO_WTTH_LOG(item);
105f2d4f7b0Sopenharmony_ci        if (item->valuestring == NULL) {
106f2d4f7b0Sopenharmony_ci            LOG_ERROR("valuestring is NULL");
107f2d4f7b0Sopenharmony_ci            FreeStringAttay(value, num);
108f2d4f7b0Sopenharmony_ci            return NULL;
109f2d4f7b0Sopenharmony_ci        }
110f2d4f7b0Sopenharmony_ci        int32_t len = strlen(item->valuestring);
111f2d4f7b0Sopenharmony_ci        value[i] = APPV_MALLOC(len + 1);
112f2d4f7b0Sopenharmony_ci        P_NULL_GOTO_WTTH_LOG(value[i]);
113f2d4f7b0Sopenharmony_ci
114f2d4f7b0Sopenharmony_ci        errno_t ret = strcpy_s(value[i], len + 1, item->valuestring);
115f2d4f7b0Sopenharmony_ci        if (ret != EOK) {
116f2d4f7b0Sopenharmony_ci            LOG_ERROR("str cpy error : %d", ret);
117f2d4f7b0Sopenharmony_ci            FreeStringAttay(value, num);
118f2d4f7b0Sopenharmony_ci            return NULL;
119f2d4f7b0Sopenharmony_ci        }
120f2d4f7b0Sopenharmony_ci    }
121f2d4f7b0Sopenharmony_ci    *numReturn = num;
122f2d4f7b0Sopenharmony_ci    return value;
123f2d4f7b0Sopenharmony_ciEXIT:
124f2d4f7b0Sopenharmony_ci    FreeStringAttay(value, num);
125f2d4f7b0Sopenharmony_ci    return NULL;
126f2d4f7b0Sopenharmony_ci}
127f2d4f7b0Sopenharmony_ci
128f2d4f7b0Sopenharmony_cistatic int32_t GetProfValidity(const cJSON *root, ProfValidity *profVal)
129f2d4f7b0Sopenharmony_ci{
130f2d4f7b0Sopenharmony_ci    cJSON *jsonObj = cJSON_GetObjectItem(root, "validity");
131f2d4f7b0Sopenharmony_ci    if (jsonObj == NULL) {
132f2d4f7b0Sopenharmony_ci        LOG_ERROR("failed to get validity");
133f2d4f7b0Sopenharmony_ci        return V_ERR;
134f2d4f7b0Sopenharmony_ci    }
135f2d4f7b0Sopenharmony_ci
136f2d4f7b0Sopenharmony_ci    cJSON *notBefore = cJSON_GetObjectItem(jsonObj, "not-before");
137f2d4f7b0Sopenharmony_ci    if (notBefore == NULL) {
138f2d4f7b0Sopenharmony_ci        LOG_ERROR("failed to get not-before");
139f2d4f7b0Sopenharmony_ci        return V_ERR;
140f2d4f7b0Sopenharmony_ci    }
141f2d4f7b0Sopenharmony_ci    profVal->notBefore = notBefore->valueint;
142f2d4f7b0Sopenharmony_ci
143f2d4f7b0Sopenharmony_ci    cJSON *notAfter = cJSON_GetObjectItem(jsonObj, "not-after");
144f2d4f7b0Sopenharmony_ci    if (notAfter == NULL) {
145f2d4f7b0Sopenharmony_ci        LOG_ERROR("failed to get not-after");
146f2d4f7b0Sopenharmony_ci        return V_ERR;
147f2d4f7b0Sopenharmony_ci    }
148f2d4f7b0Sopenharmony_ci    profVal->notAfter = notAfter->valueint;
149f2d4f7b0Sopenharmony_ci    return V_OK;
150f2d4f7b0Sopenharmony_ci}
151f2d4f7b0Sopenharmony_ci
152f2d4f7b0Sopenharmony_cistatic int32_t GetProfBundleInfo(const cJSON *root, ProfBundleInfo *profVal)
153f2d4f7b0Sopenharmony_ci{
154f2d4f7b0Sopenharmony_ci    cJSON *jsonObj = cJSON_GetObjectItem(root, "bundle-info");
155f2d4f7b0Sopenharmony_ci    if (jsonObj == NULL) {
156f2d4f7b0Sopenharmony_ci        LOG_ERROR("failed to get bundle-info");
157f2d4f7b0Sopenharmony_ci        return V_ERR;
158f2d4f7b0Sopenharmony_ci    }
159f2d4f7b0Sopenharmony_ci    /* if failed, free by caller */
160f2d4f7b0Sopenharmony_ci    profVal->developerId = GetStringTag(jsonObj, "developer-id");
161f2d4f7b0Sopenharmony_ci    P_NULL_RETURN_WTTH_LOG(profVal->developerId);
162f2d4f7b0Sopenharmony_ci
163f2d4f7b0Sopenharmony_ci    profVal->devCert = (unsigned char *)GetStringTag(jsonObj, "development-certificate");
164f2d4f7b0Sopenharmony_ci    if (profVal->devCert == NULL) {
165f2d4f7b0Sopenharmony_ci        LOG_ERROR("get development-certificat failed");
166f2d4f7b0Sopenharmony_ci        profVal->devCert = APPV_MALLOC(sizeof(char));
167f2d4f7b0Sopenharmony_ci        P_NULL_RETURN_WTTH_LOG(profVal->devCert);
168f2d4f7b0Sopenharmony_ci        profVal->devCert[0] = '\0';
169f2d4f7b0Sopenharmony_ci    }
170f2d4f7b0Sopenharmony_ci
171f2d4f7b0Sopenharmony_ci    profVal->releaseCert = (unsigned char *)GetStringTag(jsonObj, "distribution-certificate");
172f2d4f7b0Sopenharmony_ci    if (profVal->releaseCert == NULL) {
173f2d4f7b0Sopenharmony_ci        LOG_ERROR("get distribution-certificat failed");
174f2d4f7b0Sopenharmony_ci        profVal->releaseCert = APPV_MALLOC(sizeof(char));
175f2d4f7b0Sopenharmony_ci        P_NULL_RETURN_WTTH_LOG(profVal->releaseCert);
176f2d4f7b0Sopenharmony_ci        profVal->releaseCert[0] = '\0';
177f2d4f7b0Sopenharmony_ci    }
178f2d4f7b0Sopenharmony_ci
179f2d4f7b0Sopenharmony_ci    profVal->bundleName = GetStringTag(jsonObj, "bundle-name");
180f2d4f7b0Sopenharmony_ci    P_NULL_RETURN_WTTH_LOG(profVal->bundleName);
181f2d4f7b0Sopenharmony_ci
182f2d4f7b0Sopenharmony_ci    profVal->appFeature = GetStringTag(jsonObj, "app-feature");
183f2d4f7b0Sopenharmony_ci    P_NULL_RETURN_WTTH_LOG(profVal->appFeature);
184f2d4f7b0Sopenharmony_ci
185f2d4f7b0Sopenharmony_ci    return V_OK;
186f2d4f7b0Sopenharmony_ci}
187f2d4f7b0Sopenharmony_ci
188f2d4f7b0Sopenharmony_cistatic int32_t GetProfPermission(const cJSON *root, ProfPermission *profVal)
189f2d4f7b0Sopenharmony_ci{
190f2d4f7b0Sopenharmony_ci    cJSON *jsonObj = cJSON_GetObjectItem(root, "permissions");
191f2d4f7b0Sopenharmony_ci    if (jsonObj == NULL) {
192f2d4f7b0Sopenharmony_ci        LOG_ERROR("failed to get permissions");
193f2d4f7b0Sopenharmony_ci        return V_ERR;
194f2d4f7b0Sopenharmony_ci    }
195f2d4f7b0Sopenharmony_ci    profVal->permission = GetStringArrayTag(jsonObj, "feature-permissions", &profVal->permissionNum);
196f2d4f7b0Sopenharmony_ci    profVal->restricPermission = GetStringArrayTag(jsonObj, "restricted-permissions", &profVal->restricNum);
197f2d4f7b0Sopenharmony_ci    return V_OK;
198f2d4f7b0Sopenharmony_ci}
199f2d4f7b0Sopenharmony_ci
200f2d4f7b0Sopenharmony_cistatic int32_t GetProfDebugInfo(const cJSON *root, ProfDebugInfo *profVal)
201f2d4f7b0Sopenharmony_ci{
202f2d4f7b0Sopenharmony_ci    cJSON *jsonObj = cJSON_GetObjectItem(root, "debug-info");
203f2d4f7b0Sopenharmony_ci    if (jsonObj == NULL) {
204f2d4f7b0Sopenharmony_ci        LOG_INFO("failed to get debug-info");
205f2d4f7b0Sopenharmony_ci        return V_OK;
206f2d4f7b0Sopenharmony_ci    }
207f2d4f7b0Sopenharmony_ci    profVal->devIdType = GetStringTag(jsonObj, "device-id-type");
208f2d4f7b0Sopenharmony_ci    if (profVal->devIdType == NULL) {
209f2d4f7b0Sopenharmony_ci        LOG_INFO("failed to get device-id-type");
210f2d4f7b0Sopenharmony_ci        return V_OK;
211f2d4f7b0Sopenharmony_ci    }
212f2d4f7b0Sopenharmony_ci    profVal->deviceId = GetStringArrayTag(jsonObj, "device-ids", &profVal->devidNum);
213f2d4f7b0Sopenharmony_ci    return V_OK;
214f2d4f7b0Sopenharmony_ci}
215f2d4f7b0Sopenharmony_ci
216f2d4f7b0Sopenharmony_cistatic int32_t GetProfIssuerInfo(const cJSON *root, ProfileProf *pf)
217f2d4f7b0Sopenharmony_ci{
218f2d4f7b0Sopenharmony_ci    pf->issuer = GetStringTag(root, "issuer");
219f2d4f7b0Sopenharmony_ci    if (pf->issuer == NULL) {
220f2d4f7b0Sopenharmony_ci        int32_t len = strlen(APP_STORE);
221f2d4f7b0Sopenharmony_ci        pf->issuer = APPV_MALLOC(len + 1);
222f2d4f7b0Sopenharmony_ci        if (pf->issuer == NULL) {
223f2d4f7b0Sopenharmony_ci            return V_ERR;
224f2d4f7b0Sopenharmony_ci        }
225f2d4f7b0Sopenharmony_ci        errno_t ret = strcpy_s(pf->issuer, len + 1, APP_STORE);
226f2d4f7b0Sopenharmony_ci        if (ret != EOK) {
227f2d4f7b0Sopenharmony_ci            APPV_FREE(pf->issuer);
228f2d4f7b0Sopenharmony_ci            LOG_ERROR("str cpy error: %d", ret);
229f2d4f7b0Sopenharmony_ci        }
230f2d4f7b0Sopenharmony_ci        return ret;
231f2d4f7b0Sopenharmony_ci    }
232f2d4f7b0Sopenharmony_ci    return V_OK;
233f2d4f7b0Sopenharmony_ci}
234f2d4f7b0Sopenharmony_ci
235f2d4f7b0Sopenharmony_cistatic void FreeProfBundle(ProfBundleInfo *pfval)
236f2d4f7b0Sopenharmony_ci{
237f2d4f7b0Sopenharmony_ci    FREE_IF_NOT_NULL(pfval->appFeature);
238f2d4f7b0Sopenharmony_ci    FREE_IF_NOT_NULL(pfval->bundleName);
239f2d4f7b0Sopenharmony_ci    FREE_IF_NOT_NULL(pfval->devCert);
240f2d4f7b0Sopenharmony_ci    FREE_IF_NOT_NULL(pfval->developerId);
241f2d4f7b0Sopenharmony_ci    FREE_IF_NOT_NULL(pfval->releaseCert);
242f2d4f7b0Sopenharmony_ci    return;
243f2d4f7b0Sopenharmony_ci}
244f2d4f7b0Sopenharmony_ci
245f2d4f7b0Sopenharmony_cistatic void FreeProfPerssion(ProfPermission *pfval)
246f2d4f7b0Sopenharmony_ci{
247f2d4f7b0Sopenharmony_ci    FreeStringAttay(pfval->permission, pfval->permissionNum);
248f2d4f7b0Sopenharmony_ci    pfval->permissionNum = 0;
249f2d4f7b0Sopenharmony_ci    pfval->permission = NULL;
250f2d4f7b0Sopenharmony_ci
251f2d4f7b0Sopenharmony_ci    FreeStringAttay(pfval->restricPermission, pfval->restricNum);
252f2d4f7b0Sopenharmony_ci    pfval->restricNum = 0;
253f2d4f7b0Sopenharmony_ci    pfval->restricPermission = NULL;
254f2d4f7b0Sopenharmony_ci    return;
255f2d4f7b0Sopenharmony_ci}
256f2d4f7b0Sopenharmony_ci
257f2d4f7b0Sopenharmony_cistatic void FreeProfDebuginfo(ProfDebugInfo *pfval)
258f2d4f7b0Sopenharmony_ci{
259f2d4f7b0Sopenharmony_ci    FREE_IF_NOT_NULL(pfval->devIdType);
260f2d4f7b0Sopenharmony_ci
261f2d4f7b0Sopenharmony_ci    FreeStringAttay(pfval->deviceId, pfval->devidNum);
262f2d4f7b0Sopenharmony_ci    pfval->devidNum = 0;
263f2d4f7b0Sopenharmony_ci    pfval->deviceId = NULL;
264f2d4f7b0Sopenharmony_ci
265f2d4f7b0Sopenharmony_ci    return;
266f2d4f7b0Sopenharmony_ci}
267f2d4f7b0Sopenharmony_ci
268f2d4f7b0Sopenharmony_civoid ProfFreeData(ProfileProf *pf)
269f2d4f7b0Sopenharmony_ci{
270f2d4f7b0Sopenharmony_ci    if (pf == NULL) {
271f2d4f7b0Sopenharmony_ci        return;
272f2d4f7b0Sopenharmony_ci    }
273f2d4f7b0Sopenharmony_ci    FREE_IF_NOT_NULL(pf->versionName);
274f2d4f7b0Sopenharmony_ci    FREE_IF_NOT_NULL(pf->uuid);
275f2d4f7b0Sopenharmony_ci    FREE_IF_NOT_NULL(pf->type);
276f2d4f7b0Sopenharmony_ci    FREE_IF_NOT_NULL(pf->appDistType);
277f2d4f7b0Sopenharmony_ci    FreeProfBundle(&pf->bundleInfo);
278f2d4f7b0Sopenharmony_ci    FreeProfPerssion(&pf->permission);
279f2d4f7b0Sopenharmony_ci    FreeProfDebuginfo(&pf->debugInfo);
280f2d4f7b0Sopenharmony_ci    FREE_IF_NOT_NULL(pf->issuer);
281f2d4f7b0Sopenharmony_ci    FREE_IF_NOT_NULL(pf->appid);
282f2d4f7b0Sopenharmony_ci    return;
283f2d4f7b0Sopenharmony_ci}
284f2d4f7b0Sopenharmony_ci
285f2d4f7b0Sopenharmony_ci/* parse profile */
286f2d4f7b0Sopenharmony_ciint32_t ParseProfile(const char *buf, int32_t len, ProfileProf *pf)
287f2d4f7b0Sopenharmony_ci{
288f2d4f7b0Sopenharmony_ci    P_NULL_RETURN_WTTH_LOG(pf);
289f2d4f7b0Sopenharmony_ci    P_NULL_RETURN_WTTH_LOG(buf);
290f2d4f7b0Sopenharmony_ci    ProfInit(pf);
291f2d4f7b0Sopenharmony_ci    int32_t ret;
292f2d4f7b0Sopenharmony_ci    char *pfStr = strchr(buf, '{');
293f2d4f7b0Sopenharmony_ci    P_NULL_RETURN_WTTH_LOG(pfStr);
294f2d4f7b0Sopenharmony_ci
295f2d4f7b0Sopenharmony_ci    cJSON *root = cJSON_Parse(pfStr);
296f2d4f7b0Sopenharmony_ci    P_NULL_RETURN_WTTH_LOG(root);
297f2d4f7b0Sopenharmony_ci
298f2d4f7b0Sopenharmony_ci    cJSON *jsonObj = cJSON_GetObjectItem(root, "version-code");
299f2d4f7b0Sopenharmony_ci    P_NULL_GOTO_WTTH_LOG(jsonObj);
300f2d4f7b0Sopenharmony_ci    pf->versionCode = jsonObj->valueint;
301f2d4f7b0Sopenharmony_ci
302f2d4f7b0Sopenharmony_ci    pf->versionName = GetStringTag(root, "version-name");
303f2d4f7b0Sopenharmony_ci    P_NULL_GOTO_WTTH_LOG(pf->versionName);
304f2d4f7b0Sopenharmony_ci
305f2d4f7b0Sopenharmony_ci    pf->uuid = GetStringTag(root, "uuid");
306f2d4f7b0Sopenharmony_ci    P_NULL_GOTO_WTTH_LOG(pf->uuid);
307f2d4f7b0Sopenharmony_ci
308f2d4f7b0Sopenharmony_ci    pf->type = GetStringTag(root, "type");
309f2d4f7b0Sopenharmony_ci    P_NULL_GOTO_WTTH_LOG(pf->type);
310f2d4f7b0Sopenharmony_ci
311f2d4f7b0Sopenharmony_ci    pf->appDistType = GetStringTag(root, "app-distribution-type");
312f2d4f7b0Sopenharmony_ci    if (pf->appDistType == NULL) {
313f2d4f7b0Sopenharmony_ci        pf->appDistType = APPV_MALLOC(sizeof(char));
314f2d4f7b0Sopenharmony_ci        P_NULL_GOTO_WTTH_LOG(pf->appDistType);
315f2d4f7b0Sopenharmony_ci        pf->appDistType[0] = '\0';
316f2d4f7b0Sopenharmony_ci    }
317f2d4f7b0Sopenharmony_ci
318f2d4f7b0Sopenharmony_ci    ret = GetProfValidity(root, &pf->validity);
319f2d4f7b0Sopenharmony_ci    P_ERR_GOTO_WTTH_LOG(ret);
320f2d4f7b0Sopenharmony_ci
321f2d4f7b0Sopenharmony_ci    ret = GetProfBundleInfo(root, &pf->bundleInfo);
322f2d4f7b0Sopenharmony_ci    P_ERR_GOTO_WTTH_LOG(ret);
323f2d4f7b0Sopenharmony_ci
324f2d4f7b0Sopenharmony_ci    ret = GetProfPermission(root, &pf->permission);
325f2d4f7b0Sopenharmony_ci    P_ERR_GOTO_WTTH_LOG(ret);
326f2d4f7b0Sopenharmony_ci
327f2d4f7b0Sopenharmony_ci    ret = GetProfDebugInfo(root, &pf->debugInfo);
328f2d4f7b0Sopenharmony_ci    P_ERR_GOTO_WTTH_LOG(ret);
329f2d4f7b0Sopenharmony_ci
330f2d4f7b0Sopenharmony_ci    ret = GetProfIssuerInfo(root, pf);
331f2d4f7b0Sopenharmony_ci    P_ERR_GOTO_WTTH_LOG(ret);
332f2d4f7b0Sopenharmony_ci
333f2d4f7b0Sopenharmony_ci    LOG_INFO("parse profile json success");
334f2d4f7b0Sopenharmony_ci    cJSON_Delete(root);
335f2d4f7b0Sopenharmony_ci    return V_OK;
336f2d4f7b0Sopenharmony_ci
337f2d4f7b0Sopenharmony_ciEXIT:
338f2d4f7b0Sopenharmony_ci    cJSON_Delete(root);
339f2d4f7b0Sopenharmony_ci    ProfFreeData(pf);
340f2d4f7b0Sopenharmony_ci    return V_ERR;
341f2d4f7b0Sopenharmony_ci}
342f2d4f7b0Sopenharmony_ci
343f2d4f7b0Sopenharmony_cistatic int32_t VerifyAppTypeAndDistribution(const ProfileProf *pf)
344f2d4f7b0Sopenharmony_ci{
345f2d4f7b0Sopenharmony_ci    if ((strcmp(pf->type, DEBUG_TYPE) != 0) && (strcmp(pf->type, RELEASE_TYPE) != 0)) {
346f2d4f7b0Sopenharmony_ci        LOG_PRINT_STR("invalid app type: %s", pf->type);
347f2d4f7b0Sopenharmony_ci        return V_ERR;
348f2d4f7b0Sopenharmony_ci    }
349f2d4f7b0Sopenharmony_ci    if (strcmp(pf->type, RELEASE_TYPE) == 0) {
350f2d4f7b0Sopenharmony_ci        if ((strcmp(pf->appDistType, APP_GALLERY) != 0) && (strcmp(pf->appDistType, ENTERPRISE) != 0) &&
351f2d4f7b0Sopenharmony_ci            (strcmp(pf->appDistType, ENTERPRISE_NORMAL) != 0) && (strcmp(pf->appDistType, ENTERPRISE_MDM) != 0) &&
352f2d4f7b0Sopenharmony_ci            (strcmp(pf->appDistType, INTERNALTESTING) != 0) && (strcmp(pf->appDistType, OS_INTEGRATION) != 0)) {
353f2d4f7b0Sopenharmony_ci            LOG_PRINT_STR("invalid app dis type: %s", pf->appDistType);
354f2d4f7b0Sopenharmony_ci            return V_ERR;
355f2d4f7b0Sopenharmony_ci        }
356f2d4f7b0Sopenharmony_ci    }
357f2d4f7b0Sopenharmony_ci    return V_OK;
358f2d4f7b0Sopenharmony_ci}
359f2d4f7b0Sopenharmony_ci
360f2d4f7b0Sopenharmony_cistatic int32_t VerifyAppBundleInfo(const ProfileProf *pf)
361f2d4f7b0Sopenharmony_ci{
362f2d4f7b0Sopenharmony_ci    if (strcmp(pf->type, DEBUG_TYPE) == 0) {
363f2d4f7b0Sopenharmony_ci        if (strlen((char *)pf->bundleInfo.devCert) == 0) {
364f2d4f7b0Sopenharmony_ci            LOG_ERROR("debug app, dev cert null");
365f2d4f7b0Sopenharmony_ci            return V_ERR;
366f2d4f7b0Sopenharmony_ci        }
367f2d4f7b0Sopenharmony_ci    } else if (strcmp(pf->type, RELEASE_TYPE) == 0) {
368f2d4f7b0Sopenharmony_ci        if (strlen((char *)pf->bundleInfo.releaseCert) == 0) {
369f2d4f7b0Sopenharmony_ci            LOG_ERROR("debug app, dev cert null");
370f2d4f7b0Sopenharmony_ci            return V_ERR;
371f2d4f7b0Sopenharmony_ci        }
372f2d4f7b0Sopenharmony_ci    } else {
373f2d4f7b0Sopenharmony_ci        LOG_PRINT_STR("invalid app type: %s", pf->type);
374f2d4f7b0Sopenharmony_ci        return V_ERR;
375f2d4f7b0Sopenharmony_ci    }
376f2d4f7b0Sopenharmony_ci    return V_OK;
377f2d4f7b0Sopenharmony_ci}
378f2d4f7b0Sopenharmony_ci
379f2d4f7b0Sopenharmony_cistatic int32_t VerifyUdid(const ProfileProf *pf)
380f2d4f7b0Sopenharmony_ci{
381f2d4f7b0Sopenharmony_ci    uint32_t size = UDID_VERIFY_BYTES + 1;
382f2d4f7b0Sopenharmony_ci    if (pf->debugInfo.devidNum > MAX_UDID_NUM) {
383f2d4f7b0Sopenharmony_ci        LOG_ERROR("udid num exceed maximum");
384f2d4f7b0Sopenharmony_ci        return V_ERR;
385f2d4f7b0Sopenharmony_ci    }
386f2d4f7b0Sopenharmony_ci    unsigned char *udid = APPV_MALLOC(size);
387f2d4f7b0Sopenharmony_ci    if (udid == NULL) {
388f2d4f7b0Sopenharmony_ci        LOG_ERROR("udid is null");
389f2d4f7b0Sopenharmony_ci        return V_ERR;
390f2d4f7b0Sopenharmony_ci    }
391f2d4f7b0Sopenharmony_ci    (void)memset_s(udid, size, 0, size);
392f2d4f7b0Sopenharmony_ci    int32_t result = InquiryDeviceUdid(udid, size);
393f2d4f7b0Sopenharmony_ci    if (result != INQUIRY_UDID_OK) {
394f2d4f7b0Sopenharmony_ci        free(udid);
395f2d4f7b0Sopenharmony_ci        LOG_ERROR("get udid fail, ret: %d", result);
396f2d4f7b0Sopenharmony_ci        return V_ERR;
397f2d4f7b0Sopenharmony_ci    }
398f2d4f7b0Sopenharmony_ci    for (int32_t i = 0; i < pf->debugInfo.devidNum; i++) {
399f2d4f7b0Sopenharmony_ci        if (strcmp((const char *)pf->debugInfo.deviceId[i], (const char *)udid) == 0) {
400f2d4f7b0Sopenharmony_ci            LOG_INFO("find right udid");
401f2d4f7b0Sopenharmony_ci            free(udid);
402f2d4f7b0Sopenharmony_ci            udid = NULL;
403f2d4f7b0Sopenharmony_ci            return V_OK;
404f2d4f7b0Sopenharmony_ci        }
405f2d4f7b0Sopenharmony_ci    }
406f2d4f7b0Sopenharmony_ci    LOG_ERROR("udid invalid");
407f2d4f7b0Sopenharmony_ci    free(udid);
408f2d4f7b0Sopenharmony_ci    udid = NULL;
409f2d4f7b0Sopenharmony_ci    return V_ERR;
410f2d4f7b0Sopenharmony_ci}
411f2d4f7b0Sopenharmony_ci
412f2d4f7b0Sopenharmony_cistatic int32_t VerifyDebugInfo(const ProfileProf *pf)
413f2d4f7b0Sopenharmony_ci{
414f2d4f7b0Sopenharmony_ci    if (strcmp(pf->type, DEBUG_TYPE) != 0) {
415f2d4f7b0Sopenharmony_ci        LOG_INFO("not debug app, return ok");
416f2d4f7b0Sopenharmony_ci        return V_OK;
417f2d4f7b0Sopenharmony_ci    }
418f2d4f7b0Sopenharmony_ci    LOG_PRINT_STR("devid type: %s", pf->debugInfo.devIdType);
419f2d4f7b0Sopenharmony_ci    int32_t ret;
420f2d4f7b0Sopenharmony_ci    if (strcmp(pf->debugInfo.devIdType, "udid") == 0) {
421f2d4f7b0Sopenharmony_ci        ret = VerifyUdid(pf);
422f2d4f7b0Sopenharmony_ci    } else {
423f2d4f7b0Sopenharmony_ci        LOG_ERROR("devid type invalid");
424f2d4f7b0Sopenharmony_ci        ret = V_ERR;
425f2d4f7b0Sopenharmony_ci    }
426f2d4f7b0Sopenharmony_ci    return ret;
427f2d4f7b0Sopenharmony_ci}
428f2d4f7b0Sopenharmony_ci
429f2d4f7b0Sopenharmony_ciint32_t VerifyProfileContent(const ProfileProf *pf)
430f2d4f7b0Sopenharmony_ci{
431f2d4f7b0Sopenharmony_ci    P_NULL_RETURN_WTTH_LOG(pf);
432f2d4f7b0Sopenharmony_ci    int32_t ret = VerifyAppTypeAndDistribution(pf);
433f2d4f7b0Sopenharmony_ci    if (ret != V_OK) {
434f2d4f7b0Sopenharmony_ci        LOG_PRINT_STR("invalid profile distribution type : %s", pf->appDistType);
435f2d4f7b0Sopenharmony_ci        return V_ERR_INVALID_DISP_TYPE;
436f2d4f7b0Sopenharmony_ci    }
437f2d4f7b0Sopenharmony_ci    ret = VerifyAppBundleInfo(pf);
438f2d4f7b0Sopenharmony_ci    if (ret != V_OK) {
439f2d4f7b0Sopenharmony_ci        LOG_ERROR("invalid profile app bundle info");
440f2d4f7b0Sopenharmony_ci        return V_ERR_INVALID_APP_BUNDLE;
441f2d4f7b0Sopenharmony_ci    }
442f2d4f7b0Sopenharmony_ci    /* verify debug device id */
443f2d4f7b0Sopenharmony_ci    ret = VerifyDebugInfo(pf);
444f2d4f7b0Sopenharmony_ci    if (ret != V_OK) {
445f2d4f7b0Sopenharmony_ci        LOG_ERROR("validate debug info");
446f2d4f7b0Sopenharmony_ci        return V_ERR_INVALID_DEVID;
447f2d4f7b0Sopenharmony_ci    }
448f2d4f7b0Sopenharmony_ci    return V_OK;
449f2d4f7b0Sopenharmony_ci}
450