1 /*
2  * Copyright (c) 2022 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 "cm_napi_get_app_cert_info.h"
17 #include "cm_napi_get_app_cert_info_common.h"
18 
19 #include "securec.h"
20 
21 #include "cert_manager_api.h"
22 #include "cm_log.h"
23 #include "cm_mem.h"
24 #include "cm_type.h"
25 #include "cm_napi_common.h"
26 
27 namespace CMNapi {
28 namespace {
29 constexpr int CM_NAPI_GET_APP_CERT_INFO_MIN_ARGS = 1;
30 constexpr int CM_NAPI_GET_APP_CERT_INFO_MAX_ARGS = 2;
31 }  // namespace
32 
CreateGetAppCertInfoAsyncContext()33 GetAppCertInfoAsyncContext CreateGetAppCertInfoAsyncContext()
34 {
35     GetAppCertInfoAsyncContext context =
36         static_cast<GetAppCertInfoAsyncContext>(CmMalloc(sizeof(GetAppCertInfoAsyncContextT)));
37     if (context != nullptr) {
38         (void)memset_s(context, sizeof(GetAppCertInfoAsyncContextT), 0, sizeof(GetAppCertInfoAsyncContextT));
39     }
40     return context;
41 }
42 
DeleteGetAppCertInfoAsyncContext(napi_env env,GetAppCertInfoAsyncContext & context)43 void DeleteGetAppCertInfoAsyncContext(napi_env env, GetAppCertInfoAsyncContext &context)
44 {
45     if (context == nullptr) {
46         return;
47     }
48 
49     DeleteNapiContext(env, context->asyncWork, context->callback);
50 
51     if (context->keyUri != nullptr) {
52         FreeCmBlob(context->keyUri);
53     }
54 
55     if (context->credential != nullptr) {
56         FreeCredential(context->credential);
57     }
58 
59     CmFree(context);
60     context = nullptr;
61 }
62 
GetAppCertInfoParseParams(napi_env env,napi_callback_info info,GetAppCertInfoAsyncContext context)63 napi_value GetAppCertInfoParseParams(
64     napi_env env, napi_callback_info info, GetAppCertInfoAsyncContext context)
65 {
66     size_t argc = CM_NAPI_GET_APP_CERT_INFO_MAX_ARGS;
67     napi_value argv[CM_NAPI_GET_APP_CERT_INFO_MAX_ARGS] = { nullptr };
68     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
69 
70     if ((argc != CM_NAPI_GET_APP_CERT_INFO_MIN_ARGS) && (argc != CM_NAPI_GET_APP_CERT_INFO_MAX_ARGS)) {
71         ThrowError(env, PARAM_ERROR, "arguments count invalid, arguments count need between 1 and 2.");
72         CM_LOG_E("arguments count invalid. argc = %d", argc);
73         return nullptr;
74     }
75 
76     size_t index = 0;
77     napi_value result = ParseString(env, argv[index], context->keyUri);
78     if (result == nullptr) {
79         ThrowError(env, PARAM_ERROR, "keyUri is not a string or the length is 0 or too long.");
80         CM_LOG_E("could not get key uri");
81         return nullptr;
82     }
83 
84     index++;
85     if (index < argc) {
86         int32_t ret = GetCallback(env, argv[index], context->callback);
87         if (ret != CM_SUCCESS) {
88             ThrowError(env, PARAM_ERROR, "Get callback failed, callback must be a function.");
89             CM_LOG_E("get callback function faild when getting application cert info");
90             return nullptr;
91         }
92     }
93 
94     return GetInt32(env, 0);
95 }
96 
GetAppCertInfoWriteResult(napi_env env,GetAppCertInfoAsyncContext context)97 napi_value GetAppCertInfoWriteResult(napi_env env, GetAppCertInfoAsyncContext context)
98 {
99     napi_value result = nullptr;
100     NAPI_CALL(env, napi_create_object(env, &result));
101     napi_value certInfo = GenerateAppCertInfo(env, context->credential);
102     if (certInfo != nullptr) {
103         napi_set_named_property(env, result, CM_RESULT_PRPPERTY_CREDENTIAL.c_str(), certInfo);
104     } else {
105         NAPI_CALL(env, napi_get_undefined(env, &result));
106     }
107     return result;
108 }
109 
InitAppCert(struct Credential * credential)110 static void InitAppCert(struct Credential *credential)
111 {
112     credential->credData.data = static_cast<uint8_t *>(CmMalloc(MAX_LEN_CERTIFICATE_CHAIN));
113     if (credential->credData.data == nullptr) {
114         CM_LOG_E("malloc file buffer failed");
115         return;
116     }
117     (void)memset_s(credential->credData.data, MAX_LEN_CERTIFICATE_CHAIN, 0, MAX_LEN_CERTIFICATE_CHAIN);
118     credential->credData.size = MAX_LEN_CERTIFICATE_CHAIN;
119 }
120 
GetAppCertInfoAsyncWork(napi_env env,GetAppCertInfoAsyncContext asyncContext)121 napi_value GetAppCertInfoAsyncWork(napi_env env, GetAppCertInfoAsyncContext asyncContext)
122 {
123     napi_value promise = nullptr;
124     GenerateNapiPromise(env, asyncContext->callback, &asyncContext->deferred, &promise);
125 
126     napi_value resourceName = nullptr;
127     NAPI_CALL(env, napi_create_string_latin1(env, "GetAppCertInfoAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
128 
129     NAPI_CALL(env, napi_create_async_work(
130         env,
131         nullptr,
132         resourceName,
133         [](napi_env env, void *data) {
134             GetAppCertInfoAsyncContext context = static_cast<GetAppCertInfoAsyncContext>(data);
135 
136             context->credential = static_cast<struct Credential *>(CmMalloc(sizeof(struct Credential)));
137             if (context->credential != nullptr) {
138                 (void)memset_s(context->credential, sizeof(struct Credential), 0, sizeof(struct Credential));
139                 InitAppCert(context->credential);
140             }
141             context->result = CmGetAppCert(context->keyUri, context->store, context->credential);
142         },
143         [](napi_env env, napi_status status, void *data) {
144             GetAppCertInfoAsyncContext context = static_cast<GetAppCertInfoAsyncContext>(data);
145             napi_value result[RESULT_NUMBER] = { nullptr };
146             if (context->result == CM_SUCCESS) {
147                 NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0]));
148                 result[1] = GetAppCertInfoWriteResult(env, context);
149             } else {
150                 result[0] = GenerateBusinessError(env, context->result);
151                 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1]));
152             }
153             if (context->deferred != nullptr) {
154                 GeneratePromise(env, context->deferred, context->result, result, CM_ARRAY_SIZE(result));
155             } else {
156                 GenerateCallback(env, context->callback, result, CM_ARRAY_SIZE(result), context->result);
157             }
158             DeleteGetAppCertInfoAsyncContext(env, context);
159         },
160         static_cast<void *>(asyncContext),
161         &asyncContext->asyncWork));
162 
163     napi_status status = napi_queue_async_work(env, asyncContext->asyncWork);
164     if (status != napi_ok) {
165         GET_AND_THROW_LAST_ERROR((env));
166         DeleteGetAppCertInfoAsyncContext(env, asyncContext);
167         CM_LOG_E("could not queue async work");
168         return nullptr;
169     }
170 
171     return promise;
172 }
173 
CMNapiGetAppCertInfoCommon(napi_env env,napi_callback_info info,uint32_t store)174 napi_value CMNapiGetAppCertInfoCommon(napi_env env, napi_callback_info info, uint32_t store)
175 {
176     GetAppCertInfoAsyncContext context = CreateGetAppCertInfoAsyncContext();
177     if (context == nullptr) {
178         CM_LOG_E("could not create context");
179         return nullptr;
180     }
181 
182     context->store = store;
183 
184     napi_value result = GetAppCertInfoParseParams(env, info, context);
185     if (result == nullptr) {
186         CM_LOG_E("could not parse params");
187         DeleteGetAppCertInfoAsyncContext(env, context);
188         return nullptr;
189     }
190     result = GetAppCertInfoAsyncWork(env, context);
191     if (result == nullptr) {
192         CM_LOG_E("could not start async work");
193         DeleteGetAppCertInfoAsyncContext(env, context);
194         return nullptr;
195     }
196     return result;
197 }
198 }  // namespace CertManagerNapi
199