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 "cert_crl_common.h"
17 #include "cf_log.h"
18 #include "cf_memory.h"
19 
CloneCertificateObj(HcfX509Certificate * in,HcfX509Certificate ** out)20 CfResult CloneCertificateObj(HcfX509Certificate *in, HcfX509Certificate **out)
21 {
22     CfEncodingBlob encodingBlob = { 0 };
23     CfResult res = in->base.getEncoded(&(in->base), &encodingBlob);
24     if (res != CF_SUCCESS) {
25         LOGE("Get encoded failed.");
26         return res;
27     }
28     HcfX509Certificate *clone = NULL;
29     res = HcfX509CertificateCreate(&encodingBlob, &clone);
30     if (res != CF_SUCCESS) {
31         LOGE("Failed to HcfX509CertificateCreate!");
32         CfFree(encodingBlob.data);
33         return res;
34     }
35     *out = clone;
36     CfFree(encodingBlob.data);
37     return CF_SUCCESS;
38 }
39 
CloneCrlObj(HcfX509Crl * in,HcfX509Crl ** out)40 CfResult CloneCrlObj(HcfX509Crl *in, HcfX509Crl **out)
41 {
42     CfEncodingBlob encodingBlob = { 0 };
43     CfResult res = in->getEncoded(in, &encodingBlob);
44     if (res != CF_SUCCESS) {
45         LOGE("Failed to getEncoded!");
46         return res;
47     }
48     HcfX509Crl *clone = NULL;
49     res = HcfX509CrlCreate(&encodingBlob, &clone);
50     if (res != CF_SUCCESS) {
51         LOGE("Failed to HcfX509CrlCreate!");
52         CfFree(encodingBlob.data);
53         return res;
54     }
55     *out = clone;
56     CfFree(encodingBlob.data);
57     return CF_SUCCESS;
58 }
59 
FreeCertArrayData(HcfX509CertificateArray * certs)60 void FreeCertArrayData(HcfX509CertificateArray *certs)
61 {
62     if (certs == NULL|| certs->data == NULL) {
63         return;
64     }
65     for (uint32_t i = 0; i < certs->count; ++i) {
66         CfObjDestroy(certs->data[i]);
67     }
68     CF_FREE_PTR(certs->data);
69     certs->count = 0;
70 }
71 
FreeCrlArrayData(HcfX509CrlArray * crls)72 void FreeCrlArrayData(HcfX509CrlArray *crls)
73 {
74     if (crls == NULL) {
75         return;
76     }
77     for (uint32_t i = 0; i < crls->count; ++i) {
78         CfObjDestroy(crls->data[i]);
79     }
80     CF_FREE_PTR(crls->data);
81     crls->count = 0;
82 }
83