• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of the openHiTLS project.
3  *
4  * openHiTLS is licensed under the Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *
8  *     http://license.coscl.org.cn/MulanPSL2
9  *
10  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13  * See the Mulan PSL v2 for more details.
14  */
15 
16 #include "hitls_build.h"
17 #ifdef HITLS_PKI_PKCS12
18 #include "bsl_sal.h"
19 #ifdef HITLS_BSL_SAL_FILE
20 #include "sal_file.h"
21 #endif
22 #include "securec.h"
23 #include "hitls_pkcs12_local.h"
24 #include "bsl_sal.h"
25 #include "bsl_err_internal.h"
26 #include "hitls_pki_errno.h"
27 #include "crypt_eal_mac.h"
28 #include "crypt_eal_md.h"
29 #include "hitls_cert_local.h"
30 #include "crypt_eal_rand.h"
31 #include "crypt_errno.h"
32 #include "hitls_pki_pkcs12.h"
33 #include "hitls_pki_cert.h"
34 
HITLS_PKCS12_AttributesFree(void * attribute)35 void HITLS_PKCS12_AttributesFree(void *attribute)
36 {
37     if (attribute == NULL) {
38         return;
39     }
40     HITLS_PKCS12_SafeBagAttr *attr = attribute;
41     BSL_SAL_FREE(attr->attrValue.data);
42     BSL_SAL_FREE(attr);
43 }
44 
HITLS_PKCS12_SafeBagFree(HITLS_PKCS12_SafeBag * safeBag)45 void HITLS_PKCS12_SafeBagFree(HITLS_PKCS12_SafeBag *safeBag)
46 {
47     if (safeBag == NULL) {
48         return;
49     }
50     HITLS_X509_AttrsFree(safeBag->attributes, HITLS_PKCS12_AttributesFree);
51     safeBag->attributes = NULL;
52     BSL_SAL_CleanseData(safeBag->bag->data, safeBag->bag->dataLen);
53     BSL_SAL_FREE(safeBag->bag->data);
54     BSL_SAL_FREE(safeBag->bag);
55     BSL_SAL_Free(safeBag);
56     return;
57 }
58 
HITLS_PKCS12_MacDataNew(void)59 HITLS_PKCS12_MacData *HITLS_PKCS12_MacDataNew(void)
60 {
61     HITLS_PKCS12_MacData *macData = BSL_SAL_Calloc(1u, sizeof(HITLS_PKCS12_MacData));
62     if (macData == NULL) {
63         BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL);
64         return NULL;
65     }
66 
67     BSL_Buffer *macSalt = BSL_SAL_Calloc(1u, sizeof(BSL_Buffer));
68     if (macSalt == NULL) {
69         BSL_SAL_Free(macData);
70         BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL);
71         return NULL;
72     }
73 
74     BSL_Buffer *mac = BSL_SAL_Calloc(1u, sizeof(BSL_Buffer));
75     if (mac == NULL) {
76         BSL_SAL_Free(macSalt);
77         BSL_SAL_Free(macData);
78         BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL);
79         return NULL;
80     }
81     macData->mac = mac;
82     macData->macSalt = macSalt;
83     return macData;
84 }
85 
HITLS_PKCS12_MacDataFree(HITLS_PKCS12_MacData * macData)86 void HITLS_PKCS12_MacDataFree(HITLS_PKCS12_MacData *macData)
87 {
88     if (macData == NULL) {
89         return;
90     }
91 
92     if (macData->mac != NULL) {
93         BSL_SAL_FREE(macData->mac->data);
94         BSL_SAL_Free(macData->mac);
95     }
96 
97     if (macData->macSalt != NULL) {
98         BSL_SAL_CleanseData(macData->macSalt->data, macData->macSalt->dataLen);
99         BSL_SAL_FREE(macData->macSalt->data);
100         BSL_SAL_Free(macData->macSalt);
101     }
102     BSL_SAL_Free(macData);
103 }
104 
HITLS_PKCS12_New(void)105 HITLS_PKCS12 *HITLS_PKCS12_New(void)
106 {
107     HITLS_PKCS12 *p12 = BSL_SAL_Calloc(1u, sizeof(HITLS_PKCS12));
108     if (p12 == NULL) {
109         return NULL;
110     }
111     HITLS_PKCS12_MacData *macData = HITLS_PKCS12_MacDataNew();
112     if (macData == NULL) {
113         BSL_SAL_Free(p12);
114         return NULL;
115     }
116     BSL_ASN1_List *certList = BSL_LIST_New(sizeof(HITLS_PKCS12_Bag));
117     if (certList == NULL) {
118         BSL_SAL_Free(p12);
119         HITLS_PKCS12_MacDataFree(macData);
120         return NULL;
121     }
122     p12->version = 3; // RFC7292 required the version = 3;
123     p12->certList = certList;
124     p12->macData = macData;
125     return p12;
126 }
127 
HITLS_PKCS12_ProviderNew(HITLS_PKI_LibCtx * libCtx,const char * attrName)128 HITLS_PKCS12 *HITLS_PKCS12_ProviderNew(HITLS_PKI_LibCtx *libCtx, const char *attrName)
129 {
130     HITLS_PKCS12 *p12 = HITLS_PKCS12_New();
131     if (p12 == NULL) {
132         return NULL;
133     }
134     p12->libCtx = libCtx;
135     p12->attrName = attrName;
136     return p12;
137 }
138 
CertBagFree(void * value)139 static void CertBagFree(void *value)
140 {
141     if (value == NULL) {
142         return;
143     }
144     HITLS_PKCS12_Bag *bag = (HITLS_PKCS12_Bag *)value;
145     HITLS_X509_CertFree(bag->value.cert);
146     HITLS_X509_AttrsFree(bag->attributes, HITLS_PKCS12_AttributesFree);
147     bag->attributes = NULL;
148     BSL_SAL_FREE(bag);
149 }
150 
HITLS_PKCS12_Free(HITLS_PKCS12 * p12)151 void HITLS_PKCS12_Free(HITLS_PKCS12 *p12)
152 {
153     if (p12 == NULL) {
154         return;
155     }
156     if (p12->entityCert != NULL) {
157         HITLS_X509_CertFree(p12->entityCert->value.cert);
158         HITLS_X509_AttrsFree(p12->entityCert->attributes, HITLS_PKCS12_AttributesFree);
159         p12->entityCert->attributes = NULL;
160         BSL_SAL_FREE(p12->entityCert);
161     }
162     if (p12->key != NULL) {
163         CRYPT_EAL_PkeyFreeCtx(p12->key->value.key);
164         p12->key->value.key = NULL;
165         HITLS_X509_AttrsFree(p12->key->attributes, HITLS_PKCS12_AttributesFree);
166         p12->key->attributes = NULL;
167         BSL_SAL_FREE(p12->key);
168     }
169     BSL_LIST_FREE(p12->certList, CertBagFree);
170     HITLS_PKCS12_MacDataFree(p12->macData);
171     BSL_SAL_Free(p12);
172 }
173 
BagSetValue(HITLS_PKCS12_Bag * bag,void * value,uint32_t bagType)174 static int32_t BagSetValue(HITLS_PKCS12_Bag *bag, void *value, uint32_t bagType)
175 {
176     int32_t ret;
177     int32_t ref;
178     switch (bagType) {
179         case BSL_CID_PKCS8SHROUDEDKEYBAG:
180             ret = CRYPT_EAL_PkeyUpRef((CRYPT_EAL_PkeyCtx *)value);
181             if (ret != CRYPT_SUCCESS) {
182                 BSL_ERR_PUSH_ERROR(ret);
183                 return ret;
184             }
185             bag->value.key = (CRYPT_EAL_PkeyCtx *)value;
186             return HITLS_PKI_SUCCESS;
187         case BSL_CID_CERTBAG:
188             ret = HITLS_X509_CertCtrl((HITLS_X509_Cert *)value, HITLS_X509_REF_UP, &ref, sizeof(int32_t));
189             if (ret != HITLS_PKI_SUCCESS) {
190                 BSL_ERR_PUSH_ERROR(ret);
191                 return ret;
192             }
193             bag->value.cert = (HITLS_X509_Cert *)value;
194             return HITLS_PKI_SUCCESS;
195         default:
196             BSL_ERR_PUSH_ERROR(HITLS_PKCS12_ERR_INVALID_PARAM);
197             return HITLS_PKCS12_ERR_INVALID_PARAM;
198     }
199 }
200 
HITLS_PKCS12_BagNew(uint32_t bagId,uint32_t bagType,void * bagValue)201 HITLS_PKCS12_Bag *HITLS_PKCS12_BagNew(uint32_t bagId, uint32_t bagType, void *bagValue)
202 {
203     (void)bagType;
204     if (bagValue == NULL) {
205         BSL_ERR_PUSH_ERROR(HITLS_PKCS12_ERR_NULL_POINTER);
206         return NULL;
207     }
208     HITLS_PKCS12_Bag *bag = BSL_SAL_Calloc(1u, sizeof(HITLS_PKCS12_Bag));
209     if (bag == NULL) {
210         BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL);
211         return NULL;
212     }
213     if (BagSetValue(bag, bagValue, bagId) != HITLS_PKI_SUCCESS) {
214         BSL_SAL_Free(bag);
215         return NULL;
216     }
217     bag->type = bagId;
218     return bag;
219 }
220 
HITLS_PKCS12_BagFree(HITLS_PKCS12_Bag * bag)221 void HITLS_PKCS12_BagFree(HITLS_PKCS12_Bag *bag)
222 {
223     if (bag == NULL) {
224         return;
225     }
226     switch (bag->type) {
227         case BSL_CID_PKCS8SHROUDEDKEYBAG:
228             CRYPT_EAL_PkeyFreeCtx(bag->value.key);
229             break;
230         case BSL_CID_CERTBAG:
231             HITLS_X509_CertFree(bag->value.cert);
232             break;
233         default:
234             break;
235     }
236     HITLS_X509_AttrsFree(bag->attributes, HITLS_PKCS12_AttributesFree);
237     bag->attributes = NULL;
238     BSL_SAL_Free(bag);
239     return;
240 }
241 
242 typedef struct {
243     CRYPT_MD_AlgId alg;
244     uint32_t u;
245     uint32_t v;
246 } Pkcs12KdfParam;
247 
248 /*
249  * The data comes from RFC7292.
250  * https://datatracker.ietf.org/doc/html/rfc7292#appendix-B.2
251  */
252 const Pkcs12KdfParam PKCS12KDF_PARAM[] = {
253     {.alg = CRYPT_MD_SHA224, .u = 28, .v = 64},
254     {.alg = CRYPT_MD_SHA256, .u = 32, .v = 64},
255     {.alg = CRYPT_MD_SHA384, .u = 48, .v = 128},
256     {.alg = CRYPT_MD_SHA512, .u = 64, .v = 128},
257 };
258 
FindKdfParam(CRYPT_MD_AlgId id)259 const Pkcs12KdfParam *FindKdfParam(CRYPT_MD_AlgId id)
260 {
261     const Pkcs12KdfParam *param = NULL;
262     uint32_t num = sizeof(PKCS12KDF_PARAM) / sizeof(PKCS12KDF_PARAM[0]);
263     for (uint32_t i = 0; i < num; i++) {
264         if (PKCS12KDF_PARAM[i].alg == id) {
265             param = &PKCS12KDF_PARAM[i];
266             return param;
267         }
268     }
269     return NULL;
270 }
271 
InitKdfParam(const Pkcs12KdfParam * param,uint8_t ** D,uint8_t ** A,uint8_t ** B)272 static int32_t InitKdfParam(const Pkcs12KdfParam *param, uint8_t **D, uint8_t **A, uint8_t **B)
273 {
274     *D = BSL_SAL_Malloc(param->v);
275     *A = BSL_SAL_Malloc(param->u);
276     *B = BSL_SAL_Malloc(param->v);
277     if (*D == NULL || *A == NULL || *B == NULL) {
278         BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL);
279         BSL_SAL_FREE(*D);
280         BSL_SAL_FREE(*B);
281         BSL_SAL_FREE(*A);
282         return BSL_MALLOC_FAIL;
283     }
284     return HITLS_PKI_SUCCESS;
285 }
286 
MacLoop(uint32_t LoopTimes,CRYPT_EAL_MdCTX * ctx,const Pkcs12KdfParam * param,uint8_t * D,uint8_t * I,uint8_t * A,uint32_t k,uint32_t dataLen)287 static int32_t MacLoop(uint32_t LoopTimes, CRYPT_EAL_MdCTX *ctx, const Pkcs12KdfParam *param, uint8_t *D,
288     uint8_t *I, uint8_t *A, uint32_t k, uint32_t dataLen)
289 {
290     int32_t ret;
291     uint32_t tempLen = param->u;
292     /* A = H(H(H(... H(D || I)))) */
293     ret = CRYPT_EAL_MdInit(ctx);
294     if (ret != CRYPT_SUCCESS) {
295         BSL_ERR_PUSH_ERROR(ret);
296         return ret;
297     }
298     ret = CRYPT_EAL_MdUpdate(ctx, D, param->v);
299     if (ret != CRYPT_SUCCESS) {
300         BSL_ERR_PUSH_ERROR(ret);
301         return ret;
302     }
303     ret = CRYPT_EAL_MdUpdate(ctx, I, k);
304     if (ret != CRYPT_SUCCESS) {
305         BSL_ERR_PUSH_ERROR(ret);
306         return ret;
307     }
308     ret = CRYPT_EAL_MdFinal(ctx, A, &tempLen);
309     if (ret != CRYPT_SUCCESS) {
310         BSL_ERR_PUSH_ERROR(ret);
311         return ret;
312     }
313     CRYPT_EAL_MdDeinit(ctx);
314 
315     if (LoopTimes != 0) {
316         for (uint32_t j = 0; j < LoopTimes - 1; j++) {
317             ret = CRYPT_EAL_MdInit(ctx);
318             if (ret != CRYPT_SUCCESS) {
319                 BSL_ERR_PUSH_ERROR(ret);
320                 return ret;
321             }
322             ret = CRYPT_EAL_MdUpdate(ctx, A, dataLen);
323             if (ret != CRYPT_SUCCESS) {
324                 BSL_ERR_PUSH_ERROR(ret);
325                 return ret;
326             }
327             ret = CRYPT_EAL_MdFinal(ctx, A, &dataLen);
328             if (ret != CRYPT_SUCCESS) {
329                 BSL_ERR_PUSH_ERROR(ret);
330                 return ret;
331             }
332             CRYPT_EAL_MdDeinit(ctx);
333         }
334     }
335     return ret;
336 }
337 
KdfUpdate(uint8_t * I,uint8_t * A,uint8_t * B,uint32_t k,const Pkcs12KdfParam * param)338 static void KdfUpdate(uint8_t *I, uint8_t *A, uint8_t *B, uint32_t k, const Pkcs12KdfParam *param)
339 {
340     /* Concatenate copies of Ai to create a string B */
341     for (uint32_t l = 0; l < param->v; l++) {
342         B[l] = A[l % param->u];
343     }
344     /* I_j = (I_j + B + 1) mod 2^v */
345     for (uint32_t m = 0; m < k; m++) { /* K = ceiling(s/v) + ceiling(p/v) */
346         uint8_t *tempI = I + m * param->v;
347         uint8_t carry = 1;
348         for (int32_t r = (int32_t)param->v - 1; r >= 0; r--) {
349             uint8_t temp = tempI[r] + carry;
350             carry = temp < tempI[r] ? 1 : 0;
351             temp += B[r];
352             carry += temp < B[r] ? 1 : 0;
353             tempI[r] = temp;
354         }
355     }
356 }
357 
HITLS_PKCS12_KDF(BSL_Buffer * output,const uint8_t * pwd,uint32_t pwdLen,HITLS_PKCS12_KDF_IDX type,HITLS_PKCS12_MacData * macData)358 int32_t HITLS_PKCS12_KDF(BSL_Buffer *output, const uint8_t *pwd, uint32_t pwdLen, HITLS_PKCS12_KDF_IDX type,
359     HITLS_PKCS12_MacData *macData)
360 {
361     if (output == NULL || output->data == NULL || macData == NULL) {
362         BSL_ERR_PUSH_ERROR(HITLS_PKCS12_ERR_NULL_POINTER);
363         return HITLS_PKCS12_ERR_NULL_POINTER;
364     }
365 
366     if (pwd == NULL && pwdLen != 0) {
367         BSL_ERR_PUSH_ERROR(HITLS_PKCS12_ERR_INVALID_PARAM);
368         return HITLS_PKCS12_ERR_INVALID_PARAM;
369     }
370 
371     uint32_t n = output->dataLen;
372     uint32_t iter = macData->iteration;
373     uint8_t *key = output->data;
374 
375     const Pkcs12KdfParam *param = FindKdfParam((CRYPT_MD_AlgId)macData->alg);
376     if (param == NULL) {
377         BSL_ERR_PUSH_ERROR(HITLS_PKCS12_ERR_INVALID_ALGO);
378         return HITLS_PKCS12_ERR_INVALID_ALGO;
379     }
380     CRYPT_EAL_MdCTX *ctx = CRYPT_EAL_MdNewCtx((CRYPT_MD_AlgId)macData->alg);
381     if (ctx == NULL) {
382         BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL);
383         return BSL_MALLOC_FAIL;
384     }
385 
386     uint8_t *D = NULL;
387     uint8_t *A = NULL;
388     uint8_t *B = NULL;
389     uint8_t *I = NULL;
390     int32_t ret = InitKdfParam(param, &D, &A, &B);
391     if (ret != HITLS_PKI_SUCCESS) {
392         CRYPT_EAL_MdFreeCtx(ctx);
393         return ret;
394     }
395     (void)memset_s(D, param->v, type, param->v);
396     uint32_t SLen = param->v * ((macData->macSalt->dataLen + param->v - 1) / param->v);
397     uint32_t PLen = param->v * ((pwdLen + param->v - 1) / param->v);
398     uint32_t k = 0;
399     if (SLen + PLen < SLen) {
400         BSL_ERR_PUSH_ERROR(HITLS_PKCS12_ERR_KDF_TOO_LONG_INPUT);
401         ret = HITLS_PKCS12_ERR_KDF_TOO_LONG_INPUT;
402         goto EXIT;
403     }
404 
405     k = SLen + PLen;
406     I = BSL_SAL_Calloc(1u, k);
407     if (I == NULL) {
408         BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL);
409         ret = BSL_MALLOC_FAIL;
410         goto EXIT;
411     }
412 
413     /* I = S||P */
414     if (macData->macSalt->data != NULL) {
415         for (uint32_t i = 0; i < SLen; i++) {
416             I[i] = macData->macSalt->data[i % macData->macSalt->dataLen];
417         }
418     }
419 
420     if (pwd != NULL) {
421         for (uint32_t i = 0; i < PLen; i++) {
422             I[i + SLen] = pwd[i % pwdLen];
423         }
424     }
425 
426     /* C = ceiling(n/u) */
427     uint32_t c = (n + param->u - 1) / param->u;
428     for (uint32_t i = 0; i < c; i++) {
429         ret = MacLoop(iter, ctx, param, D, I, A, k, param->u);
430         if (ret != HITLS_PKI_SUCCESS) {
431             goto EXIT; // has pushed err code.
432         }
433 
434         uint32_t copyLen = n > param->u ? param->u : n;
435         if (memcpy_s(key, n, A, copyLen) != EOK) {
436             ret = BSL_MEMCPY_FAIL;
437             BSL_ERR_PUSH_ERROR(BSL_MEMCPY_FAIL);
438             goto EXIT;
439         }
440 
441         n -= copyLen;
442         if (n == 0) {
443             goto EXIT;
444         }
445         key = key + copyLen;
446         KdfUpdate(I, A, B, k / param->v, param);
447     }
448 
449 EXIT:
450     CRYPT_EAL_MdFreeCtx(ctx);
451     BSL_SAL_Free(D);
452     BSL_SAL_CleanseData(I, k);
453     BSL_SAL_Free(I);
454     BSL_SAL_Free(B);
455     BSL_SAL_Free(A);
456     return ret;
457 }
458 
GetMacId(BslCid id)459 static uint32_t GetMacId(BslCid id)
460 {
461     switch ((CRYPT_MD_AlgId)id) {
462         case CRYPT_MD_SHA224:
463             return CRYPT_MAC_HMAC_SHA224;
464         case CRYPT_MD_SHA256:
465             return CRYPT_MAC_HMAC_SHA256;
466         case CRYPT_MD_SHA384:
467             return CRYPT_MAC_HMAC_SHA384;
468         case CRYPT_MD_SHA512:
469             return CRYPT_MAC_HMAC_SHA512;
470         default:
471             BSL_ERR_PUSH_ERROR(HITLS_PKCS12_ERR_INVALID_ALGO);
472             return BSL_CID_UNKNOWN;
473     }
474 }
475 
Utf8ToUtf16(const uint8_t * src,uint8_t * target,uint32_t len)476 static int32_t Utf8ToUtf16(const uint8_t *src, uint8_t *target, uint32_t len)
477 {
478     for (uint32_t i = 0; i < len; i++) {
479         if (src[i] > 127) { // the ascii <= 127.
480             BSL_ERR_PUSH_ERROR(HITLS_PKCS12_ERR_INVALID_PASSWORD);
481             return HITLS_PKCS12_ERR_INVALID_PASSWORD;
482         }
483         target[2 * i + 1] = src[i]; // we need 2 space, [0,0] -> after encode = [0, data];
484     }
485     return HITLS_PKI_SUCCESS;
486 }
487 
TransCodePwd(BSL_Buffer * pwd,uint8_t ** transcoded,uint32_t * transcodedLen)488 static int32_t TransCodePwd(BSL_Buffer *pwd, uint8_t **transcoded, uint32_t *transcodedLen)
489 {
490     if (pwd == NULL || pwd->data == NULL) {
491         *transcodedLen = 0;
492         return HITLS_PKI_SUCCESS;
493     }
494 
495     uint32_t outputLen = 2 * pwd->dataLen + 2; // encodeLen = 2 * len, and two zeros at the end.
496     uint8_t *output = BSL_SAL_Calloc(1u, outputLen);
497     if (output == NULL) {
498         BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL);
499         return BSL_MALLOC_FAIL;
500     }
501     int32_t ret = Utf8ToUtf16(pwd->data, output, pwd->dataLen);
502     if (ret != HITLS_PKI_SUCCESS) {
503         BSL_SAL_CleanseData(output, outputLen);
504         BSL_SAL_FREE(output);
505         return ret; // has pushed err code.
506     }
507     *transcodedLen = outputLen;
508     *transcoded = output;
509     return HITLS_PKI_SUCCESS;
510 }
511 
GetHmacKey(BSL_Buffer * pwd,uint32_t macSize,HITLS_PKCS12_MacData * macData,uint8_t ** keyData)512 static int32_t GetHmacKey(BSL_Buffer *pwd, uint32_t macSize, HITLS_PKCS12_MacData *macData, uint8_t **keyData)
513 {
514     uint32_t temPwdLen = 0;
515     uint8_t *temPwd = NULL;
516     int32_t ret = TransCodePwd(pwd, &temPwd, &temPwdLen);
517     if (ret != HITLS_PKI_SUCCESS) {
518         return ret; // has pushed err code.
519     }
520 
521     uint8_t *key = BSL_SAL_Malloc(macSize);
522     if (key == NULL) {
523         BSL_SAL_CleanseData(temPwd, temPwdLen);
524         BSL_SAL_FREE(temPwd);
525         BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL);
526         return BSL_MALLOC_FAIL;
527     }
528     BSL_Buffer keyBuffer = {key, macSize};
529     ret = HITLS_PKCS12_KDF(&keyBuffer, temPwd, temPwdLen, HITLS_PKCS12_KDF_MACKEY_ID, macData);
530     BSL_SAL_CleanseData(temPwd, temPwdLen);
531     BSL_SAL_FREE(temPwd);
532     if (ret != HITLS_PKI_SUCCESS) {
533         BSL_SAL_CleanseData(key, macSize);
534         BSL_SAL_FREE(key);
535         return ret;
536     }
537     *keyData = key;
538     return ret;
539 }
540 
ParamCheckAndInit(HITLS_PKCS12_MacData * macData,BSL_Buffer * pwd,uint32_t * macId,uint32_t * macSize)541 static int32_t ParamCheckAndInit(HITLS_PKCS12_MacData *macData, BSL_Buffer *pwd, uint32_t *macId, uint32_t *macSize)
542 {
543     if (macData == NULL || macData->macSalt == NULL) {
544         BSL_ERR_PUSH_ERROR(HITLS_PKCS12_ERR_NULL_POINTER);
545         return HITLS_PKCS12_ERR_NULL_POINTER;
546     }
547 
548     if (pwd != NULL && pwd->data == NULL && pwd->dataLen != 0) {
549         BSL_ERR_PUSH_ERROR(HITLS_PKCS12_ERR_INVALID_PARAM);
550         return HITLS_PKCS12_ERR_INVALID_PARAM;
551     }
552     if (macData->iteration < 1000) { // The nist sp800-132 required the minimum iteration count = 1000.
553         BSL_ERR_PUSH_ERROR(HITLS_PKCS12_ERR_INVALID_ITERATION);
554         return HITLS_PKCS12_ERR_INVALID_ITERATION;
555     }
556     *macId = GetMacId(macData->alg);
557     *macSize = CRYPT_EAL_MdGetDigestSize((CRYPT_MD_AlgId)macData->alg);
558     if (macId == BSL_CID_UNKNOWN || macSize == 0) {
559         BSL_ERR_PUSH_ERROR(HITLS_PKCS12_ERR_INVALID_ALGO);
560         return HITLS_PKCS12_ERR_INVALID_ALGO;
561     }
562     if (macData->macSalt->data == NULL) {
563         if (macData->macSalt->dataLen == 0) {
564             BSL_ERR_PUSH_ERROR(HITLS_PKCS12_ERR_INVALID_SALTLEN);
565             return HITLS_PKCS12_ERR_INVALID_SALTLEN;
566         }
567         uint8_t *salt = BSL_SAL_Malloc(macData->macSalt->dataLen);
568         if (salt == NULL) {
569             BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL);
570             return BSL_MALLOC_FAIL;
571         }
572         int32_t ret = CRYPT_EAL_RandbytesEx(NULL, salt, macData->macSalt->dataLen);
573         if (ret != CRYPT_SUCCESS) {
574             BSL_SAL_Free(salt);
575             BSL_ERR_PUSH_ERROR(ret);
576             return ret;
577         }
578         macData->macSalt->data = salt;
579     }
580     return HITLS_PKI_SUCCESS;
581 }
582 
HITLS_PKCS12_CalMac(BSL_Buffer * output,BSL_Buffer * pwd,BSL_Buffer * initData,HITLS_PKCS12_MacData * macData)583 int32_t HITLS_PKCS12_CalMac(BSL_Buffer *output, BSL_Buffer *pwd, BSL_Buffer *initData, HITLS_PKCS12_MacData *macData)
584 {
585     uint32_t macId;
586     uint32_t macSize;
587     int32_t ret = ParamCheckAndInit(macData, pwd, &macId, &macSize);
588     if (ret != HITLS_PKI_SUCCESS) {
589         return ret;
590     }
591     uint8_t *keyData = NULL;
592     ret = GetHmacKey(pwd, macSize, macData, &keyData);
593     if (ret != HITLS_PKI_SUCCESS) {
594         return ret; // has pushed err code.
595     }
596 
597     CRYPT_EAL_MacCtx *ctx = CRYPT_EAL_MacNewCtx(macId);
598     if (ctx == NULL) {
599         BSL_SAL_CleanseData(keyData, macSize);
600         BSL_SAL_FREE(keyData);
601         BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL);
602         return BSL_MALLOC_FAIL;
603     }
604     ret = CRYPT_EAL_MacInit(ctx, keyData, macSize);
605     BSL_SAL_CleanseData(keyData, macSize);
606     BSL_SAL_FREE(keyData);
607     if (ret != HITLS_PKI_SUCCESS) {
608         CRYPT_EAL_MacFreeCtx(ctx);
609         BSL_ERR_PUSH_ERROR(ret);
610         return ret;
611     }
612     ret = CRYPT_EAL_MacUpdate(ctx, initData->data, initData->dataLen);
613     if (ret != HITLS_PKI_SUCCESS) {
614         CRYPT_EAL_MacFreeCtx(ctx);
615         BSL_ERR_PUSH_ERROR(ret);
616         return ret;
617     }
618     uint8_t *temp = BSL_SAL_Malloc(macSize);
619     if (temp == NULL) {
620         CRYPT_EAL_MacFreeCtx(ctx);
621         BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL);
622         return BSL_MALLOC_FAIL;
623     }
624     ret = CRYPT_EAL_MacFinal(ctx, temp, &macSize);
625     CRYPT_EAL_MacFreeCtx(ctx);
626     if (ret != HITLS_PKI_SUCCESS) {
627         BSL_SAL_FREE(temp);
628         BSL_ERR_PUSH_ERROR(ret);
629         return ret;
630     }
631     output->data = temp;
632     output->dataLen = macSize;
633     return ret;
634 }
635 
HITLS_PKCS12_BagCtrl(HITLS_PKCS12_Bag * bag,int32_t cmd,void * val,uint32_t valType)636 int32_t HITLS_PKCS12_BagCtrl(HITLS_PKCS12_Bag *bag, int32_t cmd, void *val, uint32_t valType)
637 {
638     if (bag == NULL) {
639         BSL_ERR_PUSH_ERROR(HITLS_PKCS12_ERR_NULL_POINTER);
640         return HITLS_PKCS12_ERR_NULL_POINTER;
641     }
642     switch (cmd) {
643         case HITLS_PKCS12_BAG_ADD_ATTR:
644             return HITLS_PKCS12_BagAddAttr(bag, valType, val);
645         default:
646             BSL_ERR_PUSH_ERROR(HITLS_PKCS12_ERR_INVALID_PARAM);
647             return HITLS_PKCS12_ERR_INVALID_PARAM;
648     }
649 }
650 #endif // HITLS_PKI_PKCS12
651