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 "add_auth_info_client.h"
17 #include "securec.h"
18 #include "huks_adapter.h"
19 #include "log.h"
20 #include "mem_stat.h"
21
22 #if !(defined(_CUT_STS_) || defined(_CUT_STS_CLIENT_) || defined(_CUT_ADD_) || defined(_CUT_ADD_CLIENT_))
23
send_add_request(const struct hichain * hichain,struct message * send)24 int32_t send_add_request(const struct hichain *hichain, struct message *send)
25 {
26 struct add_auth_info_client auth_info_client;
27 auth_info_client.sts_client = hichain->sts_client;
28 auth_info_client.auth_info = hichain->auth_info;
29
30 add_request_data *request_data = (add_request_data *)MALLOC(sizeof(add_request_data));
31 if (request_data == NULL) {
32 LOGE("Malloc add request data failed");
33 return HC_MALLOC_FAILED;
34 }
35 (void)memset_s(request_data, sizeof(*request_data), 0, sizeof(*request_data));
36
37 int32_t ret = build_add_request_data(&auth_info_client, request_data);
38 if (ret != HC_OK) {
39 LOGE("Build add request data failed. error code is %d", ret);
40 FREE(request_data);
41 return ret;
42 }
43
44 send->payload = request_data;
45 send->msg_code = ADD_AUTHINFO_REQUEST;
46
47 return HC_OK;
48 }
49
receive_add_response(const struct hichain * hichain,const struct message * receive)50 int32_t receive_add_response(const struct hichain *hichain, const struct message *receive)
51 {
52 struct add_auth_info_client auth_info_client;
53 auth_info_client.sts_client = hichain->sts_client;
54 auth_info_client.auth_info = hichain->auth_info;
55
56 int32_t ret = parse_add_response_data(&auth_info_client, (add_response_data *)receive->payload);
57 if (ret != HC_OK) {
58 LOGE("Parse add response data failed. error code is %d", ret);
59 return ret;
60 }
61 return HC_OK;
62 }
63
build_add_request_data(struct add_auth_info_client * auth_info_client,add_request_data * send)64 int32_t build_add_request_data(struct add_auth_info_client *auth_info_client, add_request_data *send)
65 {
66 struct service_id service_id = generate_service_id(auth_info_client->sts_client->identity);
67 enum huks_key_alias_type alias_type = (auth_info_client->auth_info->user_type == HC_USER_TYPE_ACCESSORY) ?
68 KEY_ALIAS_ACCESSOR_PK : KEY_ALIAS_CONTROLLER_PK;
69 struct hc_key_alias alias = generate_key_alias(&service_id,
70 &auth_info_client->auth_info->auth_id,
71 alias_type);
72 struct ltpk lt_public_key = { 0, {0} };
73 int32_t ret = export_lt_public_key(&alias, <_public_key);
74 if (ret != HC_OK) {
75 LOGE("Generate ltpk return value is %d", ret);
76 return HC_GET_LTPK_FAILED;
77 }
78
79 struct add_auth_data auth_data;
80 auth_data.user_type = auth_info_client->auth_info->user_type;
81 auth_data.ltpk = lt_public_key;
82 auth_data.auth_id = auth_info_client->auth_info->auth_id;
83
84 uint8_t *tmp = (uint8_t *)make_add_auth_info_data((void *)&auth_data);
85 if (tmp == NULL) {
86 LOGE("Make add auth info data failed");
87 return HC_BUILD_SEND_DATA_FAILED;
88 }
89
90 struct uint8_buff plain = { NULL, 0, 0 };
91 plain.val = tmp;
92 plain.length = strlen((const char *)tmp);
93 plain.size = plain.length + 1;
94
95 struct uint8_buff payload = { NULL, 0, 0 };
96 payload.val = (uint8_t *)MALLOC(HC_ADD_AUTH_DATA_BUFF_LEN);
97 if (payload.val == NULL) {
98 LOGE("Malloc payload val failed");
99 FREE(tmp);
100 return HC_MALLOC_FAILED;
101 }
102 payload.size = HC_ADD_AUTH_DATA_BUFF_LEN;
103 payload.length = 0;
104
105 ret = encrypt_payload((struct var_buffer *)&auth_info_client->sts_client->session_key,
106 &plain, HC_ADD_INFO_REQUEST, &payload);
107 FREE(tmp);
108 if (ret != HC_OK) {
109 LOGE("Decrypt exchange request payload failed");
110 FREE(payload.val);
111 return ret;
112 }
113
114 send->cipher = payload;
115 return HC_OK;
116 }
117
parse_add_response_data(struct add_auth_info_client * auth_info_client,const add_response_data * receive)118 int32_t parse_add_response_data(struct add_auth_info_client *auth_info_client, const add_response_data *receive)
119 {
120 struct uint8_buff plain = { NULL, 0, 0 };
121 int32_t ret = decrypt_payload((struct var_buffer *)&auth_info_client->sts_client->session_key,
122 &receive->cipher, HC_ADD_INFO_RESPONSE, &plain);
123 if (ret != HC_OK) {
124 FREE(plain.val);
125 LOGE("Decrypt add request payload failed");
126 return ret;
127 }
128
129 ret = plain.val[0];
130 FREE(plain.val);
131 if (ret != HC_OK) {
132 LOGE("Get decrypt plain value failed, ret: %d", ret);
133 return ret;
134 }
135
136 return HC_OK;
137 }
138
139 #else /* _CUT_XXX_ */
send_add_request(const struct hichain * hichain,struct message * send)140 int32_t send_add_request(const struct hichain *hichain, struct message *send)
141 {
142 LOGE("Donot support sts client for add");
143 (void)hichain;
144 (void)send;
145 return HC_UNSUPPORT;
146 }
147 #endif /* _CUT_XXX_ */
148