• 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 "cf_object_cert.h"
17 
18 #include "securec.h"
19 
20 #include "cf_ability.h"
21 #include "cf_log.h"
22 #include "cf_magic.h"
23 #include "cf_memory.h"
24 #include "cf_param.h"
25 #include "cf_param_parse.h"
26 #include "cf_result.h"
27 
28 #include "cf_cert_adapter_ability_define.h"
29 
30 typedef struct {
31     CfBase base;
32     CfCertAdapterAbilityFunc func;
33     CfBase *adapterRes;
34 } CfCertObjStruct;
35 
CfCertCreate(const CfEncodingBlob * in,CfBase ** obj)36 int32_t CfCertCreate(const CfEncodingBlob *in, CfBase **obj)
37 {
38     if ((in == NULL) || (obj == NULL)) {
39         CF_LOG_E("param null");
40         return CF_NULL_POINTER;
41     }
42 
43     CfCertAdapterAbilityFunc *func = (CfCertAdapterAbilityFunc *)GetAbility(CF_ABILITY(CF_ABILITY_TYPE_ADAPTER,
44         CF_OBJ_TYPE_CERT));
45     if ((func == NULL) || (func->base.type != CF_MAGIC(CF_MAGIC_TYPE_ADAPTER_FUNC, CF_OBJ_TYPE_CERT))) {
46         CF_LOG_E("invalid func type");
47         return CF_INVALID_PARAMS;
48     }
49 
50     CfCertObjStruct *tmp = CfMalloc(sizeof(CfCertObjStruct), 0);
51     if (tmp == NULL) {
52         CF_LOG_E("malloc cert obj failed");
53         return CF_ERR_MALLOC;
54     }
55     tmp->base.type = CF_MAGIC(CF_MAGIC_TYPE_OBJ_RESOURCE, CF_OBJ_TYPE_CERT);
56 
57     int32_t ret = func->adapterCreate(in, &tmp->adapterRes);
58     if (ret != CF_SUCCESS) {
59         CF_LOG_E("cert adapter create failed");
60         CfFree(tmp);
61         tmp = NULL;
62         return ret;
63     }
64     (void)memcpy_s(&tmp->func, sizeof(CfCertAdapterAbilityFunc), func, sizeof(CfCertAdapterAbilityFunc));
65 
66     *obj = &(tmp->base);
67     return CF_SUCCESS;
68 }
69 
CfCertGetItem(const CfCertObjStruct * obj,const CfParamSet * in,CfParamSet ** out)70 static int32_t CfCertGetItem(const CfCertObjStruct *obj, const CfParamSet *in, CfParamSet **out)
71 {
72     CfParam *tmpParam = NULL;
73     int32_t ret = CfGetParam(in, CF_TAG_PARAM0_INT32, &tmpParam);
74     if (ret != CF_SUCCESS) {
75         CF_LOG_E("get item id failed, ret = %{public}d", ret);
76         return ret;
77     }
78 
79     CF_LOG_I("cert get type = 0x%{public}x", tmpParam->int32Param);
80     CfBlob itemValue = { 0, NULL };
81     ret = obj->func.adapterGetItem(obj->adapterRes, (CfItemId)tmpParam->int32Param, &itemValue);
82     if (ret != CF_SUCCESS) {
83         CF_LOG_E("adapter get item failed, ret = %{public}d", ret);
84         return ret;
85     }
86 
87     CfParam params[] = {
88         { .tag = CF_TAG_RESULT_TYPE, .int32Param = CF_TAG_TYPE_BYTES },
89         { .tag = CF_TAG_RESULT_BYTES, .blob = itemValue },
90     };
91     ret = CfConstructParamSetOut(params, sizeof(params) / sizeof(CfParam), out);
92     CfFree(itemValue.data);
93     itemValue.data = NULL;
94     return ret;
95 }
96 
CfCertGet(const CfBase * obj,const CfParamSet * in,CfParamSet ** out)97 int32_t CfCertGet(const CfBase *obj, const CfParamSet *in, CfParamSet **out)
98 {
99     if ((obj == NULL) || (in == NULL) || (out == NULL)) {
100         CF_LOG_E("cfcertget params is null");
101         return CF_NULL_POINTER;
102     }
103 
104     CfCertObjStruct *tmp = (CfCertObjStruct *)obj;
105     if (tmp->base.type != CF_MAGIC(CF_MAGIC_TYPE_OBJ_RESOURCE, CF_OBJ_TYPE_CERT)) {
106         CF_LOG_E("invalid resource type");
107         return CF_INVALID_PARAMS;
108     }
109 
110     CfParam *tmpParam = NULL;
111     int32_t ret = CfGetParam(in, CF_TAG_GET_TYPE, &tmpParam);
112     if (ret != CF_SUCCESS) {
113         CF_LOG_E("get param item type failed, ret = %{public}d", ret);
114         return ret;
115     }
116 
117     switch (tmpParam->int32Param) {
118         case CF_GET_TYPE_CERT_ITEM:
119             return CfCertGetItem(tmp, in, out);
120         default:
121             CF_LOG_E("cert get type invalid, type = %{public}d", tmpParam->int32Param);
122             return CF_NOT_SUPPORT;
123     }
124 }
125 
CfCertCheck(const CfBase * obj,const CfParamSet * in,CfParamSet ** out)126 int32_t CfCertCheck(const CfBase *obj, const CfParamSet *in, CfParamSet **out)
127 {
128     if ((obj == NULL) || (in == NULL) || (out == NULL)) {
129         CF_LOG_E("cfcertcheck params is null");
130         return CF_NULL_POINTER;
131     }
132 
133     CfCertObjStruct *tmp = (CfCertObjStruct *)obj;
134     if (tmp->base.type != CF_MAGIC(CF_MAGIC_TYPE_OBJ_RESOURCE, CF_OBJ_TYPE_CERT)) {
135         CF_LOG_E("invalid resource type");
136         return CF_INVALID_PARAMS;
137     }
138 
139     return CF_SUCCESS; /* reserve check function */
140 }
141 
CfCertDestroy(CfBase ** obj)142 void CfCertDestroy(CfBase **obj)
143 {
144     if ((obj == NULL) || (*obj == NULL)) {
145         return;
146     }
147 
148     CfCertObjStruct *tmp = (CfCertObjStruct *)*obj;
149     if (tmp->base.type != CF_MAGIC(CF_MAGIC_TYPE_OBJ_RESOURCE, CF_OBJ_TYPE_CERT)) {
150         /* only cert objects can be destroyed */
151         CF_LOG_E("invalid resource type");
152         return;
153     }
154 
155     tmp->func.adapterDestory(&tmp->adapterRes);
156     CfFree(tmp);
157     *obj = NULL;
158     return;
159 }
160 
161