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