1 /*
2 * Copyright (C) 2023-2024 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_pri_key.h"
17
18 #include "log.h"
19 #include "memory.h"
20 #include "napi_crypto_framework_defines.h"
21 #include "napi_utils.h"
22 #include "securec.h"
23 #include "key.h"
24
25 namespace OHOS {
26 namespace CryptoFramework {
27 thread_local napi_ref NapiPriKey::classRef_ = nullptr;
28
NapiPriKey(HcfPriKey * priKey)29 NapiPriKey::NapiPriKey(HcfPriKey *priKey) : NapiKey(reinterpret_cast<HcfKey *>(priKey)) {}
30
~NapiPriKey()31 NapiPriKey::~NapiPriKey() {}
32
GetPriKey()33 HcfPriKey *NapiPriKey::GetPriKey()
34 {
35 return reinterpret_cast<HcfPriKey *>(NapiKey::GetHcfKey());
36 }
37
PriKeyConstructor(napi_env env,napi_callback_info info)38 napi_value NapiPriKey::PriKeyConstructor(napi_env env, napi_callback_info info)
39 {
40 napi_value thisVar = nullptr;
41 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
42 return thisVar;
43 }
44
FreeEncodeParamsSpec(HcfParamsSpec * paramsSpec)45 static void FreeEncodeParamsSpec(HcfParamsSpec *paramsSpec)
46 {
47 if (paramsSpec == nullptr) {
48 return;
49 }
50 HcfKeyEncodingParamsSpec *spec = reinterpret_cast<HcfKeyEncodingParamsSpec *>(paramsSpec);
51 if (spec->password != nullptr) {
52 size_t pwdLen = strlen(spec->password);
53 (void)memset_s((void*)spec->password, pwdLen, 0, pwdLen);
54 HcfFree(static_cast<void *>(spec->password));
55 spec->password = nullptr;
56 }
57 if (spec->cipher != nullptr) {
58 HcfFree(static_cast<void *>(spec->cipher));
59 spec->cipher = nullptr;
60 }
61 HcfFree(paramsSpec);
62 paramsSpec = nullptr;
63 }
64
ConvertToJsPriKey(napi_env env)65 napi_value NapiPriKey::ConvertToJsPriKey(napi_env env)
66 {
67 napi_value instance;
68 napi_value constructor = nullptr;
69 napi_get_reference_value(env, classRef_, &constructor);
70 napi_new_instance(env, constructor, 0, nullptr, &instance);
71
72 const char *algName = this->GetPriKey()->base.getAlgorithm(&(this->GetPriKey()->base));
73 const char *format = this->GetPriKey()->base.getFormat(&(this->GetPriKey()->base));
74
75 napi_value napiAlgName = nullptr;
76 napi_create_string_utf8(env, algName, NAPI_AUTO_LENGTH, &napiAlgName);
77 napi_set_named_property(env, instance, CRYPTO_TAG_ALG_NAME.c_str(), napiAlgName);
78
79 napi_value napiFormat = nullptr;
80 napi_create_string_utf8(env, format, NAPI_AUTO_LENGTH, &napiFormat);
81 napi_set_named_property(env, instance, CRYPTO_TAG_FORMAT.c_str(), napiFormat);
82 return instance;
83 }
84
JsGetEncoded(napi_env env,napi_callback_info info)85 napi_value NapiPriKey::JsGetEncoded(napi_env env, napi_callback_info info)
86 {
87 napi_value thisVar = nullptr;
88 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
89 NapiPriKey *napiPriKey = nullptr;
90 napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiPriKey));
91 if (status != napi_ok || napiPriKey == nullptr) {
92 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napiPriKey obj!"));
93 LOGE("failed to unwrap napiPriKey obj!");
94 return nullptr;
95 }
96
97 HcfPriKey *priKey = napiPriKey->GetPriKey();
98 if (priKey == nullptr) {
99 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get priKey obj!"));
100 LOGE("failed to get priKey obj!");
101 return nullptr;
102 }
103
104 HcfBlob returnBlob;
105 HcfResult res = priKey->base.getEncoded(&priKey->base, &returnBlob);
106 if (res != HCF_SUCCESS) {
107 napi_throw(env, GenerateBusinessError(env, res, "c getEncoded fail."));
108 LOGD("[error] c getEncoded fail.");
109 return nullptr;
110 }
111
112 napi_value instance = ConvertBlobToNapiValue(env, &returnBlob);
113 if (instance == nullptr) {
114 HcfBlobDataFree(&returnBlob);
115 napi_throw(env, GenerateBusinessError(env, res, "covert blob to napi value failed."));
116 LOGE("covert blob to napi value failed.");
117 return nullptr;
118 }
119 HcfBlobDataClearAndFree(&returnBlob);
120 return instance;
121 }
122
ValidateAndGetParams(napi_env env,napi_callback_info info,std::string & format,HcfParamsSpec ** paramsSpec,NapiPriKey ** napiPriKey)123 static bool ValidateAndGetParams(napi_env env, napi_callback_info info, std::string &format,
124 HcfParamsSpec **paramsSpec, NapiPriKey **napiPriKey)
125 {
126 size_t expectedArgc = PARAMS_NUM_TWO;
127 size_t argc = expectedArgc;
128 napi_value thisVar = nullptr;
129 napi_value argv[PARAMS_NUM_TWO] = { nullptr };
130 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
131 if ((argc != expectedArgc) && (argc != (expectedArgc - 1))) {
132 LOGE("The input args num is invalid.");
133 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "The input args num is invalid."));
134 return false;
135 }
136
137 if (!GetStringFromJSParams(env, argv[0], format)) {
138 LOGE("failed to get formatStr.");
139 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get formatStr."));
140 return false;
141 }
142
143 if (argc == expectedArgc) {
144 if (!GetEncodingParamsSpec(env, argv[1], paramsSpec)) {
145 LOGE("get params failed!");
146 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get napi paramsSpec failed!"));
147 return false;
148 }
149 }
150
151 napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(napiPriKey));
152 if (status != napi_ok || napiPriKey == nullptr) {
153 LOGE("failed to unwrap napiPriKey obj!");
154 FreeEncodeParamsSpec(*paramsSpec);
155 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napiPriKey obj!"));
156 return false;
157 }
158 return true;
159 }
160
JsGetEncodedPem(napi_env env,napi_callback_info info)161 napi_value NapiPriKey::JsGetEncodedPem(napi_env env, napi_callback_info info)
162 {
163 std::string format;
164 HcfParamsSpec *paramsSpec = nullptr;
165 NapiPriKey *napiPriKey = nullptr;
166 if (!ValidateAndGetParams(env, info, format, ¶msSpec, &napiPriKey)) {
167 return NapiGetNull(env);
168 }
169
170 HcfPriKey *priKey = napiPriKey->GetPriKey();
171 if (priKey == nullptr) {
172 FreeEncodeParamsSpec(paramsSpec);
173 LOGE("failed to get priKey obj!");
174 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get priKey obj!"));
175 return nullptr;
176 }
177
178 char *returnString = nullptr;
179 HcfResult res = priKey->getEncodedPem(priKey, paramsSpec, format.c_str(), &returnString);
180 if (res != HCF_SUCCESS) {
181 FreeEncodeParamsSpec(paramsSpec);
182 LOGE("getEncodedPem fail.");
183 napi_throw(env, GenerateBusinessError(env, res, "getEncodedPem fail."));
184 return nullptr;
185 }
186 napi_value instance = nullptr;
187 napi_create_string_utf8(env, returnString, NAPI_AUTO_LENGTH, &instance);
188 HcfFree(returnString);
189 FreeEncodeParamsSpec(paramsSpec);
190 return instance;
191 }
192
JsClearMem(napi_env env,napi_callback_info info)193 napi_value NapiPriKey::JsClearMem(napi_env env, napi_callback_info info)
194 {
195 napi_value thisVar = nullptr;
196 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
197 NapiPriKey *napiPriKey = nullptr;
198 napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiPriKey));
199 if (status != napi_ok || napiPriKey == nullptr) {
200 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napiPriKey obj!"));
201 LOGE("failed to unwrap napiPriKey obj!");
202 return nullptr;
203 }
204
205 HcfPriKey *priKey = napiPriKey->GetPriKey();
206 if (priKey == nullptr) {
207 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get priKey obj!"));
208 LOGE("failed to get priKey obj!");
209 return nullptr;
210 }
211
212 priKey->clearMem(priKey);
213 return nullptr;
214 }
215
GetAsyKeySpecBigInt(napi_env env,AsyKeySpecItem item,HcfPriKey * priKey)216 static napi_value GetAsyKeySpecBigInt(napi_env env, AsyKeySpecItem item, HcfPriKey *priKey)
217 {
218 HcfBigInteger returnBigInteger = { 0 };
219 HcfResult res = priKey->getAsyKeySpecBigInteger(priKey, item, &returnBigInteger);
220 if (res != HCF_SUCCESS) {
221 napi_throw(env, GenerateBusinessError(env, res, "C getAsyKeySpecBigInteger failed."));
222 LOGE("C getAsyKeySpecBigInteger failed.");
223 return nullptr;
224 }
225
226 napi_value instance = ConvertBigIntToNapiValue(env, &returnBigInteger);
227 (void)memset_s(returnBigInteger.data, returnBigInteger.len, 0, returnBigInteger.len);
228 HcfFree(returnBigInteger.data);
229 if (instance == nullptr) {
230 napi_throw(env, GenerateBusinessError(env, res, "covert bigInt to napi value failed."));
231 LOGE("covert bigInt to napi value failed.");
232 return nullptr;
233 }
234 return instance;
235 }
236
GetAsyKeySpecNumber(napi_env env,AsyKeySpecItem item,HcfPriKey * priKey)237 static napi_value GetAsyKeySpecNumber(napi_env env, AsyKeySpecItem item, HcfPriKey *priKey)
238 {
239 int returnInt = 0;
240 HcfResult res = priKey->getAsyKeySpecInt(priKey, item, &returnInt);
241 if (res != HCF_SUCCESS) {
242 napi_throw(env, GenerateBusinessError(env, res, "C getAsyKeySpecInt failed."));
243 LOGE("C getAsyKeySpecInt fail.");
244 return nullptr;
245 }
246
247 napi_value instance = nullptr;
248 napi_create_int32(env, returnInt, &instance);
249 return instance;
250 }
251
GetAsyKeySpecString(napi_env env,AsyKeySpecItem item,HcfPriKey * priKey)252 static napi_value GetAsyKeySpecString(napi_env env, AsyKeySpecItem item, HcfPriKey *priKey)
253 {
254 char *returnString = nullptr;
255 HcfResult res = priKey->getAsyKeySpecString(priKey, item, &returnString);
256 if (res != HCF_SUCCESS) {
257 napi_throw(env, GenerateBusinessError(env, res, "C getAsyKeySpecString failed."));
258 LOGE("c getAsyKeySpecString fail.");
259 return nullptr;
260 }
261
262 napi_value instance = nullptr;
263 napi_create_string_utf8(env, returnString, NAPI_AUTO_LENGTH, &instance);
264 HcfFree(returnString);
265 return instance;
266 }
267
JsGetAsyKeySpec(napi_env env,napi_callback_info info)268 napi_value NapiPriKey::JsGetAsyKeySpec(napi_env env, napi_callback_info info)
269 {
270 napi_value thisVar = nullptr;
271 NapiPriKey *napiPriKey = nullptr;
272 size_t expectedArgc = ARGS_SIZE_ONE;
273 size_t argc = ARGS_SIZE_ONE;
274 napi_value argv[ARGS_SIZE_ONE] = { nullptr };
275 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
276 if (argc != expectedArgc) {
277 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "JsGetAsyKeySpec fail, wrong argument num."));
278 LOGE("wrong argument num. require 1 arguments. [Argc]: %zu!", argc);
279 return nullptr;
280 }
281
282 AsyKeySpecItem item;
283 if (napi_get_value_uint32(env, argv[0], reinterpret_cast<uint32_t *>(&item)) != napi_ok) {
284 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "JsGetAsyKeySpec failed!"));
285 LOGE("JsGetAsyKeySpec failed!");
286 return nullptr;
287 }
288
289 napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiPriKey));
290 if (status != napi_ok || napiPriKey == nullptr) {
291 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napiPriKey obj!"));
292 LOGE("failed to unwrap napiPriKey obj!");
293 return nullptr;
294 }
295 HcfPriKey *priKey = napiPriKey->GetPriKey();
296 if (priKey == nullptr) {
297 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get priKey obj!"));
298 LOGE("failed to get priKey obj!");
299 return nullptr;
300 }
301 LOGD("prepare priKey ok.");
302
303 int32_t type = GetAsyKeySpecType(item);
304 if (type == SPEC_ITEM_TYPE_BIG_INT) {
305 return GetAsyKeySpecBigInt(env, item, priKey);
306 } else if (type == SPEC_ITEM_TYPE_NUM) {
307 return GetAsyKeySpecNumber(env, item, priKey);
308 } else if (type == SPEC_ITEM_TYPE_STR) {
309 return GetAsyKeySpecString(env, item, priKey);
310 } else {
311 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "AsyKeySpecItem not support!"));
312 return nullptr;
313 }
314 }
315
JsGetEncodedDer(napi_env env,napi_callback_info info)316 napi_value NapiPriKey::JsGetEncodedDer(napi_env env, napi_callback_info info)
317 {
318 napi_value thisVar = nullptr;
319 NapiPriKey *napiPriKey = nullptr;
320 size_t argc = ARGS_SIZE_ONE;
321 napi_value argv[ARGS_SIZE_ONE] = { nullptr };
322 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
323 if (argc != ARGS_SIZE_ONE) {
324 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "wrong argument num."));
325 LOGE("wrong argument num. require 1 arguments. [Argc]: %zu!", argc);
326 return nullptr;
327 }
328 std::string format;
329 if (!GetStringFromJSParams(env, argv[0], format)) {
330 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get format."));
331 LOGE("get format fail.");
332 return nullptr;
333 }
334 napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiPriKey));
335 if (status != napi_ok || napiPriKey == nullptr) {
336 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap private key obj!"));
337 LOGE("failed to unwrap private key obj!");
338 return nullptr;
339 }
340 HcfPriKey *priKey = napiPriKey->GetPriKey();
341 if (priKey == nullptr) {
342 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get private key obj!"));
343 LOGE("failed to get private key obj!");
344 return nullptr;
345 }
346 HcfBlob returnBlob = { .data = nullptr, .len = 0 };
347 HcfResult res = priKey->getEncodedDer(priKey, format.c_str(), &returnBlob);
348 if (res != HCF_SUCCESS) {
349 napi_throw(env, GenerateBusinessError(env, res, "get private key encodedDer fail."));
350 LOGE("get private key encodeDer fail.");
351 return nullptr;
352 }
353
354 napi_value instance = ConvertBlobToNapiValue(env, &returnBlob);
355 HcfBlobDataClearAndFree(&returnBlob);
356 return instance;
357 }
358
DefinePriKeyJSClass(napi_env env)359 void NapiPriKey::DefinePriKeyJSClass(napi_env env)
360 {
361 napi_property_descriptor classDesc[] = {
362 DECLARE_NAPI_FUNCTION("getEncoded", NapiPriKey::JsGetEncoded),
363 DECLARE_NAPI_FUNCTION("getEncodedDer", NapiPriKey::JsGetEncodedDer),
364 DECLARE_NAPI_FUNCTION("getEncodedPem", NapiPriKey::JsGetEncodedPem),
365 DECLARE_NAPI_FUNCTION("clearMem", NapiPriKey::JsClearMem),
366 DECLARE_NAPI_FUNCTION("getAsyKeySpec", NapiPriKey::JsGetAsyKeySpec),
367 };
368 napi_value constructor = nullptr;
369 napi_define_class(env, "PriKey", NAPI_AUTO_LENGTH, NapiPriKey::PriKeyConstructor, nullptr,
370 sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
371 napi_create_reference(env, constructor, 1, &classRef_);
372 }
373 } // CryptoFramework
374 } // OHOS
375