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