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 #include "x509_crl_openssl.h"
17
18 #include "securec.h"
19
20 #include <openssl/bio.h>
21 #include <openssl/evp.h>
22 #include <openssl/pem.h>
23 #include <openssl/pkcs7.h>
24 #include <openssl/rsa.h>
25 #include <openssl/x509.h>
26
27 #include "config.h"
28 #include "fwk_class.h"
29 #include "hcf_string.h"
30 #include "log.h"
31 #include "memory.h"
32 #include "openssl_class.h"
33 #include "openssl_common.h"
34 #include "utils.h"
35 #include "x509_crl.h"
36 #include "x509_crl_entry_openssl.h"
37 #include "x509_crl_spi.h"
38
39 typedef struct {
40 HcfX509CrlSpi base;
41 X509_CRL *crl;
42 HcfBlob *certIssuer;
43 } HcfX509CRLOpensslImpl;
44
45 #define OPENSSL_INVALID_VERSION (-1)
46 #define OPENSSL_ERROR 0
47 #define TYPE_NAME "X509"
48 #define OID_LENGTH 128
49 #define MAX_REV_NUM 256
50 #define MAX_SIGNATURE_LEN 8192
51
GetClass(void)52 static const char *GetClass(void)
53 {
54 return X509_CRL_OPENSSL_CLASS;
55 }
56
GetType(HcfX509CrlSpi * self)57 static const char *GetType(HcfX509CrlSpi *self)
58 {
59 if (self == NULL) {
60 LOGE("Invalid Paramas!");
61 return NULL;
62 }
63 if (!IsClassMatch((HcfObjectBase *)self, GetClass())) {
64 LOGE("Input wrong class type!");
65 return NULL;
66 }
67 return TYPE_NAME;
68 }
69
GetCrl(HcfX509CrlSpi * self)70 static X509_CRL *GetCrl(HcfX509CrlSpi *self)
71 {
72 if (!IsClassMatch((HcfObjectBase *)self, GetClass())) {
73 LOGE("Input wrong class type!");
74 return NULL;
75 }
76 return ((HcfX509CRLOpensslImpl *)self)->crl;
77 }
78
GetX509FromCertificate(const HcfCertificate * cert)79 static X509 *GetX509FromCertificate(const HcfCertificate *cert)
80 {
81 if (!IsClassMatch((HcfObjectBase *)cert, HCF_X509_CERTIFICATE_CLASS)) {
82 LOGE("Input wrong openssl class type!");
83 return NULL;
84 }
85 HcfX509CertificateImpl *impl = (HcfX509CertificateImpl *)cert;
86 if (!IsClassMatch((HcfObjectBase *)(impl->spiObj), X509_CERT_OPENSSL_CLASS)) {
87 LOGE("Input wrong openssl class type!");
88 return NULL;
89 }
90 HcfOpensslX509Cert *realCert = (HcfOpensslX509Cert *)(impl->spiObj);
91 return realCert->x509;
92 }
93
IsRevoked(HcfX509CrlSpi * self,const HcfCertificate * cert)94 static bool IsRevoked(HcfX509CrlSpi *self, const HcfCertificate *cert)
95 {
96 if ((self == NULL) || (cert == NULL)) {
97 LOGE("Invalid Paramas!");
98 return false;
99 }
100 if (!IsClassMatch((HcfObjectBase *)self, GetClass())) {
101 LOGE("Input wrong class type!");
102 return false;
103 }
104 X509 *certOpenssl = GetX509FromCertificate(cert);
105 if (certOpenssl == NULL) {
106 LOGE("Input Cert is wrong !");
107 return false;
108 }
109 X509_CRL *crl = ((HcfX509CRLOpensslImpl *)self)->crl;
110 if (crl == NULL) {
111 LOGE("crl is null!");
112 return false;
113 }
114 X509_REVOKED *rev = NULL;
115 int32_t res = X509_CRL_get0_by_cert(crl, &rev, certOpenssl);
116 return (res != 0);
117 }
118
GetEncoded(HcfX509CrlSpi * self,HcfEncodingBlob * encodedOut)119 static HcfResult GetEncoded(HcfX509CrlSpi *self, HcfEncodingBlob *encodedOut)
120 {
121 if ((self == NULL) || (encodedOut == NULL)) {
122 LOGE("Invalid Paramas!");
123 return HCF_INVALID_PARAMS;
124 }
125 if (!IsClassMatch((HcfObjectBase *)self, GetClass())) {
126 LOGE("Input wrong class type!");
127 return HCF_INVALID_PARAMS;
128 }
129 unsigned char *out = NULL;
130 X509_CRL *crl = ((HcfX509CRLOpensslImpl *)self)->crl;
131 if (crl == NULL) {
132 LOGE("crl is null!");
133 return HCF_INVALID_PARAMS;
134 }
135 int32_t length = i2d_X509_CRL(crl, &out);
136 if (length <= 0) {
137 LOGE("Do i2d_X509_CRL fail!");
138 HcfPrintOpensslError();
139 return HCF_ERR_CRYPTO_OPERATION;
140 }
141 encodedOut->data = (uint8_t *)HcfMalloc(length, 0);
142 if (encodedOut->data == NULL) {
143 LOGE("Failed to malloc for crl encoded data!");
144 OPENSSL_free(out);
145 return HCF_ERR_MALLOC;
146 }
147 (void)memcpy_s(encodedOut->data, length, out, length);
148 OPENSSL_free(out);
149 encodedOut->len = length;
150 encodedOut->encodingFormat = HCF_FORMAT_DER;
151 return HCF_SUCCESS;
152 }
153
Verify(HcfX509CrlSpi * self,HcfPubKey * key)154 static HcfResult Verify(HcfX509CrlSpi *self, HcfPubKey *key)
155 {
156 if ((self == NULL) || (key == NULL)) {
157 LOGE("Invalid Paramas!");
158 return HCF_INVALID_PARAMS;
159 }
160 if (!IsClassMatch((HcfObjectBase *)self, GetClass()) ||
161 (!IsClassMatch((HcfObjectBase *)key, OPENSSL_RSA_PUBKEY_CLASS))) {
162 LOGE("Input wrong class type!");
163 return HCF_INVALID_PARAMS;
164 }
165 RSA *rsaPubkey = ((HcfOpensslRsaPubKey *)key)->pk;
166 if (rsaPubkey == NULL) {
167 LOGE("rsaPubkey is null!");
168 return HCF_INVALID_PARAMS;
169 }
170 EVP_PKEY *pubKey = EVP_PKEY_new();
171 if (pubKey == NULL) {
172 LOGE("pubKey is null!");
173 HcfPrintOpensslError();
174 return HCF_ERR_CRYPTO_OPERATION;
175 }
176 if (EVP_PKEY_assign_RSA(pubKey, rsaPubkey) <= 0) {
177 LOGE("Do EVP_PKEY_assign_RSA fail!");
178 HcfPrintOpensslError();
179 return HCF_ERR_CRYPTO_OPERATION;
180 }
181 X509_CRL *crl = ((HcfX509CRLOpensslImpl *)self)->crl;
182 if (crl == NULL) {
183 LOGE("crl is null!");
184 return HCF_INVALID_PARAMS;
185 }
186 int32_t res = X509_CRL_verify(crl, pubKey);
187 if (res != HCF_OPENSSL_SUCCESS) {
188 LOGE("Verify fail!");
189 HcfPrintOpensslError();
190 return HCF_ERR_CRYPTO_OPERATION;
191 }
192 return HCF_SUCCESS;
193 }
194
GetVersion(HcfX509CrlSpi * self)195 static long GetVersion(HcfX509CrlSpi *self)
196 {
197 if (self == NULL) {
198 LOGE("Invalid Paramas!");
199 return OPENSSL_INVALID_VERSION;
200 }
201 if (!IsClassMatch((HcfObjectBase *)self, GetClass())) {
202 LOGE("Input wrong class type!");
203 return OPENSSL_INVALID_VERSION;
204 }
205 X509_CRL *crl = ((HcfX509CRLOpensslImpl *)self)->crl;
206 if (crl == NULL) {
207 LOGE("crl is null!");
208 return OPENSSL_INVALID_VERSION;
209 }
210 return X509_CRL_get_version(crl) + 1;
211 }
212
GetIssuerName(HcfX509CrlSpi * self,HcfBlob * out)213 static HcfResult GetIssuerName(HcfX509CrlSpi *self, HcfBlob *out)
214 {
215 if ((self == NULL) || (out == NULL)) {
216 LOGE("Invalid Paramas for calling GetIssuerName!");
217 return HCF_INVALID_PARAMS;
218 }
219 X509_CRL *crl = GetCrl(self);
220 if (crl == NULL) {
221 LOGE("crl is null!");
222 return HCF_INVALID_PARAMS;
223 }
224 X509_NAME *x509Name = X509_CRL_get_issuer(crl);
225 if (x509Name == NULL) {
226 LOGE("Get Issuer DN fail!");
227 HcfPrintOpensslError();
228 return HCF_ERR_CRYPTO_OPERATION;
229 }
230 const char *issuer = X509_NAME_oneline(x509Name, NULL, 0);
231 if ((issuer == NULL) || (strlen(issuer) > HCF_MAX_STR_LEN)) {
232 LOGE("X509Name convert char fail or issuer name is too long!");
233 HcfPrintOpensslError();
234 return HCF_ERR_CRYPTO_OPERATION;
235 }
236 uint32_t length = strlen(issuer) + 1;
237 out->data = (uint8_t *)HcfMalloc(length, 0);
238 if (out->data == NULL) {
239 LOGE("Failed to malloc for crl issuer data!");
240 return HCF_ERR_MALLOC;
241 }
242 (void)memcpy_s(out->data, length, issuer, length);
243 out->len = length;
244 return HCF_SUCCESS;
245 }
246
SetCertIssuer(HcfX509CrlSpi * self)247 static HcfResult SetCertIssuer(HcfX509CrlSpi *self)
248 {
249 ((HcfX509CRLOpensslImpl *)self)->certIssuer = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0);
250 if (((HcfX509CRLOpensslImpl *)self)->certIssuer == NULL) {
251 LOGE("Failed to malloc for certIssuer!");
252 return HCF_ERR_MALLOC;
253 }
254 HcfResult res = GetIssuerName(self, ((HcfX509CRLOpensslImpl *)self)->certIssuer);
255 if (res != HCF_SUCCESS) {
256 HcfFree(((HcfX509CRLOpensslImpl *)self)->certIssuer);
257 ((HcfX509CRLOpensslImpl *)self)->certIssuer = NULL;
258 }
259 return res;
260 }
261
GetLastUpdate(HcfX509CrlSpi * self,HcfBlob * out)262 static HcfResult GetLastUpdate(HcfX509CrlSpi *self, HcfBlob *out)
263 {
264 if ((self == NULL) || (out == NULL)) {
265 LOGE("Invalid Paramas for calling GetLastUpdate!");
266 return HCF_INVALID_PARAMS;
267 }
268 X509_CRL *crl = GetCrl(self);
269 if (crl == NULL) {
270 LOGE("crl is null!");
271 return HCF_INVALID_PARAMS;
272 }
273 const ASN1_TIME *time = X509_CRL_get0_lastUpdate(crl);
274 if (time == NULL) {
275 LOGE("Get this update time fail!");
276 HcfPrintOpensslError();
277 return HCF_ERR_CRYPTO_OPERATION;
278 }
279 const char *thisUpdate = (const char *)(time->data);
280 if (thisUpdate == NULL || strlen(thisUpdate) > HCF_MAX_STR_LEN) {
281 LOGE("ThisUpdate convert String fail, or thisUpdate is too long!");
282 return HCF_ERR_CRYPTO_OPERATION;
283 }
284 uint32_t length = strlen(thisUpdate) + 1;
285 out->data = (uint8_t *)HcfMalloc(length, 0);
286 if (out->data == NULL) {
287 LOGE("Failed to malloc for thisUpdate!");
288 return HCF_ERR_MALLOC;
289 }
290 (void)memcpy_s(out->data, length, thisUpdate, length);
291 out->len = length;
292 return HCF_SUCCESS;
293 }
294
GetNextUpdate(HcfX509CrlSpi * self,HcfBlob * out)295 static HcfResult GetNextUpdate(HcfX509CrlSpi *self, HcfBlob *out)
296 {
297 if ((self == NULL) || (out == NULL)) {
298 LOGE("Invalid Paramas for calling GetNextUpdate!");
299 return HCF_INVALID_PARAMS;
300 }
301 X509_CRL *crl = GetCrl(self);
302 if (crl == NULL) {
303 LOGE("crl is null!");
304 return HCF_INVALID_PARAMS;
305 }
306 const ASN1_TIME *time = X509_CRL_get0_nextUpdate(crl);
307 if (time == NULL) {
308 LOGE("Get next update time fail!");
309 HcfPrintOpensslError();
310 return HCF_ERR_CRYPTO_OPERATION;
311 }
312 const char *nextUpdate = (const char *)(time->data);
313 if ((nextUpdate == NULL) || (strlen(nextUpdate) > HCF_MAX_STR_LEN)) {
314 LOGE("Get next update time is null, or nextUpdate is too long!");
315 return HCF_ERR_CRYPTO_OPERATION;
316 }
317 uint32_t length = strlen(nextUpdate) + 1;
318 out->data = (uint8_t *)HcfMalloc(length, 0);
319 if (out->data == NULL) {
320 LOGE("Failed to malloc for nextUpdate!");
321 return HCF_ERR_MALLOC;
322 }
323 (void)memcpy_s(out->data, length, nextUpdate, length);
324 out->len = length;
325 return HCF_SUCCESS;
326 }
327
GetRevokedCert(HcfX509CrlSpi * self,long serialNumber,HcfX509CrlEntry ** entryOut)328 static HcfResult GetRevokedCert(HcfX509CrlSpi *self, long serialNumber, HcfX509CrlEntry **entryOut)
329 {
330 if ((self == NULL) || (entryOut == NULL)) {
331 LOGE("Invalid Paramas!");
332 return HCF_INVALID_PARAMS;
333 }
334 X509_CRL *crl = GetCrl(self);
335 if (crl == NULL) {
336 LOGE("crl is null!");
337 return HCF_INVALID_PARAMS;
338 }
339 ASN1_INTEGER *serial = ASN1_INTEGER_new();
340 if (serial == NULL) {
341 LOGE("Serial init fail!");
342 HcfPrintOpensslError();
343 return HCF_ERR_CRYPTO_OPERATION;
344 }
345 if (!ASN1_INTEGER_set(serial, serialNumber)) {
346 LOGE("Set serial number fail!");
347 HcfPrintOpensslError();
348 ASN1_INTEGER_free(serial);
349 return HCF_ERR_CRYPTO_OPERATION;
350 }
351 X509_REVOKED *rev = NULL;
352 int32_t opensslRes = X509_CRL_get0_by_serial(crl, &rev, serial);
353 ASN1_INTEGER_free(serial);
354 if (opensslRes != HCF_OPENSSL_SUCCESS) {
355 LOGE("Get revoked certificate fail, res : %d!", opensslRes);
356 HcfPrintOpensslError();
357 return HCF_ERR_CRYPTO_OPERATION;
358 }
359 HcfResult res = HcfCX509CRLEntryCreate(rev, entryOut, ((HcfX509CRLOpensslImpl *)self)->certIssuer);
360 if (res != HCF_SUCCESS) {
361 LOGE("X509 CRL entry create fail, res : %d!", res);
362 return res;
363 }
364 return HCF_SUCCESS;
365 }
366
GetRevokedCertWithCert(HcfX509CrlSpi * self,HcfX509Certificate * cert,HcfX509CrlEntry ** entryOut)367 static HcfResult GetRevokedCertWithCert(HcfX509CrlSpi *self, HcfX509Certificate *cert,
368 HcfX509CrlEntry **entryOut)
369 {
370 if ((self == NULL) || (cert == NULL) || (entryOut == NULL)) {
371 LOGE("Invalid Paramas!");
372 return HCF_INVALID_PARAMS;
373 }
374 if (!IsClassMatch((HcfObjectBase *)self, GetClass())) {
375 LOGE("Input wrong class type!");
376 return HCF_INVALID_PARAMS;
377 }
378 X509 *certOpenssl = GetX509FromCertificate((HcfCertificate *)cert);
379 if (certOpenssl == NULL) {
380 LOGE("Input Cert is wrong !");
381 return HCF_INVALID_PARAMS;
382 }
383 X509_CRL *crl = ((HcfX509CRLOpensslImpl *)self)->crl;
384 if (crl == NULL) {
385 LOGE("crl is null!");
386 return HCF_INVALID_PARAMS;
387 }
388 X509_REVOKED *revokedRet = NULL;
389 int32_t opensslRes = X509_CRL_get0_by_cert(crl, &revokedRet, certOpenssl);
390 if (opensslRes != HCF_OPENSSL_SUCCESS) {
391 LOGE("Get revoked certificate with cert fail, res : %d!", opensslRes);
392 HcfPrintOpensslError();
393 return HCF_ERR_CRYPTO_OPERATION;
394 }
395 HcfResult res = HcfCX509CRLEntryCreate(revokedRet, entryOut, ((HcfX509CRLOpensslImpl *)self)->certIssuer);
396 if (res != HCF_SUCCESS) {
397 LOGE("X509 CRL entry create fail, res : %d!", res);
398 return res;
399 }
400 return HCF_SUCCESS;
401 }
402
DeepCopyRevokedCertificates(HcfX509CrlSpi * self,const STACK_OF (X509_REVOKED)* entrys,int32_t i,HcfArray * entrysOut)403 static HcfResult DeepCopyRevokedCertificates(HcfX509CrlSpi *self, const STACK_OF(X509_REVOKED) *entrys,
404 int32_t i, HcfArray *entrysOut)
405 {
406 X509_REVOKED *rev = sk_X509_REVOKED_value(entrys, i);
407 if (rev == NULL) {
408 LOGE("sk_X509_REVOKED_value fail!");
409 HcfPrintOpensslError();
410 return HCF_ERR_CRYPTO_OPERATION;
411 }
412 HcfX509CrlEntry *crlEntry = NULL;
413 HcfResult res = HcfCX509CRLEntryCreate(rev, &crlEntry, ((HcfX509CRLOpensslImpl *)self)->certIssuer);
414 if (res != HCF_SUCCESS || crlEntry == NULL) {
415 LOGE("X509 CRL entry create fail, res : %d!", res);
416 return res;
417 }
418 entrysOut->data[i].data = (uint8_t *)crlEntry;
419 entrysOut->data[i].len = sizeof(HcfX509CrlEntry);
420 return HCF_SUCCESS;
421 }
422
DestroyCRLEntryArray(HcfArray * arr)423 static void DestroyCRLEntryArray(HcfArray *arr)
424 {
425 if (arr == NULL) {
426 LOGD("The input array is null, no need to free.");
427 return;
428 }
429 for (uint32_t i = 0; i < arr->count; ++i) {
430 if (arr->data[i].data == NULL) {
431 continue;
432 }
433 HcfX509CrlEntry *crlEntry = (HcfX509CrlEntry *)(arr->data[i].data);
434 crlEntry->base.destroy((HcfObjectBase *)crlEntry);
435 arr->data[i].data = NULL;
436 arr->data[i].len = 0;
437 }
438 HcfFree(arr->data);
439 arr->data = NULL;
440 }
441
GetRevokedCerts(HcfX509CrlSpi * self,HcfArray * entrysOut)442 static HcfResult GetRevokedCerts(HcfX509CrlSpi *self, HcfArray *entrysOut)
443 {
444 if ((self == NULL) || (entrysOut == NULL)) {
445 LOGE("Invalid Paramas!");
446 return HCF_INVALID_PARAMS;
447 }
448 X509_CRL *crl = GetCrl(self);
449 if (crl == NULL) {
450 LOGE("crl is null!");
451 return HCF_INVALID_PARAMS;
452 }
453 STACK_OF(X509_REVOKED) *entrys = X509_CRL_get_REVOKED(crl);
454 if (entrys == NULL) {
455 LOGE("Get revoked certificates fail!");
456 HcfPrintOpensslError();
457 return HCF_ERR_CRYPTO_OPERATION;
458 }
459 int32_t revokedNum = sk_X509_REVOKED_num(entrys);
460 if ((revokedNum <= 0) || (revokedNum > MAX_REV_NUM)) {
461 LOGE("Get revoked invalid number!");
462 HcfPrintOpensslError();
463 return HCF_ERR_CRYPTO_OPERATION;
464 }
465 uint32_t blobSize = sizeof(HcfBlob) * revokedNum;
466 entrysOut->data = (HcfBlob *)HcfMalloc(blobSize, 0);
467 if (entrysOut->data == NULL) {
468 LOGE("Failed to malloc for entrysOut array!");
469 return HCF_ERR_MALLOC;
470 }
471 entrysOut->count = revokedNum;
472 for (int32_t i = 0; i < revokedNum; i++) {
473 if (DeepCopyRevokedCertificates(self, entrys, i, entrysOut) != HCF_SUCCESS) {
474 LOGE("Falied to copy revoked certificates!");
475 DestroyCRLEntryArray(entrysOut);
476 return HCF_ERR_MALLOC;
477 }
478 }
479 return HCF_SUCCESS;
480 }
481
GetTbsList(HcfX509CrlSpi * self,HcfBlob * tbsCertListOut)482 static HcfResult GetTbsList(HcfX509CrlSpi *self, HcfBlob *tbsCertListOut)
483 {
484 if ((self == NULL) || (tbsCertListOut == NULL)) {
485 LOGE("Invalid Paramas!");
486 return HCF_INVALID_PARAMS;
487 }
488 X509_CRL *crl = GetCrl(self);
489 if (crl == NULL) {
490 LOGE("crl is null!");
491 return HCF_INVALID_PARAMS;
492 }
493 unsigned char *tbs = NULL;
494 int32_t length = i2d_re_X509_CRL_tbs(crl, &tbs);
495 if ((length <= 0) || (tbs == NULL)) {
496 LOGE("Get TBS certList fail!");
497 HcfPrintOpensslError();
498 return HCF_ERR_CRYPTO_OPERATION;
499 }
500 tbsCertListOut->data = (uint8_t *)HcfMalloc(length, 0);
501 if (tbsCertListOut->data == NULL) {
502 LOGE("Failed to malloc for tbs!");
503 OPENSSL_free(tbs);
504 return HCF_ERR_MALLOC;
505 }
506 (void)memcpy_s(tbsCertListOut->data, length, tbs, length);
507 OPENSSL_free(tbs);
508 tbsCertListOut->len = length;
509 return HCF_SUCCESS;
510 }
511
GetSignature(HcfX509CrlSpi * self,HcfBlob * signature)512 static HcfResult GetSignature(HcfX509CrlSpi *self, HcfBlob *signature)
513 {
514 if ((self == NULL) || (signature == NULL)) {
515 LOGE("Invalid Paramas!");
516 return HCF_INVALID_PARAMS;
517 }
518 X509_CRL *crl = GetCrl(self);
519 if (crl == NULL) {
520 LOGE("crl is null!");
521 return HCF_INVALID_PARAMS;
522 }
523 const ASN1_BIT_STRING *asn1Signature = NULL;
524 X509_CRL_get0_signature(((HcfX509CRLOpensslImpl *)self)->crl, &asn1Signature, NULL);
525 if (asn1Signature == NULL) {
526 LOGE("Get signature is null!");
527 HcfPrintOpensslError();
528 return HCF_ERR_CRYPTO_OPERATION;
529 }
530 int32_t signatureLen = ASN1_STRING_length(asn1Signature);
531 if (signatureLen <= 0) {
532 LOGE("Get signature length is invalid!");
533 HcfPrintOpensslError();
534 return HCF_ERR_CRYPTO_OPERATION;
535 }
536 const unsigned char *signatureStr = ASN1_STRING_get0_data(asn1Signature);
537 if ((signatureStr == NULL) || (signatureLen > MAX_SIGNATURE_LEN)) {
538 LOGE("ASN1 get string fail, or signature length is too long!");
539 HcfPrintOpensslError();
540 return HCF_ERR_CRYPTO_OPERATION;
541 }
542 signature->data = (uint8_t *)HcfMalloc(signatureLen, 0);
543 if (signature->data == NULL) {
544 LOGE("Failed to malloc for signature!");
545 return HCF_ERR_MALLOC;
546 }
547 (void)memcpy_s(signature->data, signatureLen, signatureStr, signatureLen);
548 signature->len = signatureLen;
549 return HCF_SUCCESS;
550 }
551
GetSignatureAlgOidInner(X509_CRL * crl,HcfBlob * oidOut)552 static HcfResult GetSignatureAlgOidInner(X509_CRL *crl, HcfBlob *oidOut)
553 {
554 const X509_ALGOR *palg = NULL;
555 X509_CRL_get0_signature(crl, NULL, &palg);
556 if (palg == NULL) {
557 LOGE("alg is null!");
558 HcfPrintOpensslError();
559 return HCF_ERR_CRYPTO_OPERATION;
560 }
561 const ASN1_OBJECT *oid = NULL;
562 X509_ALGOR_get0(&oid, NULL, NULL, palg);
563 if (oid == NULL) {
564 LOGE("oid is null!");
565 HcfPrintOpensslError();
566 return HCF_ERR_CRYPTO_OPERATION;
567 }
568 char *output = (char *)HcfMalloc(OID_LENGTH, 0);
569 if (output == NULL) {
570 LOGE("Failed to malloc the output!");
571 return HCF_ERR_MALLOC;
572 }
573 int32_t resLen = OBJ_obj2txt(output, OID_LENGTH, oid, 1);
574 if (resLen < 0) {
575 LOGE("Failed to do OBJ_obj2txt!");
576 HcfPrintOpensslError();
577 HcfFree(output);
578 return HCF_ERR_CRYPTO_OPERATION;
579 }
580 uint32_t length = strlen(output) + 1;
581 oidOut->data = (uint8_t *)HcfMalloc(length, 0);
582 if (oidOut->data == NULL) {
583 LOGE("Failed to malloc for oidOut!");
584 HcfFree(output);
585 return HCF_ERR_MALLOC;
586 }
587 (void)memcpy_s(oidOut->data, length, output, length);
588 HcfFree(output);
589 oidOut->len = length;
590 return HCF_SUCCESS;
591 }
592
GetSignatureAlgOid(HcfX509CrlSpi * self,HcfBlob * oidOut)593 static HcfResult GetSignatureAlgOid(HcfX509CrlSpi *self, HcfBlob *oidOut)
594 {
595 if ((self == NULL) || (oidOut == NULL)) {
596 LOGE("Invalid Paramas!");
597 return HCF_INVALID_PARAMS;
598 }
599 X509_CRL *crl = GetCrl(self);
600 if (crl == NULL) {
601 LOGE("crl is null!");
602 return HCF_INVALID_PARAMS;
603 }
604 return GetSignatureAlgOidInner(crl, oidOut);
605 }
606
GetSignatureAlgName(HcfX509CrlSpi * self,HcfBlob * algNameOut)607 static HcfResult GetSignatureAlgName(HcfX509CrlSpi *self, HcfBlob *algNameOut)
608 {
609 if ((self == NULL) || (algNameOut == NULL)) {
610 LOGE("Invalid Paramas!");
611 return HCF_INVALID_PARAMS;
612 }
613 if (!IsClassMatch((HcfObjectBase *)self, GetClass())) {
614 LOGE("Input wrong class type!");
615 return HCF_INVALID_PARAMS;
616 }
617 HcfBlob *oidOut = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0);
618 HcfResult res = GetSignatureAlgOid(self, oidOut);
619 if (res != HCF_SUCCESS) {
620 LOGE("Get signature algor oid failed!");
621 HcfFree(oidOut);
622 return res;
623 }
624 const char *algName = GetAlgorithmName((const char*)(oidOut->data));
625 HcfFree(oidOut->data);
626 HcfFree(oidOut);
627 if (algName == NULL) {
628 LOGE("Can not find algorithmName!");
629 return HCF_ERR_CRYPTO_OPERATION;
630 }
631 uint32_t length = strlen(algName) + 1;
632 algNameOut->data = (uint8_t *)HcfMalloc(length, 0);
633 if (algNameOut->data == NULL) {
634 LOGE("Failed to malloc for algName!");
635 return HCF_ERR_MALLOC;
636 }
637 (void)memcpy_s(algNameOut->data, length, algName, length);
638 algNameOut->len = length;
639 return HCF_SUCCESS;
640 }
641
GetSignatureAlgParamsInner(X509_CRL * crl,HcfBlob * sigAlgParamOut)642 static HcfResult GetSignatureAlgParamsInner(X509_CRL *crl, HcfBlob *sigAlgParamOut)
643 {
644 const X509_ALGOR *palg = NULL;
645 X509_CRL_get0_signature(crl, NULL, &palg);
646 if (palg == NULL) {
647 LOGE("Get alg is null!");
648 HcfPrintOpensslError();
649 return HCF_ERR_CRYPTO_OPERATION;
650 }
651 int32_t paramType = 0;
652 const void *paramValue = NULL;
653 X509_ALGOR_get0(NULL, ¶mType, ¶mValue, palg);
654 if (paramType == V_ASN1_UNDEF) {
655 LOGE("get_X509_ALGOR_parameter, no parameters!");
656 HcfPrintOpensslError();
657 return HCF_NOT_SUPPORT;
658 }
659 ASN1_TYPE *param = ASN1_TYPE_new();
660 if (ASN1_TYPE_set1(param, paramType, paramValue) != HCF_OPENSSL_SUCCESS) {
661 LOGE("Set type fail!");
662 ASN1_TYPE_free(param);
663 HcfPrintOpensslError();
664 return HCF_ERR_CRYPTO_OPERATION;
665 }
666 unsigned char *outParams = NULL;
667 int32_t length = i2d_ASN1_TYPE(param, &outParams);
668 ASN1_TYPE_free(param);
669 if (length <= 0) {
670 LOGE("Do i2d_ASN1_TYPE fail!");
671 HcfPrintOpensslError();
672 return HCF_ERR_CRYPTO_OPERATION;
673 }
674 sigAlgParamOut->data = (uint8_t *)HcfMalloc(length, 0);
675 if (sigAlgParamOut->data == NULL) {
676 LOGE("Failed to malloc for sigAlgParam!");
677 OPENSSL_free(outParams);
678 return HCF_ERR_MALLOC;
679 }
680 (void)memcpy_s(sigAlgParamOut->data, length, outParams, length);
681 sigAlgParamOut->len = length;
682 OPENSSL_free(outParams);
683 return HCF_SUCCESS;
684 }
685
GetSignatureAlgParams(HcfX509CrlSpi * self,HcfBlob * sigAlgParamOut)686 static HcfResult GetSignatureAlgParams(HcfX509CrlSpi *self, HcfBlob *sigAlgParamOut)
687 {
688 if ((self == NULL) || (sigAlgParamOut == NULL)) {
689 LOGE("Invalid Paramas!");
690 return HCF_INVALID_PARAMS;
691 }
692 X509_CRL *crl = GetCrl(self);
693 if (crl == NULL) {
694 LOGE("crl is null!");
695 return HCF_INVALID_PARAMS;
696 }
697 return GetSignatureAlgParamsInner(crl, sigAlgParamOut);
698 }
699
Destroy(HcfObjectBase * self)700 static void Destroy(HcfObjectBase *self)
701 {
702 if (self == NULL) {
703 return;
704 }
705 if (!IsClassMatch(self, GetClass())) {
706 LOGE("Input wrong class type!");
707 return;
708 }
709 HcfX509CRLOpensslImpl *realCrl = (HcfX509CRLOpensslImpl *)self;
710 X509_CRL_free(realCrl->crl);
711 realCrl->crl = NULL;
712 if (realCrl->certIssuer != NULL) {
713 HcfFree(realCrl->certIssuer->data);
714 realCrl->certIssuer->data = NULL;
715 HcfFree(realCrl->certIssuer);
716 realCrl->certIssuer = NULL;
717 }
718 HcfFree(realCrl);
719 }
720
ParseX509CRL(const HcfEncodingBlob * inStream)721 static X509_CRL *ParseX509CRL(const HcfEncodingBlob *inStream)
722 {
723 if ((inStream->data == NULL) || (inStream->len <= 0)) {
724 LOGE("Invalid Paramas!");
725 return NULL;
726 }
727 BIO *bio = BIO_new_mem_buf(inStream->data, inStream->len);
728 if (bio == NULL) {
729 LOGE("bio get null!");
730 HcfPrintOpensslError();
731 return NULL;
732 }
733 X509_CRL *crlOut = NULL;
734 switch (inStream->encodingFormat) {
735 case HCF_FORMAT_DER:
736 crlOut = d2i_X509_CRL_bio(bio, NULL);
737 break;
738 case HCF_FORMAT_PEM:
739 crlOut = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL);
740 break;
741 default:
742 LOGE("Not support format!");
743 break;
744 }
745 BIO_free_all(bio);
746 if (crlOut == NULL) {
747 LOGE("Parse X509 CRL fail!");
748 HcfPrintOpensslError();
749 return NULL;
750 }
751 return crlOut;
752 }
753
HcfCX509CrlSpiCreate(const HcfEncodingBlob * inStream,HcfX509CrlSpi ** spi)754 HcfResult HcfCX509CrlSpiCreate(const HcfEncodingBlob *inStream, HcfX509CrlSpi **spi)
755 {
756 if ((inStream == NULL) || (inStream->data == NULL) || (spi == NULL)) {
757 LOGE("Invalid Paramas!");
758 return HCF_INVALID_PARAMS;
759 }
760 HcfX509CRLOpensslImpl *returnCRL = (HcfX509CRLOpensslImpl *)HcfMalloc(sizeof(HcfX509CRLOpensslImpl), 0);
761 if (returnCRL == NULL) {
762 LOGE("Failed to malloc for x509 instance!");
763 return HCF_ERR_MALLOC;
764 }
765 X509_CRL *crl = ParseX509CRL(inStream);
766 if (crl == NULL) {
767 LOGE("Failed to Parse x509 CRL!");
768 HcfFree(returnCRL);
769 return HCF_INVALID_PARAMS;
770 }
771 returnCRL->crl = crl;
772 returnCRL->certIssuer = NULL;
773 returnCRL->base.base.getClass = GetClass;
774 returnCRL->base.base.destroy = Destroy;
775 returnCRL->base.engineIsRevoked = IsRevoked;
776 returnCRL->base.engineGetType = GetType;
777 returnCRL->base.engineGetEncoded = GetEncoded;
778 returnCRL->base.engineVerify = Verify;
779 returnCRL->base.engineGetVersion = GetVersion;
780 returnCRL->base.engineGetIssuerName = GetIssuerName;
781 returnCRL->base.engineGetLastUpdate = GetLastUpdate;
782 returnCRL->base.engineGetNextUpdate = GetNextUpdate;
783 returnCRL->base.engineGetRevokedCert = GetRevokedCert;
784 returnCRL->base.engineGetRevokedCertWithCert = GetRevokedCertWithCert;
785 returnCRL->base.engineGetRevokedCerts = GetRevokedCerts;
786 returnCRL->base.engineGetTbsInfo = GetTbsList;
787 returnCRL->base.engineGetSignature = GetSignature;
788 returnCRL->base.engineGetSignatureAlgName = GetSignatureAlgName;
789 returnCRL->base.engineGetSignatureAlgOid = GetSignatureAlgOid;
790 returnCRL->base.engineGetSignatureAlgParams = GetSignatureAlgParams;
791 if (SetCertIssuer((HcfX509CrlSpi *)returnCRL) != HCF_SUCCESS) {
792 LOGI("No cert issuer find or set cert issuer fail!");
793 }
794 *spi = (HcfX509CrlSpi *)returnCRL;
795 return HCF_SUCCESS;
796 }