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_CONTEXT_PROPERTY_USERID = "userId";
32 static const std::string CM_CONTEXT_PROPERTY_UID = "uid";
33 static const std::string CM_CONTEXT_PROPERTY_PACKAGENAME = "packageName";
34
35 static const std::string CM_CERT_PROPERTY_URI = "uri";
36 static const std::string CM_CERT_PROPERTY_TYPE = "type";
37 static const std::string CM_CERT_PROPERTY_CREDENTIAL_ALIAS = "alias";
38 static const std::string CM_CERT_PROPERTY_KEY_URI = "keyUri";
39 static const std::string CM_CERT_PROPERTY_KEY_NUM = "keyNum";
40 static const std::string CM_CERT_PROPERTY_CERT_NUM = "certNum";
41 static const std::string CM_CERT_PROPERTY_CREDENTIAL_DATA = "credData";
42
43 static const std::string CM_CERT_PROPERTY_CERTALIAS = "certAlias";
44 static const std::string CM_CERT_PROPERTY_ISSUERNAME = "issuerName";
45 static const std::string CM_CERT_PROPERTY_SUBJECTNAME = "subjectName";
46 static const std::string CM_CERT_PROPERTY_SERIAL = "serial";
47 static const std::string CM_CERT_PROPERTY_BEFORE = "notBefore";
48 static const std::string CM_CERT_PROPERTY_AFTER = "notAfter";
49 static const std::string CM_CERT_PROPERTY_FINGERSHA1 = "fingerprintSha1";
50 static const std::string CM_CERT_PROPERTY_FINGERSHA256 = "fingerprintSha256";
51 static const std::string CM_CERT_PROPERTY_CERT_DATA = "cert";
52 static const std::string CM_CERT_PROPERTY_STATUS = "status";
53
54 static const std::string BUSINESS_ERROR_PROPERTY_CODE = "code";
55 static const std::string BUSINESS_ERROR_PROPERTY_MESSAGE = "message";
56
57 static const std::string CM_RESULT_PRPPERTY_CERTLIST = "certList";
58 static const std::string CM_RESULT_PRPPERTY_CERTINFO = "certInfo";
59 static const std::string CM_RESULT_PRPPERTY_CREDENTIAL_LIST = "credentialList";
60 static const std::string CM_RESULT_PRPPERTY_CREDENTIAL = "credential";
61
62 static const int32_t CERT_MANAGER_SYS_CAP = 17500000;
63 static const int32_t RESULT_NUMBER = 2;
64 static const uint32_t APPLICATION_CERTIFICATE_STORE = 0;
65 static const uint32_t APPLICATION_PRIVATE_CERTIFICATE_STORE = 3;
66
67 napi_value ParseCmContext(napi_env env, napi_value object, CmContext *&cmContext);
68 napi_value ParseUint32(napi_env env, napi_value object, uint32_t &store);
69 napi_value ParseBoolean(napi_env env, napi_value object, bool &status);
70 napi_value ParseString(napi_env env, napi_value object, CmBlob *&certUri);
71 napi_value GetUint8Array(napi_env env, napi_value object, CmBlob &arrayBlob);
72
73 napi_ref GetCallback(napi_env env, napi_value object);
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 ThrowParamsError(napi_env env, int32_t errorCode, std::string errMsg);
84 napi_value GenerateBusinessError(napi_env env, int32_t errorCode, const char *errorMsg);
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);
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 NOT_SYSTEM_APP = 202,
155 PARAM_ERROR = 401,
156 INNER_FAILURE = 17500001,
157 NO_PERMISSION = 17500002,
158 NOT_FOUND = 17500003,
159 INVALID_CERT_FORMAT = 17500004,
160 };
161
162 struct CertInfoValue {
163 napi_value uri;
164 napi_value certAlias;
165 napi_value status;
166 napi_value issuerName;
167 napi_value subjectName;
168 napi_value serial;
169 napi_value notBefore;
170 napi_value notAfter;
171 napi_value fingerprintSha256;
172 napi_value certInfoBlob;
173 };
174 } // namespace CertManagerNapi
175
176 #endif
177