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 #if !(defined(_CUT_STS_) || defined(_CUT_STS_SERVER_) || defined(_CUT_REMOVE_) || defined(_CUT_REMOVE_SERVER_))
17 
18 #include "remove_auth_info.h"
19 #include "log.h"
20 #include "base.h"
21 #include "mem_stat.h"
22 #include "parsedata.h"
23 #include "huks_adapter.h"
24 
25 #define HICHAIN_REMOVE_INFO_REQUEST  "hichain_remove_info_request"
26 #define HICHAIN_REMOVE_INFO_RESPONSE "hichain_remove_info_response"
27 
28 static int32_t parse_remove_request_data(const struct hichain *hichain, remove_request_data *receive,
29                                          struct auth_info_cache *cache);
30 static int32_t build_remove_response_data(const struct hichain *hichain, remove_response_data *send);
31 static int32_t delete_auth_info(const struct hichain *hichain, int32_t user_type, struct hc_auth_id *auth_id);
send_remove_response(const struct hichain * hichain,struct message * receive,struct message * send)32 int32_t send_remove_response(const struct hichain *hichain, struct message *receive, struct message *send)
33 {
34     remove_response_data *send_data = malloc_auth_info_msg(HC_AUTH_RESPONSE_LEN);
35     if (send_data == NULL) {
36         LOGE("Malloc struct remove_response_data failed");
37         send->msg_code = INFORM_MESSAGE;
38         return HC_MALLOC_FAILED;
39     }
40     struct auth_info_cache cache;
41     int32_t ret = parse_remove_request_data(hichain, (remove_request_data *)receive->payload, &cache);
42     if (ret != HC_OK) {
43         LOGE("Parse remove request failed, error code is %d", ret);
44         goto error;
45     }
46     ret = build_remove_response_data(hichain, send_data);
47     if (ret != HC_OK) {
48         LOGE("Build remove response failed, error code is %d", ret);
49         goto error;
50     }
51     ret = delete_auth_info(hichain, cache.user_type, &cache.auth_id);
52     if (ret != HC_OK) {
53         LOGE("Delete ltpk failed, error code is %d", ret);
54         goto error;
55     }
56 
57     DBG_OUT("Send remove response success");
58     send->payload = send_data;
59     send->msg_code = REMOVE_AUTHINFO_RESPONSE;
60     return HC_OK;
61 
62 error:
63     free_auth_info_msg(send_data);
64     send->msg_code = INFORM_MESSAGE;
65     return ret;
66 }
67 
68 static int32_t get_field_from_request_payload(struct uint8_buff *payload, int32_t *user_type,
69     struct hc_auth_id *auth_id);
parse_remove_request_data(const struct hichain * hichain,remove_request_data * receive,struct auth_info_cache * cache)70 static int32_t parse_remove_request_data(const struct hichain *hichain, remove_request_data *receive,
71     struct auth_info_cache *cache)
72 {
73     const struct sts_session_key *session_key = get_sts_session_key(hichain);
74     if (session_key == NULL) {
75         LOGE("Get sts session key failed");
76         return HC_STS_OBJECT_ERROR;
77     }
78     struct uint8_buff plain = { 0, 0, 0 };
79     int32_t ret = decrypt_payload((const struct var_buffer *)session_key, &receive->cipher,
80                                   HICHAIN_REMOVE_INFO_REQUEST, &plain);
81     if (ret != HC_OK) {
82         LOGE("Decrypt remove request payload failed");
83         return ret;
84     }
85     ret = get_field_from_request_payload(&plain, &cache->user_type, &cache->auth_id);
86     (void)memset_s(plain.val, plain.size, 0, plain.size);
87     FREE(plain.val);
88     plain.val = NULL;
89     if (ret != HC_OK) {
90         LOGE("Get field from remove request message failed, error code is %d", ret);
91         return ret;
92     }
93 
94     DBG_OUT("Save ltpk success");
95     return HC_OK;
96 }
97 
build_remove_response_data(const struct hichain * hichain,remove_response_data * send)98 static int32_t build_remove_response_data(const struct hichain *hichain, remove_response_data *send)
99 {
100     uint8_t array[sizeof(int32_t)] = {0};
101     struct uint8_buff plain = {
102         .val = array,
103         .size = sizeof(array),
104         .length = sizeof(array)
105     };
106 
107     const struct sts_session_key *key = get_sts_session_key(hichain);
108     int32_t ret = encrypt_payload((const struct var_buffer *)key, &plain,
109                                   HICHAIN_REMOVE_INFO_RESPONSE, &send->cipher);
110     if (ret != HC_OK) {
111         LOGE("Encrypt remove response failed, error code is %d", ret);
112         return HC_ENCRYPT_FAILED;
113     }
114 
115     return HC_OK;
116 }
117 
delete_auth_info(const struct hichain * hichain,int32_t user_type,struct hc_auth_id * auth_id)118 static int32_t delete_auth_info(const struct hichain *hichain, int32_t user_type, struct hc_auth_id *auth_id)
119 {
120     struct service_id service_id = generate_service_id(&hichain->identity);
121     if (service_id.length == 0) {
122         LOGE("Generate service id failed");
123         return HC_GEN_SERVICE_ID_FAILED;
124     }
125     enum huks_key_alias_type alias_type = (user_type == HC_USER_TYPE_ACCESSORY) ?
126                                            KEY_ALIAS_ACCESSOR_PK : KEY_ALIAS_CONTROLLER_PK;
127     struct hc_key_alias alias = generate_key_alias(&service_id, auth_id, alias_type);
128     if (alias.length == 0) {
129         LOGE("Generate key alias failed");
130         return HC_GEN_ALIAS_FAILED;
131     }
132     int32_t ret = check_lt_public_key_exist(&alias);
133     if (ret == HC_OK) {
134         ret = delete_lt_public_key(&alias);
135         if (ret != HC_OK) {
136             LOGE("Save ltpk failed, error code is %d", ret);
137             return HC_SAVE_LTPK_FAILED;
138         }
139     }
140     DBG_OUT("Save ltpk success");
141     (void)user_type;
142     return HC_OK;
143 }
144 
get_field_from_request_payload(struct uint8_buff * payload,int32_t * user_type,struct hc_auth_id * auth_id)145 static int32_t get_field_from_request_payload(struct uint8_buff *payload, int32_t *user_type,
146     struct hc_auth_id *auth_id)
147 {
148     struct remove_auth_data *rmv_auth_data = parse_rmv_auth_info_data((char *)payload->val, JSON_STRING_DATA);
149     if (rmv_auth_data == NULL) {
150         LOGE("ParseRmvAuthInfoData failed");
151         return HC_DECRYPT_FAILED;
152     }
153     *user_type = rmv_auth_data->user_type;
154     *auth_id = rmv_auth_data->auth_id;
155     free_rmv_auth_info_data(rmv_auth_data);
156     return HC_OK;
157 }
158 
159 #endif /* _CUT_XXX_ */
160