• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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 
39 static const std::string CM_CERT_PROPERTY_CERTALIAS = "certAlias";
40 static const std::string CM_CERT_PROPERTY_ISSUERNAME = "issuerName";
41 static const std::string CM_CERT_PROPERTY_SUBJECTNAME = "subjectName";
42 static const std::string CM_CERT_PROPERTY_SERIAL = "serial";
43 static const std::string CM_CERT_PROPERTY_BEFORE = "notBefore";
44 static const std::string CM_CERT_PROPERTY_AFTER = "notAfter";
45 static const std::string CM_CERT_PROPERTY_FINGERSHA1 = "fingerprintSha1";
46 static const std::string CM_CERT_PROPERTY_FINGERSHA256 = "fingerprintSha256";
47 static const std::string CM_CERT_PROPERTY_CERT_DATA = "cert";
48 static const std::string CM_CERT_PROPERTY_STATUS = "status";
49 
50 static const std::string BUSINESS_ERROR_PROPERTY_CODE = "code";
51 static const std::string BUSINESS_ERROR_PROPERTY_MESSAGE = "message";
52 
53 static const std::string CM_RESULT_PRPPERTY_CERTLIST = "certList";
54 static const std::string CM_RESULT_PRPPERTY_CERTINFO = "certInfo";
55 static const std::string CM_RESULT_PRPPERTY_CREDENTIAL_LIST = "credentialList";
56 static const std::string CM_RESULT_PRPPERTY_CREDENTIAL = "credential";
57 
58 static const int32_t CERT_MANAGER_SYS_CAP = 17500000;
59 static const int32_t RESULT_NUMBER = 2;
60 static const uint32_t APPLICATION_CERTIFICATE_STORE = 0;
61 static const uint32_t APPLICATION_PRIVATE_CERTIFICATE_STORE = 3;
62 
63 napi_value ParseUint32(napi_env env, napi_value object, uint32_t &store);
64 napi_value ParseBoolean(napi_env env, napi_value object, bool &status);
65 napi_value ParseString(napi_env env, napi_value object, CmBlob *&certUri);
66 napi_value GetUint8Array(napi_env env, napi_value object, CmBlob &arrayBlob);
67 
68 napi_ref GetCallback(napi_env env, napi_value object);
69 
70 napi_value GenerateCertAbstractArray(
71     napi_env env, const struct CertAbstract *certAbstract, const uint32_t certCount);
72 
73 napi_value GenerateCredentialAbstractArray(napi_env env,
74     const struct CredentialAbstract *credentialAbstract, const uint32_t credentialCount);
75 
76 napi_value GenerateCertInfo(napi_env env, const struct CertInfo *certInfo);
77 napi_value GenerateAppCertInfo(napi_env env, const struct Credential *credential);
78 void ThrowParamsError(napi_env env, int32_t errorCode, std::string errMsg);
79 napi_value GenerateBusinessError(napi_env env, int32_t errorCode, const char *errorMsg);
80 
81 void DeleteNapiContext(napi_env env, napi_async_work &asyncWork, napi_ref &callback);
82 
83 void GeneratePromise(napi_env env, napi_deferred deferred, int32_t resultCode,
84     napi_value *result, int32_t arrLength);
85 void GenerateCallback(napi_env env, napi_ref callback, napi_value *result, int32_t arrLength);
86 void GenerateNapiPromise(napi_env env, napi_ref callback, napi_deferred *deferred, napi_value *promise);
87 
GetNull(napi_env env)88 inline napi_value GetNull(napi_env env)
89 {
90     napi_value result = nullptr;
91     NAPI_CALL(env, napi_get_null(env, &result));
92     return result;
93 }
94 
GetInt32(napi_env env,int32_t value)95 inline napi_value GetInt32(napi_env env, int32_t value)
96 {
97     napi_value result = nullptr;
98     NAPI_CALL(env, napi_create_int32(env, value, &result));
99     return result;
100 }
101 
FreeCmBlob(CmBlob * & blob)102 inline void FreeCmBlob(CmBlob *&blob)
103 {
104     if (blob == nullptr) {
105         return;
106     }
107 
108     if (blob->data != nullptr) {
109         CmFree(blob->data);
110         blob->data = nullptr;
111     }
112     blob->size = 0;
113 
114     CmFree(blob);
115     blob = nullptr;
116 }
117 
118 void FreeCmContext(CmContext *&context);
119 
FreeCertAbstract(CertAbstract * & certAbstract)120 inline void FreeCertAbstract(CertAbstract *&certAbstract)
121 {
122     if (certAbstract == nullptr) {
123         return;
124     }
125 
126     certAbstract->status = false;
127 
128     CmFree(certAbstract);
129     certAbstract = nullptr;
130 }
131 
FreeCredentialAbstract(CredentialAbstract * & credentialAbstract)132 inline void FreeCredentialAbstract(CredentialAbstract *&credentialAbstract)
133 {
134     if (credentialAbstract == nullptr) {
135         return;
136     }
137 
138     CmFree(credentialAbstract);
139     credentialAbstract = nullptr;
140 }
141 
142 void FreeCertList(CertList *&certList);
143 void FreeCredentialList(CredentialList *&credentialList);
144 void FreeCertInfo(CertInfo *&certInfo);
145 void FreeCredential(Credential *&credential);
146 
147 enum ErrorCode {
148     SUCCESS = 0,
149     NOT_SYSTEM_APP = 202,
150     PARAM_ERROR = 401,
151     INNER_FAILURE = 17500001,
152     NO_PERMISSION = 17500002,
153     NOT_FOUND = 17500003,
154     INVALID_CERT_FORMAT = 17500004,
155 };
156 
157 struct CertInfoValue {
158     napi_value uri;
159     napi_value certAlias;
160     napi_value status;
161     napi_value issuerName;
162     napi_value subjectName;
163     napi_value serial;
164     napi_value notBefore;
165     napi_value notAfter;
166     napi_value fingerprintSha256;
167     napi_value certInfoBlob;
168 };
169 }  // namespace CertManagerNapi
170 
171 #endif
172