1 /*
2 * Copyright (c) 2023 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 #include "napi_x509_cert_match_parameters.h"
17 #include "napi_x509_certificate.h"
18 #include "cf_log.h"
19 #include "cf_memory.h"
20 #include "cf_type.h"
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23 #include "napi_cert_defines.h"
24 #include "napi_cert_utils.h"
25 #include "napi_object.h"
26 #include "utils.h"
27
28 namespace OHOS {
29 namespace CertFramework {
30
GetValidDate(napi_env env,napi_value arg,CfBlob * & out)31 static bool GetValidDate(napi_env env, napi_value arg, CfBlob *&out)
32 {
33 napi_value obj = GetProp(env, arg, CERT_MATCH_TAG_VALID_DATE.c_str());
34 if (obj == nullptr) {
35 return true;
36 }
37
38 out = CertGetBlobFromStringJSParams(env, obj);
39 if (out == nullptr) {
40 LOGE("out is nullptr");
41 return false;
42 }
43 return true;
44 }
45
GetIssuer(napi_env env,napi_value arg,CfBlob * & out)46 static bool GetIssuer(napi_env env, napi_value arg, CfBlob *&out)
47 {
48 napi_value obj = GetProp(env, arg, CERT_MATCH_TAG_ISSUER.c_str());
49 if (obj == nullptr) {
50 return true;
51 }
52 out = CertGetBlobFromUint8ArrJSParams(env, obj);
53 if (out == nullptr) {
54 LOGE("out is nullptr");
55 return false;
56 }
57 return true;
58 }
59
GetKeyUsage(napi_env env,napi_value arg,CfBlob * & out)60 static bool GetKeyUsage(napi_env env, napi_value arg, CfBlob *&out)
61 {
62 napi_value obj = GetProp(env, arg, CERT_MATCH_TAG_KEY_USAGE.c_str());
63 if (obj == nullptr) {
64 return true;
65 }
66 out = CertGetBlobFromArrBoolJSParams(env, obj);
67 if (out == nullptr) {
68 LOGE("out is nullptr");
69 return false;
70 }
71 return true;
72 }
73
GetSerialNumber(napi_env env,napi_value arg,CfBlob * & out)74 static bool GetSerialNumber(napi_env env, napi_value arg, CfBlob *&out)
75 {
76 napi_value obj = GetProp(env, arg, CERT_MATCH_TAG_SERIAL_NUMBER.c_str());
77 if (obj == nullptr) {
78 return true;
79 }
80 CfBlob outBlob = { 0, nullptr };
81 bool flag = CertGetSerialNumberFromBigIntJSParams(env, obj, outBlob);
82 if (!flag || outBlob.data == nullptr || outBlob.size == 0) {
83 LOGE("out is nullptr");
84 return false;
85 }
86 out = static_cast<CfBlob *>(HcfMalloc(sizeof(CfBlob), 0));
87 if (out == nullptr) {
88 LOGE("Failed to allocate newBlob memory!");
89 CfBlobDataFree(&outBlob);
90 return false;
91 }
92 out->data = outBlob.data;
93 out->size = outBlob.size;
94 return true;
95 }
96
GetSubject(napi_env env,napi_value arg,CfBlob * & out)97 static bool GetSubject(napi_env env, napi_value arg, CfBlob *&out)
98 {
99 napi_value obj = GetProp(env, arg, CERT_MATCH_TAG_SUBJECT.c_str());
100 if (obj == nullptr) {
101 return true;
102 }
103 out = CertGetBlobFromUint8ArrJSParams(env, obj);
104 if (out == nullptr) {
105 LOGE("out is nullptr");
106 return false;
107 }
108 return true;
109 }
110
GetPublicKey(napi_env env,napi_value arg,CfBlob * & out)111 static bool GetPublicKey(napi_env env, napi_value arg, CfBlob *&out)
112 {
113 napi_value obj = GetProp(env, arg, CERT_MATCH_TAG_PUBLIC_KEY.c_str());
114 if (obj == nullptr) {
115 return true;
116 }
117 out = CertGetBlobFromNapiValue(env, obj);
118 if (out == nullptr) {
119 LOGE("out is nullptr");
120 return false;
121 }
122 return true;
123 }
124
GetPublicKeyAlgId(napi_env env,napi_value arg,CfBlob * & out)125 static bool GetPublicKeyAlgId(napi_env env, napi_value arg, CfBlob *&out)
126 {
127 napi_value obj = GetProp(env, arg, CERT_MATCH_TAG_PUBLIC_KEY_ALGID.c_str());
128 if (obj == nullptr) {
129 return true;
130 }
131 out = CertGetBlobFromStringJSParams(env, obj);
132 if (out == nullptr) {
133 LOGE("out is nullptr");
134 return false;
135 }
136 return true;
137 }
138
GetX509Cert(napi_env env,napi_value arg,HcfCertificate * & out)139 static bool GetX509Cert(napi_env env, napi_value arg, HcfCertificate *&out)
140 {
141 napi_value obj = GetProp(env, arg, CERT_MATCH_TAG_X509CERT.c_str());
142 if (obj == nullptr) {
143 return true;
144 }
145 NapiX509Certificate *napiX509Cert = nullptr;
146 napi_unwrap(env, obj, reinterpret_cast<void **>(&napiX509Cert));
147 if (napiX509Cert == nullptr) {
148 LOGE("napiX509Cert is null!");
149 return false;
150 }
151
152 HcfX509Certificate *cert = napiX509Cert->GetX509Cert();
153 if (cert == nullptr) {
154 LOGE("cert is null!");
155 return false;
156 }
157 LOGI("x509Cert is not null!");
158 out = &(cert->base);
159 return true;
160 }
161
BuildX509CertMatchParams(napi_env env,napi_value arg,HcfX509CertMatchParams * & matchParams)162 bool BuildX509CertMatchParams(napi_env env, napi_value arg, HcfX509CertMatchParams *&matchParams)
163 {
164 napi_valuetype type;
165 napi_typeof(env, arg, &type);
166 if (type != napi_object) {
167 LOGE("wrong argument type. expect object type. [Type]: %d", type);
168 return false;
169 }
170
171 if (!GetValidDate(env, arg, matchParams->validDate)) {
172 return false;
173 }
174 if (!GetIssuer(env, arg, matchParams->issuer)) {
175 return false;
176 }
177 if (!GetKeyUsage(env, arg, matchParams->keyUsage)) {
178 return false;
179 }
180 if (!GetSerialNumber(env, arg, matchParams->serialNumber)) {
181 return false;
182 }
183 if (!GetSubject(env, arg, matchParams->subject)) {
184 return false;
185 }
186 if (!GetPublicKey(env, arg, matchParams->publicKey)) {
187 return false;
188 }
189 if (!GetPublicKeyAlgId(env, arg, matchParams->publicKeyAlgID)) {
190 return false;
191 }
192 if (!GetX509Cert(env, arg, matchParams->x509Cert)) {
193 return false;
194 }
195
196 return true;
197 }
198
FreeX509CertMatchParams(HcfX509CertMatchParams * & matchParams)199 void FreeX509CertMatchParams(HcfX509CertMatchParams *&matchParams)
200 {
201 if (matchParams == nullptr) {
202 return;
203 }
204
205 matchParams->x509Cert = nullptr;
206 CfBlobFree(&matchParams->validDate);
207 CfBlobFree(&matchParams->issuer);
208 CfBlobFree(&matchParams->keyUsage);
209 CfBlobFree(&matchParams->serialNumber);
210 CfBlobFree(&matchParams->subject);
211 CfBlobFree(&matchParams->publicKey);
212 CfBlobFree(&matchParams->publicKeyAlgID);
213
214 CF_FREE_PTR(matchParams);
215 }
216
217 } // namespace CertFramework
218 } // namespace OHOS