1 /*
2 * Copyright (c) 2021-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 "hks_ipc_serialization.h"
17
18 #include <stddef.h>
19 #include <stdint.h>
20 #include <string.h>
21
22 #include "hks_log.h"
23 #include "hks_mem.h"
24 #include "hks_param.h"
25 #include "hks_template.h"
26 #include "securec.h"
27
28 #define NUM_TWO 2
29
30 static const uint8_t g_base64Table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
31
CopyUint32ToBuffer(uint32_t value,const struct HksBlob * destBlob,uint32_t * destOffset)32 int32_t CopyUint32ToBuffer(uint32_t value, const struct HksBlob *destBlob, uint32_t *destOffset)
33 {
34 if ((*destOffset > destBlob->size) || ((destBlob->size - *destOffset) < sizeof(value))) {
35 return HKS_ERROR_BUFFER_TOO_SMALL;
36 }
37
38 (void)memcpy_s(destBlob->data + *destOffset, destBlob->size - *destOffset, &value, sizeof(value));
39 *destOffset += sizeof(value);
40
41 return HKS_SUCCESS;
42 }
43
CopyBlobToBuffer(const struct HksBlob * blob,const struct HksBlob * destBlob,uint32_t * destOffset)44 static int32_t CopyBlobToBuffer(const struct HksBlob *blob, const struct HksBlob *destBlob, uint32_t *destOffset)
45 {
46 if ((*destOffset > destBlob->size) ||
47 ((destBlob->size - *destOffset) < (sizeof(blob->size) + ALIGN_SIZE(blob->size)))) {
48 return HKS_ERROR_BUFFER_TOO_SMALL;
49 }
50
51 (void)memcpy_s(destBlob->data + *destOffset, destBlob->size - *destOffset, &(blob->size), sizeof(blob->size));
52 *destOffset += sizeof(blob->size);
53
54 (void)memcpy_s(destBlob->data + *destOffset, destBlob->size - *destOffset, blob->data, blob->size);
55 *destOffset += ALIGN_SIZE(blob->size);
56
57 return HKS_SUCCESS;
58 }
59
CopyParamSetToBuffer(const struct HksParamSet * paramSet,const struct HksBlob * destBlob,uint32_t * destOffset)60 static int32_t CopyParamSetToBuffer(const struct HksParamSet *paramSet,
61 const struct HksBlob *destBlob, uint32_t *destOffset)
62 {
63 if ((*destOffset > destBlob->size) || (destBlob->size - *destOffset < ALIGN_SIZE(paramSet->paramSetSize))) {
64 return HKS_ERROR_BUFFER_TOO_SMALL;
65 }
66
67 (void)memcpy_s(destBlob->data + *destOffset, destBlob->size - *destOffset, paramSet, paramSet->paramSetSize);
68 *destOffset += ALIGN_SIZE(paramSet->paramSetSize);
69
70 return HKS_SUCCESS;
71 }
72
GetUint32FromBuffer(uint32_t * value,const struct HksBlob * srcBlob,uint32_t * srcOffset)73 static int32_t GetUint32FromBuffer(uint32_t *value, const struct HksBlob *srcBlob, uint32_t *srcOffset)
74 {
75 if ((*srcOffset > srcBlob->size) || (srcBlob->size - *srcOffset < sizeof(*value))) {
76 return HKS_ERROR_BUFFER_TOO_SMALL;
77 }
78
79 *value = *((uint32_t *)(srcBlob->data + *srcOffset));
80 *srcOffset += sizeof(*value);
81 return HKS_SUCCESS;
82 }
83
GetBlobFromBuffer(struct HksBlob * blob,const struct HksBlob * srcBlob,uint32_t * srcOffset)84 static int32_t GetBlobFromBuffer(struct HksBlob *blob, const struct HksBlob *srcBlob, uint32_t *srcOffset)
85 {
86 if ((*srcOffset > srcBlob->size) || ((srcBlob->size - *srcOffset) < sizeof(blob->size))) {
87 return HKS_ERROR_BUFFER_TOO_SMALL;
88 }
89
90 uint32_t size = *((uint32_t *)(srcBlob->data + *srcOffset));
91 if (ALIGN_SIZE(size) > (srcBlob->size - *srcOffset - sizeof(blob->size))) {
92 return HKS_ERROR_BUFFER_TOO_SMALL;
93 }
94 blob->size = size;
95 *srcOffset += sizeof(blob->size);
96 blob->data = (uint8_t *)(srcBlob->data + *srcOffset);
97 *srcOffset += ALIGN_SIZE(blob->size);
98
99 return HKS_SUCCESS;
100 }
101
GetParamSetFromBuffer(struct HksParamSet ** paramSet,const struct HksBlob * srcBlob,uint32_t * srcOffset)102 static int32_t GetParamSetFromBuffer(struct HksParamSet **paramSet,
103 const struct HksBlob *srcBlob, uint32_t *srcOffset)
104 {
105 if ((*srcOffset > srcBlob->size) || ((srcBlob->size - *srcOffset) < sizeof(struct HksParamSet))) {
106 return HKS_ERROR_BUFFER_TOO_SMALL;
107 }
108
109 *paramSet = (struct HksParamSet *)(srcBlob->data + *srcOffset);
110 if (ALIGN_SIZE((*paramSet)->paramSetSize) > (srcBlob->size - *srcOffset)) {
111 return HKS_ERROR_BUFFER_TOO_SMALL;
112 }
113 *srcOffset += ALIGN_SIZE((*paramSet)->paramSetSize);
114
115 return HKS_SUCCESS;
116 }
117
HksGenerateKeyPack(struct HksBlob * destData,const struct HksBlob * keyAlias,const struct HksParamSet * paramSetIn,const struct HksBlob * keyOut)118 int32_t HksGenerateKeyPack(struct HksBlob *destData, const struct HksBlob *keyAlias,
119 const struct HksParamSet *paramSetIn, const struct HksBlob *keyOut)
120 {
121 uint32_t offset = 0;
122 int32_t ret = CopyBlobToBuffer(keyAlias, destData, &offset);
123 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy keyAlias failed")
124
125 ret = CopyParamSetToBuffer(paramSetIn, destData, &offset);
126 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy paramSetIn failed")
127 return CopyUint32ToBuffer(keyOut->size, destData, &offset);
128 }
129
HksImportKeyPack(struct HksBlob * destData,const struct HksBlob * keyAlias,const struct HksParamSet * paramSet,const struct HksBlob * key)130 int32_t HksImportKeyPack(struct HksBlob *destData, const struct HksBlob *keyAlias, const struct HksParamSet *paramSet,
131 const struct HksBlob *key)
132 {
133 uint32_t offset = 0;
134 int32_t ret = CopyBlobToBuffer(keyAlias, destData, &offset);
135 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy keyAlias failed")
136
137 ret = CopyParamSetToBuffer(paramSet, destData, &offset);
138 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy paramSet failed")
139 return CopyBlobToBuffer(key, destData, &offset);
140 }
141
HksImportWrappedKeyPack(struct HksBlob * destData,const struct HksBlob * keyAlias,const struct HksBlob * wrappingKeyAlias,const struct HksParamSet * paramSet,const struct HksBlob * wrappedKeyData)142 int32_t HksImportWrappedKeyPack(struct HksBlob *destData, const struct HksBlob *keyAlias,
143 const struct HksBlob *wrappingKeyAlias, const struct HksParamSet *paramSet, const struct HksBlob *wrappedKeyData)
144 {
145 uint32_t offset = 0;
146 int32_t ret = CopyBlobToBuffer(keyAlias, destData, &offset);
147 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy keyAlias failed")
148
149 ret = CopyBlobToBuffer(wrappingKeyAlias, destData, &offset);
150 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy wrappingKeyAlias failed")
151
152 ret = CopyParamSetToBuffer(paramSet, destData, &offset);
153 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy paramSet failed")
154 return CopyBlobToBuffer(wrappedKeyData, destData, &offset);
155 }
156
HksExportPublicKeyPack(struct HksBlob * destData,const struct HksBlob * keyAlias,const struct HksBlob * key)157 int32_t HksExportPublicKeyPack(struct HksBlob *destData, const struct HksBlob *keyAlias, const struct HksBlob *key)
158 {
159 uint32_t offset = 0;
160 int32_t ret = CopyBlobToBuffer(keyAlias, destData, &offset);
161 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy keyAlias failed")
162 return CopyUint32ToBuffer(key->size, destData, &offset);
163 }
164
HksGetKeyParamSetPack(struct HksBlob * destData,const struct HksBlob * keyAlias,const struct HksBlob * keyOut)165 int32_t HksGetKeyParamSetPack(struct HksBlob *destData, const struct HksBlob *keyAlias, const struct HksBlob *keyOut)
166 {
167 uint32_t offset = 0;
168 int32_t ret = CopyBlobToBuffer(keyAlias, destData, &offset);
169 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy keyAlias failed")
170 return CopyUint32ToBuffer(keyOut->size, destData, &offset);
171 }
172
HksOnceParamPack(struct HksBlob * destData,const struct HksBlob * key,const struct HksParamSet * paramSet,uint32_t * offset)173 int32_t HksOnceParamPack(struct HksBlob *destData, const struct HksBlob *key, const struct HksParamSet *paramSet,
174 uint32_t *offset)
175 {
176 int32_t ret = CopyBlobToBuffer(key, destData, offset);
177 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy key failed")
178
179 return CopyParamSetToBuffer(paramSet, destData, offset);
180 }
181
HksOnceDataPack(struct HksBlob * destData,const struct HksBlob * inputData,const struct HksBlob * rsvData,const struct HksBlob * outputData,uint32_t * offset)182 int32_t HksOnceDataPack(struct HksBlob *destData, const struct HksBlob *inputData, const struct HksBlob *rsvData,
183 const struct HksBlob *outputData, uint32_t *offset)
184 {
185 int32_t ret = CopyBlobToBuffer(inputData, destData, offset);
186 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy inputData failed")
187
188 if (rsvData != NULL) {
189 ret = CopyBlobToBuffer(rsvData, destData, offset);
190 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy rsvData failed")
191 }
192
193 if (outputData != NULL) {
194 ret = CopyUint32ToBuffer(outputData->size, destData, offset);
195 }
196 return ret;
197 }
198
HksAgreeKeyPack(struct HksBlob * destData,const struct HksParamSet * paramSet,const struct HksBlob * privateKey,const struct HksBlob * peerPublicKey,const struct HksBlob * agreedKey)199 int32_t HksAgreeKeyPack(struct HksBlob *destData, const struct HksParamSet *paramSet, const struct HksBlob *privateKey,
200 const struct HksBlob *peerPublicKey, const struct HksBlob *agreedKey)
201 {
202 uint32_t offset = 0;
203 int32_t ret = CopyParamSetToBuffer(paramSet, destData, &offset);
204 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy paramSet failed")
205
206 ret = CopyBlobToBuffer(privateKey, destData, &offset);
207 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy privateKey failed")
208
209 ret = CopyBlobToBuffer(peerPublicKey, destData, &offset);
210 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy peerPublicKey failed")
211 return CopyUint32ToBuffer(agreedKey->size, destData, &offset);
212 }
213
HksDeriveKeyPack(struct HksBlob * destData,const struct HksParamSet * paramSet,const struct HksBlob * kdfKey,const struct HksBlob * derivedKey)214 int32_t HksDeriveKeyPack(struct HksBlob *destData, const struct HksParamSet *paramSet, const struct HksBlob *kdfKey,
215 const struct HksBlob *derivedKey)
216 {
217 uint32_t offset = 0;
218 int32_t ret = CopyParamSetToBuffer(paramSet, destData, &offset);
219 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy paramSet failed")
220
221 ret = CopyBlobToBuffer(kdfKey, destData, &offset);
222 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy kdfKey failed")
223 return CopyUint32ToBuffer(derivedKey->size, destData, &offset);
224 }
225
HksGetKeyInfoListPack(struct HksBlob * destData,uint32_t listCount,const struct HksKeyInfo * keyInfoList)226 int32_t HksGetKeyInfoListPack(struct HksBlob *destData, uint32_t listCount, const struct HksKeyInfo *keyInfoList)
227 {
228 uint32_t offset = 0;
229 int32_t ret = CopyUint32ToBuffer(listCount, destData, &offset);
230 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy listCount failed")
231
232 for (uint32_t i = 0; i < listCount; ++i) {
233 ret = CopyUint32ToBuffer(keyInfoList[i].alias.size, destData, &offset);
234 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy alias failed")
235
236 ret = CopyUint32ToBuffer(keyInfoList[i].paramSet->paramSetSize, destData, &offset);
237 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy paramSetSize failed")
238 }
239 return HKS_SUCCESS;
240 }
241
HksGetKeyInfoListUnpackFromService(const struct HksBlob * srcData,uint32_t * listCount,struct HksKeyInfo * keyInfoList)242 int32_t HksGetKeyInfoListUnpackFromService(const struct HksBlob *srcData, uint32_t *listCount,
243 struct HksKeyInfo *keyInfoList)
244 {
245 uint32_t offset = 0;
246 uint32_t countFromBuffer;
247 int32_t ret = GetUint32FromBuffer(&countFromBuffer, srcData, &offset);
248 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get listCount failed")
249
250 if (countFromBuffer > *listCount) {
251 HKS_LOG_E("listCount from buffer is invalid");
252 return HKS_ERROR_INVALID_ARGUMENT;
253 }
254 *listCount = countFromBuffer;
255
256 struct HksBlob alias = { 0, NULL };
257 struct HksParamSet *paramSet = NULL;
258 for (uint32_t i = 0; i < countFromBuffer; ++i) {
259 ret = GetBlobFromBuffer(&alias, srcData, &offset);
260 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get alias failed")
261
262 if (memcpy_s(keyInfoList[i].alias.data, keyInfoList[i].alias.size, alias.data, alias.size) != EOK) {
263 HKS_LOG_E("memcpy alias failed");
264 return ret;
265 }
266 keyInfoList[i].alias.size = alias.size;
267
268 ret = GetParamSetFromBuffer(¶mSet, srcData, &offset);
269 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get paramSet failed")
270
271 if (memcpy_s(keyInfoList[i].paramSet, keyInfoList[i].paramSet->paramSetSize,
272 paramSet, paramSet->paramSetSize) != EOK) {
273 HKS_LOG_E("memcpy paramSet failed");
274 return ret;
275 }
276
277 ret = HksFreshParamSet(keyInfoList[i].paramSet, false);
278 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "FreshParamSet fail, ret = %" LOG_PUBLIC "d", ret)
279 }
280
281 return HKS_SUCCESS;
282 }
283
HksCertificateChainPack(struct HksBlob * destData,const struct HksBlob * keyAlias,const struct HksParamSet * paramSet,const struct HksBlob * certChainBlob)284 int32_t HksCertificateChainPack(struct HksBlob *destData, const struct HksBlob *keyAlias,
285 const struct HksParamSet *paramSet, const struct HksBlob *certChainBlob)
286 {
287 uint32_t offset = 0;
288 int32_t ret = CopyBlobToBuffer(keyAlias, destData, &offset);
289 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy keyAlias failed")
290
291 ret = CopyParamSetToBuffer(paramSet, destData, &offset);
292 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "copy paramSet failed")
293
294 return CopyUint32ToBuffer(certChainBlob->size, destData, &offset);
295 }
296
Base64Encode(const uint8_t * srcData,const uint32_t srcDataSize,uint8_t * outData,const uint32_t outDataSize)297 static int32_t Base64Encode(const uint8_t *srcData, const uint32_t srcDataSize,
298 uint8_t *outData, const uint32_t outDataSize)
299 {
300 /*
301 * outDataSize is already calculated on the outside.
302 * Base64 encode like this:
303 * <------------ byte ------------>
304 * <------><-src1-><-src2-><-src3->
305 * +------------------------------+
306 * | 24 16 08 |
307 * +------------------------------+
308 * <out1><out2><out3><out4>
309 */
310 uint32_t j = 0;
311 uint32_t i = 0;
312 while (i < srcDataSize) {
313 uint32_t a = (i < srcDataSize) ? (uint8_t)srcData[i] : 0;
314 ++i;
315 uint32_t b = (i < srcDataSize) ? (uint8_t)srcData[i] : 0;
316 ++i;
317 uint32_t c = (i < srcDataSize) ? (uint8_t)srcData[i] : 0;
318 ++i;
319 /* srcData each character takes up 8 bits. 1, 2 and 3 is offset */
320 uint32_t byte = (a << (8 * 2)) + (b << (8 * 1)) + (c << (8 * 0));
321
322 /* outData each character takes up 6 bits */
323 outData[j++] = g_base64Table[(byte >> (6 * 3)) & 0b00111111]; /* 3 and 6 is offset */
324 outData[j++] = g_base64Table[(byte >> (6 * 2)) & 0b00111111]; /* 2 and 6 is offset */
325 outData[j++] = g_base64Table[(byte >> (6 * 1)) & 0b00111111]; /* 1 and 6 is offset */
326 outData[j++] = g_base64Table[(byte >> (6 * 0)) & 0b00111111]; /* 0 and 6 is offset */
327 }
328
329 const int32_t padding = srcDataSize % 3; /* 3 in each group */
330 if (padding == 0) {
331 return HKS_SUCCESS;
332 } else {
333 outData[outDataSize - 1] = '='; /* 1: padding last character with '=' */
334 }
335 if (padding == 1) {
336 outData[outDataSize - 2] = '='; /* 2: padding penultimate character with '=' */
337 }
338
339 return HKS_SUCCESS;
340 }
341
CheckAndCalculateSize(const uint32_t inSize,const uint32_t extraSize,uint32_t * outSize)342 static int32_t CheckAndCalculateSize(const uint32_t inSize, const uint32_t extraSize, uint32_t *outSize)
343 {
344 /*
345 * 2: fill it up to a multiple of three
346 * 3 and 4: every three original characters is converted to four base64 characters
347 */
348 if (inSize > UINT32_MAX - NUM_TWO) {
349 return HKS_ERROR_INVALID_ARGUMENT;
350 }
351 /* 3 and 4: every three original characters is converted to four base64 characters */
352 if (((inSize + 2) / 3) > (UINT32_MAX / 4)) { // 2: fill it up to a multiple of three
353 return HKS_ERROR_INVALID_ARGUMENT;
354 }
355 /* 3 and 4: every three original characters is converted to four base64 characters */
356 if ((((inSize + 2) / 3) * 4) > UINT32_MAX - extraSize) { // 2: fill it up to a multiple of three
357 return HKS_ERROR_INVALID_ARGUMENT;
358 }
359 /* 3 and 4: every three original characters is converted to four base64 characters */
360 *outSize = (((inSize + 2) / 3) * 4) + extraSize; // 2: fill it up to a multiple of three
361 return HKS_SUCCESS;
362 }
363
EncodeCertChain(const struct HksBlob * inBlob,struct HksBlob * outBlob)364 static int32_t EncodeCertChain(const struct HksBlob *inBlob, struct HksBlob *outBlob)
365 {
366 const char begin[] = "-----BEGIN CERTIFICATE-----\n";
367 const char end[] = "\n-----END CERTIFICATE-----";
368
369 const uint32_t beginLen = strlen(begin);
370 const uint32_t endLen = strlen(end);
371
372 uint32_t tmpSize;
373 int32_t ret = CheckAndCalculateSize(inBlob->size, beginLen + endLen, &tmpSize);
374 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check inBlob size fail")
375
376 struct HksBlob tmpBlob = { tmpSize, NULL };
377 tmpBlob.data = (uint8_t *)HksMalloc(tmpSize);
378 HKS_IF_NULL_LOGE_RETURN(tmpBlob.data, HKS_ERROR_MALLOC_FAIL, "malloc certEncoded fail")
379
380 do {
381 if (memcpy_s(tmpBlob.data, tmpSize, begin, beginLen) != EOK) {
382 HKS_LOG_E("memcpy_s cert begin fail");
383 ret = HKS_ERROR_BUFFER_TOO_SMALL;
384 break;
385 }
386
387 if (memcpy_s(tmpBlob.data + tmpSize - endLen, endLen, end, endLen) != EOK) {
388 HKS_LOG_E("memcpy_s cert end fail");
389 ret = HKS_ERROR_BUFFER_TOO_SMALL;
390 break;
391 }
392
393 ret = Base64Encode(inBlob->data, inBlob->size, tmpBlob.data + beginLen, tmpSize - beginLen - endLen);
394 HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "Base64Encode fail")
395
396 if (memcpy_s(outBlob->data, outBlob->size, tmpBlob.data, tmpBlob.size) != EOK) {
397 HKS_LOG_E("copy certs encoded fail");
398 ret = HKS_ERROR_BUFFER_TOO_SMALL;
399 break;
400 }
401 outBlob->size = tmpBlob.size;
402 } while (0);
403
404 HksFree(tmpBlob.data);
405 return ret;
406 }
407
HksCertificateChainUnpackFromService(const struct HksBlob * srcData,bool needEncode,struct HksCertChain * certChain)408 int32_t HksCertificateChainUnpackFromService(const struct HksBlob *srcData, bool needEncode,
409 struct HksCertChain *certChain)
410 {
411 if (srcData->size < sizeof(certChain->certsCount)) {
412 HKS_LOG_E("invalid certs buffer");
413 return HKS_ERROR_BUFFER_TOO_SMALL;
414 }
415 uint32_t certsCount = *(uint32_t *)(srcData->data);
416 if (certsCount > certChain->certsCount) {
417 HKS_LOG_E("not enough output certs, real count %" LOG_PUBLIC "u, output count %" LOG_PUBLIC "u",
418 certsCount, certChain->certsCount);
419 return HKS_ERROR_INSUFFICIENT_DATA;
420 }
421 uint32_t offset = sizeof(certsCount);
422
423 struct HksBlob tmp = { 0, NULL };
424 for (uint32_t i = 0; i < certsCount; ++i) {
425 HKS_IF_NOT_SUCC_LOGE_RETURN(GetBlobFromBuffer(&tmp, srcData, &offset),
426 HKS_ERROR_IPC_MSG_FAIL, "get certs %" LOG_PUBLIC "d fail", i)
427 if (memcpy_s(certChain->certs[i].data, certChain->certs[i].size, tmp.data, tmp.size)) {
428 HKS_LOG_E("copy certs %" LOG_PUBLIC "d fail", i);
429 return HKS_ERROR_INSUFFICIENT_DATA;
430 }
431
432 if (needEncode) {
433 struct HksBlob inBlob = { tmp.size, certChain->certs[i].data };
434 int32_t ret = EncodeCertChain(&inBlob, &certChain->certs[i]);
435 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "EncodeCertChain fail, ret = %" LOG_PUBLIC "d", ret)
436 } else {
437 certChain->certs[i].size = tmp.size;
438 }
439 }
440 certChain->certsCount = certsCount;
441 return HKS_SUCCESS;
442 }
443
HksParamsToParamSet(struct HksParam * params,uint32_t cnt,struct HksParamSet ** outParamSet)444 int32_t HksParamsToParamSet(struct HksParam *params, uint32_t cnt, struct HksParamSet **outParamSet)
445 {
446 struct HksParamSet *newParamSet = NULL;
447
448 int32_t ret = HksInitParamSet(&newParamSet);
449 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "init param set failed")
450
451 do {
452 uint8_t tmpData = 0;
453 struct HksBlob tmpBlob = { sizeof(tmpData), &tmpData };
454
455 for (uint32_t i = 0; i < cnt; ++i) {
456 if ((GetTagType(params[i].tag) == HKS_TAG_TYPE_BYTES) &&
457 (params[i].blob.size == 0 || params[i].blob.data == NULL)) {
458 params[i].tag += HKS_PARAM_BUFFER_NULL_INTERVAL;
459 params[i].blob = tmpBlob;
460 }
461 }
462 ret = HksAddParams(newParamSet, params, cnt);
463 HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "add in params failed")
464
465 ret = HksBuildParamSet(&newParamSet);
466 HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "build paramset failed!")
467 } while (0);
468 if (ret != HKS_SUCCESS) {
469 HksFreeParamSet(&newParamSet);
470 return ret;
471 }
472
473 *outParamSet = newParamSet;
474
475 return ret;
476 }
477