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
60 static const int32_t RESULT_NUMBER = 2;
61 static const uint32_t APPLICATION_CERTIFICATE_STORE = 0;
62 static const uint32_t APPLICATION_PRIVATE_CERTIFICATE_STORE = 3;
63 static const uint32_t APPLICATION_SYSTEM_CERTIFICATE_STORE = 4;
64
65 napi_value ParseUint32(napi_env env, napi_value object, uint32_t &store);
66 napi_value ParseBoolean(napi_env env, napi_value object, bool &status);
67 napi_value ParseCertAlias(napi_env env, napi_value napiObj, CmBlob *&certAlias);
68 napi_value ParseString(napi_env env, napi_value object, CmBlob *&stringBlob);
69 napi_value ParsePasswd(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
93 bool IsValidCertType(const uint32_t certType);
94 bool IsValidCertScope(const uint32_t scope);
95
GetNull(napi_env env)96 inline napi_value GetNull(napi_env env)
97 {
98 napi_value result = nullptr;
99 NAPI_CALL(env, napi_get_null(env, &result));
100 return result;
101 }
102
GetInt32(napi_env env,int32_t value)103 inline napi_value GetInt32(napi_env env, int32_t value)
104 {
105 napi_value result = nullptr;
106 NAPI_CALL(env, napi_create_int32(env, value, &result));
107 return result;
108 }
109
FreeCmBlob(CmBlob * & blob)110 inline void FreeCmBlob(CmBlob *&blob)
111 {
112 if (blob == nullptr) {
113 return;
114 }
115
116 if (blob->data != nullptr) {
117 CmFree(blob->data);
118 blob->data = nullptr;
119 }
120 blob->size = 0;
121
122 CmFree(blob);
123 blob = nullptr;
124 }
125
126 void FreeCmContext(CmContext *&context);
127
FreeCertAbstract(CertAbstract * & certAbstract)128 inline void FreeCertAbstract(CertAbstract *&certAbstract)
129 {
130 if (certAbstract == nullptr) {
131 return;
132 }
133
134 certAbstract->status = false;
135
136 CmFree(certAbstract);
137 certAbstract = nullptr;
138 }
139
FreeCredentialAbstract(CredentialAbstract * & credentialAbstract)140 inline void FreeCredentialAbstract(CredentialAbstract *&credentialAbstract)
141 {
142 if (credentialAbstract == nullptr) {
143 return;
144 }
145
146 CmFree(credentialAbstract);
147 credentialAbstract = nullptr;
148 }
149
150 void FreeCertList(CertList *&certList);
151 void FreeCredentialList(CredentialList *&credentialList);
152 void FreeCertInfo(CertInfo *&certInfo);
153 void FreeCredential(Credential *&credential);
154
155 enum ErrorCode {
156 SUCCESS = 0,
157 HAS_NO_PERMISSION = 201,
158 NOT_SYSTEM_APP = 202,
159 PARAM_ERROR = 401,
160 INNER_FAILURE = 17500001,
161 NOT_FOUND = 17500002,
162 INVALID_CERT_FORMAT = 17500003,
163 MAX_CERT_COUNT_REACHED = 17500004,
164 NO_AUTHORIZATION = 17500005,
165 ALIAS_LENGTH_REACHED_LIMIT = 17500006,
166 DEVICE_ENTER_ADVSECMODE = 17500007,
167 PASSWORD_IS_ERROR = 17500008,
168 };
169
170 enum CmJSKeyDigest {
171 CM_JS_DIGEST_NONE = 0,
172 CM_JS_DIGEST_MD5 = 1,
173 CM_JS_DIGEST_SHA1 = 2,
174 CM_JS_DIGEST_SHA224 = 3,
175 CM_JS_DIGEST_SHA256 = 4,
176 CM_JS_DIGEST_SHA384 = 5,
177 CM_JS_DIGEST_SHA512 = 6,
178 CM_JS_DIGEST_SM3 = 7,
179 };
180
181 enum CmJSKeyPadding {
182 CM_JS_PADDING_NONE = 0,
183 CM_JS_PADDING_PSS = 1,
184 CM_JS_PADDING_PKCS1_V1_5 = 2,
185 };
186
187 struct CertInfoValue {
188 napi_value uri;
189 napi_value certAlias;
190 napi_value status;
191 napi_value issuerName;
192 napi_value subjectName;
193 napi_value serial;
194 napi_value notBefore;
195 napi_value notAfter;
196 napi_value fingerprintSha256;
197 napi_value certInfoBlob;
198 };
199 } // namespace CertManagerNapi
200
201 #endif
202