1 /*
2 * Copyright (C) 2023 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 "napi_dh_key_util.h"
17 #include "securec.h"
18 #include "detailed_ecc_key_params.h"
19 #include "log.h"
20
21 #include "napi_crypto_framework_defines.h"
22 #include "napi_utils.h"
23 #include "napi_key_pair.h"
24 #include "napi_pri_key.h"
25 #include "napi_pub_key.h"
26 #include "utils.h"
27
28 namespace OHOS {
29 namespace CryptoFramework {
NapiDHKeyUtil()30 NapiDHKeyUtil::NapiDHKeyUtil() {}
31
~NapiDHKeyUtil()32 NapiDHKeyUtil::~NapiDHKeyUtil() {}
33
34
BuildDhInstanceToNapiValueSub(napi_env env,HcfDhCommParamsSpec * blob,napi_value * instance)35 static bool BuildDhInstanceToNapiValueSub(napi_env env, HcfDhCommParamsSpec *blob, napi_value *instance)
36 {
37 if (!BuildSetNamedProperty(env, &(blob->p), "p", instance)) {
38 LOGE("build setNamedProperty a failed!");
39 return false;
40 }
41 if (!BuildSetNamedProperty(env, &(blob->g), "g", instance)) {
42 LOGE("build setNamedProperty b failed!");
43 return false;
44 }
45 napi_value length;
46 napi_status status = napi_create_int32(env, blob->length, &length);
47 if (status != napi_ok) {
48 LOGE("create length number failed!");
49 return false;
50 }
51 status = napi_set_named_property(env, *instance, "l", length);
52 if (status != napi_ok) {
53 LOGE("create length number failed!");
54 return false;
55 }
56 return true;
57 }
58
BuildDhInstanceToNapiValue(napi_env env,HcfDhCommParamsSpec * blob,napi_value * instance)59 static bool BuildDhInstanceToNapiValue(napi_env env, HcfDhCommParamsSpec *blob, napi_value *instance)
60 {
61 napi_value algName;
62 size_t algNameLength = HcfStrlen(blob->base.algName);
63 if (!algNameLength) {
64 LOGE("algName is empty!");
65 return false;
66 }
67 napi_status status = napi_create_string_utf8(env, blob->base.algName, algNameLength, &algName);
68 if (status != napi_ok) {
69 LOGE("create algName failed!");
70 return false;
71 }
72 napi_value specType;
73 status = napi_create_uint32(env, blob->base.specType, &specType);
74 if (status != napi_ok) {
75 LOGE("create uint32 failed!");
76 return false;
77 }
78 status = napi_set_named_property(env, *instance, "algName", algName);
79 if (status != napi_ok) {
80 LOGE("create set algName failed!");
81 return false;
82 }
83 status = napi_set_named_property(env, *instance, "specType", specType);
84 if (status != napi_ok) {
85 LOGE("create set specType failed!");
86 return false;
87 }
88 if (!BuildDhInstanceToNapiValueSub(env, blob, instance)) {
89 LOGE("create intance parter napi value failed!");
90 return false;
91 }
92 return true;
93 }
94
CheckDhCommonParamSpec(napi_env env,HcfDhCommParamsSpec * blob)95 static bool CheckDhCommonParamSpec(napi_env env, HcfDhCommParamsSpec *blob)
96 {
97 if (blob == nullptr) {
98 LOGE("Invalid blob!");
99 return false;
100 }
101 if (blob->base.algName == nullptr) {
102 LOGE("Invalid blob algName!");
103 return false;
104 }
105 if (blob->p.data == nullptr || blob->p.len == 0) {
106 LOGE("Invalid blob a!");
107 return false;
108 }
109 if (blob->g.data == nullptr || blob->g.len == 0) {
110 LOGE("Invalid blob point x!");
111 return false;
112 }
113 return true;
114 }
115
ConvertDhCommParamsSpecToNapiValue(napi_env env,HcfDhCommParamsSpec * blob)116 static napi_value ConvertDhCommParamsSpecToNapiValue(napi_env env, HcfDhCommParamsSpec *blob)
117 {
118 if (!CheckDhCommonParamSpec(env, blob)) {
119 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "Invalid blob!"));
120 LOGE("Invalid blob!");
121 return NapiGetNull(env);
122 }
123 napi_value instance;
124 napi_status status = napi_create_object(env, &instance);
125 if (status != napi_ok) {
126 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "create object failed!"));
127 LOGE("create object failed!");
128 return NapiGetNull(env);
129 }
130 if (!BuildDhInstanceToNapiValue(env, blob, &instance)) {
131 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build object failed!"));
132 LOGE("Build object failed!");
133 return NapiGetNull(env);
134 }
135 return instance;
136 }
137
JsGenDHCommonParamsSpec(napi_env env,napi_callback_info info)138 napi_value NapiDHKeyUtil::JsGenDHCommonParamsSpec(napi_env env, napi_callback_info info)
139 {
140 size_t expectedArgc = PARAMS_NUM_TWO;
141 size_t argc = ARGS_SIZE_TWO;
142 napi_value argv[ARGS_SIZE_TWO] = { nullptr };
143 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
144
145 if ((argc != expectedArgc) && (argc != (expectedArgc - 1))) {
146 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "The input args num is invalid."));
147 LOGE("The input args num is invalid.");
148 return nullptr;
149 }
150
151 int32_t pLen = 0;
152 if (!GetInt32FromJSParams(env, argv[0], pLen)) {
153 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get pLen."));
154 LOGE("failed to get pLen.");
155 return NapiGetNull(env);
156 }
157
158 int32_t skLen = 0;
159 if (argc == expectedArgc) {
160 if (!GetInt32FromJSParams(env, argv[1], skLen)) {
161 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get skLen."));
162 LOGE("failed to get skLen.");
163 return NapiGetNull(env);
164 }
165 }
166 HcfDhCommParamsSpec *dhCommParamsSpec = nullptr;
167 if (HcfDhKeyUtilCreate(pLen, skLen, &dhCommParamsSpec) != HCF_SUCCESS) {
168 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "create c generator fail."));
169 LOGE("create c generator fail.");
170 return NapiGetNull(env);
171 }
172
173 napi_value instance = ConvertDhCommParamsSpecToNapiValue(env, dhCommParamsSpec);
174 FreeDhCommParamsSpec(dhCommParamsSpec);
175 return instance;
176 }
177
DHKeyUtilConstructor(napi_env env,napi_callback_info info)178 napi_value NapiDHKeyUtil::DHKeyUtilConstructor(napi_env env, napi_callback_info info)
179 {
180 napi_value thisVar = nullptr;
181 size_t argc = ARGS_SIZE_ONE;
182 napi_value argv[ARGS_SIZE_ONE] = { nullptr };
183 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
184 return thisVar;
185 }
186
GenDHCommonParamSpec(napi_env env)187 napi_value NapiDHKeyUtil::GenDHCommonParamSpec(napi_env env)
188 {
189 napi_value cons = nullptr;
190 napi_property_descriptor clzDes[] = {
191 DECLARE_NAPI_STATIC_FUNCTION("genDHCommonParamsSpec", NapiDHKeyUtil::JsGenDHCommonParamsSpec),
192 };
193 NAPI_CALL(env, napi_define_class(env, "DHKeyUtil", NAPI_AUTO_LENGTH, NapiDHKeyUtil::DHKeyUtilConstructor,
194 nullptr, sizeof(clzDes) / sizeof(clzDes[0]), clzDes, &cons));
195 return cons;
196 }
197
DefineNapiDHKeyUtilJSClass(napi_env env,napi_value exports)198 void NapiDHKeyUtil::DefineNapiDHKeyUtilJSClass(napi_env env, napi_value exports)
199 {
200 napi_set_named_property(env, exports, "DHKeyUtil", NapiDHKeyUtil::GenDHCommonParamSpec(env));
201 }
202 } // CryptoFramework
203 } // OHOS
204