1 /*
2 * Copyright (c) 2024 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_param.h"
17
18 #include "cj_cf_object.h"
19
FfiCertCjCfObjectNewInstance(const CfEncodingBlob * blob,CjCfObject * returnObj)20 int32_t FfiCertCjCfObjectNewInstance(const CfEncodingBlob *blob, CjCfObject *returnObj)
21 {
22 auto cfObj = static_cast<CfObject *>(malloc(sizeof(CfObject)));
23 if (cfObj == nullptr) {
24 return CF_ERR_MALLOC;
25 }
26 const auto errCode = CfResult(CfCreate(CF_OBJ_TYPE_EXTENSION, blob, &cfObj));
27 if (errCode != CF_SUCCESS) {
28 free(cfObj);
29 return errCode;
30 }
31 returnObj->cfObj = cfObj;
32 return CF_SUCCESS;
33 }
34
FfiCertCjCfObjectDeleteInstance(CjCfObject self)35 void FfiCertCjCfObjectDeleteInstance(CjCfObject self)
36 {
37 self.cfObj->destroy(&self.cfObj);
38 }
39
FfiCertCjCfObjectGetEncoded(const CjCfObject self,CfBlob * out)40 CfResult FfiCertCjCfObjectGetEncoded(const CjCfObject self, CfBlob *out)
41 {
42 CfParamSet *inParamSet = nullptr;
43 int32_t ret;
44 if ((ret = CfInitParamSet(&inParamSet)) != CF_SUCCESS) {
45 return CfResult(ret);
46 }
47
48 const CfParam param[] = {
49 CfParam{.tag = CF_TAG_GET_TYPE, .int32Param = CF_GET_TYPE_EXT_ITEM},
50 CfParam{.tag = CF_TAG_PARAM0_INT32, .int32Param = CF_ITEM_ENCODED},
51 };
52 if ((ret = CfAddParams(inParamSet, param, sizeof(param) / sizeof(CfParam))) != CF_SUCCESS) {
53 CfFreeParamSet(&inParamSet);
54 return CfResult(ret);
55 }
56
57 CfParamSet *outParamSet = nullptr;
58 if ((ret = self.cfObj->get(self.cfObj, inParamSet, &outParamSet)) != CF_SUCCESS) {
59 CfFreeParamSet(&inParamSet);
60 return CfResult(ret);
61 }
62
63 CfParam *resultParam = nullptr;
64 ret = CfGetParam(outParamSet, CF_TAG_RESULT_BYTES, &resultParam);
65 CfFreeParamSet(&inParamSet);
66 CfFreeParamSet(&outParamSet);
67
68 if (ret == CF_SUCCESS) {
69 *out = resultParam->blob;
70 }
71 return CfResult(ret);
72 }
73
FfiCertCjCfObjectGetOidList(const CjCfObject self,int32_t valueType,CfArray * out)74 CfResult FfiCertCjCfObjectGetOidList(const CjCfObject self, int32_t valueType, CfArray *out)
75 {
76 CfParamSet *inParamSet = nullptr;
77 int32_t ret;
78 if ((ret = CfInitParamSet(&inParamSet)) != CF_SUCCESS) {
79 return CfResult(ret);
80 }
81
82 const CfParam param[] = {
83 CfParam{.tag = CF_TAG_GET_TYPE, .int32Param = CF_GET_TYPE_EXT_OIDS},
84 CfParam{.tag = CF_TAG_PARAM0_INT32, .int32Param = valueType},
85 };
86 if ((ret = CfAddParams(inParamSet, param, sizeof(param) / sizeof(CfParam))) != CF_SUCCESS) {
87 CfFreeParamSet(&inParamSet);
88 return CfResult(ret);
89 }
90
91 CfParamSet *outParamSet = nullptr;
92 if ((ret = self.cfObj->get(self.cfObj, inParamSet, &outParamSet)) != CF_SUCCESS) {
93 CfFreeParamSet(&inParamSet);
94 return CfResult(ret);
95 }
96
97 if (outParamSet->paramSetSize <= 1) {
98 ret = CF_INVALID_PARAMS;
99 } else {
100 out->count = outParamSet->paramsCnt - 1;
101 out->format = CF_FORMAT_DER;
102 out->data = static_cast<CfBlob *>(malloc(sizeof(CfBlob) * out->count));
103 if (out->data == nullptr) {
104 CfFreeParamSet(&inParamSet);
105 CfFreeParamSet(&outParamSet);
106 return CF_ERR_MALLOC;
107 }
108 for (uint32_t i = 0; i < out->count; ++i) {
109 out->data[i] = outParamSet->params[i + 1].blob;
110 }
111 ret = CF_SUCCESS;
112 }
113
114 CfFreeParamSet(&inParamSet);
115 CfFreeParamSet(&outParamSet);
116 return CfResult(ret);
117 }
118
FfiCertCjCfObjectGetEntry(const CjCfObject self,int32_t valueType,CfBlob * oid,CfBlob * out)119 CfResult FfiCertCjCfObjectGetEntry(const CjCfObject self, int32_t valueType, CfBlob *oid, CfBlob *out)
120 {
121 CfParamSet *inParamSet = nullptr;
122 int32_t ret;
123 if ((ret = CfInitParamSet(&inParamSet)) != CF_SUCCESS) {
124 return CfResult(ret);
125 }
126
127 const CfParam param[] = {
128 CfParam{.tag = CF_TAG_GET_TYPE, .int32Param = CF_GET_TYPE_EXT_ENTRY},
129 CfParam{.tag = CF_TAG_PARAM0_INT32, .int32Param = valueType},
130 CfParam{.tag = CF_TAG_PARAM1_BUFFER, .blob = *oid},
131 };
132 if ((ret = CfAddParams(inParamSet, param, sizeof(param) / sizeof(CfParam))) != CF_SUCCESS) {
133 CfFreeParamSet(&inParamSet);
134 return CfResult(ret);
135 }
136
137 CfParamSet *outParamSet = nullptr;
138 if ((ret = self.cfObj->get(self.cfObj, inParamSet, &outParamSet)) != CF_SUCCESS) {
139 CfFreeParamSet(&inParamSet);
140 return CfResult(ret);
141 }
142 CfParam *resultParam = nullptr;
143 ret = CfGetParam(outParamSet, CF_TAG_RESULT_BYTES, &resultParam);
144 CfFreeParamSet(&inParamSet);
145 CfFreeParamSet(&outParamSet);
146
147 if (ret == CF_SUCCESS) {
148 *out = resultParam->blob;
149 }
150 return CfResult(ret);
151 }
152
FfiCertCjCfObjectCheckCA(const CjCfObject self,int32_t * out)153 CfResult FfiCertCjCfObjectCheckCA(const CjCfObject self, int32_t *out)
154 {
155 CfParamSet *inParamSet = nullptr;
156 int32_t ret;
157 if ((ret = CfInitParamSet(&inParamSet)) != CF_SUCCESS) {
158 return CfResult(ret);
159 }
160
161 const CfParam param[] = {
162 CfParam{.tag = CF_TAG_CHECK_TYPE, .int32Param = CF_CHECK_TYPE_EXT_CA},
163 };
164 if ((ret = CfAddParams(inParamSet, param, sizeof(param) / sizeof(CfParam))) != CF_SUCCESS) {
165 CfFreeParamSet(&inParamSet);
166 return CfResult(ret);
167 }
168
169 CfParamSet *outParamSet = nullptr;
170 if ((ret = self.cfObj->check(self.cfObj, inParamSet, &outParamSet)) != CF_SUCCESS) {
171 CfFreeParamSet(&inParamSet);
172 return CfResult(ret);
173 }
174 CfParam *resultParam = nullptr;
175 ret = CfGetParam(outParamSet, CF_TAG_RESULT_INT, &resultParam);
176 CfFreeParamSet(&inParamSet);
177 CfFreeParamSet(&outParamSet);
178
179 if (ret == CF_SUCCESS) {
180 *out = resultParam->int32Param;
181 }
182 return CfResult(ret);
183 }
184
FfiCertCjCfObjectHasUnsupportedCriticalExtension(const CjCfObject self,bool * out)185 CfResult FfiCertCjCfObjectHasUnsupportedCriticalExtension(const CjCfObject self, bool *out)
186 {
187 CfParamSet *inParamSet = nullptr;
188 int32_t ret;
189 if ((ret = CfInitParamSet(&inParamSet)) != CF_SUCCESS) {
190 return CfResult(ret);
191 }
192
193 const CfParam param[] = {
194 CfParam{.tag = CF_TAG_CHECK_TYPE, .int32Param = CF_CHECK_TYPE_EXT_HAS_UN_SUPPORT},
195 };
196 if ((ret = CfAddParams(inParamSet, param, sizeof(param) / sizeof(CfParam))) != CF_SUCCESS) {
197 CfFreeParamSet(&inParamSet);
198 return CfResult(ret);
199 }
200
201 CfParamSet *outParamSet = nullptr;
202 if ((ret = self.cfObj->check(self.cfObj, inParamSet, &outParamSet)) != CF_SUCCESS) {
203 CfFreeParamSet(&inParamSet);
204 return CfResult(ret);
205 }
206 CfParam *resultParam = nullptr;
207 ret = CfGetParam(outParamSet, CF_TAG_RESULT_BOOL, &resultParam);
208 CfFreeParamSet(&inParamSet);
209 CfFreeParamSet(&outParamSet);
210
211 if (ret == CF_SUCCESS) {
212 *out = resultParam->boolParam;
213 }
214 return CfResult(ret);
215 }
216