已合并
fix memory leaks of codeskey #826
jchx创建于 2025年11月19日
fix memory leaks of codeskey #826
已合并
共 15 个文件变更+135-55
| @@ -358,24 +358,6 @@ static int32_t ParseBMPString(const uint8_t *bmp, uint32_t bmpLen, BSL_ASN1_Buff | |||
| 358 | return BSL_SUCCESS; | 358 | return BSL_SUCCESS; |
| 359 | } | 359 | } |
| 360 | 360 | ||
| 361 | -static int32_t ParseT61String(const uint8_t *t61, uint32_t t61Len, BSL_ASN1_Buffer *decode) | ||
| 362 | -{ | ||
| 363 | - if (t61 == NULL || t61Len == 0 || decode == NULL) { | ||
| 364 | - return BSL_NULL_INPUT; | ||
| 365 | - } | ||
| 366 | - // Currently only supports single-byte character decoding | ||
| 367 | - // The lower 128 characters (0x00 - 0x7F) of T.61 are fully compatible with ASCII. | ||
| 368 | - // This means any pure ASCII text is valid in T.61 encoding and retains the same meaning. | ||
| 369 | - uint8_t *tmp = (uint8_t *)BSL_SAL_Malloc(t61Len); | ||
| 370 | - if (tmp == NULL) { | ||
| 371 | - return BSL_MALLOC_FAIL; | ||
| 372 | - } | ||
| 373 | - (void)memcpy_s(tmp, t61Len, t61, t61Len); | ||
| 374 | - decode->buff = tmp; | ||
| 375 | - decode->len = t61Len; | ||
| 376 | - return BSL_SUCCESS; | ||
| 377 | -} | ||
| 378 | - | ||
| 379 | static void EncodeT61String(const uint8_t *in, uint32_t inLen, uint8_t *encode, uint32_t *offset) | 361 | static void EncodeT61String(const uint8_t *in, uint32_t inLen, uint8_t *encode, uint32_t *offset) |
| 380 | { | 362 | { |
| 381 | (void)memcpy_s(encode + *offset, inLen, in, inLen); | 363 | (void)memcpy_s(encode + *offset, inLen, in, inLen); |
| @@ -418,8 +400,6 @@ int32_t BSL_ASN1_DecodePrimitiveItem(BSL_ASN1_Buffer *asn, void *decodeData) | |||
| 418 | return ParseTime(asn->tag, asn->buff, asn->len, decodeData); | 400 | return ParseTime(asn->tag, asn->buff, asn->len, decodeData); |
| 419 | case BSL_ASN1_TAG_BMPSTRING: | 401 | case BSL_ASN1_TAG_BMPSTRING: |
| 420 | return ParseBMPString(asn->buff, asn->len, decodeData); | 402 | return ParseBMPString(asn->buff, asn->len, decodeData); |
| 421 | - case BSL_ASN1_TAG_T61STRING: | ||
| 422 | - return ParseT61String(asn->buff, asn->len, decodeData); | ||
| 423 | default: | 403 | default: |
| 424 | break; | 404 | break; |
| 425 | } | 405 | } |
| @@ -207,7 +207,7 @@ const BSL_Param *BSL_PARAM_FindConstParam(const BSL_Param *param, int32_t key) | |||
| 207 | return NULL; | 207 | return NULL; |
| 208 | } | 208 | } |
| 209 | int32_t index = 0; | 209 | int32_t index = 0; |
| 210 | - while (param[index].key != 0 && index < BSL_PARAM_MAX_NUMBER) { | 210 | + while (index < BSL_PARAM_MAX_NUMBER && param[index].key != 0) { |
| 211 | if (param[index].key == key) { | 211 | if (param[index].key == key) { |
| 212 | return ¶m[index]; | 212 | return ¶m[index]; |
| 213 | } | 213 | } |
| @@ -228,7 +228,7 @@ BSL_Param *BSL_PARAM_FindParam(BSL_Param *param, int32_t key) | |||
| 228 | return NULL; | 228 | return NULL; |
| 229 | } | 229 | } |
| 230 | int32_t index = 0; | 230 | int32_t index = 0; |
| 231 | - while (param[index].key != 0 && index < BSL_PARAM_MAX_NUMBER) { | 231 | + while (index < BSL_PARAM_MAX_NUMBER && param[index].key != 0) { |
| 232 | if (param[index].key == key) { | 232 | if (param[index].key == key) { |
| 233 | return ¶m[index]; | 233 | return ¶m[index]; |
| 234 | } | 234 | } |
| @@ -257,6 +257,7 @@ static int32_t TryDecodeWithDecoder(CRYPT_DECODER_PoolCtx *poolCtx, CRYPT_DECODE | |||
| 257 | }; | 257 | }; |
| 258 | ret = CRYPT_DECODE_GetParam(currNode->decoderCtx, outParam); | 258 | ret = CRYPT_DECODE_GetParam(currNode->decoderCtx, outParam); |
| 259 | if (ret != CRYPT_SUCCESS) { | 259 | if (ret != CRYPT_SUCCESS) { |
| 260 | + CRYPT_DECODE_FreeOutData(currNode->decoderCtx, decoderParam); | ||
| 260 | BSL_ERR_PUSH_ERROR(ret); | 261 | BSL_ERR_PUSH_ERROR(ret); |
| 261 | return ret; | 262 | return ret; |
| 262 | } | 263 | } |
| @@ -267,6 +268,8 @@ static int32_t TryDecodeWithDecoder(CRYPT_DECODER_PoolCtx *poolCtx, CRYPT_DECODE | |||
| 267 | currNode->decoderCtx->decoderState = CRYPT_DECODER_STATE_SUCCESS; | 268 | currNode->decoderCtx->decoderState = CRYPT_DECODER_STATE_SUCCESS; |
| 268 | ret = UpdateDecoderPath(poolCtx, currNode); | 269 | ret = UpdateDecoderPath(poolCtx, currNode); |
| 269 | if (ret != CRYPT_SUCCESS) { | 270 | if (ret != CRYPT_SUCCESS) { |
| 271 | + CRYPT_DECODE_FreeOutData(currNode->decoderCtx, decoderParam); | ||
| 272 | + currNode->outData.data = NULL; | ||
| 270 | BSL_ERR_PUSH_ERROR(ret); | 273 | BSL_ERR_PUSH_ERROR(ret); |
| 271 | return ret; | 274 | return ret; |
| 272 | } | 275 | } |
| @@ -68,7 +68,7 @@ static inline void LeftShiftOneBit(const uint8_t *in, uint32_t len, uint8_t *out | |||
| 68 | } while (i != 0); | 68 | } while (i != 0); |
| 69 | } | 69 | } |
| 70 | 70 | ||
| 71 | -static void CMAC_Final(CRYPT_CMAC_Ctx *ctx) | 71 | +static int32_t CMAC_Final(CRYPT_CMAC_Ctx *ctx) |
| 72 | { | 72 | { |
| 73 | const uint8_t z[CIPHER_MAC_MAXBLOCKSIZE] = {0}; | 73 | const uint8_t z[CIPHER_MAC_MAXBLOCKSIZE] = {0}; |
| 74 | uint8_t rb; | 74 | uint8_t rb; |
| @@ -81,7 +81,7 @@ static void CMAC_Final(CRYPT_CMAC_Ctx *ctx) | |||
| 81 | ret = method->encryptBlock(ctx->key, z, l, blockSize); | 81 | ret = method->encryptBlock(ctx->key, z, l, blockSize); |
| 82 | if (ret != CRYPT_SUCCESS) { | 82 | if (ret != CRYPT_SUCCESS) { |
| 83 | BSL_ERR_PUSH_ERROR(ret); | 83 | BSL_ERR_PUSH_ERROR(ret); |
| 84 | - return; | 84 | + return ret; |
| 85 | } | 85 | } |
| 86 | LeftShiftOneBit(l, blockSize, k1); | 86 | LeftShiftOneBit(l, blockSize, k1); |
| 87 | 87 | ||
| @@ -111,6 +111,7 @@ static void CMAC_Final(CRYPT_CMAC_Ctx *ctx) | |||
| 111 | DATA_XOR(ctx->left, k2, ctx->left, blockSize); | 111 | DATA_XOR(ctx->left, k2, ctx->left, blockSize); |
| 112 | ctx->len = blockSize; | 112 | ctx->len = blockSize; |
| 113 | } | 113 | } |
| 114 | + return CRYPT_SUCCESS; | ||
| 114 | } | 115 | } |
| 115 | 116 | ||
| 116 | int32_t CRYPT_CMAC_Final(CRYPT_CMAC_Ctx *ctx, uint8_t *out, uint32_t *len) | 117 | int32_t CRYPT_CMAC_Final(CRYPT_CMAC_Ctx *ctx, uint8_t *out, uint32_t *len) |
| @@ -126,9 +127,12 @@ int32_t CRYPT_CMAC_Final(CRYPT_CMAC_Ctx *ctx, uint8_t *out, uint32_t *len) | |||
| 126 | return CRYPT_CMAC_OUT_BUFF_LEN_NOT_ENOUGH; | 127 | return CRYPT_CMAC_OUT_BUFF_LEN_NOT_ENOUGH; |
| 127 | } | 128 | } |
| 128 | 129 | ||
| 129 | - CMAC_Final(ctx); | 130 | + int32_t ret = CMAC_Final(ctx); |
| 131 | + if (ret != CRYPT_SUCCESS) { | ||
| 132 | + return ret; | ||
| 133 | + } | ||
| 130 | DATA_XOR(ctx->left, ctx->data, ctx->left, blockSize); | 134 | DATA_XOR(ctx->left, ctx->data, ctx->left, blockSize); |
| 131 | - int32_t ret = method->encryptBlock(ctx->key, ctx->left, out, blockSize); | 135 | + ret = method->encryptBlock(ctx->key, ctx->left, out, blockSize); |
| 132 | if (ret != CRYPT_SUCCESS) { | 136 | if (ret != CRYPT_SUCCESS) { |
| 133 | BSL_ERR_PUSH_ERROR(ret); | 137 | BSL_ERR_PUSH_ERROR(ret); |
| 134 | return ret; | 138 | return ret; |
| @@ -76,7 +76,7 @@ void *DECODER_##keyType##Der2KeyNewCtx(void *provCtx) \ | |||
| 76 | int32_t ret = CRYPT_EAL_SetPkeyMethod(&ctx->method, keyMethod, asyCipherMethod, exchMethod, signMethod, \ | 76 | int32_t ret = CRYPT_EAL_SetPkeyMethod(&ctx->method, keyMethod, asyCipherMethod, exchMethod, signMethod, \ |
| 77 | kemMethod); \ | 77 | kemMethod); \ |
| 78 | if (ret != CRYPT_SUCCESS) { \ | 78 | if (ret != CRYPT_SUCCESS) { \ |
| 79 | - BSL_SAL_Free(ctx); \ | 79 | + DECODER_DER2KEY_FreeCtx(ctx); \ |
| 80 | return NULL; \ | 80 | return NULL; \ |
| 81 | } \ | 81 | } \ |
| 82 | ctx->keyAlgId = keyId; \ | 82 | ctx->keyAlgId = keyId; \ |
| @@ -175,12 +175,14 @@ static int32_t CheckParams(DECODER_Der2KeyCtx *decoderCtx, const BSL_Param *inPa | |||
| 175 | 175 | ||
| 176 | static int32_t ConstructOutputParams(DECODER_Der2KeyCtx *decoderCtx, void *key, BSL_Param **outParam) | 176 | static int32_t ConstructOutputParams(DECODER_Der2KeyCtx *decoderCtx, void *key, BSL_Param **outParam) |
| 177 | { | 177 | { |
| 178 | + int32_t ret; | ||
| 178 | BSL_Param *result = BSL_SAL_Calloc(7, sizeof(BSL_Param)); | 179 | BSL_Param *result = BSL_SAL_Calloc(7, sizeof(BSL_Param)); |
| 179 | if (result == NULL) { | 180 | if (result == NULL) { |
| 181 | + ret = CRYPT_MEM_ALLOC_FAIL; | ||
| 180 | BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); | 182 | BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); |
| 181 | - return CRYPT_MEM_ALLOC_FAIL; | 183 | + goto EXIT; |
| 182 | } | 184 | } |
| 183 | - int32_t ret = BSL_PARAM_InitValue(&result[0], CRYPT_PARAM_DECODE_OBJECT_DATA, BSL_PARAM_TYPE_CTX_PTR, key, 0); | 185 | + ret = BSL_PARAM_InitValue(&result[0], CRYPT_PARAM_DECODE_OBJECT_DATA, BSL_PARAM_TYPE_CTX_PTR, key, 0); |
| 184 | if (ret != CRYPT_SUCCESS) { | 186 | if (ret != CRYPT_SUCCESS) { |
| 185 | BSL_ERR_PUSH_ERROR(ret); | 187 | BSL_ERR_PUSH_ERROR(ret); |
| 186 | goto EXIT; | 188 | goto EXIT; |
| @@ -112,7 +112,11 @@ int32_t DECODER_EPKI2PKI_Decode(void *ctx, const BSL_Param *inParam, BSL_Param * | |||
| 112 | BSL_ERR_PUSH_ERROR(ret); | 112 | BSL_ERR_PUSH_ERROR(ret); |
| 113 | return ret; | 113 | return ret; |
| 114 | } | 114 | } |
| 115 | - return CRYPT_DECODE_ConstructBufferOutParam(NULL, outParam, decode.data, decode.dataLen); | 115 | + ret = CRYPT_DECODE_ConstructBufferOutParam(NULL, outParam, decode.data, decode.dataLen); |
| 116 | + if (ret != CRYPT_SUCCESS) { | ||
| 117 | + BSL_SAL_ClearFree(decode.data, decode.dataLen); | ||
| 118 | + } | ||
| 119 | + return ret; | ||
| 116 | } | 120 | } |
| 117 | 121 | ||
| 118 | void DECODER_EPKI2PKI_FreeCtx(void *ctx) | 122 | void DECODER_EPKI2PKI_FreeCtx(void *ctx) |
| @@ -277,6 +277,7 @@ int32_t DECODER_LowKeyObject2PkeyObjectDecode(void *ctx, const BSL_Param *inPara | |||
| 277 | } | 277 | } |
| 278 | CRYPT_EAL_PkeyCtx *ealPKey = CRYPT_EAL_MakeKeyByPkeyAlgInfo(&pkeyAlgInfo, targetKeyRef, sizeof(void *)); | 278 | CRYPT_EAL_PkeyCtx *ealPKey = CRYPT_EAL_MakeKeyByPkeyAlgInfo(&pkeyAlgInfo, targetKeyRef, sizeof(void *)); |
| 279 | if (ealPKey == NULL) { | 279 | if (ealPKey == NULL) { |
| 280 | + ret = CRYPT_MEM_ALLOC_FAIL; | ||
| 280 | BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); | 281 | BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); |
| 281 | goto EXIT; | 282 | goto EXIT; |
| 282 | } | 283 | } |
| @@ -98,12 +98,15 @@ int32_t DECODER_Pem2DerDecode(void *ctx, const BSL_Param *inParam, BSL_Param **o | |||
| 98 | 98 | ||
| 99 | ret = BSL_PEM_DecodePemToAsn1((char **)&encode.data, &encode.dataLen, &symbol, &asn1Encode, &asn1Len); | 99 | ret = BSL_PEM_DecodePemToAsn1((char **)&encode.data, &encode.dataLen, &symbol, &asn1Encode, &asn1Len); |
| 100 | if (ret != CRYPT_SUCCESS) { | 100 | if (ret != CRYPT_SUCCESS) { |
| 101 | - BSL_SAL_Free(asn1Encode); | ||
| 102 | BSL_ERR_PUSH_ERROR(ret); | 101 | BSL_ERR_PUSH_ERROR(ret); |
| 103 | return ret; | 102 | return ret; |
| 104 | } | 103 | } |
| 105 | decoderCtx->outType = dataType; | 104 | decoderCtx->outType = dataType; |
| 106 | - return CRYPT_DECODE_ConstructBufferOutParam(inParam, outParam, asn1Encode, asn1Len); | 105 | + ret = CRYPT_DECODE_ConstructBufferOutParam(inParam, outParam, asn1Encode, asn1Len); |
| 106 | + if (ret != CRYPT_SUCCESS) { | ||
| 107 | + BSL_SAL_Free(asn1Encode); | ||
| 108 | + } | ||
| 109 | + return ret; | ||
| 107 | } | 110 | } |
| 108 | 111 | ||
| 109 | void DECODER_Pem2DerFreeOutData(void *ctx, BSL_Param *outParam) | 112 | void DECODER_Pem2DerFreeOutData(void *ctx, BSL_Param *outParam) |
| @@ -164,6 +164,11 @@ int32_t CRYPT_RSA_ParsePubkeyAsn1Buff(void *libCtx, uint8_t *buff, uint32_t buff | |||
| 164 | BSL_PARAM_END | 164 | BSL_PARAM_END |
| 165 | }; | 165 | }; |
| 166 | ret = CRYPT_RSA_SetPubKey(pctx, pubParam); | 166 | ret = CRYPT_RSA_SetPubKey(pctx, pubParam); |
| 167 | + if (ret != CRYPT_SUCCESS) { | ||
| 168 | + CRYPT_RSA_FreeCtx(pctx); | ||
| 169 | + BSL_ERR_PUSH_ERROR(ret); | ||
| 170 | + return ret; | ||
| 171 | + } | ||
| 167 | if (cid != BSL_CID_RSASSAPSS) { | 172 | if (cid != BSL_CID_RSASSAPSS) { |
| 168 | *rsaPubKey = pctx; | 173 | *rsaPubKey = pctx; |
| 169 | return CRYPT_SUCCESS; | 174 | return CRYPT_SUCCESS; |
| @@ -328,7 +328,7 @@ int32_t CRYPT_DECODE_PrikeyAsn1Buff(uint8_t *buffer, uint32_t bufferLen, BSL_ASN | |||
| 328 | uint32_t tmpBuffLen = bufferLen; | 328 | uint32_t tmpBuffLen = bufferLen; |
| 329 | BSL_ASN1_Template templ = {g_ecPriKeyTempl, sizeof(g_ecPriKeyTempl) / sizeof(g_ecPriKeyTempl[0])}; | 329 | BSL_ASN1_Template templ = {g_ecPriKeyTempl, sizeof(g_ecPriKeyTempl) / sizeof(g_ecPriKeyTempl[0])}; |
| 330 | int32_t ret = BSL_ASN1_DecodeTemplate(&templ, NULL, &tmpBuff, &tmpBuffLen, asn1, arrNum); | 330 | int32_t ret = BSL_ASN1_DecodeTemplate(&templ, NULL, &tmpBuff, &tmpBuffLen, asn1, arrNum); |
| 331 | - if (ret != CRYPT_SUCCESS) { | 331 | + if (ret != BSL_SUCCESS) { |
| 332 | BSL_ERR_PUSH_ERROR(ret); | 332 | BSL_ERR_PUSH_ERROR(ret); |
| 333 | } | 333 | } |
| 334 | return ret; | 334 | return ret; |
| @@ -348,7 +348,7 @@ int32_t CRYPT_DECODE_RsaPubkeyAsn1Buff(uint8_t *buff, uint32_t buffLen, BSL_ASN1 | |||
| 348 | 348 | ||
| 349 | BSL_ASN1_Template pubTempl = {g_rsaPubTempl, sizeof(g_rsaPubTempl) / sizeof(g_rsaPubTempl[0])}; | 349 | BSL_ASN1_Template pubTempl = {g_rsaPubTempl, sizeof(g_rsaPubTempl) / sizeof(g_rsaPubTempl[0])}; |
| 350 | int32_t ret = BSL_ASN1_DecodeTemplate(&pubTempl, NULL, &tmpBuff, &tmpBuffLen, pubAsn1, arrNum); | 350 | int32_t ret = BSL_ASN1_DecodeTemplate(&pubTempl, NULL, &tmpBuff, &tmpBuffLen, pubAsn1, arrNum); |
| 351 | - if (ret != CRYPT_SUCCESS) { | 351 | + if (ret != BSL_SUCCESS) { |
| 352 | BSL_ERR_PUSH_ERROR(ret); | 352 | BSL_ERR_PUSH_ERROR(ret); |
| 353 | } | 353 | } |
| 354 | 354 | ||
| @@ -362,7 +362,7 @@ int32_t CRYPT_DECODE_RsaPrikeyAsn1Buff(uint8_t *buff, uint32_t buffLen, BSL_ASN1 | |||
| 362 | 362 | ||
| 363 | BSL_ASN1_Template templ = {g_rsaPrvTempl, sizeof(g_rsaPrvTempl) / sizeof(g_rsaPrvTempl[0])}; | 363 | BSL_ASN1_Template templ = {g_rsaPrvTempl, sizeof(g_rsaPrvTempl) / sizeof(g_rsaPrvTempl[0])}; |
| 364 | int32_t ret = BSL_ASN1_DecodeTemplate(&templ, NULL, &tmpBuff, &tmpBuffLen, asn1, asn1Num); | 364 | int32_t ret = BSL_ASN1_DecodeTemplate(&templ, NULL, &tmpBuff, &tmpBuffLen, asn1, asn1Num); |
| 365 | - if (ret != CRYPT_SUCCESS) { | 365 | + if (ret != BSL_SUCCESS) { |
| 366 | BSL_ERR_PUSH_ERROR(ret); | 366 | BSL_ERR_PUSH_ERROR(ret); |
| 367 | } | 367 | } |
| 368 | return ret; | 368 | return ret; |
| @@ -481,7 +481,7 @@ int32_t CRYPT_DECODE_ParseSubKeyInfo(uint8_t *buff, uint32_t buffLen, BSL_ASN1_B | |||
| 481 | } | 481 | } |
| 482 | int32_t ret = BSL_ASN1_DecodeTemplate(&pubTempl, DecSubKeyInfoCb, &tmpBuff, &tmpBuffLen, pubAsn1, | 482 | int32_t ret = BSL_ASN1_DecodeTemplate(&pubTempl, DecSubKeyInfoCb, &tmpBuff, &tmpBuffLen, pubAsn1, |
| 483 | CRYPT_SUBKEYINFO_BITSTRING_IDX + 1); | 483 | CRYPT_SUBKEYINFO_BITSTRING_IDX + 1); |
| 484 | - if (ret != CRYPT_SUCCESS) { | 484 | + if (ret != BSL_SUCCESS) { |
| 485 | BSL_ERR_PUSH_ERROR(ret); | 485 | BSL_ERR_PUSH_ERROR(ret); |
| 486 | } | 486 | } |
| 487 | return ret; | 487 | return ret; |
| @@ -520,7 +520,7 @@ int32_t CRYPT_DECODE_SubPubkey(uint8_t *buff, uint32_t buffLen, BSL_ASN1_DecTemp | |||
| 520 | BSL_ASN1_Buffer *oid = algoId; | 520 | BSL_ASN1_Buffer *oid = algoId; |
| 521 | BSL_ASN1_Buffer *pubkey = &pubAsn1[CRYPT_SUBKEYINFO_BITSTRING_IDX]; | 521 | BSL_ASN1_Buffer *pubkey = &pubAsn1[CRYPT_SUBKEYINFO_BITSTRING_IDX]; |
| 522 | ret = BSL_ASN1_DecodePrimitiveItem(pubkey, &bitPubkey); | 522 | ret = BSL_ASN1_DecodePrimitiveItem(pubkey, &bitPubkey); |
| 523 | - if (ret != CRYPT_SUCCESS) { | 523 | + if (ret != BSL_SUCCESS) { |
| 524 | BSL_ERR_PUSH_ERROR(ret); | 524 | BSL_ERR_PUSH_ERROR(ret); |
| 525 | return ret; | 525 | return ret; |
| 526 | } | 526 | } |
| @@ -574,12 +574,12 @@ int32_t CRYPT_DECODE_Pkcs8Info(uint8_t *buff, uint32_t buffLen, BSL_ASN1_DecTemp | |||
| 574 | BSL_ASN1_Buffer asn1[CRYPT_PK8_PRIKEY_PRIKEY_IDX + 1] = {0}; | 574 | BSL_ASN1_Buffer asn1[CRYPT_PK8_PRIKEY_PRIKEY_IDX + 1] = {0}; |
| 575 | BSL_ASN1_Template templ = {g_pk8PriKeyTempl, sizeof(g_pk8PriKeyTempl) / sizeof(g_pk8PriKeyTempl[0])}; | 575 | BSL_ASN1_Template templ = {g_pk8PriKeyTempl, sizeof(g_pk8PriKeyTempl) / sizeof(g_pk8PriKeyTempl[0])}; |
| 576 | int32_t ret = BSL_ASN1_DecodeTemplate(&templ, NULL, &tmpBuff, &tmpBuffLen, asn1, CRYPT_PK8_PRIKEY_PRIKEY_IDX + 1); | 576 | int32_t ret = BSL_ASN1_DecodeTemplate(&templ, NULL, &tmpBuff, &tmpBuffLen, asn1, CRYPT_PK8_PRIKEY_PRIKEY_IDX + 1); |
| 577 | - if (ret != CRYPT_SUCCESS) { | 577 | + if (ret != BSL_SUCCESS) { |
| 578 | BSL_ERR_PUSH_ERROR(ret); | 578 | BSL_ERR_PUSH_ERROR(ret); |
| 579 | return ret; | 579 | return ret; |
| 580 | } | 580 | } |
| 581 | ret = BSL_ASN1_DecodePrimitiveItem(&asn1[CRYPT_PK8_PRIKEY_VERSION_IDX], &version); | 581 | ret = BSL_ASN1_DecodePrimitiveItem(&asn1[CRYPT_PK8_PRIKEY_VERSION_IDX], &version); |
| 582 | - if (ret != CRYPT_SUCCESS) { | 582 | + if (ret != BSL_SUCCESS) { |
| 583 | BSL_ERR_PUSH_ERROR(ret); | 583 | BSL_ERR_PUSH_ERROR(ret); |
| 584 | return ret; | 584 | return ret; |
| 585 | } | 585 | } |
| @@ -631,7 +631,7 @@ static int32_t ParseDeriveKeyParam(BSL_Buffer *derivekeyData, uint32_t *iter, ui | |||
| 631 | BSL_ASN1_Template templ = {g_pbkdf2DerParamTempl, sizeof(g_pbkdf2DerParamTempl) / sizeof(g_pbkdf2DerParamTempl[0])}; | 631 | BSL_ASN1_Template templ = {g_pbkdf2DerParamTempl, sizeof(g_pbkdf2DerParamTempl) / sizeof(g_pbkdf2DerParamTempl[0])}; |
| 632 | int32_t ret = BSL_ASN1_DecodeTemplate(&templ, NULL, | 632 | int32_t ret = BSL_ASN1_DecodeTemplate(&templ, NULL, |
| 633 | &tmpBuff, &tmpBuffLen, derParam, CRYPT_PKCS_ENC_DERPARAM_MAX); | 633 | &tmpBuff, &tmpBuffLen, derParam, CRYPT_PKCS_ENC_DERPARAM_MAX); |
| 634 | - if (ret != CRYPT_SUCCESS) { | 634 | + if (ret != BSL_SUCCESS) { |
| 635 | BSL_ERR_PUSH_ERROR(ret); | 635 | BSL_ERR_PUSH_ERROR(ret); |
| 636 | return ret; | 636 | return ret; |
| 637 | } | 637 | } |
| @@ -731,7 +731,7 @@ int32_t CRYPT_DECODE_Pkcs8PrvDecrypt(CRYPT_EAL_LibCtx *libctx, const char *attrN | |||
| 731 | BSL_ASN1_Buffer asn1[CRYPT_PKCS_ENCPRIKEY_MAX] = {0}; | 731 | BSL_ASN1_Buffer asn1[CRYPT_PKCS_ENCPRIKEY_MAX] = {0}; |
| 732 | BSL_ASN1_Template templ = {g_pk8EncPriKeyTempl, sizeof(g_pk8EncPriKeyTempl) / sizeof(g_pk8EncPriKeyTempl[0])}; | 732 | BSL_ASN1_Template templ = {g_pk8EncPriKeyTempl, sizeof(g_pk8EncPriKeyTempl) / sizeof(g_pk8EncPriKeyTempl[0])}; |
| 733 | int32_t ret = BSL_ASN1_DecodeTemplate(&templ, NULL, &tmpBuff, &tmpBuffLen, asn1, CRYPT_PKCS_ENCPRIKEY_MAX); | 733 | int32_t ret = BSL_ASN1_DecodeTemplate(&templ, NULL, &tmpBuff, &tmpBuffLen, asn1, CRYPT_PKCS_ENCPRIKEY_MAX); |
| 734 | - if (ret != CRYPT_SUCCESS) { | 734 | + if (ret != BSL_SUCCESS) { |
| 735 | BSL_ERR_PUSH_ERROR(ret); | 735 | BSL_ERR_PUSH_ERROR(ret); |
| 736 | return ret; | 736 | return ret; |
| 737 | } | 737 | } |
| @@ -784,14 +784,14 @@ int32_t CRYPT_DECODE_ConstructBufferOutParam(const BSL_Param *inParam, BSL_Param | |||
| 784 | } | 784 | } |
| 785 | int32_t ret = BSL_PARAM_InitValue(&result[0], CRYPT_PARAM_DECODE_BUFFER_DATA, BSL_PARAM_TYPE_OCTETS, | 785 | int32_t ret = BSL_PARAM_InitValue(&result[0], CRYPT_PARAM_DECODE_BUFFER_DATA, BSL_PARAM_TYPE_OCTETS, |
| 786 | buffer, bufferLen); | 786 | buffer, bufferLen); |
| 787 | - if (ret != CRYPT_SUCCESS) { | 787 | + if (ret != BSL_SUCCESS) { |
| 788 | BSL_SAL_Free(result); | 788 | BSL_SAL_Free(result); |
| 789 | BSL_ERR_PUSH_ERROR(ret); | 789 | BSL_ERR_PUSH_ERROR(ret); |
| 790 | return ret; | 790 | return ret; |
| 791 | } | 791 | } |
| 792 | if (encParam != NULL) { | 792 | if (encParam != NULL) { |
| 793 | ret = BSL_PARAM_InitValue(&result[1], encParam->key, encParam->valueType, encParam->value, encParam->valueLen); | 793 | ret = BSL_PARAM_InitValue(&result[1], encParam->key, encParam->valueType, encParam->value, encParam->valueLen); |
| 794 | - if (ret != CRYPT_SUCCESS) { | 794 | + if (ret != BSL_SUCCESS) { |
| 795 | BSL_SAL_Free(result); | 795 | BSL_SAL_Free(result); |
| 796 | BSL_ERR_PUSH_ERROR(ret); | 796 | BSL_ERR_PUSH_ERROR(ret); |
| 797 | return ret; | 797 | return ret; |
| @@ -821,7 +821,7 @@ int32_t CRYPT_ENCODE_RsaPubkeyAsn1Buff(BSL_ASN1_Buffer *pubAsn1, BSL_Buffer *enc | |||
| 821 | if (ret != CRYPT_SUCCESS) { | 821 | if (ret != CRYPT_SUCCESS) { |
| 822 | BSL_ERR_PUSH_ERROR(ret); | 822 | BSL_ERR_PUSH_ERROR(ret); |
| 823 | } | 823 | } |
| 824 | - return CRYPT_SUCCESS; | 824 | + return ret; |
| 825 | } | 825 | } |
| 826 | 826 | ||
| 827 | int32_t CRYPT_ENCODE_RsaPrikeyAsn1Buff(BSL_ASN1_Buffer *asn1, uint32_t asn1Num, BSL_Buffer *encode) | 827 | int32_t CRYPT_ENCODE_RsaPrikeyAsn1Buff(BSL_ASN1_Buffer *asn1, uint32_t asn1Num, BSL_Buffer *encode) |
| @@ -858,7 +858,7 @@ int32_t CRYPT_ENCODE_SubPubkeyByInfo(BSL_ASN1_Buffer *algo, BSL_Buffer *bitStr, | |||
| 858 | } | 858 | } |
| 859 | int32_t ret = BSL_ASN1_EncodeTemplate(&pubTempl, | 859 | int32_t ret = BSL_ASN1_EncodeTemplate(&pubTempl, |
| 860 | encode, CRYPT_SUBKEYINFO_BITSTRING_IDX + 1, &encodeH->data, &encodeH->dataLen); | 860 | encode, CRYPT_SUBKEYINFO_BITSTRING_IDX + 1, &encodeH->data, &encodeH->dataLen); |
| 861 | - if (ret != CRYPT_SUCCESS) { | 861 | + if (ret != BSL_SUCCESS) { |
| 862 | BSL_ERR_PUSH_ERROR(ret); | 862 | BSL_ERR_PUSH_ERROR(ret); |
| 863 | } | 863 | } |
| 864 | return ret; | 864 | return ret; |
| @@ -897,14 +897,14 @@ static int32_t EncodeDeriveKeyParam(CRYPT_EAL_LibCtx *libCtx, CRYPT_Pbkdf2Param | |||
| 897 | derParam[CRYPT_PKCS_ENC_DERSALT_IDX].tag = BSL_ASN1_TAG_OCTETSTRING; | 897 | derParam[CRYPT_PKCS_ENC_DERSALT_IDX].tag = BSL_ASN1_TAG_OCTETSTRING; |
| 898 | /* iter */ | 898 | /* iter */ |
| 899 | ret = BSL_ASN1_EncodeLimb(BSL_ASN1_TAG_INTEGER, param->itCnt, &derParam[CRYPT_PKCS_ENC_DERITER_IDX]); | 899 | ret = BSL_ASN1_EncodeLimb(BSL_ASN1_TAG_INTEGER, param->itCnt, &derParam[CRYPT_PKCS_ENC_DERITER_IDX]); |
| 900 | - if (ret != CRYPT_SUCCESS) { | 900 | + if (ret != BSL_SUCCESS) { |
| 901 | BSL_ERR_PUSH_ERROR(ret); | 901 | BSL_ERR_PUSH_ERROR(ret); |
| 902 | return ret; | 902 | return ret; |
| 903 | } | 903 | } |
| 904 | BSL_ASN1_Template templ = {g_pbkdf2DerParamTempl, sizeof(g_pbkdf2DerParamTempl) / sizeof(g_pbkdf2DerParamTempl[0])}; | 904 | BSL_ASN1_Template templ = {g_pbkdf2DerParamTempl, sizeof(g_pbkdf2DerParamTempl) / sizeof(g_pbkdf2DerParamTempl[0])}; |
| 905 | if (param->hmacId == CRYPT_MAC_HMAC_SHA1) { | 905 | if (param->hmacId == CRYPT_MAC_HMAC_SHA1) { |
| 906 | ret = BSL_ASN1_EncodeTemplate(&templ, derParam, CRYPT_PKCS_ENC_DERPRF_IDX + 1, &encode->data, &encode->dataLen); | 906 | ret = BSL_ASN1_EncodeTemplate(&templ, derParam, CRYPT_PKCS_ENC_DERPRF_IDX + 1, &encode->data, &encode->dataLen); |
| 907 | - if (ret != CRYPT_SUCCESS) { | 907 | + if (ret != BSL_SUCCESS) { |
| 908 | BSL_ERR_PUSH_ERROR(ret); | 908 | BSL_ERR_PUSH_ERROR(ret); |
| 909 | } | 909 | } |
| 910 | BSL_SAL_FREE(derParam[CRYPT_PKCS_ENC_DERITER_IDX].buff); | 910 | BSL_SAL_FREE(derParam[CRYPT_PKCS_ENC_DERITER_IDX].buff); |
| @@ -496,7 +496,7 @@ int32_t MODES_XTS_InitCtxEx(MODES_XTS_Ctx *modeCtx, const uint8_t *key, uint32_t | |||
| 496 | 496 | ||
| 497 | int32_t MODES_XTS_UpdateEx(MODES_XTS_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) | 497 | int32_t MODES_XTS_UpdateEx(MODES_XTS_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) |
| 498 | { | 498 | { |
| 499 | - if (modeCtx == NULL || modeCtx->xtsCtx.ciphMeth == NULL) { | 499 | + if (modeCtx == NULL) { |
| 500 | BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); | 500 | BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); |
| 501 | return CRYPT_NULL_INPUT; | 501 | return CRYPT_NULL_INPUT; |
| 502 | } | 502 | } |
| @@ -8,4 +8,4 @@ SDV_BSL_OBJ_CREATE_TC001 | |||
| 8 | SDV_BSL_OBJ_CREATE_TC001: | 8 | SDV_BSL_OBJ_CREATE_TC001: |
| 9 | 9 | ||
| 10 | SDV_BSL_OBJ_HASH_TABLE_LOOKUP_TC001 | 10 | SDV_BSL_OBJ_HASH_TABLE_LOOKUP_TC001 |
| 11 | -SDV_BSL_OBJ_HASH_TABLE_LOOKUP_TC001: | 11 | +SDV_BSL_OBJ_HASH_TABLE_LOOKUP_TC001: |
| @@ -38,6 +38,7 @@ | |||
| 38 | 38 | ||
| 39 | 39 | ||
| 40 | 40 | ||
| 41 | + | ||
| 41 | /* END_HEADER */ | 42 | /* END_HEADER */ |
| 42 | 43 | ||
| 43 | // clang-format off | 44 | // clang-format off |
| @@ -913,3 +914,65 @@ EXIT: | |||
| 913 | 914 | ||
| 914 | } | 915 | } |
| 915 | /* END_CASE */ | 916 | /* END_CASE */ |
| 917 | + | ||
| 918 | + | ||
| 919 | + | ||
| 920 | +static int32_t test = 0; | ||
| 921 | +static int32_t marked = 0; | ||
| 922 | +static void *STUB_BSL_SAL_Calloc(uint32_t num, uint32_t size) | ||
| 923 | +{ | ||
| 924 | + if (marked <= test) { | ||
| 925 | + marked++; | ||
| 926 | + return calloc(num, size); | ||
| 927 | + } | ||
| 928 | + return NULL; | ||
| 929 | +} | ||
| 930 | + | ||
| 931 | + | ||
| 932 | + | ||
| 933 | +/** | ||
| 934 | + * @test SDV_BSL_ASN1_PARSE_BUFF_STUB_TC001 | ||
| 935 | + * title 1. Test the decode provider with stub malloc fail | ||
| 936 | + * | ||
| 937 | + */ | ||
| 938 | +/* BEGIN_CASE */ | ||
| 939 | +void SDV_BSL_ASN1_PARSE_BUFF_STUB_TC001(char *formatStr, char *typeStr, char *path, Hex *password, int maxTriggers) | ||
| 940 | +{ | ||
| 941 | + | ||
| 942 | + (void)formatStr; | ||
| 943 | + (void)typeStr; | ||
| 944 | + (void)path; | ||
| 945 | + (void)password; | ||
| 946 | + (void)maxTriggers; | ||
| 947 | + SKIP_TEST(); | ||
| 948 | + | ||
| 949 | + CRYPT_EAL_Init(CRYPT_EAL_INIT_CPU|CRYPT_EAL_INIT_PROVIDER|CRYPT_EAL_INIT_PROVIDER_RAND); | ||
| 950 | + CRYPT_RandRegist(RandFunc); | ||
| 951 | + CRYPT_RandRegistEx(RandFuncEx); | ||
| 952 | + uint8_t *data = NULL; | ||
| 953 | + uint32_t dataLen = 0; | ||
| 954 | + FuncStubInfo tmpRpInfo; | ||
| 955 | + ASSERT_EQ(BSL_SAL_ReadFile(path, &data, &dataLen), BSL_SUCCESS); | ||
| 956 | + BSL_Buffer encode = {data, dataLen}; | ||
| 957 | + BSL_Buffer pass = {password->x, password->len}; | ||
| 958 | + CRYPT_EAL_PkeyCtx *pkeyCtx = NULL; | ||
| 959 | + test = maxTriggers; | ||
| 960 | + marked = 0; | ||
| 961 | + STUB_Init(); | ||
| 962 | + STUB_Replace(&tmpRpInfo, BSL_SAL_Calloc, STUB_BSL_SAL_Calloc); | ||
| 963 | + for (int i = maxTriggers; i > 0; i--) { | ||
| 964 | + marked = 0; | ||
| 965 | + test--; | ||
| 966 | + ASSERT_NE(CRYPT_EAL_ProviderDecodeBuffKey(NULL, NULL, BSL_CID_UNKNOWN, formatStr, typeStr, &encode, | ||
| 967 | + &pass, &pkeyCtx), CRYPT_SUCCESS); | ||
| 968 | + CRYPT_EAL_PkeyFreeCtx(pkeyCtx); | ||
| 969 | + pkeyCtx = NULL; | ||
| 970 | + } | ||
| 971 | +EXIT: | ||
| 972 | + printf("final marked=%d, test=%d\n", marked, test); | ||
| 973 | + STUB_Reset(&tmpRpInfo); | ||
| 974 | + BSL_SAL_FREE(data); | ||
| 975 | + CRYPT_EAL_PkeyFreeCtx(pkeyCtx); | ||
| 976 | + BSL_GLOBAL_DeInit(); | ||
| 977 | + | ||
| 978 | +} | ||
| @@ -333,3 +333,18 @@ SDV_BSL_ASN1_PARSE_BUFF_PROVIDER_TC002:"../testdata/provider/path1":"provider_se | |||
| 333 | 333 | ||
| 334 | SDV_BSL_ASN1_PARSE_BUFF_PROVIDER_TC002 Test Json2key | 334 | SDV_BSL_ASN1_PARSE_BUFF_PROVIDER_TC002 Test Json2key |
| 335 | SDV_BSL_ASN1_PARSE_BUFF_PROVIDER_TC002:"../testdata/provider/path1":"provider_self_decoder_test":BSL_SAL_LIB_FMT_LIBSO:"provider=test_decoder":"JSON":"PRIKEY_PKCS8_UNENCRYPT":"../testdata/cert/asn1/rsa_pss_salt10_json.key" | 335 | SDV_BSL_ASN1_PARSE_BUFF_PROVIDER_TC002:"../testdata/provider/path1":"provider_self_decoder_test":BSL_SAL_LIB_FMT_LIBSO:"provider=test_decoder":"JSON":"PRIKEY_PKCS8_UNENCRYPT":"../testdata/cert/asn1/rsa_pss_salt10_json.key" |
| 336 | + | ||
| 337 | +SDV_BSL_ASN1_PARSE_BUFF_STUB_TC001 #1 | ||
| 338 | +SDV_BSL_ASN1_PARSE_BUFF_STUB_TC001:"PEM":"PUBKEY_SUBKEY":"../testdata/cert/asn1/secp384r1pub.pem":"00":163 | ||
| 339 | + | ||
| 340 | +SDV_BSL_ASN1_PARSE_BUFF_STUB_TC001 #2 | ||
| 341 | +SDV_BSL_ASN1_PARSE_BUFF_STUB_TC001:"PEM":"PRIKEY_PKCS8_UNENCRYPT":"../testdata/cert/asn1/rsa_pss_mdsha256.key":"00":148 | ||
| 342 | + | ||
| 343 | +SDV_BSL_ASN1_PARSE_BUFF_STUB_TC001 #3 | ||
| 344 | +SDV_BSL_ASN1_PARSE_BUFF_STUB_TC001:"ASN1":"PUBKEY_RSA":"../testdata/cert/asn1/rsa2048pub_pkcs1.der":"00":113 | ||
| 345 | + | ||
| 346 | +SDV_BSL_ASN1_PARSE_BUFF_STUB_TC001 #4 | ||
| 347 | +SDV_BSL_ASN1_PARSE_BUFF_STUB_TC001:"ASN1":"PRIKEY_PKCS8_ENCRYPT":"../testdata/cert/asn1/rsa2048key_pkcs8_sm4enc.der":"31323334":159 | ||
| 348 | + | ||
| 349 | +SDV_BSL_ASN1_PARSE_BUFF_STUB_TC001 #5 | ||
| 350 | +SDV_BSL_ASN1_PARSE_BUFF_STUB_TC001:"ASN1":"PRIKEY_RSA":"../testdata/cert/asn1/rsa2048key_pkcs1.der":"00":145 | ||
| @@ -1237,17 +1237,17 @@ static int32_t SetAllCrl(HITLS_X509_Crl *crl, HITLS_X509_Cert *cert, bool includ | |||
| 1237 | ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_BEFORE_TIME, &beforeTime, sizeof(BSL_TIME)), | 1237 | ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_BEFORE_TIME, &beforeTime, sizeof(BSL_TIME)), |
| 1238 | HITLS_PKI_SUCCESS); | 1238 | HITLS_PKI_SUCCESS); |
| 1239 | 1239 | ||
| 1240 | + // Set nextUpdate period | ||
| 1241 | + afterTime = beforeTime; | ||
| 1242 | + afterTime.year += 1; | ||
| 1243 | + ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_AFTER_TIME, &afterTime, sizeof(BSL_TIME)), | ||
| 1244 | + HITLS_PKI_SUCCESS); | ||
| 1245 | + | ||
| 1240 | if (includeOptional) { | 1246 | if (includeOptional) { |
| 1241 | // Set CRL version | 1247 | // Set CRL version |
| 1242 | uint32_t version = 1; | 1248 | uint32_t version = 1; |
| 1243 | ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_VERSION, &version, sizeof(version)), HITLS_PKI_SUCCESS); | 1249 | ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_VERSION, &version, sizeof(version)), HITLS_PKI_SUCCESS); |
| 1244 | 1250 | ||
| 1245 | - // Set nextUpdate period | ||
| 1246 | - afterTime = beforeTime; | ||
| 1247 | - afterTime.year += 1; | ||
| 1248 | - ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_AFTER_TIME, &afterTime, sizeof(BSL_TIME)), | ||
| 1249 | - HITLS_PKI_SUCCESS); | ||
| 1250 | - | ||
| 1251 | // Set revoked certificates | 1251 | // Set revoked certificates |
| 1252 | for (size_t i = 0; i < sizeof(reasonCodes)/sizeof(reasonCodes[0]); i++) { | 1252 | for (size_t i = 0; i < sizeof(reasonCodes)/sizeof(reasonCodes[0]); i++) { |
| 1253 | ASSERT_EQ(SetCrlAllRevoked(crl, reasonCodes[i], useGMT), HITLS_PKI_SUCCESS); | 1253 | ASSERT_EQ(SetCrlAllRevoked(crl, reasonCodes[i], useGMT), HITLS_PKI_SUCCESS); |