1 /*
2 * Copyright (c) 2020 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 "securec.h"
17 #include "log.h"
18 #include "mem_stat.h"
19 #include "jsonutil.h"
20 #include "commonutil.h"
21 #include "parsedata.h"
22 #include "add_auth_info.h"
23
24 #if (defined(_SUPPORT_SEC_CLONE_) || defined(_SUPPORT_SEC_CLONE_SERVER_))
25 static const int32_t g_key_type_position = 1;
26 #endif
27
parse_import_add_auth_data(const char * payload,enum json_object_data_type data_type)28 void *parse_import_add_auth_data(const char *payload, enum json_object_data_type data_type)
29 {
30 struct import_auth_data *signed_auth_data =
31 (struct import_auth_data *)MALLOC(sizeof(struct import_auth_data));
32 if (signed_auth_data == NULL) {
33 return NULL;
34 }
35 (void)memset_s(signed_auth_data, sizeof(*signed_auth_data), 0, sizeof(*signed_auth_data));
36 json_pobject obj = parse_payload(payload, data_type);
37 if (obj == NULL) {
38 LOGE("Parse Import Add Auth Info Data failed");
39 goto error;
40 }
41
42 /* publicKey */
43 int32_t result = byte_convert(obj, FIELD_PUBLIC_KEY, signed_auth_data->ltpk.ltpk,
44 &signed_auth_data->ltpk.length, HC_LT_PUBLIC_KEY_LEN);
45 if (result != HC_OK) {
46 LOGE("Parse Import Add Auth Info Data failed, field is wrong in publicKey");
47 goto error;
48 }
49
50 /* ownerId */
51 result = byte_convert(obj, FIELD_OWNER_ID, signed_auth_data->owner_id.auth_id,
52 &signed_auth_data->owner_id.length, HC_AUTH_ID_BUFF_LEN);
53 if (result != HC_OK) {
54 LOGE("Parse Import Add Auth Info Data failed, field is wrong in ownerId");
55 goto error;
56 }
57
58 /* authId */
59 result = byte_convert(obj, FIELD_AUTH_ID, signed_auth_data->auth_id.auth_id,
60 &signed_auth_data->auth_id.length, HC_AUTH_ID_BUFF_LEN);
61 if (result != HC_OK) {
62 LOGE("Parse Import Add Auth Info Data failed, field is wrong in authId");
63 goto error;
64 }
65
66 #if (defined(_SUPPORT_SEC_CLONE_) || defined(_SUPPORT_SEC_CLONE_SERVER_))
67 /* keyType */
68 uint8_t key_type[HC_KEY_TYPE_LEN] = {0};
69 uint32_t key_length;
70 result = byte_convert(obj, FIELD_KEY_TYPE, key_type, &key_length, HC_KEY_TYPE_LEN);
71 if (result != HC_OK) {
72 LOGE("Parse Import Add Auth Info Data failed, field is wrong in keyType");
73 goto error;
74 }
75
76 signed_auth_data->user_type = key_type[g_key_type_position];
77 #endif
78
79 free_payload(obj, data_type);
80 return (void *)signed_auth_data;
81 error:
82 free_payload(obj, data_type);
83 FREE(signed_auth_data);
84 return NULL;
85 }
86
free_import_add_auth_data(void * obj)87 void free_import_add_auth_data(void *obj)
88 {
89 if (obj != NULL) {
90 FREE(obj);
91 }
92 }
93
94