1 /*
2  * Copyright (c) 2022-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 #ifndef CM_NAPI_COMMON_H
17 #define CM_NAPI_COMMON_H
18 
19 #include <string>
20 
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23 
24 #include "cm_mem.h"
25 #include "cm_type.h"
26 
27 #define DATA_SIZE_64KB  (1024 * 64)
28 #define NAPI_ASSERT_FUNC(f) if (CM_SUCCESS != (f)) { CM_LOG_W("Failed: %s\n", #f); return; }
29 
30 namespace CMNapi {
31 static const std::string CM_CERT_PROPERTY_URI = "uri";
32 static const std::string CM_CERT_PROPERTY_TYPE = "type";
33 static const std::string CM_CERT_PROPERTY_CREDENTIAL_ALIAS = "alias";
34 static const std::string CM_CERT_PROPERTY_KEY_URI = "keyUri";
35 static const std::string CM_CERT_PROPERTY_KEY_NUM = "keyNum";
36 static const std::string CM_CERT_PROPERTY_CERT_NUM = "certNum";
37 static const std::string CM_CERT_PROPERTY_CREDENTIAL_DATA = "credData";
38 static const std::string CM_CERT_PROPERTY_CREDENTIAL_DATA_NEW = "credentialData";
39 
40 static const std::string CM_CERT_PROPERTY_CERTALIAS = "certAlias";
41 static const std::string CM_CERT_PROPERTY_ISSUERNAME = "issuerName";
42 static const std::string CM_CERT_PROPERTY_SUBJECTNAME = "subjectName";
43 static const std::string CM_CERT_PROPERTY_SERIAL = "serial";
44 static const std::string CM_CERT_PROPERTY_BEFORE = "notBefore";
45 static const std::string CM_CERT_PROPERTY_AFTER = "notAfter";
46 static const std::string CM_CERT_PROPERTY_FINGERSHA1 = "fingerprintSha1";
47 static const std::string CM_CERT_PROPERTY_FINGERSHA256 = "fingerprintSha256";
48 static const std::string CM_CERT_PROPERTY_CERT_DATA = "cert";
49 static const std::string CM_CERT_PROPERTY_STATUS = "status";
50 static const std::string CM_CERT_PROPERTY_STATE = "state";
51 
52 static const std::string BUSINESS_ERROR_PROPERTY_CODE = "code";
53 static const std::string BUSINESS_ERROR_PROPERTY_MESSAGE = "message";
54 
55 static const std::string CM_RESULT_PRPPERTY_CERTLIST = "certList";
56 static const std::string CM_RESULT_PRPPERTY_CERTINFO = "certInfo";
57 static const std::string CM_RESULT_PRPPERTY_CREDENTIAL_LIST = "credentialList";
58 static const std::string CM_RESULT_PRPPERTY_CREDENTIAL = "credential";
59 
60 static const int32_t CERT_MANAGER_SYS_CAP = 17500000;
61 static const int32_t RESULT_NUMBER = 2;
62 static const uint32_t APPLICATION_CERTIFICATE_STORE = 0;
63 static const uint32_t APPLICATION_PRIVATE_CERTIFICATE_STORE = 3;
64 static const uint32_t APPLICATION_SYSTEM_CERTIFICATE_STORE = 4;
65 
66 napi_value ParseUint32(napi_env env, napi_value object, uint32_t &store);
67 napi_value ParseBoolean(napi_env env, napi_value object, bool &status);
68 napi_value ParseCertAlias(napi_env env, napi_value napiObj, CmBlob *&certAlias);
69 napi_value ParseString(napi_env env, napi_value object, CmBlob *&stringBlob);
70 napi_value GetUint8Array(napi_env env, napi_value object, CmBlob &arrayBlob);
71 
72 napi_ref GetCallback(napi_env env, napi_value object);
73 int32_t GetCallback(napi_env env, napi_value object, napi_ref &callback);
74 
75 napi_value GenerateCertAbstractArray(
76     napi_env env, const struct CertAbstract *certAbstract, const uint32_t certCount);
77 
78 napi_value GenerateCredentialAbstractArray(napi_env env,
79     const struct CredentialAbstract *credentialAbstract, const uint32_t credentialCount);
80 
81 napi_value GenerateCertInfo(napi_env env, const struct CertInfo *certInfo);
82 napi_value GenerateAppCertInfo(napi_env env, const struct Credential *credential);
83 void ThrowError(napi_env env, int32_t errorCode, std::string errMsg);
84 napi_value GenerateBusinessError(napi_env env, int32_t errorCode);
85 
86 void DeleteNapiContext(napi_env env, napi_async_work &asyncWork, napi_ref &callback);
87 
88 void GeneratePromise(napi_env env, napi_deferred deferred, int32_t resultCode,
89     napi_value *result, int32_t arrLength);
90 void GenerateCallback(napi_env env, napi_ref callback, napi_value *result, int32_t arrLength, int32_t ret);
91 void GenerateNapiPromise(napi_env env, napi_ref callback, napi_deferred *deferred, napi_value *promise);
92 
GetNull(napi_env env)93 inline napi_value GetNull(napi_env env)
94 {
95     napi_value result = nullptr;
96     NAPI_CALL(env, napi_get_null(env, &result));
97     return result;
98 }
99 
GetInt32(napi_env env,int32_t value)100 inline napi_value GetInt32(napi_env env, int32_t value)
101 {
102     napi_value result = nullptr;
103     NAPI_CALL(env, napi_create_int32(env, value, &result));
104     return result;
105 }
106 
FreeCmBlob(CmBlob * & blob)107 inline void FreeCmBlob(CmBlob *&blob)
108 {
109     if (blob == nullptr) {
110         return;
111     }
112 
113     if (blob->data != nullptr) {
114         CmFree(blob->data);
115         blob->data = nullptr;
116     }
117     blob->size = 0;
118 
119     CmFree(blob);
120     blob = nullptr;
121 }
122 
123 void FreeCmContext(CmContext *&context);
124 
FreeCertAbstract(CertAbstract * & certAbstract)125 inline void FreeCertAbstract(CertAbstract *&certAbstract)
126 {
127     if (certAbstract == nullptr) {
128         return;
129     }
130 
131     certAbstract->status = false;
132 
133     CmFree(certAbstract);
134     certAbstract = nullptr;
135 }
136 
FreeCredentialAbstract(CredentialAbstract * & credentialAbstract)137 inline void FreeCredentialAbstract(CredentialAbstract *&credentialAbstract)
138 {
139     if (credentialAbstract == nullptr) {
140         return;
141     }
142 
143     CmFree(credentialAbstract);
144     credentialAbstract = nullptr;
145 }
146 
147 void FreeCertList(CertList *&certList);
148 void FreeCredentialList(CredentialList *&credentialList);
149 void FreeCertInfo(CertInfo *&certInfo);
150 void FreeCredential(Credential *&credential);
151 
152 enum ErrorCode {
153     SUCCESS = 0,
154     HAS_NO_PERMISSION = 201,
155     NOT_SYSTEM_APP = 202,
156     PARAM_ERROR = 401,
157     INNER_FAILURE = 17500001,
158     NOT_FOUND = 17500002,
159     INVALID_CERT_FORMAT = 17500003,
160     MAX_CERT_COUNT_REACHED = 17500004,
161     NO_AUTHORIZATION = 17500005,
162     ALIAS_LENGTH_REACHED_LIMIT = 17500006,
163     DEVICE_ENTER_ADVSECMODE = 17500007,
164     PASSWORD_IS_ERROR = 17500008,
165 };
166 
167 enum CmJSKeyDigest {
168     CM_JS_DIGEST_NONE = 0,
169     CM_JS_DIGEST_MD5 = 1,
170     CM_JS_DIGEST_SHA1 = 2,
171     CM_JS_DIGEST_SHA224 = 3,
172     CM_JS_DIGEST_SHA256 = 4,
173     CM_JS_DIGEST_SHA384 = 5,
174     CM_JS_DIGEST_SHA512 = 6,
175 };
176 
177 enum CmJSKeyPadding {
178     CM_JS_PADDING_NONE = 0,
179     CM_JS_PADDING_PSS = 1,
180     CM_JS_PADDING_PKCS1_V1_5 = 2,
181 };
182 
183 struct CertInfoValue {
184     napi_value uri;
185     napi_value certAlias;
186     napi_value status;
187     napi_value issuerName;
188     napi_value subjectName;
189     napi_value serial;
190     napi_value notBefore;
191     napi_value notAfter;
192     napi_value fingerprintSha256;
193     napi_value certInfoBlob;
194 };
195 }  // namespace CertManagerNapi
196 
197 #endif
198