• 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_PAKE_) || defined(_CUT_PAKE_SERVER_))
17 
18 #include "securec.h"
19 #include "log.h"
20 #include "mem_stat.h"
21 #include "jsonutil.h"
22 #include "pake_server.h"
23 #include "key_agreement_version.h"
24 #include "parsedata.h"
25 
parse_pake_request(const char * payload,enum json_object_data_type data_type)26 void *parse_pake_request(const char *payload, enum json_object_data_type data_type)
27 {
28     struct pake_start_request_data *pake_request =
29         (struct pake_start_request_data *)MALLOC(sizeof(struct pake_start_request_data));
30     if (pake_request == NULL) {
31         return NULL;
32     }
33     (void)memset_s(pake_request, sizeof(*pake_request), 0, sizeof(*pake_request));
34     json_handle obj = parse_payload(payload, data_type);
35     if (obj == NULL) {
36         LOGE("Parse Pake Request parse payload failed");
37         goto error;
38     }
39     /* op */
40     int32_t op = get_json_int(obj, FIELD_OPERATION_CODE);
41     if (op == -1) {
42         LOGE("Parse Pake Request failed, field is null in operationCode");
43         goto error;
44     }
45     pake_request->operation_code = op;
46     /* version */
47     json_pobject obj_ver = get_json_obj(obj, FIELD_VERSION);
48     bool ret = parse_version(obj_ver, &pake_request->peer_version, &pake_request->peer_support_version);
49     if (!ret) {
50         LOGE("Parse Pake Request failed, field is null in version");
51         goto error;
52     }
53 
54     /* support 256 mod */
55     int32_t support_256_mod = get_json_bool(obj, FIELD_SUPPORT_256_MOD);
56     if (support_256_mod == 1) {
57         pake_request->epk_len = HC_BIG_PRIME_MAX_LEN;
58     } else {
59         pake_request->epk_len = HC_BIG_PRIME_MAX_LEN_384;
60     }
61 
62     free_payload(obj, data_type);
63     return (void *)pake_request;
64 error:
65     free_payload(obj, data_type);
66     FREE(pake_request);
67     return NULL;
68 }
69 
free_pake_request(void * obj)70 void free_pake_request(void *obj)
71 {
72     if (obj != NULL) {
73         FREE(obj);
74     }
75 }
76 
make_pake_request(void * data)77 char *make_pake_request(void *data)
78 {
79     struct pake_start_request_data *pake_request = data;
80     char *ret_str = (char *)MALLOC(RET_STR_LENGTH);
81     if (ret_str == NULL) {
82         return NULL;
83     }
84     (void)memset_s(ret_str, RET_STR_LENGTH, 0, RET_STR_LENGTH);
85     if (snprintf_s(ret_str, RET_STR_LENGTH, RET_STR_LENGTH - 1,
86         "{\"%s\":%d,\"%s\":{\"%s\":{\"%s\":\"%u.%u.%u\",\"%s\":\"%u.%u.%u\"},\"%s\":true,\"%s\":%d}}",
87         FIELD_MESSAGE, PAKE_REQUEST, FIELD_PAYLOAD, FIELD_VERSION, FIELD_CURRENT_VERSION,
88         pake_request->peer_version.first, pake_request->peer_version.second,
89         pake_request->peer_version.third, FIELD_MIN_VERSION,
90         pake_request->peer_support_version.first, pake_request->peer_support_version.second,
91         pake_request->peer_support_version.third, FIELD_SUPPORT_256_MOD,
92         FIELD_OPERATION_CODE, pake_request->operation_code) < 0) {
93         LOGE("String generate failed");
94         FREE(ret_str);
95         ret_str = NULL;
96     }
97     return ret_str;
98 }
99 
100 #else /* _CUT_XXX_ */
101 
102 #include "parsedata.h"
103 DEFINE_EMPTY_STRUCT_FUNC(pake_request)
104 
105 #endif /* _CUT_XXX_ */
106