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 "key_agreement_version.h"
17 #include "securec.h"
18 #include "log.h"
19 #include "mem_stat.h"
20 #include "jsonutil.h"
21 #include "commonutil.h"
22 #include "base.h"
23 #include "parsedata.h"
24 
split_ver(char * tmp_ver,const char * sub_ver,struct key_agreement_version * cur_ver,char * next)25 static void split_ver(char *tmp_ver, const char *sub_ver, struct key_agreement_version *cur_ver, char *next)
26 {
27     sub_ver = strtok_s(tmp_ver, ".", &next);
28     if (sub_ver != NULL) {
29         cur_ver->first = strtoul(sub_ver, NULL, HC_VERSION_DEC); /* first */
30     }
31     sub_ver = strtok_s(NULL, ".", &next);
32     if (sub_ver != NULL) {
33         cur_ver->second = strtoul(sub_ver, NULL, HC_VERSION_DEC); /* second */
34     }
35     sub_ver = strtok_s(NULL, ".", &next);
36     if (sub_ver != NULL) {
37         cur_ver->third = strtoul(sub_ver, NULL, HC_VERSION_DEC); /* third */
38     }
39 }
40 
parse_version(json_pobject obj_ver,struct key_agreement_version * min_ver,struct key_agreement_version * cur_ver)41 bool parse_version(json_pobject obj_ver, struct key_agreement_version *min_ver, struct key_agreement_version *cur_ver)
42 {
43     const char *str_cur_ver = get_json_string(obj_ver, FIELD_CURRENT_VERSION); /* version---cur */
44     if (str_cur_ver == NULL) {
45         return false;
46     }
47     char *sub_cur_ver = NULL;
48     char *next = NULL;
49     int32_t len_cur_ver = strlen(str_cur_ver);
50     char *tmp_cur_ver = (char *)MALLOC(len_cur_ver + 1);
51     if (tmp_cur_ver == NULL) {
52         return false;
53     }
54     (void)memset_s(tmp_cur_ver, len_cur_ver + 1, 0, len_cur_ver + 1);
55     if (strcpy_s(tmp_cur_ver, len_cur_ver + 1, str_cur_ver) != EOK) {
56         FREE(tmp_cur_ver);
57         tmp_cur_ver = NULL;
58         return false;
59     }
60     split_ver(tmp_cur_ver, sub_cur_ver, min_ver, next);
61     FREE(tmp_cur_ver);
62     tmp_cur_ver = NULL;
63 
64     const char *str_min_ver = get_json_string(obj_ver, FIELD_MIN_VERSION); /* version---min */
65     if (str_min_ver == NULL) {
66         return false;
67     }
68     char *sub_min_ver = NULL;
69     int32_t len_min_ver = strlen(str_min_ver);
70     char *tmp_min_ver = (char *)MALLOC(len_min_ver + 1);
71     if (tmp_min_ver == NULL) {
72         return false;
73     }
74     (void)memset_s(tmp_min_ver, len_min_ver + 1, 0, len_min_ver + 1);
75     if (strcpy_s(tmp_min_ver, len_min_ver + 1, str_min_ver) != EOK) {
76         FREE(tmp_min_ver);
77         tmp_min_ver = NULL;
78         return false;
79     }
80     split_ver(tmp_min_ver, sub_min_ver, cur_ver, next);
81     FREE(tmp_min_ver);
82     tmp_min_ver = NULL;
83     return true;
84 }
85 
raw_byte_to_hex_string(const uint8_t * hex,int32_t length)86 uint8_t *raw_byte_to_hex_string(const uint8_t *hex, int32_t length)
87 {
88     int32_t tmp_hex_len = length * BYTE_TO_HEX_OPER_LENGTH + sizeof("");
89     uint8_t *tmp_cha_data_hex = (uint8_t *)MALLOC(tmp_hex_len);
90     if (tmp_cha_data_hex == NULL) {
91         return NULL;
92     }
93     (void)memset_s(tmp_cha_data_hex, tmp_hex_len, 0, tmp_hex_len);
94     byte_to_hex_string(hex, length, tmp_cha_data_hex, length * BYTE_TO_HEX_OPER_LENGTH);
95     return tmp_cha_data_hex;
96 }
97