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