• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     FREE(plain.val);
87     plain.val = NULL;
88     if (ret != HC_OK) {
89         LOGE("Get field from remove request message failed, error code is %d", ret);
90         return ret;
91     }
92 
93     DBG_OUT("Save ltpk success");
94     return HC_OK;
95 }
96 
build_remove_response_data(const struct hichain * hichain,remove_response_data * send)97 static int32_t build_remove_response_data(const struct hichain *hichain, remove_response_data *send)
98 {
99     uint8_t array[sizeof(int32_t)] = {0};
100     struct uint8_buff plain = {
101         .val = array,
102         .size = sizeof(array),
103         .length = sizeof(array)
104     };
105 
106     const struct sts_session_key *key = get_sts_session_key(hichain);
107     int32_t ret = encrypt_payload((const struct var_buffer *)key, &plain,
108                                   HICHAIN_REMOVE_INFO_RESPONSE, &send->cipher);
109     if (ret != HC_OK) {
110         LOGE("Encrypt remove response failed, error code is %d", ret);
111         return HC_ENCRYPT_FAILED;
112     }
113 
114     return HC_OK;
115 }
116 
delete_auth_info(const struct hichain * hichain,int32_t user_type,struct hc_auth_id * auth_id)117 static int32_t delete_auth_info(const struct hichain *hichain, int32_t user_type, struct hc_auth_id *auth_id)
118 {
119     struct service_id service_id = generate_service_id(&hichain->identity);
120     if (service_id.length == 0) {
121         LOGE("Generate service id failed");
122         return HC_GEN_SERVICE_ID_FAILED;
123     }
124     enum huks_key_alias_type alias_type = (user_type == HC_USER_TYPE_ACCESSORY) ?
125                                            KEY_ALIAS_ACCESSOR_PK : KEY_ALIAS_CONTROLLER_PK;
126     struct hc_key_alias alias = generate_key_alias(&service_id, auth_id, alias_type);
127     if (alias.length == 0) {
128         LOGE("Generate key alias failed");
129         return HC_GEN_ALIAS_FAILED;
130     }
131     int32_t ret = check_lt_public_key_exist(&alias);
132     if (ret == HC_OK) {
133         ret = delete_lt_public_key(&alias);
134         if (ret != HC_OK) {
135             LOGE("Save ltpk failed, error code is %d", ret);
136             return HC_SAVE_LTPK_FAILED;
137         }
138     }
139     DBG_OUT("Save ltpk success");
140     (void)user_type;
141     return HC_OK;
142 }
143 
get_field_from_request_payload(struct uint8_buff * payload,int32_t * user_type,struct hc_auth_id * auth_id)144 static int32_t get_field_from_request_payload(struct uint8_buff *payload, int32_t *user_type,
145     struct hc_auth_id *auth_id)
146 {
147     struct remove_auth_data *rmv_auth_data = parse_rmv_auth_info_data((char *)payload->val, JSON_STRING_DATA);
148     if (rmv_auth_data == NULL) {
149         LOGE("ParseRmvAuthInfoData failed");
150         return HC_DECRYPT_FAILED;
151     }
152     *user_type = rmv_auth_data->user_type;
153     *auth_id = rmv_auth_data->auth_id;
154     free_rmv_auth_info_data(rmv_auth_data);
155     return HC_OK;
156 }
157 
158 #endif /* _CUT_XXX_ */
159