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 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
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 ParseString(napi_env env, napi_value object, CmBlob *&certUri);
68 napi_value GetUint8Array(napi_env env, napi_value object, CmBlob &arrayBlob);
69
70 napi_ref GetCallback(napi_env env, napi_value object);
71 int32_t GetCallback(napi_env env, napi_value object, napi_ref &callback);
72
73 napi_value GenerateCertAbstractArray(
74 napi_env env, const struct CertAbstract *certAbstract, const uint32_t certCount);
75
76 napi_value GenerateCredentialAbstractArray(napi_env env,
77 const struct CredentialAbstract *credentialAbstract, const uint32_t credentialCount);
78
79 napi_value GenerateCertInfo(napi_env env, const struct CertInfo *certInfo);
80 napi_value GenerateAppCertInfo(napi_env env, const struct Credential *credential);
81 void ThrowParamsError(napi_env env, int32_t errorCode, std::string errMsg);
82 napi_value GenerateBusinessError(napi_env env, int32_t errorCode, const char *errorMsg);
83
84 void DeleteNapiContext(napi_env env, napi_async_work &asyncWork, napi_ref &callback);
85
86 void GeneratePromise(napi_env env, napi_deferred deferred, int32_t resultCode,
87 napi_value *result, int32_t arrLength);
88 void GenerateCallback(napi_env env, napi_ref callback, napi_value *result, int32_t arrLength, int32_t ret);
89 void GenerateNapiPromise(napi_env env, napi_ref callback, napi_deferred *deferred, napi_value *promise);
90
GetNull(napi_env env)91 inline napi_value GetNull(napi_env env)
92 {
93 napi_value result = nullptr;
94 NAPI_CALL(env, napi_get_null(env, &result));
95 return result;
96 }
97
GetInt32(napi_env env,int32_t value)98 inline napi_value GetInt32(napi_env env, int32_t value)
99 {
100 napi_value result = nullptr;
101 NAPI_CALL(env, napi_create_int32(env, value, &result));
102 return result;
103 }
104
FreeCmBlob(CmBlob * & blob)105 inline void FreeCmBlob(CmBlob *&blob)
106 {
107 if (blob == nullptr) {
108 return;
109 }
110
111 if (blob->data != nullptr) {
112 CmFree(blob->data);
113 blob->data = nullptr;
114 }
115 blob->size = 0;
116
117 CmFree(blob);
118 blob = nullptr;
119 }
120
121 void FreeCmContext(CmContext *&context);
122
FreeCertAbstract(CertAbstract * & certAbstract)123 inline void FreeCertAbstract(CertAbstract *&certAbstract)
124 {
125 if (certAbstract == nullptr) {
126 return;
127 }
128
129 certAbstract->status = false;
130
131 CmFree(certAbstract);
132 certAbstract = nullptr;
133 }
134
FreeCredentialAbstract(CredentialAbstract * & credentialAbstract)135 inline void FreeCredentialAbstract(CredentialAbstract *&credentialAbstract)
136 {
137 if (credentialAbstract == nullptr) {
138 return;
139 }
140
141 CmFree(credentialAbstract);
142 credentialAbstract = nullptr;
143 }
144
145 void FreeCertList(CertList *&certList);
146 void FreeCredentialList(CredentialList *&credentialList);
147 void FreeCertInfo(CertInfo *&certInfo);
148 void FreeCredential(Credential *&credential);
149
150 enum ErrorCode {
151 SUCCESS = 0,
152 HAS_NO_PERMISSION = 201,
153 NOT_SYSTEM_APP = 202,
154 PARAM_ERROR = 401,
155 INNER_FAILURE = 17500001,
156 NOT_FOUND = 17500002,
157 INVALID_CERT_FORMAT = 17500003,
158 };
159
160 enum CmJSKeyDigest {
161 CM_JS_DIGEST_NONE = 0,
162 CM_JS_DIGEST_MD5 = 1,
163 CM_JS_DIGEST_SHA1 = 2,
164 CM_JS_DIGEST_SHA224 = 3,
165 CM_JS_DIGEST_SHA256 = 4,
166 CM_JS_DIGEST_SHA384 = 5,
167 CM_JS_DIGEST_SHA512 = 6,
168 };
169
170 enum CmJSKeyPadding {
171 CM_JS_PADDING_NONE = 0,
172 CM_JS_PADDING_PSS = 1,
173 CM_JS_PADDING_PKCS1_V1_5 = 2,
174 };
175
176 struct CertInfoValue {
177 napi_value uri;
178 napi_value certAlias;
179 napi_value status;
180 napi_value issuerName;
181 napi_value subjectName;
182 napi_value serial;
183 napi_value notBefore;
184 napi_value notAfter;
185 napi_value fingerprintSha256;
186 napi_value certInfoBlob;
187 };
188 } // namespace CertManagerNapi
189
190 #endif
191