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